siena_sriov.c 45 KB

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