mtip32xx.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460
  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. /* Set the identify buffer as valid. */
  1318. port->identify_valid = 1;
  1319. if (user_buffer) {
  1320. if (copy_to_user(
  1321. user_buffer,
  1322. port->identify,
  1323. ATA_ID_WORDS * sizeof(u16))) {
  1324. rv = -EFAULT;
  1325. goto out;
  1326. }
  1327. }
  1328. out:
  1329. return rv;
  1330. }
  1331. /*
  1332. * Issue a standby immediate command to the device.
  1333. *
  1334. * @port Pointer to the port structure.
  1335. *
  1336. * return value
  1337. * 0 Command was executed successfully.
  1338. * -1 An error occurred while executing the command.
  1339. */
  1340. static int mtip_standby_immediate(struct mtip_port *port)
  1341. {
  1342. int rv;
  1343. struct host_to_dev_fis fis;
  1344. unsigned long start;
  1345. /* Build the FIS. */
  1346. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1347. fis.type = 0x27;
  1348. fis.opts = 1 << 7;
  1349. fis.command = ATA_CMD_STANDBYNOW1;
  1350. start = jiffies;
  1351. rv = mtip_exec_internal_command(port,
  1352. &fis,
  1353. 5,
  1354. 0,
  1355. 0,
  1356. 0,
  1357. GFP_ATOMIC,
  1358. 15000);
  1359. dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
  1360. jiffies_to_msecs(jiffies - start));
  1361. if (rv)
  1362. dev_warn(&port->dd->pdev->dev,
  1363. "STANDBY IMMEDIATE command failed.\n");
  1364. return rv;
  1365. }
  1366. /*
  1367. * Issue a READ LOG EXT command to the device.
  1368. *
  1369. * @port pointer to the port structure.
  1370. * @page page number to fetch
  1371. * @buffer pointer to buffer
  1372. * @buffer_dma dma address corresponding to @buffer
  1373. * @sectors page length to fetch, in sectors
  1374. *
  1375. * return value
  1376. * @rv return value from mtip_exec_internal_command()
  1377. */
  1378. static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
  1379. dma_addr_t buffer_dma, unsigned int sectors)
  1380. {
  1381. struct host_to_dev_fis fis;
  1382. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1383. fis.type = 0x27;
  1384. fis.opts = 1 << 7;
  1385. fis.command = ATA_CMD_READ_LOG_EXT;
  1386. fis.sect_count = sectors & 0xFF;
  1387. fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
  1388. fis.lba_low = page;
  1389. fis.lba_mid = 0;
  1390. fis.device = ATA_DEVICE_OBS;
  1391. memset(buffer, 0, sectors * ATA_SECT_SIZE);
  1392. return mtip_exec_internal_command(port,
  1393. &fis,
  1394. 5,
  1395. buffer_dma,
  1396. sectors * ATA_SECT_SIZE,
  1397. 0,
  1398. GFP_ATOMIC,
  1399. MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
  1400. }
  1401. /*
  1402. * Issue a SMART READ DATA command to the device.
  1403. *
  1404. * @port pointer to the port structure.
  1405. * @buffer pointer to buffer
  1406. * @buffer_dma dma address corresponding to @buffer
  1407. *
  1408. * return value
  1409. * @rv return value from mtip_exec_internal_command()
  1410. */
  1411. static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
  1412. dma_addr_t buffer_dma)
  1413. {
  1414. struct host_to_dev_fis fis;
  1415. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1416. fis.type = 0x27;
  1417. fis.opts = 1 << 7;
  1418. fis.command = ATA_CMD_SMART;
  1419. fis.features = 0xD0;
  1420. fis.sect_count = 1;
  1421. fis.lba_mid = 0x4F;
  1422. fis.lba_hi = 0xC2;
  1423. fis.device = ATA_DEVICE_OBS;
  1424. return mtip_exec_internal_command(port,
  1425. &fis,
  1426. 5,
  1427. buffer_dma,
  1428. ATA_SECT_SIZE,
  1429. 0,
  1430. GFP_ATOMIC,
  1431. 15000);
  1432. }
  1433. /*
  1434. * Get the value of a smart attribute
  1435. *
  1436. * @port pointer to the port structure
  1437. * @id attribute number
  1438. * @attrib pointer to return attrib information corresponding to @id
  1439. *
  1440. * return value
  1441. * -EINVAL NULL buffer passed or unsupported attribute @id.
  1442. * -EPERM Identify data not valid, SMART not supported or not enabled
  1443. */
  1444. static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
  1445. struct smart_attr *attrib)
  1446. {
  1447. int rv, i;
  1448. struct smart_attr *pattr;
  1449. if (!attrib)
  1450. return -EINVAL;
  1451. if (!port->identify_valid) {
  1452. dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
  1453. return -EPERM;
  1454. }
  1455. if (!(port->identify[82] & 0x1)) {
  1456. dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
  1457. return -EPERM;
  1458. }
  1459. if (!(port->identify[85] & 0x1)) {
  1460. dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
  1461. return -EPERM;
  1462. }
  1463. memset(port->smart_buf, 0, ATA_SECT_SIZE);
  1464. rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
  1465. if (rv) {
  1466. dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
  1467. return rv;
  1468. }
  1469. pattr = (struct smart_attr *)(port->smart_buf + 2);
  1470. for (i = 0; i < 29; i++, pattr++)
  1471. if (pattr->attr_id == id) {
  1472. memcpy(attrib, pattr, sizeof(struct smart_attr));
  1473. break;
  1474. }
  1475. if (i == 29) {
  1476. dev_warn(&port->dd->pdev->dev,
  1477. "Query for invalid SMART attribute ID\n");
  1478. rv = -EINVAL;
  1479. }
  1480. return rv;
  1481. }
  1482. /*
  1483. * Get the drive capacity.
  1484. *
  1485. * @dd Pointer to the device data structure.
  1486. * @sectors Pointer to the variable that will receive the sector count.
  1487. *
  1488. * return value
  1489. * 1 Capacity was returned successfully.
  1490. * 0 The identify information is invalid.
  1491. */
  1492. static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
  1493. {
  1494. struct mtip_port *port = dd->port;
  1495. u64 total, raw0, raw1, raw2, raw3;
  1496. raw0 = port->identify[100];
  1497. raw1 = port->identify[101];
  1498. raw2 = port->identify[102];
  1499. raw3 = port->identify[103];
  1500. total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
  1501. *sectors = total;
  1502. return (bool) !!port->identify_valid;
  1503. }
  1504. /*
  1505. * Reset the HBA.
  1506. *
  1507. * Resets the HBA by setting the HBA Reset bit in the Global
  1508. * HBA Control register. After setting the HBA Reset bit the
  1509. * function waits for 1 second before reading the HBA Reset
  1510. * bit to make sure it has cleared. If HBA Reset is not clear
  1511. * an error is returned. Cannot be used in non-blockable
  1512. * context.
  1513. *
  1514. * @dd Pointer to the driver data structure.
  1515. *
  1516. * return value
  1517. * 0 The reset was successful.
  1518. * -1 The HBA Reset bit did not clear.
  1519. */
  1520. static int mtip_hba_reset(struct driver_data *dd)
  1521. {
  1522. mtip_deinit_port(dd->port);
  1523. /* Set the reset bit */
  1524. writel(HOST_RESET, dd->mmio + HOST_CTL);
  1525. /* Flush */
  1526. readl(dd->mmio + HOST_CTL);
  1527. /* Wait for reset to clear */
  1528. ssleep(1);
  1529. /* Check the bit has cleared */
  1530. if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
  1531. dev_err(&dd->pdev->dev,
  1532. "Reset bit did not clear.\n");
  1533. return -1;
  1534. }
  1535. return 0;
  1536. }
  1537. /*
  1538. * Display the identify command data.
  1539. *
  1540. * @port Pointer to the port data structure.
  1541. *
  1542. * return value
  1543. * None
  1544. */
  1545. static void mtip_dump_identify(struct mtip_port *port)
  1546. {
  1547. sector_t sectors;
  1548. unsigned short revid;
  1549. char cbuf[42];
  1550. if (!port->identify_valid)
  1551. return;
  1552. strlcpy(cbuf, (char *)(port->identify+10), 21);
  1553. dev_info(&port->dd->pdev->dev,
  1554. "Serial No.: %s\n", cbuf);
  1555. strlcpy(cbuf, (char *)(port->identify+23), 9);
  1556. dev_info(&port->dd->pdev->dev,
  1557. "Firmware Ver.: %s\n", cbuf);
  1558. strlcpy(cbuf, (char *)(port->identify+27), 41);
  1559. dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
  1560. if (mtip_hw_get_capacity(port->dd, &sectors))
  1561. dev_info(&port->dd->pdev->dev,
  1562. "Capacity: %llu sectors (%llu MB)\n",
  1563. (u64)sectors,
  1564. ((u64)sectors) * ATA_SECT_SIZE >> 20);
  1565. pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
  1566. switch (revid & 0xFF) {
  1567. case 0x1:
  1568. strlcpy(cbuf, "A0", 3);
  1569. break;
  1570. case 0x3:
  1571. strlcpy(cbuf, "A2", 3);
  1572. break;
  1573. default:
  1574. strlcpy(cbuf, "?", 2);
  1575. break;
  1576. }
  1577. dev_info(&port->dd->pdev->dev,
  1578. "Card Type: %s\n", cbuf);
  1579. }
  1580. /*
  1581. * Map the commands scatter list into the command table.
  1582. *
  1583. * @command Pointer to the command.
  1584. * @nents Number of scatter list entries.
  1585. *
  1586. * return value
  1587. * None
  1588. */
  1589. static inline void fill_command_sg(struct driver_data *dd,
  1590. struct mtip_cmd *command,
  1591. int nents)
  1592. {
  1593. int n;
  1594. unsigned int dma_len;
  1595. struct mtip_cmd_sg *command_sg;
  1596. struct scatterlist *sg = command->sg;
  1597. command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
  1598. for (n = 0; n < nents; n++) {
  1599. dma_len = sg_dma_len(sg);
  1600. if (dma_len > 0x400000)
  1601. dev_err(&dd->pdev->dev,
  1602. "DMA segment length truncated\n");
  1603. command_sg->info = __force_bit2int
  1604. cpu_to_le32((dma_len-1) & 0x3FFFFF);
  1605. command_sg->dba = __force_bit2int
  1606. cpu_to_le32(sg_dma_address(sg));
  1607. command_sg->dba_upper = __force_bit2int
  1608. cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
  1609. command_sg++;
  1610. sg++;
  1611. }
  1612. }
  1613. /*
  1614. * @brief Execute a drive command.
  1615. *
  1616. * return value 0 The command completed successfully.
  1617. * return value -1 An error occurred while executing the command.
  1618. */
  1619. static int exec_drive_task(struct mtip_port *port, u8 *command)
  1620. {
  1621. struct host_to_dev_fis fis;
  1622. struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
  1623. /* Build the FIS. */
  1624. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1625. fis.type = 0x27;
  1626. fis.opts = 1 << 7;
  1627. fis.command = command[0];
  1628. fis.features = command[1];
  1629. fis.sect_count = command[2];
  1630. fis.sector = command[3];
  1631. fis.cyl_low = command[4];
  1632. fis.cyl_hi = command[5];
  1633. fis.device = command[6] & ~0x10; /* Clear the dev bit*/
  1634. dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
  1635. __func__,
  1636. command[0],
  1637. command[1],
  1638. command[2],
  1639. command[3],
  1640. command[4],
  1641. command[5],
  1642. command[6]);
  1643. /* Execute the command. */
  1644. if (mtip_exec_internal_command(port,
  1645. &fis,
  1646. 5,
  1647. 0,
  1648. 0,
  1649. 0,
  1650. GFP_KERNEL,
  1651. MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
  1652. return -1;
  1653. }
  1654. command[0] = reply->command; /* Status*/
  1655. command[1] = reply->features; /* Error*/
  1656. command[4] = reply->cyl_low;
  1657. command[5] = reply->cyl_hi;
  1658. dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
  1659. __func__,
  1660. command[0],
  1661. command[1],
  1662. command[4],
  1663. command[5]);
  1664. return 0;
  1665. }
  1666. /*
  1667. * @brief Execute a drive command.
  1668. *
  1669. * @param port Pointer to the port data structure.
  1670. * @param command Pointer to the user specified command parameters.
  1671. * @param user_buffer Pointer to the user space buffer where read sector
  1672. * data should be copied.
  1673. *
  1674. * return value 0 The command completed successfully.
  1675. * return value -EFAULT An error occurred while copying the completion
  1676. * data to the user space buffer.
  1677. * return value -1 An error occurred while executing the command.
  1678. */
  1679. static int exec_drive_command(struct mtip_port *port, u8 *command,
  1680. void __user *user_buffer)
  1681. {
  1682. struct host_to_dev_fis fis;
  1683. struct host_to_dev_fis *reply;
  1684. u8 *buf = NULL;
  1685. dma_addr_t dma_addr = 0;
  1686. int rv = 0, xfer_sz = command[3];
  1687. if (xfer_sz) {
  1688. if (!user_buffer)
  1689. return -EFAULT;
  1690. buf = dmam_alloc_coherent(&port->dd->pdev->dev,
  1691. ATA_SECT_SIZE * xfer_sz,
  1692. &dma_addr,
  1693. GFP_KERNEL);
  1694. if (!buf) {
  1695. dev_err(&port->dd->pdev->dev,
  1696. "Memory allocation failed (%d bytes)\n",
  1697. ATA_SECT_SIZE * xfer_sz);
  1698. return -ENOMEM;
  1699. }
  1700. memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
  1701. }
  1702. /* Build the FIS. */
  1703. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1704. fis.type = 0x27;
  1705. fis.opts = 1 << 7;
  1706. fis.command = command[0];
  1707. fis.features = command[2];
  1708. fis.sect_count = command[3];
  1709. if (fis.command == ATA_CMD_SMART) {
  1710. fis.sector = command[1];
  1711. fis.cyl_low = 0x4F;
  1712. fis.cyl_hi = 0xC2;
  1713. }
  1714. if (xfer_sz)
  1715. reply = (port->rxfis + RX_FIS_PIO_SETUP);
  1716. else
  1717. reply = (port->rxfis + RX_FIS_D2H_REG);
  1718. dbg_printk(MTIP_DRV_NAME
  1719. " %s: User Command: cmd %x, sect %x, "
  1720. "feat %x, sectcnt %x\n",
  1721. __func__,
  1722. command[0],
  1723. command[1],
  1724. command[2],
  1725. command[3]);
  1726. /* Execute the command. */
  1727. if (mtip_exec_internal_command(port,
  1728. &fis,
  1729. 5,
  1730. (xfer_sz ? dma_addr : 0),
  1731. (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
  1732. 0,
  1733. GFP_KERNEL,
  1734. MTIP_IOCTL_COMMAND_TIMEOUT_MS)
  1735. < 0) {
  1736. rv = -EFAULT;
  1737. goto exit_drive_command;
  1738. }
  1739. /* Collect the completion status. */
  1740. command[0] = reply->command; /* Status*/
  1741. command[1] = reply->features; /* Error*/
  1742. command[2] = reply->sect_count;
  1743. dbg_printk(MTIP_DRV_NAME
  1744. " %s: Completion Status: stat %x, "
  1745. "err %x, nsect %x\n",
  1746. __func__,
  1747. command[0],
  1748. command[1],
  1749. command[2]);
  1750. if (xfer_sz) {
  1751. if (copy_to_user(user_buffer,
  1752. buf,
  1753. ATA_SECT_SIZE * command[3])) {
  1754. rv = -EFAULT;
  1755. goto exit_drive_command;
  1756. }
  1757. }
  1758. exit_drive_command:
  1759. if (buf)
  1760. dmam_free_coherent(&port->dd->pdev->dev,
  1761. ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
  1762. return rv;
  1763. }
  1764. /*
  1765. * Indicates whether a command has a single sector payload.
  1766. *
  1767. * @command passed to the device to perform the certain event.
  1768. * @features passed to the device to perform the certain event.
  1769. *
  1770. * return value
  1771. * 1 command is one that always has a single sector payload,
  1772. * regardless of the value in the Sector Count field.
  1773. * 0 otherwise
  1774. *
  1775. */
  1776. static unsigned int implicit_sector(unsigned char command,
  1777. unsigned char features)
  1778. {
  1779. unsigned int rv = 0;
  1780. /* list of commands that have an implicit sector count of 1 */
  1781. switch (command) {
  1782. case ATA_CMD_SEC_SET_PASS:
  1783. case ATA_CMD_SEC_UNLOCK:
  1784. case ATA_CMD_SEC_ERASE_PREP:
  1785. case ATA_CMD_SEC_ERASE_UNIT:
  1786. case ATA_CMD_SEC_FREEZE_LOCK:
  1787. case ATA_CMD_SEC_DISABLE_PASS:
  1788. case ATA_CMD_PMP_READ:
  1789. case ATA_CMD_PMP_WRITE:
  1790. rv = 1;
  1791. break;
  1792. case ATA_CMD_SET_MAX:
  1793. if (features == ATA_SET_MAX_UNLOCK)
  1794. rv = 1;
  1795. break;
  1796. case ATA_CMD_SMART:
  1797. if ((features == ATA_SMART_READ_VALUES) ||
  1798. (features == ATA_SMART_READ_THRESHOLDS))
  1799. rv = 1;
  1800. break;
  1801. case ATA_CMD_CONF_OVERLAY:
  1802. if ((features == ATA_DCO_IDENTIFY) ||
  1803. (features == ATA_DCO_SET))
  1804. rv = 1;
  1805. break;
  1806. }
  1807. return rv;
  1808. }
  1809. static void mtip_set_timeout(struct driver_data *dd,
  1810. struct host_to_dev_fis *fis,
  1811. unsigned int *timeout, u8 erasemode)
  1812. {
  1813. switch (fis->command) {
  1814. case ATA_CMD_DOWNLOAD_MICRO:
  1815. *timeout = 120000; /* 2 minutes */
  1816. break;
  1817. case ATA_CMD_SEC_ERASE_UNIT:
  1818. case 0xFC:
  1819. if (erasemode)
  1820. *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
  1821. else
  1822. *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
  1823. break;
  1824. case ATA_CMD_STANDBYNOW1:
  1825. *timeout = 120000; /* 2 minutes */
  1826. break;
  1827. case 0xF7:
  1828. case 0xFA:
  1829. *timeout = 60000; /* 60 seconds */
  1830. break;
  1831. case ATA_CMD_SMART:
  1832. *timeout = 15000; /* 15 seconds */
  1833. break;
  1834. default:
  1835. *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
  1836. break;
  1837. }
  1838. }
  1839. /*
  1840. * Executes a taskfile
  1841. * See ide_taskfile_ioctl() for derivation
  1842. */
  1843. static int exec_drive_taskfile(struct driver_data *dd,
  1844. void __user *buf,
  1845. ide_task_request_t *req_task,
  1846. int outtotal)
  1847. {
  1848. struct host_to_dev_fis fis;
  1849. struct host_to_dev_fis *reply;
  1850. u8 *outbuf = NULL;
  1851. u8 *inbuf = NULL;
  1852. dma_addr_t outbuf_dma = 0;
  1853. dma_addr_t inbuf_dma = 0;
  1854. dma_addr_t dma_buffer = 0;
  1855. int err = 0;
  1856. unsigned int taskin = 0;
  1857. unsigned int taskout = 0;
  1858. u8 nsect = 0;
  1859. unsigned int timeout;
  1860. unsigned int force_single_sector;
  1861. unsigned int transfer_size;
  1862. unsigned long task_file_data;
  1863. int intotal = outtotal + req_task->out_size;
  1864. int erasemode = 0;
  1865. taskout = req_task->out_size;
  1866. taskin = req_task->in_size;
  1867. /* 130560 = 512 * 0xFF*/
  1868. if (taskin > 130560 || taskout > 130560) {
  1869. err = -EINVAL;
  1870. goto abort;
  1871. }
  1872. if (taskout) {
  1873. outbuf = kzalloc(taskout, GFP_KERNEL);
  1874. if (outbuf == NULL) {
  1875. err = -ENOMEM;
  1876. goto abort;
  1877. }
  1878. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  1879. err = -EFAULT;
  1880. goto abort;
  1881. }
  1882. outbuf_dma = pci_map_single(dd->pdev,
  1883. outbuf,
  1884. taskout,
  1885. DMA_TO_DEVICE);
  1886. if (outbuf_dma == 0) {
  1887. err = -ENOMEM;
  1888. goto abort;
  1889. }
  1890. dma_buffer = outbuf_dma;
  1891. }
  1892. if (taskin) {
  1893. inbuf = kzalloc(taskin, GFP_KERNEL);
  1894. if (inbuf == NULL) {
  1895. err = -ENOMEM;
  1896. goto abort;
  1897. }
  1898. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  1899. err = -EFAULT;
  1900. goto abort;
  1901. }
  1902. inbuf_dma = pci_map_single(dd->pdev,
  1903. inbuf,
  1904. taskin, DMA_FROM_DEVICE);
  1905. if (inbuf_dma == 0) {
  1906. err = -ENOMEM;
  1907. goto abort;
  1908. }
  1909. dma_buffer = inbuf_dma;
  1910. }
  1911. /* only supports PIO and non-data commands from this ioctl. */
  1912. switch (req_task->data_phase) {
  1913. case TASKFILE_OUT:
  1914. nsect = taskout / ATA_SECT_SIZE;
  1915. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1916. break;
  1917. case TASKFILE_IN:
  1918. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1919. break;
  1920. case TASKFILE_NO_DATA:
  1921. reply = (dd->port->rxfis + RX_FIS_D2H_REG);
  1922. break;
  1923. default:
  1924. err = -EINVAL;
  1925. goto abort;
  1926. }
  1927. /* Build the FIS. */
  1928. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1929. fis.type = 0x27;
  1930. fis.opts = 1 << 7;
  1931. fis.command = req_task->io_ports[7];
  1932. fis.features = req_task->io_ports[1];
  1933. fis.sect_count = req_task->io_ports[2];
  1934. fis.lba_low = req_task->io_ports[3];
  1935. fis.lba_mid = req_task->io_ports[4];
  1936. fis.lba_hi = req_task->io_ports[5];
  1937. /* Clear the dev bit*/
  1938. fis.device = req_task->io_ports[6] & ~0x10;
  1939. if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
  1940. req_task->in_flags.all =
  1941. IDE_TASKFILE_STD_IN_FLAGS |
  1942. (IDE_HOB_STD_IN_FLAGS << 8);
  1943. fis.lba_low_ex = req_task->hob_ports[3];
  1944. fis.lba_mid_ex = req_task->hob_ports[4];
  1945. fis.lba_hi_ex = req_task->hob_ports[5];
  1946. fis.features_ex = req_task->hob_ports[1];
  1947. fis.sect_cnt_ex = req_task->hob_ports[2];
  1948. } else {
  1949. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  1950. }
  1951. force_single_sector = implicit_sector(fis.command, fis.features);
  1952. if ((taskin || taskout) && (!fis.sect_count)) {
  1953. if (nsect)
  1954. fis.sect_count = nsect;
  1955. else {
  1956. if (!force_single_sector) {
  1957. dev_warn(&dd->pdev->dev,
  1958. "data movement but "
  1959. "sect_count is 0\n");
  1960. err = -EINVAL;
  1961. goto abort;
  1962. }
  1963. }
  1964. }
  1965. dbg_printk(MTIP_DRV_NAME
  1966. " %s: cmd %x, feat %x, nsect %x,"
  1967. " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
  1968. " head/dev %x\n",
  1969. __func__,
  1970. fis.command,
  1971. fis.features,
  1972. fis.sect_count,
  1973. fis.lba_low,
  1974. fis.lba_mid,
  1975. fis.lba_hi,
  1976. fis.device);
  1977. /* check for erase mode support during secure erase.*/
  1978. if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
  1979. (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
  1980. erasemode = 1;
  1981. }
  1982. mtip_set_timeout(dd, &fis, &timeout, erasemode);
  1983. /* Determine the correct transfer size.*/
  1984. if (force_single_sector)
  1985. transfer_size = ATA_SECT_SIZE;
  1986. else
  1987. transfer_size = ATA_SECT_SIZE * fis.sect_count;
  1988. /* Execute the command.*/
  1989. if (mtip_exec_internal_command(dd->port,
  1990. &fis,
  1991. 5,
  1992. dma_buffer,
  1993. transfer_size,
  1994. 0,
  1995. GFP_KERNEL,
  1996. timeout) < 0) {
  1997. err = -EIO;
  1998. goto abort;
  1999. }
  2000. task_file_data = readl(dd->port->mmio+PORT_TFDATA);
  2001. if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
  2002. reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
  2003. req_task->io_ports[7] = reply->control;
  2004. } else {
  2005. reply = dd->port->rxfis + RX_FIS_D2H_REG;
  2006. req_task->io_ports[7] = reply->command;
  2007. }
  2008. /* reclaim the DMA buffers.*/
  2009. if (inbuf_dma)
  2010. pci_unmap_single(dd->pdev, inbuf_dma,
  2011. taskin, DMA_FROM_DEVICE);
  2012. if (outbuf_dma)
  2013. pci_unmap_single(dd->pdev, outbuf_dma,
  2014. taskout, DMA_TO_DEVICE);
  2015. inbuf_dma = 0;
  2016. outbuf_dma = 0;
  2017. /* return the ATA registers to the caller.*/
  2018. req_task->io_ports[1] = reply->features;
  2019. req_task->io_ports[2] = reply->sect_count;
  2020. req_task->io_ports[3] = reply->lba_low;
  2021. req_task->io_ports[4] = reply->lba_mid;
  2022. req_task->io_ports[5] = reply->lba_hi;
  2023. req_task->io_ports[6] = reply->device;
  2024. if (req_task->out_flags.all & 1) {
  2025. req_task->hob_ports[3] = reply->lba_low_ex;
  2026. req_task->hob_ports[4] = reply->lba_mid_ex;
  2027. req_task->hob_ports[5] = reply->lba_hi_ex;
  2028. req_task->hob_ports[1] = reply->features_ex;
  2029. req_task->hob_ports[2] = reply->sect_cnt_ex;
  2030. }
  2031. dbg_printk(MTIP_DRV_NAME
  2032. " %s: Completion: stat %x,"
  2033. "err %x, sect_cnt %x, lbalo %x,"
  2034. "lbamid %x, lbahi %x, dev %x\n",
  2035. __func__,
  2036. req_task->io_ports[7],
  2037. req_task->io_ports[1],
  2038. req_task->io_ports[2],
  2039. req_task->io_ports[3],
  2040. req_task->io_ports[4],
  2041. req_task->io_ports[5],
  2042. req_task->io_ports[6]);
  2043. if (taskout) {
  2044. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  2045. err = -EFAULT;
  2046. goto abort;
  2047. }
  2048. }
  2049. if (taskin) {
  2050. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  2051. err = -EFAULT;
  2052. goto abort;
  2053. }
  2054. }
  2055. abort:
  2056. if (inbuf_dma)
  2057. pci_unmap_single(dd->pdev, inbuf_dma,
  2058. taskin, DMA_FROM_DEVICE);
  2059. if (outbuf_dma)
  2060. pci_unmap_single(dd->pdev, outbuf_dma,
  2061. taskout, DMA_TO_DEVICE);
  2062. kfree(outbuf);
  2063. kfree(inbuf);
  2064. return err;
  2065. }
  2066. /*
  2067. * Handle IOCTL calls from the Block Layer.
  2068. *
  2069. * This function is called by the Block Layer when it receives an IOCTL
  2070. * command that it does not understand. If the IOCTL command is not supported
  2071. * this function returns -ENOTTY.
  2072. *
  2073. * @dd Pointer to the driver data structure.
  2074. * @cmd IOCTL command passed from the Block Layer.
  2075. * @arg IOCTL argument passed from the Block Layer.
  2076. *
  2077. * return value
  2078. * 0 The IOCTL completed successfully.
  2079. * -ENOTTY The specified command is not supported.
  2080. * -EFAULT An error occurred copying data to a user space buffer.
  2081. * -EIO An error occurred while executing the command.
  2082. */
  2083. static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
  2084. unsigned long arg)
  2085. {
  2086. switch (cmd) {
  2087. case HDIO_GET_IDENTITY:
  2088. {
  2089. if (copy_to_user((void __user *)arg, dd->port->identify,
  2090. sizeof(u16) * ATA_ID_WORDS))
  2091. return -EFAULT;
  2092. break;
  2093. }
  2094. case HDIO_DRIVE_CMD:
  2095. {
  2096. u8 drive_command[4];
  2097. /* Copy the user command info to our buffer. */
  2098. if (copy_from_user(drive_command,
  2099. (void __user *) arg,
  2100. sizeof(drive_command)))
  2101. return -EFAULT;
  2102. /* Execute the drive command. */
  2103. if (exec_drive_command(dd->port,
  2104. drive_command,
  2105. (void __user *) (arg+4)))
  2106. return -EIO;
  2107. /* Copy the status back to the users buffer. */
  2108. if (copy_to_user((void __user *) arg,
  2109. drive_command,
  2110. sizeof(drive_command)))
  2111. return -EFAULT;
  2112. break;
  2113. }
  2114. case HDIO_DRIVE_TASK:
  2115. {
  2116. u8 drive_command[7];
  2117. /* Copy the user command info to our buffer. */
  2118. if (copy_from_user(drive_command,
  2119. (void __user *) arg,
  2120. sizeof(drive_command)))
  2121. return -EFAULT;
  2122. /* Execute the drive command. */
  2123. if (exec_drive_task(dd->port, drive_command))
  2124. return -EIO;
  2125. /* Copy the status back to the users buffer. */
  2126. if (copy_to_user((void __user *) arg,
  2127. drive_command,
  2128. sizeof(drive_command)))
  2129. return -EFAULT;
  2130. break;
  2131. }
  2132. case HDIO_DRIVE_TASKFILE: {
  2133. ide_task_request_t req_task;
  2134. int ret, outtotal;
  2135. if (copy_from_user(&req_task, (void __user *) arg,
  2136. sizeof(req_task)))
  2137. return -EFAULT;
  2138. outtotal = sizeof(req_task);
  2139. ret = exec_drive_taskfile(dd, (void __user *) arg,
  2140. &req_task, outtotal);
  2141. if (copy_to_user((void __user *) arg, &req_task,
  2142. sizeof(req_task)))
  2143. return -EFAULT;
  2144. return ret;
  2145. }
  2146. default:
  2147. return -EINVAL;
  2148. }
  2149. return 0;
  2150. }
  2151. /*
  2152. * Submit an IO to the hw
  2153. *
  2154. * This function is called by the block layer to issue an io
  2155. * to the device. Upon completion, the callback function will
  2156. * be called with the data parameter passed as the callback data.
  2157. *
  2158. * @dd Pointer to the driver data structure.
  2159. * @start First sector to read.
  2160. * @nsect Number of sectors to read.
  2161. * @nents Number of entries in scatter list for the read command.
  2162. * @tag The tag of this read command.
  2163. * @callback Pointer to the function that should be called
  2164. * when the read completes.
  2165. * @data Callback data passed to the callback function
  2166. * when the read completes.
  2167. * @dir Direction (read or write)
  2168. *
  2169. * return value
  2170. * None
  2171. */
  2172. static void mtip_hw_submit_io(struct driver_data *dd, sector_t sector,
  2173. int nsect, int nents, int tag, void *callback,
  2174. void *data, int dir)
  2175. {
  2176. struct host_to_dev_fis *fis;
  2177. struct mtip_port *port = dd->port;
  2178. struct mtip_cmd *command = &port->commands[tag];
  2179. int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2180. u64 start = sector;
  2181. /* Map the scatter list for DMA access */
  2182. nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
  2183. command->scatter_ents = nents;
  2184. /*
  2185. * The number of retries for this command before it is
  2186. * reported as a failure to the upper layers.
  2187. */
  2188. command->retries = MTIP_MAX_RETRIES;
  2189. /* Fill out fis */
  2190. fis = command->command;
  2191. fis->type = 0x27;
  2192. fis->opts = 1 << 7;
  2193. fis->command =
  2194. (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
  2195. fis->lba_low = start & 0xFF;
  2196. fis->lba_mid = (start >> 8) & 0xFF;
  2197. fis->lba_hi = (start >> 16) & 0xFF;
  2198. fis->lba_low_ex = (start >> 24) & 0xFF;
  2199. fis->lba_mid_ex = (start >> 32) & 0xFF;
  2200. fis->lba_hi_ex = (start >> 40) & 0xFF;
  2201. fis->device = 1 << 6;
  2202. fis->features = nsect & 0xFF;
  2203. fis->features_ex = (nsect >> 8) & 0xFF;
  2204. fis->sect_count = ((tag << 3) | (tag >> 5));
  2205. fis->sect_cnt_ex = 0;
  2206. fis->control = 0;
  2207. fis->res2 = 0;
  2208. fis->res3 = 0;
  2209. fill_command_sg(dd, command, nents);
  2210. /* Populate the command header */
  2211. command->command_header->opts =
  2212. __force_bit2int cpu_to_le32(
  2213. (nents << 16) | 5 | AHCI_CMD_PREFETCH);
  2214. command->command_header->byte_count = 0;
  2215. /*
  2216. * Set the completion function and data for the command
  2217. * within this layer.
  2218. */
  2219. command->comp_data = dd;
  2220. command->comp_func = mtip_async_complete;
  2221. command->direction = dma_dir;
  2222. /*
  2223. * Set the completion function and data for the command passed
  2224. * from the upper layer.
  2225. */
  2226. command->async_data = data;
  2227. command->async_callback = callback;
  2228. /*
  2229. * To prevent this command from being issued
  2230. * if an internal command is in progress or error handling is active.
  2231. */
  2232. if (port->flags & MTIP_PF_PAUSE_IO) {
  2233. set_bit(tag, port->cmds_to_issue);
  2234. set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2235. return;
  2236. }
  2237. /* Issue the command to the hardware */
  2238. mtip_issue_ncq_command(port, tag);
  2239. return;
  2240. }
  2241. /*
  2242. * Release a command slot.
  2243. *
  2244. * @dd Pointer to the driver data structure.
  2245. * @tag Slot tag
  2246. *
  2247. * return value
  2248. * None
  2249. */
  2250. static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
  2251. {
  2252. release_slot(dd->port, tag);
  2253. }
  2254. /*
  2255. * Obtain a command slot and return its associated scatter list.
  2256. *
  2257. * @dd Pointer to the driver data structure.
  2258. * @tag Pointer to an int that will receive the allocated command
  2259. * slot tag.
  2260. *
  2261. * return value
  2262. * Pointer to the scatter list for the allocated command slot
  2263. * or NULL if no command slots are available.
  2264. */
  2265. static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
  2266. int *tag)
  2267. {
  2268. /*
  2269. * It is possible that, even with this semaphore, a thread
  2270. * may think that no command slots are available. Therefore, we
  2271. * need to make an attempt to get_slot().
  2272. */
  2273. down(&dd->port->cmd_slot);
  2274. *tag = get_slot(dd->port);
  2275. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  2276. up(&dd->port->cmd_slot);
  2277. return NULL;
  2278. }
  2279. if (unlikely(*tag < 0)) {
  2280. up(&dd->port->cmd_slot);
  2281. return NULL;
  2282. }
  2283. return dd->port->commands[*tag].sg;
  2284. }
  2285. /*
  2286. * Sysfs status dump.
  2287. *
  2288. * @dev Pointer to the device structure, passed by the kernrel.
  2289. * @attr Pointer to the device_attribute structure passed by the kernel.
  2290. * @buf Pointer to the char buffer that will receive the stats info.
  2291. *
  2292. * return value
  2293. * The size, in bytes, of the data copied into buf.
  2294. */
  2295. static ssize_t mtip_hw_show_status(struct device *dev,
  2296. struct device_attribute *attr,
  2297. char *buf)
  2298. {
  2299. struct driver_data *dd = dev_to_disk(dev)->private_data;
  2300. int size = 0;
  2301. if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
  2302. size += sprintf(buf, "%s", "thermal_shutdown\n");
  2303. else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
  2304. size += sprintf(buf, "%s", "write_protect\n");
  2305. else
  2306. size += sprintf(buf, "%s", "online\n");
  2307. return size;
  2308. }
  2309. static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
  2310. static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
  2311. size_t len, loff_t *offset)
  2312. {
  2313. struct driver_data *dd = (struct driver_data *)f->private_data;
  2314. char buf[MTIP_DFS_MAX_BUF_SIZE];
  2315. u32 group_allocated;
  2316. int size = *offset;
  2317. int n;
  2318. if (!len || size)
  2319. return 0;
  2320. size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
  2321. for (n = dd->slot_groups-1; n >= 0; n--)
  2322. size += sprintf(&buf[size], "%08X ",
  2323. readl(dd->port->s_active[n]));
  2324. size += sprintf(&buf[size], "]\n");
  2325. size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
  2326. for (n = dd->slot_groups-1; n >= 0; n--)
  2327. size += sprintf(&buf[size], "%08X ",
  2328. readl(dd->port->cmd_issue[n]));
  2329. size += sprintf(&buf[size], "]\n");
  2330. size += sprintf(&buf[size], "H/ Completed : [ 0x");
  2331. for (n = dd->slot_groups-1; n >= 0; n--)
  2332. size += sprintf(&buf[size], "%08X ",
  2333. readl(dd->port->completed[n]));
  2334. size += sprintf(&buf[size], "]\n");
  2335. size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
  2336. readl(dd->port->mmio + PORT_IRQ_STAT));
  2337. size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
  2338. readl(dd->mmio + HOST_IRQ_STAT));
  2339. size += sprintf(&buf[size], "\n");
  2340. size += sprintf(&buf[size], "L/ Allocated : [ 0x");
  2341. for (n = dd->slot_groups-1; n >= 0; n--) {
  2342. if (sizeof(long) > sizeof(u32))
  2343. group_allocated =
  2344. dd->port->allocated[n/2] >> (32*(n&1));
  2345. else
  2346. group_allocated = dd->port->allocated[n];
  2347. size += sprintf(&buf[size], "%08X ", group_allocated);
  2348. }
  2349. size += sprintf(&buf[size], "]\n");
  2350. size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
  2351. for (n = dd->slot_groups-1; n >= 0; n--) {
  2352. if (sizeof(long) > sizeof(u32))
  2353. group_allocated =
  2354. dd->port->cmds_to_issue[n/2] >> (32*(n&1));
  2355. else
  2356. group_allocated = dd->port->cmds_to_issue[n];
  2357. size += sprintf(&buf[size], "%08X ", group_allocated);
  2358. }
  2359. size += sprintf(&buf[size], "]\n");
  2360. *offset = size <= len ? size : len;
  2361. size = copy_to_user(ubuf, buf, *offset);
  2362. if (size)
  2363. return -EFAULT;
  2364. return *offset;
  2365. }
  2366. static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
  2367. size_t len, loff_t *offset)
  2368. {
  2369. struct driver_data *dd = (struct driver_data *)f->private_data;
  2370. char buf[MTIP_DFS_MAX_BUF_SIZE];
  2371. int size = *offset;
  2372. if (!len || size)
  2373. return 0;
  2374. size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
  2375. dd->port->flags);
  2376. size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
  2377. dd->dd_flag);
  2378. *offset = size <= len ? size : len;
  2379. size = copy_to_user(ubuf, buf, *offset);
  2380. if (size)
  2381. return -EFAULT;
  2382. return *offset;
  2383. }
  2384. static const struct file_operations mtip_regs_fops = {
  2385. .owner = THIS_MODULE,
  2386. .open = simple_open,
  2387. .read = mtip_hw_read_registers,
  2388. .llseek = no_llseek,
  2389. };
  2390. static const struct file_operations mtip_flags_fops = {
  2391. .owner = THIS_MODULE,
  2392. .open = simple_open,
  2393. .read = mtip_hw_read_flags,
  2394. .llseek = no_llseek,
  2395. };
  2396. /*
  2397. * Create the sysfs related attributes.
  2398. *
  2399. * @dd Pointer to the driver data structure.
  2400. * @kobj Pointer to the kobj for the block device.
  2401. *
  2402. * return value
  2403. * 0 Operation completed successfully.
  2404. * -EINVAL Invalid parameter.
  2405. */
  2406. static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
  2407. {
  2408. if (!kobj || !dd)
  2409. return -EINVAL;
  2410. if (sysfs_create_file(kobj, &dev_attr_status.attr))
  2411. dev_warn(&dd->pdev->dev,
  2412. "Error creating 'status' sysfs entry\n");
  2413. return 0;
  2414. }
  2415. /*
  2416. * Remove the sysfs related attributes.
  2417. *
  2418. * @dd Pointer to the driver data structure.
  2419. * @kobj Pointer to the kobj for the block device.
  2420. *
  2421. * return value
  2422. * 0 Operation completed successfully.
  2423. * -EINVAL Invalid parameter.
  2424. */
  2425. static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
  2426. {
  2427. if (!kobj || !dd)
  2428. return -EINVAL;
  2429. sysfs_remove_file(kobj, &dev_attr_status.attr);
  2430. return 0;
  2431. }
  2432. static int mtip_hw_debugfs_init(struct driver_data *dd)
  2433. {
  2434. if (!dfs_parent)
  2435. return -1;
  2436. dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
  2437. if (IS_ERR_OR_NULL(dd->dfs_node)) {
  2438. dev_warn(&dd->pdev->dev,
  2439. "Error creating node %s under debugfs\n",
  2440. dd->disk->disk_name);
  2441. dd->dfs_node = NULL;
  2442. return -1;
  2443. }
  2444. debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
  2445. &mtip_flags_fops);
  2446. debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
  2447. &mtip_regs_fops);
  2448. return 0;
  2449. }
  2450. static void mtip_hw_debugfs_exit(struct driver_data *dd)
  2451. {
  2452. debugfs_remove_recursive(dd->dfs_node);
  2453. }
  2454. /*
  2455. * Perform any init/resume time hardware setup
  2456. *
  2457. * @dd Pointer to the driver data structure.
  2458. *
  2459. * return value
  2460. * None
  2461. */
  2462. static inline void hba_setup(struct driver_data *dd)
  2463. {
  2464. u32 hwdata;
  2465. hwdata = readl(dd->mmio + HOST_HSORG);
  2466. /* interrupt bug workaround: use only 1 IS bit.*/
  2467. writel(hwdata |
  2468. HSORG_DISABLE_SLOTGRP_INTR |
  2469. HSORG_DISABLE_SLOTGRP_PXIS,
  2470. dd->mmio + HOST_HSORG);
  2471. }
  2472. /*
  2473. * Detect the details of the product, and store anything needed
  2474. * into the driver data structure. This includes product type and
  2475. * version and number of slot groups.
  2476. *
  2477. * @dd Pointer to the driver data structure.
  2478. *
  2479. * return value
  2480. * None
  2481. */
  2482. static void mtip_detect_product(struct driver_data *dd)
  2483. {
  2484. u32 hwdata;
  2485. unsigned int rev, slotgroups;
  2486. /*
  2487. * HBA base + 0xFC [15:0] - vendor-specific hardware interface
  2488. * info register:
  2489. * [15:8] hardware/software interface rev#
  2490. * [ 3] asic-style interface
  2491. * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
  2492. */
  2493. hwdata = readl(dd->mmio + HOST_HSORG);
  2494. dd->product_type = MTIP_PRODUCT_UNKNOWN;
  2495. dd->slot_groups = 1;
  2496. if (hwdata & 0x8) {
  2497. dd->product_type = MTIP_PRODUCT_ASICFPGA;
  2498. rev = (hwdata & HSORG_HWREV) >> 8;
  2499. slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
  2500. dev_info(&dd->pdev->dev,
  2501. "ASIC-FPGA design, HS rev 0x%x, "
  2502. "%i slot groups [%i slots]\n",
  2503. rev,
  2504. slotgroups,
  2505. slotgroups * 32);
  2506. if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
  2507. dev_warn(&dd->pdev->dev,
  2508. "Warning: driver only supports "
  2509. "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
  2510. slotgroups = MTIP_MAX_SLOT_GROUPS;
  2511. }
  2512. dd->slot_groups = slotgroups;
  2513. return;
  2514. }
  2515. dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
  2516. }
  2517. /*
  2518. * Blocking wait for FTL rebuild to complete
  2519. *
  2520. * @dd Pointer to the DRIVER_DATA structure.
  2521. *
  2522. * return value
  2523. * 0 FTL rebuild completed successfully
  2524. * -EFAULT FTL rebuild error/timeout/interruption
  2525. */
  2526. static int mtip_ftl_rebuild_poll(struct driver_data *dd)
  2527. {
  2528. unsigned long timeout, cnt = 0, start;
  2529. dev_warn(&dd->pdev->dev,
  2530. "FTL rebuild in progress. Polling for completion.\n");
  2531. start = jiffies;
  2532. timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
  2533. do {
  2534. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2535. &dd->dd_flag)))
  2536. return -EFAULT;
  2537. if (mtip_check_surprise_removal(dd->pdev))
  2538. return -EFAULT;
  2539. if (mtip_get_identify(dd->port, NULL) < 0)
  2540. return -EFAULT;
  2541. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2542. MTIP_FTL_REBUILD_MAGIC) {
  2543. ssleep(1);
  2544. /* Print message every 3 minutes */
  2545. if (cnt++ >= 180) {
  2546. dev_warn(&dd->pdev->dev,
  2547. "FTL rebuild in progress (%d secs).\n",
  2548. jiffies_to_msecs(jiffies - start) / 1000);
  2549. cnt = 0;
  2550. }
  2551. } else {
  2552. dev_warn(&dd->pdev->dev,
  2553. "FTL rebuild complete (%d secs).\n",
  2554. jiffies_to_msecs(jiffies - start) / 1000);
  2555. mtip_block_initialize(dd);
  2556. return 0;
  2557. }
  2558. ssleep(10);
  2559. } while (time_before(jiffies, timeout));
  2560. /* Check for timeout */
  2561. dev_err(&dd->pdev->dev,
  2562. "Timed out waiting for FTL rebuild to complete (%d secs).\n",
  2563. jiffies_to_msecs(jiffies - start) / 1000);
  2564. return -EFAULT;
  2565. }
  2566. /*
  2567. * service thread to issue queued commands
  2568. *
  2569. * @data Pointer to the driver data structure.
  2570. *
  2571. * return value
  2572. * 0
  2573. */
  2574. static int mtip_service_thread(void *data)
  2575. {
  2576. struct driver_data *dd = (struct driver_data *)data;
  2577. unsigned long slot, slot_start, slot_wrap;
  2578. unsigned int num_cmd_slots = dd->slot_groups * 32;
  2579. struct mtip_port *port = dd->port;
  2580. while (1) {
  2581. /*
  2582. * the condition is to check neither an internal command is
  2583. * is in progress nor error handling is active
  2584. */
  2585. wait_event_interruptible(port->svc_wait, (port->flags) &&
  2586. !(port->flags & MTIP_PF_PAUSE_IO));
  2587. if (kthread_should_stop())
  2588. break;
  2589. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2590. &dd->dd_flag)))
  2591. break;
  2592. set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2593. if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
  2594. slot = 1;
  2595. /* used to restrict the loop to one iteration */
  2596. slot_start = num_cmd_slots;
  2597. slot_wrap = 0;
  2598. while (1) {
  2599. slot = find_next_bit(port->cmds_to_issue,
  2600. num_cmd_slots, slot);
  2601. if (slot_wrap == 1) {
  2602. if ((slot_start >= slot) ||
  2603. (slot >= num_cmd_slots))
  2604. break;
  2605. }
  2606. if (unlikely(slot_start == num_cmd_slots))
  2607. slot_start = slot;
  2608. if (unlikely(slot == num_cmd_slots)) {
  2609. slot = 1;
  2610. slot_wrap = 1;
  2611. continue;
  2612. }
  2613. /* Issue the command to the hardware */
  2614. mtip_issue_ncq_command(port, slot);
  2615. clear_bit(slot, port->cmds_to_issue);
  2616. }
  2617. clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2618. } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
  2619. if (!mtip_ftl_rebuild_poll(dd))
  2620. set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
  2621. &dd->dd_flag);
  2622. clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
  2623. }
  2624. clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2625. if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
  2626. break;
  2627. }
  2628. return 0;
  2629. }
  2630. /*
  2631. * Called once for each card.
  2632. *
  2633. * @dd Pointer to the driver data structure.
  2634. *
  2635. * return value
  2636. * 0 on success, else an error code.
  2637. */
  2638. static int mtip_hw_init(struct driver_data *dd)
  2639. {
  2640. int i;
  2641. int rv;
  2642. unsigned int num_command_slots;
  2643. unsigned long timeout, timetaken;
  2644. unsigned char *buf;
  2645. struct smart_attr attr242;
  2646. dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
  2647. mtip_detect_product(dd);
  2648. if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
  2649. rv = -EIO;
  2650. goto out1;
  2651. }
  2652. num_command_slots = dd->slot_groups * 32;
  2653. hba_setup(dd);
  2654. dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
  2655. dd->numa_node);
  2656. if (!dd->port) {
  2657. dev_err(&dd->pdev->dev,
  2658. "Memory allocation: port structure\n");
  2659. return -ENOMEM;
  2660. }
  2661. /* Continue workqueue setup */
  2662. for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
  2663. dd->work[i].port = dd->port;
  2664. /* Counting semaphore to track command slot usage */
  2665. sema_init(&dd->port->cmd_slot, num_command_slots - 1);
  2666. /* Spinlock to prevent concurrent issue */
  2667. for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
  2668. spin_lock_init(&dd->port->cmd_issue_lock[i]);
  2669. /* Set the port mmio base address. */
  2670. dd->port->mmio = dd->mmio + PORT_OFFSET;
  2671. dd->port->dd = dd;
  2672. /* Allocate memory for the command list. */
  2673. dd->port->command_list =
  2674. dmam_alloc_coherent(&dd->pdev->dev,
  2675. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2676. &dd->port->command_list_dma,
  2677. GFP_KERNEL);
  2678. if (!dd->port->command_list) {
  2679. dev_err(&dd->pdev->dev,
  2680. "Memory allocation: command list\n");
  2681. rv = -ENOMEM;
  2682. goto out1;
  2683. }
  2684. /* Clear the memory we have allocated. */
  2685. memset(dd->port->command_list,
  2686. 0,
  2687. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
  2688. /* Setup the addresse of the RX FIS. */
  2689. dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
  2690. dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
  2691. /* Setup the address of the command tables. */
  2692. dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
  2693. dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
  2694. /* Setup the address of the identify data. */
  2695. dd->port->identify = dd->port->command_table +
  2696. HW_CMD_TBL_AR_SZ;
  2697. dd->port->identify_dma = dd->port->command_tbl_dma +
  2698. HW_CMD_TBL_AR_SZ;
  2699. /* Setup the address of the sector buffer - for some non-ncq cmds */
  2700. dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
  2701. dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
  2702. /* Setup the address of the log buf - for read log command */
  2703. dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
  2704. dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
  2705. /* Setup the address of the smart buf - for smart read data command */
  2706. dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
  2707. dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
  2708. /* Point the command headers at the command tables. */
  2709. for (i = 0; i < num_command_slots; i++) {
  2710. dd->port->commands[i].command_header =
  2711. dd->port->command_list +
  2712. (sizeof(struct mtip_cmd_hdr) * i);
  2713. dd->port->commands[i].command_header_dma =
  2714. dd->port->command_list_dma +
  2715. (sizeof(struct mtip_cmd_hdr) * i);
  2716. dd->port->commands[i].command =
  2717. dd->port->command_table + (HW_CMD_TBL_SZ * i);
  2718. dd->port->commands[i].command_dma =
  2719. dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
  2720. if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
  2721. dd->port->commands[i].command_header->ctbau =
  2722. __force_bit2int cpu_to_le32(
  2723. (dd->port->commands[i].command_dma >> 16) >> 16);
  2724. dd->port->commands[i].command_header->ctba =
  2725. __force_bit2int cpu_to_le32(
  2726. dd->port->commands[i].command_dma & 0xFFFFFFFF);
  2727. /*
  2728. * If this is not done, a bug is reported by the stock
  2729. * FC11 i386. Due to the fact that it has lots of kernel
  2730. * debugging enabled.
  2731. */
  2732. sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
  2733. /* Mark all commands as currently inactive.*/
  2734. atomic_set(&dd->port->commands[i].active, 0);
  2735. }
  2736. /* Setup the pointers to the extended s_active and CI registers. */
  2737. for (i = 0; i < dd->slot_groups; i++) {
  2738. dd->port->s_active[i] =
  2739. dd->port->mmio + i*0x80 + PORT_SCR_ACT;
  2740. dd->port->cmd_issue[i] =
  2741. dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
  2742. dd->port->completed[i] =
  2743. dd->port->mmio + i*0x80 + PORT_SDBV;
  2744. }
  2745. timetaken = jiffies;
  2746. timeout = jiffies + msecs_to_jiffies(30000);
  2747. while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
  2748. time_before(jiffies, timeout)) {
  2749. mdelay(100);
  2750. }
  2751. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  2752. timetaken = jiffies - timetaken;
  2753. dev_warn(&dd->pdev->dev,
  2754. "Surprise removal detected at %u ms\n",
  2755. jiffies_to_msecs(timetaken));
  2756. rv = -ENODEV;
  2757. goto out2 ;
  2758. }
  2759. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  2760. timetaken = jiffies - timetaken;
  2761. dev_warn(&dd->pdev->dev,
  2762. "Removal detected at %u ms\n",
  2763. jiffies_to_msecs(timetaken));
  2764. rv = -EFAULT;
  2765. goto out2;
  2766. }
  2767. /* Conditionally reset the HBA. */
  2768. if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
  2769. if (mtip_hba_reset(dd) < 0) {
  2770. dev_err(&dd->pdev->dev,
  2771. "Card did not reset within timeout\n");
  2772. rv = -EIO;
  2773. goto out2;
  2774. }
  2775. } else {
  2776. /* Clear any pending interrupts on the HBA */
  2777. writel(readl(dd->mmio + HOST_IRQ_STAT),
  2778. dd->mmio + HOST_IRQ_STAT);
  2779. }
  2780. mtip_init_port(dd->port);
  2781. mtip_start_port(dd->port);
  2782. /* Setup the ISR and enable interrupts. */
  2783. rv = devm_request_irq(&dd->pdev->dev,
  2784. dd->pdev->irq,
  2785. mtip_irq_handler,
  2786. IRQF_SHARED,
  2787. dev_driver_string(&dd->pdev->dev),
  2788. dd);
  2789. if (rv) {
  2790. dev_err(&dd->pdev->dev,
  2791. "Unable to allocate IRQ %d\n", dd->pdev->irq);
  2792. goto out2;
  2793. }
  2794. irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
  2795. /* Enable interrupts on the HBA. */
  2796. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2797. dd->mmio + HOST_CTL);
  2798. init_timer(&dd->port->cmd_timer);
  2799. init_waitqueue_head(&dd->port->svc_wait);
  2800. dd->port->cmd_timer.data = (unsigned long int) dd->port;
  2801. dd->port->cmd_timer.function = mtip_timeout_function;
  2802. mod_timer(&dd->port->cmd_timer,
  2803. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  2804. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
  2805. rv = -EFAULT;
  2806. goto out3;
  2807. }
  2808. if (mtip_get_identify(dd->port, NULL) < 0) {
  2809. rv = -EFAULT;
  2810. goto out3;
  2811. }
  2812. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2813. MTIP_FTL_REBUILD_MAGIC) {
  2814. set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
  2815. return MTIP_FTL_REBUILD_MAGIC;
  2816. }
  2817. mtip_dump_identify(dd->port);
  2818. /* check write protect, over temp and rebuild statuses */
  2819. rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
  2820. dd->port->log_buf,
  2821. dd->port->log_buf_dma, 1);
  2822. if (rv) {
  2823. dev_warn(&dd->pdev->dev,
  2824. "Error in READ LOG EXT (10h) command\n");
  2825. /* non-critical error, don't fail the load */
  2826. } else {
  2827. buf = (unsigned char *)dd->port->log_buf;
  2828. if (buf[259] & 0x1) {
  2829. dev_info(&dd->pdev->dev,
  2830. "Write protect bit is set.\n");
  2831. set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
  2832. }
  2833. if (buf[288] == 0xF7) {
  2834. dev_info(&dd->pdev->dev,
  2835. "Exceeded Tmax, drive in thermal shutdown.\n");
  2836. set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
  2837. }
  2838. if (buf[288] == 0xBF) {
  2839. dev_info(&dd->pdev->dev,
  2840. "Drive indicates rebuild has failed.\n");
  2841. /* TODO */
  2842. }
  2843. }
  2844. /* get write protect progess */
  2845. memset(&attr242, 0, sizeof(struct smart_attr));
  2846. if (mtip_get_smart_attr(dd->port, 242, &attr242))
  2847. dev_warn(&dd->pdev->dev,
  2848. "Unable to check write protect progress\n");
  2849. else
  2850. dev_info(&dd->pdev->dev,
  2851. "Write protect progress: %u%% (%u blocks)\n",
  2852. attr242.cur, le32_to_cpu(attr242.data));
  2853. return rv;
  2854. out3:
  2855. del_timer_sync(&dd->port->cmd_timer);
  2856. /* Disable interrupts on the HBA. */
  2857. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2858. dd->mmio + HOST_CTL);
  2859. /* Release the IRQ. */
  2860. irq_set_affinity_hint(dd->pdev->irq, NULL);
  2861. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2862. out2:
  2863. mtip_deinit_port(dd->port);
  2864. /* Free the command/command header memory. */
  2865. dmam_free_coherent(&dd->pdev->dev,
  2866. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2867. dd->port->command_list,
  2868. dd->port->command_list_dma);
  2869. out1:
  2870. /* Free the memory allocated for the for structure. */
  2871. kfree(dd->port);
  2872. return rv;
  2873. }
  2874. /*
  2875. * Called to deinitialize an interface.
  2876. *
  2877. * @dd Pointer to the driver data structure.
  2878. *
  2879. * return value
  2880. * 0
  2881. */
  2882. static int mtip_hw_exit(struct driver_data *dd)
  2883. {
  2884. /*
  2885. * Send standby immediate (E0h) to the drive so that it
  2886. * saves its state.
  2887. */
  2888. if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
  2889. if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
  2890. if (mtip_standby_immediate(dd->port))
  2891. dev_warn(&dd->pdev->dev,
  2892. "STANDBY IMMEDIATE failed\n");
  2893. /* de-initialize the port. */
  2894. mtip_deinit_port(dd->port);
  2895. /* Disable interrupts on the HBA. */
  2896. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2897. dd->mmio + HOST_CTL);
  2898. }
  2899. del_timer_sync(&dd->port->cmd_timer);
  2900. /* Release the IRQ. */
  2901. irq_set_affinity_hint(dd->pdev->irq, NULL);
  2902. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2903. /* Free the command/command header memory. */
  2904. dmam_free_coherent(&dd->pdev->dev,
  2905. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2906. dd->port->command_list,
  2907. dd->port->command_list_dma);
  2908. /* Free the memory allocated for the for structure. */
  2909. kfree(dd->port);
  2910. return 0;
  2911. }
  2912. /*
  2913. * Issue a Standby Immediate command to the device.
  2914. *
  2915. * This function is called by the Block Layer just before the
  2916. * system powers off during a shutdown.
  2917. *
  2918. * @dd Pointer to the driver data structure.
  2919. *
  2920. * return value
  2921. * 0
  2922. */
  2923. static int mtip_hw_shutdown(struct driver_data *dd)
  2924. {
  2925. /*
  2926. * Send standby immediate (E0h) to the drive so that it
  2927. * saves its state.
  2928. */
  2929. mtip_standby_immediate(dd->port);
  2930. return 0;
  2931. }
  2932. /*
  2933. * Suspend function
  2934. *
  2935. * This function is called by the Block Layer just before the
  2936. * system hibernates.
  2937. *
  2938. * @dd Pointer to the driver data structure.
  2939. *
  2940. * return value
  2941. * 0 Suspend was successful
  2942. * -EFAULT Suspend was not successful
  2943. */
  2944. static int mtip_hw_suspend(struct driver_data *dd)
  2945. {
  2946. /*
  2947. * Send standby immediate (E0h) to the drive
  2948. * so that it saves its state.
  2949. */
  2950. if (mtip_standby_immediate(dd->port) != 0) {
  2951. dev_err(&dd->pdev->dev,
  2952. "Failed standby-immediate command\n");
  2953. return -EFAULT;
  2954. }
  2955. /* Disable interrupts on the HBA.*/
  2956. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2957. dd->mmio + HOST_CTL);
  2958. mtip_deinit_port(dd->port);
  2959. return 0;
  2960. }
  2961. /*
  2962. * Resume function
  2963. *
  2964. * This function is called by the Block Layer as the
  2965. * system resumes.
  2966. *
  2967. * @dd Pointer to the driver data structure.
  2968. *
  2969. * return value
  2970. * 0 Resume was successful
  2971. * -EFAULT Resume was not successful
  2972. */
  2973. static int mtip_hw_resume(struct driver_data *dd)
  2974. {
  2975. /* Perform any needed hardware setup steps */
  2976. hba_setup(dd);
  2977. /* Reset the HBA */
  2978. if (mtip_hba_reset(dd) != 0) {
  2979. dev_err(&dd->pdev->dev,
  2980. "Unable to reset the HBA\n");
  2981. return -EFAULT;
  2982. }
  2983. /*
  2984. * Enable the port, DMA engine, and FIS reception specific
  2985. * h/w in controller.
  2986. */
  2987. mtip_init_port(dd->port);
  2988. mtip_start_port(dd->port);
  2989. /* Enable interrupts on the HBA.*/
  2990. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2991. dd->mmio + HOST_CTL);
  2992. return 0;
  2993. }
  2994. /*
  2995. * Helper function for reusing disk name
  2996. * upon hot insertion.
  2997. */
  2998. static int rssd_disk_name_format(char *prefix,
  2999. int index,
  3000. char *buf,
  3001. int buflen)
  3002. {
  3003. const int base = 'z' - 'a' + 1;
  3004. char *begin = buf + strlen(prefix);
  3005. char *end = buf + buflen;
  3006. char *p;
  3007. int unit;
  3008. p = end - 1;
  3009. *p = '\0';
  3010. unit = base;
  3011. do {
  3012. if (p == begin)
  3013. return -EINVAL;
  3014. *--p = 'a' + (index % unit);
  3015. index = (index / unit) - 1;
  3016. } while (index >= 0);
  3017. memmove(begin, p, end - p);
  3018. memcpy(buf, prefix, strlen(prefix));
  3019. return 0;
  3020. }
  3021. /*
  3022. * Block layer IOCTL handler.
  3023. *
  3024. * @dev Pointer to the block_device structure.
  3025. * @mode ignored
  3026. * @cmd IOCTL command passed from the user application.
  3027. * @arg Argument passed from the user application.
  3028. *
  3029. * return value
  3030. * 0 IOCTL completed successfully.
  3031. * -ENOTTY IOCTL not supported or invalid driver data
  3032. * structure pointer.
  3033. */
  3034. static int mtip_block_ioctl(struct block_device *dev,
  3035. fmode_t mode,
  3036. unsigned cmd,
  3037. unsigned long arg)
  3038. {
  3039. struct driver_data *dd = dev->bd_disk->private_data;
  3040. if (!capable(CAP_SYS_ADMIN))
  3041. return -EACCES;
  3042. if (!dd)
  3043. return -ENOTTY;
  3044. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  3045. return -ENOTTY;
  3046. switch (cmd) {
  3047. case BLKFLSBUF:
  3048. return -ENOTTY;
  3049. default:
  3050. return mtip_hw_ioctl(dd, cmd, arg);
  3051. }
  3052. }
  3053. #ifdef CONFIG_COMPAT
  3054. /*
  3055. * Block layer compat IOCTL handler.
  3056. *
  3057. * @dev Pointer to the block_device structure.
  3058. * @mode ignored
  3059. * @cmd IOCTL command passed from the user application.
  3060. * @arg Argument passed from the user application.
  3061. *
  3062. * return value
  3063. * 0 IOCTL completed successfully.
  3064. * -ENOTTY IOCTL not supported or invalid driver data
  3065. * structure pointer.
  3066. */
  3067. static int mtip_block_compat_ioctl(struct block_device *dev,
  3068. fmode_t mode,
  3069. unsigned cmd,
  3070. unsigned long arg)
  3071. {
  3072. struct driver_data *dd = dev->bd_disk->private_data;
  3073. if (!capable(CAP_SYS_ADMIN))
  3074. return -EACCES;
  3075. if (!dd)
  3076. return -ENOTTY;
  3077. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  3078. return -ENOTTY;
  3079. switch (cmd) {
  3080. case BLKFLSBUF:
  3081. return -ENOTTY;
  3082. case HDIO_DRIVE_TASKFILE: {
  3083. struct mtip_compat_ide_task_request_s __user *compat_req_task;
  3084. ide_task_request_t req_task;
  3085. int compat_tasksize, outtotal, ret;
  3086. compat_tasksize =
  3087. sizeof(struct mtip_compat_ide_task_request_s);
  3088. compat_req_task =
  3089. (struct mtip_compat_ide_task_request_s __user *) arg;
  3090. if (copy_from_user(&req_task, (void __user *) arg,
  3091. compat_tasksize - (2 * sizeof(compat_long_t))))
  3092. return -EFAULT;
  3093. if (get_user(req_task.out_size, &compat_req_task->out_size))
  3094. return -EFAULT;
  3095. if (get_user(req_task.in_size, &compat_req_task->in_size))
  3096. return -EFAULT;
  3097. outtotal = sizeof(struct mtip_compat_ide_task_request_s);
  3098. ret = exec_drive_taskfile(dd, (void __user *) arg,
  3099. &req_task, outtotal);
  3100. if (copy_to_user((void __user *) arg, &req_task,
  3101. compat_tasksize -
  3102. (2 * sizeof(compat_long_t))))
  3103. return -EFAULT;
  3104. if (put_user(req_task.out_size, &compat_req_task->out_size))
  3105. return -EFAULT;
  3106. if (put_user(req_task.in_size, &compat_req_task->in_size))
  3107. return -EFAULT;
  3108. return ret;
  3109. }
  3110. default:
  3111. return mtip_hw_ioctl(dd, cmd, arg);
  3112. }
  3113. }
  3114. #endif
  3115. /*
  3116. * Obtain the geometry of the device.
  3117. *
  3118. * You may think that this function is obsolete, but some applications,
  3119. * fdisk for example still used CHS values. This function describes the
  3120. * device as having 224 heads and 56 sectors per cylinder. These values are
  3121. * chosen so that each cylinder is aligned on a 4KB boundary. Since a
  3122. * partition is described in terms of a start and end cylinder this means
  3123. * that each partition is also 4KB aligned. Non-aligned partitions adversely
  3124. * affects performance.
  3125. *
  3126. * @dev Pointer to the block_device strucutre.
  3127. * @geo Pointer to a hd_geometry structure.
  3128. *
  3129. * return value
  3130. * 0 Operation completed successfully.
  3131. * -ENOTTY An error occurred while reading the drive capacity.
  3132. */
  3133. static int mtip_block_getgeo(struct block_device *dev,
  3134. struct hd_geometry *geo)
  3135. {
  3136. struct driver_data *dd = dev->bd_disk->private_data;
  3137. sector_t capacity;
  3138. if (!dd)
  3139. return -ENOTTY;
  3140. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3141. dev_warn(&dd->pdev->dev,
  3142. "Could not get drive capacity.\n");
  3143. return -ENOTTY;
  3144. }
  3145. geo->heads = 224;
  3146. geo->sectors = 56;
  3147. sector_div(capacity, (geo->heads * geo->sectors));
  3148. geo->cylinders = capacity;
  3149. return 0;
  3150. }
  3151. /*
  3152. * Block device operation function.
  3153. *
  3154. * This structure contains pointers to the functions required by the block
  3155. * layer.
  3156. */
  3157. static const struct block_device_operations mtip_block_ops = {
  3158. .ioctl = mtip_block_ioctl,
  3159. #ifdef CONFIG_COMPAT
  3160. .compat_ioctl = mtip_block_compat_ioctl,
  3161. #endif
  3162. .getgeo = mtip_block_getgeo,
  3163. .owner = THIS_MODULE
  3164. };
  3165. /*
  3166. * Block layer make request function.
  3167. *
  3168. * This function is called by the kernel to process a BIO for
  3169. * the P320 device.
  3170. *
  3171. * @queue Pointer to the request queue. Unused other than to obtain
  3172. * the driver data structure.
  3173. * @bio Pointer to the BIO.
  3174. *
  3175. */
  3176. static void mtip_make_request(struct request_queue *queue, struct bio *bio)
  3177. {
  3178. struct driver_data *dd = queue->queuedata;
  3179. struct scatterlist *sg;
  3180. struct bio_vec *bvec;
  3181. int nents = 0;
  3182. int tag = 0;
  3183. if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
  3184. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  3185. &dd->dd_flag))) {
  3186. bio_endio(bio, -ENXIO);
  3187. return;
  3188. }
  3189. if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
  3190. bio_endio(bio, -ENODATA);
  3191. return;
  3192. }
  3193. if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
  3194. &dd->dd_flag) &&
  3195. bio_data_dir(bio))) {
  3196. bio_endio(bio, -ENODATA);
  3197. return;
  3198. }
  3199. if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
  3200. bio_endio(bio, -ENODATA);
  3201. return;
  3202. }
  3203. }
  3204. if (unlikely(!bio_has_data(bio))) {
  3205. blk_queue_flush(queue, 0);
  3206. bio_endio(bio, 0);
  3207. return;
  3208. }
  3209. sg = mtip_hw_get_scatterlist(dd, &tag);
  3210. if (likely(sg != NULL)) {
  3211. blk_queue_bounce(queue, &bio);
  3212. if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
  3213. dev_warn(&dd->pdev->dev,
  3214. "Maximum number of SGL entries exceeded\n");
  3215. bio_io_error(bio);
  3216. mtip_hw_release_scatterlist(dd, tag);
  3217. return;
  3218. }
  3219. /* Create the scatter list for this bio. */
  3220. bio_for_each_segment(bvec, bio, nents) {
  3221. sg_set_page(&sg[nents],
  3222. bvec->bv_page,
  3223. bvec->bv_len,
  3224. bvec->bv_offset);
  3225. }
  3226. /* Issue the read/write. */
  3227. mtip_hw_submit_io(dd,
  3228. bio->bi_sector,
  3229. bio_sectors(bio),
  3230. nents,
  3231. tag,
  3232. bio_endio,
  3233. bio,
  3234. bio_data_dir(bio));
  3235. } else
  3236. bio_io_error(bio);
  3237. }
  3238. /*
  3239. * Block layer initialization function.
  3240. *
  3241. * This function is called once by the PCI layer for each P320
  3242. * device that is connected to the system.
  3243. *
  3244. * @dd Pointer to the driver data structure.
  3245. *
  3246. * return value
  3247. * 0 on success else an error code.
  3248. */
  3249. static int mtip_block_initialize(struct driver_data *dd)
  3250. {
  3251. int rv = 0, wait_for_rebuild = 0;
  3252. sector_t capacity;
  3253. unsigned int index = 0;
  3254. struct kobject *kobj;
  3255. unsigned char thd_name[16];
  3256. if (dd->disk)
  3257. goto skip_create_disk; /* hw init done, before rebuild */
  3258. /* Initialize the protocol layer. */
  3259. wait_for_rebuild = mtip_hw_init(dd);
  3260. if (wait_for_rebuild < 0) {
  3261. dev_err(&dd->pdev->dev,
  3262. "Protocol layer initialization failed\n");
  3263. rv = -EINVAL;
  3264. goto protocol_init_error;
  3265. }
  3266. dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
  3267. if (dd->disk == NULL) {
  3268. dev_err(&dd->pdev->dev,
  3269. "Unable to allocate gendisk structure\n");
  3270. rv = -EINVAL;
  3271. goto alloc_disk_error;
  3272. }
  3273. /* Generate the disk name, implemented same as in sd.c */
  3274. do {
  3275. if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
  3276. goto ida_get_error;
  3277. spin_lock(&rssd_index_lock);
  3278. rv = ida_get_new(&rssd_index_ida, &index);
  3279. spin_unlock(&rssd_index_lock);
  3280. } while (rv == -EAGAIN);
  3281. if (rv)
  3282. goto ida_get_error;
  3283. rv = rssd_disk_name_format("rssd",
  3284. index,
  3285. dd->disk->disk_name,
  3286. DISK_NAME_LEN);
  3287. if (rv)
  3288. goto disk_index_error;
  3289. dd->disk->driverfs_dev = &dd->pdev->dev;
  3290. dd->disk->major = dd->major;
  3291. dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
  3292. dd->disk->fops = &mtip_block_ops;
  3293. dd->disk->private_data = dd;
  3294. dd->index = index;
  3295. /*
  3296. * if rebuild pending, start the service thread, and delay the block
  3297. * queue creation and add_disk()
  3298. */
  3299. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3300. goto start_service_thread;
  3301. skip_create_disk:
  3302. /* Allocate the request queue. */
  3303. dd->queue = blk_alloc_queue_node(GFP_KERNEL, dd->numa_node);
  3304. if (dd->queue == NULL) {
  3305. dev_err(&dd->pdev->dev,
  3306. "Unable to allocate request queue\n");
  3307. rv = -ENOMEM;
  3308. goto block_queue_alloc_init_error;
  3309. }
  3310. /* Attach our request function to the request queue. */
  3311. blk_queue_make_request(dd->queue, mtip_make_request);
  3312. dd->disk->queue = dd->queue;
  3313. dd->queue->queuedata = dd;
  3314. /* Set device limits. */
  3315. set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
  3316. blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
  3317. blk_queue_physical_block_size(dd->queue, 4096);
  3318. blk_queue_max_hw_sectors(dd->queue, 0xffff);
  3319. blk_queue_max_segment_size(dd->queue, 0x400000);
  3320. blk_queue_io_min(dd->queue, 4096);
  3321. /*
  3322. * write back cache is not supported in the device. FUA depends on
  3323. * write back cache support, hence setting flush support to zero.
  3324. */
  3325. blk_queue_flush(dd->queue, 0);
  3326. /* Set the capacity of the device in 512 byte sectors. */
  3327. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3328. dev_warn(&dd->pdev->dev,
  3329. "Could not read drive capacity\n");
  3330. rv = -EIO;
  3331. goto read_capacity_error;
  3332. }
  3333. set_capacity(dd->disk, capacity);
  3334. /* Enable the block device and add it to /dev */
  3335. add_disk(dd->disk);
  3336. /*
  3337. * Now that the disk is active, initialize any sysfs attributes
  3338. * managed by the protocol layer.
  3339. */
  3340. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3341. if (kobj) {
  3342. mtip_hw_sysfs_init(dd, kobj);
  3343. kobject_put(kobj);
  3344. }
  3345. mtip_hw_debugfs_init(dd);
  3346. if (dd->mtip_svc_handler) {
  3347. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3348. return rv; /* service thread created for handling rebuild */
  3349. }
  3350. start_service_thread:
  3351. sprintf(thd_name, "mtip_svc_thd_%02d", index);
  3352. dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
  3353. dd, dd->numa_node, thd_name);
  3354. if (IS_ERR(dd->mtip_svc_handler)) {
  3355. dev_err(&dd->pdev->dev, "service thread failed to start\n");
  3356. dd->mtip_svc_handler = NULL;
  3357. rv = -EFAULT;
  3358. goto kthread_run_error;
  3359. }
  3360. wake_up_process(dd->mtip_svc_handler);
  3361. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3362. rv = wait_for_rebuild;
  3363. return rv;
  3364. kthread_run_error:
  3365. mtip_hw_debugfs_exit(dd);
  3366. /* Delete our gendisk. This also removes the device from /dev */
  3367. del_gendisk(dd->disk);
  3368. read_capacity_error:
  3369. blk_cleanup_queue(dd->queue);
  3370. block_queue_alloc_init_error:
  3371. disk_index_error:
  3372. spin_lock(&rssd_index_lock);
  3373. ida_remove(&rssd_index_ida, index);
  3374. spin_unlock(&rssd_index_lock);
  3375. ida_get_error:
  3376. put_disk(dd->disk);
  3377. alloc_disk_error:
  3378. mtip_hw_exit(dd); /* De-initialize the protocol layer. */
  3379. protocol_init_error:
  3380. return rv;
  3381. }
  3382. /*
  3383. * Block layer deinitialization function.
  3384. *
  3385. * Called by the PCI layer as each P320 device is removed.
  3386. *
  3387. * @dd Pointer to the driver data structure.
  3388. *
  3389. * return value
  3390. * 0
  3391. */
  3392. static int mtip_block_remove(struct driver_data *dd)
  3393. {
  3394. struct kobject *kobj;
  3395. if (dd->mtip_svc_handler) {
  3396. set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
  3397. wake_up_interruptible(&dd->port->svc_wait);
  3398. kthread_stop(dd->mtip_svc_handler);
  3399. }
  3400. /* Clean up the sysfs attributes, if created */
  3401. if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
  3402. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3403. if (kobj) {
  3404. mtip_hw_sysfs_exit(dd, kobj);
  3405. kobject_put(kobj);
  3406. }
  3407. }
  3408. mtip_hw_debugfs_exit(dd);
  3409. /*
  3410. * Delete our gendisk structure. This also removes the device
  3411. * from /dev
  3412. */
  3413. del_gendisk(dd->disk);
  3414. spin_lock(&rssd_index_lock);
  3415. ida_remove(&rssd_index_ida, dd->index);
  3416. spin_unlock(&rssd_index_lock);
  3417. blk_cleanup_queue(dd->queue);
  3418. dd->disk = NULL;
  3419. dd->queue = NULL;
  3420. /* De-initialize the protocol layer. */
  3421. mtip_hw_exit(dd);
  3422. return 0;
  3423. }
  3424. /*
  3425. * Function called by the PCI layer when just before the
  3426. * machine shuts down.
  3427. *
  3428. * If a protocol layer shutdown function is present it will be called
  3429. * by this function.
  3430. *
  3431. * @dd Pointer to the driver data structure.
  3432. *
  3433. * return value
  3434. * 0
  3435. */
  3436. static int mtip_block_shutdown(struct driver_data *dd)
  3437. {
  3438. dev_info(&dd->pdev->dev,
  3439. "Shutting down %s ...\n", dd->disk->disk_name);
  3440. /* Delete our gendisk structure, and cleanup the blk queue. */
  3441. del_gendisk(dd->disk);
  3442. spin_lock(&rssd_index_lock);
  3443. ida_remove(&rssd_index_ida, dd->index);
  3444. spin_unlock(&rssd_index_lock);
  3445. blk_cleanup_queue(dd->queue);
  3446. dd->disk = NULL;
  3447. dd->queue = NULL;
  3448. mtip_hw_shutdown(dd);
  3449. return 0;
  3450. }
  3451. static int mtip_block_suspend(struct driver_data *dd)
  3452. {
  3453. dev_info(&dd->pdev->dev,
  3454. "Suspending %s ...\n", dd->disk->disk_name);
  3455. mtip_hw_suspend(dd);
  3456. return 0;
  3457. }
  3458. static int mtip_block_resume(struct driver_data *dd)
  3459. {
  3460. dev_info(&dd->pdev->dev, "Resuming %s ...\n",
  3461. dd->disk->disk_name);
  3462. mtip_hw_resume(dd);
  3463. return 0;
  3464. }
  3465. static void drop_cpu(int cpu)
  3466. {
  3467. cpu_use[cpu]--;
  3468. }
  3469. static int get_least_used_cpu_on_node(int node)
  3470. {
  3471. int cpu, least_used_cpu, least_cnt;
  3472. const struct cpumask *node_mask;
  3473. node_mask = cpumask_of_node(node);
  3474. least_used_cpu = cpumask_first(node_mask);
  3475. least_cnt = cpu_use[least_used_cpu];
  3476. cpu = least_used_cpu;
  3477. for_each_cpu(cpu, node_mask) {
  3478. if (cpu_use[cpu] < least_cnt) {
  3479. least_used_cpu = cpu;
  3480. least_cnt = cpu_use[cpu];
  3481. }
  3482. }
  3483. cpu_use[least_used_cpu]++;
  3484. return least_used_cpu;
  3485. }
  3486. /* Helper for selecting a node in round robin mode */
  3487. static inline int mtip_get_next_rr_node(void)
  3488. {
  3489. static int next_node = -1;
  3490. if (next_node == -1) {
  3491. next_node = first_online_node;
  3492. return next_node;
  3493. }
  3494. next_node = next_online_node(next_node);
  3495. if (next_node == MAX_NUMNODES)
  3496. next_node = first_online_node;
  3497. return next_node;
  3498. }
  3499. DEFINE_HANDLER(0);
  3500. DEFINE_HANDLER(1);
  3501. DEFINE_HANDLER(2);
  3502. DEFINE_HANDLER(3);
  3503. DEFINE_HANDLER(4);
  3504. DEFINE_HANDLER(5);
  3505. DEFINE_HANDLER(6);
  3506. DEFINE_HANDLER(7);
  3507. /*
  3508. * Called for each supported PCI device detected.
  3509. *
  3510. * This function allocates the private data structure, enables the
  3511. * PCI device and then calls the block layer initialization function.
  3512. *
  3513. * return value
  3514. * 0 on success else an error code.
  3515. */
  3516. static int mtip_pci_probe(struct pci_dev *pdev,
  3517. const struct pci_device_id *ent)
  3518. {
  3519. int rv = 0;
  3520. struct driver_data *dd = NULL;
  3521. char cpu_list[256];
  3522. const struct cpumask *node_mask;
  3523. int cpu, i = 0, j = 0;
  3524. int my_node = NUMA_NO_NODE;
  3525. /* Allocate memory for this devices private data. */
  3526. my_node = pcibus_to_node(pdev->bus);
  3527. if (my_node != NUMA_NO_NODE) {
  3528. if (!node_online(my_node))
  3529. my_node = mtip_get_next_rr_node();
  3530. } else {
  3531. dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
  3532. my_node = mtip_get_next_rr_node();
  3533. }
  3534. dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
  3535. my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
  3536. cpu_to_node(smp_processor_id()), smp_processor_id());
  3537. dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
  3538. if (dd == NULL) {
  3539. dev_err(&pdev->dev,
  3540. "Unable to allocate memory for driver data\n");
  3541. return -ENOMEM;
  3542. }
  3543. /* Attach the private data to this PCI device. */
  3544. pci_set_drvdata(pdev, dd);
  3545. rv = pcim_enable_device(pdev);
  3546. if (rv < 0) {
  3547. dev_err(&pdev->dev, "Unable to enable device\n");
  3548. goto iomap_err;
  3549. }
  3550. /* Map BAR5 to memory. */
  3551. rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
  3552. if (rv < 0) {
  3553. dev_err(&pdev->dev, "Unable to map regions\n");
  3554. goto iomap_err;
  3555. }
  3556. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3557. rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3558. if (rv) {
  3559. rv = pci_set_consistent_dma_mask(pdev,
  3560. DMA_BIT_MASK(32));
  3561. if (rv) {
  3562. dev_warn(&pdev->dev,
  3563. "64-bit DMA enable failed\n");
  3564. goto setmask_err;
  3565. }
  3566. }
  3567. }
  3568. /* Copy the info we may need later into the private data structure. */
  3569. dd->major = mtip_major;
  3570. dd->instance = instance;
  3571. dd->pdev = pdev;
  3572. dd->numa_node = my_node;
  3573. memset(dd->workq_name, 0, 32);
  3574. snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
  3575. dd->isr_workq = create_workqueue(dd->workq_name);
  3576. if (!dd->isr_workq) {
  3577. dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
  3578. goto block_initialize_err;
  3579. }
  3580. memset(cpu_list, 0, sizeof(cpu_list));
  3581. node_mask = cpumask_of_node(dd->numa_node);
  3582. if (!cpumask_empty(node_mask)) {
  3583. for_each_cpu(cpu, node_mask)
  3584. {
  3585. snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
  3586. j = strlen(cpu_list);
  3587. }
  3588. dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
  3589. dd->numa_node,
  3590. topology_physical_package_id(cpumask_first(node_mask)),
  3591. nr_cpus_node(dd->numa_node),
  3592. cpu_list);
  3593. } else
  3594. dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
  3595. dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
  3596. dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
  3597. cpu_to_node(dd->isr_binding), dd->isr_binding);
  3598. /* first worker context always runs in ISR */
  3599. dd->work[0].cpu_binding = dd->isr_binding;
  3600. dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
  3601. dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
  3602. dd->work[3].cpu_binding = dd->work[0].cpu_binding;
  3603. dd->work[4].cpu_binding = dd->work[1].cpu_binding;
  3604. dd->work[5].cpu_binding = dd->work[2].cpu_binding;
  3605. dd->work[6].cpu_binding = dd->work[2].cpu_binding;
  3606. dd->work[7].cpu_binding = dd->work[1].cpu_binding;
  3607. /* Log the bindings */
  3608. for_each_present_cpu(cpu) {
  3609. memset(cpu_list, 0, sizeof(cpu_list));
  3610. for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
  3611. if (dd->work[i].cpu_binding == cpu) {
  3612. snprintf(&cpu_list[j], 256 - j, "%d ", i);
  3613. j = strlen(cpu_list);
  3614. }
  3615. }
  3616. if (j)
  3617. dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
  3618. }
  3619. INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
  3620. INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
  3621. INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
  3622. INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
  3623. INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
  3624. INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
  3625. INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
  3626. INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
  3627. pci_set_master(pdev);
  3628. if (pci_enable_msi(pdev)) {
  3629. dev_warn(&pdev->dev,
  3630. "Unable to enable MSI interrupt.\n");
  3631. goto block_initialize_err;
  3632. }
  3633. /* Initialize the block layer. */
  3634. rv = mtip_block_initialize(dd);
  3635. if (rv < 0) {
  3636. dev_err(&pdev->dev,
  3637. "Unable to initialize block layer\n");
  3638. goto block_initialize_err;
  3639. }
  3640. /*
  3641. * Increment the instance count so that each device has a unique
  3642. * instance number.
  3643. */
  3644. instance++;
  3645. if (rv != MTIP_FTL_REBUILD_MAGIC)
  3646. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3647. goto done;
  3648. block_initialize_err:
  3649. pci_disable_msi(pdev);
  3650. if (dd->isr_workq) {
  3651. flush_workqueue(dd->isr_workq);
  3652. destroy_workqueue(dd->isr_workq);
  3653. drop_cpu(dd->work[0].cpu_binding);
  3654. drop_cpu(dd->work[1].cpu_binding);
  3655. drop_cpu(dd->work[2].cpu_binding);
  3656. }
  3657. setmask_err:
  3658. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3659. iomap_err:
  3660. kfree(dd);
  3661. pci_set_drvdata(pdev, NULL);
  3662. return rv;
  3663. done:
  3664. return rv;
  3665. }
  3666. /*
  3667. * Called for each probed device when the device is removed or the
  3668. * driver is unloaded.
  3669. *
  3670. * return value
  3671. * None
  3672. */
  3673. static void mtip_pci_remove(struct pci_dev *pdev)
  3674. {
  3675. struct driver_data *dd = pci_get_drvdata(pdev);
  3676. int counter = 0;
  3677. set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
  3678. if (mtip_check_surprise_removal(pdev)) {
  3679. while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
  3680. counter++;
  3681. msleep(20);
  3682. if (counter == 10) {
  3683. /* Cleanup the outstanding commands */
  3684. mtip_command_cleanup(dd);
  3685. break;
  3686. }
  3687. }
  3688. }
  3689. /* Clean up the block layer. */
  3690. mtip_block_remove(dd);
  3691. if (dd->isr_workq) {
  3692. flush_workqueue(dd->isr_workq);
  3693. destroy_workqueue(dd->isr_workq);
  3694. drop_cpu(dd->work[0].cpu_binding);
  3695. drop_cpu(dd->work[1].cpu_binding);
  3696. drop_cpu(dd->work[2].cpu_binding);
  3697. }
  3698. pci_disable_msi(pdev);
  3699. kfree(dd);
  3700. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3701. }
  3702. /*
  3703. * Called for each probed device when the device is suspended.
  3704. *
  3705. * return value
  3706. * 0 Success
  3707. * <0 Error
  3708. */
  3709. static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  3710. {
  3711. int rv = 0;
  3712. struct driver_data *dd = pci_get_drvdata(pdev);
  3713. if (!dd) {
  3714. dev_err(&pdev->dev,
  3715. "Driver private datastructure is NULL\n");
  3716. return -EFAULT;
  3717. }
  3718. set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  3719. /* Disable ports & interrupts then send standby immediate */
  3720. rv = mtip_block_suspend(dd);
  3721. if (rv < 0) {
  3722. dev_err(&pdev->dev,
  3723. "Failed to suspend controller\n");
  3724. return rv;
  3725. }
  3726. /*
  3727. * Save the pci config space to pdev structure &
  3728. * disable the device
  3729. */
  3730. pci_save_state(pdev);
  3731. pci_disable_device(pdev);
  3732. /* Move to Low power state*/
  3733. pci_set_power_state(pdev, PCI_D3hot);
  3734. return rv;
  3735. }
  3736. /*
  3737. * Called for each probed device when the device is resumed.
  3738. *
  3739. * return value
  3740. * 0 Success
  3741. * <0 Error
  3742. */
  3743. static int mtip_pci_resume(struct pci_dev *pdev)
  3744. {
  3745. int rv = 0;
  3746. struct driver_data *dd;
  3747. dd = pci_get_drvdata(pdev);
  3748. if (!dd) {
  3749. dev_err(&pdev->dev,
  3750. "Driver private datastructure is NULL\n");
  3751. return -EFAULT;
  3752. }
  3753. /* Move the device to active State */
  3754. pci_set_power_state(pdev, PCI_D0);
  3755. /* Restore PCI configuration space */
  3756. pci_restore_state(pdev);
  3757. /* Enable the PCI device*/
  3758. rv = pcim_enable_device(pdev);
  3759. if (rv < 0) {
  3760. dev_err(&pdev->dev,
  3761. "Failed to enable card during resume\n");
  3762. goto err;
  3763. }
  3764. pci_set_master(pdev);
  3765. /*
  3766. * Calls hbaReset, initPort, & startPort function
  3767. * then enables interrupts
  3768. */
  3769. rv = mtip_block_resume(dd);
  3770. if (rv < 0)
  3771. dev_err(&pdev->dev, "Unable to resume\n");
  3772. err:
  3773. clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  3774. return rv;
  3775. }
  3776. /*
  3777. * Shutdown routine
  3778. *
  3779. * return value
  3780. * None
  3781. */
  3782. static void mtip_pci_shutdown(struct pci_dev *pdev)
  3783. {
  3784. struct driver_data *dd = pci_get_drvdata(pdev);
  3785. if (dd)
  3786. mtip_block_shutdown(dd);
  3787. }
  3788. /* Table of device ids supported by this driver. */
  3789. static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
  3790. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
  3791. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
  3792. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
  3793. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
  3794. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
  3795. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
  3796. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
  3797. { 0 }
  3798. };
  3799. /* Structure that describes the PCI driver functions. */
  3800. static struct pci_driver mtip_pci_driver = {
  3801. .name = MTIP_DRV_NAME,
  3802. .id_table = mtip_pci_tbl,
  3803. .probe = mtip_pci_probe,
  3804. .remove = mtip_pci_remove,
  3805. .suspend = mtip_pci_suspend,
  3806. .resume = mtip_pci_resume,
  3807. .shutdown = mtip_pci_shutdown,
  3808. };
  3809. MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
  3810. /*
  3811. * Module initialization function.
  3812. *
  3813. * Called once when the module is loaded. This function allocates a major
  3814. * block device number to the Cyclone devices and registers the PCI layer
  3815. * of the driver.
  3816. *
  3817. * Return value
  3818. * 0 on success else error code.
  3819. */
  3820. static int __init mtip_init(void)
  3821. {
  3822. int error;
  3823. pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
  3824. /* Allocate a major block device number to use with this driver. */
  3825. error = register_blkdev(0, MTIP_DRV_NAME);
  3826. if (error <= 0) {
  3827. pr_err("Unable to register block device (%d)\n",
  3828. error);
  3829. return -EBUSY;
  3830. }
  3831. mtip_major = error;
  3832. if (!dfs_parent) {
  3833. dfs_parent = debugfs_create_dir("rssd", NULL);
  3834. if (IS_ERR_OR_NULL(dfs_parent)) {
  3835. pr_warn("Error creating debugfs parent\n");
  3836. dfs_parent = NULL;
  3837. }
  3838. }
  3839. /* Register our PCI operations. */
  3840. error = pci_register_driver(&mtip_pci_driver);
  3841. if (error) {
  3842. debugfs_remove(dfs_parent);
  3843. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  3844. }
  3845. return error;
  3846. }
  3847. /*
  3848. * Module de-initialization function.
  3849. *
  3850. * Called once when the module is unloaded. This function deallocates
  3851. * the major block device number allocated by mtip_init() and
  3852. * unregisters the PCI layer of the driver.
  3853. *
  3854. * Return value
  3855. * none
  3856. */
  3857. static void __exit mtip_exit(void)
  3858. {
  3859. debugfs_remove_recursive(dfs_parent);
  3860. /* Release the allocated major block device number. */
  3861. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  3862. /* Unregister the PCI driver. */
  3863. pci_unregister_driver(&mtip_pci_driver);
  3864. }
  3865. MODULE_AUTHOR("Micron Technology, Inc");
  3866. MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
  3867. MODULE_LICENSE("GPL");
  3868. MODULE_VERSION(MTIP_DRV_VERSION);
  3869. module_init(mtip_init);
  3870. module_exit(mtip_exit);