t4vf_hw.c 41 KB

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