mtip32xx.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871
  1. /*
  2. * Driver for the Micron P320 SSD
  3. * Copyright (C) 2011 Micron Technology, Inc.
  4. *
  5. * Portions of this code were derived from works subjected to the
  6. * following copyright:
  7. * Copyright (C) 2009 Integrated Device Technology, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include <linux/pci.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/ata.h>
  23. #include <linux/delay.h>
  24. #include <linux/hdreg.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/random.h>
  27. #include <linux/smp.h>
  28. #include <linux/compat.h>
  29. #include <linux/fs.h>
  30. #include <linux/module.h>
  31. #include <linux/genhd.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/bio.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/idr.h>
  36. #include <linux/kthread.h>
  37. #include <../drivers/ata/ahci.h>
  38. #include <linux/export.h>
  39. #include <linux/debugfs.h>
  40. #include "mtip32xx.h"
  41. #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
  42. #define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
  43. #define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
  44. #define HW_PORT_PRIV_DMA_SZ \
  45. (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
  46. #define HOST_CAP_NZDMA (1 << 19)
  47. #define HOST_HSORG 0xFC
  48. #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
  49. #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
  50. #define HSORG_HWREV 0xFF00
  51. #define HSORG_STYLE 0x8
  52. #define HSORG_SLOTGROUPS 0x7
  53. #define PORT_COMMAND_ISSUE 0x38
  54. #define PORT_SDBV 0x7C
  55. #define PORT_OFFSET 0x100
  56. #define PORT_MEM_SIZE 0x80
  57. #define PORT_IRQ_ERR \
  58. (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
  59. PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
  60. PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
  61. PORT_IRQ_OVERFLOW)
  62. #define PORT_IRQ_LEGACY \
  63. (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
  64. #define PORT_IRQ_HANDLED \
  65. (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
  66. PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
  67. PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
  68. #define DEF_PORT_IRQ \
  69. (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
  70. /* product numbers */
  71. #define MTIP_PRODUCT_UNKNOWN 0x00
  72. #define MTIP_PRODUCT_ASICFPGA 0x11
  73. /* Device instance number, incremented each time a device is probed. */
  74. static int instance;
  75. struct list_head online_list;
  76. struct list_head removing_list;
  77. spinlock_t dev_lock;
  78. /*
  79. * Global variable used to hold the major block device number
  80. * allocated in mtip_init().
  81. */
  82. static int mtip_major;
  83. static struct dentry *dfs_parent;
  84. static struct dentry *dfs_device_status;
  85. static u32 cpu_use[NR_CPUS];
  86. static DEFINE_SPINLOCK(rssd_index_lock);
  87. static DEFINE_IDA(rssd_index_ida);
  88. static int mtip_block_initialize(struct driver_data *dd);
  89. #ifdef CONFIG_COMPAT
  90. struct mtip_compat_ide_task_request_s {
  91. __u8 io_ports[8];
  92. __u8 hob_ports[8];
  93. ide_reg_valid_t out_flags;
  94. ide_reg_valid_t in_flags;
  95. int data_phase;
  96. int req_cmd;
  97. compat_ulong_t out_size;
  98. compat_ulong_t in_size;
  99. };
  100. #endif
  101. /*
  102. * This function check_for_surprise_removal is called
  103. * while card is removed from the system and it will
  104. * read the vendor id from the configration space
  105. *
  106. * @pdev Pointer to the pci_dev structure.
  107. *
  108. * return value
  109. * true if device removed, else false
  110. */
  111. static bool mtip_check_surprise_removal(struct pci_dev *pdev)
  112. {
  113. u16 vendor_id = 0;
  114. struct driver_data *dd = pci_get_drvdata(pdev);
  115. if (dd->sr)
  116. return true;
  117. /* Read the vendorID from the configuration space */
  118. pci_read_config_word(pdev, 0x00, &vendor_id);
  119. if (vendor_id == 0xFFFF) {
  120. dd->sr = true;
  121. if (dd->queue)
  122. set_bit(QUEUE_FLAG_DEAD, &dd->queue->queue_flags);
  123. else
  124. dev_warn(&dd->pdev->dev,
  125. "%s: dd->queue is NULL\n", __func__);
  126. if (dd->port) {
  127. set_bit(MTIP_PF_SR_CLEANUP_BIT, &dd->port->flags);
  128. wake_up_interruptible(&dd->port->svc_wait);
  129. } else
  130. dev_warn(&dd->pdev->dev,
  131. "%s: dd->port is NULL\n", __func__);
  132. return true; /* device removed */
  133. }
  134. return false; /* device present */
  135. }
  136. /*
  137. * Obtain an empty command slot.
  138. *
  139. * This function needs to be reentrant since it could be called
  140. * at the same time on multiple CPUs. The allocation of the
  141. * command slot must be atomic.
  142. *
  143. * @port Pointer to the port data structure.
  144. *
  145. * return value
  146. * >= 0 Index of command slot obtained.
  147. * -1 No command slots available.
  148. */
  149. static int get_slot(struct mtip_port *port)
  150. {
  151. int slot, i;
  152. unsigned int num_command_slots = port->dd->slot_groups * 32;
  153. /*
  154. * Try 10 times, because there is a small race here.
  155. * that's ok, because it's still cheaper than a lock.
  156. *
  157. * Race: Since this section is not protected by lock, same bit
  158. * could be chosen by different process contexts running in
  159. * different processor. So instead of costly lock, we are going
  160. * with loop.
  161. */
  162. for (i = 0; i < 10; i++) {
  163. slot = find_next_zero_bit(port->allocated,
  164. num_command_slots, 1);
  165. if ((slot < num_command_slots) &&
  166. (!test_and_set_bit(slot, port->allocated)))
  167. return slot;
  168. }
  169. dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
  170. mtip_check_surprise_removal(port->dd->pdev);
  171. return -1;
  172. }
  173. /*
  174. * Release a command slot.
  175. *
  176. * @port Pointer to the port data structure.
  177. * @tag Tag of command to release
  178. *
  179. * return value
  180. * None
  181. */
  182. static inline void release_slot(struct mtip_port *port, int tag)
  183. {
  184. smp_mb__before_clear_bit();
  185. clear_bit(tag, port->allocated);
  186. smp_mb__after_clear_bit();
  187. }
  188. /*
  189. * IO completion function.
  190. *
  191. * This completion function is called by the driver ISR when a
  192. * command that was issued by the kernel completes. It first calls the
  193. * asynchronous completion function which normally calls back into the block
  194. * layer passing the asynchronous callback data, then unmaps the
  195. * scatter list associated with the completed command, and finally
  196. * clears the allocated bit associated with the completed command.
  197. *
  198. * @port Pointer to the port data structure.
  199. * @tag Tag of the command.
  200. * @data Pointer to driver_data.
  201. * @status Completion status.
  202. *
  203. * return value
  204. * None
  205. */
  206. static void mtip_async_complete(struct mtip_port *port,
  207. int tag,
  208. void *data,
  209. int status)
  210. {
  211. struct mtip_cmd *command;
  212. struct driver_data *dd = data;
  213. int cb_status = status ? -EIO : 0;
  214. if (unlikely(!dd) || unlikely(!port))
  215. return;
  216. command = &port->commands[tag];
  217. if (unlikely(status == PORT_IRQ_TF_ERR)) {
  218. dev_warn(&port->dd->pdev->dev,
  219. "Command tag %d failed due to TFE\n", tag);
  220. }
  221. /* Upper layer callback */
  222. if (likely(command->async_callback))
  223. command->async_callback(command->async_data, cb_status);
  224. command->async_callback = NULL;
  225. command->comp_func = NULL;
  226. /* Unmap the DMA scatter list entries */
  227. dma_unmap_sg(&dd->pdev->dev,
  228. command->sg,
  229. command->scatter_ents,
  230. command->direction);
  231. /* Clear the allocated and active bits for the command */
  232. atomic_set(&port->commands[tag].active, 0);
  233. release_slot(port, tag);
  234. up(&port->cmd_slot);
  235. }
  236. /*
  237. * This function is called for clean the pending command in the
  238. * command slot during the surprise removal of device and return
  239. * error to the upper layer.
  240. *
  241. * @dd Pointer to the DRIVER_DATA structure.
  242. *
  243. * return value
  244. * None
  245. */
  246. static void mtip_command_cleanup(struct driver_data *dd)
  247. {
  248. int tag = 0;
  249. struct mtip_cmd *cmd;
  250. struct mtip_port *port = dd->port;
  251. unsigned int num_cmd_slots = dd->slot_groups * 32;
  252. if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
  253. return;
  254. if (!port)
  255. return;
  256. cmd = &port->commands[MTIP_TAG_INTERNAL];
  257. if (atomic_read(&cmd->active))
  258. if (readl(port->cmd_issue[MTIP_TAG_INTERNAL]) &
  259. (1 << MTIP_TAG_INTERNAL))
  260. if (cmd->comp_func)
  261. cmd->comp_func(port, MTIP_TAG_INTERNAL,
  262. cmd->comp_data, -ENODEV);
  263. while (1) {
  264. tag = find_next_bit(port->allocated, num_cmd_slots, tag);
  265. if (tag >= num_cmd_slots)
  266. break;
  267. cmd = &port->commands[tag];
  268. if (atomic_read(&cmd->active))
  269. mtip_async_complete(port, tag, dd, -ENODEV);
  270. }
  271. set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
  272. }
  273. /*
  274. * Reset the HBA (without sleeping)
  275. *
  276. * @dd Pointer to the driver data structure.
  277. *
  278. * return value
  279. * 0 The reset was successful.
  280. * -1 The HBA Reset bit did not clear.
  281. */
  282. static int mtip_hba_reset(struct driver_data *dd)
  283. {
  284. unsigned long timeout;
  285. /* Set the reset bit */
  286. writel(HOST_RESET, dd->mmio + HOST_CTL);
  287. /* Flush */
  288. readl(dd->mmio + HOST_CTL);
  289. /* Spin for up to 2 seconds, waiting for reset acknowledgement */
  290. timeout = jiffies + msecs_to_jiffies(2000);
  291. do {
  292. mdelay(10);
  293. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
  294. return -1;
  295. } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
  296. && time_before(jiffies, timeout));
  297. if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
  298. return -1;
  299. return 0;
  300. }
  301. /*
  302. * Issue a command to the hardware.
  303. *
  304. * Set the appropriate bit in the s_active and Command Issue hardware
  305. * registers, causing hardware command processing to begin.
  306. *
  307. * @port Pointer to the port structure.
  308. * @tag The tag of the command to be issued.
  309. *
  310. * return value
  311. * None
  312. */
  313. static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
  314. {
  315. int group = tag >> 5;
  316. atomic_set(&port->commands[tag].active, 1);
  317. /* guard SACT and CI registers */
  318. spin_lock(&port->cmd_issue_lock[group]);
  319. writel((1 << MTIP_TAG_BIT(tag)),
  320. port->s_active[MTIP_TAG_INDEX(tag)]);
  321. writel((1 << MTIP_TAG_BIT(tag)),
  322. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  323. spin_unlock(&port->cmd_issue_lock[group]);
  324. /* Set the command's timeout value.*/
  325. port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
  326. MTIP_NCQ_COMMAND_TIMEOUT_MS);
  327. }
  328. /*
  329. * Enable/disable the reception of FIS
  330. *
  331. * @port Pointer to the port data structure
  332. * @enable 1 to enable, 0 to disable
  333. *
  334. * return value
  335. * Previous state: 1 enabled, 0 disabled
  336. */
  337. static int mtip_enable_fis(struct mtip_port *port, int enable)
  338. {
  339. u32 tmp;
  340. /* enable FIS reception */
  341. tmp = readl(port->mmio + PORT_CMD);
  342. if (enable)
  343. writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  344. else
  345. writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  346. /* Flush */
  347. readl(port->mmio + PORT_CMD);
  348. return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
  349. }
  350. /*
  351. * Enable/disable the DMA engine
  352. *
  353. * @port Pointer to the port data structure
  354. * @enable 1 to enable, 0 to disable
  355. *
  356. * return value
  357. * Previous state: 1 enabled, 0 disabled.
  358. */
  359. static int mtip_enable_engine(struct mtip_port *port, int enable)
  360. {
  361. u32 tmp;
  362. /* enable FIS reception */
  363. tmp = readl(port->mmio + PORT_CMD);
  364. if (enable)
  365. writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
  366. else
  367. writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
  368. readl(port->mmio + PORT_CMD);
  369. return (((tmp & PORT_CMD_START) == PORT_CMD_START));
  370. }
  371. /*
  372. * Enables the port DMA engine and FIS reception.
  373. *
  374. * return value
  375. * None
  376. */
  377. static inline void mtip_start_port(struct mtip_port *port)
  378. {
  379. /* Enable FIS reception */
  380. mtip_enable_fis(port, 1);
  381. /* Enable the DMA engine */
  382. mtip_enable_engine(port, 1);
  383. }
  384. /*
  385. * Deinitialize a port by disabling port interrupts, the DMA engine,
  386. * and FIS reception.
  387. *
  388. * @port Pointer to the port structure
  389. *
  390. * return value
  391. * None
  392. */
  393. static inline void mtip_deinit_port(struct mtip_port *port)
  394. {
  395. /* Disable interrupts on this port */
  396. writel(0, port->mmio + PORT_IRQ_MASK);
  397. /* Disable the DMA engine */
  398. mtip_enable_engine(port, 0);
  399. /* Disable FIS reception */
  400. mtip_enable_fis(port, 0);
  401. }
  402. /*
  403. * Initialize a port.
  404. *
  405. * This function deinitializes the port by calling mtip_deinit_port() and
  406. * then initializes it by setting the command header and RX FIS addresses,
  407. * clearing the SError register and any pending port interrupts before
  408. * re-enabling the default set of port interrupts.
  409. *
  410. * @port Pointer to the port structure.
  411. *
  412. * return value
  413. * None
  414. */
  415. static void mtip_init_port(struct mtip_port *port)
  416. {
  417. int i;
  418. mtip_deinit_port(port);
  419. /* Program the command list base and FIS base addresses */
  420. if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
  421. writel((port->command_list_dma >> 16) >> 16,
  422. port->mmio + PORT_LST_ADDR_HI);
  423. writel((port->rxfis_dma >> 16) >> 16,
  424. port->mmio + PORT_FIS_ADDR_HI);
  425. }
  426. writel(port->command_list_dma & 0xFFFFFFFF,
  427. port->mmio + PORT_LST_ADDR);
  428. writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
  429. /* Clear SError */
  430. writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
  431. /* reset the completed registers.*/
  432. for (i = 0; i < port->dd->slot_groups; i++)
  433. writel(0xFFFFFFFF, port->completed[i]);
  434. /* Clear any pending interrupts for this port */
  435. writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
  436. /* Clear any pending interrupts on the HBA. */
  437. writel(readl(port->dd->mmio + HOST_IRQ_STAT),
  438. port->dd->mmio + HOST_IRQ_STAT);
  439. /* Enable port interrupts */
  440. writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
  441. }
  442. /*
  443. * Restart a port
  444. *
  445. * @port Pointer to the port data structure.
  446. *
  447. * return value
  448. * None
  449. */
  450. static void mtip_restart_port(struct mtip_port *port)
  451. {
  452. unsigned long timeout;
  453. /* Disable the DMA engine */
  454. mtip_enable_engine(port, 0);
  455. /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
  456. timeout = jiffies + msecs_to_jiffies(500);
  457. while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
  458. && time_before(jiffies, timeout))
  459. ;
  460. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  461. return;
  462. /*
  463. * Chip quirk: escalate to hba reset if
  464. * PxCMD.CR not clear after 500 ms
  465. */
  466. if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
  467. dev_warn(&port->dd->pdev->dev,
  468. "PxCMD.CR not clear, escalating reset\n");
  469. if (mtip_hba_reset(port->dd))
  470. dev_err(&port->dd->pdev->dev,
  471. "HBA reset escalation failed.\n");
  472. /* 30 ms delay before com reset to quiesce chip */
  473. mdelay(30);
  474. }
  475. dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
  476. /* Set PxSCTL.DET */
  477. writel(readl(port->mmio + PORT_SCR_CTL) |
  478. 1, port->mmio + PORT_SCR_CTL);
  479. readl(port->mmio + PORT_SCR_CTL);
  480. /* Wait 1 ms to quiesce chip function */
  481. timeout = jiffies + msecs_to_jiffies(1);
  482. while (time_before(jiffies, timeout))
  483. ;
  484. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  485. return;
  486. /* Clear PxSCTL.DET */
  487. writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
  488. port->mmio + PORT_SCR_CTL);
  489. readl(port->mmio + PORT_SCR_CTL);
  490. /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
  491. timeout = jiffies + msecs_to_jiffies(500);
  492. while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  493. && time_before(jiffies, timeout))
  494. ;
  495. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  496. return;
  497. if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  498. dev_warn(&port->dd->pdev->dev,
  499. "COM reset failed\n");
  500. mtip_init_port(port);
  501. mtip_start_port(port);
  502. }
  503. static int mtip_device_reset(struct driver_data *dd)
  504. {
  505. int rv = 0;
  506. if (mtip_check_surprise_removal(dd->pdev))
  507. return 0;
  508. if (mtip_hba_reset(dd) < 0)
  509. rv = -EFAULT;
  510. mdelay(1);
  511. mtip_init_port(dd->port);
  512. mtip_start_port(dd->port);
  513. /* Enable interrupts on the HBA. */
  514. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  515. dd->mmio + HOST_CTL);
  516. return rv;
  517. }
  518. /*
  519. * Helper function for tag logging
  520. */
  521. static void print_tags(struct driver_data *dd,
  522. char *msg,
  523. unsigned long *tagbits,
  524. int cnt)
  525. {
  526. unsigned char tagmap[128];
  527. int group, tagmap_len = 0;
  528. memset(tagmap, 0, sizeof(tagmap));
  529. for (group = SLOTBITS_IN_LONGS; group > 0; group--)
  530. tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
  531. tagbits[group-1]);
  532. dev_warn(&dd->pdev->dev,
  533. "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
  534. }
  535. /*
  536. * Called periodically to see if any read/write commands are
  537. * taking too long to complete.
  538. *
  539. * @data Pointer to the PORT data structure.
  540. *
  541. * return value
  542. * None
  543. */
  544. static void mtip_timeout_function(unsigned long int data)
  545. {
  546. struct mtip_port *port = (struct mtip_port *) data;
  547. struct host_to_dev_fis *fis;
  548. struct mtip_cmd *command;
  549. int tag, cmdto_cnt = 0;
  550. unsigned int bit, group;
  551. unsigned int num_command_slots;
  552. unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
  553. if (unlikely(!port))
  554. return;
  555. if (unlikely(port->dd->sr))
  556. return;
  557. if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
  558. mod_timer(&port->cmd_timer,
  559. jiffies + msecs_to_jiffies(30000));
  560. return;
  561. }
  562. /* clear the tag accumulator */
  563. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  564. num_command_slots = port->dd->slot_groups * 32;
  565. for (tag = 0; tag < num_command_slots; tag++) {
  566. /*
  567. * Skip internal command slot as it has
  568. * its own timeout mechanism
  569. */
  570. if (tag == MTIP_TAG_INTERNAL)
  571. continue;
  572. if (atomic_read(&port->commands[tag].active) &&
  573. (time_after(jiffies, port->commands[tag].comp_time))) {
  574. group = tag >> 5;
  575. bit = tag & 0x1F;
  576. command = &port->commands[tag];
  577. fis = (struct host_to_dev_fis *) command->command;
  578. set_bit(tag, tagaccum);
  579. cmdto_cnt++;
  580. if (cmdto_cnt == 1)
  581. set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  582. /*
  583. * Clear the completed bit. This should prevent
  584. * any interrupt handlers from trying to retire
  585. * the command.
  586. */
  587. writel(1 << bit, port->completed[group]);
  588. /* Call the async completion callback. */
  589. if (likely(command->async_callback))
  590. command->async_callback(command->async_data,
  591. -EIO);
  592. command->async_callback = NULL;
  593. command->comp_func = NULL;
  594. /* Unmap the DMA scatter list entries */
  595. dma_unmap_sg(&port->dd->pdev->dev,
  596. command->sg,
  597. command->scatter_ents,
  598. command->direction);
  599. /*
  600. * Clear the allocated bit and active tag for the
  601. * command.
  602. */
  603. atomic_set(&port->commands[tag].active, 0);
  604. release_slot(port, tag);
  605. up(&port->cmd_slot);
  606. }
  607. }
  608. if (cmdto_cnt) {
  609. print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
  610. if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
  611. mtip_device_reset(port->dd);
  612. wake_up_interruptible(&port->svc_wait);
  613. }
  614. clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  615. }
  616. if (port->ic_pause_timer) {
  617. to = port->ic_pause_timer + msecs_to_jiffies(1000);
  618. if (time_after(jiffies, to)) {
  619. if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
  620. port->ic_pause_timer = 0;
  621. clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  622. clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  623. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  624. wake_up_interruptible(&port->svc_wait);
  625. }
  626. }
  627. }
  628. /* Restart the timer */
  629. mod_timer(&port->cmd_timer,
  630. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  631. }
  632. /*
  633. * Internal command completion callback function.
  634. *
  635. * This function is normally called by the driver ISR when an internal
  636. * command completed. This function signals the command completion by
  637. * calling complete().
  638. *
  639. * @port Pointer to the port data structure.
  640. * @tag Tag of the command that has completed.
  641. * @data Pointer to a completion structure.
  642. * @status Completion status.
  643. *
  644. * return value
  645. * None
  646. */
  647. static void mtip_completion(struct mtip_port *port,
  648. int tag,
  649. void *data,
  650. int status)
  651. {
  652. struct mtip_cmd *command = &port->commands[tag];
  653. struct completion *waiting = data;
  654. if (unlikely(status == PORT_IRQ_TF_ERR))
  655. dev_warn(&port->dd->pdev->dev,
  656. "Internal command %d completed with TFE\n", tag);
  657. command->async_callback = NULL;
  658. command->comp_func = NULL;
  659. complete(waiting);
  660. }
  661. static void mtip_null_completion(struct mtip_port *port,
  662. int tag,
  663. void *data,
  664. int status)
  665. {
  666. return;
  667. }
  668. static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
  669. dma_addr_t buffer_dma, unsigned int sectors);
  670. static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
  671. struct smart_attr *attrib);
  672. /*
  673. * Handle an error.
  674. *
  675. * @dd Pointer to the DRIVER_DATA structure.
  676. *
  677. * return value
  678. * None
  679. */
  680. static void mtip_handle_tfe(struct driver_data *dd)
  681. {
  682. int group, tag, bit, reissue, rv;
  683. struct mtip_port *port;
  684. struct mtip_cmd *cmd;
  685. u32 completed;
  686. struct host_to_dev_fis *fis;
  687. unsigned long tagaccum[SLOTBITS_IN_LONGS];
  688. unsigned int cmd_cnt = 0;
  689. unsigned char *buf;
  690. char *fail_reason = NULL;
  691. int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
  692. dev_warn(&dd->pdev->dev, "Taskfile error\n");
  693. port = dd->port;
  694. /* Stop the timer to prevent command timeouts. */
  695. del_timer(&port->cmd_timer);
  696. set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  697. if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
  698. test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
  699. cmd = &port->commands[MTIP_TAG_INTERNAL];
  700. dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
  701. atomic_inc(&cmd->active); /* active > 1 indicates error */
  702. if (cmd->comp_data && cmd->comp_func) {
  703. cmd->comp_func(port, MTIP_TAG_INTERNAL,
  704. cmd->comp_data, PORT_IRQ_TF_ERR);
  705. }
  706. goto handle_tfe_exit;
  707. }
  708. /* clear the tag accumulator */
  709. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  710. /* Loop through all the groups */
  711. for (group = 0; group < dd->slot_groups; group++) {
  712. completed = readl(port->completed[group]);
  713. /* clear completed status register in the hardware.*/
  714. writel(completed, port->completed[group]);
  715. /* Process successfully completed commands */
  716. for (bit = 0; bit < 32 && completed; bit++) {
  717. if (!(completed & (1<<bit)))
  718. continue;
  719. tag = (group << 5) + bit;
  720. /* Skip the internal command slot */
  721. if (tag == MTIP_TAG_INTERNAL)
  722. continue;
  723. cmd = &port->commands[tag];
  724. if (likely(cmd->comp_func)) {
  725. set_bit(tag, tagaccum);
  726. cmd_cnt++;
  727. atomic_set(&cmd->active, 0);
  728. cmd->comp_func(port,
  729. tag,
  730. cmd->comp_data,
  731. 0);
  732. } else {
  733. dev_err(&port->dd->pdev->dev,
  734. "Missing completion func for tag %d",
  735. tag);
  736. if (mtip_check_surprise_removal(dd->pdev)) {
  737. /* don't proceed further */
  738. return;
  739. }
  740. }
  741. }
  742. }
  743. print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
  744. /* Restart the port */
  745. mdelay(20);
  746. mtip_restart_port(port);
  747. /* Trying to determine the cause of the error */
  748. rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
  749. dd->port->log_buf,
  750. dd->port->log_buf_dma, 1);
  751. if (rv) {
  752. dev_warn(&dd->pdev->dev,
  753. "Error in READ LOG EXT (10h) command\n");
  754. /* non-critical error, don't fail the load */
  755. } else {
  756. buf = (unsigned char *)dd->port->log_buf;
  757. if (buf[259] & 0x1) {
  758. dev_info(&dd->pdev->dev,
  759. "Write protect bit is set.\n");
  760. set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
  761. fail_all_ncq_write = 1;
  762. fail_reason = "write protect";
  763. }
  764. if (buf[288] == 0xF7) {
  765. dev_info(&dd->pdev->dev,
  766. "Exceeded Tmax, drive in thermal shutdown.\n");
  767. set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
  768. fail_all_ncq_cmds = 1;
  769. fail_reason = "thermal shutdown";
  770. }
  771. if (buf[288] == 0xBF) {
  772. dev_info(&dd->pdev->dev,
  773. "Drive indicates rebuild has failed.\n");
  774. fail_all_ncq_cmds = 1;
  775. fail_reason = "rebuild failed";
  776. }
  777. }
  778. /* clear the tag accumulator */
  779. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  780. /* Loop through all the groups */
  781. for (group = 0; group < dd->slot_groups; group++) {
  782. for (bit = 0; bit < 32; bit++) {
  783. reissue = 1;
  784. tag = (group << 5) + bit;
  785. cmd = &port->commands[tag];
  786. /* If the active bit is set re-issue the command */
  787. if (atomic_read(&cmd->active) == 0)
  788. continue;
  789. fis = (struct host_to_dev_fis *)cmd->command;
  790. /* Should re-issue? */
  791. if (tag == MTIP_TAG_INTERNAL ||
  792. fis->command == ATA_CMD_SET_FEATURES)
  793. reissue = 0;
  794. else {
  795. if (fail_all_ncq_cmds ||
  796. (fail_all_ncq_write &&
  797. fis->command == ATA_CMD_FPDMA_WRITE)) {
  798. dev_warn(&dd->pdev->dev,
  799. " Fail: %s w/tag %d [%s].\n",
  800. fis->command == ATA_CMD_FPDMA_WRITE ?
  801. "write" : "read",
  802. tag,
  803. fail_reason != NULL ?
  804. fail_reason : "unknown");
  805. atomic_set(&cmd->active, 0);
  806. if (cmd->comp_func) {
  807. cmd->comp_func(port, tag,
  808. cmd->comp_data,
  809. -ENODATA);
  810. }
  811. continue;
  812. }
  813. }
  814. /*
  815. * First check if this command has
  816. * exceeded its retries.
  817. */
  818. if (reissue && (cmd->retries-- > 0)) {
  819. set_bit(tag, tagaccum);
  820. /* Re-issue the command. */
  821. mtip_issue_ncq_command(port, tag);
  822. continue;
  823. }
  824. /* Retire a command that will not be reissued */
  825. dev_warn(&port->dd->pdev->dev,
  826. "retiring tag %d\n", tag);
  827. atomic_set(&cmd->active, 0);
  828. if (cmd->comp_func)
  829. cmd->comp_func(
  830. port,
  831. tag,
  832. cmd->comp_data,
  833. PORT_IRQ_TF_ERR);
  834. else
  835. dev_warn(&port->dd->pdev->dev,
  836. "Bad completion for tag %d\n",
  837. tag);
  838. }
  839. }
  840. print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
  841. handle_tfe_exit:
  842. /* clear eh_active */
  843. clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  844. wake_up_interruptible(&port->svc_wait);
  845. mod_timer(&port->cmd_timer,
  846. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  847. }
  848. /*
  849. * Handle a set device bits interrupt
  850. */
  851. static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
  852. u32 completed)
  853. {
  854. struct driver_data *dd = port->dd;
  855. int tag, bit;
  856. struct mtip_cmd *command;
  857. if (!completed) {
  858. WARN_ON_ONCE(!completed);
  859. return;
  860. }
  861. /* clear completed status register in the hardware.*/
  862. writel(completed, port->completed[group]);
  863. /* Process completed commands. */
  864. for (bit = 0; (bit < 32) && completed; bit++) {
  865. if (completed & 0x01) {
  866. tag = (group << 5) | bit;
  867. /* skip internal command slot. */
  868. if (unlikely(tag == MTIP_TAG_INTERNAL))
  869. continue;
  870. command = &port->commands[tag];
  871. /* make internal callback */
  872. if (likely(command->comp_func)) {
  873. command->comp_func(
  874. port,
  875. tag,
  876. command->comp_data,
  877. 0);
  878. } else {
  879. dev_dbg(&dd->pdev->dev,
  880. "Null completion for tag %d",
  881. tag);
  882. if (mtip_check_surprise_removal(
  883. dd->pdev)) {
  884. return;
  885. }
  886. }
  887. }
  888. completed >>= 1;
  889. }
  890. /* If last, re-enable interrupts */
  891. if (atomic_dec_return(&dd->irq_workers_active) == 0)
  892. writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
  893. }
  894. /*
  895. * Process legacy pio and d2h interrupts
  896. */
  897. static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
  898. {
  899. struct mtip_port *port = dd->port;
  900. struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
  901. if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
  902. (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  903. & (1 << MTIP_TAG_INTERNAL))) {
  904. if (cmd->comp_func) {
  905. cmd->comp_func(port,
  906. MTIP_TAG_INTERNAL,
  907. cmd->comp_data,
  908. 0);
  909. return;
  910. }
  911. }
  912. return;
  913. }
  914. /*
  915. * Demux and handle errors
  916. */
  917. static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
  918. {
  919. if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
  920. mtip_handle_tfe(dd);
  921. if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
  922. dev_warn(&dd->pdev->dev,
  923. "Clearing PxSERR.DIAG.x\n");
  924. writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
  925. }
  926. if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
  927. dev_warn(&dd->pdev->dev,
  928. "Clearing PxSERR.DIAG.n\n");
  929. writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
  930. }
  931. if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
  932. dev_warn(&dd->pdev->dev,
  933. "Port stat errors %x unhandled\n",
  934. (port_stat & ~PORT_IRQ_HANDLED));
  935. }
  936. }
  937. static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
  938. {
  939. struct driver_data *dd = (struct driver_data *) data;
  940. struct mtip_port *port = dd->port;
  941. u32 hba_stat, port_stat;
  942. int rv = IRQ_NONE;
  943. int do_irq_enable = 1, i, workers;
  944. struct mtip_work *twork;
  945. hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
  946. if (hba_stat) {
  947. rv = IRQ_HANDLED;
  948. /* Acknowledge the interrupt status on the port.*/
  949. port_stat = readl(port->mmio + PORT_IRQ_STAT);
  950. writel(port_stat, port->mmio + PORT_IRQ_STAT);
  951. /* Demux port status */
  952. if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
  953. do_irq_enable = 0;
  954. WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
  955. /* Start at 1: group zero is always local? */
  956. for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
  957. i++) {
  958. twork = &dd->work[i];
  959. twork->completed = readl(port->completed[i]);
  960. if (twork->completed)
  961. workers++;
  962. }
  963. atomic_set(&dd->irq_workers_active, workers);
  964. if (workers) {
  965. for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
  966. twork = &dd->work[i];
  967. if (twork->completed)
  968. queue_work_on(
  969. twork->cpu_binding,
  970. dd->isr_workq,
  971. &twork->work);
  972. }
  973. if (likely(dd->work[0].completed))
  974. mtip_workq_sdbfx(port, 0,
  975. dd->work[0].completed);
  976. } else {
  977. /*
  978. * Chip quirk: SDB interrupt but nothing
  979. * to complete
  980. */
  981. do_irq_enable = 1;
  982. }
  983. }
  984. if (unlikely(port_stat & PORT_IRQ_ERR)) {
  985. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  986. /* don't proceed further */
  987. return IRQ_HANDLED;
  988. }
  989. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  990. &dd->dd_flag))
  991. return rv;
  992. mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
  993. }
  994. if (unlikely(port_stat & PORT_IRQ_LEGACY))
  995. mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
  996. }
  997. /* acknowledge interrupt */
  998. if (unlikely(do_irq_enable))
  999. writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
  1000. return rv;
  1001. }
  1002. /*
  1003. * HBA interrupt subroutine.
  1004. *
  1005. * @irq IRQ number.
  1006. * @instance Pointer to the driver data structure.
  1007. *
  1008. * return value
  1009. * IRQ_HANDLED A HBA interrupt was pending and handled.
  1010. * IRQ_NONE This interrupt was not for the HBA.
  1011. */
  1012. static irqreturn_t mtip_irq_handler(int irq, void *instance)
  1013. {
  1014. struct driver_data *dd = instance;
  1015. return mtip_handle_irq(dd);
  1016. }
  1017. static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
  1018. {
  1019. atomic_set(&port->commands[tag].active, 1);
  1020. writel(1 << MTIP_TAG_BIT(tag),
  1021. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  1022. }
  1023. static bool mtip_pause_ncq(struct mtip_port *port,
  1024. struct host_to_dev_fis *fis)
  1025. {
  1026. struct host_to_dev_fis *reply;
  1027. unsigned long task_file_data;
  1028. reply = port->rxfis + RX_FIS_D2H_REG;
  1029. task_file_data = readl(port->mmio+PORT_TFDATA);
  1030. if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
  1031. clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
  1032. if ((task_file_data & 1))
  1033. return false;
  1034. if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
  1035. set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  1036. set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
  1037. port->ic_pause_timer = jiffies;
  1038. return true;
  1039. } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
  1040. (fis->features == 0x03)) {
  1041. set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  1042. port->ic_pause_timer = jiffies;
  1043. return true;
  1044. } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
  1045. ((fis->command == 0xFC) &&
  1046. (fis->features == 0x27 || fis->features == 0x72 ||
  1047. fis->features == 0x62 || fis->features == 0x26))) {
  1048. /* Com reset after secure erase or lowlevel format */
  1049. mtip_restart_port(port);
  1050. return false;
  1051. }
  1052. return false;
  1053. }
  1054. /*
  1055. * Wait for port to quiesce
  1056. *
  1057. * @port Pointer to port data structure
  1058. * @timeout Max duration to wait (ms)
  1059. *
  1060. * return value
  1061. * 0 Success
  1062. * -EBUSY Commands still active
  1063. */
  1064. static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
  1065. {
  1066. unsigned long to;
  1067. unsigned int n;
  1068. unsigned int active = 1;
  1069. to = jiffies + msecs_to_jiffies(timeout);
  1070. do {
  1071. if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
  1072. test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
  1073. msleep(20);
  1074. continue; /* svc thd is actively issuing commands */
  1075. }
  1076. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  1077. return -EFAULT;
  1078. /*
  1079. * Ignore s_active bit 0 of array element 0.
  1080. * This bit will always be set
  1081. */
  1082. active = readl(port->s_active[0]) & 0xFFFFFFFE;
  1083. for (n = 1; n < port->dd->slot_groups; n++)
  1084. active |= readl(port->s_active[n]);
  1085. if (!active)
  1086. break;
  1087. msleep(20);
  1088. } while (time_before(jiffies, to));
  1089. return active ? -EBUSY : 0;
  1090. }
  1091. /*
  1092. * Execute an internal command and wait for the completion.
  1093. *
  1094. * @port Pointer to the port data structure.
  1095. * @fis Pointer to the FIS that describes the command.
  1096. * @fis_len Length in WORDS of the FIS.
  1097. * @buffer DMA accessible for command data.
  1098. * @buf_len Length, in bytes, of the data buffer.
  1099. * @opts Command header options, excluding the FIS length
  1100. * and the number of PRD entries.
  1101. * @timeout Time in ms to wait for the command to complete.
  1102. *
  1103. * return value
  1104. * 0 Command completed successfully.
  1105. * -EFAULT The buffer address is not correctly aligned.
  1106. * -EBUSY Internal command or other IO in progress.
  1107. * -EAGAIN Time out waiting for command to complete.
  1108. */
  1109. static int mtip_exec_internal_command(struct mtip_port *port,
  1110. struct host_to_dev_fis *fis,
  1111. int fis_len,
  1112. dma_addr_t buffer,
  1113. int buf_len,
  1114. u32 opts,
  1115. gfp_t atomic,
  1116. unsigned long timeout)
  1117. {
  1118. struct mtip_cmd_sg *command_sg;
  1119. DECLARE_COMPLETION_ONSTACK(wait);
  1120. int rv = 0, ready2go = 1;
  1121. struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
  1122. unsigned long to;
  1123. struct driver_data *dd = port->dd;
  1124. /* Make sure the buffer is 8 byte aligned. This is asic specific. */
  1125. if (buffer & 0x00000007) {
  1126. dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
  1127. return -EFAULT;
  1128. }
  1129. to = jiffies + msecs_to_jiffies(timeout);
  1130. do {
  1131. ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
  1132. port->allocated);
  1133. if (ready2go)
  1134. break;
  1135. mdelay(100);
  1136. } while (time_before(jiffies, to));
  1137. if (!ready2go) {
  1138. dev_warn(&dd->pdev->dev,
  1139. "Internal cmd active. new cmd [%02X]\n", fis->command);
  1140. return -EBUSY;
  1141. }
  1142. set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1143. port->ic_pause_timer = 0;
  1144. clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  1145. clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  1146. if (atomic == GFP_KERNEL) {
  1147. if (fis->command != ATA_CMD_STANDBYNOW1) {
  1148. /* wait for io to complete if non atomic */
  1149. if (mtip_quiesce_io(port, 5000) < 0) {
  1150. dev_warn(&dd->pdev->dev,
  1151. "Failed to quiesce IO\n");
  1152. release_slot(port, MTIP_TAG_INTERNAL);
  1153. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1154. wake_up_interruptible(&port->svc_wait);
  1155. return -EBUSY;
  1156. }
  1157. }
  1158. /* Set the completion function and data for the command. */
  1159. int_cmd->comp_data = &wait;
  1160. int_cmd->comp_func = mtip_completion;
  1161. } else {
  1162. /* Clear completion - we're going to poll */
  1163. int_cmd->comp_data = NULL;
  1164. int_cmd->comp_func = mtip_null_completion;
  1165. }
  1166. /* Copy the command to the command table */
  1167. memcpy(int_cmd->command, fis, fis_len*4);
  1168. /* Populate the SG list */
  1169. int_cmd->command_header->opts =
  1170. __force_bit2int cpu_to_le32(opts | fis_len);
  1171. if (buf_len) {
  1172. command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
  1173. command_sg->info =
  1174. __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
  1175. command_sg->dba =
  1176. __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
  1177. command_sg->dba_upper =
  1178. __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
  1179. int_cmd->command_header->opts |=
  1180. __force_bit2int cpu_to_le32((1 << 16));
  1181. }
  1182. /* Populate the command header */
  1183. int_cmd->command_header->byte_count = 0;
  1184. /* Issue the command to the hardware */
  1185. mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
  1186. if (atomic == GFP_KERNEL) {
  1187. /* Wait for the command to complete or timeout. */
  1188. if (wait_for_completion_interruptible_timeout(
  1189. &wait,
  1190. msecs_to_jiffies(timeout)) <= 0) {
  1191. if (rv == -ERESTARTSYS) { /* interrupted */
  1192. dev_err(&dd->pdev->dev,
  1193. "Internal command [%02X] was interrupted after %lu ms\n",
  1194. fis->command, timeout);
  1195. rv = -EINTR;
  1196. goto exec_ic_exit;
  1197. } else if (rv == 0) /* timeout */
  1198. dev_err(&dd->pdev->dev,
  1199. "Internal command did not complete [%02X] within timeout of %lu ms\n",
  1200. fis->command, timeout);
  1201. else
  1202. dev_err(&dd->pdev->dev,
  1203. "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
  1204. fis->command, rv, timeout);
  1205. if (mtip_check_surprise_removal(dd->pdev) ||
  1206. test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1207. &dd->dd_flag)) {
  1208. dev_err(&dd->pdev->dev,
  1209. "Internal command [%02X] wait returned due to SR\n",
  1210. fis->command);
  1211. rv = -ENXIO;
  1212. goto exec_ic_exit;
  1213. }
  1214. mtip_device_reset(dd); /* recover from timeout issue */
  1215. rv = -EAGAIN;
  1216. goto exec_ic_exit;
  1217. }
  1218. } else {
  1219. u32 hba_stat, port_stat;
  1220. /* Spin for <timeout> checking if command still outstanding */
  1221. timeout = jiffies + msecs_to_jiffies(timeout);
  1222. while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1223. & (1 << MTIP_TAG_INTERNAL))
  1224. && time_before(jiffies, timeout)) {
  1225. if (mtip_check_surprise_removal(dd->pdev)) {
  1226. rv = -ENXIO;
  1227. goto exec_ic_exit;
  1228. }
  1229. if ((fis->command != ATA_CMD_STANDBYNOW1) &&
  1230. test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1231. &dd->dd_flag)) {
  1232. rv = -ENXIO;
  1233. goto exec_ic_exit;
  1234. }
  1235. port_stat = readl(port->mmio + PORT_IRQ_STAT);
  1236. if (!port_stat)
  1237. continue;
  1238. if (port_stat & PORT_IRQ_ERR) {
  1239. dev_err(&dd->pdev->dev,
  1240. "Internal command [%02X] failed\n",
  1241. fis->command);
  1242. mtip_device_reset(dd);
  1243. rv = -EIO;
  1244. goto exec_ic_exit;
  1245. } else {
  1246. writel(port_stat, port->mmio + PORT_IRQ_STAT);
  1247. hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
  1248. if (hba_stat)
  1249. writel(hba_stat,
  1250. dd->mmio + HOST_IRQ_STAT);
  1251. }
  1252. break;
  1253. }
  1254. }
  1255. if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1256. & (1 << MTIP_TAG_INTERNAL)) {
  1257. rv = -ENXIO;
  1258. if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
  1259. mtip_device_reset(dd);
  1260. rv = -EAGAIN;
  1261. }
  1262. }
  1263. exec_ic_exit:
  1264. /* Clear the allocated and active bits for the internal command. */
  1265. atomic_set(&int_cmd->active, 0);
  1266. release_slot(port, MTIP_TAG_INTERNAL);
  1267. if (rv >= 0 && mtip_pause_ncq(port, fis)) {
  1268. /* NCQ paused */
  1269. return rv;
  1270. }
  1271. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1272. wake_up_interruptible(&port->svc_wait);
  1273. return rv;
  1274. }
  1275. /*
  1276. * Byte-swap ATA ID strings.
  1277. *
  1278. * ATA identify data contains strings in byte-swapped 16-bit words.
  1279. * They must be swapped (on all architectures) to be usable as C strings.
  1280. * This function swaps bytes in-place.
  1281. *
  1282. * @buf The buffer location of the string
  1283. * @len The number of bytes to swap
  1284. *
  1285. * return value
  1286. * None
  1287. */
  1288. static inline void ata_swap_string(u16 *buf, unsigned int len)
  1289. {
  1290. int i;
  1291. for (i = 0; i < (len/2); i++)
  1292. be16_to_cpus(&buf[i]);
  1293. }
  1294. /*
  1295. * Request the device identity information.
  1296. *
  1297. * If a user space buffer is not specified, i.e. is NULL, the
  1298. * identify information is still read from the drive and placed
  1299. * into the identify data buffer (@e port->identify) in the
  1300. * port data structure.
  1301. * When the identify buffer contains valid identify information @e
  1302. * port->identify_valid is non-zero.
  1303. *
  1304. * @port Pointer to the port structure.
  1305. * @user_buffer A user space buffer where the identify data should be
  1306. * copied.
  1307. *
  1308. * return value
  1309. * 0 Command completed successfully.
  1310. * -EFAULT An error occurred while coping data to the user buffer.
  1311. * -1 Command failed.
  1312. */
  1313. static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
  1314. {
  1315. int rv = 0;
  1316. struct host_to_dev_fis fis;
  1317. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  1318. return -EFAULT;
  1319. /* Build the FIS. */
  1320. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1321. fis.type = 0x27;
  1322. fis.opts = 1 << 7;
  1323. fis.command = ATA_CMD_ID_ATA;
  1324. /* Set the identify information as invalid. */
  1325. port->identify_valid = 0;
  1326. /* Clear the identify information. */
  1327. memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
  1328. /* Execute the command. */
  1329. if (mtip_exec_internal_command(port,
  1330. &fis,
  1331. 5,
  1332. port->identify_dma,
  1333. sizeof(u16) * ATA_ID_WORDS,
  1334. 0,
  1335. GFP_KERNEL,
  1336. MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
  1337. < 0) {
  1338. rv = -1;
  1339. goto out;
  1340. }
  1341. /*
  1342. * Perform any necessary byte-swapping. Yes, the kernel does in fact
  1343. * perform field-sensitive swapping on the string fields.
  1344. * See the kernel use of ata_id_string() for proof of this.
  1345. */
  1346. #ifdef __LITTLE_ENDIAN
  1347. ata_swap_string(port->identify + 27, 40); /* model string*/
  1348. ata_swap_string(port->identify + 23, 8); /* firmware string*/
  1349. ata_swap_string(port->identify + 10, 20); /* serial# string*/
  1350. #else
  1351. {
  1352. int i;
  1353. for (i = 0; i < ATA_ID_WORDS; i++)
  1354. port->identify[i] = le16_to_cpu(port->identify[i]);
  1355. }
  1356. #endif
  1357. #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
  1358. /* Demux ID.DRAT & ID.RZAT to determine trim support */
  1359. if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
  1360. port->dd->trim_supp = true;
  1361. else
  1362. #endif
  1363. port->dd->trim_supp = false;
  1364. /* Set the identify buffer as valid. */
  1365. port->identify_valid = 1;
  1366. if (user_buffer) {
  1367. if (copy_to_user(
  1368. user_buffer,
  1369. port->identify,
  1370. ATA_ID_WORDS * sizeof(u16))) {
  1371. rv = -EFAULT;
  1372. goto out;
  1373. }
  1374. }
  1375. out:
  1376. return rv;
  1377. }
  1378. /*
  1379. * Issue a standby immediate command to the device.
  1380. *
  1381. * @port Pointer to the port structure.
  1382. *
  1383. * return value
  1384. * 0 Command was executed successfully.
  1385. * -1 An error occurred while executing the command.
  1386. */
  1387. static int mtip_standby_immediate(struct mtip_port *port)
  1388. {
  1389. int rv;
  1390. struct host_to_dev_fis fis;
  1391. unsigned long start;
  1392. /* Build the FIS. */
  1393. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1394. fis.type = 0x27;
  1395. fis.opts = 1 << 7;
  1396. fis.command = ATA_CMD_STANDBYNOW1;
  1397. start = jiffies;
  1398. rv = mtip_exec_internal_command(port,
  1399. &fis,
  1400. 5,
  1401. 0,
  1402. 0,
  1403. 0,
  1404. GFP_ATOMIC,
  1405. 15000);
  1406. dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
  1407. jiffies_to_msecs(jiffies - start));
  1408. if (rv)
  1409. dev_warn(&port->dd->pdev->dev,
  1410. "STANDBY IMMEDIATE command failed.\n");
  1411. return rv;
  1412. }
  1413. /*
  1414. * Issue a READ LOG EXT command to the device.
  1415. *
  1416. * @port pointer to the port structure.
  1417. * @page page number to fetch
  1418. * @buffer pointer to buffer
  1419. * @buffer_dma dma address corresponding to @buffer
  1420. * @sectors page length to fetch, in sectors
  1421. *
  1422. * return value
  1423. * @rv return value from mtip_exec_internal_command()
  1424. */
  1425. static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
  1426. dma_addr_t buffer_dma, unsigned int sectors)
  1427. {
  1428. struct host_to_dev_fis fis;
  1429. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1430. fis.type = 0x27;
  1431. fis.opts = 1 << 7;
  1432. fis.command = ATA_CMD_READ_LOG_EXT;
  1433. fis.sect_count = sectors & 0xFF;
  1434. fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
  1435. fis.lba_low = page;
  1436. fis.lba_mid = 0;
  1437. fis.device = ATA_DEVICE_OBS;
  1438. memset(buffer, 0, sectors * ATA_SECT_SIZE);
  1439. return mtip_exec_internal_command(port,
  1440. &fis,
  1441. 5,
  1442. buffer_dma,
  1443. sectors * ATA_SECT_SIZE,
  1444. 0,
  1445. GFP_ATOMIC,
  1446. MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
  1447. }
  1448. /*
  1449. * Issue a SMART READ DATA command to the device.
  1450. *
  1451. * @port pointer to the port structure.
  1452. * @buffer pointer to buffer
  1453. * @buffer_dma dma address corresponding to @buffer
  1454. *
  1455. * return value
  1456. * @rv return value from mtip_exec_internal_command()
  1457. */
  1458. static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
  1459. dma_addr_t buffer_dma)
  1460. {
  1461. struct host_to_dev_fis fis;
  1462. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1463. fis.type = 0x27;
  1464. fis.opts = 1 << 7;
  1465. fis.command = ATA_CMD_SMART;
  1466. fis.features = 0xD0;
  1467. fis.sect_count = 1;
  1468. fis.lba_mid = 0x4F;
  1469. fis.lba_hi = 0xC2;
  1470. fis.device = ATA_DEVICE_OBS;
  1471. return mtip_exec_internal_command(port,
  1472. &fis,
  1473. 5,
  1474. buffer_dma,
  1475. ATA_SECT_SIZE,
  1476. 0,
  1477. GFP_ATOMIC,
  1478. 15000);
  1479. }
  1480. /*
  1481. * Get the value of a smart attribute
  1482. *
  1483. * @port pointer to the port structure
  1484. * @id attribute number
  1485. * @attrib pointer to return attrib information corresponding to @id
  1486. *
  1487. * return value
  1488. * -EINVAL NULL buffer passed or unsupported attribute @id.
  1489. * -EPERM Identify data not valid, SMART not supported or not enabled
  1490. */
  1491. static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
  1492. struct smart_attr *attrib)
  1493. {
  1494. int rv, i;
  1495. struct smart_attr *pattr;
  1496. if (!attrib)
  1497. return -EINVAL;
  1498. if (!port->identify_valid) {
  1499. dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
  1500. return -EPERM;
  1501. }
  1502. if (!(port->identify[82] & 0x1)) {
  1503. dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
  1504. return -EPERM;
  1505. }
  1506. if (!(port->identify[85] & 0x1)) {
  1507. dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
  1508. return -EPERM;
  1509. }
  1510. memset(port->smart_buf, 0, ATA_SECT_SIZE);
  1511. rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
  1512. if (rv) {
  1513. dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
  1514. return rv;
  1515. }
  1516. pattr = (struct smart_attr *)(port->smart_buf + 2);
  1517. for (i = 0; i < 29; i++, pattr++)
  1518. if (pattr->attr_id == id) {
  1519. memcpy(attrib, pattr, sizeof(struct smart_attr));
  1520. break;
  1521. }
  1522. if (i == 29) {
  1523. dev_warn(&port->dd->pdev->dev,
  1524. "Query for invalid SMART attribute ID\n");
  1525. rv = -EINVAL;
  1526. }
  1527. return rv;
  1528. }
  1529. /*
  1530. * Trim unused sectors
  1531. *
  1532. * @dd pointer to driver_data structure
  1533. * @lba starting lba
  1534. * @len # of 512b sectors to trim
  1535. *
  1536. * return value
  1537. * -ENOMEM Out of dma memory
  1538. * -EINVAL Invalid parameters passed in, trim not supported
  1539. * -EIO Error submitting trim request to hw
  1540. */
  1541. static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
  1542. unsigned int len)
  1543. {
  1544. int i, rv = 0;
  1545. u64 tlba, tlen, sect_left;
  1546. struct mtip_trim_entry *buf;
  1547. dma_addr_t dma_addr;
  1548. struct host_to_dev_fis fis;
  1549. if (!len || dd->trim_supp == false)
  1550. return -EINVAL;
  1551. /* Trim request too big */
  1552. WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
  1553. /* Trim request not aligned on 4k boundary */
  1554. WARN_ON(len % 8 != 0);
  1555. /* Warn if vu_trim structure is too big */
  1556. WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
  1557. /* Allocate a DMA buffer for the trim structure */
  1558. buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
  1559. GFP_KERNEL);
  1560. if (!buf)
  1561. return -ENOMEM;
  1562. memset(buf, 0, ATA_SECT_SIZE);
  1563. for (i = 0, sect_left = len, tlba = lba;
  1564. i < MTIP_MAX_TRIM_ENTRIES && sect_left;
  1565. i++) {
  1566. tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
  1567. MTIP_MAX_TRIM_ENTRY_LEN :
  1568. sect_left);
  1569. buf[i].lba = __force_bit2int cpu_to_le32(tlba);
  1570. buf[i].range = __force_bit2int cpu_to_le16(tlen);
  1571. tlba += tlen;
  1572. sect_left -= tlen;
  1573. }
  1574. WARN_ON(sect_left != 0);
  1575. /* Build the fis */
  1576. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1577. fis.type = 0x27;
  1578. fis.opts = 1 << 7;
  1579. fis.command = 0xfb;
  1580. fis.features = 0x60;
  1581. fis.sect_count = 1;
  1582. fis.device = ATA_DEVICE_OBS;
  1583. if (mtip_exec_internal_command(dd->port,
  1584. &fis,
  1585. 5,
  1586. dma_addr,
  1587. ATA_SECT_SIZE,
  1588. 0,
  1589. GFP_KERNEL,
  1590. MTIP_TRIM_TIMEOUT_MS) < 0)
  1591. rv = -EIO;
  1592. dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
  1593. return rv;
  1594. }
  1595. /*
  1596. * Get the drive capacity.
  1597. *
  1598. * @dd Pointer to the device data structure.
  1599. * @sectors Pointer to the variable that will receive the sector count.
  1600. *
  1601. * return value
  1602. * 1 Capacity was returned successfully.
  1603. * 0 The identify information is invalid.
  1604. */
  1605. static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
  1606. {
  1607. struct mtip_port *port = dd->port;
  1608. u64 total, raw0, raw1, raw2, raw3;
  1609. raw0 = port->identify[100];
  1610. raw1 = port->identify[101];
  1611. raw2 = port->identify[102];
  1612. raw3 = port->identify[103];
  1613. total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
  1614. *sectors = total;
  1615. return (bool) !!port->identify_valid;
  1616. }
  1617. /*
  1618. * Display the identify command data.
  1619. *
  1620. * @port Pointer to the port data structure.
  1621. *
  1622. * return value
  1623. * None
  1624. */
  1625. static void mtip_dump_identify(struct mtip_port *port)
  1626. {
  1627. sector_t sectors;
  1628. unsigned short revid;
  1629. char cbuf[42];
  1630. if (!port->identify_valid)
  1631. return;
  1632. strlcpy(cbuf, (char *)(port->identify+10), 21);
  1633. dev_info(&port->dd->pdev->dev,
  1634. "Serial No.: %s\n", cbuf);
  1635. strlcpy(cbuf, (char *)(port->identify+23), 9);
  1636. dev_info(&port->dd->pdev->dev,
  1637. "Firmware Ver.: %s\n", cbuf);
  1638. strlcpy(cbuf, (char *)(port->identify+27), 41);
  1639. dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
  1640. if (mtip_hw_get_capacity(port->dd, &sectors))
  1641. dev_info(&port->dd->pdev->dev,
  1642. "Capacity: %llu sectors (%llu MB)\n",
  1643. (u64)sectors,
  1644. ((u64)sectors) * ATA_SECT_SIZE >> 20);
  1645. pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
  1646. switch (revid & 0xFF) {
  1647. case 0x1:
  1648. strlcpy(cbuf, "A0", 3);
  1649. break;
  1650. case 0x3:
  1651. strlcpy(cbuf, "A2", 3);
  1652. break;
  1653. default:
  1654. strlcpy(cbuf, "?", 2);
  1655. break;
  1656. }
  1657. dev_info(&port->dd->pdev->dev,
  1658. "Card Type: %s\n", cbuf);
  1659. }
  1660. /*
  1661. * Map the commands scatter list into the command table.
  1662. *
  1663. * @command Pointer to the command.
  1664. * @nents Number of scatter list entries.
  1665. *
  1666. * return value
  1667. * None
  1668. */
  1669. static inline void fill_command_sg(struct driver_data *dd,
  1670. struct mtip_cmd *command,
  1671. int nents)
  1672. {
  1673. int n;
  1674. unsigned int dma_len;
  1675. struct mtip_cmd_sg *command_sg;
  1676. struct scatterlist *sg = command->sg;
  1677. command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
  1678. for (n = 0; n < nents; n++) {
  1679. dma_len = sg_dma_len(sg);
  1680. if (dma_len > 0x400000)
  1681. dev_err(&dd->pdev->dev,
  1682. "DMA segment length truncated\n");
  1683. command_sg->info = __force_bit2int
  1684. cpu_to_le32((dma_len-1) & 0x3FFFFF);
  1685. command_sg->dba = __force_bit2int
  1686. cpu_to_le32(sg_dma_address(sg));
  1687. command_sg->dba_upper = __force_bit2int
  1688. cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
  1689. command_sg++;
  1690. sg++;
  1691. }
  1692. }
  1693. /*
  1694. * @brief Execute a drive command.
  1695. *
  1696. * return value 0 The command completed successfully.
  1697. * return value -1 An error occurred while executing the command.
  1698. */
  1699. static int exec_drive_task(struct mtip_port *port, u8 *command)
  1700. {
  1701. struct host_to_dev_fis fis;
  1702. struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
  1703. /* Build the FIS. */
  1704. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1705. fis.type = 0x27;
  1706. fis.opts = 1 << 7;
  1707. fis.command = command[0];
  1708. fis.features = command[1];
  1709. fis.sect_count = command[2];
  1710. fis.sector = command[3];
  1711. fis.cyl_low = command[4];
  1712. fis.cyl_hi = command[5];
  1713. fis.device = command[6] & ~0x10; /* Clear the dev bit*/
  1714. dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
  1715. __func__,
  1716. command[0],
  1717. command[1],
  1718. command[2],
  1719. command[3],
  1720. command[4],
  1721. command[5],
  1722. command[6]);
  1723. /* Execute the command. */
  1724. if (mtip_exec_internal_command(port,
  1725. &fis,
  1726. 5,
  1727. 0,
  1728. 0,
  1729. 0,
  1730. GFP_KERNEL,
  1731. MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
  1732. return -1;
  1733. }
  1734. command[0] = reply->command; /* Status*/
  1735. command[1] = reply->features; /* Error*/
  1736. command[4] = reply->cyl_low;
  1737. command[5] = reply->cyl_hi;
  1738. dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
  1739. __func__,
  1740. command[0],
  1741. command[1],
  1742. command[4],
  1743. command[5]);
  1744. return 0;
  1745. }
  1746. /*
  1747. * @brief Execute a drive command.
  1748. *
  1749. * @param port Pointer to the port data structure.
  1750. * @param command Pointer to the user specified command parameters.
  1751. * @param user_buffer Pointer to the user space buffer where read sector
  1752. * data should be copied.
  1753. *
  1754. * return value 0 The command completed successfully.
  1755. * return value -EFAULT An error occurred while copying the completion
  1756. * data to the user space buffer.
  1757. * return value -1 An error occurred while executing the command.
  1758. */
  1759. static int exec_drive_command(struct mtip_port *port, u8 *command,
  1760. void __user *user_buffer)
  1761. {
  1762. struct host_to_dev_fis fis;
  1763. struct host_to_dev_fis *reply;
  1764. u8 *buf = NULL;
  1765. dma_addr_t dma_addr = 0;
  1766. int rv = 0, xfer_sz = command[3];
  1767. if (xfer_sz) {
  1768. if (!user_buffer)
  1769. return -EFAULT;
  1770. buf = dmam_alloc_coherent(&port->dd->pdev->dev,
  1771. ATA_SECT_SIZE * xfer_sz,
  1772. &dma_addr,
  1773. GFP_KERNEL);
  1774. if (!buf) {
  1775. dev_err(&port->dd->pdev->dev,
  1776. "Memory allocation failed (%d bytes)\n",
  1777. ATA_SECT_SIZE * xfer_sz);
  1778. return -ENOMEM;
  1779. }
  1780. memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
  1781. }
  1782. /* Build the FIS. */
  1783. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1784. fis.type = 0x27;
  1785. fis.opts = 1 << 7;
  1786. fis.command = command[0];
  1787. fis.features = command[2];
  1788. fis.sect_count = command[3];
  1789. if (fis.command == ATA_CMD_SMART) {
  1790. fis.sector = command[1];
  1791. fis.cyl_low = 0x4F;
  1792. fis.cyl_hi = 0xC2;
  1793. }
  1794. if (xfer_sz)
  1795. reply = (port->rxfis + RX_FIS_PIO_SETUP);
  1796. else
  1797. reply = (port->rxfis + RX_FIS_D2H_REG);
  1798. dbg_printk(MTIP_DRV_NAME
  1799. " %s: User Command: cmd %x, sect %x, "
  1800. "feat %x, sectcnt %x\n",
  1801. __func__,
  1802. command[0],
  1803. command[1],
  1804. command[2],
  1805. command[3]);
  1806. /* Execute the command. */
  1807. if (mtip_exec_internal_command(port,
  1808. &fis,
  1809. 5,
  1810. (xfer_sz ? dma_addr : 0),
  1811. (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
  1812. 0,
  1813. GFP_KERNEL,
  1814. MTIP_IOCTL_COMMAND_TIMEOUT_MS)
  1815. < 0) {
  1816. rv = -EFAULT;
  1817. goto exit_drive_command;
  1818. }
  1819. /* Collect the completion status. */
  1820. command[0] = reply->command; /* Status*/
  1821. command[1] = reply->features; /* Error*/
  1822. command[2] = reply->sect_count;
  1823. dbg_printk(MTIP_DRV_NAME
  1824. " %s: Completion Status: stat %x, "
  1825. "err %x, nsect %x\n",
  1826. __func__,
  1827. command[0],
  1828. command[1],
  1829. command[2]);
  1830. if (xfer_sz) {
  1831. if (copy_to_user(user_buffer,
  1832. buf,
  1833. ATA_SECT_SIZE * command[3])) {
  1834. rv = -EFAULT;
  1835. goto exit_drive_command;
  1836. }
  1837. }
  1838. exit_drive_command:
  1839. if (buf)
  1840. dmam_free_coherent(&port->dd->pdev->dev,
  1841. ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
  1842. return rv;
  1843. }
  1844. /*
  1845. * Indicates whether a command has a single sector payload.
  1846. *
  1847. * @command passed to the device to perform the certain event.
  1848. * @features passed to the device to perform the certain event.
  1849. *
  1850. * return value
  1851. * 1 command is one that always has a single sector payload,
  1852. * regardless of the value in the Sector Count field.
  1853. * 0 otherwise
  1854. *
  1855. */
  1856. static unsigned int implicit_sector(unsigned char command,
  1857. unsigned char features)
  1858. {
  1859. unsigned int rv = 0;
  1860. /* list of commands that have an implicit sector count of 1 */
  1861. switch (command) {
  1862. case ATA_CMD_SEC_SET_PASS:
  1863. case ATA_CMD_SEC_UNLOCK:
  1864. case ATA_CMD_SEC_ERASE_PREP:
  1865. case ATA_CMD_SEC_ERASE_UNIT:
  1866. case ATA_CMD_SEC_FREEZE_LOCK:
  1867. case ATA_CMD_SEC_DISABLE_PASS:
  1868. case ATA_CMD_PMP_READ:
  1869. case ATA_CMD_PMP_WRITE:
  1870. rv = 1;
  1871. break;
  1872. case ATA_CMD_SET_MAX:
  1873. if (features == ATA_SET_MAX_UNLOCK)
  1874. rv = 1;
  1875. break;
  1876. case ATA_CMD_SMART:
  1877. if ((features == ATA_SMART_READ_VALUES) ||
  1878. (features == ATA_SMART_READ_THRESHOLDS))
  1879. rv = 1;
  1880. break;
  1881. case ATA_CMD_CONF_OVERLAY:
  1882. if ((features == ATA_DCO_IDENTIFY) ||
  1883. (features == ATA_DCO_SET))
  1884. rv = 1;
  1885. break;
  1886. }
  1887. return rv;
  1888. }
  1889. static void mtip_set_timeout(struct driver_data *dd,
  1890. struct host_to_dev_fis *fis,
  1891. unsigned int *timeout, u8 erasemode)
  1892. {
  1893. switch (fis->command) {
  1894. case ATA_CMD_DOWNLOAD_MICRO:
  1895. *timeout = 120000; /* 2 minutes */
  1896. break;
  1897. case ATA_CMD_SEC_ERASE_UNIT:
  1898. case 0xFC:
  1899. if (erasemode)
  1900. *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
  1901. else
  1902. *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
  1903. break;
  1904. case ATA_CMD_STANDBYNOW1:
  1905. *timeout = 120000; /* 2 minutes */
  1906. break;
  1907. case 0xF7:
  1908. case 0xFA:
  1909. *timeout = 60000; /* 60 seconds */
  1910. break;
  1911. case ATA_CMD_SMART:
  1912. *timeout = 15000; /* 15 seconds */
  1913. break;
  1914. default:
  1915. *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
  1916. break;
  1917. }
  1918. }
  1919. /*
  1920. * Executes a taskfile
  1921. * See ide_taskfile_ioctl() for derivation
  1922. */
  1923. static int exec_drive_taskfile(struct driver_data *dd,
  1924. void __user *buf,
  1925. ide_task_request_t *req_task,
  1926. int outtotal)
  1927. {
  1928. struct host_to_dev_fis fis;
  1929. struct host_to_dev_fis *reply;
  1930. u8 *outbuf = NULL;
  1931. u8 *inbuf = NULL;
  1932. dma_addr_t outbuf_dma = 0;
  1933. dma_addr_t inbuf_dma = 0;
  1934. dma_addr_t dma_buffer = 0;
  1935. int err = 0;
  1936. unsigned int taskin = 0;
  1937. unsigned int taskout = 0;
  1938. u8 nsect = 0;
  1939. unsigned int timeout;
  1940. unsigned int force_single_sector;
  1941. unsigned int transfer_size;
  1942. unsigned long task_file_data;
  1943. int intotal = outtotal + req_task->out_size;
  1944. int erasemode = 0;
  1945. taskout = req_task->out_size;
  1946. taskin = req_task->in_size;
  1947. /* 130560 = 512 * 0xFF*/
  1948. if (taskin > 130560 || taskout > 130560) {
  1949. err = -EINVAL;
  1950. goto abort;
  1951. }
  1952. if (taskout) {
  1953. outbuf = kzalloc(taskout, GFP_KERNEL);
  1954. if (outbuf == NULL) {
  1955. err = -ENOMEM;
  1956. goto abort;
  1957. }
  1958. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  1959. err = -EFAULT;
  1960. goto abort;
  1961. }
  1962. outbuf_dma = pci_map_single(dd->pdev,
  1963. outbuf,
  1964. taskout,
  1965. DMA_TO_DEVICE);
  1966. if (outbuf_dma == 0) {
  1967. err = -ENOMEM;
  1968. goto abort;
  1969. }
  1970. dma_buffer = outbuf_dma;
  1971. }
  1972. if (taskin) {
  1973. inbuf = kzalloc(taskin, GFP_KERNEL);
  1974. if (inbuf == NULL) {
  1975. err = -ENOMEM;
  1976. goto abort;
  1977. }
  1978. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  1979. err = -EFAULT;
  1980. goto abort;
  1981. }
  1982. inbuf_dma = pci_map_single(dd->pdev,
  1983. inbuf,
  1984. taskin, DMA_FROM_DEVICE);
  1985. if (inbuf_dma == 0) {
  1986. err = -ENOMEM;
  1987. goto abort;
  1988. }
  1989. dma_buffer = inbuf_dma;
  1990. }
  1991. /* only supports PIO and non-data commands from this ioctl. */
  1992. switch (req_task->data_phase) {
  1993. case TASKFILE_OUT:
  1994. nsect = taskout / ATA_SECT_SIZE;
  1995. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1996. break;
  1997. case TASKFILE_IN:
  1998. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1999. break;
  2000. case TASKFILE_NO_DATA:
  2001. reply = (dd->port->rxfis + RX_FIS_D2H_REG);
  2002. break;
  2003. default:
  2004. err = -EINVAL;
  2005. goto abort;
  2006. }
  2007. /* Build the FIS. */
  2008. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  2009. fis.type = 0x27;
  2010. fis.opts = 1 << 7;
  2011. fis.command = req_task->io_ports[7];
  2012. fis.features = req_task->io_ports[1];
  2013. fis.sect_count = req_task->io_ports[2];
  2014. fis.lba_low = req_task->io_ports[3];
  2015. fis.lba_mid = req_task->io_ports[4];
  2016. fis.lba_hi = req_task->io_ports[5];
  2017. /* Clear the dev bit*/
  2018. fis.device = req_task->io_ports[6] & ~0x10;
  2019. if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
  2020. req_task->in_flags.all =
  2021. IDE_TASKFILE_STD_IN_FLAGS |
  2022. (IDE_HOB_STD_IN_FLAGS << 8);
  2023. fis.lba_low_ex = req_task->hob_ports[3];
  2024. fis.lba_mid_ex = req_task->hob_ports[4];
  2025. fis.lba_hi_ex = req_task->hob_ports[5];
  2026. fis.features_ex = req_task->hob_ports[1];
  2027. fis.sect_cnt_ex = req_task->hob_ports[2];
  2028. } else {
  2029. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  2030. }
  2031. force_single_sector = implicit_sector(fis.command, fis.features);
  2032. if ((taskin || taskout) && (!fis.sect_count)) {
  2033. if (nsect)
  2034. fis.sect_count = nsect;
  2035. else {
  2036. if (!force_single_sector) {
  2037. dev_warn(&dd->pdev->dev,
  2038. "data movement but "
  2039. "sect_count is 0\n");
  2040. err = -EINVAL;
  2041. goto abort;
  2042. }
  2043. }
  2044. }
  2045. dbg_printk(MTIP_DRV_NAME
  2046. " %s: cmd %x, feat %x, nsect %x,"
  2047. " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
  2048. " head/dev %x\n",
  2049. __func__,
  2050. fis.command,
  2051. fis.features,
  2052. fis.sect_count,
  2053. fis.lba_low,
  2054. fis.lba_mid,
  2055. fis.lba_hi,
  2056. fis.device);
  2057. /* check for erase mode support during secure erase.*/
  2058. if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
  2059. (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
  2060. erasemode = 1;
  2061. }
  2062. mtip_set_timeout(dd, &fis, &timeout, erasemode);
  2063. /* Determine the correct transfer size.*/
  2064. if (force_single_sector)
  2065. transfer_size = ATA_SECT_SIZE;
  2066. else
  2067. transfer_size = ATA_SECT_SIZE * fis.sect_count;
  2068. /* Execute the command.*/
  2069. if (mtip_exec_internal_command(dd->port,
  2070. &fis,
  2071. 5,
  2072. dma_buffer,
  2073. transfer_size,
  2074. 0,
  2075. GFP_KERNEL,
  2076. timeout) < 0) {
  2077. err = -EIO;
  2078. goto abort;
  2079. }
  2080. task_file_data = readl(dd->port->mmio+PORT_TFDATA);
  2081. if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
  2082. reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
  2083. req_task->io_ports[7] = reply->control;
  2084. } else {
  2085. reply = dd->port->rxfis + RX_FIS_D2H_REG;
  2086. req_task->io_ports[7] = reply->command;
  2087. }
  2088. /* reclaim the DMA buffers.*/
  2089. if (inbuf_dma)
  2090. pci_unmap_single(dd->pdev, inbuf_dma,
  2091. taskin, DMA_FROM_DEVICE);
  2092. if (outbuf_dma)
  2093. pci_unmap_single(dd->pdev, outbuf_dma,
  2094. taskout, DMA_TO_DEVICE);
  2095. inbuf_dma = 0;
  2096. outbuf_dma = 0;
  2097. /* return the ATA registers to the caller.*/
  2098. req_task->io_ports[1] = reply->features;
  2099. req_task->io_ports[2] = reply->sect_count;
  2100. req_task->io_ports[3] = reply->lba_low;
  2101. req_task->io_ports[4] = reply->lba_mid;
  2102. req_task->io_ports[5] = reply->lba_hi;
  2103. req_task->io_ports[6] = reply->device;
  2104. if (req_task->out_flags.all & 1) {
  2105. req_task->hob_ports[3] = reply->lba_low_ex;
  2106. req_task->hob_ports[4] = reply->lba_mid_ex;
  2107. req_task->hob_ports[5] = reply->lba_hi_ex;
  2108. req_task->hob_ports[1] = reply->features_ex;
  2109. req_task->hob_ports[2] = reply->sect_cnt_ex;
  2110. }
  2111. dbg_printk(MTIP_DRV_NAME
  2112. " %s: Completion: stat %x,"
  2113. "err %x, sect_cnt %x, lbalo %x,"
  2114. "lbamid %x, lbahi %x, dev %x\n",
  2115. __func__,
  2116. req_task->io_ports[7],
  2117. req_task->io_ports[1],
  2118. req_task->io_ports[2],
  2119. req_task->io_ports[3],
  2120. req_task->io_ports[4],
  2121. req_task->io_ports[5],
  2122. req_task->io_ports[6]);
  2123. if (taskout) {
  2124. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  2125. err = -EFAULT;
  2126. goto abort;
  2127. }
  2128. }
  2129. if (taskin) {
  2130. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  2131. err = -EFAULT;
  2132. goto abort;
  2133. }
  2134. }
  2135. abort:
  2136. if (inbuf_dma)
  2137. pci_unmap_single(dd->pdev, inbuf_dma,
  2138. taskin, DMA_FROM_DEVICE);
  2139. if (outbuf_dma)
  2140. pci_unmap_single(dd->pdev, outbuf_dma,
  2141. taskout, DMA_TO_DEVICE);
  2142. kfree(outbuf);
  2143. kfree(inbuf);
  2144. return err;
  2145. }
  2146. /*
  2147. * Handle IOCTL calls from the Block Layer.
  2148. *
  2149. * This function is called by the Block Layer when it receives an IOCTL
  2150. * command that it does not understand. If the IOCTL command is not supported
  2151. * this function returns -ENOTTY.
  2152. *
  2153. * @dd Pointer to the driver data structure.
  2154. * @cmd IOCTL command passed from the Block Layer.
  2155. * @arg IOCTL argument passed from the Block Layer.
  2156. *
  2157. * return value
  2158. * 0 The IOCTL completed successfully.
  2159. * -ENOTTY The specified command is not supported.
  2160. * -EFAULT An error occurred copying data to a user space buffer.
  2161. * -EIO An error occurred while executing the command.
  2162. */
  2163. static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
  2164. unsigned long arg)
  2165. {
  2166. switch (cmd) {
  2167. case HDIO_GET_IDENTITY:
  2168. {
  2169. if (copy_to_user((void __user *)arg, dd->port->identify,
  2170. sizeof(u16) * ATA_ID_WORDS))
  2171. return -EFAULT;
  2172. break;
  2173. }
  2174. case HDIO_DRIVE_CMD:
  2175. {
  2176. u8 drive_command[4];
  2177. /* Copy the user command info to our buffer. */
  2178. if (copy_from_user(drive_command,
  2179. (void __user *) arg,
  2180. sizeof(drive_command)))
  2181. return -EFAULT;
  2182. /* Execute the drive command. */
  2183. if (exec_drive_command(dd->port,
  2184. drive_command,
  2185. (void __user *) (arg+4)))
  2186. return -EIO;
  2187. /* Copy the status back to the users buffer. */
  2188. if (copy_to_user((void __user *) arg,
  2189. drive_command,
  2190. sizeof(drive_command)))
  2191. return -EFAULT;
  2192. break;
  2193. }
  2194. case HDIO_DRIVE_TASK:
  2195. {
  2196. u8 drive_command[7];
  2197. /* Copy the user command info to our buffer. */
  2198. if (copy_from_user(drive_command,
  2199. (void __user *) arg,
  2200. sizeof(drive_command)))
  2201. return -EFAULT;
  2202. /* Execute the drive command. */
  2203. if (exec_drive_task(dd->port, drive_command))
  2204. return -EIO;
  2205. /* Copy the status back to the users buffer. */
  2206. if (copy_to_user((void __user *) arg,
  2207. drive_command,
  2208. sizeof(drive_command)))
  2209. return -EFAULT;
  2210. break;
  2211. }
  2212. case HDIO_DRIVE_TASKFILE: {
  2213. ide_task_request_t req_task;
  2214. int ret, outtotal;
  2215. if (copy_from_user(&req_task, (void __user *) arg,
  2216. sizeof(req_task)))
  2217. return -EFAULT;
  2218. outtotal = sizeof(req_task);
  2219. ret = exec_drive_taskfile(dd, (void __user *) arg,
  2220. &req_task, outtotal);
  2221. if (copy_to_user((void __user *) arg, &req_task,
  2222. sizeof(req_task)))
  2223. return -EFAULT;
  2224. return ret;
  2225. }
  2226. default:
  2227. return -EINVAL;
  2228. }
  2229. return 0;
  2230. }
  2231. /*
  2232. * Submit an IO to the hw
  2233. *
  2234. * This function is called by the block layer to issue an io
  2235. * to the device. Upon completion, the callback function will
  2236. * be called with the data parameter passed as the callback data.
  2237. *
  2238. * @dd Pointer to the driver data structure.
  2239. * @start First sector to read.
  2240. * @nsect Number of sectors to read.
  2241. * @nents Number of entries in scatter list for the read command.
  2242. * @tag The tag of this read command.
  2243. * @callback Pointer to the function that should be called
  2244. * when the read completes.
  2245. * @data Callback data passed to the callback function
  2246. * when the read completes.
  2247. * @dir Direction (read or write)
  2248. *
  2249. * return value
  2250. * None
  2251. */
  2252. static void mtip_hw_submit_io(struct driver_data *dd, sector_t sector,
  2253. int nsect, int nents, int tag, void *callback,
  2254. void *data, int dir, int unaligned)
  2255. {
  2256. struct host_to_dev_fis *fis;
  2257. struct mtip_port *port = dd->port;
  2258. struct mtip_cmd *command = &port->commands[tag];
  2259. int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2260. u64 start = sector;
  2261. /* Map the scatter list for DMA access */
  2262. nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
  2263. command->scatter_ents = nents;
  2264. command->unaligned = unaligned;
  2265. /*
  2266. * The number of retries for this command before it is
  2267. * reported as a failure to the upper layers.
  2268. */
  2269. command->retries = MTIP_MAX_RETRIES;
  2270. /* Fill out fis */
  2271. fis = command->command;
  2272. fis->type = 0x27;
  2273. fis->opts = 1 << 7;
  2274. fis->command =
  2275. (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
  2276. fis->lba_low = start & 0xFF;
  2277. fis->lba_mid = (start >> 8) & 0xFF;
  2278. fis->lba_hi = (start >> 16) & 0xFF;
  2279. fis->lba_low_ex = (start >> 24) & 0xFF;
  2280. fis->lba_mid_ex = (start >> 32) & 0xFF;
  2281. fis->lba_hi_ex = (start >> 40) & 0xFF;
  2282. fis->device = 1 << 6;
  2283. fis->features = nsect & 0xFF;
  2284. fis->features_ex = (nsect >> 8) & 0xFF;
  2285. fis->sect_count = ((tag << 3) | (tag >> 5));
  2286. fis->sect_cnt_ex = 0;
  2287. fis->control = 0;
  2288. fis->res2 = 0;
  2289. fis->res3 = 0;
  2290. fill_command_sg(dd, command, nents);
  2291. if (unaligned)
  2292. fis->device |= 1 << 7;
  2293. /* Populate the command header */
  2294. command->command_header->opts =
  2295. __force_bit2int cpu_to_le32(
  2296. (nents << 16) | 5 | AHCI_CMD_PREFETCH);
  2297. command->command_header->byte_count = 0;
  2298. /*
  2299. * Set the completion function and data for the command
  2300. * within this layer.
  2301. */
  2302. command->comp_data = dd;
  2303. command->comp_func = mtip_async_complete;
  2304. command->direction = dma_dir;
  2305. /*
  2306. * Set the completion function and data for the command passed
  2307. * from the upper layer.
  2308. */
  2309. command->async_data = data;
  2310. command->async_callback = callback;
  2311. /*
  2312. * To prevent this command from being issued
  2313. * if an internal command is in progress or error handling is active.
  2314. */
  2315. if (port->flags & MTIP_PF_PAUSE_IO) {
  2316. set_bit(tag, port->cmds_to_issue);
  2317. set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2318. return;
  2319. }
  2320. /* Issue the command to the hardware */
  2321. mtip_issue_ncq_command(port, tag);
  2322. return;
  2323. }
  2324. /*
  2325. * Release a command slot.
  2326. *
  2327. * @dd Pointer to the driver data structure.
  2328. * @tag Slot tag
  2329. *
  2330. * return value
  2331. * None
  2332. */
  2333. static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag,
  2334. int unaligned)
  2335. {
  2336. struct semaphore *sem = unaligned ? &dd->port->cmd_slot_unal :
  2337. &dd->port->cmd_slot;
  2338. release_slot(dd->port, tag);
  2339. up(sem);
  2340. }
  2341. /*
  2342. * Obtain a command slot and return its associated scatter list.
  2343. *
  2344. * @dd Pointer to the driver data structure.
  2345. * @tag Pointer to an int that will receive the allocated command
  2346. * slot tag.
  2347. *
  2348. * return value
  2349. * Pointer to the scatter list for the allocated command slot
  2350. * or NULL if no command slots are available.
  2351. */
  2352. static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
  2353. int *tag, int unaligned)
  2354. {
  2355. struct semaphore *sem = unaligned ? &dd->port->cmd_slot_unal :
  2356. &dd->port->cmd_slot;
  2357. /*
  2358. * It is possible that, even with this semaphore, a thread
  2359. * may think that no command slots are available. Therefore, we
  2360. * need to make an attempt to get_slot().
  2361. */
  2362. down(sem);
  2363. *tag = get_slot(dd->port);
  2364. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  2365. up(sem);
  2366. return NULL;
  2367. }
  2368. if (unlikely(*tag < 0)) {
  2369. up(sem);
  2370. return NULL;
  2371. }
  2372. return dd->port->commands[*tag].sg;
  2373. }
  2374. /*
  2375. * Sysfs status dump.
  2376. *
  2377. * @dev Pointer to the device structure, passed by the kernrel.
  2378. * @attr Pointer to the device_attribute structure passed by the kernel.
  2379. * @buf Pointer to the char buffer that will receive the stats info.
  2380. *
  2381. * return value
  2382. * The size, in bytes, of the data copied into buf.
  2383. */
  2384. static ssize_t mtip_hw_show_status(struct device *dev,
  2385. struct device_attribute *attr,
  2386. char *buf)
  2387. {
  2388. struct driver_data *dd = dev_to_disk(dev)->private_data;
  2389. int size = 0;
  2390. if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
  2391. size += sprintf(buf, "%s", "thermal_shutdown\n");
  2392. else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
  2393. size += sprintf(buf, "%s", "write_protect\n");
  2394. else
  2395. size += sprintf(buf, "%s", "online\n");
  2396. return size;
  2397. }
  2398. static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
  2399. /* debugsfs entries */
  2400. static ssize_t show_device_status(struct device_driver *drv, char *buf)
  2401. {
  2402. int size = 0;
  2403. struct driver_data *dd, *tmp;
  2404. unsigned long flags;
  2405. char id_buf[42];
  2406. u16 status = 0;
  2407. spin_lock_irqsave(&dev_lock, flags);
  2408. size += sprintf(&buf[size], "Devices Present:\n");
  2409. list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
  2410. if (dd->pdev) {
  2411. if (dd->port &&
  2412. dd->port->identify &&
  2413. dd->port->identify_valid) {
  2414. strlcpy(id_buf,
  2415. (char *) (dd->port->identify + 10), 21);
  2416. status = *(dd->port->identify + 141);
  2417. } else {
  2418. memset(id_buf, 0, 42);
  2419. status = 0;
  2420. }
  2421. if (dd->port &&
  2422. test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
  2423. size += sprintf(&buf[size],
  2424. " device %s %s (ftl rebuild %d %%)\n",
  2425. dev_name(&dd->pdev->dev),
  2426. id_buf,
  2427. status);
  2428. } else {
  2429. size += sprintf(&buf[size],
  2430. " device %s %s\n",
  2431. dev_name(&dd->pdev->dev),
  2432. id_buf);
  2433. }
  2434. }
  2435. }
  2436. size += sprintf(&buf[size], "Devices Being Removed:\n");
  2437. list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
  2438. if (dd->pdev) {
  2439. if (dd->port &&
  2440. dd->port->identify &&
  2441. dd->port->identify_valid) {
  2442. strlcpy(id_buf,
  2443. (char *) (dd->port->identify+10), 21);
  2444. status = *(dd->port->identify + 141);
  2445. } else {
  2446. memset(id_buf, 0, 42);
  2447. status = 0;
  2448. }
  2449. if (dd->port &&
  2450. test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
  2451. size += sprintf(&buf[size],
  2452. " device %s %s (ftl rebuild %d %%)\n",
  2453. dev_name(&dd->pdev->dev),
  2454. id_buf,
  2455. status);
  2456. } else {
  2457. size += sprintf(&buf[size],
  2458. " device %s %s\n",
  2459. dev_name(&dd->pdev->dev),
  2460. id_buf);
  2461. }
  2462. }
  2463. }
  2464. spin_unlock_irqrestore(&dev_lock, flags);
  2465. return size;
  2466. }
  2467. static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
  2468. size_t len, loff_t *offset)
  2469. {
  2470. struct driver_data *dd = (struct driver_data *)f->private_data;
  2471. int size = *offset;
  2472. char *buf;
  2473. int rv = 0;
  2474. if (!len || *offset)
  2475. return 0;
  2476. buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
  2477. if (!buf) {
  2478. dev_err(&dd->pdev->dev,
  2479. "Memory allocation: status buffer\n");
  2480. return -ENOMEM;
  2481. }
  2482. size += show_device_status(NULL, buf);
  2483. *offset = size <= len ? size : len;
  2484. size = copy_to_user(ubuf, buf, *offset);
  2485. if (size)
  2486. rv = -EFAULT;
  2487. kfree(buf);
  2488. return rv ? rv : *offset;
  2489. }
  2490. static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
  2491. size_t len, loff_t *offset)
  2492. {
  2493. struct driver_data *dd = (struct driver_data *)f->private_data;
  2494. char *buf;
  2495. u32 group_allocated;
  2496. int size = *offset;
  2497. int n, rv = 0;
  2498. if (!len || size)
  2499. return 0;
  2500. buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
  2501. if (!buf) {
  2502. dev_err(&dd->pdev->dev,
  2503. "Memory allocation: register buffer\n");
  2504. return -ENOMEM;
  2505. }
  2506. size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
  2507. for (n = dd->slot_groups-1; n >= 0; n--)
  2508. size += sprintf(&buf[size], "%08X ",
  2509. readl(dd->port->s_active[n]));
  2510. size += sprintf(&buf[size], "]\n");
  2511. size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
  2512. for (n = dd->slot_groups-1; n >= 0; n--)
  2513. size += sprintf(&buf[size], "%08X ",
  2514. readl(dd->port->cmd_issue[n]));
  2515. size += sprintf(&buf[size], "]\n");
  2516. size += sprintf(&buf[size], "H/ Completed : [ 0x");
  2517. for (n = dd->slot_groups-1; n >= 0; n--)
  2518. size += sprintf(&buf[size], "%08X ",
  2519. readl(dd->port->completed[n]));
  2520. size += sprintf(&buf[size], "]\n");
  2521. size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
  2522. readl(dd->port->mmio + PORT_IRQ_STAT));
  2523. size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
  2524. readl(dd->mmio + HOST_IRQ_STAT));
  2525. size += sprintf(&buf[size], "\n");
  2526. size += sprintf(&buf[size], "L/ Allocated : [ 0x");
  2527. for (n = dd->slot_groups-1; n >= 0; n--) {
  2528. if (sizeof(long) > sizeof(u32))
  2529. group_allocated =
  2530. dd->port->allocated[n/2] >> (32*(n&1));
  2531. else
  2532. group_allocated = dd->port->allocated[n];
  2533. size += sprintf(&buf[size], "%08X ", group_allocated);
  2534. }
  2535. size += sprintf(&buf[size], "]\n");
  2536. size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
  2537. for (n = dd->slot_groups-1; n >= 0; n--) {
  2538. if (sizeof(long) > sizeof(u32))
  2539. group_allocated =
  2540. dd->port->cmds_to_issue[n/2] >> (32*(n&1));
  2541. else
  2542. group_allocated = dd->port->cmds_to_issue[n];
  2543. size += sprintf(&buf[size], "%08X ", group_allocated);
  2544. }
  2545. size += sprintf(&buf[size], "]\n");
  2546. *offset = size <= len ? size : len;
  2547. size = copy_to_user(ubuf, buf, *offset);
  2548. if (size)
  2549. rv = -EFAULT;
  2550. kfree(buf);
  2551. return rv ? rv : *offset;
  2552. }
  2553. static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
  2554. size_t len, loff_t *offset)
  2555. {
  2556. struct driver_data *dd = (struct driver_data *)f->private_data;
  2557. char *buf;
  2558. int size = *offset;
  2559. int rv = 0;
  2560. if (!len || size)
  2561. return 0;
  2562. buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
  2563. if (!buf) {
  2564. dev_err(&dd->pdev->dev,
  2565. "Memory allocation: flag buffer\n");
  2566. return -ENOMEM;
  2567. }
  2568. size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
  2569. dd->port->flags);
  2570. size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
  2571. dd->dd_flag);
  2572. *offset = size <= len ? size : len;
  2573. size = copy_to_user(ubuf, buf, *offset);
  2574. if (size)
  2575. rv = -EFAULT;
  2576. kfree(buf);
  2577. return rv ? rv : *offset;
  2578. }
  2579. static const struct file_operations mtip_device_status_fops = {
  2580. .owner = THIS_MODULE,
  2581. .open = simple_open,
  2582. .read = mtip_hw_read_device_status,
  2583. .llseek = no_llseek,
  2584. };
  2585. static const struct file_operations mtip_regs_fops = {
  2586. .owner = THIS_MODULE,
  2587. .open = simple_open,
  2588. .read = mtip_hw_read_registers,
  2589. .llseek = no_llseek,
  2590. };
  2591. static const struct file_operations mtip_flags_fops = {
  2592. .owner = THIS_MODULE,
  2593. .open = simple_open,
  2594. .read = mtip_hw_read_flags,
  2595. .llseek = no_llseek,
  2596. };
  2597. /*
  2598. * Create the sysfs related attributes.
  2599. *
  2600. * @dd Pointer to the driver data structure.
  2601. * @kobj Pointer to the kobj for the block device.
  2602. *
  2603. * return value
  2604. * 0 Operation completed successfully.
  2605. * -EINVAL Invalid parameter.
  2606. */
  2607. static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
  2608. {
  2609. if (!kobj || !dd)
  2610. return -EINVAL;
  2611. if (sysfs_create_file(kobj, &dev_attr_status.attr))
  2612. dev_warn(&dd->pdev->dev,
  2613. "Error creating 'status' sysfs entry\n");
  2614. return 0;
  2615. }
  2616. /*
  2617. * Remove the sysfs related attributes.
  2618. *
  2619. * @dd Pointer to the driver data structure.
  2620. * @kobj Pointer to the kobj for the block device.
  2621. *
  2622. * return value
  2623. * 0 Operation completed successfully.
  2624. * -EINVAL Invalid parameter.
  2625. */
  2626. static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
  2627. {
  2628. if (!kobj || !dd)
  2629. return -EINVAL;
  2630. sysfs_remove_file(kobj, &dev_attr_status.attr);
  2631. return 0;
  2632. }
  2633. static int mtip_hw_debugfs_init(struct driver_data *dd)
  2634. {
  2635. if (!dfs_parent)
  2636. return -1;
  2637. dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
  2638. if (IS_ERR_OR_NULL(dd->dfs_node)) {
  2639. dev_warn(&dd->pdev->dev,
  2640. "Error creating node %s under debugfs\n",
  2641. dd->disk->disk_name);
  2642. dd->dfs_node = NULL;
  2643. return -1;
  2644. }
  2645. debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
  2646. &mtip_flags_fops);
  2647. debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
  2648. &mtip_regs_fops);
  2649. return 0;
  2650. }
  2651. static void mtip_hw_debugfs_exit(struct driver_data *dd)
  2652. {
  2653. if (dd->dfs_node)
  2654. debugfs_remove_recursive(dd->dfs_node);
  2655. }
  2656. static int mtip_free_orphan(struct driver_data *dd)
  2657. {
  2658. struct kobject *kobj;
  2659. if (dd->bdev) {
  2660. if (dd->bdev->bd_holders >= 1)
  2661. return -2;
  2662. bdput(dd->bdev);
  2663. dd->bdev = NULL;
  2664. }
  2665. mtip_hw_debugfs_exit(dd);
  2666. spin_lock(&rssd_index_lock);
  2667. ida_remove(&rssd_index_ida, dd->index);
  2668. spin_unlock(&rssd_index_lock);
  2669. if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag) &&
  2670. test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
  2671. put_disk(dd->disk);
  2672. } else {
  2673. if (dd->disk) {
  2674. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  2675. if (kobj) {
  2676. mtip_hw_sysfs_exit(dd, kobj);
  2677. kobject_put(kobj);
  2678. }
  2679. del_gendisk(dd->disk);
  2680. dd->disk = NULL;
  2681. }
  2682. if (dd->queue) {
  2683. dd->queue->queuedata = NULL;
  2684. blk_cleanup_queue(dd->queue);
  2685. dd->queue = NULL;
  2686. }
  2687. }
  2688. kfree(dd);
  2689. return 0;
  2690. }
  2691. /*
  2692. * Perform any init/resume time hardware setup
  2693. *
  2694. * @dd Pointer to the driver data structure.
  2695. *
  2696. * return value
  2697. * None
  2698. */
  2699. static inline void hba_setup(struct driver_data *dd)
  2700. {
  2701. u32 hwdata;
  2702. hwdata = readl(dd->mmio + HOST_HSORG);
  2703. /* interrupt bug workaround: use only 1 IS bit.*/
  2704. writel(hwdata |
  2705. HSORG_DISABLE_SLOTGRP_INTR |
  2706. HSORG_DISABLE_SLOTGRP_PXIS,
  2707. dd->mmio + HOST_HSORG);
  2708. }
  2709. static int mtip_device_unaligned_constrained(struct driver_data *dd)
  2710. {
  2711. return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
  2712. }
  2713. /*
  2714. * Detect the details of the product, and store anything needed
  2715. * into the driver data structure. This includes product type and
  2716. * version and number of slot groups.
  2717. *
  2718. * @dd Pointer to the driver data structure.
  2719. *
  2720. * return value
  2721. * None
  2722. */
  2723. static void mtip_detect_product(struct driver_data *dd)
  2724. {
  2725. u32 hwdata;
  2726. unsigned int rev, slotgroups;
  2727. /*
  2728. * HBA base + 0xFC [15:0] - vendor-specific hardware interface
  2729. * info register:
  2730. * [15:8] hardware/software interface rev#
  2731. * [ 3] asic-style interface
  2732. * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
  2733. */
  2734. hwdata = readl(dd->mmio + HOST_HSORG);
  2735. dd->product_type = MTIP_PRODUCT_UNKNOWN;
  2736. dd->slot_groups = 1;
  2737. if (hwdata & 0x8) {
  2738. dd->product_type = MTIP_PRODUCT_ASICFPGA;
  2739. rev = (hwdata & HSORG_HWREV) >> 8;
  2740. slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
  2741. dev_info(&dd->pdev->dev,
  2742. "ASIC-FPGA design, HS rev 0x%x, "
  2743. "%i slot groups [%i slots]\n",
  2744. rev,
  2745. slotgroups,
  2746. slotgroups * 32);
  2747. if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
  2748. dev_warn(&dd->pdev->dev,
  2749. "Warning: driver only supports "
  2750. "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
  2751. slotgroups = MTIP_MAX_SLOT_GROUPS;
  2752. }
  2753. dd->slot_groups = slotgroups;
  2754. return;
  2755. }
  2756. dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
  2757. }
  2758. /*
  2759. * Blocking wait for FTL rebuild to complete
  2760. *
  2761. * @dd Pointer to the DRIVER_DATA structure.
  2762. *
  2763. * return value
  2764. * 0 FTL rebuild completed successfully
  2765. * -EFAULT FTL rebuild error/timeout/interruption
  2766. */
  2767. static int mtip_ftl_rebuild_poll(struct driver_data *dd)
  2768. {
  2769. unsigned long timeout, cnt = 0, start;
  2770. dev_warn(&dd->pdev->dev,
  2771. "FTL rebuild in progress. Polling for completion.\n");
  2772. start = jiffies;
  2773. timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
  2774. do {
  2775. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2776. &dd->dd_flag)))
  2777. return -EFAULT;
  2778. if (mtip_check_surprise_removal(dd->pdev))
  2779. return -EFAULT;
  2780. if (mtip_get_identify(dd->port, NULL) < 0)
  2781. return -EFAULT;
  2782. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2783. MTIP_FTL_REBUILD_MAGIC) {
  2784. ssleep(1);
  2785. /* Print message every 3 minutes */
  2786. if (cnt++ >= 180) {
  2787. dev_warn(&dd->pdev->dev,
  2788. "FTL rebuild in progress (%d secs).\n",
  2789. jiffies_to_msecs(jiffies - start) / 1000);
  2790. cnt = 0;
  2791. }
  2792. } else {
  2793. dev_warn(&dd->pdev->dev,
  2794. "FTL rebuild complete (%d secs).\n",
  2795. jiffies_to_msecs(jiffies - start) / 1000);
  2796. mtip_block_initialize(dd);
  2797. return 0;
  2798. }
  2799. ssleep(10);
  2800. } while (time_before(jiffies, timeout));
  2801. /* Check for timeout */
  2802. dev_err(&dd->pdev->dev,
  2803. "Timed out waiting for FTL rebuild to complete (%d secs).\n",
  2804. jiffies_to_msecs(jiffies - start) / 1000);
  2805. return -EFAULT;
  2806. }
  2807. /*
  2808. * service thread to issue queued commands
  2809. *
  2810. * @data Pointer to the driver data structure.
  2811. *
  2812. * return value
  2813. * 0
  2814. */
  2815. static int mtip_service_thread(void *data)
  2816. {
  2817. struct driver_data *dd = (struct driver_data *)data;
  2818. unsigned long slot, slot_start, slot_wrap;
  2819. unsigned int num_cmd_slots = dd->slot_groups * 32;
  2820. struct mtip_port *port = dd->port;
  2821. int ret;
  2822. while (1) {
  2823. /*
  2824. * the condition is to check neither an internal command is
  2825. * is in progress nor error handling is active
  2826. */
  2827. wait_event_interruptible(port->svc_wait, (port->flags) &&
  2828. !(port->flags & MTIP_PF_PAUSE_IO));
  2829. if (kthread_should_stop())
  2830. goto st_out;
  2831. set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2832. /* If I am an orphan, start self cleanup */
  2833. if (test_bit(MTIP_PF_SR_CLEANUP_BIT, &port->flags))
  2834. break;
  2835. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2836. &dd->dd_flag)))
  2837. goto st_out;
  2838. if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
  2839. slot = 1;
  2840. /* used to restrict the loop to one iteration */
  2841. slot_start = num_cmd_slots;
  2842. slot_wrap = 0;
  2843. while (1) {
  2844. slot = find_next_bit(port->cmds_to_issue,
  2845. num_cmd_slots, slot);
  2846. if (slot_wrap == 1) {
  2847. if ((slot_start >= slot) ||
  2848. (slot >= num_cmd_slots))
  2849. break;
  2850. }
  2851. if (unlikely(slot_start == num_cmd_slots))
  2852. slot_start = slot;
  2853. if (unlikely(slot == num_cmd_slots)) {
  2854. slot = 1;
  2855. slot_wrap = 1;
  2856. continue;
  2857. }
  2858. /* Issue the command to the hardware */
  2859. mtip_issue_ncq_command(port, slot);
  2860. clear_bit(slot, port->cmds_to_issue);
  2861. }
  2862. clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2863. } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
  2864. if (mtip_ftl_rebuild_poll(dd) < 0)
  2865. set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
  2866. &dd->dd_flag);
  2867. clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
  2868. }
  2869. clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2870. if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
  2871. goto st_out;
  2872. }
  2873. /* wait for pci remove to exit */
  2874. while (1) {
  2875. if (test_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag))
  2876. break;
  2877. msleep_interruptible(1000);
  2878. if (kthread_should_stop())
  2879. goto st_out;
  2880. }
  2881. while (1) {
  2882. ret = mtip_free_orphan(dd);
  2883. if (!ret) {
  2884. /* NOTE: All data structures are invalid, do not
  2885. * access any here */
  2886. return 0;
  2887. }
  2888. msleep_interruptible(1000);
  2889. if (kthread_should_stop())
  2890. goto st_out;
  2891. }
  2892. st_out:
  2893. return 0;
  2894. }
  2895. /*
  2896. * Called once for each card.
  2897. *
  2898. * @dd Pointer to the driver data structure.
  2899. *
  2900. * return value
  2901. * 0 on success, else an error code.
  2902. */
  2903. static int mtip_hw_init(struct driver_data *dd)
  2904. {
  2905. int i;
  2906. int rv;
  2907. unsigned int num_command_slots;
  2908. unsigned long timeout, timetaken;
  2909. unsigned char *buf;
  2910. struct smart_attr attr242;
  2911. dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
  2912. mtip_detect_product(dd);
  2913. if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
  2914. rv = -EIO;
  2915. goto out1;
  2916. }
  2917. num_command_slots = dd->slot_groups * 32;
  2918. hba_setup(dd);
  2919. dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
  2920. dd->numa_node);
  2921. if (!dd->port) {
  2922. dev_err(&dd->pdev->dev,
  2923. "Memory allocation: port structure\n");
  2924. return -ENOMEM;
  2925. }
  2926. /* Continue workqueue setup */
  2927. for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
  2928. dd->work[i].port = dd->port;
  2929. /* Enable unaligned IO constraints for some devices */
  2930. if (mtip_device_unaligned_constrained(dd))
  2931. dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
  2932. else
  2933. dd->unal_qdepth = 0;
  2934. /* Counting semaphore to track command slot usage */
  2935. sema_init(&dd->port->cmd_slot, num_command_slots - 1 - dd->unal_qdepth);
  2936. sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
  2937. /* Spinlock to prevent concurrent issue */
  2938. for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
  2939. spin_lock_init(&dd->port->cmd_issue_lock[i]);
  2940. /* Set the port mmio base address. */
  2941. dd->port->mmio = dd->mmio + PORT_OFFSET;
  2942. dd->port->dd = dd;
  2943. /* Allocate memory for the command list. */
  2944. dd->port->command_list =
  2945. dmam_alloc_coherent(&dd->pdev->dev,
  2946. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2947. &dd->port->command_list_dma,
  2948. GFP_KERNEL);
  2949. if (!dd->port->command_list) {
  2950. dev_err(&dd->pdev->dev,
  2951. "Memory allocation: command list\n");
  2952. rv = -ENOMEM;
  2953. goto out1;
  2954. }
  2955. /* Clear the memory we have allocated. */
  2956. memset(dd->port->command_list,
  2957. 0,
  2958. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
  2959. /* Setup the addresse of the RX FIS. */
  2960. dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
  2961. dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
  2962. /* Setup the address of the command tables. */
  2963. dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
  2964. dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
  2965. /* Setup the address of the identify data. */
  2966. dd->port->identify = dd->port->command_table +
  2967. HW_CMD_TBL_AR_SZ;
  2968. dd->port->identify_dma = dd->port->command_tbl_dma +
  2969. HW_CMD_TBL_AR_SZ;
  2970. /* Setup the address of the sector buffer - for some non-ncq cmds */
  2971. dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
  2972. dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
  2973. /* Setup the address of the log buf - for read log command */
  2974. dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
  2975. dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
  2976. /* Setup the address of the smart buf - for smart read data command */
  2977. dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
  2978. dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
  2979. /* Point the command headers at the command tables. */
  2980. for (i = 0; i < num_command_slots; i++) {
  2981. dd->port->commands[i].command_header =
  2982. dd->port->command_list +
  2983. (sizeof(struct mtip_cmd_hdr) * i);
  2984. dd->port->commands[i].command_header_dma =
  2985. dd->port->command_list_dma +
  2986. (sizeof(struct mtip_cmd_hdr) * i);
  2987. dd->port->commands[i].command =
  2988. dd->port->command_table + (HW_CMD_TBL_SZ * i);
  2989. dd->port->commands[i].command_dma =
  2990. dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
  2991. if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
  2992. dd->port->commands[i].command_header->ctbau =
  2993. __force_bit2int cpu_to_le32(
  2994. (dd->port->commands[i].command_dma >> 16) >> 16);
  2995. dd->port->commands[i].command_header->ctba =
  2996. __force_bit2int cpu_to_le32(
  2997. dd->port->commands[i].command_dma & 0xFFFFFFFF);
  2998. /*
  2999. * If this is not done, a bug is reported by the stock
  3000. * FC11 i386. Due to the fact that it has lots of kernel
  3001. * debugging enabled.
  3002. */
  3003. sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
  3004. /* Mark all commands as currently inactive.*/
  3005. atomic_set(&dd->port->commands[i].active, 0);
  3006. }
  3007. /* Setup the pointers to the extended s_active and CI registers. */
  3008. for (i = 0; i < dd->slot_groups; i++) {
  3009. dd->port->s_active[i] =
  3010. dd->port->mmio + i*0x80 + PORT_SCR_ACT;
  3011. dd->port->cmd_issue[i] =
  3012. dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
  3013. dd->port->completed[i] =
  3014. dd->port->mmio + i*0x80 + PORT_SDBV;
  3015. }
  3016. timetaken = jiffies;
  3017. timeout = jiffies + msecs_to_jiffies(30000);
  3018. while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
  3019. time_before(jiffies, timeout)) {
  3020. mdelay(100);
  3021. }
  3022. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  3023. timetaken = jiffies - timetaken;
  3024. dev_warn(&dd->pdev->dev,
  3025. "Surprise removal detected at %u ms\n",
  3026. jiffies_to_msecs(timetaken));
  3027. rv = -ENODEV;
  3028. goto out2 ;
  3029. }
  3030. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  3031. timetaken = jiffies - timetaken;
  3032. dev_warn(&dd->pdev->dev,
  3033. "Removal detected at %u ms\n",
  3034. jiffies_to_msecs(timetaken));
  3035. rv = -EFAULT;
  3036. goto out2;
  3037. }
  3038. /* Conditionally reset the HBA. */
  3039. if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
  3040. if (mtip_hba_reset(dd) < 0) {
  3041. dev_err(&dd->pdev->dev,
  3042. "Card did not reset within timeout\n");
  3043. rv = -EIO;
  3044. goto out2;
  3045. }
  3046. } else {
  3047. /* Clear any pending interrupts on the HBA */
  3048. writel(readl(dd->mmio + HOST_IRQ_STAT),
  3049. dd->mmio + HOST_IRQ_STAT);
  3050. }
  3051. mtip_init_port(dd->port);
  3052. mtip_start_port(dd->port);
  3053. /* Setup the ISR and enable interrupts. */
  3054. rv = devm_request_irq(&dd->pdev->dev,
  3055. dd->pdev->irq,
  3056. mtip_irq_handler,
  3057. IRQF_SHARED,
  3058. dev_driver_string(&dd->pdev->dev),
  3059. dd);
  3060. if (rv) {
  3061. dev_err(&dd->pdev->dev,
  3062. "Unable to allocate IRQ %d\n", dd->pdev->irq);
  3063. goto out2;
  3064. }
  3065. irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
  3066. /* Enable interrupts on the HBA. */
  3067. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  3068. dd->mmio + HOST_CTL);
  3069. init_timer(&dd->port->cmd_timer);
  3070. init_waitqueue_head(&dd->port->svc_wait);
  3071. dd->port->cmd_timer.data = (unsigned long int) dd->port;
  3072. dd->port->cmd_timer.function = mtip_timeout_function;
  3073. mod_timer(&dd->port->cmd_timer,
  3074. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  3075. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
  3076. rv = -EFAULT;
  3077. goto out3;
  3078. }
  3079. if (mtip_get_identify(dd->port, NULL) < 0) {
  3080. rv = -EFAULT;
  3081. goto out3;
  3082. }
  3083. mtip_dump_identify(dd->port);
  3084. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  3085. MTIP_FTL_REBUILD_MAGIC) {
  3086. set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
  3087. return MTIP_FTL_REBUILD_MAGIC;
  3088. }
  3089. /* check write protect, over temp and rebuild statuses */
  3090. rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
  3091. dd->port->log_buf,
  3092. dd->port->log_buf_dma, 1);
  3093. if (rv) {
  3094. dev_warn(&dd->pdev->dev,
  3095. "Error in READ LOG EXT (10h) command\n");
  3096. /* non-critical error, don't fail the load */
  3097. } else {
  3098. buf = (unsigned char *)dd->port->log_buf;
  3099. if (buf[259] & 0x1) {
  3100. dev_info(&dd->pdev->dev,
  3101. "Write protect bit is set.\n");
  3102. set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
  3103. }
  3104. if (buf[288] == 0xF7) {
  3105. dev_info(&dd->pdev->dev,
  3106. "Exceeded Tmax, drive in thermal shutdown.\n");
  3107. set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
  3108. }
  3109. if (buf[288] == 0xBF) {
  3110. dev_info(&dd->pdev->dev,
  3111. "Drive is in security locked state.\n");
  3112. set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
  3113. }
  3114. }
  3115. /* get write protect progess */
  3116. memset(&attr242, 0, sizeof(struct smart_attr));
  3117. if (mtip_get_smart_attr(dd->port, 242, &attr242))
  3118. dev_warn(&dd->pdev->dev,
  3119. "Unable to check write protect progress\n");
  3120. else
  3121. dev_info(&dd->pdev->dev,
  3122. "Write protect progress: %u%% (%u blocks)\n",
  3123. attr242.cur, le32_to_cpu(attr242.data));
  3124. return rv;
  3125. out3:
  3126. del_timer_sync(&dd->port->cmd_timer);
  3127. /* Disable interrupts on the HBA. */
  3128. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  3129. dd->mmio + HOST_CTL);
  3130. /* Release the IRQ. */
  3131. irq_set_affinity_hint(dd->pdev->irq, NULL);
  3132. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  3133. out2:
  3134. mtip_deinit_port(dd->port);
  3135. /* Free the command/command header memory. */
  3136. dmam_free_coherent(&dd->pdev->dev,
  3137. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  3138. dd->port->command_list,
  3139. dd->port->command_list_dma);
  3140. out1:
  3141. /* Free the memory allocated for the for structure. */
  3142. kfree(dd->port);
  3143. return rv;
  3144. }
  3145. /*
  3146. * Called to deinitialize an interface.
  3147. *
  3148. * @dd Pointer to the driver data structure.
  3149. *
  3150. * return value
  3151. * 0
  3152. */
  3153. static int mtip_hw_exit(struct driver_data *dd)
  3154. {
  3155. /*
  3156. * Send standby immediate (E0h) to the drive so that it
  3157. * saves its state.
  3158. */
  3159. if (!dd->sr) {
  3160. if (!test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
  3161. if (mtip_standby_immediate(dd->port))
  3162. dev_warn(&dd->pdev->dev,
  3163. "STANDBY IMMEDIATE failed\n");
  3164. /* de-initialize the port. */
  3165. mtip_deinit_port(dd->port);
  3166. /* Disable interrupts on the HBA. */
  3167. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  3168. dd->mmio + HOST_CTL);
  3169. }
  3170. del_timer_sync(&dd->port->cmd_timer);
  3171. /* Release the IRQ. */
  3172. irq_set_affinity_hint(dd->pdev->irq, NULL);
  3173. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  3174. /* Free the command/command header memory. */
  3175. dmam_free_coherent(&dd->pdev->dev,
  3176. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  3177. dd->port->command_list,
  3178. dd->port->command_list_dma);
  3179. /* Free the memory allocated for the for structure. */
  3180. kfree(dd->port);
  3181. dd->port = NULL;
  3182. return 0;
  3183. }
  3184. /*
  3185. * Issue a Standby Immediate command to the device.
  3186. *
  3187. * This function is called by the Block Layer just before the
  3188. * system powers off during a shutdown.
  3189. *
  3190. * @dd Pointer to the driver data structure.
  3191. *
  3192. * return value
  3193. * 0
  3194. */
  3195. static int mtip_hw_shutdown(struct driver_data *dd)
  3196. {
  3197. /*
  3198. * Send standby immediate (E0h) to the drive so that it
  3199. * saves its state.
  3200. */
  3201. if (!dd->sr && dd->port)
  3202. mtip_standby_immediate(dd->port);
  3203. return 0;
  3204. }
  3205. /*
  3206. * Suspend function
  3207. *
  3208. * This function is called by the Block Layer just before the
  3209. * system hibernates.
  3210. *
  3211. * @dd Pointer to the driver data structure.
  3212. *
  3213. * return value
  3214. * 0 Suspend was successful
  3215. * -EFAULT Suspend was not successful
  3216. */
  3217. static int mtip_hw_suspend(struct driver_data *dd)
  3218. {
  3219. /*
  3220. * Send standby immediate (E0h) to the drive
  3221. * so that it saves its state.
  3222. */
  3223. if (mtip_standby_immediate(dd->port) != 0) {
  3224. dev_err(&dd->pdev->dev,
  3225. "Failed standby-immediate command\n");
  3226. return -EFAULT;
  3227. }
  3228. /* Disable interrupts on the HBA.*/
  3229. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  3230. dd->mmio + HOST_CTL);
  3231. mtip_deinit_port(dd->port);
  3232. return 0;
  3233. }
  3234. /*
  3235. * Resume function
  3236. *
  3237. * This function is called by the Block Layer as the
  3238. * system resumes.
  3239. *
  3240. * @dd Pointer to the driver data structure.
  3241. *
  3242. * return value
  3243. * 0 Resume was successful
  3244. * -EFAULT Resume was not successful
  3245. */
  3246. static int mtip_hw_resume(struct driver_data *dd)
  3247. {
  3248. /* Perform any needed hardware setup steps */
  3249. hba_setup(dd);
  3250. /* Reset the HBA */
  3251. if (mtip_hba_reset(dd) != 0) {
  3252. dev_err(&dd->pdev->dev,
  3253. "Unable to reset the HBA\n");
  3254. return -EFAULT;
  3255. }
  3256. /*
  3257. * Enable the port, DMA engine, and FIS reception specific
  3258. * h/w in controller.
  3259. */
  3260. mtip_init_port(dd->port);
  3261. mtip_start_port(dd->port);
  3262. /* Enable interrupts on the HBA.*/
  3263. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  3264. dd->mmio + HOST_CTL);
  3265. return 0;
  3266. }
  3267. /*
  3268. * Helper function for reusing disk name
  3269. * upon hot insertion.
  3270. */
  3271. static int rssd_disk_name_format(char *prefix,
  3272. int index,
  3273. char *buf,
  3274. int buflen)
  3275. {
  3276. const int base = 'z' - 'a' + 1;
  3277. char *begin = buf + strlen(prefix);
  3278. char *end = buf + buflen;
  3279. char *p;
  3280. int unit;
  3281. p = end - 1;
  3282. *p = '\0';
  3283. unit = base;
  3284. do {
  3285. if (p == begin)
  3286. return -EINVAL;
  3287. *--p = 'a' + (index % unit);
  3288. index = (index / unit) - 1;
  3289. } while (index >= 0);
  3290. memmove(begin, p, end - p);
  3291. memcpy(buf, prefix, strlen(prefix));
  3292. return 0;
  3293. }
  3294. /*
  3295. * Block layer IOCTL handler.
  3296. *
  3297. * @dev Pointer to the block_device structure.
  3298. * @mode ignored
  3299. * @cmd IOCTL command passed from the user application.
  3300. * @arg Argument passed from the user application.
  3301. *
  3302. * return value
  3303. * 0 IOCTL completed successfully.
  3304. * -ENOTTY IOCTL not supported or invalid driver data
  3305. * structure pointer.
  3306. */
  3307. static int mtip_block_ioctl(struct block_device *dev,
  3308. fmode_t mode,
  3309. unsigned cmd,
  3310. unsigned long arg)
  3311. {
  3312. struct driver_data *dd = dev->bd_disk->private_data;
  3313. if (!capable(CAP_SYS_ADMIN))
  3314. return -EACCES;
  3315. if (!dd)
  3316. return -ENOTTY;
  3317. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  3318. return -ENOTTY;
  3319. switch (cmd) {
  3320. case BLKFLSBUF:
  3321. return -ENOTTY;
  3322. default:
  3323. return mtip_hw_ioctl(dd, cmd, arg);
  3324. }
  3325. }
  3326. #ifdef CONFIG_COMPAT
  3327. /*
  3328. * Block layer compat IOCTL handler.
  3329. *
  3330. * @dev Pointer to the block_device structure.
  3331. * @mode ignored
  3332. * @cmd IOCTL command passed from the user application.
  3333. * @arg Argument passed from the user application.
  3334. *
  3335. * return value
  3336. * 0 IOCTL completed successfully.
  3337. * -ENOTTY IOCTL not supported or invalid driver data
  3338. * structure pointer.
  3339. */
  3340. static int mtip_block_compat_ioctl(struct block_device *dev,
  3341. fmode_t mode,
  3342. unsigned cmd,
  3343. unsigned long arg)
  3344. {
  3345. struct driver_data *dd = dev->bd_disk->private_data;
  3346. if (!capable(CAP_SYS_ADMIN))
  3347. return -EACCES;
  3348. if (!dd)
  3349. return -ENOTTY;
  3350. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  3351. return -ENOTTY;
  3352. switch (cmd) {
  3353. case BLKFLSBUF:
  3354. return -ENOTTY;
  3355. case HDIO_DRIVE_TASKFILE: {
  3356. struct mtip_compat_ide_task_request_s __user *compat_req_task;
  3357. ide_task_request_t req_task;
  3358. int compat_tasksize, outtotal, ret;
  3359. compat_tasksize =
  3360. sizeof(struct mtip_compat_ide_task_request_s);
  3361. compat_req_task =
  3362. (struct mtip_compat_ide_task_request_s __user *) arg;
  3363. if (copy_from_user(&req_task, (void __user *) arg,
  3364. compat_tasksize - (2 * sizeof(compat_long_t))))
  3365. return -EFAULT;
  3366. if (get_user(req_task.out_size, &compat_req_task->out_size))
  3367. return -EFAULT;
  3368. if (get_user(req_task.in_size, &compat_req_task->in_size))
  3369. return -EFAULT;
  3370. outtotal = sizeof(struct mtip_compat_ide_task_request_s);
  3371. ret = exec_drive_taskfile(dd, (void __user *) arg,
  3372. &req_task, outtotal);
  3373. if (copy_to_user((void __user *) arg, &req_task,
  3374. compat_tasksize -
  3375. (2 * sizeof(compat_long_t))))
  3376. return -EFAULT;
  3377. if (put_user(req_task.out_size, &compat_req_task->out_size))
  3378. return -EFAULT;
  3379. if (put_user(req_task.in_size, &compat_req_task->in_size))
  3380. return -EFAULT;
  3381. return ret;
  3382. }
  3383. default:
  3384. return mtip_hw_ioctl(dd, cmd, arg);
  3385. }
  3386. }
  3387. #endif
  3388. /*
  3389. * Obtain the geometry of the device.
  3390. *
  3391. * You may think that this function is obsolete, but some applications,
  3392. * fdisk for example still used CHS values. This function describes the
  3393. * device as having 224 heads and 56 sectors per cylinder. These values are
  3394. * chosen so that each cylinder is aligned on a 4KB boundary. Since a
  3395. * partition is described in terms of a start and end cylinder this means
  3396. * that each partition is also 4KB aligned. Non-aligned partitions adversely
  3397. * affects performance.
  3398. *
  3399. * @dev Pointer to the block_device strucutre.
  3400. * @geo Pointer to a hd_geometry structure.
  3401. *
  3402. * return value
  3403. * 0 Operation completed successfully.
  3404. * -ENOTTY An error occurred while reading the drive capacity.
  3405. */
  3406. static int mtip_block_getgeo(struct block_device *dev,
  3407. struct hd_geometry *geo)
  3408. {
  3409. struct driver_data *dd = dev->bd_disk->private_data;
  3410. sector_t capacity;
  3411. if (!dd)
  3412. return -ENOTTY;
  3413. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3414. dev_warn(&dd->pdev->dev,
  3415. "Could not get drive capacity.\n");
  3416. return -ENOTTY;
  3417. }
  3418. geo->heads = 224;
  3419. geo->sectors = 56;
  3420. sector_div(capacity, (geo->heads * geo->sectors));
  3421. geo->cylinders = capacity;
  3422. return 0;
  3423. }
  3424. /*
  3425. * Block device operation function.
  3426. *
  3427. * This structure contains pointers to the functions required by the block
  3428. * layer.
  3429. */
  3430. static const struct block_device_operations mtip_block_ops = {
  3431. .ioctl = mtip_block_ioctl,
  3432. #ifdef CONFIG_COMPAT
  3433. .compat_ioctl = mtip_block_compat_ioctl,
  3434. #endif
  3435. .getgeo = mtip_block_getgeo,
  3436. .owner = THIS_MODULE
  3437. };
  3438. /*
  3439. * Block layer make request function.
  3440. *
  3441. * This function is called by the kernel to process a BIO for
  3442. * the P320 device.
  3443. *
  3444. * @queue Pointer to the request queue. Unused other than to obtain
  3445. * the driver data structure.
  3446. * @bio Pointer to the BIO.
  3447. *
  3448. */
  3449. static void mtip_make_request(struct request_queue *queue, struct bio *bio)
  3450. {
  3451. struct driver_data *dd = queue->queuedata;
  3452. struct scatterlist *sg;
  3453. struct bio_vec *bvec;
  3454. int i, nents = 0;
  3455. int tag = 0, unaligned = 0;
  3456. if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
  3457. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  3458. &dd->dd_flag))) {
  3459. bio_endio(bio, -ENXIO);
  3460. return;
  3461. }
  3462. if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
  3463. bio_endio(bio, -ENODATA);
  3464. return;
  3465. }
  3466. if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
  3467. &dd->dd_flag) &&
  3468. bio_data_dir(bio))) {
  3469. bio_endio(bio, -ENODATA);
  3470. return;
  3471. }
  3472. if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
  3473. bio_endio(bio, -ENODATA);
  3474. return;
  3475. }
  3476. if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
  3477. bio_endio(bio, -ENXIO);
  3478. return;
  3479. }
  3480. }
  3481. if (unlikely(bio->bi_rw & REQ_DISCARD)) {
  3482. bio_endio(bio, mtip_send_trim(dd, bio->bi_sector,
  3483. bio_sectors(bio)));
  3484. return;
  3485. }
  3486. if (unlikely(!bio_has_data(bio))) {
  3487. blk_queue_flush(queue, 0);
  3488. bio_endio(bio, 0);
  3489. return;
  3490. }
  3491. if (bio_data_dir(bio) == WRITE && bio_sectors(bio) <= 64 &&
  3492. dd->unal_qdepth) {
  3493. if (bio->bi_sector % 8 != 0) /* Unaligned on 4k boundaries */
  3494. unaligned = 1;
  3495. else if (bio_sectors(bio) % 8 != 0) /* Aligned but not 4k/8k */
  3496. unaligned = 1;
  3497. }
  3498. sg = mtip_hw_get_scatterlist(dd, &tag, unaligned);
  3499. if (likely(sg != NULL)) {
  3500. blk_queue_bounce(queue, &bio);
  3501. if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
  3502. dev_warn(&dd->pdev->dev,
  3503. "Maximum number of SGL entries exceeded\n");
  3504. bio_io_error(bio);
  3505. mtip_hw_release_scatterlist(dd, tag, unaligned);
  3506. return;
  3507. }
  3508. /* Create the scatter list for this bio. */
  3509. bio_for_each_segment(bvec, bio, i) {
  3510. sg_set_page(&sg[nents],
  3511. bvec->bv_page,
  3512. bvec->bv_len,
  3513. bvec->bv_offset);
  3514. nents++;
  3515. }
  3516. /* Issue the read/write. */
  3517. mtip_hw_submit_io(dd,
  3518. bio->bi_sector,
  3519. bio_sectors(bio),
  3520. nents,
  3521. tag,
  3522. bio_endio,
  3523. bio,
  3524. bio_data_dir(bio),
  3525. unaligned);
  3526. } else
  3527. bio_io_error(bio);
  3528. }
  3529. /*
  3530. * Block layer initialization function.
  3531. *
  3532. * This function is called once by the PCI layer for each P320
  3533. * device that is connected to the system.
  3534. *
  3535. * @dd Pointer to the driver data structure.
  3536. *
  3537. * return value
  3538. * 0 on success else an error code.
  3539. */
  3540. static int mtip_block_initialize(struct driver_data *dd)
  3541. {
  3542. int rv = 0, wait_for_rebuild = 0;
  3543. sector_t capacity;
  3544. unsigned int index = 0;
  3545. struct kobject *kobj;
  3546. unsigned char thd_name[16];
  3547. if (dd->disk)
  3548. goto skip_create_disk; /* hw init done, before rebuild */
  3549. /* Initialize the protocol layer. */
  3550. wait_for_rebuild = mtip_hw_init(dd);
  3551. if (wait_for_rebuild < 0) {
  3552. dev_err(&dd->pdev->dev,
  3553. "Protocol layer initialization failed\n");
  3554. rv = -EINVAL;
  3555. goto protocol_init_error;
  3556. }
  3557. dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
  3558. if (dd->disk == NULL) {
  3559. dev_err(&dd->pdev->dev,
  3560. "Unable to allocate gendisk structure\n");
  3561. rv = -EINVAL;
  3562. goto alloc_disk_error;
  3563. }
  3564. /* Generate the disk name, implemented same as in sd.c */
  3565. do {
  3566. if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
  3567. goto ida_get_error;
  3568. spin_lock(&rssd_index_lock);
  3569. rv = ida_get_new(&rssd_index_ida, &index);
  3570. spin_unlock(&rssd_index_lock);
  3571. } while (rv == -EAGAIN);
  3572. if (rv)
  3573. goto ida_get_error;
  3574. rv = rssd_disk_name_format("rssd",
  3575. index,
  3576. dd->disk->disk_name,
  3577. DISK_NAME_LEN);
  3578. if (rv)
  3579. goto disk_index_error;
  3580. dd->disk->driverfs_dev = &dd->pdev->dev;
  3581. dd->disk->major = dd->major;
  3582. dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
  3583. dd->disk->fops = &mtip_block_ops;
  3584. dd->disk->private_data = dd;
  3585. dd->index = index;
  3586. mtip_hw_debugfs_init(dd);
  3587. /*
  3588. * if rebuild pending, start the service thread, and delay the block
  3589. * queue creation and add_disk()
  3590. */
  3591. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3592. goto start_service_thread;
  3593. skip_create_disk:
  3594. /* Allocate the request queue. */
  3595. dd->queue = blk_alloc_queue_node(GFP_KERNEL, dd->numa_node);
  3596. if (dd->queue == NULL) {
  3597. dev_err(&dd->pdev->dev,
  3598. "Unable to allocate request queue\n");
  3599. rv = -ENOMEM;
  3600. goto block_queue_alloc_init_error;
  3601. }
  3602. /* Attach our request function to the request queue. */
  3603. blk_queue_make_request(dd->queue, mtip_make_request);
  3604. dd->disk->queue = dd->queue;
  3605. dd->queue->queuedata = dd;
  3606. /* Set device limits. */
  3607. set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
  3608. blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
  3609. blk_queue_physical_block_size(dd->queue, 4096);
  3610. blk_queue_max_hw_sectors(dd->queue, 0xffff);
  3611. blk_queue_max_segment_size(dd->queue, 0x400000);
  3612. blk_queue_io_min(dd->queue, 4096);
  3613. /*
  3614. * write back cache is not supported in the device. FUA depends on
  3615. * write back cache support, hence setting flush support to zero.
  3616. */
  3617. blk_queue_flush(dd->queue, 0);
  3618. /* Signal trim support */
  3619. if (dd->trim_supp == true) {
  3620. set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
  3621. dd->queue->limits.discard_granularity = 4096;
  3622. blk_queue_max_discard_sectors(dd->queue,
  3623. MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
  3624. dd->queue->limits.discard_zeroes_data = 0;
  3625. }
  3626. /* Set the capacity of the device in 512 byte sectors. */
  3627. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3628. dev_warn(&dd->pdev->dev,
  3629. "Could not read drive capacity\n");
  3630. rv = -EIO;
  3631. goto read_capacity_error;
  3632. }
  3633. set_capacity(dd->disk, capacity);
  3634. /* Enable the block device and add it to /dev */
  3635. add_disk(dd->disk);
  3636. dd->bdev = bdget_disk(dd->disk, 0);
  3637. /*
  3638. * Now that the disk is active, initialize any sysfs attributes
  3639. * managed by the protocol layer.
  3640. */
  3641. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3642. if (kobj) {
  3643. mtip_hw_sysfs_init(dd, kobj);
  3644. kobject_put(kobj);
  3645. }
  3646. if (dd->mtip_svc_handler) {
  3647. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3648. return rv; /* service thread created for handling rebuild */
  3649. }
  3650. start_service_thread:
  3651. sprintf(thd_name, "mtip_svc_thd_%02d", index);
  3652. dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
  3653. dd, dd->numa_node, "%s",
  3654. thd_name);
  3655. if (IS_ERR(dd->mtip_svc_handler)) {
  3656. dev_err(&dd->pdev->dev, "service thread failed to start\n");
  3657. dd->mtip_svc_handler = NULL;
  3658. rv = -EFAULT;
  3659. goto kthread_run_error;
  3660. }
  3661. wake_up_process(dd->mtip_svc_handler);
  3662. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3663. rv = wait_for_rebuild;
  3664. return rv;
  3665. kthread_run_error:
  3666. bdput(dd->bdev);
  3667. dd->bdev = NULL;
  3668. /* Delete our gendisk. This also removes the device from /dev */
  3669. del_gendisk(dd->disk);
  3670. read_capacity_error:
  3671. blk_cleanup_queue(dd->queue);
  3672. block_queue_alloc_init_error:
  3673. mtip_hw_debugfs_exit(dd);
  3674. disk_index_error:
  3675. spin_lock(&rssd_index_lock);
  3676. ida_remove(&rssd_index_ida, index);
  3677. spin_unlock(&rssd_index_lock);
  3678. ida_get_error:
  3679. put_disk(dd->disk);
  3680. alloc_disk_error:
  3681. mtip_hw_exit(dd); /* De-initialize the protocol layer. */
  3682. protocol_init_error:
  3683. return rv;
  3684. }
  3685. /*
  3686. * Block layer deinitialization function.
  3687. *
  3688. * Called by the PCI layer as each P320 device is removed.
  3689. *
  3690. * @dd Pointer to the driver data structure.
  3691. *
  3692. * return value
  3693. * 0
  3694. */
  3695. static int mtip_block_remove(struct driver_data *dd)
  3696. {
  3697. struct kobject *kobj;
  3698. if (!dd->sr) {
  3699. mtip_hw_debugfs_exit(dd);
  3700. if (dd->mtip_svc_handler) {
  3701. set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
  3702. wake_up_interruptible(&dd->port->svc_wait);
  3703. kthread_stop(dd->mtip_svc_handler);
  3704. }
  3705. /* Clean up the sysfs attributes, if created */
  3706. if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
  3707. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3708. if (kobj) {
  3709. mtip_hw_sysfs_exit(dd, kobj);
  3710. kobject_put(kobj);
  3711. }
  3712. }
  3713. /*
  3714. * Delete our gendisk structure. This also removes the device
  3715. * from /dev
  3716. */
  3717. if (dd->bdev) {
  3718. bdput(dd->bdev);
  3719. dd->bdev = NULL;
  3720. }
  3721. if (dd->disk) {
  3722. if (dd->disk->queue) {
  3723. del_gendisk(dd->disk);
  3724. blk_cleanup_queue(dd->queue);
  3725. dd->queue = NULL;
  3726. } else
  3727. put_disk(dd->disk);
  3728. }
  3729. dd->disk = NULL;
  3730. spin_lock(&rssd_index_lock);
  3731. ida_remove(&rssd_index_ida, dd->index);
  3732. spin_unlock(&rssd_index_lock);
  3733. } else {
  3734. dev_info(&dd->pdev->dev, "device %s surprise removal\n",
  3735. dd->disk->disk_name);
  3736. }
  3737. /* De-initialize the protocol layer. */
  3738. mtip_hw_exit(dd);
  3739. return 0;
  3740. }
  3741. /*
  3742. * Function called by the PCI layer when just before the
  3743. * machine shuts down.
  3744. *
  3745. * If a protocol layer shutdown function is present it will be called
  3746. * by this function.
  3747. *
  3748. * @dd Pointer to the driver data structure.
  3749. *
  3750. * return value
  3751. * 0
  3752. */
  3753. static int mtip_block_shutdown(struct driver_data *dd)
  3754. {
  3755. /* Delete our gendisk structure, and cleanup the blk queue. */
  3756. if (dd->disk) {
  3757. dev_info(&dd->pdev->dev,
  3758. "Shutting down %s ...\n", dd->disk->disk_name);
  3759. if (dd->disk->queue) {
  3760. del_gendisk(dd->disk);
  3761. blk_cleanup_queue(dd->queue);
  3762. } else
  3763. put_disk(dd->disk);
  3764. dd->disk = NULL;
  3765. dd->queue = NULL;
  3766. }
  3767. spin_lock(&rssd_index_lock);
  3768. ida_remove(&rssd_index_ida, dd->index);
  3769. spin_unlock(&rssd_index_lock);
  3770. mtip_hw_shutdown(dd);
  3771. return 0;
  3772. }
  3773. static int mtip_block_suspend(struct driver_data *dd)
  3774. {
  3775. dev_info(&dd->pdev->dev,
  3776. "Suspending %s ...\n", dd->disk->disk_name);
  3777. mtip_hw_suspend(dd);
  3778. return 0;
  3779. }
  3780. static int mtip_block_resume(struct driver_data *dd)
  3781. {
  3782. dev_info(&dd->pdev->dev, "Resuming %s ...\n",
  3783. dd->disk->disk_name);
  3784. mtip_hw_resume(dd);
  3785. return 0;
  3786. }
  3787. static void drop_cpu(int cpu)
  3788. {
  3789. cpu_use[cpu]--;
  3790. }
  3791. static int get_least_used_cpu_on_node(int node)
  3792. {
  3793. int cpu, least_used_cpu, least_cnt;
  3794. const struct cpumask *node_mask;
  3795. node_mask = cpumask_of_node(node);
  3796. least_used_cpu = cpumask_first(node_mask);
  3797. least_cnt = cpu_use[least_used_cpu];
  3798. cpu = least_used_cpu;
  3799. for_each_cpu(cpu, node_mask) {
  3800. if (cpu_use[cpu] < least_cnt) {
  3801. least_used_cpu = cpu;
  3802. least_cnt = cpu_use[cpu];
  3803. }
  3804. }
  3805. cpu_use[least_used_cpu]++;
  3806. return least_used_cpu;
  3807. }
  3808. /* Helper for selecting a node in round robin mode */
  3809. static inline int mtip_get_next_rr_node(void)
  3810. {
  3811. static int next_node = -1;
  3812. if (next_node == -1) {
  3813. next_node = first_online_node;
  3814. return next_node;
  3815. }
  3816. next_node = next_online_node(next_node);
  3817. if (next_node == MAX_NUMNODES)
  3818. next_node = first_online_node;
  3819. return next_node;
  3820. }
  3821. static DEFINE_HANDLER(0);
  3822. static DEFINE_HANDLER(1);
  3823. static DEFINE_HANDLER(2);
  3824. static DEFINE_HANDLER(3);
  3825. static DEFINE_HANDLER(4);
  3826. static DEFINE_HANDLER(5);
  3827. static DEFINE_HANDLER(6);
  3828. static DEFINE_HANDLER(7);
  3829. /*
  3830. * Called for each supported PCI device detected.
  3831. *
  3832. * This function allocates the private data structure, enables the
  3833. * PCI device and then calls the block layer initialization function.
  3834. *
  3835. * return value
  3836. * 0 on success else an error code.
  3837. */
  3838. static int mtip_pci_probe(struct pci_dev *pdev,
  3839. const struct pci_device_id *ent)
  3840. {
  3841. int rv = 0;
  3842. struct driver_data *dd = NULL;
  3843. char cpu_list[256];
  3844. const struct cpumask *node_mask;
  3845. int cpu, i = 0, j = 0;
  3846. int my_node = NUMA_NO_NODE;
  3847. unsigned long flags;
  3848. /* Allocate memory for this devices private data. */
  3849. my_node = pcibus_to_node(pdev->bus);
  3850. if (my_node != NUMA_NO_NODE) {
  3851. if (!node_online(my_node))
  3852. my_node = mtip_get_next_rr_node();
  3853. } else {
  3854. dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
  3855. my_node = mtip_get_next_rr_node();
  3856. }
  3857. dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
  3858. my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
  3859. cpu_to_node(smp_processor_id()), smp_processor_id());
  3860. dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
  3861. if (dd == NULL) {
  3862. dev_err(&pdev->dev,
  3863. "Unable to allocate memory for driver data\n");
  3864. return -ENOMEM;
  3865. }
  3866. /* Attach the private data to this PCI device. */
  3867. pci_set_drvdata(pdev, dd);
  3868. rv = pcim_enable_device(pdev);
  3869. if (rv < 0) {
  3870. dev_err(&pdev->dev, "Unable to enable device\n");
  3871. goto iomap_err;
  3872. }
  3873. /* Map BAR5 to memory. */
  3874. rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
  3875. if (rv < 0) {
  3876. dev_err(&pdev->dev, "Unable to map regions\n");
  3877. goto iomap_err;
  3878. }
  3879. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3880. rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3881. if (rv) {
  3882. rv = pci_set_consistent_dma_mask(pdev,
  3883. DMA_BIT_MASK(32));
  3884. if (rv) {
  3885. dev_warn(&pdev->dev,
  3886. "64-bit DMA enable failed\n");
  3887. goto setmask_err;
  3888. }
  3889. }
  3890. }
  3891. /* Copy the info we may need later into the private data structure. */
  3892. dd->major = mtip_major;
  3893. dd->instance = instance;
  3894. dd->pdev = pdev;
  3895. dd->numa_node = my_node;
  3896. INIT_LIST_HEAD(&dd->online_list);
  3897. INIT_LIST_HEAD(&dd->remove_list);
  3898. memset(dd->workq_name, 0, 32);
  3899. snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
  3900. dd->isr_workq = create_workqueue(dd->workq_name);
  3901. if (!dd->isr_workq) {
  3902. dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
  3903. rv = -ENOMEM;
  3904. goto block_initialize_err;
  3905. }
  3906. memset(cpu_list, 0, sizeof(cpu_list));
  3907. node_mask = cpumask_of_node(dd->numa_node);
  3908. if (!cpumask_empty(node_mask)) {
  3909. for_each_cpu(cpu, node_mask)
  3910. {
  3911. snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
  3912. j = strlen(cpu_list);
  3913. }
  3914. dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
  3915. dd->numa_node,
  3916. topology_physical_package_id(cpumask_first(node_mask)),
  3917. nr_cpus_node(dd->numa_node),
  3918. cpu_list);
  3919. } else
  3920. dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
  3921. dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
  3922. dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
  3923. cpu_to_node(dd->isr_binding), dd->isr_binding);
  3924. /* first worker context always runs in ISR */
  3925. dd->work[0].cpu_binding = dd->isr_binding;
  3926. dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
  3927. dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
  3928. dd->work[3].cpu_binding = dd->work[0].cpu_binding;
  3929. dd->work[4].cpu_binding = dd->work[1].cpu_binding;
  3930. dd->work[5].cpu_binding = dd->work[2].cpu_binding;
  3931. dd->work[6].cpu_binding = dd->work[2].cpu_binding;
  3932. dd->work[7].cpu_binding = dd->work[1].cpu_binding;
  3933. /* Log the bindings */
  3934. for_each_present_cpu(cpu) {
  3935. memset(cpu_list, 0, sizeof(cpu_list));
  3936. for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
  3937. if (dd->work[i].cpu_binding == cpu) {
  3938. snprintf(&cpu_list[j], 256 - j, "%d ", i);
  3939. j = strlen(cpu_list);
  3940. }
  3941. }
  3942. if (j)
  3943. dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
  3944. }
  3945. INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
  3946. INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
  3947. INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
  3948. INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
  3949. INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
  3950. INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
  3951. INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
  3952. INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
  3953. pci_set_master(pdev);
  3954. rv = pci_enable_msi(pdev);
  3955. if (rv) {
  3956. dev_warn(&pdev->dev,
  3957. "Unable to enable MSI interrupt.\n");
  3958. goto block_initialize_err;
  3959. }
  3960. /* Initialize the block layer. */
  3961. rv = mtip_block_initialize(dd);
  3962. if (rv < 0) {
  3963. dev_err(&pdev->dev,
  3964. "Unable to initialize block layer\n");
  3965. goto block_initialize_err;
  3966. }
  3967. /*
  3968. * Increment the instance count so that each device has a unique
  3969. * instance number.
  3970. */
  3971. instance++;
  3972. if (rv != MTIP_FTL_REBUILD_MAGIC)
  3973. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3974. else
  3975. rv = 0; /* device in rebuild state, return 0 from probe */
  3976. /* Add to online list even if in ftl rebuild */
  3977. spin_lock_irqsave(&dev_lock, flags);
  3978. list_add(&dd->online_list, &online_list);
  3979. spin_unlock_irqrestore(&dev_lock, flags);
  3980. goto done;
  3981. block_initialize_err:
  3982. pci_disable_msi(pdev);
  3983. if (dd->isr_workq) {
  3984. flush_workqueue(dd->isr_workq);
  3985. destroy_workqueue(dd->isr_workq);
  3986. drop_cpu(dd->work[0].cpu_binding);
  3987. drop_cpu(dd->work[1].cpu_binding);
  3988. drop_cpu(dd->work[2].cpu_binding);
  3989. }
  3990. setmask_err:
  3991. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3992. iomap_err:
  3993. kfree(dd);
  3994. pci_set_drvdata(pdev, NULL);
  3995. return rv;
  3996. done:
  3997. return rv;
  3998. }
  3999. /*
  4000. * Called for each probed device when the device is removed or the
  4001. * driver is unloaded.
  4002. *
  4003. * return value
  4004. * None
  4005. */
  4006. static void mtip_pci_remove(struct pci_dev *pdev)
  4007. {
  4008. struct driver_data *dd = pci_get_drvdata(pdev);
  4009. unsigned long flags, to;
  4010. set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
  4011. spin_lock_irqsave(&dev_lock, flags);
  4012. list_del_init(&dd->online_list);
  4013. list_add(&dd->remove_list, &removing_list);
  4014. spin_unlock_irqrestore(&dev_lock, flags);
  4015. mtip_check_surprise_removal(pdev);
  4016. synchronize_irq(dd->pdev->irq);
  4017. /* Spin until workers are done */
  4018. to = jiffies + msecs_to_jiffies(4000);
  4019. do {
  4020. msleep(20);
  4021. } while (atomic_read(&dd->irq_workers_active) != 0 &&
  4022. time_before(jiffies, to));
  4023. if (atomic_read(&dd->irq_workers_active) != 0) {
  4024. dev_warn(&dd->pdev->dev,
  4025. "Completion workers still active!\n");
  4026. }
  4027. /* Cleanup the outstanding commands */
  4028. mtip_command_cleanup(dd);
  4029. /* Clean up the block layer. */
  4030. mtip_block_remove(dd);
  4031. if (dd->isr_workq) {
  4032. flush_workqueue(dd->isr_workq);
  4033. destroy_workqueue(dd->isr_workq);
  4034. drop_cpu(dd->work[0].cpu_binding);
  4035. drop_cpu(dd->work[1].cpu_binding);
  4036. drop_cpu(dd->work[2].cpu_binding);
  4037. }
  4038. pci_disable_msi(pdev);
  4039. spin_lock_irqsave(&dev_lock, flags);
  4040. list_del_init(&dd->remove_list);
  4041. spin_unlock_irqrestore(&dev_lock, flags);
  4042. if (!dd->sr)
  4043. kfree(dd);
  4044. else
  4045. set_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag);
  4046. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  4047. pci_set_drvdata(pdev, NULL);
  4048. pci_dev_put(pdev);
  4049. }
  4050. /*
  4051. * Called for each probed device when the device is suspended.
  4052. *
  4053. * return value
  4054. * 0 Success
  4055. * <0 Error
  4056. */
  4057. static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  4058. {
  4059. int rv = 0;
  4060. struct driver_data *dd = pci_get_drvdata(pdev);
  4061. if (!dd) {
  4062. dev_err(&pdev->dev,
  4063. "Driver private datastructure is NULL\n");
  4064. return -EFAULT;
  4065. }
  4066. set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  4067. /* Disable ports & interrupts then send standby immediate */
  4068. rv = mtip_block_suspend(dd);
  4069. if (rv < 0) {
  4070. dev_err(&pdev->dev,
  4071. "Failed to suspend controller\n");
  4072. return rv;
  4073. }
  4074. /*
  4075. * Save the pci config space to pdev structure &
  4076. * disable the device
  4077. */
  4078. pci_save_state(pdev);
  4079. pci_disable_device(pdev);
  4080. /* Move to Low power state*/
  4081. pci_set_power_state(pdev, PCI_D3hot);
  4082. return rv;
  4083. }
  4084. /*
  4085. * Called for each probed device when the device is resumed.
  4086. *
  4087. * return value
  4088. * 0 Success
  4089. * <0 Error
  4090. */
  4091. static int mtip_pci_resume(struct pci_dev *pdev)
  4092. {
  4093. int rv = 0;
  4094. struct driver_data *dd;
  4095. dd = pci_get_drvdata(pdev);
  4096. if (!dd) {
  4097. dev_err(&pdev->dev,
  4098. "Driver private datastructure is NULL\n");
  4099. return -EFAULT;
  4100. }
  4101. /* Move the device to active State */
  4102. pci_set_power_state(pdev, PCI_D0);
  4103. /* Restore PCI configuration space */
  4104. pci_restore_state(pdev);
  4105. /* Enable the PCI device*/
  4106. rv = pcim_enable_device(pdev);
  4107. if (rv < 0) {
  4108. dev_err(&pdev->dev,
  4109. "Failed to enable card during resume\n");
  4110. goto err;
  4111. }
  4112. pci_set_master(pdev);
  4113. /*
  4114. * Calls hbaReset, initPort, & startPort function
  4115. * then enables interrupts
  4116. */
  4117. rv = mtip_block_resume(dd);
  4118. if (rv < 0)
  4119. dev_err(&pdev->dev, "Unable to resume\n");
  4120. err:
  4121. clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  4122. return rv;
  4123. }
  4124. /*
  4125. * Shutdown routine
  4126. *
  4127. * return value
  4128. * None
  4129. */
  4130. static void mtip_pci_shutdown(struct pci_dev *pdev)
  4131. {
  4132. struct driver_data *dd = pci_get_drvdata(pdev);
  4133. if (dd)
  4134. mtip_block_shutdown(dd);
  4135. }
  4136. /* Table of device ids supported by this driver. */
  4137. static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
  4138. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
  4139. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
  4140. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
  4141. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
  4142. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
  4143. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
  4144. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
  4145. { 0 }
  4146. };
  4147. /* Structure that describes the PCI driver functions. */
  4148. static struct pci_driver mtip_pci_driver = {
  4149. .name = MTIP_DRV_NAME,
  4150. .id_table = mtip_pci_tbl,
  4151. .probe = mtip_pci_probe,
  4152. .remove = mtip_pci_remove,
  4153. .suspend = mtip_pci_suspend,
  4154. .resume = mtip_pci_resume,
  4155. .shutdown = mtip_pci_shutdown,
  4156. };
  4157. MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
  4158. /*
  4159. * Module initialization function.
  4160. *
  4161. * Called once when the module is loaded. This function allocates a major
  4162. * block device number to the Cyclone devices and registers the PCI layer
  4163. * of the driver.
  4164. *
  4165. * Return value
  4166. * 0 on success else error code.
  4167. */
  4168. static int __init mtip_init(void)
  4169. {
  4170. int error;
  4171. pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
  4172. spin_lock_init(&dev_lock);
  4173. INIT_LIST_HEAD(&online_list);
  4174. INIT_LIST_HEAD(&removing_list);
  4175. /* Allocate a major block device number to use with this driver. */
  4176. error = register_blkdev(0, MTIP_DRV_NAME);
  4177. if (error <= 0) {
  4178. pr_err("Unable to register block device (%d)\n",
  4179. error);
  4180. return -EBUSY;
  4181. }
  4182. mtip_major = error;
  4183. dfs_parent = debugfs_create_dir("rssd", NULL);
  4184. if (IS_ERR_OR_NULL(dfs_parent)) {
  4185. pr_warn("Error creating debugfs parent\n");
  4186. dfs_parent = NULL;
  4187. }
  4188. if (dfs_parent) {
  4189. dfs_device_status = debugfs_create_file("device_status",
  4190. S_IRUGO, dfs_parent, NULL,
  4191. &mtip_device_status_fops);
  4192. if (IS_ERR_OR_NULL(dfs_device_status)) {
  4193. pr_err("Error creating device_status node\n");
  4194. dfs_device_status = NULL;
  4195. }
  4196. }
  4197. /* Register our PCI operations. */
  4198. error = pci_register_driver(&mtip_pci_driver);
  4199. if (error) {
  4200. debugfs_remove(dfs_parent);
  4201. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  4202. }
  4203. return error;
  4204. }
  4205. /*
  4206. * Module de-initialization function.
  4207. *
  4208. * Called once when the module is unloaded. This function deallocates
  4209. * the major block device number allocated by mtip_init() and
  4210. * unregisters the PCI layer of the driver.
  4211. *
  4212. * Return value
  4213. * none
  4214. */
  4215. static void __exit mtip_exit(void)
  4216. {
  4217. debugfs_remove_recursive(dfs_parent);
  4218. /* Release the allocated major block device number. */
  4219. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  4220. /* Unregister the PCI driver. */
  4221. pci_unregister_driver(&mtip_pci_driver);
  4222. }
  4223. MODULE_AUTHOR("Micron Technology, Inc");
  4224. MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
  4225. MODULE_LICENSE("GPL");
  4226. MODULE_VERSION(MTIP_DRV_VERSION);
  4227. module_init(mtip_init);
  4228. module_exit(mtip_exit);