ide-tape.c 55 KB

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