ide-tape.c 110 KB

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