ide-tape.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  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. int buffer_size;
  1503. u16 *ctl = (u16 *)&tape->caps[12];
  1504. ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
  1505. drive->pc_callback = ide_tape_callback;
  1506. drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
  1507. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  1508. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  1509. tape->name);
  1510. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1511. }
  1512. /* Seagate Travan drives do not support DSC overlap. */
  1513. if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
  1514. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1515. tape->minor = minor;
  1516. tape->name[0] = 'h';
  1517. tape->name[1] = 't';
  1518. tape->name[2] = '0' + minor;
  1519. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1520. idetape_get_inquiry_results(drive);
  1521. idetape_get_mode_sense_results(drive);
  1522. ide_tape_get_bsize_from_bdesc(drive);
  1523. tape->user_bs_factor = 1;
  1524. tape->buffer_size = *ctl * tape->blk_size;
  1525. while (tape->buffer_size > 0xffff) {
  1526. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  1527. *ctl /= 2;
  1528. tape->buffer_size = *ctl * tape->blk_size;
  1529. }
  1530. buffer_size = tape->buffer_size;
  1531. /* select the "best" DSC read/write polling freq */
  1532. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  1533. t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
  1534. /*
  1535. * Ensure that the number we got makes sense; limit it within
  1536. * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  1537. */
  1538. tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
  1539. IDETAPE_DSC_RW_MAX);
  1540. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  1541. "%ums tDSC%s\n",
  1542. drive->name, tape->name, *(u16 *)&tape->caps[14],
  1543. (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
  1544. tape->buffer_size / 1024,
  1545. jiffies_to_msecs(tape->best_dsc_rw_freq),
  1546. (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
  1547. ide_proc_register_driver(drive, tape->driver);
  1548. }
  1549. static void ide_tape_remove(ide_drive_t *drive)
  1550. {
  1551. idetape_tape_t *tape = drive->driver_data;
  1552. ide_proc_unregister_driver(drive, tape->driver);
  1553. device_del(&tape->dev);
  1554. ide_unregister_region(tape->disk);
  1555. mutex_lock(&idetape_ref_mutex);
  1556. put_device(&tape->dev);
  1557. mutex_unlock(&idetape_ref_mutex);
  1558. }
  1559. static void ide_tape_release(struct device *dev)
  1560. {
  1561. struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
  1562. ide_drive_t *drive = tape->drive;
  1563. struct gendisk *g = tape->disk;
  1564. BUG_ON(tape->valid);
  1565. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1566. drive->driver_data = NULL;
  1567. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  1568. device_destroy(idetape_sysfs_class,
  1569. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  1570. idetape_devs[tape->minor] = NULL;
  1571. g->private_data = NULL;
  1572. put_disk(g);
  1573. kfree(tape);
  1574. }
  1575. #ifdef CONFIG_IDE_PROC_FS
  1576. static int idetape_name_proc_show(struct seq_file *m, void *v)
  1577. {
  1578. ide_drive_t *drive = (ide_drive_t *) m->private;
  1579. idetape_tape_t *tape = drive->driver_data;
  1580. seq_printf(m, "%s\n", tape->name);
  1581. return 0;
  1582. }
  1583. static ide_proc_entry_t idetape_proc[] = {
  1584. { "capacity", S_IFREG|S_IRUGO, ide_capacity_proc_show },
  1585. { "name", S_IFREG|S_IRUGO, idetape_name_proc_show },
  1586. {}
  1587. };
  1588. static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
  1589. {
  1590. return idetape_proc;
  1591. }
  1592. static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
  1593. {
  1594. return idetape_settings;
  1595. }
  1596. #endif
  1597. static int ide_tape_probe(ide_drive_t *);
  1598. static struct ide_driver idetape_driver = {
  1599. .gen_driver = {
  1600. .owner = THIS_MODULE,
  1601. .name = "ide-tape",
  1602. .bus = &ide_bus_type,
  1603. },
  1604. .probe = ide_tape_probe,
  1605. .remove = ide_tape_remove,
  1606. .version = IDETAPE_VERSION,
  1607. .do_request = idetape_do_request,
  1608. #ifdef CONFIG_IDE_PROC_FS
  1609. .proc_entries = ide_tape_proc_entries,
  1610. .proc_devsets = ide_tape_proc_devsets,
  1611. #endif
  1612. };
  1613. /* Our character device supporting functions, passed to register_chrdev. */
  1614. static const struct file_operations idetape_fops = {
  1615. .owner = THIS_MODULE,
  1616. .read = idetape_chrdev_read,
  1617. .write = idetape_chrdev_write,
  1618. .unlocked_ioctl = idetape_chrdev_ioctl,
  1619. .open = idetape_chrdev_open,
  1620. .release = idetape_chrdev_release,
  1621. .llseek = noop_llseek,
  1622. };
  1623. static int idetape_open(struct block_device *bdev, fmode_t mode)
  1624. {
  1625. struct ide_tape_obj *tape;
  1626. mutex_lock(&ide_tape_mutex);
  1627. tape = ide_tape_get(bdev->bd_disk, false, 0);
  1628. mutex_unlock(&ide_tape_mutex);
  1629. if (!tape)
  1630. return -ENXIO;
  1631. return 0;
  1632. }
  1633. static void idetape_release(struct gendisk *disk, fmode_t mode)
  1634. {
  1635. struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
  1636. mutex_lock(&ide_tape_mutex);
  1637. ide_tape_put(tape);
  1638. mutex_unlock(&ide_tape_mutex);
  1639. }
  1640. static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
  1641. unsigned int cmd, unsigned long arg)
  1642. {
  1643. struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
  1644. ide_drive_t *drive = tape->drive;
  1645. int err;
  1646. mutex_lock(&ide_tape_mutex);
  1647. err = generic_ide_ioctl(drive, bdev, cmd, arg);
  1648. if (err == -EINVAL)
  1649. err = idetape_blkdev_ioctl(drive, cmd, arg);
  1650. mutex_unlock(&ide_tape_mutex);
  1651. return err;
  1652. }
  1653. static const struct block_device_operations idetape_block_ops = {
  1654. .owner = THIS_MODULE,
  1655. .open = idetape_open,
  1656. .release = idetape_release,
  1657. .ioctl = idetape_ioctl,
  1658. };
  1659. static int ide_tape_probe(ide_drive_t *drive)
  1660. {
  1661. idetape_tape_t *tape;
  1662. struct gendisk *g;
  1663. int minor;
  1664. ide_debug_log(IDE_DBG_FUNC, "enter");
  1665. if (!strstr(DRV_NAME, drive->driver_req))
  1666. goto failed;
  1667. if (drive->media != ide_tape)
  1668. goto failed;
  1669. if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
  1670. ide_check_atapi_device(drive, DRV_NAME) == 0) {
  1671. printk(KERN_ERR "ide-tape: %s: not supported by this version of"
  1672. " the driver\n", drive->name);
  1673. goto failed;
  1674. }
  1675. tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
  1676. if (tape == NULL) {
  1677. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
  1678. drive->name);
  1679. goto failed;
  1680. }
  1681. g = alloc_disk(1 << PARTN_BITS);
  1682. if (!g)
  1683. goto out_free_tape;
  1684. ide_init_disk(g, drive);
  1685. tape->dev.parent = &drive->gendev;
  1686. tape->dev.release = ide_tape_release;
  1687. dev_set_name(&tape->dev, "%s", dev_name(&drive->gendev));
  1688. if (device_register(&tape->dev))
  1689. goto out_free_disk;
  1690. tape->drive = drive;
  1691. tape->driver = &idetape_driver;
  1692. tape->disk = g;
  1693. g->private_data = &tape->driver;
  1694. drive->driver_data = tape;
  1695. mutex_lock(&idetape_ref_mutex);
  1696. for (minor = 0; idetape_devs[minor]; minor++)
  1697. ;
  1698. idetape_devs[minor] = tape;
  1699. mutex_unlock(&idetape_ref_mutex);
  1700. idetape_setup(drive, tape, minor);
  1701. device_create(idetape_sysfs_class, &drive->gendev,
  1702. MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
  1703. device_create(idetape_sysfs_class, &drive->gendev,
  1704. MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
  1705. "n%s", tape->name);
  1706. g->fops = &idetape_block_ops;
  1707. ide_register_region(g);
  1708. return 0;
  1709. out_free_disk:
  1710. put_disk(g);
  1711. out_free_tape:
  1712. kfree(tape);
  1713. failed:
  1714. return -ENODEV;
  1715. }
  1716. static void __exit idetape_exit(void)
  1717. {
  1718. driver_unregister(&idetape_driver.gen_driver);
  1719. class_destroy(idetape_sysfs_class);
  1720. unregister_chrdev(IDETAPE_MAJOR, "ht");
  1721. }
  1722. static int __init idetape_init(void)
  1723. {
  1724. int error = 1;
  1725. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  1726. if (IS_ERR(idetape_sysfs_class)) {
  1727. idetape_sysfs_class = NULL;
  1728. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  1729. error = -EBUSY;
  1730. goto out;
  1731. }
  1732. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  1733. printk(KERN_ERR "ide-tape: Failed to register chrdev"
  1734. " interface\n");
  1735. error = -EBUSY;
  1736. goto out_free_class;
  1737. }
  1738. error = driver_register(&idetape_driver.gen_driver);
  1739. if (error)
  1740. goto out_free_chrdev;
  1741. return 0;
  1742. out_free_chrdev:
  1743. unregister_chrdev(IDETAPE_MAJOR, "ht");
  1744. out_free_class:
  1745. class_destroy(idetape_sysfs_class);
  1746. out:
  1747. return error;
  1748. }
  1749. MODULE_ALIAS("ide:*m-tape*");
  1750. module_init(idetape_init);
  1751. module_exit(idetape_exit);
  1752. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
  1753. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  1754. MODULE_LICENSE("GPL");