ide-tape.c 110 KB

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