ide-tape.c 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377
  1. /*
  2. * IDE ATAPI streaming tape driver.
  3. *
  4. * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
  5. * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
  6. *
  7. * This driver was constructed as a student project in the software laboratory
  8. * of the faculty of electrical engineering in the Technion - Israel's
  9. * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
  10. *
  11. * It is hereby placed under the terms of the GNU general public license.
  12. * (See linux/COPYING).
  13. *
  14. * For a historical changelog see
  15. * Documentation/ide/ChangeLog.ide-tape.1995-2002
  16. */
  17. #define IDETAPE_VERSION "1.19"
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/timer.h>
  24. #include <linux/mm.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/major.h>
  28. #include <linux/errno.h>
  29. #include <linux/genhd.h>
  30. #include <linux/slab.h>
  31. #include <linux/pci.h>
  32. #include <linux/ide.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/completion.h>
  35. #include <linux/bitops.h>
  36. #include <linux/mutex.h>
  37. #include <asm/byteorder.h>
  38. #include <asm/irq.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/io.h>
  41. #include <asm/unaligned.h>
  42. /*
  43. * partition
  44. */
  45. typedef struct os_partition_s {
  46. __u8 partition_num;
  47. __u8 par_desc_ver;
  48. __u16 wrt_pass_cntr;
  49. __u32 first_frame_addr;
  50. __u32 last_frame_addr;
  51. __u32 eod_frame_addr;
  52. } os_partition_t;
  53. /*
  54. * DAT entry
  55. */
  56. typedef struct os_dat_entry_s {
  57. __u32 blk_sz;
  58. __u16 blk_cnt;
  59. __u8 flags;
  60. __u8 reserved;
  61. } os_dat_entry_t;
  62. /*
  63. * DAT
  64. */
  65. #define OS_DAT_FLAGS_DATA (0xc)
  66. #define OS_DAT_FLAGS_MARK (0x1)
  67. typedef struct os_dat_s {
  68. __u8 dat_sz;
  69. __u8 reserved1;
  70. __u8 entry_cnt;
  71. __u8 reserved3;
  72. os_dat_entry_t dat_list[16];
  73. } os_dat_t;
  74. #include <linux/mtio.h>
  75. /**************************** Tunable parameters *****************************/
  76. /*
  77. * Pipelined mode parameters.
  78. *
  79. * We try to use the minimum number of stages which is enough to
  80. * keep the tape constantly streaming. To accomplish that, we implement
  81. * a feedback loop around the maximum number of stages:
  82. *
  83. * We start from MIN maximum stages (we will not even use MIN stages
  84. * if we don't need them), increment it by RATE*(MAX-MIN)
  85. * whenever we sense that the pipeline is empty, until we reach
  86. * the optimum value or until we reach MAX.
  87. *
  88. * Setting the following parameter to 0 is illegal: the pipelined mode
  89. * cannot be disabled (calculate_speeds() divides by tape->max_stages.)
  90. */
  91. #define IDETAPE_MIN_PIPELINE_STAGES 1
  92. #define IDETAPE_MAX_PIPELINE_STAGES 400
  93. #define IDETAPE_INCREASE_STAGES_RATE 20
  94. /*
  95. * The following are used to debug the driver:
  96. *
  97. * Setting IDETAPE_DEBUG_INFO to 1 will report device capabilities.
  98. * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
  99. * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
  100. * some places.
  101. *
  102. * Setting them to 0 will restore normal operation mode:
  103. *
  104. * 1. Disable logging normal successful operations.
  105. * 2. Disable self-sanity checks.
  106. * 3. Errors will still be logged, of course.
  107. *
  108. * All the #if DEBUG code will be removed some day, when the driver
  109. * is verified to be stable enough. This will make it much more
  110. * esthetic.
  111. */
  112. #define IDETAPE_DEBUG_INFO 0
  113. #define IDETAPE_DEBUG_LOG 0
  114. #define IDETAPE_DEBUG_BUGS 1
  115. /*
  116. * After each failed packet command we issue a request sense command
  117. * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
  118. *
  119. * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
  120. */
  121. #define IDETAPE_MAX_PC_RETRIES 3
  122. /*
  123. * With each packet command, we allocate a buffer of
  124. * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
  125. * commands (Not for READ/WRITE commands).
  126. */
  127. #define IDETAPE_PC_BUFFER_SIZE 256
  128. /*
  129. * In various places in the driver, we need to allocate storage
  130. * for packet commands and requests, which will remain valid while
  131. * we leave the driver to wait for an interrupt or a timeout event.
  132. */
  133. #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
  134. /*
  135. * Some drives (for example, Seagate STT3401A Travan) require a very long
  136. * timeout, because they don't return an interrupt or clear their busy bit
  137. * until after the command completes (even retension commands).
  138. */
  139. #define IDETAPE_WAIT_CMD (900*HZ)
  140. /*
  141. * The following parameter is used to select the point in the internal
  142. * tape fifo in which we will start to refill the buffer. Decreasing
  143. * the following parameter will improve the system's latency and
  144. * interactive response, while using a high value might improve system
  145. * throughput.
  146. */
  147. #define IDETAPE_FIFO_THRESHOLD 2
  148. /*
  149. * DSC polling parameters.
  150. *
  151. * Polling for DSC (a single bit in the status register) is a very
  152. * important function in ide-tape. There are two cases in which we
  153. * poll for DSC:
  154. *
  155. * 1. Before a read/write packet command, to ensure that we
  156. * can transfer data from/to the tape's data buffers, without
  157. * causing an actual media access. In case the tape is not
  158. * ready yet, we take out our request from the device
  159. * request queue, so that ide.c will service requests from
  160. * the other device on the same interface meanwhile.
  161. *
  162. * 2. After the successful initialization of a "media access
  163. * packet command", which is a command which can take a long
  164. * time to complete (it can be several seconds or even an hour).
  165. *
  166. * Again, we postpone our request in the middle to free the bus
  167. * for the other device. The polling frequency here should be
  168. * lower than the read/write frequency since those media access
  169. * commands are slow. We start from a "fast" frequency -
  170. * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
  171. * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
  172. * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
  173. *
  174. * We also set a timeout for the timer, in case something goes wrong.
  175. * The timeout should be longer then the maximum execution time of a
  176. * tape operation.
  177. */
  178. /*
  179. * DSC timings.
  180. */
  181. #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
  182. #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
  183. #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
  184. #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
  185. #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
  186. #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
  187. #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
  188. /*************************** End of tunable parameters ***********************/
  189. /*
  190. * Read/Write error simulation
  191. */
  192. #define SIMULATE_ERRORS 0
  193. /*
  194. * For general magnetic tape device compatibility.
  195. */
  196. typedef enum {
  197. idetape_direction_none,
  198. idetape_direction_read,
  199. idetape_direction_write
  200. } idetape_chrdev_direction_t;
  201. struct idetape_bh {
  202. u32 b_size;
  203. atomic_t b_count;
  204. struct idetape_bh *b_reqnext;
  205. char *b_data;
  206. };
  207. /*
  208. * Our view of a packet command.
  209. */
  210. typedef struct idetape_packet_command_s {
  211. u8 c[12]; /* Actual packet bytes */
  212. int retries; /* On each retry, we increment retries */
  213. int error; /* Error code */
  214. int request_transfer; /* Bytes to transfer */
  215. int actually_transferred; /* Bytes actually transferred */
  216. int buffer_size; /* Size of our data buffer */
  217. struct idetape_bh *bh;
  218. char *b_data;
  219. int b_count;
  220. u8 *buffer; /* Data buffer */
  221. u8 *current_position; /* Pointer into the above buffer */
  222. ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
  223. u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
  224. unsigned long flags; /* Status/Action bit flags: long for set_bit */
  225. } idetape_pc_t;
  226. /*
  227. * Packet command flag bits.
  228. */
  229. /* Set when an error is considered normal - We won't retry */
  230. #define PC_ABORT 0
  231. /* 1 When polling for DSC on a media access command */
  232. #define PC_WAIT_FOR_DSC 1
  233. /* 1 when we prefer to use DMA if possible */
  234. #define PC_DMA_RECOMMENDED 2
  235. /* 1 while DMA in progress */
  236. #define PC_DMA_IN_PROGRESS 3
  237. /* 1 when encountered problem during DMA */
  238. #define PC_DMA_ERROR 4
  239. /* Data direction */
  240. #define PC_WRITING 5
  241. /*
  242. * Capabilities and Mechanical Status Page
  243. */
  244. typedef struct {
  245. unsigned page_code :6; /* Page code - Should be 0x2a */
  246. __u8 reserved0_6 :1;
  247. __u8 ps :1; /* parameters saveable */
  248. __u8 page_length; /* Page Length - Should be 0x12 */
  249. __u8 reserved2, reserved3;
  250. unsigned ro :1; /* Read Only Mode */
  251. unsigned reserved4_1234 :4;
  252. unsigned sprev :1; /* Supports SPACE in the reverse direction */
  253. unsigned reserved4_67 :2;
  254. unsigned reserved5_012 :3;
  255. unsigned efmt :1; /* Supports ERASE command initiated formatting */
  256. unsigned reserved5_4 :1;
  257. unsigned qfa :1; /* Supports the QFA two partition formats */
  258. unsigned reserved5_67 :2;
  259. unsigned lock :1; /* Supports locking the volume */
  260. unsigned locked :1; /* The volume is locked */
  261. unsigned prevent :1; /* The device defaults in the prevent state after power up */
  262. unsigned eject :1; /* The device can eject the volume */
  263. __u8 disconnect :1; /* The device can break request > ctl */
  264. __u8 reserved6_5 :1;
  265. unsigned ecc :1; /* Supports error correction */
  266. unsigned cmprs :1; /* Supports data compression */
  267. unsigned reserved7_0 :1;
  268. unsigned blk512 :1; /* Supports 512 bytes block size */
  269. unsigned blk1024 :1; /* Supports 1024 bytes block size */
  270. unsigned reserved7_3_6 :4;
  271. unsigned blk32768 :1; /* slowb - the device restricts the byte count for PIO */
  272. /* transfers for slow buffer memory ??? */
  273. /* Also 32768 block size in some cases */
  274. __u16 max_speed; /* Maximum speed supported in KBps */
  275. __u8 reserved10, reserved11;
  276. __u16 ctl; /* Continuous Transfer Limit in blocks */
  277. __u16 speed; /* Current Speed, in KBps */
  278. __u16 buffer_size; /* Buffer Size, in 512 bytes */
  279. __u8 reserved18, reserved19;
  280. } idetape_capabilities_page_t;
  281. /*
  282. * Block Size Page
  283. */
  284. typedef struct {
  285. unsigned page_code :6; /* Page code - Should be 0x30 */
  286. unsigned reserved1_6 :1;
  287. unsigned ps :1;
  288. __u8 page_length; /* Page Length - Should be 2 */
  289. __u8 reserved2;
  290. unsigned play32 :1;
  291. unsigned play32_5 :1;
  292. unsigned reserved2_23 :2;
  293. unsigned record32 :1;
  294. unsigned record32_5 :1;
  295. unsigned reserved2_6 :1;
  296. unsigned one :1;
  297. } idetape_block_size_page_t;
  298. /*
  299. * A pipeline stage.
  300. */
  301. typedef struct idetape_stage_s {
  302. struct request rq; /* The corresponding request */
  303. struct idetape_bh *bh; /* The data buffers */
  304. struct idetape_stage_s *next; /* Pointer to the next stage */
  305. } idetape_stage_t;
  306. /*
  307. * Most of our global data which we need to save even as we leave the
  308. * driver due to an interrupt or a timer event is stored in a variable
  309. * of type idetape_tape_t, defined below.
  310. */
  311. typedef struct ide_tape_obj {
  312. ide_drive_t *drive;
  313. ide_driver_t *driver;
  314. struct gendisk *disk;
  315. struct kref kref;
  316. /*
  317. * Since a typical character device operation requires more
  318. * than one packet command, we provide here enough memory
  319. * for the maximum of interconnected packet commands.
  320. * The packet commands are stored in the circular array pc_stack.
  321. * pc_stack_index points to the last used entry, and warps around
  322. * to the start when we get to the last array entry.
  323. *
  324. * pc points to the current processed packet command.
  325. *
  326. * failed_pc points to the last failed packet command, or contains
  327. * NULL if we do not need to retry any packet command. This is
  328. * required since an additional packet command is needed before the
  329. * retry, to get detailed information on what went wrong.
  330. */
  331. /* Current packet command */
  332. idetape_pc_t *pc;
  333. /* Last failed packet command */
  334. idetape_pc_t *failed_pc;
  335. /* Packet command stack */
  336. idetape_pc_t pc_stack[IDETAPE_PC_STACK];
  337. /* Next free packet command storage space */
  338. int pc_stack_index;
  339. struct request rq_stack[IDETAPE_PC_STACK];
  340. /* We implement a circular array */
  341. int rq_stack_index;
  342. /*
  343. * DSC polling variables.
  344. *
  345. * While polling for DSC we use postponed_rq to postpone the
  346. * current request so that ide.c will be able to service
  347. * pending requests on the other device. Note that at most
  348. * we will have only one DSC (usually data transfer) request
  349. * in the device request queue. Additional requests can be
  350. * queued in our internal pipeline, but they will be visible
  351. * to ide.c only one at a time.
  352. */
  353. struct request *postponed_rq;
  354. /* The time in which we started polling for DSC */
  355. unsigned long dsc_polling_start;
  356. /* Timer used to poll for dsc */
  357. struct timer_list dsc_timer;
  358. /* Read/Write dsc polling frequency */
  359. unsigned long best_dsc_rw_frequency;
  360. /* The current polling frequency */
  361. unsigned long dsc_polling_frequency;
  362. /* Maximum waiting time */
  363. unsigned long dsc_timeout;
  364. /*
  365. * Read position information
  366. */
  367. u8 partition;
  368. /* Current block */
  369. unsigned int first_frame_position;
  370. unsigned int last_frame_position;
  371. unsigned int blocks_in_buffer;
  372. /*
  373. * Last error information
  374. */
  375. u8 sense_key, asc, ascq;
  376. /*
  377. * Character device operation
  378. */
  379. unsigned int minor;
  380. /* device name */
  381. char name[4];
  382. /* Current character device data transfer direction */
  383. idetape_chrdev_direction_t chrdev_direction;
  384. /*
  385. * Device information
  386. */
  387. /* Usually 512 or 1024 bytes */
  388. unsigned short tape_block_size;
  389. int user_bs_factor;
  390. /* Copy of the tape's Capabilities and Mechanical Page */
  391. idetape_capabilities_page_t capabilities;
  392. /*
  393. * Active data transfer request parameters.
  394. *
  395. * At most, there is only one ide-tape originated data transfer
  396. * request in the device request queue. This allows ide.c to
  397. * easily service requests from the other device when we
  398. * postpone our active request. In the pipelined operation
  399. * mode, we use our internal pipeline structure to hold
  400. * more data requests.
  401. *
  402. * The data buffer size is chosen based on the tape's
  403. * recommendation.
  404. */
  405. /* Pointer to the request which is waiting in the device request queue */
  406. struct request *active_data_request;
  407. /* Data buffer size (chosen based on the tape's recommendation */
  408. int stage_size;
  409. idetape_stage_t *merge_stage;
  410. int merge_stage_size;
  411. struct idetape_bh *bh;
  412. char *b_data;
  413. int b_count;
  414. /*
  415. * Pipeline parameters.
  416. *
  417. * To accomplish non-pipelined mode, we simply set the following
  418. * variables to zero (or NULL, where appropriate).
  419. */
  420. /* Number of currently used stages */
  421. int nr_stages;
  422. /* Number of pending stages */
  423. int nr_pending_stages;
  424. /* We will not allocate more than this number of stages */
  425. int max_stages, min_pipeline, max_pipeline;
  426. /* The first stage which will be removed from the pipeline */
  427. idetape_stage_t *first_stage;
  428. /* The currently active stage */
  429. idetape_stage_t *active_stage;
  430. /* Will be serviced after the currently active request */
  431. idetape_stage_t *next_stage;
  432. /* New requests will be added to the pipeline here */
  433. idetape_stage_t *last_stage;
  434. /* Optional free stage which we can use */
  435. idetape_stage_t *cache_stage;
  436. int pages_per_stage;
  437. /* Wasted space in each stage */
  438. int excess_bh_size;
  439. /* Status/Action flags: long for set_bit */
  440. unsigned long flags;
  441. /* protects the ide-tape queue */
  442. spinlock_t spinlock;
  443. /*
  444. * Measures average tape speed
  445. */
  446. unsigned long avg_time;
  447. int avg_size;
  448. int avg_speed;
  449. char vendor_id[10];
  450. char product_id[18];
  451. char firmware_revision[6];
  452. int firmware_revision_num;
  453. /* the door is currently locked */
  454. int door_locked;
  455. /* the tape hardware is write protected */
  456. char drv_write_prot;
  457. /* the tape is write protected (hardware or opened as read-only) */
  458. char write_prot;
  459. /*
  460. * Limit the number of times a request can
  461. * be postponed, to avoid an infinite postpone
  462. * deadlock.
  463. */
  464. /* request postpone count limit */
  465. int postpone_cnt;
  466. /*
  467. * Measures number of frames:
  468. *
  469. * 1. written/read to/from the driver pipeline (pipeline_head).
  470. * 2. written/read to/from the tape buffers (idetape_bh).
  471. * 3. written/read by the tape to/from the media (tape_head).
  472. */
  473. int pipeline_head;
  474. int buffer_head;
  475. int tape_head;
  476. int last_tape_head;
  477. /*
  478. * Speed control at the tape buffers input/output
  479. */
  480. unsigned long insert_time;
  481. int insert_size;
  482. int insert_speed;
  483. int max_insert_speed;
  484. int measure_insert_time;
  485. /*
  486. * Measure tape still time, in milliseconds
  487. */
  488. unsigned long tape_still_time_begin;
  489. int tape_still_time;
  490. /*
  491. * Speed regulation negative feedback loop
  492. */
  493. int speed_control;
  494. int pipeline_head_speed;
  495. int controlled_pipeline_head_speed;
  496. int uncontrolled_pipeline_head_speed;
  497. int controlled_last_pipeline_head;
  498. int uncontrolled_last_pipeline_head;
  499. unsigned long uncontrolled_pipeline_head_time;
  500. unsigned long controlled_pipeline_head_time;
  501. int controlled_previous_pipeline_head;
  502. int uncontrolled_previous_pipeline_head;
  503. unsigned long controlled_previous_head_time;
  504. unsigned long uncontrolled_previous_head_time;
  505. int restart_speed_control_req;
  506. /*
  507. * Debug_level determines amount of debugging output;
  508. * can be changed using /proc/ide/hdx/settings
  509. * 0 : almost no debugging output
  510. * 1 : 0+output errors only
  511. * 2 : 1+output all sensekey/asc
  512. * 3 : 2+follow all chrdev related procedures
  513. * 4 : 3+follow all procedures
  514. * 5 : 4+include pc_stack rq_stack info
  515. * 6 : 5+USE_COUNT updates
  516. */
  517. int debug_level;
  518. } idetape_tape_t;
  519. static DEFINE_MUTEX(idetape_ref_mutex);
  520. static struct class *idetape_sysfs_class;
  521. #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
  522. #define ide_tape_g(disk) \
  523. container_of((disk)->private_data, struct ide_tape_obj, driver)
  524. static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
  525. {
  526. struct ide_tape_obj *tape = NULL;
  527. mutex_lock(&idetape_ref_mutex);
  528. tape = ide_tape_g(disk);
  529. if (tape)
  530. kref_get(&tape->kref);
  531. mutex_unlock(&idetape_ref_mutex);
  532. return tape;
  533. }
  534. static void ide_tape_release(struct kref *);
  535. static void ide_tape_put(struct ide_tape_obj *tape)
  536. {
  537. mutex_lock(&idetape_ref_mutex);
  538. kref_put(&tape->kref, ide_tape_release);
  539. mutex_unlock(&idetape_ref_mutex);
  540. }
  541. /*
  542. * Tape door status
  543. */
  544. #define DOOR_UNLOCKED 0
  545. #define DOOR_LOCKED 1
  546. #define DOOR_EXPLICITLY_LOCKED 2
  547. /*
  548. * Tape flag bits values.
  549. */
  550. #define IDETAPE_IGNORE_DSC 0
  551. #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
  552. #define IDETAPE_BUSY 2 /* Device already opened */
  553. #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
  554. #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
  555. #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
  556. #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
  557. #define IDETAPE_READ_ERROR 7
  558. #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
  559. /* 0 = no tape is loaded, so we don't rewind after ejecting */
  560. #define IDETAPE_MEDIUM_PRESENT 9
  561. /*
  562. * Supported ATAPI tape drives packet commands
  563. */
  564. #define IDETAPE_TEST_UNIT_READY_CMD 0x00
  565. #define IDETAPE_REWIND_CMD 0x01
  566. #define IDETAPE_REQUEST_SENSE_CMD 0x03
  567. #define IDETAPE_READ_CMD 0x08
  568. #define IDETAPE_WRITE_CMD 0x0a
  569. #define IDETAPE_WRITE_FILEMARK_CMD 0x10
  570. #define IDETAPE_SPACE_CMD 0x11
  571. #define IDETAPE_INQUIRY_CMD 0x12
  572. #define IDETAPE_ERASE_CMD 0x19
  573. #define IDETAPE_MODE_SENSE_CMD 0x1a
  574. #define IDETAPE_MODE_SELECT_CMD 0x15
  575. #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
  576. #define IDETAPE_PREVENT_CMD 0x1e
  577. #define IDETAPE_LOCATE_CMD 0x2b
  578. #define IDETAPE_READ_POSITION_CMD 0x34
  579. #define IDETAPE_READ_BUFFER_CMD 0x3c
  580. #define IDETAPE_SET_SPEED_CMD 0xbb
  581. /*
  582. * Some defines for the READ BUFFER command
  583. */
  584. #define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
  585. /*
  586. * Some defines for the SPACE command
  587. */
  588. #define IDETAPE_SPACE_OVER_FILEMARK 1
  589. #define IDETAPE_SPACE_TO_EOD 3
  590. /*
  591. * Some defines for the LOAD UNLOAD command
  592. */
  593. #define IDETAPE_LU_LOAD_MASK 1
  594. #define IDETAPE_LU_RETENSION_MASK 2
  595. #define IDETAPE_LU_EOT_MASK 4
  596. /*
  597. * Special requests for our block device strategy routine.
  598. *
  599. * In order to service a character device command, we add special
  600. * requests to the tail of our block device request queue and wait
  601. * for their completion.
  602. */
  603. enum {
  604. REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
  605. REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
  606. REQ_IDETAPE_READ = (1 << 2),
  607. REQ_IDETAPE_WRITE = (1 << 3),
  608. REQ_IDETAPE_READ_BUFFER = (1 << 4),
  609. };
  610. /*
  611. * Error codes which are returned in rq->errors to the higher part
  612. * of the driver.
  613. */
  614. #define IDETAPE_ERROR_GENERAL 101
  615. #define IDETAPE_ERROR_FILEMARK 102
  616. #define IDETAPE_ERROR_EOD 103
  617. /*
  618. * The following is used to format the general configuration word of
  619. * the ATAPI IDENTIFY DEVICE command.
  620. */
  621. struct idetape_id_gcw {
  622. unsigned packet_size :2; /* Packet Size */
  623. unsigned reserved234 :3; /* Reserved */
  624. unsigned drq_type :2; /* Command packet DRQ type */
  625. unsigned removable :1; /* Removable media */
  626. unsigned device_type :5; /* Device type */
  627. unsigned reserved13 :1; /* Reserved */
  628. unsigned protocol :2; /* Protocol type */
  629. };
  630. /*
  631. * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
  632. */
  633. typedef struct {
  634. unsigned device_type :5; /* Peripheral Device Type */
  635. unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
  636. unsigned reserved1_6t0 :7; /* Reserved */
  637. unsigned rmb :1; /* Removable Medium Bit */
  638. unsigned ansi_version :3; /* ANSI Version */
  639. unsigned ecma_version :3; /* ECMA Version */
  640. unsigned iso_version :2; /* ISO Version */
  641. unsigned response_format :4; /* Response Data Format */
  642. unsigned reserved3_45 :2; /* Reserved */
  643. unsigned reserved3_6 :1; /* TrmIOP - Reserved */
  644. unsigned reserved3_7 :1; /* AENC - Reserved */
  645. __u8 additional_length; /* Additional Length (total_length-4) */
  646. __u8 rsv5, rsv6, rsv7; /* Reserved */
  647. __u8 vendor_id[8]; /* Vendor Identification */
  648. __u8 product_id[16]; /* Product Identification */
  649. __u8 revision_level[4]; /* Revision Level */
  650. __u8 vendor_specific[20]; /* Vendor Specific - Optional */
  651. __u8 reserved56t95[40]; /* Reserved - Optional */
  652. /* Additional information may be returned */
  653. } idetape_inquiry_result_t;
  654. /*
  655. * READ POSITION packet command - Data Format (From Table 6-57)
  656. */
  657. typedef struct {
  658. unsigned reserved0_10 :2; /* Reserved */
  659. unsigned bpu :1; /* Block Position Unknown */
  660. unsigned reserved0_543 :3; /* Reserved */
  661. unsigned eop :1; /* End Of Partition */
  662. unsigned bop :1; /* Beginning Of Partition */
  663. u8 partition; /* Partition Number */
  664. u8 reserved2, reserved3; /* Reserved */
  665. u32 first_block; /* First Block Location */
  666. u32 last_block; /* Last Block Location (Optional) */
  667. u8 reserved12; /* Reserved */
  668. u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
  669. u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
  670. } idetape_read_position_result_t;
  671. /*
  672. * Follows structures which are related to the SELECT SENSE / MODE SENSE
  673. * packet commands. Those packet commands are still not supported
  674. * by ide-tape.
  675. */
  676. #define IDETAPE_BLOCK_DESCRIPTOR 0
  677. #define IDETAPE_CAPABILITIES_PAGE 0x2a
  678. #define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */
  679. #define IDETAPE_BLOCK_SIZE_PAGE 0x30
  680. #define IDETAPE_BUFFER_FILLING_PAGE 0x33
  681. /*
  682. * Mode Parameter Block Descriptor the MODE SENSE packet command
  683. *
  684. * Support for block descriptors is optional.
  685. */
  686. typedef struct {
  687. __u8 density_code; /* Medium density code */
  688. __u8 blocks[3]; /* Number of blocks */
  689. __u8 reserved4; /* Reserved */
  690. __u8 length[3]; /* Block Length */
  691. } idetape_parameter_block_descriptor_t;
  692. /*
  693. * The Data Compression Page, as returned by the MODE SENSE packet command.
  694. */
  695. typedef struct {
  696. unsigned page_code :6; /* Page Code - Should be 0xf */
  697. unsigned reserved0 :1; /* Reserved */
  698. unsigned ps :1;
  699. __u8 page_length; /* Page Length - Should be 14 */
  700. unsigned reserved2 :6; /* Reserved */
  701. unsigned dcc :1; /* Data Compression Capable */
  702. unsigned dce :1; /* Data Compression Enable */
  703. unsigned reserved3 :5; /* Reserved */
  704. unsigned red :2; /* Report Exception on Decompression */
  705. unsigned dde :1; /* Data Decompression Enable */
  706. __u32 ca; /* Compression Algorithm */
  707. __u32 da; /* Decompression Algorithm */
  708. __u8 reserved[4]; /* Reserved */
  709. } idetape_data_compression_page_t;
  710. /*
  711. * The Medium Partition Page, as returned by the MODE SENSE packet command.
  712. */
  713. typedef struct {
  714. unsigned page_code :6; /* Page Code - Should be 0x11 */
  715. unsigned reserved1_6 :1; /* Reserved */
  716. unsigned ps :1;
  717. __u8 page_length; /* Page Length - Should be 6 */
  718. __u8 map; /* Maximum Additional Partitions - Should be 0 */
  719. __u8 apd; /* Additional Partitions Defined - Should be 0 */
  720. unsigned reserved4_012 :3; /* Reserved */
  721. unsigned psum :2; /* Should be 0 */
  722. unsigned idp :1; /* Should be 0 */
  723. unsigned sdp :1; /* Should be 0 */
  724. unsigned fdp :1; /* Fixed Data Partitions */
  725. __u8 mfr; /* Medium Format Recognition */
  726. __u8 reserved[2]; /* Reserved */
  727. } idetape_medium_partition_page_t;
  728. /*
  729. * Run time configurable parameters.
  730. */
  731. typedef struct {
  732. int dsc_rw_frequency;
  733. int dsc_media_access_frequency;
  734. int nr_stages;
  735. } idetape_config_t;
  736. /*
  737. * The variables below are used for the character device interface.
  738. * Additional state variables are defined in our ide_drive_t structure.
  739. */
  740. static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
  741. #define ide_tape_f(file) ((file)->private_data)
  742. static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
  743. {
  744. struct ide_tape_obj *tape = NULL;
  745. mutex_lock(&idetape_ref_mutex);
  746. tape = idetape_devs[i];
  747. if (tape)
  748. kref_get(&tape->kref);
  749. mutex_unlock(&idetape_ref_mutex);
  750. return tape;
  751. }
  752. /*
  753. * Function declarations
  754. *
  755. */
  756. static int idetape_chrdev_release (struct inode *inode, struct file *filp);
  757. static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
  758. /*
  759. * Too bad. The drive wants to send us data which we are not ready to accept.
  760. * Just throw it away.
  761. */
  762. static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
  763. {
  764. while (bcount--)
  765. (void) HWIF(drive)->INB(IDE_DATA_REG);
  766. }
  767. static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
  768. {
  769. struct idetape_bh *bh = pc->bh;
  770. int count;
  771. while (bcount) {
  772. #if IDETAPE_DEBUG_BUGS
  773. if (bh == NULL) {
  774. printk(KERN_ERR "ide-tape: bh == NULL in "
  775. "idetape_input_buffers\n");
  776. idetape_discard_data(drive, bcount);
  777. return;
  778. }
  779. #endif /* IDETAPE_DEBUG_BUGS */
  780. count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
  781. HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
  782. bcount -= count;
  783. atomic_add(count, &bh->b_count);
  784. if (atomic_read(&bh->b_count) == bh->b_size) {
  785. bh = bh->b_reqnext;
  786. if (bh)
  787. atomic_set(&bh->b_count, 0);
  788. }
  789. }
  790. pc->bh = bh;
  791. }
  792. static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
  793. {
  794. struct idetape_bh *bh = pc->bh;
  795. int count;
  796. while (bcount) {
  797. #if IDETAPE_DEBUG_BUGS
  798. if (bh == NULL) {
  799. printk(KERN_ERR "ide-tape: bh == NULL in "
  800. "idetape_output_buffers\n");
  801. return;
  802. }
  803. #endif /* IDETAPE_DEBUG_BUGS */
  804. count = min((unsigned int)pc->b_count, (unsigned int)bcount);
  805. HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
  806. bcount -= count;
  807. pc->b_data += count;
  808. pc->b_count -= count;
  809. if (!pc->b_count) {
  810. pc->bh = bh = bh->b_reqnext;
  811. if (bh) {
  812. pc->b_data = bh->b_data;
  813. pc->b_count = atomic_read(&bh->b_count);
  814. }
  815. }
  816. }
  817. }
  818. static void idetape_update_buffers (idetape_pc_t *pc)
  819. {
  820. struct idetape_bh *bh = pc->bh;
  821. int count;
  822. unsigned int bcount = pc->actually_transferred;
  823. if (test_bit(PC_WRITING, &pc->flags))
  824. return;
  825. while (bcount) {
  826. #if IDETAPE_DEBUG_BUGS
  827. if (bh == NULL) {
  828. printk(KERN_ERR "ide-tape: bh == NULL in "
  829. "idetape_update_buffers\n");
  830. return;
  831. }
  832. #endif /* IDETAPE_DEBUG_BUGS */
  833. count = min((unsigned int)bh->b_size, (unsigned int)bcount);
  834. atomic_set(&bh->b_count, count);
  835. if (atomic_read(&bh->b_count) == bh->b_size)
  836. bh = bh->b_reqnext;
  837. bcount -= count;
  838. }
  839. pc->bh = bh;
  840. }
  841. /*
  842. * idetape_next_pc_storage returns a pointer to a place in which we can
  843. * safely store a packet command, even though we intend to leave the
  844. * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
  845. * commands is allocated at initialization time.
  846. */
  847. static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
  848. {
  849. idetape_tape_t *tape = drive->driver_data;
  850. #if IDETAPE_DEBUG_LOG
  851. if (tape->debug_level >= 5)
  852. printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
  853. tape->pc_stack_index);
  854. #endif /* IDETAPE_DEBUG_LOG */
  855. if (tape->pc_stack_index == IDETAPE_PC_STACK)
  856. tape->pc_stack_index=0;
  857. return (&tape->pc_stack[tape->pc_stack_index++]);
  858. }
  859. /*
  860. * idetape_next_rq_storage is used along with idetape_next_pc_storage.
  861. * Since we queue packet commands in the request queue, we need to
  862. * allocate a request, along with the allocation of a packet command.
  863. */
  864. /**************************************************************
  865. * *
  866. * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
  867. * followed later on by kfree(). -ml *
  868. * *
  869. **************************************************************/
  870. static struct request *idetape_next_rq_storage (ide_drive_t *drive)
  871. {
  872. idetape_tape_t *tape = drive->driver_data;
  873. #if IDETAPE_DEBUG_LOG
  874. if (tape->debug_level >= 5)
  875. printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
  876. tape->rq_stack_index);
  877. #endif /* IDETAPE_DEBUG_LOG */
  878. if (tape->rq_stack_index == IDETAPE_PC_STACK)
  879. tape->rq_stack_index=0;
  880. return (&tape->rq_stack[tape->rq_stack_index++]);
  881. }
  882. /*
  883. * idetape_init_pc initializes a packet command.
  884. */
  885. static void idetape_init_pc (idetape_pc_t *pc)
  886. {
  887. memset(pc->c, 0, 12);
  888. pc->retries = 0;
  889. pc->flags = 0;
  890. pc->request_transfer = 0;
  891. pc->buffer = pc->pc_buffer;
  892. pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
  893. pc->bh = NULL;
  894. pc->b_data = NULL;
  895. }
  896. /*
  897. * called on each failed packet command retry to analyze the request sense. We
  898. * currently do not utilize this information.
  899. */
  900. static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
  901. {
  902. idetape_tape_t *tape = drive->driver_data;
  903. idetape_pc_t *pc = tape->failed_pc;
  904. tape->sense_key = sense[2] & 0xF;
  905. tape->asc = sense[12];
  906. tape->ascq = sense[13];
  907. #if IDETAPE_DEBUG_LOG
  908. /*
  909. * Without debugging, we only log an error if we decided to give up
  910. * retrying.
  911. */
  912. if (tape->debug_level >= 1)
  913. printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
  914. "asc = %x, ascq = %x\n",
  915. pc->c[0], tape->sense_key,
  916. tape->asc, tape->ascq);
  917. #endif /* IDETAPE_DEBUG_LOG */
  918. /* Correct pc->actually_transferred by asking the tape. */
  919. if (test_bit(PC_DMA_ERROR, &pc->flags)) {
  920. pc->actually_transferred = pc->request_transfer -
  921. tape->tape_block_size *
  922. ntohl(get_unaligned((u32 *)&sense[3]));
  923. idetape_update_buffers(pc);
  924. }
  925. /*
  926. * If error was the result of a zero-length read or write command,
  927. * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
  928. * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
  929. */
  930. if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
  931. /* length == 0 */
  932. && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
  933. if (tape->sense_key == 5) {
  934. /* don't report an error, everything's ok */
  935. pc->error = 0;
  936. /* don't retry read/write */
  937. set_bit(PC_ABORT, &pc->flags);
  938. }
  939. }
  940. if (pc->c[0] == IDETAPE_READ_CMD && (sense[2] & 0x80)) {
  941. pc->error = IDETAPE_ERROR_FILEMARK;
  942. set_bit(PC_ABORT, &pc->flags);
  943. }
  944. if (pc->c[0] == IDETAPE_WRITE_CMD) {
  945. if ((sense[2] & 0x40) || (tape->sense_key == 0xd
  946. && tape->asc == 0x0 && tape->ascq == 0x2)) {
  947. pc->error = IDETAPE_ERROR_EOD;
  948. set_bit(PC_ABORT, &pc->flags);
  949. }
  950. }
  951. if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
  952. if (tape->sense_key == 8) {
  953. pc->error = IDETAPE_ERROR_EOD;
  954. set_bit(PC_ABORT, &pc->flags);
  955. }
  956. if (!test_bit(PC_ABORT, &pc->flags) &&
  957. pc->actually_transferred)
  958. pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
  959. }
  960. }
  961. /*
  962. * idetape_active_next_stage will declare the next stage as "active".
  963. */
  964. static void idetape_active_next_stage (ide_drive_t *drive)
  965. {
  966. idetape_tape_t *tape = drive->driver_data;
  967. idetape_stage_t *stage = tape->next_stage;
  968. struct request *rq = &stage->rq;
  969. #if IDETAPE_DEBUG_LOG
  970. if (tape->debug_level >= 4)
  971. printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
  972. #endif /* IDETAPE_DEBUG_LOG */
  973. #if IDETAPE_DEBUG_BUGS
  974. if (stage == NULL) {
  975. printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
  976. return;
  977. }
  978. #endif /* IDETAPE_DEBUG_BUGS */
  979. rq->rq_disk = tape->disk;
  980. rq->buffer = NULL;
  981. rq->special = (void *)stage->bh;
  982. tape->active_data_request = rq;
  983. tape->active_stage = stage;
  984. tape->next_stage = stage->next;
  985. }
  986. /*
  987. * idetape_increase_max_pipeline_stages is a part of the feedback
  988. * loop which tries to find the optimum number of stages. In the
  989. * feedback loop, we are starting from a minimum maximum number of
  990. * stages, and if we sense that the pipeline is empty, we try to
  991. * increase it, until we reach the user compile time memory limit.
  992. */
  993. static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
  994. {
  995. idetape_tape_t *tape = drive->driver_data;
  996. int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
  997. #if IDETAPE_DEBUG_LOG
  998. if (tape->debug_level >= 4)
  999. printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
  1000. #endif /* IDETAPE_DEBUG_LOG */
  1001. tape->max_stages += max(increase, 1);
  1002. tape->max_stages = max(tape->max_stages, tape->min_pipeline);
  1003. tape->max_stages = min(tape->max_stages, tape->max_pipeline);
  1004. }
  1005. /*
  1006. * idetape_kfree_stage calls kfree to completely free a stage, along with
  1007. * its related buffers.
  1008. */
  1009. static void __idetape_kfree_stage (idetape_stage_t *stage)
  1010. {
  1011. struct idetape_bh *prev_bh, *bh = stage->bh;
  1012. int size;
  1013. while (bh != NULL) {
  1014. if (bh->b_data != NULL) {
  1015. size = (int) bh->b_size;
  1016. while (size > 0) {
  1017. free_page((unsigned long) bh->b_data);
  1018. size -= PAGE_SIZE;
  1019. bh->b_data += PAGE_SIZE;
  1020. }
  1021. }
  1022. prev_bh = bh;
  1023. bh = bh->b_reqnext;
  1024. kfree(prev_bh);
  1025. }
  1026. kfree(stage);
  1027. }
  1028. static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
  1029. {
  1030. __idetape_kfree_stage(stage);
  1031. }
  1032. /*
  1033. * idetape_remove_stage_head removes tape->first_stage from the pipeline.
  1034. * The caller should avoid race conditions.
  1035. */
  1036. static void idetape_remove_stage_head (ide_drive_t *drive)
  1037. {
  1038. idetape_tape_t *tape = drive->driver_data;
  1039. idetape_stage_t *stage;
  1040. #if IDETAPE_DEBUG_LOG
  1041. if (tape->debug_level >= 4)
  1042. printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
  1043. #endif /* IDETAPE_DEBUG_LOG */
  1044. #if IDETAPE_DEBUG_BUGS
  1045. if (tape->first_stage == NULL) {
  1046. printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
  1047. return;
  1048. }
  1049. if (tape->active_stage == tape->first_stage) {
  1050. printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
  1051. return;
  1052. }
  1053. #endif /* IDETAPE_DEBUG_BUGS */
  1054. stage = tape->first_stage;
  1055. tape->first_stage = stage->next;
  1056. idetape_kfree_stage(tape, stage);
  1057. tape->nr_stages--;
  1058. if (tape->first_stage == NULL) {
  1059. tape->last_stage = NULL;
  1060. #if IDETAPE_DEBUG_BUGS
  1061. if (tape->next_stage != NULL)
  1062. printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
  1063. if (tape->nr_stages)
  1064. printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
  1065. #endif /* IDETAPE_DEBUG_BUGS */
  1066. }
  1067. }
  1068. /*
  1069. * This will free all the pipeline stages starting from new_last_stage->next
  1070. * to the end of the list, and point tape->last_stage to new_last_stage.
  1071. */
  1072. static void idetape_abort_pipeline(ide_drive_t *drive,
  1073. idetape_stage_t *new_last_stage)
  1074. {
  1075. idetape_tape_t *tape = drive->driver_data;
  1076. idetape_stage_t *stage = new_last_stage->next;
  1077. idetape_stage_t *nstage;
  1078. #if IDETAPE_DEBUG_LOG
  1079. if (tape->debug_level >= 4)
  1080. printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
  1081. #endif
  1082. while (stage) {
  1083. nstage = stage->next;
  1084. idetape_kfree_stage(tape, stage);
  1085. --tape->nr_stages;
  1086. --tape->nr_pending_stages;
  1087. stage = nstage;
  1088. }
  1089. if (new_last_stage)
  1090. new_last_stage->next = NULL;
  1091. tape->last_stage = new_last_stage;
  1092. tape->next_stage = NULL;
  1093. }
  1094. /*
  1095. * idetape_end_request is used to finish servicing a request, and to
  1096. * insert a pending pipeline request into the main device queue.
  1097. */
  1098. static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
  1099. {
  1100. struct request *rq = HWGROUP(drive)->rq;
  1101. idetape_tape_t *tape = drive->driver_data;
  1102. unsigned long flags;
  1103. int error;
  1104. int remove_stage = 0;
  1105. idetape_stage_t *active_stage;
  1106. #if IDETAPE_DEBUG_LOG
  1107. if (tape->debug_level >= 4)
  1108. printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
  1109. #endif /* IDETAPE_DEBUG_LOG */
  1110. switch (uptodate) {
  1111. case 0: error = IDETAPE_ERROR_GENERAL; break;
  1112. case 1: error = 0; break;
  1113. default: error = uptodate;
  1114. }
  1115. rq->errors = error;
  1116. if (error)
  1117. tape->failed_pc = NULL;
  1118. if (!blk_special_request(rq)) {
  1119. ide_end_request(drive, uptodate, nr_sects);
  1120. return 0;
  1121. }
  1122. spin_lock_irqsave(&tape->spinlock, flags);
  1123. /* The request was a pipelined data transfer request */
  1124. if (tape->active_data_request == rq) {
  1125. active_stage = tape->active_stage;
  1126. tape->active_stage = NULL;
  1127. tape->active_data_request = NULL;
  1128. tape->nr_pending_stages--;
  1129. if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
  1130. remove_stage = 1;
  1131. if (error) {
  1132. set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  1133. if (error == IDETAPE_ERROR_EOD)
  1134. idetape_abort_pipeline(drive, active_stage);
  1135. }
  1136. } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
  1137. if (error == IDETAPE_ERROR_EOD) {
  1138. set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  1139. idetape_abort_pipeline(drive, active_stage);
  1140. }
  1141. }
  1142. if (tape->next_stage != NULL) {
  1143. idetape_active_next_stage(drive);
  1144. /*
  1145. * Insert the next request into the request queue.
  1146. */
  1147. (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
  1148. } else if (!error) {
  1149. idetape_increase_max_pipeline_stages(drive);
  1150. }
  1151. }
  1152. ide_end_drive_cmd(drive, 0, 0);
  1153. // blkdev_dequeue_request(rq);
  1154. // drive->rq = NULL;
  1155. // end_that_request_last(rq);
  1156. if (remove_stage)
  1157. idetape_remove_stage_head(drive);
  1158. if (tape->active_data_request == NULL)
  1159. clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  1160. spin_unlock_irqrestore(&tape->spinlock, flags);
  1161. return 0;
  1162. }
  1163. static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
  1164. {
  1165. idetape_tape_t *tape = drive->driver_data;
  1166. #if IDETAPE_DEBUG_LOG
  1167. if (tape->debug_level >= 4)
  1168. printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
  1169. #endif /* IDETAPE_DEBUG_LOG */
  1170. if (!tape->pc->error) {
  1171. idetape_analyze_error(drive, tape->pc->buffer);
  1172. idetape_end_request(drive, 1, 0);
  1173. } else {
  1174. printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
  1175. idetape_end_request(drive, 0, 0);
  1176. }
  1177. return ide_stopped;
  1178. }
  1179. static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
  1180. {
  1181. idetape_init_pc(pc);
  1182. pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
  1183. pc->c[4] = 20;
  1184. pc->request_transfer = 20;
  1185. pc->callback = &idetape_request_sense_callback;
  1186. }
  1187. static void idetape_init_rq(struct request *rq, u8 cmd)
  1188. {
  1189. memset(rq, 0, sizeof(*rq));
  1190. rq->cmd_type = REQ_TYPE_SPECIAL;
  1191. rq->cmd[0] = cmd;
  1192. }
  1193. /*
  1194. * idetape_queue_pc_head generates a new packet command request in front
  1195. * of the request queue, before the current request, so that it will be
  1196. * processed immediately, on the next pass through the driver.
  1197. *
  1198. * idetape_queue_pc_head is called from the request handling part of
  1199. * the driver (the "bottom" part). Safe storage for the request should
  1200. * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
  1201. * before calling idetape_queue_pc_head.
  1202. *
  1203. * Memory for those requests is pre-allocated at initialization time, and
  1204. * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
  1205. * space for the maximum possible number of inter-dependent packet commands.
  1206. *
  1207. * The higher level of the driver - The ioctl handler and the character
  1208. * device handling functions should queue request to the lower level part
  1209. * and wait for their completion using idetape_queue_pc_tail or
  1210. * idetape_queue_rw_tail.
  1211. */
  1212. static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
  1213. {
  1214. struct ide_tape_obj *tape = drive->driver_data;
  1215. idetape_init_rq(rq, REQ_IDETAPE_PC1);
  1216. rq->buffer = (char *) pc;
  1217. rq->rq_disk = tape->disk;
  1218. (void) ide_do_drive_cmd(drive, rq, ide_preempt);
  1219. }
  1220. /*
  1221. * idetape_retry_pc is called when an error was detected during the
  1222. * last packet command. We queue a request sense packet command in
  1223. * the head of the request list.
  1224. */
  1225. static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
  1226. {
  1227. idetape_tape_t *tape = drive->driver_data;
  1228. idetape_pc_t *pc;
  1229. struct request *rq;
  1230. (void)drive->hwif->INB(IDE_ERROR_REG);
  1231. pc = idetape_next_pc_storage(drive);
  1232. rq = idetape_next_rq_storage(drive);
  1233. idetape_create_request_sense_cmd(pc);
  1234. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  1235. idetape_queue_pc_head(drive, pc, rq);
  1236. return ide_stopped;
  1237. }
  1238. /*
  1239. * idetape_postpone_request postpones the current request so that
  1240. * ide.c will be able to service requests from another device on
  1241. * the same hwgroup while we are polling for DSC.
  1242. */
  1243. static void idetape_postpone_request (ide_drive_t *drive)
  1244. {
  1245. idetape_tape_t *tape = drive->driver_data;
  1246. #if IDETAPE_DEBUG_LOG
  1247. if (tape->debug_level >= 4)
  1248. printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
  1249. #endif
  1250. tape->postponed_rq = HWGROUP(drive)->rq;
  1251. ide_stall_queue(drive, tape->dsc_polling_frequency);
  1252. }
  1253. /*
  1254. * idetape_pc_intr is the usual interrupt handler which will be called
  1255. * during a packet command. We will transfer some of the data (as
  1256. * requested by the drive) and will re-point interrupt handler to us.
  1257. * When data transfer is finished, we will act according to the
  1258. * algorithm described before idetape_issue_packet_command.
  1259. *
  1260. */
  1261. static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
  1262. {
  1263. ide_hwif_t *hwif = drive->hwif;
  1264. idetape_tape_t *tape = drive->driver_data;
  1265. idetape_pc_t *pc = tape->pc;
  1266. unsigned int temp;
  1267. #if SIMULATE_ERRORS
  1268. static int error_sim_count = 0;
  1269. #endif
  1270. u16 bcount;
  1271. u8 stat, ireason;
  1272. #if IDETAPE_DEBUG_LOG
  1273. if (tape->debug_level >= 4)
  1274. printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
  1275. "interrupt handler\n");
  1276. #endif /* IDETAPE_DEBUG_LOG */
  1277. /* Clear the interrupt */
  1278. stat = hwif->INB(IDE_STATUS_REG);
  1279. if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  1280. if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
  1281. /*
  1282. * A DMA error is sometimes expected. For example,
  1283. * if the tape is crossing a filemark during a
  1284. * READ command, it will issue an irq and position
  1285. * itself before the filemark, so that only a partial
  1286. * data transfer will occur (which causes the DMA
  1287. * error). In that case, we will later ask the tape
  1288. * how much bytes of the original request were
  1289. * actually transferred (we can't receive that
  1290. * information from the DMA engine on most chipsets).
  1291. */
  1292. /*
  1293. * On the contrary, a DMA error is never expected;
  1294. * it usually indicates a hardware error or abort.
  1295. * If the tape crosses a filemark during a READ
  1296. * command, it will issue an irq and position itself
  1297. * after the filemark (not before). Only a partial
  1298. * data transfer will occur, but no DMA error.
  1299. * (AS, 19 Apr 2001)
  1300. */
  1301. set_bit(PC_DMA_ERROR, &pc->flags);
  1302. } else {
  1303. pc->actually_transferred = pc->request_transfer;
  1304. idetape_update_buffers(pc);
  1305. }
  1306. #if IDETAPE_DEBUG_LOG
  1307. if (tape->debug_level >= 4)
  1308. printk(KERN_INFO "ide-tape: DMA finished\n");
  1309. #endif /* IDETAPE_DEBUG_LOG */
  1310. }
  1311. /* No more interrupts */
  1312. if ((stat & DRQ_STAT) == 0) {
  1313. #if IDETAPE_DEBUG_LOG
  1314. if (tape->debug_level >= 2)
  1315. printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
  1316. #endif /* IDETAPE_DEBUG_LOG */
  1317. clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  1318. local_irq_enable();
  1319. #if SIMULATE_ERRORS
  1320. if ((pc->c[0] == IDETAPE_WRITE_CMD ||
  1321. pc->c[0] == IDETAPE_READ_CMD) &&
  1322. (++error_sim_count % 100) == 0) {
  1323. printk(KERN_INFO "ide-tape: %s: simulating error\n",
  1324. tape->name);
  1325. stat |= ERR_STAT;
  1326. }
  1327. #endif
  1328. if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
  1329. stat &= ~ERR_STAT;
  1330. if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
  1331. /* Error detected */
  1332. #if IDETAPE_DEBUG_LOG
  1333. if (tape->debug_level >= 1)
  1334. printk(KERN_INFO "ide-tape: %s: I/O error\n",
  1335. tape->name);
  1336. #endif /* IDETAPE_DEBUG_LOG */
  1337. if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  1338. printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
  1339. return ide_do_reset(drive);
  1340. }
  1341. #if IDETAPE_DEBUG_LOG
  1342. if (tape->debug_level >= 1)
  1343. printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
  1344. #endif
  1345. /* Retry operation */
  1346. return idetape_retry_pc(drive);
  1347. }
  1348. pc->error = 0;
  1349. if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
  1350. (stat & SEEK_STAT) == 0) {
  1351. /* Media access command */
  1352. tape->dsc_polling_start = jiffies;
  1353. tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
  1354. tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
  1355. /* Allow ide.c to handle other requests */
  1356. idetape_postpone_request(drive);
  1357. return ide_stopped;
  1358. }
  1359. if (tape->failed_pc == pc)
  1360. tape->failed_pc = NULL;
  1361. /* Command finished - Call the callback function */
  1362. return pc->callback(drive);
  1363. }
  1364. if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  1365. printk(KERN_ERR "ide-tape: The tape wants to issue more "
  1366. "interrupts in DMA mode\n");
  1367. printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
  1368. ide_dma_off(drive);
  1369. return ide_do_reset(drive);
  1370. }
  1371. /* Get the number of bytes to transfer on this interrupt. */
  1372. bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
  1373. hwif->INB(IDE_BCOUNTL_REG);
  1374. ireason = hwif->INB(IDE_IREASON_REG);
  1375. if (ireason & CD) {
  1376. printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
  1377. return ide_do_reset(drive);
  1378. }
  1379. if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
  1380. /* Hopefully, we will never get here */
  1381. printk(KERN_ERR "ide-tape: We wanted to %s, ",
  1382. (ireason & IO) ? "Write" : "Read");
  1383. printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
  1384. (ireason & IO) ? "Read" : "Write");
  1385. return ide_do_reset(drive);
  1386. }
  1387. if (!test_bit(PC_WRITING, &pc->flags)) {
  1388. /* Reading - Check that we have enough space */
  1389. temp = pc->actually_transferred + bcount;
  1390. if (temp > pc->request_transfer) {
  1391. if (temp > pc->buffer_size) {
  1392. printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
  1393. idetape_discard_data(drive, bcount);
  1394. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  1395. return ide_started;
  1396. }
  1397. #if IDETAPE_DEBUG_LOG
  1398. if (tape->debug_level >= 2)
  1399. printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
  1400. #endif /* IDETAPE_DEBUG_LOG */
  1401. }
  1402. }
  1403. if (test_bit(PC_WRITING, &pc->flags)) {
  1404. if (pc->bh != NULL)
  1405. idetape_output_buffers(drive, pc, bcount);
  1406. else
  1407. /* Write the current buffer */
  1408. hwif->atapi_output_bytes(drive, pc->current_position,
  1409. bcount);
  1410. } else {
  1411. if (pc->bh != NULL)
  1412. idetape_input_buffers(drive, pc, bcount);
  1413. else
  1414. /* Read the current buffer */
  1415. hwif->atapi_input_bytes(drive, pc->current_position,
  1416. bcount);
  1417. }
  1418. /* Update the current position */
  1419. pc->actually_transferred += bcount;
  1420. pc->current_position += bcount;
  1421. #if IDETAPE_DEBUG_LOG
  1422. if (tape->debug_level >= 2)
  1423. printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
  1424. "on that interrupt\n", pc->c[0], bcount);
  1425. #endif
  1426. /* And set the interrupt handler again */
  1427. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  1428. return ide_started;
  1429. }
  1430. /*
  1431. * Packet Command Interface
  1432. *
  1433. * The current Packet Command is available in tape->pc, and will not
  1434. * change until we finish handling it. Each packet command is associated
  1435. * with a callback function that will be called when the command is
  1436. * finished.
  1437. *
  1438. * The handling will be done in three stages:
  1439. *
  1440. * 1. idetape_issue_packet_command will send the packet command to the
  1441. * drive, and will set the interrupt handler to idetape_pc_intr.
  1442. *
  1443. * 2. On each interrupt, idetape_pc_intr will be called. This step
  1444. * will be repeated until the device signals us that no more
  1445. * interrupts will be issued.
  1446. *
  1447. * 3. ATAPI Tape media access commands have immediate status with a
  1448. * delayed process. In case of a successful initiation of a
  1449. * media access packet command, the DSC bit will be set when the
  1450. * actual execution of the command is finished.
  1451. * Since the tape drive will not issue an interrupt, we have to
  1452. * poll for this event. In this case, we define the request as
  1453. * "low priority request" by setting rq_status to
  1454. * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
  1455. * the driver.
  1456. *
  1457. * ide.c will then give higher priority to requests which
  1458. * originate from the other device, until will change rq_status
  1459. * to RQ_ACTIVE.
  1460. *
  1461. * 4. When the packet command is finished, it will be checked for errors.
  1462. *
  1463. * 5. In case an error was found, we queue a request sense packet
  1464. * command in front of the request queue and retry the operation
  1465. * up to IDETAPE_MAX_PC_RETRIES times.
  1466. *
  1467. * 6. In case no error was found, or we decided to give up and not
  1468. * to retry again, the callback function will be called and then
  1469. * we will handle the next request.
  1470. *
  1471. */
  1472. static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
  1473. {
  1474. ide_hwif_t *hwif = drive->hwif;
  1475. idetape_tape_t *tape = drive->driver_data;
  1476. idetape_pc_t *pc = tape->pc;
  1477. int retries = 100;
  1478. ide_startstop_t startstop;
  1479. u8 ireason;
  1480. if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
  1481. printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
  1482. return startstop;
  1483. }
  1484. ireason = hwif->INB(IDE_IREASON_REG);
  1485. while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
  1486. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
  1487. "a packet command, retrying\n");
  1488. udelay(100);
  1489. ireason = hwif->INB(IDE_IREASON_REG);
  1490. if (retries == 0) {
  1491. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
  1492. "issuing a packet command, ignoring\n");
  1493. ireason |= CD;
  1494. ireason &= ~IO;
  1495. }
  1496. }
  1497. if ((ireason & CD) == 0 || (ireason & IO)) {
  1498. printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
  1499. "a packet command\n");
  1500. return ide_do_reset(drive);
  1501. }
  1502. /* Set the interrupt routine */
  1503. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  1504. #ifdef CONFIG_BLK_DEV_IDEDMA
  1505. /* Begin DMA, if necessary */
  1506. if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
  1507. hwif->dma_start(drive);
  1508. #endif
  1509. /* Send the actual packet */
  1510. HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
  1511. return ide_started;
  1512. }
  1513. static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
  1514. {
  1515. ide_hwif_t *hwif = drive->hwif;
  1516. idetape_tape_t *tape = drive->driver_data;
  1517. int dma_ok = 0;
  1518. u16 bcount;
  1519. #if IDETAPE_DEBUG_BUGS
  1520. if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
  1521. pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  1522. printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
  1523. "Two request sense in serial were issued\n");
  1524. }
  1525. #endif /* IDETAPE_DEBUG_BUGS */
  1526. if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
  1527. tape->failed_pc = pc;
  1528. /* Set the current packet command */
  1529. tape->pc = pc;
  1530. if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
  1531. test_bit(PC_ABORT, &pc->flags)) {
  1532. /*
  1533. * We will "abort" retrying a packet command in case
  1534. * a legitimate error code was received (crossing a
  1535. * filemark, or end of the media, for example).
  1536. */
  1537. if (!test_bit(PC_ABORT, &pc->flags)) {
  1538. if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
  1539. tape->sense_key == 2 && tape->asc == 4 &&
  1540. (tape->ascq == 1 || tape->ascq == 8))) {
  1541. printk(KERN_ERR "ide-tape: %s: I/O error, "
  1542. "pc = %2x, key = %2x, "
  1543. "asc = %2x, ascq = %2x\n",
  1544. tape->name, pc->c[0],
  1545. tape->sense_key, tape->asc,
  1546. tape->ascq);
  1547. }
  1548. /* Giving up */
  1549. pc->error = IDETAPE_ERROR_GENERAL;
  1550. }
  1551. tape->failed_pc = NULL;
  1552. return pc->callback(drive);
  1553. }
  1554. #if IDETAPE_DEBUG_LOG
  1555. if (tape->debug_level >= 2)
  1556. printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
  1557. #endif /* IDETAPE_DEBUG_LOG */
  1558. pc->retries++;
  1559. /* We haven't transferred any data yet */
  1560. pc->actually_transferred = 0;
  1561. pc->current_position = pc->buffer;
  1562. /* Request to transfer the entire buffer at once */
  1563. bcount = pc->request_transfer;
  1564. if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
  1565. printk(KERN_WARNING "ide-tape: DMA disabled, "
  1566. "reverting to PIO\n");
  1567. ide_dma_off(drive);
  1568. }
  1569. if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
  1570. dma_ok = !hwif->dma_setup(drive);
  1571. ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
  1572. IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
  1573. if (dma_ok) /* Will begin DMA later */
  1574. set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  1575. if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
  1576. ide_execute_command(drive, WIN_PACKETCMD, &idetape_transfer_pc,
  1577. IDETAPE_WAIT_CMD, NULL);
  1578. return ide_started;
  1579. } else {
  1580. hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  1581. return idetape_transfer_pc(drive);
  1582. }
  1583. }
  1584. /*
  1585. * General packet command callback function.
  1586. */
  1587. static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
  1588. {
  1589. idetape_tape_t *tape = drive->driver_data;
  1590. #if IDETAPE_DEBUG_LOG
  1591. if (tape->debug_level >= 4)
  1592. printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
  1593. #endif /* IDETAPE_DEBUG_LOG */
  1594. idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
  1595. return ide_stopped;
  1596. }
  1597. /*
  1598. * A mode sense command is used to "sense" tape parameters.
  1599. */
  1600. static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
  1601. {
  1602. idetape_init_pc(pc);
  1603. pc->c[0] = IDETAPE_MODE_SENSE_CMD;
  1604. if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
  1605. pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
  1606. pc->c[2] = page_code;
  1607. /*
  1608. * Changed pc->c[3] to 0 (255 will at best return unused info).
  1609. *
  1610. * For SCSI this byte is defined as subpage instead of high byte
  1611. * of length and some IDE drives seem to interpret it this way
  1612. * and return an error when 255 is used.
  1613. */
  1614. pc->c[3] = 0;
  1615. pc->c[4] = 255; /* (We will just discard data in that case) */
  1616. if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
  1617. pc->request_transfer = 12;
  1618. else if (page_code == IDETAPE_CAPABILITIES_PAGE)
  1619. pc->request_transfer = 24;
  1620. else
  1621. pc->request_transfer = 50;
  1622. pc->callback = &idetape_pc_callback;
  1623. }
  1624. static void calculate_speeds(ide_drive_t *drive)
  1625. {
  1626. idetape_tape_t *tape = drive->driver_data;
  1627. int full = 125, empty = 75;
  1628. if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
  1629. tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
  1630. tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
  1631. tape->controlled_last_pipeline_head = tape->pipeline_head;
  1632. tape->controlled_pipeline_head_time = jiffies;
  1633. }
  1634. if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
  1635. tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
  1636. else if (time_after(jiffies, tape->controlled_previous_head_time))
  1637. tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
  1638. if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
  1639. /* -1 for read mode error recovery */
  1640. if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
  1641. tape->uncontrolled_pipeline_head_time = jiffies;
  1642. tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
  1643. }
  1644. } else {
  1645. tape->uncontrolled_previous_head_time = jiffies;
  1646. tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
  1647. if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
  1648. tape->uncontrolled_pipeline_head_time = jiffies;
  1649. }
  1650. }
  1651. tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
  1652. if (tape->speed_control == 0) {
  1653. tape->max_insert_speed = 5000;
  1654. } else if (tape->speed_control == 1) {
  1655. if (tape->nr_pending_stages >= tape->max_stages / 2)
  1656. tape->max_insert_speed = tape->pipeline_head_speed +
  1657. (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
  1658. else
  1659. tape->max_insert_speed = 500 +
  1660. (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
  1661. if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
  1662. tape->max_insert_speed = 5000;
  1663. } else if (tape->speed_control == 2) {
  1664. tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
  1665. (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
  1666. } else
  1667. tape->max_insert_speed = tape->speed_control;
  1668. tape->max_insert_speed = max(tape->max_insert_speed, 500);
  1669. }
  1670. static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
  1671. {
  1672. idetape_tape_t *tape = drive->driver_data;
  1673. idetape_pc_t *pc = tape->pc;
  1674. u8 stat;
  1675. stat = drive->hwif->INB(IDE_STATUS_REG);
  1676. if (stat & SEEK_STAT) {
  1677. if (stat & ERR_STAT) {
  1678. /* Error detected */
  1679. if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
  1680. printk(KERN_ERR "ide-tape: %s: I/O error, ",
  1681. tape->name);
  1682. /* Retry operation */
  1683. return idetape_retry_pc(drive);
  1684. }
  1685. pc->error = 0;
  1686. if (tape->failed_pc == pc)
  1687. tape->failed_pc = NULL;
  1688. } else {
  1689. pc->error = IDETAPE_ERROR_GENERAL;
  1690. tape->failed_pc = NULL;
  1691. }
  1692. return pc->callback(drive);
  1693. }
  1694. static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
  1695. {
  1696. idetape_tape_t *tape = drive->driver_data;
  1697. struct request *rq = HWGROUP(drive)->rq;
  1698. int blocks = tape->pc->actually_transferred / tape->tape_block_size;
  1699. tape->avg_size += blocks * tape->tape_block_size;
  1700. tape->insert_size += blocks * tape->tape_block_size;
  1701. if (tape->insert_size > 1024 * 1024)
  1702. tape->measure_insert_time = 1;
  1703. if (tape->measure_insert_time) {
  1704. tape->measure_insert_time = 0;
  1705. tape->insert_time = jiffies;
  1706. tape->insert_size = 0;
  1707. }
  1708. if (time_after(jiffies, tape->insert_time))
  1709. tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
  1710. if (time_after_eq(jiffies, tape->avg_time + HZ)) {
  1711. tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
  1712. tape->avg_size = 0;
  1713. tape->avg_time = jiffies;
  1714. }
  1715. #if IDETAPE_DEBUG_LOG
  1716. if (tape->debug_level >= 4)
  1717. printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
  1718. #endif /* IDETAPE_DEBUG_LOG */
  1719. tape->first_frame_position += blocks;
  1720. rq->current_nr_sectors -= blocks;
  1721. if (!tape->pc->error)
  1722. idetape_end_request(drive, 1, 0);
  1723. else
  1724. idetape_end_request(drive, tape->pc->error, 0);
  1725. return ide_stopped;
  1726. }
  1727. static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  1728. {
  1729. idetape_init_pc(pc);
  1730. pc->c[0] = IDETAPE_READ_CMD;
  1731. put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
  1732. pc->c[1] = 1;
  1733. pc->callback = &idetape_rw_callback;
  1734. pc->bh = bh;
  1735. atomic_set(&bh->b_count, 0);
  1736. pc->buffer = NULL;
  1737. pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
  1738. if (pc->request_transfer == tape->stage_size)
  1739. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  1740. }
  1741. static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  1742. {
  1743. int size = 32768;
  1744. struct idetape_bh *p = bh;
  1745. idetape_init_pc(pc);
  1746. pc->c[0] = IDETAPE_READ_BUFFER_CMD;
  1747. pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
  1748. pc->c[7] = size >> 8;
  1749. pc->c[8] = size & 0xff;
  1750. pc->callback = &idetape_pc_callback;
  1751. pc->bh = bh;
  1752. atomic_set(&bh->b_count, 0);
  1753. pc->buffer = NULL;
  1754. while (p) {
  1755. atomic_set(&p->b_count, 0);
  1756. p = p->b_reqnext;
  1757. }
  1758. pc->request_transfer = pc->buffer_size = size;
  1759. }
  1760. static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  1761. {
  1762. idetape_init_pc(pc);
  1763. pc->c[0] = IDETAPE_WRITE_CMD;
  1764. put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
  1765. pc->c[1] = 1;
  1766. pc->callback = &idetape_rw_callback;
  1767. set_bit(PC_WRITING, &pc->flags);
  1768. pc->bh = bh;
  1769. pc->b_data = bh->b_data;
  1770. pc->b_count = atomic_read(&bh->b_count);
  1771. pc->buffer = NULL;
  1772. pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
  1773. if (pc->request_transfer == tape->stage_size)
  1774. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  1775. }
  1776. /*
  1777. * idetape_do_request is our request handling function.
  1778. */
  1779. static ide_startstop_t idetape_do_request(ide_drive_t *drive,
  1780. struct request *rq, sector_t block)
  1781. {
  1782. idetape_tape_t *tape = drive->driver_data;
  1783. idetape_pc_t *pc = NULL;
  1784. struct request *postponed_rq = tape->postponed_rq;
  1785. u8 stat;
  1786. #if IDETAPE_DEBUG_LOG
  1787. if (tape->debug_level >= 2)
  1788. printk(KERN_INFO "ide-tape: sector: %ld, "
  1789. "nr_sectors: %ld, current_nr_sectors: %d\n",
  1790. rq->sector, rq->nr_sectors, rq->current_nr_sectors);
  1791. #endif /* IDETAPE_DEBUG_LOG */
  1792. if (!blk_special_request(rq)) {
  1793. /*
  1794. * We do not support buffer cache originated requests.
  1795. */
  1796. printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
  1797. "request queue (%d)\n", drive->name, rq->cmd_type);
  1798. ide_end_request(drive, 0, 0);
  1799. return ide_stopped;
  1800. }
  1801. /*
  1802. * Retry a failed packet command
  1803. */
  1804. if (tape->failed_pc != NULL &&
  1805. tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  1806. return idetape_issue_packet_command(drive, tape->failed_pc);
  1807. }
  1808. #if IDETAPE_DEBUG_BUGS
  1809. if (postponed_rq != NULL)
  1810. if (rq != postponed_rq) {
  1811. printk(KERN_ERR "ide-tape: ide-tape.c bug - "
  1812. "Two DSC requests were queued\n");
  1813. idetape_end_request(drive, 0, 0);
  1814. return ide_stopped;
  1815. }
  1816. #endif /* IDETAPE_DEBUG_BUGS */
  1817. tape->postponed_rq = NULL;
  1818. /*
  1819. * If the tape is still busy, postpone our request and service
  1820. * the other device meanwhile.
  1821. */
  1822. stat = drive->hwif->INB(IDE_STATUS_REG);
  1823. if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
  1824. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  1825. if (drive->post_reset == 1) {
  1826. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  1827. drive->post_reset = 0;
  1828. }
  1829. if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
  1830. tape->measure_insert_time = 1;
  1831. if (time_after(jiffies, tape->insert_time))
  1832. tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
  1833. calculate_speeds(drive);
  1834. if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
  1835. (stat & SEEK_STAT) == 0) {
  1836. if (postponed_rq == NULL) {
  1837. tape->dsc_polling_start = jiffies;
  1838. tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
  1839. tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
  1840. } else if (time_after(jiffies, tape->dsc_timeout)) {
  1841. printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
  1842. tape->name);
  1843. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  1844. idetape_media_access_finished(drive);
  1845. return ide_stopped;
  1846. } else {
  1847. return ide_do_reset(drive);
  1848. }
  1849. } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
  1850. tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
  1851. idetape_postpone_request(drive);
  1852. return ide_stopped;
  1853. }
  1854. if (rq->cmd[0] & REQ_IDETAPE_READ) {
  1855. tape->buffer_head++;
  1856. tape->postpone_cnt = 0;
  1857. pc = idetape_next_pc_storage(drive);
  1858. idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  1859. goto out;
  1860. }
  1861. if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
  1862. tape->buffer_head++;
  1863. tape->postpone_cnt = 0;
  1864. pc = idetape_next_pc_storage(drive);
  1865. idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  1866. goto out;
  1867. }
  1868. if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
  1869. tape->postpone_cnt = 0;
  1870. pc = idetape_next_pc_storage(drive);
  1871. idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  1872. goto out;
  1873. }
  1874. if (rq->cmd[0] & REQ_IDETAPE_PC1) {
  1875. pc = (idetape_pc_t *) rq->buffer;
  1876. rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
  1877. rq->cmd[0] |= REQ_IDETAPE_PC2;
  1878. goto out;
  1879. }
  1880. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  1881. idetape_media_access_finished(drive);
  1882. return ide_stopped;
  1883. }
  1884. BUG();
  1885. out:
  1886. return idetape_issue_packet_command(drive, pc);
  1887. }
  1888. /*
  1889. * Pipeline related functions
  1890. */
  1891. static inline int idetape_pipeline_active (idetape_tape_t *tape)
  1892. {
  1893. int rc1, rc2;
  1894. rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  1895. rc2 = (tape->active_data_request != NULL);
  1896. return rc1;
  1897. }
  1898. /*
  1899. * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
  1900. * stage, along with all the necessary small buffers which together make
  1901. * a buffer of size tape->stage_size (or a bit more). We attempt to
  1902. * combine sequential pages as much as possible.
  1903. *
  1904. * Returns a pointer to the new allocated stage, or NULL if we
  1905. * can't (or don't want to) allocate a stage.
  1906. *
  1907. * Pipeline stages are optional and are used to increase performance.
  1908. * If we can't allocate them, we'll manage without them.
  1909. */
  1910. static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
  1911. {
  1912. idetape_stage_t *stage;
  1913. struct idetape_bh *prev_bh, *bh;
  1914. int pages = tape->pages_per_stage;
  1915. char *b_data = NULL;
  1916. if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
  1917. return NULL;
  1918. stage->next = NULL;
  1919. bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  1920. if (bh == NULL)
  1921. goto abort;
  1922. bh->b_reqnext = NULL;
  1923. if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
  1924. goto abort;
  1925. if (clear)
  1926. memset(bh->b_data, 0, PAGE_SIZE);
  1927. bh->b_size = PAGE_SIZE;
  1928. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  1929. while (--pages) {
  1930. if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
  1931. goto abort;
  1932. if (clear)
  1933. memset(b_data, 0, PAGE_SIZE);
  1934. if (bh->b_data == b_data + PAGE_SIZE) {
  1935. bh->b_size += PAGE_SIZE;
  1936. bh->b_data -= PAGE_SIZE;
  1937. if (full)
  1938. atomic_add(PAGE_SIZE, &bh->b_count);
  1939. continue;
  1940. }
  1941. if (b_data == bh->b_data + bh->b_size) {
  1942. bh->b_size += PAGE_SIZE;
  1943. if (full)
  1944. atomic_add(PAGE_SIZE, &bh->b_count);
  1945. continue;
  1946. }
  1947. prev_bh = bh;
  1948. if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
  1949. free_page((unsigned long) b_data);
  1950. goto abort;
  1951. }
  1952. bh->b_reqnext = NULL;
  1953. bh->b_data = b_data;
  1954. bh->b_size = PAGE_SIZE;
  1955. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  1956. prev_bh->b_reqnext = bh;
  1957. }
  1958. bh->b_size -= tape->excess_bh_size;
  1959. if (full)
  1960. atomic_sub(tape->excess_bh_size, &bh->b_count);
  1961. return stage;
  1962. abort:
  1963. __idetape_kfree_stage(stage);
  1964. return NULL;
  1965. }
  1966. static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
  1967. {
  1968. idetape_stage_t *cache_stage = tape->cache_stage;
  1969. #if IDETAPE_DEBUG_LOG
  1970. if (tape->debug_level >= 4)
  1971. printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
  1972. #endif /* IDETAPE_DEBUG_LOG */
  1973. if (tape->nr_stages >= tape->max_stages)
  1974. return NULL;
  1975. if (cache_stage != NULL) {
  1976. tape->cache_stage = NULL;
  1977. return cache_stage;
  1978. }
  1979. return __idetape_kmalloc_stage(tape, 0, 0);
  1980. }
  1981. static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
  1982. {
  1983. struct idetape_bh *bh = tape->bh;
  1984. int count;
  1985. int ret = 0;
  1986. while (n) {
  1987. #if IDETAPE_DEBUG_BUGS
  1988. if (bh == NULL) {
  1989. printk(KERN_ERR "ide-tape: bh == NULL in "
  1990. "idetape_copy_stage_from_user\n");
  1991. return 1;
  1992. }
  1993. #endif /* IDETAPE_DEBUG_BUGS */
  1994. count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
  1995. if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
  1996. ret = 1;
  1997. n -= count;
  1998. atomic_add(count, &bh->b_count);
  1999. buf += count;
  2000. if (atomic_read(&bh->b_count) == bh->b_size) {
  2001. bh = bh->b_reqnext;
  2002. if (bh)
  2003. atomic_set(&bh->b_count, 0);
  2004. }
  2005. }
  2006. tape->bh = bh;
  2007. return ret;
  2008. }
  2009. static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
  2010. {
  2011. struct idetape_bh *bh = tape->bh;
  2012. int count;
  2013. int ret = 0;
  2014. while (n) {
  2015. #if IDETAPE_DEBUG_BUGS
  2016. if (bh == NULL) {
  2017. printk(KERN_ERR "ide-tape: bh == NULL in "
  2018. "idetape_copy_stage_to_user\n");
  2019. return 1;
  2020. }
  2021. #endif /* IDETAPE_DEBUG_BUGS */
  2022. count = min(tape->b_count, n);
  2023. if (copy_to_user(buf, tape->b_data, count))
  2024. ret = 1;
  2025. n -= count;
  2026. tape->b_data += count;
  2027. tape->b_count -= count;
  2028. buf += count;
  2029. if (!tape->b_count) {
  2030. tape->bh = bh = bh->b_reqnext;
  2031. if (bh) {
  2032. tape->b_data = bh->b_data;
  2033. tape->b_count = atomic_read(&bh->b_count);
  2034. }
  2035. }
  2036. }
  2037. return ret;
  2038. }
  2039. static void idetape_init_merge_stage (idetape_tape_t *tape)
  2040. {
  2041. struct idetape_bh *bh = tape->merge_stage->bh;
  2042. tape->bh = bh;
  2043. if (tape->chrdev_direction == idetape_direction_write)
  2044. atomic_set(&bh->b_count, 0);
  2045. else {
  2046. tape->b_data = bh->b_data;
  2047. tape->b_count = atomic_read(&bh->b_count);
  2048. }
  2049. }
  2050. static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
  2051. {
  2052. struct idetape_bh *tmp;
  2053. tmp = stage->bh;
  2054. stage->bh = tape->merge_stage->bh;
  2055. tape->merge_stage->bh = tmp;
  2056. idetape_init_merge_stage(tape);
  2057. }
  2058. /*
  2059. * idetape_add_stage_tail adds a new stage at the end of the pipeline.
  2060. */
  2061. static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
  2062. {
  2063. idetape_tape_t *tape = drive->driver_data;
  2064. unsigned long flags;
  2065. #if IDETAPE_DEBUG_LOG
  2066. if (tape->debug_level >= 4)
  2067. printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
  2068. #endif /* IDETAPE_DEBUG_LOG */
  2069. spin_lock_irqsave(&tape->spinlock, flags);
  2070. stage->next = NULL;
  2071. if (tape->last_stage != NULL)
  2072. tape->last_stage->next=stage;
  2073. else
  2074. tape->first_stage = tape->next_stage=stage;
  2075. tape->last_stage = stage;
  2076. if (tape->next_stage == NULL)
  2077. tape->next_stage = tape->last_stage;
  2078. tape->nr_stages++;
  2079. tape->nr_pending_stages++;
  2080. spin_unlock_irqrestore(&tape->spinlock, flags);
  2081. }
  2082. /*
  2083. * idetape_wait_for_request installs a completion in a pending request
  2084. * and sleeps until it is serviced.
  2085. *
  2086. * The caller should ensure that the request will not be serviced
  2087. * before we install the completion (usually by disabling interrupts).
  2088. */
  2089. static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
  2090. {
  2091. DECLARE_COMPLETION_ONSTACK(wait);
  2092. idetape_tape_t *tape = drive->driver_data;
  2093. #if IDETAPE_DEBUG_BUGS
  2094. if (rq == NULL || !blk_special_request(rq)) {
  2095. printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
  2096. return;
  2097. }
  2098. #endif /* IDETAPE_DEBUG_BUGS */
  2099. rq->end_io_data = &wait;
  2100. rq->end_io = blk_end_sync_rq;
  2101. spin_unlock_irq(&tape->spinlock);
  2102. wait_for_completion(&wait);
  2103. /* The stage and its struct request have been deallocated */
  2104. spin_lock_irq(&tape->spinlock);
  2105. }
  2106. static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
  2107. {
  2108. idetape_tape_t *tape = drive->driver_data;
  2109. idetape_read_position_result_t *result;
  2110. #if IDETAPE_DEBUG_LOG
  2111. if (tape->debug_level >= 4)
  2112. printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
  2113. #endif /* IDETAPE_DEBUG_LOG */
  2114. if (!tape->pc->error) {
  2115. result = (idetape_read_position_result_t *) tape->pc->buffer;
  2116. #if IDETAPE_DEBUG_LOG
  2117. if (tape->debug_level >= 2)
  2118. printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
  2119. if (tape->debug_level >= 2)
  2120. printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
  2121. #endif /* IDETAPE_DEBUG_LOG */
  2122. if (result->bpu) {
  2123. printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
  2124. clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
  2125. idetape_end_request(drive, 0, 0);
  2126. } else {
  2127. #if IDETAPE_DEBUG_LOG
  2128. if (tape->debug_level >= 2)
  2129. printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
  2130. #endif /* IDETAPE_DEBUG_LOG */
  2131. tape->partition = result->partition;
  2132. tape->first_frame_position = ntohl(result->first_block);
  2133. tape->last_frame_position = ntohl(result->last_block);
  2134. tape->blocks_in_buffer = result->blocks_in_buffer[2];
  2135. set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
  2136. idetape_end_request(drive, 1, 0);
  2137. }
  2138. } else {
  2139. idetape_end_request(drive, 0, 0);
  2140. }
  2141. return ide_stopped;
  2142. }
  2143. /*
  2144. * idetape_create_write_filemark_cmd will:
  2145. *
  2146. * 1. Write a filemark if write_filemark=1.
  2147. * 2. Flush the device buffers without writing a filemark
  2148. * if write_filemark=0.
  2149. *
  2150. */
  2151. static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
  2152. {
  2153. idetape_init_pc(pc);
  2154. pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
  2155. pc->c[4] = write_filemark;
  2156. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2157. pc->callback = &idetape_pc_callback;
  2158. }
  2159. static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
  2160. {
  2161. idetape_init_pc(pc);
  2162. pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
  2163. pc->callback = &idetape_pc_callback;
  2164. }
  2165. /*
  2166. * idetape_queue_pc_tail is based on the following functions:
  2167. *
  2168. * ide_do_drive_cmd from ide.c
  2169. * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
  2170. *
  2171. * We add a special packet command request to the tail of the request
  2172. * queue, and wait for it to be serviced.
  2173. *
  2174. * This is not to be called from within the request handling part
  2175. * of the driver ! We allocate here data in the stack, and it is valid
  2176. * until the request is finished. This is not the case for the bottom
  2177. * part of the driver, where we are always leaving the functions to wait
  2178. * for an interrupt or a timer event.
  2179. *
  2180. * From the bottom part of the driver, we should allocate safe memory
  2181. * using idetape_next_pc_storage and idetape_next_rq_storage, and add
  2182. * the request to the request list without waiting for it to be serviced !
  2183. * In that case, we usually use idetape_queue_pc_head.
  2184. */
  2185. static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
  2186. {
  2187. struct ide_tape_obj *tape = drive->driver_data;
  2188. struct request rq;
  2189. idetape_init_rq(&rq, REQ_IDETAPE_PC1);
  2190. rq.buffer = (char *) pc;
  2191. rq.rq_disk = tape->disk;
  2192. return ide_do_drive_cmd(drive, &rq, ide_wait);
  2193. }
  2194. static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
  2195. {
  2196. idetape_init_pc(pc);
  2197. pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
  2198. pc->c[4] = cmd;
  2199. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2200. pc->callback = &idetape_pc_callback;
  2201. }
  2202. static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
  2203. {
  2204. idetape_tape_t *tape = drive->driver_data;
  2205. idetape_pc_t pc;
  2206. int load_attempted = 0;
  2207. /*
  2208. * Wait for the tape to become ready
  2209. */
  2210. set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
  2211. timeout += jiffies;
  2212. while (time_before(jiffies, timeout)) {
  2213. idetape_create_test_unit_ready_cmd(&pc);
  2214. if (!__idetape_queue_pc_tail(drive, &pc))
  2215. return 0;
  2216. if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
  2217. || (tape->asc == 0x3A)) { /* no media */
  2218. if (load_attempted)
  2219. return -ENOMEDIUM;
  2220. idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
  2221. __idetape_queue_pc_tail(drive, &pc);
  2222. load_attempted = 1;
  2223. /* not about to be ready */
  2224. } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
  2225. (tape->ascq == 1 || tape->ascq == 8)))
  2226. return -EIO;
  2227. msleep(100);
  2228. }
  2229. return -EIO;
  2230. }
  2231. static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
  2232. {
  2233. return __idetape_queue_pc_tail(drive, pc);
  2234. }
  2235. static int idetape_flush_tape_buffers (ide_drive_t *drive)
  2236. {
  2237. idetape_pc_t pc;
  2238. int rc;
  2239. idetape_create_write_filemark_cmd(drive, &pc, 0);
  2240. if ((rc = idetape_queue_pc_tail(drive, &pc)))
  2241. return rc;
  2242. idetape_wait_ready(drive, 60 * 5 * HZ);
  2243. return 0;
  2244. }
  2245. static void idetape_create_read_position_cmd (idetape_pc_t *pc)
  2246. {
  2247. idetape_init_pc(pc);
  2248. pc->c[0] = IDETAPE_READ_POSITION_CMD;
  2249. pc->request_transfer = 20;
  2250. pc->callback = &idetape_read_position_callback;
  2251. }
  2252. static int idetape_read_position (ide_drive_t *drive)
  2253. {
  2254. idetape_tape_t *tape = drive->driver_data;
  2255. idetape_pc_t pc;
  2256. int position;
  2257. #if IDETAPE_DEBUG_LOG
  2258. if (tape->debug_level >= 4)
  2259. printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
  2260. #endif /* IDETAPE_DEBUG_LOG */
  2261. idetape_create_read_position_cmd(&pc);
  2262. if (idetape_queue_pc_tail(drive, &pc))
  2263. return -1;
  2264. position = tape->first_frame_position;
  2265. return position;
  2266. }
  2267. static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
  2268. {
  2269. idetape_init_pc(pc);
  2270. pc->c[0] = IDETAPE_LOCATE_CMD;
  2271. pc->c[1] = 2;
  2272. put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
  2273. pc->c[8] = partition;
  2274. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2275. pc->callback = &idetape_pc_callback;
  2276. }
  2277. static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
  2278. {
  2279. idetape_tape_t *tape = drive->driver_data;
  2280. if (!tape->capabilities.lock)
  2281. return 0;
  2282. idetape_init_pc(pc);
  2283. pc->c[0] = IDETAPE_PREVENT_CMD;
  2284. pc->c[4] = prevent;
  2285. pc->callback = &idetape_pc_callback;
  2286. return 1;
  2287. }
  2288. static int __idetape_discard_read_pipeline (ide_drive_t *drive)
  2289. {
  2290. idetape_tape_t *tape = drive->driver_data;
  2291. unsigned long flags;
  2292. int cnt;
  2293. if (tape->chrdev_direction != idetape_direction_read)
  2294. return 0;
  2295. /* Remove merge stage. */
  2296. cnt = tape->merge_stage_size / tape->tape_block_size;
  2297. if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
  2298. ++cnt; /* Filemarks count as 1 sector */
  2299. tape->merge_stage_size = 0;
  2300. if (tape->merge_stage != NULL) {
  2301. __idetape_kfree_stage(tape->merge_stage);
  2302. tape->merge_stage = NULL;
  2303. }
  2304. /* Clear pipeline flags. */
  2305. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  2306. tape->chrdev_direction = idetape_direction_none;
  2307. /* Remove pipeline stages. */
  2308. if (tape->first_stage == NULL)
  2309. return 0;
  2310. spin_lock_irqsave(&tape->spinlock, flags);
  2311. tape->next_stage = NULL;
  2312. if (idetape_pipeline_active(tape))
  2313. idetape_wait_for_request(drive, tape->active_data_request);
  2314. spin_unlock_irqrestore(&tape->spinlock, flags);
  2315. while (tape->first_stage != NULL) {
  2316. struct request *rq_ptr = &tape->first_stage->rq;
  2317. cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
  2318. if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
  2319. ++cnt;
  2320. idetape_remove_stage_head(drive);
  2321. }
  2322. tape->nr_pending_stages = 0;
  2323. tape->max_stages = tape->min_pipeline;
  2324. return cnt;
  2325. }
  2326. /*
  2327. * idetape_position_tape positions the tape to the requested block
  2328. * using the LOCATE packet command. A READ POSITION command is then
  2329. * issued to check where we are positioned.
  2330. *
  2331. * Like all higher level operations, we queue the commands at the tail
  2332. * of the request queue and wait for their completion.
  2333. *
  2334. */
  2335. static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
  2336. {
  2337. idetape_tape_t *tape = drive->driver_data;
  2338. int retval;
  2339. idetape_pc_t pc;
  2340. if (tape->chrdev_direction == idetape_direction_read)
  2341. __idetape_discard_read_pipeline(drive);
  2342. idetape_wait_ready(drive, 60 * 5 * HZ);
  2343. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  2344. retval = idetape_queue_pc_tail(drive, &pc);
  2345. if (retval)
  2346. return (retval);
  2347. idetape_create_read_position_cmd(&pc);
  2348. return (idetape_queue_pc_tail(drive, &pc));
  2349. }
  2350. static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
  2351. {
  2352. idetape_tape_t *tape = drive->driver_data;
  2353. int cnt;
  2354. int seek, position;
  2355. cnt = __idetape_discard_read_pipeline(drive);
  2356. if (restore_position) {
  2357. position = idetape_read_position(drive);
  2358. seek = position > cnt ? position - cnt : 0;
  2359. if (idetape_position_tape(drive, seek, 0, 0)) {
  2360. printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
  2361. return;
  2362. }
  2363. }
  2364. }
  2365. /*
  2366. * idetape_queue_rw_tail generates a read/write request for the block
  2367. * device interface and wait for it to be serviced.
  2368. */
  2369. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
  2370. {
  2371. idetape_tape_t *tape = drive->driver_data;
  2372. struct request rq;
  2373. #if IDETAPE_DEBUG_LOG
  2374. if (tape->debug_level >= 2)
  2375. printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
  2376. #endif /* IDETAPE_DEBUG_LOG */
  2377. #if IDETAPE_DEBUG_BUGS
  2378. if (idetape_pipeline_active(tape)) {
  2379. printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
  2380. return (0);
  2381. }
  2382. #endif /* IDETAPE_DEBUG_BUGS */
  2383. idetape_init_rq(&rq, cmd);
  2384. rq.rq_disk = tape->disk;
  2385. rq.special = (void *)bh;
  2386. rq.sector = tape->first_frame_position;
  2387. rq.nr_sectors = rq.current_nr_sectors = blocks;
  2388. (void) ide_do_drive_cmd(drive, &rq, ide_wait);
  2389. if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
  2390. return 0;
  2391. if (tape->merge_stage)
  2392. idetape_init_merge_stage(tape);
  2393. if (rq.errors == IDETAPE_ERROR_GENERAL)
  2394. return -EIO;
  2395. return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
  2396. }
  2397. /*
  2398. * idetape_insert_pipeline_into_queue is used to start servicing the
  2399. * pipeline stages, starting from tape->next_stage.
  2400. */
  2401. static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
  2402. {
  2403. idetape_tape_t *tape = drive->driver_data;
  2404. if (tape->next_stage == NULL)
  2405. return;
  2406. if (!idetape_pipeline_active(tape)) {
  2407. set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  2408. idetape_active_next_stage(drive);
  2409. (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
  2410. }
  2411. }
  2412. static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
  2413. {
  2414. idetape_init_pc(pc);
  2415. pc->c[0] = IDETAPE_INQUIRY_CMD;
  2416. pc->c[4] = pc->request_transfer = 254;
  2417. pc->callback = &idetape_pc_callback;
  2418. }
  2419. static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
  2420. {
  2421. idetape_init_pc(pc);
  2422. pc->c[0] = IDETAPE_REWIND_CMD;
  2423. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2424. pc->callback = &idetape_pc_callback;
  2425. }
  2426. static void idetape_create_erase_cmd (idetape_pc_t *pc)
  2427. {
  2428. idetape_init_pc(pc);
  2429. pc->c[0] = IDETAPE_ERASE_CMD;
  2430. pc->c[1] = 1;
  2431. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2432. pc->callback = &idetape_pc_callback;
  2433. }
  2434. static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
  2435. {
  2436. idetape_init_pc(pc);
  2437. pc->c[0] = IDETAPE_SPACE_CMD;
  2438. put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
  2439. pc->c[1] = cmd;
  2440. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2441. pc->callback = &idetape_pc_callback;
  2442. }
  2443. static void idetape_wait_first_stage (ide_drive_t *drive)
  2444. {
  2445. idetape_tape_t *tape = drive->driver_data;
  2446. unsigned long flags;
  2447. if (tape->first_stage == NULL)
  2448. return;
  2449. spin_lock_irqsave(&tape->spinlock, flags);
  2450. if (tape->active_stage == tape->first_stage)
  2451. idetape_wait_for_request(drive, tape->active_data_request);
  2452. spin_unlock_irqrestore(&tape->spinlock, flags);
  2453. }
  2454. /*
  2455. * idetape_add_chrdev_write_request tries to add a character device
  2456. * originated write request to our pipeline. In case we don't succeed,
  2457. * we revert to non-pipelined operation mode for this request.
  2458. *
  2459. * 1. Try to allocate a new pipeline stage.
  2460. * 2. If we can't, wait for more and more requests to be serviced
  2461. * and try again each time.
  2462. * 3. If we still can't allocate a stage, fallback to
  2463. * non-pipelined operation mode for this request.
  2464. */
  2465. static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
  2466. {
  2467. idetape_tape_t *tape = drive->driver_data;
  2468. idetape_stage_t *new_stage;
  2469. unsigned long flags;
  2470. struct request *rq;
  2471. #if IDETAPE_DEBUG_LOG
  2472. if (tape->debug_level >= 3)
  2473. printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
  2474. #endif /* IDETAPE_DEBUG_LOG */
  2475. /*
  2476. * Attempt to allocate a new stage.
  2477. * Pay special attention to possible race conditions.
  2478. */
  2479. while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
  2480. spin_lock_irqsave(&tape->spinlock, flags);
  2481. if (idetape_pipeline_active(tape)) {
  2482. idetape_wait_for_request(drive, tape->active_data_request);
  2483. spin_unlock_irqrestore(&tape->spinlock, flags);
  2484. } else {
  2485. spin_unlock_irqrestore(&tape->spinlock, flags);
  2486. idetape_insert_pipeline_into_queue(drive);
  2487. if (idetape_pipeline_active(tape))
  2488. continue;
  2489. /*
  2490. * Linux is short on memory. Fallback to
  2491. * non-pipelined operation mode for this request.
  2492. */
  2493. return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
  2494. }
  2495. }
  2496. rq = &new_stage->rq;
  2497. idetape_init_rq(rq, REQ_IDETAPE_WRITE);
  2498. /* Doesn't actually matter - We always assume sequential access */
  2499. rq->sector = tape->first_frame_position;
  2500. rq->nr_sectors = rq->current_nr_sectors = blocks;
  2501. idetape_switch_buffers(tape, new_stage);
  2502. idetape_add_stage_tail(drive, new_stage);
  2503. tape->pipeline_head++;
  2504. calculate_speeds(drive);
  2505. /*
  2506. * Estimate whether the tape has stopped writing by checking
  2507. * if our write pipeline is currently empty. If we are not
  2508. * writing anymore, wait for the pipeline to be full enough
  2509. * (90%) before starting to service requests, so that we will
  2510. * be able to keep up with the higher speeds of the tape.
  2511. */
  2512. if (!idetape_pipeline_active(tape)) {
  2513. if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
  2514. tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
  2515. tape->measure_insert_time = 1;
  2516. tape->insert_time = jiffies;
  2517. tape->insert_size = 0;
  2518. tape->insert_speed = 0;
  2519. idetape_insert_pipeline_into_queue(drive);
  2520. }
  2521. }
  2522. if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
  2523. /* Return a deferred error */
  2524. return -EIO;
  2525. return blocks;
  2526. }
  2527. /*
  2528. * idetape_wait_for_pipeline will wait until all pending pipeline
  2529. * requests are serviced. Typically called on device close.
  2530. */
  2531. static void idetape_wait_for_pipeline (ide_drive_t *drive)
  2532. {
  2533. idetape_tape_t *tape = drive->driver_data;
  2534. unsigned long flags;
  2535. while (tape->next_stage || idetape_pipeline_active(tape)) {
  2536. idetape_insert_pipeline_into_queue(drive);
  2537. spin_lock_irqsave(&tape->spinlock, flags);
  2538. if (idetape_pipeline_active(tape))
  2539. idetape_wait_for_request(drive, tape->active_data_request);
  2540. spin_unlock_irqrestore(&tape->spinlock, flags);
  2541. }
  2542. }
  2543. static void idetape_empty_write_pipeline (ide_drive_t *drive)
  2544. {
  2545. idetape_tape_t *tape = drive->driver_data;
  2546. int blocks, min;
  2547. struct idetape_bh *bh;
  2548. #if IDETAPE_DEBUG_BUGS
  2549. if (tape->chrdev_direction != idetape_direction_write) {
  2550. printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
  2551. return;
  2552. }
  2553. if (tape->merge_stage_size > tape->stage_size) {
  2554. printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
  2555. tape->merge_stage_size = tape->stage_size;
  2556. }
  2557. #endif /* IDETAPE_DEBUG_BUGS */
  2558. if (tape->merge_stage_size) {
  2559. blocks = tape->merge_stage_size / tape->tape_block_size;
  2560. if (tape->merge_stage_size % tape->tape_block_size) {
  2561. unsigned int i;
  2562. blocks++;
  2563. i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
  2564. bh = tape->bh->b_reqnext;
  2565. while (bh) {
  2566. atomic_set(&bh->b_count, 0);
  2567. bh = bh->b_reqnext;
  2568. }
  2569. bh = tape->bh;
  2570. while (i) {
  2571. if (bh == NULL) {
  2572. printk(KERN_INFO "ide-tape: bug, bh NULL\n");
  2573. break;
  2574. }
  2575. min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
  2576. memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
  2577. atomic_add(min, &bh->b_count);
  2578. i -= min;
  2579. bh = bh->b_reqnext;
  2580. }
  2581. }
  2582. (void) idetape_add_chrdev_write_request(drive, blocks);
  2583. tape->merge_stage_size = 0;
  2584. }
  2585. idetape_wait_for_pipeline(drive);
  2586. if (tape->merge_stage != NULL) {
  2587. __idetape_kfree_stage(tape->merge_stage);
  2588. tape->merge_stage = NULL;
  2589. }
  2590. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  2591. tape->chrdev_direction = idetape_direction_none;
  2592. /*
  2593. * On the next backup, perform the feedback loop again.
  2594. * (I don't want to keep sense information between backups,
  2595. * as some systems are constantly on, and the system load
  2596. * can be totally different on the next backup).
  2597. */
  2598. tape->max_stages = tape->min_pipeline;
  2599. #if IDETAPE_DEBUG_BUGS
  2600. if (tape->first_stage != NULL ||
  2601. tape->next_stage != NULL ||
  2602. tape->last_stage != NULL ||
  2603. tape->nr_stages != 0) {
  2604. printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
  2605. "first_stage %p, next_stage %p, "
  2606. "last_stage %p, nr_stages %d\n",
  2607. tape->first_stage, tape->next_stage,
  2608. tape->last_stage, tape->nr_stages);
  2609. }
  2610. #endif /* IDETAPE_DEBUG_BUGS */
  2611. }
  2612. static void idetape_restart_speed_control (ide_drive_t *drive)
  2613. {
  2614. idetape_tape_t *tape = drive->driver_data;
  2615. tape->restart_speed_control_req = 0;
  2616. tape->pipeline_head = 0;
  2617. tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
  2618. tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
  2619. tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
  2620. tape->uncontrolled_pipeline_head_speed = 0;
  2621. tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
  2622. tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
  2623. }
  2624. static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
  2625. {
  2626. idetape_tape_t *tape = drive->driver_data;
  2627. idetape_stage_t *new_stage;
  2628. struct request rq;
  2629. int bytes_read;
  2630. int blocks = tape->capabilities.ctl;
  2631. /* Initialize read operation */
  2632. if (tape->chrdev_direction != idetape_direction_read) {
  2633. if (tape->chrdev_direction == idetape_direction_write) {
  2634. idetape_empty_write_pipeline(drive);
  2635. idetape_flush_tape_buffers(drive);
  2636. }
  2637. #if IDETAPE_DEBUG_BUGS
  2638. if (tape->merge_stage || tape->merge_stage_size) {
  2639. printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
  2640. tape->merge_stage_size = 0;
  2641. }
  2642. #endif /* IDETAPE_DEBUG_BUGS */
  2643. if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
  2644. return -ENOMEM;
  2645. tape->chrdev_direction = idetape_direction_read;
  2646. /*
  2647. * Issue a read 0 command to ensure that DSC handshake
  2648. * is switched from completion mode to buffer available
  2649. * mode.
  2650. * No point in issuing this if DSC overlap isn't supported,
  2651. * some drives (Seagate STT3401A) will return an error.
  2652. */
  2653. if (drive->dsc_overlap) {
  2654. bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
  2655. if (bytes_read < 0) {
  2656. __idetape_kfree_stage(tape->merge_stage);
  2657. tape->merge_stage = NULL;
  2658. tape->chrdev_direction = idetape_direction_none;
  2659. return bytes_read;
  2660. }
  2661. }
  2662. }
  2663. if (tape->restart_speed_control_req)
  2664. idetape_restart_speed_control(drive);
  2665. idetape_init_rq(&rq, REQ_IDETAPE_READ);
  2666. rq.sector = tape->first_frame_position;
  2667. rq.nr_sectors = rq.current_nr_sectors = blocks;
  2668. if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
  2669. tape->nr_stages < max_stages) {
  2670. new_stage = idetape_kmalloc_stage(tape);
  2671. while (new_stage != NULL) {
  2672. new_stage->rq = rq;
  2673. idetape_add_stage_tail(drive, new_stage);
  2674. if (tape->nr_stages >= max_stages)
  2675. break;
  2676. new_stage = idetape_kmalloc_stage(tape);
  2677. }
  2678. }
  2679. if (!idetape_pipeline_active(tape)) {
  2680. if (tape->nr_pending_stages >= 3 * max_stages / 4) {
  2681. tape->measure_insert_time = 1;
  2682. tape->insert_time = jiffies;
  2683. tape->insert_size = 0;
  2684. tape->insert_speed = 0;
  2685. idetape_insert_pipeline_into_queue(drive);
  2686. }
  2687. }
  2688. return 0;
  2689. }
  2690. /*
  2691. * idetape_add_chrdev_read_request is called from idetape_chrdev_read
  2692. * to service a character device read request and add read-ahead
  2693. * requests to our pipeline.
  2694. */
  2695. static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
  2696. {
  2697. idetape_tape_t *tape = drive->driver_data;
  2698. unsigned long flags;
  2699. struct request *rq_ptr;
  2700. int bytes_read;
  2701. #if IDETAPE_DEBUG_LOG
  2702. if (tape->debug_level >= 4)
  2703. printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
  2704. #endif /* IDETAPE_DEBUG_LOG */
  2705. /*
  2706. * If we are at a filemark, return a read length of 0
  2707. */
  2708. if (test_bit(IDETAPE_FILEMARK, &tape->flags))
  2709. return 0;
  2710. /*
  2711. * Wait for the next block to be available at the head
  2712. * of the pipeline
  2713. */
  2714. idetape_initiate_read(drive, tape->max_stages);
  2715. if (tape->first_stage == NULL) {
  2716. if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
  2717. return 0;
  2718. return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
  2719. }
  2720. idetape_wait_first_stage(drive);
  2721. rq_ptr = &tape->first_stage->rq;
  2722. bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
  2723. rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
  2724. if (rq_ptr->errors == IDETAPE_ERROR_EOD)
  2725. return 0;
  2726. else {
  2727. idetape_switch_buffers(tape, tape->first_stage);
  2728. if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
  2729. set_bit(IDETAPE_FILEMARK, &tape->flags);
  2730. spin_lock_irqsave(&tape->spinlock, flags);
  2731. idetape_remove_stage_head(drive);
  2732. spin_unlock_irqrestore(&tape->spinlock, flags);
  2733. tape->pipeline_head++;
  2734. calculate_speeds(drive);
  2735. }
  2736. #if IDETAPE_DEBUG_BUGS
  2737. if (bytes_read > blocks * tape->tape_block_size) {
  2738. printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
  2739. bytes_read = blocks * tape->tape_block_size;
  2740. }
  2741. #endif /* IDETAPE_DEBUG_BUGS */
  2742. return (bytes_read);
  2743. }
  2744. static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
  2745. {
  2746. idetape_tape_t *tape = drive->driver_data;
  2747. struct idetape_bh *bh;
  2748. int blocks;
  2749. while (bcount) {
  2750. unsigned int count;
  2751. bh = tape->merge_stage->bh;
  2752. count = min(tape->stage_size, bcount);
  2753. bcount -= count;
  2754. blocks = count / tape->tape_block_size;
  2755. while (count) {
  2756. atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
  2757. memset(bh->b_data, 0, atomic_read(&bh->b_count));
  2758. count -= atomic_read(&bh->b_count);
  2759. bh = bh->b_reqnext;
  2760. }
  2761. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
  2762. }
  2763. }
  2764. static int idetape_pipeline_size (ide_drive_t *drive)
  2765. {
  2766. idetape_tape_t *tape = drive->driver_data;
  2767. idetape_stage_t *stage;
  2768. struct request *rq;
  2769. int size = 0;
  2770. idetape_wait_for_pipeline(drive);
  2771. stage = tape->first_stage;
  2772. while (stage != NULL) {
  2773. rq = &stage->rq;
  2774. size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
  2775. if (rq->errors == IDETAPE_ERROR_FILEMARK)
  2776. size += tape->tape_block_size;
  2777. stage = stage->next;
  2778. }
  2779. size += tape->merge_stage_size;
  2780. return size;
  2781. }
  2782. /*
  2783. * Rewinds the tape to the Beginning Of the current Partition (BOP).
  2784. *
  2785. * We currently support only one partition.
  2786. */
  2787. static int idetape_rewind_tape (ide_drive_t *drive)
  2788. {
  2789. int retval;
  2790. idetape_pc_t pc;
  2791. #if IDETAPE_DEBUG_LOG
  2792. idetape_tape_t *tape = drive->driver_data;
  2793. if (tape->debug_level >= 2)
  2794. printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
  2795. #endif /* IDETAPE_DEBUG_LOG */
  2796. idetape_create_rewind_cmd(drive, &pc);
  2797. retval = idetape_queue_pc_tail(drive, &pc);
  2798. if (retval)
  2799. return retval;
  2800. idetape_create_read_position_cmd(&pc);
  2801. retval = idetape_queue_pc_tail(drive, &pc);
  2802. if (retval)
  2803. return retval;
  2804. return 0;
  2805. }
  2806. /*
  2807. * Our special ide-tape ioctl's.
  2808. *
  2809. * Currently there aren't any ioctl's.
  2810. * mtio.h compatible commands should be issued to the character device
  2811. * interface.
  2812. */
  2813. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  2814. {
  2815. idetape_tape_t *tape = drive->driver_data;
  2816. idetape_config_t config;
  2817. void __user *argp = (void __user *)arg;
  2818. #if IDETAPE_DEBUG_LOG
  2819. if (tape->debug_level >= 4)
  2820. printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
  2821. #endif /* IDETAPE_DEBUG_LOG */
  2822. switch (cmd) {
  2823. case 0x0340:
  2824. if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
  2825. return -EFAULT;
  2826. tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
  2827. tape->max_stages = config.nr_stages;
  2828. break;
  2829. case 0x0350:
  2830. config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
  2831. config.nr_stages = tape->max_stages;
  2832. if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
  2833. return -EFAULT;
  2834. break;
  2835. default:
  2836. return -EIO;
  2837. }
  2838. return 0;
  2839. }
  2840. /*
  2841. * idetape_space_over_filemarks is now a bit more complicated than just
  2842. * passing the command to the tape since we may have crossed some
  2843. * filemarks during our pipelined read-ahead mode.
  2844. *
  2845. * As a minor side effect, the pipeline enables us to support MTFSFM when
  2846. * the filemark is in our internal pipeline even if the tape doesn't
  2847. * support spacing over filemarks in the reverse direction.
  2848. */
  2849. static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
  2850. {
  2851. idetape_tape_t *tape = drive->driver_data;
  2852. idetape_pc_t pc;
  2853. unsigned long flags;
  2854. int retval,count=0;
  2855. if (mt_count == 0)
  2856. return 0;
  2857. if (MTBSF == mt_op || MTBSFM == mt_op) {
  2858. if (!tape->capabilities.sprev)
  2859. return -EIO;
  2860. mt_count = - mt_count;
  2861. }
  2862. if (tape->chrdev_direction == idetape_direction_read) {
  2863. /*
  2864. * We have a read-ahead buffer. Scan it for crossed
  2865. * filemarks.
  2866. */
  2867. tape->merge_stage_size = 0;
  2868. if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
  2869. ++count;
  2870. while (tape->first_stage != NULL) {
  2871. if (count == mt_count) {
  2872. if (mt_op == MTFSFM)
  2873. set_bit(IDETAPE_FILEMARK, &tape->flags);
  2874. return 0;
  2875. }
  2876. spin_lock_irqsave(&tape->spinlock, flags);
  2877. if (tape->first_stage == tape->active_stage) {
  2878. /*
  2879. * We have reached the active stage in the read pipeline.
  2880. * There is no point in allowing the drive to continue
  2881. * reading any farther, so we stop the pipeline.
  2882. *
  2883. * This section should be moved to a separate subroutine,
  2884. * because a similar function is performed in
  2885. * __idetape_discard_read_pipeline(), for example.
  2886. */
  2887. tape->next_stage = NULL;
  2888. spin_unlock_irqrestore(&tape->spinlock, flags);
  2889. idetape_wait_first_stage(drive);
  2890. tape->next_stage = tape->first_stage->next;
  2891. } else
  2892. spin_unlock_irqrestore(&tape->spinlock, flags);
  2893. if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
  2894. ++count;
  2895. idetape_remove_stage_head(drive);
  2896. }
  2897. idetape_discard_read_pipeline(drive, 0);
  2898. }
  2899. /*
  2900. * The filemark was not found in our internal pipeline.
  2901. * Now we can issue the space command.
  2902. */
  2903. switch (mt_op) {
  2904. case MTFSF:
  2905. case MTBSF:
  2906. idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
  2907. return (idetape_queue_pc_tail(drive, &pc));
  2908. case MTFSFM:
  2909. case MTBSFM:
  2910. if (!tape->capabilities.sprev)
  2911. return (-EIO);
  2912. retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
  2913. if (retval) return (retval);
  2914. count = (MTBSFM == mt_op ? 1 : -1);
  2915. return (idetape_space_over_filemarks(drive, MTFSF, count));
  2916. default:
  2917. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
  2918. return (-EIO);
  2919. }
  2920. }
  2921. /*
  2922. * Our character device read / write functions.
  2923. *
  2924. * The tape is optimized to maximize throughput when it is transferring
  2925. * an integral number of the "continuous transfer limit", which is
  2926. * a parameter of the specific tape (26 KB on my particular tape).
  2927. * (32 kB for Onstream)
  2928. *
  2929. * As of version 1.3 of the driver, the character device provides an
  2930. * abstract continuous view of the media - any mix of block sizes (even 1
  2931. * byte) on the same backup/restore procedure is supported. The driver
  2932. * will internally convert the requests to the recommended transfer unit,
  2933. * so that an unmatch between the user's block size to the recommended
  2934. * size will only result in a (slightly) increased driver overhead, but
  2935. * will no longer hit performance.
  2936. * This is not applicable to Onstream.
  2937. */
  2938. static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
  2939. size_t count, loff_t *ppos)
  2940. {
  2941. struct ide_tape_obj *tape = ide_tape_f(file);
  2942. ide_drive_t *drive = tape->drive;
  2943. ssize_t bytes_read,temp, actually_read = 0, rc;
  2944. ssize_t ret = 0;
  2945. #if IDETAPE_DEBUG_LOG
  2946. if (tape->debug_level >= 3)
  2947. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
  2948. #endif /* IDETAPE_DEBUG_LOG */
  2949. if (tape->chrdev_direction != idetape_direction_read) {
  2950. if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
  2951. if (count > tape->tape_block_size &&
  2952. (count % tape->tape_block_size) == 0)
  2953. tape->user_bs_factor = count / tape->tape_block_size;
  2954. }
  2955. if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
  2956. return rc;
  2957. if (count == 0)
  2958. return (0);
  2959. if (tape->merge_stage_size) {
  2960. actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
  2961. if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
  2962. ret = -EFAULT;
  2963. buf += actually_read;
  2964. tape->merge_stage_size -= actually_read;
  2965. count -= actually_read;
  2966. }
  2967. while (count >= tape->stage_size) {
  2968. bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
  2969. if (bytes_read <= 0)
  2970. goto finish;
  2971. if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
  2972. ret = -EFAULT;
  2973. buf += bytes_read;
  2974. count -= bytes_read;
  2975. actually_read += bytes_read;
  2976. }
  2977. if (count) {
  2978. bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
  2979. if (bytes_read <= 0)
  2980. goto finish;
  2981. temp = min((unsigned long)count, (unsigned long)bytes_read);
  2982. if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
  2983. ret = -EFAULT;
  2984. actually_read += temp;
  2985. tape->merge_stage_size = bytes_read-temp;
  2986. }
  2987. finish:
  2988. if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
  2989. #if IDETAPE_DEBUG_LOG
  2990. if (tape->debug_level >= 2)
  2991. printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
  2992. #endif
  2993. idetape_space_over_filemarks(drive, MTFSF, 1);
  2994. return 0;
  2995. }
  2996. return (ret) ? ret : actually_read;
  2997. }
  2998. static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
  2999. size_t count, loff_t *ppos)
  3000. {
  3001. struct ide_tape_obj *tape = ide_tape_f(file);
  3002. ide_drive_t *drive = tape->drive;
  3003. ssize_t actually_written = 0;
  3004. ssize_t ret = 0;
  3005. /* The drive is write protected. */
  3006. if (tape->write_prot)
  3007. return -EACCES;
  3008. #if IDETAPE_DEBUG_LOG
  3009. if (tape->debug_level >= 3)
  3010. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
  3011. "count %Zd\n", count);
  3012. #endif /* IDETAPE_DEBUG_LOG */
  3013. /* Initialize write operation */
  3014. if (tape->chrdev_direction != idetape_direction_write) {
  3015. if (tape->chrdev_direction == idetape_direction_read)
  3016. idetape_discard_read_pipeline(drive, 1);
  3017. #if IDETAPE_DEBUG_BUGS
  3018. if (tape->merge_stage || tape->merge_stage_size) {
  3019. printk(KERN_ERR "ide-tape: merge_stage_size "
  3020. "should be 0 now\n");
  3021. tape->merge_stage_size = 0;
  3022. }
  3023. #endif /* IDETAPE_DEBUG_BUGS */
  3024. if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
  3025. return -ENOMEM;
  3026. tape->chrdev_direction = idetape_direction_write;
  3027. idetape_init_merge_stage(tape);
  3028. /*
  3029. * Issue a write 0 command to ensure that DSC handshake
  3030. * is switched from completion mode to buffer available
  3031. * mode.
  3032. * No point in issuing this if DSC overlap isn't supported,
  3033. * some drives (Seagate STT3401A) will return an error.
  3034. */
  3035. if (drive->dsc_overlap) {
  3036. ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
  3037. if (retval < 0) {
  3038. __idetape_kfree_stage(tape->merge_stage);
  3039. tape->merge_stage = NULL;
  3040. tape->chrdev_direction = idetape_direction_none;
  3041. return retval;
  3042. }
  3043. }
  3044. }
  3045. if (count == 0)
  3046. return (0);
  3047. if (tape->restart_speed_control_req)
  3048. idetape_restart_speed_control(drive);
  3049. if (tape->merge_stage_size) {
  3050. #if IDETAPE_DEBUG_BUGS
  3051. if (tape->merge_stage_size >= tape->stage_size) {
  3052. printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
  3053. tape->merge_stage_size = 0;
  3054. }
  3055. #endif /* IDETAPE_DEBUG_BUGS */
  3056. actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
  3057. if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
  3058. ret = -EFAULT;
  3059. buf += actually_written;
  3060. tape->merge_stage_size += actually_written;
  3061. count -= actually_written;
  3062. if (tape->merge_stage_size == tape->stage_size) {
  3063. ssize_t retval;
  3064. tape->merge_stage_size = 0;
  3065. retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
  3066. if (retval <= 0)
  3067. return (retval);
  3068. }
  3069. }
  3070. while (count >= tape->stage_size) {
  3071. ssize_t retval;
  3072. if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
  3073. ret = -EFAULT;
  3074. buf += tape->stage_size;
  3075. count -= tape->stage_size;
  3076. retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
  3077. actually_written += tape->stage_size;
  3078. if (retval <= 0)
  3079. return (retval);
  3080. }
  3081. if (count) {
  3082. actually_written += count;
  3083. if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
  3084. ret = -EFAULT;
  3085. tape->merge_stage_size += count;
  3086. }
  3087. return (ret) ? ret : actually_written;
  3088. }
  3089. static int idetape_write_filemark (ide_drive_t *drive)
  3090. {
  3091. idetape_pc_t pc;
  3092. /* Write a filemark */
  3093. idetape_create_write_filemark_cmd(drive, &pc, 1);
  3094. if (idetape_queue_pc_tail(drive, &pc)) {
  3095. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  3096. return -EIO;
  3097. }
  3098. return 0;
  3099. }
  3100. /*
  3101. * idetape_mtioctop is called from idetape_chrdev_ioctl when
  3102. * the general mtio MTIOCTOP ioctl is requested.
  3103. *
  3104. * We currently support the following mtio.h operations:
  3105. *
  3106. * MTFSF - Space over mt_count filemarks in the positive direction.
  3107. * The tape is positioned after the last spaced filemark.
  3108. *
  3109. * MTFSFM - Same as MTFSF, but the tape is positioned before the
  3110. * last filemark.
  3111. *
  3112. * MTBSF - Steps background over mt_count filemarks, tape is
  3113. * positioned before the last filemark.
  3114. *
  3115. * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
  3116. *
  3117. * Note:
  3118. *
  3119. * MTBSF and MTBSFM are not supported when the tape doesn't
  3120. * support spacing over filemarks in the reverse direction.
  3121. * In this case, MTFSFM is also usually not supported (it is
  3122. * supported in the rare case in which we crossed the filemark
  3123. * during our read-ahead pipelined operation mode).
  3124. *
  3125. * MTWEOF - Writes mt_count filemarks. Tape is positioned after
  3126. * the last written filemark.
  3127. *
  3128. * MTREW - Rewinds tape.
  3129. *
  3130. * MTLOAD - Loads the tape.
  3131. *
  3132. * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
  3133. * MTUNLOAD prevents further access until the media is replaced.
  3134. *
  3135. * MTNOP - Flushes tape buffers.
  3136. *
  3137. * MTRETEN - Retension media. This typically consists of one end
  3138. * to end pass on the media.
  3139. *
  3140. * MTEOM - Moves to the end of recorded data.
  3141. *
  3142. * MTERASE - Erases tape.
  3143. *
  3144. * MTSETBLK - Sets the user block size to mt_count bytes. If
  3145. * mt_count is 0, we will attempt to autodetect
  3146. * the block size.
  3147. *
  3148. * MTSEEK - Positions the tape in a specific block number, where
  3149. * each block is assumed to contain which user_block_size
  3150. * bytes.
  3151. *
  3152. * MTSETPART - Switches to another tape partition.
  3153. *
  3154. * MTLOCK - Locks the tape door.
  3155. *
  3156. * MTUNLOCK - Unlocks the tape door.
  3157. *
  3158. * The following commands are currently not supported:
  3159. *
  3160. * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
  3161. * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
  3162. */
  3163. static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
  3164. {
  3165. idetape_tape_t *tape = drive->driver_data;
  3166. idetape_pc_t pc;
  3167. int i,retval;
  3168. #if IDETAPE_DEBUG_LOG
  3169. if (tape->debug_level >= 1)
  3170. printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
  3171. "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
  3172. #endif /* IDETAPE_DEBUG_LOG */
  3173. /*
  3174. * Commands which need our pipelined read-ahead stages.
  3175. */
  3176. switch (mt_op) {
  3177. case MTFSF:
  3178. case MTFSFM:
  3179. case MTBSF:
  3180. case MTBSFM:
  3181. if (!mt_count)
  3182. return (0);
  3183. return (idetape_space_over_filemarks(drive,mt_op,mt_count));
  3184. default:
  3185. break;
  3186. }
  3187. switch (mt_op) {
  3188. case MTWEOF:
  3189. if (tape->write_prot)
  3190. return -EACCES;
  3191. idetape_discard_read_pipeline(drive, 1);
  3192. for (i = 0; i < mt_count; i++) {
  3193. retval = idetape_write_filemark(drive);
  3194. if (retval)
  3195. return retval;
  3196. }
  3197. return (0);
  3198. case MTREW:
  3199. idetape_discard_read_pipeline(drive, 0);
  3200. if (idetape_rewind_tape(drive))
  3201. return -EIO;
  3202. return 0;
  3203. case MTLOAD:
  3204. idetape_discard_read_pipeline(drive, 0);
  3205. idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
  3206. return (idetape_queue_pc_tail(drive, &pc));
  3207. case MTUNLOAD:
  3208. case MTOFFL:
  3209. /*
  3210. * If door is locked, attempt to unlock before
  3211. * attempting to eject.
  3212. */
  3213. if (tape->door_locked) {
  3214. if (idetape_create_prevent_cmd(drive, &pc, 0))
  3215. if (!idetape_queue_pc_tail(drive, &pc))
  3216. tape->door_locked = DOOR_UNLOCKED;
  3217. }
  3218. idetape_discard_read_pipeline(drive, 0);
  3219. idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
  3220. retval = idetape_queue_pc_tail(drive, &pc);
  3221. if (!retval)
  3222. clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
  3223. return retval;
  3224. case MTNOP:
  3225. idetape_discard_read_pipeline(drive, 0);
  3226. return (idetape_flush_tape_buffers(drive));
  3227. case MTRETEN:
  3228. idetape_discard_read_pipeline(drive, 0);
  3229. idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  3230. return (idetape_queue_pc_tail(drive, &pc));
  3231. case MTEOM:
  3232. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  3233. return (idetape_queue_pc_tail(drive, &pc));
  3234. case MTERASE:
  3235. (void) idetape_rewind_tape(drive);
  3236. idetape_create_erase_cmd(&pc);
  3237. return (idetape_queue_pc_tail(drive, &pc));
  3238. case MTSETBLK:
  3239. if (mt_count) {
  3240. if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
  3241. return -EIO;
  3242. tape->user_bs_factor = mt_count / tape->tape_block_size;
  3243. clear_bit(IDETAPE_DETECT_BS, &tape->flags);
  3244. } else
  3245. set_bit(IDETAPE_DETECT_BS, &tape->flags);
  3246. return 0;
  3247. case MTSEEK:
  3248. idetape_discard_read_pipeline(drive, 0);
  3249. return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
  3250. case MTSETPART:
  3251. idetape_discard_read_pipeline(drive, 0);
  3252. return (idetape_position_tape(drive, 0, mt_count, 0));
  3253. case MTFSR:
  3254. case MTBSR:
  3255. case MTLOCK:
  3256. if (!idetape_create_prevent_cmd(drive, &pc, 1))
  3257. return 0;
  3258. retval = idetape_queue_pc_tail(drive, &pc);
  3259. if (retval) return retval;
  3260. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  3261. return 0;
  3262. case MTUNLOCK:
  3263. if (!idetape_create_prevent_cmd(drive, &pc, 0))
  3264. return 0;
  3265. retval = idetape_queue_pc_tail(drive, &pc);
  3266. if (retval) return retval;
  3267. tape->door_locked = DOOR_UNLOCKED;
  3268. return 0;
  3269. default:
  3270. printk(KERN_ERR "ide-tape: MTIO operation %d not "
  3271. "supported\n", mt_op);
  3272. return (-EIO);
  3273. }
  3274. }
  3275. /*
  3276. * Our character device ioctls.
  3277. *
  3278. * General mtio.h magnetic io commands are supported here, and not in
  3279. * the corresponding block interface.
  3280. *
  3281. * The following ioctls are supported:
  3282. *
  3283. * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
  3284. *
  3285. * MTIOCGET - The mt_dsreg field in the returned mtget structure
  3286. * will be set to (user block size in bytes <<
  3287. * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
  3288. *
  3289. * The mt_blkno is set to the current user block number.
  3290. * The other mtget fields are not supported.
  3291. *
  3292. * MTIOCPOS - The current tape "block position" is returned. We
  3293. * assume that each block contains user_block_size
  3294. * bytes.
  3295. *
  3296. * Our own ide-tape ioctls are supported on both interfaces.
  3297. */
  3298. static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  3299. {
  3300. struct ide_tape_obj *tape = ide_tape_f(file);
  3301. ide_drive_t *drive = tape->drive;
  3302. struct mtop mtop;
  3303. struct mtget mtget;
  3304. struct mtpos mtpos;
  3305. int block_offset = 0, position = tape->first_frame_position;
  3306. void __user *argp = (void __user *)arg;
  3307. #if IDETAPE_DEBUG_LOG
  3308. if (tape->debug_level >= 3)
  3309. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
  3310. "cmd=%u\n", cmd);
  3311. #endif /* IDETAPE_DEBUG_LOG */
  3312. tape->restart_speed_control_req = 1;
  3313. if (tape->chrdev_direction == idetape_direction_write) {
  3314. idetape_empty_write_pipeline(drive);
  3315. idetape_flush_tape_buffers(drive);
  3316. }
  3317. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  3318. block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
  3319. if ((position = idetape_read_position(drive)) < 0)
  3320. return -EIO;
  3321. }
  3322. switch (cmd) {
  3323. case MTIOCTOP:
  3324. if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
  3325. return -EFAULT;
  3326. return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
  3327. case MTIOCGET:
  3328. memset(&mtget, 0, sizeof (struct mtget));
  3329. mtget.mt_type = MT_ISSCSI2;
  3330. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  3331. mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  3332. if (tape->drv_write_prot) {
  3333. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  3334. }
  3335. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  3336. return -EFAULT;
  3337. return 0;
  3338. case MTIOCPOS:
  3339. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  3340. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  3341. return -EFAULT;
  3342. return 0;
  3343. default:
  3344. if (tape->chrdev_direction == idetape_direction_read)
  3345. idetape_discard_read_pipeline(drive, 1);
  3346. return idetape_blkdev_ioctl(drive, cmd, arg);
  3347. }
  3348. }
  3349. static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
  3350. /*
  3351. * Our character device open function.
  3352. */
  3353. static int idetape_chrdev_open (struct inode *inode, struct file *filp)
  3354. {
  3355. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  3356. ide_drive_t *drive;
  3357. idetape_tape_t *tape;
  3358. idetape_pc_t pc;
  3359. int retval;
  3360. /*
  3361. * We really want to do nonseekable_open(inode, filp); here, but some
  3362. * versions of tar incorrectly call lseek on tapes and bail out if that
  3363. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  3364. */
  3365. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  3366. #if IDETAPE_DEBUG_LOG
  3367. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
  3368. #endif /* IDETAPE_DEBUG_LOG */
  3369. if (i >= MAX_HWIFS * MAX_DRIVES)
  3370. return -ENXIO;
  3371. if (!(tape = ide_tape_chrdev_get(i)))
  3372. return -ENXIO;
  3373. drive = tape->drive;
  3374. filp->private_data = tape;
  3375. if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
  3376. retval = -EBUSY;
  3377. goto out_put_tape;
  3378. }
  3379. retval = idetape_wait_ready(drive, 60 * HZ);
  3380. if (retval) {
  3381. clear_bit(IDETAPE_BUSY, &tape->flags);
  3382. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  3383. goto out_put_tape;
  3384. }
  3385. idetape_read_position(drive);
  3386. if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
  3387. (void)idetape_rewind_tape(drive);
  3388. if (tape->chrdev_direction != idetape_direction_read)
  3389. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  3390. /* Read block size and write protect status from drive. */
  3391. idetape_get_blocksize_from_block_descriptor(drive);
  3392. /* Set write protect flag if device is opened as read-only. */
  3393. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  3394. tape->write_prot = 1;
  3395. else
  3396. tape->write_prot = tape->drv_write_prot;
  3397. /* Make sure drive isn't write protected if user wants to write. */
  3398. if (tape->write_prot) {
  3399. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  3400. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  3401. clear_bit(IDETAPE_BUSY, &tape->flags);
  3402. retval = -EROFS;
  3403. goto out_put_tape;
  3404. }
  3405. }
  3406. /*
  3407. * Lock the tape drive door so user can't eject.
  3408. */
  3409. if (tape->chrdev_direction == idetape_direction_none) {
  3410. if (idetape_create_prevent_cmd(drive, &pc, 1)) {
  3411. if (!idetape_queue_pc_tail(drive, &pc)) {
  3412. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  3413. tape->door_locked = DOOR_LOCKED;
  3414. }
  3415. }
  3416. }
  3417. idetape_restart_speed_control(drive);
  3418. tape->restart_speed_control_req = 0;
  3419. return 0;
  3420. out_put_tape:
  3421. ide_tape_put(tape);
  3422. return retval;
  3423. }
  3424. static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
  3425. {
  3426. idetape_tape_t *tape = drive->driver_data;
  3427. idetape_empty_write_pipeline(drive);
  3428. tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
  3429. if (tape->merge_stage != NULL) {
  3430. idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
  3431. __idetape_kfree_stage(tape->merge_stage);
  3432. tape->merge_stage = NULL;
  3433. }
  3434. idetape_write_filemark(drive);
  3435. idetape_flush_tape_buffers(drive);
  3436. idetape_flush_tape_buffers(drive);
  3437. }
  3438. /*
  3439. * Our character device release function.
  3440. */
  3441. static int idetape_chrdev_release (struct inode *inode, struct file *filp)
  3442. {
  3443. struct ide_tape_obj *tape = ide_tape_f(filp);
  3444. ide_drive_t *drive = tape->drive;
  3445. idetape_pc_t pc;
  3446. unsigned int minor = iminor(inode);
  3447. lock_kernel();
  3448. tape = drive->driver_data;
  3449. #if IDETAPE_DEBUG_LOG
  3450. if (tape->debug_level >= 3)
  3451. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
  3452. #endif /* IDETAPE_DEBUG_LOG */
  3453. if (tape->chrdev_direction == idetape_direction_write)
  3454. idetape_write_release(drive, minor);
  3455. if (tape->chrdev_direction == idetape_direction_read) {
  3456. if (minor < 128)
  3457. idetape_discard_read_pipeline(drive, 1);
  3458. else
  3459. idetape_wait_for_pipeline(drive);
  3460. }
  3461. if (tape->cache_stage != NULL) {
  3462. __idetape_kfree_stage(tape->cache_stage);
  3463. tape->cache_stage = NULL;
  3464. }
  3465. if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
  3466. (void) idetape_rewind_tape(drive);
  3467. if (tape->chrdev_direction == idetape_direction_none) {
  3468. if (tape->door_locked == DOOR_LOCKED) {
  3469. if (idetape_create_prevent_cmd(drive, &pc, 0)) {
  3470. if (!idetape_queue_pc_tail(drive, &pc))
  3471. tape->door_locked = DOOR_UNLOCKED;
  3472. }
  3473. }
  3474. }
  3475. clear_bit(IDETAPE_BUSY, &tape->flags);
  3476. ide_tape_put(tape);
  3477. unlock_kernel();
  3478. return 0;
  3479. }
  3480. /*
  3481. * idetape_identify_device is called to check the contents of the
  3482. * ATAPI IDENTIFY command results. We return:
  3483. *
  3484. * 1 If the tape can be supported by us, based on the information
  3485. * we have so far.
  3486. *
  3487. * 0 If this tape driver is not currently supported by us.
  3488. */
  3489. static int idetape_identify_device (ide_drive_t *drive)
  3490. {
  3491. struct idetape_id_gcw gcw;
  3492. struct hd_driveid *id = drive->id;
  3493. if (drive->id_read == 0)
  3494. return 1;
  3495. *((unsigned short *) &gcw) = id->config;
  3496. #if IDETAPE_DEBUG_INFO
  3497. printk(KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n");
  3498. printk(KERN_INFO "ide-tape: Protocol Type: ");
  3499. switch (gcw.protocol) {
  3500. case 0: case 1: printk("ATA\n");break;
  3501. case 2: printk("ATAPI\n");break;
  3502. case 3: printk("Reserved (Unknown to ide-tape)\n");break;
  3503. }
  3504. printk(KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
  3505. switch (gcw.device_type) {
  3506. case 0: printk("Direct-access Device\n");break;
  3507. case 1: printk("Streaming Tape Device\n");break;
  3508. case 2: case 3: case 4: printk("Reserved\n");break;
  3509. case 5: printk("CD-ROM Device\n");break;
  3510. case 6: printk("Reserved\n");
  3511. case 7: printk("Optical memory Device\n");break;
  3512. case 0x1f: printk("Unknown or no Device type\n");break;
  3513. default: printk("Reserved\n");
  3514. }
  3515. printk(KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n");
  3516. printk(KERN_INFO "ide-tape: Command Packet DRQ Type: ");
  3517. switch (gcw.drq_type) {
  3518. case 0: printk("Microprocessor DRQ\n");break;
  3519. case 1: printk("Interrupt DRQ\n");break;
  3520. case 2: printk("Accelerated DRQ\n");break;
  3521. case 3: printk("Reserved\n");break;
  3522. }
  3523. printk(KERN_INFO "ide-tape: Command Packet Size: ");
  3524. switch (gcw.packet_size) {
  3525. case 0: printk("12 bytes\n");break;
  3526. case 1: printk("16 bytes\n");break;
  3527. default: printk("Reserved\n");break;
  3528. }
  3529. #endif /* IDETAPE_DEBUG_INFO */
  3530. /* Check that we can support this device */
  3531. if (gcw.protocol != 2)
  3532. printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
  3533. gcw.protocol);
  3534. else if (gcw.device_type != 1)
  3535. printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
  3536. "to tape\n", gcw.device_type);
  3537. else if (!gcw.removable)
  3538. printk(KERN_ERR "ide-tape: The removable flag is not set\n");
  3539. else if (gcw.packet_size != 0) {
  3540. printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12 "
  3541. "bytes long\n", gcw.packet_size);
  3542. } else
  3543. return 1;
  3544. return 0;
  3545. }
  3546. /*
  3547. * Use INQUIRY to get the firmware revision
  3548. */
  3549. static void idetape_get_inquiry_results (ide_drive_t *drive)
  3550. {
  3551. char *r;
  3552. idetape_tape_t *tape = drive->driver_data;
  3553. idetape_pc_t pc;
  3554. idetape_inquiry_result_t *inquiry;
  3555. idetape_create_inquiry_cmd(&pc);
  3556. if (idetape_queue_pc_tail(drive, &pc)) {
  3557. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
  3558. return;
  3559. }
  3560. inquiry = (idetape_inquiry_result_t *) pc.buffer;
  3561. memcpy(tape->vendor_id, inquiry->vendor_id, 8);
  3562. memcpy(tape->product_id, inquiry->product_id, 16);
  3563. memcpy(tape->firmware_revision, inquiry->revision_level, 4);
  3564. ide_fixstring(tape->vendor_id, 10, 0);
  3565. ide_fixstring(tape->product_id, 18, 0);
  3566. ide_fixstring(tape->firmware_revision, 6, 0);
  3567. r = tape->firmware_revision;
  3568. if (*(r + 1) == '.')
  3569. tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
  3570. printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n", drive->name, tape->name, tape->vendor_id, tape->product_id, tape->firmware_revision);
  3571. }
  3572. /*
  3573. * idetape_get_mode_sense_results asks the tape about its various
  3574. * parameters. In particular, we will adjust our data transfer buffer
  3575. * size to the recommended value as returned by the tape.
  3576. */
  3577. static void idetape_get_mode_sense_results (ide_drive_t *drive)
  3578. {
  3579. idetape_tape_t *tape = drive->driver_data;
  3580. idetape_pc_t pc;
  3581. idetape_capabilities_page_t *capabilities;
  3582. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  3583. if (idetape_queue_pc_tail(drive, &pc)) {
  3584. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
  3585. tape->tape_block_size = 512;
  3586. tape->capabilities.ctl = 52;
  3587. tape->capabilities.speed = 450;
  3588. tape->capabilities.buffer_size = 6 * 52;
  3589. return;
  3590. }
  3591. capabilities = (idetape_capabilities_page_t *)
  3592. (pc.buffer + 4 + pc.buffer[3]);
  3593. capabilities->max_speed = ntohs(capabilities->max_speed);
  3594. capabilities->ctl = ntohs(capabilities->ctl);
  3595. capabilities->speed = ntohs(capabilities->speed);
  3596. capabilities->buffer_size = ntohs(capabilities->buffer_size);
  3597. if (!capabilities->speed) {
  3598. printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
  3599. capabilities->speed = 650;
  3600. }
  3601. if (!capabilities->max_speed) {
  3602. printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
  3603. capabilities->max_speed = 650;
  3604. }
  3605. tape->capabilities = *capabilities; /* Save us a copy */
  3606. if (capabilities->blk512)
  3607. tape->tape_block_size = 512;
  3608. else if (capabilities->blk1024)
  3609. tape->tape_block_size = 1024;
  3610. #if IDETAPE_DEBUG_INFO
  3611. printk(KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n");
  3612. printk(KERN_INFO "ide-tape: Mode Parameter Header:\n");
  3613. printk(KERN_INFO "ide-tape: Mode Data Length - %d\n", pc.buffer[0]);
  3614. printk(KERN_INFO "ide-tape: Medium Type - %d\n", pc.buffer[1]);
  3615. printk(KERN_INFO "ide-tape: Device Specific Parameter - %d\n",
  3616. pc.buffer[2]);
  3617. printk(KERN_INFO "ide-tape: Block Descriptor Length - %d\n",
  3618. pc.buffer[3]);
  3619. printk(KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n");
  3620. printk(KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code);
  3621. printk(KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length);
  3622. printk(KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No");
  3623. printk(KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
  3624. printk(KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
  3625. printk(KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
  3626. printk(KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
  3627. printk(KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
  3628. printk(KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
  3629. printk(KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
  3630. printk(KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
  3631. printk(KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
  3632. printk(KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
  3633. printk(KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
  3634. printk(KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No");
  3635. printk(KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed);
  3636. printk(KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl);
  3637. printk(KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed);
  3638. printk(KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512);
  3639. #endif /* IDETAPE_DEBUG_INFO */
  3640. }
  3641. /*
  3642. * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
  3643. * and if it succeeds sets the tape block size with the reported value
  3644. */
  3645. static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
  3646. {
  3647. idetape_tape_t *tape = drive->driver_data;
  3648. idetape_pc_t pc;
  3649. idetape_parameter_block_descriptor_t *block_descrp;
  3650. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  3651. if (idetape_queue_pc_tail(drive, &pc)) {
  3652. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  3653. if (tape->tape_block_size == 0) {
  3654. printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
  3655. tape->tape_block_size = 32768;
  3656. }
  3657. return;
  3658. }
  3659. block_descrp = (idetape_parameter_block_descriptor_t *)(pc.buffer + 4);
  3660. tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
  3661. tape->drv_write_prot = (pc.buffer[2] & 0x80) >> 7;
  3662. #if IDETAPE_DEBUG_INFO
  3663. printk(KERN_INFO "ide-tape: Adjusted block size - %d\n", tape->tape_block_size);
  3664. #endif /* IDETAPE_DEBUG_INFO */
  3665. }
  3666. #ifdef CONFIG_IDE_PROC_FS
  3667. static void idetape_add_settings (ide_drive_t *drive)
  3668. {
  3669. idetape_tape_t *tape = drive->driver_data;
  3670. /*
  3671. * drive setting name read/write data type min max mul_factor div_factor data pointer set function
  3672. */
  3673. ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
  3674. ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
  3675. ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
  3676. ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
  3677. ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
  3678. ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
  3679. ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
  3680. ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
  3681. ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_frequency, NULL);
  3682. ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
  3683. ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
  3684. ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
  3685. ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
  3686. ide_add_setting(drive, "debug_level", SETTING_RW, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
  3687. }
  3688. #else
  3689. static inline void idetape_add_settings(ide_drive_t *drive) { ; }
  3690. #endif
  3691. /*
  3692. * ide_setup is called to:
  3693. *
  3694. * 1. Initialize our various state variables.
  3695. * 2. Ask the tape for its capabilities.
  3696. * 3. Allocate a buffer which will be used for data
  3697. * transfer. The buffer size is chosen based on
  3698. * the recommendation which we received in step (2).
  3699. *
  3700. * Note that at this point ide.c already assigned us an irq, so that
  3701. * we can queue requests here and wait for their completion.
  3702. */
  3703. static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
  3704. {
  3705. unsigned long t1, tmid, tn, t;
  3706. int speed;
  3707. struct idetape_id_gcw gcw;
  3708. int stage_size;
  3709. struct sysinfo si;
  3710. spin_lock_init(&tape->spinlock);
  3711. drive->dsc_overlap = 1;
  3712. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  3713. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  3714. tape->name);
  3715. drive->dsc_overlap = 0;
  3716. }
  3717. /* Seagate Travan drives do not support DSC overlap. */
  3718. if (strstr(drive->id->model, "Seagate STT3401"))
  3719. drive->dsc_overlap = 0;
  3720. tape->minor = minor;
  3721. tape->name[0] = 'h';
  3722. tape->name[1] = 't';
  3723. tape->name[2] = '0' + minor;
  3724. tape->chrdev_direction = idetape_direction_none;
  3725. tape->pc = tape->pc_stack;
  3726. tape->max_insert_speed = 10000;
  3727. tape->speed_control = 1;
  3728. *((unsigned short *) &gcw) = drive->id->config;
  3729. if (gcw.drq_type == 1)
  3730. set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
  3731. tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
  3732. idetape_get_inquiry_results(drive);
  3733. idetape_get_mode_sense_results(drive);
  3734. idetape_get_blocksize_from_block_descriptor(drive);
  3735. tape->user_bs_factor = 1;
  3736. tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
  3737. while (tape->stage_size > 0xffff) {
  3738. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  3739. tape->capabilities.ctl /= 2;
  3740. tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
  3741. }
  3742. stage_size = tape->stage_size;
  3743. tape->pages_per_stage = stage_size / PAGE_SIZE;
  3744. if (stage_size % PAGE_SIZE) {
  3745. tape->pages_per_stage++;
  3746. tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
  3747. }
  3748. /*
  3749. * Select the "best" DSC read/write polling frequency
  3750. * and pipeline size.
  3751. */
  3752. speed = max(tape->capabilities.speed, tape->capabilities.max_speed);
  3753. tape->max_stages = speed * 1000 * 10 / tape->stage_size;
  3754. /*
  3755. * Limit memory use for pipeline to 10% of physical memory
  3756. */
  3757. si_meminfo(&si);
  3758. if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
  3759. tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
  3760. tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
  3761. tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
  3762. tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
  3763. if (tape->max_stages == 0)
  3764. tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
  3765. t1 = (tape->stage_size * HZ) / (speed * 1000);
  3766. tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
  3767. tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
  3768. if (tape->max_stages)
  3769. t = tn;
  3770. else
  3771. t = t1;
  3772. /*
  3773. * Ensure that the number we got makes sense; limit
  3774. * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  3775. */
  3776. tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
  3777. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  3778. "%dkB pipeline, %lums tDSC%s\n",
  3779. drive->name, tape->name, tape->capabilities.speed,
  3780. (tape->capabilities.buffer_size * 512) / tape->stage_size,
  3781. tape->stage_size / 1024,
  3782. tape->max_stages * tape->stage_size / 1024,
  3783. tape->best_dsc_rw_frequency * 1000 / HZ,
  3784. drive->using_dma ? ", DMA":"");
  3785. idetape_add_settings(drive);
  3786. }
  3787. static void ide_tape_remove(ide_drive_t *drive)
  3788. {
  3789. idetape_tape_t *tape = drive->driver_data;
  3790. ide_proc_unregister_driver(drive, tape->driver);
  3791. ide_unregister_region(tape->disk);
  3792. ide_tape_put(tape);
  3793. }
  3794. static void ide_tape_release(struct kref *kref)
  3795. {
  3796. struct ide_tape_obj *tape = to_ide_tape(kref);
  3797. ide_drive_t *drive = tape->drive;
  3798. struct gendisk *g = tape->disk;
  3799. BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
  3800. drive->dsc_overlap = 0;
  3801. drive->driver_data = NULL;
  3802. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  3803. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  3804. idetape_devs[tape->minor] = NULL;
  3805. g->private_data = NULL;
  3806. put_disk(g);
  3807. kfree(tape);
  3808. }
  3809. #ifdef CONFIG_IDE_PROC_FS
  3810. static int proc_idetape_read_name
  3811. (char *page, char **start, off_t off, int count, int *eof, void *data)
  3812. {
  3813. ide_drive_t *drive = (ide_drive_t *) data;
  3814. idetape_tape_t *tape = drive->driver_data;
  3815. char *out = page;
  3816. int len;
  3817. len = sprintf(out, "%s\n", tape->name);
  3818. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  3819. }
  3820. static ide_proc_entry_t idetape_proc[] = {
  3821. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  3822. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  3823. { NULL, 0, NULL, NULL }
  3824. };
  3825. #endif
  3826. static int ide_tape_probe(ide_drive_t *);
  3827. static ide_driver_t idetape_driver = {
  3828. .gen_driver = {
  3829. .owner = THIS_MODULE,
  3830. .name = "ide-tape",
  3831. .bus = &ide_bus_type,
  3832. },
  3833. .probe = ide_tape_probe,
  3834. .remove = ide_tape_remove,
  3835. .version = IDETAPE_VERSION,
  3836. .media = ide_tape,
  3837. .supports_dsc_overlap = 1,
  3838. .do_request = idetape_do_request,
  3839. .end_request = idetape_end_request,
  3840. .error = __ide_error,
  3841. .abort = __ide_abort,
  3842. #ifdef CONFIG_IDE_PROC_FS
  3843. .proc = idetape_proc,
  3844. #endif
  3845. };
  3846. /*
  3847. * Our character device supporting functions, passed to register_chrdev.
  3848. */
  3849. static const struct file_operations idetape_fops = {
  3850. .owner = THIS_MODULE,
  3851. .read = idetape_chrdev_read,
  3852. .write = idetape_chrdev_write,
  3853. .ioctl = idetape_chrdev_ioctl,
  3854. .open = idetape_chrdev_open,
  3855. .release = idetape_chrdev_release,
  3856. };
  3857. static int idetape_open(struct inode *inode, struct file *filp)
  3858. {
  3859. struct gendisk *disk = inode->i_bdev->bd_disk;
  3860. struct ide_tape_obj *tape;
  3861. if (!(tape = ide_tape_get(disk)))
  3862. return -ENXIO;
  3863. return 0;
  3864. }
  3865. static int idetape_release(struct inode *inode, struct file *filp)
  3866. {
  3867. struct gendisk *disk = inode->i_bdev->bd_disk;
  3868. struct ide_tape_obj *tape = ide_tape_g(disk);
  3869. ide_tape_put(tape);
  3870. return 0;
  3871. }
  3872. static int idetape_ioctl(struct inode *inode, struct file *file,
  3873. unsigned int cmd, unsigned long arg)
  3874. {
  3875. struct block_device *bdev = inode->i_bdev;
  3876. struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
  3877. ide_drive_t *drive = tape->drive;
  3878. int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  3879. if (err == -EINVAL)
  3880. err = idetape_blkdev_ioctl(drive, cmd, arg);
  3881. return err;
  3882. }
  3883. static struct block_device_operations idetape_block_ops = {
  3884. .owner = THIS_MODULE,
  3885. .open = idetape_open,
  3886. .release = idetape_release,
  3887. .ioctl = idetape_ioctl,
  3888. };
  3889. static int ide_tape_probe(ide_drive_t *drive)
  3890. {
  3891. idetape_tape_t *tape;
  3892. struct gendisk *g;
  3893. int minor;
  3894. if (!strstr("ide-tape", drive->driver_req))
  3895. goto failed;
  3896. if (!drive->present)
  3897. goto failed;
  3898. if (drive->media != ide_tape)
  3899. goto failed;
  3900. if (!idetape_identify_device (drive)) {
  3901. printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
  3902. goto failed;
  3903. }
  3904. if (drive->scsi) {
  3905. printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
  3906. goto failed;
  3907. }
  3908. if (strstr(drive->id->model, "OnStream DI-")) {
  3909. printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
  3910. printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
  3911. }
  3912. tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
  3913. if (tape == NULL) {
  3914. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
  3915. goto failed;
  3916. }
  3917. g = alloc_disk(1 << PARTN_BITS);
  3918. if (!g)
  3919. goto out_free_tape;
  3920. ide_init_disk(g, drive);
  3921. ide_proc_register_driver(drive, &idetape_driver);
  3922. kref_init(&tape->kref);
  3923. tape->drive = drive;
  3924. tape->driver = &idetape_driver;
  3925. tape->disk = g;
  3926. g->private_data = &tape->driver;
  3927. drive->driver_data = tape;
  3928. mutex_lock(&idetape_ref_mutex);
  3929. for (minor = 0; idetape_devs[minor]; minor++)
  3930. ;
  3931. idetape_devs[minor] = tape;
  3932. mutex_unlock(&idetape_ref_mutex);
  3933. idetape_setup(drive, tape, minor);
  3934. device_create(idetape_sysfs_class, &drive->gendev,
  3935. MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
  3936. device_create(idetape_sysfs_class, &drive->gendev,
  3937. MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
  3938. g->fops = &idetape_block_ops;
  3939. ide_register_region(g);
  3940. return 0;
  3941. out_free_tape:
  3942. kfree(tape);
  3943. failed:
  3944. return -ENODEV;
  3945. }
  3946. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  3947. MODULE_LICENSE("GPL");
  3948. static void __exit idetape_exit (void)
  3949. {
  3950. driver_unregister(&idetape_driver.gen_driver);
  3951. class_destroy(idetape_sysfs_class);
  3952. unregister_chrdev(IDETAPE_MAJOR, "ht");
  3953. }
  3954. static int __init idetape_init(void)
  3955. {
  3956. int error = 1;
  3957. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  3958. if (IS_ERR(idetape_sysfs_class)) {
  3959. idetape_sysfs_class = NULL;
  3960. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  3961. error = -EBUSY;
  3962. goto out;
  3963. }
  3964. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  3965. printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
  3966. error = -EBUSY;
  3967. goto out_free_class;
  3968. }
  3969. error = driver_register(&idetape_driver.gen_driver);
  3970. if (error)
  3971. goto out_free_driver;
  3972. return 0;
  3973. out_free_driver:
  3974. driver_unregister(&idetape_driver.gen_driver);
  3975. out_free_class:
  3976. class_destroy(idetape_sysfs_class);
  3977. out:
  3978. return error;
  3979. }
  3980. MODULE_ALIAS("ide:*m-tape*");
  3981. module_init(idetape_init);
  3982. module_exit(idetape_exit);
  3983. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);