ide-tape.c 75 KB

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