xref: /illumos-gate/usr/src/uts/common/io/nge/nge_chip.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "nge.h"
28 static uint32_t	nge_watchdog_count	= 1 << 29;
29 extern boolean_t nge_enable_msi;
30 static void nge_sync_mac_modes(nge_t *);
31 
32 #undef NGE_DBG
33 #define	NGE_DBG		NGE_DBG_CHIP
34 
35 /*
36  * Operating register get/set access routines
37  */
38 uint8_t nge_reg_get8(nge_t *ngep, nge_regno_t regno);
39 #pragma	inline(nge_reg_get8)
40 
41 uint8_t
42 nge_reg_get8(nge_t *ngep, nge_regno_t regno)
43 {
44 	NGE_TRACE(("nge_reg_get8($%p, 0x%lx)", (void *)ngep, regno));
45 
46 	return (ddi_get8(ngep->io_handle, PIO_ADDR(ngep, regno)));
47 }
48 
49 void nge_reg_put8(nge_t *ngep, nge_regno_t regno, uint8_t data);
50 #pragma	inline(nge_reg_put8)
51 
52 void
53 nge_reg_put8(nge_t *ngep, nge_regno_t regno, uint8_t data)
54 {
55 	NGE_TRACE(("nge_reg_put8($%p, 0x%lx, 0x%x)",
56 	    (void *)ngep, regno, data));
57 	ddi_put8(ngep->io_handle, PIO_ADDR(ngep, regno), data);
58 
59 }
60 
61 uint16_t nge_reg_get16(nge_t *ngep, nge_regno_t regno);
62 #pragma	inline(nge_reg_get16)
63 
64 uint16_t
65 nge_reg_get16(nge_t *ngep, nge_regno_t regno)
66 {
67 	NGE_TRACE(("nge_reg_get16($%p, 0x%lx)", (void *)ngep, regno));
68 	return (ddi_get16(ngep->io_handle, PIO_ADDR(ngep, regno)));
69 }
70 
71 void nge_reg_put16(nge_t *ngep, nge_regno_t regno, uint16_t data);
72 #pragma	inline(nge_reg_put16)
73 
74 void
75 nge_reg_put16(nge_t *ngep, nge_regno_t regno, uint16_t data)
76 {
77 	NGE_TRACE(("nge_reg_put16($%p, 0x%lx, 0x%x)",
78 	    (void *)ngep, regno, data));
79 	ddi_put16(ngep->io_handle, PIO_ADDR(ngep, regno), data);
80 
81 }
82 
83 uint32_t nge_reg_get32(nge_t *ngep, nge_regno_t regno);
84 #pragma	inline(nge_reg_get32)
85 
86 uint32_t
87 nge_reg_get32(nge_t *ngep, nge_regno_t regno)
88 {
89 	NGE_TRACE(("nge_reg_get32($%p, 0x%lx)", (void *)ngep, regno));
90 	return (ddi_get32(ngep->io_handle, PIO_ADDR(ngep, regno)));
91 }
92 
93 void nge_reg_put32(nge_t *ngep, nge_regno_t regno, uint32_t data);
94 #pragma	inline(nge_reg_put32)
95 
96 void
97 nge_reg_put32(nge_t *ngep, nge_regno_t regno, uint32_t data)
98 {
99 	NGE_TRACE(("nge_reg_put32($%p, 0x%lx, 0x%x)",
100 	    (void *)ngep, regno, data));
101 	ddi_put32(ngep->io_handle, PIO_ADDR(ngep, regno), data);
102 
103 }
104 
105 
106 static int nge_chip_peek_cfg(nge_t *ngep, nge_peekpoke_t *ppd);
107 #pragma	no_inline(nge_chip_peek_cfg)
108 
109 static int
110 nge_chip_peek_cfg(nge_t *ngep, nge_peekpoke_t *ppd)
111 {
112 	int err;
113 	uint64_t regval;
114 	uint64_t regno;
115 
116 	NGE_TRACE(("nge_chip_peek_cfg($%p, $%p)",
117 	    (void *)ngep, (void *)ppd));
118 
119 	err = DDI_SUCCESS;
120 	regno = ppd->pp_acc_offset;
121 
122 	switch (ppd->pp_acc_size) {
123 	case 1:
124 		regval = pci_config_get8(ngep->cfg_handle, regno);
125 		break;
126 
127 	case 2:
128 		regval = pci_config_get16(ngep->cfg_handle, regno);
129 		break;
130 
131 	case 4:
132 		regval = pci_config_get32(ngep->cfg_handle, regno);
133 		break;
134 
135 	case 8:
136 		regval = pci_config_get64(ngep->cfg_handle, regno);
137 		break;
138 	}
139 	ppd->pp_acc_data = regval;
140 	return (err);
141 }
142 
143 static int nge_chip_poke_cfg(nge_t *ngep, nge_peekpoke_t *ppd);
144 
145 static int
146 nge_chip_poke_cfg(nge_t *ngep, nge_peekpoke_t *ppd)
147 {
148 	int err;
149 	uint64_t regval;
150 	uint64_t regno;
151 
152 	NGE_TRACE(("nge_chip_poke_cfg($%p, $%p)",
153 	    (void *)ngep, (void *)ppd));
154 
155 	err = DDI_SUCCESS;
156 	regno = ppd->pp_acc_offset;
157 	regval = ppd->pp_acc_data;
158 
159 	switch (ppd->pp_acc_size) {
160 	case 1:
161 		pci_config_put8(ngep->cfg_handle, regno, regval);
162 		break;
163 
164 	case 2:
165 		pci_config_put16(ngep->cfg_handle, regno, regval);
166 		break;
167 
168 	case 4:
169 		pci_config_put32(ngep->cfg_handle, regno, regval);
170 		break;
171 
172 	case 8:
173 		pci_config_put64(ngep->cfg_handle, regno, regval);
174 		break;
175 	}
176 
177 	return (err);
178 
179 }
180 
181 static int nge_chip_peek_reg(nge_t *ngep, nge_peekpoke_t *ppd);
182 
183 static int
184 nge_chip_peek_reg(nge_t *ngep, nge_peekpoke_t *ppd)
185 {
186 	int err;
187 	uint64_t regval;
188 	void *regaddr;
189 
190 	NGE_TRACE(("nge_chip_peek_reg($%p, $%p)",
191 	    (void *)ngep, (void *)ppd));
192 
193 	err = DDI_SUCCESS;
194 	regaddr = PIO_ADDR(ngep, ppd->pp_acc_offset);
195 
196 	switch (ppd->pp_acc_size) {
197 	case 1:
198 		regval = ddi_get8(ngep->io_handle, regaddr);
199 	break;
200 
201 	case 2:
202 		regval = ddi_get16(ngep->io_handle, regaddr);
203 	break;
204 
205 	case 4:
206 		regval = ddi_get32(ngep->io_handle, regaddr);
207 	break;
208 
209 	case 8:
210 		regval = ddi_get64(ngep->io_handle, regaddr);
211 	break;
212 
213 	default:
214 		regval = 0x0ull;
215 	break;
216 	}
217 	ppd->pp_acc_data = regval;
218 	return (err);
219 }
220 
221 static int nge_chip_poke_reg(nge_t *ngep, nge_peekpoke_t *ppd);
222 
223 static int
224 nge_chip_poke_reg(nge_t *ngep, nge_peekpoke_t *ppd)
225 {
226 	int err;
227 	uint64_t regval;
228 	void *regaddr;
229 
230 	NGE_TRACE(("nge_chip_poke_reg($%p, $%p)",
231 	    (void *)ngep, (void *)ppd));
232 
233 	err = DDI_SUCCESS;
234 	regaddr = PIO_ADDR(ngep, ppd->pp_acc_offset);
235 	regval = ppd->pp_acc_data;
236 
237 	switch (ppd->pp_acc_size) {
238 	case 1:
239 		ddi_put8(ngep->io_handle, regaddr, regval);
240 		break;
241 
242 	case 2:
243 		ddi_put16(ngep->io_handle, regaddr, regval);
244 		break;
245 
246 	case 4:
247 		ddi_put32(ngep->io_handle, regaddr, regval);
248 		break;
249 
250 	case 8:
251 		ddi_put64(ngep->io_handle, regaddr, regval);
252 		break;
253 	}
254 	return (err);
255 }
256 
257 static int nge_chip_peek_mii(nge_t *ngep, nge_peekpoke_t *ppd);
258 #pragma	no_inline(nge_chip_peek_mii)
259 
260 static int
261 nge_chip_peek_mii(nge_t *ngep, nge_peekpoke_t *ppd)
262 {
263 	int err;
264 
265 	err = DDI_SUCCESS;
266 	ppd->pp_acc_data = nge_mii_get16(ngep, ppd->pp_acc_offset/2);
267 	return (err);
268 }
269 
270 static int nge_chip_poke_mii(nge_t *ngep, nge_peekpoke_t *ppd);
271 #pragma	no_inline(nge_chip_poke_mii)
272 
273 static int
274 nge_chip_poke_mii(nge_t *ngep, nge_peekpoke_t *ppd)
275 {
276 	int err;
277 	err = DDI_SUCCESS;
278 	nge_mii_put16(ngep, ppd->pp_acc_offset/2, ppd->pp_acc_data);
279 	return (err);
280 }
281 
282 /*
283  * Basic SEEPROM get/set access routine
284  *
285  * This uses the chip's SEEPROM auto-access method, controlled by the
286  * Serial EEPROM Address/Data Registers at 0x504h, so the CPU
287  * doesn't have to fiddle with the individual bits.
288  *
289  * The caller should hold <genlock> and *also* have already acquired
290  * the right to access the SEEPROM.
291  *
292  * Return value:
293  *	0 on success,
294  *	ENODATA on access timeout (maybe retryable: device may just be busy)
295  *	EPROTO on other h/w or s/w errors.
296  *
297  * <*dp> is an input to a SEEPROM_ACCESS_WRITE operation, or an output
298  * from a (successful) SEEPROM_ACCESS_READ.
299  */
300 
301 static int
302 nge_seeprom_access(nge_t *ngep, uint32_t cmd, nge_regno_t addr, uint16_t *dp)
303 {
304 	uint32_t tries;
305 	nge_ep_cmd cmd_reg;
306 	nge_ep_data data_reg;
307 
308 	NGE_TRACE(("nge_seeprom_access($%p, %d, %x, $%p)",
309 	    (void *)ngep, cmd, addr, (void *)dp));
310 
311 	ASSERT(mutex_owned(ngep->genlock));
312 
313 	/*
314 	 * Check there's no command in progress.
315 	 *
316 	 * Note: this *shouldn't* ever find that there is a command
317 	 * in progress, because we already hold the <genlock> mutex.
318 	 * Also, to ensure we don't have a conflict with the chip's
319 	 * internal firmware or a process accessing the same (shared)
320 	 * So this is just a final consistency check: we shouldn't
321 	 * see EITHER the START bit (command started but not complete)
322 	 * OR the COMPLETE bit (command completed but not cleared).
323 	 */
324 	cmd_reg.cmd_val = nge_reg_get32(ngep, NGE_EP_CMD);
325 	for (tries = 0; tries < 30; tries++) {
326 		if (cmd_reg.cmd_bits.sts == SEEPROM_READY)
327 			break;
328 		drv_usecwait(10);
329 		cmd_reg.cmd_val = nge_reg_get32(ngep, NGE_EP_CMD);
330 	}
331 
332 	/*
333 	 * This should not happen. If so, we have to restart eeprom
334 	 *  state machine
335 	 */
336 	if (tries == 30) {
337 		cmd_reg.cmd_bits.sts = SEEPROM_READY;
338 		nge_reg_put32(ngep, NGE_EP_CMD, cmd_reg.cmd_val);
339 		drv_usecwait(10);
340 		/*
341 		 * Polling the status bit to make assure the eeprom is ready
342 		 */
343 		cmd_reg.cmd_val = nge_reg_get32(ngep, NGE_EP_CMD);
344 		for (tries = 0; tries < 30; tries++) {
345 			if (cmd_reg.cmd_bits.sts == SEEPROM_READY)
346 				break;
347 			drv_usecwait(10);
348 			cmd_reg.cmd_val = nge_reg_get32(ngep, NGE_EP_CMD);
349 		}
350 	}
351 
352 	/*
353 	 * Assemble the command ...
354 	 */
355 	cmd_reg.cmd_bits.addr = (uint32_t)addr;
356 	cmd_reg.cmd_bits.cmd = cmd;
357 	cmd_reg.cmd_bits.sts = 0;
358 
359 	nge_reg_put32(ngep, NGE_EP_CMD, cmd_reg.cmd_val);
360 
361 	/*
362 	 * Polling whether the access is successful.
363 	 *
364 	 */
365 	cmd_reg.cmd_val = nge_reg_get32(ngep, NGE_EP_CMD);
366 	for (tries = 0; tries < 30; tries++) {
367 		if (cmd_reg.cmd_bits.sts == SEEPROM_READY)
368 			break;
369 		drv_usecwait(10);
370 		cmd_reg.cmd_val = nge_reg_get32(ngep, NGE_EP_CMD);
371 	}
372 
373 	if (tries == 30) {
374 		nge_report(ngep, NGE_HW_ROM);
375 		return (DDI_FAILURE);
376 	}
377 	switch (cmd) {
378 	default:
379 	case SEEPROM_CMD_WRITE_ENABLE:
380 	case SEEPROM_CMD_ERASE:
381 	case SEEPROM_CMD_ERALSE_ALL:
382 	case SEEPROM_CMD_WRITE_DIS:
383 	break;
384 
385 	case SEEPROM_CMD_READ:
386 		data_reg.data_val = nge_reg_get32(ngep, NGE_EP_DATA);
387 		*dp = data_reg.data_bits.data;
388 	break;
389 
390 	case SEEPROM_CMD_WRITE:
391 		data_reg.data_val = nge_reg_get32(ngep, NGE_EP_DATA);
392 		data_reg.data_bits.data = *dp;
393 		nge_reg_put32(ngep, NGE_EP_DATA, data_reg.data_val);
394 	break;
395 	}
396 
397 	return (DDI_SUCCESS);
398 }
399 
400 
401 static int
402 nge_chip_peek_seeprom(nge_t *ngep, nge_peekpoke_t *ppd)
403 {
404 	uint16_t data;
405 	int err;
406 
407 	err = nge_seeprom_access(ngep, SEEPROM_CMD_READ,
408 	    ppd->pp_acc_offset, &data);
409 	ppd->pp_acc_data =  data;
410 	return (err);
411 }
412 
413 static int
414 nge_chip_poke_seeprom(nge_t *ngep, nge_peekpoke_t *ppd)
415 {
416 	uint16_t data;
417 	int err;
418 
419 	data = ppd->pp_acc_data;
420 	err = nge_seeprom_access(ngep, SEEPROM_CMD_WRITE,
421 	    ppd->pp_acc_offset, &data);
422 	return (err);
423 }
424 
425 void
426 nge_init_dev_spec_param(nge_t *ngep)
427 {
428 	nge_dev_spec_param_t	*dev_param_p;
429 	chip_info_t	*infop;
430 
431 	dev_param_p = &ngep->dev_spec_param;
432 	infop = (chip_info_t *)&ngep->chipinfo;
433 
434 	switch (infop->device) {
435 	case DEVICE_ID_NF3_E6:
436 	case DEVICE_ID_NF3_DF:
437 	case DEVICE_ID_MCP04_37:
438 	case DEVICE_ID_MCP04_38:
439 		dev_param_p->msi = B_FALSE;
440 		dev_param_p->msi_x = B_FALSE;
441 		dev_param_p->vlan = B_FALSE;
442 		dev_param_p->advanced_pm = B_FALSE;
443 		dev_param_p->mac_addr_order = B_FALSE;
444 		dev_param_p->tx_pause_frame = B_FALSE;
445 		dev_param_p->rx_pause_frame = B_FALSE;
446 		dev_param_p->jumbo = B_FALSE;
447 		dev_param_p->tx_rx_64byte = B_FALSE;
448 		dev_param_p->rx_hw_checksum = B_FALSE;
449 		dev_param_p->tx_hw_checksum = 0;
450 		dev_param_p->desc_type = DESC_OFFLOAD;
451 		dev_param_p->rx_desc_num = NGE_RECV_SLOTS_DESC_1024;
452 		dev_param_p->tx_desc_num = NGE_SEND_SLOTS_DESC_1024;
453 		dev_param_p->nge_split = NGE_SPLIT_32;
454 		break;
455 
456 	case DEVICE_ID_CK804_56:
457 	case DEVICE_ID_CK804_57:
458 		dev_param_p->msi = B_TRUE;
459 		dev_param_p->msi_x = B_TRUE;
460 		dev_param_p->vlan = B_FALSE;
461 		dev_param_p->advanced_pm = B_FALSE;
462 		dev_param_p->mac_addr_order = B_FALSE;
463 		dev_param_p->tx_pause_frame = B_FALSE;
464 		dev_param_p->rx_pause_frame = B_TRUE;
465 		dev_param_p->jumbo = B_TRUE;
466 		dev_param_p->tx_rx_64byte = B_FALSE;
467 		dev_param_p->rx_hw_checksum = B_TRUE;
468 		dev_param_p->tx_hw_checksum = HCKSUM_IPHDRCKSUM;
469 		dev_param_p->desc_type = DESC_HOT;
470 		dev_param_p->rx_desc_num = NGE_RECV_SLOTS_DESC_3072;
471 		dev_param_p->tx_desc_num = NGE_SEND_SLOTS_DESC_3072;
472 		dev_param_p->nge_split = NGE_SPLIT_96;
473 		break;
474 
475 	case DEVICE_ID_MCP51_268:
476 	case DEVICE_ID_MCP51_269:
477 		dev_param_p->msi = B_FALSE;
478 		dev_param_p->msi_x = B_FALSE;
479 		dev_param_p->vlan = B_FALSE;
480 		dev_param_p->advanced_pm = B_TRUE;
481 		dev_param_p->mac_addr_order = B_FALSE;
482 		dev_param_p->tx_pause_frame = B_FALSE;
483 		dev_param_p->rx_pause_frame = B_FALSE;
484 		dev_param_p->jumbo = B_FALSE;
485 		dev_param_p->tx_rx_64byte = B_TRUE;
486 		dev_param_p->rx_hw_checksum = B_FALSE;
487 		dev_param_p->tx_hw_checksum = 0;
488 		dev_param_p->desc_type = DESC_OFFLOAD;
489 		dev_param_p->rx_desc_num = NGE_RECV_SLOTS_DESC_1024;
490 		dev_param_p->tx_desc_num = NGE_SEND_SLOTS_DESC_1024;
491 		dev_param_p->nge_split = NGE_SPLIT_32;
492 		break;
493 
494 	case DEVICE_ID_MCP55_372:
495 	case DEVICE_ID_MCP55_373:
496 		dev_param_p->msi = B_TRUE;
497 		dev_param_p->msi_x = B_TRUE;
498 		dev_param_p->vlan = B_TRUE;
499 		dev_param_p->advanced_pm = B_TRUE;
500 		dev_param_p->mac_addr_order = B_FALSE;
501 		dev_param_p->tx_pause_frame = B_TRUE;
502 		dev_param_p->rx_pause_frame = B_TRUE;
503 		dev_param_p->jumbo = B_TRUE;
504 		dev_param_p->tx_rx_64byte = B_TRUE;
505 		dev_param_p->rx_hw_checksum = B_TRUE;
506 		dev_param_p->tx_hw_checksum = HCKSUM_IPHDRCKSUM;
507 		dev_param_p->desc_type = DESC_HOT;
508 		dev_param_p->rx_desc_num = NGE_RECV_SLOTS_DESC_3072;
509 		dev_param_p->tx_desc_num = NGE_SEND_SLOTS_DESC_3072;
510 		dev_param_p->nge_split = NGE_SPLIT_96;
511 		break;
512 
513 	case DEVICE_ID_MCP61_3EE:
514 	case DEVICE_ID_MCP61_3EF:
515 		dev_param_p->msi = B_FALSE;
516 		dev_param_p->msi_x = B_FALSE;
517 		dev_param_p->vlan = B_FALSE;
518 		dev_param_p->advanced_pm = B_TRUE;
519 		dev_param_p->mac_addr_order = B_TRUE;
520 		dev_param_p->tx_pause_frame = B_FALSE;
521 		dev_param_p->rx_pause_frame = B_FALSE;
522 		dev_param_p->jumbo = B_FALSE;
523 		dev_param_p->tx_rx_64byte = B_TRUE;
524 		dev_param_p->rx_hw_checksum = B_FALSE;
525 		dev_param_p->tx_hw_checksum = 0;
526 		dev_param_p->desc_type = DESC_OFFLOAD;
527 		dev_param_p->rx_desc_num = NGE_RECV_SLOTS_DESC_1024;
528 		dev_param_p->tx_desc_num = NGE_SEND_SLOTS_DESC_1024;
529 		dev_param_p->nge_split = NGE_SPLIT_32;
530 		break;
531 
532 	case DEVICE_ID_MCP77_760:
533 	case DEVICE_ID_MCP79_AB0:
534 		dev_param_p->msi = B_FALSE;
535 		dev_param_p->msi_x = B_FALSE;
536 		dev_param_p->vlan = B_FALSE;
537 		dev_param_p->advanced_pm = B_TRUE;
538 		dev_param_p->mac_addr_order = B_TRUE;
539 		dev_param_p->tx_pause_frame = B_FALSE;
540 		dev_param_p->rx_pause_frame = B_FALSE;
541 		dev_param_p->jumbo = B_FALSE;
542 		dev_param_p->tx_rx_64byte = B_TRUE;
543 		dev_param_p->rx_hw_checksum = B_FALSE;
544 		dev_param_p->tx_hw_checksum = 0;
545 		dev_param_p->desc_type = DESC_HOT;
546 		dev_param_p->rx_desc_num = NGE_RECV_SLOTS_DESC_1024;
547 		dev_param_p->tx_desc_num = NGE_SEND_SLOTS_DESC_1024;
548 		dev_param_p->nge_split = NGE_SPLIT_32;
549 		break;
550 
551 	default:
552 		dev_param_p->msi = B_FALSE;
553 		dev_param_p->msi_x = B_FALSE;
554 		dev_param_p->vlan = B_FALSE;
555 		dev_param_p->advanced_pm = B_FALSE;
556 		dev_param_p->mac_addr_order = B_FALSE;
557 		dev_param_p->tx_pause_frame = B_FALSE;
558 		dev_param_p->rx_pause_frame = B_FALSE;
559 		dev_param_p->jumbo = B_FALSE;
560 		dev_param_p->tx_rx_64byte = B_FALSE;
561 		dev_param_p->rx_hw_checksum = B_FALSE;
562 		dev_param_p->tx_hw_checksum = 0;
563 		dev_param_p->desc_type = DESC_OFFLOAD;
564 		dev_param_p->rx_desc_num = NGE_RECV_SLOTS_DESC_1024;
565 		dev_param_p->tx_desc_num = NGE_SEND_SLOTS_DESC_1024;
566 		dev_param_p->nge_split = NGE_SPLIT_32;
567 		return;
568 	}
569 }
570 /*
571  * Perform first-stage chip (re-)initialisation, using only config-space
572  * accesses:
573  *
574  * + Read the vendor/device/revision/subsystem/cache-line-size registers,
575  *   returning the data in the structure pointed to by <infop>.
576  */
577 void nge_chip_cfg_init(nge_t *ngep, chip_info_t *infop, boolean_t reset);
578 #pragma	no_inline(nge_chip_cfg_init)
579 
580 void
581 nge_chip_cfg_init(nge_t *ngep, chip_info_t *infop, boolean_t reset)
582 {
583 	uint16_t command;
584 	ddi_acc_handle_t handle;
585 	nge_interbus_conf interbus_conf;
586 	nge_msi_mask_conf msi_mask_conf;
587 	nge_msi_map_cap_conf cap_conf;
588 
589 	NGE_TRACE(("nge_chip_cfg_init($%p, $%p, %d)",
590 	    (void *)ngep, (void *)infop, reset));
591 
592 	/*
593 	 * save PCI cache line size and subsystem vendor ID
594 	 *
595 	 * Read all the config-space registers that characterise the
596 	 * chip, specifically vendor/device/revision/subsystem vendor
597 	 * and subsystem device id.  We expect (but don't check) that
598 	 */
599 	handle = ngep->cfg_handle;
600 	/* reading the vendor information once */
601 	if (reset == B_FALSE) {
602 		infop->command = pci_config_get16(handle,
603 		    PCI_CONF_COMM);
604 		infop->vendor = pci_config_get16(handle,
605 		    PCI_CONF_VENID);
606 		infop->device = pci_config_get16(handle,
607 		    PCI_CONF_DEVID);
608 		infop->subven = pci_config_get16(handle,
609 		    PCI_CONF_SUBVENID);
610 		infop->subdev = pci_config_get16(handle,
611 		    PCI_CONF_SUBSYSID);
612 		infop->class_code = pci_config_get8(handle,
613 		    PCI_CONF_BASCLASS);
614 		infop->revision = pci_config_get8(handle,
615 		    PCI_CONF_REVID);
616 		infop->clsize = pci_config_get8(handle,
617 		    PCI_CONF_CACHE_LINESZ);
618 		infop->latency = pci_config_get8(handle,
619 		    PCI_CONF_LATENCY_TIMER);
620 	}
621 	if (nge_enable_msi) {
622 		/* Disable the hidden for MSI support */
623 		interbus_conf.conf_val = pci_config_get32(handle,
624 		    PCI_CONF_HT_INTERNAL);
625 		if ((infop->device == DEVICE_ID_MCP55_373) ||
626 		    (infop->device == DEVICE_ID_MCP55_372))
627 			interbus_conf.conf_bits.msix_off = NGE_SET;
628 		interbus_conf.conf_bits.msi_off = NGE_CLEAR;
629 		pci_config_put32(handle, PCI_CONF_HT_INTERNAL,
630 		    interbus_conf.conf_val);
631 
632 		if ((infop->device == DEVICE_ID_MCP55_373) ||
633 		    (infop->device == DEVICE_ID_MCP55_372)) {
634 
635 			/* Disable the vector off for mcp55 */
636 			msi_mask_conf.msi_mask_conf_val =
637 			    pci_config_get32(handle, PCI_CONF_HT_MSI_MASK);
638 			msi_mask_conf.msi_mask_bits.vec0_off = NGE_CLEAR;
639 			msi_mask_conf.msi_mask_bits.vec1_off = NGE_CLEAR;
640 			msi_mask_conf.msi_mask_bits.vec2_off = NGE_CLEAR;
641 			msi_mask_conf.msi_mask_bits.vec3_off = NGE_CLEAR;
642 			msi_mask_conf.msi_mask_bits.vec4_off = NGE_CLEAR;
643 			msi_mask_conf.msi_mask_bits.vec5_off = NGE_CLEAR;
644 			msi_mask_conf.msi_mask_bits.vec6_off = NGE_CLEAR;
645 			msi_mask_conf.msi_mask_bits.vec7_off = NGE_CLEAR;
646 			pci_config_put32(handle, PCI_CONF_HT_MSI_MASK,
647 			    msi_mask_conf.msi_mask_conf_val);
648 
649 			/* Enable the MSI mapping */
650 			cap_conf.msi_map_cap_conf_val =
651 			    pci_config_get32(handle, PCI_CONF_HT_MSI_MAP_CAP);
652 			cap_conf.map_cap_conf_bits.map_en = NGE_SET;
653 			pci_config_put32(handle, PCI_CONF_HT_MSI_MAP_CAP,
654 			    cap_conf.msi_map_cap_conf_val);
655 		}
656 	} else {
657 		interbus_conf.conf_val = pci_config_get32(handle,
658 		    PCI_CONF_HT_INTERNAL);
659 		interbus_conf.conf_bits.msi_off = NGE_SET;
660 		pci_config_put32(handle, PCI_CONF_HT_INTERNAL,
661 		    interbus_conf.conf_val);
662 	}
663 	command = infop->command | PCI_COMM_MAE;
664 	command &= ~PCI_COMM_MEMWR_INVAL;
665 	command |= PCI_COMM_ME;
666 	pci_config_put16(handle, PCI_CONF_COMM, command);
667 	pci_config_put16(handle, PCI_CONF_STAT, ~0);
668 
669 }
670 
671 int
672 nge_chip_stop(nge_t *ngep, boolean_t fault)
673 {
674 	int err;
675 	uint32_t reg_val;
676 	uint32_t	tries;
677 	nge_mintr_src mintr_src;
678 	nge_mii_cs mii_cs;
679 	nge_rx_poll rx_poll;
680 	nge_tx_poll tx_poll;
681 	nge_rx_en rx_en;
682 	nge_tx_en tx_en;
683 	nge_tx_sta tx_sta;
684 	nge_rx_sta rx_sta;
685 	nge_mode_cntl mode;
686 	nge_pmu_cntl2 pmu_cntl2;
687 
688 	NGE_TRACE(("nge_chip_stop($%p, %d)", (void *)ngep, fault));
689 
690 	err = DDI_SUCCESS;
691 
692 	/* Clear any pending PHY interrupt */
693 	mintr_src.src_val = nge_reg_get8(ngep, NGE_MINTR_SRC);
694 	nge_reg_put8(ngep, NGE_MINTR_SRC, mintr_src.src_val);
695 
696 	/* Mask all interrupts */
697 	reg_val = nge_reg_get32(ngep, NGE_INTR_MASK);
698 	reg_val &= ~NGE_INTR_ALL_EN;
699 	nge_reg_put32(ngep, NGE_INTR_MASK, reg_val);
700 
701 	/* Disable auto-polling of phy */
702 	mii_cs.cs_val = nge_reg_get32(ngep, NGE_MII_CS);
703 	mii_cs.cs_bits.ap_en = NGE_CLEAR;
704 	nge_reg_put32(ngep, NGE_MII_CS, mii_cs.cs_val);
705 
706 	/* Reset buffer management & DMA */
707 	mode.mode_val = nge_reg_get32(ngep, NGE_MODE_CNTL);
708 	mode.mode_bits.dma_dis = NGE_SET;
709 	mode.mode_bits.desc_type = ngep->desc_mode;
710 	nge_reg_put32(ngep, NGE_MODE_CNTL, mode.mode_val);
711 
712 	for (tries = 0; tries < 10000; tries++) {
713 		drv_usecwait(10);
714 		mode.mode_val = nge_reg_get32(ngep, NGE_MODE_CNTL);
715 		if (mode.mode_bits.dma_status == NGE_SET)
716 			break;
717 	}
718 	if (tries == 10000) {
719 		ngep->nge_chip_state = NGE_CHIP_ERROR;
720 		return (DDI_FAILURE);
721 	}
722 
723 	/* Disable rx's machine */
724 	rx_en.val = nge_reg_get8(ngep, NGE_RX_EN);
725 	rx_en.bits.rx_en = NGE_CLEAR;
726 	nge_reg_put8(ngep, NGE_RX_EN, rx_en.val);
727 
728 	/* Disable tx's machine */
729 	tx_en.val = nge_reg_get8(ngep, NGE_TX_EN);
730 	tx_en.bits.tx_en = NGE_CLEAR;
731 	nge_reg_put8(ngep, NGE_TX_EN, tx_en.val);
732 
733 	/*
734 	 * Clean the status of tx's state machine
735 	 * and Make assure the tx's channel is idle
736 	 */
737 	tx_sta.sta_val = nge_reg_get32(ngep, NGE_TX_STA);
738 	for (tries = 0; tries < 1000; tries++) {
739 		if (tx_sta.sta_bits.tx_chan_sta == NGE_CLEAR)
740 			break;
741 		drv_usecwait(10);
742 		tx_sta.sta_val = nge_reg_get32(ngep, NGE_TX_STA);
743 	}
744 	if (tries == 1000) {
745 		ngep->nge_chip_state = NGE_CHIP_ERROR;
746 		return (DDI_FAILURE);
747 	}
748 	nge_reg_put32(ngep, NGE_TX_STA,  tx_sta.sta_val);
749 
750 	/*
751 	 * Clean the status of rx's state machine
752 	 * and Make assure the tx's channel is idle
753 	 */
754 	rx_sta.sta_val = nge_reg_get32(ngep, NGE_RX_STA);
755 	for (tries = 0; tries < 1000; tries++) {
756 		if (rx_sta.sta_bits.rx_chan_sta == NGE_CLEAR)
757 			break;
758 		drv_usecwait(10);
759 		rx_sta.sta_val = nge_reg_get32(ngep, NGE_RX_STA);
760 	}
761 	if (tries == 1000) {
762 		ngep->nge_chip_state = NGE_CHIP_ERROR;
763 		return (DDI_FAILURE);
764 	}
765 	nge_reg_put32(ngep, NGE_RX_STA, rx_sta.sta_val);
766 
767 	/* Disable auto-poll of rx's state machine */
768 	rx_poll.poll_val = nge_reg_get32(ngep, NGE_RX_POLL);
769 	rx_poll.poll_bits.rpen = NGE_CLEAR;
770 	rx_poll.poll_bits.rpi = NGE_CLEAR;
771 	nge_reg_put32(ngep, NGE_RX_POLL, rx_poll.poll_val);
772 
773 	/* Disable auto-polling of tx's  state machine */
774 	tx_poll.poll_val = nge_reg_get32(ngep, NGE_TX_POLL);
775 	tx_poll.poll_bits.tpen = NGE_CLEAR;
776 	tx_poll.poll_bits.tpi = NGE_CLEAR;
777 	nge_reg_put32(ngep, NGE_TX_POLL, tx_poll.poll_val);
778 
779 	/* Restore buffer management */
780 	mode.mode_val = nge_reg_get32(ngep, NGE_MODE_CNTL);
781 	mode.mode_bits.bm_reset = NGE_SET;
782 	mode.mode_bits.tx_rcom_en = NGE_SET;
783 	nge_reg_put32(ngep, NGE_MODE_CNTL, mode.mode_val);
784 
785 	if (ngep->dev_spec_param.advanced_pm) {
786 
787 		nge_reg_put32(ngep, NGE_PMU_CIDLE_LIMIT, 0);
788 		nge_reg_put32(ngep, NGE_PMU_DIDLE_LIMIT, 0);
789 
790 		pmu_cntl2.cntl2_val = nge_reg_get32(ngep, NGE_PMU_CNTL2);
791 		pmu_cntl2.cntl2_bits.cidle_timer = NGE_CLEAR;
792 		pmu_cntl2.cntl2_bits.didle_timer = NGE_CLEAR;
793 		nge_reg_put32(ngep, NGE_PMU_CNTL2, pmu_cntl2.cntl2_val);
794 	}
795 	if (fault)
796 		ngep->nge_chip_state = NGE_CHIP_FAULT;
797 	else
798 		ngep->nge_chip_state = NGE_CHIP_STOPPED;
799 
800 	return (err);
801 }
802 
803 static void
804 nge_rx_setup(nge_t *ngep)
805 {
806 	uint64_t desc_addr;
807 	nge_rxtx_dlen dlen;
808 	nge_rx_poll rx_poll;
809 
810 	/*
811 	 * Filling the address and length of rx's descriptors
812 	 */
813 	desc_addr = ngep->recv->desc.cookie.dmac_laddress;
814 	nge_reg_put32(ngep, NGE_RX_DADR, desc_addr);
815 	nge_reg_put32(ngep, NGE_RX_DADR_HI, desc_addr >> 32);
816 	dlen.dlen_val = nge_reg_get32(ngep, NGE_RXTX_DLEN);
817 	dlen.dlen_bits.rdlen = ngep->recv->desc.nslots - 1;
818 	nge_reg_put32(ngep, NGE_RXTX_DLEN, dlen.dlen_val);
819 
820 	rx_poll.poll_val = nge_reg_get32(ngep, NGE_RX_POLL);
821 	rx_poll.poll_bits.rpi = RX_POLL_INTV_1G;
822 	rx_poll.poll_bits.rpen = NGE_SET;
823 	nge_reg_put32(ngep, NGE_RX_POLL, rx_poll.poll_val);
824 }
825 
826 static void
827 nge_tx_setup(nge_t *ngep)
828 {
829 	uint64_t desc_addr;
830 	nge_rxtx_dlen dlen;
831 
832 	/*
833 	 * Filling the address and length of tx's descriptors
834 	 */
835 	desc_addr = ngep->send->desc.cookie.dmac_laddress;
836 	nge_reg_put32(ngep, NGE_TX_DADR, desc_addr);
837 	nge_reg_put32(ngep, NGE_TX_DADR_HI, desc_addr >> 32);
838 	dlen.dlen_val = nge_reg_get32(ngep, NGE_RXTX_DLEN);
839 	dlen.dlen_bits.tdlen = ngep->send->desc.nslots - 1;
840 	nge_reg_put32(ngep, NGE_RXTX_DLEN, dlen.dlen_val);
841 }
842 
843 static int
844 nge_buff_setup(nge_t *ngep)
845 {
846 	nge_mode_cntl mode_cntl;
847 	nge_dev_spec_param_t	*dev_param_p;
848 
849 	dev_param_p = &ngep->dev_spec_param;
850 
851 	/*
852 	 * Configure Rx&Tx's buffer
853 	 */
854 	nge_rx_setup(ngep);
855 	nge_tx_setup(ngep);
856 
857 	/*
858 	 * Configure buffer attribute
859 	 */
860 	mode_cntl.mode_val = nge_reg_get32(ngep, NGE_MODE_CNTL);
861 
862 	/*
863 	 * Enable Dma access request
864 	 */
865 	mode_cntl.mode_bits.dma_dis = NGE_CLEAR;
866 
867 	/*
868 	 * Enbale Buffer management
869 	 */
870 	mode_cntl.mode_bits.bm_reset = NGE_CLEAR;
871 
872 	/*
873 	 * Support Standoffload Descriptor
874 	 */
875 	mode_cntl.mode_bits.desc_type = ngep->desc_mode;
876 
877 	/*
878 	 * Support receive hardware checksum
879 	 */
880 	if (dev_param_p->rx_hw_checksum) {
881 		mode_cntl.mode_bits.rx_sum_en = NGE_SET;
882 	} else
883 		mode_cntl.mode_bits.rx_sum_en = NGE_CLEAR;
884 
885 	/*
886 	 * Disable Tx PRD coarse update
887 	 */
888 	mode_cntl.mode_bits.tx_prd_cu_en = NGE_CLEAR;
889 
890 	/*
891 	 * Disable 64-byte access
892 	 */
893 	mode_cntl.mode_bits.w64_dis = NGE_SET;
894 
895 	/*
896 	 * Skip Rx Error Frame is not supported and if
897 	 * enable it, jumbo frame does not work any more.
898 	 */
899 	mode_cntl.mode_bits.rx_filter_en = NGE_CLEAR;
900 
901 	/*
902 	 * Can not support hot mode now
903 	 */
904 	mode_cntl.mode_bits.resv15 = NGE_CLEAR;
905 
906 	if (dev_param_p->vlan) {
907 		/* Disable the vlan strip for devices which support vlan */
908 		mode_cntl.mode_bits.vlan_strip = NGE_CLEAR;
909 
910 		/* Disable the vlan insert for devices which supprot vlan */
911 		mode_cntl.mode_bits.vlan_ins = NGE_CLEAR;
912 	}
913 
914 	if (dev_param_p->tx_rx_64byte) {
915 
916 		/* Set the maximum TX PRD fetch size to 64 bytes */
917 		mode_cntl.mode_bits.tx_fetch_prd = NGE_SET;
918 
919 		/* Set the maximum RX PRD fetch size to 64 bytes */
920 		mode_cntl.mode_bits.rx_fetch_prd = NGE_SET;
921 	}
922 	/*
923 	 * Upload Rx data as it arrives, rather than waiting for full frame
924 	 */
925 	mode_cntl.mode_bits.resv16 = NGE_CLEAR;
926 
927 	/*
928 	 * Normal HOT table accesses
929 	 */
930 	mode_cntl.mode_bits.resv17 = NGE_CLEAR;
931 
932 	/*
933 	 * Normal HOT buffer requesting
934 	 */
935 	mode_cntl.mode_bits.resv18 = NGE_CLEAR;
936 	nge_reg_put32(ngep, NGE_MODE_CNTL, mode_cntl.mode_val);
937 
938 	/*
939 	 * Signal controller to check for new Rx descriptors
940 	 */
941 	mode_cntl.mode_val = nge_reg_get32(ngep, NGE_MODE_CNTL);
942 	mode_cntl.mode_bits.rxdm = NGE_SET;
943 	mode_cntl.mode_bits.tx_rcom_en = NGE_SET;
944 	nge_reg_put32(ngep, NGE_MODE_CNTL, mode_cntl.mode_val);
945 
946 
947 	return (DDI_SUCCESS);
948 }
949 
950 /*
951  * When chipset resets, the chipset can not restore  the orignial
952  * mac address to the mac address registers.
953  *
954  * When the driver is dettached, the function will write the orignial
955  * mac address to the mac address registers.
956  */
957 
958 void
959 nge_restore_mac_addr(nge_t *ngep)
960 {
961 	uint32_t mac_addr;
962 
963 	mac_addr = (uint32_t)ngep->chipinfo.hw_mac_addr;
964 	nge_reg_put32(ngep, NGE_UNI_ADDR0, mac_addr);
965 	mac_addr = (uint32_t)(ngep->chipinfo.hw_mac_addr >> 32);
966 	nge_reg_put32(ngep, NGE_UNI_ADDR1, mac_addr);
967 }
968 
969 int
970 nge_chip_reset(nge_t *ngep)
971 {
972 	int err;
973 	uint8_t i;
974 	uint32_t regno;
975 	uint64_t mac = 0;
976 	nge_uni_addr1 uaddr1;
977 	nge_cp_cntl ee_cntl;
978 	nge_soft_misc soft_misc;
979 	nge_pmu_cntl0 pmu_cntl0;
980 	nge_pmu_cntl2 pmu_cntl2;
981 	nge_pm_cntl2 pm_cntl2;
982 	const nge_ksindex_t *ksip;
983 
984 	NGE_TRACE(("nge_chip_reset($%p)", (void *)ngep));
985 
986 	/*
987 	 * Clear the statistics by reading the statistics register
988 	 */
989 	for (ksip = nge_statistics; ksip->name != NULL; ++ksip) {
990 		regno = KS_BASE + ksip->index * sizeof (uint32_t);
991 		(void) nge_reg_get32(ngep, regno);
992 	}
993 
994 	/*
995 	 * Setup seeprom control
996 	 */
997 	ee_cntl.cntl_val = nge_reg_get32(ngep, NGE_EP_CNTL);
998 	ee_cntl.cntl_bits.clkdiv = EEPROM_CLKDIV;
999 	ee_cntl.cntl_bits.rom_size = EEPROM_32K;
1000 	ee_cntl.cntl_bits.word_wid = ACCESS_16BIT;
1001 	ee_cntl.cntl_bits.wait_slots = EEPROM_WAITCLK;
1002 	nge_reg_put32(ngep, NGE_EP_CNTL, ee_cntl.cntl_val);
1003 
1004 	/*
1005 	 * Reading the unicast mac address table
1006 	 */
1007 	if (ngep->nge_chip_state == NGE_CHIP_INITIAL) {
1008 		uaddr1.addr_val = nge_reg_get32(ngep, NGE_UNI_ADDR1);
1009 		mac = uaddr1.addr_bits.addr;
1010 		mac <<= 32;
1011 		mac |= nge_reg_get32(ngep, NGE_UNI_ADDR0);
1012 			ngep->chipinfo.hw_mac_addr = mac;
1013 			if (ngep->dev_spec_param.mac_addr_order) {
1014 				for (i = 0; i < ETHERADDRL; i++) {
1015 					ngep->chipinfo.vendor_addr.addr[i] =
1016 					    (uchar_t)mac;
1017 					ngep->cur_uni_addr.addr[i] =
1018 					    (uchar_t)mac;
1019 					mac >>= 8;
1020 				}
1021 			} else {
1022 				for (i = ETHERADDRL; i-- != 0; ) {
1023 					ngep->chipinfo.vendor_addr.addr[i] =
1024 					    (uchar_t)mac;
1025 					ngep->cur_uni_addr.addr[i] =
1026 					    (uchar_t)mac;
1027 					mac >>= 8;
1028 				}
1029 			}
1030 			ngep->chipinfo.vendor_addr.set = 1;
1031 	}
1032 	pci_config_put8(ngep->cfg_handle, PCI_CONF_CACHE_LINESZ,
1033 	    ngep->chipinfo.clsize);
1034 	pci_config_put8(ngep->cfg_handle, PCI_CONF_LATENCY_TIMER,
1035 	    ngep->chipinfo.latency);
1036 
1037 
1038 	if (ngep->dev_spec_param.advanced_pm) {
1039 
1040 		/* Program software misc register */
1041 		soft_misc.misc_val = nge_reg_get32(ngep, NGE_SOFT_MISC);
1042 		soft_misc.misc_bits.rx_clk_vx_rst = NGE_SET;
1043 		soft_misc.misc_bits.tx_clk_vx_rst = NGE_SET;
1044 		soft_misc.misc_bits.clk12m_vx_rst = NGE_SET;
1045 		soft_misc.misc_bits.fpci_clk_vx_rst = NGE_SET;
1046 		soft_misc.misc_bits.rx_clk_vc_rst = NGE_SET;
1047 		soft_misc.misc_bits.tx_clk_vc_rst = NGE_SET;
1048 		soft_misc.misc_bits.fs_clk_vc_rst = NGE_SET;
1049 		soft_misc.misc_bits.rst_ex_m2pintf = NGE_SET;
1050 		nge_reg_put32(ngep, NGE_SOFT_MISC, soft_misc.misc_val);
1051 
1052 		/* wait for 32 us */
1053 		drv_usecwait(32);
1054 
1055 		soft_misc.misc_val = nge_reg_get32(ngep, NGE_SOFT_MISC);
1056 		soft_misc.misc_bits.rx_clk_vx_rst = NGE_CLEAR;
1057 		soft_misc.misc_bits.tx_clk_vx_rst = NGE_CLEAR;
1058 		soft_misc.misc_bits.clk12m_vx_rst = NGE_CLEAR;
1059 		soft_misc.misc_bits.fpci_clk_vx_rst = NGE_CLEAR;
1060 		soft_misc.misc_bits.rx_clk_vc_rst = NGE_CLEAR;
1061 		soft_misc.misc_bits.tx_clk_vc_rst = NGE_CLEAR;
1062 		soft_misc.misc_bits.fs_clk_vc_rst = NGE_CLEAR;
1063 		soft_misc.misc_bits.rst_ex_m2pintf = NGE_CLEAR;
1064 		nge_reg_put32(ngep, NGE_SOFT_MISC, soft_misc.misc_val);
1065 
1066 		/* Program PMU registers */
1067 		pmu_cntl0.cntl0_val = nge_reg_get32(ngep, NGE_PMU_CNTL0);
1068 		pmu_cntl0.cntl0_bits.core_spd10_fp =
1069 		    NGE_PMU_CORE_SPD10_BUSY;
1070 		pmu_cntl0.cntl0_bits.core_spd10_idle =
1071 		    NGE_PMU_CORE_SPD10_IDLE;
1072 		pmu_cntl0.cntl0_bits.core_spd100_fp =
1073 		    NGE_PMU_CORE_SPD100_BUSY;
1074 		pmu_cntl0.cntl0_bits.core_spd100_idle =
1075 		    NGE_PMU_CORE_SPD100_IDLE;
1076 		pmu_cntl0.cntl0_bits.core_spd1000_fp =
1077 		    NGE_PMU_CORE_SPD1000_BUSY;
1078 		pmu_cntl0.cntl0_bits.core_spd1000_idle =
1079 		    NGE_PMU_CORE_SPD100_IDLE;
1080 		pmu_cntl0.cntl0_bits.core_spd10_idle =
1081 		    NGE_PMU_CORE_SPD10_IDLE;
1082 		nge_reg_put32(ngep, NGE_PMU_CNTL0, pmu_cntl0.cntl0_val);
1083 
1084 		/* Set the core idle limit value */
1085 		nge_reg_put32(ngep, NGE_PMU_CIDLE_LIMIT,
1086 		    NGE_PMU_CIDLE_LIMIT_DEF);
1087 
1088 		/* Set the device idle limit value */
1089 		nge_reg_put32(ngep, NGE_PMU_DIDLE_LIMIT,
1090 		    NGE_PMU_DIDLE_LIMIT_DEF);
1091 
1092 		/* Enable the core/device idle timer in PMU control 2 */
1093 		pmu_cntl2.cntl2_val = nge_reg_get32(ngep, NGE_PMU_CNTL2);
1094 		pmu_cntl2.cntl2_bits.cidle_timer = NGE_SET;
1095 		pmu_cntl2.cntl2_bits.didle_timer = NGE_SET;
1096 		pmu_cntl2.cntl2_bits.core_enable = NGE_SET;
1097 		pmu_cntl2.cntl2_bits.dev_enable = NGE_SET;
1098 		nge_reg_put32(ngep, NGE_PMU_CNTL2, pmu_cntl2.cntl2_val);
1099 	}
1100 	/*
1101 	 * Stop the chipset and clear buffer management
1102 	 */
1103 	err = nge_chip_stop(ngep, B_FALSE);
1104 	if (err == DDI_FAILURE)
1105 		return (err);
1106 	/*
1107 	 * Clear the power state bits for phy since interface no longer
1108 	 * works after rebooting from Windows on a multi-boot machine
1109 	 */
1110 	if (ngep->chipinfo.device == DEVICE_ID_MCP51_268 ||
1111 	    ngep->chipinfo.device == DEVICE_ID_MCP51_269 ||
1112 	    ngep->chipinfo.device == DEVICE_ID_MCP55_372 ||
1113 	    ngep->chipinfo.device == DEVICE_ID_MCP55_373 ||
1114 	    ngep->chipinfo.device == DEVICE_ID_MCP61_3EE ||
1115 	    ngep->chipinfo.device == DEVICE_ID_MCP61_3EF ||
1116 	    ngep->chipinfo.device == DEVICE_ID_MCP77_760 ||
1117 	    ngep->chipinfo.device == DEVICE_ID_MCP79_AB0) {
1118 
1119 		pm_cntl2.cntl_val = nge_reg_get32(ngep, NGE_PM_CNTL2);
1120 		/* bring phy out of coma mode */
1121 		pm_cntl2.cntl_bits.phy_coma_set = NGE_CLEAR;
1122 		/* disable auto reset coma bits */
1123 		pm_cntl2.cntl_bits.resv4 = NGE_CLEAR;
1124 		/* restore power to gated clocks */
1125 		pm_cntl2.cntl_bits.resv8_11 = NGE_CLEAR;
1126 		nge_reg_put32(ngep, NGE_PM_CNTL2, pm_cntl2.cntl_val);
1127 	}
1128 
1129 	ngep->nge_chip_state = NGE_CHIP_RESET;
1130 	return (DDI_SUCCESS);
1131 }
1132 
1133 int
1134 nge_chip_start(nge_t *ngep)
1135 {
1136 	int err;
1137 	nge_itc itc;
1138 	nge_tx_cntl tx_cntl;
1139 	nge_rx_cntrl0 rx_cntl0;
1140 	nge_rx_cntl1 rx_cntl1;
1141 	nge_tx_en tx_en;
1142 	nge_rx_en rx_en;
1143 	nge_mii_cs mii_cs;
1144 	nge_swtr_cntl swtr_cntl;
1145 	nge_rx_fifo_wm rx_fifo;
1146 	nge_intr_mask intr_mask;
1147 	nge_mintr_mask mintr_mask;
1148 	nge_dev_spec_param_t	*dev_param_p;
1149 
1150 	NGE_TRACE(("nge_chip_start($%p)", (void *)ngep));
1151 
1152 	/*
1153 	 * Setup buffer management
1154 	 */
1155 	err = nge_buff_setup(ngep);
1156 	if (err == DDI_FAILURE)
1157 		return (err);
1158 
1159 	dev_param_p = &ngep->dev_spec_param;
1160 
1161 	/*
1162 	 * Enable polling attribute
1163 	 */
1164 	mii_cs.cs_val = nge_reg_get32(ngep, NGE_MII_CS);
1165 	mii_cs.cs_bits.ap_paddr = ngep->phy_xmii_addr;
1166 	mii_cs.cs_bits.ap_en = NGE_SET;
1167 	mii_cs.cs_bits.ap_intv = MII_POLL_INTV;
1168 	nge_reg_put32(ngep, NGE_MII_CS, mii_cs.cs_val);
1169 
1170 	/*
1171 	 * Setup link
1172 	 */
1173 	(*ngep->physops->phys_update)(ngep);
1174 
1175 	/*
1176 	 * Configure the tx's parameters
1177 	 */
1178 	tx_cntl.cntl_val = nge_reg_get32(ngep, NGE_TX_CNTL);
1179 	if (dev_param_p->tx_pause_frame)
1180 		tx_cntl.cntl_bits.paen = NGE_SET;
1181 	else
1182 		tx_cntl.cntl_bits.paen = NGE_CLEAR;
1183 	tx_cntl.cntl_bits.retry_en = NGE_SET;
1184 	tx_cntl.cntl_bits.pad_en = NGE_SET;
1185 	tx_cntl.cntl_bits.fappend_en = NGE_SET;
1186 	tx_cntl.cntl_bits.two_def_en = NGE_SET;
1187 	tx_cntl.cntl_bits.max_retry = 15;
1188 	tx_cntl.cntl_bits.burst_en = NGE_CLEAR;
1189 	tx_cntl.cntl_bits.uflo_err_mask = NGE_CLEAR;
1190 	tx_cntl.cntl_bits.tlcol_mask = NGE_CLEAR;
1191 	tx_cntl.cntl_bits.lcar_mask = NGE_CLEAR;
1192 	tx_cntl.cntl_bits.def_mask = NGE_CLEAR;
1193 	tx_cntl.cntl_bits.exdef_mask = NGE_SET;
1194 	tx_cntl.cntl_bits.lcar_mask = NGE_SET;
1195 	tx_cntl.cntl_bits.tlcol_mask = NGE_SET;
1196 	tx_cntl.cntl_bits.uflo_err_mask = NGE_SET;
1197 	tx_cntl.cntl_bits.jam_seq_en = NGE_CLEAR;
1198 	nge_reg_put32(ngep, NGE_TX_CNTL, tx_cntl.cntl_val);
1199 
1200 
1201 	/*
1202 	 * Configure the parameters of Rx's state machine
1203 	 * Enabe the parameters:
1204 	 * 1). Pad Strip
1205 	 * 2). FCS Relay
1206 	 * 3). Pause
1207 	 * 4). Address filter
1208 	 * 5). Runt Packet receive
1209 	 * 6). Broadcast
1210 	 * 7). Receive Deferral
1211 	 *
1212 	 * Disable the following parameters for decreasing
1213 	 * the number of interrupts:
1214 	 * 1). Runt Inerrupt.
1215 	 * 2). Rx's Late Collision interrupt.
1216 	 * 3). Rx's Max length Error Interrupt.
1217 	 * 4). Rx's Length Field error Interrupt.
1218 	 * 5). Rx's FCS error interrupt.
1219 	 * 6). Rx's overflow error interrupt.
1220 	 * 7). Rx's Frame alignment error interrupt.
1221 	 */
1222 	rx_cntl0.cntl_val = nge_reg_get32(ngep, NGE_RX_CNTL0);
1223 	rx_cntl0.cntl_bits.padsen = NGE_CLEAR;
1224 	rx_cntl0.cntl_bits.fcsren = NGE_CLEAR;
1225 	if (dev_param_p->rx_pause_frame)
1226 		rx_cntl0.cntl_bits.paen = NGE_SET;
1227 	else
1228 		rx_cntl0.cntl_bits.paen = NGE_CLEAR;
1229 	rx_cntl0.cntl_bits.lben = NGE_CLEAR;
1230 	rx_cntl0.cntl_bits.afen = NGE_SET;
1231 	rx_cntl0.cntl_bits.runten = NGE_CLEAR;
1232 	rx_cntl0.cntl_bits.brdis = NGE_CLEAR;
1233 	rx_cntl0.cntl_bits.rdfen = NGE_CLEAR;
1234 	rx_cntl0.cntl_bits.runtm = NGE_CLEAR;
1235 	rx_cntl0.cntl_bits.slfb = NGE_CLEAR;
1236 	rx_cntl0.cntl_bits.rlcolm = NGE_CLEAR;
1237 	rx_cntl0.cntl_bits.maxerm = NGE_CLEAR;
1238 	rx_cntl0.cntl_bits.lferm = NGE_CLEAR;
1239 	rx_cntl0.cntl_bits.crcm = NGE_CLEAR;
1240 	rx_cntl0.cntl_bits.ofolm = NGE_CLEAR;
1241 	rx_cntl0.cntl_bits.framerm = NGE_CLEAR;
1242 	nge_reg_put32(ngep, NGE_RX_CNTL0, rx_cntl0.cntl_val);
1243 
1244 	/*
1245 	 * Configure the watermark for the rx's statemachine
1246 	 */
1247 	rx_fifo.wm_val = nge_reg_get32(ngep, NGE_RX_FIFO_WM);
1248 	rx_fifo.wm_bits.data_hwm = ngep->rx_datahwm;
1249 	rx_fifo.wm_bits.prd_lwm = ngep->rx_prdlwm;
1250 	rx_fifo.wm_bits.prd_hwm = ngep->rx_prdhwm;
1251 	nge_reg_put32(ngep, NGE_RX_FIFO_WM, rx_fifo.wm_val);
1252 
1253 	/*
1254 	 * Configure the deffer time slot for rx's state machine
1255 	 */
1256 	nge_reg_put8(ngep, NGE_RX_DEf, ngep->rx_def);
1257 
1258 	/*
1259 	 * Configure the length of rx's packet
1260 	 */
1261 	rx_cntl1.cntl_val = nge_reg_get32(ngep, NGE_RX_CNTL1);
1262 	rx_cntl1.cntl_bits.length = ngep->max_sdu;
1263 	nge_reg_put32(ngep, NGE_RX_CNTL1, rx_cntl1.cntl_val);
1264 	/*
1265 	 * Enable Tx's state machine
1266 	 */
1267 	tx_en.val = nge_reg_get8(ngep, NGE_TX_EN);
1268 	tx_en.bits.tx_en = NGE_SET;
1269 	nge_reg_put8(ngep, NGE_TX_EN, tx_en.val);
1270 
1271 	/*
1272 	 * Enable Rx's state machine
1273 	 */
1274 	rx_en.val = nge_reg_get8(ngep, NGE_RX_EN);
1275 	rx_en.bits.rx_en = NGE_SET;
1276 	nge_reg_put8(ngep, NGE_RX_EN, rx_en.val);
1277 
1278 	itc.itc_val = nge_reg_get32(ngep, NGE_SWTR_ITC);
1279 	itc.itc_bits.sw_intv = ngep->sw_intr_intv;
1280 	nge_reg_put32(ngep, NGE_SWTR_ITC, itc.itc_val);
1281 
1282 	swtr_cntl.ctrl_val = nge_reg_get8(ngep, NGE_SWTR_CNTL);
1283 	swtr_cntl.cntl_bits.sten = NGE_SET;
1284 	swtr_cntl.cntl_bits.stren = NGE_SET;
1285 	nge_reg_put32(ngep, NGE_SWTR_CNTL, swtr_cntl.ctrl_val);
1286 
1287 	/*
1288 	 * Disable all mii read/write operation Interrupt
1289 	 */
1290 	mintr_mask.mask_val = nge_reg_get8(ngep, NGE_MINTR_MASK);
1291 	mintr_mask.mask_bits.mrei = NGE_CLEAR;
1292 	mintr_mask.mask_bits.mcc2 = NGE_CLEAR;
1293 	mintr_mask.mask_bits.mcc1 = NGE_CLEAR;
1294 	mintr_mask.mask_bits.mapi = NGE_SET;
1295 	mintr_mask.mask_bits.mpdi = NGE_SET;
1296 	nge_reg_put8(ngep, NGE_MINTR_MASK, mintr_mask.mask_val);
1297 
1298 	/*
1299 	 * Enable all interrupt event
1300 	 */
1301 	intr_mask.mask_val = nge_reg_get32(ngep, NGE_INTR_MASK);
1302 	intr_mask.mask_bits.reint = NGE_SET;
1303 	intr_mask.mask_bits.rcint = NGE_SET;
1304 	intr_mask.mask_bits.miss = NGE_SET;
1305 	intr_mask.mask_bits.teint = NGE_CLEAR;
1306 	intr_mask.mask_bits.tcint = NGE_SET;
1307 	intr_mask.mask_bits.stint = NGE_CLEAR;
1308 	intr_mask.mask_bits.mint = NGE_CLEAR;
1309 	intr_mask.mask_bits.rfint = NGE_CLEAR;
1310 	intr_mask.mask_bits.tfint = NGE_CLEAR;
1311 	intr_mask.mask_bits.feint = NGE_SET;
1312 	intr_mask.mask_bits.resv10 = NGE_CLEAR;
1313 	intr_mask.mask_bits.resv11 = NGE_CLEAR;
1314 	intr_mask.mask_bits.resv12 = NGE_CLEAR;
1315 	intr_mask.mask_bits.resv13 = NGE_CLEAR;
1316 	intr_mask.mask_bits.phyint = NGE_CLEAR;
1317 	ngep->intr_masks = intr_mask.mask_val;
1318 	nge_reg_put32(ngep, NGE_INTR_MASK, intr_mask.mask_val);
1319 	ngep->nge_chip_state = NGE_CHIP_RUNNING;
1320 	return (DDI_SUCCESS);
1321 }
1322 
1323 /*
1324  * nge_chip_sync() -- program the chip with the unicast MAC address,
1325  * the multicast hash table, the required level of promiscuity.
1326  */
1327 void
1328 nge_chip_sync(nge_t *ngep)
1329 {
1330 	uint8_t i;
1331 	uint64_t macaddr;
1332 	uint64_t mul_addr;
1333 	uint64_t mul_mask;
1334 	nge_rx_cntrl0 rx_cntl;
1335 	nge_uni_addr1 uni_adr1;
1336 
1337 	NGE_TRACE(("nge_chip_sync($%p)", (void *)ngep));
1338 
1339 	macaddr = 0x0ull;
1340 	mul_addr = 0x0ull;
1341 	mul_mask = 0x0ull;
1342 	rx_cntl.cntl_val = nge_reg_get32(ngep, NGE_RX_CNTL0);
1343 
1344 	if (ngep->promisc) {
1345 		rx_cntl.cntl_bits.afen = NGE_CLEAR;
1346 		rx_cntl.cntl_bits.brdis = NGE_SET;
1347 	} else {
1348 		rx_cntl.cntl_bits.afen = NGE_SET;
1349 		rx_cntl.cntl_bits.brdis = NGE_CLEAR;
1350 	}
1351 
1352 	/*
1353 	 * Transform the MAC address from host to chip format, the unicast
1354 	 * MAC address(es) ...
1355 	 */
1356 	for (i = ETHERADDRL, macaddr = 0ull; i != 0; --i) {
1357 		macaddr |= ngep->cur_uni_addr.addr[i-1];
1358 		macaddr <<= (i > 1) ? 8 : 0;
1359 	}
1360 
1361 	nge_reg_put32(ngep, NGE_UNI_ADDR0, (uint32_t)macaddr);
1362 	macaddr = macaddr >>32;
1363 	uni_adr1.addr_val = nge_reg_get32(ngep, NGE_UNI_ADDR1);
1364 	uni_adr1.addr_bits.addr = (uint16_t)macaddr;
1365 	uni_adr1.addr_bits.resv16_31 = (uint16_t)0;
1366 	nge_reg_put32(ngep, NGE_UNI_ADDR1, uni_adr1.addr_val);
1367 
1368 	/*
1369 	 * Reprogram the  multicast address table ...
1370 	 */
1371 	for (i = ETHERADDRL, mul_addr = 0ull; i != 0; --i) {
1372 		mul_addr |= ngep->cur_mul_addr.addr[i-1];
1373 		mul_addr <<= (i > 1) ? 8 : 0;
1374 		mul_mask |= ngep->cur_mul_mask.addr[i-1];
1375 		mul_mask <<= (i > 1) ? 8 : 0;
1376 	}
1377 	nge_reg_put32(ngep, NGE_MUL_ADDR0, (uint32_t)mul_addr);
1378 	mul_addr >>= 32;
1379 	nge_reg_put32(ngep, NGE_MUL_ADDR1, mul_addr);
1380 	nge_reg_put32(ngep, NGE_MUL_MASK, (uint32_t)mul_mask);
1381 	mul_mask >>= 32;
1382 	nge_reg_put32(ngep, NGE_MUL_MASK1, mul_mask);
1383 	/*
1384 	 * Set or clear the PROMISCUOUS mode bit
1385 	 */
1386 	nge_reg_put32(ngep, NGE_RX_CNTL0, rx_cntl.cntl_val);
1387 	/*
1388 	 * For internal PHY loopback, the link will
1389 	 * not be up, so it need to sync mac modes directly.
1390 	 */
1391 	if (ngep->param_loop_mode == NGE_LOOP_INTERNAL_PHY)
1392 		nge_sync_mac_modes(ngep);
1393 }
1394 
1395 static void
1396 nge_chip_err(nge_t *ngep)
1397 {
1398 	nge_reg010 reg010_ins;
1399 	nge_sw_statistics_t *psw_stat;
1400 	nge_intr_mask intr_mask;
1401 
1402 	NGE_TRACE(("nge_chip_err($%p)", (void *)ngep));
1403 
1404 	psw_stat = (nge_sw_statistics_t *)&ngep->statistics.sw_statistics;
1405 	reg010_ins.reg010_val = nge_reg_get32(ngep, NGE_REG010);
1406 	if (reg010_ins.reg010_bits.resv0)
1407 		psw_stat->fe_err.tso_err_mss ++;
1408 
1409 	if (reg010_ins.reg010_bits.resv1)
1410 		psw_stat->fe_err.tso_dis ++;
1411 
1412 	if (reg010_ins.reg010_bits.resv2)
1413 		psw_stat->fe_err.tso_err_nosum ++;
1414 
1415 	if (reg010_ins.reg010_bits.resv3)
1416 		psw_stat->fe_err.tso_err_hov ++;
1417 
1418 	if (reg010_ins.reg010_bits.resv4)
1419 		psw_stat->fe_err.tso_err_huf ++;
1420 
1421 	if (reg010_ins.reg010_bits.resv5)
1422 		psw_stat->fe_err.tso_err_l2 ++;
1423 
1424 	if (reg010_ins.reg010_bits.resv6)
1425 		psw_stat->fe_err.tso_err_ip ++;
1426 
1427 	if (reg010_ins.reg010_bits.resv7)
1428 		psw_stat->fe_err.tso_err_l4 ++;
1429 
1430 	if (reg010_ins.reg010_bits.resv8)
1431 		psw_stat->fe_err.tso_err_tcp ++;
1432 
1433 	if (reg010_ins.reg010_bits.resv9)
1434 		psw_stat->fe_err.hsum_err_ip ++;
1435 
1436 	if (reg010_ins.reg010_bits.resv10)
1437 		psw_stat->fe_err.hsum_err_l4 ++;
1438 
1439 	if (reg010_ins.reg010_val != 0) {
1440 
1441 		/*
1442 		 * Fatal error is triggered by malformed driver commands.
1443 		 * Disable unless debugging.
1444 		 */
1445 		intr_mask.mask_val = nge_reg_get32(ngep, NGE_INTR_MASK);
1446 		intr_mask.mask_bits.feint = NGE_CLEAR;
1447 		nge_reg_put32(ngep, NGE_INTR_MASK, intr_mask.mask_val);
1448 		ngep->intr_masks = intr_mask.mask_val;
1449 
1450 	}
1451 }
1452 
1453 static void
1454 nge_sync_mac_modes(nge_t *ngep)
1455 {
1456 	nge_tx_def tx_def;
1457 	nge_tx_fifo_wm tx_fifo;
1458 	nge_bkoff_cntl bk_cntl;
1459 	nge_mac2phy m2p;
1460 	nge_rx_cntrl0 rx_cntl0;
1461 	nge_dev_spec_param_t	*dev_param_p;
1462 
1463 	dev_param_p = &ngep->dev_spec_param;
1464 
1465 	tx_def.def_val = nge_reg_get32(ngep, NGE_TX_DEF);
1466 	m2p.m2p_val = nge_reg_get32(ngep, NGE_MAC2PHY);
1467 	tx_fifo.wm_val = nge_reg_get32(ngep, NGE_TX_FIFO_WM);
1468 	bk_cntl.cntl_val = nge_reg_get32(ngep, NGE_BKOFF_CNTL);
1469 	bk_cntl.bkoff_bits.rseed = BKOFF_RSEED;
1470 	switch (ngep->param_link_speed) {
1471 	case 10:
1472 		m2p.m2p_bits.speed = low_speed;
1473 		tx_def.def_bits.ifg1_def = TX_IFG1_DEFAULT;
1474 		if (ngep->phy_mode == RGMII_IN) {
1475 			tx_def.def_bits.ifg2_def = TX_IFG2_RGMII_10_100;
1476 			tx_def.def_bits.if_def = TX_IFG_RGMII_OTHER;
1477 		} else {
1478 			tx_def.def_bits.if_def = TX_TIFG_MII;
1479 			tx_def.def_bits.ifg2_def = TX_IFG2_MII;
1480 		}
1481 		tx_fifo.wm_bits.nbfb_wm = TX_FIFO_NOB_WM_MII;
1482 		bk_cntl.bkoff_bits.sltm = BKOFF_SLIM_MII;
1483 		break;
1484 
1485 	case 100:
1486 		m2p.m2p_bits.speed = fast_speed;
1487 		tx_def.def_bits.ifg1_def = TX_IFG1_DEFAULT;
1488 		if (ngep->phy_mode == RGMII_IN) {
1489 			tx_def.def_bits.ifg2_def = TX_IFG2_RGMII_10_100;
1490 			tx_def.def_bits.if_def = TX_IFG_RGMII_OTHER;
1491 		} else {
1492 			tx_def.def_bits.if_def = TX_TIFG_MII;
1493 			tx_def.def_bits.ifg2_def = TX_IFG2_MII;
1494 		}
1495 		tx_fifo.wm_bits.nbfb_wm = TX_FIFO_NOB_WM_MII;
1496 		bk_cntl.bkoff_bits.sltm = BKOFF_SLIM_MII;
1497 		break;
1498 
1499 	case 1000:
1500 		m2p.m2p_bits.speed = giga_speed;
1501 		tx_def.def_bits.ifg1_def = TX_IFG1_DEFAULT;
1502 		if (ngep->param_link_duplex == LINK_DUPLEX_FULL) {
1503 			tx_def.def_bits.ifg2_def = TX_IFG2_RGMII_1000;
1504 			tx_def.def_bits.if_def = TX_IFG_RGMII_1000_FD;
1505 		} else {
1506 			tx_def.def_bits.ifg2_def = TX_IFG2_RGMII_1000;
1507 			tx_def.def_bits.if_def = TX_IFG_RGMII_OTHER;
1508 		}
1509 
1510 		tx_fifo.wm_bits.nbfb_wm = TX_FIFO_NOB_WM_GMII;
1511 		bk_cntl.bkoff_bits.sltm = BKOFF_SLIM_GMII;
1512 		break;
1513 	}
1514 
1515 	if (ngep->chipinfo.device == DEVICE_ID_MCP55_373 ||
1516 	    ngep->chipinfo.device == DEVICE_ID_MCP55_372) {
1517 		m2p.m2p_bits.phyintr = NGE_CLEAR;
1518 		m2p.m2p_bits.phyintrlvl = NGE_CLEAR;
1519 	}
1520 	if (ngep->param_link_duplex == LINK_DUPLEX_HALF) {
1521 		m2p.m2p_bits.hdup_en = NGE_SET;
1522 	}
1523 	else
1524 		m2p.m2p_bits.hdup_en = NGE_CLEAR;
1525 	nge_reg_put32(ngep, NGE_MAC2PHY, m2p.m2p_val);
1526 	nge_reg_put32(ngep, NGE_TX_DEF, tx_def.def_val);
1527 
1528 	tx_fifo.wm_bits.data_lwm = TX_FIFO_DATA_LWM;
1529 	tx_fifo.wm_bits.prd_lwm = TX_FIFO_PRD_LWM;
1530 	tx_fifo.wm_bits.uprd_hwm = TX_FIFO_PRD_HWM;
1531 	tx_fifo.wm_bits.fb_wm = TX_FIFO_TBFW;
1532 	nge_reg_put32(ngep, NGE_TX_FIFO_WM, tx_fifo.wm_val);
1533 
1534 	nge_reg_put32(ngep, NGE_BKOFF_CNTL, bk_cntl.cntl_val);
1535 
1536 	rx_cntl0.cntl_val = nge_reg_get32(ngep, NGE_RX_CNTL0);
1537 	if (ngep->param_link_rx_pause && dev_param_p->rx_pause_frame)
1538 		rx_cntl0.cntl_bits.paen = NGE_SET;
1539 	else
1540 		rx_cntl0.cntl_bits.paen = NGE_CLEAR;
1541 	nge_reg_put32(ngep, NGE_RX_CNTL0, rx_cntl0.cntl_val);
1542 }
1543 
1544 /*
1545  * Handler for hardware link state change.
1546  *
1547  * When this routine is called, the hardware link state has changed
1548  * and the new state is reflected in the param_* variables.  Here
1549  * we must update the softstate, reprogram the MAC to match, and
1550  * record the change in the log and/or on the console.
1551  */
1552 static void
1553 nge_factotum_link_handler(nge_t *ngep)
1554 {
1555 	/*
1556 	 * Update the s/w link_state
1557 	 */
1558 	if (ngep->param_link_up)
1559 		ngep->link_state = LINK_STATE_UP;
1560 	else
1561 		ngep->link_state = LINK_STATE_DOWN;
1562 
1563 	/*
1564 	 * Reprogram the MAC modes to match
1565 	 */
1566 	nge_sync_mac_modes(ngep);
1567 }
1568 
1569 static boolean_t
1570 nge_factotum_link_check(nge_t *ngep)
1571 {
1572 	boolean_t lchg;
1573 	boolean_t check;
1574 
1575 	ASSERT(mutex_owned(ngep->genlock));
1576 
1577 	(*ngep->physops->phys_check)(ngep);
1578 	switch (ngep->link_state) {
1579 	case LINK_STATE_UP:
1580 		lchg = (ngep->param_link_up == B_FALSE);
1581 		check = (ngep->param_link_up == B_FALSE);
1582 		break;
1583 
1584 	case LINK_STATE_DOWN:
1585 		lchg = (ngep->param_link_up == B_TRUE);
1586 		check = (ngep->param_link_up == B_TRUE);
1587 		break;
1588 
1589 	default:
1590 		check = B_TRUE;
1591 		break;
1592 	}
1593 
1594 	/*
1595 	 * If <check> is false, we're sure the link hasn't changed.
1596 	 * If true, however, it's not yet definitive; we have to call
1597 	 * nge_phys_check() to determine whether the link has settled
1598 	 * into a new state yet ... and if it has, then call the link
1599 	 * state change handler.But when the chip is 5700 in Dell 6650
1600 	 * ,even if check is false, the link may have changed.So we
1601 	 * have to call nge_phys_check() to determine the link state.
1602 	 */
1603 	if (check)
1604 		nge_factotum_link_handler(ngep);
1605 
1606 	return (lchg);
1607 }
1608 
1609 /*
1610  * Factotum routine to check for Tx stall, using the 'watchdog' counter
1611  */
1612 static boolean_t nge_factotum_stall_check(nge_t *ngep);
1613 
1614 static boolean_t
1615 nge_factotum_stall_check(nge_t *ngep)
1616 {
1617 	uint32_t dogval;
1618 	/*
1619 	 * Specific check for Tx stall ...
1620 	 *
1621 	 * The 'watchdog' counter is incremented whenever a packet
1622 	 * is queued, reset to 1 when some (but not all) buffers
1623 	 * are reclaimed, reset to 0 (disabled) when all buffers
1624 	 * are reclaimed, and shifted left here.  If it exceeds the
1625 	 * threshold value, the chip is assumed to have stalled and
1626 	 * is put into the ERROR state.  The factotum will then reset
1627 	 * it on the next pass.
1628 	 *
1629 	 * All of which should ensure that we don't get into a state
1630 	 * where packets are left pending indefinitely!
1631 	 */
1632 	dogval = nge_atomic_shl32(&ngep->watchdog, 1);
1633 	if (dogval < nge_watchdog_count) {
1634 		ngep->stall_cknum = 0;
1635 	} else {
1636 		ngep->stall_cknum++;
1637 	}
1638 	if (ngep->stall_cknum < 16) {
1639 		return (B_FALSE);
1640 	} else {
1641 		ngep->stall_cknum = 0;
1642 		ngep->statistics.sw_statistics.tx_stall++;
1643 		return (B_TRUE);
1644 	}
1645 }
1646 
1647 
1648 
1649 /*
1650  * The factotum is woken up when there's something to do that we'd rather
1651  * not do from inside a hardware interrupt handler or high-level cyclic.
1652  * Its two main tasks are:
1653  *	reset & restart the chip after an error
1654  *	check the link status whenever necessary
1655  */
1656 /* ARGSUSED */
1657 uint_t
1658 nge_chip_factotum(caddr_t args1, caddr_t args2)
1659 {
1660 	uint_t result;
1661 	nge_t *ngep;
1662 	boolean_t err;
1663 	boolean_t linkchg;
1664 
1665 	ngep = (nge_t *)args1;
1666 
1667 	NGE_TRACE(("nge_chip_factotum($%p)", (void *)ngep));
1668 
1669 	mutex_enter(ngep->softlock);
1670 	if (ngep->factotum_flag == 0) {
1671 		mutex_exit(ngep->softlock);
1672 		return (DDI_INTR_UNCLAIMED);
1673 	}
1674 	ngep->factotum_flag = 0;
1675 	mutex_exit(ngep->softlock);
1676 	err = B_FALSE;
1677 	linkchg = B_FALSE;
1678 	result = DDI_INTR_CLAIMED;
1679 
1680 	mutex_enter(ngep->genlock);
1681 	switch (ngep->nge_chip_state) {
1682 	default:
1683 		break;
1684 
1685 	case NGE_CHIP_RUNNING:
1686 		linkchg = nge_factotum_link_check(ngep);
1687 		err = nge_factotum_stall_check(ngep);
1688 		break;
1689 
1690 	case NGE_CHIP_FAULT:
1691 		(void) nge_restart(ngep);
1692 		NGE_REPORT((ngep, "automatic recovery activated"));
1693 		break;
1694 	}
1695 
1696 	if (err)
1697 		(void) nge_chip_stop(ngep, B_TRUE);
1698 	mutex_exit(ngep->genlock);
1699 
1700 	/*
1701 	 * If the link state changed, tell the world about it (if
1702 	 * this version of MAC supports link state notification).
1703 	 * Note: can't do this while still holding the mutex.
1704 	 */
1705 	if (linkchg)
1706 		mac_link_update(ngep->mh, ngep->link_state);
1707 
1708 	return (result);
1709 
1710 }
1711 
1712 static void
1713 nge_intr_handle(nge_t *ngep, nge_intr_src *pintr_src)
1714 {
1715 	boolean_t brx;
1716 	boolean_t btx;
1717 	nge_mintr_src mintr_src;
1718 
1719 	brx = B_FALSE;
1720 	btx = B_FALSE;
1721 	ngep->statistics.sw_statistics.intr_count++;
1722 	ngep->statistics.sw_statistics.intr_lval = pintr_src->intr_val;
1723 	brx = (pintr_src->int_bits.reint | pintr_src->int_bits.miss
1724 	    | pintr_src->int_bits.rcint | pintr_src->int_bits.stint)
1725 	    != 0 ? B_TRUE : B_FALSE;
1726 	if (pintr_src->int_bits.reint)
1727 		ngep->statistics.sw_statistics.rx_err++;
1728 	if (pintr_src->int_bits.miss)
1729 		ngep->statistics.sw_statistics.rx_nobuffer++;
1730 
1731 	btx = (pintr_src->int_bits.teint | pintr_src->int_bits.tcint)
1732 	    != 0 ? B_TRUE : B_FALSE;
1733 	if (pintr_src->int_bits.stint && ngep->poll)
1734 		ngep->stint_count ++;
1735 	if (ngep->poll && (ngep->stint_count % ngep->param_tx_n_intr == 0))
1736 		btx = B_TRUE;
1737 	if (btx)
1738 		nge_tx_recycle(ngep, B_TRUE);
1739 	if (brx)
1740 		nge_receive(ngep);
1741 	if (pintr_src->int_bits.teint)
1742 		ngep->statistics.sw_statistics.tx_stop_err++;
1743 	if (ngep->intr_moderation && brx) {
1744 		if (ngep->poll) {
1745 			if (ngep->recv_count < ngep->param_rx_intr_hwater) {
1746 				ngep->quiet_time++;
1747 				if (ngep->quiet_time ==
1748 				    ngep->param_poll_quiet_time) {
1749 					ngep->poll = B_FALSE;
1750 					ngep->quiet_time = 0;
1751 					ngep->stint_count = 0;
1752 					nge_tx_recycle(ngep, B_TRUE);
1753 				}
1754 			} else
1755 				ngep->quiet_time = 0;
1756 		} else {
1757 			if (ngep->recv_count > ngep->param_rx_intr_lwater) {
1758 				ngep->busy_time++;
1759 				if (ngep->busy_time ==
1760 				    ngep->param_poll_busy_time) {
1761 					ngep->poll = B_TRUE;
1762 					ngep->busy_time = 0;
1763 				}
1764 			} else
1765 				ngep->busy_time = 0;
1766 		}
1767 	}
1768 	ngep->recv_count = 0;
1769 	if (pintr_src->int_bits.feint)
1770 		nge_chip_err(ngep);
1771 	/* link interrupt, check the link state */
1772 	if (pintr_src->int_bits.mint) {
1773 		mintr_src.src_val = nge_reg_get32(ngep, NGE_MINTR_SRC);
1774 		nge_reg_put32(ngep, NGE_MINTR_SRC, mintr_src.src_val);
1775 		nge_wake_factotum(ngep);
1776 	}
1777 }
1778 
1779 /*
1780  *	nge_chip_intr() -- handle chip interrupts
1781  */
1782 /* ARGSUSED */
1783 uint_t
1784 nge_chip_intr(caddr_t arg1, caddr_t arg2)
1785 {
1786 	nge_t *ngep = (nge_t *)arg1;
1787 	nge_intr_src intr_src;
1788 	nge_intr_mask intr_mask;
1789 
1790 	mutex_enter(ngep->genlock);
1791 
1792 	if (ngep->suspended) {
1793 		mutex_exit(ngep->genlock);
1794 		return (DDI_INTR_UNCLAIMED);
1795 	}
1796 
1797 	/*
1798 	 * Check whether chip's says it's asserting #INTA;
1799 	 * if not, don't process or claim the interrupt.
1800 	 */
1801 	intr_src.intr_val = nge_reg_get32(ngep, NGE_INTR_SRC);
1802 	if (intr_src.intr_val == 0) {
1803 		mutex_exit(ngep->genlock);
1804 		return (DDI_INTR_UNCLAIMED);
1805 	}
1806 	/*
1807 	 * Ack the interrupt
1808 	 */
1809 	nge_reg_put32(ngep, NGE_INTR_SRC, intr_src.intr_val);
1810 
1811 	if (ngep->nge_chip_state != NGE_CHIP_RUNNING) {
1812 		mutex_exit(ngep->genlock);
1813 		return (DDI_INTR_CLAIMED);
1814 	}
1815 	nge_intr_handle(ngep, &intr_src);
1816 	if (ngep->poll && !ngep->ch_intr_mode) {
1817 		intr_mask.mask_val = nge_reg_get32(ngep, NGE_INTR_MASK);
1818 		intr_mask.mask_bits.stint = NGE_SET;
1819 		intr_mask.mask_bits.rcint = NGE_CLEAR;
1820 		intr_mask.mask_bits.reint = NGE_CLEAR;
1821 		intr_mask.mask_bits.tcint = NGE_CLEAR;
1822 		intr_mask.mask_bits.teint = NGE_CLEAR;
1823 		nge_reg_put32(ngep, NGE_INTR_MASK, intr_mask.mask_val);
1824 		ngep->ch_intr_mode = B_TRUE;
1825 	} else if ((ngep->ch_intr_mode) && (!ngep->poll)) {
1826 		nge_reg_put32(ngep, NGE_INTR_MASK, ngep->intr_masks);
1827 		ngep->ch_intr_mode = B_FALSE;
1828 	}
1829 	mutex_exit(ngep->genlock);
1830 	return (DDI_INTR_CLAIMED);
1831 }
1832 
1833 static enum ioc_reply
1834 nge_pp_ioctl(nge_t *ngep, int cmd, mblk_t *mp, struct iocblk *iocp)
1835 {
1836 	int err;
1837 	uint64_t sizemask;
1838 	uint64_t mem_va;
1839 	uint64_t maxoff;
1840 	boolean_t peek;
1841 	nge_peekpoke_t *ppd;
1842 	int (*ppfn)(nge_t *ngep, nge_peekpoke_t *ppd);
1843 
1844 	switch (cmd) {
1845 	default:
1846 		return (IOC_INVAL);
1847 
1848 	case NGE_PEEK:
1849 		peek = B_TRUE;
1850 		break;
1851 
1852 	case NGE_POKE:
1853 		peek = B_FALSE;
1854 		break;
1855 	}
1856 
1857 	/*
1858 	 * Validate format of ioctl
1859 	 */
1860 	if (iocp->ioc_count != sizeof (nge_peekpoke_t))
1861 		return (IOC_INVAL);
1862 	if (mp->b_cont == NULL)
1863 		return (IOC_INVAL);
1864 	ppd = (nge_peekpoke_t *)mp->b_cont->b_rptr;
1865 
1866 	/*
1867 	 * Validate request parameters
1868 	 */
1869 	switch (ppd->pp_acc_space) {
1870 	default:
1871 		return (IOC_INVAL);
1872 
1873 	case NGE_PP_SPACE_CFG:
1874 		/*
1875 		 * Config space
1876 		 */
1877 		sizemask = 8|4|2|1;
1878 		mem_va = 0;
1879 		maxoff = PCI_CONF_HDR_SIZE;
1880 		ppfn = peek ? nge_chip_peek_cfg : nge_chip_poke_cfg;
1881 		break;
1882 
1883 	case NGE_PP_SPACE_REG:
1884 		/*
1885 		 * Memory-mapped I/O space
1886 		 */
1887 		sizemask = 8|4|2|1;
1888 		mem_va = 0;
1889 		maxoff = NGE_REG_SIZE;
1890 		ppfn = peek ? nge_chip_peek_reg : nge_chip_poke_reg;
1891 		break;
1892 
1893 	case NGE_PP_SPACE_MII:
1894 		sizemask = 4|2|1;
1895 		mem_va = 0;
1896 		maxoff = NGE_MII_SIZE;
1897 		ppfn = peek ? nge_chip_peek_mii : nge_chip_poke_mii;
1898 		break;
1899 
1900 	case NGE_PP_SPACE_SEEPROM:
1901 		sizemask = 4|2|1;
1902 		mem_va = 0;
1903 		maxoff = NGE_SEEROM_SIZE;
1904 		ppfn = peek ? nge_chip_peek_seeprom : nge_chip_poke_seeprom;
1905 		break;
1906 	}
1907 
1908 	switch (ppd->pp_acc_size) {
1909 	default:
1910 		return (IOC_INVAL);
1911 
1912 	case 8:
1913 	case 4:
1914 	case 2:
1915 	case 1:
1916 		if ((ppd->pp_acc_size & sizemask) == 0)
1917 			return (IOC_INVAL);
1918 		break;
1919 	}
1920 
1921 	if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
1922 		return (IOC_INVAL);
1923 
1924 	if (ppd->pp_acc_offset >= maxoff)
1925 		return (IOC_INVAL);
1926 
1927 	if (ppd->pp_acc_offset+ppd->pp_acc_size > maxoff)
1928 		return (IOC_INVAL);
1929 
1930 	/*
1931 	 * All OK - go do it!
1932 	 */
1933 	ppd->pp_acc_offset += mem_va;
1934 	if (ppfn)
1935 		err = (*ppfn)(ngep, ppd);
1936 	if (err != DDI_SUCCESS)
1937 		return (IOC_INVAL);
1938 	return (peek ? IOC_REPLY : IOC_ACK);
1939 }
1940 
1941 static enum ioc_reply nge_diag_ioctl(nge_t *ngep, int cmd, mblk_t *mp,
1942 					struct iocblk *iocp);
1943 #pragma	no_inline(nge_diag_ioctl)
1944 
1945 static enum ioc_reply
1946 nge_diag_ioctl(nge_t *ngep, int cmd, mblk_t *mp, struct iocblk *iocp)
1947 {
1948 	ASSERT(mutex_owned(ngep->genlock));
1949 
1950 	switch (cmd) {
1951 	default:
1952 		nge_error(ngep, "nge_diag_ioctl: invalid cmd 0x%x", cmd);
1953 		return (IOC_INVAL);
1954 
1955 	case NGE_DIAG:
1956 		return (IOC_ACK);
1957 
1958 	case NGE_PEEK:
1959 	case NGE_POKE:
1960 		return (nge_pp_ioctl(ngep, cmd, mp, iocp));
1961 
1962 	case NGE_PHY_RESET:
1963 		return (IOC_RESTART_ACK);
1964 
1965 	case NGE_SOFT_RESET:
1966 	case NGE_HARD_RESET:
1967 		return (IOC_ACK);
1968 	}
1969 
1970 	/* NOTREACHED */
1971 }
1972 
1973 enum ioc_reply
1974 nge_chip_ioctl(nge_t *ngep, mblk_t *mp, struct iocblk *iocp)
1975 {
1976 	int cmd;
1977 
1978 	ASSERT(mutex_owned(ngep->genlock));
1979 
1980 	cmd = iocp->ioc_cmd;
1981 
1982 	switch (cmd) {
1983 	default:
1984 		return (IOC_INVAL);
1985 
1986 	case NGE_DIAG:
1987 	case NGE_PEEK:
1988 	case NGE_POKE:
1989 	case NGE_PHY_RESET:
1990 	case NGE_SOFT_RESET:
1991 	case NGE_HARD_RESET:
1992 #if	NGE_DEBUGGING
1993 		return (nge_diag_ioctl(ngep, cmd, mp, iocp));
1994 #else
1995 		return (IOC_INVAL);
1996 #endif
1997 
1998 	case NGE_MII_READ:
1999 	case NGE_MII_WRITE:
2000 		return (IOC_INVAL);
2001 
2002 #if	NGE_SEE_IO32
2003 	case NGE_SEE_READ:
2004 	case NGE_SEE_WRITE:
2005 		return (IOC_INVAL);
2006 #endif
2007 
2008 #if	NGE_FLASH_IO32
2009 	case NGE_FLASH_READ:
2010 	case NGE_FLASH_WRITE:
2011 		return (IOC_INVAL);
2012 #endif
2013 	}
2014 }
2015