iPXE
velocity.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Adrian Jamr√≥z <adrian.jamroz@gmail.com>
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 
20 FILE_LICENCE ( GPL2_OR_LATER );
21 
22 #include <stdint.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <byteswap.h>
27 #include <ipxe/netdevice.h>
28 #include <ipxe/ethernet.h>
29 #include <ipxe/if_ether.h>
30 #include <ipxe/iobuf.h>
31 #include <ipxe/malloc.h>
32 #include <ipxe/pci.h>
33 #include <ipxe/mii.h>
34 #include "velocity.h"
35 
36 #define velocity_setbit(_reg, _mask) writeb ( readb ( _reg ) | _mask, _reg )
37 #define virt_to_le32bus(x) ( cpu_to_le32 ( virt_to_bus ( x ) ) )
38 
39 /** @file
40  *
41  * VIA Velocity network driver
42  *
43  */
44 
45 /******************************************************************************
46  *
47  * MII interface
48  *
49  ******************************************************************************
50  */
51 
52 /**
53  * Stop MII auto-polling
54  *
55  * @v vlc Velocity device
56  * @ret rc Return status code
57  */
58 static int velocity_autopoll_stop ( struct velocity_nic *vlc ) {
60 
61  /* Disable MII auto polling */
62  writeb ( 0, vlc->regs + VELOCITY_MIICR );
63 
64  /* Wait for disabling to take effect */
65  while ( timeout-- ) {
66  udelay ( 1 );
67  if ( readb ( vlc->regs + VELOCITY_MIISR ) &
69  return 0;
70  }
71 
72  DBGC ( vlc, "MII autopoll stop timeout\n" );
73  return -ETIMEDOUT;
74 }
75 
76 /**
77  * Start MII auto-polling
78  *
79  * @v vlc Velocity device
80  * @ret rc Return status code
81  */
82 static int velocity_autopoll_start ( struct velocity_nic *vlc ) {
84 
85  /* Enable MII auto polling */
87 
88  /* Wait for enabling to take effect */
89  while ( timeout-- ) {
90  udelay ( 1 );
91  if ( ( readb ( vlc->regs + VELOCITY_MIISR ) &
92  VELOCITY_MIISR_IDLE ) == 0 )
93  return 0;
94  }
95 
96  DBGC ( vlc, "MII autopoll start timeout\n" );
97  return -ETIMEDOUT;
98 }
99 
100 /**
101  * Read from MII register
102  *
103  * @v mdio MII interface
104  * @v phy PHY address
105  * @v reg Register address
106  * @ret value Data read, or negative error
107  */
108 static int velocity_mii_read ( struct mii_interface *mdio,
109  unsigned int phy __unused, unsigned int reg ) {
110  struct velocity_nic *vlc =
111  container_of ( mdio, struct velocity_nic, mdio );
113  int result;
114 
115  DBGC2 ( vlc, "VELOCITY %p MII read reg %d\n", vlc, reg );
116 
117  /* Disable autopolling before we can access MII */
118  velocity_autopoll_stop ( vlc );
119 
120  /* Send read command and address */
121  writeb ( reg, vlc->regs + VELOCITY_MIIADDR );
123 
124  /* Wait for read to complete */
125  while ( timeout-- ) {
126  udelay ( 1 );
127  if ( ( readb ( vlc->regs + VELOCITY_MIICR ) &
128  VELOCITY_MIICR_RCMD ) == 0 ) {
129  result = readw ( vlc->regs + VELOCITY_MIIDATA );
130  velocity_autopoll_start ( vlc );
131  return result;
132  }
133  }
134 
135  /* Restart autopolling */
136  velocity_autopoll_start ( vlc );
137 
138  DBGC ( vlc, "MII read timeout\n" );
139  return -ETIMEDOUT;
140 }
141 
142 /**
143  * Write to MII register
144  *
145  * @v mdio MII interface
146  * @v phy PHY address
147  * @v reg Register address
148  * @v data Data to write
149  * @ret rc Return status code
150  */
151 static int velocity_mii_write ( struct mii_interface *mdio,
152  unsigned int phy __unused, unsigned int reg,
153  unsigned int data) {
154  struct velocity_nic *vlc =
155  container_of ( mdio, struct velocity_nic, mdio );
157 
158  DBGC2 ( vlc, "VELOCITY %p MII write reg %d data 0x%04x\n",
159  vlc, reg, data );
160 
161  /* Disable autopolling before we can access MII */
162  velocity_autopoll_stop ( vlc );
163 
164  /* Send write command, data and destination register */
165  writeb ( reg, vlc->regs + VELOCITY_MIIADDR );
166  writew ( data, vlc->regs + VELOCITY_MIIDATA );
168 
169  /* Wait for write to complete */
170  while ( timeout-- ) {
171  udelay ( 1 );
172  if ( ( readb ( vlc->regs + VELOCITY_MIICR ) &
173  VELOCITY_MIICR_WCMD ) == 0 ) {
174  velocity_autopoll_start ( vlc );
175  return 0;
176  }
177  }
178 
179  /* Restart autopolling */
180  velocity_autopoll_start ( vlc );
181 
182  DBGC ( vlc, "MII write timeout\n" );
183  return -ETIMEDOUT;
184 }
185 
186 /** Velocity MII operations */
189  .write = velocity_mii_write,
190 };
191 
192 /**
193  * Set Link speed
194  *
195  * @v vlc Velocity device
196  */
197 static void velocity_set_link ( struct velocity_nic *vlc ) {
198  int tmp;
199 
200  /* Advertise 1000MBit */
201  tmp = mii_read ( &vlc->mii, MII_CTRL1000 );
203  mii_write ( &vlc->mii, MII_CTRL1000, tmp );
204 
205  /* Enable GBit operation in MII Control Register */
206  tmp = mii_read ( &vlc->mii, MII_BMCR );
207  tmp |= BMCR_SPEED1000;
208  mii_write ( &vlc->mii, MII_BMCR, tmp );
209 }
210 
211 /******************************************************************************
212  *
213  * Device reset
214  *
215  ******************************************************************************
216  */
217 
218 /**
219  * Reload eeprom contents
220  *
221  * @v vlc Velocity device
222  */
223 static int velocity_reload_eeprom ( struct velocity_nic *vlc ) {
225 
226  /* Initiate reload */
228 
229  /* Wait for reload to complete */
230  while ( timeout-- ) {
231  udelay ( 1 );
232  if ( ( readb ( vlc->regs + VELOCITY_EECSR ) &
233  VELOCITY_EECSR_RELOAD ) == 0 )
234  return 0;
235  }
236 
237  DBGC ( vlc, "VELOCITY %p EEPROM reload timeout\n", vlc );
238  return -ETIMEDOUT;
239 }
240 
241 /**
242  * Reset hardware
243  *
244  * @v vlc Velocity device
245  * @ret rc Return status code
246  */
247 static int velocity_reset ( struct velocity_nic *vlc ) {
249  uint8_t tmp;
250 
251  DBGC ( vlc, "VELOCITY %p reset\n", vlc );
252 
253  /* clear sticky Power state bits */
254  tmp = readb ( vlc->regs + VELOCITY_STICKY );
256  writeb ( tmp, vlc->regs + VELOCITY_STICKY );
257 
258  /* clear PACPI, which might have been enabled by the EEPROM reload */
259  tmp = readb ( vlc->regs + VELOCITY_CFGA );
261  writeb ( tmp, vlc->regs + VELOCITY_CFGA );
262 
264 
265  /* Wait for reset to complete */
266  while ( timeout-- ) {
267  udelay ( 1 );
268  if ( ( readb ( vlc->regs + VELOCITY_CRS1 ) &
269  VELOCITY_CR1_SFRST ) == 0 )
270  return 0;
271  }
272 
273  return -EINVAL;
274 }
275 
276 /******************************************************************************
277  *
278  * Link state
279  *
280  ******************************************************************************
281  */
282 
283 /**
284  * Check link state
285  *
286  * @v netdev Network device
287  */
288 static void velocity_check_link ( struct net_device *netdev ) {
289  struct velocity_nic *vlc = netdev->priv;
290 
291  if ( readb ( vlc->regs + VELOCITY_PHYSTS0 ) & VELOCITY_PHYSTS0_LINK ) {
293  DBGC ( vlc, "VELOCITY %p link up\n", vlc );
294  } else {
296  DBGC ( vlc, "VELOCITY %p link down\n", vlc );
297  }
298 
299  /* The card disables auto-poll after a link change */
300  velocity_autopoll_start ( vlc );
301 }
302 
303 /******************************************************************************
304  *
305  * Network device interface
306  *
307  ******************************************************************************
308  */
309 
310 /**
311  * Allocate descriptor rings
312  *
313  * @v vlc Velocity device
314  * @ret rc Return status code
315  */
316 static int velocity_alloc_rings ( struct velocity_nic *vlc ) {
317  int rc = 0;
318 
319  /* Allocate RX descriptor ring */
320  vlc->rx_prod = 0;
321  vlc->rx_cons = 0;
322  vlc->rx_commit = 0;
324  if ( ! vlc->rx_ring )
325  return -ENOMEM;
326 
327  memset ( vlc->rx_ring, 0, VELOCITY_RXDESC_SIZE );
328 
329  DBGC2 ( vlc, "VELOCITY %p RX ring start address: %p(phys: %#08lx)\n",
330  vlc, vlc->rx_ring, virt_to_bus ( vlc->rx_ring ) );
331 
332  /* Allocate TX descriptor ring */
333  vlc->tx_prod = 0;
334  vlc->tx_cons = 0;
336  if ( ! vlc->tx_ring ) {
337  rc = -ENOMEM;
338  goto err_tx_alloc;
339  }
340 
341  memset ( vlc->tx_ring, 0, VELOCITY_TXDESC_SIZE );
342 
343  /* Send RX ring to the card */
344  writel ( virt_to_bus ( vlc->rx_ring ),
345  vlc->regs + VELOCITY_RXDESC_ADDR_LO );
347 
348  /* Send TX ring to the card */
349  writel ( virt_to_bus ( vlc->tx_ring ),
352 
353  DBGC2 ( vlc, "VELOCITY %p TX ring start address: %p(phys: %#08lx)\n",
354  vlc, vlc->tx_ring, virt_to_bus ( vlc->tx_ring ) );
355 
356  return 0;
357 
358 err_tx_alloc:
360  return rc;
361 }
362 
363 /**
364  * Refill receive descriptor ring
365  *
366  * @v vlc Velocity device
367  */
368 static void velocity_refill_rx ( struct velocity_nic *vlc ) {
369  struct velocity_rx_descriptor *desc;
370  struct io_buffer *iobuf;
371  int rx_idx, i = 0;
372 
373  /* Check for new packets */
374  while ( ( vlc->rx_prod - vlc->rx_cons ) < VELOCITY_RXDESC_NUM ) {
375  iobuf = alloc_iob ( VELOCITY_RX_MAX_LEN );
376 
377  /* Memory pressure: try again next poll */
378  if ( ! iobuf )
379  break;
380 
381  rx_idx = ( vlc->rx_prod++ % VELOCITY_RXDESC_NUM );
382  desc = &vlc->rx_ring[rx_idx];
383 
384  /* Set descrptor fields */
385  desc->des1 = 0;
386  desc->addr = virt_to_le32bus ( iobuf-> data );
387  desc->des2 = cpu_to_le32 (
390 
391  vlc->rx_buffs[rx_idx] = iobuf;
392  i++;
393 
394  /* Return RX descriptors in blocks of 4 (hw requirement) */
395  if ( rx_idx % 4 == 3 ) {
396  int j;
397  for (j = 0; j < 4; j++) {
398  desc = &vlc->rx_ring[rx_idx - j];
399  desc->des0 = cpu_to_le32 ( VELOCITY_DES0_OWN );
400  }
401  vlc->rx_commit += 4;
402  }
403  }
404 
405  wmb();
406 
407  if ( vlc->rx_commit ) {
408  writew ( vlc->rx_commit,
410  vlc->rx_commit = 0;
411  }
412 
413  if ( i > 0 )
414  DBGC2 ( vlc, "VELOCITY %p refilled %d RX descriptors\n",
415  vlc, i );
416 }
417 
418 /**
419  * Open network device
420  *
421  * @v netdev Network device
422  * @ret rc Return status code
423  */
424 static int velocity_open ( struct net_device *netdev ) {
425  struct velocity_nic *vlc = netdev->priv;
426  int rc;
427 
428  DBGC ( vlc, "VELOCITY %p open\n", vlc );
429  DBGC ( vlc, "VELOCITY %p regs at: %p\n", vlc, vlc->regs );
430 
431  /* Allocate descriptor rings */
432  if ( ( rc = velocity_alloc_rings ( vlc ) ) != 0 )
433  return rc;
434 
435  velocity_refill_rx ( vlc );
436 
437  /* Enable TX/RX queue */
440  vlc->regs + VELOCITY_RXQCSRS );
441 
442  /* Enable interrupts */
443  writeb ( 0xff, vlc->regs + VELOCITY_IMR0 );
444  writeb ( 0xff, vlc->regs + VELOCITY_IMR1 );
445 
446  /* Start MAC */
450  vlc->regs + VELOCITY_CRS0 );
451 
452  /* Receive all packets */
453  writeb ( 0xff, vlc->regs + VELOCITY_RCR );
454 
455  /* Set initial link state */
457 
458  velocity_autopoll_start ( vlc );
459 
460  DBGC2 ( vlc, "VELOCITY %p CR3 %02x\n",
461  vlc, readb ( vlc->regs + 0x0B ) );
462 
463  return 0;
464 }
465 
466 /**
467  * Close network device
468  *
469  * @v netdev Network device
470  */
471 static void velocity_close ( struct net_device *netdev ) {
472  struct velocity_nic *vlc = netdev->priv;
473  int i;
474 
475  /* Stop NIC */
477  vlc->regs + VELOCITY_CRC0 );
479 
480  /* Clear RX ring information */
481  writel ( 0, vlc->regs + VELOCITY_RXDESC_ADDR_LO );
482  writew ( 0, vlc->regs + VELOCITY_RXDESCNUM );
483 
484  /* Destroy RX ring */
486  vlc->rx_ring = NULL;
487  vlc->rx_prod = 0;
488  vlc->rx_cons = 0;
489 
490  /* Discard receive buffers */
491  for ( i = 0 ; i < VELOCITY_RXDESC_NUM ; i++ ) {
492  if ( vlc->rx_buffs[i] )
493  free_iob ( vlc->rx_buffs[i] );
494  vlc->rx_buffs[i] = NULL;
495  }
496 
497  /* Clear TX ring information */
498  writel ( 0, vlc->regs + VELOCITY_TXDESC_ADDR_LO0 );
499  writew ( 0, vlc->regs + VELOCITY_TXDESCNUM );
500 
501  /* Destroy TX ring */
503  vlc->tx_ring = NULL;
504  vlc->tx_prod = 0;
505  vlc->tx_cons = 0;
506 }
507 
508 /**
509  * Transmit packet
510  *
511  * @v netdev Network device
512  * @v iobuf I/O buffer
513  * @ret rc Return status code
514  */
515 static int velocity_transmit ( struct net_device *netdev,
516  struct io_buffer *iobuf ) {
517  struct velocity_nic *vlc = netdev->priv;
518  struct velocity_tx_descriptor *desc;
519  unsigned int tx_idx;
520 
521  /* Pad packet to minimum length */
522  iob_pad ( iobuf, ETH_ZLEN );
523 
524  tx_idx = ( vlc->tx_prod++ % VELOCITY_TXDESC_NUM );
525  desc = &vlc->tx_ring[tx_idx];
526 
527  /* Set packet size and transfer ownership to NIC */
529  VELOCITY_DES2_SIZE ( iob_len ( iobuf ) ) );
530  /* Data in first desc fragment, only desc for packet, generate INT */
531  desc->des1 = cpu_to_le32 ( VELOCITY_DES1_FRAG ( 1 ) |
534 
535  desc->frags[0].addr = virt_to_le32bus ( iobuf->data );
536  desc->frags[0].des2 = cpu_to_le32 (
537  VELOCITY_DES2_SIZE ( iob_len ( iobuf ) ) );
538 
539  wmb();
540 
541  /* Initiate TX */
543 
544  DBGC2 ( vlc, "VELOCITY %p tx_prod=%d desc=%p iobuf=%p len=%zd\n",
545  vlc, tx_idx, desc, iobuf->data, iob_len ( iobuf ) );
546 
547  return 0;
548 }
549 
550 /**
551  * Poll for received packets.
552  *
553  * @v vlc Velocity device
554  */
555 static void velocity_poll_rx ( struct velocity_nic *vlc ) {
556  struct velocity_rx_descriptor *desc;
557  struct io_buffer *iobuf;
558  int rx_idx;
559  size_t len;
560  uint32_t des0;
561 
562  /* Check for packets */
563  while ( vlc->rx_cons != vlc->rx_prod ) {
564  rx_idx = ( vlc->rx_cons % VELOCITY_RXDESC_NUM );
565  desc = &vlc->rx_ring[rx_idx];
566 
567  des0 = cpu_to_le32 ( desc->des0 );
568 
569  /* Return if descriptor still in use */
570  if ( des0 & VELOCITY_DES0_OWN )
571  return;
572 
573  iobuf = vlc->rx_buffs[rx_idx];
574 
575  /* Get length, strip CRC */
576  len = VELOCITY_DES0_RMBC ( des0 ) - 4;
577  iob_put ( iobuf, len );
578 
579  DBGC2 ( vlc, "VELOCITY %p got packet on idx=%d (prod=%d), len %zd\n",
580  vlc, rx_idx, vlc->rx_prod % VELOCITY_RXDESC_NUM, len );
581 
582  if ( des0 & VELOCITY_DES0_RX_ERR ) {
583  /* Report receive error */
584  netdev_rx_err ( vlc->netdev, iobuf, -EINVAL );
585  DBGC ( vlc, "VELOCITY %p receive error, status: %02x\n",
586  vlc, des0 );
587  } else if ( des0 & VELOCITY_DES0_RXOK ) {
588  /* Report receive success */
589  netdev_rx( vlc->netdev, iobuf );
590  } else {
591  /* Card indicated neither success nor failure
592  * Technically this shouldn't happen, but we saw it
593  * in debugging once. */
594  DBGC ( vlc, "VELOCITY %p RX neither ERR nor OK: %04x\n",
595  vlc, des0 );
596  DBGC ( vlc, "packet len: %zd\n", len );
597  DBGC_HD ( vlc, iobuf->data, 64 );
598 
599  /* we don't know what it is, treat is as an error */
600  netdev_rx_err ( vlc->netdev, iobuf, -EINVAL );
601  }
602 
603  vlc->rx_cons++;
604  }
605 }
606 
607 /**
608  * Poll for completed packets.
609  *
610  * @v vlc Velocity device
611  */
612 static void velocity_poll_tx ( struct velocity_nic *vlc ) {
613  struct velocity_tx_descriptor *desc;
614  int tx_idx;
615 
616  /* Check for packets */
617  while ( vlc->tx_cons != vlc->tx_prod ) {
618  tx_idx = ( vlc->tx_cons % VELOCITY_TXDESC_NUM );
619  desc = &vlc->tx_ring[tx_idx];
620 
621  /* Return if descriptor still in use */
622  if ( le32_to_cpu ( desc->des0 ) & VELOCITY_DES0_OWN )
623  return;
624 
625  /* Report errors */
626  if ( le32_to_cpu ( desc->des0 ) & VELOCITY_DES0_TERR ) {
628  return;
629  }
630 
632 
633  DBGC2 ( vlc, "VELOCITY %p poll_tx cons=%d prod=%d tsr=%04x\n",
634  vlc, tx_idx, vlc->tx_prod % VELOCITY_TXDESC_NUM,
635  ( desc->des0 & 0xffff ) );
636  vlc->tx_cons++;
637  }
638 }
639 
640 /**
641  * Poll for completed and received packets
642  *
643  * @v netdev Network device
644  */
645 static void velocity_poll ( struct net_device *netdev ) {
646  struct velocity_nic *vlc = netdev->priv;
647  uint8_t isr1;
648 
649  isr1 = readb ( vlc->regs + VELOCITY_ISR1 );
650 
651  /* ACK interrupts */
652  writew ( 0xFFFF, vlc->regs + VELOCITY_ISR0 );
653 
654  /* Check for competed packets */
655  velocity_poll_rx ( vlc );
656  velocity_poll_tx ( vlc );
657 
658  if ( isr1 & VELOCITY_ISR1_SRCI ) {
659  /* Update linkstate */
660  DBGC2 ( vlc, "VELOCITY %p link status interrupt\n", vlc );
662  }
663 
664  velocity_refill_rx ( vlc );
665 
666  /* deal with potential RX stall caused by RX ring underrun */
668  vlc->regs + VELOCITY_RXQCSRS );
669 }
670 
671 /**
672  * Enable or disable interrupts
673  *
674  * @v netdev Network device
675  * @v enable Interrupts should be enabled
676  */
677 static void velocity_irq ( struct net_device *netdev, int enable ) {
678  struct velocity_nic *vlc = netdev->priv;
679 
680  DBGC ( vlc, "VELOCITY %p interrupts %s\n", vlc,
681  enable ? "enable" : "disable" );
682 
683  if (enable) {
684  /* Enable interrupts */
686  } else {
687  /* Disable interrupts */
689  }
690 }
691 
692 /** Velocity network device operations */
694  .open = velocity_open,
695  .close = velocity_close,
696  .transmit = velocity_transmit,
697  .poll = velocity_poll,
698  .irq = velocity_irq,
699 };
700 
701 /******************************************************************************
702  *
703  * PCI interface
704  *
705  ******************************************************************************
706  */
707 
708 /**
709  * Probe PCI device
710  *
711  * @v pci PCI device
712  * @ret rc Return status code
713  */
714 static int velocity_probe ( struct pci_device *pci ) {
715  struct net_device *netdev;
716  struct velocity_nic *vlc;
717  int rc;
718 
719  /* Allocate and initialise net device */
720  netdev = alloc_etherdev ( sizeof ( *vlc ) );
721  if ( ! netdev ) {
722  rc = -ENOMEM;
723  goto err_alloc;
724  }
726  vlc = netdev->priv;
727  pci_set_drvdata ( pci, netdev );
728  netdev->dev = &pci->dev;
729 
730  /* Fix up PCI device */
731  adjust_pci_device ( pci );
732 
733  /* Map registers */
734  vlc->regs = ioremap ( pci->membase, VELOCITY_BAR_SIZE );
735  vlc->netdev = netdev;
736 
737  /* Reset the NIC */
738  if ( ( rc = velocity_reset ( vlc ) ) != 0 )
739  goto err_reset;
740 
741  /* Reload EEPROM */
742  if ( ( rc = velocity_reload_eeprom ( vlc ) ) != 0 )
743  goto err_reset;
744 
745  /* Get MAC address */
746  netdev->hw_addr[0] = readb ( vlc->regs + VELOCITY_MAC0 );
747  netdev->hw_addr[1] = readb ( vlc->regs + VELOCITY_MAC1 );
748  netdev->hw_addr[2] = readb ( vlc->regs + VELOCITY_MAC2 );
749  netdev->hw_addr[3] = readb ( vlc->regs + VELOCITY_MAC3 );
750  netdev->hw_addr[4] = readb ( vlc->regs + VELOCITY_MAC4 );
751  netdev->hw_addr[5] = readb ( vlc->regs + VELOCITY_MAC5 );
752 
753  /* Initialise and reset MII interface */
755  mii_init ( &vlc->mii, &vlc->mdio, 0 );
756  if ( ( rc = mii_reset ( &vlc->mii ) ) != 0 ) {
757  DBGC ( vlc, "VELOCITY %p could not reset MII: %s\n",
758  vlc, strerror ( rc ) );
759  goto err_mii_reset;
760  }
761 
762  /* Enable proper link advertising */
763  velocity_set_link ( vlc );
764 
765  /* Register network device */
766  if ( ( rc = register_netdev ( netdev ) ) != 0 )
767  goto err_register_netdev;
768 
769  return 0;
770 
771  err_register_netdev:
772  err_mii_reset:
773  velocity_reset ( vlc );
774  err_reset:
776  netdev_put ( netdev );
777  err_alloc:
778  return rc;
779 }
780 
781 /**
782  * Remove PCI device
783  *
784  * @v pci PCI device
785  */
786 static void velocity_remove ( struct pci_device *pci ) {
787  struct net_device *netdev = pci_get_drvdata ( pci );
788  struct velocity_nic *vlc = netdev->priv;
789 
790  /* Unregister network device */
792 
793  /* Reset card */
794  velocity_reset ( vlc );
795 
796  /* Free network device */
798  netdev_put ( netdev );
799 }
800 
801 /** Velocity PCI device IDs */
802 static struct pci_device_id velocity_nics[] = {
803  PCI_ROM ( 0x1106, 0x3119, "vt6122", "VIA Velocity", 0 ),
804 };
805 
806 /** Velocity PCI driver */
807 struct pci_driver velocity_driver __pci_driver = {
808  .ids = velocity_nics,
809  .id_count = ( sizeof ( velocity_nics ) / sizeof ( velocity_nics[0] ) ),
812 };
#define VELOCITY_MIIADDR
Definition: velocity.h:297
#define VELOCITY_TXQCSRS
Definition: velocity.h:232
#define velocity_setbit(_reg, _mask)
Definition: velocity.c:36
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct velocity_tx_descriptor * tx_ring
Transmit descriptor ring.
Definition: velocity.h:351
#define VELOCITY_DES1_INTR
Definition: velocity.h:68
#define VELOCITY_DES0_RX_ERR
Definition: velocity.h:59
unsigned long membase
Memory base.
Definition: pci.h:194
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct velocity_rx_descriptor * rx_ring
Receive descriptor ring.
Definition: velocity.h:336
wmb()
unsigned int rx_cons
Receive consumer index.
Definition: velocity.h:342
uint8_t readb(volatile uint8_t *io_addr)
Read byte from memory-mapped device.
#define iob_put(iobuf, len)
Definition: iobuf.h:116
void netdev_rx_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Discard received packet.
Definition: netdevice.c:501
#define VELOCITY_DES0_RMBC(_n)
Definition: velocity.h:38
static int velocity_probe(struct pci_device *pci)
Probe PCI device.
Definition: velocity.c:714
#define ADVERTISE_1000FULL
Definition: mii.h:131
#define VELOCITY_ISR1_SRCI
Definition: velocity.h:198
A PCI driver.
Definition: pci.h:224
#define VELOCITY_ISR0
Interrupt status register 0.
Definition: velocity.h:186
#define VELOCITY_TXQCSRS_RUN0
Definition: velocity.h:249
#define le32_to_cpu(value)
Definition: byteswap.h:113
int(* open)(struct net_device *netdev)
Open network device.
Definition: netdevice.h:222
#define VELOCITY_ISR1
Interrupt status register 1.
Definition: velocity.h:197
Error codes.
#define VELOCITY_EECSR
Definition: velocity.h:318
uint16_t readw(volatile uint16_t *io_addr)
Read 16-bit word from memory-mapped device.
static void netdev_tx_complete_next(struct net_device *netdev)
Complete network transmission.
Definition: netdevice.h:758
I/O buffers.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
struct pci_device_id * ids
PCI ID table.
Definition: pci.h:226
int(* read)(struct mii_interface *mdio, unsigned int phy, unsigned int reg)
Read from MII register.
Definition: mii.h:27
struct pci_driver velocity_driver __pci_driver
Velocity PCI driver.
Definition: velocity.c:807
#define VELOCITY_RXDESCNUM
Definition: velocity.h:269
#define VELOCITY_IMR1
Interrupt mask register 1.
Definition: velocity.h:223
#define DBGC(...)
Definition: compiler.h:505
#define VELOCITY_DES2_IC
Definition: velocity.h:77
uint32_t des2
Definition: velocity.h:20
struct io_buffer * rx_buffs[VELOCITY_RXDESC_NUM]
Receive I/O buffers.
Definition: velocity.h:338
#define VELOCITY_MIIDATA
Definition: velocity.h:298
void netdev_tx_complete_next_err(struct net_device *netdev, int rc)
Complete network transmission.
Definition: netdevice.c:434
void netdev_link_down(struct net_device *netdev)
Mark network device as having link down.
Definition: netdevice.c:186
#define VELOCITY_MAC0
MAC address registers.
Definition: velocity.h:100
#define VELOCITY_MAC3
Definition: velocity.h:103
#define VELOCITY_RXDESC_RESIDUECNT
Definition: velocity.h:276
#define VELOCITY_CR1_SFRST
Definition: velocity.h:134
#define virt_to_le32bus(x)
Definition: velocity.c:37
static int velocity_autopoll_stop(struct velocity_nic *vlc)
Stop MII auto-polling.
Definition: velocity.c:58
static int velocity_alloc_rings(struct velocity_nic *vlc)
Allocate descriptor rings.
Definition: velocity.c:316
void adjust_pci_device(struct pci_device *pci)
Enable PCI device.
Definition: pci.c:149
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:128
#define VELOCITY_RXQCSR_RUN
Definition: velocity.h:257
#define VELOCITY_MIICR_WCMD
Definition: velocity.h:290
struct device dev
Generic device.
Definition: pci.h:189
uint32_t des0
Definition: rhine.h:11
void writeb(uint8_t data, volatile uint8_t *io_addr)
Write byte to memory-mapped device.
unsigned int rx_commit
Receive commit number.
Definition: velocity.h:348
static void mdio_init(struct mii_interface *mdio, struct mii_operations *op)
Initialise MII interface.
Definition: mii.h:63
#define VELOCITY_STICKY_DS1
Definition: velocity.h:314
#define VELOCITY_PHYSTS0_LINK
Definition: velocity.h:285
Dynamic memory allocation.
struct velocity_frag frags[7]
Definition: velocity.h:28
#define BMCR_SPEED1000
Definition: mii.h:41
#define VELOCITY_CRS1
Command register 1 (set)
Definition: velocity.h:133
static void netdev_init(struct net_device *netdev, struct net_device_operations *op)
Initialise a network device.
Definition: netdevice.h:498
static void pci_set_drvdata(struct pci_device *pci, void *priv)
Set PCI driver-private data.
Definition: pci.h:338
#define ENOMEM
Not enough space.
Definition: errno.h:534
#define VELOCITY_TIMEOUT_US
Default timeout.
Definition: velocity.h:16
#define VELOCITY_RING_ALIGN
Descriptor alignment.
Definition: velocity.h:94
#define VELOCITY_RXDESC_NUM
Number of receive descriptors.
Definition: velocity.h:84
FILE_LICENCE(GPL2_OR_LATER)
static void velocity_check_link(struct net_device *netdev)
Check link state.
Definition: velocity.c:288
static int velocity_mii_read(struct mii_interface *mdio, unsigned int phy __unused, unsigned int reg)
Read from MII register.
Definition: velocity.c:108
int result
Definition: bigint.h:148
static __always_inline unsigned long virt_to_bus(volatile const void *addr)
Convert virtual address to a bus address.
Definition: io.h:183
#define VELOCITY_MAC1
Definition: velocity.h:101
static int velocity_transmit(struct net_device *netdev, struct io_buffer *iobuf)
Transmit packet.
Definition: velocity.c:515
#define VELOCITY_CR1_DPOLL
Definition: velocity.h:137
static void velocity_remove(struct pci_device *pci)
Remove PCI device.
Definition: velocity.c:786
#define VELOCITY_RCR
Receive control register.
Definition: velocity.h:108
#define VELOCITY_DES0_RXOK
Definition: velocity.h:41
static struct mii_operations velocity_mii_operations
Velocity MII operations.
Definition: velocity.c:187
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition: netdevice.h:555
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
Ethernet protocol.
#define VELOCITY_TXDESC_NUM
Number of transmit descriptors.
Definition: velocity.h:89
void * priv
Driver private data.
Definition: netdevice.h:425
unsigned int rx_prod
Receive producer index.
Definition: velocity.h:340
#define VELOCITY_IMR0
Interrupt mask register 0.
Definition: velocity.h:220
static void netdev_link_up(struct net_device *netdev)
Mark network device as having link up.
Definition: netdevice.h:768
void writel(uint32_t data, volatile uint32_t *io_addr)
Write 32-bit dword to memory-mapped device.
void udelay(unsigned long usecs)
Delay for a fixed number of microseconds.
Definition: timer.c:60
static int velocity_reload_eeprom(struct velocity_nic *vlc)
Reload eeprom contents.
Definition: velocity.c:223
static struct net_device * netdev
Definition: gdbudp.c:52
static struct net_device_operations velocity_operations
Velocity network device operations.
Definition: velocity.c:693
#define MII_CTRL1000
Definition: mii.h:24
static void velocity_poll_rx(struct velocity_nic *vlc)
Poll for received packets.
Definition: velocity.c:555
static void velocity_poll_tx(struct velocity_nic *vlc)
Poll for completed packets.
Definition: velocity.c:612
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition: netdevice.c:844
#define cpu_to_le32(value)
Definition: byteswap.h:107
#define VELOCITY_CR0_TXON
Definition: velocity.h:127
A Velocity network card.
Definition: velocity.h:325
#define VELOCITY_PHYSTS0
Definition: velocity.h:284
#define VELOCITY_DES2_SIZE(_n)
Definition: velocity.h:78
unsigned int tx_prod
Transmit producer index.
Definition: velocity.h:353
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define VELOCITY_DES0_OWN
Definition: velocity.h:39
static void velocity_close(struct net_device *netdev)
Close network device.
Definition: velocity.c:471
#define VELOCITY_CRS0
Command register 0 (set)
Definition: velocity.h:126
static void velocity_poll(struct net_device *netdev)
Poll for completed and received packets.
Definition: velocity.c:645
#define VELOCITY_BAR_SIZE
Skeleton BAR size.
Definition: velocity.h:13
static unsigned int unsigned int reg
Definition: intel.h:245
PCI bus.
A PCI device.
Definition: pci.h:187
int register_netdev(struct net_device *netdev)
Register network device.
Definition: netdevice.c:667
#define VELOCITY_CRC3
Command register 3 (clear.
Definition: velocity.h:166
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
#define VELOCITY_RX_MAX_LEN
Receive buffer length.
Definition: velocity.h:97
uint8_t * tmp
Definition: entropy.h:156
#define DBGC_HD(...)
Definition: compiler.h:507
#define VELOCITY_RXQCSRS
Definition: velocity.h:251
A network device.
Definition: netdevice.h:348
Media Independent Interface.
#define VELOCITY_RXQCSR_WAK
Definition: velocity.h:255
#define VELOCITY_TXDESC_SIZE
Definition: velocity.h:90
static void netdev_nullify(struct net_device *netdev)
Stop using a network device.
Definition: netdevice.h:511
static int velocity_reset(struct velocity_nic *vlc)
Reset hardware.
Definition: velocity.c:247
Velocity descriptor format.
Definition: velocity.h:24
#define VELOCITY_TXDESC_ADDR_LO0
Definition: velocity.h:265
unsigned char uint8_t
Definition: stdint.h:10
#define MII_BMCR
Definition: atl1e.h:871
int mii_reset(struct mii_device *mii)
Reset MII device.
Definition: mii.c:74
#define VELOCITY_MIISR
Definition: velocity.h:282
#define ETH_ZLEN
Definition: if_ether.h:10
A PCI device ID list entry.
Definition: pci.h:151
#define ADVERTISE_1000HALF
Definition: mii.h:132
#define VELOCITY_MIISR_IDLE
Definition: velocity.h:283
unsigned int uint32_t
Definition: stdint.h:12
#define VELOCITY_DES1_FRAG(_n)
TX descriptor fragment number.
Definition: velocity.h:66
static struct xen_remove_from_physmap * remove
Definition: xenmem.h:39
static int velocity_mii_write(struct mii_interface *mdio, unsigned int phy __unused, unsigned int reg, unsigned int data)
Write to MII register.
Definition: velocity.c:151
#define VELOCITY_EECSR_RELOAD
Definition: velocity.h:319
#define VELOCITY_CRS3
Command register 3 (set)
Definition: velocity.h:148
Network device operations.
Definition: netdevice.h:213
void netdev_rx(struct net_device *netdev, struct io_buffer *iobuf)
Add packet to receive queue.
Definition: netdevice.c:470
struct device * dev
Underlying hardware device.
Definition: netdevice.h:360
static void mii_init(struct mii_device *mii, struct mii_interface *mdio, unsigned int address)
Initialise MII device.
Definition: mii.h:75
#define VELOCITY_MIICR
Definition: velocity.h:287
Network device management.
MII interface operations.
Definition: mii.h:18
static int velocity_open(struct net_device *netdev)
Open network device.
Definition: velocity.c:424
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static void * pci_get_drvdata(struct pci_device *pci)
Get PCI driver-private data.
Definition: pci.h:348
#define VELOCITY_CR0_RXON
Definition: velocity.h:128
static void velocity_irq(struct net_device *netdev, int enable)
Enable or disable interrupts.
Definition: velocity.c:677
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
#define VELOCITY_STICKY
Power Management Sticky Register.
Definition: velocity.h:312
int(* probe)(struct pci_device *pci)
Probe device.
Definition: pci.h:237
#define writew
Definition: w89c840.c:159
#define VELOCITY_DES0_TERR
Definition: velocity.h:40
#define VELOCITY_RXDESC_ADDR_LO
Definition: velocity.h:261
static struct pci_device_id velocity_nics[]
Velocity PCI device IDs.
Definition: velocity.c:802
void * data
Start of data.
Definition: iobuf.h:44
#define VELOCITY_DES1_TCPLS
Definition: velocity.h:67
#define VELOCITY_CFGA_PACPI
Definition: velocity.h:309
#define VELOCITY_MIICR_MAUTO
Definition: velocity.h:288
struct net_device * alloc_etherdev(size_t priv_size)
Allocate Ethernet device.
Definition: ethernet.c:264
unsigned int tx_cons
Transmit consumer index.
Definition: velocity.h:355
An MII interface.
Definition: mii.h:43
static void *__malloc malloc_dma(size_t size, size_t phys_align)
Allocate memory for DMA.
Definition: malloc.h:66
static void velocity_set_link(struct velocity_nic *vlc)
Set Link speed.
Definition: velocity.c:197
static void velocity_refill_rx(struct velocity_nic *vlc)
Refill receive descriptor ring.
Definition: velocity.c:368
#define VELOCITY_MAC4
Definition: velocity.h:104
#define VELOCITY_CR0_STOP
Definition: velocity.h:129
#define VELOCITY_MAC5
Definition: velocity.h:105
struct mii_device mii
MII device.
Definition: velocity.h:331
#define VELOCITY_TXQCSRS_WAK0
Definition: velocity.h:247
static int mii_read(int phy_id, int location)
Definition: epic100.c:499
void timeout(int)
uint32_t addr
Definition: velocity.h:19
#define VELOCITY_CFGA
Chip Configuration Register A.
Definition: velocity.h:308
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
#define VELOCITY_STICKY_DS0
Definition: velocity.h:313
#define VELOCITY_RXDESC_SIZE
Definition: velocity.h:85
struct mii_interface mdio
MII interface.
Definition: velocity.h:329
void * ioremap(unsigned long bus_addr, size_t len)
Map bus address as an I/O address.
struct net_device * netdev
Netdev.
Definition: velocity.h:333
static int velocity_autopoll_start(struct velocity_nic *vlc)
Start MII auto-polling.
Definition: velocity.c:82
static void free_dma(void *ptr, size_t size)
Free memory allocated with malloc_dma()
Definition: malloc.h:81
static int mii_write(struct mii_device *mii, unsigned int reg, unsigned int data)
Write to MII register.
Definition: mii.h:104
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:375
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
#define VELOCITY_MAC2
Definition: velocity.h:102
VIA Velocity network driver.
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
String functions.
#define PCI_ROM(_vendor, _device, _name, _description, _data)
Definition: pci.h:283
void iob_pad(struct io_buffer *iobuf, size_t min_len)
Pad I/O buffer.
Definition: iobpad.c:49
#define VELOCITY_CR3_GINTMSK1
Definition: velocity.h:153
#define VELOCITY_MIICR_RCMD
Definition: velocity.h:289
#define VELOCITY_CRC0
Command register 0 (clear)
Definition: velocity.h:157
#define VELOCITY_TXDESCNUM
Definition: velocity.h:270
void * regs
Registers.
Definition: velocity.h:327
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:32
#define VELOCITY_CR0_START
Definition: velocity.h:130