libata-core.c 125 KB

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