ide-tape.c 55 KB

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