t4_hw.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2003-2010 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/delay.h>
  35. #include "cxgb4.h"
  36. #include "t4_regs.h"
  37. #include "t4fw_api.h"
  38. static int t4_fw_upgrade(struct adapter *adap, unsigned int mbox,
  39. const u8 *fw_data, unsigned int size, int force);
  40. /**
  41. * t4_wait_op_done_val - wait until an operation is completed
  42. * @adapter: the adapter performing the operation
  43. * @reg: the register to check for completion
  44. * @mask: a single-bit field within @reg that indicates completion
  45. * @polarity: the value of the field when the operation is completed
  46. * @attempts: number of check iterations
  47. * @delay: delay in usecs between iterations
  48. * @valp: where to store the value of the register at completion time
  49. *
  50. * Wait until an operation is completed by checking a bit in a register
  51. * up to @attempts times. If @valp is not NULL the value of the register
  52. * at the time it indicated completion is stored there. Returns 0 if the
  53. * operation completes and -EAGAIN otherwise.
  54. */
  55. static int t4_wait_op_done_val(struct adapter *adapter, int reg, u32 mask,
  56. int polarity, int attempts, int delay, u32 *valp)
  57. {
  58. while (1) {
  59. u32 val = t4_read_reg(adapter, reg);
  60. if (!!(val & mask) == polarity) {
  61. if (valp)
  62. *valp = val;
  63. return 0;
  64. }
  65. if (--attempts == 0)
  66. return -EAGAIN;
  67. if (delay)
  68. udelay(delay);
  69. }
  70. }
  71. static inline int t4_wait_op_done(struct adapter *adapter, int reg, u32 mask,
  72. int polarity, int attempts, int delay)
  73. {
  74. return t4_wait_op_done_val(adapter, reg, mask, polarity, attempts,
  75. delay, NULL);
  76. }
  77. /**
  78. * t4_set_reg_field - set a register field to a value
  79. * @adapter: the adapter to program
  80. * @addr: the register address
  81. * @mask: specifies the portion of the register to modify
  82. * @val: the new value for the register field
  83. *
  84. * Sets a register field specified by the supplied mask to the
  85. * given value.
  86. */
  87. void t4_set_reg_field(struct adapter *adapter, unsigned int addr, u32 mask,
  88. u32 val)
  89. {
  90. u32 v = t4_read_reg(adapter, addr) & ~mask;
  91. t4_write_reg(adapter, addr, v | val);
  92. (void) t4_read_reg(adapter, addr); /* flush */
  93. }
  94. /**
  95. * t4_read_indirect - read indirectly addressed registers
  96. * @adap: the adapter
  97. * @addr_reg: register holding the indirect address
  98. * @data_reg: register holding the value of the indirect register
  99. * @vals: where the read register values are stored
  100. * @nregs: how many indirect registers to read
  101. * @start_idx: index of first indirect register to read
  102. *
  103. * Reads registers that are accessed indirectly through an address/data
  104. * register pair.
  105. */
  106. void t4_read_indirect(struct adapter *adap, unsigned int addr_reg,
  107. unsigned int data_reg, u32 *vals,
  108. unsigned int nregs, unsigned int start_idx)
  109. {
  110. while (nregs--) {
  111. t4_write_reg(adap, addr_reg, start_idx);
  112. *vals++ = t4_read_reg(adap, data_reg);
  113. start_idx++;
  114. }
  115. }
  116. /**
  117. * t4_write_indirect - write indirectly addressed registers
  118. * @adap: the adapter
  119. * @addr_reg: register holding the indirect addresses
  120. * @data_reg: register holding the value for the indirect registers
  121. * @vals: values to write
  122. * @nregs: how many indirect registers to write
  123. * @start_idx: address of first indirect register to write
  124. *
  125. * Writes a sequential block of registers that are accessed indirectly
  126. * through an address/data register pair.
  127. */
  128. void t4_write_indirect(struct adapter *adap, unsigned int addr_reg,
  129. unsigned int data_reg, const u32 *vals,
  130. unsigned int nregs, unsigned int start_idx)
  131. {
  132. while (nregs--) {
  133. t4_write_reg(adap, addr_reg, start_idx++);
  134. t4_write_reg(adap, data_reg, *vals++);
  135. }
  136. }
  137. /*
  138. * Get the reply to a mailbox command and store it in @rpl in big-endian order.
  139. */
  140. static void get_mbox_rpl(struct adapter *adap, __be64 *rpl, int nflit,
  141. u32 mbox_addr)
  142. {
  143. for ( ; nflit; nflit--, mbox_addr += 8)
  144. *rpl++ = cpu_to_be64(t4_read_reg64(adap, mbox_addr));
  145. }
  146. /*
  147. * Handle a FW assertion reported in a mailbox.
  148. */
  149. static void fw_asrt(struct adapter *adap, u32 mbox_addr)
  150. {
  151. struct fw_debug_cmd asrt;
  152. get_mbox_rpl(adap, (__be64 *)&asrt, sizeof(asrt) / 8, mbox_addr);
  153. dev_alert(adap->pdev_dev,
  154. "FW assertion at %.16s:%u, val0 %#x, val1 %#x\n",
  155. asrt.u.assert.filename_0_7, ntohl(asrt.u.assert.line),
  156. ntohl(asrt.u.assert.x), ntohl(asrt.u.assert.y));
  157. }
  158. static void dump_mbox(struct adapter *adap, int mbox, u32 data_reg)
  159. {
  160. dev_err(adap->pdev_dev,
  161. "mbox %d: %llx %llx %llx %llx %llx %llx %llx %llx\n", mbox,
  162. (unsigned long long)t4_read_reg64(adap, data_reg),
  163. (unsigned long long)t4_read_reg64(adap, data_reg + 8),
  164. (unsigned long long)t4_read_reg64(adap, data_reg + 16),
  165. (unsigned long long)t4_read_reg64(adap, data_reg + 24),
  166. (unsigned long long)t4_read_reg64(adap, data_reg + 32),
  167. (unsigned long long)t4_read_reg64(adap, data_reg + 40),
  168. (unsigned long long)t4_read_reg64(adap, data_reg + 48),
  169. (unsigned long long)t4_read_reg64(adap, data_reg + 56));
  170. }
  171. /**
  172. * t4_wr_mbox_meat - send a command to FW through the given mailbox
  173. * @adap: the adapter
  174. * @mbox: index of the mailbox to use
  175. * @cmd: the command to write
  176. * @size: command length in bytes
  177. * @rpl: where to optionally store the reply
  178. * @sleep_ok: if true we may sleep while awaiting command completion
  179. *
  180. * Sends the given command to FW through the selected mailbox and waits
  181. * for the FW to execute the command. If @rpl is not %NULL it is used to
  182. * store the FW's reply to the command. The command and its optional
  183. * reply are of the same length. FW can take up to %FW_CMD_MAX_TIMEOUT ms
  184. * to respond. @sleep_ok determines whether we may sleep while awaiting
  185. * the response. If sleeping is allowed we use progressive backoff
  186. * otherwise we spin.
  187. *
  188. * The return value is 0 on success or a negative errno on failure. A
  189. * failure can happen either because we are not able to execute the
  190. * command or FW executes it but signals an error. In the latter case
  191. * the return value is the error code indicated by FW (negated).
  192. */
  193. int t4_wr_mbox_meat(struct adapter *adap, int mbox, const void *cmd, int size,
  194. void *rpl, bool sleep_ok)
  195. {
  196. static const int delay[] = {
  197. 1, 1, 3, 5, 10, 10, 20, 50, 100, 200
  198. };
  199. u32 v;
  200. u64 res;
  201. int i, ms, delay_idx;
  202. const __be64 *p = cmd;
  203. u32 data_reg = PF_REG(mbox, CIM_PF_MAILBOX_DATA);
  204. u32 ctl_reg = PF_REG(mbox, CIM_PF_MAILBOX_CTRL);
  205. if ((size & 15) || size > MBOX_LEN)
  206. return -EINVAL;
  207. /*
  208. * If the device is off-line, as in EEH, commands will time out.
  209. * Fail them early so we don't waste time waiting.
  210. */
  211. if (adap->pdev->error_state != pci_channel_io_normal)
  212. return -EIO;
  213. v = MBOWNER_GET(t4_read_reg(adap, ctl_reg));
  214. for (i = 0; v == MBOX_OWNER_NONE && i < 3; i++)
  215. v = MBOWNER_GET(t4_read_reg(adap, ctl_reg));
  216. if (v != MBOX_OWNER_DRV)
  217. return v ? -EBUSY : -ETIMEDOUT;
  218. for (i = 0; i < size; i += 8)
  219. t4_write_reg64(adap, data_reg + i, be64_to_cpu(*p++));
  220. t4_write_reg(adap, ctl_reg, MBMSGVALID | MBOWNER(MBOX_OWNER_FW));
  221. t4_read_reg(adap, ctl_reg); /* flush write */
  222. delay_idx = 0;
  223. ms = delay[0];
  224. for (i = 0; i < FW_CMD_MAX_TIMEOUT; i += ms) {
  225. if (sleep_ok) {
  226. ms = delay[delay_idx]; /* last element may repeat */
  227. if (delay_idx < ARRAY_SIZE(delay) - 1)
  228. delay_idx++;
  229. msleep(ms);
  230. } else
  231. mdelay(ms);
  232. v = t4_read_reg(adap, ctl_reg);
  233. if (MBOWNER_GET(v) == MBOX_OWNER_DRV) {
  234. if (!(v & MBMSGVALID)) {
  235. t4_write_reg(adap, ctl_reg, 0);
  236. continue;
  237. }
  238. res = t4_read_reg64(adap, data_reg);
  239. if (FW_CMD_OP_GET(res >> 32) == FW_DEBUG_CMD) {
  240. fw_asrt(adap, data_reg);
  241. res = FW_CMD_RETVAL(EIO);
  242. } else if (rpl)
  243. get_mbox_rpl(adap, rpl, size / 8, data_reg);
  244. if (FW_CMD_RETVAL_GET((int)res))
  245. dump_mbox(adap, mbox, data_reg);
  246. t4_write_reg(adap, ctl_reg, 0);
  247. return -FW_CMD_RETVAL_GET((int)res);
  248. }
  249. }
  250. dump_mbox(adap, mbox, data_reg);
  251. dev_err(adap->pdev_dev, "command %#x in mailbox %d timed out\n",
  252. *(const u8 *)cmd, mbox);
  253. return -ETIMEDOUT;
  254. }
  255. /**
  256. * t4_mc_read - read from MC through backdoor accesses
  257. * @adap: the adapter
  258. * @addr: address of first byte requested
  259. * @idx: which MC to access
  260. * @data: 64 bytes of data containing the requested address
  261. * @ecc: where to store the corresponding 64-bit ECC word
  262. *
  263. * Read 64 bytes of data from MC starting at a 64-byte-aligned address
  264. * that covers the requested address @addr. If @parity is not %NULL it
  265. * is assigned the 64-bit ECC word for the read data.
  266. */
  267. int t4_mc_read(struct adapter *adap, int idx, u32 addr, __be32 *data, u64 *ecc)
  268. {
  269. int i;
  270. u32 mc_bist_cmd, mc_bist_cmd_addr, mc_bist_cmd_len;
  271. u32 mc_bist_status_rdata, mc_bist_data_pattern;
  272. if (is_t4(adap->params.chip)) {
  273. mc_bist_cmd = MC_BIST_CMD;
  274. mc_bist_cmd_addr = MC_BIST_CMD_ADDR;
  275. mc_bist_cmd_len = MC_BIST_CMD_LEN;
  276. mc_bist_status_rdata = MC_BIST_STATUS_RDATA;
  277. mc_bist_data_pattern = MC_BIST_DATA_PATTERN;
  278. } else {
  279. mc_bist_cmd = MC_REG(MC_P_BIST_CMD, idx);
  280. mc_bist_cmd_addr = MC_REG(MC_P_BIST_CMD_ADDR, idx);
  281. mc_bist_cmd_len = MC_REG(MC_P_BIST_CMD_LEN, idx);
  282. mc_bist_status_rdata = MC_REG(MC_P_BIST_STATUS_RDATA, idx);
  283. mc_bist_data_pattern = MC_REG(MC_P_BIST_DATA_PATTERN, idx);
  284. }
  285. if (t4_read_reg(adap, mc_bist_cmd) & START_BIST)
  286. return -EBUSY;
  287. t4_write_reg(adap, mc_bist_cmd_addr, addr & ~0x3fU);
  288. t4_write_reg(adap, mc_bist_cmd_len, 64);
  289. t4_write_reg(adap, mc_bist_data_pattern, 0xc);
  290. t4_write_reg(adap, mc_bist_cmd, BIST_OPCODE(1) | START_BIST |
  291. BIST_CMD_GAP(1));
  292. i = t4_wait_op_done(adap, mc_bist_cmd, START_BIST, 0, 10, 1);
  293. if (i)
  294. return i;
  295. #define MC_DATA(i) MC_BIST_STATUS_REG(mc_bist_status_rdata, i)
  296. for (i = 15; i >= 0; i--)
  297. *data++ = htonl(t4_read_reg(adap, MC_DATA(i)));
  298. if (ecc)
  299. *ecc = t4_read_reg64(adap, MC_DATA(16));
  300. #undef MC_DATA
  301. return 0;
  302. }
  303. /**
  304. * t4_edc_read - read from EDC through backdoor accesses
  305. * @adap: the adapter
  306. * @idx: which EDC to access
  307. * @addr: address of first byte requested
  308. * @data: 64 bytes of data containing the requested address
  309. * @ecc: where to store the corresponding 64-bit ECC word
  310. *
  311. * Read 64 bytes of data from EDC starting at a 64-byte-aligned address
  312. * that covers the requested address @addr. If @parity is not %NULL it
  313. * is assigned the 64-bit ECC word for the read data.
  314. */
  315. int t4_edc_read(struct adapter *adap, int idx, u32 addr, __be32 *data, u64 *ecc)
  316. {
  317. int i;
  318. u32 edc_bist_cmd, edc_bist_cmd_addr, edc_bist_cmd_len;
  319. u32 edc_bist_cmd_data_pattern, edc_bist_status_rdata;
  320. if (is_t4(adap->params.chip)) {
  321. edc_bist_cmd = EDC_REG(EDC_BIST_CMD, idx);
  322. edc_bist_cmd_addr = EDC_REG(EDC_BIST_CMD_ADDR, idx);
  323. edc_bist_cmd_len = EDC_REG(EDC_BIST_CMD_LEN, idx);
  324. edc_bist_cmd_data_pattern = EDC_REG(EDC_BIST_DATA_PATTERN,
  325. idx);
  326. edc_bist_status_rdata = EDC_REG(EDC_BIST_STATUS_RDATA,
  327. idx);
  328. } else {
  329. edc_bist_cmd = EDC_REG_T5(EDC_H_BIST_CMD, idx);
  330. edc_bist_cmd_addr = EDC_REG_T5(EDC_H_BIST_CMD_ADDR, idx);
  331. edc_bist_cmd_len = EDC_REG_T5(EDC_H_BIST_CMD_LEN, idx);
  332. edc_bist_cmd_data_pattern =
  333. EDC_REG_T5(EDC_H_BIST_DATA_PATTERN, idx);
  334. edc_bist_status_rdata =
  335. EDC_REG_T5(EDC_H_BIST_STATUS_RDATA, idx);
  336. }
  337. if (t4_read_reg(adap, edc_bist_cmd) & START_BIST)
  338. return -EBUSY;
  339. t4_write_reg(adap, edc_bist_cmd_addr, addr & ~0x3fU);
  340. t4_write_reg(adap, edc_bist_cmd_len, 64);
  341. t4_write_reg(adap, edc_bist_cmd_data_pattern, 0xc);
  342. t4_write_reg(adap, edc_bist_cmd,
  343. BIST_OPCODE(1) | BIST_CMD_GAP(1) | START_BIST);
  344. i = t4_wait_op_done(adap, edc_bist_cmd, START_BIST, 0, 10, 1);
  345. if (i)
  346. return i;
  347. #define EDC_DATA(i) (EDC_BIST_STATUS_REG(edc_bist_status_rdata, i))
  348. for (i = 15; i >= 0; i--)
  349. *data++ = htonl(t4_read_reg(adap, EDC_DATA(i)));
  350. if (ecc)
  351. *ecc = t4_read_reg64(adap, EDC_DATA(16));
  352. #undef EDC_DATA
  353. return 0;
  354. }
  355. /*
  356. * t4_mem_win_rw - read/write memory through PCIE memory window
  357. * @adap: the adapter
  358. * @addr: address of first byte requested
  359. * @data: MEMWIN0_APERTURE bytes of data containing the requested address
  360. * @dir: direction of transfer 1 => read, 0 => write
  361. *
  362. * Read/write MEMWIN0_APERTURE bytes of data from MC starting at a
  363. * MEMWIN0_APERTURE-byte-aligned address that covers the requested
  364. * address @addr.
  365. */
  366. static int t4_mem_win_rw(struct adapter *adap, u32 addr, __be32 *data, int dir)
  367. {
  368. int i;
  369. u32 win_pf = is_t4(adap->params.chip) ? 0 : V_PFNUM(adap->fn);
  370. /*
  371. * Setup offset into PCIE memory window. Address must be a
  372. * MEMWIN0_APERTURE-byte-aligned address. (Read back MA register to
  373. * ensure that changes propagate before we attempt to use the new
  374. * values.)
  375. */
  376. t4_write_reg(adap, PCIE_MEM_ACCESS_OFFSET,
  377. (addr & ~(MEMWIN0_APERTURE - 1)) | win_pf);
  378. t4_read_reg(adap, PCIE_MEM_ACCESS_OFFSET);
  379. /* Collecting data 4 bytes at a time upto MEMWIN0_APERTURE */
  380. for (i = 0; i < MEMWIN0_APERTURE; i = i+0x4) {
  381. if (dir)
  382. *data++ = (__force __be32) t4_read_reg(adap,
  383. (MEMWIN0_BASE + i));
  384. else
  385. t4_write_reg(adap, (MEMWIN0_BASE + i),
  386. (__force u32) *data++);
  387. }
  388. return 0;
  389. }
  390. /**
  391. * t4_memory_rw - read/write EDC 0, EDC 1 or MC via PCIE memory window
  392. * @adap: the adapter
  393. * @mtype: memory type: MEM_EDC0, MEM_EDC1 or MEM_MC
  394. * @addr: address within indicated memory type
  395. * @len: amount of memory to transfer
  396. * @buf: host memory buffer
  397. * @dir: direction of transfer 1 => read, 0 => write
  398. *
  399. * Reads/writes an [almost] arbitrary memory region in the firmware: the
  400. * firmware memory address, length and host buffer must be aligned on
  401. * 32-bit boudaries. The memory is transferred as a raw byte sequence
  402. * from/to the firmware's memory. If this memory contains data
  403. * structures which contain multi-byte integers, it's the callers
  404. * responsibility to perform appropriate byte order conversions.
  405. */
  406. static int t4_memory_rw(struct adapter *adap, int mtype, u32 addr, u32 len,
  407. __be32 *buf, int dir)
  408. {
  409. u32 pos, start, end, offset, memoffset;
  410. u32 edc_size, mc_size;
  411. int ret = 0;
  412. __be32 *data;
  413. /*
  414. * Argument sanity checks ...
  415. */
  416. if ((addr & 0x3) || (len & 0x3))
  417. return -EINVAL;
  418. data = vmalloc(MEMWIN0_APERTURE);
  419. if (!data)
  420. return -ENOMEM;
  421. /* Offset into the region of memory which is being accessed
  422. * MEM_EDC0 = 0
  423. * MEM_EDC1 = 1
  424. * MEM_MC = 2 -- T4
  425. * MEM_MC0 = 2 -- For T5
  426. * MEM_MC1 = 3 -- For T5
  427. */
  428. edc_size = EDRAM_SIZE_GET(t4_read_reg(adap, MA_EDRAM0_BAR));
  429. if (mtype != MEM_MC1)
  430. memoffset = (mtype * (edc_size * 1024 * 1024));
  431. else {
  432. mc_size = EXT_MEM_SIZE_GET(t4_read_reg(adap,
  433. MA_EXT_MEMORY_BAR));
  434. memoffset = (MEM_MC0 * edc_size + mc_size) * 1024 * 1024;
  435. }
  436. /* Determine the PCIE_MEM_ACCESS_OFFSET */
  437. addr = addr + memoffset;
  438. /*
  439. * The underlaying EDC/MC read routines read MEMWIN0_APERTURE bytes
  440. * at a time so we need to round down the start and round up the end.
  441. * We'll start copying out of the first line at (addr - start) a word
  442. * at a time.
  443. */
  444. start = addr & ~(MEMWIN0_APERTURE-1);
  445. end = (addr + len + MEMWIN0_APERTURE-1) & ~(MEMWIN0_APERTURE-1);
  446. offset = (addr - start)/sizeof(__be32);
  447. for (pos = start; pos < end; pos += MEMWIN0_APERTURE, offset = 0) {
  448. /*
  449. * If we're writing, copy the data from the caller's memory
  450. * buffer
  451. */
  452. if (!dir) {
  453. /*
  454. * If we're doing a partial write, then we need to do
  455. * a read-modify-write ...
  456. */
  457. if (offset || len < MEMWIN0_APERTURE) {
  458. ret = t4_mem_win_rw(adap, pos, data, 1);
  459. if (ret)
  460. break;
  461. }
  462. while (offset < (MEMWIN0_APERTURE/sizeof(__be32)) &&
  463. len > 0) {
  464. data[offset++] = *buf++;
  465. len -= sizeof(__be32);
  466. }
  467. }
  468. /*
  469. * Transfer a block of memory and bail if there's an error.
  470. */
  471. ret = t4_mem_win_rw(adap, pos, data, dir);
  472. if (ret)
  473. break;
  474. /*
  475. * If we're reading, copy the data into the caller's memory
  476. * buffer.
  477. */
  478. if (dir)
  479. while (offset < (MEMWIN0_APERTURE/sizeof(__be32)) &&
  480. len > 0) {
  481. *buf++ = data[offset++];
  482. len -= sizeof(__be32);
  483. }
  484. }
  485. vfree(data);
  486. return ret;
  487. }
  488. int t4_memory_write(struct adapter *adap, int mtype, u32 addr, u32 len,
  489. __be32 *buf)
  490. {
  491. return t4_memory_rw(adap, mtype, addr, len, buf, 0);
  492. }
  493. #define EEPROM_STAT_ADDR 0x7bfc
  494. #define VPD_BASE 0x400
  495. #define VPD_BASE_OLD 0
  496. #define VPD_LEN 1024
  497. /**
  498. * t4_seeprom_wp - enable/disable EEPROM write protection
  499. * @adapter: the adapter
  500. * @enable: whether to enable or disable write protection
  501. *
  502. * Enables or disables write protection on the serial EEPROM.
  503. */
  504. int t4_seeprom_wp(struct adapter *adapter, bool enable)
  505. {
  506. unsigned int v = enable ? 0xc : 0;
  507. int ret = pci_write_vpd(adapter->pdev, EEPROM_STAT_ADDR, 4, &v);
  508. return ret < 0 ? ret : 0;
  509. }
  510. /**
  511. * get_vpd_params - read VPD parameters from VPD EEPROM
  512. * @adapter: adapter to read
  513. * @p: where to store the parameters
  514. *
  515. * Reads card parameters stored in VPD EEPROM.
  516. */
  517. int get_vpd_params(struct adapter *adapter, struct vpd_params *p)
  518. {
  519. u32 cclk_param, cclk_val;
  520. int i, ret, addr;
  521. int ec, sn, pn;
  522. u8 *vpd, csum;
  523. unsigned int vpdr_len, kw_offset, id_len;
  524. vpd = vmalloc(VPD_LEN);
  525. if (!vpd)
  526. return -ENOMEM;
  527. ret = pci_read_vpd(adapter->pdev, VPD_BASE, sizeof(u32), vpd);
  528. if (ret < 0)
  529. goto out;
  530. addr = *vpd == 0x82 ? VPD_BASE : VPD_BASE_OLD;
  531. ret = pci_read_vpd(adapter->pdev, addr, VPD_LEN, vpd);
  532. if (ret < 0)
  533. goto out;
  534. if (vpd[0] != PCI_VPD_LRDT_ID_STRING) {
  535. dev_err(adapter->pdev_dev, "missing VPD ID string\n");
  536. ret = -EINVAL;
  537. goto out;
  538. }
  539. id_len = pci_vpd_lrdt_size(vpd);
  540. if (id_len > ID_LEN)
  541. id_len = ID_LEN;
  542. i = pci_vpd_find_tag(vpd, 0, VPD_LEN, PCI_VPD_LRDT_RO_DATA);
  543. if (i < 0) {
  544. dev_err(adapter->pdev_dev, "missing VPD-R section\n");
  545. ret = -EINVAL;
  546. goto out;
  547. }
  548. vpdr_len = pci_vpd_lrdt_size(&vpd[i]);
  549. kw_offset = i + PCI_VPD_LRDT_TAG_SIZE;
  550. if (vpdr_len + kw_offset > VPD_LEN) {
  551. dev_err(adapter->pdev_dev, "bad VPD-R length %u\n", vpdr_len);
  552. ret = -EINVAL;
  553. goto out;
  554. }
  555. #define FIND_VPD_KW(var, name) do { \
  556. var = pci_vpd_find_info_keyword(vpd, kw_offset, vpdr_len, name); \
  557. if (var < 0) { \
  558. dev_err(adapter->pdev_dev, "missing VPD keyword " name "\n"); \
  559. ret = -EINVAL; \
  560. goto out; \
  561. } \
  562. var += PCI_VPD_INFO_FLD_HDR_SIZE; \
  563. } while (0)
  564. FIND_VPD_KW(i, "RV");
  565. for (csum = 0; i >= 0; i--)
  566. csum += vpd[i];
  567. if (csum) {
  568. dev_err(adapter->pdev_dev,
  569. "corrupted VPD EEPROM, actual csum %u\n", csum);
  570. ret = -EINVAL;
  571. goto out;
  572. }
  573. FIND_VPD_KW(ec, "EC");
  574. FIND_VPD_KW(sn, "SN");
  575. FIND_VPD_KW(pn, "PN");
  576. #undef FIND_VPD_KW
  577. memcpy(p->id, vpd + PCI_VPD_LRDT_TAG_SIZE, id_len);
  578. strim(p->id);
  579. memcpy(p->ec, vpd + ec, EC_LEN);
  580. strim(p->ec);
  581. i = pci_vpd_info_field_size(vpd + sn - PCI_VPD_INFO_FLD_HDR_SIZE);
  582. memcpy(p->sn, vpd + sn, min(i, SERNUM_LEN));
  583. strim(p->sn);
  584. memcpy(p->pn, vpd + pn, min(i, PN_LEN));
  585. strim(p->pn);
  586. /*
  587. * Ask firmware for the Core Clock since it knows how to translate the
  588. * Reference Clock ('V2') VPD field into a Core Clock value ...
  589. */
  590. cclk_param = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
  591. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CCLK));
  592. ret = t4_query_params(adapter, adapter->mbox, 0, 0,
  593. 1, &cclk_param, &cclk_val);
  594. out:
  595. vfree(vpd);
  596. if (ret)
  597. return ret;
  598. p->cclk = cclk_val;
  599. return 0;
  600. }
  601. /* serial flash and firmware constants */
  602. enum {
  603. SF_ATTEMPTS = 10, /* max retries for SF operations */
  604. /* flash command opcodes */
  605. SF_PROG_PAGE = 2, /* program page */
  606. SF_WR_DISABLE = 4, /* disable writes */
  607. SF_RD_STATUS = 5, /* read status register */
  608. SF_WR_ENABLE = 6, /* enable writes */
  609. SF_RD_DATA_FAST = 0xb, /* read flash */
  610. SF_RD_ID = 0x9f, /* read ID */
  611. SF_ERASE_SECTOR = 0xd8, /* erase sector */
  612. FW_MAX_SIZE = 16 * SF_SEC_SIZE,
  613. };
  614. /**
  615. * sf1_read - read data from the serial flash
  616. * @adapter: the adapter
  617. * @byte_cnt: number of bytes to read
  618. * @cont: whether another operation will be chained
  619. * @lock: whether to lock SF for PL access only
  620. * @valp: where to store the read data
  621. *
  622. * Reads up to 4 bytes of data from the serial flash. The location of
  623. * the read needs to be specified prior to calling this by issuing the
  624. * appropriate commands to the serial flash.
  625. */
  626. static int sf1_read(struct adapter *adapter, unsigned int byte_cnt, int cont,
  627. int lock, u32 *valp)
  628. {
  629. int ret;
  630. if (!byte_cnt || byte_cnt > 4)
  631. return -EINVAL;
  632. if (t4_read_reg(adapter, SF_OP) & SF_BUSY)
  633. return -EBUSY;
  634. cont = cont ? SF_CONT : 0;
  635. lock = lock ? SF_LOCK : 0;
  636. t4_write_reg(adapter, SF_OP, lock | cont | BYTECNT(byte_cnt - 1));
  637. ret = t4_wait_op_done(adapter, SF_OP, SF_BUSY, 0, SF_ATTEMPTS, 5);
  638. if (!ret)
  639. *valp = t4_read_reg(adapter, SF_DATA);
  640. return ret;
  641. }
  642. /**
  643. * sf1_write - write data to the serial flash
  644. * @adapter: the adapter
  645. * @byte_cnt: number of bytes to write
  646. * @cont: whether another operation will be chained
  647. * @lock: whether to lock SF for PL access only
  648. * @val: value to write
  649. *
  650. * Writes up to 4 bytes of data to the serial flash. The location of
  651. * the write needs to be specified prior to calling this by issuing the
  652. * appropriate commands to the serial flash.
  653. */
  654. static int sf1_write(struct adapter *adapter, unsigned int byte_cnt, int cont,
  655. int lock, u32 val)
  656. {
  657. if (!byte_cnt || byte_cnt > 4)
  658. return -EINVAL;
  659. if (t4_read_reg(adapter, SF_OP) & SF_BUSY)
  660. return -EBUSY;
  661. cont = cont ? SF_CONT : 0;
  662. lock = lock ? SF_LOCK : 0;
  663. t4_write_reg(adapter, SF_DATA, val);
  664. t4_write_reg(adapter, SF_OP, lock |
  665. cont | BYTECNT(byte_cnt - 1) | OP_WR);
  666. return t4_wait_op_done(adapter, SF_OP, SF_BUSY, 0, SF_ATTEMPTS, 5);
  667. }
  668. /**
  669. * flash_wait_op - wait for a flash operation to complete
  670. * @adapter: the adapter
  671. * @attempts: max number of polls of the status register
  672. * @delay: delay between polls in ms
  673. *
  674. * Wait for a flash operation to complete by polling the status register.
  675. */
  676. static int flash_wait_op(struct adapter *adapter, int attempts, int delay)
  677. {
  678. int ret;
  679. u32 status;
  680. while (1) {
  681. if ((ret = sf1_write(adapter, 1, 1, 1, SF_RD_STATUS)) != 0 ||
  682. (ret = sf1_read(adapter, 1, 0, 1, &status)) != 0)
  683. return ret;
  684. if (!(status & 1))
  685. return 0;
  686. if (--attempts == 0)
  687. return -EAGAIN;
  688. if (delay)
  689. msleep(delay);
  690. }
  691. }
  692. /**
  693. * t4_read_flash - read words from serial flash
  694. * @adapter: the adapter
  695. * @addr: the start address for the read
  696. * @nwords: how many 32-bit words to read
  697. * @data: where to store the read data
  698. * @byte_oriented: whether to store data as bytes or as words
  699. *
  700. * Read the specified number of 32-bit words from the serial flash.
  701. * If @byte_oriented is set the read data is stored as a byte array
  702. * (i.e., big-endian), otherwise as 32-bit words in the platform's
  703. * natural endianess.
  704. */
  705. static int t4_read_flash(struct adapter *adapter, unsigned int addr,
  706. unsigned int nwords, u32 *data, int byte_oriented)
  707. {
  708. int ret;
  709. if (addr + nwords * sizeof(u32) > adapter->params.sf_size || (addr & 3))
  710. return -EINVAL;
  711. addr = swab32(addr) | SF_RD_DATA_FAST;
  712. if ((ret = sf1_write(adapter, 4, 1, 0, addr)) != 0 ||
  713. (ret = sf1_read(adapter, 1, 1, 0, data)) != 0)
  714. return ret;
  715. for ( ; nwords; nwords--, data++) {
  716. ret = sf1_read(adapter, 4, nwords > 1, nwords == 1, data);
  717. if (nwords == 1)
  718. t4_write_reg(adapter, SF_OP, 0); /* unlock SF */
  719. if (ret)
  720. return ret;
  721. if (byte_oriented)
  722. *data = (__force __u32) (htonl(*data));
  723. }
  724. return 0;
  725. }
  726. /**
  727. * t4_write_flash - write up to a page of data to the serial flash
  728. * @adapter: the adapter
  729. * @addr: the start address to write
  730. * @n: length of data to write in bytes
  731. * @data: the data to write
  732. *
  733. * Writes up to a page of data (256 bytes) to the serial flash starting
  734. * at the given address. All the data must be written to the same page.
  735. */
  736. static int t4_write_flash(struct adapter *adapter, unsigned int addr,
  737. unsigned int n, const u8 *data)
  738. {
  739. int ret;
  740. u32 buf[64];
  741. unsigned int i, c, left, val, offset = addr & 0xff;
  742. if (addr >= adapter->params.sf_size || offset + n > SF_PAGE_SIZE)
  743. return -EINVAL;
  744. val = swab32(addr) | SF_PROG_PAGE;
  745. if ((ret = sf1_write(adapter, 1, 0, 1, SF_WR_ENABLE)) != 0 ||
  746. (ret = sf1_write(adapter, 4, 1, 1, val)) != 0)
  747. goto unlock;
  748. for (left = n; left; left -= c) {
  749. c = min(left, 4U);
  750. for (val = 0, i = 0; i < c; ++i)
  751. val = (val << 8) + *data++;
  752. ret = sf1_write(adapter, c, c != left, 1, val);
  753. if (ret)
  754. goto unlock;
  755. }
  756. ret = flash_wait_op(adapter, 8, 1);
  757. if (ret)
  758. goto unlock;
  759. t4_write_reg(adapter, SF_OP, 0); /* unlock SF */
  760. /* Read the page to verify the write succeeded */
  761. ret = t4_read_flash(adapter, addr & ~0xff, ARRAY_SIZE(buf), buf, 1);
  762. if (ret)
  763. return ret;
  764. if (memcmp(data - n, (u8 *)buf + offset, n)) {
  765. dev_err(adapter->pdev_dev,
  766. "failed to correctly write the flash page at %#x\n",
  767. addr);
  768. return -EIO;
  769. }
  770. return 0;
  771. unlock:
  772. t4_write_reg(adapter, SF_OP, 0); /* unlock SF */
  773. return ret;
  774. }
  775. /**
  776. * t4_get_fw_version - read the firmware version
  777. * @adapter: the adapter
  778. * @vers: where to place the version
  779. *
  780. * Reads the FW version from flash.
  781. */
  782. int t4_get_fw_version(struct adapter *adapter, u32 *vers)
  783. {
  784. return t4_read_flash(adapter, FLASH_FW_START +
  785. offsetof(struct fw_hdr, fw_ver), 1,
  786. vers, 0);
  787. }
  788. /**
  789. * t4_get_tp_version - read the TP microcode version
  790. * @adapter: the adapter
  791. * @vers: where to place the version
  792. *
  793. * Reads the TP microcode version from flash.
  794. */
  795. int t4_get_tp_version(struct adapter *adapter, u32 *vers)
  796. {
  797. return t4_read_flash(adapter, FLASH_FW_START +
  798. offsetof(struct fw_hdr, tp_microcode_ver),
  799. 1, vers, 0);
  800. }
  801. /* Is the given firmware API compatible with the one the driver was compiled
  802. * with?
  803. */
  804. static int fw_compatible(const struct fw_hdr *hdr1, const struct fw_hdr *hdr2)
  805. {
  806. /* short circuit if it's the exact same firmware version */
  807. if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
  808. return 1;
  809. #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
  810. if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
  811. SAME_INTF(ri) && SAME_INTF(iscsi) && SAME_INTF(fcoe))
  812. return 1;
  813. #undef SAME_INTF
  814. return 0;
  815. }
  816. /* The firmware in the filesystem is usable, but should it be installed?
  817. * This routine explains itself in detail if it indicates the filesystem
  818. * firmware should be installed.
  819. */
  820. static int should_install_fs_fw(struct adapter *adap, int card_fw_usable,
  821. int k, int c)
  822. {
  823. const char *reason;
  824. if (!card_fw_usable) {
  825. reason = "incompatible or unusable";
  826. goto install;
  827. }
  828. if (k > c) {
  829. reason = "older than the version supported with this driver";
  830. goto install;
  831. }
  832. return 0;
  833. install:
  834. dev_err(adap->pdev_dev, "firmware on card (%u.%u.%u.%u) is %s, "
  835. "installing firmware %u.%u.%u.%u on card.\n",
  836. FW_HDR_FW_VER_MAJOR_GET(c), FW_HDR_FW_VER_MINOR_GET(c),
  837. FW_HDR_FW_VER_MICRO_GET(c), FW_HDR_FW_VER_BUILD_GET(c), reason,
  838. FW_HDR_FW_VER_MAJOR_GET(k), FW_HDR_FW_VER_MINOR_GET(k),
  839. FW_HDR_FW_VER_MICRO_GET(k), FW_HDR_FW_VER_BUILD_GET(k));
  840. return 1;
  841. }
  842. int t4_prep_fw(struct adapter *adap, struct fw_info *fw_info,
  843. const u8 *fw_data, unsigned int fw_size,
  844. struct fw_hdr *card_fw, enum dev_state state,
  845. int *reset)
  846. {
  847. int ret, card_fw_usable, fs_fw_usable;
  848. const struct fw_hdr *fs_fw;
  849. const struct fw_hdr *drv_fw;
  850. drv_fw = &fw_info->fw_hdr;
  851. /* Read the header of the firmware on the card */
  852. ret = -t4_read_flash(adap, FLASH_FW_START,
  853. sizeof(*card_fw) / sizeof(uint32_t),
  854. (uint32_t *)card_fw, 1);
  855. if (ret == 0) {
  856. card_fw_usable = fw_compatible(drv_fw, (const void *)card_fw);
  857. } else {
  858. dev_err(adap->pdev_dev,
  859. "Unable to read card's firmware header: %d\n", ret);
  860. card_fw_usable = 0;
  861. }
  862. if (fw_data != NULL) {
  863. fs_fw = (const void *)fw_data;
  864. fs_fw_usable = fw_compatible(drv_fw, fs_fw);
  865. } else {
  866. fs_fw = NULL;
  867. fs_fw_usable = 0;
  868. }
  869. if (card_fw_usable && card_fw->fw_ver == drv_fw->fw_ver &&
  870. (!fs_fw_usable || fs_fw->fw_ver == drv_fw->fw_ver)) {
  871. /* Common case: the firmware on the card is an exact match and
  872. * the filesystem one is an exact match too, or the filesystem
  873. * one is absent/incompatible.
  874. */
  875. } else if (fs_fw_usable && state == DEV_STATE_UNINIT &&
  876. should_install_fs_fw(adap, card_fw_usable,
  877. be32_to_cpu(fs_fw->fw_ver),
  878. be32_to_cpu(card_fw->fw_ver))) {
  879. ret = -t4_fw_upgrade(adap, adap->mbox, fw_data,
  880. fw_size, 0);
  881. if (ret != 0) {
  882. dev_err(adap->pdev_dev,
  883. "failed to install firmware: %d\n", ret);
  884. goto bye;
  885. }
  886. /* Installed successfully, update the cached header too. */
  887. memcpy(card_fw, fs_fw, sizeof(*card_fw));
  888. card_fw_usable = 1;
  889. *reset = 0; /* already reset as part of load_fw */
  890. }
  891. if (!card_fw_usable) {
  892. uint32_t d, c, k;
  893. d = be32_to_cpu(drv_fw->fw_ver);
  894. c = be32_to_cpu(card_fw->fw_ver);
  895. k = fs_fw ? be32_to_cpu(fs_fw->fw_ver) : 0;
  896. dev_err(adap->pdev_dev, "Cannot find a usable firmware: "
  897. "chip state %d, "
  898. "driver compiled with %d.%d.%d.%d, "
  899. "card has %d.%d.%d.%d, filesystem has %d.%d.%d.%d\n",
  900. state,
  901. FW_HDR_FW_VER_MAJOR_GET(d), FW_HDR_FW_VER_MINOR_GET(d),
  902. FW_HDR_FW_VER_MICRO_GET(d), FW_HDR_FW_VER_BUILD_GET(d),
  903. FW_HDR_FW_VER_MAJOR_GET(c), FW_HDR_FW_VER_MINOR_GET(c),
  904. FW_HDR_FW_VER_MICRO_GET(c), FW_HDR_FW_VER_BUILD_GET(c),
  905. FW_HDR_FW_VER_MAJOR_GET(k), FW_HDR_FW_VER_MINOR_GET(k),
  906. FW_HDR_FW_VER_MICRO_GET(k), FW_HDR_FW_VER_BUILD_GET(k));
  907. ret = EINVAL;
  908. goto bye;
  909. }
  910. /* We're using whatever's on the card and it's known to be good. */
  911. adap->params.fw_vers = be32_to_cpu(card_fw->fw_ver);
  912. adap->params.tp_vers = be32_to_cpu(card_fw->tp_microcode_ver);
  913. bye:
  914. return ret;
  915. }
  916. /**
  917. * t4_flash_erase_sectors - erase a range of flash sectors
  918. * @adapter: the adapter
  919. * @start: the first sector to erase
  920. * @end: the last sector to erase
  921. *
  922. * Erases the sectors in the given inclusive range.
  923. */
  924. static int t4_flash_erase_sectors(struct adapter *adapter, int start, int end)
  925. {
  926. int ret = 0;
  927. while (start <= end) {
  928. if ((ret = sf1_write(adapter, 1, 0, 1, SF_WR_ENABLE)) != 0 ||
  929. (ret = sf1_write(adapter, 4, 0, 1,
  930. SF_ERASE_SECTOR | (start << 8))) != 0 ||
  931. (ret = flash_wait_op(adapter, 14, 500)) != 0) {
  932. dev_err(adapter->pdev_dev,
  933. "erase of flash sector %d failed, error %d\n",
  934. start, ret);
  935. break;
  936. }
  937. start++;
  938. }
  939. t4_write_reg(adapter, SF_OP, 0); /* unlock SF */
  940. return ret;
  941. }
  942. /**
  943. * t4_flash_cfg_addr - return the address of the flash configuration file
  944. * @adapter: the adapter
  945. *
  946. * Return the address within the flash where the Firmware Configuration
  947. * File is stored.
  948. */
  949. unsigned int t4_flash_cfg_addr(struct adapter *adapter)
  950. {
  951. if (adapter->params.sf_size == 0x100000)
  952. return FLASH_FPGA_CFG_START;
  953. else
  954. return FLASH_CFG_START;
  955. }
  956. /**
  957. * t4_load_fw - download firmware
  958. * @adap: the adapter
  959. * @fw_data: the firmware image to write
  960. * @size: image size
  961. *
  962. * Write the supplied firmware image to the card's serial flash.
  963. */
  964. int t4_load_fw(struct adapter *adap, const u8 *fw_data, unsigned int size)
  965. {
  966. u32 csum;
  967. int ret, addr;
  968. unsigned int i;
  969. u8 first_page[SF_PAGE_SIZE];
  970. const __be32 *p = (const __be32 *)fw_data;
  971. const struct fw_hdr *hdr = (const struct fw_hdr *)fw_data;
  972. unsigned int sf_sec_size = adap->params.sf_size / adap->params.sf_nsec;
  973. unsigned int fw_img_start = adap->params.sf_fw_start;
  974. unsigned int fw_start_sec = fw_img_start / sf_sec_size;
  975. if (!size) {
  976. dev_err(adap->pdev_dev, "FW image has no data\n");
  977. return -EINVAL;
  978. }
  979. if (size & 511) {
  980. dev_err(adap->pdev_dev,
  981. "FW image size not multiple of 512 bytes\n");
  982. return -EINVAL;
  983. }
  984. if (ntohs(hdr->len512) * 512 != size) {
  985. dev_err(adap->pdev_dev,
  986. "FW image size differs from size in FW header\n");
  987. return -EINVAL;
  988. }
  989. if (size > FW_MAX_SIZE) {
  990. dev_err(adap->pdev_dev, "FW image too large, max is %u bytes\n",
  991. FW_MAX_SIZE);
  992. return -EFBIG;
  993. }
  994. for (csum = 0, i = 0; i < size / sizeof(csum); i++)
  995. csum += ntohl(p[i]);
  996. if (csum != 0xffffffff) {
  997. dev_err(adap->pdev_dev,
  998. "corrupted firmware image, checksum %#x\n", csum);
  999. return -EINVAL;
  1000. }
  1001. i = DIV_ROUND_UP(size, sf_sec_size); /* # of sectors spanned */
  1002. ret = t4_flash_erase_sectors(adap, fw_start_sec, fw_start_sec + i - 1);
  1003. if (ret)
  1004. goto out;
  1005. /*
  1006. * We write the correct version at the end so the driver can see a bad
  1007. * version if the FW write fails. Start by writing a copy of the
  1008. * first page with a bad version.
  1009. */
  1010. memcpy(first_page, fw_data, SF_PAGE_SIZE);
  1011. ((struct fw_hdr *)first_page)->fw_ver = htonl(0xffffffff);
  1012. ret = t4_write_flash(adap, fw_img_start, SF_PAGE_SIZE, first_page);
  1013. if (ret)
  1014. goto out;
  1015. addr = fw_img_start;
  1016. for (size -= SF_PAGE_SIZE; size; size -= SF_PAGE_SIZE) {
  1017. addr += SF_PAGE_SIZE;
  1018. fw_data += SF_PAGE_SIZE;
  1019. ret = t4_write_flash(adap, addr, SF_PAGE_SIZE, fw_data);
  1020. if (ret)
  1021. goto out;
  1022. }
  1023. ret = t4_write_flash(adap,
  1024. fw_img_start + offsetof(struct fw_hdr, fw_ver),
  1025. sizeof(hdr->fw_ver), (const u8 *)&hdr->fw_ver);
  1026. out:
  1027. if (ret)
  1028. dev_err(adap->pdev_dev, "firmware download failed, error %d\n",
  1029. ret);
  1030. return ret;
  1031. }
  1032. #define ADVERT_MASK (FW_PORT_CAP_SPEED_100M | FW_PORT_CAP_SPEED_1G |\
  1033. FW_PORT_CAP_SPEED_10G | FW_PORT_CAP_SPEED_40G | \
  1034. FW_PORT_CAP_ANEG)
  1035. /**
  1036. * t4_link_start - apply link configuration to MAC/PHY
  1037. * @phy: the PHY to setup
  1038. * @mac: the MAC to setup
  1039. * @lc: the requested link configuration
  1040. *
  1041. * Set up a port's MAC and PHY according to a desired link configuration.
  1042. * - If the PHY can auto-negotiate first decide what to advertise, then
  1043. * enable/disable auto-negotiation as desired, and reset.
  1044. * - If the PHY does not auto-negotiate just reset it.
  1045. * - If auto-negotiation is off set the MAC to the proper speed/duplex/FC,
  1046. * otherwise do it later based on the outcome of auto-negotiation.
  1047. */
  1048. int t4_link_start(struct adapter *adap, unsigned int mbox, unsigned int port,
  1049. struct link_config *lc)
  1050. {
  1051. struct fw_port_cmd c;
  1052. unsigned int fc = 0, mdi = FW_PORT_MDI(FW_PORT_MDI_AUTO);
  1053. lc->link_ok = 0;
  1054. if (lc->requested_fc & PAUSE_RX)
  1055. fc |= FW_PORT_CAP_FC_RX;
  1056. if (lc->requested_fc & PAUSE_TX)
  1057. fc |= FW_PORT_CAP_FC_TX;
  1058. memset(&c, 0, sizeof(c));
  1059. c.op_to_portid = htonl(FW_CMD_OP(FW_PORT_CMD) | FW_CMD_REQUEST |
  1060. FW_CMD_EXEC | FW_PORT_CMD_PORTID(port));
  1061. c.action_to_len16 = htonl(FW_PORT_CMD_ACTION(FW_PORT_ACTION_L1_CFG) |
  1062. FW_LEN16(c));
  1063. if (!(lc->supported & FW_PORT_CAP_ANEG)) {
  1064. c.u.l1cfg.rcap = htonl((lc->supported & ADVERT_MASK) | fc);
  1065. lc->fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX);
  1066. } else if (lc->autoneg == AUTONEG_DISABLE) {
  1067. c.u.l1cfg.rcap = htonl(lc->requested_speed | fc | mdi);
  1068. lc->fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX);
  1069. } else
  1070. c.u.l1cfg.rcap = htonl(lc->advertising | fc | mdi);
  1071. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  1072. }
  1073. /**
  1074. * t4_restart_aneg - restart autonegotiation
  1075. * @adap: the adapter
  1076. * @mbox: mbox to use for the FW command
  1077. * @port: the port id
  1078. *
  1079. * Restarts autonegotiation for the selected port.
  1080. */
  1081. int t4_restart_aneg(struct adapter *adap, unsigned int mbox, unsigned int port)
  1082. {
  1083. struct fw_port_cmd c;
  1084. memset(&c, 0, sizeof(c));
  1085. c.op_to_portid = htonl(FW_CMD_OP(FW_PORT_CMD) | FW_CMD_REQUEST |
  1086. FW_CMD_EXEC | FW_PORT_CMD_PORTID(port));
  1087. c.action_to_len16 = htonl(FW_PORT_CMD_ACTION(FW_PORT_ACTION_L1_CFG) |
  1088. FW_LEN16(c));
  1089. c.u.l1cfg.rcap = htonl(FW_PORT_CAP_ANEG);
  1090. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  1091. }
  1092. typedef void (*int_handler_t)(struct adapter *adap);
  1093. struct intr_info {
  1094. unsigned int mask; /* bits to check in interrupt status */
  1095. const char *msg; /* message to print or NULL */
  1096. short stat_idx; /* stat counter to increment or -1 */
  1097. unsigned short fatal; /* whether the condition reported is fatal */
  1098. int_handler_t int_handler; /* platform-specific int handler */
  1099. };
  1100. /**
  1101. * t4_handle_intr_status - table driven interrupt handler
  1102. * @adapter: the adapter that generated the interrupt
  1103. * @reg: the interrupt status register to process
  1104. * @acts: table of interrupt actions
  1105. *
  1106. * A table driven interrupt handler that applies a set of masks to an
  1107. * interrupt status word and performs the corresponding actions if the
  1108. * interrupts described by the mask have occurred. The actions include
  1109. * optionally emitting a warning or alert message. The table is terminated
  1110. * by an entry specifying mask 0. Returns the number of fatal interrupt
  1111. * conditions.
  1112. */
  1113. static int t4_handle_intr_status(struct adapter *adapter, unsigned int reg,
  1114. const struct intr_info *acts)
  1115. {
  1116. int fatal = 0;
  1117. unsigned int mask = 0;
  1118. unsigned int status = t4_read_reg(adapter, reg);
  1119. for ( ; acts->mask; ++acts) {
  1120. if (!(status & acts->mask))
  1121. continue;
  1122. if (acts->fatal) {
  1123. fatal++;
  1124. dev_alert(adapter->pdev_dev, "%s (0x%x)\n", acts->msg,
  1125. status & acts->mask);
  1126. } else if (acts->msg && printk_ratelimit())
  1127. dev_warn(adapter->pdev_dev, "%s (0x%x)\n", acts->msg,
  1128. status & acts->mask);
  1129. if (acts->int_handler)
  1130. acts->int_handler(adapter);
  1131. mask |= acts->mask;
  1132. }
  1133. status &= mask;
  1134. if (status) /* clear processed interrupts */
  1135. t4_write_reg(adapter, reg, status);
  1136. return fatal;
  1137. }
  1138. /*
  1139. * Interrupt handler for the PCIE module.
  1140. */
  1141. static void pcie_intr_handler(struct adapter *adapter)
  1142. {
  1143. static const struct intr_info sysbus_intr_info[] = {
  1144. { RNPP, "RXNP array parity error", -1, 1 },
  1145. { RPCP, "RXPC array parity error", -1, 1 },
  1146. { RCIP, "RXCIF array parity error", -1, 1 },
  1147. { RCCP, "Rx completions control array parity error", -1, 1 },
  1148. { RFTP, "RXFT array parity error", -1, 1 },
  1149. { 0 }
  1150. };
  1151. static const struct intr_info pcie_port_intr_info[] = {
  1152. { TPCP, "TXPC array parity error", -1, 1 },
  1153. { TNPP, "TXNP array parity error", -1, 1 },
  1154. { TFTP, "TXFT array parity error", -1, 1 },
  1155. { TCAP, "TXCA array parity error", -1, 1 },
  1156. { TCIP, "TXCIF array parity error", -1, 1 },
  1157. { RCAP, "RXCA array parity error", -1, 1 },
  1158. { OTDD, "outbound request TLP discarded", -1, 1 },
  1159. { RDPE, "Rx data parity error", -1, 1 },
  1160. { TDUE, "Tx uncorrectable data error", -1, 1 },
  1161. { 0 }
  1162. };
  1163. static const struct intr_info pcie_intr_info[] = {
  1164. { MSIADDRLPERR, "MSI AddrL parity error", -1, 1 },
  1165. { MSIADDRHPERR, "MSI AddrH parity error", -1, 1 },
  1166. { MSIDATAPERR, "MSI data parity error", -1, 1 },
  1167. { MSIXADDRLPERR, "MSI-X AddrL parity error", -1, 1 },
  1168. { MSIXADDRHPERR, "MSI-X AddrH parity error", -1, 1 },
  1169. { MSIXDATAPERR, "MSI-X data parity error", -1, 1 },
  1170. { MSIXDIPERR, "MSI-X DI parity error", -1, 1 },
  1171. { PIOCPLPERR, "PCI PIO completion FIFO parity error", -1, 1 },
  1172. { PIOREQPERR, "PCI PIO request FIFO parity error", -1, 1 },
  1173. { TARTAGPERR, "PCI PCI target tag FIFO parity error", -1, 1 },
  1174. { CCNTPERR, "PCI CMD channel count parity error", -1, 1 },
  1175. { CREQPERR, "PCI CMD channel request parity error", -1, 1 },
  1176. { CRSPPERR, "PCI CMD channel response parity error", -1, 1 },
  1177. { DCNTPERR, "PCI DMA channel count parity error", -1, 1 },
  1178. { DREQPERR, "PCI DMA channel request parity error", -1, 1 },
  1179. { DRSPPERR, "PCI DMA channel response parity error", -1, 1 },
  1180. { HCNTPERR, "PCI HMA channel count parity error", -1, 1 },
  1181. { HREQPERR, "PCI HMA channel request parity error", -1, 1 },
  1182. { HRSPPERR, "PCI HMA channel response parity error", -1, 1 },
  1183. { CFGSNPPERR, "PCI config snoop FIFO parity error", -1, 1 },
  1184. { FIDPERR, "PCI FID parity error", -1, 1 },
  1185. { INTXCLRPERR, "PCI INTx clear parity error", -1, 1 },
  1186. { MATAGPERR, "PCI MA tag parity error", -1, 1 },
  1187. { PIOTAGPERR, "PCI PIO tag parity error", -1, 1 },
  1188. { RXCPLPERR, "PCI Rx completion parity error", -1, 1 },
  1189. { RXWRPERR, "PCI Rx write parity error", -1, 1 },
  1190. { RPLPERR, "PCI replay buffer parity error", -1, 1 },
  1191. { PCIESINT, "PCI core secondary fault", -1, 1 },
  1192. { PCIEPINT, "PCI core primary fault", -1, 1 },
  1193. { UNXSPLCPLERR, "PCI unexpected split completion error", -1, 0 },
  1194. { 0 }
  1195. };
  1196. static struct intr_info t5_pcie_intr_info[] = {
  1197. { MSTGRPPERR, "Master Response Read Queue parity error",
  1198. -1, 1 },
  1199. { MSTTIMEOUTPERR, "Master Timeout FIFO parity error", -1, 1 },
  1200. { MSIXSTIPERR, "MSI-X STI SRAM parity error", -1, 1 },
  1201. { MSIXADDRLPERR, "MSI-X AddrL parity error", -1, 1 },
  1202. { MSIXADDRHPERR, "MSI-X AddrH parity error", -1, 1 },
  1203. { MSIXDATAPERR, "MSI-X data parity error", -1, 1 },
  1204. { MSIXDIPERR, "MSI-X DI parity error", -1, 1 },
  1205. { PIOCPLGRPPERR, "PCI PIO completion Group FIFO parity error",
  1206. -1, 1 },
  1207. { PIOREQGRPPERR, "PCI PIO request Group FIFO parity error",
  1208. -1, 1 },
  1209. { TARTAGPERR, "PCI PCI target tag FIFO parity error", -1, 1 },
  1210. { MSTTAGQPERR, "PCI master tag queue parity error", -1, 1 },
  1211. { CREQPERR, "PCI CMD channel request parity error", -1, 1 },
  1212. { CRSPPERR, "PCI CMD channel response parity error", -1, 1 },
  1213. { DREQWRPERR, "PCI DMA channel write request parity error",
  1214. -1, 1 },
  1215. { DREQPERR, "PCI DMA channel request parity error", -1, 1 },
  1216. { DRSPPERR, "PCI DMA channel response parity error", -1, 1 },
  1217. { HREQWRPERR, "PCI HMA channel count parity error", -1, 1 },
  1218. { HREQPERR, "PCI HMA channel request parity error", -1, 1 },
  1219. { HRSPPERR, "PCI HMA channel response parity error", -1, 1 },
  1220. { CFGSNPPERR, "PCI config snoop FIFO parity error", -1, 1 },
  1221. { FIDPERR, "PCI FID parity error", -1, 1 },
  1222. { VFIDPERR, "PCI INTx clear parity error", -1, 1 },
  1223. { MAGRPPERR, "PCI MA group FIFO parity error", -1, 1 },
  1224. { PIOTAGPERR, "PCI PIO tag parity error", -1, 1 },
  1225. { IPRXHDRGRPPERR, "PCI IP Rx header group parity error",
  1226. -1, 1 },
  1227. { IPRXDATAGRPPERR, "PCI IP Rx data group parity error", -1, 1 },
  1228. { RPLPERR, "PCI IP replay buffer parity error", -1, 1 },
  1229. { IPSOTPERR, "PCI IP SOT buffer parity error", -1, 1 },
  1230. { TRGT1GRPPERR, "PCI TRGT1 group FIFOs parity error", -1, 1 },
  1231. { READRSPERR, "Outbound read error", -1, 0 },
  1232. { 0 }
  1233. };
  1234. int fat;
  1235. fat = t4_handle_intr_status(adapter,
  1236. PCIE_CORE_UTL_SYSTEM_BUS_AGENT_STATUS,
  1237. sysbus_intr_info) +
  1238. t4_handle_intr_status(adapter,
  1239. PCIE_CORE_UTL_PCI_EXPRESS_PORT_STATUS,
  1240. pcie_port_intr_info) +
  1241. t4_handle_intr_status(adapter, PCIE_INT_CAUSE,
  1242. is_t4(adapter->params.chip) ?
  1243. pcie_intr_info : t5_pcie_intr_info);
  1244. if (fat)
  1245. t4_fatal_err(adapter);
  1246. }
  1247. /*
  1248. * TP interrupt handler.
  1249. */
  1250. static void tp_intr_handler(struct adapter *adapter)
  1251. {
  1252. static const struct intr_info tp_intr_info[] = {
  1253. { 0x3fffffff, "TP parity error", -1, 1 },
  1254. { FLMTXFLSTEMPTY, "TP out of Tx pages", -1, 1 },
  1255. { 0 }
  1256. };
  1257. if (t4_handle_intr_status(adapter, TP_INT_CAUSE, tp_intr_info))
  1258. t4_fatal_err(adapter);
  1259. }
  1260. /*
  1261. * SGE interrupt handler.
  1262. */
  1263. static void sge_intr_handler(struct adapter *adapter)
  1264. {
  1265. u64 v;
  1266. static const struct intr_info sge_intr_info[] = {
  1267. { ERR_CPL_EXCEED_IQE_SIZE,
  1268. "SGE received CPL exceeding IQE size", -1, 1 },
  1269. { ERR_INVALID_CIDX_INC,
  1270. "SGE GTS CIDX increment too large", -1, 0 },
  1271. { ERR_CPL_OPCODE_0, "SGE received 0-length CPL", -1, 0 },
  1272. { DBFIFO_LP_INT, NULL, -1, 0, t4_db_full },
  1273. { DBFIFO_HP_INT, NULL, -1, 0, t4_db_full },
  1274. { ERR_DROPPED_DB, NULL, -1, 0, t4_db_dropped },
  1275. { ERR_DATA_CPL_ON_HIGH_QID1 | ERR_DATA_CPL_ON_HIGH_QID0,
  1276. "SGE IQID > 1023 received CPL for FL", -1, 0 },
  1277. { ERR_BAD_DB_PIDX3, "SGE DBP 3 pidx increment too large", -1,
  1278. 0 },
  1279. { ERR_BAD_DB_PIDX2, "SGE DBP 2 pidx increment too large", -1,
  1280. 0 },
  1281. { ERR_BAD_DB_PIDX1, "SGE DBP 1 pidx increment too large", -1,
  1282. 0 },
  1283. { ERR_BAD_DB_PIDX0, "SGE DBP 0 pidx increment too large", -1,
  1284. 0 },
  1285. { ERR_ING_CTXT_PRIO,
  1286. "SGE too many priority ingress contexts", -1, 0 },
  1287. { ERR_EGR_CTXT_PRIO,
  1288. "SGE too many priority egress contexts", -1, 0 },
  1289. { INGRESS_SIZE_ERR, "SGE illegal ingress QID", -1, 0 },
  1290. { EGRESS_SIZE_ERR, "SGE illegal egress QID", -1, 0 },
  1291. { 0 }
  1292. };
  1293. v = (u64)t4_read_reg(adapter, SGE_INT_CAUSE1) |
  1294. ((u64)t4_read_reg(adapter, SGE_INT_CAUSE2) << 32);
  1295. if (v) {
  1296. dev_alert(adapter->pdev_dev, "SGE parity error (%#llx)\n",
  1297. (unsigned long long)v);
  1298. t4_write_reg(adapter, SGE_INT_CAUSE1, v);
  1299. t4_write_reg(adapter, SGE_INT_CAUSE2, v >> 32);
  1300. }
  1301. if (t4_handle_intr_status(adapter, SGE_INT_CAUSE3, sge_intr_info) ||
  1302. v != 0)
  1303. t4_fatal_err(adapter);
  1304. }
  1305. /*
  1306. * CIM interrupt handler.
  1307. */
  1308. static void cim_intr_handler(struct adapter *adapter)
  1309. {
  1310. static const struct intr_info cim_intr_info[] = {
  1311. { PREFDROPINT, "CIM control register prefetch drop", -1, 1 },
  1312. { OBQPARERR, "CIM OBQ parity error", -1, 1 },
  1313. { IBQPARERR, "CIM IBQ parity error", -1, 1 },
  1314. { MBUPPARERR, "CIM mailbox uP parity error", -1, 1 },
  1315. { MBHOSTPARERR, "CIM mailbox host parity error", -1, 1 },
  1316. { TIEQINPARERRINT, "CIM TIEQ outgoing parity error", -1, 1 },
  1317. { TIEQOUTPARERRINT, "CIM TIEQ incoming parity error", -1, 1 },
  1318. { 0 }
  1319. };
  1320. static const struct intr_info cim_upintr_info[] = {
  1321. { RSVDSPACEINT, "CIM reserved space access", -1, 1 },
  1322. { ILLTRANSINT, "CIM illegal transaction", -1, 1 },
  1323. { ILLWRINT, "CIM illegal write", -1, 1 },
  1324. { ILLRDINT, "CIM illegal read", -1, 1 },
  1325. { ILLRDBEINT, "CIM illegal read BE", -1, 1 },
  1326. { ILLWRBEINT, "CIM illegal write BE", -1, 1 },
  1327. { SGLRDBOOTINT, "CIM single read from boot space", -1, 1 },
  1328. { SGLWRBOOTINT, "CIM single write to boot space", -1, 1 },
  1329. { BLKWRBOOTINT, "CIM block write to boot space", -1, 1 },
  1330. { SGLRDFLASHINT, "CIM single read from flash space", -1, 1 },
  1331. { SGLWRFLASHINT, "CIM single write to flash space", -1, 1 },
  1332. { BLKWRFLASHINT, "CIM block write to flash space", -1, 1 },
  1333. { SGLRDEEPROMINT, "CIM single EEPROM read", -1, 1 },
  1334. { SGLWREEPROMINT, "CIM single EEPROM write", -1, 1 },
  1335. { BLKRDEEPROMINT, "CIM block EEPROM read", -1, 1 },
  1336. { BLKWREEPROMINT, "CIM block EEPROM write", -1, 1 },
  1337. { SGLRDCTLINT , "CIM single read from CTL space", -1, 1 },
  1338. { SGLWRCTLINT , "CIM single write to CTL space", -1, 1 },
  1339. { BLKRDCTLINT , "CIM block read from CTL space", -1, 1 },
  1340. { BLKWRCTLINT , "CIM block write to CTL space", -1, 1 },
  1341. { SGLRDPLINT , "CIM single read from PL space", -1, 1 },
  1342. { SGLWRPLINT , "CIM single write to PL space", -1, 1 },
  1343. { BLKRDPLINT , "CIM block read from PL space", -1, 1 },
  1344. { BLKWRPLINT , "CIM block write to PL space", -1, 1 },
  1345. { REQOVRLOOKUPINT , "CIM request FIFO overwrite", -1, 1 },
  1346. { RSPOVRLOOKUPINT , "CIM response FIFO overwrite", -1, 1 },
  1347. { TIMEOUTINT , "CIM PIF timeout", -1, 1 },
  1348. { TIMEOUTMAINT , "CIM PIF MA timeout", -1, 1 },
  1349. { 0 }
  1350. };
  1351. int fat;
  1352. fat = t4_handle_intr_status(adapter, CIM_HOST_INT_CAUSE,
  1353. cim_intr_info) +
  1354. t4_handle_intr_status(adapter, CIM_HOST_UPACC_INT_CAUSE,
  1355. cim_upintr_info);
  1356. if (fat)
  1357. t4_fatal_err(adapter);
  1358. }
  1359. /*
  1360. * ULP RX interrupt handler.
  1361. */
  1362. static void ulprx_intr_handler(struct adapter *adapter)
  1363. {
  1364. static const struct intr_info ulprx_intr_info[] = {
  1365. { 0x1800000, "ULPRX context error", -1, 1 },
  1366. { 0x7fffff, "ULPRX parity error", -1, 1 },
  1367. { 0 }
  1368. };
  1369. if (t4_handle_intr_status(adapter, ULP_RX_INT_CAUSE, ulprx_intr_info))
  1370. t4_fatal_err(adapter);
  1371. }
  1372. /*
  1373. * ULP TX interrupt handler.
  1374. */
  1375. static void ulptx_intr_handler(struct adapter *adapter)
  1376. {
  1377. static const struct intr_info ulptx_intr_info[] = {
  1378. { PBL_BOUND_ERR_CH3, "ULPTX channel 3 PBL out of bounds", -1,
  1379. 0 },
  1380. { PBL_BOUND_ERR_CH2, "ULPTX channel 2 PBL out of bounds", -1,
  1381. 0 },
  1382. { PBL_BOUND_ERR_CH1, "ULPTX channel 1 PBL out of bounds", -1,
  1383. 0 },
  1384. { PBL_BOUND_ERR_CH0, "ULPTX channel 0 PBL out of bounds", -1,
  1385. 0 },
  1386. { 0xfffffff, "ULPTX parity error", -1, 1 },
  1387. { 0 }
  1388. };
  1389. if (t4_handle_intr_status(adapter, ULP_TX_INT_CAUSE, ulptx_intr_info))
  1390. t4_fatal_err(adapter);
  1391. }
  1392. /*
  1393. * PM TX interrupt handler.
  1394. */
  1395. static void pmtx_intr_handler(struct adapter *adapter)
  1396. {
  1397. static const struct intr_info pmtx_intr_info[] = {
  1398. { PCMD_LEN_OVFL0, "PMTX channel 0 pcmd too large", -1, 1 },
  1399. { PCMD_LEN_OVFL1, "PMTX channel 1 pcmd too large", -1, 1 },
  1400. { PCMD_LEN_OVFL2, "PMTX channel 2 pcmd too large", -1, 1 },
  1401. { ZERO_C_CMD_ERROR, "PMTX 0-length pcmd", -1, 1 },
  1402. { PMTX_FRAMING_ERROR, "PMTX framing error", -1, 1 },
  1403. { OESPI_PAR_ERROR, "PMTX oespi parity error", -1, 1 },
  1404. { DB_OPTIONS_PAR_ERROR, "PMTX db_options parity error", -1, 1 },
  1405. { ICSPI_PAR_ERROR, "PMTX icspi parity error", -1, 1 },
  1406. { C_PCMD_PAR_ERROR, "PMTX c_pcmd parity error", -1, 1},
  1407. { 0 }
  1408. };
  1409. if (t4_handle_intr_status(adapter, PM_TX_INT_CAUSE, pmtx_intr_info))
  1410. t4_fatal_err(adapter);
  1411. }
  1412. /*
  1413. * PM RX interrupt handler.
  1414. */
  1415. static void pmrx_intr_handler(struct adapter *adapter)
  1416. {
  1417. static const struct intr_info pmrx_intr_info[] = {
  1418. { ZERO_E_CMD_ERROR, "PMRX 0-length pcmd", -1, 1 },
  1419. { PMRX_FRAMING_ERROR, "PMRX framing error", -1, 1 },
  1420. { OCSPI_PAR_ERROR, "PMRX ocspi parity error", -1, 1 },
  1421. { DB_OPTIONS_PAR_ERROR, "PMRX db_options parity error", -1, 1 },
  1422. { IESPI_PAR_ERROR, "PMRX iespi parity error", -1, 1 },
  1423. { E_PCMD_PAR_ERROR, "PMRX e_pcmd parity error", -1, 1},
  1424. { 0 }
  1425. };
  1426. if (t4_handle_intr_status(adapter, PM_RX_INT_CAUSE, pmrx_intr_info))
  1427. t4_fatal_err(adapter);
  1428. }
  1429. /*
  1430. * CPL switch interrupt handler.
  1431. */
  1432. static void cplsw_intr_handler(struct adapter *adapter)
  1433. {
  1434. static const struct intr_info cplsw_intr_info[] = {
  1435. { CIM_OP_MAP_PERR, "CPLSW CIM op_map parity error", -1, 1 },
  1436. { CIM_OVFL_ERROR, "CPLSW CIM overflow", -1, 1 },
  1437. { TP_FRAMING_ERROR, "CPLSW TP framing error", -1, 1 },
  1438. { SGE_FRAMING_ERROR, "CPLSW SGE framing error", -1, 1 },
  1439. { CIM_FRAMING_ERROR, "CPLSW CIM framing error", -1, 1 },
  1440. { ZERO_SWITCH_ERROR, "CPLSW no-switch error", -1, 1 },
  1441. { 0 }
  1442. };
  1443. if (t4_handle_intr_status(adapter, CPL_INTR_CAUSE, cplsw_intr_info))
  1444. t4_fatal_err(adapter);
  1445. }
  1446. /*
  1447. * LE interrupt handler.
  1448. */
  1449. static void le_intr_handler(struct adapter *adap)
  1450. {
  1451. static const struct intr_info le_intr_info[] = {
  1452. { LIPMISS, "LE LIP miss", -1, 0 },
  1453. { LIP0, "LE 0 LIP error", -1, 0 },
  1454. { PARITYERR, "LE parity error", -1, 1 },
  1455. { UNKNOWNCMD, "LE unknown command", -1, 1 },
  1456. { REQQPARERR, "LE request queue parity error", -1, 1 },
  1457. { 0 }
  1458. };
  1459. if (t4_handle_intr_status(adap, LE_DB_INT_CAUSE, le_intr_info))
  1460. t4_fatal_err(adap);
  1461. }
  1462. /*
  1463. * MPS interrupt handler.
  1464. */
  1465. static void mps_intr_handler(struct adapter *adapter)
  1466. {
  1467. static const struct intr_info mps_rx_intr_info[] = {
  1468. { 0xffffff, "MPS Rx parity error", -1, 1 },
  1469. { 0 }
  1470. };
  1471. static const struct intr_info mps_tx_intr_info[] = {
  1472. { TPFIFO, "MPS Tx TP FIFO parity error", -1, 1 },
  1473. { NCSIFIFO, "MPS Tx NC-SI FIFO parity error", -1, 1 },
  1474. { TXDATAFIFO, "MPS Tx data FIFO parity error", -1, 1 },
  1475. { TXDESCFIFO, "MPS Tx desc FIFO parity error", -1, 1 },
  1476. { BUBBLE, "MPS Tx underflow", -1, 1 },
  1477. { SECNTERR, "MPS Tx SOP/EOP error", -1, 1 },
  1478. { FRMERR, "MPS Tx framing error", -1, 1 },
  1479. { 0 }
  1480. };
  1481. static const struct intr_info mps_trc_intr_info[] = {
  1482. { FILTMEM, "MPS TRC filter parity error", -1, 1 },
  1483. { PKTFIFO, "MPS TRC packet FIFO parity error", -1, 1 },
  1484. { MISCPERR, "MPS TRC misc parity error", -1, 1 },
  1485. { 0 }
  1486. };
  1487. static const struct intr_info mps_stat_sram_intr_info[] = {
  1488. { 0x1fffff, "MPS statistics SRAM parity error", -1, 1 },
  1489. { 0 }
  1490. };
  1491. static const struct intr_info mps_stat_tx_intr_info[] = {
  1492. { 0xfffff, "MPS statistics Tx FIFO parity error", -1, 1 },
  1493. { 0 }
  1494. };
  1495. static const struct intr_info mps_stat_rx_intr_info[] = {
  1496. { 0xffffff, "MPS statistics Rx FIFO parity error", -1, 1 },
  1497. { 0 }
  1498. };
  1499. static const struct intr_info mps_cls_intr_info[] = {
  1500. { MATCHSRAM, "MPS match SRAM parity error", -1, 1 },
  1501. { MATCHTCAM, "MPS match TCAM parity error", -1, 1 },
  1502. { HASHSRAM, "MPS hash SRAM parity error", -1, 1 },
  1503. { 0 }
  1504. };
  1505. int fat;
  1506. fat = t4_handle_intr_status(adapter, MPS_RX_PERR_INT_CAUSE,
  1507. mps_rx_intr_info) +
  1508. t4_handle_intr_status(adapter, MPS_TX_INT_CAUSE,
  1509. mps_tx_intr_info) +
  1510. t4_handle_intr_status(adapter, MPS_TRC_INT_CAUSE,
  1511. mps_trc_intr_info) +
  1512. t4_handle_intr_status(adapter, MPS_STAT_PERR_INT_CAUSE_SRAM,
  1513. mps_stat_sram_intr_info) +
  1514. t4_handle_intr_status(adapter, MPS_STAT_PERR_INT_CAUSE_TX_FIFO,
  1515. mps_stat_tx_intr_info) +
  1516. t4_handle_intr_status(adapter, MPS_STAT_PERR_INT_CAUSE_RX_FIFO,
  1517. mps_stat_rx_intr_info) +
  1518. t4_handle_intr_status(adapter, MPS_CLS_INT_CAUSE,
  1519. mps_cls_intr_info);
  1520. t4_write_reg(adapter, MPS_INT_CAUSE, CLSINT | TRCINT |
  1521. RXINT | TXINT | STATINT);
  1522. t4_read_reg(adapter, MPS_INT_CAUSE); /* flush */
  1523. if (fat)
  1524. t4_fatal_err(adapter);
  1525. }
  1526. #define MEM_INT_MASK (PERR_INT_CAUSE | ECC_CE_INT_CAUSE | ECC_UE_INT_CAUSE)
  1527. /*
  1528. * EDC/MC interrupt handler.
  1529. */
  1530. static void mem_intr_handler(struct adapter *adapter, int idx)
  1531. {
  1532. static const char name[3][5] = { "EDC0", "EDC1", "MC" };
  1533. unsigned int addr, cnt_addr, v;
  1534. if (idx <= MEM_EDC1) {
  1535. addr = EDC_REG(EDC_INT_CAUSE, idx);
  1536. cnt_addr = EDC_REG(EDC_ECC_STATUS, idx);
  1537. } else {
  1538. addr = MC_INT_CAUSE;
  1539. cnt_addr = MC_ECC_STATUS;
  1540. }
  1541. v = t4_read_reg(adapter, addr) & MEM_INT_MASK;
  1542. if (v & PERR_INT_CAUSE)
  1543. dev_alert(adapter->pdev_dev, "%s FIFO parity error\n",
  1544. name[idx]);
  1545. if (v & ECC_CE_INT_CAUSE) {
  1546. u32 cnt = ECC_CECNT_GET(t4_read_reg(adapter, cnt_addr));
  1547. t4_write_reg(adapter, cnt_addr, ECC_CECNT_MASK);
  1548. if (printk_ratelimit())
  1549. dev_warn(adapter->pdev_dev,
  1550. "%u %s correctable ECC data error%s\n",
  1551. cnt, name[idx], cnt > 1 ? "s" : "");
  1552. }
  1553. if (v & ECC_UE_INT_CAUSE)
  1554. dev_alert(adapter->pdev_dev,
  1555. "%s uncorrectable ECC data error\n", name[idx]);
  1556. t4_write_reg(adapter, addr, v);
  1557. if (v & (PERR_INT_CAUSE | ECC_UE_INT_CAUSE))
  1558. t4_fatal_err(adapter);
  1559. }
  1560. /*
  1561. * MA interrupt handler.
  1562. */
  1563. static void ma_intr_handler(struct adapter *adap)
  1564. {
  1565. u32 v, status = t4_read_reg(adap, MA_INT_CAUSE);
  1566. if (status & MEM_PERR_INT_CAUSE)
  1567. dev_alert(adap->pdev_dev,
  1568. "MA parity error, parity status %#x\n",
  1569. t4_read_reg(adap, MA_PARITY_ERROR_STATUS));
  1570. if (status & MEM_WRAP_INT_CAUSE) {
  1571. v = t4_read_reg(adap, MA_INT_WRAP_STATUS);
  1572. dev_alert(adap->pdev_dev, "MA address wrap-around error by "
  1573. "client %u to address %#x\n",
  1574. MEM_WRAP_CLIENT_NUM_GET(v),
  1575. MEM_WRAP_ADDRESS_GET(v) << 4);
  1576. }
  1577. t4_write_reg(adap, MA_INT_CAUSE, status);
  1578. t4_fatal_err(adap);
  1579. }
  1580. /*
  1581. * SMB interrupt handler.
  1582. */
  1583. static void smb_intr_handler(struct adapter *adap)
  1584. {
  1585. static const struct intr_info smb_intr_info[] = {
  1586. { MSTTXFIFOPARINT, "SMB master Tx FIFO parity error", -1, 1 },
  1587. { MSTRXFIFOPARINT, "SMB master Rx FIFO parity error", -1, 1 },
  1588. { SLVFIFOPARINT, "SMB slave FIFO parity error", -1, 1 },
  1589. { 0 }
  1590. };
  1591. if (t4_handle_intr_status(adap, SMB_INT_CAUSE, smb_intr_info))
  1592. t4_fatal_err(adap);
  1593. }
  1594. /*
  1595. * NC-SI interrupt handler.
  1596. */
  1597. static void ncsi_intr_handler(struct adapter *adap)
  1598. {
  1599. static const struct intr_info ncsi_intr_info[] = {
  1600. { CIM_DM_PRTY_ERR, "NC-SI CIM parity error", -1, 1 },
  1601. { MPS_DM_PRTY_ERR, "NC-SI MPS parity error", -1, 1 },
  1602. { TXFIFO_PRTY_ERR, "NC-SI Tx FIFO parity error", -1, 1 },
  1603. { RXFIFO_PRTY_ERR, "NC-SI Rx FIFO parity error", -1, 1 },
  1604. { 0 }
  1605. };
  1606. if (t4_handle_intr_status(adap, NCSI_INT_CAUSE, ncsi_intr_info))
  1607. t4_fatal_err(adap);
  1608. }
  1609. /*
  1610. * XGMAC interrupt handler.
  1611. */
  1612. static void xgmac_intr_handler(struct adapter *adap, int port)
  1613. {
  1614. u32 v, int_cause_reg;
  1615. if (is_t4(adap->params.chip))
  1616. int_cause_reg = PORT_REG(port, XGMAC_PORT_INT_CAUSE);
  1617. else
  1618. int_cause_reg = T5_PORT_REG(port, MAC_PORT_INT_CAUSE);
  1619. v = t4_read_reg(adap, int_cause_reg);
  1620. v &= TXFIFO_PRTY_ERR | RXFIFO_PRTY_ERR;
  1621. if (!v)
  1622. return;
  1623. if (v & TXFIFO_PRTY_ERR)
  1624. dev_alert(adap->pdev_dev, "XGMAC %d Tx FIFO parity error\n",
  1625. port);
  1626. if (v & RXFIFO_PRTY_ERR)
  1627. dev_alert(adap->pdev_dev, "XGMAC %d Rx FIFO parity error\n",
  1628. port);
  1629. t4_write_reg(adap, PORT_REG(port, XGMAC_PORT_INT_CAUSE), v);
  1630. t4_fatal_err(adap);
  1631. }
  1632. /*
  1633. * PL interrupt handler.
  1634. */
  1635. static void pl_intr_handler(struct adapter *adap)
  1636. {
  1637. static const struct intr_info pl_intr_info[] = {
  1638. { FATALPERR, "T4 fatal parity error", -1, 1 },
  1639. { PERRVFID, "PL VFID_MAP parity error", -1, 1 },
  1640. { 0 }
  1641. };
  1642. if (t4_handle_intr_status(adap, PL_PL_INT_CAUSE, pl_intr_info))
  1643. t4_fatal_err(adap);
  1644. }
  1645. #define PF_INTR_MASK (PFSW)
  1646. #define GLBL_INTR_MASK (CIM | MPS | PL | PCIE | MC | EDC0 | \
  1647. EDC1 | LE | TP | MA | PM_TX | PM_RX | ULP_RX | \
  1648. CPL_SWITCH | SGE | ULP_TX)
  1649. /**
  1650. * t4_slow_intr_handler - control path interrupt handler
  1651. * @adapter: the adapter
  1652. *
  1653. * T4 interrupt handler for non-data global interrupt events, e.g., errors.
  1654. * The designation 'slow' is because it involves register reads, while
  1655. * data interrupts typically don't involve any MMIOs.
  1656. */
  1657. int t4_slow_intr_handler(struct adapter *adapter)
  1658. {
  1659. u32 cause = t4_read_reg(adapter, PL_INT_CAUSE);
  1660. if (!(cause & GLBL_INTR_MASK))
  1661. return 0;
  1662. if (cause & CIM)
  1663. cim_intr_handler(adapter);
  1664. if (cause & MPS)
  1665. mps_intr_handler(adapter);
  1666. if (cause & NCSI)
  1667. ncsi_intr_handler(adapter);
  1668. if (cause & PL)
  1669. pl_intr_handler(adapter);
  1670. if (cause & SMB)
  1671. smb_intr_handler(adapter);
  1672. if (cause & XGMAC0)
  1673. xgmac_intr_handler(adapter, 0);
  1674. if (cause & XGMAC1)
  1675. xgmac_intr_handler(adapter, 1);
  1676. if (cause & XGMAC_KR0)
  1677. xgmac_intr_handler(adapter, 2);
  1678. if (cause & XGMAC_KR1)
  1679. xgmac_intr_handler(adapter, 3);
  1680. if (cause & PCIE)
  1681. pcie_intr_handler(adapter);
  1682. if (cause & MC)
  1683. mem_intr_handler(adapter, MEM_MC);
  1684. if (cause & EDC0)
  1685. mem_intr_handler(adapter, MEM_EDC0);
  1686. if (cause & EDC1)
  1687. mem_intr_handler(adapter, MEM_EDC1);
  1688. if (cause & LE)
  1689. le_intr_handler(adapter);
  1690. if (cause & TP)
  1691. tp_intr_handler(adapter);
  1692. if (cause & MA)
  1693. ma_intr_handler(adapter);
  1694. if (cause & PM_TX)
  1695. pmtx_intr_handler(adapter);
  1696. if (cause & PM_RX)
  1697. pmrx_intr_handler(adapter);
  1698. if (cause & ULP_RX)
  1699. ulprx_intr_handler(adapter);
  1700. if (cause & CPL_SWITCH)
  1701. cplsw_intr_handler(adapter);
  1702. if (cause & SGE)
  1703. sge_intr_handler(adapter);
  1704. if (cause & ULP_TX)
  1705. ulptx_intr_handler(adapter);
  1706. /* Clear the interrupts just processed for which we are the master. */
  1707. t4_write_reg(adapter, PL_INT_CAUSE, cause & GLBL_INTR_MASK);
  1708. (void) t4_read_reg(adapter, PL_INT_CAUSE); /* flush */
  1709. return 1;
  1710. }
  1711. /**
  1712. * t4_intr_enable - enable interrupts
  1713. * @adapter: the adapter whose interrupts should be enabled
  1714. *
  1715. * Enable PF-specific interrupts for the calling function and the top-level
  1716. * interrupt concentrator for global interrupts. Interrupts are already
  1717. * enabled at each module, here we just enable the roots of the interrupt
  1718. * hierarchies.
  1719. *
  1720. * Note: this function should be called only when the driver manages
  1721. * non PF-specific interrupts from the various HW modules. Only one PCI
  1722. * function at a time should be doing this.
  1723. */
  1724. void t4_intr_enable(struct adapter *adapter)
  1725. {
  1726. u32 pf = SOURCEPF_GET(t4_read_reg(adapter, PL_WHOAMI));
  1727. t4_write_reg(adapter, SGE_INT_ENABLE3, ERR_CPL_EXCEED_IQE_SIZE |
  1728. ERR_INVALID_CIDX_INC | ERR_CPL_OPCODE_0 |
  1729. ERR_DROPPED_DB | ERR_DATA_CPL_ON_HIGH_QID1 |
  1730. ERR_DATA_CPL_ON_HIGH_QID0 | ERR_BAD_DB_PIDX3 |
  1731. ERR_BAD_DB_PIDX2 | ERR_BAD_DB_PIDX1 |
  1732. ERR_BAD_DB_PIDX0 | ERR_ING_CTXT_PRIO |
  1733. ERR_EGR_CTXT_PRIO | INGRESS_SIZE_ERR |
  1734. DBFIFO_HP_INT | DBFIFO_LP_INT |
  1735. EGRESS_SIZE_ERR);
  1736. t4_write_reg(adapter, MYPF_REG(PL_PF_INT_ENABLE), PF_INTR_MASK);
  1737. t4_set_reg_field(adapter, PL_INT_MAP0, 0, 1 << pf);
  1738. }
  1739. /**
  1740. * t4_intr_disable - disable interrupts
  1741. * @adapter: the adapter whose interrupts should be disabled
  1742. *
  1743. * Disable interrupts. We only disable the top-level interrupt
  1744. * concentrators. The caller must be a PCI function managing global
  1745. * interrupts.
  1746. */
  1747. void t4_intr_disable(struct adapter *adapter)
  1748. {
  1749. u32 pf = SOURCEPF_GET(t4_read_reg(adapter, PL_WHOAMI));
  1750. t4_write_reg(adapter, MYPF_REG(PL_PF_INT_ENABLE), 0);
  1751. t4_set_reg_field(adapter, PL_INT_MAP0, 1 << pf, 0);
  1752. }
  1753. /**
  1754. * hash_mac_addr - return the hash value of a MAC address
  1755. * @addr: the 48-bit Ethernet MAC address
  1756. *
  1757. * Hashes a MAC address according to the hash function used by HW inexact
  1758. * (hash) address matching.
  1759. */
  1760. static int hash_mac_addr(const u8 *addr)
  1761. {
  1762. u32 a = ((u32)addr[0] << 16) | ((u32)addr[1] << 8) | addr[2];
  1763. u32 b = ((u32)addr[3] << 16) | ((u32)addr[4] << 8) | addr[5];
  1764. a ^= b;
  1765. a ^= (a >> 12);
  1766. a ^= (a >> 6);
  1767. return a & 0x3f;
  1768. }
  1769. /**
  1770. * t4_config_rss_range - configure a portion of the RSS mapping table
  1771. * @adapter: the adapter
  1772. * @mbox: mbox to use for the FW command
  1773. * @viid: virtual interface whose RSS subtable is to be written
  1774. * @start: start entry in the table to write
  1775. * @n: how many table entries to write
  1776. * @rspq: values for the response queue lookup table
  1777. * @nrspq: number of values in @rspq
  1778. *
  1779. * Programs the selected part of the VI's RSS mapping table with the
  1780. * provided values. If @nrspq < @n the supplied values are used repeatedly
  1781. * until the full table range is populated.
  1782. *
  1783. * The caller must ensure the values in @rspq are in the range allowed for
  1784. * @viid.
  1785. */
  1786. int t4_config_rss_range(struct adapter *adapter, int mbox, unsigned int viid,
  1787. int start, int n, const u16 *rspq, unsigned int nrspq)
  1788. {
  1789. int ret;
  1790. const u16 *rsp = rspq;
  1791. const u16 *rsp_end = rspq + nrspq;
  1792. struct fw_rss_ind_tbl_cmd cmd;
  1793. memset(&cmd, 0, sizeof(cmd));
  1794. cmd.op_to_viid = htonl(FW_CMD_OP(FW_RSS_IND_TBL_CMD) |
  1795. FW_CMD_REQUEST | FW_CMD_WRITE |
  1796. FW_RSS_IND_TBL_CMD_VIID(viid));
  1797. cmd.retval_len16 = htonl(FW_LEN16(cmd));
  1798. /* each fw_rss_ind_tbl_cmd takes up to 32 entries */
  1799. while (n > 0) {
  1800. int nq = min(n, 32);
  1801. __be32 *qp = &cmd.iq0_to_iq2;
  1802. cmd.niqid = htons(nq);
  1803. cmd.startidx = htons(start);
  1804. start += nq;
  1805. n -= nq;
  1806. while (nq > 0) {
  1807. unsigned int v;
  1808. v = FW_RSS_IND_TBL_CMD_IQ0(*rsp);
  1809. if (++rsp >= rsp_end)
  1810. rsp = rspq;
  1811. v |= FW_RSS_IND_TBL_CMD_IQ1(*rsp);
  1812. if (++rsp >= rsp_end)
  1813. rsp = rspq;
  1814. v |= FW_RSS_IND_TBL_CMD_IQ2(*rsp);
  1815. if (++rsp >= rsp_end)
  1816. rsp = rspq;
  1817. *qp++ = htonl(v);
  1818. nq -= 3;
  1819. }
  1820. ret = t4_wr_mbox(adapter, mbox, &cmd, sizeof(cmd), NULL);
  1821. if (ret)
  1822. return ret;
  1823. }
  1824. return 0;
  1825. }
  1826. /**
  1827. * t4_config_glbl_rss - configure the global RSS mode
  1828. * @adapter: the adapter
  1829. * @mbox: mbox to use for the FW command
  1830. * @mode: global RSS mode
  1831. * @flags: mode-specific flags
  1832. *
  1833. * Sets the global RSS mode.
  1834. */
  1835. int t4_config_glbl_rss(struct adapter *adapter, int mbox, unsigned int mode,
  1836. unsigned int flags)
  1837. {
  1838. struct fw_rss_glb_config_cmd c;
  1839. memset(&c, 0, sizeof(c));
  1840. c.op_to_write = htonl(FW_CMD_OP(FW_RSS_GLB_CONFIG_CMD) |
  1841. FW_CMD_REQUEST | FW_CMD_WRITE);
  1842. c.retval_len16 = htonl(FW_LEN16(c));
  1843. if (mode == FW_RSS_GLB_CONFIG_CMD_MODE_MANUAL) {
  1844. c.u.manual.mode_pkd = htonl(FW_RSS_GLB_CONFIG_CMD_MODE(mode));
  1845. } else if (mode == FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL) {
  1846. c.u.basicvirtual.mode_pkd =
  1847. htonl(FW_RSS_GLB_CONFIG_CMD_MODE(mode));
  1848. c.u.basicvirtual.synmapen_to_hashtoeplitz = htonl(flags);
  1849. } else
  1850. return -EINVAL;
  1851. return t4_wr_mbox(adapter, mbox, &c, sizeof(c), NULL);
  1852. }
  1853. /**
  1854. * t4_tp_get_tcp_stats - read TP's TCP MIB counters
  1855. * @adap: the adapter
  1856. * @v4: holds the TCP/IP counter values
  1857. * @v6: holds the TCP/IPv6 counter values
  1858. *
  1859. * Returns the values of TP's TCP/IP and TCP/IPv6 MIB counters.
  1860. * Either @v4 or @v6 may be %NULL to skip the corresponding stats.
  1861. */
  1862. void t4_tp_get_tcp_stats(struct adapter *adap, struct tp_tcp_stats *v4,
  1863. struct tp_tcp_stats *v6)
  1864. {
  1865. u32 val[TP_MIB_TCP_RXT_SEG_LO - TP_MIB_TCP_OUT_RST + 1];
  1866. #define STAT_IDX(x) ((TP_MIB_TCP_##x) - TP_MIB_TCP_OUT_RST)
  1867. #define STAT(x) val[STAT_IDX(x)]
  1868. #define STAT64(x) (((u64)STAT(x##_HI) << 32) | STAT(x##_LO))
  1869. if (v4) {
  1870. t4_read_indirect(adap, TP_MIB_INDEX, TP_MIB_DATA, val,
  1871. ARRAY_SIZE(val), TP_MIB_TCP_OUT_RST);
  1872. v4->tcpOutRsts = STAT(OUT_RST);
  1873. v4->tcpInSegs = STAT64(IN_SEG);
  1874. v4->tcpOutSegs = STAT64(OUT_SEG);
  1875. v4->tcpRetransSegs = STAT64(RXT_SEG);
  1876. }
  1877. if (v6) {
  1878. t4_read_indirect(adap, TP_MIB_INDEX, TP_MIB_DATA, val,
  1879. ARRAY_SIZE(val), TP_MIB_TCP_V6OUT_RST);
  1880. v6->tcpOutRsts = STAT(OUT_RST);
  1881. v6->tcpInSegs = STAT64(IN_SEG);
  1882. v6->tcpOutSegs = STAT64(OUT_SEG);
  1883. v6->tcpRetransSegs = STAT64(RXT_SEG);
  1884. }
  1885. #undef STAT64
  1886. #undef STAT
  1887. #undef STAT_IDX
  1888. }
  1889. /**
  1890. * t4_read_mtu_tbl - returns the values in the HW path MTU table
  1891. * @adap: the adapter
  1892. * @mtus: where to store the MTU values
  1893. * @mtu_log: where to store the MTU base-2 log (may be %NULL)
  1894. *
  1895. * Reads the HW path MTU table.
  1896. */
  1897. void t4_read_mtu_tbl(struct adapter *adap, u16 *mtus, u8 *mtu_log)
  1898. {
  1899. u32 v;
  1900. int i;
  1901. for (i = 0; i < NMTUS; ++i) {
  1902. t4_write_reg(adap, TP_MTU_TABLE,
  1903. MTUINDEX(0xff) | MTUVALUE(i));
  1904. v = t4_read_reg(adap, TP_MTU_TABLE);
  1905. mtus[i] = MTUVALUE_GET(v);
  1906. if (mtu_log)
  1907. mtu_log[i] = MTUWIDTH_GET(v);
  1908. }
  1909. }
  1910. /**
  1911. * t4_tp_wr_bits_indirect - set/clear bits in an indirect TP register
  1912. * @adap: the adapter
  1913. * @addr: the indirect TP register address
  1914. * @mask: specifies the field within the register to modify
  1915. * @val: new value for the field
  1916. *
  1917. * Sets a field of an indirect TP register to the given value.
  1918. */
  1919. void t4_tp_wr_bits_indirect(struct adapter *adap, unsigned int addr,
  1920. unsigned int mask, unsigned int val)
  1921. {
  1922. t4_write_reg(adap, TP_PIO_ADDR, addr);
  1923. val |= t4_read_reg(adap, TP_PIO_DATA) & ~mask;
  1924. t4_write_reg(adap, TP_PIO_DATA, val);
  1925. }
  1926. /**
  1927. * init_cong_ctrl - initialize congestion control parameters
  1928. * @a: the alpha values for congestion control
  1929. * @b: the beta values for congestion control
  1930. *
  1931. * Initialize the congestion control parameters.
  1932. */
  1933. static void init_cong_ctrl(unsigned short *a, unsigned short *b)
  1934. {
  1935. a[0] = a[1] = a[2] = a[3] = a[4] = a[5] = a[6] = a[7] = a[8] = 1;
  1936. a[9] = 2;
  1937. a[10] = 3;
  1938. a[11] = 4;
  1939. a[12] = 5;
  1940. a[13] = 6;
  1941. a[14] = 7;
  1942. a[15] = 8;
  1943. a[16] = 9;
  1944. a[17] = 10;
  1945. a[18] = 14;
  1946. a[19] = 17;
  1947. a[20] = 21;
  1948. a[21] = 25;
  1949. a[22] = 30;
  1950. a[23] = 35;
  1951. a[24] = 45;
  1952. a[25] = 60;
  1953. a[26] = 80;
  1954. a[27] = 100;
  1955. a[28] = 200;
  1956. a[29] = 300;
  1957. a[30] = 400;
  1958. a[31] = 500;
  1959. b[0] = b[1] = b[2] = b[3] = b[4] = b[5] = b[6] = b[7] = b[8] = 0;
  1960. b[9] = b[10] = 1;
  1961. b[11] = b[12] = 2;
  1962. b[13] = b[14] = b[15] = b[16] = 3;
  1963. b[17] = b[18] = b[19] = b[20] = b[21] = 4;
  1964. b[22] = b[23] = b[24] = b[25] = b[26] = b[27] = 5;
  1965. b[28] = b[29] = 6;
  1966. b[30] = b[31] = 7;
  1967. }
  1968. /* The minimum additive increment value for the congestion control table */
  1969. #define CC_MIN_INCR 2U
  1970. /**
  1971. * t4_load_mtus - write the MTU and congestion control HW tables
  1972. * @adap: the adapter
  1973. * @mtus: the values for the MTU table
  1974. * @alpha: the values for the congestion control alpha parameter
  1975. * @beta: the values for the congestion control beta parameter
  1976. *
  1977. * Write the HW MTU table with the supplied MTUs and the high-speed
  1978. * congestion control table with the supplied alpha, beta, and MTUs.
  1979. * We write the two tables together because the additive increments
  1980. * depend on the MTUs.
  1981. */
  1982. void t4_load_mtus(struct adapter *adap, const unsigned short *mtus,
  1983. const unsigned short *alpha, const unsigned short *beta)
  1984. {
  1985. static const unsigned int avg_pkts[NCCTRL_WIN] = {
  1986. 2, 6, 10, 14, 20, 28, 40, 56, 80, 112, 160, 224, 320, 448, 640,
  1987. 896, 1281, 1792, 2560, 3584, 5120, 7168, 10240, 14336, 20480,
  1988. 28672, 40960, 57344, 81920, 114688, 163840, 229376
  1989. };
  1990. unsigned int i, w;
  1991. for (i = 0; i < NMTUS; ++i) {
  1992. unsigned int mtu = mtus[i];
  1993. unsigned int log2 = fls(mtu);
  1994. if (!(mtu & ((1 << log2) >> 2))) /* round */
  1995. log2--;
  1996. t4_write_reg(adap, TP_MTU_TABLE, MTUINDEX(i) |
  1997. MTUWIDTH(log2) | MTUVALUE(mtu));
  1998. for (w = 0; w < NCCTRL_WIN; ++w) {
  1999. unsigned int inc;
  2000. inc = max(((mtu - 40) * alpha[w]) / avg_pkts[w],
  2001. CC_MIN_INCR);
  2002. t4_write_reg(adap, TP_CCTRL_TABLE, (i << 21) |
  2003. (w << 16) | (beta[w] << 13) | inc);
  2004. }
  2005. }
  2006. }
  2007. /**
  2008. * get_mps_bg_map - return the buffer groups associated with a port
  2009. * @adap: the adapter
  2010. * @idx: the port index
  2011. *
  2012. * Returns a bitmap indicating which MPS buffer groups are associated
  2013. * with the given port. Bit i is set if buffer group i is used by the
  2014. * port.
  2015. */
  2016. static unsigned int get_mps_bg_map(struct adapter *adap, int idx)
  2017. {
  2018. u32 n = NUMPORTS_GET(t4_read_reg(adap, MPS_CMN_CTL));
  2019. if (n == 0)
  2020. return idx == 0 ? 0xf : 0;
  2021. if (n == 1)
  2022. return idx < 2 ? (3 << (2 * idx)) : 0;
  2023. return 1 << idx;
  2024. }
  2025. /**
  2026. * t4_get_port_type_description - return Port Type string description
  2027. * @port_type: firmware Port Type enumeration
  2028. */
  2029. const char *t4_get_port_type_description(enum fw_port_type port_type)
  2030. {
  2031. static const char *const port_type_description[] = {
  2032. "R XFI",
  2033. "R XAUI",
  2034. "T SGMII",
  2035. "T XFI",
  2036. "T XAUI",
  2037. "KX4",
  2038. "CX4",
  2039. "KX",
  2040. "KR",
  2041. "R SFP+",
  2042. "KR/KX",
  2043. "KR/KX/KX4",
  2044. "R QSFP_10G",
  2045. "",
  2046. "R QSFP",
  2047. "R BP40_BA",
  2048. };
  2049. if (port_type < ARRAY_SIZE(port_type_description))
  2050. return port_type_description[port_type];
  2051. return "UNKNOWN";
  2052. }
  2053. /**
  2054. * t4_get_port_stats - collect port statistics
  2055. * @adap: the adapter
  2056. * @idx: the port index
  2057. * @p: the stats structure to fill
  2058. *
  2059. * Collect statistics related to the given port from HW.
  2060. */
  2061. void t4_get_port_stats(struct adapter *adap, int idx, struct port_stats *p)
  2062. {
  2063. u32 bgmap = get_mps_bg_map(adap, idx);
  2064. #define GET_STAT(name) \
  2065. t4_read_reg64(adap, \
  2066. (is_t4(adap->params.chip) ? PORT_REG(idx, MPS_PORT_STAT_##name##_L) : \
  2067. T5_PORT_REG(idx, MPS_PORT_STAT_##name##_L)))
  2068. #define GET_STAT_COM(name) t4_read_reg64(adap, MPS_STAT_##name##_L)
  2069. p->tx_octets = GET_STAT(TX_PORT_BYTES);
  2070. p->tx_frames = GET_STAT(TX_PORT_FRAMES);
  2071. p->tx_bcast_frames = GET_STAT(TX_PORT_BCAST);
  2072. p->tx_mcast_frames = GET_STAT(TX_PORT_MCAST);
  2073. p->tx_ucast_frames = GET_STAT(TX_PORT_UCAST);
  2074. p->tx_error_frames = GET_STAT(TX_PORT_ERROR);
  2075. p->tx_frames_64 = GET_STAT(TX_PORT_64B);
  2076. p->tx_frames_65_127 = GET_STAT(TX_PORT_65B_127B);
  2077. p->tx_frames_128_255 = GET_STAT(TX_PORT_128B_255B);
  2078. p->tx_frames_256_511 = GET_STAT(TX_PORT_256B_511B);
  2079. p->tx_frames_512_1023 = GET_STAT(TX_PORT_512B_1023B);
  2080. p->tx_frames_1024_1518 = GET_STAT(TX_PORT_1024B_1518B);
  2081. p->tx_frames_1519_max = GET_STAT(TX_PORT_1519B_MAX);
  2082. p->tx_drop = GET_STAT(TX_PORT_DROP);
  2083. p->tx_pause = GET_STAT(TX_PORT_PAUSE);
  2084. p->tx_ppp0 = GET_STAT(TX_PORT_PPP0);
  2085. p->tx_ppp1 = GET_STAT(TX_PORT_PPP1);
  2086. p->tx_ppp2 = GET_STAT(TX_PORT_PPP2);
  2087. p->tx_ppp3 = GET_STAT(TX_PORT_PPP3);
  2088. p->tx_ppp4 = GET_STAT(TX_PORT_PPP4);
  2089. p->tx_ppp5 = GET_STAT(TX_PORT_PPP5);
  2090. p->tx_ppp6 = GET_STAT(TX_PORT_PPP6);
  2091. p->tx_ppp7 = GET_STAT(TX_PORT_PPP7);
  2092. p->rx_octets = GET_STAT(RX_PORT_BYTES);
  2093. p->rx_frames = GET_STAT(RX_PORT_FRAMES);
  2094. p->rx_bcast_frames = GET_STAT(RX_PORT_BCAST);
  2095. p->rx_mcast_frames = GET_STAT(RX_PORT_MCAST);
  2096. p->rx_ucast_frames = GET_STAT(RX_PORT_UCAST);
  2097. p->rx_too_long = GET_STAT(RX_PORT_MTU_ERROR);
  2098. p->rx_jabber = GET_STAT(RX_PORT_MTU_CRC_ERROR);
  2099. p->rx_fcs_err = GET_STAT(RX_PORT_CRC_ERROR);
  2100. p->rx_len_err = GET_STAT(RX_PORT_LEN_ERROR);
  2101. p->rx_symbol_err = GET_STAT(RX_PORT_SYM_ERROR);
  2102. p->rx_runt = GET_STAT(RX_PORT_LESS_64B);
  2103. p->rx_frames_64 = GET_STAT(RX_PORT_64B);
  2104. p->rx_frames_65_127 = GET_STAT(RX_PORT_65B_127B);
  2105. p->rx_frames_128_255 = GET_STAT(RX_PORT_128B_255B);
  2106. p->rx_frames_256_511 = GET_STAT(RX_PORT_256B_511B);
  2107. p->rx_frames_512_1023 = GET_STAT(RX_PORT_512B_1023B);
  2108. p->rx_frames_1024_1518 = GET_STAT(RX_PORT_1024B_1518B);
  2109. p->rx_frames_1519_max = GET_STAT(RX_PORT_1519B_MAX);
  2110. p->rx_pause = GET_STAT(RX_PORT_PAUSE);
  2111. p->rx_ppp0 = GET_STAT(RX_PORT_PPP0);
  2112. p->rx_ppp1 = GET_STAT(RX_PORT_PPP1);
  2113. p->rx_ppp2 = GET_STAT(RX_PORT_PPP2);
  2114. p->rx_ppp3 = GET_STAT(RX_PORT_PPP3);
  2115. p->rx_ppp4 = GET_STAT(RX_PORT_PPP4);
  2116. p->rx_ppp5 = GET_STAT(RX_PORT_PPP5);
  2117. p->rx_ppp6 = GET_STAT(RX_PORT_PPP6);
  2118. p->rx_ppp7 = GET_STAT(RX_PORT_PPP7);
  2119. p->rx_ovflow0 = (bgmap & 1) ? GET_STAT_COM(RX_BG_0_MAC_DROP_FRAME) : 0;
  2120. p->rx_ovflow1 = (bgmap & 2) ? GET_STAT_COM(RX_BG_1_MAC_DROP_FRAME) : 0;
  2121. p->rx_ovflow2 = (bgmap & 4) ? GET_STAT_COM(RX_BG_2_MAC_DROP_FRAME) : 0;
  2122. p->rx_ovflow3 = (bgmap & 8) ? GET_STAT_COM(RX_BG_3_MAC_DROP_FRAME) : 0;
  2123. p->rx_trunc0 = (bgmap & 1) ? GET_STAT_COM(RX_BG_0_MAC_TRUNC_FRAME) : 0;
  2124. p->rx_trunc1 = (bgmap & 2) ? GET_STAT_COM(RX_BG_1_MAC_TRUNC_FRAME) : 0;
  2125. p->rx_trunc2 = (bgmap & 4) ? GET_STAT_COM(RX_BG_2_MAC_TRUNC_FRAME) : 0;
  2126. p->rx_trunc3 = (bgmap & 8) ? GET_STAT_COM(RX_BG_3_MAC_TRUNC_FRAME) : 0;
  2127. #undef GET_STAT
  2128. #undef GET_STAT_COM
  2129. }
  2130. /**
  2131. * t4_wol_magic_enable - enable/disable magic packet WoL
  2132. * @adap: the adapter
  2133. * @port: the physical port index
  2134. * @addr: MAC address expected in magic packets, %NULL to disable
  2135. *
  2136. * Enables/disables magic packet wake-on-LAN for the selected port.
  2137. */
  2138. void t4_wol_magic_enable(struct adapter *adap, unsigned int port,
  2139. const u8 *addr)
  2140. {
  2141. u32 mag_id_reg_l, mag_id_reg_h, port_cfg_reg;
  2142. if (is_t4(adap->params.chip)) {
  2143. mag_id_reg_l = PORT_REG(port, XGMAC_PORT_MAGIC_MACID_LO);
  2144. mag_id_reg_h = PORT_REG(port, XGMAC_PORT_MAGIC_MACID_HI);
  2145. port_cfg_reg = PORT_REG(port, XGMAC_PORT_CFG2);
  2146. } else {
  2147. mag_id_reg_l = T5_PORT_REG(port, MAC_PORT_MAGIC_MACID_LO);
  2148. mag_id_reg_h = T5_PORT_REG(port, MAC_PORT_MAGIC_MACID_HI);
  2149. port_cfg_reg = T5_PORT_REG(port, MAC_PORT_CFG2);
  2150. }
  2151. if (addr) {
  2152. t4_write_reg(adap, mag_id_reg_l,
  2153. (addr[2] << 24) | (addr[3] << 16) |
  2154. (addr[4] << 8) | addr[5]);
  2155. t4_write_reg(adap, mag_id_reg_h,
  2156. (addr[0] << 8) | addr[1]);
  2157. }
  2158. t4_set_reg_field(adap, port_cfg_reg, MAGICEN,
  2159. addr ? MAGICEN : 0);
  2160. }
  2161. /**
  2162. * t4_wol_pat_enable - enable/disable pattern-based WoL
  2163. * @adap: the adapter
  2164. * @port: the physical port index
  2165. * @map: bitmap of which HW pattern filters to set
  2166. * @mask0: byte mask for bytes 0-63 of a packet
  2167. * @mask1: byte mask for bytes 64-127 of a packet
  2168. * @crc: Ethernet CRC for selected bytes
  2169. * @enable: enable/disable switch
  2170. *
  2171. * Sets the pattern filters indicated in @map to mask out the bytes
  2172. * specified in @mask0/@mask1 in received packets and compare the CRC of
  2173. * the resulting packet against @crc. If @enable is %true pattern-based
  2174. * WoL is enabled, otherwise disabled.
  2175. */
  2176. int t4_wol_pat_enable(struct adapter *adap, unsigned int port, unsigned int map,
  2177. u64 mask0, u64 mask1, unsigned int crc, bool enable)
  2178. {
  2179. int i;
  2180. u32 port_cfg_reg;
  2181. if (is_t4(adap->params.chip))
  2182. port_cfg_reg = PORT_REG(port, XGMAC_PORT_CFG2);
  2183. else
  2184. port_cfg_reg = T5_PORT_REG(port, MAC_PORT_CFG2);
  2185. if (!enable) {
  2186. t4_set_reg_field(adap, port_cfg_reg, PATEN, 0);
  2187. return 0;
  2188. }
  2189. if (map > 0xff)
  2190. return -EINVAL;
  2191. #define EPIO_REG(name) \
  2192. (is_t4(adap->params.chip) ? PORT_REG(port, XGMAC_PORT_EPIO_##name) : \
  2193. T5_PORT_REG(port, MAC_PORT_EPIO_##name))
  2194. t4_write_reg(adap, EPIO_REG(DATA1), mask0 >> 32);
  2195. t4_write_reg(adap, EPIO_REG(DATA2), mask1);
  2196. t4_write_reg(adap, EPIO_REG(DATA3), mask1 >> 32);
  2197. for (i = 0; i < NWOL_PAT; i++, map >>= 1) {
  2198. if (!(map & 1))
  2199. continue;
  2200. /* write byte masks */
  2201. t4_write_reg(adap, EPIO_REG(DATA0), mask0);
  2202. t4_write_reg(adap, EPIO_REG(OP), ADDRESS(i) | EPIOWR);
  2203. t4_read_reg(adap, EPIO_REG(OP)); /* flush */
  2204. if (t4_read_reg(adap, EPIO_REG(OP)) & SF_BUSY)
  2205. return -ETIMEDOUT;
  2206. /* write CRC */
  2207. t4_write_reg(adap, EPIO_REG(DATA0), crc);
  2208. t4_write_reg(adap, EPIO_REG(OP), ADDRESS(i + 32) | EPIOWR);
  2209. t4_read_reg(adap, EPIO_REG(OP)); /* flush */
  2210. if (t4_read_reg(adap, EPIO_REG(OP)) & SF_BUSY)
  2211. return -ETIMEDOUT;
  2212. }
  2213. #undef EPIO_REG
  2214. t4_set_reg_field(adap, PORT_REG(port, XGMAC_PORT_CFG2), 0, PATEN);
  2215. return 0;
  2216. }
  2217. /* t4_mk_filtdelwr - create a delete filter WR
  2218. * @ftid: the filter ID
  2219. * @wr: the filter work request to populate
  2220. * @qid: ingress queue to receive the delete notification
  2221. *
  2222. * Creates a filter work request to delete the supplied filter. If @qid is
  2223. * negative the delete notification is suppressed.
  2224. */
  2225. void t4_mk_filtdelwr(unsigned int ftid, struct fw_filter_wr *wr, int qid)
  2226. {
  2227. memset(wr, 0, sizeof(*wr));
  2228. wr->op_pkd = htonl(FW_WR_OP(FW_FILTER_WR));
  2229. wr->len16_pkd = htonl(FW_WR_LEN16(sizeof(*wr) / 16));
  2230. wr->tid_to_iq = htonl(V_FW_FILTER_WR_TID(ftid) |
  2231. V_FW_FILTER_WR_NOREPLY(qid < 0));
  2232. wr->del_filter_to_l2tix = htonl(F_FW_FILTER_WR_DEL_FILTER);
  2233. if (qid >= 0)
  2234. wr->rx_chan_rx_rpl_iq = htons(V_FW_FILTER_WR_RX_RPL_IQ(qid));
  2235. }
  2236. #define INIT_CMD(var, cmd, rd_wr) do { \
  2237. (var).op_to_write = htonl(FW_CMD_OP(FW_##cmd##_CMD) | \
  2238. FW_CMD_REQUEST | FW_CMD_##rd_wr); \
  2239. (var).retval_len16 = htonl(FW_LEN16(var)); \
  2240. } while (0)
  2241. int t4_fwaddrspace_write(struct adapter *adap, unsigned int mbox,
  2242. u32 addr, u32 val)
  2243. {
  2244. struct fw_ldst_cmd c;
  2245. memset(&c, 0, sizeof(c));
  2246. c.op_to_addrspace = htonl(FW_CMD_OP(FW_LDST_CMD) | FW_CMD_REQUEST |
  2247. FW_CMD_WRITE |
  2248. FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_FIRMWARE));
  2249. c.cycles_to_len16 = htonl(FW_LEN16(c));
  2250. c.u.addrval.addr = htonl(addr);
  2251. c.u.addrval.val = htonl(val);
  2252. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  2253. }
  2254. /**
  2255. * t4_mem_win_read_len - read memory through PCIE memory window
  2256. * @adap: the adapter
  2257. * @addr: address of first byte requested aligned on 32b.
  2258. * @data: len bytes to hold the data read
  2259. * @len: amount of data to read from window. Must be <=
  2260. * MEMWIN0_APERATURE after adjusting for 16B for T4 and
  2261. * 128B for T5 alignment requirements of the the memory window.
  2262. *
  2263. * Read len bytes of data from MC starting at @addr.
  2264. */
  2265. int t4_mem_win_read_len(struct adapter *adap, u32 addr, __be32 *data, int len)
  2266. {
  2267. int i, off;
  2268. u32 win_pf = is_t4(adap->params.chip) ? 0 : V_PFNUM(adap->fn);
  2269. /* Align on a 2KB boundary.
  2270. */
  2271. off = addr & MEMWIN0_APERTURE;
  2272. if ((addr & 3) || (len + off) > MEMWIN0_APERTURE)
  2273. return -EINVAL;
  2274. t4_write_reg(adap, PCIE_MEM_ACCESS_OFFSET,
  2275. (addr & ~MEMWIN0_APERTURE) | win_pf);
  2276. t4_read_reg(adap, PCIE_MEM_ACCESS_OFFSET);
  2277. for (i = 0; i < len; i += 4)
  2278. *data++ = (__force __be32) t4_read_reg(adap,
  2279. (MEMWIN0_BASE + off + i));
  2280. return 0;
  2281. }
  2282. /**
  2283. * t4_mdio_rd - read a PHY register through MDIO
  2284. * @adap: the adapter
  2285. * @mbox: mailbox to use for the FW command
  2286. * @phy_addr: the PHY address
  2287. * @mmd: the PHY MMD to access (0 for clause 22 PHYs)
  2288. * @reg: the register to read
  2289. * @valp: where to store the value
  2290. *
  2291. * Issues a FW command through the given mailbox to read a PHY register.
  2292. */
  2293. int t4_mdio_rd(struct adapter *adap, unsigned int mbox, unsigned int phy_addr,
  2294. unsigned int mmd, unsigned int reg, u16 *valp)
  2295. {
  2296. int ret;
  2297. struct fw_ldst_cmd c;
  2298. memset(&c, 0, sizeof(c));
  2299. c.op_to_addrspace = htonl(FW_CMD_OP(FW_LDST_CMD) | FW_CMD_REQUEST |
  2300. FW_CMD_READ | FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MDIO));
  2301. c.cycles_to_len16 = htonl(FW_LEN16(c));
  2302. c.u.mdio.paddr_mmd = htons(FW_LDST_CMD_PADDR(phy_addr) |
  2303. FW_LDST_CMD_MMD(mmd));
  2304. c.u.mdio.raddr = htons(reg);
  2305. ret = t4_wr_mbox(adap, mbox, &c, sizeof(c), &c);
  2306. if (ret == 0)
  2307. *valp = ntohs(c.u.mdio.rval);
  2308. return ret;
  2309. }
  2310. /**
  2311. * t4_mdio_wr - write a PHY register through MDIO
  2312. * @adap: the adapter
  2313. * @mbox: mailbox to use for the FW command
  2314. * @phy_addr: the PHY address
  2315. * @mmd: the PHY MMD to access (0 for clause 22 PHYs)
  2316. * @reg: the register to write
  2317. * @valp: value to write
  2318. *
  2319. * Issues a FW command through the given mailbox to write a PHY register.
  2320. */
  2321. int t4_mdio_wr(struct adapter *adap, unsigned int mbox, unsigned int phy_addr,
  2322. unsigned int mmd, unsigned int reg, u16 val)
  2323. {
  2324. struct fw_ldst_cmd c;
  2325. memset(&c, 0, sizeof(c));
  2326. c.op_to_addrspace = htonl(FW_CMD_OP(FW_LDST_CMD) | FW_CMD_REQUEST |
  2327. FW_CMD_WRITE | FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MDIO));
  2328. c.cycles_to_len16 = htonl(FW_LEN16(c));
  2329. c.u.mdio.paddr_mmd = htons(FW_LDST_CMD_PADDR(phy_addr) |
  2330. FW_LDST_CMD_MMD(mmd));
  2331. c.u.mdio.raddr = htons(reg);
  2332. c.u.mdio.rval = htons(val);
  2333. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  2334. }
  2335. /**
  2336. * t4_sge_decode_idma_state - decode the idma state
  2337. * @adap: the adapter
  2338. * @state: the state idma is stuck in
  2339. */
  2340. void t4_sge_decode_idma_state(struct adapter *adapter, int state)
  2341. {
  2342. static const char * const t4_decode[] = {
  2343. "IDMA_IDLE",
  2344. "IDMA_PUSH_MORE_CPL_FIFO",
  2345. "IDMA_PUSH_CPL_MSG_HEADER_TO_FIFO",
  2346. "Not used",
  2347. "IDMA_PHYSADDR_SEND_PCIEHDR",
  2348. "IDMA_PHYSADDR_SEND_PAYLOAD_FIRST",
  2349. "IDMA_PHYSADDR_SEND_PAYLOAD",
  2350. "IDMA_SEND_FIFO_TO_IMSG",
  2351. "IDMA_FL_REQ_DATA_FL_PREP",
  2352. "IDMA_FL_REQ_DATA_FL",
  2353. "IDMA_FL_DROP",
  2354. "IDMA_FL_H_REQ_HEADER_FL",
  2355. "IDMA_FL_H_SEND_PCIEHDR",
  2356. "IDMA_FL_H_PUSH_CPL_FIFO",
  2357. "IDMA_FL_H_SEND_CPL",
  2358. "IDMA_FL_H_SEND_IP_HDR_FIRST",
  2359. "IDMA_FL_H_SEND_IP_HDR",
  2360. "IDMA_FL_H_REQ_NEXT_HEADER_FL",
  2361. "IDMA_FL_H_SEND_NEXT_PCIEHDR",
  2362. "IDMA_FL_H_SEND_IP_HDR_PADDING",
  2363. "IDMA_FL_D_SEND_PCIEHDR",
  2364. "IDMA_FL_D_SEND_CPL_AND_IP_HDR",
  2365. "IDMA_FL_D_REQ_NEXT_DATA_FL",
  2366. "IDMA_FL_SEND_PCIEHDR",
  2367. "IDMA_FL_PUSH_CPL_FIFO",
  2368. "IDMA_FL_SEND_CPL",
  2369. "IDMA_FL_SEND_PAYLOAD_FIRST",
  2370. "IDMA_FL_SEND_PAYLOAD",
  2371. "IDMA_FL_REQ_NEXT_DATA_FL",
  2372. "IDMA_FL_SEND_NEXT_PCIEHDR",
  2373. "IDMA_FL_SEND_PADDING",
  2374. "IDMA_FL_SEND_COMPLETION_TO_IMSG",
  2375. "IDMA_FL_SEND_FIFO_TO_IMSG",
  2376. "IDMA_FL_REQ_DATAFL_DONE",
  2377. "IDMA_FL_REQ_HEADERFL_DONE",
  2378. };
  2379. static const char * const t5_decode[] = {
  2380. "IDMA_IDLE",
  2381. "IDMA_ALMOST_IDLE",
  2382. "IDMA_PUSH_MORE_CPL_FIFO",
  2383. "IDMA_PUSH_CPL_MSG_HEADER_TO_FIFO",
  2384. "IDMA_SGEFLRFLUSH_SEND_PCIEHDR",
  2385. "IDMA_PHYSADDR_SEND_PCIEHDR",
  2386. "IDMA_PHYSADDR_SEND_PAYLOAD_FIRST",
  2387. "IDMA_PHYSADDR_SEND_PAYLOAD",
  2388. "IDMA_SEND_FIFO_TO_IMSG",
  2389. "IDMA_FL_REQ_DATA_FL",
  2390. "IDMA_FL_DROP",
  2391. "IDMA_FL_DROP_SEND_INC",
  2392. "IDMA_FL_H_REQ_HEADER_FL",
  2393. "IDMA_FL_H_SEND_PCIEHDR",
  2394. "IDMA_FL_H_PUSH_CPL_FIFO",
  2395. "IDMA_FL_H_SEND_CPL",
  2396. "IDMA_FL_H_SEND_IP_HDR_FIRST",
  2397. "IDMA_FL_H_SEND_IP_HDR",
  2398. "IDMA_FL_H_REQ_NEXT_HEADER_FL",
  2399. "IDMA_FL_H_SEND_NEXT_PCIEHDR",
  2400. "IDMA_FL_H_SEND_IP_HDR_PADDING",
  2401. "IDMA_FL_D_SEND_PCIEHDR",
  2402. "IDMA_FL_D_SEND_CPL_AND_IP_HDR",
  2403. "IDMA_FL_D_REQ_NEXT_DATA_FL",
  2404. "IDMA_FL_SEND_PCIEHDR",
  2405. "IDMA_FL_PUSH_CPL_FIFO",
  2406. "IDMA_FL_SEND_CPL",
  2407. "IDMA_FL_SEND_PAYLOAD_FIRST",
  2408. "IDMA_FL_SEND_PAYLOAD",
  2409. "IDMA_FL_REQ_NEXT_DATA_FL",
  2410. "IDMA_FL_SEND_NEXT_PCIEHDR",
  2411. "IDMA_FL_SEND_PADDING",
  2412. "IDMA_FL_SEND_COMPLETION_TO_IMSG",
  2413. };
  2414. static const u32 sge_regs[] = {
  2415. SGE_DEBUG_DATA_LOW_INDEX_2,
  2416. SGE_DEBUG_DATA_LOW_INDEX_3,
  2417. SGE_DEBUG_DATA_HIGH_INDEX_10,
  2418. };
  2419. const char **sge_idma_decode;
  2420. int sge_idma_decode_nstates;
  2421. int i;
  2422. if (is_t4(adapter->params.chip)) {
  2423. sge_idma_decode = (const char **)t4_decode;
  2424. sge_idma_decode_nstates = ARRAY_SIZE(t4_decode);
  2425. } else {
  2426. sge_idma_decode = (const char **)t5_decode;
  2427. sge_idma_decode_nstates = ARRAY_SIZE(t5_decode);
  2428. }
  2429. if (state < sge_idma_decode_nstates)
  2430. CH_WARN(adapter, "idma state %s\n", sge_idma_decode[state]);
  2431. else
  2432. CH_WARN(adapter, "idma state %d unknown\n", state);
  2433. for (i = 0; i < ARRAY_SIZE(sge_regs); i++)
  2434. CH_WARN(adapter, "SGE register %#x value %#x\n",
  2435. sge_regs[i], t4_read_reg(adapter, sge_regs[i]));
  2436. }
  2437. /**
  2438. * t4_fw_hello - establish communication with FW
  2439. * @adap: the adapter
  2440. * @mbox: mailbox to use for the FW command
  2441. * @evt_mbox: mailbox to receive async FW events
  2442. * @master: specifies the caller's willingness to be the device master
  2443. * @state: returns the current device state (if non-NULL)
  2444. *
  2445. * Issues a command to establish communication with FW. Returns either
  2446. * an error (negative integer) or the mailbox of the Master PF.
  2447. */
  2448. int t4_fw_hello(struct adapter *adap, unsigned int mbox, unsigned int evt_mbox,
  2449. enum dev_master master, enum dev_state *state)
  2450. {
  2451. int ret;
  2452. struct fw_hello_cmd c;
  2453. u32 v;
  2454. unsigned int master_mbox;
  2455. int retries = FW_CMD_HELLO_RETRIES;
  2456. retry:
  2457. memset(&c, 0, sizeof(c));
  2458. INIT_CMD(c, HELLO, WRITE);
  2459. c.err_to_clearinit = htonl(
  2460. FW_HELLO_CMD_MASTERDIS(master == MASTER_CANT) |
  2461. FW_HELLO_CMD_MASTERFORCE(master == MASTER_MUST) |
  2462. FW_HELLO_CMD_MBMASTER(master == MASTER_MUST ? mbox :
  2463. FW_HELLO_CMD_MBMASTER_MASK) |
  2464. FW_HELLO_CMD_MBASYNCNOT(evt_mbox) |
  2465. FW_HELLO_CMD_STAGE(fw_hello_cmd_stage_os) |
  2466. FW_HELLO_CMD_CLEARINIT);
  2467. /*
  2468. * Issue the HELLO command to the firmware. If it's not successful
  2469. * but indicates that we got a "busy" or "timeout" condition, retry
  2470. * the HELLO until we exhaust our retry limit.
  2471. */
  2472. ret = t4_wr_mbox(adap, mbox, &c, sizeof(c), &c);
  2473. if (ret < 0) {
  2474. if ((ret == -EBUSY || ret == -ETIMEDOUT) && retries-- > 0)
  2475. goto retry;
  2476. return ret;
  2477. }
  2478. v = ntohl(c.err_to_clearinit);
  2479. master_mbox = FW_HELLO_CMD_MBMASTER_GET(v);
  2480. if (state) {
  2481. if (v & FW_HELLO_CMD_ERR)
  2482. *state = DEV_STATE_ERR;
  2483. else if (v & FW_HELLO_CMD_INIT)
  2484. *state = DEV_STATE_INIT;
  2485. else
  2486. *state = DEV_STATE_UNINIT;
  2487. }
  2488. /*
  2489. * If we're not the Master PF then we need to wait around for the
  2490. * Master PF Driver to finish setting up the adapter.
  2491. *
  2492. * Note that we also do this wait if we're a non-Master-capable PF and
  2493. * there is no current Master PF; a Master PF may show up momentarily
  2494. * and we wouldn't want to fail pointlessly. (This can happen when an
  2495. * OS loads lots of different drivers rapidly at the same time). In
  2496. * this case, the Master PF returned by the firmware will be
  2497. * FW_PCIE_FW_MASTER_MASK so the test below will work ...
  2498. */
  2499. if ((v & (FW_HELLO_CMD_ERR|FW_HELLO_CMD_INIT)) == 0 &&
  2500. master_mbox != mbox) {
  2501. int waiting = FW_CMD_HELLO_TIMEOUT;
  2502. /*
  2503. * Wait for the firmware to either indicate an error or
  2504. * initialized state. If we see either of these we bail out
  2505. * and report the issue to the caller. If we exhaust the
  2506. * "hello timeout" and we haven't exhausted our retries, try
  2507. * again. Otherwise bail with a timeout error.
  2508. */
  2509. for (;;) {
  2510. u32 pcie_fw;
  2511. msleep(50);
  2512. waiting -= 50;
  2513. /*
  2514. * If neither Error nor Initialialized are indicated
  2515. * by the firmware keep waiting till we exaust our
  2516. * timeout ... and then retry if we haven't exhausted
  2517. * our retries ...
  2518. */
  2519. pcie_fw = t4_read_reg(adap, MA_PCIE_FW);
  2520. if (!(pcie_fw & (FW_PCIE_FW_ERR|FW_PCIE_FW_INIT))) {
  2521. if (waiting <= 0) {
  2522. if (retries-- > 0)
  2523. goto retry;
  2524. return -ETIMEDOUT;
  2525. }
  2526. continue;
  2527. }
  2528. /*
  2529. * We either have an Error or Initialized condition
  2530. * report errors preferentially.
  2531. */
  2532. if (state) {
  2533. if (pcie_fw & FW_PCIE_FW_ERR)
  2534. *state = DEV_STATE_ERR;
  2535. else if (pcie_fw & FW_PCIE_FW_INIT)
  2536. *state = DEV_STATE_INIT;
  2537. }
  2538. /*
  2539. * If we arrived before a Master PF was selected and
  2540. * there's not a valid Master PF, grab its identity
  2541. * for our caller.
  2542. */
  2543. if (master_mbox == FW_PCIE_FW_MASTER_MASK &&
  2544. (pcie_fw & FW_PCIE_FW_MASTER_VLD))
  2545. master_mbox = FW_PCIE_FW_MASTER_GET(pcie_fw);
  2546. break;
  2547. }
  2548. }
  2549. return master_mbox;
  2550. }
  2551. /**
  2552. * t4_fw_bye - end communication with FW
  2553. * @adap: the adapter
  2554. * @mbox: mailbox to use for the FW command
  2555. *
  2556. * Issues a command to terminate communication with FW.
  2557. */
  2558. int t4_fw_bye(struct adapter *adap, unsigned int mbox)
  2559. {
  2560. struct fw_bye_cmd c;
  2561. memset(&c, 0, sizeof(c));
  2562. INIT_CMD(c, BYE, WRITE);
  2563. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  2564. }
  2565. /**
  2566. * t4_init_cmd - ask FW to initialize the device
  2567. * @adap: the adapter
  2568. * @mbox: mailbox to use for the FW command
  2569. *
  2570. * Issues a command to FW to partially initialize the device. This
  2571. * performs initialization that generally doesn't depend on user input.
  2572. */
  2573. int t4_early_init(struct adapter *adap, unsigned int mbox)
  2574. {
  2575. struct fw_initialize_cmd c;
  2576. memset(&c, 0, sizeof(c));
  2577. INIT_CMD(c, INITIALIZE, WRITE);
  2578. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  2579. }
  2580. /**
  2581. * t4_fw_reset - issue a reset to FW
  2582. * @adap: the adapter
  2583. * @mbox: mailbox to use for the FW command
  2584. * @reset: specifies the type of reset to perform
  2585. *
  2586. * Issues a reset command of the specified type to FW.
  2587. */
  2588. int t4_fw_reset(struct adapter *adap, unsigned int mbox, int reset)
  2589. {
  2590. struct fw_reset_cmd c;
  2591. memset(&c, 0, sizeof(c));
  2592. INIT_CMD(c, RESET, WRITE);
  2593. c.val = htonl(reset);
  2594. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  2595. }
  2596. /**
  2597. * t4_fw_halt - issue a reset/halt to FW and put uP into RESET
  2598. * @adap: the adapter
  2599. * @mbox: mailbox to use for the FW RESET command (if desired)
  2600. * @force: force uP into RESET even if FW RESET command fails
  2601. *
  2602. * Issues a RESET command to firmware (if desired) with a HALT indication
  2603. * and then puts the microprocessor into RESET state. The RESET command
  2604. * will only be issued if a legitimate mailbox is provided (mbox <=
  2605. * FW_PCIE_FW_MASTER_MASK).
  2606. *
  2607. * This is generally used in order for the host to safely manipulate the
  2608. * adapter without fear of conflicting with whatever the firmware might
  2609. * be doing. The only way out of this state is to RESTART the firmware
  2610. * ...
  2611. */
  2612. static int t4_fw_halt(struct adapter *adap, unsigned int mbox, int force)
  2613. {
  2614. int ret = 0;
  2615. /*
  2616. * If a legitimate mailbox is provided, issue a RESET command
  2617. * with a HALT indication.
  2618. */
  2619. if (mbox <= FW_PCIE_FW_MASTER_MASK) {
  2620. struct fw_reset_cmd c;
  2621. memset(&c, 0, sizeof(c));
  2622. INIT_CMD(c, RESET, WRITE);
  2623. c.val = htonl(PIORST | PIORSTMODE);
  2624. c.halt_pkd = htonl(FW_RESET_CMD_HALT(1U));
  2625. ret = t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  2626. }
  2627. /*
  2628. * Normally we won't complete the operation if the firmware RESET
  2629. * command fails but if our caller insists we'll go ahead and put the
  2630. * uP into RESET. This can be useful if the firmware is hung or even
  2631. * missing ... We'll have to take the risk of putting the uP into
  2632. * RESET without the cooperation of firmware in that case.
  2633. *
  2634. * We also force the firmware's HALT flag to be on in case we bypassed
  2635. * the firmware RESET command above or we're dealing with old firmware
  2636. * which doesn't have the HALT capability. This will serve as a flag
  2637. * for the incoming firmware to know that it's coming out of a HALT
  2638. * rather than a RESET ... if it's new enough to understand that ...
  2639. */
  2640. if (ret == 0 || force) {
  2641. t4_set_reg_field(adap, CIM_BOOT_CFG, UPCRST, UPCRST);
  2642. t4_set_reg_field(adap, PCIE_FW, FW_PCIE_FW_HALT,
  2643. FW_PCIE_FW_HALT);
  2644. }
  2645. /*
  2646. * And we always return the result of the firmware RESET command
  2647. * even when we force the uP into RESET ...
  2648. */
  2649. return ret;
  2650. }
  2651. /**
  2652. * t4_fw_restart - restart the firmware by taking the uP out of RESET
  2653. * @adap: the adapter
  2654. * @reset: if we want to do a RESET to restart things
  2655. *
  2656. * Restart firmware previously halted by t4_fw_halt(). On successful
  2657. * return the previous PF Master remains as the new PF Master and there
  2658. * is no need to issue a new HELLO command, etc.
  2659. *
  2660. * We do this in two ways:
  2661. *
  2662. * 1. If we're dealing with newer firmware we'll simply want to take
  2663. * the chip's microprocessor out of RESET. This will cause the
  2664. * firmware to start up from its start vector. And then we'll loop
  2665. * until the firmware indicates it's started again (PCIE_FW.HALT
  2666. * reset to 0) or we timeout.
  2667. *
  2668. * 2. If we're dealing with older firmware then we'll need to RESET
  2669. * the chip since older firmware won't recognize the PCIE_FW.HALT
  2670. * flag and automatically RESET itself on startup.
  2671. */
  2672. static int t4_fw_restart(struct adapter *adap, unsigned int mbox, int reset)
  2673. {
  2674. if (reset) {
  2675. /*
  2676. * Since we're directing the RESET instead of the firmware
  2677. * doing it automatically, we need to clear the PCIE_FW.HALT
  2678. * bit.
  2679. */
  2680. t4_set_reg_field(adap, PCIE_FW, FW_PCIE_FW_HALT, 0);
  2681. /*
  2682. * If we've been given a valid mailbox, first try to get the
  2683. * firmware to do the RESET. If that works, great and we can
  2684. * return success. Otherwise, if we haven't been given a
  2685. * valid mailbox or the RESET command failed, fall back to
  2686. * hitting the chip with a hammer.
  2687. */
  2688. if (mbox <= FW_PCIE_FW_MASTER_MASK) {
  2689. t4_set_reg_field(adap, CIM_BOOT_CFG, UPCRST, 0);
  2690. msleep(100);
  2691. if (t4_fw_reset(adap, mbox,
  2692. PIORST | PIORSTMODE) == 0)
  2693. return 0;
  2694. }
  2695. t4_write_reg(adap, PL_RST, PIORST | PIORSTMODE);
  2696. msleep(2000);
  2697. } else {
  2698. int ms;
  2699. t4_set_reg_field(adap, CIM_BOOT_CFG, UPCRST, 0);
  2700. for (ms = 0; ms < FW_CMD_MAX_TIMEOUT; ) {
  2701. if (!(t4_read_reg(adap, PCIE_FW) & FW_PCIE_FW_HALT))
  2702. return 0;
  2703. msleep(100);
  2704. ms += 100;
  2705. }
  2706. return -ETIMEDOUT;
  2707. }
  2708. return 0;
  2709. }
  2710. /**
  2711. * t4_fw_upgrade - perform all of the steps necessary to upgrade FW
  2712. * @adap: the adapter
  2713. * @mbox: mailbox to use for the FW RESET command (if desired)
  2714. * @fw_data: the firmware image to write
  2715. * @size: image size
  2716. * @force: force upgrade even if firmware doesn't cooperate
  2717. *
  2718. * Perform all of the steps necessary for upgrading an adapter's
  2719. * firmware image. Normally this requires the cooperation of the
  2720. * existing firmware in order to halt all existing activities
  2721. * but if an invalid mailbox token is passed in we skip that step
  2722. * (though we'll still put the adapter microprocessor into RESET in
  2723. * that case).
  2724. *
  2725. * On successful return the new firmware will have been loaded and
  2726. * the adapter will have been fully RESET losing all previous setup
  2727. * state. On unsuccessful return the adapter may be completely hosed ...
  2728. * positive errno indicates that the adapter is ~probably~ intact, a
  2729. * negative errno indicates that things are looking bad ...
  2730. */
  2731. static int t4_fw_upgrade(struct adapter *adap, unsigned int mbox,
  2732. const u8 *fw_data, unsigned int size, int force)
  2733. {
  2734. const struct fw_hdr *fw_hdr = (const struct fw_hdr *)fw_data;
  2735. int reset, ret;
  2736. ret = t4_fw_halt(adap, mbox, force);
  2737. if (ret < 0 && !force)
  2738. return ret;
  2739. ret = t4_load_fw(adap, fw_data, size);
  2740. if (ret < 0)
  2741. return ret;
  2742. /*
  2743. * Older versions of the firmware don't understand the new
  2744. * PCIE_FW.HALT flag and so won't know to perform a RESET when they
  2745. * restart. So for newly loaded older firmware we'll have to do the
  2746. * RESET for it so it starts up on a clean slate. We can tell if
  2747. * the newly loaded firmware will handle this right by checking
  2748. * its header flags to see if it advertises the capability.
  2749. */
  2750. reset = ((ntohl(fw_hdr->flags) & FW_HDR_FLAGS_RESET_HALT) == 0);
  2751. return t4_fw_restart(adap, mbox, reset);
  2752. }
  2753. /**
  2754. * t4_fixup_host_params - fix up host-dependent parameters
  2755. * @adap: the adapter
  2756. * @page_size: the host's Base Page Size
  2757. * @cache_line_size: the host's Cache Line Size
  2758. *
  2759. * Various registers in T4 contain values which are dependent on the
  2760. * host's Base Page and Cache Line Sizes. This function will fix all of
  2761. * those registers with the appropriate values as passed in ...
  2762. */
  2763. int t4_fixup_host_params(struct adapter *adap, unsigned int page_size,
  2764. unsigned int cache_line_size)
  2765. {
  2766. unsigned int page_shift = fls(page_size) - 1;
  2767. unsigned int sge_hps = page_shift - 10;
  2768. unsigned int stat_len = cache_line_size > 64 ? 128 : 64;
  2769. unsigned int fl_align = cache_line_size < 32 ? 32 : cache_line_size;
  2770. unsigned int fl_align_log = fls(fl_align) - 1;
  2771. t4_write_reg(adap, SGE_HOST_PAGE_SIZE,
  2772. HOSTPAGESIZEPF0(sge_hps) |
  2773. HOSTPAGESIZEPF1(sge_hps) |
  2774. HOSTPAGESIZEPF2(sge_hps) |
  2775. HOSTPAGESIZEPF3(sge_hps) |
  2776. HOSTPAGESIZEPF4(sge_hps) |
  2777. HOSTPAGESIZEPF5(sge_hps) |
  2778. HOSTPAGESIZEPF6(sge_hps) |
  2779. HOSTPAGESIZEPF7(sge_hps));
  2780. t4_set_reg_field(adap, SGE_CONTROL,
  2781. INGPADBOUNDARY_MASK |
  2782. EGRSTATUSPAGESIZE_MASK,
  2783. INGPADBOUNDARY(fl_align_log - 5) |
  2784. EGRSTATUSPAGESIZE(stat_len != 64));
  2785. /*
  2786. * Adjust various SGE Free List Host Buffer Sizes.
  2787. *
  2788. * This is something of a crock since we're using fixed indices into
  2789. * the array which are also known by the sge.c code and the T4
  2790. * Firmware Configuration File. We need to come up with a much better
  2791. * approach to managing this array. For now, the first four entries
  2792. * are:
  2793. *
  2794. * 0: Host Page Size
  2795. * 1: 64KB
  2796. * 2: Buffer size corresponding to 1500 byte MTU (unpacked mode)
  2797. * 3: Buffer size corresponding to 9000 byte MTU (unpacked mode)
  2798. *
  2799. * For the single-MTU buffers in unpacked mode we need to include
  2800. * space for the SGE Control Packet Shift, 14 byte Ethernet header,
  2801. * possible 4 byte VLAN tag, all rounded up to the next Ingress Packet
  2802. * Padding boundry. All of these are accommodated in the Factory
  2803. * Default Firmware Configuration File but we need to adjust it for
  2804. * this host's cache line size.
  2805. */
  2806. t4_write_reg(adap, SGE_FL_BUFFER_SIZE0, page_size);
  2807. t4_write_reg(adap, SGE_FL_BUFFER_SIZE2,
  2808. (t4_read_reg(adap, SGE_FL_BUFFER_SIZE2) + fl_align-1)
  2809. & ~(fl_align-1));
  2810. t4_write_reg(adap, SGE_FL_BUFFER_SIZE3,
  2811. (t4_read_reg(adap, SGE_FL_BUFFER_SIZE3) + fl_align-1)
  2812. & ~(fl_align-1));
  2813. t4_write_reg(adap, ULP_RX_TDDP_PSZ, HPZ0(page_shift - 12));
  2814. return 0;
  2815. }
  2816. /**
  2817. * t4_fw_initialize - ask FW to initialize the device
  2818. * @adap: the adapter
  2819. * @mbox: mailbox to use for the FW command
  2820. *
  2821. * Issues a command to FW to partially initialize the device. This
  2822. * performs initialization that generally doesn't depend on user input.
  2823. */
  2824. int t4_fw_initialize(struct adapter *adap, unsigned int mbox)
  2825. {
  2826. struct fw_initialize_cmd c;
  2827. memset(&c, 0, sizeof(c));
  2828. INIT_CMD(c, INITIALIZE, WRITE);
  2829. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  2830. }
  2831. /**
  2832. * t4_query_params - query FW or device parameters
  2833. * @adap: the adapter
  2834. * @mbox: mailbox to use for the FW command
  2835. * @pf: the PF
  2836. * @vf: the VF
  2837. * @nparams: the number of parameters
  2838. * @params: the parameter names
  2839. * @val: the parameter values
  2840. *
  2841. * Reads the value of FW or device parameters. Up to 7 parameters can be
  2842. * queried at once.
  2843. */
  2844. int t4_query_params(struct adapter *adap, unsigned int mbox, unsigned int pf,
  2845. unsigned int vf, unsigned int nparams, const u32 *params,
  2846. u32 *val)
  2847. {
  2848. int i, ret;
  2849. struct fw_params_cmd c;
  2850. __be32 *p = &c.param[0].mnem;
  2851. if (nparams > 7)
  2852. return -EINVAL;
  2853. memset(&c, 0, sizeof(c));
  2854. c.op_to_vfn = htonl(FW_CMD_OP(FW_PARAMS_CMD) | FW_CMD_REQUEST |
  2855. FW_CMD_READ | FW_PARAMS_CMD_PFN(pf) |
  2856. FW_PARAMS_CMD_VFN(vf));
  2857. c.retval_len16 = htonl(FW_LEN16(c));
  2858. for (i = 0; i < nparams; i++, p += 2)
  2859. *p = htonl(*params++);
  2860. ret = t4_wr_mbox(adap, mbox, &c, sizeof(c), &c);
  2861. if (ret == 0)
  2862. for (i = 0, p = &c.param[0].val; i < nparams; i++, p += 2)
  2863. *val++ = ntohl(*p);
  2864. return ret;
  2865. }
  2866. /**
  2867. * t4_set_params - sets FW or device parameters
  2868. * @adap: the adapter
  2869. * @mbox: mailbox to use for the FW command
  2870. * @pf: the PF
  2871. * @vf: the VF
  2872. * @nparams: the number of parameters
  2873. * @params: the parameter names
  2874. * @val: the parameter values
  2875. *
  2876. * Sets the value of FW or device parameters. Up to 7 parameters can be
  2877. * specified at once.
  2878. */
  2879. int t4_set_params(struct adapter *adap, unsigned int mbox, unsigned int pf,
  2880. unsigned int vf, unsigned int nparams, const u32 *params,
  2881. const u32 *val)
  2882. {
  2883. struct fw_params_cmd c;
  2884. __be32 *p = &c.param[0].mnem;
  2885. if (nparams > 7)
  2886. return -EINVAL;
  2887. memset(&c, 0, sizeof(c));
  2888. c.op_to_vfn = htonl(FW_CMD_OP(FW_PARAMS_CMD) | FW_CMD_REQUEST |
  2889. FW_CMD_WRITE | FW_PARAMS_CMD_PFN(pf) |
  2890. FW_PARAMS_CMD_VFN(vf));
  2891. c.retval_len16 = htonl(FW_LEN16(c));
  2892. while (nparams--) {
  2893. *p++ = htonl(*params++);
  2894. *p++ = htonl(*val++);
  2895. }
  2896. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  2897. }
  2898. /**
  2899. * t4_cfg_pfvf - configure PF/VF resource limits
  2900. * @adap: the adapter
  2901. * @mbox: mailbox to use for the FW command
  2902. * @pf: the PF being configured
  2903. * @vf: the VF being configured
  2904. * @txq: the max number of egress queues
  2905. * @txq_eth_ctrl: the max number of egress Ethernet or control queues
  2906. * @rxqi: the max number of interrupt-capable ingress queues
  2907. * @rxq: the max number of interruptless ingress queues
  2908. * @tc: the PCI traffic class
  2909. * @vi: the max number of virtual interfaces
  2910. * @cmask: the channel access rights mask for the PF/VF
  2911. * @pmask: the port access rights mask for the PF/VF
  2912. * @nexact: the maximum number of exact MPS filters
  2913. * @rcaps: read capabilities
  2914. * @wxcaps: write/execute capabilities
  2915. *
  2916. * Configures resource limits and capabilities for a physical or virtual
  2917. * function.
  2918. */
  2919. int t4_cfg_pfvf(struct adapter *adap, unsigned int mbox, unsigned int pf,
  2920. unsigned int vf, unsigned int txq, unsigned int txq_eth_ctrl,
  2921. unsigned int rxqi, unsigned int rxq, unsigned int tc,
  2922. unsigned int vi, unsigned int cmask, unsigned int pmask,
  2923. unsigned int nexact, unsigned int rcaps, unsigned int wxcaps)
  2924. {
  2925. struct fw_pfvf_cmd c;
  2926. memset(&c, 0, sizeof(c));
  2927. c.op_to_vfn = htonl(FW_CMD_OP(FW_PFVF_CMD) | FW_CMD_REQUEST |
  2928. FW_CMD_WRITE | FW_PFVF_CMD_PFN(pf) |
  2929. FW_PFVF_CMD_VFN(vf));
  2930. c.retval_len16 = htonl(FW_LEN16(c));
  2931. c.niqflint_niq = htonl(FW_PFVF_CMD_NIQFLINT(rxqi) |
  2932. FW_PFVF_CMD_NIQ(rxq));
  2933. c.type_to_neq = htonl(FW_PFVF_CMD_CMASK(cmask) |
  2934. FW_PFVF_CMD_PMASK(pmask) |
  2935. FW_PFVF_CMD_NEQ(txq));
  2936. c.tc_to_nexactf = htonl(FW_PFVF_CMD_TC(tc) | FW_PFVF_CMD_NVI(vi) |
  2937. FW_PFVF_CMD_NEXACTF(nexact));
  2938. c.r_caps_to_nethctrl = htonl(FW_PFVF_CMD_R_CAPS(rcaps) |
  2939. FW_PFVF_CMD_WX_CAPS(wxcaps) |
  2940. FW_PFVF_CMD_NETHCTRL(txq_eth_ctrl));
  2941. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  2942. }
  2943. /**
  2944. * t4_alloc_vi - allocate a virtual interface
  2945. * @adap: the adapter
  2946. * @mbox: mailbox to use for the FW command
  2947. * @port: physical port associated with the VI
  2948. * @pf: the PF owning the VI
  2949. * @vf: the VF owning the VI
  2950. * @nmac: number of MAC addresses needed (1 to 5)
  2951. * @mac: the MAC addresses of the VI
  2952. * @rss_size: size of RSS table slice associated with this VI
  2953. *
  2954. * Allocates a virtual interface for the given physical port. If @mac is
  2955. * not %NULL it contains the MAC addresses of the VI as assigned by FW.
  2956. * @mac should be large enough to hold @nmac Ethernet addresses, they are
  2957. * stored consecutively so the space needed is @nmac * 6 bytes.
  2958. * Returns a negative error number or the non-negative VI id.
  2959. */
  2960. int t4_alloc_vi(struct adapter *adap, unsigned int mbox, unsigned int port,
  2961. unsigned int pf, unsigned int vf, unsigned int nmac, u8 *mac,
  2962. unsigned int *rss_size)
  2963. {
  2964. int ret;
  2965. struct fw_vi_cmd c;
  2966. memset(&c, 0, sizeof(c));
  2967. c.op_to_vfn = htonl(FW_CMD_OP(FW_VI_CMD) | FW_CMD_REQUEST |
  2968. FW_CMD_WRITE | FW_CMD_EXEC |
  2969. FW_VI_CMD_PFN(pf) | FW_VI_CMD_VFN(vf));
  2970. c.alloc_to_len16 = htonl(FW_VI_CMD_ALLOC | FW_LEN16(c));
  2971. c.portid_pkd = FW_VI_CMD_PORTID(port);
  2972. c.nmac = nmac - 1;
  2973. ret = t4_wr_mbox(adap, mbox, &c, sizeof(c), &c);
  2974. if (ret)
  2975. return ret;
  2976. if (mac) {
  2977. memcpy(mac, c.mac, sizeof(c.mac));
  2978. switch (nmac) {
  2979. case 5:
  2980. memcpy(mac + 24, c.nmac3, sizeof(c.nmac3));
  2981. case 4:
  2982. memcpy(mac + 18, c.nmac2, sizeof(c.nmac2));
  2983. case 3:
  2984. memcpy(mac + 12, c.nmac1, sizeof(c.nmac1));
  2985. case 2:
  2986. memcpy(mac + 6, c.nmac0, sizeof(c.nmac0));
  2987. }
  2988. }
  2989. if (rss_size)
  2990. *rss_size = FW_VI_CMD_RSSSIZE_GET(ntohs(c.rsssize_pkd));
  2991. return FW_VI_CMD_VIID_GET(ntohs(c.type_viid));
  2992. }
  2993. /**
  2994. * t4_set_rxmode - set Rx properties of a virtual interface
  2995. * @adap: the adapter
  2996. * @mbox: mailbox to use for the FW command
  2997. * @viid: the VI id
  2998. * @mtu: the new MTU or -1
  2999. * @promisc: 1 to enable promiscuous mode, 0 to disable it, -1 no change
  3000. * @all_multi: 1 to enable all-multi mode, 0 to disable it, -1 no change
  3001. * @bcast: 1 to enable broadcast Rx, 0 to disable it, -1 no change
  3002. * @vlanex: 1 to enable HW VLAN extraction, 0 to disable it, -1 no change
  3003. * @sleep_ok: if true we may sleep while awaiting command completion
  3004. *
  3005. * Sets Rx properties of a virtual interface.
  3006. */
  3007. int t4_set_rxmode(struct adapter *adap, unsigned int mbox, unsigned int viid,
  3008. int mtu, int promisc, int all_multi, int bcast, int vlanex,
  3009. bool sleep_ok)
  3010. {
  3011. struct fw_vi_rxmode_cmd c;
  3012. /* convert to FW values */
  3013. if (mtu < 0)
  3014. mtu = FW_RXMODE_MTU_NO_CHG;
  3015. if (promisc < 0)
  3016. promisc = FW_VI_RXMODE_CMD_PROMISCEN_MASK;
  3017. if (all_multi < 0)
  3018. all_multi = FW_VI_RXMODE_CMD_ALLMULTIEN_MASK;
  3019. if (bcast < 0)
  3020. bcast = FW_VI_RXMODE_CMD_BROADCASTEN_MASK;
  3021. if (vlanex < 0)
  3022. vlanex = FW_VI_RXMODE_CMD_VLANEXEN_MASK;
  3023. memset(&c, 0, sizeof(c));
  3024. c.op_to_viid = htonl(FW_CMD_OP(FW_VI_RXMODE_CMD) | FW_CMD_REQUEST |
  3025. FW_CMD_WRITE | FW_VI_RXMODE_CMD_VIID(viid));
  3026. c.retval_len16 = htonl(FW_LEN16(c));
  3027. c.mtu_to_vlanexen = htonl(FW_VI_RXMODE_CMD_MTU(mtu) |
  3028. FW_VI_RXMODE_CMD_PROMISCEN(promisc) |
  3029. FW_VI_RXMODE_CMD_ALLMULTIEN(all_multi) |
  3030. FW_VI_RXMODE_CMD_BROADCASTEN(bcast) |
  3031. FW_VI_RXMODE_CMD_VLANEXEN(vlanex));
  3032. return t4_wr_mbox_meat(adap, mbox, &c, sizeof(c), NULL, sleep_ok);
  3033. }
  3034. /**
  3035. * t4_alloc_mac_filt - allocates exact-match filters for MAC addresses
  3036. * @adap: the adapter
  3037. * @mbox: mailbox to use for the FW command
  3038. * @viid: the VI id
  3039. * @free: if true any existing filters for this VI id are first removed
  3040. * @naddr: the number of MAC addresses to allocate filters for (up to 7)
  3041. * @addr: the MAC address(es)
  3042. * @idx: where to store the index of each allocated filter
  3043. * @hash: pointer to hash address filter bitmap
  3044. * @sleep_ok: call is allowed to sleep
  3045. *
  3046. * Allocates an exact-match filter for each of the supplied addresses and
  3047. * sets it to the corresponding address. If @idx is not %NULL it should
  3048. * have at least @naddr entries, each of which will be set to the index of
  3049. * the filter allocated for the corresponding MAC address. If a filter
  3050. * could not be allocated for an address its index is set to 0xffff.
  3051. * If @hash is not %NULL addresses that fail to allocate an exact filter
  3052. * are hashed and update the hash filter bitmap pointed at by @hash.
  3053. *
  3054. * Returns a negative error number or the number of filters allocated.
  3055. */
  3056. int t4_alloc_mac_filt(struct adapter *adap, unsigned int mbox,
  3057. unsigned int viid, bool free, unsigned int naddr,
  3058. const u8 **addr, u16 *idx, u64 *hash, bool sleep_ok)
  3059. {
  3060. int i, ret;
  3061. struct fw_vi_mac_cmd c;
  3062. struct fw_vi_mac_exact *p;
  3063. unsigned int max_naddr = is_t4(adap->params.chip) ?
  3064. NUM_MPS_CLS_SRAM_L_INSTANCES :
  3065. NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
  3066. if (naddr > 7)
  3067. return -EINVAL;
  3068. memset(&c, 0, sizeof(c));
  3069. c.op_to_viid = htonl(FW_CMD_OP(FW_VI_MAC_CMD) | FW_CMD_REQUEST |
  3070. FW_CMD_WRITE | (free ? FW_CMD_EXEC : 0) |
  3071. FW_VI_MAC_CMD_VIID(viid));
  3072. c.freemacs_to_len16 = htonl(FW_VI_MAC_CMD_FREEMACS(free) |
  3073. FW_CMD_LEN16((naddr + 2) / 2));
  3074. for (i = 0, p = c.u.exact; i < naddr; i++, p++) {
  3075. p->valid_to_idx = htons(FW_VI_MAC_CMD_VALID |
  3076. FW_VI_MAC_CMD_IDX(FW_VI_MAC_ADD_MAC));
  3077. memcpy(p->macaddr, addr[i], sizeof(p->macaddr));
  3078. }
  3079. ret = t4_wr_mbox_meat(adap, mbox, &c, sizeof(c), &c, sleep_ok);
  3080. if (ret)
  3081. return ret;
  3082. for (i = 0, p = c.u.exact; i < naddr; i++, p++) {
  3083. u16 index = FW_VI_MAC_CMD_IDX_GET(ntohs(p->valid_to_idx));
  3084. if (idx)
  3085. idx[i] = index >= max_naddr ? 0xffff : index;
  3086. if (index < max_naddr)
  3087. ret++;
  3088. else if (hash)
  3089. *hash |= (1ULL << hash_mac_addr(addr[i]));
  3090. }
  3091. return ret;
  3092. }
  3093. /**
  3094. * t4_change_mac - modifies the exact-match filter for a MAC address
  3095. * @adap: the adapter
  3096. * @mbox: mailbox to use for the FW command
  3097. * @viid: the VI id
  3098. * @idx: index of existing filter for old value of MAC address, or -1
  3099. * @addr: the new MAC address value
  3100. * @persist: whether a new MAC allocation should be persistent
  3101. * @add_smt: if true also add the address to the HW SMT
  3102. *
  3103. * Modifies an exact-match filter and sets it to the new MAC address.
  3104. * Note that in general it is not possible to modify the value of a given
  3105. * filter so the generic way to modify an address filter is to free the one
  3106. * being used by the old address value and allocate a new filter for the
  3107. * new address value. @idx can be -1 if the address is a new addition.
  3108. *
  3109. * Returns a negative error number or the index of the filter with the new
  3110. * MAC value.
  3111. */
  3112. int t4_change_mac(struct adapter *adap, unsigned int mbox, unsigned int viid,
  3113. int idx, const u8 *addr, bool persist, bool add_smt)
  3114. {
  3115. int ret, mode;
  3116. struct fw_vi_mac_cmd c;
  3117. struct fw_vi_mac_exact *p = c.u.exact;
  3118. unsigned int max_mac_addr = is_t4(adap->params.chip) ?
  3119. NUM_MPS_CLS_SRAM_L_INSTANCES :
  3120. NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
  3121. if (idx < 0) /* new allocation */
  3122. idx = persist ? FW_VI_MAC_ADD_PERSIST_MAC : FW_VI_MAC_ADD_MAC;
  3123. mode = add_smt ? FW_VI_MAC_SMT_AND_MPSTCAM : FW_VI_MAC_MPS_TCAM_ENTRY;
  3124. memset(&c, 0, sizeof(c));
  3125. c.op_to_viid = htonl(FW_CMD_OP(FW_VI_MAC_CMD) | FW_CMD_REQUEST |
  3126. FW_CMD_WRITE | FW_VI_MAC_CMD_VIID(viid));
  3127. c.freemacs_to_len16 = htonl(FW_CMD_LEN16(1));
  3128. p->valid_to_idx = htons(FW_VI_MAC_CMD_VALID |
  3129. FW_VI_MAC_CMD_SMAC_RESULT(mode) |
  3130. FW_VI_MAC_CMD_IDX(idx));
  3131. memcpy(p->macaddr, addr, sizeof(p->macaddr));
  3132. ret = t4_wr_mbox(adap, mbox, &c, sizeof(c), &c);
  3133. if (ret == 0) {
  3134. ret = FW_VI_MAC_CMD_IDX_GET(ntohs(p->valid_to_idx));
  3135. if (ret >= max_mac_addr)
  3136. ret = -ENOMEM;
  3137. }
  3138. return ret;
  3139. }
  3140. /**
  3141. * t4_set_addr_hash - program the MAC inexact-match hash filter
  3142. * @adap: the adapter
  3143. * @mbox: mailbox to use for the FW command
  3144. * @viid: the VI id
  3145. * @ucast: whether the hash filter should also match unicast addresses
  3146. * @vec: the value to be written to the hash filter
  3147. * @sleep_ok: call is allowed to sleep
  3148. *
  3149. * Sets the 64-bit inexact-match hash filter for a virtual interface.
  3150. */
  3151. int t4_set_addr_hash(struct adapter *adap, unsigned int mbox, unsigned int viid,
  3152. bool ucast, u64 vec, bool sleep_ok)
  3153. {
  3154. struct fw_vi_mac_cmd c;
  3155. memset(&c, 0, sizeof(c));
  3156. c.op_to_viid = htonl(FW_CMD_OP(FW_VI_MAC_CMD) | FW_CMD_REQUEST |
  3157. FW_CMD_WRITE | FW_VI_ENABLE_CMD_VIID(viid));
  3158. c.freemacs_to_len16 = htonl(FW_VI_MAC_CMD_HASHVECEN |
  3159. FW_VI_MAC_CMD_HASHUNIEN(ucast) |
  3160. FW_CMD_LEN16(1));
  3161. c.u.hash.hashvec = cpu_to_be64(vec);
  3162. return t4_wr_mbox_meat(adap, mbox, &c, sizeof(c), NULL, sleep_ok);
  3163. }
  3164. /**
  3165. * t4_enable_vi - enable/disable a virtual interface
  3166. * @adap: the adapter
  3167. * @mbox: mailbox to use for the FW command
  3168. * @viid: the VI id
  3169. * @rx_en: 1=enable Rx, 0=disable Rx
  3170. * @tx_en: 1=enable Tx, 0=disable Tx
  3171. *
  3172. * Enables/disables a virtual interface.
  3173. */
  3174. int t4_enable_vi(struct adapter *adap, unsigned int mbox, unsigned int viid,
  3175. bool rx_en, bool tx_en)
  3176. {
  3177. struct fw_vi_enable_cmd c;
  3178. memset(&c, 0, sizeof(c));
  3179. c.op_to_viid = htonl(FW_CMD_OP(FW_VI_ENABLE_CMD) | FW_CMD_REQUEST |
  3180. FW_CMD_EXEC | FW_VI_ENABLE_CMD_VIID(viid));
  3181. c.ien_to_len16 = htonl(FW_VI_ENABLE_CMD_IEN(rx_en) |
  3182. FW_VI_ENABLE_CMD_EEN(tx_en) | FW_LEN16(c));
  3183. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  3184. }
  3185. /**
  3186. * t4_identify_port - identify a VI's port by blinking its LED
  3187. * @adap: the adapter
  3188. * @mbox: mailbox to use for the FW command
  3189. * @viid: the VI id
  3190. * @nblinks: how many times to blink LED at 2.5 Hz
  3191. *
  3192. * Identifies a VI's port by blinking its LED.
  3193. */
  3194. int t4_identify_port(struct adapter *adap, unsigned int mbox, unsigned int viid,
  3195. unsigned int nblinks)
  3196. {
  3197. struct fw_vi_enable_cmd c;
  3198. memset(&c, 0, sizeof(c));
  3199. c.op_to_viid = htonl(FW_CMD_OP(FW_VI_ENABLE_CMD) | FW_CMD_REQUEST |
  3200. FW_CMD_EXEC | FW_VI_ENABLE_CMD_VIID(viid));
  3201. c.ien_to_len16 = htonl(FW_VI_ENABLE_CMD_LED | FW_LEN16(c));
  3202. c.blinkdur = htons(nblinks);
  3203. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  3204. }
  3205. /**
  3206. * t4_iq_free - free an ingress queue and its FLs
  3207. * @adap: the adapter
  3208. * @mbox: mailbox to use for the FW command
  3209. * @pf: the PF owning the queues
  3210. * @vf: the VF owning the queues
  3211. * @iqtype: the ingress queue type
  3212. * @iqid: ingress queue id
  3213. * @fl0id: FL0 queue id or 0xffff if no attached FL0
  3214. * @fl1id: FL1 queue id or 0xffff if no attached FL1
  3215. *
  3216. * Frees an ingress queue and its associated FLs, if any.
  3217. */
  3218. int t4_iq_free(struct adapter *adap, unsigned int mbox, unsigned int pf,
  3219. unsigned int vf, unsigned int iqtype, unsigned int iqid,
  3220. unsigned int fl0id, unsigned int fl1id)
  3221. {
  3222. struct fw_iq_cmd c;
  3223. memset(&c, 0, sizeof(c));
  3224. c.op_to_vfn = htonl(FW_CMD_OP(FW_IQ_CMD) | FW_CMD_REQUEST |
  3225. FW_CMD_EXEC | FW_IQ_CMD_PFN(pf) |
  3226. FW_IQ_CMD_VFN(vf));
  3227. c.alloc_to_len16 = htonl(FW_IQ_CMD_FREE | FW_LEN16(c));
  3228. c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE(iqtype));
  3229. c.iqid = htons(iqid);
  3230. c.fl0id = htons(fl0id);
  3231. c.fl1id = htons(fl1id);
  3232. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  3233. }
  3234. /**
  3235. * t4_eth_eq_free - free an Ethernet egress queue
  3236. * @adap: the adapter
  3237. * @mbox: mailbox to use for the FW command
  3238. * @pf: the PF owning the queue
  3239. * @vf: the VF owning the queue
  3240. * @eqid: egress queue id
  3241. *
  3242. * Frees an Ethernet egress queue.
  3243. */
  3244. int t4_eth_eq_free(struct adapter *adap, unsigned int mbox, unsigned int pf,
  3245. unsigned int vf, unsigned int eqid)
  3246. {
  3247. struct fw_eq_eth_cmd c;
  3248. memset(&c, 0, sizeof(c));
  3249. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_ETH_CMD) | FW_CMD_REQUEST |
  3250. FW_CMD_EXEC | FW_EQ_ETH_CMD_PFN(pf) |
  3251. FW_EQ_ETH_CMD_VFN(vf));
  3252. c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_FREE | FW_LEN16(c));
  3253. c.eqid_pkd = htonl(FW_EQ_ETH_CMD_EQID(eqid));
  3254. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  3255. }
  3256. /**
  3257. * t4_ctrl_eq_free - free a control egress queue
  3258. * @adap: the adapter
  3259. * @mbox: mailbox to use for the FW command
  3260. * @pf: the PF owning the queue
  3261. * @vf: the VF owning the queue
  3262. * @eqid: egress queue id
  3263. *
  3264. * Frees a control egress queue.
  3265. */
  3266. int t4_ctrl_eq_free(struct adapter *adap, unsigned int mbox, unsigned int pf,
  3267. unsigned int vf, unsigned int eqid)
  3268. {
  3269. struct fw_eq_ctrl_cmd c;
  3270. memset(&c, 0, sizeof(c));
  3271. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST |
  3272. FW_CMD_EXEC | FW_EQ_CTRL_CMD_PFN(pf) |
  3273. FW_EQ_CTRL_CMD_VFN(vf));
  3274. c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_FREE | FW_LEN16(c));
  3275. c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_EQID(eqid));
  3276. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  3277. }
  3278. /**
  3279. * t4_ofld_eq_free - free an offload egress queue
  3280. * @adap: the adapter
  3281. * @mbox: mailbox to use for the FW command
  3282. * @pf: the PF owning the queue
  3283. * @vf: the VF owning the queue
  3284. * @eqid: egress queue id
  3285. *
  3286. * Frees a control egress queue.
  3287. */
  3288. int t4_ofld_eq_free(struct adapter *adap, unsigned int mbox, unsigned int pf,
  3289. unsigned int vf, unsigned int eqid)
  3290. {
  3291. struct fw_eq_ofld_cmd c;
  3292. memset(&c, 0, sizeof(c));
  3293. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST |
  3294. FW_CMD_EXEC | FW_EQ_OFLD_CMD_PFN(pf) |
  3295. FW_EQ_OFLD_CMD_VFN(vf));
  3296. c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_FREE | FW_LEN16(c));
  3297. c.eqid_pkd = htonl(FW_EQ_OFLD_CMD_EQID(eqid));
  3298. return t4_wr_mbox(adap, mbox, &c, sizeof(c), NULL);
  3299. }
  3300. /**
  3301. * t4_handle_fw_rpl - process a FW reply message
  3302. * @adap: the adapter
  3303. * @rpl: start of the FW message
  3304. *
  3305. * Processes a FW message, such as link state change messages.
  3306. */
  3307. int t4_handle_fw_rpl(struct adapter *adap, const __be64 *rpl)
  3308. {
  3309. u8 opcode = *(const u8 *)rpl;
  3310. if (opcode == FW_PORT_CMD) { /* link/module state change message */
  3311. int speed = 0, fc = 0;
  3312. const struct fw_port_cmd *p = (void *)rpl;
  3313. int chan = FW_PORT_CMD_PORTID_GET(ntohl(p->op_to_portid));
  3314. int port = adap->chan_map[chan];
  3315. struct port_info *pi = adap2pinfo(adap, port);
  3316. struct link_config *lc = &pi->link_cfg;
  3317. u32 stat = ntohl(p->u.info.lstatus_to_modtype);
  3318. int link_ok = (stat & FW_PORT_CMD_LSTATUS) != 0;
  3319. u32 mod = FW_PORT_CMD_MODTYPE_GET(stat);
  3320. if (stat & FW_PORT_CMD_RXPAUSE)
  3321. fc |= PAUSE_RX;
  3322. if (stat & FW_PORT_CMD_TXPAUSE)
  3323. fc |= PAUSE_TX;
  3324. if (stat & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_100M))
  3325. speed = 100;
  3326. else if (stat & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_1G))
  3327. speed = 1000;
  3328. else if (stat & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_10G))
  3329. speed = 10000;
  3330. else if (stat & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_40G))
  3331. speed = 40000;
  3332. if (link_ok != lc->link_ok || speed != lc->speed ||
  3333. fc != lc->fc) { /* something changed */
  3334. lc->link_ok = link_ok;
  3335. lc->speed = speed;
  3336. lc->fc = fc;
  3337. t4_os_link_changed(adap, port, link_ok);
  3338. }
  3339. if (mod != pi->mod_type) {
  3340. pi->mod_type = mod;
  3341. t4_os_portmod_changed(adap, port);
  3342. }
  3343. }
  3344. return 0;
  3345. }
  3346. static void get_pci_mode(struct adapter *adapter, struct pci_params *p)
  3347. {
  3348. u16 val;
  3349. if (pci_is_pcie(adapter->pdev)) {
  3350. pcie_capability_read_word(adapter->pdev, PCI_EXP_LNKSTA, &val);
  3351. p->speed = val & PCI_EXP_LNKSTA_CLS;
  3352. p->width = (val & PCI_EXP_LNKSTA_NLW) >> 4;
  3353. }
  3354. }
  3355. /**
  3356. * init_link_config - initialize a link's SW state
  3357. * @lc: structure holding the link state
  3358. * @caps: link capabilities
  3359. *
  3360. * Initializes the SW state maintained for each link, including the link's
  3361. * capabilities and default speed/flow-control/autonegotiation settings.
  3362. */
  3363. static void init_link_config(struct link_config *lc, unsigned int caps)
  3364. {
  3365. lc->supported = caps;
  3366. lc->requested_speed = 0;
  3367. lc->speed = 0;
  3368. lc->requested_fc = lc->fc = PAUSE_RX | PAUSE_TX;
  3369. if (lc->supported & FW_PORT_CAP_ANEG) {
  3370. lc->advertising = lc->supported & ADVERT_MASK;
  3371. lc->autoneg = AUTONEG_ENABLE;
  3372. lc->requested_fc |= PAUSE_AUTONEG;
  3373. } else {
  3374. lc->advertising = 0;
  3375. lc->autoneg = AUTONEG_DISABLE;
  3376. }
  3377. }
  3378. int t4_wait_dev_ready(struct adapter *adap)
  3379. {
  3380. if (t4_read_reg(adap, PL_WHOAMI) != 0xffffffff)
  3381. return 0;
  3382. msleep(500);
  3383. return t4_read_reg(adap, PL_WHOAMI) != 0xffffffff ? 0 : -EIO;
  3384. }
  3385. static int get_flash_params(struct adapter *adap)
  3386. {
  3387. int ret;
  3388. u32 info;
  3389. ret = sf1_write(adap, 1, 1, 0, SF_RD_ID);
  3390. if (!ret)
  3391. ret = sf1_read(adap, 3, 0, 1, &info);
  3392. t4_write_reg(adap, SF_OP, 0); /* unlock SF */
  3393. if (ret)
  3394. return ret;
  3395. if ((info & 0xff) != 0x20) /* not a Numonix flash */
  3396. return -EINVAL;
  3397. info >>= 16; /* log2 of size */
  3398. if (info >= 0x14 && info < 0x18)
  3399. adap->params.sf_nsec = 1 << (info - 16);
  3400. else if (info == 0x18)
  3401. adap->params.sf_nsec = 64;
  3402. else
  3403. return -EINVAL;
  3404. adap->params.sf_size = 1 << info;
  3405. adap->params.sf_fw_start =
  3406. t4_read_reg(adap, CIM_BOOT_CFG) & BOOTADDR_MASK;
  3407. return 0;
  3408. }
  3409. /**
  3410. * t4_prep_adapter - prepare SW and HW for operation
  3411. * @adapter: the adapter
  3412. * @reset: if true perform a HW reset
  3413. *
  3414. * Initialize adapter SW state for the various HW modules, set initial
  3415. * values for some adapter tunables, take PHYs out of reset, and
  3416. * initialize the MDIO interface.
  3417. */
  3418. int t4_prep_adapter(struct adapter *adapter)
  3419. {
  3420. int ret, ver;
  3421. uint16_t device_id;
  3422. u32 pl_rev;
  3423. ret = t4_wait_dev_ready(adapter);
  3424. if (ret < 0)
  3425. return ret;
  3426. get_pci_mode(adapter, &adapter->params.pci);
  3427. pl_rev = G_REV(t4_read_reg(adapter, PL_REV));
  3428. ret = get_flash_params(adapter);
  3429. if (ret < 0) {
  3430. dev_err(adapter->pdev_dev, "error %d identifying flash\n", ret);
  3431. return ret;
  3432. }
  3433. /* Retrieve adapter's device ID
  3434. */
  3435. pci_read_config_word(adapter->pdev, PCI_DEVICE_ID, &device_id);
  3436. ver = device_id >> 12;
  3437. adapter->params.chip = 0;
  3438. switch (ver) {
  3439. case CHELSIO_T4:
  3440. adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
  3441. break;
  3442. case CHELSIO_T5:
  3443. adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
  3444. break;
  3445. default:
  3446. dev_err(adapter->pdev_dev, "Device %d is not supported\n",
  3447. device_id);
  3448. return -EINVAL;
  3449. }
  3450. init_cong_ctrl(adapter->params.a_wnd, adapter->params.b_wnd);
  3451. /*
  3452. * Default port for debugging in case we can't reach FW.
  3453. */
  3454. adapter->params.nports = 1;
  3455. adapter->params.portvec = 1;
  3456. adapter->params.vpd.cclk = 50000;
  3457. return 0;
  3458. }
  3459. /**
  3460. * t4_init_tp_params - initialize adap->params.tp
  3461. * @adap: the adapter
  3462. *
  3463. * Initialize various fields of the adapter's TP Parameters structure.
  3464. */
  3465. int t4_init_tp_params(struct adapter *adap)
  3466. {
  3467. int chan;
  3468. u32 v;
  3469. v = t4_read_reg(adap, TP_TIMER_RESOLUTION);
  3470. adap->params.tp.tre = TIMERRESOLUTION_GET(v);
  3471. adap->params.tp.dack_re = DELAYEDACKRESOLUTION_GET(v);
  3472. /* MODQ_REQ_MAP defaults to setting queues 0-3 to chan 0-3 */
  3473. for (chan = 0; chan < NCHAN; chan++)
  3474. adap->params.tp.tx_modq[chan] = chan;
  3475. /* Cache the adapter's Compressed Filter Mode and global Incress
  3476. * Configuration.
  3477. */
  3478. t4_read_indirect(adap, TP_PIO_ADDR, TP_PIO_DATA,
  3479. &adap->params.tp.vlan_pri_map, 1,
  3480. TP_VLAN_PRI_MAP);
  3481. t4_read_indirect(adap, TP_PIO_ADDR, TP_PIO_DATA,
  3482. &adap->params.tp.ingress_config, 1,
  3483. TP_INGRESS_CONFIG);
  3484. /* Now that we have TP_VLAN_PRI_MAP cached, we can calculate the field
  3485. * shift positions of several elements of the Compressed Filter Tuple
  3486. * for this adapter which we need frequently ...
  3487. */
  3488. adap->params.tp.vlan_shift = t4_filter_field_shift(adap, F_VLAN);
  3489. adap->params.tp.vnic_shift = t4_filter_field_shift(adap, F_VNIC_ID);
  3490. adap->params.tp.port_shift = t4_filter_field_shift(adap, F_PORT);
  3491. adap->params.tp.protocol_shift = t4_filter_field_shift(adap,
  3492. F_PROTOCOL);
  3493. /* If TP_INGRESS_CONFIG.VNID == 0, then TP_VLAN_PRI_MAP.VNIC_ID
  3494. * represents the presense of an Outer VLAN instead of a VNIC ID.
  3495. */
  3496. if ((adap->params.tp.ingress_config & F_VNIC) == 0)
  3497. adap->params.tp.vnic_shift = -1;
  3498. return 0;
  3499. }
  3500. /**
  3501. * t4_filter_field_shift - calculate filter field shift
  3502. * @adap: the adapter
  3503. * @filter_sel: the desired field (from TP_VLAN_PRI_MAP bits)
  3504. *
  3505. * Return the shift position of a filter field within the Compressed
  3506. * Filter Tuple. The filter field is specified via its selection bit
  3507. * within TP_VLAN_PRI_MAL (filter mode). E.g. F_VLAN.
  3508. */
  3509. int t4_filter_field_shift(const struct adapter *adap, int filter_sel)
  3510. {
  3511. unsigned int filter_mode = adap->params.tp.vlan_pri_map;
  3512. unsigned int sel;
  3513. int field_shift;
  3514. if ((filter_mode & filter_sel) == 0)
  3515. return -1;
  3516. for (sel = 1, field_shift = 0; sel < filter_sel; sel <<= 1) {
  3517. switch (filter_mode & sel) {
  3518. case F_FCOE:
  3519. field_shift += W_FT_FCOE;
  3520. break;
  3521. case F_PORT:
  3522. field_shift += W_FT_PORT;
  3523. break;
  3524. case F_VNIC_ID:
  3525. field_shift += W_FT_VNIC_ID;
  3526. break;
  3527. case F_VLAN:
  3528. field_shift += W_FT_VLAN;
  3529. break;
  3530. case F_TOS:
  3531. field_shift += W_FT_TOS;
  3532. break;
  3533. case F_PROTOCOL:
  3534. field_shift += W_FT_PROTOCOL;
  3535. break;
  3536. case F_ETHERTYPE:
  3537. field_shift += W_FT_ETHERTYPE;
  3538. break;
  3539. case F_MACMATCH:
  3540. field_shift += W_FT_MACMATCH;
  3541. break;
  3542. case F_MPSHITTYPE:
  3543. field_shift += W_FT_MPSHITTYPE;
  3544. break;
  3545. case F_FRAGMENTATION:
  3546. field_shift += W_FT_FRAGMENTATION;
  3547. break;
  3548. }
  3549. }
  3550. return field_shift;
  3551. }
  3552. int t4_port_init(struct adapter *adap, int mbox, int pf, int vf)
  3553. {
  3554. u8 addr[6];
  3555. int ret, i, j = 0;
  3556. struct fw_port_cmd c;
  3557. struct fw_rss_vi_config_cmd rvc;
  3558. memset(&c, 0, sizeof(c));
  3559. memset(&rvc, 0, sizeof(rvc));
  3560. for_each_port(adap, i) {
  3561. unsigned int rss_size;
  3562. struct port_info *p = adap2pinfo(adap, i);
  3563. while ((adap->params.portvec & (1 << j)) == 0)
  3564. j++;
  3565. c.op_to_portid = htonl(FW_CMD_OP(FW_PORT_CMD) |
  3566. FW_CMD_REQUEST | FW_CMD_READ |
  3567. FW_PORT_CMD_PORTID(j));
  3568. c.action_to_len16 = htonl(
  3569. FW_PORT_CMD_ACTION(FW_PORT_ACTION_GET_PORT_INFO) |
  3570. FW_LEN16(c));
  3571. ret = t4_wr_mbox(adap, mbox, &c, sizeof(c), &c);
  3572. if (ret)
  3573. return ret;
  3574. ret = t4_alloc_vi(adap, mbox, j, pf, vf, 1, addr, &rss_size);
  3575. if (ret < 0)
  3576. return ret;
  3577. p->viid = ret;
  3578. p->tx_chan = j;
  3579. p->lport = j;
  3580. p->rss_size = rss_size;
  3581. memcpy(adap->port[i]->dev_addr, addr, ETH_ALEN);
  3582. adap->port[i]->dev_port = j;
  3583. ret = ntohl(c.u.info.lstatus_to_modtype);
  3584. p->mdio_addr = (ret & FW_PORT_CMD_MDIOCAP) ?
  3585. FW_PORT_CMD_MDIOADDR_GET(ret) : -1;
  3586. p->port_type = FW_PORT_CMD_PTYPE_GET(ret);
  3587. p->mod_type = FW_PORT_MOD_TYPE_NA;
  3588. rvc.op_to_viid = htonl(FW_CMD_OP(FW_RSS_VI_CONFIG_CMD) |
  3589. FW_CMD_REQUEST | FW_CMD_READ |
  3590. FW_RSS_VI_CONFIG_CMD_VIID(p->viid));
  3591. rvc.retval_len16 = htonl(FW_LEN16(rvc));
  3592. ret = t4_wr_mbox(adap, mbox, &rvc, sizeof(rvc), &rvc);
  3593. if (ret)
  3594. return ret;
  3595. p->rss_mode = ntohl(rvc.u.basicvirtual.defaultq_to_udpen);
  3596. init_link_config(&p->link_cfg, ntohs(c.u.info.pcap));
  3597. j++;
  3598. }
  3599. return 0;
  3600. }