ide-tape.c 84 KB

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