ide-tape.c 159 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858
  1. /*
  2. * Copyright (C) 1995-1999 Gadi Oxman <gadio@netvision.net.il>
  3. * Copyright (C) 2003-2005 Bartlomiej Zolnierkiewicz
  4. *
  5. * $Header$
  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. /*
  15. * IDE ATAPI streaming tape driver.
  16. *
  17. * This driver is a part of the Linux ide driver and works in co-operation
  18. * with linux/drivers/block/ide.c.
  19. *
  20. * The driver, in co-operation with ide.c, basically traverses the
  21. * request-list for the block device interface. The character device
  22. * interface, on the other hand, creates new requests, adds them
  23. * to the request-list of the block device, and waits for their completion.
  24. *
  25. * Pipelined operation mode is now supported on both reads and writes.
  26. *
  27. * The block device major and minor numbers are determined from the
  28. * tape's relative position in the ide interfaces, as explained in ide.c.
  29. *
  30. * The character device interface consists of the following devices:
  31. *
  32. * ht0 major 37, minor 0 first IDE tape, rewind on close.
  33. * ht1 major 37, minor 1 second IDE tape, rewind on close.
  34. * ...
  35. * nht0 major 37, minor 128 first IDE tape, no rewind on close.
  36. * nht1 major 37, minor 129 second IDE tape, no rewind on close.
  37. * ...
  38. *
  39. * Run linux/scripts/MAKEDEV.ide to create the above entries.
  40. *
  41. * The general magnetic tape commands compatible interface, as defined by
  42. * include/linux/mtio.h, is accessible through the character device.
  43. *
  44. * General ide driver configuration options, such as the interrupt-unmask
  45. * flag, can be configured by issuing an ioctl to the block device interface,
  46. * as any other ide device.
  47. *
  48. * Our own ide-tape ioctl's can be issued to either the block device or
  49. * the character device interface.
  50. *
  51. * Maximal throughput with minimal bus load will usually be achieved in the
  52. * following scenario:
  53. *
  54. * 1. ide-tape is operating in the pipelined operation mode.
  55. * 2. No buffering is performed by the user backup program.
  56. *
  57. * Testing was done with a 2 GB CONNER CTMA 4000 IDE ATAPI Streaming Tape Drive.
  58. *
  59. * Ver 0.1 Nov 1 95 Pre-working code :-)
  60. * Ver 0.2 Nov 23 95 A short backup (few megabytes) and restore procedure
  61. * was successful ! (Using tar cvf ... on the block
  62. * device interface).
  63. * A longer backup resulted in major swapping, bad
  64. * overall Linux performance and eventually failed as
  65. * we received non serial read-ahead requests from the
  66. * buffer cache.
  67. * Ver 0.3 Nov 28 95 Long backups are now possible, thanks to the
  68. * character device interface. Linux's responsiveness
  69. * and performance doesn't seem to be much affected
  70. * from the background backup procedure.
  71. * Some general mtio.h magnetic tape operations are
  72. * now supported by our character device. As a result,
  73. * popular tape utilities are starting to work with
  74. * ide tapes :-)
  75. * The following configurations were tested:
  76. * 1. An IDE ATAPI TAPE shares the same interface
  77. * and irq with an IDE ATAPI CDROM.
  78. * 2. An IDE ATAPI TAPE shares the same interface
  79. * and irq with a normal IDE disk.
  80. * Both configurations seemed to work just fine !
  81. * However, to be on the safe side, it is meanwhile
  82. * recommended to give the IDE TAPE its own interface
  83. * and irq.
  84. * The one thing which needs to be done here is to
  85. * add a "request postpone" feature to ide.c,
  86. * so that we won't have to wait for the tape to finish
  87. * performing a long media access (DSC) request (such
  88. * as a rewind) before we can access the other device
  89. * on the same interface. This effect doesn't disturb
  90. * normal operation most of the time because read/write
  91. * requests are relatively fast, and once we are
  92. * performing one tape r/w request, a lot of requests
  93. * from the other device can be queued and ide.c will
  94. * service all of them after this single tape request.
  95. * Ver 1.0 Dec 11 95 Integrated into Linux 1.3.46 development tree.
  96. * On each read / write request, we now ask the drive
  97. * if we can transfer a constant number of bytes
  98. * (a parameter of the drive) only to its buffers,
  99. * without causing actual media access. If we can't,
  100. * we just wait until we can by polling the DSC bit.
  101. * This ensures that while we are not transferring
  102. * more bytes than the constant referred to above, the
  103. * interrupt latency will not become too high and
  104. * we won't cause an interrupt timeout, as happened
  105. * occasionally in the previous version.
  106. * While polling for DSC, the current request is
  107. * postponed and ide.c is free to handle requests from
  108. * the other device. This is handled transparently to
  109. * ide.c. The hwgroup locking method which was used
  110. * in the previous version was removed.
  111. * Use of new general features which are provided by
  112. * ide.c for use with atapi devices.
  113. * (Programming done by Mark Lord)
  114. * Few potential bug fixes (Again, suggested by Mark)
  115. * Single character device data transfers are now
  116. * not limited in size, as they were before.
  117. * We are asking the tape about its recommended
  118. * transfer unit and send a larger data transfer
  119. * as several transfers of the above size.
  120. * For best results, use an integral number of this
  121. * basic unit (which is shown during driver
  122. * initialization). I will soon add an ioctl to get
  123. * this important parameter.
  124. * Our data transfer buffer is allocated on startup,
  125. * rather than before each data transfer. This should
  126. * ensure that we will indeed have a data buffer.
  127. * Ver 1.1 Dec 14 95 Fixed random problems which occurred when the tape
  128. * shared an interface with another device.
  129. * (poll_for_dsc was a complete mess).
  130. * Removed some old (non-active) code which had
  131. * to do with supporting buffer cache originated
  132. * requests.
  133. * The block device interface can now be opened, so
  134. * that general ide driver features like the unmask
  135. * interrupts flag can be selected with an ioctl.
  136. * This is the only use of the block device interface.
  137. * New fast pipelined operation mode (currently only on
  138. * writes). When using the pipelined mode, the
  139. * throughput can potentially reach the maximum
  140. * tape supported throughput, regardless of the
  141. * user backup program. On my tape drive, it sometimes
  142. * boosted performance by a factor of 2. Pipelined
  143. * mode is enabled by default, but since it has a few
  144. * downfalls as well, you may want to disable it.
  145. * A short explanation of the pipelined operation mode
  146. * is available below.
  147. * Ver 1.2 Jan 1 96 Eliminated pipelined mode race condition.
  148. * Added pipeline read mode. As a result, restores
  149. * are now as fast as backups.
  150. * Optimized shared interface behavior. The new behavior
  151. * typically results in better IDE bus efficiency and
  152. * higher tape throughput.
  153. * Pre-calculation of the expected read/write request
  154. * service time, based on the tape's parameters. In
  155. * the pipelined operation mode, this allows us to
  156. * adjust our polling frequency to a much lower value,
  157. * and thus to dramatically reduce our load on Linux,
  158. * without any decrease in performance.
  159. * Implemented additional mtio.h operations.
  160. * The recommended user block size is returned by
  161. * the MTIOCGET ioctl.
  162. * Additional minor changes.
  163. * Ver 1.3 Feb 9 96 Fixed pipelined read mode bug which prevented the
  164. * use of some block sizes during a restore procedure.
  165. * The character device interface will now present a
  166. * continuous view of the media - any mix of block sizes
  167. * during a backup/restore procedure is supported. The
  168. * driver will buffer the requests internally and
  169. * convert them to the tape's recommended transfer
  170. * unit, making performance almost independent of the
  171. * chosen user block size.
  172. * Some improvements in error recovery.
  173. * By cooperating with ide-dma.c, bus mastering DMA can
  174. * now sometimes be used with IDE tape drives as well.
  175. * Bus mastering DMA has the potential to dramatically
  176. * reduce the CPU's overhead when accessing the device,
  177. * and can be enabled by using hdparm -d1 on the tape's
  178. * block device interface. For more info, read the
  179. * comments in ide-dma.c.
  180. * Ver 1.4 Mar 13 96 Fixed serialize support.
  181. * Ver 1.5 Apr 12 96 Fixed shared interface operation, broken in 1.3.85.
  182. * Fixed pipelined read mode inefficiency.
  183. * Fixed nasty null dereferencing bug.
  184. * Ver 1.6 Aug 16 96 Fixed FPU usage in the driver.
  185. * Fixed end of media bug.
  186. * Ver 1.7 Sep 10 96 Minor changes for the CONNER CTT8000-A model.
  187. * Ver 1.8 Sep 26 96 Attempt to find a better balance between good
  188. * interactive response and high system throughput.
  189. * Ver 1.9 Nov 5 96 Automatically cross encountered filemarks rather
  190. * than requiring an explicit FSF command.
  191. * Abort pending requests at end of media.
  192. * MTTELL was sometimes returning incorrect results.
  193. * Return the real block size in the MTIOCGET ioctl.
  194. * Some error recovery bug fixes.
  195. * Ver 1.10 Nov 5 96 Major reorganization.
  196. * Reduced CPU overhead a bit by eliminating internal
  197. * bounce buffers.
  198. * Added module support.
  199. * Added multiple tape drives support.
  200. * Added partition support.
  201. * Rewrote DSC handling.
  202. * Some portability fixes.
  203. * Removed ide-tape.h.
  204. * Additional minor changes.
  205. * Ver 1.11 Dec 2 96 Bug fix in previous DSC timeout handling.
  206. * Use ide_stall_queue() for DSC overlap.
  207. * Use the maximum speed rather than the current speed
  208. * to compute the request service time.
  209. * Ver 1.12 Dec 7 97 Fix random memory overwriting and/or last block data
  210. * corruption, which could occur if the total number
  211. * of bytes written to the tape was not an integral
  212. * number of tape blocks.
  213. * Add support for INTERRUPT DRQ devices.
  214. * Ver 1.13 Jan 2 98 Add "speed == 0" work-around for HP COLORADO 5GB
  215. * Ver 1.14 Dec 30 98 Partial fixes for the Sony/AIWA tape drives.
  216. * Replace cli()/sti() with hwgroup spinlocks.
  217. * Ver 1.15 Mar 25 99 Fix SMP race condition by replacing hwgroup
  218. * spinlock with private per-tape spinlock.
  219. * Ver 1.16 Sep 1 99 Add OnStream tape support.
  220. * Abort read pipeline on EOD.
  221. * Wait for the tape to become ready in case it returns
  222. * "in the process of becoming ready" on open().
  223. * Fix zero padding of the last written block in
  224. * case the tape block size is larger than PAGE_SIZE.
  225. * Decrease the default disconnection time to tn.
  226. * Ver 1.16e Oct 3 99 Minor fixes.
  227. * Ver 1.16e1 Oct 13 99 Patches by Arnold Niessen,
  228. * niessen@iae.nl / arnold.niessen@philips.com
  229. * GO-1) Undefined code in idetape_read_position
  230. * according to Gadi's email
  231. * AJN-1) Minor fix asc == 11 should be asc == 0x11
  232. * in idetape_issue_packet_command (did effect
  233. * debugging output only)
  234. * AJN-2) Added more debugging output, and
  235. * added ide-tape: where missing. I would also
  236. * like to add tape->name where possible
  237. * AJN-3) Added different debug_level's
  238. * via /proc/ide/hdc/settings
  239. * "debug_level" determines amount of debugging output;
  240. * can be changed using /proc/ide/hdx/settings
  241. * 0 : almost no debugging output
  242. * 1 : 0+output errors only
  243. * 2 : 1+output all sensekey/asc
  244. * 3 : 2+follow all chrdev related procedures
  245. * 4 : 3+follow all procedures
  246. * 5 : 4+include pc_stack rq_stack info
  247. * 6 : 5+USE_COUNT updates
  248. * AJN-4) Fixed timeout for retension in idetape_queue_pc_tail
  249. * from 5 to 10 minutes
  250. * AJN-5) Changed maximum number of blocks to skip when
  251. * reading tapes with multiple consecutive write
  252. * errors from 100 to 1000 in idetape_get_logical_blk
  253. * Proposed changes to code:
  254. * 1) output "logical_blk_num" via /proc
  255. * 2) output "current_operation" via /proc
  256. * 3) Either solve or document the fact that `mt rewind' is
  257. * required after reading from /dev/nhtx to be
  258. * able to rmmod the idetape module;
  259. * Also, sometimes an application finishes but the
  260. * device remains `busy' for some time. Same cause ?
  261. * Proposed changes to release-notes:
  262. * 4) write a simple `quickstart' section in the
  263. * release notes; I volunteer if you don't want to
  264. * 5) include a pointer to video4linux in the doc
  265. * to stimulate video applications
  266. * 6) release notes lines 331 and 362: explain what happens
  267. * if the application data rate is higher than 1100 KB/s;
  268. * similar approach to lower-than-500 kB/s ?
  269. * 7) 6.6 Comparison; wouldn't it be better to allow different
  270. * strategies for read and write ?
  271. * Wouldn't it be better to control the tape buffer
  272. * contents instead of the bandwidth ?
  273. * 8) line 536: replace will by would (if I understand
  274. * this section correctly, a hypothetical and unwanted situation
  275. * is being described)
  276. * Ver 1.16f Dec 15 99 Change place of the secondary OnStream header frames.
  277. * Ver 1.17 Nov 2000 / Jan 2001 Marcel Mol, marcel@mesa.nl
  278. * - Add idetape_onstream_mode_sense_tape_parameter_page
  279. * function to get tape capacity in frames: tape->capacity.
  280. * - Add support for DI-50 drives( or any DI- drive).
  281. * - 'workaround' for read error/blank block around block 3000.
  282. * - Implement Early warning for end of media for Onstream.
  283. * - Cosmetic code changes for readability.
  284. * - Idetape_position_tape should not use SKIP bit during
  285. * Onstream read recovery.
  286. * - Add capacity, logical_blk_num and first/last_frame_position
  287. * to /proc/ide/hd?/settings.
  288. * - Module use count was gone in the Linux 2.4 driver.
  289. * Ver 1.17a Apr 2001 Willem Riede osst@riede.org
  290. * - Get drive's actual block size from mode sense block descriptor
  291. * - Limit size of pipeline
  292. * Ver 1.17b Oct 2002 Alan Stern <stern@rowland.harvard.edu>
  293. * Changed IDETAPE_MIN_PIPELINE_STAGES to 1 and actually used
  294. * it in the code!
  295. * Actually removed aborted stages in idetape_abort_pipeline
  296. * instead of just changing the command code.
  297. * Made the transfer byte count for Request Sense equal to the
  298. * actual length of the data transfer.
  299. * Changed handling of partial data transfers: they do not
  300. * cause DMA errors.
  301. * Moved initiation of DMA transfers to the correct place.
  302. * Removed reference to unallocated memory.
  303. * Made __idetape_discard_read_pipeline return the number of
  304. * sectors skipped, not the number of stages.
  305. * Replaced errant kfree() calls with __idetape_kfree_stage().
  306. * Fixed off-by-one error in testing the pipeline length.
  307. * Fixed handling of filemarks in the read pipeline.
  308. * Small code optimization for MTBSF and MTBSFM ioctls.
  309. * Don't try to unlock the door during device close if is
  310. * already unlocked!
  311. * Cosmetic fixes to miscellaneous debugging output messages.
  312. * Set the minimum /proc/ide/hd?/settings values for "pipeline",
  313. * "pipeline_min", and "pipeline_max" to 1.
  314. *
  315. * Here are some words from the first releases of hd.c, which are quoted
  316. * in ide.c and apply here as well:
  317. *
  318. * | Special care is recommended. Have Fun!
  319. *
  320. */
  321. /*
  322. * An overview of the pipelined operation mode.
  323. *
  324. * In the pipelined write mode, we will usually just add requests to our
  325. * pipeline and return immediately, before we even start to service them. The
  326. * user program will then have enough time to prepare the next request while
  327. * we are still busy servicing previous requests. In the pipelined read mode,
  328. * the situation is similar - we add read-ahead requests into the pipeline,
  329. * before the user even requested them.
  330. *
  331. * The pipeline can be viewed as a "safety net" which will be activated when
  332. * the system load is high and prevents the user backup program from keeping up
  333. * with the current tape speed. At this point, the pipeline will get
  334. * shorter and shorter but the tape will still be streaming at the same speed.
  335. * Assuming we have enough pipeline stages, the system load will hopefully
  336. * decrease before the pipeline is completely empty, and the backup program
  337. * will be able to "catch up" and refill the pipeline again.
  338. *
  339. * When using the pipelined mode, it would be best to disable any type of
  340. * buffering done by the user program, as ide-tape already provides all the
  341. * benefits in the kernel, where it can be done in a more efficient way.
  342. * As we will usually not block the user program on a request, the most
  343. * efficient user code will then be a simple read-write-read-... cycle.
  344. * Any additional logic will usually just slow down the backup process.
  345. *
  346. * Using the pipelined mode, I get a constant over 400 KBps throughput,
  347. * which seems to be the maximum throughput supported by my tape.
  348. *
  349. * However, there are some downfalls:
  350. *
  351. * 1. We use memory (for data buffers) in proportional to the number
  352. * of pipeline stages (each stage is about 26 KB with my tape).
  353. * 2. In the pipelined write mode, we cheat and postpone error codes
  354. * to the user task. In read mode, the actual tape position
  355. * will be a bit further than the last requested block.
  356. *
  357. * Concerning (1):
  358. *
  359. * 1. We allocate stages dynamically only when we need them. When
  360. * we don't need them, we don't consume additional memory. In
  361. * case we can't allocate stages, we just manage without them
  362. * (at the expense of decreased throughput) so when Linux is
  363. * tight in memory, we will not pose additional difficulties.
  364. *
  365. * 2. The maximum number of stages (which is, in fact, the maximum
  366. * amount of memory) which we allocate is limited by the compile
  367. * time parameter IDETAPE_MAX_PIPELINE_STAGES.
  368. *
  369. * 3. The maximum number of stages is a controlled parameter - We
  370. * don't start from the user defined maximum number of stages
  371. * but from the lower IDETAPE_MIN_PIPELINE_STAGES (again, we
  372. * will not even allocate this amount of stages if the user
  373. * program can't handle the speed). We then implement a feedback
  374. * loop which checks if the pipeline is empty, and if it is, we
  375. * increase the maximum number of stages as necessary until we
  376. * reach the optimum value which just manages to keep the tape
  377. * busy with minimum allocated memory or until we reach
  378. * IDETAPE_MAX_PIPELINE_STAGES.
  379. *
  380. * Concerning (2):
  381. *
  382. * In pipelined write mode, ide-tape can not return accurate error codes
  383. * to the user program since we usually just add the request to the
  384. * pipeline without waiting for it to be serviced. In case an error
  385. * occurs, I will report it on the next user request.
  386. *
  387. * In the pipelined read mode, subsequent read requests or forward
  388. * filemark spacing will perform correctly, as we preserve all blocks
  389. * and filemarks which we encountered during our excess read-ahead.
  390. *
  391. * For accurate tape positioning and error reporting, disabling
  392. * pipelined mode might be the best option.
  393. *
  394. * You can enable/disable/tune the pipelined operation mode by adjusting
  395. * the compile time parameters below.
  396. */
  397. /*
  398. * Possible improvements.
  399. *
  400. * 1. Support for the ATAPI overlap protocol.
  401. *
  402. * In order to maximize bus throughput, we currently use the DSC
  403. * overlap method which enables ide.c to service requests from the
  404. * other device while the tape is busy executing a command. The
  405. * DSC overlap method involves polling the tape's status register
  406. * for the DSC bit, and servicing the other device while the tape
  407. * isn't ready.
  408. *
  409. * In the current QIC development standard (December 1995),
  410. * it is recommended that new tape drives will *in addition*
  411. * implement the ATAPI overlap protocol, which is used for the
  412. * same purpose - efficient use of the IDE bus, but is interrupt
  413. * driven and thus has much less CPU overhead.
  414. *
  415. * ATAPI overlap is likely to be supported in most new ATAPI
  416. * devices, including new ATAPI cdroms, and thus provides us
  417. * a method by which we can achieve higher throughput when
  418. * sharing a (fast) ATA-2 disk with any (slow) new ATAPI device.
  419. */
  420. #define IDETAPE_VERSION "1.19"
  421. #include <linux/module.h>
  422. #include <linux/types.h>
  423. #include <linux/string.h>
  424. #include <linux/kernel.h>
  425. #include <linux/delay.h>
  426. #include <linux/timer.h>
  427. #include <linux/mm.h>
  428. #include <linux/interrupt.h>
  429. #include <linux/jiffies.h>
  430. #include <linux/major.h>
  431. #include <linux/errno.h>
  432. #include <linux/genhd.h>
  433. #include <linux/slab.h>
  434. #include <linux/pci.h>
  435. #include <linux/ide.h>
  436. #include <linux/smp_lock.h>
  437. #include <linux/completion.h>
  438. #include <linux/bitops.h>
  439. #include <linux/mutex.h>
  440. #include <asm/byteorder.h>
  441. #include <asm/irq.h>
  442. #include <asm/uaccess.h>
  443. #include <asm/io.h>
  444. #include <asm/unaligned.h>
  445. /*
  446. * partition
  447. */
  448. typedef struct os_partition_s {
  449. __u8 partition_num;
  450. __u8 par_desc_ver;
  451. __u16 wrt_pass_cntr;
  452. __u32 first_frame_addr;
  453. __u32 last_frame_addr;
  454. __u32 eod_frame_addr;
  455. } os_partition_t;
  456. /*
  457. * DAT entry
  458. */
  459. typedef struct os_dat_entry_s {
  460. __u32 blk_sz;
  461. __u16 blk_cnt;
  462. __u8 flags;
  463. __u8 reserved;
  464. } os_dat_entry_t;
  465. /*
  466. * DAT
  467. */
  468. #define OS_DAT_FLAGS_DATA (0xc)
  469. #define OS_DAT_FLAGS_MARK (0x1)
  470. typedef struct os_dat_s {
  471. __u8 dat_sz;
  472. __u8 reserved1;
  473. __u8 entry_cnt;
  474. __u8 reserved3;
  475. os_dat_entry_t dat_list[16];
  476. } os_dat_t;
  477. #include <linux/mtio.h>
  478. /**************************** Tunable parameters *****************************/
  479. /*
  480. * Pipelined mode parameters.
  481. *
  482. * We try to use the minimum number of stages which is enough to
  483. * keep the tape constantly streaming. To accomplish that, we implement
  484. * a feedback loop around the maximum number of stages:
  485. *
  486. * We start from MIN maximum stages (we will not even use MIN stages
  487. * if we don't need them), increment it by RATE*(MAX-MIN)
  488. * whenever we sense that the pipeline is empty, until we reach
  489. * the optimum value or until we reach MAX.
  490. *
  491. * Setting the following parameter to 0 is illegal: the pipelined mode
  492. * cannot be disabled (calculate_speeds() divides by tape->max_stages.)
  493. */
  494. #define IDETAPE_MIN_PIPELINE_STAGES 1
  495. #define IDETAPE_MAX_PIPELINE_STAGES 400
  496. #define IDETAPE_INCREASE_STAGES_RATE 20
  497. /*
  498. * The following are used to debug the driver:
  499. *
  500. * Setting IDETAPE_DEBUG_INFO to 1 will report device capabilities.
  501. * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
  502. * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
  503. * some places.
  504. *
  505. * Setting them to 0 will restore normal operation mode:
  506. *
  507. * 1. Disable logging normal successful operations.
  508. * 2. Disable self-sanity checks.
  509. * 3. Errors will still be logged, of course.
  510. *
  511. * All the #if DEBUG code will be removed some day, when the driver
  512. * is verified to be stable enough. This will make it much more
  513. * esthetic.
  514. */
  515. #define IDETAPE_DEBUG_INFO 0
  516. #define IDETAPE_DEBUG_LOG 0
  517. #define IDETAPE_DEBUG_BUGS 1
  518. /*
  519. * After each failed packet command we issue a request sense command
  520. * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
  521. *
  522. * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
  523. */
  524. #define IDETAPE_MAX_PC_RETRIES 3
  525. /*
  526. * With each packet command, we allocate a buffer of
  527. * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
  528. * commands (Not for READ/WRITE commands).
  529. */
  530. #define IDETAPE_PC_BUFFER_SIZE 256
  531. /*
  532. * In various places in the driver, we need to allocate storage
  533. * for packet commands and requests, which will remain valid while
  534. * we leave the driver to wait for an interrupt or a timeout event.
  535. */
  536. #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
  537. /*
  538. * Some drives (for example, Seagate STT3401A Travan) require a very long
  539. * timeout, because they don't return an interrupt or clear their busy bit
  540. * until after the command completes (even retension commands).
  541. */
  542. #define IDETAPE_WAIT_CMD (900*HZ)
  543. /*
  544. * The following parameter is used to select the point in the internal
  545. * tape fifo in which we will start to refill the buffer. Decreasing
  546. * the following parameter will improve the system's latency and
  547. * interactive response, while using a high value might improve system
  548. * throughput.
  549. */
  550. #define IDETAPE_FIFO_THRESHOLD 2
  551. /*
  552. * DSC polling parameters.
  553. *
  554. * Polling for DSC (a single bit in the status register) is a very
  555. * important function in ide-tape. There are two cases in which we
  556. * poll for DSC:
  557. *
  558. * 1. Before a read/write packet command, to ensure that we
  559. * can transfer data from/to the tape's data buffers, without
  560. * causing an actual media access. In case the tape is not
  561. * ready yet, we take out our request from the device
  562. * request queue, so that ide.c will service requests from
  563. * the other device on the same interface meanwhile.
  564. *
  565. * 2. After the successful initialization of a "media access
  566. * packet command", which is a command which can take a long
  567. * time to complete (it can be several seconds or even an hour).
  568. *
  569. * Again, we postpone our request in the middle to free the bus
  570. * for the other device. The polling frequency here should be
  571. * lower than the read/write frequency since those media access
  572. * commands are slow. We start from a "fast" frequency -
  573. * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
  574. * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
  575. * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
  576. *
  577. * We also set a timeout for the timer, in case something goes wrong.
  578. * The timeout should be longer then the maximum execution time of a
  579. * tape operation.
  580. */
  581. /*
  582. * DSC timings.
  583. */
  584. #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
  585. #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
  586. #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
  587. #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
  588. #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
  589. #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
  590. #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
  591. /*************************** End of tunable parameters ***********************/
  592. /*
  593. * Read/Write error simulation
  594. */
  595. #define SIMULATE_ERRORS 0
  596. /*
  597. * For general magnetic tape device compatibility.
  598. */
  599. typedef enum {
  600. idetape_direction_none,
  601. idetape_direction_read,
  602. idetape_direction_write
  603. } idetape_chrdev_direction_t;
  604. struct idetape_bh {
  605. u32 b_size;
  606. atomic_t b_count;
  607. struct idetape_bh *b_reqnext;
  608. char *b_data;
  609. };
  610. /*
  611. * Our view of a packet command.
  612. */
  613. typedef struct idetape_packet_command_s {
  614. u8 c[12]; /* Actual packet bytes */
  615. int retries; /* On each retry, we increment retries */
  616. int error; /* Error code */
  617. int request_transfer; /* Bytes to transfer */
  618. int actually_transferred; /* Bytes actually transferred */
  619. int buffer_size; /* Size of our data buffer */
  620. struct idetape_bh *bh;
  621. char *b_data;
  622. int b_count;
  623. u8 *buffer; /* Data buffer */
  624. u8 *current_position; /* Pointer into the above buffer */
  625. ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
  626. u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
  627. unsigned long flags; /* Status/Action bit flags: long for set_bit */
  628. } idetape_pc_t;
  629. /*
  630. * Packet command flag bits.
  631. */
  632. /* Set when an error is considered normal - We won't retry */
  633. #define PC_ABORT 0
  634. /* 1 When polling for DSC on a media access command */
  635. #define PC_WAIT_FOR_DSC 1
  636. /* 1 when we prefer to use DMA if possible */
  637. #define PC_DMA_RECOMMENDED 2
  638. /* 1 while DMA in progress */
  639. #define PC_DMA_IN_PROGRESS 3
  640. /* 1 when encountered problem during DMA */
  641. #define PC_DMA_ERROR 4
  642. /* Data direction */
  643. #define PC_WRITING 5
  644. /*
  645. * Capabilities and Mechanical Status Page
  646. */
  647. typedef struct {
  648. unsigned page_code :6; /* Page code - Should be 0x2a */
  649. __u8 reserved0_6 :1;
  650. __u8 ps :1; /* parameters saveable */
  651. __u8 page_length; /* Page Length - Should be 0x12 */
  652. __u8 reserved2, reserved3;
  653. unsigned ro :1; /* Read Only Mode */
  654. unsigned reserved4_1234 :4;
  655. unsigned sprev :1; /* Supports SPACE in the reverse direction */
  656. unsigned reserved4_67 :2;
  657. unsigned reserved5_012 :3;
  658. unsigned efmt :1; /* Supports ERASE command initiated formatting */
  659. unsigned reserved5_4 :1;
  660. unsigned qfa :1; /* Supports the QFA two partition formats */
  661. unsigned reserved5_67 :2;
  662. unsigned lock :1; /* Supports locking the volume */
  663. unsigned locked :1; /* The volume is locked */
  664. unsigned prevent :1; /* The device defaults in the prevent state after power up */
  665. unsigned eject :1; /* The device can eject the volume */
  666. __u8 disconnect :1; /* The device can break request > ctl */
  667. __u8 reserved6_5 :1;
  668. unsigned ecc :1; /* Supports error correction */
  669. unsigned cmprs :1; /* Supports data compression */
  670. unsigned reserved7_0 :1;
  671. unsigned blk512 :1; /* Supports 512 bytes block size */
  672. unsigned blk1024 :1; /* Supports 1024 bytes block size */
  673. unsigned reserved7_3_6 :4;
  674. unsigned blk32768 :1; /* slowb - the device restricts the byte count for PIO */
  675. /* transfers for slow buffer memory ??? */
  676. /* Also 32768 block size in some cases */
  677. __u16 max_speed; /* Maximum speed supported in KBps */
  678. __u8 reserved10, reserved11;
  679. __u16 ctl; /* Continuous Transfer Limit in blocks */
  680. __u16 speed; /* Current Speed, in KBps */
  681. __u16 buffer_size; /* Buffer Size, in 512 bytes */
  682. __u8 reserved18, reserved19;
  683. } idetape_capabilities_page_t;
  684. /*
  685. * Block Size Page
  686. */
  687. typedef struct {
  688. unsigned page_code :6; /* Page code - Should be 0x30 */
  689. unsigned reserved1_6 :1;
  690. unsigned ps :1;
  691. __u8 page_length; /* Page Length - Should be 2 */
  692. __u8 reserved2;
  693. unsigned play32 :1;
  694. unsigned play32_5 :1;
  695. unsigned reserved2_23 :2;
  696. unsigned record32 :1;
  697. unsigned record32_5 :1;
  698. unsigned reserved2_6 :1;
  699. unsigned one :1;
  700. } idetape_block_size_page_t;
  701. /*
  702. * A pipeline stage.
  703. */
  704. typedef struct idetape_stage_s {
  705. struct request rq; /* The corresponding request */
  706. struct idetape_bh *bh; /* The data buffers */
  707. struct idetape_stage_s *next; /* Pointer to the next stage */
  708. } idetape_stage_t;
  709. /*
  710. * REQUEST SENSE packet command result - Data Format.
  711. */
  712. typedef struct {
  713. unsigned error_code :7; /* Current of deferred errors */
  714. unsigned valid :1; /* The information field conforms to QIC-157C */
  715. __u8 reserved1 :8; /* Segment Number - Reserved */
  716. unsigned sense_key :4; /* Sense Key */
  717. unsigned reserved2_4 :1; /* Reserved */
  718. unsigned ili :1; /* Incorrect Length Indicator */
  719. unsigned eom :1; /* End Of Medium */
  720. unsigned filemark :1; /* Filemark */
  721. __u32 information __attribute__ ((packed));
  722. __u8 asl; /* Additional sense length (n-7) */
  723. __u32 command_specific; /* Additional command specific information */
  724. __u8 asc; /* Additional Sense Code */
  725. __u8 ascq; /* Additional Sense Code Qualifier */
  726. __u8 replaceable_unit_code; /* Field Replaceable Unit Code */
  727. unsigned sk_specific1 :7; /* Sense Key Specific */
  728. unsigned sksv :1; /* Sense Key Specific information is valid */
  729. __u8 sk_specific2; /* Sense Key Specific */
  730. __u8 sk_specific3; /* Sense Key Specific */
  731. __u8 pad[2]; /* Padding to 20 bytes */
  732. } idetape_request_sense_result_t;
  733. /*
  734. * Most of our global data which we need to save even as we leave the
  735. * driver due to an interrupt or a timer event is stored in a variable
  736. * of type idetape_tape_t, defined below.
  737. */
  738. typedef struct ide_tape_obj {
  739. ide_drive_t *drive;
  740. ide_driver_t *driver;
  741. struct gendisk *disk;
  742. struct kref kref;
  743. /*
  744. * Since a typical character device operation requires more
  745. * than one packet command, we provide here enough memory
  746. * for the maximum of interconnected packet commands.
  747. * The packet commands are stored in the circular array pc_stack.
  748. * pc_stack_index points to the last used entry, and warps around
  749. * to the start when we get to the last array entry.
  750. *
  751. * pc points to the current processed packet command.
  752. *
  753. * failed_pc points to the last failed packet command, or contains
  754. * NULL if we do not need to retry any packet command. This is
  755. * required since an additional packet command is needed before the
  756. * retry, to get detailed information on what went wrong.
  757. */
  758. /* Current packet command */
  759. idetape_pc_t *pc;
  760. /* Last failed packet command */
  761. idetape_pc_t *failed_pc;
  762. /* Packet command stack */
  763. idetape_pc_t pc_stack[IDETAPE_PC_STACK];
  764. /* Next free packet command storage space */
  765. int pc_stack_index;
  766. struct request rq_stack[IDETAPE_PC_STACK];
  767. /* We implement a circular array */
  768. int rq_stack_index;
  769. /*
  770. * DSC polling variables.
  771. *
  772. * While polling for DSC we use postponed_rq to postpone the
  773. * current request so that ide.c will be able to service
  774. * pending requests on the other device. Note that at most
  775. * we will have only one DSC (usually data transfer) request
  776. * in the device request queue. Additional requests can be
  777. * queued in our internal pipeline, but they will be visible
  778. * to ide.c only one at a time.
  779. */
  780. struct request *postponed_rq;
  781. /* The time in which we started polling for DSC */
  782. unsigned long dsc_polling_start;
  783. /* Timer used to poll for dsc */
  784. struct timer_list dsc_timer;
  785. /* Read/Write dsc polling frequency */
  786. unsigned long best_dsc_rw_frequency;
  787. /* The current polling frequency */
  788. unsigned long dsc_polling_frequency;
  789. /* Maximum waiting time */
  790. unsigned long dsc_timeout;
  791. /*
  792. * Read position information
  793. */
  794. u8 partition;
  795. /* Current block */
  796. unsigned int first_frame_position;
  797. unsigned int last_frame_position;
  798. unsigned int blocks_in_buffer;
  799. /*
  800. * Last error information
  801. */
  802. u8 sense_key, asc, ascq;
  803. /*
  804. * Character device operation
  805. */
  806. unsigned int minor;
  807. /* device name */
  808. char name[4];
  809. /* Current character device data transfer direction */
  810. idetape_chrdev_direction_t chrdev_direction;
  811. /*
  812. * Device information
  813. */
  814. /* Usually 512 or 1024 bytes */
  815. unsigned short tape_block_size;
  816. int user_bs_factor;
  817. /* Copy of the tape's Capabilities and Mechanical Page */
  818. idetape_capabilities_page_t capabilities;
  819. /*
  820. * Active data transfer request parameters.
  821. *
  822. * At most, there is only one ide-tape originated data transfer
  823. * request in the device request queue. This allows ide.c to
  824. * easily service requests from the other device when we
  825. * postpone our active request. In the pipelined operation
  826. * mode, we use our internal pipeline structure to hold
  827. * more data requests.
  828. *
  829. * The data buffer size is chosen based on the tape's
  830. * recommendation.
  831. */
  832. /* Pointer to the request which is waiting in the device request queue */
  833. struct request *active_data_request;
  834. /* Data buffer size (chosen based on the tape's recommendation */
  835. int stage_size;
  836. idetape_stage_t *merge_stage;
  837. int merge_stage_size;
  838. struct idetape_bh *bh;
  839. char *b_data;
  840. int b_count;
  841. /*
  842. * Pipeline parameters.
  843. *
  844. * To accomplish non-pipelined mode, we simply set the following
  845. * variables to zero (or NULL, where appropriate).
  846. */
  847. /* Number of currently used stages */
  848. int nr_stages;
  849. /* Number of pending stages */
  850. int nr_pending_stages;
  851. /* We will not allocate more than this number of stages */
  852. int max_stages, min_pipeline, max_pipeline;
  853. /* The first stage which will be removed from the pipeline */
  854. idetape_stage_t *first_stage;
  855. /* The currently active stage */
  856. idetape_stage_t *active_stage;
  857. /* Will be serviced after the currently active request */
  858. idetape_stage_t *next_stage;
  859. /* New requests will be added to the pipeline here */
  860. idetape_stage_t *last_stage;
  861. /* Optional free stage which we can use */
  862. idetape_stage_t *cache_stage;
  863. int pages_per_stage;
  864. /* Wasted space in each stage */
  865. int excess_bh_size;
  866. /* Status/Action flags: long for set_bit */
  867. unsigned long flags;
  868. /* protects the ide-tape queue */
  869. spinlock_t spinlock;
  870. /*
  871. * Measures average tape speed
  872. */
  873. unsigned long avg_time;
  874. int avg_size;
  875. int avg_speed;
  876. /* last sense information */
  877. idetape_request_sense_result_t sense;
  878. char vendor_id[10];
  879. char product_id[18];
  880. char firmware_revision[6];
  881. int firmware_revision_num;
  882. /* the door is currently locked */
  883. int door_locked;
  884. /* the tape hardware is write protected */
  885. char drv_write_prot;
  886. /* the tape is write protected (hardware or opened as read-only) */
  887. char write_prot;
  888. /*
  889. * Limit the number of times a request can
  890. * be postponed, to avoid an infinite postpone
  891. * deadlock.
  892. */
  893. /* request postpone count limit */
  894. int postpone_cnt;
  895. /*
  896. * Measures number of frames:
  897. *
  898. * 1. written/read to/from the driver pipeline (pipeline_head).
  899. * 2. written/read to/from the tape buffers (idetape_bh).
  900. * 3. written/read by the tape to/from the media (tape_head).
  901. */
  902. int pipeline_head;
  903. int buffer_head;
  904. int tape_head;
  905. int last_tape_head;
  906. /*
  907. * Speed control at the tape buffers input/output
  908. */
  909. unsigned long insert_time;
  910. int insert_size;
  911. int insert_speed;
  912. int max_insert_speed;
  913. int measure_insert_time;
  914. /*
  915. * Measure tape still time, in milliseconds
  916. */
  917. unsigned long tape_still_time_begin;
  918. int tape_still_time;
  919. /*
  920. * Speed regulation negative feedback loop
  921. */
  922. int speed_control;
  923. int pipeline_head_speed;
  924. int controlled_pipeline_head_speed;
  925. int uncontrolled_pipeline_head_speed;
  926. int controlled_last_pipeline_head;
  927. int uncontrolled_last_pipeline_head;
  928. unsigned long uncontrolled_pipeline_head_time;
  929. unsigned long controlled_pipeline_head_time;
  930. int controlled_previous_pipeline_head;
  931. int uncontrolled_previous_pipeline_head;
  932. unsigned long controlled_previous_head_time;
  933. unsigned long uncontrolled_previous_head_time;
  934. int restart_speed_control_req;
  935. /*
  936. * Debug_level determines amount of debugging output;
  937. * can be changed using /proc/ide/hdx/settings
  938. * 0 : almost no debugging output
  939. * 1 : 0+output errors only
  940. * 2 : 1+output all sensekey/asc
  941. * 3 : 2+follow all chrdev related procedures
  942. * 4 : 3+follow all procedures
  943. * 5 : 4+include pc_stack rq_stack info
  944. * 6 : 5+USE_COUNT updates
  945. */
  946. int debug_level;
  947. } idetape_tape_t;
  948. static DEFINE_MUTEX(idetape_ref_mutex);
  949. static struct class *idetape_sysfs_class;
  950. #define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
  951. #define ide_tape_g(disk) \
  952. container_of((disk)->private_data, struct ide_tape_obj, driver)
  953. static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
  954. {
  955. struct ide_tape_obj *tape = NULL;
  956. mutex_lock(&idetape_ref_mutex);
  957. tape = ide_tape_g(disk);
  958. if (tape)
  959. kref_get(&tape->kref);
  960. mutex_unlock(&idetape_ref_mutex);
  961. return tape;
  962. }
  963. static void ide_tape_release(struct kref *);
  964. static void ide_tape_put(struct ide_tape_obj *tape)
  965. {
  966. mutex_lock(&idetape_ref_mutex);
  967. kref_put(&tape->kref, ide_tape_release);
  968. mutex_unlock(&idetape_ref_mutex);
  969. }
  970. /*
  971. * Tape door status
  972. */
  973. #define DOOR_UNLOCKED 0
  974. #define DOOR_LOCKED 1
  975. #define DOOR_EXPLICITLY_LOCKED 2
  976. /*
  977. * Tape flag bits values.
  978. */
  979. #define IDETAPE_IGNORE_DSC 0
  980. #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
  981. #define IDETAPE_BUSY 2 /* Device already opened */
  982. #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
  983. #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
  984. #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
  985. #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
  986. #define IDETAPE_READ_ERROR 7
  987. #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
  988. /* 0 = no tape is loaded, so we don't rewind after ejecting */
  989. #define IDETAPE_MEDIUM_PRESENT 9
  990. /*
  991. * Supported ATAPI tape drives packet commands
  992. */
  993. #define IDETAPE_TEST_UNIT_READY_CMD 0x00
  994. #define IDETAPE_REWIND_CMD 0x01
  995. #define IDETAPE_REQUEST_SENSE_CMD 0x03
  996. #define IDETAPE_READ_CMD 0x08
  997. #define IDETAPE_WRITE_CMD 0x0a
  998. #define IDETAPE_WRITE_FILEMARK_CMD 0x10
  999. #define IDETAPE_SPACE_CMD 0x11
  1000. #define IDETAPE_INQUIRY_CMD 0x12
  1001. #define IDETAPE_ERASE_CMD 0x19
  1002. #define IDETAPE_MODE_SENSE_CMD 0x1a
  1003. #define IDETAPE_MODE_SELECT_CMD 0x15
  1004. #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
  1005. #define IDETAPE_PREVENT_CMD 0x1e
  1006. #define IDETAPE_LOCATE_CMD 0x2b
  1007. #define IDETAPE_READ_POSITION_CMD 0x34
  1008. #define IDETAPE_READ_BUFFER_CMD 0x3c
  1009. #define IDETAPE_SET_SPEED_CMD 0xbb
  1010. /*
  1011. * Some defines for the READ BUFFER command
  1012. */
  1013. #define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
  1014. /*
  1015. * Some defines for the SPACE command
  1016. */
  1017. #define IDETAPE_SPACE_OVER_FILEMARK 1
  1018. #define IDETAPE_SPACE_TO_EOD 3
  1019. /*
  1020. * Some defines for the LOAD UNLOAD command
  1021. */
  1022. #define IDETAPE_LU_LOAD_MASK 1
  1023. #define IDETAPE_LU_RETENSION_MASK 2
  1024. #define IDETAPE_LU_EOT_MASK 4
  1025. /*
  1026. * Special requests for our block device strategy routine.
  1027. *
  1028. * In order to service a character device command, we add special
  1029. * requests to the tail of our block device request queue and wait
  1030. * for their completion.
  1031. */
  1032. enum {
  1033. REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
  1034. REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
  1035. REQ_IDETAPE_READ = (1 << 2),
  1036. REQ_IDETAPE_WRITE = (1 << 3),
  1037. REQ_IDETAPE_READ_BUFFER = (1 << 4),
  1038. };
  1039. /*
  1040. * Error codes which are returned in rq->errors to the higher part
  1041. * of the driver.
  1042. */
  1043. #define IDETAPE_ERROR_GENERAL 101
  1044. #define IDETAPE_ERROR_FILEMARK 102
  1045. #define IDETAPE_ERROR_EOD 103
  1046. /*
  1047. * The following is used to format the general configuration word of
  1048. * the ATAPI IDENTIFY DEVICE command.
  1049. */
  1050. struct idetape_id_gcw {
  1051. unsigned packet_size :2; /* Packet Size */
  1052. unsigned reserved234 :3; /* Reserved */
  1053. unsigned drq_type :2; /* Command packet DRQ type */
  1054. unsigned removable :1; /* Removable media */
  1055. unsigned device_type :5; /* Device type */
  1056. unsigned reserved13 :1; /* Reserved */
  1057. unsigned protocol :2; /* Protocol type */
  1058. };
  1059. /*
  1060. * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
  1061. */
  1062. typedef struct {
  1063. unsigned device_type :5; /* Peripheral Device Type */
  1064. unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
  1065. unsigned reserved1_6t0 :7; /* Reserved */
  1066. unsigned rmb :1; /* Removable Medium Bit */
  1067. unsigned ansi_version :3; /* ANSI Version */
  1068. unsigned ecma_version :3; /* ECMA Version */
  1069. unsigned iso_version :2; /* ISO Version */
  1070. unsigned response_format :4; /* Response Data Format */
  1071. unsigned reserved3_45 :2; /* Reserved */
  1072. unsigned reserved3_6 :1; /* TrmIOP - Reserved */
  1073. unsigned reserved3_7 :1; /* AENC - Reserved */
  1074. __u8 additional_length; /* Additional Length (total_length-4) */
  1075. __u8 rsv5, rsv6, rsv7; /* Reserved */
  1076. __u8 vendor_id[8]; /* Vendor Identification */
  1077. __u8 product_id[16]; /* Product Identification */
  1078. __u8 revision_level[4]; /* Revision Level */
  1079. __u8 vendor_specific[20]; /* Vendor Specific - Optional */
  1080. __u8 reserved56t95[40]; /* Reserved - Optional */
  1081. /* Additional information may be returned */
  1082. } idetape_inquiry_result_t;
  1083. /*
  1084. * READ POSITION packet command - Data Format (From Table 6-57)
  1085. */
  1086. typedef struct {
  1087. unsigned reserved0_10 :2; /* Reserved */
  1088. unsigned bpu :1; /* Block Position Unknown */
  1089. unsigned reserved0_543 :3; /* Reserved */
  1090. unsigned eop :1; /* End Of Partition */
  1091. unsigned bop :1; /* Beginning Of Partition */
  1092. u8 partition; /* Partition Number */
  1093. u8 reserved2, reserved3; /* Reserved */
  1094. u32 first_block; /* First Block Location */
  1095. u32 last_block; /* Last Block Location (Optional) */
  1096. u8 reserved12; /* Reserved */
  1097. u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
  1098. u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
  1099. } idetape_read_position_result_t;
  1100. /*
  1101. * Follows structures which are related to the SELECT SENSE / MODE SENSE
  1102. * packet commands. Those packet commands are still not supported
  1103. * by ide-tape.
  1104. */
  1105. #define IDETAPE_BLOCK_DESCRIPTOR 0
  1106. #define IDETAPE_CAPABILITIES_PAGE 0x2a
  1107. #define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */
  1108. #define IDETAPE_BLOCK_SIZE_PAGE 0x30
  1109. #define IDETAPE_BUFFER_FILLING_PAGE 0x33
  1110. /*
  1111. * Mode Parameter Header for the MODE SENSE packet command
  1112. */
  1113. typedef struct {
  1114. __u8 mode_data_length; /* Length of the following data transfer */
  1115. __u8 medium_type; /* Medium Type */
  1116. __u8 dsp; /* Device Specific Parameter */
  1117. __u8 bdl; /* Block Descriptor Length */
  1118. #if 0
  1119. /* data transfer page */
  1120. __u8 page_code :6;
  1121. __u8 reserved0_6 :1;
  1122. __u8 ps :1; /* parameters saveable */
  1123. __u8 page_length; /* page Length == 0x02 */
  1124. __u8 reserved2;
  1125. __u8 read32k :1; /* 32k blk size (data only) */
  1126. __u8 read32k5 :1; /* 32.5k blk size (data&AUX) */
  1127. __u8 reserved3_23 :2;
  1128. __u8 write32k :1; /* 32k blk size (data only) */
  1129. __u8 write32k5 :1; /* 32.5k blk size (data&AUX) */
  1130. __u8 reserved3_6 :1;
  1131. __u8 streaming :1; /* streaming mode enable */
  1132. #endif
  1133. } idetape_mode_parameter_header_t;
  1134. /*
  1135. * Mode Parameter Block Descriptor the MODE SENSE packet command
  1136. *
  1137. * Support for block descriptors is optional.
  1138. */
  1139. typedef struct {
  1140. __u8 density_code; /* Medium density code */
  1141. __u8 blocks[3]; /* Number of blocks */
  1142. __u8 reserved4; /* Reserved */
  1143. __u8 length[3]; /* Block Length */
  1144. } idetape_parameter_block_descriptor_t;
  1145. /*
  1146. * The Data Compression Page, as returned by the MODE SENSE packet command.
  1147. */
  1148. typedef struct {
  1149. unsigned page_code :6; /* Page Code - Should be 0xf */
  1150. unsigned reserved0 :1; /* Reserved */
  1151. unsigned ps :1;
  1152. __u8 page_length; /* Page Length - Should be 14 */
  1153. unsigned reserved2 :6; /* Reserved */
  1154. unsigned dcc :1; /* Data Compression Capable */
  1155. unsigned dce :1; /* Data Compression Enable */
  1156. unsigned reserved3 :5; /* Reserved */
  1157. unsigned red :2; /* Report Exception on Decompression */
  1158. unsigned dde :1; /* Data Decompression Enable */
  1159. __u32 ca; /* Compression Algorithm */
  1160. __u32 da; /* Decompression Algorithm */
  1161. __u8 reserved[4]; /* Reserved */
  1162. } idetape_data_compression_page_t;
  1163. /*
  1164. * The Medium Partition Page, as returned by the MODE SENSE packet command.
  1165. */
  1166. typedef struct {
  1167. unsigned page_code :6; /* Page Code - Should be 0x11 */
  1168. unsigned reserved1_6 :1; /* Reserved */
  1169. unsigned ps :1;
  1170. __u8 page_length; /* Page Length - Should be 6 */
  1171. __u8 map; /* Maximum Additional Partitions - Should be 0 */
  1172. __u8 apd; /* Additional Partitions Defined - Should be 0 */
  1173. unsigned reserved4_012 :3; /* Reserved */
  1174. unsigned psum :2; /* Should be 0 */
  1175. unsigned idp :1; /* Should be 0 */
  1176. unsigned sdp :1; /* Should be 0 */
  1177. unsigned fdp :1; /* Fixed Data Partitions */
  1178. __u8 mfr; /* Medium Format Recognition */
  1179. __u8 reserved[2]; /* Reserved */
  1180. } idetape_medium_partition_page_t;
  1181. /*
  1182. * Run time configurable parameters.
  1183. */
  1184. typedef struct {
  1185. int dsc_rw_frequency;
  1186. int dsc_media_access_frequency;
  1187. int nr_stages;
  1188. } idetape_config_t;
  1189. /*
  1190. * The variables below are used for the character device interface.
  1191. * Additional state variables are defined in our ide_drive_t structure.
  1192. */
  1193. static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
  1194. #define ide_tape_f(file) ((file)->private_data)
  1195. static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
  1196. {
  1197. struct ide_tape_obj *tape = NULL;
  1198. mutex_lock(&idetape_ref_mutex);
  1199. tape = idetape_devs[i];
  1200. if (tape)
  1201. kref_get(&tape->kref);
  1202. mutex_unlock(&idetape_ref_mutex);
  1203. return tape;
  1204. }
  1205. /*
  1206. * Function declarations
  1207. *
  1208. */
  1209. static int idetape_chrdev_release (struct inode *inode, struct file *filp);
  1210. static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
  1211. /*
  1212. * Too bad. The drive wants to send us data which we are not ready to accept.
  1213. * Just throw it away.
  1214. */
  1215. static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
  1216. {
  1217. while (bcount--)
  1218. (void) HWIF(drive)->INB(IDE_DATA_REG);
  1219. }
  1220. static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
  1221. {
  1222. struct idetape_bh *bh = pc->bh;
  1223. int count;
  1224. while (bcount) {
  1225. #if IDETAPE_DEBUG_BUGS
  1226. if (bh == NULL) {
  1227. printk(KERN_ERR "ide-tape: bh == NULL in "
  1228. "idetape_input_buffers\n");
  1229. idetape_discard_data(drive, bcount);
  1230. return;
  1231. }
  1232. #endif /* IDETAPE_DEBUG_BUGS */
  1233. count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
  1234. HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
  1235. bcount -= count;
  1236. atomic_add(count, &bh->b_count);
  1237. if (atomic_read(&bh->b_count) == bh->b_size) {
  1238. bh = bh->b_reqnext;
  1239. if (bh)
  1240. atomic_set(&bh->b_count, 0);
  1241. }
  1242. }
  1243. pc->bh = bh;
  1244. }
  1245. static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
  1246. {
  1247. struct idetape_bh *bh = pc->bh;
  1248. int count;
  1249. while (bcount) {
  1250. #if IDETAPE_DEBUG_BUGS
  1251. if (bh == NULL) {
  1252. printk(KERN_ERR "ide-tape: bh == NULL in "
  1253. "idetape_output_buffers\n");
  1254. return;
  1255. }
  1256. #endif /* IDETAPE_DEBUG_BUGS */
  1257. count = min((unsigned int)pc->b_count, (unsigned int)bcount);
  1258. HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
  1259. bcount -= count;
  1260. pc->b_data += count;
  1261. pc->b_count -= count;
  1262. if (!pc->b_count) {
  1263. pc->bh = bh = bh->b_reqnext;
  1264. if (bh) {
  1265. pc->b_data = bh->b_data;
  1266. pc->b_count = atomic_read(&bh->b_count);
  1267. }
  1268. }
  1269. }
  1270. }
  1271. static void idetape_update_buffers (idetape_pc_t *pc)
  1272. {
  1273. struct idetape_bh *bh = pc->bh;
  1274. int count;
  1275. unsigned int bcount = pc->actually_transferred;
  1276. if (test_bit(PC_WRITING, &pc->flags))
  1277. return;
  1278. while (bcount) {
  1279. #if IDETAPE_DEBUG_BUGS
  1280. if (bh == NULL) {
  1281. printk(KERN_ERR "ide-tape: bh == NULL in "
  1282. "idetape_update_buffers\n");
  1283. return;
  1284. }
  1285. #endif /* IDETAPE_DEBUG_BUGS */
  1286. count = min((unsigned int)bh->b_size, (unsigned int)bcount);
  1287. atomic_set(&bh->b_count, count);
  1288. if (atomic_read(&bh->b_count) == bh->b_size)
  1289. bh = bh->b_reqnext;
  1290. bcount -= count;
  1291. }
  1292. pc->bh = bh;
  1293. }
  1294. /*
  1295. * idetape_next_pc_storage returns a pointer to a place in which we can
  1296. * safely store a packet command, even though we intend to leave the
  1297. * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
  1298. * commands is allocated at initialization time.
  1299. */
  1300. static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
  1301. {
  1302. idetape_tape_t *tape = drive->driver_data;
  1303. #if IDETAPE_DEBUG_LOG
  1304. if (tape->debug_level >= 5)
  1305. printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
  1306. tape->pc_stack_index);
  1307. #endif /* IDETAPE_DEBUG_LOG */
  1308. if (tape->pc_stack_index == IDETAPE_PC_STACK)
  1309. tape->pc_stack_index=0;
  1310. return (&tape->pc_stack[tape->pc_stack_index++]);
  1311. }
  1312. /*
  1313. * idetape_next_rq_storage is used along with idetape_next_pc_storage.
  1314. * Since we queue packet commands in the request queue, we need to
  1315. * allocate a request, along with the allocation of a packet command.
  1316. */
  1317. /**************************************************************
  1318. * *
  1319. * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
  1320. * followed later on by kfree(). -ml *
  1321. * *
  1322. **************************************************************/
  1323. static struct request *idetape_next_rq_storage (ide_drive_t *drive)
  1324. {
  1325. idetape_tape_t *tape = drive->driver_data;
  1326. #if IDETAPE_DEBUG_LOG
  1327. if (tape->debug_level >= 5)
  1328. printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
  1329. tape->rq_stack_index);
  1330. #endif /* IDETAPE_DEBUG_LOG */
  1331. if (tape->rq_stack_index == IDETAPE_PC_STACK)
  1332. tape->rq_stack_index=0;
  1333. return (&tape->rq_stack[tape->rq_stack_index++]);
  1334. }
  1335. /*
  1336. * idetape_init_pc initializes a packet command.
  1337. */
  1338. static void idetape_init_pc (idetape_pc_t *pc)
  1339. {
  1340. memset(pc->c, 0, 12);
  1341. pc->retries = 0;
  1342. pc->flags = 0;
  1343. pc->request_transfer = 0;
  1344. pc->buffer = pc->pc_buffer;
  1345. pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
  1346. pc->bh = NULL;
  1347. pc->b_data = NULL;
  1348. }
  1349. /*
  1350. * idetape_analyze_error is called on each failed packet command retry
  1351. * to analyze the request sense. We currently do not utilize this
  1352. * information.
  1353. */
  1354. static void idetape_analyze_error (ide_drive_t *drive, idetape_request_sense_result_t *result)
  1355. {
  1356. idetape_tape_t *tape = drive->driver_data;
  1357. idetape_pc_t *pc = tape->failed_pc;
  1358. tape->sense = *result;
  1359. tape->sense_key = result->sense_key;
  1360. tape->asc = result->asc;
  1361. tape->ascq = result->ascq;
  1362. #if IDETAPE_DEBUG_LOG
  1363. /*
  1364. * Without debugging, we only log an error if we decided to
  1365. * give up retrying.
  1366. */
  1367. if (tape->debug_level >= 1)
  1368. printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
  1369. "asc = %x, ascq = %x\n",
  1370. pc->c[0], result->sense_key,
  1371. result->asc, result->ascq);
  1372. #endif /* IDETAPE_DEBUG_LOG */
  1373. /*
  1374. * Correct pc->actually_transferred by asking the tape.
  1375. */
  1376. if (test_bit(PC_DMA_ERROR, &pc->flags)) {
  1377. pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl(get_unaligned(&result->information));
  1378. idetape_update_buffers(pc);
  1379. }
  1380. /*
  1381. * If error was the result of a zero-length read or write command,
  1382. * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
  1383. * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
  1384. */
  1385. if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
  1386. && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { /* length==0 */
  1387. if (result->sense_key == 5) {
  1388. /* don't report an error, everything's ok */
  1389. pc->error = 0;
  1390. /* don't retry read/write */
  1391. set_bit(PC_ABORT, &pc->flags);
  1392. }
  1393. }
  1394. if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
  1395. pc->error = IDETAPE_ERROR_FILEMARK;
  1396. set_bit(PC_ABORT, &pc->flags);
  1397. }
  1398. if (pc->c[0] == IDETAPE_WRITE_CMD) {
  1399. if (result->eom ||
  1400. (result->sense_key == 0xd && result->asc == 0x0 &&
  1401. result->ascq == 0x2)) {
  1402. pc->error = IDETAPE_ERROR_EOD;
  1403. set_bit(PC_ABORT, &pc->flags);
  1404. }
  1405. }
  1406. if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
  1407. if (result->sense_key == 8) {
  1408. pc->error = IDETAPE_ERROR_EOD;
  1409. set_bit(PC_ABORT, &pc->flags);
  1410. }
  1411. if (!test_bit(PC_ABORT, &pc->flags) &&
  1412. pc->actually_transferred)
  1413. pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
  1414. }
  1415. }
  1416. /*
  1417. * idetape_active_next_stage will declare the next stage as "active".
  1418. */
  1419. static void idetape_active_next_stage (ide_drive_t *drive)
  1420. {
  1421. idetape_tape_t *tape = drive->driver_data;
  1422. idetape_stage_t *stage = tape->next_stage;
  1423. struct request *rq = &stage->rq;
  1424. #if IDETAPE_DEBUG_LOG
  1425. if (tape->debug_level >= 4)
  1426. printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
  1427. #endif /* IDETAPE_DEBUG_LOG */
  1428. #if IDETAPE_DEBUG_BUGS
  1429. if (stage == NULL) {
  1430. printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
  1431. return;
  1432. }
  1433. #endif /* IDETAPE_DEBUG_BUGS */
  1434. rq->rq_disk = tape->disk;
  1435. rq->buffer = NULL;
  1436. rq->special = (void *)stage->bh;
  1437. tape->active_data_request = rq;
  1438. tape->active_stage = stage;
  1439. tape->next_stage = stage->next;
  1440. }
  1441. /*
  1442. * idetape_increase_max_pipeline_stages is a part of the feedback
  1443. * loop which tries to find the optimum number of stages. In the
  1444. * feedback loop, we are starting from a minimum maximum number of
  1445. * stages, and if we sense that the pipeline is empty, we try to
  1446. * increase it, until we reach the user compile time memory limit.
  1447. */
  1448. static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
  1449. {
  1450. idetape_tape_t *tape = drive->driver_data;
  1451. int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
  1452. #if IDETAPE_DEBUG_LOG
  1453. if (tape->debug_level >= 4)
  1454. printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
  1455. #endif /* IDETAPE_DEBUG_LOG */
  1456. tape->max_stages += max(increase, 1);
  1457. tape->max_stages = max(tape->max_stages, tape->min_pipeline);
  1458. tape->max_stages = min(tape->max_stages, tape->max_pipeline);
  1459. }
  1460. /*
  1461. * idetape_kfree_stage calls kfree to completely free a stage, along with
  1462. * its related buffers.
  1463. */
  1464. static void __idetape_kfree_stage (idetape_stage_t *stage)
  1465. {
  1466. struct idetape_bh *prev_bh, *bh = stage->bh;
  1467. int size;
  1468. while (bh != NULL) {
  1469. if (bh->b_data != NULL) {
  1470. size = (int) bh->b_size;
  1471. while (size > 0) {
  1472. free_page((unsigned long) bh->b_data);
  1473. size -= PAGE_SIZE;
  1474. bh->b_data += PAGE_SIZE;
  1475. }
  1476. }
  1477. prev_bh = bh;
  1478. bh = bh->b_reqnext;
  1479. kfree(prev_bh);
  1480. }
  1481. kfree(stage);
  1482. }
  1483. static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
  1484. {
  1485. __idetape_kfree_stage(stage);
  1486. }
  1487. /*
  1488. * idetape_remove_stage_head removes tape->first_stage from the pipeline.
  1489. * The caller should avoid race conditions.
  1490. */
  1491. static void idetape_remove_stage_head (ide_drive_t *drive)
  1492. {
  1493. idetape_tape_t *tape = drive->driver_data;
  1494. idetape_stage_t *stage;
  1495. #if IDETAPE_DEBUG_LOG
  1496. if (tape->debug_level >= 4)
  1497. printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
  1498. #endif /* IDETAPE_DEBUG_LOG */
  1499. #if IDETAPE_DEBUG_BUGS
  1500. if (tape->first_stage == NULL) {
  1501. printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
  1502. return;
  1503. }
  1504. if (tape->active_stage == tape->first_stage) {
  1505. printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
  1506. return;
  1507. }
  1508. #endif /* IDETAPE_DEBUG_BUGS */
  1509. stage = tape->first_stage;
  1510. tape->first_stage = stage->next;
  1511. idetape_kfree_stage(tape, stage);
  1512. tape->nr_stages--;
  1513. if (tape->first_stage == NULL) {
  1514. tape->last_stage = NULL;
  1515. #if IDETAPE_DEBUG_BUGS
  1516. if (tape->next_stage != NULL)
  1517. printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
  1518. if (tape->nr_stages)
  1519. printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
  1520. #endif /* IDETAPE_DEBUG_BUGS */
  1521. }
  1522. }
  1523. /*
  1524. * This will free all the pipeline stages starting from new_last_stage->next
  1525. * to the end of the list, and point tape->last_stage to new_last_stage.
  1526. */
  1527. static void idetape_abort_pipeline(ide_drive_t *drive,
  1528. idetape_stage_t *new_last_stage)
  1529. {
  1530. idetape_tape_t *tape = drive->driver_data;
  1531. idetape_stage_t *stage = new_last_stage->next;
  1532. idetape_stage_t *nstage;
  1533. #if IDETAPE_DEBUG_LOG
  1534. if (tape->debug_level >= 4)
  1535. printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
  1536. #endif
  1537. while (stage) {
  1538. nstage = stage->next;
  1539. idetape_kfree_stage(tape, stage);
  1540. --tape->nr_stages;
  1541. --tape->nr_pending_stages;
  1542. stage = nstage;
  1543. }
  1544. if (new_last_stage)
  1545. new_last_stage->next = NULL;
  1546. tape->last_stage = new_last_stage;
  1547. tape->next_stage = NULL;
  1548. }
  1549. /*
  1550. * idetape_end_request is used to finish servicing a request, and to
  1551. * insert a pending pipeline request into the main device queue.
  1552. */
  1553. static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
  1554. {
  1555. struct request *rq = HWGROUP(drive)->rq;
  1556. idetape_tape_t *tape = drive->driver_data;
  1557. unsigned long flags;
  1558. int error;
  1559. int remove_stage = 0;
  1560. idetape_stage_t *active_stage;
  1561. #if IDETAPE_DEBUG_LOG
  1562. if (tape->debug_level >= 4)
  1563. printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
  1564. #endif /* IDETAPE_DEBUG_LOG */
  1565. switch (uptodate) {
  1566. case 0: error = IDETAPE_ERROR_GENERAL; break;
  1567. case 1: error = 0; break;
  1568. default: error = uptodate;
  1569. }
  1570. rq->errors = error;
  1571. if (error)
  1572. tape->failed_pc = NULL;
  1573. if (!blk_special_request(rq)) {
  1574. ide_end_request(drive, uptodate, nr_sects);
  1575. return 0;
  1576. }
  1577. spin_lock_irqsave(&tape->spinlock, flags);
  1578. /* The request was a pipelined data transfer request */
  1579. if (tape->active_data_request == rq) {
  1580. active_stage = tape->active_stage;
  1581. tape->active_stage = NULL;
  1582. tape->active_data_request = NULL;
  1583. tape->nr_pending_stages--;
  1584. if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
  1585. remove_stage = 1;
  1586. if (error) {
  1587. set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  1588. if (error == IDETAPE_ERROR_EOD)
  1589. idetape_abort_pipeline(drive, active_stage);
  1590. }
  1591. } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
  1592. if (error == IDETAPE_ERROR_EOD) {
  1593. set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  1594. idetape_abort_pipeline(drive, active_stage);
  1595. }
  1596. }
  1597. if (tape->next_stage != NULL) {
  1598. idetape_active_next_stage(drive);
  1599. /*
  1600. * Insert the next request into the request queue.
  1601. */
  1602. (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
  1603. } else if (!error) {
  1604. idetape_increase_max_pipeline_stages(drive);
  1605. }
  1606. }
  1607. ide_end_drive_cmd(drive, 0, 0);
  1608. // blkdev_dequeue_request(rq);
  1609. // drive->rq = NULL;
  1610. // end_that_request_last(rq);
  1611. if (remove_stage)
  1612. idetape_remove_stage_head(drive);
  1613. if (tape->active_data_request == NULL)
  1614. clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  1615. spin_unlock_irqrestore(&tape->spinlock, flags);
  1616. return 0;
  1617. }
  1618. static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
  1619. {
  1620. idetape_tape_t *tape = drive->driver_data;
  1621. #if IDETAPE_DEBUG_LOG
  1622. if (tape->debug_level >= 4)
  1623. printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
  1624. #endif /* IDETAPE_DEBUG_LOG */
  1625. if (!tape->pc->error) {
  1626. idetape_analyze_error(drive, (idetape_request_sense_result_t *) tape->pc->buffer);
  1627. idetape_end_request(drive, 1, 0);
  1628. } else {
  1629. printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
  1630. idetape_end_request(drive, 0, 0);
  1631. }
  1632. return ide_stopped;
  1633. }
  1634. static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
  1635. {
  1636. idetape_init_pc(pc);
  1637. pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
  1638. pc->c[4] = 20;
  1639. pc->request_transfer = 20;
  1640. pc->callback = &idetape_request_sense_callback;
  1641. }
  1642. static void idetape_init_rq(struct request *rq, u8 cmd)
  1643. {
  1644. memset(rq, 0, sizeof(*rq));
  1645. rq->cmd_type = REQ_TYPE_SPECIAL;
  1646. rq->cmd[0] = cmd;
  1647. }
  1648. /*
  1649. * idetape_queue_pc_head generates a new packet command request in front
  1650. * of the request queue, before the current request, so that it will be
  1651. * processed immediately, on the next pass through the driver.
  1652. *
  1653. * idetape_queue_pc_head is called from the request handling part of
  1654. * the driver (the "bottom" part). Safe storage for the request should
  1655. * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
  1656. * before calling idetape_queue_pc_head.
  1657. *
  1658. * Memory for those requests is pre-allocated at initialization time, and
  1659. * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
  1660. * space for the maximum possible number of inter-dependent packet commands.
  1661. *
  1662. * The higher level of the driver - The ioctl handler and the character
  1663. * device handling functions should queue request to the lower level part
  1664. * and wait for their completion using idetape_queue_pc_tail or
  1665. * idetape_queue_rw_tail.
  1666. */
  1667. static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
  1668. {
  1669. struct ide_tape_obj *tape = drive->driver_data;
  1670. idetape_init_rq(rq, REQ_IDETAPE_PC1);
  1671. rq->buffer = (char *) pc;
  1672. rq->rq_disk = tape->disk;
  1673. (void) ide_do_drive_cmd(drive, rq, ide_preempt);
  1674. }
  1675. /*
  1676. * idetape_retry_pc is called when an error was detected during the
  1677. * last packet command. We queue a request sense packet command in
  1678. * the head of the request list.
  1679. */
  1680. static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
  1681. {
  1682. idetape_tape_t *tape = drive->driver_data;
  1683. idetape_pc_t *pc;
  1684. struct request *rq;
  1685. (void)drive->hwif->INB(IDE_ERROR_REG);
  1686. pc = idetape_next_pc_storage(drive);
  1687. rq = idetape_next_rq_storage(drive);
  1688. idetape_create_request_sense_cmd(pc);
  1689. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  1690. idetape_queue_pc_head(drive, pc, rq);
  1691. return ide_stopped;
  1692. }
  1693. /*
  1694. * idetape_postpone_request postpones the current request so that
  1695. * ide.c will be able to service requests from another device on
  1696. * the same hwgroup while we are polling for DSC.
  1697. */
  1698. static void idetape_postpone_request (ide_drive_t *drive)
  1699. {
  1700. idetape_tape_t *tape = drive->driver_data;
  1701. #if IDETAPE_DEBUG_LOG
  1702. if (tape->debug_level >= 4)
  1703. printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
  1704. #endif
  1705. tape->postponed_rq = HWGROUP(drive)->rq;
  1706. ide_stall_queue(drive, tape->dsc_polling_frequency);
  1707. }
  1708. /*
  1709. * idetape_pc_intr is the usual interrupt handler which will be called
  1710. * during a packet command. We will transfer some of the data (as
  1711. * requested by the drive) and will re-point interrupt handler to us.
  1712. * When data transfer is finished, we will act according to the
  1713. * algorithm described before idetape_issue_packet_command.
  1714. *
  1715. */
  1716. static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
  1717. {
  1718. ide_hwif_t *hwif = drive->hwif;
  1719. idetape_tape_t *tape = drive->driver_data;
  1720. idetape_pc_t *pc = tape->pc;
  1721. unsigned int temp;
  1722. #if SIMULATE_ERRORS
  1723. static int error_sim_count = 0;
  1724. #endif
  1725. u16 bcount;
  1726. u8 stat, ireason;
  1727. #if IDETAPE_DEBUG_LOG
  1728. if (tape->debug_level >= 4)
  1729. printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
  1730. "interrupt handler\n");
  1731. #endif /* IDETAPE_DEBUG_LOG */
  1732. /* Clear the interrupt */
  1733. stat = hwif->INB(IDE_STATUS_REG);
  1734. if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  1735. if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
  1736. /*
  1737. * A DMA error is sometimes expected. For example,
  1738. * if the tape is crossing a filemark during a
  1739. * READ command, it will issue an irq and position
  1740. * itself before the filemark, so that only a partial
  1741. * data transfer will occur (which causes the DMA
  1742. * error). In that case, we will later ask the tape
  1743. * how much bytes of the original request were
  1744. * actually transferred (we can't receive that
  1745. * information from the DMA engine on most chipsets).
  1746. */
  1747. /*
  1748. * On the contrary, a DMA error is never expected;
  1749. * it usually indicates a hardware error or abort.
  1750. * If the tape crosses a filemark during a READ
  1751. * command, it will issue an irq and position itself
  1752. * after the filemark (not before). Only a partial
  1753. * data transfer will occur, but no DMA error.
  1754. * (AS, 19 Apr 2001)
  1755. */
  1756. set_bit(PC_DMA_ERROR, &pc->flags);
  1757. } else {
  1758. pc->actually_transferred = pc->request_transfer;
  1759. idetape_update_buffers(pc);
  1760. }
  1761. #if IDETAPE_DEBUG_LOG
  1762. if (tape->debug_level >= 4)
  1763. printk(KERN_INFO "ide-tape: DMA finished\n");
  1764. #endif /* IDETAPE_DEBUG_LOG */
  1765. }
  1766. /* No more interrupts */
  1767. if ((stat & DRQ_STAT) == 0) {
  1768. #if IDETAPE_DEBUG_LOG
  1769. if (tape->debug_level >= 2)
  1770. printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
  1771. #endif /* IDETAPE_DEBUG_LOG */
  1772. clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  1773. local_irq_enable();
  1774. #if SIMULATE_ERRORS
  1775. if ((pc->c[0] == IDETAPE_WRITE_CMD ||
  1776. pc->c[0] == IDETAPE_READ_CMD) &&
  1777. (++error_sim_count % 100) == 0) {
  1778. printk(KERN_INFO "ide-tape: %s: simulating error\n",
  1779. tape->name);
  1780. stat |= ERR_STAT;
  1781. }
  1782. #endif
  1783. if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
  1784. stat &= ~ERR_STAT;
  1785. if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
  1786. /* Error detected */
  1787. #if IDETAPE_DEBUG_LOG
  1788. if (tape->debug_level >= 1)
  1789. printk(KERN_INFO "ide-tape: %s: I/O error\n",
  1790. tape->name);
  1791. #endif /* IDETAPE_DEBUG_LOG */
  1792. if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  1793. printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
  1794. return ide_do_reset(drive);
  1795. }
  1796. #if IDETAPE_DEBUG_LOG
  1797. if (tape->debug_level >= 1)
  1798. printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
  1799. #endif
  1800. /* Retry operation */
  1801. return idetape_retry_pc(drive);
  1802. }
  1803. pc->error = 0;
  1804. if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
  1805. (stat & SEEK_STAT) == 0) {
  1806. /* Media access command */
  1807. tape->dsc_polling_start = jiffies;
  1808. tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
  1809. tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
  1810. /* Allow ide.c to handle other requests */
  1811. idetape_postpone_request(drive);
  1812. return ide_stopped;
  1813. }
  1814. if (tape->failed_pc == pc)
  1815. tape->failed_pc = NULL;
  1816. /* Command finished - Call the callback function */
  1817. return pc->callback(drive);
  1818. }
  1819. if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  1820. printk(KERN_ERR "ide-tape: The tape wants to issue more "
  1821. "interrupts in DMA mode\n");
  1822. printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
  1823. ide_dma_off(drive);
  1824. return ide_do_reset(drive);
  1825. }
  1826. /* Get the number of bytes to transfer on this interrupt. */
  1827. bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
  1828. hwif->INB(IDE_BCOUNTL_REG);
  1829. ireason = hwif->INB(IDE_IREASON_REG);
  1830. if (ireason & CD) {
  1831. printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
  1832. return ide_do_reset(drive);
  1833. }
  1834. if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
  1835. /* Hopefully, we will never get here */
  1836. printk(KERN_ERR "ide-tape: We wanted to %s, ",
  1837. (ireason & IO) ? "Write" : "Read");
  1838. printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
  1839. (ireason & IO) ? "Read" : "Write");
  1840. return ide_do_reset(drive);
  1841. }
  1842. if (!test_bit(PC_WRITING, &pc->flags)) {
  1843. /* Reading - Check that we have enough space */
  1844. temp = pc->actually_transferred + bcount;
  1845. if (temp > pc->request_transfer) {
  1846. if (temp > pc->buffer_size) {
  1847. printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
  1848. idetape_discard_data(drive, bcount);
  1849. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  1850. return ide_started;
  1851. }
  1852. #if IDETAPE_DEBUG_LOG
  1853. if (tape->debug_level >= 2)
  1854. printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
  1855. #endif /* IDETAPE_DEBUG_LOG */
  1856. }
  1857. }
  1858. if (test_bit(PC_WRITING, &pc->flags)) {
  1859. if (pc->bh != NULL)
  1860. idetape_output_buffers(drive, pc, bcount);
  1861. else
  1862. /* Write the current buffer */
  1863. hwif->atapi_output_bytes(drive, pc->current_position,
  1864. bcount);
  1865. } else {
  1866. if (pc->bh != NULL)
  1867. idetape_input_buffers(drive, pc, bcount);
  1868. else
  1869. /* Read the current buffer */
  1870. hwif->atapi_input_bytes(drive, pc->current_position,
  1871. bcount);
  1872. }
  1873. /* Update the current position */
  1874. pc->actually_transferred += bcount;
  1875. pc->current_position += bcount;
  1876. #if IDETAPE_DEBUG_LOG
  1877. if (tape->debug_level >= 2)
  1878. printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
  1879. "on that interrupt\n", pc->c[0], bcount);
  1880. #endif
  1881. /* And set the interrupt handler again */
  1882. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  1883. return ide_started;
  1884. }
  1885. /*
  1886. * Packet Command Interface
  1887. *
  1888. * The current Packet Command is available in tape->pc, and will not
  1889. * change until we finish handling it. Each packet command is associated
  1890. * with a callback function that will be called when the command is
  1891. * finished.
  1892. *
  1893. * The handling will be done in three stages:
  1894. *
  1895. * 1. idetape_issue_packet_command will send the packet command to the
  1896. * drive, and will set the interrupt handler to idetape_pc_intr.
  1897. *
  1898. * 2. On each interrupt, idetape_pc_intr will be called. This step
  1899. * will be repeated until the device signals us that no more
  1900. * interrupts will be issued.
  1901. *
  1902. * 3. ATAPI Tape media access commands have immediate status with a
  1903. * delayed process. In case of a successful initiation of a
  1904. * media access packet command, the DSC bit will be set when the
  1905. * actual execution of the command is finished.
  1906. * Since the tape drive will not issue an interrupt, we have to
  1907. * poll for this event. In this case, we define the request as
  1908. * "low priority request" by setting rq_status to
  1909. * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
  1910. * the driver.
  1911. *
  1912. * ide.c will then give higher priority to requests which
  1913. * originate from the other device, until will change rq_status
  1914. * to RQ_ACTIVE.
  1915. *
  1916. * 4. When the packet command is finished, it will be checked for errors.
  1917. *
  1918. * 5. In case an error was found, we queue a request sense packet
  1919. * command in front of the request queue and retry the operation
  1920. * up to IDETAPE_MAX_PC_RETRIES times.
  1921. *
  1922. * 6. In case no error was found, or we decided to give up and not
  1923. * to retry again, the callback function will be called and then
  1924. * we will handle the next request.
  1925. *
  1926. */
  1927. static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
  1928. {
  1929. ide_hwif_t *hwif = drive->hwif;
  1930. idetape_tape_t *tape = drive->driver_data;
  1931. idetape_pc_t *pc = tape->pc;
  1932. int retries = 100;
  1933. ide_startstop_t startstop;
  1934. u8 ireason;
  1935. if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
  1936. printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
  1937. return startstop;
  1938. }
  1939. ireason = hwif->INB(IDE_IREASON_REG);
  1940. while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
  1941. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
  1942. "a packet command, retrying\n");
  1943. udelay(100);
  1944. ireason = hwif->INB(IDE_IREASON_REG);
  1945. if (retries == 0) {
  1946. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
  1947. "issuing a packet command, ignoring\n");
  1948. ireason |= CD;
  1949. ireason &= ~IO;
  1950. }
  1951. }
  1952. if ((ireason & CD) == 0 || (ireason & IO)) {
  1953. printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
  1954. "a packet command\n");
  1955. return ide_do_reset(drive);
  1956. }
  1957. /* Set the interrupt routine */
  1958. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  1959. #ifdef CONFIG_BLK_DEV_IDEDMA
  1960. /* Begin DMA, if necessary */
  1961. if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
  1962. hwif->dma_start(drive);
  1963. #endif
  1964. /* Send the actual packet */
  1965. HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
  1966. return ide_started;
  1967. }
  1968. static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
  1969. {
  1970. ide_hwif_t *hwif = drive->hwif;
  1971. idetape_tape_t *tape = drive->driver_data;
  1972. int dma_ok = 0;
  1973. u16 bcount;
  1974. #if IDETAPE_DEBUG_BUGS
  1975. if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
  1976. pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  1977. printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
  1978. "Two request sense in serial were issued\n");
  1979. }
  1980. #endif /* IDETAPE_DEBUG_BUGS */
  1981. if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
  1982. tape->failed_pc = pc;
  1983. /* Set the current packet command */
  1984. tape->pc = pc;
  1985. if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
  1986. test_bit(PC_ABORT, &pc->flags)) {
  1987. /*
  1988. * We will "abort" retrying a packet command in case
  1989. * a legitimate error code was received (crossing a
  1990. * filemark, or end of the media, for example).
  1991. */
  1992. if (!test_bit(PC_ABORT, &pc->flags)) {
  1993. if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
  1994. tape->sense_key == 2 && tape->asc == 4 &&
  1995. (tape->ascq == 1 || tape->ascq == 8))) {
  1996. printk(KERN_ERR "ide-tape: %s: I/O error, "
  1997. "pc = %2x, key = %2x, "
  1998. "asc = %2x, ascq = %2x\n",
  1999. tape->name, pc->c[0],
  2000. tape->sense_key, tape->asc,
  2001. tape->ascq);
  2002. }
  2003. /* Giving up */
  2004. pc->error = IDETAPE_ERROR_GENERAL;
  2005. }
  2006. tape->failed_pc = NULL;
  2007. return pc->callback(drive);
  2008. }
  2009. #if IDETAPE_DEBUG_LOG
  2010. if (tape->debug_level >= 2)
  2011. printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
  2012. #endif /* IDETAPE_DEBUG_LOG */
  2013. pc->retries++;
  2014. /* We haven't transferred any data yet */
  2015. pc->actually_transferred = 0;
  2016. pc->current_position = pc->buffer;
  2017. /* Request to transfer the entire buffer at once */
  2018. bcount = pc->request_transfer;
  2019. if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
  2020. printk(KERN_WARNING "ide-tape: DMA disabled, "
  2021. "reverting to PIO\n");
  2022. ide_dma_off(drive);
  2023. }
  2024. if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
  2025. dma_ok = !hwif->dma_setup(drive);
  2026. ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
  2027. IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
  2028. if (dma_ok) /* Will begin DMA later */
  2029. set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  2030. if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
  2031. ide_set_handler(drive, &idetape_transfer_pc, IDETAPE_WAIT_CMD, NULL);
  2032. hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  2033. return ide_started;
  2034. } else {
  2035. hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  2036. return idetape_transfer_pc(drive);
  2037. }
  2038. }
  2039. /*
  2040. * General packet command callback function.
  2041. */
  2042. static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
  2043. {
  2044. idetape_tape_t *tape = drive->driver_data;
  2045. #if IDETAPE_DEBUG_LOG
  2046. if (tape->debug_level >= 4)
  2047. printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
  2048. #endif /* IDETAPE_DEBUG_LOG */
  2049. idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
  2050. return ide_stopped;
  2051. }
  2052. /*
  2053. * A mode sense command is used to "sense" tape parameters.
  2054. */
  2055. static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
  2056. {
  2057. idetape_init_pc(pc);
  2058. pc->c[0] = IDETAPE_MODE_SENSE_CMD;
  2059. if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
  2060. pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
  2061. pc->c[2] = page_code;
  2062. /*
  2063. * Changed pc->c[3] to 0 (255 will at best return unused info).
  2064. *
  2065. * For SCSI this byte is defined as subpage instead of high byte
  2066. * of length and some IDE drives seem to interpret it this way
  2067. * and return an error when 255 is used.
  2068. */
  2069. pc->c[3] = 0;
  2070. pc->c[4] = 255; /* (We will just discard data in that case) */
  2071. if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
  2072. pc->request_transfer = 12;
  2073. else if (page_code == IDETAPE_CAPABILITIES_PAGE)
  2074. pc->request_transfer = 24;
  2075. else
  2076. pc->request_transfer = 50;
  2077. pc->callback = &idetape_pc_callback;
  2078. }
  2079. static void calculate_speeds(ide_drive_t *drive)
  2080. {
  2081. idetape_tape_t *tape = drive->driver_data;
  2082. int full = 125, empty = 75;
  2083. if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
  2084. tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
  2085. tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
  2086. tape->controlled_last_pipeline_head = tape->pipeline_head;
  2087. tape->controlled_pipeline_head_time = jiffies;
  2088. }
  2089. if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
  2090. tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
  2091. else if (time_after(jiffies, tape->controlled_previous_head_time))
  2092. tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
  2093. if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
  2094. /* -1 for read mode error recovery */
  2095. if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
  2096. tape->uncontrolled_pipeline_head_time = jiffies;
  2097. tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
  2098. }
  2099. } else {
  2100. tape->uncontrolled_previous_head_time = jiffies;
  2101. tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
  2102. if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
  2103. tape->uncontrolled_pipeline_head_time = jiffies;
  2104. }
  2105. }
  2106. tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
  2107. if (tape->speed_control == 0) {
  2108. tape->max_insert_speed = 5000;
  2109. } else if (tape->speed_control == 1) {
  2110. if (tape->nr_pending_stages >= tape->max_stages / 2)
  2111. tape->max_insert_speed = tape->pipeline_head_speed +
  2112. (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
  2113. else
  2114. tape->max_insert_speed = 500 +
  2115. (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
  2116. if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
  2117. tape->max_insert_speed = 5000;
  2118. } else if (tape->speed_control == 2) {
  2119. tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
  2120. (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
  2121. } else
  2122. tape->max_insert_speed = tape->speed_control;
  2123. tape->max_insert_speed = max(tape->max_insert_speed, 500);
  2124. }
  2125. static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
  2126. {
  2127. idetape_tape_t *tape = drive->driver_data;
  2128. idetape_pc_t *pc = tape->pc;
  2129. u8 stat;
  2130. stat = drive->hwif->INB(IDE_STATUS_REG);
  2131. if (stat & SEEK_STAT) {
  2132. if (stat & ERR_STAT) {
  2133. /* Error detected */
  2134. if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
  2135. printk(KERN_ERR "ide-tape: %s: I/O error, ",
  2136. tape->name);
  2137. /* Retry operation */
  2138. return idetape_retry_pc(drive);
  2139. }
  2140. pc->error = 0;
  2141. if (tape->failed_pc == pc)
  2142. tape->failed_pc = NULL;
  2143. } else {
  2144. pc->error = IDETAPE_ERROR_GENERAL;
  2145. tape->failed_pc = NULL;
  2146. }
  2147. return pc->callback(drive);
  2148. }
  2149. static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
  2150. {
  2151. idetape_tape_t *tape = drive->driver_data;
  2152. struct request *rq = HWGROUP(drive)->rq;
  2153. int blocks = tape->pc->actually_transferred / tape->tape_block_size;
  2154. tape->avg_size += blocks * tape->tape_block_size;
  2155. tape->insert_size += blocks * tape->tape_block_size;
  2156. if (tape->insert_size > 1024 * 1024)
  2157. tape->measure_insert_time = 1;
  2158. if (tape->measure_insert_time) {
  2159. tape->measure_insert_time = 0;
  2160. tape->insert_time = jiffies;
  2161. tape->insert_size = 0;
  2162. }
  2163. if (time_after(jiffies, tape->insert_time))
  2164. tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
  2165. if (time_after_eq(jiffies, tape->avg_time + HZ)) {
  2166. tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
  2167. tape->avg_size = 0;
  2168. tape->avg_time = jiffies;
  2169. }
  2170. #if IDETAPE_DEBUG_LOG
  2171. if (tape->debug_level >= 4)
  2172. printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
  2173. #endif /* IDETAPE_DEBUG_LOG */
  2174. tape->first_frame_position += blocks;
  2175. rq->current_nr_sectors -= blocks;
  2176. if (!tape->pc->error)
  2177. idetape_end_request(drive, 1, 0);
  2178. else
  2179. idetape_end_request(drive, tape->pc->error, 0);
  2180. return ide_stopped;
  2181. }
  2182. static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  2183. {
  2184. idetape_init_pc(pc);
  2185. pc->c[0] = IDETAPE_READ_CMD;
  2186. put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
  2187. pc->c[1] = 1;
  2188. pc->callback = &idetape_rw_callback;
  2189. pc->bh = bh;
  2190. atomic_set(&bh->b_count, 0);
  2191. pc->buffer = NULL;
  2192. pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
  2193. if (pc->request_transfer == tape->stage_size)
  2194. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  2195. }
  2196. static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  2197. {
  2198. int size = 32768;
  2199. struct idetape_bh *p = bh;
  2200. idetape_init_pc(pc);
  2201. pc->c[0] = IDETAPE_READ_BUFFER_CMD;
  2202. pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
  2203. pc->c[7] = size >> 8;
  2204. pc->c[8] = size & 0xff;
  2205. pc->callback = &idetape_pc_callback;
  2206. pc->bh = bh;
  2207. atomic_set(&bh->b_count, 0);
  2208. pc->buffer = NULL;
  2209. while (p) {
  2210. atomic_set(&p->b_count, 0);
  2211. p = p->b_reqnext;
  2212. }
  2213. pc->request_transfer = pc->buffer_size = size;
  2214. }
  2215. static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
  2216. {
  2217. idetape_init_pc(pc);
  2218. pc->c[0] = IDETAPE_WRITE_CMD;
  2219. put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
  2220. pc->c[1] = 1;
  2221. pc->callback = &idetape_rw_callback;
  2222. set_bit(PC_WRITING, &pc->flags);
  2223. pc->bh = bh;
  2224. pc->b_data = bh->b_data;
  2225. pc->b_count = atomic_read(&bh->b_count);
  2226. pc->buffer = NULL;
  2227. pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
  2228. if (pc->request_transfer == tape->stage_size)
  2229. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  2230. }
  2231. /*
  2232. * idetape_do_request is our request handling function.
  2233. */
  2234. static ide_startstop_t idetape_do_request(ide_drive_t *drive,
  2235. struct request *rq, sector_t block)
  2236. {
  2237. idetape_tape_t *tape = drive->driver_data;
  2238. idetape_pc_t *pc = NULL;
  2239. struct request *postponed_rq = tape->postponed_rq;
  2240. u8 stat;
  2241. #if IDETAPE_DEBUG_LOG
  2242. #if 0
  2243. if (tape->debug_level >= 5)
  2244. printk(KERN_INFO "ide-tape: %d, "
  2245. "dev: %s, cmd: %ld, errors: %d\n",
  2246. rq->rq_disk->disk_name, rq->cmd[0], rq->errors);
  2247. #endif
  2248. if (tape->debug_level >= 2)
  2249. printk(KERN_INFO "ide-tape: sector: %ld, "
  2250. "nr_sectors: %ld, current_nr_sectors: %d\n",
  2251. rq->sector, rq->nr_sectors, rq->current_nr_sectors);
  2252. #endif /* IDETAPE_DEBUG_LOG */
  2253. if (!blk_special_request(rq)) {
  2254. /*
  2255. * We do not support buffer cache originated requests.
  2256. */
  2257. printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
  2258. "request queue (%d)\n", drive->name, rq->cmd_type);
  2259. ide_end_request(drive, 0, 0);
  2260. return ide_stopped;
  2261. }
  2262. /*
  2263. * Retry a failed packet command
  2264. */
  2265. if (tape->failed_pc != NULL &&
  2266. tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  2267. return idetape_issue_packet_command(drive, tape->failed_pc);
  2268. }
  2269. #if IDETAPE_DEBUG_BUGS
  2270. if (postponed_rq != NULL)
  2271. if (rq != postponed_rq) {
  2272. printk(KERN_ERR "ide-tape: ide-tape.c bug - "
  2273. "Two DSC requests were queued\n");
  2274. idetape_end_request(drive, 0, 0);
  2275. return ide_stopped;
  2276. }
  2277. #endif /* IDETAPE_DEBUG_BUGS */
  2278. tape->postponed_rq = NULL;
  2279. /*
  2280. * If the tape is still busy, postpone our request and service
  2281. * the other device meanwhile.
  2282. */
  2283. stat = drive->hwif->INB(IDE_STATUS_REG);
  2284. if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
  2285. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  2286. if (drive->post_reset == 1) {
  2287. set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
  2288. drive->post_reset = 0;
  2289. }
  2290. if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
  2291. tape->measure_insert_time = 1;
  2292. if (time_after(jiffies, tape->insert_time))
  2293. tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
  2294. calculate_speeds(drive);
  2295. if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
  2296. (stat & SEEK_STAT) == 0) {
  2297. if (postponed_rq == NULL) {
  2298. tape->dsc_polling_start = jiffies;
  2299. tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
  2300. tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
  2301. } else if (time_after(jiffies, tape->dsc_timeout)) {
  2302. printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
  2303. tape->name);
  2304. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  2305. idetape_media_access_finished(drive);
  2306. return ide_stopped;
  2307. } else {
  2308. return ide_do_reset(drive);
  2309. }
  2310. } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
  2311. tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
  2312. idetape_postpone_request(drive);
  2313. return ide_stopped;
  2314. }
  2315. if (rq->cmd[0] & REQ_IDETAPE_READ) {
  2316. tape->buffer_head++;
  2317. tape->postpone_cnt = 0;
  2318. pc = idetape_next_pc_storage(drive);
  2319. idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  2320. goto out;
  2321. }
  2322. if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
  2323. tape->buffer_head++;
  2324. tape->postpone_cnt = 0;
  2325. pc = idetape_next_pc_storage(drive);
  2326. idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  2327. goto out;
  2328. }
  2329. if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
  2330. tape->postpone_cnt = 0;
  2331. pc = idetape_next_pc_storage(drive);
  2332. idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
  2333. goto out;
  2334. }
  2335. if (rq->cmd[0] & REQ_IDETAPE_PC1) {
  2336. pc = (idetape_pc_t *) rq->buffer;
  2337. rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
  2338. rq->cmd[0] |= REQ_IDETAPE_PC2;
  2339. goto out;
  2340. }
  2341. if (rq->cmd[0] & REQ_IDETAPE_PC2) {
  2342. idetape_media_access_finished(drive);
  2343. return ide_stopped;
  2344. }
  2345. BUG();
  2346. out:
  2347. return idetape_issue_packet_command(drive, pc);
  2348. }
  2349. /*
  2350. * Pipeline related functions
  2351. */
  2352. static inline int idetape_pipeline_active (idetape_tape_t *tape)
  2353. {
  2354. int rc1, rc2;
  2355. rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  2356. rc2 = (tape->active_data_request != NULL);
  2357. return rc1;
  2358. }
  2359. /*
  2360. * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
  2361. * stage, along with all the necessary small buffers which together make
  2362. * a buffer of size tape->stage_size (or a bit more). We attempt to
  2363. * combine sequential pages as much as possible.
  2364. *
  2365. * Returns a pointer to the new allocated stage, or NULL if we
  2366. * can't (or don't want to) allocate a stage.
  2367. *
  2368. * Pipeline stages are optional and are used to increase performance.
  2369. * If we can't allocate them, we'll manage without them.
  2370. */
  2371. static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
  2372. {
  2373. idetape_stage_t *stage;
  2374. struct idetape_bh *prev_bh, *bh;
  2375. int pages = tape->pages_per_stage;
  2376. char *b_data = NULL;
  2377. if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
  2378. return NULL;
  2379. stage->next = NULL;
  2380. bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
  2381. if (bh == NULL)
  2382. goto abort;
  2383. bh->b_reqnext = NULL;
  2384. if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
  2385. goto abort;
  2386. if (clear)
  2387. memset(bh->b_data, 0, PAGE_SIZE);
  2388. bh->b_size = PAGE_SIZE;
  2389. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  2390. while (--pages) {
  2391. if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
  2392. goto abort;
  2393. if (clear)
  2394. memset(b_data, 0, PAGE_SIZE);
  2395. if (bh->b_data == b_data + PAGE_SIZE) {
  2396. bh->b_size += PAGE_SIZE;
  2397. bh->b_data -= PAGE_SIZE;
  2398. if (full)
  2399. atomic_add(PAGE_SIZE, &bh->b_count);
  2400. continue;
  2401. }
  2402. if (b_data == bh->b_data + bh->b_size) {
  2403. bh->b_size += PAGE_SIZE;
  2404. if (full)
  2405. atomic_add(PAGE_SIZE, &bh->b_count);
  2406. continue;
  2407. }
  2408. prev_bh = bh;
  2409. if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
  2410. free_page((unsigned long) b_data);
  2411. goto abort;
  2412. }
  2413. bh->b_reqnext = NULL;
  2414. bh->b_data = b_data;
  2415. bh->b_size = PAGE_SIZE;
  2416. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  2417. prev_bh->b_reqnext = bh;
  2418. }
  2419. bh->b_size -= tape->excess_bh_size;
  2420. if (full)
  2421. atomic_sub(tape->excess_bh_size, &bh->b_count);
  2422. return stage;
  2423. abort:
  2424. __idetape_kfree_stage(stage);
  2425. return NULL;
  2426. }
  2427. static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
  2428. {
  2429. idetape_stage_t *cache_stage = tape->cache_stage;
  2430. #if IDETAPE_DEBUG_LOG
  2431. if (tape->debug_level >= 4)
  2432. printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
  2433. #endif /* IDETAPE_DEBUG_LOG */
  2434. if (tape->nr_stages >= tape->max_stages)
  2435. return NULL;
  2436. if (cache_stage != NULL) {
  2437. tape->cache_stage = NULL;
  2438. return cache_stage;
  2439. }
  2440. return __idetape_kmalloc_stage(tape, 0, 0);
  2441. }
  2442. static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
  2443. {
  2444. struct idetape_bh *bh = tape->bh;
  2445. int count;
  2446. int ret = 0;
  2447. while (n) {
  2448. #if IDETAPE_DEBUG_BUGS
  2449. if (bh == NULL) {
  2450. printk(KERN_ERR "ide-tape: bh == NULL in "
  2451. "idetape_copy_stage_from_user\n");
  2452. return 1;
  2453. }
  2454. #endif /* IDETAPE_DEBUG_BUGS */
  2455. count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
  2456. if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
  2457. ret = 1;
  2458. n -= count;
  2459. atomic_add(count, &bh->b_count);
  2460. buf += count;
  2461. if (atomic_read(&bh->b_count) == bh->b_size) {
  2462. bh = bh->b_reqnext;
  2463. if (bh)
  2464. atomic_set(&bh->b_count, 0);
  2465. }
  2466. }
  2467. tape->bh = bh;
  2468. return ret;
  2469. }
  2470. static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
  2471. {
  2472. struct idetape_bh *bh = tape->bh;
  2473. int count;
  2474. int ret = 0;
  2475. while (n) {
  2476. #if IDETAPE_DEBUG_BUGS
  2477. if (bh == NULL) {
  2478. printk(KERN_ERR "ide-tape: bh == NULL in "
  2479. "idetape_copy_stage_to_user\n");
  2480. return 1;
  2481. }
  2482. #endif /* IDETAPE_DEBUG_BUGS */
  2483. count = min(tape->b_count, n);
  2484. if (copy_to_user(buf, tape->b_data, count))
  2485. ret = 1;
  2486. n -= count;
  2487. tape->b_data += count;
  2488. tape->b_count -= count;
  2489. buf += count;
  2490. if (!tape->b_count) {
  2491. tape->bh = bh = bh->b_reqnext;
  2492. if (bh) {
  2493. tape->b_data = bh->b_data;
  2494. tape->b_count = atomic_read(&bh->b_count);
  2495. }
  2496. }
  2497. }
  2498. return ret;
  2499. }
  2500. static void idetape_init_merge_stage (idetape_tape_t *tape)
  2501. {
  2502. struct idetape_bh *bh = tape->merge_stage->bh;
  2503. tape->bh = bh;
  2504. if (tape->chrdev_direction == idetape_direction_write)
  2505. atomic_set(&bh->b_count, 0);
  2506. else {
  2507. tape->b_data = bh->b_data;
  2508. tape->b_count = atomic_read(&bh->b_count);
  2509. }
  2510. }
  2511. static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
  2512. {
  2513. struct idetape_bh *tmp;
  2514. tmp = stage->bh;
  2515. stage->bh = tape->merge_stage->bh;
  2516. tape->merge_stage->bh = tmp;
  2517. idetape_init_merge_stage(tape);
  2518. }
  2519. /*
  2520. * idetape_add_stage_tail adds a new stage at the end of the pipeline.
  2521. */
  2522. static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
  2523. {
  2524. idetape_tape_t *tape = drive->driver_data;
  2525. unsigned long flags;
  2526. #if IDETAPE_DEBUG_LOG
  2527. if (tape->debug_level >= 4)
  2528. printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
  2529. #endif /* IDETAPE_DEBUG_LOG */
  2530. spin_lock_irqsave(&tape->spinlock, flags);
  2531. stage->next = NULL;
  2532. if (tape->last_stage != NULL)
  2533. tape->last_stage->next=stage;
  2534. else
  2535. tape->first_stage = tape->next_stage=stage;
  2536. tape->last_stage = stage;
  2537. if (tape->next_stage == NULL)
  2538. tape->next_stage = tape->last_stage;
  2539. tape->nr_stages++;
  2540. tape->nr_pending_stages++;
  2541. spin_unlock_irqrestore(&tape->spinlock, flags);
  2542. }
  2543. /*
  2544. * idetape_wait_for_request installs a completion in a pending request
  2545. * and sleeps until it is serviced.
  2546. *
  2547. * The caller should ensure that the request will not be serviced
  2548. * before we install the completion (usually by disabling interrupts).
  2549. */
  2550. static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
  2551. {
  2552. DECLARE_COMPLETION_ONSTACK(wait);
  2553. idetape_tape_t *tape = drive->driver_data;
  2554. #if IDETAPE_DEBUG_BUGS
  2555. if (rq == NULL || !blk_special_request(rq)) {
  2556. printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
  2557. return;
  2558. }
  2559. #endif /* IDETAPE_DEBUG_BUGS */
  2560. rq->end_io_data = &wait;
  2561. rq->end_io = blk_end_sync_rq;
  2562. spin_unlock_irq(&tape->spinlock);
  2563. wait_for_completion(&wait);
  2564. /* The stage and its struct request have been deallocated */
  2565. spin_lock_irq(&tape->spinlock);
  2566. }
  2567. static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
  2568. {
  2569. idetape_tape_t *tape = drive->driver_data;
  2570. idetape_read_position_result_t *result;
  2571. #if IDETAPE_DEBUG_LOG
  2572. if (tape->debug_level >= 4)
  2573. printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
  2574. #endif /* IDETAPE_DEBUG_LOG */
  2575. if (!tape->pc->error) {
  2576. result = (idetape_read_position_result_t *) tape->pc->buffer;
  2577. #if IDETAPE_DEBUG_LOG
  2578. if (tape->debug_level >= 2)
  2579. printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
  2580. if (tape->debug_level >= 2)
  2581. printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
  2582. #endif /* IDETAPE_DEBUG_LOG */
  2583. if (result->bpu) {
  2584. printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
  2585. clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
  2586. idetape_end_request(drive, 0, 0);
  2587. } else {
  2588. #if IDETAPE_DEBUG_LOG
  2589. if (tape->debug_level >= 2)
  2590. printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
  2591. #endif /* IDETAPE_DEBUG_LOG */
  2592. tape->partition = result->partition;
  2593. tape->first_frame_position = ntohl(result->first_block);
  2594. tape->last_frame_position = ntohl(result->last_block);
  2595. tape->blocks_in_buffer = result->blocks_in_buffer[2];
  2596. set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
  2597. idetape_end_request(drive, 1, 0);
  2598. }
  2599. } else {
  2600. idetape_end_request(drive, 0, 0);
  2601. }
  2602. return ide_stopped;
  2603. }
  2604. /*
  2605. * idetape_create_write_filemark_cmd will:
  2606. *
  2607. * 1. Write a filemark if write_filemark=1.
  2608. * 2. Flush the device buffers without writing a filemark
  2609. * if write_filemark=0.
  2610. *
  2611. */
  2612. static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
  2613. {
  2614. idetape_init_pc(pc);
  2615. pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
  2616. pc->c[4] = write_filemark;
  2617. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2618. pc->callback = &idetape_pc_callback;
  2619. }
  2620. static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
  2621. {
  2622. idetape_init_pc(pc);
  2623. pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
  2624. pc->callback = &idetape_pc_callback;
  2625. }
  2626. /*
  2627. * idetape_queue_pc_tail is based on the following functions:
  2628. *
  2629. * ide_do_drive_cmd from ide.c
  2630. * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
  2631. *
  2632. * We add a special packet command request to the tail of the request
  2633. * queue, and wait for it to be serviced.
  2634. *
  2635. * This is not to be called from within the request handling part
  2636. * of the driver ! We allocate here data in the stack, and it is valid
  2637. * until the request is finished. This is not the case for the bottom
  2638. * part of the driver, where we are always leaving the functions to wait
  2639. * for an interrupt or a timer event.
  2640. *
  2641. * From the bottom part of the driver, we should allocate safe memory
  2642. * using idetape_next_pc_storage and idetape_next_rq_storage, and add
  2643. * the request to the request list without waiting for it to be serviced !
  2644. * In that case, we usually use idetape_queue_pc_head.
  2645. */
  2646. static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
  2647. {
  2648. struct ide_tape_obj *tape = drive->driver_data;
  2649. struct request rq;
  2650. idetape_init_rq(&rq, REQ_IDETAPE_PC1);
  2651. rq.buffer = (char *) pc;
  2652. rq.rq_disk = tape->disk;
  2653. return ide_do_drive_cmd(drive, &rq, ide_wait);
  2654. }
  2655. static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
  2656. {
  2657. idetape_init_pc(pc);
  2658. pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
  2659. pc->c[4] = cmd;
  2660. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2661. pc->callback = &idetape_pc_callback;
  2662. }
  2663. static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
  2664. {
  2665. idetape_tape_t *tape = drive->driver_data;
  2666. idetape_pc_t pc;
  2667. int load_attempted = 0;
  2668. /*
  2669. * Wait for the tape to become ready
  2670. */
  2671. set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
  2672. timeout += jiffies;
  2673. while (time_before(jiffies, timeout)) {
  2674. idetape_create_test_unit_ready_cmd(&pc);
  2675. if (!__idetape_queue_pc_tail(drive, &pc))
  2676. return 0;
  2677. if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
  2678. || (tape->asc == 0x3A)) { /* no media */
  2679. if (load_attempted)
  2680. return -ENOMEDIUM;
  2681. idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
  2682. __idetape_queue_pc_tail(drive, &pc);
  2683. load_attempted = 1;
  2684. /* not about to be ready */
  2685. } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
  2686. (tape->ascq == 1 || tape->ascq == 8)))
  2687. return -EIO;
  2688. msleep(100);
  2689. }
  2690. return -EIO;
  2691. }
  2692. static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
  2693. {
  2694. return __idetape_queue_pc_tail(drive, pc);
  2695. }
  2696. static int idetape_flush_tape_buffers (ide_drive_t *drive)
  2697. {
  2698. idetape_pc_t pc;
  2699. int rc;
  2700. idetape_create_write_filemark_cmd(drive, &pc, 0);
  2701. if ((rc = idetape_queue_pc_tail(drive, &pc)))
  2702. return rc;
  2703. idetape_wait_ready(drive, 60 * 5 * HZ);
  2704. return 0;
  2705. }
  2706. static void idetape_create_read_position_cmd (idetape_pc_t *pc)
  2707. {
  2708. idetape_init_pc(pc);
  2709. pc->c[0] = IDETAPE_READ_POSITION_CMD;
  2710. pc->request_transfer = 20;
  2711. pc->callback = &idetape_read_position_callback;
  2712. }
  2713. static int idetape_read_position (ide_drive_t *drive)
  2714. {
  2715. idetape_tape_t *tape = drive->driver_data;
  2716. idetape_pc_t pc;
  2717. int position;
  2718. #if IDETAPE_DEBUG_LOG
  2719. if (tape->debug_level >= 4)
  2720. printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
  2721. #endif /* IDETAPE_DEBUG_LOG */
  2722. idetape_create_read_position_cmd(&pc);
  2723. if (idetape_queue_pc_tail(drive, &pc))
  2724. return -1;
  2725. position = tape->first_frame_position;
  2726. return position;
  2727. }
  2728. static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
  2729. {
  2730. idetape_init_pc(pc);
  2731. pc->c[0] = IDETAPE_LOCATE_CMD;
  2732. pc->c[1] = 2;
  2733. put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
  2734. pc->c[8] = partition;
  2735. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2736. pc->callback = &idetape_pc_callback;
  2737. }
  2738. static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
  2739. {
  2740. idetape_tape_t *tape = drive->driver_data;
  2741. if (!tape->capabilities.lock)
  2742. return 0;
  2743. idetape_init_pc(pc);
  2744. pc->c[0] = IDETAPE_PREVENT_CMD;
  2745. pc->c[4] = prevent;
  2746. pc->callback = &idetape_pc_callback;
  2747. return 1;
  2748. }
  2749. static int __idetape_discard_read_pipeline (ide_drive_t *drive)
  2750. {
  2751. idetape_tape_t *tape = drive->driver_data;
  2752. unsigned long flags;
  2753. int cnt;
  2754. if (tape->chrdev_direction != idetape_direction_read)
  2755. return 0;
  2756. /* Remove merge stage. */
  2757. cnt = tape->merge_stage_size / tape->tape_block_size;
  2758. if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
  2759. ++cnt; /* Filemarks count as 1 sector */
  2760. tape->merge_stage_size = 0;
  2761. if (tape->merge_stage != NULL) {
  2762. __idetape_kfree_stage(tape->merge_stage);
  2763. tape->merge_stage = NULL;
  2764. }
  2765. /* Clear pipeline flags. */
  2766. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  2767. tape->chrdev_direction = idetape_direction_none;
  2768. /* Remove pipeline stages. */
  2769. if (tape->first_stage == NULL)
  2770. return 0;
  2771. spin_lock_irqsave(&tape->spinlock, flags);
  2772. tape->next_stage = NULL;
  2773. if (idetape_pipeline_active(tape))
  2774. idetape_wait_for_request(drive, tape->active_data_request);
  2775. spin_unlock_irqrestore(&tape->spinlock, flags);
  2776. while (tape->first_stage != NULL) {
  2777. struct request *rq_ptr = &tape->first_stage->rq;
  2778. cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
  2779. if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
  2780. ++cnt;
  2781. idetape_remove_stage_head(drive);
  2782. }
  2783. tape->nr_pending_stages = 0;
  2784. tape->max_stages = tape->min_pipeline;
  2785. return cnt;
  2786. }
  2787. /*
  2788. * idetape_position_tape positions the tape to the requested block
  2789. * using the LOCATE packet command. A READ POSITION command is then
  2790. * issued to check where we are positioned.
  2791. *
  2792. * Like all higher level operations, we queue the commands at the tail
  2793. * of the request queue and wait for their completion.
  2794. *
  2795. */
  2796. static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
  2797. {
  2798. idetape_tape_t *tape = drive->driver_data;
  2799. int retval;
  2800. idetape_pc_t pc;
  2801. if (tape->chrdev_direction == idetape_direction_read)
  2802. __idetape_discard_read_pipeline(drive);
  2803. idetape_wait_ready(drive, 60 * 5 * HZ);
  2804. idetape_create_locate_cmd(drive, &pc, block, partition, skip);
  2805. retval = idetape_queue_pc_tail(drive, &pc);
  2806. if (retval)
  2807. return (retval);
  2808. idetape_create_read_position_cmd(&pc);
  2809. return (idetape_queue_pc_tail(drive, &pc));
  2810. }
  2811. static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
  2812. {
  2813. idetape_tape_t *tape = drive->driver_data;
  2814. int cnt;
  2815. int seek, position;
  2816. cnt = __idetape_discard_read_pipeline(drive);
  2817. if (restore_position) {
  2818. position = idetape_read_position(drive);
  2819. seek = position > cnt ? position - cnt : 0;
  2820. if (idetape_position_tape(drive, seek, 0, 0)) {
  2821. printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
  2822. return;
  2823. }
  2824. }
  2825. }
  2826. /*
  2827. * idetape_queue_rw_tail generates a read/write request for the block
  2828. * device interface and wait for it to be serviced.
  2829. */
  2830. static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
  2831. {
  2832. idetape_tape_t *tape = drive->driver_data;
  2833. struct request rq;
  2834. #if IDETAPE_DEBUG_LOG
  2835. if (tape->debug_level >= 2)
  2836. printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
  2837. #endif /* IDETAPE_DEBUG_LOG */
  2838. #if IDETAPE_DEBUG_BUGS
  2839. if (idetape_pipeline_active(tape)) {
  2840. printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
  2841. return (0);
  2842. }
  2843. #endif /* IDETAPE_DEBUG_BUGS */
  2844. idetape_init_rq(&rq, cmd);
  2845. rq.rq_disk = tape->disk;
  2846. rq.special = (void *)bh;
  2847. rq.sector = tape->first_frame_position;
  2848. rq.nr_sectors = rq.current_nr_sectors = blocks;
  2849. (void) ide_do_drive_cmd(drive, &rq, ide_wait);
  2850. if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
  2851. return 0;
  2852. if (tape->merge_stage)
  2853. idetape_init_merge_stage(tape);
  2854. if (rq.errors == IDETAPE_ERROR_GENERAL)
  2855. return -EIO;
  2856. return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
  2857. }
  2858. /*
  2859. * idetape_insert_pipeline_into_queue is used to start servicing the
  2860. * pipeline stages, starting from tape->next_stage.
  2861. */
  2862. static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
  2863. {
  2864. idetape_tape_t *tape = drive->driver_data;
  2865. if (tape->next_stage == NULL)
  2866. return;
  2867. if (!idetape_pipeline_active(tape)) {
  2868. set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  2869. idetape_active_next_stage(drive);
  2870. (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
  2871. }
  2872. }
  2873. static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
  2874. {
  2875. idetape_init_pc(pc);
  2876. pc->c[0] = IDETAPE_INQUIRY_CMD;
  2877. pc->c[4] = pc->request_transfer = 254;
  2878. pc->callback = &idetape_pc_callback;
  2879. }
  2880. static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
  2881. {
  2882. idetape_init_pc(pc);
  2883. pc->c[0] = IDETAPE_REWIND_CMD;
  2884. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2885. pc->callback = &idetape_pc_callback;
  2886. }
  2887. #if 0
  2888. static void idetape_create_mode_select_cmd (idetape_pc_t *pc, int length)
  2889. {
  2890. idetape_init_pc(pc);
  2891. set_bit(PC_WRITING, &pc->flags);
  2892. pc->c[0] = IDETAPE_MODE_SELECT_CMD;
  2893. pc->c[1] = 0x10;
  2894. put_unaligned(htons(length), (unsigned short *) &pc->c[3]);
  2895. pc->request_transfer = 255;
  2896. pc->callback = &idetape_pc_callback;
  2897. }
  2898. #endif
  2899. static void idetape_create_erase_cmd (idetape_pc_t *pc)
  2900. {
  2901. idetape_init_pc(pc);
  2902. pc->c[0] = IDETAPE_ERASE_CMD;
  2903. pc->c[1] = 1;
  2904. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2905. pc->callback = &idetape_pc_callback;
  2906. }
  2907. static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
  2908. {
  2909. idetape_init_pc(pc);
  2910. pc->c[0] = IDETAPE_SPACE_CMD;
  2911. put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
  2912. pc->c[1] = cmd;
  2913. set_bit(PC_WAIT_FOR_DSC, &pc->flags);
  2914. pc->callback = &idetape_pc_callback;
  2915. }
  2916. static void idetape_wait_first_stage (ide_drive_t *drive)
  2917. {
  2918. idetape_tape_t *tape = drive->driver_data;
  2919. unsigned long flags;
  2920. if (tape->first_stage == NULL)
  2921. return;
  2922. spin_lock_irqsave(&tape->spinlock, flags);
  2923. if (tape->active_stage == tape->first_stage)
  2924. idetape_wait_for_request(drive, tape->active_data_request);
  2925. spin_unlock_irqrestore(&tape->spinlock, flags);
  2926. }
  2927. /*
  2928. * idetape_add_chrdev_write_request tries to add a character device
  2929. * originated write request to our pipeline. In case we don't succeed,
  2930. * we revert to non-pipelined operation mode for this request.
  2931. *
  2932. * 1. Try to allocate a new pipeline stage.
  2933. * 2. If we can't, wait for more and more requests to be serviced
  2934. * and try again each time.
  2935. * 3. If we still can't allocate a stage, fallback to
  2936. * non-pipelined operation mode for this request.
  2937. */
  2938. static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
  2939. {
  2940. idetape_tape_t *tape = drive->driver_data;
  2941. idetape_stage_t *new_stage;
  2942. unsigned long flags;
  2943. struct request *rq;
  2944. #if IDETAPE_DEBUG_LOG
  2945. if (tape->debug_level >= 3)
  2946. printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
  2947. #endif /* IDETAPE_DEBUG_LOG */
  2948. /*
  2949. * Attempt to allocate a new stage.
  2950. * Pay special attention to possible race conditions.
  2951. */
  2952. while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
  2953. spin_lock_irqsave(&tape->spinlock, flags);
  2954. if (idetape_pipeline_active(tape)) {
  2955. idetape_wait_for_request(drive, tape->active_data_request);
  2956. spin_unlock_irqrestore(&tape->spinlock, flags);
  2957. } else {
  2958. spin_unlock_irqrestore(&tape->spinlock, flags);
  2959. idetape_insert_pipeline_into_queue(drive);
  2960. if (idetape_pipeline_active(tape))
  2961. continue;
  2962. /*
  2963. * Linux is short on memory. Fallback to
  2964. * non-pipelined operation mode for this request.
  2965. */
  2966. return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
  2967. }
  2968. }
  2969. rq = &new_stage->rq;
  2970. idetape_init_rq(rq, REQ_IDETAPE_WRITE);
  2971. /* Doesn't actually matter - We always assume sequential access */
  2972. rq->sector = tape->first_frame_position;
  2973. rq->nr_sectors = rq->current_nr_sectors = blocks;
  2974. idetape_switch_buffers(tape, new_stage);
  2975. idetape_add_stage_tail(drive, new_stage);
  2976. tape->pipeline_head++;
  2977. calculate_speeds(drive);
  2978. /*
  2979. * Estimate whether the tape has stopped writing by checking
  2980. * if our write pipeline is currently empty. If we are not
  2981. * writing anymore, wait for the pipeline to be full enough
  2982. * (90%) before starting to service requests, so that we will
  2983. * be able to keep up with the higher speeds of the tape.
  2984. */
  2985. if (!idetape_pipeline_active(tape)) {
  2986. if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
  2987. tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
  2988. tape->measure_insert_time = 1;
  2989. tape->insert_time = jiffies;
  2990. tape->insert_size = 0;
  2991. tape->insert_speed = 0;
  2992. idetape_insert_pipeline_into_queue(drive);
  2993. }
  2994. }
  2995. if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
  2996. /* Return a deferred error */
  2997. return -EIO;
  2998. return blocks;
  2999. }
  3000. /*
  3001. * idetape_wait_for_pipeline will wait until all pending pipeline
  3002. * requests are serviced. Typically called on device close.
  3003. */
  3004. static void idetape_wait_for_pipeline (ide_drive_t *drive)
  3005. {
  3006. idetape_tape_t *tape = drive->driver_data;
  3007. unsigned long flags;
  3008. while (tape->next_stage || idetape_pipeline_active(tape)) {
  3009. idetape_insert_pipeline_into_queue(drive);
  3010. spin_lock_irqsave(&tape->spinlock, flags);
  3011. if (idetape_pipeline_active(tape))
  3012. idetape_wait_for_request(drive, tape->active_data_request);
  3013. spin_unlock_irqrestore(&tape->spinlock, flags);
  3014. }
  3015. }
  3016. static void idetape_empty_write_pipeline (ide_drive_t *drive)
  3017. {
  3018. idetape_tape_t *tape = drive->driver_data;
  3019. int blocks, min;
  3020. struct idetape_bh *bh;
  3021. #if IDETAPE_DEBUG_BUGS
  3022. if (tape->chrdev_direction != idetape_direction_write) {
  3023. printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
  3024. return;
  3025. }
  3026. if (tape->merge_stage_size > tape->stage_size) {
  3027. printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
  3028. tape->merge_stage_size = tape->stage_size;
  3029. }
  3030. #endif /* IDETAPE_DEBUG_BUGS */
  3031. if (tape->merge_stage_size) {
  3032. blocks = tape->merge_stage_size / tape->tape_block_size;
  3033. if (tape->merge_stage_size % tape->tape_block_size) {
  3034. unsigned int i;
  3035. blocks++;
  3036. i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
  3037. bh = tape->bh->b_reqnext;
  3038. while (bh) {
  3039. atomic_set(&bh->b_count, 0);
  3040. bh = bh->b_reqnext;
  3041. }
  3042. bh = tape->bh;
  3043. while (i) {
  3044. if (bh == NULL) {
  3045. printk(KERN_INFO "ide-tape: bug, bh NULL\n");
  3046. break;
  3047. }
  3048. min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
  3049. memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
  3050. atomic_add(min, &bh->b_count);
  3051. i -= min;
  3052. bh = bh->b_reqnext;
  3053. }
  3054. }
  3055. (void) idetape_add_chrdev_write_request(drive, blocks);
  3056. tape->merge_stage_size = 0;
  3057. }
  3058. idetape_wait_for_pipeline(drive);
  3059. if (tape->merge_stage != NULL) {
  3060. __idetape_kfree_stage(tape->merge_stage);
  3061. tape->merge_stage = NULL;
  3062. }
  3063. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  3064. tape->chrdev_direction = idetape_direction_none;
  3065. /*
  3066. * On the next backup, perform the feedback loop again.
  3067. * (I don't want to keep sense information between backups,
  3068. * as some systems are constantly on, and the system load
  3069. * can be totally different on the next backup).
  3070. */
  3071. tape->max_stages = tape->min_pipeline;
  3072. #if IDETAPE_DEBUG_BUGS
  3073. if (tape->first_stage != NULL ||
  3074. tape->next_stage != NULL ||
  3075. tape->last_stage != NULL ||
  3076. tape->nr_stages != 0) {
  3077. printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
  3078. "first_stage %p, next_stage %p, "
  3079. "last_stage %p, nr_stages %d\n",
  3080. tape->first_stage, tape->next_stage,
  3081. tape->last_stage, tape->nr_stages);
  3082. }
  3083. #endif /* IDETAPE_DEBUG_BUGS */
  3084. }
  3085. static void idetape_restart_speed_control (ide_drive_t *drive)
  3086. {
  3087. idetape_tape_t *tape = drive->driver_data;
  3088. tape->restart_speed_control_req = 0;
  3089. tape->pipeline_head = 0;
  3090. tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
  3091. tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
  3092. tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
  3093. tape->uncontrolled_pipeline_head_speed = 0;
  3094. tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
  3095. tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
  3096. }
  3097. static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
  3098. {
  3099. idetape_tape_t *tape = drive->driver_data;
  3100. idetape_stage_t *new_stage;
  3101. struct request rq;
  3102. int bytes_read;
  3103. int blocks = tape->capabilities.ctl;
  3104. /* Initialize read operation */
  3105. if (tape->chrdev_direction != idetape_direction_read) {
  3106. if (tape->chrdev_direction == idetape_direction_write) {
  3107. idetape_empty_write_pipeline(drive);
  3108. idetape_flush_tape_buffers(drive);
  3109. }
  3110. #if IDETAPE_DEBUG_BUGS
  3111. if (tape->merge_stage || tape->merge_stage_size) {
  3112. printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
  3113. tape->merge_stage_size = 0;
  3114. }
  3115. #endif /* IDETAPE_DEBUG_BUGS */
  3116. if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
  3117. return -ENOMEM;
  3118. tape->chrdev_direction = idetape_direction_read;
  3119. /*
  3120. * Issue a read 0 command to ensure that DSC handshake
  3121. * is switched from completion mode to buffer available
  3122. * mode.
  3123. * No point in issuing this if DSC overlap isn't supported,
  3124. * some drives (Seagate STT3401A) will return an error.
  3125. */
  3126. if (drive->dsc_overlap) {
  3127. bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
  3128. if (bytes_read < 0) {
  3129. __idetape_kfree_stage(tape->merge_stage);
  3130. tape->merge_stage = NULL;
  3131. tape->chrdev_direction = idetape_direction_none;
  3132. return bytes_read;
  3133. }
  3134. }
  3135. }
  3136. if (tape->restart_speed_control_req)
  3137. idetape_restart_speed_control(drive);
  3138. idetape_init_rq(&rq, REQ_IDETAPE_READ);
  3139. rq.sector = tape->first_frame_position;
  3140. rq.nr_sectors = rq.current_nr_sectors = blocks;
  3141. if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
  3142. tape->nr_stages < max_stages) {
  3143. new_stage = idetape_kmalloc_stage(tape);
  3144. while (new_stage != NULL) {
  3145. new_stage->rq = rq;
  3146. idetape_add_stage_tail(drive, new_stage);
  3147. if (tape->nr_stages >= max_stages)
  3148. break;
  3149. new_stage = idetape_kmalloc_stage(tape);
  3150. }
  3151. }
  3152. if (!idetape_pipeline_active(tape)) {
  3153. if (tape->nr_pending_stages >= 3 * max_stages / 4) {
  3154. tape->measure_insert_time = 1;
  3155. tape->insert_time = jiffies;
  3156. tape->insert_size = 0;
  3157. tape->insert_speed = 0;
  3158. idetape_insert_pipeline_into_queue(drive);
  3159. }
  3160. }
  3161. return 0;
  3162. }
  3163. /*
  3164. * idetape_add_chrdev_read_request is called from idetape_chrdev_read
  3165. * to service a character device read request and add read-ahead
  3166. * requests to our pipeline.
  3167. */
  3168. static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
  3169. {
  3170. idetape_tape_t *tape = drive->driver_data;
  3171. unsigned long flags;
  3172. struct request *rq_ptr;
  3173. int bytes_read;
  3174. #if IDETAPE_DEBUG_LOG
  3175. if (tape->debug_level >= 4)
  3176. printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
  3177. #endif /* IDETAPE_DEBUG_LOG */
  3178. /*
  3179. * If we are at a filemark, return a read length of 0
  3180. */
  3181. if (test_bit(IDETAPE_FILEMARK, &tape->flags))
  3182. return 0;
  3183. /*
  3184. * Wait for the next block to be available at the head
  3185. * of the pipeline
  3186. */
  3187. idetape_initiate_read(drive, tape->max_stages);
  3188. if (tape->first_stage == NULL) {
  3189. if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
  3190. return 0;
  3191. return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
  3192. }
  3193. idetape_wait_first_stage(drive);
  3194. rq_ptr = &tape->first_stage->rq;
  3195. bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
  3196. rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
  3197. if (rq_ptr->errors == IDETAPE_ERROR_EOD)
  3198. return 0;
  3199. else {
  3200. idetape_switch_buffers(tape, tape->first_stage);
  3201. if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
  3202. set_bit(IDETAPE_FILEMARK, &tape->flags);
  3203. spin_lock_irqsave(&tape->spinlock, flags);
  3204. idetape_remove_stage_head(drive);
  3205. spin_unlock_irqrestore(&tape->spinlock, flags);
  3206. tape->pipeline_head++;
  3207. calculate_speeds(drive);
  3208. }
  3209. #if IDETAPE_DEBUG_BUGS
  3210. if (bytes_read > blocks * tape->tape_block_size) {
  3211. printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
  3212. bytes_read = blocks * tape->tape_block_size;
  3213. }
  3214. #endif /* IDETAPE_DEBUG_BUGS */
  3215. return (bytes_read);
  3216. }
  3217. static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
  3218. {
  3219. idetape_tape_t *tape = drive->driver_data;
  3220. struct idetape_bh *bh;
  3221. int blocks;
  3222. while (bcount) {
  3223. unsigned int count;
  3224. bh = tape->merge_stage->bh;
  3225. count = min(tape->stage_size, bcount);
  3226. bcount -= count;
  3227. blocks = count / tape->tape_block_size;
  3228. while (count) {
  3229. atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
  3230. memset(bh->b_data, 0, atomic_read(&bh->b_count));
  3231. count -= atomic_read(&bh->b_count);
  3232. bh = bh->b_reqnext;
  3233. }
  3234. idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
  3235. }
  3236. }
  3237. static int idetape_pipeline_size (ide_drive_t *drive)
  3238. {
  3239. idetape_tape_t *tape = drive->driver_data;
  3240. idetape_stage_t *stage;
  3241. struct request *rq;
  3242. int size = 0;
  3243. idetape_wait_for_pipeline(drive);
  3244. stage = tape->first_stage;
  3245. while (stage != NULL) {
  3246. rq = &stage->rq;
  3247. size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
  3248. if (rq->errors == IDETAPE_ERROR_FILEMARK)
  3249. size += tape->tape_block_size;
  3250. stage = stage->next;
  3251. }
  3252. size += tape->merge_stage_size;
  3253. return size;
  3254. }
  3255. /*
  3256. * Rewinds the tape to the Beginning Of the current Partition (BOP).
  3257. *
  3258. * We currently support only one partition.
  3259. */
  3260. static int idetape_rewind_tape (ide_drive_t *drive)
  3261. {
  3262. int retval;
  3263. idetape_pc_t pc;
  3264. #if IDETAPE_DEBUG_LOG
  3265. idetape_tape_t *tape = drive->driver_data;
  3266. if (tape->debug_level >= 2)
  3267. printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
  3268. #endif /* IDETAPE_DEBUG_LOG */
  3269. idetape_create_rewind_cmd(drive, &pc);
  3270. retval = idetape_queue_pc_tail(drive, &pc);
  3271. if (retval)
  3272. return retval;
  3273. idetape_create_read_position_cmd(&pc);
  3274. retval = idetape_queue_pc_tail(drive, &pc);
  3275. if (retval)
  3276. return retval;
  3277. return 0;
  3278. }
  3279. /*
  3280. * Our special ide-tape ioctl's.
  3281. *
  3282. * Currently there aren't any ioctl's.
  3283. * mtio.h compatible commands should be issued to the character device
  3284. * interface.
  3285. */
  3286. static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  3287. {
  3288. idetape_tape_t *tape = drive->driver_data;
  3289. idetape_config_t config;
  3290. void __user *argp = (void __user *)arg;
  3291. #if IDETAPE_DEBUG_LOG
  3292. if (tape->debug_level >= 4)
  3293. printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
  3294. #endif /* IDETAPE_DEBUG_LOG */
  3295. switch (cmd) {
  3296. case 0x0340:
  3297. if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
  3298. return -EFAULT;
  3299. tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
  3300. tape->max_stages = config.nr_stages;
  3301. break;
  3302. case 0x0350:
  3303. config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
  3304. config.nr_stages = tape->max_stages;
  3305. if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
  3306. return -EFAULT;
  3307. break;
  3308. default:
  3309. return -EIO;
  3310. }
  3311. return 0;
  3312. }
  3313. /*
  3314. * idetape_space_over_filemarks is now a bit more complicated than just
  3315. * passing the command to the tape since we may have crossed some
  3316. * filemarks during our pipelined read-ahead mode.
  3317. *
  3318. * As a minor side effect, the pipeline enables us to support MTFSFM when
  3319. * the filemark is in our internal pipeline even if the tape doesn't
  3320. * support spacing over filemarks in the reverse direction.
  3321. */
  3322. static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
  3323. {
  3324. idetape_tape_t *tape = drive->driver_data;
  3325. idetape_pc_t pc;
  3326. unsigned long flags;
  3327. int retval,count=0;
  3328. if (mt_count == 0)
  3329. return 0;
  3330. if (MTBSF == mt_op || MTBSFM == mt_op) {
  3331. if (!tape->capabilities.sprev)
  3332. return -EIO;
  3333. mt_count = - mt_count;
  3334. }
  3335. if (tape->chrdev_direction == idetape_direction_read) {
  3336. /*
  3337. * We have a read-ahead buffer. Scan it for crossed
  3338. * filemarks.
  3339. */
  3340. tape->merge_stage_size = 0;
  3341. if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
  3342. ++count;
  3343. while (tape->first_stage != NULL) {
  3344. if (count == mt_count) {
  3345. if (mt_op == MTFSFM)
  3346. set_bit(IDETAPE_FILEMARK, &tape->flags);
  3347. return 0;
  3348. }
  3349. spin_lock_irqsave(&tape->spinlock, flags);
  3350. if (tape->first_stage == tape->active_stage) {
  3351. /*
  3352. * We have reached the active stage in the read pipeline.
  3353. * There is no point in allowing the drive to continue
  3354. * reading any farther, so we stop the pipeline.
  3355. *
  3356. * This section should be moved to a separate subroutine,
  3357. * because a similar function is performed in
  3358. * __idetape_discard_read_pipeline(), for example.
  3359. */
  3360. tape->next_stage = NULL;
  3361. spin_unlock_irqrestore(&tape->spinlock, flags);
  3362. idetape_wait_first_stage(drive);
  3363. tape->next_stage = tape->first_stage->next;
  3364. } else
  3365. spin_unlock_irqrestore(&tape->spinlock, flags);
  3366. if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
  3367. ++count;
  3368. idetape_remove_stage_head(drive);
  3369. }
  3370. idetape_discard_read_pipeline(drive, 0);
  3371. }
  3372. /*
  3373. * The filemark was not found in our internal pipeline.
  3374. * Now we can issue the space command.
  3375. */
  3376. switch (mt_op) {
  3377. case MTFSF:
  3378. case MTBSF:
  3379. idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
  3380. return (idetape_queue_pc_tail(drive, &pc));
  3381. case MTFSFM:
  3382. case MTBSFM:
  3383. if (!tape->capabilities.sprev)
  3384. return (-EIO);
  3385. retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
  3386. if (retval) return (retval);
  3387. count = (MTBSFM == mt_op ? 1 : -1);
  3388. return (idetape_space_over_filemarks(drive, MTFSF, count));
  3389. default:
  3390. printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
  3391. return (-EIO);
  3392. }
  3393. }
  3394. /*
  3395. * Our character device read / write functions.
  3396. *
  3397. * The tape is optimized to maximize throughput when it is transferring
  3398. * an integral number of the "continuous transfer limit", which is
  3399. * a parameter of the specific tape (26 KB on my particular tape).
  3400. * (32 kB for Onstream)
  3401. *
  3402. * As of version 1.3 of the driver, the character device provides an
  3403. * abstract continuous view of the media - any mix of block sizes (even 1
  3404. * byte) on the same backup/restore procedure is supported. The driver
  3405. * will internally convert the requests to the recommended transfer unit,
  3406. * so that an unmatch between the user's block size to the recommended
  3407. * size will only result in a (slightly) increased driver overhead, but
  3408. * will no longer hit performance.
  3409. * This is not applicable to Onstream.
  3410. */
  3411. static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
  3412. size_t count, loff_t *ppos)
  3413. {
  3414. struct ide_tape_obj *tape = ide_tape_f(file);
  3415. ide_drive_t *drive = tape->drive;
  3416. ssize_t bytes_read,temp, actually_read = 0, rc;
  3417. ssize_t ret = 0;
  3418. #if IDETAPE_DEBUG_LOG
  3419. if (tape->debug_level >= 3)
  3420. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
  3421. #endif /* IDETAPE_DEBUG_LOG */
  3422. if (tape->chrdev_direction != idetape_direction_read) {
  3423. if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
  3424. if (count > tape->tape_block_size &&
  3425. (count % tape->tape_block_size) == 0)
  3426. tape->user_bs_factor = count / tape->tape_block_size;
  3427. }
  3428. if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
  3429. return rc;
  3430. if (count == 0)
  3431. return (0);
  3432. if (tape->merge_stage_size) {
  3433. actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
  3434. if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
  3435. ret = -EFAULT;
  3436. buf += actually_read;
  3437. tape->merge_stage_size -= actually_read;
  3438. count -= actually_read;
  3439. }
  3440. while (count >= tape->stage_size) {
  3441. bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
  3442. if (bytes_read <= 0)
  3443. goto finish;
  3444. if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
  3445. ret = -EFAULT;
  3446. buf += bytes_read;
  3447. count -= bytes_read;
  3448. actually_read += bytes_read;
  3449. }
  3450. if (count) {
  3451. bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
  3452. if (bytes_read <= 0)
  3453. goto finish;
  3454. temp = min((unsigned long)count, (unsigned long)bytes_read);
  3455. if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
  3456. ret = -EFAULT;
  3457. actually_read += temp;
  3458. tape->merge_stage_size = bytes_read-temp;
  3459. }
  3460. finish:
  3461. if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
  3462. #if IDETAPE_DEBUG_LOG
  3463. if (tape->debug_level >= 2)
  3464. printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
  3465. #endif
  3466. idetape_space_over_filemarks(drive, MTFSF, 1);
  3467. return 0;
  3468. }
  3469. return (ret) ? ret : actually_read;
  3470. }
  3471. static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
  3472. size_t count, loff_t *ppos)
  3473. {
  3474. struct ide_tape_obj *tape = ide_tape_f(file);
  3475. ide_drive_t *drive = tape->drive;
  3476. ssize_t actually_written = 0;
  3477. ssize_t ret = 0;
  3478. /* The drive is write protected. */
  3479. if (tape->write_prot)
  3480. return -EACCES;
  3481. #if IDETAPE_DEBUG_LOG
  3482. if (tape->debug_level >= 3)
  3483. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
  3484. "count %Zd\n", count);
  3485. #endif /* IDETAPE_DEBUG_LOG */
  3486. /* Initialize write operation */
  3487. if (tape->chrdev_direction != idetape_direction_write) {
  3488. if (tape->chrdev_direction == idetape_direction_read)
  3489. idetape_discard_read_pipeline(drive, 1);
  3490. #if IDETAPE_DEBUG_BUGS
  3491. if (tape->merge_stage || tape->merge_stage_size) {
  3492. printk(KERN_ERR "ide-tape: merge_stage_size "
  3493. "should be 0 now\n");
  3494. tape->merge_stage_size = 0;
  3495. }
  3496. #endif /* IDETAPE_DEBUG_BUGS */
  3497. if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
  3498. return -ENOMEM;
  3499. tape->chrdev_direction = idetape_direction_write;
  3500. idetape_init_merge_stage(tape);
  3501. /*
  3502. * Issue a write 0 command to ensure that DSC handshake
  3503. * is switched from completion mode to buffer available
  3504. * mode.
  3505. * No point in issuing this if DSC overlap isn't supported,
  3506. * some drives (Seagate STT3401A) will return an error.
  3507. */
  3508. if (drive->dsc_overlap) {
  3509. ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
  3510. if (retval < 0) {
  3511. __idetape_kfree_stage(tape->merge_stage);
  3512. tape->merge_stage = NULL;
  3513. tape->chrdev_direction = idetape_direction_none;
  3514. return retval;
  3515. }
  3516. }
  3517. }
  3518. if (count == 0)
  3519. return (0);
  3520. if (tape->restart_speed_control_req)
  3521. idetape_restart_speed_control(drive);
  3522. if (tape->merge_stage_size) {
  3523. #if IDETAPE_DEBUG_BUGS
  3524. if (tape->merge_stage_size >= tape->stage_size) {
  3525. printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
  3526. tape->merge_stage_size = 0;
  3527. }
  3528. #endif /* IDETAPE_DEBUG_BUGS */
  3529. actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
  3530. if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
  3531. ret = -EFAULT;
  3532. buf += actually_written;
  3533. tape->merge_stage_size += actually_written;
  3534. count -= actually_written;
  3535. if (tape->merge_stage_size == tape->stage_size) {
  3536. ssize_t retval;
  3537. tape->merge_stage_size = 0;
  3538. retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
  3539. if (retval <= 0)
  3540. return (retval);
  3541. }
  3542. }
  3543. while (count >= tape->stage_size) {
  3544. ssize_t retval;
  3545. if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
  3546. ret = -EFAULT;
  3547. buf += tape->stage_size;
  3548. count -= tape->stage_size;
  3549. retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
  3550. actually_written += tape->stage_size;
  3551. if (retval <= 0)
  3552. return (retval);
  3553. }
  3554. if (count) {
  3555. actually_written += count;
  3556. if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
  3557. ret = -EFAULT;
  3558. tape->merge_stage_size += count;
  3559. }
  3560. return (ret) ? ret : actually_written;
  3561. }
  3562. static int idetape_write_filemark (ide_drive_t *drive)
  3563. {
  3564. idetape_pc_t pc;
  3565. /* Write a filemark */
  3566. idetape_create_write_filemark_cmd(drive, &pc, 1);
  3567. if (idetape_queue_pc_tail(drive, &pc)) {
  3568. printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
  3569. return -EIO;
  3570. }
  3571. return 0;
  3572. }
  3573. /*
  3574. * idetape_mtioctop is called from idetape_chrdev_ioctl when
  3575. * the general mtio MTIOCTOP ioctl is requested.
  3576. *
  3577. * We currently support the following mtio.h operations:
  3578. *
  3579. * MTFSF - Space over mt_count filemarks in the positive direction.
  3580. * The tape is positioned after the last spaced filemark.
  3581. *
  3582. * MTFSFM - Same as MTFSF, but the tape is positioned before the
  3583. * last filemark.
  3584. *
  3585. * MTBSF - Steps background over mt_count filemarks, tape is
  3586. * positioned before the last filemark.
  3587. *
  3588. * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
  3589. *
  3590. * Note:
  3591. *
  3592. * MTBSF and MTBSFM are not supported when the tape doesn't
  3593. * support spacing over filemarks in the reverse direction.
  3594. * In this case, MTFSFM is also usually not supported (it is
  3595. * supported in the rare case in which we crossed the filemark
  3596. * during our read-ahead pipelined operation mode).
  3597. *
  3598. * MTWEOF - Writes mt_count filemarks. Tape is positioned after
  3599. * the last written filemark.
  3600. *
  3601. * MTREW - Rewinds tape.
  3602. *
  3603. * MTLOAD - Loads the tape.
  3604. *
  3605. * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
  3606. * MTUNLOAD prevents further access until the media is replaced.
  3607. *
  3608. * MTNOP - Flushes tape buffers.
  3609. *
  3610. * MTRETEN - Retension media. This typically consists of one end
  3611. * to end pass on the media.
  3612. *
  3613. * MTEOM - Moves to the end of recorded data.
  3614. *
  3615. * MTERASE - Erases tape.
  3616. *
  3617. * MTSETBLK - Sets the user block size to mt_count bytes. If
  3618. * mt_count is 0, we will attempt to autodetect
  3619. * the block size.
  3620. *
  3621. * MTSEEK - Positions the tape in a specific block number, where
  3622. * each block is assumed to contain which user_block_size
  3623. * bytes.
  3624. *
  3625. * MTSETPART - Switches to another tape partition.
  3626. *
  3627. * MTLOCK - Locks the tape door.
  3628. *
  3629. * MTUNLOCK - Unlocks the tape door.
  3630. *
  3631. * The following commands are currently not supported:
  3632. *
  3633. * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
  3634. * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
  3635. */
  3636. static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
  3637. {
  3638. idetape_tape_t *tape = drive->driver_data;
  3639. idetape_pc_t pc;
  3640. int i,retval;
  3641. #if IDETAPE_DEBUG_LOG
  3642. if (tape->debug_level >= 1)
  3643. printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
  3644. "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
  3645. #endif /* IDETAPE_DEBUG_LOG */
  3646. /*
  3647. * Commands which need our pipelined read-ahead stages.
  3648. */
  3649. switch (mt_op) {
  3650. case MTFSF:
  3651. case MTFSFM:
  3652. case MTBSF:
  3653. case MTBSFM:
  3654. if (!mt_count)
  3655. return (0);
  3656. return (idetape_space_over_filemarks(drive,mt_op,mt_count));
  3657. default:
  3658. break;
  3659. }
  3660. switch (mt_op) {
  3661. case MTWEOF:
  3662. if (tape->write_prot)
  3663. return -EACCES;
  3664. idetape_discard_read_pipeline(drive, 1);
  3665. for (i = 0; i < mt_count; i++) {
  3666. retval = idetape_write_filemark(drive);
  3667. if (retval)
  3668. return retval;
  3669. }
  3670. return (0);
  3671. case MTREW:
  3672. idetape_discard_read_pipeline(drive, 0);
  3673. if (idetape_rewind_tape(drive))
  3674. return -EIO;
  3675. return 0;
  3676. case MTLOAD:
  3677. idetape_discard_read_pipeline(drive, 0);
  3678. idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
  3679. return (idetape_queue_pc_tail(drive, &pc));
  3680. case MTUNLOAD:
  3681. case MTOFFL:
  3682. /*
  3683. * If door is locked, attempt to unlock before
  3684. * attempting to eject.
  3685. */
  3686. if (tape->door_locked) {
  3687. if (idetape_create_prevent_cmd(drive, &pc, 0))
  3688. if (!idetape_queue_pc_tail(drive, &pc))
  3689. tape->door_locked = DOOR_UNLOCKED;
  3690. }
  3691. idetape_discard_read_pipeline(drive, 0);
  3692. idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
  3693. retval = idetape_queue_pc_tail(drive, &pc);
  3694. if (!retval)
  3695. clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
  3696. return retval;
  3697. case MTNOP:
  3698. idetape_discard_read_pipeline(drive, 0);
  3699. return (idetape_flush_tape_buffers(drive));
  3700. case MTRETEN:
  3701. idetape_discard_read_pipeline(drive, 0);
  3702. idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  3703. return (idetape_queue_pc_tail(drive, &pc));
  3704. case MTEOM:
  3705. idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
  3706. return (idetape_queue_pc_tail(drive, &pc));
  3707. case MTERASE:
  3708. (void) idetape_rewind_tape(drive);
  3709. idetape_create_erase_cmd(&pc);
  3710. return (idetape_queue_pc_tail(drive, &pc));
  3711. case MTSETBLK:
  3712. if (mt_count) {
  3713. if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
  3714. return -EIO;
  3715. tape->user_bs_factor = mt_count / tape->tape_block_size;
  3716. clear_bit(IDETAPE_DETECT_BS, &tape->flags);
  3717. } else
  3718. set_bit(IDETAPE_DETECT_BS, &tape->flags);
  3719. return 0;
  3720. case MTSEEK:
  3721. idetape_discard_read_pipeline(drive, 0);
  3722. return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
  3723. case MTSETPART:
  3724. idetape_discard_read_pipeline(drive, 0);
  3725. return (idetape_position_tape(drive, 0, mt_count, 0));
  3726. case MTFSR:
  3727. case MTBSR:
  3728. case MTLOCK:
  3729. if (!idetape_create_prevent_cmd(drive, &pc, 1))
  3730. return 0;
  3731. retval = idetape_queue_pc_tail(drive, &pc);
  3732. if (retval) return retval;
  3733. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  3734. return 0;
  3735. case MTUNLOCK:
  3736. if (!idetape_create_prevent_cmd(drive, &pc, 0))
  3737. return 0;
  3738. retval = idetape_queue_pc_tail(drive, &pc);
  3739. if (retval) return retval;
  3740. tape->door_locked = DOOR_UNLOCKED;
  3741. return 0;
  3742. default:
  3743. printk(KERN_ERR "ide-tape: MTIO operation %d not "
  3744. "supported\n", mt_op);
  3745. return (-EIO);
  3746. }
  3747. }
  3748. /*
  3749. * Our character device ioctls.
  3750. *
  3751. * General mtio.h magnetic io commands are supported here, and not in
  3752. * the corresponding block interface.
  3753. *
  3754. * The following ioctls are supported:
  3755. *
  3756. * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
  3757. *
  3758. * MTIOCGET - The mt_dsreg field in the returned mtget structure
  3759. * will be set to (user block size in bytes <<
  3760. * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
  3761. *
  3762. * The mt_blkno is set to the current user block number.
  3763. * The other mtget fields are not supported.
  3764. *
  3765. * MTIOCPOS - The current tape "block position" is returned. We
  3766. * assume that each block contains user_block_size
  3767. * bytes.
  3768. *
  3769. * Our own ide-tape ioctls are supported on both interfaces.
  3770. */
  3771. static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  3772. {
  3773. struct ide_tape_obj *tape = ide_tape_f(file);
  3774. ide_drive_t *drive = tape->drive;
  3775. struct mtop mtop;
  3776. struct mtget mtget;
  3777. struct mtpos mtpos;
  3778. int block_offset = 0, position = tape->first_frame_position;
  3779. void __user *argp = (void __user *)arg;
  3780. #if IDETAPE_DEBUG_LOG
  3781. if (tape->debug_level >= 3)
  3782. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
  3783. "cmd=%u\n", cmd);
  3784. #endif /* IDETAPE_DEBUG_LOG */
  3785. tape->restart_speed_control_req = 1;
  3786. if (tape->chrdev_direction == idetape_direction_write) {
  3787. idetape_empty_write_pipeline(drive);
  3788. idetape_flush_tape_buffers(drive);
  3789. }
  3790. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  3791. block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
  3792. if ((position = idetape_read_position(drive)) < 0)
  3793. return -EIO;
  3794. }
  3795. switch (cmd) {
  3796. case MTIOCTOP:
  3797. if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
  3798. return -EFAULT;
  3799. return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
  3800. case MTIOCGET:
  3801. memset(&mtget, 0, sizeof (struct mtget));
  3802. mtget.mt_type = MT_ISSCSI2;
  3803. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  3804. mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  3805. if (tape->drv_write_prot) {
  3806. mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
  3807. }
  3808. if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
  3809. return -EFAULT;
  3810. return 0;
  3811. case MTIOCPOS:
  3812. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  3813. if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
  3814. return -EFAULT;
  3815. return 0;
  3816. default:
  3817. if (tape->chrdev_direction == idetape_direction_read)
  3818. idetape_discard_read_pipeline(drive, 1);
  3819. return idetape_blkdev_ioctl(drive, cmd, arg);
  3820. }
  3821. }
  3822. static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
  3823. /*
  3824. * Our character device open function.
  3825. */
  3826. static int idetape_chrdev_open (struct inode *inode, struct file *filp)
  3827. {
  3828. unsigned int minor = iminor(inode), i = minor & ~0xc0;
  3829. ide_drive_t *drive;
  3830. idetape_tape_t *tape;
  3831. idetape_pc_t pc;
  3832. int retval;
  3833. /*
  3834. * We really want to do nonseekable_open(inode, filp); here, but some
  3835. * versions of tar incorrectly call lseek on tapes and bail out if that
  3836. * fails. So we disallow pread() and pwrite(), but permit lseeks.
  3837. */
  3838. filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  3839. #if IDETAPE_DEBUG_LOG
  3840. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
  3841. #endif /* IDETAPE_DEBUG_LOG */
  3842. if (i >= MAX_HWIFS * MAX_DRIVES)
  3843. return -ENXIO;
  3844. if (!(tape = ide_tape_chrdev_get(i)))
  3845. return -ENXIO;
  3846. drive = tape->drive;
  3847. filp->private_data = tape;
  3848. if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
  3849. retval = -EBUSY;
  3850. goto out_put_tape;
  3851. }
  3852. retval = idetape_wait_ready(drive, 60 * HZ);
  3853. if (retval) {
  3854. clear_bit(IDETAPE_BUSY, &tape->flags);
  3855. printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
  3856. goto out_put_tape;
  3857. }
  3858. idetape_read_position(drive);
  3859. if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
  3860. (void)idetape_rewind_tape(drive);
  3861. if (tape->chrdev_direction != idetape_direction_read)
  3862. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  3863. /* Read block size and write protect status from drive. */
  3864. idetape_get_blocksize_from_block_descriptor(drive);
  3865. /* Set write protect flag if device is opened as read-only. */
  3866. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  3867. tape->write_prot = 1;
  3868. else
  3869. tape->write_prot = tape->drv_write_prot;
  3870. /* Make sure drive isn't write protected if user wants to write. */
  3871. if (tape->write_prot) {
  3872. if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
  3873. (filp->f_flags & O_ACCMODE) == O_RDWR) {
  3874. clear_bit(IDETAPE_BUSY, &tape->flags);
  3875. retval = -EROFS;
  3876. goto out_put_tape;
  3877. }
  3878. }
  3879. /*
  3880. * Lock the tape drive door so user can't eject.
  3881. */
  3882. if (tape->chrdev_direction == idetape_direction_none) {
  3883. if (idetape_create_prevent_cmd(drive, &pc, 1)) {
  3884. if (!idetape_queue_pc_tail(drive, &pc)) {
  3885. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  3886. tape->door_locked = DOOR_LOCKED;
  3887. }
  3888. }
  3889. }
  3890. idetape_restart_speed_control(drive);
  3891. tape->restart_speed_control_req = 0;
  3892. return 0;
  3893. out_put_tape:
  3894. ide_tape_put(tape);
  3895. return retval;
  3896. }
  3897. static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
  3898. {
  3899. idetape_tape_t *tape = drive->driver_data;
  3900. idetape_empty_write_pipeline(drive);
  3901. tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
  3902. if (tape->merge_stage != NULL) {
  3903. idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
  3904. __idetape_kfree_stage(tape->merge_stage);
  3905. tape->merge_stage = NULL;
  3906. }
  3907. idetape_write_filemark(drive);
  3908. idetape_flush_tape_buffers(drive);
  3909. idetape_flush_tape_buffers(drive);
  3910. }
  3911. /*
  3912. * Our character device release function.
  3913. */
  3914. static int idetape_chrdev_release (struct inode *inode, struct file *filp)
  3915. {
  3916. struct ide_tape_obj *tape = ide_tape_f(filp);
  3917. ide_drive_t *drive = tape->drive;
  3918. idetape_pc_t pc;
  3919. unsigned int minor = iminor(inode);
  3920. lock_kernel();
  3921. tape = drive->driver_data;
  3922. #if IDETAPE_DEBUG_LOG
  3923. if (tape->debug_level >= 3)
  3924. printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
  3925. #endif /* IDETAPE_DEBUG_LOG */
  3926. if (tape->chrdev_direction == idetape_direction_write)
  3927. idetape_write_release(drive, minor);
  3928. if (tape->chrdev_direction == idetape_direction_read) {
  3929. if (minor < 128)
  3930. idetape_discard_read_pipeline(drive, 1);
  3931. else
  3932. idetape_wait_for_pipeline(drive);
  3933. }
  3934. if (tape->cache_stage != NULL) {
  3935. __idetape_kfree_stage(tape->cache_stage);
  3936. tape->cache_stage = NULL;
  3937. }
  3938. if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
  3939. (void) idetape_rewind_tape(drive);
  3940. if (tape->chrdev_direction == idetape_direction_none) {
  3941. if (tape->door_locked == DOOR_LOCKED) {
  3942. if (idetape_create_prevent_cmd(drive, &pc, 0)) {
  3943. if (!idetape_queue_pc_tail(drive, &pc))
  3944. tape->door_locked = DOOR_UNLOCKED;
  3945. }
  3946. }
  3947. }
  3948. clear_bit(IDETAPE_BUSY, &tape->flags);
  3949. ide_tape_put(tape);
  3950. unlock_kernel();
  3951. return 0;
  3952. }
  3953. /*
  3954. * idetape_identify_device is called to check the contents of the
  3955. * ATAPI IDENTIFY command results. We return:
  3956. *
  3957. * 1 If the tape can be supported by us, based on the information
  3958. * we have so far.
  3959. *
  3960. * 0 If this tape driver is not currently supported by us.
  3961. */
  3962. static int idetape_identify_device (ide_drive_t *drive)
  3963. {
  3964. struct idetape_id_gcw gcw;
  3965. struct hd_driveid *id = drive->id;
  3966. if (drive->id_read == 0)
  3967. return 1;
  3968. *((unsigned short *) &gcw) = id->config;
  3969. #if IDETAPE_DEBUG_INFO
  3970. printk(KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n");
  3971. printk(KERN_INFO "ide-tape: Protocol Type: ");
  3972. switch (gcw.protocol) {
  3973. case 0: case 1: printk("ATA\n");break;
  3974. case 2: printk("ATAPI\n");break;
  3975. case 3: printk("Reserved (Unknown to ide-tape)\n");break;
  3976. }
  3977. printk(KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
  3978. switch (gcw.device_type) {
  3979. case 0: printk("Direct-access Device\n");break;
  3980. case 1: printk("Streaming Tape Device\n");break;
  3981. case 2: case 3: case 4: printk("Reserved\n");break;
  3982. case 5: printk("CD-ROM Device\n");break;
  3983. case 6: printk("Reserved\n");
  3984. case 7: printk("Optical memory Device\n");break;
  3985. case 0x1f: printk("Unknown or no Device type\n");break;
  3986. default: printk("Reserved\n");
  3987. }
  3988. printk(KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n");
  3989. printk(KERN_INFO "ide-tape: Command Packet DRQ Type: ");
  3990. switch (gcw.drq_type) {
  3991. case 0: printk("Microprocessor DRQ\n");break;
  3992. case 1: printk("Interrupt DRQ\n");break;
  3993. case 2: printk("Accelerated DRQ\n");break;
  3994. case 3: printk("Reserved\n");break;
  3995. }
  3996. printk(KERN_INFO "ide-tape: Command Packet Size: ");
  3997. switch (gcw.packet_size) {
  3998. case 0: printk("12 bytes\n");break;
  3999. case 1: printk("16 bytes\n");break;
  4000. default: printk("Reserved\n");break;
  4001. }
  4002. #endif /* IDETAPE_DEBUG_INFO */
  4003. /* Check that we can support this device */
  4004. if (gcw.protocol !=2 )
  4005. printk(KERN_ERR "ide-tape: Protocol is not ATAPI\n");
  4006. else if (gcw.device_type != 1)
  4007. printk(KERN_ERR "ide-tape: Device type is not set to tape\n");
  4008. else if (!gcw.removable)
  4009. printk(KERN_ERR "ide-tape: The removable flag is not set\n");
  4010. else if (gcw.packet_size != 0) {
  4011. printk(KERN_ERR "ide-tape: Packet size is not 12 bytes long\n");
  4012. if (gcw.packet_size == 1)
  4013. printk(KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n");
  4014. } else
  4015. return 1;
  4016. return 0;
  4017. }
  4018. /*
  4019. * Use INQUIRY to get the firmware revision
  4020. */
  4021. static void idetape_get_inquiry_results (ide_drive_t *drive)
  4022. {
  4023. char *r;
  4024. idetape_tape_t *tape = drive->driver_data;
  4025. idetape_pc_t pc;
  4026. idetape_inquiry_result_t *inquiry;
  4027. idetape_create_inquiry_cmd(&pc);
  4028. if (idetape_queue_pc_tail(drive, &pc)) {
  4029. printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
  4030. return;
  4031. }
  4032. inquiry = (idetape_inquiry_result_t *) pc.buffer;
  4033. memcpy(tape->vendor_id, inquiry->vendor_id, 8);
  4034. memcpy(tape->product_id, inquiry->product_id, 16);
  4035. memcpy(tape->firmware_revision, inquiry->revision_level, 4);
  4036. ide_fixstring(tape->vendor_id, 10, 0);
  4037. ide_fixstring(tape->product_id, 18, 0);
  4038. ide_fixstring(tape->firmware_revision, 6, 0);
  4039. r = tape->firmware_revision;
  4040. if (*(r + 1) == '.')
  4041. tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
  4042. printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n", drive->name, tape->name, tape->vendor_id, tape->product_id, tape->firmware_revision);
  4043. }
  4044. /*
  4045. * idetape_get_mode_sense_results asks the tape about its various
  4046. * parameters. In particular, we will adjust our data transfer buffer
  4047. * size to the recommended value as returned by the tape.
  4048. */
  4049. static void idetape_get_mode_sense_results (ide_drive_t *drive)
  4050. {
  4051. idetape_tape_t *tape = drive->driver_data;
  4052. idetape_pc_t pc;
  4053. idetape_mode_parameter_header_t *header;
  4054. idetape_capabilities_page_t *capabilities;
  4055. idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
  4056. if (idetape_queue_pc_tail(drive, &pc)) {
  4057. printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
  4058. tape->tape_block_size = 512;
  4059. tape->capabilities.ctl = 52;
  4060. tape->capabilities.speed = 450;
  4061. tape->capabilities.buffer_size = 6 * 52;
  4062. return;
  4063. }
  4064. header = (idetape_mode_parameter_header_t *) pc.buffer;
  4065. capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
  4066. capabilities->max_speed = ntohs(capabilities->max_speed);
  4067. capabilities->ctl = ntohs(capabilities->ctl);
  4068. capabilities->speed = ntohs(capabilities->speed);
  4069. capabilities->buffer_size = ntohs(capabilities->buffer_size);
  4070. if (!capabilities->speed) {
  4071. printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
  4072. capabilities->speed = 650;
  4073. }
  4074. if (!capabilities->max_speed) {
  4075. printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
  4076. capabilities->max_speed = 650;
  4077. }
  4078. tape->capabilities = *capabilities; /* Save us a copy */
  4079. if (capabilities->blk512)
  4080. tape->tape_block_size = 512;
  4081. else if (capabilities->blk1024)
  4082. tape->tape_block_size = 1024;
  4083. #if IDETAPE_DEBUG_INFO
  4084. printk(KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n");
  4085. printk(KERN_INFO "ide-tape: Mode Parameter Header:\n");
  4086. printk(KERN_INFO "ide-tape: Mode Data Length - %d\n",header->mode_data_length);
  4087. printk(KERN_INFO "ide-tape: Medium Type - %d\n",header->medium_type);
  4088. printk(KERN_INFO "ide-tape: Device Specific Parameter - %d\n",header->dsp);
  4089. printk(KERN_INFO "ide-tape: Block Descriptor Length - %d\n",header->bdl);
  4090. printk(KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n");
  4091. printk(KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code);
  4092. printk(KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length);
  4093. printk(KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No");
  4094. printk(KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
  4095. printk(KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
  4096. printk(KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
  4097. printk(KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
  4098. printk(KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
  4099. printk(KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
  4100. printk(KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
  4101. printk(KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
  4102. printk(KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
  4103. printk(KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
  4104. printk(KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
  4105. printk(KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No");
  4106. printk(KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed);
  4107. printk(KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl);
  4108. printk(KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed);
  4109. printk(KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512);
  4110. #endif /* IDETAPE_DEBUG_INFO */
  4111. }
  4112. /*
  4113. * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
  4114. * and if it succeeds sets the tape block size with the reported value
  4115. */
  4116. static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
  4117. {
  4118. idetape_tape_t *tape = drive->driver_data;
  4119. idetape_pc_t pc;
  4120. idetape_mode_parameter_header_t *header;
  4121. idetape_parameter_block_descriptor_t *block_descrp;
  4122. idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
  4123. if (idetape_queue_pc_tail(drive, &pc)) {
  4124. printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
  4125. if (tape->tape_block_size == 0) {
  4126. printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
  4127. tape->tape_block_size = 32768;
  4128. }
  4129. return;
  4130. }
  4131. header = (idetape_mode_parameter_header_t *) pc.buffer;
  4132. block_descrp = (idetape_parameter_block_descriptor_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t));
  4133. tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
  4134. tape->drv_write_prot = (header->dsp & 0x80) >> 7;
  4135. #if IDETAPE_DEBUG_INFO
  4136. printk(KERN_INFO "ide-tape: Adjusted block size - %d\n", tape->tape_block_size);
  4137. #endif /* IDETAPE_DEBUG_INFO */
  4138. }
  4139. #ifdef CONFIG_IDE_PROC_FS
  4140. static void idetape_add_settings (ide_drive_t *drive)
  4141. {
  4142. idetape_tape_t *tape = drive->driver_data;
  4143. /*
  4144. * drive setting name read/write data type min max mul_factor div_factor data pointer set function
  4145. */
  4146. ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
  4147. ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
  4148. ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
  4149. ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
  4150. ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
  4151. ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
  4152. ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
  4153. ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
  4154. 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);
  4155. ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
  4156. ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
  4157. ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
  4158. ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
  4159. ide_add_setting(drive, "debug_level", SETTING_RW, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
  4160. }
  4161. #else
  4162. static inline void idetape_add_settings(ide_drive_t *drive) { ; }
  4163. #endif
  4164. /*
  4165. * ide_setup is called to:
  4166. *
  4167. * 1. Initialize our various state variables.
  4168. * 2. Ask the tape for its capabilities.
  4169. * 3. Allocate a buffer which will be used for data
  4170. * transfer. The buffer size is chosen based on
  4171. * the recommendation which we received in step (2).
  4172. *
  4173. * Note that at this point ide.c already assigned us an irq, so that
  4174. * we can queue requests here and wait for their completion.
  4175. */
  4176. static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
  4177. {
  4178. unsigned long t1, tmid, tn, t;
  4179. int speed;
  4180. struct idetape_id_gcw gcw;
  4181. int stage_size;
  4182. struct sysinfo si;
  4183. spin_lock_init(&tape->spinlock);
  4184. drive->dsc_overlap = 1;
  4185. if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
  4186. printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
  4187. tape->name);
  4188. drive->dsc_overlap = 0;
  4189. }
  4190. /* Seagate Travan drives do not support DSC overlap. */
  4191. if (strstr(drive->id->model, "Seagate STT3401"))
  4192. drive->dsc_overlap = 0;
  4193. tape->minor = minor;
  4194. tape->name[0] = 'h';
  4195. tape->name[1] = 't';
  4196. tape->name[2] = '0' + minor;
  4197. tape->chrdev_direction = idetape_direction_none;
  4198. tape->pc = tape->pc_stack;
  4199. tape->max_insert_speed = 10000;
  4200. tape->speed_control = 1;
  4201. *((unsigned short *) &gcw) = drive->id->config;
  4202. if (gcw.drq_type == 1)
  4203. set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
  4204. tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
  4205. idetape_get_inquiry_results(drive);
  4206. idetape_get_mode_sense_results(drive);
  4207. idetape_get_blocksize_from_block_descriptor(drive);
  4208. tape->user_bs_factor = 1;
  4209. tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
  4210. while (tape->stage_size > 0xffff) {
  4211. printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
  4212. tape->capabilities.ctl /= 2;
  4213. tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
  4214. }
  4215. stage_size = tape->stage_size;
  4216. tape->pages_per_stage = stage_size / PAGE_SIZE;
  4217. if (stage_size % PAGE_SIZE) {
  4218. tape->pages_per_stage++;
  4219. tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
  4220. }
  4221. /*
  4222. * Select the "best" DSC read/write polling frequency
  4223. * and pipeline size.
  4224. */
  4225. speed = max(tape->capabilities.speed, tape->capabilities.max_speed);
  4226. tape->max_stages = speed * 1000 * 10 / tape->stage_size;
  4227. /*
  4228. * Limit memory use for pipeline to 10% of physical memory
  4229. */
  4230. si_meminfo(&si);
  4231. if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
  4232. tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
  4233. tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
  4234. tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
  4235. tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
  4236. if (tape->max_stages == 0)
  4237. tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
  4238. t1 = (tape->stage_size * HZ) / (speed * 1000);
  4239. tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
  4240. tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
  4241. if (tape->max_stages)
  4242. t = tn;
  4243. else
  4244. t = t1;
  4245. /*
  4246. * Ensure that the number we got makes sense; limit
  4247. * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  4248. */
  4249. tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
  4250. printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
  4251. "%dkB pipeline, %lums tDSC%s\n",
  4252. drive->name, tape->name, tape->capabilities.speed,
  4253. (tape->capabilities.buffer_size * 512) / tape->stage_size,
  4254. tape->stage_size / 1024,
  4255. tape->max_stages * tape->stage_size / 1024,
  4256. tape->best_dsc_rw_frequency * 1000 / HZ,
  4257. drive->using_dma ? ", DMA":"");
  4258. idetape_add_settings(drive);
  4259. }
  4260. static void ide_tape_remove(ide_drive_t *drive)
  4261. {
  4262. idetape_tape_t *tape = drive->driver_data;
  4263. ide_proc_unregister_driver(drive, tape->driver);
  4264. ide_unregister_region(tape->disk);
  4265. ide_tape_put(tape);
  4266. }
  4267. static void ide_tape_release(struct kref *kref)
  4268. {
  4269. struct ide_tape_obj *tape = to_ide_tape(kref);
  4270. ide_drive_t *drive = tape->drive;
  4271. struct gendisk *g = tape->disk;
  4272. BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
  4273. drive->dsc_overlap = 0;
  4274. drive->driver_data = NULL;
  4275. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
  4276. device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
  4277. idetape_devs[tape->minor] = NULL;
  4278. g->private_data = NULL;
  4279. put_disk(g);
  4280. kfree(tape);
  4281. }
  4282. #ifdef CONFIG_IDE_PROC_FS
  4283. static int proc_idetape_read_name
  4284. (char *page, char **start, off_t off, int count, int *eof, void *data)
  4285. {
  4286. ide_drive_t *drive = (ide_drive_t *) data;
  4287. idetape_tape_t *tape = drive->driver_data;
  4288. char *out = page;
  4289. int len;
  4290. len = sprintf(out, "%s\n", tape->name);
  4291. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  4292. }
  4293. static ide_proc_entry_t idetape_proc[] = {
  4294. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  4295. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  4296. { NULL, 0, NULL, NULL }
  4297. };
  4298. #endif
  4299. static int ide_tape_probe(ide_drive_t *);
  4300. static ide_driver_t idetape_driver = {
  4301. .gen_driver = {
  4302. .owner = THIS_MODULE,
  4303. .name = "ide-tape",
  4304. .bus = &ide_bus_type,
  4305. },
  4306. .probe = ide_tape_probe,
  4307. .remove = ide_tape_remove,
  4308. .version = IDETAPE_VERSION,
  4309. .media = ide_tape,
  4310. .supports_dsc_overlap = 1,
  4311. .do_request = idetape_do_request,
  4312. .end_request = idetape_end_request,
  4313. .error = __ide_error,
  4314. .abort = __ide_abort,
  4315. #ifdef CONFIG_IDE_PROC_FS
  4316. .proc = idetape_proc,
  4317. #endif
  4318. };
  4319. /*
  4320. * Our character device supporting functions, passed to register_chrdev.
  4321. */
  4322. static const struct file_operations idetape_fops = {
  4323. .owner = THIS_MODULE,
  4324. .read = idetape_chrdev_read,
  4325. .write = idetape_chrdev_write,
  4326. .ioctl = idetape_chrdev_ioctl,
  4327. .open = idetape_chrdev_open,
  4328. .release = idetape_chrdev_release,
  4329. };
  4330. static int idetape_open(struct inode *inode, struct file *filp)
  4331. {
  4332. struct gendisk *disk = inode->i_bdev->bd_disk;
  4333. struct ide_tape_obj *tape;
  4334. if (!(tape = ide_tape_get(disk)))
  4335. return -ENXIO;
  4336. return 0;
  4337. }
  4338. static int idetape_release(struct inode *inode, struct file *filp)
  4339. {
  4340. struct gendisk *disk = inode->i_bdev->bd_disk;
  4341. struct ide_tape_obj *tape = ide_tape_g(disk);
  4342. ide_tape_put(tape);
  4343. return 0;
  4344. }
  4345. static int idetape_ioctl(struct inode *inode, struct file *file,
  4346. unsigned int cmd, unsigned long arg)
  4347. {
  4348. struct block_device *bdev = inode->i_bdev;
  4349. struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
  4350. ide_drive_t *drive = tape->drive;
  4351. int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  4352. if (err == -EINVAL)
  4353. err = idetape_blkdev_ioctl(drive, cmd, arg);
  4354. return err;
  4355. }
  4356. static struct block_device_operations idetape_block_ops = {
  4357. .owner = THIS_MODULE,
  4358. .open = idetape_open,
  4359. .release = idetape_release,
  4360. .ioctl = idetape_ioctl,
  4361. };
  4362. static int ide_tape_probe(ide_drive_t *drive)
  4363. {
  4364. idetape_tape_t *tape;
  4365. struct gendisk *g;
  4366. int minor;
  4367. if (!strstr("ide-tape", drive->driver_req))
  4368. goto failed;
  4369. if (!drive->present)
  4370. goto failed;
  4371. if (drive->media != ide_tape)
  4372. goto failed;
  4373. if (!idetape_identify_device (drive)) {
  4374. printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
  4375. goto failed;
  4376. }
  4377. if (drive->scsi) {
  4378. printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
  4379. goto failed;
  4380. }
  4381. if (strstr(drive->id->model, "OnStream DI-")) {
  4382. printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
  4383. printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
  4384. }
  4385. tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
  4386. if (tape == NULL) {
  4387. printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
  4388. goto failed;
  4389. }
  4390. g = alloc_disk(1 << PARTN_BITS);
  4391. if (!g)
  4392. goto out_free_tape;
  4393. ide_init_disk(g, drive);
  4394. ide_proc_register_driver(drive, &idetape_driver);
  4395. kref_init(&tape->kref);
  4396. tape->drive = drive;
  4397. tape->driver = &idetape_driver;
  4398. tape->disk = g;
  4399. g->private_data = &tape->driver;
  4400. drive->driver_data = tape;
  4401. mutex_lock(&idetape_ref_mutex);
  4402. for (minor = 0; idetape_devs[minor]; minor++)
  4403. ;
  4404. idetape_devs[minor] = tape;
  4405. mutex_unlock(&idetape_ref_mutex);
  4406. idetape_setup(drive, tape, minor);
  4407. device_create(idetape_sysfs_class, &drive->gendev,
  4408. MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
  4409. device_create(idetape_sysfs_class, &drive->gendev,
  4410. MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
  4411. g->fops = &idetape_block_ops;
  4412. ide_register_region(g);
  4413. return 0;
  4414. out_free_tape:
  4415. kfree(tape);
  4416. failed:
  4417. return -ENODEV;
  4418. }
  4419. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  4420. MODULE_LICENSE("GPL");
  4421. static void __exit idetape_exit (void)
  4422. {
  4423. driver_unregister(&idetape_driver.gen_driver);
  4424. class_destroy(idetape_sysfs_class);
  4425. unregister_chrdev(IDETAPE_MAJOR, "ht");
  4426. }
  4427. static int __init idetape_init(void)
  4428. {
  4429. int error = 1;
  4430. idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
  4431. if (IS_ERR(idetape_sysfs_class)) {
  4432. idetape_sysfs_class = NULL;
  4433. printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
  4434. error = -EBUSY;
  4435. goto out;
  4436. }
  4437. if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
  4438. printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
  4439. error = -EBUSY;
  4440. goto out_free_class;
  4441. }
  4442. error = driver_register(&idetape_driver.gen_driver);
  4443. if (error)
  4444. goto out_free_driver;
  4445. return 0;
  4446. out_free_driver:
  4447. driver_unregister(&idetape_driver.gen_driver);
  4448. out_free_class:
  4449. class_destroy(idetape_sysfs_class);
  4450. out:
  4451. return error;
  4452. }
  4453. MODULE_ALIAS("ide:*m-tape*");
  4454. module_init(idetape_init);
  4455. module_exit(idetape_exit);
  4456. MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);