ide-tape.c 55 KB

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