vnic_main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
  2. /*
  3. * Copyright(c) 2017 - 2020 Intel Corporation.
  4. */
  5. /*
  6. * This file contains HFI1 support for VNIC functionality
  7. */
  8. #include <linux/io.h>
  9. #include <linux/if_vlan.h>
  10. #include "vnic.h"
  11. #include "netdev.h"
  12. #define HFI_TX_TIMEOUT_MS 1000
  13. #define HFI1_VNIC_RCV_Q_SIZE 1024
  14. #define HFI1_VNIC_UP 0
  15. static DEFINE_SPINLOCK(vport_cntr_lock);
  16. #define SUM_GRP_COUNTERS(stats, qstats, x_grp) do { \
  17. u64 *src64, *dst64; \
  18. for (src64 = &qstats->x_grp.unicast, \
  19. dst64 = &stats->x_grp.unicast; \
  20. dst64 <= &stats->x_grp.s_1519_max;) { \
  21. *dst64++ += *src64++; \
  22. } \
  23. } while (0)
  24. #define VNIC_MASK (0xFF)
  25. #define VNIC_ID(val) ((1ull << 24) | ((val) & VNIC_MASK))
  26. /* hfi1_vnic_update_stats - update statistics */
  27. static void hfi1_vnic_update_stats(struct hfi1_vnic_vport_info *vinfo,
  28. struct opa_vnic_stats *stats)
  29. {
  30. struct net_device *netdev = vinfo->netdev;
  31. u8 i;
  32. /* add tx counters on different queues */
  33. for (i = 0; i < vinfo->num_tx_q; i++) {
  34. struct opa_vnic_stats *qstats = &vinfo->stats[i];
  35. struct rtnl_link_stats64 *qnstats = &vinfo->stats[i].netstats;
  36. stats->netstats.tx_fifo_errors += qnstats->tx_fifo_errors;
  37. stats->netstats.tx_carrier_errors += qnstats->tx_carrier_errors;
  38. stats->tx_drop_state += qstats->tx_drop_state;
  39. stats->tx_dlid_zero += qstats->tx_dlid_zero;
  40. SUM_GRP_COUNTERS(stats, qstats, tx_grp);
  41. stats->netstats.tx_packets += qnstats->tx_packets;
  42. stats->netstats.tx_bytes += qnstats->tx_bytes;
  43. }
  44. /* add rx counters on different queues */
  45. for (i = 0; i < vinfo->num_rx_q; i++) {
  46. struct opa_vnic_stats *qstats = &vinfo->stats[i];
  47. struct rtnl_link_stats64 *qnstats = &vinfo->stats[i].netstats;
  48. stats->netstats.rx_fifo_errors += qnstats->rx_fifo_errors;
  49. stats->netstats.rx_nohandler += qnstats->rx_nohandler;
  50. stats->rx_drop_state += qstats->rx_drop_state;
  51. stats->rx_oversize += qstats->rx_oversize;
  52. stats->rx_runt += qstats->rx_runt;
  53. SUM_GRP_COUNTERS(stats, qstats, rx_grp);
  54. stats->netstats.rx_packets += qnstats->rx_packets;
  55. stats->netstats.rx_bytes += qnstats->rx_bytes;
  56. }
  57. stats->netstats.tx_errors = stats->netstats.tx_fifo_errors +
  58. stats->netstats.tx_carrier_errors +
  59. stats->tx_drop_state + stats->tx_dlid_zero;
  60. stats->netstats.tx_dropped = stats->netstats.tx_errors;
  61. stats->netstats.rx_errors = stats->netstats.rx_fifo_errors +
  62. stats->netstats.rx_nohandler +
  63. stats->rx_drop_state + stats->rx_oversize +
  64. stats->rx_runt;
  65. stats->netstats.rx_dropped = stats->netstats.rx_errors;
  66. netdev->stats.tx_packets = stats->netstats.tx_packets;
  67. netdev->stats.tx_bytes = stats->netstats.tx_bytes;
  68. netdev->stats.tx_fifo_errors = stats->netstats.tx_fifo_errors;
  69. netdev->stats.tx_carrier_errors = stats->netstats.tx_carrier_errors;
  70. netdev->stats.tx_errors = stats->netstats.tx_errors;
  71. netdev->stats.tx_dropped = stats->netstats.tx_dropped;
  72. netdev->stats.rx_packets = stats->netstats.rx_packets;
  73. netdev->stats.rx_bytes = stats->netstats.rx_bytes;
  74. netdev->stats.rx_fifo_errors = stats->netstats.rx_fifo_errors;
  75. netdev->stats.multicast = stats->rx_grp.mcastbcast;
  76. netdev->stats.rx_length_errors = stats->rx_oversize + stats->rx_runt;
  77. netdev->stats.rx_errors = stats->netstats.rx_errors;
  78. netdev->stats.rx_dropped = stats->netstats.rx_dropped;
  79. }
  80. /* update_len_counters - update pkt's len histogram counters */
  81. static inline void update_len_counters(struct opa_vnic_grp_stats *grp,
  82. int len)
  83. {
  84. /* account for 4 byte FCS */
  85. if (len >= 1515)
  86. grp->s_1519_max++;
  87. else if (len >= 1020)
  88. grp->s_1024_1518++;
  89. else if (len >= 508)
  90. grp->s_512_1023++;
  91. else if (len >= 252)
  92. grp->s_256_511++;
  93. else if (len >= 124)
  94. grp->s_128_255++;
  95. else if (len >= 61)
  96. grp->s_65_127++;
  97. else
  98. grp->s_64++;
  99. }
  100. /* hfi1_vnic_update_tx_counters - update transmit counters */
  101. static void hfi1_vnic_update_tx_counters(struct hfi1_vnic_vport_info *vinfo,
  102. u8 q_idx, struct sk_buff *skb, int err)
  103. {
  104. struct ethhdr *mac_hdr = (struct ethhdr *)skb_mac_header(skb);
  105. struct opa_vnic_stats *stats = &vinfo->stats[q_idx];
  106. struct opa_vnic_grp_stats *tx_grp = &stats->tx_grp;
  107. u16 vlan_tci;
  108. stats->netstats.tx_packets++;
  109. stats->netstats.tx_bytes += skb->len + ETH_FCS_LEN;
  110. update_len_counters(tx_grp, skb->len);
  111. /* rest of the counts are for good packets only */
  112. if (unlikely(err))
  113. return;
  114. if (is_multicast_ether_addr(mac_hdr->h_dest))
  115. tx_grp->mcastbcast++;
  116. else
  117. tx_grp->unicast++;
  118. if (!__vlan_get_tag(skb, &vlan_tci))
  119. tx_grp->vlan++;
  120. else
  121. tx_grp->untagged++;
  122. }
  123. /* hfi1_vnic_update_rx_counters - update receive counters */
  124. static void hfi1_vnic_update_rx_counters(struct hfi1_vnic_vport_info *vinfo,
  125. u8 q_idx, struct sk_buff *skb, int err)
  126. {
  127. struct ethhdr *mac_hdr = (struct ethhdr *)skb->data;
  128. struct opa_vnic_stats *stats = &vinfo->stats[q_idx];
  129. struct opa_vnic_grp_stats *rx_grp = &stats->rx_grp;
  130. u16 vlan_tci;
  131. stats->netstats.rx_packets++;
  132. stats->netstats.rx_bytes += skb->len + ETH_FCS_LEN;
  133. update_len_counters(rx_grp, skb->len);
  134. /* rest of the counts are for good packets only */
  135. if (unlikely(err))
  136. return;
  137. if (is_multicast_ether_addr(mac_hdr->h_dest))
  138. rx_grp->mcastbcast++;
  139. else
  140. rx_grp->unicast++;
  141. if (!__vlan_get_tag(skb, &vlan_tci))
  142. rx_grp->vlan++;
  143. else
  144. rx_grp->untagged++;
  145. }
  146. /* This function is overloaded for opa_vnic specific implementation */
  147. static void hfi1_vnic_get_stats64(struct net_device *netdev,
  148. struct rtnl_link_stats64 *stats)
  149. {
  150. struct opa_vnic_stats *vstats = (struct opa_vnic_stats *)stats;
  151. struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
  152. hfi1_vnic_update_stats(vinfo, vstats);
  153. }
  154. static u64 create_bypass_pbc(u32 vl, u32 dw_len)
  155. {
  156. u64 pbc;
  157. pbc = ((u64)PBC_IHCRC_NONE << PBC_INSERT_HCRC_SHIFT)
  158. | PBC_INSERT_BYPASS_ICRC | PBC_CREDIT_RETURN
  159. | PBC_PACKET_BYPASS
  160. | ((vl & PBC_VL_MASK) << PBC_VL_SHIFT)
  161. | (dw_len & PBC_LENGTH_DWS_MASK) << PBC_LENGTH_DWS_SHIFT;
  162. return pbc;
  163. }
  164. /* hfi1_vnic_maybe_stop_tx - stop tx queue if required */
  165. static void hfi1_vnic_maybe_stop_tx(struct hfi1_vnic_vport_info *vinfo,
  166. u8 q_idx)
  167. {
  168. netif_stop_subqueue(vinfo->netdev, q_idx);
  169. if (!hfi1_vnic_sdma_write_avail(vinfo, q_idx))
  170. return;
  171. netif_start_subqueue(vinfo->netdev, q_idx);
  172. }
  173. static netdev_tx_t hfi1_netdev_start_xmit(struct sk_buff *skb,
  174. struct net_device *netdev)
  175. {
  176. struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
  177. u8 pad_len, q_idx = skb->queue_mapping;
  178. struct hfi1_devdata *dd = vinfo->dd;
  179. struct opa_vnic_skb_mdata *mdata;
  180. u32 pkt_len, total_len;
  181. int err = -EINVAL;
  182. u64 pbc;
  183. v_dbg("xmit: queue %d skb len %d\n", q_idx, skb->len);
  184. if (unlikely(!netif_oper_up(netdev))) {
  185. vinfo->stats[q_idx].tx_drop_state++;
  186. goto tx_finish;
  187. }
  188. /* take out meta data */
  189. mdata = (struct opa_vnic_skb_mdata *)skb->data;
  190. skb_pull(skb, sizeof(*mdata));
  191. if (unlikely(mdata->flags & OPA_VNIC_SKB_MDATA_ENCAP_ERR)) {
  192. vinfo->stats[q_idx].tx_dlid_zero++;
  193. goto tx_finish;
  194. }
  195. /* add tail padding (for 8 bytes size alignment) and icrc */
  196. pad_len = -(skb->len + OPA_VNIC_ICRC_TAIL_LEN) & 0x7;
  197. pad_len += OPA_VNIC_ICRC_TAIL_LEN;
  198. /*
  199. * pkt_len is how much data we have to write, includes header and data.
  200. * total_len is length of the packet in Dwords plus the PBC should not
  201. * include the CRC.
  202. */
  203. pkt_len = (skb->len + pad_len) >> 2;
  204. total_len = pkt_len + 2; /* PBC + packet */
  205. pbc = create_bypass_pbc(mdata->vl, total_len);
  206. skb_get(skb);
  207. v_dbg("pbc 0x%016llX len %d pad_len %d\n", pbc, skb->len, pad_len);
  208. err = dd->process_vnic_dma_send(dd, q_idx, vinfo, skb, pbc, pad_len);
  209. if (unlikely(err)) {
  210. if (err == -ENOMEM)
  211. vinfo->stats[q_idx].netstats.tx_fifo_errors++;
  212. else if (err != -EBUSY)
  213. vinfo->stats[q_idx].netstats.tx_carrier_errors++;
  214. }
  215. /* remove the header before updating tx counters */
  216. skb_pull(skb, OPA_VNIC_HDR_LEN);
  217. if (unlikely(err == -EBUSY)) {
  218. hfi1_vnic_maybe_stop_tx(vinfo, q_idx);
  219. dev_kfree_skb_any(skb);
  220. return NETDEV_TX_BUSY;
  221. }
  222. tx_finish:
  223. /* update tx counters */
  224. hfi1_vnic_update_tx_counters(vinfo, q_idx, skb, err);
  225. dev_kfree_skb_any(skb);
  226. return NETDEV_TX_OK;
  227. }
  228. static u16 hfi1_vnic_select_queue(struct net_device *netdev,
  229. struct sk_buff *skb,
  230. struct net_device *sb_dev)
  231. {
  232. struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
  233. struct opa_vnic_skb_mdata *mdata;
  234. struct sdma_engine *sde;
  235. mdata = (struct opa_vnic_skb_mdata *)skb->data;
  236. sde = sdma_select_engine_vl(vinfo->dd, mdata->entropy, mdata->vl);
  237. return sde->this_idx;
  238. }
  239. /* hfi1_vnic_decap_skb - strip OPA header from the skb (ethernet) packet */
  240. static inline int hfi1_vnic_decap_skb(struct hfi1_vnic_rx_queue *rxq,
  241. struct sk_buff *skb)
  242. {
  243. struct hfi1_vnic_vport_info *vinfo = rxq->vinfo;
  244. int max_len = vinfo->netdev->mtu + VLAN_ETH_HLEN;
  245. int rc = -EFAULT;
  246. skb_pull(skb, OPA_VNIC_HDR_LEN);
  247. /* Validate Packet length */
  248. if (unlikely(skb->len > max_len))
  249. vinfo->stats[rxq->idx].rx_oversize++;
  250. else if (unlikely(skb->len < ETH_ZLEN))
  251. vinfo->stats[rxq->idx].rx_runt++;
  252. else
  253. rc = 0;
  254. return rc;
  255. }
  256. static struct hfi1_vnic_vport_info *get_vnic_port(struct hfi1_devdata *dd,
  257. int vesw_id)
  258. {
  259. int vnic_id = VNIC_ID(vesw_id);
  260. return hfi1_netdev_get_data(dd, vnic_id);
  261. }
  262. static struct hfi1_vnic_vport_info *get_first_vnic_port(struct hfi1_devdata *dd)
  263. {
  264. struct hfi1_vnic_vport_info *vinfo;
  265. int next_id = VNIC_ID(0);
  266. vinfo = hfi1_netdev_get_first_data(dd, &next_id);
  267. if (next_id > VNIC_ID(VNIC_MASK))
  268. return NULL;
  269. return vinfo;
  270. }
  271. void hfi1_vnic_bypass_rcv(struct hfi1_packet *packet)
  272. {
  273. struct hfi1_devdata *dd = packet->rcd->dd;
  274. struct hfi1_vnic_vport_info *vinfo = NULL;
  275. struct hfi1_vnic_rx_queue *rxq;
  276. struct sk_buff *skb;
  277. int l4_type, vesw_id = -1, rc;
  278. u8 q_idx;
  279. unsigned char *pad_info;
  280. l4_type = hfi1_16B_get_l4(packet->ebuf);
  281. if (likely(l4_type == OPA_16B_L4_ETHR)) {
  282. vesw_id = HFI1_VNIC_GET_VESWID(packet->ebuf);
  283. vinfo = get_vnic_port(dd, vesw_id);
  284. /*
  285. * In case of invalid vesw id, count the error on
  286. * the first available vport.
  287. */
  288. if (unlikely(!vinfo)) {
  289. struct hfi1_vnic_vport_info *vinfo_tmp;
  290. vinfo_tmp = get_first_vnic_port(dd);
  291. if (vinfo_tmp) {
  292. spin_lock(&vport_cntr_lock);
  293. vinfo_tmp->stats[0].netstats.rx_nohandler++;
  294. spin_unlock(&vport_cntr_lock);
  295. }
  296. }
  297. }
  298. if (unlikely(!vinfo)) {
  299. dd_dev_warn(dd, "vnic rcv err: l4 %d vesw id %d ctx %d\n",
  300. l4_type, vesw_id, packet->rcd->ctxt);
  301. return;
  302. }
  303. q_idx = packet->rcd->vnic_q_idx;
  304. rxq = &vinfo->rxq[q_idx];
  305. if (unlikely(!netif_oper_up(vinfo->netdev))) {
  306. vinfo->stats[q_idx].rx_drop_state++;
  307. return;
  308. }
  309. skb = netdev_alloc_skb(vinfo->netdev, packet->tlen);
  310. if (unlikely(!skb)) {
  311. vinfo->stats[q_idx].netstats.rx_fifo_errors++;
  312. return;
  313. }
  314. memcpy(skb->data, packet->ebuf, packet->tlen);
  315. skb_put(skb, packet->tlen);
  316. pad_info = skb->data + skb->len - 1;
  317. skb_trim(skb, (skb->len - OPA_VNIC_ICRC_TAIL_LEN -
  318. ((*pad_info) & 0x7)));
  319. rc = hfi1_vnic_decap_skb(rxq, skb);
  320. /* update rx counters */
  321. hfi1_vnic_update_rx_counters(vinfo, rxq->idx, skb, rc);
  322. if (unlikely(rc)) {
  323. dev_kfree_skb_any(skb);
  324. return;
  325. }
  326. skb_checksum_none_assert(skb);
  327. skb->protocol = eth_type_trans(skb, rxq->netdev);
  328. napi_gro_receive(&rxq->napi, skb);
  329. }
  330. static int hfi1_vnic_up(struct hfi1_vnic_vport_info *vinfo)
  331. {
  332. struct hfi1_devdata *dd = vinfo->dd;
  333. struct net_device *netdev = vinfo->netdev;
  334. int rc;
  335. /* ensure virtual eth switch id is valid */
  336. if (!vinfo->vesw_id)
  337. return -EINVAL;
  338. rc = hfi1_netdev_add_data(dd, VNIC_ID(vinfo->vesw_id), vinfo);
  339. if (rc < 0)
  340. return rc;
  341. rc = hfi1_netdev_rx_init(dd);
  342. if (rc)
  343. goto err_remove;
  344. netif_carrier_on(netdev);
  345. netif_tx_start_all_queues(netdev);
  346. set_bit(HFI1_VNIC_UP, &vinfo->flags);
  347. return 0;
  348. err_remove:
  349. hfi1_netdev_remove_data(dd, VNIC_ID(vinfo->vesw_id));
  350. return rc;
  351. }
  352. static void hfi1_vnic_down(struct hfi1_vnic_vport_info *vinfo)
  353. {
  354. struct hfi1_devdata *dd = vinfo->dd;
  355. clear_bit(HFI1_VNIC_UP, &vinfo->flags);
  356. netif_carrier_off(vinfo->netdev);
  357. netif_tx_disable(vinfo->netdev);
  358. hfi1_netdev_remove_data(dd, VNIC_ID(vinfo->vesw_id));
  359. hfi1_netdev_rx_destroy(dd);
  360. }
  361. static int hfi1_netdev_open(struct net_device *netdev)
  362. {
  363. struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
  364. int rc;
  365. mutex_lock(&vinfo->lock);
  366. rc = hfi1_vnic_up(vinfo);
  367. mutex_unlock(&vinfo->lock);
  368. return rc;
  369. }
  370. static int hfi1_netdev_close(struct net_device *netdev)
  371. {
  372. struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
  373. mutex_lock(&vinfo->lock);
  374. if (test_bit(HFI1_VNIC_UP, &vinfo->flags))
  375. hfi1_vnic_down(vinfo);
  376. mutex_unlock(&vinfo->lock);
  377. return 0;
  378. }
  379. static int hfi1_vnic_init(struct hfi1_vnic_vport_info *vinfo)
  380. {
  381. struct hfi1_devdata *dd = vinfo->dd;
  382. int rc = 0;
  383. mutex_lock(&hfi1_mutex);
  384. if (!dd->vnic_num_vports) {
  385. rc = hfi1_vnic_txreq_init(dd);
  386. if (rc)
  387. goto txreq_fail;
  388. }
  389. rc = hfi1_netdev_rx_init(dd);
  390. if (rc) {
  391. dd_dev_err(dd, "Unable to initialize netdev contexts\n");
  392. goto alloc_fail;
  393. }
  394. hfi1_init_vnic_rsm(dd);
  395. dd->vnic_num_vports++;
  396. hfi1_vnic_sdma_init(vinfo);
  397. alloc_fail:
  398. if (!dd->vnic_num_vports)
  399. hfi1_vnic_txreq_deinit(dd);
  400. txreq_fail:
  401. mutex_unlock(&hfi1_mutex);
  402. return rc;
  403. }
  404. static void hfi1_vnic_deinit(struct hfi1_vnic_vport_info *vinfo)
  405. {
  406. struct hfi1_devdata *dd = vinfo->dd;
  407. mutex_lock(&hfi1_mutex);
  408. if (--dd->vnic_num_vports == 0) {
  409. hfi1_deinit_vnic_rsm(dd);
  410. hfi1_vnic_txreq_deinit(dd);
  411. }
  412. mutex_unlock(&hfi1_mutex);
  413. hfi1_netdev_rx_destroy(dd);
  414. }
  415. static void hfi1_vnic_set_vesw_id(struct net_device *netdev, int id)
  416. {
  417. struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
  418. bool reopen = false;
  419. /*
  420. * If vesw_id is being changed, and if the vnic port is up,
  421. * reset the vnic port to ensure new vesw_id gets picked up
  422. */
  423. if (id != vinfo->vesw_id) {
  424. mutex_lock(&vinfo->lock);
  425. if (test_bit(HFI1_VNIC_UP, &vinfo->flags)) {
  426. hfi1_vnic_down(vinfo);
  427. reopen = true;
  428. }
  429. vinfo->vesw_id = id;
  430. if (reopen)
  431. hfi1_vnic_up(vinfo);
  432. mutex_unlock(&vinfo->lock);
  433. }
  434. }
  435. /* netdev ops */
  436. static const struct net_device_ops hfi1_netdev_ops = {
  437. .ndo_open = hfi1_netdev_open,
  438. .ndo_stop = hfi1_netdev_close,
  439. .ndo_start_xmit = hfi1_netdev_start_xmit,
  440. .ndo_select_queue = hfi1_vnic_select_queue,
  441. .ndo_get_stats64 = hfi1_vnic_get_stats64,
  442. };
  443. static void hfi1_vnic_free_rn(struct net_device *netdev)
  444. {
  445. struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
  446. hfi1_vnic_deinit(vinfo);
  447. mutex_destroy(&vinfo->lock);
  448. free_netdev(netdev);
  449. }
  450. struct net_device *hfi1_vnic_alloc_rn(struct ib_device *device,
  451. u32 port_num,
  452. enum rdma_netdev_t type,
  453. const char *name,
  454. unsigned char name_assign_type,
  455. void (*setup)(struct net_device *))
  456. {
  457. struct hfi1_devdata *dd = dd_from_ibdev(device);
  458. struct hfi1_vnic_vport_info *vinfo;
  459. struct net_device *netdev;
  460. struct rdma_netdev *rn;
  461. int i, size, rc;
  462. if (!dd->num_netdev_contexts)
  463. return ERR_PTR(-ENOMEM);
  464. if (!port_num || (port_num > dd->num_pports))
  465. return ERR_PTR(-EINVAL);
  466. if (type != RDMA_NETDEV_OPA_VNIC)
  467. return ERR_PTR(-EOPNOTSUPP);
  468. size = sizeof(struct opa_vnic_rdma_netdev) + sizeof(*vinfo);
  469. netdev = alloc_netdev_mqs(size, name, name_assign_type, setup,
  470. chip_sdma_engines(dd),
  471. dd->num_netdev_contexts);
  472. if (!netdev)
  473. return ERR_PTR(-ENOMEM);
  474. rn = netdev_priv(netdev);
  475. vinfo = opa_vnic_dev_priv(netdev);
  476. vinfo->dd = dd;
  477. vinfo->num_tx_q = chip_sdma_engines(dd);
  478. vinfo->num_rx_q = dd->num_netdev_contexts;
  479. vinfo->netdev = netdev;
  480. rn->free_rdma_netdev = hfi1_vnic_free_rn;
  481. rn->set_id = hfi1_vnic_set_vesw_id;
  482. netdev->features = NETIF_F_HIGHDMA | NETIF_F_SG;
  483. netdev->hw_features = netdev->features;
  484. netdev->vlan_features = netdev->features;
  485. netdev->watchdog_timeo = msecs_to_jiffies(HFI_TX_TIMEOUT_MS);
  486. netdev->netdev_ops = &hfi1_netdev_ops;
  487. mutex_init(&vinfo->lock);
  488. for (i = 0; i < vinfo->num_rx_q; i++) {
  489. struct hfi1_vnic_rx_queue *rxq = &vinfo->rxq[i];
  490. rxq->idx = i;
  491. rxq->vinfo = vinfo;
  492. rxq->netdev = netdev;
  493. }
  494. rc = hfi1_vnic_init(vinfo);
  495. if (rc)
  496. goto init_fail;
  497. return netdev;
  498. init_fail:
  499. mutex_destroy(&vinfo->lock);
  500. free_netdev(netdev);
  501. return ERR_PTR(rc);
  502. }