ide-tape.c 110 KB

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