ide-tape.c 83 KB

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