ide-tape.c 113 KB

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