ide-tape.c 109 KB

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