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. int merge_stage_size;
  278. struct idetape_bh *bh;
  279. char *b_data;
  280. int b_count;
  281. int pages_per_buffer;
  282. /* Wasted space in each stage */
  283. int excess_bh_size;
  284. /* Status/Action flags: long for set_bit */
  285. unsigned long flags;
  286. /* protects the ide-tape queue */
  287. spinlock_t lock;
  288. /* Measures average tape speed */
  289. unsigned long avg_time;
  290. int avg_size;
  291. int avg_speed;
  292. /* the door is currently locked */
  293. int door_locked;
  294. /* the tape hardware is write protected */
  295. char drv_write_prot;
  296. /* the tape is write protected (hardware or opened as read-only) */
  297. char write_prot;
  298. u32 debug_mask;
  299. } idetape_tape_t;
  300. static DEFINE_MUTEX(idetape_ref_mutex);
  301. static struct class *idetape_sysfs_class;
  302. #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
  303. #define ide_tape_g(disk) \
  304. container_of((disk)->private_data, struct ide_tape_obj, driver)
  305. static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
  306. {
  307. struct ide_tape_obj *tape = NULL;
  308. mutex_lock(&idetape_ref_mutex);
  309. tape = ide_tape_g(disk);
  310. if (tape)
  311. kref_get(&tape->kref);
  312. mutex_unlock(&idetape_ref_mutex);
  313. return tape;
  314. }
  315. static void ide_tape_release(struct kref *);
  316. static void ide_tape_put(struct ide_tape_obj *tape)
  317. {
  318. mutex_lock(&idetape_ref_mutex);
  319. kref_put(&tape->kref, ide_tape_release);
  320. mutex_unlock(&idetape_ref_mutex);
  321. }
  322. /*
  323. * The variables below are used for the character device interface. Additional
  324. * state variables are defined in our ide_drive_t structure.
  325. */
  326. static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
  327. #define ide_tape_f(file) ((file)->private_data)
  328. static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
  329. {
  330. struct ide_tape_obj *tape = NULL;
  331. mutex_lock(&idetape_ref_mutex);
  332. tape = idetape_devs[i];
  333. if (tape)
  334. kref_get(&tape->kref);
  335. mutex_unlock(&idetape_ref_mutex);
  336. return tape;
  337. }
  338. static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  339. unsigned int bcount)
  340. {
  341. struct idetape_bh *bh = pc->bh;
  342. int count;
  343. while (bcount) {
  344. if (bh == NULL) {
  345. printk(KERN_ERR "ide-tape: bh == NULL in "
  346. "idetape_input_buffers\n");
  347. ide_atapi_discard_data(drive, bcount);
  348. return;
  349. }
  350. count = min(
  351. (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
  352. bcount);
  353. HWIF(drive)->atapi_input_bytes(drive, bh->b_data +
  354. atomic_read(&bh->b_count), count);
  355. bcount -= count;
  356. atomic_add(count, &bh->b_count);
  357. if (atomic_read(&bh->b_count) == bh->b_size) {
  358. bh = bh->b_reqnext;
  359. if (bh)
  360. atomic_set(&bh->b_count, 0);
  361. }
  362. }
  363. pc->bh = bh;
  364. }
  365. static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  366. unsigned int bcount)
  367. {
  368. struct idetape_bh *bh = pc->bh;
  369. int count;
  370. while (bcount) {
  371. if (bh == NULL) {
  372. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  373. __func__);
  374. return;
  375. }
  376. count = min((unsigned int)pc->b_count, (unsigned int)bcount);
  377. HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
  378. bcount -= count;
  379. pc->b_data += count;
  380. pc->b_count -= count;
  381. if (!pc->b_count) {
  382. bh = bh->b_reqnext;
  383. pc->bh = bh;
  384. if (bh) {
  385. pc->b_data = bh->b_data;
  386. pc->b_count = atomic_read(&bh->b_count);
  387. }
  388. }
  389. }
  390. }
  391. static void idetape_update_buffers(struct ide_atapi_pc *pc)
  392. {
  393. struct idetape_bh *bh = pc->bh;
  394. int count;
  395. unsigned int bcount = pc->xferred;
  396. if (pc->flags & PC_FLAG_WRITING)
  397. return;
  398. while (bcount) {
  399. if (bh == NULL) {
  400. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  401. __func__);
  402. return;
  403. }
  404. count = min((unsigned int)bh->b_size, (unsigned int)bcount);
  405. atomic_set(&bh->b_count, count);
  406. if (atomic_read(&bh->b_count) == bh->b_size)
  407. bh = bh->b_reqnext;
  408. bcount -= count;
  409. }
  410. pc->bh = bh;
  411. }
  412. /*
  413. * idetape_next_pc_storage returns a pointer to a place in which we can
  414. * safely store a packet command, even though we intend to leave the
  415. * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
  416. * commands is allocated at initialization time.
  417. */
  418. static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive)
  419. {
  420. idetape_tape_t *tape = drive->driver_data;
  421. debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index);
  422. if (tape->pc_stack_index == IDETAPE_PC_STACK)
  423. tape->pc_stack_index = 0;
  424. return (&tape->pc_stack[tape->pc_stack_index++]);
  425. }
  426. /*
  427. * idetape_next_rq_storage is used along with idetape_next_pc_storage.
  428. * Since we queue packet commands in the request queue, we need to
  429. * allocate a request, along with the allocation of a packet command.
  430. */
  431. /**************************************************************
  432. * *
  433. * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
  434. * followed later on by kfree(). -ml *
  435. * *
  436. **************************************************************/
  437. static struct request *idetape_next_rq_storage(ide_drive_t *drive)
  438. {
  439. idetape_tape_t *tape = drive->driver_data;
  440. debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index);
  441. if (tape->rq_stack_index == IDETAPE_PC_STACK)
  442. tape->rq_stack_index = 0;
  443. return (&tape->rq_stack[tape->rq_stack_index++]);
  444. }
  445. static void idetape_init_pc(struct ide_atapi_pc *pc)
  446. {
  447. memset(pc->c, 0, 12);
  448. pc->retries = 0;
  449. pc->flags = 0;
  450. pc->req_xfer = 0;
  451. pc->buf = pc->pc_buf;
  452. pc->buf_size = IDETAPE_PC_BUFFER_SIZE;
  453. pc->bh = NULL;
  454. pc->b_data = NULL;
  455. }
  456. /*
  457. * called on each failed packet command retry to analyze the request sense. We
  458. * currently do not utilize this information.
  459. */
  460. static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
  461. {
  462. idetape_tape_t *tape = drive->driver_data;
  463. struct ide_atapi_pc *pc = tape->failed_pc;
  464. tape->sense_key = sense[2] & 0xF;
  465. tape->asc = sense[12];
  466. tape->ascq = sense[13];
  467. debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n",
  468. pc->c[0], tape->sense_key, tape->asc, tape->ascq);
  469. /* Correct pc->xferred by asking the tape. */
  470. if (pc->flags & PC_FLAG_DMA_ERROR) {
  471. pc->xferred = pc->req_xfer -
  472. tape->blk_size *
  473. be32_to_cpu(get_unaligned((u32 *)&sense[3]));
  474. idetape_update_buffers(pc);
  475. }
  476. /*
  477. * If error was the result of a zero-length read or write command,
  478. * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
  479. * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
  480. */
  481. if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
  482. /* length == 0 */
  483. && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
  484. if (tape->sense_key == 5) {
  485. /* don't report an error, everything's ok */
  486. pc->error = 0;
  487. /* don't retry read/write */
  488. pc->flags |= PC_FLAG_ABORT;
  489. }
  490. }
  491. if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
  492. pc->error = IDETAPE_ERROR_FILEMARK;
  493. pc->flags |= PC_FLAG_ABORT;
  494. }
  495. if (pc->c[0] == WRITE_6) {
  496. if ((sense[2] & 0x40) || (tape->sense_key == 0xd
  497. && tape->asc == 0x0 && tape->ascq == 0x2)) {
  498. pc->error = IDETAPE_ERROR_EOD;
  499. pc->flags |= PC_FLAG_ABORT;
  500. }
  501. }
  502. if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
  503. if (tape->sense_key == 8) {
  504. pc->error = IDETAPE_ERROR_EOD;
  505. pc->flags |= PC_FLAG_ABORT;
  506. }
  507. if (!(pc->flags & PC_FLAG_ABORT) &&
  508. pc->xferred)
  509. pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
  510. }
  511. }
  512. /* Free a stage along with its related buffers completely. */
  513. static void __idetape_kfree_stage(idetape_stage_t *stage)
  514. {
  515. struct idetape_bh *prev_bh, *bh = stage->bh;
  516. int size;
  517. while (bh != NULL) {
  518. if (bh->b_data != NULL) {
  519. size = (int) bh->b_size;
  520. while (size > 0) {
  521. free_page((unsigned long) bh->b_data);
  522. size -= PAGE_SIZE;
  523. bh->b_data += PAGE_SIZE;
  524. }
  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. __idetape_kfree_stage(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 int __idetape_discard_read_pipeline(ide_drive_t *drive)
  1457. {
  1458. idetape_tape_t *tape = drive->driver_data;
  1459. if (tape->chrdev_dir != IDETAPE_DIR_READ)
  1460. return 0;
  1461. clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
  1462. tape->merge_stage_size = 0;
  1463. if (tape->merge_stage != NULL) {
  1464. __idetape_kfree_stage(tape->merge_stage);
  1465. tape->merge_stage = NULL;
  1466. }
  1467. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1468. return 0;
  1469. }
  1470. /*
  1471. * Position the tape to the requested block using the LOCATE packet command.
  1472. * A READ POSITION command is then issued to check where we are positioned. Like
  1473. * all higher level operations, we queue the commands at the tail of the request
  1474. * queue and wait for their completion.
  1475. */
  1476. static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
  1477. u8 partition, int skip)
  1478. {
  1479. idetape_tape_t *tape = drive->driver_data;
  1480. int retval;
  1481. struct ide_atapi_pc pc;
  1482. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1483. __idetape_discard_read_pipeline(drive);
  1484. idetape_wait_ready(drive, 60 * 5 * HZ);
  1485. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  1486. retval = idetape_queue_pc_tail(drive, &pc);
  1487. if (retval)
  1488. return (retval);
  1489. idetape_create_read_position_cmd(&pc);
  1490. return (idetape_queue_pc_tail(drive, &pc));
  1491. }
  1492. static void idetape_discard_read_pipeline(ide_drive_t *drive,
  1493. int restore_position)
  1494. {
  1495. idetape_tape_t *tape = drive->driver_data;
  1496. int cnt;
  1497. int seek, position;
  1498. cnt = __idetape_discard_read_pipeline(drive);
  1499. if (restore_position) {
  1500. position = idetape_read_position(drive);
  1501. seek = position > cnt ? position - cnt : 0;
  1502. if (idetape_position_tape(drive, seek, 0, 0)) {
  1503. printk(KERN_INFO "ide-tape: %s: position_tape failed in"
  1504. " discard_pipeline()\n", tape->name);
  1505. return;
  1506. }
  1507. }
  1508. }
  1509. /*
  1510. * Generate a read/write request for the block device interface and wait for it
  1511. * to be serviced.
  1512. */
  1513. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
  1514. struct idetape_bh *bh)
  1515. {
  1516. idetape_tape_t *tape = drive->driver_data;
  1517. struct request rq;
  1518. debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
  1519. idetape_init_rq(&rq, cmd);
  1520. rq.rq_disk = tape->disk;
  1521. rq.special = (void *)bh;
  1522. rq.sector = tape->first_frame;
  1523. rq.nr_sectors = blocks;
  1524. rq.current_nr_sectors = blocks;
  1525. (void) ide_do_drive_cmd(drive, &rq, ide_wait);
  1526. if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
  1527. return 0;
  1528. if (tape->merge_stage)
  1529. idetape_init_merge_stage(tape);
  1530. if (rq.errors == IDETAPE_ERROR_GENERAL)
  1531. return -EIO;
  1532. return (tape->blk_size * (blocks-rq.current_nr_sectors));
  1533. }
  1534. static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
  1535. {
  1536. idetape_init_pc(pc);
  1537. pc->c[0] = INQUIRY;
  1538. pc->c[4] = 254;
  1539. pc->req_xfer = 254;
  1540. pc->idetape_callback = &idetape_pc_callback;
  1541. }
  1542. static void idetape_create_rewind_cmd(ide_drive_t *drive,
  1543. struct ide_atapi_pc *pc)
  1544. {
  1545. idetape_init_pc(pc);
  1546. pc->c[0] = REZERO_UNIT;
  1547. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1548. pc->idetape_callback = &idetape_pc_callback;
  1549. }
  1550. static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
  1551. {
  1552. idetape_init_pc(pc);
  1553. pc->c[0] = ERASE;
  1554. pc->c[1] = 1;
  1555. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1556. pc->idetape_callback = &idetape_pc_callback;
  1557. }
  1558. static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
  1559. {
  1560. idetape_init_pc(pc);
  1561. pc->c[0] = SPACE;
  1562. put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
  1563. pc->c[1] = cmd;
  1564. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1565. pc->idetape_callback = &idetape_pc_callback;
  1566. }
  1567. /* Queue up a character device originated write request. */
  1568. static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
  1569. {
  1570. idetape_tape_t *tape = drive->driver_data;
  1571. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1572. return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
  1573. blocks, tape->merge_stage->bh);
  1574. }
  1575. static void idetape_empty_write_pipeline(ide_drive_t *drive)
  1576. {
  1577. idetape_tape_t *tape = drive->driver_data;
  1578. int blocks, min;
  1579. struct idetape_bh *bh;
  1580. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1581. printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline,"
  1582. " but we are not writing.\n");
  1583. return;
  1584. }
  1585. if (tape->merge_stage_size > tape->buffer_size) {
  1586. printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
  1587. tape->merge_stage_size = tape->buffer_size;
  1588. }
  1589. if (tape->merge_stage_size) {
  1590. blocks = tape->merge_stage_size / tape->blk_size;
  1591. if (tape->merge_stage_size % tape->blk_size) {
  1592. unsigned int i;
  1593. blocks++;
  1594. i = tape->blk_size - tape->merge_stage_size %
  1595. tape->blk_size;
  1596. bh = tape->bh->b_reqnext;
  1597. while (bh) {
  1598. atomic_set(&bh->b_count, 0);
  1599. bh = bh->b_reqnext;
  1600. }
  1601. bh = tape->bh;
  1602. while (i) {
  1603. if (bh == NULL) {
  1604. printk(KERN_INFO "ide-tape: bug,"
  1605. " bh NULL\n");
  1606. break;
  1607. }
  1608. min = min(i, (unsigned int)(bh->b_size -
  1609. atomic_read(&bh->b_count)));
  1610. memset(bh->b_data + atomic_read(&bh->b_count),
  1611. 0, min);
  1612. atomic_add(min, &bh->b_count);
  1613. i -= min;
  1614. bh = bh->b_reqnext;
  1615. }
  1616. }
  1617. (void) idetape_add_chrdev_write_request(drive, blocks);
  1618. tape->merge_stage_size = 0;
  1619. }
  1620. if (tape->merge_stage != NULL) {
  1621. __idetape_kfree_stage(tape->merge_stage);
  1622. tape->merge_stage = NULL;
  1623. }
  1624. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1625. }
  1626. static int idetape_init_read(ide_drive_t *drive)
  1627. {
  1628. idetape_tape_t *tape = drive->driver_data;
  1629. int bytes_read;
  1630. /* Initialize read operation */
  1631. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1632. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1633. idetape_empty_write_pipeline(drive);
  1634. idetape_flush_tape_buffers(drive);
  1635. }
  1636. if (tape->merge_stage || tape->merge_stage_size) {
  1637. printk(KERN_ERR "ide-tape: merge_stage_size should be"
  1638. " 0 now\n");
  1639. tape->merge_stage_size = 0;
  1640. }
  1641. tape->merge_stage = ide_tape_kmalloc_buffer(tape, 0, 0);
  1642. if (!tape->merge_stage)
  1643. return -ENOMEM;
  1644. tape->chrdev_dir = IDETAPE_DIR_READ;
  1645. /*
  1646. * Issue a read 0 command to ensure that DSC handshake is
  1647. * switched from completion mode to buffer available mode.
  1648. * No point in issuing this if DSC overlap isn't supported, some
  1649. * drives (Seagate STT3401A) will return an error.
  1650. */
  1651. if (drive->dsc_overlap) {
  1652. bytes_read = idetape_queue_rw_tail(drive,
  1653. REQ_IDETAPE_READ, 0,
  1654. tape->merge_stage->bh);
  1655. if (bytes_read < 0) {
  1656. __idetape_kfree_stage(tape->merge_stage);
  1657. tape->merge_stage = NULL;
  1658. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1659. return bytes_read;
  1660. }
  1661. }
  1662. }
  1663. return 0;
  1664. }
  1665. /* called from idetape_chrdev_read() to service a chrdev read request. */
  1666. static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
  1667. {
  1668. idetape_tape_t *tape = drive->driver_data;
  1669. debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
  1670. /* If we are at a filemark, return a read length of 0 */
  1671. if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
  1672. return 0;
  1673. idetape_init_read(drive);
  1674. return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
  1675. tape->merge_stage->bh);
  1676. }
  1677. static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
  1678. {
  1679. idetape_tape_t *tape = drive->driver_data;
  1680. struct idetape_bh *bh;
  1681. int blocks;
  1682. while (bcount) {
  1683. unsigned int count;
  1684. bh = tape->merge_stage->bh;
  1685. count = min(tape->buffer_size, bcount);
  1686. bcount -= count;
  1687. blocks = count / tape->blk_size;
  1688. while (count) {
  1689. atomic_set(&bh->b_count,
  1690. min(count, (unsigned int)bh->b_size));
  1691. memset(bh->b_data, 0, atomic_read(&bh->b_count));
  1692. count -= atomic_read(&bh->b_count);
  1693. bh = bh->b_reqnext;
  1694. }
  1695. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
  1696. tape->merge_stage->bh);
  1697. }
  1698. }
  1699. /*
  1700. * Rewinds the tape to the Beginning Of the current Partition (BOP). We
  1701. * currently support only one partition.
  1702. */
  1703. static int idetape_rewind_tape(ide_drive_t *drive)
  1704. {
  1705. int retval;
  1706. struct ide_atapi_pc pc;
  1707. idetape_tape_t *tape;
  1708. tape = drive->driver_data;
  1709. debug_log(DBG_SENSE, "Enter %s\n", __func__);
  1710. idetape_create_rewind_cmd(drive, &pc);
  1711. retval = idetape_queue_pc_tail(drive, &pc);
  1712. if (retval)
  1713. return retval;
  1714. idetape_create_read_position_cmd(&pc);
  1715. retval = idetape_queue_pc_tail(drive, &pc);
  1716. if (retval)
  1717. return retval;
  1718. return 0;
  1719. }
  1720. /* mtio.h compatible commands should be issued to the chrdev interface. */
  1721. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
  1722. unsigned long arg)
  1723. {
  1724. idetape_tape_t *tape = drive->driver_data;
  1725. void __user *argp = (void __user *)arg;
  1726. struct idetape_config {
  1727. int dsc_rw_frequency;
  1728. int dsc_media_access_frequency;
  1729. int nr_stages;
  1730. } config;
  1731. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1732. switch (cmd) {
  1733. case 0x0340:
  1734. if (copy_from_user(&config, argp, sizeof(config)))
  1735. return -EFAULT;
  1736. tape->best_dsc_rw_freq = config.dsc_rw_frequency;
  1737. break;
  1738. case 0x0350:
  1739. config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
  1740. config.nr_stages = 1;
  1741. if (copy_to_user(argp, &config, sizeof(config)))
  1742. return -EFAULT;
  1743. break;
  1744. default:
  1745. return -EIO;
  1746. }
  1747. return 0;
  1748. }
  1749. static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
  1750. int mt_count)
  1751. {
  1752. idetape_tape_t *tape = drive->driver_data;
  1753. struct ide_atapi_pc pc;
  1754. int retval, count = 0;
  1755. int sprev = !!(tape->caps[4] & 0x20);
  1756. if (mt_count == 0)
  1757. return 0;
  1758. if (MTBSF == mt_op || MTBSFM == mt_op) {
  1759. if (!sprev)
  1760. return -EIO;
  1761. mt_count = -mt_count;
  1762. }
  1763. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1764. tape->merge_stage_size = 0;
  1765. if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
  1766. ++count;
  1767. idetape_discard_read_pipeline(drive, 0);
  1768. }
  1769. /*
  1770. * The filemark was not found in our internal pipeline; now we can issue
  1771. * the space command.
  1772. */
  1773. switch (mt_op) {
  1774. case MTFSF:
  1775. case MTBSF:
  1776. idetape_create_space_cmd(&pc, mt_count - count,
  1777. IDETAPE_SPACE_OVER_FILEMARK);
  1778. return idetape_queue_pc_tail(drive, &pc);
  1779. case MTFSFM:
  1780. case MTBSFM:
  1781. if (!sprev)
  1782. return -EIO;
  1783. retval = idetape_space_over_filemarks(drive, MTFSF,
  1784. mt_count - count);
  1785. if (retval)
  1786. return retval;
  1787. count = (MTBSFM == mt_op ? 1 : -1);
  1788. return idetape_space_over_filemarks(drive, MTFSF, count);
  1789. default:
  1790. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1791. mt_op);
  1792. return -EIO;
  1793. }
  1794. }
  1795. /*
  1796. * Our character device read / write functions.
  1797. *
  1798. * The tape is optimized to maximize throughput when it is transferring an
  1799. * integral number of the "continuous transfer limit", which is a parameter of
  1800. * the specific tape (26kB on my particular tape, 32kB for Onstream).
  1801. *
  1802. * As of version 1.3 of the driver, the character device provides an abstract
  1803. * continuous view of the media - any mix of block sizes (even 1 byte) on the
  1804. * same backup/restore procedure is supported. The driver will internally
  1805. * convert the requests to the recommended transfer unit, so that an unmatch
  1806. * between the user's block size to the recommended size will only result in a
  1807. * (slightly) increased driver overhead, but will no longer hit performance.
  1808. * This is not applicable to Onstream.
  1809. */
  1810. static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
  1811. size_t count, loff_t *ppos)
  1812. {
  1813. struct ide_tape_obj *tape = ide_tape_f(file);
  1814. ide_drive_t *drive = tape->drive;
  1815. ssize_t bytes_read, temp, actually_read = 0, rc;
  1816. ssize_t ret = 0;
  1817. u16 ctl = *(u16 *)&tape->caps[12];
  1818. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1819. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1820. if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
  1821. if (count > tape->blk_size &&
  1822. (count % tape->blk_size) == 0)
  1823. tape->user_bs_factor = count / tape->blk_size;
  1824. }
  1825. rc = idetape_init_read(drive);
  1826. if (rc < 0)
  1827. return rc;
  1828. if (count == 0)
  1829. return (0);
  1830. if (tape->merge_stage_size) {
  1831. actually_read = min((unsigned int)(tape->merge_stage_size),
  1832. (unsigned int)count);
  1833. if (idetape_copy_stage_to_user(tape, buf, actually_read))
  1834. ret = -EFAULT;
  1835. buf += actually_read;
  1836. tape->merge_stage_size -= actually_read;
  1837. count -= actually_read;
  1838. }
  1839. while (count >= tape->buffer_size) {
  1840. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1841. if (bytes_read <= 0)
  1842. goto finish;
  1843. if (idetape_copy_stage_to_user(tape, buf, bytes_read))
  1844. ret = -EFAULT;
  1845. buf += bytes_read;
  1846. count -= bytes_read;
  1847. actually_read += bytes_read;
  1848. }
  1849. if (count) {
  1850. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1851. if (bytes_read <= 0)
  1852. goto finish;
  1853. temp = min((unsigned long)count, (unsigned long)bytes_read);
  1854. if (idetape_copy_stage_to_user(tape, buf, temp))
  1855. ret = -EFAULT;
  1856. actually_read += temp;
  1857. tape->merge_stage_size = bytes_read-temp;
  1858. }
  1859. finish:
  1860. if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
  1861. debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
  1862. idetape_space_over_filemarks(drive, MTFSF, 1);
  1863. return 0;
  1864. }
  1865. return ret ? ret : actually_read;
  1866. }
  1867. static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
  1868. size_t count, loff_t *ppos)
  1869. {
  1870. struct ide_tape_obj *tape = ide_tape_f(file);
  1871. ide_drive_t *drive = tape->drive;
  1872. ssize_t actually_written = 0;
  1873. ssize_t ret = 0;
  1874. u16 ctl = *(u16 *)&tape->caps[12];
  1875. /* The drive is write protected. */
  1876. if (tape->write_prot)
  1877. return -EACCES;
  1878. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1879. /* Initialize write operation */
  1880. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1881. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1882. idetape_discard_read_pipeline(drive, 1);
  1883. if (tape->merge_stage || tape->merge_stage_size) {
  1884. printk(KERN_ERR "ide-tape: merge_stage_size "
  1885. "should be 0 now\n");
  1886. tape->merge_stage_size = 0;
  1887. }
  1888. tape->merge_stage = ide_tape_kmalloc_buffer(tape, 0, 0);
  1889. if (!tape->merge_stage)
  1890. return -ENOMEM;
  1891. tape->chrdev_dir = IDETAPE_DIR_WRITE;
  1892. idetape_init_merge_stage(tape);
  1893. /*
  1894. * Issue a write 0 command to ensure that DSC handshake is
  1895. * switched from completion mode to buffer available mode. No
  1896. * point in issuing this if DSC overlap isn't supported, some
  1897. * drives (Seagate STT3401A) will return an error.
  1898. */
  1899. if (drive->dsc_overlap) {
  1900. ssize_t retval = idetape_queue_rw_tail(drive,
  1901. REQ_IDETAPE_WRITE, 0,
  1902. tape->merge_stage->bh);
  1903. if (retval < 0) {
  1904. __idetape_kfree_stage(tape->merge_stage);
  1905. tape->merge_stage = NULL;
  1906. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1907. return retval;
  1908. }
  1909. }
  1910. }
  1911. if (count == 0)
  1912. return (0);
  1913. if (tape->merge_stage_size) {
  1914. if (tape->merge_stage_size >= tape->buffer_size) {
  1915. printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
  1916. tape->merge_stage_size = 0;
  1917. }
  1918. actually_written = min((unsigned int)
  1919. (tape->buffer_size - tape->merge_stage_size),
  1920. (unsigned int)count);
  1921. if (idetape_copy_stage_from_user(tape, buf, actually_written))
  1922. ret = -EFAULT;
  1923. buf += actually_written;
  1924. tape->merge_stage_size += actually_written;
  1925. count -= actually_written;
  1926. if (tape->merge_stage_size == tape->buffer_size) {
  1927. ssize_t retval;
  1928. tape->merge_stage_size = 0;
  1929. retval = idetape_add_chrdev_write_request(drive, ctl);
  1930. if (retval <= 0)
  1931. return (retval);
  1932. }
  1933. }
  1934. while (count >= tape->buffer_size) {
  1935. ssize_t retval;
  1936. if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
  1937. ret = -EFAULT;
  1938. buf += tape->buffer_size;
  1939. count -= tape->buffer_size;
  1940. retval = idetape_add_chrdev_write_request(drive, ctl);
  1941. actually_written += tape->buffer_size;
  1942. if (retval <= 0)
  1943. return (retval);
  1944. }
  1945. if (count) {
  1946. actually_written += count;
  1947. if (idetape_copy_stage_from_user(tape, buf, count))
  1948. ret = -EFAULT;
  1949. tape->merge_stage_size += count;
  1950. }
  1951. return ret ? ret : actually_written;
  1952. }
  1953. static int idetape_write_filemark(ide_drive_t *drive)
  1954. {
  1955. struct ide_atapi_pc pc;
  1956. /* Write a filemark */
  1957. idetape_create_write_filemark_cmd(drive, &pc, 1);
  1958. if (idetape_queue_pc_tail(drive, &pc)) {
  1959. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  1960. return -EIO;
  1961. }
  1962. return 0;
  1963. }
  1964. /*
  1965. * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
  1966. * requested.
  1967. *
  1968. * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
  1969. * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
  1970. * usually not supported.
  1971. *
  1972. * The following commands are currently not supported:
  1973. *
  1974. * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
  1975. * MT_ST_WRITE_THRESHOLD.
  1976. */
  1977. static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
  1978. {
  1979. idetape_tape_t *tape = drive->driver_data;
  1980. struct ide_atapi_pc pc;
  1981. int i, retval;
  1982. debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
  1983. mt_op, mt_count);
  1984. switch (mt_op) {
  1985. case MTFSF:
  1986. case MTFSFM:
  1987. case MTBSF:
  1988. case MTBSFM:
  1989. if (!mt_count)
  1990. return 0;
  1991. return idetape_space_over_filemarks(drive, mt_op, mt_count);
  1992. default:
  1993. break;
  1994. }
  1995. switch (mt_op) {
  1996. case MTWEOF:
  1997. if (tape->write_prot)
  1998. return -EACCES;
  1999. idetape_discard_read_pipeline(drive, 1);
  2000. for (i = 0; i < mt_count; i++) {
  2001. retval = idetape_write_filemark(drive);
  2002. if (retval)
  2003. return retval;
  2004. }
  2005. return 0;
  2006. case MTREW:
  2007. idetape_discard_read_pipeline(drive, 0);
  2008. if (idetape_rewind_tape(drive))
  2009. return -EIO;
  2010. return 0;
  2011. case MTLOAD:
  2012. idetape_discard_read_pipeline(drive, 0);
  2013. idetape_create_load_unload_cmd(drive, &pc,
  2014. IDETAPE_LU_LOAD_MASK);
  2015. return idetape_queue_pc_tail(drive, &pc);
  2016. case MTUNLOAD:
  2017. case MTOFFL:
  2018. /*
  2019. * If door is locked, attempt to unlock before
  2020. * attempting to eject.
  2021. */
  2022. if (tape->door_locked) {
  2023. if (idetape_create_prevent_cmd(drive, &pc, 0))
  2024. if (!idetape_queue_pc_tail(drive, &pc))
  2025. tape->door_locked = DOOR_UNLOCKED;
  2026. }
  2027. idetape_discard_read_pipeline(drive, 0);
  2028. idetape_create_load_unload_cmd(drive, &pc,
  2029. !IDETAPE_LU_LOAD_MASK);
  2030. retval = idetape_queue_pc_tail(drive, &pc);
  2031. if (!retval)
  2032. clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
  2033. return retval;
  2034. case MTNOP:
  2035. idetape_discard_read_pipeline(drive, 0);
  2036. return idetape_flush_tape_buffers(drive);
  2037. case MTRETEN:
  2038. idetape_discard_read_pipeline(drive, 0);
  2039. idetape_create_load_unload_cmd(drive, &pc,
  2040. IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  2041. return idetape_queue_pc_tail(drive, &pc);
  2042. case MTEOM:
  2043. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  2044. return idetape_queue_pc_tail(drive, &pc);
  2045. case MTERASE:
  2046. (void)idetape_rewind_tape(drive);
  2047. idetape_create_erase_cmd(&pc);
  2048. return idetape_queue_pc_tail(drive, &pc);
  2049. case MTSETBLK:
  2050. if (mt_count) {
  2051. if (mt_count < tape->blk_size ||
  2052. mt_count % tape->blk_size)
  2053. return -EIO;
  2054. tape->user_bs_factor = mt_count / tape->blk_size;
  2055. clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
  2056. } else
  2057. set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
  2058. return 0;
  2059. case MTSEEK:
  2060. idetape_discard_read_pipeline(drive, 0);
  2061. return idetape_position_tape(drive,
  2062. mt_count * tape->user_bs_factor, tape->partition, 0);
  2063. case MTSETPART:
  2064. idetape_discard_read_pipeline(drive, 0);
  2065. return idetape_position_tape(drive, 0, mt_count, 0);
  2066. case MTFSR:
  2067. case MTBSR:
  2068. case MTLOCK:
  2069. if (!idetape_create_prevent_cmd(drive, &pc, 1))
  2070. return 0;
  2071. retval = idetape_queue_pc_tail(drive, &pc);
  2072. if (retval)
  2073. return retval;
  2074. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  2075. return 0;
  2076. case MTUNLOCK:
  2077. if (!idetape_create_prevent_cmd(drive, &pc, 0))
  2078. return 0;
  2079. retval = idetape_queue_pc_tail(drive, &pc);
  2080. if (retval)
  2081. return retval;
  2082. tape->door_locked = DOOR_UNLOCKED;
  2083. return 0;
  2084. default:
  2085. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  2086. mt_op);
  2087. return -EIO;
  2088. }
  2089. }
  2090. /*
  2091. * Our character device ioctls. General mtio.h magnetic io commands are
  2092. * supported here, and not in the corresponding block interface. Our own
  2093. * ide-tape ioctls are supported on both interfaces.
  2094. */
  2095. static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
  2096. unsigned int cmd, unsigned long arg)
  2097. {
  2098. struct ide_tape_obj *tape = ide_tape_f(file);
  2099. ide_drive_t *drive = tape->drive;
  2100. struct mtop mtop;
  2101. struct mtget mtget;
  2102. struct mtpos mtpos;
  2103. int block_offset = 0, position = tape->first_frame;
  2104. void __user *argp = (void __user *)arg;
  2105. debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
  2106. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  2107. idetape_empty_write_pipeline(drive);
  2108. idetape_flush_tape_buffers(drive);
  2109. }
  2110. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  2111. block_offset = tape->merge_stage_size /
  2112. (tape->blk_size * tape->user_bs_factor);
  2113. position = idetape_read_position(drive);
  2114. if (position < 0)
  2115. return -EIO;
  2116. }
  2117. switch (cmd) {
  2118. case MTIOCTOP:
  2119. if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
  2120. return -EFAULT;
  2121. return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
  2122. case MTIOCGET:
  2123. memset(&mtget, 0, sizeof(struct mtget));
  2124. mtget.mt_type = MT_ISSCSI2;
  2125. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  2126. mtget.mt_dsreg =
  2127. ((tape->blk_size * tape->user_bs_factor)
  2128. << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  2129. if (tape->drv_write_prot)
  2130. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  2131. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  2132. return -EFAULT;
  2133. return 0;
  2134. case MTIOCPOS:
  2135. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  2136. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  2137. return -EFAULT;
  2138. return 0;
  2139. default:
  2140. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  2141. idetape_discard_read_pipeline(drive, 1);
  2142. return idetape_blkdev_ioctl(drive, cmd, arg);
  2143. }
  2144. }
  2145. /*
  2146. * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
  2147. * block size with the reported value.
  2148. */
  2149. static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
  2150. {
  2151. idetape_tape_t *tape = drive->driver_data;
  2152. struct ide_atapi_pc pc;
  2153. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  2154. if (idetape_queue_pc_tail(drive, &pc)) {
  2155. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  2156. if (tape->blk_size == 0) {
  2157. printk(KERN_WARNING "ide-tape: Cannot deal with zero "
  2158. "block size, assuming 32k\n");
  2159. tape->blk_size = 32768;
  2160. }
  2161. return;
  2162. }
  2163. tape->blk_size = (pc.buf[4 + 5] << 16) +
  2164. (pc.buf[4 + 6] << 8) +
  2165. pc.buf[4 + 7];
  2166. tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
  2167. }
  2168. static int idetape_chrdev_open(struct inode *inode, struct file *filp)
  2169. {
  2170. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  2171. ide_drive_t *drive;
  2172. idetape_tape_t *tape;
  2173. struct ide_atapi_pc pc;
  2174. int retval;
  2175. if (i >= MAX_HWIFS * MAX_DRIVES)
  2176. return -ENXIO;
  2177. tape = ide_tape_chrdev_get(i);
  2178. if (!tape)
  2179. return -ENXIO;
  2180. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  2181. /*
  2182. * We really want to do nonseekable_open(inode, filp); here, but some
  2183. * versions of tar incorrectly call lseek on tapes and bail out if that
  2184. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  2185. */
  2186. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  2187. drive = tape->drive;
  2188. filp->private_data = tape;
  2189. if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
  2190. retval = -EBUSY;
  2191. goto out_put_tape;
  2192. }
  2193. retval = idetape_wait_ready(drive, 60 * HZ);
  2194. if (retval) {
  2195. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  2196. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  2197. goto out_put_tape;
  2198. }
  2199. idetape_read_position(drive);
  2200. if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
  2201. (void)idetape_rewind_tape(drive);
  2202. /* Read block size and write protect status from drive. */
  2203. ide_tape_get_bsize_from_bdesc(drive);
  2204. /* Set write protect flag if device is opened as read-only. */
  2205. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  2206. tape->write_prot = 1;
  2207. else
  2208. tape->write_prot = tape->drv_write_prot;
  2209. /* Make sure drive isn't write protected if user wants to write. */
  2210. if (tape->write_prot) {
  2211. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  2212. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  2213. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  2214. retval = -EROFS;
  2215. goto out_put_tape;
  2216. }
  2217. }
  2218. /* Lock the tape drive door so user can't eject. */
  2219. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  2220. if (idetape_create_prevent_cmd(drive, &pc, 1)) {
  2221. if (!idetape_queue_pc_tail(drive, &pc)) {
  2222. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  2223. tape->door_locked = DOOR_LOCKED;
  2224. }
  2225. }
  2226. }
  2227. return 0;
  2228. out_put_tape:
  2229. ide_tape_put(tape);
  2230. return retval;
  2231. }
  2232. static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
  2233. {
  2234. idetape_tape_t *tape = drive->driver_data;
  2235. idetape_empty_write_pipeline(drive);
  2236. tape->merge_stage = ide_tape_kmalloc_buffer(tape, 1, 0);
  2237. if (tape->merge_stage != NULL) {
  2238. idetape_pad_zeros(drive, tape->blk_size *
  2239. (tape->user_bs_factor - 1));
  2240. __idetape_kfree_stage(tape->merge_stage);
  2241. tape->merge_stage = NULL;
  2242. }
  2243. idetape_write_filemark(drive);
  2244. idetape_flush_tape_buffers(drive);
  2245. idetape_flush_tape_buffers(drive);
  2246. }
  2247. static int idetape_chrdev_release(struct inode *inode, struct file *filp)
  2248. {
  2249. struct ide_tape_obj *tape = ide_tape_f(filp);
  2250. ide_drive_t *drive = tape->drive;
  2251. struct ide_atapi_pc pc;
  2252. unsigned int minor = iminor(inode);
  2253. lock_kernel();
  2254. tape = drive->driver_data;
  2255. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  2256. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  2257. idetape_write_release(drive, minor);
  2258. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  2259. if (minor < 128)
  2260. idetape_discard_read_pipeline(drive, 1);
  2261. }
  2262. if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
  2263. (void) idetape_rewind_tape(drive);
  2264. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  2265. if (tape->door_locked == DOOR_LOCKED) {
  2266. if (idetape_create_prevent_cmd(drive, &pc, 0)) {
  2267. if (!idetape_queue_pc_tail(drive, &pc))
  2268. tape->door_locked = DOOR_UNLOCKED;
  2269. }
  2270. }
  2271. }
  2272. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  2273. ide_tape_put(tape);
  2274. unlock_kernel();
  2275. return 0;
  2276. }
  2277. /*
  2278. * check the contents of the ATAPI IDENTIFY command results. We return:
  2279. *
  2280. * 1 - If the tape can be supported by us, based on the information we have so
  2281. * far.
  2282. *
  2283. * 0 - If this tape driver is not currently supported by us.
  2284. */
  2285. static int idetape_identify_device(ide_drive_t *drive)
  2286. {
  2287. u8 gcw[2], protocol, device_type, removable, packet_size;
  2288. if (drive->id_read == 0)
  2289. return 1;
  2290. *((unsigned short *) &gcw) = drive->id->config;
  2291. protocol = (gcw[1] & 0xC0) >> 6;
  2292. device_type = gcw[1] & 0x1F;
  2293. removable = !!(gcw[0] & 0x80);
  2294. packet_size = gcw[0] & 0x3;
  2295. /* Check that we can support this device */
  2296. if (protocol != 2)
  2297. printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
  2298. protocol);
  2299. else if (device_type != 1)
  2300. printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
  2301. "to tape\n", device_type);
  2302. else if (!removable)
  2303. printk(KERN_ERR "ide-tape: The removable flag is not set\n");
  2304. else if (packet_size != 0) {
  2305. printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
  2306. " bytes\n", packet_size);
  2307. } else
  2308. return 1;
  2309. return 0;
  2310. }
  2311. static void idetape_get_inquiry_results(ide_drive_t *drive)
  2312. {
  2313. idetape_tape_t *tape = drive->driver_data;
  2314. struct ide_atapi_pc pc;
  2315. char fw_rev[6], vendor_id[10], product_id[18];
  2316. idetape_create_inquiry_cmd(&pc);
  2317. if (idetape_queue_pc_tail(drive, &pc)) {
  2318. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
  2319. tape->name);
  2320. return;
  2321. }
  2322. memcpy(vendor_id, &pc.buf[8], 8);
  2323. memcpy(product_id, &pc.buf[16], 16);
  2324. memcpy(fw_rev, &pc.buf[32], 4);
  2325. ide_fixstring(vendor_id, 10, 0);
  2326. ide_fixstring(product_id, 18, 0);
  2327. ide_fixstring(fw_rev, 6, 0);
  2328. printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
  2329. drive->name, tape->name, vendor_id, product_id, fw_rev);
  2330. }
  2331. /*
  2332. * Ask the tape about its various parameters. In particular, we will adjust our
  2333. * data transfer buffer size to the recommended value as returned by the tape.
  2334. */
  2335. static void idetape_get_mode_sense_results(ide_drive_t *drive)
  2336. {
  2337. idetape_tape_t *tape = drive->driver_data;
  2338. struct ide_atapi_pc pc;
  2339. u8 *caps;
  2340. u8 speed, max_speed;
  2341. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  2342. if (idetape_queue_pc_tail(drive, &pc)) {
  2343. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
  2344. " some default values\n");
  2345. tape->blk_size = 512;
  2346. put_unaligned(52, (u16 *)&tape->caps[12]);
  2347. put_unaligned(540, (u16 *)&tape->caps[14]);
  2348. put_unaligned(6*52, (u16 *)&tape->caps[16]);
  2349. return;
  2350. }
  2351. caps = pc.buf + 4 + pc.buf[3];
  2352. /* convert to host order and save for later use */
  2353. speed = be16_to_cpu(*(u16 *)&caps[14]);
  2354. max_speed = be16_to_cpu(*(u16 *)&caps[8]);
  2355. put_unaligned(max_speed, (u16 *)&caps[8]);
  2356. put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
  2357. put_unaligned(speed, (u16 *)&caps[14]);
  2358. put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
  2359. if (!speed) {
  2360. printk(KERN_INFO "ide-tape: %s: invalid tape speed "
  2361. "(assuming 650KB/sec)\n", drive->name);
  2362. put_unaligned(650, (u16 *)&caps[14]);
  2363. }
  2364. if (!max_speed) {
  2365. printk(KERN_INFO "ide-tape: %s: invalid max_speed "
  2366. "(assuming 650KB/sec)\n", drive->name);
  2367. put_unaligned(650, (u16 *)&caps[8]);
  2368. }
  2369. memcpy(&tape->caps, caps, 20);
  2370. if (caps[7] & 0x02)
  2371. tape->blk_size = 512;
  2372. else if (caps[7] & 0x04)
  2373. tape->blk_size = 1024;
  2374. }
  2375. #ifdef CONFIG_IDE_PROC_FS
  2376. static void idetape_add_settings(ide_drive_t *drive)
  2377. {
  2378. idetape_tape_t *tape = drive->driver_data;
  2379. ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
  2380. 1, 2, (u16 *)&tape->caps[16], NULL);
  2381. ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
  2382. 1, 1, (u16 *)&tape->caps[14], NULL);
  2383. ide_add_setting(drive, "buffer_size", SETTING_READ, TYPE_INT, 0, 0xffff,
  2384. 1, 1024, &tape->buffer_size, NULL);
  2385. ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
  2386. IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
  2387. NULL);
  2388. ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
  2389. 1, &drive->dsc_overlap, NULL);
  2390. ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
  2391. 1, 1, &tape->avg_speed, NULL);
  2392. ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
  2393. 1, &tape->debug_mask, NULL);
  2394. }
  2395. #else
  2396. static inline void idetape_add_settings(ide_drive_t *drive) { ; }
  2397. #endif
  2398. /*
  2399. * The function below is called to:
  2400. *
  2401. * 1. Initialize our various state variables.
  2402. * 2. Ask the tape for its capabilities.
  2403. * 3. Allocate a buffer which will be used for data transfer. The buffer size
  2404. * is chosen based on the recommendation which we received in step 2.
  2405. *
  2406. * Note that at this point ide.c already assigned us an irq, so that we can
  2407. * queue requests here and wait for their completion.
  2408. */
  2409. static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
  2410. {
  2411. unsigned long t;
  2412. int speed;
  2413. int buffer_size;
  2414. u8 gcw[2];
  2415. u16 *ctl = (u16 *)&tape->caps[12];
  2416. spin_lock_init(&tape->lock);
  2417. drive->dsc_overlap = 1;
  2418. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  2419. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  2420. tape->name);
  2421. drive->dsc_overlap = 0;
  2422. }
  2423. /* Seagate Travan drives do not support DSC overlap. */
  2424. if (strstr(drive->id->model, "Seagate STT3401"))
  2425. drive->dsc_overlap = 0;
  2426. tape->minor = minor;
  2427. tape->name[0] = 'h';
  2428. tape->name[1] = 't';
  2429. tape->name[2] = '0' + minor;
  2430. tape->chrdev_dir = IDETAPE_DIR_NONE;
  2431. tape->pc = tape->pc_stack;
  2432. *((unsigned short *) &gcw) = drive->id->config;
  2433. /* Command packet DRQ type */
  2434. if (((gcw[0] & 0x60) >> 5) == 1)
  2435. set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
  2436. idetape_get_inquiry_results(drive);
  2437. idetape_get_mode_sense_results(drive);
  2438. ide_tape_get_bsize_from_bdesc(drive);
  2439. tape->user_bs_factor = 1;
  2440. tape->buffer_size = *ctl * tape->blk_size;
  2441. while (tape->buffer_size > 0xffff) {
  2442. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  2443. *ctl /= 2;
  2444. tape->buffer_size = *ctl * tape->blk_size;
  2445. }
  2446. buffer_size = tape->buffer_size;
  2447. tape->pages_per_buffer = buffer_size / PAGE_SIZE;
  2448. if (buffer_size % PAGE_SIZE) {
  2449. tape->pages_per_buffer++;
  2450. tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
  2451. }
  2452. /* select the "best" DSC read/write polling freq */
  2453. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  2454. t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
  2455. /*
  2456. * Ensure that the number we got makes sense; limit it within
  2457. * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  2458. */
  2459. tape->best_dsc_rw_freq = max_t(unsigned long,
  2460. min_t(unsigned long, t, IDETAPE_DSC_RW_MAX),
  2461. IDETAPE_DSC_RW_MIN);
  2462. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  2463. "%lums tDSC%s\n",
  2464. drive->name, tape->name, *(u16 *)&tape->caps[14],
  2465. (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
  2466. tape->buffer_size / 1024,
  2467. tape->best_dsc_rw_freq * 1000 / HZ,
  2468. drive->using_dma ? ", DMA":"");
  2469. idetape_add_settings(drive);
  2470. }
  2471. static void ide_tape_remove(ide_drive_t *drive)
  2472. {
  2473. idetape_tape_t *tape = drive->driver_data;
  2474. ide_proc_unregister_driver(drive, tape->driver);
  2475. ide_unregister_region(tape->disk);
  2476. ide_tape_put(tape);
  2477. }
  2478. static void ide_tape_release(struct kref *kref)
  2479. {
  2480. struct ide_tape_obj *tape = to_ide_tape(kref);
  2481. ide_drive_t *drive = tape->drive;
  2482. struct gendisk *g = tape->disk;
  2483. BUG_ON(tape->merge_stage_size);
  2484. drive->dsc_overlap = 0;
  2485. drive->driver_data = NULL;
  2486. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  2487. device_destroy(idetape_sysfs_class,
  2488. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  2489. idetape_devs[tape->minor] = NULL;
  2490. g->private_data = NULL;
  2491. put_disk(g);
  2492. kfree(tape);
  2493. }
  2494. #ifdef CONFIG_IDE_PROC_FS
  2495. static int proc_idetape_read_name
  2496. (char *page, char **start, off_t off, int count, int *eof, void *data)
  2497. {
  2498. ide_drive_t *drive = (ide_drive_t *) data;
  2499. idetape_tape_t *tape = drive->driver_data;
  2500. char *out = page;
  2501. int len;
  2502. len = sprintf(out, "%s\n", tape->name);
  2503. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  2504. }
  2505. static ide_proc_entry_t idetape_proc[] = {
  2506. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  2507. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  2508. { NULL, 0, NULL, NULL }
  2509. };
  2510. #endif
  2511. static int ide_tape_probe(ide_drive_t *);
  2512. static ide_driver_t idetape_driver = {
  2513. .gen_driver = {
  2514. .owner = THIS_MODULE,
  2515. .name = "ide-tape",
  2516. .bus = &ide_bus_type,
  2517. },
  2518. .probe = ide_tape_probe,
  2519. .remove = ide_tape_remove,
  2520. .version = IDETAPE_VERSION,
  2521. .media = ide_tape,
  2522. .supports_dsc_overlap = 1,
  2523. .do_request = idetape_do_request,
  2524. .end_request = idetape_end_request,
  2525. .error = __ide_error,
  2526. .abort = __ide_abort,
  2527. #ifdef CONFIG_IDE_PROC_FS
  2528. .proc = idetape_proc,
  2529. #endif
  2530. };
  2531. /* Our character device supporting functions, passed to register_chrdev. */
  2532. static const struct file_operations idetape_fops = {
  2533. .owner = THIS_MODULE,
  2534. .read = idetape_chrdev_read,
  2535. .write = idetape_chrdev_write,
  2536. .ioctl = idetape_chrdev_ioctl,
  2537. .open = idetape_chrdev_open,
  2538. .release = idetape_chrdev_release,
  2539. };
  2540. static int idetape_open(struct inode *inode, struct file *filp)
  2541. {
  2542. struct gendisk *disk = inode->i_bdev->bd_disk;
  2543. struct ide_tape_obj *tape;
  2544. tape = ide_tape_get(disk);
  2545. if (!tape)
  2546. return -ENXIO;
  2547. return 0;
  2548. }
  2549. static int idetape_release(struct inode *inode, struct file *filp)
  2550. {
  2551. struct gendisk *disk = inode->i_bdev->bd_disk;
  2552. struct ide_tape_obj *tape = ide_tape_g(disk);
  2553. ide_tape_put(tape);
  2554. return 0;
  2555. }
  2556. static int idetape_ioctl(struct inode *inode, struct file *file,
  2557. unsigned int cmd, unsigned long arg)
  2558. {
  2559. struct block_device *bdev = inode->i_bdev;
  2560. struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
  2561. ide_drive_t *drive = tape->drive;
  2562. int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  2563. if (err == -EINVAL)
  2564. err = idetape_blkdev_ioctl(drive, cmd, arg);
  2565. return err;
  2566. }
  2567. static struct block_device_operations idetape_block_ops = {
  2568. .owner = THIS_MODULE,
  2569. .open = idetape_open,
  2570. .release = idetape_release,
  2571. .ioctl = idetape_ioctl,
  2572. };
  2573. static int ide_tape_probe(ide_drive_t *drive)
  2574. {
  2575. idetape_tape_t *tape;
  2576. struct gendisk *g;
  2577. int minor;
  2578. if (!strstr("ide-tape", drive->driver_req))
  2579. goto failed;
  2580. if (!drive->present)
  2581. goto failed;
  2582. if (drive->media != ide_tape)
  2583. goto failed;
  2584. if (!idetape_identify_device(drive)) {
  2585. printk(KERN_ERR "ide-tape: %s: not supported by this version of"
  2586. " the driver\n", drive->name);
  2587. goto failed;
  2588. }
  2589. if (drive->scsi) {
  2590. printk(KERN_INFO "ide-tape: passing drive %s to ide-scsi"
  2591. " emulation.\n", drive->name);
  2592. goto failed;
  2593. }
  2594. tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
  2595. if (tape == NULL) {
  2596. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
  2597. drive->name);
  2598. goto failed;
  2599. }
  2600. g = alloc_disk(1 << PARTN_BITS);
  2601. if (!g)
  2602. goto out_free_tape;
  2603. ide_init_disk(g, drive);
  2604. ide_proc_register_driver(drive, &idetape_driver);
  2605. kref_init(&tape->kref);
  2606. tape->drive = drive;
  2607. tape->driver = &idetape_driver;
  2608. tape->disk = g;
  2609. g->private_data = &tape->driver;
  2610. drive->driver_data = tape;
  2611. mutex_lock(&idetape_ref_mutex);
  2612. for (minor = 0; idetape_devs[minor]; minor++)
  2613. ;
  2614. idetape_devs[minor] = tape;
  2615. mutex_unlock(&idetape_ref_mutex);
  2616. idetape_setup(drive, tape, minor);
  2617. device_create(idetape_sysfs_class, &drive->gendev,
  2618. MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
  2619. device_create(idetape_sysfs_class, &drive->gendev,
  2620. MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
  2621. g->fops = &idetape_block_ops;
  2622. ide_register_region(g);
  2623. return 0;
  2624. out_free_tape:
  2625. kfree(tape);
  2626. failed:
  2627. return -ENODEV;
  2628. }
  2629. static void __exit idetape_exit(void)
  2630. {
  2631. driver_unregister(&idetape_driver.gen_driver);
  2632. class_destroy(idetape_sysfs_class);
  2633. unregister_chrdev(IDETAPE_MAJOR, "ht");
  2634. }
  2635. static int __init idetape_init(void)
  2636. {
  2637. int error = 1;
  2638. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  2639. if (IS_ERR(idetape_sysfs_class)) {
  2640. idetape_sysfs_class = NULL;
  2641. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  2642. error = -EBUSY;
  2643. goto out;
  2644. }
  2645. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  2646. printk(KERN_ERR "ide-tape: Failed to register chrdev"
  2647. " interface\n");
  2648. error = -EBUSY;
  2649. goto out_free_class;
  2650. }
  2651. error = driver_register(&idetape_driver.gen_driver);
  2652. if (error)
  2653. goto out_free_driver;
  2654. return 0;
  2655. out_free_driver:
  2656. driver_unregister(&idetape_driver.gen_driver);
  2657. out_free_class:
  2658. class_destroy(idetape_sysfs_class);
  2659. out:
  2660. return error;
  2661. }
  2662. MODULE_ALIAS("ide:*m-tape*");
  2663. module_init(idetape_init);
  2664. module_exit(idetape_exit);
  2665. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
  2666. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  2667. MODULE_LICENSE("GPL");