ide-tape.c 128 KB

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