ide-tape.c 127 KB

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