ide-tape.c 122 KB

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