libata-core.c 130 KB

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