asix: On RX avoid creating bad Ethernet frames
[firefly-linux-kernel-4.4.55.git] / drivers / net / usb / asix_common.c
1 /*
2  * ASIX AX8817X based USB 2.0 Ethernet Devices
3  * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
4  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
5  * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
6  * Copyright (c) 2002-2003 TiVo Inc.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "asix.h"
23
24 int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
25                   u16 size, void *data)
26 {
27         int ret;
28         ret = usbnet_read_cmd(dev, cmd,
29                                USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
30                                value, index, data, size);
31
32         if (ret != size && ret >= 0)
33                 return -EINVAL;
34         return ret;
35 }
36
37 int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
38                    u16 size, void *data)
39 {
40         return usbnet_write_cmd(dev, cmd,
41                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
42                                 value, index, data, size);
43 }
44
45 void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
46                           u16 size, void *data)
47 {
48         usbnet_write_cmd_async(dev, cmd,
49                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
50                                value, index, data, size);
51 }
52
53 int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
54                            struct asix_rx_fixup_info *rx)
55 {
56         int offset = 0;
57         u16 size;
58
59         /* When an Ethernet frame spans multiple URB socket buffers,
60          * do a sanity test for the Data header synchronisation.
61          * Attempt to detect the situation of the previous socket buffer having
62          * been truncated or a socket buffer was missing. These situations
63          * cause a discontinuity in the data stream and therefore need to avoid
64          * appending bad data to the end of the current netdev socket buffer.
65          * Also avoid unnecessarily discarding a good current netdev socket
66          * buffer.
67          */
68         if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) {
69                 offset = ((rx->remaining + 1) & 0xfffe) + sizeof(u32);
70                 rx->header = get_unaligned_le32(skb->data + offset);
71                 offset = 0;
72
73                 size = (u16)(rx->header & 0x7ff);
74                 if (size != ((~rx->header >> 16) & 0x7ff)) {
75                         netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n",
76                                    rx->remaining);
77                         kfree_skb(rx->ax_skb);
78                         rx->ax_skb = NULL;
79                         /* Discard the incomplete netdev Ethernet frame and
80                          * assume the Data header is at the start of the current
81                          * URB socket buffer.
82                          */
83                         rx->remaining = 0;
84                 }
85         }
86
87         while (offset + sizeof(u16) <= skb->len) {
88                 u16 copy_length;
89                 unsigned char *data;
90
91                 if (!rx->remaining) {
92                         if (skb->len - offset == sizeof(u16)) {
93                                 rx->header = get_unaligned_le16(
94                                                 skb->data + offset);
95                                 rx->split_head = true;
96                                 offset += sizeof(u16);
97                                 break;
98                         }
99
100                         if (rx->split_head == true) {
101                                 rx->header |= (get_unaligned_le16(
102                                                 skb->data + offset) << 16);
103                                 rx->split_head = false;
104                                 offset += sizeof(u16);
105                         } else {
106                                 rx->header = get_unaligned_le32(skb->data +
107                                                                 offset);
108                                 offset += sizeof(u32);
109                         }
110
111                         /* take frame length from Data header 32-bit word */
112                         size = (u16)(rx->header & 0x7ff);
113                         if (size != ((~rx->header >> 16) & 0x7ff)) {
114                                 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
115                                            rx->header, offset);
116                                 return 0;
117                         }
118                         if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
119                                 netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
120                                            size);
121                                 return 0;
122                         }
123
124                         rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
125                         if (!rx->ax_skb)
126                                 return 0;
127
128                         rx->remaining = size;
129                 }
130
131                 if (rx->remaining > skb->len - offset) {
132                         copy_length = skb->len - offset;
133                         rx->remaining -= copy_length;
134                 } else {
135                         copy_length = rx->remaining;
136                         rx->remaining = 0;
137                 }
138
139                 data = skb_put(rx->ax_skb, copy_length);
140                 memcpy(data, skb->data + offset, copy_length);
141                 if (!rx->remaining)
142                         usbnet_skb_return(dev, rx->ax_skb);
143
144                 offset += (copy_length + 1) & 0xfffe;
145         }
146
147         if (skb->len != offset) {
148                 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
149                            skb->len, offset);
150                 return 0;
151         }
152
153         return 1;
154 }
155
156 int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
157 {
158         struct asix_common_private *dp = dev->driver_priv;
159         struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
160
161         return asix_rx_fixup_internal(dev, skb, rx);
162 }
163
164 struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
165                               gfp_t flags)
166 {
167         int padlen;
168         int headroom = skb_headroom(skb);
169         int tailroom = skb_tailroom(skb);
170         u32 packet_len;
171         u32 padbytes = 0xffff0000;
172
173         padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
174
175         /* We need to push 4 bytes in front of frame (packet_len)
176          * and maybe add 4 bytes after the end (if padlen is 4)
177          *
178          * Avoid skb_copy_expand() expensive call, using following rules :
179          * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
180          *   is false (and if we have 4 bytes of headroom)
181          * - We are allowed to put 4 bytes at tail if skb_cloned()
182          *   is false (and if we have 4 bytes of tailroom)
183          *
184          * TCP packets for example are cloned, but skb_header_release()
185          * was called in tcp stack, allowing us to use headroom for our needs.
186          */
187         if (!skb_header_cloned(skb) &&
188             !(padlen && skb_cloned(skb)) &&
189             headroom + tailroom >= 4 + padlen) {
190                 /* following should not happen, but better be safe */
191                 if (headroom < 4 ||
192                     tailroom < padlen) {
193                         skb->data = memmove(skb->head + 4, skb->data, skb->len);
194                         skb_set_tail_pointer(skb, skb->len);
195                 }
196         } else {
197                 struct sk_buff *skb2;
198
199                 skb2 = skb_copy_expand(skb, 4, padlen, flags);
200                 dev_kfree_skb_any(skb);
201                 skb = skb2;
202                 if (!skb)
203                         return NULL;
204         }
205
206         packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
207         skb_push(skb, 4);
208         cpu_to_le32s(&packet_len);
209         skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
210
211         if (padlen) {
212                 cpu_to_le32s(&padbytes);
213                 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
214                 skb_put(skb, sizeof(padbytes));
215         }
216
217         usbnet_set_skb_tx_stats(skb, 1, 0);
218         return skb;
219 }
220
221 int asix_set_sw_mii(struct usbnet *dev)
222 {
223         int ret;
224         ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
225         if (ret < 0)
226                 netdev_err(dev->net, "Failed to enable software MII access\n");
227         return ret;
228 }
229
230 int asix_set_hw_mii(struct usbnet *dev)
231 {
232         int ret;
233         ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
234         if (ret < 0)
235                 netdev_err(dev->net, "Failed to enable hardware MII access\n");
236         return ret;
237 }
238
239 int asix_read_phy_addr(struct usbnet *dev, int internal)
240 {
241         int offset = (internal ? 1 : 0);
242         u8 buf[2];
243         int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
244
245         netdev_dbg(dev->net, "asix_get_phy_addr()\n");
246
247         if (ret < 0) {
248                 netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
249                 goto out;
250         }
251         netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
252                    *((__le16 *)buf));
253         ret = buf[offset];
254
255 out:
256         return ret;
257 }
258
259 int asix_get_phy_addr(struct usbnet *dev)
260 {
261         /* return the address of the internal phy */
262         return asix_read_phy_addr(dev, 1);
263 }
264
265
266 int asix_sw_reset(struct usbnet *dev, u8 flags)
267 {
268         int ret;
269
270         ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
271         if (ret < 0)
272                 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
273
274         return ret;
275 }
276
277 u16 asix_read_rx_ctl(struct usbnet *dev)
278 {
279         __le16 v;
280         int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
281
282         if (ret < 0) {
283                 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
284                 goto out;
285         }
286         ret = le16_to_cpu(v);
287 out:
288         return ret;
289 }
290
291 int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
292 {
293         int ret;
294
295         netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
296         ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
297         if (ret < 0)
298                 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
299                            mode, ret);
300
301         return ret;
302 }
303
304 u16 asix_read_medium_status(struct usbnet *dev)
305 {
306         __le16 v;
307         int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
308
309         if (ret < 0) {
310                 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
311                            ret);
312                 return ret;     /* TODO: callers not checking for error ret */
313         }
314
315         return le16_to_cpu(v);
316
317 }
318
319 int asix_write_medium_mode(struct usbnet *dev, u16 mode)
320 {
321         int ret;
322
323         netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
324         ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
325         if (ret < 0)
326                 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
327                            mode, ret);
328
329         return ret;
330 }
331
332 int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
333 {
334         int ret;
335
336         netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
337         ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
338         if (ret < 0)
339                 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
340                            value, ret);
341
342         if (sleep)
343                 msleep(sleep);
344
345         return ret;
346 }
347
348 /*
349  * AX88772 & AX88178 have a 16-bit RX_CTL value
350  */
351 void asix_set_multicast(struct net_device *net)
352 {
353         struct usbnet *dev = netdev_priv(net);
354         struct asix_data *data = (struct asix_data *)&dev->data;
355         u16 rx_ctl = AX_DEFAULT_RX_CTL;
356
357         if (net->flags & IFF_PROMISC) {
358                 rx_ctl |= AX_RX_CTL_PRO;
359         } else if (net->flags & IFF_ALLMULTI ||
360                    netdev_mc_count(net) > AX_MAX_MCAST) {
361                 rx_ctl |= AX_RX_CTL_AMALL;
362         } else if (netdev_mc_empty(net)) {
363                 /* just broadcast and directed */
364         } else {
365                 /* We use the 20 byte dev->data
366                  * for our 8 byte filter buffer
367                  * to avoid allocating memory that
368                  * is tricky to free later */
369                 struct netdev_hw_addr *ha;
370                 u32 crc_bits;
371
372                 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
373
374                 /* Build the multicast hash filter. */
375                 netdev_for_each_mc_addr(ha, net) {
376                         crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
377                         data->multi_filter[crc_bits >> 3] |=
378                             1 << (crc_bits & 7);
379                 }
380
381                 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
382                                    AX_MCAST_FILTER_SIZE, data->multi_filter);
383
384                 rx_ctl |= AX_RX_CTL_AM;
385         }
386
387         asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
388 }
389
390 int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
391 {
392         struct usbnet *dev = netdev_priv(netdev);
393         __le16 res;
394
395         mutex_lock(&dev->phy_mutex);
396         asix_set_sw_mii(dev);
397         asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
398                                 (__u16)loc, 2, &res);
399         asix_set_hw_mii(dev);
400         mutex_unlock(&dev->phy_mutex);
401
402         netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
403                    phy_id, loc, le16_to_cpu(res));
404
405         return le16_to_cpu(res);
406 }
407
408 void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
409 {
410         struct usbnet *dev = netdev_priv(netdev);
411         __le16 res = cpu_to_le16(val);
412
413         netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
414                    phy_id, loc, val);
415         mutex_lock(&dev->phy_mutex);
416         asix_set_sw_mii(dev);
417         asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
418         asix_set_hw_mii(dev);
419         mutex_unlock(&dev->phy_mutex);
420 }
421
422 void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
423 {
424         struct usbnet *dev = netdev_priv(net);
425         u8 opt;
426
427         if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
428                 wolinfo->supported = 0;
429                 wolinfo->wolopts = 0;
430                 return;
431         }
432         wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
433         wolinfo->wolopts = 0;
434         if (opt & AX_MONITOR_LINK)
435                 wolinfo->wolopts |= WAKE_PHY;
436         if (opt & AX_MONITOR_MAGIC)
437                 wolinfo->wolopts |= WAKE_MAGIC;
438 }
439
440 int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
441 {
442         struct usbnet *dev = netdev_priv(net);
443         u8 opt = 0;
444
445         if (wolinfo->wolopts & WAKE_PHY)
446                 opt |= AX_MONITOR_LINK;
447         if (wolinfo->wolopts & WAKE_MAGIC)
448                 opt |= AX_MONITOR_MAGIC;
449
450         if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
451                               opt, 0, 0, NULL) < 0)
452                 return -EINVAL;
453
454         return 0;
455 }
456
457 int asix_get_eeprom_len(struct net_device *net)
458 {
459         return AX_EEPROM_LEN;
460 }
461
462 int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
463                     u8 *data)
464 {
465         struct usbnet *dev = netdev_priv(net);
466         u16 *eeprom_buff;
467         int first_word, last_word;
468         int i;
469
470         if (eeprom->len == 0)
471                 return -EINVAL;
472
473         eeprom->magic = AX_EEPROM_MAGIC;
474
475         first_word = eeprom->offset >> 1;
476         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
477
478         eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
479                               GFP_KERNEL);
480         if (!eeprom_buff)
481                 return -ENOMEM;
482
483         /* ax8817x returns 2 bytes from eeprom on read */
484         for (i = first_word; i <= last_word; i++) {
485                 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
486                                   &(eeprom_buff[i - first_word])) < 0) {
487                         kfree(eeprom_buff);
488                         return -EIO;
489                 }
490         }
491
492         memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
493         kfree(eeprom_buff);
494         return 0;
495 }
496
497 int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
498                     u8 *data)
499 {
500         struct usbnet *dev = netdev_priv(net);
501         u16 *eeprom_buff;
502         int first_word, last_word;
503         int i;
504         int ret;
505
506         netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
507                    eeprom->len, eeprom->offset, eeprom->magic);
508
509         if (eeprom->len == 0)
510                 return -EINVAL;
511
512         if (eeprom->magic != AX_EEPROM_MAGIC)
513                 return -EINVAL;
514
515         first_word = eeprom->offset >> 1;
516         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
517
518         eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
519                               GFP_KERNEL);
520         if (!eeprom_buff)
521                 return -ENOMEM;
522
523         /* align data to 16 bit boundaries, read the missing data from
524            the EEPROM */
525         if (eeprom->offset & 1) {
526                 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
527                                     &(eeprom_buff[0]));
528                 if (ret < 0) {
529                         netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
530                         goto free;
531                 }
532         }
533
534         if ((eeprom->offset + eeprom->len) & 1) {
535                 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
536                                     &(eeprom_buff[last_word - first_word]));
537                 if (ret < 0) {
538                         netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
539                         goto free;
540                 }
541         }
542
543         memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
544
545         /* write data to EEPROM */
546         ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL);
547         if (ret < 0) {
548                 netdev_err(net, "Failed to enable EEPROM write\n");
549                 goto free;
550         }
551         msleep(20);
552
553         for (i = first_word; i <= last_word; i++) {
554                 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
555                            i, eeprom_buff[i - first_word]);
556                 ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
557                                      eeprom_buff[i - first_word], 0, NULL);
558                 if (ret < 0) {
559                         netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
560                                    i);
561                         goto free;
562                 }
563                 msleep(20);
564         }
565
566         ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL);
567         if (ret < 0) {
568                 netdev_err(net, "Failed to disable EEPROM write\n");
569                 goto free;
570         }
571
572         ret = 0;
573 free:
574         kfree(eeprom_buff);
575         return ret;
576 }
577
578 void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
579 {
580         /* Inherit standard device info */
581         usbnet_get_drvinfo(net, info);
582         strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
583         strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
584         info->eedump_len = AX_EEPROM_LEN;
585 }
586
587 int asix_set_mac_address(struct net_device *net, void *p)
588 {
589         struct usbnet *dev = netdev_priv(net);
590         struct asix_data *data = (struct asix_data *)&dev->data;
591         struct sockaddr *addr = p;
592
593         if (netif_running(net))
594                 return -EBUSY;
595         if (!is_valid_ether_addr(addr->sa_data))
596                 return -EADDRNOTAVAIL;
597
598         memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
599
600         /* We use the 20 byte dev->data
601          * for our 6 byte mac buffer
602          * to avoid allocating memory that
603          * is tricky to free later */
604         memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
605         asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
606                                                         data->mac_addr);
607
608         return 0;
609 }