ide-tape.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  1. /*
  2. * IDE ATAPI streaming tape driver.
  3. *
  4. * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
  5. * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
  6. *
  7. * This driver was constructed as a student project in the software laboratory
  8. * of the faculty of electrical engineering in the Technion - Israel's
  9. * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
  10. *
  11. * It is hereby placed under the terms of the GNU general public license.
  12. * (See linux/COPYING).
  13. *
  14. * For a historical changelog see
  15. * Documentation/ide/ChangeLog.ide-tape.1995-2002
  16. */
  17. #define DRV_NAME "ide-tape"
  18. #define IDETAPE_VERSION "1.20"
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/string.h>
  22. #include <linux/kernel.h>
  23. #include <linux/delay.h>
  24. #include <linux/timer.h>
  25. #include <linux/mm.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/major.h>
  29. #include <linux/errno.h>
  30. #include <linux/genhd.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/slab.h>
  33. #include <linux/pci.h>
  34. #include <linux/ide.h>
  35. #include <linux/completion.h>
  36. #include <linux/bitops.h>
  37. #include <linux/mutex.h>
  38. #include <scsi/scsi.h>
  39. #include <asm/byteorder.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/io.h>
  42. #include <asm/unaligned.h>
  43. #include <linux/mtio.h>
  44. /* define to see debug info */
  45. #undef IDETAPE_DEBUG_LOG
  46. #ifdef IDETAPE_DEBUG_LOG
  47. #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
  48. #else
  49. #define ide_debug_log(lvl, fmt, args...) do {} while (0)
  50. #endif
  51. /**************************** Tunable parameters *****************************/
  52. /*
  53. * After each failed packet command we issue a request sense command and retry
  54. * the packet command IDETAPE_MAX_PC_RETRIES times.
  55. *
  56. * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
  57. */
  58. #define IDETAPE_MAX_PC_RETRIES 3
  59. /*
  60. * The following parameter is used to select the point in the internal tape fifo
  61. * in which we will start to refill the buffer. Decreasing the following
  62. * parameter will improve the system's latency and interactive response, while
  63. * using a high value might improve system throughput.
  64. */
  65. #define IDETAPE_FIFO_THRESHOLD 2
  66. /*
  67. * DSC polling parameters.
  68. *
  69. * Polling for DSC (a single bit in the status register) is a very important
  70. * function in ide-tape. There are two cases in which we poll for DSC:
  71. *
  72. * 1. Before a read/write packet command, to ensure that we can transfer data
  73. * from/to the tape's data buffers, without causing an actual media access.
  74. * In case the tape is not ready yet, we take out our request from the device
  75. * request queue, so that ide.c could service requests from the other device
  76. * on the same interface in the meantime.
  77. *
  78. * 2. After the successful initialization of a "media access packet command",
  79. * which is a command that can take a long time to complete (the interval can
  80. * range from several seconds to even an hour). Again, we postpone our request
  81. * in the middle to free the bus for the other device. The polling frequency
  82. * here should be lower than the read/write frequency since those media access
  83. * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
  84. * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
  85. * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
  86. *
  87. * We also set a timeout for the timer, in case something goes wrong. The
  88. * timeout should be longer then the maximum execution time of a tape operation.
  89. */
  90. /* DSC timings. */
  91. #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
  92. #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
  93. #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
  94. #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
  95. #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
  96. #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
  97. #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
  98. /*************************** End of tunable parameters ***********************/
  99. /* tape directions */
  100. enum {
  101. IDETAPE_DIR_NONE = (1 << 0),
  102. IDETAPE_DIR_READ = (1 << 1),
  103. IDETAPE_DIR_WRITE = (1 << 2),
  104. };
  105. /* Tape door status */
  106. #define DOOR_UNLOCKED 0
  107. #define DOOR_LOCKED 1
  108. #define DOOR_EXPLICITLY_LOCKED 2
  109. /* Some defines for the SPACE command */
  110. #define IDETAPE_SPACE_OVER_FILEMARK 1
  111. #define IDETAPE_SPACE_TO_EOD 3
  112. /* Some defines for the LOAD UNLOAD command */
  113. #define IDETAPE_LU_LOAD_MASK 1
  114. #define IDETAPE_LU_RETENSION_MASK 2
  115. #define IDETAPE_LU_EOT_MASK 4
  116. /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
  117. #define IDETAPE_BLOCK_DESCRIPTOR 0
  118. #define IDETAPE_CAPABILITIES_PAGE 0x2a
  119. /*
  120. * Most of our global data which we need to save even as we leave the driver due
  121. * to an interrupt or a timer event is stored in the struct defined below.
  122. */
  123. typedef struct ide_tape_obj {
  124. ide_drive_t *drive;
  125. struct ide_driver *driver;
  126. struct gendisk *disk;
  127. struct device dev;
  128. /* used by REQ_IDETAPE_{READ,WRITE} requests */
  129. struct ide_atapi_pc queued_pc;
  130. /*
  131. * DSC polling variables.
  132. *
  133. * While polling for DSC we use postponed_rq to postpone the current
  134. * request so that ide.c will be able to service pending requests on the
  135. * other device. Note that at most we will have only one DSC (usually
  136. * data transfer) request in the device request queue.
  137. */
  138. bool postponed_rq;
  139. /* The time in which we started polling for DSC */
  140. unsigned long dsc_polling_start;
  141. /* Timer used to poll for dsc */
  142. struct timer_list dsc_timer;
  143. /* Read/Write dsc polling frequency */
  144. unsigned long best_dsc_rw_freq;
  145. unsigned long dsc_poll_freq;
  146. unsigned long dsc_timeout;
  147. /* Read position information */
  148. u8 partition;
  149. /* Current block */
  150. unsigned int first_frame;
  151. /* Last error information */
  152. u8 sense_key, asc, ascq;
  153. /* Character device operation */
  154. unsigned int minor;
  155. /* device name */
  156. char name[4];
  157. /* Current character device data transfer direction */
  158. u8 chrdev_dir;
  159. /* tape block size, usually 512 or 1024 bytes */
  160. unsigned short blk_size;
  161. int user_bs_factor;
  162. /* Copy of the tape's Capabilities and Mechanical Page */
  163. u8 caps[20];
  164. /*
  165. * Active data transfer request parameters.
  166. *
  167. * At most, there is only one ide-tape originated data transfer request
  168. * in the device request queue. This allows ide.c to easily service
  169. * requests from the other device when we postpone our active request.
  170. */
  171. /* Data buffer size chosen based on the tape's recommendation */
  172. int buffer_size;
  173. /* Staging buffer of buffer_size bytes */
  174. void *buf;
  175. /* The read/write cursor */
  176. void *cur;
  177. /* The number of valid bytes in buf */
  178. size_t valid;
  179. /* Measures average tape speed */
  180. unsigned long avg_time;
  181. int avg_size;
  182. int avg_speed;
  183. /* the door is currently locked */
  184. int door_locked;
  185. /* the tape hardware is write protected */
  186. char drv_write_prot;
  187. /* the tape is write protected (hardware or opened as read-only) */
  188. char write_prot;
  189. } idetape_tape_t;
  190. static DEFINE_MUTEX(ide_tape_mutex);
  191. static DEFINE_MUTEX(idetape_ref_mutex);
  192. static DEFINE_MUTEX(idetape_chrdev_mutex);
  193. static struct class *idetape_sysfs_class;
  194. static void ide_tape_release(struct device *);
  195. static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
  196. static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
  197. unsigned int i)
  198. {
  199. struct ide_tape_obj *tape = NULL;
  200. mutex_lock(&idetape_ref_mutex);
  201. if (cdev)
  202. tape = idetape_devs[i];
  203. else
  204. tape = ide_drv_g(disk, ide_tape_obj);
  205. if (tape) {
  206. if (ide_device_get(tape->drive))
  207. tape = NULL;
  208. else
  209. get_device(&tape->dev);
  210. }
  211. mutex_unlock(&idetape_ref_mutex);
  212. return tape;
  213. }
  214. static void ide_tape_put(struct ide_tape_obj *tape)
  215. {
  216. ide_drive_t *drive = tape->drive;
  217. mutex_lock(&idetape_ref_mutex);
  218. put_device(&tape->dev);
  219. ide_device_put(drive);
  220. mutex_unlock(&idetape_ref_mutex);
  221. }
  222. /*
  223. * called on each failed packet command retry to analyze the request sense. We
  224. * currently do not utilize this information.
  225. */
  226. static void idetape_analyze_error(ide_drive_t *drive)
  227. {
  228. idetape_tape_t *tape = drive->driver_data;
  229. struct ide_atapi_pc *pc = drive->failed_pc;
  230. struct request *rq = drive->hwif->rq;
  231. u8 *sense = bio_data(rq->bio);
  232. tape->sense_key = sense[2] & 0xF;
  233. tape->asc = sense[12];
  234. tape->ascq = sense[13];
  235. ide_debug_log(IDE_DBG_FUNC,
  236. "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
  237. rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
  238. /* correct remaining bytes to transfer */
  239. if (pc->flags & PC_FLAG_DMA_ERROR)
  240. scsi_req(rq)->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
  241. /*
  242. * If error was the result of a zero-length read or write command,
  243. * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
  244. * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
  245. */
  246. if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
  247. /* length == 0 */
  248. && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
  249. if (tape->sense_key == 5) {
  250. /* don't report an error, everything's ok */
  251. pc->error = 0;
  252. /* don't retry read/write */
  253. pc->flags |= PC_FLAG_ABORT;
  254. }
  255. }
  256. if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
  257. pc->error = IDE_DRV_ERROR_FILEMARK;
  258. pc->flags |= PC_FLAG_ABORT;
  259. }
  260. if (pc->c[0] == WRITE_6) {
  261. if ((sense[2] & 0x40) || (tape->sense_key == 0xd
  262. && tape->asc == 0x0 && tape->ascq == 0x2)) {
  263. pc->error = IDE_DRV_ERROR_EOD;
  264. pc->flags |= PC_FLAG_ABORT;
  265. }
  266. }
  267. if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
  268. if (tape->sense_key == 8) {
  269. pc->error = IDE_DRV_ERROR_EOD;
  270. pc->flags |= PC_FLAG_ABORT;
  271. }
  272. if (!(pc->flags & PC_FLAG_ABORT) &&
  273. (blk_rq_bytes(rq) - scsi_req(rq)->resid_len))
  274. pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
  275. }
  276. }
  277. static void ide_tape_handle_dsc(ide_drive_t *);
  278. static int ide_tape_callback(ide_drive_t *drive, int dsc)
  279. {
  280. idetape_tape_t *tape = drive->driver_data;
  281. struct ide_atapi_pc *pc = drive->pc;
  282. struct request *rq = drive->hwif->rq;
  283. int uptodate = pc->error ? 0 : 1;
  284. int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
  285. ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
  286. dsc, err);
  287. if (dsc)
  288. ide_tape_handle_dsc(drive);
  289. if (drive->failed_pc == pc)
  290. drive->failed_pc = NULL;
  291. if (pc->c[0] == REQUEST_SENSE) {
  292. if (uptodate)
  293. idetape_analyze_error(drive);
  294. else
  295. printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
  296. "itself - Aborting request!\n");
  297. } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
  298. unsigned int blocks =
  299. (blk_rq_bytes(rq) - scsi_req(rq)->resid_len) / tape->blk_size;
  300. tape->avg_size += blocks * tape->blk_size;
  301. if (time_after_eq(jiffies, tape->avg_time + HZ)) {
  302. tape->avg_speed = tape->avg_size * HZ /
  303. (jiffies - tape->avg_time) / 1024;
  304. tape->avg_size = 0;
  305. tape->avg_time = jiffies;
  306. }
  307. tape->first_frame += blocks;
  308. if (pc->error) {
  309. uptodate = 0;
  310. err = pc->error;
  311. }
  312. }
  313. scsi_req(rq)->result = err;
  314. return uptodate;
  315. }
  316. /*
  317. * Postpone the current request so that ide.c will be able to service requests
  318. * from another device on the same port while we are polling for DSC.
  319. */
  320. static void ide_tape_stall_queue(ide_drive_t *drive)
  321. {
  322. idetape_tape_t *tape = drive->driver_data;
  323. ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
  324. drive->hwif->rq->cmd[0], tape->dsc_poll_freq);
  325. tape->postponed_rq = true;
  326. ide_stall_queue(drive, tape->dsc_poll_freq);
  327. }
  328. static void ide_tape_handle_dsc(ide_drive_t *drive)
  329. {
  330. idetape_tape_t *tape = drive->driver_data;
  331. /* Media access command */
  332. tape->dsc_polling_start = jiffies;
  333. tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
  334. tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
  335. /* Allow ide.c to handle other requests */
  336. ide_tape_stall_queue(drive);
  337. }
  338. /*
  339. * Packet Command Interface
  340. *
  341. * The current Packet Command is available in drive->pc, and will not change
  342. * until we finish handling it. Each packet command is associated with a
  343. * callback function that will be called when the command is finished.
  344. *
  345. * The handling will be done in three stages:
  346. *
  347. * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
  348. * the interrupt handler to ide_pc_intr.
  349. *
  350. * 2. On each interrupt, ide_pc_intr will be called. This step will be
  351. * repeated until the device signals us that no more interrupts will be issued.
  352. *
  353. * 3. ATAPI Tape media access commands have immediate status with a delayed
  354. * process. In case of a successful initiation of a media access packet command,
  355. * the DSC bit will be set when the actual execution of the command is finished.
  356. * Since the tape drive will not issue an interrupt, we have to poll for this
  357. * event. In this case, we define the request as "low priority request" by
  358. * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
  359. * exit the driver.
  360. *
  361. * ide.c will then give higher priority to requests which originate from the
  362. * other device, until will change rq_status to RQ_ACTIVE.
  363. *
  364. * 4. When the packet command is finished, it will be checked for errors.
  365. *
  366. * 5. In case an error was found, we queue a request sense packet command in
  367. * front of the request queue and retry the operation up to
  368. * IDETAPE_MAX_PC_RETRIES times.
  369. *
  370. * 6. In case no error was found, or we decided to give up and not to retry
  371. * again, the callback function will be called and then we will handle the next
  372. * request.
  373. */
  374. static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
  375. struct ide_cmd *cmd,
  376. struct ide_atapi_pc *pc)
  377. {
  378. idetape_tape_t *tape = drive->driver_data;
  379. struct request *rq = drive->hwif->rq;
  380. if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
  381. drive->failed_pc = pc;
  382. /* Set the current packet command */
  383. drive->pc = pc;
  384. if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
  385. (pc->flags & PC_FLAG_ABORT)) {
  386. /*
  387. * We will "abort" retrying a packet command in case legitimate
  388. * error code was received (crossing a filemark, or end of the
  389. * media, for example).
  390. */
  391. if (!(pc->flags & PC_FLAG_ABORT)) {
  392. if (!(pc->c[0] == TEST_UNIT_READY &&
  393. tape->sense_key == 2 && tape->asc == 4 &&
  394. (tape->ascq == 1 || tape->ascq == 8))) {
  395. printk(KERN_ERR "ide-tape: %s: I/O error, "
  396. "pc = %2x, key = %2x, "
  397. "asc = %2x, ascq = %2x\n",
  398. tape->name, pc->c[0],
  399. tape->sense_key, tape->asc,
  400. tape->ascq);
  401. }
  402. /* Giving up */
  403. pc->error = IDE_DRV_ERROR_GENERAL;
  404. }
  405. drive->failed_pc = NULL;
  406. drive->pc_callback(drive, 0);
  407. ide_complete_rq(drive, BLK_STS_IOERR, blk_rq_bytes(rq));
  408. return ide_stopped;
  409. }
  410. ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries,
  411. pc->c[0]);
  412. pc->retries++;
  413. return ide_issue_pc(drive, cmd);
  414. }
  415. /* A mode sense command is used to "sense" tape parameters. */
  416. static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
  417. {
  418. ide_init_pc(pc);
  419. pc->c[0] = MODE_SENSE;
  420. if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
  421. /* DBD = 1 - Don't return block descriptors */
  422. pc->c[1] = 8;
  423. pc->c[2] = page_code;
  424. /*
  425. * Changed pc->c[3] to 0 (255 will at best return unused info).
  426. *
  427. * For SCSI this byte is defined as subpage instead of high byte
  428. * of length and some IDE drives seem to interpret it this way
  429. * and return an error when 255 is used.
  430. */
  431. pc->c[3] = 0;
  432. /* We will just discard data in that case */
  433. pc->c[4] = 255;
  434. if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
  435. pc->req_xfer = 12;
  436. else if (page_code == IDETAPE_CAPABILITIES_PAGE)
  437. pc->req_xfer = 24;
  438. else
  439. pc->req_xfer = 50;
  440. }
  441. static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
  442. {
  443. ide_hwif_t *hwif = drive->hwif;
  444. idetape_tape_t *tape = drive->driver_data;
  445. struct ide_atapi_pc *pc = drive->pc;
  446. u8 stat;
  447. stat = hwif->tp_ops->read_status(hwif);
  448. if (stat & ATA_DSC) {
  449. if (stat & ATA_ERR) {
  450. /* Error detected */
  451. if (pc->c[0] != TEST_UNIT_READY)
  452. printk(KERN_ERR "ide-tape: %s: I/O error, ",
  453. tape->name);
  454. /* Retry operation */
  455. ide_retry_pc(drive);
  456. return ide_stopped;
  457. }
  458. pc->error = 0;
  459. } else {
  460. pc->error = IDE_DRV_ERROR_GENERAL;
  461. drive->failed_pc = NULL;
  462. }
  463. drive->pc_callback(drive, 0);
  464. return ide_stopped;
  465. }
  466. static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
  467. struct ide_atapi_pc *pc, struct request *rq,
  468. u8 opcode)
  469. {
  470. unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
  471. ide_init_pc(pc);
  472. put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
  473. pc->c[1] = 1;
  474. if (blk_rq_bytes(rq) == tape->buffer_size)
  475. pc->flags |= PC_FLAG_DMA_OK;
  476. if (opcode == READ_6)
  477. pc->c[0] = READ_6;
  478. else if (opcode == WRITE_6) {
  479. pc->c[0] = WRITE_6;
  480. pc->flags |= PC_FLAG_WRITING;
  481. }
  482. memcpy(scsi_req(rq)->cmd, pc->c, 12);
  483. }
  484. static ide_startstop_t idetape_do_request(ide_drive_t *drive,
  485. struct request *rq, sector_t block)
  486. {
  487. ide_hwif_t *hwif = drive->hwif;
  488. idetape_tape_t *tape = drive->driver_data;
  489. struct ide_atapi_pc *pc = NULL;
  490. struct ide_cmd cmd;
  491. struct scsi_request *req = scsi_req(rq);
  492. u8 stat;
  493. ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
  494. req->cmd[0], (unsigned long long)blk_rq_pos(rq),
  495. blk_rq_sectors(rq));
  496. BUG_ON(!blk_rq_is_private(rq));
  497. BUG_ON(ide_req(rq)->type != ATA_PRIV_MISC &&
  498. ide_req(rq)->type != ATA_PRIV_SENSE);
  499. /* Retry a failed packet command */
  500. if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
  501. pc = drive->failed_pc;
  502. goto out;
  503. }
  504. /*
  505. * If the tape is still busy, postpone our request and service
  506. * the other device meanwhile.
  507. */
  508. stat = hwif->tp_ops->read_status(hwif);
  509. if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
  510. (req->cmd[13] & REQ_IDETAPE_PC2) == 0)
  511. drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
  512. if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
  513. drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
  514. drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
  515. }
  516. if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
  517. !(stat & ATA_DSC)) {
  518. if (!tape->postponed_rq) {
  519. tape->dsc_polling_start = jiffies;
  520. tape->dsc_poll_freq = tape->best_dsc_rw_freq;
  521. tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
  522. } else if (time_after(jiffies, tape->dsc_timeout)) {
  523. printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
  524. tape->name);
  525. if (req->cmd[13] & REQ_IDETAPE_PC2) {
  526. idetape_media_access_finished(drive);
  527. return ide_stopped;
  528. } else {
  529. return ide_do_reset(drive);
  530. }
  531. } else if (time_after(jiffies,
  532. tape->dsc_polling_start +
  533. IDETAPE_DSC_MA_THRESHOLD))
  534. tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
  535. ide_tape_stall_queue(drive);
  536. return ide_stopped;
  537. } else {
  538. drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
  539. tape->postponed_rq = false;
  540. }
  541. if (req->cmd[13] & REQ_IDETAPE_READ) {
  542. pc = &tape->queued_pc;
  543. ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
  544. goto out;
  545. }
  546. if (req->cmd[13] & REQ_IDETAPE_WRITE) {
  547. pc = &tape->queued_pc;
  548. ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
  549. goto out;
  550. }
  551. if (req->cmd[13] & REQ_IDETAPE_PC1) {
  552. pc = (struct ide_atapi_pc *)rq->special;
  553. req->cmd[13] &= ~(REQ_IDETAPE_PC1);
  554. req->cmd[13] |= REQ_IDETAPE_PC2;
  555. goto out;
  556. }
  557. if (req->cmd[13] & REQ_IDETAPE_PC2) {
  558. idetape_media_access_finished(drive);
  559. return ide_stopped;
  560. }
  561. BUG();
  562. out:
  563. /* prepare sense request for this command */
  564. ide_prep_sense(drive, rq);
  565. memset(&cmd, 0, sizeof(cmd));
  566. if (rq_data_dir(rq))
  567. cmd.tf_flags |= IDE_TFLAG_WRITE;
  568. cmd.rq = rq;
  569. ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
  570. ide_map_sg(drive, &cmd);
  571. return ide_tape_issue_pc(drive, &cmd, pc);
  572. }
  573. /*
  574. * Write a filemark if write_filemark=1. Flush the device buffers without
  575. * writing a filemark otherwise.
  576. */
  577. static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
  578. struct ide_atapi_pc *pc, int write_filemark)
  579. {
  580. ide_init_pc(pc);
  581. pc->c[0] = WRITE_FILEMARKS;
  582. pc->c[4] = write_filemark;
  583. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  584. }
  585. static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
  586. {
  587. idetape_tape_t *tape = drive->driver_data;
  588. struct gendisk *disk = tape->disk;
  589. int load_attempted = 0;
  590. /* Wait for the tape to become ready */
  591. set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
  592. timeout += jiffies;
  593. while (time_before(jiffies, timeout)) {
  594. if (ide_do_test_unit_ready(drive, disk) == 0)
  595. return 0;
  596. if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
  597. || (tape->asc == 0x3A)) {
  598. /* no media */
  599. if (load_attempted)
  600. return -ENOMEDIUM;
  601. ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
  602. load_attempted = 1;
  603. /* not about to be ready */
  604. } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
  605. (tape->ascq == 1 || tape->ascq == 8)))
  606. return -EIO;
  607. msleep(100);
  608. }
  609. return -EIO;
  610. }
  611. static int idetape_flush_tape_buffers(ide_drive_t *drive)
  612. {
  613. struct ide_tape_obj *tape = drive->driver_data;
  614. struct ide_atapi_pc pc;
  615. int rc;
  616. idetape_create_write_filemark_cmd(drive, &pc, 0);
  617. rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
  618. if (rc)
  619. return rc;
  620. idetape_wait_ready(drive, 60 * 5 * HZ);
  621. return 0;
  622. }
  623. static int ide_tape_read_position(ide_drive_t *drive)
  624. {
  625. idetape_tape_t *tape = drive->driver_data;
  626. struct ide_atapi_pc pc;
  627. u8 buf[20];
  628. ide_debug_log(IDE_DBG_FUNC, "enter");
  629. /* prep cmd */
  630. ide_init_pc(&pc);
  631. pc.c[0] = READ_POSITION;
  632. pc.req_xfer = 20;
  633. if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer))
  634. return -1;
  635. if (!pc.error) {
  636. ide_debug_log(IDE_DBG_FUNC, "BOP - %s",
  637. (buf[0] & 0x80) ? "Yes" : "No");
  638. ide_debug_log(IDE_DBG_FUNC, "EOP - %s",
  639. (buf[0] & 0x40) ? "Yes" : "No");
  640. if (buf[0] & 0x4) {
  641. printk(KERN_INFO "ide-tape: Block location is unknown"
  642. "to the tape\n");
  643. clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
  644. &drive->atapi_flags);
  645. return -1;
  646. } else {
  647. ide_debug_log(IDE_DBG_FUNC, "Block Location: %u",
  648. be32_to_cpup((__be32 *)&buf[4]));
  649. tape->partition = buf[1];
  650. tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
  651. set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
  652. &drive->atapi_flags);
  653. }
  654. }
  655. return tape->first_frame;
  656. }
  657. static void idetape_create_locate_cmd(ide_drive_t *drive,
  658. struct ide_atapi_pc *pc,
  659. unsigned int block, u8 partition, int skip)
  660. {
  661. ide_init_pc(pc);
  662. pc->c[0] = POSITION_TO_ELEMENT;
  663. pc->c[1] = 2;
  664. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
  665. pc->c[8] = partition;
  666. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  667. }
  668. static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
  669. {
  670. idetape_tape_t *tape = drive->driver_data;
  671. if (tape->chrdev_dir != IDETAPE_DIR_READ)
  672. return;
  673. clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
  674. tape->valid = 0;
  675. if (tape->buf != NULL) {
  676. kfree(tape->buf);
  677. tape->buf = NULL;
  678. }
  679. tape->chrdev_dir = IDETAPE_DIR_NONE;
  680. }
  681. /*
  682. * Position the tape to the requested block using the LOCATE packet command.
  683. * A READ POSITION command is then issued to check where we are positioned. Like
  684. * all higher level operations, we queue the commands at the tail of the request
  685. * queue and wait for their completion.
  686. */
  687. static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
  688. u8 partition, int skip)
  689. {
  690. idetape_tape_t *tape = drive->driver_data;
  691. struct gendisk *disk = tape->disk;
  692. int ret;
  693. struct ide_atapi_pc pc;
  694. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  695. __ide_tape_discard_merge_buffer(drive);
  696. idetape_wait_ready(drive, 60 * 5 * HZ);
  697. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  698. ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
  699. if (ret)
  700. return ret;
  701. ret = ide_tape_read_position(drive);
  702. if (ret < 0)
  703. return ret;
  704. return 0;
  705. }
  706. static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
  707. int restore_position)
  708. {
  709. idetape_tape_t *tape = drive->driver_data;
  710. int seek, position;
  711. __ide_tape_discard_merge_buffer(drive);
  712. if (restore_position) {
  713. position = ide_tape_read_position(drive);
  714. seek = position > 0 ? position : 0;
  715. if (idetape_position_tape(drive, seek, 0, 0)) {
  716. printk(KERN_INFO "ide-tape: %s: position_tape failed in"
  717. " %s\n", tape->name, __func__);
  718. return;
  719. }
  720. }
  721. }
  722. /*
  723. * Generate a read/write request for the block device interface and wait for it
  724. * to be serviced.
  725. */
  726. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
  727. {
  728. idetape_tape_t *tape = drive->driver_data;
  729. struct request *rq;
  730. int ret;
  731. ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size);
  732. BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
  733. BUG_ON(size < 0 || size % tape->blk_size);
  734. rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
  735. ide_req(rq)->type = ATA_PRIV_MISC;
  736. scsi_req(rq)->cmd[13] = cmd;
  737. rq->rq_disk = tape->disk;
  738. rq->__sector = tape->first_frame;
  739. if (size) {
  740. ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
  741. GFP_NOIO);
  742. if (ret)
  743. goto out_put;
  744. }
  745. blk_execute_rq(drive->queue, tape->disk, rq, 0);
  746. /* calculate the number of transferred bytes and update buffer state */
  747. size -= scsi_req(rq)->resid_len;
  748. tape->cur = tape->buf;
  749. if (cmd == REQ_IDETAPE_READ)
  750. tape->valid = size;
  751. else
  752. tape->valid = 0;
  753. ret = size;
  754. if (scsi_req(rq)->result == IDE_DRV_ERROR_GENERAL)
  755. ret = -EIO;
  756. out_put:
  757. blk_put_request(rq);
  758. return ret;
  759. }
  760. static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
  761. {
  762. ide_init_pc(pc);
  763. pc->c[0] = INQUIRY;
  764. pc->c[4] = 254;
  765. pc->req_xfer = 254;
  766. }
  767. static void idetape_create_rewind_cmd(ide_drive_t *drive,
  768. struct ide_atapi_pc *pc)
  769. {
  770. ide_init_pc(pc);
  771. pc->c[0] = REZERO_UNIT;
  772. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  773. }
  774. static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
  775. {
  776. ide_init_pc(pc);
  777. pc->c[0] = ERASE;
  778. pc->c[1] = 1;
  779. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  780. }
  781. static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
  782. {
  783. ide_init_pc(pc);
  784. pc->c[0] = SPACE;
  785. put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
  786. pc->c[1] = cmd;
  787. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  788. }
  789. static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
  790. {
  791. idetape_tape_t *tape = drive->driver_data;
  792. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  793. printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
  794. " but we are not writing.\n");
  795. return;
  796. }
  797. if (tape->buf) {
  798. size_t aligned = roundup(tape->valid, tape->blk_size);
  799. memset(tape->cur, 0, aligned - tape->valid);
  800. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
  801. kfree(tape->buf);
  802. tape->buf = NULL;
  803. }
  804. tape->chrdev_dir = IDETAPE_DIR_NONE;
  805. }
  806. static int idetape_init_rw(ide_drive_t *drive, int dir)
  807. {
  808. idetape_tape_t *tape = drive->driver_data;
  809. int rc;
  810. BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
  811. if (tape->chrdev_dir == dir)
  812. return 0;
  813. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  814. ide_tape_discard_merge_buffer(drive, 1);
  815. else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  816. ide_tape_flush_merge_buffer(drive);
  817. idetape_flush_tape_buffers(drive);
  818. }
  819. if (tape->buf || tape->valid) {
  820. printk(KERN_ERR "ide-tape: valid should be 0 now\n");
  821. tape->valid = 0;
  822. }
  823. tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
  824. if (!tape->buf)
  825. return -ENOMEM;
  826. tape->chrdev_dir = dir;
  827. tape->cur = tape->buf;
  828. /*
  829. * Issue a 0 rw command to ensure that DSC handshake is
  830. * switched from completion mode to buffer available mode. No
  831. * point in issuing this if DSC overlap isn't supported, some
  832. * drives (Seagate STT3401A) will return an error.
  833. */
  834. if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
  835. int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
  836. : REQ_IDETAPE_WRITE;
  837. rc = idetape_queue_rw_tail(drive, cmd, 0);
  838. if (rc < 0) {
  839. kfree(tape->buf);
  840. tape->buf = NULL;
  841. tape->chrdev_dir = IDETAPE_DIR_NONE;
  842. return rc;
  843. }
  844. }
  845. return 0;
  846. }
  847. static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
  848. {
  849. idetape_tape_t *tape = drive->driver_data;
  850. memset(tape->buf, 0, tape->buffer_size);
  851. while (bcount) {
  852. unsigned int count = min(tape->buffer_size, bcount);
  853. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
  854. bcount -= count;
  855. }
  856. }
  857. /*
  858. * Rewinds the tape to the Beginning Of the current Partition (BOP). We
  859. * currently support only one partition.
  860. */
  861. static int idetape_rewind_tape(ide_drive_t *drive)
  862. {
  863. struct ide_tape_obj *tape = drive->driver_data;
  864. struct gendisk *disk = tape->disk;
  865. struct ide_atapi_pc pc;
  866. int ret;
  867. ide_debug_log(IDE_DBG_FUNC, "enter");
  868. idetape_create_rewind_cmd(drive, &pc);
  869. ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
  870. if (ret)
  871. return ret;
  872. ret = ide_tape_read_position(drive);
  873. if (ret < 0)
  874. return ret;
  875. return 0;
  876. }
  877. /* mtio.h compatible commands should be issued to the chrdev interface. */
  878. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
  879. unsigned long arg)
  880. {
  881. idetape_tape_t *tape = drive->driver_data;
  882. void __user *argp = (void __user *)arg;
  883. struct idetape_config {
  884. int dsc_rw_frequency;
  885. int dsc_media_access_frequency;
  886. int nr_stages;
  887. } config;
  888. ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd);
  889. switch (cmd) {
  890. case 0x0340:
  891. if (copy_from_user(&config, argp, sizeof(config)))
  892. return -EFAULT;
  893. tape->best_dsc_rw_freq = config.dsc_rw_frequency;
  894. break;
  895. case 0x0350:
  896. memset(&config, 0, sizeof(config));
  897. config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
  898. config.nr_stages = 1;
  899. if (copy_to_user(argp, &config, sizeof(config)))
  900. return -EFAULT;
  901. break;
  902. default:
  903. return -EIO;
  904. }
  905. return 0;
  906. }
  907. static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
  908. int mt_count)
  909. {
  910. idetape_tape_t *tape = drive->driver_data;
  911. struct gendisk *disk = tape->disk;
  912. struct ide_atapi_pc pc;
  913. int retval, count = 0;
  914. int sprev = !!(tape->caps[4] & 0x20);
  915. ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count);
  916. if (mt_count == 0)
  917. return 0;
  918. if (MTBSF == mt_op || MTBSFM == mt_op) {
  919. if (!sprev)
  920. return -EIO;
  921. mt_count = -mt_count;
  922. }
  923. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  924. tape->valid = 0;
  925. if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
  926. &drive->atapi_flags))
  927. ++count;
  928. ide_tape_discard_merge_buffer(drive, 0);
  929. }
  930. switch (mt_op) {
  931. case MTFSF:
  932. case MTBSF:
  933. idetape_create_space_cmd(&pc, mt_count - count,
  934. IDETAPE_SPACE_OVER_FILEMARK);
  935. return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
  936. case MTFSFM:
  937. case MTBSFM:
  938. if (!sprev)
  939. return -EIO;
  940. retval = idetape_space_over_filemarks(drive, MTFSF,
  941. mt_count - count);
  942. if (retval)
  943. return retval;
  944. count = (MTBSFM == mt_op ? 1 : -1);
  945. return idetape_space_over_filemarks(drive, MTFSF, count);
  946. default:
  947. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  948. mt_op);
  949. return -EIO;
  950. }
  951. }
  952. /*
  953. * Our character device read / write functions.
  954. *
  955. * The tape is optimized to maximize throughput when it is transferring an
  956. * integral number of the "continuous transfer limit", which is a parameter of
  957. * the specific tape (26kB on my particular tape, 32kB for Onstream).
  958. *
  959. * As of version 1.3 of the driver, the character device provides an abstract
  960. * continuous view of the media - any mix of block sizes (even 1 byte) on the
  961. * same backup/restore procedure is supported. The driver will internally
  962. * convert the requests to the recommended transfer unit, so that an unmatch
  963. * between the user's block size to the recommended size will only result in a
  964. * (slightly) increased driver overhead, but will no longer hit performance.
  965. * This is not applicable to Onstream.
  966. */
  967. static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
  968. size_t count, loff_t *ppos)
  969. {
  970. struct ide_tape_obj *tape = file->private_data;
  971. ide_drive_t *drive = tape->drive;
  972. size_t done = 0;
  973. ssize_t ret = 0;
  974. int rc;
  975. ide_debug_log(IDE_DBG_FUNC, "count %zd", count);
  976. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  977. if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
  978. if (count > tape->blk_size &&
  979. (count % tape->blk_size) == 0)
  980. tape->user_bs_factor = count / tape->blk_size;
  981. }
  982. rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
  983. if (rc < 0)
  984. return rc;
  985. while (done < count) {
  986. size_t todo;
  987. /* refill if staging buffer is empty */
  988. if (!tape->valid) {
  989. /* If we are at a filemark, nothing more to read */
  990. if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
  991. &drive->atapi_flags))
  992. break;
  993. /* read */
  994. if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
  995. tape->buffer_size) <= 0)
  996. break;
  997. }
  998. /* copy out */
  999. todo = min_t(size_t, count - done, tape->valid);
  1000. if (copy_to_user(buf + done, tape->cur, todo))
  1001. ret = -EFAULT;
  1002. tape->cur += todo;
  1003. tape->valid -= todo;
  1004. done += todo;
  1005. }
  1006. if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
  1007. idetape_space_over_filemarks(drive, MTFSF, 1);
  1008. return 0;
  1009. }
  1010. return ret ? ret : done;
  1011. }
  1012. static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
  1013. size_t count, loff_t *ppos)
  1014. {
  1015. struct ide_tape_obj *tape = file->private_data;
  1016. ide_drive_t *drive = tape->drive;
  1017. size_t done = 0;
  1018. ssize_t ret = 0;
  1019. int rc;
  1020. /* The drive is write protected. */
  1021. if (tape->write_prot)
  1022. return -EACCES;
  1023. ide_debug_log(IDE_DBG_FUNC, "count %zd", count);
  1024. /* Initialize write operation */
  1025. rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
  1026. if (rc < 0)
  1027. return rc;
  1028. while (done < count) {
  1029. size_t todo;
  1030. /* flush if staging buffer is full */
  1031. if (tape->valid == tape->buffer_size &&
  1032. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
  1033. tape->buffer_size) <= 0)
  1034. return rc;
  1035. /* copy in */
  1036. todo = min_t(size_t, count - done,
  1037. tape->buffer_size - tape->valid);
  1038. if (copy_from_user(tape->cur, buf + done, todo))
  1039. ret = -EFAULT;
  1040. tape->cur += todo;
  1041. tape->valid += todo;
  1042. done += todo;
  1043. }
  1044. return ret ? ret : done;
  1045. }
  1046. static int idetape_write_filemark(ide_drive_t *drive)
  1047. {
  1048. struct ide_tape_obj *tape = drive->driver_data;
  1049. struct ide_atapi_pc pc;
  1050. /* Write a filemark */
  1051. idetape_create_write_filemark_cmd(drive, &pc, 1);
  1052. if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
  1053. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  1054. return -EIO;
  1055. }
  1056. return 0;
  1057. }
  1058. /*
  1059. * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
  1060. * requested.
  1061. *
  1062. * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
  1063. * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
  1064. * usually not supported.
  1065. *
  1066. * The following commands are currently not supported:
  1067. *
  1068. * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
  1069. * MT_ST_WRITE_THRESHOLD.
  1070. */
  1071. static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
  1072. {
  1073. idetape_tape_t *tape = drive->driver_data;
  1074. struct gendisk *disk = tape->disk;
  1075. struct ide_atapi_pc pc;
  1076. int i, retval;
  1077. ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
  1078. mt_op, mt_count);
  1079. switch (mt_op) {
  1080. case MTFSF:
  1081. case MTFSFM:
  1082. case MTBSF:
  1083. case MTBSFM:
  1084. if (!mt_count)
  1085. return 0;
  1086. return idetape_space_over_filemarks(drive, mt_op, mt_count);
  1087. default:
  1088. break;
  1089. }
  1090. switch (mt_op) {
  1091. case MTWEOF:
  1092. if (tape->write_prot)
  1093. return -EACCES;
  1094. ide_tape_discard_merge_buffer(drive, 1);
  1095. for (i = 0; i < mt_count; i++) {
  1096. retval = idetape_write_filemark(drive);
  1097. if (retval)
  1098. return retval;
  1099. }
  1100. return 0;
  1101. case MTREW:
  1102. ide_tape_discard_merge_buffer(drive, 0);
  1103. if (idetape_rewind_tape(drive))
  1104. return -EIO;
  1105. return 0;
  1106. case MTLOAD:
  1107. ide_tape_discard_merge_buffer(drive, 0);
  1108. return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
  1109. case MTUNLOAD:
  1110. case MTOFFL:
  1111. /*
  1112. * If door is locked, attempt to unlock before
  1113. * attempting to eject.
  1114. */
  1115. if (tape->door_locked) {
  1116. if (!ide_set_media_lock(drive, disk, 0))
  1117. tape->door_locked = DOOR_UNLOCKED;
  1118. }
  1119. ide_tape_discard_merge_buffer(drive, 0);
  1120. retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
  1121. if (!retval)
  1122. clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
  1123. &drive->atapi_flags);
  1124. return retval;
  1125. case MTNOP:
  1126. ide_tape_discard_merge_buffer(drive, 0);
  1127. return idetape_flush_tape_buffers(drive);
  1128. case MTRETEN:
  1129. ide_tape_discard_merge_buffer(drive, 0);
  1130. return ide_do_start_stop(drive, disk,
  1131. IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  1132. case MTEOM:
  1133. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  1134. return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
  1135. case MTERASE:
  1136. (void)idetape_rewind_tape(drive);
  1137. idetape_create_erase_cmd(&pc);
  1138. return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
  1139. case MTSETBLK:
  1140. if (mt_count) {
  1141. if (mt_count < tape->blk_size ||
  1142. mt_count % tape->blk_size)
  1143. return -EIO;
  1144. tape->user_bs_factor = mt_count / tape->blk_size;
  1145. clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
  1146. &drive->atapi_flags);
  1147. } else
  1148. set_bit(ilog2(IDE_AFLAG_DETECT_BS),
  1149. &drive->atapi_flags);
  1150. return 0;
  1151. case MTSEEK:
  1152. ide_tape_discard_merge_buffer(drive, 0);
  1153. return idetape_position_tape(drive,
  1154. mt_count * tape->user_bs_factor, tape->partition, 0);
  1155. case MTSETPART:
  1156. ide_tape_discard_merge_buffer(drive, 0);
  1157. return idetape_position_tape(drive, 0, mt_count, 0);
  1158. case MTFSR:
  1159. case MTBSR:
  1160. case MTLOCK:
  1161. retval = ide_set_media_lock(drive, disk, 1);
  1162. if (retval)
  1163. return retval;
  1164. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  1165. return 0;
  1166. case MTUNLOCK:
  1167. retval = ide_set_media_lock(drive, disk, 0);
  1168. if (retval)
  1169. return retval;
  1170. tape->door_locked = DOOR_UNLOCKED;
  1171. return 0;
  1172. default:
  1173. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1174. mt_op);
  1175. return -EIO;
  1176. }
  1177. }
  1178. /*
  1179. * Our character device ioctls. General mtio.h magnetic io commands are
  1180. * supported here, and not in the corresponding block interface. Our own
  1181. * ide-tape ioctls are supported on both interfaces.
  1182. */
  1183. static long do_idetape_chrdev_ioctl(struct file *file,
  1184. unsigned int cmd, unsigned long arg)
  1185. {
  1186. struct ide_tape_obj *tape = file->private_data;
  1187. ide_drive_t *drive = tape->drive;
  1188. struct mtop mtop;
  1189. struct mtget mtget;
  1190. struct mtpos mtpos;
  1191. int block_offset = 0, position = tape->first_frame;
  1192. void __user *argp = (void __user *)arg;
  1193. ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd);
  1194. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1195. ide_tape_flush_merge_buffer(drive);
  1196. idetape_flush_tape_buffers(drive);
  1197. }
  1198. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  1199. block_offset = tape->valid /
  1200. (tape->blk_size * tape->user_bs_factor);
  1201. position = ide_tape_read_position(drive);
  1202. if (position < 0)
  1203. return -EIO;
  1204. }
  1205. switch (cmd) {
  1206. case MTIOCTOP:
  1207. if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
  1208. return -EFAULT;
  1209. return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
  1210. case MTIOCGET:
  1211. memset(&mtget, 0, sizeof(struct mtget));
  1212. mtget.mt_type = MT_ISSCSI2;
  1213. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  1214. mtget.mt_dsreg =
  1215. ((tape->blk_size * tape->user_bs_factor)
  1216. << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  1217. if (tape->drv_write_prot)
  1218. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  1219. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  1220. return -EFAULT;
  1221. return 0;
  1222. case MTIOCPOS:
  1223. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  1224. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  1225. return -EFAULT;
  1226. return 0;
  1227. default:
  1228. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1229. ide_tape_discard_merge_buffer(drive, 1);
  1230. return idetape_blkdev_ioctl(drive, cmd, arg);
  1231. }
  1232. }
  1233. static long idetape_chrdev_ioctl(struct file *file,
  1234. unsigned int cmd, unsigned long arg)
  1235. {
  1236. long ret;
  1237. mutex_lock(&ide_tape_mutex);
  1238. ret = do_idetape_chrdev_ioctl(file, cmd, arg);
  1239. mutex_unlock(&ide_tape_mutex);
  1240. return ret;
  1241. }
  1242. /*
  1243. * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
  1244. * block size with the reported value.
  1245. */
  1246. static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
  1247. {
  1248. idetape_tape_t *tape = drive->driver_data;
  1249. struct ide_atapi_pc pc;
  1250. u8 buf[12];
  1251. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  1252. if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
  1253. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  1254. if (tape->blk_size == 0) {
  1255. printk(KERN_WARNING "ide-tape: Cannot deal with zero "
  1256. "block size, assuming 32k\n");
  1257. tape->blk_size = 32768;
  1258. }
  1259. return;
  1260. }
  1261. tape->blk_size = (buf[4 + 5] << 16) +
  1262. (buf[4 + 6] << 8) +
  1263. buf[4 + 7];
  1264. tape->drv_write_prot = (buf[2] & 0x80) >> 7;
  1265. ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
  1266. tape->blk_size, tape->drv_write_prot);
  1267. }
  1268. static int idetape_chrdev_open(struct inode *inode, struct file *filp)
  1269. {
  1270. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  1271. ide_drive_t *drive;
  1272. idetape_tape_t *tape;
  1273. int retval;
  1274. if (i >= MAX_HWIFS * MAX_DRIVES)
  1275. return -ENXIO;
  1276. mutex_lock(&idetape_chrdev_mutex);
  1277. tape = ide_tape_get(NULL, true, i);
  1278. if (!tape) {
  1279. mutex_unlock(&idetape_chrdev_mutex);
  1280. return -ENXIO;
  1281. }
  1282. drive = tape->drive;
  1283. filp->private_data = tape;
  1284. ide_debug_log(IDE_DBG_FUNC, "enter");
  1285. /*
  1286. * We really want to do nonseekable_open(inode, filp); here, but some
  1287. * versions of tar incorrectly call lseek on tapes and bail out if that
  1288. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  1289. */
  1290. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  1291. if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
  1292. retval = -EBUSY;
  1293. goto out_put_tape;
  1294. }
  1295. retval = idetape_wait_ready(drive, 60 * HZ);
  1296. if (retval) {
  1297. clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
  1298. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  1299. goto out_put_tape;
  1300. }
  1301. ide_tape_read_position(drive);
  1302. if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
  1303. (void)idetape_rewind_tape(drive);
  1304. /* Read block size and write protect status from drive. */
  1305. ide_tape_get_bsize_from_bdesc(drive);
  1306. /* Set write protect flag if device is opened as read-only. */
  1307. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  1308. tape->write_prot = 1;
  1309. else
  1310. tape->write_prot = tape->drv_write_prot;
  1311. /* Make sure drive isn't write protected if user wants to write. */
  1312. if (tape->write_prot) {
  1313. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  1314. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  1315. clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
  1316. retval = -EROFS;
  1317. goto out_put_tape;
  1318. }
  1319. }
  1320. /* Lock the tape drive door so user can't eject. */
  1321. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1322. if (!ide_set_media_lock(drive, tape->disk, 1)) {
  1323. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  1324. tape->door_locked = DOOR_LOCKED;
  1325. }
  1326. }
  1327. mutex_unlock(&idetape_chrdev_mutex);
  1328. return 0;
  1329. out_put_tape:
  1330. ide_tape_put(tape);
  1331. mutex_unlock(&idetape_chrdev_mutex);
  1332. return retval;
  1333. }
  1334. static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
  1335. {
  1336. idetape_tape_t *tape = drive->driver_data;
  1337. ide_tape_flush_merge_buffer(drive);
  1338. tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
  1339. if (tape->buf != NULL) {
  1340. idetape_pad_zeros(drive, tape->blk_size *
  1341. (tape->user_bs_factor - 1));
  1342. kfree(tape->buf);
  1343. tape->buf = NULL;
  1344. }
  1345. idetape_write_filemark(drive);
  1346. idetape_flush_tape_buffers(drive);
  1347. idetape_flush_tape_buffers(drive);
  1348. }
  1349. static int idetape_chrdev_release(struct inode *inode, struct file *filp)
  1350. {
  1351. struct ide_tape_obj *tape = filp->private_data;
  1352. ide_drive_t *drive = tape->drive;
  1353. unsigned int minor = iminor(inode);
  1354. mutex_lock(&idetape_chrdev_mutex);
  1355. tape = drive->driver_data;
  1356. ide_debug_log(IDE_DBG_FUNC, "enter");
  1357. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  1358. idetape_write_release(drive, minor);
  1359. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1360. if (minor < 128)
  1361. ide_tape_discard_merge_buffer(drive, 1);
  1362. }
  1363. if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
  1364. &drive->atapi_flags))
  1365. (void) idetape_rewind_tape(drive);
  1366. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1367. if (tape->door_locked == DOOR_LOCKED) {
  1368. if (!ide_set_media_lock(drive, tape->disk, 0))
  1369. tape->door_locked = DOOR_UNLOCKED;
  1370. }
  1371. }
  1372. clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
  1373. ide_tape_put(tape);
  1374. mutex_unlock(&idetape_chrdev_mutex);
  1375. return 0;
  1376. }
  1377. static void idetape_get_inquiry_results(ide_drive_t *drive)
  1378. {
  1379. idetape_tape_t *tape = drive->driver_data;
  1380. struct ide_atapi_pc pc;
  1381. u8 pc_buf[256];
  1382. char fw_rev[4], vendor_id[8], product_id[16];
  1383. idetape_create_inquiry_cmd(&pc);
  1384. if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
  1385. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
  1386. tape->name);
  1387. return;
  1388. }
  1389. memcpy(vendor_id, &pc_buf[8], 8);
  1390. memcpy(product_id, &pc_buf[16], 16);
  1391. memcpy(fw_rev, &pc_buf[32], 4);
  1392. ide_fixstring(vendor_id, 8, 0);
  1393. ide_fixstring(product_id, 16, 0);
  1394. ide_fixstring(fw_rev, 4, 0);
  1395. printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
  1396. drive->name, tape->name, vendor_id, product_id, fw_rev);
  1397. }
  1398. /*
  1399. * Ask the tape about its various parameters. In particular, we will adjust our
  1400. * data transfer buffer size to the recommended value as returned by the tape.
  1401. */
  1402. static void idetape_get_mode_sense_results(ide_drive_t *drive)
  1403. {
  1404. idetape_tape_t *tape = drive->driver_data;
  1405. struct ide_atapi_pc pc;
  1406. u8 buf[24], *caps;
  1407. u8 speed, max_speed;
  1408. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  1409. if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
  1410. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
  1411. " some default values\n");
  1412. tape->blk_size = 512;
  1413. put_unaligned(52, (u16 *)&tape->caps[12]);
  1414. put_unaligned(540, (u16 *)&tape->caps[14]);
  1415. put_unaligned(6*52, (u16 *)&tape->caps[16]);
  1416. return;
  1417. }
  1418. caps = buf + 4 + buf[3];
  1419. /* convert to host order and save for later use */
  1420. speed = be16_to_cpup((__be16 *)&caps[14]);
  1421. max_speed = be16_to_cpup((__be16 *)&caps[8]);
  1422. *(u16 *)&caps[8] = max_speed;
  1423. *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
  1424. *(u16 *)&caps[14] = speed;
  1425. *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
  1426. if (!speed) {
  1427. printk(KERN_INFO "ide-tape: %s: invalid tape speed "
  1428. "(assuming 650KB/sec)\n", drive->name);
  1429. *(u16 *)&caps[14] = 650;
  1430. }
  1431. if (!max_speed) {
  1432. printk(KERN_INFO "ide-tape: %s: invalid max_speed "
  1433. "(assuming 650KB/sec)\n", drive->name);
  1434. *(u16 *)&caps[8] = 650;
  1435. }
  1436. memcpy(&tape->caps, caps, 20);
  1437. /* device lacks locking support according to capabilities page */
  1438. if ((caps[6] & 1) == 0)
  1439. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  1440. if (caps[7] & 0x02)
  1441. tape->blk_size = 512;
  1442. else if (caps[7] & 0x04)
  1443. tape->blk_size = 1024;
  1444. }
  1445. #ifdef CONFIG_IDE_PROC_FS
  1446. #define ide_tape_devset_get(name, field) \
  1447. static int get_##name(ide_drive_t *drive) \
  1448. { \
  1449. idetape_tape_t *tape = drive->driver_data; \
  1450. return tape->field; \
  1451. }
  1452. #define ide_tape_devset_set(name, field) \
  1453. static int set_##name(ide_drive_t *drive, int arg) \
  1454. { \
  1455. idetape_tape_t *tape = drive->driver_data; \
  1456. tape->field = arg; \
  1457. return 0; \
  1458. }
  1459. #define ide_tape_devset_rw_field(_name, _field) \
  1460. ide_tape_devset_get(_name, _field) \
  1461. ide_tape_devset_set(_name, _field) \
  1462. IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
  1463. #define ide_tape_devset_r_field(_name, _field) \
  1464. ide_tape_devset_get(_name, _field) \
  1465. IDE_DEVSET(_name, 0, get_##_name, NULL)
  1466. static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
  1467. static int divf_tdsc(ide_drive_t *drive) { return HZ; }
  1468. static int divf_buffer(ide_drive_t *drive) { return 2; }
  1469. static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
  1470. ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
  1471. ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
  1472. ide_tape_devset_r_field(avg_speed, avg_speed);
  1473. ide_tape_devset_r_field(speed, caps[14]);
  1474. ide_tape_devset_r_field(buffer, caps[16]);
  1475. ide_tape_devset_r_field(buffer_size, buffer_size);
  1476. static const struct ide_proc_devset idetape_settings[] = {
  1477. __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
  1478. __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
  1479. __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
  1480. __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
  1481. __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
  1482. __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
  1483. mulf_tdsc, divf_tdsc),
  1484. { NULL },
  1485. };
  1486. #endif
  1487. /*
  1488. * The function below is called to:
  1489. *
  1490. * 1. Initialize our various state variables.
  1491. * 2. Ask the tape for its capabilities.
  1492. * 3. Allocate a buffer which will be used for data transfer. The buffer size
  1493. * is chosen based on the recommendation which we received in step 2.
  1494. *
  1495. * Note that at this point ide.c already assigned us an irq, so that we can
  1496. * queue requests here and wait for their completion.
  1497. */
  1498. static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
  1499. {
  1500. unsigned long t;
  1501. int speed;
  1502. u16 *ctl = (u16 *)&tape->caps[12];
  1503. ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
  1504. drive->pc_callback = ide_tape_callback;
  1505. drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
  1506. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  1507. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  1508. tape->name);
  1509. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1510. }
  1511. /* Seagate Travan drives do not support DSC overlap. */
  1512. if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
  1513. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1514. tape->minor = minor;
  1515. tape->name[0] = 'h';
  1516. tape->name[1] = 't';
  1517. tape->name[2] = '0' + minor;
  1518. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1519. idetape_get_inquiry_results(drive);
  1520. idetape_get_mode_sense_results(drive);
  1521. ide_tape_get_bsize_from_bdesc(drive);
  1522. tape->user_bs_factor = 1;
  1523. tape->buffer_size = *ctl * tape->blk_size;
  1524. while (tape->buffer_size > 0xffff) {
  1525. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  1526. *ctl /= 2;
  1527. tape->buffer_size = *ctl * tape->blk_size;
  1528. }
  1529. /* select the "best" DSC read/write polling freq */
  1530. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  1531. t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
  1532. /*
  1533. * Ensure that the number we got makes sense; limit it within
  1534. * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  1535. */
  1536. tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
  1537. IDETAPE_DSC_RW_MAX);
  1538. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  1539. "%ums tDSC%s\n",
  1540. drive->name, tape->name, *(u16 *)&tape->caps[14],
  1541. (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
  1542. tape->buffer_size / 1024,
  1543. jiffies_to_msecs(tape->best_dsc_rw_freq),
  1544. (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
  1545. ide_proc_register_driver(drive, tape->driver);
  1546. }
  1547. static void ide_tape_remove(ide_drive_t *drive)
  1548. {
  1549. idetape_tape_t *tape = drive->driver_data;
  1550. ide_proc_unregister_driver(drive, tape->driver);
  1551. device_del(&tape->dev);
  1552. ide_unregister_region(tape->disk);
  1553. mutex_lock(&idetape_ref_mutex);
  1554. put_device(&tape->dev);
  1555. mutex_unlock(&idetape_ref_mutex);
  1556. }
  1557. static void ide_tape_release(struct device *dev)
  1558. {
  1559. struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
  1560. ide_drive_t *drive = tape->drive;
  1561. struct gendisk *g = tape->disk;
  1562. BUG_ON(tape->valid);
  1563. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1564. drive->driver_data = NULL;
  1565. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  1566. device_destroy(idetape_sysfs_class,
  1567. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  1568. idetape_devs[tape->minor] = NULL;
  1569. g->private_data = NULL;
  1570. put_disk(g);
  1571. kfree(tape);
  1572. }
  1573. #ifdef CONFIG_IDE_PROC_FS
  1574. static int idetape_name_proc_show(struct seq_file *m, void *v)
  1575. {
  1576. ide_drive_t *drive = (ide_drive_t *) m->private;
  1577. idetape_tape_t *tape = drive->driver_data;
  1578. seq_printf(m, "%s\n", tape->name);
  1579. return 0;
  1580. }
  1581. static ide_proc_entry_t idetape_proc[] = {
  1582. { "capacity", S_IFREG|S_IRUGO, ide_capacity_proc_show },
  1583. { "name", S_IFREG|S_IRUGO, idetape_name_proc_show },
  1584. {}
  1585. };
  1586. static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
  1587. {
  1588. return idetape_proc;
  1589. }
  1590. static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
  1591. {
  1592. return idetape_settings;
  1593. }
  1594. #endif
  1595. static int ide_tape_probe(ide_drive_t *);
  1596. static struct ide_driver idetape_driver = {
  1597. .gen_driver = {
  1598. .owner = THIS_MODULE,
  1599. .name = "ide-tape",
  1600. .bus = &ide_bus_type,
  1601. },
  1602. .probe = ide_tape_probe,
  1603. .remove = ide_tape_remove,
  1604. .version = IDETAPE_VERSION,
  1605. .do_request = idetape_do_request,
  1606. #ifdef CONFIG_IDE_PROC_FS
  1607. .proc_entries = ide_tape_proc_entries,
  1608. .proc_devsets = ide_tape_proc_devsets,
  1609. #endif
  1610. };
  1611. /* Our character device supporting functions, passed to register_chrdev. */
  1612. static const struct file_operations idetape_fops = {
  1613. .owner = THIS_MODULE,
  1614. .read = idetape_chrdev_read,
  1615. .write = idetape_chrdev_write,
  1616. .unlocked_ioctl = idetape_chrdev_ioctl,
  1617. .open = idetape_chrdev_open,
  1618. .release = idetape_chrdev_release,
  1619. .llseek = noop_llseek,
  1620. };
  1621. static int idetape_open(struct block_device *bdev, fmode_t mode)
  1622. {
  1623. struct ide_tape_obj *tape;
  1624. mutex_lock(&ide_tape_mutex);
  1625. tape = ide_tape_get(bdev->bd_disk, false, 0);
  1626. mutex_unlock(&ide_tape_mutex);
  1627. if (!tape)
  1628. return -ENXIO;
  1629. return 0;
  1630. }
  1631. static void idetape_release(struct gendisk *disk, fmode_t mode)
  1632. {
  1633. struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
  1634. mutex_lock(&ide_tape_mutex);
  1635. ide_tape_put(tape);
  1636. mutex_unlock(&ide_tape_mutex);
  1637. }
  1638. static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
  1639. unsigned int cmd, unsigned long arg)
  1640. {
  1641. struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
  1642. ide_drive_t *drive = tape->drive;
  1643. int err;
  1644. mutex_lock(&ide_tape_mutex);
  1645. err = generic_ide_ioctl(drive, bdev, cmd, arg);
  1646. if (err == -EINVAL)
  1647. err = idetape_blkdev_ioctl(drive, cmd, arg);
  1648. mutex_unlock(&ide_tape_mutex);
  1649. return err;
  1650. }
  1651. static const struct block_device_operations idetape_block_ops = {
  1652. .owner = THIS_MODULE,
  1653. .open = idetape_open,
  1654. .release = idetape_release,
  1655. .ioctl = idetape_ioctl,
  1656. };
  1657. static int ide_tape_probe(ide_drive_t *drive)
  1658. {
  1659. idetape_tape_t *tape;
  1660. struct gendisk *g;
  1661. int minor;
  1662. ide_debug_log(IDE_DBG_FUNC, "enter");
  1663. if (!strstr(DRV_NAME, drive->driver_req))
  1664. goto failed;
  1665. if (drive->media != ide_tape)
  1666. goto failed;
  1667. if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
  1668. ide_check_atapi_device(drive, DRV_NAME) == 0) {
  1669. printk(KERN_ERR "ide-tape: %s: not supported by this version of"
  1670. " the driver\n", drive->name);
  1671. goto failed;
  1672. }
  1673. tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
  1674. if (tape == NULL) {
  1675. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
  1676. drive->name);
  1677. goto failed;
  1678. }
  1679. g = alloc_disk(1 << PARTN_BITS);
  1680. if (!g)
  1681. goto out_free_tape;
  1682. ide_init_disk(g, drive);
  1683. tape->dev.parent = &drive->gendev;
  1684. tape->dev.release = ide_tape_release;
  1685. dev_set_name(&tape->dev, "%s", dev_name(&drive->gendev));
  1686. if (device_register(&tape->dev))
  1687. goto out_free_disk;
  1688. tape->drive = drive;
  1689. tape->driver = &idetape_driver;
  1690. tape->disk = g;
  1691. g->private_data = &tape->driver;
  1692. drive->driver_data = tape;
  1693. mutex_lock(&idetape_ref_mutex);
  1694. for (minor = 0; idetape_devs[minor]; minor++)
  1695. ;
  1696. idetape_devs[minor] = tape;
  1697. mutex_unlock(&idetape_ref_mutex);
  1698. idetape_setup(drive, tape, minor);
  1699. device_create(idetape_sysfs_class, &drive->gendev,
  1700. MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
  1701. device_create(idetape_sysfs_class, &drive->gendev,
  1702. MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
  1703. "n%s", tape->name);
  1704. g->fops = &idetape_block_ops;
  1705. ide_register_region(g);
  1706. return 0;
  1707. out_free_disk:
  1708. put_disk(g);
  1709. out_free_tape:
  1710. kfree(tape);
  1711. failed:
  1712. return -ENODEV;
  1713. }
  1714. static void __exit idetape_exit(void)
  1715. {
  1716. driver_unregister(&idetape_driver.gen_driver);
  1717. class_destroy(idetape_sysfs_class);
  1718. unregister_chrdev(IDETAPE_MAJOR, "ht");
  1719. }
  1720. static int __init idetape_init(void)
  1721. {
  1722. int error = 1;
  1723. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  1724. if (IS_ERR(idetape_sysfs_class)) {
  1725. idetape_sysfs_class = NULL;
  1726. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  1727. error = -EBUSY;
  1728. goto out;
  1729. }
  1730. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  1731. printk(KERN_ERR "ide-tape: Failed to register chrdev"
  1732. " interface\n");
  1733. error = -EBUSY;
  1734. goto out_free_class;
  1735. }
  1736. error = driver_register(&idetape_driver.gen_driver);
  1737. if (error)
  1738. goto out_free_chrdev;
  1739. return 0;
  1740. out_free_chrdev:
  1741. unregister_chrdev(IDETAPE_MAJOR, "ht");
  1742. out_free_class:
  1743. class_destroy(idetape_sysfs_class);
  1744. out:
  1745. return error;
  1746. }
  1747. MODULE_ALIAS("ide:*m-tape*");
  1748. module_init(idetape_init);
  1749. module_exit(idetape_exit);
  1750. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
  1751. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  1752. MODULE_LICENSE("GPL");