pasemi_mac: Abstract and fix up interrupt restart routines
[powerpc.git] / drivers / net / pasemi_mac.c
index 76fe9dd..73e79f3 100644 (file)
@@ -33,6 +33,8 @@
 #include <linux/tcp.h>
 #include <net/checksum.h>
 
+#include <asm/irq.h>
+
 #include "pasemi_mac.h"
 
 
@@ -360,6 +362,42 @@ static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
        mac->rx->next_to_fill += count;
 }
 
+static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
+{
+       unsigned int reg, stat;
+       /* Re-enable packet count interrupts: finally
+        * ack the packet count interrupt we got in rx_intr.
+        */
+
+       pci_read_config_dword(mac->iob_pdev,
+                             PAS_IOB_DMA_RXCH_STAT(mac->dma_rxch),
+                             &stat);
+
+       reg = PAS_IOB_DMA_RXCH_RESET_PCNT(stat & PAS_IOB_DMA_RXCH_STAT_CNTDEL_M)
+               | PAS_IOB_DMA_RXCH_RESET_PINTC;
+
+       pci_write_config_dword(mac->iob_pdev,
+                              PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch),
+                              reg);
+}
+
+static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
+{
+       unsigned int reg, stat;
+
+       /* Re-enable packet count interrupts */
+       pci_read_config_dword(mac->iob_pdev,
+                             PAS_IOB_DMA_TXCH_STAT(mac->dma_txch), &stat);
+
+       reg = PAS_IOB_DMA_TXCH_RESET_PCNT(stat & PAS_IOB_DMA_TXCH_STAT_CNTDEL_M)
+               | PAS_IOB_DMA_TXCH_RESET_PINTC;
+
+       pci_write_config_dword(mac->iob_pdev,
+                              PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
+}
+
+
+
 static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
 {
        unsigned int i;
@@ -476,6 +514,8 @@ static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
        mac->tx->next_to_clean += count;
        spin_unlock_irqrestore(&mac->tx->lock, flags);
 
+       netif_wake_queue(mac->netdev);
+
        return count;
 }
 
@@ -510,9 +550,6 @@ 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;
-       int was_full;
-
-       was_full = mac->tx->next_to_clean - mac->tx->next_to_use == TX_RING_SIZE;
 
        if (!(*mac->tx_status & PAS_STATUS_INT))
                return IRQ_NONE;
@@ -526,15 +563,13 @@ static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
        pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch),
                               reg);
 
-       if (was_full)
-               netif_wake_queue(dev);
-
        return IRQ_HANDLED;
 }
 
 static int pasemi_mac_open(struct net_device *dev)
 {
        struct pasemi_mac *mac = netdev_priv(dev);
+       int base_irq;
        unsigned int flags;
        int ret;
 
@@ -560,6 +595,10 @@ static int pasemi_mac_open(struct net_device *dev)
        pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
                               PAS_IOB_DMA_RXCH_CFG_CNTTH(30));
 
+       /* Clear out any residual packet count state from firmware */
+       pasemi_mac_restart_rx_intr(mac);
+       pasemi_mac_restart_tx_intr(mac);
+
        pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
                               PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(1000000));
 
@@ -598,28 +637,37 @@ static int pasemi_mac_open(struct net_device *dev)
        netif_start_queue(dev);
        netif_poll_enable(dev);
 
-       ret = request_irq(mac->dma_pdev->irq + mac->dma_txch,
-                         &pasemi_mac_tx_intr, IRQF_DISABLED,
+       /* Interrupts are a bit different for our DMA controller: While
+        * it's got one a regular PCI device header, the interrupt there
+        * is really the base of the range it's using. Each tx and rx
+        * channel has it's own interrupt source.
+        */
+
+       base_irq = virq_to_hw(mac->dma_pdev->irq);
+
+       mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
+       mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
+
+       ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
                          mac->tx->irq_name, dev);
        if (ret) {
                dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
-                      mac->dma_pdev->irq + mac->dma_txch, ret);
+                       base_irq + mac->dma_txch, ret);
                goto out_tx_int;
        }
 
-       ret = request_irq(mac->dma_pdev->irq + 20 + mac->dma_rxch,
-                         &pasemi_mac_rx_intr, IRQF_DISABLED,
+       ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
                          mac->rx->irq_name, dev);
        if (ret) {
                dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
-                      mac->dma_pdev->irq + 20 + mac->dma_rxch, ret);
+                       base_irq + 20 + mac->dma_rxch, ret);
                goto out_rx_int;
        }
 
        return 0;
 
 out_rx_int:
-       free_irq(mac->dma_pdev->irq + mac->dma_txch, dev);
+       free_irq(mac->tx_irq, dev);
 out_tx_int:
        netif_poll_disable(dev);
        netif_stop_queue(dev);
@@ -660,40 +708,37 @@ static int pasemi_mac_close(struct net_device *dev)
                pci_read_config_dword(mac->dma_pdev,
                                      PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
                                      &stat);
-               if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
+               if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
                        break;
                cond_resched();
        }
 
-       if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)) {
+       if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
                dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
-       }
 
        for (retries = 0; retries < MAX_RETRIES; retries++) {
                pci_read_config_dword(mac->dma_pdev,
                                      PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
                                      &stat);
-               if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
+               if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
                        break;
                cond_resched();
        }
 
-       if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)) {
+       if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
                dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
-       }
 
        for (retries = 0; retries < MAX_RETRIES; retries++) {
                pci_read_config_dword(mac->dma_pdev,
                                      PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
                                      &stat);
-               if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
+               if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
                        break;
                cond_resched();
        }
 
-       if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT)) {
+       if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
                dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
-       }
 
        /* Then, disable the channel. This must be done separately from
         * stopping, since you can't disable when active.
@@ -706,8 +751,8 @@ static int pasemi_mac_close(struct net_device *dev)
        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_irq(mac->tx_irq, dev);
+       free_irq(mac->rx_irq, dev);
 
        /* Free resources */
        pasemi_mac_free_rx_resources(dev);
@@ -830,9 +875,7 @@ static int pasemi_mac_poll(struct net_device *dev, int *budget)
                /* 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));
+               pasemi_mac_restart_rx_intr(mac);
                return 0;
        } else {
                /* used up our quantum, so reschedule */