ide-tape.c 71 KB

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