ide-tape.c 83 KB

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