libata-core.c 135 KB

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