ide-tape.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764
  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. ide_hwif_t *hwif = drive->hwif;
  812. idetape_tape_t *tape = drive->driver_data;
  813. struct ide_atapi_pc *pc = tape->pc;
  814. u8 stat;
  815. stat = hwif->read_status(hwif);
  816. if (stat & SEEK_STAT) {
  817. if (stat & ERR_STAT) {
  818. /* Error detected */
  819. if (pc->c[0] != TEST_UNIT_READY)
  820. printk(KERN_ERR "ide-tape: %s: I/O error, ",
  821. tape->name);
  822. /* Retry operation */
  823. idetape_retry_pc(drive);
  824. return ide_stopped;
  825. }
  826. pc->error = 0;
  827. } else {
  828. pc->error = IDETAPE_ERROR_GENERAL;
  829. tape->failed_pc = NULL;
  830. }
  831. pc->callback(drive);
  832. return ide_stopped;
  833. }
  834. static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
  835. struct ide_atapi_pc *pc, unsigned int length,
  836. struct idetape_bh *bh, u8 opcode)
  837. {
  838. idetape_init_pc(pc);
  839. put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
  840. pc->c[1] = 1;
  841. pc->bh = bh;
  842. pc->buf = NULL;
  843. pc->buf_size = length * tape->blk_size;
  844. pc->req_xfer = pc->buf_size;
  845. if (pc->req_xfer == tape->buffer_size)
  846. pc->flags |= PC_FLAG_DMA_OK;
  847. if (opcode == READ_6) {
  848. pc->c[0] = READ_6;
  849. atomic_set(&bh->b_count, 0);
  850. } else if (opcode == WRITE_6) {
  851. pc->c[0] = WRITE_6;
  852. pc->flags |= PC_FLAG_WRITING;
  853. pc->b_data = bh->b_data;
  854. pc->b_count = atomic_read(&bh->b_count);
  855. }
  856. }
  857. static ide_startstop_t idetape_do_request(ide_drive_t *drive,
  858. struct request *rq, sector_t block)
  859. {
  860. ide_hwif_t *hwif = drive->hwif;
  861. idetape_tape_t *tape = drive->driver_data;
  862. struct ide_atapi_pc *pc = NULL;
  863. struct request *postponed_rq = tape->postponed_rq;
  864. u8 stat;
  865. debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld,"
  866. " current_nr_sectors: %d\n",
  867. rq->sector, rq->nr_sectors, rq->current_nr_sectors);
  868. if (!blk_special_request(rq)) {
  869. /* We do not support buffer cache originated requests. */
  870. printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
  871. "request queue (%d)\n", drive->name, rq->cmd_type);
  872. ide_end_request(drive, 0, 0);
  873. return ide_stopped;
  874. }
  875. /* Retry a failed packet command */
  876. if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) {
  877. pc = tape->failed_pc;
  878. goto out;
  879. }
  880. if (postponed_rq != NULL)
  881. if (rq != postponed_rq) {
  882. printk(KERN_ERR "ide-tape: ide-tape.c bug - "
  883. "Two DSC requests were queued\n");
  884. idetape_end_request(drive, 0, 0);
  885. return ide_stopped;
  886. }
  887. tape->postponed_rq = NULL;
  888. /*
  889. * If the tape is still busy, postpone our request and service
  890. * the other device meanwhile.
  891. */
  892. stat = hwif->read_status(hwif);
  893. if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
  894. set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
  895. if (drive->post_reset == 1) {
  896. set_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags);
  897. drive->post_reset = 0;
  898. }
  899. if (!test_and_clear_bit(IDETAPE_FLAG_IGNORE_DSC, &tape->flags) &&
  900. (stat & SEEK_STAT) == 0) {
  901. if (postponed_rq == NULL) {
  902. tape->dsc_polling_start = jiffies;
  903. tape->dsc_poll_freq = tape->best_dsc_rw_freq;
  904. tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
  905. } else if (time_after(jiffies, tape->dsc_timeout)) {
  906. printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
  907. tape->name);
  908. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  909. idetape_media_access_finished(drive);
  910. return ide_stopped;
  911. } else {
  912. return ide_do_reset(drive);
  913. }
  914. } else if (time_after(jiffies,
  915. tape->dsc_polling_start +
  916. IDETAPE_DSC_MA_THRESHOLD))
  917. tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
  918. idetape_postpone_request(drive);
  919. return ide_stopped;
  920. }
  921. if (rq->cmd[0] & REQ_IDETAPE_READ) {
  922. pc = idetape_next_pc_storage(drive);
  923. ide_tape_create_rw_cmd(tape, pc, rq->current_nr_sectors,
  924. (struct idetape_bh *)rq->special,
  925. READ_6);
  926. goto out;
  927. }
  928. if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
  929. pc = idetape_next_pc_storage(drive);
  930. ide_tape_create_rw_cmd(tape, pc, rq->current_nr_sectors,
  931. (struct idetape_bh *)rq->special,
  932. WRITE_6);
  933. goto out;
  934. }
  935. if (rq->cmd[0] & REQ_IDETAPE_PC1) {
  936. pc = (struct ide_atapi_pc *) rq->buffer;
  937. rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
  938. rq->cmd[0] |= REQ_IDETAPE_PC2;
  939. goto out;
  940. }
  941. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  942. idetape_media_access_finished(drive);
  943. return ide_stopped;
  944. }
  945. BUG();
  946. out:
  947. if (test_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags))
  948. pc->flags |= PC_FLAG_DRQ_INTERRUPT;
  949. return idetape_issue_pc(drive, pc);
  950. }
  951. /*
  952. * The function below uses __get_free_pages to allocate a data buffer of size
  953. * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
  954. * much as possible.
  955. *
  956. * It returns a pointer to the newly allocated buffer, or NULL in case of
  957. * failure.
  958. */
  959. static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
  960. int full, int clear)
  961. {
  962. struct idetape_bh *prev_bh, *bh, *merge_bh;
  963. int pages = tape->pages_per_buffer;
  964. unsigned int order, b_allocd;
  965. char *b_data = NULL;
  966. merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  967. bh = merge_bh;
  968. if (bh == NULL)
  969. goto abort;
  970. order = fls(pages) - 1;
  971. bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
  972. if (!bh->b_data)
  973. goto abort;
  974. b_allocd = (1 << order) * PAGE_SIZE;
  975. pages &= (order-1);
  976. if (clear)
  977. memset(bh->b_data, 0, b_allocd);
  978. bh->b_reqnext = NULL;
  979. bh->b_size = b_allocd;
  980. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  981. while (pages) {
  982. order = fls(pages) - 1;
  983. b_data = (char *) __get_free_pages(GFP_KERNEL, order);
  984. if (!b_data)
  985. goto abort;
  986. b_allocd = (1 << order) * PAGE_SIZE;
  987. if (clear)
  988. memset(b_data, 0, b_allocd);
  989. /* newly allocated page frames below buffer header or ...*/
  990. if (bh->b_data == b_data + b_allocd) {
  991. bh->b_size += b_allocd;
  992. bh->b_data -= b_allocd;
  993. if (full)
  994. atomic_add(b_allocd, &bh->b_count);
  995. continue;
  996. }
  997. /* they are above the header */
  998. if (b_data == bh->b_data + bh->b_size) {
  999. bh->b_size += b_allocd;
  1000. if (full)
  1001. atomic_add(b_allocd, &bh->b_count);
  1002. continue;
  1003. }
  1004. prev_bh = bh;
  1005. bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  1006. if (!bh) {
  1007. free_pages((unsigned long) b_data, order);
  1008. goto abort;
  1009. }
  1010. bh->b_reqnext = NULL;
  1011. bh->b_data = b_data;
  1012. bh->b_size = b_allocd;
  1013. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  1014. prev_bh->b_reqnext = bh;
  1015. pages &= (order-1);
  1016. }
  1017. bh->b_size -= tape->excess_bh_size;
  1018. if (full)
  1019. atomic_sub(tape->excess_bh_size, &bh->b_count);
  1020. return merge_bh;
  1021. abort:
  1022. ide_tape_kfree_buffer(tape);
  1023. return NULL;
  1024. }
  1025. static int idetape_copy_stage_from_user(idetape_tape_t *tape,
  1026. const char __user *buf, int n)
  1027. {
  1028. struct idetape_bh *bh = tape->bh;
  1029. int count;
  1030. int ret = 0;
  1031. while (n) {
  1032. if (bh == NULL) {
  1033. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  1034. __func__);
  1035. return 1;
  1036. }
  1037. count = min((unsigned int)
  1038. (bh->b_size - atomic_read(&bh->b_count)),
  1039. (unsigned int)n);
  1040. if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
  1041. count))
  1042. ret = 1;
  1043. n -= count;
  1044. atomic_add(count, &bh->b_count);
  1045. buf += count;
  1046. if (atomic_read(&bh->b_count) == bh->b_size) {
  1047. bh = bh->b_reqnext;
  1048. if (bh)
  1049. atomic_set(&bh->b_count, 0);
  1050. }
  1051. }
  1052. tape->bh = bh;
  1053. return ret;
  1054. }
  1055. static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
  1056. int n)
  1057. {
  1058. struct idetape_bh *bh = tape->bh;
  1059. int count;
  1060. int ret = 0;
  1061. while (n) {
  1062. if (bh == NULL) {
  1063. printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
  1064. __func__);
  1065. return 1;
  1066. }
  1067. count = min(tape->b_count, n);
  1068. if (copy_to_user(buf, tape->b_data, count))
  1069. ret = 1;
  1070. n -= count;
  1071. tape->b_data += count;
  1072. tape->b_count -= count;
  1073. buf += count;
  1074. if (!tape->b_count) {
  1075. bh = bh->b_reqnext;
  1076. tape->bh = bh;
  1077. if (bh) {
  1078. tape->b_data = bh->b_data;
  1079. tape->b_count = atomic_read(&bh->b_count);
  1080. }
  1081. }
  1082. }
  1083. return ret;
  1084. }
  1085. static void idetape_init_merge_buffer(idetape_tape_t *tape)
  1086. {
  1087. struct idetape_bh *bh = tape->merge_bh;
  1088. tape->bh = tape->merge_bh;
  1089. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  1090. atomic_set(&bh->b_count, 0);
  1091. else {
  1092. tape->b_data = bh->b_data;
  1093. tape->b_count = atomic_read(&bh->b_count);
  1094. }
  1095. }
  1096. /*
  1097. * Write a filemark if write_filemark=1. Flush the device buffers without
  1098. * writing a filemark otherwise.
  1099. */
  1100. static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
  1101. struct ide_atapi_pc *pc, int write_filemark)
  1102. {
  1103. idetape_init_pc(pc);
  1104. pc->c[0] = WRITE_FILEMARKS;
  1105. pc->c[4] = write_filemark;
  1106. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1107. }
  1108. static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
  1109. {
  1110. idetape_init_pc(pc);
  1111. pc->c[0] = TEST_UNIT_READY;
  1112. }
  1113. /*
  1114. * We add a special packet command request to the tail of the request queue, and
  1115. * wait for it to be serviced. This is not to be called from within the request
  1116. * handling part of the driver! We allocate here data on the stack and it is
  1117. * valid until the request is finished. This is not the case for the bottom part
  1118. * of the driver, where we are always leaving the functions to wait for an
  1119. * interrupt or a timer event.
  1120. *
  1121. * From the bottom part of the driver, we should allocate safe memory using
  1122. * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request
  1123. * to the request list without waiting for it to be serviced! In that case, we
  1124. * usually use idetape_queue_pc_head().
  1125. */
  1126. static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
  1127. {
  1128. struct ide_tape_obj *tape = drive->driver_data;
  1129. struct request *rq;
  1130. int error;
  1131. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  1132. rq->cmd_type = REQ_TYPE_SPECIAL;
  1133. rq->cmd[0] = REQ_IDETAPE_PC1;
  1134. rq->buffer = (char *)pc;
  1135. error = blk_execute_rq(drive->queue, tape->disk, rq, 0);
  1136. blk_put_request(rq);
  1137. return error;
  1138. }
  1139. static void idetape_create_load_unload_cmd(ide_drive_t *drive,
  1140. struct ide_atapi_pc *pc, int cmd)
  1141. {
  1142. idetape_init_pc(pc);
  1143. pc->c[0] = START_STOP;
  1144. pc->c[4] = cmd;
  1145. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1146. }
  1147. static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
  1148. {
  1149. idetape_tape_t *tape = drive->driver_data;
  1150. struct ide_atapi_pc pc;
  1151. int load_attempted = 0;
  1152. /* Wait for the tape to become ready */
  1153. set_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
  1154. timeout += jiffies;
  1155. while (time_before(jiffies, timeout)) {
  1156. idetape_create_test_unit_ready_cmd(&pc);
  1157. if (!idetape_queue_pc_tail(drive, &pc))
  1158. return 0;
  1159. if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
  1160. || (tape->asc == 0x3A)) {
  1161. /* no media */
  1162. if (load_attempted)
  1163. return -ENOMEDIUM;
  1164. idetape_create_load_unload_cmd(drive, &pc,
  1165. IDETAPE_LU_LOAD_MASK);
  1166. idetape_queue_pc_tail(drive, &pc);
  1167. load_attempted = 1;
  1168. /* not about to be ready */
  1169. } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
  1170. (tape->ascq == 1 || tape->ascq == 8)))
  1171. return -EIO;
  1172. msleep(100);
  1173. }
  1174. return -EIO;
  1175. }
  1176. static int idetape_flush_tape_buffers(ide_drive_t *drive)
  1177. {
  1178. struct ide_atapi_pc pc;
  1179. int rc;
  1180. idetape_create_write_filemark_cmd(drive, &pc, 0);
  1181. rc = idetape_queue_pc_tail(drive, &pc);
  1182. if (rc)
  1183. return rc;
  1184. idetape_wait_ready(drive, 60 * 5 * HZ);
  1185. return 0;
  1186. }
  1187. static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc)
  1188. {
  1189. idetape_init_pc(pc);
  1190. pc->c[0] = READ_POSITION;
  1191. pc->req_xfer = 20;
  1192. }
  1193. static int idetape_read_position(ide_drive_t *drive)
  1194. {
  1195. idetape_tape_t *tape = drive->driver_data;
  1196. struct ide_atapi_pc pc;
  1197. int position;
  1198. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1199. idetape_create_read_position_cmd(&pc);
  1200. if (idetape_queue_pc_tail(drive, &pc))
  1201. return -1;
  1202. position = tape->first_frame;
  1203. return position;
  1204. }
  1205. static void idetape_create_locate_cmd(ide_drive_t *drive,
  1206. struct ide_atapi_pc *pc,
  1207. unsigned int block, u8 partition, int skip)
  1208. {
  1209. idetape_init_pc(pc);
  1210. pc->c[0] = POSITION_TO_ELEMENT;
  1211. pc->c[1] = 2;
  1212. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
  1213. pc->c[8] = partition;
  1214. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1215. }
  1216. static int idetape_create_prevent_cmd(ide_drive_t *drive,
  1217. struct ide_atapi_pc *pc, int prevent)
  1218. {
  1219. idetape_tape_t *tape = drive->driver_data;
  1220. /* device supports locking according to capabilities page */
  1221. if (!(tape->caps[6] & 0x01))
  1222. return 0;
  1223. idetape_init_pc(pc);
  1224. pc->c[0] = ALLOW_MEDIUM_REMOVAL;
  1225. pc->c[4] = prevent;
  1226. return 1;
  1227. }
  1228. static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
  1229. {
  1230. idetape_tape_t *tape = drive->driver_data;
  1231. if (tape->chrdev_dir != IDETAPE_DIR_READ)
  1232. return;
  1233. clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags);
  1234. tape->merge_bh_size = 0;
  1235. if (tape->merge_bh != NULL) {
  1236. ide_tape_kfree_buffer(tape);
  1237. tape->merge_bh = NULL;
  1238. }
  1239. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1240. }
  1241. /*
  1242. * Position the tape to the requested block using the LOCATE packet command.
  1243. * A READ POSITION command is then issued to check where we are positioned. Like
  1244. * all higher level operations, we queue the commands at the tail of the request
  1245. * queue and wait for their completion.
  1246. */
  1247. static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
  1248. u8 partition, int skip)
  1249. {
  1250. idetape_tape_t *tape = drive->driver_data;
  1251. int retval;
  1252. struct ide_atapi_pc pc;
  1253. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1254. __ide_tape_discard_merge_buffer(drive);
  1255. idetape_wait_ready(drive, 60 * 5 * HZ);
  1256. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  1257. retval = idetape_queue_pc_tail(drive, &pc);
  1258. if (retval)
  1259. return (retval);
  1260. idetape_create_read_position_cmd(&pc);
  1261. return (idetape_queue_pc_tail(drive, &pc));
  1262. }
  1263. static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
  1264. int restore_position)
  1265. {
  1266. idetape_tape_t *tape = drive->driver_data;
  1267. int seek, position;
  1268. __ide_tape_discard_merge_buffer(drive);
  1269. if (restore_position) {
  1270. position = idetape_read_position(drive);
  1271. seek = position > 0 ? position : 0;
  1272. if (idetape_position_tape(drive, seek, 0, 0)) {
  1273. printk(KERN_INFO "ide-tape: %s: position_tape failed in"
  1274. " %s\n", tape->name, __func__);
  1275. return;
  1276. }
  1277. }
  1278. }
  1279. /*
  1280. * Generate a read/write request for the block device interface and wait for it
  1281. * to be serviced.
  1282. */
  1283. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks,
  1284. struct idetape_bh *bh)
  1285. {
  1286. idetape_tape_t *tape = drive->driver_data;
  1287. struct request *rq;
  1288. int ret, errors;
  1289. debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
  1290. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  1291. rq->cmd_type = REQ_TYPE_SPECIAL;
  1292. rq->cmd[0] = cmd;
  1293. rq->rq_disk = tape->disk;
  1294. rq->special = (void *)bh;
  1295. rq->sector = tape->first_frame;
  1296. rq->nr_sectors = blocks;
  1297. rq->current_nr_sectors = blocks;
  1298. blk_execute_rq(drive->queue, tape->disk, rq, 0);
  1299. errors = rq->errors;
  1300. ret = tape->blk_size * (blocks - rq->current_nr_sectors);
  1301. blk_put_request(rq);
  1302. if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
  1303. return 0;
  1304. if (tape->merge_bh)
  1305. idetape_init_merge_buffer(tape);
  1306. if (errors == IDETAPE_ERROR_GENERAL)
  1307. return -EIO;
  1308. return ret;
  1309. }
  1310. static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
  1311. {
  1312. idetape_init_pc(pc);
  1313. pc->c[0] = INQUIRY;
  1314. pc->c[4] = 254;
  1315. pc->req_xfer = 254;
  1316. }
  1317. static void idetape_create_rewind_cmd(ide_drive_t *drive,
  1318. struct ide_atapi_pc *pc)
  1319. {
  1320. idetape_init_pc(pc);
  1321. pc->c[0] = REZERO_UNIT;
  1322. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1323. }
  1324. static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
  1325. {
  1326. idetape_init_pc(pc);
  1327. pc->c[0] = ERASE;
  1328. pc->c[1] = 1;
  1329. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1330. }
  1331. static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
  1332. {
  1333. idetape_init_pc(pc);
  1334. pc->c[0] = SPACE;
  1335. put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
  1336. pc->c[1] = cmd;
  1337. pc->flags |= PC_FLAG_WAIT_FOR_DSC;
  1338. }
  1339. /* Queue up a character device originated write request. */
  1340. static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
  1341. {
  1342. idetape_tape_t *tape = drive->driver_data;
  1343. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1344. return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
  1345. blocks, tape->merge_bh);
  1346. }
  1347. static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
  1348. {
  1349. idetape_tape_t *tape = drive->driver_data;
  1350. int blocks, min;
  1351. struct idetape_bh *bh;
  1352. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1353. printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
  1354. " but we are not writing.\n");
  1355. return;
  1356. }
  1357. if (tape->merge_bh_size > tape->buffer_size) {
  1358. printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
  1359. tape->merge_bh_size = tape->buffer_size;
  1360. }
  1361. if (tape->merge_bh_size) {
  1362. blocks = tape->merge_bh_size / tape->blk_size;
  1363. if (tape->merge_bh_size % tape->blk_size) {
  1364. unsigned int i;
  1365. blocks++;
  1366. i = tape->blk_size - tape->merge_bh_size %
  1367. tape->blk_size;
  1368. bh = tape->bh->b_reqnext;
  1369. while (bh) {
  1370. atomic_set(&bh->b_count, 0);
  1371. bh = bh->b_reqnext;
  1372. }
  1373. bh = tape->bh;
  1374. while (i) {
  1375. if (bh == NULL) {
  1376. printk(KERN_INFO "ide-tape: bug,"
  1377. " bh NULL\n");
  1378. break;
  1379. }
  1380. min = min(i, (unsigned int)(bh->b_size -
  1381. atomic_read(&bh->b_count)));
  1382. memset(bh->b_data + atomic_read(&bh->b_count),
  1383. 0, min);
  1384. atomic_add(min, &bh->b_count);
  1385. i -= min;
  1386. bh = bh->b_reqnext;
  1387. }
  1388. }
  1389. (void) idetape_add_chrdev_write_request(drive, blocks);
  1390. tape->merge_bh_size = 0;
  1391. }
  1392. if (tape->merge_bh != NULL) {
  1393. ide_tape_kfree_buffer(tape);
  1394. tape->merge_bh = NULL;
  1395. }
  1396. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1397. }
  1398. static int idetape_init_read(ide_drive_t *drive)
  1399. {
  1400. idetape_tape_t *tape = drive->driver_data;
  1401. int bytes_read;
  1402. /* Initialize read operation */
  1403. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1404. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1405. ide_tape_flush_merge_buffer(drive);
  1406. idetape_flush_tape_buffers(drive);
  1407. }
  1408. if (tape->merge_bh || tape->merge_bh_size) {
  1409. printk(KERN_ERR "ide-tape: merge_bh_size should be"
  1410. " 0 now\n");
  1411. tape->merge_bh_size = 0;
  1412. }
  1413. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
  1414. if (!tape->merge_bh)
  1415. return -ENOMEM;
  1416. tape->chrdev_dir = IDETAPE_DIR_READ;
  1417. /*
  1418. * Issue a read 0 command to ensure that DSC handshake is
  1419. * switched from completion mode to buffer available mode.
  1420. * No point in issuing this if DSC overlap isn't supported, some
  1421. * drives (Seagate STT3401A) will return an error.
  1422. */
  1423. if (drive->dsc_overlap) {
  1424. bytes_read = idetape_queue_rw_tail(drive,
  1425. REQ_IDETAPE_READ, 0,
  1426. tape->merge_bh);
  1427. if (bytes_read < 0) {
  1428. ide_tape_kfree_buffer(tape);
  1429. tape->merge_bh = NULL;
  1430. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1431. return bytes_read;
  1432. }
  1433. }
  1434. }
  1435. return 0;
  1436. }
  1437. /* called from idetape_chrdev_read() to service a chrdev read request. */
  1438. static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
  1439. {
  1440. idetape_tape_t *tape = drive->driver_data;
  1441. debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks);
  1442. /* If we are at a filemark, return a read length of 0 */
  1443. if (test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
  1444. return 0;
  1445. idetape_init_read(drive);
  1446. return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks,
  1447. tape->merge_bh);
  1448. }
  1449. static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
  1450. {
  1451. idetape_tape_t *tape = drive->driver_data;
  1452. struct idetape_bh *bh;
  1453. int blocks;
  1454. while (bcount) {
  1455. unsigned int count;
  1456. bh = tape->merge_bh;
  1457. count = min(tape->buffer_size, bcount);
  1458. bcount -= count;
  1459. blocks = count / tape->blk_size;
  1460. while (count) {
  1461. atomic_set(&bh->b_count,
  1462. min(count, (unsigned int)bh->b_size));
  1463. memset(bh->b_data, 0, atomic_read(&bh->b_count));
  1464. count -= atomic_read(&bh->b_count);
  1465. bh = bh->b_reqnext;
  1466. }
  1467. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
  1468. tape->merge_bh);
  1469. }
  1470. }
  1471. /*
  1472. * Rewinds the tape to the Beginning Of the current Partition (BOP). We
  1473. * currently support only one partition.
  1474. */
  1475. static int idetape_rewind_tape(ide_drive_t *drive)
  1476. {
  1477. int retval;
  1478. struct ide_atapi_pc pc;
  1479. idetape_tape_t *tape;
  1480. tape = drive->driver_data;
  1481. debug_log(DBG_SENSE, "Enter %s\n", __func__);
  1482. idetape_create_rewind_cmd(drive, &pc);
  1483. retval = idetape_queue_pc_tail(drive, &pc);
  1484. if (retval)
  1485. return retval;
  1486. idetape_create_read_position_cmd(&pc);
  1487. retval = idetape_queue_pc_tail(drive, &pc);
  1488. if (retval)
  1489. return retval;
  1490. return 0;
  1491. }
  1492. /* mtio.h compatible commands should be issued to the chrdev interface. */
  1493. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
  1494. unsigned long arg)
  1495. {
  1496. idetape_tape_t *tape = drive->driver_data;
  1497. void __user *argp = (void __user *)arg;
  1498. struct idetape_config {
  1499. int dsc_rw_frequency;
  1500. int dsc_media_access_frequency;
  1501. int nr_stages;
  1502. } config;
  1503. debug_log(DBG_PROCS, "Enter %s\n", __func__);
  1504. switch (cmd) {
  1505. case 0x0340:
  1506. if (copy_from_user(&config, argp, sizeof(config)))
  1507. return -EFAULT;
  1508. tape->best_dsc_rw_freq = config.dsc_rw_frequency;
  1509. break;
  1510. case 0x0350:
  1511. config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
  1512. config.nr_stages = 1;
  1513. if (copy_to_user(argp, &config, sizeof(config)))
  1514. return -EFAULT;
  1515. break;
  1516. default:
  1517. return -EIO;
  1518. }
  1519. return 0;
  1520. }
  1521. static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
  1522. int mt_count)
  1523. {
  1524. idetape_tape_t *tape = drive->driver_data;
  1525. struct ide_atapi_pc pc;
  1526. int retval, count = 0;
  1527. int sprev = !!(tape->caps[4] & 0x20);
  1528. if (mt_count == 0)
  1529. return 0;
  1530. if (MTBSF == mt_op || MTBSFM == mt_op) {
  1531. if (!sprev)
  1532. return -EIO;
  1533. mt_count = -mt_count;
  1534. }
  1535. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  1536. tape->merge_bh_size = 0;
  1537. if (test_and_clear_bit(IDETAPE_FLAG_FILEMARK, &tape->flags))
  1538. ++count;
  1539. ide_tape_discard_merge_buffer(drive, 0);
  1540. }
  1541. switch (mt_op) {
  1542. case MTFSF:
  1543. case MTBSF:
  1544. idetape_create_space_cmd(&pc, mt_count - count,
  1545. IDETAPE_SPACE_OVER_FILEMARK);
  1546. return idetape_queue_pc_tail(drive, &pc);
  1547. case MTFSFM:
  1548. case MTBSFM:
  1549. if (!sprev)
  1550. return -EIO;
  1551. retval = idetape_space_over_filemarks(drive, MTFSF,
  1552. mt_count - count);
  1553. if (retval)
  1554. return retval;
  1555. count = (MTBSFM == mt_op ? 1 : -1);
  1556. return idetape_space_over_filemarks(drive, MTFSF, count);
  1557. default:
  1558. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1559. mt_op);
  1560. return -EIO;
  1561. }
  1562. }
  1563. /*
  1564. * Our character device read / write functions.
  1565. *
  1566. * The tape is optimized to maximize throughput when it is transferring an
  1567. * integral number of the "continuous transfer limit", which is a parameter of
  1568. * the specific tape (26kB on my particular tape, 32kB for Onstream).
  1569. *
  1570. * As of version 1.3 of the driver, the character device provides an abstract
  1571. * continuous view of the media - any mix of block sizes (even 1 byte) on the
  1572. * same backup/restore procedure is supported. The driver will internally
  1573. * convert the requests to the recommended transfer unit, so that an unmatch
  1574. * between the user's block size to the recommended size will only result in a
  1575. * (slightly) increased driver overhead, but will no longer hit performance.
  1576. * This is not applicable to Onstream.
  1577. */
  1578. static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
  1579. size_t count, loff_t *ppos)
  1580. {
  1581. struct ide_tape_obj *tape = ide_tape_f(file);
  1582. ide_drive_t *drive = tape->drive;
  1583. ssize_t bytes_read, temp, actually_read = 0, rc;
  1584. ssize_t ret = 0;
  1585. u16 ctl = *(u16 *)&tape->caps[12];
  1586. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1587. if (tape->chrdev_dir != IDETAPE_DIR_READ) {
  1588. if (test_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags))
  1589. if (count > tape->blk_size &&
  1590. (count % tape->blk_size) == 0)
  1591. tape->user_bs_factor = count / tape->blk_size;
  1592. }
  1593. rc = idetape_init_read(drive);
  1594. if (rc < 0)
  1595. return rc;
  1596. if (count == 0)
  1597. return (0);
  1598. if (tape->merge_bh_size) {
  1599. actually_read = min((unsigned int)(tape->merge_bh_size),
  1600. (unsigned int)count);
  1601. if (idetape_copy_stage_to_user(tape, buf, actually_read))
  1602. ret = -EFAULT;
  1603. buf += actually_read;
  1604. tape->merge_bh_size -= actually_read;
  1605. count -= actually_read;
  1606. }
  1607. while (count >= tape->buffer_size) {
  1608. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1609. if (bytes_read <= 0)
  1610. goto finish;
  1611. if (idetape_copy_stage_to_user(tape, buf, bytes_read))
  1612. ret = -EFAULT;
  1613. buf += bytes_read;
  1614. count -= bytes_read;
  1615. actually_read += bytes_read;
  1616. }
  1617. if (count) {
  1618. bytes_read = idetape_add_chrdev_read_request(drive, ctl);
  1619. if (bytes_read <= 0)
  1620. goto finish;
  1621. temp = min((unsigned long)count, (unsigned long)bytes_read);
  1622. if (idetape_copy_stage_to_user(tape, buf, temp))
  1623. ret = -EFAULT;
  1624. actually_read += temp;
  1625. tape->merge_bh_size = bytes_read-temp;
  1626. }
  1627. finish:
  1628. if (!actually_read && test_bit(IDETAPE_FLAG_FILEMARK, &tape->flags)) {
  1629. debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
  1630. idetape_space_over_filemarks(drive, MTFSF, 1);
  1631. return 0;
  1632. }
  1633. return ret ? ret : actually_read;
  1634. }
  1635. static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
  1636. size_t count, loff_t *ppos)
  1637. {
  1638. struct ide_tape_obj *tape = ide_tape_f(file);
  1639. ide_drive_t *drive = tape->drive;
  1640. ssize_t actually_written = 0;
  1641. ssize_t ret = 0;
  1642. u16 ctl = *(u16 *)&tape->caps[12];
  1643. /* The drive is write protected. */
  1644. if (tape->write_prot)
  1645. return -EACCES;
  1646. debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
  1647. /* Initialize write operation */
  1648. if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
  1649. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1650. ide_tape_discard_merge_buffer(drive, 1);
  1651. if (tape->merge_bh || tape->merge_bh_size) {
  1652. printk(KERN_ERR "ide-tape: merge_bh_size "
  1653. "should be 0 now\n");
  1654. tape->merge_bh_size = 0;
  1655. }
  1656. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
  1657. if (!tape->merge_bh)
  1658. return -ENOMEM;
  1659. tape->chrdev_dir = IDETAPE_DIR_WRITE;
  1660. idetape_init_merge_buffer(tape);
  1661. /*
  1662. * Issue a write 0 command to ensure that DSC handshake is
  1663. * switched from completion mode to buffer available mode. No
  1664. * point in issuing this if DSC overlap isn't supported, some
  1665. * drives (Seagate STT3401A) will return an error.
  1666. */
  1667. if (drive->dsc_overlap) {
  1668. ssize_t retval = idetape_queue_rw_tail(drive,
  1669. REQ_IDETAPE_WRITE, 0,
  1670. tape->merge_bh);
  1671. if (retval < 0) {
  1672. ide_tape_kfree_buffer(tape);
  1673. tape->merge_bh = NULL;
  1674. tape->chrdev_dir = IDETAPE_DIR_NONE;
  1675. return retval;
  1676. }
  1677. }
  1678. }
  1679. if (count == 0)
  1680. return (0);
  1681. if (tape->merge_bh_size) {
  1682. if (tape->merge_bh_size >= tape->buffer_size) {
  1683. printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
  1684. tape->merge_bh_size = 0;
  1685. }
  1686. actually_written = min((unsigned int)
  1687. (tape->buffer_size - tape->merge_bh_size),
  1688. (unsigned int)count);
  1689. if (idetape_copy_stage_from_user(tape, buf, actually_written))
  1690. ret = -EFAULT;
  1691. buf += actually_written;
  1692. tape->merge_bh_size += actually_written;
  1693. count -= actually_written;
  1694. if (tape->merge_bh_size == tape->buffer_size) {
  1695. ssize_t retval;
  1696. tape->merge_bh_size = 0;
  1697. retval = idetape_add_chrdev_write_request(drive, ctl);
  1698. if (retval <= 0)
  1699. return (retval);
  1700. }
  1701. }
  1702. while (count >= tape->buffer_size) {
  1703. ssize_t retval;
  1704. if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
  1705. ret = -EFAULT;
  1706. buf += tape->buffer_size;
  1707. count -= tape->buffer_size;
  1708. retval = idetape_add_chrdev_write_request(drive, ctl);
  1709. actually_written += tape->buffer_size;
  1710. if (retval <= 0)
  1711. return (retval);
  1712. }
  1713. if (count) {
  1714. actually_written += count;
  1715. if (idetape_copy_stage_from_user(tape, buf, count))
  1716. ret = -EFAULT;
  1717. tape->merge_bh_size += count;
  1718. }
  1719. return ret ? ret : actually_written;
  1720. }
  1721. static int idetape_write_filemark(ide_drive_t *drive)
  1722. {
  1723. struct ide_atapi_pc pc;
  1724. /* Write a filemark */
  1725. idetape_create_write_filemark_cmd(drive, &pc, 1);
  1726. if (idetape_queue_pc_tail(drive, &pc)) {
  1727. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  1728. return -EIO;
  1729. }
  1730. return 0;
  1731. }
  1732. /*
  1733. * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
  1734. * requested.
  1735. *
  1736. * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
  1737. * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
  1738. * usually not supported.
  1739. *
  1740. * The following commands are currently not supported:
  1741. *
  1742. * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
  1743. * MT_ST_WRITE_THRESHOLD.
  1744. */
  1745. static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
  1746. {
  1747. idetape_tape_t *tape = drive->driver_data;
  1748. struct ide_atapi_pc pc;
  1749. int i, retval;
  1750. debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",
  1751. mt_op, mt_count);
  1752. switch (mt_op) {
  1753. case MTFSF:
  1754. case MTFSFM:
  1755. case MTBSF:
  1756. case MTBSFM:
  1757. if (!mt_count)
  1758. return 0;
  1759. return idetape_space_over_filemarks(drive, mt_op, mt_count);
  1760. default:
  1761. break;
  1762. }
  1763. switch (mt_op) {
  1764. case MTWEOF:
  1765. if (tape->write_prot)
  1766. return -EACCES;
  1767. ide_tape_discard_merge_buffer(drive, 1);
  1768. for (i = 0; i < mt_count; i++) {
  1769. retval = idetape_write_filemark(drive);
  1770. if (retval)
  1771. return retval;
  1772. }
  1773. return 0;
  1774. case MTREW:
  1775. ide_tape_discard_merge_buffer(drive, 0);
  1776. if (idetape_rewind_tape(drive))
  1777. return -EIO;
  1778. return 0;
  1779. case MTLOAD:
  1780. ide_tape_discard_merge_buffer(drive, 0);
  1781. idetape_create_load_unload_cmd(drive, &pc,
  1782. IDETAPE_LU_LOAD_MASK);
  1783. return idetape_queue_pc_tail(drive, &pc);
  1784. case MTUNLOAD:
  1785. case MTOFFL:
  1786. /*
  1787. * If door is locked, attempt to unlock before
  1788. * attempting to eject.
  1789. */
  1790. if (tape->door_locked) {
  1791. if (idetape_create_prevent_cmd(drive, &pc, 0))
  1792. if (!idetape_queue_pc_tail(drive, &pc))
  1793. tape->door_locked = DOOR_UNLOCKED;
  1794. }
  1795. ide_tape_discard_merge_buffer(drive, 0);
  1796. idetape_create_load_unload_cmd(drive, &pc,
  1797. !IDETAPE_LU_LOAD_MASK);
  1798. retval = idetape_queue_pc_tail(drive, &pc);
  1799. if (!retval)
  1800. clear_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags);
  1801. return retval;
  1802. case MTNOP:
  1803. ide_tape_discard_merge_buffer(drive, 0);
  1804. return idetape_flush_tape_buffers(drive);
  1805. case MTRETEN:
  1806. ide_tape_discard_merge_buffer(drive, 0);
  1807. idetape_create_load_unload_cmd(drive, &pc,
  1808. IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  1809. return idetape_queue_pc_tail(drive, &pc);
  1810. case MTEOM:
  1811. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  1812. return idetape_queue_pc_tail(drive, &pc);
  1813. case MTERASE:
  1814. (void)idetape_rewind_tape(drive);
  1815. idetape_create_erase_cmd(&pc);
  1816. return idetape_queue_pc_tail(drive, &pc);
  1817. case MTSETBLK:
  1818. if (mt_count) {
  1819. if (mt_count < tape->blk_size ||
  1820. mt_count % tape->blk_size)
  1821. return -EIO;
  1822. tape->user_bs_factor = mt_count / tape->blk_size;
  1823. clear_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
  1824. } else
  1825. set_bit(IDETAPE_FLAG_DETECT_BS, &tape->flags);
  1826. return 0;
  1827. case MTSEEK:
  1828. ide_tape_discard_merge_buffer(drive, 0);
  1829. return idetape_position_tape(drive,
  1830. mt_count * tape->user_bs_factor, tape->partition, 0);
  1831. case MTSETPART:
  1832. ide_tape_discard_merge_buffer(drive, 0);
  1833. return idetape_position_tape(drive, 0, mt_count, 0);
  1834. case MTFSR:
  1835. case MTBSR:
  1836. case MTLOCK:
  1837. if (!idetape_create_prevent_cmd(drive, &pc, 1))
  1838. return 0;
  1839. retval = idetape_queue_pc_tail(drive, &pc);
  1840. if (retval)
  1841. return retval;
  1842. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  1843. return 0;
  1844. case MTUNLOCK:
  1845. if (!idetape_create_prevent_cmd(drive, &pc, 0))
  1846. return 0;
  1847. retval = idetape_queue_pc_tail(drive, &pc);
  1848. if (retval)
  1849. return retval;
  1850. tape->door_locked = DOOR_UNLOCKED;
  1851. return 0;
  1852. default:
  1853. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
  1854. mt_op);
  1855. return -EIO;
  1856. }
  1857. }
  1858. /*
  1859. * Our character device ioctls. General mtio.h magnetic io commands are
  1860. * supported here, and not in the corresponding block interface. Our own
  1861. * ide-tape ioctls are supported on both interfaces.
  1862. */
  1863. static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
  1864. unsigned int cmd, unsigned long arg)
  1865. {
  1866. struct ide_tape_obj *tape = ide_tape_f(file);
  1867. ide_drive_t *drive = tape->drive;
  1868. struct mtop mtop;
  1869. struct mtget mtget;
  1870. struct mtpos mtpos;
  1871. int block_offset = 0, position = tape->first_frame;
  1872. void __user *argp = (void __user *)arg;
  1873. debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd);
  1874. if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
  1875. ide_tape_flush_merge_buffer(drive);
  1876. idetape_flush_tape_buffers(drive);
  1877. }
  1878. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  1879. block_offset = tape->merge_bh_size /
  1880. (tape->blk_size * tape->user_bs_factor);
  1881. position = idetape_read_position(drive);
  1882. if (position < 0)
  1883. return -EIO;
  1884. }
  1885. switch (cmd) {
  1886. case MTIOCTOP:
  1887. if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
  1888. return -EFAULT;
  1889. return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
  1890. case MTIOCGET:
  1891. memset(&mtget, 0, sizeof(struct mtget));
  1892. mtget.mt_type = MT_ISSCSI2;
  1893. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  1894. mtget.mt_dsreg =
  1895. ((tape->blk_size * tape->user_bs_factor)
  1896. << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  1897. if (tape->drv_write_prot)
  1898. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  1899. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  1900. return -EFAULT;
  1901. return 0;
  1902. case MTIOCPOS:
  1903. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  1904. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  1905. return -EFAULT;
  1906. return 0;
  1907. default:
  1908. if (tape->chrdev_dir == IDETAPE_DIR_READ)
  1909. ide_tape_discard_merge_buffer(drive, 1);
  1910. return idetape_blkdev_ioctl(drive, cmd, arg);
  1911. }
  1912. }
  1913. /*
  1914. * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
  1915. * block size with the reported value.
  1916. */
  1917. static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
  1918. {
  1919. idetape_tape_t *tape = drive->driver_data;
  1920. struct ide_atapi_pc pc;
  1921. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  1922. if (idetape_queue_pc_tail(drive, &pc)) {
  1923. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  1924. if (tape->blk_size == 0) {
  1925. printk(KERN_WARNING "ide-tape: Cannot deal with zero "
  1926. "block size, assuming 32k\n");
  1927. tape->blk_size = 32768;
  1928. }
  1929. return;
  1930. }
  1931. tape->blk_size = (pc.buf[4 + 5] << 16) +
  1932. (pc.buf[4 + 6] << 8) +
  1933. pc.buf[4 + 7];
  1934. tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7;
  1935. }
  1936. static int idetape_chrdev_open(struct inode *inode, struct file *filp)
  1937. {
  1938. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  1939. ide_drive_t *drive;
  1940. idetape_tape_t *tape;
  1941. struct ide_atapi_pc pc;
  1942. int retval;
  1943. if (i >= MAX_HWIFS * MAX_DRIVES)
  1944. return -ENXIO;
  1945. lock_kernel();
  1946. tape = ide_tape_chrdev_get(i);
  1947. if (!tape) {
  1948. unlock_kernel();
  1949. return -ENXIO;
  1950. }
  1951. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  1952. /*
  1953. * We really want to do nonseekable_open(inode, filp); here, but some
  1954. * versions of tar incorrectly call lseek on tapes and bail out if that
  1955. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  1956. */
  1957. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  1958. drive = tape->drive;
  1959. filp->private_data = tape;
  1960. if (test_and_set_bit(IDETAPE_FLAG_BUSY, &tape->flags)) {
  1961. retval = -EBUSY;
  1962. goto out_put_tape;
  1963. }
  1964. retval = idetape_wait_ready(drive, 60 * HZ);
  1965. if (retval) {
  1966. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  1967. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  1968. goto out_put_tape;
  1969. }
  1970. idetape_read_position(drive);
  1971. if (!test_bit(IDETAPE_FLAG_ADDRESS_VALID, &tape->flags))
  1972. (void)idetape_rewind_tape(drive);
  1973. /* Read block size and write protect status from drive. */
  1974. ide_tape_get_bsize_from_bdesc(drive);
  1975. /* Set write protect flag if device is opened as read-only. */
  1976. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  1977. tape->write_prot = 1;
  1978. else
  1979. tape->write_prot = tape->drv_write_prot;
  1980. /* Make sure drive isn't write protected if user wants to write. */
  1981. if (tape->write_prot) {
  1982. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  1983. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  1984. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  1985. retval = -EROFS;
  1986. goto out_put_tape;
  1987. }
  1988. }
  1989. /* Lock the tape drive door so user can't eject. */
  1990. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  1991. if (idetape_create_prevent_cmd(drive, &pc, 1)) {
  1992. if (!idetape_queue_pc_tail(drive, &pc)) {
  1993. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  1994. tape->door_locked = DOOR_LOCKED;
  1995. }
  1996. }
  1997. }
  1998. unlock_kernel();
  1999. return 0;
  2000. out_put_tape:
  2001. ide_tape_put(tape);
  2002. unlock_kernel();
  2003. return retval;
  2004. }
  2005. static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
  2006. {
  2007. idetape_tape_t *tape = drive->driver_data;
  2008. ide_tape_flush_merge_buffer(drive);
  2009. tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0);
  2010. if (tape->merge_bh != NULL) {
  2011. idetape_pad_zeros(drive, tape->blk_size *
  2012. (tape->user_bs_factor - 1));
  2013. ide_tape_kfree_buffer(tape);
  2014. tape->merge_bh = NULL;
  2015. }
  2016. idetape_write_filemark(drive);
  2017. idetape_flush_tape_buffers(drive);
  2018. idetape_flush_tape_buffers(drive);
  2019. }
  2020. static int idetape_chrdev_release(struct inode *inode, struct file *filp)
  2021. {
  2022. struct ide_tape_obj *tape = ide_tape_f(filp);
  2023. ide_drive_t *drive = tape->drive;
  2024. struct ide_atapi_pc pc;
  2025. unsigned int minor = iminor(inode);
  2026. lock_kernel();
  2027. tape = drive->driver_data;
  2028. debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
  2029. if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
  2030. idetape_write_release(drive, minor);
  2031. if (tape->chrdev_dir == IDETAPE_DIR_READ) {
  2032. if (minor < 128)
  2033. ide_tape_discard_merge_buffer(drive, 1);
  2034. }
  2035. if (minor < 128 && test_bit(IDETAPE_FLAG_MEDIUM_PRESENT, &tape->flags))
  2036. (void) idetape_rewind_tape(drive);
  2037. if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
  2038. if (tape->door_locked == DOOR_LOCKED) {
  2039. if (idetape_create_prevent_cmd(drive, &pc, 0)) {
  2040. if (!idetape_queue_pc_tail(drive, &pc))
  2041. tape->door_locked = DOOR_UNLOCKED;
  2042. }
  2043. }
  2044. }
  2045. clear_bit(IDETAPE_FLAG_BUSY, &tape->flags);
  2046. ide_tape_put(tape);
  2047. unlock_kernel();
  2048. return 0;
  2049. }
  2050. /*
  2051. * check the contents of the ATAPI IDENTIFY command results. We return:
  2052. *
  2053. * 1 - If the tape can be supported by us, based on the information we have so
  2054. * far.
  2055. *
  2056. * 0 - If this tape driver is not currently supported by us.
  2057. */
  2058. static int idetape_identify_device(ide_drive_t *drive)
  2059. {
  2060. u8 gcw[2], protocol, device_type, removable, packet_size;
  2061. if (drive->id_read == 0)
  2062. return 1;
  2063. *((unsigned short *) &gcw) = drive->id->config;
  2064. protocol = (gcw[1] & 0xC0) >> 6;
  2065. device_type = gcw[1] & 0x1F;
  2066. removable = !!(gcw[0] & 0x80);
  2067. packet_size = gcw[0] & 0x3;
  2068. /* Check that we can support this device */
  2069. if (protocol != 2)
  2070. printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n",
  2071. protocol);
  2072. else if (device_type != 1)
  2073. printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set "
  2074. "to tape\n", device_type);
  2075. else if (!removable)
  2076. printk(KERN_ERR "ide-tape: The removable flag is not set\n");
  2077. else if (packet_size != 0) {
  2078. printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12"
  2079. " bytes\n", packet_size);
  2080. } else
  2081. return 1;
  2082. return 0;
  2083. }
  2084. static void idetape_get_inquiry_results(ide_drive_t *drive)
  2085. {
  2086. idetape_tape_t *tape = drive->driver_data;
  2087. struct ide_atapi_pc pc;
  2088. char fw_rev[6], vendor_id[10], product_id[18];
  2089. idetape_create_inquiry_cmd(&pc);
  2090. if (idetape_queue_pc_tail(drive, &pc)) {
  2091. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
  2092. tape->name);
  2093. return;
  2094. }
  2095. memcpy(vendor_id, &pc.buf[8], 8);
  2096. memcpy(product_id, &pc.buf[16], 16);
  2097. memcpy(fw_rev, &pc.buf[32], 4);
  2098. ide_fixstring(vendor_id, 10, 0);
  2099. ide_fixstring(product_id, 18, 0);
  2100. ide_fixstring(fw_rev, 6, 0);
  2101. printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n",
  2102. drive->name, tape->name, vendor_id, product_id, fw_rev);
  2103. }
  2104. /*
  2105. * Ask the tape about its various parameters. In particular, we will adjust our
  2106. * data transfer buffer size to the recommended value as returned by the tape.
  2107. */
  2108. static void idetape_get_mode_sense_results(ide_drive_t *drive)
  2109. {
  2110. idetape_tape_t *tape = drive->driver_data;
  2111. struct ide_atapi_pc pc;
  2112. u8 *caps;
  2113. u8 speed, max_speed;
  2114. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  2115. if (idetape_queue_pc_tail(drive, &pc)) {
  2116. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
  2117. " some default values\n");
  2118. tape->blk_size = 512;
  2119. put_unaligned(52, (u16 *)&tape->caps[12]);
  2120. put_unaligned(540, (u16 *)&tape->caps[14]);
  2121. put_unaligned(6*52, (u16 *)&tape->caps[16]);
  2122. return;
  2123. }
  2124. caps = pc.buf + 4 + pc.buf[3];
  2125. /* convert to host order and save for later use */
  2126. speed = be16_to_cpu(*(u16 *)&caps[14]);
  2127. max_speed = be16_to_cpu(*(u16 *)&caps[8]);
  2128. put_unaligned(max_speed, (u16 *)&caps[8]);
  2129. put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]);
  2130. put_unaligned(speed, (u16 *)&caps[14]);
  2131. put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]);
  2132. if (!speed) {
  2133. printk(KERN_INFO "ide-tape: %s: invalid tape speed "
  2134. "(assuming 650KB/sec)\n", drive->name);
  2135. put_unaligned(650, (u16 *)&caps[14]);
  2136. }
  2137. if (!max_speed) {
  2138. printk(KERN_INFO "ide-tape: %s: invalid max_speed "
  2139. "(assuming 650KB/sec)\n", drive->name);
  2140. put_unaligned(650, (u16 *)&caps[8]);
  2141. }
  2142. memcpy(&tape->caps, caps, 20);
  2143. if (caps[7] & 0x02)
  2144. tape->blk_size = 512;
  2145. else if (caps[7] & 0x04)
  2146. tape->blk_size = 1024;
  2147. }
  2148. #ifdef CONFIG_IDE_PROC_FS
  2149. static void idetape_add_settings(ide_drive_t *drive)
  2150. {
  2151. idetape_tape_t *tape = drive->driver_data;
  2152. ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff,
  2153. 1, 2, (u16 *)&tape->caps[16], NULL);
  2154. ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff,
  2155. 1, 1, (u16 *)&tape->caps[14], NULL);
  2156. ide_add_setting(drive, "buffer_size", SETTING_READ, TYPE_INT, 0, 0xffff,
  2157. 1, 1024, &tape->buffer_size, NULL);
  2158. ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN,
  2159. IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq,
  2160. NULL);
  2161. ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1,
  2162. 1, &drive->dsc_overlap, NULL);
  2163. ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff,
  2164. 1, 1, &tape->avg_speed, NULL);
  2165. ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1,
  2166. 1, &tape->debug_mask, NULL);
  2167. }
  2168. #else
  2169. static inline void idetape_add_settings(ide_drive_t *drive) { ; }
  2170. #endif
  2171. /*
  2172. * The function below is called to:
  2173. *
  2174. * 1. Initialize our various state variables.
  2175. * 2. Ask the tape for its capabilities.
  2176. * 3. Allocate a buffer which will be used for data transfer. The buffer size
  2177. * is chosen based on the recommendation which we received in step 2.
  2178. *
  2179. * Note that at this point ide.c already assigned us an irq, so that we can
  2180. * queue requests here and wait for their completion.
  2181. */
  2182. static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
  2183. {
  2184. unsigned long t;
  2185. int speed;
  2186. int buffer_size;
  2187. u8 gcw[2];
  2188. u16 *ctl = (u16 *)&tape->caps[12];
  2189. spin_lock_init(&tape->lock);
  2190. drive->dsc_overlap = 1;
  2191. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  2192. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  2193. tape->name);
  2194. drive->dsc_overlap = 0;
  2195. }
  2196. /* Seagate Travan drives do not support DSC overlap. */
  2197. if (strstr(drive->id->model, "Seagate STT3401"))
  2198. drive->dsc_overlap = 0;
  2199. tape->minor = minor;
  2200. tape->name[0] = 'h';
  2201. tape->name[1] = 't';
  2202. tape->name[2] = '0' + minor;
  2203. tape->chrdev_dir = IDETAPE_DIR_NONE;
  2204. tape->pc = tape->pc_stack;
  2205. *((unsigned short *) &gcw) = drive->id->config;
  2206. /* Command packet DRQ type */
  2207. if (((gcw[0] & 0x60) >> 5) == 1)
  2208. set_bit(IDETAPE_FLAG_DRQ_INTERRUPT, &tape->flags);
  2209. idetape_get_inquiry_results(drive);
  2210. idetape_get_mode_sense_results(drive);
  2211. ide_tape_get_bsize_from_bdesc(drive);
  2212. tape->user_bs_factor = 1;
  2213. tape->buffer_size = *ctl * tape->blk_size;
  2214. while (tape->buffer_size > 0xffff) {
  2215. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  2216. *ctl /= 2;
  2217. tape->buffer_size = *ctl * tape->blk_size;
  2218. }
  2219. buffer_size = tape->buffer_size;
  2220. tape->pages_per_buffer = buffer_size / PAGE_SIZE;
  2221. if (buffer_size % PAGE_SIZE) {
  2222. tape->pages_per_buffer++;
  2223. tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
  2224. }
  2225. /* select the "best" DSC read/write polling freq */
  2226. speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
  2227. t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
  2228. /*
  2229. * Ensure that the number we got makes sense; limit it within
  2230. * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  2231. */
  2232. tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
  2233. IDETAPE_DSC_RW_MAX);
  2234. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  2235. "%lums tDSC%s\n",
  2236. drive->name, tape->name, *(u16 *)&tape->caps[14],
  2237. (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
  2238. tape->buffer_size / 1024,
  2239. tape->best_dsc_rw_freq * 1000 / HZ,
  2240. drive->using_dma ? ", DMA":"");
  2241. idetape_add_settings(drive);
  2242. }
  2243. static void ide_tape_remove(ide_drive_t *drive)
  2244. {
  2245. idetape_tape_t *tape = drive->driver_data;
  2246. ide_proc_unregister_driver(drive, tape->driver);
  2247. ide_unregister_region(tape->disk);
  2248. ide_tape_put(tape);
  2249. }
  2250. static void ide_tape_release(struct kref *kref)
  2251. {
  2252. struct ide_tape_obj *tape = to_ide_tape(kref);
  2253. ide_drive_t *drive = tape->drive;
  2254. struct gendisk *g = tape->disk;
  2255. BUG_ON(tape->merge_bh_size);
  2256. drive->dsc_overlap = 0;
  2257. drive->driver_data = NULL;
  2258. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  2259. device_destroy(idetape_sysfs_class,
  2260. MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  2261. idetape_devs[tape->minor] = NULL;
  2262. g->private_data = NULL;
  2263. put_disk(g);
  2264. kfree(tape);
  2265. }
  2266. #ifdef CONFIG_IDE_PROC_FS
  2267. static int proc_idetape_read_name
  2268. (char *page, char **start, off_t off, int count, int *eof, void *data)
  2269. {
  2270. ide_drive_t *drive = (ide_drive_t *) data;
  2271. idetape_tape_t *tape = drive->driver_data;
  2272. char *out = page;
  2273. int len;
  2274. len = sprintf(out, "%s\n", tape->name);
  2275. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  2276. }
  2277. static ide_proc_entry_t idetape_proc[] = {
  2278. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  2279. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  2280. { NULL, 0, NULL, NULL }
  2281. };
  2282. #endif
  2283. static int ide_tape_probe(ide_drive_t *);
  2284. static ide_driver_t idetape_driver = {
  2285. .gen_driver = {
  2286. .owner = THIS_MODULE,
  2287. .name = "ide-tape",
  2288. .bus = &ide_bus_type,
  2289. },
  2290. .probe = ide_tape_probe,
  2291. .remove = ide_tape_remove,
  2292. .version = IDETAPE_VERSION,
  2293. .media = ide_tape,
  2294. .supports_dsc_overlap = 1,
  2295. .do_request = idetape_do_request,
  2296. .end_request = idetape_end_request,
  2297. .error = __ide_error,
  2298. #ifdef CONFIG_IDE_PROC_FS
  2299. .proc = idetape_proc,
  2300. #endif
  2301. };
  2302. /* Our character device supporting functions, passed to register_chrdev. */
  2303. static const struct file_operations idetape_fops = {
  2304. .owner = THIS_MODULE,
  2305. .read = idetape_chrdev_read,
  2306. .write = idetape_chrdev_write,
  2307. .ioctl = idetape_chrdev_ioctl,
  2308. .open = idetape_chrdev_open,
  2309. .release = idetape_chrdev_release,
  2310. };
  2311. static int idetape_open(struct inode *inode, struct file *filp)
  2312. {
  2313. struct gendisk *disk = inode->i_bdev->bd_disk;
  2314. struct ide_tape_obj *tape;
  2315. tape = ide_tape_get(disk);
  2316. if (!tape)
  2317. return -ENXIO;
  2318. return 0;
  2319. }
  2320. static int idetape_release(struct inode *inode, struct file *filp)
  2321. {
  2322. struct gendisk *disk = inode->i_bdev->bd_disk;
  2323. struct ide_tape_obj *tape = ide_tape_g(disk);
  2324. ide_tape_put(tape);
  2325. return 0;
  2326. }
  2327. static int idetape_ioctl(struct inode *inode, struct file *file,
  2328. unsigned int cmd, unsigned long arg)
  2329. {
  2330. struct block_device *bdev = inode->i_bdev;
  2331. struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
  2332. ide_drive_t *drive = tape->drive;
  2333. int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  2334. if (err == -EINVAL)
  2335. err = idetape_blkdev_ioctl(drive, cmd, arg);
  2336. return err;
  2337. }
  2338. static struct block_device_operations idetape_block_ops = {
  2339. .owner = THIS_MODULE,
  2340. .open = idetape_open,
  2341. .release = idetape_release,
  2342. .ioctl = idetape_ioctl,
  2343. };
  2344. static int ide_tape_probe(ide_drive_t *drive)
  2345. {
  2346. idetape_tape_t *tape;
  2347. struct gendisk *g;
  2348. int minor;
  2349. if (!strstr("ide-tape", drive->driver_req))
  2350. goto failed;
  2351. if (!drive->present)
  2352. goto failed;
  2353. if (drive->media != ide_tape)
  2354. goto failed;
  2355. if (!idetape_identify_device(drive)) {
  2356. printk(KERN_ERR "ide-tape: %s: not supported by this version of"
  2357. " the driver\n", drive->name);
  2358. goto failed;
  2359. }
  2360. tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
  2361. if (tape == NULL) {
  2362. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
  2363. drive->name);
  2364. goto failed;
  2365. }
  2366. g = alloc_disk(1 << PARTN_BITS);
  2367. if (!g)
  2368. goto out_free_tape;
  2369. ide_init_disk(g, drive);
  2370. ide_proc_register_driver(drive, &idetape_driver);
  2371. kref_init(&tape->kref);
  2372. tape->drive = drive;
  2373. tape->driver = &idetape_driver;
  2374. tape->disk = g;
  2375. g->private_data = &tape->driver;
  2376. drive->driver_data = tape;
  2377. mutex_lock(&idetape_ref_mutex);
  2378. for (minor = 0; idetape_devs[minor]; minor++)
  2379. ;
  2380. idetape_devs[minor] = tape;
  2381. mutex_unlock(&idetape_ref_mutex);
  2382. idetape_setup(drive, tape, minor);
  2383. device_create_drvdata(idetape_sysfs_class, &drive->gendev,
  2384. MKDEV(IDETAPE_MAJOR, minor), NULL,
  2385. "%s", tape->name);
  2386. device_create_drvdata(idetape_sysfs_class, &drive->gendev,
  2387. MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
  2388. "n%s", tape->name);
  2389. g->fops = &idetape_block_ops;
  2390. ide_register_region(g);
  2391. return 0;
  2392. out_free_tape:
  2393. kfree(tape);
  2394. failed:
  2395. return -ENODEV;
  2396. }
  2397. static void __exit idetape_exit(void)
  2398. {
  2399. driver_unregister(&idetape_driver.gen_driver);
  2400. class_destroy(idetape_sysfs_class);
  2401. unregister_chrdev(IDETAPE_MAJOR, "ht");
  2402. }
  2403. static int __init idetape_init(void)
  2404. {
  2405. int error = 1;
  2406. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  2407. if (IS_ERR(idetape_sysfs_class)) {
  2408. idetape_sysfs_class = NULL;
  2409. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  2410. error = -EBUSY;
  2411. goto out;
  2412. }
  2413. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  2414. printk(KERN_ERR "ide-tape: Failed to register chrdev"
  2415. " interface\n");
  2416. error = -EBUSY;
  2417. goto out_free_class;
  2418. }
  2419. error = driver_register(&idetape_driver.gen_driver);
  2420. if (error)
  2421. goto out_free_driver;
  2422. return 0;
  2423. out_free_driver:
  2424. driver_unregister(&idetape_driver.gen_driver);
  2425. out_free_class:
  2426. class_destroy(idetape_sysfs_class);
  2427. out:
  2428. return error;
  2429. }
  2430. MODULE_ALIAS("ide:*m-tape*");
  2431. module_init(idetape_init);
  2432. module_exit(idetape_exit);
  2433. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
  2434. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  2435. MODULE_LICENSE("GPL");