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