ide-tape.c 68 KB

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