libata-core.c 123 KB

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