ide-tape.c 68 KB

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