mtip32xx.c 117 KB

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