ide-tape.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  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/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/uaccess.h>
  41. #include <linux/io.h>
  42. #include <asm/unaligned.h>
  43. #include <linux/mtio.h>
  44. /* define to see debug info */
  45. #undef IDETAPE_DEBUG_LOG
  46. #ifdef IDETAPE_DEBUG_LOG
  47. #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
  48. #else
  49. #define ide_debug_log(lvl, fmt, args...) do {} while (0)
  50. #endif
  51. /**************************** Tunable parameters *****************************/
  52. /*
  53. * After each failed packet command we issue a request sense command and retry
  54. * the packet command IDETAPE_MAX_PC_RETRIES times.
  55. *
  56. * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
  57. */
  58. #define IDETAPE_MAX_PC_RETRIES 3
  59. /*
  60. * The following parameter is used to select the point in the internal tape fifo
  61. * in which we will start to refill the buffer. Decreasing the following
  62. * parameter will improve the system's latency and interactive response, while
  63. * using a high value might improve system throughput.
  64. */
  65. #define IDETAPE_FIFO_THRESHOLD 2
  66. /*
  67. * DSC polling parameters.
  68. *
  69. * Polling for DSC (a single bit in the status register) is a very important
  70. * function in ide-tape. There are two cases in which we poll for DSC:
  71. *
  72. * 1. Before a read/write packet command, to ensure that we can transfer data
  73. * from/to the tape's data buffers, without causing an actual media access.
  74. * In case the tape is not ready yet, we take out our request from the device
  75. * request queue, so that ide.c could service requests from the other device
  76. * on the same interface in the meantime.
  77. *
  78. * 2. After the successful initialization of a "media access packet command",
  79. * which is a command that can take a long time to complete (the interval can
  80. * range from several seconds to even an hour). Again, we postpone our request
  81. * in the middle to free the bus for the other device. The polling frequency
  82. * here should be lower than the read/write frequency since those media access
  83. * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
  84. * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
  85. * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
  86. *
  87. * We also set a timeout for the timer, in case something goes wrong. The
  88. * timeout should be longer then the maximum execution time of a tape operation.
  89. */
  90. /* DSC timings. */
  91. #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
  92. #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
  93. #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
  94. #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
  95. #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
  96. #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
  97. #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
  98. /*************************** End of tunable parameters ***********************/
  99. /* tape directions */
  100. enum {
  101. IDETAPE_DIR_NONE = (1 << 0),
  102. IDETAPE_DIR_READ = (1 << 1),
  103. IDETAPE_DIR_WRITE = (1 << 2),
  104. };
  105. /* Tape door status */
  106. #define DOOR_UNLOCKED 0
  107. #define DOOR_LOCKED 1
  108. #define DOOR_EXPLICITLY_LOCKED 2
  109. /* Some defines for the SPACE command */
  110. #define IDETAPE_SPACE_OVER_FILEMARK 1
  111. #define IDETAPE_SPACE_TO_EOD 3
  112. /* Some defines for the LOAD UNLOAD command */
  113. #define IDETAPE_LU_LOAD_MASK 1
  114. #define IDETAPE_LU_RETENSION_MASK 2
  115. #define IDETAPE_LU_EOT_MASK 4
  116. /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
  117. #define IDETAPE_BLOCK_DESCRIPTOR 0
  118. #define IDETAPE_CAPABILITIES_PAGE 0x2a
  119. /*
  120. * Most of our global data which we need to save even as we leave the driver due
  121. * to an interrupt or a timer event is stored in the struct defined below.
  122. */
  123. typedef struct ide_tape_obj {
  124. ide_drive_t *drive;
  125. struct ide_driver *driver;
  126. struct gendisk *disk;
  127. struct device dev;
  128. /* used by REQ_IDETAPE_{READ,WRITE} requests */
  129. struct ide_atapi_pc queued_pc;
  130. /*
  131. * DSC polling variables.
  132. *
  133. * While polling for DSC we use postponed_rq to postpone the current
  134. * request so that ide.c will be able to service pending requests on the
  135. * other device. Note that at most we will have only one DSC (usually
  136. * data transfer) request in the device request queue.
  137. */
  138. bool postponed_rq;
  139. /* The time in which we started polling for DSC */
  140. unsigned long dsc_polling_start;
  141. /* Timer used to poll for dsc */
  142. struct timer_list dsc_timer;
  143. /* Read/Write dsc polling frequency */
  144. unsigned long best_dsc_rw_freq;
  145. unsigned long dsc_poll_freq;
  146. unsigned long dsc_timeout;
  147. /* Read position information */
  148. u8 partition;
  149. /* Current block */
  150. unsigned int first_frame;
  151. /* Last error information */
  152. u8 sense_key, asc, ascq;
  153. /* Character device operation */
  154. unsigned int minor;
  155. /* device name */
  156. char name[4];
  157. /* Current character device data transfer direction */
  158. u8 chrdev_dir;
  159. /* tape block size, usually 512 or 1024 bytes */
  160. unsigned short blk_size;
  161. int user_bs_factor;
  162. /* Copy of the tape's Capabilities and Mechanical Page */
  163. u8 caps[20];
  164. /*
  165. * Active data transfer request parameters.
  166. *
  167. * At most, there is only one ide-tape originated data transfer request
  168. * in the device request queue. This allows ide.c to easily service
  169. * requests from the other device when we postpone our active request.
  170. */
  171. /* Data buffer size chosen based on the tape's recommendation */
  172. int buffer_size;
  173. /* Staging buffer of buffer_size bytes */
  174. void *buf;
  175. /* The read/write cursor */
  176. void *cur;
  177. /* The number of valid bytes in buf */
  178. size_t valid;
  179. /* Measures average tape speed */
  180. unsigned long avg_time;
  181. int avg_size;
  182. int avg_speed;
  183. /* the door is currently locked */
  184. int door_locked;
  185. /* the tape hardware is write protected */
  186. char drv_write_prot;
  187. /* the tape is write protected (hardware or opened as read-only) */
  188. char write_prot;
  189. } idetape_tape_t;
  190. static DEFINE_MUTEX(ide_tape_mutex);
  191. static DEFINE_MUTEX(idetape_ref_mutex);
  192. static DEFINE_MUTEX(idetape_chrdev_mutex);
  193. static struct class *idetape_sysfs_class;
  194. static void ide_tape_release(struct device *);
  195. static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
  196. static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
  197. unsigned int i)
  198. {
  199. struct ide_tape_obj *tape = NULL;
  200. mutex_lock(&idetape_ref_mutex);
  201. if (cdev)
  202. tape = idetape_devs[i];
  203. else
  204. tape = ide_drv_g(disk, ide_tape_obj);
  205. if (tape) {
  206. if (ide_device_get(tape->drive))
  207. tape = NULL;
  208. else
  209. get_device(&tape->dev);
  210. }
  211. mutex_unlock(&idetape_ref_mutex);
  212. return tape;
  213. }
  214. static void ide_tape_put(struct ide_tape_obj *tape)
  215. {
  216. ide_drive_t *drive = tape->drive;
  217. mutex_lock(&idetape_ref_mutex);
  218. put_device(&tape->dev);
  219. ide_device_put(drive);
  220. mutex_unlock(&idetape_ref_mutex);
  221. }
  222. /*
  223. * called on each failed packet command retry to analyze the request sense. We
  224. * currently do not utilize this information.
  225. */
  226. static void idetape_analyze_error(ide_drive_t *drive)
  227. {
  228. idetape_tape_t *tape = drive->driver_data;
  229. struct ide_atapi_pc *pc = drive->failed_pc;
  230. struct request *rq = drive->hwif->rq;
  231. u8 *sense = bio_data(rq->bio);
  232. tape->sense_key = sense[2] & 0xF;
  233. tape->asc = sense[12];
  234. tape->ascq = sense[13];
  235. ide_debug_log(IDE_DBG_FUNC,
  236. "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
  237. rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
  238. /* correct remaining bytes to transfer */
  239. if (pc->flags & PC_FLAG_DMA_ERROR)
  240. rq->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
  241. /*
  242. * If error was the result of a zero-length read or write command,
  243. * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
  244. * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
  245. */
  246. if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
  247. /* length == 0 */
  248. && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
  249. if (tape->sense_key == 5) {
  250. /* don't report an error, everything's ok */
  251. pc->error = 0;
  252. /* don't retry read/write */
  253. pc->flags |= PC_FLAG_ABORT;
  254. }
  255. }
  256. if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
  257. pc->error = IDE_DRV_ERROR_FILEMARK;
  258. pc->flags |= PC_FLAG_ABORT;
  259. }
  260. if (pc->c[0] == WRITE_6) {
  261. if ((sense[2] & 0x40) || (tape->sense_key == 0xd
  262. && tape->asc == 0x0 && tape->ascq == 0x2)) {
  263. pc->error = IDE_DRV_ERROR_EOD;
  264. pc->flags |= PC_FLAG_ABORT;
  265. }
  266. }
  267. if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
  268. if (tape->sense_key == 8) {
  269. pc->error = IDE_DRV_ERROR_EOD;
  270. pc->flags |= PC_FLAG_ABORT;
  271. }
  272. if (!(pc->flags & PC_FLAG_ABORT) &&
  273. (blk_rq_bytes(rq) - rq->resid_len))
  274. pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
  275. }
  276. }
  277. static void ide_tape_handle_dsc(ide_drive_t *);
  278. static int ide_tape_callback(ide_drive_t *drive, int dsc)
  279. {
  280. idetape_tape_t *tape = drive->driver_data;
  281. struct ide_atapi_pc *pc = drive->pc;
  282. struct request *rq = drive->hwif->rq;
  283. int uptodate = pc->error ? 0 : 1;
  284. int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
  285. ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
  286. dsc, err);
  287. if (dsc)
  288. ide_tape_handle_dsc(drive);
  289. if (drive->failed_pc == pc)
  290. drive->failed_pc = NULL;
  291. if (pc->c[0] == REQUEST_SENSE) {
  292. if (uptodate)
  293. idetape_analyze_error(drive);
  294. else
  295. printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
  296. "itself - Aborting request!\n");
  297. } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
  298. unsigned int blocks =
  299. (blk_rq_bytes(rq) - rq->resid_len) / tape->blk_size;
  300. tape->avg_size += blocks * tape->blk_size;
  301. if (time_after_eq(jiffies, tape->avg_time + HZ)) {
  302. tape->avg_speed = tape->avg_size * HZ /
  303. (jiffies - tape->avg_time) / 1024;
  304. tape->avg_size = 0;
  305. tape->avg_time = jiffies;
  306. }
  307. tape->first_frame += blocks;
  308. if (pc->error) {
  309. uptodate = 0;
  310. err = pc->error;
  311. }
  312. }
  313. rq->errors = err;
  314. return uptodate;
  315. }
  316. /*
  317. * Postpone the current request so that ide.c will be able to service requests
  318. * from another device on the same port while we are polling for DSC.
  319. */
  320. static void ide_tape_stall_queue(ide_drive_t *drive)
  321. {
  322. idetape_tape_t *tape = drive->driver_data;
  323. ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
  324. drive->hwif->rq->cmd[0], tape->dsc_poll_freq);
  325. tape->postponed_rq = true;
  326. ide_stall_queue(drive, tape->dsc_poll_freq);
  327. }
  328. static void ide_tape_handle_dsc(ide_drive_t *drive)
  329. {
  330. idetape_tape_t *tape = drive->driver_data;
  331. /* Media access command */
  332. tape->dsc_polling_start = jiffies;
  333. tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
  334. tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
  335. /* Allow ide.c to handle other requests */
  336. ide_tape_stall_queue(drive);
  337. }
  338. /*
  339. * Packet Command Interface
  340. *
  341. * The current Packet Command is available in drive->pc, and will not change
  342. * until we finish handling it. Each packet command is associated with a
  343. * callback function that will be called when the command is finished.
  344. *
  345. * The handling will be done in three stages:
  346. *
  347. * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
  348. * the interrupt handler to ide_pc_intr.
  349. *
  350. * 2. On each interrupt, ide_pc_intr will be called. This step will be
  351. * repeated until the device signals us that no more interrupts will be issued.
  352. *
  353. * 3. ATAPI Tape media access commands have immediate status with a delayed
  354. * process. In case of a successful initiation of a media access packet command,
  355. * the DSC bit will be set when the actual execution of the command is finished.
  356. * Since the tape drive will not issue an interrupt, we have to poll for this
  357. * event. In this case, we define the request as "low priority request" by
  358. * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
  359. * exit the driver.
  360. *
  361. * ide.c will then give higher priority to requests which originate from the
  362. * other device, until will change rq_status to RQ_ACTIVE.
  363. *
  364. * 4. When the packet command is finished, it will be checked for errors.
  365. *
  366. * 5. In case an error was found, we queue a request sense packet command in
  367. * front of the request queue and retry the operation up to
  368. * IDETAPE_MAX_PC_RETRIES times.
  369. *
  370. * 6. In case no error was found, or we decided to give up and not to retry
  371. * again, the callback function will be called and then we will handle the next
  372. * request.
  373. */
  374. static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
  375. struct ide_cmd *cmd,
  376. struct ide_atapi_pc *pc)
  377. {
  378. idetape_tape_t *tape = drive->driver_data;
  379. struct request *rq = drive->hwif->rq;
  380. if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
  381. drive->failed_pc = pc;
  382. /* Set the current packet command */
  383. drive->pc = pc;
  384. if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
  385. (pc->flags & PC_FLAG_ABORT)) {
  386. /*
  387. * We will "abort" retrying a packet command in case legitimate
  388. * error code was received (crossing a filemark, or end of the
  389. * media, for example).
  390. */
  391. if (!(pc->flags & PC_FLAG_ABORT)) {
  392. if (!(pc->c[0] == TEST_UNIT_READY &&
  393. tape->sense_key == 2 && tape->asc == 4 &&
  394. (tape->ascq == 1 || tape->ascq == 8))) {
  395. printk(KERN_ERR "ide-tape: %s: I/O error, "
  396. "pc = %2x, key = %2x, "
  397. "asc = %2x, ascq = %2x\n",
  398. tape->name, pc->c[0],
  399. tape->sense_key, tape->asc,
  400. tape->ascq);
  401. }
  402. /* Giving up */
  403. pc->error = IDE_DRV_ERROR_GENERAL;
  404. }
  405. drive->failed_pc = NULL;
  406. drive->pc_callback(drive, 0);
  407. ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
  408. return ide_stopped;
  409. }
  410. ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries,
  411. pc->c[0]);
  412. pc->retries++;
  413. return ide_issue_pc(drive, cmd);
  414. }
  415. /* A mode sense command is used to "sense" tape parameters. */
  416. static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
  417. {
  418. ide_init_pc(pc);
  419. pc->c[0] = MODE_SENSE;
  420. if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
  421. /* DBD = 1 - Don't return block descriptors */
  422. pc->c[1] = 8;
  423. pc->c[2] = page_code;
  424. /*
  425. * Changed pc->c[3] to 0 (255 will at best return unused info).
  426. *
  427. * For SCSI this byte is defined as subpage instead of high byte
  428. * of length and some IDE drives seem to interpret it this way
  429. * and return an error when 255 is used.
  430. */
  431. pc->c[3] = 0;
  432. /* We will just discard data in that case */
  433. pc->c[4] = 255;
  434. if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
  435. pc->req_xfer = 12;
  436. else if (page_code == IDETAPE_CAPABILITIES_PAGE)
  437. pc->req_xfer = 24;
  438. else
  439. pc->req_xfer = 50;
  440. }
  441. static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
  442. {
  443. ide_hwif_t *hwif = drive->hwif;
  444. idetape_tape_t *tape = drive->driver_data;
  445. struct ide_atapi_pc *pc = drive->pc;
  446. u8 stat;
  447. stat = hwif->tp_ops->read_status(hwif);
  448. if (stat & ATA_DSC) {
  449. if (stat & ATA_ERR) {
  450. /* Error detected */
  451. if (pc->c[0] != TEST_UNIT_READY)
  452. printk(KERN_ERR "ide-tape: %s: I/O error, ",
  453. tape->name);
  454. /* Retry operation */
  455. ide_retry_pc(drive);
  456. return ide_stopped;
  457. }
  458. pc->error = 0;
  459. } else {
  460. pc->error = IDE_DRV_ERROR_GENERAL;
  461. drive->failed_pc = NULL;
  462. }
  463. drive->pc_callback(drive, 0);
  464. return ide_stopped;
  465. }
  466. static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
  467. struct ide_atapi_pc *pc, struct request *rq,
  468. u8 opcode)
  469. {
  470. unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
  471. ide_init_pc(pc);
  472. put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
  473. pc->c[1] = 1;
  474. if (blk_rq_bytes(rq) == tape->buffer_size)
  475. pc->flags |= PC_FLAG_DMA_OK;
  476. if (opcode == READ_6)
  477. pc->c[0] = READ_6;
  478. else if (opcode == WRITE_6) {
  479. pc->c[0] = WRITE_6;
  480. pc->flags |= PC_FLAG_WRITING;
  481. }
  482. memcpy(rq->cmd, pc->c, 12);
  483. }
  484. static ide_startstop_t idetape_do_request(ide_drive_t *drive,
  485. struct request *rq, sector_t block)
  486. {
  487. ide_hwif_t *hwif = drive->hwif;
  488. idetape_tape_t *tape = drive->driver_data;
  489. struct ide_atapi_pc *pc = NULL;
  490. struct ide_cmd cmd;
  491. u8 stat;
  492. ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
  493. rq->cmd[0], (unsigned long long)blk_rq_pos(rq),
  494. blk_rq_sectors(rq));
  495. BUG_ON(!(rq->cmd_type == REQ_TYPE_DRV_PRIV ||
  496. rq->cmd_type == REQ_TYPE_ATA_SENSE));
  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_RECLAIM);
  733. rq->cmd_type = REQ_TYPE_DRV_PRIV;
  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_RECLAIM);
  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 long do_idetape_chrdev_ioctl(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. static long idetape_chrdev_ioctl(struct file *file,
  1232. unsigned int cmd, unsigned long arg)
  1233. {
  1234. long ret;
  1235. mutex_lock(&ide_tape_mutex);
  1236. ret = do_idetape_chrdev_ioctl(file, cmd, arg);
  1237. mutex_unlock(&ide_tape_mutex);
  1238. return ret;
  1239. }
  1240. /*
  1241. * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
  1242. * block size with the reported value.
  1243. */
  1244. static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
  1245. {
  1246. idetape_tape_t *tape = drive->driver_data;
  1247. struct ide_atapi_pc pc;
  1248. u8 buf[12];
  1249. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  1250. if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
  1251. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  1252. if (tape->blk_size == 0) {
  1253. printk(KERN_WARNING "ide-tape: Cannot deal with zero "
  1254. "block size, assuming 32k\n");
  1255. tape->blk_size = 32768;
  1256. }
  1257. return;
  1258. }
  1259. tape->blk_size = (buf[4 + 5] << 16) +
  1260. (buf[4 + 6] << 8) +
  1261. buf[4 + 7];
  1262. tape->drv_write_prot = (buf[2] & 0x80) >> 7;
  1263. ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
  1264. tape->blk_size, tape->drv_write_prot);
  1265. }
  1266. static int idetape_chrdev_open(struct inode *inode, struct file *filp)
  1267. {
  1268. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  1269. ide_drive_t *drive;
  1270. idetape_tape_t *tape;
  1271. int retval;
  1272. if (i >= MAX_HWIFS * MAX_DRIVES)
  1273. return -ENXIO;
  1274. mutex_lock(&idetape_chrdev_mutex);
  1275. tape = ide_tape_get(NULL, true, i);
  1276. if (!tape) {
  1277. mutex_unlock(&idetape_chrdev_mutex);
  1278. return -ENXIO;
  1279. }
  1280. drive = tape->drive;
  1281. filp->private_data = tape;
  1282. ide_debug_log(IDE_DBG_FUNC, "enter");
  1283. /*
  1284. * We really want to do nonseekable_open(inode, filp); here, but some
  1285. * versions of tar incorrectly call lseek on tapes and bail out if that
  1286. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  1287. */
  1288. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  1289. if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
  1290. retval = -EBUSY;
  1291. goto out_put_tape;
  1292. }
  1293. retval = idetape_wait_ready(drive, 60 * HZ);
  1294. if (retval) {
  1295. clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
  1296. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  1297. goto out_put_tape;
  1298. }
  1299. ide_tape_read_position(drive);
  1300. if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
  1301. (void)idetape_rewind_tape(drive);
  1302. /* Read block size and write protect status from drive. */
  1303. ide_tape_get_bsize_from_bdesc(drive);
  1304. /* Set write protect flag if device is opened as read-only. */
  1305. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  1306. tape->write_prot = 1;
  1307. else
  1308. tape->write_prot = tape->drv_write_prot;
  1309. /* Make sure drive isn't write protected if user wants to write. */
  1310. if (tape->write_prot) {
  1311. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  1312. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  1313. clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
  1314. retval = -EROFS;
  1315. goto out_put_tape;
  1316. }
  1317. }
  1318. /* Lock the tape drive door so user can't eject. */
  1319. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1320. if (!ide_set_media_lock(drive, tape->disk, 1)) {
  1321. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  1322. tape->door_locked = DOOR_LOCKED;
  1323. }
  1324. }
  1325. mutex_unlock(&idetape_chrdev_mutex);
  1326. return 0;
  1327. out_put_tape:
  1328. ide_tape_put(tape);
  1329. mutex_unlock(&idetape_chrdev_mutex);
  1330. return retval;
  1331. }
  1332. static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
  1333. {
  1334. idetape_tape_t *tape = drive->driver_data;
  1335. ide_tape_flush_merge_buffer(drive);
  1336. tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
  1337. if (tape->buf != NULL) {
  1338. idetape_pad_zeros(drive, tape->blk_size *
  1339. (tape->user_bs_factor - 1));
  1340. kfree(tape->buf);
  1341. tape->buf = NULL;
  1342. }
  1343. idetape_write_filemark(drive);
  1344. idetape_flush_tape_buffers(drive);
  1345. idetape_flush_tape_buffers(drive);
  1346. }
  1347. static int idetape_chrdev_release(struct inode *inode, struct file *filp)
  1348. {
  1349. struct ide_tape_obj *tape = filp->private_data;
  1350. ide_drive_t *drive = tape->drive;
  1351. unsigned int minor = iminor(inode);
  1352. mutex_lock(&idetape_chrdev_mutex);
  1353. tape = drive->driver_data;
  1354. ide_debug_log(IDE_DBG_FUNC, "enter");
  1355. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  1356. idetape_write_release(drive, minor);
  1357. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1358. if (minor < 128)
  1359. ide_tape_discard_merge_buffer(drive, 1);
  1360. }
  1361. if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
  1362. &drive->atapi_flags))
  1363. (void) idetape_rewind_tape(drive);
  1364. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1365. if (tape->door_locked == DOOR_LOCKED) {
  1366. if (!ide_set_media_lock(drive, tape->disk, 0))
  1367. tape->door_locked = DOOR_UNLOCKED;
  1368. }
  1369. }
  1370. clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
  1371. ide_tape_put(tape);
  1372. mutex_unlock(&idetape_chrdev_mutex);
  1373. return 0;
  1374. }
  1375. static void idetape_get_inquiry_results(ide_drive_t *drive)
  1376. {
  1377. idetape_tape_t *tape = drive->driver_data;
  1378. struct ide_atapi_pc pc;
  1379. u8 pc_buf[256];
  1380. char fw_rev[4], vendor_id[8], product_id[16];
  1381. idetape_create_inquiry_cmd(&pc);
  1382. if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
  1383. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
  1384. tape->name);
  1385. return;
  1386. }
  1387. memcpy(vendor_id, &pc_buf[8], 8);
  1388. memcpy(product_id, &pc_buf[16], 16);
  1389. memcpy(fw_rev, &pc_buf[32], 4);
  1390. ide_fixstring(vendor_id, 8, 0);
  1391. ide_fixstring(product_id, 16, 0);
  1392. ide_fixstring(fw_rev, 4, 0);
  1393. printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
  1394. drive->name, tape->name, vendor_id, product_id, fw_rev);
  1395. }
  1396. /*
  1397. * Ask the tape about its various parameters. In particular, we will adjust our
  1398. * data transfer buffer size to the recommended value as returned by the tape.
  1399. */
  1400. static void idetape_get_mode_sense_results(ide_drive_t *drive)
  1401. {
  1402. idetape_tape_t *tape = drive->driver_data;
  1403. struct ide_atapi_pc pc;
  1404. u8 buf[24], *caps;
  1405. u8 speed, max_speed;
  1406. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  1407. if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
  1408. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
  1409. " some default values\n");
  1410. tape->blk_size = 512;
  1411. put_unaligned(52, (u16 *)&tape->caps[12]);
  1412. put_unaligned(540, (u16 *)&tape->caps[14]);
  1413. put_unaligned(6*52, (u16 *)&tape->caps[16]);
  1414. return;
  1415. }
  1416. caps = buf + 4 + buf[3];
  1417. /* convert to host order and save for later use */
  1418. speed = be16_to_cpup((__be16 *)&caps[14]);
  1419. max_speed = be16_to_cpup((__be16 *)&caps[8]);
  1420. *(u16 *)&caps[8] = max_speed;
  1421. *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
  1422. *(u16 *)&caps[14] = speed;
  1423. *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
  1424. if (!speed) {
  1425. printk(KERN_INFO "ide-tape: %s: invalid tape speed "
  1426. "(assuming 650KB/sec)\n", drive->name);
  1427. *(u16 *)&caps[14] = 650;
  1428. }
  1429. if (!max_speed) {
  1430. printk(KERN_INFO "ide-tape: %s: invalid max_speed "
  1431. "(assuming 650KB/sec)\n", drive->name);
  1432. *(u16 *)&caps[8] = 650;
  1433. }
  1434. memcpy(&tape->caps, caps, 20);
  1435. /* device lacks locking support according to capabilities page */
  1436. if ((caps[6] & 1) == 0)
  1437. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  1438. if (caps[7] & 0x02)
  1439. tape->blk_size = 512;
  1440. else if (caps[7] & 0x04)
  1441. tape->blk_size = 1024;
  1442. }
  1443. #ifdef CONFIG_IDE_PROC_FS
  1444. #define ide_tape_devset_get(name, field) \
  1445. static int get_##name(ide_drive_t *drive) \
  1446. { \
  1447. idetape_tape_t *tape = drive->driver_data; \
  1448. return tape->field; \
  1449. }
  1450. #define ide_tape_devset_set(name, field) \
  1451. static int set_##name(ide_drive_t *drive, int arg) \
  1452. { \
  1453. idetape_tape_t *tape = drive->driver_data; \
  1454. tape->field = arg; \
  1455. return 0; \
  1456. }
  1457. #define ide_tape_devset_rw_field(_name, _field) \
  1458. ide_tape_devset_get(_name, _field) \
  1459. ide_tape_devset_set(_name, _field) \
  1460. IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
  1461. #define ide_tape_devset_r_field(_name, _field) \
  1462. ide_tape_devset_get(_name, _field) \
  1463. IDE_DEVSET(_name, 0, get_##_name, NULL)
  1464. static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
  1465. static int divf_tdsc(ide_drive_t *drive) { return HZ; }
  1466. static int divf_buffer(ide_drive_t *drive) { return 2; }
  1467. static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
  1468. ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
  1469. ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
  1470. ide_tape_devset_r_field(avg_speed, avg_speed);
  1471. ide_tape_devset_r_field(speed, caps[14]);
  1472. ide_tape_devset_r_field(buffer, caps[16]);
  1473. ide_tape_devset_r_field(buffer_size, buffer_size);
  1474. static const struct ide_proc_devset idetape_settings[] = {
  1475. __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
  1476. __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
  1477. __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
  1478. __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
  1479. __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
  1480. __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
  1481. mulf_tdsc, divf_tdsc),
  1482. { NULL },
  1483. };
  1484. #endif
  1485. /*
  1486. * The function below is called to:
  1487. *
  1488. * 1. Initialize our various state variables.
  1489. * 2. Ask the tape for its capabilities.
  1490. * 3. Allocate a buffer which will be used for data transfer. The buffer size
  1491. * is chosen based on the recommendation which we received in step 2.
  1492. *
  1493. * Note that at this point ide.c already assigned us an irq, so that we can
  1494. * queue requests here and wait for their completion.
  1495. */
  1496. static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
  1497. {
  1498. unsigned long t;
  1499. int speed;
  1500. int buffer_size;
  1501. u16 *ctl = (u16 *)&tape->caps[12];
  1502. ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
  1503. drive->pc_callback = ide_tape_callback;
  1504. drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
  1505. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  1506. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  1507. tape->name);
  1508. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1509. }
  1510. /* Seagate Travan drives do not support DSC overlap. */
  1511. if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
  1512. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1513. tape->minor = minor;
  1514. tape->name[0] = 'h';
  1515. tape->name[1] = 't';
  1516. tape->name[2] = '0' + minor;
  1517. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1518. idetape_get_inquiry_results(drive);
  1519. idetape_get_mode_sense_results(drive);
  1520. ide_tape_get_bsize_from_bdesc(drive);
  1521. tape->user_bs_factor = 1;
  1522. tape->buffer_size = *ctl * tape->blk_size;
  1523. while (tape->buffer_size > 0xffff) {
  1524. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  1525. *ctl /= 2;
  1526. tape->buffer_size = *ctl * tape->blk_size;
  1527. }
  1528. buffer_size = tape->buffer_size;
  1529. /* select the "best" DSC read/write polling freq */
  1530. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  1531. t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
  1532. /*
  1533. * Ensure that the number we got makes sense; limit it within
  1534. * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  1535. */
  1536. tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
  1537. IDETAPE_DSC_RW_MAX);
  1538. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  1539. "%ums tDSC%s\n",
  1540. drive->name, tape->name, *(u16 *)&tape->caps[14],
  1541. (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
  1542. tape->buffer_size / 1024,
  1543. jiffies_to_msecs(tape->best_dsc_rw_freq),
  1544. (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
  1545. ide_proc_register_driver(drive, tape->driver);
  1546. }
  1547. static void ide_tape_remove(ide_drive_t *drive)
  1548. {
  1549. idetape_tape_t *tape = drive->driver_data;
  1550. ide_proc_unregister_driver(drive, tape->driver);
  1551. device_del(&tape->dev);
  1552. ide_unregister_region(tape->disk);
  1553. mutex_lock(&idetape_ref_mutex);
  1554. put_device(&tape->dev);
  1555. mutex_unlock(&idetape_ref_mutex);
  1556. }
  1557. static void ide_tape_release(struct device *dev)
  1558. {
  1559. struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
  1560. ide_drive_t *drive = tape->drive;
  1561. struct gendisk *g = tape->disk;
  1562. BUG_ON(tape->valid);
  1563. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1564. drive->driver_data = NULL;
  1565. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  1566. device_destroy(idetape_sysfs_class,
  1567. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  1568. idetape_devs[tape->minor] = NULL;
  1569. g->private_data = NULL;
  1570. put_disk(g);
  1571. kfree(tape);
  1572. }
  1573. #ifdef CONFIG_IDE_PROC_FS
  1574. static int idetape_name_proc_show(struct seq_file *m, void *v)
  1575. {
  1576. ide_drive_t *drive = (ide_drive_t *) m->private;
  1577. idetape_tape_t *tape = drive->driver_data;
  1578. seq_printf(m, "%s\n", tape->name);
  1579. return 0;
  1580. }
  1581. static int idetape_name_proc_open(struct inode *inode, struct file *file)
  1582. {
  1583. return single_open(file, idetape_name_proc_show, PDE_DATA(inode));
  1584. }
  1585. static const struct file_operations idetape_name_proc_fops = {
  1586. .owner = THIS_MODULE,
  1587. .open = idetape_name_proc_open,
  1588. .read = seq_read,
  1589. .llseek = seq_lseek,
  1590. .release = single_release,
  1591. };
  1592. static ide_proc_entry_t idetape_proc[] = {
  1593. { "capacity", S_IFREG|S_IRUGO, &ide_capacity_proc_fops },
  1594. { "name", S_IFREG|S_IRUGO, &idetape_name_proc_fops },
  1595. {}
  1596. };
  1597. static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
  1598. {
  1599. return idetape_proc;
  1600. }
  1601. static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
  1602. {
  1603. return idetape_settings;
  1604. }
  1605. #endif
  1606. static int ide_tape_probe(ide_drive_t *);
  1607. static struct ide_driver idetape_driver = {
  1608. .gen_driver = {
  1609. .owner = THIS_MODULE,
  1610. .name = "ide-tape",
  1611. .bus = &ide_bus_type,
  1612. },
  1613. .probe = ide_tape_probe,
  1614. .remove = ide_tape_remove,
  1615. .version = IDETAPE_VERSION,
  1616. .do_request = idetape_do_request,
  1617. #ifdef CONFIG_IDE_PROC_FS
  1618. .proc_entries = ide_tape_proc_entries,
  1619. .proc_devsets = ide_tape_proc_devsets,
  1620. #endif
  1621. };
  1622. /* Our character device supporting functions, passed to register_chrdev. */
  1623. static const struct file_operations idetape_fops = {
  1624. .owner = THIS_MODULE,
  1625. .read = idetape_chrdev_read,
  1626. .write = idetape_chrdev_write,
  1627. .unlocked_ioctl = idetape_chrdev_ioctl,
  1628. .open = idetape_chrdev_open,
  1629. .release = idetape_chrdev_release,
  1630. .llseek = noop_llseek,
  1631. };
  1632. static int idetape_open(struct block_device *bdev, fmode_t mode)
  1633. {
  1634. struct ide_tape_obj *tape;
  1635. mutex_lock(&ide_tape_mutex);
  1636. tape = ide_tape_get(bdev->bd_disk, false, 0);
  1637. mutex_unlock(&ide_tape_mutex);
  1638. if (!tape)
  1639. return -ENXIO;
  1640. return 0;
  1641. }
  1642. static void idetape_release(struct gendisk *disk, fmode_t mode)
  1643. {
  1644. struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
  1645. mutex_lock(&ide_tape_mutex);
  1646. ide_tape_put(tape);
  1647. mutex_unlock(&ide_tape_mutex);
  1648. }
  1649. static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
  1650. unsigned int cmd, unsigned long arg)
  1651. {
  1652. struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
  1653. ide_drive_t *drive = tape->drive;
  1654. int err;
  1655. mutex_lock(&ide_tape_mutex);
  1656. err = generic_ide_ioctl(drive, bdev, cmd, arg);
  1657. if (err == -EINVAL)
  1658. err = idetape_blkdev_ioctl(drive, cmd, arg);
  1659. mutex_unlock(&ide_tape_mutex);
  1660. return err;
  1661. }
  1662. static const struct block_device_operations idetape_block_ops = {
  1663. .owner = THIS_MODULE,
  1664. .open = idetape_open,
  1665. .release = idetape_release,
  1666. .ioctl = idetape_ioctl,
  1667. };
  1668. static int ide_tape_probe(ide_drive_t *drive)
  1669. {
  1670. idetape_tape_t *tape;
  1671. struct gendisk *g;
  1672. int minor;
  1673. ide_debug_log(IDE_DBG_FUNC, "enter");
  1674. if (!strstr(DRV_NAME, drive->driver_req))
  1675. goto failed;
  1676. if (drive->media != ide_tape)
  1677. goto failed;
  1678. if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
  1679. ide_check_atapi_device(drive, DRV_NAME) == 0) {
  1680. printk(KERN_ERR "ide-tape: %s: not supported by this version of"
  1681. " the driver\n", drive->name);
  1682. goto failed;
  1683. }
  1684. tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
  1685. if (tape == NULL) {
  1686. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
  1687. drive->name);
  1688. goto failed;
  1689. }
  1690. g = alloc_disk(1 << PARTN_BITS);
  1691. if (!g)
  1692. goto out_free_tape;
  1693. ide_init_disk(g, drive);
  1694. tape->dev.parent = &drive->gendev;
  1695. tape->dev.release = ide_tape_release;
  1696. dev_set_name(&tape->dev, "%s", dev_name(&drive->gendev));
  1697. if (device_register(&tape->dev))
  1698. goto out_free_disk;
  1699. tape->drive = drive;
  1700. tape->driver = &idetape_driver;
  1701. tape->disk = g;
  1702. g->private_data = &tape->driver;
  1703. drive->driver_data = tape;
  1704. mutex_lock(&idetape_ref_mutex);
  1705. for (minor = 0; idetape_devs[minor]; minor++)
  1706. ;
  1707. idetape_devs[minor] = tape;
  1708. mutex_unlock(&idetape_ref_mutex);
  1709. idetape_setup(drive, tape, minor);
  1710. device_create(idetape_sysfs_class, &drive->gendev,
  1711. MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
  1712. device_create(idetape_sysfs_class, &drive->gendev,
  1713. MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
  1714. "n%s", tape->name);
  1715. g->fops = &idetape_block_ops;
  1716. ide_register_region(g);
  1717. return 0;
  1718. out_free_disk:
  1719. put_disk(g);
  1720. out_free_tape:
  1721. kfree(tape);
  1722. failed:
  1723. return -ENODEV;
  1724. }
  1725. static void __exit idetape_exit(void)
  1726. {
  1727. driver_unregister(&idetape_driver.gen_driver);
  1728. class_destroy(idetape_sysfs_class);
  1729. unregister_chrdev(IDETAPE_MAJOR, "ht");
  1730. }
  1731. static int __init idetape_init(void)
  1732. {
  1733. int error = 1;
  1734. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  1735. if (IS_ERR(idetape_sysfs_class)) {
  1736. idetape_sysfs_class = NULL;
  1737. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  1738. error = -EBUSY;
  1739. goto out;
  1740. }
  1741. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  1742. printk(KERN_ERR "ide-tape: Failed to register chrdev"
  1743. " interface\n");
  1744. error = -EBUSY;
  1745. goto out_free_class;
  1746. }
  1747. error = driver_register(&idetape_driver.gen_driver);
  1748. if (error)
  1749. goto out_free_driver;
  1750. return 0;
  1751. out_free_driver:
  1752. driver_unregister(&idetape_driver.gen_driver);
  1753. out_free_class:
  1754. class_destroy(idetape_sysfs_class);
  1755. out:
  1756. return error;
  1757. }
  1758. MODULE_ALIAS("ide:*m-tape*");
  1759. module_init(idetape_init);
  1760. module_exit(idetape_exit);
  1761. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
  1762. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  1763. MODULE_LICENSE("GPL");