ide-tape.c 84 KB

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