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