t4vf_hw.c 42 KB

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