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