ide-tape.c 97 KB

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