ide-tape.c 84 KB

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