ide-tape.c 54 KB

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