These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / net / smsc75xx.c
1 /*
2  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <byteswap.h>
30 #include <ipxe/ethernet.h>
31 #include <ipxe/usb.h>
32 #include <ipxe/usbnet.h>
33 #include <ipxe/profile.h>
34 #include "smsc75xx.h"
35
36 /** @file
37  *
38  * SMSC LAN75xx USB Ethernet driver
39  *
40  */
41
42 /** Interrupt completion profiler */
43 static struct profiler smsc75xx_intr_profiler __profiler =
44         { .name = "smsc75xx.intr" };
45
46 /** Bulk IN completion profiler */
47 static struct profiler smsc75xx_in_profiler __profiler =
48         { .name = "smsc75xx.in" };
49
50 /** Bulk OUT profiler */
51 static struct profiler smsc75xx_out_profiler __profiler =
52         { .name = "smsc75xx.out" };
53
54 /******************************************************************************
55  *
56  * Register access
57  *
58  ******************************************************************************
59  */
60
61 /**
62  * Write register (without byte-swapping)
63  *
64  * @v smsc75xx          SMSC75xx device
65  * @v address           Register address
66  * @v value             Register value
67  * @ret rc              Return status code
68  */
69 static int smsc75xx_raw_writel ( struct smsc75xx_device *smsc75xx,
70                                  unsigned int address, uint32_t value ) {
71         int rc;
72
73         /* Write register */
74         if ( ( rc = usb_control ( smsc75xx->usb, SMSC75XX_REGISTER_WRITE, 0,
75                                   address, &value, sizeof ( value ) ) ) != 0 ) {
76                 DBGC ( smsc75xx, "SMSC75XX %p could not write %03x: %s\n",
77                        smsc75xx, address, strerror ( rc ) );
78                 return rc;
79         }
80
81         return 0;
82 }
83
84 /**
85  * Write register
86  *
87  * @v smsc75xx          SMSC75xx device
88  * @v address           Register address
89  * @v value             Register value
90  * @ret rc              Return status code
91  */
92 static inline __attribute__ (( always_inline )) int
93 smsc75xx_writel ( struct smsc75xx_device *smsc75xx, unsigned int address,
94                   uint32_t value ) {
95         int rc;
96
97         /* Write register */
98         if ( ( rc = smsc75xx_raw_writel ( smsc75xx, address,
99                                           cpu_to_le32 ( value ) ) ) != 0 )
100                 return rc;
101
102         return 0;
103 }
104
105 /**
106  * Read register (without byte-swapping)
107  *
108  * @v smsc75xx          SMSC75xx device
109  * @v address           Register address
110  * @ret value           Register value
111  * @ret rc              Return status code
112  */
113 static int smsc75xx_raw_readl ( struct smsc75xx_device *smsc75xx,
114                                 unsigned int address, uint32_t *value ) {
115         int rc;
116
117         /* Read register */
118         if ( ( rc = usb_control ( smsc75xx->usb, SMSC75XX_REGISTER_READ, 0,
119                                   address, value, sizeof ( *value ) ) ) != 0 ) {
120                 DBGC ( smsc75xx, "SMSC75XX %p could not read %03x: %s\n",
121                        smsc75xx, address, strerror ( rc ) );
122                 return rc;
123         }
124
125         return 0;
126 }
127
128 /**
129  * Read register
130  *
131  * @v smsc75xx          SMSC75xx device
132  * @v address           Register address
133  * @ret value           Register value
134  * @ret rc              Return status code
135  */
136 static inline __attribute__ (( always_inline )) int
137 smsc75xx_readl ( struct smsc75xx_device *smsc75xx, unsigned int address,
138                  uint32_t *value ) {
139         int rc;
140
141         /* Read register */
142         if ( ( rc = smsc75xx_raw_readl ( smsc75xx, address, value ) ) != 0 )
143                 return rc;
144         le32_to_cpus ( value );
145
146         return 0;
147 }
148
149 /******************************************************************************
150  *
151  * EEPROM access
152  *
153  ******************************************************************************
154  */
155
156 /**
157  * Wait for EEPROM to become idle
158  *
159  * @v smsc75xx          SMSC75xx device
160  * @ret rc              Return status code
161  */
162 static int smsc75xx_eeprom_wait ( struct smsc75xx_device *smsc75xx ) {
163         uint32_t e2p_cmd;
164         unsigned int i;
165         int rc;
166
167         /* Wait for EPC_BSY to become clear */
168         for ( i = 0 ; i < SMSC75XX_EEPROM_MAX_WAIT_MS ; i++ ) {
169
170                 /* Read E2P_CMD and check EPC_BSY */
171                 if ( ( rc = smsc75xx_readl ( smsc75xx, SMSC75XX_E2P_CMD,
172                                              &e2p_cmd ) ) != 0 )
173                         return rc;
174                 if ( ! ( e2p_cmd & SMSC75XX_E2P_CMD_EPC_BSY ) )
175                         return 0;
176
177                 /* Delay */
178                 mdelay ( 1 );
179         }
180
181         DBGC ( smsc75xx, "SMSC75XX %p timed out waiting for EEPROM\n",
182                smsc75xx );
183         return -ETIMEDOUT;
184 }
185
186 /**
187  * Read byte from EEPROM
188  *
189  * @v smsc75xx          SMSC75xx device
190  * @v address           EEPROM address
191  * @ret byte            Byte read, or negative error
192  */
193 static int smsc75xx_eeprom_read_byte ( struct smsc75xx_device *smsc75xx,
194                                        unsigned int address ) {
195         uint32_t e2p_cmd;
196         uint32_t e2p_data;
197         int rc;
198
199         /* Wait for EEPROM to become idle */
200         if ( ( rc = smsc75xx_eeprom_wait ( smsc75xx ) ) != 0 )
201                 return rc;
202
203         /* Initiate read command */
204         e2p_cmd = ( SMSC75XX_E2P_CMD_EPC_BSY | SMSC75XX_E2P_CMD_EPC_CMD_READ |
205                     SMSC75XX_E2P_CMD_EPC_ADDR ( address ) );
206         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_E2P_CMD,
207                                       e2p_cmd ) ) != 0 )
208                 return rc;
209
210         /* Wait for command to complete */
211         if ( ( rc = smsc75xx_eeprom_wait ( smsc75xx ) ) != 0 )
212                 return rc;
213
214         /* Read EEPROM data */
215         if ( ( rc = smsc75xx_readl ( smsc75xx, SMSC75XX_E2P_DATA,
216                                      &e2p_data ) ) != 0 )
217                 return rc;
218
219         return SMSC75XX_E2P_DATA_GET ( e2p_data );
220 }
221
222 /**
223  * Read data from EEPROM
224  *
225  * @v smsc75xx          SMSC75xx device
226  * @v address           EEPROM address
227  * @v data              Data buffer
228  * @v len               Length of data
229  * @ret rc              Return status code
230  */
231 static int smsc75xx_eeprom_read ( struct smsc75xx_device *smsc75xx,
232                                   unsigned int address, void *data,
233                                   size_t len ) {
234         uint8_t *bytes;
235         int byte;
236
237         /* Read bytes */
238         for ( bytes = data ; len-- ; address++, bytes++ ) {
239                 byte = smsc75xx_eeprom_read_byte ( smsc75xx, address );
240                 if ( byte < 0 )
241                         return byte;
242                 *bytes = byte;
243         }
244
245         return 0;
246 }
247
248 /******************************************************************************
249  *
250  * MII access
251  *
252  ******************************************************************************
253  */
254
255 /**
256  * Wait for MII to become idle
257  *
258  * @v smsc75xx          SMSC75xx device
259  * @ret rc              Return status code
260  */
261 static int smsc75xx_mii_wait ( struct smsc75xx_device *smsc75xx ) {
262         uint32_t mii_access;
263         unsigned int i;
264         int rc;
265
266         /* Wait for MIIBZY to become clear */
267         for ( i = 0 ; i < SMSC75XX_MII_MAX_WAIT_MS ; i++ ) {
268
269                 /* Read MII_ACCESS and check MIIBZY */
270                 if ( ( rc = smsc75xx_readl ( smsc75xx, SMSC75XX_MII_ACCESS,
271                                              &mii_access ) ) != 0 )
272                         return rc;
273                 if ( ! ( mii_access & SMSC75XX_MII_ACCESS_MIIBZY ) )
274                         return 0;
275
276                 /* Delay */
277                 mdelay ( 1 );
278         }
279
280         DBGC ( smsc75xx, "SMSC75XX %p timed out waiting for MII\n",
281                smsc75xx );
282         return -ETIMEDOUT;
283 }
284
285 /**
286  * Read from MII register
287  *
288  * @v mii               MII interface
289  * @v reg               Register address
290  * @ret value           Data read, or negative error
291  */
292 static int smsc75xx_mii_read ( struct mii_interface *mii, unsigned int reg ) {
293         struct smsc75xx_device *smsc75xx =
294                 container_of ( mii, struct smsc75xx_device, mii );
295         uint32_t mii_access;
296         uint32_t mii_data;
297         int rc;
298
299         /* Wait for MII to become idle */
300         if ( ( rc = smsc75xx_mii_wait ( smsc75xx ) ) != 0 )
301                 return rc;
302
303         /* Initiate read command */
304         mii_access = ( SMSC75XX_MII_ACCESS_PHY_ADDRESS |
305                        SMSC75XX_MII_ACCESS_MIIRINDA ( reg ) |
306                        SMSC75XX_MII_ACCESS_MIIBZY );
307         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MII_ACCESS,
308                                       mii_access ) ) != 0 )
309                 return rc;
310
311         /* Wait for command to complete */
312         if ( ( rc = smsc75xx_mii_wait ( smsc75xx ) ) != 0 )
313                 return rc;
314
315         /* Read MII data */
316         if ( ( rc = smsc75xx_readl ( smsc75xx, SMSC75XX_MII_DATA,
317                                      &mii_data ) ) != 0 )
318                 return rc;
319
320         return SMSC75XX_MII_DATA_GET ( mii_data );
321 }
322
323 /**
324  * Write to MII register
325  *
326  * @v mii               MII interface
327  * @v reg               Register address
328  * @v data              Data to write
329  * @ret rc              Return status code
330  */
331 static int smsc75xx_mii_write ( struct mii_interface *mii, unsigned int reg,
332                                 unsigned int data ) {
333         struct smsc75xx_device *smsc75xx =
334                 container_of ( mii, struct smsc75xx_device, mii );
335         uint32_t mii_access;
336         uint32_t mii_data;
337         int rc;
338
339         /* Wait for MII to become idle */
340         if ( ( rc = smsc75xx_mii_wait ( smsc75xx ) ) != 0 )
341                 return rc;
342
343         /* Write MII data */
344         mii_data = SMSC75XX_MII_DATA_SET ( data );
345         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MII_DATA,
346                                       mii_data ) ) != 0 )
347                 return rc;
348
349         /* Initiate write command */
350         mii_access = ( SMSC75XX_MII_ACCESS_PHY_ADDRESS |
351                        SMSC75XX_MII_ACCESS_MIIRINDA ( reg ) |
352                        SMSC75XX_MII_ACCESS_MIIWNR |
353                        SMSC75XX_MII_ACCESS_MIIBZY );
354         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MII_ACCESS,
355                                       mii_access ) ) != 0 )
356                 return rc;
357
358         /* Wait for command to complete */
359         if ( ( rc = smsc75xx_mii_wait ( smsc75xx ) ) != 0 )
360                 return rc;
361
362         return 0;
363 }
364
365 /** MII operations */
366 static struct mii_operations smsc75xx_mii_operations = {
367         .read = smsc75xx_mii_read,
368         .write = smsc75xx_mii_write,
369 };
370
371 /**
372  * Check link status
373  *
374  * @v smsc75xx          SMSC75xx device
375  * @ret rc              Return status code
376  */
377 static int smsc75xx_check_link ( struct smsc75xx_device *smsc75xx ) {
378         struct net_device *netdev = smsc75xx->netdev;
379         int intr;
380         int rc;
381
382         /* Read PHY interrupt source */
383         intr = mii_read ( &smsc75xx->mii, SMSC75XX_MII_PHY_INTR_SOURCE );
384         if ( intr < 0 ) {
385                 rc = intr;
386                 DBGC ( smsc75xx, "SMSC75XX %p could not get PHY interrupt "
387                        "source: %s\n", smsc75xx, strerror ( rc ) );
388                 return rc;
389         }
390
391         /* Acknowledge PHY interrupt */
392         if ( ( rc = mii_write ( &smsc75xx->mii, SMSC75XX_MII_PHY_INTR_SOURCE,
393                                 intr ) ) != 0 ) {
394                 DBGC ( smsc75xx, "SMSC75XX %p could not acknowledge PHY "
395                        "interrupt: %s\n", smsc75xx, strerror ( rc ) );
396                 return rc;
397         }
398
399         /* Check link status */
400         if ( ( rc = mii_check_link ( &smsc75xx->mii, netdev ) ) != 0 ) {
401                 DBGC ( smsc75xx, "SMSC75XX %p could not check link: %s\n",
402                        smsc75xx, strerror ( rc ) );
403                 return rc;
404         }
405
406         DBGC ( smsc75xx, "SMSC75XX %p link %s (intr %#04x)\n",
407                smsc75xx, ( netdev_link_ok ( netdev ) ? "up" : "down" ), intr );
408         return 0;
409 }
410
411 /******************************************************************************
412  *
413  * Statistics (for debugging)
414  *
415  ******************************************************************************
416  */
417
418 /**
419  * Get statistics
420  *
421  * @v smsc75xx          SMSC75xx device
422  * @v stats             Statistics to fill in
423  * @ret rc              Return status code
424  */
425 static int smsc75xx_get_statistics ( struct smsc75xx_device *smsc75xx,
426                                      struct smsc75xx_statistics *stats ) {
427         int rc;
428
429         /* Get statistics */
430         if ( ( rc = usb_control ( smsc75xx->usb, SMSC75XX_GET_STATISTICS, 0, 0,
431                                   stats, sizeof ( *stats ) ) ) != 0 ) {
432                 DBGC ( smsc75xx, "SMSC75XX %p could not get statistics: %s\n",
433                        smsc75xx, strerror ( rc ) );
434                 return rc;
435         }
436
437         return 0;
438 }
439
440 /**
441  * Dump statistics (for debugging)
442  *
443  * @v smsc75xx          SMSC75xx device
444  * @ret rc              Return status code
445  */
446 static int smsc75xx_dump_statistics ( struct smsc75xx_device *smsc75xx ) {
447         struct smsc75xx_statistics stats;
448         int rc;
449
450         /* Do nothing unless debugging is enabled */
451         if ( ! DBG_LOG )
452                 return 0;
453
454         /* Get statistics */
455         if ( ( rc = smsc75xx_get_statistics ( smsc75xx, &stats ) ) != 0 )
456                 return rc;
457
458         /* Dump statistics */
459         DBGC ( smsc75xx, "SMSC75XX %p RXE fcs %d aln %d frg %d jab %d und %d "
460                "ovr %d drp %d\n", smsc75xx, le32_to_cpu ( stats.rx.err.fcs ),
461                le32_to_cpu ( stats.rx.err.alignment ),
462                le32_to_cpu ( stats.rx.err.fragment ),
463                le32_to_cpu ( stats.rx.err.jabber ),
464                le32_to_cpu ( stats.rx.err.undersize ),
465                le32_to_cpu ( stats.rx.err.oversize ),
466                le32_to_cpu ( stats.rx.err.dropped ) );
467         DBGC ( smsc75xx, "SMSC75XX %p RXB ucast %d bcast %d mcast %d\n",
468                smsc75xx, le32_to_cpu ( stats.rx.byte.unicast ),
469                le32_to_cpu ( stats.rx.byte.broadcast ),
470                le32_to_cpu ( stats.rx.byte.multicast ) );
471         DBGC ( smsc75xx, "SMSC75XX %p RXF ucast %d bcast %d mcast %d pause "
472                "%d\n", smsc75xx, le32_to_cpu ( stats.rx.frame.unicast ),
473                le32_to_cpu ( stats.rx.frame.broadcast ),
474                le32_to_cpu ( stats.rx.frame.multicast ),
475                le32_to_cpu ( stats.rx.frame.pause ) );
476         DBGC ( smsc75xx, "SMSC75XX %p TXE fcs %d def %d car %d cnt %d sgl %d "
477                "mul %d exc %d lat %d\n", smsc75xx,
478                le32_to_cpu ( stats.tx.err.fcs ),
479                le32_to_cpu ( stats.tx.err.deferral ),
480                le32_to_cpu ( stats.tx.err.carrier ),
481                le32_to_cpu ( stats.tx.err.count ),
482                le32_to_cpu ( stats.tx.err.single ),
483                le32_to_cpu ( stats.tx.err.multiple ),
484                le32_to_cpu ( stats.tx.err.excessive ),
485                le32_to_cpu ( stats.tx.err.late ) );
486         DBGC ( smsc75xx, "SMSC75XX %p TXB ucast %d bcast %d mcast %d\n",
487                smsc75xx, le32_to_cpu ( stats.tx.byte.unicast ),
488                le32_to_cpu ( stats.tx.byte.broadcast ),
489                le32_to_cpu ( stats.tx.byte.multicast ) );
490         DBGC ( smsc75xx, "SMSC75XX %p TXF ucast %d bcast %d mcast %d pause "
491                "%d\n", smsc75xx, le32_to_cpu ( stats.tx.frame.unicast ),
492                le32_to_cpu ( stats.tx.frame.broadcast ),
493                le32_to_cpu ( stats.tx.frame.multicast ),
494                le32_to_cpu ( stats.tx.frame.pause ) );
495
496         return 0;
497 }
498
499 /******************************************************************************
500  *
501  * Device reset
502  *
503  ******************************************************************************
504  */
505
506 /**
507  * Reset device
508  *
509  * @v smsc75xx          SMSC75xx device
510  * @ret rc              Return status code
511  */
512 static int smsc75xx_reset ( struct smsc75xx_device *smsc75xx ) {
513         uint32_t hw_cfg;
514         int rc;
515
516         /* Reset device */
517         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_HW_CFG,
518                                       SMSC75XX_HW_CFG_LRST ) ) != 0 )
519                 return rc;
520
521         /* Wait for reset to complete */
522         udelay ( SMSC75XX_RESET_DELAY_US );
523
524         /* Check that reset has completed */
525         if ( ( rc = smsc75xx_readl ( smsc75xx, SMSC75XX_HW_CFG,
526                                      &hw_cfg ) ) != 0 )
527                 return rc;
528         if ( hw_cfg & SMSC75XX_HW_CFG_LRST ) {
529                 DBGC ( smsc75xx, "SMSC75XX %p failed to reset\n", smsc75xx );
530                 return -ETIMEDOUT;
531         }
532
533         return 0;
534 }
535
536 /******************************************************************************
537  *
538  * Endpoint operations
539  *
540  ******************************************************************************
541  */
542
543 /**
544  * Complete interrupt transfer
545  *
546  * @v ep                USB endpoint
547  * @v iobuf             I/O buffer
548  * @v rc                Completion status code
549  */
550 static void smsc75xx_intr_complete ( struct usb_endpoint *ep,
551                                      struct io_buffer *iobuf, int rc ) {
552         struct smsc75xx_device *smsc75xx =
553                 container_of ( ep, struct smsc75xx_device, usbnet.intr );
554         struct net_device *netdev = smsc75xx->netdev;
555         struct smsc75xx_interrupt *intr;
556
557         /* Profile completions */
558         profile_start ( &smsc75xx_intr_profiler );
559
560         /* Ignore packets cancelled when the endpoint closes */
561         if ( ! ep->open )
562                 goto done;
563
564         /* Record USB errors against the network device */
565         if ( rc != 0 ) {
566                 DBGC ( smsc75xx, "SMSC75XX %p interrupt failed: %s\n",
567                        smsc75xx, strerror ( rc ) );
568                 DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
569                 netdev_rx_err ( netdev, NULL, rc );
570                 goto done;
571         }
572
573         /* Extract interrupt data */
574         if ( iob_len ( iobuf ) != sizeof ( *intr ) ) {
575                 DBGC ( smsc75xx, "SMSC75XX %p malformed interrupt\n",
576                        smsc75xx );
577                 DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
578                 netdev_rx_err ( netdev, NULL, rc );
579                 goto done;
580         }
581         intr = iobuf->data;
582
583         /* Record interrupt status */
584         smsc75xx->int_sts = le32_to_cpu ( intr->int_sts );
585         profile_stop ( &smsc75xx_intr_profiler );
586
587  done:
588         /* Free I/O buffer */
589         free_iob ( iobuf );
590 }
591
592 /** Interrupt endpoint operations */
593 static struct usb_endpoint_driver_operations smsc75xx_intr_operations = {
594         .complete = smsc75xx_intr_complete,
595 };
596
597 /**
598  * Complete bulk IN transfer
599  *
600  * @v ep                USB endpoint
601  * @v iobuf             I/O buffer
602  * @v rc                Completion status code
603  */
604 static void smsc75xx_in_complete ( struct usb_endpoint *ep,
605                                    struct io_buffer *iobuf, int rc ) {
606         struct smsc75xx_device *smsc75xx =
607                 container_of ( ep, struct smsc75xx_device, usbnet.in );
608         struct net_device *netdev = smsc75xx->netdev;
609         struct smsc75xx_rx_header *header;
610
611         /* Profile completions */
612         profile_start ( &smsc75xx_in_profiler );
613
614         /* Ignore packets cancelled when the endpoint closes */
615         if ( ! ep->open ) {
616                 free_iob ( iobuf );
617                 return;
618         }
619
620         /* Record USB errors against the network device */
621         if ( rc != 0 ) {
622                 DBGC ( smsc75xx, "SMSC75XX %p bulk IN failed: %s\n",
623                        smsc75xx, strerror ( rc ) );
624                 goto err;
625         }
626
627         /* Sanity check */
628         if ( iob_len ( iobuf ) < ( sizeof ( *header ) ) ) {
629                 DBGC ( smsc75xx, "SMSC75XX %p underlength bulk IN\n",
630                        smsc75xx );
631                 DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
632                 rc = -EINVAL;
633                 goto err;
634         }
635
636         /* Strip header */
637         header = iobuf->data;
638         iob_pull ( iobuf, sizeof ( *header ) );
639
640         /* Check for errors */
641         if ( header->command & cpu_to_le32 ( SMSC75XX_RX_RED ) ) {
642                 DBGC ( smsc75xx, "SMSC75XX %p receive error (%08x):\n",
643                        smsc75xx, le32_to_cpu ( header->command ) );
644                 DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
645                 rc = -EIO;
646                 goto err;
647         }
648
649         /* Hand off to network stack */
650         netdev_rx ( netdev, iob_disown ( iobuf ) );
651
652         profile_stop ( &smsc75xx_in_profiler );
653         return;
654
655  err:
656         /* Hand off to network stack */
657         netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
658 }
659
660 /** Bulk IN endpoint operations */
661 static struct usb_endpoint_driver_operations smsc75xx_in_operations = {
662         .complete = smsc75xx_in_complete,
663 };
664
665 /**
666  * Transmit packet
667  *
668  * @v smsc75xx          SMSC75xx device
669  * @v iobuf             I/O buffer
670  * @ret rc              Return status code
671  */
672 static int smsc75xx_out_transmit ( struct smsc75xx_device *smsc75xx,
673                                    struct io_buffer *iobuf ) {
674         struct smsc75xx_tx_header *header;
675         size_t len = iob_len ( iobuf );
676         int rc;
677
678         /* Profile transmissions */
679         profile_start ( &smsc75xx_out_profiler );
680
681         /* Prepend header */
682         if ( ( rc = iob_ensure_headroom ( iobuf, sizeof ( *header ) ) ) != 0 )
683                 return rc;
684         header = iob_push ( iobuf, sizeof ( *header ) );
685         header->command = cpu_to_le32 ( SMSC75XX_TX_FCS | len );
686         header->tag = 0;
687         header->mss = 0;
688
689         /* Enqueue I/O buffer */
690         if ( ( rc = usb_stream ( &smsc75xx->usbnet.out, iobuf, 0 ) ) != 0 )
691                 return rc;
692
693         profile_stop ( &smsc75xx_out_profiler );
694         return 0;
695 }
696
697 /**
698  * Complete bulk OUT transfer
699  *
700  * @v ep                USB endpoint
701  * @v iobuf             I/O buffer
702  * @v rc                Completion status code
703  */
704 static void smsc75xx_out_complete ( struct usb_endpoint *ep,
705                                     struct io_buffer *iobuf, int rc ) {
706         struct smsc75xx_device *smsc75xx =
707                 container_of ( ep, struct smsc75xx_device, usbnet.out );
708         struct net_device *netdev = smsc75xx->netdev;
709
710         /* Report TX completion */
711         netdev_tx_complete_err ( netdev, iobuf, rc );
712 }
713
714 /** Bulk OUT endpoint operations */
715 static struct usb_endpoint_driver_operations smsc75xx_out_operations = {
716         .complete = smsc75xx_out_complete,
717 };
718
719 /******************************************************************************
720  *
721  * Network device interface
722  *
723  ******************************************************************************
724  */
725
726 /**
727  * Open network device
728  *
729  * @v netdev            Network device
730  * @ret rc              Return status code
731  */
732 static int smsc75xx_open ( struct net_device *netdev ) {
733         struct smsc75xx_device *smsc75xx = netdev->priv;
734         union smsc75xx_mac mac;
735         int rc;
736
737         /* Clear stored interrupt status */
738         smsc75xx->int_sts = 0;
739
740         /* Copy MAC address */
741         memset ( &mac, 0, sizeof ( mac ) );
742         memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
743
744         /* Configure bulk IN empty response */
745         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_HW_CFG,
746                                       SMSC75XX_HW_CFG_BIR ) ) != 0 )
747                 goto err_hw_cfg;
748
749         /* Open USB network device */
750         if ( ( rc = usbnet_open ( &smsc75xx->usbnet ) ) != 0 ) {
751                 DBGC ( smsc75xx, "SMSC75XX %p could not open: %s\n",
752                        smsc75xx, strerror ( rc ) );
753                 goto err_open;
754         }
755
756         /* Configure interrupt endpoint */
757         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_INT_EP_CTL,
758                                       ( SMSC75XX_INT_EP_CTL_RDFO_EN |
759                                         SMSC75XX_INT_EP_CTL_PHY_EN ) ) ) != 0 )
760                 goto err_int_ep_ctl;
761
762         /* Configure bulk IN delay */
763         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_BULK_IN_DLY,
764                                       SMSC75XX_BULK_IN_DLY_SET ( 0 ) ) ) != 0 )
765                 goto err_bulk_in_dly;
766
767         /* Configure receive filters */
768         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_RFE_CTL,
769                                       ( SMSC75XX_RFE_CTL_AB |
770                                         SMSC75XX_RFE_CTL_AM |
771                                         SMSC75XX_RFE_CTL_AU ) ) ) != 0 )
772                 goto err_rfe_ctl;
773
774         /* Configure receive FIFO */
775         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_FCT_RX_CTL,
776                                       ( SMSC75XX_FCT_RX_CTL_EN |
777                                         SMSC75XX_FCT_RX_CTL_BAD ) ) ) != 0 )
778                 goto err_fct_rx_ctl;
779
780         /* Configure transmit FIFO */
781         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_FCT_TX_CTL,
782                                       SMSC75XX_FCT_TX_CTL_EN ) ) != 0 )
783                 goto err_fct_tx_ctl;
784
785         /* Configure receive datapath */
786         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MAC_RX,
787                                       ( SMSC75XX_MAC_RX_MAX_SIZE_DEFAULT |
788                                         SMSC75XX_MAC_RX_FCS |
789                                         SMSC75XX_MAC_RX_EN ) ) ) != 0 )
790                 goto err_mac_rx;
791
792         /* Configure transmit datapath */
793         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MAC_TX,
794                                       SMSC75XX_MAC_TX_EN ) ) != 0 )
795                 goto err_mac_tx;
796
797         /* Write MAC address high register */
798         if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_RX_ADDRH,
799                                           mac.addr.h ) ) != 0 )
800                 goto err_rx_addrh;
801
802         /* Write MAC address low register */
803         if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_RX_ADDRL,
804                                           mac.addr.l ) ) != 0 )
805                 goto err_rx_addrl;
806
807         /* Write MAC address perfect filter high register */
808         mac.addr.h |= cpu_to_le32 ( SMSC75XX_ADDR_FILTH_VALID );
809         if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_ADDR_FILTH ( 0 ),
810                                           mac.addr.h ) ) != 0 )
811                 goto err_addr_filth;
812
813         /* Write MAC address perfect filter low register */
814         if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_ADDR_FILTL ( 0 ),
815                                           mac.addr.l ) ) != 0 )
816                 goto err_addr_filtl;
817
818         /* Enable PHY interrupts */
819         if ( ( rc = mii_write ( &smsc75xx->mii, SMSC75XX_MII_PHY_INTR_MASK,
820                                 ( SMSC75XX_PHY_INTR_ANEG_DONE |
821                                   SMSC75XX_PHY_INTR_LINK_DOWN ) ) ) != 0 ) {
822                 DBGC ( smsc75xx, "SMSC75XX %p could not set PHY interrupt "
823                        "mask: %s\n", smsc75xx, strerror ( rc ) );
824                 goto err_phy_intr_mask;
825         }
826
827         /* Update link status */
828         smsc75xx_check_link ( smsc75xx );
829
830         return 0;
831
832  err_phy_intr_mask:
833  err_addr_filtl:
834  err_addr_filth:
835  err_rx_addrl:
836  err_rx_addrh:
837  err_mac_tx:
838  err_mac_rx:
839  err_fct_tx_ctl:
840  err_fct_rx_ctl:
841  err_rfe_ctl:
842  err_bulk_in_dly:
843  err_int_ep_ctl:
844         usbnet_close ( &smsc75xx->usbnet );
845  err_open:
846  err_hw_cfg:
847         smsc75xx_reset ( smsc75xx );
848         return rc;
849 }
850
851 /**
852  * Close network device
853  *
854  * @v netdev            Network device
855  */
856 static void smsc75xx_close ( struct net_device *netdev ) {
857         struct smsc75xx_device *smsc75xx = netdev->priv;
858
859         /* Close USB network device */
860         usbnet_close ( &smsc75xx->usbnet );
861
862         /* Dump statistics (for debugging) */
863         smsc75xx_dump_statistics ( smsc75xx );
864
865         /* Reset device */
866         smsc75xx_reset ( smsc75xx );
867 }
868
869 /**
870  * Transmit packet
871  *
872  * @v netdev            Network device
873  * @v iobuf             I/O buffer
874  * @ret rc              Return status code
875  */
876 static int smsc75xx_transmit ( struct net_device *netdev,
877                                struct io_buffer *iobuf ) {
878         struct smsc75xx_device *smsc75xx = netdev->priv;
879         int rc;
880
881         /* Transmit packet */
882         if ( ( rc = smsc75xx_out_transmit ( smsc75xx, iobuf ) ) != 0 )
883                 return rc;
884
885         return 0;
886 }
887
888 /**
889  * Poll for completed and received packets
890  *
891  * @v netdev            Network device
892  */
893 static void smsc75xx_poll ( struct net_device *netdev ) {
894         struct smsc75xx_device *smsc75xx = netdev->priv;
895         uint32_t int_sts;
896         int rc;
897
898         /* Poll USB bus */
899         usb_poll ( smsc75xx->bus );
900
901         /* Refill endpoints */
902         if ( ( rc = usbnet_refill ( &smsc75xx->usbnet ) ) != 0 )
903                 netdev_rx_err ( netdev, NULL, rc );
904
905         /* Do nothing more unless there are interrupts to handle */
906         int_sts = smsc75xx->int_sts;
907         if ( ! int_sts )
908                 return;
909
910         /* Check link status if applicable */
911         if ( int_sts & SMSC75XX_INT_STS_PHY_INT ) {
912                 smsc75xx_check_link ( smsc75xx );
913                 int_sts &= ~SMSC75XX_INT_STS_PHY_INT;
914         }
915
916         /* Record RX FIFO overflow if applicable */
917         if ( int_sts & SMSC75XX_INT_STS_RDFO_INT ) {
918                 DBGC2 ( smsc75xx, "SMSC75XX %p RX FIFO overflowed\n",
919                         smsc75xx );
920                 netdev_rx_err ( netdev, NULL, -ENOBUFS );
921                 int_sts &= ~SMSC75XX_INT_STS_RDFO_INT;
922         }
923
924         /* Check for unexpected interrupts */
925         if ( int_sts ) {
926                 DBGC ( smsc75xx, "SMSC75XX %p unexpected interrupt %#08x\n",
927                        smsc75xx, int_sts );
928                 netdev_rx_err ( netdev, NULL, -ENOTTY );
929         }
930
931         /* Clear interrupts */
932         if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_INT_STS,
933                                       smsc75xx->int_sts ) ) != 0 )
934                 netdev_rx_err ( netdev, NULL, rc );
935         smsc75xx->int_sts = 0;
936 }
937
938 /** SMSC75xx network device operations */
939 static struct net_device_operations smsc75xx_operations = {
940         .open           = smsc75xx_open,
941         .close          = smsc75xx_close,
942         .transmit       = smsc75xx_transmit,
943         .poll           = smsc75xx_poll,
944 };
945
946 /******************************************************************************
947  *
948  * USB interface
949  *
950  ******************************************************************************
951  */
952
953 /**
954  * Probe device
955  *
956  * @v func              USB function
957  * @v config            Configuration descriptor
958  * @ret rc              Return status code
959  */
960 static int smsc75xx_probe ( struct usb_function *func,
961                             struct usb_configuration_descriptor *config ) {
962         struct usb_device *usb = func->usb;
963         struct net_device *netdev;
964         struct smsc75xx_device *smsc75xx;
965         int rc;
966
967         /* Allocate and initialise structure */
968         netdev = alloc_etherdev ( sizeof ( *smsc75xx ) );
969         if ( ! netdev ) {
970                 rc = -ENOMEM;
971                 goto err_alloc;
972         }
973         netdev_init ( netdev, &smsc75xx_operations );
974         netdev->dev = &func->dev;
975         smsc75xx = netdev->priv;
976         memset ( smsc75xx, 0, sizeof ( *smsc75xx ) );
977         smsc75xx->usb = usb;
978         smsc75xx->bus = usb->port->hub->bus;
979         smsc75xx->netdev = netdev;
980         usbnet_init ( &smsc75xx->usbnet, func, &smsc75xx_intr_operations,
981                       &smsc75xx_in_operations, &smsc75xx_out_operations );
982         usb_refill_init ( &smsc75xx->usbnet.intr, 0, SMSC75XX_INTR_MAX_FILL );
983         usb_refill_init ( &smsc75xx->usbnet.in, SMSC75XX_IN_MTU,
984                           SMSC75XX_IN_MAX_FILL );
985         mii_init ( &smsc75xx->mii, &smsc75xx_mii_operations );
986         DBGC ( smsc75xx, "SMSC75XX %p on %s\n", smsc75xx, func->name );
987
988         /* Describe USB network device */
989         if ( ( rc = usbnet_describe ( &smsc75xx->usbnet, config ) ) != 0 ) {
990                 DBGC ( smsc75xx, "SMSC75XX %p could not describe: %s\n",
991                        smsc75xx, strerror ( rc ) );
992                 goto err_describe;
993         }
994
995         /* Reset device */
996         if ( ( rc = smsc75xx_reset ( smsc75xx ) ) != 0 )
997                 goto err_reset;
998
999         /* Read MAC address */
1000         if ( ( rc = smsc75xx_eeprom_read ( smsc75xx, SMSC75XX_EEPROM_MAC,
1001                                            netdev->hw_addr, ETH_ALEN ) ) != 0 )
1002                 goto err_eeprom_read;
1003
1004         /* Register network device */
1005         if ( ( rc = register_netdev ( netdev ) ) != 0 )
1006                 goto err_register;
1007
1008         usb_func_set_drvdata ( func, netdev );
1009         return 0;
1010
1011         unregister_netdev ( netdev );
1012  err_register:
1013  err_eeprom_read:
1014  err_reset:
1015  err_describe:
1016         netdev_nullify ( netdev );
1017         netdev_put ( netdev );
1018  err_alloc:
1019         return rc;
1020 }
1021
1022 /**
1023  * Remove device
1024  *
1025  * @v func              USB function
1026  */
1027 static void smsc75xx_remove ( struct usb_function *func ) {
1028         struct net_device *netdev = usb_func_get_drvdata ( func );
1029
1030         unregister_netdev ( netdev );
1031         netdev_nullify ( netdev );
1032         netdev_put ( netdev );
1033 }
1034
1035 /** SMSC75xx device IDs */
1036 static struct usb_device_id smsc75xx_ids[] = {
1037         {
1038                 .name = "smsc7500",
1039                 .vendor = 0x0424,
1040                 .product = 0x7500,
1041                 .class = { 0xff, 0x00, 0xff },
1042         },
1043         {
1044                 .name = "smsc7505",
1045                 .vendor = 0x0424,
1046                 .product = 0x7505,
1047                 .class = { 0xff, 0x00, 0xff },
1048         },
1049 };
1050
1051 /** SMSC LAN75xx driver */
1052 struct usb_driver smsc75xx_driver __usb_driver = {
1053         .ids = smsc75xx_ids,
1054         .id_count = ( sizeof ( smsc75xx_ids ) / sizeof ( smsc75xx_ids[0] ) ),
1055         .probe = smsc75xx_probe,
1056         .remove = smsc75xx_remove,
1057 };