ide-tape.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  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);
  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) || blk_sense_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->special;
  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. /* prepare sense request for this command */
  733. ide_prep_sense(drive, rq);
  734. memset(&cmd, 0, sizeof(cmd));
  735. if (rq_data_dir(rq))
  736. cmd.tf_flags |= IDE_TFLAG_WRITE;
  737. cmd.rq = rq;
  738. ide_init_sg_cmd(&cmd, pc->req_xfer);
  739. ide_map_sg(drive, &cmd);
  740. return ide_tape_issue_pc(drive, &cmd, pc);
  741. }
  742. /*
  743. * The function below uses __get_free_pages to allocate a data buffer of size
  744. * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
  745. * much as possible.
  746. *
  747. * It returns a pointer to the newly allocated buffer, or NULL in case of
  748. * failure.
  749. */
  750. static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
  751. int full, int clear)
  752. {
  753. struct idetape_bh *prev_bh, *bh, *merge_bh;
  754. int pages = tape->pages_per_buffer;
  755. unsigned int order, b_allocd;
  756. char *b_data = NULL;
  757. merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  758. bh = merge_bh;
  759. if (bh == NULL)
  760. goto abort;
  761. order = fls(pages) - 1;
  762. bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
  763. if (!bh->b_data)
  764. goto abort;
  765. b_allocd = (1 << order) * PAGE_SIZE;
  766. pages &= (order-1);
  767. if (clear)
  768. memset(bh->b_data, 0, b_allocd);
  769. bh->b_reqnext = NULL;
  770. bh->b_size = b_allocd;
  771. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  772. while (pages) {
  773. order = fls(pages) - 1;
  774. b_data = (char *) __get_free_pages(GFP_KERNEL, order);
  775. if (!b_data)
  776. goto abort;
  777. b_allocd = (1 << order) * PAGE_SIZE;
  778. if (clear)
  779. memset(b_data, 0, b_allocd);
  780. /* newly allocated page frames below buffer header or ...*/
  781. if (bh->b_data == b_data + b_allocd) {
  782. bh->b_size += b_allocd;
  783. bh->b_data -= b_allocd;
  784. if (full)
  785. atomic_add(b_allocd, &bh->b_count);
  786. continue;
  787. }
  788. /* they are above the header */
  789. if (b_data == bh->b_data + bh->b_size) {
  790. bh->b_size += b_allocd;
  791. if (full)
  792. atomic_add(b_allocd, &bh->b_count);
  793. continue;
  794. }
  795. prev_bh = bh;
  796. bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  797. if (!bh) {
  798. free_pages((unsigned long) b_data, order);
  799. goto abort;
  800. }
  801. bh->b_reqnext = NULL;
  802. bh->b_data = b_data;
  803. bh->b_size = b_allocd;
  804. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  805. prev_bh->b_reqnext = bh;
  806. pages &= (order-1);
  807. }
  808. bh->b_size -= tape->excess_bh_size;
  809. if (full)
  810. atomic_sub(tape->excess_bh_size, &bh->b_count);
  811. return merge_bh;
  812. abort:
  813. ide_tape_kfree_buffer(tape);
  814. return NULL;
  815. }
  816. static int idetape_copy_stage_from_user(idetape_tape_t *tape,
  817. const char __user *buf, int n)
  818. {
  819. struct idetape_bh *bh = tape->bh;
  820. int count;
  821. int ret = 0;
  822. while (n) {
  823. if (bh == NULL) {
  824. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  825. __func__);
  826. return 1;
  827. }
  828. count = min((unsigned int)
  829. (bh->b_size - atomic_read(&bh->b_count)),
  830. (unsigned int)n);
  831. if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
  832. count))
  833. ret = 1;
  834. n -= count;
  835. atomic_add(count, &bh->b_count);
  836. buf += count;
  837. if (atomic_read(&bh->b_count) == bh->b_size) {
  838. bh = bh->b_reqnext;
  839. if (bh)
  840. atomic_set(&bh->b_count, 0);
  841. }
  842. }
  843. tape->bh = bh;
  844. return ret;
  845. }
  846. static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
  847. int n)
  848. {
  849. struct idetape_bh *bh = tape->bh;
  850. int count;
  851. int ret = 0;
  852. while (n) {
  853. if (bh == NULL) {
  854. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  855. __func__);
  856. return 1;
  857. }
  858. count = min(tape->b_count, n);
  859. if (copy_to_user(buf, tape->b_data, count))
  860. ret = 1;
  861. n -= count;
  862. tape->b_data += count;
  863. tape->b_count -= count;
  864. buf += count;
  865. if (!tape->b_count) {
  866. bh = bh->b_reqnext;
  867. tape->bh = bh;
  868. if (bh) {
  869. tape->b_data = bh->b_data;
  870. tape->b_count = atomic_read(&bh->b_count);
  871. }
  872. }
  873. }
  874. return ret;
  875. }
  876. static void idetape_init_merge_buffer(idetape_tape_t *tape)
  877. {
  878. struct idetape_bh *bh = tape->merge_bh;
  879. tape->bh = tape->merge_bh;
  880. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  881. atomic_set(&bh->b_count, 0);
  882. else {
  883. tape->b_data = bh->b_data;
  884. tape->b_count = atomic_read(&bh->b_count);
  885. }
  886. }
  887. /*
  888. * Write a filemark if write_filemark=1. Flush the device buffers without
  889. * writing a filemark otherwise.
  890. */
  891. static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
  892. struct ide_atapi_pc *pc, int write_filemark)
  893. {
  894. ide_init_pc(pc);
  895. pc->c[0] = WRITE_FILEMARKS;
  896. pc->c[4] = write_filemark;
  897. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  898. }
  899. static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
  900. {
  901. idetape_tape_t *tape = drive->driver_data;
  902. struct gendisk *disk = tape->disk;
  903. int load_attempted = 0;
  904. /* Wait for the tape to become ready */
  905. set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
  906. timeout += jiffies;
  907. while (time_before(jiffies, timeout)) {
  908. if (ide_do_test_unit_ready(drive, disk) == 0)
  909. return 0;
  910. if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
  911. || (tape->asc == 0x3A)) {
  912. /* no media */
  913. if (load_attempted)
  914. return -ENOMEDIUM;
  915. ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
  916. load_attempted = 1;
  917. /* not about to be ready */
  918. } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
  919. (tape->ascq == 1 || tape->ascq == 8)))
  920. return -EIO;
  921. msleep(100);
  922. }
  923. return -EIO;
  924. }
  925. static int idetape_flush_tape_buffers(ide_drive_t *drive)
  926. {
  927. struct ide_tape_obj *tape = drive->driver_data;
  928. struct ide_atapi_pc pc;
  929. int rc;
  930. idetape_create_write_filemark_cmd(drive, &pc, 0);
  931. rc = ide_queue_pc_tail(drive, tape->disk, &pc);
  932. if (rc)
  933. return rc;
  934. idetape_wait_ready(drive, 60 * 5 * HZ);
  935. return 0;
  936. }
  937. static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
  938. {
  939. ide_init_pc(pc);
  940. pc->c[0] = READ_POSITION;
  941. pc->req_xfer = 20;
  942. }
  943. static int idetape_read_position(ide_drive_t *drive)
  944. {
  945. idetape_tape_t *tape = drive->driver_data;
  946. struct ide_atapi_pc pc;
  947. int position;
  948. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  949. idetape_create_read_position_cmd(&pc);
  950. if (ide_queue_pc_tail(drive, tape->disk, &pc))
  951. return -1;
  952. position = tape->first_frame;
  953. return position;
  954. }
  955. static void idetape_create_locate_cmd(ide_drive_t *drive,
  956. struct ide_atapi_pc *pc,
  957. unsigned int block, u8 partition, int skip)
  958. {
  959. ide_init_pc(pc);
  960. pc->c[0] = POSITION_TO_ELEMENT;
  961. pc->c[1] = 2;
  962. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
  963. pc->c[8] = partition;
  964. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  965. }
  966. static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
  967. {
  968. idetape_tape_t *tape = drive->driver_data;
  969. if (tape->chrdev_dir != IDETAPE_DIR_READ)
  970. return;
  971. clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
  972. tape->merge_bh_size = 0;
  973. if (tape->merge_bh != NULL) {
  974. ide_tape_kfree_buffer(tape);
  975. tape->merge_bh = NULL;
  976. }
  977. tape->chrdev_dir = IDETAPE_DIR_NONE;
  978. }
  979. /*
  980. * Position the tape to the requested block using the LOCATE packet command.
  981. * A READ POSITION command is then issued to check where we are positioned. Like
  982. * all higher level operations, we queue the commands at the tail of the request
  983. * queue and wait for their completion.
  984. */
  985. static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
  986. u8 partition, int skip)
  987. {
  988. idetape_tape_t *tape = drive->driver_data;
  989. struct gendisk *disk = tape->disk;
  990. int retval;
  991. struct ide_atapi_pc pc;
  992. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  993. __ide_tape_discard_merge_buffer(drive);
  994. idetape_wait_ready(drive, 60 * 5 * HZ);
  995. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  996. retval = ide_queue_pc_tail(drive, disk, &pc);
  997. if (retval)
  998. return (retval);
  999. idetape_create_read_position_cmd(&pc);
  1000. return ide_queue_pc_tail(drive, disk, &pc);
  1001. }
  1002. static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
  1003. int restore_position)
  1004. {
  1005. idetape_tape_t *tape = drive->driver_data;
  1006. int seek, position;
  1007. __ide_tape_discard_merge_buffer(drive);
  1008. if (restore_position) {
  1009. position = idetape_read_position(drive);
  1010. seek = position > 0 ? position : 0;
  1011. if (idetape_position_tape(drive, seek, 0, 0)) {
  1012. printk(KERN_INFO "ide-tape: %s: position_tape failed in"
  1013. " %s\n", tape->name, __func__);
  1014. return;
  1015. }
  1016. }
  1017. }
  1018. /*
  1019. * Generate a read/write request for the block device interface and wait for it
  1020. * to be serviced.
  1021. */
  1022. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
  1023. struct idetape_bh *bh)
  1024. {
  1025. idetape_tape_t *tape = drive->driver_data;
  1026. struct request *rq;
  1027. int ret, errors;
  1028. debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
  1029. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  1030. rq->cmd_type = REQ_TYPE_SPECIAL;
  1031. rq->cmd[13] = cmd;
  1032. rq->rq_disk = tape->disk;
  1033. rq->special = (void *)bh;
  1034. rq->sector = tape->first_frame;
  1035. rq->nr_sectors = blocks;
  1036. rq->current_nr_sectors = blocks;
  1037. blk_execute_rq(drive->queue, tape->disk, rq, 0);
  1038. errors = rq->errors;
  1039. ret = tape->blk_size * (blocks - rq->current_nr_sectors);
  1040. blk_put_request(rq);
  1041. if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
  1042. return 0;
  1043. if (tape->merge_bh)
  1044. idetape_init_merge_buffer(tape);
  1045. if (errors == IDE_DRV_ERROR_GENERAL)
  1046. return -EIO;
  1047. return ret;
  1048. }
  1049. static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
  1050. {
  1051. ide_init_pc(pc);
  1052. pc->c[0] = INQUIRY;
  1053. pc->c[4] = 254;
  1054. pc->req_xfer = 254;
  1055. }
  1056. static void idetape_create_rewind_cmd(ide_drive_t *drive,
  1057. struct ide_atapi_pc *pc)
  1058. {
  1059. ide_init_pc(pc);
  1060. pc->c[0] = REZERO_UNIT;
  1061. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1062. }
  1063. static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
  1064. {
  1065. ide_init_pc(pc);
  1066. pc->c[0] = ERASE;
  1067. pc->c[1] = 1;
  1068. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1069. }
  1070. static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
  1071. {
  1072. ide_init_pc(pc);
  1073. pc->c[0] = SPACE;
  1074. put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
  1075. pc->c[1] = cmd;
  1076. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1077. }
  1078. /* Queue up a character device originated write request. */
  1079. static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
  1080. {
  1081. idetape_tape_t *tape = drive->driver_data;
  1082. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1083. return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
  1084. blocks, tape->merge_bh);
  1085. }
  1086. static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
  1087. {
  1088. idetape_tape_t *tape = drive->driver_data;
  1089. int blocks, min;
  1090. struct idetape_bh *bh;
  1091. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1092. printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
  1093. " but we are not writing.\n");
  1094. return;
  1095. }
  1096. if (tape->merge_bh_size > tape->buffer_size) {
  1097. printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
  1098. tape->merge_bh_size = tape->buffer_size;
  1099. }
  1100. if (tape->merge_bh_size) {
  1101. blocks = tape->merge_bh_size / tape->blk_size;
  1102. if (tape->merge_bh_size % tape->blk_size) {
  1103. unsigned int i;
  1104. blocks++;
  1105. i = tape->blk_size - tape->merge_bh_size %
  1106. tape->blk_size;
  1107. bh = tape->bh->b_reqnext;
  1108. while (bh) {
  1109. atomic_set(&bh->b_count, 0);
  1110. bh = bh->b_reqnext;
  1111. }
  1112. bh = tape->bh;
  1113. while (i) {
  1114. if (bh == NULL) {
  1115. printk(KERN_INFO "ide-tape: bug,"
  1116. " bh NULL\n");
  1117. break;
  1118. }
  1119. min = min(i, (unsigned int)(bh->b_size -
  1120. atomic_read(&bh->b_count)));
  1121. memset(bh->b_data + atomic_read(&bh->b_count),
  1122. 0, min);
  1123. atomic_add(min, &bh->b_count);
  1124. i -= min;
  1125. bh = bh->b_reqnext;
  1126. }
  1127. }
  1128. (void) idetape_add_chrdev_write_request(drive, blocks);
  1129. tape->merge_bh_size = 0;
  1130. }
  1131. if (tape->merge_bh != NULL) {
  1132. ide_tape_kfree_buffer(tape);
  1133. tape->merge_bh = NULL;
  1134. }
  1135. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1136. }
  1137. static int idetape_init_read(ide_drive_t *drive)
  1138. {
  1139. idetape_tape_t *tape = drive->driver_data;
  1140. int bytes_read;
  1141. /* Initialize read operation */
  1142. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1143. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1144. ide_tape_flush_merge_buffer(drive);
  1145. idetape_flush_tape_buffers(drive);
  1146. }
  1147. if (tape->merge_bh || tape->merge_bh_size) {
  1148. printk(KERN_ERR "ide-tape: merge_bh_size should be"
  1149. " 0 now\n");
  1150. tape->merge_bh_size = 0;
  1151. }
  1152. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
  1153. if (!tape->merge_bh)
  1154. return -ENOMEM;
  1155. tape->chrdev_dir = IDETAPE_DIR_READ;
  1156. /*
  1157. * Issue a read 0 command to ensure that DSC handshake is
  1158. * switched from completion mode to buffer available mode.
  1159. * No point in issuing this if DSC overlap isn't supported, some
  1160. * drives (Seagate STT3401A) will return an error.
  1161. */
  1162. if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
  1163. bytes_read = idetape_queue_rw_tail(drive,
  1164. REQ_IDETAPE_READ, 0,
  1165. tape->merge_bh);
  1166. if (bytes_read < 0) {
  1167. ide_tape_kfree_buffer(tape);
  1168. tape->merge_bh = NULL;
  1169. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1170. return bytes_read;
  1171. }
  1172. }
  1173. }
  1174. return 0;
  1175. }
  1176. /* called from idetape_chrdev_read() to service a chrdev read request. */
  1177. static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
  1178. {
  1179. idetape_tape_t *tape = drive->driver_data;
  1180. debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
  1181. /* If we are at a filemark, return a read length of 0 */
  1182. if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
  1183. return 0;
  1184. idetape_init_read(drive);
  1185. return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
  1186. tape->merge_bh);
  1187. }
  1188. static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
  1189. {
  1190. idetape_tape_t *tape = drive->driver_data;
  1191. struct idetape_bh *bh;
  1192. int blocks;
  1193. while (bcount) {
  1194. unsigned int count;
  1195. bh = tape->merge_bh;
  1196. count = min(tape->buffer_size, bcount);
  1197. bcount -= count;
  1198. blocks = count / tape->blk_size;
  1199. while (count) {
  1200. atomic_set(&bh->b_count,
  1201. min(count, (unsigned int)bh->b_size));
  1202. memset(bh->b_data, 0, atomic_read(&bh->b_count));
  1203. count -= atomic_read(&bh->b_count);
  1204. bh = bh->b_reqnext;
  1205. }
  1206. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
  1207. tape->merge_bh);
  1208. }
  1209. }
  1210. /*
  1211. * Rewinds the tape to the Beginning Of the current Partition (BOP). We
  1212. * currently support only one partition.
  1213. */
  1214. static int idetape_rewind_tape(ide_drive_t *drive)
  1215. {
  1216. struct ide_tape_obj *tape = drive->driver_data;
  1217. struct gendisk *disk = tape->disk;
  1218. int retval;
  1219. struct ide_atapi_pc pc;
  1220. debug_log(DBG_SENSE, "Enter %s\n", __func__);
  1221. idetape_create_rewind_cmd(drive, &pc);
  1222. retval = ide_queue_pc_tail(drive, disk, &pc);
  1223. if (retval)
  1224. return retval;
  1225. idetape_create_read_position_cmd(&pc);
  1226. retval = ide_queue_pc_tail(drive, disk, &pc);
  1227. if (retval)
  1228. return retval;
  1229. return 0;
  1230. }
  1231. /* mtio.h compatible commands should be issued to the chrdev interface. */
  1232. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
  1233. unsigned long arg)
  1234. {
  1235. idetape_tape_t *tape = drive->driver_data;
  1236. void __user *argp = (void __user *)arg;
  1237. struct idetape_config {
  1238. int dsc_rw_frequency;
  1239. int dsc_media_access_frequency;
  1240. int nr_stages;
  1241. } config;
  1242. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1243. switch (cmd) {
  1244. case 0x0340:
  1245. if (copy_from_user(&config, argp, sizeof(config)))
  1246. return -EFAULT;
  1247. tape->best_dsc_rw_freq = config.dsc_rw_frequency;
  1248. break;
  1249. case 0x0350:
  1250. config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
  1251. config.nr_stages = 1;
  1252. if (copy_to_user(argp, &config, sizeof(config)))
  1253. return -EFAULT;
  1254. break;
  1255. default:
  1256. return -EIO;
  1257. }
  1258. return 0;
  1259. }
  1260. static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
  1261. int mt_count)
  1262. {
  1263. idetape_tape_t *tape = drive->driver_data;
  1264. struct gendisk *disk = tape->disk;
  1265. struct ide_atapi_pc pc;
  1266. int retval, count = 0;
  1267. int sprev = !!(tape->caps[4] & 0x20);
  1268. if (mt_count == 0)
  1269. return 0;
  1270. if (MTBSF == mt_op || MTBSFM == mt_op) {
  1271. if (!sprev)
  1272. return -EIO;
  1273. mt_count = -mt_count;
  1274. }
  1275. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1276. tape->merge_bh_size = 0;
  1277. if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
  1278. ++count;
  1279. ide_tape_discard_merge_buffer(drive, 0);
  1280. }
  1281. switch (mt_op) {
  1282. case MTFSF:
  1283. case MTBSF:
  1284. idetape_create_space_cmd(&pc, mt_count - count,
  1285. IDETAPE_SPACE_OVER_FILEMARK);
  1286. return ide_queue_pc_tail(drive, disk, &pc);
  1287. case MTFSFM:
  1288. case MTBSFM:
  1289. if (!sprev)
  1290. return -EIO;
  1291. retval = idetape_space_over_filemarks(drive, MTFSF,
  1292. mt_count - count);
  1293. if (retval)
  1294. return retval;
  1295. count = (MTBSFM == mt_op ? 1 : -1);
  1296. return idetape_space_over_filemarks(drive, MTFSF, count);
  1297. default:
  1298. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1299. mt_op);
  1300. return -EIO;
  1301. }
  1302. }
  1303. /*
  1304. * Our character device read / write functions.
  1305. *
  1306. * The tape is optimized to maximize throughput when it is transferring an
  1307. * integral number of the "continuous transfer limit", which is a parameter of
  1308. * the specific tape (26kB on my particular tape, 32kB for Onstream).
  1309. *
  1310. * As of version 1.3 of the driver, the character device provides an abstract
  1311. * continuous view of the media - any mix of block sizes (even 1 byte) on the
  1312. * same backup/restore procedure is supported. The driver will internally
  1313. * convert the requests to the recommended transfer unit, so that an unmatch
  1314. * between the user's block size to the recommended size will only result in a
  1315. * (slightly) increased driver overhead, but will no longer hit performance.
  1316. * This is not applicable to Onstream.
  1317. */
  1318. static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
  1319. size_t count, loff_t *ppos)
  1320. {
  1321. struct ide_tape_obj *tape = file->private_data;
  1322. ide_drive_t *drive = tape->drive;
  1323. ssize_t bytes_read, temp, actually_read = 0, rc;
  1324. ssize_t ret = 0;
  1325. u16 ctl = *(u16 *)&tape->caps[12];
  1326. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1327. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1328. if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
  1329. if (count > tape->blk_size &&
  1330. (count % tape->blk_size) == 0)
  1331. tape->user_bs_factor = count / tape->blk_size;
  1332. }
  1333. rc = idetape_init_read(drive);
  1334. if (rc < 0)
  1335. return rc;
  1336. if (count == 0)
  1337. return (0);
  1338. if (tape->merge_bh_size) {
  1339. actually_read = min((unsigned int)(tape->merge_bh_size),
  1340. (unsigned int)count);
  1341. if (idetape_copy_stage_to_user(tape, buf, actually_read))
  1342. ret = -EFAULT;
  1343. buf += actually_read;
  1344. tape->merge_bh_size -= actually_read;
  1345. count -= actually_read;
  1346. }
  1347. while (count >= tape->buffer_size) {
  1348. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1349. if (bytes_read <= 0)
  1350. goto finish;
  1351. if (idetape_copy_stage_to_user(tape, buf, bytes_read))
  1352. ret = -EFAULT;
  1353. buf += bytes_read;
  1354. count -= bytes_read;
  1355. actually_read += bytes_read;
  1356. }
  1357. if (count) {
  1358. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1359. if (bytes_read <= 0)
  1360. goto finish;
  1361. temp = min((unsigned long)count, (unsigned long)bytes_read);
  1362. if (idetape_copy_stage_to_user(tape, buf, temp))
  1363. ret = -EFAULT;
  1364. actually_read += temp;
  1365. tape->merge_bh_size = bytes_read-temp;
  1366. }
  1367. finish:
  1368. if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
  1369. debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
  1370. idetape_space_over_filemarks(drive, MTFSF, 1);
  1371. return 0;
  1372. }
  1373. return ret ? ret : actually_read;
  1374. }
  1375. static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
  1376. size_t count, loff_t *ppos)
  1377. {
  1378. struct ide_tape_obj *tape = file->private_data;
  1379. ide_drive_t *drive = tape->drive;
  1380. ssize_t actually_written = 0;
  1381. ssize_t ret = 0;
  1382. u16 ctl = *(u16 *)&tape->caps[12];
  1383. /* The drive is write protected. */
  1384. if (tape->write_prot)
  1385. return -EACCES;
  1386. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1387. /* Initialize write operation */
  1388. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1389. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1390. ide_tape_discard_merge_buffer(drive, 1);
  1391. if (tape->merge_bh || tape->merge_bh_size) {
  1392. printk(KERN_ERR "ide-tape: merge_bh_size "
  1393. "should be 0 now\n");
  1394. tape->merge_bh_size = 0;
  1395. }
  1396. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
  1397. if (!tape->merge_bh)
  1398. return -ENOMEM;
  1399. tape->chrdev_dir = IDETAPE_DIR_WRITE;
  1400. idetape_init_merge_buffer(tape);
  1401. /*
  1402. * Issue a write 0 command to ensure that DSC handshake is
  1403. * switched from completion mode to buffer available mode. No
  1404. * point in issuing this if DSC overlap isn't supported, some
  1405. * drives (Seagate STT3401A) will return an error.
  1406. */
  1407. if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
  1408. ssize_t retval = idetape_queue_rw_tail(drive,
  1409. REQ_IDETAPE_WRITE, 0,
  1410. tape->merge_bh);
  1411. if (retval < 0) {
  1412. ide_tape_kfree_buffer(tape);
  1413. tape->merge_bh = NULL;
  1414. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1415. return retval;
  1416. }
  1417. }
  1418. }
  1419. if (count == 0)
  1420. return (0);
  1421. if (tape->merge_bh_size) {
  1422. if (tape->merge_bh_size >= tape->buffer_size) {
  1423. printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
  1424. tape->merge_bh_size = 0;
  1425. }
  1426. actually_written = min((unsigned int)
  1427. (tape->buffer_size - tape->merge_bh_size),
  1428. (unsigned int)count);
  1429. if (idetape_copy_stage_from_user(tape, buf, actually_written))
  1430. ret = -EFAULT;
  1431. buf += actually_written;
  1432. tape->merge_bh_size += actually_written;
  1433. count -= actually_written;
  1434. if (tape->merge_bh_size == tape->buffer_size) {
  1435. ssize_t retval;
  1436. tape->merge_bh_size = 0;
  1437. retval = idetape_add_chrdev_write_request(drive, ctl);
  1438. if (retval <= 0)
  1439. return (retval);
  1440. }
  1441. }
  1442. while (count >= tape->buffer_size) {
  1443. ssize_t retval;
  1444. if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
  1445. ret = -EFAULT;
  1446. buf += tape->buffer_size;
  1447. count -= tape->buffer_size;
  1448. retval = idetape_add_chrdev_write_request(drive, ctl);
  1449. actually_written += tape->buffer_size;
  1450. if (retval <= 0)
  1451. return (retval);
  1452. }
  1453. if (count) {
  1454. actually_written += count;
  1455. if (idetape_copy_stage_from_user(tape, buf, count))
  1456. ret = -EFAULT;
  1457. tape->merge_bh_size += count;
  1458. }
  1459. return ret ? ret : actually_written;
  1460. }
  1461. static int idetape_write_filemark(ide_drive_t *drive)
  1462. {
  1463. struct ide_tape_obj *tape = drive->driver_data;
  1464. struct ide_atapi_pc pc;
  1465. /* Write a filemark */
  1466. idetape_create_write_filemark_cmd(drive, &pc, 1);
  1467. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1468. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  1469. return -EIO;
  1470. }
  1471. return 0;
  1472. }
  1473. /*
  1474. * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
  1475. * requested.
  1476. *
  1477. * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
  1478. * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
  1479. * usually not supported.
  1480. *
  1481. * The following commands are currently not supported:
  1482. *
  1483. * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
  1484. * MT_ST_WRITE_THRESHOLD.
  1485. */
  1486. static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
  1487. {
  1488. idetape_tape_t *tape = drive->driver_data;
  1489. struct gendisk *disk = tape->disk;
  1490. struct ide_atapi_pc pc;
  1491. int i, retval;
  1492. debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
  1493. mt_op, mt_count);
  1494. switch (mt_op) {
  1495. case MTFSF:
  1496. case MTFSFM:
  1497. case MTBSF:
  1498. case MTBSFM:
  1499. if (!mt_count)
  1500. return 0;
  1501. return idetape_space_over_filemarks(drive, mt_op, mt_count);
  1502. default:
  1503. break;
  1504. }
  1505. switch (mt_op) {
  1506. case MTWEOF:
  1507. if (tape->write_prot)
  1508. return -EACCES;
  1509. ide_tape_discard_merge_buffer(drive, 1);
  1510. for (i = 0; i < mt_count; i++) {
  1511. retval = idetape_write_filemark(drive);
  1512. if (retval)
  1513. return retval;
  1514. }
  1515. return 0;
  1516. case MTREW:
  1517. ide_tape_discard_merge_buffer(drive, 0);
  1518. if (idetape_rewind_tape(drive))
  1519. return -EIO;
  1520. return 0;
  1521. case MTLOAD:
  1522. ide_tape_discard_merge_buffer(drive, 0);
  1523. return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
  1524. case MTUNLOAD:
  1525. case MTOFFL:
  1526. /*
  1527. * If door is locked, attempt to unlock before
  1528. * attempting to eject.
  1529. */
  1530. if (tape->door_locked) {
  1531. if (!ide_set_media_lock(drive, disk, 0))
  1532. tape->door_locked = DOOR_UNLOCKED;
  1533. }
  1534. ide_tape_discard_merge_buffer(drive, 0);
  1535. retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
  1536. if (!retval)
  1537. clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
  1538. return retval;
  1539. case MTNOP:
  1540. ide_tape_discard_merge_buffer(drive, 0);
  1541. return idetape_flush_tape_buffers(drive);
  1542. case MTRETEN:
  1543. ide_tape_discard_merge_buffer(drive, 0);
  1544. return ide_do_start_stop(drive, disk,
  1545. IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  1546. case MTEOM:
  1547. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  1548. return ide_queue_pc_tail(drive, disk, &pc);
  1549. case MTERASE:
  1550. (void)idetape_rewind_tape(drive);
  1551. idetape_create_erase_cmd(&pc);
  1552. return ide_queue_pc_tail(drive, disk, &pc);
  1553. case MTSETBLK:
  1554. if (mt_count) {
  1555. if (mt_count < tape->blk_size ||
  1556. mt_count % tape->blk_size)
  1557. return -EIO;
  1558. tape->user_bs_factor = mt_count / tape->blk_size;
  1559. clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
  1560. } else
  1561. set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
  1562. return 0;
  1563. case MTSEEK:
  1564. ide_tape_discard_merge_buffer(drive, 0);
  1565. return idetape_position_tape(drive,
  1566. mt_count * tape->user_bs_factor, tape->partition, 0);
  1567. case MTSETPART:
  1568. ide_tape_discard_merge_buffer(drive, 0);
  1569. return idetape_position_tape(drive, 0, mt_count, 0);
  1570. case MTFSR:
  1571. case MTBSR:
  1572. case MTLOCK:
  1573. retval = ide_set_media_lock(drive, disk, 1);
  1574. if (retval)
  1575. return retval;
  1576. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  1577. return 0;
  1578. case MTUNLOCK:
  1579. retval = ide_set_media_lock(drive, disk, 0);
  1580. if (retval)
  1581. return retval;
  1582. tape->door_locked = DOOR_UNLOCKED;
  1583. return 0;
  1584. default:
  1585. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1586. mt_op);
  1587. return -EIO;
  1588. }
  1589. }
  1590. /*
  1591. * Our character device ioctls. General mtio.h magnetic io commands are
  1592. * supported here, and not in the corresponding block interface. Our own
  1593. * ide-tape ioctls are supported on both interfaces.
  1594. */
  1595. static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
  1596. unsigned int cmd, unsigned long arg)
  1597. {
  1598. struct ide_tape_obj *tape = file->private_data;
  1599. ide_drive_t *drive = tape->drive;
  1600. struct mtop mtop;
  1601. struct mtget mtget;
  1602. struct mtpos mtpos;
  1603. int block_offset = 0, position = tape->first_frame;
  1604. void __user *argp = (void __user *)arg;
  1605. debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
  1606. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1607. ide_tape_flush_merge_buffer(drive);
  1608. idetape_flush_tape_buffers(drive);
  1609. }
  1610. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  1611. block_offset = tape->merge_bh_size /
  1612. (tape->blk_size * tape->user_bs_factor);
  1613. position = idetape_read_position(drive);
  1614. if (position < 0)
  1615. return -EIO;
  1616. }
  1617. switch (cmd) {
  1618. case MTIOCTOP:
  1619. if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
  1620. return -EFAULT;
  1621. return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
  1622. case MTIOCGET:
  1623. memset(&mtget, 0, sizeof(struct mtget));
  1624. mtget.mt_type = MT_ISSCSI2;
  1625. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  1626. mtget.mt_dsreg =
  1627. ((tape->blk_size * tape->user_bs_factor)
  1628. << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  1629. if (tape->drv_write_prot)
  1630. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  1631. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  1632. return -EFAULT;
  1633. return 0;
  1634. case MTIOCPOS:
  1635. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  1636. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  1637. return -EFAULT;
  1638. return 0;
  1639. default:
  1640. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1641. ide_tape_discard_merge_buffer(drive, 1);
  1642. return idetape_blkdev_ioctl(drive, cmd, arg);
  1643. }
  1644. }
  1645. /*
  1646. * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
  1647. * block size with the reported value.
  1648. */
  1649. static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
  1650. {
  1651. idetape_tape_t *tape = drive->driver_data;
  1652. struct ide_atapi_pc pc;
  1653. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  1654. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1655. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  1656. if (tape->blk_size == 0) {
  1657. printk(KERN_WARNING "ide-tape: Cannot deal with zero "
  1658. "block size, assuming 32k\n");
  1659. tape->blk_size = 32768;
  1660. }
  1661. return;
  1662. }
  1663. tape->blk_size = (pc.buf[4 + 5] << 16) +
  1664. (pc.buf[4 + 6] << 8) +
  1665. pc.buf[4 + 7];
  1666. tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
  1667. }
  1668. static int idetape_chrdev_open(struct inode *inode, struct file *filp)
  1669. {
  1670. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  1671. ide_drive_t *drive;
  1672. idetape_tape_t *tape;
  1673. int retval;
  1674. if (i >= MAX_HWIFS * MAX_DRIVES)
  1675. return -ENXIO;
  1676. lock_kernel();
  1677. tape = ide_tape_chrdev_get(i);
  1678. if (!tape) {
  1679. unlock_kernel();
  1680. return -ENXIO;
  1681. }
  1682. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1683. /*
  1684. * We really want to do nonseekable_open(inode, filp); here, but some
  1685. * versions of tar incorrectly call lseek on tapes and bail out if that
  1686. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  1687. */
  1688. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  1689. drive = tape->drive;
  1690. filp->private_data = tape;
  1691. if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
  1692. retval = -EBUSY;
  1693. goto out_put_tape;
  1694. }
  1695. retval = idetape_wait_ready(drive, 60 * HZ);
  1696. if (retval) {
  1697. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1698. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  1699. goto out_put_tape;
  1700. }
  1701. idetape_read_position(drive);
  1702. if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
  1703. (void)idetape_rewind_tape(drive);
  1704. /* Read block size and write protect status from drive. */
  1705. ide_tape_get_bsize_from_bdesc(drive);
  1706. /* Set write protect flag if device is opened as read-only. */
  1707. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  1708. tape->write_prot = 1;
  1709. else
  1710. tape->write_prot = tape->drv_write_prot;
  1711. /* Make sure drive isn't write protected if user wants to write. */
  1712. if (tape->write_prot) {
  1713. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  1714. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  1715. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1716. retval = -EROFS;
  1717. goto out_put_tape;
  1718. }
  1719. }
  1720. /* Lock the tape drive door so user can't eject. */
  1721. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1722. if (!ide_set_media_lock(drive, tape->disk, 1)) {
  1723. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  1724. tape->door_locked = DOOR_LOCKED;
  1725. }
  1726. }
  1727. unlock_kernel();
  1728. return 0;
  1729. out_put_tape:
  1730. ide_tape_put(tape);
  1731. unlock_kernel();
  1732. return retval;
  1733. }
  1734. static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
  1735. {
  1736. idetape_tape_t *tape = drive->driver_data;
  1737. ide_tape_flush_merge_buffer(drive);
  1738. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
  1739. if (tape->merge_bh != NULL) {
  1740. idetape_pad_zeros(drive, tape->blk_size *
  1741. (tape->user_bs_factor - 1));
  1742. ide_tape_kfree_buffer(tape);
  1743. tape->merge_bh = NULL;
  1744. }
  1745. idetape_write_filemark(drive);
  1746. idetape_flush_tape_buffers(drive);
  1747. idetape_flush_tape_buffers(drive);
  1748. }
  1749. static int idetape_chrdev_release(struct inode *inode, struct file *filp)
  1750. {
  1751. struct ide_tape_obj *tape = filp->private_data;
  1752. ide_drive_t *drive = tape->drive;
  1753. unsigned int minor = iminor(inode);
  1754. lock_kernel();
  1755. tape = drive->driver_data;
  1756. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1757. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  1758. idetape_write_release(drive, minor);
  1759. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1760. if (minor < 128)
  1761. ide_tape_discard_merge_buffer(drive, 1);
  1762. }
  1763. if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
  1764. (void) idetape_rewind_tape(drive);
  1765. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1766. if (tape->door_locked == DOOR_LOCKED) {
  1767. if (!ide_set_media_lock(drive, tape->disk, 0))
  1768. tape->door_locked = DOOR_UNLOCKED;
  1769. }
  1770. }
  1771. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1772. ide_tape_put(tape);
  1773. unlock_kernel();
  1774. return 0;
  1775. }
  1776. static void idetape_get_inquiry_results(ide_drive_t *drive)
  1777. {
  1778. idetape_tape_t *tape = drive->driver_data;
  1779. struct ide_atapi_pc pc;
  1780. u8 pc_buf[256];
  1781. char fw_rev[4], vendor_id[8], product_id[16];
  1782. idetape_create_inquiry_cmd(&pc);
  1783. pc.buf = &pc_buf[0];
  1784. pc.buf_size = sizeof(pc_buf);
  1785. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1786. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
  1787. tape->name);
  1788. return;
  1789. }
  1790. memcpy(vendor_id, &pc.buf[8], 8);
  1791. memcpy(product_id, &pc.buf[16], 16);
  1792. memcpy(fw_rev, &pc.buf[32], 4);
  1793. ide_fixstring(vendor_id, 8, 0);
  1794. ide_fixstring(product_id, 16, 0);
  1795. ide_fixstring(fw_rev, 4, 0);
  1796. printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
  1797. drive->name, tape->name, vendor_id, product_id, fw_rev);
  1798. }
  1799. /*
  1800. * Ask the tape about its various parameters. In particular, we will adjust our
  1801. * data transfer buffer size to the recommended value as returned by the tape.
  1802. */
  1803. static void idetape_get_mode_sense_results(ide_drive_t *drive)
  1804. {
  1805. idetape_tape_t *tape = drive->driver_data;
  1806. struct ide_atapi_pc pc;
  1807. u8 *caps;
  1808. u8 speed, max_speed;
  1809. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  1810. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1811. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
  1812. " some default values\n");
  1813. tape->blk_size = 512;
  1814. put_unaligned(52, (u16 *)&tape->caps[12]);
  1815. put_unaligned(540, (u16 *)&tape->caps[14]);
  1816. put_unaligned(6*52, (u16 *)&tape->caps[16]);
  1817. return;
  1818. }
  1819. caps = pc.buf + 4 + pc.buf[3];
  1820. /* convert to host order and save for later use */
  1821. speed = be16_to_cpup((__be16 *)&caps[14]);
  1822. max_speed = be16_to_cpup((__be16 *)&caps[8]);
  1823. *(u16 *)&caps[8] = max_speed;
  1824. *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
  1825. *(u16 *)&caps[14] = speed;
  1826. *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
  1827. if (!speed) {
  1828. printk(KERN_INFO "ide-tape: %s: invalid tape speed "
  1829. "(assuming 650KB/sec)\n", drive->name);
  1830. *(u16 *)&caps[14] = 650;
  1831. }
  1832. if (!max_speed) {
  1833. printk(KERN_INFO "ide-tape: %s: invalid max_speed "
  1834. "(assuming 650KB/sec)\n", drive->name);
  1835. *(u16 *)&caps[8] = 650;
  1836. }
  1837. memcpy(&tape->caps, caps, 20);
  1838. /* device lacks locking support according to capabilities page */
  1839. if ((caps[6] & 1) == 0)
  1840. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  1841. if (caps[7] & 0x02)
  1842. tape->blk_size = 512;
  1843. else if (caps[7] & 0x04)
  1844. tape->blk_size = 1024;
  1845. }
  1846. #ifdef CONFIG_IDE_PROC_FS
  1847. #define ide_tape_devset_get(name, field) \
  1848. static int get_##name(ide_drive_t *drive) \
  1849. { \
  1850. idetape_tape_t *tape = drive->driver_data; \
  1851. return tape->field; \
  1852. }
  1853. #define ide_tape_devset_set(name, field) \
  1854. static int set_##name(ide_drive_t *drive, int arg) \
  1855. { \
  1856. idetape_tape_t *tape = drive->driver_data; \
  1857. tape->field = arg; \
  1858. return 0; \
  1859. }
  1860. #define ide_tape_devset_rw_field(_name, _field) \
  1861. ide_tape_devset_get(_name, _field) \
  1862. ide_tape_devset_set(_name, _field) \
  1863. IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
  1864. #define ide_tape_devset_r_field(_name, _field) \
  1865. ide_tape_devset_get(_name, _field) \
  1866. IDE_DEVSET(_name, 0, get_##_name, NULL)
  1867. static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
  1868. static int divf_tdsc(ide_drive_t *drive) { return HZ; }
  1869. static int divf_buffer(ide_drive_t *drive) { return 2; }
  1870. static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
  1871. ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
  1872. ide_tape_devset_rw_field(debug_mask, debug_mask);
  1873. ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
  1874. ide_tape_devset_r_field(avg_speed, avg_speed);
  1875. ide_tape_devset_r_field(speed, caps[14]);
  1876. ide_tape_devset_r_field(buffer, caps[16]);
  1877. ide_tape_devset_r_field(buffer_size, buffer_size);
  1878. static const struct ide_proc_devset idetape_settings[] = {
  1879. __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
  1880. __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
  1881. __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
  1882. __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
  1883. __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
  1884. __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
  1885. __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
  1886. mulf_tdsc, divf_tdsc),
  1887. { NULL },
  1888. };
  1889. #endif
  1890. /*
  1891. * The function below is called to:
  1892. *
  1893. * 1. Initialize our various state variables.
  1894. * 2. Ask the tape for its capabilities.
  1895. * 3. Allocate a buffer which will be used for data transfer. The buffer size
  1896. * is chosen based on the recommendation which we received in step 2.
  1897. *
  1898. * Note that at this point ide.c already assigned us an irq, so that we can
  1899. * queue requests here and wait for their completion.
  1900. */
  1901. static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
  1902. {
  1903. unsigned long t;
  1904. int speed;
  1905. int buffer_size;
  1906. u16 *ctl = (u16 *)&tape->caps[12];
  1907. drive->pc_callback = ide_tape_callback;
  1908. drive->pc_update_buffers = idetape_update_buffers;
  1909. drive->pc_io_buffers = ide_tape_io_buffers;
  1910. drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
  1911. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  1912. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  1913. tape->name);
  1914. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1915. }
  1916. /* Seagate Travan drives do not support DSC overlap. */
  1917. if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
  1918. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1919. tape->minor = minor;
  1920. tape->name[0] = 'h';
  1921. tape->name[1] = 't';
  1922. tape->name[2] = '0' + minor;
  1923. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1924. idetape_get_inquiry_results(drive);
  1925. idetape_get_mode_sense_results(drive);
  1926. ide_tape_get_bsize_from_bdesc(drive);
  1927. tape->user_bs_factor = 1;
  1928. tape->buffer_size = *ctl * tape->blk_size;
  1929. while (tape->buffer_size > 0xffff) {
  1930. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  1931. *ctl /= 2;
  1932. tape->buffer_size = *ctl * tape->blk_size;
  1933. }
  1934. buffer_size = tape->buffer_size;
  1935. tape->pages_per_buffer = buffer_size / PAGE_SIZE;
  1936. if (buffer_size % PAGE_SIZE) {
  1937. tape->pages_per_buffer++;
  1938. tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
  1939. }
  1940. /* select the "best" DSC read/write polling freq */
  1941. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  1942. t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
  1943. /*
  1944. * Ensure that the number we got makes sense; limit it within
  1945. * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  1946. */
  1947. tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
  1948. IDETAPE_DSC_RW_MAX);
  1949. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  1950. "%lums tDSC%s\n",
  1951. drive->name, tape->name, *(u16 *)&tape->caps[14],
  1952. (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
  1953. tape->buffer_size / 1024,
  1954. tape->best_dsc_rw_freq * 1000 / HZ,
  1955. (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
  1956. ide_proc_register_driver(drive, tape->driver);
  1957. }
  1958. static void ide_tape_remove(ide_drive_t *drive)
  1959. {
  1960. idetape_tape_t *tape = drive->driver_data;
  1961. ide_proc_unregister_driver(drive, tape->driver);
  1962. device_del(&tape->dev);
  1963. ide_unregister_region(tape->disk);
  1964. mutex_lock(&idetape_ref_mutex);
  1965. put_device(&tape->dev);
  1966. mutex_unlock(&idetape_ref_mutex);
  1967. }
  1968. static void ide_tape_release(struct device *dev)
  1969. {
  1970. struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
  1971. ide_drive_t *drive = tape->drive;
  1972. struct gendisk *g = tape->disk;
  1973. BUG_ON(tape->merge_bh_size);
  1974. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1975. drive->driver_data = NULL;
  1976. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  1977. device_destroy(idetape_sysfs_class,
  1978. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  1979. idetape_devs[tape->minor] = NULL;
  1980. g->private_data = NULL;
  1981. put_disk(g);
  1982. kfree(tape);
  1983. }
  1984. #ifdef CONFIG_IDE_PROC_FS
  1985. static int proc_idetape_read_name
  1986. (char *page, char **start, off_t off, int count, int *eof, void *data)
  1987. {
  1988. ide_drive_t *drive = (ide_drive_t *) data;
  1989. idetape_tape_t *tape = drive->driver_data;
  1990. char *out = page;
  1991. int len;
  1992. len = sprintf(out, "%s\n", tape->name);
  1993. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  1994. }
  1995. static ide_proc_entry_t idetape_proc[] = {
  1996. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  1997. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  1998. { NULL, 0, NULL, NULL }
  1999. };
  2000. static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
  2001. {
  2002. return idetape_proc;
  2003. }
  2004. static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
  2005. {
  2006. return idetape_settings;
  2007. }
  2008. #endif
  2009. static int ide_tape_probe(ide_drive_t *);
  2010. static struct ide_driver idetape_driver = {
  2011. .gen_driver = {
  2012. .owner = THIS_MODULE,
  2013. .name = "ide-tape",
  2014. .bus = &ide_bus_type,
  2015. },
  2016. .probe = ide_tape_probe,
  2017. .remove = ide_tape_remove,
  2018. .version = IDETAPE_VERSION,
  2019. .do_request = idetape_do_request,
  2020. #ifdef CONFIG_IDE_PROC_FS
  2021. .proc_entries = ide_tape_proc_entries,
  2022. .proc_devsets = ide_tape_proc_devsets,
  2023. #endif
  2024. };
  2025. /* Our character device supporting functions, passed to register_chrdev. */
  2026. static const struct file_operations idetape_fops = {
  2027. .owner = THIS_MODULE,
  2028. .read = idetape_chrdev_read,
  2029. .write = idetape_chrdev_write,
  2030. .ioctl = idetape_chrdev_ioctl,
  2031. .open = idetape_chrdev_open,
  2032. .release = idetape_chrdev_release,
  2033. };
  2034. static int idetape_open(struct block_device *bdev, fmode_t mode)
  2035. {
  2036. struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
  2037. if (!tape)
  2038. return -ENXIO;
  2039. return 0;
  2040. }
  2041. static int idetape_release(struct gendisk *disk, fmode_t mode)
  2042. {
  2043. struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
  2044. ide_tape_put(tape);
  2045. return 0;
  2046. }
  2047. static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
  2048. unsigned int cmd, unsigned long arg)
  2049. {
  2050. struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
  2051. ide_drive_t *drive = tape->drive;
  2052. int err = generic_ide_ioctl(drive, bdev, cmd, arg);
  2053. if (err == -EINVAL)
  2054. err = idetape_blkdev_ioctl(drive, cmd, arg);
  2055. return err;
  2056. }
  2057. static struct block_device_operations idetape_block_ops = {
  2058. .owner = THIS_MODULE,
  2059. .open = idetape_open,
  2060. .release = idetape_release,
  2061. .locked_ioctl = idetape_ioctl,
  2062. };
  2063. static int ide_tape_probe(ide_drive_t *drive)
  2064. {
  2065. idetape_tape_t *tape;
  2066. struct gendisk *g;
  2067. int minor;
  2068. if (!strstr("ide-tape", drive->driver_req))
  2069. goto failed;
  2070. if (drive->media != ide_tape)
  2071. goto failed;
  2072. if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
  2073. ide_check_atapi_device(drive, DRV_NAME) == 0) {
  2074. printk(KERN_ERR "ide-tape: %s: not supported by this version of"
  2075. " the driver\n", drive->name);
  2076. goto failed;
  2077. }
  2078. tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
  2079. if (tape == NULL) {
  2080. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
  2081. drive->name);
  2082. goto failed;
  2083. }
  2084. g = alloc_disk(1 << PARTN_BITS);
  2085. if (!g)
  2086. goto out_free_tape;
  2087. ide_init_disk(g, drive);
  2088. tape->dev.parent = &drive->gendev;
  2089. tape->dev.release = ide_tape_release;
  2090. dev_set_name(&tape->dev, dev_name(&drive->gendev));
  2091. if (device_register(&tape->dev))
  2092. goto out_free_disk;
  2093. tape->drive = drive;
  2094. tape->driver = &idetape_driver;
  2095. tape->disk = g;
  2096. g->private_data = &tape->driver;
  2097. drive->driver_data = tape;
  2098. mutex_lock(&idetape_ref_mutex);
  2099. for (minor = 0; idetape_devs[minor]; minor++)
  2100. ;
  2101. idetape_devs[minor] = tape;
  2102. mutex_unlock(&idetape_ref_mutex);
  2103. idetape_setup(drive, tape, minor);
  2104. device_create(idetape_sysfs_class, &drive->gendev,
  2105. MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
  2106. device_create(idetape_sysfs_class, &drive->gendev,
  2107. MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
  2108. "n%s", tape->name);
  2109. g->fops = &idetape_block_ops;
  2110. ide_register_region(g);
  2111. return 0;
  2112. out_free_disk:
  2113. put_disk(g);
  2114. out_free_tape:
  2115. kfree(tape);
  2116. failed:
  2117. return -ENODEV;
  2118. }
  2119. static void __exit idetape_exit(void)
  2120. {
  2121. driver_unregister(&idetape_driver.gen_driver);
  2122. class_destroy(idetape_sysfs_class);
  2123. unregister_chrdev(IDETAPE_MAJOR, "ht");
  2124. }
  2125. static int __init idetape_init(void)
  2126. {
  2127. int error = 1;
  2128. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  2129. if (IS_ERR(idetape_sysfs_class)) {
  2130. idetape_sysfs_class = NULL;
  2131. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  2132. error = -EBUSY;
  2133. goto out;
  2134. }
  2135. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  2136. printk(KERN_ERR "ide-tape: Failed to register chrdev"
  2137. " interface\n");
  2138. error = -EBUSY;
  2139. goto out_free_class;
  2140. }
  2141. error = driver_register(&idetape_driver.gen_driver);
  2142. if (error)
  2143. goto out_free_driver;
  2144. return 0;
  2145. out_free_driver:
  2146. driver_unregister(&idetape_driver.gen_driver);
  2147. out_free_class:
  2148. class_destroy(idetape_sysfs_class);
  2149. out:
  2150. return error;
  2151. }
  2152. MODULE_ALIAS("ide:*m-tape*");
  2153. module_init(idetape_init);
  2154. module_exit(idetape_exit);
  2155. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
  2156. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  2157. MODULE_LICENSE("GPL");