ide-tape.c 61 KB

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