[efi] Poll for TX completions only when there is an outstanding TX buffer
[ipxe.git] / src / drivers / net / efi / nii.c
1 /*
2 * Copyright (C) 2014 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 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 <strings.h>
28 #include <unistd.h>
29 #include <errno.h>
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>
39 #include "nii.h"
40
41 /** @file
42 *
43 * NII driver
44 *
45 */
46
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, \
51 "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, \
55 "Invalid CPB" )
56 #define EIO_BUSY __einfo_error ( EINFO_EIO_BUSY )
57 #define EINFO_EIO_BUSY \
58 __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_BUSY, \
59 "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, \
63 "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, \
67 "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, \
71 "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, \
75 "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, \
83 "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, \
87 "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, \
95 "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, \
99 "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, \
107 "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, \
123 "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 )
132
133 /** Maximum PCI BAR
134 *
135 * This is defined in <ipxe/efi/IndustryStandard/Pci22.h>, but we
136 * can't #include that since it collides with <ipxe/pci.h>.
137 */
138 #define PCI_MAX_BAR 6
139
140 /** An NII NIC */
141 struct nii_nic {
142 /** EFI device */
143 struct efi_device *efidev;
144 /** Network interface identifier protocol */
145 EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *nii;
146 /** !PXE structure */
147 PXE_SW_UNDI *undi;
148 /** Entry point */
149 EFIAPI VOID ( * issue ) ( UINT64 cdb );
150 /** Generic device */
151 struct device dev;
152
153 /** PCI device */
154 EFI_HANDLE pci_device;
155 /** PCI I/O protocol */
156 EFI_PCI_IO_PROTOCOL *pci_io;
157 /** Memory BAR */
158 unsigned int mem_bar;
159 /** I/O BAR */
160 unsigned int io_bar;
161
162 /** Broadcast address */
163 PXE_MAC_ADDR broadcast;
164 /** Maximum packet length */
165 size_t mtu;
166
167 /** Hardware transmit/receive buffer */
168 userptr_t buffer;
169 /** Hardware transmit/receive buffer length */
170 size_t buffer_len;
171
172 /** Saved task priority level */
173 EFI_TPL saved_tpl;
174
175 /** Media status is supported */
176 int media;
177
178 /** Current transmit buffer */
179 struct io_buffer *txbuf;
180 /** Current receive buffer */
181 struct io_buffer *rxbuf;
182 };
183
184 /** Maximum number of received packets per poll */
185 #define NII_RX_QUOTA 4
186
187 /**
188 * Open PCI I/O protocol and identify BARs
189 *
190 * @v nii NII NIC
191 * @ret rc Return status code
192 */
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;
197 union {
198 EFI_PCI_IO_PROTOCOL *pci_io;
199 void *interface;
200 } pci_io;
201 union {
202 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *acpi;
203 void *resource;
204 } desc;
205 unsigned int bar;
206 EFI_STATUS efirc;
207 int rc;
208
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 ) );
214 goto err_locate;
215 }
216 nii->pci_device = pci_device;
217
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,
221 device,
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 ) );
226 goto err_open;
227 }
228 nii->pci_io = pci_io.pci_io;
229
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,
235 &desc.resource );
236 if ( efirc == EFI_UNSUPPORTED ) {
237 /* BAR not present; ignore */
238 continue;
239 }
240 if ( efirc != 0 ) {
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;
245 }
246 if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM ) {
247 nii->mem_bar = bar;
248 } else if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_IO ) {
249 nii->io_bar = bar;
250 }
251 bs->FreePool ( desc.resource );
252 }
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 );
256 } else {
257 DBGC ( nii, "no memory BAR and " );
258 }
259 if ( nii->io_bar < PCI_MAX_BAR ) {
260 DBGC ( nii, "I/O BAR %d\n", nii->io_bar );
261 } else {
262 DBGC ( nii, "no I/O BAR\n" );
263 }
264
265 return 0;
266
267 err_get_bar_attributes:
268 bs->CloseProtocol ( pci_device, &efi_pci_io_protocol_guid,
269 efi_image_handle, device );
270 err_open:
271 err_locate:
272 return rc;
273 }
274
275 /**
276 * Close PCI I/O protocol
277 *
278 * @v nii NII NIC
279 * @ret rc Return status code
280 */
281 static void nii_pci_close ( struct nii_nic *nii ) {
282 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
283
284 bs->CloseProtocol ( nii->pci_device, &efi_pci_io_protocol_guid,
285 efi_image_handle, nii->efidev->device );
286 }
287
288 /**
289 * I/O callback
290 *
291 * @v unique_id NII NIC
292 * @v op Operations
293 * @v len Length of data
294 * @v addr Address
295 * @v data Data buffer
296 */
297 static EFIAPI VOID nii_io ( UINT64 unique_id, UINT8 op, UINT8 len, UINT64 addr,
298 UINT64 data ) {
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;
303 unsigned int bar;
304 EFI_STATUS efirc;
305 int rc;
306
307 /* Determine accessor and BAR */
308 if ( op & ( PXE_MEM_READ | PXE_MEM_WRITE ) ) {
309 access = &nii->pci_io->Mem;
310 bar = nii->mem_bar;
311 } else {
312 access = &nii->pci_io->Io;
313 bar = nii->io_bar;
314 }
315
316 /* Determine operaton */
317 io = ( ( op & ( PXE_IO_WRITE | PXE_MEM_WRITE ) ) ?
318 access->Write : access->Read );
319
320 /* Determine width */
321 width = ( fls ( len ) - 1 );
322
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 */
330 return;
331 }
332 }
333
334 /**
335 * Delay callback
336 *
337 * @v unique_id NII NIC
338 * @v microseconds Delay in microseconds
339 */
340 static EFIAPI VOID nii_delay ( UINT64 unique_id __unused, UINTN microseconds ) {
341
342 udelay ( microseconds );
343 }
344
345 /**
346 * Block callback
347 *
348 * @v unique_id NII NIC
349 * @v acquire Acquire lock
350 */
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;
354
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
358 * behaviour.
359 */
360 if ( acquire ) {
361 nii->saved_tpl = bs->RaiseTPL ( TPL_NOTIFY );
362 } else {
363 bs->RestoreTPL ( nii->saved_tpl );
364 }
365 }
366
367 /**
368 * Construct operation from opcode and flags
369 *
370 * @v opcode Opcode
371 * @v opflags Flags
372 * @ret op Operation
373 */
374 #define NII_OP( opcode, opflags ) ( (opcode) | ( (opflags) << 16 ) )
375
376 /**
377 * Extract opcode from operation
378 *
379 * @v op Operation
380 * @ret opcode Opcode
381 */
382 #define NII_OPCODE( op ) ( (op) & 0xffff )
383
384 /**
385 * Extract flags from operation
386 *
387 * @v op Operation
388 * @ret opflags Flags
389 */
390 #define NII_OPFLAGS( op ) ( (op) >> 16 )
391
392 /**
393 * Issue command with parameter block and data block
394 *
395 * @v nii NII NIC
396 * @v op Operation
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
402 */
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 ) {
405 PXE_CDB cdb;
406
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 );
414 cdb.DBsize = db_len;
415 cdb.IFnum = nii->nii->IfNum;
416
417 /* Issue command */
418 nii->issue ( ( intptr_t ) &cdb );
419
420 /* Check completion status */
421 if ( cdb.StatCode != PXE_STATCODE_SUCCESS )
422 return -cdb.StatCode;
423
424 /* Return command-specific status flags */
425 return ( cdb.StatFlags & ~PXE_STATFLAGS_STATUS_MASK );
426 }
427
428 /**
429 * Issue command with parameter block
430 *
431 * @v nii NII NIC
432 * @v op Operation
433 * @v cpb Command parameter block, or NULL
434 * @v cpb_len Command parameter block length
435 * @ret stat Status flags, or negative status code
436 */
437 static int nii_issue_cpb ( struct nii_nic *nii, unsigned int op, void *cpb,
438 size_t cpb_len ) {
439
440 return nii_issue_cpb_db ( nii, op, cpb, cpb_len, NULL, 0 );
441 }
442
443 /**
444 * Issue command with data block
445 *
446 * @v nii NII NIC
447 * @v op Operation
448 * @v db Data block, or NULL
449 * @v db_len Data block length
450 * @ret stat Status flags, or negative status code
451 */
452 static int nii_issue_db ( struct nii_nic *nii, unsigned int op, void *db,
453 size_t db_len ) {
454
455 return nii_issue_cpb_db ( nii, op, NULL, 0, db, db_len );
456 }
457
458 /**
459 * Issue command
460 *
461 *
462 * @v nii NII NIC
463 * @v op Operation
464 * @ret stat Status flags, or negative status code
465 */
466 static int nii_issue ( struct nii_nic *nii, unsigned int op ) {
467
468 return nii_issue_cpb_db ( nii, op, NULL, 0, NULL, 0 );
469 }
470
471 /**
472 * Start UNDI
473 *
474 * @v nii NII NIC
475 * @ret rc Return status code
476 */
477 static int nii_start_undi ( struct nii_nic *nii ) {
478 PXE_CPB_START_31 cpb;
479 int stat;
480 int rc;
481
482 /* Construct parameter block */
483 memset ( &cpb, 0, sizeof ( cpb ) );
484 cpb.Delay = ( ( intptr_t ) nii_delay );
485 cpb.Block = ( ( intptr_t ) nii_block );
486 cpb.Mem_IO = ( ( intptr_t ) nii_io );
487 cpb.Unique_ID = ( ( intptr_t ) nii );
488
489 /* Issue command */
490 if ( ( stat = nii_issue_cpb ( nii, PXE_OPCODE_START, &cpb,
491 sizeof ( cpb ) ) ) < 0 ) {
492 rc = -EIO_STAT ( stat );
493 DBGC ( nii, "NII %s could not start: %s\n",
494 nii->dev.name, strerror ( rc ) );
495 return rc;
496 }
497
498 return 0;
499 }
500
501 /**
502 * Stop UNDI
503 *
504 * @v nii NII NIC
505 */
506 static void nii_stop_undi ( struct nii_nic *nii ) {
507 int stat;
508 int rc;
509
510 /* Issue command */
511 if ( ( stat = nii_issue ( nii, PXE_OPCODE_STOP ) ) < 0 ) {
512 rc = -EIO_STAT ( stat );
513 DBGC ( nii, "NII %s could not stop: %s\n",
514 nii->dev.name, strerror ( rc ) );
515 /* Nothing we can do about it */
516 return;
517 }
518 }
519
520 /**
521 * Get initialisation information
522 *
523 * @v nii NII NIC
524 * @v netdev Network device to fill in
525 * @ret rc Return status code
526 */
527 static int nii_get_init_info ( struct nii_nic *nii,
528 struct net_device *netdev ) {
529 PXE_DB_GET_INIT_INFO db;
530 int stat;
531 int rc;
532
533 /* Issue command */
534 if ( ( stat = nii_issue_db ( nii, PXE_OPCODE_GET_INIT_INFO, &db,
535 sizeof ( db ) ) ) < 0 ) {
536 rc = -EIO_STAT ( stat );
537 DBGC ( nii, "NII %s could not get initialisation info: %s\n",
538 nii->dev.name, strerror ( rc ) );
539 return rc;
540 }
541
542 /* Determine link layer protocol */
543 switch ( db.IFtype ) {
544 case PXE_IFTYPE_ETHERNET :
545 netdev->ll_protocol = &ethernet_protocol;
546 break;
547 default:
548 DBGC ( nii, "NII %s unknown interface type %#02x\n",
549 nii->dev.name, db.IFtype );
550 return -ENOTSUP;
551 }
552
553 /* Sanity checks */
554 assert ( db.MediaHeaderLen == netdev->ll_protocol->ll_header_len );
555 assert ( db.HWaddrLen == netdev->ll_protocol->hw_addr_len );
556 assert ( db.HWaddrLen == netdev->ll_protocol->ll_addr_len );
557
558 /* Extract parameters */
559 nii->buffer_len = db.MemoryRequired;
560 nii->mtu = ( db.FrameDataLen + db.MediaHeaderLen );
561 netdev->max_pkt_len = nii->mtu;
562 nii->media = ( stat & PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED );
563
564 return 0;
565 }
566
567 /**
568 * Initialise UNDI
569 *
570 * @v nii NII NIC
571 * @ret rc Return status code
572 */
573 static int nii_initialise ( struct nii_nic *nii ) {
574 PXE_CPB_INITIALIZE cpb;
575 PXE_DB_INITIALIZE db;
576 unsigned int op;
577 int stat;
578 int rc;
579
580 /* Allocate memory buffer */
581 nii->buffer = umalloc ( nii->buffer_len );
582 if ( ! nii->buffer ) {
583 rc = -ENOMEM;
584 goto err_alloc;
585 }
586
587 /* Construct parameter block */
588 memset ( &cpb, 0, sizeof ( cpb ) );
589 cpb.MemoryAddr = ( ( intptr_t ) nii->buffer );
590 cpb.MemoryLength = nii->buffer_len;
591
592 /* Construct data block */
593 memset ( &db, 0, sizeof ( db ) );
594
595 /* Issue command */
596 op = NII_OP ( PXE_OPCODE_INITIALIZE,
597 PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE );
598 if ( ( stat = nii_issue_cpb_db ( nii, op, &cpb, sizeof ( cpb ),
599 &db, sizeof ( db ) ) ) < 0 ) {
600 rc = -EIO_STAT ( stat );
601 DBGC ( nii, "NII %s could not initialise: %s\n",
602 nii->dev.name, strerror ( rc ) );
603 goto err_initialize;
604 }
605
606 return 0;
607
608 err_initialize:
609 ufree ( nii->buffer );
610 err_alloc:
611 return rc;
612 }
613
614 /**
615 * Shut down UNDI
616 *
617 * @v nii NII NIC
618 */
619 static void nii_shutdown ( struct nii_nic *nii ) {
620 int stat;
621 int rc;
622
623 /* Issue command */
624 if ( ( stat = nii_issue ( nii, PXE_OPCODE_SHUTDOWN ) ) < 0 ) {
625 rc = -EIO_STAT ( stat );
626 DBGC ( nii, "NII %s could not shut down: %s\n",
627 nii->dev.name, strerror ( rc ) );
628 /* Leak memory to avoid corruption */
629 return;
630 }
631
632 /* Free buffer */
633 ufree ( nii->buffer );
634 }
635
636 /**
637 * Get station addresses
638 *
639 * @v nii NII NIC
640 * @v netdev Network device to fill in
641 * @ret rc Return status code
642 */
643 static int nii_get_station_address ( struct nii_nic *nii,
644 struct net_device *netdev ) {
645 PXE_DB_STATION_ADDRESS db;
646 int stat;
647 int rc;
648
649 /* Initialise UNDI */
650 if ( ( rc = nii_initialise ( nii ) ) != 0 )
651 goto err_initialise;
652
653 /* Issue command */
654 if ( ( stat = nii_issue_db ( nii, PXE_OPCODE_STATION_ADDRESS, &db,
655 sizeof ( db ) ) ) < 0 ) {
656 rc = -EIO_STAT ( stat );
657 DBGC ( nii, "NII %s could not get station address: %s\n",
658 nii->dev.name, strerror ( rc ) );
659 goto err_station_address;
660 }
661
662 /* Copy MAC addresses */
663 memcpy ( netdev->ll_addr, db.StationAddr,
664 netdev->ll_protocol->ll_addr_len );
665 memcpy ( netdev->hw_addr, db.PermanentAddr,
666 netdev->ll_protocol->hw_addr_len );
667 memcpy ( nii->broadcast, db.BroadcastAddr,
668 sizeof ( nii->broadcast ) );
669
670 err_station_address:
671 nii_shutdown ( nii );
672 err_initialise:
673 return rc;
674 }
675
676 /**
677 * Set station address
678 *
679 * @v nii NII NIC
680 * @v netdev Network device
681 * @ret rc Return status code
682 */
683 static int nii_set_station_address ( struct nii_nic *nii,
684 struct net_device *netdev ) {
685 PXE_CPB_STATION_ADDRESS cpb;
686 int stat;
687 int rc;
688
689 /* Construct parameter block */
690 memset ( &cpb, 0, sizeof ( cpb ) );
691 memcpy ( cpb.StationAddr, netdev->ll_addr,
692 netdev->ll_protocol->ll_addr_len );
693
694 /* Issue command */
695 if ( ( stat = nii_issue_cpb ( nii, PXE_OPCODE_STATION_ADDRESS,
696 &cpb, sizeof ( cpb ) ) ) < 0 ) {
697 rc = -EIO_STAT ( stat );
698 DBGC ( nii, "NII %s could not set station address: %s\n",
699 nii->dev.name, strerror ( rc ) );
700 return rc;
701 }
702
703 return 0;
704 }
705
706 /**
707 * Set receive filters
708 *
709 * @v nii NII NIC
710 * @ret rc Return status code
711 */
712 static int nii_set_rx_filters ( struct nii_nic *nii ) {
713 unsigned int op;
714 int stat;
715 int rc;
716
717 /* Issue command */
718 op = NII_OP ( PXE_OPCODE_RECEIVE_FILTERS,
719 ( PXE_OPFLAGS_RECEIVE_FILTER_ENABLE |
720 PXE_OPFLAGS_RECEIVE_FILTER_UNICAST |
721 PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST |
722 PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS |
723 PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST ) );
724 if ( ( stat = nii_issue ( nii, op ) ) < 0 ) {
725 rc = -EIO_STAT ( stat );
726 DBGC ( nii, "NII %s could not set receive filters: %s\n",
727 nii->dev.name, strerror ( rc ) );
728 return rc;
729 }
730
731 return 0;
732 }
733
734 /**
735 * Transmit packet
736 *
737 * @v netdev Network device
738 * @v iobuf I/O buffer
739 * @ret rc Return status code
740 */
741 static int nii_transmit ( struct net_device *netdev,
742 struct io_buffer *iobuf ) {
743 struct nii_nic *nii = netdev->priv;
744 PXE_CPB_TRANSMIT cpb;
745 int stat;
746 int rc;
747
748 /* Defer the packet if there is already a transmission in progress */
749 if ( nii->txbuf ) {
750 netdev_tx_defer ( netdev, iobuf );
751 return 0;
752 }
753
754 /* Construct parameter block */
755 memset ( &cpb, 0, sizeof ( cpb ) );
756 cpb.FrameAddr = virt_to_bus ( iobuf->data );
757 cpb.DataLen = iob_len ( iobuf );
758 cpb.MediaheaderLen = netdev->ll_protocol->ll_header_len;
759
760 /* Transmit packet */
761 if ( ( stat = nii_issue_cpb ( nii, PXE_OPCODE_TRANSMIT, &cpb,
762 sizeof ( cpb ) ) ) < 0 ) {
763 rc = -EIO_STAT ( stat );
764 DBGC ( nii, "NII %s could not transmit: %s\n",
765 nii->dev.name, strerror ( rc ) );
766 return rc;
767 }
768 nii->txbuf = iobuf;
769
770 return 0;
771 }
772
773 /**
774 * Poll for completed packets
775 *
776 * @v netdev Network device
777 * @v stat Status flags
778 */
779 static void nii_poll_tx ( struct net_device *netdev, unsigned int stat ) {
780 struct nii_nic *nii = netdev->priv;
781 struct io_buffer *iobuf;
782
783 /* Do nothing unless we have a completion */
784 if ( stat & PXE_STATFLAGS_GET_STATUS_NO_TXBUFS_WRITTEN )
785 return;
786
787 /* Sanity check */
788 assert ( nii->txbuf != NULL );
789
790 /* Complete transmission */
791 iobuf = nii->txbuf;
792 nii->txbuf = NULL;
793 netdev_tx_complete ( netdev, iobuf );
794 }
795
796 /**
797 * Poll for received packets
798 *
799 * @v netdev Network device
800 */
801 static void nii_poll_rx ( struct net_device *netdev ) {
802 struct nii_nic *nii = netdev->priv;
803 PXE_CPB_RECEIVE cpb;
804 PXE_DB_RECEIVE db;
805 unsigned int quota;
806 int stat;
807 int rc;
808
809 /* Retrieve up to NII_RX_QUOTA packets */
810 for ( quota = NII_RX_QUOTA ; quota ; quota-- ) {
811
812 /* Allocate buffer, if required */
813 if ( ! nii->rxbuf ) {
814 nii->rxbuf = alloc_iob ( nii->mtu );
815 if ( ! nii->rxbuf ) {
816 /* Leave for next poll */
817 break;
818 }
819 }
820
821 /* Construct parameter block */
822 memset ( &cpb, 0, sizeof ( cpb ) );
823 cpb.BufferAddr = virt_to_bus ( nii->rxbuf->data );
824 cpb.BufferLen = iob_tailroom ( nii->rxbuf );
825
826 /* Issue command */
827 if ( ( stat = nii_issue_cpb_db ( nii, PXE_OPCODE_RECEIVE,
828 &cpb, sizeof ( cpb ),
829 &db, sizeof ( db ) ) ) < 0 ) {
830
831 /* PXE_STATCODE_NO_DATA is just the usual "no packet"
832 * status indicator; ignore it.
833 */
834 if ( stat == -PXE_STATCODE_NO_DATA )
835 break;
836
837 /* Anything else is an error */
838 rc = -EIO_STAT ( stat );
839 DBGC ( nii, "NII %s could not receive: %s\n",
840 nii->dev.name, strerror ( rc ) );
841 netdev_rx_err ( netdev, NULL, rc );
842 break;
843 }
844
845 /* Hand off to network stack */
846 iob_put ( nii->rxbuf, db.FrameLen );
847 netdev_rx ( netdev, nii->rxbuf );
848 nii->rxbuf = NULL;
849 }
850 }
851
852 /**
853 * Check for link state changes
854 *
855 * @v netdev Network device
856 * @v stat Status flags
857 */
858 static void nii_poll_link ( struct net_device *netdev, unsigned int stat ) {
859 int no_media = ( stat & PXE_STATFLAGS_GET_STATUS_NO_MEDIA );
860
861 if ( no_media && netdev_link_ok ( netdev ) ) {
862 netdev_link_down ( netdev );
863 } else if ( ( ! no_media ) && ( ! netdev_link_ok ( netdev ) ) ) {
864 netdev_link_up ( netdev );
865 }
866 }
867
868 /**
869 * Poll for completed packets
870 *
871 * @v netdev Network device
872 */
873 static void nii_poll ( struct net_device *netdev ) {
874 struct nii_nic *nii = netdev->priv;
875 PXE_DB_GET_STATUS db;
876 unsigned int op;
877 int stat;
878 int rc;
879
880 /* Construct data block */
881 memset ( &db, 0, sizeof ( db ) );
882
883 /* Get status */
884 op = NII_OP ( PXE_OPCODE_GET_STATUS,
885 ( PXE_OPFLAGS_GET_INTERRUPT_STATUS |
886 ( nii->txbuf ? PXE_OPFLAGS_GET_TRANSMITTED_BUFFERS : 0)|
887 ( nii->media ? PXE_OPFLAGS_GET_MEDIA_STATUS : 0 ) ) );
888 if ( ( stat = nii_issue_db ( nii, op, &db, sizeof ( db ) ) ) < 0 ) {
889 rc = -EIO_STAT ( stat );
890 DBGC ( nii, "NII %s could not get status: %s\n",
891 nii->dev.name, strerror ( rc ) );
892 return;
893 }
894
895 /* Process any TX completions */
896 if ( nii->txbuf )
897 nii_poll_tx ( netdev, stat );
898
899 /* Process any RX completions */
900 nii_poll_rx ( netdev );
901
902 /* Check for link state changes */
903 if ( nii->media )
904 nii_poll_link ( netdev, stat );
905 }
906
907 /**
908 * Open network device
909 *
910 * @v netdev Network device
911 * @ret rc Return status code
912 */
913 static int nii_open ( struct net_device *netdev ) {
914 struct nii_nic *nii = netdev->priv;
915 int rc;
916
917 /* Initialise NIC */
918 if ( ( rc = nii_initialise ( nii ) ) != 0 )
919 goto err_initialise;
920
921 /* Attempt to set station address */
922 if ( ( rc = nii_set_station_address ( nii, netdev ) ) != 0 ) {
923 DBGC ( nii, "NII %s could not set station address: %s\n",
924 nii->dev.name, strerror ( rc ) );
925 /* Treat as non-fatal */
926 }
927
928 /* Set receive filters */
929 if ( ( rc = nii_set_rx_filters ( nii ) ) != 0 )
930 goto err_set_rx_filters;
931
932 return 0;
933
934 err_set_rx_filters:
935 nii_shutdown ( nii );
936 err_initialise:
937 return rc;
938 }
939
940 /**
941 * Close network device
942 *
943 * @v netdev Network device
944 */
945 static void nii_close ( struct net_device *netdev ) {
946 struct nii_nic *nii = netdev->priv;
947
948 /* Shut down NIC */
949 nii_shutdown ( nii );
950
951 /* Discard transmit buffer, if applicable */
952 if ( nii->txbuf ) {
953 netdev_tx_complete_err ( netdev, nii->txbuf, -ECANCELED );
954 nii->txbuf = NULL;
955 }
956
957 /* Discard receive buffer, if applicable */
958 if ( nii->rxbuf ) {
959 free_iob ( nii->rxbuf );
960 nii->rxbuf = NULL;
961 }
962 }
963
964 /** NII network device operations */
965 static struct net_device_operations nii_operations = {
966 .open = nii_open,
967 .close = nii_close,
968 .transmit = nii_transmit,
969 .poll = nii_poll,
970 };
971
972 /**
973 * Attach driver to device
974 *
975 * @v efidev EFI device
976 * @ret rc Return status code
977 */
978 int nii_start ( struct efi_device *efidev ) {
979 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
980 EFI_HANDLE device = efidev->device;
981 struct net_device *netdev;
982 struct nii_nic *nii;
983 void *interface;
984 EFI_STATUS efirc;
985 int rc;
986
987 /* Allocate and initialise structure */
988 netdev = alloc_netdev ( sizeof ( *nii ) );
989 if ( ! netdev ) {
990 rc = -ENOMEM;
991 goto err_alloc;
992 }
993 netdev_init ( netdev, &nii_operations );
994 nii = netdev->priv;
995 nii->efidev = efidev;
996 netdev->ll_broadcast = nii->broadcast;
997 efidev_set_drvdata ( efidev, netdev );
998
999 /* Populate underlying device information */
1000 efi_device_info ( device, "NII", &nii->dev );
1001 nii->dev.driver_name = "NII";
1002 nii->dev.parent = &efidev->dev;
1003 list_add ( &nii->dev.siblings, &efidev->dev.children );
1004 INIT_LIST_HEAD ( &nii->dev.children );
1005 netdev->dev = &nii->dev;
1006
1007 /* Open NII protocol */
1008 if ( ( efirc = bs->OpenProtocol ( device, &efi_nii31_protocol_guid,
1009 &interface, efi_image_handle, device,
1010 ( EFI_OPEN_PROTOCOL_BY_DRIVER |
1011 EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
1012 rc = -EEFI ( efirc );
1013 DBGC ( nii, "NII %s cannot open NII protocol: %s\n",
1014 nii->dev.name, strerror ( rc ) );
1015 DBGC_EFI_OPENERS ( device, device, &efi_nii31_protocol_guid );
1016 goto err_open_protocol;
1017 }
1018 nii->nii = interface;
1019
1020 /* Locate UNDI and entry point */
1021 nii->undi = ( ( void * ) ( intptr_t ) nii->nii->Id );
1022 if ( ! nii->undi ) {
1023 DBGC ( nii, "NII %s has no UNDI\n", nii->dev.name );
1024 rc = -ENODEV;
1025 goto err_no_undi;
1026 }
1027 if ( nii->undi->Implementation & PXE_ROMID_IMP_HW_UNDI ) {
1028 DBGC ( nii, "NII %s is a mythical hardware UNDI\n",
1029 nii->dev.name );
1030 rc = -ENOTSUP;
1031 goto err_hw_undi;
1032 }
1033 if ( nii->undi->Implementation & PXE_ROMID_IMP_SW_VIRT_ADDR ) {
1034 nii->issue = ( ( void * ) ( intptr_t ) nii->undi->EntryPoint );
1035 } else {
1036 nii->issue = ( ( ( void * ) nii->undi ) +
1037 nii->undi->EntryPoint );
1038 }
1039 DBGC ( nii, "NII %s using UNDI v%x.%x at %p entry %p\n", nii->dev.name,
1040 nii->nii->MajorVer, nii->nii->MinorVer, nii->undi, nii->issue );
1041
1042 /* Open PCI I/O protocols and locate BARs */
1043 if ( ( rc = nii_pci_open ( nii ) ) != 0 )
1044 goto err_pci_open;
1045
1046 /* Start UNDI */
1047 if ( ( rc = nii_start_undi ( nii ) ) != 0 )
1048 goto err_start_undi;
1049
1050 /* Get initialisation information */
1051 if ( ( rc = nii_get_init_info ( nii, netdev ) ) != 0 )
1052 goto err_get_init_info;
1053
1054 /* Get MAC addresses */
1055 if ( ( rc = nii_get_station_address ( nii, netdev ) ) != 0 )
1056 goto err_get_station_address;
1057
1058 /* Register network device */
1059 if ( ( rc = register_netdev ( netdev ) ) != 0 )
1060 goto err_register_netdev;
1061 DBGC ( nii, "NII %s registered as %s for %p %s\n", nii->dev.name,
1062 netdev->name, device, efi_handle_name ( device ) );
1063
1064 /* Set initial link state (if media detection is not supported) */
1065 if ( ! nii->media )
1066 netdev_link_up ( netdev );
1067
1068 return 0;
1069
1070 unregister_netdev ( netdev );
1071 err_register_netdev:
1072 err_get_station_address:
1073 err_get_init_info:
1074 nii_stop_undi ( nii );
1075 err_start_undi:
1076 nii_pci_close ( nii );
1077 err_pci_open:
1078 err_hw_undi:
1079 err_no_undi:
1080 bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
1081 efi_image_handle, device );
1082 err_open_protocol:
1083 list_del ( &nii->dev.siblings );
1084 netdev_nullify ( netdev );
1085 netdev_put ( netdev );
1086 err_alloc:
1087 return rc;
1088 }
1089
1090 /**
1091 * Detach driver from device
1092 *
1093 * @v efidev EFI device
1094 */
1095 void nii_stop ( struct efi_device *efidev ) {
1096 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1097 struct net_device *netdev = efidev_get_drvdata ( efidev );
1098 struct nii_nic *nii = netdev->priv;
1099 EFI_HANDLE device = efidev->device;
1100
1101 /* Unregister network device */
1102 unregister_netdev ( netdev );
1103
1104 /* Stop UNDI */
1105 nii_stop_undi ( nii );
1106
1107 /* Close PCI I/O protocols */
1108 nii_pci_close ( nii );
1109
1110 /* Close NII protocol */
1111 bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
1112 efi_image_handle, device );
1113
1114 /* Free network device */
1115 list_del ( &nii->dev.siblings );
1116 netdev_nullify ( netdev );
1117 netdev_put ( netdev );
1118 }