mtip32xx.c 113 KB

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