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.
29 #include <gpxe/malloc.h>
30 #include <gpxe/iobuf.h>
31 #include <gpxe/netdevice.h>
32 #include <gpxe/if_ether.h>
33 #include <gpxe/ethernet.h>
44 /** Maximum time to wait for SPI lock */
45 #define PHN_SPI_LOCK_TIMEOUT_MS 100
47 /** Maximum time to wait for SPI command to be issued */
48 #define PHN_SPI_CMD_TIMEOUT_MS 100
50 /** Maximum time to wait for command PEG to initialise
54 * The command PEG will currently report initialisation complete only
55 * when at least one PHY has detected a link (so that the global PHY
56 * clock can be set to 10G/1G as appropriate). This can take a very,
59 * A future firmware revision should decouple PHY initialisation from
60 * firmware initialisation, at which point the command PEG will report
61 * initialisation complete much earlier, and this timeout can be
64 #define PHN_CMDPEG_INIT_TIMEOUT_SEC 50
66 /** Maximum time to wait for receive PEG to initialise */
67 #define PHN_RCVPEG_INIT_TIMEOUT_SEC 2
69 /** Maximum time to wait for firmware to accept a command */
70 #define PHN_ISSUE_CMD_TIMEOUT_MS 2000
72 /** Maximum time to wait for test memory */
73 #define PHN_TEST_MEM_TIMEOUT_MS 100
75 /** Link state poll frequency
77 * The link state will be checked once in every N calls to poll().
79 #define PHN_LINK_POLL_FREQUENCY 4096
81 /** Number of RX descriptors */
82 #define PHN_NUM_RDS 32
84 /** RX maximum fill level. Must be strictly less than PHN_NUM_RDS. */
85 #define PHN_RDS_MAX_FILL 16
88 #define PHN_RX_BUFSIZE ( 32 /* max LL padding added by card */ + \
91 /** Number of RX status descriptors */
92 #define PHN_NUM_SDS 32
94 /** Number of TX descriptors */
97 /** A Phantom descriptor ring set */
98 struct phantom_descriptor_rings {
100 struct phantom_rds rds[PHN_NUM_RDS];
101 /** RX status descriptors */
102 struct phantom_sds sds[PHN_NUM_SDS];
103 /** TX descriptors */
104 union phantom_cds cds[PHN_NUM_CDS];
105 /** TX consumer index */
106 volatile uint32_t cmd_cons;
109 /** A Phantom NIC port */
110 struct phantom_nic_port {
111 /** Phantom NIC containing this port */
112 struct phantom_nic *phantom;
118 uint16_t rx_context_id;
119 /** RX descriptor producer CRB offset */
120 unsigned long rds_producer_crb;
121 /** RX status descriptor consumer CRB offset */
122 unsigned long sds_consumer_crb;
124 /** RX producer index */
125 unsigned int rds_producer_idx;
126 /** RX consumer index */
127 unsigned int rds_consumer_idx;
128 /** RX status consumer index */
129 unsigned int sds_consumer_idx;
130 /** RX I/O buffers */
131 struct io_buffer *rds_iobuf[PHN_RDS_MAX_FILL];
135 uint16_t tx_context_id;
136 /** TX descriptor producer CRB offset */
137 unsigned long cds_producer_crb;
139 /** TX producer index */
140 unsigned int cds_producer_idx;
141 /** TX consumer index */
142 unsigned int cds_consumer_idx;
143 /** TX I/O buffers */
144 struct io_buffer *cds_iobuf[PHN_NUM_CDS];
147 /** Link state poll timer */
148 unsigned long link_poll_timer;
151 /** Descriptor rings */
152 struct phantom_descriptor_rings *desc;
155 /** RX context creation request and response buffers */
156 struct phantom_create_rx_ctx_rqrsp {
158 struct nx_hostrq_rx_ctx_s rx_ctx;
159 struct nx_hostrq_rds_ring_s rds;
160 struct nx_hostrq_sds_ring_s sds;
161 } __unm_dma_aligned hostrq;
163 struct nx_cardrsp_rx_ctx_s rx_ctx;
164 struct nx_cardrsp_rds_ring_s rds;
165 struct nx_cardrsp_sds_ring_s sds;
166 } __unm_dma_aligned cardrsp;
169 /** TX context creation request and response buffers */
170 struct phantom_create_tx_ctx_rqrsp {
172 struct nx_hostrq_tx_ctx_s tx_ctx;
173 } __unm_dma_aligned hostrq;
175 struct nx_cardrsp_tx_ctx_s tx_ctx;
176 } __unm_dma_aligned cardrsp;
179 /** A Phantom DMA buffer area */
180 union phantom_dma_buffer {
181 /** Dummy area required for (read-only) self-tests */
182 uint8_t dummy_dma[UNM_DUMMY_DMA_SIZE];
183 /** RX context creation request and response buffers */
184 struct phantom_create_rx_ctx_rqrsp create_rx_ctx;
185 /** TX context creation request and response buffers */
186 struct phantom_create_tx_ctx_rqrsp create_tx_ctx;
193 /** Current CRB window */
194 unsigned long crb_window;
195 /** CRB window access method */
196 unsigned long ( *crb_access ) ( struct phantom_nic *phantom,
199 /** Number of ports */
201 /** Per-port network devices */
202 struct net_device *netdev[UNM_FLASH_NUM_PORTS];
205 union phantom_dma_buffer *dma_buf;
207 /** Flash memory SPI bus */
208 struct spi_bus spi_bus;
209 /** Flash memory SPI device */
210 struct spi_device flash;
212 /** Last known link state */
216 /***************************************************************************
218 * CRB register access
223 * Prepare for access to CRB register via 128MB BAR
225 * @v phantom Phantom NIC
226 * @v reg Register offset within abstract address space
227 * @ret offset Register offset within PCI BAR0
229 static unsigned long phantom_crb_access_128m ( struct phantom_nic *phantom,
230 unsigned long reg ) {
231 static const uint32_t reg_window[] = {
232 [UNM_CRB_BLK_PCIE] = 0x0000000,
233 [UNM_CRB_BLK_CAM] = 0x2000000,
234 [UNM_CRB_BLK_ROMUSB] = 0x2000000,
235 [UNM_CRB_BLK_TEST] = 0x0000000,
237 static const uint32_t reg_bases[] = {
238 [UNM_CRB_BLK_PCIE] = 0x6100000,
239 [UNM_CRB_BLK_CAM] = 0x6200000,
240 [UNM_CRB_BLK_ROMUSB] = 0x7300000,
241 [UNM_CRB_BLK_TEST] = 0x6200000,
243 unsigned int block = UNM_CRB_BLK ( reg );
244 unsigned long offset = UNM_CRB_OFFSET ( reg );
245 uint32_t window = reg_window[block];
246 uint32_t verify_window;
248 if ( phantom->crb_window != window ) {
250 /* Write to the CRB window register */
251 writel ( window, phantom->bar0 + UNM_128M_CRB_WINDOW );
253 /* Ensure that the write has reached the card */
254 verify_window = readl ( phantom->bar0 + UNM_128M_CRB_WINDOW );
255 assert ( verify_window == window );
257 /* Record new window */
258 phantom->crb_window = window;
261 return ( reg_bases[block] + offset );
265 * Prepare for access to CRB register via 32MB BAR
267 * @v phantom Phantom NIC
268 * @v reg Register offset within abstract address space
269 * @ret offset Register offset within PCI BAR0
271 static unsigned long phantom_crb_access_32m ( struct phantom_nic *phantom,
272 unsigned long reg ) {
273 static const uint32_t reg_window[] = {
274 [UNM_CRB_BLK_PCIE] = 0x0000000,
275 [UNM_CRB_BLK_CAM] = 0x2000000,
276 [UNM_CRB_BLK_ROMUSB] = 0x2000000,
277 [UNM_CRB_BLK_TEST] = 0x0000000,
279 static const uint32_t reg_bases[] = {
280 [UNM_CRB_BLK_PCIE] = 0x0100000,
281 [UNM_CRB_BLK_CAM] = 0x0200000,
282 [UNM_CRB_BLK_ROMUSB] = 0x1300000,
283 [UNM_CRB_BLK_TEST] = 0x0200000,
285 unsigned int block = UNM_CRB_BLK ( reg );
286 unsigned long offset = UNM_CRB_OFFSET ( reg );
287 uint32_t window = reg_window[block];
288 uint32_t verify_window;
290 if ( phantom->crb_window != window ) {
292 /* Write to the CRB window register */
293 writel ( window, phantom->bar0 + UNM_32M_CRB_WINDOW );
295 /* Ensure that the write has reached the card */
296 verify_window = readl ( phantom->bar0 + UNM_32M_CRB_WINDOW );
297 assert ( verify_window == window );
299 /* Record new window */
300 phantom->crb_window = window;
303 return ( reg_bases[block] + offset );
307 * Prepare for access to CRB register via 2MB BAR
309 * @v phantom Phantom NIC
310 * @v reg Register offset within abstract address space
311 * @ret offset Register offset within PCI BAR0
313 static unsigned long phantom_crb_access_2m ( struct phantom_nic *phantom,
314 unsigned long reg ) {
315 static const uint32_t reg_window_hi[] = {
316 [UNM_CRB_BLK_PCIE] = 0x77300000,
317 [UNM_CRB_BLK_CAM] = 0x41600000,
318 [UNM_CRB_BLK_ROMUSB] = 0x42100000,
319 [UNM_CRB_BLK_TEST] = 0x29500000,
321 unsigned int block = UNM_CRB_BLK ( reg );
322 unsigned long offset = UNM_CRB_OFFSET ( reg );
323 uint32_t window = ( reg_window_hi[block] | ( offset & 0x000f0000 ) );
324 uint32_t verify_window;
326 if ( phantom->crb_window != window ) {
328 /* Write to the CRB window register */
329 writel ( window, phantom->bar0 + UNM_2M_CRB_WINDOW );
331 /* Ensure that the write has reached the card */
332 verify_window = readl ( phantom->bar0 + UNM_2M_CRB_WINDOW );
333 assert ( verify_window == window );
335 /* Record new window */
336 phantom->crb_window = window;
339 return ( 0x1e0000 + ( offset & 0xffff ) );
343 * Read from Phantom CRB register
345 * @v phantom Phantom NIC
346 * @v reg Register offset within abstract address space
347 * @ret value Register value
349 static uint32_t phantom_readl ( struct phantom_nic *phantom,
350 unsigned long reg ) {
351 unsigned long offset;
353 offset = phantom->crb_access ( phantom, reg );
354 return readl ( phantom->bar0 + offset );
358 * Write to Phantom CRB register
360 * @v phantom Phantom NIC
361 * @v value Register value
362 * @v reg Register offset within abstract address space
364 static void phantom_writel ( struct phantom_nic *phantom, uint32_t value,
365 unsigned long reg ) {
366 unsigned long offset;
368 offset = phantom->crb_access ( phantom, reg );
369 writel ( value, phantom->bar0 + offset );
373 * Write to Phantom CRB HI/LO register pair
375 * @v phantom Phantom NIC
376 * @v value Register value
377 * @v lo_offset LO register offset within CRB
378 * @v hi_offset HI register offset within CRB
380 static inline void phantom_write_hilo ( struct phantom_nic *phantom,
382 unsigned long lo_offset,
383 unsigned long hi_offset ) {
384 uint32_t lo = ( value & 0xffffffffUL );
385 uint32_t hi = ( value >> 32 );
387 phantom_writel ( phantom, lo, lo_offset );
388 phantom_writel ( phantom, hi, hi_offset );
391 /***************************************************************************
393 * Firmware message buffer access (for debug)
398 * Read from Phantom test memory
400 * @v phantom Phantom NIC
401 * @v offset Offset within test memory
402 * @v buf 8-byte buffer to fill
403 * @ret rc Return status code
405 static int phantom_read_test_mem ( struct phantom_nic *phantom,
406 uint64_t offset, uint32_t buf[2] ) {
407 unsigned int retries;
408 uint32_t test_control;
410 phantom_write_hilo ( phantom, offset, UNM_TEST_ADDR_LO,
412 phantom_writel ( phantom, UNM_TEST_CONTROL_ENABLE, UNM_TEST_CONTROL );
413 phantom_writel ( phantom,
414 ( UNM_TEST_CONTROL_ENABLE | UNM_TEST_CONTROL_START ),
417 for ( retries = 0 ; retries < PHN_TEST_MEM_TIMEOUT_MS ; retries++ ) {
418 test_control = phantom_readl ( phantom, UNM_TEST_CONTROL );
419 if ( ( test_control & UNM_TEST_CONTROL_BUSY ) == 0 ) {
420 buf[0] = phantom_readl ( phantom, UNM_TEST_RDDATA_LO );
421 buf[1] = phantom_readl ( phantom, UNM_TEST_RDDATA_HI );
427 DBGC ( phantom, "Phantom %p timed out waiting for test memory\n",
433 * Dump Phantom firmware dmesg log
435 * @v phantom Phantom NIC
438 static void phantom_dmesg ( struct phantom_nic *phantom, unsigned int log ) {
451 /* Optimise out for non-debug builds */
455 head = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_HEAD ( log ) );
456 len = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_LEN ( log ) );
457 tail = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_TAIL ( log ) );
458 sig = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_SIG ( log ) );
459 DBGC ( phantom, "Phantom %p firmware dmesg buffer %d (%08lx-%08lx)\n",
460 phantom, log, head, tail );
461 assert ( ( head & 0x07 ) == 0 );
462 if ( sig != UNM_CAM_RAM_DMESG_SIG_MAGIC ) {
463 DBGC ( phantom, "Warning: bad signature %08lx (want %08lx)\n",
464 sig, UNM_CAM_RAM_DMESG_SIG_MAGIC );
467 for ( offset = head ; offset < tail ; offset += 8 ) {
468 if ( ( rc = phantom_read_test_mem ( phantom, offset,
469 buf.dwords ) ) != 0 ) {
470 DBGC ( phantom, "Phantom %p could not read from test "
471 "memory: %s\n", phantom, strerror ( rc ) );
474 for ( i = 0 ; ( ( i < sizeof ( buf ) ) &&
475 ( offset + i ) < tail ) ; i++ ) {
476 DBG ( "%c", buf.bytes[i] );
483 * Dump Phantom firmware dmesg logs
485 * @v phantom Phantom NIC
487 static void __attribute__ (( unused ))
488 phantom_dmesg_all ( struct phantom_nic *phantom ) {
491 for ( i = 0 ; i < UNM_CAM_RAM_NUM_DMESG_BUFFERS ; i++ )
492 phantom_dmesg ( phantom, i );
495 /***************************************************************************
497 * SPI bus access (for flash memory)
502 * Acquire Phantom SPI lock
504 * @v phantom Phantom NIC
505 * @ret rc Return status code
507 static int phantom_spi_lock ( struct phantom_nic *phantom ) {
508 unsigned int retries;
509 uint32_t pcie_sem2_lock;
511 for ( retries = 0 ; retries < PHN_SPI_LOCK_TIMEOUT_MS ; retries++ ) {
512 pcie_sem2_lock = phantom_readl ( phantom, UNM_PCIE_SEM2_LOCK );
513 if ( pcie_sem2_lock != 0 )
518 DBGC ( phantom, "Phantom %p timed out waiting for SPI lock\n",
524 * Wait for Phantom SPI command to complete
526 * @v phantom Phantom NIC
527 * @ret rc Return status code
529 static int phantom_spi_wait ( struct phantom_nic *phantom ) {
530 unsigned int retries;
533 for ( retries = 0 ; retries < PHN_SPI_CMD_TIMEOUT_MS ; retries++ ) {
534 glb_status = phantom_readl ( phantom, UNM_ROMUSB_GLB_STATUS );
535 if ( glb_status & UNM_ROMUSB_GLB_STATUS_ROM_DONE )
540 DBGC ( phantom, "Phantom %p timed out waiting for SPI command\n",
546 * Release Phantom SPI lock
548 * @v phantom Phantom NIC
550 static void phantom_spi_unlock ( struct phantom_nic *phantom ) {
551 phantom_readl ( phantom, UNM_PCIE_SEM2_UNLOCK );
555 * Read/write data via Phantom SPI bus
558 * @v device SPI device
560 * @v address Address to read/write (<0 for no address)
561 * @v data_out TX data buffer (or NULL)
562 * @v data_in RX data buffer (or NULL)
563 * @v len Length of data buffer(s)
564 * @ret rc Return status code
566 static int phantom_spi_rw ( struct spi_bus *bus,
567 struct spi_device *device,
568 unsigned int command, int address,
569 const void *data_out, void *data_in,
571 struct phantom_nic *phantom =
572 container_of ( bus, struct phantom_nic, spi_bus );
576 DBGCP ( phantom, "Phantom %p SPI command %x at %x+%zx\n",
577 phantom, command, address, len );
579 DBGCP_HDA ( phantom, address, data_out, len );
581 /* We support only exactly 4-byte reads */
582 if ( len != UNM_SPI_BLKSIZE ) {
583 DBGC ( phantom, "Phantom %p invalid SPI length %zx\n",
588 /* Acquire SPI lock */
589 if ( ( rc = phantom_spi_lock ( phantom ) ) != 0 )
592 /* Issue SPI command as per the PRM */
594 memcpy ( &data, data_out, sizeof ( data ) );
595 phantom_writel ( phantom, data, UNM_ROMUSB_ROM_WDATA );
597 phantom_writel ( phantom, address, UNM_ROMUSB_ROM_ADDRESS );
598 phantom_writel ( phantom, ( device->address_len / 8 ),
599 UNM_ROMUSB_ROM_ABYTE_CNT );
600 udelay ( 100 ); /* according to PRM */
601 phantom_writel ( phantom, 0, UNM_ROMUSB_ROM_DUMMY_BYTE_CNT );
602 phantom_writel ( phantom, command, UNM_ROMUSB_ROM_INSTR_OPCODE );
604 /* Wait for SPI command to complete */
605 if ( ( rc = phantom_spi_wait ( phantom ) ) != 0 )
608 /* Reset address byte count and dummy byte count, because the
611 phantom_writel ( phantom, 0, UNM_ROMUSB_ROM_ABYTE_CNT );
612 udelay ( 100 ); /* according to PRM */
613 phantom_writel ( phantom, 0, UNM_ROMUSB_ROM_DUMMY_BYTE_CNT );
615 /* Read data, if applicable */
617 data = phantom_readl ( phantom, UNM_ROMUSB_ROM_RDATA );
618 memcpy ( data_in, &data, sizeof ( data ) );
619 DBGCP_HDA ( phantom, address, data_in, len );
623 phantom_spi_unlock ( phantom );
628 /***************************************************************************
635 * Wait for firmware to accept command
637 * @v phantom Phantom NIC
638 * @ret rc Return status code
640 static int phantom_wait_for_cmd ( struct phantom_nic *phantom ) {
641 unsigned int retries;
644 for ( retries = 0 ; retries < PHN_ISSUE_CMD_TIMEOUT_MS ; retries++ ) {
646 cdrp = phantom_readl ( phantom, UNM_NIC_REG_NX_CDRP );
647 if ( NX_CDRP_IS_RSP ( cdrp ) ) {
648 switch ( NX_CDRP_FORM_RSP ( cdrp ) ) {
651 case NX_CDRP_RSP_FAIL:
653 case NX_CDRP_RSP_TIMEOUT:
661 DBGC ( phantom, "Phantom %p timed out waiting for firmware to accept "
662 "command\n", phantom );
667 * Issue command to firmware
669 * @v phantom_port Phantom NIC port
670 * @v command Firmware command
674 * @ret rc Return status code
676 static int phantom_issue_cmd ( struct phantom_nic_port *phantom_port,
677 uint32_t command, uint32_t arg1, uint32_t arg2,
679 struct phantom_nic *phantom = phantom_port->phantom;
684 signature = NX_CDRP_SIGNATURE_MAKE ( phantom_port->port,
686 DBGC2 ( phantom, "Phantom %p port %d issuing command %08lx (%08lx, "
687 "%08lx, %08lx)\n", phantom, phantom_port->port,
688 command, arg1, arg2, arg3 );
689 phantom_writel ( phantom, signature, UNM_NIC_REG_NX_SIGN );
690 phantom_writel ( phantom, arg1, UNM_NIC_REG_NX_ARG1 );
691 phantom_writel ( phantom, arg2, UNM_NIC_REG_NX_ARG2 );
692 phantom_writel ( phantom, arg3, UNM_NIC_REG_NX_ARG3 );
693 phantom_writel ( phantom, NX_CDRP_FORM_CMD ( command ),
694 UNM_NIC_REG_NX_CDRP );
696 /* Wait for command to be accepted */
697 if ( ( rc = phantom_wait_for_cmd ( phantom ) ) != 0 ) {
698 DBGC ( phantom, "Phantom %p could not issue command: %s\n",
699 phantom, strerror ( rc ) );
707 * Issue buffer-format command to firmware
709 * @v phantom_port Phantom NIC port
710 * @v command Firmware command
711 * @v buffer Buffer to pass to firmware
712 * @v len Length of buffer
713 * @ret rc Return status code
715 static int phantom_issue_buf_cmd ( struct phantom_nic_port *phantom_port,
716 uint32_t command, void *buffer,
720 physaddr = virt_to_bus ( buffer );
721 return phantom_issue_cmd ( phantom_port, command, ( physaddr >> 32 ),
722 ( physaddr & 0xffffffffUL ), len );
726 * Create Phantom RX context
728 * @v phantom_port Phantom NIC port
729 * @ret rc Return status code
731 static int phantom_create_rx_ctx ( struct phantom_nic_port *phantom_port ) {
732 struct phantom_nic *phantom = phantom_port->phantom;
733 struct phantom_create_rx_ctx_rqrsp *buf;
736 /* Prepare request */
737 buf = &phantom->dma_buf->create_rx_ctx;
738 memset ( buf, 0, sizeof ( *buf ) );
739 buf->hostrq.rx_ctx.host_rsp_dma_addr =
740 cpu_to_le64 ( virt_to_bus ( &buf->cardrsp ) );
741 buf->hostrq.rx_ctx.capabilities[0] =
742 cpu_to_le32 ( NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN );
743 buf->hostrq.rx_ctx.host_int_crb_mode =
744 cpu_to_le32 ( NX_HOST_INT_CRB_MODE_SHARED );
745 buf->hostrq.rx_ctx.host_rds_crb_mode =
746 cpu_to_le32 ( NX_HOST_RDS_CRB_MODE_UNIQUE );
747 buf->hostrq.rx_ctx.rds_ring_offset = cpu_to_le32 ( 0 );
748 buf->hostrq.rx_ctx.sds_ring_offset =
749 cpu_to_le32 ( sizeof ( buf->hostrq.rds ) );
750 buf->hostrq.rx_ctx.num_rds_rings = cpu_to_le16 ( 1 );
751 buf->hostrq.rx_ctx.num_sds_rings = cpu_to_le16 ( 1 );
752 buf->hostrq.rds.host_phys_addr =
753 cpu_to_le64 ( virt_to_bus ( phantom_port->desc->rds ) );
754 buf->hostrq.rds.buff_size = cpu_to_le64 ( PHN_RX_BUFSIZE );
755 buf->hostrq.rds.ring_size = cpu_to_le32 ( PHN_NUM_RDS );
756 buf->hostrq.rds.ring_kind = cpu_to_le32 ( NX_RDS_RING_TYPE_NORMAL );
757 buf->hostrq.sds.host_phys_addr =
758 cpu_to_le64 ( virt_to_bus ( phantom_port->desc->sds ) );
759 buf->hostrq.sds.ring_size = cpu_to_le32 ( PHN_NUM_SDS );
761 DBGC ( phantom, "Phantom %p port %d creating RX context\n",
762 phantom, phantom_port->port );
763 DBGC2_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
764 &buf->hostrq, sizeof ( buf->hostrq ) );
767 if ( ( rc = phantom_issue_buf_cmd ( phantom_port,
768 NX_CDRP_CMD_CREATE_RX_CTX,
770 sizeof ( buf->hostrq ) ) ) != 0 ) {
771 DBGC ( phantom, "Phantom %p port %d could not create RX "
773 phantom, phantom_port->port, strerror ( rc ) );
774 DBGC ( phantom, "Request:\n" );
775 DBGC_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
776 &buf->hostrq, sizeof ( buf->hostrq ) );
777 DBGC ( phantom, "Response:\n" );
778 DBGC_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
779 &buf->cardrsp, sizeof ( buf->cardrsp ) );
783 /* Retrieve context parameters */
784 phantom_port->rx_context_id =
785 le16_to_cpu ( buf->cardrsp.rx_ctx.context_id );
786 phantom_port->rds_producer_crb =
788 le32_to_cpu ( buf->cardrsp.rds.host_producer_crb ));
789 phantom_port->sds_consumer_crb =
791 le32_to_cpu ( buf->cardrsp.sds.host_consumer_crb ));
793 DBGC ( phantom, "Phantom %p port %d created RX context (id %04x, "
794 "port phys %02x virt %02x)\n", phantom, phantom_port->port,
795 phantom_port->rx_context_id, buf->cardrsp.rx_ctx.phys_port,
796 buf->cardrsp.rx_ctx.virt_port );
797 DBGC2_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
798 &buf->cardrsp, sizeof ( buf->cardrsp ) );
799 DBGC ( phantom, "Phantom %p port %d RDS producer CRB is %08lx\n",
800 phantom, phantom_port->port, phantom_port->rds_producer_crb );
801 DBGC ( phantom, "Phantom %p port %d SDS consumer CRB is %08lx\n",
802 phantom, phantom_port->port, phantom_port->sds_consumer_crb );
808 * Destroy Phantom RX context
810 * @v phantom_port Phantom NIC port
811 * @ret rc Return status code
813 static void phantom_destroy_rx_ctx ( struct phantom_nic_port *phantom_port ) {
814 struct phantom_nic *phantom = phantom_port->phantom;
817 DBGC ( phantom, "Phantom %p port %d destroying RX context (id %04x)\n",
818 phantom, phantom_port->port, phantom_port->rx_context_id );
821 if ( ( rc = phantom_issue_cmd ( phantom_port,
822 NX_CDRP_CMD_DESTROY_RX_CTX,
823 phantom_port->rx_context_id,
824 NX_DESTROY_CTX_RESET, 0 ) ) != 0 ) {
825 DBGC ( phantom, "Phantom %p port %d could not destroy RX "
827 phantom, phantom_port->port, strerror ( rc ) );
828 /* We're probably screwed */
832 /* Clear context parameters */
833 phantom_port->rx_context_id = 0;
834 phantom_port->rds_producer_crb = 0;
835 phantom_port->sds_consumer_crb = 0;
837 /* Reset software counters */
838 phantom_port->rds_producer_idx = 0;
839 phantom_port->rds_consumer_idx = 0;
840 phantom_port->sds_consumer_idx = 0;
844 * Create Phantom TX context
846 * @v phantom_port Phantom NIC port
847 * @ret rc Return status code
849 static int phantom_create_tx_ctx ( struct phantom_nic_port *phantom_port ) {
850 struct phantom_nic *phantom = phantom_port->phantom;
851 struct phantom_create_tx_ctx_rqrsp *buf;
854 /* Prepare request */
855 buf = &phantom->dma_buf->create_tx_ctx;
856 memset ( buf, 0, sizeof ( *buf ) );
857 buf->hostrq.tx_ctx.host_rsp_dma_addr =
858 cpu_to_le64 ( virt_to_bus ( &buf->cardrsp ) );
859 buf->hostrq.tx_ctx.cmd_cons_dma_addr =
860 cpu_to_le64 ( virt_to_bus ( &phantom_port->desc->cmd_cons ) );
861 buf->hostrq.tx_ctx.dummy_dma_addr =
862 cpu_to_le64 ( virt_to_bus ( phantom->dma_buf->dummy_dma ) );
863 buf->hostrq.tx_ctx.capabilities[0] =
864 cpu_to_le32 ( NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN );
865 buf->hostrq.tx_ctx.host_int_crb_mode =
866 cpu_to_le32 ( NX_HOST_INT_CRB_MODE_SHARED );
867 buf->hostrq.tx_ctx.cds_ring.host_phys_addr =
868 cpu_to_le64 ( virt_to_bus ( phantom_port->desc->cds ) );
869 buf->hostrq.tx_ctx.cds_ring.ring_size = cpu_to_le32 ( PHN_NUM_CDS );
871 DBGC ( phantom, "Phantom %p port %d creating TX context\n",
872 phantom, phantom_port->port );
873 DBGC2_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
874 &buf->hostrq, sizeof ( buf->hostrq ) );
877 if ( ( rc = phantom_issue_buf_cmd ( phantom_port,
878 NX_CDRP_CMD_CREATE_TX_CTX,
880 sizeof ( buf->hostrq ) ) ) != 0 ) {
881 DBGC ( phantom, "Phantom %p port %d could not create TX "
883 phantom, phantom_port->port, strerror ( rc ) );
884 DBGC ( phantom, "Request:\n" );
885 DBGC_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
886 &buf->hostrq, sizeof ( buf->hostrq ) );
887 DBGC ( phantom, "Response:\n" );
888 DBGC_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
889 &buf->cardrsp, sizeof ( buf->cardrsp ) );
893 /* Retrieve context parameters */
894 phantom_port->tx_context_id =
895 le16_to_cpu ( buf->cardrsp.tx_ctx.context_id );
896 phantom_port->cds_producer_crb =
898 le32_to_cpu(buf->cardrsp.tx_ctx.cds_ring.host_producer_crb));
900 DBGC ( phantom, "Phantom %p port %d created TX context (id %04x, "
901 "port phys %02x virt %02x)\n", phantom, phantom_port->port,
902 phantom_port->tx_context_id, buf->cardrsp.tx_ctx.phys_port,
903 buf->cardrsp.tx_ctx.virt_port );
904 DBGC2_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
905 &buf->cardrsp, sizeof ( buf->cardrsp ) );
906 DBGC ( phantom, "Phantom %p port %d CDS producer CRB is %08lx\n",
907 phantom, phantom_port->port, phantom_port->cds_producer_crb );
913 * Destroy Phantom TX context
915 * @v phantom_port Phantom NIC port
916 * @ret rc Return status code
918 static void phantom_destroy_tx_ctx ( struct phantom_nic_port *phantom_port ) {
919 struct phantom_nic *phantom = phantom_port->phantom;
922 DBGC ( phantom, "Phantom %p port %d destroying TX context (id %04x)\n",
923 phantom, phantom_port->port, phantom_port->tx_context_id );
926 if ( ( rc = phantom_issue_cmd ( phantom_port,
927 NX_CDRP_CMD_DESTROY_TX_CTX,
928 phantom_port->tx_context_id,
929 NX_DESTROY_CTX_RESET, 0 ) ) != 0 ) {
930 DBGC ( phantom, "Phantom %p port %d could not destroy TX "
932 phantom, phantom_port->port, strerror ( rc ) );
933 /* We're probably screwed */
937 /* Clear context parameters */
938 phantom_port->tx_context_id = 0;
939 phantom_port->cds_producer_crb = 0;
941 /* Reset software counters */
942 phantom_port->cds_producer_idx = 0;
943 phantom_port->cds_consumer_idx = 0;
946 /***************************************************************************
948 * Descriptor ring management
953 * Allocate Phantom RX descriptor
955 * @v phantom_port Phantom NIC port
956 * @ret index RX descriptor index, or negative error
958 static int phantom_alloc_rds ( struct phantom_nic_port *phantom_port ) {
959 struct phantom_nic *phantom = phantom_port->phantom;
960 unsigned int rds_producer_idx;
961 unsigned int next_rds_producer_idx;
963 /* Check for space in the ring. RX descriptors are consumed
964 * out of order, but they are *read* by the hardware in strict
965 * order. We maintain a pessimistic consumer index, which is
966 * guaranteed never to be an overestimate of the number of
967 * descriptors read by the hardware.
969 rds_producer_idx = phantom_port->rds_producer_idx;
970 next_rds_producer_idx = ( ( rds_producer_idx + 1 ) % PHN_NUM_RDS );
971 if ( next_rds_producer_idx == phantom_port->rds_consumer_idx ) {
972 DBGC ( phantom, "Phantom %p port %d RDS ring full (index %d "
973 "not consumed)\n", phantom, phantom_port->port,
974 next_rds_producer_idx );
978 return rds_producer_idx;
982 * Post Phantom RX descriptor
984 * @v phantom_port Phantom NIC port
985 * @v rds RX descriptor
987 static void phantom_post_rds ( struct phantom_nic_port *phantom_port,
988 struct phantom_rds *rds ) {
989 struct phantom_nic *phantom = phantom_port->phantom;
990 unsigned int rds_producer_idx;
991 unsigned int next_rds_producer_idx;
992 struct phantom_rds *entry;
994 /* Copy descriptor to ring */
995 rds_producer_idx = phantom_port->rds_producer_idx;
996 entry = &phantom_port->desc->rds[rds_producer_idx];
997 memcpy ( entry, rds, sizeof ( *entry ) );
998 DBGC2 ( phantom, "Phantom %p port %d posting RDS %ld (slot %d):\n",
999 phantom, phantom_port->port, NX_GET ( rds, handle ),
1001 DBGC2_HDA ( phantom, virt_to_bus ( entry ), entry, sizeof ( *entry ) );
1003 /* Update producer index */
1004 next_rds_producer_idx = ( ( rds_producer_idx + 1 ) % PHN_NUM_RDS );
1005 phantom_port->rds_producer_idx = next_rds_producer_idx;
1007 phantom_writel ( phantom, phantom_port->rds_producer_idx,
1008 phantom_port->rds_producer_crb );
1012 * Allocate Phantom TX descriptor
1014 * @v phantom_port Phantom NIC port
1015 * @ret index TX descriptor index, or negative error
1017 static int phantom_alloc_cds ( struct phantom_nic_port *phantom_port ) {
1018 struct phantom_nic *phantom = phantom_port->phantom;
1019 unsigned int cds_producer_idx;
1020 unsigned int next_cds_producer_idx;
1022 /* Check for space in the ring. TX descriptors are consumed
1023 * in strict order, so we just check for a collision against
1024 * the consumer index.
1026 cds_producer_idx = phantom_port->cds_producer_idx;
1027 next_cds_producer_idx = ( ( cds_producer_idx + 1 ) % PHN_NUM_CDS );
1028 if ( next_cds_producer_idx == phantom_port->cds_consumer_idx ) {
1029 DBGC ( phantom, "Phantom %p port %d CDS ring full (index %d "
1030 "not consumed)\n", phantom, phantom_port->port,
1031 next_cds_producer_idx );
1035 return cds_producer_idx;
1039 * Post Phantom TX descriptor
1041 * @v phantom_port Phantom NIC port
1042 * @v cds TX descriptor
1044 static void phantom_post_cds ( struct phantom_nic_port *phantom_port,
1045 union phantom_cds *cds ) {
1046 struct phantom_nic *phantom = phantom_port->phantom;
1047 unsigned int cds_producer_idx;
1048 unsigned int next_cds_producer_idx;
1049 union phantom_cds *entry;
1051 /* Copy descriptor to ring */
1052 cds_producer_idx = phantom_port->cds_producer_idx;
1053 entry = &phantom_port->desc->cds[cds_producer_idx];
1054 memcpy ( entry, cds, sizeof ( *entry ) );
1055 DBGC2 ( phantom, "Phantom %p port %d posting CDS %d:\n",
1056 phantom, phantom_port->port, cds_producer_idx );
1057 DBGC2_HDA ( phantom, virt_to_bus ( entry ), entry, sizeof ( *entry ) );
1059 /* Update producer index */
1060 next_cds_producer_idx = ( ( cds_producer_idx + 1 ) % PHN_NUM_CDS );
1061 phantom_port->cds_producer_idx = next_cds_producer_idx;
1063 phantom_writel ( phantom, phantom_port->cds_producer_idx,
1064 phantom_port->cds_producer_crb );
1067 /***************************************************************************
1069 * MAC address management
1074 * Add/remove MAC address
1076 * @v phantom_port Phantom NIC port
1077 * @v ll_addr MAC address to add or remove
1078 * @v opcode MAC request opcode
1079 * @ret rc Return status code
1081 static int phantom_update_macaddr ( struct phantom_nic_port *phantom_port,
1082 const uint8_t *ll_addr,
1083 unsigned int opcode ) {
1084 union phantom_cds cds;
1087 /* Get descriptor ring entry */
1088 index = phantom_alloc_cds ( phantom_port );
1092 /* Fill descriptor ring entry */
1093 memset ( &cds, 0, sizeof ( cds ) );
1094 NX_FILL_1 ( &cds, 0,
1095 nic_request.common.opcode, UNM_NIC_REQUEST );
1096 NX_FILL_2 ( &cds, 1,
1097 nic_request.header.opcode, UNM_MAC_EVENT,
1098 nic_request.header.context_id, phantom_port->port );
1099 NX_FILL_7 ( &cds, 2,
1100 nic_request.body.mac_request.opcode, opcode,
1101 nic_request.body.mac_request.mac_addr_0, ll_addr[0],
1102 nic_request.body.mac_request.mac_addr_1, ll_addr[1],
1103 nic_request.body.mac_request.mac_addr_2, ll_addr[2],
1104 nic_request.body.mac_request.mac_addr_3, ll_addr[3],
1105 nic_request.body.mac_request.mac_addr_4, ll_addr[4],
1106 nic_request.body.mac_request.mac_addr_5, ll_addr[5] );
1108 /* Post descriptor */
1109 phantom_post_cds ( phantom_port, &cds );
1117 * @v phantom_port Phantom NIC port
1118 * @v ll_addr MAC address to add or remove
1119 * @ret rc Return status code
1121 static inline int phantom_add_macaddr ( struct phantom_nic_port *phantom_port,
1122 const uint8_t *ll_addr ) {
1123 struct phantom_nic *phantom = phantom_port->phantom;
1125 DBGC ( phantom, "Phantom %p port %d adding MAC address %s\n",
1126 phantom, phantom_port->port, eth_ntoa ( ll_addr ) );
1128 return phantom_update_macaddr ( phantom_port, ll_addr, UNM_MAC_ADD );
1132 * Remove MAC address
1134 * @v phantom_port Phantom NIC port
1135 * @v ll_addr MAC address to add or remove
1136 * @ret rc Return status code
1138 static inline int phantom_del_macaddr ( struct phantom_nic_port *phantom_port,
1139 const uint8_t *ll_addr ) {
1140 struct phantom_nic *phantom = phantom_port->phantom;
1142 DBGC ( phantom, "Phantom %p port %d removing MAC address %s\n",
1143 phantom, phantom_port->port, eth_ntoa ( ll_addr ) );
1145 return phantom_update_macaddr ( phantom_port, ll_addr, UNM_MAC_DEL );
1148 /***************************************************************************
1150 * Link state detection
1157 * @v phantom Phantom NIC
1159 static void phantom_poll_link_state ( struct phantom_nic *phantom ) {
1160 struct net_device *netdev;
1161 struct phantom_nic_port *phantom_port;
1162 uint32_t xg_state_p3;
1166 /* Read link state */
1167 xg_state_p3 = phantom_readl ( phantom, UNM_NIC_REG_XG_STATE_P3 );
1169 /* If there is no change, do nothing */
1170 if ( phantom->link_state == xg_state_p3 )
1173 /* Record new link state */
1174 DBGC ( phantom, "Phantom %p new link state %08lx (was %08lx)\n",
1175 phantom, xg_state_p3, phantom->link_state );
1176 phantom->link_state = xg_state_p3;
1178 /* Indicate per-port link state to gPXE */
1179 for ( i = 0 ; i < phantom->num_ports ; i++ ) {
1180 netdev = phantom->netdev[i];
1181 phantom_port = netdev_priv ( netdev );
1182 link = UNM_NIC_REG_XG_STATE_P3_LINK ( phantom_port->port,
1183 phantom->link_state );
1185 case UNM_NIC_REG_XG_STATE_P3_LINK_UP:
1186 DBGC ( phantom, "Phantom %p port %d link is up\n",
1187 phantom, phantom_port->port );
1188 netdev_link_up ( netdev );
1190 case UNM_NIC_REG_XG_STATE_P3_LINK_DOWN:
1191 DBGC ( phantom, "Phantom %p port %d link is down\n",
1192 phantom, phantom_port->port );
1193 netdev_link_down ( netdev );
1196 DBGC ( phantom, "Phantom %p port %d bad link state "
1197 "%d\n", phantom, phantom_port->port, link );
1203 /***************************************************************************
1210 * Refill descriptor ring
1212 * @v netdev Net device
1214 static void phantom_refill_rx_ring ( struct net_device *netdev ) {
1215 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1216 struct io_buffer *iobuf;
1217 struct phantom_rds rds;
1218 unsigned int handle;
1221 for ( handle = 0 ; handle < PHN_RDS_MAX_FILL ; handle++ ) {
1223 /* Skip this index if the descriptor has not yet been
1226 if ( phantom_port->rds_iobuf[handle] != NULL )
1229 /* Allocate descriptor ring entry */
1230 index = phantom_alloc_rds ( phantom_port );
1231 assert ( PHN_RDS_MAX_FILL < PHN_NUM_RDS );
1232 assert ( index >= 0 ); /* Guaranteed by MAX_FILL < NUM_RDS ) */
1234 /* Try to allocate an I/O buffer */
1235 iobuf = alloc_iob ( PHN_RX_BUFSIZE );
1237 /* Failure is non-fatal; we will retry later */
1238 netdev_rx_err ( netdev, NULL, -ENOMEM );
1242 /* Fill descriptor ring entry */
1243 memset ( &rds, 0, sizeof ( rds ) );
1244 NX_FILL_2 ( &rds, 0,
1246 length, iob_len ( iobuf ) );
1247 NX_FILL_1 ( &rds, 1,
1248 dma_addr, virt_to_bus ( iobuf->data ) );
1250 /* Record I/O buffer */
1251 assert ( phantom_port->rds_iobuf[handle] == NULL );
1252 phantom_port->rds_iobuf[handle] = iobuf;
1254 /* Post descriptor */
1255 phantom_post_rds ( phantom_port, &rds );
1262 * @v netdev Net device
1263 * @ret rc Return status code
1265 static int phantom_open ( struct net_device *netdev ) {
1266 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1269 /* Allocate and zero descriptor rings */
1270 phantom_port->desc = malloc_dma ( sizeof ( *(phantom_port->desc) ),
1271 UNM_DMA_BUFFER_ALIGN );
1272 if ( ! phantom_port->desc ) {
1274 goto err_alloc_desc;
1276 memset ( phantom_port->desc, 0, sizeof ( *(phantom_port->desc) ) );
1278 /* Create RX context */
1279 if ( ( rc = phantom_create_rx_ctx ( phantom_port ) ) != 0 )
1280 goto err_create_rx_ctx;
1282 /* Create TX context */
1283 if ( ( rc = phantom_create_tx_ctx ( phantom_port ) ) != 0 )
1284 goto err_create_tx_ctx;
1286 /* Fill the RX descriptor ring */
1287 phantom_refill_rx_ring ( netdev );
1289 /* Add MAC addresses
1293 * We would like to be able to enable receiving all multicast
1294 * packets (or, failing that, promiscuous mode), but the
1295 * firmware doesn't currently support this.
1297 if ( ( rc = phantom_add_macaddr ( phantom_port,
1298 netdev->ll_protocol->ll_broadcast ) ) != 0 )
1299 goto err_add_macaddr_broadcast;
1300 if ( ( rc = phantom_add_macaddr ( phantom_port,
1301 netdev->ll_addr ) ) != 0 )
1302 goto err_add_macaddr_unicast;
1306 phantom_del_macaddr ( phantom_port, netdev->ll_addr );
1307 err_add_macaddr_unicast:
1308 phantom_del_macaddr ( phantom_port,
1309 netdev->ll_protocol->ll_broadcast );
1310 err_add_macaddr_broadcast:
1311 phantom_destroy_tx_ctx ( phantom_port );
1313 phantom_destroy_rx_ctx ( phantom_port );
1315 free_dma ( phantom_port->desc, sizeof ( *(phantom_port->desc) ) );
1316 phantom_port->desc = NULL;
1324 * @v netdev Net device
1326 static void phantom_close ( struct net_device *netdev ) {
1327 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1328 struct io_buffer *iobuf;
1331 /* Shut down the port */
1332 phantom_del_macaddr ( phantom_port, netdev->ll_addr );
1333 phantom_del_macaddr ( phantom_port,
1334 netdev->ll_protocol->ll_broadcast );
1335 phantom_destroy_tx_ctx ( phantom_port );
1336 phantom_destroy_rx_ctx ( phantom_port );
1337 free_dma ( phantom_port->desc, sizeof ( *(phantom_port->desc) ) );
1338 phantom_port->desc = NULL;
1340 /* Flush any uncompleted descriptors */
1341 for ( i = 0 ; i < PHN_RDS_MAX_FILL ; i++ ) {
1342 iobuf = phantom_port->rds_iobuf[i];
1345 phantom_port->rds_iobuf[i] = NULL;
1348 for ( i = 0 ; i < PHN_NUM_CDS ; i++ ) {
1349 iobuf = phantom_port->cds_iobuf[i];
1351 netdev_tx_complete_err ( netdev, iobuf, -ECANCELED );
1352 phantom_port->cds_iobuf[i] = NULL;
1360 * @v netdev Network device
1361 * @v iobuf I/O buffer
1362 * @ret rc Return status code
1364 static int phantom_transmit ( struct net_device *netdev,
1365 struct io_buffer *iobuf ) {
1366 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1367 union phantom_cds cds;
1370 /* Get descriptor ring entry */
1371 index = phantom_alloc_cds ( phantom_port );
1375 /* Fill descriptor ring entry */
1376 memset ( &cds, 0, sizeof ( cds ) );
1377 NX_FILL_3 ( &cds, 0,
1378 tx.opcode, UNM_TX_ETHER_PKT,
1380 tx.length, iob_len ( iobuf ) );
1381 NX_FILL_2 ( &cds, 2,
1382 tx.port, phantom_port->port,
1383 tx.context_id, phantom_port->port );
1384 NX_FILL_1 ( &cds, 4,
1385 tx.buffer1_dma_addr, virt_to_bus ( iobuf->data ) );
1386 NX_FILL_1 ( &cds, 5,
1387 tx.buffer1_length, iob_len ( iobuf ) );
1389 /* Record I/O buffer */
1390 assert ( phantom_port->cds_iobuf[index] == NULL );
1391 phantom_port->cds_iobuf[index] = iobuf;
1393 /* Post descriptor */
1394 phantom_post_cds ( phantom_port, &cds );
1400 * Poll for received packets
1402 * @v netdev Network device
1404 static void phantom_poll ( struct net_device *netdev ) {
1405 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1406 struct phantom_nic *phantom = phantom_port->phantom;
1407 struct io_buffer *iobuf;
1408 unsigned int cds_consumer_idx;
1409 unsigned int raw_new_cds_consumer_idx;
1410 unsigned int new_cds_consumer_idx;
1411 unsigned int rds_consumer_idx;
1412 unsigned int sds_consumer_idx;
1413 struct phantom_sds *sds;
1414 unsigned int sds_handle;
1415 unsigned int sds_opcode;
1417 /* Check for TX completions */
1418 cds_consumer_idx = phantom_port->cds_consumer_idx;
1419 raw_new_cds_consumer_idx = phantom_port->desc->cmd_cons;
1420 new_cds_consumer_idx = le32_to_cpu ( raw_new_cds_consumer_idx );
1421 while ( cds_consumer_idx != new_cds_consumer_idx ) {
1422 DBGC2 ( phantom, "Phantom %p port %d CDS %d complete\n",
1423 phantom, phantom_port->port, cds_consumer_idx );
1424 /* Completions may be for commands other than TX, so
1425 * there may not always be an associated I/O buffer.
1427 if ( ( iobuf = phantom_port->cds_iobuf[cds_consumer_idx] ) ) {
1428 netdev_tx_complete ( netdev, iobuf );
1429 phantom_port->cds_iobuf[cds_consumer_idx] = NULL;
1431 cds_consumer_idx = ( ( cds_consumer_idx + 1 ) % PHN_NUM_CDS );
1432 phantom_port->cds_consumer_idx = cds_consumer_idx;
1435 /* Check for received packets */
1436 rds_consumer_idx = phantom_port->rds_consumer_idx;
1437 sds_consumer_idx = phantom_port->sds_consumer_idx;
1439 sds = &phantom_port->desc->sds[sds_consumer_idx];
1440 if ( NX_GET ( sds, owner ) == 0 )
1443 DBGC2 ( phantom, "Phantom %p port %d SDS %d status:\n",
1444 phantom, phantom_port->port, sds_consumer_idx );
1445 DBGC2_HDA ( phantom, virt_to_bus ( sds ), sds, sizeof (*sds) );
1447 /* Check received opcode */
1448 sds_opcode = NX_GET ( sds, opcode );
1449 if ( ( sds_opcode == UNM_RXPKT_DESC ) ||
1450 ( sds_opcode == UNM_SYN_OFFLOAD ) ) {
1452 /* Sanity check: ensure that all of the SDS
1453 * descriptor has been written.
1455 if ( NX_GET ( sds, total_length ) == 0 ) {
1456 DBGC ( phantom, "Phantom %p port %d SDS %d "
1457 "incomplete; deferring\n", phantom,
1458 phantom_port->port, sds_consumer_idx );
1459 /* Leave for next poll() */
1463 /* Process received packet */
1464 sds_handle = NX_GET ( sds, handle );
1465 iobuf = phantom_port->rds_iobuf[sds_handle];
1466 assert ( iobuf != NULL );
1467 iob_put ( iobuf, NX_GET ( sds, total_length ) );
1468 iob_pull ( iobuf, NX_GET ( sds, pkt_offset ) );
1469 DBGC2 ( phantom, "Phantom %p port %d RDS %d "
1471 phantom, phantom_port->port, sds_handle );
1472 netdev_rx ( netdev, iobuf );
1473 phantom_port->rds_iobuf[sds_handle] = NULL;
1475 /* Update RDS consumer counter. This is a
1476 * lower bound for the number of descriptors
1477 * that have been read by the hardware, since
1478 * the hardware must have read at least one
1479 * descriptor for each completion that we
1483 ( ( rds_consumer_idx + 1 ) % PHN_NUM_RDS );
1484 phantom_port->rds_consumer_idx = rds_consumer_idx;
1488 DBGC ( phantom, "Phantom %p port %d unexpected SDS "
1490 phantom, phantom_port->port, sds_opcode );
1491 DBGC_HDA ( phantom, virt_to_bus ( sds ),
1492 sds, sizeof ( *sds ) );
1495 /* Clear status descriptor */
1496 memset ( sds, 0, sizeof ( *sds ) );
1498 /* Update SDS consumer index */
1499 sds_consumer_idx = ( ( sds_consumer_idx + 1 ) % PHN_NUM_SDS );
1500 phantom_port->sds_consumer_idx = sds_consumer_idx;
1502 phantom_writel ( phantom, phantom_port->sds_consumer_idx,
1503 phantom_port->sds_consumer_crb );
1506 /* Refill the RX descriptor ring */
1507 phantom_refill_rx_ring ( netdev );
1509 /* Occasionally poll the link state */
1510 if ( phantom_port->link_poll_timer-- == 0 ) {
1511 phantom_poll_link_state ( phantom );
1512 /* Reset the link poll timer */
1513 phantom_port->link_poll_timer = PHN_LINK_POLL_FREQUENCY;
1518 * Enable/disable interrupts
1520 * @v netdev Network device
1521 * @v enable Interrupts should be enabled
1523 static void phantom_irq ( struct net_device *netdev, int enable ) {
1524 struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
1525 struct phantom_nic *phantom = phantom_port->phantom;
1526 static const unsigned long sw_int_mask_reg[UNM_FLASH_NUM_PORTS] = {
1527 UNM_NIC_REG_SW_INT_MASK_0,
1528 UNM_NIC_REG_SW_INT_MASK_1,
1529 UNM_NIC_REG_SW_INT_MASK_2,
1530 UNM_NIC_REG_SW_INT_MASK_3
1533 phantom_writel ( phantom,
1535 sw_int_mask_reg[phantom_port->port] );
1538 /** Phantom net device operations */
1539 static struct net_device_operations phantom_operations = {
1540 .open = phantom_open,
1541 .close = phantom_close,
1542 .transmit = phantom_transmit,
1543 .poll = phantom_poll,
1548 * Map Phantom CRB window
1550 * @v phantom Phantom NIC
1551 * @ret rc Return status code
1553 static int phantom_map_crb ( struct phantom_nic *phantom,
1554 struct pci_device *pci ) {
1555 unsigned long bar0_start;
1556 unsigned long bar0_size;
1558 bar0_start = pci_bar_start ( pci, PCI_BASE_ADDRESS_0 );
1559 bar0_size = pci_bar_size ( pci, PCI_BASE_ADDRESS_0 );
1560 DBGC ( phantom, "Phantom %p BAR0 is %08lx+%lx\n",
1561 phantom, bar0_start, bar0_size );
1563 switch ( bar0_size ) {
1564 case ( 128 * 1024 * 1024 ) :
1565 DBGC ( phantom, "Phantom %p has 128MB BAR\n", phantom );
1566 phantom->crb_access = phantom_crb_access_128m;
1568 case ( 32 * 1024 * 1024 ) :
1569 DBGC ( phantom, "Phantom %p has 32MB BAR\n", phantom );
1570 phantom->crb_access = phantom_crb_access_32m;
1572 case ( 2 * 1024 * 1024 ) :
1573 DBGC ( phantom, "Phantom %p has 2MB BAR\n", phantom );
1574 phantom->crb_access = phantom_crb_access_2m;
1577 DBGC ( phantom, "Phantom %p has bad BAR size\n", phantom );
1581 phantom->bar0 = ioremap ( bar0_start, bar0_size );
1582 if ( ! phantom->bar0 ) {
1583 DBGC ( phantom, "Phantom %p could not map BAR0\n", phantom );
1587 /* Mark current CRB window as invalid, so that the first
1588 * read/write will set the current window.
1590 phantom->crb_window = -1UL;
1596 * Read Phantom flash contents
1598 * @v phantom Phantom NIC
1599 * @ret rc Return status code
1601 static int phantom_read_flash ( struct phantom_nic *phantom ) {
1602 struct unm_board_info board_info;
1605 /* Initialise flash access */
1606 phantom->spi_bus.rw = phantom_spi_rw;
1607 phantom->flash.bus = &phantom->spi_bus;
1608 init_m25p32 ( &phantom->flash );
1609 /* Phantom doesn't support greater than 4-byte block sizes */
1610 phantom->flash.nvs.block_size = UNM_SPI_BLKSIZE;
1612 /* Read and verify board information */
1613 if ( ( rc = nvs_read ( &phantom->flash.nvs, UNM_BRDCFG_START,
1614 &board_info, sizeof ( board_info ) ) ) != 0 ) {
1615 DBGC ( phantom, "Phantom %p could not read board info: %s\n",
1616 phantom, strerror ( rc ) );
1619 if ( board_info.magic != UNM_BDINFO_MAGIC ) {
1620 DBGC ( phantom, "Phantom %p has bad board info magic %lx\n",
1621 phantom, board_info.magic );
1622 DBGC_HD ( phantom, &board_info, sizeof ( board_info ) );
1625 if ( board_info.header_version != UNM_BDINFO_VERSION ) {
1626 DBGC ( phantom, "Phantom %p has bad board info version %lx\n",
1627 phantom, board_info.header_version );
1628 DBGC_HD ( phantom, &board_info, sizeof ( board_info ) );
1632 /* Identify board type and number of ports */
1633 switch ( board_info.board_type ) {
1634 case UNM_BRDTYPE_P3_4_GB:
1635 case UNM_BRDTYPE_P3_4_GB_MM:
1636 phantom->num_ports = 4;
1638 case UNM_BRDTYPE_P3_HMEZ:
1639 case UNM_BRDTYPE_P3_IMEZ:
1640 case UNM_BRDTYPE_P3_10G_CX4:
1641 case UNM_BRDTYPE_P3_10G_CX4_LP:
1642 case UNM_BRDTYPE_P3_10G_SFP_PLUS:
1643 case UNM_BRDTYPE_P3_XG_LOM:
1644 phantom->num_ports = 2;
1646 case UNM_BRDTYPE_P3_10000_BASE_T:
1647 case UNM_BRDTYPE_P3_10G_XFP:
1648 phantom->num_ports = 1;
1651 DBGC ( phantom, "Phantom %p unrecognised board type %#lx; "
1652 "assuming single-port\n",
1653 phantom, board_info.board_type );
1654 phantom->num_ports = 1;
1657 DBGC ( phantom, "Phantom %p board type is %#lx (%d ports)\n",
1658 phantom, board_info.board_type, phantom->num_ports );
1664 * Initialise the Phantom command PEG
1666 * @v phantom Phantom NIC
1667 * @ret rc Return status code
1669 static int phantom_init_cmdpeg ( struct phantom_nic *phantom ) {
1672 physaddr_t dummy_dma_phys;
1673 unsigned int retries;
1674 uint32_t cmdpeg_state;
1675 uint32_t last_cmdpeg_state = 0;
1677 /* Check for a previous initialisation. This could have
1678 * happened if, for example, the BIOS used the UNDI API to
1679 * drive the NIC prior to a full PXE boot.
1681 cmdpeg_state = phantom_readl ( phantom, UNM_NIC_REG_CMDPEG_STATE );
1682 if ( cmdpeg_state == UNM_NIC_REG_CMDPEG_STATE_INITIALIZE_ACK ) {
1683 DBGC ( phantom, "Phantom %p command PEG already initialized\n",
1688 /* If this was a cold boot, check that the hardware came up ok */
1689 cold_boot = phantom_readl ( phantom, UNM_CAM_RAM_COLD_BOOT );
1690 if ( cold_boot == UNM_CAM_RAM_COLD_BOOT_MAGIC ) {
1691 DBGC ( phantom, "Phantom %p coming up from cold boot\n",
1693 sw_reset = phantom_readl ( phantom, UNM_ROMUSB_GLB_SW_RESET );
1694 if ( sw_reset != UNM_ROMUSB_GLB_SW_RESET_MAGIC ) {
1695 DBGC ( phantom, "Phantom %p reset failed: %08lx\n",
1696 phantom, sw_reset );
1700 DBGC ( phantom, "Phantom %p coming up from warm boot "
1701 "(%08lx)\n", phantom, cold_boot );
1703 /* Clear cold-boot flag */
1704 phantom_writel ( phantom, 0, UNM_CAM_RAM_COLD_BOOT );
1706 /* Set port modes */
1707 phantom_writel ( phantom, UNM_CAM_RAM_PORT_MODE_AUTO_NEG_1G,
1708 UNM_CAM_RAM_WOL_PORT_MODE );
1710 /* Pass dummy DMA area to card */
1711 dummy_dma_phys = virt_to_bus ( phantom->dma_buf->dummy_dma );
1712 DBGC ( phantom, "Phantom %p dummy DMA at %08lx\n",
1713 phantom, dummy_dma_phys );
1714 phantom_write_hilo ( phantom, dummy_dma_phys,
1715 UNM_NIC_REG_DUMMY_BUF_ADDR_LO,
1716 UNM_NIC_REG_DUMMY_BUF_ADDR_HI );
1717 phantom_writel ( phantom, UNM_NIC_REG_DUMMY_BUF_INIT,
1718 UNM_NIC_REG_DUMMY_BUF );
1720 /* Tell the hardware that tuning is complete */
1721 phantom_writel ( phantom, UNM_ROMUSB_GLB_PEGTUNE_DONE_MAGIC,
1722 UNM_ROMUSB_GLB_PEGTUNE_DONE );
1724 /* Wait for command PEG to finish initialising */
1725 DBGC ( phantom, "Phantom %p initialising command PEG (will take up to "
1726 "%d seconds)...\n", phantom, PHN_CMDPEG_INIT_TIMEOUT_SEC );
1727 for ( retries = 0; retries < PHN_CMDPEG_INIT_TIMEOUT_SEC; retries++ ) {
1728 cmdpeg_state = phantom_readl ( phantom,
1729 UNM_NIC_REG_CMDPEG_STATE );
1730 if ( cmdpeg_state != last_cmdpeg_state ) {
1731 DBGC ( phantom, "Phantom %p command PEG state is "
1732 "%08lx after %d seconds...\n",
1733 phantom, cmdpeg_state, retries );
1734 last_cmdpeg_state = cmdpeg_state;
1736 if ( cmdpeg_state == UNM_NIC_REG_CMDPEG_STATE_INITIALIZED ) {
1737 /* Acknowledge the PEG initialisation */
1738 phantom_writel ( phantom,
1739 UNM_NIC_REG_CMDPEG_STATE_INITIALIZE_ACK,
1740 UNM_NIC_REG_CMDPEG_STATE );
1746 DBGC ( phantom, "Phantom %p timed out waiting for command PEG to "
1747 "initialise (status %08lx)\n", phantom, cmdpeg_state );
1752 * Read Phantom MAC address
1754 * @v phanton_port Phantom NIC port
1755 * @v ll_addr Buffer to fill with MAC address
1757 static void phantom_get_macaddr ( struct phantom_nic_port *phantom_port,
1758 uint8_t *ll_addr ) {
1759 struct phantom_nic *phantom = phantom_port->phantom;
1761 uint8_t mac_addr[2][ETH_ALEN];
1764 unsigned long offset;
1767 /* Read the three dwords that include this MAC address and one other */
1768 offset = ( UNM_CAM_RAM_MAC_ADDRS +
1769 ( 12 * ( phantom_port->port / 2 ) ) );
1770 for ( i = 0 ; i < 3 ; i++, offset += 4 ) {
1771 u.dwords[i] = phantom_readl ( phantom, offset );
1774 /* Copy out the relevant MAC address */
1775 for ( i = 0 ; i < ETH_ALEN ; i++ ) {
1776 ll_addr[ ETH_ALEN - i - 1 ] =
1777 u.mac_addr[ phantom_port->port & 1 ][i];
1779 DBGC ( phantom, "Phantom %p port %d MAC address is %s\n",
1780 phantom, phantom_port->port, eth_ntoa ( ll_addr ) );
1784 * Initialise Phantom receive PEG
1786 * @v phantom Phantom NIC
1787 * @ret rc Return status code
1789 static int phantom_init_rcvpeg ( struct phantom_nic *phantom ) {
1790 unsigned int retries;
1791 uint32_t rcvpeg_state;
1792 uint32_t last_rcvpeg_state = 0;
1794 DBGC ( phantom, "Phantom %p initialising receive PEG (will take up to "
1795 "%d seconds)...\n", phantom, PHN_RCVPEG_INIT_TIMEOUT_SEC );
1796 for ( retries = 0; retries < PHN_RCVPEG_INIT_TIMEOUT_SEC; retries++ ) {
1797 rcvpeg_state = phantom_readl ( phantom,
1798 UNM_NIC_REG_RCVPEG_STATE );
1799 if ( rcvpeg_state != last_rcvpeg_state ) {
1800 DBGC ( phantom, "Phantom %p receive PEG state is "
1801 "%08lx after %d seconds...\n",
1802 phantom, rcvpeg_state, retries );
1803 last_rcvpeg_state = rcvpeg_state;
1805 if ( rcvpeg_state == UNM_NIC_REG_RCVPEG_STATE_INITIALIZED )
1810 DBGC ( phantom, "Phantom %p timed out waiting for receive PEG to "
1811 "initialise (status %08lx)\n", phantom, rcvpeg_state );
1820 * @ret rc Return status code
1822 static int phantom_probe ( struct pci_device *pci,
1823 const struct pci_device_id *id __unused ) {
1824 struct phantom_nic *phantom;
1825 struct net_device *netdev;
1826 struct phantom_nic_port *phantom_port;
1830 /* Phantom NICs expose multiple PCI functions, used for
1831 * virtualisation. Ignore everything except function 0.
1833 if ( PCI_FUNC ( pci->devfn ) != 0 )
1836 /* Allocate Phantom device */
1837 phantom = zalloc ( sizeof ( *phantom ) );
1840 goto err_alloc_phantom;
1842 pci_set_drvdata ( pci, phantom );
1844 /* Fix up PCI device */
1845 adjust_pci_device ( pci );
1848 if ( ( rc = phantom_map_crb ( phantom, pci ) ) != 0 )
1851 /* Read flash information */
1852 if ( ( rc = phantom_read_flash ( phantom ) ) != 0 )
1853 goto err_read_flash;
1855 /* Allocate net devices for each port */
1856 for ( i = 0 ; i < phantom->num_ports ; i++ ) {
1857 netdev = alloc_etherdev ( sizeof ( *phantom_port ) );
1860 goto err_alloc_etherdev;
1862 phantom->netdev[i] = netdev;
1863 netdev_init ( netdev, &phantom_operations );
1864 phantom_port = netdev_priv ( netdev );
1865 netdev->dev = &pci->dev;
1866 phantom_port->phantom = phantom;
1867 phantom_port->port = i;
1870 /* BUG5945 - need to hack PCI config space on P3 B1 silicon.
1871 * B2 will have this fixed; remove this hack when B1 is no
1874 for ( i = 0 ; i < 8 ; i++ ) {
1876 pci->devfn = PCI_DEVFN ( PCI_SLOT ( pci->devfn ), i );
1877 pci_read_config_dword ( pci, 0xc8, &temp );
1878 pci_read_config_dword ( pci, 0xc8, &temp );
1879 pci_write_config_dword ( pci, 0xc8, 0xf1000 );
1881 pci->devfn = PCI_DEVFN ( PCI_SLOT ( pci->devfn ), 0 );
1883 /* Allocate dummy DMA buffer and perform initial hardware handshake */
1884 phantom->dma_buf = malloc_dma ( sizeof ( *(phantom->dma_buf) ),
1885 UNM_DMA_BUFFER_ALIGN );
1886 if ( ! phantom->dma_buf )
1888 if ( ( rc = phantom_init_cmdpeg ( phantom ) ) != 0 )
1889 goto err_init_cmdpeg;
1891 /* Initialise the receive firmware */
1892 if ( ( rc = phantom_init_rcvpeg ( phantom ) ) != 0 )
1893 goto err_init_rcvpeg;
1895 /* Read MAC addresses */
1896 for ( i = 0 ; i < phantom->num_ports ; i++ ) {
1897 phantom_port = netdev_priv ( phantom->netdev[i] );
1898 phantom_get_macaddr ( phantom_port,
1899 phantom->netdev[i]->ll_addr );
1902 /* Register network devices */
1903 for ( i = 0 ; i < phantom->num_ports ; i++ ) {
1904 if ( ( rc = register_netdev ( phantom->netdev[i] ) ) != 0 ) {
1905 DBGC ( phantom, "Phantom %p could not register port "
1906 "%d: %s\n", phantom, i, strerror ( rc ) );
1907 goto err_register_netdev;
1913 i = ( phantom->num_ports - 1 );
1914 err_register_netdev:
1915 for ( ; i >= 0 ; i-- )
1916 unregister_netdev ( phantom->netdev[i] );
1919 free_dma ( phantom->dma_buf, sizeof ( *(phantom->dma_buf) ) );
1920 phantom->dma_buf = NULL;
1922 i = ( phantom->num_ports - 1 );
1924 for ( ; i >= 0 ; i-- ) {
1925 netdev_nullify ( phantom->netdev[i] );
1926 netdev_put ( phantom->netdev[i] );
1940 static void phantom_remove ( struct pci_device *pci ) {
1941 struct phantom_nic *phantom = pci_get_drvdata ( pci );
1944 for ( i = ( phantom->num_ports - 1 ) ; i >= 0 ; i-- )
1945 unregister_netdev ( phantom->netdev[i] );
1946 free_dma ( phantom->dma_buf, sizeof ( *(phantom->dma_buf) ) );
1947 phantom->dma_buf = NULL;
1948 for ( i = ( phantom->num_ports - 1 ) ; i >= 0 ; i-- ) {
1949 netdev_nullify ( phantom->netdev[i] );
1950 netdev_put ( phantom->netdev[i] );
1955 /** Phantom PCI IDs */
1956 static struct pci_device_id phantom_nics[] = {
1957 PCI_ROM ( 0x4040, 0x0100, "nx", "NX" ),
1960 /** Phantom PCI driver */
1961 struct pci_driver phantom_driver __pci_driver = {
1962 .ids = phantom_nics,
1963 .id_count = ( sizeof ( phantom_nics ) / sizeof ( phantom_nics[0] ) ),
1964 .probe = phantom_probe,
1965 .remove = phantom_remove,