Debian Codegraph @

common/dev_gt.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 *
5 * Galileo GT64010/GT64120A/GT96100A system controller.
6 *
7 * The DMA stuff is not complete, only "normal" transfers are working
8 * (source and destination addresses incrementing).
9 *
10 * Also, these transfers are "instantaneous" from a CPU point-of-view: when
11 * a channel is enabled, the transfer is immediately done. So, this is not
12 * very realistic.
13 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stddef.h>
19
20 #include "utils.h"
21 #include "net.h"
22 #include "cpu.h"
23 #include "vm.h"
24 #include "dynamips.h"
25 #include "memory.h"
26 #include "device.h"
27 #include "net_io.h"
28 #include "ptask.h"
29 #include "dev_gt.h"
30
31 /* Debugging flags */
32 #define DEBUG_UNKNOWN 0
33 #define DEBUG_DMA 0
34 #define DEBUG_SDMA 0
35 #define DEBUG_MPSC 0
36 #define DEBUG_MII 0
37 #define DEBUG_ETH 0
38 #define DEBUG_ETH_TX 0
39 #define DEBUG_ETH_RX 0
40 #define DEBUG_ETH_HASH 0
41
42 /* PCI identification */
43 #define PCI_VENDOR_GALILEO 0x11ab /* Galileo Technology */
44 #define PCI_PRODUCT_GALILEO_GT64010 0x0146 /* GT-64010 */
45 #define PCI_PRODUCT_GALILEO_GT64011 0x4146 /* GT-64011 */
46 #define PCI_PRODUCT_GALILEO_GT64120 0x4620 /* GT-64120 */
47 #define PCI_PRODUCT_GALILEO_GT96100 0x9653 /* GT-96100 */
48
49 /* === Global definitions ================================================= */
50
51 /* Interrupt High Cause Register */
52 #define GT_IHCR_ETH0_SUM 0x00000001
53 #define GT_IHCR_ETH1_SUM 0x00000002
54 #define GT_IHCR_SDMA_SUM 0x00000010
55
56 /* Serial Cause Register */
57 #define GT_SCR_ETH0_SUM 0x00000001
58 #define GT_SCR_ETH1_SUM 0x00000002
59 #define GT_SCR_SDMA_SUM 0x00000010
60 #define GT_SCR_SDMA0_SUM 0x00000100
61 #define GT_SCR_MPSC0_SUM 0x00000200
62
63 /* === DMA definitions ==================================================== */
64 #define GT_DMA_CHANNELS 4
65
66 #define GT_DMA_FLYBY_ENABLE 0x00000001 /* FlyBy Enable */
67 #define GT_DMA_FLYBY_RDWR 0x00000002 /* SDRAM Read/Write (FlyBy) */
68 #define GT_DMA_SRC_DIR 0x0000000c /* Source Direction */
69 #define GT_DMA_DST_DIR 0x00000030 /* Destination Direction */
70 #define GT_DMA_DATA_LIMIT 0x000001c0 /* Data Transfer Limit */
71 #define GT_DMA_CHAIN_MODE 0x00000200 /* Chained Mode */
72 #define GT_DMA_INT_MODE 0x00000400 /* Interrupt Mode */
73 #define GT_DMA_TRANS_MODE 0x00000800 /* Transfer Mode */
74 #define GT_DMA_CHAN_ENABLE 0x00001000 /* Channel Enable */
75 #define GT_DMA_FETCH_NEXT 0x00002000 /* Fetch Next Record */
76 #define GT_DMA_ACT_STATUS 0x00004000 /* DMA Activity Status */
77 #define GT_DMA_SDA 0x00008000 /* Source/Destination Alignment */
78 #define GT_DMA_MDREQ 0x00010000 /* Mask DMA Requests */
79 #define GT_DMA_CDE 0x00020000 /* Close Descriptor Enable */
80 #define GT_DMA_EOTE 0x00040000 /* End-of-Transfer (EOT) Enable */
81 #define GT_DMA_EOTIE 0x00080000 /* EOT Interrupt Enable */
82 #define GT_DMA_ABORT 0x00100000 /* Abort DMA Transfer */
83 #define GT_DMA_SLP 0x00600000 /* Override Source Address */
84 #define GT_DMA_DLP 0x01800000 /* Override Dest Address */
85 #define GT_DMA_RLP 0x06000000 /* Override Record Address */
86 #define GT_DMA_REQ_SRC 0x10000000 /* DMA Request Source */
87
88 /* Galileo DMA channel */
89 struct dma_channel {
90 m_uint32_t byte_count;
91 m_uint32_t src_addr;
92 m_uint32_t dst_addr;
93 m_uint32_t cdptr;
94 m_uint32_t nrptr;
95 m_uint32_t ctrl;
96 };
97
98 /* === Serial DMA (SDMA) ================================================== */
99
100 /* SDMA: 2 groups of 8 channels */
101 #define GT_SDMA_CHANNELS 8
102 #define GT_SDMA_GROUPS 2
103
104 /* SDMA channel */
105 struct sdma_channel {
106 u_int id;
107
108 m_uint32_t sdc;
109 m_uint32_t sdcm;
110 m_uint32_t rx_desc;
111 m_uint32_t rx_buf_ptr;
112 m_uint32_t scrdp;
113 m_uint32_t tx_desc;
114 m_uint32_t sctdp;
115 m_uint32_t sftdp;
116 };
117
118 /* SGCR: SDMA Group Register */
119 #define GT_REG_SGC 0x101af0
120
121 /* SDMA cause register: 8 fields (1 for each channel) of 4 bits */
122 #define GT_SDMA_CAUSE_RXBUF0 0x01
123 #define GT_SDMA_CAUSE_RXERR0 0x02
124 #define GT_SDMA_CAUSE_TXBUF0 0x04
125 #define GT_SDMA_CAUSE_TXEND0 0x08
126
127 /* SDMA channel register offsets */
128 #define GT_SDMA_SDC 0x000900 /* Configuration Register */
129 #define GT_SDMA_SDCM 0x000908 /* Command Register */
130 #define GT_SDMA_RX_DESC 0x008900 /* RX descriptor */
131 #define GT_SDMA_SCRDP 0x008910 /* Current RX descriptor */
132 #define GT_SDMA_TX_DESC 0x00c900 /* TX descriptor */
133 #define GT_SDMA_SCTDP 0x00c910 /* Current TX desc. pointer */
134 #define GT_SDMA_SFTDP 0x00c914 /* First TX desc. pointer */
135
136 /* SDCR: SDMA Configuration Register */
137 #define GT_SDCR_RFT 0x00000001 /* Receive FIFO Threshold */
138 #define GT_SDCR_SFM 0x00000002 /* Single Frame Mode */
139 #define GT_SDCR_RC 0x0000003c /* Retransmit count */
140 #define GT_SDCR_BLMR 0x00000040 /* Big/Little Endian RX mode */
141 #define GT_SDCR_BLMT 0x00000080 /* Big/Litlle Endian TX mode */
142 #define GT_SDCR_POVR 0x00000100 /* PCI override */
143 #define GT_SDCR_RIFB 0x00000200 /* RX IRQ on frame boundary */
144 #define GT_SDCR_BSZ 0x00003000 /* Burst size */
145
146 /* SDCMR: SDMA Command Register */
147 #define GT_SDCMR_ERD 0x00000080 /* Enable RX DMA */
148 #define GT_SDCMR_AR 0x00008000 /* Abort Receive */
149 #define GT_SDCMR_STD 0x00010000 /* Stop TX */
150 #define GT_SDCMR_STDH GT_SDCMR_STD /* Stop TX High */
151 #define GT_SDCMR_STDL 0x00020000 /* Stop TX Low */
152 #define GT_SDCMR_TXD 0x00800000 /* TX Demand */
153 #define GT_SDCMR_TXDH GT_SDCMR_TXD /* Start TX High */
154 #define GT_SDCMR_TXDL 0x01000000 /* Start TX Low */
155 #define GT_SDCMR_AT 0x80000000 /* Abort Transmit */
156
157 /* SDMA RX/TX descriptor */
158 struct sdma_desc {
159 m_uint32_t buf_size;
160 m_uint32_t cmd_stat;
161 m_uint32_t next_ptr;
162 m_uint32_t buf_ptr;
163 };
164
165 /* SDMA Descriptor Command/Status word */
166 #define GT_SDMA_CMD_O 0x80000000 /* Owner bit */
167 #define GT_SDMA_CMD_AM 0x40000000 /* Auto-mode */
168 #define GT_SDMA_CMD_EI 0x00800000 /* Enable Interrupt */
169 #define GT_SDMA_CMD_F 0x00020000 /* First buffer */
170 #define GT_SDMA_CMD_L 0x00010000 /* Last buffer */
171
172 #define GT_SDMA_CMD_OFFSET offsetof(struct sdma_desc,cmd_stat) /* Offset of the Command/Status word */
173
174 /* === MultiProtocol Serial Controller (MPSC) ============================= */
175
176 /* 8 MPSC channels */
177 #define GT_MPSC_CHANNELS 8
178
179 /* MPSC channel */
180 struct mpsc_channel {
181 m_uint32_t mmcrl;
182 m_uint32_t mmcrh;
183 m_uint32_t mpcr;
184 m_uint32_t chr[10];
185
186 vtty_t *vtty;
187 netio_desc_t *nio;
188 };
189
190 #define GT_MPSC_MMCRL 0x000A00 /* Main Config Register Low */
191 #define GT_MPSC_MMCRH 0x000A04 /* Main Config Register High */
192 #define GT_MPSC_MPCR 0x000A08 /* Protocol Config Register */
193 #define GT_MPSC_CHR1 0x000A0C
194 #define GT_MPSC_CHR2 0x000A10
195 #define GT_MPSC_CHR3 0x000A14
196 #define GT_MPSC_CHR4 0x000A18
197 #define GT_MPSC_CHR5 0x000A1C
198 #define GT_MPSC_CHR6 0x000A20
199 #define GT_MPSC_CHR7 0x000A24
200 #define GT_MPSC_CHR8 0x000A28
201 #define GT_MPSC_CHR9 0x000A2C
202 #define GT_MPSC_CHR10 0x000A30
203
204 #define GT_MMCRL_MODE_MASK 0x0000007
205
206 #define GT_MPSC_MODE_HDLC 0
207 #define GT_MPSC_MODE_UART 4
208 #define GT_MPSC_MODE_BISYNC 5
209
210 /* === Ethernet definitions =============================================== */
211 #define GT_ETH_PORTS 2
212 #define GT_MAX_PKT_SIZE 2048
213
214 /* SMI register */
215 #define GT_SMIR_DATA_MASK 0x0000FFFF
216 #define GT_SMIR_PHYAD_MASK 0x001F0000 /* PHY Device Address */
217 #define GT_SMIR_PHYAD_SHIFT 16
218 #define GT_SMIR_REGAD_MASK 0x03e00000 /* PHY Device Register Address */
219 #define GT_SMIR_REGAD_SHIFT 21
220 #define GT_SMIR_OPCODE_MASK 0x04000000 /* Opcode (0: write, 1: read) */
221 #define GT_SMIR_OPCODE_READ 0x04000000
222 #define GT_SMIR_RVALID_FLAG 0x08000000 /* Read Valid */
223 #define GT_SMIR_BUSY_FLAG 0x10000000 /* Busy: 1=op in progress */
224
225 /* PCR: Port Configuration Register */
226 #define GT_PCR_PM 0x00000001 /* Promiscuous mode */
227 #define GT_PCR_RBM 0x00000002 /* Reject broadcast mode */
228 #define GT_PCR_PBF 0x00000004 /* Pass bad frames */
229 #define GT_PCR_EN 0x00000080 /* Port Enabled/Disabled */
230 #define GT_PCR_LPBK 0x00000300 /* Loopback mode */
231 #define GT_PCR_FC 0x00000400 /* Force collision */
232 #define GT_PCR_HS 0x00001000 /* Hash size */
233 #define GT_PCR_HM 0x00002000 /* Hash mode */
234 #define GT_PCR_HDM 0x00004000 /* Hash default mode */
235 #define GT_PCR_HD 0x00008000 /* Duplex Mode */
236 #define GT_PCR_ISL 0x70000000 /* ISL enabled (0x06) */
237 #define GT_PCR_ACCS 0x80000000 /* Accelerate Slot Time */
238
239 /* PCXR: Port Configuration Extend Register */
240 #define GT_PCXR_IGMP 0x00000001 /* IGMP packet capture */
241 #define GT_PCXR_SPAN 0x00000002 /* BPDU packet capture */
242 #define GT_PCXR_PAR 0x00000004 /* Partition Enable */
243 #define GT_PCXR_PRIOTX 0x00000038 /* Priority weight for TX */
244 #define GT_PCXR_PRIORX 0x000000C0 /* Priority weight for RX */
245 #define GT_PCXR_PRIORX_OV 0x00000100 /* Prio RX override */
246 #define GT_PCXR_DPLX_EN 0x00000200 /* Autoneg for Duplex */
247 #define GT_PCXR_FCTL_EN 0x00000400 /* Autoneg for 802.3x */
248 #define GT_PCXR_FLP 0x00000800 /* Force Link Pass */
249 #define GT_PCXR_FCTL 0x00001000 /* Flow Control Mode */
250 #define GT_PCXR_MFL 0x0000C000 /* Maximum Frame Length */
251 #define GT_PCXR_MIB_CLR_MODE 0x00010000 /* MIB counters clear mode */
252 #define GT_PCXR_SPEED 0x00040000 /* Port Speed */
253 #define GT_PCXR_SPEED_EN 0x00080000 /* Autoneg for Speed */
254 #define GT_PCXR_RMII_EN 0x00100000 /* RMII Enable */
255 #define GT_PCXR_DSCP_EN 0x00200000 /* DSCP decoding enable */
256
257 /* PCMR: Port Command Register */
258 #define GT_PCMR_FJ 0x00008000 /* Force Jam / Flow Control */
259
260 /* PSR: Port Status Register */
261 #define GT_PSR_SPEED 0x00000001 /* Speed: 10/100 Mb/s (100=>1)*/
262 #define GT_PSR_DUPLEX 0x00000002 /* Duplex (1: full) */
263 #define GT_PSR_FCTL 0x00000004 /* Flow Control Mode */
264 #define GT_PSR_LINK 0x00000008 /* Link Up/Down */
265 #define GT_PSR_PAUSE 0x00000010 /* Flow-control disabled state */
266 #define GT_PSR_TXLOW 0x00000020 /* TX Low priority status */
267 #define GT_PSR_TXHIGH 0x00000040 /* TX High priority status */
268 #define GT_PSR_TXINP 0x00000080 /* TX in Progress */
269
270 /* ICR: Interrupt Cause Register */
271 #define GT_ICR_RXBUF 0x00000001 /* RX Buffer returned to host */
272 #define GT_ICR_TXBUFH 0x00000004 /* TX Buffer High */
273 #define GT_ICR_TXBUFL 0x00000008 /* TX Buffer Low */
274 #define GT_ICR_TXENDH 0x00000040 /* TX End High */
275 #define GT_ICR_TXENDL 0x00000080 /* TX End Low */
276 #define GT_ICR_RXERR 0x00000100 /* RX Error */
277 #define GT_ICR_TXERRH 0x00000400 /* TX Error High */
278 #define GT_ICR_TXERRL 0x00000800 /* TX Error Low */
279 #define GT_ICR_RXOVR 0x00001000 /* RX Overrun */
280 #define GT_ICR_TXUDR 0x00002000 /* TX Underrun */
281 #define GT_ICR_RXBUFQ0 0x00010000 /* RX Buffer in Prio Queue 0 */
282 #define GT_ICR_RXBUFQ1 0x00020000 /* RX Buffer in Prio Queue 1 */
283 #define GT_ICR_RXBUFQ2 0x00040000 /* RX Buffer in Prio Queue 2 */
284 #define GT_ICR_RXBUFQ3 0x00080000 /* RX Buffer in Prio Queue 3 */
285 #define GT_ICR_RXERRQ0 0x00010000 /* RX Error in Prio Queue 0 */
286 #define GT_ICR_RXERRQ1 0x00020000 /* RX Error in Prio Queue 1 */
287 #define GT_ICR_RXERRQ2 0x00040000 /* RX Error in Prio Queue 2 */
288 #define GT_ICR_RXERRQ3 0x00080000 /* RX Error in Prio Queue 3 */
289 #define GT_ICR_MII_STC 0x10000000 /* MII PHY Status Change */
290 #define GT_ICR_SMI_DONE 0x20000000 /* SMI Command Done */
291 #define GT_ICR_INT_SUM 0x80000000 /* Ethernet Interrupt Summary */
292 #define GT_ICR_MASK 0x7FFFFFFF
293
294 /* Ethernet hash entry */
295 #define GT_HTE_VALID 0x00000001 /* Valid entry */
296 #define GT_HTE_SKIP 0x00000002 /* Skip entry in a chain */
297 #define GT_HTE_RD 0x00000004 /* 0: Discard, 1: Receive */
298 #define GT_HTE_ADDR_MASK 0x7fffffffffff8ULL
299
300 #define GT_HTE_HOPNUM 12 /* Hash Table Hop Number */
301
302 enum {
303 GT_HTLOOKUP_MISS,
304 GT_HTLOOKUP_MATCH,
305 GT_HTLOOKUP_HOP_EXCEEDED,
306 };
307
308 /* TX Descriptor */
309 #define GT_TXDESC_OWN 0x80000000 /* Ownership */
310 #define GT_TXDESC_AM 0x40000000 /* Auto-mode */
311 #define GT_TXDESC_EI 0x00800000 /* Enable Interrupt */
312 #define GT_TXDESC_GC 0x00400000 /* Generate CRC */
313 #define GT_TXDESC_P 0x00040000 /* Padding */
314 #define GT_TXDESC_F 0x00020000 /* First buffer of packet */
315 #define GT_TXDESC_L 0x00010000 /* Last buffer of packet */
316 #define GT_TXDESC_ES 0x00008000 /* Error Summary */
317 #define GT_TXDESC_RC 0x00003c00 /* Retransmit Count */
318 #define GT_TXDESC_COL 0x00000200 /* Collision */
319 #define GT_TXDESC_RL 0x00000100 /* Retransmit Limit Error */
320 #define GT_TXDESC_UR 0x00000040 /* Underrun Error */
321 #define GT_TXDESC_LC 0x00000020 /* Late Collision Error */
322
323 #define GT_TXDESC_BC_MASK 0xFFFF0000 /* Number of bytes to transmit */
324 #define GT_TXDESC_BC_SHIFT 16
325
326 /* RX Descriptor */
327 #define GT_RXDESC_OWN 0x80000000 /* Ownership */
328 #define GT_RXDESC_AM 0x40000000 /* Auto-mode */
329 #define GT_RXDESC_EI 0x00800000 /* Enable Interrupt */
330 #define GT_RXDESC_F 0x00020000 /* First buffer of packet */
331 #define GT_RXDESC_L 0x00010000 /* Last buffer of packet */
332 #define GT_RXDESC_ES 0x00008000 /* Error Summary */
333 #define GT_RXDESC_IGMP 0x00004000 /* IGMP packet detected */
334 #define GT_RXDESC_HE 0x00002000 /* Hash Table Expired */
335 #define GT_RXDESC_M 0x00001000 /* Dst MAC Miss in Hash Table */
336 #define GT_RXDESC_FT 0x00000800 /* Frame Type (802.3/Ethernet) */
337 #define GT_RXDESC_SF 0x00000100 /* Short Frame Error */
338 #define GT_RXDESC_MFL 0x00000080 /* Maximum Frame Length Error */
339 #define GT_RXDESC_OR 0x00000040 /* Overrun Error */
340 #define GT_RXDESC_COL 0x00000010 /* Collision */
341 #define GT_RXDESC_CE 0x00000001 /* CRC Error */
342
343 #define GT_RXDESC_BC_MASK 0x0000FFFF /* Byte count */
344 #define GT_RXDESC_BS_MASK 0xFFFF0000 /* Buffer size */
345 #define GT_RXDESC_BS_SHIFT 16
346
347 /* Galileo Ethernet port */
348 struct eth_port {
349 netio_desc_t *nio;
350
351 /* First and Current RX descriptors (4 queues) */
352 m_uint32_t rx_start[4],rx_current[4];
353
354 /* Current TX descriptors (2 queues) */
355 m_uint32_t tx_current[2];
356
357 /* Port registers */
358 m_uint32_t pcr,pcxr,pcmr,psr;
359
360 /* SDMA registers */
361 m_uint32_t sdcr,sdcmr;
362
363 /* Interrupt registers */
364 m_uint32_t icr,imr;
365
366 /* Hash Table pointer */
367 m_uint32_t ht_addr;
368
369 /* Ethernet MIB counters */
370 m_uint32_t rx_bytes,tx_bytes,rx_frames,tx_frames;
371 };
372
373 /* ======================================================================== */
374
375 /* Galileo GT64xxx/GT96xxx system controller */
376 struct gt_data {
377 char *name;
378 vm_obj_t vm_obj;
379 struct vdevice dev;
380 struct pci_device *pci_dev;
381 vm_instance_t *vm;
382 pthread_mutex_t lock;
383
384 struct pci_bus *bus[2];
385 struct dma_channel dma[GT_DMA_CHANNELS];
386
387 /* Interrupts (common) */
388 m_uint32_t int_cause_reg;
389 m_uint32_t int_high_cause_reg;
390 m_uint32_t int_mask_reg;
391
392 /* Interrupts (GT96100) */
393 m_uint32_t int0_main_mask_reg,int0_high_mask_reg;
394 m_uint32_t int1_main_mask_reg,int1_high_mask_reg;
395 m_uint32_t ser_cause_reg;
396 m_uint32_t serint0_mask_reg,serint1_mask_reg;
397 u_int int0_irq,int1_irq,serint0_irq,serint1_irq;
398
399 /* SDMA - Serial DMA (GT96100) */
400 m_uint32_t sgcr;
401 m_uint32_t sdma_cause_reg,sdma_mask_reg;
402 struct sdma_channel sdma[GT_SDMA_GROUPS][GT_SDMA_CHANNELS];
403
404 /* MPSC - MultiProtocol Serial Controller (GT96100) */
405 struct mpsc_channel mpsc[GT_MPSC_CHANNELS];
406
407 /* Ethernet ports (GT96100) */
408 u_int eth_irq;
409 ptask_id_t eth_tx_tid;
410 struct eth_port eth_ports[GT_ETH_PORTS];
411 m_uint32_t smi_reg;
412 m_uint16_t mii_regs[32][32];
413
414 /* IRQ status update */
415 void (*gt_update_irq_status)(struct gt_data *gt_data);
416 };
417
418 #define GT_LOCK(d) pthread_mutex_lock(&(d)->lock)
419 #define GT_UNLOCK(d) pthread_mutex_unlock(&(d)->lock)
420
421 /* Log a GT message */
422 #define GT_LOG(d,msg...) vm_log((d)->vm,(d)->name,msg)
423
424 /* Update the interrupt status */
425 static void gt64k_update_irq_status(struct gt_data *gt_data)
426 {
427 if (gt_data->pci_dev) {
428 if (gt_data->int_cause_reg & gt_data->int_mask_reg)
429 pci_dev_trigger_irq(gt_data->vm,gt_data->pci_dev);
430 else
431 pci_dev_clear_irq(gt_data->vm,gt_data->pci_dev);
432 }
433 }
434
435 /* Fetch a DMA record (chained mode) */
436 static void gt_dma_fetch_rec(vm_instance_t *vm,struct dma_channel *channel)
437 {
438 m_uint32_t ptr;
439
440 #if DEBUG_DMA
441 vm_log(vm,"GT_DMA","fetching record at address 0x%x\n",channel->nrptr);
442 #endif
443
444 /* fetch the record from RAM */
445 ptr = channel->nrptr;
446 channel->byte_count = swap32(physmem_copy_u32_from_vm(vm,ptr));
447 channel->src_addr = swap32(physmem_copy_u32_from_vm(vm,ptr+0x04));
448 channel->dst_addr = swap32(physmem_copy_u32_from_vm(vm,ptr+0x08));
449 channel->nrptr = swap32(physmem_copy_u32_from_vm(vm,ptr+0x0c));
450
451 /* clear the "fetch next record bit" */
452 channel->ctrl &= ~GT_DMA_FETCH_NEXT;
453 }
454
455 /* Handle control register of a DMA channel */
456 static void gt_dma_handle_ctrl(struct gt_data *gt_data,int chan_id)
457 {
458 struct dma_channel *channel = &gt_data->dma[chan_id];
459 vm_instance_t *vm = gt_data->vm;
460 int done;
461
462 if (channel->ctrl & GT_DMA_FETCH_NEXT) {
463 if (channel->nrptr == 0) {
464 vm_log(vm,"GT_DMA","trying to load a NULL DMA record...\n");
465 return;
466 }
467
468 gt_dma_fetch_rec(vm,channel);
469 }
470
471 if (channel->ctrl & GT_DMA_CHAN_ENABLE)
472 {
473 do {
474 done = TRUE;
475
476 #if DEBUG_DMA
477 vm_log(vm,"GT_DMA",
478 "starting transfer from 0x%x to 0x%x (size=%u bytes)\n",
479 channel->src_addr,channel->dst_addr,
480 channel->byte_count & 0xFFFF);
481 #endif
482 physmem_dma_transfer(vm,channel->src_addr,channel->dst_addr,
483 channel->byte_count & 0xFFFF);
484
485 /* chained mode */
486 if (!(channel->ctrl & GT_DMA_CHAIN_MODE)) {
487 if (channel->nrptr) {
488 gt_dma_fetch_rec(vm,channel);
489 done = FALSE;
490 }
491 }
492 }while(!done);
493
494 #if DEBUG_DMA
495 vm_log(vm,"GT_DMA","finished transfer.\n");
496 #endif
497 /* Trigger DMA interrupt */
498 gt_data->int_cause_reg |= 1 << (4 + chan_id);
499 gt_data->gt_update_irq_status(gt_data);
500 }
501 }
502
503 #define DMA_REG(ch,reg_name) \
504 if (op_type == MTS_WRITE) \
505 gt_data->dma[ch].reg_name = *data; \
506 else \
507 *data = gt_data->dma[ch].reg_name;
508
509 /* Handle a DMA channel */
510 static int gt_dma_access(cpu_gen_t *cpu,struct vdevice *dev,
511 m_uint32_t offset,u_int op_size,u_int op_type,
512 m_uint64_t *data)
513 {
514 struct gt_data *gt_data = dev->priv_data;
515
516 switch(offset) {
517 /* DMA Source Address */
518 case 0x810: DMA_REG(0,src_addr); return(1);
519 case 0x814: DMA_REG(1,src_addr); return(1);
520 case 0x818: DMA_REG(2,src_addr); return(1);
521 case 0x81c: DMA_REG(3,src_addr); return(1);
522
523 /* DMA Destination Address */
524 case 0x820: DMA_REG(0,dst_addr); return(1);
525 case 0x824: DMA_REG(1,dst_addr); return(1);
526 case 0x828: DMA_REG(2,dst_addr); return(1);
527 case 0x82c: DMA_REG(3,dst_addr); return(1);
528
529 /* DMA Next Record Pointer */
530 case 0x830:
531 gt_data->dma[0].cdptr = *data;
532 DMA_REG(0,nrptr);
533 return(1);
534
535 case 0x834:
536 gt_data->dma[1].cdptr = *data;
537 DMA_REG(1,nrptr);
538 return(1);
539
540 case 0x838:
541 gt_data->dma[2].cdptr = *data;
542 DMA_REG(2,nrptr);
543 return(1);
544
545 case 0x83c:
546 gt_data->dma[3].cdptr = *data;
547 DMA_REG(3,nrptr);
548 return(1);
549
550 /* DMA Channel Control */
551 case 0x840:
552 DMA_REG(0,ctrl);
553 if (op_type == MTS_WRITE)
554 gt_dma_handle_ctrl(gt_data,0);
555 return(1);
556
557 case 0x844:
558 DMA_REG(1,ctrl);
559 if (op_type == MTS_WRITE)
560 gt_dma_handle_ctrl(gt_data,1);
561 return(1);
562
563 case 0x848:
564 DMA_REG(2,ctrl);
565 if (op_type == MTS_WRITE)
566 gt_dma_handle_ctrl(gt_data,2);
567 return(1);
568
569 case 0x84c:
570 DMA_REG(3,ctrl);
571 if (op_type == MTS_WRITE)
572 gt_dma_handle_ctrl(gt_data,3);
573 return(1);
574 }
575
576 return(0);
577 }
578
579 /*
580 * dev_gt64010_access()
581 */
582 void *dev_gt64010_access(cpu_gen_t *cpu,struct vdevice *dev,m_uint32_t offset,
583 u_int op_size,u_int op_type,m_uint64_t *data)
584 {
585 struct gt_data *gt_data = dev->priv_data;
586
587 if (op_type == MTS_READ) {
588 *data = 0;
589 } else {
590 *data = swap32(*data);
591 }
592
593 if (gt_dma_access(cpu,dev,offset,op_size,op_type,data) != 0)
594 goto done;
595
596 switch(offset) {
597 /* ===== DRAM Settings (completely faked, 128 Mb) ===== */
598 case 0x008: /* ras10_low */
599 if (op_type == MTS_READ)
600 *data = 0x000;
601 break;
602 case 0x010: /* ras10_high */
603 if (op_type == MTS_READ)
604 *data = 0x7F;
605 break;
606 case 0x018: /* ras32_low */
607 if (op_type == MTS_READ)
608 *data = 0x080;
609 break;
610 case 0x020: /* ras32_high */
611 if (op_type == MTS_READ)
612 *data = 0x7F;
613 break;
614 case 0x400: /* ras0_low */
615 if (op_type == MTS_READ)
616 *data = 0x00;
617 break;
618 case 0x404: /* ras0_high */
619 if (op_type == MTS_READ)
620 *data = 0xFF;
621 break;
622 case 0x408: /* ras1_low */
623 if (op_type == MTS_READ)
624 *data = 0x7F;
625 break;
626 case 0x40c: /* ras1_high */
627 if (op_type == MTS_READ)
628 *data = 0x00;
629 break;
630 case 0x410: /* ras2_low */
631 if (op_type == MTS_READ)
632 *data = 0x00;
633 break;
634 case 0x414: /* ras2_high */
635 if (op_type == MTS_READ)
636 *data = 0xFF;
637 break;
638 case 0x418: /* ras3_low */
639 if (op_type == MTS_READ)
640 *data = 0x7F;
641 break;
642 case 0x41c: /* ras3_high */
643 if (op_type == MTS_READ)
644 *data = 0x00;
645 break;
646 case 0xc08: /* pci0_cs10 */
647 if (op_type == MTS_READ)
648 *data = 0xFFF;
649 break;
650 case 0xc0c: /* pci0_cs32 */
651 if (op_type == MTS_READ)
652 *data = 0xFFF;
653 break;
654
655 case 0xc00: /* pci_cmd */
656 if (op_type == MTS_READ)
657 *data = 0x00008001;
658 break;
659
660 /* ===== Interrupt Cause Register ===== */
661 case 0xc18:
662 if (op_type == MTS_READ) {
663 *data = gt_data->int_cause_reg;
664 } else {
665 gt_data->int_cause_reg &= *data;
666 gt64k_update_irq_status(gt_data);
667 }
668 break;
669
670 /* ===== Interrupt Mask Register ===== */
671 case 0xc1c:
672 if (op_type == MTS_READ)
673 *data = gt_data->int_mask_reg;
674 else {
675 gt_data->int_mask_reg = *data;
676 gt64k_update_irq_status(gt_data);
677 }
678 break;
679
680 /* ===== PCI Configuration ===== */
681 case PCI_BUS_ADDR: /* pci configuration address (0xcf8) */
682 pci_dev_addr_handler(cpu,gt_data->bus[0],op_type,FALSE,data);
683 break;
684
685 case PCI_BUS_DATA: /* pci data address (0xcfc) */
686 pci_dev_data_handler(cpu,gt_data->bus[0],op_type,FALSE,data);
687 break;
688
689 #if DEBUG_UNKNOWN
690 default:
691 if (op_type == MTS_READ) {
692 cpu_log(cpu,"GT64010","read from unknown addr 0x%x, pc=0x%llx\n",
693 offset,cpu_get_pc(cpu));
694 } else {
695 cpu_log(cpu,"GT64010","write to unknown addr 0x%x, value=0x%llx, "
696 "pc=0x%llx\n",offset,*data,cpu_get_pc(cpu));
697 }
698 #endif
699 }
700
701 done:
702 if (op_type == MTS_READ)
703 *data = swap32(*data);
704 return NULL;
705 }
706
707 /*
708 * dev_gt64120_access()
709 */
710 void *dev_gt64120_access(cpu_gen_t *cpu,struct vdevice *dev,m_uint32_t offset,
711 u_int op_size,u_int op_type,m_uint64_t *data)
712 {
713 struct gt_data *gt_data = dev->priv_data;
714
715 if (op_type == MTS_READ) {
716 *data = 0;
717 } else {
718 *data = swap32(*data);
719 }
720
721 if (gt_dma_access(cpu,dev,offset,op_size,op_type,data) != 0)
722 goto done;
723
724 switch(offset) {
725 case 0x008: /* ras10_low */
726 if (op_type == MTS_READ)
727 *data = 0x000;
728 break;
729 case 0x010: /* ras10_high */
730 if (op_type == MTS_READ)
731 *data = 0x7F;
732 break;
733 case 0x018: /* ras32_low */
734 if (op_type == MTS_READ)
735 *data = 0x100;
736 break;
737 case 0x020: /* ras32_high */
738 if (op_type == MTS_READ)
739 *data = 0x7F;
740 break;
741 case 0x400: /* ras0_low */
742 if (op_type == MTS_READ)
743 *data = 0x00;
744 break;
745 case 0x404: /* ras0_high */
746 if (op_type == MTS_READ)
747 *data = 0xFF;
748 break;
749 case 0x408: /* ras1_low */
750 if (op_type == MTS_READ)
751 *data = 0x7F;
752 break;
753 case 0x40c: /* ras1_high */
754 if (op_type == MTS_READ)
755 *data = 0x00;
756 break;
757 case 0x410: /* ras2_low */
758 if (op_type == MTS_READ)
759 *data = 0x00;
760 break;
761 case 0x414: /* ras2_high */
762 if (op_type == MTS_READ)
763 *data = 0xFF;
764 break;
765 case 0x418: /* ras3_low */
766 if (op_type == MTS_READ)
767 *data = 0x7F;
768 break;
769 case 0x41c: /* ras3_high */
770 if (op_type == MTS_READ)
771 *data = 0x00;
772 break;
773 case 0xc08: /* pci0_cs10 */
774 if (op_type == MTS_READ)
775 *data = 0xFFF;
776 break;
777 case 0xc0c: /* pci0_cs32 */
778 if (op_type == MTS_READ)
779 *data = 0xFFF;
780 break;
781
782 case 0xc00: /* pci_cmd */
783 if (op_type == MTS_READ)
784 *data = 0x00008001;
785 break;
786
787 /* ===== Interrupt Cause Register ===== */
788 case 0xc18:
789 if (op_type == MTS_READ)
790 *data = gt_data->int_cause_reg;
791 else {
792 gt_data->int_cause_reg &= *data;
793 gt64k_update_irq_status(gt_data);
794 }
795 break;
796
797 /* ===== Interrupt Mask Register ===== */
798 case 0xc1c:
799 if (op_type == MTS_READ) {
800 *data = gt_data->int_mask_reg;
801 } else {
802 gt_data->int_mask_reg = *data;
803 gt64k_update_irq_status(gt_data);
804 }
805 break;
806
807 /* ===== PCI Bus 1 ===== */
808 case 0xcf0:
809 pci_dev_addr_handler(cpu,gt_data->bus[1],op_type,FALSE,data);
810 break;
811
812 case 0xcf4:
813 pci_dev_data_handler(cpu,gt_data->bus[1],op_type,FALSE,data);
814 break;
815
816 /* ===== PCI Bus 0 ===== */
817 case PCI_BUS_ADDR: /* pci configuration address (0xcf8) */
818 pci_dev_addr_handler(cpu,gt_data->bus[0],op_type,FALSE,data);
819 break;
820
821 case PCI_BUS_DATA: /* pci data address (0xcfc) */
822 pci_dev_data_handler(cpu,gt_data->bus[0],op_type,FALSE,data);
823 break;
824
825 #if DEBUG_UNKNOWN
826 default:
827 if (op_type == MTS_READ) {
828 cpu_log(cpu,"GT64120","read from unknown addr 0x%x, pc=0x%llx\n",
829 offset,cpu_get_pc(cpu));
830 } else {
831 cpu_log(cpu,"GT64120","write to unknown addr 0x%x, value=0x%llx, "
832 "pc=0x%llx\n",offset,*data,cpu_get_pc(cpu));
833 }
834 #endif
835 }
836
837 done:
838 if (op_type == MTS_READ)
839 *data = swap32(*data);
840 return NULL;
841 }
842
843 /* ======================================================================== */
844 /* GT96k Interrupts */
845 /* ======================================================================== */
846 static void gt96k_update_irq_status(struct gt_data *d)
847 {
848 /* Interrupt0* active ? */
849 if ((d->int_cause_reg & d->int0_main_mask_reg) ||
850 (d->int_high_cause_reg & d->int0_high_mask_reg))
851 {
852 d->int_cause_reg |= 1 << 30;
853 vm_set_irq(d->vm,d->int0_irq);
854 }
855 else
856 {
857 d->int_cause_reg &= ~(1 << 30);
858 vm_clear_irq(d->vm,d->int0_irq);
859 }
860
861 /* Interrupt1* active ? */
862 if ((d->int_cause_reg & d->int1_main_mask_reg) ||
863 (d->int_high_cause_reg & d->int1_high_mask_reg))
864 {
865 d->int_cause_reg |= 1 << 31;
866 vm_set_irq(d->vm,d->int1_irq);
867 }
868 else
869 {
870 d->int_cause_reg &= ~(1 << 31);
871 vm_clear_irq(d->vm,d->int1_irq);
872 }
873
874 /* SerInt0* active ? */
875 if (d->ser_cause_reg & d->serint0_mask_reg) {
876 vm_set_irq(d->vm,d->serint0_irq);
877 } else {
878 vm_clear_irq(d->vm,d->serint0_irq);
879 }
880
881 /* SerInt1* active ? */
882 if (d->ser_cause_reg & d->serint1_mask_reg) {
883 vm_set_irq(d->vm,d->serint1_irq);
884 } else {
885 vm_clear_irq(d->vm,d->serint1_irq);
886 }
887 }
888
889 /* ======================================================================== */
890 /* SDMA (Serial DMA) */
891 /* ======================================================================== */
892
893 /* Update SDMA interrupt status */
894 static void gt_sdma_update_int_status(struct gt_data *d)
895 {
896 /* Update general SDMA status */
897 if (d->sdma_cause_reg & d->sdma_mask_reg) {
898 d->ser_cause_reg |= GT_SCR_SDMA_SUM;
899 d->int_high_cause_reg |= GT_IHCR_SDMA_SUM;
900 } else {
901 d->ser_cause_reg &= ~GT_SCR_SDMA_SUM;
902 d->int_high_cause_reg &= ~GT_IHCR_SDMA_SUM;
903 }
904
905 gt96k_update_irq_status(d);
906 }
907
908 /* Update SDMA interrupt status for the specified channel */
909 static void gt_sdma_update_channel_int_status(struct gt_data *d,u_int chan_id)
910 {
911 m_uint32_t ch_st;
912
913 /* Get the status of the specified SDMA channel */
914 ch_st = d->sdma_cause_reg & (0x0000000F << (chan_id << 2));
915
916 if (ch_st)
917 d->ser_cause_reg |= GT_SCR_SDMA0_SUM << (chan_id << 1);
918 else
919 d->ser_cause_reg &= ~(GT_SCR_SDMA0_SUM << (chan_id << 1));
920
921 gt_sdma_update_int_status(d);
922 }
923
924 /* Set SDMA cause register for a channel */
925 static inline void gt_sdma_set_cause(struct gt_data *d,u_int chan_id,
926 u_int value)
927 {
928 d->sdma_cause_reg |= value << (chan_id << 2);
929 }
930
931 /* Read a SDMA descriptor from memory */
932 static void gt_sdma_desc_read(struct gt_data *d,m_uint32_t addr,
933 struct sdma_desc *desc)
934 {
935 physmem_copy_from_vm(d->vm,desc,addr,sizeof(struct sdma_desc));
936
937 /* byte-swapping */
938 desc->buf_size = vmtoh32(desc->buf_size);
939 desc->cmd_stat = vmtoh32(desc->cmd_stat);
940 desc->next_ptr = vmtoh32(desc->next_ptr);
941 desc->buf_ptr = vmtoh32(desc->buf_ptr);
942 }
943
944 /* Write a SDMA descriptor to memory */
945 static void gt_sdma_desc_write(struct gt_data *d,m_uint32_t addr,
946 struct sdma_desc *desc)
947 {
948 struct sdma_desc tmp;
949
950 /* byte-swapping */
951 tmp.cmd_stat = vmtoh32(desc->cmd_stat);
952 tmp.buf_size = vmtoh32(desc->buf_size);
953 tmp.next_ptr = vmtoh32(desc->next_ptr);
954 tmp.buf_ptr = vmtoh32(desc->buf_ptr);
955
956 physmem_copy_to_vm(d->vm,&tmp,addr,sizeof(struct sdma_desc));
957 }
958
959 /* Send contents of a SDMA buffer */
960 static void gt_sdma_send_buffer(struct gt_data *d,u_int chan_id,
961 u_char *buffer,m_uint32_t len)
962 {
963 struct mpsc_channel *channel;
964 u_int mode;
965
966 channel = &d->mpsc[chan_id];
967 mode = channel->mmcrl & GT_MMCRL_MODE_MASK;
968
969 switch(mode) {
970 case GT_MPSC_MODE_HDLC:
971 if (channel->nio != NULL)
972 netio_send(channel->nio,buffer,len);
973 break;
974
975 case GT_MPSC_MODE_UART:
976 if (channel->vtty != NULL)
977 vtty_put_buffer(channel->vtty,(char *)buffer,len);
978 break;
979 }
980 }
981
982 /* Start TX DMA process */
983 static int gt_sdma_tx_start(struct gt_data *d,struct sdma_channel *chan)
984 {
985 u_char pkt[GT_MAX_PKT_SIZE],*pkt_ptr;
986 struct sdma_desc txd0,ctxd,*ptxd;
987 m_uint32_t tx_start,tx_current;
988 m_uint32_t len,tot_len;
989 int abort = FALSE;
990
991 tx_start = tx_current = chan->sctdp;
992
993 if (!tx_start)
994 return(FALSE);
995
996 ptxd = &txd0;
997 gt_sdma_desc_read(d,tx_start,ptxd);
998
999 /* If we don't own the first descriptor, we cannot transmit */
1000 if (!(txd0.cmd_stat & GT_TXDESC_OWN))
1001 return(FALSE);
1002
1003 /* Empty packet for now */
1004 pkt_ptr = pkt;
1005 tot_len = 0;
1006
1007 for(;;)
1008 {
1009 /* Copy packet data to the buffer */
1010 len = (ptxd->buf_size & GT_TXDESC_BC_MASK) >> GT_TXDESC_BC_SHIFT;
1011
1012 physmem_copy_from_vm(d->vm,pkt_ptr,ptxd->buf_ptr,len);
1013 pkt_ptr += len;
1014 tot_len += len;
1015
1016 /* Clear the OWN bit if this is not the first descriptor */
1017 if (!(ptxd->cmd_stat & GT_TXDESC_F)) {
1018 ptxd->cmd_stat &= ~GT_TXDESC_OWN;
1019 physmem_copy_u32_to_vm(d->vm,tx_current+GT_SDMA_CMD_OFFSET,ptxd->cmd_stat);
1020 }
1021
1022 tx_current = ptxd->next_ptr;
1023
1024 /* Last descriptor or no more desc available ? */
1025 if (ptxd->cmd_stat & GT_TXDESC_L)
1026 break;
1027
1028 if (!tx_current) {
1029 abort = TRUE;
1030 break;
1031 }
1032
1033 /* Fetch the next descriptor */
1034 gt_sdma_desc_read(d,tx_current,&ctxd);
1035 ptxd = &ctxd;
1036 }
1037
1038 if ((tot_len != 0) && !abort) {
1039 #if DEBUG_SDMA
1040 GT_LOG(d,"SDMA%u: sending packet of %u bytes\n",tot_len);
1041 mem_dump(d->vm->log_fd,pkt,tot_len);
1042 #endif
1043 /* send it on wire */
1044 gt_sdma_send_buffer(d,chan->id,pkt,tot_len);
1045
1046 /* Signal that a TX buffer has been transmitted */
1047 gt_sdma_set_cause(d,chan->id,GT_SDMA_CAUSE_TXBUF0);
1048 }
1049
1050 /* Clear the OWN flag of the first descriptor */
1051 txd0.cmd_stat &= ~GT_TXDESC_OWN;
1052 physmem_copy_u32_to_vm(d->vm,tx_start+GT_SDMA_CMD_OFFSET,txd0.cmd_stat);
1053
1054 chan->sctdp = tx_current;
1055
1056 if (abort || !tx_current) {
1057 gt_sdma_set_cause(d,chan->id,GT_SDMA_CAUSE_TXEND0);
1058 chan->sdcm &= ~GT_SDCMR_TXD;
1059 }
1060
1061 /* Update interrupt status */
1062 gt_sdma_update_channel_int_status(d,chan->id);
1063 return(TRUE);
1064 }
1065
1066 /* Put a packet in buffer of a descriptor */
1067 static void gt_sdma_rxdesc_put_pkt(struct gt_data *d,struct sdma_desc *rxd,
1068 u_char **pkt,ssize_t *pkt_len)
1069 {
1070 ssize_t len,cp_len;
1071
1072 len = (rxd->buf_size & GT_RXDESC_BS_MASK) >> GT_RXDESC_BS_SHIFT;
1073
1074 /* compute the data length to copy */
1075 cp_len = m_min(len,*pkt_len);
1076
1077 /* copy packet data to the VM physical RAM */
1078 physmem_copy_to_vm(d->vm,*pkt,rxd->buf_ptr,cp_len);
1079
1080 /* set the byte count in descriptor */
1081 rxd->buf_size |= cp_len;
1082
1083 *pkt += cp_len;
1084 *pkt_len -= cp_len;
1085 }
1086
1087 /* Put a packet into SDMA buffers */
1088 static int gt_sdma_handle_rxqueue(struct gt_data *d,
1089 struct sdma_channel *channel,
1090 u_char *pkt,ssize_t pkt_len)
1091 {
1092 m_uint32_t rx_start,rx_current;
1093 struct sdma_desc rxd0,rxdn,*rxdc;
1094 ssize_t tot_len = pkt_len;
1095 u_char *pkt_ptr = pkt;
1096 int i;
1097
1098 /* Truncate the packet if it is too big */
1099 pkt_len = m_min(pkt_len,GT_MAX_PKT_SIZE);
1100
1101 /* Copy the first RX descriptor */
1102 if (!(rx_start = rx_current = channel->scrdp))
1103 goto dma_error;
1104
1105 /* Load the first RX descriptor */
1106 gt_sdma_desc_read(d,rx_start,&rxd0);
1107
1108 #if DEBUG_SDMA
1109 GT_LOG(d,"SDMA channel %u: reading desc at 0x%8.8x "
1110 "[buf_size=0x%8.8x,cmd_stat=0x%8.8x,"
1111 "next_ptr=0x%8.8x,buf_ptr=0x%8.8x]\n",
1112 channel->id,rx_start,rxd0.buf_size,rxd0.cmd_stat,
1113 rxd0.next_ptr,rxd0.buf_ptr);
1114 #endif
1115
1116 for(i=0,rxdc=&rxd0;tot_len>0;i++)
1117 {
1118 /* We must own the descriptor */
1119 if (!(rxdc->cmd_stat & GT_RXDESC_OWN))
1120 goto dma_error;
1121
1122 /* Put data into the descriptor buffer */
1123 gt_sdma_rxdesc_put_pkt(d,rxdc,&pkt_ptr,&tot_len);
1124
1125 /* Clear the OWN bit */
1126 rxdc->cmd_stat &= ~GT_RXDESC_OWN;
1127
1128 /* We have finished if the complete packet has been stored */
1129 if (tot_len == 0) {
1130 rxdc->cmd_stat |= GT_RXDESC_L;
1131 rxdc->buf_size += 2; /* Add 2 bytes for CRC */
1132 }
1133
1134 /* Update the descriptor in host memory (but not the 1st) */
1135 if (i != 0)
1136 gt_sdma_desc_write(d,rx_current,rxdc);
1137
1138 /* Get address of the next descriptor */
1139 rx_current = rxdc->next_ptr;
1140
1141 if (tot_len == 0)
1142 break;
1143
1144 if (!rx_current)
1145 goto dma_error;
1146
1147 /* Read the next descriptor from VM physical RAM */
1148 gt_sdma_desc_read(d,rx_current,&rxdn);
1149 rxdc = &rxdn;
1150 }
1151
1152 /* Update the RX pointers */
1153 channel->scrdp = rx_current;
1154
1155 /* Update the first RX descriptor */
1156 rxd0.cmd_stat |= GT_RXDESC_F;
1157 gt_sdma_desc_write(d,rx_start,&rxd0);
1158
1159 /* Indicate that we have a frame ready */
1160 gt_sdma_set_cause(d,channel->id,GT_SDMA_CAUSE_RXBUF0);
1161 gt_sdma_update_channel_int_status(d,channel->id);
1162 return(TRUE);
1163
1164 dma_error:
1165 gt_sdma_set_cause(d,channel->id,GT_SDMA_CAUSE_RXERR0);
1166 gt_sdma_update_channel_int_status(d,channel->id);
1167 return(FALSE);
1168 }
1169
1170 /* Handle RX packet for a SDMA channel */
1171 static int gt_sdma_handle_rx_pkt(netio_desc_t *nio,
1172 u_char *pkt,ssize_t pkt_len,
1173 struct gt_data *d,void *arg)
1174 {
1175 struct sdma_channel *channel;
1176 u_int chan_id = (u_int)(u_long)arg;
1177 u_int group_id;
1178
1179 GT_LOCK(d);
1180
1181 /* Find the SDMA group associated to the MPSC channel for receiving */
1182 group_id = (d->sgcr >> chan_id) & 0x01;
1183 channel = &d->sdma[group_id][chan_id];
1184
1185 gt_sdma_handle_rxqueue(d,channel,pkt,pkt_len);
1186 GT_UNLOCK(d);
1187 return(TRUE);
1188 }
1189
1190 /* Handle a SDMA channel */
1191 static int gt_sdma_access(cpu_gen_t *cpu,struct vdevice *dev,
1192 m_uint32_t offset,u_int op_size,u_int op_type,
1193 m_uint64_t *data)
1194 {
1195 struct gt_data *gt_data = dev->priv_data;
1196 struct sdma_channel *channel;
1197 u_int group,chan_id,reg;
1198
1199 if ((offset & 0x000F00) != 0x000900)
1200 return(FALSE);
1201
1202 /* Decode group, channel and register */
1203 group = (offset >> 20) & 0x0F;
1204 chan_id = (offset >> 16) & 0x0F;
1205 reg = offset & 0xFFFF;
1206
1207 if ((group >= GT_SDMA_GROUPS) || (chan_id >= GT_SDMA_CHANNELS)) {
1208 cpu_log(cpu,"GT96100","invalid SDMA register 0x%8.8x\n",offset);
1209 return(TRUE);
1210 }
1211
1212 channel = &gt_data->sdma[group][chan_id];
1213
1214 #if 0
1215 printf("SDMA: access to reg 0x%6.6x (group=%u, channel=%u)\n",
1216 offset, group, chan_id);
1217 #endif
1218
1219 switch(reg) {
1220 /* Configuration Register */
1221 case GT_SDMA_SDC:
1222 break;
1223
1224 /* Command Register */
1225 case GT_SDMA_SDCM:
1226 if (op_type == MTS_WRITE) {
1227 channel->sdcm = *data;
1228
1229 if (channel->sdcm & GT_SDCMR_TXD) {
1230 #if DEBUG_SDMA
1231 cpu_log(cpu,"GT96100-SDMA","starting TX transfer (%u/%u)\n",
1232 group,chan_id);
1233 #endif
1234 while(gt_sdma_tx_start(gt_data,channel))
1235 ;
1236 }
1237 } else {
1238 *data = 0xFF; //0xFFFFFFFF;
1239 }
1240 break;
1241
1242 /* Current RX descriptor */
1243 case GT_SDMA_SCRDP:
1244 if (op_type == MTS_READ)
1245 *data = channel->scrdp;
1246 else
1247 channel->scrdp = *data;
1248 break;
1249
1250 /* Current TX desc. pointer */
1251 case GT_SDMA_SCTDP:
1252 if (op_type == MTS_READ)
1253 *data = channel->sctdp;
1254 else
1255 channel->sctdp = *data;
1256 break;
1257
1258 /* First TX desc. pointer */
1259 case GT_SDMA_SFTDP:
1260 if (op_type == MTS_READ)
1261 *data = channel->sftdp;
1262 else
1263 channel->sftdp = *data;
1264 break;
1265
1266 default:
1267 /* unknown/unmanaged register */
1268 return(FALSE);
1269 }
1270
1271 return(TRUE);
1272 }
1273
1274 /* ======================================================================== */
1275 /* MPSC (MultiProtocol Serial Controller) */
1276 /* ======================================================================== */
1277
1278 /* Handle a MPSC channel */
1279 static int gt_mpsc_access(cpu_gen_t *cpu,struct vdevice *dev,
1280 m_uint32_t offset,u_int op_size,u_int op_type,
1281 m_uint64_t *data)
1282 {
1283 struct gt_data *gt_data = dev->priv_data;
1284 struct mpsc_channel *channel;
1285 u_int chan_id,reg,reg2;
1286
1287 if ((offset & 0x000F00) != 0x000A00)
1288 return(FALSE);
1289
1290 /* Decode channel ID and register */
1291 chan_id = offset >> 15;
1292 reg = offset & 0xFFF;
1293
1294 if (chan_id >= GT_MPSC_CHANNELS)
1295 return(FALSE);
1296
1297 channel = &gt_data->mpsc[chan_id];
1298
1299 switch(reg) {
1300 /* Main Config Register Low */
1301 case GT_MPSC_MMCRL:
1302 if (op_type == MTS_READ) {
1303 *data = channel->mmcrl;
1304 } else {
1305 #if DEBUG_MPSC
1306 GT_LOG(gt_data,"MPSC channel %u set in mode %llu\n",
1307 chan_id,*data & 0x07);
1308 #endif
1309 channel->mmcrl = *data;
1310 }
1311 break;
1312
1313 /* Main Config Register High */
1314 case GT_MPSC_MMCRH:
1315 if (op_type == MTS_READ)
1316 *data = channel->mmcrh;
1317 else
1318 channel->mmcrh = *data;
1319 break;
1320
1321 /* Protocol Config Register */
1322 case GT_MPSC_MPCR:
1323 if (op_type == MTS_READ)
1324 *data = channel->mpcr;
1325 else
1326 channel->mpcr = *data;
1327 break;
1328
1329 /* Channel registers */
1330 case GT_MPSC_CHR1:
1331 case GT_MPSC_CHR2:
1332 case GT_MPSC_CHR3:
1333 case GT_MPSC_CHR4:
1334 case GT_MPSC_CHR5:
1335 case GT_MPSC_CHR6:
1336 case GT_MPSC_CHR7:
1337 case GT_MPSC_CHR8:
1338 case GT_MPSC_CHR9:
1339 //case GT_MPSC_CHR10:
1340 reg2 = (reg - GT_MPSC_CHR1) >> 2;
1341 if (op_type == MTS_READ)
1342 *data = channel->chr[reg2];
1343 else
1344 channel->chr[reg2] = *data;
1345 break;
1346
1347 case GT_MPSC_CHR10:
1348 if (op_type == MTS_READ)
1349 *data = channel->chr[9] | 0x20;
1350 else
1351 channel->chr[9] = *data;
1352 break;
1353
1354 default:
1355 /* unknown/unmanaged register */
1356 return(FALSE);
1357 }
1358
1359 return(TRUE);
1360 }
1361
1362 /* Set NIO for a MPSC channel */
1363 int dev_gt96100_mpsc_set_nio(struct gt_data *d,u_int chan_id,
1364 netio_desc_t *nio)
1365 {
1366 struct mpsc_channel *channel;
1367
1368 if (chan_id >= GT_MPSC_CHANNELS)
1369 return(-1);
1370
1371 channel = &d->mpsc[chan_id];
1372
1373 if (channel->nio != NULL)
1374 return(-1);
1375
1376 channel->nio = nio;
1377 netio_rxl_add(nio,(netio_rx_handler_t)gt_sdma_handle_rx_pkt,
1378 d,(void *)(u_long)chan_id);
1379 return(0);
1380 }
1381
1382 /* Unset NIO for a MPSC channel */
1383 int dev_gt96100_mpsc_unset_nio(struct gt_data *d,u_int chan_id)
1384 {
1385 struct mpsc_channel *channel;
1386
1387 if (chan_id >= GT_MPSC_CHANNELS)
1388 return(-1);
1389
1390 if (d == NULL)
1391 return(0);
1392
1393 channel = &d->mpsc[chan_id];
1394
1395 if (channel->nio != NULL) {
1396 netio_rxl_remove(channel->nio);
1397 channel->nio = NULL;
1398 }
1399
1400 return(0);
1401 }
1402
1403 /* Set a VTTY for a MPSC channel */
1404 int dev_gt96100_mpsc_set_vtty(struct gt_data *d,u_int chan_id,vtty_t *vtty)
1405 {
1406 struct mpsc_channel *channel;
1407
1408 if (chan_id >= GT_MPSC_CHANNELS)
1409 return(-1);
1410
1411 channel = &d->mpsc[chan_id];
1412
1413 if (channel->vtty != NULL)
1414 return(-1);
1415
1416 channel->vtty = vtty;
1417 return(0);
1418 }
1419
1420 /* Unset a VTTY for a MPSC channel */
1421 int dev_gt96100_mpsc_unset_vtty(struct gt_data *d,u_int chan_id)
1422 {
1423 struct mpsc_channel *channel;
1424
1425 if (chan_id >= GT_MPSC_CHANNELS)
1426 return(-1);
1427
1428 channel = &d->mpsc[chan_id];
1429
1430 if (channel->vtty != NULL) {
1431 channel->vtty = NULL;
1432 }
1433
1434 return(0);
1435 }
1436
1437 /* ======================================================================== */
1438 /* Ethernet */
1439 /* ======================================================================== */
1440
1441 /* Trigger/clear Ethernet interrupt if one or both port have pending events */
1442 static void gt_eth_set_int_status(struct gt_data *d)
1443 {
1444 /* Compute Ether0 summary */
1445 if (d->eth_ports[0].icr & GT_ICR_INT_SUM) {
1446 d->ser_cause_reg |= GT_SCR_ETH0_SUM;
1447 d->int_high_cause_reg |= GT_IHCR_ETH0_SUM;
1448 } else {
1449 d->ser_cause_reg &= ~GT_SCR_ETH0_SUM;
1450 d->int_high_cause_reg &= ~GT_IHCR_ETH0_SUM;
1451 }
1452
1453 /* Compute Ether1 summary */
1454 if (d->eth_ports[1].icr & GT_ICR_INT_SUM) {
1455 d->ser_cause_reg |= GT_SCR_ETH1_SUM;
1456 d->int_high_cause_reg |= GT_IHCR_ETH1_SUM;
1457 } else {
1458 d->ser_cause_reg &= ~GT_SCR_ETH1_SUM;
1459 d->int_high_cause_reg &= ~GT_IHCR_ETH1_SUM;
1460 }
1461
1462 gt96k_update_irq_status(d);
1463 }
1464
1465 /* Update the Ethernet port interrupt status */
1466 static void gt_eth_update_int_status(struct gt_data *d,struct eth_port *port)
1467 {
1468 if (port->icr & port->imr & GT_ICR_MASK) {
1469 port->icr |= GT_ICR_INT_SUM;
1470 } else {
1471 port->icr &= ~GT_ICR_INT_SUM;
1472 }
1473
1474 gt_eth_set_int_status(d);
1475 }
1476
1477 /* Read a MII register */
1478 static m_uint32_t gt_mii_read(struct gt_data *d)
1479 {
1480 m_uint8_t port,reg;
1481 m_uint32_t res = 0;
1482
1483 port = (d->smi_reg & GT_SMIR_PHYAD_MASK) >> GT_SMIR_PHYAD_SHIFT;
1484 reg = (d->smi_reg & GT_SMIR_REGAD_MASK) >> GT_SMIR_REGAD_SHIFT;
1485
1486 #if DEBUG_MII
1487 GT_LOG(d,"MII: port 0x%4.4x, reg 0x%2.2x: reading.\n",port,reg);
1488 #endif
1489
1490 if ((port < GT_ETH_PORTS) && (reg < 32)) {
1491 res = d->mii_regs[port][reg];
1492
1493 switch(reg) {
1494 case 0x00:
1495 res &= ~0x8200; /* clear reset bit and autoneg restart */
1496 break;
1497 case 0x01:
1498 #if 0
1499 if (d->ports[port].nio && bcm5600_mii_port_status(d,port))
1500 d->mii_output = 0x782C;
1501 else
1502 d->mii_output = 0;
1503 #endif
1504 res = 0x782c;
1505 break;
1506 case 0x02:
1507 res = 0x40;
1508 break;
1509 case 0x03:
1510 res = 0x61d4;
1511 break;
1512 case 0x04:
1513 res = 0x1E1;
1514 break;
1515 case 0x05:
1516 res = 0x41E1;
1517 break;
1518 default:
1519 res = 0;
1520 }
1521 }
1522
1523 /* Mark the data as ready */
1524 res |= GT_SMIR_RVALID_FLAG;
1525
1526 return(res);
1527 }
1528
1529 /* Write a MII register */
1530 static void gt_mii_write(struct gt_data *d)
1531 {
1532 m_uint8_t port,reg;
1533 m_uint16_t isolation;
1534
1535 port = (d->smi_reg & GT_SMIR_PHYAD_MASK) >> GT_SMIR_PHYAD_SHIFT;
1536 reg = (d->smi_reg & GT_SMIR_REGAD_MASK) >> GT_SMIR_REGAD_SHIFT;
1537
1538 if ((port < GT_ETH_PORTS) && (reg < 32))
1539 {
1540 #if DEBUG_MII
1541 GT_LOG(d,"MII: port 0x%4.4x, reg 0x%2.2x: writing 0x%4.4x\n",
1542 port,reg,d->smi_reg & GT_SMIR_DATA_MASK);
1543 #endif
1544
1545 /* Check if PHY isolation status is changing */
1546 if (reg == 0) {
1547 isolation = (d->smi_reg ^ d->mii_regs[port][reg]) & 0x400;
1548
1549 if (isolation) {
1550 #if DEBUG_MII
1551 GT_LOG(d,"MII: port 0x%4.4x: generating IRQ\n",port);
1552 #endif
1553 d->eth_ports[port].icr |= GT_ICR_MII_STC;
1554 gt_eth_update_int_status(d,&d->eth_ports[port]);
1555 }
1556 }
1557
1558 d->mii_regs[port][reg] = d->smi_reg & GT_SMIR_DATA_MASK;
1559 }
1560 }
1561
1562 /* Handle registers of Ethernet ports */
1563 static int gt_eth_access(cpu_gen_t *cpu,struct vdevice *dev,
1564 m_uint32_t offset,u_int op_size,u_int op_type,
1565 m_uint64_t *data)
1566 {
1567 struct gt_data *d = dev->priv_data;
1568 struct eth_port *port = NULL;
1569 u_int queue;
1570 #if DEBUG_ETH
1571 const char* const access = (op_type == MTS_READ)? "read": "write";
1572 #endif
1573
1574 if ((offset < 0x80000) || (offset >= 0x90000))
1575 return(FALSE);
1576
1577 /* Determine the Ethernet port */
1578 if ((offset >= 0x84800) && (offset < 0x88800))
1579 port = &d->eth_ports[0];
1580 else if ((offset >= 0x88800) && (offset < 0x8c800))
1581 port = &d->eth_ports[1];
1582
1583 switch(offset) {
1584 /* SMI register */
1585 case 0x80810:
1586 #if DEBUG_ETH
1587 cpu_log(cpu,"GT96100/ETH","SMI register\n");
1588 #endif
1589 if (op_type == MTS_WRITE) {
1590 d->smi_reg = *data;
1591
1592 if (!(d->smi_reg & GT_SMIR_OPCODE_READ))
1593 gt_mii_write(d);
1594 } else {
1595 *data = 0;
1596
1597 if (d->smi_reg & GT_SMIR_OPCODE_READ)
1598 *data = gt_mii_read(d);
1599 }
1600 break;
1601
1602 /* ICR: Interrupt Cause Register */
1603 case 0x84850:
1604 case 0x88850:
1605 #if DEBUG_ETH
1606 cpu_log(cpu,"GT96100/ETH","ICR: Interrupt Cause Register [%s]\n",access);
1607 #endif
1608 if (op_type == MTS_READ) {
1609 *data = port->icr;
1610 } else {
1611 port->icr &= *data;
1612 gt_eth_update_int_status(d,port);
1613 }
1614 break;
1615
1616 /* IMR: Interrupt Mask Register */
1617 case 0x84858:
1618 case 0x88858:
1619 #if DEBUG_ETH
1620 cpu_log(cpu,"GT96100/ETH","IMR: Interrupt Mask Register [%s]\n",access);
1621 #endif
1622 if (op_type == MTS_READ) {
1623 *data = port->imr;
1624 } else {
1625 port->imr = *data;
1626 gt_eth_update_int_status(d,port);
1627 }
1628 break;
1629
1630 /* PCR: Port Configuration Register */
1631 case 0x84800:
1632 case 0x88800:
1633 #if DEBUG_ETH
1634 cpu_log(cpu,"GT96100/ETH","PCR: Port Configuration Register [%s]\n",access);
1635 #endif
1636 if (op_type == MTS_READ)
1637 *data = port->pcr;
1638 else
1639 port->pcr = *data;
1640 break;
1641
1642 /* PCXR: Port Configuration Extend Register */
1643 case 0x84808:
1644 case 0x88808:
1645 #if DEBUG_ETH
1646 cpu_log(cpu,"GT96100/ETH","PCXR: Port Configuration Extend Register [%s]\n",access);
1647 #endif
1648 if (op_type == MTS_READ) {
1649 *data = port->pcxr;
1650 *data |= GT_PCXR_SPEED;
1651 } else
1652 port->pcxr = *data;
1653 break;
1654
1655 /* PCMR: Port Command Register */
1656 case 0x84810:
1657 case 0x88810:
1658 #if DEBUG_ETH
1659 cpu_log(cpu,"GT96100/ETH","PCMR: Port Command Register [%s]\n",access);
1660 #endif
1661 if (op_type == MTS_READ)
1662 *data = port->pcmr;
1663 else
1664 port->pcmr = *data;
1665 break;
1666
1667 /* Port Status Register */
1668 case 0x84818:
1669 case 0x88818:
1670 #if DEBUG_ETH
1671 cpu_log(cpu,"GT96100/ETH","Port Status Register [%s]\n",access);
1672 #endif
1673 if (op_type == MTS_READ)
1674 *data = 0x0F;
1675 break;
1676
1677 /* First RX descriptor */
1678 case 0x84880:
1679 case 0x88880:
1680 case 0x84884:
1681 case 0x88884:
1682 case 0x84888:
1683 case 0x88888:
1684 case 0x8488C:
1685 case 0x8888C:
1686 #if DEBUG_ETH
1687 cpu_log(cpu,"GT96100/ETH","First RX descriptor [%s]\n",access);
1688 #endif
1689 queue = (offset >> 2) & 0x03;
1690 if (op_type == MTS_READ)
1691 *data = port->rx_start[queue];
1692 else
1693 port->rx_start[queue] = *data;
1694 break;
1695
1696 /* Current RX descriptor */
1697 case 0x848A0:
1698 case 0x888A0:
1699 case 0x848A4:
1700 case 0x888A4:
1701 case 0x848A8:
1702 case 0x888A8:
1703 case 0x848AC:
1704 case 0x888AC:
1705 #if DEBUG_ETH
1706 cpu_log(cpu,"GT96100/ETH","Current RX descriptor [%s]\n",access);
1707 #endif
1708 queue = (offset >> 2) & 0x03;
1709 if (op_type == MTS_READ)
1710 *data = port->rx_current[queue];
1711 else
1712 port->rx_current[queue] = *data;
1713 break;
1714
1715 /* Current TX descriptor */
1716 case 0x848E0:
1717 case 0x888E0:
1718 case 0x848E4:
1719 case 0x888E4:
1720 #if DEBUG_ETH
1721 cpu_log(cpu,"GT96100/ETH","Current RX descriptor [%s]\n",access);
1722 #endif
1723 queue = (offset >> 2) & 0x01;
1724 if (op_type == MTS_READ)
1725 *data = port->tx_current[queue];
1726 else
1727 port->tx_current[queue] = *data;
1728 break;
1729
1730 /* Hash Table Pointer */
1731 case 0x84828:
1732 case 0x88828:
1733 #if DEBUG_ETH
1734 cpu_log(cpu,"GT96100/ETH","Hash Table Pointer [%s]\n",access);
1735 #endif
1736 if (op_type == MTS_READ)
1737 *data = port->ht_addr;
1738 else
1739 port->ht_addr = *data;
1740 break;
1741
1742 /* SDCR: SDMA Configuration Register */
1743 case 0x84840:
1744 case 0x88840:
1745 #if DEBUG_ETH
1746 cpu_log(cpu,"GT96100/ETH","SDCR: SDMA Configuration Register [%s]\n",access);
1747 #endif
1748 if (op_type == MTS_READ)
1749 *data = port->sdcr;
1750 else
1751 port->sdcr = *data;
1752 break;
1753
1754 /* SDCMR: SDMA Command Register */
1755 case 0x84848:
1756 case 0x88848:
1757 #if DEBUG_ETH
1758 cpu_log(cpu,"GT96100/ETH","SDCMR: SDMA Command Register [%s]\n",access);
1759 #endif
1760 if (op_type == MTS_WRITE) {
1761 /* Start RX DMA */
1762 if (*data & GT_SDCMR_ERD) {
1763 port->sdcmr |= GT_SDCMR_ERD;
1764 port->sdcmr &= ~GT_SDCMR_AR;
1765 }
1766
1767 /* Abort RX DMA */
1768 if (*data & GT_SDCMR_AR)
1769 port->sdcmr &= ~GT_SDCMR_ERD;
1770
1771 /* Start TX High */
1772 if (*data & GT_SDCMR_TXDH) {
1773 port->sdcmr |= GT_SDCMR_TXDH;
1774 port->sdcmr &= ~GT_SDCMR_STDH;
1775 }
1776
1777 /* Start TX Low */
1778 if (*data & GT_SDCMR_TXDL) {
1779 port->sdcmr |= GT_SDCMR_TXDL;
1780 port->sdcmr &= ~GT_SDCMR_STDL;
1781 }
1782
1783 /* Stop TX High */
1784 if (*data & GT_SDCMR_STDH) {
1785 port->sdcmr &= ~GT_SDCMR_TXDH;
1786 port->sdcmr |= GT_SDCMR_STDH;
1787 }
1788
1789 /* Stop TX Low */
1790 if (*data & GT_SDCMR_STDL) {
1791 port->sdcmr &= ~GT_SDCMR_TXDL;
1792 port->sdcmr |= GT_SDCMR_STDL;
1793 }
1794 } else {
1795 *data = port->sdcmr;
1796 }
1797 break;
1798
1799 /* Ethernet MIB Counters */
1800 case 0x85800:
1801 case 0x89800:
1802 #if DEBUG_ETH
1803 cpu_log(cpu,"GT96100/ETH","Ethernet MIB Counters - Bytes Received [%s]\n",access);
1804 #endif
1805 if (op_type == MTS_READ) {
1806 *data = port->rx_bytes;
1807 port->rx_bytes = 0;
1808 }
1809 break;
1810
1811 case 0x85804:
1812 case 0x89804:
1813 #if DEBUG_ETH
1814 cpu_log(cpu,"GT96100/ETH","Ethernet MIB Counters - Bytes Sent [%s]\n",access);
1815 #endif
1816 if (op_type == MTS_READ) {
1817 *data = port->tx_bytes;
1818 port->tx_bytes = 0;
1819 }
1820 break;
1821
1822 case 0x85808:
1823 case 0x89808:
1824 #if DEBUG_ETH
1825 cpu_log(cpu,"GT96100/ETH","Ethernet MIB Counters - Frames Received [%s]\n",access);
1826 #endif
1827 if (op_type == MTS_READ) {
1828 *data = port->rx_frames;
1829 port->rx_frames = 0;
1830 }
1831 break;
1832
1833 case 0x8580C:
1834 case 0x8980C:
1835 #if DEBUG_ETH
1836 cpu_log(cpu,"GT96100/ETH","Ethernet MIB Counters - Frames Sent [%s]\n",access);
1837 #endif
1838 if (op_type == MTS_READ) {
1839 *data = port->tx_frames;
1840 port->tx_frames = 0;
1841 }
1842 break;
1843
1844 #if DEBUG_UNKNOWN
1845 default:
1846 if (op_type == MTS_READ) {
1847 cpu_log(cpu,"GT96100/ETH",
1848 "read access to unknown register 0x%x, pc=0x%llx\n",
1849 offset,cpu_get_pc(cpu));
1850 } else {
1851 cpu_log(cpu,"GT96100/ETH",
1852 "write access to unknown register 0x%x, value=0x%llx, "
1853 "pc=0x%llx\n",offset,*data,cpu_get_pc(cpu));
1854 }
1855 #endif
1856 }
1857
1858 #if DEBUG_ETH
1859 cpu_log(cpu,"GT96100/ETH",
1860 "DONE register 0x%x, value=0x%llx, pc=0x%llx [%s]\n",
1861 offset,*data,cpu_get_pc(cpu),access);
1862 #endif
1863
1864 return(TRUE);
1865 }
1866
1867 /*
1868 * dev_gt96100_access()
1869 */
1870 void *dev_gt96100_access(cpu_gen_t *cpu,struct vdevice *dev,m_uint32_t offset,
1871 u_int op_size,u_int op_type,m_uint64_t *data)
1872 {
1873 struct gt_data *gt_data = dev->priv_data;
1874
1875 GT_LOCK(gt_data);
1876
1877 if (op_type == MTS_READ) {
1878 *data = 0;
1879 } else {
1880 if (op_size == 4)
1881 *data = swap32(*data);
1882 }
1883
1884 #if 0 /* DEBUG */
1885 if (offset != 0x101a80) {
1886 if (op_type == MTS_READ) {
1887 cpu_log(cpu,"GT96100","READ OFFSET 0x%6.6x\n",offset);
1888 } else {
1889 cpu_log(cpu,"GT96100","WRITE OFFSET 0x%6.6x, DATA=0x%8.8llx\n",
1890 offset,*data);
1891 }
1892 }
1893 #endif
1894
1895 /* DMA registers */
1896 if (gt_dma_access(cpu,dev,offset,op_size,op_type,data) != 0)
1897 goto done;
1898
1899 /* Serial DMA channel registers */
1900 if (gt_sdma_access(cpu,dev,offset,op_size,op_type,data) != 0)
1901 goto done;
1902
1903 /* MPSC registers */
1904 if (gt_mpsc_access(cpu,dev,offset,op_size,op_type,data) != 0)
1905 goto done;
1906
1907 /* Ethernet registers */
1908 if (gt_eth_access(cpu,dev,offset,op_size,op_type,data) != 0)
1909 goto done;
1910
1911 switch(offset) {
1912 /* Watchdog configuration register */
1913 case 0x101a80:
1914 break;
1915
1916 /* Watchdog value register */
1917 case 0x101a84:
1918 break;
1919
1920 case 0x008: /* ras10_low */
1921 if (op_type == MTS_READ)
1922 *data = 0x000;
1923 break;
1924 case 0x010: /* ras10_high */
1925 if (op_type == MTS_READ)
1926 *data = 0x7F;
1927 break;
1928 case 0x018: /* ras32_low */
1929 if (op_type == MTS_READ)
1930 *data = 0x100;
1931 break;
1932 case 0x020: /* ras32_high */
1933 if (op_type == MTS_READ)
1934 *data = 0x7F;
1935 break;
1936 case 0x400: /* ras0_low */
1937 if (op_type == MTS_READ)
1938 *data = 0x00;
1939 break;
1940 case 0x404: /* ras0_high */
1941 if (op_type == MTS_READ)
1942 *data = 0xFF;
1943 break;
1944 case 0x408: /* ras1_low */
1945 if (op_type == MTS_READ)
1946 *data = 0x7F;
1947 break;
1948 case 0x40c: /* ras1_high */
1949 if (op_type == MTS_READ)
1950 *data = 0x00;
1951 break;
1952 case 0x410: /* ras2_low */
1953 if (op_type == MTS_READ)
1954 *data = 0x00;
1955 break;
1956 case 0x414: /* ras2_high */
1957 if (op_type == MTS_READ)
1958 *data = 0xFF;
1959 break;
1960 case 0x418: /* ras3_low */
1961 if (op_type == MTS_READ)
1962 *data = 0x7F;
1963 break;
1964 case 0x41c: /* ras3_high */
1965 if (op_type == MTS_READ)
1966 *data = 0x00;
1967 break;
1968 case 0xc08: /* pci0_cs10 */
1969 if (op_type == MTS_READ)
1970 *data = 0xFFF;
1971 break;
1972 case 0xc0c: /* pci0_cs32 */
1973 if (op_type == MTS_READ)
1974 *data = 0xFFF;
1975 break;
1976
1977 case 0xc00: /* pci_cmd */
1978 if (op_type == MTS_READ)
1979 *data = 0x00008001;
1980 break;
1981
1982 /* ===== Interrupt Main Cause Register ===== */
1983 case 0xc18:
1984 if (op_type == MTS_READ) {
1985 *data = gt_data->int_cause_reg;
1986 } else {
1987 /* Don't touch bit 0, 30 and 31 which are read-only */
1988 gt_data->int_cause_reg &= (*data | 0xC0000001);
1989 gt96k_update_irq_status(gt_data);
1990 }
1991 break;
1992
1993 /* ===== Interrupt High Cause Register ===== */
1994 case 0xc98:
1995 if (op_type == MTS_READ)
1996 *data = gt_data->int_high_cause_reg;
1997 break;
1998
1999 /* ===== Interrupt0 Main Mask Register ===== */
2000 case 0xc1c:
2001 if (op_type == MTS_READ) {
2002 *data = gt_data->int0_main_mask_reg;
2003 } else {
2004 gt_data->int0_main_mask_reg = *data;
2005 gt96k_update_irq_status(gt_data);
2006 }
2007 break;
2008
2009 /* ===== Interrupt0 High Mask Register ===== */
2010 case 0xc9c:
2011 if (op_type == MTS_READ) {
2012 *data = gt_data->int0_high_mask_reg;
2013 } else {
2014 gt_data->int0_high_mask_reg = *data;
2015 gt96k_update_irq_status(gt_data);
2016 }
2017 break;
2018
2019 /* ===== Interrupt1 Main Mask Register ===== */
2020 case 0xc24:
2021 if (op_type == MTS_READ) {
2022 *data = gt_data->int1_main_mask_reg;
2023 } else {
2024 gt_data->int1_main_mask_reg = *data;
2025 gt96k_update_irq_status(gt_data);
2026 }
2027 break;
2028
2029 /* ===== Interrupt1 High Mask Register ===== */
2030 case 0xca4:
2031 if (op_type == MTS_READ) {
2032 *data = gt_data->int1_high_mask_reg;
2033 } else {
2034 gt_data->int1_high_mask_reg = *data;
2035 gt96k_update_irq_status(gt_data);
2036 }
2037 break;
2038
2039 /* ===== Serial Cause Register (read-only) ===== */
2040 case 0x103a00:
2041 if (op_type == MTS_READ)
2042 *data = gt_data->ser_cause_reg;
2043 break;
2044
2045 /* ===== SerInt0 Mask Register ===== */
2046 case 0x103a80:
2047 if (op_type == MTS_READ) {
2048 *data = gt_data->serint0_mask_reg;
2049 } else {
2050 gt_data->serint0_mask_reg = *data;
2051 gt96k_update_irq_status(gt_data);
2052 }
2053 break;
2054
2055 /* ===== SerInt1 Mask Register ===== */
2056 case 0x103a88:
2057 if (op_type == MTS_READ) {
2058 *data = gt_data->serint1_mask_reg;
2059 } else {
2060 gt_data->serint1_mask_reg = *data;
2061 gt96k_update_irq_status(gt_data);
2062 }
2063 break;
2064
2065 /* ===== SDMA cause register ===== */
2066 case 0x103a10:
2067 if (op_type == MTS_READ) {
2068 *data = gt_data->sdma_cause_reg;
2069 } else {
2070 gt_data->sdma_cause_reg &= *data;
2071 gt_sdma_update_int_status(gt_data);
2072 }
2073 break;
2074
2075 case 0x103a13:
2076 if (op_type == MTS_WRITE) {
2077 //printf("Writing 0x103a13, *data = 0x%8.8llx, "
2078 // "sdma_cause_reg=0x%8.8x\n",
2079 // *data, gt_data->sdma_cause_reg);
2080
2081 gt_data->sdma_cause_reg = 0;
2082 gt_sdma_update_channel_int_status(gt_data,6);
2083 gt_sdma_update_channel_int_status(gt_data,7);
2084 }
2085 break;
2086
2087 /* ==== SDMA mask register */
2088 case 0x103a90:
2089 if (op_type == MTS_READ) {
2090 *data = gt_data->sdma_mask_reg;
2091 } else {
2092 gt_data->sdma_mask_reg = *data;
2093 gt_sdma_update_int_status(gt_data);
2094 }
2095 break;
2096
2097 case 0x103a38:
2098 case 0x103a3c:
2099 case 0x100A48:
2100 if (op_type == MTS_READ) {
2101 //*data = 0xFFFFFFFF;
2102 }
2103 break;
2104
2105 /* CIU Arbiter Configuration Register */
2106 case 0x101ac0:
2107 if (op_type == MTS_READ)
2108 *data = 0x80000000;
2109 break;
2110
2111 /* SGCR - SDMA Global Configuration Register */
2112 case GT_REG_SGC:
2113 if (op_type == MTS_READ)
2114 *data = gt_data->sgcr;
2115 else
2116 gt_data->sgcr = *data;
2117 break;
2118
2119 /* ===== PCI Bus 1 ===== */
2120 case 0xcf0:
2121 pci_dev_addr_handler(cpu,gt_data->bus[1],op_type,FALSE,data);
2122 break;
2123
2124 case 0xcf4:
2125 pci_dev_data_handler(cpu,gt_data->bus[1],op_type,FALSE,data);
2126 break;
2127
2128 /* ===== PCI Bus 0 ===== */
2129 case PCI_BUS_ADDR: /* pci configuration address (0xcf8) */
2130 pci_dev_addr_handler(cpu,gt_data->bus[0],op_type,FALSE,data);
2131 break;
2132
2133 case PCI_BUS_DATA: /* pci data address (0xcfc) */
2134 pci_dev_data_handler(cpu,gt_data->bus[0],op_type,FALSE,data);
2135 break;
2136
2137 #if DEBUG_UNKNOWN
2138 default:
2139 if (op_type == MTS_READ) {
2140 cpu_log(cpu,"GT96100","read from unknown addr 0x%x, pc=0x%llx\n",
2141 offset,cpu_get_pc(cpu));
2142 } else {
2143 cpu_log(cpu,"GT96100","write to unknown addr 0x%x, value=0x%llx, "
2144 "pc=0x%llx\n",offset,*data,cpu_get_pc(cpu));
2145 }
2146 #endif
2147 }
2148
2149 done:
2150 GT_UNLOCK(gt_data);
2151 if ((op_type == MTS_READ) && (op_size == 4))
2152 *data = swap32(*data);
2153 return NULL;
2154 }
2155
2156 /* Handle a TX queue (single packet) */
2157 static int gt_eth_handle_port_txqueue(struct gt_data *d,struct eth_port *port,
2158 int queue)
2159 {
2160 u_char pkt[GT_MAX_PKT_SIZE],*pkt_ptr;
2161 struct sdma_desc ctxd;
2162 m_uint32_t tx_current;
2163 m_uint32_t len,tot_len;
2164 int abort = FALSE;
2165
2166 /* Check if this TX queue is active */
2167 if ((queue == 0) && (port->sdcmr & GT_SDCMR_STDL))
2168 return(FALSE);
2169
2170 if ((queue == 1) && (port->sdcmr & GT_SDCMR_STDH))
2171 return(FALSE);
2172
2173 /* Copy the current txring descriptor */
2174 tx_current = port->tx_current[queue];
2175
2176 if (!tx_current)
2177 return(FALSE);
2178
2179 gt_sdma_desc_read(d,tx_current,&ctxd);
2180
2181 /* If we don't own the first descriptor, we cannot transmit */
2182 if (!(ctxd.cmd_stat & GT_TXDESC_OWN)) {
2183 if (queue == 0) {
2184 port->icr |= GT_ICR_TXENDL;
2185 port->sdcmr |= GT_SDCMR_STDL;
2186 port->sdcmr &= ~GT_SDCMR_TXDL;
2187
2188 } else {
2189 port->icr |= GT_ICR_TXENDH;
2190 port->sdcmr |= GT_SDCMR_STDH;
2191 port->sdcmr &= ~GT_SDCMR_TXDH;
2192 }
2193
2194 gt_eth_update_int_status(d,port);
2195 return(FALSE);
2196 }
2197
2198 /* Empty packet for now */
2199 pkt_ptr = pkt;
2200 tot_len = 0;
2201
2202 for(;;) {
2203 #if DEBUG_ETH_TX
2204 GT_LOG(d,"gt_eth_handle_txqueue: loop: "
2205 "tx_current=0x%08x, cmd_stat=0x%08x, buf_size=0x%08x, next_ptr=0x%08x, buf_ptr=0x%08x\n",
2206 tx_current, ctxd.cmd_stat, ctxd.buf_size, ctxd.next_ptr, ctxd.buf_ptr);
2207 #endif
2208
2209 if (!(ctxd.cmd_stat & GT_TXDESC_OWN)) {
2210 #if DEBUG_ETH_TX
2211 GT_LOG(d,"gt_eth_handle_txqueue: descriptor not owned!\n");
2212 #endif
2213 abort = TRUE;
2214 break;
2215 }
2216
2217 /* Copy packet data to the buffer */
2218 len = (ctxd.buf_size & GT_TXDESC_BC_MASK) >> GT_TXDESC_BC_SHIFT;
2219
2220 physmem_copy_from_vm(d->vm,pkt_ptr,ctxd.buf_ptr,len);
2221 pkt_ptr += len;
2222 tot_len += len;
2223
2224 /* Clear the OWN bit if this is not the last descriptor */
2225 if (!(ctxd.cmd_stat & GT_TXDESC_L)) {
2226 ctxd.cmd_stat &= ~GT_TXDESC_OWN;
2227 physmem_copy_u32_to_vm(d->vm,tx_current+GT_SDMA_CMD_OFFSET,ctxd.cmd_stat);
2228 }
2229
2230 /* Last descriptor or no more desc available ? */
2231 if (ctxd.cmd_stat & GT_TXDESC_L)
2232 break;
2233
2234 if (!(ctxd.next_ptr)) {
2235 abort = TRUE;
2236 break;
2237 }
2238
2239 /* Fetch the next descriptor */
2240 tx_current = ctxd.next_ptr;
2241 gt_sdma_desc_read(d,tx_current,&ctxd);
2242 }
2243
2244 if ((tot_len != 0) && !abort) {
2245 #if DEBUG_ETH_TX
2246 GT_LOG(d,"Ethernet: sending packet of %u bytes\n",tot_len);
2247 mem_dump(d->vm->log_fd,pkt,tot_len);
2248 #endif
2249 /* rewrite ISL header if required */
2250 cisco_isl_rewrite(pkt,tot_len);
2251
2252 /* send it on wire */
2253 netio_send(port->nio,pkt,tot_len);
2254
2255 /* Update MIB counters */
2256 port->tx_bytes += tot_len;
2257 port->tx_frames++;
2258 }
2259
2260 /* Clear the OWN flag of the last descriptor */
2261 ctxd.cmd_stat &= ~GT_TXDESC_OWN;
2262 physmem_copy_u32_to_vm(d->vm,tx_current+GT_SDMA_CMD_OFFSET,ctxd.cmd_stat);
2263
2264 port->tx_current[queue] = tx_current = ctxd.next_ptr;
2265
2266 /* Notify host about transmitted packet */
2267 if (queue == 0)
2268 port->icr |= GT_ICR_TXBUFL;
2269 else
2270 port->icr |= GT_ICR_TXBUFH;
2271
2272 if (abort) {
2273 /* TX underrun */
2274 port->icr |= GT_ICR_TXUDR;
2275
2276 if (queue == 0)
2277 port->icr |= GT_ICR_TXERRL;
2278 else
2279 port->icr |= GT_ICR_TXERRH;
2280 } else {
2281 /* End of queue has been reached */
2282 if (!tx_current) {
2283 if (queue == 0)
2284 port->icr |= GT_ICR_TXENDL;
2285 else
2286 port->icr |= GT_ICR_TXENDH;
2287 }
2288 }
2289
2290 /* Update the interrupt status */
2291 gt_eth_update_int_status(d,port);
2292 return(TRUE);
2293 }
2294
2295 /* Handle TX ring of the specified port */
2296 static void gt_eth_handle_port_txqueues(struct gt_data *d,u_int port)
2297 {
2298 gt_eth_handle_port_txqueue(d,&d->eth_ports[port],0); /* TX Low */
2299 gt_eth_handle_port_txqueue(d,&d->eth_ports[port],1); /* TX High */
2300 }
2301
2302 /* Handle all TX rings of all Ethernet ports */
2303 static int gt_eth_handle_txqueues(struct gt_data *d)
2304 {
2305 int i;
2306
2307 GT_LOCK(d);
2308
2309 for(i=0;i<GT_ETH_PORTS;i++)
2310 gt_eth_handle_port_txqueues(d,i);
2311
2312 GT_UNLOCK(d);
2313 return(TRUE);
2314 }
2315
2316 /* Inverse a nibble */
2317 static const int inv_nibble[16] = {
2318 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
2319 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF
2320 };
2321
2322 /* Inverse a 9-bit value */
2323 static inline u_int gt_hash_inv_9bit(u_int val)
2324 {
2325 u_int res;
2326
2327 res = inv_nibble[val & 0x0F] << 5;
2328 res |= inv_nibble[(val & 0xF0) >> 4] << 1;
2329 res |= (val & 0x100) >> 8;
2330 return(res);
2331 }
2332
2333 /*
2334 * Compute hash value for Ethernet address filtering.
2335 * Two modes are available (p.271 of the GT96100 doc).
2336 */
2337 static u_int gt_eth_hash_value(n_eth_addr_t *addr,int mode)
2338 {
2339 m_uint64_t tmp;
2340 u_int res;
2341 int i;
2342
2343 /* Swap the nibbles */
2344 for(i=0,tmp=0;i<N_ETH_ALEN;i++) {
2345 tmp <<= 8;
2346 tmp |= (inv_nibble[addr->eth_addr_byte[i] & 0x0F]) << 4;
2347 tmp |= inv_nibble[(addr->eth_addr_byte[i] & 0xF0) >> 4];
2348 }
2349
2350 if (mode == 0) {
2351 /* Fill bits 0:8 */
2352 res = (tmp & 0x00000003) | ((tmp & 0x00007f00) >> 6);
2353 res ^= (tmp & 0x00ff8000) >> 15;
2354 res ^= (tmp & 0x1ff000000ULL) >> 24;
2355
2356 /* Fill bits 9:14 */
2357 res |= (tmp & 0xfc) << 7;
2358 } else {
2359 /* Fill bits 0:8 */
2360 res = gt_hash_inv_9bit((tmp & 0x00007fc0) >> 6);
2361 res ^= gt_hash_inv_9bit((tmp & 0x00ff8000) >> 15);
2362 res ^= gt_hash_inv_9bit((tmp & 0x1ff000000ULL) >> 24);
2363
2364 /* Fill bits 9:14 */
2365 res |= (tmp & 0x3f) << 9;
2366 }
2367
2368 return(res);
2369 }
2370
2371 /*
2372 * Walk through the Ethernet hash table.
2373 */
2374 static int gt_eth_hash_lookup(struct gt_data *d,struct eth_port *port,
2375 n_eth_addr_t *addr,m_uint64_t *entry)
2376 {
2377 m_uint64_t eth_val;
2378 m_uint32_t hte_addr;
2379 u_int hash_val;
2380 int i;
2381
2382 eth_val = (m_uint64_t)addr->eth_addr_byte[0] << 3;
2383 eth_val |= (m_uint64_t)addr->eth_addr_byte[1] << 11;
2384 eth_val |= (m_uint64_t)addr->eth_addr_byte[2] << 19;
2385 eth_val |= (m_uint64_t)addr->eth_addr_byte[3] << 27;
2386 eth_val |= (m_uint64_t)addr->eth_addr_byte[4] << 35;
2387 eth_val |= (m_uint64_t)addr->eth_addr_byte[5] << 43;
2388
2389 /* Compute hash value for Ethernet address filtering */
2390 hash_val = gt_eth_hash_value(addr,port->pcr & GT_PCR_HM);
2391
2392 if (port->pcr & GT_PCR_HS) {
2393 /* 1/2K address filtering */
2394 hte_addr = port->ht_addr + ((hash_val & 0x7ff) << 3);
2395 } else {
2396 /* 8K address filtering */
2397 hte_addr = port->ht_addr + (hash_val << 3);
2398 }
2399
2400 #if DEBUG_ETH_HASH
2401 GT_LOG(d,"Hash Lookup for Ethernet address "
2402 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x: addr=0x%x\n",
2403 addr->eth_addr_byte[0], addr->eth_addr_byte[1],
2404 addr->eth_addr_byte[2], addr->eth_addr_byte[3],
2405 addr->eth_addr_byte[4], addr->eth_addr_byte[5],
2406 hte_addr);
2407 #endif
2408
2409 for(i=0;i<GT_HTE_HOPNUM;i++,hte_addr+=8) {
2410 *entry = ((m_uint64_t)physmem_copy_u32_from_vm(d->vm,hte_addr)) << 32;
2411 *entry |= physmem_copy_u32_from_vm(d->vm,hte_addr+4);
2412
2413 /* Empty entry ? */
2414 if (!(*entry & GT_HTE_VALID))
2415 return(GT_HTLOOKUP_MISS);
2416
2417 /* Skip flag or different Ethernet address: jump to next entry */
2418 if ((*entry & GT_HTE_SKIP) || ((*entry & GT_HTE_ADDR_MASK) != eth_val))
2419 continue;
2420
2421 /* We have the good MAC address in this entry */
2422 return(GT_HTLOOKUP_MATCH);
2423 }
2424
2425 return(GT_HTLOOKUP_HOP_EXCEEDED);
2426 }
2427
2428 /*
2429 * Check if a packet (given its destination address) must be handled
2430 * at RX path.
2431 *
2432 * Return values:
2433 * - 0: Discard packet ;
2434 * - 1: Receive packet but set "M" bit in RX descriptor;
2435 * - 2: Receive packet.
2436 *
2437 * The documentation is not clear about the M bit in RX descriptor.
2438 * It is described as "Miss" or "Match" depending on the section.
2439 * However, it turns out that IOS treats the bit as "Miss" bit.
2440 * If the bit is set, the destination MAC address has not been found
2441 * in the hash table, and the frame may be subject to software MAC
2442 * address filter associated by IOS with the interface. If the bit
2443 * is clear, the destination MAC address has been found in the hash
2444 * table and the frame will be accepted by IOS unconditionally.
2445 * The M bit is required to correctly handle unicast frames destined
2446 * to other MAC addresses when the interface works in promiscuous mode.
2447 * IOS puts an interface into promiscuous mode when multicast routing
2448 * or bridging has been configured on it.
2449 */
2450 static inline int gt_eth_handle_rx_daddr(struct gt_data *d,
2451 struct eth_port *port,
2452 u_int hash_res,
2453 m_uint64_t hash_entry)
2454 {
2455 /* Hop Number exceeded */
2456 if (hash_res == GT_HTLOOKUP_HOP_EXCEEDED)
2457 return(1);
2458
2459 /* Match and hash entry marked as "Receive" */
2460 if ((hash_res == GT_HTLOOKUP_MATCH) && (hash_entry & GT_HTE_RD))
2461 return(2);
2462
2463 /* Miss but hash table default mode to forward ? */
2464 if ((hash_res == GT_HTLOOKUP_MISS) && (port->pcr & GT_PCR_HDM))
2465 return(2);
2466
2467 /* Promiscous Mode */
2468 if (port->pcr & GT_PCR_PM)
2469 return(1);
2470
2471 /* Drop packet for other cases */
2472 return(0);
2473 }
2474
2475 /* Put a packet in the specified RX queue */
2476 static int gt_eth_handle_rxqueue(struct gt_data *d,u_int port_id,u_int queue,
2477 u_char *pkt,ssize_t pkt_len)
2478 {
2479 struct eth_port *port = &d->eth_ports[port_id];
2480 m_uint32_t rx_start,rx_current;
2481 struct sdma_desc rxd0,rxdn,*rxdc;
2482 ssize_t tot_len = pkt_len;
2483 u_char *pkt_ptr = pkt;
2484 n_eth_dot1q_hdr_t *hdr;
2485 m_uint64_t hash_entry;
2486 int i,hash_res,addr_action;
2487
2488 /* Truncate the packet if it is too big */
2489 pkt_len = m_min(pkt_len,GT_MAX_PKT_SIZE);
2490
2491 /* Copy the first RX descriptor */
2492 if (!(rx_start = rx_current = port->rx_start[queue]))
2493 goto dma_error;
2494
2495 /* Analyze the Ethernet header */
2496 hdr = (n_eth_dot1q_hdr_t *)pkt;
2497
2498 /* Hash table lookup for address filtering */
2499 hash_res = gt_eth_hash_lookup(d,port,&hdr->daddr,&hash_entry);
2500
2501 #if DEBUG_ETH_HASH
2502 GT_LOG(d,"Hash result: %d, hash_entry=0x%llx\n",hash_res,hash_entry);
2503 #endif
2504
2505 if (!(addr_action = gt_eth_handle_rx_daddr(d,port,hash_res,hash_entry)))
2506 return(FALSE);
2507
2508 /* Load the first RX descriptor */
2509 gt_sdma_desc_read(d,rx_start,&rxd0);
2510
2511 #if DEBUG_ETH_RX
2512 GT_LOG(d,"port %u/queue %u: reading desc at 0x%8.8x "
2513 "[buf_size=0x%8.8x,cmd_stat=0x%8.8x,"
2514 "next_ptr=0x%8.8x,buf_ptr=0x%8.8x]\n",
2515 port_id,queue,rx_start,
2516 rxd0.buf_size,rxd0.cmd_stat,rxd0.next_ptr,rxd0.buf_ptr);
2517 #endif
2518
2519 for(i=0,rxdc=&rxd0;tot_len>0;i++)
2520 {
2521 /* We must own the descriptor */
2522 if (!(rxdc->cmd_stat & GT_RXDESC_OWN))
2523 goto dma_error;
2524
2525 /* Put data into the descriptor buffer */
2526 gt_sdma_rxdesc_put_pkt(d,rxdc,&pkt_ptr,&tot_len);
2527
2528 /* Clear the OWN bit */
2529 rxdc->cmd_stat &= ~GT_RXDESC_OWN;
2530
2531 /* We have finished if the complete packet has been stored */
2532 if (tot_len == 0) {
2533 rxdc->cmd_stat |= GT_RXDESC_L;
2534 rxdc->buf_size += 4; /* Add 4 bytes for CRC */
2535 }
2536
2537 /* Update the descriptor in host memory (but not the 1st) */
2538 if (i != 0)
2539 gt_sdma_desc_write(d,rx_current,rxdc);
2540
2541 /* Get address of the next descriptor */
2542 rx_current = rxdc->next_ptr;
2543
2544 if (tot_len == 0)
2545 break;
2546
2547 if (!rx_current)
2548 goto dma_error;
2549
2550 /* Read the next descriptor from VM physical RAM */
2551 gt_sdma_desc_read(d,rx_current,&rxdn);
2552 rxdc = &rxdn;
2553 }
2554
2555 /* Update the RX pointers */
2556 port->rx_start[queue] = port->rx_current[queue] = rx_current;
2557
2558 /* Update the first RX descriptor */
2559 rxd0.cmd_stat |= GT_RXDESC_F;
2560
2561 if (hash_res == GT_HTLOOKUP_HOP_EXCEEDED)
2562 rxd0.cmd_stat |= GT_RXDESC_HE;
2563
2564 if (addr_action == 1)
2565 rxd0.cmd_stat |= GT_RXDESC_M;
2566
2567 if (ntohs(hdr->type) <= N_ETH_MTU) /* 802.3 frame */
2568 rxd0.cmd_stat |= GT_RXDESC_FT;
2569
2570 gt_sdma_desc_write(d,rx_start,&rxd0);
2571
2572 /* Update MIB counters */
2573 port->rx_bytes += pkt_len;
2574 port->rx_frames++;
2575
2576 /* Indicate that we have a frame ready */
2577 port->icr |= (GT_ICR_RXBUFQ0 << queue) | GT_ICR_RXBUF;
2578 gt_eth_update_int_status(d,port);
2579 return(TRUE);
2580
2581 dma_error:
2582 port->icr |= (GT_ICR_RXERRQ0 << queue) | GT_ICR_RXERR;
2583 gt_eth_update_int_status(d,port);
2584 return(FALSE);
2585 }
2586
2587 /* Handle RX packet for an Ethernet port */
2588 static int gt_eth_handle_rx_pkt(netio_desc_t *nio,
2589 u_char *pkt,ssize_t pkt_len,
2590 struct gt_data *d,void *arg)
2591 {
2592 u_int queue,port_id = (u_int)(u_long)arg;
2593 struct eth_port *port;
2594
2595 port = &d->eth_ports[port_id];
2596
2597 GT_LOCK(d);
2598
2599 /* Check if RX DMA is active */
2600 if (!(port->sdcmr & GT_SDCMR_ERD)) {
2601 GT_UNLOCK(d);
2602 return(FALSE);
2603 }
2604
2605 queue = 0; /* At this time, only put packet in queue 0 */
2606 gt_eth_handle_rxqueue(d,port_id,queue,pkt,pkt_len);
2607 GT_UNLOCK(d);
2608 return(TRUE);
2609 }
2610
2611 /* Shutdown a GT system controller */
2612 void dev_gt_shutdown(vm_instance_t *vm,struct gt_data *d)
2613 {
2614 if (d != NULL) {
2615 /* Stop the Ethernet TX ring scanner */
2616 ptask_remove(d->eth_tx_tid);
2617
2618 /* Remove the device */
2619 dev_remove(vm,&d->dev);
2620
2621 /* Remove the PCI device */
2622 pci_dev_remove(d->pci_dev);
2623
2624 /* Free the structure itself */
2625 free(d);
2626 }
2627 }
2628
2629 /* Create a new GT64010 controller */
2630 int dev_gt64010_init(vm_instance_t *vm,char *name,
2631 m_uint64_t paddr,m_uint32_t len,u_int irq)
2632 {
2633 struct gt_data *d;
2634
2635 if (!(d = malloc(sizeof(*d)))) {
2636 fprintf(stderr,"gt64010: unable to create device data.\n");
2637 return(-1);
2638 }
2639
2640 memset(d,0,sizeof(*d));
2641 pthread_mutex_init(&d->lock,NULL);
2642 d->vm = vm;
2643 d->bus[0] = vm->pci_bus[0];
2644 d->gt_update_irq_status = gt64k_update_irq_status;
2645
2646 vm_object_init(&d->vm_obj);
2647 d->vm_obj.name = name;
2648 d->vm_obj.data = d;
2649 d->vm_obj.shutdown = (vm_shutdown_t)dev_gt_shutdown;
2650
2651 dev_init(&d->dev);
2652 d->dev.name = name;
2653 d->dev.priv_data = d;
2654 d->dev.phys_addr = paddr;
2655 d->dev.phys_len = len;
2656 d->dev.handler = dev_gt64010_access;
2657
2658 /* Add the controller as a PCI device */
2659 if (!pci_dev_lookup(d->bus[0],0,0,0)) {
2660 d->pci_dev = pci_dev_add(d->bus[0],name,
2661 PCI_VENDOR_GALILEO,PCI_PRODUCT_GALILEO_GT64010,
2662 0,0,irq,d,NULL,NULL,NULL);
2663
2664 if (!d->pci_dev) {
2665 fprintf(stderr,"gt64010: unable to create PCI device.\n");
2666 return(-1);
2667 }
2668 }
2669
2670 /* Map this device to the VM */
2671 vm_bind_device(vm,&d->dev);
2672 vm_object_add(vm,&d->vm_obj);
2673 return(0);
2674 }
2675
2676 /*
2677 * pci_gt64120_read()
2678 *
2679 * Read a PCI register.
2680 */
2681 static m_uint32_t pci_gt64120_read(cpu_gen_t *cpu,struct pci_device *dev,
2682 int reg)
2683 {
2684 switch (reg) {
2685 case 0x08:
2686 return(0x03008005);
2687 default:
2688 return(0);
2689 }
2690 }
2691
2692 /* Create a new GT64120 controller */
2693 int dev_gt64120_init(vm_instance_t *vm,char *name,
2694 m_uint64_t paddr,m_uint32_t len,u_int irq)
2695 {
2696 struct gt_data *d;
2697
2698 if (!(d = malloc(sizeof(*d)))) {
2699 fprintf(stderr,"gt64120: unable to create device data.\n");
2700 return(-1);
2701 }
2702
2703 memset(d,0,sizeof(*d));
2704 pthread_mutex_init(&d->lock,NULL);
2705 d->vm = vm;
2706 d->bus[0] = vm->pci_bus[0];
2707 d->bus[1] = vm->pci_bus[1];
2708 d->gt_update_irq_status = gt64k_update_irq_status;
2709
2710 vm_object_init(&d->vm_obj);
2711 d->vm_obj.name = name;
2712 d->vm_obj.data = d;
2713 d->vm_obj.shutdown = (vm_shutdown_t)dev_gt_shutdown;
2714
2715 dev_init(&d->dev);
2716 d->dev.name = name;
2717 d->dev.priv_data = d;
2718 d->dev.phys_addr = paddr;
2719 d->dev.phys_len = len;
2720 d->dev.handler = dev_gt64120_access;
2721
2722 /* Add the controller as a PCI device */
2723 if (!pci_dev_lookup(d->bus[0],0,0,0)) {
2724 d->pci_dev = pci_dev_add(d->bus[0],name,
2725 PCI_VENDOR_GALILEO,PCI_PRODUCT_GALILEO_GT64120,
2726 0,0,irq,d,NULL,pci_gt64120_read,NULL);
2727 if (!d->pci_dev) {
2728 fprintf(stderr,"gt64120: unable to create PCI device.\n");
2729 return(-1);
2730 }
2731 }
2732
2733 /* Map this device to the VM */
2734 vm_bind_device(vm,&d->dev);
2735 vm_object_add(vm,&d->vm_obj);
2736 return(0);
2737 }
2738
2739 /*
2740 * pci_gt96100_read()
2741 *
2742 * Read a PCI register.
2743 */
2744 static m_uint32_t pci_gt96100_read(cpu_gen_t *cpu,struct pci_device *dev,
2745 int reg)
2746 {
2747 switch (reg) {
2748 case 0x08:
2749 return(0x03008005);
2750 default:
2751 return(0);
2752 }
2753 }
2754
2755 /* Create a new GT96100 controller */
2756 int dev_gt96100_init(vm_instance_t *vm,char *name,
2757 m_uint64_t paddr,m_uint32_t len,
2758 u_int int0_irq,u_int int1_irq,
2759 u_int serint0_irq,u_int serint1_irq)
2760 {
2761 struct gt_data *d;
2762 u_int i;
2763
2764 if (!(d = malloc(sizeof(*d)))) {
2765 fprintf(stderr,"gt96100: unable to create device data.\n");
2766 return(-1);
2767 }
2768
2769 memset(d,0,sizeof(*d));
2770 pthread_mutex_init(&d->lock,NULL);
2771 d->name = name;
2772 d->vm = vm;
2773 d->gt_update_irq_status = gt96k_update_irq_status;
2774
2775 for(i=0;i<GT_SDMA_CHANNELS;i++) {
2776 d->sdma[0][i].id = i;
2777 d->sdma[1][i].id = i;
2778 }
2779
2780 /* IRQ setup */
2781 d->int0_irq = int0_irq;
2782 d->int1_irq = int1_irq;
2783 d->serint0_irq = serint0_irq;
2784 d->serint1_irq = serint1_irq;
2785
2786 d->bus[0] = vm->pci_bus[0];
2787 d->bus[1] = vm->pci_bus[1];
2788
2789 vm_object_init(&d->vm_obj);
2790 d->vm_obj.name = name;
2791 d->vm_obj.data = d;
2792 d->vm_obj.shutdown = (vm_shutdown_t)dev_gt_shutdown;
2793
2794 dev_init(&d->dev);
2795 d->dev.name = name;
2796 d->dev.priv_data = d;
2797 d->dev.phys_addr = paddr;
2798 d->dev.phys_len = len;
2799 d->dev.handler = dev_gt96100_access;
2800
2801 /* Add the controller as a PCI device */
2802 if (!pci_dev_lookup(d->bus[0],0,0,0)) {
2803 d->pci_dev = pci_dev_add(d->bus[0],name,
2804 PCI_VENDOR_GALILEO,PCI_PRODUCT_GALILEO_GT96100,
2805 0,0,-1,d,NULL,pci_gt96100_read,NULL);
2806 if (!d->pci_dev) {
2807 fprintf(stderr,"gt96100: unable to create PCI device.\n");
2808 return(-1);
2809 }
2810 }
2811
2812 /* Start the Ethernet TX ring scanner */
2813 d->eth_tx_tid = ptask_add((ptask_callback)gt_eth_handle_txqueues,d,NULL);
2814
2815 /* Map this device to the VM */
2816 vm_bind_device(vm,&d->dev);
2817 vm_object_add(vm,&d->vm_obj);
2818 return(0);
2819 }
2820
2821 /* Bind a NIO to GT96100 Ethernet device */
2822 int dev_gt96100_eth_set_nio(struct gt_data *d,u_int port_id,netio_desc_t *nio)
2823 {
2824 struct eth_port *port;
2825
2826 if (!d || (port_id >= GT_ETH_PORTS))
2827 return(-1);
2828
2829 port = &d->eth_ports[port_id];
2830
2831 /* check that a NIO is not already bound */
2832 if (port->nio != NULL)
2833 return(-1);
2834
2835 port->nio = nio;
2836 netio_rxl_add(nio,(netio_rx_handler_t)gt_eth_handle_rx_pkt,
2837 d,(void *)(u_long)port_id);
2838 return(0);
2839 }
2840
2841 /* Unbind a NIO from a GT96100 device */
2842 int dev_gt96100_eth_unset_nio(struct gt_data *d,u_int port_id)
2843 {
2844 struct eth_port *port;
2845
2846 if (!d || (port_id >= GT_ETH_PORTS))
2847 return(-1);
2848
2849 port = &d->eth_ports[port_id];
2850
2851 if (port->nio != NULL) {
2852 netio_rxl_remove(port->nio);
2853 port->nio = NULL;
2854 }
2855
2856 return(0);
2857 }
2858
2859 /* Show debugging information */
2860 static void dev_gt96100_show_eth_info(struct gt_data *d,u_int port_id)
2861 {
2862 struct eth_port *port;
2863
2864 port = &d->eth_ports[port_id];
2865
2866 printf("GT96100 Ethernet port %u:\n",port_id);
2867 printf(" PCR = 0x%8.8x\n",port->pcr);
2868 printf(" PCXR = 0x%8.8x\n",port->pcxr);
2869 printf(" PCMR = 0x%8.8x\n",port->pcmr);
2870 printf(" PSR = 0x%8.8x\n",port->psr);
2871 printf(" ICR = 0x%8.8x\n",port->icr);
2872 printf(" IMR = 0x%8.8x\n",port->imr);
2873
2874 printf("\n");
2875 }
2876
2877 /* Show debugging information */
2878 int dev_gt96100_show_info(struct gt_data *d)
2879 {
2880 GT_LOCK(d);
2881 dev_gt96100_show_eth_info(d,0);
2882 dev_gt96100_show_eth_info(d,1);
2883 GT_UNLOCK(d);
2884 return(0);
2885 }