t4vf_hw.c 40 KB

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