ide-tape.c 110 KB

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