libata-core.c 123 KB

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