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