siena_sriov.c 47 KB

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