ide-tape.c 82 KB

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