ide-tape.c 83 KB

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