xref: /illumos-gate/usr/src/uts/common/io/e1000g/e1000g_stat.c (revision 4914a7d0d1ee59f8cc21b19bfd7979cb65681eac)
1 /*
2  * This file is provided under a CDDLv1 license.  When using or
3  * redistributing this file, you may do so under this license.
4  * In redistributing this file this license must be included
5  * and no other modification of this header file is permitted.
6  *
7  * CDDL LICENSE SUMMARY
8  *
9  * Copyright(c) 1999 - 2007 Intel Corporation. All rights reserved.
10  *
11  * The contents of this file are subject to the terms of Version
12  * 1.0 of the Common Development and Distribution License (the "License").
13  *
14  * You should have received a copy of the License with this software.
15  * You can obtain a copy of the License at
16  *	http://www.opensolaris.org/os/licensing.
17  * See the License for the specific language governing permissions
18  * and limitations under the License.
19  */
20 
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms of the CDDLv1.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * **********************************************************************
30  *									*
31  * Module Name:  e1000g_stat.c						*
32  *									*
33  * Abstract: Functions for processing statistics			*
34  *									*
35  * **********************************************************************
36  */
37 #include "e1000g_sw.h"
38 #include "e1000g_debug.h"
39 
40 static int e1000g_update_stats(kstat_t *ksp, int rw);
41 
42 /*
43  * e1000_tbi_adjust_stats
44  *
45  * Adjusts statistic counters when a frame is accepted
46  * under the TBI workaround. This function has been
47  * adapted for Solaris from shared code.
48  */
49 void
50 e1000_tbi_adjust_stats(struct e1000g *Adapter,
51     uint32_t frame_len, uint8_t *mac_addr)
52 {
53 	struct e1000_hw *hw = &Adapter->shared;
54 	uint32_t carry_bit;
55 	p_e1000g_stat_t e1000g_ksp;
56 
57 	e1000g_ksp = (p_e1000g_stat_t)Adapter->e1000g_ksp->ks_data;
58 
59 	/* First adjust the frame length */
60 	frame_len--;
61 
62 	/*
63 	 * We need to adjust the statistics counters, since the hardware
64 	 * counters overcount this packet as a CRC error and undercount
65 	 * the packet as a good packet
66 	 */
67 	/* This packet should not be counted as a CRC error */
68 	e1000g_ksp->Crcerrs.value.ul--;
69 	/* This packet does count as a Good Packet Received */
70 	e1000g_ksp->Gprc.value.ul++;
71 
72 	/*
73 	 * Adjust the Good Octets received counters
74 	 */
75 	carry_bit = 0x80000000 & e1000g_ksp->Gorl.value.ul;
76 	e1000g_ksp->Gorl.value.ul += frame_len;
77 	/*
78 	 * If the high bit of Gorcl (the low 32 bits of the Good Octets
79 	 * Received Count) was one before the addition,
80 	 * AND it is zero after, then we lost the carry out,
81 	 * need to add one to Gorch (Good Octets Received Count High).
82 	 * This could be simplified if all environments supported
83 	 * 64-bit integers.
84 	 */
85 	if (carry_bit && ((e1000g_ksp->Gorl.value.ul & 0x80000000) == 0)) {
86 		e1000g_ksp->Gorh.value.ul++;
87 	}
88 	/*
89 	 * Is this a broadcast or multicast?  Check broadcast first,
90 	 * since the test for a multicast frame will test positive on
91 	 * a broadcast frame.
92 	 */
93 	if ((mac_addr[0] == (uint8_t)0xff) &&
94 	    (mac_addr[1] == (uint8_t)0xff)) {
95 		/*
96 		 * Broadcast packet
97 		 */
98 		e1000g_ksp->Bprc.value.ul++;
99 	} else if (*mac_addr & 0x01) {
100 		/*
101 		 * Multicast packet
102 		 */
103 		e1000g_ksp->Mprc.value.ul++;
104 	}
105 
106 	if (frame_len == hw->mac.max_frame_size) {
107 		/*
108 		 * In this case, the hardware has overcounted the number of
109 		 * oversize frames.
110 		 */
111 		if (e1000g_ksp->Roc.value.ul > 0)
112 			e1000g_ksp->Roc.value.ul--;
113 	}
114 
115 	/*
116 	 * Adjust the bin counters when the extra byte put the frame in the
117 	 * wrong bin. Remember that the frame_len was adjusted above.
118 	 */
119 	if (frame_len == 64) {
120 		e1000g_ksp->Prc64.value.ul++;
121 		e1000g_ksp->Prc127.value.ul--;
122 	} else if (frame_len == 127) {
123 		e1000g_ksp->Prc127.value.ul++;
124 		e1000g_ksp->Prc255.value.ul--;
125 	} else if (frame_len == 255) {
126 		e1000g_ksp->Prc255.value.ul++;
127 		e1000g_ksp->Prc511.value.ul--;
128 	} else if (frame_len == 511) {
129 		e1000g_ksp->Prc511.value.ul++;
130 		e1000g_ksp->Prc1023.value.ul--;
131 	} else if (frame_len == 1023) {
132 		e1000g_ksp->Prc1023.value.ul++;
133 		e1000g_ksp->Prc1522.value.ul--;
134 	} else if (frame_len == 1522) {
135 		e1000g_ksp->Prc1522.value.ul++;
136 	}
137 }
138 
139 
140 /*
141  * e1000g_update_stats - update driver private kstat counters
142  *
143  * This routine will dump and reset the e1000's internal
144  * statistics counters. The current stats dump values will
145  * be sent to the kernel status area.
146  */
147 static int
148 e1000g_update_stats(kstat_t *ksp, int rw)
149 {
150 	struct e1000g *Adapter;
151 	struct e1000_hw *hw;
152 	p_e1000g_stat_t e1000g_ksp;
153 	e1000g_tx_ring_t *tx_ring;
154 	e1000g_rx_ring_t *rx_ring;
155 	uint64_t val;
156 	uint32_t low_val, high_val;
157 
158 	if (rw == KSTAT_WRITE)
159 		return (EACCES);
160 
161 	Adapter = (struct e1000g *)ksp->ks_private;
162 	ASSERT(Adapter != NULL);
163 	e1000g_ksp = (p_e1000g_stat_t)ksp->ks_data;
164 	ASSERT(e1000g_ksp != NULL);
165 	hw = &Adapter->shared;
166 
167 	tx_ring = Adapter->tx_ring;
168 	rx_ring = Adapter->rx_ring;
169 
170 	rw_enter(&Adapter->chip_lock, RW_WRITER);
171 
172 	e1000g_ksp->link_speed.value.ul = Adapter->link_speed;
173 	e1000g_ksp->reset_count.value.ul = Adapter->reset_count;
174 
175 	e1000g_ksp->rx_error.value.ul = rx_ring->stat_error;
176 	e1000g_ksp->rx_esballoc_fail.value.ul = rx_ring->stat_esballoc_fail;
177 	e1000g_ksp->rx_allocb_fail.value.ul = rx_ring->stat_allocb_fail;
178 	e1000g_ksp->rx_exceed_pkt.value.ul = rx_ring->stat_exceed_pkt;
179 
180 	e1000g_ksp->tx_no_swpkt.value.ul = tx_ring->stat_no_swpkt;
181 	e1000g_ksp->tx_no_desc.value.ul = tx_ring->stat_no_desc;
182 	e1000g_ksp->tx_send_fail.value.ul = tx_ring->stat_send_fail;
183 	e1000g_ksp->tx_reschedule.value.ul = tx_ring->stat_reschedule;
184 	e1000g_ksp->tx_over_size.value.ul = tx_ring->stat_over_size;
185 
186 #ifdef E1000G_DEBUG
187 	e1000g_ksp->rx_none.value.ul = rx_ring->stat_none;
188 	e1000g_ksp->rx_multi_desc.value.ul = rx_ring->stat_multi_desc;
189 	e1000g_ksp->rx_no_freepkt.value.ul = rx_ring->stat_no_freepkt;
190 	e1000g_ksp->rx_avail_freepkt.value.ul = rx_ring->avail_freepkt;
191 
192 	e1000g_ksp->tx_under_size.value.ul = tx_ring->stat_under_size;
193 	e1000g_ksp->tx_exceed_frags.value.ul = tx_ring->stat_exceed_frags;
194 	e1000g_ksp->tx_empty_frags.value.ul = tx_ring->stat_empty_frags;
195 	e1000g_ksp->tx_recycle.value.ul = tx_ring->stat_recycle;
196 	e1000g_ksp->tx_recycle_intr.value.ul = tx_ring->stat_recycle_intr;
197 	e1000g_ksp->tx_recycle_retry.value.ul = tx_ring->stat_recycle_retry;
198 	e1000g_ksp->tx_recycle_none.value.ul = tx_ring->stat_recycle_none;
199 	e1000g_ksp->tx_copy.value.ul = tx_ring->stat_copy;
200 	e1000g_ksp->tx_bind.value.ul = tx_ring->stat_bind;
201 	e1000g_ksp->tx_multi_copy.value.ul = tx_ring->stat_multi_copy;
202 	e1000g_ksp->tx_multi_cookie.value.ul = tx_ring->stat_multi_cookie;
203 	e1000g_ksp->tx_lack_desc.value.ul = tx_ring->stat_lack_desc;
204 #endif
205 
206 	/*
207 	 * Standard Stats
208 	 */
209 	e1000g_ksp->Mpc.value.ul += E1000_READ_REG(hw, E1000_MPC);
210 	e1000g_ksp->Rlec.value.ul += E1000_READ_REG(hw, E1000_RLEC);
211 	e1000g_ksp->Xonrxc.value.ul += E1000_READ_REG(hw, E1000_XONRXC);
212 	e1000g_ksp->Xontxc.value.ul += E1000_READ_REG(hw, E1000_XONTXC);
213 	e1000g_ksp->Xoffrxc.value.ul += E1000_READ_REG(hw, E1000_XOFFRXC);
214 	e1000g_ksp->Xofftxc.value.ul += E1000_READ_REG(hw, E1000_XOFFTXC);
215 	e1000g_ksp->Fcruc.value.ul += E1000_READ_REG(hw, E1000_FCRUC);
216 
217 	if ((hw->mac.type != e1000_ich8lan) &&
218 	    (hw->mac.type != e1000_ich9lan)) {
219 		e1000g_ksp->Symerrs.value.ul +=
220 		    E1000_READ_REG(hw, E1000_SYMERRS);
221 
222 		e1000g_ksp->Prc64.value.ul +=
223 		    E1000_READ_REG(hw, E1000_PRC64);
224 		e1000g_ksp->Prc127.value.ul +=
225 		    E1000_READ_REG(hw, E1000_PRC127);
226 		e1000g_ksp->Prc255.value.ul +=
227 		    E1000_READ_REG(hw, E1000_PRC255);
228 		e1000g_ksp->Prc511.value.ul +=
229 		    E1000_READ_REG(hw, E1000_PRC511);
230 		e1000g_ksp->Prc1023.value.ul +=
231 		    E1000_READ_REG(hw, E1000_PRC1023);
232 		e1000g_ksp->Prc1522.value.ul +=
233 		    E1000_READ_REG(hw, E1000_PRC1522);
234 
235 		e1000g_ksp->Ptc64.value.ul +=
236 		    E1000_READ_REG(hw, E1000_PTC64);
237 		e1000g_ksp->Ptc127.value.ul +=
238 		    E1000_READ_REG(hw, E1000_PTC127);
239 		e1000g_ksp->Ptc255.value.ul +=
240 		    E1000_READ_REG(hw, E1000_PTC255);
241 		e1000g_ksp->Ptc511.value.ul +=
242 		    E1000_READ_REG(hw, E1000_PTC511);
243 		e1000g_ksp->Ptc1023.value.ul +=
244 		    E1000_READ_REG(hw, E1000_PTC1023);
245 		e1000g_ksp->Ptc1522.value.ul +=
246 		    E1000_READ_REG(hw, E1000_PTC1522);
247 	}
248 
249 	e1000g_ksp->Gprc.value.ul += E1000_READ_REG(hw, E1000_GPRC);
250 	e1000g_ksp->Gptc.value.ul += E1000_READ_REG(hw, E1000_GPTC);
251 	e1000g_ksp->Ruc.value.ul += E1000_READ_REG(hw, E1000_RUC);
252 	e1000g_ksp->Rfc.value.ul += E1000_READ_REG(hw, E1000_RFC);
253 	e1000g_ksp->Roc.value.ul += E1000_READ_REG(hw, E1000_ROC);
254 	e1000g_ksp->Rjc.value.ul += E1000_READ_REG(hw, E1000_RJC);
255 	e1000g_ksp->Tpr.value.ul += E1000_READ_REG(hw, E1000_TPR);
256 	e1000g_ksp->Tncrs.value.ul += E1000_READ_REG(hw, E1000_TNCRS);
257 	e1000g_ksp->Tsctc.value.ul += E1000_READ_REG(hw, E1000_TSCTC);
258 	e1000g_ksp->Tsctfc.value.ul += E1000_READ_REG(hw, E1000_TSCTFC);
259 
260 	/*
261 	 * Adaptive Calculations
262 	 */
263 	hw->mac.tx_packet_delta = E1000_READ_REG(hw, E1000_TPT);
264 	e1000g_ksp->Tpt.value.ul += hw->mac.tx_packet_delta;
265 
266 	/*
267 	 * The 64-bit register will reset whenever the upper
268 	 * 32 bits are read. So we need to read the lower
269 	 * 32 bits first, then read the upper 32 bits.
270 	 */
271 	low_val = E1000_READ_REG(hw, E1000_GORCL);
272 	high_val = E1000_READ_REG(hw, E1000_GORCH);
273 	val = (uint64_t)e1000g_ksp->Gorh.value.ul << 32 |
274 	    (uint64_t)e1000g_ksp->Gorl.value.ul;
275 	val += (uint64_t)high_val << 32 | (uint64_t)low_val;
276 	e1000g_ksp->Gorl.value.ul = (uint32_t)val;
277 	e1000g_ksp->Gorh.value.ul = (uint32_t)(val >> 32);
278 
279 	low_val = E1000_READ_REG(hw, E1000_GOTCL);
280 	high_val = E1000_READ_REG(hw, E1000_GOTCH);
281 	val = (uint64_t)e1000g_ksp->Goth.value.ul << 32 |
282 	    (uint64_t)e1000g_ksp->Gotl.value.ul;
283 	val += (uint64_t)high_val << 32 | (uint64_t)low_val;
284 	e1000g_ksp->Gotl.value.ul = (uint32_t)val;
285 	e1000g_ksp->Goth.value.ul = (uint32_t)(val >> 32);
286 
287 	low_val = E1000_READ_REG(hw, E1000_TORL);
288 	high_val = E1000_READ_REG(hw, E1000_TORH);
289 	val = (uint64_t)e1000g_ksp->Torh.value.ul << 32 |
290 	    (uint64_t)e1000g_ksp->Torl.value.ul;
291 	val += (uint64_t)high_val << 32 | (uint64_t)low_val;
292 	e1000g_ksp->Torl.value.ul = (uint32_t)val;
293 	e1000g_ksp->Torh.value.ul = (uint32_t)(val >> 32);
294 
295 	low_val = E1000_READ_REG(hw, E1000_TOTL);
296 	high_val = E1000_READ_REG(hw, E1000_TOTH);
297 	val = (uint64_t)e1000g_ksp->Toth.value.ul << 32 |
298 	    (uint64_t)e1000g_ksp->Totl.value.ul;
299 	val += (uint64_t)high_val << 32 | (uint64_t)low_val;
300 	e1000g_ksp->Totl.value.ul = (uint32_t)val;
301 	e1000g_ksp->Toth.value.ul = (uint32_t)(val >> 32);
302 
303 	rw_exit(&Adapter->chip_lock);
304 
305 	return (0);
306 }
307 
308 int
309 e1000g_m_stat(void *arg, uint_t stat, uint64_t *val)
310 {
311 	struct e1000g *Adapter = (struct e1000g *)arg;
312 	struct e1000_hw *hw = &Adapter->shared;
313 	p_e1000g_stat_t e1000g_ksp;
314 	uint32_t low_val, high_val;
315 
316 	e1000g_ksp = (p_e1000g_stat_t)Adapter->e1000g_ksp->ks_data;
317 
318 	rw_enter(&Adapter->chip_lock, RW_READER);
319 
320 	switch (stat) {
321 	case MAC_STAT_IFSPEED:
322 		*val = Adapter->link_speed * 1000000ull;
323 		break;
324 
325 	case MAC_STAT_MULTIRCV:
326 		e1000g_ksp->Mprc.value.ul +=
327 		    E1000_READ_REG(hw, E1000_MPRC);
328 		*val = e1000g_ksp->Mprc.value.ul;
329 		break;
330 
331 	case MAC_STAT_BRDCSTRCV:
332 		e1000g_ksp->Bprc.value.ul +=
333 		    E1000_READ_REG(hw, E1000_BPRC);
334 		*val = e1000g_ksp->Bprc.value.ul;
335 		break;
336 
337 	case MAC_STAT_MULTIXMT:
338 		e1000g_ksp->Mptc.value.ul +=
339 		    E1000_READ_REG(hw, E1000_MPTC);
340 		*val = e1000g_ksp->Mptc.value.ul;
341 		break;
342 
343 	case MAC_STAT_BRDCSTXMT:
344 		e1000g_ksp->Bptc.value.ul +=
345 		    E1000_READ_REG(hw, E1000_BPTC);
346 		*val = e1000g_ksp->Bptc.value.ul;
347 		break;
348 
349 	case MAC_STAT_NORCVBUF:
350 		e1000g_ksp->Rnbc.value.ul +=
351 		    E1000_READ_REG(hw, E1000_RNBC);
352 		*val = e1000g_ksp->Rnbc.value.ul;
353 		break;
354 
355 	case MAC_STAT_IERRORS:
356 		e1000g_ksp->Rxerrc.value.ul +=
357 		    E1000_READ_REG(hw, E1000_RXERRC);
358 		e1000g_ksp->Algnerrc.value.ul +=
359 		    E1000_READ_REG(hw, E1000_ALGNERRC);
360 		e1000g_ksp->Rlec.value.ul +=
361 		    E1000_READ_REG(hw, E1000_RLEC);
362 		e1000g_ksp->Crcerrs.value.ul +=
363 		    E1000_READ_REG(hw, E1000_CRCERRS);
364 		e1000g_ksp->Cexterr.value.ul +=
365 		    E1000_READ_REG(hw, E1000_CEXTERR);
366 		*val = e1000g_ksp->Rxerrc.value.ul +
367 		    e1000g_ksp->Algnerrc.value.ul +
368 		    e1000g_ksp->Rlec.value.ul +
369 		    e1000g_ksp->Crcerrs.value.ul +
370 		    e1000g_ksp->Cexterr.value.ul;
371 		break;
372 
373 	case MAC_STAT_NOXMTBUF:
374 		*val = Adapter->tx_ring->stat_no_desc;
375 		break;
376 
377 	case MAC_STAT_OERRORS:
378 		e1000g_ksp->Ecol.value.ul +=
379 		    E1000_READ_REG(hw, E1000_ECOL);
380 		*val = e1000g_ksp->Ecol.value.ul;
381 		break;
382 
383 	case MAC_STAT_COLLISIONS:
384 		e1000g_ksp->Colc.value.ul +=
385 		    E1000_READ_REG(hw, E1000_COLC);
386 		*val = e1000g_ksp->Colc.value.ul;
387 		break;
388 
389 	case MAC_STAT_RBYTES:
390 		/*
391 		 * The 64-bit register will reset whenever the upper
392 		 * 32 bits are read. So we need to read the lower
393 		 * 32 bits first, then read the upper 32 bits.
394 		 */
395 		low_val = E1000_READ_REG(hw, E1000_TORL);
396 		high_val = E1000_READ_REG(hw, E1000_TORH);
397 		*val = (uint64_t)e1000g_ksp->Torh.value.ul << 32 |
398 		    (uint64_t)e1000g_ksp->Torl.value.ul;
399 		*val += (uint64_t)high_val << 32 | (uint64_t)low_val;
400 
401 		e1000g_ksp->Torl.value.ul = (uint32_t)*val;
402 		e1000g_ksp->Torh.value.ul = (uint32_t)(*val >> 32);
403 		break;
404 
405 	case MAC_STAT_IPACKETS:
406 		e1000g_ksp->Tpr.value.ul +=
407 		    E1000_READ_REG(hw, E1000_TPR);
408 		*val = e1000g_ksp->Tpr.value.ul;
409 		break;
410 
411 	case MAC_STAT_OBYTES:
412 		/*
413 		 * The 64-bit register will reset whenever the upper
414 		 * 32 bits are read. So we need to read the lower
415 		 * 32 bits first, then read the upper 32 bits.
416 		 */
417 		low_val = E1000_READ_REG(hw, E1000_TOTL);
418 		high_val = E1000_READ_REG(hw, E1000_TOTH);
419 		*val = (uint64_t)e1000g_ksp->Toth.value.ul << 32 |
420 		    (uint64_t)e1000g_ksp->Totl.value.ul;
421 		*val += (uint64_t)high_val << 32 | (uint64_t)low_val;
422 
423 		e1000g_ksp->Totl.value.ul = (uint32_t)*val;
424 		e1000g_ksp->Toth.value.ul = (uint32_t)(*val >> 32);
425 		break;
426 
427 	case MAC_STAT_OPACKETS:
428 		e1000g_ksp->Tpt.value.ul +=
429 		    E1000_READ_REG(hw, E1000_TPT);
430 		*val = e1000g_ksp->Tpt.value.ul;
431 		break;
432 
433 	case ETHER_STAT_ALIGN_ERRORS:
434 		e1000g_ksp->Algnerrc.value.ul +=
435 		    E1000_READ_REG(hw, E1000_ALGNERRC);
436 		*val = e1000g_ksp->Algnerrc.value.ul;
437 		break;
438 
439 	case ETHER_STAT_FCS_ERRORS:
440 		e1000g_ksp->Crcerrs.value.ul +=
441 		    E1000_READ_REG(hw, E1000_CRCERRS);
442 		*val = e1000g_ksp->Crcerrs.value.ul;
443 		break;
444 
445 	case ETHER_STAT_SQE_ERRORS:
446 		e1000g_ksp->Sec.value.ul +=
447 		    E1000_READ_REG(hw, E1000_SEC);
448 		*val = e1000g_ksp->Sec.value.ul;
449 		break;
450 
451 	case ETHER_STAT_CARRIER_ERRORS:
452 		e1000g_ksp->Cexterr.value.ul +=
453 		    E1000_READ_REG(hw, E1000_CEXTERR);
454 		*val = e1000g_ksp->Cexterr.value.ul;
455 		break;
456 
457 	case ETHER_STAT_EX_COLLISIONS:
458 		e1000g_ksp->Ecol.value.ul +=
459 		    E1000_READ_REG(hw, E1000_ECOL);
460 		*val = e1000g_ksp->Ecol.value.ul;
461 		break;
462 
463 	case ETHER_STAT_TX_LATE_COLLISIONS:
464 		e1000g_ksp->Latecol.value.ul +=
465 		    E1000_READ_REG(hw, E1000_LATECOL);
466 		*val = e1000g_ksp->Latecol.value.ul;
467 		break;
468 
469 	case ETHER_STAT_DEFER_XMTS:
470 		e1000g_ksp->Dc.value.ul +=
471 		    E1000_READ_REG(hw, E1000_DC);
472 		*val = e1000g_ksp->Dc.value.ul;
473 		break;
474 
475 	case ETHER_STAT_FIRST_COLLISIONS:
476 		e1000g_ksp->Scc.value.ul +=
477 		    E1000_READ_REG(hw, E1000_SCC);
478 		*val = e1000g_ksp->Scc.value.ul;
479 		break;
480 
481 	case ETHER_STAT_MULTI_COLLISIONS:
482 		e1000g_ksp->Mcc.value.ul +=
483 		    E1000_READ_REG(hw, E1000_MCC);
484 		*val = e1000g_ksp->Mcc.value.ul;
485 		break;
486 
487 	case ETHER_STAT_MACRCV_ERRORS:
488 		e1000g_ksp->Rxerrc.value.ul +=
489 		    E1000_READ_REG(hw, E1000_RXERRC);
490 		*val = e1000g_ksp->Rxerrc.value.ul;
491 		break;
492 
493 	case ETHER_STAT_MACXMT_ERRORS:
494 		e1000g_ksp->Ecol.value.ul +=
495 		    E1000_READ_REG(hw, E1000_ECOL);
496 		*val = e1000g_ksp->Ecol.value.ul;
497 		break;
498 
499 	case ETHER_STAT_TOOLONG_ERRORS:
500 		e1000g_ksp->Roc.value.ul +=
501 		    E1000_READ_REG(hw, E1000_ROC);
502 		*val = e1000g_ksp->Roc.value.ul;
503 		break;
504 
505 	case ETHER_STAT_XCVR_ADDR:
506 		/* The Internal PHY's MDI address for each MAC is 1 */
507 		*val = 1;
508 		break;
509 
510 	case ETHER_STAT_XCVR_ID:
511 		*val = hw->phy.id | hw->phy.revision;
512 		break;
513 
514 	case ETHER_STAT_XCVR_INUSE:
515 		switch (Adapter->link_speed) {
516 		case SPEED_1000:
517 			*val =
518 			    (hw->media_type == e1000_media_type_copper) ?
519 			    XCVR_1000T : XCVR_1000X;
520 			break;
521 		case SPEED_100:
522 			*val =
523 			    (hw->media_type == e1000_media_type_copper) ?
524 			    (Adapter->phy_status & MII_SR_100T4_CAPS) ?
525 			    XCVR_100T4 : XCVR_100T2 : XCVR_100X;
526 			break;
527 		case SPEED_10:
528 			*val = XCVR_10;
529 			break;
530 		default:
531 			*val = XCVR_NONE;
532 			break;
533 		}
534 		break;
535 
536 	case ETHER_STAT_CAP_1000FDX:
537 		*val = ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
538 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
539 		break;
540 
541 	case ETHER_STAT_CAP_1000HDX:
542 		*val = ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
543 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
544 		break;
545 
546 	case ETHER_STAT_CAP_100FDX:
547 		*val = ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
548 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
549 		break;
550 
551 	case ETHER_STAT_CAP_100HDX:
552 		*val = ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
553 		    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
554 		break;
555 
556 	case ETHER_STAT_CAP_10FDX:
557 		*val = (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
558 		break;
559 
560 	case ETHER_STAT_CAP_10HDX:
561 		*val = (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
562 		break;
563 
564 	case ETHER_STAT_CAP_ASMPAUSE:
565 		*val = (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
566 		break;
567 
568 	case ETHER_STAT_CAP_PAUSE:
569 		*val = (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
570 		break;
571 
572 	case ETHER_STAT_CAP_AUTONEG:
573 		*val = (Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
574 		break;
575 
576 	case ETHER_STAT_ADV_CAP_1000FDX:
577 		*val = (Adapter->phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
578 		break;
579 
580 	case ETHER_STAT_ADV_CAP_1000HDX:
581 		*val = (Adapter->phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
582 		break;
583 
584 	case ETHER_STAT_ADV_CAP_100FDX:
585 		*val = (Adapter->phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
586 		break;
587 
588 	case ETHER_STAT_ADV_CAP_100HDX:
589 		*val = (Adapter->phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
590 		break;
591 
592 	case ETHER_STAT_ADV_CAP_10FDX:
593 		*val = (Adapter->phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
594 		break;
595 
596 	case ETHER_STAT_ADV_CAP_10HDX:
597 		*val = (Adapter->phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
598 		break;
599 
600 	case ETHER_STAT_ADV_CAP_ASMPAUSE:
601 		*val = (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
602 		break;
603 
604 	case ETHER_STAT_ADV_CAP_PAUSE:
605 		*val = (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
606 		break;
607 
608 	case ETHER_STAT_ADV_CAP_AUTONEG:
609 		*val = hw->mac.autoneg;
610 		break;
611 
612 	case ETHER_STAT_LP_CAP_1000FDX:
613 		*val =
614 		    (Adapter->phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
615 		break;
616 
617 	case ETHER_STAT_LP_CAP_1000HDX:
618 		*val =
619 		    (Adapter->phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
620 		break;
621 
622 	case ETHER_STAT_LP_CAP_100FDX:
623 		*val = (Adapter->phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
624 		break;
625 
626 	case ETHER_STAT_LP_CAP_100HDX:
627 		*val = (Adapter->phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
628 		break;
629 
630 	case ETHER_STAT_LP_CAP_10FDX:
631 		*val = (Adapter->phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
632 		break;
633 
634 	case ETHER_STAT_LP_CAP_10HDX:
635 		*val = (Adapter->phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
636 		break;
637 
638 	case ETHER_STAT_LP_CAP_ASMPAUSE:
639 		*val = (Adapter->phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
640 		break;
641 
642 	case ETHER_STAT_LP_CAP_PAUSE:
643 		*val = (Adapter->phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
644 		break;
645 
646 	case ETHER_STAT_LP_CAP_AUTONEG:
647 		*val = (Adapter->phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
648 		break;
649 
650 	case ETHER_STAT_LINK_ASMPAUSE:
651 		*val = (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
652 		break;
653 
654 	case ETHER_STAT_LINK_PAUSE:
655 		*val = (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
656 		break;
657 
658 	case ETHER_STAT_LINK_AUTONEG:
659 		*val = (Adapter->phy_ctrl & MII_CR_AUTO_NEG_EN) ? 1 : 0;
660 		break;
661 
662 	case ETHER_STAT_LINK_DUPLEX:
663 		*val = (Adapter->link_duplex == FULL_DUPLEX) ?
664 		    LINK_DUPLEX_FULL : LINK_DUPLEX_HALF;
665 		break;
666 
667 	default:
668 		rw_exit(&Adapter->chip_lock);
669 		return (ENOTSUP);
670 	}
671 
672 	rw_exit(&Adapter->chip_lock);
673 
674 	return (0);
675 }
676 
677 /*
678  * e1000g_init_stats - initialize kstat data structures
679  *
680  * This routine will create and initialize the driver private
681  * statistics counters.
682  */
683 int
684 e1000g_init_stats(struct e1000g *Adapter)
685 {
686 	kstat_t *ksp;
687 	p_e1000g_stat_t e1000g_ksp;
688 
689 	/*
690 	 * Create and init kstat
691 	 */
692 	ksp = kstat_create(WSNAME, ddi_get_instance(Adapter->dip),
693 	    "statistics", "net", KSTAT_TYPE_NAMED,
694 	    sizeof (e1000g_stat_t) / sizeof (kstat_named_t), 0);
695 
696 	if (ksp == NULL) {
697 		e1000g_log(Adapter, CE_WARN,
698 		    "Could not create kernel statistics\n");
699 		return (DDI_FAILURE);
700 	}
701 
702 	Adapter->e1000g_ksp = ksp;	/* Fill in the Adapters ksp */
703 
704 	e1000g_ksp = (p_e1000g_stat_t)ksp->ks_data;
705 
706 	/*
707 	 * Initialize all the statistics
708 	 */
709 	kstat_named_init(&e1000g_ksp->link_speed, "link_speed",
710 	    KSTAT_DATA_ULONG);
711 	kstat_named_init(&e1000g_ksp->reset_count, "Reset Count",
712 	    KSTAT_DATA_ULONG);
713 
714 	kstat_named_init(&e1000g_ksp->rx_error, "Rx Error",
715 	    KSTAT_DATA_ULONG);
716 	kstat_named_init(&e1000g_ksp->rx_esballoc_fail, "Rx Desballoc Failure",
717 	    KSTAT_DATA_ULONG);
718 	kstat_named_init(&e1000g_ksp->rx_exceed_pkt, "Rx Exceed Max Pkt Count",
719 	    KSTAT_DATA_ULONG);
720 	kstat_named_init(&e1000g_ksp->rx_allocb_fail, "Rx Allocb Failure",
721 	    KSTAT_DATA_ULONG);
722 
723 	kstat_named_init(&e1000g_ksp->tx_no_desc, "Tx No Desc",
724 	    KSTAT_DATA_ULONG);
725 	kstat_named_init(&e1000g_ksp->tx_no_swpkt, "Tx No Buffer",
726 	    KSTAT_DATA_ULONG);
727 	kstat_named_init(&e1000g_ksp->tx_send_fail, "Tx Send Failure",
728 	    KSTAT_DATA_ULONG);
729 	kstat_named_init(&e1000g_ksp->tx_over_size, "Tx Pkt Over Size",
730 	    KSTAT_DATA_ULONG);
731 	kstat_named_init(&e1000g_ksp->tx_reschedule, "Tx Reschedule",
732 	    KSTAT_DATA_ULONG);
733 
734 	kstat_named_init(&e1000g_ksp->Mpc, "Recv_Missed_Packets",
735 	    KSTAT_DATA_ULONG);
736 	kstat_named_init(&e1000g_ksp->Symerrs, "Recv_Symbol_Errors",
737 	    KSTAT_DATA_ULONG);
738 	kstat_named_init(&e1000g_ksp->Rlec, "Recv_Length_Errors",
739 	    KSTAT_DATA_ULONG);
740 	kstat_named_init(&e1000g_ksp->Xonrxc, "XONs_Recvd",
741 	    KSTAT_DATA_ULONG);
742 	kstat_named_init(&e1000g_ksp->Xontxc, "XONs_Xmitd",
743 	    KSTAT_DATA_ULONG);
744 	kstat_named_init(&e1000g_ksp->Xoffrxc, "XOFFs_Recvd",
745 	    KSTAT_DATA_ULONG);
746 	kstat_named_init(&e1000g_ksp->Xofftxc, "XOFFs_Xmitd",
747 	    KSTAT_DATA_ULONG);
748 	kstat_named_init(&e1000g_ksp->Fcruc, "Recv_Unsupport_FC_Pkts",
749 	    KSTAT_DATA_ULONG);
750 	kstat_named_init(&e1000g_ksp->Prc64, "Pkts_Recvd_(  64b)",
751 	    KSTAT_DATA_ULONG);
752 	kstat_named_init(&e1000g_ksp->Prc127, "Pkts_Recvd_(  65- 127b)",
753 	    KSTAT_DATA_ULONG);
754 	kstat_named_init(&e1000g_ksp->Prc255, "Pkts_Recvd_( 127- 255b)",
755 	    KSTAT_DATA_ULONG);
756 	kstat_named_init(&e1000g_ksp->Prc511, "Pkts_Recvd_( 256- 511b)",
757 	    KSTAT_DATA_ULONG);
758 	kstat_named_init(&e1000g_ksp->Prc1023, "Pkts_Recvd_( 511-1023b)",
759 	    KSTAT_DATA_ULONG);
760 	kstat_named_init(&e1000g_ksp->Prc1522, "Pkts_Recvd_(1024-1522b)",
761 	    KSTAT_DATA_ULONG);
762 	kstat_named_init(&e1000g_ksp->Gprc, "Good_Pkts_Recvd",
763 	    KSTAT_DATA_ULONG);
764 	kstat_named_init(&e1000g_ksp->Gptc, "Good_Pkts_Xmitd",
765 	    KSTAT_DATA_ULONG);
766 	kstat_named_init(&e1000g_ksp->Gorl, "Good_Octets_Recvd_Lo",
767 	    KSTAT_DATA_ULONG);
768 	kstat_named_init(&e1000g_ksp->Gorh, "Good_Octets_Recvd_Hi",
769 	    KSTAT_DATA_ULONG);
770 	kstat_named_init(&e1000g_ksp->Gotl, "Good_Octets_Xmitd_Lo",
771 	    KSTAT_DATA_ULONG);
772 	kstat_named_init(&e1000g_ksp->Goth, "Good_Octets_Xmitd_Hi",
773 	    KSTAT_DATA_ULONG);
774 	kstat_named_init(&e1000g_ksp->Ruc, "Recv_Undersize",
775 	    KSTAT_DATA_ULONG);
776 	kstat_named_init(&e1000g_ksp->Rfc, "Recv_Frag",
777 	    KSTAT_DATA_ULONG);
778 	kstat_named_init(&e1000g_ksp->Roc, "Recv_Oversize",
779 	    KSTAT_DATA_ULONG);
780 	kstat_named_init(&e1000g_ksp->Rjc, "Recv_Jabber",
781 	    KSTAT_DATA_ULONG);
782 	kstat_named_init(&e1000g_ksp->Torl, "Total_Octets_Recvd_Lo",
783 	    KSTAT_DATA_ULONG);
784 	kstat_named_init(&e1000g_ksp->Torh, "Total_Octets_Recvd_Hi",
785 	    KSTAT_DATA_ULONG);
786 	kstat_named_init(&e1000g_ksp->Totl, "Total_Octets_Xmitd_Lo",
787 	    KSTAT_DATA_ULONG);
788 	kstat_named_init(&e1000g_ksp->Toth, "Total_Octets_Xmitd_Hi",
789 	    KSTAT_DATA_ULONG);
790 	kstat_named_init(&e1000g_ksp->Tpr, "Total_Packets_Recvd",
791 	    KSTAT_DATA_ULONG);
792 	kstat_named_init(&e1000g_ksp->Tpt, "Total_Packets_Xmitd",
793 	    KSTAT_DATA_ULONG);
794 	kstat_named_init(&e1000g_ksp->Ptc64, "Pkts_Xmitd_(  64b)",
795 	    KSTAT_DATA_ULONG);
796 	kstat_named_init(&e1000g_ksp->Ptc127, "Pkts_Xmitd_(  65- 127b)",
797 	    KSTAT_DATA_ULONG);
798 	kstat_named_init(&e1000g_ksp->Ptc255, "Pkts_Xmitd_( 128- 255b)",
799 	    KSTAT_DATA_ULONG);
800 	kstat_named_init(&e1000g_ksp->Ptc511, "Pkts_Xmitd_( 255- 511b)",
801 	    KSTAT_DATA_ULONG);
802 	kstat_named_init(&e1000g_ksp->Ptc1023, "Pkts_Xmitd_( 512-1023b)",
803 	    KSTAT_DATA_ULONG);
804 	kstat_named_init(&e1000g_ksp->Ptc1522, "Pkts_Xmitd_(1024-1522b)",
805 	    KSTAT_DATA_ULONG);
806 	kstat_named_init(&e1000g_ksp->Tncrs, "Xmit_with_No_CRS",
807 	    KSTAT_DATA_ULONG);
808 	kstat_named_init(&e1000g_ksp->Tsctc, "Xmit_TCP_Seg_Contexts",
809 	    KSTAT_DATA_ULONG);
810 	kstat_named_init(&e1000g_ksp->Tsctfc, "Xmit_TCP_Seg_Contexts_Fail",
811 	    KSTAT_DATA_ULONG);
812 
813 #ifdef E1000G_DEBUG
814 	kstat_named_init(&e1000g_ksp->rx_none, "Rx No Data",
815 	    KSTAT_DATA_ULONG);
816 	kstat_named_init(&e1000g_ksp->rx_multi_desc, "Rx Span Multi Desc",
817 	    KSTAT_DATA_ULONG);
818 	kstat_named_init(&e1000g_ksp->rx_no_freepkt, "Rx Freelist Empty",
819 	    KSTAT_DATA_ULONG);
820 	kstat_named_init(&e1000g_ksp->rx_avail_freepkt, "Rx Freelist Avail",
821 	    KSTAT_DATA_ULONG);
822 
823 	kstat_named_init(&e1000g_ksp->tx_under_size, "Tx Pkt Under Size",
824 	    KSTAT_DATA_ULONG);
825 	kstat_named_init(&e1000g_ksp->tx_exceed_frags, "Tx Exceed Max Frags",
826 	    KSTAT_DATA_ULONG);
827 	kstat_named_init(&e1000g_ksp->tx_empty_frags, "Tx Empty Frags",
828 	    KSTAT_DATA_ULONG);
829 	kstat_named_init(&e1000g_ksp->tx_recycle, "Tx Recycle",
830 	    KSTAT_DATA_ULONG);
831 	kstat_named_init(&e1000g_ksp->tx_recycle_intr, "Tx Recycle Intr",
832 	    KSTAT_DATA_ULONG);
833 	kstat_named_init(&e1000g_ksp->tx_recycle_retry, "Tx Recycle Retry",
834 	    KSTAT_DATA_ULONG);
835 	kstat_named_init(&e1000g_ksp->tx_recycle_none, "Tx Recycled None",
836 	    KSTAT_DATA_ULONG);
837 	kstat_named_init(&e1000g_ksp->tx_copy, "Tx Send Copy",
838 	    KSTAT_DATA_ULONG);
839 	kstat_named_init(&e1000g_ksp->tx_bind, "Tx Send Bind",
840 	    KSTAT_DATA_ULONG);
841 	kstat_named_init(&e1000g_ksp->tx_multi_copy, "Tx Copy Multi Frags",
842 	    KSTAT_DATA_ULONG);
843 	kstat_named_init(&e1000g_ksp->tx_multi_cookie, "Tx Bind Multi Cookies",
844 	    KSTAT_DATA_ULONG);
845 	kstat_named_init(&e1000g_ksp->tx_lack_desc, "Tx Desc Insufficient",
846 	    KSTAT_DATA_ULONG);
847 #endif
848 
849 	/*
850 	 * Function to provide kernel stat update on demand
851 	 */
852 	ksp->ks_update = e1000g_update_stats;
853 
854 	/*
855 	 * Pointer into provider's raw statistics
856 	 */
857 	ksp->ks_private = (void *)Adapter;
858 
859 	/*
860 	 * Add kstat to systems kstat chain
861 	 */
862 	kstat_install(ksp);
863 
864 	return (DDI_SUCCESS);
865 }
866