2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3 * Copyright (C) 2008 NetXen, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <gpxe/malloc.h>
29 #include <gpxe/iobuf.h>
30 #include <gpxe/netdevice.h>
31 #include <gpxe/if_ether.h>
32 #include <gpxe/ethernet.h>
43 /** Maximum time to wait for SPI lock */
44 #define PHN_SPI_LOCK_TIMEOUT_MS 100
46 /** Maximum time to wait for SPI command to be issued */
47 #define PHN_SPI_CMD_TIMEOUT_MS 100
49 /** Maximum time to wait for command PEG to initialise
53 * The command PEG will currently report initialisation complete only
54 * when at least one PHY has detected a link (so that the global PHY
55 * clock can be set to 10G/1G as appropriate). This can take a very,
58 * A future firmware revision should decouple PHY initialisation from
59 * firmware initialisation, at which point the command PEG will report
60 * initialisation complete much earlier, and this timeout can be
63 #define PHN_CMDPEG_INIT_TIMEOUT_SEC 50
65 /** Maximum time to wait for receive PEG to initialise */
66 #define PHN_RCVPEG_INIT_TIMEOUT_SEC 2
68 /** Maximum time to wait for firmware to accept a command */
69 #define PHN_ISSUE_CMD_TIMEOUT_MS 2000
71 /** Maximum time to wait for test memory */
72 #define PHN_TEST_MEM_TIMEOUT_MS 100
74 /** Link state poll frequency
76 * The link state will be checked once in every N calls to poll().
78 #define PHN_LINK_POLL_FREQUENCY 4096
80 /** Number of RX descriptors */
81 #define PHN_NUM_RDS 32
83 /** RX maximum fill level. Must be strictly less than PHN_NUM_RDS. */
84 #define PHN_RDS_MAX_FILL 16
87 #define PHN_RX_BUFSIZE ( 32 /* max LL padding added by card */ + \
90 /** Number of RX status descriptors */
91 #define PHN_NUM_SDS 32
93 /** Number of TX descriptors */
96 /** A Phantom descriptor ring set */
97 struct phantom_descriptor_rings {
99 struct phantom_rds rds[PHN_NUM_RDS];
100 /** RX status descriptors */
101 struct phantom_sds sds[PHN_NUM_SDS];
102 /** TX descriptors */
103 union phantom_cds cds[PHN_NUM_CDS];
104 /** TX consumer index */
105 volatile uint32_t cmd_cons;
108 /** A Phantom NIC port */
109 struct phantom_nic_port {
110 /** Phantom NIC containing this port */
111 struct phantom_nic *phantom;
117 uint16_t rx_context_id;
118 /** RX descriptor producer CRB offset */
119 unsigned long rds_producer_crb;
120 /** RX status descriptor consumer CRB offset */
121 unsigned long sds_consumer_crb;
123 /** RX producer index */
124 unsigned int rds_producer_idx;
125 /** RX consumer index */
126 unsigned int rds_consumer_idx;
127 /** RX status consumer index */
128 unsigned int sds_consumer_idx;
129 /** RX I/O buffers */
130 struct io_buffer *rds_iobuf[PHN_RDS_MAX_FILL];
134 uint16_t tx_context_id;
135 /** TX descriptor producer CRB offset */
136 unsigned long cds_producer_crb;
138 /** TX producer index */
139 unsigned int cds_producer_idx;
140 /** TX consumer index */
141 unsigned int cds_consumer_idx;
142 /** TX I/O buffers */
143 struct io_buffer *cds_iobuf[PHN_NUM_CDS];
146 /** Link state poll timer */
147 unsigned long link_poll_timer;
150 /** Descriptor rings */
151 struct phantom_descriptor_rings *desc;
154 /** RX context creation request and response buffers */
155 struct phantom_create_rx_ctx_rqrsp {
157 struct nx_hostrq_rx_ctx_s rx_ctx;
158 struct nx_hostrq_rds_ring_s rds;
159 struct nx_hostrq_sds_ring_s sds;
160 } __unm_dma_aligned hostrq;
162 struct nx_cardrsp_rx_ctx_s rx_ctx;
163 struct nx_cardrsp_rds_ring_s rds;
164 struct nx_cardrsp_sds_ring_s sds;
165 } __unm_dma_aligned cardrsp;
168 /** TX context creation request and response buffers */
169 struct phantom_create_tx_ctx_rqrsp {
171 struct nx_hostrq_tx_ctx_s tx_ctx;
172 } __unm_dma_aligned hostrq;
174 struct nx_cardrsp_tx_ctx_s tx_ctx;
175 } __unm_dma_aligned cardrsp;
178 /** A Phantom DMA buffer area */
179 union phantom_dma_buffer {
180 /** Dummy area required for (read-only) self-tests */
181 uint8_t dummy_dma[UNM_DUMMY_DMA_SIZE];
182 /** RX context creation request and response buffers */
183 struct phantom_create_rx_ctx_rqrsp create_rx_ctx;
184 /** TX context creation request and response buffers */
185 struct phantom_create_tx_ctx_rqrsp create_tx_ctx;
192 /** Current CRB window */
193 unsigned long crb_window;
194 /** CRB window access method */
195 unsigned long ( *crb_access ) ( struct phantom_nic *phantom,
198 /** Number of ports */
200 /** Per-port network devices */
201 struct net_device *netdev[UNM_FLASH_NUM_PORTS];
204 union phantom_dma_buffer *dma_buf;
206 /** Flash memory SPI bus */
207 struct spi_bus spi_bus;
208 /** Flash memory SPI device */
209 struct spi_device flash;
211 /** Last known link state */
215 /***************************************************************************
217 * CRB register access
222 * Prepare for access to CRB register via 128MB BAR
224 * @v phantom Phantom NIC
225 * @v reg Register offset within abstract address space
226 * @ret offset Register offset within PCI BAR0
228 static unsigned long phantom_crb_access_128m ( struct phantom_nic *phantom,
229 unsigned long reg ) {
230 static const uint32_t reg_window[] = {
231 [UNM_CRB_BLK_PCIE] = 0x0000000,
232 [UNM_CRB_BLK_CAM] = 0x2000000,
233 [UNM_CRB_BLK_ROMUSB] = 0x2000000,
234 [UNM_CRB_BLK_TEST] = 0x0000000,
236 static const uint32_t reg_bases[] = {
237 [UNM_CRB_BLK_PCIE] = 0x6100000,
238 [UNM_CRB_BLK_CAM] = 0x6200000,
239 [UNM_CRB_BLK_ROMUSB] = 0x7300000,
240 [UNM_CRB_BLK_TEST] = 0x6200000,
242 unsigned int block = UNM_CRB_BLK ( reg );
243 unsigned long offset = UNM_CRB_OFFSET ( reg );
244 uint32_t window = reg_window[block];
245 uint32_t verify_window;
247 if ( phantom->crb_window != window ) {
249 /* Write to the CRB window register */
250 writel ( window, phantom->bar0 + UNM_128M_CRB_WINDOW );
252 /* Ensure that the write has reached the card */
253 verify_window = readl ( phantom->bar0 + UNM_128M_CRB_WINDOW );
254 assert ( verify_window == window );
256 /* Record new window */
257 phantom->crb_window = window;
260 return ( reg_bases[block] + offset );
264 * Prepare for access to CRB register via 32MB BAR
266 * @v phantom Phantom NIC
267 * @v reg Register offset within abstract address space
268 * @ret offset Register offset within PCI BAR0
270 static unsigned long phantom_crb_access_32m ( struct phantom_nic *phantom,
271 unsigned long reg ) {
272 static const uint32_t reg_window[] = {
273 [UNM_CRB_BLK_PCIE] = 0x0000000,
274 [UNM_CRB_BLK_CAM] = 0x2000000,
275 [UNM_CRB_BLK_ROMUSB] = 0x2000000,
276 [UNM_CRB_BLK_TEST] = 0x0000000,
278 static const uint32_t reg_bases[] = {
279 [UNM_CRB_BLK_PCIE] = 0x0100000,
280 [UNM_CRB_BLK_CAM] = 0x0200000,
281 [UNM_CRB_BLK_ROMUSB] = 0x1300000,
282 [UNM_CRB_BLK_TEST] = 0x0200000,
284 unsigned int block = UNM_CRB_BLK ( reg );
285 unsigned long offset = UNM_CRB_OFFSET ( reg );
286 uint32_t window = reg_window[block];
287 uint32_t verify_window;
289 if ( phantom->crb_window != window ) {
291 /* Write to the CRB window register */
292 writel ( window, phantom->bar0 + UNM_32M_CRB_WINDOW );
294 /* Ensure that the write has reached the card */
295 verify_window = readl ( phantom->bar0 + UNM_32M_CRB_WINDOW );
296 assert ( verify_window == window );
298 /* Record new window */
299 phantom->crb_window = window;
302 return ( reg_bases[block] + offset );
306 * Prepare for access to CRB register via 2MB BAR
308 * @v phantom Phantom NIC
309 * @v reg Register offset within abstract address space
310 * @ret offset Register offset within PCI BAR0
312 static unsigned long phantom_crb_access_2m ( struct phantom_nic *phantom,
313 unsigned long reg ) {
314 static const uint32_t reg_window_hi[] = {
315 [UNM_CRB_BLK_PCIE] = 0x77300000,
316 [UNM_CRB_BLK_CAM] = 0x41600000,
317 [UNM_CRB_BLK_ROMUSB] = 0x42100000,
318 [UNM_CRB_BLK_TEST] = 0x29500000,
320 unsigned int block = UNM_CRB_BLK ( reg );
321 unsigned long offset = UNM_CRB_OFFSET ( reg );
322 uint32_t window = ( reg_window_hi[block] | ( offset & 0x000f0000 ) );
323 uint32_t verify_window;
325 if ( phantom->crb_window != window ) {
327 /* Write to the CRB window register */
328 writel ( window, phantom->bar0 + UNM_2M_CRB_WINDOW );
330 /* Ensure that the write has reached the card */
331 verify_window = readl ( phantom->bar0 + UNM_2M_CRB_WINDOW );
332 assert ( verify_window == window );
334 /* Record new window */
335 phantom->crb_window = window;
338 return ( 0x1e0000 + ( offset & 0xffff ) );
342 * Read from Phantom CRB register
344 * @v phantom Phantom NIC
345 * @v reg Register offset within abstract address space
346 * @ret value Register value
348 static uint32_t phantom_readl ( struct phantom_nic *phantom,
349 unsigned long reg ) {
350 unsigned long offset;
352 offset = phantom->crb_access ( phantom, reg );
353 return readl ( phantom->bar0 + offset );
357 * Write to Phantom CRB register
359 * @v phantom Phantom NIC
360 * @v value Register value
361 * @v reg Register offset within abstract address space
363 static void phantom_writel ( struct phantom_nic *phantom, uint32_t value,
364 unsigned long reg ) {
365 unsigned long offset;
367 offset = phantom->crb_access ( phantom, reg );
368 writel ( value, phantom->bar0 + offset );
372 * Write to Phantom CRB HI/LO register pair
374 * @v phantom Phantom NIC
375 * @v value Register value
376 * @v lo_offset LO register offset within CRB
377 * @v hi_offset HI register offset within CRB
379 static inline void phantom_write_hilo ( struct phantom_nic *phantom,
381 unsigned long lo_offset,
382 unsigned long hi_offset ) {
383 uint32_t lo = ( value & 0xffffffffUL );
384 uint32_t hi = ( value >> 32 );
386 phantom_writel ( phantom, lo, lo_offset );
387 phantom_writel ( phantom, hi, hi_offset );
390 /***************************************************************************
392 * Firmware message buffer access (for debug)
397 * Read from Phantom test memory
399 * @v phantom Phantom NIC
400 * @v offset Offset within test memory
401 * @v buf 8-byte buffer to fill
402 * @ret rc Return status code
404 static int phantom_read_test_mem ( struct phantom_nic *phantom,
405 uint64_t offset, uint32_t buf[2] ) {
406 unsigned int retries;
407 uint32_t test_control;
409 phantom_write_hilo ( phantom, offset, UNM_TEST_ADDR_LO,
411 phantom_writel ( phantom, UNM_TEST_CONTROL_ENABLE, UNM_TEST_CONTROL );
412 phantom_writel ( phantom,
413 ( UNM_TEST_CONTROL_ENABLE | UNM_TEST_CONTROL_START ),
416 for ( retries = 0 ; retries < PHN_TEST_MEM_TIMEOUT_MS ; retries++ ) {
417 test_control = phantom_readl ( phantom, UNM_TEST_CONTROL );
418 if ( ( test_control & UNM_TEST_CONTROL_BUSY ) == 0 ) {
419 buf[0] = phantom_readl ( phantom, UNM_TEST_RDDATA_LO );
420 buf[1] = phantom_readl ( phantom, UNM_TEST_RDDATA_HI );
426 DBGC ( phantom, "Phantom %p timed out waiting for test memory\n",
432 * Dump Phantom firmware dmesg log
434 * @v phantom Phantom NIC
437 static void phantom_dmesg ( struct phantom_nic *phantom, unsigned int log ) {
450 /* Optimise out for non-debug builds */
454 head = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_HEAD ( log ) );
455 len = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_LEN ( log ) );
456 tail = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_TAIL ( log ) );
457 sig = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_SIG ( log ) );
458 DBGC ( phantom, "Phantom %p firmware dmesg buffer %d (%08lx-%08lx)\n",
459 phantom, log, head, tail );
460 assert ( ( head & 0x07 ) == 0 );
461 if ( sig != UNM_CAM_RAM_DMESG_SIG_MAGIC ) {
462 DBGC ( phantom, "Warning: bad signature %08lx (want %08lx)\n",
463 sig, UNM_CAM_RAM_DMESG_SIG_MAGIC );
466 for ( offset = head ; offset < tail ; offset += 8 ) {
467 if ( ( rc = phantom_read_test_mem ( phantom, offset,
468 buf.dwords ) ) != 0 ) {
469 DBGC ( phantom, "Phantom %p could not read from test "
470 "memory: %s\n", phantom, strerror ( rc ) );
473 for ( i = 0 ; ( ( i < sizeof ( buf ) ) &&
474 ( offset + i ) < tail ) ; i++ ) {
475 DBG ( "%c", buf.bytes[i] );
482 * Dump Phantom firmware dmesg logs
484 * @v phantom Phantom NIC
486 static void __attribute__ (( unused ))
487 phantom_dmesg_all ( struct phantom_nic *phantom ) {
490 for ( i = 0 ; i < UNM_CAM_RAM_NUM_DMESG_BUFFERS ; i++ )
491 phantom_dmesg ( phantom, i );
494 /***************************************************************************
496 * SPI bus access (for flash memory)
501 * Acquire Phantom SPI lock
503 * @v phantom Phantom NIC
504 * @ret rc Return status code
506 static int phantom_spi_lock ( struct phantom_nic *phantom ) {
507 unsigned int retries;
508 uint32_t pcie_sem2_lock;
510 for ( retries = 0 ; retries < PHN_SPI_LOCK_TIMEOUT_MS ; retries++ ) {
511 pcie_sem2_lock = phantom_readl ( phantom, UNM_PCIE_SEM2_LOCK );
512 if ( pcie_sem2_lock != 0 )
517 DBGC ( phantom, "Phantom %p timed out waiting for SPI lock\n",
523 * Wait for Phantom SPI command to complete
525 * @v phantom Phantom NIC
526 * @ret rc Return status code
528 static int phantom_spi_wait ( struct phantom_nic *phantom ) {
529 unsigned int retries;
532 for ( retries = 0 ; retries < PHN_SPI_CMD_TIMEOUT_MS ; retries++ ) {
533 glb_status = phantom_readl ( phantom, UNM_ROMUSB_GLB_STATUS );
534 if ( glb_status & UNM_ROMUSB_GLB_STATUS_ROM_DONE )
539 DBGC ( phantom, "Phantom %p timed out waiting for SPI command\n",
545 * Release Phantom SPI lock
547 * @v phantom Phantom NIC
549 static void phantom_spi_unlock ( struct phantom_nic *phantom ) {
550 phantom_readl ( phantom, UNM_PCIE_SEM2_UNLOCK );
554 * Read/write data via Phantom SPI bus
557 * @v device SPI device
559 * @v address Address to read/write (<0 for no address)
560 * @v data_out TX data buffer (or NULL)
561 * @v data_in RX data buffer (or NULL)
562 * @v len Length of data buffer(s)
563 * @ret rc Return status code
565 static int phantom_spi_rw ( struct spi_bus *bus,
566 struct spi_device *device,
567 unsigned int command, int address,
568 const void *data_out, void *data_in,
570 struct phantom_nic *phantom =
571 container_of ( bus, struct phantom_nic, spi_bus );
575 DBGCP ( phantom, "Phantom %p SPI command %x at %x+%zx\n",
576 phantom, command, address, len );
578 DBGCP_HDA ( phantom, address, data_out, len );
580 /* We support only exactly 4-byte reads */
581 if ( len != UNM_SPI_BLKSIZE ) {
582 DBGC ( phantom, "Phantom %p invalid SPI length %zx\n",
587 /* Acquire SPI lock */
588 if ( ( rc = phantom_spi_lock ( phantom ) ) != 0 )
591 /* Issue SPI command as per the PRM */
593 memcpy ( &data, data_out, sizeof ( data ) );
594 phantom_writel ( phantom, data, UNM_ROMUSB_ROM_WDATA );
596 phantom_writel ( phantom, address, UNM_ROMUSB_ROM_ADDRESS );
597 phantom_writel ( phantom, ( device->address_len / 8 ),
598 UNM_ROMUSB_ROM_ABYTE_CNT );
599 udelay ( 100 ); /* according to PRM */
600 phantom_writel ( phantom, 0, UNM_ROMUSB_ROM_DUMMY_BYTE_CNT );
601 phantom_writel ( phantom, command, UNM_ROMUSB_ROM_INSTR_OPCODE );
603 /* Wait for SPI command to complete */
604 if ( ( rc = phantom_spi_wait ( phantom ) ) != 0 )
607 /* Reset address byte count and dummy byte count, because the
610 phantom_writel ( phantom, 0, UNM_ROMUSB_ROM_ABYTE_CNT );
611 udelay ( 100 ); /* according to PRM */
612 phantom_writel ( phantom, 0, UNM_ROMUSB_ROM_DUMMY_BYTE_CNT );
614 /* Read data, if applicable */
616 data = phantom_readl ( phantom, UNM_ROMUSB_ROM_RDATA );
617 memcpy ( data_in, &data, sizeof ( data ) );
618 DBGCP_HDA ( phantom, address, data_in, len );
622 phantom_spi_unlock ( phantom );
627 /***************************************************************************
634 * Wait for firmware to accept command
636 * @v phantom Phantom NIC
637 * @ret rc Return status code
639 static int phantom_wait_for_cmd ( struct phantom_nic *phantom ) {
640 unsigned int retries;
643 for ( retries = 0 ; retries < PHN_ISSUE_CMD_TIMEOUT_MS ; retries++ ) {
645 cdrp = phantom_readl ( phantom, UNM_NIC_REG_NX_CDRP );
646 if ( NX_CDRP_IS_RSP ( cdrp ) ) {
647 switch ( NX_CDRP_FORM_RSP ( cdrp ) ) {
650 case NX_CDRP_RSP_FAIL:
652 case NX_CDRP_RSP_TIMEOUT:
660 DBGC ( phantom, "Phantom %p timed out waiting for firmware to accept "
661 "command\n", phantom );
666 * Issue command to firmware
668 * @v phantom_port Phantom NIC port
669 * @v command Firmware command
673 * @ret rc Return status code
675 static int phantom_issue_cmd ( struct phantom_nic_port *phantom_port,
676 uint32_t command, uint32_t arg1, uint32_t arg2,
678 struct phantom_nic *phantom = phantom_port->phantom;
683 signature = NX_CDRP_SIGNATURE_MAKE ( phantom_port->port,
685 DBGC2 ( phantom, "Phantom %p port %d issuing command %08lx (%08lx, "
686 "%08lx, %08lx)\n", phantom, phantom_port->port,
687 command, arg1, arg2, arg3 );
688 phantom_writel ( phantom, signature, UNM_NIC_REG_NX_SIGN );
689 phantom_writel ( phantom, arg1, UNM_NIC_REG_NX_ARG1 );
690 phantom_writel ( phantom, arg2, UNM_NIC_REG_NX_ARG2 );
691 phantom_writel ( phantom, arg3, UNM_NIC_REG_NX_ARG3 );
692 phantom_writel ( phantom, NX_CDRP_FORM_CMD ( command ),
693 UNM_NIC_REG_NX_CDRP );
695 /* Wait for command to be accepted */
696 if ( ( rc = phantom_wait_for_cmd ( phantom ) ) != 0 ) {
697 DBGC ( phantom, "Phantom %p could not issue command: %s\n",
698 phantom, strerror ( rc ) );
706 * Issue buffer-format command to firmware
708 * @v phantom_port Phantom NIC port
709 * @v command Firmware command
710 * @v buffer Buffer to pass to firmware
711 * @v len Length of buffer
712 * @ret rc Return status code
714 static int phantom_issue_buf_cmd ( struct phantom_nic_port *phantom_port,
715 uint32_t command, void *buffer,
719 physaddr = virt_to_bus ( buffer );
720 return phantom_issue_cmd ( phantom_port, command, ( physaddr >> 32 ),
721 ( physaddr & 0xffffffffUL ), len );
725 * Create Phantom RX context
727 * @v phantom_port Phantom NIC port
728 * @ret rc Return status code
730 static int phantom_create_rx_ctx ( struct phantom_nic_port *phantom_port ) {
731 struct phantom_nic *phantom = phantom_port->phantom;
732 struct phantom_create_rx_ctx_rqrsp *buf;
735 /* Prepare request */
736 buf = &phantom->dma_buf->create_rx_ctx;
737 memset ( buf, 0, sizeof ( *buf ) );
738 buf->hostrq.rx_ctx.host_rsp_dma_addr =
739 cpu_to_le64 ( virt_to_bus ( &buf->cardrsp ) );
740 buf->hostrq.rx_ctx.capabilities[0] =
741 cpu_to_le32 ( NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN );
742 buf->hostrq.rx_ctx.host_int_crb_mode =
743 cpu_to_le32 ( NX_HOST_INT_CRB_MODE_SHARED );
744 buf->hostrq.rx_ctx.host_rds_crb_mode =
745 cpu_to_le32 ( NX_HOST_RDS_CRB_MODE_UNIQUE );
746 buf->hostrq.rx_ctx.rds_ring_offset = cpu_to_le32 ( 0 );
747 buf->hostrq.rx_ctx.sds_ring_offset =
748 cpu_to_le32 ( sizeof ( buf->hostrq.rds ) );
749 buf->hostrq.rx_ctx.num_rds_rings = cpu_to_le16 ( 1 );
750 buf->hostrq.rx_ctx.num_sds_rings = cpu_to_le16 ( 1 );
751 buf->hostrq.rds.host_phys_addr =
752 cpu_to_le64 ( virt_to_bus ( phantom_port->desc->rds ) );
753 buf->hostrq.rds.buff_size = cpu_to_le64 ( PHN_RX_BUFSIZE );
754 buf->hostrq.rds.ring_size = cpu_to_le32 ( PHN_NUM_RDS );
755 buf->hostrq.rds.ring_kind = cpu_to_le32 ( NX_RDS_RING_TYPE_NORMAL );
756 buf->hostrq.sds.host_phys_addr =
757 cpu_to_le64 ( virt_to_bus ( phantom_port->desc->sds ) );
758 buf->hostrq.sds.ring_size = cpu_to_le32 ( PHN_NUM_SDS );
760 DBGC ( phantom, "Phantom %p port %d creating RX context\n",
761 phantom, phantom_port->port );
762 DBGC2_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
763 &buf->hostrq, sizeof ( buf->hostrq ) );
766 if ( ( rc = phantom_issue_buf_cmd ( phantom_port,
767 NX_CDRP_CMD_CREATE_RX_CTX,
769 sizeof ( buf->hostrq ) ) ) != 0 ) {
770 DBGC ( phantom, "Phantom %p port %d could not create RX "
772 phantom, phantom_port->port, strerror ( rc ) );
773 DBGC ( phantom, "Request:\n" );
774 DBGC_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
775 &buf->hostrq, sizeof ( buf->hostrq ) );
776 DBGC ( phantom, "Response:\n" );
777 DBGC_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
778 &buf->cardrsp, sizeof ( buf->cardrsp ) );
782 /* Retrieve context parameters */
783 phantom_port->rx_context_id =
784 le16_to_cpu ( buf->cardrsp.rx_ctx.context_id );
785 phantom_port->rds_producer_crb =
787 le32_to_cpu ( buf->cardrsp.rds.host_producer_crb ));
788 phantom_port->sds_consumer_crb =
790 le32_to_cpu ( buf->cardrsp.sds.host_consumer_crb ));
792 DBGC ( phantom, "Phantom %p port %d created RX context (id %04x, "
793 "port phys %02x virt %02x)\n", phantom, phantom_port->port,
794 phantom_port->rx_context_id, buf->cardrsp.rx_ctx.phys_port,
795 buf->cardrsp.rx_ctx.virt_port );
796 DBGC2_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
797 &buf->cardrsp, sizeof ( buf->cardrsp ) );
798 DBGC ( phantom, "Phantom %p port %d RDS producer CRB is %08lx\n",
799 phantom, phantom_port->port, phantom_port->rds_producer_crb );
800 DBGC ( phantom, "Phantom %p port %d SDS consumer CRB is %08lx\n",
801 phantom, phantom_port->port, phantom_port->sds_consumer_crb );
807 * Destroy Phantom RX context
809 * @v phantom_port Phantom NIC port
810 * @ret rc Return status code
812 static void phantom_destroy_rx_ctx ( struct phantom_nic_port *phantom_port ) {
813 struct phantom_nic *phantom = phantom_port->phantom;
816 DBGC ( phantom, "Phantom %p port %d destroying RX context (id %04x)\n",
817 phantom, phantom_port->port, phantom_port->rx_context_id );
820 if ( ( rc = phantom_issue_cmd ( phantom_port,
821 NX_CDRP_CMD_DESTROY_RX_CTX,
822 phantom_port->rx_context_id,
823 NX_DESTROY_CTX_RESET, 0 ) ) != 0 ) {
824 DBGC ( phantom, "Phantom %p port %d could not destroy RX "
826 phantom, phantom_port->port, strerror ( rc ) );
827 /* We're probably screwed */
831 /* Clear context parameters */
832 phantom_port->rx_context_id = 0;
833 phantom_port->rds_producer_crb = 0;
834 phantom_port->sds_consumer_crb = 0;
836 /* Reset software counters */
837 phantom_port->rds_producer_idx = 0;
838 phantom_port->rds_consumer_idx = 0;
839 phantom_port->sds_consumer_idx = 0;
843 * Create Phantom TX context
845 * @v phantom_port Phantom NIC port
846 * @ret rc Return status code
848 static int phantom_create_tx_ctx ( struct phantom_nic_port *phantom_port ) {
849 struct phantom_nic *phantom = phantom_port->phantom;
850 struct phantom_create_tx_ctx_rqrsp *buf;
853 /* Prepare request */
854 buf = &phantom->dma_buf->create_tx_ctx;
855 memset ( buf, 0, sizeof ( *buf ) );
856 buf->hostrq.tx_ctx.host_rsp_dma_addr =
857 cpu_to_le64 ( virt_to_bus ( &buf->cardrsp ) );
858 buf->hostrq.tx_ctx.cmd_cons_dma_addr =
859 cpu_to_le64 ( virt_to_bus ( &phantom_port->desc->cmd_cons ) );
860 buf->hostrq.tx_ctx.dummy_dma_addr =
861 cpu_to_le64 ( virt_to_bus ( phantom->dma_buf->dummy_dma ) );
862 buf->hostrq.tx_ctx.capabilities[0] =
863 cpu_to_le32 ( NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN );
864 buf->hostrq.tx_ctx.host_int_crb_mode =
865 cpu_to_le32 ( NX_HOST_INT_CRB_MODE_SHARED );
866 buf->hostrq.tx_ctx.cds_ring.host_phys_addr =
867 cpu_to_le64 ( virt_to_bus ( phantom_port->desc->cds ) );
868 buf->hostrq.tx_ctx.cds_ring.ring_size = cpu_to_le32 ( PHN_NUM_CDS );
870 DBGC ( phantom, "Phantom %p port %d creating TX context\n",
871 phantom, phantom_port->port );
872 DBGC2_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
873 &buf->hostrq, sizeof ( buf->hostrq ) );
876 if ( ( rc = phantom_issue_buf_cmd ( phantom_port,
877 NX_CDRP_CMD_CREATE_TX_CTX,
879 sizeof ( buf->hostrq ) ) ) != 0 ) {
880 DBGC ( phantom, "Phantom %p port %d could not create TX "
882 phantom, phantom_port->port, strerror ( rc ) );
883 DBGC ( phantom, "Request:\n" );
884 DBGC_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
885 &buf->hostrq, sizeof ( buf->hostrq ) );
886 DBGC ( phantom, "Response:\n" );
887 DBGC_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
888 &buf->cardrsp, sizeof ( buf->cardrsp ) );
892 /* Retrieve context parameters */
893 phantom_port->tx_context_id =
894 le16_to_cpu ( buf->cardrsp.tx_ctx.context_id );
895 phantom_port->cds_producer_crb =
897 le32_to_cpu(buf->cardrsp.tx_ctx.cds_ring.host_producer_crb));
899 DBGC ( phantom, "Phantom %p port %d created TX context (id %04x, "
900 "port phys %02x virt %02x)\n", phantom, phantom_port->port,
901 phantom_port->tx_context_id, buf->cardrsp.tx_ctx.phys_port,
902 buf->cardrsp.tx_ctx.virt_port );
903 DBGC2_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
904 &buf->cardrsp, sizeof ( buf->cardrsp ) );
905 DBGC ( phantom, "Phantom %p port %d CDS producer CRB is %08lx\n",
906 phantom, phantom_port->port, phantom_port->cds_producer_crb );
912 * Destroy Phantom TX context
914 * @v phantom_port Phantom NIC port
915 * @ret rc Return status code
917 static void phantom_destroy_tx_ctx ( struct phantom_nic_port *phantom_port ) {
918 struct phantom_nic *phantom = phantom_port->phantom;
921 DBGC ( phantom, "Phantom %p port %d destroying TX context (id %04x)\n",
922 phantom, phantom_port->port, phantom_port->tx_context_id );
925 if ( ( rc = phantom_issue_cmd ( phantom_port,
926 NX_CDRP_CMD_DESTROY_TX_CTX,
927 phantom_port->tx_context_id,
928 NX_DESTROY_CTX_RESET, 0 ) ) != 0 ) {
929 DBGC ( phantom, "Phantom %p port %d could not destroy TX "
931 phantom, phantom_port->port, strerror ( rc ) );
932 /* We're probably screwed */
936 /* Clear context parameters */
937 phantom_port->tx_context_id = 0;
938 phantom_port->cds_producer_crb = 0;
940 /* Reset software counters */
941 phantom_port->cds_producer_idx = 0;
942 phantom_port->cds_consumer_idx = 0;
945 /***************************************************************************
947 * Descriptor ring management
952 * Allocate Phantom RX descriptor
954 * @v phantom_port Phantom NIC port
955 * @ret index RX descriptor index, or negative error
957 static int phantom_alloc_rds ( struct phantom_nic_port *phantom_port ) {
958 struct phantom_nic *phantom = phantom_port->phantom;
959 unsigned int rds_producer_idx;
960 unsigned int next_rds_producer_idx;
962 /* Check for space in the ring. RX descriptors are consumed
963 * out of order, but they are *read* by the hardware in strict
964 * order. We maintain a pessimistic consumer index, which is
965 * guaranteed never to be an overestimate of the number of
966 * descriptors read by the hardware.
968 rds_producer_idx = phantom_port->rds_producer_idx;
969 next_rds_producer_idx = ( ( rds_producer_idx + 1 ) % PHN_NUM_RDS );
970 if ( next_rds_producer_idx == phantom_port->rds_consumer_idx ) {
971 DBGC ( phantom, "Phantom %p port %d RDS ring full (index %d "
972 "not consumed)\n", phantom, phantom_port->port,
973 next_rds_producer_idx );
977 return rds_producer_idx;
981 * Post Phantom RX descriptor
983 * @v phantom_port Phantom NIC port
984 * @v rds RX descriptor
986 static void phantom_post_rds ( struct phantom_nic_port *phantom_port,
987 struct phantom_rds *rds ) {
988 struct phantom_nic *phantom = phantom_port->phantom;
989 unsigned int rds_producer_idx;
990 unsigned int next_rds_producer_idx;
991 struct phantom_rds *entry;
993 /* Copy descriptor to ring */
994 rds_producer_idx = phantom_port->rds_producer_idx;
995 entry = &phantom_port->desc->rds[rds_producer_idx];
996 memcpy ( entry, rds, sizeof ( *entry ) );
997 DBGC2 ( phantom, "Phantom %p port %d posting RDS %ld (slot %d):\n",
998 phantom, phantom_port->port, NX_GET ( rds, handle ),
1000 DBGC2_HDA ( phantom, virt_to_bus ( entry ), entry, sizeof ( *entry ) );
1002 /* Update producer index */
1003 next_rds_producer_idx = ( ( rds_producer_idx + 1 ) % PHN_NUM_RDS );
1004 phantom_port->rds_producer_idx = next_rds_producer_idx;
1006 phantom_writel ( phantom, phantom_port->rds_producer_idx,
1007 phantom_port->rds_producer_crb );
1011 * Allocate Phantom TX descriptor
1013 * @v phantom_port Phantom NIC port
1014 * @ret index TX descriptor index, or negative error
1016 static int phantom_alloc_cds ( struct phantom_nic_port *phantom_port ) {
1017 struct phantom_nic *phantom = phantom_port->phantom;
1018 unsigned int cds_producer_idx;
1019 unsigned int next_cds_producer_idx;
1021 /* Check for space in the ring. TX descriptors are consumed
1022 * in strict order, so we just check for a collision against
1023 * the consumer index.
1025 cds_producer_idx = phantom_port->cds_producer_idx;
1026 next_cds_producer_idx = ( ( cds_producer_idx + 1 ) % PHN_NUM_CDS );
1027 if ( next_cds_producer_idx == phantom_port->cds_consumer_idx ) {
1028 DBGC ( phantom, "Phantom %p port %d CDS ring full (index %d "
1029 "not consumed)\n", phantom, phantom_port->port,
1030 next_cds_producer_idx );
1034 return cds_producer_idx;
1038 * Post Phantom TX descriptor
1040 * @v phantom_port Phantom NIC port
1041 * @v cds TX descriptor
1043 static void phantom_post_cds ( struct phantom_nic_port *phantom_port,
1044 union phantom_cds *cds ) {
1045 struct phantom_nic *phantom = phantom_port->phantom;
1046 unsigned int cds_producer_idx;
1047 unsigned int next_cds_producer_idx;
1048 union phantom_cds *entry;
1050 /* Copy descriptor to ring */
1051 cds_producer_idx = phantom_port->cds_producer_idx;
1052 entry = &phantom_port->desc->cds[cds_producer_idx];
1053 memcpy ( entry, cds, sizeof ( *entry ) );
1054 DBGC2 ( phantom, "Phantom %p port %d posting CDS %d:\n",
1055 phantom, phantom_port->port, cds_producer_idx );
1056 DBGC2_HDA ( phantom, virt_to_bus ( entry ), entry, sizeof ( *entry ) );
1058 /* Update producer index */
1059 next_cds_producer_idx = ( ( cds_producer_idx + 1 ) % PHN_NUM_CDS );
1060 phantom_port->cds_producer_idx = next_cds_producer_idx;
1062 phantom_writel ( phantom, phantom_port->cds_producer_idx,
1063 phantom_port->cds_producer_crb );
1066 /***************************************************************************
1068 * MAC address management
1073 * Add/remove MAC address
1075 * @v phantom_port Phantom NIC port
1076 * @v ll_addr MAC address to add or remove
1077 * @v opcode MAC request opcode
1078 * @ret rc Return status code
1080 static int phantom_update_macaddr ( struct phantom_nic_port *phantom_port,
1081 const uint8_t *ll_addr,
1082 unsigned int opcode ) {
1083 union phantom_cds cds;
1086 /* Get descriptor ring entry */
1087 index = phantom_alloc_cds ( phantom_port );
1091 /* Fill descriptor ring entry */
1092 memset ( &cds, 0, sizeof ( cds ) );
1093 NX_FILL_1 ( &cds, 0,
1094 nic_request.common.opcode, UNM_NIC_REQUEST );
1095 NX_FILL_2 ( &cds, 1,
1096 nic_request.header.opcode, UNM_MAC_EVENT,
1097 nic_request.header.context_id, phantom_port->port );
1098 NX_FILL_7 ( &cds, 2,
1099 nic_request.body.mac_request.opcode, opcode,
1100 nic_request.body.mac_request.mac_addr_0, ll_addr[0],
1101 nic_request.body.mac_request.mac_addr_1, ll_addr[1],
1102 nic_request.body.mac_request.mac_addr_2, ll_addr[2],
1103 nic_request.body.mac_request.mac_addr_3, ll_addr[3],
1104 nic_request.body.mac_request.mac_addr_4, ll_addr[4],
1105 nic_request.body.mac_request.mac_addr_5, ll_addr[5] );
1107 /* Post descriptor */
1108 phantom_post_cds ( phantom_port, &cds );
1116 * @v phantom_port Phantom NIC port
1117 * @v ll_addr MAC address to add or remove
1118 * @ret rc Return status code
1120 static inline int phantom_add_macaddr ( struct phantom_nic_port *phantom_port,
1121 const uint8_t *ll_addr ) {
1122 struct phantom_nic *phantom = phantom_port->phantom;
1124 DBGC ( phantom, "Phantom %p port %d adding MAC address %s\n",
1125 phantom, phantom_port->port, eth_ntoa ( ll_addr ) );
1127 return phantom_update_macaddr ( phantom_port, ll_addr, UNM_MAC_ADD );
1131 * Remove MAC address
1133 * @v phantom_port Phantom NIC port
1134 * @v ll_addr MAC address to add or remove
1135 * @ret rc Return status code
1137 static inline int phantom_del_macaddr ( struct phantom_nic_port *phantom_port,
1138 const uint8_t *ll_addr ) {
1139 struct phantom_nic *phantom = phantom_port->phantom;
1141 DBGC ( phantom, "Phantom %p port %d removing MAC address %s\n",
1142 phantom, phantom_port->port, eth_ntoa ( ll_addr ) );
1144 return phantom_update_macaddr ( phantom_port, ll_addr, UNM_MAC_DEL );
1147 /***************************************************************************
1149 * Link state detection
1156 * @v phantom Phantom NIC
1158 static void phantom_poll_link_state ( struct phantom_nic *phantom ) {
1159 struct net_device *netdev;
1160 struct phantom_nic_port *phantom_port;
1161 uint32_t xg_state_p3;
1165 /* Read link state */
1166 xg_state_p3 = phantom_readl ( phantom, UNM_NIC_REG_XG_STATE_P3 );
1168 /* If there is no change, do nothing */
1169 if ( phantom->link_state == xg_state_p3 )
1172 /* Record new link state */
1173 DBGC ( phantom, "Phantom %p new link state %08lx (was %08lx)\n",
1174 phantom, xg_state_p3, phantom->link_state );
1175 phantom->link_state = xg_state_p3;
1177 /* Indicate per-port link state to gPXE */
1178 for ( i = 0 ; i < phantom->num_ports ; i++ ) {
1179 netdev = phantom->netdev[i];
1180 phantom_port = netdev_priv ( netdev );
1181 link = UNM_NIC_REG_XG_STATE_P3_LINK ( phantom_port->port,
1182 phantom->link_state );
1184 case UNM_NIC_REG_XG_STATE_P3_LINK_UP:
1185 DBGC ( phantom, "Phantom %p port %d link is up\n",
1186 phantom, phantom_port->port );
1187 netdev_link_up ( netdev );
1189 case UNM_NIC_REG_XG_STATE_P3_LINK_DOWN:
1190 DBGC ( phantom, "Phantom %p port %d link is down\n",
1191 phantom, phantom_port->port );
1192 netdev_link_down ( netdev );
1195 DBGC ( phantom, "Phantom %p port %d bad link state "
1196 "%d\n", phantom, phantom_port->port, link );
1202 /***************************************************************************
1209 * Refill descriptor ring
1211 * @v netdev Net device
1213 static void phantom_refill_rx_ring ( struct net_device *netdev ) {
1214 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1215 struct io_buffer *iobuf;
1216 struct phantom_rds rds;
1217 unsigned int handle;
1220 for ( handle = 0 ; handle < PHN_RDS_MAX_FILL ; handle++ ) {
1222 /* Skip this index if the descriptor has not yet been
1225 if ( phantom_port->rds_iobuf[handle] != NULL )
1228 /* Allocate descriptor ring entry */
1229 index = phantom_alloc_rds ( phantom_port );
1230 assert ( PHN_RDS_MAX_FILL < PHN_NUM_RDS );
1231 assert ( index >= 0 ); /* Guaranteed by MAX_FILL < NUM_RDS ) */
1233 /* Try to allocate an I/O buffer */
1234 iobuf = alloc_iob ( PHN_RX_BUFSIZE );
1236 /* Failure is non-fatal; we will retry later */
1237 netdev_rx_err ( netdev, NULL, -ENOMEM );
1241 /* Fill descriptor ring entry */
1242 memset ( &rds, 0, sizeof ( rds ) );
1243 NX_FILL_2 ( &rds, 0,
1245 length, iob_len ( iobuf ) );
1246 NX_FILL_1 ( &rds, 1,
1247 dma_addr, virt_to_bus ( iobuf->data ) );
1249 /* Record I/O buffer */
1250 assert ( phantom_port->rds_iobuf[handle] == NULL );
1251 phantom_port->rds_iobuf[handle] = iobuf;
1253 /* Post descriptor */
1254 phantom_post_rds ( phantom_port, &rds );
1261 * @v netdev Net device
1262 * @ret rc Return status code
1264 static int phantom_open ( struct net_device *netdev ) {
1265 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1268 /* Allocate and zero descriptor rings */
1269 phantom_port->desc = malloc_dma ( sizeof ( *(phantom_port->desc) ),
1270 UNM_DMA_BUFFER_ALIGN );
1271 if ( ! phantom_port->desc ) {
1273 goto err_alloc_desc;
1275 memset ( phantom_port->desc, 0, sizeof ( *(phantom_port->desc) ) );
1277 /* Create RX context */
1278 if ( ( rc = phantom_create_rx_ctx ( phantom_port ) ) != 0 )
1279 goto err_create_rx_ctx;
1281 /* Create TX context */
1282 if ( ( rc = phantom_create_tx_ctx ( phantom_port ) ) != 0 )
1283 goto err_create_tx_ctx;
1285 /* Fill the RX descriptor ring */
1286 phantom_refill_rx_ring ( netdev );
1288 /* Add MAC addresses
1292 * We would like to be able to enable receiving all multicast
1293 * packets (or, failing that, promiscuous mode), but the
1294 * firmware doesn't currently support this.
1296 if ( ( rc = phantom_add_macaddr ( phantom_port,
1297 netdev->ll_protocol->ll_broadcast ) ) != 0 )
1298 goto err_add_macaddr_broadcast;
1299 if ( ( rc = phantom_add_macaddr ( phantom_port,
1300 netdev->ll_addr ) ) != 0 )
1301 goto err_add_macaddr_unicast;
1305 phantom_del_macaddr ( phantom_port, netdev->ll_addr );
1306 err_add_macaddr_unicast:
1307 phantom_del_macaddr ( phantom_port,
1308 netdev->ll_protocol->ll_broadcast );
1309 err_add_macaddr_broadcast:
1310 phantom_destroy_tx_ctx ( phantom_port );
1312 phantom_destroy_rx_ctx ( phantom_port );
1314 free_dma ( phantom_port->desc, sizeof ( *(phantom_port->desc) ) );
1315 phantom_port->desc = NULL;
1323 * @v netdev Net device
1325 static void phantom_close ( struct net_device *netdev ) {
1326 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1327 struct io_buffer *iobuf;
1330 /* Shut down the port */
1331 phantom_del_macaddr ( phantom_port, netdev->ll_addr );
1332 phantom_del_macaddr ( phantom_port,
1333 netdev->ll_protocol->ll_broadcast );
1334 phantom_destroy_tx_ctx ( phantom_port );
1335 phantom_destroy_rx_ctx ( phantom_port );
1336 free_dma ( phantom_port->desc, sizeof ( *(phantom_port->desc) ) );
1337 phantom_port->desc = NULL;
1339 /* Flush any uncompleted descriptors */
1340 for ( i = 0 ; i < PHN_RDS_MAX_FILL ; i++ ) {
1341 iobuf = phantom_port->rds_iobuf[i];
1344 phantom_port->rds_iobuf[i] = NULL;
1347 for ( i = 0 ; i < PHN_NUM_CDS ; i++ ) {
1348 iobuf = phantom_port->cds_iobuf[i];
1350 netdev_tx_complete_err ( netdev, iobuf, -ECANCELED );
1351 phantom_port->cds_iobuf[i] = NULL;
1359 * @v netdev Network device
1360 * @v iobuf I/O buffer
1361 * @ret rc Return status code
1363 static int phantom_transmit ( struct net_device *netdev,
1364 struct io_buffer *iobuf ) {
1365 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1366 union phantom_cds cds;
1369 /* Get descriptor ring entry */
1370 index = phantom_alloc_cds ( phantom_port );
1374 /* Fill descriptor ring entry */
1375 memset ( &cds, 0, sizeof ( cds ) );
1376 NX_FILL_3 ( &cds, 0,
1377 tx.opcode, UNM_TX_ETHER_PKT,
1379 tx.length, iob_len ( iobuf ) );
1380 NX_FILL_2 ( &cds, 2,
1381 tx.port, phantom_port->port,
1382 tx.context_id, phantom_port->port );
1383 NX_FILL_1 ( &cds, 4,
1384 tx.buffer1_dma_addr, virt_to_bus ( iobuf->data ) );
1385 NX_FILL_1 ( &cds, 5,
1386 tx.buffer1_length, iob_len ( iobuf ) );
1388 /* Record I/O buffer */
1389 assert ( phantom_port->cds_iobuf[index] == NULL );
1390 phantom_port->cds_iobuf[index] = iobuf;
1392 /* Post descriptor */
1393 phantom_post_cds ( phantom_port, &cds );
1399 * Poll for received packets
1401 * @v netdev Network device
1403 static void phantom_poll ( struct net_device *netdev ) {
1404 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1405 struct phantom_nic *phantom = phantom_port->phantom;
1406 struct io_buffer *iobuf;
1407 unsigned int cds_consumer_idx;
1408 unsigned int raw_new_cds_consumer_idx;
1409 unsigned int new_cds_consumer_idx;
1410 unsigned int rds_consumer_idx;
1411 unsigned int sds_consumer_idx;
1412 struct phantom_sds *sds;
1413 unsigned int sds_handle;
1414 unsigned int sds_opcode;
1416 /* Check for TX completions */
1417 cds_consumer_idx = phantom_port->cds_consumer_idx;
1418 raw_new_cds_consumer_idx = phantom_port->desc->cmd_cons;
1419 new_cds_consumer_idx = le32_to_cpu ( raw_new_cds_consumer_idx );
1420 while ( cds_consumer_idx != new_cds_consumer_idx ) {
1421 DBGC2 ( phantom, "Phantom %p port %d CDS %d complete\n",
1422 phantom, phantom_port->port, cds_consumer_idx );
1423 /* Completions may be for commands other than TX, so
1424 * there may not always be an associated I/O buffer.
1426 if ( ( iobuf = phantom_port->cds_iobuf[cds_consumer_idx] ) ) {
1427 netdev_tx_complete ( netdev, iobuf );
1428 phantom_port->cds_iobuf[cds_consumer_idx] = NULL;
1430 cds_consumer_idx = ( ( cds_consumer_idx + 1 ) % PHN_NUM_CDS );
1431 phantom_port->cds_consumer_idx = cds_consumer_idx;
1434 /* Check for received packets */
1435 rds_consumer_idx = phantom_port->rds_consumer_idx;
1436 sds_consumer_idx = phantom_port->sds_consumer_idx;
1438 sds = &phantom_port->desc->sds[sds_consumer_idx];
1439 if ( NX_GET ( sds, owner ) == 0 )
1442 DBGC2 ( phantom, "Phantom %p port %d SDS %d status:\n",
1443 phantom, phantom_port->port, sds_consumer_idx );
1444 DBGC2_HDA ( phantom, virt_to_bus ( sds ), sds, sizeof (*sds) );
1446 /* Check received opcode */
1447 sds_opcode = NX_GET ( sds, opcode );
1448 if ( ( sds_opcode == UNM_RXPKT_DESC ) ||
1449 ( sds_opcode == UNM_SYN_OFFLOAD ) ) {
1451 /* Sanity check: ensure that all of the SDS
1452 * descriptor has been written.
1454 if ( NX_GET ( sds, total_length ) == 0 ) {
1455 DBGC ( phantom, "Phantom %p port %d SDS %d "
1456 "incomplete; deferring\n", phantom,
1457 phantom_port->port, sds_consumer_idx );
1458 /* Leave for next poll() */
1462 /* Process received packet */
1463 sds_handle = NX_GET ( sds, handle );
1464 iobuf = phantom_port->rds_iobuf[sds_handle];
1465 assert ( iobuf != NULL );
1466 iob_put ( iobuf, NX_GET ( sds, total_length ) );
1467 iob_pull ( iobuf, NX_GET ( sds, pkt_offset ) );
1468 DBGC2 ( phantom, "Phantom %p port %d RDS %d "
1470 phantom, phantom_port->port, sds_handle );
1471 netdev_rx ( netdev, iobuf );
1472 phantom_port->rds_iobuf[sds_handle] = NULL;
1474 /* Update RDS consumer counter. This is a
1475 * lower bound for the number of descriptors
1476 * that have been read by the hardware, since
1477 * the hardware must have read at least one
1478 * descriptor for each completion that we
1482 ( ( rds_consumer_idx + 1 ) % PHN_NUM_RDS );
1483 phantom_port->rds_consumer_idx = rds_consumer_idx;
1487 DBGC ( phantom, "Phantom %p port %d unexpected SDS "
1489 phantom, phantom_port->port, sds_opcode );
1490 DBGC_HDA ( phantom, virt_to_bus ( sds ),
1491 sds, sizeof ( *sds ) );
1494 /* Clear status descriptor */
1495 memset ( sds, 0, sizeof ( *sds ) );
1497 /* Update SDS consumer index */
1498 sds_consumer_idx = ( ( sds_consumer_idx + 1 ) % PHN_NUM_SDS );
1499 phantom_port->sds_consumer_idx = sds_consumer_idx;
1501 phantom_writel ( phantom, phantom_port->sds_consumer_idx,
1502 phantom_port->sds_consumer_crb );
1505 /* Refill the RX descriptor ring */
1506 phantom_refill_rx_ring ( netdev );
1508 /* Occasionally poll the link state */
1509 if ( phantom_port->link_poll_timer-- == 0 ) {
1510 phantom_poll_link_state ( phantom );
1511 /* Reset the link poll timer */
1512 phantom_port->link_poll_timer = PHN_LINK_POLL_FREQUENCY;
1517 * Enable/disable interrupts
1519 * @v netdev Network device
1520 * @v enable Interrupts should be enabled
1522 static void phantom_irq ( struct net_device *netdev, int enable ) {
1523 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1524 struct phantom_nic *phantom = phantom_port->phantom;
1525 static const unsigned long sw_int_mask_reg[UNM_FLASH_NUM_PORTS] = {
1526 UNM_NIC_REG_SW_INT_MASK_0,
1527 UNM_NIC_REG_SW_INT_MASK_1,
1528 UNM_NIC_REG_SW_INT_MASK_2,
1529 UNM_NIC_REG_SW_INT_MASK_3
1532 phantom_writel ( phantom,
1534 sw_int_mask_reg[phantom_port->port] );
1537 /** Phantom net device operations */
1538 static struct net_device_operations phantom_operations = {
1539 .open = phantom_open,
1540 .close = phantom_close,
1541 .transmit = phantom_transmit,
1542 .poll = phantom_poll,
1547 * Map Phantom CRB window
1549 * @v phantom Phantom NIC
1550 * @ret rc Return status code
1552 static int phantom_map_crb ( struct phantom_nic *phantom,
1553 struct pci_device *pci ) {
1554 unsigned long bar0_start;
1555 unsigned long bar0_size;
1557 bar0_start = pci_bar_start ( pci, PCI_BASE_ADDRESS_0 );
1558 bar0_size = pci_bar_size ( pci, PCI_BASE_ADDRESS_0 );
1559 DBGC ( phantom, "Phantom %p BAR0 is %08lx+%lx\n",
1560 phantom, bar0_start, bar0_size );
1562 switch ( bar0_size ) {
1563 case ( 128 * 1024 * 1024 ) :
1564 DBGC ( phantom, "Phantom %p has 128MB BAR\n", phantom );
1565 phantom->crb_access = phantom_crb_access_128m;
1567 case ( 32 * 1024 * 1024 ) :
1568 DBGC ( phantom, "Phantom %p has 32MB BAR\n", phantom );
1569 phantom->crb_access = phantom_crb_access_32m;
1571 case ( 2 * 1024 * 1024 ) :
1572 DBGC ( phantom, "Phantom %p has 2MB BAR\n", phantom );
1573 phantom->crb_access = phantom_crb_access_2m;
1576 DBGC ( phantom, "Phantom %p has bad BAR size\n", phantom );
1580 phantom->bar0 = ioremap ( bar0_start, bar0_size );
1581 if ( ! phantom->bar0 ) {
1582 DBGC ( phantom, "Phantom %p could not map BAR0\n", phantom );
1586 /* Mark current CRB window as invalid, so that the first
1587 * read/write will set the current window.
1589 phantom->crb_window = -1UL;
1595 * Read Phantom flash contents
1597 * @v phantom Phantom NIC
1598 * @ret rc Return status code
1600 static int phantom_read_flash ( struct phantom_nic *phantom ) {
1601 struct unm_board_info board_info;
1604 /* Initialise flash access */
1605 phantom->spi_bus.rw = phantom_spi_rw;
1606 phantom->flash.bus = &phantom->spi_bus;
1607 init_m25p32 ( &phantom->flash );
1608 /* Phantom doesn't support greater than 4-byte block sizes */
1609 phantom->flash.nvs.block_size = UNM_SPI_BLKSIZE;
1611 /* Read and verify board information */
1612 if ( ( rc = nvs_read ( &phantom->flash.nvs, UNM_BRDCFG_START,
1613 &board_info, sizeof ( board_info ) ) ) != 0 ) {
1614 DBGC ( phantom, "Phantom %p could not read board info: %s\n",
1615 phantom, strerror ( rc ) );
1618 if ( board_info.magic != UNM_BDINFO_MAGIC ) {
1619 DBGC ( phantom, "Phantom %p has bad board info magic %lx\n",
1620 phantom, board_info.magic );
1621 DBGC_HD ( phantom, &board_info, sizeof ( board_info ) );
1624 if ( board_info.header_version != UNM_BDINFO_VERSION ) {
1625 DBGC ( phantom, "Phantom %p has bad board info version %lx\n",
1626 phantom, board_info.header_version );
1627 DBGC_HD ( phantom, &board_info, sizeof ( board_info ) );
1631 /* Identify board type and number of ports */
1632 switch ( board_info.board_type ) {
1633 case UNM_BRDTYPE_P3_4_GB:
1634 case UNM_BRDTYPE_P3_4_GB_MM:
1635 phantom->num_ports = 4;
1637 case UNM_BRDTYPE_P3_HMEZ:
1638 case UNM_BRDTYPE_P3_IMEZ:
1639 case UNM_BRDTYPE_P3_10G_CX4:
1640 case UNM_BRDTYPE_P3_10G_CX4_LP:
1641 case UNM_BRDTYPE_P3_10G_SFP_PLUS:
1642 case UNM_BRDTYPE_P3_XG_LOM:
1643 phantom->num_ports = 2;
1645 case UNM_BRDTYPE_P3_10000_BASE_T:
1646 case UNM_BRDTYPE_P3_10G_XFP:
1647 phantom->num_ports = 1;
1650 DBGC ( phantom, "Phantom %p unrecognised board type %#lx; "
1651 "assuming single-port\n",
1652 phantom, board_info.board_type );
1653 phantom->num_ports = 1;
1656 DBGC ( phantom, "Phantom %p board type is %#lx (%d ports)\n",
1657 phantom, board_info.board_type, phantom->num_ports );
1663 * Initialise the Phantom command PEG
1665 * @v phantom Phantom NIC
1666 * @ret rc Return status code
1668 static int phantom_init_cmdpeg ( struct phantom_nic *phantom ) {
1671 physaddr_t dummy_dma_phys;
1672 unsigned int retries;
1673 uint32_t cmdpeg_state;
1674 uint32_t last_cmdpeg_state = 0;
1676 /* Check for a previous initialisation. This could have
1677 * happened if, for example, the BIOS used the UNDI API to
1678 * drive the NIC prior to a full PXE boot.
1680 cmdpeg_state = phantom_readl ( phantom, UNM_NIC_REG_CMDPEG_STATE );
1681 if ( cmdpeg_state == UNM_NIC_REG_CMDPEG_STATE_INITIALIZE_ACK ) {
1682 DBGC ( phantom, "Phantom %p command PEG already initialized\n",
1687 /* If this was a cold boot, check that the hardware came up ok */
1688 cold_boot = phantom_readl ( phantom, UNM_CAM_RAM_COLD_BOOT );
1689 if ( cold_boot == UNM_CAM_RAM_COLD_BOOT_MAGIC ) {
1690 DBGC ( phantom, "Phantom %p coming up from cold boot\n",
1692 sw_reset = phantom_readl ( phantom, UNM_ROMUSB_GLB_SW_RESET );
1693 if ( sw_reset != UNM_ROMUSB_GLB_SW_RESET_MAGIC ) {
1694 DBGC ( phantom, "Phantom %p reset failed: %08lx\n",
1695 phantom, sw_reset );
1699 DBGC ( phantom, "Phantom %p coming up from warm boot "
1700 "(%08lx)\n", phantom, cold_boot );
1702 /* Clear cold-boot flag */
1703 phantom_writel ( phantom, 0, UNM_CAM_RAM_COLD_BOOT );
1705 /* Set port modes */
1706 phantom_writel ( phantom, UNM_CAM_RAM_PORT_MODE_AUTO_NEG_1G,
1707 UNM_CAM_RAM_WOL_PORT_MODE );
1709 /* Pass dummy DMA area to card */
1710 dummy_dma_phys = virt_to_bus ( phantom->dma_buf->dummy_dma );
1711 DBGC ( phantom, "Phantom %p dummy DMA at %08lx\n",
1712 phantom, dummy_dma_phys );
1713 phantom_write_hilo ( phantom, dummy_dma_phys,
1714 UNM_NIC_REG_DUMMY_BUF_ADDR_LO,
1715 UNM_NIC_REG_DUMMY_BUF_ADDR_HI );
1716 phantom_writel ( phantom, UNM_NIC_REG_DUMMY_BUF_INIT,
1717 UNM_NIC_REG_DUMMY_BUF );
1719 /* Tell the hardware that tuning is complete */
1720 phantom_writel ( phantom, UNM_ROMUSB_GLB_PEGTUNE_DONE_MAGIC,
1721 UNM_ROMUSB_GLB_PEGTUNE_DONE );
1723 /* Wait for command PEG to finish initialising */
1724 DBGC ( phantom, "Phantom %p initialising command PEG (will take up to "
1725 "%d seconds)...\n", phantom, PHN_CMDPEG_INIT_TIMEOUT_SEC );
1726 for ( retries = 0; retries < PHN_CMDPEG_INIT_TIMEOUT_SEC; retries++ ) {
1727 cmdpeg_state = phantom_readl ( phantom,
1728 UNM_NIC_REG_CMDPEG_STATE );
1729 if ( cmdpeg_state != last_cmdpeg_state ) {
1730 DBGC ( phantom, "Phantom %p command PEG state is "
1731 "%08lx after %d seconds...\n",
1732 phantom, cmdpeg_state, retries );
1733 last_cmdpeg_state = cmdpeg_state;
1735 if ( cmdpeg_state == UNM_NIC_REG_CMDPEG_STATE_INITIALIZED ) {
1736 /* Acknowledge the PEG initialisation */
1737 phantom_writel ( phantom,
1738 UNM_NIC_REG_CMDPEG_STATE_INITIALIZE_ACK,
1739 UNM_NIC_REG_CMDPEG_STATE );
1745 DBGC ( phantom, "Phantom %p timed out waiting for command PEG to "
1746 "initialise (status %08lx)\n", phantom, cmdpeg_state );
1751 * Read Phantom MAC address
1753 * @v phanton_port Phantom NIC port
1754 * @v ll_addr Buffer to fill with MAC address
1756 static void phantom_get_macaddr ( struct phantom_nic_port *phantom_port,
1757 uint8_t *ll_addr ) {
1758 struct phantom_nic *phantom = phantom_port->phantom;
1760 uint8_t mac_addr[2][ETH_ALEN];
1763 unsigned long offset;
1766 /* Read the three dwords that include this MAC address and one other */
1767 offset = ( UNM_CAM_RAM_MAC_ADDRS +
1768 ( 12 * ( phantom_port->port / 2 ) ) );
1769 for ( i = 0 ; i < 3 ; i++, offset += 4 ) {
1770 u.dwords[i] = phantom_readl ( phantom, offset );
1773 /* Copy out the relevant MAC address */
1774 for ( i = 0 ; i < ETH_ALEN ; i++ ) {
1775 ll_addr[ ETH_ALEN - i - 1 ] =
1776 u.mac_addr[ phantom_port->port & 1 ][i];
1778 DBGC ( phantom, "Phantom %p port %d MAC address is %s\n",
1779 phantom, phantom_port->port, eth_ntoa ( ll_addr ) );
1783 * Initialise Phantom receive PEG
1785 * @v phantom Phantom NIC
1786 * @ret rc Return status code
1788 static int phantom_init_rcvpeg ( struct phantom_nic *phantom ) {
1789 unsigned int retries;
1790 uint32_t rcvpeg_state;
1791 uint32_t last_rcvpeg_state = 0;
1793 DBGC ( phantom, "Phantom %p initialising receive PEG (will take up to "
1794 "%d seconds)...\n", phantom, PHN_RCVPEG_INIT_TIMEOUT_SEC );
1795 for ( retries = 0; retries < PHN_RCVPEG_INIT_TIMEOUT_SEC; retries++ ) {
1796 rcvpeg_state = phantom_readl ( phantom,
1797 UNM_NIC_REG_RCVPEG_STATE );
1798 if ( rcvpeg_state != last_rcvpeg_state ) {
1799 DBGC ( phantom, "Phantom %p receive PEG state is "
1800 "%08lx after %d seconds...\n",
1801 phantom, rcvpeg_state, retries );
1802 last_rcvpeg_state = rcvpeg_state;
1804 if ( rcvpeg_state == UNM_NIC_REG_RCVPEG_STATE_INITIALIZED )
1809 DBGC ( phantom, "Phantom %p timed out waiting for receive PEG to "
1810 "initialise (status %08lx)\n", phantom, rcvpeg_state );
1819 * @ret rc Return status code
1821 static int phantom_probe ( struct pci_device *pci,
1822 const struct pci_device_id *id __unused ) {
1823 struct phantom_nic *phantom;
1824 struct net_device *netdev;
1825 struct phantom_nic_port *phantom_port;
1829 /* Phantom NICs expose multiple PCI functions, used for
1830 * virtualisation. Ignore everything except function 0.
1832 if ( PCI_FUNC ( pci->devfn ) != 0 )
1835 /* Allocate Phantom device */
1836 phantom = zalloc ( sizeof ( *phantom ) );
1839 goto err_alloc_phantom;
1841 pci_set_drvdata ( pci, phantom );
1843 /* Fix up PCI device */
1844 adjust_pci_device ( pci );
1847 if ( ( rc = phantom_map_crb ( phantom, pci ) ) != 0 )
1850 /* Read flash information */
1851 if ( ( rc = phantom_read_flash ( phantom ) ) != 0 )
1852 goto err_read_flash;
1854 /* Allocate net devices for each port */
1855 for ( i = 0 ; i < phantom->num_ports ; i++ ) {
1856 netdev = alloc_etherdev ( sizeof ( *phantom_port ) );
1859 goto err_alloc_etherdev;
1861 phantom->netdev[i] = netdev;
1862 netdev_init ( netdev, &phantom_operations );
1863 phantom_port = netdev_priv ( netdev );
1864 netdev->dev = &pci->dev;
1865 phantom_port->phantom = phantom;
1866 phantom_port->port = i;
1869 /* BUG5945 - need to hack PCI config space on P3 B1 silicon.
1870 * B2 will have this fixed; remove this hack when B1 is no
1873 for ( i = 0 ; i < 8 ; i++ ) {
1875 pci->devfn = PCI_DEVFN ( PCI_SLOT ( pci->devfn ), i );
1876 pci_read_config_dword ( pci, 0xc8, &temp );
1877 pci_read_config_dword ( pci, 0xc8, &temp );
1878 pci_write_config_dword ( pci, 0xc8, 0xf1000 );
1880 pci->devfn = PCI_DEVFN ( PCI_SLOT ( pci->devfn ), 0 );
1882 /* Allocate dummy DMA buffer and perform initial hardware handshake */
1883 phantom->dma_buf = malloc_dma ( sizeof ( *(phantom->dma_buf) ),
1884 UNM_DMA_BUFFER_ALIGN );
1885 if ( ! phantom->dma_buf )
1887 if ( ( rc = phantom_init_cmdpeg ( phantom ) ) != 0 )
1888 goto err_init_cmdpeg;
1890 /* Initialise the receive firmware */
1891 if ( ( rc = phantom_init_rcvpeg ( phantom ) ) != 0 )
1892 goto err_init_rcvpeg;
1894 /* Read MAC addresses */
1895 for ( i = 0 ; i < phantom->num_ports ; i++ ) {
1896 phantom_port = netdev_priv ( phantom->netdev[i] );
1897 phantom_get_macaddr ( phantom_port,
1898 phantom->netdev[i]->ll_addr );
1901 /* Register network devices */
1902 for ( i = 0 ; i < phantom->num_ports ; i++ ) {
1903 if ( ( rc = register_netdev ( phantom->netdev[i] ) ) != 0 ) {
1904 DBGC ( phantom, "Phantom %p could not register port "
1905 "%d: %s\n", phantom, i, strerror ( rc ) );
1906 goto err_register_netdev;
1912 i = ( phantom->num_ports - 1 );
1913 err_register_netdev:
1914 for ( ; i >= 0 ; i-- )
1915 unregister_netdev ( phantom->netdev[i] );
1918 free_dma ( phantom->dma_buf, sizeof ( *(phantom->dma_buf) ) );
1919 phantom->dma_buf = NULL;
1921 i = ( phantom->num_ports - 1 );
1923 for ( ; i >= 0 ; i-- ) {
1924 netdev_nullify ( phantom->netdev[i] );
1925 netdev_put ( phantom->netdev[i] );
1939 static void phantom_remove ( struct pci_device *pci ) {
1940 struct phantom_nic *phantom = pci_get_drvdata ( pci );
1943 for ( i = ( phantom->num_ports - 1 ) ; i >= 0 ; i-- )
1944 unregister_netdev ( phantom->netdev[i] );
1945 free_dma ( phantom->dma_buf, sizeof ( *(phantom->dma_buf) ) );
1946 phantom->dma_buf = NULL;
1947 for ( i = ( phantom->num_ports - 1 ) ; i >= 0 ; i-- ) {
1948 netdev_nullify ( phantom->netdev[i] );
1949 netdev_put ( phantom->netdev[i] );
1954 /** Phantom PCI IDs */
1955 static struct pci_device_id phantom_nics[] = {
1956 PCI_ROM ( 0x4040, 0x0100, "nx", "NX" ),
1959 /** Phantom PCI driver */
1960 struct pci_driver phantom_driver __pci_driver = {
1961 .ids = phantom_nics,
1962 .id_count = ( sizeof ( phantom_nics ) / sizeof ( phantom_nics[0] ) ),
1963 .probe = phantom_probe,
1964 .remove = phantom_remove,