ide-tape.c 56 KB

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