Skip to contentSkip to conversation

PA Semi PWRficient Ethernet driver

PATCH Superseded Olof Johansson 7 participants 23 messages Jan 29, 2007 – Feb 2, 2007

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PA Semi PWRficient Ethernet driver

Driver for the PA Semi PWRficient on-chip Ethernet (1/10G)

Basic enablement, will be complemented with performance enhancements over time. PHY support will be added as well.

This patch still uses the numerical PCI vendor id, it will be replaced when the pci_ids.h change goes in (same as the other currently pending drivers).

drivers/net/Kconfig +7 -0
Index: merge/drivers/net/Kconfig
===================================================================
@@ -2348,6 +2348,13 @@ config QLA3XXX
 	  To compile this driver as a module, choose M here: the module
 	  will be called qla3xxx.
 
+config PASEMI_MAC
+	tristate "PA Semi 1/10Gbit MAC"
+	depends on PPC64 && PCI
+	help
+	  This driver supports the on-chip 1/10Gbit Ethernet controller on
+	  PA Semi's PWRficient line of chips.
+
 endmenu
 
 #
drivers/net/Makefile +1 -0
Index: merge/drivers/net/Makefile
===================================================================
@@ -196,6 +196,7 @@ obj-$(CONFIG_SMC91X) += smc91x.o
 obj-$(CONFIG_SMC911X) += smc911x.o
 obj-$(CONFIG_DM9000) += dm9000.o
 obj-$(CONFIG_FEC_8XX) += fec_8xx/
+obj-$(CONFIG_PASEMI_MAC) += pasemi_mac.o
 
 obj-$(CONFIG_MACB) += macb.o
 
drivers/net/pasemi_mac.c new file +797 -0

797 lines not on this page · raw message

drivers/net/pasemi_mac.h new file +442 -0

442 lines not on this page · raw message

#1 lore raw

Basic initalization, setup comments.

drivers/net/pasemi_mac.c line 283

Quoted 7 lines
drivers/net/pasemi_mac.c new file +7 -0
+static int pasemi_mac_open(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int flags;
+	int ret;
+
+	pr_debug("pasemi_mac_open\n");

dev_dbg() ?

Quoted 48 lines
drivers/net/pasemi_mac.c new file +48 -0
+
+	/* enable rx section */
+	pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_RXCMD,
+			       PAS_DMA_COM_RXCMD_EN);
+
+	/* enable tx section */
+	pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_TXCMD,
+			       PAS_DMA_COM_TXCMD_EN);
+
+	flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
+		PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
+		PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
+
+	pci_write_config_dword(mac->pdev, PAS_MAC_CFG_TXP, flags);
+
+	flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
+		PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
+
+	flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
+
+	pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
+			       PAS_IOB_DMA_RXCH_CFG_CNTTH(30));
+
+	pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
+			       PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(1000000));
+
+	pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
+
+	pasemi_mac_setup_rx_resources(dev);
+	pasemi_mac_setup_tx_resources(dev);
+
+	pci_write_config_dword(mac->pdev, PAS_MAC_IPC_CHNL,
+			       PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
+			       PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
+
+	/* enable rx if */
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
+			       PAS_DMA_RXINT_RCMDSTA_EN);
+
+	/* enable rx channel */
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
+			       PAS_DMA_RXCHAN_CCMDSTA_EN |
+			       PAS_DMA_RXCHAN_CCMDSTA_DU);
+
+	/* enable tx channel */
+	pci_write_config_dword(mac->dma_pdev,

drivers/net/pasemi_mac.c line 341

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+			       PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
+			       PAS_DMA_TXCHAN_TCMDSTA_EN);
+
+	pasemi_mac_replenish_rx_ring(dev);
+
+	netif_start_queue(dev);
+	netif_poll_enable(dev);
+
+	ret = request_irq(128 + mac->dma_txch, &pasemi_mac_tx_intr,
+			  IRQF_DISABLED, "pasemi_mac tx", dev);

Shouldn't you get IRQ value from PCI config?

drivers/net/pasemi_mac.c line 350

Quoted 9 lines
drivers/net/pasemi_mac.c new file +9 -0
+	if (ret)
+		printk("request_irq of irq %d failed: %d\n",
+		       mac->dma_pdev->irq + mac->dma_txch, ret);
+
+	ret = request_irq(128 + 20 + mac->dma_rxch, &pasemi_mac_rx_intr,
+			  IRQF_DISABLED, "pasemi_mac rx", dev);
+	if (ret)
+		printk("request_irq of irq %d failed: %d\n",
+		       mac->dma_pdev->irq + 20 + mac->dma_rxch, ret);

You need to return error code and unwind other request_irq.

Quoted 100 lines
drivers/net/pasemi_mac.c new file +100 -0
+	return 0;
+}
+
+static int pasemi_mac_close(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int stat;
+
+	netif_stop_queue(dev);
+
+	/* Clean out any pending buffers */
+	pasemi_mac_clean_tx(mac);
+	pasemi_mac_clean_rx(mac, mac->rx->count);
+
+	/* Disable interface */
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
+			       PAS_DMA_TXCHAN_TCMDSTA_ST);
+	pci_write_config_dword(mac->dma_pdev,
+		      PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
+		      PAS_DMA_RXINT_RCMDSTA_ST);
+	pci_write_config_dword(mac->dma_pdev,
+		      PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
+		      PAS_DMA_RXCHAN_CCMDSTA_ST);
+
+	do {
+		pci_read_config_dword(mac->dma_pdev,
+				      PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
+				      &stat);
+	} while (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT);
+
+	do {
+		pci_read_config_dword(mac->dma_pdev,
+				      PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
+				      &stat);
+	} while (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT);
+
+	do {
+		pci_read_config_dword(mac->dma_pdev,
+				      PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
+				      &stat);
+	} while (stat & PAS_DMA_RXINT_RCMDSTA_ACT);
+
+	/* Then, disable the channel. This must be done separately from
+	 * stopping, since you can't disable when active.
+	 */
+
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
+
+	free_irq(mac->dma_pdev->irq + mac->dma_txch, dev);
+	free_irq(mac->dma_pdev->irq + 20 + mac->dma_rxch, dev);
+
+	/* Free resources */
+	pasemi_mac_free_resources(dev);
+
+	return 0;
+}
+
+static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	struct pasemi_mac_txring *txring;
+	u64 flags;
+	dma_addr_t map;
+
+	if (mac->tx->next_to_clean+mac->tx->count == mac->tx->next_to_use)
+		pasemi_mac_clean_tx(mac);
+
+	mac->stats.tx_packets++;
+	mac->stats.tx_bytes += skb->len;
+
+	txring = mac->tx;
+
+	flags = XCT_MACTX_O | XCT_MACTX_ST |
+		XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
+
+	if (skb->ip_summed == CHECKSUM_PARTIAL) {
+		switch (skb->nh.iph->protocol) {
+		case IPPROTO_TCP:
+			flags |= XCT_MACTX_CSUM_TCP;
+			flags |= XCT_MACTX_IPH((skb->h.raw - skb->nh.raw) >> 2);
+			flags |= XCT_MACTX_IPO(skb->nh.raw - skb->data);
+			break;
+		case IPPROTO_UDP:
+			flags |= XCT_MACTX_CSUM_UDP;
+			flags |= XCT_MACTX_IPH((skb->h.raw - skb->nh.raw) >> 2);
+			flags |= XCT_MACTX_IPO(skb->nh.raw - skb->data);
+			break;
+		}
+	}
+
+	map = virt_to_phys(skb->data);
+
+	DESCR(txring, txring->next_to_use).mactx = flags |
+						XCT_MACTX_LLEN(skb->len);

drivers/net/pasemi_mac.c line 461

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	DESCR(txring, txring->next_to_use).ptr = XCT_PTR_LEN(skb->len) |
+						XCT_PTR_ADDR(map);
+	INFO(txring, txring->next_to_use).dma = map;
+	INFO(txring, txring->next_to_use).skb = skb;
+	/* XXXOJN Deal with fragmented packets when larger MTU is supported */
+
+	txring->next_to_use++;
+
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);

You need to handle flow control and do netif_stop_queue/netif_wake_queue when your transmit ring gets full.

Quoted 188 lines
drivers/net/pasemi_mac.c new file +188 -0
+
+	return NETDEV_TX_OK;
+}
+
+static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+
+	return &mac->stats;
+}
+
+static void pasemi_mac_set_rx_mode(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int flags;
+
+	return;
+
+	pci_read_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, &flags);
+
+	/* Set promiscuous */
+	if (dev->flags & IFF_PROMISC)
+		flags |= PAS_MAC_CFG_PCFG_PR;
+	else
+		flags &= ~PAS_MAC_CFG_PCFG_PR;
+
+	pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
+}
+
+
+static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
+{
+	int i, j;
+	struct pas_dma_xct_descr descr;
+	struct pasemi_mac_buffer *info;
+	struct sk_buff *skb;
+	unsigned int len;
+	int start;
+	int count;
+	dma_addr_t dma;
+
+	start = mac->rx->next_to_clean;
+	count = 0;
+
+	for (i = start; i < start+mac->rx->count && count < limit; i++) {
+		rmb();
+		mb();
+		descr = DESCR(mac->rx, i);
+		if (!(descr.macrx & XCT_MACRX_O))
+			break;
+
+		count++;
+
+		info = NULL;
+
+		/* We have to scan for our skb since there's no way
+		 * to back-map them from the descriptor, and if we
+		 * have several receive channels then they might not
+		 * show up in the same order as they were put on the
+		 * interface ring.
+		 */
+
+		dma = (descr.ptr & XCT_PTR_ADDR_M);
+		for (j = start; j < start+mac->rx->count; j++)
+			if (INFO(mac->rx, j).dma == dma) {
+				info = &INFO(mac->rx, j);
+				break;
+			}
+
+		BUG_ON(!info);
+
+		skb = info->skb;
+
+		len = (descr.macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
+
+		skb_put(skb, len);
+
+		skb->protocol = eth_type_trans(skb, mac->netdev);
+
+		if ((descr.macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
+			skb->ip_summed = CHECKSUM_COMPLETE;
+			skb->csum = (descr.macrx & XCT_MACRX_CSUM_M) >>
+					   XCT_MACRX_CSUM_S;
+		} else
+			skb->ip_summed = CHECKSUM_NONE;
+
+		mac->stats.rx_bytes += len;
+		mac->stats.rx_packets++;
+
+		netif_receive_skb(skb);
+
+		DESCR(mac->rx, i).ptr = 0;
+		DESCR(mac->rx, i).macrx = 0;
+		info->dma = 0;
+		info->skb = 0;
+		mb();
+	}
+
+	mac->rx->next_to_clean += count;
+	pasemi_mac_replenish_rx_ring(mac->netdev);
+
+	return count;
+}
+
+static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
+{
+	int i;
+	struct pasemi_mac_buffer *info;
+	struct pas_dma_xct_descr *dp;
+	int start;
+	int count;
+
+	start = mac->tx->next_to_clean;
+	count = 0;
+
+	for (i = start; i < mac->tx->next_to_use; i++) {
+		dp = &DESCR(mac->tx, i);
+		if (!dp || (dp->mactx & XCT_MACTX_O))
+			break;
+
+		count++;
+
+		info = &INFO(mac->tx, i);
+
+		dev_kfree_skb_irq(info->skb);
+		info->skb = NULL;
+		info->dma = 0;
+		dp->mactx = 0;
+		dp->ptr = 0;
+	}
+	mac->tx->next_to_clean += count;
+	return count;
+}
+
+
+static int pasemi_mac_poll(struct net_device *dev, int *budget)
+{
+	int pkts, limit = min(*budget, dev->quota);
+	struct pasemi_mac *mac = netdev_priv(dev);
+
+	pkts = pasemi_mac_clean_rx(mac, limit);
+
+	if (pkts < limit) {
+		/* all done, no more packets present */
+		netif_rx_complete(dev);
+
+		/* re-enable receive interrupts */
+		pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
+				       PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(1000000));
+		return 0;
+	} else {
+		/* used up our quantum, so reschedule */
+		dev->quota -= pkts;
+		*budget -= pkts;
+		return 1;
+	}
+}
+
+
+static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
+{
+	struct net_device *dev = data;
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int reg;
+
+	netif_rx_schedule(dev);
+	pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
+			       PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0));
+
+	reg = PAS_IOB_DMA_RXCH_RESET_PINTC | PAS_IOB_DMA_RXCH_RESET_SINTC |
+	      PAS_IOB_DMA_RXCH_RESET_DINTC;
+	if (*mac->rx_status & PAS_STATUS_TIMER)
+		reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
+
+	pci_write_config_dword(mac->iob_pdev,
+			       PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
+
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
+{
+	struct net_device *dev = data;
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int reg;
+
+	pasemi_mac_clean_tx(mac);

drivers/net/pasemi_mac.c line 659

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+
+	reg = PAS_IOB_DMA_TXCH_RESET_PINTC | PAS_IOB_DMA_TXCH_RESET_SINTC;
+	if (*mac->tx_status & PAS_STATUS_TIMER)
+		reg |= PAS_IOB_DMA_TXCH_RESET_TINTC;
+
+	pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch),
+			       reg);
+
+	return IRQ_HANDLED;
+}

To do shared IRQ's properly you need to check to see if this is your device IRQ or not. Maybe reading config value?

drivers/net/pasemi_mac.c line 672

Quoted 12 lines
drivers/net/pasemi_mac.c new file +12 -0
+static int __devinit
+pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+{
+	static int index = 0;
+	struct net_device *dev;
+	struct pasemi_mac *mac;
+	int err;
+
+	err = pci_enable_device(pdev);
+	if (err) {
+		dev_err(&pdev->dev, "pasemi_mac: Could not enable device.\n");
+		return -ENODEV;

Please return err instead. This allows propagating a possibly useful value.

drivers/net/pasemi_mac.c line 678

Quoted 6 lines
drivers/net/pasemi_mac.c new file +6 -0
+	}
+	dev = alloc_etherdev(sizeof(struct pasemi_mac));
+	if (dev == NULL) {
+		dev_err(&pdev->dev,
+			"pasemi_mac: Could not allocate ethernet device.\n");
+		return -ENODEV;
	return -ENOMEM;

drivers/net/pasemi_mac.c line 686

Quoted 8 lines
drivers/net/pasemi_mac.c new file +8 -0
+	}
+	SET_MODULE_OWNER(dev);
+
+	pci_set_drvdata(pdev, dev);
+	SET_NETDEV_DEV(dev, &pdev->dev);
+
+	mac = netdev_priv(dev);
+	memset(mac, 0, sizeof(struct pasemi_mac));

Unnecessary, alloc_etherdev already zeros.

drivers/net/pasemi_mac.c line 690

Quoted 3 lines
drivers/net/pasemi_mac.c new file +3 -0
+	mac->pdev = pdev;
+	mac->netdev = dev;
+	mac->dma_pdev = pci_find_device(0x1959, 0xa007, NULL);

Do not use pci_find_device in new code. See comment in kernel doc's. Use pci_get_device instead. You are not checking for error values.

Quoted 30 lines
drivers/net/pasemi_mac.c new file +30 -0
+	mac->iob_pdev = pci_find_device(0x1959, 0xa001, NULL);
+
+	if (!mac->iob_pdev) {
+		dev_err(&pdev->dev, "Can't find I/O Bridge\n");
+		return -ENODEV;
+	}
+
+	/* These should come out of the device tree eventually */
+	mac->dma_txch = index;
+	mac->dma_rxch = index;
+
+	/* We probe GMAC before XAUI, but the DMA interfaces are
+	 * in XAUI, GMAC order.
+	 */
+	if (index < 4)
+		mac->dma_if = index + 2;
+	else
+		mac->dma_if = index - 4;
+	index++;
+
+	switch (pdev->device) {
+	case 0xa005:
+		mac->type = MAC_TYPE_GMAC;
+		break;
+	case 0xa006:
+		mac->type = MAC_TYPE_XAUI;
+		break;
+	default:
+		err = -ENODEV;
+		goto out;

drivers/net/pasemi_mac.c line 730

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	}
+
+	/* get mac addr from device tree */
+	if (pasemi_set_mac_addr(mac)) {
+		err = -ENODEV;
+		goto out;
+	}
+	memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
+
+	strcpy(dev->name, "eth%d");

alloc_etherdev already sets this for dev->name

Quoted 13 lines
drivers/net/pasemi_mac.c new file +13 -0
+	dev->open = pasemi_mac_open;
+	dev->stop = pasemi_mac_close;
+	dev->hard_start_xmit = pasemi_mac_start_tx;
+	dev->get_stats = pasemi_mac_get_stats;
+	dev->set_multicast_list = pasemi_mac_set_rx_mode;
+	dev->weight = 64;
+	dev->poll = pasemi_mac_poll;
+	dev->features = NETIF_F_HW_CSUM;
+
+	/* The dma status structure is located in the I/O bridge, and
+	 * is cache coherent.
+	 */
+	if (!dma_status)

drivers/net/pasemi_mac.c line 754

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+		/* XXXOJN This should come from the device tree */
+		dma_status = __ioremap(0xfd800000, 0x1000, 0);
+
+	mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
+	mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
+
+	err = register_netdev(dev);
+
+	if (err)
+		printk("register_netdev failed with error %d\n", err);

You are leaking netdevice, and all your pci setup needs to be undone.

drivers/net/pasemi_mac.c line 766

Quoted 12 lines
drivers/net/pasemi_mac.c new file +12 -0
+	else
+		printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
+		       "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
+		       dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
+		       mac->dma_if, mac->dma_txch, mac->dma_rxch,
+		       dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
+		       dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
+
+	return err;
+
+out:
+	printk(KERN_ERR "pasemi_mac: init failed\n");

You need to unwind the pci_ stuff.

Quoted 7 lines
drivers/net/pasemi_mac.c new file +7 -0
+	pci_disable_device(pdev);
+	free_netdev(dev);
+	return err;
+}
+
+static struct pci_device_id pasemi_mac_pci_tbl[] = {
+	{ PCI_DEVICE(0x1959, 0xa005) },

drivers/net/pasemi_mac.c line 784

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	{ PCI_DEVICE(0x1959, 0xa006) },
+	{ 0 }
+};
+
+MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
+
+static struct pci_driver pasemi_mac_driver = {
+	.name = "pasemi_mac",
+	.id_table = pasemi_mac_pci_tbl,
+	.probe = pasemi_mac_probe,

Don't you need a remove routine?

drivers/net/pasemi_mac.c line 794

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+};
+
+static void __exit pasemi_mac_cleanup_module(void)
+{
+	pci_unregister_driver(&pasemi_mac_driver);
+}
+
+int pasemi_mac_init_module(void)
+{
+	return pci_module_init(&pasemi_mac_driver);

pci_module_init is marked as obsolete, please use pci_register_driver

Quoted 3 lines
drivers/net/pasemi_mac.c new file +3 -0
+}
+module_init(pasemi_mac_init_module);
+module_exit(pasemi_mac_cleanup_module);
Signature · 1 line
Stephen Hemminger <[email protected]>
#5 lore raw
In reply to Olof Johansson

Olof Johansson <[email protected]> :

Quoted 4 lines

Driver for the PA Semi PWRficient on-chip Ethernet (1/10G)

Basic enablement, will be complemented with performance enhancements over time. PHY support will be added as well.

- The driver does not contain a single SMP locking instruction but
http://www.pasemi.com claims the platform to be multicore. Is the driver really designed to be lockless ?

- Is there really no other choice than constantly accessing the registers of the device through pci_write_config_dword() ? No PCI BAR remappable area ?

- Is there a volunteer to maintain the driver ? If so the MAINTAINERS file should be updated (hint, hint).

- No known public documentation for the hardware ?

Inlined remarks below.

[...]

Quoted 36 lines

Index: merge/drivers/net/pasemi_mac.c
===================================================================

drivers/net/pasemi_mac.c new file +31 -0
@@ -0,0 +1,797 @@
+/*
+ * Copyright (C) 2006-2007 PA Semi, Inc
+ *
+ * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/interrupt.h>
+#include <linux/dmaengine.h>
+#include <linux/delay.h>
+#include <linux/netdevice.h>
+#include <linux/etherdevice.h>
+#include <asm/dma-mapping.h>
+#include <linux/in.h>
+#include <linux/skbuff.h>
+

drivers/net/pasemi_mac.c line 41

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+#include <linux/ip.h>
+#include <linux/tcp.h>
+#include <net/checksum.h>
+
+#include "pasemi_mac.h"
+
+#define INITIAL_RX_RING_SIZE 512
+#define INITIAL_TX_RING_SIZE 512
+
+#define BUF_SIZE 2048

Is there a specific reason for this rather unusual size ?

Quoted 5 lines
drivers/net/pasemi_mac.c new file +5 -0
+
+#define PAS_DMA_MAX_IF     40
+#define PAS_DMA_MAX_RXCH   8
+#define PAS_DMA_MAX_TXCH   8
+

drivers/net/pasemi_mac.c line 56

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+/* XXXOJN these should come out of the device tree some day */
+#define PAS_DMA_CAP_BASE   0xe00d0040
+#define PAS_DMA_CAP_SIZE   0x100
+#define PAS_DMA_COM_BASE   0xe00d0100
+#define PAS_DMA_COM_SIZE   0x100
+
+static irqreturn_t pasemi_mac_tx_intr(int, void *);
+static irqreturn_t pasemi_mac_rx_intr(int, void *);
+static int pasemi_mac_clean_tx(struct pasemi_mac *mac);
+static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit);

Reorder and remove the forward declarations ?

Quoted 21 lines
drivers/net/pasemi_mac.c new file +21 -0
+
+static struct pasdma_status *dma_status;
+
+static int pasemi_set_mac_addr(struct pasemi_mac *mac)
+{
+	struct pci_dev *pdev = mac->pdev;
+	struct device_node *dn = pci_device_to_OF_node(pdev);
+	const u8 *maddr;
+	u8 addr[6];
+
+	if (!dn) {
+		dev_dbg(&pdev->dev,
+			  "No device node for mac, not configuring\n");
+		return -ENOENT;
+	}
+
+	maddr = get_property(dn, "mac-address", NULL);
+	if (maddr == NULL) {
+		dev_warn(&pdev->dev,
+			 "no mac address in device tree, not configuring\n");
+		return -ENOENT;

drivers/net/pasemi_mac.c line 87

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	}
+
+	if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
+		   &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
+		dev_warn(&pdev->dev,
+			 "can't parse mac address, not configuring\n");
+		return -EINVAL;
+	}
+
+	memcpy(mac->mac_addr, addr, sizeof(addr));

Add a check for is_valid_ether_addr() ?

drivers/net/pasemi_mac.c line 97

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	return 0;
+}
+
+static void pasemi_mac_setup_rx_resources(struct net_device *dev)
+{
+	struct pasemi_mac_rxring *ring;
+	struct pasemi_mac *mac = netdev_priv(dev);
+	int chan_id = mac->dma_rxch;
+
+	ring = kzalloc(sizeof(*ring), GFP_KERNEL);

k*alloc can fail. Please check !ring.

Quoted 7 lines
drivers/net/pasemi_mac.c new file +7 -0
+
+	ring->count = INITIAL_RX_RING_SIZE;
+
+	ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer)*ring->count,
+				  GFP_KERNEL);
+
+	/* Allocate descriptors */

drivers/net/pasemi_mac.c line 114

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	ring->desc = (void *)__get_free_pages(GFP_KERNEL,
+				      get_order(ring->count *
+				      sizeof(struct pas_dma_xct_descr)));
+	ring->dma = virt_to_phys(ring->desc);
+	memset(ring->desc, 0, ring->count * sizeof(struct pas_dma_xct_descr));
+
+	ring->buffers = (void *)__get_free_pages(GFP_KERNEL,
+					 get_order(ring->count * sizeof(u64)));
+	ring->buf_dma = virt_to_phys(ring->buffers);
+	memset(ring->buffers, 0, ring->count * sizeof(u64));

Use pci_alloc_consistent() ?

Quoted 9 lines
drivers/net/pasemi_mac.c new file +9 -0
+
+	pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_BASEL(chan_id),
+			       PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
+
+	pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_BASEU(chan_id),
+			       PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
+			       PAS_DMA_RXCHAN_BASEU_SIZ(INITIAL_RX_RING_SIZE >> 2));
+
+	pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_CFG(chan_id),

drivers/net/pasemi_mac.c line 133

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+			       PAS_DMA_RXCHAN_CFG_HBU(1));
+
+	pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXINT_BASEL(mac->dma_if),
+			       PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
+
+	pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXINT_BASEU(mac->dma_if),
+			       PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
+			       PAS_DMA_RXINT_BASEU_SIZ(INITIAL_RX_RING_SIZE >> 3));
+
+	ring->next_to_fill = 0; ring->next_to_clean = 0;

Line feed please.

drivers/net/pasemi_mac.c line 145

Quoted 12 lines
drivers/net/pasemi_mac.c new file +12 -0
+	mac->rx = ring;
+}
+
+
+static void pasemi_mac_setup_tx_resources(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	u32 val;
+	int chan_id = mac->dma_txch;
+	struct pasemi_mac_txring *ring;
+
+	ring = kzalloc(sizeof(*ring), GFP_KERNEL);

k*alloc can fail.

drivers/net/pasemi_mac.c line 157

Quoted 12 lines
drivers/net/pasemi_mac.c new file +12 -0
+
+	ring->count = INITIAL_TX_RING_SIZE;
+
+	ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer)*ring->count,
+				  GFP_KERNEL);
+	/* Allocate descriptors */
+	ring->desc = (void *)__get_free_pages(GFP_KERNEL,
+				      get_order(ring->count *
+				      sizeof(struct pas_dma_xct_descr)));
+	ring->dma = virt_to_phys(ring->desc);
+
+	memset(ring->desc, 0, ring->count * sizeof(struct pas_dma_xct_descr));

Use pci_alloc_consistent() ?

Quoted 5 lines
drivers/net/pasemi_mac.c new file +5 -0
+
+	pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_BASEL(chan_id),
+			       PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
+	val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
+	val |= PAS_DMA_TXCHAN_BASEU_SIZ(INITIAL_TX_RING_SIZE >> 2);

drivers/net/pasemi_mac.c line 172

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+
+	pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_BASEU(chan_id), val);
+
+	pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_CFG(chan_id),
+			       PAS_DMA_TXCHAN_CFG_TY_IFACE |
+			       PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
+			       PAS_DMA_TXCHAN_CFG_UP |
+			       PAS_DMA_TXCHAN_CFG_WT(2));
+
+	ring->next_to_use = 0; ring->next_to_clean = 0;

Line feed please.

drivers/net/pasemi_mac.c line 179

Quoted 7 lines
drivers/net/pasemi_mac.c new file +7 -0
+	mac->tx = ring;
+}
+
+static noinline void pasemi_mac_free_resources(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	int i;

unsigned int is supposed to save some cycles on ppc.

Quoted 57 lines
drivers/net/pasemi_mac.c new file +57 -0
+
+	for (i = 0; i < mac->tx->count; i++) {
+		if (INFO(mac->tx, i).dma) {
+			pr_debug("cleaning tx %d, dma addr %lx\n", i, INFO(mac->tx, i).dma);
+			if (INFO(mac->tx, i).skb)
+				dev_kfree_skb_any(INFO(mac->tx, i).skb);
+			INFO(mac->tx, i).dma = 0;
+			INFO(mac->tx, i).skb = 0;
+			DESCR(mac->tx, i).mactx = 0;
+			DESCR(mac->tx, i).ptr = 0;
+		}
+	}
+
+	/* Add free of all data structures here */
+	free_pages((unsigned long)mac->tx->desc, get_order(
+			mac->tx->count * sizeof(struct pas_dma_xct_descr)));
+
+	kfree(mac->tx);
+	mac->tx = NULL;
+
+	for (i = 0; i < mac->rx->count; i++) {
+		if (INFO(mac->rx, i).dma) {
+			pr_debug("cleaning rx %d, dma addr %lx\n", i, INFO(mac->rx, i).dma);
+			if (INFO(mac->rx, i).skb)
+				dev_kfree_skb_any(INFO(mac->rx, i).skb);
+			INFO(mac->rx, i).dma = 0;
+			INFO(mac->rx, i).skb = 0;
+			DESCR(mac->rx, i).macrx = 0;
+			DESCR(mac->rx, i).ptr = 0;
+		}
+	}
+
+	free_pages((unsigned long)mac->rx->desc, get_order(mac->rx->count *
+		   sizeof(struct pas_dma_xct_descr)));
+
+	free_pages((unsigned long)mac->rx->buffers,
+		   get_order(mac->rx->count * sizeof(u64)));
+
+	kfree(mac->rx);
+	mac->rx = NULL;
+}
+
+static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int i;
+	dma_addr_t dma;
+	struct sk_buff *skb;
+	int start = mac->rx->next_to_fill;
+	int count;
+
+	count = ((mac->rx->next_to_clean & ~7) + mac->rx->count -
+		 mac->rx->next_to_fill) % mac->rx->count;
+
+	if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0)) {
+		pr_debug("first time fill, clean %d fill %d\n",
+			 mac->rx->next_to_clean, mac->rx->next_to_fill);

drivers/net/pasemi_mac.c line 246

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+		count = mac->rx->count - 8;
+	}
+
+	/* Limit so we don't go into the last cache line */
+	count -= 8;
+
+	if (count <= 0)
+		return;
+
+	for (i = start; i < start+count; i++) {
                                ^^^

drivers/net/pasemi_mac.c line 254

Quoted 8 lines
drivers/net/pasemi_mac.c new file +8 -0
+		skb = dev_alloc_skb(BUF_SIZE);
+
+		if (!skb)
+			return;
+
+		skb->dev = dev;
+
+		dma = virt_to_phys(skb->data);

Use pci_map_single() and friends ?

It is described in Documentation/DMA-mapping.txt and widely used in the in-kernel drivers.

[...]

Quoted 2 lines
drivers/net/pasemi_mac.c new file +2 -0
+static int pasemi_mac_open(struct net_device *dev)
+{

[...]

drivers/net/pasemi_mac.c line 344

Quoted 3 lines
drivers/net/pasemi_mac.c new file +3 -0
+	if (ret)
+		printk("request_irq of irq %d failed: %d\n",
+		       mac->dma_pdev->irq + mac->dma_txch, ret);

Missing KERN_XYZ.

drivers/net/pasemi_mac.c line 350

Quoted 6 lines
drivers/net/pasemi_mac.c new file +6 -0
+
+	ret = request_irq(128 + 20 + mac->dma_rxch, &pasemi_mac_rx_intr,
+			  IRQF_DISABLED, "pasemi_mac rx", dev);
+	if (ret)
+		printk("request_irq of irq %d failed: %d\n",
+		       mac->dma_pdev->irq + 20 + mac->dma_rxch, ret);

Missing KERN_XYZ.

[...]

Quoted 24 lines
drivers/net/pasemi_mac.c new file +24 -0
+static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	struct pasemi_mac_txring *txring;
+	u64 flags;
+	dma_addr_t map;
+
+	if (mac->tx->next_to_clean+mac->tx->count == mac->tx->next_to_use)
+		pasemi_mac_clean_tx(mac);
+
+	mac->stats.tx_packets++;
+	mac->stats.tx_bytes += skb->len;
+
+	txring = mac->tx;
+
+	flags = XCT_MACTX_O | XCT_MACTX_ST |
+		XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
+
+	if (skb->ip_summed == CHECKSUM_PARTIAL) {
+		switch (skb->nh.iph->protocol) {
+		case IPPROTO_TCP:
+			flags |= XCT_MACTX_CSUM_TCP;
+			flags |= XCT_MACTX_IPH((skb->h.raw - skb->nh.raw) >> 2);
+			flags |= XCT_MACTX_IPO(skb->nh.raw - skb->data);

drivers/net/pasemi_mac.c line 448

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+			break;
+		case IPPROTO_UDP:
+			flags |= XCT_MACTX_CSUM_UDP;
+			flags |= XCT_MACTX_IPH((skb->h.raw - skb->nh.raw) >> 2);
+			flags |= XCT_MACTX_IPO(skb->nh.raw - skb->data);
+			break;
+		}
+	}
+
+	map = virt_to_phys(skb->data);

Use pci_map_single() and friends ?

Quoted 5 lines
drivers/net/pasemi_mac.c new file +5 -0
+
+	DESCR(txring, txring->next_to_use).mactx = flags |
+						XCT_MACTX_LLEN(skb->len);
+	DESCR(txring, txring->next_to_use).ptr = XCT_PTR_LEN(skb->len) |
+						XCT_PTR_ADDR(map);

drivers/net/pasemi_mac.c line 463

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	INFO(txring, txring->next_to_use).dma = map;
+	INFO(txring, txring->next_to_use).skb = skb;
+	/* XXXOJN Deal with fragmented packets when larger MTU is supported */
+
+	txring->next_to_use++;
+
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
+
+	return NETDEV_TX_OK;

How is the TX process stopped when the ring gets full ?

Quoted 5 lines
drivers/net/pasemi_mac.c new file +5 -0
+}
+
+static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);

drivers/net/pasemi_mac.c line 478

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+
+	return &mac->stats;
+}
+
+static void pasemi_mac_set_rx_mode(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int flags;
+
+	return;

Huh ?

Quoted 6 lines
drivers/net/pasemi_mac.c new file +6 -0
+
+	pci_read_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, &flags);
+
+	/* Set promiscuous */
+	if (dev->flags & IFF_PROMISC)
+		flags |= PAS_MAC_CFG_PCFG_PR;

drivers/net/pasemi_mac.c line 494

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	else
+		flags &= ~PAS_MAC_CFG_PCFG_PR;
+
+	pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
+}
+
+
+static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
+{
+	int i, j;

unsigned int ?

Needlessly wide scope for j...

drivers/net/pasemi_mac.c line 497

Quoted 3 lines
drivers/net/pasemi_mac.c new file +3 -0
+	struct pas_dma_xct_descr descr;
+	struct pasemi_mac_buffer *info;
+	struct sk_buff *skb;

... and for descr/info/skb...

drivers/net/pasemi_mac.c line 501

Quoted 4 lines
drivers/net/pasemi_mac.c new file +4 -0
+	unsigned int len;
+	int start;
+	int count;
+	dma_addr_t dma;

... and for dma.

drivers/net/pasemi_mac.c line 506

Quoted 5 lines
drivers/net/pasemi_mac.c new file +5 -0
+
+	start = mac->rx->next_to_clean;
+	count = 0;
+
+	for (i = start; i < start+mac->rx->count && count < limit; i++) {
                                ^^^

I would not protest against a few parenthesis here and there.

drivers/net/pasemi_mac.c

Quoted 2 lines
drivers/net/pasemi_mac.c new file +2 -0
+		rmb();
+		mb();

rmb() _and_ mb() ?

Btw a scroll of ancient incantation is available in Documentation/memory-barriers.txt btw.

Quoted 11 lines
drivers/net/pasemi_mac.c new file +11 -0
+		descr = DESCR(mac->rx, i);
+		if (!(descr.macrx & XCT_MACRX_O))
+			break;
+
+		count++;
+
+		info = NULL;
+
+		/* We have to scan for our skb since there's no way
+		 * to back-map them from the descriptor, and if we
+		 * have several receive channels then they might not

drivers/net/pasemi_mac.c line 529

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+		 * show up in the same order as they were put on the
+		 * interface ring.
+		 */
+
+		dma = (descr.ptr & XCT_PTR_ADDR_M);
+		for (j = start; j < start+mac->rx->count; j++)
+			if (INFO(mac->rx, j).dma == dma) {
+				info = &INFO(mac->rx, j);
+				break;
+			}

This is not a single line statement: please add curly-braces.

[...]

Quoted 3 lines
drivers/net/pasemi_mac.c new file +3 -0
+static int __devinit
+pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+{

[..]

drivers/net/pasemi_mac.c line 746

Quoted 6 lines
drivers/net/pasemi_mac.c new file +6 -0
+	/* The dma status structure is located in the I/O bridge, and
+	 * is cache coherent.
+	 */
+	if (!dma_status)
+		/* XXXOJN This should come from the device tree */
+		dma_status = __ioremap(0xfd800000, 0x1000, 0);

Is this address really set in stone or can it be retrieved after some pci_get_device(...) practice ?

drivers/net/pasemi_mac.c line 749

Quoted 3 lines
drivers/net/pasemi_mac.c new file +3 -0
+
+	mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
+	mac->tx_status = &dma_status->tx_sta[mac->dma_txch];

Addresses returned from ioremap are not guaranteed to be dereferencable like that.

drivers/net/pasemi_mac.c line 754

Quoted 5 lines
drivers/net/pasemi_mac.c new file +5 -0
+
+	err = register_netdev(dev);
+
+	if (err)
+		printk("register_netdev failed with error %d\n", err);

Missing KERN_XYZ.

Quoted 11 lines
drivers/net/pasemi_mac.c new file +11 -0
+	else
+		printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
+		       "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
+		       dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
+		       mac->dma_if, mac->dma_txch, mac->dma_rxch,
+		       dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
+		       dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
+
+	return err;
+
+out:

drivers/net/pasemi_mac.c line 775

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	printk(KERN_ERR "pasemi_mac: init failed\n");
+
+	pci_disable_device(pdev);
+	free_netdev(dev);
+	return err;
+}
+
+static struct pci_device_id pasemi_mac_pci_tbl[] = {
+	{ PCI_DEVICE(0x1959, 0xa005) },
+	{ PCI_DEVICE(0x1959, 0xa006) },

Minor nit: just use a #define for the vendor ID and you will simply submit a one-line removal the day pci_ids is updated.

drivers/net/pasemi_mac.c line 784

Quoted 9 lines
drivers/net/pasemi_mac.c new file +9 -0
+	{ 0 }
+};
+
+MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
+
+static struct pci_driver pasemi_mac_driver = {
+	.name = "pasemi_mac",
+	.id_table = pasemi_mac_pci_tbl,
+	.probe = pasemi_mac_probe,
	.name		= "pasemi_mac",
	.id_table	= pasemi_mac_pci_tbl,
	.probe		= pasemi_mac_probe,

[...]

Quoted 4 lines

Index: merge/drivers/net/pasemi_mac.h
===================================================================

drivers/net/pasemi_mac.h new file +0 -0

[...]

drivers/net/pasemi_mac.h line 61

Quoted 11 lines
drivers/net/pasemi_mac.h new file +11 -0
+/* Number of unused descriptors, considering ring wraparounds */
+#define PASEMI_MAC_DESC_UNUSED(ring) ((((ring)->next_to_clean >		\
+					(ring)->next_to_use) ?		\
+					  0 :				\
+					  (ring)->count) +		\
+					  (ring)->next_to_clean -	\
+					  (ring)->next_to_use - 1)
+
+#define DESCR(ring, i) ((ring)->desc[i % ((ring)->count)])
+#define BUFF(ring, i) ((ring)->buffers[i % ((ring)->count)])
+#define INFO(ring, i) ((ring)->desc_info[i % ((ring)->count)])

A bit ugly/obfuscating/name clash prone imvho.

Use local variables ?

drivers/net/pasemi_mac.h line 72

Quoted 11 lines
drivers/net/pasemi_mac.h new file +11 -0
+
+struct pasemi_mac {
+	struct net_device *netdev;
+	struct pci_dev *pdev;
+	struct pci_dev *dma_pdev;
+	struct pci_dev *iob_pdev;
+	struct net_device_stats stats;
+
+	/* Pointer to the cacheable per-channel status registers */
+	uint64_t	*rx_status;
+	uint64_t	*tx_status;

No uintxy_t please. Use plain u64.

Signature · 1 line
Ueimor
#2 lore raw
In reply to Stephen Hemminger
Quoted 1 line · Stephen Hemminger

Basic initalization, setup comments.

Thanks, fixes have been incorporated and will be reposted. Most of them was obviously just my lack of diligence. See however the two below.

Quoted 12 lines
Quoted 12 lines
drivers/net/pasemi_mac.c new file +12 -0
+static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
+{
+	struct net_device *dev = data;
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int reg;
+
+	pasemi_mac_clean_tx(mac);
+
+	reg = PAS_IOB_DMA_TXCH_RESET_PINTC | PAS_IOB_DMA_TXCH_RESET_SINTC;
+	if (*mac->tx_status & PAS_STATUS_TIMER)
+		reg |= PAS_IOB_DMA_TXCH_RESET_TINTC;
+
Quoted 8 lines

drivers/net/pasemi_mac.c line 659

Quoted 5 lines
drivers/net/pasemi_mac.c new file +5 -0
+	pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch),
+			       reg);
+
+	return IRQ_HANDLED;
+}

To do shared IRQ's properly you need to check to see if this is your device IRQ or not. Maybe reading config value?

Right now it's guaranteed that the interrupts will not be shared. They're fixed for the on-chip devices, and no other driver should be binding to the same channels (and thus irqs).

If it changes in the future, the driver would need other rework as well.

Quoted 7 lines

drivers/net/pasemi_mac.c line 784

Quoted 5 lines
drivers/net/pasemi_mac.c new file +5 -0
+
+static struct pci_driver pasemi_mac_driver = {
+   .name = "pasemi_mac",
+   .id_table = pasemi_mac_pci_tbl,
+   .probe = pasemi_mac_probe,

Don't you need a remove routine?

No hotplug support at this time, so I didn't see any use in providing one.

-Olof

#6 lore raw
In reply to Francois Romieu

Hi,

Thanks for the comments. In general I have applied them, with some specific comments below.

I'll repost a new version of the driver based on this and other feedback later tonight.

Quoted 9 lines · Francois Romieu

Olof Johansson <[email protected]> :

Quoted 4 lines

Driver for the PA Semi PWRficient on-chip Ethernet (1/10G)

Basic enablement, will be complemented with performance enhancements over time. PHY support will be added as well.

- The driver does not contain a single SMP locking instruction but
http://www.pasemi.com claims the platform to be multicore. Is the driver really designed to be lockless ?

Unless I misunderstood something, NAPI drivers that don't set NETIF_F_LLTX will have all locking taken care of by higher layers, no?

Quoted 3 lines

- Is there really no other choice than constantly accessing the registers of the device through pci_write_config_dword() ? No PCI BAR remappable area ?

Maybe a bit of introduction could be useful (also regarding the pci_map/alloc comments below).

Our devices are on-chip, and while they're not on a PCI(e) bus internally, they do have config headers and will show up as devices on a pseudo-bus (the root one, in fact).

Also, while the driver could go through the IOMMU layers, there's no real use in doing so at this time.

When it comes to register access -- it would probably make sense to remap them separately and use normal accessors instead of going through the quite heavyweight PCI config accessors. I used them right now for convenience.

As I already mentioned, there's not been a whole lot of performance work done on this driver yet; I expect to address this when I get started on that.

Quoted 2 lines

- Is there a volunteer to maintain the driver ? If so the MAINTAINERS file should be updated (hint, hint).

Yep, forgot to include it.

Quoted 1 line

- No known public documentation for the hardware ?

Not at this time, but the driver will be actively maintained so it shouldn't be an issue.

Quoted 1 line

Inlined remarks below.

Comments to some of them below.

Quoted 3 lines
Quoted 1 line

+#define BUF_SIZE 2048

Is there a specific reason for this rather unusual size ?

A nice and round and large enough number. But no, no real reason. Fixed.

(And yes, large MTU support is also on the todo list. :-)

Quoted 6 lines

drivers/net/pasemi_mac.c line 114

Quoted 4 lines
drivers/net/pasemi_mac.c new file +4 -0
+	ring->buffers = (void *)__get_free_pages(GFP_KERNEL,
+					 get_order(ring->count * sizeof(u64)));
+	ring->buf_dma = virt_to_phys(ring->buffers);
+	memset(ring->buffers, 0, ring->count * sizeof(u64));

Use pci_alloc_consistent() ?

Nope, for reasons above.

Quoted 6 lines

drivers/net/pasemi_mac.c line 179

Quoted 4 lines
drivers/net/pasemi_mac.c new file +4 -0
+static noinline void pasemi_mac_free_resources(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	int i;

unsigned int is supposed to save some cycles on ppc.

Who told you that? That's not true.
Still, there's no need for the iterator to be signed.

Quoted 6 lines

drivers/net/pasemi_mac.c line 344

Quoted 3 lines
drivers/net/pasemi_mac.c new file +3 -0
+	if (ret)
+		printk("request_irq of irq %d failed: %d\n",
+		       mac->dma_pdev->irq + mac->dma_txch, ret);

Missing KERN_XYZ.

Changed all the printk's to be dev_*() instead based on Stephen's comments.

Quoted 8 lines

drivers/net/pasemi_mac.c line 478

Quoted 6 lines
drivers/net/pasemi_mac.c new file +6 -0
+static void pasemi_mac_set_rx_mode(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int flags;
+
+	return;

Huh ?

Yeah, forgot it there from debugging. Can't even remember why I added it and I obviously missed it when going through before posting.

Quoted 5 lines

drivers/net/pasemi_mac.c line 506

Quoted 2 lines
drivers/net/pasemi_mac.c new file +2 -0
+
+	for (i = start; i < start+mac->rx->count && count < limit; i++) {
                                ^^^

I would not protest against a few parenthesis here and there.

Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		rmb();
+		mb();

rmb() _and_ mb() ?

Btw a scroll of ancient incantation is available in Documentation/memory-barriers.txt btw.

Not sure why they were still left in there. Only rmb is needed. Same for the barrier at the bottom of the loop.

Quoted 4 lines
Quoted 3 lines
drivers/net/pasemi_mac.c new file +3 -0
+static int __devinit
+pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+{

[..]

Quoted 9 lines

drivers/net/pasemi_mac.c line 746

Quoted 6 lines
drivers/net/pasemi_mac.c new file +6 -0
+	/* The dma status structure is located in the I/O bridge, and
+	 * is cache coherent.
+	 */
+	if (!dma_status)
+		/* XXXOJN This should come from the device tree */
+		dma_status = __ioremap(0xfd800000, 0x1000, 0);

Is this address really set in stone or can it be retrieved after some pci_get_device(...) practice ?

As the comment says -- one day it should come out of the device tree. It's a well-known and not dynamic address on the current chips; but it might be located somewhere else on future products.

Quoted 5 lines

drivers/net/pasemi_mac.c line 749

Quoted 2 lines
drivers/net/pasemi_mac.c new file +2 -0
+	mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
+	mac->tx_status = &dma_status->tx_sta[mac->dma_txch];

Addresses returned from ioremap are not guaranteed to be dereferencable like that.

That's why I'm using __ioremap instead, to get a cacheable regular area to just reference.

Is there another preferred method of doing this? Note that it is a cache-coherent status area, so regular ioremap() is not the solution.

Quoted 5 lines

drivers/net/pasemi_mac.c line 775

Quoted 2 lines
drivers/net/pasemi_mac.c new file +2 -0
+	{ PCI_DEVICE(0x1959, 0xa005) },
+	{ PCI_DEVICE(0x1959, 0xa006) },

Minor nit: just use a #define for the vendor ID and you will simply submit a one-line removal the day pci_ids is updated.

I'll just include the vendor ID in this patch instead.

Quoted 7 lines

drivers/net/pasemi_mac.h line 61

Quoted 3 lines
drivers/net/pasemi_mac.h new file +3 -0
+#define DESCR(ring, i) ((ring)->desc[i % ((ring)->count)])
+#define BUFF(ring, i) ((ring)->buffers[i % ((ring)->count)])
+#define INFO(ring, i) ((ring)->desc_info[i % ((ring)->count)])

A bit ugly/obfuscating/name clash prone imvho.

Use local variables ?

I'm open for suggestions here, not sure how local variables will help though?

-Olof

#11 lore raw

PATCH v2 1/1 PA Semi PWRficient Ethernet driver

In reply to Olof Johansson

Driver for the PA Semi PWRficient on-chip Ethernet (1/10G)

Basic enablement, will be complemented with performance enhancements over time. PHY support will be added as well.

This patch still uses numerical PCI IDs, they will be replaced when the pci_ids.h change goes in, together with other currently pending drivers.

---

This version contains changes based on comments from Stephen Hemminger and Francois Romieu.

drivers/net/Kconfig +7 -0
Index: merge/drivers/net/Kconfig
===================================================================
@@ -2348,6 +2348,13 @@ config QLA3XXX
 	  To compile this driver as a module, choose M here: the module
 	  will be called qla3xxx.
 
+config PASEMI_MAC
+	tristate "PA Semi 1/10Gbit MAC"
+	depends on PPC64 && PCI
+	help
+	  This driver supports the on-chip 1/10Gbit Ethernet controller on
+	  PA Semi's PWRficient line of chips.
+
 endmenu
 
 #
drivers/net/Makefile +1 -0
Index: merge/drivers/net/Makefile
===================================================================
@@ -196,6 +196,7 @@ obj-$(CONFIG_SMC91X) += smc91x.o
 obj-$(CONFIG_SMC911X) += smc911x.o
 obj-$(CONFIG_DM9000) += dm9000.o
 obj-$(CONFIG_FEC_8XX) += fec_8xx/
+obj-$(CONFIG_PASEMI_MAC) += pasemi_mac.o
 
 obj-$(CONFIG_MACB) += macb.o
 
drivers/net/pasemi_mac.c new file +875 -0

875 lines not on this page · raw message

drivers/net/pasemi_mac.h new file +442 -0

442 lines not on this page · raw message

MAINTAINERS +6 -0
Index: merge/MAINTAINERS
===================================================================
@@ -2484,6 +2484,12 @@ L:	[email protected]
 W:	http://www.nongnu.org/orinoco/
 S:	Maintained
 
+PA SEMI ETHERNET DRIVER
+P:	Olof Johansson
+M:	[email protected]
+L:	[email protected]
+S:	Maintained
+
 PARALLEL PORT SUPPORT
 P:	Phil Blundell
 M:	[email protected]
include/linux/pci_ids.h +2 -0
Index: merge/include/linux/pci_ids.h
===================================================================
@@ -2064,6 +2064,8 @@
 #define PCI_VENDOR_ID_TDI               0x192E
 #define PCI_DEVICE_ID_TDI_EHCI          0x0101
 
+#define PCI_VENDOR_ID_PASEMI		0x1959
+
 #define PCI_VENDOR_ID_JMICRON		0x197B
 #define PCI_DEVICE_ID_JMICRON_JMB360	0x2360
 #define PCI_DEVICE_ID_JMICRON_JMB361	0x2361
#3 lore raw
In reply to Olof Johansson
Quoted 26 lines · Olof Johansson
Quoted 1 line · Stephen Hemminger

Basic initalization, setup comments.

Thanks, fixes have been incorporated and will be reposted. Most of them was obviously just my lack of diligence. See however the two below.

Quoted 11 lines
Quoted 11 lines
drivers/net/pasemi_mac.c new file +11 -0
+static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
+{
+	struct net_device *dev = data;
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int reg;
+
+	pasemi_mac_clean_tx(mac);
+
+	reg = PAS_IOB_DMA_TXCH_RESET_PINTC | PAS_IOB_DMA_TXCH_RESET_SINTC;
+	if (*mac->tx_status & PAS_STATUS_TIMER)
+		reg |= PAS_IOB_DMA_TXCH_RESET_TINTC;
Quoted 8 lines

drivers/net/pasemi_mac.c line 659

Quoted 6 lines
drivers/net/pasemi_mac.c new file +6 -0
+
+	pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch),
+			       reg);
+
+	return IRQ_HANDLED;
+}

To do shared IRQ's properly you need to check to see if this is your device IRQ or not. Maybe reading config value?

Quoted 5 lines

Right now it's guaranteed that the interrupts will not be shared. They're fixed for the on-chip devices, and no other driver should be binding to the same channels (and thus irqs).

If it changes in the future, the driver would need other rework as well.

Nonetheless, it is far more sane to check for work, and return if no work. Who knows if the hardware will signal an interrupt early or late.

Quoted 7 lines
Quoted 5 lines

drivers/net/pasemi_mac.c line 784

Quoted 4 lines
drivers/net/pasemi_mac.c new file +4 -0
+static struct pci_driver pasemi_mac_driver = {
+   .name = "pasemi_mac",
+   .id_table = pasemi_mac_pci_tbl,
+   .probe = pasemi_mac_probe,

Don't you need a remove routine?

No hotplug support at this time, so I didn't see any use in providing one.

module remove.

Jeff

#21 lore raw
In reply to Olof Johansson
Quoted 10 lines · Olof Johansson

Driver for the PA Semi PWRficient on-chip Ethernet (1/10G)

Basic enablement, will be complemented with performance enhancements over time. PHY support will be added as well.

This patch still uses the numerical PCI vendor id, it will be replaced when the pci_ids.h change goes in (same as the other currently pending drivers).

From a quick glance over the code you don't seem to handle memory allocation

erros at all, that needs some fixing.

#7 lore raw
In reply to Olof Johansson
Quoted 9 lines · Olof Johansson

Maybe a bit of introduction could be useful (also regarding the pci_map/alloc comments below).

Our devices are on-chip, and while they're not on a PCI(e) bus internally, they do have config headers and will show up as devices on a pseudo-bus (the root one, in fact).

Also, while the driver could go through the IOMMU layers, there's no real use in doing so at this time.

sorry, but bypassing this is not what what we want in drivers at all. In case you have an iommu and don't want it on the root bus chose the noop dma implementation for this bus, which is easily possible on powerpc.

#8 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 7 lines · Olof Johansson

Maybe a bit of introduction could be useful (also regarding the pci_map/alloc comments below).

Our devices are on-chip, and while they're not on a PCI(e) bus internally, they do have config headers and will show up as devices on a pseudo-bus (the root one, in fact).

Quoted 7 lines
Quoted 2 lines

Also, while the driver could go through the IOMMU layers, there's no real use in doing so at this time.

sorry, but bypassing this is not what what we want in drivers at all. In case you have an iommu and don't want it on the root bus chose the noop dma implementation for this bus, which is easily possible on powerpc.

Good point, I'll do that instead.

-Olof

#22 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Olof Johansson

Driver for the PA Semi PWRficient on-chip Ethernet (1/10G)

Basic enablement, will be complemented with performance enhancements over time. PHY support will be added as well.

Quoted 6 lines
Quoted 5 lines

This patch still uses the numerical PCI vendor id, it will be replaced when the pci_ids.h change goes in (same as the other currently pending drivers).

From a quick glance over the code you don't seem to handle memory allocation

erros at all, that needs some fixing.

v2 that I posted as a comment to the first one should have taken care of most cases. I'll go through once more and see if I missed any before posting a revised version later today.

Thanks,

-Olof

#4 lore raw
In reply to Jeff Garzik
Quoted 9 lines · Jeff Garzik
Quoted 5 lines · Olof Johansson

Right now it's guaranteed that the interrupts will not be shared. They're fixed for the on-chip devices, and no other driver should be binding to the same channels (and thus irqs).

If it changes in the future, the driver would need other rework as well.

Nonetheless, it is far more sane to check for work, and return if no work. Who knows if the hardware will signal an interrupt early or late.

Easy enough. Added in the next version.

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

drivers/net/pasemi_mac.c line 784

Quoted 4 lines
drivers/net/pasemi_mac.c new file +4 -0
+static struct pci_driver pasemi_mac_driver = {
+   .name = "pasemi_mac",
+   .id_table = pasemi_mac_pci_tbl,
+   .probe = pasemi_mac_probe,

Don't you need a remove routine?

No hotplug support at this time, so I didn't see any use in providing one.

module remove.

Oh, good point. Added in the next version to be posted.

Thanks,

-Olof

#9 lore raw
In reply to Olof Johansson

Olof Johansson <[email protected]> :

Quoted 1 line · Francois Romieu

[...]

Quoted 6 lines
Quoted 3 lines

- The driver does not contain a single SMP locking instruction but
http://www.pasemi.com claims the platform to be multicore. Is the driver really designed to be lockless ?

Unless I misunderstood something, NAPI drivers that don't set NETIF_F_LLTX will have all locking taken care of by higher layers, no?

It is not necessarily _that_ simple (it would be cool though :o) ).

For instance, what does prevent pasemi_mac_clean_tx() to be issued from IRQ context (pasemi_mac_tx_intr) and from the xmit handler (pasemi_mac_start_tx) at the same time ?

[...]

Quoted 3 lines
Quoted 1 line

unsigned int is supposed to save some cycles on ppc.

Who told you that? That's not true.

Jon D Mason <[email protected]> on 25/08/2004 about ppc64 (not ppc, sorry).

[...]

Quoted 9 lines
Quoted 7 lines

drivers/net/pasemi_mac.h line 61

Quoted 3 lines
drivers/net/pasemi_mac.h new file +3 -0
+#define DESCR(ring, i) ((ring)->desc[i % ((ring)->count)])
+#define BUFF(ring, i) ((ring)->buffers[i % ((ring)->count)])
+#define INFO(ring, i) ((ring)->desc_info[i % ((ring)->count)])

A bit ugly/obfuscating/name clash prone imvho.

Use local variables ?

I'm open for suggestions here, not sure how local variables will help though?

	struct pas_dma_xct_descr *desc = ring->desc[i % ring->count];
Signature · 1 line
Ueimor
#10 lore raw
Quoted 7 lines · Francois Romieu

Olof Johansson <[email protected]> :

Quoted 1 line · Francois Romieu

[...]

Quoted 4 lines
Quoted 3 lines

- The driver does not contain a single SMP locking instruction but
http://www.pasemi.com claims the platform to be multicore. Is the driver really designed to be lockless ?

Quoted 8 lines
Quoted 2 lines

Unless I misunderstood something, NAPI drivers that don't set NETIF_F_LLTX will have all locking taken care of by higher layers, no?

It is not necessarily _that_ simple (it would be cool though :o) ).

For instance, what does prevent pasemi_mac_clean_tx() to be issued from IRQ context (pasemi_mac_tx_intr) and from the xmit handler (pasemi_mac_start_tx) at the same time ?

You're right. Bummer. I'll add locking on the rings.

Quoted 6 lines

[...]

Quoted 3 lines
Quoted 1 line

unsigned int is supposed to save some cycles on ppc.

Who told you that? That's not true.

Jon D Mason <[email protected]> on 25/08/2004 about ppc64 (not ppc, sorry).

Interesting, I hadn't thought about that before.

There's nothing architectural in PPC that makes signed math slower than unsigned, but in the case of modulo operations (which we do alot on the rings), unsigned is per definition more complex to do the operations on.

It's pretty much within the noise on the current implementation, but still an interesting tidbit. Thanks.

Quoted 7 lines

[...]

Quoted 6 lines
Quoted 6 lines

drivers/net/pasemi_mac.h line 61

Quoted 3 lines
drivers/net/pasemi_mac.h new file +3 -0
+#define DESCR(ring, i) ((ring)->desc[i % ((ring)->count)])
+#define BUFF(ring, i) ((ring)->buffers[i % ((ring)->count)])
+#define INFO(ring, i) ((ring)->desc_info[i % ((ring)->count)])

A bit ugly/obfuscating/name clash prone imvho.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Use local variables ?

I'm open for suggestions here, not sure how local variables will help though?

	struct pas_dma_xct_descr *desc = ring->desc[i % ring->count];

That makes sense. Done.

-Olof

#12 lore raw

PATCH v3 1/1 PA Semi PWRficient Ethernet driver

In reply to Olof Johansson

Driver for the PA Semi PWRficient on-chip Ethernet (1/10G)

Basic enablement, will be complemented with performance enhancements over time. PHY support will be added as well.

---

Further improvements based on comments:

* Added remove function
* Checking interrupt status in handler
* Misc cleanups w.r.t. ring handing (INFO/DESCR/BUFF are gone)
* Added locking of the rings
* Using PCI DMA for all buffers

Misc other changes while I was touching the code:

* Setting the interrupt descriptor field to include interface number
* Added PCI vendor ID, it's been submitted to sf.net as well.
* Moved Kconfig entry to the 10GbE section

drivers/net/Kconfig +7 -0
Index: merge/drivers/net/Kconfig
===================================================================
@@ -2488,6 +2488,13 @@ config NETXEN_NIC
 	help
 	  This enables the support for NetXen's Gigabit Ethernet card.
 
+config PASEMI_MAC
+	tristate "PA Semi 1/10Gbit MAC"
+	depends on PPC64 && PCI
+	help
+	  This driver supports the on-chip 1/10Gbit Ethernet controller on
+	  PA Semi's PWRficient line of chips.
+
 endmenu
 
 source "drivers/net/tokenring/Kconfig"
drivers/net/Makefile +1 -0
Index: merge/drivers/net/Makefile
===================================================================
@@ -196,6 +196,7 @@ obj-$(CONFIG_SMC91X) += smc91x.o
 obj-$(CONFIG_SMC911X) += smc911x.o
 obj-$(CONFIG_DM9000) += dm9000.o
 obj-$(CONFIG_FEC_8XX) += fec_8xx/
+obj-$(CONFIG_PASEMI_MAC) += pasemi_mac.o
 
 obj-$(CONFIG_MACB) += macb.o
 
drivers/net/pasemi_mac.c new file +963 -0

963 lines not on this page · raw message

drivers/net/pasemi_mac.h new file +443 -0

443 lines not on this page · raw message

MAINTAINERS +6 -0
Index: merge/MAINTAINERS
===================================================================
@@ -2484,6 +2484,12 @@ L:	[email protected]
 W:	http://www.nongnu.org/orinoco/
 S:	Maintained
 
+PA SEMI ETHERNET DRIVER
+P:	Olof Johansson
+M:	[email protected]
+L:	[email protected]
+S:	Maintained
+
 PARALLEL PORT SUPPORT
 P:	Phil Blundell
 M:	[email protected]
include/linux/pci_ids.h +2 -0
Index: merge/include/linux/pci_ids.h
===================================================================
@@ -2064,6 +2064,8 @@
 #define PCI_VENDOR_ID_TDI               0x192E
 #define PCI_DEVICE_ID_TDI_EHCI          0x0101
 
+#define PCI_VENDOR_ID_PASEMI		0x1959
+
 #define PCI_VENDOR_ID_JMICRON		0x197B
 #define PCI_DEVICE_ID_JMICRON_JMB360	0x2360
 #define PCI_DEVICE_ID_JMICRON_JMB361	0x2361
#13 lore raw
Quoted 6 lines · Olof Johansson

Driver for the PA Semi PWRficient on-chip Ethernet (1/10G)

Basic enablement, will be complemented with performance enhancements over time. PHY support will be added as well.

Looks generally pretty clean, well done.

Comments included inline...

Quoted 6 lines
drivers/net/pasemi_mac.c new file +6 -0
+#include "pasemi_mac.h"
+
+#define INITIAL_RX_RING_SIZE 512
+#define INITIAL_TX_RING_SIZE 512
+
+#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */

drivers/net/pasemi_mac.c line 51

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+
+#define PAS_DMA_MAX_IF     40
+#define PAS_DMA_MAX_RXCH   8
+#define PAS_DMA_MAX_TXCH   8
+
+/* XXXOJN these should come out of the device tree some day */
+#define PAS_DMA_CAP_BASE   0xe00d0040
+#define PAS_DMA_CAP_SIZE   0x100
+#define PAS_DMA_COM_BASE   0xe00d0100
+#define PAS_DMA_COM_SIZE   0x100

consider enums rather than #define's for constants. they generate symbols at the C level rather than cpp level, making the code more readable, providing more type information to the C compiler, and making symbols visible at the debugger level.

example:

enum {
	PAS_DMA_MAX_IF		= 40,
	PAS_DMA_MAX_RXCH	= 8,
	PAS_DMA_MAX_TXCH	= 8,
};
Quoted 1 line

+static int pasemi_set_mac_addr(struct pasemi_mac *mac)

poor name. from the context of the code reader and driver, this should be "pasemi_GET_mac_addr", rather than ...set... ;

Quoted 42 lines
drivers/net/pasemi_mac.c new file +42 -0
+	struct pci_dev *pdev = mac->pdev;
+	struct device_node *dn = pci_device_to_OF_node(pdev);
+	const u8 *maddr;
+	u8 addr[6];
+
+	if (!dn) {
+		dev_dbg(&pdev->dev,
+			  "No device node for mac, not configuring\n");
+		return -ENOENT;
+	}
+
+	maddr = get_property(dn, "mac-address", NULL);
+	if (maddr == NULL) {
+		dev_warn(&pdev->dev,
+			 "no mac address in device tree, not configuring\n");
+		return -ENOENT;
+	}
+
+	if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
+		   &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
+		dev_warn(&pdev->dev,
+			 "can't parse mac address, not configuring\n");
+		return -EINVAL;
+	}
+
+	memcpy(mac->mac_addr, addr, sizeof(addr));
+	return 0;
+}
+
+
+static noinline void pasemi_mac_free_tx_resources(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int i;
+	struct pasemi_mac_buffer *info;
+	struct pas_dma_xct_descr *dp;
+
+	for (i = 0; i < mac->tx->count; i++) {
+		info = &mac->tx->desc_info[i % mac->tx->count];
+		dp = &mac->tx->desc[i % mac->tx->count];
+		if (info->dma) {
+			if (info->skb) {

drivers/net/pasemi_mac.c

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+				pci_unmap_single(mac->dma_pdev,
+						 info->dma,
+						 info->skb->len,
+						 PCI_DMA_TODEVICE);
+				dev_kfree_skb_any(info->skb);
+			}
+			info->dma = 0;
+			info->skb = 0;
+			dp->mactx = 0;
+			dp->ptr = 0;

"0" is not the same as "NULL". Use NULL where appropriate.

Then make sure your driver passes sparse checks (read Documentation/sparse.txt)

Quoted 21 lines
drivers/net/pasemi_mac.c new file +21 -0
+	dma_free_coherent(&mac->dma_pdev->dev,
+			  mac->tx->count * sizeof(struct pas_dma_xct_descr),
+			  mac->tx->desc, mac->tx->dma);
+
+	kfree(mac->tx->desc_info);
+	kfree(mac->tx);
+	mac->tx = NULL;
+}
+
+static noinline void pasemi_mac_free_rx_resources(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int i;
+	struct pasemi_mac_buffer *info;
+	struct pas_dma_xct_descr *dp;
+
+	for (i = 0; i < mac->rx->count; i++) {
+		info = &mac->rx->desc_info[i % mac->rx->count];
+		dp = &mac->rx->desc[i % mac->rx->count];
+		if (info->dma) {
+			if (info->skb) {

drivers/net/pasemi_mac.c line 278

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+				pci_unmap_single(mac->dma_pdev,
+						 info->dma,
+						 info->skb->len,
+						 PCI_DMA_FROMDEVICE);
+				dev_kfree_skb_any(info->skb);
+			}
+			info->dma = 0;
+			info->skb = 0;
+			dp->macrx = 0;
+			dp->ptr = 0;

0 != NULL

Quoted 11 lines
drivers/net/pasemi_mac.c new file +11 -0
+	dma_free_coherent(&mac->dma_pdev->dev,
+			  mac->rx->count * sizeof(struct pas_dma_xct_descr),
+			  mac->rx->desc, mac->rx->dma);
+
+	dma_free_coherent(&mac->dma_pdev->dev, mac->rx->count * sizeof(u64),
+			  mac->rx->buffers, mac->rx->buf_dma);
+
+	kfree(mac->rx->desc_info);
+	kfree(mac->rx);
+	mac->rx = NULL;
+}

drivers/net/pasemi_mac.c line 302

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+
+static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int i;
+	int start = mac->rx->next_to_fill;
+	int count;
+
+	count = ((mac->rx->next_to_clean & ~7) + mac->rx->count -
+		 mac->rx->next_to_fill) % mac->rx->count;

if feasible, logical operations are often more optimal than '%'

maybe you need something like tg3.c's NEXT_TX() ?

drivers/net/pasemi_mac.c line 305

Quoted 2 lines
drivers/net/pasemi_mac.c new file +2 -0
+	if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0))
+		count = mac->rx->count - 8;

why this is needed?

Quoted 11 lines
drivers/net/pasemi_mac.c new file +11 -0
+	/* Limit so we don't go into the last cache line */
+	count -= 8;
+
+	if (count <= 0)
+		return;
+
+	for (i = start; i < start + count; i++) {
+		struct pasemi_mac_buffer *info = &mac->rx->desc_info[i % mac->rx->count];
+		u64 *buff = &mac->rx->buffers[i % mac->rx->count];
+		struct sk_buff *skb;
+		dma_addr_t dma;

drivers/net/pasemi_mac.c line 327

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+
+		skb = dev_alloc_skb(BUF_SIZE);
+
+		if (!skb)
+			return;
+
+		skb->dev = dev;
+
+		dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
+				     PCI_DMA_FROMDEVICE);

check for DMA mapping error

Quoted 79 lines
drivers/net/pasemi_mac.c new file +79 -0
+		info->skb = skb;
+		info->dma = dma;
+		*buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
+	}
+
+	wmb();
+
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_RXCHAN_INCR(mac->dma_rxch),
+			       count);
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_RXINT_INCR(mac->dma_if),
+			       count);
+
+	mac->rx->next_to_fill += count;
+}
+
+static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
+{
+	unsigned int i;
+	int start, count;
+
+	spin_lock(&mac->rx->lock);
+
+	start = mac->rx->next_to_clean;
+	count = 0;
+
+	for (i = start; i < (start + mac->rx->count) && count < limit; i++) {
+		struct pas_dma_xct_descr *dp;
+		struct pasemi_mac_buffer *info;
+		struct sk_buff *skb;
+		unsigned int j, len;
+		dma_addr_t dma;
+
+		rmb();
+
+		dp = &mac->rx->desc[i % mac->rx->count];
+
+		if (!(dp->macrx & XCT_MACRX_O))
+			break;
+
+		count++;
+
+		info = NULL;
+
+		/* We have to scan for our skb since there's no way
+		 * to back-map them from the descriptor, and if we
+		 * have several receive channels then they might not
+		 * show up in the same order as they were put on the
+		 * interface ring.
+		 */
+
+		dma = (dp->ptr & XCT_PTR_ADDR_M);
+		for (j = start; j < (start + mac->rx->count); j++) {
+			info = &mac->rx->desc_info[j % mac->rx->count];
+			if (info->dma == dma)
+				break;
+		}
+
+		BUG_ON(!info);
+		BUG_ON(info->dma != dma);
+
+		pci_unmap_single(mac->dma_pdev, info->dma, info->skb->len,
+				 PCI_DMA_FROMDEVICE);
+
+		skb = info->skb;
+
+		len = (dp->macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
+
+		skb_put(skb, len);
+
+		skb->protocol = eth_type_trans(skb, mac->netdev);
+
+		if ((dp->macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
+			skb->ip_summed = CHECKSUM_COMPLETE;
+			skb->csum = (dp->macrx & XCT_MACRX_CSUM_M) >>
+					   XCT_MACRX_CSUM_S;
+		} else
+			skb->ip_summed = CHECKSUM_NONE;

drivers/net/pasemi_mac.c line 416

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+
+		mac->stats.rx_bytes += len;
+		mac->stats.rx_packets++;
+
+		netif_receive_skb(skb);
+
+		dp->ptr = 0;
+		dp->macrx = 0;
+		info->dma = 0;
+		info->skb = 0;

0 != NULL

Quoted 8 lines
drivers/net/pasemi_mac.c new file +8 -0
+	mac->rx->next_to_clean += count;
+	pasemi_mac_replenish_rx_ring(mac->netdev);
+
+	spin_unlock(&mac->rx->lock);
+
+	return count;
+}
+

drivers/net/pasemi_mac.c line 436

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
+{
+	int i;
+	struct pasemi_mac_buffer *info;
+	struct pas_dma_xct_descr *dp;
+	int start, count;
+	int flags;
+
+	if (!spin_trylock_irqsave(&mac->tx->lock, flags))
+		return 0;

what prevents starvation?

Quoted 37 lines
drivers/net/pasemi_mac.c new file +37 -0
+	start = mac->tx->next_to_clean;
+	count = 0;
+
+	for (i = start; i < mac->tx->next_to_use; i++) {
+		dp = &mac->tx->desc[i % mac->tx->count];
+		if (!dp || (dp->mactx & XCT_MACTX_O))
+			break;
+
+		count++;
+
+		info = &mac->tx->desc_info[i % mac->tx->count];
+
+		pci_unmap_single(mac->dma_pdev, info->dma,
+				 info->skb->len, PCI_DMA_TODEVICE);
+		dev_kfree_skb_irq(info->skb);
+		info->skb = NULL;
+		info->dma = 0;
+		dp->mactx = 0;
+		dp->ptr = 0;
+	}
+	mac->tx->next_to_clean += count;
+	spin_unlock_irqrestore(&mac->tx->lock, flags);
+
+	return count;
+}
+
+
+static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
+{
+	struct net_device *dev = data;
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int reg;
+
+	if (!(*mac->rx_status & PAS_STATUS_INT))
+		return IRQ_NONE;
+
+	netif_rx_schedule(dev);

drivers/net/pasemi_mac.c line 484

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
+			       PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0));
+
+	reg = PAS_IOB_DMA_RXCH_RESET_PINTC | PAS_IOB_DMA_RXCH_RESET_SINTC |
+	      PAS_IOB_DMA_RXCH_RESET_DINTC;
+	if (*mac->rx_status & PAS_STATUS_TIMER)
+		reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
+
+	pci_write_config_dword(mac->iob_pdev,
+			       PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);

is there any faster method of register reading/writing than through PCI config registers?

pci_{read,write}_config_foo acquires and releases a spinlock for each operation, making it rather expensive in fast path code

Quoted 46 lines
drivers/net/pasemi_mac.c new file +46 -0
+static int pasemi_mac_close(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int stat;
+
+	netif_stop_queue(dev);
+
+	/* Clean out any pending buffers */
+	pasemi_mac_clean_tx(mac);
+	pasemi_mac_clean_rx(mac, mac->rx->count);
+
+	/* Disable interface */
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
+			       PAS_DMA_TXCHAN_TCMDSTA_ST);
+	pci_write_config_dword(mac->dma_pdev,
+		      PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
+		      PAS_DMA_RXINT_RCMDSTA_ST);
+	pci_write_config_dword(mac->dma_pdev,
+		      PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
+		      PAS_DMA_RXCHAN_CCMDSTA_ST);
+
+	do {
+		pci_read_config_dword(mac->dma_pdev,
+				      PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
+				      &stat);
+	} while (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT);
+
+	do {
+		pci_read_config_dword(mac->dma_pdev,
+				      PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
+				      &stat);
+	} while (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT);
+
+	do {
+		pci_read_config_dword(mac->dma_pdev,
+				      PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
+				      &stat);
+	} while (stat & PAS_DMA_RXINT_RCMDSTA_ACT);
+
+	/* Then, disable the channel. This must be done separately from
+	 * stopping, since you can't disable when active.
+	 */
+
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);

drivers/net/pasemi_mac.c line 671

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
+
+	synchronize_irq(mac->dma_pdev->irq + mac->dma_txch);
+	synchronize_irq(mac->dma_pdev->irq + 20 + mac->dma_rxch);
+
+	free_irq(mac->dma_pdev->irq + mac->dma_txch, dev);
+	free_irq(mac->dma_pdev->irq + 20 + mac->dma_rxch, dev);

shouldn't need synchronize_irq() right before free_irq()

Quoted 5 lines
drivers/net/pasemi_mac.c new file +5 -0
+	/* Free resources */
+	pasemi_mac_free_rx_resources(dev);
+	pasemi_mac_free_tx_resources(dev);
+
+	return 0;

drivers/net/pasemi_mac.c line 687

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+}
+
+static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	struct pasemi_mac_txring *txring;
+	struct pasemi_mac_buffer *info;
+	struct pas_dma_xct_descr *dp;
+	u64 flags;
+	dma_addr_t map;

needs locking, as mentioned elsewhere

Quoted 26 lines
drivers/net/pasemi_mac.c new file +26 -0
+	txring = mac->tx;
+
+	if (txring->next_to_clean + txring->count == txring->next_to_use) {
+		pasemi_mac_clean_tx(mac);
+
+		if (txring->next_to_clean + txring->count == txring->next_to_use) {
+			/* Still no room -- stop the queue and wait for tx
+			 * intr when there's room.
+			 */
+			netif_stop_queue(dev);
+			return NETDEV_TX_BUSY;
+		}
+	}
+
+	mac->stats.tx_packets++;
+	mac->stats.tx_bytes += skb->len;
+
+	flags = XCT_MACTX_O | XCT_MACTX_ST |
+		XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
+
+	if (skb->ip_summed == CHECKSUM_PARTIAL) {
+		switch (skb->nh.iph->protocol) {
+		case IPPROTO_TCP:
+			flags |= XCT_MACTX_CSUM_TCP;
+			flags |= XCT_MACTX_IPH((skb->h.raw - skb->nh.raw) >> 2);
+			flags |= XCT_MACTX_IPO(skb->nh.raw - skb->data);

drivers/net/pasemi_mac.c line 724

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+			break;
+		case IPPROTO_UDP:
+			flags |= XCT_MACTX_CSUM_UDP;
+			flags |= XCT_MACTX_IPH((skb->h.raw - skb->nh.raw) >> 2);
+			flags |= XCT_MACTX_IPO(skb->nh.raw - skb->data);
+			break;
+		}
+	}
+
+	map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);

check for mapping error

drivers/net/pasemi_mac.c line 733

Quoted 8 lines
drivers/net/pasemi_mac.c new file +8 -0
+	dp = &txring->desc[txring->next_to_use % txring->count];
+	info = &txring->desc_info[txring->next_to_use % txring->count];
+
+	dp->mactx = flags | XCT_MACTX_LLEN(skb->len);
+	dp->ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
+	info->dma = map;
+	info->skb = skb;
+	/* XXXOJN Deal with fragmented packets when larger MTU is supported */

does this comment imply that larger MTUs make the driver go splat, or does driver code elsewhere prevent the user from using an invalid MTU?

Quoted 19 lines
drivers/net/pasemi_mac.c new file +19 -0
+	txring->next_to_use++;
+
+	pci_write_config_dword(mac->dma_pdev,
+			       PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
+
+	return NETDEV_TX_OK;
+}
+
+static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+
+	return &mac->stats;
+}
+
+static void pasemi_mac_set_rx_mode(struct net_device *dev)
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	unsigned int flags;

drivers/net/pasemi_mac.c line 763

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+
+	pci_read_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, &flags);
+
+	/* Set promiscuous */
+	if (dev->flags & IFF_PROMISC)
+		flags |= PAS_MAC_CFG_PCFG_PR;
+	else
+		flags &= ~PAS_MAC_CFG_PCFG_PR;
+
+	pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);

you have no multicast capability?

Quoted 25 lines
drivers/net/pasemi_mac.c new file +25 -0
+static int pasemi_mac_poll(struct net_device *dev, int *budget)
+{
+	int pkts, limit = min(*budget, dev->quota);
+	struct pasemi_mac *mac = netdev_priv(dev);
+
+	pkts = pasemi_mac_clean_rx(mac, limit);
+
+	if (pkts < limit) {
+		/* all done, no more packets present */
+		netif_rx_complete(dev);
+
+		/* re-enable receive interrupts */
+		pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
+				       PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(1000000));
+		return 0;
+	} else {
+		/* used up our quantum, so reschedule */
+		dev->quota -= pkts;
+		*budget -= pkts;
+		return 1;
+	}
+}
+
+static int __devinit
+pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)

drivers/net/pasemi_mac.c line 801

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+{
+	static int index = 0;
+	struct net_device *dev;
+	struct pasemi_mac *mac;
+	int err;
+
+	err = pci_enable_device(pdev);
+	if (err) {
+		dev_err(&pdev->dev, "pasemi_mac: Could not enable device.\n");
+		return err;

PCI layer already prints out an error msg for this case

drivers/net/pasemi_mac.c line 807

Quoted 5 lines
drivers/net/pasemi_mac.c new file +5 -0
+	dev = alloc_etherdev(sizeof(struct pasemi_mac));
+	if (dev == NULL) {
+		dev_err(&pdev->dev,
+			"pasemi_mac: Could not allocate ethernet device.\n");
+		return -ENOMEM;

call pci_disable_device() on error

drivers/net/pasemi_mac.c line 822

Quoted 13 lines
drivers/net/pasemi_mac.c new file +13 -0
+	SET_MODULE_OWNER(dev);
+	pci_set_drvdata(pdev, dev);
+	SET_NETDEV_DEV(dev, &pdev->dev);
+
+	mac = netdev_priv(dev);
+
+	mac->pdev = pdev;
+	mac->netdev = dev;
+	mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
+
+	if (!mac->dma_pdev) {
+		dev_err(&pdev->dev, "Can't find DMA Controller\n");
+		free_netdev(dev);

undo pci_enable_device()
undo pci_set_drvdata()

drivers/net/pasemi_mac.c line 830

Quoted 8 lines
drivers/net/pasemi_mac.c new file +8 -0
+		return -ENODEV;
+	}
+
+	mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
+
+	if (!mac->iob_pdev) {
+		dev_err(&pdev->dev, "Can't find I/O Bridge\n");
+		free_netdev(dev);

ditto

Quoted 40 lines
drivers/net/pasemi_mac.c new file +40 -0
+		return -ENODEV;
+	}
+
+	/* These should come out of the device tree eventually */
+	mac->dma_txch = index;
+	mac->dma_rxch = index;
+
+	/* We probe GMAC before XAUI, but the DMA interfaces are
+	 * in XAUI, GMAC order.
+	 */
+	if (index < 4)
+		mac->dma_if = index + 2;
+	else
+		mac->dma_if = index - 4;
+	index++;
+
+	switch (pdev->device) {
+	case 0xa005:
+		mac->type = MAC_TYPE_GMAC;
+		break;
+	case 0xa006:
+		mac->type = MAC_TYPE_XAUI;
+		break;
+	default:
+		err = -ENODEV;
+		goto out;
+	}
+
+	/* get mac addr from device tree */
+	if (pasemi_set_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
+		err = -ENODEV;
+		goto out;
+	}
+	memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
+
+	dev->open = pasemi_mac_open;
+	dev->stop = pasemi_mac_close;
+	dev->hard_start_xmit = pasemi_mac_start_tx;
+	dev->get_stats = pasemi_mac_get_stats;
+	dev->set_multicast_list = pasemi_mac_set_rx_mode;

drivers/net/pasemi_mac.c line 880

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	dev->weight = 64;
+	dev->poll = pasemi_mac_poll;
+	dev->features = NETIF_F_HW_CSUM;
+
+	/* The dma status structure is located in the I/O bridge, and
+	 * is cache coherent.
+	 */
+	if (!dma_status)
+		/* XXXOJN This should come from the device tree */
+		dma_status = __ioremap(0xfd800000, 0x1000, 0);

why __ioremap ?

Quoted 43 lines
drivers/net/pasemi_mac.c new file +43 -0
+	mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
+	mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
+
+	err = register_netdev(dev);
+
+	if (err) {
+		dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
+			err);
+		goto out;
+	} else
+		printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
+		       "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
+		       dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
+		       mac->dma_if, mac->dma_txch, mac->dma_rxch,
+		       dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
+		       dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
+
+	return err;
+
+out:
+	dev_err(&mac->pdev->dev, "pasemi_mac: init failed\n");
+
+	pci_disable_device(pdev);
+	pci_dev_put(mac->dma_pdev);
+	pci_dev_put(mac->iob_pdev);
+	free_netdev(dev);
+	return err;
+}
+
+static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
+{
+	struct net_device *netdev = pci_get_drvdata(pdev);
+	struct pasemi_mac *mac;
+
+	if (!netdev)
+		return;
+
+	mac = netdev_priv(netdev);
+
+	unregister_netdev(netdev);
+
+	pci_disable_device(pdev);
+	pci_dev_put(mac->dma_pdev);

drivers/net/pasemi_mac.c line 934

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+	pci_dev_put(mac->iob_pdev);
+
+	pci_set_drvdata(pdev, NULL);
+	free_netdev(netdev);
+}
+
+static struct pci_device_id pasemi_mac_pci_tbl[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
+	{ 0 }

remove the '0'

Quoted 28 lines
drivers/net/pasemi_mac.c new file +28 -0
+};
+
+MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
+
+static struct pci_driver pasemi_mac_driver = {
+	.name		= "pasemi_mac",
+	.id_table	= pasemi_mac_pci_tbl,
+	.probe		= pasemi_mac_probe,
+	.remove		= __devexit_p(pasemi_mac_remove),
+};
+
+static void __exit pasemi_mac_cleanup_module(void)
+{
+	pci_unregister_driver(&pasemi_mac_driver);
+	__iounmap(dma_status);
+	dma_status = NULL;
+}
+
+int pasemi_mac_init_module(void)
+{
+	return pci_register_driver(&pasemi_mac_driver);
+}
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR ("Olof Johansson <[email protected]>");
+MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
+
+module_init(pasemi_mac_init_module);
#15 lore raw
In reply to Olof Johansson

Hi,

Olof Johansson schrieb:
[...]

Quoted 2 lines
drivers/net/pasemi_mac.c new file +2 -0
+static int pasemi_mac_close(struct net_device *dev)
+{

[..]

Quoted 7 lines
drivers/net/pasemi_mac.c new file +7 -0
+	do {
+		pci_read_config_dword(mac->dma_pdev,
+				      PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
+				      &stat);
+	} while (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT);
+
+	do {

drivers/net/pasemi_mac.c line 654

Quoted 10 lines
drivers/net/pasemi_mac.c new file +10 -0
+		pci_read_config_dword(mac->dma_pdev,
+				      PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
+				      &stat);
+	} while (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT);
+
+	do {
+		pci_read_config_dword(mac->dma_pdev,
+				      PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
+				      &stat);
+	} while (stat & PAS_DMA_RXINT_RCMDSTA_ACT);

You might want to write these loops like that:

#define MAX_READ_TRIES 10000
unsigned int tries;
unsigned int state;
for(tries=0; tries < MAX_READ_TRIES; tries++)

{
	read_stat(&stat);
	if ((stat & STATE_FLAG) == 0);
		break;
	cond_resched();
}
if (stat & STATE_FLAG) {
	dev_err(&mac->pdev->dev, "Failed to stop device, possible hardware error?\n");
        /* Panic, disable device, mark unusable, whatever is better than hanging here */
}

That way you:
- Let you give other processes a chance to run,
while you wait for your hardware state to change
(if your hardware can tolerate these latencies).
- can somehow handle hardware failure and at least give the user a clue what is happening.

Regards

Ingo Oeser

#16 lore raw

Hi,

Quoted 1 line · Ingo Oeser

You might want to write these loops like that:

[...]

Quoted 6 lines

That way you:
- Let you give other processes a chance to run,
while you wait for your hardware state to change
(if your hardware can tolerate these latencies).
- can somehow handle hardware failure and at least give the user a clue what is happening.

Yep, makes sense. I'll implement something like it.

Thanks,

-Olof

#17 lore raw
In reply to Olof Johansson

O

Quoted 1 line

+static noinline void pasemi_mac_free_tx_resources(struct net_device *dev)

Why tell compiler how to do it's job. marking it noinline?

drivers/net/pasemi_mac.h

Quoted 4 lines

Index: merge/drivers/net/pasemi_mac.h
===================================================================

drivers/net/pasemi_mac.h new file +0 -0

PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT_M)

Quoted 31 lines
drivers/net/pasemi_mac.h new file +31 -0
+
+struct pas_dma_xct_descr {
+	union {
+ 		u64	mactx;
+#define	XCT_MACTX_T		0x8000000000000000
+#define	XCT_MACTX_ST		0x4000000000000000
+#define XCT_MACTX_NORES		0x0000000000000000
+#define XCT_MACTX_8BRES		0x1000000000000000
+#define XCT_MACTX_24BRES	0x2000000000000000
+#define XCT_MACTX_40BRES	0x3000000000000000
+#define XCT_MACTX_I		0x0800000000000000
+#define XCT_MACTX_O		0x0400000000000000
+#define XCT_MACTX_E		0x0200000000000000
+#define XCT_MACTX_VLAN_M	0x0180000000000000
+#define XCT_MACTX_VLAN_NOP	0x0000000000000000
+#define XCT_MACTX_VLAN_REMOVE	0x0080000000000000
+#define XCT_MACTX_VLAN_INSERT   0x0100000000000000
+#define XCT_MACTX_VLAN_REPLACE  0x0180000000000000
+#define XCT_MACTX_CRC_M		0x0060000000000000
+#define XCT_MACTX_CRC_NOP	0x0000000000000000
+#define XCT_MACTX_CRC_INSERT	0x0020000000000000
+#define XCT_MACTX_CRC_PAD	0x0040000000000000
+#define XCT_MACTX_CRC_REPLACE	0x0060000000000000
+#define XCT_MACTX_SS		0x0010000000000000
+#define XCT_MACTX_LLEN_M	0x00007fff00000000
+#define XCT_MACTX_LLEN_S	32ull
+#define XCT_MACTX_LLEN(x)	((((long)(x)) << XCT_MACTX_LLEN_S) & XCT_MACTX_LLEN_M)
+#define XCT_MACTX_IPH_M		0x00000000f8000000
+#define XCT_MACTX_IPH_S		27ull
+#define XCT_MACTX_IPH(x)	((((long)(x)) << XCT_MACTX_IPH_S) & XCT_MACTX_IPH_M)
+#define XCT_MACTX_IPO_M		0x0000000007c00000

drivers/net/pasemi_mac.h line 382

Quoted 10 lines
drivers/net/pasemi_mac.h new file +10 -0
+#define XCT_MACTX_IPO_S		22ull
+#define XCT_MACTX_IPO(x)	((((long)(x)) << XCT_MACTX_IPO_S) & XCT_MACTX_IPO_M)
+#define XCT_MACTX_CSUM_M	0x0000000000000060
+#define XCT_MACTX_CSUM_NOP	0x0000000000000000
+#define XCT_MACTX_CSUM_TCP	0x0000000000000040
+#define XCT_MACTX_CSUM_UDP	0x0000000000000060
+#define XCT_MACTX_V6		0x0000000000000010
+#define XCT_MACTX_C		0x0000000000000004
+#define XCT_MACTX_AL2		0x0000000000000002
+		u64	macrx;

Mixing definitions and structure values gets confusing.

Signature · 1 line
Stephen Hemminger <[email protected]>
#18 lore raw
Quoted 4 lines · Stephen Hemminger

O

Quoted 1 line

+static noinline void pasemi_mac_free_tx_resources(struct net_device *dev)

Why tell compiler how to do it's job. marking it noinline?

Leftover from debugging. Gone in the next version.

Quoted 1 line

Mixing definitions and structure values gets confusing.

I find if the definitions are few, having them near the structure definition is easier to read. This is way past the limit for that though. I'll move them.

Thanks,

-Olof

#14 lore raw
In reply to Jeff Garzik
Quoted 9 lines · Jeff Garzik
Quoted 6 lines · Olof Johansson

Driver for the PA Semi PWRficient on-chip Ethernet (1/10G)

Basic enablement, will be complemented with performance enhancements over time. PHY support will be added as well.

Looks generally pretty clean, well done.

Getting better, I really should have found alot of this on my own. Thanks to everyone for their patience.

I added a TODO list in the driver for the things that I'll add incrementally over time.

(I also took out some unused definitions while I was at it)

Quoted 1 line

Comments included inline...

Replies where approprate, taking out most of the code. New patch posted separately.

Quoted 5 lines

consider enums rather than #define's for constants. they generate symbols at the C level rather than cpp level, making the code more readable, providing more type information to the C compiler, and making symbols visible at the debugger level.

Quoted 7 lines

example:

enum {
	PAS_DMA_MAX_IF		= 40,
	PAS_DMA_MAX_RXCH	= 8,
	PAS_DMA_MAX_TXCH	= 8,
};

That works quite OK for things like register numbers. For bitfields I'm not so sure the compiler/debugger will have much benefit from it though, right? It's also nice to keep the mask/shift and macro together.

I've broken out the simpler register numbers into enums. If you feel really strongly that I should have the rest the same way I'll give it a shot.

Also, enums are ints, right? The 64-bit fields will be hard to describe that way.

Quoted 5 lines
Quoted 2 lines

+static int pasemi_set_mac_addr(struct pasemi_mac *mac)

poor name. from the context of the code reader and driver, this should be "pasemi_GET_mac_addr", rather than ...set...

Set it in the structure, get it from the hardware. Yes, I was thinking of it the other way around there. Fixed.

Quoted 4 lines

"0" is not the same as "NULL". Use NULL where appropriate.

Then make sure your driver passes sparse checks (read Documentation/sparse.txt)

Fixed, and other places where sparse complained (0x...ull on large constants).

Only exception is the ioremap, see below.

Quoted 3 lines

if feasible, logical operations are often more optimal than '%'

maybe you need something like tg3.c's NEXT_TX() ?

Nice. Yes, much better. Only drawback is that the ring size will be compile-time (not that I have the ethtool hookups to set it now anyway).

Quoted 5 lines

drivers/net/pasemi_mac.c line 305

Quoted 3 lines
(quoted diff) +1 -0
+	if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 

0))

drivers/net/pasemi_mac.c new file +1 -0
+		count = mac->rx->count - 8;

why this is needed?

Added a comment before the statment why the check is needed -- both will be 0 on the very first fill. The -8 can come out now, old workaround that's no longer needed.

Quoted 4 lines

drivers/net/pasemi_mac.c line 327

Quoted 2 lines
drivers/net/pasemi_mac.c new file +2 -0
+		dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
+				     PCI_DMA_FROMDEVICE);

check for DMA mapping error

Done. However, I noticed that lots of other network drivers don't do it. :-)

Quoted 4 lines

drivers/net/pasemi_mac.c line 436

Quoted 2 lines
drivers/net/pasemi_mac.c new file +2 -0
+	if (!spin_trylock_irqsave(&mac->tx->lock, flags))
+		return 0;

what prevents starvation?

Uhm, nothing. Thanks.

Quoted 8 lines

drivers/net/pasemi_mac.c line 484

Quoted 2 lines
drivers/net/pasemi_mac.c new file +2 -0
+	pci_write_config_dword(mac->iob_pdev,
+			       PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);

is there any faster method of register reading/writing than through PCI config registers?

pci_{read,write}_config_foo acquires and releases a spinlock for each operation, making it rather expensive in fast path code

Yeah, I noticed lately that while it fits nice with our register structure, it's really heavy on overhead.

It's at the top of my list for what to work on next; If it's OK with you I'd prefer to do it incrementally after merge though.

Quoted 11 lines

drivers/net/pasemi_mac.c line 687

Quoted 9 lines

+static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)

drivers/net/pasemi_mac.c new file +7 -0
+{
+	struct pasemi_mac *mac = netdev_priv(dev);
+	struct pasemi_mac_txring *txring;
+	struct pasemi_mac_buffer *info;
+	struct pas_dma_xct_descr *dp;
+	u64 flags;
+	dma_addr_t map;

needs locking, as mentioned elsewhere

Doh, need to protect the whole ring not just in clean. Added.

Quoted 5 lines
Quoted 2 lines
(quoted diff) +1 -0
+	/* XXXOJN Deal with fragmented packets when larger MTU is supported 

*/

does this comment imply that larger MTUs make the driver go splat, or does driver code elsewhere prevent the user from using an invalid MTU?

Larger MTU support is just not wired up yet (no change_mtu function). It's on the list.

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);

you have no multicast capability?

The device has it, just not hooked up in the driver. On the list.

Quoted 8 lines

drivers/net/pasemi_mac.c line 880

Quoted 6 lines
drivers/net/pasemi_mac.c new file +6 -0
+	/* The dma status structure is located in the I/O bridge, and
+	 * is cache coherent.
+	 */
+	if (!dma_status)
+		/* XXXOJN This should come from the device tree */
+		dma_status = __ioremap(0xfd800000, 0x1000, 0);

why __ioremap ?

As the comment says, the registers live in the I/O bridge, and they are cache coherent. So to map them in, I use the version that (on powerpc at least) you can specify the flags -- and I'm not specifying _PAGE_NO_CACHE|_PAGE_GUARDED that the standard ioremap does.

Still, this makes sparse unhappy since it's still flagged as another address space. I'm open for better ideas.

-Olof

#19 lore raw

PATCH v4 1/1 PA Semi PWRficient Ethernet driver

In reply to Olof Johansson

Driver for the PA Semi PWRficient on-chip Ethernet (1/10G)

Basic enablement, will be complemented with performance enhancements over time. PHY support will be added as well.

---

v4 changes:

* Check for dma mapping error
* Sparse cleanups
* Moved some register offsets to enums
* Don't spin infinitely on interface shutdown
* Remove some more debug cruft
* Better ring wrapping
* More locking fixes
* Misc other cleanups

v3 changes:

* Added remove function
* Checking interrupt status in handler
* Misc cleanups w.r.t. ring handing (INFO/DESCR/BUFF are gone)
* Added locking of the rings
* Using PCI DMA for all buffers
* Setting the interrupt descriptor field to include interface number
* Added PCI vendor ID, it's been submitted to sf.net as well.
* Moved Kconfig entry to the 10GbE section

drivers/net/Kconfig +7 -0
Index: merge/drivers/net/Kconfig
===================================================================
@@ -2488,6 +2488,13 @@ config NETXEN_NIC
 	help
 	  This enables the support for NetXen's Gigabit Ethernet card.
 
+config PASEMI_MAC
+	tristate "PA Semi 1/10Gbit MAC"
+	depends on PPC64 && PCI
+	help
+	  This driver supports the on-chip 1/10Gbit Ethernet controller on
+	  PA Semi's PWRficient line of chips.
+
 endmenu
 
 source "drivers/net/tokenring/Kconfig"
drivers/net/Makefile +1 -0
Index: merge/drivers/net/Makefile
===================================================================
@@ -196,6 +196,7 @@ obj-$(CONFIG_SMC91X) += smc91x.o
 obj-$(CONFIG_SMC911X) += smc911x.o
 obj-$(CONFIG_DM9000) += dm9000.o
 obj-$(CONFIG_FEC_8XX) += fec_8xx/
+obj-$(CONFIG_PASEMI_MAC) += pasemi_mac.o
 
 obj-$(CONFIG_MACB) += macb.o
 
drivers/net/pasemi_mac.c new file +1019 -0

1,019 lines not on this page · raw message

drivers/net/pasemi_mac.h new file +460 -0

460 lines not on this page · raw message

MAINTAINERS +6 -0
Index: merge/MAINTAINERS
===================================================================
@@ -2490,6 +2490,12 @@ M:	[email protected]
 L:	[email protected]
 S:	Maintained
 
+PA SEMI ETHERNET DRIVER
+P:	Olof Johansson
+M:	[email protected]
+L:	[email protected]
+S:	Maintained
+
 PARALLEL PORT SUPPORT
 P:	Phil Blundell
 M:	[email protected]
include/linux/pci_ids.h +2 -0
Index: merge/include/linux/pci_ids.h
===================================================================
@@ -2064,6 +2064,8 @@
 #define PCI_VENDOR_ID_TDI               0x192E
 #define PCI_DEVICE_ID_TDI_EHCI          0x0101
 
+#define PCI_VENDOR_ID_PASEMI		0x1959
+
 #define PCI_VENDOR_ID_JMICRON		0x197B
 #define PCI_DEVICE_ID_JMICRON_JMB360	0x2360
 #define PCI_DEVICE_ID_JMICRON_JMB361	0x2361
#20 lore raw
Quoted 6 lines · Olof Johansson

Driver for the PA Semi PWRficient on-chip Ethernet (1/10G)

Basic enablement, will be complemented with performance enhancements over time. PHY support will be added as well.

patch applied. You may now send diffs for all future changes

Jeff