ide-tape.c 113 KB

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