xref: /openbmc/qemu/hw/net/npcm_gmac.c (revision 7d87775f)
1 /*
2  * Nuvoton NPCM7xx/8xx GMAC Module
3  *
4  * Copyright 2024 Google LLC
5  * Authors:
6  * Hao Wu <wuhaotsh@google.com>
7  * Nabih Estefan <nabihestefan@google.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17  * for more details.
18  *
19  * Unsupported/unimplemented features:
20  * - MII is not implemented, MII_ADDR.BUSY and MII_DATA always return zero
21  * - Precision timestamp (PTP) is not implemented.
22  */
23 
24 #include "qemu/osdep.h"
25 
26 #include "hw/registerfields.h"
27 #include "hw/net/mii.h"
28 #include "hw/net/npcm_gmac.h"
29 #include "migration/vmstate.h"
30 #include "net/checksum.h"
31 #include "net/eth.h"
32 #include "net/net.h"
33 #include "qemu/cutils.h"
34 #include "qemu/log.h"
35 #include "qemu/units.h"
36 #include "sysemu/dma.h"
37 #include "trace.h"
38 
39 REG32(NPCM_DMA_BUS_MODE, 0x1000)
40 REG32(NPCM_DMA_XMT_POLL_DEMAND, 0x1004)
41 REG32(NPCM_DMA_RCV_POLL_DEMAND, 0x1008)
42 REG32(NPCM_DMA_RX_BASE_ADDR, 0x100c)
43 REG32(NPCM_DMA_TX_BASE_ADDR, 0x1010)
44 REG32(NPCM_DMA_STATUS, 0x1014)
45 REG32(NPCM_DMA_CONTROL, 0x1018)
46 REG32(NPCM_DMA_INTR_ENA, 0x101c)
47 REG32(NPCM_DMA_MISSED_FRAME_CTR, 0x1020)
48 REG32(NPCM_DMA_HOST_TX_DESC, 0x1048)
49 REG32(NPCM_DMA_HOST_RX_DESC, 0x104c)
50 REG32(NPCM_DMA_CUR_TX_BUF_ADDR, 0x1050)
51 REG32(NPCM_DMA_CUR_RX_BUF_ADDR, 0x1054)
52 REG32(NPCM_DMA_HW_FEATURE, 0x1058)
53 
54 REG32(NPCM_GMAC_MAC_CONFIG, 0x0)
55 REG32(NPCM_GMAC_FRAME_FILTER, 0x4)
56 REG32(NPCM_GMAC_HASH_HIGH, 0x8)
57 REG32(NPCM_GMAC_HASH_LOW, 0xc)
58 REG32(NPCM_GMAC_MII_ADDR, 0x10)
59 REG32(NPCM_GMAC_MII_DATA, 0x14)
60 REG32(NPCM_GMAC_FLOW_CTRL, 0x18)
61 REG32(NPCM_GMAC_VLAN_FLAG, 0x1c)
62 REG32(NPCM_GMAC_VERSION, 0x20)
63 REG32(NPCM_GMAC_WAKEUP_FILTER, 0x28)
64 REG32(NPCM_GMAC_PMT, 0x2c)
65 REG32(NPCM_GMAC_LPI_CTRL, 0x30)
66 REG32(NPCM_GMAC_TIMER_CTRL, 0x34)
67 REG32(NPCM_GMAC_INT_STATUS, 0x38)
68 REG32(NPCM_GMAC_INT_MASK, 0x3c)
69 REG32(NPCM_GMAC_MAC0_ADDR_HI, 0x40)
70 REG32(NPCM_GMAC_MAC0_ADDR_LO, 0x44)
71 REG32(NPCM_GMAC_MAC1_ADDR_HI, 0x48)
72 REG32(NPCM_GMAC_MAC1_ADDR_LO, 0x4c)
73 REG32(NPCM_GMAC_MAC2_ADDR_HI, 0x50)
74 REG32(NPCM_GMAC_MAC2_ADDR_LO, 0x54)
75 REG32(NPCM_GMAC_MAC3_ADDR_HI, 0x58)
76 REG32(NPCM_GMAC_MAC3_ADDR_LO, 0x5c)
77 REG32(NPCM_GMAC_RGMII_STATUS, 0xd8)
78 REG32(NPCM_GMAC_WATCHDOG, 0xdc)
79 REG32(NPCM_GMAC_PTP_TCR, 0x700)
80 REG32(NPCM_GMAC_PTP_SSIR, 0x704)
81 REG32(NPCM_GMAC_PTP_STSR, 0x708)
82 REG32(NPCM_GMAC_PTP_STNSR, 0x70c)
83 REG32(NPCM_GMAC_PTP_STSUR, 0x710)
84 REG32(NPCM_GMAC_PTP_STNSUR, 0x714)
85 REG32(NPCM_GMAC_PTP_TAR, 0x718)
86 REG32(NPCM_GMAC_PTP_TTSR, 0x71c)
87 
88 /* Register Fields */
89 #define NPCM_GMAC_MII_ADDR_BUSY             BIT(0)
90 #define NPCM_GMAC_MII_ADDR_WRITE            BIT(1)
91 #define NPCM_GMAC_MII_ADDR_GR(rv)           extract16((rv), 6, 5)
92 #define NPCM_GMAC_MII_ADDR_PA(rv)           extract16((rv), 11, 5)
93 
94 #define NPCM_GMAC_INT_MASK_LPIIM            BIT(10)
95 #define NPCM_GMAC_INT_MASK_PMTM             BIT(3)
96 #define NPCM_GMAC_INT_MASK_RGIM             BIT(0)
97 
98 #define NPCM_DMA_BUS_MODE_SWR               BIT(0)
99 
100 static const uint32_t npcm_gmac_cold_reset_values[NPCM_GMAC_NR_REGS] = {
101     /* Reduce version to 3.2 so that the kernel can enable interrupt. */
102     [R_NPCM_GMAC_VERSION]         = 0x00001032,
103     [R_NPCM_GMAC_TIMER_CTRL]      = 0x03e80000,
104     [R_NPCM_GMAC_MAC0_ADDR_HI]    = 0x8000ffff,
105     [R_NPCM_GMAC_MAC0_ADDR_LO]    = 0xffffffff,
106     [R_NPCM_GMAC_MAC1_ADDR_HI]    = 0x0000ffff,
107     [R_NPCM_GMAC_MAC1_ADDR_LO]    = 0xffffffff,
108     [R_NPCM_GMAC_MAC2_ADDR_HI]    = 0x0000ffff,
109     [R_NPCM_GMAC_MAC2_ADDR_LO]    = 0xffffffff,
110     [R_NPCM_GMAC_MAC3_ADDR_HI]    = 0x0000ffff,
111     [R_NPCM_GMAC_MAC3_ADDR_LO]    = 0xffffffff,
112     [R_NPCM_GMAC_PTP_TCR]         = 0x00002000,
113     [R_NPCM_DMA_BUS_MODE]         = 0x00020101,
114     [R_NPCM_DMA_HW_FEATURE]       = 0x100d4f37,
115 };
116 
117 static const uint16_t phy_reg_init[] = {
118     [MII_BMCR]      = MII_BMCR_AUTOEN | MII_BMCR_FD | MII_BMCR_SPEED1000,
119     [MII_BMSR]      = MII_BMSR_100TX_FD | MII_BMSR_100TX_HD | MII_BMSR_10T_FD |
120                       MII_BMSR_10T_HD | MII_BMSR_EXTSTAT | MII_BMSR_AUTONEG |
121                       MII_BMSR_LINK_ST | MII_BMSR_EXTCAP,
122     [MII_PHYID1]    = 0x0362,
123     [MII_PHYID2]    = 0x5e6a,
124     [MII_ANAR]      = MII_ANAR_TXFD | MII_ANAR_TX | MII_ANAR_10FD |
125                       MII_ANAR_10 | MII_ANAR_CSMACD,
126     [MII_ANLPAR]    = MII_ANLPAR_ACK | MII_ANLPAR_PAUSE |
127                       MII_ANLPAR_TXFD | MII_ANLPAR_TX | MII_ANLPAR_10FD |
128                       MII_ANLPAR_10 | MII_ANLPAR_CSMACD,
129     [MII_ANER]      = 0x64 | MII_ANER_NWAY,
130     [MII_ANNP]      = 0x2001,
131     [MII_CTRL1000]  = MII_CTRL1000_FULL,
132     [MII_STAT1000]  = MII_STAT1000_FULL,
133     [MII_EXTSTAT]   = 0x3000, /* 1000BASTE_T full-duplex capable */
134 };
135 
136 static void npcm_gmac_soft_reset(NPCMGMACState *gmac)
137 {
138     memcpy(gmac->regs, npcm_gmac_cold_reset_values,
139            NPCM_GMAC_NR_REGS * sizeof(uint32_t));
140     /* Clear reset bits */
141     gmac->regs[R_NPCM_DMA_BUS_MODE] &= ~NPCM_DMA_BUS_MODE_SWR;
142 }
143 
144 static void gmac_phy_set_link(NPCMGMACState *gmac, bool active)
145 {
146     /* Autonegotiation status mirrors link status.  */
147     if (active) {
148         gmac->phy_regs[0][MII_BMSR] |= (MII_BMSR_LINK_ST | MII_BMSR_AN_COMP);
149     } else {
150         gmac->phy_regs[0][MII_BMSR] &= ~(MII_BMSR_LINK_ST | MII_BMSR_AN_COMP);
151     }
152 }
153 
154 static bool gmac_can_receive(NetClientState *nc)
155 {
156     NPCMGMACState *gmac = NPCM_GMAC(qemu_get_nic_opaque(nc));
157 
158     /* If GMAC receive is disabled. */
159     if (!(gmac->regs[R_NPCM_GMAC_MAC_CONFIG] & NPCM_GMAC_MAC_CONFIG_RX_EN)) {
160         return false;
161     }
162 
163     /* If GMAC DMA RX is stopped. */
164     if (!(gmac->regs[R_NPCM_DMA_CONTROL] & NPCM_DMA_CONTROL_START_STOP_RX)) {
165         return false;
166     }
167     return true;
168 }
169 
170 /*
171  * Function that updates the GMAC IRQ
172  * It find the logical OR of the enabled bits for NIS (if enabled)
173  * It find the logical OR of the enabled bits for AIS (if enabled)
174  */
175 static void gmac_update_irq(NPCMGMACState *gmac)
176 {
177     /*
178      * Check if the normal interrupts summary is enabled
179      * if so, add the bits for the summary that are enabled
180      */
181     if (gmac->regs[R_NPCM_DMA_INTR_ENA] & gmac->regs[R_NPCM_DMA_STATUS] &
182         (NPCM_DMA_INTR_ENAB_NIE_BITS)) {
183         gmac->regs[R_NPCM_DMA_STATUS] |=  NPCM_DMA_STATUS_NIS;
184     }
185     /*
186      * Check if the abnormal interrupts summary is enabled
187      * if so, add the bits for the summary that are enabled
188      */
189     if (gmac->regs[R_NPCM_DMA_INTR_ENA] & gmac->regs[R_NPCM_DMA_STATUS] &
190         (NPCM_DMA_INTR_ENAB_AIE_BITS)) {
191         gmac->regs[R_NPCM_DMA_STATUS] |=  NPCM_DMA_STATUS_AIS;
192     }
193 
194     /* Get the logical OR of both normal and abnormal interrupts */
195     int level = !!((gmac->regs[R_NPCM_DMA_STATUS] &
196                     gmac->regs[R_NPCM_DMA_INTR_ENA] &
197                     NPCM_DMA_STATUS_NIS) |
198                    (gmac->regs[R_NPCM_DMA_STATUS] &
199                    gmac->regs[R_NPCM_DMA_INTR_ENA] &
200                    NPCM_DMA_STATUS_AIS));
201 
202     /* Set the IRQ */
203     trace_npcm_gmac_update_irq(DEVICE(gmac)->canonical_path,
204                                gmac->regs[R_NPCM_DMA_STATUS],
205                                gmac->regs[R_NPCM_DMA_INTR_ENA],
206                                level);
207     qemu_set_irq(gmac->irq, level);
208 }
209 
210 static int gmac_read_rx_desc(dma_addr_t addr, struct NPCMGMACRxDesc *desc)
211 {
212     if (dma_memory_read(&address_space_memory, addr, desc,
213                         sizeof(*desc), MEMTXATTRS_UNSPECIFIED)) {
214         qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to read descriptor @ 0x%"
215                       HWADDR_PRIx "\n", __func__, addr);
216         return -1;
217     }
218     desc->rdes0 = le32_to_cpu(desc->rdes0);
219     desc->rdes1 = le32_to_cpu(desc->rdes1);
220     desc->rdes2 = le32_to_cpu(desc->rdes2);
221     desc->rdes3 = le32_to_cpu(desc->rdes3);
222     return 0;
223 }
224 
225 static int gmac_write_rx_desc(dma_addr_t addr, struct NPCMGMACRxDesc *desc)
226 {
227     struct NPCMGMACRxDesc le_desc;
228     le_desc.rdes0 = cpu_to_le32(desc->rdes0);
229     le_desc.rdes1 = cpu_to_le32(desc->rdes1);
230     le_desc.rdes2 = cpu_to_le32(desc->rdes2);
231     le_desc.rdes3 = cpu_to_le32(desc->rdes3);
232     if (dma_memory_write(&address_space_memory, addr, &le_desc,
233                         sizeof(le_desc), MEMTXATTRS_UNSPECIFIED)) {
234         qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to write descriptor @ 0x%"
235                       HWADDR_PRIx "\n", __func__, addr);
236         return -1;
237     }
238     return 0;
239 }
240 
241 static int gmac_read_tx_desc(dma_addr_t addr, struct NPCMGMACTxDesc *desc)
242 {
243     if (dma_memory_read(&address_space_memory, addr, desc,
244                         sizeof(*desc), MEMTXATTRS_UNSPECIFIED)) {
245         qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to read descriptor @ 0x%"
246                       HWADDR_PRIx "\n", __func__, addr);
247         return -1;
248     }
249     desc->tdes0 = le32_to_cpu(desc->tdes0);
250     desc->tdes1 = le32_to_cpu(desc->tdes1);
251     desc->tdes2 = le32_to_cpu(desc->tdes2);
252     desc->tdes3 = le32_to_cpu(desc->tdes3);
253     return 0;
254 }
255 
256 static int gmac_write_tx_desc(dma_addr_t addr, struct NPCMGMACTxDesc *desc)
257 {
258     struct NPCMGMACTxDesc le_desc;
259     le_desc.tdes0 = cpu_to_le32(desc->tdes0);
260     le_desc.tdes1 = cpu_to_le32(desc->tdes1);
261     le_desc.tdes2 = cpu_to_le32(desc->tdes2);
262     le_desc.tdes3 = cpu_to_le32(desc->tdes3);
263     if (dma_memory_write(&address_space_memory, addr, &le_desc,
264                         sizeof(le_desc), MEMTXATTRS_UNSPECIFIED)) {
265         qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to write descriptor @ 0x%"
266                       HWADDR_PRIx "\n", __func__, addr);
267         return -1;
268     }
269     return 0;
270 }
271 
272 static int gmac_rx_transfer_frame_to_buffer(uint32_t rx_buf_len,
273                                             uint32_t *left_frame,
274                                             uint32_t rx_buf_addr,
275                                             bool *eof_transferred,
276                                             const uint8_t **frame_ptr,
277                                             uint16_t *transferred)
278 {
279     uint32_t to_transfer;
280     /*
281      * Check that buffer is bigger than the frame being transfered
282      * If bigger then transfer only whats left of frame
283      * Else, fill frame with all the content possible
284      */
285     if (rx_buf_len >= *left_frame) {
286         to_transfer = *left_frame;
287         *eof_transferred = true;
288     } else {
289         to_transfer = rx_buf_len;
290     }
291 
292     /* write frame part to memory */
293     if (dma_memory_write(&address_space_memory, (uint64_t) rx_buf_addr,
294                          *frame_ptr, to_transfer, MEMTXATTRS_UNSPECIFIED)) {
295         return -1;
296     }
297 
298     /* update frame pointer and size of whats left of frame */
299     *frame_ptr += to_transfer;
300     *left_frame -= to_transfer;
301     *transferred += to_transfer;
302 
303     return 0;
304 }
305 
306 static void gmac_dma_set_state(NPCMGMACState *gmac, int shift, uint32_t state)
307 {
308     gmac->regs[R_NPCM_DMA_STATUS] = deposit32(gmac->regs[R_NPCM_DMA_STATUS],
309         shift, 3, state);
310 }
311 
312 static ssize_t gmac_receive(NetClientState *nc, const uint8_t *buf, size_t len)
313 {
314     /*
315      * Comments have steps that relate to the
316      * receiving process steps in pg 386
317      */
318     NPCMGMACState *gmac = NPCM_GMAC(qemu_get_nic_opaque(nc));
319     uint32_t left_frame = len;
320     const uint8_t *frame_ptr = buf;
321     uint32_t desc_addr;
322     uint32_t rx_buf_len, rx_buf_addr;
323     struct NPCMGMACRxDesc rx_desc;
324     uint16_t transferred = 0;
325     bool eof_transferred = false;
326 
327     trace_npcm_gmac_packet_receive(DEVICE(gmac)->canonical_path, len);
328     if (!gmac_can_receive(nc)) {
329         qemu_log_mask(LOG_GUEST_ERROR, "GMAC Currently is not able for Rx");
330         return -1;
331     }
332     if (!gmac->regs[R_NPCM_DMA_HOST_RX_DESC]) {
333         gmac->regs[R_NPCM_DMA_HOST_RX_DESC] =
334             NPCM_DMA_HOST_RX_DESC_MASK(gmac->regs[R_NPCM_DMA_RX_BASE_ADDR]);
335     }
336     desc_addr = NPCM_DMA_HOST_RX_DESC_MASK(gmac->regs[R_NPCM_DMA_HOST_RX_DESC]);
337 
338     /* step 1 */
339     gmac_dma_set_state(gmac, NPCM_DMA_STATUS_RX_PROCESS_STATE_SHIFT,
340         NPCM_DMA_STATUS_RX_RUNNING_FETCHING_STATE);
341     trace_npcm_gmac_packet_desc_read(DEVICE(gmac)->canonical_path, desc_addr);
342     if (gmac_read_rx_desc(desc_addr, &rx_desc)) {
343         qemu_log_mask(LOG_GUEST_ERROR, "RX Descriptor @ 0x%x cant be read\n",
344                       desc_addr);
345         gmac_dma_set_state(gmac, NPCM_DMA_STATUS_RX_PROCESS_STATE_SHIFT,
346             NPCM_DMA_STATUS_RX_SUSPENDED_STATE);
347         return -1;
348     }
349 
350     /* step 2 */
351     if (!(rx_desc.rdes0 & RX_DESC_RDES0_OWN)) {
352         qemu_log_mask(LOG_GUEST_ERROR,
353                       "RX Descriptor @ 0x%x is owned by software\n",
354                       desc_addr);
355         gmac->regs[R_NPCM_DMA_STATUS] |= NPCM_DMA_STATUS_RU;
356         gmac->regs[R_NPCM_DMA_STATUS] |= NPCM_DMA_STATUS_RI;
357         gmac_dma_set_state(gmac, NPCM_DMA_STATUS_RX_PROCESS_STATE_SHIFT,
358             NPCM_DMA_STATUS_RX_SUSPENDED_STATE);
359         gmac_update_irq(gmac);
360         return len;
361     }
362     /* step 3 */
363     /*
364      * TODO --
365      * Implement all frame filtering and processing (with its own interrupts)
366      */
367     trace_npcm_gmac_debug_desc_data(DEVICE(gmac)->canonical_path, &rx_desc,
368                                     rx_desc.rdes0, rx_desc.rdes1, rx_desc.rdes2,
369                                     rx_desc.rdes3);
370     /* Clear rdes0 for the incoming descriptor and set FS in first descriptor.*/
371     rx_desc.rdes0 = RX_DESC_RDES0_FIRST_DESC_MASK;
372 
373     gmac_dma_set_state(gmac, NPCM_DMA_STATUS_RX_PROCESS_STATE_SHIFT,
374         NPCM_DMA_STATUS_RX_RUNNING_TRANSFERRING_STATE);
375 
376     /* Pad the frame with FCS as the kernel driver will strip it away. */
377     left_frame += ETH_FCS_LEN;
378 
379     /* repeat while we still have frame to transfer to memory */
380     while (!eof_transferred) {
381         /* Return descriptor no matter what happens */
382         rx_desc.rdes0 &= ~RX_DESC_RDES0_OWN;
383         /* Set the frame to be an IPv4/IPv6 frame. */
384         rx_desc.rdes0 |= RX_DESC_RDES0_FRM_TYPE_MASK;
385 
386         /* step 4 */
387         rx_buf_len = RX_DESC_RDES1_BFFR1_SZ_MASK(rx_desc.rdes1);
388         rx_buf_addr = rx_desc.rdes2;
389         gmac->regs[R_NPCM_DMA_CUR_RX_BUF_ADDR] = rx_buf_addr;
390         gmac_rx_transfer_frame_to_buffer(rx_buf_len, &left_frame, rx_buf_addr,
391                                          &eof_transferred, &frame_ptr,
392                                          &transferred);
393 
394         trace_npcm_gmac_packet_receiving_buffer(DEVICE(gmac)->canonical_path,
395                                                 rx_buf_len, rx_buf_addr);
396         /* if we still have frame left and the second buffer is not chained */
397          if (!(rx_desc.rdes1 & RX_DESC_RDES1_SEC_ADDR_CHND_MASK) && \
398               !eof_transferred) {
399             /* repeat process from above on buffer 2 */
400             rx_buf_len = RX_DESC_RDES1_BFFR2_SZ_MASK(rx_desc.rdes1);
401             rx_buf_addr = rx_desc.rdes3;
402             gmac->regs[R_NPCM_DMA_CUR_RX_BUF_ADDR] = rx_buf_addr;
403             gmac_rx_transfer_frame_to_buffer(rx_buf_len, &left_frame,
404                                              rx_buf_addr, &eof_transferred,
405                                              &frame_ptr, &transferred);
406             trace_npcm_gmac_packet_receiving_buffer( \
407                                                 DEVICE(gmac)->canonical_path,
408                                                 rx_buf_len, rx_buf_addr);
409         }
410         /* update address for descriptor */
411         gmac->regs[R_NPCM_DMA_HOST_RX_DESC] = rx_buf_addr;
412         /* Return descriptor */
413         rx_desc.rdes0 &= ~RX_DESC_RDES0_OWN;
414         /* Update frame length transferred */
415         rx_desc.rdes0 |= ((uint32_t)transferred)
416             << RX_DESC_RDES0_FRAME_LEN_SHIFT;
417         trace_npcm_gmac_debug_desc_data(DEVICE(gmac)->canonical_path, &rx_desc,
418                                         rx_desc.rdes0, rx_desc.rdes1,
419                                         rx_desc.rdes2, rx_desc.rdes3);
420 
421         /* step 5 */
422         gmac_write_rx_desc(desc_addr, &rx_desc);
423         trace_npcm_gmac_debug_desc_data(DEVICE(gmac)->canonical_path,
424                                         &rx_desc, rx_desc.rdes0,
425                                         rx_desc.rdes1, rx_desc.rdes2,
426                                         rx_desc.rdes3);
427         /* read new descriptor into rx_desc if needed*/
428         if (!eof_transferred) {
429             /* Get next descriptor address (chained or sequential) */
430             if (rx_desc.rdes1 & RX_DESC_RDES1_RC_END_RING_MASK) {
431                 desc_addr = gmac->regs[R_NPCM_DMA_RX_BASE_ADDR];
432             } else if (rx_desc.rdes1 & RX_DESC_RDES1_SEC_ADDR_CHND_MASK) {
433                 desc_addr = rx_desc.rdes3;
434             } else {
435                 desc_addr += sizeof(rx_desc);
436             }
437             trace_npcm_gmac_packet_desc_read(DEVICE(gmac)->canonical_path,
438                                              desc_addr);
439             if (gmac_read_rx_desc(desc_addr, &rx_desc)) {
440                 qemu_log_mask(LOG_GUEST_ERROR,
441                               "RX Descriptor @ 0x%x cant be read\n",
442                               desc_addr);
443                 gmac->regs[R_NPCM_DMA_STATUS] |= NPCM_DMA_STATUS_RU;
444                 gmac_update_irq(gmac);
445                 return len;
446             }
447 
448             /* step 6 */
449             if (!(rx_desc.rdes0 & RX_DESC_RDES0_OWN)) {
450                 if (!(gmac->regs[R_NPCM_DMA_CONTROL] & \
451                      NPCM_DMA_CONTROL_FLUSH_MASK)) {
452                     rx_desc.rdes0 |= RX_DESC_RDES0_DESC_ERR_MASK;
453                 }
454                 eof_transferred = true;
455             }
456             /* Clear rdes0 for the incoming descriptor */
457             rx_desc.rdes0 = 0;
458         }
459     }
460     gmac_dma_set_state(gmac, NPCM_DMA_STATUS_RX_PROCESS_STATE_SHIFT,
461         NPCM_DMA_STATUS_RX_RUNNING_CLOSING_STATE);
462 
463     rx_desc.rdes0 |= RX_DESC_RDES0_LAST_DESC_MASK;
464     if (!(rx_desc.rdes1 & RX_DESC_RDES1_DIS_INTR_COMP_MASK)) {
465         gmac->regs[R_NPCM_DMA_STATUS] |= NPCM_DMA_STATUS_RI;
466         gmac_update_irq(gmac);
467     }
468     trace_npcm_gmac_debug_desc_data(DEVICE(gmac)->canonical_path, &rx_desc,
469                                     rx_desc.rdes0, rx_desc.rdes1, rx_desc.rdes2,
470                                     rx_desc.rdes3);
471 
472     /* step 8 */
473     gmac->regs[R_NPCM_DMA_CONTROL] |= NPCM_DMA_CONTROL_FLUSH_MASK;
474 
475     /* step 9 */
476     trace_npcm_gmac_packet_received(DEVICE(gmac)->canonical_path, left_frame);
477     gmac_dma_set_state(gmac, NPCM_DMA_STATUS_RX_PROCESS_STATE_SHIFT,
478         NPCM_DMA_STATUS_RX_RUNNING_WAITING_STATE);
479     gmac_write_rx_desc(desc_addr, &rx_desc);
480 
481     /* Get next descriptor address (chained or sequential) */
482     if (rx_desc.rdes1 & RX_DESC_RDES1_RC_END_RING_MASK) {
483         desc_addr = gmac->regs[R_NPCM_DMA_RX_BASE_ADDR];
484     } else if (rx_desc.rdes1 & RX_DESC_RDES1_SEC_ADDR_CHND_MASK) {
485         desc_addr = rx_desc.rdes3;
486     } else {
487         desc_addr += sizeof(rx_desc);
488     }
489     gmac->regs[R_NPCM_DMA_HOST_RX_DESC] = desc_addr;
490     return len;
491 }
492 
493 static int gmac_tx_get_csum(uint32_t tdes1)
494 {
495     uint32_t mask = TX_DESC_TDES1_CHKSM_INS_CTRL_MASK(tdes1);
496     int csum = 0;
497 
498     if (likely(mask > 0)) {
499         csum |= CSUM_IP;
500     }
501     if (likely(mask > 1)) {
502         csum |= CSUM_TCP | CSUM_UDP;
503     }
504 
505     return csum;
506 }
507 
508 static void gmac_try_send_next_packet(NPCMGMACState *gmac)
509 {
510     /*
511      * Comments about steps refer to steps for
512      * transmitting in page 384 of datasheet
513      */
514     uint16_t tx_buffer_size = 2048;
515     g_autofree uint8_t *tx_send_buffer = g_malloc(tx_buffer_size);
516     uint32_t desc_addr;
517     struct NPCMGMACTxDesc tx_desc;
518     uint32_t tx_buf_addr, tx_buf_len;
519     uint16_t length = 0;
520     uint8_t *buf = tx_send_buffer;
521     uint32_t prev_buf_size = 0;
522     int csum = 0;
523 
524     /* steps 1&2 */
525     if (!gmac->regs[R_NPCM_DMA_HOST_TX_DESC]) {
526         gmac->regs[R_NPCM_DMA_HOST_TX_DESC] =
527             NPCM_DMA_HOST_TX_DESC_MASK(gmac->regs[R_NPCM_DMA_TX_BASE_ADDR]);
528     }
529     desc_addr = gmac->regs[R_NPCM_DMA_HOST_TX_DESC];
530 
531     while (true) {
532         gmac_dma_set_state(gmac, NPCM_DMA_STATUS_TX_PROCESS_STATE_SHIFT,
533             NPCM_DMA_STATUS_TX_RUNNING_FETCHING_STATE);
534         if (gmac_read_tx_desc(desc_addr, &tx_desc)) {
535             qemu_log_mask(LOG_GUEST_ERROR,
536                           "TX Descriptor @ 0x%x can't be read\n",
537                           desc_addr);
538             return;
539         }
540         /* step 3 */
541 
542         trace_npcm_gmac_packet_desc_read(DEVICE(gmac)->canonical_path,
543             desc_addr);
544         trace_npcm_gmac_debug_desc_data(DEVICE(gmac)->canonical_path, &tx_desc,
545             tx_desc.tdes0, tx_desc.tdes1, tx_desc.tdes2, tx_desc.tdes3);
546 
547         /* 1 = DMA Owned, 0 = Software Owned */
548         if (!(tx_desc.tdes0 & TX_DESC_TDES0_OWN)) {
549             trace_npcm_gmac_tx_desc_owner(DEVICE(gmac)->canonical_path,
550                                           desc_addr);
551             gmac->regs[R_NPCM_DMA_STATUS] |= NPCM_DMA_STATUS_TU;
552             gmac_dma_set_state(gmac, NPCM_DMA_STATUS_TX_PROCESS_STATE_SHIFT,
553                 NPCM_DMA_STATUS_TX_SUSPENDED_STATE);
554             gmac_update_irq(gmac);
555             return;
556         }
557 
558         gmac_dma_set_state(gmac, NPCM_DMA_STATUS_TX_PROCESS_STATE_SHIFT,
559             NPCM_DMA_STATUS_TX_RUNNING_READ_STATE);
560         /* Give the descriptor back regardless of what happens. */
561         tx_desc.tdes0 &= ~TX_DESC_TDES0_OWN;
562 
563         if (tx_desc.tdes1 & TX_DESC_TDES1_FIRST_SEG_MASK) {
564             csum = gmac_tx_get_csum(tx_desc.tdes1);
565         }
566 
567         /* step 4 */
568         tx_buf_addr = tx_desc.tdes2;
569         gmac->regs[R_NPCM_DMA_CUR_TX_BUF_ADDR] = tx_buf_addr;
570         tx_buf_len = TX_DESC_TDES1_BFFR1_SZ_MASK(tx_desc.tdes1);
571         buf = &tx_send_buffer[prev_buf_size];
572 
573         if ((prev_buf_size + tx_buf_len) > sizeof(buf)) {
574             tx_buffer_size = prev_buf_size + tx_buf_len;
575             tx_send_buffer = g_realloc(tx_send_buffer, tx_buffer_size);
576             buf = &tx_send_buffer[prev_buf_size];
577         }
578 
579         /* step 5 */
580         if (dma_memory_read(&address_space_memory, tx_buf_addr, buf,
581                             tx_buf_len, MEMTXATTRS_UNSPECIFIED)) {
582             qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to read packet @ 0x%x\n",
583                         __func__, tx_buf_addr);
584             return;
585         }
586         length += tx_buf_len;
587         prev_buf_size += tx_buf_len;
588 
589         /* If not chained we'll have a second buffer. */
590         if (!(tx_desc.tdes1 & TX_DESC_TDES1_SEC_ADDR_CHND_MASK)) {
591             tx_buf_addr = tx_desc.tdes3;
592             gmac->regs[R_NPCM_DMA_CUR_TX_BUF_ADDR] = tx_buf_addr;
593             tx_buf_len = TX_DESC_TDES1_BFFR2_SZ_MASK(tx_desc.tdes1);
594             buf = &tx_send_buffer[prev_buf_size];
595 
596             if ((prev_buf_size + tx_buf_len) > sizeof(buf)) {
597                 tx_buffer_size = prev_buf_size + tx_buf_len;
598                 tx_send_buffer = g_realloc(tx_send_buffer, tx_buffer_size);
599                 buf = &tx_send_buffer[prev_buf_size];
600             }
601 
602             if (dma_memory_read(&address_space_memory, tx_buf_addr, buf,
603                                 tx_buf_len, MEMTXATTRS_UNSPECIFIED)) {
604                 qemu_log_mask(LOG_GUEST_ERROR,
605                               "%s: Failed to read packet @ 0x%x\n",
606                               __func__, tx_buf_addr);
607                 return;
608             }
609             length += tx_buf_len;
610             prev_buf_size += tx_buf_len;
611         }
612         if (tx_desc.tdes1 & TX_DESC_TDES1_LAST_SEG_MASK) {
613             net_checksum_calculate(tx_send_buffer, length, csum);
614             qemu_send_packet(qemu_get_queue(gmac->nic), tx_send_buffer, length);
615             trace_npcm_gmac_packet_sent(DEVICE(gmac)->canonical_path, length);
616             buf = tx_send_buffer;
617             length = 0;
618         }
619 
620         /* step 6 */
621         gmac_dma_set_state(gmac, NPCM_DMA_STATUS_TX_PROCESS_STATE_SHIFT,
622             NPCM_DMA_STATUS_TX_RUNNING_CLOSING_STATE);
623         gmac_write_tx_desc(desc_addr, &tx_desc);
624         if (tx_desc.tdes1 & TX_DESC_TDES1_TX_END_RING_MASK) {
625             desc_addr = gmac->regs[R_NPCM_DMA_TX_BASE_ADDR];
626         } else if (tx_desc.tdes1 & TX_DESC_TDES1_SEC_ADDR_CHND_MASK) {
627             desc_addr = tx_desc.tdes3;
628         } else {
629             desc_addr += sizeof(tx_desc);
630         }
631         gmac->regs[R_NPCM_DMA_HOST_TX_DESC] = desc_addr;
632 
633         /* step 7 */
634         if (tx_desc.tdes1 & TX_DESC_TDES1_INTERR_COMP_MASK) {
635             gmac->regs[R_NPCM_DMA_STATUS] |= NPCM_DMA_STATUS_TI;
636             gmac_update_irq(gmac);
637         }
638     }
639 }
640 
641 static void gmac_cleanup(NetClientState *nc)
642 {
643     /* Nothing to do yet. */
644 }
645 
646 static void gmac_set_link(NetClientState *nc)
647 {
648     NPCMGMACState *gmac = qemu_get_nic_opaque(nc);
649 
650     trace_npcm_gmac_set_link(!nc->link_down);
651     gmac_phy_set_link(gmac, !nc->link_down);
652 }
653 
654 static void npcm_gmac_mdio_access(NPCMGMACState *gmac, uint16_t v)
655 {
656     bool busy = v & NPCM_GMAC_MII_ADDR_BUSY;
657     uint8_t is_write;
658     uint8_t pa, gr;
659     uint16_t data;
660 
661     if (busy) {
662         is_write = v & NPCM_GMAC_MII_ADDR_WRITE;
663         pa = NPCM_GMAC_MII_ADDR_PA(v);
664         gr = NPCM_GMAC_MII_ADDR_GR(v);
665         /* Both pa and gr are 5 bits, so they are less than 32. */
666         g_assert(pa < NPCM_GMAC_MAX_PHYS);
667         g_assert(gr < NPCM_GMAC_MAX_PHY_REGS);
668 
669 
670         if (v & NPCM_GMAC_MII_ADDR_WRITE) {
671             data = gmac->regs[R_NPCM_GMAC_MII_DATA];
672             /* Clear reset bit for BMCR register */
673             switch (gr) {
674             case MII_BMCR:
675                 data &= ~MII_BMCR_RESET;
676                 /* Autonegotiation is a W1C bit*/
677                 if (data & MII_BMCR_ANRESTART) {
678                     /* Tells autonegotiation to not restart again */
679                     data &= ~MII_BMCR_ANRESTART;
680                 }
681                 if ((data & MII_BMCR_AUTOEN) &&
682                     !(gmac->phy_regs[pa][MII_BMSR] & MII_BMSR_AN_COMP)) {
683                     /* sets autonegotiation as complete */
684                     gmac->phy_regs[pa][MII_BMSR] |= MII_BMSR_AN_COMP;
685                     /* Resolve AN automatically->need to set this */
686                     gmac->phy_regs[0][MII_ANLPAR] = 0x0000;
687                 }
688             }
689             gmac->phy_regs[pa][gr] = data;
690         } else {
691             data = gmac->phy_regs[pa][gr];
692             gmac->regs[R_NPCM_GMAC_MII_DATA] = data;
693         }
694         trace_npcm_gmac_mdio_access(DEVICE(gmac)->canonical_path, is_write, pa,
695                                         gr, data);
696     }
697     gmac->regs[R_NPCM_GMAC_MII_ADDR] = v & ~NPCM_GMAC_MII_ADDR_BUSY;
698 }
699 
700 static uint64_t npcm_gmac_read(void *opaque, hwaddr offset, unsigned size)
701 {
702     NPCMGMACState *gmac = opaque;
703     uint32_t v = 0;
704 
705     switch (offset) {
706     /* Write only registers */
707     case A_NPCM_DMA_XMT_POLL_DEMAND:
708     case A_NPCM_DMA_RCV_POLL_DEMAND:
709         qemu_log_mask(LOG_GUEST_ERROR,
710                       "%s: Read of write-only reg: offset: 0x%04" HWADDR_PRIx
711                       "\n", DEVICE(gmac)->canonical_path, offset);
712         break;
713 
714     default:
715         v = gmac->regs[offset / sizeof(uint32_t)];
716     }
717 
718     trace_npcm_gmac_reg_read(DEVICE(gmac)->canonical_path, offset, v);
719     return v;
720 }
721 
722 static void npcm_gmac_write(void *opaque, hwaddr offset,
723                               uint64_t v, unsigned size)
724 {
725     NPCMGMACState *gmac = opaque;
726 
727     trace_npcm_gmac_reg_write(DEVICE(gmac)->canonical_path, offset, v);
728 
729     switch (offset) {
730     /* Read only registers */
731     case A_NPCM_GMAC_VERSION:
732     case A_NPCM_GMAC_INT_STATUS:
733     case A_NPCM_GMAC_RGMII_STATUS:
734     case A_NPCM_GMAC_PTP_STSR:
735     case A_NPCM_GMAC_PTP_STNSR:
736     case A_NPCM_DMA_MISSED_FRAME_CTR:
737     case A_NPCM_DMA_HOST_TX_DESC:
738     case A_NPCM_DMA_HOST_RX_DESC:
739     case A_NPCM_DMA_CUR_TX_BUF_ADDR:
740     case A_NPCM_DMA_CUR_RX_BUF_ADDR:
741     case A_NPCM_DMA_HW_FEATURE:
742         qemu_log_mask(LOG_GUEST_ERROR,
743                       "%s: Write of read-only reg: offset: 0x%04" HWADDR_PRIx
744                       ", value: 0x%04" PRIx64 "\n",
745                       DEVICE(gmac)->canonical_path, offset, v);
746         break;
747 
748     case A_NPCM_GMAC_MAC_CONFIG:
749         gmac->regs[offset / sizeof(uint32_t)] = v;
750         break;
751 
752     case A_NPCM_GMAC_MII_ADDR:
753         npcm_gmac_mdio_access(gmac, v);
754         break;
755 
756     case A_NPCM_GMAC_MAC0_ADDR_HI:
757         gmac->regs[offset / sizeof(uint32_t)] = v;
758         gmac->conf.macaddr.a[0] = v >> 8;
759         gmac->conf.macaddr.a[1] = v >> 0;
760         break;
761 
762     case A_NPCM_GMAC_MAC0_ADDR_LO:
763         gmac->regs[offset / sizeof(uint32_t)] = v;
764         gmac->conf.macaddr.a[2] = v >> 24;
765         gmac->conf.macaddr.a[3] = v >> 16;
766         gmac->conf.macaddr.a[4] = v >> 8;
767         gmac->conf.macaddr.a[5] = v >> 0;
768         break;
769 
770     case A_NPCM_GMAC_MAC1_ADDR_HI:
771     case A_NPCM_GMAC_MAC1_ADDR_LO:
772     case A_NPCM_GMAC_MAC2_ADDR_HI:
773     case A_NPCM_GMAC_MAC2_ADDR_LO:
774     case A_NPCM_GMAC_MAC3_ADDR_HI:
775     case A_NPCM_GMAC_MAC3_ADDR_LO:
776         gmac->regs[offset / sizeof(uint32_t)] = v;
777         qemu_log_mask(LOG_UNIMP,
778                       "%s: Only MAC Address 0 is supported. This request "
779                       "is ignored.\n", DEVICE(gmac)->canonical_path);
780         break;
781 
782     case A_NPCM_DMA_BUS_MODE:
783         gmac->regs[offset / sizeof(uint32_t)] = v;
784         if (v & NPCM_DMA_BUS_MODE_SWR) {
785             npcm_gmac_soft_reset(gmac);
786         }
787         break;
788 
789     case A_NPCM_DMA_RCV_POLL_DEMAND:
790         /* We dont actually care about the value */
791         gmac_dma_set_state(gmac, NPCM_DMA_STATUS_RX_PROCESS_STATE_SHIFT,
792             NPCM_DMA_STATUS_RX_RUNNING_WAITING_STATE);
793         break;
794 
795     case A_NPCM_DMA_XMT_POLL_DEMAND:
796         /* We dont actually care about the value */
797         gmac_try_send_next_packet(gmac);
798         break;
799 
800     case A_NPCM_DMA_CONTROL:
801         gmac->regs[offset / sizeof(uint32_t)] = v;
802         if (v & NPCM_DMA_CONTROL_START_STOP_TX) {
803             gmac_try_send_next_packet(gmac);
804         } else {
805             gmac_dma_set_state(gmac, NPCM_DMA_STATUS_TX_PROCESS_STATE_SHIFT,
806                 NPCM_DMA_STATUS_TX_STOPPED_STATE);
807         }
808         if (v & NPCM_DMA_CONTROL_START_STOP_RX) {
809             gmac_dma_set_state(gmac, NPCM_DMA_STATUS_RX_PROCESS_STATE_SHIFT,
810                 NPCM_DMA_STATUS_RX_RUNNING_WAITING_STATE);
811             qemu_flush_queued_packets(qemu_get_queue(gmac->nic));
812         } else {
813             gmac_dma_set_state(gmac, NPCM_DMA_STATUS_RX_PROCESS_STATE_SHIFT,
814                 NPCM_DMA_STATUS_RX_STOPPED_STATE);
815         }
816         break;
817 
818     case A_NPCM_DMA_STATUS:
819         /* Check that RO bits are not written to */
820         if (NPCM_DMA_STATUS_RO_MASK(v)) {
821             qemu_log_mask(LOG_GUEST_ERROR,
822                           "%s: Write of read-only bits of reg: offset: 0x%04"
823                            HWADDR_PRIx ", value: 0x%04" PRIx64 "\n",
824                            DEVICE(gmac)->canonical_path, offset, v);
825         }
826         /* for W1C bits, implement W1C */
827         gmac->regs[offset / sizeof(uint32_t)] &= ~NPCM_DMA_STATUS_W1C_MASK(v);
828         if (v & NPCM_DMA_STATUS_RU) {
829             /* Clearing RU bit indicates descriptor is owned by DMA again. */
830             gmac_dma_set_state(gmac, NPCM_DMA_STATUS_RX_PROCESS_STATE_SHIFT,
831                 NPCM_DMA_STATUS_RX_RUNNING_WAITING_STATE);
832             qemu_flush_queued_packets(qemu_get_queue(gmac->nic));
833         }
834         break;
835 
836     default:
837         gmac->regs[offset / sizeof(uint32_t)] = v;
838         break;
839     }
840 
841     gmac_update_irq(gmac);
842 }
843 
844 static void npcm_gmac_reset(DeviceState *dev)
845 {
846     NPCMGMACState *gmac = NPCM_GMAC(dev);
847 
848     npcm_gmac_soft_reset(gmac);
849     memcpy(gmac->phy_regs[0], phy_reg_init, sizeof(phy_reg_init));
850 
851     trace_npcm_gmac_reset(DEVICE(gmac)->canonical_path,
852                           gmac->phy_regs[0][MII_BMSR]);
853 }
854 
855 static NetClientInfo net_npcm_gmac_info = {
856     .type = NET_CLIENT_DRIVER_NIC,
857     .size = sizeof(NICState),
858     .can_receive = gmac_can_receive,
859     .receive = gmac_receive,
860     .cleanup = gmac_cleanup,
861     .link_status_changed = gmac_set_link,
862 };
863 
864 static const struct MemoryRegionOps npcm_gmac_ops = {
865     .read = npcm_gmac_read,
866     .write = npcm_gmac_write,
867     .endianness = DEVICE_LITTLE_ENDIAN,
868     .valid = {
869         .min_access_size = 4,
870         .max_access_size = 4,
871         .unaligned = false,
872     },
873 };
874 
875 static void npcm_gmac_realize(DeviceState *dev, Error **errp)
876 {
877     NPCMGMACState *gmac = NPCM_GMAC(dev);
878     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
879 
880     memory_region_init_io(&gmac->iomem, OBJECT(gmac), &npcm_gmac_ops, gmac,
881                           TYPE_NPCM_GMAC, 8 * KiB);
882     sysbus_init_mmio(sbd, &gmac->iomem);
883     sysbus_init_irq(sbd, &gmac->irq);
884 
885     qemu_macaddr_default_if_unset(&gmac->conf.macaddr);
886 
887     gmac->nic = qemu_new_nic(&net_npcm_gmac_info, &gmac->conf, TYPE_NPCM_GMAC,
888                              dev->id, &dev->mem_reentrancy_guard, gmac);
889     qemu_format_nic_info_str(qemu_get_queue(gmac->nic), gmac->conf.macaddr.a);
890     gmac->regs[R_NPCM_GMAC_MAC0_ADDR_HI] = (gmac->conf.macaddr.a[0] << 8) + \
891                                             gmac->conf.macaddr.a[1];
892     gmac->regs[R_NPCM_GMAC_MAC0_ADDR_LO] = (gmac->conf.macaddr.a[2] << 24) + \
893                                            (gmac->conf.macaddr.a[3] << 16) + \
894                                            (gmac->conf.macaddr.a[4] << 8) + \
895                                             gmac->conf.macaddr.a[5];
896 }
897 
898 static void npcm_gmac_unrealize(DeviceState *dev)
899 {
900     NPCMGMACState *gmac = NPCM_GMAC(dev);
901 
902     qemu_del_nic(gmac->nic);
903 }
904 
905 static const VMStateDescription vmstate_npcm_gmac = {
906     .name = TYPE_NPCM_GMAC,
907     .version_id = 0,
908     .minimum_version_id = 0,
909     .fields = (VMStateField[]) {
910         VMSTATE_UINT32_ARRAY(regs, NPCMGMACState, NPCM_GMAC_NR_REGS),
911         VMSTATE_END_OF_LIST(),
912     },
913 };
914 
915 static Property npcm_gmac_properties[] = {
916     DEFINE_NIC_PROPERTIES(NPCMGMACState, conf),
917     DEFINE_PROP_END_OF_LIST(),
918 };
919 
920 static void npcm_gmac_class_init(ObjectClass *klass, void *data)
921 {
922     DeviceClass *dc = DEVICE_CLASS(klass);
923 
924     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
925     dc->desc = "NPCM GMAC Controller";
926     dc->realize = npcm_gmac_realize;
927     dc->unrealize = npcm_gmac_unrealize;
928     device_class_set_legacy_reset(dc, npcm_gmac_reset);
929     dc->vmsd = &vmstate_npcm_gmac;
930     device_class_set_props(dc, npcm_gmac_properties);
931 }
932 
933 static const TypeInfo npcm_gmac_types[] = {
934     {
935         .name = TYPE_NPCM_GMAC,
936         .parent = TYPE_SYS_BUS_DEVICE,
937         .instance_size = sizeof(NPCMGMACState),
938         .class_init = npcm_gmac_class_init,
939     },
940 };
941 DEFINE_TYPES(npcm_gmac_types)
942