ide-tape.c 67 KB

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