t4_hw.c 87 KB

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