ide-tape.c 71 KB

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