libata-core.c 125 KB

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