ide-tape.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  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 blocks)
  764. {
  765. idetape_tape_t *tape = drive->driver_data;
  766. size_t size = blocks * tape->blk_size;
  767. struct request *rq;
  768. int ret;
  769. debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
  770. BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
  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. /* Queue up a character device originated write request. */
  827. static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
  828. {
  829. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  830. return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks);
  831. }
  832. static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
  833. {
  834. idetape_tape_t *tape = drive->driver_data;
  835. int blocks;
  836. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  837. printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
  838. " but we are not writing.\n");
  839. return;
  840. }
  841. if (tape->buf) {
  842. blocks = tape->valid / tape->blk_size;
  843. if (tape->valid % tape->blk_size) {
  844. blocks++;
  845. memset(tape->buf + tape->valid, 0,
  846. tape->blk_size - tape->valid % tape->blk_size);
  847. }
  848. (void) idetape_add_chrdev_write_request(drive, blocks);
  849. }
  850. if (tape->buf != NULL) {
  851. kfree(tape->buf);
  852. tape->buf = NULL;
  853. }
  854. tape->chrdev_dir = IDETAPE_DIR_NONE;
  855. }
  856. static int idetape_init_read(ide_drive_t *drive)
  857. {
  858. idetape_tape_t *tape = drive->driver_data;
  859. int bytes_read;
  860. /* Initialize read operation */
  861. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  862. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  863. ide_tape_flush_merge_buffer(drive);
  864. idetape_flush_tape_buffers(drive);
  865. }
  866. if (tape->buf || tape->valid) {
  867. printk(KERN_ERR "ide-tape: valid should be 0 now\n");
  868. tape->valid = 0;
  869. }
  870. tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
  871. if (!tape->buf)
  872. return -ENOMEM;
  873. tape->chrdev_dir = IDETAPE_DIR_READ;
  874. tape->cur = tape->buf;
  875. /*
  876. * Issue a read 0 command to ensure that DSC handshake is
  877. * switched from completion mode to buffer available mode.
  878. * No point in issuing this if DSC overlap isn't supported, some
  879. * drives (Seagate STT3401A) will return an error.
  880. */
  881. if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
  882. bytes_read = idetape_queue_rw_tail(drive,
  883. REQ_IDETAPE_READ, 0);
  884. if (bytes_read < 0) {
  885. kfree(tape->buf);
  886. tape->buf = NULL;
  887. tape->chrdev_dir = IDETAPE_DIR_NONE;
  888. return bytes_read;
  889. }
  890. }
  891. }
  892. return 0;
  893. }
  894. /* called from idetape_chrdev_read() to service a chrdev read request. */
  895. static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
  896. {
  897. debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
  898. /* If we are at a filemark, return a read length of 0 */
  899. if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
  900. return 0;
  901. idetape_init_read(drive);
  902. return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks);
  903. }
  904. static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
  905. {
  906. idetape_tape_t *tape = drive->driver_data;
  907. memset(tape->buf, 0, tape->buffer_size);
  908. while (bcount) {
  909. unsigned int count = min(tape->buffer_size, bcount);
  910. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
  911. count / tape->blk_size);
  912. bcount -= count;
  913. }
  914. }
  915. /*
  916. * Rewinds the tape to the Beginning Of the current Partition (BOP). We
  917. * currently support only one partition.
  918. */
  919. static int idetape_rewind_tape(ide_drive_t *drive)
  920. {
  921. struct ide_tape_obj *tape = drive->driver_data;
  922. struct gendisk *disk = tape->disk;
  923. int retval;
  924. struct ide_atapi_pc pc;
  925. debug_log(DBG_SENSE, "Enter %s\n", __func__);
  926. idetape_create_rewind_cmd(drive, &pc);
  927. retval = ide_queue_pc_tail(drive, disk, &pc);
  928. if (retval)
  929. return retval;
  930. idetape_create_read_position_cmd(&pc);
  931. retval = ide_queue_pc_tail(drive, disk, &pc);
  932. if (retval)
  933. return retval;
  934. return 0;
  935. }
  936. /* mtio.h compatible commands should be issued to the chrdev interface. */
  937. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
  938. unsigned long arg)
  939. {
  940. idetape_tape_t *tape = drive->driver_data;
  941. void __user *argp = (void __user *)arg;
  942. struct idetape_config {
  943. int dsc_rw_frequency;
  944. int dsc_media_access_frequency;
  945. int nr_stages;
  946. } config;
  947. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  948. switch (cmd) {
  949. case 0x0340:
  950. if (copy_from_user(&config, argp, sizeof(config)))
  951. return -EFAULT;
  952. tape->best_dsc_rw_freq = config.dsc_rw_frequency;
  953. break;
  954. case 0x0350:
  955. config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
  956. config.nr_stages = 1;
  957. if (copy_to_user(argp, &config, sizeof(config)))
  958. return -EFAULT;
  959. break;
  960. default:
  961. return -EIO;
  962. }
  963. return 0;
  964. }
  965. static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
  966. int mt_count)
  967. {
  968. idetape_tape_t *tape = drive->driver_data;
  969. struct gendisk *disk = tape->disk;
  970. struct ide_atapi_pc pc;
  971. int retval, count = 0;
  972. int sprev = !!(tape->caps[4] & 0x20);
  973. if (mt_count == 0)
  974. return 0;
  975. if (MTBSF == mt_op || MTBSFM == mt_op) {
  976. if (!sprev)
  977. return -EIO;
  978. mt_count = -mt_count;
  979. }
  980. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  981. tape->valid = 0;
  982. if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
  983. ++count;
  984. ide_tape_discard_merge_buffer(drive, 0);
  985. }
  986. switch (mt_op) {
  987. case MTFSF:
  988. case MTBSF:
  989. idetape_create_space_cmd(&pc, mt_count - count,
  990. IDETAPE_SPACE_OVER_FILEMARK);
  991. return ide_queue_pc_tail(drive, disk, &pc);
  992. case MTFSFM:
  993. case MTBSFM:
  994. if (!sprev)
  995. return -EIO;
  996. retval = idetape_space_over_filemarks(drive, MTFSF,
  997. mt_count - count);
  998. if (retval)
  999. return retval;
  1000. count = (MTBSFM == mt_op ? 1 : -1);
  1001. return idetape_space_over_filemarks(drive, MTFSF, count);
  1002. default:
  1003. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1004. mt_op);
  1005. return -EIO;
  1006. }
  1007. }
  1008. /*
  1009. * Our character device read / write functions.
  1010. *
  1011. * The tape is optimized to maximize throughput when it is transferring an
  1012. * integral number of the "continuous transfer limit", which is a parameter of
  1013. * the specific tape (26kB on my particular tape, 32kB for Onstream).
  1014. *
  1015. * As of version 1.3 of the driver, the character device provides an abstract
  1016. * continuous view of the media - any mix of block sizes (even 1 byte) on the
  1017. * same backup/restore procedure is supported. The driver will internally
  1018. * convert the requests to the recommended transfer unit, so that an unmatch
  1019. * between the user's block size to the recommended size will only result in a
  1020. * (slightly) increased driver overhead, but will no longer hit performance.
  1021. * This is not applicable to Onstream.
  1022. */
  1023. static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
  1024. size_t count, loff_t *ppos)
  1025. {
  1026. struct ide_tape_obj *tape = file->private_data;
  1027. ide_drive_t *drive = tape->drive;
  1028. ssize_t bytes_read, temp, actually_read = 0, rc;
  1029. ssize_t ret = 0;
  1030. u16 ctl = *(u16 *)&tape->caps[12];
  1031. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1032. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1033. if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
  1034. if (count > tape->blk_size &&
  1035. (count % tape->blk_size) == 0)
  1036. tape->user_bs_factor = count / tape->blk_size;
  1037. }
  1038. rc = idetape_init_read(drive);
  1039. if (rc < 0)
  1040. return rc;
  1041. if (count == 0)
  1042. return (0);
  1043. if (tape->valid) {
  1044. actually_read = min_t(unsigned int, tape->valid, count);
  1045. if (copy_to_user(buf, tape->cur, actually_read))
  1046. ret = -EFAULT;
  1047. buf += actually_read;
  1048. count -= actually_read;
  1049. tape->cur += actually_read;
  1050. tape->valid -= actually_read;
  1051. }
  1052. while (count >= tape->buffer_size) {
  1053. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1054. if (bytes_read <= 0)
  1055. goto finish;
  1056. if (copy_to_user(buf, tape->cur, bytes_read))
  1057. ret = -EFAULT;
  1058. buf += bytes_read;
  1059. count -= bytes_read;
  1060. actually_read += bytes_read;
  1061. }
  1062. if (count) {
  1063. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1064. if (bytes_read <= 0)
  1065. goto finish;
  1066. temp = min((unsigned long)count, (unsigned long)bytes_read);
  1067. if (copy_to_user(buf, tape->cur, temp))
  1068. ret = -EFAULT;
  1069. actually_read += temp;
  1070. tape->cur += temp;
  1071. tape->valid -= temp;
  1072. }
  1073. finish:
  1074. if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
  1075. debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
  1076. idetape_space_over_filemarks(drive, MTFSF, 1);
  1077. return 0;
  1078. }
  1079. return ret ? ret : actually_read;
  1080. }
  1081. static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
  1082. size_t count, loff_t *ppos)
  1083. {
  1084. struct ide_tape_obj *tape = file->private_data;
  1085. ide_drive_t *drive = tape->drive;
  1086. ssize_t actually_written = 0;
  1087. ssize_t ret = 0;
  1088. u16 ctl = *(u16 *)&tape->caps[12];
  1089. /* The drive is write protected. */
  1090. if (tape->write_prot)
  1091. return -EACCES;
  1092. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1093. /* Initialize write operation */
  1094. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1095. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1096. ide_tape_discard_merge_buffer(drive, 1);
  1097. if (tape->buf || tape->valid) {
  1098. printk(KERN_ERR "ide-tape: valid should be 0 now\n");
  1099. tape->valid = 0;
  1100. }
  1101. tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
  1102. if (!tape->buf)
  1103. return -ENOMEM;
  1104. tape->chrdev_dir = IDETAPE_DIR_WRITE;
  1105. tape->cur = tape->buf;
  1106. /*
  1107. * Issue a write 0 command to ensure that DSC handshake is
  1108. * switched from completion mode to buffer available mode. No
  1109. * point in issuing this if DSC overlap isn't supported, some
  1110. * drives (Seagate STT3401A) will return an error.
  1111. */
  1112. if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
  1113. ssize_t retval = idetape_queue_rw_tail(drive,
  1114. REQ_IDETAPE_WRITE, 0);
  1115. if (retval < 0) {
  1116. kfree(tape->buf);
  1117. tape->buf = NULL;
  1118. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1119. return retval;
  1120. }
  1121. }
  1122. }
  1123. if (count == 0)
  1124. return (0);
  1125. if (tape->valid < tape->buffer_size) {
  1126. actually_written = min_t(unsigned int,
  1127. tape->buffer_size - tape->valid,
  1128. count);
  1129. if (copy_from_user(tape->cur, buf, actually_written))
  1130. ret = -EFAULT;
  1131. buf += actually_written;
  1132. count -= actually_written;
  1133. tape->cur += actually_written;
  1134. tape->valid += actually_written;
  1135. if (tape->valid == tape->buffer_size) {
  1136. ssize_t retval;
  1137. retval = idetape_add_chrdev_write_request(drive, ctl);
  1138. if (retval <= 0)
  1139. return (retval);
  1140. }
  1141. }
  1142. while (count >= tape->buffer_size) {
  1143. ssize_t retval;
  1144. if (copy_from_user(tape->cur, buf, tape->buffer_size))
  1145. ret = -EFAULT;
  1146. buf += tape->buffer_size;
  1147. count -= tape->buffer_size;
  1148. retval = idetape_add_chrdev_write_request(drive, ctl);
  1149. actually_written += tape->buffer_size;
  1150. if (retval <= 0)
  1151. return (retval);
  1152. }
  1153. if (count) {
  1154. actually_written += count;
  1155. if (copy_from_user(tape->cur, buf, count))
  1156. ret = -EFAULT;
  1157. tape->cur += count;
  1158. tape->valid += count;
  1159. }
  1160. return ret ? ret : actually_written;
  1161. }
  1162. static int idetape_write_filemark(ide_drive_t *drive)
  1163. {
  1164. struct ide_tape_obj *tape = drive->driver_data;
  1165. struct ide_atapi_pc pc;
  1166. /* Write a filemark */
  1167. idetape_create_write_filemark_cmd(drive, &pc, 1);
  1168. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1169. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  1170. return -EIO;
  1171. }
  1172. return 0;
  1173. }
  1174. /*
  1175. * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
  1176. * requested.
  1177. *
  1178. * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
  1179. * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
  1180. * usually not supported.
  1181. *
  1182. * The following commands are currently not supported:
  1183. *
  1184. * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
  1185. * MT_ST_WRITE_THRESHOLD.
  1186. */
  1187. static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
  1188. {
  1189. idetape_tape_t *tape = drive->driver_data;
  1190. struct gendisk *disk = tape->disk;
  1191. struct ide_atapi_pc pc;
  1192. int i, retval;
  1193. debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
  1194. mt_op, mt_count);
  1195. switch (mt_op) {
  1196. case MTFSF:
  1197. case MTFSFM:
  1198. case MTBSF:
  1199. case MTBSFM:
  1200. if (!mt_count)
  1201. return 0;
  1202. return idetape_space_over_filemarks(drive, mt_op, mt_count);
  1203. default:
  1204. break;
  1205. }
  1206. switch (mt_op) {
  1207. case MTWEOF:
  1208. if (tape->write_prot)
  1209. return -EACCES;
  1210. ide_tape_discard_merge_buffer(drive, 1);
  1211. for (i = 0; i < mt_count; i++) {
  1212. retval = idetape_write_filemark(drive);
  1213. if (retval)
  1214. return retval;
  1215. }
  1216. return 0;
  1217. case MTREW:
  1218. ide_tape_discard_merge_buffer(drive, 0);
  1219. if (idetape_rewind_tape(drive))
  1220. return -EIO;
  1221. return 0;
  1222. case MTLOAD:
  1223. ide_tape_discard_merge_buffer(drive, 0);
  1224. return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
  1225. case MTUNLOAD:
  1226. case MTOFFL:
  1227. /*
  1228. * If door is locked, attempt to unlock before
  1229. * attempting to eject.
  1230. */
  1231. if (tape->door_locked) {
  1232. if (!ide_set_media_lock(drive, disk, 0))
  1233. tape->door_locked = DOOR_UNLOCKED;
  1234. }
  1235. ide_tape_discard_merge_buffer(drive, 0);
  1236. retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
  1237. if (!retval)
  1238. clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
  1239. return retval;
  1240. case MTNOP:
  1241. ide_tape_discard_merge_buffer(drive, 0);
  1242. return idetape_flush_tape_buffers(drive);
  1243. case MTRETEN:
  1244. ide_tape_discard_merge_buffer(drive, 0);
  1245. return ide_do_start_stop(drive, disk,
  1246. IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  1247. case MTEOM:
  1248. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  1249. return ide_queue_pc_tail(drive, disk, &pc);
  1250. case MTERASE:
  1251. (void)idetape_rewind_tape(drive);
  1252. idetape_create_erase_cmd(&pc);
  1253. return ide_queue_pc_tail(drive, disk, &pc);
  1254. case MTSETBLK:
  1255. if (mt_count) {
  1256. if (mt_count < tape->blk_size ||
  1257. mt_count % tape->blk_size)
  1258. return -EIO;
  1259. tape->user_bs_factor = mt_count / tape->blk_size;
  1260. clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
  1261. } else
  1262. set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
  1263. return 0;
  1264. case MTSEEK:
  1265. ide_tape_discard_merge_buffer(drive, 0);
  1266. return idetape_position_tape(drive,
  1267. mt_count * tape->user_bs_factor, tape->partition, 0);
  1268. case MTSETPART:
  1269. ide_tape_discard_merge_buffer(drive, 0);
  1270. return idetape_position_tape(drive, 0, mt_count, 0);
  1271. case MTFSR:
  1272. case MTBSR:
  1273. case MTLOCK:
  1274. retval = ide_set_media_lock(drive, disk, 1);
  1275. if (retval)
  1276. return retval;
  1277. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  1278. return 0;
  1279. case MTUNLOCK:
  1280. retval = ide_set_media_lock(drive, disk, 0);
  1281. if (retval)
  1282. return retval;
  1283. tape->door_locked = DOOR_UNLOCKED;
  1284. return 0;
  1285. default:
  1286. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1287. mt_op);
  1288. return -EIO;
  1289. }
  1290. }
  1291. /*
  1292. * Our character device ioctls. General mtio.h magnetic io commands are
  1293. * supported here, and not in the corresponding block interface. Our own
  1294. * ide-tape ioctls are supported on both interfaces.
  1295. */
  1296. static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
  1297. unsigned int cmd, unsigned long arg)
  1298. {
  1299. struct ide_tape_obj *tape = file->private_data;
  1300. ide_drive_t *drive = tape->drive;
  1301. struct mtop mtop;
  1302. struct mtget mtget;
  1303. struct mtpos mtpos;
  1304. int block_offset = 0, position = tape->first_frame;
  1305. void __user *argp = (void __user *)arg;
  1306. debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
  1307. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1308. ide_tape_flush_merge_buffer(drive);
  1309. idetape_flush_tape_buffers(drive);
  1310. }
  1311. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  1312. block_offset = tape->valid /
  1313. (tape->blk_size * tape->user_bs_factor);
  1314. position = idetape_read_position(drive);
  1315. if (position < 0)
  1316. return -EIO;
  1317. }
  1318. switch (cmd) {
  1319. case MTIOCTOP:
  1320. if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
  1321. return -EFAULT;
  1322. return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
  1323. case MTIOCGET:
  1324. memset(&mtget, 0, sizeof(struct mtget));
  1325. mtget.mt_type = MT_ISSCSI2;
  1326. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  1327. mtget.mt_dsreg =
  1328. ((tape->blk_size * tape->user_bs_factor)
  1329. << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  1330. if (tape->drv_write_prot)
  1331. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  1332. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  1333. return -EFAULT;
  1334. return 0;
  1335. case MTIOCPOS:
  1336. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  1337. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  1338. return -EFAULT;
  1339. return 0;
  1340. default:
  1341. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1342. ide_tape_discard_merge_buffer(drive, 1);
  1343. return idetape_blkdev_ioctl(drive, cmd, arg);
  1344. }
  1345. }
  1346. /*
  1347. * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
  1348. * block size with the reported value.
  1349. */
  1350. static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
  1351. {
  1352. idetape_tape_t *tape = drive->driver_data;
  1353. struct ide_atapi_pc pc;
  1354. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  1355. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1356. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  1357. if (tape->blk_size == 0) {
  1358. printk(KERN_WARNING "ide-tape: Cannot deal with zero "
  1359. "block size, assuming 32k\n");
  1360. tape->blk_size = 32768;
  1361. }
  1362. return;
  1363. }
  1364. tape->blk_size = (pc.buf[4 + 5] << 16) +
  1365. (pc.buf[4 + 6] << 8) +
  1366. pc.buf[4 + 7];
  1367. tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
  1368. }
  1369. static int idetape_chrdev_open(struct inode *inode, struct file *filp)
  1370. {
  1371. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  1372. ide_drive_t *drive;
  1373. idetape_tape_t *tape;
  1374. int retval;
  1375. if (i >= MAX_HWIFS * MAX_DRIVES)
  1376. return -ENXIO;
  1377. lock_kernel();
  1378. tape = ide_tape_chrdev_get(i);
  1379. if (!tape) {
  1380. unlock_kernel();
  1381. return -ENXIO;
  1382. }
  1383. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1384. /*
  1385. * We really want to do nonseekable_open(inode, filp); here, but some
  1386. * versions of tar incorrectly call lseek on tapes and bail out if that
  1387. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  1388. */
  1389. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  1390. drive = tape->drive;
  1391. filp->private_data = tape;
  1392. if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
  1393. retval = -EBUSY;
  1394. goto out_put_tape;
  1395. }
  1396. retval = idetape_wait_ready(drive, 60 * HZ);
  1397. if (retval) {
  1398. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1399. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  1400. goto out_put_tape;
  1401. }
  1402. idetape_read_position(drive);
  1403. if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
  1404. (void)idetape_rewind_tape(drive);
  1405. /* Read block size and write protect status from drive. */
  1406. ide_tape_get_bsize_from_bdesc(drive);
  1407. /* Set write protect flag if device is opened as read-only. */
  1408. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  1409. tape->write_prot = 1;
  1410. else
  1411. tape->write_prot = tape->drv_write_prot;
  1412. /* Make sure drive isn't write protected if user wants to write. */
  1413. if (tape->write_prot) {
  1414. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  1415. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  1416. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1417. retval = -EROFS;
  1418. goto out_put_tape;
  1419. }
  1420. }
  1421. /* Lock the tape drive door so user can't eject. */
  1422. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1423. if (!ide_set_media_lock(drive, tape->disk, 1)) {
  1424. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  1425. tape->door_locked = DOOR_LOCKED;
  1426. }
  1427. }
  1428. unlock_kernel();
  1429. return 0;
  1430. out_put_tape:
  1431. ide_tape_put(tape);
  1432. unlock_kernel();
  1433. return retval;
  1434. }
  1435. static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
  1436. {
  1437. idetape_tape_t *tape = drive->driver_data;
  1438. ide_tape_flush_merge_buffer(drive);
  1439. tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
  1440. if (tape->buf != NULL) {
  1441. idetape_pad_zeros(drive, tape->blk_size *
  1442. (tape->user_bs_factor - 1));
  1443. kfree(tape->buf);
  1444. tape->buf = NULL;
  1445. }
  1446. idetape_write_filemark(drive);
  1447. idetape_flush_tape_buffers(drive);
  1448. idetape_flush_tape_buffers(drive);
  1449. }
  1450. static int idetape_chrdev_release(struct inode *inode, struct file *filp)
  1451. {
  1452. struct ide_tape_obj *tape = filp->private_data;
  1453. ide_drive_t *drive = tape->drive;
  1454. unsigned int minor = iminor(inode);
  1455. lock_kernel();
  1456. tape = drive->driver_data;
  1457. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1458. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  1459. idetape_write_release(drive, minor);
  1460. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1461. if (minor < 128)
  1462. ide_tape_discard_merge_buffer(drive, 1);
  1463. }
  1464. if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
  1465. (void) idetape_rewind_tape(drive);
  1466. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1467. if (tape->door_locked == DOOR_LOCKED) {
  1468. if (!ide_set_media_lock(drive, tape->disk, 0))
  1469. tape->door_locked = DOOR_UNLOCKED;
  1470. }
  1471. }
  1472. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1473. ide_tape_put(tape);
  1474. unlock_kernel();
  1475. return 0;
  1476. }
  1477. static void idetape_get_inquiry_results(ide_drive_t *drive)
  1478. {
  1479. idetape_tape_t *tape = drive->driver_data;
  1480. struct ide_atapi_pc pc;
  1481. u8 pc_buf[256];
  1482. char fw_rev[4], vendor_id[8], product_id[16];
  1483. idetape_create_inquiry_cmd(&pc);
  1484. pc.buf = &pc_buf[0];
  1485. pc.buf_size = sizeof(pc_buf);
  1486. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1487. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
  1488. tape->name);
  1489. return;
  1490. }
  1491. memcpy(vendor_id, &pc.buf[8], 8);
  1492. memcpy(product_id, &pc.buf[16], 16);
  1493. memcpy(fw_rev, &pc.buf[32], 4);
  1494. ide_fixstring(vendor_id, 8, 0);
  1495. ide_fixstring(product_id, 16, 0);
  1496. ide_fixstring(fw_rev, 4, 0);
  1497. printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
  1498. drive->name, tape->name, vendor_id, product_id, fw_rev);
  1499. }
  1500. /*
  1501. * Ask the tape about its various parameters. In particular, we will adjust our
  1502. * data transfer buffer size to the recommended value as returned by the tape.
  1503. */
  1504. static void idetape_get_mode_sense_results(ide_drive_t *drive)
  1505. {
  1506. idetape_tape_t *tape = drive->driver_data;
  1507. struct ide_atapi_pc pc;
  1508. u8 *caps;
  1509. u8 speed, max_speed;
  1510. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  1511. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1512. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
  1513. " some default values\n");
  1514. tape->blk_size = 512;
  1515. put_unaligned(52, (u16 *)&tape->caps[12]);
  1516. put_unaligned(540, (u16 *)&tape->caps[14]);
  1517. put_unaligned(6*52, (u16 *)&tape->caps[16]);
  1518. return;
  1519. }
  1520. caps = pc.buf + 4 + pc.buf[3];
  1521. /* convert to host order and save for later use */
  1522. speed = be16_to_cpup((__be16 *)&caps[14]);
  1523. max_speed = be16_to_cpup((__be16 *)&caps[8]);
  1524. *(u16 *)&caps[8] = max_speed;
  1525. *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
  1526. *(u16 *)&caps[14] = speed;
  1527. *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
  1528. if (!speed) {
  1529. printk(KERN_INFO "ide-tape: %s: invalid tape speed "
  1530. "(assuming 650KB/sec)\n", drive->name);
  1531. *(u16 *)&caps[14] = 650;
  1532. }
  1533. if (!max_speed) {
  1534. printk(KERN_INFO "ide-tape: %s: invalid max_speed "
  1535. "(assuming 650KB/sec)\n", drive->name);
  1536. *(u16 *)&caps[8] = 650;
  1537. }
  1538. memcpy(&tape->caps, caps, 20);
  1539. /* device lacks locking support according to capabilities page */
  1540. if ((caps[6] & 1) == 0)
  1541. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  1542. if (caps[7] & 0x02)
  1543. tape->blk_size = 512;
  1544. else if (caps[7] & 0x04)
  1545. tape->blk_size = 1024;
  1546. }
  1547. #ifdef CONFIG_IDE_PROC_FS
  1548. #define ide_tape_devset_get(name, field) \
  1549. static int get_##name(ide_drive_t *drive) \
  1550. { \
  1551. idetape_tape_t *tape = drive->driver_data; \
  1552. return tape->field; \
  1553. }
  1554. #define ide_tape_devset_set(name, field) \
  1555. static int set_##name(ide_drive_t *drive, int arg) \
  1556. { \
  1557. idetape_tape_t *tape = drive->driver_data; \
  1558. tape->field = arg; \
  1559. return 0; \
  1560. }
  1561. #define ide_tape_devset_rw_field(_name, _field) \
  1562. ide_tape_devset_get(_name, _field) \
  1563. ide_tape_devset_set(_name, _field) \
  1564. IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
  1565. #define ide_tape_devset_r_field(_name, _field) \
  1566. ide_tape_devset_get(_name, _field) \
  1567. IDE_DEVSET(_name, 0, get_##_name, NULL)
  1568. static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
  1569. static int divf_tdsc(ide_drive_t *drive) { return HZ; }
  1570. static int divf_buffer(ide_drive_t *drive) { return 2; }
  1571. static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
  1572. ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
  1573. ide_tape_devset_rw_field(debug_mask, debug_mask);
  1574. ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
  1575. ide_tape_devset_r_field(avg_speed, avg_speed);
  1576. ide_tape_devset_r_field(speed, caps[14]);
  1577. ide_tape_devset_r_field(buffer, caps[16]);
  1578. ide_tape_devset_r_field(buffer_size, buffer_size);
  1579. static const struct ide_proc_devset idetape_settings[] = {
  1580. __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
  1581. __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
  1582. __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
  1583. __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
  1584. __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
  1585. __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
  1586. __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
  1587. mulf_tdsc, divf_tdsc),
  1588. { NULL },
  1589. };
  1590. #endif
  1591. /*
  1592. * The function below is called to:
  1593. *
  1594. * 1. Initialize our various state variables.
  1595. * 2. Ask the tape for its capabilities.
  1596. * 3. Allocate a buffer which will be used for data transfer. The buffer size
  1597. * is chosen based on the recommendation which we received in step 2.
  1598. *
  1599. * Note that at this point ide.c already assigned us an irq, so that we can
  1600. * queue requests here and wait for their completion.
  1601. */
  1602. static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
  1603. {
  1604. unsigned long t;
  1605. int speed;
  1606. int buffer_size;
  1607. u16 *ctl = (u16 *)&tape->caps[12];
  1608. drive->pc_callback = ide_tape_callback;
  1609. drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
  1610. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  1611. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  1612. tape->name);
  1613. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1614. }
  1615. /* Seagate Travan drives do not support DSC overlap. */
  1616. if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
  1617. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1618. tape->minor = minor;
  1619. tape->name[0] = 'h';
  1620. tape->name[1] = 't';
  1621. tape->name[2] = '0' + minor;
  1622. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1623. idetape_get_inquiry_results(drive);
  1624. idetape_get_mode_sense_results(drive);
  1625. ide_tape_get_bsize_from_bdesc(drive);
  1626. tape->user_bs_factor = 1;
  1627. tape->buffer_size = *ctl * tape->blk_size;
  1628. while (tape->buffer_size > 0xffff) {
  1629. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  1630. *ctl /= 2;
  1631. tape->buffer_size = *ctl * tape->blk_size;
  1632. }
  1633. buffer_size = tape->buffer_size;
  1634. /* select the "best" DSC read/write polling freq */
  1635. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  1636. t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
  1637. /*
  1638. * Ensure that the number we got makes sense; limit it within
  1639. * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  1640. */
  1641. tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
  1642. IDETAPE_DSC_RW_MAX);
  1643. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  1644. "%lums tDSC%s\n",
  1645. drive->name, tape->name, *(u16 *)&tape->caps[14],
  1646. (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
  1647. tape->buffer_size / 1024,
  1648. tape->best_dsc_rw_freq * 1000 / HZ,
  1649. (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
  1650. ide_proc_register_driver(drive, tape->driver);
  1651. }
  1652. static void ide_tape_remove(ide_drive_t *drive)
  1653. {
  1654. idetape_tape_t *tape = drive->driver_data;
  1655. ide_proc_unregister_driver(drive, tape->driver);
  1656. device_del(&tape->dev);
  1657. ide_unregister_region(tape->disk);
  1658. mutex_lock(&idetape_ref_mutex);
  1659. put_device(&tape->dev);
  1660. mutex_unlock(&idetape_ref_mutex);
  1661. }
  1662. static void ide_tape_release(struct device *dev)
  1663. {
  1664. struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
  1665. ide_drive_t *drive = tape->drive;
  1666. struct gendisk *g = tape->disk;
  1667. BUG_ON(tape->valid);
  1668. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1669. drive->driver_data = NULL;
  1670. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  1671. device_destroy(idetape_sysfs_class,
  1672. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  1673. idetape_devs[tape->minor] = NULL;
  1674. g->private_data = NULL;
  1675. put_disk(g);
  1676. kfree(tape);
  1677. }
  1678. #ifdef CONFIG_IDE_PROC_FS
  1679. static int proc_idetape_read_name
  1680. (char *page, char **start, off_t off, int count, int *eof, void *data)
  1681. {
  1682. ide_drive_t *drive = (ide_drive_t *) data;
  1683. idetape_tape_t *tape = drive->driver_data;
  1684. char *out = page;
  1685. int len;
  1686. len = sprintf(out, "%s\n", tape->name);
  1687. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  1688. }
  1689. static ide_proc_entry_t idetape_proc[] = {
  1690. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  1691. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  1692. { NULL, 0, NULL, NULL }
  1693. };
  1694. static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
  1695. {
  1696. return idetape_proc;
  1697. }
  1698. static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
  1699. {
  1700. return idetape_settings;
  1701. }
  1702. #endif
  1703. static int ide_tape_probe(ide_drive_t *);
  1704. static struct ide_driver idetape_driver = {
  1705. .gen_driver = {
  1706. .owner = THIS_MODULE,
  1707. .name = "ide-tape",
  1708. .bus = &ide_bus_type,
  1709. },
  1710. .probe = ide_tape_probe,
  1711. .remove = ide_tape_remove,
  1712. .version = IDETAPE_VERSION,
  1713. .do_request = idetape_do_request,
  1714. #ifdef CONFIG_IDE_PROC_FS
  1715. .proc_entries = ide_tape_proc_entries,
  1716. .proc_devsets = ide_tape_proc_devsets,
  1717. #endif
  1718. };
  1719. /* Our character device supporting functions, passed to register_chrdev. */
  1720. static const struct file_operations idetape_fops = {
  1721. .owner = THIS_MODULE,
  1722. .read = idetape_chrdev_read,
  1723. .write = idetape_chrdev_write,
  1724. .ioctl = idetape_chrdev_ioctl,
  1725. .open = idetape_chrdev_open,
  1726. .release = idetape_chrdev_release,
  1727. };
  1728. static int idetape_open(struct block_device *bdev, fmode_t mode)
  1729. {
  1730. struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
  1731. if (!tape)
  1732. return -ENXIO;
  1733. return 0;
  1734. }
  1735. static int idetape_release(struct gendisk *disk, fmode_t mode)
  1736. {
  1737. struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
  1738. ide_tape_put(tape);
  1739. return 0;
  1740. }
  1741. static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
  1742. unsigned int cmd, unsigned long arg)
  1743. {
  1744. struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
  1745. ide_drive_t *drive = tape->drive;
  1746. int err = generic_ide_ioctl(drive, bdev, cmd, arg);
  1747. if (err == -EINVAL)
  1748. err = idetape_blkdev_ioctl(drive, cmd, arg);
  1749. return err;
  1750. }
  1751. static struct block_device_operations idetape_block_ops = {
  1752. .owner = THIS_MODULE,
  1753. .open = idetape_open,
  1754. .release = idetape_release,
  1755. .locked_ioctl = idetape_ioctl,
  1756. };
  1757. static int ide_tape_probe(ide_drive_t *drive)
  1758. {
  1759. idetape_tape_t *tape;
  1760. struct gendisk *g;
  1761. int minor;
  1762. if (!strstr("ide-tape", drive->driver_req))
  1763. goto failed;
  1764. if (drive->media != ide_tape)
  1765. goto failed;
  1766. if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
  1767. ide_check_atapi_device(drive, DRV_NAME) == 0) {
  1768. printk(KERN_ERR "ide-tape: %s: not supported by this version of"
  1769. " the driver\n", drive->name);
  1770. goto failed;
  1771. }
  1772. tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
  1773. if (tape == NULL) {
  1774. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
  1775. drive->name);
  1776. goto failed;
  1777. }
  1778. g = alloc_disk(1 << PARTN_BITS);
  1779. if (!g)
  1780. goto out_free_tape;
  1781. ide_init_disk(g, drive);
  1782. tape->dev.parent = &drive->gendev;
  1783. tape->dev.release = ide_tape_release;
  1784. dev_set_name(&tape->dev, dev_name(&drive->gendev));
  1785. if (device_register(&tape->dev))
  1786. goto out_free_disk;
  1787. tape->drive = drive;
  1788. tape->driver = &idetape_driver;
  1789. tape->disk = g;
  1790. g->private_data = &tape->driver;
  1791. drive->driver_data = tape;
  1792. mutex_lock(&idetape_ref_mutex);
  1793. for (minor = 0; idetape_devs[minor]; minor++)
  1794. ;
  1795. idetape_devs[minor] = tape;
  1796. mutex_unlock(&idetape_ref_mutex);
  1797. idetape_setup(drive, tape, minor);
  1798. device_create(idetape_sysfs_class, &drive->gendev,
  1799. MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
  1800. device_create(idetape_sysfs_class, &drive->gendev,
  1801. MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
  1802. "n%s", tape->name);
  1803. g->fops = &idetape_block_ops;
  1804. ide_register_region(g);
  1805. return 0;
  1806. out_free_disk:
  1807. put_disk(g);
  1808. out_free_tape:
  1809. kfree(tape);
  1810. failed:
  1811. return -ENODEV;
  1812. }
  1813. static void __exit idetape_exit(void)
  1814. {
  1815. driver_unregister(&idetape_driver.gen_driver);
  1816. class_destroy(idetape_sysfs_class);
  1817. unregister_chrdev(IDETAPE_MAJOR, "ht");
  1818. }
  1819. static int __init idetape_init(void)
  1820. {
  1821. int error = 1;
  1822. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  1823. if (IS_ERR(idetape_sysfs_class)) {
  1824. idetape_sysfs_class = NULL;
  1825. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  1826. error = -EBUSY;
  1827. goto out;
  1828. }
  1829. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  1830. printk(KERN_ERR "ide-tape: Failed to register chrdev"
  1831. " interface\n");
  1832. error = -EBUSY;
  1833. goto out_free_class;
  1834. }
  1835. error = driver_register(&idetape_driver.gen_driver);
  1836. if (error)
  1837. goto out_free_driver;
  1838. return 0;
  1839. out_free_driver:
  1840. driver_unregister(&idetape_driver.gen_driver);
  1841. out_free_class:
  1842. class_destroy(idetape_sysfs_class);
  1843. out:
  1844. return error;
  1845. }
  1846. MODULE_ALIAS("ide:*m-tape*");
  1847. module_init(idetape_init);
  1848. module_exit(idetape_exit);
  1849. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
  1850. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  1851. MODULE_LICENSE("GPL");