ide-tape.c 84 KB

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