ide-tape.c 69 KB

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