ide-tape.c 93 KB

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