ide-tape.c 67 KB

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