ide-tape.c 93 KB

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