ide-tape.c 55 KB

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