libata-core.c 124 KB

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