ide-tape.c 79 KB

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