mtip32xx.c 111 KB

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