libata-core.c 123 KB

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