libata-core.c 125 KB

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