ide-tape.c 71 KB

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