t4_hw.c 104 KB

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