siena_sriov.c 47 KB

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