ide-tape.c 135 KB

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