ide-tape.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  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->buf = NULL;
  514. pc->buf_size = length * tape->blk_size;
  515. pc->req_xfer = pc->buf_size;
  516. if (pc->req_xfer == tape->buffer_size)
  517. pc->flags |= PC_FLAG_DMA_OK;
  518. if (opcode == READ_6)
  519. pc->c[0] = READ_6;
  520. else if (opcode == WRITE_6) {
  521. pc->c[0] = WRITE_6;
  522. pc->flags |= PC_FLAG_WRITING;
  523. }
  524. memcpy(rq->cmd, pc->c, 12);
  525. }
  526. static ide_startstop_t idetape_do_request(ide_drive_t *drive,
  527. struct request *rq, sector_t block)
  528. {
  529. ide_hwif_t *hwif = drive->hwif;
  530. idetape_tape_t *tape = drive->driver_data;
  531. struct ide_atapi_pc *pc = NULL;
  532. struct request *postponed_rq = tape->postponed_rq;
  533. struct ide_cmd cmd;
  534. u8 stat;
  535. debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu\n"
  536. (unsigned long long)rq->sector, rq->nr_sectors);
  537. if (!(blk_special_request(rq) || blk_sense_request(rq))) {
  538. /* We do not support buffer cache originated requests. */
  539. printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
  540. "request queue (%d)\n", drive->name, rq->cmd_type);
  541. if (blk_fs_request(rq) == 0 && rq->errors == 0)
  542. rq->errors = -EIO;
  543. ide_complete_rq(drive, -EIO, ide_rq_bytes(rq));
  544. return ide_stopped;
  545. }
  546. /* Retry a failed packet command */
  547. if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
  548. pc = drive->failed_pc;
  549. goto out;
  550. }
  551. if (postponed_rq != NULL)
  552. if (rq != postponed_rq) {
  553. printk(KERN_ERR "ide-tape: ide-tape.c bug - "
  554. "Two DSC requests were queued\n");
  555. drive->failed_pc = NULL;
  556. rq->errors = 0;
  557. ide_complete_rq(drive, 0, blk_rq_bytes(rq));
  558. return ide_stopped;
  559. }
  560. tape->postponed_rq = NULL;
  561. /*
  562. * If the tape is still busy, postpone our request and service
  563. * the other device meanwhile.
  564. */
  565. stat = hwif->tp_ops->read_status(hwif);
  566. if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
  567. (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
  568. set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
  569. if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
  570. set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
  571. drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
  572. }
  573. if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
  574. (stat & ATA_DSC) == 0) {
  575. if (postponed_rq == NULL) {
  576. tape->dsc_polling_start = jiffies;
  577. tape->dsc_poll_freq = tape->best_dsc_rw_freq;
  578. tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
  579. } else if (time_after(jiffies, tape->dsc_timeout)) {
  580. printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
  581. tape->name);
  582. if (rq->cmd[13] & REQ_IDETAPE_PC2) {
  583. idetape_media_access_finished(drive);
  584. return ide_stopped;
  585. } else {
  586. return ide_do_reset(drive);
  587. }
  588. } else if (time_after(jiffies,
  589. tape->dsc_polling_start +
  590. IDETAPE_DSC_MA_THRESHOLD))
  591. tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
  592. idetape_postpone_request(drive);
  593. return ide_stopped;
  594. }
  595. if (rq->cmd[13] & REQ_IDETAPE_READ) {
  596. pc = &tape->queued_pc;
  597. ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
  598. goto out;
  599. }
  600. if (rq->cmd[13] & REQ_IDETAPE_WRITE) {
  601. pc = &tape->queued_pc;
  602. ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
  603. goto out;
  604. }
  605. if (rq->cmd[13] & REQ_IDETAPE_PC1) {
  606. pc = (struct ide_atapi_pc *)rq->special;
  607. rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
  608. rq->cmd[13] |= REQ_IDETAPE_PC2;
  609. goto out;
  610. }
  611. if (rq->cmd[13] & REQ_IDETAPE_PC2) {
  612. idetape_media_access_finished(drive);
  613. return ide_stopped;
  614. }
  615. BUG();
  616. out:
  617. /* prepare sense request for this command */
  618. ide_prep_sense(drive, rq);
  619. memset(&cmd, 0, sizeof(cmd));
  620. if (rq_data_dir(rq))
  621. cmd.tf_flags |= IDE_TFLAG_WRITE;
  622. cmd.rq = rq;
  623. ide_init_sg_cmd(&cmd, pc->req_xfer);
  624. ide_map_sg(drive, &cmd);
  625. return ide_tape_issue_pc(drive, &cmd, pc);
  626. }
  627. /*
  628. * Write a filemark if write_filemark=1. Flush the device buffers without
  629. * writing a filemark otherwise.
  630. */
  631. static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
  632. struct ide_atapi_pc *pc, int write_filemark)
  633. {
  634. ide_init_pc(pc);
  635. pc->c[0] = WRITE_FILEMARKS;
  636. pc->c[4] = write_filemark;
  637. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  638. }
  639. static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
  640. {
  641. idetape_tape_t *tape = drive->driver_data;
  642. struct gendisk *disk = tape->disk;
  643. int load_attempted = 0;
  644. /* Wait for the tape to become ready */
  645. set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
  646. timeout += jiffies;
  647. while (time_before(jiffies, timeout)) {
  648. if (ide_do_test_unit_ready(drive, disk) == 0)
  649. return 0;
  650. if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
  651. || (tape->asc == 0x3A)) {
  652. /* no media */
  653. if (load_attempted)
  654. return -ENOMEDIUM;
  655. ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
  656. load_attempted = 1;
  657. /* not about to be ready */
  658. } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
  659. (tape->ascq == 1 || tape->ascq == 8)))
  660. return -EIO;
  661. msleep(100);
  662. }
  663. return -EIO;
  664. }
  665. static int idetape_flush_tape_buffers(ide_drive_t *drive)
  666. {
  667. struct ide_tape_obj *tape = drive->driver_data;
  668. struct ide_atapi_pc pc;
  669. int rc;
  670. idetape_create_write_filemark_cmd(drive, &pc, 0);
  671. rc = ide_queue_pc_tail(drive, tape->disk, &pc);
  672. if (rc)
  673. return rc;
  674. idetape_wait_ready(drive, 60 * 5 * HZ);
  675. return 0;
  676. }
  677. static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
  678. {
  679. ide_init_pc(pc);
  680. pc->c[0] = READ_POSITION;
  681. pc->req_xfer = 20;
  682. }
  683. static int idetape_read_position(ide_drive_t *drive)
  684. {
  685. idetape_tape_t *tape = drive->driver_data;
  686. struct ide_atapi_pc pc;
  687. int position;
  688. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  689. idetape_create_read_position_cmd(&pc);
  690. if (ide_queue_pc_tail(drive, tape->disk, &pc))
  691. return -1;
  692. position = tape->first_frame;
  693. return position;
  694. }
  695. static void idetape_create_locate_cmd(ide_drive_t *drive,
  696. struct ide_atapi_pc *pc,
  697. unsigned int block, u8 partition, int skip)
  698. {
  699. ide_init_pc(pc);
  700. pc->c[0] = POSITION_TO_ELEMENT;
  701. pc->c[1] = 2;
  702. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
  703. pc->c[8] = partition;
  704. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  705. }
  706. static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
  707. {
  708. idetape_tape_t *tape = drive->driver_data;
  709. if (tape->chrdev_dir != IDETAPE_DIR_READ)
  710. return;
  711. clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
  712. tape->valid = 0;
  713. if (tape->buf != NULL) {
  714. kfree(tape->buf);
  715. tape->buf = NULL;
  716. }
  717. tape->chrdev_dir = IDETAPE_DIR_NONE;
  718. }
  719. /*
  720. * Position the tape to the requested block using the LOCATE packet command.
  721. * A READ POSITION command is then issued to check where we are positioned. Like
  722. * all higher level operations, we queue the commands at the tail of the request
  723. * queue and wait for their completion.
  724. */
  725. static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
  726. u8 partition, int skip)
  727. {
  728. idetape_tape_t *tape = drive->driver_data;
  729. struct gendisk *disk = tape->disk;
  730. int retval;
  731. struct ide_atapi_pc pc;
  732. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  733. __ide_tape_discard_merge_buffer(drive);
  734. idetape_wait_ready(drive, 60 * 5 * HZ);
  735. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  736. retval = ide_queue_pc_tail(drive, disk, &pc);
  737. if (retval)
  738. return (retval);
  739. idetape_create_read_position_cmd(&pc);
  740. return ide_queue_pc_tail(drive, disk, &pc);
  741. }
  742. static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
  743. int restore_position)
  744. {
  745. idetape_tape_t *tape = drive->driver_data;
  746. int seek, position;
  747. __ide_tape_discard_merge_buffer(drive);
  748. if (restore_position) {
  749. position = idetape_read_position(drive);
  750. seek = position > 0 ? position : 0;
  751. if (idetape_position_tape(drive, seek, 0, 0)) {
  752. printk(KERN_INFO "ide-tape: %s: position_tape failed in"
  753. " %s\n", tape->name, __func__);
  754. return;
  755. }
  756. }
  757. }
  758. /*
  759. * Generate a read/write request for the block device interface and wait for it
  760. * to be serviced.
  761. */
  762. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
  763. {
  764. idetape_tape_t *tape = drive->driver_data;
  765. struct request *rq;
  766. int ret;
  767. debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
  768. BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
  769. BUG_ON(size < 0 || size % tape->blk_size);
  770. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  771. rq->cmd_type = REQ_TYPE_SPECIAL;
  772. rq->cmd[13] = cmd;
  773. rq->rq_disk = tape->disk;
  774. rq->sector = tape->first_frame;
  775. if (size) {
  776. ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
  777. __GFP_WAIT);
  778. if (ret)
  779. goto out_put;
  780. }
  781. blk_execute_rq(drive->queue, tape->disk, rq, 0);
  782. /* calculate the number of transferred bytes and update buffer state */
  783. size -= rq->data_len;
  784. tape->cur = tape->buf;
  785. if (cmd == REQ_IDETAPE_READ)
  786. tape->valid = size;
  787. else
  788. tape->valid = 0;
  789. ret = size;
  790. if (rq->errors == IDE_DRV_ERROR_GENERAL)
  791. ret = -EIO;
  792. out_put:
  793. blk_put_request(rq);
  794. return ret;
  795. }
  796. static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
  797. {
  798. ide_init_pc(pc);
  799. pc->c[0] = INQUIRY;
  800. pc->c[4] = 254;
  801. pc->req_xfer = 254;
  802. }
  803. static void idetape_create_rewind_cmd(ide_drive_t *drive,
  804. struct ide_atapi_pc *pc)
  805. {
  806. ide_init_pc(pc);
  807. pc->c[0] = REZERO_UNIT;
  808. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  809. }
  810. static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
  811. {
  812. ide_init_pc(pc);
  813. pc->c[0] = ERASE;
  814. pc->c[1] = 1;
  815. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  816. }
  817. static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
  818. {
  819. ide_init_pc(pc);
  820. pc->c[0] = SPACE;
  821. put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
  822. pc->c[1] = cmd;
  823. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  824. }
  825. static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
  826. {
  827. idetape_tape_t *tape = drive->driver_data;
  828. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  829. printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
  830. " but we are not writing.\n");
  831. return;
  832. }
  833. if (tape->buf) {
  834. size_t aligned = roundup(tape->valid, tape->blk_size);
  835. memset(tape->cur, 0, aligned - tape->valid);
  836. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
  837. kfree(tape->buf);
  838. tape->buf = NULL;
  839. }
  840. tape->chrdev_dir = IDETAPE_DIR_NONE;
  841. }
  842. static int idetape_init_rw(ide_drive_t *drive, int dir)
  843. {
  844. idetape_tape_t *tape = drive->driver_data;
  845. int rc;
  846. BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
  847. if (tape->chrdev_dir == dir)
  848. return 0;
  849. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  850. ide_tape_discard_merge_buffer(drive, 1);
  851. else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  852. ide_tape_flush_merge_buffer(drive);
  853. idetape_flush_tape_buffers(drive);
  854. }
  855. if (tape->buf || tape->valid) {
  856. printk(KERN_ERR "ide-tape: valid should be 0 now\n");
  857. tape->valid = 0;
  858. }
  859. tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
  860. if (!tape->buf)
  861. return -ENOMEM;
  862. tape->chrdev_dir = dir;
  863. tape->cur = tape->buf;
  864. /*
  865. * Issue a 0 rw command to ensure that DSC handshake is
  866. * switched from completion mode to buffer available mode. No
  867. * point in issuing this if DSC overlap isn't supported, some
  868. * drives (Seagate STT3401A) will return an error.
  869. */
  870. if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
  871. int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
  872. : REQ_IDETAPE_WRITE;
  873. rc = idetape_queue_rw_tail(drive, cmd, 0);
  874. if (rc < 0) {
  875. kfree(tape->buf);
  876. tape->buf = NULL;
  877. tape->chrdev_dir = IDETAPE_DIR_NONE;
  878. return rc;
  879. }
  880. }
  881. return 0;
  882. }
  883. static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
  884. {
  885. idetape_tape_t *tape = drive->driver_data;
  886. memset(tape->buf, 0, tape->buffer_size);
  887. while (bcount) {
  888. unsigned int count = min(tape->buffer_size, bcount);
  889. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
  890. bcount -= count;
  891. }
  892. }
  893. /*
  894. * Rewinds the tape to the Beginning Of the current Partition (BOP). We
  895. * currently support only one partition.
  896. */
  897. static int idetape_rewind_tape(ide_drive_t *drive)
  898. {
  899. struct ide_tape_obj *tape = drive->driver_data;
  900. struct gendisk *disk = tape->disk;
  901. int retval;
  902. struct ide_atapi_pc pc;
  903. debug_log(DBG_SENSE, "Enter %s\n", __func__);
  904. idetape_create_rewind_cmd(drive, &pc);
  905. retval = ide_queue_pc_tail(drive, disk, &pc);
  906. if (retval)
  907. return retval;
  908. idetape_create_read_position_cmd(&pc);
  909. retval = ide_queue_pc_tail(drive, disk, &pc);
  910. if (retval)
  911. return retval;
  912. return 0;
  913. }
  914. /* mtio.h compatible commands should be issued to the chrdev interface. */
  915. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
  916. unsigned long arg)
  917. {
  918. idetape_tape_t *tape = drive->driver_data;
  919. void __user *argp = (void __user *)arg;
  920. struct idetape_config {
  921. int dsc_rw_frequency;
  922. int dsc_media_access_frequency;
  923. int nr_stages;
  924. } config;
  925. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  926. switch (cmd) {
  927. case 0x0340:
  928. if (copy_from_user(&config, argp, sizeof(config)))
  929. return -EFAULT;
  930. tape->best_dsc_rw_freq = config.dsc_rw_frequency;
  931. break;
  932. case 0x0350:
  933. config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
  934. config.nr_stages = 1;
  935. if (copy_to_user(argp, &config, sizeof(config)))
  936. return -EFAULT;
  937. break;
  938. default:
  939. return -EIO;
  940. }
  941. return 0;
  942. }
  943. static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
  944. int mt_count)
  945. {
  946. idetape_tape_t *tape = drive->driver_data;
  947. struct gendisk *disk = tape->disk;
  948. struct ide_atapi_pc pc;
  949. int retval, count = 0;
  950. int sprev = !!(tape->caps[4] & 0x20);
  951. if (mt_count == 0)
  952. return 0;
  953. if (MTBSF == mt_op || MTBSFM == mt_op) {
  954. if (!sprev)
  955. return -EIO;
  956. mt_count = -mt_count;
  957. }
  958. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  959. tape->valid = 0;
  960. if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
  961. ++count;
  962. ide_tape_discard_merge_buffer(drive, 0);
  963. }
  964. switch (mt_op) {
  965. case MTFSF:
  966. case MTBSF:
  967. idetape_create_space_cmd(&pc, mt_count - count,
  968. IDETAPE_SPACE_OVER_FILEMARK);
  969. return ide_queue_pc_tail(drive, disk, &pc);
  970. case MTFSFM:
  971. case MTBSFM:
  972. if (!sprev)
  973. return -EIO;
  974. retval = idetape_space_over_filemarks(drive, MTFSF,
  975. mt_count - count);
  976. if (retval)
  977. return retval;
  978. count = (MTBSFM == mt_op ? 1 : -1);
  979. return idetape_space_over_filemarks(drive, MTFSF, count);
  980. default:
  981. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  982. mt_op);
  983. return -EIO;
  984. }
  985. }
  986. /*
  987. * Our character device read / write functions.
  988. *
  989. * The tape is optimized to maximize throughput when it is transferring an
  990. * integral number of the "continuous transfer limit", which is a parameter of
  991. * the specific tape (26kB on my particular tape, 32kB for Onstream).
  992. *
  993. * As of version 1.3 of the driver, the character device provides an abstract
  994. * continuous view of the media - any mix of block sizes (even 1 byte) on the
  995. * same backup/restore procedure is supported. The driver will internally
  996. * convert the requests to the recommended transfer unit, so that an unmatch
  997. * between the user's block size to the recommended size will only result in a
  998. * (slightly) increased driver overhead, but will no longer hit performance.
  999. * This is not applicable to Onstream.
  1000. */
  1001. static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
  1002. size_t count, loff_t *ppos)
  1003. {
  1004. struct ide_tape_obj *tape = file->private_data;
  1005. ide_drive_t *drive = tape->drive;
  1006. size_t done = 0;
  1007. ssize_t ret = 0;
  1008. int rc;
  1009. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1010. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1011. if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
  1012. if (count > tape->blk_size &&
  1013. (count % tape->blk_size) == 0)
  1014. tape->user_bs_factor = count / tape->blk_size;
  1015. }
  1016. rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
  1017. if (rc < 0)
  1018. return rc;
  1019. while (done < count) {
  1020. size_t todo;
  1021. /* refill if staging buffer is empty */
  1022. if (!tape->valid) {
  1023. /* If we are at a filemark, nothing more to read */
  1024. if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
  1025. break;
  1026. /* read */
  1027. if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
  1028. tape->buffer_size) <= 0)
  1029. break;
  1030. }
  1031. /* copy out */
  1032. todo = min_t(size_t, count - done, tape->valid);
  1033. if (copy_to_user(buf + done, tape->cur, todo))
  1034. ret = -EFAULT;
  1035. tape->cur += todo;
  1036. tape->valid -= todo;
  1037. done += todo;
  1038. }
  1039. if (!done && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
  1040. debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
  1041. idetape_space_over_filemarks(drive, MTFSF, 1);
  1042. return 0;
  1043. }
  1044. return ret ? ret : done;
  1045. }
  1046. static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
  1047. size_t count, loff_t *ppos)
  1048. {
  1049. struct ide_tape_obj *tape = file->private_data;
  1050. ide_drive_t *drive = tape->drive;
  1051. size_t done = 0;
  1052. ssize_t ret = 0;
  1053. int rc;
  1054. /* The drive is write protected. */
  1055. if (tape->write_prot)
  1056. return -EACCES;
  1057. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1058. /* Initialize write operation */
  1059. rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
  1060. if (rc < 0)
  1061. return rc;
  1062. while (done < count) {
  1063. size_t todo;
  1064. /* flush if staging buffer is full */
  1065. if (tape->valid == tape->buffer_size &&
  1066. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
  1067. tape->buffer_size) <= 0)
  1068. return rc;
  1069. /* copy in */
  1070. todo = min_t(size_t, count - done,
  1071. tape->buffer_size - tape->valid);
  1072. if (copy_from_user(tape->cur, buf + done, todo))
  1073. ret = -EFAULT;
  1074. tape->cur += todo;
  1075. tape->valid += todo;
  1076. done += todo;
  1077. }
  1078. return ret ? ret : done;
  1079. }
  1080. static int idetape_write_filemark(ide_drive_t *drive)
  1081. {
  1082. struct ide_tape_obj *tape = drive->driver_data;
  1083. struct ide_atapi_pc pc;
  1084. /* Write a filemark */
  1085. idetape_create_write_filemark_cmd(drive, &pc, 1);
  1086. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1087. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  1088. return -EIO;
  1089. }
  1090. return 0;
  1091. }
  1092. /*
  1093. * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
  1094. * requested.
  1095. *
  1096. * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
  1097. * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
  1098. * usually not supported.
  1099. *
  1100. * The following commands are currently not supported:
  1101. *
  1102. * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
  1103. * MT_ST_WRITE_THRESHOLD.
  1104. */
  1105. static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
  1106. {
  1107. idetape_tape_t *tape = drive->driver_data;
  1108. struct gendisk *disk = tape->disk;
  1109. struct ide_atapi_pc pc;
  1110. int i, retval;
  1111. debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
  1112. mt_op, mt_count);
  1113. switch (mt_op) {
  1114. case MTFSF:
  1115. case MTFSFM:
  1116. case MTBSF:
  1117. case MTBSFM:
  1118. if (!mt_count)
  1119. return 0;
  1120. return idetape_space_over_filemarks(drive, mt_op, mt_count);
  1121. default:
  1122. break;
  1123. }
  1124. switch (mt_op) {
  1125. case MTWEOF:
  1126. if (tape->write_prot)
  1127. return -EACCES;
  1128. ide_tape_discard_merge_buffer(drive, 1);
  1129. for (i = 0; i < mt_count; i++) {
  1130. retval = idetape_write_filemark(drive);
  1131. if (retval)
  1132. return retval;
  1133. }
  1134. return 0;
  1135. case MTREW:
  1136. ide_tape_discard_merge_buffer(drive, 0);
  1137. if (idetape_rewind_tape(drive))
  1138. return -EIO;
  1139. return 0;
  1140. case MTLOAD:
  1141. ide_tape_discard_merge_buffer(drive, 0);
  1142. return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
  1143. case MTUNLOAD:
  1144. case MTOFFL:
  1145. /*
  1146. * If door is locked, attempt to unlock before
  1147. * attempting to eject.
  1148. */
  1149. if (tape->door_locked) {
  1150. if (!ide_set_media_lock(drive, disk, 0))
  1151. tape->door_locked = DOOR_UNLOCKED;
  1152. }
  1153. ide_tape_discard_merge_buffer(drive, 0);
  1154. retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
  1155. if (!retval)
  1156. clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
  1157. return retval;
  1158. case MTNOP:
  1159. ide_tape_discard_merge_buffer(drive, 0);
  1160. return idetape_flush_tape_buffers(drive);
  1161. case MTRETEN:
  1162. ide_tape_discard_merge_buffer(drive, 0);
  1163. return ide_do_start_stop(drive, disk,
  1164. IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  1165. case MTEOM:
  1166. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  1167. return ide_queue_pc_tail(drive, disk, &pc);
  1168. case MTERASE:
  1169. (void)idetape_rewind_tape(drive);
  1170. idetape_create_erase_cmd(&pc);
  1171. return ide_queue_pc_tail(drive, disk, &pc);
  1172. case MTSETBLK:
  1173. if (mt_count) {
  1174. if (mt_count < tape->blk_size ||
  1175. mt_count % tape->blk_size)
  1176. return -EIO;
  1177. tape->user_bs_factor = mt_count / tape->blk_size;
  1178. clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
  1179. } else
  1180. set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
  1181. return 0;
  1182. case MTSEEK:
  1183. ide_tape_discard_merge_buffer(drive, 0);
  1184. return idetape_position_tape(drive,
  1185. mt_count * tape->user_bs_factor, tape->partition, 0);
  1186. case MTSETPART:
  1187. ide_tape_discard_merge_buffer(drive, 0);
  1188. return idetape_position_tape(drive, 0, mt_count, 0);
  1189. case MTFSR:
  1190. case MTBSR:
  1191. case MTLOCK:
  1192. retval = ide_set_media_lock(drive, disk, 1);
  1193. if (retval)
  1194. return retval;
  1195. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  1196. return 0;
  1197. case MTUNLOCK:
  1198. retval = ide_set_media_lock(drive, disk, 0);
  1199. if (retval)
  1200. return retval;
  1201. tape->door_locked = DOOR_UNLOCKED;
  1202. return 0;
  1203. default:
  1204. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1205. mt_op);
  1206. return -EIO;
  1207. }
  1208. }
  1209. /*
  1210. * Our character device ioctls. General mtio.h magnetic io commands are
  1211. * supported here, and not in the corresponding block interface. Our own
  1212. * ide-tape ioctls are supported on both interfaces.
  1213. */
  1214. static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
  1215. unsigned int cmd, unsigned long arg)
  1216. {
  1217. struct ide_tape_obj *tape = file->private_data;
  1218. ide_drive_t *drive = tape->drive;
  1219. struct mtop mtop;
  1220. struct mtget mtget;
  1221. struct mtpos mtpos;
  1222. int block_offset = 0, position = tape->first_frame;
  1223. void __user *argp = (void __user *)arg;
  1224. debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
  1225. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1226. ide_tape_flush_merge_buffer(drive);
  1227. idetape_flush_tape_buffers(drive);
  1228. }
  1229. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  1230. block_offset = tape->valid /
  1231. (tape->blk_size * tape->user_bs_factor);
  1232. position = idetape_read_position(drive);
  1233. if (position < 0)
  1234. return -EIO;
  1235. }
  1236. switch (cmd) {
  1237. case MTIOCTOP:
  1238. if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
  1239. return -EFAULT;
  1240. return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
  1241. case MTIOCGET:
  1242. memset(&mtget, 0, sizeof(struct mtget));
  1243. mtget.mt_type = MT_ISSCSI2;
  1244. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  1245. mtget.mt_dsreg =
  1246. ((tape->blk_size * tape->user_bs_factor)
  1247. << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  1248. if (tape->drv_write_prot)
  1249. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  1250. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  1251. return -EFAULT;
  1252. return 0;
  1253. case MTIOCPOS:
  1254. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  1255. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  1256. return -EFAULT;
  1257. return 0;
  1258. default:
  1259. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1260. ide_tape_discard_merge_buffer(drive, 1);
  1261. return idetape_blkdev_ioctl(drive, cmd, arg);
  1262. }
  1263. }
  1264. /*
  1265. * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
  1266. * block size with the reported value.
  1267. */
  1268. static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
  1269. {
  1270. idetape_tape_t *tape = drive->driver_data;
  1271. struct ide_atapi_pc pc;
  1272. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  1273. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1274. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  1275. if (tape->blk_size == 0) {
  1276. printk(KERN_WARNING "ide-tape: Cannot deal with zero "
  1277. "block size, assuming 32k\n");
  1278. tape->blk_size = 32768;
  1279. }
  1280. return;
  1281. }
  1282. tape->blk_size = (pc.buf[4 + 5] << 16) +
  1283. (pc.buf[4 + 6] << 8) +
  1284. pc.buf[4 + 7];
  1285. tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
  1286. }
  1287. static int idetape_chrdev_open(struct inode *inode, struct file *filp)
  1288. {
  1289. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  1290. ide_drive_t *drive;
  1291. idetape_tape_t *tape;
  1292. int retval;
  1293. if (i >= MAX_HWIFS * MAX_DRIVES)
  1294. return -ENXIO;
  1295. lock_kernel();
  1296. tape = ide_tape_chrdev_get(i);
  1297. if (!tape) {
  1298. unlock_kernel();
  1299. return -ENXIO;
  1300. }
  1301. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1302. /*
  1303. * We really want to do nonseekable_open(inode, filp); here, but some
  1304. * versions of tar incorrectly call lseek on tapes and bail out if that
  1305. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  1306. */
  1307. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  1308. drive = tape->drive;
  1309. filp->private_data = tape;
  1310. if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
  1311. retval = -EBUSY;
  1312. goto out_put_tape;
  1313. }
  1314. retval = idetape_wait_ready(drive, 60 * HZ);
  1315. if (retval) {
  1316. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1317. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  1318. goto out_put_tape;
  1319. }
  1320. idetape_read_position(drive);
  1321. if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
  1322. (void)idetape_rewind_tape(drive);
  1323. /* Read block size and write protect status from drive. */
  1324. ide_tape_get_bsize_from_bdesc(drive);
  1325. /* Set write protect flag if device is opened as read-only. */
  1326. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  1327. tape->write_prot = 1;
  1328. else
  1329. tape->write_prot = tape->drv_write_prot;
  1330. /* Make sure drive isn't write protected if user wants to write. */
  1331. if (tape->write_prot) {
  1332. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  1333. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  1334. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1335. retval = -EROFS;
  1336. goto out_put_tape;
  1337. }
  1338. }
  1339. /* Lock the tape drive door so user can't eject. */
  1340. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1341. if (!ide_set_media_lock(drive, tape->disk, 1)) {
  1342. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  1343. tape->door_locked = DOOR_LOCKED;
  1344. }
  1345. }
  1346. unlock_kernel();
  1347. return 0;
  1348. out_put_tape:
  1349. ide_tape_put(tape);
  1350. unlock_kernel();
  1351. return retval;
  1352. }
  1353. static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
  1354. {
  1355. idetape_tape_t *tape = drive->driver_data;
  1356. ide_tape_flush_merge_buffer(drive);
  1357. tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
  1358. if (tape->buf != NULL) {
  1359. idetape_pad_zeros(drive, tape->blk_size *
  1360. (tape->user_bs_factor - 1));
  1361. kfree(tape->buf);
  1362. tape->buf = NULL;
  1363. }
  1364. idetape_write_filemark(drive);
  1365. idetape_flush_tape_buffers(drive);
  1366. idetape_flush_tape_buffers(drive);
  1367. }
  1368. static int idetape_chrdev_release(struct inode *inode, struct file *filp)
  1369. {
  1370. struct ide_tape_obj *tape = filp->private_data;
  1371. ide_drive_t *drive = tape->drive;
  1372. unsigned int minor = iminor(inode);
  1373. lock_kernel();
  1374. tape = drive->driver_data;
  1375. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1376. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  1377. idetape_write_release(drive, minor);
  1378. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1379. if (minor < 128)
  1380. ide_tape_discard_merge_buffer(drive, 1);
  1381. }
  1382. if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
  1383. (void) idetape_rewind_tape(drive);
  1384. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1385. if (tape->door_locked == DOOR_LOCKED) {
  1386. if (!ide_set_media_lock(drive, tape->disk, 0))
  1387. tape->door_locked = DOOR_UNLOCKED;
  1388. }
  1389. }
  1390. clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
  1391. ide_tape_put(tape);
  1392. unlock_kernel();
  1393. return 0;
  1394. }
  1395. static void idetape_get_inquiry_results(ide_drive_t *drive)
  1396. {
  1397. idetape_tape_t *tape = drive->driver_data;
  1398. struct ide_atapi_pc pc;
  1399. u8 pc_buf[256];
  1400. char fw_rev[4], vendor_id[8], product_id[16];
  1401. idetape_create_inquiry_cmd(&pc);
  1402. pc.buf = &pc_buf[0];
  1403. pc.buf_size = sizeof(pc_buf);
  1404. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1405. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
  1406. tape->name);
  1407. return;
  1408. }
  1409. memcpy(vendor_id, &pc.buf[8], 8);
  1410. memcpy(product_id, &pc.buf[16], 16);
  1411. memcpy(fw_rev, &pc.buf[32], 4);
  1412. ide_fixstring(vendor_id, 8, 0);
  1413. ide_fixstring(product_id, 16, 0);
  1414. ide_fixstring(fw_rev, 4, 0);
  1415. printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
  1416. drive->name, tape->name, vendor_id, product_id, fw_rev);
  1417. }
  1418. /*
  1419. * Ask the tape about its various parameters. In particular, we will adjust our
  1420. * data transfer buffer size to the recommended value as returned by the tape.
  1421. */
  1422. static void idetape_get_mode_sense_results(ide_drive_t *drive)
  1423. {
  1424. idetape_tape_t *tape = drive->driver_data;
  1425. struct ide_atapi_pc pc;
  1426. u8 *caps;
  1427. u8 speed, max_speed;
  1428. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  1429. if (ide_queue_pc_tail(drive, tape->disk, &pc)) {
  1430. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
  1431. " some default values\n");
  1432. tape->blk_size = 512;
  1433. put_unaligned(52, (u16 *)&tape->caps[12]);
  1434. put_unaligned(540, (u16 *)&tape->caps[14]);
  1435. put_unaligned(6*52, (u16 *)&tape->caps[16]);
  1436. return;
  1437. }
  1438. caps = pc.buf + 4 + pc.buf[3];
  1439. /* convert to host order and save for later use */
  1440. speed = be16_to_cpup((__be16 *)&caps[14]);
  1441. max_speed = be16_to_cpup((__be16 *)&caps[8]);
  1442. *(u16 *)&caps[8] = max_speed;
  1443. *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
  1444. *(u16 *)&caps[14] = speed;
  1445. *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
  1446. if (!speed) {
  1447. printk(KERN_INFO "ide-tape: %s: invalid tape speed "
  1448. "(assuming 650KB/sec)\n", drive->name);
  1449. *(u16 *)&caps[14] = 650;
  1450. }
  1451. if (!max_speed) {
  1452. printk(KERN_INFO "ide-tape: %s: invalid max_speed "
  1453. "(assuming 650KB/sec)\n", drive->name);
  1454. *(u16 *)&caps[8] = 650;
  1455. }
  1456. memcpy(&tape->caps, caps, 20);
  1457. /* device lacks locking support according to capabilities page */
  1458. if ((caps[6] & 1) == 0)
  1459. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  1460. if (caps[7] & 0x02)
  1461. tape->blk_size = 512;
  1462. else if (caps[7] & 0x04)
  1463. tape->blk_size = 1024;
  1464. }
  1465. #ifdef CONFIG_IDE_PROC_FS
  1466. #define ide_tape_devset_get(name, field) \
  1467. static int get_##name(ide_drive_t *drive) \
  1468. { \
  1469. idetape_tape_t *tape = drive->driver_data; \
  1470. return tape->field; \
  1471. }
  1472. #define ide_tape_devset_set(name, field) \
  1473. static int set_##name(ide_drive_t *drive, int arg) \
  1474. { \
  1475. idetape_tape_t *tape = drive->driver_data; \
  1476. tape->field = arg; \
  1477. return 0; \
  1478. }
  1479. #define ide_tape_devset_rw_field(_name, _field) \
  1480. ide_tape_devset_get(_name, _field) \
  1481. ide_tape_devset_set(_name, _field) \
  1482. IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
  1483. #define ide_tape_devset_r_field(_name, _field) \
  1484. ide_tape_devset_get(_name, _field) \
  1485. IDE_DEVSET(_name, 0, get_##_name, NULL)
  1486. static int mulf_tdsc(ide_drive_t *drive) { return 1000; }
  1487. static int divf_tdsc(ide_drive_t *drive) { return HZ; }
  1488. static int divf_buffer(ide_drive_t *drive) { return 2; }
  1489. static int divf_buffer_size(ide_drive_t *drive) { return 1024; }
  1490. ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
  1491. ide_tape_devset_rw_field(debug_mask, debug_mask);
  1492. ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
  1493. ide_tape_devset_r_field(avg_speed, avg_speed);
  1494. ide_tape_devset_r_field(speed, caps[14]);
  1495. ide_tape_devset_r_field(buffer, caps[16]);
  1496. ide_tape_devset_r_field(buffer_size, buffer_size);
  1497. static const struct ide_proc_devset idetape_settings[] = {
  1498. __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL),
  1499. __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer),
  1500. __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size),
  1501. __IDE_PROC_DEVSET(debug_mask, 0, 0xffff, NULL, NULL),
  1502. __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL),
  1503. __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL),
  1504. __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
  1505. mulf_tdsc, divf_tdsc),
  1506. { NULL },
  1507. };
  1508. #endif
  1509. /*
  1510. * The function below is called to:
  1511. *
  1512. * 1. Initialize our various state variables.
  1513. * 2. Ask the tape for its capabilities.
  1514. * 3. Allocate a buffer which will be used for data transfer. The buffer size
  1515. * is chosen based on the recommendation which we received in step 2.
  1516. *
  1517. * Note that at this point ide.c already assigned us an irq, so that we can
  1518. * queue requests here and wait for their completion.
  1519. */
  1520. static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
  1521. {
  1522. unsigned long t;
  1523. int speed;
  1524. int buffer_size;
  1525. u16 *ctl = (u16 *)&tape->caps[12];
  1526. drive->pc_callback = ide_tape_callback;
  1527. drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
  1528. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  1529. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  1530. tape->name);
  1531. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1532. }
  1533. /* Seagate Travan drives do not support DSC overlap. */
  1534. if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
  1535. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1536. tape->minor = minor;
  1537. tape->name[0] = 'h';
  1538. tape->name[1] = 't';
  1539. tape->name[2] = '0' + minor;
  1540. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1541. idetape_get_inquiry_results(drive);
  1542. idetape_get_mode_sense_results(drive);
  1543. ide_tape_get_bsize_from_bdesc(drive);
  1544. tape->user_bs_factor = 1;
  1545. tape->buffer_size = *ctl * tape->blk_size;
  1546. while (tape->buffer_size > 0xffff) {
  1547. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  1548. *ctl /= 2;
  1549. tape->buffer_size = *ctl * tape->blk_size;
  1550. }
  1551. buffer_size = tape->buffer_size;
  1552. /* select the "best" DSC read/write polling freq */
  1553. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  1554. t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
  1555. /*
  1556. * Ensure that the number we got makes sense; limit it within
  1557. * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  1558. */
  1559. tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
  1560. IDETAPE_DSC_RW_MAX);
  1561. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  1562. "%lums tDSC%s\n",
  1563. drive->name, tape->name, *(u16 *)&tape->caps[14],
  1564. (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
  1565. tape->buffer_size / 1024,
  1566. tape->best_dsc_rw_freq * 1000 / HZ,
  1567. (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
  1568. ide_proc_register_driver(drive, tape->driver);
  1569. }
  1570. static void ide_tape_remove(ide_drive_t *drive)
  1571. {
  1572. idetape_tape_t *tape = drive->driver_data;
  1573. ide_proc_unregister_driver(drive, tape->driver);
  1574. device_del(&tape->dev);
  1575. ide_unregister_region(tape->disk);
  1576. mutex_lock(&idetape_ref_mutex);
  1577. put_device(&tape->dev);
  1578. mutex_unlock(&idetape_ref_mutex);
  1579. }
  1580. static void ide_tape_release(struct device *dev)
  1581. {
  1582. struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
  1583. ide_drive_t *drive = tape->drive;
  1584. struct gendisk *g = tape->disk;
  1585. BUG_ON(tape->valid);
  1586. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  1587. drive->driver_data = NULL;
  1588. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  1589. device_destroy(idetape_sysfs_class,
  1590. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  1591. idetape_devs[tape->minor] = NULL;
  1592. g->private_data = NULL;
  1593. put_disk(g);
  1594. kfree(tape);
  1595. }
  1596. #ifdef CONFIG_IDE_PROC_FS
  1597. static int proc_idetape_read_name
  1598. (char *page, char **start, off_t off, int count, int *eof, void *data)
  1599. {
  1600. ide_drive_t *drive = (ide_drive_t *) data;
  1601. idetape_tape_t *tape = drive->driver_data;
  1602. char *out = page;
  1603. int len;
  1604. len = sprintf(out, "%s\n", tape->name);
  1605. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  1606. }
  1607. static ide_proc_entry_t idetape_proc[] = {
  1608. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  1609. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  1610. { NULL, 0, NULL, NULL }
  1611. };
  1612. static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
  1613. {
  1614. return idetape_proc;
  1615. }
  1616. static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
  1617. {
  1618. return idetape_settings;
  1619. }
  1620. #endif
  1621. static int ide_tape_probe(ide_drive_t *);
  1622. static struct ide_driver idetape_driver = {
  1623. .gen_driver = {
  1624. .owner = THIS_MODULE,
  1625. .name = "ide-tape",
  1626. .bus = &ide_bus_type,
  1627. },
  1628. .probe = ide_tape_probe,
  1629. .remove = ide_tape_remove,
  1630. .version = IDETAPE_VERSION,
  1631. .do_request = idetape_do_request,
  1632. #ifdef CONFIG_IDE_PROC_FS
  1633. .proc_entries = ide_tape_proc_entries,
  1634. .proc_devsets = ide_tape_proc_devsets,
  1635. #endif
  1636. };
  1637. /* Our character device supporting functions, passed to register_chrdev. */
  1638. static const struct file_operations idetape_fops = {
  1639. .owner = THIS_MODULE,
  1640. .read = idetape_chrdev_read,
  1641. .write = idetape_chrdev_write,
  1642. .ioctl = idetape_chrdev_ioctl,
  1643. .open = idetape_chrdev_open,
  1644. .release = idetape_chrdev_release,
  1645. };
  1646. static int idetape_open(struct block_device *bdev, fmode_t mode)
  1647. {
  1648. struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
  1649. if (!tape)
  1650. return -ENXIO;
  1651. return 0;
  1652. }
  1653. static int idetape_release(struct gendisk *disk, fmode_t mode)
  1654. {
  1655. struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
  1656. ide_tape_put(tape);
  1657. return 0;
  1658. }
  1659. static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
  1660. unsigned int cmd, unsigned long arg)
  1661. {
  1662. struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
  1663. ide_drive_t *drive = tape->drive;
  1664. int err = generic_ide_ioctl(drive, bdev, cmd, arg);
  1665. if (err == -EINVAL)
  1666. err = idetape_blkdev_ioctl(drive, cmd, arg);
  1667. return err;
  1668. }
  1669. static struct block_device_operations idetape_block_ops = {
  1670. .owner = THIS_MODULE,
  1671. .open = idetape_open,
  1672. .release = idetape_release,
  1673. .locked_ioctl = idetape_ioctl,
  1674. };
  1675. static int ide_tape_probe(ide_drive_t *drive)
  1676. {
  1677. idetape_tape_t *tape;
  1678. struct gendisk *g;
  1679. int minor;
  1680. if (!strstr("ide-tape", drive->driver_req))
  1681. goto failed;
  1682. if (drive->media != ide_tape)
  1683. goto failed;
  1684. if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
  1685. ide_check_atapi_device(drive, DRV_NAME) == 0) {
  1686. printk(KERN_ERR "ide-tape: %s: not supported by this version of"
  1687. " the driver\n", drive->name);
  1688. goto failed;
  1689. }
  1690. tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
  1691. if (tape == NULL) {
  1692. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
  1693. drive->name);
  1694. goto failed;
  1695. }
  1696. g = alloc_disk(1 << PARTN_BITS);
  1697. if (!g)
  1698. goto out_free_tape;
  1699. ide_init_disk(g, drive);
  1700. tape->dev.parent = &drive->gendev;
  1701. tape->dev.release = ide_tape_release;
  1702. dev_set_name(&tape->dev, dev_name(&drive->gendev));
  1703. if (device_register(&tape->dev))
  1704. goto out_free_disk;
  1705. tape->drive = drive;
  1706. tape->driver = &idetape_driver;
  1707. tape->disk = g;
  1708. g->private_data = &tape->driver;
  1709. drive->driver_data = tape;
  1710. mutex_lock(&idetape_ref_mutex);
  1711. for (minor = 0; idetape_devs[minor]; minor++)
  1712. ;
  1713. idetape_devs[minor] = tape;
  1714. mutex_unlock(&idetape_ref_mutex);
  1715. idetape_setup(drive, tape, minor);
  1716. device_create(idetape_sysfs_class, &drive->gendev,
  1717. MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
  1718. device_create(idetape_sysfs_class, &drive->gendev,
  1719. MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
  1720. "n%s", tape->name);
  1721. g->fops = &idetape_block_ops;
  1722. ide_register_region(g);
  1723. return 0;
  1724. out_free_disk:
  1725. put_disk(g);
  1726. out_free_tape:
  1727. kfree(tape);
  1728. failed:
  1729. return -ENODEV;
  1730. }
  1731. static void __exit idetape_exit(void)
  1732. {
  1733. driver_unregister(&idetape_driver.gen_driver);
  1734. class_destroy(idetape_sysfs_class);
  1735. unregister_chrdev(IDETAPE_MAJOR, "ht");
  1736. }
  1737. static int __init idetape_init(void)
  1738. {
  1739. int error = 1;
  1740. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  1741. if (IS_ERR(idetape_sysfs_class)) {
  1742. idetape_sysfs_class = NULL;
  1743. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  1744. error = -EBUSY;
  1745. goto out;
  1746. }
  1747. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  1748. printk(KERN_ERR "ide-tape: Failed to register chrdev"
  1749. " interface\n");
  1750. error = -EBUSY;
  1751. goto out_free_class;
  1752. }
  1753. error = driver_register(&idetape_driver.gen_driver);
  1754. if (error)
  1755. goto out_free_driver;
  1756. return 0;
  1757. out_free_driver:
  1758. driver_unregister(&idetape_driver.gen_driver);
  1759. out_free_class:
  1760. class_destroy(idetape_sysfs_class);
  1761. out:
  1762. return error;
  1763. }
  1764. MODULE_ALIAS("ide:*m-tape*");
  1765. module_init(idetape_init);
  1766. module_exit(idetape_exit);
  1767. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
  1768. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  1769. MODULE_LICENSE("GPL");