ide-tape.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937
  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 IDETAPE_VERSION "1.20"
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/timer.h>
  24. #include <linux/mm.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/major.h>
  28. #include <linux/errno.h>
  29. #include <linux/genhd.h>
  30. #include <linux/slab.h>
  31. #include <linux/pci.h>
  32. #include <linux/ide.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/completion.h>
  35. #include <linux/bitops.h>
  36. #include <linux/mutex.h>
  37. #include <scsi/scsi.h>
  38. #include <asm/byteorder.h>
  39. #include <linux/irq.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/io.h>
  42. #include <asm/unaligned.h>
  43. #include <linux/mtio.h>
  44. enum {
  45. /* output errors only */
  46. DBG_ERR = (1 << 0),
  47. /* output all sense key/asc */
  48. DBG_SENSE = (1 << 1),
  49. /* info regarding all chrdev-related procedures */
  50. DBG_CHRDEV = (1 << 2),
  51. /* all remaining procedures */
  52. DBG_PROCS = (1 << 3),
  53. /* buffer alloc info (pc_stack & rq_stack) */
  54. DBG_PCRQ_STACK = (1 << 4),
  55. };
  56. /* define to see debug info */
  57. #define IDETAPE_DEBUG_LOG 0
  58. #if IDETAPE_DEBUG_LOG
  59. #define debug_log(lvl, fmt, args...) \
  60. { \
  61. if (tape->debug_mask & lvl) \
  62. printk(KERN_INFO "ide-tape: " fmt, ## args); \
  63. }
  64. #else
  65. #define debug_log(lvl, fmt, args...) do {} while (0)
  66. #endif
  67. /**************************** Tunable parameters *****************************/
  68. /*
  69. * After each failed packet command we issue a request sense command and retry
  70. * the packet command IDETAPE_MAX_PC_RETRIES times.
  71. *
  72. * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
  73. */
  74. #define IDETAPE_MAX_PC_RETRIES 3
  75. /*
  76. * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE
  77. * bytes. This is used for several packet commands (Not for READ/WRITE commands)
  78. */
  79. #define IDETAPE_PC_BUFFER_SIZE 256
  80. /*
  81. * In various places in the driver, we need to allocate storage
  82. * for packet commands and requests, which will remain valid while
  83. * we leave the driver to wait for an interrupt or a timeout event.
  84. */
  85. #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
  86. /*
  87. * Some drives (for example, Seagate STT3401A Travan) require a very long
  88. * timeout, because they don't return an interrupt or clear their busy bit
  89. * until after the command completes (even retension commands).
  90. */
  91. #define IDETAPE_WAIT_CMD (900*HZ)
  92. /*
  93. * The following parameter is used to select the point in the internal tape fifo
  94. * in which we will start to refill the buffer. Decreasing the following
  95. * parameter will improve the system's latency and interactive response, while
  96. * using a high value might improve system throughput.
  97. */
  98. #define IDETAPE_FIFO_THRESHOLD 2
  99. /*
  100. * DSC polling parameters.
  101. *
  102. * Polling for DSC (a single bit in the status register) is a very important
  103. * function in ide-tape. There are two cases in which we poll for DSC:
  104. *
  105. * 1. Before a read/write packet command, to ensure that we can transfer data
  106. * from/to the tape's data buffers, without causing an actual media access.
  107. * In case the tape is not ready yet, we take out our request from the device
  108. * request queue, so that ide.c could service requests from the other device
  109. * on the same interface in the meantime.
  110. *
  111. * 2. After the successful initialization of a "media access packet command",
  112. * which is a command that can take a long time to complete (the interval can
  113. * range from several seconds to even an hour). Again, we postpone our request
  114. * in the middle to free the bus for the other device. The polling frequency
  115. * here should be lower than the read/write frequency since those media access
  116. * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
  117. * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
  118. * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
  119. *
  120. * We also set a timeout for the timer, in case something goes wrong. The
  121. * timeout should be longer then the maximum execution time of a tape operation.
  122. */
  123. /* DSC timings. */
  124. #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
  125. #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
  126. #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
  127. #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
  128. #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
  129. #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
  130. #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
  131. /*************************** End of tunable parameters ***********************/
  132. /* tape directions */
  133. enum {
  134. IDETAPE_DIR_NONE = (1 << 0),
  135. IDETAPE_DIR_READ = (1 << 1),
  136. IDETAPE_DIR_WRITE = (1 << 2),
  137. };
  138. struct idetape_bh {
  139. u32 b_size;
  140. atomic_t b_count;
  141. struct idetape_bh *b_reqnext;
  142. char *b_data;
  143. };
  144. /* Tape door status */
  145. #define DOOR_UNLOCKED 0
  146. #define DOOR_LOCKED 1
  147. #define DOOR_EXPLICITLY_LOCKED 2
  148. /* Some defines for the SPACE command */
  149. #define IDETAPE_SPACE_OVER_FILEMARK 1
  150. #define IDETAPE_SPACE_TO_EOD 3
  151. /* Some defines for the LOAD UNLOAD command */
  152. #define IDETAPE_LU_LOAD_MASK 1
  153. #define IDETAPE_LU_RETENSION_MASK 2
  154. #define IDETAPE_LU_EOT_MASK 4
  155. /*
  156. * Special requests for our block device strategy routine.
  157. *
  158. * In order to service a character device command, we add special requests to
  159. * the tail of our block device request queue and wait for their completion.
  160. */
  161. enum {
  162. REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
  163. REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
  164. REQ_IDETAPE_READ = (1 << 2),
  165. REQ_IDETAPE_WRITE = (1 << 3),
  166. };
  167. /* Error codes returned in rq->errors to the higher part of the driver. */
  168. #define IDETAPE_ERROR_GENERAL 101
  169. #define IDETAPE_ERROR_FILEMARK 102
  170. #define IDETAPE_ERROR_EOD 103
  171. /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
  172. #define IDETAPE_BLOCK_DESCRIPTOR 0
  173. #define IDETAPE_CAPABILITIES_PAGE 0x2a
  174. /* Tape flag bits values. */
  175. enum {
  176. IDETAPE_FLAG_IGNORE_DSC = (1 << 0),
  177. /* 0 When the tape position is unknown */
  178. IDETAPE_FLAG_ADDRESS_VALID = (1 << 1),
  179. /* Device already opened */
  180. IDETAPE_FLAG_BUSY = (1 << 2),
  181. /* Attempt to auto-detect the current user block size */
  182. IDETAPE_FLAG_DETECT_BS = (1 << 3),
  183. /* Currently on a filemark */
  184. IDETAPE_FLAG_FILEMARK = (1 << 4),
  185. /* DRQ interrupt device */
  186. IDETAPE_FLAG_DRQ_INTERRUPT = (1 << 5),
  187. /* 0 = no tape is loaded, so we don't rewind after ejecting */
  188. IDETAPE_FLAG_MEDIUM_PRESENT = (1 << 6),
  189. };
  190. /*
  191. * Most of our global data which we need to save even as we leave the driver due
  192. * to an interrupt or a timer event is stored in the struct defined below.
  193. */
  194. typedef struct ide_tape_obj {
  195. ide_drive_t *drive;
  196. ide_driver_t *driver;
  197. struct gendisk *disk;
  198. struct kref kref;
  199. /*
  200. * Since a typical character device operation requires more
  201. * than one packet command, we provide here enough memory
  202. * for the maximum of interconnected packet commands.
  203. * The packet commands are stored in the circular array pc_stack.
  204. * pc_stack_index points to the last used entry, and warps around
  205. * to the start when we get to the last array entry.
  206. *
  207. * pc points to the current processed packet command.
  208. *
  209. * failed_pc points to the last failed packet command, or contains
  210. * NULL if we do not need to retry any packet command. This is
  211. * required since an additional packet command is needed before the
  212. * retry, to get detailed information on what went wrong.
  213. */
  214. /* Current packet command */
  215. struct ide_atapi_pc *pc;
  216. /* Last failed packet command */
  217. struct ide_atapi_pc *failed_pc;
  218. /* Packet command stack */
  219. struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK];
  220. /* Next free packet command storage space */
  221. int pc_stack_index;
  222. struct request rq_stack[IDETAPE_PC_STACK];
  223. /* We implement a circular array */
  224. int rq_stack_index;
  225. /*
  226. * DSC polling variables.
  227. *
  228. * While polling for DSC we use postponed_rq to postpone the current
  229. * request so that ide.c will be able to service pending requests on the
  230. * other device. Note that at most we will have only one DSC (usually
  231. * data transfer) request in the device request queue.
  232. */
  233. struct request *postponed_rq;
  234. /* The time in which we started polling for DSC */
  235. unsigned long dsc_polling_start;
  236. /* Timer used to poll for dsc */
  237. struct timer_list dsc_timer;
  238. /* Read/Write dsc polling frequency */
  239. unsigned long best_dsc_rw_freq;
  240. unsigned long dsc_poll_freq;
  241. unsigned long dsc_timeout;
  242. /* Read position information */
  243. u8 partition;
  244. /* Current block */
  245. unsigned int first_frame;
  246. /* Last error information */
  247. u8 sense_key, asc, ascq;
  248. /* Character device operation */
  249. unsigned int minor;
  250. /* device name */
  251. char name[4];
  252. /* Current character device data transfer direction */
  253. u8 chrdev_dir;
  254. /* tape block size, usually 512 or 1024 bytes */
  255. unsigned short blk_size;
  256. int user_bs_factor;
  257. /* Copy of the tape's Capabilities and Mechanical Page */
  258. u8 caps[20];
  259. /*
  260. * Active data transfer request parameters.
  261. *
  262. * At most, there is only one ide-tape originated data transfer request
  263. * in the device request queue. This allows ide.c to easily service
  264. * requests from the other device when we postpone our active request.
  265. */
  266. /* Data buffer size chosen based on the tape's recommendation */
  267. int buffer_size;
  268. /* merge buffer */
  269. struct idetape_bh *merge_bh;
  270. /* size of the merge buffer */
  271. int merge_bh_size;
  272. /* pointer to current buffer head within the merge buffer */
  273. struct idetape_bh *bh;
  274. char *b_data;
  275. int b_count;
  276. int pages_per_buffer;
  277. /* Wasted space in each stage */
  278. int excess_bh_size;
  279. /* Status/Action flags: long for set_bit */
  280. unsigned long flags;
  281. /* protects the ide-tape queue */
  282. spinlock_t lock;
  283. /* Measures average tape speed */
  284. unsigned long avg_time;
  285. int avg_size;
  286. int avg_speed;
  287. /* the door is currently locked */
  288. int door_locked;
  289. /* the tape hardware is write protected */
  290. char drv_write_prot;
  291. /* the tape is write protected (hardware or opened as read-only) */
  292. char write_prot;
  293. u32 debug_mask;
  294. } idetape_tape_t;
  295. static DEFINE_MUTEX(idetape_ref_mutex);
  296. static struct class *idetape_sysfs_class;
  297. #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
  298. #define ide_tape_g(disk) \
  299. container_of((disk)->private_data, struct ide_tape_obj, driver)
  300. static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
  301. {
  302. struct ide_tape_obj *tape = NULL;
  303. mutex_lock(&idetape_ref_mutex);
  304. tape = ide_tape_g(disk);
  305. if (tape)
  306. kref_get(&tape->kref);
  307. mutex_unlock(&idetape_ref_mutex);
  308. return tape;
  309. }
  310. static void ide_tape_release(struct kref *);
  311. static void ide_tape_put(struct ide_tape_obj *tape)
  312. {
  313. mutex_lock(&idetape_ref_mutex);
  314. kref_put(&tape->kref, ide_tape_release);
  315. mutex_unlock(&idetape_ref_mutex);
  316. }
  317. /*
  318. * The variables below are used for the character device interface. Additional
  319. * state variables are defined in our ide_drive_t structure.
  320. */
  321. static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
  322. #define ide_tape_f(file) ((file)->private_data)
  323. static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
  324. {
  325. struct ide_tape_obj *tape = NULL;
  326. mutex_lock(&idetape_ref_mutex);
  327. tape = idetape_devs[i];
  328. if (tape)
  329. kref_get(&tape->kref);
  330. mutex_unlock(&idetape_ref_mutex);
  331. return tape;
  332. }
  333. static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  334. unsigned int bcount)
  335. {
  336. struct idetape_bh *bh = pc->bh;
  337. int count;
  338. while (bcount) {
  339. if (bh == NULL) {
  340. printk(KERN_ERR "ide-tape: bh == NULL in "
  341. "idetape_input_buffers\n");
  342. ide_pad_transfer(drive, 0, bcount);
  343. return;
  344. }
  345. count = min(
  346. (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
  347. bcount);
  348. drive->hwif->input_data(drive, NULL, bh->b_data +
  349. atomic_read(&bh->b_count), count);
  350. bcount -= count;
  351. atomic_add(count, &bh->b_count);
  352. if (atomic_read(&bh->b_count) == bh->b_size) {
  353. bh = bh->b_reqnext;
  354. if (bh)
  355. atomic_set(&bh->b_count, 0);
  356. }
  357. }
  358. pc->bh = bh;
  359. }
  360. static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  361. unsigned int bcount)
  362. {
  363. struct idetape_bh *bh = pc->bh;
  364. int count;
  365. while (bcount) {
  366. if (bh == NULL) {
  367. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  368. __func__);
  369. return;
  370. }
  371. count = min((unsigned int)pc->b_count, (unsigned int)bcount);
  372. drive->hwif->output_data(drive, NULL, pc->b_data, count);
  373. bcount -= count;
  374. pc->b_data += count;
  375. pc->b_count -= count;
  376. if (!pc->b_count) {
  377. bh = bh->b_reqnext;
  378. pc->bh = bh;
  379. if (bh) {
  380. pc->b_data = bh->b_data;
  381. pc->b_count = atomic_read(&bh->b_count);
  382. }
  383. }
  384. }
  385. }
  386. static void idetape_update_buffers(struct ide_atapi_pc *pc)
  387. {
  388. struct idetape_bh *bh = pc->bh;
  389. int count;
  390. unsigned int bcount = pc->xferred;
  391. if (pc->flags & PC_FLAG_WRITING)
  392. return;
  393. while (bcount) {
  394. if (bh == NULL) {
  395. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  396. __func__);
  397. return;
  398. }
  399. count = min((unsigned int)bh->b_size, (unsigned int)bcount);
  400. atomic_set(&bh->b_count, count);
  401. if (atomic_read(&bh->b_count) == bh->b_size)
  402. bh = bh->b_reqnext;
  403. bcount -= count;
  404. }
  405. pc->bh = bh;
  406. }
  407. /*
  408. * idetape_next_pc_storage returns a pointer to a place in which we can
  409. * safely store a packet command, even though we intend to leave the
  410. * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
  411. * commands is allocated at initialization time.
  412. */
  413. static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
  414. {
  415. idetape_tape_t *tape = drive->driver_data;
  416. debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
  417. if (tape->pc_stack_index == IDETAPE_PC_STACK)
  418. tape->pc_stack_index = 0;
  419. return (&tape->pc_stack[tape->pc_stack_index++]);
  420. }
  421. /*
  422. * idetape_next_rq_storage is used along with idetape_next_pc_storage.
  423. * Since we queue packet commands in the request queue, we need to
  424. * allocate a request, along with the allocation of a packet command.
  425. */
  426. /**************************************************************
  427. * *
  428. * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
  429. * followed later on by kfree(). -ml *
  430. * *
  431. **************************************************************/
  432. static struct request *idetape_next_rq_storage(ide_drive_t *drive)
  433. {
  434. idetape_tape_t *tape = drive->driver_data;
  435. debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
  436. if (tape->rq_stack_index == IDETAPE_PC_STACK)
  437. tape->rq_stack_index = 0;
  438. return (&tape->rq_stack[tape->rq_stack_index++]);
  439. }
  440. /*
  441. * called on each failed packet command retry to analyze the request sense. We
  442. * currently do not utilize this information.
  443. */
  444. static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
  445. {
  446. idetape_tape_t *tape = drive->driver_data;
  447. struct ide_atapi_pc *pc = tape->failed_pc;
  448. tape->sense_key = sense[2] & 0xF;
  449. tape->asc = sense[12];
  450. tape->ascq = sense[13];
  451. debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
  452. pc->c[0], tape->sense_key, tape->asc, tape->ascq);
  453. /* Correct pc->xferred by asking the tape. */
  454. if (pc->flags & PC_FLAG_DMA_ERROR) {
  455. pc->xferred = pc->req_xfer -
  456. tape->blk_size *
  457. get_unaligned_be32(&sense[3]);
  458. idetape_update_buffers(pc);
  459. }
  460. /*
  461. * If error was the result of a zero-length read or write command,
  462. * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
  463. * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
  464. */
  465. if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
  466. /* length == 0 */
  467. && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
  468. if (tape->sense_key == 5) {
  469. /* don't report an error, everything's ok */
  470. pc->error = 0;
  471. /* don't retry read/write */
  472. pc->flags |= PC_FLAG_ABORT;
  473. }
  474. }
  475. if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
  476. pc->error = IDETAPE_ERROR_FILEMARK;
  477. pc->flags |= PC_FLAG_ABORT;
  478. }
  479. if (pc->c[0] == WRITE_6) {
  480. if ((sense[2] & 0x40) || (tape->sense_key == 0xd
  481. && tape->asc == 0x0 && tape->ascq == 0x2)) {
  482. pc->error = IDETAPE_ERROR_EOD;
  483. pc->flags |= PC_FLAG_ABORT;
  484. }
  485. }
  486. if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
  487. if (tape->sense_key == 8) {
  488. pc->error = IDETAPE_ERROR_EOD;
  489. pc->flags |= PC_FLAG_ABORT;
  490. }
  491. if (!(pc->flags & PC_FLAG_ABORT) &&
  492. pc->xferred)
  493. pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
  494. }
  495. }
  496. /* Free data buffers completely. */
  497. static void ide_tape_kfree_buffer(idetape_tape_t *tape)
  498. {
  499. struct idetape_bh *prev_bh, *bh = tape->merge_bh;
  500. while (bh) {
  501. u32 size = bh->b_size;
  502. while (size) {
  503. unsigned int order = fls(size >> PAGE_SHIFT)-1;
  504. if (bh->b_data)
  505. free_pages((unsigned long)bh->b_data, order);
  506. size &= (order-1);
  507. bh->b_data += (1 << order) * PAGE_SIZE;
  508. }
  509. prev_bh = bh;
  510. bh = bh->b_reqnext;
  511. kfree(prev_bh);
  512. }
  513. kfree(tape->merge_bh);
  514. }
  515. static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
  516. {
  517. struct request *rq = HWGROUP(drive)->rq;
  518. idetape_tape_t *tape = drive->driver_data;
  519. unsigned long flags;
  520. int error;
  521. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  522. switch (uptodate) {
  523. case 0: error = IDETAPE_ERROR_GENERAL; break;
  524. case 1: error = 0; break;
  525. default: error = uptodate;
  526. }
  527. rq->errors = error;
  528. if (error)
  529. tape->failed_pc = NULL;
  530. if (!blk_special_request(rq)) {
  531. ide_end_request(drive, uptodate, nr_sects);
  532. return 0;
  533. }
  534. spin_lock_irqsave(&tape->lock, flags);
  535. ide_end_drive_cmd(drive, 0, 0);
  536. spin_unlock_irqrestore(&tape->lock, flags);
  537. return 0;
  538. }
  539. static void ide_tape_callback(ide_drive_t *drive)
  540. {
  541. idetape_tape_t *tape = drive->driver_data;
  542. struct ide_atapi_pc *pc = tape->pc;
  543. int uptodate = pc->error ? 0 : 1;
  544. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  545. if (pc->c[0] == REQUEST_SENSE) {
  546. if (uptodate)
  547. idetape_analyze_error(drive, pc->buf);
  548. else
  549. printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
  550. "itself - Aborting request!\n");
  551. } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
  552. struct request *rq = drive->hwif->hwgroup->rq;
  553. int blocks = pc->xferred / tape->blk_size;
  554. tape->avg_size += blocks * tape->blk_size;
  555. if (time_after_eq(jiffies, tape->avg_time + HZ)) {
  556. tape->avg_speed = tape->avg_size * HZ /
  557. (jiffies - tape->avg_time) / 1024;
  558. tape->avg_size = 0;
  559. tape->avg_time = jiffies;
  560. }
  561. tape->first_frame += blocks;
  562. rq->current_nr_sectors -= blocks;
  563. if (pc->error)
  564. uptodate = pc->error;
  565. } else if (pc->c[0] == READ_POSITION && uptodate) {
  566. u8 *readpos = tape->pc->buf;
  567. debug_log(DBG_SENSE, "BOP - %s\n",
  568. (readpos[0] & 0x80) ? "Yes" : "No");
  569. debug_log(DBG_SENSE, "EOP - %s\n",
  570. (readpos[0] & 0x40) ? "Yes" : "No");
  571. if (readpos[0] & 0x4) {
  572. printk(KERN_INFO "ide-tape: Block location is unknown"
  573. "to the tape\n");
  574. clear_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
  575. uptodate = 0;
  576. } else {
  577. debug_log(DBG_SENSE, "Block Location - %u\n",
  578. be32_to_cpu(*(u32 *)&readpos[4]));
  579. tape->partition = readpos[1];
  580. tape->first_frame = be32_to_cpu(*(u32 *)&readpos[4]);
  581. set_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags);
  582. }
  583. }
  584. idetape_end_request(drive, uptodate, 0);
  585. }
  586. static void idetape_init_pc(struct ide_atapi_pc *pc)
  587. {
  588. memset(pc->c, 0, 12);
  589. pc->retries = 0;
  590. pc->flags = 0;
  591. pc->req_xfer = 0;
  592. pc->buf = pc->pc_buf;
  593. pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
  594. pc->bh = NULL;
  595. pc->b_data = NULL;
  596. pc->idetape_callback = ide_tape_callback;
  597. }
  598. static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc)
  599. {
  600. idetape_init_pc(pc);
  601. pc->c[0] = REQUEST_SENSE;
  602. pc->c[4] = 20;
  603. pc->req_xfer = 20;
  604. }
  605. static void idetape_init_rq(struct request *rq, u8 cmd)
  606. {
  607. blk_rq_init(NULL, rq);
  608. rq->cmd_type = REQ_TYPE_SPECIAL;
  609. rq->cmd[0] = cmd;
  610. }
  611. /*
  612. * Generate a new packet command request in front of the request queue, before
  613. * the current request, so that it will be processed immediately, on the next
  614. * pass through the driver. The function below is called from the request
  615. * handling part of the driver (the "bottom" part). Safe storage for the request
  616. * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that.
  617. *
  618. * Memory for those requests is pre-allocated at initialization time, and is
  619. * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for
  620. * the maximum possible number of inter-dependent packet commands.
  621. *
  622. * The higher level of the driver - The ioctl handler and the character device
  623. * handling functions should queue request to the lower level part and wait for
  624. * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail.
  625. */
  626. static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
  627. struct request *rq)
  628. {
  629. struct ide_tape_obj *tape = drive->driver_data;
  630. idetape_init_rq(rq, REQ_IDETAPE_PC1);
  631. rq->cmd_flags |= REQ_PREEMPT;
  632. rq->buffer = (char *) pc;
  633. rq->rq_disk = tape->disk;
  634. ide_do_drive_cmd(drive, rq);
  635. }
  636. /*
  637. * idetape_retry_pc is called when an error was detected during the
  638. * last packet command. We queue a request sense packet command in
  639. * the head of the request list.
  640. */
  641. static void idetape_retry_pc(ide_drive_t *drive)
  642. {
  643. idetape_tape_t *tape = drive->driver_data;
  644. struct ide_atapi_pc *pc;
  645. struct request *rq;
  646. (void)ide_read_error(drive);
  647. pc = idetape_next_pc_storage(drive);
  648. rq = idetape_next_rq_storage(drive);
  649. idetape_create_request_sense_cmd(pc);
  650. set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
  651. idetape_queue_pc_head(drive, pc, rq);
  652. }
  653. /*
  654. * Postpone the current request so that ide.c will be able to service requests
  655. * from another device on the same hwgroup while we are polling for DSC.
  656. */
  657. static void idetape_postpone_request(ide_drive_t *drive)
  658. {
  659. idetape_tape_t *tape = drive->driver_data;
  660. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  661. tape->postponed_rq = HWGROUP(drive)->rq;
  662. ide_stall_queue(drive, tape->dsc_poll_freq);
  663. }
  664. typedef void idetape_io_buf(ide_drive_t *, struct ide_atapi_pc *, unsigned int);
  665. /*
  666. * This is the usual interrupt handler which will be called during a packet
  667. * command. We will transfer some of the data (as requested by the drive) and
  668. * will re-point interrupt handler to us. When data transfer is finished, we
  669. * will act according to the algorithm described before
  670. * idetape_issue_pc.
  671. */
  672. static ide_startstop_t idetape_pc_intr(ide_drive_t *drive)
  673. {
  674. ide_hwif_t *hwif = drive->hwif;
  675. idetape_tape_t *tape = drive->driver_data;
  676. struct ide_atapi_pc *pc = tape->pc;
  677. xfer_func_t *xferfunc;
  678. idetape_io_buf *iobuf;
  679. unsigned int temp;
  680. u16 bcount;
  681. u8 stat, ireason;
  682. debug_log(DBG_PROCS, "Enter %s - interrupt handler\n", __func__);
  683. /* Clear the interrupt */
  684. stat = ide_read_status(drive);
  685. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  686. if (hwif->dma_ops->dma_end(drive) || (stat & ERR_STAT)) {
  687. pc->flags |= PC_FLAG_DMA_ERROR;
  688. } else {
  689. pc->xferred = pc->req_xfer;
  690. idetape_update_buffers(pc);
  691. }
  692. debug_log(DBG_PROCS, "DMA finished\n");
  693. }
  694. /* No more interrupts */
  695. if ((stat & DRQ_STAT) == 0) {
  696. debug_log(DBG_SENSE, "Packet command completed, %d bytes"
  697. " transferred\n", pc->xferred);
  698. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  699. local_irq_enable_in_hardirq();
  700. if ((stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
  701. stat &= ~ERR_STAT;
  702. if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
  703. /* Error detected */
  704. debug_log(DBG_ERR, "%s: I/O error\n", tape->name);
  705. if (pc->c[0] == REQUEST_SENSE) {
  706. printk(KERN_ERR "%s: I/O error in request sense"
  707. " command\n", drive->name);
  708. return ide_do_reset(drive);
  709. }
  710. debug_log(DBG_ERR, "[cmd %x]: check condition\n",
  711. pc->c[0]);
  712. /* Retry operation */
  713. idetape_retry_pc(drive);
  714. return ide_stopped;
  715. }
  716. pc->error = 0;
  717. if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
  718. (stat & SEEK_STAT) == 0) {
  719. /* Media access command */
  720. tape->dsc_polling_start = jiffies;
  721. tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
  722. tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
  723. /* Allow ide.c to handle other requests */
  724. idetape_postpone_request(drive);
  725. return ide_stopped;
  726. }
  727. if (tape->failed_pc == pc)
  728. tape->failed_pc = NULL;
  729. /* Command finished - Call the callback function */
  730. pc->idetape_callback(drive);
  731. return ide_stopped;
  732. }
  733. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  734. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  735. printk(KERN_ERR "%s: The device wants to issue more interrupts "
  736. "in DMA mode\n", drive->name);
  737. ide_dma_off(drive);
  738. return ide_do_reset(drive);
  739. }
  740. /* Get the number of bytes to transfer on this interrupt. */
  741. bcount = (hwif->INB(hwif->io_ports.lbah_addr) << 8) |
  742. hwif->INB(hwif->io_ports.lbam_addr);
  743. ireason = hwif->INB(hwif->io_ports.nsect_addr);
  744. if (ireason & CD) {
  745. printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
  746. return ide_do_reset(drive);
  747. }
  748. if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
  749. /* Hopefully, we will never get here */
  750. printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
  751. "to %s!\n", drive->name,
  752. (ireason & IO) ? "Write" : "Read",
  753. (ireason & IO) ? "Read" : "Write");
  754. return ide_do_reset(drive);
  755. }
  756. if (!(pc->flags & PC_FLAG_WRITING)) {
  757. /* Reading - Check that we have enough space */
  758. temp = pc->xferred + bcount;
  759. if (temp > pc->req_xfer) {
  760. if (temp > pc->buf_size) {
  761. printk(KERN_ERR "%s: The device wants to send "
  762. "us more data than expected - "
  763. "discarding data\n",
  764. drive->name);
  765. ide_pad_transfer(drive, 0, bcount);
  766. ide_set_handler(drive, &idetape_pc_intr,
  767. IDETAPE_WAIT_CMD, NULL);
  768. return ide_started;
  769. }
  770. debug_log(DBG_SENSE, "The device wants to send us more "
  771. "data than expected - allowing transfer\n");
  772. }
  773. iobuf = &idetape_input_buffers;
  774. xferfunc = hwif->input_data;
  775. } else {
  776. iobuf = &idetape_output_buffers;
  777. xferfunc = hwif->output_data;
  778. }
  779. if (pc->bh)
  780. iobuf(drive, pc, bcount);
  781. else
  782. xferfunc(drive, NULL, pc->cur_pos, bcount);
  783. /* Update the current position */
  784. pc->xferred += bcount;
  785. pc->cur_pos += bcount;
  786. debug_log(DBG_SENSE, "[cmd %x] transferred %d bytes on that intr.\n",
  787. pc->c[0], bcount);
  788. /* And set the interrupt handler again */
  789. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  790. return ide_started;
  791. }
  792. /*
  793. * Packet Command Interface
  794. *
  795. * The current Packet Command is available in tape->pc, and will not change
  796. * until we finish handling it. Each packet command is associated with a
  797. * callback function that will be called when the command is finished.
  798. *
  799. * The handling will be done in three stages:
  800. *
  801. * 1. idetape_issue_pc will send the packet command to the drive, and will set
  802. * the interrupt handler to idetape_pc_intr.
  803. *
  804. * 2. On each interrupt, idetape_pc_intr will be called. This step will be
  805. * repeated until the device signals us that no more interrupts will be issued.
  806. *
  807. * 3. ATAPI Tape media access commands have immediate status with a delayed
  808. * process. In case of a successful initiation of a media access packet command,
  809. * the DSC bit will be set when the actual execution of the command is finished.
  810. * Since the tape drive will not issue an interrupt, we have to poll for this
  811. * event. In this case, we define the request as "low priority request" by
  812. * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
  813. * exit the driver.
  814. *
  815. * ide.c will then give higher priority to requests which originate from the
  816. * other device, until will change rq_status to RQ_ACTIVE.
  817. *
  818. * 4. When the packet command is finished, it will be checked for errors.
  819. *
  820. * 5. In case an error was found, we queue a request sense packet command in
  821. * front of the request queue and retry the operation up to
  822. * IDETAPE_MAX_PC_RETRIES times.
  823. *
  824. * 6. In case no error was found, or we decided to give up and not to retry
  825. * again, the callback function will be called and then we will handle the next
  826. * request.
  827. */
  828. static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
  829. {
  830. ide_hwif_t *hwif = drive->hwif;
  831. idetape_tape_t *tape = drive->driver_data;
  832. struct ide_atapi_pc *pc = tape->pc;
  833. int retries = 100;
  834. ide_startstop_t startstop;
  835. u8 ireason;
  836. if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
  837. printk(KERN_ERR "%s: Strange, packet command initiated yet "
  838. "DRQ isn't asserted\n", drive->name);
  839. return startstop;
  840. }
  841. ireason = hwif->INB(hwif->io_ports.nsect_addr);
  842. while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
  843. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  844. "a packet command, retrying\n", drive->name);
  845. udelay(100);
  846. ireason = hwif->INB(hwif->io_ports.nsect_addr);
  847. if (retries == 0) {
  848. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  849. "a packet command, ignoring\n",
  850. drive->name);
  851. ireason |= CD;
  852. ireason &= ~IO;
  853. }
  854. }
  855. if ((ireason & CD) == 0 || (ireason & IO)) {
  856. printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
  857. "a packet command\n", drive->name);
  858. return ide_do_reset(drive);
  859. }
  860. /* Set the interrupt routine */
  861. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  862. /* Begin DMA, if necessary */
  863. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS)
  864. hwif->dma_ops->dma_start(drive);
  865. /* Send the actual packet */
  866. hwif->output_data(drive, NULL, pc->c, 12);
  867. return ide_started;
  868. }
  869. static ide_startstop_t idetape_issue_pc(ide_drive_t *drive,
  870. struct ide_atapi_pc *pc)
  871. {
  872. ide_hwif_t *hwif = drive->hwif;
  873. idetape_tape_t *tape = drive->driver_data;
  874. int dma_ok = 0;
  875. u16 bcount;
  876. if (tape->pc->c[0] == REQUEST_SENSE &&
  877. pc->c[0] == REQUEST_SENSE) {
  878. printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
  879. "Two request sense in serial were issued\n");
  880. }
  881. if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
  882. tape->failed_pc = pc;
  883. /* Set the current packet command */
  884. tape->pc = pc;
  885. if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
  886. (pc->flags & PC_FLAG_ABORT)) {
  887. /*
  888. * We will "abort" retrying a packet command in case legitimate
  889. * error code was received (crossing a filemark, or end of the
  890. * media, for example).
  891. */
  892. if (!(pc->flags & PC_FLAG_ABORT)) {
  893. if (!(pc->c[0] == TEST_UNIT_READY &&
  894. tape->sense_key == 2 && tape->asc == 4 &&
  895. (tape->ascq == 1 || tape->ascq == 8))) {
  896. printk(KERN_ERR "ide-tape: %s: I/O error, "
  897. "pc = %2x, key = %2x, "
  898. "asc = %2x, ascq = %2x\n",
  899. tape->name, pc->c[0],
  900. tape->sense_key, tape->asc,
  901. tape->ascq);
  902. }
  903. /* Giving up */
  904. pc->error = IDETAPE_ERROR_GENERAL;
  905. }
  906. tape->failed_pc = NULL;
  907. pc->idetape_callback(drive);
  908. return ide_stopped;
  909. }
  910. debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
  911. pc->retries++;
  912. /* We haven't transferred any data yet */
  913. pc->xferred = 0;
  914. pc->cur_pos = pc->buf;
  915. /* Request to transfer the entire buffer at once */
  916. bcount = pc->req_xfer;
  917. if (pc->flags & PC_FLAG_DMA_ERROR) {
  918. pc->flags &= ~PC_FLAG_DMA_ERROR;
  919. ide_dma_off(drive);
  920. }
  921. if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
  922. dma_ok = !hwif->dma_ops->dma_setup(drive);
  923. ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
  924. if (dma_ok)
  925. /* Will begin DMA later */
  926. pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
  927. if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags)) {
  928. ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
  929. IDETAPE_WAIT_CMD, NULL);
  930. return ide_started;
  931. } else {
  932. ide_execute_pkt_cmd(drive);
  933. return idetape_transfer_pc(drive);
  934. }
  935. }
  936. /* A mode sense command is used to "sense" tape parameters. */
  937. static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
  938. {
  939. idetape_init_pc(pc);
  940. pc->c[0] = MODE_SENSE;
  941. if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
  942. /* DBD = 1 - Don't return block descriptors */
  943. pc->c[1] = 8;
  944. pc->c[2] = page_code;
  945. /*
  946. * Changed pc->c[3] to 0 (255 will at best return unused info).
  947. *
  948. * For SCSI this byte is defined as subpage instead of high byte
  949. * of length and some IDE drives seem to interpret it this way
  950. * and return an error when 255 is used.
  951. */
  952. pc->c[3] = 0;
  953. /* We will just discard data in that case */
  954. pc->c[4] = 255;
  955. if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
  956. pc->req_xfer = 12;
  957. else if (page_code == IDETAPE_CAPABILITIES_PAGE)
  958. pc->req_xfer = 24;
  959. else
  960. pc->req_xfer = 50;
  961. }
  962. static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
  963. {
  964. idetape_tape_t *tape = drive->driver_data;
  965. struct ide_atapi_pc *pc = tape->pc;
  966. u8 stat;
  967. stat = ide_read_status(drive);
  968. if (stat & SEEK_STAT) {
  969. if (stat & ERR_STAT) {
  970. /* Error detected */
  971. if (pc->c[0] != TEST_UNIT_READY)
  972. printk(KERN_ERR "ide-tape: %s: I/O error, ",
  973. tape->name);
  974. /* Retry operation */
  975. idetape_retry_pc(drive);
  976. return ide_stopped;
  977. }
  978. pc->error = 0;
  979. if (tape->failed_pc == pc)
  980. tape->failed_pc = NULL;
  981. } else {
  982. pc->error = IDETAPE_ERROR_GENERAL;
  983. tape->failed_pc = NULL;
  984. }
  985. pc->idetape_callback(drive);
  986. return ide_stopped;
  987. }
  988. static void idetape_create_read_cmd(idetape_tape_t *tape,
  989. struct ide_atapi_pc *pc,
  990. unsigned int length, struct idetape_bh *bh)
  991. {
  992. idetape_init_pc(pc);
  993. pc->c[0] = READ_6;
  994. put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
  995. pc->c[1] = 1;
  996. pc->bh = bh;
  997. atomic_set(&bh->b_count, 0);
  998. pc->buf = NULL;
  999. pc->buf_size = length * tape->blk_size;
  1000. pc->req_xfer = pc->buf_size;
  1001. if (pc->req_xfer == tape->buffer_size)
  1002. pc->flags |= PC_FLAG_DMA_RECOMMENDED;
  1003. }
  1004. static void idetape_create_write_cmd(idetape_tape_t *tape,
  1005. struct ide_atapi_pc *pc,
  1006. unsigned int length, struct idetape_bh *bh)
  1007. {
  1008. idetape_init_pc(pc);
  1009. pc->c[0] = WRITE_6;
  1010. put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
  1011. pc->c[1] = 1;
  1012. pc->flags |= PC_FLAG_WRITING;
  1013. pc->bh = bh;
  1014. pc->b_data = bh->b_data;
  1015. pc->b_count = atomic_read(&bh->b_count);
  1016. pc->buf = NULL;
  1017. pc->buf_size = length * tape->blk_size;
  1018. pc->req_xfer = pc->buf_size;
  1019. if (pc->req_xfer == tape->buffer_size)
  1020. pc->flags |= PC_FLAG_DMA_RECOMMENDED;
  1021. }
  1022. static ide_startstop_t idetape_do_request(ide_drive_t *drive,
  1023. struct request *rq, sector_t block)
  1024. {
  1025. idetape_tape_t *tape = drive->driver_data;
  1026. struct ide_atapi_pc *pc = NULL;
  1027. struct request *postponed_rq = tape->postponed_rq;
  1028. u8 stat;
  1029. debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
  1030. " current_nr_sectors: %d\n",
  1031. rq->sector, rq->nr_sectors, rq->current_nr_sectors);
  1032. if (!blk_special_request(rq)) {
  1033. /* We do not support buffer cache originated requests. */
  1034. printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
  1035. "request queue (%d)\n", drive->name, rq->cmd_type);
  1036. ide_end_request(drive, 0, 0);
  1037. return ide_stopped;
  1038. }
  1039. /* Retry a failed packet command */
  1040. if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE)
  1041. return idetape_issue_pc(drive, tape->failed_pc);
  1042. if (postponed_rq != NULL)
  1043. if (rq != postponed_rq) {
  1044. printk(KERN_ERR "ide-tape: ide-tape.c bug - "
  1045. "Two DSC requests were queued\n");
  1046. idetape_end_request(drive, 0, 0);
  1047. return ide_stopped;
  1048. }
  1049. tape->postponed_rq = NULL;
  1050. /*
  1051. * If the tape is still busy, postpone our request and service
  1052. * the other device meanwhile.
  1053. */
  1054. stat = ide_read_status(drive);
  1055. if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
  1056. set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
  1057. if (drive->post_reset == 1) {
  1058. set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
  1059. drive->post_reset = 0;
  1060. }
  1061. if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
  1062. (stat & SEEK_STAT) == 0) {
  1063. if (postponed_rq == NULL) {
  1064. tape->dsc_polling_start = jiffies;
  1065. tape->dsc_poll_freq = tape->best_dsc_rw_freq;
  1066. tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
  1067. } else if (time_after(jiffies, tape->dsc_timeout)) {
  1068. printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
  1069. tape->name);
  1070. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  1071. idetape_media_access_finished(drive);
  1072. return ide_stopped;
  1073. } else {
  1074. return ide_do_reset(drive);
  1075. }
  1076. } else if (time_after(jiffies,
  1077. tape->dsc_polling_start +
  1078. IDETAPE_DSC_MA_THRESHOLD))
  1079. tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
  1080. idetape_postpone_request(drive);
  1081. return ide_stopped;
  1082. }
  1083. if (rq->cmd[0] & REQ_IDETAPE_READ) {
  1084. pc = idetape_next_pc_storage(drive);
  1085. idetape_create_read_cmd(tape, pc, rq->current_nr_sectors,
  1086. (struct idetape_bh *)rq->special);
  1087. goto out;
  1088. }
  1089. if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
  1090. pc = idetape_next_pc_storage(drive);
  1091. idetape_create_write_cmd(tape, pc, rq->current_nr_sectors,
  1092. (struct idetape_bh *)rq->special);
  1093. goto out;
  1094. }
  1095. if (rq->cmd[0] & REQ_IDETAPE_PC1) {
  1096. pc = (struct ide_atapi_pc *) rq->buffer;
  1097. rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
  1098. rq->cmd[0] |= REQ_IDETAPE_PC2;
  1099. goto out;
  1100. }
  1101. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  1102. idetape_media_access_finished(drive);
  1103. return ide_stopped;
  1104. }
  1105. BUG();
  1106. out:
  1107. return idetape_issue_pc(drive, pc);
  1108. }
  1109. /*
  1110. * The function below uses __get_free_pages to allocate a data buffer of size
  1111. * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
  1112. * much as possible.
  1113. *
  1114. * It returns a pointer to the newly allocated buffer, or NULL in case of
  1115. * failure.
  1116. */
  1117. static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
  1118. int full, int clear)
  1119. {
  1120. struct idetape_bh *prev_bh, *bh, *merge_bh;
  1121. int pages = tape->pages_per_buffer;
  1122. unsigned int order, b_allocd;
  1123. char *b_data = NULL;
  1124. merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  1125. bh = merge_bh;
  1126. if (bh == NULL)
  1127. goto abort;
  1128. order = fls(pages) - 1;
  1129. bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
  1130. if (!bh->b_data)
  1131. goto abort;
  1132. b_allocd = (1 << order) * PAGE_SIZE;
  1133. pages &= (order-1);
  1134. if (clear)
  1135. memset(bh->b_data, 0, b_allocd);
  1136. bh->b_reqnext = NULL;
  1137. bh->b_size = b_allocd;
  1138. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  1139. while (pages) {
  1140. order = fls(pages) - 1;
  1141. b_data = (char *) __get_free_pages(GFP_KERNEL, order);
  1142. if (!b_data)
  1143. goto abort;
  1144. b_allocd = (1 << order) * PAGE_SIZE;
  1145. if (clear)
  1146. memset(b_data, 0, b_allocd);
  1147. /* newly allocated page frames below buffer header or ...*/
  1148. if (bh->b_data == b_data + b_allocd) {
  1149. bh->b_size += b_allocd;
  1150. bh->b_data -= b_allocd;
  1151. if (full)
  1152. atomic_add(b_allocd, &bh->b_count);
  1153. continue;
  1154. }
  1155. /* they are above the header */
  1156. if (b_data == bh->b_data + bh->b_size) {
  1157. bh->b_size += b_allocd;
  1158. if (full)
  1159. atomic_add(b_allocd, &bh->b_count);
  1160. continue;
  1161. }
  1162. prev_bh = bh;
  1163. bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  1164. if (!bh) {
  1165. free_pages((unsigned long) b_data, order);
  1166. goto abort;
  1167. }
  1168. bh->b_reqnext = NULL;
  1169. bh->b_data = b_data;
  1170. bh->b_size = b_allocd;
  1171. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  1172. prev_bh->b_reqnext = bh;
  1173. pages &= (order-1);
  1174. }
  1175. bh->b_size -= tape->excess_bh_size;
  1176. if (full)
  1177. atomic_sub(tape->excess_bh_size, &bh->b_count);
  1178. return merge_bh;
  1179. abort:
  1180. ide_tape_kfree_buffer(tape);
  1181. return NULL;
  1182. }
  1183. static int idetape_copy_stage_from_user(idetape_tape_t *tape,
  1184. const char __user *buf, int n)
  1185. {
  1186. struct idetape_bh *bh = tape->bh;
  1187. int count;
  1188. int ret = 0;
  1189. while (n) {
  1190. if (bh == NULL) {
  1191. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  1192. __func__);
  1193. return 1;
  1194. }
  1195. count = min((unsigned int)
  1196. (bh->b_size - atomic_read(&bh->b_count)),
  1197. (unsigned int)n);
  1198. if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
  1199. count))
  1200. ret = 1;
  1201. n -= count;
  1202. atomic_add(count, &bh->b_count);
  1203. buf += count;
  1204. if (atomic_read(&bh->b_count) == bh->b_size) {
  1205. bh = bh->b_reqnext;
  1206. if (bh)
  1207. atomic_set(&bh->b_count, 0);
  1208. }
  1209. }
  1210. tape->bh = bh;
  1211. return ret;
  1212. }
  1213. static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
  1214. int n)
  1215. {
  1216. struct idetape_bh *bh = tape->bh;
  1217. int count;
  1218. int ret = 0;
  1219. while (n) {
  1220. if (bh == NULL) {
  1221. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  1222. __func__);
  1223. return 1;
  1224. }
  1225. count = min(tape->b_count, n);
  1226. if (copy_to_user(buf, tape->b_data, count))
  1227. ret = 1;
  1228. n -= count;
  1229. tape->b_data += count;
  1230. tape->b_count -= count;
  1231. buf += count;
  1232. if (!tape->b_count) {
  1233. bh = bh->b_reqnext;
  1234. tape->bh = bh;
  1235. if (bh) {
  1236. tape->b_data = bh->b_data;
  1237. tape->b_count = atomic_read(&bh->b_count);
  1238. }
  1239. }
  1240. }
  1241. return ret;
  1242. }
  1243. static void idetape_init_merge_buffer(idetape_tape_t *tape)
  1244. {
  1245. struct idetape_bh *bh = tape->merge_bh;
  1246. tape->bh = tape->merge_bh;
  1247. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  1248. atomic_set(&bh->b_count, 0);
  1249. else {
  1250. tape->b_data = bh->b_data;
  1251. tape->b_count = atomic_read(&bh->b_count);
  1252. }
  1253. }
  1254. /*
  1255. * Write a filemark if write_filemark=1. Flush the device buffers without
  1256. * writing a filemark otherwise.
  1257. */
  1258. static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
  1259. struct ide_atapi_pc *pc, int write_filemark)
  1260. {
  1261. idetape_init_pc(pc);
  1262. pc->c[0] = WRITE_FILEMARKS;
  1263. pc->c[4] = write_filemark;
  1264. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1265. }
  1266. static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
  1267. {
  1268. idetape_init_pc(pc);
  1269. pc->c[0] = TEST_UNIT_READY;
  1270. }
  1271. /*
  1272. * We add a special packet command request to the tail of the request queue, and
  1273. * wait for it to be serviced. This is not to be called from within the request
  1274. * handling part of the driver! We allocate here data on the stack and it is
  1275. * valid until the request is finished. This is not the case for the bottom part
  1276. * of the driver, where we are always leaving the functions to wait for an
  1277. * interrupt or a timer event.
  1278. *
  1279. * From the bottom part of the driver, we should allocate safe memory using
  1280. * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
  1281. * to the request list without waiting for it to be serviced! In that case, we
  1282. * usually use idetape_queue_pc_head().
  1283. */
  1284. static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
  1285. {
  1286. struct ide_tape_obj *tape = drive->driver_data;
  1287. struct request *rq;
  1288. int error;
  1289. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  1290. rq->cmd_type = REQ_TYPE_SPECIAL;
  1291. rq->cmd[0] = REQ_IDETAPE_PC1;
  1292. rq->buffer = (char *)pc;
  1293. error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
  1294. blk_put_request(rq);
  1295. return error;
  1296. }
  1297. static void idetape_create_load_unload_cmd(ide_drive_t *drive,
  1298. struct ide_atapi_pc *pc, int cmd)
  1299. {
  1300. idetape_init_pc(pc);
  1301. pc->c[0] = START_STOP;
  1302. pc->c[4] = cmd;
  1303. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1304. }
  1305. static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
  1306. {
  1307. idetape_tape_t *tape = drive->driver_data;
  1308. struct ide_atapi_pc pc;
  1309. int load_attempted = 0;
  1310. /* Wait for the tape to become ready */
  1311. set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
  1312. timeout += jiffies;
  1313. while (time_before(jiffies, timeout)) {
  1314. idetape_create_test_unit_ready_cmd(&pc);
  1315. if (!idetape_queue_pc_tail(drive, &pc))
  1316. return 0;
  1317. if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
  1318. || (tape->asc == 0x3A)) {
  1319. /* no media */
  1320. if (load_attempted)
  1321. return -ENOMEDIUM;
  1322. idetape_create_load_unload_cmd(drive, &pc,
  1323. IDETAPE_LU_LOAD_MASK);
  1324. idetape_queue_pc_tail(drive, &pc);
  1325. load_attempted = 1;
  1326. /* not about to be ready */
  1327. } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
  1328. (tape->ascq == 1 || tape->ascq == 8)))
  1329. return -EIO;
  1330. msleep(100);
  1331. }
  1332. return -EIO;
  1333. }
  1334. static int idetape_flush_tape_buffers(ide_drive_t *drive)
  1335. {
  1336. struct ide_atapi_pc pc;
  1337. int rc;
  1338. idetape_create_write_filemark_cmd(drive, &pc, 0);
  1339. rc = idetape_queue_pc_tail(drive, &pc);
  1340. if (rc)
  1341. return rc;
  1342. idetape_wait_ready(drive, 60 * 5 * HZ);
  1343. return 0;
  1344. }
  1345. static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
  1346. {
  1347. idetape_init_pc(pc);
  1348. pc->c[0] = READ_POSITION;
  1349. pc->req_xfer = 20;
  1350. }
  1351. static int idetape_read_position(ide_drive_t *drive)
  1352. {
  1353. idetape_tape_t *tape = drive->driver_data;
  1354. struct ide_atapi_pc pc;
  1355. int position;
  1356. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1357. idetape_create_read_position_cmd(&pc);
  1358. if (idetape_queue_pc_tail(drive, &pc))
  1359. return -1;
  1360. position = tape->first_frame;
  1361. return position;
  1362. }
  1363. static void idetape_create_locate_cmd(ide_drive_t *drive,
  1364. struct ide_atapi_pc *pc,
  1365. unsigned int block, u8 partition, int skip)
  1366. {
  1367. idetape_init_pc(pc);
  1368. pc->c[0] = POSITION_TO_ELEMENT;
  1369. pc->c[1] = 2;
  1370. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
  1371. pc->c[8] = partition;
  1372. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1373. }
  1374. static int idetape_create_prevent_cmd(ide_drive_t *drive,
  1375. struct ide_atapi_pc *pc, int prevent)
  1376. {
  1377. idetape_tape_t *tape = drive->driver_data;
  1378. /* device supports locking according to capabilities page */
  1379. if (!(tape->caps[6] & 0x01))
  1380. return 0;
  1381. idetape_init_pc(pc);
  1382. pc->c[0] = ALLOW_MEDIUM_REMOVAL;
  1383. pc->c[4] = prevent;
  1384. return 1;
  1385. }
  1386. static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
  1387. {
  1388. idetape_tape_t *tape = drive->driver_data;
  1389. if (tape->chrdev_dir != IDETAPE_DIR_READ)
  1390. return;
  1391. clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
  1392. tape->merge_bh_size = 0;
  1393. if (tape->merge_bh != NULL) {
  1394. ide_tape_kfree_buffer(tape);
  1395. tape->merge_bh = NULL;
  1396. }
  1397. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1398. }
  1399. /*
  1400. * Position the tape to the requested block using the LOCATE packet command.
  1401. * A READ POSITION command is then issued to check where we are positioned. Like
  1402. * all higher level operations, we queue the commands at the tail of the request
  1403. * queue and wait for their completion.
  1404. */
  1405. static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
  1406. u8 partition, int skip)
  1407. {
  1408. idetape_tape_t *tape = drive->driver_data;
  1409. int retval;
  1410. struct ide_atapi_pc pc;
  1411. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1412. __ide_tape_discard_merge_buffer(drive);
  1413. idetape_wait_ready(drive, 60 * 5 * HZ);
  1414. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  1415. retval = idetape_queue_pc_tail(drive, &pc);
  1416. if (retval)
  1417. return (retval);
  1418. idetape_create_read_position_cmd(&pc);
  1419. return (idetape_queue_pc_tail(drive, &pc));
  1420. }
  1421. static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
  1422. int restore_position)
  1423. {
  1424. idetape_tape_t *tape = drive->driver_data;
  1425. int seek, position;
  1426. __ide_tape_discard_merge_buffer(drive);
  1427. if (restore_position) {
  1428. position = idetape_read_position(drive);
  1429. seek = position > 0 ? position : 0;
  1430. if (idetape_position_tape(drive, seek, 0, 0)) {
  1431. printk(KERN_INFO "ide-tape: %s: position_tape failed in"
  1432. " %s\n", tape->name, __func__);
  1433. return;
  1434. }
  1435. }
  1436. }
  1437. /*
  1438. * Generate a read/write request for the block device interface and wait for it
  1439. * to be serviced.
  1440. */
  1441. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
  1442. struct idetape_bh *bh)
  1443. {
  1444. idetape_tape_t *tape = drive->driver_data;
  1445. struct request *rq;
  1446. int ret, errors;
  1447. debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
  1448. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  1449. rq->cmd_type = REQ_TYPE_SPECIAL;
  1450. rq->cmd[0] = cmd;
  1451. rq->rq_disk = tape->disk;
  1452. rq->special = (void *)bh;
  1453. rq->sector = tape->first_frame;
  1454. rq->nr_sectors = blocks;
  1455. rq->current_nr_sectors = blocks;
  1456. blk_execute_rq(drive->queue, tape->disk, rq, 0);
  1457. errors = rq->errors;
  1458. ret = tape->blk_size * (blocks - rq->current_nr_sectors);
  1459. blk_put_request(rq);
  1460. if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
  1461. return 0;
  1462. if (tape->merge_bh)
  1463. idetape_init_merge_buffer(tape);
  1464. if (errors == IDETAPE_ERROR_GENERAL)
  1465. return -EIO;
  1466. return ret;
  1467. }
  1468. static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
  1469. {
  1470. idetape_init_pc(pc);
  1471. pc->c[0] = INQUIRY;
  1472. pc->c[4] = 254;
  1473. pc->req_xfer = 254;
  1474. }
  1475. static void idetape_create_rewind_cmd(ide_drive_t *drive,
  1476. struct ide_atapi_pc *pc)
  1477. {
  1478. idetape_init_pc(pc);
  1479. pc->c[0] = REZERO_UNIT;
  1480. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1481. }
  1482. static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
  1483. {
  1484. idetape_init_pc(pc);
  1485. pc->c[0] = ERASE;
  1486. pc->c[1] = 1;
  1487. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1488. }
  1489. static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
  1490. {
  1491. idetape_init_pc(pc);
  1492. pc->c[0] = SPACE;
  1493. put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
  1494. pc->c[1] = cmd;
  1495. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1496. }
  1497. /* Queue up a character device originated write request. */
  1498. static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
  1499. {
  1500. idetape_tape_t *tape = drive->driver_data;
  1501. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1502. return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
  1503. blocks, tape->merge_bh);
  1504. }
  1505. static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
  1506. {
  1507. idetape_tape_t *tape = drive->driver_data;
  1508. int blocks, min;
  1509. struct idetape_bh *bh;
  1510. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1511. printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
  1512. " but we are not writing.\n");
  1513. return;
  1514. }
  1515. if (tape->merge_bh_size > tape->buffer_size) {
  1516. printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
  1517. tape->merge_bh_size = tape->buffer_size;
  1518. }
  1519. if (tape->merge_bh_size) {
  1520. blocks = tape->merge_bh_size / tape->blk_size;
  1521. if (tape->merge_bh_size % tape->blk_size) {
  1522. unsigned int i;
  1523. blocks++;
  1524. i = tape->blk_size - tape->merge_bh_size %
  1525. tape->blk_size;
  1526. bh = tape->bh->b_reqnext;
  1527. while (bh) {
  1528. atomic_set(&bh->b_count, 0);
  1529. bh = bh->b_reqnext;
  1530. }
  1531. bh = tape->bh;
  1532. while (i) {
  1533. if (bh == NULL) {
  1534. printk(KERN_INFO "ide-tape: bug,"
  1535. " bh NULL\n");
  1536. break;
  1537. }
  1538. min = min(i, (unsigned int)(bh->b_size -
  1539. atomic_read(&bh->b_count)));
  1540. memset(bh->b_data + atomic_read(&bh->b_count),
  1541. 0, min);
  1542. atomic_add(min, &bh->b_count);
  1543. i -= min;
  1544. bh = bh->b_reqnext;
  1545. }
  1546. }
  1547. (void) idetape_add_chrdev_write_request(drive, blocks);
  1548. tape->merge_bh_size = 0;
  1549. }
  1550. if (tape->merge_bh != NULL) {
  1551. ide_tape_kfree_buffer(tape);
  1552. tape->merge_bh = NULL;
  1553. }
  1554. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1555. }
  1556. static int idetape_init_read(ide_drive_t *drive)
  1557. {
  1558. idetape_tape_t *tape = drive->driver_data;
  1559. int bytes_read;
  1560. /* Initialize read operation */
  1561. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1562. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1563. ide_tape_flush_merge_buffer(drive);
  1564. idetape_flush_tape_buffers(drive);
  1565. }
  1566. if (tape->merge_bh || tape->merge_bh_size) {
  1567. printk(KERN_ERR "ide-tape: merge_bh_size should be"
  1568. " 0 now\n");
  1569. tape->merge_bh_size = 0;
  1570. }
  1571. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
  1572. if (!tape->merge_bh)
  1573. return -ENOMEM;
  1574. tape->chrdev_dir = IDETAPE_DIR_READ;
  1575. /*
  1576. * Issue a read 0 command to ensure that DSC handshake is
  1577. * switched from completion mode to buffer available mode.
  1578. * No point in issuing this if DSC overlap isn't supported, some
  1579. * drives (Seagate STT3401A) will return an error.
  1580. */
  1581. if (drive->dsc_overlap) {
  1582. bytes_read = idetape_queue_rw_tail(drive,
  1583. REQ_IDETAPE_READ, 0,
  1584. tape->merge_bh);
  1585. if (bytes_read < 0) {
  1586. ide_tape_kfree_buffer(tape);
  1587. tape->merge_bh = NULL;
  1588. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1589. return bytes_read;
  1590. }
  1591. }
  1592. }
  1593. return 0;
  1594. }
  1595. /* called from idetape_chrdev_read() to service a chrdev read request. */
  1596. static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
  1597. {
  1598. idetape_tape_t *tape = drive->driver_data;
  1599. debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
  1600. /* If we are at a filemark, return a read length of 0 */
  1601. if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
  1602. return 0;
  1603. idetape_init_read(drive);
  1604. return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
  1605. tape->merge_bh);
  1606. }
  1607. static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
  1608. {
  1609. idetape_tape_t *tape = drive->driver_data;
  1610. struct idetape_bh *bh;
  1611. int blocks;
  1612. while (bcount) {
  1613. unsigned int count;
  1614. bh = tape->merge_bh;
  1615. count = min(tape->buffer_size, bcount);
  1616. bcount -= count;
  1617. blocks = count / tape->blk_size;
  1618. while (count) {
  1619. atomic_set(&bh->b_count,
  1620. min(count, (unsigned int)bh->b_size));
  1621. memset(bh->b_data, 0, atomic_read(&bh->b_count));
  1622. count -= atomic_read(&bh->b_count);
  1623. bh = bh->b_reqnext;
  1624. }
  1625. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
  1626. tape->merge_bh);
  1627. }
  1628. }
  1629. /*
  1630. * Rewinds the tape to the Beginning Of the current Partition (BOP). We
  1631. * currently support only one partition.
  1632. */
  1633. static int idetape_rewind_tape(ide_drive_t *drive)
  1634. {
  1635. int retval;
  1636. struct ide_atapi_pc pc;
  1637. idetape_tape_t *tape;
  1638. tape = drive->driver_data;
  1639. debug_log(DBG_SENSE, "Enter %s\n", __func__);
  1640. idetape_create_rewind_cmd(drive, &pc);
  1641. retval = idetape_queue_pc_tail(drive, &pc);
  1642. if (retval)
  1643. return retval;
  1644. idetape_create_read_position_cmd(&pc);
  1645. retval = idetape_queue_pc_tail(drive, &pc);
  1646. if (retval)
  1647. return retval;
  1648. return 0;
  1649. }
  1650. /* mtio.h compatible commands should be issued to the chrdev interface. */
  1651. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
  1652. unsigned long arg)
  1653. {
  1654. idetape_tape_t *tape = drive->driver_data;
  1655. void __user *argp = (void __user *)arg;
  1656. struct idetape_config {
  1657. int dsc_rw_frequency;
  1658. int dsc_media_access_frequency;
  1659. int nr_stages;
  1660. } config;
  1661. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1662. switch (cmd) {
  1663. case 0x0340:
  1664. if (copy_from_user(&config, argp, sizeof(config)))
  1665. return -EFAULT;
  1666. tape->best_dsc_rw_freq = config.dsc_rw_frequency;
  1667. break;
  1668. case 0x0350:
  1669. config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
  1670. config.nr_stages = 1;
  1671. if (copy_to_user(argp, &config, sizeof(config)))
  1672. return -EFAULT;
  1673. break;
  1674. default:
  1675. return -EIO;
  1676. }
  1677. return 0;
  1678. }
  1679. static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
  1680. int mt_count)
  1681. {
  1682. idetape_tape_t *tape = drive->driver_data;
  1683. struct ide_atapi_pc pc;
  1684. int retval, count = 0;
  1685. int sprev = !!(tape->caps[4] & 0x20);
  1686. if (mt_count == 0)
  1687. return 0;
  1688. if (MTBSF == mt_op || MTBSFM == mt_op) {
  1689. if (!sprev)
  1690. return -EIO;
  1691. mt_count = -mt_count;
  1692. }
  1693. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1694. tape->merge_bh_size = 0;
  1695. if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
  1696. ++count;
  1697. ide_tape_discard_merge_buffer(drive, 0);
  1698. }
  1699. switch (mt_op) {
  1700. case MTFSF:
  1701. case MTBSF:
  1702. idetape_create_space_cmd(&pc, mt_count - count,
  1703. IDETAPE_SPACE_OVER_FILEMARK);
  1704. return idetape_queue_pc_tail(drive, &pc);
  1705. case MTFSFM:
  1706. case MTBSFM:
  1707. if (!sprev)
  1708. return -EIO;
  1709. retval = idetape_space_over_filemarks(drive, MTFSF,
  1710. mt_count - count);
  1711. if (retval)
  1712. return retval;
  1713. count = (MTBSFM == mt_op ? 1 : -1);
  1714. return idetape_space_over_filemarks(drive, MTFSF, count);
  1715. default:
  1716. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1717. mt_op);
  1718. return -EIO;
  1719. }
  1720. }
  1721. /*
  1722. * Our character device read / write functions.
  1723. *
  1724. * The tape is optimized to maximize throughput when it is transferring an
  1725. * integral number of the "continuous transfer limit", which is a parameter of
  1726. * the specific tape (26kB on my particular tape, 32kB for Onstream).
  1727. *
  1728. * As of version 1.3 of the driver, the character device provides an abstract
  1729. * continuous view of the media - any mix of block sizes (even 1 byte) on the
  1730. * same backup/restore procedure is supported. The driver will internally
  1731. * convert the requests to the recommended transfer unit, so that an unmatch
  1732. * between the user's block size to the recommended size will only result in a
  1733. * (slightly) increased driver overhead, but will no longer hit performance.
  1734. * This is not applicable to Onstream.
  1735. */
  1736. static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
  1737. size_t count, loff_t *ppos)
  1738. {
  1739. struct ide_tape_obj *tape = ide_tape_f(file);
  1740. ide_drive_t *drive = tape->drive;
  1741. ssize_t bytes_read, temp, actually_read = 0, rc;
  1742. ssize_t ret = 0;
  1743. u16 ctl = *(u16 *)&tape->caps[12];
  1744. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1745. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1746. if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
  1747. if (count > tape->blk_size &&
  1748. (count % tape->blk_size) == 0)
  1749. tape->user_bs_factor = count / tape->blk_size;
  1750. }
  1751. rc = idetape_init_read(drive);
  1752. if (rc < 0)
  1753. return rc;
  1754. if (count == 0)
  1755. return (0);
  1756. if (tape->merge_bh_size) {
  1757. actually_read = min((unsigned int)(tape->merge_bh_size),
  1758. (unsigned int)count);
  1759. if (idetape_copy_stage_to_user(tape, buf, actually_read))
  1760. ret = -EFAULT;
  1761. buf += actually_read;
  1762. tape->merge_bh_size -= actually_read;
  1763. count -= actually_read;
  1764. }
  1765. while (count >= tape->buffer_size) {
  1766. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1767. if (bytes_read <= 0)
  1768. goto finish;
  1769. if (idetape_copy_stage_to_user(tape, buf, bytes_read))
  1770. ret = -EFAULT;
  1771. buf += bytes_read;
  1772. count -= bytes_read;
  1773. actually_read += bytes_read;
  1774. }
  1775. if (count) {
  1776. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1777. if (bytes_read <= 0)
  1778. goto finish;
  1779. temp = min((unsigned long)count, (unsigned long)bytes_read);
  1780. if (idetape_copy_stage_to_user(tape, buf, temp))
  1781. ret = -EFAULT;
  1782. actually_read += temp;
  1783. tape->merge_bh_size = bytes_read-temp;
  1784. }
  1785. finish:
  1786. if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
  1787. debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
  1788. idetape_space_over_filemarks(drive, MTFSF, 1);
  1789. return 0;
  1790. }
  1791. return ret ? ret : actually_read;
  1792. }
  1793. static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
  1794. size_t count, loff_t *ppos)
  1795. {
  1796. struct ide_tape_obj *tape = ide_tape_f(file);
  1797. ide_drive_t *drive = tape->drive;
  1798. ssize_t actually_written = 0;
  1799. ssize_t ret = 0;
  1800. u16 ctl = *(u16 *)&tape->caps[12];
  1801. /* The drive is write protected. */
  1802. if (tape->write_prot)
  1803. return -EACCES;
  1804. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1805. /* Initialize write operation */
  1806. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1807. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1808. ide_tape_discard_merge_buffer(drive, 1);
  1809. if (tape->merge_bh || tape->merge_bh_size) {
  1810. printk(KERN_ERR "ide-tape: merge_bh_size "
  1811. "should be 0 now\n");
  1812. tape->merge_bh_size = 0;
  1813. }
  1814. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
  1815. if (!tape->merge_bh)
  1816. return -ENOMEM;
  1817. tape->chrdev_dir = IDETAPE_DIR_WRITE;
  1818. idetape_init_merge_buffer(tape);
  1819. /*
  1820. * Issue a write 0 command to ensure that DSC handshake is
  1821. * switched from completion mode to buffer available mode. No
  1822. * point in issuing this if DSC overlap isn't supported, some
  1823. * drives (Seagate STT3401A) will return an error.
  1824. */
  1825. if (drive->dsc_overlap) {
  1826. ssize_t retval = idetape_queue_rw_tail(drive,
  1827. REQ_IDETAPE_WRITE, 0,
  1828. tape->merge_bh);
  1829. if (retval < 0) {
  1830. ide_tape_kfree_buffer(tape);
  1831. tape->merge_bh = NULL;
  1832. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1833. return retval;
  1834. }
  1835. }
  1836. }
  1837. if (count == 0)
  1838. return (0);
  1839. if (tape->merge_bh_size) {
  1840. if (tape->merge_bh_size >= tape->buffer_size) {
  1841. printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
  1842. tape->merge_bh_size = 0;
  1843. }
  1844. actually_written = min((unsigned int)
  1845. (tape->buffer_size - tape->merge_bh_size),
  1846. (unsigned int)count);
  1847. if (idetape_copy_stage_from_user(tape, buf, actually_written))
  1848. ret = -EFAULT;
  1849. buf += actually_written;
  1850. tape->merge_bh_size += actually_written;
  1851. count -= actually_written;
  1852. if (tape->merge_bh_size == tape->buffer_size) {
  1853. ssize_t retval;
  1854. tape->merge_bh_size = 0;
  1855. retval = idetape_add_chrdev_write_request(drive, ctl);
  1856. if (retval <= 0)
  1857. return (retval);
  1858. }
  1859. }
  1860. while (count >= tape->buffer_size) {
  1861. ssize_t retval;
  1862. if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
  1863. ret = -EFAULT;
  1864. buf += tape->buffer_size;
  1865. count -= tape->buffer_size;
  1866. retval = idetape_add_chrdev_write_request(drive, ctl);
  1867. actually_written += tape->buffer_size;
  1868. if (retval <= 0)
  1869. return (retval);
  1870. }
  1871. if (count) {
  1872. actually_written += count;
  1873. if (idetape_copy_stage_from_user(tape, buf, count))
  1874. ret = -EFAULT;
  1875. tape->merge_bh_size += count;
  1876. }
  1877. return ret ? ret : actually_written;
  1878. }
  1879. static int idetape_write_filemark(ide_drive_t *drive)
  1880. {
  1881. struct ide_atapi_pc pc;
  1882. /* Write a filemark */
  1883. idetape_create_write_filemark_cmd(drive, &pc, 1);
  1884. if (idetape_queue_pc_tail(drive, &pc)) {
  1885. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  1886. return -EIO;
  1887. }
  1888. return 0;
  1889. }
  1890. /*
  1891. * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
  1892. * requested.
  1893. *
  1894. * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
  1895. * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
  1896. * usually not supported.
  1897. *
  1898. * The following commands are currently not supported:
  1899. *
  1900. * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
  1901. * MT_ST_WRITE_THRESHOLD.
  1902. */
  1903. static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
  1904. {
  1905. idetape_tape_t *tape = drive->driver_data;
  1906. struct ide_atapi_pc pc;
  1907. int i, retval;
  1908. debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
  1909. mt_op, mt_count);
  1910. switch (mt_op) {
  1911. case MTFSF:
  1912. case MTFSFM:
  1913. case MTBSF:
  1914. case MTBSFM:
  1915. if (!mt_count)
  1916. return 0;
  1917. return idetape_space_over_filemarks(drive, mt_op, mt_count);
  1918. default:
  1919. break;
  1920. }
  1921. switch (mt_op) {
  1922. case MTWEOF:
  1923. if (tape->write_prot)
  1924. return -EACCES;
  1925. ide_tape_discard_merge_buffer(drive, 1);
  1926. for (i = 0; i < mt_count; i++) {
  1927. retval = idetape_write_filemark(drive);
  1928. if (retval)
  1929. return retval;
  1930. }
  1931. return 0;
  1932. case MTREW:
  1933. ide_tape_discard_merge_buffer(drive, 0);
  1934. if (idetape_rewind_tape(drive))
  1935. return -EIO;
  1936. return 0;
  1937. case MTLOAD:
  1938. ide_tape_discard_merge_buffer(drive, 0);
  1939. idetape_create_load_unload_cmd(drive, &pc,
  1940. IDETAPE_LU_LOAD_MASK);
  1941. return idetape_queue_pc_tail(drive, &pc);
  1942. case MTUNLOAD:
  1943. case MTOFFL:
  1944. /*
  1945. * If door is locked, attempt to unlock before
  1946. * attempting to eject.
  1947. */
  1948. if (tape->door_locked) {
  1949. if (idetape_create_prevent_cmd(drive, &pc, 0))
  1950. if (!idetape_queue_pc_tail(drive, &pc))
  1951. tape->door_locked = DOOR_UNLOCKED;
  1952. }
  1953. ide_tape_discard_merge_buffer(drive, 0);
  1954. idetape_create_load_unload_cmd(drive, &pc,
  1955. !IDETAPE_LU_LOAD_MASK);
  1956. retval = idetape_queue_pc_tail(drive, &pc);
  1957. if (!retval)
  1958. clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
  1959. return retval;
  1960. case MTNOP:
  1961. ide_tape_discard_merge_buffer(drive, 0);
  1962. return idetape_flush_tape_buffers(drive);
  1963. case MTRETEN:
  1964. ide_tape_discard_merge_buffer(drive, 0);
  1965. idetape_create_load_unload_cmd(drive, &pc,
  1966. IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  1967. return idetape_queue_pc_tail(drive, &pc);
  1968. case MTEOM:
  1969. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  1970. return idetape_queue_pc_tail(drive, &pc);
  1971. case MTERASE:
  1972. (void)idetape_rewind_tape(drive);
  1973. idetape_create_erase_cmd(&pc);
  1974. return idetape_queue_pc_tail(drive, &pc);
  1975. case MTSETBLK:
  1976. if (mt_count) {
  1977. if (mt_count < tape->blk_size ||
  1978. mt_count % tape->blk_size)
  1979. return -EIO;
  1980. tape->user_bs_factor = mt_count / tape->blk_size;
  1981. clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
  1982. } else
  1983. set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
  1984. return 0;
  1985. case MTSEEK:
  1986. ide_tape_discard_merge_buffer(drive, 0);
  1987. return idetape_position_tape(drive,
  1988. mt_count * tape->user_bs_factor, tape->partition, 0);
  1989. case MTSETPART:
  1990. ide_tape_discard_merge_buffer(drive, 0);
  1991. return idetape_position_tape(drive, 0, mt_count, 0);
  1992. case MTFSR:
  1993. case MTBSR:
  1994. case MTLOCK:
  1995. if (!idetape_create_prevent_cmd(drive, &pc, 1))
  1996. return 0;
  1997. retval = idetape_queue_pc_tail(drive, &pc);
  1998. if (retval)
  1999. return retval;
  2000. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  2001. return 0;
  2002. case MTUNLOCK:
  2003. if (!idetape_create_prevent_cmd(drive, &pc, 0))
  2004. return 0;
  2005. retval = idetape_queue_pc_tail(drive, &pc);
  2006. if (retval)
  2007. return retval;
  2008. tape->door_locked = DOOR_UNLOCKED;
  2009. return 0;
  2010. default:
  2011. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  2012. mt_op);
  2013. return -EIO;
  2014. }
  2015. }
  2016. /*
  2017. * Our character device ioctls. General mtio.h magnetic io commands are
  2018. * supported here, and not in the corresponding block interface. Our own
  2019. * ide-tape ioctls are supported on both interfaces.
  2020. */
  2021. static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
  2022. unsigned int cmd, unsigned long arg)
  2023. {
  2024. struct ide_tape_obj *tape = ide_tape_f(file);
  2025. ide_drive_t *drive = tape->drive;
  2026. struct mtop mtop;
  2027. struct mtget mtget;
  2028. struct mtpos mtpos;
  2029. int block_offset = 0, position = tape->first_frame;
  2030. void __user *argp = (void __user *)arg;
  2031. debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
  2032. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  2033. ide_tape_flush_merge_buffer(drive);
  2034. idetape_flush_tape_buffers(drive);
  2035. }
  2036. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  2037. block_offset = tape->merge_bh_size /
  2038. (tape->blk_size * tape->user_bs_factor);
  2039. position = idetape_read_position(drive);
  2040. if (position < 0)
  2041. return -EIO;
  2042. }
  2043. switch (cmd) {
  2044. case MTIOCTOP:
  2045. if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
  2046. return -EFAULT;
  2047. return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
  2048. case MTIOCGET:
  2049. memset(&mtget, 0, sizeof(struct mtget));
  2050. mtget.mt_type = MT_ISSCSI2;
  2051. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  2052. mtget.mt_dsreg =
  2053. ((tape->blk_size * tape->user_bs_factor)
  2054. << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  2055. if (tape->drv_write_prot)
  2056. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  2057. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  2058. return -EFAULT;
  2059. return 0;
  2060. case MTIOCPOS:
  2061. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  2062. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  2063. return -EFAULT;
  2064. return 0;
  2065. default:
  2066. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  2067. ide_tape_discard_merge_buffer(drive, 1);
  2068. return idetape_blkdev_ioctl(drive, cmd, arg);
  2069. }
  2070. }
  2071. /*
  2072. * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
  2073. * block size with the reported value.
  2074. */
  2075. static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
  2076. {
  2077. idetape_tape_t *tape = drive->driver_data;
  2078. struct ide_atapi_pc pc;
  2079. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  2080. if (idetape_queue_pc_tail(drive, &pc)) {
  2081. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  2082. if (tape->blk_size == 0) {
  2083. printk(KERN_WARNING "ide-tape: Cannot deal with zero "
  2084. "block size, assuming 32k\n");
  2085. tape->blk_size = 32768;
  2086. }
  2087. return;
  2088. }
  2089. tape->blk_size = (pc.buf[4 + 5] << 16) +
  2090. (pc.buf[4 + 6] << 8) +
  2091. pc.buf[4 + 7];
  2092. tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
  2093. }
  2094. static int idetape_chrdev_open(struct inode *inode, struct file *filp)
  2095. {
  2096. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  2097. ide_drive_t *drive;
  2098. idetape_tape_t *tape;
  2099. struct ide_atapi_pc pc;
  2100. int retval;
  2101. if (i >= MAX_HWIFS * MAX_DRIVES)
  2102. return -ENXIO;
  2103. tape = ide_tape_chrdev_get(i);
  2104. if (!tape)
  2105. return -ENXIO;
  2106. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  2107. /*
  2108. * We really want to do nonseekable_open(inode, filp); here, but some
  2109. * versions of tar incorrectly call lseek on tapes and bail out if that
  2110. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  2111. */
  2112. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  2113. drive = tape->drive;
  2114. filp->private_data = tape;
  2115. if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
  2116. retval = -EBUSY;
  2117. goto out_put_tape;
  2118. }
  2119. retval = idetape_wait_ready(drive, 60 * HZ);
  2120. if (retval) {
  2121. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  2122. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  2123. goto out_put_tape;
  2124. }
  2125. idetape_read_position(drive);
  2126. if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
  2127. (void)idetape_rewind_tape(drive);
  2128. /* Read block size and write protect status from drive. */
  2129. ide_tape_get_bsize_from_bdesc(drive);
  2130. /* Set write protect flag if device is opened as read-only. */
  2131. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  2132. tape->write_prot = 1;
  2133. else
  2134. tape->write_prot = tape->drv_write_prot;
  2135. /* Make sure drive isn't write protected if user wants to write. */
  2136. if (tape->write_prot) {
  2137. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  2138. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  2139. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  2140. retval = -EROFS;
  2141. goto out_put_tape;
  2142. }
  2143. }
  2144. /* Lock the tape drive door so user can't eject. */
  2145. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  2146. if (idetape_create_prevent_cmd(drive, &pc, 1)) {
  2147. if (!idetape_queue_pc_tail(drive, &pc)) {
  2148. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  2149. tape->door_locked = DOOR_LOCKED;
  2150. }
  2151. }
  2152. }
  2153. return 0;
  2154. out_put_tape:
  2155. ide_tape_put(tape);
  2156. return retval;
  2157. }
  2158. static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
  2159. {
  2160. idetape_tape_t *tape = drive->driver_data;
  2161. ide_tape_flush_merge_buffer(drive);
  2162. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
  2163. if (tape->merge_bh != NULL) {
  2164. idetape_pad_zeros(drive, tape->blk_size *
  2165. (tape->user_bs_factor - 1));
  2166. ide_tape_kfree_buffer(tape);
  2167. tape->merge_bh = NULL;
  2168. }
  2169. idetape_write_filemark(drive);
  2170. idetape_flush_tape_buffers(drive);
  2171. idetape_flush_tape_buffers(drive);
  2172. }
  2173. static int idetape_chrdev_release(struct inode *inode, struct file *filp)
  2174. {
  2175. struct ide_tape_obj *tape = ide_tape_f(filp);
  2176. ide_drive_t *drive = tape->drive;
  2177. struct ide_atapi_pc pc;
  2178. unsigned int minor = iminor(inode);
  2179. lock_kernel();
  2180. tape = drive->driver_data;
  2181. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  2182. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  2183. idetape_write_release(drive, minor);
  2184. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  2185. if (minor < 128)
  2186. ide_tape_discard_merge_buffer(drive, 1);
  2187. }
  2188. if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
  2189. (void) idetape_rewind_tape(drive);
  2190. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  2191. if (tape->door_locked == DOOR_LOCKED) {
  2192. if (idetape_create_prevent_cmd(drive, &pc, 0)) {
  2193. if (!idetape_queue_pc_tail(drive, &pc))
  2194. tape->door_locked = DOOR_UNLOCKED;
  2195. }
  2196. }
  2197. }
  2198. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  2199. ide_tape_put(tape);
  2200. unlock_kernel();
  2201. return 0;
  2202. }
  2203. /*
  2204. * check the contents of the ATAPI IDENTIFY command results. We return:
  2205. *
  2206. * 1 - If the tape can be supported by us, based on the information we have so
  2207. * far.
  2208. *
  2209. * 0 - If this tape driver is not currently supported by us.
  2210. */
  2211. static int idetape_identify_device(ide_drive_t *drive)
  2212. {
  2213. u8 gcw[2], protocol, device_type, removable, packet_size;
  2214. if (drive->id_read == 0)
  2215. return 1;
  2216. *((unsigned short *) &gcw) = drive->id->config;
  2217. protocol = (gcw[1] & 0xC0) >> 6;
  2218. device_type = gcw[1] & 0x1F;
  2219. removable = !!(gcw[0] & 0x80);
  2220. packet_size = gcw[0] & 0x3;
  2221. /* Check that we can support this device */
  2222. if (protocol != 2)
  2223. printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
  2224. protocol);
  2225. else if (device_type != 1)
  2226. printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
  2227. "to tape\n", device_type);
  2228. else if (!removable)
  2229. printk(KERN_ERR "ide-tape: The removable flag is not set\n");
  2230. else if (packet_size != 0) {
  2231. printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
  2232. " bytes\n", packet_size);
  2233. } else
  2234. return 1;
  2235. return 0;
  2236. }
  2237. static void idetape_get_inquiry_results(ide_drive_t *drive)
  2238. {
  2239. idetape_tape_t *tape = drive->driver_data;
  2240. struct ide_atapi_pc pc;
  2241. char fw_rev[6], vendor_id[10], product_id[18];
  2242. idetape_create_inquiry_cmd(&pc);
  2243. if (idetape_queue_pc_tail(drive, &pc)) {
  2244. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
  2245. tape->name);
  2246. return;
  2247. }
  2248. memcpy(vendor_id, &pc.buf[8], 8);
  2249. memcpy(product_id, &pc.buf[16], 16);
  2250. memcpy(fw_rev, &pc.buf[32], 4);
  2251. ide_fixstring(vendor_id, 10, 0);
  2252. ide_fixstring(product_id, 18, 0);
  2253. ide_fixstring(fw_rev, 6, 0);
  2254. printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
  2255. drive->name, tape->name, vendor_id, product_id, fw_rev);
  2256. }
  2257. /*
  2258. * Ask the tape about its various parameters. In particular, we will adjust our
  2259. * data transfer buffer size to the recommended value as returned by the tape.
  2260. */
  2261. static void idetape_get_mode_sense_results(ide_drive_t *drive)
  2262. {
  2263. idetape_tape_t *tape = drive->driver_data;
  2264. struct ide_atapi_pc pc;
  2265. u8 *caps;
  2266. u8 speed, max_speed;
  2267. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  2268. if (idetape_queue_pc_tail(drive, &pc)) {
  2269. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
  2270. " some default values\n");
  2271. tape->blk_size = 512;
  2272. put_unaligned(52, (u16 *)&tape->caps[12]);
  2273. put_unaligned(540, (u16 *)&tape->caps[14]);
  2274. put_unaligned(6*52, (u16 *)&tape->caps[16]);
  2275. return;
  2276. }
  2277. caps = pc.buf + 4 + pc.buf[3];
  2278. /* convert to host order and save for later use */
  2279. speed = be16_to_cpu(*(u16 *)&caps[14]);
  2280. max_speed = be16_to_cpu(*(u16 *)&caps[8]);
  2281. put_unaligned(max_speed, (u16 *)&caps[8]);
  2282. put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
  2283. put_unaligned(speed, (u16 *)&caps[14]);
  2284. put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
  2285. if (!speed) {
  2286. printk(KERN_INFO "ide-tape: %s: invalid tape speed "
  2287. "(assuming 650KB/sec)\n", drive->name);
  2288. put_unaligned(650, (u16 *)&caps[14]);
  2289. }
  2290. if (!max_speed) {
  2291. printk(KERN_INFO "ide-tape: %s: invalid max_speed "
  2292. "(assuming 650KB/sec)\n", drive->name);
  2293. put_unaligned(650, (u16 *)&caps[8]);
  2294. }
  2295. memcpy(&tape->caps, caps, 20);
  2296. if (caps[7] & 0x02)
  2297. tape->blk_size = 512;
  2298. else if (caps[7] & 0x04)
  2299. tape->blk_size = 1024;
  2300. }
  2301. #ifdef CONFIG_IDE_PROC_FS
  2302. static void idetape_add_settings(ide_drive_t *drive)
  2303. {
  2304. idetape_tape_t *tape = drive->driver_data;
  2305. ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
  2306. 1, 2, (u16 *)&tape->caps[16], NULL);
  2307. ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
  2308. 1, 1, (u16 *)&tape->caps[14], NULL);
  2309. ide_add_setting(drive, "buffer_size", SETTING_READ, TYPE_INT, 0, 0xffff,
  2310. 1, 1024, &tape->buffer_size, NULL);
  2311. ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
  2312. IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
  2313. NULL);
  2314. ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
  2315. 1, &drive->dsc_overlap, NULL);
  2316. ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
  2317. 1, 1, &tape->avg_speed, NULL);
  2318. ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
  2319. 1, &tape->debug_mask, NULL);
  2320. }
  2321. #else
  2322. static inline void idetape_add_settings(ide_drive_t *drive) { ; }
  2323. #endif
  2324. /*
  2325. * The function below is called to:
  2326. *
  2327. * 1. Initialize our various state variables.
  2328. * 2. Ask the tape for its capabilities.
  2329. * 3. Allocate a buffer which will be used for data transfer. The buffer size
  2330. * is chosen based on the recommendation which we received in step 2.
  2331. *
  2332. * Note that at this point ide.c already assigned us an irq, so that we can
  2333. * queue requests here and wait for their completion.
  2334. */
  2335. static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
  2336. {
  2337. unsigned long t;
  2338. int speed;
  2339. int buffer_size;
  2340. u8 gcw[2];
  2341. u16 *ctl = (u16 *)&tape->caps[12];
  2342. spin_lock_init(&tape->lock);
  2343. drive->dsc_overlap = 1;
  2344. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  2345. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  2346. tape->name);
  2347. drive->dsc_overlap = 0;
  2348. }
  2349. /* Seagate Travan drives do not support DSC overlap. */
  2350. if (strstr(drive->id->model, "Seagate STT3401"))
  2351. drive->dsc_overlap = 0;
  2352. tape->minor = minor;
  2353. tape->name[0] = 'h';
  2354. tape->name[1] = 't';
  2355. tape->name[2] = '0' + minor;
  2356. tape->chrdev_dir = IDETAPE_DIR_NONE;
  2357. tape->pc = tape->pc_stack;
  2358. *((unsigned short *) &gcw) = drive->id->config;
  2359. /* Command packet DRQ type */
  2360. if (((gcw[0] & 0x60) >> 5) == 1)
  2361. set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
  2362. idetape_get_inquiry_results(drive);
  2363. idetape_get_mode_sense_results(drive);
  2364. ide_tape_get_bsize_from_bdesc(drive);
  2365. tape->user_bs_factor = 1;
  2366. tape->buffer_size = *ctl * tape->blk_size;
  2367. while (tape->buffer_size > 0xffff) {
  2368. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  2369. *ctl /= 2;
  2370. tape->buffer_size = *ctl * tape->blk_size;
  2371. }
  2372. buffer_size = tape->buffer_size;
  2373. tape->pages_per_buffer = buffer_size / PAGE_SIZE;
  2374. if (buffer_size % PAGE_SIZE) {
  2375. tape->pages_per_buffer++;
  2376. tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
  2377. }
  2378. /* select the "best" DSC read/write polling freq */
  2379. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  2380. t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
  2381. /*
  2382. * Ensure that the number we got makes sense; limit it within
  2383. * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  2384. */
  2385. tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
  2386. IDETAPE_DSC_RW_MAX);
  2387. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  2388. "%lums tDSC%s\n",
  2389. drive->name, tape->name, *(u16 *)&tape->caps[14],
  2390. (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
  2391. tape->buffer_size / 1024,
  2392. tape->best_dsc_rw_freq * 1000 / HZ,
  2393. drive->using_dma ? ", DMA":"");
  2394. idetape_add_settings(drive);
  2395. }
  2396. static void ide_tape_remove(ide_drive_t *drive)
  2397. {
  2398. idetape_tape_t *tape = drive->driver_data;
  2399. ide_proc_unregister_driver(drive, tape->driver);
  2400. ide_unregister_region(tape->disk);
  2401. ide_tape_put(tape);
  2402. }
  2403. static void ide_tape_release(struct kref *kref)
  2404. {
  2405. struct ide_tape_obj *tape = to_ide_tape(kref);
  2406. ide_drive_t *drive = tape->drive;
  2407. struct gendisk *g = tape->disk;
  2408. BUG_ON(tape->merge_bh_size);
  2409. drive->dsc_overlap = 0;
  2410. drive->driver_data = NULL;
  2411. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  2412. device_destroy(idetape_sysfs_class,
  2413. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  2414. idetape_devs[tape->minor] = NULL;
  2415. g->private_data = NULL;
  2416. put_disk(g);
  2417. kfree(tape);
  2418. }
  2419. #ifdef CONFIG_IDE_PROC_FS
  2420. static int proc_idetape_read_name
  2421. (char *page, char **start, off_t off, int count, int *eof, void *data)
  2422. {
  2423. ide_drive_t *drive = (ide_drive_t *) data;
  2424. idetape_tape_t *tape = drive->driver_data;
  2425. char *out = page;
  2426. int len;
  2427. len = sprintf(out, "%s\n", tape->name);
  2428. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  2429. }
  2430. static ide_proc_entry_t idetape_proc[] = {
  2431. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  2432. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  2433. { NULL, 0, NULL, NULL }
  2434. };
  2435. #endif
  2436. static int ide_tape_probe(ide_drive_t *);
  2437. static ide_driver_t idetape_driver = {
  2438. .gen_driver = {
  2439. .owner = THIS_MODULE,
  2440. .name = "ide-tape",
  2441. .bus = &ide_bus_type,
  2442. },
  2443. .probe = ide_tape_probe,
  2444. .remove = ide_tape_remove,
  2445. .version = IDETAPE_VERSION,
  2446. .media = ide_tape,
  2447. .supports_dsc_overlap = 1,
  2448. .do_request = idetape_do_request,
  2449. .end_request = idetape_end_request,
  2450. .error = __ide_error,
  2451. .abort = __ide_abort,
  2452. #ifdef CONFIG_IDE_PROC_FS
  2453. .proc = idetape_proc,
  2454. #endif
  2455. };
  2456. /* Our character device supporting functions, passed to register_chrdev. */
  2457. static const struct file_operations idetape_fops = {
  2458. .owner = THIS_MODULE,
  2459. .read = idetape_chrdev_read,
  2460. .write = idetape_chrdev_write,
  2461. .ioctl = idetape_chrdev_ioctl,
  2462. .open = idetape_chrdev_open,
  2463. .release = idetape_chrdev_release,
  2464. };
  2465. static int idetape_open(struct inode *inode, struct file *filp)
  2466. {
  2467. struct gendisk *disk = inode->i_bdev->bd_disk;
  2468. struct ide_tape_obj *tape;
  2469. tape = ide_tape_get(disk);
  2470. if (!tape)
  2471. return -ENXIO;
  2472. return 0;
  2473. }
  2474. static int idetape_release(struct inode *inode, struct file *filp)
  2475. {
  2476. struct gendisk *disk = inode->i_bdev->bd_disk;
  2477. struct ide_tape_obj *tape = ide_tape_g(disk);
  2478. ide_tape_put(tape);
  2479. return 0;
  2480. }
  2481. static int idetape_ioctl(struct inode *inode, struct file *file,
  2482. unsigned int cmd, unsigned long arg)
  2483. {
  2484. struct block_device *bdev = inode->i_bdev;
  2485. struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
  2486. ide_drive_t *drive = tape->drive;
  2487. int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  2488. if (err == -EINVAL)
  2489. err = idetape_blkdev_ioctl(drive, cmd, arg);
  2490. return err;
  2491. }
  2492. static struct block_device_operations idetape_block_ops = {
  2493. .owner = THIS_MODULE,
  2494. .open = idetape_open,
  2495. .release = idetape_release,
  2496. .ioctl = idetape_ioctl,
  2497. };
  2498. static int ide_tape_probe(ide_drive_t *drive)
  2499. {
  2500. idetape_tape_t *tape;
  2501. struct gendisk *g;
  2502. int minor;
  2503. if (!strstr("ide-tape", drive->driver_req))
  2504. goto failed;
  2505. if (!drive->present)
  2506. goto failed;
  2507. if (drive->media != ide_tape)
  2508. goto failed;
  2509. if (!idetape_identify_device(drive)) {
  2510. printk(KERN_ERR "ide-tape: %s: not supported by this version of"
  2511. " the driver\n", drive->name);
  2512. goto failed;
  2513. }
  2514. if (drive->scsi) {
  2515. printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
  2516. " emulation.\n", drive->name);
  2517. goto failed;
  2518. }
  2519. tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
  2520. if (tape == NULL) {
  2521. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
  2522. drive->name);
  2523. goto failed;
  2524. }
  2525. g = alloc_disk(1 << PARTN_BITS);
  2526. if (!g)
  2527. goto out_free_tape;
  2528. ide_init_disk(g, drive);
  2529. ide_proc_register_driver(drive, &idetape_driver);
  2530. kref_init(&tape->kref);
  2531. tape->drive = drive;
  2532. tape->driver = &idetape_driver;
  2533. tape->disk = g;
  2534. g->private_data = &tape->driver;
  2535. drive->driver_data = tape;
  2536. mutex_lock(&idetape_ref_mutex);
  2537. for (minor = 0; idetape_devs[minor]; minor++)
  2538. ;
  2539. idetape_devs[minor] = tape;
  2540. mutex_unlock(&idetape_ref_mutex);
  2541. idetape_setup(drive, tape, minor);
  2542. device_create(idetape_sysfs_class, &drive->gendev,
  2543. MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
  2544. device_create(idetape_sysfs_class, &drive->gendev,
  2545. MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
  2546. g->fops = &idetape_block_ops;
  2547. ide_register_region(g);
  2548. return 0;
  2549. out_free_tape:
  2550. kfree(tape);
  2551. failed:
  2552. return -ENODEV;
  2553. }
  2554. static void __exit idetape_exit(void)
  2555. {
  2556. driver_unregister(&idetape_driver.gen_driver);
  2557. class_destroy(idetape_sysfs_class);
  2558. unregister_chrdev(IDETAPE_MAJOR, "ht");
  2559. }
  2560. static int __init idetape_init(void)
  2561. {
  2562. int error = 1;
  2563. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  2564. if (IS_ERR(idetape_sysfs_class)) {
  2565. idetape_sysfs_class = NULL;
  2566. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  2567. error = -EBUSY;
  2568. goto out;
  2569. }
  2570. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  2571. printk(KERN_ERR "ide-tape: Failed to register chrdev"
  2572. " interface\n");
  2573. error = -EBUSY;
  2574. goto out_free_class;
  2575. }
  2576. error = driver_register(&idetape_driver.gen_driver);
  2577. if (error)
  2578. goto out_free_driver;
  2579. return 0;
  2580. out_free_driver:
  2581. driver_unregister(&idetape_driver.gen_driver);
  2582. out_free_class:
  2583. class_destroy(idetape_sysfs_class);
  2584. out:
  2585. return error;
  2586. }
  2587. MODULE_ALIAS("ide:*m-tape*");
  2588. module_init(idetape_init);
  2589. module_exit(idetape_exit);
  2590. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
  2591. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  2592. MODULE_LICENSE("GPL");