ide-tape.c 66 KB

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