t4vf_hw.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. /*
  2. * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet
  3. * driver for Linux.
  4. *
  5. * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/pci.h>
  36. #include "t4vf_common.h"
  37. #include "t4vf_defs.h"
  38. #include "../cxgb4/t4_regs.h"
  39. #include "../cxgb4/t4fw_api.h"
  40. /*
  41. * Wait for the device to become ready (signified by our "who am I" register
  42. * returning a value other than all 1's). Return an error if it doesn't
  43. * become ready ...
  44. */
  45. int __devinit t4vf_wait_dev_ready(struct adapter *adapter)
  46. {
  47. const u32 whoami = T4VF_PL_BASE_ADDR + PL_VF_WHOAMI;
  48. const u32 notready1 = 0xffffffff;
  49. const u32 notready2 = 0xeeeeeeee;
  50. u32 val;
  51. val = t4_read_reg(adapter, whoami);
  52. if (val != notready1 && val != notready2)
  53. return 0;
  54. msleep(500);
  55. val = t4_read_reg(adapter, whoami);
  56. if (val != notready1 && val != notready2)
  57. return 0;
  58. else
  59. return -EIO;
  60. }
  61. /*
  62. * Get the reply to a mailbox command and store it in @rpl in big-endian order
  63. * (since the firmware data structures are specified in a big-endian layout).
  64. */
  65. static void get_mbox_rpl(struct adapter *adapter, __be64 *rpl, int size,
  66. u32 mbox_data)
  67. {
  68. for ( ; size; size -= 8, mbox_data += 8)
  69. *rpl++ = cpu_to_be64(t4_read_reg64(adapter, mbox_data));
  70. }
  71. /*
  72. * Dump contents of mailbox with a leading tag.
  73. */
  74. static void dump_mbox(struct adapter *adapter, const char *tag, u32 mbox_data)
  75. {
  76. dev_err(adapter->pdev_dev,
  77. "mbox %s: %llx %llx %llx %llx %llx %llx %llx %llx\n", tag,
  78. (unsigned long long)t4_read_reg64(adapter, mbox_data + 0),
  79. (unsigned long long)t4_read_reg64(adapter, mbox_data + 8),
  80. (unsigned long long)t4_read_reg64(adapter, mbox_data + 16),
  81. (unsigned long long)t4_read_reg64(adapter, mbox_data + 24),
  82. (unsigned long long)t4_read_reg64(adapter, mbox_data + 32),
  83. (unsigned long long)t4_read_reg64(adapter, mbox_data + 40),
  84. (unsigned long long)t4_read_reg64(adapter, mbox_data + 48),
  85. (unsigned long long)t4_read_reg64(adapter, mbox_data + 56));
  86. }
  87. /**
  88. * t4vf_wr_mbox_core - send a command to FW through the mailbox
  89. * @adapter: the adapter
  90. * @cmd: the command to write
  91. * @size: command length in bytes
  92. * @rpl: where to optionally store the reply
  93. * @sleep_ok: if true we may sleep while awaiting command completion
  94. *
  95. * Sends the given command to FW through the mailbox and waits for the
  96. * FW to execute the command. If @rpl is not %NULL it is used to store
  97. * the FW's reply to the command. The command and its optional reply
  98. * are of the same length. FW can take up to 500 ms to respond.
  99. * @sleep_ok determines whether we may sleep while awaiting the response.
  100. * If sleeping is allowed we use progressive backoff otherwise we spin.
  101. *
  102. * The return value is 0 on success or a negative errno on failure. A
  103. * failure can happen either because we are not able to execute the
  104. * command or FW executes it but signals an error. In the latter case
  105. * the return value is the error code indicated by FW (negated).
  106. */
  107. int t4vf_wr_mbox_core(struct adapter *adapter, const void *cmd, int size,
  108. void *rpl, bool sleep_ok)
  109. {
  110. static const int delay[] = {
  111. 1, 1, 3, 5, 10, 10, 20, 50, 100
  112. };
  113. u32 v;
  114. int i, ms, delay_idx;
  115. const __be64 *p;
  116. u32 mbox_data = T4VF_MBDATA_BASE_ADDR;
  117. u32 mbox_ctl = T4VF_CIM_BASE_ADDR + CIM_VF_EXT_MAILBOX_CTRL;
  118. /*
  119. * Commands must be multiples of 16 bytes in length and may not be
  120. * larger than the size of the Mailbox Data register array.
  121. */
  122. if ((size % 16) != 0 ||
  123. size > NUM_CIM_VF_MAILBOX_DATA_INSTANCES * 4)
  124. return -EINVAL;
  125. /*
  126. * Loop trying to get ownership of the mailbox. Return an error
  127. * if we can't gain ownership.
  128. */
  129. v = MBOWNER_GET(t4_read_reg(adapter, mbox_ctl));
  130. for (i = 0; v == MBOX_OWNER_NONE && i < 3; i++)
  131. v = MBOWNER_GET(t4_read_reg(adapter, mbox_ctl));
  132. if (v != MBOX_OWNER_DRV)
  133. return v == MBOX_OWNER_FW ? -EBUSY : -ETIMEDOUT;
  134. /*
  135. * Write the command array into the Mailbox Data register array and
  136. * transfer ownership of the mailbox to the firmware.
  137. *
  138. * For the VFs, the Mailbox Data "registers" are actually backed by
  139. * T4's "MA" interface rather than PL Registers (as is the case for
  140. * the PFs). Because these are in different coherency domains, the
  141. * write to the VF's PL-register-backed Mailbox Control can race in
  142. * front of the writes to the MA-backed VF Mailbox Data "registers".
  143. * So we need to do a read-back on at least one byte of the VF Mailbox
  144. * Data registers before doing the write to the VF Mailbox Control
  145. * register.
  146. */
  147. for (i = 0, p = cmd; i < size; i += 8)
  148. t4_write_reg64(adapter, mbox_data + i, be64_to_cpu(*p++));
  149. t4_read_reg(adapter, mbox_data); /* flush write */
  150. t4_write_reg(adapter, mbox_ctl,
  151. MBMSGVALID | MBOWNER(MBOX_OWNER_FW));
  152. t4_read_reg(adapter, mbox_ctl); /* flush write */
  153. /*
  154. * Spin waiting for firmware to acknowledge processing our command.
  155. */
  156. delay_idx = 0;
  157. ms = delay[0];
  158. for (i = 0; i < FW_CMD_MAX_TIMEOUT; i += ms) {
  159. if (sleep_ok) {
  160. ms = delay[delay_idx];
  161. if (delay_idx < ARRAY_SIZE(delay) - 1)
  162. delay_idx++;
  163. msleep(ms);
  164. } else
  165. mdelay(ms);
  166. /*
  167. * If we're the owner, see if this is the reply we wanted.
  168. */
  169. v = t4_read_reg(adapter, mbox_ctl);
  170. if (MBOWNER_GET(v) == MBOX_OWNER_DRV) {
  171. /*
  172. * If the Message Valid bit isn't on, revoke ownership
  173. * of the mailbox and continue waiting for our reply.
  174. */
  175. if ((v & MBMSGVALID) == 0) {
  176. t4_write_reg(adapter, mbox_ctl,
  177. MBOWNER(MBOX_OWNER_NONE));
  178. continue;
  179. }
  180. /*
  181. * We now have our reply. Extract the command return
  182. * value, copy the reply back to our caller's buffer
  183. * (if specified) and revoke ownership of the mailbox.
  184. * We return the (negated) firmware command return
  185. * code (this depends on FW_SUCCESS == 0).
  186. */
  187. /* return value in low-order little-endian word */
  188. v = t4_read_reg(adapter, mbox_data);
  189. if (FW_CMD_RETVAL_GET(v))
  190. dump_mbox(adapter, "FW Error", mbox_data);
  191. if (rpl) {
  192. /* request bit in high-order BE word */
  193. WARN_ON((be32_to_cpu(*(const u32 *)cmd)
  194. & FW_CMD_REQUEST) == 0);
  195. get_mbox_rpl(adapter, rpl, size, mbox_data);
  196. WARN_ON((be32_to_cpu(*(u32 *)rpl)
  197. & FW_CMD_REQUEST) != 0);
  198. }
  199. t4_write_reg(adapter, mbox_ctl,
  200. MBOWNER(MBOX_OWNER_NONE));
  201. return -FW_CMD_RETVAL_GET(v);
  202. }
  203. }
  204. /*
  205. * We timed out. Return the error ...
  206. */
  207. dump_mbox(adapter, "FW Timeout", mbox_data);
  208. return -ETIMEDOUT;
  209. }
  210. /**
  211. * hash_mac_addr - return the hash value of a MAC address
  212. * @addr: the 48-bit Ethernet MAC address
  213. *
  214. * Hashes a MAC address according to the hash function used by hardware
  215. * inexact (hash) address matching.
  216. */
  217. static int hash_mac_addr(const u8 *addr)
  218. {
  219. u32 a = ((u32)addr[0] << 16) | ((u32)addr[1] << 8) | addr[2];
  220. u32 b = ((u32)addr[3] << 16) | ((u32)addr[4] << 8) | addr[5];
  221. a ^= b;
  222. a ^= (a >> 12);
  223. a ^= (a >> 6);
  224. return a & 0x3f;
  225. }
  226. /**
  227. * init_link_config - initialize a link's SW state
  228. * @lc: structure holding the link state
  229. * @caps: link capabilities
  230. *
  231. * Initializes the SW state maintained for each link, including the link's
  232. * capabilities and default speed/flow-control/autonegotiation settings.
  233. */
  234. static void __devinit init_link_config(struct link_config *lc,
  235. unsigned int caps)
  236. {
  237. lc->supported = caps;
  238. lc->requested_speed = 0;
  239. lc->speed = 0;
  240. lc->requested_fc = lc->fc = PAUSE_RX | PAUSE_TX;
  241. if (lc->supported & SUPPORTED_Autoneg) {
  242. lc->advertising = lc->supported;
  243. lc->autoneg = AUTONEG_ENABLE;
  244. lc->requested_fc |= PAUSE_AUTONEG;
  245. } else {
  246. lc->advertising = 0;
  247. lc->autoneg = AUTONEG_DISABLE;
  248. }
  249. }
  250. /**
  251. * t4vf_port_init - initialize port hardware/software state
  252. * @adapter: the adapter
  253. * @pidx: the adapter port index
  254. */
  255. int __devinit t4vf_port_init(struct adapter *adapter, int pidx)
  256. {
  257. struct port_info *pi = adap2pinfo(adapter, pidx);
  258. struct fw_vi_cmd vi_cmd, vi_rpl;
  259. struct fw_port_cmd port_cmd, port_rpl;
  260. int v;
  261. u32 word;
  262. /*
  263. * Execute a VI Read command to get our Virtual Interface information
  264. * like MAC address, etc.
  265. */
  266. memset(&vi_cmd, 0, sizeof(vi_cmd));
  267. vi_cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
  268. FW_CMD_REQUEST |
  269. FW_CMD_READ);
  270. vi_cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(vi_cmd));
  271. vi_cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID(pi->viid));
  272. v = t4vf_wr_mbox(adapter, &vi_cmd, sizeof(vi_cmd), &vi_rpl);
  273. if (v)
  274. return v;
  275. BUG_ON(pi->port_id != FW_VI_CMD_PORTID_GET(vi_rpl.portid_pkd));
  276. pi->rss_size = FW_VI_CMD_RSSSIZE_GET(be16_to_cpu(vi_rpl.rsssize_pkd));
  277. t4_os_set_hw_addr(adapter, pidx, vi_rpl.mac);
  278. /*
  279. * If we don't have read access to our port information, we're done
  280. * now. Otherwise, execute a PORT Read command to get it ...
  281. */
  282. if (!(adapter->params.vfres.r_caps & FW_CMD_CAP_PORT))
  283. return 0;
  284. memset(&port_cmd, 0, sizeof(port_cmd));
  285. port_cmd.op_to_portid = cpu_to_be32(FW_CMD_OP(FW_PORT_CMD) |
  286. FW_CMD_REQUEST |
  287. FW_CMD_READ |
  288. FW_PORT_CMD_PORTID(pi->port_id));
  289. port_cmd.action_to_len16 =
  290. cpu_to_be32(FW_PORT_CMD_ACTION(FW_PORT_ACTION_GET_PORT_INFO) |
  291. FW_LEN16(port_cmd));
  292. v = t4vf_wr_mbox(adapter, &port_cmd, sizeof(port_cmd), &port_rpl);
  293. if (v)
  294. return v;
  295. v = 0;
  296. word = be16_to_cpu(port_rpl.u.info.pcap);
  297. if (word & FW_PORT_CAP_SPEED_100M)
  298. v |= SUPPORTED_100baseT_Full;
  299. if (word & FW_PORT_CAP_SPEED_1G)
  300. v |= SUPPORTED_1000baseT_Full;
  301. if (word & FW_PORT_CAP_SPEED_10G)
  302. v |= SUPPORTED_10000baseT_Full;
  303. if (word & FW_PORT_CAP_ANEG)
  304. v |= SUPPORTED_Autoneg;
  305. init_link_config(&pi->link_cfg, v);
  306. return 0;
  307. }
  308. /**
  309. * t4vf_fw_reset - issue a reset to FW
  310. * @adapter: the adapter
  311. *
  312. * Issues a reset command to FW. For a Physical Function this would
  313. * result in the Firmware reseting all of its state. For a Virtual
  314. * Function this just resets the state associated with the VF.
  315. */
  316. int t4vf_fw_reset(struct adapter *adapter)
  317. {
  318. struct fw_reset_cmd cmd;
  319. memset(&cmd, 0, sizeof(cmd));
  320. cmd.op_to_write = cpu_to_be32(FW_CMD_OP(FW_RESET_CMD) |
  321. FW_CMD_WRITE);
  322. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  323. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  324. }
  325. /**
  326. * t4vf_query_params - query FW or device parameters
  327. * @adapter: the adapter
  328. * @nparams: the number of parameters
  329. * @params: the parameter names
  330. * @vals: the parameter values
  331. *
  332. * Reads the values of firmware or device parameters. Up to 7 parameters
  333. * can be queried at once.
  334. */
  335. int t4vf_query_params(struct adapter *adapter, unsigned int nparams,
  336. const u32 *params, u32 *vals)
  337. {
  338. int i, ret;
  339. struct fw_params_cmd cmd, rpl;
  340. struct fw_params_param *p;
  341. size_t len16;
  342. if (nparams > 7)
  343. return -EINVAL;
  344. memset(&cmd, 0, sizeof(cmd));
  345. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PARAMS_CMD) |
  346. FW_CMD_REQUEST |
  347. FW_CMD_READ);
  348. len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
  349. param[nparams].mnem), 16);
  350. cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
  351. for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++)
  352. p->mnem = htonl(*params++);
  353. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  354. if (ret == 0)
  355. for (i = 0, p = &rpl.param[0]; i < nparams; i++, p++)
  356. *vals++ = be32_to_cpu(p->val);
  357. return ret;
  358. }
  359. /**
  360. * t4vf_set_params - sets FW or device parameters
  361. * @adapter: the adapter
  362. * @nparams: the number of parameters
  363. * @params: the parameter names
  364. * @vals: the parameter values
  365. *
  366. * Sets the values of firmware or device parameters. Up to 7 parameters
  367. * can be specified at once.
  368. */
  369. int t4vf_set_params(struct adapter *adapter, unsigned int nparams,
  370. const u32 *params, const u32 *vals)
  371. {
  372. int i;
  373. struct fw_params_cmd cmd;
  374. struct fw_params_param *p;
  375. size_t len16;
  376. if (nparams > 7)
  377. return -EINVAL;
  378. memset(&cmd, 0, sizeof(cmd));
  379. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PARAMS_CMD) |
  380. FW_CMD_REQUEST |
  381. FW_CMD_WRITE);
  382. len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
  383. param[nparams]), 16);
  384. cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
  385. for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++) {
  386. p->mnem = cpu_to_be32(*params++);
  387. p->val = cpu_to_be32(*vals++);
  388. }
  389. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  390. }
  391. /**
  392. * t4vf_get_sge_params - retrieve adapter Scatter gather Engine parameters
  393. * @adapter: the adapter
  394. *
  395. * Retrieves various core SGE parameters in the form of hardware SGE
  396. * register values. The caller is responsible for decoding these as
  397. * needed. The SGE parameters are stored in @adapter->params.sge.
  398. */
  399. int t4vf_get_sge_params(struct adapter *adapter)
  400. {
  401. struct sge_params *sge_params = &adapter->params.sge;
  402. u32 params[7], vals[7];
  403. int v;
  404. params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  405. FW_PARAMS_PARAM_XYZ(SGE_CONTROL));
  406. params[1] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  407. FW_PARAMS_PARAM_XYZ(SGE_HOST_PAGE_SIZE));
  408. params[2] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  409. FW_PARAMS_PARAM_XYZ(SGE_FL_BUFFER_SIZE0));
  410. params[3] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  411. FW_PARAMS_PARAM_XYZ(SGE_FL_BUFFER_SIZE1));
  412. params[4] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  413. FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_0_AND_1));
  414. params[5] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  415. FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_2_AND_3));
  416. params[6] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  417. FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_4_AND_5));
  418. v = t4vf_query_params(adapter, 7, params, vals);
  419. if (v)
  420. return v;
  421. sge_params->sge_control = vals[0];
  422. sge_params->sge_host_page_size = vals[1];
  423. sge_params->sge_fl_buffer_size[0] = vals[2];
  424. sge_params->sge_fl_buffer_size[1] = vals[3];
  425. sge_params->sge_timer_value_0_and_1 = vals[4];
  426. sge_params->sge_timer_value_2_and_3 = vals[5];
  427. sge_params->sge_timer_value_4_and_5 = vals[6];
  428. params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  429. FW_PARAMS_PARAM_XYZ(SGE_INGRESS_RX_THRESHOLD));
  430. v = t4vf_query_params(adapter, 1, params, vals);
  431. if (v)
  432. return v;
  433. sge_params->sge_ingress_rx_threshold = vals[0];
  434. return 0;
  435. }
  436. /**
  437. * t4vf_get_vpd_params - retrieve device VPD paremeters
  438. * @adapter: the adapter
  439. *
  440. * Retrives various device Vital Product Data parameters. The parameters
  441. * are stored in @adapter->params.vpd.
  442. */
  443. int t4vf_get_vpd_params(struct adapter *adapter)
  444. {
  445. struct vpd_params *vpd_params = &adapter->params.vpd;
  446. u32 params[7], vals[7];
  447. int v;
  448. params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
  449. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CCLK));
  450. v = t4vf_query_params(adapter, 1, params, vals);
  451. if (v)
  452. return v;
  453. vpd_params->cclk = vals[0];
  454. return 0;
  455. }
  456. /**
  457. * t4vf_get_dev_params - retrieve device paremeters
  458. * @adapter: the adapter
  459. *
  460. * Retrives various device parameters. The parameters are stored in
  461. * @adapter->params.dev.
  462. */
  463. int t4vf_get_dev_params(struct adapter *adapter)
  464. {
  465. struct dev_params *dev_params = &adapter->params.dev;
  466. u32 params[7], vals[7];
  467. int v;
  468. params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
  469. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_FWREV));
  470. params[1] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
  471. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_TPREV));
  472. v = t4vf_query_params(adapter, 2, params, vals);
  473. if (v)
  474. return v;
  475. dev_params->fwrev = vals[0];
  476. dev_params->tprev = vals[1];
  477. return 0;
  478. }
  479. /**
  480. * t4vf_get_rss_glb_config - retrieve adapter RSS Global Configuration
  481. * @adapter: the adapter
  482. *
  483. * Retrieves global RSS mode and parameters with which we have to live
  484. * and stores them in the @adapter's RSS parameters.
  485. */
  486. int t4vf_get_rss_glb_config(struct adapter *adapter)
  487. {
  488. struct rss_params *rss = &adapter->params.rss;
  489. struct fw_rss_glb_config_cmd cmd, rpl;
  490. int v;
  491. /*
  492. * Execute an RSS Global Configuration read command to retrieve
  493. * our RSS configuration.
  494. */
  495. memset(&cmd, 0, sizeof(cmd));
  496. cmd.op_to_write = cpu_to_be32(FW_CMD_OP(FW_RSS_GLB_CONFIG_CMD) |
  497. FW_CMD_REQUEST |
  498. FW_CMD_READ);
  499. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  500. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  501. if (v)
  502. return v;
  503. /*
  504. * Transate the big-endian RSS Global Configuration into our
  505. * cpu-endian format based on the RSS mode. We also do first level
  506. * filtering at this point to weed out modes which don't support
  507. * VF Drivers ...
  508. */
  509. rss->mode = FW_RSS_GLB_CONFIG_CMD_MODE_GET(
  510. be32_to_cpu(rpl.u.manual.mode_pkd));
  511. switch (rss->mode) {
  512. case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
  513. u32 word = be32_to_cpu(
  514. rpl.u.basicvirtual.synmapen_to_hashtoeplitz);
  515. rss->u.basicvirtual.synmapen =
  516. ((word & FW_RSS_GLB_CONFIG_CMD_SYNMAPEN) != 0);
  517. rss->u.basicvirtual.syn4tupenipv6 =
  518. ((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV6) != 0);
  519. rss->u.basicvirtual.syn2tupenipv6 =
  520. ((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV6) != 0);
  521. rss->u.basicvirtual.syn4tupenipv4 =
  522. ((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV4) != 0);
  523. rss->u.basicvirtual.syn2tupenipv4 =
  524. ((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV4) != 0);
  525. rss->u.basicvirtual.ofdmapen =
  526. ((word & FW_RSS_GLB_CONFIG_CMD_OFDMAPEN) != 0);
  527. rss->u.basicvirtual.tnlmapen =
  528. ((word & FW_RSS_GLB_CONFIG_CMD_TNLMAPEN) != 0);
  529. rss->u.basicvirtual.tnlalllookup =
  530. ((word & FW_RSS_GLB_CONFIG_CMD_TNLALLLKP) != 0);
  531. rss->u.basicvirtual.hashtoeplitz =
  532. ((word & FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ) != 0);
  533. /* we need at least Tunnel Map Enable to be set */
  534. if (!rss->u.basicvirtual.tnlmapen)
  535. return -EINVAL;
  536. break;
  537. }
  538. default:
  539. /* all unknown/unsupported RSS modes result in an error */
  540. return -EINVAL;
  541. }
  542. return 0;
  543. }
  544. /**
  545. * t4vf_get_vfres - retrieve VF resource limits
  546. * @adapter: the adapter
  547. *
  548. * Retrieves configured resource limits and capabilities for a virtual
  549. * function. The results are stored in @adapter->vfres.
  550. */
  551. int t4vf_get_vfres(struct adapter *adapter)
  552. {
  553. struct vf_resources *vfres = &adapter->params.vfres;
  554. struct fw_pfvf_cmd cmd, rpl;
  555. int v;
  556. u32 word;
  557. /*
  558. * Execute PFVF Read command to get VF resource limits; bail out early
  559. * with error on command failure.
  560. */
  561. memset(&cmd, 0, sizeof(cmd));
  562. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PFVF_CMD) |
  563. FW_CMD_REQUEST |
  564. FW_CMD_READ);
  565. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  566. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  567. if (v)
  568. return v;
  569. /*
  570. * Extract VF resource limits and return success.
  571. */
  572. word = be32_to_cpu(rpl.niqflint_niq);
  573. vfres->niqflint = FW_PFVF_CMD_NIQFLINT_GET(word);
  574. vfres->niq = FW_PFVF_CMD_NIQ_GET(word);
  575. word = be32_to_cpu(rpl.type_to_neq);
  576. vfres->neq = FW_PFVF_CMD_NEQ_GET(word);
  577. vfres->pmask = FW_PFVF_CMD_PMASK_GET(word);
  578. word = be32_to_cpu(rpl.tc_to_nexactf);
  579. vfres->tc = FW_PFVF_CMD_TC_GET(word);
  580. vfres->nvi = FW_PFVF_CMD_NVI_GET(word);
  581. vfres->nexactf = FW_PFVF_CMD_NEXACTF_GET(word);
  582. word = be32_to_cpu(rpl.r_caps_to_nethctrl);
  583. vfres->r_caps = FW_PFVF_CMD_R_CAPS_GET(word);
  584. vfres->wx_caps = FW_PFVF_CMD_WX_CAPS_GET(word);
  585. vfres->nethctrl = FW_PFVF_CMD_NETHCTRL_GET(word);
  586. return 0;
  587. }
  588. /**
  589. * t4vf_read_rss_vi_config - read a VI's RSS configuration
  590. * @adapter: the adapter
  591. * @viid: Virtual Interface ID
  592. * @config: pointer to host-native VI RSS Configuration buffer
  593. *
  594. * Reads the Virtual Interface's RSS configuration information and
  595. * translates it into CPU-native format.
  596. */
  597. int t4vf_read_rss_vi_config(struct adapter *adapter, unsigned int viid,
  598. union rss_vi_config *config)
  599. {
  600. struct fw_rss_vi_config_cmd cmd, rpl;
  601. int v;
  602. memset(&cmd, 0, sizeof(cmd));
  603. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_VI_CONFIG_CMD) |
  604. FW_CMD_REQUEST |
  605. FW_CMD_READ |
  606. FW_RSS_VI_CONFIG_CMD_VIID(viid));
  607. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  608. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  609. if (v)
  610. return v;
  611. switch (adapter->params.rss.mode) {
  612. case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
  613. u32 word = be32_to_cpu(rpl.u.basicvirtual.defaultq_to_udpen);
  614. config->basicvirtual.ip6fourtupen =
  615. ((word & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN) != 0);
  616. config->basicvirtual.ip6twotupen =
  617. ((word & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN) != 0);
  618. config->basicvirtual.ip4fourtupen =
  619. ((word & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN) != 0);
  620. config->basicvirtual.ip4twotupen =
  621. ((word & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN) != 0);
  622. config->basicvirtual.udpen =
  623. ((word & FW_RSS_VI_CONFIG_CMD_UDPEN) != 0);
  624. config->basicvirtual.defaultq =
  625. FW_RSS_VI_CONFIG_CMD_DEFAULTQ_GET(word);
  626. break;
  627. }
  628. default:
  629. return -EINVAL;
  630. }
  631. return 0;
  632. }
  633. /**
  634. * t4vf_write_rss_vi_config - write a VI's RSS configuration
  635. * @adapter: the adapter
  636. * @viid: Virtual Interface ID
  637. * @config: pointer to host-native VI RSS Configuration buffer
  638. *
  639. * Write the Virtual Interface's RSS configuration information
  640. * (translating it into firmware-native format before writing).
  641. */
  642. int t4vf_write_rss_vi_config(struct adapter *adapter, unsigned int viid,
  643. union rss_vi_config *config)
  644. {
  645. struct fw_rss_vi_config_cmd cmd, rpl;
  646. memset(&cmd, 0, sizeof(cmd));
  647. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_VI_CONFIG_CMD) |
  648. FW_CMD_REQUEST |
  649. FW_CMD_WRITE |
  650. FW_RSS_VI_CONFIG_CMD_VIID(viid));
  651. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  652. switch (adapter->params.rss.mode) {
  653. case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
  654. u32 word = 0;
  655. if (config->basicvirtual.ip6fourtupen)
  656. word |= FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
  657. if (config->basicvirtual.ip6twotupen)
  658. word |= FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
  659. if (config->basicvirtual.ip4fourtupen)
  660. word |= FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
  661. if (config->basicvirtual.ip4twotupen)
  662. word |= FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
  663. if (config->basicvirtual.udpen)
  664. word |= FW_RSS_VI_CONFIG_CMD_UDPEN;
  665. word |= FW_RSS_VI_CONFIG_CMD_DEFAULTQ(
  666. config->basicvirtual.defaultq);
  667. cmd.u.basicvirtual.defaultq_to_udpen = cpu_to_be32(word);
  668. break;
  669. }
  670. default:
  671. return -EINVAL;
  672. }
  673. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  674. }
  675. /**
  676. * t4vf_config_rss_range - configure a portion of the RSS mapping table
  677. * @adapter: the adapter
  678. * @viid: Virtual Interface of RSS Table Slice
  679. * @start: starting entry in the table to write
  680. * @n: how many table entries to write
  681. * @rspq: values for the "Response Queue" (Ingress Queue) lookup table
  682. * @nrspq: number of values in @rspq
  683. *
  684. * Programs the selected part of the VI's RSS mapping table with the
  685. * provided values. If @nrspq < @n the supplied values are used repeatedly
  686. * until the full table range is populated.
  687. *
  688. * The caller must ensure the values in @rspq are in the range 0..1023.
  689. */
  690. int t4vf_config_rss_range(struct adapter *adapter, unsigned int viid,
  691. int start, int n, const u16 *rspq, int nrspq)
  692. {
  693. const u16 *rsp = rspq;
  694. const u16 *rsp_end = rspq+nrspq;
  695. struct fw_rss_ind_tbl_cmd cmd;
  696. /*
  697. * Initialize firmware command template to write the RSS table.
  698. */
  699. memset(&cmd, 0, sizeof(cmd));
  700. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_IND_TBL_CMD) |
  701. FW_CMD_REQUEST |
  702. FW_CMD_WRITE |
  703. FW_RSS_IND_TBL_CMD_VIID(viid));
  704. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  705. /*
  706. * Each firmware RSS command can accommodate up to 32 RSS Ingress
  707. * Queue Identifiers. These Ingress Queue IDs are packed three to
  708. * a 32-bit word as 10-bit values with the upper remaining 2 bits
  709. * reserved.
  710. */
  711. while (n > 0) {
  712. __be32 *qp = &cmd.iq0_to_iq2;
  713. int nq = min(n, 32);
  714. int ret;
  715. /*
  716. * Set up the firmware RSS command header to send the next
  717. * "nq" Ingress Queue IDs to the firmware.
  718. */
  719. cmd.niqid = cpu_to_be16(nq);
  720. cmd.startidx = cpu_to_be16(start);
  721. /*
  722. * "nq" more done for the start of the next loop.
  723. */
  724. start += nq;
  725. n -= nq;
  726. /*
  727. * While there are still Ingress Queue IDs to stuff into the
  728. * current firmware RSS command, retrieve them from the
  729. * Ingress Queue ID array and insert them into the command.
  730. */
  731. while (nq > 0) {
  732. /*
  733. * Grab up to the next 3 Ingress Queue IDs (wrapping
  734. * around the Ingress Queue ID array if necessary) and
  735. * insert them into the firmware RSS command at the
  736. * current 3-tuple position within the commad.
  737. */
  738. u16 qbuf[3];
  739. u16 *qbp = qbuf;
  740. int nqbuf = min(3, nq);
  741. nq -= nqbuf;
  742. qbuf[0] = qbuf[1] = qbuf[2] = 0;
  743. while (nqbuf) {
  744. nqbuf--;
  745. *qbp++ = *rsp++;
  746. if (rsp >= rsp_end)
  747. rsp = rspq;
  748. }
  749. *qp++ = cpu_to_be32(FW_RSS_IND_TBL_CMD_IQ0(qbuf[0]) |
  750. FW_RSS_IND_TBL_CMD_IQ1(qbuf[1]) |
  751. FW_RSS_IND_TBL_CMD_IQ2(qbuf[2]));
  752. }
  753. /*
  754. * Send this portion of the RRS table update to the firmware;
  755. * bail out on any errors.
  756. */
  757. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  758. if (ret)
  759. return ret;
  760. }
  761. return 0;
  762. }
  763. /**
  764. * t4vf_alloc_vi - allocate a virtual interface on a port
  765. * @adapter: the adapter
  766. * @port_id: physical port associated with the VI
  767. *
  768. * Allocate a new Virtual Interface and bind it to the indicated
  769. * physical port. Return the new Virtual Interface Identifier on
  770. * success, or a [negative] error number on failure.
  771. */
  772. int t4vf_alloc_vi(struct adapter *adapter, int port_id)
  773. {
  774. struct fw_vi_cmd cmd, rpl;
  775. int v;
  776. /*
  777. * Execute a VI command to allocate Virtual Interface and return its
  778. * VIID.
  779. */
  780. memset(&cmd, 0, sizeof(cmd));
  781. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
  782. FW_CMD_REQUEST |
  783. FW_CMD_WRITE |
  784. FW_CMD_EXEC);
  785. cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
  786. FW_VI_CMD_ALLOC);
  787. cmd.portid_pkd = FW_VI_CMD_PORTID(port_id);
  788. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  789. if (v)
  790. return v;
  791. return FW_VI_CMD_VIID_GET(be16_to_cpu(rpl.type_viid));
  792. }
  793. /**
  794. * t4vf_free_vi -- free a virtual interface
  795. * @adapter: the adapter
  796. * @viid: the virtual interface identifier
  797. *
  798. * Free a previously allocated Virtual Interface. Return an error on
  799. * failure.
  800. */
  801. int t4vf_free_vi(struct adapter *adapter, int viid)
  802. {
  803. struct fw_vi_cmd cmd;
  804. /*
  805. * Execute a VI command to free the Virtual Interface.
  806. */
  807. memset(&cmd, 0, sizeof(cmd));
  808. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
  809. FW_CMD_REQUEST |
  810. FW_CMD_EXEC);
  811. cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
  812. FW_VI_CMD_FREE);
  813. cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID(viid));
  814. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  815. }
  816. /**
  817. * t4vf_enable_vi - enable/disable a virtual interface
  818. * @adapter: the adapter
  819. * @viid: the Virtual Interface ID
  820. * @rx_en: 1=enable Rx, 0=disable Rx
  821. * @tx_en: 1=enable Tx, 0=disable Tx
  822. *
  823. * Enables/disables a virtual interface.
  824. */
  825. int t4vf_enable_vi(struct adapter *adapter, unsigned int viid,
  826. bool rx_en, bool tx_en)
  827. {
  828. struct fw_vi_enable_cmd cmd;
  829. memset(&cmd, 0, sizeof(cmd));
  830. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_ENABLE_CMD) |
  831. FW_CMD_REQUEST |
  832. FW_CMD_EXEC |
  833. FW_VI_ENABLE_CMD_VIID(viid));
  834. cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_IEN(rx_en) |
  835. FW_VI_ENABLE_CMD_EEN(tx_en) |
  836. FW_LEN16(cmd));
  837. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  838. }
  839. /**
  840. * t4vf_identify_port - identify a VI's port by blinking its LED
  841. * @adapter: the adapter
  842. * @viid: the Virtual Interface ID
  843. * @nblinks: how many times to blink LED at 2.5 Hz
  844. *
  845. * Identifies a VI's port by blinking its LED.
  846. */
  847. int t4vf_identify_port(struct adapter *adapter, unsigned int viid,
  848. unsigned int nblinks)
  849. {
  850. struct fw_vi_enable_cmd cmd;
  851. memset(&cmd, 0, sizeof(cmd));
  852. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_ENABLE_CMD) |
  853. FW_CMD_REQUEST |
  854. FW_CMD_EXEC |
  855. FW_VI_ENABLE_CMD_VIID(viid));
  856. cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_LED |
  857. FW_LEN16(cmd));
  858. cmd.blinkdur = cpu_to_be16(nblinks);
  859. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  860. }
  861. /**
  862. * t4vf_set_rxmode - set Rx properties of a virtual interface
  863. * @adapter: the adapter
  864. * @viid: the VI id
  865. * @mtu: the new MTU or -1 for no change
  866. * @promisc: 1 to enable promiscuous mode, 0 to disable it, -1 no change
  867. * @all_multi: 1 to enable all-multi mode, 0 to disable it, -1 no change
  868. * @bcast: 1 to enable broadcast Rx, 0 to disable it, -1 no change
  869. * @vlanex: 1 to enable hardware VLAN Tag extraction, 0 to disable it,
  870. * -1 no change
  871. *
  872. * Sets Rx properties of a virtual interface.
  873. */
  874. int t4vf_set_rxmode(struct adapter *adapter, unsigned int viid,
  875. int mtu, int promisc, int all_multi, int bcast, int vlanex,
  876. bool sleep_ok)
  877. {
  878. struct fw_vi_rxmode_cmd cmd;
  879. /* convert to FW values */
  880. if (mtu < 0)
  881. mtu = FW_VI_RXMODE_CMD_MTU_MASK;
  882. if (promisc < 0)
  883. promisc = FW_VI_RXMODE_CMD_PROMISCEN_MASK;
  884. if (all_multi < 0)
  885. all_multi = FW_VI_RXMODE_CMD_ALLMULTIEN_MASK;
  886. if (bcast < 0)
  887. bcast = FW_VI_RXMODE_CMD_BROADCASTEN_MASK;
  888. if (vlanex < 0)
  889. vlanex = FW_VI_RXMODE_CMD_VLANEXEN_MASK;
  890. memset(&cmd, 0, sizeof(cmd));
  891. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_RXMODE_CMD) |
  892. FW_CMD_REQUEST |
  893. FW_CMD_WRITE |
  894. FW_VI_RXMODE_CMD_VIID(viid));
  895. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  896. cmd.mtu_to_vlanexen =
  897. cpu_to_be32(FW_VI_RXMODE_CMD_MTU(mtu) |
  898. FW_VI_RXMODE_CMD_PROMISCEN(promisc) |
  899. FW_VI_RXMODE_CMD_ALLMULTIEN(all_multi) |
  900. FW_VI_RXMODE_CMD_BROADCASTEN(bcast) |
  901. FW_VI_RXMODE_CMD_VLANEXEN(vlanex));
  902. return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
  903. }
  904. /**
  905. * t4vf_alloc_mac_filt - allocates exact-match filters for MAC addresses
  906. * @adapter: the adapter
  907. * @viid: the Virtual Interface Identifier
  908. * @free: if true any existing filters for this VI id are first removed
  909. * @naddr: the number of MAC addresses to allocate filters for (up to 7)
  910. * @addr: the MAC address(es)
  911. * @idx: where to store the index of each allocated filter
  912. * @hash: pointer to hash address filter bitmap
  913. * @sleep_ok: call is allowed to sleep
  914. *
  915. * Allocates an exact-match filter for each of the supplied addresses and
  916. * sets it to the corresponding address. If @idx is not %NULL it should
  917. * have at least @naddr entries, each of which will be set to the index of
  918. * the filter allocated for the corresponding MAC address. If a filter
  919. * could not be allocated for an address its index is set to 0xffff.
  920. * If @hash is not %NULL addresses that fail to allocate an exact filter
  921. * are hashed and update the hash filter bitmap pointed at by @hash.
  922. *
  923. * Returns a negative error number or the number of filters allocated.
  924. */
  925. int t4vf_alloc_mac_filt(struct adapter *adapter, unsigned int viid, bool free,
  926. unsigned int naddr, const u8 **addr, u16 *idx,
  927. u64 *hash, bool sleep_ok)
  928. {
  929. int offset, ret = 0;
  930. unsigned nfilters = 0;
  931. unsigned int rem = naddr;
  932. struct fw_vi_mac_cmd cmd, rpl;
  933. if (naddr > FW_CLS_TCAM_NUM_ENTRIES)
  934. return -EINVAL;
  935. for (offset = 0; offset < naddr; /**/) {
  936. unsigned int fw_naddr = (rem < ARRAY_SIZE(cmd.u.exact)
  937. ? rem
  938. : ARRAY_SIZE(cmd.u.exact));
  939. size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
  940. u.exact[fw_naddr]), 16);
  941. struct fw_vi_mac_exact *p;
  942. int i;
  943. memset(&cmd, 0, sizeof(cmd));
  944. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
  945. FW_CMD_REQUEST |
  946. FW_CMD_WRITE |
  947. (free ? FW_CMD_EXEC : 0) |
  948. FW_VI_MAC_CMD_VIID(viid));
  949. cmd.freemacs_to_len16 =
  950. cpu_to_be32(FW_VI_MAC_CMD_FREEMACS(free) |
  951. FW_CMD_LEN16(len16));
  952. for (i = 0, p = cmd.u.exact; i < fw_naddr; i++, p++) {
  953. p->valid_to_idx = cpu_to_be16(
  954. FW_VI_MAC_CMD_VALID |
  955. FW_VI_MAC_CMD_IDX(FW_VI_MAC_ADD_MAC));
  956. memcpy(p->macaddr, addr[offset+i], sizeof(p->macaddr));
  957. }
  958. ret = t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), &rpl,
  959. sleep_ok);
  960. if (ret && ret != -ENOMEM)
  961. break;
  962. for (i = 0, p = rpl.u.exact; i < fw_naddr; i++, p++) {
  963. u16 index = FW_VI_MAC_CMD_IDX_GET(
  964. be16_to_cpu(p->valid_to_idx));
  965. if (idx)
  966. idx[offset+i] =
  967. (index >= FW_CLS_TCAM_NUM_ENTRIES
  968. ? 0xffff
  969. : index);
  970. if (index < FW_CLS_TCAM_NUM_ENTRIES)
  971. nfilters++;
  972. else if (hash)
  973. *hash |= (1ULL << hash_mac_addr(addr[offset+i]));
  974. }
  975. free = false;
  976. offset += fw_naddr;
  977. rem -= fw_naddr;
  978. }
  979. /*
  980. * If there were no errors or we merely ran out of room in our MAC
  981. * address arena, return the number of filters actually written.
  982. */
  983. if (ret == 0 || ret == -ENOMEM)
  984. ret = nfilters;
  985. return ret;
  986. }
  987. /**
  988. * t4vf_change_mac - modifies the exact-match filter for a MAC address
  989. * @adapter: the adapter
  990. * @viid: the Virtual Interface ID
  991. * @idx: index of existing filter for old value of MAC address, or -1
  992. * @addr: the new MAC address value
  993. * @persist: if idx < 0, the new MAC allocation should be persistent
  994. *
  995. * Modifies an exact-match filter and sets it to the new MAC address.
  996. * Note that in general it is not possible to modify the value of a given
  997. * filter so the generic way to modify an address filter is to free the
  998. * one being used by the old address value and allocate a new filter for
  999. * the new address value. @idx can be -1 if the address is a new
  1000. * addition.
  1001. *
  1002. * Returns a negative error number or the index of the filter with the new
  1003. * MAC value.
  1004. */
  1005. int t4vf_change_mac(struct adapter *adapter, unsigned int viid,
  1006. int idx, const u8 *addr, bool persist)
  1007. {
  1008. int ret;
  1009. struct fw_vi_mac_cmd cmd, rpl;
  1010. struct fw_vi_mac_exact *p = &cmd.u.exact[0];
  1011. size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
  1012. u.exact[1]), 16);
  1013. /*
  1014. * If this is a new allocation, determine whether it should be
  1015. * persistent (across a "freemacs" operation) or not.
  1016. */
  1017. if (idx < 0)
  1018. idx = persist ? FW_VI_MAC_ADD_PERSIST_MAC : FW_VI_MAC_ADD_MAC;
  1019. memset(&cmd, 0, sizeof(cmd));
  1020. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
  1021. FW_CMD_REQUEST |
  1022. FW_CMD_WRITE |
  1023. FW_VI_MAC_CMD_VIID(viid));
  1024. cmd.freemacs_to_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
  1025. p->valid_to_idx = cpu_to_be16(FW_VI_MAC_CMD_VALID |
  1026. FW_VI_MAC_CMD_IDX(idx));
  1027. memcpy(p->macaddr, addr, sizeof(p->macaddr));
  1028. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  1029. if (ret == 0) {
  1030. p = &rpl.u.exact[0];
  1031. ret = FW_VI_MAC_CMD_IDX_GET(be16_to_cpu(p->valid_to_idx));
  1032. if (ret >= FW_CLS_TCAM_NUM_ENTRIES)
  1033. ret = -ENOMEM;
  1034. }
  1035. return ret;
  1036. }
  1037. /**
  1038. * t4vf_set_addr_hash - program the MAC inexact-match hash filter
  1039. * @adapter: the adapter
  1040. * @viid: the Virtual Interface Identifier
  1041. * @ucast: whether the hash filter should also match unicast addresses
  1042. * @vec: the value to be written to the hash filter
  1043. * @sleep_ok: call is allowed to sleep
  1044. *
  1045. * Sets the 64-bit inexact-match hash filter for a virtual interface.
  1046. */
  1047. int t4vf_set_addr_hash(struct adapter *adapter, unsigned int viid,
  1048. bool ucast, u64 vec, bool sleep_ok)
  1049. {
  1050. struct fw_vi_mac_cmd cmd;
  1051. size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
  1052. u.exact[0]), 16);
  1053. memset(&cmd, 0, sizeof(cmd));
  1054. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
  1055. FW_CMD_REQUEST |
  1056. FW_CMD_WRITE |
  1057. FW_VI_ENABLE_CMD_VIID(viid));
  1058. cmd.freemacs_to_len16 = cpu_to_be32(FW_VI_MAC_CMD_HASHVECEN |
  1059. FW_VI_MAC_CMD_HASHUNIEN(ucast) |
  1060. FW_CMD_LEN16(len16));
  1061. cmd.u.hash.hashvec = cpu_to_be64(vec);
  1062. return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
  1063. }
  1064. /**
  1065. * t4vf_get_port_stats - collect "port" statistics
  1066. * @adapter: the adapter
  1067. * @pidx: the port index
  1068. * @s: the stats structure to fill
  1069. *
  1070. * Collect statistics for the "port"'s Virtual Interface.
  1071. */
  1072. int t4vf_get_port_stats(struct adapter *adapter, int pidx,
  1073. struct t4vf_port_stats *s)
  1074. {
  1075. struct port_info *pi = adap2pinfo(adapter, pidx);
  1076. struct fw_vi_stats_vf fwstats;
  1077. unsigned int rem = VI_VF_NUM_STATS;
  1078. __be64 *fwsp = (__be64 *)&fwstats;
  1079. /*
  1080. * Grab the Virtual Interface statistics a chunk at a time via mailbox
  1081. * commands. We could use a Work Request and get all of them at once
  1082. * but that's an asynchronous interface which is awkward to use.
  1083. */
  1084. while (rem) {
  1085. unsigned int ix = VI_VF_NUM_STATS - rem;
  1086. unsigned int nstats = min(6U, rem);
  1087. struct fw_vi_stats_cmd cmd, rpl;
  1088. size_t len = (offsetof(struct fw_vi_stats_cmd, u) +
  1089. sizeof(struct fw_vi_stats_ctl));
  1090. size_t len16 = DIV_ROUND_UP(len, 16);
  1091. int ret;
  1092. memset(&cmd, 0, sizeof(cmd));
  1093. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_STATS_CMD) |
  1094. FW_VI_STATS_CMD_VIID(pi->viid) |
  1095. FW_CMD_REQUEST |
  1096. FW_CMD_READ);
  1097. cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
  1098. cmd.u.ctl.nstats_ix =
  1099. cpu_to_be16(FW_VI_STATS_CMD_IX(ix) |
  1100. FW_VI_STATS_CMD_NSTATS(nstats));
  1101. ret = t4vf_wr_mbox_ns(adapter, &cmd, len, &rpl);
  1102. if (ret)
  1103. return ret;
  1104. memcpy(fwsp, &rpl.u.ctl.stat0, sizeof(__be64) * nstats);
  1105. rem -= nstats;
  1106. fwsp += nstats;
  1107. }
  1108. /*
  1109. * Translate firmware statistics into host native statistics.
  1110. */
  1111. s->tx_bcast_bytes = be64_to_cpu(fwstats.tx_bcast_bytes);
  1112. s->tx_bcast_frames = be64_to_cpu(fwstats.tx_bcast_frames);
  1113. s->tx_mcast_bytes = be64_to_cpu(fwstats.tx_mcast_bytes);
  1114. s->tx_mcast_frames = be64_to_cpu(fwstats.tx_mcast_frames);
  1115. s->tx_ucast_bytes = be64_to_cpu(fwstats.tx_ucast_bytes);
  1116. s->tx_ucast_frames = be64_to_cpu(fwstats.tx_ucast_frames);
  1117. s->tx_drop_frames = be64_to_cpu(fwstats.tx_drop_frames);
  1118. s->tx_offload_bytes = be64_to_cpu(fwstats.tx_offload_bytes);
  1119. s->tx_offload_frames = be64_to_cpu(fwstats.tx_offload_frames);
  1120. s->rx_bcast_bytes = be64_to_cpu(fwstats.rx_bcast_bytes);
  1121. s->rx_bcast_frames = be64_to_cpu(fwstats.rx_bcast_frames);
  1122. s->rx_mcast_bytes = be64_to_cpu(fwstats.rx_mcast_bytes);
  1123. s->rx_mcast_frames = be64_to_cpu(fwstats.rx_mcast_frames);
  1124. s->rx_ucast_bytes = be64_to_cpu(fwstats.rx_ucast_bytes);
  1125. s->rx_ucast_frames = be64_to_cpu(fwstats.rx_ucast_frames);
  1126. s->rx_err_frames = be64_to_cpu(fwstats.rx_err_frames);
  1127. return 0;
  1128. }
  1129. /**
  1130. * t4vf_iq_free - free an ingress queue and its free lists
  1131. * @adapter: the adapter
  1132. * @iqtype: the ingress queue type (FW_IQ_TYPE_FL_INT_CAP, etc.)
  1133. * @iqid: ingress queue ID
  1134. * @fl0id: FL0 queue ID or 0xffff if no attached FL0
  1135. * @fl1id: FL1 queue ID or 0xffff if no attached FL1
  1136. *
  1137. * Frees an ingress queue and its associated free lists, if any.
  1138. */
  1139. int t4vf_iq_free(struct adapter *adapter, unsigned int iqtype,
  1140. unsigned int iqid, unsigned int fl0id, unsigned int fl1id)
  1141. {
  1142. struct fw_iq_cmd cmd;
  1143. memset(&cmd, 0, sizeof(cmd));
  1144. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_IQ_CMD) |
  1145. FW_CMD_REQUEST |
  1146. FW_CMD_EXEC);
  1147. cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_FREE |
  1148. FW_LEN16(cmd));
  1149. cmd.type_to_iqandstindex =
  1150. cpu_to_be32(FW_IQ_CMD_TYPE(iqtype));
  1151. cmd.iqid = cpu_to_be16(iqid);
  1152. cmd.fl0id = cpu_to_be16(fl0id);
  1153. cmd.fl1id = cpu_to_be16(fl1id);
  1154. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  1155. }
  1156. /**
  1157. * t4vf_eth_eq_free - free an Ethernet egress queue
  1158. * @adapter: the adapter
  1159. * @eqid: egress queue ID
  1160. *
  1161. * Frees an Ethernet egress queue.
  1162. */
  1163. int t4vf_eth_eq_free(struct adapter *adapter, unsigned int eqid)
  1164. {
  1165. struct fw_eq_eth_cmd cmd;
  1166. memset(&cmd, 0, sizeof(cmd));
  1167. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_EQ_ETH_CMD) |
  1168. FW_CMD_REQUEST |
  1169. FW_CMD_EXEC);
  1170. cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_FREE |
  1171. FW_LEN16(cmd));
  1172. cmd.eqid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_EQID(eqid));
  1173. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  1174. }
  1175. /**
  1176. * t4vf_handle_fw_rpl - process a firmware reply message
  1177. * @adapter: the adapter
  1178. * @rpl: start of the firmware message
  1179. *
  1180. * Processes a firmware message, such as link state change messages.
  1181. */
  1182. int t4vf_handle_fw_rpl(struct adapter *adapter, const __be64 *rpl)
  1183. {
  1184. const struct fw_cmd_hdr *cmd_hdr = (const struct fw_cmd_hdr *)rpl;
  1185. u8 opcode = FW_CMD_OP_GET(be32_to_cpu(cmd_hdr->hi));
  1186. switch (opcode) {
  1187. case FW_PORT_CMD: {
  1188. /*
  1189. * Link/module state change message.
  1190. */
  1191. const struct fw_port_cmd *port_cmd =
  1192. (const struct fw_port_cmd *)rpl;
  1193. u32 word;
  1194. int action, port_id, link_ok, speed, fc, pidx;
  1195. /*
  1196. * Extract various fields from port status change message.
  1197. */
  1198. action = FW_PORT_CMD_ACTION_GET(
  1199. be32_to_cpu(port_cmd->action_to_len16));
  1200. if (action != FW_PORT_ACTION_GET_PORT_INFO) {
  1201. dev_err(adapter->pdev_dev,
  1202. "Unknown firmware PORT reply action %x\n",
  1203. action);
  1204. break;
  1205. }
  1206. port_id = FW_PORT_CMD_PORTID_GET(
  1207. be32_to_cpu(port_cmd->op_to_portid));
  1208. word = be32_to_cpu(port_cmd->u.info.lstatus_to_modtype);
  1209. link_ok = (word & FW_PORT_CMD_LSTATUS) != 0;
  1210. speed = 0;
  1211. fc = 0;
  1212. if (word & FW_PORT_CMD_RXPAUSE)
  1213. fc |= PAUSE_RX;
  1214. if (word & FW_PORT_CMD_TXPAUSE)
  1215. fc |= PAUSE_TX;
  1216. if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_100M))
  1217. speed = SPEED_100;
  1218. else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_1G))
  1219. speed = SPEED_1000;
  1220. else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_10G))
  1221. speed = SPEED_10000;
  1222. /*
  1223. * Scan all of our "ports" (Virtual Interfaces) looking for
  1224. * those bound to the physical port which has changed. If
  1225. * our recorded state doesn't match the current state,
  1226. * signal that change to the OS code.
  1227. */
  1228. for_each_port(adapter, pidx) {
  1229. struct port_info *pi = adap2pinfo(adapter, pidx);
  1230. struct link_config *lc;
  1231. if (pi->port_id != port_id)
  1232. continue;
  1233. lc = &pi->link_cfg;
  1234. if (link_ok != lc->link_ok || speed != lc->speed ||
  1235. fc != lc->fc) {
  1236. /* something changed */
  1237. lc->link_ok = link_ok;
  1238. lc->speed = speed;
  1239. lc->fc = fc;
  1240. t4vf_os_link_changed(adapter, pidx, link_ok);
  1241. }
  1242. }
  1243. break;
  1244. }
  1245. default:
  1246. dev_err(adapter->pdev_dev, "Unknown firmware reply %X\n",
  1247. opcode);
  1248. }
  1249. return 0;
  1250. }