ide-tape.c 55 KB

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