libata-core.c 126 KB

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