ide-tape.c 66 KB

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