libata-core.c 128 KB

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