t4vf_hw.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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 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 init_link_config(struct link_config *lc, unsigned int caps)
  235. {
  236. lc->supported = caps;
  237. lc->requested_speed = 0;
  238. lc->speed = 0;
  239. lc->requested_fc = lc->fc = PAUSE_RX | PAUSE_TX;
  240. if (lc->supported & SUPPORTED_Autoneg) {
  241. lc->advertising = lc->supported;
  242. lc->autoneg = AUTONEG_ENABLE;
  243. lc->requested_fc |= PAUSE_AUTONEG;
  244. } else {
  245. lc->advertising = 0;
  246. lc->autoneg = AUTONEG_DISABLE;
  247. }
  248. }
  249. /**
  250. * t4vf_port_init - initialize port hardware/software state
  251. * @adapter: the adapter
  252. * @pidx: the adapter port index
  253. */
  254. int t4vf_port_init(struct adapter *adapter, int pidx)
  255. {
  256. struct port_info *pi = adap2pinfo(adapter, pidx);
  257. struct fw_vi_cmd vi_cmd, vi_rpl;
  258. struct fw_port_cmd port_cmd, port_rpl;
  259. int v;
  260. u32 word;
  261. /*
  262. * Execute a VI Read command to get our Virtual Interface information
  263. * like MAC address, etc.
  264. */
  265. memset(&vi_cmd, 0, sizeof(vi_cmd));
  266. vi_cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
  267. FW_CMD_REQUEST |
  268. FW_CMD_READ);
  269. vi_cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(vi_cmd));
  270. vi_cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID(pi->viid));
  271. v = t4vf_wr_mbox(adapter, &vi_cmd, sizeof(vi_cmd), &vi_rpl);
  272. if (v)
  273. return v;
  274. BUG_ON(pi->port_id != FW_VI_CMD_PORTID_GET(vi_rpl.portid_pkd));
  275. pi->rss_size = FW_VI_CMD_RSSSIZE_GET(be16_to_cpu(vi_rpl.rsssize_pkd));
  276. t4_os_set_hw_addr(adapter, pidx, vi_rpl.mac);
  277. /*
  278. * If we don't have read access to our port information, we're done
  279. * now. Otherwise, execute a PORT Read command to get it ...
  280. */
  281. if (!(adapter->params.vfres.r_caps & FW_CMD_CAP_PORT))
  282. return 0;
  283. memset(&port_cmd, 0, sizeof(port_cmd));
  284. port_cmd.op_to_portid = cpu_to_be32(FW_CMD_OP(FW_PORT_CMD) |
  285. FW_CMD_REQUEST |
  286. FW_CMD_READ |
  287. FW_PORT_CMD_PORTID(pi->port_id));
  288. port_cmd.action_to_len16 =
  289. cpu_to_be32(FW_PORT_CMD_ACTION(FW_PORT_ACTION_GET_PORT_INFO) |
  290. FW_LEN16(port_cmd));
  291. v = t4vf_wr_mbox(adapter, &port_cmd, sizeof(port_cmd), &port_rpl);
  292. if (v)
  293. return v;
  294. v = 0;
  295. word = be16_to_cpu(port_rpl.u.info.pcap);
  296. if (word & FW_PORT_CAP_SPEED_100M)
  297. v |= SUPPORTED_100baseT_Full;
  298. if (word & FW_PORT_CAP_SPEED_1G)
  299. v |= SUPPORTED_1000baseT_Full;
  300. if (word & FW_PORT_CAP_SPEED_10G)
  301. v |= SUPPORTED_10000baseT_Full;
  302. if (word & FW_PORT_CAP_SPEED_40G)
  303. v |= SUPPORTED_40000baseSR4_Full;
  304. if (word & FW_PORT_CAP_ANEG)
  305. v |= SUPPORTED_Autoneg;
  306. init_link_config(&pi->link_cfg, v);
  307. return 0;
  308. }
  309. /**
  310. * t4vf_fw_reset - issue a reset to FW
  311. * @adapter: the adapter
  312. *
  313. * Issues a reset command to FW. For a Physical Function this would
  314. * result in the Firmware reseting all of its state. For a Virtual
  315. * Function this just resets the state associated with the VF.
  316. */
  317. int t4vf_fw_reset(struct adapter *adapter)
  318. {
  319. struct fw_reset_cmd cmd;
  320. memset(&cmd, 0, sizeof(cmd));
  321. cmd.op_to_write = cpu_to_be32(FW_CMD_OP(FW_RESET_CMD) |
  322. FW_CMD_WRITE);
  323. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  324. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  325. }
  326. /**
  327. * t4vf_query_params - query FW or device parameters
  328. * @adapter: the adapter
  329. * @nparams: the number of parameters
  330. * @params: the parameter names
  331. * @vals: the parameter values
  332. *
  333. * Reads the values of firmware or device parameters. Up to 7 parameters
  334. * can be queried at once.
  335. */
  336. static int t4vf_query_params(struct adapter *adapter, unsigned int nparams,
  337. const u32 *params, u32 *vals)
  338. {
  339. int i, ret;
  340. struct fw_params_cmd cmd, rpl;
  341. struct fw_params_param *p;
  342. size_t len16;
  343. if (nparams > 7)
  344. return -EINVAL;
  345. memset(&cmd, 0, sizeof(cmd));
  346. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PARAMS_CMD) |
  347. FW_CMD_REQUEST |
  348. FW_CMD_READ);
  349. len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
  350. param[nparams].mnem), 16);
  351. cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
  352. for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++)
  353. p->mnem = htonl(*params++);
  354. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  355. if (ret == 0)
  356. for (i = 0, p = &rpl.param[0]; i < nparams; i++, p++)
  357. *vals++ = be32_to_cpu(p->val);
  358. return ret;
  359. }
  360. /**
  361. * t4vf_set_params - sets FW or device parameters
  362. * @adapter: the adapter
  363. * @nparams: the number of parameters
  364. * @params: the parameter names
  365. * @vals: the parameter values
  366. *
  367. * Sets the values of firmware or device parameters. Up to 7 parameters
  368. * can be specified at once.
  369. */
  370. int t4vf_set_params(struct adapter *adapter, unsigned int nparams,
  371. const u32 *params, const u32 *vals)
  372. {
  373. int i;
  374. struct fw_params_cmd cmd;
  375. struct fw_params_param *p;
  376. size_t len16;
  377. if (nparams > 7)
  378. return -EINVAL;
  379. memset(&cmd, 0, sizeof(cmd));
  380. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PARAMS_CMD) |
  381. FW_CMD_REQUEST |
  382. FW_CMD_WRITE);
  383. len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
  384. param[nparams]), 16);
  385. cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
  386. for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++) {
  387. p->mnem = cpu_to_be32(*params++);
  388. p->val = cpu_to_be32(*vals++);
  389. }
  390. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  391. }
  392. /**
  393. * t4vf_get_sge_params - retrieve adapter Scatter gather Engine parameters
  394. * @adapter: the adapter
  395. *
  396. * Retrieves various core SGE parameters in the form of hardware SGE
  397. * register values. The caller is responsible for decoding these as
  398. * needed. The SGE parameters are stored in @adapter->params.sge.
  399. */
  400. int t4vf_get_sge_params(struct adapter *adapter)
  401. {
  402. struct sge_params *sge_params = &adapter->params.sge;
  403. u32 params[7], vals[7];
  404. int v;
  405. params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  406. FW_PARAMS_PARAM_XYZ(SGE_CONTROL));
  407. params[1] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  408. FW_PARAMS_PARAM_XYZ(SGE_HOST_PAGE_SIZE));
  409. params[2] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  410. FW_PARAMS_PARAM_XYZ(SGE_FL_BUFFER_SIZE0));
  411. params[3] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  412. FW_PARAMS_PARAM_XYZ(SGE_FL_BUFFER_SIZE1));
  413. params[4] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  414. FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_0_AND_1));
  415. params[5] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  416. FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_2_AND_3));
  417. params[6] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  418. FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_4_AND_5));
  419. v = t4vf_query_params(adapter, 7, params, vals);
  420. if (v)
  421. return v;
  422. sge_params->sge_control = vals[0];
  423. sge_params->sge_host_page_size = vals[1];
  424. sge_params->sge_fl_buffer_size[0] = vals[2];
  425. sge_params->sge_fl_buffer_size[1] = vals[3];
  426. sge_params->sge_timer_value_0_and_1 = vals[4];
  427. sge_params->sge_timer_value_2_and_3 = vals[5];
  428. sge_params->sge_timer_value_4_and_5 = vals[6];
  429. /* T4 uses a single control field to specify both the PCIe Padding and
  430. * Packing Boundary. T5 introduced the ability to specify these
  431. * separately with the Padding Boundary in SGE_CONTROL and and Packing
  432. * Boundary in SGE_CONTROL2. So for T5 and later we need to grab
  433. * SGE_CONTROL in order to determine how ingress packet data will be
  434. * laid out in Packed Buffer Mode. Unfortunately, older versions of
  435. * the firmware won't let us retrieve SGE_CONTROL2 so if we get a
  436. * failure grabbing it we throw an error since we can't figure out the
  437. * right value.
  438. */
  439. if (!is_t4(adapter->params.chip)) {
  440. params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  441. FW_PARAMS_PARAM_XYZ(SGE_CONTROL2_A));
  442. v = t4vf_query_params(adapter, 1, params, vals);
  443. if (v != FW_SUCCESS) {
  444. dev_err(adapter->pdev_dev,
  445. "Unable to get SGE Control2; "
  446. "probably old firmware.\n");
  447. return v;
  448. }
  449. sge_params->sge_control2 = vals[0];
  450. }
  451. params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  452. FW_PARAMS_PARAM_XYZ(SGE_INGRESS_RX_THRESHOLD));
  453. params[1] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
  454. FW_PARAMS_PARAM_XYZ(SGE_CONM_CTRL));
  455. v = t4vf_query_params(adapter, 2, params, vals);
  456. if (v)
  457. return v;
  458. sge_params->sge_ingress_rx_threshold = vals[0];
  459. sge_params->sge_congestion_control = vals[1];
  460. return 0;
  461. }
  462. /**
  463. * t4vf_get_vpd_params - retrieve device VPD paremeters
  464. * @adapter: the adapter
  465. *
  466. * Retrives various device Vital Product Data parameters. The parameters
  467. * are stored in @adapter->params.vpd.
  468. */
  469. int t4vf_get_vpd_params(struct adapter *adapter)
  470. {
  471. struct vpd_params *vpd_params = &adapter->params.vpd;
  472. u32 params[7], vals[7];
  473. int v;
  474. params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
  475. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CCLK));
  476. v = t4vf_query_params(adapter, 1, params, vals);
  477. if (v)
  478. return v;
  479. vpd_params->cclk = vals[0];
  480. return 0;
  481. }
  482. /**
  483. * t4vf_get_dev_params - retrieve device paremeters
  484. * @adapter: the adapter
  485. *
  486. * Retrives various device parameters. The parameters are stored in
  487. * @adapter->params.dev.
  488. */
  489. int t4vf_get_dev_params(struct adapter *adapter)
  490. {
  491. struct dev_params *dev_params = &adapter->params.dev;
  492. u32 params[7], vals[7];
  493. int v;
  494. params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
  495. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_FWREV));
  496. params[1] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
  497. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_TPREV));
  498. v = t4vf_query_params(adapter, 2, params, vals);
  499. if (v)
  500. return v;
  501. dev_params->fwrev = vals[0];
  502. dev_params->tprev = vals[1];
  503. return 0;
  504. }
  505. /**
  506. * t4vf_get_rss_glb_config - retrieve adapter RSS Global Configuration
  507. * @adapter: the adapter
  508. *
  509. * Retrieves global RSS mode and parameters with which we have to live
  510. * and stores them in the @adapter's RSS parameters.
  511. */
  512. int t4vf_get_rss_glb_config(struct adapter *adapter)
  513. {
  514. struct rss_params *rss = &adapter->params.rss;
  515. struct fw_rss_glb_config_cmd cmd, rpl;
  516. int v;
  517. /*
  518. * Execute an RSS Global Configuration read command to retrieve
  519. * our RSS configuration.
  520. */
  521. memset(&cmd, 0, sizeof(cmd));
  522. cmd.op_to_write = cpu_to_be32(FW_CMD_OP(FW_RSS_GLB_CONFIG_CMD) |
  523. FW_CMD_REQUEST |
  524. FW_CMD_READ);
  525. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  526. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  527. if (v)
  528. return v;
  529. /*
  530. * Transate the big-endian RSS Global Configuration into our
  531. * cpu-endian format based on the RSS mode. We also do first level
  532. * filtering at this point to weed out modes which don't support
  533. * VF Drivers ...
  534. */
  535. rss->mode = FW_RSS_GLB_CONFIG_CMD_MODE_GET(
  536. be32_to_cpu(rpl.u.manual.mode_pkd));
  537. switch (rss->mode) {
  538. case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
  539. u32 word = be32_to_cpu(
  540. rpl.u.basicvirtual.synmapen_to_hashtoeplitz);
  541. rss->u.basicvirtual.synmapen =
  542. ((word & FW_RSS_GLB_CONFIG_CMD_SYNMAPEN) != 0);
  543. rss->u.basicvirtual.syn4tupenipv6 =
  544. ((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV6) != 0);
  545. rss->u.basicvirtual.syn2tupenipv6 =
  546. ((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV6) != 0);
  547. rss->u.basicvirtual.syn4tupenipv4 =
  548. ((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV4) != 0);
  549. rss->u.basicvirtual.syn2tupenipv4 =
  550. ((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV4) != 0);
  551. rss->u.basicvirtual.ofdmapen =
  552. ((word & FW_RSS_GLB_CONFIG_CMD_OFDMAPEN) != 0);
  553. rss->u.basicvirtual.tnlmapen =
  554. ((word & FW_RSS_GLB_CONFIG_CMD_TNLMAPEN) != 0);
  555. rss->u.basicvirtual.tnlalllookup =
  556. ((word & FW_RSS_GLB_CONFIG_CMD_TNLALLLKP) != 0);
  557. rss->u.basicvirtual.hashtoeplitz =
  558. ((word & FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ) != 0);
  559. /* we need at least Tunnel Map Enable to be set */
  560. if (!rss->u.basicvirtual.tnlmapen)
  561. return -EINVAL;
  562. break;
  563. }
  564. default:
  565. /* all unknown/unsupported RSS modes result in an error */
  566. return -EINVAL;
  567. }
  568. return 0;
  569. }
  570. /**
  571. * t4vf_get_vfres - retrieve VF resource limits
  572. * @adapter: the adapter
  573. *
  574. * Retrieves configured resource limits and capabilities for a virtual
  575. * function. The results are stored in @adapter->vfres.
  576. */
  577. int t4vf_get_vfres(struct adapter *adapter)
  578. {
  579. struct vf_resources *vfres = &adapter->params.vfres;
  580. struct fw_pfvf_cmd cmd, rpl;
  581. int v;
  582. u32 word;
  583. /*
  584. * Execute PFVF Read command to get VF resource limits; bail out early
  585. * with error on command failure.
  586. */
  587. memset(&cmd, 0, sizeof(cmd));
  588. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PFVF_CMD) |
  589. FW_CMD_REQUEST |
  590. FW_CMD_READ);
  591. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  592. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  593. if (v)
  594. return v;
  595. /*
  596. * Extract VF resource limits and return success.
  597. */
  598. word = be32_to_cpu(rpl.niqflint_niq);
  599. vfres->niqflint = FW_PFVF_CMD_NIQFLINT_GET(word);
  600. vfres->niq = FW_PFVF_CMD_NIQ_GET(word);
  601. word = be32_to_cpu(rpl.type_to_neq);
  602. vfres->neq = FW_PFVF_CMD_NEQ_GET(word);
  603. vfres->pmask = FW_PFVF_CMD_PMASK_GET(word);
  604. word = be32_to_cpu(rpl.tc_to_nexactf);
  605. vfres->tc = FW_PFVF_CMD_TC_GET(word);
  606. vfres->nvi = FW_PFVF_CMD_NVI_GET(word);
  607. vfres->nexactf = FW_PFVF_CMD_NEXACTF_GET(word);
  608. word = be32_to_cpu(rpl.r_caps_to_nethctrl);
  609. vfres->r_caps = FW_PFVF_CMD_R_CAPS_GET(word);
  610. vfres->wx_caps = FW_PFVF_CMD_WX_CAPS_GET(word);
  611. vfres->nethctrl = FW_PFVF_CMD_NETHCTRL_GET(word);
  612. return 0;
  613. }
  614. /**
  615. * t4vf_read_rss_vi_config - read a VI's RSS configuration
  616. * @adapter: the adapter
  617. * @viid: Virtual Interface ID
  618. * @config: pointer to host-native VI RSS Configuration buffer
  619. *
  620. * Reads the Virtual Interface's RSS configuration information and
  621. * translates it into CPU-native format.
  622. */
  623. int t4vf_read_rss_vi_config(struct adapter *adapter, unsigned int viid,
  624. union rss_vi_config *config)
  625. {
  626. struct fw_rss_vi_config_cmd cmd, rpl;
  627. int v;
  628. memset(&cmd, 0, sizeof(cmd));
  629. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_VI_CONFIG_CMD) |
  630. FW_CMD_REQUEST |
  631. FW_CMD_READ |
  632. FW_RSS_VI_CONFIG_CMD_VIID(viid));
  633. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  634. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  635. if (v)
  636. return v;
  637. switch (adapter->params.rss.mode) {
  638. case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
  639. u32 word = be32_to_cpu(rpl.u.basicvirtual.defaultq_to_udpen);
  640. config->basicvirtual.ip6fourtupen =
  641. ((word & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN) != 0);
  642. config->basicvirtual.ip6twotupen =
  643. ((word & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN) != 0);
  644. config->basicvirtual.ip4fourtupen =
  645. ((word & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN) != 0);
  646. config->basicvirtual.ip4twotupen =
  647. ((word & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN) != 0);
  648. config->basicvirtual.udpen =
  649. ((word & FW_RSS_VI_CONFIG_CMD_UDPEN) != 0);
  650. config->basicvirtual.defaultq =
  651. FW_RSS_VI_CONFIG_CMD_DEFAULTQ_GET(word);
  652. break;
  653. }
  654. default:
  655. return -EINVAL;
  656. }
  657. return 0;
  658. }
  659. /**
  660. * t4vf_write_rss_vi_config - write a VI's RSS configuration
  661. * @adapter: the adapter
  662. * @viid: Virtual Interface ID
  663. * @config: pointer to host-native VI RSS Configuration buffer
  664. *
  665. * Write the Virtual Interface's RSS configuration information
  666. * (translating it into firmware-native format before writing).
  667. */
  668. int t4vf_write_rss_vi_config(struct adapter *adapter, unsigned int viid,
  669. union rss_vi_config *config)
  670. {
  671. struct fw_rss_vi_config_cmd cmd, rpl;
  672. memset(&cmd, 0, sizeof(cmd));
  673. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_VI_CONFIG_CMD) |
  674. FW_CMD_REQUEST |
  675. FW_CMD_WRITE |
  676. FW_RSS_VI_CONFIG_CMD_VIID(viid));
  677. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  678. switch (adapter->params.rss.mode) {
  679. case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
  680. u32 word = 0;
  681. if (config->basicvirtual.ip6fourtupen)
  682. word |= FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
  683. if (config->basicvirtual.ip6twotupen)
  684. word |= FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
  685. if (config->basicvirtual.ip4fourtupen)
  686. word |= FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
  687. if (config->basicvirtual.ip4twotupen)
  688. word |= FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
  689. if (config->basicvirtual.udpen)
  690. word |= FW_RSS_VI_CONFIG_CMD_UDPEN;
  691. word |= FW_RSS_VI_CONFIG_CMD_DEFAULTQ(
  692. config->basicvirtual.defaultq);
  693. cmd.u.basicvirtual.defaultq_to_udpen = cpu_to_be32(word);
  694. break;
  695. }
  696. default:
  697. return -EINVAL;
  698. }
  699. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  700. }
  701. /**
  702. * t4vf_config_rss_range - configure a portion of the RSS mapping table
  703. * @adapter: the adapter
  704. * @viid: Virtual Interface of RSS Table Slice
  705. * @start: starting entry in the table to write
  706. * @n: how many table entries to write
  707. * @rspq: values for the "Response Queue" (Ingress Queue) lookup table
  708. * @nrspq: number of values in @rspq
  709. *
  710. * Programs the selected part of the VI's RSS mapping table with the
  711. * provided values. If @nrspq < @n the supplied values are used repeatedly
  712. * until the full table range is populated.
  713. *
  714. * The caller must ensure the values in @rspq are in the range 0..1023.
  715. */
  716. int t4vf_config_rss_range(struct adapter *adapter, unsigned int viid,
  717. int start, int n, const u16 *rspq, int nrspq)
  718. {
  719. const u16 *rsp = rspq;
  720. const u16 *rsp_end = rspq+nrspq;
  721. struct fw_rss_ind_tbl_cmd cmd;
  722. /*
  723. * Initialize firmware command template to write the RSS table.
  724. */
  725. memset(&cmd, 0, sizeof(cmd));
  726. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_IND_TBL_CMD) |
  727. FW_CMD_REQUEST |
  728. FW_CMD_WRITE |
  729. FW_RSS_IND_TBL_CMD_VIID(viid));
  730. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  731. /*
  732. * Each firmware RSS command can accommodate up to 32 RSS Ingress
  733. * Queue Identifiers. These Ingress Queue IDs are packed three to
  734. * a 32-bit word as 10-bit values with the upper remaining 2 bits
  735. * reserved.
  736. */
  737. while (n > 0) {
  738. __be32 *qp = &cmd.iq0_to_iq2;
  739. int nq = min(n, 32);
  740. int ret;
  741. /*
  742. * Set up the firmware RSS command header to send the next
  743. * "nq" Ingress Queue IDs to the firmware.
  744. */
  745. cmd.niqid = cpu_to_be16(nq);
  746. cmd.startidx = cpu_to_be16(start);
  747. /*
  748. * "nq" more done for the start of the next loop.
  749. */
  750. start += nq;
  751. n -= nq;
  752. /*
  753. * While there are still Ingress Queue IDs to stuff into the
  754. * current firmware RSS command, retrieve them from the
  755. * Ingress Queue ID array and insert them into the command.
  756. */
  757. while (nq > 0) {
  758. /*
  759. * Grab up to the next 3 Ingress Queue IDs (wrapping
  760. * around the Ingress Queue ID array if necessary) and
  761. * insert them into the firmware RSS command at the
  762. * current 3-tuple position within the commad.
  763. */
  764. u16 qbuf[3];
  765. u16 *qbp = qbuf;
  766. int nqbuf = min(3, nq);
  767. nq -= nqbuf;
  768. qbuf[0] = qbuf[1] = qbuf[2] = 0;
  769. while (nqbuf) {
  770. nqbuf--;
  771. *qbp++ = *rsp++;
  772. if (rsp >= rsp_end)
  773. rsp = rspq;
  774. }
  775. *qp++ = cpu_to_be32(FW_RSS_IND_TBL_CMD_IQ0(qbuf[0]) |
  776. FW_RSS_IND_TBL_CMD_IQ1(qbuf[1]) |
  777. FW_RSS_IND_TBL_CMD_IQ2(qbuf[2]));
  778. }
  779. /*
  780. * Send this portion of the RRS table update to the firmware;
  781. * bail out on any errors.
  782. */
  783. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  784. if (ret)
  785. return ret;
  786. }
  787. return 0;
  788. }
  789. /**
  790. * t4vf_alloc_vi - allocate a virtual interface on a port
  791. * @adapter: the adapter
  792. * @port_id: physical port associated with the VI
  793. *
  794. * Allocate a new Virtual Interface and bind it to the indicated
  795. * physical port. Return the new Virtual Interface Identifier on
  796. * success, or a [negative] error number on failure.
  797. */
  798. int t4vf_alloc_vi(struct adapter *adapter, int port_id)
  799. {
  800. struct fw_vi_cmd cmd, rpl;
  801. int v;
  802. /*
  803. * Execute a VI command to allocate Virtual Interface and return its
  804. * VIID.
  805. */
  806. memset(&cmd, 0, sizeof(cmd));
  807. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
  808. FW_CMD_REQUEST |
  809. FW_CMD_WRITE |
  810. FW_CMD_EXEC);
  811. cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
  812. FW_VI_CMD_ALLOC);
  813. cmd.portid_pkd = FW_VI_CMD_PORTID(port_id);
  814. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  815. if (v)
  816. return v;
  817. return FW_VI_CMD_VIID_GET(be16_to_cpu(rpl.type_viid));
  818. }
  819. /**
  820. * t4vf_free_vi -- free a virtual interface
  821. * @adapter: the adapter
  822. * @viid: the virtual interface identifier
  823. *
  824. * Free a previously allocated Virtual Interface. Return an error on
  825. * failure.
  826. */
  827. int t4vf_free_vi(struct adapter *adapter, int viid)
  828. {
  829. struct fw_vi_cmd cmd;
  830. /*
  831. * Execute a VI command to free the Virtual Interface.
  832. */
  833. memset(&cmd, 0, sizeof(cmd));
  834. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
  835. FW_CMD_REQUEST |
  836. FW_CMD_EXEC);
  837. cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
  838. FW_VI_CMD_FREE);
  839. cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID(viid));
  840. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  841. }
  842. /**
  843. * t4vf_enable_vi - enable/disable a virtual interface
  844. * @adapter: the adapter
  845. * @viid: the Virtual Interface ID
  846. * @rx_en: 1=enable Rx, 0=disable Rx
  847. * @tx_en: 1=enable Tx, 0=disable Tx
  848. *
  849. * Enables/disables a virtual interface.
  850. */
  851. int t4vf_enable_vi(struct adapter *adapter, unsigned int viid,
  852. bool rx_en, bool tx_en)
  853. {
  854. struct fw_vi_enable_cmd cmd;
  855. memset(&cmd, 0, sizeof(cmd));
  856. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_ENABLE_CMD) |
  857. FW_CMD_REQUEST |
  858. FW_CMD_EXEC |
  859. FW_VI_ENABLE_CMD_VIID(viid));
  860. cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_IEN(rx_en) |
  861. FW_VI_ENABLE_CMD_EEN(tx_en) |
  862. FW_LEN16(cmd));
  863. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  864. }
  865. /**
  866. * t4vf_identify_port - identify a VI's port by blinking its LED
  867. * @adapter: the adapter
  868. * @viid: the Virtual Interface ID
  869. * @nblinks: how many times to blink LED at 2.5 Hz
  870. *
  871. * Identifies a VI's port by blinking its LED.
  872. */
  873. int t4vf_identify_port(struct adapter *adapter, unsigned int viid,
  874. unsigned int nblinks)
  875. {
  876. struct fw_vi_enable_cmd cmd;
  877. memset(&cmd, 0, sizeof(cmd));
  878. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_ENABLE_CMD) |
  879. FW_CMD_REQUEST |
  880. FW_CMD_EXEC |
  881. FW_VI_ENABLE_CMD_VIID(viid));
  882. cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_LED |
  883. FW_LEN16(cmd));
  884. cmd.blinkdur = cpu_to_be16(nblinks);
  885. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  886. }
  887. /**
  888. * t4vf_set_rxmode - set Rx properties of a virtual interface
  889. * @adapter: the adapter
  890. * @viid: the VI id
  891. * @mtu: the new MTU or -1 for no change
  892. * @promisc: 1 to enable promiscuous mode, 0 to disable it, -1 no change
  893. * @all_multi: 1 to enable all-multi mode, 0 to disable it, -1 no change
  894. * @bcast: 1 to enable broadcast Rx, 0 to disable it, -1 no change
  895. * @vlanex: 1 to enable hardware VLAN Tag extraction, 0 to disable it,
  896. * -1 no change
  897. *
  898. * Sets Rx properties of a virtual interface.
  899. */
  900. int t4vf_set_rxmode(struct adapter *adapter, unsigned int viid,
  901. int mtu, int promisc, int all_multi, int bcast, int vlanex,
  902. bool sleep_ok)
  903. {
  904. struct fw_vi_rxmode_cmd cmd;
  905. /* convert to FW values */
  906. if (mtu < 0)
  907. mtu = FW_VI_RXMODE_CMD_MTU_MASK;
  908. if (promisc < 0)
  909. promisc = FW_VI_RXMODE_CMD_PROMISCEN_MASK;
  910. if (all_multi < 0)
  911. all_multi = FW_VI_RXMODE_CMD_ALLMULTIEN_MASK;
  912. if (bcast < 0)
  913. bcast = FW_VI_RXMODE_CMD_BROADCASTEN_MASK;
  914. if (vlanex < 0)
  915. vlanex = FW_VI_RXMODE_CMD_VLANEXEN_MASK;
  916. memset(&cmd, 0, sizeof(cmd));
  917. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_RXMODE_CMD) |
  918. FW_CMD_REQUEST |
  919. FW_CMD_WRITE |
  920. FW_VI_RXMODE_CMD_VIID(viid));
  921. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  922. cmd.mtu_to_vlanexen =
  923. cpu_to_be32(FW_VI_RXMODE_CMD_MTU(mtu) |
  924. FW_VI_RXMODE_CMD_PROMISCEN(promisc) |
  925. FW_VI_RXMODE_CMD_ALLMULTIEN(all_multi) |
  926. FW_VI_RXMODE_CMD_BROADCASTEN(bcast) |
  927. FW_VI_RXMODE_CMD_VLANEXEN(vlanex));
  928. return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
  929. }
  930. /**
  931. * t4vf_alloc_mac_filt - allocates exact-match filters for MAC addresses
  932. * @adapter: the adapter
  933. * @viid: the Virtual Interface Identifier
  934. * @free: if true any existing filters for this VI id are first removed
  935. * @naddr: the number of MAC addresses to allocate filters for (up to 7)
  936. * @addr: the MAC address(es)
  937. * @idx: where to store the index of each allocated filter
  938. * @hash: pointer to hash address filter bitmap
  939. * @sleep_ok: call is allowed to sleep
  940. *
  941. * Allocates an exact-match filter for each of the supplied addresses and
  942. * sets it to the corresponding address. If @idx is not %NULL it should
  943. * have at least @naddr entries, each of which will be set to the index of
  944. * the filter allocated for the corresponding MAC address. If a filter
  945. * could not be allocated for an address its index is set to 0xffff.
  946. * If @hash is not %NULL addresses that fail to allocate an exact filter
  947. * are hashed and update the hash filter bitmap pointed at by @hash.
  948. *
  949. * Returns a negative error number or the number of filters allocated.
  950. */
  951. int t4vf_alloc_mac_filt(struct adapter *adapter, unsigned int viid, bool free,
  952. unsigned int naddr, const u8 **addr, u16 *idx,
  953. u64 *hash, bool sleep_ok)
  954. {
  955. int offset, ret = 0;
  956. unsigned nfilters = 0;
  957. unsigned int rem = naddr;
  958. struct fw_vi_mac_cmd cmd, rpl;
  959. unsigned int max_naddr = is_t4(adapter->params.chip) ?
  960. NUM_MPS_CLS_SRAM_L_INSTANCES :
  961. NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
  962. if (naddr > max_naddr)
  963. return -EINVAL;
  964. for (offset = 0; offset < naddr; /**/) {
  965. unsigned int fw_naddr = (rem < ARRAY_SIZE(cmd.u.exact)
  966. ? rem
  967. : ARRAY_SIZE(cmd.u.exact));
  968. size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
  969. u.exact[fw_naddr]), 16);
  970. struct fw_vi_mac_exact *p;
  971. int i;
  972. memset(&cmd, 0, sizeof(cmd));
  973. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
  974. FW_CMD_REQUEST |
  975. FW_CMD_WRITE |
  976. (free ? FW_CMD_EXEC : 0) |
  977. FW_VI_MAC_CMD_VIID(viid));
  978. cmd.freemacs_to_len16 =
  979. cpu_to_be32(FW_VI_MAC_CMD_FREEMACS(free) |
  980. FW_CMD_LEN16(len16));
  981. for (i = 0, p = cmd.u.exact; i < fw_naddr; i++, p++) {
  982. p->valid_to_idx = cpu_to_be16(
  983. FW_VI_MAC_CMD_VALID |
  984. FW_VI_MAC_CMD_IDX(FW_VI_MAC_ADD_MAC));
  985. memcpy(p->macaddr, addr[offset+i], sizeof(p->macaddr));
  986. }
  987. ret = t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), &rpl,
  988. sleep_ok);
  989. if (ret && ret != -ENOMEM)
  990. break;
  991. for (i = 0, p = rpl.u.exact; i < fw_naddr; i++, p++) {
  992. u16 index = FW_VI_MAC_CMD_IDX_GET(
  993. be16_to_cpu(p->valid_to_idx));
  994. if (idx)
  995. idx[offset+i] =
  996. (index >= max_naddr
  997. ? 0xffff
  998. : index);
  999. if (index < max_naddr)
  1000. nfilters++;
  1001. else if (hash)
  1002. *hash |= (1ULL << hash_mac_addr(addr[offset+i]));
  1003. }
  1004. free = false;
  1005. offset += fw_naddr;
  1006. rem -= fw_naddr;
  1007. }
  1008. /*
  1009. * If there were no errors or we merely ran out of room in our MAC
  1010. * address arena, return the number of filters actually written.
  1011. */
  1012. if (ret == 0 || ret == -ENOMEM)
  1013. ret = nfilters;
  1014. return ret;
  1015. }
  1016. /**
  1017. * t4vf_change_mac - modifies the exact-match filter for a MAC address
  1018. * @adapter: the adapter
  1019. * @viid: the Virtual Interface ID
  1020. * @idx: index of existing filter for old value of MAC address, or -1
  1021. * @addr: the new MAC address value
  1022. * @persist: if idx < 0, the new MAC allocation should be persistent
  1023. *
  1024. * Modifies an exact-match filter and sets it to the new MAC address.
  1025. * Note that in general it is not possible to modify the value of a given
  1026. * filter so the generic way to modify an address filter is to free the
  1027. * one being used by the old address value and allocate a new filter for
  1028. * the new address value. @idx can be -1 if the address is a new
  1029. * addition.
  1030. *
  1031. * Returns a negative error number or the index of the filter with the new
  1032. * MAC value.
  1033. */
  1034. int t4vf_change_mac(struct adapter *adapter, unsigned int viid,
  1035. int idx, const u8 *addr, bool persist)
  1036. {
  1037. int ret;
  1038. struct fw_vi_mac_cmd cmd, rpl;
  1039. struct fw_vi_mac_exact *p = &cmd.u.exact[0];
  1040. size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
  1041. u.exact[1]), 16);
  1042. unsigned int max_naddr = is_t4(adapter->params.chip) ?
  1043. NUM_MPS_CLS_SRAM_L_INSTANCES :
  1044. NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
  1045. /*
  1046. * If this is a new allocation, determine whether it should be
  1047. * persistent (across a "freemacs" operation) or not.
  1048. */
  1049. if (idx < 0)
  1050. idx = persist ? FW_VI_MAC_ADD_PERSIST_MAC : FW_VI_MAC_ADD_MAC;
  1051. memset(&cmd, 0, sizeof(cmd));
  1052. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
  1053. FW_CMD_REQUEST |
  1054. FW_CMD_WRITE |
  1055. FW_VI_MAC_CMD_VIID(viid));
  1056. cmd.freemacs_to_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
  1057. p->valid_to_idx = cpu_to_be16(FW_VI_MAC_CMD_VALID |
  1058. FW_VI_MAC_CMD_IDX(idx));
  1059. memcpy(p->macaddr, addr, sizeof(p->macaddr));
  1060. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  1061. if (ret == 0) {
  1062. p = &rpl.u.exact[0];
  1063. ret = FW_VI_MAC_CMD_IDX_GET(be16_to_cpu(p->valid_to_idx));
  1064. if (ret >= max_naddr)
  1065. ret = -ENOMEM;
  1066. }
  1067. return ret;
  1068. }
  1069. /**
  1070. * t4vf_set_addr_hash - program the MAC inexact-match hash filter
  1071. * @adapter: the adapter
  1072. * @viid: the Virtual Interface Identifier
  1073. * @ucast: whether the hash filter should also match unicast addresses
  1074. * @vec: the value to be written to the hash filter
  1075. * @sleep_ok: call is allowed to sleep
  1076. *
  1077. * Sets the 64-bit inexact-match hash filter for a virtual interface.
  1078. */
  1079. int t4vf_set_addr_hash(struct adapter *adapter, unsigned int viid,
  1080. bool ucast, u64 vec, bool sleep_ok)
  1081. {
  1082. struct fw_vi_mac_cmd cmd;
  1083. size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
  1084. u.exact[0]), 16);
  1085. memset(&cmd, 0, sizeof(cmd));
  1086. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
  1087. FW_CMD_REQUEST |
  1088. FW_CMD_WRITE |
  1089. FW_VI_ENABLE_CMD_VIID(viid));
  1090. cmd.freemacs_to_len16 = cpu_to_be32(FW_VI_MAC_CMD_HASHVECEN |
  1091. FW_VI_MAC_CMD_HASHUNIEN(ucast) |
  1092. FW_CMD_LEN16(len16));
  1093. cmd.u.hash.hashvec = cpu_to_be64(vec);
  1094. return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
  1095. }
  1096. /**
  1097. * t4vf_get_port_stats - collect "port" statistics
  1098. * @adapter: the adapter
  1099. * @pidx: the port index
  1100. * @s: the stats structure to fill
  1101. *
  1102. * Collect statistics for the "port"'s Virtual Interface.
  1103. */
  1104. int t4vf_get_port_stats(struct adapter *adapter, int pidx,
  1105. struct t4vf_port_stats *s)
  1106. {
  1107. struct port_info *pi = adap2pinfo(adapter, pidx);
  1108. struct fw_vi_stats_vf fwstats;
  1109. unsigned int rem = VI_VF_NUM_STATS;
  1110. __be64 *fwsp = (__be64 *)&fwstats;
  1111. /*
  1112. * Grab the Virtual Interface statistics a chunk at a time via mailbox
  1113. * commands. We could use a Work Request and get all of them at once
  1114. * but that's an asynchronous interface which is awkward to use.
  1115. */
  1116. while (rem) {
  1117. unsigned int ix = VI_VF_NUM_STATS - rem;
  1118. unsigned int nstats = min(6U, rem);
  1119. struct fw_vi_stats_cmd cmd, rpl;
  1120. size_t len = (offsetof(struct fw_vi_stats_cmd, u) +
  1121. sizeof(struct fw_vi_stats_ctl));
  1122. size_t len16 = DIV_ROUND_UP(len, 16);
  1123. int ret;
  1124. memset(&cmd, 0, sizeof(cmd));
  1125. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_STATS_CMD) |
  1126. FW_VI_STATS_CMD_VIID(pi->viid) |
  1127. FW_CMD_REQUEST |
  1128. FW_CMD_READ);
  1129. cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
  1130. cmd.u.ctl.nstats_ix =
  1131. cpu_to_be16(FW_VI_STATS_CMD_IX(ix) |
  1132. FW_VI_STATS_CMD_NSTATS(nstats));
  1133. ret = t4vf_wr_mbox_ns(adapter, &cmd, len, &rpl);
  1134. if (ret)
  1135. return ret;
  1136. memcpy(fwsp, &rpl.u.ctl.stat0, sizeof(__be64) * nstats);
  1137. rem -= nstats;
  1138. fwsp += nstats;
  1139. }
  1140. /*
  1141. * Translate firmware statistics into host native statistics.
  1142. */
  1143. s->tx_bcast_bytes = be64_to_cpu(fwstats.tx_bcast_bytes);
  1144. s->tx_bcast_frames = be64_to_cpu(fwstats.tx_bcast_frames);
  1145. s->tx_mcast_bytes = be64_to_cpu(fwstats.tx_mcast_bytes);
  1146. s->tx_mcast_frames = be64_to_cpu(fwstats.tx_mcast_frames);
  1147. s->tx_ucast_bytes = be64_to_cpu(fwstats.tx_ucast_bytes);
  1148. s->tx_ucast_frames = be64_to_cpu(fwstats.tx_ucast_frames);
  1149. s->tx_drop_frames = be64_to_cpu(fwstats.tx_drop_frames);
  1150. s->tx_offload_bytes = be64_to_cpu(fwstats.tx_offload_bytes);
  1151. s->tx_offload_frames = be64_to_cpu(fwstats.tx_offload_frames);
  1152. s->rx_bcast_bytes = be64_to_cpu(fwstats.rx_bcast_bytes);
  1153. s->rx_bcast_frames = be64_to_cpu(fwstats.rx_bcast_frames);
  1154. s->rx_mcast_bytes = be64_to_cpu(fwstats.rx_mcast_bytes);
  1155. s->rx_mcast_frames = be64_to_cpu(fwstats.rx_mcast_frames);
  1156. s->rx_ucast_bytes = be64_to_cpu(fwstats.rx_ucast_bytes);
  1157. s->rx_ucast_frames = be64_to_cpu(fwstats.rx_ucast_frames);
  1158. s->rx_err_frames = be64_to_cpu(fwstats.rx_err_frames);
  1159. return 0;
  1160. }
  1161. /**
  1162. * t4vf_iq_free - free an ingress queue and its free lists
  1163. * @adapter: the adapter
  1164. * @iqtype: the ingress queue type (FW_IQ_TYPE_FL_INT_CAP, etc.)
  1165. * @iqid: ingress queue ID
  1166. * @fl0id: FL0 queue ID or 0xffff if no attached FL0
  1167. * @fl1id: FL1 queue ID or 0xffff if no attached FL1
  1168. *
  1169. * Frees an ingress queue and its associated free lists, if any.
  1170. */
  1171. int t4vf_iq_free(struct adapter *adapter, unsigned int iqtype,
  1172. unsigned int iqid, unsigned int fl0id, unsigned int fl1id)
  1173. {
  1174. struct fw_iq_cmd cmd;
  1175. memset(&cmd, 0, sizeof(cmd));
  1176. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_IQ_CMD) |
  1177. FW_CMD_REQUEST |
  1178. FW_CMD_EXEC);
  1179. cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_FREE |
  1180. FW_LEN16(cmd));
  1181. cmd.type_to_iqandstindex =
  1182. cpu_to_be32(FW_IQ_CMD_TYPE(iqtype));
  1183. cmd.iqid = cpu_to_be16(iqid);
  1184. cmd.fl0id = cpu_to_be16(fl0id);
  1185. cmd.fl1id = cpu_to_be16(fl1id);
  1186. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  1187. }
  1188. /**
  1189. * t4vf_eth_eq_free - free an Ethernet egress queue
  1190. * @adapter: the adapter
  1191. * @eqid: egress queue ID
  1192. *
  1193. * Frees an Ethernet egress queue.
  1194. */
  1195. int t4vf_eth_eq_free(struct adapter *adapter, unsigned int eqid)
  1196. {
  1197. struct fw_eq_eth_cmd cmd;
  1198. memset(&cmd, 0, sizeof(cmd));
  1199. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_EQ_ETH_CMD) |
  1200. FW_CMD_REQUEST |
  1201. FW_CMD_EXEC);
  1202. cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_FREE |
  1203. FW_LEN16(cmd));
  1204. cmd.eqid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_EQID(eqid));
  1205. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  1206. }
  1207. /**
  1208. * t4vf_handle_fw_rpl - process a firmware reply message
  1209. * @adapter: the adapter
  1210. * @rpl: start of the firmware message
  1211. *
  1212. * Processes a firmware message, such as link state change messages.
  1213. */
  1214. int t4vf_handle_fw_rpl(struct adapter *adapter, const __be64 *rpl)
  1215. {
  1216. const struct fw_cmd_hdr *cmd_hdr = (const struct fw_cmd_hdr *)rpl;
  1217. u8 opcode = FW_CMD_OP_GET(be32_to_cpu(cmd_hdr->hi));
  1218. switch (opcode) {
  1219. case FW_PORT_CMD: {
  1220. /*
  1221. * Link/module state change message.
  1222. */
  1223. const struct fw_port_cmd *port_cmd =
  1224. (const struct fw_port_cmd *)rpl;
  1225. u32 word;
  1226. int action, port_id, link_ok, speed, fc, pidx;
  1227. /*
  1228. * Extract various fields from port status change message.
  1229. */
  1230. action = FW_PORT_CMD_ACTION_GET(
  1231. be32_to_cpu(port_cmd->action_to_len16));
  1232. if (action != FW_PORT_ACTION_GET_PORT_INFO) {
  1233. dev_err(adapter->pdev_dev,
  1234. "Unknown firmware PORT reply action %x\n",
  1235. action);
  1236. break;
  1237. }
  1238. port_id = FW_PORT_CMD_PORTID_GET(
  1239. be32_to_cpu(port_cmd->op_to_portid));
  1240. word = be32_to_cpu(port_cmd->u.info.lstatus_to_modtype);
  1241. link_ok = (word & FW_PORT_CMD_LSTATUS) != 0;
  1242. speed = 0;
  1243. fc = 0;
  1244. if (word & FW_PORT_CMD_RXPAUSE)
  1245. fc |= PAUSE_RX;
  1246. if (word & FW_PORT_CMD_TXPAUSE)
  1247. fc |= PAUSE_TX;
  1248. if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_100M))
  1249. speed = 100;
  1250. else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_1G))
  1251. speed = 1000;
  1252. else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_10G))
  1253. speed = 10000;
  1254. else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_40G))
  1255. speed = 40000;
  1256. /*
  1257. * Scan all of our "ports" (Virtual Interfaces) looking for
  1258. * those bound to the physical port which has changed. If
  1259. * our recorded state doesn't match the current state,
  1260. * signal that change to the OS code.
  1261. */
  1262. for_each_port(adapter, pidx) {
  1263. struct port_info *pi = adap2pinfo(adapter, pidx);
  1264. struct link_config *lc;
  1265. if (pi->port_id != port_id)
  1266. continue;
  1267. lc = &pi->link_cfg;
  1268. if (link_ok != lc->link_ok || speed != lc->speed ||
  1269. fc != lc->fc) {
  1270. /* something changed */
  1271. lc->link_ok = link_ok;
  1272. lc->speed = speed;
  1273. lc->fc = fc;
  1274. t4vf_os_link_changed(adapter, pidx, link_ok);
  1275. }
  1276. }
  1277. break;
  1278. }
  1279. default:
  1280. dev_err(adapter->pdev_dev, "Unknown firmware reply %X\n",
  1281. opcode);
  1282. }
  1283. return 0;
  1284. }