ide-tape.c 56 KB

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