ide-tape.c 55 KB

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