siena_sriov.c 46 KB

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