libata-core.c 118 KB

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