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