libata-core.c 124 KB

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