ide-tape.c 66 KB

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