libata-core.c 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961
  1. /*
  2. * libata-core.c - helper library for ATA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2004 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from http://www.t13.org/ and
  31. * http://www.sata-io.org/
  32. *
  33. */
  34. #include <linux/config.h>
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/pci.h>
  38. #include <linux/init.h>
  39. #include <linux/list.h>
  40. #include <linux/mm.h>
  41. #include <linux/highmem.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/blkdev.h>
  44. #include <linux/delay.h>
  45. #include <linux/timer.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/completion.h>
  48. #include <linux/suspend.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/jiffies.h>
  51. #include <linux/scatterlist.h>
  52. #include <scsi/scsi.h>
  53. #include "scsi_priv.h"
  54. #include <scsi/scsi_cmnd.h>
  55. #include <scsi/scsi_host.h>
  56. #include <linux/libata.h>
  57. #include <asm/io.h>
  58. #include <asm/semaphore.h>
  59. #include <asm/byteorder.h>
  60. #include "libata.h"
  61. static unsigned int ata_dev_init_params(struct ata_port *ap,
  62. struct ata_device *dev,
  63. u16 heads,
  64. u16 sectors);
  65. static void ata_set_mode(struct ata_port *ap);
  66. static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
  67. struct ata_device *dev);
  68. static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev);
  69. static unsigned int ata_unique_id = 1;
  70. static struct workqueue_struct *ata_wq;
  71. int atapi_enabled = 1;
  72. module_param(atapi_enabled, int, 0444);
  73. MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
  74. int libata_fua = 0;
  75. module_param_named(fua, libata_fua, int, 0444);
  76. MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
  77. MODULE_AUTHOR("Jeff Garzik");
  78. MODULE_DESCRIPTION("Library module for ATA devices");
  79. MODULE_LICENSE("GPL");
  80. MODULE_VERSION(DRV_VERSION);
  81. /**
  82. * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
  83. * @tf: Taskfile to convert
  84. * @fis: Buffer into which data will output
  85. * @pmp: Port multiplier port
  86. *
  87. * Converts a standard ATA taskfile to a Serial ATA
  88. * FIS structure (Register - Host to Device).
  89. *
  90. * LOCKING:
  91. * Inherited from caller.
  92. */
  93. void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
  94. {
  95. fis[0] = 0x27; /* Register - Host to Device FIS */
  96. fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
  97. bit 7 indicates Command FIS */
  98. fis[2] = tf->command;
  99. fis[3] = tf->feature;
  100. fis[4] = tf->lbal;
  101. fis[5] = tf->lbam;
  102. fis[6] = tf->lbah;
  103. fis[7] = tf->device;
  104. fis[8] = tf->hob_lbal;
  105. fis[9] = tf->hob_lbam;
  106. fis[10] = tf->hob_lbah;
  107. fis[11] = tf->hob_feature;
  108. fis[12] = tf->nsect;
  109. fis[13] = tf->hob_nsect;
  110. fis[14] = 0;
  111. fis[15] = tf->ctl;
  112. fis[16] = 0;
  113. fis[17] = 0;
  114. fis[18] = 0;
  115. fis[19] = 0;
  116. }
  117. /**
  118. * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
  119. * @fis: Buffer from which data will be input
  120. * @tf: Taskfile to output
  121. *
  122. * Converts a serial ATA FIS structure to a standard ATA taskfile.
  123. *
  124. * LOCKING:
  125. * Inherited from caller.
  126. */
  127. void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
  128. {
  129. tf->command = fis[2]; /* status */
  130. tf->feature = fis[3]; /* error */
  131. tf->lbal = fis[4];
  132. tf->lbam = fis[5];
  133. tf->lbah = fis[6];
  134. tf->device = fis[7];
  135. tf->hob_lbal = fis[8];
  136. tf->hob_lbam = fis[9];
  137. tf->hob_lbah = fis[10];
  138. tf->nsect = fis[12];
  139. tf->hob_nsect = fis[13];
  140. }
  141. static const u8 ata_rw_cmds[] = {
  142. /* pio multi */
  143. ATA_CMD_READ_MULTI,
  144. ATA_CMD_WRITE_MULTI,
  145. ATA_CMD_READ_MULTI_EXT,
  146. ATA_CMD_WRITE_MULTI_EXT,
  147. 0,
  148. 0,
  149. 0,
  150. ATA_CMD_WRITE_MULTI_FUA_EXT,
  151. /* pio */
  152. ATA_CMD_PIO_READ,
  153. ATA_CMD_PIO_WRITE,
  154. ATA_CMD_PIO_READ_EXT,
  155. ATA_CMD_PIO_WRITE_EXT,
  156. 0,
  157. 0,
  158. 0,
  159. 0,
  160. /* dma */
  161. ATA_CMD_READ,
  162. ATA_CMD_WRITE,
  163. ATA_CMD_READ_EXT,
  164. ATA_CMD_WRITE_EXT,
  165. 0,
  166. 0,
  167. 0,
  168. ATA_CMD_WRITE_FUA_EXT
  169. };
  170. /**
  171. * ata_rwcmd_protocol - set taskfile r/w commands and protocol
  172. * @qc: command to examine and configure
  173. *
  174. * Examine the device configuration and tf->flags to calculate
  175. * the proper read/write commands and protocol to use.
  176. *
  177. * LOCKING:
  178. * caller.
  179. */
  180. int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
  181. {
  182. struct ata_taskfile *tf = &qc->tf;
  183. struct ata_device *dev = qc->dev;
  184. u8 cmd;
  185. int index, fua, lba48, write;
  186. fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
  187. lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
  188. write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
  189. if (dev->flags & ATA_DFLAG_PIO) {
  190. tf->protocol = ATA_PROT_PIO;
  191. index = dev->multi_count ? 0 : 8;
  192. } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
  193. /* Unable to use DMA due to host limitation */
  194. tf->protocol = ATA_PROT_PIO;
  195. index = dev->multi_count ? 0 : 8;
  196. } else {
  197. tf->protocol = ATA_PROT_DMA;
  198. index = 16;
  199. }
  200. cmd = ata_rw_cmds[index + fua + lba48 + write];
  201. if (cmd) {
  202. tf->command = cmd;
  203. return 0;
  204. }
  205. return -1;
  206. }
  207. /**
  208. * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
  209. * @pio_mask: pio_mask
  210. * @mwdma_mask: mwdma_mask
  211. * @udma_mask: udma_mask
  212. *
  213. * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
  214. * unsigned int xfer_mask.
  215. *
  216. * LOCKING:
  217. * None.
  218. *
  219. * RETURNS:
  220. * Packed xfer_mask.
  221. */
  222. static unsigned int ata_pack_xfermask(unsigned int pio_mask,
  223. unsigned int mwdma_mask,
  224. unsigned int udma_mask)
  225. {
  226. return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
  227. ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
  228. ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
  229. }
  230. /**
  231. * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
  232. * @xfer_mask: xfer_mask to unpack
  233. * @pio_mask: resulting pio_mask
  234. * @mwdma_mask: resulting mwdma_mask
  235. * @udma_mask: resulting udma_mask
  236. *
  237. * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
  238. * Any NULL distination masks will be ignored.
  239. */
  240. static void ata_unpack_xfermask(unsigned int xfer_mask,
  241. unsigned int *pio_mask,
  242. unsigned int *mwdma_mask,
  243. unsigned int *udma_mask)
  244. {
  245. if (pio_mask)
  246. *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
  247. if (mwdma_mask)
  248. *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
  249. if (udma_mask)
  250. *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
  251. }
  252. static const struct ata_xfer_ent {
  253. int shift, bits;
  254. u8 base;
  255. } ata_xfer_tbl[] = {
  256. { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
  257. { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
  258. { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
  259. { -1, },
  260. };
  261. /**
  262. * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
  263. * @xfer_mask: xfer_mask of interest
  264. *
  265. * Return matching XFER_* value for @xfer_mask. Only the highest
  266. * bit of @xfer_mask is considered.
  267. *
  268. * LOCKING:
  269. * None.
  270. *
  271. * RETURNS:
  272. * Matching XFER_* value, 0 if no match found.
  273. */
  274. static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
  275. {
  276. int highbit = fls(xfer_mask) - 1;
  277. const struct ata_xfer_ent *ent;
  278. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  279. if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
  280. return ent->base + highbit - ent->shift;
  281. return 0;
  282. }
  283. /**
  284. * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
  285. * @xfer_mode: XFER_* of interest
  286. *
  287. * Return matching xfer_mask for @xfer_mode.
  288. *
  289. * LOCKING:
  290. * None.
  291. *
  292. * RETURNS:
  293. * Matching xfer_mask, 0 if no match found.
  294. */
  295. static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
  296. {
  297. const struct ata_xfer_ent *ent;
  298. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  299. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  300. return 1 << (ent->shift + xfer_mode - ent->base);
  301. return 0;
  302. }
  303. /**
  304. * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
  305. * @xfer_mode: XFER_* of interest
  306. *
  307. * Return matching xfer_shift for @xfer_mode.
  308. *
  309. * LOCKING:
  310. * None.
  311. *
  312. * RETURNS:
  313. * Matching xfer_shift, -1 if no match found.
  314. */
  315. static int ata_xfer_mode2shift(unsigned int xfer_mode)
  316. {
  317. const struct ata_xfer_ent *ent;
  318. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  319. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  320. return ent->shift;
  321. return -1;
  322. }
  323. /**
  324. * ata_mode_string - convert xfer_mask to string
  325. * @xfer_mask: mask of bits supported; only highest bit counts.
  326. *
  327. * Determine string which represents the highest speed
  328. * (highest bit in @modemask).
  329. *
  330. * LOCKING:
  331. * None.
  332. *
  333. * RETURNS:
  334. * Constant C string representing highest speed listed in
  335. * @mode_mask, or the constant C string "<n/a>".
  336. */
  337. static const char *ata_mode_string(unsigned int xfer_mask)
  338. {
  339. static const char * const xfer_mode_str[] = {
  340. "PIO0",
  341. "PIO1",
  342. "PIO2",
  343. "PIO3",
  344. "PIO4",
  345. "MWDMA0",
  346. "MWDMA1",
  347. "MWDMA2",
  348. "UDMA/16",
  349. "UDMA/25",
  350. "UDMA/33",
  351. "UDMA/44",
  352. "UDMA/66",
  353. "UDMA/100",
  354. "UDMA/133",
  355. "UDMA7",
  356. };
  357. int highbit;
  358. highbit = fls(xfer_mask) - 1;
  359. if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
  360. return xfer_mode_str[highbit];
  361. return "<n/a>";
  362. }
  363. static const char *sata_spd_string(unsigned int spd)
  364. {
  365. static const char * const spd_str[] = {
  366. "1.5 Gbps",
  367. "3.0 Gbps",
  368. };
  369. if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
  370. return "<unknown>";
  371. return spd_str[spd - 1];
  372. }
  373. static void ata_dev_disable(struct ata_port *ap, struct ata_device *dev)
  374. {
  375. if (ata_dev_enabled(dev)) {
  376. printk(KERN_WARNING "ata%u: dev %u disabled\n",
  377. ap->id, dev->devno);
  378. dev->class++;
  379. }
  380. }
  381. /**
  382. * ata_pio_devchk - PATA device presence detection
  383. * @ap: ATA channel to examine
  384. * @device: Device to examine (starting at zero)
  385. *
  386. * This technique was originally described in
  387. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  388. * later found its way into the ATA/ATAPI spec.
  389. *
  390. * Write a pattern to the ATA shadow registers,
  391. * and if a device is present, it will respond by
  392. * correctly storing and echoing back the
  393. * ATA shadow register contents.
  394. *
  395. * LOCKING:
  396. * caller.
  397. */
  398. static unsigned int ata_pio_devchk(struct ata_port *ap,
  399. unsigned int device)
  400. {
  401. struct ata_ioports *ioaddr = &ap->ioaddr;
  402. u8 nsect, lbal;
  403. ap->ops->dev_select(ap, device);
  404. outb(0x55, ioaddr->nsect_addr);
  405. outb(0xaa, ioaddr->lbal_addr);
  406. outb(0xaa, ioaddr->nsect_addr);
  407. outb(0x55, ioaddr->lbal_addr);
  408. outb(0x55, ioaddr->nsect_addr);
  409. outb(0xaa, ioaddr->lbal_addr);
  410. nsect = inb(ioaddr->nsect_addr);
  411. lbal = inb(ioaddr->lbal_addr);
  412. if ((nsect == 0x55) && (lbal == 0xaa))
  413. return 1; /* we found a device */
  414. return 0; /* nothing found */
  415. }
  416. /**
  417. * ata_mmio_devchk - PATA device presence detection
  418. * @ap: ATA channel to examine
  419. * @device: Device to examine (starting at zero)
  420. *
  421. * This technique was originally described in
  422. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  423. * later found its way into the ATA/ATAPI spec.
  424. *
  425. * Write a pattern to the ATA shadow registers,
  426. * and if a device is present, it will respond by
  427. * correctly storing and echoing back the
  428. * ATA shadow register contents.
  429. *
  430. * LOCKING:
  431. * caller.
  432. */
  433. static unsigned int ata_mmio_devchk(struct ata_port *ap,
  434. unsigned int device)
  435. {
  436. struct ata_ioports *ioaddr = &ap->ioaddr;
  437. u8 nsect, lbal;
  438. ap->ops->dev_select(ap, device);
  439. writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
  440. writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
  441. writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
  442. writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
  443. writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
  444. writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
  445. nsect = readb((void __iomem *) ioaddr->nsect_addr);
  446. lbal = readb((void __iomem *) ioaddr->lbal_addr);
  447. if ((nsect == 0x55) && (lbal == 0xaa))
  448. return 1; /* we found a device */
  449. return 0; /* nothing found */
  450. }
  451. /**
  452. * ata_devchk - PATA device presence detection
  453. * @ap: ATA channel to examine
  454. * @device: Device to examine (starting at zero)
  455. *
  456. * Dispatch ATA device presence detection, depending
  457. * on whether we are using PIO or MMIO to talk to the
  458. * ATA shadow registers.
  459. *
  460. * LOCKING:
  461. * caller.
  462. */
  463. static unsigned int ata_devchk(struct ata_port *ap,
  464. unsigned int device)
  465. {
  466. if (ap->flags & ATA_FLAG_MMIO)
  467. return ata_mmio_devchk(ap, device);
  468. return ata_pio_devchk(ap, device);
  469. }
  470. /**
  471. * ata_dev_classify - determine device type based on ATA-spec signature
  472. * @tf: ATA taskfile register set for device to be identified
  473. *
  474. * Determine from taskfile register contents whether a device is
  475. * ATA or ATAPI, as per "Signature and persistence" section
  476. * of ATA/PI spec (volume 1, sect 5.14).
  477. *
  478. * LOCKING:
  479. * None.
  480. *
  481. * RETURNS:
  482. * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
  483. * the event of failure.
  484. */
  485. unsigned int ata_dev_classify(const struct ata_taskfile *tf)
  486. {
  487. /* Apple's open source Darwin code hints that some devices only
  488. * put a proper signature into the LBA mid/high registers,
  489. * So, we only check those. It's sufficient for uniqueness.
  490. */
  491. if (((tf->lbam == 0) && (tf->lbah == 0)) ||
  492. ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
  493. DPRINTK("found ATA device by sig\n");
  494. return ATA_DEV_ATA;
  495. }
  496. if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
  497. ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
  498. DPRINTK("found ATAPI device by sig\n");
  499. return ATA_DEV_ATAPI;
  500. }
  501. DPRINTK("unknown device\n");
  502. return ATA_DEV_UNKNOWN;
  503. }
  504. /**
  505. * ata_dev_try_classify - Parse returned ATA device signature
  506. * @ap: ATA channel to examine
  507. * @device: Device to examine (starting at zero)
  508. * @r_err: Value of error register on completion
  509. *
  510. * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
  511. * an ATA/ATAPI-defined set of values is placed in the ATA
  512. * shadow registers, indicating the results of device detection
  513. * and diagnostics.
  514. *
  515. * Select the ATA device, and read the values from the ATA shadow
  516. * registers. Then parse according to the Error register value,
  517. * and the spec-defined values examined by ata_dev_classify().
  518. *
  519. * LOCKING:
  520. * caller.
  521. *
  522. * RETURNS:
  523. * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
  524. */
  525. static unsigned int
  526. ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
  527. {
  528. struct ata_taskfile tf;
  529. unsigned int class;
  530. u8 err;
  531. ap->ops->dev_select(ap, device);
  532. memset(&tf, 0, sizeof(tf));
  533. ap->ops->tf_read(ap, &tf);
  534. err = tf.feature;
  535. if (r_err)
  536. *r_err = err;
  537. /* see if device passed diags */
  538. if (err == 1)
  539. /* do nothing */ ;
  540. else if ((device == 0) && (err == 0x81))
  541. /* do nothing */ ;
  542. else
  543. return ATA_DEV_NONE;
  544. /* determine if device is ATA or ATAPI */
  545. class = ata_dev_classify(&tf);
  546. if (class == ATA_DEV_UNKNOWN)
  547. return ATA_DEV_NONE;
  548. if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
  549. return ATA_DEV_NONE;
  550. return class;
  551. }
  552. /**
  553. * ata_id_string - Convert IDENTIFY DEVICE page into string
  554. * @id: IDENTIFY DEVICE results we will examine
  555. * @s: string into which data is output
  556. * @ofs: offset into identify device page
  557. * @len: length of string to return. must be an even number.
  558. *
  559. * The strings in the IDENTIFY DEVICE page are broken up into
  560. * 16-bit chunks. Run through the string, and output each
  561. * 8-bit chunk linearly, regardless of platform.
  562. *
  563. * LOCKING:
  564. * caller.
  565. */
  566. void ata_id_string(const u16 *id, unsigned char *s,
  567. unsigned int ofs, unsigned int len)
  568. {
  569. unsigned int c;
  570. while (len > 0) {
  571. c = id[ofs] >> 8;
  572. *s = c;
  573. s++;
  574. c = id[ofs] & 0xff;
  575. *s = c;
  576. s++;
  577. ofs++;
  578. len -= 2;
  579. }
  580. }
  581. /**
  582. * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
  583. * @id: IDENTIFY DEVICE results we will examine
  584. * @s: string into which data is output
  585. * @ofs: offset into identify device page
  586. * @len: length of string to return. must be an odd number.
  587. *
  588. * This function is identical to ata_id_string except that it
  589. * trims trailing spaces and terminates the resulting string with
  590. * null. @len must be actual maximum length (even number) + 1.
  591. *
  592. * LOCKING:
  593. * caller.
  594. */
  595. void ata_id_c_string(const u16 *id, unsigned char *s,
  596. unsigned int ofs, unsigned int len)
  597. {
  598. unsigned char *p;
  599. WARN_ON(!(len & 1));
  600. ata_id_string(id, s, ofs, len - 1);
  601. p = s + strnlen(s, len - 1);
  602. while (p > s && p[-1] == ' ')
  603. p--;
  604. *p = '\0';
  605. }
  606. static u64 ata_id_n_sectors(const u16 *id)
  607. {
  608. if (ata_id_has_lba(id)) {
  609. if (ata_id_has_lba48(id))
  610. return ata_id_u64(id, 100);
  611. else
  612. return ata_id_u32(id, 60);
  613. } else {
  614. if (ata_id_current_chs_valid(id))
  615. return ata_id_u32(id, 57);
  616. else
  617. return id[1] * id[3] * id[6];
  618. }
  619. }
  620. /**
  621. * ata_noop_dev_select - Select device 0/1 on ATA bus
  622. * @ap: ATA channel to manipulate
  623. * @device: ATA device (numbered from zero) to select
  624. *
  625. * This function performs no actual function.
  626. *
  627. * May be used as the dev_select() entry in ata_port_operations.
  628. *
  629. * LOCKING:
  630. * caller.
  631. */
  632. void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
  633. {
  634. }
  635. /**
  636. * ata_std_dev_select - Select device 0/1 on ATA bus
  637. * @ap: ATA channel to manipulate
  638. * @device: ATA device (numbered from zero) to select
  639. *
  640. * Use the method defined in the ATA specification to
  641. * make either device 0, or device 1, active on the
  642. * ATA channel. Works with both PIO and MMIO.
  643. *
  644. * May be used as the dev_select() entry in ata_port_operations.
  645. *
  646. * LOCKING:
  647. * caller.
  648. */
  649. void ata_std_dev_select (struct ata_port *ap, unsigned int device)
  650. {
  651. u8 tmp;
  652. if (device == 0)
  653. tmp = ATA_DEVICE_OBS;
  654. else
  655. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  656. if (ap->flags & ATA_FLAG_MMIO) {
  657. writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
  658. } else {
  659. outb(tmp, ap->ioaddr.device_addr);
  660. }
  661. ata_pause(ap); /* needed; also flushes, for mmio */
  662. }
  663. /**
  664. * ata_dev_select - Select device 0/1 on ATA bus
  665. * @ap: ATA channel to manipulate
  666. * @device: ATA device (numbered from zero) to select
  667. * @wait: non-zero to wait for Status register BSY bit to clear
  668. * @can_sleep: non-zero if context allows sleeping
  669. *
  670. * Use the method defined in the ATA specification to
  671. * make either device 0, or device 1, active on the
  672. * ATA channel.
  673. *
  674. * This is a high-level version of ata_std_dev_select(),
  675. * which additionally provides the services of inserting
  676. * the proper pauses and status polling, where needed.
  677. *
  678. * LOCKING:
  679. * caller.
  680. */
  681. void ata_dev_select(struct ata_port *ap, unsigned int device,
  682. unsigned int wait, unsigned int can_sleep)
  683. {
  684. VPRINTK("ENTER, ata%u: device %u, wait %u\n",
  685. ap->id, device, wait);
  686. if (wait)
  687. ata_wait_idle(ap);
  688. ap->ops->dev_select(ap, device);
  689. if (wait) {
  690. if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
  691. msleep(150);
  692. ata_wait_idle(ap);
  693. }
  694. }
  695. /**
  696. * ata_dump_id - IDENTIFY DEVICE info debugging output
  697. * @id: IDENTIFY DEVICE page to dump
  698. *
  699. * Dump selected 16-bit words from the given IDENTIFY DEVICE
  700. * page.
  701. *
  702. * LOCKING:
  703. * caller.
  704. */
  705. static inline void ata_dump_id(const u16 *id)
  706. {
  707. DPRINTK("49==0x%04x "
  708. "53==0x%04x "
  709. "63==0x%04x "
  710. "64==0x%04x "
  711. "75==0x%04x \n",
  712. id[49],
  713. id[53],
  714. id[63],
  715. id[64],
  716. id[75]);
  717. DPRINTK("80==0x%04x "
  718. "81==0x%04x "
  719. "82==0x%04x "
  720. "83==0x%04x "
  721. "84==0x%04x \n",
  722. id[80],
  723. id[81],
  724. id[82],
  725. id[83],
  726. id[84]);
  727. DPRINTK("88==0x%04x "
  728. "93==0x%04x\n",
  729. id[88],
  730. id[93]);
  731. }
  732. /**
  733. * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
  734. * @id: IDENTIFY data to compute xfer mask from
  735. *
  736. * Compute the xfermask for this device. This is not as trivial
  737. * as it seems if we must consider early devices correctly.
  738. *
  739. * FIXME: pre IDE drive timing (do we care ?).
  740. *
  741. * LOCKING:
  742. * None.
  743. *
  744. * RETURNS:
  745. * Computed xfermask
  746. */
  747. static unsigned int ata_id_xfermask(const u16 *id)
  748. {
  749. unsigned int pio_mask, mwdma_mask, udma_mask;
  750. /* Usual case. Word 53 indicates word 64 is valid */
  751. if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
  752. pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
  753. pio_mask <<= 3;
  754. pio_mask |= 0x7;
  755. } else {
  756. /* If word 64 isn't valid then Word 51 high byte holds
  757. * the PIO timing number for the maximum. Turn it into
  758. * a mask.
  759. */
  760. pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
  761. /* But wait.. there's more. Design your standards by
  762. * committee and you too can get a free iordy field to
  763. * process. However its the speeds not the modes that
  764. * are supported... Note drivers using the timing API
  765. * will get this right anyway
  766. */
  767. }
  768. mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
  769. udma_mask = 0;
  770. if (id[ATA_ID_FIELD_VALID] & (1 << 2))
  771. udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
  772. return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  773. }
  774. /**
  775. * ata_port_queue_task - Queue port_task
  776. * @ap: The ata_port to queue port_task for
  777. *
  778. * Schedule @fn(@data) for execution after @delay jiffies using
  779. * port_task. There is one port_task per port and it's the
  780. * user(low level driver)'s responsibility to make sure that only
  781. * one task is active at any given time.
  782. *
  783. * libata core layer takes care of synchronization between
  784. * port_task and EH. ata_port_queue_task() may be ignored for EH
  785. * synchronization.
  786. *
  787. * LOCKING:
  788. * Inherited from caller.
  789. */
  790. void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
  791. unsigned long delay)
  792. {
  793. int rc;
  794. if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
  795. return;
  796. PREPARE_WORK(&ap->port_task, fn, data);
  797. if (!delay)
  798. rc = queue_work(ata_wq, &ap->port_task);
  799. else
  800. rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
  801. /* rc == 0 means that another user is using port task */
  802. WARN_ON(rc == 0);
  803. }
  804. /**
  805. * ata_port_flush_task - Flush port_task
  806. * @ap: The ata_port to flush port_task for
  807. *
  808. * After this function completes, port_task is guranteed not to
  809. * be running or scheduled.
  810. *
  811. * LOCKING:
  812. * Kernel thread context (may sleep)
  813. */
  814. void ata_port_flush_task(struct ata_port *ap)
  815. {
  816. unsigned long flags;
  817. DPRINTK("ENTER\n");
  818. spin_lock_irqsave(&ap->host_set->lock, flags);
  819. ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
  820. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  821. DPRINTK("flush #1\n");
  822. flush_workqueue(ata_wq);
  823. /*
  824. * At this point, if a task is running, it's guaranteed to see
  825. * the FLUSH flag; thus, it will never queue pio tasks again.
  826. * Cancel and flush.
  827. */
  828. if (!cancel_delayed_work(&ap->port_task)) {
  829. DPRINTK("flush #2\n");
  830. flush_workqueue(ata_wq);
  831. }
  832. spin_lock_irqsave(&ap->host_set->lock, flags);
  833. ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
  834. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  835. DPRINTK("EXIT\n");
  836. }
  837. void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  838. {
  839. struct completion *waiting = qc->private_data;
  840. qc->ap->ops->tf_read(qc->ap, &qc->tf);
  841. complete(waiting);
  842. }
  843. /**
  844. * ata_exec_internal - execute libata internal command
  845. * @ap: Port to which the command is sent
  846. * @dev: Device to which the command is sent
  847. * @tf: Taskfile registers for the command and the result
  848. * @dma_dir: Data tranfer direction of the command
  849. * @buf: Data buffer of the command
  850. * @buflen: Length of data buffer
  851. *
  852. * Executes libata internal command with timeout. @tf contains
  853. * command on entry and result on return. Timeout and error
  854. * conditions are reported via return value. No recovery action
  855. * is taken after a command times out. It's caller's duty to
  856. * clean up after timeout.
  857. *
  858. * LOCKING:
  859. * None. Should be called with kernel context, might sleep.
  860. */
  861. static unsigned
  862. ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
  863. struct ata_taskfile *tf,
  864. int dma_dir, void *buf, unsigned int buflen)
  865. {
  866. u8 command = tf->command;
  867. struct ata_queued_cmd *qc;
  868. DECLARE_COMPLETION(wait);
  869. unsigned long flags;
  870. unsigned int err_mask;
  871. spin_lock_irqsave(&ap->host_set->lock, flags);
  872. qc = ata_qc_new_init(ap, dev);
  873. BUG_ON(qc == NULL);
  874. qc->tf = *tf;
  875. qc->dma_dir = dma_dir;
  876. if (dma_dir != DMA_NONE) {
  877. ata_sg_init_one(qc, buf, buflen);
  878. qc->nsect = buflen / ATA_SECT_SIZE;
  879. }
  880. qc->private_data = &wait;
  881. qc->complete_fn = ata_qc_complete_internal;
  882. ata_qc_issue(qc);
  883. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  884. if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
  885. ata_port_flush_task(ap);
  886. spin_lock_irqsave(&ap->host_set->lock, flags);
  887. /* We're racing with irq here. If we lose, the
  888. * following test prevents us from completing the qc
  889. * again. If completion irq occurs after here but
  890. * before the caller cleans up, it will result in a
  891. * spurious interrupt. We can live with that.
  892. */
  893. if (qc->flags & ATA_QCFLAG_ACTIVE) {
  894. qc->err_mask = AC_ERR_TIMEOUT;
  895. ata_qc_complete(qc);
  896. printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
  897. ap->id, command);
  898. }
  899. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  900. }
  901. *tf = qc->tf;
  902. err_mask = qc->err_mask;
  903. ata_qc_free(qc);
  904. /* XXX - Some LLDDs (sata_mv) disable port on command failure.
  905. * Until those drivers are fixed, we detect the condition
  906. * here, fail the command with AC_ERR_SYSTEM and reenable the
  907. * port.
  908. *
  909. * Note that this doesn't change any behavior as internal
  910. * command failure results in disabling the device in the
  911. * higher layer for LLDDs without new reset/EH callbacks.
  912. *
  913. * Kill the following code as soon as those drivers are fixed.
  914. */
  915. if (ap->flags & ATA_FLAG_PORT_DISABLED) {
  916. err_mask |= AC_ERR_SYSTEM;
  917. ata_port_probe(ap);
  918. }
  919. return err_mask;
  920. }
  921. /**
  922. * ata_pio_need_iordy - check if iordy needed
  923. * @adev: ATA device
  924. *
  925. * Check if the current speed of the device requires IORDY. Used
  926. * by various controllers for chip configuration.
  927. */
  928. unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  929. {
  930. int pio;
  931. int speed = adev->pio_mode - XFER_PIO_0;
  932. if (speed < 2)
  933. return 0;
  934. if (speed > 2)
  935. return 1;
  936. /* If we have no drive specific rule, then PIO 2 is non IORDY */
  937. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
  938. pio = adev->id[ATA_ID_EIDE_PIO];
  939. /* Is the speed faster than the drive allows non IORDY ? */
  940. if (pio) {
  941. /* This is cycle times not frequency - watch the logic! */
  942. if (pio > 240) /* PIO2 is 240nS per cycle */
  943. return 1;
  944. return 0;
  945. }
  946. }
  947. return 0;
  948. }
  949. /**
  950. * ata_dev_read_id - Read ID data from the specified device
  951. * @ap: port on which target device resides
  952. * @dev: target device
  953. * @p_class: pointer to class of the target device (may be changed)
  954. * @post_reset: is this read ID post-reset?
  955. * @p_id: read IDENTIFY page (newly allocated)
  956. *
  957. * Read ID data from the specified device. ATA_CMD_ID_ATA is
  958. * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
  959. * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
  960. * for pre-ATA4 drives.
  961. *
  962. * LOCKING:
  963. * Kernel thread context (may sleep)
  964. *
  965. * RETURNS:
  966. * 0 on success, -errno otherwise.
  967. */
  968. static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
  969. unsigned int *p_class, int post_reset, u16 **p_id)
  970. {
  971. unsigned int class = *p_class;
  972. struct ata_taskfile tf;
  973. unsigned int err_mask = 0;
  974. u16 *id;
  975. const char *reason;
  976. int rc;
  977. DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
  978. ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
  979. id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
  980. if (id == NULL) {
  981. rc = -ENOMEM;
  982. reason = "out of memory";
  983. goto err_out;
  984. }
  985. retry:
  986. ata_tf_init(ap, &tf, dev->devno);
  987. switch (class) {
  988. case ATA_DEV_ATA:
  989. tf.command = ATA_CMD_ID_ATA;
  990. break;
  991. case ATA_DEV_ATAPI:
  992. tf.command = ATA_CMD_ID_ATAPI;
  993. break;
  994. default:
  995. rc = -ENODEV;
  996. reason = "unsupported class";
  997. goto err_out;
  998. }
  999. tf.protocol = ATA_PROT_PIO;
  1000. err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
  1001. id, sizeof(id[0]) * ATA_ID_WORDS);
  1002. if (err_mask) {
  1003. rc = -EIO;
  1004. reason = "I/O error";
  1005. goto err_out;
  1006. }
  1007. swap_buf_le16(id, ATA_ID_WORDS);
  1008. /* sanity check */
  1009. if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
  1010. rc = -EINVAL;
  1011. reason = "device reports illegal type";
  1012. goto err_out;
  1013. }
  1014. if (post_reset && class == ATA_DEV_ATA) {
  1015. /*
  1016. * The exact sequence expected by certain pre-ATA4 drives is:
  1017. * SRST RESET
  1018. * IDENTIFY
  1019. * INITIALIZE DEVICE PARAMETERS
  1020. * anything else..
  1021. * Some drives were very specific about that exact sequence.
  1022. */
  1023. if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
  1024. err_mask = ata_dev_init_params(ap, dev, id[3], id[6]);
  1025. if (err_mask) {
  1026. rc = -EIO;
  1027. reason = "INIT_DEV_PARAMS failed";
  1028. goto err_out;
  1029. }
  1030. /* current CHS translation info (id[53-58]) might be
  1031. * changed. reread the identify device info.
  1032. */
  1033. post_reset = 0;
  1034. goto retry;
  1035. }
  1036. }
  1037. *p_class = class;
  1038. *p_id = id;
  1039. return 0;
  1040. err_out:
  1041. printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
  1042. ap->id, dev->devno, reason);
  1043. kfree(id);
  1044. return rc;
  1045. }
  1046. static inline u8 ata_dev_knobble(const struct ata_port *ap,
  1047. struct ata_device *dev)
  1048. {
  1049. return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
  1050. }
  1051. /**
  1052. * ata_dev_configure - Configure the specified ATA/ATAPI device
  1053. * @ap: Port on which target device resides
  1054. * @dev: Target device to configure
  1055. * @print_info: Enable device info printout
  1056. *
  1057. * Configure @dev according to @dev->id. Generic and low-level
  1058. * driver specific fixups are also applied.
  1059. *
  1060. * LOCKING:
  1061. * Kernel thread context (may sleep)
  1062. *
  1063. * RETURNS:
  1064. * 0 on success, -errno otherwise
  1065. */
  1066. static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
  1067. int print_info)
  1068. {
  1069. const u16 *id = dev->id;
  1070. unsigned int xfer_mask;
  1071. int i, rc;
  1072. if (!ata_dev_enabled(dev)) {
  1073. DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
  1074. ap->id, dev->devno);
  1075. return 0;
  1076. }
  1077. DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
  1078. /* print device capabilities */
  1079. if (print_info)
  1080. printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
  1081. "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
  1082. ap->id, dev->devno, id[49], id[82], id[83],
  1083. id[84], id[85], id[86], id[87], id[88]);
  1084. /* initialize to-be-configured parameters */
  1085. dev->flags = 0;
  1086. dev->max_sectors = 0;
  1087. dev->cdb_len = 0;
  1088. dev->n_sectors = 0;
  1089. dev->cylinders = 0;
  1090. dev->heads = 0;
  1091. dev->sectors = 0;
  1092. /*
  1093. * common ATA, ATAPI feature tests
  1094. */
  1095. /* find max transfer mode; for printk only */
  1096. xfer_mask = ata_id_xfermask(id);
  1097. ata_dump_id(id);
  1098. /* ATA-specific feature tests */
  1099. if (dev->class == ATA_DEV_ATA) {
  1100. dev->n_sectors = ata_id_n_sectors(id);
  1101. if (ata_id_has_lba(id)) {
  1102. const char *lba_desc;
  1103. lba_desc = "LBA";
  1104. dev->flags |= ATA_DFLAG_LBA;
  1105. if (ata_id_has_lba48(id)) {
  1106. dev->flags |= ATA_DFLAG_LBA48;
  1107. lba_desc = "LBA48";
  1108. }
  1109. /* print device info to dmesg */
  1110. if (print_info)
  1111. printk(KERN_INFO "ata%u: dev %u ATA-%d, "
  1112. "max %s, %Lu sectors: %s\n",
  1113. ap->id, dev->devno,
  1114. ata_id_major_version(id),
  1115. ata_mode_string(xfer_mask),
  1116. (unsigned long long)dev->n_sectors,
  1117. lba_desc);
  1118. } else {
  1119. /* CHS */
  1120. /* Default translation */
  1121. dev->cylinders = id[1];
  1122. dev->heads = id[3];
  1123. dev->sectors = id[6];
  1124. if (ata_id_current_chs_valid(id)) {
  1125. /* Current CHS translation is valid. */
  1126. dev->cylinders = id[54];
  1127. dev->heads = id[55];
  1128. dev->sectors = id[56];
  1129. }
  1130. /* print device info to dmesg */
  1131. if (print_info)
  1132. printk(KERN_INFO "ata%u: dev %u ATA-%d, "
  1133. "max %s, %Lu sectors: CHS %u/%u/%u\n",
  1134. ap->id, dev->devno,
  1135. ata_id_major_version(id),
  1136. ata_mode_string(xfer_mask),
  1137. (unsigned long long)dev->n_sectors,
  1138. dev->cylinders, dev->heads, dev->sectors);
  1139. }
  1140. dev->cdb_len = 16;
  1141. }
  1142. /* ATAPI-specific feature tests */
  1143. else if (dev->class == ATA_DEV_ATAPI) {
  1144. rc = atapi_cdb_len(id);
  1145. if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
  1146. printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
  1147. rc = -EINVAL;
  1148. goto err_out_nosup;
  1149. }
  1150. dev->cdb_len = (unsigned int) rc;
  1151. /* print device info to dmesg */
  1152. if (print_info)
  1153. printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
  1154. ap->id, dev->devno, ata_mode_string(xfer_mask));
  1155. }
  1156. ap->host->max_cmd_len = 0;
  1157. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1158. ap->host->max_cmd_len = max_t(unsigned int,
  1159. ap->host->max_cmd_len,
  1160. ap->device[i].cdb_len);
  1161. /* limit bridge transfers to udma5, 200 sectors */
  1162. if (ata_dev_knobble(ap, dev)) {
  1163. if (print_info)
  1164. printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
  1165. ap->id, dev->devno);
  1166. dev->udma_mask &= ATA_UDMA5;
  1167. dev->max_sectors = ATA_MAX_SECTORS;
  1168. }
  1169. if (ap->ops->dev_config)
  1170. ap->ops->dev_config(ap, dev);
  1171. DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
  1172. return 0;
  1173. err_out_nosup:
  1174. DPRINTK("EXIT, err\n");
  1175. return rc;
  1176. }
  1177. /**
  1178. * ata_bus_probe - Reset and probe ATA bus
  1179. * @ap: Bus to probe
  1180. *
  1181. * Master ATA bus probing function. Initiates a hardware-dependent
  1182. * bus reset, then attempts to identify any devices found on
  1183. * the bus.
  1184. *
  1185. * LOCKING:
  1186. * PCI/etc. bus probe sem.
  1187. *
  1188. * RETURNS:
  1189. * Zero on success, negative errno otherwise.
  1190. */
  1191. static int ata_bus_probe(struct ata_port *ap)
  1192. {
  1193. unsigned int classes[ATA_MAX_DEVICES];
  1194. int i, rc, found = 0;
  1195. ata_port_probe(ap);
  1196. /* reset and determine device classes */
  1197. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1198. classes[i] = ATA_DEV_UNKNOWN;
  1199. if (ap->ops->probe_reset) {
  1200. rc = ap->ops->probe_reset(ap, classes);
  1201. if (rc) {
  1202. printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
  1203. return rc;
  1204. }
  1205. } else {
  1206. ap->ops->phy_reset(ap);
  1207. if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
  1208. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1209. classes[i] = ap->device[i].class;
  1210. ata_port_probe(ap);
  1211. }
  1212. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1213. if (classes[i] == ATA_DEV_UNKNOWN)
  1214. classes[i] = ATA_DEV_NONE;
  1215. /* read IDENTIFY page and configure devices */
  1216. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1217. struct ata_device *dev = &ap->device[i];
  1218. dev->class = classes[i];
  1219. if (!ata_dev_enabled(dev))
  1220. continue;
  1221. WARN_ON(dev->id != NULL);
  1222. if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
  1223. dev->class = ATA_DEV_NONE;
  1224. continue;
  1225. }
  1226. if (ata_dev_configure(ap, dev, 1)) {
  1227. ata_dev_disable(ap, dev);
  1228. continue;
  1229. }
  1230. found = 1;
  1231. }
  1232. if (!found)
  1233. goto err_out_disable;
  1234. if (ap->ops->set_mode)
  1235. ap->ops->set_mode(ap);
  1236. else
  1237. ata_set_mode(ap);
  1238. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  1239. goto err_out_disable;
  1240. return 0;
  1241. err_out_disable:
  1242. ap->ops->port_disable(ap);
  1243. return -ENODEV;
  1244. }
  1245. /**
  1246. * ata_port_probe - Mark port as enabled
  1247. * @ap: Port for which we indicate enablement
  1248. *
  1249. * Modify @ap data structure such that the system
  1250. * thinks that the entire port is enabled.
  1251. *
  1252. * LOCKING: host_set lock, or some other form of
  1253. * serialization.
  1254. */
  1255. void ata_port_probe(struct ata_port *ap)
  1256. {
  1257. ap->flags &= ~ATA_FLAG_PORT_DISABLED;
  1258. }
  1259. /**
  1260. * sata_print_link_status - Print SATA link status
  1261. * @ap: SATA port to printk link status about
  1262. *
  1263. * This function prints link speed and status of a SATA link.
  1264. *
  1265. * LOCKING:
  1266. * None.
  1267. */
  1268. static void sata_print_link_status(struct ata_port *ap)
  1269. {
  1270. u32 sstatus, tmp;
  1271. if (!ap->ops->scr_read)
  1272. return;
  1273. sstatus = scr_read(ap, SCR_STATUS);
  1274. if (sata_dev_present(ap)) {
  1275. tmp = (sstatus >> 4) & 0xf;
  1276. printk(KERN_INFO "ata%u: SATA link up %s (SStatus %X)\n",
  1277. ap->id, sata_spd_string(tmp), sstatus);
  1278. } else {
  1279. printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
  1280. ap->id, sstatus);
  1281. }
  1282. }
  1283. /**
  1284. * __sata_phy_reset - Wake/reset a low-level SATA PHY
  1285. * @ap: SATA port associated with target SATA PHY.
  1286. *
  1287. * This function issues commands to standard SATA Sxxx
  1288. * PHY registers, to wake up the phy (and device), and
  1289. * clear any reset condition.
  1290. *
  1291. * LOCKING:
  1292. * PCI/etc. bus probe sem.
  1293. *
  1294. */
  1295. void __sata_phy_reset(struct ata_port *ap)
  1296. {
  1297. u32 sstatus;
  1298. unsigned long timeout = jiffies + (HZ * 5);
  1299. if (ap->flags & ATA_FLAG_SATA_RESET) {
  1300. /* issue phy wake/reset */
  1301. scr_write_flush(ap, SCR_CONTROL, 0x301);
  1302. /* Couldn't find anything in SATA I/II specs, but
  1303. * AHCI-1.1 10.4.2 says at least 1 ms. */
  1304. mdelay(1);
  1305. }
  1306. scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
  1307. /* wait for phy to become ready, if necessary */
  1308. do {
  1309. msleep(200);
  1310. sstatus = scr_read(ap, SCR_STATUS);
  1311. if ((sstatus & 0xf) != 1)
  1312. break;
  1313. } while (time_before(jiffies, timeout));
  1314. /* print link status */
  1315. sata_print_link_status(ap);
  1316. /* TODO: phy layer with polling, timeouts, etc. */
  1317. if (sata_dev_present(ap))
  1318. ata_port_probe(ap);
  1319. else
  1320. ata_port_disable(ap);
  1321. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  1322. return;
  1323. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  1324. ata_port_disable(ap);
  1325. return;
  1326. }
  1327. ap->cbl = ATA_CBL_SATA;
  1328. }
  1329. /**
  1330. * sata_phy_reset - Reset SATA bus.
  1331. * @ap: SATA port associated with target SATA PHY.
  1332. *
  1333. * This function resets the SATA bus, and then probes
  1334. * the bus for devices.
  1335. *
  1336. * LOCKING:
  1337. * PCI/etc. bus probe sem.
  1338. *
  1339. */
  1340. void sata_phy_reset(struct ata_port *ap)
  1341. {
  1342. __sata_phy_reset(ap);
  1343. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  1344. return;
  1345. ata_bus_reset(ap);
  1346. }
  1347. /**
  1348. * ata_dev_pair - return other device on cable
  1349. * @ap: port
  1350. * @adev: device
  1351. *
  1352. * Obtain the other device on the same cable, or if none is
  1353. * present NULL is returned
  1354. */
  1355. struct ata_device *ata_dev_pair(struct ata_port *ap, struct ata_device *adev)
  1356. {
  1357. struct ata_device *pair = &ap->device[1 - adev->devno];
  1358. if (!ata_dev_enabled(pair))
  1359. return NULL;
  1360. return pair;
  1361. }
  1362. /**
  1363. * ata_port_disable - Disable port.
  1364. * @ap: Port to be disabled.
  1365. *
  1366. * Modify @ap data structure such that the system
  1367. * thinks that the entire port is disabled, and should
  1368. * never attempt to probe or communicate with devices
  1369. * on this port.
  1370. *
  1371. * LOCKING: host_set lock, or some other form of
  1372. * serialization.
  1373. */
  1374. void ata_port_disable(struct ata_port *ap)
  1375. {
  1376. ap->device[0].class = ATA_DEV_NONE;
  1377. ap->device[1].class = ATA_DEV_NONE;
  1378. ap->flags |= ATA_FLAG_PORT_DISABLED;
  1379. }
  1380. /*
  1381. * This mode timing computation functionality is ported over from
  1382. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  1383. */
  1384. /*
  1385. * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  1386. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  1387. * for PIO 5, which is a nonstandard extension and UDMA6, which
  1388. * is currently supported only by Maxtor drives.
  1389. */
  1390. static const struct ata_timing ata_timing[] = {
  1391. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
  1392. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
  1393. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
  1394. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
  1395. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
  1396. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
  1397. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
  1398. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  1399. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
  1400. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
  1401. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
  1402. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
  1403. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
  1404. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
  1405. /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
  1406. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
  1407. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
  1408. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
  1409. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
  1410. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
  1411. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
  1412. { 0xFF }
  1413. };
  1414. #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
  1415. #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
  1416. static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
  1417. {
  1418. q->setup = EZ(t->setup * 1000, T);
  1419. q->act8b = EZ(t->act8b * 1000, T);
  1420. q->rec8b = EZ(t->rec8b * 1000, T);
  1421. q->cyc8b = EZ(t->cyc8b * 1000, T);
  1422. q->active = EZ(t->active * 1000, T);
  1423. q->recover = EZ(t->recover * 1000, T);
  1424. q->cycle = EZ(t->cycle * 1000, T);
  1425. q->udma = EZ(t->udma * 1000, UT);
  1426. }
  1427. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  1428. struct ata_timing *m, unsigned int what)
  1429. {
  1430. if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
  1431. if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
  1432. if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
  1433. if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
  1434. if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
  1435. if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
  1436. if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
  1437. if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
  1438. }
  1439. static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
  1440. {
  1441. const struct ata_timing *t;
  1442. for (t = ata_timing; t->mode != speed; t++)
  1443. if (t->mode == 0xFF)
  1444. return NULL;
  1445. return t;
  1446. }
  1447. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  1448. struct ata_timing *t, int T, int UT)
  1449. {
  1450. const struct ata_timing *s;
  1451. struct ata_timing p;
  1452. /*
  1453. * Find the mode.
  1454. */
  1455. if (!(s = ata_timing_find_mode(speed)))
  1456. return -EINVAL;
  1457. memcpy(t, s, sizeof(*s));
  1458. /*
  1459. * If the drive is an EIDE drive, it can tell us it needs extended
  1460. * PIO/MW_DMA cycle timing.
  1461. */
  1462. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  1463. memset(&p, 0, sizeof(p));
  1464. if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
  1465. if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
  1466. else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
  1467. } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
  1468. p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
  1469. }
  1470. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  1471. }
  1472. /*
  1473. * Convert the timing to bus clock counts.
  1474. */
  1475. ata_timing_quantize(t, t, T, UT);
  1476. /*
  1477. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
  1478. * S.M.A.R.T * and some other commands. We have to ensure that the
  1479. * DMA cycle timing is slower/equal than the fastest PIO timing.
  1480. */
  1481. if (speed > XFER_PIO_4) {
  1482. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  1483. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  1484. }
  1485. /*
  1486. * Lengthen active & recovery time so that cycle time is correct.
  1487. */
  1488. if (t->act8b + t->rec8b < t->cyc8b) {
  1489. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  1490. t->rec8b = t->cyc8b - t->act8b;
  1491. }
  1492. if (t->active + t->recover < t->cycle) {
  1493. t->active += (t->cycle - (t->active + t->recover)) / 2;
  1494. t->recover = t->cycle - t->active;
  1495. }
  1496. return 0;
  1497. }
  1498. static int ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
  1499. {
  1500. unsigned int err_mask;
  1501. int rc;
  1502. if (dev->xfer_shift == ATA_SHIFT_PIO)
  1503. dev->flags |= ATA_DFLAG_PIO;
  1504. err_mask = ata_dev_set_xfermode(ap, dev);
  1505. if (err_mask) {
  1506. printk(KERN_ERR
  1507. "ata%u: failed to set xfermode (err_mask=0x%x)\n",
  1508. ap->id, err_mask);
  1509. return -EIO;
  1510. }
  1511. rc = ata_dev_revalidate(ap, dev, 0);
  1512. if (rc) {
  1513. printk(KERN_ERR
  1514. "ata%u: failed to revalidate after set xfermode\n",
  1515. ap->id);
  1516. return rc;
  1517. }
  1518. DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
  1519. dev->xfer_shift, (int)dev->xfer_mode);
  1520. printk(KERN_INFO "ata%u: dev %u configured for %s\n",
  1521. ap->id, dev->devno,
  1522. ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
  1523. return 0;
  1524. }
  1525. /**
  1526. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  1527. * @ap: port on which timings will be programmed
  1528. *
  1529. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
  1530. *
  1531. * LOCKING:
  1532. * PCI/etc. bus probe sem.
  1533. */
  1534. static void ata_set_mode(struct ata_port *ap)
  1535. {
  1536. struct ata_device *dev;
  1537. int i, rc, used_dma = 0, found = 0;
  1538. /* step 1: calculate xfer_mask */
  1539. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1540. unsigned int pio_mask, dma_mask;
  1541. dev = &ap->device[i];
  1542. if (!ata_dev_enabled(dev))
  1543. continue;
  1544. ata_dev_xfermask(ap, dev);
  1545. pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
  1546. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
  1547. dev->pio_mode = ata_xfer_mask2mode(pio_mask);
  1548. dev->dma_mode = ata_xfer_mask2mode(dma_mask);
  1549. found = 1;
  1550. if (dev->dma_mode)
  1551. used_dma = 1;
  1552. }
  1553. if (!found)
  1554. return;
  1555. /* step 2: always set host PIO timings */
  1556. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1557. dev = &ap->device[i];
  1558. if (!ata_dev_enabled(dev))
  1559. continue;
  1560. if (!dev->pio_mode) {
  1561. printk(KERN_WARNING "ata%u: dev %u no PIO support\n",
  1562. ap->id, dev->devno);
  1563. rc = -EINVAL;
  1564. goto err_out;
  1565. }
  1566. dev->xfer_mode = dev->pio_mode;
  1567. dev->xfer_shift = ATA_SHIFT_PIO;
  1568. if (ap->ops->set_piomode)
  1569. ap->ops->set_piomode(ap, dev);
  1570. }
  1571. /* step 3: set host DMA timings */
  1572. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1573. dev = &ap->device[i];
  1574. if (!ata_dev_enabled(dev) || !dev->dma_mode)
  1575. continue;
  1576. dev->xfer_mode = dev->dma_mode;
  1577. dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
  1578. if (ap->ops->set_dmamode)
  1579. ap->ops->set_dmamode(ap, dev);
  1580. }
  1581. /* step 4: update devices' xfer mode */
  1582. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1583. dev = &ap->device[i];
  1584. if (!ata_dev_enabled(dev))
  1585. continue;
  1586. rc = ata_dev_set_mode(ap, dev);
  1587. if (rc)
  1588. goto err_out;
  1589. }
  1590. /* Record simplex status. If we selected DMA then the other
  1591. * host channels are not permitted to do so.
  1592. */
  1593. if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
  1594. ap->host_set->simplex_claimed = 1;
  1595. /* step5: chip specific finalisation */
  1596. if (ap->ops->post_set_mode)
  1597. ap->ops->post_set_mode(ap);
  1598. return;
  1599. err_out:
  1600. ata_port_disable(ap);
  1601. }
  1602. /**
  1603. * ata_tf_to_host - issue ATA taskfile to host controller
  1604. * @ap: port to which command is being issued
  1605. * @tf: ATA taskfile register set
  1606. *
  1607. * Issues ATA taskfile register set to ATA host controller,
  1608. * with proper synchronization with interrupt handler and
  1609. * other threads.
  1610. *
  1611. * LOCKING:
  1612. * spin_lock_irqsave(host_set lock)
  1613. */
  1614. static inline void ata_tf_to_host(struct ata_port *ap,
  1615. const struct ata_taskfile *tf)
  1616. {
  1617. ap->ops->tf_load(ap, tf);
  1618. ap->ops->exec_command(ap, tf);
  1619. }
  1620. /**
  1621. * ata_busy_sleep - sleep until BSY clears, or timeout
  1622. * @ap: port containing status register to be polled
  1623. * @tmout_pat: impatience timeout
  1624. * @tmout: overall timeout
  1625. *
  1626. * Sleep until ATA Status register bit BSY clears,
  1627. * or a timeout occurs.
  1628. *
  1629. * LOCKING: None.
  1630. */
  1631. unsigned int ata_busy_sleep (struct ata_port *ap,
  1632. unsigned long tmout_pat, unsigned long tmout)
  1633. {
  1634. unsigned long timer_start, timeout;
  1635. u8 status;
  1636. status = ata_busy_wait(ap, ATA_BUSY, 300);
  1637. timer_start = jiffies;
  1638. timeout = timer_start + tmout_pat;
  1639. while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
  1640. msleep(50);
  1641. status = ata_busy_wait(ap, ATA_BUSY, 3);
  1642. }
  1643. if (status & ATA_BUSY)
  1644. printk(KERN_WARNING "ata%u is slow to respond, "
  1645. "please be patient\n", ap->id);
  1646. timeout = timer_start + tmout;
  1647. while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
  1648. msleep(50);
  1649. status = ata_chk_status(ap);
  1650. }
  1651. if (status & ATA_BUSY) {
  1652. printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
  1653. ap->id, tmout / HZ);
  1654. return 1;
  1655. }
  1656. return 0;
  1657. }
  1658. static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
  1659. {
  1660. struct ata_ioports *ioaddr = &ap->ioaddr;
  1661. unsigned int dev0 = devmask & (1 << 0);
  1662. unsigned int dev1 = devmask & (1 << 1);
  1663. unsigned long timeout;
  1664. /* if device 0 was found in ata_devchk, wait for its
  1665. * BSY bit to clear
  1666. */
  1667. if (dev0)
  1668. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  1669. /* if device 1 was found in ata_devchk, wait for
  1670. * register access, then wait for BSY to clear
  1671. */
  1672. timeout = jiffies + ATA_TMOUT_BOOT;
  1673. while (dev1) {
  1674. u8 nsect, lbal;
  1675. ap->ops->dev_select(ap, 1);
  1676. if (ap->flags & ATA_FLAG_MMIO) {
  1677. nsect = readb((void __iomem *) ioaddr->nsect_addr);
  1678. lbal = readb((void __iomem *) ioaddr->lbal_addr);
  1679. } else {
  1680. nsect = inb(ioaddr->nsect_addr);
  1681. lbal = inb(ioaddr->lbal_addr);
  1682. }
  1683. if ((nsect == 1) && (lbal == 1))
  1684. break;
  1685. if (time_after(jiffies, timeout)) {
  1686. dev1 = 0;
  1687. break;
  1688. }
  1689. msleep(50); /* give drive a breather */
  1690. }
  1691. if (dev1)
  1692. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  1693. /* is all this really necessary? */
  1694. ap->ops->dev_select(ap, 0);
  1695. if (dev1)
  1696. ap->ops->dev_select(ap, 1);
  1697. if (dev0)
  1698. ap->ops->dev_select(ap, 0);
  1699. }
  1700. static unsigned int ata_bus_softreset(struct ata_port *ap,
  1701. unsigned int devmask)
  1702. {
  1703. struct ata_ioports *ioaddr = &ap->ioaddr;
  1704. DPRINTK("ata%u: bus reset via SRST\n", ap->id);
  1705. /* software reset. causes dev0 to be selected */
  1706. if (ap->flags & ATA_FLAG_MMIO) {
  1707. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  1708. udelay(20); /* FIXME: flush */
  1709. writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
  1710. udelay(20); /* FIXME: flush */
  1711. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  1712. } else {
  1713. outb(ap->ctl, ioaddr->ctl_addr);
  1714. udelay(10);
  1715. outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  1716. udelay(10);
  1717. outb(ap->ctl, ioaddr->ctl_addr);
  1718. }
  1719. /* spec mandates ">= 2ms" before checking status.
  1720. * We wait 150ms, because that was the magic delay used for
  1721. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  1722. * between when the ATA command register is written, and then
  1723. * status is checked. Because waiting for "a while" before
  1724. * checking status is fine, post SRST, we perform this magic
  1725. * delay here as well.
  1726. *
  1727. * Old drivers/ide uses the 2mS rule and then waits for ready
  1728. */
  1729. msleep(150);
  1730. /* Before we perform post reset processing we want to see if
  1731. * the bus shows 0xFF because the odd clown forgets the D7
  1732. * pulldown resistor.
  1733. */
  1734. if (ata_check_status(ap) == 0xFF)
  1735. return AC_ERR_OTHER;
  1736. ata_bus_post_reset(ap, devmask);
  1737. return 0;
  1738. }
  1739. /**
  1740. * ata_bus_reset - reset host port and associated ATA channel
  1741. * @ap: port to reset
  1742. *
  1743. * This is typically the first time we actually start issuing
  1744. * commands to the ATA channel. We wait for BSY to clear, then
  1745. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  1746. * result. Determine what devices, if any, are on the channel
  1747. * by looking at the device 0/1 error register. Look at the signature
  1748. * stored in each device's taskfile registers, to determine if
  1749. * the device is ATA or ATAPI.
  1750. *
  1751. * LOCKING:
  1752. * PCI/etc. bus probe sem.
  1753. * Obtains host_set lock.
  1754. *
  1755. * SIDE EFFECTS:
  1756. * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
  1757. */
  1758. void ata_bus_reset(struct ata_port *ap)
  1759. {
  1760. struct ata_ioports *ioaddr = &ap->ioaddr;
  1761. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  1762. u8 err;
  1763. unsigned int dev0, dev1 = 0, devmask = 0;
  1764. DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
  1765. /* determine if device 0/1 are present */
  1766. if (ap->flags & ATA_FLAG_SATA_RESET)
  1767. dev0 = 1;
  1768. else {
  1769. dev0 = ata_devchk(ap, 0);
  1770. if (slave_possible)
  1771. dev1 = ata_devchk(ap, 1);
  1772. }
  1773. if (dev0)
  1774. devmask |= (1 << 0);
  1775. if (dev1)
  1776. devmask |= (1 << 1);
  1777. /* select device 0 again */
  1778. ap->ops->dev_select(ap, 0);
  1779. /* issue bus reset */
  1780. if (ap->flags & ATA_FLAG_SRST)
  1781. if (ata_bus_softreset(ap, devmask))
  1782. goto err_out;
  1783. /*
  1784. * determine by signature whether we have ATA or ATAPI devices
  1785. */
  1786. ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
  1787. if ((slave_possible) && (err != 0x81))
  1788. ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
  1789. /* re-enable interrupts */
  1790. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  1791. ata_irq_on(ap);
  1792. /* is double-select really necessary? */
  1793. if (ap->device[1].class != ATA_DEV_NONE)
  1794. ap->ops->dev_select(ap, 1);
  1795. if (ap->device[0].class != ATA_DEV_NONE)
  1796. ap->ops->dev_select(ap, 0);
  1797. /* if no devices were detected, disable this port */
  1798. if ((ap->device[0].class == ATA_DEV_NONE) &&
  1799. (ap->device[1].class == ATA_DEV_NONE))
  1800. goto err_out;
  1801. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  1802. /* set up device control for ATA_FLAG_SATA_RESET */
  1803. if (ap->flags & ATA_FLAG_MMIO)
  1804. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  1805. else
  1806. outb(ap->ctl, ioaddr->ctl_addr);
  1807. }
  1808. DPRINTK("EXIT\n");
  1809. return;
  1810. err_out:
  1811. printk(KERN_ERR "ata%u: disabling port\n", ap->id);
  1812. ap->ops->port_disable(ap);
  1813. DPRINTK("EXIT\n");
  1814. }
  1815. static int sata_phy_resume(struct ata_port *ap)
  1816. {
  1817. unsigned long timeout = jiffies + (HZ * 5);
  1818. u32 sstatus;
  1819. scr_write_flush(ap, SCR_CONTROL, 0x300);
  1820. /* Wait for phy to become ready, if necessary. */
  1821. do {
  1822. msleep(200);
  1823. sstatus = scr_read(ap, SCR_STATUS);
  1824. if ((sstatus & 0xf) != 1)
  1825. return 0;
  1826. } while (time_before(jiffies, timeout));
  1827. return -1;
  1828. }
  1829. /**
  1830. * ata_std_probeinit - initialize probing
  1831. * @ap: port to be probed
  1832. *
  1833. * @ap is about to be probed. Initialize it. This function is
  1834. * to be used as standard callback for ata_drive_probe_reset().
  1835. *
  1836. * NOTE!!! Do not use this function as probeinit if a low level
  1837. * driver implements only hardreset. Just pass NULL as probeinit
  1838. * in that case. Using this function is probably okay but doing
  1839. * so makes reset sequence different from the original
  1840. * ->phy_reset implementation and Jeff nervous. :-P
  1841. */
  1842. void ata_std_probeinit(struct ata_port *ap)
  1843. {
  1844. if ((ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read) {
  1845. sata_phy_resume(ap);
  1846. if (sata_dev_present(ap))
  1847. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  1848. }
  1849. }
  1850. /**
  1851. * ata_std_softreset - reset host port via ATA SRST
  1852. * @ap: port to reset
  1853. * @verbose: fail verbosely
  1854. * @classes: resulting classes of attached devices
  1855. *
  1856. * Reset host port using ATA SRST. This function is to be used
  1857. * as standard callback for ata_drive_*_reset() functions.
  1858. *
  1859. * LOCKING:
  1860. * Kernel thread context (may sleep)
  1861. *
  1862. * RETURNS:
  1863. * 0 on success, -errno otherwise.
  1864. */
  1865. int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
  1866. {
  1867. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  1868. unsigned int devmask = 0, err_mask;
  1869. u8 err;
  1870. DPRINTK("ENTER\n");
  1871. if (ap->ops->scr_read && !sata_dev_present(ap)) {
  1872. classes[0] = ATA_DEV_NONE;
  1873. goto out;
  1874. }
  1875. /* determine if device 0/1 are present */
  1876. if (ata_devchk(ap, 0))
  1877. devmask |= (1 << 0);
  1878. if (slave_possible && ata_devchk(ap, 1))
  1879. devmask |= (1 << 1);
  1880. /* select device 0 again */
  1881. ap->ops->dev_select(ap, 0);
  1882. /* issue bus reset */
  1883. DPRINTK("about to softreset, devmask=%x\n", devmask);
  1884. err_mask = ata_bus_softreset(ap, devmask);
  1885. if (err_mask) {
  1886. if (verbose)
  1887. printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
  1888. ap->id, err_mask);
  1889. else
  1890. DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
  1891. err_mask);
  1892. return -EIO;
  1893. }
  1894. /* determine by signature whether we have ATA or ATAPI devices */
  1895. classes[0] = ata_dev_try_classify(ap, 0, &err);
  1896. if (slave_possible && err != 0x81)
  1897. classes[1] = ata_dev_try_classify(ap, 1, &err);
  1898. out:
  1899. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  1900. return 0;
  1901. }
  1902. /**
  1903. * sata_std_hardreset - reset host port via SATA phy reset
  1904. * @ap: port to reset
  1905. * @verbose: fail verbosely
  1906. * @class: resulting class of attached device
  1907. *
  1908. * SATA phy-reset host port using DET bits of SControl register.
  1909. * This function is to be used as standard callback for
  1910. * ata_drive_*_reset().
  1911. *
  1912. * LOCKING:
  1913. * Kernel thread context (may sleep)
  1914. *
  1915. * RETURNS:
  1916. * 0 on success, -errno otherwise.
  1917. */
  1918. int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
  1919. {
  1920. DPRINTK("ENTER\n");
  1921. /* Issue phy wake/reset */
  1922. scr_write_flush(ap, SCR_CONTROL, 0x301);
  1923. /*
  1924. * Couldn't find anything in SATA I/II specs, but AHCI-1.1
  1925. * 10.4.2 says at least 1 ms.
  1926. */
  1927. msleep(1);
  1928. /* Bring phy back */
  1929. sata_phy_resume(ap);
  1930. /* TODO: phy layer with polling, timeouts, etc. */
  1931. if (!sata_dev_present(ap)) {
  1932. *class = ATA_DEV_NONE;
  1933. DPRINTK("EXIT, link offline\n");
  1934. return 0;
  1935. }
  1936. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  1937. if (verbose)
  1938. printk(KERN_ERR "ata%u: COMRESET failed "
  1939. "(device not ready)\n", ap->id);
  1940. else
  1941. DPRINTK("EXIT, device not ready\n");
  1942. return -EIO;
  1943. }
  1944. ap->ops->dev_select(ap, 0); /* probably unnecessary */
  1945. *class = ata_dev_try_classify(ap, 0, NULL);
  1946. DPRINTK("EXIT, class=%u\n", *class);
  1947. return 0;
  1948. }
  1949. /**
  1950. * ata_std_postreset - standard postreset callback
  1951. * @ap: the target ata_port
  1952. * @classes: classes of attached devices
  1953. *
  1954. * This function is invoked after a successful reset. Note that
  1955. * the device might have been reset more than once using
  1956. * different reset methods before postreset is invoked.
  1957. *
  1958. * This function is to be used as standard callback for
  1959. * ata_drive_*_reset().
  1960. *
  1961. * LOCKING:
  1962. * Kernel thread context (may sleep)
  1963. */
  1964. void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
  1965. {
  1966. DPRINTK("ENTER\n");
  1967. /* set cable type if it isn't already set */
  1968. if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
  1969. ap->cbl = ATA_CBL_SATA;
  1970. /* print link status */
  1971. if (ap->cbl == ATA_CBL_SATA)
  1972. sata_print_link_status(ap);
  1973. /* re-enable interrupts */
  1974. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  1975. ata_irq_on(ap);
  1976. /* is double-select really necessary? */
  1977. if (classes[0] != ATA_DEV_NONE)
  1978. ap->ops->dev_select(ap, 1);
  1979. if (classes[1] != ATA_DEV_NONE)
  1980. ap->ops->dev_select(ap, 0);
  1981. /* bail out if no device is present */
  1982. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  1983. DPRINTK("EXIT, no device\n");
  1984. return;
  1985. }
  1986. /* set up device control */
  1987. if (ap->ioaddr.ctl_addr) {
  1988. if (ap->flags & ATA_FLAG_MMIO)
  1989. writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
  1990. else
  1991. outb(ap->ctl, ap->ioaddr.ctl_addr);
  1992. }
  1993. DPRINTK("EXIT\n");
  1994. }
  1995. /**
  1996. * ata_std_probe_reset - standard probe reset method
  1997. * @ap: prot to perform probe-reset
  1998. * @classes: resulting classes of attached devices
  1999. *
  2000. * The stock off-the-shelf ->probe_reset method.
  2001. *
  2002. * LOCKING:
  2003. * Kernel thread context (may sleep)
  2004. *
  2005. * RETURNS:
  2006. * 0 on success, -errno otherwise.
  2007. */
  2008. int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
  2009. {
  2010. ata_reset_fn_t hardreset;
  2011. hardreset = NULL;
  2012. if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
  2013. hardreset = sata_std_hardreset;
  2014. return ata_drive_probe_reset(ap, ata_std_probeinit,
  2015. ata_std_softreset, hardreset,
  2016. ata_std_postreset, classes);
  2017. }
  2018. static int ata_do_reset(struct ata_port *ap,
  2019. ata_reset_fn_t reset, ata_postreset_fn_t postreset,
  2020. int verbose, unsigned int *classes)
  2021. {
  2022. int i, rc;
  2023. for (i = 0; i < ATA_MAX_DEVICES; i++)
  2024. classes[i] = ATA_DEV_UNKNOWN;
  2025. rc = reset(ap, verbose, classes);
  2026. if (rc)
  2027. return rc;
  2028. /* If any class isn't ATA_DEV_UNKNOWN, consider classification
  2029. * is complete and convert all ATA_DEV_UNKNOWN to
  2030. * ATA_DEV_NONE.
  2031. */
  2032. for (i = 0; i < ATA_MAX_DEVICES; i++)
  2033. if (classes[i] != ATA_DEV_UNKNOWN)
  2034. break;
  2035. if (i < ATA_MAX_DEVICES)
  2036. for (i = 0; i < ATA_MAX_DEVICES; i++)
  2037. if (classes[i] == ATA_DEV_UNKNOWN)
  2038. classes[i] = ATA_DEV_NONE;
  2039. if (postreset)
  2040. postreset(ap, classes);
  2041. return 0;
  2042. }
  2043. /**
  2044. * ata_drive_probe_reset - Perform probe reset with given methods
  2045. * @ap: port to reset
  2046. * @probeinit: probeinit method (can be NULL)
  2047. * @softreset: softreset method (can be NULL)
  2048. * @hardreset: hardreset method (can be NULL)
  2049. * @postreset: postreset method (can be NULL)
  2050. * @classes: resulting classes of attached devices
  2051. *
  2052. * Reset the specified port and classify attached devices using
  2053. * given methods. This function prefers softreset but tries all
  2054. * possible reset sequences to reset and classify devices. This
  2055. * function is intended to be used for constructing ->probe_reset
  2056. * callback by low level drivers.
  2057. *
  2058. * Reset methods should follow the following rules.
  2059. *
  2060. * - Return 0 on sucess, -errno on failure.
  2061. * - If classification is supported, fill classes[] with
  2062. * recognized class codes.
  2063. * - If classification is not supported, leave classes[] alone.
  2064. * - If verbose is non-zero, print error message on failure;
  2065. * otherwise, shut up.
  2066. *
  2067. * LOCKING:
  2068. * Kernel thread context (may sleep)
  2069. *
  2070. * RETURNS:
  2071. * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
  2072. * if classification fails, and any error code from reset
  2073. * methods.
  2074. */
  2075. int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
  2076. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2077. ata_postreset_fn_t postreset, unsigned int *classes)
  2078. {
  2079. int rc = -EINVAL;
  2080. if (probeinit)
  2081. probeinit(ap);
  2082. if (softreset) {
  2083. rc = ata_do_reset(ap, softreset, postreset, 0, classes);
  2084. if (rc == 0 && classes[0] != ATA_DEV_UNKNOWN)
  2085. goto done;
  2086. }
  2087. if (!hardreset)
  2088. goto done;
  2089. rc = ata_do_reset(ap, hardreset, postreset, 0, classes);
  2090. if (rc || classes[0] != ATA_DEV_UNKNOWN)
  2091. goto done;
  2092. if (softreset)
  2093. rc = ata_do_reset(ap, softreset, postreset, 0, classes);
  2094. done:
  2095. if (rc == 0 && classes[0] == ATA_DEV_UNKNOWN)
  2096. rc = -ENODEV;
  2097. return rc;
  2098. }
  2099. /**
  2100. * ata_dev_same_device - Determine whether new ID matches configured device
  2101. * @ap: port on which the device to compare against resides
  2102. * @dev: device to compare against
  2103. * @new_class: class of the new device
  2104. * @new_id: IDENTIFY page of the new device
  2105. *
  2106. * Compare @new_class and @new_id against @dev and determine
  2107. * whether @dev is the device indicated by @new_class and
  2108. * @new_id.
  2109. *
  2110. * LOCKING:
  2111. * None.
  2112. *
  2113. * RETURNS:
  2114. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  2115. */
  2116. static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
  2117. unsigned int new_class, const u16 *new_id)
  2118. {
  2119. const u16 *old_id = dev->id;
  2120. unsigned char model[2][41], serial[2][21];
  2121. u64 new_n_sectors;
  2122. if (dev->class != new_class) {
  2123. printk(KERN_INFO
  2124. "ata%u: dev %u class mismatch %d != %d\n",
  2125. ap->id, dev->devno, dev->class, new_class);
  2126. return 0;
  2127. }
  2128. ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
  2129. ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
  2130. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
  2131. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
  2132. new_n_sectors = ata_id_n_sectors(new_id);
  2133. if (strcmp(model[0], model[1])) {
  2134. printk(KERN_INFO
  2135. "ata%u: dev %u model number mismatch '%s' != '%s'\n",
  2136. ap->id, dev->devno, model[0], model[1]);
  2137. return 0;
  2138. }
  2139. if (strcmp(serial[0], serial[1])) {
  2140. printk(KERN_INFO
  2141. "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
  2142. ap->id, dev->devno, serial[0], serial[1]);
  2143. return 0;
  2144. }
  2145. if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
  2146. printk(KERN_INFO
  2147. "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
  2148. ap->id, dev->devno, (unsigned long long)dev->n_sectors,
  2149. (unsigned long long)new_n_sectors);
  2150. return 0;
  2151. }
  2152. return 1;
  2153. }
  2154. /**
  2155. * ata_dev_revalidate - Revalidate ATA device
  2156. * @ap: port on which the device to revalidate resides
  2157. * @dev: device to revalidate
  2158. * @post_reset: is this revalidation after reset?
  2159. *
  2160. * Re-read IDENTIFY page and make sure @dev is still attached to
  2161. * the port.
  2162. *
  2163. * LOCKING:
  2164. * Kernel thread context (may sleep)
  2165. *
  2166. * RETURNS:
  2167. * 0 on success, negative errno otherwise
  2168. */
  2169. int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
  2170. int post_reset)
  2171. {
  2172. unsigned int class;
  2173. u16 *id;
  2174. int rc;
  2175. if (!ata_dev_enabled(dev))
  2176. return -ENODEV;
  2177. class = dev->class;
  2178. id = NULL;
  2179. /* allocate & read ID data */
  2180. rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
  2181. if (rc)
  2182. goto fail;
  2183. /* is the device still there? */
  2184. if (!ata_dev_same_device(ap, dev, class, id)) {
  2185. rc = -ENODEV;
  2186. goto fail;
  2187. }
  2188. kfree(dev->id);
  2189. dev->id = id;
  2190. /* configure device according to the new ID */
  2191. return ata_dev_configure(ap, dev, 0);
  2192. fail:
  2193. printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
  2194. ap->id, dev->devno, rc);
  2195. kfree(id);
  2196. return rc;
  2197. }
  2198. static const char * const ata_dma_blacklist [] = {
  2199. "WDC AC11000H", NULL,
  2200. "WDC AC22100H", NULL,
  2201. "WDC AC32500H", NULL,
  2202. "WDC AC33100H", NULL,
  2203. "WDC AC31600H", NULL,
  2204. "WDC AC32100H", "24.09P07",
  2205. "WDC AC23200L", "21.10N21",
  2206. "Compaq CRD-8241B", NULL,
  2207. "CRD-8400B", NULL,
  2208. "CRD-8480B", NULL,
  2209. "CRD-8482B", NULL,
  2210. "CRD-84", NULL,
  2211. "SanDisk SDP3B", NULL,
  2212. "SanDisk SDP3B-64", NULL,
  2213. "SANYO CD-ROM CRD", NULL,
  2214. "HITACHI CDR-8", NULL,
  2215. "HITACHI CDR-8335", NULL,
  2216. "HITACHI CDR-8435", NULL,
  2217. "Toshiba CD-ROM XM-6202B", NULL,
  2218. "TOSHIBA CD-ROM XM-1702BC", NULL,
  2219. "CD-532E-A", NULL,
  2220. "E-IDE CD-ROM CR-840", NULL,
  2221. "CD-ROM Drive/F5A", NULL,
  2222. "WPI CDD-820", NULL,
  2223. "SAMSUNG CD-ROM SC-148C", NULL,
  2224. "SAMSUNG CD-ROM SC", NULL,
  2225. "SanDisk SDP3B-64", NULL,
  2226. "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
  2227. "_NEC DV5800A", NULL,
  2228. "SAMSUNG CD-ROM SN-124", "N001"
  2229. };
  2230. static int ata_strim(char *s, size_t len)
  2231. {
  2232. len = strnlen(s, len);
  2233. /* ATAPI specifies that empty space is blank-filled; remove blanks */
  2234. while ((len > 0) && (s[len - 1] == ' ')) {
  2235. len--;
  2236. s[len] = 0;
  2237. }
  2238. return len;
  2239. }
  2240. static int ata_dma_blacklisted(const struct ata_device *dev)
  2241. {
  2242. unsigned char model_num[40];
  2243. unsigned char model_rev[16];
  2244. unsigned int nlen, rlen;
  2245. int i;
  2246. ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
  2247. sizeof(model_num));
  2248. ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
  2249. sizeof(model_rev));
  2250. nlen = ata_strim(model_num, sizeof(model_num));
  2251. rlen = ata_strim(model_rev, sizeof(model_rev));
  2252. for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
  2253. if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
  2254. if (ata_dma_blacklist[i+1] == NULL)
  2255. return 1;
  2256. if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
  2257. return 1;
  2258. }
  2259. }
  2260. return 0;
  2261. }
  2262. /**
  2263. * ata_dev_xfermask - Compute supported xfermask of the given device
  2264. * @ap: Port on which the device to compute xfermask for resides
  2265. * @dev: Device to compute xfermask for
  2266. *
  2267. * Compute supported xfermask of @dev and store it in
  2268. * dev->*_mask. This function is responsible for applying all
  2269. * known limits including host controller limits, device
  2270. * blacklist, etc...
  2271. *
  2272. * FIXME: The current implementation limits all transfer modes to
  2273. * the fastest of the lowested device on the port. This is not
  2274. * required on most controllers.
  2275. *
  2276. * LOCKING:
  2277. * None.
  2278. */
  2279. static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
  2280. {
  2281. struct ata_host_set *hs = ap->host_set;
  2282. unsigned long xfer_mask;
  2283. int i;
  2284. xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
  2285. ap->udma_mask);
  2286. /* FIXME: Use port-wide xfermask for now */
  2287. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2288. struct ata_device *d = &ap->device[i];
  2289. if (!ata_dev_enabled(d))
  2290. continue;
  2291. xfer_mask &= ata_pack_xfermask(d->pio_mask, d->mwdma_mask,
  2292. d->udma_mask);
  2293. xfer_mask &= ata_id_xfermask(d->id);
  2294. if (ata_dma_blacklisted(d))
  2295. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2296. /* Apply cable rule here. Don't apply it early because when
  2297. we handle hot plug the cable type can itself change */
  2298. if (ap->cbl == ATA_CBL_PATA40)
  2299. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  2300. }
  2301. if (ata_dma_blacklisted(dev))
  2302. printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
  2303. "disabling DMA\n", ap->id, dev->devno);
  2304. if (hs->flags & ATA_HOST_SIMPLEX) {
  2305. if (hs->simplex_claimed)
  2306. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2307. }
  2308. if (ap->ops->mode_filter)
  2309. xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
  2310. ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
  2311. &dev->udma_mask);
  2312. }
  2313. /**
  2314. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  2315. * @ap: Port associated with device @dev
  2316. * @dev: Device to which command will be sent
  2317. *
  2318. * Issue SET FEATURES - XFER MODE command to device @dev
  2319. * on port @ap.
  2320. *
  2321. * LOCKING:
  2322. * PCI/etc. bus probe sem.
  2323. *
  2324. * RETURNS:
  2325. * 0 on success, AC_ERR_* mask otherwise.
  2326. */
  2327. static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
  2328. struct ata_device *dev)
  2329. {
  2330. struct ata_taskfile tf;
  2331. unsigned int err_mask;
  2332. /* set up set-features taskfile */
  2333. DPRINTK("set features - xfer mode\n");
  2334. ata_tf_init(ap, &tf, dev->devno);
  2335. tf.command = ATA_CMD_SET_FEATURES;
  2336. tf.feature = SETFEATURES_XFER;
  2337. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2338. tf.protocol = ATA_PROT_NODATA;
  2339. tf.nsect = dev->xfer_mode;
  2340. err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
  2341. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  2342. return err_mask;
  2343. }
  2344. /**
  2345. * ata_dev_init_params - Issue INIT DEV PARAMS command
  2346. * @ap: Port associated with device @dev
  2347. * @dev: Device to which command will be sent
  2348. *
  2349. * LOCKING:
  2350. * Kernel thread context (may sleep)
  2351. *
  2352. * RETURNS:
  2353. * 0 on success, AC_ERR_* mask otherwise.
  2354. */
  2355. static unsigned int ata_dev_init_params(struct ata_port *ap,
  2356. struct ata_device *dev,
  2357. u16 heads,
  2358. u16 sectors)
  2359. {
  2360. struct ata_taskfile tf;
  2361. unsigned int err_mask;
  2362. /* Number of sectors per track 1-255. Number of heads 1-16 */
  2363. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  2364. return AC_ERR_INVALID;
  2365. /* set up init dev params taskfile */
  2366. DPRINTK("init dev params \n");
  2367. ata_tf_init(ap, &tf, dev->devno);
  2368. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  2369. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2370. tf.protocol = ATA_PROT_NODATA;
  2371. tf.nsect = sectors;
  2372. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  2373. err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
  2374. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  2375. return err_mask;
  2376. }
  2377. /**
  2378. * ata_sg_clean - Unmap DMA memory associated with command
  2379. * @qc: Command containing DMA memory to be released
  2380. *
  2381. * Unmap all mapped DMA memory associated with this command.
  2382. *
  2383. * LOCKING:
  2384. * spin_lock_irqsave(host_set lock)
  2385. */
  2386. static void ata_sg_clean(struct ata_queued_cmd *qc)
  2387. {
  2388. struct ata_port *ap = qc->ap;
  2389. struct scatterlist *sg = qc->__sg;
  2390. int dir = qc->dma_dir;
  2391. void *pad_buf = NULL;
  2392. WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
  2393. WARN_ON(sg == NULL);
  2394. if (qc->flags & ATA_QCFLAG_SINGLE)
  2395. WARN_ON(qc->n_elem > 1);
  2396. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  2397. /* if we padded the buffer out to 32-bit bound, and data
  2398. * xfer direction is from-device, we must copy from the
  2399. * pad buffer back into the supplied buffer
  2400. */
  2401. if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
  2402. pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2403. if (qc->flags & ATA_QCFLAG_SG) {
  2404. if (qc->n_elem)
  2405. dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
  2406. /* restore last sg */
  2407. sg[qc->orig_n_elem - 1].length += qc->pad_len;
  2408. if (pad_buf) {
  2409. struct scatterlist *psg = &qc->pad_sgent;
  2410. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2411. memcpy(addr + psg->offset, pad_buf, qc->pad_len);
  2412. kunmap_atomic(addr, KM_IRQ0);
  2413. }
  2414. } else {
  2415. if (qc->n_elem)
  2416. dma_unmap_single(ap->dev,
  2417. sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
  2418. dir);
  2419. /* restore sg */
  2420. sg->length += qc->pad_len;
  2421. if (pad_buf)
  2422. memcpy(qc->buf_virt + sg->length - qc->pad_len,
  2423. pad_buf, qc->pad_len);
  2424. }
  2425. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  2426. qc->__sg = NULL;
  2427. }
  2428. /**
  2429. * ata_fill_sg - Fill PCI IDE PRD table
  2430. * @qc: Metadata associated with taskfile to be transferred
  2431. *
  2432. * Fill PCI IDE PRD (scatter-gather) table with segments
  2433. * associated with the current disk command.
  2434. *
  2435. * LOCKING:
  2436. * spin_lock_irqsave(host_set lock)
  2437. *
  2438. */
  2439. static void ata_fill_sg(struct ata_queued_cmd *qc)
  2440. {
  2441. struct ata_port *ap = qc->ap;
  2442. struct scatterlist *sg;
  2443. unsigned int idx;
  2444. WARN_ON(qc->__sg == NULL);
  2445. WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
  2446. idx = 0;
  2447. ata_for_each_sg(sg, qc) {
  2448. u32 addr, offset;
  2449. u32 sg_len, len;
  2450. /* determine if physical DMA addr spans 64K boundary.
  2451. * Note h/w doesn't support 64-bit, so we unconditionally
  2452. * truncate dma_addr_t to u32.
  2453. */
  2454. addr = (u32) sg_dma_address(sg);
  2455. sg_len = sg_dma_len(sg);
  2456. while (sg_len) {
  2457. offset = addr & 0xffff;
  2458. len = sg_len;
  2459. if ((offset + sg_len) > 0x10000)
  2460. len = 0x10000 - offset;
  2461. ap->prd[idx].addr = cpu_to_le32(addr);
  2462. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  2463. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  2464. idx++;
  2465. sg_len -= len;
  2466. addr += len;
  2467. }
  2468. }
  2469. if (idx)
  2470. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  2471. }
  2472. /**
  2473. * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  2474. * @qc: Metadata associated with taskfile to check
  2475. *
  2476. * Allow low-level driver to filter ATA PACKET commands, returning
  2477. * a status indicating whether or not it is OK to use DMA for the
  2478. * supplied PACKET command.
  2479. *
  2480. * LOCKING:
  2481. * spin_lock_irqsave(host_set lock)
  2482. *
  2483. * RETURNS: 0 when ATAPI DMA can be used
  2484. * nonzero otherwise
  2485. */
  2486. int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  2487. {
  2488. struct ata_port *ap = qc->ap;
  2489. int rc = 0; /* Assume ATAPI DMA is OK by default */
  2490. if (ap->ops->check_atapi_dma)
  2491. rc = ap->ops->check_atapi_dma(qc);
  2492. return rc;
  2493. }
  2494. /**
  2495. * ata_qc_prep - Prepare taskfile for submission
  2496. * @qc: Metadata associated with taskfile to be prepared
  2497. *
  2498. * Prepare ATA taskfile for submission.
  2499. *
  2500. * LOCKING:
  2501. * spin_lock_irqsave(host_set lock)
  2502. */
  2503. void ata_qc_prep(struct ata_queued_cmd *qc)
  2504. {
  2505. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  2506. return;
  2507. ata_fill_sg(qc);
  2508. }
  2509. void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
  2510. /**
  2511. * ata_sg_init_one - Associate command with memory buffer
  2512. * @qc: Command to be associated
  2513. * @buf: Memory buffer
  2514. * @buflen: Length of memory buffer, in bytes.
  2515. *
  2516. * Initialize the data-related elements of queued_cmd @qc
  2517. * to point to a single memory buffer, @buf of byte length @buflen.
  2518. *
  2519. * LOCKING:
  2520. * spin_lock_irqsave(host_set lock)
  2521. */
  2522. void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
  2523. {
  2524. struct scatterlist *sg;
  2525. qc->flags |= ATA_QCFLAG_SINGLE;
  2526. memset(&qc->sgent, 0, sizeof(qc->sgent));
  2527. qc->__sg = &qc->sgent;
  2528. qc->n_elem = 1;
  2529. qc->orig_n_elem = 1;
  2530. qc->buf_virt = buf;
  2531. sg = qc->__sg;
  2532. sg_init_one(sg, buf, buflen);
  2533. }
  2534. /**
  2535. * ata_sg_init - Associate command with scatter-gather table.
  2536. * @qc: Command to be associated
  2537. * @sg: Scatter-gather table.
  2538. * @n_elem: Number of elements in s/g table.
  2539. *
  2540. * Initialize the data-related elements of queued_cmd @qc
  2541. * to point to a scatter-gather table @sg, containing @n_elem
  2542. * elements.
  2543. *
  2544. * LOCKING:
  2545. * spin_lock_irqsave(host_set lock)
  2546. */
  2547. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  2548. unsigned int n_elem)
  2549. {
  2550. qc->flags |= ATA_QCFLAG_SG;
  2551. qc->__sg = sg;
  2552. qc->n_elem = n_elem;
  2553. qc->orig_n_elem = n_elem;
  2554. }
  2555. /**
  2556. * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
  2557. * @qc: Command with memory buffer to be mapped.
  2558. *
  2559. * DMA-map the memory buffer associated with queued_cmd @qc.
  2560. *
  2561. * LOCKING:
  2562. * spin_lock_irqsave(host_set lock)
  2563. *
  2564. * RETURNS:
  2565. * Zero on success, negative on error.
  2566. */
  2567. static int ata_sg_setup_one(struct ata_queued_cmd *qc)
  2568. {
  2569. struct ata_port *ap = qc->ap;
  2570. int dir = qc->dma_dir;
  2571. struct scatterlist *sg = qc->__sg;
  2572. dma_addr_t dma_address;
  2573. int trim_sg = 0;
  2574. /* we must lengthen transfers to end on a 32-bit boundary */
  2575. qc->pad_len = sg->length & 3;
  2576. if (qc->pad_len) {
  2577. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2578. struct scatterlist *psg = &qc->pad_sgent;
  2579. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  2580. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  2581. if (qc->tf.flags & ATA_TFLAG_WRITE)
  2582. memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
  2583. qc->pad_len);
  2584. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  2585. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  2586. /* trim sg */
  2587. sg->length -= qc->pad_len;
  2588. if (sg->length == 0)
  2589. trim_sg = 1;
  2590. DPRINTK("padding done, sg->length=%u pad_len=%u\n",
  2591. sg->length, qc->pad_len);
  2592. }
  2593. if (trim_sg) {
  2594. qc->n_elem--;
  2595. goto skip_map;
  2596. }
  2597. dma_address = dma_map_single(ap->dev, qc->buf_virt,
  2598. sg->length, dir);
  2599. if (dma_mapping_error(dma_address)) {
  2600. /* restore sg */
  2601. sg->length += qc->pad_len;
  2602. return -1;
  2603. }
  2604. sg_dma_address(sg) = dma_address;
  2605. sg_dma_len(sg) = sg->length;
  2606. skip_map:
  2607. DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
  2608. qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  2609. return 0;
  2610. }
  2611. /**
  2612. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  2613. * @qc: Command with scatter-gather table to be mapped.
  2614. *
  2615. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  2616. *
  2617. * LOCKING:
  2618. * spin_lock_irqsave(host_set lock)
  2619. *
  2620. * RETURNS:
  2621. * Zero on success, negative on error.
  2622. *
  2623. */
  2624. static int ata_sg_setup(struct ata_queued_cmd *qc)
  2625. {
  2626. struct ata_port *ap = qc->ap;
  2627. struct scatterlist *sg = qc->__sg;
  2628. struct scatterlist *lsg = &sg[qc->n_elem - 1];
  2629. int n_elem, pre_n_elem, dir, trim_sg = 0;
  2630. VPRINTK("ENTER, ata%u\n", ap->id);
  2631. WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
  2632. /* we must lengthen transfers to end on a 32-bit boundary */
  2633. qc->pad_len = lsg->length & 3;
  2634. if (qc->pad_len) {
  2635. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2636. struct scatterlist *psg = &qc->pad_sgent;
  2637. unsigned int offset;
  2638. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  2639. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  2640. /*
  2641. * psg->page/offset are used to copy to-be-written
  2642. * data in this function or read data in ata_sg_clean.
  2643. */
  2644. offset = lsg->offset + lsg->length - qc->pad_len;
  2645. psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
  2646. psg->offset = offset_in_page(offset);
  2647. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  2648. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2649. memcpy(pad_buf, addr + psg->offset, qc->pad_len);
  2650. kunmap_atomic(addr, KM_IRQ0);
  2651. }
  2652. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  2653. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  2654. /* trim last sg */
  2655. lsg->length -= qc->pad_len;
  2656. if (lsg->length == 0)
  2657. trim_sg = 1;
  2658. DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
  2659. qc->n_elem - 1, lsg->length, qc->pad_len);
  2660. }
  2661. pre_n_elem = qc->n_elem;
  2662. if (trim_sg && pre_n_elem)
  2663. pre_n_elem--;
  2664. if (!pre_n_elem) {
  2665. n_elem = 0;
  2666. goto skip_map;
  2667. }
  2668. dir = qc->dma_dir;
  2669. n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
  2670. if (n_elem < 1) {
  2671. /* restore last sg */
  2672. lsg->length += qc->pad_len;
  2673. return -1;
  2674. }
  2675. DPRINTK("%d sg elements mapped\n", n_elem);
  2676. skip_map:
  2677. qc->n_elem = n_elem;
  2678. return 0;
  2679. }
  2680. /**
  2681. * ata_poll_qc_complete - turn irq back on and finish qc
  2682. * @qc: Command to complete
  2683. * @err_mask: ATA status register content
  2684. *
  2685. * LOCKING:
  2686. * None. (grabs host lock)
  2687. */
  2688. void ata_poll_qc_complete(struct ata_queued_cmd *qc)
  2689. {
  2690. struct ata_port *ap = qc->ap;
  2691. unsigned long flags;
  2692. spin_lock_irqsave(&ap->host_set->lock, flags);
  2693. ap->flags &= ~ATA_FLAG_NOINTR;
  2694. ata_irq_on(ap);
  2695. ata_qc_complete(qc);
  2696. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  2697. }
  2698. /**
  2699. * ata_pio_poll - poll using PIO, depending on current state
  2700. * @ap: the target ata_port
  2701. *
  2702. * LOCKING:
  2703. * None. (executing in kernel thread context)
  2704. *
  2705. * RETURNS:
  2706. * timeout value to use
  2707. */
  2708. static unsigned long ata_pio_poll(struct ata_port *ap)
  2709. {
  2710. struct ata_queued_cmd *qc;
  2711. u8 status;
  2712. unsigned int poll_state = HSM_ST_UNKNOWN;
  2713. unsigned int reg_state = HSM_ST_UNKNOWN;
  2714. qc = ata_qc_from_tag(ap, ap->active_tag);
  2715. WARN_ON(qc == NULL);
  2716. switch (ap->hsm_task_state) {
  2717. case HSM_ST:
  2718. case HSM_ST_POLL:
  2719. poll_state = HSM_ST_POLL;
  2720. reg_state = HSM_ST;
  2721. break;
  2722. case HSM_ST_LAST:
  2723. case HSM_ST_LAST_POLL:
  2724. poll_state = HSM_ST_LAST_POLL;
  2725. reg_state = HSM_ST_LAST;
  2726. break;
  2727. default:
  2728. BUG();
  2729. break;
  2730. }
  2731. status = ata_chk_status(ap);
  2732. if (status & ATA_BUSY) {
  2733. if (time_after(jiffies, ap->pio_task_timeout)) {
  2734. qc->err_mask |= AC_ERR_TIMEOUT;
  2735. ap->hsm_task_state = HSM_ST_TMOUT;
  2736. return 0;
  2737. }
  2738. ap->hsm_task_state = poll_state;
  2739. return ATA_SHORT_PAUSE;
  2740. }
  2741. ap->hsm_task_state = reg_state;
  2742. return 0;
  2743. }
  2744. /**
  2745. * ata_pio_complete - check if drive is busy or idle
  2746. * @ap: the target ata_port
  2747. *
  2748. * LOCKING:
  2749. * None. (executing in kernel thread context)
  2750. *
  2751. * RETURNS:
  2752. * Non-zero if qc completed, zero otherwise.
  2753. */
  2754. static int ata_pio_complete (struct ata_port *ap)
  2755. {
  2756. struct ata_queued_cmd *qc;
  2757. u8 drv_stat;
  2758. /*
  2759. * This is purely heuristic. This is a fast path. Sometimes when
  2760. * we enter, BSY will be cleared in a chk-status or two. If not,
  2761. * the drive is probably seeking or something. Snooze for a couple
  2762. * msecs, then chk-status again. If still busy, fall back to
  2763. * HSM_ST_POLL state.
  2764. */
  2765. drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
  2766. if (drv_stat & ATA_BUSY) {
  2767. msleep(2);
  2768. drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
  2769. if (drv_stat & ATA_BUSY) {
  2770. ap->hsm_task_state = HSM_ST_LAST_POLL;
  2771. ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
  2772. return 0;
  2773. }
  2774. }
  2775. qc = ata_qc_from_tag(ap, ap->active_tag);
  2776. WARN_ON(qc == NULL);
  2777. drv_stat = ata_wait_idle(ap);
  2778. if (!ata_ok(drv_stat)) {
  2779. qc->err_mask |= __ac_err_mask(drv_stat);
  2780. ap->hsm_task_state = HSM_ST_ERR;
  2781. return 0;
  2782. }
  2783. ap->hsm_task_state = HSM_ST_IDLE;
  2784. WARN_ON(qc->err_mask);
  2785. ata_poll_qc_complete(qc);
  2786. /* another command may start at this point */
  2787. return 1;
  2788. }
  2789. /**
  2790. * swap_buf_le16 - swap halves of 16-bit words in place
  2791. * @buf: Buffer to swap
  2792. * @buf_words: Number of 16-bit words in buffer.
  2793. *
  2794. * Swap halves of 16-bit words if needed to convert from
  2795. * little-endian byte order to native cpu byte order, or
  2796. * vice-versa.
  2797. *
  2798. * LOCKING:
  2799. * Inherited from caller.
  2800. */
  2801. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  2802. {
  2803. #ifdef __BIG_ENDIAN
  2804. unsigned int i;
  2805. for (i = 0; i < buf_words; i++)
  2806. buf[i] = le16_to_cpu(buf[i]);
  2807. #endif /* __BIG_ENDIAN */
  2808. }
  2809. /**
  2810. * ata_mmio_data_xfer - Transfer data by MMIO
  2811. * @ap: port to read/write
  2812. * @buf: data buffer
  2813. * @buflen: buffer length
  2814. * @write_data: read/write
  2815. *
  2816. * Transfer data from/to the device data register by MMIO.
  2817. *
  2818. * LOCKING:
  2819. * Inherited from caller.
  2820. */
  2821. static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
  2822. unsigned int buflen, int write_data)
  2823. {
  2824. unsigned int i;
  2825. unsigned int words = buflen >> 1;
  2826. u16 *buf16 = (u16 *) buf;
  2827. void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
  2828. /* Transfer multiple of 2 bytes */
  2829. if (write_data) {
  2830. for (i = 0; i < words; i++)
  2831. writew(le16_to_cpu(buf16[i]), mmio);
  2832. } else {
  2833. for (i = 0; i < words; i++)
  2834. buf16[i] = cpu_to_le16(readw(mmio));
  2835. }
  2836. /* Transfer trailing 1 byte, if any. */
  2837. if (unlikely(buflen & 0x01)) {
  2838. u16 align_buf[1] = { 0 };
  2839. unsigned char *trailing_buf = buf + buflen - 1;
  2840. if (write_data) {
  2841. memcpy(align_buf, trailing_buf, 1);
  2842. writew(le16_to_cpu(align_buf[0]), mmio);
  2843. } else {
  2844. align_buf[0] = cpu_to_le16(readw(mmio));
  2845. memcpy(trailing_buf, align_buf, 1);
  2846. }
  2847. }
  2848. }
  2849. /**
  2850. * ata_pio_data_xfer - Transfer data by PIO
  2851. * @ap: port to read/write
  2852. * @buf: data buffer
  2853. * @buflen: buffer length
  2854. * @write_data: read/write
  2855. *
  2856. * Transfer data from/to the device data register by PIO.
  2857. *
  2858. * LOCKING:
  2859. * Inherited from caller.
  2860. */
  2861. static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
  2862. unsigned int buflen, int write_data)
  2863. {
  2864. unsigned int words = buflen >> 1;
  2865. /* Transfer multiple of 2 bytes */
  2866. if (write_data)
  2867. outsw(ap->ioaddr.data_addr, buf, words);
  2868. else
  2869. insw(ap->ioaddr.data_addr, buf, words);
  2870. /* Transfer trailing 1 byte, if any. */
  2871. if (unlikely(buflen & 0x01)) {
  2872. u16 align_buf[1] = { 0 };
  2873. unsigned char *trailing_buf = buf + buflen - 1;
  2874. if (write_data) {
  2875. memcpy(align_buf, trailing_buf, 1);
  2876. outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
  2877. } else {
  2878. align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
  2879. memcpy(trailing_buf, align_buf, 1);
  2880. }
  2881. }
  2882. }
  2883. /**
  2884. * ata_data_xfer - Transfer data from/to the data register.
  2885. * @ap: port to read/write
  2886. * @buf: data buffer
  2887. * @buflen: buffer length
  2888. * @do_write: read/write
  2889. *
  2890. * Transfer data from/to the device data register.
  2891. *
  2892. * LOCKING:
  2893. * Inherited from caller.
  2894. */
  2895. static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
  2896. unsigned int buflen, int do_write)
  2897. {
  2898. /* Make the crap hardware pay the costs not the good stuff */
  2899. if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
  2900. unsigned long flags;
  2901. local_irq_save(flags);
  2902. if (ap->flags & ATA_FLAG_MMIO)
  2903. ata_mmio_data_xfer(ap, buf, buflen, do_write);
  2904. else
  2905. ata_pio_data_xfer(ap, buf, buflen, do_write);
  2906. local_irq_restore(flags);
  2907. } else {
  2908. if (ap->flags & ATA_FLAG_MMIO)
  2909. ata_mmio_data_xfer(ap, buf, buflen, do_write);
  2910. else
  2911. ata_pio_data_xfer(ap, buf, buflen, do_write);
  2912. }
  2913. }
  2914. /**
  2915. * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
  2916. * @qc: Command on going
  2917. *
  2918. * Transfer ATA_SECT_SIZE of data from/to the ATA device.
  2919. *
  2920. * LOCKING:
  2921. * Inherited from caller.
  2922. */
  2923. static void ata_pio_sector(struct ata_queued_cmd *qc)
  2924. {
  2925. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  2926. struct scatterlist *sg = qc->__sg;
  2927. struct ata_port *ap = qc->ap;
  2928. struct page *page;
  2929. unsigned int offset;
  2930. unsigned char *buf;
  2931. if (qc->cursect == (qc->nsect - 1))
  2932. ap->hsm_task_state = HSM_ST_LAST;
  2933. page = sg[qc->cursg].page;
  2934. offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
  2935. /* get the current page and offset */
  2936. page = nth_page(page, (offset >> PAGE_SHIFT));
  2937. offset %= PAGE_SIZE;
  2938. buf = kmap(page) + offset;
  2939. qc->cursect++;
  2940. qc->cursg_ofs++;
  2941. if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
  2942. qc->cursg++;
  2943. qc->cursg_ofs = 0;
  2944. }
  2945. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  2946. /* do the actual data transfer */
  2947. do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  2948. ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
  2949. kunmap(page);
  2950. }
  2951. /**
  2952. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  2953. * @qc: Command on going
  2954. * @bytes: number of bytes
  2955. *
  2956. * Transfer Transfer data from/to the ATAPI device.
  2957. *
  2958. * LOCKING:
  2959. * Inherited from caller.
  2960. *
  2961. */
  2962. static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  2963. {
  2964. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  2965. struct scatterlist *sg = qc->__sg;
  2966. struct ata_port *ap = qc->ap;
  2967. struct page *page;
  2968. unsigned char *buf;
  2969. unsigned int offset, count;
  2970. if (qc->curbytes + bytes >= qc->nbytes)
  2971. ap->hsm_task_state = HSM_ST_LAST;
  2972. next_sg:
  2973. if (unlikely(qc->cursg >= qc->n_elem)) {
  2974. /*
  2975. * The end of qc->sg is reached and the device expects
  2976. * more data to transfer. In order not to overrun qc->sg
  2977. * and fulfill length specified in the byte count register,
  2978. * - for read case, discard trailing data from the device
  2979. * - for write case, padding zero data to the device
  2980. */
  2981. u16 pad_buf[1] = { 0 };
  2982. unsigned int words = bytes >> 1;
  2983. unsigned int i;
  2984. if (words) /* warning if bytes > 1 */
  2985. printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
  2986. ap->id, bytes);
  2987. for (i = 0; i < words; i++)
  2988. ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
  2989. ap->hsm_task_state = HSM_ST_LAST;
  2990. return;
  2991. }
  2992. sg = &qc->__sg[qc->cursg];
  2993. page = sg->page;
  2994. offset = sg->offset + qc->cursg_ofs;
  2995. /* get the current page and offset */
  2996. page = nth_page(page, (offset >> PAGE_SHIFT));
  2997. offset %= PAGE_SIZE;
  2998. /* don't overrun current sg */
  2999. count = min(sg->length - qc->cursg_ofs, bytes);
  3000. /* don't cross page boundaries */
  3001. count = min(count, (unsigned int)PAGE_SIZE - offset);
  3002. buf = kmap(page) + offset;
  3003. bytes -= count;
  3004. qc->curbytes += count;
  3005. qc->cursg_ofs += count;
  3006. if (qc->cursg_ofs == sg->length) {
  3007. qc->cursg++;
  3008. qc->cursg_ofs = 0;
  3009. }
  3010. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3011. /* do the actual data transfer */
  3012. ata_data_xfer(ap, buf, count, do_write);
  3013. kunmap(page);
  3014. if (bytes)
  3015. goto next_sg;
  3016. }
  3017. /**
  3018. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3019. * @qc: Command on going
  3020. *
  3021. * Transfer Transfer data from/to the ATAPI device.
  3022. *
  3023. * LOCKING:
  3024. * Inherited from caller.
  3025. */
  3026. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  3027. {
  3028. struct ata_port *ap = qc->ap;
  3029. struct ata_device *dev = qc->dev;
  3030. unsigned int ireason, bc_lo, bc_hi, bytes;
  3031. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  3032. ap->ops->tf_read(ap, &qc->tf);
  3033. ireason = qc->tf.nsect;
  3034. bc_lo = qc->tf.lbam;
  3035. bc_hi = qc->tf.lbah;
  3036. bytes = (bc_hi << 8) | bc_lo;
  3037. /* shall be cleared to zero, indicating xfer of data */
  3038. if (ireason & (1 << 0))
  3039. goto err_out;
  3040. /* make sure transfer direction matches expected */
  3041. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  3042. if (do_write != i_write)
  3043. goto err_out;
  3044. __atapi_pio_bytes(qc, bytes);
  3045. return;
  3046. err_out:
  3047. printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
  3048. ap->id, dev->devno);
  3049. qc->err_mask |= AC_ERR_HSM;
  3050. ap->hsm_task_state = HSM_ST_ERR;
  3051. }
  3052. /**
  3053. * ata_pio_block - start PIO on a block
  3054. * @ap: the target ata_port
  3055. *
  3056. * LOCKING:
  3057. * None. (executing in kernel thread context)
  3058. */
  3059. static void ata_pio_block(struct ata_port *ap)
  3060. {
  3061. struct ata_queued_cmd *qc;
  3062. u8 status;
  3063. /*
  3064. * This is purely heuristic. This is a fast path.
  3065. * Sometimes when we enter, BSY will be cleared in
  3066. * a chk-status or two. If not, the drive is probably seeking
  3067. * or something. Snooze for a couple msecs, then
  3068. * chk-status again. If still busy, fall back to
  3069. * HSM_ST_POLL state.
  3070. */
  3071. status = ata_busy_wait(ap, ATA_BUSY, 5);
  3072. if (status & ATA_BUSY) {
  3073. msleep(2);
  3074. status = ata_busy_wait(ap, ATA_BUSY, 10);
  3075. if (status & ATA_BUSY) {
  3076. ap->hsm_task_state = HSM_ST_POLL;
  3077. ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
  3078. return;
  3079. }
  3080. }
  3081. qc = ata_qc_from_tag(ap, ap->active_tag);
  3082. WARN_ON(qc == NULL);
  3083. /* check error */
  3084. if (status & (ATA_ERR | ATA_DF)) {
  3085. qc->err_mask |= AC_ERR_DEV;
  3086. ap->hsm_task_state = HSM_ST_ERR;
  3087. return;
  3088. }
  3089. /* transfer data if any */
  3090. if (is_atapi_taskfile(&qc->tf)) {
  3091. /* DRQ=0 means no more data to transfer */
  3092. if ((status & ATA_DRQ) == 0) {
  3093. ap->hsm_task_state = HSM_ST_LAST;
  3094. return;
  3095. }
  3096. atapi_pio_bytes(qc);
  3097. } else {
  3098. /* handle BSY=0, DRQ=0 as error */
  3099. if ((status & ATA_DRQ) == 0) {
  3100. qc->err_mask |= AC_ERR_HSM;
  3101. ap->hsm_task_state = HSM_ST_ERR;
  3102. return;
  3103. }
  3104. ata_pio_sector(qc);
  3105. }
  3106. }
  3107. static void ata_pio_error(struct ata_port *ap)
  3108. {
  3109. struct ata_queued_cmd *qc;
  3110. qc = ata_qc_from_tag(ap, ap->active_tag);
  3111. WARN_ON(qc == NULL);
  3112. if (qc->tf.command != ATA_CMD_PACKET)
  3113. printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
  3114. /* make sure qc->err_mask is available to
  3115. * know what's wrong and recover
  3116. */
  3117. WARN_ON(qc->err_mask == 0);
  3118. ap->hsm_task_state = HSM_ST_IDLE;
  3119. ata_poll_qc_complete(qc);
  3120. }
  3121. static void ata_pio_task(void *_data)
  3122. {
  3123. struct ata_port *ap = _data;
  3124. unsigned long timeout;
  3125. int qc_completed;
  3126. fsm_start:
  3127. timeout = 0;
  3128. qc_completed = 0;
  3129. switch (ap->hsm_task_state) {
  3130. case HSM_ST_IDLE:
  3131. return;
  3132. case HSM_ST:
  3133. ata_pio_block(ap);
  3134. break;
  3135. case HSM_ST_LAST:
  3136. qc_completed = ata_pio_complete(ap);
  3137. break;
  3138. case HSM_ST_POLL:
  3139. case HSM_ST_LAST_POLL:
  3140. timeout = ata_pio_poll(ap);
  3141. break;
  3142. case HSM_ST_TMOUT:
  3143. case HSM_ST_ERR:
  3144. ata_pio_error(ap);
  3145. return;
  3146. }
  3147. if (timeout)
  3148. ata_port_queue_task(ap, ata_pio_task, ap, timeout);
  3149. else if (!qc_completed)
  3150. goto fsm_start;
  3151. }
  3152. /**
  3153. * atapi_packet_task - Write CDB bytes to hardware
  3154. * @_data: Port to which ATAPI device is attached.
  3155. *
  3156. * When device has indicated its readiness to accept
  3157. * a CDB, this function is called. Send the CDB.
  3158. * If DMA is to be performed, exit immediately.
  3159. * Otherwise, we are in polling mode, so poll
  3160. * status under operation succeeds or fails.
  3161. *
  3162. * LOCKING:
  3163. * Kernel thread context (may sleep)
  3164. */
  3165. static void atapi_packet_task(void *_data)
  3166. {
  3167. struct ata_port *ap = _data;
  3168. struct ata_queued_cmd *qc;
  3169. u8 status;
  3170. qc = ata_qc_from_tag(ap, ap->active_tag);
  3171. WARN_ON(qc == NULL);
  3172. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  3173. /* sleep-wait for BSY to clear */
  3174. DPRINTK("busy wait\n");
  3175. if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
  3176. qc->err_mask |= AC_ERR_TIMEOUT;
  3177. goto err_out;
  3178. }
  3179. /* make sure DRQ is set */
  3180. status = ata_chk_status(ap);
  3181. if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
  3182. qc->err_mask |= AC_ERR_HSM;
  3183. goto err_out;
  3184. }
  3185. /* send SCSI cdb */
  3186. DPRINTK("send cdb\n");
  3187. WARN_ON(qc->dev->cdb_len < 12);
  3188. if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
  3189. qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
  3190. unsigned long flags;
  3191. /* Once we're done issuing command and kicking bmdma,
  3192. * irq handler takes over. To not lose irq, we need
  3193. * to clear NOINTR flag before sending cdb, but
  3194. * interrupt handler shouldn't be invoked before we're
  3195. * finished. Hence, the following locking.
  3196. */
  3197. spin_lock_irqsave(&ap->host_set->lock, flags);
  3198. ap->flags &= ~ATA_FLAG_NOINTR;
  3199. ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
  3200. if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
  3201. ap->ops->bmdma_start(qc); /* initiate bmdma */
  3202. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  3203. } else {
  3204. ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
  3205. /* PIO commands are handled by polling */
  3206. ap->hsm_task_state = HSM_ST;
  3207. ata_port_queue_task(ap, ata_pio_task, ap, 0);
  3208. }
  3209. return;
  3210. err_out:
  3211. ata_poll_qc_complete(qc);
  3212. }
  3213. /**
  3214. * ata_qc_timeout - Handle timeout of queued command
  3215. * @qc: Command that timed out
  3216. *
  3217. * Some part of the kernel (currently, only the SCSI layer)
  3218. * has noticed that the active command on port @ap has not
  3219. * completed after a specified length of time. Handle this
  3220. * condition by disabling DMA (if necessary) and completing
  3221. * transactions, with error if necessary.
  3222. *
  3223. * This also handles the case of the "lost interrupt", where
  3224. * for some reason (possibly hardware bug, possibly driver bug)
  3225. * an interrupt was not delivered to the driver, even though the
  3226. * transaction completed successfully.
  3227. *
  3228. * LOCKING:
  3229. * Inherited from SCSI layer (none, can sleep)
  3230. */
  3231. static void ata_qc_timeout(struct ata_queued_cmd *qc)
  3232. {
  3233. struct ata_port *ap = qc->ap;
  3234. struct ata_host_set *host_set = ap->host_set;
  3235. u8 host_stat = 0, drv_stat;
  3236. unsigned long flags;
  3237. DPRINTK("ENTER\n");
  3238. ap->hsm_task_state = HSM_ST_IDLE;
  3239. spin_lock_irqsave(&host_set->lock, flags);
  3240. switch (qc->tf.protocol) {
  3241. case ATA_PROT_DMA:
  3242. case ATA_PROT_ATAPI_DMA:
  3243. host_stat = ap->ops->bmdma_status(ap);
  3244. /* before we do anything else, clear DMA-Start bit */
  3245. ap->ops->bmdma_stop(qc);
  3246. /* fall through */
  3247. default:
  3248. ata_altstatus(ap);
  3249. drv_stat = ata_chk_status(ap);
  3250. /* ack bmdma irq events */
  3251. ap->ops->irq_clear(ap);
  3252. printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
  3253. ap->id, qc->tf.command, drv_stat, host_stat);
  3254. /* complete taskfile transaction */
  3255. qc->err_mask |= ac_err_mask(drv_stat);
  3256. break;
  3257. }
  3258. spin_unlock_irqrestore(&host_set->lock, flags);
  3259. ata_eh_qc_complete(qc);
  3260. DPRINTK("EXIT\n");
  3261. }
  3262. /**
  3263. * ata_eng_timeout - Handle timeout of queued command
  3264. * @ap: Port on which timed-out command is active
  3265. *
  3266. * Some part of the kernel (currently, only the SCSI layer)
  3267. * has noticed that the active command on port @ap has not
  3268. * completed after a specified length of time. Handle this
  3269. * condition by disabling DMA (if necessary) and completing
  3270. * transactions, with error if necessary.
  3271. *
  3272. * This also handles the case of the "lost interrupt", where
  3273. * for some reason (possibly hardware bug, possibly driver bug)
  3274. * an interrupt was not delivered to the driver, even though the
  3275. * transaction completed successfully.
  3276. *
  3277. * LOCKING:
  3278. * Inherited from SCSI layer (none, can sleep)
  3279. */
  3280. void ata_eng_timeout(struct ata_port *ap)
  3281. {
  3282. DPRINTK("ENTER\n");
  3283. ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
  3284. DPRINTK("EXIT\n");
  3285. }
  3286. /**
  3287. * ata_qc_new - Request an available ATA command, for queueing
  3288. * @ap: Port associated with device @dev
  3289. * @dev: Device from whom we request an available command structure
  3290. *
  3291. * LOCKING:
  3292. * None.
  3293. */
  3294. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  3295. {
  3296. struct ata_queued_cmd *qc = NULL;
  3297. unsigned int i;
  3298. for (i = 0; i < ATA_MAX_QUEUE; i++)
  3299. if (!test_and_set_bit(i, &ap->qactive)) {
  3300. qc = ata_qc_from_tag(ap, i);
  3301. break;
  3302. }
  3303. if (qc)
  3304. qc->tag = i;
  3305. return qc;
  3306. }
  3307. /**
  3308. * ata_qc_new_init - Request an available ATA command, and initialize it
  3309. * @ap: Port associated with device @dev
  3310. * @dev: Device from whom we request an available command structure
  3311. *
  3312. * LOCKING:
  3313. * None.
  3314. */
  3315. struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
  3316. struct ata_device *dev)
  3317. {
  3318. struct ata_queued_cmd *qc;
  3319. qc = ata_qc_new(ap);
  3320. if (qc) {
  3321. qc->scsicmd = NULL;
  3322. qc->ap = ap;
  3323. qc->dev = dev;
  3324. ata_qc_reinit(qc);
  3325. }
  3326. return qc;
  3327. }
  3328. /**
  3329. * ata_qc_free - free unused ata_queued_cmd
  3330. * @qc: Command to complete
  3331. *
  3332. * Designed to free unused ata_queued_cmd object
  3333. * in case something prevents using it.
  3334. *
  3335. * LOCKING:
  3336. * spin_lock_irqsave(host_set lock)
  3337. */
  3338. void ata_qc_free(struct ata_queued_cmd *qc)
  3339. {
  3340. struct ata_port *ap = qc->ap;
  3341. unsigned int tag;
  3342. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3343. qc->flags = 0;
  3344. tag = qc->tag;
  3345. if (likely(ata_tag_valid(tag))) {
  3346. if (tag == ap->active_tag)
  3347. ap->active_tag = ATA_TAG_POISON;
  3348. qc->tag = ATA_TAG_POISON;
  3349. clear_bit(tag, &ap->qactive);
  3350. }
  3351. }
  3352. void __ata_qc_complete(struct ata_queued_cmd *qc)
  3353. {
  3354. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3355. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  3356. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  3357. ata_sg_clean(qc);
  3358. /* atapi: mark qc as inactive to prevent the interrupt handler
  3359. * from completing the command twice later, before the error handler
  3360. * is called. (when rc != 0 and atapi request sense is needed)
  3361. */
  3362. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  3363. /* call completion callback */
  3364. qc->complete_fn(qc);
  3365. }
  3366. static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
  3367. {
  3368. struct ata_port *ap = qc->ap;
  3369. switch (qc->tf.protocol) {
  3370. case ATA_PROT_DMA:
  3371. case ATA_PROT_ATAPI_DMA:
  3372. return 1;
  3373. case ATA_PROT_ATAPI:
  3374. case ATA_PROT_PIO:
  3375. if (ap->flags & ATA_FLAG_PIO_DMA)
  3376. return 1;
  3377. /* fall through */
  3378. default:
  3379. return 0;
  3380. }
  3381. /* never reached */
  3382. }
  3383. /**
  3384. * ata_qc_issue - issue taskfile to device
  3385. * @qc: command to issue to device
  3386. *
  3387. * Prepare an ATA command to submission to device.
  3388. * This includes mapping the data into a DMA-able
  3389. * area, filling in the S/G table, and finally
  3390. * writing the taskfile to hardware, starting the command.
  3391. *
  3392. * LOCKING:
  3393. * spin_lock_irqsave(host_set lock)
  3394. */
  3395. void ata_qc_issue(struct ata_queued_cmd *qc)
  3396. {
  3397. struct ata_port *ap = qc->ap;
  3398. qc->ap->active_tag = qc->tag;
  3399. qc->flags |= ATA_QCFLAG_ACTIVE;
  3400. if (ata_should_dma_map(qc)) {
  3401. if (qc->flags & ATA_QCFLAG_SG) {
  3402. if (ata_sg_setup(qc))
  3403. goto sg_err;
  3404. } else if (qc->flags & ATA_QCFLAG_SINGLE) {
  3405. if (ata_sg_setup_one(qc))
  3406. goto sg_err;
  3407. }
  3408. } else {
  3409. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3410. }
  3411. ap->ops->qc_prep(qc);
  3412. qc->err_mask |= ap->ops->qc_issue(qc);
  3413. if (unlikely(qc->err_mask))
  3414. goto err;
  3415. return;
  3416. sg_err:
  3417. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3418. qc->err_mask |= AC_ERR_SYSTEM;
  3419. err:
  3420. ata_qc_complete(qc);
  3421. }
  3422. /**
  3423. * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
  3424. * @qc: command to issue to device
  3425. *
  3426. * Using various libata functions and hooks, this function
  3427. * starts an ATA command. ATA commands are grouped into
  3428. * classes called "protocols", and issuing each type of protocol
  3429. * is slightly different.
  3430. *
  3431. * May be used as the qc_issue() entry in ata_port_operations.
  3432. *
  3433. * LOCKING:
  3434. * spin_lock_irqsave(host_set lock)
  3435. *
  3436. * RETURNS:
  3437. * Zero on success, AC_ERR_* mask on failure
  3438. */
  3439. unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  3440. {
  3441. struct ata_port *ap = qc->ap;
  3442. ata_dev_select(ap, qc->dev->devno, 1, 0);
  3443. switch (qc->tf.protocol) {
  3444. case ATA_PROT_NODATA:
  3445. ata_tf_to_host(ap, &qc->tf);
  3446. break;
  3447. case ATA_PROT_DMA:
  3448. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  3449. ap->ops->bmdma_setup(qc); /* set up bmdma */
  3450. ap->ops->bmdma_start(qc); /* initiate bmdma */
  3451. break;
  3452. case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
  3453. ata_qc_set_polling(qc);
  3454. ata_tf_to_host(ap, &qc->tf);
  3455. ap->hsm_task_state = HSM_ST;
  3456. ata_port_queue_task(ap, ata_pio_task, ap, 0);
  3457. break;
  3458. case ATA_PROT_ATAPI:
  3459. ata_qc_set_polling(qc);
  3460. ata_tf_to_host(ap, &qc->tf);
  3461. ata_port_queue_task(ap, atapi_packet_task, ap, 0);
  3462. break;
  3463. case ATA_PROT_ATAPI_NODATA:
  3464. ap->flags |= ATA_FLAG_NOINTR;
  3465. ata_tf_to_host(ap, &qc->tf);
  3466. ata_port_queue_task(ap, atapi_packet_task, ap, 0);
  3467. break;
  3468. case ATA_PROT_ATAPI_DMA:
  3469. ap->flags |= ATA_FLAG_NOINTR;
  3470. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  3471. ap->ops->bmdma_setup(qc); /* set up bmdma */
  3472. ata_port_queue_task(ap, atapi_packet_task, ap, 0);
  3473. break;
  3474. default:
  3475. WARN_ON(1);
  3476. return AC_ERR_SYSTEM;
  3477. }
  3478. return 0;
  3479. }
  3480. /**
  3481. * ata_host_intr - Handle host interrupt for given (port, task)
  3482. * @ap: Port on which interrupt arrived (possibly...)
  3483. * @qc: Taskfile currently active in engine
  3484. *
  3485. * Handle host interrupt for given queued command. Currently,
  3486. * only DMA interrupts are handled. All other commands are
  3487. * handled via polling with interrupts disabled (nIEN bit).
  3488. *
  3489. * LOCKING:
  3490. * spin_lock_irqsave(host_set lock)
  3491. *
  3492. * RETURNS:
  3493. * One if interrupt was handled, zero if not (shared irq).
  3494. */
  3495. inline unsigned int ata_host_intr (struct ata_port *ap,
  3496. struct ata_queued_cmd *qc)
  3497. {
  3498. u8 status, host_stat;
  3499. switch (qc->tf.protocol) {
  3500. case ATA_PROT_DMA:
  3501. case ATA_PROT_ATAPI_DMA:
  3502. case ATA_PROT_ATAPI:
  3503. /* check status of DMA engine */
  3504. host_stat = ap->ops->bmdma_status(ap);
  3505. VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
  3506. /* if it's not our irq... */
  3507. if (!(host_stat & ATA_DMA_INTR))
  3508. goto idle_irq;
  3509. /* before we do anything else, clear DMA-Start bit */
  3510. ap->ops->bmdma_stop(qc);
  3511. /* fall through */
  3512. case ATA_PROT_ATAPI_NODATA:
  3513. case ATA_PROT_NODATA:
  3514. /* check altstatus */
  3515. status = ata_altstatus(ap);
  3516. if (status & ATA_BUSY)
  3517. goto idle_irq;
  3518. /* check main status, clearing INTRQ */
  3519. status = ata_chk_status(ap);
  3520. if (unlikely(status & ATA_BUSY))
  3521. goto idle_irq;
  3522. DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
  3523. ap->id, qc->tf.protocol, status);
  3524. /* ack bmdma irq events */
  3525. ap->ops->irq_clear(ap);
  3526. /* complete taskfile transaction */
  3527. qc->err_mask |= ac_err_mask(status);
  3528. ata_qc_complete(qc);
  3529. break;
  3530. default:
  3531. goto idle_irq;
  3532. }
  3533. return 1; /* irq handled */
  3534. idle_irq:
  3535. ap->stats.idle_irq++;
  3536. #ifdef ATA_IRQ_TRAP
  3537. if ((ap->stats.idle_irq % 1000) == 0) {
  3538. ata_irq_ack(ap, 0); /* debug trap */
  3539. printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
  3540. return 1;
  3541. }
  3542. #endif
  3543. return 0; /* irq not handled */
  3544. }
  3545. /**
  3546. * ata_interrupt - Default ATA host interrupt handler
  3547. * @irq: irq line (unused)
  3548. * @dev_instance: pointer to our ata_host_set information structure
  3549. * @regs: unused
  3550. *
  3551. * Default interrupt handler for PCI IDE devices. Calls
  3552. * ata_host_intr() for each port that is not disabled.
  3553. *
  3554. * LOCKING:
  3555. * Obtains host_set lock during operation.
  3556. *
  3557. * RETURNS:
  3558. * IRQ_NONE or IRQ_HANDLED.
  3559. */
  3560. irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
  3561. {
  3562. struct ata_host_set *host_set = dev_instance;
  3563. unsigned int i;
  3564. unsigned int handled = 0;
  3565. unsigned long flags;
  3566. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  3567. spin_lock_irqsave(&host_set->lock, flags);
  3568. for (i = 0; i < host_set->n_ports; i++) {
  3569. struct ata_port *ap;
  3570. ap = host_set->ports[i];
  3571. if (ap &&
  3572. !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
  3573. struct ata_queued_cmd *qc;
  3574. qc = ata_qc_from_tag(ap, ap->active_tag);
  3575. if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
  3576. (qc->flags & ATA_QCFLAG_ACTIVE))
  3577. handled |= ata_host_intr(ap, qc);
  3578. }
  3579. }
  3580. spin_unlock_irqrestore(&host_set->lock, flags);
  3581. return IRQ_RETVAL(handled);
  3582. }
  3583. /*
  3584. * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
  3585. * without filling any other registers
  3586. */
  3587. static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
  3588. u8 cmd)
  3589. {
  3590. struct ata_taskfile tf;
  3591. int err;
  3592. ata_tf_init(ap, &tf, dev->devno);
  3593. tf.command = cmd;
  3594. tf.flags |= ATA_TFLAG_DEVICE;
  3595. tf.protocol = ATA_PROT_NODATA;
  3596. err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
  3597. if (err)
  3598. printk(KERN_ERR "%s: ata command failed: %d\n",
  3599. __FUNCTION__, err);
  3600. return err;
  3601. }
  3602. static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
  3603. {
  3604. u8 cmd;
  3605. if (!ata_try_flush_cache(dev))
  3606. return 0;
  3607. if (ata_id_has_flush_ext(dev->id))
  3608. cmd = ATA_CMD_FLUSH_EXT;
  3609. else
  3610. cmd = ATA_CMD_FLUSH;
  3611. return ata_do_simple_cmd(ap, dev, cmd);
  3612. }
  3613. static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
  3614. {
  3615. return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
  3616. }
  3617. static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
  3618. {
  3619. return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
  3620. }
  3621. /**
  3622. * ata_device_resume - wakeup a previously suspended devices
  3623. * @ap: port the device is connected to
  3624. * @dev: the device to resume
  3625. *
  3626. * Kick the drive back into action, by sending it an idle immediate
  3627. * command and making sure its transfer mode matches between drive
  3628. * and host.
  3629. *
  3630. */
  3631. int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
  3632. {
  3633. if (ap->flags & ATA_FLAG_SUSPENDED) {
  3634. ap->flags &= ~ATA_FLAG_SUSPENDED;
  3635. ata_set_mode(ap);
  3636. }
  3637. if (!ata_dev_enabled(dev))
  3638. return 0;
  3639. if (dev->class == ATA_DEV_ATA)
  3640. ata_start_drive(ap, dev);
  3641. return 0;
  3642. }
  3643. /**
  3644. * ata_device_suspend - prepare a device for suspend
  3645. * @ap: port the device is connected to
  3646. * @dev: the device to suspend
  3647. *
  3648. * Flush the cache on the drive, if appropriate, then issue a
  3649. * standbynow command.
  3650. */
  3651. int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state)
  3652. {
  3653. if (!ata_dev_enabled(dev))
  3654. return 0;
  3655. if (dev->class == ATA_DEV_ATA)
  3656. ata_flush_cache(ap, dev);
  3657. if (state.event != PM_EVENT_FREEZE)
  3658. ata_standby_drive(ap, dev);
  3659. ap->flags |= ATA_FLAG_SUSPENDED;
  3660. return 0;
  3661. }
  3662. /**
  3663. * ata_port_start - Set port up for dma.
  3664. * @ap: Port to initialize
  3665. *
  3666. * Called just after data structures for each port are
  3667. * initialized. Allocates space for PRD table.
  3668. *
  3669. * May be used as the port_start() entry in ata_port_operations.
  3670. *
  3671. * LOCKING:
  3672. * Inherited from caller.
  3673. */
  3674. int ata_port_start (struct ata_port *ap)
  3675. {
  3676. struct device *dev = ap->dev;
  3677. int rc;
  3678. ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
  3679. if (!ap->prd)
  3680. return -ENOMEM;
  3681. rc = ata_pad_alloc(ap, dev);
  3682. if (rc) {
  3683. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  3684. return rc;
  3685. }
  3686. DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
  3687. return 0;
  3688. }
  3689. /**
  3690. * ata_port_stop - Undo ata_port_start()
  3691. * @ap: Port to shut down
  3692. *
  3693. * Frees the PRD table.
  3694. *
  3695. * May be used as the port_stop() entry in ata_port_operations.
  3696. *
  3697. * LOCKING:
  3698. * Inherited from caller.
  3699. */
  3700. void ata_port_stop (struct ata_port *ap)
  3701. {
  3702. struct device *dev = ap->dev;
  3703. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  3704. ata_pad_free(ap, dev);
  3705. }
  3706. void ata_host_stop (struct ata_host_set *host_set)
  3707. {
  3708. if (host_set->mmio_base)
  3709. iounmap(host_set->mmio_base);
  3710. }
  3711. /**
  3712. * ata_host_remove - Unregister SCSI host structure with upper layers
  3713. * @ap: Port to unregister
  3714. * @do_unregister: 1 if we fully unregister, 0 to just stop the port
  3715. *
  3716. * LOCKING:
  3717. * Inherited from caller.
  3718. */
  3719. static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
  3720. {
  3721. struct Scsi_Host *sh = ap->host;
  3722. DPRINTK("ENTER\n");
  3723. if (do_unregister)
  3724. scsi_remove_host(sh);
  3725. ap->ops->port_stop(ap);
  3726. }
  3727. /**
  3728. * ata_host_init - Initialize an ata_port structure
  3729. * @ap: Structure to initialize
  3730. * @host: associated SCSI mid-layer structure
  3731. * @host_set: Collection of hosts to which @ap belongs
  3732. * @ent: Probe information provided by low-level driver
  3733. * @port_no: Port number associated with this ata_port
  3734. *
  3735. * Initialize a new ata_port structure, and its associated
  3736. * scsi_host.
  3737. *
  3738. * LOCKING:
  3739. * Inherited from caller.
  3740. */
  3741. static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
  3742. struct ata_host_set *host_set,
  3743. const struct ata_probe_ent *ent, unsigned int port_no)
  3744. {
  3745. unsigned int i;
  3746. host->max_id = 16;
  3747. host->max_lun = 1;
  3748. host->max_channel = 1;
  3749. host->unique_id = ata_unique_id++;
  3750. host->max_cmd_len = 12;
  3751. ap->flags = ATA_FLAG_PORT_DISABLED;
  3752. ap->id = host->unique_id;
  3753. ap->host = host;
  3754. ap->ctl = ATA_DEVCTL_OBS;
  3755. ap->host_set = host_set;
  3756. ap->dev = ent->dev;
  3757. ap->port_no = port_no;
  3758. ap->hard_port_no =
  3759. ent->legacy_mode ? ent->hard_port_no : port_no;
  3760. ap->pio_mask = ent->pio_mask;
  3761. ap->mwdma_mask = ent->mwdma_mask;
  3762. ap->udma_mask = ent->udma_mask;
  3763. ap->flags |= ent->host_flags;
  3764. ap->ops = ent->port_ops;
  3765. ap->cbl = ATA_CBL_NONE;
  3766. ap->active_tag = ATA_TAG_POISON;
  3767. ap->last_ctl = 0xFF;
  3768. INIT_WORK(&ap->port_task, NULL, NULL);
  3769. INIT_LIST_HEAD(&ap->eh_done_q);
  3770. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  3771. struct ata_device *dev = &ap->device[i];
  3772. dev->devno = i;
  3773. dev->pio_mask = UINT_MAX;
  3774. dev->mwdma_mask = UINT_MAX;
  3775. dev->udma_mask = UINT_MAX;
  3776. }
  3777. #ifdef ATA_IRQ_TRAP
  3778. ap->stats.unhandled_irq = 1;
  3779. ap->stats.idle_irq = 1;
  3780. #endif
  3781. memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
  3782. }
  3783. /**
  3784. * ata_host_add - Attach low-level ATA driver to system
  3785. * @ent: Information provided by low-level driver
  3786. * @host_set: Collections of ports to which we add
  3787. * @port_no: Port number associated with this host
  3788. *
  3789. * Attach low-level ATA driver to system.
  3790. *
  3791. * LOCKING:
  3792. * PCI/etc. bus probe sem.
  3793. *
  3794. * RETURNS:
  3795. * New ata_port on success, for NULL on error.
  3796. */
  3797. static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
  3798. struct ata_host_set *host_set,
  3799. unsigned int port_no)
  3800. {
  3801. struct Scsi_Host *host;
  3802. struct ata_port *ap;
  3803. int rc;
  3804. DPRINTK("ENTER\n");
  3805. if (!ent->port_ops->probe_reset &&
  3806. !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
  3807. printk(KERN_ERR "ata%u: no reset mechanism available\n",
  3808. port_no);
  3809. return NULL;
  3810. }
  3811. host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
  3812. if (!host)
  3813. return NULL;
  3814. host->transportt = &ata_scsi_transport_template;
  3815. ap = (struct ata_port *) &host->hostdata[0];
  3816. ata_host_init(ap, host, host_set, ent, port_no);
  3817. rc = ap->ops->port_start(ap);
  3818. if (rc)
  3819. goto err_out;
  3820. return ap;
  3821. err_out:
  3822. scsi_host_put(host);
  3823. return NULL;
  3824. }
  3825. /**
  3826. * ata_device_add - Register hardware device with ATA and SCSI layers
  3827. * @ent: Probe information describing hardware device to be registered
  3828. *
  3829. * This function processes the information provided in the probe
  3830. * information struct @ent, allocates the necessary ATA and SCSI
  3831. * host information structures, initializes them, and registers
  3832. * everything with requisite kernel subsystems.
  3833. *
  3834. * This function requests irqs, probes the ATA bus, and probes
  3835. * the SCSI bus.
  3836. *
  3837. * LOCKING:
  3838. * PCI/etc. bus probe sem.
  3839. *
  3840. * RETURNS:
  3841. * Number of ports registered. Zero on error (no ports registered).
  3842. */
  3843. int ata_device_add(const struct ata_probe_ent *ent)
  3844. {
  3845. unsigned int count = 0, i;
  3846. struct device *dev = ent->dev;
  3847. struct ata_host_set *host_set;
  3848. DPRINTK("ENTER\n");
  3849. /* alloc a container for our list of ATA ports (buses) */
  3850. host_set = kzalloc(sizeof(struct ata_host_set) +
  3851. (ent->n_ports * sizeof(void *)), GFP_KERNEL);
  3852. if (!host_set)
  3853. return 0;
  3854. spin_lock_init(&host_set->lock);
  3855. host_set->dev = dev;
  3856. host_set->n_ports = ent->n_ports;
  3857. host_set->irq = ent->irq;
  3858. host_set->mmio_base = ent->mmio_base;
  3859. host_set->private_data = ent->private_data;
  3860. host_set->ops = ent->port_ops;
  3861. host_set->flags = ent->host_set_flags;
  3862. /* register each port bound to this device */
  3863. for (i = 0; i < ent->n_ports; i++) {
  3864. struct ata_port *ap;
  3865. unsigned long xfer_mode_mask;
  3866. ap = ata_host_add(ent, host_set, i);
  3867. if (!ap)
  3868. goto err_out;
  3869. host_set->ports[i] = ap;
  3870. xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
  3871. (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
  3872. (ap->pio_mask << ATA_SHIFT_PIO);
  3873. /* print per-port info to dmesg */
  3874. printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
  3875. "bmdma 0x%lX irq %lu\n",
  3876. ap->id,
  3877. ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
  3878. ata_mode_string(xfer_mode_mask),
  3879. ap->ioaddr.cmd_addr,
  3880. ap->ioaddr.ctl_addr,
  3881. ap->ioaddr.bmdma_addr,
  3882. ent->irq);
  3883. ata_chk_status(ap);
  3884. host_set->ops->irq_clear(ap);
  3885. count++;
  3886. }
  3887. if (!count)
  3888. goto err_free_ret;
  3889. /* obtain irq, that is shared between channels */
  3890. if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
  3891. DRV_NAME, host_set))
  3892. goto err_out;
  3893. /* perform each probe synchronously */
  3894. DPRINTK("probe begin\n");
  3895. for (i = 0; i < count; i++) {
  3896. struct ata_port *ap;
  3897. int rc;
  3898. ap = host_set->ports[i];
  3899. DPRINTK("ata%u: bus probe begin\n", ap->id);
  3900. rc = ata_bus_probe(ap);
  3901. DPRINTK("ata%u: bus probe end\n", ap->id);
  3902. if (rc) {
  3903. /* FIXME: do something useful here?
  3904. * Current libata behavior will
  3905. * tear down everything when
  3906. * the module is removed
  3907. * or the h/w is unplugged.
  3908. */
  3909. }
  3910. rc = scsi_add_host(ap->host, dev);
  3911. if (rc) {
  3912. printk(KERN_ERR "ata%u: scsi_add_host failed\n",
  3913. ap->id);
  3914. /* FIXME: do something useful here */
  3915. /* FIXME: handle unconditional calls to
  3916. * scsi_scan_host and ata_host_remove, below,
  3917. * at the very least
  3918. */
  3919. }
  3920. }
  3921. /* probes are done, now scan each port's disk(s) */
  3922. DPRINTK("host probe begin\n");
  3923. for (i = 0; i < count; i++) {
  3924. struct ata_port *ap = host_set->ports[i];
  3925. ata_scsi_scan_host(ap);
  3926. }
  3927. dev_set_drvdata(dev, host_set);
  3928. VPRINTK("EXIT, returning %u\n", ent->n_ports);
  3929. return ent->n_ports; /* success */
  3930. err_out:
  3931. for (i = 0; i < count; i++) {
  3932. ata_host_remove(host_set->ports[i], 1);
  3933. scsi_host_put(host_set->ports[i]->host);
  3934. }
  3935. err_free_ret:
  3936. kfree(host_set);
  3937. VPRINTK("EXIT, returning 0\n");
  3938. return 0;
  3939. }
  3940. /**
  3941. * ata_host_set_remove - PCI layer callback for device removal
  3942. * @host_set: ATA host set that was removed
  3943. *
  3944. * Unregister all objects associated with this host set. Free those
  3945. * objects.
  3946. *
  3947. * LOCKING:
  3948. * Inherited from calling layer (may sleep).
  3949. */
  3950. void ata_host_set_remove(struct ata_host_set *host_set)
  3951. {
  3952. struct ata_port *ap;
  3953. unsigned int i;
  3954. for (i = 0; i < host_set->n_ports; i++) {
  3955. ap = host_set->ports[i];
  3956. scsi_remove_host(ap->host);
  3957. }
  3958. free_irq(host_set->irq, host_set);
  3959. for (i = 0; i < host_set->n_ports; i++) {
  3960. ap = host_set->ports[i];
  3961. ata_scsi_release(ap->host);
  3962. if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
  3963. struct ata_ioports *ioaddr = &ap->ioaddr;
  3964. if (ioaddr->cmd_addr == 0x1f0)
  3965. release_region(0x1f0, 8);
  3966. else if (ioaddr->cmd_addr == 0x170)
  3967. release_region(0x170, 8);
  3968. }
  3969. scsi_host_put(ap->host);
  3970. }
  3971. if (host_set->ops->host_stop)
  3972. host_set->ops->host_stop(host_set);
  3973. kfree(host_set);
  3974. }
  3975. /**
  3976. * ata_scsi_release - SCSI layer callback hook for host unload
  3977. * @host: libata host to be unloaded
  3978. *
  3979. * Performs all duties necessary to shut down a libata port...
  3980. * Kill port kthread, disable port, and release resources.
  3981. *
  3982. * LOCKING:
  3983. * Inherited from SCSI layer.
  3984. *
  3985. * RETURNS:
  3986. * One.
  3987. */
  3988. int ata_scsi_release(struct Scsi_Host *host)
  3989. {
  3990. struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
  3991. int i;
  3992. DPRINTK("ENTER\n");
  3993. ap->ops->port_disable(ap);
  3994. ata_host_remove(ap, 0);
  3995. for (i = 0; i < ATA_MAX_DEVICES; i++)
  3996. kfree(ap->device[i].id);
  3997. DPRINTK("EXIT\n");
  3998. return 1;
  3999. }
  4000. /**
  4001. * ata_std_ports - initialize ioaddr with standard port offsets.
  4002. * @ioaddr: IO address structure to be initialized
  4003. *
  4004. * Utility function which initializes data_addr, error_addr,
  4005. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  4006. * device_addr, status_addr, and command_addr to standard offsets
  4007. * relative to cmd_addr.
  4008. *
  4009. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  4010. */
  4011. void ata_std_ports(struct ata_ioports *ioaddr)
  4012. {
  4013. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  4014. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  4015. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  4016. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  4017. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  4018. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  4019. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  4020. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  4021. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  4022. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  4023. }
  4024. #ifdef CONFIG_PCI
  4025. void ata_pci_host_stop (struct ata_host_set *host_set)
  4026. {
  4027. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  4028. pci_iounmap(pdev, host_set->mmio_base);
  4029. }
  4030. /**
  4031. * ata_pci_remove_one - PCI layer callback for device removal
  4032. * @pdev: PCI device that was removed
  4033. *
  4034. * PCI layer indicates to libata via this hook that
  4035. * hot-unplug or module unload event has occurred.
  4036. * Handle this by unregistering all objects associated
  4037. * with this PCI device. Free those objects. Then finally
  4038. * release PCI resources and disable device.
  4039. *
  4040. * LOCKING:
  4041. * Inherited from PCI layer (may sleep).
  4042. */
  4043. void ata_pci_remove_one (struct pci_dev *pdev)
  4044. {
  4045. struct device *dev = pci_dev_to_dev(pdev);
  4046. struct ata_host_set *host_set = dev_get_drvdata(dev);
  4047. ata_host_set_remove(host_set);
  4048. pci_release_regions(pdev);
  4049. pci_disable_device(pdev);
  4050. dev_set_drvdata(dev, NULL);
  4051. }
  4052. /* move to PCI subsystem */
  4053. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  4054. {
  4055. unsigned long tmp = 0;
  4056. switch (bits->width) {
  4057. case 1: {
  4058. u8 tmp8 = 0;
  4059. pci_read_config_byte(pdev, bits->reg, &tmp8);
  4060. tmp = tmp8;
  4061. break;
  4062. }
  4063. case 2: {
  4064. u16 tmp16 = 0;
  4065. pci_read_config_word(pdev, bits->reg, &tmp16);
  4066. tmp = tmp16;
  4067. break;
  4068. }
  4069. case 4: {
  4070. u32 tmp32 = 0;
  4071. pci_read_config_dword(pdev, bits->reg, &tmp32);
  4072. tmp = tmp32;
  4073. break;
  4074. }
  4075. default:
  4076. return -EINVAL;
  4077. }
  4078. tmp &= bits->mask;
  4079. return (tmp == bits->val) ? 1 : 0;
  4080. }
  4081. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
  4082. {
  4083. pci_save_state(pdev);
  4084. pci_disable_device(pdev);
  4085. pci_set_power_state(pdev, PCI_D3hot);
  4086. return 0;
  4087. }
  4088. int ata_pci_device_resume(struct pci_dev *pdev)
  4089. {
  4090. pci_set_power_state(pdev, PCI_D0);
  4091. pci_restore_state(pdev);
  4092. pci_enable_device(pdev);
  4093. pci_set_master(pdev);
  4094. return 0;
  4095. }
  4096. #endif /* CONFIG_PCI */
  4097. static int __init ata_init(void)
  4098. {
  4099. ata_wq = create_workqueue("ata");
  4100. if (!ata_wq)
  4101. return -ENOMEM;
  4102. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  4103. return 0;
  4104. }
  4105. static void __exit ata_exit(void)
  4106. {
  4107. destroy_workqueue(ata_wq);
  4108. }
  4109. module_init(ata_init);
  4110. module_exit(ata_exit);
  4111. static unsigned long ratelimit_time;
  4112. static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
  4113. int ata_ratelimit(void)
  4114. {
  4115. int rc;
  4116. unsigned long flags;
  4117. spin_lock_irqsave(&ata_ratelimit_lock, flags);
  4118. if (time_after(jiffies, ratelimit_time)) {
  4119. rc = 1;
  4120. ratelimit_time = jiffies + (HZ/5);
  4121. } else
  4122. rc = 0;
  4123. spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
  4124. return rc;
  4125. }
  4126. /*
  4127. * libata is essentially a library of internal helper functions for
  4128. * low-level ATA host controller drivers. As such, the API/ABI is
  4129. * likely to change as new drivers are added and updated.
  4130. * Do not depend on ABI/API stability.
  4131. */
  4132. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  4133. EXPORT_SYMBOL_GPL(ata_std_ports);
  4134. EXPORT_SYMBOL_GPL(ata_device_add);
  4135. EXPORT_SYMBOL_GPL(ata_host_set_remove);
  4136. EXPORT_SYMBOL_GPL(ata_sg_init);
  4137. EXPORT_SYMBOL_GPL(ata_sg_init_one);
  4138. EXPORT_SYMBOL_GPL(__ata_qc_complete);
  4139. EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
  4140. EXPORT_SYMBOL_GPL(ata_eng_timeout);
  4141. EXPORT_SYMBOL_GPL(ata_tf_load);
  4142. EXPORT_SYMBOL_GPL(ata_tf_read);
  4143. EXPORT_SYMBOL_GPL(ata_noop_dev_select);
  4144. EXPORT_SYMBOL_GPL(ata_std_dev_select);
  4145. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  4146. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  4147. EXPORT_SYMBOL_GPL(ata_check_status);
  4148. EXPORT_SYMBOL_GPL(ata_altstatus);
  4149. EXPORT_SYMBOL_GPL(ata_exec_command);
  4150. EXPORT_SYMBOL_GPL(ata_port_start);
  4151. EXPORT_SYMBOL_GPL(ata_port_stop);
  4152. EXPORT_SYMBOL_GPL(ata_host_stop);
  4153. EXPORT_SYMBOL_GPL(ata_interrupt);
  4154. EXPORT_SYMBOL_GPL(ata_qc_prep);
  4155. EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
  4156. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  4157. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  4158. EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
  4159. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  4160. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  4161. EXPORT_SYMBOL_GPL(ata_port_probe);
  4162. EXPORT_SYMBOL_GPL(sata_phy_reset);
  4163. EXPORT_SYMBOL_GPL(__sata_phy_reset);
  4164. EXPORT_SYMBOL_GPL(ata_bus_reset);
  4165. EXPORT_SYMBOL_GPL(ata_std_probeinit);
  4166. EXPORT_SYMBOL_GPL(ata_std_softreset);
  4167. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  4168. EXPORT_SYMBOL_GPL(ata_std_postreset);
  4169. EXPORT_SYMBOL_GPL(ata_std_probe_reset);
  4170. EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
  4171. EXPORT_SYMBOL_GPL(ata_dev_revalidate);
  4172. EXPORT_SYMBOL_GPL(ata_dev_classify);
  4173. EXPORT_SYMBOL_GPL(ata_dev_pair);
  4174. EXPORT_SYMBOL_GPL(ata_port_disable);
  4175. EXPORT_SYMBOL_GPL(ata_ratelimit);
  4176. EXPORT_SYMBOL_GPL(ata_busy_sleep);
  4177. EXPORT_SYMBOL_GPL(ata_port_queue_task);
  4178. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  4179. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  4180. EXPORT_SYMBOL_GPL(ata_scsi_error);
  4181. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  4182. EXPORT_SYMBOL_GPL(ata_scsi_release);
  4183. EXPORT_SYMBOL_GPL(ata_host_intr);
  4184. EXPORT_SYMBOL_GPL(ata_id_string);
  4185. EXPORT_SYMBOL_GPL(ata_id_c_string);
  4186. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  4187. EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
  4188. EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
  4189. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  4190. EXPORT_SYMBOL_GPL(ata_timing_compute);
  4191. EXPORT_SYMBOL_GPL(ata_timing_merge);
  4192. #ifdef CONFIG_PCI
  4193. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  4194. EXPORT_SYMBOL_GPL(ata_pci_host_stop);
  4195. EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
  4196. EXPORT_SYMBOL_GPL(ata_pci_init_one);
  4197. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  4198. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  4199. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  4200. EXPORT_SYMBOL_GPL(ata_pci_default_filter);
  4201. EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
  4202. #endif /* CONFIG_PCI */
  4203. EXPORT_SYMBOL_GPL(ata_device_suspend);
  4204. EXPORT_SYMBOL_GPL(ata_device_resume);
  4205. EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
  4206. EXPORT_SYMBOL_GPL(ata_scsi_device_resume);