libata-core.c 122 KB

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