ide-tape.c 119 KB

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