Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2017 Intel Corporation. |
| 3 | * |
| 4 | * This file is provided under a dual BSD/GPLv2 license. When using or |
| 5 | * redistributing this file, you may do so under either license. |
| 6 | * |
| 7 | * GPL LICENSE SUMMARY |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of version 2 of the GNU General Public License as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * BSD LICENSE |
| 19 | * |
| 20 | * Redistribution and use in source and binary forms, with or without |
| 21 | * modification, are permitted provided that the following conditions |
| 22 | * are met: |
| 23 | * |
| 24 | * - Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * - Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in |
| 28 | * the documentation and/or other materials provided with the |
| 29 | * distribution. |
| 30 | * - Neither the name of Intel Corporation nor the names of its |
| 31 | * contributors may be used to endorse or promote products derived |
| 32 | * from this software without specific prior written permission. |
| 33 | * |
| 34 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 35 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 36 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 37 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 38 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 39 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 40 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 41 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 42 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 43 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 44 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 45 | * |
| 46 | */ |
| 47 | |
| 48 | /* |
| 49 | * This file contains HFI1 support for VNIC functionality |
| 50 | */ |
| 51 | |
| 52 | #include <linux/io.h> |
| 53 | #include <linux/if_vlan.h> |
| 54 | |
| 55 | #include "vnic.h" |
| 56 | |
| 57 | #define HFI_TX_TIMEOUT_MS 1000 |
| 58 | |
| 59 | #define HFI1_VNIC_RCV_Q_SIZE 1024 |
| 60 | |
| 61 | #define HFI1_VNIC_UP 0 |
| 62 | |
| 63 | static DEFINE_SPINLOCK(vport_cntr_lock); |
| 64 | |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame^] | 65 | static int setup_vnic_ctxt(struct hfi1_devdata *dd, struct hfi1_ctxtdata *uctxt) |
| 66 | { |
| 67 | unsigned int rcvctrl_ops = 0; |
| 68 | int ret; |
| 69 | |
| 70 | ret = hfi1_init_ctxt(uctxt->sc); |
| 71 | if (ret) |
| 72 | goto done; |
| 73 | |
| 74 | uctxt->do_interrupt = &handle_receive_interrupt; |
| 75 | |
| 76 | /* Now allocate the RcvHdr queue and eager buffers. */ |
| 77 | ret = hfi1_create_rcvhdrq(dd, uctxt); |
| 78 | if (ret) |
| 79 | goto done; |
| 80 | |
| 81 | ret = hfi1_setup_eagerbufs(uctxt); |
| 82 | if (ret) |
| 83 | goto done; |
| 84 | |
| 85 | set_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags); |
| 86 | |
| 87 | if (uctxt->rcvhdrtail_kvaddr) |
| 88 | clear_rcvhdrtail(uctxt); |
| 89 | |
| 90 | rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB; |
| 91 | rcvctrl_ops |= HFI1_RCVCTRL_INTRAVAIL_ENB; |
| 92 | |
| 93 | if (!HFI1_CAP_KGET_MASK(uctxt->flags, MULTI_PKT_EGR)) |
| 94 | rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB; |
| 95 | if (HFI1_CAP_KGET_MASK(uctxt->flags, NODROP_EGR_FULL)) |
| 96 | rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB; |
| 97 | if (HFI1_CAP_KGET_MASK(uctxt->flags, NODROP_RHQ_FULL)) |
| 98 | rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB; |
| 99 | if (HFI1_CAP_KGET_MASK(uctxt->flags, DMA_RTAIL)) |
| 100 | rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB; |
| 101 | |
| 102 | hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt->ctxt); |
| 103 | |
| 104 | uctxt->is_vnic = true; |
| 105 | done: |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static int allocate_vnic_ctxt(struct hfi1_devdata *dd, |
| 110 | struct hfi1_ctxtdata **vnic_ctxt) |
| 111 | { |
| 112 | struct hfi1_ctxtdata *uctxt; |
| 113 | unsigned int ctxt; |
| 114 | int ret; |
| 115 | |
| 116 | if (dd->flags & HFI1_FROZEN) |
| 117 | return -EIO; |
| 118 | |
| 119 | for (ctxt = dd->first_dyn_alloc_ctxt; |
| 120 | ctxt < dd->num_rcv_contexts; ctxt++) |
| 121 | if (!dd->rcd[ctxt]) |
| 122 | break; |
| 123 | |
| 124 | if (ctxt == dd->num_rcv_contexts) |
| 125 | return -EBUSY; |
| 126 | |
| 127 | uctxt = hfi1_create_ctxtdata(dd->pport, ctxt, dd->node); |
| 128 | if (!uctxt) { |
| 129 | dd_dev_err(dd, "Unable to create ctxtdata, failing open\n"); |
| 130 | return -ENOMEM; |
| 131 | } |
| 132 | |
| 133 | uctxt->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) | |
| 134 | HFI1_CAP_KGET(NODROP_RHQ_FULL) | |
| 135 | HFI1_CAP_KGET(NODROP_EGR_FULL) | |
| 136 | HFI1_CAP_KGET(DMA_RTAIL); |
| 137 | uctxt->seq_cnt = 1; |
| 138 | |
| 139 | /* Allocate and enable a PIO send context */ |
| 140 | uctxt->sc = sc_alloc(dd, SC_VNIC, uctxt->rcvhdrqentsize, |
| 141 | uctxt->numa_id); |
| 142 | |
| 143 | ret = uctxt->sc ? 0 : -ENOMEM; |
| 144 | if (ret) |
| 145 | goto bail; |
| 146 | |
| 147 | dd_dev_dbg(dd, "allocated vnic send context %u(%u)\n", |
| 148 | uctxt->sc->sw_index, uctxt->sc->hw_context); |
| 149 | ret = sc_enable(uctxt->sc); |
| 150 | if (ret) |
| 151 | goto bail; |
| 152 | |
| 153 | if (dd->num_msix_entries) |
| 154 | hfi1_set_vnic_msix_info(uctxt); |
| 155 | |
| 156 | hfi1_stats.sps_ctxts++; |
| 157 | dd_dev_dbg(dd, "created vnic context %d\n", uctxt->ctxt); |
| 158 | *vnic_ctxt = uctxt; |
| 159 | |
| 160 | return ret; |
| 161 | bail: |
| 162 | /* |
| 163 | * hfi1_free_ctxtdata() also releases send_context |
| 164 | * structure if uctxt->sc is not null |
| 165 | */ |
| 166 | dd->rcd[uctxt->ctxt] = NULL; |
| 167 | hfi1_free_ctxtdata(dd, uctxt); |
| 168 | dd_dev_dbg(dd, "vnic allocation failed. rc %d\n", ret); |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static void deallocate_vnic_ctxt(struct hfi1_devdata *dd, |
| 173 | struct hfi1_ctxtdata *uctxt) |
| 174 | { |
| 175 | unsigned long flags; |
| 176 | |
| 177 | dd_dev_dbg(dd, "closing vnic context %d\n", uctxt->ctxt); |
| 178 | flush_wc(); |
| 179 | |
| 180 | if (dd->num_msix_entries) |
| 181 | hfi1_reset_vnic_msix_info(uctxt); |
| 182 | |
| 183 | spin_lock_irqsave(&dd->uctxt_lock, flags); |
| 184 | /* |
| 185 | * Disable receive context and interrupt available, reset all |
| 186 | * RcvCtxtCtrl bits to default values. |
| 187 | */ |
| 188 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS | |
| 189 | HFI1_RCVCTRL_TIDFLOW_DIS | |
| 190 | HFI1_RCVCTRL_INTRAVAIL_DIS | |
| 191 | HFI1_RCVCTRL_ONE_PKT_EGR_DIS | |
| 192 | HFI1_RCVCTRL_NO_RHQ_DROP_DIS | |
| 193 | HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt->ctxt); |
| 194 | /* |
| 195 | * VNIC contexts are allocated from user context pool. |
| 196 | * Release them back to user context pool. |
| 197 | * |
| 198 | * Reset context integrity checks to default. |
| 199 | * (writes to CSRs probably belong in chip.c) |
| 200 | */ |
| 201 | write_kctxt_csr(dd, uctxt->sc->hw_context, SEND_CTXT_CHECK_ENABLE, |
| 202 | hfi1_pkt_default_send_ctxt_mask(dd, SC_USER)); |
| 203 | sc_disable(uctxt->sc); |
| 204 | |
| 205 | dd->send_contexts[uctxt->sc->sw_index].type = SC_USER; |
| 206 | spin_unlock_irqrestore(&dd->uctxt_lock, flags); |
| 207 | |
| 208 | dd->rcd[uctxt->ctxt] = NULL; |
| 209 | uctxt->event_flags = 0; |
| 210 | |
| 211 | hfi1_clear_tids(uctxt); |
| 212 | hfi1_clear_ctxt_pkey(dd, uctxt->ctxt); |
| 213 | |
| 214 | hfi1_stats.sps_ctxts--; |
| 215 | hfi1_free_ctxtdata(dd, uctxt); |
| 216 | } |
| 217 | |
Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 218 | void hfi1_vnic_setup(struct hfi1_devdata *dd) |
| 219 | { |
| 220 | idr_init(&dd->vnic.vesw_idr); |
| 221 | } |
| 222 | |
| 223 | void hfi1_vnic_cleanup(struct hfi1_devdata *dd) |
| 224 | { |
| 225 | idr_destroy(&dd->vnic.vesw_idr); |
| 226 | } |
| 227 | |
| 228 | #define SUM_GRP_COUNTERS(stats, qstats, x_grp) do { \ |
| 229 | u64 *src64, *dst64; \ |
| 230 | for (src64 = &qstats->x_grp.unicast, \ |
| 231 | dst64 = &stats->x_grp.unicast; \ |
| 232 | dst64 <= &stats->x_grp.s_1519_max;) { \ |
| 233 | *dst64++ += *src64++; \ |
| 234 | } \ |
| 235 | } while (0) |
| 236 | |
| 237 | /* hfi1_vnic_update_stats - update statistics */ |
| 238 | static void hfi1_vnic_update_stats(struct hfi1_vnic_vport_info *vinfo, |
| 239 | struct opa_vnic_stats *stats) |
| 240 | { |
| 241 | struct net_device *netdev = vinfo->netdev; |
| 242 | u8 i; |
| 243 | |
| 244 | /* add tx counters on different queues */ |
| 245 | for (i = 0; i < vinfo->num_tx_q; i++) { |
| 246 | struct opa_vnic_stats *qstats = &vinfo->stats[i]; |
| 247 | struct rtnl_link_stats64 *qnstats = &vinfo->stats[i].netstats; |
| 248 | |
| 249 | stats->netstats.tx_fifo_errors += qnstats->tx_fifo_errors; |
| 250 | stats->netstats.tx_carrier_errors += qnstats->tx_carrier_errors; |
| 251 | stats->tx_drop_state += qstats->tx_drop_state; |
| 252 | stats->tx_dlid_zero += qstats->tx_dlid_zero; |
| 253 | |
| 254 | SUM_GRP_COUNTERS(stats, qstats, tx_grp); |
| 255 | stats->netstats.tx_packets += qnstats->tx_packets; |
| 256 | stats->netstats.tx_bytes += qnstats->tx_bytes; |
| 257 | } |
| 258 | |
| 259 | /* add rx counters on different queues */ |
| 260 | for (i = 0; i < vinfo->num_rx_q; i++) { |
| 261 | struct opa_vnic_stats *qstats = &vinfo->stats[i]; |
| 262 | struct rtnl_link_stats64 *qnstats = &vinfo->stats[i].netstats; |
| 263 | |
| 264 | stats->netstats.rx_fifo_errors += qnstats->rx_fifo_errors; |
| 265 | stats->netstats.rx_nohandler += qnstats->rx_nohandler; |
| 266 | stats->rx_drop_state += qstats->rx_drop_state; |
| 267 | stats->rx_oversize += qstats->rx_oversize; |
| 268 | stats->rx_runt += qstats->rx_runt; |
| 269 | |
| 270 | SUM_GRP_COUNTERS(stats, qstats, rx_grp); |
| 271 | stats->netstats.rx_packets += qnstats->rx_packets; |
| 272 | stats->netstats.rx_bytes += qnstats->rx_bytes; |
| 273 | } |
| 274 | |
| 275 | stats->netstats.tx_errors = stats->netstats.tx_fifo_errors + |
| 276 | stats->netstats.tx_carrier_errors + |
| 277 | stats->tx_drop_state + stats->tx_dlid_zero; |
| 278 | stats->netstats.tx_dropped = stats->netstats.tx_errors; |
| 279 | |
| 280 | stats->netstats.rx_errors = stats->netstats.rx_fifo_errors + |
| 281 | stats->netstats.rx_nohandler + |
| 282 | stats->rx_drop_state + stats->rx_oversize + |
| 283 | stats->rx_runt; |
| 284 | stats->netstats.rx_dropped = stats->netstats.rx_errors; |
| 285 | |
| 286 | netdev->stats.tx_packets = stats->netstats.tx_packets; |
| 287 | netdev->stats.tx_bytes = stats->netstats.tx_bytes; |
| 288 | netdev->stats.tx_fifo_errors = stats->netstats.tx_fifo_errors; |
| 289 | netdev->stats.tx_carrier_errors = stats->netstats.tx_carrier_errors; |
| 290 | netdev->stats.tx_errors = stats->netstats.tx_errors; |
| 291 | netdev->stats.tx_dropped = stats->netstats.tx_dropped; |
| 292 | |
| 293 | netdev->stats.rx_packets = stats->netstats.rx_packets; |
| 294 | netdev->stats.rx_bytes = stats->netstats.rx_bytes; |
| 295 | netdev->stats.rx_fifo_errors = stats->netstats.rx_fifo_errors; |
| 296 | netdev->stats.multicast = stats->rx_grp.mcastbcast; |
| 297 | netdev->stats.rx_length_errors = stats->rx_oversize + stats->rx_runt; |
| 298 | netdev->stats.rx_errors = stats->netstats.rx_errors; |
| 299 | netdev->stats.rx_dropped = stats->netstats.rx_dropped; |
| 300 | } |
| 301 | |
| 302 | /* update_len_counters - update pkt's len histogram counters */ |
| 303 | static inline void update_len_counters(struct opa_vnic_grp_stats *grp, |
| 304 | int len) |
| 305 | { |
| 306 | /* account for 4 byte FCS */ |
| 307 | if (len >= 1515) |
| 308 | grp->s_1519_max++; |
| 309 | else if (len >= 1020) |
| 310 | grp->s_1024_1518++; |
| 311 | else if (len >= 508) |
| 312 | grp->s_512_1023++; |
| 313 | else if (len >= 252) |
| 314 | grp->s_256_511++; |
| 315 | else if (len >= 124) |
| 316 | grp->s_128_255++; |
| 317 | else if (len >= 61) |
| 318 | grp->s_65_127++; |
| 319 | else |
| 320 | grp->s_64++; |
| 321 | } |
| 322 | |
| 323 | /* hfi1_vnic_update_tx_counters - update transmit counters */ |
| 324 | static void hfi1_vnic_update_tx_counters(struct hfi1_vnic_vport_info *vinfo, |
| 325 | u8 q_idx, struct sk_buff *skb, int err) |
| 326 | { |
| 327 | struct ethhdr *mac_hdr = (struct ethhdr *)skb_mac_header(skb); |
| 328 | struct opa_vnic_stats *stats = &vinfo->stats[q_idx]; |
| 329 | struct opa_vnic_grp_stats *tx_grp = &stats->tx_grp; |
| 330 | u16 vlan_tci; |
| 331 | |
| 332 | stats->netstats.tx_packets++; |
| 333 | stats->netstats.tx_bytes += skb->len + ETH_FCS_LEN; |
| 334 | |
| 335 | update_len_counters(tx_grp, skb->len); |
| 336 | |
| 337 | /* rest of the counts are for good packets only */ |
| 338 | if (unlikely(err)) |
| 339 | return; |
| 340 | |
| 341 | if (is_multicast_ether_addr(mac_hdr->h_dest)) |
| 342 | tx_grp->mcastbcast++; |
| 343 | else |
| 344 | tx_grp->unicast++; |
| 345 | |
| 346 | if (!__vlan_get_tag(skb, &vlan_tci)) |
| 347 | tx_grp->vlan++; |
| 348 | else |
| 349 | tx_grp->untagged++; |
| 350 | } |
| 351 | |
| 352 | /* hfi1_vnic_update_rx_counters - update receive counters */ |
| 353 | static void hfi1_vnic_update_rx_counters(struct hfi1_vnic_vport_info *vinfo, |
| 354 | u8 q_idx, struct sk_buff *skb, int err) |
| 355 | { |
| 356 | struct ethhdr *mac_hdr = (struct ethhdr *)skb->data; |
| 357 | struct opa_vnic_stats *stats = &vinfo->stats[q_idx]; |
| 358 | struct opa_vnic_grp_stats *rx_grp = &stats->rx_grp; |
| 359 | u16 vlan_tci; |
| 360 | |
| 361 | stats->netstats.rx_packets++; |
| 362 | stats->netstats.rx_bytes += skb->len + ETH_FCS_LEN; |
| 363 | |
| 364 | update_len_counters(rx_grp, skb->len); |
| 365 | |
| 366 | /* rest of the counts are for good packets only */ |
| 367 | if (unlikely(err)) |
| 368 | return; |
| 369 | |
| 370 | if (is_multicast_ether_addr(mac_hdr->h_dest)) |
| 371 | rx_grp->mcastbcast++; |
| 372 | else |
| 373 | rx_grp->unicast++; |
| 374 | |
| 375 | if (!__vlan_get_tag(skb, &vlan_tci)) |
| 376 | rx_grp->vlan++; |
| 377 | else |
| 378 | rx_grp->untagged++; |
| 379 | } |
| 380 | |
| 381 | /* This function is overloaded for opa_vnic specific implementation */ |
| 382 | static void hfi1_vnic_get_stats64(struct net_device *netdev, |
| 383 | struct rtnl_link_stats64 *stats) |
| 384 | { |
| 385 | struct opa_vnic_stats *vstats = (struct opa_vnic_stats *)stats; |
| 386 | struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev); |
| 387 | |
| 388 | hfi1_vnic_update_stats(vinfo, vstats); |
| 389 | } |
| 390 | |
| 391 | static u64 create_bypass_pbc(u32 vl, u32 dw_len) |
| 392 | { |
| 393 | u64 pbc; |
| 394 | |
| 395 | pbc = ((u64)PBC_IHCRC_NONE << PBC_INSERT_HCRC_SHIFT) |
| 396 | | PBC_INSERT_BYPASS_ICRC | PBC_CREDIT_RETURN |
| 397 | | PBC_PACKET_BYPASS |
| 398 | | ((vl & PBC_VL_MASK) << PBC_VL_SHIFT) |
| 399 | | (dw_len & PBC_LENGTH_DWS_MASK) << PBC_LENGTH_DWS_SHIFT; |
| 400 | |
| 401 | return pbc; |
| 402 | } |
| 403 | |
| 404 | /* hfi1_vnic_maybe_stop_tx - stop tx queue if required */ |
| 405 | static void hfi1_vnic_maybe_stop_tx(struct hfi1_vnic_vport_info *vinfo, |
| 406 | u8 q_idx) |
| 407 | { |
| 408 | netif_stop_subqueue(vinfo->netdev, q_idx); |
| 409 | } |
| 410 | |
| 411 | static netdev_tx_t hfi1_netdev_start_xmit(struct sk_buff *skb, |
| 412 | struct net_device *netdev) |
| 413 | { |
| 414 | struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev); |
| 415 | u8 pad_len, q_idx = skb->queue_mapping; |
| 416 | struct hfi1_devdata *dd = vinfo->dd; |
| 417 | struct opa_vnic_skb_mdata *mdata; |
| 418 | u32 pkt_len, total_len; |
| 419 | int err = -EINVAL; |
| 420 | u64 pbc; |
| 421 | |
| 422 | v_dbg("xmit: queue %d skb len %d\n", q_idx, skb->len); |
| 423 | if (unlikely(!netif_oper_up(netdev))) { |
| 424 | vinfo->stats[q_idx].tx_drop_state++; |
| 425 | goto tx_finish; |
| 426 | } |
| 427 | |
| 428 | /* take out meta data */ |
| 429 | mdata = (struct opa_vnic_skb_mdata *)skb->data; |
| 430 | skb_pull(skb, sizeof(*mdata)); |
| 431 | if (unlikely(mdata->flags & OPA_VNIC_SKB_MDATA_ENCAP_ERR)) { |
| 432 | vinfo->stats[q_idx].tx_dlid_zero++; |
| 433 | goto tx_finish; |
| 434 | } |
| 435 | |
| 436 | /* add tail padding (for 8 bytes size alignment) and icrc */ |
| 437 | pad_len = -(skb->len + OPA_VNIC_ICRC_TAIL_LEN) & 0x7; |
| 438 | pad_len += OPA_VNIC_ICRC_TAIL_LEN; |
| 439 | |
| 440 | /* |
| 441 | * pkt_len is how much data we have to write, includes header and data. |
| 442 | * total_len is length of the packet in Dwords plus the PBC should not |
| 443 | * include the CRC. |
| 444 | */ |
| 445 | pkt_len = (skb->len + pad_len) >> 2; |
| 446 | total_len = pkt_len + 2; /* PBC + packet */ |
| 447 | |
| 448 | pbc = create_bypass_pbc(mdata->vl, total_len); |
| 449 | |
| 450 | skb_get(skb); |
| 451 | v_dbg("pbc 0x%016llX len %d pad_len %d\n", pbc, skb->len, pad_len); |
| 452 | err = dd->process_vnic_dma_send(dd, q_idx, vinfo, skb, pbc, pad_len); |
| 453 | if (unlikely(err)) { |
| 454 | if (err == -ENOMEM) |
| 455 | vinfo->stats[q_idx].netstats.tx_fifo_errors++; |
| 456 | else if (err != -EBUSY) |
| 457 | vinfo->stats[q_idx].netstats.tx_carrier_errors++; |
| 458 | } |
| 459 | /* remove the header before updating tx counters */ |
| 460 | skb_pull(skb, OPA_VNIC_HDR_LEN); |
| 461 | |
| 462 | if (unlikely(err == -EBUSY)) { |
| 463 | hfi1_vnic_maybe_stop_tx(vinfo, q_idx); |
| 464 | dev_kfree_skb_any(skb); |
| 465 | return NETDEV_TX_BUSY; |
| 466 | } |
| 467 | |
| 468 | tx_finish: |
| 469 | /* update tx counters */ |
| 470 | hfi1_vnic_update_tx_counters(vinfo, q_idx, skb, err); |
| 471 | dev_kfree_skb_any(skb); |
| 472 | return NETDEV_TX_OK; |
| 473 | } |
| 474 | |
| 475 | static u16 hfi1_vnic_select_queue(struct net_device *netdev, |
| 476 | struct sk_buff *skb, |
| 477 | void *accel_priv, |
| 478 | select_queue_fallback_t fallback) |
| 479 | { |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /* hfi1_vnic_decap_skb - strip OPA header from the skb (ethernet) packet */ |
| 484 | static inline int hfi1_vnic_decap_skb(struct hfi1_vnic_rx_queue *rxq, |
| 485 | struct sk_buff *skb) |
| 486 | { |
| 487 | struct hfi1_vnic_vport_info *vinfo = rxq->vinfo; |
| 488 | int max_len = vinfo->netdev->mtu + VLAN_ETH_HLEN; |
| 489 | int rc = -EFAULT; |
| 490 | |
| 491 | skb_pull(skb, OPA_VNIC_HDR_LEN); |
| 492 | |
| 493 | /* Validate Packet length */ |
| 494 | if (unlikely(skb->len > max_len)) |
| 495 | vinfo->stats[rxq->idx].rx_oversize++; |
| 496 | else if (unlikely(skb->len < ETH_ZLEN)) |
| 497 | vinfo->stats[rxq->idx].rx_runt++; |
| 498 | else |
| 499 | rc = 0; |
| 500 | return rc; |
| 501 | } |
| 502 | |
| 503 | static inline struct sk_buff *hfi1_vnic_get_skb(struct hfi1_vnic_rx_queue *rxq) |
| 504 | { |
| 505 | unsigned char *pad_info; |
| 506 | struct sk_buff *skb; |
| 507 | |
| 508 | skb = skb_dequeue(&rxq->skbq); |
| 509 | if (unlikely(!skb)) |
| 510 | return NULL; |
| 511 | |
| 512 | /* remove tail padding and icrc */ |
| 513 | pad_info = skb->data + skb->len - 1; |
| 514 | skb_trim(skb, (skb->len - OPA_VNIC_ICRC_TAIL_LEN - |
| 515 | ((*pad_info) & 0x7))); |
| 516 | |
| 517 | return skb; |
| 518 | } |
| 519 | |
| 520 | /* hfi1_vnic_handle_rx - handle skb receive */ |
| 521 | static void hfi1_vnic_handle_rx(struct hfi1_vnic_rx_queue *rxq, |
| 522 | int *work_done, int work_to_do) |
| 523 | { |
| 524 | struct hfi1_vnic_vport_info *vinfo = rxq->vinfo; |
| 525 | struct sk_buff *skb; |
| 526 | int rc; |
| 527 | |
| 528 | while (1) { |
| 529 | if (*work_done >= work_to_do) |
| 530 | break; |
| 531 | |
| 532 | skb = hfi1_vnic_get_skb(rxq); |
| 533 | if (unlikely(!skb)) |
| 534 | break; |
| 535 | |
| 536 | rc = hfi1_vnic_decap_skb(rxq, skb); |
| 537 | /* update rx counters */ |
| 538 | hfi1_vnic_update_rx_counters(vinfo, rxq->idx, skb, rc); |
| 539 | if (unlikely(rc)) { |
| 540 | dev_kfree_skb_any(skb); |
| 541 | continue; |
| 542 | } |
| 543 | |
| 544 | skb_checksum_none_assert(skb); |
| 545 | skb->protocol = eth_type_trans(skb, rxq->netdev); |
| 546 | |
| 547 | napi_gro_receive(&rxq->napi, skb); |
| 548 | (*work_done)++; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /* hfi1_vnic_napi - napi receive polling callback function */ |
| 553 | static int hfi1_vnic_napi(struct napi_struct *napi, int budget) |
| 554 | { |
| 555 | struct hfi1_vnic_rx_queue *rxq = container_of(napi, |
| 556 | struct hfi1_vnic_rx_queue, napi); |
| 557 | struct hfi1_vnic_vport_info *vinfo = rxq->vinfo; |
| 558 | int work_done = 0; |
| 559 | |
| 560 | v_dbg("napi %d budget %d\n", rxq->idx, budget); |
| 561 | hfi1_vnic_handle_rx(rxq, &work_done, budget); |
| 562 | |
| 563 | v_dbg("napi %d work_done %d\n", rxq->idx, work_done); |
| 564 | if (work_done < budget) |
| 565 | napi_complete(napi); |
| 566 | |
| 567 | return work_done; |
| 568 | } |
| 569 | |
| 570 | void hfi1_vnic_bypass_rcv(struct hfi1_packet *packet) |
| 571 | { |
| 572 | struct hfi1_devdata *dd = packet->rcd->dd; |
| 573 | struct hfi1_vnic_vport_info *vinfo = NULL; |
| 574 | struct hfi1_vnic_rx_queue *rxq; |
| 575 | struct sk_buff *skb; |
| 576 | int l4_type, vesw_id = -1; |
| 577 | u8 q_idx; |
| 578 | |
| 579 | l4_type = HFI1_GET_L4_TYPE(packet->ebuf); |
| 580 | if (likely(l4_type == OPA_VNIC_L4_ETHR)) { |
| 581 | vesw_id = HFI1_VNIC_GET_VESWID(packet->ebuf); |
| 582 | vinfo = idr_find(&dd->vnic.vesw_idr, vesw_id); |
| 583 | |
| 584 | /* |
| 585 | * In case of invalid vesw id, count the error on |
| 586 | * the first available vport. |
| 587 | */ |
| 588 | if (unlikely(!vinfo)) { |
| 589 | struct hfi1_vnic_vport_info *vinfo_tmp; |
| 590 | int id_tmp = 0; |
| 591 | |
| 592 | vinfo_tmp = idr_get_next(&dd->vnic.vesw_idr, &id_tmp); |
| 593 | if (vinfo_tmp) { |
| 594 | spin_lock(&vport_cntr_lock); |
| 595 | vinfo_tmp->stats[0].netstats.rx_nohandler++; |
| 596 | spin_unlock(&vport_cntr_lock); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | if (unlikely(!vinfo)) { |
| 602 | dd_dev_warn(dd, "vnic rcv err: l4 %d vesw id %d ctx %d\n", |
| 603 | l4_type, vesw_id, packet->rcd->ctxt); |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | q_idx = packet->rcd->vnic_q_idx; |
| 608 | rxq = &vinfo->rxq[q_idx]; |
| 609 | if (unlikely(!netif_oper_up(vinfo->netdev))) { |
| 610 | vinfo->stats[q_idx].rx_drop_state++; |
| 611 | skb_queue_purge(&rxq->skbq); |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | if (unlikely(skb_queue_len(&rxq->skbq) > HFI1_VNIC_RCV_Q_SIZE)) { |
| 616 | vinfo->stats[q_idx].netstats.rx_fifo_errors++; |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | skb = netdev_alloc_skb(vinfo->netdev, packet->tlen); |
| 621 | if (unlikely(!skb)) { |
| 622 | vinfo->stats[q_idx].netstats.rx_fifo_errors++; |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | memcpy(skb->data, packet->ebuf, packet->tlen); |
| 627 | skb_put(skb, packet->tlen); |
| 628 | skb_queue_tail(&rxq->skbq, skb); |
| 629 | |
| 630 | if (napi_schedule_prep(&rxq->napi)) { |
| 631 | v_dbg("napi %d scheduling\n", q_idx); |
| 632 | __napi_schedule(&rxq->napi); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | static int hfi1_vnic_up(struct hfi1_vnic_vport_info *vinfo) |
| 637 | { |
| 638 | struct hfi1_devdata *dd = vinfo->dd; |
| 639 | struct net_device *netdev = vinfo->netdev; |
| 640 | int i, rc; |
| 641 | |
| 642 | /* ensure virtual eth switch id is valid */ |
| 643 | if (!vinfo->vesw_id) |
| 644 | return -EINVAL; |
| 645 | |
| 646 | rc = idr_alloc(&dd->vnic.vesw_idr, vinfo, vinfo->vesw_id, |
| 647 | vinfo->vesw_id + 1, GFP_NOWAIT); |
| 648 | if (rc < 0) |
| 649 | return rc; |
| 650 | |
| 651 | for (i = 0; i < vinfo->num_rx_q; i++) { |
| 652 | struct hfi1_vnic_rx_queue *rxq = &vinfo->rxq[i]; |
| 653 | |
| 654 | skb_queue_head_init(&rxq->skbq); |
| 655 | napi_enable(&rxq->napi); |
| 656 | } |
| 657 | |
| 658 | netif_carrier_on(netdev); |
| 659 | netif_tx_start_all_queues(netdev); |
| 660 | set_bit(HFI1_VNIC_UP, &vinfo->flags); |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static void hfi1_vnic_down(struct hfi1_vnic_vport_info *vinfo) |
| 666 | { |
| 667 | struct hfi1_devdata *dd = vinfo->dd; |
| 668 | u8 i; |
| 669 | |
| 670 | clear_bit(HFI1_VNIC_UP, &vinfo->flags); |
| 671 | netif_carrier_off(vinfo->netdev); |
| 672 | netif_tx_disable(vinfo->netdev); |
| 673 | idr_remove(&dd->vnic.vesw_idr, vinfo->vesw_id); |
| 674 | |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame^] | 675 | /* ensure irqs see the change */ |
| 676 | hfi1_vnic_synchronize_irq(dd); |
| 677 | |
Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 678 | /* remove unread skbs */ |
| 679 | for (i = 0; i < vinfo->num_rx_q; i++) { |
| 680 | struct hfi1_vnic_rx_queue *rxq = &vinfo->rxq[i]; |
| 681 | |
| 682 | napi_disable(&rxq->napi); |
| 683 | skb_queue_purge(&rxq->skbq); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | static int hfi1_netdev_open(struct net_device *netdev) |
| 688 | { |
| 689 | struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev); |
| 690 | int rc; |
| 691 | |
| 692 | mutex_lock(&vinfo->lock); |
| 693 | rc = hfi1_vnic_up(vinfo); |
| 694 | mutex_unlock(&vinfo->lock); |
| 695 | return rc; |
| 696 | } |
| 697 | |
| 698 | static int hfi1_netdev_close(struct net_device *netdev) |
| 699 | { |
| 700 | struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev); |
| 701 | |
| 702 | mutex_lock(&vinfo->lock); |
| 703 | if (test_bit(HFI1_VNIC_UP, &vinfo->flags)) |
| 704 | hfi1_vnic_down(vinfo); |
| 705 | mutex_unlock(&vinfo->lock); |
| 706 | return 0; |
| 707 | } |
| 708 | |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame^] | 709 | static int hfi1_vnic_allot_ctxt(struct hfi1_devdata *dd, |
| 710 | struct hfi1_ctxtdata **vnic_ctxt) |
| 711 | { |
| 712 | int rc; |
| 713 | |
| 714 | rc = allocate_vnic_ctxt(dd, vnic_ctxt); |
| 715 | if (rc) { |
| 716 | dd_dev_err(dd, "vnic ctxt alloc failed %d\n", rc); |
| 717 | return rc; |
| 718 | } |
| 719 | |
| 720 | rc = setup_vnic_ctxt(dd, *vnic_ctxt); |
| 721 | if (rc) { |
| 722 | dd_dev_err(dd, "vnic ctxt setup failed %d\n", rc); |
| 723 | deallocate_vnic_ctxt(dd, *vnic_ctxt); |
| 724 | *vnic_ctxt = NULL; |
| 725 | } |
| 726 | |
| 727 | return rc; |
| 728 | } |
| 729 | |
| 730 | static int hfi1_vnic_init(struct hfi1_vnic_vport_info *vinfo) |
| 731 | { |
| 732 | struct hfi1_devdata *dd = vinfo->dd; |
| 733 | int i, rc = 0; |
| 734 | |
| 735 | mutex_lock(&hfi1_mutex); |
| 736 | if (!dd->vnic.num_vports) |
| 737 | dd->vnic.msix_idx = dd->first_dyn_msix_idx; |
| 738 | |
| 739 | for (i = dd->vnic.num_ctxt; i < vinfo->num_rx_q; i++) { |
| 740 | rc = hfi1_vnic_allot_ctxt(dd, &dd->vnic.ctxt[i]); |
| 741 | if (rc) |
| 742 | break; |
| 743 | dd->vnic.ctxt[i]->vnic_q_idx = i; |
| 744 | } |
| 745 | |
| 746 | if (i < vinfo->num_rx_q) { |
| 747 | /* |
| 748 | * If required amount of contexts is not |
| 749 | * allocated successfully then remaining contexts |
| 750 | * are released. |
| 751 | */ |
| 752 | while (i-- > dd->vnic.num_ctxt) { |
| 753 | deallocate_vnic_ctxt(dd, dd->vnic.ctxt[i]); |
| 754 | dd->vnic.ctxt[i] = NULL; |
| 755 | } |
| 756 | goto alloc_fail; |
| 757 | } |
| 758 | |
| 759 | if (dd->vnic.num_ctxt != i) { |
| 760 | dd->vnic.num_ctxt = i; |
| 761 | hfi1_init_vnic_rsm(dd); |
| 762 | } |
| 763 | |
| 764 | dd->vnic.num_vports++; |
| 765 | alloc_fail: |
| 766 | mutex_unlock(&hfi1_mutex); |
| 767 | return rc; |
| 768 | } |
| 769 | |
| 770 | static void hfi1_vnic_deinit(struct hfi1_vnic_vport_info *vinfo) |
| 771 | { |
| 772 | struct hfi1_devdata *dd = vinfo->dd; |
| 773 | int i; |
| 774 | |
| 775 | mutex_lock(&hfi1_mutex); |
| 776 | if (--dd->vnic.num_vports == 0) { |
| 777 | for (i = 0; i < dd->vnic.num_ctxt; i++) { |
| 778 | deallocate_vnic_ctxt(dd, dd->vnic.ctxt[i]); |
| 779 | dd->vnic.ctxt[i] = NULL; |
| 780 | } |
| 781 | hfi1_deinit_vnic_rsm(dd); |
| 782 | dd->vnic.num_ctxt = 0; |
| 783 | } |
| 784 | mutex_unlock(&hfi1_mutex); |
| 785 | } |
| 786 | |
Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 787 | static void hfi1_vnic_set_vesw_id(struct net_device *netdev, int id) |
| 788 | { |
| 789 | struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev); |
| 790 | bool reopen = false; |
| 791 | |
| 792 | /* |
| 793 | * If vesw_id is being changed, and if the vnic port is up, |
| 794 | * reset the vnic port to ensure new vesw_id gets picked up |
| 795 | */ |
| 796 | if (id != vinfo->vesw_id) { |
| 797 | mutex_lock(&vinfo->lock); |
| 798 | if (test_bit(HFI1_VNIC_UP, &vinfo->flags)) { |
| 799 | hfi1_vnic_down(vinfo); |
| 800 | reopen = true; |
| 801 | } |
| 802 | |
| 803 | vinfo->vesw_id = id; |
| 804 | if (reopen) |
| 805 | hfi1_vnic_up(vinfo); |
| 806 | |
| 807 | mutex_unlock(&vinfo->lock); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /* netdev ops */ |
| 812 | static const struct net_device_ops hfi1_netdev_ops = { |
| 813 | .ndo_open = hfi1_netdev_open, |
| 814 | .ndo_stop = hfi1_netdev_close, |
| 815 | .ndo_start_xmit = hfi1_netdev_start_xmit, |
| 816 | .ndo_select_queue = hfi1_vnic_select_queue, |
| 817 | .ndo_get_stats64 = hfi1_vnic_get_stats64, |
| 818 | }; |
| 819 | |
| 820 | struct net_device *hfi1_vnic_alloc_rn(struct ib_device *device, |
| 821 | u8 port_num, |
| 822 | enum rdma_netdev_t type, |
| 823 | const char *name, |
| 824 | unsigned char name_assign_type, |
| 825 | void (*setup)(struct net_device *)) |
| 826 | { |
| 827 | struct hfi1_devdata *dd = dd_from_ibdev(device); |
| 828 | struct hfi1_vnic_vport_info *vinfo; |
| 829 | struct net_device *netdev; |
| 830 | struct rdma_netdev *rn; |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame^] | 831 | int i, size, rc; |
Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 832 | |
| 833 | if (!port_num || (port_num > dd->num_pports)) |
| 834 | return ERR_PTR(-EINVAL); |
| 835 | |
| 836 | if (type != RDMA_NETDEV_OPA_VNIC) |
| 837 | return ERR_PTR(-EOPNOTSUPP); |
| 838 | |
| 839 | size = sizeof(struct opa_vnic_rdma_netdev) + sizeof(*vinfo); |
| 840 | netdev = alloc_netdev_mqs(size, name, name_assign_type, setup, |
| 841 | dd->chip_sdma_engines, HFI1_NUM_VNIC_CTXT); |
| 842 | if (!netdev) |
| 843 | return ERR_PTR(-ENOMEM); |
| 844 | |
| 845 | rn = netdev_priv(netdev); |
| 846 | vinfo = opa_vnic_dev_priv(netdev); |
| 847 | vinfo->dd = dd; |
| 848 | vinfo->num_tx_q = dd->chip_sdma_engines; |
| 849 | vinfo->num_rx_q = HFI1_NUM_VNIC_CTXT; |
| 850 | vinfo->netdev = netdev; |
| 851 | rn->set_id = hfi1_vnic_set_vesw_id; |
| 852 | |
| 853 | netdev->features = NETIF_F_HIGHDMA | NETIF_F_SG; |
| 854 | netdev->hw_features = netdev->features; |
| 855 | netdev->vlan_features = netdev->features; |
| 856 | netdev->watchdog_timeo = msecs_to_jiffies(HFI_TX_TIMEOUT_MS); |
| 857 | netdev->netdev_ops = &hfi1_netdev_ops; |
| 858 | mutex_init(&vinfo->lock); |
| 859 | |
| 860 | for (i = 0; i < vinfo->num_rx_q; i++) { |
| 861 | struct hfi1_vnic_rx_queue *rxq = &vinfo->rxq[i]; |
| 862 | |
| 863 | rxq->idx = i; |
| 864 | rxq->vinfo = vinfo; |
| 865 | rxq->netdev = netdev; |
| 866 | netif_napi_add(netdev, &rxq->napi, hfi1_vnic_napi, 64); |
| 867 | } |
| 868 | |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame^] | 869 | rc = hfi1_vnic_init(vinfo); |
| 870 | if (rc) |
| 871 | goto init_fail; |
| 872 | |
Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 873 | return netdev; |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame^] | 874 | init_fail: |
| 875 | mutex_destroy(&vinfo->lock); |
| 876 | free_netdev(netdev); |
| 877 | return ERR_PTR(rc); |
Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | void hfi1_vnic_free_rn(struct net_device *netdev) |
| 881 | { |
| 882 | struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev); |
| 883 | |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame^] | 884 | hfi1_vnic_deinit(vinfo); |
Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 885 | mutex_destroy(&vinfo->lock); |
| 886 | free_netdev(netdev); |
| 887 | } |