libata-core.c 122 KB

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