Print this page


Split Close
Expand all
Collapse all
          --- old/usr/src/uts/common/io/vioif/vioif.c
          +++ new/usr/src/uts/common/io/vioif/vioif.c
↓ open down ↓ 2 lines elided ↑ open up ↑
   3    3   * Common Development and Distribution License ("CDDL"), version 1.0.
   4    4   * You may only use this file in accordance with the terms of version
   5    5   * 1.0 of the CDDL.
   6    6   *
   7    7   * A full copy of the text of the CDDL should have accompanied this
   8    8   * source.  A copy of the CDDL is also available via the Internet at
   9    9   * http://www.illumos.org/license/CDDL.
  10   10   */
  11   11  
  12   12  /*
  13      - * Copyright 2013 Nexenta Inc.  All rights reserved.
       13 + * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
  14   14   * Copyright (c) 2014, 2015 by Delphix. All rights reserved.
  15   15   */
  16   16  
  17   17  /* Based on the NetBSD virtio driver by Minoura Makoto. */
  18   18  /*
  19   19   * Copyright (c) 2010 Minoura Makoto.
  20   20   * All rights reserved.
  21   21   *
  22   22   * Redistribution and use in source and binary forms, with or without
  23   23   * modification, are permitted provided that the following conditions
↓ open down ↓ 228 lines elided ↑ open up ↑
 252  252          mac_handle_t sc_mac_handle;
 253  253          mac_register_t *sc_macp;
 254  254  
 255  255          struct virtqueue        *sc_rx_vq;
 256  256          struct virtqueue        *sc_tx_vq;
 257  257          struct virtqueue        *sc_ctrl_vq;
 258  258  
 259  259          unsigned int            sc_tx_stopped:1;
 260  260  
 261  261          /* Feature bits. */
 262      -        unsigned int            sc_rx_csum:1;
 263      -        unsigned int            sc_tx_csum:1;
      262 +        unsigned int            sc_rx_csum:1;
      263 +        unsigned int            sc_tx_csum:1;
 264  264          unsigned int            sc_tx_tso4:1;
 265  265  
 266      -        int                     sc_mtu;
      266 +        int                     sc_mtu;
 267  267          uint8_t                 sc_mac[ETHERADDRL];
 268  268          /*
 269  269           * For rx buffers, we keep a pointer array, because the buffers
 270  270           * can be loaned upstream, and we have to repopulate the array with
 271  271           * new members.
 272  272           */
 273  273          struct vioif_rx_buf     **sc_rxbufs;
 274  274  
 275  275          /*
 276  276           * For tx, we just allocate an array of buffers. The packet can
↓ open down ↓ 26 lines elided ↑ open up ↑
 303  303          uint64_t                sc_notxbuf;
 304  304          uint64_t                sc_ierrors;
 305  305          uint64_t                sc_oerrors;
 306  306  };
 307  307  
 308  308  #define ETHER_HEADER_LEN                sizeof (struct ether_header)
 309  309  
 310  310  /* MTU + the ethernet header. */
 311  311  #define MAX_PAYLOAD     65535
 312  312  #define MAX_MTU         (MAX_PAYLOAD - ETHER_HEADER_LEN)
 313      -#define DEFAULT_MTU     ETHERMTU
      313 +#define DEFAULT_MTU     ETHERMTU
 314  314  
 315  315  /*
 316  316   * Yeah, we spend 8M per device. Turns out, there is no point
 317  317   * being smart and using merged rx buffers (VIRTIO_NET_F_MRG_RXBUF),
 318  318   * because vhost does not support them, and we expect to be used with
 319  319   * vhost in production environment.
 320  320   */
 321  321  /* The buffer keeps both the packet data and the virtio_net_header. */
 322  322  #define VIOIF_RX_SIZE (MAX_PAYLOAD + sizeof (struct virtio_net_hdr))
 323  323  
↓ open down ↓ 18 lines elided ↑ open up ↑
 342  342  #define VIOIF_TX_QLEN 0
 343  343  #define VIOIF_CTRL_QLEN 0
 344  344  
 345  345  static uchar_t vioif_broadcast[ETHERADDRL] = {
 346  346          0xff, 0xff, 0xff, 0xff, 0xff, 0xff
 347  347  };
 348  348  
 349  349  #define VIOIF_TX_THRESH_MAX     640
 350  350  #define VIOIF_RX_THRESH_MAX     640
 351  351  
 352      -#define CACHE_NAME_SIZE 32
      352 +#define CACHE_NAME_SIZE 32
 353  353  
 354  354  static char vioif_txcopy_thresh[] =
 355  355          "vioif_txcopy_thresh";
 356  356  static char vioif_rxcopy_thresh[] =
 357  357          "vioif_rxcopy_thresh";
 358  358  
 359  359  static char *vioif_priv_props[] = {
 360  360          vioif_txcopy_thresh,
 361  361          vioif_rxcopy_thresh,
 362  362          NULL
↓ open down ↓ 14 lines elided ↑ open up ↑
 377  377          ddi_dma_impl_t *dmah_impl = (void *) dmah;
 378  378          dmah_impl->dmai_cookie = dmac;
 379  379  }
 380  380  
 381  381  static link_state_t
 382  382  vioif_link_state(struct vioif_softc *sc)
 383  383  {
 384  384          if (sc->sc_virtio.sc_features & VIRTIO_NET_F_STATUS) {
 385  385                  if (virtio_read_device_config_2(&sc->sc_virtio,
 386  386                      VIRTIO_NET_CONFIG_STATUS) & VIRTIO_NET_S_LINK_UP) {
      387 +
 387  388                          return (LINK_STATE_UP);
 388  389                  } else {
 389  390                          return (LINK_STATE_DOWN);
 390  391                  }
 391  392          }
 392  393  
 393  394          return (LINK_STATE_UP);
 394  395  }
 395  396  
 396  397  static ddi_dma_attr_t vioif_inline_buf_dma_attr = {
↓ open down ↓ 291 lines elided ↑ open up ↑
 688  689  
 689  690  
 690  691  static int
 691  692  vioif_add_rx(struct vioif_softc *sc, int kmflag)
 692  693  {
 693  694          struct vq_entry *ve;
 694  695          struct vioif_rx_buf *buf;
 695  696  
 696  697          ve = vq_alloc_entry(sc->sc_rx_vq);
 697  698          if (!ve) {
 698      -                /*
 699      -                 * Out of free descriptors - ring already full.
 700      -                 * It would be better to update sc_norxdescavail
 701      -                 * but MAC does not ask for this info, hence we
 702      -                 * update sc_norecvbuf.
      699 +                /* Out of free descriptors - ring already full.
      700 +                 * would be better to update sc_norxdescavail
      701 +                 * but MAC does not ask for this info
      702 +                 * hence update sc_norecvbuf
 703  703                   */
 704  704                  sc->sc_norecvbuf++;
 705  705                  goto exit_vq;
 706  706          }
 707  707          buf = sc->sc_rxbufs[ve->qe_index];
 708  708  
 709  709          if (!buf) {
 710  710                  /* First run, allocate the buffer. */
 711  711                  buf = kmem_cache_alloc(sc->sc_rxbuf_cache, kmflag);
 712  712                  sc->sc_rxbufs[ve->qe_index] = buf;
↓ open down ↓ 124 lines elided ↑ open up ↑
 837  837                                  sc->sc_norecvbuf++;
 838  838                                  sc->sc_ierrors++;
 839  839  
 840  840                                  virtio_free_chain(ve);
 841  841                                  break;
 842  842                          }
 843  843                          mp->b_wptr = mp->b_rptr + len;
 844  844  
 845  845                          atomic_inc_ulong(&sc->sc_rxloan);
 846  846                          /*
 847      -                         * Buffer loaned, we will have to allocte a new one
      847 +                         * Buffer loanded, we will have to allocate a new one
 848  848                           * for this slot.
 849  849                           */
 850  850                          sc->sc_rxbufs[ve->qe_index] = NULL;
 851  851                  }
 852      -
 853      -                /*
 854      -                 * virtio-net does not tell us if this packet is multicast
 855      -                 * or broadcast, so we have to check it.
      852 +                /* virtio-net does not provide the info if this packet
      853 +                 * is multicast or broadcast. So we have to check it
 856  854                   */
 857  855                  if (mp->b_rptr[0] & 0x1) {
 858  856                          if (bcmp(mp->b_rptr, vioif_broadcast, ETHERADDRL) != 0)
 859  857                                  sc->sc_multircv++;
 860  858                          else
 861  859                                  sc->sc_brdcstrcv++;
 862  860                  }
 863  861  
 864  862                  sc->sc_rbytes += len;
 865  863                  sc->sc_ipackets++;
↓ open down ↓ 443 lines elided ↑ open up ↑
1309 1307                  err = mac_maxsdu_update(sc->sc_mac_handle, *new_mtu);
1310 1308                  if (err) {
1311 1309                          return (err);
1312 1310                  }
1313 1311                  break;
1314 1312          case MAC_PROP_PRIVATE:
1315 1313                  err = vioif_set_prop_private(sc, pr_name,
1316 1314                      pr_valsize, pr_val);
1317 1315                  if (err)
1318 1316                          return (err);
1319      -                break;
1320 1317          default:
1321 1318                  return (ENOTSUP);
1322 1319          }
1323 1320  
1324 1321          return (0);
1325 1322  }
1326 1323  
1327 1324  static int
1328 1325  vioif_get_prop_private(struct vioif_softc *sc, const char *pr_name,
1329 1326      uint_t pr_valsize, void *pr_val)
↓ open down ↓ 36 lines elided ↑ open up ↑
1366 1363                  break;
1367 1364          }
1368 1365          return (err);
1369 1366  }
1370 1367  
1371 1368  static void
1372 1369  vioif_propinfo(void *arg, const char *pr_name, mac_prop_id_t pr_num,
1373 1370      mac_prop_info_handle_t prh)
1374 1371  {
1375 1372          struct vioif_softc *sc = arg;
1376      -        char valstr[64];
1377      -        int value;
1378 1373  
1379 1374          switch (pr_num) {
1380 1375          case MAC_PROP_MTU:
1381 1376                  mac_prop_info_set_range_uint32(prh, ETHERMIN, MAX_MTU);
1382 1377                  break;
1383 1378  
1384      -        case MAC_PROP_PRIVATE:
     1379 +        case MAC_PROP_PRIVATE: {
     1380 +                char valstr[64];
     1381 +                int value;
     1382 +
1385 1383                  bzero(valstr, sizeof (valstr));
1386 1384                  if (strcmp(pr_name, vioif_txcopy_thresh) == 0) {
1387 1385  
1388 1386                          value = sc->sc_txcopy_thresh;
1389 1387                  } else  if (strcmp(pr_name,
1390 1388                      vioif_rxcopy_thresh) == 0) {
1391 1389                          value = sc->sc_rxcopy_thresh;
1392 1390                  } else {
1393 1391                          return;
1394 1392                  }
1395 1393                  (void) snprintf(valstr, sizeof (valstr), "%d", value);
1396      -                break;
1397      -
     1394 +        }
1398 1395          default:
1399 1396                  break;
1400 1397          }
1401 1398  }
1402 1399  
1403 1400  static boolean_t
1404 1401  vioif_getcapab(void *arg, mac_capab_t cap, void *cap_data)
1405 1402  {
1406 1403          struct vioif_softc *sc = arg;
1407 1404  
↓ open down ↓ 15 lines elided ↑ open up ↑
1423 1420                          return (B_TRUE);
1424 1421                  }
1425 1422                  return (B_FALSE);
1426 1423          default:
1427 1424                  break;
1428 1425          }
1429 1426          return (B_FALSE);
1430 1427  }
1431 1428  
1432 1429  static mac_callbacks_t vioif_m_callbacks = {
1433      -        .mc_callbacks   = (MC_GETCAPAB | MC_SETPROP | MC_GETPROP | MC_PROPINFO),
     1430 +        .mc_callbacks   = (MC_GETCAPAB | MC_SETPROP | MC_GETPROP | MC_PROPINFO),
1434 1431          .mc_getstat     = vioif_stat,
1435 1432          .mc_start       = vioif_start,
1436 1433          .mc_stop        = vioif_stop,
1437 1434          .mc_setpromisc  = vioif_promisc,
1438 1435          .mc_multicst    = vioif_multicst,
1439 1436          .mc_unicst      = vioif_unicst,
1440 1437          .mc_tx          = vioif_tx,
1441 1438          /* Optional callbacks */
1442 1439          .mc_reserved    = NULL,         /* reserved */
1443 1440          .mc_ioctl       = NULL,         /* mc_ioctl */
↓ open down ↓ 3 lines elided ↑ open up ↑
1447 1444          .mc_setprop     = vioif_setprop,
1448 1445          .mc_getprop     = vioif_getprop,
1449 1446          .mc_propinfo    = vioif_propinfo,
1450 1447  };
1451 1448  
1452 1449  static void
1453 1450  vioif_show_features(struct vioif_softc *sc, const char *prefix,
1454 1451      uint32_t features)
1455 1452  {
1456 1453          char buf[512];
1457      -        char *bufp = buf;
1458      -        char *bufend = buf + sizeof (buf);
1459 1454  
1460      -        /* LINTED E_PTRDIFF_OVERFLOW */
1461      -        bufp += snprintf(bufp, bufend - bufp, prefix);
1462      -
1463      -        /* LINTED E_PTRDIFF_OVERFLOW */
1464      -        bufp += virtio_show_features(features, bufp, bufend - bufp);
1465      -
1466      -        /* LINTED E_PTRDIFF_OVERFLOW */
1467      -        bufp += snprintf(bufp, bufend - bufp, "Vioif ( ");
1468      -
1469      -        if (features & VIRTIO_NET_F_CSUM)
1470      -                /* LINTED E_PTRDIFF_OVERFLOW */
1471      -                bufp += snprintf(bufp, bufend - bufp, "CSUM ");
1472      -        if (features & VIRTIO_NET_F_GUEST_CSUM)
1473      -                /* LINTED E_PTRDIFF_OVERFLOW */
1474      -                bufp += snprintf(bufp, bufend - bufp, "GUEST_CSUM ");
1475      -        if (features & VIRTIO_NET_F_MAC)
1476      -                /* LINTED E_PTRDIFF_OVERFLOW */
1477      -                bufp += snprintf(bufp, bufend - bufp, "MAC ");
1478      -        if (features & VIRTIO_NET_F_GSO)
1479      -                /* LINTED E_PTRDIFF_OVERFLOW */
1480      -                bufp += snprintf(bufp, bufend - bufp, "GSO ");
1481      -        if (features & VIRTIO_NET_F_GUEST_TSO4)
1482      -                /* LINTED E_PTRDIFF_OVERFLOW */
1483      -                bufp += snprintf(bufp, bufend - bufp, "GUEST_TSO4 ");
1484      -        if (features & VIRTIO_NET_F_GUEST_TSO6)
1485      -                /* LINTED E_PTRDIFF_OVERFLOW */
1486      -                bufp += snprintf(bufp, bufend - bufp, "GUEST_TSO6 ");
1487      -        if (features & VIRTIO_NET_F_GUEST_ECN)
1488      -                /* LINTED E_PTRDIFF_OVERFLOW */
1489      -                bufp += snprintf(bufp, bufend - bufp, "GUEST_ECN ");
1490      -        if (features & VIRTIO_NET_F_GUEST_UFO)
1491      -                /* LINTED E_PTRDIFF_OVERFLOW */
1492      -                bufp += snprintf(bufp, bufend - bufp, "GUEST_UFO ");
1493      -        if (features & VIRTIO_NET_F_HOST_TSO4)
1494      -                /* LINTED E_PTRDIFF_OVERFLOW */
1495      -                bufp += snprintf(bufp, bufend - bufp, "HOST_TSO4 ");
1496      -        if (features & VIRTIO_NET_F_HOST_TSO6)
1497      -                /* LINTED E_PTRDIFF_OVERFLOW */
1498      -                bufp += snprintf(bufp, bufend - bufp, "HOST_TSO6 ");
1499      -        if (features & VIRTIO_NET_F_HOST_ECN)
1500      -                /* LINTED E_PTRDIFF_OVERFLOW */
1501      -                bufp += snprintf(bufp, bufend - bufp, "HOST_ECN ");
1502      -        if (features & VIRTIO_NET_F_HOST_UFO)
1503      -                /* LINTED E_PTRDIFF_OVERFLOW */
1504      -                bufp += snprintf(bufp, bufend - bufp, "HOST_UFO ");
1505      -        if (features & VIRTIO_NET_F_MRG_RXBUF)
1506      -                /* LINTED E_PTRDIFF_OVERFLOW */
1507      -                bufp += snprintf(bufp, bufend - bufp, "MRG_RXBUF ");
1508      -        if (features & VIRTIO_NET_F_STATUS)
1509      -                /* LINTED E_PTRDIFF_OVERFLOW */
1510      -                bufp += snprintf(bufp, bufend - bufp, "STATUS ");
1511      -        if (features & VIRTIO_NET_F_CTRL_VQ)
1512      -                /* LINTED E_PTRDIFF_OVERFLOW */
1513      -                bufp += snprintf(bufp, bufend - bufp, "CTRL_VQ ");
1514      -        if (features & VIRTIO_NET_F_CTRL_RX)
1515      -                /* LINTED E_PTRDIFF_OVERFLOW */
1516      -                bufp += snprintf(bufp, bufend - bufp, "CTRL_RX ");
1517      -        if (features & VIRTIO_NET_F_CTRL_VLAN)
1518      -                /* LINTED E_PTRDIFF_OVERFLOW */
1519      -                bufp += snprintf(bufp, bufend - bufp, "CTRL_VLAN ");
1520      -        if (features & VIRTIO_NET_F_CTRL_RX_EXTRA)
1521      -                /* LINTED E_PTRDIFF_OVERFLOW */
1522      -                bufp += snprintf(bufp, bufend - bufp, "CTRL_RX_EXTRA ");
1523      -
1524      -        /* LINTED E_PTRDIFF_OVERFLOW */
1525      -        bufp += snprintf(bufp, bufend - bufp, ")");
1526      -        *bufp = '\0';
1527      -
1528      -        dev_err(sc->sc_dev, CE_NOTE, "%s", buf);
     1455 +        dev_err(sc->sc_dev, CE_NOTE, "%s %s Vioif (%b)", prefix, virtio_show_features(...), features, "\020\1CSUM\2GUEST_CSUM\3MAC\4GSO\5GUEST_TSO4\5GUEST_TSO6\6GUEST_ECN\7GUEST_UFO\8HOST_TSO4\9HOST_TSO6\10HOST_ECN\11HOST_UFO\12MRG_RXBUF\13STATUS\14CTRL_VQ\15CTRL_RX\16CTRL_VLAN\17CTRL_RX_EXTRA", buf);
1529 1456  }
1530 1457  
1531 1458  /*
1532 1459   * Find out which features are supported by the device and
1533 1460   * choose which ones we wish to use.
1534 1461   */
1535 1462  static int
1536 1463  vioif_dev_features(struct vioif_softc *sc)
1537 1464  {
1538 1465          uint32_t host_features;
↓ open down ↓ 1 lines elided ↑ open up ↑
1540 1467          host_features = virtio_negotiate_features(&sc->sc_virtio,
1541 1468              VIRTIO_NET_F_CSUM |
1542 1469              VIRTIO_NET_F_HOST_TSO4 |
1543 1470              VIRTIO_NET_F_HOST_ECN |
1544 1471              VIRTIO_NET_F_MAC |
1545 1472              VIRTIO_NET_F_STATUS |
1546 1473              VIRTIO_F_RING_INDIRECT_DESC |
1547 1474              VIRTIO_F_NOTIFY_ON_EMPTY);
1548 1475  
1549 1476          vioif_show_features(sc, "Host features: ", host_features);
1550      -        vioif_show_features(sc, "Negotiated features: ",
     1477 +        vioif_show_features(sc,  ",
1551 1478              sc->sc_virtio.sc_features);
1552 1479  
1553 1480          if (!(sc->sc_virtio.sc_features & VIRTIO_F_RING_INDIRECT_DESC)) {
1554 1481                  dev_err(sc->sc_dev, CE_NOTE,
1555 1482                      "Host does not support RING_INDIRECT_DESC, bye.");
1556 1483                  return (DDI_FAILURE);
1557 1484          }
1558 1485  
1559 1486          return (DDI_SUCCESS);
1560 1487  }
↓ open down ↓ 136 lines elided ↑ open up ↑
1697 1624          char cache_name[CACHE_NAME_SIZE];
1698 1625  
1699 1626          instance = ddi_get_instance(devinfo);
1700 1627  
1701 1628          switch (cmd) {
1702 1629          case DDI_ATTACH:
1703 1630                  break;
1704 1631  
1705 1632          case DDI_RESUME:
1706 1633          case DDI_PM_RESUME:
1707      -                /* not supported yet */
     1634 +                dev_err(devinfo, CE_WARN, "resume not supported yet");
1708 1635                  goto exit;
1709 1636  
1710 1637          default:
1711      -                /* unrecognized command */
     1638 +                dev_err(devinfo, CE_WARN, "cmd 0x%x unrecognized", cmd);
1712 1639                  goto exit;
1713 1640          }
1714 1641  
1715 1642          sc = kmem_zalloc(sizeof (struct vioif_softc), KM_SLEEP);
1716 1643          ddi_set_driver_private(devinfo, sc);
1717 1644  
1718 1645          vsc = &sc->sc_virtio;
1719 1646  
1720 1647          /* Duplicate for less typing */
1721 1648          sc->sc_dev = devinfo;
↓ open down ↓ 158 lines elided ↑ open up ↑
1880 1807          struct vioif_softc *sc;
1881 1808  
1882 1809          if ((sc = ddi_get_driver_private(devinfo)) == NULL)
1883 1810                  return (DDI_FAILURE);
1884 1811  
1885 1812          switch (cmd) {
1886 1813          case DDI_DETACH:
1887 1814                  break;
1888 1815  
1889 1816          case DDI_PM_SUSPEND:
1890      -                /* not supported yet */
     1817 +                cmn_err(CE_WARN, "suspend not supported yet");
1891 1818                  return (DDI_FAILURE);
1892 1819  
1893 1820          default:
1894      -                /* unrecognized command */
     1821 +                cmn_err(CE_WARN, "cmd 0x%x unrecognized", cmd);
1895 1822                  return (DDI_FAILURE);
1896 1823          }
1897 1824  
1898 1825          if (sc->sc_rxloan) {
1899      -                cmn_err(CE_NOTE, "Some rx buffers are still upstream, "
     1826 +                cmn_err(CE_WARN, "Some rx buffers are still upstream, "
1900 1827                      "Not detaching");
1901 1828                  return (DDI_FAILURE);
1902 1829          }
1903 1830  
1904 1831          virtio_stop_vq_intr(sc->sc_rx_vq);
1905 1832          virtio_stop_vq_intr(sc->sc_tx_vq);
1906 1833  
1907 1834          virtio_release_ints(&sc->sc_virtio);
1908 1835  
1909 1836          if (mac_unregister(sc->sc_mac_handle)) {
↓ open down ↓ 35 lines elided ↑ open up ↑
1945 1872  int
1946 1873  _init(void)
1947 1874  {
1948 1875          int ret = 0;
1949 1876  
1950 1877          mac_init_ops(&vioif_ops, "vioif");
1951 1878  
1952 1879          ret = mod_install(&modlinkage);
1953 1880          if (ret != DDI_SUCCESS) {
1954 1881                  mac_fini_ops(&vioif_ops);
     1882 +                cmn_err(CE_WARN, "Unable to install the driver");
1955 1883                  return (ret);
1956 1884          }
1957 1885  
1958 1886          return (0);
1959 1887  }
1960 1888  
1961 1889  int
1962 1890  _fini(void)
1963 1891  {
1964 1892          int ret;
↓ open down ↓ 14 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX