b0590d754099a28f31fb86bfcfd3a45c4a0d9207
[openwrt.git] / target / linux / ar7 / patches-2.6.24 / 140-cpmac_fix.patch
1 --- /usr/src/linux-2.6.24/drivers/net/cpmac.c   2008-01-25 02:20:37.000000000 +0100
2 +++ cpmac.c     2008-04-06 21:30:03.000000000 +0200
3 @@ -38,6 +38,7 @@
4  #include <linux/platform_device.h>
5  #include <linux/dma-mapping.h>
6  #include <asm/gpio.h>
7 +#include <asm/atomic.h>
8  
9  MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
10  MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
11 @@ -207,6 +208,7 @@
12         struct work_struct reset_work;
13         struct platform_device *pdev;
14         struct napi_struct napi;
15 +       atomic_t reset_pending;
16  };
17  
18  static irqreturn_t cpmac_irq(int, void *);
19 @@ -455,6 +457,9 @@
20         struct cpmac_desc *desc;
21         struct cpmac_priv *priv = netdev_priv(dev);
22  
23 +       if (unlikely(atomic_read(&priv->reset_pending)))
24 +               return NETDEV_TX_BUSY;
25 +
26         if (unlikely(skb_padto(skb, ETH_ZLEN)))
27                 return NETDEV_TX_OK;
28  
29 @@ -634,14 +639,14 @@
30                 priv->desc_ring[i].dataflags = 0;
31                 if (priv->desc_ring[i].skb) {
32                         dev_kfree_skb_any(priv->desc_ring[i].skb);
33 -                       if (netif_subqueue_stopped(dev, i))
34 -                           netif_wake_subqueue(dev, i);
35 +                       priv->desc_ring[i].skb = NULL;
36                 }
37         }
38  }
39  
40  static void cpmac_hw_error(struct work_struct *work)
41  {
42 +       int i;
43         struct cpmac_priv *priv =
44                 container_of(work, struct cpmac_priv, reset_work);
45  
46 @@ -650,8 +655,47 @@
47         spin_unlock(&priv->rx_lock);
48         cpmac_clear_tx(priv->dev);
49         cpmac_hw_start(priv->dev);
50 -       napi_enable(&priv->napi);
51 -       netif_start_queue(priv->dev);
52 +       barrier();
53 +       atomic_dec(&priv->reset_pending);
54 +       
55 +       for (i = 0; i < CPMAC_QUEUES; i++) {
56 +               netif_wake_subqueue(priv->dev, i);
57 +       }
58 +       netif_wake_queue(priv->dev);
59 +       cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
60 +}
61 +
62 +static void cpmac_check_status(struct net_device *dev)
63 +{
64 +       struct cpmac_priv *priv = netdev_priv(dev);
65 +
66 +       u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
67 +       int rx_channel = (macstatus >> 8) & 7;
68 +       int rx_code = (macstatus >> 12) & 15;
69 +       int tx_channel = (macstatus >> 16) & 7;
70 +       int tx_code = (macstatus >> 20) & 15;
71 +
72 +       if (rx_code || tx_code) {
73 +               if (netif_msg_drv(priv) && net_ratelimit()) {
74 +                       /* Can't find any documentation on what these error codes actually are.
75 +                        * So just log them and hope..
76 +                        */
77 +                       if (rx_code)
78 +                               printk(KERN_WARNING "%s: host error %d on rx channel %d (macstatus %08x), resetting\n",
79 +                                      dev->name, rx_code, rx_channel, macstatus);
80 +                       if (tx_code)
81 +                               printk(KERN_WARNING "%s: host error %d on tx channel %d (macstatus %08x), resetting\n",
82 +                                      dev->name, tx_code, tx_channel, macstatus);
83 +               }
84 +               
85 +               netif_stop_queue(dev);
86 +               cpmac_hw_stop(dev);
87 +               if (schedule_work(&priv->reset_work))
88 +                       atomic_inc(&priv->reset_pending);
89 +               if (unlikely(netif_msg_hw(priv)))
90 +                       cpmac_dump_regs(dev);
91 +       }
92 +       cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
93  }
94  
95  static irqreturn_t cpmac_irq(int irq, void *dev_id)
96 @@ -661,9 +705,6 @@
97         int queue;
98         u32 status;
99  
100 -       if (!dev)
101 -               return IRQ_NONE;
102 -
103         priv = netdev_priv(dev);
104  
105         status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
106 @@ -685,49 +726,33 @@
107  
108         cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
109  
110 -       if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS))) {
111 -               if (netif_msg_drv(priv) && net_ratelimit())
112 -                       printk(KERN_ERR "%s: hw error, resetting...\n",
113 -                              dev->name);
114 -               netif_stop_queue(dev);
115 -               napi_disable(&priv->napi);
116 -               cpmac_hw_stop(dev);
117 -               schedule_work(&priv->reset_work);
118 -               if (unlikely(netif_msg_hw(priv)))
119 -                       cpmac_dump_regs(dev);
120 -       }
121 +       if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
122 +               cpmac_check_status(dev);
123  
124         return IRQ_HANDLED;
125  }
126  
127  static void cpmac_tx_timeout(struct net_device *dev)
128  {
129 -       struct cpmac_priv *priv = netdev_priv(dev);
130         int i;
131 +       struct cpmac_priv *priv = netdev_priv(dev);
132  
133         spin_lock(&priv->lock);
134         dev->stats.tx_errors++;
135         spin_unlock(&priv->lock);
136         if (netif_msg_tx_err(priv) && net_ratelimit())
137                 printk(KERN_WARNING "%s: transmit timeout\n", dev->name);
138 -       /* 
139 -        * FIXME: waking up random queue is not the best thing to
140 -        * do... on the other hand why we got here at all?
141 -        */
142 -#ifdef CONFIG_NETDEVICES_MULTIQUEUE
143 -       for (i = 0; i < CPMAC_QUEUES; i++)
144 -               if (priv->desc_ring[i].skb) {
145 -                       priv->desc_ring[i].dataflags = 0;
146 -                       dev_kfree_skb_any(priv->desc_ring[i].skb);
147 -                       netif_wake_subqueue(dev, i);
148 -                       break;
149 -               }
150 -#else
151 -       priv->desc_ring[0].dataflags = 0;
152 -       if (priv->desc_ring[0].skb)
153 -               dev_kfree_skb_any(priv->desc_ring[0].skb);
154 -       netif_wake_queue(dev);
155 -#endif
156 +
157 +       atomic_inc(&priv->reset_pending);
158 +       barrier();
159 +       cpmac_clear_tx(dev);
160 +       barrier();
161 +       atomic_dec(&priv->reset_pending);
162 +
163 +       netif_wake_queue(priv->dev);
164 +       for (i = 0; i < CPMAC_QUEUES; i++) {
165 +               netif_wake_subqueue(dev, i);
166 +       }
167  }
168  
169  static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
170 @@ -848,15 +873,6 @@
171         spin_unlock(&priv->lock);
172  }
173  
174 -static int cpmac_link_update(struct net_device *dev,
175 -                            struct fixed_phy_status *status)
176 -{
177 -       status->link = 1;
178 -       status->speed = 100;
179 -       status->duplex = 1;
180 -       return 0;
181 -}
182 -
183  static int cpmac_open(struct net_device *dev)
184  {
185         int i, size, res;
186 @@ -923,6 +939,7 @@
187                 goto fail_irq;
188         }
189  
190 +       atomic_set(&priv->reset_pending, 0);
191         INIT_WORK(&priv->reset_work, cpmac_hw_error);
192         cpmac_hw_start(dev);
193  
194 @@ -999,11 +1016,11 @@
195  static int __devinit cpmac_probe(struct platform_device *pdev)
196  {
197         int rc, phy_id, i;
198 +       int mdio_bus_id = cpmac_mii.id;
199         struct resource *mem;
200         struct cpmac_priv *priv;
201         struct net_device *dev;
202         struct plat_cpmac_data *pdata;
203 -       struct fixed_info *fixed_phy;
204         DECLARE_MAC_BUF(mac);
205  
206         pdata = pdev->dev.platform_data;
207 @@ -1017,9 +1034,23 @@
208         }
209  
210         if (phy_id == PHY_MAX_ADDR) {
211 -               if (external_switch || dumb_switch)
212 +               if (external_switch || dumb_switch) {
213 +                       struct fixed_phy_status status = {};
214 +
215 +                       mdio_bus_id = 0;
216 +
217 +                       /*
218 +                        * FIXME: this should be in the platform code!
219 +                        * Since there is not platform code at all (that is,
220 +                        * no mainline users of that driver), place it here
221 +                        * for now.
222 +                        */
223                         phy_id = 0;
224 -               else {
225 +                       status.link = 1;
226 +                       status.duplex = 1;
227 +                       status.speed = 100;
228 +                       fixed_phy_add(PHY_POLL, phy_id, &status);
229 +               } else {
230                         printk(KERN_ERR "cpmac: no PHY present\n");
231                         return -ENODEV;
232                 }
233 @@ -1063,32 +1094,8 @@
234         priv->msg_enable = netif_msg_init(debug_level, 0xff);
235         memcpy(dev->dev_addr, pdata->dev_addr, sizeof(dev->dev_addr));
236  
237 -       if (phy_id == 31) {
238 -               snprintf(priv->phy_name, BUS_ID_SIZE, PHY_ID_FMT, cpmac_mii.id,
239 -                        phy_id);
240 -       } else {
241 -               /* Let's try to get a free fixed phy... */
242 -               for (i = 0; i < MAX_PHY_AMNT; i++) {
243 -                       fixed_phy = fixed_mdio_get_phydev(i);
244 -                       if (!fixed_phy)
245 -                               continue;
246 -                       if (!fixed_phy->phydev->attached_dev) {
247 -                               strncpy(priv->phy_name,
248 -                                       fixed_phy->phydev->dev.bus_id,
249 -                                       BUS_ID_SIZE);
250 -                               fixed_mdio_set_link_update(fixed_phy->phydev,
251 -                                                          &cpmac_link_update);
252 -                               goto phy_found;
253 -                       }
254 -               }
255 -               if (netif_msg_drv(priv))
256 -                       printk(KERN_ERR "%s: Could not find fixed PHY\n",
257 -                              dev->name);
258 -               rc = -ENODEV;
259 -               goto fail;
260 -       }
261 +       snprintf(priv->phy_name, BUS_ID_SIZE, PHY_ID_FMT, mdio_bus_id, phy_id);
262  
263 -phy_found:
264         priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link, 0,
265                                 PHY_INTERFACE_MODE_MII);
266         if (IS_ERR(priv->phy)) {