ide-tape.c 67 KB

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