ide-tape.c 55 KB

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