siena_sriov.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2010-2011 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. #include <linux/pci.h>
  10. #include <linux/module.h>
  11. #include "net_driver.h"
  12. #include "efx.h"
  13. #include "nic.h"
  14. #include "io.h"
  15. #include "mcdi.h"
  16. #include "filter.h"
  17. #include "mcdi_pcol.h"
  18. #include "regs.h"
  19. #include "vfdi.h"
  20. /* Number of longs required to track all the VIs in a VF */
  21. #define VI_MASK_LENGTH BITS_TO_LONGS(1 << EFX_VI_SCALE_MAX)
  22. /* Maximum number of RX queues supported */
  23. #define VF_MAX_RX_QUEUES 63
  24. /**
  25. * enum efx_vf_tx_filter_mode - TX MAC filtering behaviour
  26. * @VF_TX_FILTER_OFF: Disabled
  27. * @VF_TX_FILTER_AUTO: Enabled if MAC address assigned to VF and only
  28. * 2 TX queues allowed per VF.
  29. * @VF_TX_FILTER_ON: Enabled
  30. */
  31. enum efx_vf_tx_filter_mode {
  32. VF_TX_FILTER_OFF,
  33. VF_TX_FILTER_AUTO,
  34. VF_TX_FILTER_ON,
  35. };
  36. /**
  37. * struct efx_vf - Back-end resource and protocol state for a PCI VF
  38. * @efx: The Efx NIC owning this VF
  39. * @pci_rid: The PCI requester ID for this VF
  40. * @pci_name: The PCI name (formatted address) of this VF
  41. * @index: Index of VF within its port and PF.
  42. * @req: VFDI incoming request work item. Incoming USR_EV events are received
  43. * by the NAPI handler, but must be handled by executing MCDI requests
  44. * inside a work item.
  45. * @req_addr: VFDI incoming request DMA address (in VF's PCI address space).
  46. * @req_type: Expected next incoming (from VF) %VFDI_EV_TYPE member.
  47. * @req_seqno: Expected next incoming (from VF) %VFDI_EV_SEQ member.
  48. * @msg_seqno: Next %VFDI_EV_SEQ member to reply to VF. Protected by
  49. * @status_lock
  50. * @busy: VFDI request queued to be processed or being processed. Receiving
  51. * a VFDI request when @busy is set is an error condition.
  52. * @buf: Incoming VFDI requests are DMA from the VF into this buffer.
  53. * @buftbl_base: Buffer table entries for this VF start at this index.
  54. * @rx_filtering: Receive filtering has been requested by the VF driver.
  55. * @rx_filter_flags: The flags sent in the %VFDI_OP_INSERT_FILTER request.
  56. * @rx_filter_qid: VF relative qid for RX filter requested by VF.
  57. * @rx_filter_id: Receive MAC filter ID. Only one filter per VF is supported.
  58. * @tx_filter_mode: Transmit MAC filtering mode.
  59. * @tx_filter_id: Transmit MAC filter ID.
  60. * @addr: The MAC address and outer vlan tag of the VF.
  61. * @status_addr: VF DMA address of page for &struct vfdi_status updates.
  62. * @status_lock: Mutex protecting @msg_seqno, @status_addr, @addr,
  63. * @peer_page_addrs and @peer_page_count from simultaneous
  64. * updates by the VM and consumption by
  65. * efx_sriov_update_vf_addr()
  66. * @peer_page_addrs: Pointer to an array of guest pages for local addresses.
  67. * @peer_page_count: Number of entries in @peer_page_count.
  68. * @evq0_addrs: Array of guest pages backing evq0.
  69. * @evq0_count: Number of entries in @evq0_addrs.
  70. * @flush_waitq: wait queue used by %VFDI_OP_FINI_ALL_QUEUES handler
  71. * to wait for flush completions.
  72. * @txq_lock: Mutex for TX queue allocation.
  73. * @txq_mask: Mask of initialized transmit queues.
  74. * @txq_count: Number of initialized transmit queues.
  75. * @rxq_mask: Mask of initialized receive queues.
  76. * @rxq_count: Number of initialized receive queues.
  77. * @rxq_retry_mask: Mask or receive queues that need to be flushed again
  78. * due to flush failure.
  79. * @rxq_retry_count: Number of receive queues in @rxq_retry_mask.
  80. * @reset_work: Work item to schedule a VF reset.
  81. */
  82. struct efx_vf {
  83. struct efx_nic *efx;
  84. unsigned int pci_rid;
  85. char pci_name[13]; /* dddd:bb:dd.f */
  86. unsigned int index;
  87. struct work_struct req;
  88. u64 req_addr;
  89. int req_type;
  90. unsigned req_seqno;
  91. unsigned msg_seqno;
  92. bool busy;
  93. struct efx_buffer buf;
  94. unsigned buftbl_base;
  95. bool rx_filtering;
  96. enum efx_filter_flags rx_filter_flags;
  97. unsigned rx_filter_qid;
  98. int rx_filter_id;
  99. enum efx_vf_tx_filter_mode tx_filter_mode;
  100. int tx_filter_id;
  101. struct vfdi_endpoint addr;
  102. u64 status_addr;
  103. struct mutex status_lock;
  104. u64 *peer_page_addrs;
  105. unsigned peer_page_count;
  106. u64 evq0_addrs[EFX_MAX_VF_EVQ_SIZE * sizeof(efx_qword_t) /
  107. EFX_BUF_SIZE];
  108. unsigned evq0_count;
  109. wait_queue_head_t flush_waitq;
  110. struct mutex txq_lock;
  111. unsigned long txq_mask[VI_MASK_LENGTH];
  112. unsigned txq_count;
  113. unsigned long rxq_mask[VI_MASK_LENGTH];
  114. unsigned rxq_count;
  115. unsigned long rxq_retry_mask[VI_MASK_LENGTH];
  116. atomic_t rxq_retry_count;
  117. struct work_struct reset_work;
  118. };
  119. struct efx_memcpy_req {
  120. unsigned int from_rid;
  121. void *from_buf;
  122. u64 from_addr;
  123. unsigned int to_rid;
  124. u64 to_addr;
  125. unsigned length;
  126. };
  127. /**
  128. * struct efx_local_addr - A MAC address on the vswitch without a VF.
  129. *
  130. * Siena does not have a switch, so VFs can't transmit data to each
  131. * other. Instead the VFs must be made aware of the local addresses
  132. * on the vswitch, so that they can arrange for an alternative
  133. * software datapath to be used.
  134. *
  135. * @link: List head for insertion into efx->local_addr_list.
  136. * @addr: Ethernet address
  137. */
  138. struct efx_local_addr {
  139. struct list_head link;
  140. u8 addr[ETH_ALEN];
  141. };
  142. /**
  143. * struct efx_endpoint_page - Page of vfdi_endpoint structures
  144. *
  145. * @link: List head for insertion into efx->local_page_list.
  146. * @ptr: Pointer to page.
  147. * @addr: DMA address of page.
  148. */
  149. struct efx_endpoint_page {
  150. struct list_head link;
  151. void *ptr;
  152. dma_addr_t addr;
  153. };
  154. /* Buffer table entries are reserved txq0,rxq0,evq0,txq1,rxq1,evq1 */
  155. #define EFX_BUFTBL_TXQ_BASE(_vf, _qid) \
  156. ((_vf)->buftbl_base + EFX_VF_BUFTBL_PER_VI * (_qid))
  157. #define EFX_BUFTBL_RXQ_BASE(_vf, _qid) \
  158. (EFX_BUFTBL_TXQ_BASE(_vf, _qid) + \
  159. (EFX_MAX_DMAQ_SIZE * sizeof(efx_qword_t) / EFX_BUF_SIZE))
  160. #define EFX_BUFTBL_EVQ_BASE(_vf, _qid) \
  161. (EFX_BUFTBL_TXQ_BASE(_vf, _qid) + \
  162. (2 * EFX_MAX_DMAQ_SIZE * sizeof(efx_qword_t) / EFX_BUF_SIZE))
  163. #define EFX_FIELD_MASK(_field) \
  164. ((1 << _field ## _WIDTH) - 1)
  165. /* VFs can only use this many transmit channels */
  166. static unsigned int vf_max_tx_channels = 2;
  167. module_param(vf_max_tx_channels, uint, 0444);
  168. MODULE_PARM_DESC(vf_max_tx_channels,
  169. "Limit the number of TX channels VFs can use");
  170. static int max_vfs = -1;
  171. module_param(max_vfs, int, 0444);
  172. MODULE_PARM_DESC(max_vfs,
  173. "Reduce the number of VFs initialized by the driver");
  174. /* Workqueue used by VFDI communication. We can't use the global
  175. * workqueue because it may be running the VF driver's probe()
  176. * routine, which will be blocked there waiting for a VFDI response.
  177. */
  178. static struct workqueue_struct *vfdi_workqueue;
  179. static unsigned abs_index(struct efx_vf *vf, unsigned index)
  180. {
  181. return EFX_VI_BASE + vf->index * efx_vf_size(vf->efx) + index;
  182. }
  183. static int efx_sriov_cmd(struct efx_nic *efx, bool enable,
  184. unsigned *vi_scale_out, unsigned *vf_total_out)
  185. {
  186. MCDI_DECLARE_BUF(inbuf, MC_CMD_SRIOV_IN_LEN);
  187. MCDI_DECLARE_BUF(outbuf, MC_CMD_SRIOV_OUT_LEN);
  188. unsigned vi_scale, vf_total;
  189. size_t outlen;
  190. int rc;
  191. MCDI_SET_DWORD(inbuf, SRIOV_IN_ENABLE, enable ? 1 : 0);
  192. MCDI_SET_DWORD(inbuf, SRIOV_IN_VI_BASE, EFX_VI_BASE);
  193. MCDI_SET_DWORD(inbuf, SRIOV_IN_VF_COUNT, efx->vf_count);
  194. rc = efx_mcdi_rpc(efx, MC_CMD_SRIOV, inbuf, MC_CMD_SRIOV_IN_LEN,
  195. outbuf, MC_CMD_SRIOV_OUT_LEN, &outlen);
  196. if (rc)
  197. return rc;
  198. if (outlen < MC_CMD_SRIOV_OUT_LEN)
  199. return -EIO;
  200. vf_total = MCDI_DWORD(outbuf, SRIOV_OUT_VF_TOTAL);
  201. vi_scale = MCDI_DWORD(outbuf, SRIOV_OUT_VI_SCALE);
  202. if (vi_scale > EFX_VI_SCALE_MAX)
  203. return -EOPNOTSUPP;
  204. if (vi_scale_out)
  205. *vi_scale_out = vi_scale;
  206. if (vf_total_out)
  207. *vf_total_out = vf_total;
  208. return 0;
  209. }
  210. static void efx_sriov_usrev(struct efx_nic *efx, bool enabled)
  211. {
  212. efx_oword_t reg;
  213. EFX_POPULATE_OWORD_2(reg,
  214. FRF_CZ_USREV_DIS, enabled ? 0 : 1,
  215. FRF_CZ_DFLT_EVQ, efx->vfdi_channel->channel);
  216. efx_writeo(efx, &reg, FR_CZ_USR_EV_CFG);
  217. }
  218. static int efx_sriov_memcpy(struct efx_nic *efx, struct efx_memcpy_req *req,
  219. unsigned int count)
  220. {
  221. MCDI_DECLARE_BUF(inbuf, MCDI_CTL_SDU_LEN_MAX_V1);
  222. MCDI_DECLARE_STRUCT_PTR(record);
  223. unsigned int index, used;
  224. u32 from_rid, from_hi, from_lo;
  225. int rc;
  226. mb(); /* Finish writing source/reading dest before DMA starts */
  227. if (WARN_ON(count > MC_CMD_MEMCPY_IN_RECORD_MAXNUM))
  228. return -ENOBUFS;
  229. used = MC_CMD_MEMCPY_IN_LEN(count);
  230. for (index = 0; index < count; index++) {
  231. record = MCDI_ARRAY_STRUCT_PTR(inbuf, MEMCPY_IN_RECORD, index);
  232. MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_NUM_RECORDS,
  233. count);
  234. MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_TO_RID,
  235. req->to_rid);
  236. MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_TO_ADDR_LO,
  237. (u32)req->to_addr);
  238. MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_TO_ADDR_HI,
  239. (u32)(req->to_addr >> 32));
  240. if (req->from_buf == NULL) {
  241. from_rid = req->from_rid;
  242. from_lo = (u32)req->from_addr;
  243. from_hi = (u32)(req->from_addr >> 32);
  244. } else {
  245. if (WARN_ON(used + req->length >
  246. MCDI_CTL_SDU_LEN_MAX_V1)) {
  247. rc = -ENOBUFS;
  248. goto out;
  249. }
  250. from_rid = MC_CMD_MEMCPY_RECORD_TYPEDEF_RID_INLINE;
  251. from_lo = used;
  252. from_hi = 0;
  253. memcpy(_MCDI_PTR(inbuf, used), req->from_buf,
  254. req->length);
  255. used += req->length;
  256. }
  257. MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_FROM_RID, from_rid);
  258. MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_FROM_ADDR_LO,
  259. from_lo);
  260. MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_FROM_ADDR_HI,
  261. from_hi);
  262. MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_LENGTH,
  263. req->length);
  264. ++req;
  265. }
  266. rc = efx_mcdi_rpc(efx, MC_CMD_MEMCPY, inbuf, used, NULL, 0, NULL);
  267. out:
  268. mb(); /* Don't write source/read dest before DMA is complete */
  269. return rc;
  270. }
  271. /* The TX filter is entirely controlled by this driver, and is modified
  272. * underneath the feet of the VF
  273. */
  274. static void efx_sriov_reset_tx_filter(struct efx_vf *vf)
  275. {
  276. struct efx_nic *efx = vf->efx;
  277. struct efx_filter_spec filter;
  278. u16 vlan;
  279. int rc;
  280. if (vf->tx_filter_id != -1) {
  281. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  282. vf->tx_filter_id);
  283. netif_dbg(efx, hw, efx->net_dev, "Removed vf %s tx filter %d\n",
  284. vf->pci_name, vf->tx_filter_id);
  285. vf->tx_filter_id = -1;
  286. }
  287. if (is_zero_ether_addr(vf->addr.mac_addr))
  288. return;
  289. /* Turn on TX filtering automatically if not explicitly
  290. * enabled or disabled.
  291. */
  292. if (vf->tx_filter_mode == VF_TX_FILTER_AUTO && vf_max_tx_channels <= 2)
  293. vf->tx_filter_mode = VF_TX_FILTER_ON;
  294. vlan = ntohs(vf->addr.tci) & VLAN_VID_MASK;
  295. efx_filter_init_tx(&filter, abs_index(vf, 0));
  296. rc = efx_filter_set_eth_local(&filter,
  297. vlan ? vlan : EFX_FILTER_VID_UNSPEC,
  298. vf->addr.mac_addr);
  299. BUG_ON(rc);
  300. rc = efx_filter_insert_filter(efx, &filter, true);
  301. if (rc < 0) {
  302. netif_warn(efx, hw, efx->net_dev,
  303. "Unable to migrate tx filter for vf %s\n",
  304. vf->pci_name);
  305. } else {
  306. netif_dbg(efx, hw, efx->net_dev, "Inserted vf %s tx filter %d\n",
  307. vf->pci_name, rc);
  308. vf->tx_filter_id = rc;
  309. }
  310. }
  311. /* The RX filter is managed here on behalf of the VF driver */
  312. static void efx_sriov_reset_rx_filter(struct efx_vf *vf)
  313. {
  314. struct efx_nic *efx = vf->efx;
  315. struct efx_filter_spec filter;
  316. u16 vlan;
  317. int rc;
  318. if (vf->rx_filter_id != -1) {
  319. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  320. vf->rx_filter_id);
  321. netif_dbg(efx, hw, efx->net_dev, "Removed vf %s rx filter %d\n",
  322. vf->pci_name, vf->rx_filter_id);
  323. vf->rx_filter_id = -1;
  324. }
  325. if (!vf->rx_filtering || is_zero_ether_addr(vf->addr.mac_addr))
  326. return;
  327. vlan = ntohs(vf->addr.tci) & VLAN_VID_MASK;
  328. efx_filter_init_rx(&filter, EFX_FILTER_PRI_REQUIRED,
  329. vf->rx_filter_flags,
  330. abs_index(vf, vf->rx_filter_qid));
  331. rc = efx_filter_set_eth_local(&filter,
  332. vlan ? vlan : EFX_FILTER_VID_UNSPEC,
  333. vf->addr.mac_addr);
  334. BUG_ON(rc);
  335. rc = efx_filter_insert_filter(efx, &filter, true);
  336. if (rc < 0) {
  337. netif_warn(efx, hw, efx->net_dev,
  338. "Unable to insert rx filter for vf %s\n",
  339. vf->pci_name);
  340. } else {
  341. netif_dbg(efx, hw, efx->net_dev, "Inserted vf %s rx filter %d\n",
  342. vf->pci_name, rc);
  343. vf->rx_filter_id = rc;
  344. }
  345. }
  346. static void __efx_sriov_update_vf_addr(struct efx_vf *vf)
  347. {
  348. efx_sriov_reset_tx_filter(vf);
  349. efx_sriov_reset_rx_filter(vf);
  350. queue_work(vfdi_workqueue, &vf->efx->peer_work);
  351. }
  352. /* Push the peer list to this VF. The caller must hold status_lock to interlock
  353. * with VFDI requests, and they must be serialised against manipulation of
  354. * local_page_list, either by acquiring local_lock or by running from
  355. * efx_sriov_peer_work()
  356. */
  357. static void __efx_sriov_push_vf_status(struct efx_vf *vf)
  358. {
  359. struct efx_nic *efx = vf->efx;
  360. struct vfdi_status *status = efx->vfdi_status.addr;
  361. struct efx_memcpy_req copy[4];
  362. struct efx_endpoint_page *epp;
  363. unsigned int pos, count;
  364. unsigned data_offset;
  365. efx_qword_t event;
  366. WARN_ON(!mutex_is_locked(&vf->status_lock));
  367. WARN_ON(!vf->status_addr);
  368. status->local = vf->addr;
  369. status->generation_end = ++status->generation_start;
  370. memset(copy, '\0', sizeof(copy));
  371. /* Write generation_start */
  372. copy[0].from_buf = &status->generation_start;
  373. copy[0].to_rid = vf->pci_rid;
  374. copy[0].to_addr = vf->status_addr + offsetof(struct vfdi_status,
  375. generation_start);
  376. copy[0].length = sizeof(status->generation_start);
  377. /* DMA the rest of the structure (excluding the generations). This
  378. * assumes that the non-generation portion of vfdi_status is in
  379. * one chunk starting at the version member.
  380. */
  381. data_offset = offsetof(struct vfdi_status, version);
  382. copy[1].from_rid = efx->pci_dev->devfn;
  383. copy[1].from_addr = efx->vfdi_status.dma_addr + data_offset;
  384. copy[1].to_rid = vf->pci_rid;
  385. copy[1].to_addr = vf->status_addr + data_offset;
  386. copy[1].length = status->length - data_offset;
  387. /* Copy the peer pages */
  388. pos = 2;
  389. count = 0;
  390. list_for_each_entry(epp, &efx->local_page_list, link) {
  391. if (count == vf->peer_page_count) {
  392. /* The VF driver will know they need to provide more
  393. * pages because peer_addr_count is too large.
  394. */
  395. break;
  396. }
  397. copy[pos].from_buf = NULL;
  398. copy[pos].from_rid = efx->pci_dev->devfn;
  399. copy[pos].from_addr = epp->addr;
  400. copy[pos].to_rid = vf->pci_rid;
  401. copy[pos].to_addr = vf->peer_page_addrs[count];
  402. copy[pos].length = EFX_PAGE_SIZE;
  403. if (++pos == ARRAY_SIZE(copy)) {
  404. efx_sriov_memcpy(efx, copy, ARRAY_SIZE(copy));
  405. pos = 0;
  406. }
  407. ++count;
  408. }
  409. /* Write generation_end */
  410. copy[pos].from_buf = &status->generation_end;
  411. copy[pos].to_rid = vf->pci_rid;
  412. copy[pos].to_addr = vf->status_addr + offsetof(struct vfdi_status,
  413. generation_end);
  414. copy[pos].length = sizeof(status->generation_end);
  415. efx_sriov_memcpy(efx, copy, pos + 1);
  416. /* Notify the guest */
  417. EFX_POPULATE_QWORD_3(event,
  418. FSF_AZ_EV_CODE, FSE_CZ_EV_CODE_USER_EV,
  419. VFDI_EV_SEQ, (vf->msg_seqno & 0xff),
  420. VFDI_EV_TYPE, VFDI_EV_TYPE_STATUS);
  421. ++vf->msg_seqno;
  422. efx_generate_event(efx, EFX_VI_BASE + vf->index * efx_vf_size(efx),
  423. &event);
  424. }
  425. static void efx_sriov_bufs(struct efx_nic *efx, unsigned offset,
  426. u64 *addr, unsigned count)
  427. {
  428. efx_qword_t buf;
  429. unsigned pos;
  430. for (pos = 0; pos < count; ++pos) {
  431. EFX_POPULATE_QWORD_3(buf,
  432. FRF_AZ_BUF_ADR_REGION, 0,
  433. FRF_AZ_BUF_ADR_FBUF,
  434. addr ? addr[pos] >> 12 : 0,
  435. FRF_AZ_BUF_OWNER_ID_FBUF, 0);
  436. efx_sram_writeq(efx, efx->membase + FR_BZ_BUF_FULL_TBL,
  437. &buf, offset + pos);
  438. }
  439. }
  440. static bool bad_vf_index(struct efx_nic *efx, unsigned index)
  441. {
  442. return index >= efx_vf_size(efx);
  443. }
  444. static bool bad_buf_count(unsigned buf_count, unsigned max_entry_count)
  445. {
  446. unsigned max_buf_count = max_entry_count *
  447. sizeof(efx_qword_t) / EFX_BUF_SIZE;
  448. return ((buf_count & (buf_count - 1)) || buf_count > max_buf_count);
  449. }
  450. /* Check that VI specified by per-port index belongs to a VF.
  451. * Optionally set VF index and VI index within the VF.
  452. */
  453. static bool map_vi_index(struct efx_nic *efx, unsigned abs_index,
  454. struct efx_vf **vf_out, unsigned *rel_index_out)
  455. {
  456. unsigned vf_i;
  457. if (abs_index < EFX_VI_BASE)
  458. return true;
  459. vf_i = (abs_index - EFX_VI_BASE) / efx_vf_size(efx);
  460. if (vf_i >= efx->vf_init_count)
  461. return true;
  462. if (vf_out)
  463. *vf_out = efx->vf + vf_i;
  464. if (rel_index_out)
  465. *rel_index_out = abs_index % efx_vf_size(efx);
  466. return false;
  467. }
  468. static int efx_vfdi_init_evq(struct efx_vf *vf)
  469. {
  470. struct efx_nic *efx = vf->efx;
  471. struct vfdi_req *req = vf->buf.addr;
  472. unsigned vf_evq = req->u.init_evq.index;
  473. unsigned buf_count = req->u.init_evq.buf_count;
  474. unsigned abs_evq = abs_index(vf, vf_evq);
  475. unsigned buftbl = EFX_BUFTBL_EVQ_BASE(vf, vf_evq);
  476. efx_oword_t reg;
  477. if (bad_vf_index(efx, vf_evq) ||
  478. bad_buf_count(buf_count, EFX_MAX_VF_EVQ_SIZE)) {
  479. if (net_ratelimit())
  480. netif_err(efx, hw, efx->net_dev,
  481. "ERROR: Invalid INIT_EVQ from %s: evq %d bufs %d\n",
  482. vf->pci_name, vf_evq, buf_count);
  483. return VFDI_RC_EINVAL;
  484. }
  485. efx_sriov_bufs(efx, buftbl, req->u.init_evq.addr, buf_count);
  486. EFX_POPULATE_OWORD_3(reg,
  487. FRF_CZ_TIMER_Q_EN, 1,
  488. FRF_CZ_HOST_NOTIFY_MODE, 0,
  489. FRF_CZ_TIMER_MODE, FFE_CZ_TIMER_MODE_DIS);
  490. efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, abs_evq);
  491. EFX_POPULATE_OWORD_3(reg,
  492. FRF_AZ_EVQ_EN, 1,
  493. FRF_AZ_EVQ_SIZE, __ffs(buf_count),
  494. FRF_AZ_EVQ_BUF_BASE_ID, buftbl);
  495. efx_writeo_table(efx, &reg, FR_BZ_EVQ_PTR_TBL, abs_evq);
  496. if (vf_evq == 0) {
  497. memcpy(vf->evq0_addrs, req->u.init_evq.addr,
  498. buf_count * sizeof(u64));
  499. vf->evq0_count = buf_count;
  500. }
  501. return VFDI_RC_SUCCESS;
  502. }
  503. static int efx_vfdi_init_rxq(struct efx_vf *vf)
  504. {
  505. struct efx_nic *efx = vf->efx;
  506. struct vfdi_req *req = vf->buf.addr;
  507. unsigned vf_rxq = req->u.init_rxq.index;
  508. unsigned vf_evq = req->u.init_rxq.evq;
  509. unsigned buf_count = req->u.init_rxq.buf_count;
  510. unsigned buftbl = EFX_BUFTBL_RXQ_BASE(vf, vf_rxq);
  511. unsigned label;
  512. efx_oword_t reg;
  513. if (bad_vf_index(efx, vf_evq) || bad_vf_index(efx, vf_rxq) ||
  514. vf_rxq >= VF_MAX_RX_QUEUES ||
  515. bad_buf_count(buf_count, EFX_MAX_DMAQ_SIZE)) {
  516. if (net_ratelimit())
  517. netif_err(efx, hw, efx->net_dev,
  518. "ERROR: Invalid INIT_RXQ from %s: rxq %d evq %d "
  519. "buf_count %d\n", vf->pci_name, vf_rxq,
  520. vf_evq, buf_count);
  521. return VFDI_RC_EINVAL;
  522. }
  523. if (__test_and_set_bit(req->u.init_rxq.index, vf->rxq_mask))
  524. ++vf->rxq_count;
  525. efx_sriov_bufs(efx, buftbl, req->u.init_rxq.addr, buf_count);
  526. label = req->u.init_rxq.label & EFX_FIELD_MASK(FRF_AZ_RX_DESCQ_LABEL);
  527. EFX_POPULATE_OWORD_6(reg,
  528. FRF_AZ_RX_DESCQ_BUF_BASE_ID, buftbl,
  529. FRF_AZ_RX_DESCQ_EVQ_ID, abs_index(vf, vf_evq),
  530. FRF_AZ_RX_DESCQ_LABEL, label,
  531. FRF_AZ_RX_DESCQ_SIZE, __ffs(buf_count),
  532. FRF_AZ_RX_DESCQ_JUMBO,
  533. !!(req->u.init_rxq.flags &
  534. VFDI_RXQ_FLAG_SCATTER_EN),
  535. FRF_AZ_RX_DESCQ_EN, 1);
  536. efx_writeo_table(efx, &reg, FR_BZ_RX_DESC_PTR_TBL,
  537. abs_index(vf, vf_rxq));
  538. return VFDI_RC_SUCCESS;
  539. }
  540. static int efx_vfdi_init_txq(struct efx_vf *vf)
  541. {
  542. struct efx_nic *efx = vf->efx;
  543. struct vfdi_req *req = vf->buf.addr;
  544. unsigned vf_txq = req->u.init_txq.index;
  545. unsigned vf_evq = req->u.init_txq.evq;
  546. unsigned buf_count = req->u.init_txq.buf_count;
  547. unsigned buftbl = EFX_BUFTBL_TXQ_BASE(vf, vf_txq);
  548. unsigned label, eth_filt_en;
  549. efx_oword_t reg;
  550. if (bad_vf_index(efx, vf_evq) || bad_vf_index(efx, vf_txq) ||
  551. vf_txq >= vf_max_tx_channels ||
  552. bad_buf_count(buf_count, EFX_MAX_DMAQ_SIZE)) {
  553. if (net_ratelimit())
  554. netif_err(efx, hw, efx->net_dev,
  555. "ERROR: Invalid INIT_TXQ from %s: txq %d evq %d "
  556. "buf_count %d\n", vf->pci_name, vf_txq,
  557. vf_evq, buf_count);
  558. return VFDI_RC_EINVAL;
  559. }
  560. mutex_lock(&vf->txq_lock);
  561. if (__test_and_set_bit(req->u.init_txq.index, vf->txq_mask))
  562. ++vf->txq_count;
  563. mutex_unlock(&vf->txq_lock);
  564. efx_sriov_bufs(efx, buftbl, req->u.init_txq.addr, buf_count);
  565. eth_filt_en = vf->tx_filter_mode == VF_TX_FILTER_ON;
  566. label = req->u.init_txq.label & EFX_FIELD_MASK(FRF_AZ_TX_DESCQ_LABEL);
  567. EFX_POPULATE_OWORD_8(reg,
  568. FRF_CZ_TX_DPT_Q_MASK_WIDTH, min(efx->vi_scale, 1U),
  569. FRF_CZ_TX_DPT_ETH_FILT_EN, eth_filt_en,
  570. FRF_AZ_TX_DESCQ_EN, 1,
  571. FRF_AZ_TX_DESCQ_BUF_BASE_ID, buftbl,
  572. FRF_AZ_TX_DESCQ_EVQ_ID, abs_index(vf, vf_evq),
  573. FRF_AZ_TX_DESCQ_LABEL, label,
  574. FRF_AZ_TX_DESCQ_SIZE, __ffs(buf_count),
  575. FRF_BZ_TX_NON_IP_DROP_DIS, 1);
  576. efx_writeo_table(efx, &reg, FR_BZ_TX_DESC_PTR_TBL,
  577. abs_index(vf, vf_txq));
  578. return VFDI_RC_SUCCESS;
  579. }
  580. /* Returns true when efx_vfdi_fini_all_queues should wake */
  581. static bool efx_vfdi_flush_wake(struct efx_vf *vf)
  582. {
  583. /* Ensure that all updates are visible to efx_vfdi_fini_all_queues() */
  584. smp_mb();
  585. return (!vf->txq_count && !vf->rxq_count) ||
  586. atomic_read(&vf->rxq_retry_count);
  587. }
  588. static void efx_vfdi_flush_clear(struct efx_vf *vf)
  589. {
  590. memset(vf->txq_mask, 0, sizeof(vf->txq_mask));
  591. vf->txq_count = 0;
  592. memset(vf->rxq_mask, 0, sizeof(vf->rxq_mask));
  593. vf->rxq_count = 0;
  594. memset(vf->rxq_retry_mask, 0, sizeof(vf->rxq_retry_mask));
  595. atomic_set(&vf->rxq_retry_count, 0);
  596. }
  597. static int efx_vfdi_fini_all_queues(struct efx_vf *vf)
  598. {
  599. struct efx_nic *efx = vf->efx;
  600. efx_oword_t reg;
  601. unsigned count = efx_vf_size(efx);
  602. unsigned vf_offset = EFX_VI_BASE + vf->index * efx_vf_size(efx);
  603. unsigned timeout = HZ;
  604. unsigned index, rxqs_count;
  605. MCDI_DECLARE_BUF(inbuf, MC_CMD_FLUSH_RX_QUEUES_IN_LENMAX);
  606. int rc;
  607. BUILD_BUG_ON(VF_MAX_RX_QUEUES >
  608. MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM);
  609. rtnl_lock();
  610. siena_prepare_flush(efx);
  611. rtnl_unlock();
  612. /* Flush all the initialized queues */
  613. rxqs_count = 0;
  614. for (index = 0; index < count; ++index) {
  615. if (test_bit(index, vf->txq_mask)) {
  616. EFX_POPULATE_OWORD_2(reg,
  617. FRF_AZ_TX_FLUSH_DESCQ_CMD, 1,
  618. FRF_AZ_TX_FLUSH_DESCQ,
  619. vf_offset + index);
  620. efx_writeo(efx, &reg, FR_AZ_TX_FLUSH_DESCQ);
  621. }
  622. if (test_bit(index, vf->rxq_mask)) {
  623. MCDI_SET_ARRAY_DWORD(
  624. inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
  625. rxqs_count, vf_offset + index);
  626. rxqs_count++;
  627. }
  628. }
  629. atomic_set(&vf->rxq_retry_count, 0);
  630. while (timeout && (vf->rxq_count || vf->txq_count)) {
  631. rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf,
  632. MC_CMD_FLUSH_RX_QUEUES_IN_LEN(rxqs_count),
  633. NULL, 0, NULL);
  634. WARN_ON(rc < 0);
  635. timeout = wait_event_timeout(vf->flush_waitq,
  636. efx_vfdi_flush_wake(vf),
  637. timeout);
  638. rxqs_count = 0;
  639. for (index = 0; index < count; ++index) {
  640. if (test_and_clear_bit(index, vf->rxq_retry_mask)) {
  641. atomic_dec(&vf->rxq_retry_count);
  642. MCDI_SET_ARRAY_DWORD(
  643. inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
  644. rxqs_count, vf_offset + index);
  645. rxqs_count++;
  646. }
  647. }
  648. }
  649. rtnl_lock();
  650. siena_finish_flush(efx);
  651. rtnl_unlock();
  652. /* Irrespective of success/failure, fini the queues */
  653. EFX_ZERO_OWORD(reg);
  654. for (index = 0; index < count; ++index) {
  655. efx_writeo_table(efx, &reg, FR_BZ_RX_DESC_PTR_TBL,
  656. vf_offset + index);
  657. efx_writeo_table(efx, &reg, FR_BZ_TX_DESC_PTR_TBL,
  658. vf_offset + index);
  659. efx_writeo_table(efx, &reg, FR_BZ_EVQ_PTR_TBL,
  660. vf_offset + index);
  661. efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL,
  662. vf_offset + index);
  663. }
  664. efx_sriov_bufs(efx, vf->buftbl_base, NULL,
  665. EFX_VF_BUFTBL_PER_VI * efx_vf_size(efx));
  666. efx_vfdi_flush_clear(vf);
  667. vf->evq0_count = 0;
  668. return timeout ? 0 : VFDI_RC_ETIMEDOUT;
  669. }
  670. static int efx_vfdi_insert_filter(struct efx_vf *vf)
  671. {
  672. struct efx_nic *efx = vf->efx;
  673. struct vfdi_req *req = vf->buf.addr;
  674. unsigned vf_rxq = req->u.mac_filter.rxq;
  675. unsigned flags;
  676. if (bad_vf_index(efx, vf_rxq) || vf->rx_filtering) {
  677. if (net_ratelimit())
  678. netif_err(efx, hw, efx->net_dev,
  679. "ERROR: Invalid INSERT_FILTER from %s: rxq %d "
  680. "flags 0x%x\n", vf->pci_name, vf_rxq,
  681. req->u.mac_filter.flags);
  682. return VFDI_RC_EINVAL;
  683. }
  684. flags = 0;
  685. if (req->u.mac_filter.flags & VFDI_MAC_FILTER_FLAG_RSS)
  686. flags |= EFX_FILTER_FLAG_RX_RSS;
  687. if (req->u.mac_filter.flags & VFDI_MAC_FILTER_FLAG_SCATTER)
  688. flags |= EFX_FILTER_FLAG_RX_SCATTER;
  689. vf->rx_filter_flags = flags;
  690. vf->rx_filter_qid = vf_rxq;
  691. vf->rx_filtering = true;
  692. efx_sriov_reset_rx_filter(vf);
  693. queue_work(vfdi_workqueue, &efx->peer_work);
  694. return VFDI_RC_SUCCESS;
  695. }
  696. static int efx_vfdi_remove_all_filters(struct efx_vf *vf)
  697. {
  698. vf->rx_filtering = false;
  699. efx_sriov_reset_rx_filter(vf);
  700. queue_work(vfdi_workqueue, &vf->efx->peer_work);
  701. return VFDI_RC_SUCCESS;
  702. }
  703. static int efx_vfdi_set_status_page(struct efx_vf *vf)
  704. {
  705. struct efx_nic *efx = vf->efx;
  706. struct vfdi_req *req = vf->buf.addr;
  707. u64 page_count = req->u.set_status_page.peer_page_count;
  708. u64 max_page_count =
  709. (EFX_PAGE_SIZE -
  710. offsetof(struct vfdi_req, u.set_status_page.peer_page_addr[0]))
  711. / sizeof(req->u.set_status_page.peer_page_addr[0]);
  712. if (!req->u.set_status_page.dma_addr || page_count > max_page_count) {
  713. if (net_ratelimit())
  714. netif_err(efx, hw, efx->net_dev,
  715. "ERROR: Invalid SET_STATUS_PAGE from %s\n",
  716. vf->pci_name);
  717. return VFDI_RC_EINVAL;
  718. }
  719. mutex_lock(&efx->local_lock);
  720. mutex_lock(&vf->status_lock);
  721. vf->status_addr = req->u.set_status_page.dma_addr;
  722. kfree(vf->peer_page_addrs);
  723. vf->peer_page_addrs = NULL;
  724. vf->peer_page_count = 0;
  725. if (page_count) {
  726. vf->peer_page_addrs = kcalloc(page_count, sizeof(u64),
  727. GFP_KERNEL);
  728. if (vf->peer_page_addrs) {
  729. memcpy(vf->peer_page_addrs,
  730. req->u.set_status_page.peer_page_addr,
  731. page_count * sizeof(u64));
  732. vf->peer_page_count = page_count;
  733. }
  734. }
  735. __efx_sriov_push_vf_status(vf);
  736. mutex_unlock(&vf->status_lock);
  737. mutex_unlock(&efx->local_lock);
  738. return VFDI_RC_SUCCESS;
  739. }
  740. static int efx_vfdi_clear_status_page(struct efx_vf *vf)
  741. {
  742. mutex_lock(&vf->status_lock);
  743. vf->status_addr = 0;
  744. mutex_unlock(&vf->status_lock);
  745. return VFDI_RC_SUCCESS;
  746. }
  747. typedef int (*efx_vfdi_op_t)(struct efx_vf *vf);
  748. static const efx_vfdi_op_t vfdi_ops[VFDI_OP_LIMIT] = {
  749. [VFDI_OP_INIT_EVQ] = efx_vfdi_init_evq,
  750. [VFDI_OP_INIT_TXQ] = efx_vfdi_init_txq,
  751. [VFDI_OP_INIT_RXQ] = efx_vfdi_init_rxq,
  752. [VFDI_OP_FINI_ALL_QUEUES] = efx_vfdi_fini_all_queues,
  753. [VFDI_OP_INSERT_FILTER] = efx_vfdi_insert_filter,
  754. [VFDI_OP_REMOVE_ALL_FILTERS] = efx_vfdi_remove_all_filters,
  755. [VFDI_OP_SET_STATUS_PAGE] = efx_vfdi_set_status_page,
  756. [VFDI_OP_CLEAR_STATUS_PAGE] = efx_vfdi_clear_status_page,
  757. };
  758. static void efx_sriov_vfdi(struct work_struct *work)
  759. {
  760. struct efx_vf *vf = container_of(work, struct efx_vf, req);
  761. struct efx_nic *efx = vf->efx;
  762. struct vfdi_req *req = vf->buf.addr;
  763. struct efx_memcpy_req copy[2];
  764. int rc;
  765. /* Copy this page into the local address space */
  766. memset(copy, '\0', sizeof(copy));
  767. copy[0].from_rid = vf->pci_rid;
  768. copy[0].from_addr = vf->req_addr;
  769. copy[0].to_rid = efx->pci_dev->devfn;
  770. copy[0].to_addr = vf->buf.dma_addr;
  771. copy[0].length = EFX_PAGE_SIZE;
  772. rc = efx_sriov_memcpy(efx, copy, 1);
  773. if (rc) {
  774. /* If we can't get the request, we can't reply to the caller */
  775. if (net_ratelimit())
  776. netif_err(efx, hw, efx->net_dev,
  777. "ERROR: Unable to fetch VFDI request from %s rc %d\n",
  778. vf->pci_name, -rc);
  779. vf->busy = false;
  780. return;
  781. }
  782. if (req->op < VFDI_OP_LIMIT && vfdi_ops[req->op] != NULL) {
  783. rc = vfdi_ops[req->op](vf);
  784. if (rc == 0) {
  785. netif_dbg(efx, hw, efx->net_dev,
  786. "vfdi request %d from %s ok\n",
  787. req->op, vf->pci_name);
  788. }
  789. } else {
  790. netif_dbg(efx, hw, efx->net_dev,
  791. "ERROR: Unrecognised request %d from VF %s addr "
  792. "%llx\n", req->op, vf->pci_name,
  793. (unsigned long long)vf->req_addr);
  794. rc = VFDI_RC_EOPNOTSUPP;
  795. }
  796. /* Allow subsequent VF requests */
  797. vf->busy = false;
  798. smp_wmb();
  799. /* Respond to the request */
  800. req->rc = rc;
  801. req->op = VFDI_OP_RESPONSE;
  802. memset(copy, '\0', sizeof(copy));
  803. copy[0].from_buf = &req->rc;
  804. copy[0].to_rid = vf->pci_rid;
  805. copy[0].to_addr = vf->req_addr + offsetof(struct vfdi_req, rc);
  806. copy[0].length = sizeof(req->rc);
  807. copy[1].from_buf = &req->op;
  808. copy[1].to_rid = vf->pci_rid;
  809. copy[1].to_addr = vf->req_addr + offsetof(struct vfdi_req, op);
  810. copy[1].length = sizeof(req->op);
  811. (void) efx_sriov_memcpy(efx, copy, ARRAY_SIZE(copy));
  812. }
  813. /* After a reset the event queues inside the guests no longer exist. Fill the
  814. * event ring in guest memory with VFDI reset events, then (re-initialise) the
  815. * event queue to raise an interrupt. The guest driver will then recover.
  816. */
  817. static void efx_sriov_reset_vf(struct efx_vf *vf, struct efx_buffer *buffer)
  818. {
  819. struct efx_nic *efx = vf->efx;
  820. struct efx_memcpy_req copy_req[4];
  821. efx_qword_t event;
  822. unsigned int pos, count, k, buftbl, abs_evq;
  823. efx_oword_t reg;
  824. efx_dword_t ptr;
  825. int rc;
  826. BUG_ON(buffer->len != EFX_PAGE_SIZE);
  827. if (!vf->evq0_count)
  828. return;
  829. BUG_ON(vf->evq0_count & (vf->evq0_count - 1));
  830. mutex_lock(&vf->status_lock);
  831. EFX_POPULATE_QWORD_3(event,
  832. FSF_AZ_EV_CODE, FSE_CZ_EV_CODE_USER_EV,
  833. VFDI_EV_SEQ, vf->msg_seqno,
  834. VFDI_EV_TYPE, VFDI_EV_TYPE_RESET);
  835. vf->msg_seqno++;
  836. for (pos = 0; pos < EFX_PAGE_SIZE; pos += sizeof(event))
  837. memcpy(buffer->addr + pos, &event, sizeof(event));
  838. for (pos = 0; pos < vf->evq0_count; pos += count) {
  839. count = min_t(unsigned, vf->evq0_count - pos,
  840. ARRAY_SIZE(copy_req));
  841. for (k = 0; k < count; k++) {
  842. copy_req[k].from_buf = NULL;
  843. copy_req[k].from_rid = efx->pci_dev->devfn;
  844. copy_req[k].from_addr = buffer->dma_addr;
  845. copy_req[k].to_rid = vf->pci_rid;
  846. copy_req[k].to_addr = vf->evq0_addrs[pos + k];
  847. copy_req[k].length = EFX_PAGE_SIZE;
  848. }
  849. rc = efx_sriov_memcpy(efx, copy_req, count);
  850. if (rc) {
  851. if (net_ratelimit())
  852. netif_err(efx, hw, efx->net_dev,
  853. "ERROR: Unable to notify %s of reset"
  854. ": %d\n", vf->pci_name, -rc);
  855. break;
  856. }
  857. }
  858. /* Reinitialise, arm and trigger evq0 */
  859. abs_evq = abs_index(vf, 0);
  860. buftbl = EFX_BUFTBL_EVQ_BASE(vf, 0);
  861. efx_sriov_bufs(efx, buftbl, vf->evq0_addrs, vf->evq0_count);
  862. EFX_POPULATE_OWORD_3(reg,
  863. FRF_CZ_TIMER_Q_EN, 1,
  864. FRF_CZ_HOST_NOTIFY_MODE, 0,
  865. FRF_CZ_TIMER_MODE, FFE_CZ_TIMER_MODE_DIS);
  866. efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, abs_evq);
  867. EFX_POPULATE_OWORD_3(reg,
  868. FRF_AZ_EVQ_EN, 1,
  869. FRF_AZ_EVQ_SIZE, __ffs(vf->evq0_count),
  870. FRF_AZ_EVQ_BUF_BASE_ID, buftbl);
  871. efx_writeo_table(efx, &reg, FR_BZ_EVQ_PTR_TBL, abs_evq);
  872. EFX_POPULATE_DWORD_1(ptr, FRF_AZ_EVQ_RPTR, 0);
  873. efx_writed(efx, &ptr, FR_BZ_EVQ_RPTR + FR_BZ_EVQ_RPTR_STEP * abs_evq);
  874. mutex_unlock(&vf->status_lock);
  875. }
  876. static void efx_sriov_reset_vf_work(struct work_struct *work)
  877. {
  878. struct efx_vf *vf = container_of(work, struct efx_vf, req);
  879. struct efx_nic *efx = vf->efx;
  880. struct efx_buffer buf;
  881. if (!efx_nic_alloc_buffer(efx, &buf, EFX_PAGE_SIZE)) {
  882. efx_sriov_reset_vf(vf, &buf);
  883. efx_nic_free_buffer(efx, &buf);
  884. }
  885. }
  886. static void efx_sriov_handle_no_channel(struct efx_nic *efx)
  887. {
  888. netif_err(efx, drv, efx->net_dev,
  889. "ERROR: IOV requires MSI-X and 1 additional interrupt"
  890. "vector. IOV disabled\n");
  891. efx->vf_count = 0;
  892. }
  893. static int efx_sriov_probe_channel(struct efx_channel *channel)
  894. {
  895. channel->efx->vfdi_channel = channel;
  896. return 0;
  897. }
  898. static void
  899. efx_sriov_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
  900. {
  901. snprintf(buf, len, "%s-iov", channel->efx->name);
  902. }
  903. static const struct efx_channel_type efx_sriov_channel_type = {
  904. .handle_no_channel = efx_sriov_handle_no_channel,
  905. .pre_probe = efx_sriov_probe_channel,
  906. .post_remove = efx_channel_dummy_op_void,
  907. .get_name = efx_sriov_get_channel_name,
  908. /* no copy operation; channel must not be reallocated */
  909. .keep_eventq = true,
  910. };
  911. void efx_sriov_probe(struct efx_nic *efx)
  912. {
  913. unsigned count;
  914. if (!max_vfs)
  915. return;
  916. if (efx_sriov_cmd(efx, false, &efx->vi_scale, &count))
  917. return;
  918. if (count > 0 && count > max_vfs)
  919. count = max_vfs;
  920. /* efx_nic_dimension_resources() will reduce vf_count as appopriate */
  921. efx->vf_count = count;
  922. efx->extra_channel_type[EFX_EXTRA_CHANNEL_IOV] = &efx_sriov_channel_type;
  923. }
  924. /* Copy the list of individual addresses into the vfdi_status.peers
  925. * array and auxillary pages, protected by %local_lock. Drop that lock
  926. * and then broadcast the address list to every VF.
  927. */
  928. static void efx_sriov_peer_work(struct work_struct *data)
  929. {
  930. struct efx_nic *efx = container_of(data, struct efx_nic, peer_work);
  931. struct vfdi_status *vfdi_status = efx->vfdi_status.addr;
  932. struct efx_vf *vf;
  933. struct efx_local_addr *local_addr;
  934. struct vfdi_endpoint *peer;
  935. struct efx_endpoint_page *epp;
  936. struct list_head pages;
  937. unsigned int peer_space;
  938. unsigned int peer_count;
  939. unsigned int pos;
  940. mutex_lock(&efx->local_lock);
  941. /* Move the existing peer pages off %local_page_list */
  942. INIT_LIST_HEAD(&pages);
  943. list_splice_tail_init(&efx->local_page_list, &pages);
  944. /* Populate the VF addresses starting from entry 1 (entry 0 is
  945. * the PF address)
  946. */
  947. peer = vfdi_status->peers + 1;
  948. peer_space = ARRAY_SIZE(vfdi_status->peers) - 1;
  949. peer_count = 1;
  950. for (pos = 0; pos < efx->vf_count; ++pos) {
  951. vf = efx->vf + pos;
  952. mutex_lock(&vf->status_lock);
  953. if (vf->rx_filtering && !is_zero_ether_addr(vf->addr.mac_addr)) {
  954. *peer++ = vf->addr;
  955. ++peer_count;
  956. --peer_space;
  957. BUG_ON(peer_space == 0);
  958. }
  959. mutex_unlock(&vf->status_lock);
  960. }
  961. /* Fill the remaining addresses */
  962. list_for_each_entry(local_addr, &efx->local_addr_list, link) {
  963. memcpy(peer->mac_addr, local_addr->addr, ETH_ALEN);
  964. peer->tci = 0;
  965. ++peer;
  966. ++peer_count;
  967. if (--peer_space == 0) {
  968. if (list_empty(&pages)) {
  969. epp = kmalloc(sizeof(*epp), GFP_KERNEL);
  970. if (!epp)
  971. break;
  972. epp->ptr = dma_alloc_coherent(
  973. &efx->pci_dev->dev, EFX_PAGE_SIZE,
  974. &epp->addr, GFP_KERNEL);
  975. if (!epp->ptr) {
  976. kfree(epp);
  977. break;
  978. }
  979. } else {
  980. epp = list_first_entry(
  981. &pages, struct efx_endpoint_page, link);
  982. list_del(&epp->link);
  983. }
  984. list_add_tail(&epp->link, &efx->local_page_list);
  985. peer = (struct vfdi_endpoint *)epp->ptr;
  986. peer_space = EFX_PAGE_SIZE / sizeof(struct vfdi_endpoint);
  987. }
  988. }
  989. vfdi_status->peer_count = peer_count;
  990. mutex_unlock(&efx->local_lock);
  991. /* Free any now unused endpoint pages */
  992. while (!list_empty(&pages)) {
  993. epp = list_first_entry(
  994. &pages, struct efx_endpoint_page, link);
  995. list_del(&epp->link);
  996. dma_free_coherent(&efx->pci_dev->dev, EFX_PAGE_SIZE,
  997. epp->ptr, epp->addr);
  998. kfree(epp);
  999. }
  1000. /* Finally, push the pages */
  1001. for (pos = 0; pos < efx->vf_count; ++pos) {
  1002. vf = efx->vf + pos;
  1003. mutex_lock(&vf->status_lock);
  1004. if (vf->status_addr)
  1005. __efx_sriov_push_vf_status(vf);
  1006. mutex_unlock(&vf->status_lock);
  1007. }
  1008. }
  1009. static void efx_sriov_free_local(struct efx_nic *efx)
  1010. {
  1011. struct efx_local_addr *local_addr;
  1012. struct efx_endpoint_page *epp;
  1013. while (!list_empty(&efx->local_addr_list)) {
  1014. local_addr = list_first_entry(&efx->local_addr_list,
  1015. struct efx_local_addr, link);
  1016. list_del(&local_addr->link);
  1017. kfree(local_addr);
  1018. }
  1019. while (!list_empty(&efx->local_page_list)) {
  1020. epp = list_first_entry(&efx->local_page_list,
  1021. struct efx_endpoint_page, link);
  1022. list_del(&epp->link);
  1023. dma_free_coherent(&efx->pci_dev->dev, EFX_PAGE_SIZE,
  1024. epp->ptr, epp->addr);
  1025. kfree(epp);
  1026. }
  1027. }
  1028. static int efx_sriov_vf_alloc(struct efx_nic *efx)
  1029. {
  1030. unsigned index;
  1031. struct efx_vf *vf;
  1032. efx->vf = kzalloc(sizeof(struct efx_vf) * efx->vf_count, GFP_KERNEL);
  1033. if (!efx->vf)
  1034. return -ENOMEM;
  1035. for (index = 0; index < efx->vf_count; ++index) {
  1036. vf = efx->vf + index;
  1037. vf->efx = efx;
  1038. vf->index = index;
  1039. vf->rx_filter_id = -1;
  1040. vf->tx_filter_mode = VF_TX_FILTER_AUTO;
  1041. vf->tx_filter_id = -1;
  1042. INIT_WORK(&vf->req, efx_sriov_vfdi);
  1043. INIT_WORK(&vf->reset_work, efx_sriov_reset_vf_work);
  1044. init_waitqueue_head(&vf->flush_waitq);
  1045. mutex_init(&vf->status_lock);
  1046. mutex_init(&vf->txq_lock);
  1047. }
  1048. return 0;
  1049. }
  1050. static void efx_sriov_vfs_fini(struct efx_nic *efx)
  1051. {
  1052. struct efx_vf *vf;
  1053. unsigned int pos;
  1054. for (pos = 0; pos < efx->vf_count; ++pos) {
  1055. vf = efx->vf + pos;
  1056. efx_nic_free_buffer(efx, &vf->buf);
  1057. kfree(vf->peer_page_addrs);
  1058. vf->peer_page_addrs = NULL;
  1059. vf->peer_page_count = 0;
  1060. vf->evq0_count = 0;
  1061. }
  1062. }
  1063. static int efx_sriov_vfs_init(struct efx_nic *efx)
  1064. {
  1065. struct pci_dev *pci_dev = efx->pci_dev;
  1066. unsigned index, devfn, sriov, buftbl_base;
  1067. u16 offset, stride;
  1068. struct efx_vf *vf;
  1069. int rc;
  1070. sriov = pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV);
  1071. if (!sriov)
  1072. return -ENOENT;
  1073. pci_read_config_word(pci_dev, sriov + PCI_SRIOV_VF_OFFSET, &offset);
  1074. pci_read_config_word(pci_dev, sriov + PCI_SRIOV_VF_STRIDE, &stride);
  1075. buftbl_base = efx->vf_buftbl_base;
  1076. devfn = pci_dev->devfn + offset;
  1077. for (index = 0; index < efx->vf_count; ++index) {
  1078. vf = efx->vf + index;
  1079. /* Reserve buffer entries */
  1080. vf->buftbl_base = buftbl_base;
  1081. buftbl_base += EFX_VF_BUFTBL_PER_VI * efx_vf_size(efx);
  1082. vf->pci_rid = devfn;
  1083. snprintf(vf->pci_name, sizeof(vf->pci_name),
  1084. "%04x:%02x:%02x.%d",
  1085. pci_domain_nr(pci_dev->bus), pci_dev->bus->number,
  1086. PCI_SLOT(devfn), PCI_FUNC(devfn));
  1087. rc = efx_nic_alloc_buffer(efx, &vf->buf, EFX_PAGE_SIZE);
  1088. if (rc)
  1089. goto fail;
  1090. devfn += stride;
  1091. }
  1092. return 0;
  1093. fail:
  1094. efx_sriov_vfs_fini(efx);
  1095. return rc;
  1096. }
  1097. int efx_sriov_init(struct efx_nic *efx)
  1098. {
  1099. struct net_device *net_dev = efx->net_dev;
  1100. struct vfdi_status *vfdi_status;
  1101. int rc;
  1102. /* Ensure there's room for vf_channel */
  1103. BUILD_BUG_ON(EFX_MAX_CHANNELS + 1 >= EFX_VI_BASE);
  1104. /* Ensure that VI_BASE is aligned on VI_SCALE */
  1105. BUILD_BUG_ON(EFX_VI_BASE & ((1 << EFX_VI_SCALE_MAX) - 1));
  1106. if (efx->vf_count == 0)
  1107. return 0;
  1108. rc = efx_sriov_cmd(efx, true, NULL, NULL);
  1109. if (rc)
  1110. goto fail_cmd;
  1111. rc = efx_nic_alloc_buffer(efx, &efx->vfdi_status, sizeof(*vfdi_status));
  1112. if (rc)
  1113. goto fail_status;
  1114. vfdi_status = efx->vfdi_status.addr;
  1115. memset(vfdi_status, 0, sizeof(*vfdi_status));
  1116. vfdi_status->version = 1;
  1117. vfdi_status->length = sizeof(*vfdi_status);
  1118. vfdi_status->max_tx_channels = vf_max_tx_channels;
  1119. vfdi_status->vi_scale = efx->vi_scale;
  1120. vfdi_status->rss_rxq_count = efx->rss_spread;
  1121. vfdi_status->peer_count = 1 + efx->vf_count;
  1122. vfdi_status->timer_quantum_ns = efx->timer_quantum_ns;
  1123. rc = efx_sriov_vf_alloc(efx);
  1124. if (rc)
  1125. goto fail_alloc;
  1126. mutex_init(&efx->local_lock);
  1127. INIT_WORK(&efx->peer_work, efx_sriov_peer_work);
  1128. INIT_LIST_HEAD(&efx->local_addr_list);
  1129. INIT_LIST_HEAD(&efx->local_page_list);
  1130. rc = efx_sriov_vfs_init(efx);
  1131. if (rc)
  1132. goto fail_vfs;
  1133. rtnl_lock();
  1134. memcpy(vfdi_status->peers[0].mac_addr,
  1135. net_dev->dev_addr, ETH_ALEN);
  1136. efx->vf_init_count = efx->vf_count;
  1137. rtnl_unlock();
  1138. efx_sriov_usrev(efx, true);
  1139. /* At this point we must be ready to accept VFDI requests */
  1140. rc = pci_enable_sriov(efx->pci_dev, efx->vf_count);
  1141. if (rc)
  1142. goto fail_pci;
  1143. netif_info(efx, probe, net_dev,
  1144. "enabled SR-IOV for %d VFs, %d VI per VF\n",
  1145. efx->vf_count, efx_vf_size(efx));
  1146. return 0;
  1147. fail_pci:
  1148. efx_sriov_usrev(efx, false);
  1149. rtnl_lock();
  1150. efx->vf_init_count = 0;
  1151. rtnl_unlock();
  1152. efx_sriov_vfs_fini(efx);
  1153. fail_vfs:
  1154. cancel_work_sync(&efx->peer_work);
  1155. efx_sriov_free_local(efx);
  1156. kfree(efx->vf);
  1157. fail_alloc:
  1158. efx_nic_free_buffer(efx, &efx->vfdi_status);
  1159. fail_status:
  1160. efx_sriov_cmd(efx, false, NULL, NULL);
  1161. fail_cmd:
  1162. return rc;
  1163. }
  1164. void efx_sriov_fini(struct efx_nic *efx)
  1165. {
  1166. struct efx_vf *vf;
  1167. unsigned int pos;
  1168. if (efx->vf_init_count == 0)
  1169. return;
  1170. /* Disable all interfaces to reconfiguration */
  1171. BUG_ON(efx->vfdi_channel->enabled);
  1172. efx_sriov_usrev(efx, false);
  1173. rtnl_lock();
  1174. efx->vf_init_count = 0;
  1175. rtnl_unlock();
  1176. /* Flush all reconfiguration work */
  1177. for (pos = 0; pos < efx->vf_count; ++pos) {
  1178. vf = efx->vf + pos;
  1179. cancel_work_sync(&vf->req);
  1180. cancel_work_sync(&vf->reset_work);
  1181. }
  1182. cancel_work_sync(&efx->peer_work);
  1183. pci_disable_sriov(efx->pci_dev);
  1184. /* Tear down back-end state */
  1185. efx_sriov_vfs_fini(efx);
  1186. efx_sriov_free_local(efx);
  1187. kfree(efx->vf);
  1188. efx_nic_free_buffer(efx, &efx->vfdi_status);
  1189. efx_sriov_cmd(efx, false, NULL, NULL);
  1190. }
  1191. void efx_sriov_event(struct efx_channel *channel, efx_qword_t *event)
  1192. {
  1193. struct efx_nic *efx = channel->efx;
  1194. struct efx_vf *vf;
  1195. unsigned qid, seq, type, data;
  1196. qid = EFX_QWORD_FIELD(*event, FSF_CZ_USER_QID);
  1197. /* USR_EV_REG_VALUE is dword0, so access the VFDI_EV fields directly */
  1198. BUILD_BUG_ON(FSF_CZ_USER_EV_REG_VALUE_LBN != 0);
  1199. seq = EFX_QWORD_FIELD(*event, VFDI_EV_SEQ);
  1200. type = EFX_QWORD_FIELD(*event, VFDI_EV_TYPE);
  1201. data = EFX_QWORD_FIELD(*event, VFDI_EV_DATA);
  1202. netif_vdbg(efx, hw, efx->net_dev,
  1203. "USR_EV event from qid %d seq 0x%x type %d data 0x%x\n",
  1204. qid, seq, type, data);
  1205. if (map_vi_index(efx, qid, &vf, NULL))
  1206. return;
  1207. if (vf->busy)
  1208. goto error;
  1209. if (type == VFDI_EV_TYPE_REQ_WORD0) {
  1210. /* Resynchronise */
  1211. vf->req_type = VFDI_EV_TYPE_REQ_WORD0;
  1212. vf->req_seqno = seq + 1;
  1213. vf->req_addr = 0;
  1214. } else if (seq != (vf->req_seqno++ & 0xff) || type != vf->req_type)
  1215. goto error;
  1216. switch (vf->req_type) {
  1217. case VFDI_EV_TYPE_REQ_WORD0:
  1218. case VFDI_EV_TYPE_REQ_WORD1:
  1219. case VFDI_EV_TYPE_REQ_WORD2:
  1220. vf->req_addr |= (u64)data << (vf->req_type << 4);
  1221. ++vf->req_type;
  1222. return;
  1223. case VFDI_EV_TYPE_REQ_WORD3:
  1224. vf->req_addr |= (u64)data << 48;
  1225. vf->req_type = VFDI_EV_TYPE_REQ_WORD0;
  1226. vf->busy = true;
  1227. queue_work(vfdi_workqueue, &vf->req);
  1228. return;
  1229. }
  1230. error:
  1231. if (net_ratelimit())
  1232. netif_err(efx, hw, efx->net_dev,
  1233. "ERROR: Screaming VFDI request from %s\n",
  1234. vf->pci_name);
  1235. /* Reset the request and sequence number */
  1236. vf->req_type = VFDI_EV_TYPE_REQ_WORD0;
  1237. vf->req_seqno = seq + 1;
  1238. }
  1239. void efx_sriov_flr(struct efx_nic *efx, unsigned vf_i)
  1240. {
  1241. struct efx_vf *vf;
  1242. if (vf_i > efx->vf_init_count)
  1243. return;
  1244. vf = efx->vf + vf_i;
  1245. netif_info(efx, hw, efx->net_dev,
  1246. "FLR on VF %s\n", vf->pci_name);
  1247. vf->status_addr = 0;
  1248. efx_vfdi_remove_all_filters(vf);
  1249. efx_vfdi_flush_clear(vf);
  1250. vf->evq0_count = 0;
  1251. }
  1252. void efx_sriov_mac_address_changed(struct efx_nic *efx)
  1253. {
  1254. struct vfdi_status *vfdi_status = efx->vfdi_status.addr;
  1255. if (!efx->vf_init_count)
  1256. return;
  1257. memcpy(vfdi_status->peers[0].mac_addr,
  1258. efx->net_dev->dev_addr, ETH_ALEN);
  1259. queue_work(vfdi_workqueue, &efx->peer_work);
  1260. }
  1261. void efx_sriov_tx_flush_done(struct efx_nic *efx, efx_qword_t *event)
  1262. {
  1263. struct efx_vf *vf;
  1264. unsigned queue, qid;
  1265. queue = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
  1266. if (map_vi_index(efx, queue, &vf, &qid))
  1267. return;
  1268. /* Ignore flush completions triggered by an FLR */
  1269. if (!test_bit(qid, vf->txq_mask))
  1270. return;
  1271. __clear_bit(qid, vf->txq_mask);
  1272. --vf->txq_count;
  1273. if (efx_vfdi_flush_wake(vf))
  1274. wake_up(&vf->flush_waitq);
  1275. }
  1276. void efx_sriov_rx_flush_done(struct efx_nic *efx, efx_qword_t *event)
  1277. {
  1278. struct efx_vf *vf;
  1279. unsigned ev_failed, queue, qid;
  1280. queue = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_DESCQ_ID);
  1281. ev_failed = EFX_QWORD_FIELD(*event,
  1282. FSF_AZ_DRIVER_EV_RX_FLUSH_FAIL);
  1283. if (map_vi_index(efx, queue, &vf, &qid))
  1284. return;
  1285. if (!test_bit(qid, vf->rxq_mask))
  1286. return;
  1287. if (ev_failed) {
  1288. set_bit(qid, vf->rxq_retry_mask);
  1289. atomic_inc(&vf->rxq_retry_count);
  1290. } else {
  1291. __clear_bit(qid, vf->rxq_mask);
  1292. --vf->rxq_count;
  1293. }
  1294. if (efx_vfdi_flush_wake(vf))
  1295. wake_up(&vf->flush_waitq);
  1296. }
  1297. /* Called from napi. Schedule the reset work item */
  1298. void efx_sriov_desc_fetch_err(struct efx_nic *efx, unsigned dmaq)
  1299. {
  1300. struct efx_vf *vf;
  1301. unsigned int rel;
  1302. if (map_vi_index(efx, dmaq, &vf, &rel))
  1303. return;
  1304. if (net_ratelimit())
  1305. netif_err(efx, hw, efx->net_dev,
  1306. "VF %d DMA Q %d reports descriptor fetch error.\n",
  1307. vf->index, rel);
  1308. queue_work(vfdi_workqueue, &vf->reset_work);
  1309. }
  1310. /* Reset all VFs */
  1311. void efx_sriov_reset(struct efx_nic *efx)
  1312. {
  1313. unsigned int vf_i;
  1314. struct efx_buffer buf;
  1315. struct efx_vf *vf;
  1316. ASSERT_RTNL();
  1317. if (efx->vf_init_count == 0)
  1318. return;
  1319. efx_sriov_usrev(efx, true);
  1320. (void)efx_sriov_cmd(efx, true, NULL, NULL);
  1321. if (efx_nic_alloc_buffer(efx, &buf, EFX_PAGE_SIZE))
  1322. return;
  1323. for (vf_i = 0; vf_i < efx->vf_init_count; ++vf_i) {
  1324. vf = efx->vf + vf_i;
  1325. efx_sriov_reset_vf(vf, &buf);
  1326. }
  1327. efx_nic_free_buffer(efx, &buf);
  1328. }
  1329. int efx_init_sriov(void)
  1330. {
  1331. /* A single threaded workqueue is sufficient. efx_sriov_vfdi() and
  1332. * efx_sriov_peer_work() spend almost all their time sleeping for
  1333. * MCDI to complete anyway
  1334. */
  1335. vfdi_workqueue = create_singlethread_workqueue("sfc_vfdi");
  1336. if (!vfdi_workqueue)
  1337. return -ENOMEM;
  1338. return 0;
  1339. }
  1340. void efx_fini_sriov(void)
  1341. {
  1342. destroy_workqueue(vfdi_workqueue);
  1343. }
  1344. int efx_sriov_set_vf_mac(struct net_device *net_dev, int vf_i, u8 *mac)
  1345. {
  1346. struct efx_nic *efx = netdev_priv(net_dev);
  1347. struct efx_vf *vf;
  1348. if (vf_i >= efx->vf_init_count)
  1349. return -EINVAL;
  1350. vf = efx->vf + vf_i;
  1351. mutex_lock(&vf->status_lock);
  1352. memcpy(vf->addr.mac_addr, mac, ETH_ALEN);
  1353. __efx_sriov_update_vf_addr(vf);
  1354. mutex_unlock(&vf->status_lock);
  1355. return 0;
  1356. }
  1357. int efx_sriov_set_vf_vlan(struct net_device *net_dev, int vf_i,
  1358. u16 vlan, u8 qos)
  1359. {
  1360. struct efx_nic *efx = netdev_priv(net_dev);
  1361. struct efx_vf *vf;
  1362. u16 tci;
  1363. if (vf_i >= efx->vf_init_count)
  1364. return -EINVAL;
  1365. vf = efx->vf + vf_i;
  1366. mutex_lock(&vf->status_lock);
  1367. tci = (vlan & VLAN_VID_MASK) | ((qos & 0x7) << VLAN_PRIO_SHIFT);
  1368. vf->addr.tci = htons(tci);
  1369. __efx_sriov_update_vf_addr(vf);
  1370. mutex_unlock(&vf->status_lock);
  1371. return 0;
  1372. }
  1373. int efx_sriov_set_vf_spoofchk(struct net_device *net_dev, int vf_i,
  1374. bool spoofchk)
  1375. {
  1376. struct efx_nic *efx = netdev_priv(net_dev);
  1377. struct efx_vf *vf;
  1378. int rc;
  1379. if (vf_i >= efx->vf_init_count)
  1380. return -EINVAL;
  1381. vf = efx->vf + vf_i;
  1382. mutex_lock(&vf->txq_lock);
  1383. if (vf->txq_count == 0) {
  1384. vf->tx_filter_mode =
  1385. spoofchk ? VF_TX_FILTER_ON : VF_TX_FILTER_OFF;
  1386. rc = 0;
  1387. } else {
  1388. /* This cannot be changed while TX queues are running */
  1389. rc = -EBUSY;
  1390. }
  1391. mutex_unlock(&vf->txq_lock);
  1392. return rc;
  1393. }
  1394. int efx_sriov_get_vf_config(struct net_device *net_dev, int vf_i,
  1395. struct ifla_vf_info *ivi)
  1396. {
  1397. struct efx_nic *efx = netdev_priv(net_dev);
  1398. struct efx_vf *vf;
  1399. u16 tci;
  1400. if (vf_i >= efx->vf_init_count)
  1401. return -EINVAL;
  1402. vf = efx->vf + vf_i;
  1403. ivi->vf = vf_i;
  1404. memcpy(ivi->mac, vf->addr.mac_addr, ETH_ALEN);
  1405. ivi->tx_rate = 0;
  1406. tci = ntohs(vf->addr.tci);
  1407. ivi->vlan = tci & VLAN_VID_MASK;
  1408. ivi->qos = (tci >> VLAN_PRIO_SHIFT) & 0x7;
  1409. ivi->spoofchk = vf->tx_filter_mode == VF_TX_FILTER_ON;
  1410. return 0;
  1411. }