ide-tape.c 66 KB

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