2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
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 any later version.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
30 #include <ipxe/netdevice.h>
31 #include <ipxe/ethernet.h>
32 #include <ipxe/umalloc.h>
33 #include <ipxe/efi/efi.h>
34 #include <ipxe/efi/efi_driver.h>
35 #include <ipxe/efi/efi_pci.h>
36 #include <ipxe/efi/efi_utils.h>
37 #include <ipxe/efi/Protocol/NetworkInterfaceIdentifier.h>
38 #include <ipxe/efi/IndustryStandard/Acpi10.h>
47 /* Error numbers generated by NII */
48 #define EIO_INVALID_CDB __einfo_error ( EINFO_EIO_INVALID_CDB )
49 #define EINFO_EIO_INVALID_CDB \
50 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_INVALID_CDB, \
52 #define EIO_INVALID_CPB __einfo_error ( EINFO_EIO_INVALID_CPB )
53 #define EINFO_EIO_INVALID_CPB \
54 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_INVALID_CPB, \
56 #define EIO_BUSY __einfo_error ( EINFO_EIO_BUSY )
57 #define EINFO_EIO_BUSY \
58 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_BUSY, \
60 #define EIO_QUEUE_FULL __einfo_error ( EINFO_EIO_QUEUE_FULL )
61 #define EINFO_EIO_QUEUE_FULL \
62 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_QUEUE_FULL, \
64 #define EIO_ALREADY_STARTED __einfo_error ( EINFO_EIO_ALREADY_STARTED )
65 #define EINFO_EIO_ALREADY_STARTED \
66 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_ALREADY_STARTED, \
68 #define EIO_NOT_STARTED __einfo_error ( EINFO_EIO_NOT_STARTED )
69 #define EINFO_EIO_NOT_STARTED \
70 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NOT_STARTED, \
72 #define EIO_NOT_SHUTDOWN __einfo_error ( EINFO_EIO_NOT_SHUTDOWN )
73 #define EINFO_EIO_NOT_SHUTDOWN \
74 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NOT_SHUTDOWN, \
76 #define EIO_ALREADY_INITIALIZED __einfo_error ( EINFO_EIO_ALREADY_INITIALIZED )
77 #define EINFO_EIO_ALREADY_INITIALIZED \
78 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_ALREADY_INITIALIZED, \
79 "Already initialized" )
80 #define EIO_NOT_INITIALIZED __einfo_error ( EINFO_EIO_NOT_INITIALIZED )
81 #define EINFO_EIO_NOT_INITIALIZED \
82 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NOT_INITIALIZED, \
84 #define EIO_DEVICE_FAILURE __einfo_error ( EINFO_EIO_DEVICE_FAILURE )
85 #define EINFO_EIO_DEVICE_FAILURE \
86 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_DEVICE_FAILURE, \
88 #define EIO_NVDATA_FAILURE __einfo_error ( EINFO_EIO_NVDATA_FAILURE )
89 #define EINFO_EIO_NVDATA_FAILURE \
90 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NVDATA_FAILURE, \
91 "Non-volatile data failure" )
92 #define EIO_UNSUPPORTED __einfo_error ( EINFO_EIO_UNSUPPORTED )
93 #define EINFO_EIO_UNSUPPORTED \
94 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_UNSUPPORTED, \
96 #define EIO_BUFFER_FULL __einfo_error ( EINFO_EIO_BUFFER_FULL )
97 #define EINFO_EIO_BUFFER_FULL \
98 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_BUFFER_FULL, \
100 #define EIO_INVALID_PARAMETER __einfo_error ( EINFO_EIO_INVALID_PARAMETER )
101 #define EINFO_EIO_INVALID_PARAMETER \
102 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_INVALID_PARAMETER, \
103 "Invalid parameter" )
104 #define EIO_INVALID_UNDI __einfo_error ( EINFO_EIO_INVALID_UNDI )
105 #define EINFO_EIO_INVALID_UNDI \
106 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_INVALID_UNDI, \
108 #define EIO_IPV4_NOT_SUPPORTED __einfo_error ( EINFO_EIO_IPV4_NOT_SUPPORTED )
109 #define EINFO_EIO_IPV4_NOT_SUPPORTED \
110 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_IPV4_NOT_SUPPORTED, \
111 "IPv4 not supported" )
112 #define EIO_IPV6_NOT_SUPPORTED __einfo_error ( EINFO_EIO_IPV6_NOT_SUPPORTED )
113 #define EINFO_EIO_IPV6_NOT_SUPPORTED \
114 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_IPV6_NOT_SUPPORTED, \
115 "IPv6 not supported" )
116 #define EIO_NOT_ENOUGH_MEMORY __einfo_error ( EINFO_EIO_NOT_ENOUGH_MEMORY )
117 #define EINFO_EIO_NOT_ENOUGH_MEMORY \
118 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NOT_ENOUGH_MEMORY, \
119 "Not enough memory" )
120 #define EIO_NO_DATA __einfo_error ( EINFO_EIO_NO_DATA )
121 #define EINFO_EIO_NO_DATA \
122 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NO_DATA, \
124 #define EIO_STAT( stat ) \
125 EUNIQ ( EINFO_EIO, -(stat), EIO_INVALID_CDB, EIO_INVALID_CPB, \
126 EIO_BUSY, EIO_QUEUE_FULL, EIO_ALREADY_STARTED, \
127 EIO_NOT_STARTED, EIO_NOT_SHUTDOWN, EIO_ALREADY_INITIALIZED, \
128 EIO_NOT_INITIALIZED, EIO_DEVICE_FAILURE, EIO_NVDATA_FAILURE, \
129 EIO_UNSUPPORTED, EIO_BUFFER_FULL, EIO_INVALID_PARAMETER, \
130 EIO_INVALID_UNDI, EIO_IPV4_NOT_SUPPORTED, \
131 EIO_IPV6_NOT_SUPPORTED, EIO_NOT_ENOUGH_MEMORY, EIO_NO_DATA )
135 * This is defined in <ipxe/efi/IndustryStandard/Pci22.h>, but we
136 * can't #include that since it collides with <ipxe/pci.h>.
138 #define PCI_MAX_BAR 6
143 struct efi_device *efidev;
144 /** Network interface identifier protocol */
145 EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *nii;
146 /** !PXE structure */
149 EFIAPI VOID ( * issue ) ( UINT64 cdb );
150 /** Generic device */
154 EFI_HANDLE pci_device;
155 /** PCI I/O protocol */
156 EFI_PCI_IO_PROTOCOL *pci_io;
158 unsigned int mem_bar;
162 /** Broadcast address */
163 PXE_MAC_ADDR broadcast;
164 /** Maximum packet length */
167 /** Hardware transmit/receive buffer */
169 /** Hardware transmit/receive buffer length */
172 /** Saved task priority level */
175 /** Media status is supported */
178 /** Current transmit buffer */
179 struct io_buffer *txbuf;
180 /** Current receive buffer */
181 struct io_buffer *rxbuf;
184 /** Maximum number of received packets per poll */
185 #define NII_RX_QUOTA 4
188 * Open PCI I/O protocol and identify BARs
191 * @ret rc Return status code
193 static int nii_pci_open ( struct nii_nic *nii ) {
194 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
195 EFI_HANDLE device = nii->efidev->device;
196 EFI_HANDLE pci_device;
198 EFI_PCI_IO_PROTOCOL *pci_io;
202 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *acpi;
209 /* Locate PCI I/O protocol */
210 if ( ( rc = efi_locate_device ( device, &efi_pci_io_protocol_guid,
211 &pci_device ) ) != 0 ) {
212 DBGC ( nii, "NII %s could not locate PCI I/O protocol: %s\n",
213 nii->dev.name, strerror ( rc ) );
216 nii->pci_device = pci_device;
218 /* Open PCI I/O protocol */
219 if ( ( efirc = bs->OpenProtocol ( pci_device, &efi_pci_io_protocol_guid,
220 &pci_io.interface, efi_image_handle,
222 EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
223 rc = -EEFI ( efirc );
224 DBGC ( nii, "NII %s could not open PCI I/O protocol: %s\n",
225 nii->dev.name, strerror ( rc ) );
228 nii->pci_io = pci_io.pci_io;
230 /* Identify memory and I/O BARs */
231 nii->mem_bar = PCI_MAX_BAR;
232 nii->io_bar = PCI_MAX_BAR;
233 for ( bar = 0 ; bar < PCI_MAX_BAR ; bar++ ) {
234 efirc = nii->pci_io->GetBarAttributes ( nii->pci_io, bar, NULL,
236 if ( efirc == EFI_UNSUPPORTED ) {
237 /* BAR not present; ignore */
241 rc = -EEFI ( efirc );
242 DBGC ( nii, "NII %s could not get BAR %d attributes: "
243 "%s\n", nii->dev.name, bar, strerror ( rc ) );
244 goto err_get_bar_attributes;
246 if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM ) {
248 } else if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_IO ) {
251 bs->FreePool ( desc.resource );
253 DBGC ( nii, "NII %s has ", nii->dev.name );
254 if ( nii->mem_bar < PCI_MAX_BAR ) {
255 DBGC ( nii, "memory BAR %d and ", nii->mem_bar );
257 DBGC ( nii, "no memory BAR and " );
259 if ( nii->io_bar < PCI_MAX_BAR ) {
260 DBGC ( nii, "I/O BAR %d\n", nii->io_bar );
262 DBGC ( nii, "no I/O BAR\n" );
267 err_get_bar_attributes:
268 bs->CloseProtocol ( pci_device, &efi_pci_io_protocol_guid,
269 efi_image_handle, device );
276 * Close PCI I/O protocol
279 * @ret rc Return status code
281 static void nii_pci_close ( struct nii_nic *nii ) {
282 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
284 bs->CloseProtocol ( nii->pci_device, &efi_pci_io_protocol_guid,
285 efi_image_handle, nii->efidev->device );
291 * @v unique_id NII NIC
293 * @v len Length of data
295 * @v data Data buffer
297 static EFIAPI VOID nii_io ( UINT64 unique_id, UINT8 op, UINT8 len, UINT64 addr,
299 struct nii_nic *nii = ( ( void * ) ( intptr_t ) unique_id );
300 EFI_PCI_IO_PROTOCOL_ACCESS *access;
301 EFI_PCI_IO_PROTOCOL_IO_MEM io;
302 EFI_PCI_IO_PROTOCOL_WIDTH width;
307 /* Determine accessor and BAR */
308 if ( op & ( PXE_MEM_READ | PXE_MEM_WRITE ) ) {
309 access = &nii->pci_io->Mem;
312 access = &nii->pci_io->Io;
316 /* Determine operaton */
317 io = ( ( op & ( PXE_IO_WRITE | PXE_MEM_WRITE ) ) ?
318 access->Write : access->Read );
320 /* Determine width */
321 width = ( fls ( len ) - 1 );
323 /* Issue operation */
324 if ( ( efirc = io ( nii->pci_io, width, bar, addr, 1,
325 ( ( void * ) ( intptr_t ) data ) ) ) != 0 ) {
326 rc = -EEFI ( efirc );
327 DBGC ( nii, "NII %s I/O operation %#x failed: %s\n",
328 nii->dev.name, op, strerror ( rc ) );
329 /* No way to report failure */
337 * @v unique_id NII NIC
338 * @v microseconds Delay in microseconds
340 static EFIAPI VOID nii_delay ( UINT64 unique_id __unused, UINTN microseconds ) {
342 udelay ( microseconds );
348 * @v unique_id NII NIC
349 * @v acquire Acquire lock
351 static EFIAPI VOID nii_block ( UINT64 unique_id, UINT32 acquire ) {
352 struct nii_nic *nii = ( ( void * ) ( intptr_t ) unique_id );
353 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
355 /* This functionality (which is copied verbatim from the
356 * SnpDxe implementation of this function) appears to be
357 * totally brain-dead, since it produces no actual blocking
361 nii->saved_tpl = bs->RaiseTPL ( TPL_NOTIFY );
363 bs->RestoreTPL ( nii->saved_tpl );
368 * Construct operation from opcode and flags
374 #define NII_OP( opcode, opflags ) ( (opcode) | ( (opflags) << 16 ) )
377 * Extract opcode from operation
382 #define NII_OPCODE( op ) ( (op) & 0xffff )
385 * Extract flags from operation
390 #define NII_OPFLAGS( op ) ( (op) >> 16 )
393 * Issue command with parameter block and data block
397 * @v cpb Command parameter block, or NULL
398 * @v cpb_len Command parameter block length
399 * @v db Data block, or NULL
400 * @v db_len Data block length
401 * @ret stat Status flags, or negative status code
403 static int nii_issue_cpb_db ( struct nii_nic *nii, unsigned int op, void *cpb,
404 size_t cpb_len, void *db, size_t db_len ) {
407 /* Prepare command descriptor block */
408 memset ( &cdb, 0, sizeof ( cdb ) );
409 cdb.OpCode = NII_OPCODE ( op );
410 cdb.OpFlags = NII_OPFLAGS ( op );
411 cdb.CPBaddr = ( ( intptr_t ) cpb );
412 cdb.CPBsize = cpb_len;
413 cdb.DBaddr = ( ( intptr_t ) db );
415 cdb.IFnum = nii->nii->IfNum;
418 DBGC2 ( nii, "NII %s issuing %02x:%04x ifnum %d%s%s\n",
419 nii->dev.name, cdb.OpCode, cdb.OpFlags, cdb.IFnum,
420 ( cpb ? " cpb" : "" ), ( db ? " db" : "" ) );
422 DBGC2_HD ( nii, cpb, cpb_len );
424 DBGC2_HD ( nii, db, db_len );
425 nii->issue ( ( intptr_t ) &cdb );
427 /* Check completion status */
428 if ( cdb.StatCode != PXE_STATCODE_SUCCESS )
429 return -cdb.StatCode;
431 /* Return command-specific status flags */
432 return ( cdb.StatFlags & ~PXE_STATFLAGS_STATUS_MASK );
436 * Issue command with parameter block
440 * @v cpb Command parameter block, or NULL
441 * @v cpb_len Command parameter block length
442 * @ret stat Status flags, or negative status code
444 static int nii_issue_cpb ( struct nii_nic *nii, unsigned int op, void *cpb,
447 return nii_issue_cpb_db ( nii, op, cpb, cpb_len, NULL, 0 );
451 * Issue command with data block
455 * @v db Data block, or NULL
456 * @v db_len Data block length
457 * @ret stat Status flags, or negative status code
459 static int nii_issue_db ( struct nii_nic *nii, unsigned int op, void *db,
462 return nii_issue_cpb_db ( nii, op, NULL, 0, db, db_len );
471 * @ret stat Status flags, or negative status code
473 static int nii_issue ( struct nii_nic *nii, unsigned int op ) {
475 return nii_issue_cpb_db ( nii, op, NULL, 0, NULL, 0 );
482 * @ret rc Return status code
484 static int nii_start_undi ( struct nii_nic *nii ) {
485 PXE_CPB_START_31 cpb;
489 /* Construct parameter block */
490 memset ( &cpb, 0, sizeof ( cpb ) );
491 cpb.Delay = ( ( intptr_t ) nii_delay );
492 cpb.Block = ( ( intptr_t ) nii_block );
493 cpb.Mem_IO = ( ( intptr_t ) nii_io );
494 cpb.Unique_ID = ( ( intptr_t ) nii );
497 if ( ( stat = nii_issue_cpb ( nii, PXE_OPCODE_START, &cpb,
498 sizeof ( cpb ) ) ) < 0 ) {
499 rc = -EIO_STAT ( stat );
500 DBGC ( nii, "NII %s could not start: %s\n",
501 nii->dev.name, strerror ( rc ) );
513 static void nii_stop_undi ( struct nii_nic *nii ) {
518 if ( ( stat = nii_issue ( nii, PXE_OPCODE_STOP ) ) < 0 ) {
519 rc = -EIO_STAT ( stat );
520 DBGC ( nii, "NII %s could not stop: %s\n",
521 nii->dev.name, strerror ( rc ) );
522 /* Nothing we can do about it */
528 * Get initialisation information
531 * @v netdev Network device to fill in
532 * @ret rc Return status code
534 static int nii_get_init_info ( struct nii_nic *nii,
535 struct net_device *netdev ) {
536 PXE_DB_GET_INIT_INFO db;
541 if ( ( stat = nii_issue_db ( nii, PXE_OPCODE_GET_INIT_INFO, &db,
542 sizeof ( db ) ) ) < 0 ) {
543 rc = -EIO_STAT ( stat );
544 DBGC ( nii, "NII %s could not get initialisation info: %s\n",
545 nii->dev.name, strerror ( rc ) );
549 /* Determine link layer protocol */
550 switch ( db.IFtype ) {
551 case PXE_IFTYPE_ETHERNET :
552 netdev->ll_protocol = ðernet_protocol;
555 DBGC ( nii, "NII %s unknown interface type %#02x\n",
556 nii->dev.name, db.IFtype );
561 assert ( db.MediaHeaderLen == netdev->ll_protocol->ll_header_len );
562 assert ( db.HWaddrLen == netdev->ll_protocol->hw_addr_len );
563 assert ( db.HWaddrLen == netdev->ll_protocol->ll_addr_len );
565 /* Extract parameters */
566 nii->buffer_len = db.MemoryRequired;
567 nii->mtu = ( db.FrameDataLen + db.MediaHeaderLen );
568 netdev->max_pkt_len = nii->mtu;
569 nii->media = ( stat & PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED );
579 * @ret rc Return status code
581 static int nii_initialise_flags ( struct nii_nic *nii, unsigned int flags ) {
582 PXE_CPB_INITIALIZE cpb;
583 PXE_DB_INITIALIZE db;
588 /* Allocate memory buffer */
589 nii->buffer = umalloc ( nii->buffer_len );
590 if ( ! nii->buffer ) {
595 /* Construct parameter block */
596 memset ( &cpb, 0, sizeof ( cpb ) );
597 cpb.MemoryAddr = ( ( intptr_t ) nii->buffer );
598 cpb.MemoryLength = nii->buffer_len;
600 /* Construct data block */
601 memset ( &db, 0, sizeof ( db ) );
604 op = NII_OP ( PXE_OPCODE_INITIALIZE, flags );
605 if ( ( stat = nii_issue_cpb_db ( nii, op, &cpb, sizeof ( cpb ),
606 &db, sizeof ( db ) ) ) < 0 ) {
607 rc = -EIO_STAT ( stat );
608 DBGC ( nii, "NII %s could not initialise: %s\n",
609 nii->dev.name, strerror ( rc ) );
616 ufree ( nii->buffer );
625 * @ret rc Return status code
627 static int nii_initialise ( struct nii_nic *nii ) {
630 /* Initialise UNDI */
631 flags = PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE;
632 return nii_initialise_flags ( nii, flags );
636 * Initialise UNDI and detect cable
639 * @ret rc Return status code
641 static int nii_initialise_and_detect ( struct nii_nic *nii ) {
644 /* Initialise UNDI and detect cable. This is required to work
645 * around bugs in some Emulex NII drivers.
647 flags = PXE_OPFLAGS_INITIALIZE_DETECT_CABLE;
648 return nii_initialise_flags ( nii, flags );
656 static void nii_shutdown ( struct nii_nic *nii ) {
661 if ( ( stat = nii_issue ( nii, PXE_OPCODE_SHUTDOWN ) ) < 0 ) {
662 rc = -EIO_STAT ( stat );
663 DBGC ( nii, "NII %s could not shut down: %s\n",
664 nii->dev.name, strerror ( rc ) );
665 /* Leak memory to avoid corruption */
670 ufree ( nii->buffer );
674 * Get station addresses
677 * @v netdev Network device to fill in
678 * @ret rc Return status code
680 static int nii_get_station_address ( struct nii_nic *nii,
681 struct net_device *netdev ) {
682 PXE_DB_STATION_ADDRESS db;
687 /* Initialise UNDI */
688 if ( ( rc = nii_initialise ( nii ) ) != 0 )
692 op = NII_OP ( PXE_OPCODE_STATION_ADDRESS,
693 PXE_OPFLAGS_STATION_ADDRESS_READ );
694 if ( ( stat = nii_issue_db ( nii, op, &db, sizeof ( db ) ) ) < 0 ) {
695 rc = -EIO_STAT ( stat );
696 DBGC ( nii, "NII %s could not get station address: %s\n",
697 nii->dev.name, strerror ( rc ) );
698 goto err_station_address;
701 /* Copy MAC addresses */
702 memcpy ( netdev->ll_addr, db.StationAddr,
703 netdev->ll_protocol->ll_addr_len );
704 memcpy ( netdev->hw_addr, db.PermanentAddr,
705 netdev->ll_protocol->hw_addr_len );
706 memcpy ( nii->broadcast, db.BroadcastAddr,
707 sizeof ( nii->broadcast ) );
710 nii_shutdown ( nii );
716 * Set station address
719 * @v netdev Network device
720 * @ret rc Return status code
722 static int nii_set_station_address ( struct nii_nic *nii,
723 struct net_device *netdev ) {
724 uint32_t implementation = nii->undi->Implementation;
725 PXE_CPB_STATION_ADDRESS cpb;
730 /* Fail if setting station address is unsupported */
731 if ( ! ( implementation & PXE_ROMID_IMP_STATION_ADDR_SETTABLE ) )
734 /* Construct parameter block */
735 memset ( &cpb, 0, sizeof ( cpb ) );
736 memcpy ( cpb.StationAddr, netdev->ll_addr,
737 netdev->ll_protocol->ll_addr_len );
740 op = NII_OP ( PXE_OPCODE_STATION_ADDRESS,
741 PXE_OPFLAGS_STATION_ADDRESS_WRITE );
742 if ( ( stat = nii_issue_cpb ( nii, op, &cpb, sizeof ( cpb ) ) ) < 0 ) {
743 rc = -EIO_STAT ( stat );
744 DBGC ( nii, "NII %s could not set station address: %s\n",
745 nii->dev.name, strerror ( rc ) );
753 * Set receive filters
756 * @ret rc Return status code
758 static int nii_set_rx_filters ( struct nii_nic *nii ) {
759 uint32_t implementation = nii->undi->Implementation;
765 /* Construct receive filter set */
766 flags = ( PXE_OPFLAGS_RECEIVE_FILTER_ENABLE |
767 PXE_OPFLAGS_RECEIVE_FILTER_UNICAST );
768 if ( implementation & PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED )
769 flags |= PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST;
770 if ( implementation & PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED )
771 flags |= PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS;
772 if ( implementation & PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED )
773 flags |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;
776 op = NII_OP ( PXE_OPCODE_RECEIVE_FILTERS, flags );
777 if ( ( stat = nii_issue ( nii, op ) ) < 0 ) {
778 rc = -EIO_STAT ( stat );
779 DBGC ( nii, "NII %s could not set receive filters %#04x: %s\n",
780 nii->dev.name, flags, strerror ( rc ) );
790 * @v netdev Network device
791 * @v iobuf I/O buffer
792 * @ret rc Return status code
794 static int nii_transmit ( struct net_device *netdev,
795 struct io_buffer *iobuf ) {
796 struct nii_nic *nii = netdev->priv;
797 PXE_CPB_TRANSMIT cpb;
802 /* Defer the packet if there is already a transmission in progress */
804 netdev_tx_defer ( netdev, iobuf );
808 /* Construct parameter block */
809 memset ( &cpb, 0, sizeof ( cpb ) );
810 cpb.FrameAddr = virt_to_bus ( iobuf->data );
811 cpb.DataLen = iob_len ( iobuf );
812 cpb.MediaheaderLen = netdev->ll_protocol->ll_header_len;
814 /* Transmit packet */
815 op = NII_OP ( PXE_OPCODE_TRANSMIT,
816 ( PXE_OPFLAGS_TRANSMIT_WHOLE |
817 PXE_OPFLAGS_TRANSMIT_DONT_BLOCK ) );
818 if ( ( stat = nii_issue_cpb ( nii, op, &cpb, sizeof ( cpb ) ) ) < 0 ) {
819 rc = -EIO_STAT ( stat );
820 DBGC ( nii, "NII %s could not transmit: %s\n",
821 nii->dev.name, strerror ( rc ) );
830 * Poll for completed packets
832 * @v netdev Network device
833 * @v stat Status flags
835 static void nii_poll_tx ( struct net_device *netdev, unsigned int stat ) {
836 struct nii_nic *nii = netdev->priv;
837 struct io_buffer *iobuf;
839 /* Do nothing unless we have a completion */
840 if ( stat & PXE_STATFLAGS_GET_STATUS_NO_TXBUFS_WRITTEN )
844 assert ( nii->txbuf != NULL );
846 /* Complete transmission */
849 netdev_tx_complete ( netdev, iobuf );
853 * Poll for received packets
855 * @v netdev Network device
857 static void nii_poll_rx ( struct net_device *netdev ) {
858 struct nii_nic *nii = netdev->priv;
865 /* Retrieve up to NII_RX_QUOTA packets */
866 for ( quota = NII_RX_QUOTA ; quota ; quota-- ) {
868 /* Allocate buffer, if required */
869 if ( ! nii->rxbuf ) {
870 nii->rxbuf = alloc_iob ( nii->mtu );
871 if ( ! nii->rxbuf ) {
872 /* Leave for next poll */
877 /* Construct parameter block */
878 memset ( &cpb, 0, sizeof ( cpb ) );
879 cpb.BufferAddr = virt_to_bus ( nii->rxbuf->data );
880 cpb.BufferLen = iob_tailroom ( nii->rxbuf );
883 if ( ( stat = nii_issue_cpb_db ( nii, PXE_OPCODE_RECEIVE,
884 &cpb, sizeof ( cpb ),
885 &db, sizeof ( db ) ) ) < 0 ) {
887 /* PXE_STATCODE_NO_DATA is just the usual "no packet"
888 * status indicator; ignore it.
890 if ( stat == -PXE_STATCODE_NO_DATA )
893 /* Anything else is an error */
894 rc = -EIO_STAT ( stat );
895 DBGC ( nii, "NII %s could not receive: %s\n",
896 nii->dev.name, strerror ( rc ) );
897 netdev_rx_err ( netdev, NULL, rc );
901 /* Hand off to network stack */
902 iob_put ( nii->rxbuf, db.FrameLen );
903 netdev_rx ( netdev, nii->rxbuf );
909 * Check for link state changes
911 * @v netdev Network device
912 * @v stat Status flags
914 static void nii_poll_link ( struct net_device *netdev, unsigned int stat ) {
915 int no_media = ( stat & PXE_STATFLAGS_GET_STATUS_NO_MEDIA );
917 if ( no_media && netdev_link_ok ( netdev ) ) {
918 netdev_link_down ( netdev );
919 } else if ( ( ! no_media ) && ( ! netdev_link_ok ( netdev ) ) ) {
920 netdev_link_up ( netdev );
925 * Poll for completed packets
927 * @v netdev Network device
929 static void nii_poll ( struct net_device *netdev ) {
930 struct nii_nic *nii = netdev->priv;
931 PXE_DB_GET_STATUS db;
936 /* Construct data block */
937 memset ( &db, 0, sizeof ( db ) );
940 op = NII_OP ( PXE_OPCODE_GET_STATUS,
941 ( PXE_OPFLAGS_GET_INTERRUPT_STATUS |
942 ( nii->txbuf ? PXE_OPFLAGS_GET_TRANSMITTED_BUFFERS : 0)|
943 ( nii->media ? PXE_OPFLAGS_GET_MEDIA_STATUS : 0 ) ) );
944 if ( ( stat = nii_issue_db ( nii, op, &db, sizeof ( db ) ) ) < 0 ) {
945 rc = -EIO_STAT ( stat );
946 DBGC ( nii, "NII %s could not get status: %s\n",
947 nii->dev.name, strerror ( rc ) );
951 /* Process any TX completions */
953 nii_poll_tx ( netdev, stat );
955 /* Process any RX completions */
956 nii_poll_rx ( netdev );
958 /* Check for link state changes */
960 nii_poll_link ( netdev, stat );
964 * Open network device
966 * @v netdev Network device
967 * @ret rc Return status code
969 static int nii_open ( struct net_device *netdev ) {
970 struct nii_nic *nii = netdev->priv;
975 * Some Emulex NII drivers have a bug which prevents packets
976 * from being sent or received unless we specifically ask it
977 * to detect cable presence during initialisation. Work
978 * around these buggy drivers by requesting cable detection at
979 * this point, even though we don't care about link state here
980 * (and would prefer to have the NIC initialise even if no
981 * cable is present, to match the behaviour of all other iPXE
984 if ( ( rc = nii_initialise_and_detect ( nii ) ) != 0 )
987 /* Attempt to set station address */
988 if ( ( rc = nii_set_station_address ( nii, netdev ) ) != 0 ) {
989 DBGC ( nii, "NII %s could not set station address: %s\n",
990 nii->dev.name, strerror ( rc ) );
991 /* Treat as non-fatal */
994 /* Set receive filters */
995 if ( ( rc = nii_set_rx_filters ( nii ) ) != 0 )
996 goto err_set_rx_filters;
1001 nii_shutdown ( nii );
1007 * Close network device
1009 * @v netdev Network device
1011 static void nii_close ( struct net_device *netdev ) {
1012 struct nii_nic *nii = netdev->priv;
1015 nii_shutdown ( nii );
1017 /* Discard transmit buffer, if applicable */
1019 netdev_tx_complete_err ( netdev, nii->txbuf, -ECANCELED );
1023 /* Discard receive buffer, if applicable */
1025 free_iob ( nii->rxbuf );
1030 /** NII network device operations */
1031 static struct net_device_operations nii_operations = {
1034 .transmit = nii_transmit,
1039 * Attach driver to device
1041 * @v efidev EFI device
1042 * @ret rc Return status code
1044 int nii_start ( struct efi_device *efidev ) {
1045 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1046 EFI_HANDLE device = efidev->device;
1047 struct net_device *netdev;
1048 struct nii_nic *nii;
1053 /* Allocate and initialise structure */
1054 netdev = alloc_netdev ( sizeof ( *nii ) );
1059 netdev_init ( netdev, &nii_operations );
1061 nii->efidev = efidev;
1062 netdev->ll_broadcast = nii->broadcast;
1063 efidev_set_drvdata ( efidev, netdev );
1065 /* Populate underlying device information */
1066 efi_device_info ( device, "NII", &nii->dev );
1067 nii->dev.driver_name = "NII";
1068 nii->dev.parent = &efidev->dev;
1069 list_add ( &nii->dev.siblings, &efidev->dev.children );
1070 INIT_LIST_HEAD ( &nii->dev.children );
1071 netdev->dev = &nii->dev;
1073 /* Open NII protocol */
1074 if ( ( efirc = bs->OpenProtocol ( device, &efi_nii31_protocol_guid,
1075 &interface, efi_image_handle, device,
1076 ( EFI_OPEN_PROTOCOL_BY_DRIVER |
1077 EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
1078 rc = -EEFI ( efirc );
1079 DBGC ( nii, "NII %s cannot open NII protocol: %s\n",
1080 nii->dev.name, strerror ( rc ) );
1081 DBGC_EFI_OPENERS ( device, device, &efi_nii31_protocol_guid );
1082 goto err_open_protocol;
1084 nii->nii = interface;
1086 /* Locate UNDI and entry point */
1087 nii->undi = ( ( void * ) ( intptr_t ) nii->nii->Id );
1088 if ( ! nii->undi ) {
1089 DBGC ( nii, "NII %s has no UNDI\n", nii->dev.name );
1093 if ( nii->undi->Implementation & PXE_ROMID_IMP_HW_UNDI ) {
1094 DBGC ( nii, "NII %s is a mythical hardware UNDI\n",
1099 if ( nii->undi->Implementation & PXE_ROMID_IMP_SW_VIRT_ADDR ) {
1100 nii->issue = ( ( void * ) ( intptr_t ) nii->undi->EntryPoint );
1102 nii->issue = ( ( ( void * ) nii->undi ) +
1103 nii->undi->EntryPoint );
1105 DBGC ( nii, "NII %s using UNDI v%x.%x at %p entry %p impl %#08x\n",
1106 nii->dev.name, nii->nii->MajorVer, nii->nii->MinorVer,
1107 nii->undi, nii->issue, nii->undi->Implementation );
1109 /* Open PCI I/O protocols and locate BARs */
1110 if ( ( rc = nii_pci_open ( nii ) ) != 0 )
1114 if ( ( rc = nii_start_undi ( nii ) ) != 0 )
1115 goto err_start_undi;
1117 /* Get initialisation information */
1118 if ( ( rc = nii_get_init_info ( nii, netdev ) ) != 0 )
1119 goto err_get_init_info;
1121 /* Get MAC addresses */
1122 if ( ( rc = nii_get_station_address ( nii, netdev ) ) != 0 )
1123 goto err_get_station_address;
1125 /* Register network device */
1126 if ( ( rc = register_netdev ( netdev ) ) != 0 )
1127 goto err_register_netdev;
1128 DBGC ( nii, "NII %s registered as %s for %p %s\n", nii->dev.name,
1129 netdev->name, device, efi_handle_name ( device ) );
1131 /* Set initial link state (if media detection is not supported) */
1133 netdev_link_up ( netdev );
1137 unregister_netdev ( netdev );
1138 err_register_netdev:
1139 err_get_station_address:
1141 nii_stop_undi ( nii );
1143 nii_pci_close ( nii );
1147 bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
1148 efi_image_handle, device );
1150 list_del ( &nii->dev.siblings );
1151 netdev_nullify ( netdev );
1152 netdev_put ( netdev );
1158 * Detach driver from device
1160 * @v efidev EFI device
1162 void nii_stop ( struct efi_device *efidev ) {
1163 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1164 struct net_device *netdev = efidev_get_drvdata ( efidev );
1165 struct nii_nic *nii = netdev->priv;
1166 EFI_HANDLE device = efidev->device;
1168 /* Unregister network device */
1169 unregister_netdev ( netdev );
1172 nii_stop_undi ( nii );
1174 /* Close PCI I/O protocols */
1175 nii_pci_close ( nii );
1177 /* Close NII protocol */
1178 bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
1179 efi_image_handle, device );
1181 /* Free network device */
1182 list_del ( &nii->dev.siblings );
1183 netdev_nullify ( netdev );
1184 netdev_put ( netdev );