ide-tape.c 66 KB

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