ide-tape.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573
  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 void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
  1067. {
  1068. idetape_tape_t *tape = drive->driver_data;
  1069. if (tape->chrdev_dir != IDETAPE_DIR_READ)
  1070. return;
  1071. clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
  1072. tape->merge_bh_size = 0;
  1073. if (tape->merge_bh != NULL) {
  1074. ide_tape_kfree_buffer(tape);
  1075. tape->merge_bh = NULL;
  1076. }
  1077. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1078. }
  1079. /*
  1080. * Position the tape to the requested block using the LOCATE packet command.
  1081. * A READ POSITION command is then issued to check where we are positioned. Like
  1082. * all higher level operations, we queue the commands at the tail of the request
  1083. * queue and wait for their completion.
  1084. */
  1085. static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
  1086. u8 partition, int skip)
  1087. {
  1088. idetape_tape_t *tape = drive->driver_data;
  1089. struct gendisk *disk = tape->disk;
  1090. int retval;
  1091. struct ide_atapi_pc pc;
  1092. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1093. __ide_tape_discard_merge_buffer(drive);
  1094. idetape_wait_ready(drive, 60 * 5 * HZ);
  1095. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  1096. retval = ide_queue_pc_tail(drive, disk, &pc);
  1097. if (retval)
  1098. return (retval);
  1099. idetape_create_read_position_cmd(&pc);
  1100. return ide_queue_pc_tail(drive, disk, &pc);
  1101. }
  1102. static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
  1103. int restore_position)
  1104. {
  1105. idetape_tape_t *tape = drive->driver_data;
  1106. int seek, position;
  1107. __ide_tape_discard_merge_buffer(drive);
  1108. if (restore_position) {
  1109. position = idetape_read_position(drive);
  1110. seek = position > 0 ? position : 0;
  1111. if (idetape_position_tape(drive, seek, 0, 0)) {
  1112. printk(KERN_INFO "ide-tape: %s: position_tape failed in"
  1113. " %s\n", tape->name, __func__);
  1114. return;
  1115. }
  1116. }
  1117. }
  1118. /*
  1119. * Generate a read/write request for the block device interface and wait for it
  1120. * to be serviced.
  1121. */
  1122. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
  1123. struct idetape_bh *bh)
  1124. {
  1125. idetape_tape_t *tape = drive->driver_data;
  1126. struct request *rq;
  1127. int ret, errors;
  1128. debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
  1129. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  1130. rq->cmd_type = REQ_TYPE_SPECIAL;
  1131. rq->cmd[13] = cmd;
  1132. rq->rq_disk = tape->disk;
  1133. rq->special = (void *)bh;
  1134. rq->sector = tape->first_frame;
  1135. rq->nr_sectors = blocks;
  1136. rq->current_nr_sectors = blocks;
  1137. blk_execute_rq(drive->queue, tape->disk, rq, 0);
  1138. errors = rq->errors;
  1139. ret = tape->blk_size * (blocks - rq->current_nr_sectors);
  1140. blk_put_request(rq);
  1141. if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
  1142. return 0;
  1143. if (tape->merge_bh)
  1144. idetape_init_merge_buffer(tape);
  1145. if (errors == IDETAPE_ERROR_GENERAL)
  1146. return -EIO;
  1147. return ret;
  1148. }
  1149. static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
  1150. {
  1151. ide_init_pc(pc);
  1152. pc->c[0] = INQUIRY;
  1153. pc->c[4] = 254;
  1154. pc->req_xfer = 254;
  1155. }
  1156. static void idetape_create_rewind_cmd(ide_drive_t *drive,
  1157. struct ide_atapi_pc *pc)
  1158. {
  1159. ide_init_pc(pc);
  1160. pc->c[0] = REZERO_UNIT;
  1161. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1162. }
  1163. static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
  1164. {
  1165. ide_init_pc(pc);
  1166. pc->c[0] = ERASE;
  1167. pc->c[1] = 1;
  1168. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1169. }
  1170. static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
  1171. {
  1172. ide_init_pc(pc);
  1173. pc->c[0] = SPACE;
  1174. put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
  1175. pc->c[1] = cmd;
  1176. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1177. }
  1178. /* Queue up a character device originated write request. */
  1179. static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
  1180. {
  1181. idetape_tape_t *tape = drive->driver_data;
  1182. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1183. return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
  1184. blocks, tape->merge_bh);
  1185. }
  1186. static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
  1187. {
  1188. idetape_tape_t *tape = drive->driver_data;
  1189. int blocks, min;
  1190. struct idetape_bh *bh;
  1191. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1192. printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
  1193. " but we are not writing.\n");
  1194. return;
  1195. }
  1196. if (tape->merge_bh_size > tape->buffer_size) {
  1197. printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
  1198. tape->merge_bh_size = tape->buffer_size;
  1199. }
  1200. if (tape->merge_bh_size) {
  1201. blocks = tape->merge_bh_size / tape->blk_size;
  1202. if (tape->merge_bh_size % tape->blk_size) {
  1203. unsigned int i;
  1204. blocks++;
  1205. i = tape->blk_size - tape->merge_bh_size %
  1206. tape->blk_size;
  1207. bh = tape->bh->b_reqnext;
  1208. while (bh) {
  1209. atomic_set(&bh->b_count, 0);
  1210. bh = bh->b_reqnext;
  1211. }
  1212. bh = tape->bh;
  1213. while (i) {
  1214. if (bh == NULL) {
  1215. printk(KERN_INFO "ide-tape: bug,"
  1216. " bh NULL\n");
  1217. break;
  1218. }
  1219. min = min(i, (unsigned int)(bh->b_size -
  1220. atomic_read(&bh->b_count)));
  1221. memset(bh->b_data + atomic_read(&bh->b_count),
  1222. 0, min);
  1223. atomic_add(min, &bh->b_count);
  1224. i -= min;
  1225. bh = bh->b_reqnext;
  1226. }
  1227. }
  1228. (void) idetape_add_chrdev_write_request(drive, blocks);
  1229. tape->merge_bh_size = 0;
  1230. }
  1231. if (tape->merge_bh != NULL) {
  1232. ide_tape_kfree_buffer(tape);
  1233. tape->merge_bh = NULL;
  1234. }
  1235. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1236. }
  1237. static int idetape_init_read(ide_drive_t *drive)
  1238. {
  1239. idetape_tape_t *tape = drive->driver_data;
  1240. int bytes_read;
  1241. /* Initialize read operation */
  1242. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1243. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1244. ide_tape_flush_merge_buffer(drive);
  1245. idetape_flush_tape_buffers(drive);
  1246. }
  1247. if (tape->merge_bh || tape->merge_bh_size) {
  1248. printk(KERN_ERR "ide-tape: merge_bh_size should be"
  1249. " 0 now\n");
  1250. tape->merge_bh_size = 0;
  1251. }
  1252. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
  1253. if (!tape->merge_bh)
  1254. return -ENOMEM;
  1255. tape->chrdev_dir = IDETAPE_DIR_READ;
  1256. /*
  1257. * Issue a read 0 command to ensure that DSC handshake is
  1258. * switched from completion mode to buffer available mode.
  1259. * No point in issuing this if DSC overlap isn't supported, some
  1260. * drives (Seagate STT3401A) will return an error.
  1261. */
  1262. if (drive->dsc_overlap) {
  1263. bytes_read = idetape_queue_rw_tail(drive,
  1264. REQ_IDETAPE_READ, 0,
  1265. tape->merge_bh);
  1266. if (bytes_read < 0) {
  1267. ide_tape_kfree_buffer(tape);
  1268. tape->merge_bh = NULL;
  1269. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1270. return bytes_read;
  1271. }
  1272. }
  1273. }
  1274. return 0;
  1275. }
  1276. /* called from idetape_chrdev_read() to service a chrdev read request. */
  1277. static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
  1278. {
  1279. idetape_tape_t *tape = drive->driver_data;
  1280. debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
  1281. /* If we are at a filemark, return a read length of 0 */
  1282. if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
  1283. return 0;
  1284. idetape_init_read(drive);
  1285. return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
  1286. tape->merge_bh);
  1287. }
  1288. static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
  1289. {
  1290. idetape_tape_t *tape = drive->driver_data;
  1291. struct idetape_bh *bh;
  1292. int blocks;
  1293. while (bcount) {
  1294. unsigned int count;
  1295. bh = tape->merge_bh;
  1296. count = min(tape->buffer_size, bcount);
  1297. bcount -= count;
  1298. blocks = count / tape->blk_size;
  1299. while (count) {
  1300. atomic_set(&bh->b_count,
  1301. min(count, (unsigned int)bh->b_size));
  1302. memset(bh->b_data, 0, atomic_read(&bh->b_count));
  1303. count -= atomic_read(&bh->b_count);
  1304. bh = bh->b_reqnext;
  1305. }
  1306. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
  1307. tape->merge_bh);
  1308. }
  1309. }
  1310. /*
  1311. * Rewinds the tape to the Beginning Of the current Partition (BOP). We
  1312. * currently support only one partition.
  1313. */
  1314. static int idetape_rewind_tape(ide_drive_t *drive)
  1315. {
  1316. struct ide_tape_obj *tape = drive->driver_data;
  1317. struct gendisk *disk = tape->disk;
  1318. int retval;
  1319. struct ide_atapi_pc pc;
  1320. debug_log(DBG_SENSE, "Enter %s\n", __func__);
  1321. idetape_create_rewind_cmd(drive, &pc);
  1322. retval = ide_queue_pc_tail(drive, disk, &pc);
  1323. if (retval)
  1324. return retval;
  1325. idetape_create_read_position_cmd(&pc);
  1326. retval = ide_queue_pc_tail(drive, disk, &pc);
  1327. if (retval)
  1328. return retval;
  1329. return 0;
  1330. }
  1331. /* mtio.h compatible commands should be issued to the chrdev interface. */
  1332. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
  1333. unsigned long arg)
  1334. {
  1335. idetape_tape_t *tape = drive->driver_data;
  1336. void __user *argp = (void __user *)arg;
  1337. struct idetape_config {
  1338. int dsc_rw_frequency;
  1339. int dsc_media_access_frequency;
  1340. int nr_stages;
  1341. } config;
  1342. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1343. switch (cmd) {
  1344. case 0x0340:
  1345. if (copy_from_user(&config, argp, sizeof(config)))
  1346. return -EFAULT;
  1347. tape->best_dsc_rw_freq = config.dsc_rw_frequency;
  1348. break;
  1349. case 0x0350:
  1350. config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
  1351. config.nr_stages = 1;
  1352. if (copy_to_user(argp, &config, sizeof(config)))
  1353. return -EFAULT;
  1354. break;
  1355. default:
  1356. return -EIO;
  1357. }
  1358. return 0;
  1359. }
  1360. static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
  1361. int mt_count)
  1362. {
  1363. idetape_tape_t *tape = drive->driver_data;
  1364. struct gendisk *disk = tape->disk;
  1365. struct ide_atapi_pc pc;
  1366. int retval, count = 0;
  1367. int sprev = !!(tape->caps[4] & 0x20);
  1368. if (mt_count == 0)
  1369. return 0;
  1370. if (MTBSF == mt_op || MTBSFM == mt_op) {
  1371. if (!sprev)
  1372. return -EIO;
  1373. mt_count = -mt_count;
  1374. }
  1375. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1376. tape->merge_bh_size = 0;
  1377. if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
  1378. ++count;
  1379. ide_tape_discard_merge_buffer(drive, 0);
  1380. }
  1381. switch (mt_op) {
  1382. case MTFSF:
  1383. case MTBSF:
  1384. idetape_create_space_cmd(&pc, mt_count - count,
  1385. IDETAPE_SPACE_OVER_FILEMARK);
  1386. return ide_queue_pc_tail(drive, disk, &pc);
  1387. case MTFSFM:
  1388. case MTBSFM:
  1389. if (!sprev)
  1390. return -EIO;
  1391. retval = idetape_space_over_filemarks(drive, MTFSF,
  1392. mt_count - count);
  1393. if (retval)
  1394. return retval;
  1395. count = (MTBSFM == mt_op ? 1 : -1);
  1396. return idetape_space_over_filemarks(drive, MTFSF, count);
  1397. default:
  1398. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1399. mt_op);
  1400. return -EIO;
  1401. }
  1402. }
  1403. /*
  1404. * Our character device read / write functions.
  1405. *
  1406. * The tape is optimized to maximize throughput when it is transferring an
  1407. * integral number of the "continuous transfer limit", which is a parameter of
  1408. * the specific tape (26kB on my particular tape, 32kB for Onstream).
  1409. *
  1410. * As of version 1.3 of the driver, the character device provides an abstract
  1411. * continuous view of the media - any mix of block sizes (even 1 byte) on the
  1412. * same backup/restore procedure is supported. The driver will internally
  1413. * convert the requests to the recommended transfer unit, so that an unmatch
  1414. * between the user's block size to the recommended size will only result in a
  1415. * (slightly) increased driver overhead, but will no longer hit performance.
  1416. * This is not applicable to Onstream.
  1417. */
  1418. static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
  1419. size_t count, loff_t *ppos)
  1420. {
  1421. struct ide_tape_obj *tape = ide_tape_f(file);
  1422. ide_drive_t *drive = tape->drive;
  1423. ssize_t bytes_read, temp, actually_read = 0, rc;
  1424. ssize_t ret = 0;
  1425. u16 ctl = *(u16 *)&tape->caps[12];
  1426. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1427. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1428. if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
  1429. if (count > tape->blk_size &&
  1430. (count % tape->blk_size) == 0)
  1431. tape->user_bs_factor = count / tape->blk_size;
  1432. }
  1433. rc = idetape_init_read(drive);
  1434. if (rc < 0)
  1435. return rc;
  1436. if (count == 0)
  1437. return (0);
  1438. if (tape->merge_bh_size) {
  1439. actually_read = min((unsigned int)(tape->merge_bh_size),
  1440. (unsigned int)count);
  1441. if (idetape_copy_stage_to_user(tape, buf, actually_read))
  1442. ret = -EFAULT;
  1443. buf += actually_read;
  1444. tape->merge_bh_size -= actually_read;
  1445. count -= actually_read;
  1446. }
  1447. while (count >= tape->buffer_size) {
  1448. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1449. if (bytes_read <= 0)
  1450. goto finish;
  1451. if (idetape_copy_stage_to_user(tape, buf, bytes_read))
  1452. ret = -EFAULT;
  1453. buf += bytes_read;
  1454. count -= bytes_read;
  1455. actually_read += bytes_read;
  1456. }
  1457. if (count) {
  1458. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1459. if (bytes_read <= 0)
  1460. goto finish;
  1461. temp = min((unsigned long)count, (unsigned long)bytes_read);
  1462. if (idetape_copy_stage_to_user(tape, buf, temp))
  1463. ret = -EFAULT;
  1464. actually_read += temp;
  1465. tape->merge_bh_size = bytes_read-temp;
  1466. }
  1467. finish:
  1468. if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
  1469. debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
  1470. idetape_space_over_filemarks(drive, MTFSF, 1);
  1471. return 0;
  1472. }
  1473. return ret ? ret : actually_read;
  1474. }
  1475. static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
  1476. size_t count, loff_t *ppos)
  1477. {
  1478. struct ide_tape_obj *tape = ide_tape_f(file);
  1479. ide_drive_t *drive = tape->drive;
  1480. ssize_t actually_written = 0;
  1481. ssize_t ret = 0;
  1482. u16 ctl = *(u16 *)&tape->caps[12];
  1483. /* The drive is write protected. */
  1484. if (tape->write_prot)
  1485. return -EACCES;
  1486. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1487. /* Initialize write operation */
  1488. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1489. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1490. ide_tape_discard_merge_buffer(drive, 1);
  1491. if (tape->merge_bh || tape->merge_bh_size) {
  1492. printk(KERN_ERR "ide-tape: merge_bh_size "
  1493. "should be 0 now\n");
  1494. tape->merge_bh_size = 0;
  1495. }
  1496. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
  1497. if (!tape->merge_bh)
  1498. return -ENOMEM;
  1499. tape->chrdev_dir = IDETAPE_DIR_WRITE;
  1500. idetape_init_merge_buffer(tape);
  1501. /*
  1502. * Issue a write 0 command to ensure that DSC handshake is
  1503. * switched from completion mode to buffer available mode. No
  1504. * point in issuing this if DSC overlap isn't supported, some
  1505. * drives (Seagate STT3401A) will return an error.
  1506. */
  1507. if (drive->dsc_overlap) {
  1508. ssize_t retval = idetape_queue_rw_tail(drive,
  1509. REQ_IDETAPE_WRITE, 0,
  1510. tape->merge_bh);
  1511. if (retval < 0) {
  1512. ide_tape_kfree_buffer(tape);
  1513. tape->merge_bh = NULL;
  1514. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1515. return retval;
  1516. }
  1517. }
  1518. }
  1519. if (count == 0)
  1520. return (0);
  1521. if (tape->merge_bh_size) {
  1522. if (tape->merge_bh_size >= tape->buffer_size) {
  1523. printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
  1524. tape->merge_bh_size = 0;
  1525. }
  1526. actually_written = min((unsigned int)
  1527. (tape->buffer_size - tape->merge_bh_size),
  1528. (unsigned int)count);
  1529. if (idetape_copy_stage_from_user(tape, buf, actually_written))
  1530. ret = -EFAULT;
  1531. buf += actually_written;
  1532. tape->merge_bh_size += actually_written;
  1533. count -= actually_written;
  1534. if (tape->merge_bh_size == tape->buffer_size) {
  1535. ssize_t retval;
  1536. tape->merge_bh_size = 0;
  1537. retval = idetape_add_chrdev_write_request(drive, ctl);
  1538. if (retval <= 0)
  1539. return (retval);
  1540. }
  1541. }
  1542. while (count >= tape->buffer_size) {
  1543. ssize_t retval;
  1544. if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
  1545. ret = -EFAULT;
  1546. buf += tape->buffer_size;
  1547. count -= tape->buffer_size;
  1548. retval = idetape_add_chrdev_write_request(drive, ctl);
  1549. actually_written += tape->buffer_size;
  1550. if (retval <= 0)
  1551. return (retval);
  1552. }
  1553. if (count) {
  1554. actually_written += count;
  1555. if (idetape_copy_stage_from_user(tape, buf, count))
  1556. ret = -EFAULT;
  1557. tape->merge_bh_size += count;
  1558. }
  1559. return ret ? ret : actually_written;
  1560. }
  1561. static int idetape_write_filemark(ide_drive_t *drive)
  1562. {
  1563. struct ide_tape_obj *tape = drive->driver_data;
  1564. struct ide_atapi_pc pc;
  1565. /* Write a filemark */
  1566. idetape_create_write_filemark_cmd(drive, &pc, 1);
  1567. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1568. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  1569. return -EIO;
  1570. }
  1571. return 0;
  1572. }
  1573. /*
  1574. * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
  1575. * requested.
  1576. *
  1577. * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
  1578. * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
  1579. * usually not supported.
  1580. *
  1581. * The following commands are currently not supported:
  1582. *
  1583. * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
  1584. * MT_ST_WRITE_THRESHOLD.
  1585. */
  1586. static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
  1587. {
  1588. idetape_tape_t *tape = drive->driver_data;
  1589. struct gendisk *disk = tape->disk;
  1590. struct ide_atapi_pc pc;
  1591. int i, retval;
  1592. debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
  1593. mt_op, mt_count);
  1594. switch (mt_op) {
  1595. case MTFSF:
  1596. case MTFSFM:
  1597. case MTBSF:
  1598. case MTBSFM:
  1599. if (!mt_count)
  1600. return 0;
  1601. return idetape_space_over_filemarks(drive, mt_op, mt_count);
  1602. default:
  1603. break;
  1604. }
  1605. switch (mt_op) {
  1606. case MTWEOF:
  1607. if (tape->write_prot)
  1608. return -EACCES;
  1609. ide_tape_discard_merge_buffer(drive, 1);
  1610. for (i = 0; i < mt_count; i++) {
  1611. retval = idetape_write_filemark(drive);
  1612. if (retval)
  1613. return retval;
  1614. }
  1615. return 0;
  1616. case MTREW:
  1617. ide_tape_discard_merge_buffer(drive, 0);
  1618. if (idetape_rewind_tape(drive))
  1619. return -EIO;
  1620. return 0;
  1621. case MTLOAD:
  1622. ide_tape_discard_merge_buffer(drive, 0);
  1623. idetape_create_load_unload_cmd(drive, &pc,
  1624. IDETAPE_LU_LOAD_MASK);
  1625. return ide_queue_pc_tail(drive, disk, &pc);
  1626. case MTUNLOAD:
  1627. case MTOFFL:
  1628. /*
  1629. * If door is locked, attempt to unlock before
  1630. * attempting to eject.
  1631. */
  1632. if (tape->door_locked) {
  1633. if (!ide_set_media_lock(drive, disk, 0))
  1634. tape->door_locked = DOOR_UNLOCKED;
  1635. }
  1636. ide_tape_discard_merge_buffer(drive, 0);
  1637. idetape_create_load_unload_cmd(drive, &pc,
  1638. !IDETAPE_LU_LOAD_MASK);
  1639. retval = ide_queue_pc_tail(drive, disk, &pc);
  1640. if (!retval)
  1641. clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
  1642. return retval;
  1643. case MTNOP:
  1644. ide_tape_discard_merge_buffer(drive, 0);
  1645. return idetape_flush_tape_buffers(drive);
  1646. case MTRETEN:
  1647. ide_tape_discard_merge_buffer(drive, 0);
  1648. idetape_create_load_unload_cmd(drive, &pc,
  1649. IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  1650. return ide_queue_pc_tail(drive, disk, &pc);
  1651. case MTEOM:
  1652. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  1653. return ide_queue_pc_tail(drive, disk, &pc);
  1654. case MTERASE:
  1655. (void)idetape_rewind_tape(drive);
  1656. idetape_create_erase_cmd(&pc);
  1657. return ide_queue_pc_tail(drive, disk, &pc);
  1658. case MTSETBLK:
  1659. if (mt_count) {
  1660. if (mt_count < tape->blk_size ||
  1661. mt_count % tape->blk_size)
  1662. return -EIO;
  1663. tape->user_bs_factor = mt_count / tape->blk_size;
  1664. clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
  1665. } else
  1666. set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
  1667. return 0;
  1668. case MTSEEK:
  1669. ide_tape_discard_merge_buffer(drive, 0);
  1670. return idetape_position_tape(drive,
  1671. mt_count * tape->user_bs_factor, tape->partition, 0);
  1672. case MTSETPART:
  1673. ide_tape_discard_merge_buffer(drive, 0);
  1674. return idetape_position_tape(drive, 0, mt_count, 0);
  1675. case MTFSR:
  1676. case MTBSR:
  1677. case MTLOCK:
  1678. retval = ide_set_media_lock(drive, disk, 1);
  1679. if (retval)
  1680. return retval;
  1681. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  1682. return 0;
  1683. case MTUNLOCK:
  1684. retval = ide_set_media_lock(drive, disk, 0);
  1685. if (retval)
  1686. return retval;
  1687. tape->door_locked = DOOR_UNLOCKED;
  1688. return 0;
  1689. default:
  1690. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1691. mt_op);
  1692. return -EIO;
  1693. }
  1694. }
  1695. /*
  1696. * Our character device ioctls. General mtio.h magnetic io commands are
  1697. * supported here, and not in the corresponding block interface. Our own
  1698. * ide-tape ioctls are supported on both interfaces.
  1699. */
  1700. static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
  1701. unsigned int cmd, unsigned long arg)
  1702. {
  1703. struct ide_tape_obj *tape = ide_tape_f(file);
  1704. ide_drive_t *drive = tape->drive;
  1705. struct mtop mtop;
  1706. struct mtget mtget;
  1707. struct mtpos mtpos;
  1708. int block_offset = 0, position = tape->first_frame;
  1709. void __user *argp = (void __user *)arg;
  1710. debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
  1711. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1712. ide_tape_flush_merge_buffer(drive);
  1713. idetape_flush_tape_buffers(drive);
  1714. }
  1715. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  1716. block_offset = tape->merge_bh_size /
  1717. (tape->blk_size * tape->user_bs_factor);
  1718. position = idetape_read_position(drive);
  1719. if (position < 0)
  1720. return -EIO;
  1721. }
  1722. switch (cmd) {
  1723. case MTIOCTOP:
  1724. if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
  1725. return -EFAULT;
  1726. return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
  1727. case MTIOCGET:
  1728. memset(&mtget, 0, sizeof(struct mtget));
  1729. mtget.mt_type = MT_ISSCSI2;
  1730. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  1731. mtget.mt_dsreg =
  1732. ((tape->blk_size * tape->user_bs_factor)
  1733. << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  1734. if (tape->drv_write_prot)
  1735. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  1736. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  1737. return -EFAULT;
  1738. return 0;
  1739. case MTIOCPOS:
  1740. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  1741. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  1742. return -EFAULT;
  1743. return 0;
  1744. default:
  1745. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1746. ide_tape_discard_merge_buffer(drive, 1);
  1747. return idetape_blkdev_ioctl(drive, cmd, arg);
  1748. }
  1749. }
  1750. /*
  1751. * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
  1752. * block size with the reported value.
  1753. */
  1754. static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
  1755. {
  1756. idetape_tape_t *tape = drive->driver_data;
  1757. struct ide_atapi_pc pc;
  1758. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  1759. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1760. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  1761. if (tape->blk_size == 0) {
  1762. printk(KERN_WARNING "ide-tape: Cannot deal with zero "
  1763. "block size, assuming 32k\n");
  1764. tape->blk_size = 32768;
  1765. }
  1766. return;
  1767. }
  1768. tape->blk_size = (pc.buf[4 + 5] << 16) +
  1769. (pc.buf[4 + 6] << 8) +
  1770. pc.buf[4 + 7];
  1771. tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
  1772. }
  1773. static int idetape_chrdev_open(struct inode *inode, struct file *filp)
  1774. {
  1775. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  1776. ide_drive_t *drive;
  1777. idetape_tape_t *tape;
  1778. int retval;
  1779. if (i >= MAX_HWIFS * MAX_DRIVES)
  1780. return -ENXIO;
  1781. lock_kernel();
  1782. tape = ide_tape_chrdev_get(i);
  1783. if (!tape) {
  1784. unlock_kernel();
  1785. return -ENXIO;
  1786. }
  1787. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1788. /*
  1789. * We really want to do nonseekable_open(inode, filp); here, but some
  1790. * versions of tar incorrectly call lseek on tapes and bail out if that
  1791. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  1792. */
  1793. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  1794. drive = tape->drive;
  1795. filp->private_data = tape;
  1796. if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
  1797. retval = -EBUSY;
  1798. goto out_put_tape;
  1799. }
  1800. retval = idetape_wait_ready(drive, 60 * HZ);
  1801. if (retval) {
  1802. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1803. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  1804. goto out_put_tape;
  1805. }
  1806. idetape_read_position(drive);
  1807. if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
  1808. (void)idetape_rewind_tape(drive);
  1809. /* Read block size and write protect status from drive. */
  1810. ide_tape_get_bsize_from_bdesc(drive);
  1811. /* Set write protect flag if device is opened as read-only. */
  1812. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  1813. tape->write_prot = 1;
  1814. else
  1815. tape->write_prot = tape->drv_write_prot;
  1816. /* Make sure drive isn't write protected if user wants to write. */
  1817. if (tape->write_prot) {
  1818. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  1819. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  1820. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1821. retval = -EROFS;
  1822. goto out_put_tape;
  1823. }
  1824. }
  1825. /* Lock the tape drive door so user can't eject. */
  1826. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1827. if (!ide_set_media_lock(drive, tape->disk, 1)) {
  1828. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  1829. tape->door_locked = DOOR_LOCKED;
  1830. }
  1831. }
  1832. unlock_kernel();
  1833. return 0;
  1834. out_put_tape:
  1835. ide_tape_put(tape);
  1836. unlock_kernel();
  1837. return retval;
  1838. }
  1839. static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
  1840. {
  1841. idetape_tape_t *tape = drive->driver_data;
  1842. ide_tape_flush_merge_buffer(drive);
  1843. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
  1844. if (tape->merge_bh != NULL) {
  1845. idetape_pad_zeros(drive, tape->blk_size *
  1846. (tape->user_bs_factor - 1));
  1847. ide_tape_kfree_buffer(tape);
  1848. tape->merge_bh = NULL;
  1849. }
  1850. idetape_write_filemark(drive);
  1851. idetape_flush_tape_buffers(drive);
  1852. idetape_flush_tape_buffers(drive);
  1853. }
  1854. static int idetape_chrdev_release(struct inode *inode, struct file *filp)
  1855. {
  1856. struct ide_tape_obj *tape = ide_tape_f(filp);
  1857. ide_drive_t *drive = tape->drive;
  1858. unsigned int minor = iminor(inode);
  1859. lock_kernel();
  1860. tape = drive->driver_data;
  1861. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1862. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  1863. idetape_write_release(drive, minor);
  1864. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1865. if (minor < 128)
  1866. ide_tape_discard_merge_buffer(drive, 1);
  1867. }
  1868. if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
  1869. (void) idetape_rewind_tape(drive);
  1870. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1871. if (tape->door_locked == DOOR_LOCKED) {
  1872. if (!ide_set_media_lock(drive, tape->disk, 0))
  1873. tape->door_locked = DOOR_UNLOCKED;
  1874. }
  1875. }
  1876. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1877. ide_tape_put(tape);
  1878. unlock_kernel();
  1879. return 0;
  1880. }
  1881. static void idetape_get_inquiry_results(ide_drive_t *drive)
  1882. {
  1883. idetape_tape_t *tape = drive->driver_data;
  1884. struct ide_atapi_pc pc;
  1885. char fw_rev[4], vendor_id[8], product_id[16];
  1886. idetape_create_inquiry_cmd(&pc);
  1887. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1888. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
  1889. tape->name);
  1890. return;
  1891. }
  1892. memcpy(vendor_id, &pc.buf[8], 8);
  1893. memcpy(product_id, &pc.buf[16], 16);
  1894. memcpy(fw_rev, &pc.buf[32], 4);
  1895. ide_fixstring(vendor_id, 8, 0);
  1896. ide_fixstring(product_id, 16, 0);
  1897. ide_fixstring(fw_rev, 4, 0);
  1898. printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
  1899. drive->name, tape->name, vendor_id, product_id, fw_rev);
  1900. }
  1901. /*
  1902. * Ask the tape about its various parameters. In particular, we will adjust our
  1903. * data transfer buffer size to the recommended value as returned by the tape.
  1904. */
  1905. static void idetape_get_mode_sense_results(ide_drive_t *drive)
  1906. {
  1907. idetape_tape_t *tape = drive->driver_data;
  1908. struct ide_atapi_pc pc;
  1909. u8 *caps;
  1910. u8 speed, max_speed;
  1911. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  1912. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1913. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
  1914. " some default values\n");
  1915. tape->blk_size = 512;
  1916. put_unaligned(52, (u16 *)&tape->caps[12]);
  1917. put_unaligned(540, (u16 *)&tape->caps[14]);
  1918. put_unaligned(6*52, (u16 *)&tape->caps[16]);
  1919. return;
  1920. }
  1921. caps = pc.buf + 4 + pc.buf[3];
  1922. /* convert to host order and save for later use */
  1923. speed = be16_to_cpup((__be16 *)&caps[14]);
  1924. max_speed = be16_to_cpup((__be16 *)&caps[8]);
  1925. *(u16 *)&caps[8] = max_speed;
  1926. *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
  1927. *(u16 *)&caps[14] = speed;
  1928. *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
  1929. if (!speed) {
  1930. printk(KERN_INFO "ide-tape: %s: invalid tape speed "
  1931. "(assuming 650KB/sec)\n", drive->name);
  1932. *(u16 *)&caps[14] = 650;
  1933. }
  1934. if (!max_speed) {
  1935. printk(KERN_INFO "ide-tape: %s: invalid max_speed "
  1936. "(assuming 650KB/sec)\n", drive->name);
  1937. *(u16 *)&caps[8] = 650;
  1938. }
  1939. memcpy(&tape->caps, caps, 20);
  1940. /* device lacks locking support according to capabilities page */
  1941. if ((caps[6] & 1) == 0)
  1942. drive->atapi_flags |= IDE_AFLAG_NO_DOORLOCK;
  1943. if (caps[7] & 0x02)
  1944. tape->blk_size = 512;
  1945. else if (caps[7] & 0x04)
  1946. tape->blk_size = 1024;
  1947. }
  1948. #ifdef CONFIG_IDE_PROC_FS
  1949. #define ide_tape_devset_get(name, field) \
  1950. static int get_##name(ide_drive_t *drive) \
  1951. { \
  1952. idetape_tape_t *tape = drive->driver_data; \
  1953. return tape->field; \
  1954. }
  1955. #define ide_tape_devset_set(name, field) \
  1956. static int set_##name(ide_drive_t *drive, int arg) \
  1957. { \
  1958. idetape_tape_t *tape = drive->driver_data; \
  1959. tape->field = arg; \
  1960. return 0; \
  1961. }
  1962. #define ide_tape_devset_rw(_name, _min, _max, _field, _mulf, _divf) \
  1963. ide_tape_devset_get(_name, _field) \
  1964. ide_tape_devset_set(_name, _field) \
  1965. __IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name, _mulf, _divf)
  1966. #define ide_tape_devset_r(_name, _min, _max, _field, _mulf, _divf) \
  1967. ide_tape_devset_get(_name, _field) \
  1968. __IDE_DEVSET(_name, S_READ, _min, _max, get_##_name, NULL, _mulf, _divf)
  1969. static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
  1970. static int divf_tdsc(ide_drive_t *drive) { return HZ; }
  1971. static int divf_buffer(ide_drive_t *drive) { return 2; }
  1972. static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
  1973. ide_devset_rw(dsc_overlap, 0, 1, dsc_overlap);
  1974. ide_tape_devset_rw(debug_mask, 0, 0xffff, debug_mask, NULL, NULL);
  1975. ide_tape_devset_rw(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
  1976. best_dsc_rw_freq, mulf_tdsc, divf_tdsc);
  1977. ide_tape_devset_r(avg_speed, 0, 0xffff, avg_speed, NULL, NULL);
  1978. ide_tape_devset_r(speed, 0, 0xffff, caps[14], NULL, NULL);
  1979. ide_tape_devset_r(buffer, 0, 0xffff, caps[16], NULL, divf_buffer);
  1980. ide_tape_devset_r(buffer_size, 0, 0xffff, buffer_size, NULL, divf_buffer_size);
  1981. static const struct ide_devset *idetape_settings[] = {
  1982. &ide_devset_avg_speed,
  1983. &ide_devset_buffer,
  1984. &ide_devset_buffer_size,
  1985. &ide_devset_debug_mask,
  1986. &ide_devset_dsc_overlap,
  1987. &ide_devset_speed,
  1988. &ide_devset_tdsc,
  1989. NULL
  1990. };
  1991. #endif
  1992. /*
  1993. * The function below is called to:
  1994. *
  1995. * 1. Initialize our various state variables.
  1996. * 2. Ask the tape for its capabilities.
  1997. * 3. Allocate a buffer which will be used for data transfer. The buffer size
  1998. * is chosen based on the recommendation which we received in step 2.
  1999. *
  2000. * Note that at this point ide.c already assigned us an irq, so that we can
  2001. * queue requests here and wait for their completion.
  2002. */
  2003. static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
  2004. {
  2005. unsigned long t;
  2006. int speed;
  2007. int buffer_size;
  2008. u8 gcw[2];
  2009. u16 *ctl = (u16 *)&tape->caps[12];
  2010. drive->pc_callback = ide_tape_callback;
  2011. spin_lock_init(&tape->lock);
  2012. drive->dsc_overlap = 1;
  2013. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  2014. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  2015. tape->name);
  2016. drive->dsc_overlap = 0;
  2017. }
  2018. /* Seagate Travan drives do not support DSC overlap. */
  2019. if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
  2020. drive->dsc_overlap = 0;
  2021. tape->minor = minor;
  2022. tape->name[0] = 'h';
  2023. tape->name[1] = 't';
  2024. tape->name[2] = '0' + minor;
  2025. tape->chrdev_dir = IDETAPE_DIR_NONE;
  2026. *((u16 *)&gcw) = drive->id[ATA_ID_CONFIG];
  2027. /* Command packet DRQ type */
  2028. if (((gcw[0] & 0x60) >> 5) == 1)
  2029. set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
  2030. idetape_get_inquiry_results(drive);
  2031. idetape_get_mode_sense_results(drive);
  2032. ide_tape_get_bsize_from_bdesc(drive);
  2033. tape->user_bs_factor = 1;
  2034. tape->buffer_size = *ctl * tape->blk_size;
  2035. while (tape->buffer_size > 0xffff) {
  2036. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  2037. *ctl /= 2;
  2038. tape->buffer_size = *ctl * tape->blk_size;
  2039. }
  2040. buffer_size = tape->buffer_size;
  2041. tape->pages_per_buffer = buffer_size / PAGE_SIZE;
  2042. if (buffer_size % PAGE_SIZE) {
  2043. tape->pages_per_buffer++;
  2044. tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
  2045. }
  2046. /* select the "best" DSC read/write polling freq */
  2047. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  2048. t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
  2049. /*
  2050. * Ensure that the number we got makes sense; limit it within
  2051. * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  2052. */
  2053. tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
  2054. IDETAPE_DSC_RW_MAX);
  2055. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  2056. "%lums tDSC%s\n",
  2057. drive->name, tape->name, *(u16 *)&tape->caps[14],
  2058. (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
  2059. tape->buffer_size / 1024,
  2060. tape->best_dsc_rw_freq * 1000 / HZ,
  2061. drive->using_dma ? ", DMA":"");
  2062. ide_proc_register_driver(drive, tape->driver);
  2063. }
  2064. static void ide_tape_remove(ide_drive_t *drive)
  2065. {
  2066. idetape_tape_t *tape = drive->driver_data;
  2067. ide_proc_unregister_driver(drive, tape->driver);
  2068. ide_unregister_region(tape->disk);
  2069. ide_tape_put(tape);
  2070. }
  2071. static void ide_tape_release(struct kref *kref)
  2072. {
  2073. struct ide_tape_obj *tape = to_ide_tape(kref);
  2074. ide_drive_t *drive = tape->drive;
  2075. struct gendisk *g = tape->disk;
  2076. BUG_ON(tape->merge_bh_size);
  2077. drive->dsc_overlap = 0;
  2078. drive->driver_data = NULL;
  2079. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  2080. device_destroy(idetape_sysfs_class,
  2081. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  2082. idetape_devs[tape->minor] = NULL;
  2083. g->private_data = NULL;
  2084. put_disk(g);
  2085. kfree(tape);
  2086. }
  2087. #ifdef CONFIG_IDE_PROC_FS
  2088. static int proc_idetape_read_name
  2089. (char *page, char **start, off_t off, int count, int *eof, void *data)
  2090. {
  2091. ide_drive_t *drive = (ide_drive_t *) data;
  2092. idetape_tape_t *tape = drive->driver_data;
  2093. char *out = page;
  2094. int len;
  2095. len = sprintf(out, "%s\n", tape->name);
  2096. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  2097. }
  2098. static ide_proc_entry_t idetape_proc[] = {
  2099. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  2100. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  2101. { NULL, 0, NULL, NULL }
  2102. };
  2103. #endif
  2104. static int ide_tape_probe(ide_drive_t *);
  2105. static ide_driver_t idetape_driver = {
  2106. .gen_driver = {
  2107. .owner = THIS_MODULE,
  2108. .name = "ide-tape",
  2109. .bus = &ide_bus_type,
  2110. },
  2111. .probe = ide_tape_probe,
  2112. .remove = ide_tape_remove,
  2113. .version = IDETAPE_VERSION,
  2114. .media = ide_tape,
  2115. .do_request = idetape_do_request,
  2116. .end_request = idetape_end_request,
  2117. .error = __ide_error,
  2118. #ifdef CONFIG_IDE_PROC_FS
  2119. .proc = idetape_proc,
  2120. .settings = idetape_settings,
  2121. #endif
  2122. };
  2123. /* Our character device supporting functions, passed to register_chrdev. */
  2124. static const struct file_operations idetape_fops = {
  2125. .owner = THIS_MODULE,
  2126. .read = idetape_chrdev_read,
  2127. .write = idetape_chrdev_write,
  2128. .ioctl = idetape_chrdev_ioctl,
  2129. .open = idetape_chrdev_open,
  2130. .release = idetape_chrdev_release,
  2131. };
  2132. static int idetape_open(struct inode *inode, struct file *filp)
  2133. {
  2134. struct gendisk *disk = inode->i_bdev->bd_disk;
  2135. struct ide_tape_obj *tape;
  2136. tape = ide_tape_get(disk);
  2137. if (!tape)
  2138. return -ENXIO;
  2139. return 0;
  2140. }
  2141. static int idetape_release(struct inode *inode, struct file *filp)
  2142. {
  2143. struct gendisk *disk = inode->i_bdev->bd_disk;
  2144. struct ide_tape_obj *tape = ide_tape_g(disk);
  2145. ide_tape_put(tape);
  2146. return 0;
  2147. }
  2148. static int idetape_ioctl(struct inode *inode, struct file *file,
  2149. unsigned int cmd, unsigned long arg)
  2150. {
  2151. struct block_device *bdev = inode->i_bdev;
  2152. struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
  2153. ide_drive_t *drive = tape->drive;
  2154. int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  2155. if (err == -EINVAL)
  2156. err = idetape_blkdev_ioctl(drive, cmd, arg);
  2157. return err;
  2158. }
  2159. static struct block_device_operations idetape_block_ops = {
  2160. .owner = THIS_MODULE,
  2161. .open = idetape_open,
  2162. .release = idetape_release,
  2163. .ioctl = idetape_ioctl,
  2164. };
  2165. static int ide_tape_probe(ide_drive_t *drive)
  2166. {
  2167. idetape_tape_t *tape;
  2168. struct gendisk *g;
  2169. int minor;
  2170. if (!strstr("ide-tape", drive->driver_req))
  2171. goto failed;
  2172. if (drive->media != ide_tape)
  2173. goto failed;
  2174. if (drive->id_read == 1 && !ide_check_atapi_device(drive, DRV_NAME)) {
  2175. printk(KERN_ERR "ide-tape: %s: not supported by this version of"
  2176. " the driver\n", drive->name);
  2177. goto failed;
  2178. }
  2179. tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
  2180. if (tape == NULL) {
  2181. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
  2182. drive->name);
  2183. goto failed;
  2184. }
  2185. g = alloc_disk(1 << PARTN_BITS);
  2186. if (!g)
  2187. goto out_free_tape;
  2188. ide_init_disk(g, drive);
  2189. kref_init(&tape->kref);
  2190. tape->drive = drive;
  2191. tape->driver = &idetape_driver;
  2192. tape->disk = g;
  2193. g->private_data = &tape->driver;
  2194. drive->driver_data = tape;
  2195. mutex_lock(&idetape_ref_mutex);
  2196. for (minor = 0; idetape_devs[minor]; minor++)
  2197. ;
  2198. idetape_devs[minor] = tape;
  2199. mutex_unlock(&idetape_ref_mutex);
  2200. idetape_setup(drive, tape, minor);
  2201. device_create_drvdata(idetape_sysfs_class, &drive->gendev,
  2202. MKDEV(IDETAPE_MAJOR, minor), NULL,
  2203. "%s", tape->name);
  2204. device_create_drvdata(idetape_sysfs_class, &drive->gendev,
  2205. MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
  2206. "n%s", tape->name);
  2207. g->fops = &idetape_block_ops;
  2208. ide_register_region(g);
  2209. return 0;
  2210. out_free_tape:
  2211. kfree(tape);
  2212. failed:
  2213. return -ENODEV;
  2214. }
  2215. static void __exit idetape_exit(void)
  2216. {
  2217. driver_unregister(&idetape_driver.gen_driver);
  2218. class_destroy(idetape_sysfs_class);
  2219. unregister_chrdev(IDETAPE_MAJOR, "ht");
  2220. }
  2221. static int __init idetape_init(void)
  2222. {
  2223. int error = 1;
  2224. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  2225. if (IS_ERR(idetape_sysfs_class)) {
  2226. idetape_sysfs_class = NULL;
  2227. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  2228. error = -EBUSY;
  2229. goto out;
  2230. }
  2231. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  2232. printk(KERN_ERR "ide-tape: Failed to register chrdev"
  2233. " interface\n");
  2234. error = -EBUSY;
  2235. goto out_free_class;
  2236. }
  2237. error = driver_register(&idetape_driver.gen_driver);
  2238. if (error)
  2239. goto out_free_driver;
  2240. return 0;
  2241. out_free_driver:
  2242. driver_unregister(&idetape_driver.gen_driver);
  2243. out_free_class:
  2244. class_destroy(idetape_sysfs_class);
  2245. out:
  2246. return error;
  2247. }
  2248. MODULE_ALIAS("ide:*m-tape*");
  2249. module_init(idetape_init);
  2250. module_exit(idetape_exit);
  2251. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
  2252. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  2253. MODULE_LICENSE("GPL");