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