ide-tape.c 66 KB

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