ide-tape.c 75 KB

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