ide-tape.c 55 KB

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