ide-tape.c 90 KB

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