ide-tape.c 55 KB

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