ide-tape.c 67 KB

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