libata-core.c 122 KB

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