ide-tape.c 106 KB

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