ide-tape.c 63 KB

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