ide-tape.c 55 KB

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