siena_sriov.c 47 KB

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