t4vf_hw.c 42 KB

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