xref: /illumos-gate/usr/src/uts/common/io/cxgbe/t4nex/t4_nexus.c (revision d35eb49b13b481dcf2e0b66df3d6b2fad04f3572)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source. A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * This file is part of the Chelsio T4 support code.
14  *
15  * Copyright (C) 2010-2013 Chelsio Communications.  All rights reserved.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the LICENSE file included in this
20  * release for licensing terms and conditions.
21  */
22 
23 /*
24  * Copyright 2025 Oxide Computer Company
25  */
26 
27 #include <sys/ddi.h>
28 #include <sys/sunddi.h>
29 #include <sys/sunndi.h>
30 #include <sys/modctl.h>
31 #include <sys/conf.h>
32 #include <sys/devops.h>
33 #include <sys/pci.h>
34 #include <sys/atomic.h>
35 #include <sys/types.h>
36 #include <sys/file.h>
37 #include <sys/errno.h>
38 #include <sys/open.h>
39 #include <sys/cred.h>
40 #include <sys/stat.h>
41 #include <sys/mkdev.h>
42 #include <sys/queue.h>
43 #include <sys/containerof.h>
44 #include <sys/sensors.h>
45 #include <sys/firmload.h>
46 #include <sys/mac_provider.h>
47 #include <sys/mac_ether.h>
48 #include <sys/vlan.h>
49 
50 #include "common/common.h"
51 #include "common/t4_msg.h"
52 #include "common/t4_regs.h"
53 #include "common/t4_extra_regs.h"
54 
55 static void *t4_soft_state;
56 
57 static kmutex_t t4_adapter_list_lock;
58 static list_t t4_adapter_list;
59 
60 struct intrs_and_queues {
61 	int intr_type;		/* DDI_INTR_TYPE_* */
62 	int nirq;		/* Number of vectors */
63 	int intr_fwd;		/* Interrupts forwarded */
64 	int ntxq10g;		/* # of NIC txq's for each 10G port */
65 	int nrxq10g;		/* # of NIC rxq's for each 10G port */
66 	int ntxq1g;		/* # of NIC txq's for each 1G port */
67 	int nrxq1g;		/* # of NIC rxq's for each 1G port */
68 };
69 
70 static unsigned int getpf(struct adapter *sc);
71 static int prep_firmware(struct adapter *sc);
72 static int upload_config_file(struct adapter *sc, uint32_t *mt, uint32_t *ma);
73 static int partition_resources(struct adapter *sc);
74 static int adap__pre_init_tweaks(struct adapter *sc);
75 static int get_params__pre_init(struct adapter *sc);
76 static int get_params__post_init(struct adapter *sc);
77 static int set_params__post_init(struct adapter *);
78 static void t4_setup_adapter_memwin(struct adapter *sc);
79 static int validate_mt_off_len(struct adapter *, int, uint32_t, int,
80     uint32_t *);
81 static uint32_t t4_position_memwin(struct adapter *, int, uint32_t);
82 static int init_driver_props(struct adapter *sc, struct driver_properties *p);
83 static int remove_extra_props(struct adapter *sc, int n10g, int n1g);
84 static int cfg_itype_and_nqueues(struct adapter *sc, int n10g, int n1g,
85     struct intrs_and_queues *iaq);
86 static int add_child_node(struct adapter *sc, int idx);
87 static int remove_child_node(struct adapter *sc, int idx);
88 static kstat_t *setup_kstats(struct adapter *sc);
89 static kstat_t *setup_wc_kstats(struct adapter *);
90 static int update_wc_kstats(kstat_t *, int);
91 static int t4_port_full_uninit(struct port_info *);
92 
93 static int t4_temperature_read(void *, sensor_ioctl_scalar_t *);
94 static int t4_voltage_read(void *, sensor_ioctl_scalar_t *);
95 static const ksensor_ops_t t4_temp_ops = {
96 	.kso_kind = ksensor_kind_temperature,
97 	.kso_scalar = t4_temperature_read
98 };
99 
100 static const ksensor_ops_t t4_volt_ops = {
101 	.kso_kind = ksensor_kind_voltage,
102 	.kso_scalar = t4_voltage_read
103 };
104 
105 static int t4_ufm_getcaps(ddi_ufm_handle_t *, void *, ddi_ufm_cap_t *);
106 static int t4_ufm_fill_image(ddi_ufm_handle_t *, void *, uint_t,
107     ddi_ufm_image_t *);
108 static int t4_ufm_fill_slot(ddi_ufm_handle_t *, void *, uint_t, uint_t,
109     ddi_ufm_slot_t *);
110 static ddi_ufm_ops_t t4_ufm_ops = {
111 	.ddi_ufm_op_fill_image = t4_ufm_fill_image,
112 	.ddi_ufm_op_fill_slot = t4_ufm_fill_slot,
113 	.ddi_ufm_op_getcaps = t4_ufm_getcaps
114 };
115 
116 /* ARGSUSED */
117 static int
t4_devo_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** rp)118 t4_devo_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **rp)
119 {
120 	struct adapter *sc;
121 	minor_t minor;
122 
123 	minor = getminor((dev_t)arg);	/* same as instance# in our case */
124 
125 	if (cmd == DDI_INFO_DEVT2DEVINFO) {
126 		sc = ddi_get_soft_state(t4_soft_state, minor);
127 		if (sc == NULL)
128 			return (DDI_FAILURE);
129 
130 		ASSERT(sc->dev == (dev_t)arg);
131 		*rp = (void *)sc->dip;
132 	} else if (cmd == DDI_INFO_DEVT2INSTANCE)
133 		*rp = (void *) (unsigned long) minor;
134 	else
135 		ASSERT(0);
136 
137 	return (DDI_SUCCESS);
138 }
139 
140 static int
t4_devo_probe(dev_info_t * dip)141 t4_devo_probe(dev_info_t *dip)
142 {
143 	int rc, id, *reg;
144 	uint_t n, pf;
145 
146 	id = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
147 	    "device-id", 0xffff);
148 	if (id == 0xffff)
149 		return (DDI_PROBE_DONTCARE);
150 
151 	rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
152 	    "reg", &reg, &n);
153 	if (rc != DDI_SUCCESS)
154 		return (DDI_PROBE_DONTCARE);
155 
156 	pf = PCI_REG_FUNC_G(reg[0]);
157 	ddi_prop_free(reg);
158 
159 	/* Prevent driver attachment on any PF except 0 on the FPGA */
160 	if (id == 0xa000 && pf != 0)
161 		return (DDI_PROBE_FAILURE);
162 
163 	return (DDI_PROBE_DONTCARE);
164 }
165 
166 static int t4_devo_detach(dev_info_t *, ddi_detach_cmd_t);
167 
168 static int
t4_devo_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)169 t4_devo_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
170 {
171 	struct adapter *sc = NULL;
172 	struct sge *s;
173 	int i, instance, rc = DDI_SUCCESS, rqidx, tqidx, q;
174 	int irq = 0, nxg = 0, n1g = 0;
175 	char name[16];
176 	struct driver_properties *prp;
177 	struct intrs_and_queues iaq;
178 	ddi_device_acc_attr_t da = {
179 		.devacc_attr_version = DDI_DEVICE_ATTR_V0,
180 		.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC,
181 		.devacc_attr_dataorder = DDI_STRICTORDER_ACC
182 	};
183 	ddi_device_acc_attr_t da_bar2 = {
184 		.devacc_attr_version = DDI_DEVICE_ATTR_V0,
185 		.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC,
186 		.devacc_attr_dataorder = DDI_STRICTORDER_ACC
187 	};
188 
189 	if (cmd != DDI_ATTACH)
190 		return (DDI_FAILURE);
191 
192 	/*
193 	 * Allocate space for soft state.
194 	 */
195 	instance = ddi_get_instance(dip);
196 	rc = ddi_soft_state_zalloc(t4_soft_state, instance);
197 	if (rc != DDI_SUCCESS) {
198 		cxgb_printf(dip, CE_WARN,
199 		    "failed to allocate soft state: %d", rc);
200 		return (DDI_FAILURE);
201 	}
202 
203 	sc = ddi_get_soft_state(t4_soft_state, instance);
204 	sc->dip = dip;
205 	sc->dev = makedevice(ddi_driver_major(dip), instance);
206 	mutex_init(&sc->lock, NULL, MUTEX_DRIVER, NULL);
207 	cv_init(&sc->cv, NULL, CV_DRIVER, NULL);
208 	mutex_init(&sc->sfl_lock, NULL, MUTEX_DRIVER, NULL);
209 	TAILQ_INIT(&sc->sfl);
210 	mutex_init(&sc->mbox_lock, NULL, MUTEX_DRIVER, NULL);
211 	STAILQ_INIT(&sc->mbox_list);
212 
213 	mutex_enter(&t4_adapter_list_lock);
214 	list_insert_tail(&t4_adapter_list, sc);
215 	mutex_exit(&t4_adapter_list_lock);
216 
217 	sc->pf = getpf(sc);
218 	if (sc->pf > 8) {
219 		rc = EINVAL;
220 		cxgb_printf(dip, CE_WARN,
221 		    "failed to determine PCI PF# of device");
222 		goto done;
223 	}
224 	sc->mbox = sc->pf;
225 
226 	/* Initialize the driver properties */
227 	prp = &sc->props;
228 	(void) init_driver_props(sc, prp);
229 
230 	/*
231 	 * Enable access to the PCI config space.
232 	 */
233 	rc = pci_config_setup(dip, &sc->pci_regh);
234 	if (rc != DDI_SUCCESS) {
235 		cxgb_printf(dip, CE_WARN,
236 		    "failed to enable PCI config space access: %d", rc);
237 		goto done;
238 	}
239 
240 	/* TODO: Set max read request to 4K */
241 
242 	/*
243 	 * Enable BAR0 access.
244 	 */
245 	rc = ddi_regs_map_setup(dip, 1, &sc->regp, 0, 0, &da, &sc->regh);
246 	if (rc != DDI_SUCCESS) {
247 		cxgb_printf(dip, CE_WARN,
248 		    "failed to map device registers: %d", rc);
249 		goto done;
250 	}
251 
252 	(void) memset(sc->chan_map, 0xff, sizeof (sc->chan_map));
253 
254 	/*
255 	 * Prepare the adapter for operation.
256 	 */
257 	rc = -t4_prep_adapter(sc, false);
258 	if (rc != 0) {
259 		cxgb_printf(dip, CE_WARN, "failed to prepare adapter: %d", rc);
260 		goto done;
261 	}
262 
263 	/*
264 	 * Enable BAR2 access.
265 	 */
266 	sc->doorbells |= DOORBELL_KDB;
267 	rc = ddi_regs_map_setup(dip, 2, &sc->bar2_ptr, 0, 0, &da_bar2,
268 	    &sc->bar2_hdl);
269 	if (rc != DDI_SUCCESS) {
270 		cxgb_printf(dip, CE_WARN,
271 		    "failed to map BAR2 device registers: %d", rc);
272 		goto done;
273 	} else {
274 		if (t4_cver_ge(sc, CHELSIO_T5)) {
275 			sc->doorbells |= DOORBELL_UDB;
276 			if (prp->wc) {
277 				/*
278 				 * Enable write combining on BAR2.  This is the
279 				 * userspace doorbell BAR and is split into 128B
280 				 * (UDBS_SEG_SIZE) doorbell regions, each
281 				 * associated with an egress queue.  The first
282 				 * 64B has the doorbell and the second 64B can
283 				 * be used to submit a tx work request with an
284 				 * implicit doorbell.
285 				 */
286 				sc->doorbells &= ~DOORBELL_UDB;
287 				sc->doorbells |= (DOORBELL_WCWR |
288 				    DOORBELL_UDBWC);
289 
290 				const uint32_t stat_mode =
291 				    t4_cver_ge(sc, CHELSIO_T6) ?
292 				    V_T6_STATMODE(0) : V_STATMODE(0);
293 				t4_write_reg(sc, A_SGE_STAT_CFG,
294 				    V_STATSOURCE_T5(7) | stat_mode);
295 			}
296 		}
297 	}
298 
299 	/*
300 	 * Do this really early.  Note that minor number = instance.
301 	 */
302 	(void) snprintf(name, sizeof (name), "%s,%d", T4_NEXUS_NAME, instance);
303 	rc = ddi_create_minor_node(dip, name, S_IFCHR, instance,
304 	    DDI_NT_NEXUS, 0);
305 	if (rc != DDI_SUCCESS) {
306 		cxgb_printf(dip, CE_WARN,
307 		    "failed to create device node: %d", rc);
308 		rc = DDI_SUCCESS; /* carry on */
309 	}
310 
311 	/* Do this early. Memory window is required for loading config file. */
312 	t4_setup_adapter_memwin(sc);
313 
314 	/* Prepare the firmware for operation */
315 	rc = prep_firmware(sc);
316 	if (rc != 0)
317 		goto done; /* error message displayed already */
318 
319 	rc = adap__pre_init_tweaks(sc);
320 	if (rc != 0)
321 		goto done;
322 
323 	rc = get_params__pre_init(sc);
324 	if (rc != 0)
325 		goto done; /* error message displayed already */
326 
327 	t4_sge_init(sc);
328 
329 	if (sc->flags & TAF_MASTER_PF) {
330 		/* get basic stuff going */
331 		rc = -t4_fw_initialize(sc, sc->mbox);
332 		if (rc != 0) {
333 			cxgb_printf(sc->dip, CE_WARN,
334 			    "early init failed: %d.\n", rc);
335 			goto done;
336 		}
337 	}
338 
339 	rc = get_params__post_init(sc);
340 	if (rc != 0)
341 		goto done; /* error message displayed already */
342 
343 	rc = set_params__post_init(sc);
344 	if (rc != 0)
345 		goto done; /* error message displayed already */
346 
347 	/*
348 	 * TODO: This is the place to call t4_set_filter_mode()
349 	 */
350 
351 	/* tweak some settings */
352 	t4_write_reg(sc, A_TP_SHIFT_CNT,
353 	    V_SYNSHIFTMAX(6) |
354 	    V_RXTSHIFTMAXR1(4) |
355 	    V_RXTSHIFTMAXR2(15) |
356 	    V_PERSHIFTBACKOFFMAX(8) |
357 	    V_PERSHIFTMAX(8) |
358 	    V_KEEPALIVEMAXR1(4) |
359 	    V_KEEPALIVEMAXR2(9));
360 	t4_write_reg(sc, A_ULP_RX_TDDP_PSZ, V_HPZ0(PAGE_SHIFT - 12));
361 
362 	/*
363 	 * Work-around for bug 2619
364 	 * Set DisableVlan field in TP_RSS_CONFIG_VRT register so that the
365 	 * VLAN tag extraction is disabled.
366 	 */
367 	t4_set_reg_field(sc, A_TP_RSS_CONFIG_VRT, F_DISABLEVLAN, F_DISABLEVLAN);
368 
369 	/* Store filter mode */
370 	t4_read_indirect(sc, A_TP_PIO_ADDR, A_TP_PIO_DATA, &sc->filter_mode, 1,
371 	    A_TP_VLAN_PRI_MAP);
372 
373 	/*
374 	 * First pass over all the ports - allocate VIs and initialize some
375 	 * basic parameters like mac address, port type, etc.  We also figure
376 	 * out whether a port is 10G or 1G and use that information when
377 	 * calculating how many interrupts to attempt to allocate.
378 	 */
379 	for_each_port(sc, i) {
380 		struct port_info *pi;
381 
382 		pi = kmem_zalloc(sizeof (*pi), KM_SLEEP);
383 		sc->port[i] = pi;
384 
385 		/* These must be set before t4_port_init */
386 		pi->adapter = sc;
387 		pi->port_id = i;
388 	}
389 
390 	/* Allocate the vi and initialize parameters like mac addr */
391 	rc = -t4_port_init(sc, sc->mbox, sc->pf, 0);
392 	if (rc) {
393 		cxgb_printf(dip, CE_WARN, "unable to initialize port: %d", rc);
394 		goto done;
395 	}
396 
397 	for_each_port(sc, i) {
398 		struct port_info *pi = sc->port[i];
399 
400 		mutex_init(&pi->lock, NULL, MUTEX_DRIVER, NULL);
401 		pi->mtu = ETHERMTU;
402 
403 		if (t4_port_is_10xg(pi)) {
404 			nxg++;
405 			pi->tmr_idx = prp->tmr_idx_10g;
406 			pi->pktc_idx = prp->pktc_idx_10g;
407 		} else {
408 			n1g++;
409 			pi->tmr_idx = prp->tmr_idx_1g;
410 			pi->pktc_idx = prp->pktc_idx_1g;
411 		}
412 		pi->dbq_timer_idx = prp->dbq_timer_idx;
413 
414 		pi->xact_addr_filt = -1;
415 	}
416 
417 	(void) remove_extra_props(sc, nxg, n1g);
418 
419 	rc = cfg_itype_and_nqueues(sc, nxg, n1g, &iaq);
420 	if (rc != 0)
421 		goto done; /* error message displayed already */
422 
423 	sc->intr_type = iaq.intr_type;
424 	sc->intr_count = iaq.nirq;
425 
426 	if (sc->props.multi_rings && (sc->intr_type != DDI_INTR_TYPE_MSIX)) {
427 		sc->props.multi_rings = 0;
428 		cxgb_printf(dip, CE_WARN,
429 		    "Multiple rings disabled as interrupt type is not MSI-X");
430 	}
431 
432 	if (sc->props.multi_rings && iaq.intr_fwd) {
433 		sc->props.multi_rings = 0;
434 		cxgb_printf(dip, CE_WARN,
435 		    "Multiple rings disabled as interrupts are forwarded");
436 	}
437 
438 	if (!sc->props.multi_rings) {
439 		iaq.ntxq10g = 1;
440 		iaq.ntxq1g = 1;
441 	}
442 	s = &sc->sge;
443 	s->nrxq = nxg * iaq.nrxq10g + n1g * iaq.nrxq1g;
444 	s->ntxq = nxg * iaq.ntxq10g + n1g * iaq.ntxq1g;
445 	s->neq = s->ntxq + s->nrxq;	/* the fl in an rxq is an eq */
446 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
447 	if (iaq.intr_fwd != 0)
448 		sc->flags |= TAF_INTR_FWD;
449 	s->rxq = kmem_zalloc(s->nrxq * sizeof (struct sge_rxq), KM_SLEEP);
450 	s->txq = kmem_zalloc(s->ntxq * sizeof (struct sge_txq), KM_SLEEP);
451 	s->iqmap =
452 	    kmem_zalloc(s->iqmap_sz * sizeof (struct sge_iq *), KM_SLEEP);
453 	s->eqmap =
454 	    kmem_zalloc(s->eqmap_sz * sizeof (struct sge_eq *), KM_SLEEP);
455 
456 	sc->intr_handle =
457 	    kmem_zalloc(sc->intr_count * sizeof (ddi_intr_handle_t), KM_SLEEP);
458 
459 	/*
460 	 * Second pass over the ports.  This time we know the number of rx and
461 	 * tx queues that each port should get.
462 	 */
463 	rqidx = tqidx = 0;
464 	for_each_port(sc, i) {
465 		struct port_info *pi = sc->port[i];
466 
467 		if (pi == NULL)
468 			continue;
469 
470 		t4_mc_cb_init(pi);
471 		pi->first_rxq = rqidx;
472 		pi->nrxq = (t4_port_is_10xg(pi)) ? iaq.nrxq10g : iaq.nrxq1g;
473 		pi->first_txq = tqidx;
474 		pi->ntxq = (t4_port_is_10xg(pi)) ? iaq.ntxq10g : iaq.ntxq1g;
475 
476 		rqidx += pi->nrxq;
477 		tqidx += pi->ntxq;
478 
479 		/*
480 		 * Enable hw checksumming and LSO for all ports by default.
481 		 * They can be disabled using ndd (hw_csum and hw_lso).
482 		 */
483 		pi->features |= (CXGBE_HW_CSUM | CXGBE_HW_LSO);
484 	}
485 
486 	/*
487 	 * Setup Interrupts.
488 	 */
489 
490 	i = 0;
491 	rc = ddi_intr_alloc(dip, sc->intr_handle, sc->intr_type, 0,
492 	    sc->intr_count, &i, DDI_INTR_ALLOC_STRICT);
493 	if (rc != DDI_SUCCESS) {
494 		cxgb_printf(dip, CE_WARN,
495 		    "failed to allocate %d interrupt(s) of type %d: %d, %d",
496 		    sc->intr_count, sc->intr_type, rc, i);
497 		goto done;
498 	}
499 	ASSERT(sc->intr_count == i); /* allocation was STRICT */
500 	(void) ddi_intr_get_cap(sc->intr_handle[0], &sc->intr_cap);
501 	(void) ddi_intr_get_pri(sc->intr_handle[0], &sc->intr_pri);
502 	if (sc->intr_count == 1) {
503 		ASSERT(sc->flags & TAF_INTR_FWD);
504 		(void) ddi_intr_add_handler(sc->intr_handle[0], t4_intr_all, sc,
505 		    &s->fwq);
506 	} else {
507 		/* Multiple interrupts.  The first one is always error intr */
508 		(void) ddi_intr_add_handler(sc->intr_handle[0], t4_intr_err, sc,
509 		    NULL);
510 		irq++;
511 
512 		/* The second one is always the firmware event queue */
513 		(void) ddi_intr_add_handler(sc->intr_handle[1], t4_intr, sc,
514 		    &s->fwq);
515 		irq++;
516 		/*
517 		 * Note that if TAF_INTR_FWD is set then either the NIC rx
518 		 * queues or (exclusive or) the TOE rx queueus will be taking
519 		 * direct interrupts.
520 		 *
521 		 * There is no need to check for is_offload(sc) as nofldrxq
522 		 * will be 0 if offload is disabled.
523 		 */
524 		for_each_port(sc, i) {
525 			struct port_info *pi = sc->port[i];
526 			struct sge_rxq *rxq;
527 			rxq = &s->rxq[pi->first_rxq];
528 			for (q = 0; q < pi->nrxq; q++, rxq++) {
529 				(void) ddi_intr_add_handler(
530 				    sc->intr_handle[irq], t4_intr, sc,
531 				    &rxq->iq);
532 				irq++;
533 			}
534 		}
535 
536 	}
537 	sc->flags |= TAF_INTR_ALLOC;
538 
539 	if ((rc = ksensor_create_scalar_pcidev(dip, SENSOR_KIND_TEMPERATURE,
540 	    &t4_temp_ops, sc, "temp", &sc->temp_sensor)) != 0) {
541 		cxgb_printf(dip, CE_WARN, "failed to create temperature "
542 		    "sensor: %d", rc);
543 		rc = DDI_FAILURE;
544 		goto done;
545 	}
546 
547 	if ((rc = ksensor_create_scalar_pcidev(dip, SENSOR_KIND_VOLTAGE,
548 	    &t4_volt_ops, sc, "vdd", &sc->volt_sensor)) != 0) {
549 		cxgb_printf(dip, CE_WARN, "failed to create voltage "
550 		    "sensor: %d", rc);
551 		rc = DDI_FAILURE;
552 		goto done;
553 	}
554 
555 
556 	if ((rc = ddi_ufm_init(dip, DDI_UFM_CURRENT_VERSION, &t4_ufm_ops,
557 	    &sc->ufm_hdl, sc)) != 0) {
558 		cxgb_printf(dip, CE_WARN, "failed to enable UFM ops: %d", rc);
559 		rc = DDI_FAILURE;
560 		goto done;
561 	}
562 	ddi_ufm_update(sc->ufm_hdl);
563 
564 	if ((rc = t4_alloc_fwq(sc)) != 0) {
565 		cxgb_printf(dip, CE_WARN, "failed to alloc FWQ: %d", rc);
566 		rc = DDI_FAILURE;
567 		goto done;
568 	}
569 
570 	if (sc->intr_cap & DDI_INTR_FLAG_BLOCK) {
571 		(void) ddi_intr_block_enable(sc->intr_handle, sc->intr_count);
572 	} else {
573 		for (i = 0; i < sc->intr_count; i++)
574 			(void) ddi_intr_enable(sc->intr_handle[i]);
575 	}
576 	t4_intr_enable(sc);
577 
578 	/*
579 	 * At this point, adapter-level initialization can be considered
580 	 * successful.  The ports themselves will be initialized later when mac
581 	 * attaches/starts them via cxgbe.
582 	 */
583 	sc->flags |= TAF_INIT_DONE;
584 	ddi_report_dev(dip);
585 
586 	/*
587 	 * Hardware/Firmware/etc. Version/Revision IDs.
588 	 */
589 	t4_dump_version_info(sc);
590 
591 	cxgb_printf(dip, CE_NOTE, "(%d rxq, %d txq total) %d %s.",
592 	    rqidx, tqidx, sc->intr_count,
593 	    sc->intr_type == DDI_INTR_TYPE_MSIX ? "MSI-X interrupts" :
594 	    sc->intr_type == DDI_INTR_TYPE_MSI ? "MSI interrupts" :
595 	    "fixed interrupt");
596 
597 	sc->ksp = setup_kstats(sc);
598 	sc->ksp_stat = setup_wc_kstats(sc);
599 	sc->params.drv_memwin = MEMWIN_NIC;
600 
601 done:
602 	if (rc != DDI_SUCCESS) {
603 		(void) t4_devo_detach(dip, DDI_DETACH);
604 
605 		/* rc may have errno style errors or DDI errors */
606 		rc = DDI_FAILURE;
607 	}
608 
609 	return (rc);
610 }
611 
612 static int
t4_devo_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)613 t4_devo_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
614 {
615 	int instance, i;
616 	struct adapter *sc;
617 	struct port_info *pi;
618 	struct sge *s;
619 
620 	if (cmd != DDI_DETACH)
621 		return (DDI_FAILURE);
622 
623 	instance = ddi_get_instance(dip);
624 	sc = ddi_get_soft_state(t4_soft_state, instance);
625 	if (sc == NULL)
626 		return (DDI_SUCCESS);
627 
628 	if (sc->flags & TAF_INIT_DONE) {
629 		t4_intr_disable(sc);
630 		for_each_port(sc, i) {
631 			pi = sc->port[i];
632 			if (pi && pi->flags & TPF_INIT_DONE)
633 				(void) t4_port_full_uninit(pi);
634 		}
635 
636 		if (sc->intr_cap & DDI_INTR_FLAG_BLOCK) {
637 			(void) ddi_intr_block_disable(sc->intr_handle,
638 			    sc->intr_count);
639 		} else {
640 			for (i = 0; i < sc->intr_count; i++)
641 				(void) ddi_intr_disable(sc->intr_handle[i]);
642 		}
643 
644 		(void) t4_free_fwq(sc);
645 
646 		sc->flags &= ~TAF_INIT_DONE;
647 	}
648 
649 	/* Safe to call no matter what */
650 	if (sc->ufm_hdl != NULL) {
651 		ddi_ufm_fini(sc->ufm_hdl);
652 		sc->ufm_hdl = NULL;
653 	}
654 	(void) ksensor_remove(dip, KSENSOR_ALL_IDS);
655 	ddi_prop_remove_all(dip);
656 	ddi_remove_minor_node(dip, NULL);
657 
658 	if (sc->ksp != NULL)
659 		kstat_delete(sc->ksp);
660 	if (sc->ksp_stat != NULL)
661 		kstat_delete(sc->ksp_stat);
662 
663 	s = &sc->sge;
664 	if (s->rxq != NULL)
665 		kmem_free(s->rxq, s->nrxq * sizeof (struct sge_rxq));
666 	if (s->txq != NULL)
667 		kmem_free(s->txq, s->ntxq * sizeof (struct sge_txq));
668 	if (s->iqmap != NULL)
669 		kmem_free(s->iqmap, s->iqmap_sz * sizeof (struct sge_iq *));
670 	if (s->eqmap != NULL)
671 		kmem_free(s->eqmap, s->eqmap_sz * sizeof (struct sge_eq *));
672 
673 	if (s->rxbuf_cache != NULL)
674 		kmem_cache_destroy(s->rxbuf_cache);
675 
676 	if (sc->flags & TAF_INTR_ALLOC) {
677 		for (i = 0; i < sc->intr_count; i++) {
678 			(void) ddi_intr_remove_handler(sc->intr_handle[i]);
679 			(void) ddi_intr_free(sc->intr_handle[i]);
680 		}
681 		sc->flags &= ~TAF_INTR_ALLOC;
682 	}
683 
684 	if (sc->intr_handle != NULL) {
685 		kmem_free(sc->intr_handle,
686 		    sc->intr_count * sizeof (*sc->intr_handle));
687 	}
688 
689 	for_each_port(sc, i) {
690 		pi = sc->port[i];
691 		if (pi != NULL) {
692 			mutex_destroy(&pi->lock);
693 			kmem_free(pi, sizeof (*pi));
694 		}
695 	}
696 
697 	if (sc->flags & FW_OK)
698 		(void) t4_fw_bye(sc, sc->mbox);
699 
700 	if (sc->bar2_hdl != NULL) {
701 		ddi_regs_map_free(&sc->bar2_hdl);
702 		sc->bar2_hdl = NULL;
703 		sc->bar2_ptr = NULL;
704 	}
705 
706 	if (sc->regh != NULL) {
707 		ddi_regs_map_free(&sc->regh);
708 		sc->regh = NULL;
709 		sc->regp = NULL;
710 	}
711 
712 	if (sc->pci_regh != NULL) {
713 		pci_config_teardown(&sc->pci_regh);
714 	}
715 
716 	mutex_enter(&t4_adapter_list_lock);
717 	list_remove(&t4_adapter_list, sc);
718 	mutex_exit(&t4_adapter_list_lock);
719 
720 	mutex_destroy(&sc->mbox_lock);
721 	mutex_destroy(&sc->lock);
722 	cv_destroy(&sc->cv);
723 	mutex_destroy(&sc->sfl_lock);
724 
725 #ifdef DEBUG
726 	bzero(sc, sizeof (*sc));
727 #endif
728 	ddi_soft_state_free(t4_soft_state, instance);
729 
730 	return (DDI_SUCCESS);
731 }
732 
733 static int
t4_devo_quiesce(dev_info_t * dip)734 t4_devo_quiesce(dev_info_t *dip)
735 {
736 	int instance;
737 	struct adapter *sc;
738 
739 	instance = ddi_get_instance(dip);
740 	sc = ddi_get_soft_state(t4_soft_state, instance);
741 	if (sc == NULL)
742 		return (DDI_SUCCESS);
743 
744 	t4_set_reg_field(sc, A_SGE_CONTROL, F_GLOBALENABLE, 0);
745 	t4_intr_disable(sc);
746 	t4_write_reg(sc, A_PL_RST, F_PIORSTMODE | F_PIORST);
747 
748 	return (DDI_SUCCESS);
749 }
750 
751 static int
t4_bus_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t op,void * arg,void * result)752 t4_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t op, void *arg,
753     void *result)
754 {
755 	char s[4];
756 	struct port_info *pi;
757 	dev_info_t *child = (dev_info_t *)arg;
758 
759 	switch (op) {
760 	case DDI_CTLOPS_REPORTDEV:
761 		pi = ddi_get_parent_data(rdip);
762 		pi->instance = ddi_get_instance(dip);
763 		pi->child_inst = ddi_get_instance(rdip);
764 		return (DDI_SUCCESS);
765 
766 	case DDI_CTLOPS_INITCHILD:
767 		pi = ddi_get_parent_data(child);
768 		if (pi == NULL)
769 			return (DDI_NOT_WELL_FORMED);
770 		(void) snprintf(s, sizeof (s), "%d", pi->port_id);
771 		ddi_set_name_addr(child, s);
772 		return (DDI_SUCCESS);
773 
774 	case DDI_CTLOPS_UNINITCHILD:
775 		ddi_set_name_addr(child, NULL);
776 		return (DDI_SUCCESS);
777 
778 	case DDI_CTLOPS_ATTACH:
779 	case DDI_CTLOPS_DETACH:
780 		return (DDI_SUCCESS);
781 
782 	default:
783 		return (ddi_ctlops(dip, rdip, op, arg, result));
784 	}
785 }
786 
787 static int
t4_bus_config(dev_info_t * dip,uint_t flags,ddi_bus_config_op_t op,void * arg,dev_info_t ** cdipp)788 t4_bus_config(dev_info_t *dip, uint_t flags, ddi_bus_config_op_t op, void *arg,
789     dev_info_t **cdipp)
790 {
791 	int instance, i;
792 	struct adapter *sc;
793 
794 	instance = ddi_get_instance(dip);
795 	sc = ddi_get_soft_state(t4_soft_state, instance);
796 
797 	if (op == BUS_CONFIG_ONE) {
798 		char *c;
799 
800 		/*
801 		 * arg is something like "cxgb@0" where 0 is the port_id hanging
802 		 * off this nexus.
803 		 */
804 
805 		c = arg;
806 		while (*(c + 1))
807 			c++;
808 
809 		/* There should be exactly 1 digit after '@' */
810 		if (*(c - 1) != '@')
811 			return (NDI_FAILURE);
812 
813 		i = *c - '0';
814 
815 		if (add_child_node(sc, i) != 0)
816 			return (NDI_FAILURE);
817 
818 		flags |= NDI_ONLINE_ATTACH;
819 
820 	} else if (op == BUS_CONFIG_ALL || op == BUS_CONFIG_DRIVER) {
821 		/* Allocate and bind all child device nodes */
822 		for_each_port(sc, i)
823 		    (void) add_child_node(sc, i);
824 		flags |= NDI_ONLINE_ATTACH;
825 	}
826 
827 	return (ndi_busop_bus_config(dip, flags, op, arg, cdipp, 0));
828 }
829 
830 static int
t4_bus_unconfig(dev_info_t * dip,uint_t flags,ddi_bus_config_op_t op,void * arg)831 t4_bus_unconfig(dev_info_t *dip, uint_t flags, ddi_bus_config_op_t op,
832     void *arg)
833 {
834 	int instance, i, rc;
835 	struct adapter *sc;
836 
837 	instance = ddi_get_instance(dip);
838 	sc = ddi_get_soft_state(t4_soft_state, instance);
839 
840 	if (op == BUS_CONFIG_ONE || op == BUS_UNCONFIG_ALL ||
841 	    op == BUS_UNCONFIG_DRIVER)
842 		flags |= NDI_UNCONFIG;
843 
844 	rc = ndi_busop_bus_unconfig(dip, flags, op, arg);
845 	if (rc != 0)
846 		return (rc);
847 
848 	if (op == BUS_UNCONFIG_ONE) {
849 		char *c;
850 
851 		c = arg;
852 		while (*(c + 1))
853 			c++;
854 
855 		if (*(c - 1) != '@')
856 			return (NDI_SUCCESS);
857 
858 		i = *c - '0';
859 
860 		rc = remove_child_node(sc, i);
861 
862 	} else if (op == BUS_UNCONFIG_ALL || op == BUS_UNCONFIG_DRIVER) {
863 
864 		for_each_port(sc, i)
865 		    (void) remove_child_node(sc, i);
866 	}
867 
868 	return (rc);
869 }
870 
871 /* ARGSUSED */
872 static int
t4_cb_open(dev_t * devp,int flag,int otyp,cred_t * credp)873 t4_cb_open(dev_t *devp, int flag, int otyp, cred_t *credp)
874 {
875 	struct adapter *sc;
876 
877 	if (otyp != OTYP_CHR)
878 		return (EINVAL);
879 
880 	sc = ddi_get_soft_state(t4_soft_state, getminor(*devp));
881 	if (sc == NULL)
882 		return (ENXIO);
883 
884 	return (atomic_cas_uint(&sc->open, 0, EBUSY));
885 }
886 
887 /* ARGSUSED */
888 static int
t4_cb_close(dev_t dev,int flag,int otyp,cred_t * credp)889 t4_cb_close(dev_t dev, int flag, int otyp, cred_t *credp)
890 {
891 	struct adapter *sc;
892 
893 	sc = ddi_get_soft_state(t4_soft_state, getminor(dev));
894 	if (sc == NULL)
895 		return (EINVAL);
896 
897 	(void) atomic_swap_uint(&sc->open, 0);
898 	return (0);
899 }
900 
901 /* ARGSUSED */
902 static int
t4_cb_ioctl(dev_t dev,int cmd,intptr_t d,int mode,cred_t * credp,int * rp)903 t4_cb_ioctl(dev_t dev, int cmd, intptr_t d, int mode, cred_t *credp, int *rp)
904 {
905 	int instance;
906 	struct adapter *sc;
907 	void *data = (void *)d;
908 
909 	if (crgetuid(credp) != 0)
910 		return (EPERM);
911 
912 	instance = getminor(dev);
913 	sc = ddi_get_soft_state(t4_soft_state, instance);
914 	if (sc == NULL)
915 		return (EINVAL);
916 
917 	return (t4_ioctl(sc, cmd, data, mode));
918 }
919 
920 static unsigned int
getpf(struct adapter * sc)921 getpf(struct adapter *sc)
922 {
923 	int rc, *data;
924 	uint_t n, pf;
925 
926 	rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, sc->dip,
927 	    DDI_PROP_DONTPASS, "reg", &data, &n);
928 	if (rc != DDI_SUCCESS) {
929 		cxgb_printf(sc->dip, CE_WARN,
930 		    "failed to lookup \"reg\" property: %d", rc);
931 		return (0xff);
932 	}
933 
934 	pf = PCI_REG_FUNC_G(data[0]);
935 	ddi_prop_free(data);
936 
937 	return (pf);
938 }
939 
940 /*
941  * Install a compatible firmware (if required), establish contact with it,
942  * become the master, and reset the device.
943  */
944 static int
prep_firmware(struct adapter * sc)945 prep_firmware(struct adapter *sc)
946 {
947 	int rc;
948 	size_t fw_size;
949 	int reset = 1;
950 	enum dev_state state;
951 	unsigned char *fw_data;
952 	struct fw_hdr *card_fw, *hdr;
953 	const char *fw_file = NULL;
954 	firmware_handle_t fw_hdl;
955 	struct fw_info fi, *fw_info = &fi;
956 
957 	struct driver_properties *p = &sc->props;
958 
959 	/* Contact firmware, request master */
960 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MUST, &state);
961 	if (rc < 0) {
962 		rc = -rc;
963 		cxgb_printf(sc->dip, CE_WARN,
964 		    "failed to connect to the firmware: %d.", rc);
965 		return (rc);
966 	}
967 
968 	if (rc == sc->mbox)
969 		sc->flags |= TAF_MASTER_PF;
970 
971 	/* We may need FW version info for later reporting */
972 	t4_get_version_info(sc);
973 
974 	switch (CHELSIO_CHIP_VERSION(sc->params.chip)) {
975 	case CHELSIO_T4:
976 		fw_file = "t4fw.bin";
977 		break;
978 	case CHELSIO_T5:
979 		fw_file = "t5fw.bin";
980 		break;
981 	case CHELSIO_T6:
982 		fw_file = "t6fw.bin";
983 		break;
984 	default:
985 		cxgb_printf(sc->dip, CE_WARN, "Adapter type not supported\n");
986 		return (EINVAL);
987 	}
988 
989 	if (firmware_open(T4_PORT_NAME, fw_file, &fw_hdl) != 0) {
990 		cxgb_printf(sc->dip, CE_WARN, "Could not open %s\n", fw_file);
991 		return (EINVAL);
992 	}
993 
994 	fw_size = firmware_get_size(fw_hdl);
995 
996 	if (fw_size < sizeof (struct fw_hdr)) {
997 		cxgb_printf(sc->dip, CE_WARN, "%s is too small (%ld bytes)\n",
998 		    fw_file, fw_size);
999 		firmware_close(fw_hdl);
1000 		return (EINVAL);
1001 	}
1002 
1003 	if (fw_size > FLASH_FW_MAX_SIZE) {
1004 		cxgb_printf(sc->dip, CE_WARN,
1005 		    "%s is too large (%ld bytes, max allowed is %ld)\n",
1006 		    fw_file, fw_size, FLASH_FW_MAX_SIZE);
1007 		firmware_close(fw_hdl);
1008 		return (EFBIG);
1009 	}
1010 
1011 	fw_data = kmem_zalloc(fw_size, KM_SLEEP);
1012 	if (firmware_read(fw_hdl, 0, fw_data, fw_size) != 0) {
1013 		cxgb_printf(sc->dip, CE_WARN, "Failed to read from %s\n",
1014 		    fw_file);
1015 		firmware_close(fw_hdl);
1016 		kmem_free(fw_data, fw_size);
1017 		return (EINVAL);
1018 	}
1019 	firmware_close(fw_hdl);
1020 
1021 	bzero(fw_info, sizeof (*fw_info));
1022 	fw_info->chip = CHELSIO_CHIP_VERSION(sc->params.chip);
1023 
1024 	hdr = (struct fw_hdr *)fw_data;
1025 	fw_info->fw_hdr.fw_ver = hdr->fw_ver;
1026 	fw_info->fw_hdr.chip = hdr->chip;
1027 	fw_info->fw_hdr.intfver_nic = hdr->intfver_nic;
1028 	fw_info->fw_hdr.intfver_vnic = hdr->intfver_vnic;
1029 	fw_info->fw_hdr.intfver_ofld = hdr->intfver_ofld;
1030 	fw_info->fw_hdr.intfver_ri = hdr->intfver_ri;
1031 	fw_info->fw_hdr.intfver_iscsipdu = hdr->intfver_iscsipdu;
1032 	fw_info->fw_hdr.intfver_iscsi = hdr->intfver_iscsi;
1033 	fw_info->fw_hdr.intfver_fcoepdu = hdr->intfver_fcoepdu;
1034 	fw_info->fw_hdr.intfver_fcoe = hdr->intfver_fcoe;
1035 
1036 	/* allocate memory to read the header of the firmware on the card */
1037 	card_fw = kmem_zalloc(sizeof (*card_fw), KM_SLEEP);
1038 
1039 	rc = -t4_prep_fw(sc, fw_info, fw_data, fw_size, card_fw,
1040 	    p->t4_fw_install, state, &reset);
1041 
1042 	kmem_free(card_fw, sizeof (*card_fw));
1043 	kmem_free(fw_data, fw_size);
1044 
1045 	if (rc != 0) {
1046 		cxgb_printf(sc->dip, CE_WARN,
1047 		    "failed to install firmware: %d", rc);
1048 		return (rc);
1049 	} else {
1050 		/* refresh */
1051 		(void) t4_check_fw_version(sc);
1052 	}
1053 
1054 	/* Reset device */
1055 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
1056 	if (rc != 0) {
1057 		cxgb_printf(sc->dip, CE_WARN,
1058 		    "firmware reset failed: %d.", rc);
1059 		if (rc != ETIMEDOUT && rc != EIO)
1060 			(void) t4_fw_bye(sc, sc->mbox);
1061 		return (rc);
1062 	}
1063 
1064 	/* Partition adapter resources as specified in the config file. */
1065 	if (sc->flags & TAF_MASTER_PF) {
1066 		/* Handle default vs special T4 config file */
1067 
1068 		rc = partition_resources(sc);
1069 		if (rc != 0)
1070 			goto err;	/* error message displayed already */
1071 	}
1072 
1073 	sc->flags |= FW_OK;
1074 	return (0);
1075 err:
1076 	return (rc);
1077 
1078 }
1079 
1080 static const struct memwin t4_memwin[] = {
1081 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
1082 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
1083 	{ MEMWIN2_BASE, MEMWIN2_APERTURE }
1084 };
1085 
1086 static const struct memwin t5_memwin[] = {
1087 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
1088 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
1089 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
1090 };
1091 
1092 #define	FW_PARAM_DEV(param) \
1093 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
1094 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
1095 #define	FW_PARAM_PFVF(param) \
1096 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
1097 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
1098 
1099 /*
1100  * Verify that the memory range specified by the memtype/offset/len pair is
1101  * valid and lies entirely within the memtype specified.  The global address of
1102  * the start of the range is returned in addr.
1103  */
1104 int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,int len,uint32_t * addr)1105 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, int len,
1106     uint32_t *addr)
1107 {
1108 	uint32_t em, addr_len, maddr, mlen;
1109 
1110 	/* Memory can only be accessed in naturally aligned 4 byte units */
1111 	if (off & 3 || len & 3 || len == 0)
1112 		return (EINVAL);
1113 
1114 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
1115 	switch (mtype) {
1116 		case MEM_EDC0:
1117 			if (!(em & F_EDRAM0_ENABLE))
1118 				return (EINVAL);
1119 			addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
1120 			maddr = G_EDRAM0_BASE(addr_len) << 20;
1121 			mlen = G_EDRAM0_SIZE(addr_len) << 20;
1122 			break;
1123 		case MEM_EDC1:
1124 			if (!(em & F_EDRAM1_ENABLE))
1125 				return (EINVAL);
1126 			addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
1127 			maddr = G_EDRAM1_BASE(addr_len) << 20;
1128 			mlen = G_EDRAM1_SIZE(addr_len) << 20;
1129 			break;
1130 		case MEM_MC:
1131 			if (!(em & F_EXT_MEM_ENABLE))
1132 				return (EINVAL);
1133 			addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
1134 			maddr = G_EXT_MEM_BASE(addr_len) << 20;
1135 			mlen = G_EXT_MEM_SIZE(addr_len) << 20;
1136 			break;
1137 		case MEM_MC1:
1138 			if (t4_cver_eq(sc, CHELSIO_T4) ||
1139 			    !(em & F_EXT_MEM1_ENABLE)) {
1140 				return (EINVAL);
1141 			}
1142 			addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
1143 			maddr = G_EXT_MEM1_BASE(addr_len) << 20;
1144 			mlen = G_EXT_MEM1_SIZE(addr_len) << 20;
1145 			break;
1146 		default:
1147 			return (EINVAL);
1148 	}
1149 
1150 	if (mlen > 0 && off < mlen && off + len <= mlen) {
1151 		*addr = maddr + off;    /* global address */
1152 		return (0);
1153 	}
1154 
1155 	return (EFAULT);
1156 }
1157 
1158 static void
memwin_info(struct adapter * sc,int win,uint32_t * base,uint32_t * aperture)1159 memwin_info(struct adapter *sc, int win, uint32_t *base, uint32_t *aperture)
1160 {
1161 	const struct memwin *mw;
1162 
1163 	if (t4_cver_eq(sc, CHELSIO_T4)) {
1164 		mw = &t4_memwin[win];
1165 	} else {
1166 		mw = &t5_memwin[win];
1167 	}
1168 
1169 	if (base != NULL)
1170 		*base = mw->base;
1171 	if (aperture != NULL)
1172 		*aperture = mw->aperture;
1173 }
1174 
1175 /*
1176  * Upload configuration file to card's memory.
1177  */
1178 static int
upload_config_file(struct adapter * sc,uint32_t * mt,uint32_t * ma)1179 upload_config_file(struct adapter *sc, uint32_t *mt, uint32_t *ma)
1180 {
1181 	int rc = 0;
1182 	size_t cflen, cfbaselen;
1183 	uint_t i, n;
1184 	uint32_t param, val, addr, mtype, maddr;
1185 	uint32_t off, mw_base, mw_aperture;
1186 	uint32_t *cfdata, *cfbase;
1187 	firmware_handle_t fw_hdl;
1188 	const char *cfg_file = NULL;
1189 
1190 	/* Figure out where the firmware wants us to upload it. */
1191 	param = FW_PARAM_DEV(CF);
1192 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
1193 	if (rc != 0) {
1194 		/* Firmwares without config file support will fail this way */
1195 		cxgb_printf(sc->dip, CE_WARN,
1196 		    "failed to query config file location: %d.\n", rc);
1197 		return (rc);
1198 	}
1199 	*mt = mtype = G_FW_PARAMS_PARAM_Y(val);
1200 	*ma = maddr = G_FW_PARAMS_PARAM_Z(val) << 16;
1201 
1202 	switch (CHELSIO_CHIP_VERSION(sc->params.chip)) {
1203 	case CHELSIO_T4:
1204 		cfg_file = "t4fw_cfg.txt";
1205 		break;
1206 	case CHELSIO_T5:
1207 		cfg_file = "t5fw_cfg.txt";
1208 		break;
1209 	case CHELSIO_T6:
1210 		cfg_file = "t6fw_cfg.txt";
1211 		break;
1212 	default:
1213 		cxgb_printf(sc->dip, CE_WARN, "Invalid Adapter detected\n");
1214 		return (EINVAL);
1215 	}
1216 
1217 	if (firmware_open(T4_PORT_NAME, cfg_file, &fw_hdl) != 0) {
1218 		cxgb_printf(sc->dip, CE_WARN, "Could not open %s\n", cfg_file);
1219 		return (EINVAL);
1220 	}
1221 
1222 	cflen = firmware_get_size(fw_hdl);
1223 	/*
1224 	 * Truncate the length to a multiple of uint32_ts. The configuration
1225 	 * text files have trailing comments (and hopefully always will) so
1226 	 * nothing important is lost.
1227 	 */
1228 	cflen &= ~3;
1229 
1230 	if (cflen > FLASH_CFG_MAX_SIZE) {
1231 		cxgb_printf(sc->dip, CE_WARN,
1232 		    "config file too long (%d, max allowed is %d).  ",
1233 		    cflen, FLASH_CFG_MAX_SIZE);
1234 		firmware_close(fw_hdl);
1235 		return (EFBIG);
1236 	}
1237 
1238 	rc = validate_mt_off_len(sc, mtype, maddr, cflen, &addr);
1239 	if (rc != 0) {
1240 		cxgb_printf(sc->dip, CE_WARN,
1241 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.  "
1242 		    "Will try to use the config on the card, if any.\n",
1243 		    __func__, mtype, maddr, cflen, rc);
1244 		firmware_close(fw_hdl);
1245 		return (EFAULT);
1246 	}
1247 
1248 	cfbaselen = cflen;
1249 	cfbase = cfdata = kmem_zalloc(cflen, KM_SLEEP);
1250 	if (firmware_read(fw_hdl, 0, cfdata, cflen) != 0) {
1251 		cxgb_printf(sc->dip, CE_WARN, "Failed to read from %s\n",
1252 		    cfg_file);
1253 		firmware_close(fw_hdl);
1254 		kmem_free(cfbase, cfbaselen);
1255 		return (EINVAL);
1256 	}
1257 	firmware_close(fw_hdl);
1258 
1259 	memwin_info(sc, 2, &mw_base, &mw_aperture);
1260 	while (cflen) {
1261 		off = t4_position_memwin(sc, 2, addr);
1262 		n = min(cflen, mw_aperture - off);
1263 		for (i = 0; i < n; i += 4)
1264 			t4_write_reg(sc, mw_base + off + i, *cfdata++);
1265 		cflen -= n;
1266 		addr += n;
1267 	}
1268 
1269 	kmem_free(cfbase, cfbaselen);
1270 
1271 	return (rc);
1272 }
1273 
1274 /*
1275  * Partition chip resources for use between various PFs, VFs, etc.  This is done
1276  * by uploading the firmware configuration file to the adapter and instructing
1277  * the firmware to process it.
1278  */
1279 static int
partition_resources(struct adapter * sc)1280 partition_resources(struct adapter *sc)
1281 {
1282 	int rc;
1283 	struct fw_caps_config_cmd caps;
1284 	uint32_t mtype, maddr, finicsum, cfcsum;
1285 
1286 	rc = upload_config_file(sc, &mtype, &maddr);
1287 	if (rc != 0) {
1288 		mtype = FW_MEMTYPE_CF_FLASH;
1289 		maddr = t4_flash_cfg_addr(sc);
1290 	}
1291 
1292 	bzero(&caps, sizeof (caps));
1293 	caps.op_to_write = BE_32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
1294 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
1295 	caps.cfvalid_to_len16 = BE_32(F_FW_CAPS_CONFIG_CMD_CFVALID |
1296 	    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
1297 	    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(maddr >> 16) | FW_LEN16(caps));
1298 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof (caps), &caps);
1299 	if (rc != 0) {
1300 		cxgb_printf(sc->dip, CE_WARN,
1301 		    "failed to pre-process config file: %d.\n", rc);
1302 		return (rc);
1303 	}
1304 
1305 	finicsum = ntohl(caps.finicsum);
1306 	cfcsum = ntohl(caps.cfcsum);
1307 	if (finicsum != cfcsum) {
1308 		cxgb_printf(sc->dip, CE_WARN,
1309 		    "WARNING: config file checksum mismatch: %08x %08x\n",
1310 		    finicsum, cfcsum);
1311 	}
1312 	sc->cfcsum = cfcsum;
1313 
1314 	/* TODO: Need to configure this correctly */
1315 	caps.toecaps = htons(FW_CAPS_CONFIG_TOE);
1316 	caps.iscsicaps = 0;
1317 	caps.rdmacaps = 0;
1318 	caps.fcoecaps = 0;
1319 	/* TODO: Disable VNIC cap for now */
1320 	caps.niccaps ^= htons(FW_CAPS_CONFIG_NIC_VM);
1321 
1322 	caps.op_to_write = htonl(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
1323 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
1324 	caps.cfvalid_to_len16 = htonl(FW_LEN16(caps));
1325 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof (caps), NULL);
1326 	if (rc != 0) {
1327 		cxgb_printf(sc->dip, CE_WARN,
1328 		    "failed to process config file: %d.\n", rc);
1329 		return (rc);
1330 	}
1331 
1332 	return (0);
1333 }
1334 
1335 /*
1336  * Tweak configuration based on module parameters, etc.  Most of these have
1337  * defaults assigned to them by Firmware Configuration Files (if we're using
1338  * them) but need to be explicitly set if we're using hard-coded
1339  * initialization.  But even in the case of using Firmware Configuration
1340  * Files, we'd like to expose the ability to change these via module
1341  * parameters so these are essentially common tweaks/settings for
1342  * Configuration Files and hard-coded initialization ...
1343  */
1344 static int
adap__pre_init_tweaks(struct adapter * sc)1345 adap__pre_init_tweaks(struct adapter *sc)
1346 {
1347 	int rx_dma_offset = 2; /* Offset of RX packets into DMA buffers */
1348 
1349 	/*
1350 	 * Fix up various Host-Dependent Parameters like Page Size, Cache
1351 	 * Line Size, etc.  The firmware default is for a 4KB Page Size and
1352 	 * 64B Cache Line Size ...
1353 	 */
1354 	(void) t4_fixup_host_params_compat(sc, PAGE_SIZE, _CACHE_LINE_SIZE,
1355 	    T5_LAST_REV);
1356 
1357 	t4_set_reg_field(sc, A_SGE_CONTROL, V_PKTSHIFT(M_PKTSHIFT),
1358 	    V_PKTSHIFT(rx_dma_offset));
1359 
1360 	return (0);
1361 }
1362 /*
1363  * Retrieve parameters that are needed (or nice to have) prior to calling
1364  * t4_sge_init and t4_fw_initialize.
1365  */
1366 static int
get_params__pre_init(struct adapter * sc)1367 get_params__pre_init(struct adapter *sc)
1368 {
1369 	int rc;
1370 	uint32_t param[2], val[2];
1371 	struct fw_devlog_cmd cmd;
1372 	struct devlog_params *dlog = &sc->params.devlog;
1373 
1374 	/*
1375 	 * Grab the raw VPD parameters.
1376 	 */
1377 	rc = -t4_get_raw_vpd_params(sc, &sc->params.vpd);
1378 	if (rc != 0) {
1379 		cxgb_printf(sc->dip, CE_WARN,
1380 		    "failed to query VPD parameters (pre_init): %d.\n", rc);
1381 		return (rc);
1382 	}
1383 
1384 	param[0] = FW_PARAM_DEV(PORTVEC);
1385 	param[1] = FW_PARAM_DEV(CCLK);
1386 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
1387 	if (rc != 0) {
1388 		cxgb_printf(sc->dip, CE_WARN,
1389 		    "failed to query parameters (pre_init): %d.\n", rc);
1390 		return (rc);
1391 	}
1392 
1393 	if (val[0] == 0) {
1394 		cxgb_printf(sc->dip, CE_WARN, "no usable ports");
1395 		return (ENODEV);
1396 	}
1397 
1398 	sc->params.portvec = val[0];
1399 	sc->params.nports = 0;
1400 	while (val[0]) {
1401 		sc->params.nports++;
1402 		val[0] &= val[0] - 1;
1403 	}
1404 
1405 	sc->params.vpd.cclk = val[1];
1406 
1407 	/* Read device log parameters. */
1408 	bzero(&cmd, sizeof (cmd));
1409 	cmd.op_to_write = htonl(V_FW_CMD_OP(FW_DEVLOG_CMD) |
1410 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
1411 	cmd.retval_len16 = htonl(FW_LEN16(cmd));
1412 	rc = -t4_wr_mbox(sc, sc->mbox, &cmd, sizeof (cmd), &cmd);
1413 	if (rc != 0) {
1414 		cxgb_printf(sc->dip, CE_WARN,
1415 		    "failed to get devlog parameters: %d.\n", rc);
1416 		bzero(dlog, sizeof (*dlog));
1417 		rc = 0;	/* devlog isn't critical for device operation */
1418 	} else {
1419 		val[0] = ntohl(cmd.memtype_devlog_memaddr16_devlog);
1420 		dlog->memtype = G_FW_DEVLOG_CMD_MEMTYPE_DEVLOG(val[0]);
1421 		dlog->start = G_FW_DEVLOG_CMD_MEMADDR16_DEVLOG(val[0]) << 4;
1422 		dlog->size = ntohl(cmd.memsize_devlog);
1423 	}
1424 
1425 	return (rc);
1426 }
1427 
1428 /*
1429  * Retrieve various parameters that are of interest to the driver.  The device
1430  * has been initialized by the firmware at this point.
1431  */
1432 static int
get_params__post_init(struct adapter * sc)1433 get_params__post_init(struct adapter *sc)
1434 {
1435 	int rc;
1436 	uint32_t param[4], val[4];
1437 	struct fw_caps_config_cmd caps;
1438 
1439 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
1440 	param[1] = FW_PARAM_PFVF(EQ_START);
1441 	param[2] = FW_PARAM_PFVF(IQFLINT_END);
1442 	param[3] = FW_PARAM_PFVF(EQ_END);
1443 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
1444 	if (rc != 0) {
1445 		cxgb_printf(sc->dip, CE_WARN,
1446 		    "failed to query parameters (post_init): %d.\n", rc);
1447 		return (rc);
1448 	}
1449 
1450 	sc->sge.iq_start = val[0];
1451 	sc->sge.eq_start = val[1];
1452 	sc->sge.iqmap_sz = val[2] - sc->sge.iq_start + 1;
1453 	sc->sge.eqmap_sz = val[3] - sc->sge.eq_start + 1;
1454 
1455 	uint32_t r = t4_read_reg(sc, A_SGE_EGRESS_QUEUES_PER_PAGE_PF);
1456 	r >>= S_QUEUESPERPAGEPF0 +
1457 	    (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * sc->pf;
1458 	sc->sge.s_qpp = r & M_QUEUESPERPAGEPF0;
1459 
1460 	/* get capabilites */
1461 	bzero(&caps, sizeof (caps));
1462 	caps.op_to_write = htonl(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
1463 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
1464 	caps.cfvalid_to_len16 = htonl(FW_LEN16(caps));
1465 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof (caps), &caps);
1466 	if (rc != 0) {
1467 		cxgb_printf(sc->dip, CE_WARN,
1468 		    "failed to get card capabilities: %d.\n", rc);
1469 		return (rc);
1470 	}
1471 
1472 	/* Check if DBQ timer is available for tracking egress completions */
1473 	param[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
1474 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DBQ_TIMERTICK));
1475 	rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
1476 	if (rc == 0) {
1477 		sc->sge.dbq_timer_tick = val[0];
1478 		rc = t4_read_sge_dbqtimers(sc,
1479 		    ARRAY_SIZE(sc->sge.dbq_timers), sc->sge.dbq_timers);
1480 		if (rc == 0) {
1481 			sc->flags |= TAF_DBQ_TIMER;
1482 		} else {
1483 			sc->sge.dbq_timer_tick = 0;
1484 		}
1485 	}
1486 
1487 	/*
1488 	 * Now that we know if the DBQ timer is present, tune the properties for
1489 	 * hold-off parameter defaults.
1490 	 */
1491 	struct driver_properties *prp = &sc->props;
1492 	if ((sc->flags & TAF_DBQ_TIMER) != 0) {
1493 		/*
1494 		 * Choose default DBQ timer index to be closest to 100us.  With
1495 		 * that available, more aggressive coalescing on the FWQ is
1496 		 * unnecessary, so shorter hold-off parameters are fine there.
1497 		 */
1498 		prp->dbq_timer_idx = t4_choose_dbq_timer(sc, 100);
1499 		prp->fwq_tmr_idx = t4_choose_holdoff_timer(sc, 10);
1500 		prp->fwq_pktc_idx = t4_choose_holdoff_pktcnt(sc, -1);
1501 	} else {
1502 		/*
1503 		 * Without the DBQ timer, we fall back to the
1504 		 * CIDXFlushThresholdOverride mechanism for TX completions,
1505 		 * which can result in many more notifications, depending on the
1506 		 * traffic pattern.  More aggressive interrupt coalescing on the
1507 		 * firmware queue (where such notifications land) is recommended
1508 		 * to deal with it.
1509 		 *
1510 		 * Pick values closest to a hold-off of 100us and/or 32 entries.
1511 		 */
1512 		prp->fwq_tmr_idx = t4_choose_holdoff_timer(sc, 100);
1513 		prp->fwq_pktc_idx = t4_choose_holdoff_pktcnt(sc, 32);
1514 	}
1515 	sc->sge.fwq_tmr_idx = prp->fwq_tmr_idx;
1516 	sc->sge.fwq_pktc_idx = prp->fwq_pktc_idx;
1517 
1518 	rc = -t4_get_pfres(sc);
1519 	if (rc != 0) {
1520 		cxgb_printf(sc->dip, CE_WARN,
1521 		    "failed to query PF resource params: %d.\n", rc);
1522 		return (rc);
1523 	}
1524 
1525 	/* These are finalized by FW initialization, load their values now */
1526 	val[0] = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
1527 	sc->params.tp.tre = G_TIMERRESOLUTION(val[0]);
1528 	sc->params.tp.dack_re = G_DELAYEDACKRESOLUTION(val[0]);
1529 	t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
1530 	(void) t4_init_sge_params(sc);
1531 
1532 	return (0);
1533 }
1534 
1535 static int
set_params__post_init(struct adapter * sc)1536 set_params__post_init(struct adapter *sc)
1537 {
1538 	uint32_t param, val;
1539 
1540 	/* ask for encapsulated CPLs */
1541 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
1542 	val = 1;
1543 	(void) t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
1544 
1545 	return (0);
1546 }
1547 
1548 /* TODO: verify */
1549 static void
t4_setup_adapter_memwin(struct adapter * sc)1550 t4_setup_adapter_memwin(struct adapter *sc)
1551 {
1552 	pci_regspec_t *data;
1553 	int rc;
1554 	uint_t n;
1555 	uintptr_t bar0;
1556 	uintptr_t mem_win0_base, mem_win1_base, mem_win2_base;
1557 	uintptr_t mem_win2_aperture;
1558 
1559 	rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, sc->dip,
1560 	    DDI_PROP_DONTPASS, "assigned-addresses", (int **)&data, &n);
1561 	if (rc != DDI_SUCCESS) {
1562 		cxgb_printf(sc->dip, CE_WARN,
1563 		    "failed to lookup \"assigned-addresses\" property: %d", rc);
1564 		return;
1565 	}
1566 	n /= sizeof (*data);
1567 
1568 	bar0 = ((uint64_t)data[0].pci_phys_mid << 32) | data[0].pci_phys_low;
1569 	ddi_prop_free(data);
1570 
1571 	if (t4_cver_eq(sc, CHELSIO_T4)) {
1572 		mem_win0_base = bar0 + MEMWIN0_BASE;
1573 		mem_win1_base = bar0 + MEMWIN1_BASE;
1574 		mem_win2_base = bar0 + MEMWIN2_BASE;
1575 		mem_win2_aperture = MEMWIN2_APERTURE;
1576 	} else {
1577 		/* For T5, only relative offset inside the PCIe BAR is passed */
1578 		mem_win0_base = MEMWIN0_BASE;
1579 		mem_win1_base = MEMWIN1_BASE;
1580 		mem_win2_base = MEMWIN2_BASE_T5;
1581 		mem_win2_aperture = MEMWIN2_APERTURE_T5;
1582 	}
1583 
1584 	t4_write_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 0),
1585 	    mem_win0_base | V_BIR(0) |
1586 	    V_WINDOW(ilog2(MEMWIN0_APERTURE) - 10));
1587 
1588 	t4_write_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 1),
1589 	    mem_win1_base | V_BIR(0) |
1590 	    V_WINDOW(ilog2(MEMWIN1_APERTURE) - 10));
1591 
1592 	t4_write_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2),
1593 	    mem_win2_base | V_BIR(0) |
1594 	    V_WINDOW(ilog2(mem_win2_aperture) - 10));
1595 
1596 	/* flush */
1597 	(void) t4_read_reg(sc,
1598 	    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2));
1599 }
1600 
1601 /*
1602  * Positions the memory window such that it can be used to access the specified
1603  * address in the chip's address space.  The return value is the offset of addr
1604  * from the start of the window.
1605  */
1606 static uint32_t
t4_position_memwin(struct adapter * sc,int n,uint32_t addr)1607 t4_position_memwin(struct adapter *sc, int n, uint32_t addr)
1608 {
1609 	uint32_t start, pf;
1610 	uint32_t reg;
1611 
1612 	if (addr & 3) {
1613 		cxgb_printf(sc->dip, CE_WARN,
1614 		    "addr (0x%x) is not at a 4B boundary.\n", addr);
1615 		return (EFAULT);
1616 	}
1617 
1618 	if (t4_cver_eq(sc, CHELSIO_T4)) {
1619 		pf = 0;
1620 		start = addr & ~0xf;    /* start must be 16B aligned */
1621 	} else {
1622 		pf = V_PFNUM(sc->pf);
1623 		start = addr & ~0x7f;   /* start must be 128B aligned */
1624 	}
1625 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, n);
1626 
1627 	t4_write_reg(sc, reg, start | pf);
1628 	(void) t4_read_reg(sc, reg);
1629 
1630 	return (addr - start);
1631 }
1632 
1633 static int
prop_lookup_int(struct adapter * sc,char * name,int defval)1634 prop_lookup_int(struct adapter *sc, char *name, int defval)
1635 {
1636 	int rc;
1637 
1638 	rc = ddi_prop_get_int(sc->dev, sc->dip, DDI_PROP_DONTPASS, name, -1);
1639 	if (rc != -1)
1640 		return (rc);
1641 
1642 	return (ddi_prop_get_int(DDI_DEV_T_ANY, sc->dip, DDI_PROP_DONTPASS,
1643 	    name, defval));
1644 }
1645 
1646 const uint_t t4_holdoff_timer_default[SGE_NTIMERS] = {5, 10, 20, 50, 100, 200};
1647 const uint_t t4_holdoff_pktcnt_default[SGE_NCOUNTERS] = {1, 8, 16, 32};
1648 
1649 static int
init_driver_props(struct adapter * sc,struct driver_properties * p)1650 init_driver_props(struct adapter *sc, struct driver_properties *p)
1651 {
1652 	dev_t dev = sc->dev;
1653 	dev_info_t *dip = sc->dip;
1654 	int i;
1655 
1656 	/*
1657 	 * For now, just use the defaults for the hold-off timers and counters.
1658 	 *
1659 	 * They can be turned back into writable properties if/when there is a
1660 	 * demonstrable need.
1661 	 */
1662 	for (uint_t i = 0; i < SGE_NTIMERS; i++) {
1663 		p->holdoff_timer_us[i] = t4_holdoff_timer_default[i];
1664 	}
1665 	for (uint_t i = 0; i < SGE_NCOUNTERS; i++) {
1666 		p->holdoff_pktcnt[i] = t4_holdoff_pktcnt_default[i];
1667 	}
1668 	(void) ddi_prop_update_int_array(dev, dip, "holdoff-timer-us-values",
1669 	    (int *)p->holdoff_timer_us, SGE_NTIMERS);
1670 	(void) ddi_prop_update_int_array(dev, dip, "holdoff-pkt-counter-values",
1671 	    (int *)p->holdoff_pktcnt, SGE_NCOUNTERS);
1672 
1673 	/*
1674 	 * Maximum # of tx and rx queues to use for each
1675 	 * 100G, 40G, 25G, 10G and 1G port.
1676 	 */
1677 	p->max_ntxq_10g = prop_lookup_int(sc, "max-ntxq-10G-port", 8);
1678 	(void) ddi_prop_update_int(dev, dip, "max-ntxq-10G-port",
1679 	    p->max_ntxq_10g);
1680 
1681 	p->max_nrxq_10g = prop_lookup_int(sc, "max-nrxq-10G-port", 8);
1682 	(void) ddi_prop_update_int(dev, dip, "max-nrxq-10G-port",
1683 	    p->max_nrxq_10g);
1684 
1685 	p->max_ntxq_1g = prop_lookup_int(sc, "max-ntxq-1G-port", 2);
1686 	(void) ddi_prop_update_int(dev, dip, "max-ntxq-1G-port",
1687 	    p->max_ntxq_1g);
1688 
1689 	p->max_nrxq_1g = prop_lookup_int(sc, "max-nrxq-1G-port", 2);
1690 	(void) ddi_prop_update_int(dev, dip, "max-nrxq-1G-port",
1691 	    p->max_nrxq_1g);
1692 
1693 	/*
1694 	 * Holdoff parameters for 10G and 1G ports.
1695 	 */
1696 	p->tmr_idx_10g = prop_lookup_int(sc, "holdoff-timer-idx-10G", 0);
1697 	(void) ddi_prop_update_int(dev, dip, "holdoff-timer-idx-10G",
1698 	    p->tmr_idx_10g);
1699 
1700 	p->pktc_idx_10g = prop_lookup_int(sc, "holdoff-pktc-idx-10G", 2);
1701 	(void) ddi_prop_update_int(dev, dip, "holdoff-pktc-idx-10G",
1702 	    p->pktc_idx_10g);
1703 
1704 	p->tmr_idx_1g = prop_lookup_int(sc, "holdoff-timer-idx-1G", 0);
1705 	(void) ddi_prop_update_int(dev, dip, "holdoff-timer-idx-1G",
1706 	    p->tmr_idx_1g);
1707 
1708 	p->pktc_idx_1g = prop_lookup_int(sc, "holdoff-pktc-idx-1G", 2);
1709 	(void) ddi_prop_update_int(dev, dip, "holdoff-pktc-idx-1G",
1710 	    p->pktc_idx_1g);
1711 
1712 	/*
1713 	 * Size (number of entries) of each tx and rx queue.
1714 	 */
1715 	i = prop_lookup_int(sc, "qsize-txq", TX_EQ_QSIZE);
1716 	p->qsize_txq = max(i, 128);
1717 	if (p->qsize_txq != i) {
1718 		cxgb_printf(dip, CE_WARN,
1719 		    "using %d instead of %d as the tx queue size",
1720 		    p->qsize_txq, i);
1721 	}
1722 	(void) ddi_prop_update_int(dev, dip, "qsize-txq", p->qsize_txq);
1723 
1724 	i = prop_lookup_int(sc, "qsize-rxq", RX_IQ_QSIZE);
1725 	p->qsize_rxq = max(i, 128);
1726 	while (p->qsize_rxq & 7)
1727 		p->qsize_rxq--;
1728 	if (p->qsize_rxq != i) {
1729 		cxgb_printf(dip, CE_WARN,
1730 		    "using %d instead of %d as the rx queue size",
1731 		    p->qsize_rxq, i);
1732 	}
1733 	(void) ddi_prop_update_int(dev, dip, "qsize-rxq", p->qsize_rxq);
1734 
1735 	/*
1736 	 * Interrupt types allowed.
1737 	 * Bits 0, 1, 2 = INTx, MSI, MSI-X respectively.  See sys/ddi_intr.h
1738 	 */
1739 	p->intr_types = prop_lookup_int(sc, "interrupt-types",
1740 	    DDI_INTR_TYPE_MSIX | DDI_INTR_TYPE_MSI | DDI_INTR_TYPE_FIXED);
1741 	(void) ddi_prop_update_int(dev, dip, "interrupt-types", p->intr_types);
1742 
1743 	/*
1744 	 * Write combining
1745 	 * 0 to disable, 1 to enable
1746 	 */
1747 	p->wc = prop_lookup_int(sc, "write-combine", 1);
1748 	cxgb_printf(dip, CE_WARN, "write-combine: using of %d", p->wc);
1749 	if (p->wc != 0 && p->wc != 1) {
1750 		cxgb_printf(dip, CE_WARN,
1751 		    "write-combine: using 1 instead of %d", p->wc);
1752 		p->wc = 1;
1753 	}
1754 	(void) ddi_prop_update_int(dev, dip, "write-combine", p->wc);
1755 
1756 	p->t4_fw_install = prop_lookup_int(sc, "t4_fw_install", 1);
1757 	if (p->t4_fw_install != 0 && p->t4_fw_install != 2)
1758 		p->t4_fw_install = 1;
1759 	(void) ddi_prop_update_int(dev, dip, "t4_fw_install", p->t4_fw_install);
1760 
1761 	/* Multiple Rings */
1762 	p->multi_rings = prop_lookup_int(sc, "multi-rings", 1);
1763 	if (p->multi_rings != 0 && p->multi_rings != 1) {
1764 		cxgb_printf(dip, CE_NOTE,
1765 		    "multi-rings: using value 1 instead of %d", p->multi_rings);
1766 		p->multi_rings = 1;
1767 	}
1768 
1769 	(void) ddi_prop_update_int(dev, dip, "multi-rings", p->multi_rings);
1770 
1771 	return (0);
1772 }
1773 
1774 static int
remove_extra_props(struct adapter * sc,int n10g,int n1g)1775 remove_extra_props(struct adapter *sc, int n10g, int n1g)
1776 {
1777 	if (n10g == 0) {
1778 		(void) ddi_prop_remove(sc->dev, sc->dip, "max-ntxq-10G-port");
1779 		(void) ddi_prop_remove(sc->dev, sc->dip, "max-nrxq-10G-port");
1780 		(void) ddi_prop_remove(sc->dev, sc->dip,
1781 		    "holdoff-timer-idx-10G");
1782 		(void) ddi_prop_remove(sc->dev, sc->dip,
1783 		    "holdoff-pktc-idx-10G");
1784 	}
1785 
1786 	if (n1g == 0) {
1787 		(void) ddi_prop_remove(sc->dev, sc->dip, "max-ntxq-1G-port");
1788 		(void) ddi_prop_remove(sc->dev, sc->dip, "max-nrxq-1G-port");
1789 		(void) ddi_prop_remove(sc->dev, sc->dip,
1790 		    "holdoff-timer-idx-1G");
1791 		(void) ddi_prop_remove(sc->dev, sc->dip, "holdoff-pktc-idx-1G");
1792 	}
1793 
1794 	return (0);
1795 }
1796 
1797 static int
cfg_itype_and_nqueues(struct adapter * sc,int n10g,int n1g,struct intrs_and_queues * iaq)1798 cfg_itype_and_nqueues(struct adapter *sc, int n10g, int n1g,
1799     struct intrs_and_queues *iaq)
1800 {
1801 	struct driver_properties *p = &sc->props;
1802 	int rc, itype, itypes, navail, nc, n;
1803 	int pfres_rxq, pfres_txq, pfresq;
1804 
1805 	bzero(iaq, sizeof (*iaq));
1806 	nc = ncpus;	/* our snapshot of the number of CPUs */
1807 	iaq->ntxq10g = min(nc, p->max_ntxq_10g);
1808 	iaq->ntxq1g = min(nc, p->max_ntxq_1g);
1809 	iaq->nrxq10g = min(nc, p->max_nrxq_10g);
1810 	iaq->nrxq1g = min(nc, p->max_nrxq_1g);
1811 
1812 	pfres_rxq = iaq->nrxq10g * n10g + iaq->nrxq1g * n1g;
1813 	pfres_txq = iaq->ntxq10g * n10g + iaq->ntxq1g * n1g;
1814 
1815 	/*
1816 	 * If current configuration of max number of Rxqs and Txqs exceed
1817 	 * the max available for all the ports under this PF, then shrink
1818 	 * the queues to max available. Reduce them in a way that each
1819 	 * port under this PF has equally distributed number of queues.
1820 	 * Must guarantee at least 1 queue for each port for both NIC
1821 	 * and Offload queues.
1822 	 *
1823 	 * neq - fixed max number of Egress queues on Tx path and Free List
1824 	 * queues that hold Rx payload data on Rx path. Half are reserved
1825 	 * for Egress queues and the other half for Free List queues.
1826 	 * Hence, the division by 2.
1827 	 *
1828 	 * niqflint - max number of Ingress queues with interrupts on Rx
1829 	 * path to receive completions that indicate Rx payload has been
1830 	 * posted in its associated Free List queue. Also handles Tx
1831 	 * completions for packets successfully transmitted on Tx path.
1832 	 *
1833 	 * nethctrl - max number of Egress queues only for Tx path. This
1834 	 * number is usually half of neq. However, if it became less than
1835 	 * neq due to lack of resources based on firmware configuration,
1836 	 * then take the lower value.
1837 	 */
1838 	const uint_t max_rxq =
1839 	    MIN(sc->params.pfres.neq / 2, sc->params.pfres.niqflint);
1840 	while (pfres_rxq > max_rxq) {
1841 		pfresq = pfres_rxq;
1842 
1843 		if (iaq->nrxq10g > 1) {
1844 			iaq->nrxq10g--;
1845 			pfres_rxq -= n10g;
1846 		}
1847 
1848 		if (iaq->nrxq1g > 1) {
1849 			iaq->nrxq1g--;
1850 			pfres_rxq -= n1g;
1851 		}
1852 
1853 		/* Break if nothing changed */
1854 		if (pfresq == pfres_rxq)
1855 			break;
1856 	}
1857 
1858 	const uint_t max_txq =
1859 	    MIN(sc->params.pfres.neq / 2, sc->params.pfres.nethctrl);
1860 	while (pfres_txq > max_txq) {
1861 		pfresq = pfres_txq;
1862 
1863 		if (iaq->ntxq10g > 1) {
1864 			iaq->ntxq10g--;
1865 			pfres_txq -= n10g;
1866 		}
1867 
1868 		if (iaq->ntxq1g > 1) {
1869 			iaq->ntxq1g--;
1870 			pfres_txq -= n1g;
1871 		}
1872 
1873 		/* Break if nothing changed */
1874 		if (pfresq == pfres_txq)
1875 			break;
1876 	}
1877 
1878 	rc = ddi_intr_get_supported_types(sc->dip, &itypes);
1879 	if (rc != DDI_SUCCESS) {
1880 		cxgb_printf(sc->dip, CE_WARN,
1881 		    "failed to determine supported interrupt types: %d", rc);
1882 		return (rc);
1883 	}
1884 
1885 	for (itype = DDI_INTR_TYPE_MSIX; itype; itype >>= 1) {
1886 		ASSERT(itype == DDI_INTR_TYPE_MSIX ||
1887 		    itype == DDI_INTR_TYPE_MSI ||
1888 		    itype == DDI_INTR_TYPE_FIXED);
1889 
1890 		if ((itype & itypes & p->intr_types) == 0)
1891 			continue;	/* not supported or not allowed */
1892 
1893 		navail = 0;
1894 		rc = ddi_intr_get_navail(sc->dip, itype, &navail);
1895 		if (rc != DDI_SUCCESS || navail == 0) {
1896 			cxgb_printf(sc->dip, CE_WARN,
1897 			    "failed to get # of interrupts for type %d: %d",
1898 			    itype, rc);
1899 			continue;	/* carry on */
1900 		}
1901 
1902 		iaq->intr_type = itype;
1903 		if (navail == 0)
1904 			continue;
1905 
1906 		/*
1907 		 * Best option: an interrupt vector for errors, one for the
1908 		 * firmware event queue, and one each for each rxq (NIC as well
1909 		 * as offload).
1910 		 */
1911 		iaq->nirq = T4_EXTRA_INTR;
1912 		iaq->nirq += n10g * iaq->nrxq10g;
1913 		iaq->nirq += n1g * iaq->nrxq1g;
1914 
1915 		if (iaq->nirq <= navail &&
1916 		    (itype != DDI_INTR_TYPE_MSI || ISP2(iaq->nirq))) {
1917 			iaq->intr_fwd = 0;
1918 			goto allocate;
1919 		}
1920 
1921 		/*
1922 		 * Second best option: an interrupt vector for errors, one for
1923 		 * the firmware event queue, and one each for either NIC or
1924 		 * offload rxq's.
1925 		 */
1926 		iaq->nirq = T4_EXTRA_INTR;
1927 		iaq->nirq += n10g * iaq->nrxq10g;
1928 		iaq->nirq += n1g * iaq->nrxq1g;
1929 		if (iaq->nirq <= navail &&
1930 		    (itype != DDI_INTR_TYPE_MSI || ISP2(iaq->nirq))) {
1931 			iaq->intr_fwd = 1;
1932 			goto allocate;
1933 		}
1934 
1935 		/*
1936 		 * Next best option: an interrupt vector for errors, one for the
1937 		 * firmware event queue, and at least one per port.  At this
1938 		 * point we know we'll have to downsize nrxq or nofldrxq to fit
1939 		 * what's available to us.
1940 		 */
1941 		iaq->nirq = T4_EXTRA_INTR;
1942 		iaq->nirq += n10g + n1g;
1943 		if (iaq->nirq <= navail) {
1944 			int leftover = navail - iaq->nirq;
1945 
1946 			if (n10g > 0) {
1947 				int target = iaq->nrxq10g;
1948 
1949 				n = 1;
1950 				while (n < target && leftover >= n10g) {
1951 					leftover -= n10g;
1952 					iaq->nirq += n10g;
1953 					n++;
1954 				}
1955 				iaq->nrxq10g = min(n, iaq->nrxq10g);
1956 			}
1957 
1958 			if (n1g > 0) {
1959 				int target = iaq->nrxq1g;
1960 
1961 				n = 1;
1962 				while (n < target && leftover >= n1g) {
1963 					leftover -= n1g;
1964 					iaq->nirq += n1g;
1965 					n++;
1966 				}
1967 				iaq->nrxq1g = min(n, iaq->nrxq1g);
1968 			}
1969 
1970 			/*
1971 			 * We have arrived at a minimum value required to enable
1972 			 * per queue irq(either NIC or offload). Thus for non-
1973 			 * offload case, we will get a vector per queue, while
1974 			 * offload case, we will get a vector per offload/NIC q.
1975 			 * Hence enable Interrupt forwarding only for offload
1976 			 * case.
1977 			 */
1978 			if (itype != DDI_INTR_TYPE_MSI) {
1979 				goto allocate;
1980 			}
1981 		}
1982 
1983 		/*
1984 		 * Least desirable option: one interrupt vector for everything.
1985 		 */
1986 		iaq->nirq = iaq->nrxq10g = iaq->nrxq1g = 1;
1987 		iaq->intr_fwd = 1;
1988 
1989 allocate:
1990 		return (0);
1991 	}
1992 
1993 	cxgb_printf(sc->dip, CE_WARN,
1994 	    "failed to find a usable interrupt type.  supported=%d, allowed=%d",
1995 	    itypes, p->intr_types);
1996 	return (DDI_FAILURE);
1997 }
1998 
1999 static int
add_child_node(struct adapter * sc,int idx)2000 add_child_node(struct adapter *sc, int idx)
2001 {
2002 	int rc;
2003 	struct port_info *pi;
2004 
2005 	if (idx < 0 || idx >= sc->params.nports)
2006 		return (EINVAL);
2007 
2008 	pi = sc->port[idx];
2009 	if (pi == NULL)
2010 		return (ENODEV);	/* t4_port_init failed earlier */
2011 
2012 	PORT_LOCK(pi);
2013 	if (pi->dip != NULL) {
2014 		rc = 0;		/* EEXIST really, but then bus_config fails */
2015 		goto done;
2016 	}
2017 
2018 	rc = ndi_devi_alloc(sc->dip, T4_PORT_NAME, DEVI_SID_NODEID, &pi->dip);
2019 	if (rc != DDI_SUCCESS || pi->dip == NULL) {
2020 		rc = ENOMEM;
2021 		goto done;
2022 	}
2023 
2024 	(void) ddi_set_parent_data(pi->dip, pi);
2025 	(void) ndi_devi_bind_driver(pi->dip, 0);
2026 	rc = 0;
2027 done:
2028 	PORT_UNLOCK(pi);
2029 	return (rc);
2030 }
2031 
2032 static int
remove_child_node(struct adapter * sc,int idx)2033 remove_child_node(struct adapter *sc, int idx)
2034 {
2035 	int rc;
2036 	struct port_info *pi;
2037 
2038 	if (idx < 0 || idx >= sc->params.nports)
2039 		return (EINVAL);
2040 
2041 	pi = sc->port[idx];
2042 	if (pi == NULL)
2043 		return (ENODEV);
2044 
2045 	PORT_LOCK(pi);
2046 	if (pi->dip == NULL) {
2047 		rc = ENODEV;
2048 		goto done;
2049 	}
2050 
2051 	rc = ndi_devi_free(pi->dip);
2052 	if (rc == 0)
2053 		pi->dip = NULL;
2054 done:
2055 	PORT_UNLOCK(pi);
2056 	return (rc);
2057 }
2058 
2059 static const char *
t4_port_speed_name(const struct port_info * pi)2060 t4_port_speed_name(const struct port_info *pi)
2061 {
2062 	if (pi == NULL) {
2063 		return ("-");
2064 	}
2065 
2066 	const uint32_t pcaps = pi->link_cfg.pcaps;
2067 	if (pcaps & FW_PORT_CAP32_SPEED_100G) {
2068 		return ("100G");
2069 	} else if (pcaps & FW_PORT_CAP32_SPEED_50G) {
2070 		return ("50G");
2071 	} else if (pcaps & FW_PORT_CAP32_SPEED_40G) {
2072 		return ("40G");
2073 	} else if (pcaps & FW_PORT_CAP32_SPEED_25G) {
2074 		return ("25G");
2075 	} else if (pcaps & FW_PORT_CAP32_SPEED_10G) {
2076 		return ("10G");
2077 	} else {
2078 		return ("1G");
2079 	}
2080 }
2081 
2082 #define	KS_UINIT(x)	kstat_named_init(&kstatp->x, #x, KSTAT_DATA_ULONG)
2083 #define	KS_CINIT(x)	kstat_named_init(&kstatp->x, #x, KSTAT_DATA_CHAR)
2084 #define	KS_U64INIT(x)	kstat_named_init(&kstatp->x, #x, KSTAT_DATA_UINT64)
2085 #define	KS_U_SET(x, y)	kstatp->x.value.ul = (y)
2086 #define	KS_C_SET(x, ...)	\
2087 			(void) snprintf(kstatp->x.value.c, 16,  __VA_ARGS__)
2088 
2089 /*
2090  * t4nex:X:config
2091  */
2092 struct t4_kstats {
2093 	kstat_named_t chip_ver;
2094 	kstat_named_t fw_vers;
2095 	kstat_named_t tp_vers;
2096 	kstat_named_t driver_version;
2097 	kstat_named_t serial_number;
2098 	kstat_named_t ec_level;
2099 	kstat_named_t id;
2100 	kstat_named_t bus_type;
2101 	kstat_named_t bus_width;
2102 	kstat_named_t bus_speed;
2103 	kstat_named_t core_clock;
2104 	kstat_named_t port_cnt;
2105 	kstat_named_t port_type;
2106 	kstat_named_t pci_vendor_id;
2107 	kstat_named_t pci_device_id;
2108 };
2109 static kstat_t *
setup_kstats(struct adapter * sc)2110 setup_kstats(struct adapter *sc)
2111 {
2112 	kstat_t *ksp;
2113 	struct t4_kstats *kstatp;
2114 	int ndata;
2115 	struct pci_params *p = &sc->params.pci;
2116 	struct vpd_params *v = &sc->params.vpd;
2117 	uint16_t pci_vendor, pci_device;
2118 
2119 	ndata = sizeof (struct t4_kstats) / sizeof (kstat_named_t);
2120 
2121 	ksp = kstat_create(T4_NEXUS_NAME, ddi_get_instance(sc->dip), "config",
2122 	    "nexus", KSTAT_TYPE_NAMED, ndata, 0);
2123 	if (ksp == NULL) {
2124 		cxgb_printf(sc->dip, CE_WARN, "failed to initialize kstats.");
2125 		return (NULL);
2126 	}
2127 
2128 	kstatp = (struct t4_kstats *)ksp->ks_data;
2129 
2130 	KS_UINIT(chip_ver);
2131 	KS_CINIT(fw_vers);
2132 	KS_CINIT(tp_vers);
2133 	KS_CINIT(driver_version);
2134 	KS_CINIT(serial_number);
2135 	KS_CINIT(ec_level);
2136 	KS_CINIT(id);
2137 	KS_CINIT(bus_type);
2138 	KS_CINIT(bus_width);
2139 	KS_CINIT(bus_speed);
2140 	KS_UINIT(core_clock);
2141 	KS_UINIT(port_cnt);
2142 	KS_CINIT(port_type);
2143 	KS_CINIT(pci_vendor_id);
2144 	KS_CINIT(pci_device_id);
2145 
2146 	KS_U_SET(chip_ver, sc->params.chip);
2147 	KS_C_SET(fw_vers, "%d.%d.%d.%d",
2148 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
2149 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
2150 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
2151 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
2152 	KS_C_SET(tp_vers, "%d.%d.%d.%d",
2153 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
2154 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
2155 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
2156 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
2157 	KS_C_SET(driver_version, DRV_VERSION);
2158 	KS_C_SET(serial_number, "%s", v->sn);
2159 	KS_C_SET(ec_level, "%s", v->ec);
2160 	KS_C_SET(id, "%s", v->id);
2161 	KS_C_SET(bus_type, "pci-express");
2162 	KS_C_SET(bus_width, "x%d lanes", p->width);
2163 	KS_C_SET(bus_speed, "%d", p->speed);
2164 	KS_U_SET(core_clock, v->cclk);
2165 	KS_U_SET(port_cnt, sc->params.nports);
2166 
2167 	pci_vendor = pci_config_get16(sc->pci_regh, PCI_CONF_VENID);
2168 	KS_C_SET(pci_vendor_id, "0x%x", pci_vendor);
2169 
2170 	pci_device = pci_config_get16(sc->pci_regh, PCI_CONF_DEVID);
2171 	KS_C_SET(pci_device_id, "0x%x", pci_device);
2172 
2173 	KS_C_SET(port_type, "%s/%s/%s/%s",
2174 	    t4_port_speed_name(sc->port[0]),
2175 	    t4_port_speed_name(sc->port[1]),
2176 	    t4_port_speed_name(sc->port[2]),
2177 	    t4_port_speed_name(sc->port[3]));
2178 
2179 	/* Do NOT set ksp->ks_update.  These kstats do not change. */
2180 
2181 	/* Install the kstat */
2182 	ksp->ks_private = (void *)sc;
2183 	kstat_install(ksp);
2184 
2185 	return (ksp);
2186 }
2187 
2188 /*
2189  * t4nex:X:stat
2190  */
2191 struct t4_wc_kstats {
2192 	kstat_named_t write_coal_success;
2193 	kstat_named_t write_coal_failure;
2194 };
2195 static kstat_t *
setup_wc_kstats(struct adapter * sc)2196 setup_wc_kstats(struct adapter *sc)
2197 {
2198 	kstat_t *ksp;
2199 	struct t4_wc_kstats *kstatp;
2200 
2201 	const uint_t ndata =
2202 	    sizeof (struct t4_wc_kstats) / sizeof (kstat_named_t);
2203 	ksp = kstat_create(T4_NEXUS_NAME, ddi_get_instance(sc->dip), "stats",
2204 	    "nexus", KSTAT_TYPE_NAMED, ndata, 0);
2205 	if (ksp == NULL) {
2206 		cxgb_printf(sc->dip, CE_WARN, "failed to initialize kstats.");
2207 		return (NULL);
2208 	}
2209 
2210 	kstatp = (struct t4_wc_kstats *)ksp->ks_data;
2211 
2212 	KS_UINIT(write_coal_success);
2213 	KS_UINIT(write_coal_failure);
2214 
2215 	ksp->ks_update = update_wc_kstats;
2216 	/* Install the kstat */
2217 	ksp->ks_private = (void *)sc;
2218 	kstat_install(ksp);
2219 
2220 	return (ksp);
2221 }
2222 
2223 static int
update_wc_kstats(kstat_t * ksp,int rw)2224 update_wc_kstats(kstat_t *ksp, int rw)
2225 {
2226 	struct t4_wc_kstats *kstatp = (struct t4_wc_kstats *)ksp->ks_data;
2227 	struct adapter *sc = ksp->ks_private;
2228 	uint32_t wc_total, wc_success, wc_failure;
2229 
2230 	if (rw == KSTAT_WRITE)
2231 		return (0);
2232 
2233 	if (t4_cver_ge(sc, CHELSIO_T5)) {
2234 		wc_total = t4_read_reg(sc, A_SGE_STAT_TOTAL);
2235 		wc_failure = t4_read_reg(sc, A_SGE_STAT_MATCH);
2236 		wc_success = wc_total - wc_failure;
2237 	} else {
2238 		wc_success = 0;
2239 		wc_failure = 0;
2240 	}
2241 
2242 	KS_U_SET(write_coal_success, wc_success);
2243 	KS_U_SET(write_coal_failure, wc_failure);
2244 
2245 	return (0);
2246 }
2247 
2248 /*
2249  * cxgbe:X:fec
2250  *
2251  * This provides visibility into the errors that have been found by the
2252  * different FEC subsystems. While it's tempting to combine the two different
2253  * FEC types logically, the data that the errors tell us are pretty different
2254  * between the two. Firecode is strictly per-lane, but RS has parts that are
2255  * related to symbol distribution to lanes and also to the overall channel.
2256  */
2257 struct cxgbe_port_fec_kstats {
2258 	kstat_named_t rs_corr;
2259 	kstat_named_t rs_uncorr;
2260 	kstat_named_t rs_sym0_corr;
2261 	kstat_named_t rs_sym1_corr;
2262 	kstat_named_t rs_sym2_corr;
2263 	kstat_named_t rs_sym3_corr;
2264 	kstat_named_t fc_lane0_corr;
2265 	kstat_named_t fc_lane0_uncorr;
2266 	kstat_named_t fc_lane1_corr;
2267 	kstat_named_t fc_lane1_uncorr;
2268 	kstat_named_t fc_lane2_corr;
2269 	kstat_named_t fc_lane2_uncorr;
2270 	kstat_named_t fc_lane3_corr;
2271 	kstat_named_t fc_lane3_uncorr;
2272 };
2273 
2274 static uint32_t
read_fec_pair(struct port_info * pi,uint32_t lo_reg,uint32_t high_reg)2275 read_fec_pair(struct port_info *pi, uint32_t lo_reg, uint32_t high_reg)
2276 {
2277 	struct adapter *sc = pi->adapter;
2278 	uint8_t port = pi->tx_chan;
2279 	uint32_t low, high, ret;
2280 
2281 	low = t4_read_reg(sc, T5_PORT_REG(port, lo_reg));
2282 	high = t4_read_reg(sc, T5_PORT_REG(port, high_reg));
2283 	ret = low & 0xffff;
2284 	ret |= (high & 0xffff) << 16;
2285 	return (ret);
2286 }
2287 
2288 static int
update_port_fec_kstats(kstat_t * ksp,int rw)2289 update_port_fec_kstats(kstat_t *ksp, int rw)
2290 {
2291 	struct cxgbe_port_fec_kstats *fec = ksp->ks_data;
2292 	struct port_info *pi = ksp->ks_private;
2293 
2294 	if (rw == KSTAT_WRITE) {
2295 		return (EACCES);
2296 	}
2297 
2298 	/*
2299 	 * First go ahead and gather RS related stats.
2300 	 */
2301 	fec->rs_corr.value.ui64 += read_fec_pair(pi, T6_RS_FEC_CCW_LO,
2302 	    T6_RS_FEC_CCW_HI);
2303 	fec->rs_uncorr.value.ui64 += read_fec_pair(pi, T6_RS_FEC_NCCW_LO,
2304 	    T6_RS_FEC_NCCW_HI);
2305 	fec->rs_sym0_corr.value.ui64 += read_fec_pair(pi, T6_RS_FEC_SYMERR0_LO,
2306 	    T6_RS_FEC_SYMERR0_HI);
2307 	fec->rs_sym1_corr.value.ui64 += read_fec_pair(pi, T6_RS_FEC_SYMERR1_LO,
2308 	    T6_RS_FEC_SYMERR1_HI);
2309 	fec->rs_sym2_corr.value.ui64 += read_fec_pair(pi, T6_RS_FEC_SYMERR2_LO,
2310 	    T6_RS_FEC_SYMERR2_HI);
2311 	fec->rs_sym3_corr.value.ui64 += read_fec_pair(pi, T6_RS_FEC_SYMERR3_LO,
2312 	    T6_RS_FEC_SYMERR3_HI);
2313 
2314 	/*
2315 	 * Now go through and try to grab Firecode/BASE-R stats.
2316 	 */
2317 	fec->fc_lane0_corr.value.ui64 += read_fec_pair(pi, T6_FC_FEC_L0_CERR_LO,
2318 	    T6_FC_FEC_L0_CERR_HI);
2319 	fec->fc_lane0_uncorr.value.ui64 += read_fec_pair(pi,
2320 	    T6_FC_FEC_L0_NCERR_LO, T6_FC_FEC_L0_NCERR_HI);
2321 	fec->fc_lane1_corr.value.ui64 += read_fec_pair(pi, T6_FC_FEC_L1_CERR_LO,
2322 	    T6_FC_FEC_L1_CERR_HI);
2323 	fec->fc_lane1_uncorr.value.ui64 += read_fec_pair(pi,
2324 	    T6_FC_FEC_L1_NCERR_LO, T6_FC_FEC_L1_NCERR_HI);
2325 	fec->fc_lane2_corr.value.ui64 += read_fec_pair(pi, T6_FC_FEC_L2_CERR_LO,
2326 	    T6_FC_FEC_L2_CERR_HI);
2327 	fec->fc_lane2_uncorr.value.ui64 += read_fec_pair(pi,
2328 	    T6_FC_FEC_L2_NCERR_LO, T6_FC_FEC_L2_NCERR_HI);
2329 	fec->fc_lane3_corr.value.ui64 += read_fec_pair(pi, T6_FC_FEC_L3_CERR_LO,
2330 	    T6_FC_FEC_L3_CERR_HI);
2331 	fec->fc_lane3_uncorr.value.ui64 += read_fec_pair(pi,
2332 	    T6_FC_FEC_L3_NCERR_LO, T6_FC_FEC_L3_NCERR_HI);
2333 
2334 	return (0);
2335 }
2336 
2337 static kstat_t *
setup_port_fec_kstats(struct port_info * pi)2338 setup_port_fec_kstats(struct port_info *pi)
2339 {
2340 	kstat_t *ksp;
2341 	struct cxgbe_port_fec_kstats *kstatp;
2342 
2343 	if (!t4_cver_ge(pi->adapter, CHELSIO_T6)) {
2344 		return (NULL);
2345 	}
2346 
2347 	ksp = kstat_create(T4_PORT_NAME, ddi_get_instance(pi->dip), "fec",
2348 	    "net", KSTAT_TYPE_NAMED, sizeof (struct cxgbe_port_fec_kstats) /
2349 	    sizeof (kstat_named_t), 0);
2350 	if (ksp == NULL) {
2351 		cxgb_printf(pi->dip, CE_WARN, "failed to initialize fec "
2352 		    "kstats.");
2353 		return (NULL);
2354 	}
2355 
2356 	kstatp = ksp->ks_data;
2357 	KS_U64INIT(rs_corr);
2358 	KS_U64INIT(rs_uncorr);
2359 	KS_U64INIT(rs_sym0_corr);
2360 	KS_U64INIT(rs_sym1_corr);
2361 	KS_U64INIT(rs_sym2_corr);
2362 	KS_U64INIT(rs_sym3_corr);
2363 	KS_U64INIT(fc_lane0_corr);
2364 	KS_U64INIT(fc_lane0_uncorr);
2365 	KS_U64INIT(fc_lane1_corr);
2366 	KS_U64INIT(fc_lane1_uncorr);
2367 	KS_U64INIT(fc_lane2_corr);
2368 	KS_U64INIT(fc_lane2_uncorr);
2369 	KS_U64INIT(fc_lane3_corr);
2370 	KS_U64INIT(fc_lane3_uncorr);
2371 
2372 	ksp->ks_update = update_port_fec_kstats;
2373 	ksp->ks_private = pi;
2374 	kstat_install(ksp);
2375 
2376 	return (ksp);
2377 }
2378 
2379 int
t4_port_full_init(struct port_info * pi)2380 t4_port_full_init(struct port_info *pi)
2381 {
2382 	struct adapter *sc = pi->adapter;
2383 	uint16_t *rss;
2384 	struct sge_rxq *rxq;
2385 	int rc, i;
2386 
2387 	ASSERT((pi->flags & TPF_INIT_DONE) == 0);
2388 
2389 	/*
2390 	 * Allocate tx/rx/fl queues for this port.
2391 	 */
2392 	rc = t4_setup_port_queues(pi);
2393 	if (rc != 0)
2394 		goto done;	/* error message displayed already */
2395 
2396 	/*
2397 	 * Setup RSS for this port.
2398 	 */
2399 	rss = kmem_zalloc(pi->nrxq * sizeof (*rss), KM_SLEEP);
2400 	for_each_rxq(pi, i, rxq) {
2401 		rss[i] = rxq->iq.abs_id;
2402 	}
2403 	rc = -t4_config_rss_range(sc, sc->mbox, pi->viid, 0,
2404 	    pi->rss_size, rss, pi->nrxq);
2405 	kmem_free(rss, pi->nrxq * sizeof (*rss));
2406 	if (rc != 0) {
2407 		cxgb_printf(pi->dip, CE_WARN, "rss_config failed: %d", rc);
2408 		goto done;
2409 	}
2410 
2411 	/*
2412 	 * Initialize our per-port FEC kstats.
2413 	 */
2414 	pi->ksp_fec = setup_port_fec_kstats(pi);
2415 
2416 	pi->flags |= TPF_INIT_DONE;
2417 done:
2418 	if (rc != 0)
2419 		(void) t4_port_full_uninit(pi);
2420 
2421 	return (rc);
2422 }
2423 
2424 /*
2425  * Idempotent.
2426  */
2427 static int
t4_port_full_uninit(struct port_info * pi)2428 t4_port_full_uninit(struct port_info *pi)
2429 {
2430 
2431 	ASSERT(pi->flags & TPF_INIT_DONE);
2432 
2433 	if (pi->ksp_fec != NULL) {
2434 		kstat_delete(pi->ksp_fec);
2435 		pi->ksp_fec = NULL;
2436 	}
2437 	(void) t4_teardown_port_queues(pi);
2438 	pi->flags &= ~TPF_INIT_DONE;
2439 
2440 	return (0);
2441 }
2442 
2443 void
t4_port_queues_enable(struct port_info * pi)2444 t4_port_queues_enable(struct port_info *pi)
2445 {
2446 	ASSERT(pi->flags & TPF_INIT_DONE);
2447 
2448 	/*
2449 	 * TODO: whatever was queued up after we set iq->state to IQS_DISABLED
2450 	 * back in t4_port_queues_disable will be processed now, after an
2451 	 * unbounded delay.  This can't be good.
2452 	 */
2453 
2454 	int i;
2455 	struct adapter *sc = pi->adapter;
2456 	struct sge_rxq *rxq;
2457 
2458 	mutex_enter(&sc->sfl_lock);
2459 	for_each_rxq(pi, i, rxq) {
2460 		struct sge_iq *iq = &rxq->iq;
2461 
2462 		if (atomic_cas_uint(&iq->state, IQS_DISABLED, IQS_IDLE) !=
2463 		    IQS_DISABLED)
2464 			panic("%s: iq %p wasn't disabled", __func__,
2465 			    (void *) iq);
2466 
2467 		/*
2468 		 * Freelists which were marked "doomed" by a previous
2469 		 * t4_port_queues_disable() call should clear that status.
2470 		 */
2471 		rxq->fl.flags &= ~FL_DOOMED;
2472 
2473 		t4_iq_gts_update(iq, iq->intr_params, 0);
2474 
2475 	}
2476 	mutex_exit(&sc->sfl_lock);
2477 }
2478 
2479 void
t4_port_queues_disable(struct port_info * pi)2480 t4_port_queues_disable(struct port_info *pi)
2481 {
2482 	int i;
2483 	struct adapter *sc = pi->adapter;
2484 	struct sge_rxq *rxq;
2485 
2486 	ASSERT(pi->flags & TPF_INIT_DONE);
2487 
2488 	/*
2489 	 * TODO: need proper implementation for all tx queues (ctrl, eth, ofld).
2490 	 */
2491 
2492 	for_each_rxq(pi, i, rxq) {
2493 		while (atomic_cas_uint(&rxq->iq.state, IQS_IDLE,
2494 		    IQS_DISABLED) != IQS_IDLE)
2495 			msleep(1);
2496 	}
2497 
2498 	mutex_enter(&sc->sfl_lock);
2499 	for_each_rxq(pi, i, rxq) {
2500 		rxq->fl.flags |= FL_DOOMED;
2501 	}
2502 	mutex_exit(&sc->sfl_lock);
2503 	/* TODO: need to wait for all fl's to be removed from sc->sfl */
2504 }
2505 
2506 void
t4_fatal_err(struct adapter * sc)2507 t4_fatal_err(struct adapter *sc)
2508 {
2509 	t4_set_reg_field(sc, A_SGE_CONTROL, F_GLOBALENABLE, 0);
2510 	t4_intr_disable(sc);
2511 	cxgb_printf(sc->dip, CE_WARN,
2512 	    "encountered fatal error, adapter stopped.");
2513 }
2514 
2515 int
t4_os_find_pci_capability(struct adapter * sc,uint8_t cap)2516 t4_os_find_pci_capability(struct adapter *sc, uint8_t cap)
2517 {
2518 	const uint16_t stat = pci_config_get16(sc->pci_regh, PCI_CONF_STAT);
2519 	if ((stat & PCI_STAT_CAP) == 0) {
2520 		return (0);
2521 	}
2522 
2523 	uint8_t cap_ptr = pci_config_get8(sc->pci_regh, PCI_CONF_CAP_PTR);
2524 	while (cap_ptr) {
2525 		uint8_t cap_id =
2526 		    pci_config_get8(sc->pci_regh, cap_ptr + PCI_CAP_ID);
2527 		if (cap_id == cap) {
2528 			return (cap_ptr);
2529 		}
2530 		cap_ptr =
2531 		    pci_config_get8(sc->pci_regh, cap_ptr + PCI_CAP_NEXT_PTR);
2532 	}
2533 
2534 	return (0);
2535 }
2536 
2537 void
t4_os_portmod_changed(struct adapter * sc,int idx)2538 t4_os_portmod_changed(struct adapter *sc, int idx)
2539 {
2540 	static const char *mod_str[] = {
2541 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM"
2542 	};
2543 	struct port_info *pi = sc->port[idx];
2544 
2545 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
2546 		cxgb_printf(pi->dip, CE_NOTE, "transceiver unplugged.");
2547 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
2548 		cxgb_printf(pi->dip, CE_NOTE,
2549 		    "unknown transceiver inserted.\n");
2550 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
2551 		cxgb_printf(pi->dip, CE_NOTE,
2552 		    "unsupported transceiver inserted.\n");
2553 	else if (pi->mod_type > 0 && pi->mod_type < ARRAY_SIZE(mod_str))
2554 		cxgb_printf(pi->dip, CE_NOTE, "%s transceiver inserted.\n",
2555 		    mod_str[pi->mod_type]);
2556 	else
2557 		cxgb_printf(pi->dip, CE_NOTE, "transceiver (type %d) inserted.",
2558 		    pi->mod_type);
2559 
2560 	if ((pi->flags & TPF_OPEN) != 0 && pi->link_cfg.new_module) {
2561 		pi->link_cfg.redo_l1cfg = true;
2562 	}
2563 }
2564 
2565 void
t4_os_set_hw_addr(struct adapter * sc,int idx,const uint8_t * hw_addr)2566 t4_os_set_hw_addr(struct adapter *sc, int idx, const uint8_t *hw_addr)
2567 {
2568 	bcopy(hw_addr, sc->port[idx]->hw_addr, ETHERADDRL);
2569 }
2570 
2571 uint32_t
t4_read_reg(struct adapter * sc,uint32_t reg)2572 t4_read_reg(struct adapter *sc, uint32_t reg)
2573 {
2574 	const uint32_t val = ddi_get32(sc->regh, (uint32_t *)(sc->regp + reg));
2575 	DTRACE_PROBE3(t4__reg__read, struct adapter *, sc, uint32_t, reg,
2576 	    uint64_t, val);
2577 	return (val);
2578 }
2579 
2580 void
t4_write_reg(struct adapter * sc,uint32_t reg,uint32_t val)2581 t4_write_reg(struct adapter *sc, uint32_t reg, uint32_t val)
2582 {
2583 	DTRACE_PROBE3(t4__reg__write, struct adapter *, sc, uint32_t, reg,
2584 	    uint64_t, val);
2585 	ddi_put32(sc->regh, (uint32_t *)(sc->regp + reg), val);
2586 }
2587 
2588 uint64_t
t4_read_reg64(struct adapter * sc,uint32_t reg)2589 t4_read_reg64(struct adapter *sc, uint32_t reg)
2590 {
2591 	const uint64_t val = ddi_get64(sc->regh, (uint64_t *)(sc->regp + reg));
2592 	DTRACE_PROBE3(t4__reg__read, struct adapter *, sc, uint32_t, reg,
2593 	    uint64_t, val);
2594 	return (val);
2595 }
2596 
2597 void
t4_write_reg64(struct adapter * sc,uint32_t reg,uint64_t val)2598 t4_write_reg64(struct adapter *sc, uint32_t reg, uint64_t val)
2599 {
2600 	DTRACE_PROBE3(t4__reg__write, struct adapter *, sc, uint32_t, reg,
2601 	    uint64_t, val);
2602 	ddi_put64(sc->regh, (uint64_t *)(sc->regp + reg), val);
2603 }
2604 
2605 static int
t4_sensor_read(struct adapter * sc,uint32_t diag,uint32_t * valp)2606 t4_sensor_read(struct adapter *sc, uint32_t diag, uint32_t *valp)
2607 {
2608 	int rc;
2609 	uint32_t param, val;
2610 
2611 	ADAPTER_LOCK(sc);
2612 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
2613 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
2614 	    V_FW_PARAMS_PARAM_Y(diag);
2615 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
2616 	ADAPTER_UNLOCK(sc);
2617 
2618 	if (rc != 0) {
2619 		return (rc);
2620 	} else if (val == 0) {
2621 		return (EIO);
2622 	}
2623 
2624 	*valp = val;
2625 	return (0);
2626 }
2627 
2628 static int
t4_temperature_read(void * arg,sensor_ioctl_scalar_t * scalar)2629 t4_temperature_read(void *arg, sensor_ioctl_scalar_t *scalar)
2630 {
2631 	int ret;
2632 	struct adapter *sc = arg;
2633 	uint32_t val;
2634 
2635 	ret = t4_sensor_read(sc, FW_PARAM_DEV_DIAG_TMP, &val);
2636 	if (ret != 0) {
2637 		return (ret);
2638 	}
2639 
2640 	/*
2641 	 * The device measures temperature in units of 1 degree Celsius. We
2642 	 * don't know its precision.
2643 	 */
2644 	scalar->sis_unit = SENSOR_UNIT_CELSIUS;
2645 	scalar->sis_gran = 1;
2646 	scalar->sis_prec = 0;
2647 	scalar->sis_value = val;
2648 
2649 	return (0);
2650 }
2651 
2652 static int
t4_voltage_read(void * arg,sensor_ioctl_scalar_t * scalar)2653 t4_voltage_read(void *arg, sensor_ioctl_scalar_t *scalar)
2654 {
2655 	int ret;
2656 	struct adapter *sc = arg;
2657 	uint32_t val;
2658 
2659 	ret = t4_sensor_read(sc, FW_PARAM_DEV_DIAG_VDD, &val);
2660 	if (ret != 0) {
2661 		return (ret);
2662 	}
2663 
2664 	scalar->sis_unit = SENSOR_UNIT_VOLTS;
2665 	scalar->sis_gran = 1000;
2666 	scalar->sis_prec = 0;
2667 	scalar->sis_value = val;
2668 
2669 	return (0);
2670 }
2671 
2672 /*
2673  * While the hardware supports the ability to read and write the flash image,
2674  * this is not currently wired up.
2675  */
2676 static int
t4_ufm_getcaps(ddi_ufm_handle_t * ufmh,void * arg,ddi_ufm_cap_t * caps)2677 t4_ufm_getcaps(ddi_ufm_handle_t *ufmh, void *arg, ddi_ufm_cap_t *caps)
2678 {
2679 	*caps = DDI_UFM_CAP_REPORT;
2680 	return (0);
2681 }
2682 
2683 static int
t4_ufm_fill_image(ddi_ufm_handle_t * ufmh,void * arg,uint_t imgno,ddi_ufm_image_t * imgp)2684 t4_ufm_fill_image(ddi_ufm_handle_t *ufmh, void *arg, uint_t imgno,
2685     ddi_ufm_image_t *imgp)
2686 {
2687 	if (imgno != 0) {
2688 		return (EINVAL);
2689 	}
2690 
2691 	ddi_ufm_image_set_desc(imgp, "Firmware");
2692 	ddi_ufm_image_set_nslots(imgp, 1);
2693 
2694 	return (0);
2695 }
2696 
2697 static int
t4_ufm_fill_slot_version(nvlist_t * nvl,const char * key,uint32_t vers)2698 t4_ufm_fill_slot_version(nvlist_t *nvl, const char *key, uint32_t vers)
2699 {
2700 	char buf[128];
2701 
2702 	if (vers == 0) {
2703 		return (0);
2704 	}
2705 
2706 	if (snprintf(buf, sizeof (buf), "%u.%u.%u.%u",
2707 	    G_FW_HDR_FW_VER_MAJOR(vers), G_FW_HDR_FW_VER_MINOR(vers),
2708 	    G_FW_HDR_FW_VER_MICRO(vers), G_FW_HDR_FW_VER_BUILD(vers)) >=
2709 	    sizeof (buf)) {
2710 		return (EOVERFLOW);
2711 	}
2712 
2713 	return (nvlist_add_string(nvl, key, buf));
2714 }
2715 
2716 static int
t4_ufm_fill_slot(ddi_ufm_handle_t * ufmh,void * arg,uint_t imgno,uint_t slotno,ddi_ufm_slot_t * slotp)2717 t4_ufm_fill_slot(ddi_ufm_handle_t *ufmh, void *arg, uint_t imgno, uint_t slotno,
2718     ddi_ufm_slot_t *slotp)
2719 {
2720 	int ret;
2721 	struct adapter *sc = arg;
2722 	nvlist_t *misc = NULL;
2723 	char buf[128];
2724 
2725 	if (imgno != 0 || slotno != 0) {
2726 		return (EINVAL);
2727 	}
2728 
2729 	if (snprintf(buf, sizeof (buf), "%u.%u.%u.%u",
2730 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
2731 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
2732 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
2733 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers)) >= sizeof (buf)) {
2734 		return (EOVERFLOW);
2735 	}
2736 
2737 	ddi_ufm_slot_set_version(slotp, buf);
2738 
2739 	(void) nvlist_alloc(&misc, NV_UNIQUE_NAME, KM_SLEEP);
2740 	if ((ret = t4_ufm_fill_slot_version(misc, "TP Microcode",
2741 	    sc->params.tp_vers)) != 0) {
2742 		goto err;
2743 	}
2744 
2745 	if ((ret = t4_ufm_fill_slot_version(misc, "Bootstrap",
2746 	    sc->params.bs_vers)) != 0) {
2747 		goto err;
2748 	}
2749 
2750 	if ((ret = t4_ufm_fill_slot_version(misc, "Expansion ROM",
2751 	    sc->params.er_vers)) != 0) {
2752 		goto err;
2753 	}
2754 
2755 	if ((ret = nvlist_add_uint32(misc, "Serial Configuration",
2756 	    sc->params.scfg_vers)) != 0) {
2757 		goto err;
2758 	}
2759 
2760 	if ((ret = nvlist_add_uint32(misc, "VPD Version",
2761 	    sc->params.vpd_vers)) != 0) {
2762 		goto err;
2763 	}
2764 
2765 	ddi_ufm_slot_set_misc(slotp, misc);
2766 	ddi_ufm_slot_set_attrs(slotp, DDI_UFM_ATTR_ACTIVE |
2767 	    DDI_UFM_ATTR_WRITEABLE | DDI_UFM_ATTR_READABLE);
2768 	return (0);
2769 
2770 err:
2771 	nvlist_free(misc);
2772 	return (ret);
2773 
2774 }
2775 
2776 int
t4_cxgbe_attach(struct port_info * pi,dev_info_t * dip)2777 t4_cxgbe_attach(struct port_info *pi, dev_info_t *dip)
2778 {
2779 	ASSERT(pi != NULL);
2780 
2781 	mac_register_t *mac = mac_alloc(MAC_VERSION);
2782 	if (mac == NULL) {
2783 		return (DDI_FAILURE);
2784 	}
2785 
2786 	size_t prop_size;
2787 	const char **props = t4_get_priv_props(pi, &prop_size);
2788 
2789 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
2790 	mac->m_driver = pi;
2791 	mac->m_dip = dip;
2792 	mac->m_src_addr = pi->hw_addr;
2793 	mac->m_callbacks = pi->mc;
2794 	mac->m_max_sdu = pi->mtu;
2795 	/* mac_register() treats this as const, so we can cast it away */
2796 	mac->m_priv_props = (char **)props;
2797 	mac->m_margin = VLAN_TAGSZ;
2798 
2799 	if (!mac->m_callbacks->mc_unicst) {
2800 		/* Multiple rings enabled */
2801 		mac->m_v12n = MAC_VIRT_LEVEL1;
2802 	}
2803 
2804 	mac_handle_t mh = NULL;
2805 	const int rc = mac_register(mac, &mh);
2806 	mac_free(mac);
2807 	kmem_free(props, prop_size);
2808 	if (rc != 0) {
2809 		return (DDI_FAILURE);
2810 	}
2811 
2812 	pi->mh = mh;
2813 
2814 	/*
2815 	 * Link state from this point onwards to the time interface is plumbed,
2816 	 * should be set to LINK_STATE_UNKNOWN. The mac should be updated about
2817 	 * the link state as either LINK_STATE_UP or LINK_STATE_DOWN based on
2818 	 * the actual link state detection after interface plumb.
2819 	 */
2820 	mac_link_update(mh, LINK_STATE_UNKNOWN);
2821 
2822 	return (DDI_SUCCESS);
2823 }
2824 
2825 int
t4_cxgbe_detach(struct port_info * pi)2826 t4_cxgbe_detach(struct port_info *pi)
2827 {
2828 	ASSERT(pi != NULL);
2829 	ASSERT(pi->mh != NULL);
2830 
2831 	if (mac_unregister(pi->mh) == 0) {
2832 		pi->mh = NULL;
2833 		return (DDI_SUCCESS);
2834 	}
2835 
2836 	return (DDI_FAILURE);
2837 }
2838 
2839 struct cb_ops t4_cb_ops = {
2840 	.cb_open =		t4_cb_open,
2841 	.cb_close =		t4_cb_close,
2842 	.cb_strategy =		nodev,
2843 	.cb_print =		nodev,
2844 	.cb_dump =		nodev,
2845 	.cb_read =		nodev,
2846 	.cb_write =		nodev,
2847 	.cb_ioctl =		t4_cb_ioctl,
2848 	.cb_devmap =		nodev,
2849 	.cb_mmap =		nodev,
2850 	.cb_segmap =		nodev,
2851 	.cb_chpoll =		nochpoll,
2852 	.cb_prop_op =		ddi_prop_op,
2853 	.cb_flag =		D_MP,
2854 	.cb_rev =		CB_REV,
2855 	.cb_aread =		nodev,
2856 	.cb_awrite =		nodev
2857 };
2858 
2859 struct bus_ops t4_bus_ops = {
2860 	.busops_rev =		BUSO_REV,
2861 	.bus_ctl =		t4_bus_ctl,
2862 	.bus_prop_op =		ddi_bus_prop_op,
2863 	.bus_config =		t4_bus_config,
2864 	.bus_unconfig =		t4_bus_unconfig,
2865 };
2866 
2867 static struct dev_ops t4_dev_ops = {
2868 	.devo_rev =		DEVO_REV,
2869 	.devo_getinfo =		t4_devo_getinfo,
2870 	.devo_identify =	nulldev,
2871 	.devo_probe =		t4_devo_probe,
2872 	.devo_attach =		t4_devo_attach,
2873 	.devo_detach =		t4_devo_detach,
2874 	.devo_reset =		nodev,
2875 	.devo_cb_ops =		&t4_cb_ops,
2876 	.devo_bus_ops =		&t4_bus_ops,
2877 	.devo_quiesce =		&t4_devo_quiesce,
2878 };
2879 
2880 static struct modldrv t4nex_modldrv = {
2881 	.drv_modops =		&mod_driverops,
2882 	.drv_linkinfo =		"Chelsio T4-T6 nexus " DRV_VERSION,
2883 	.drv_dev_ops =		&t4_dev_ops
2884 };
2885 
2886 static struct modlinkage t4nex_modlinkage = {
2887 	.ml_rev =		MODREV_1,
2888 	.ml_linkage =		{&t4nex_modldrv, NULL},
2889 };
2890 
2891 int
_init(void)2892 _init(void)
2893 {
2894 	int rc;
2895 
2896 	rc = ddi_soft_state_init(&t4_soft_state, sizeof (struct adapter), 0);
2897 	if (rc != 0) {
2898 		return (rc);
2899 	}
2900 
2901 	mutex_init(&t4_adapter_list_lock, NULL, MUTEX_DRIVER, NULL);
2902 	list_create(&t4_adapter_list, sizeof (adapter_t),
2903 	    offsetof(adapter_t, node));
2904 	t4_debug_init();
2905 
2906 	rc = mod_install(&t4nex_modlinkage);
2907 	if (rc != 0) {
2908 		ddi_soft_state_fini(&t4_soft_state);
2909 		mutex_destroy(&t4_adapter_list_lock);
2910 		list_destroy(&t4_adapter_list);
2911 		t4_debug_fini();
2912 	}
2913 
2914 	return (rc);
2915 }
2916 
2917 int
_fini(void)2918 _fini(void)
2919 {
2920 	const int rc = mod_remove(&t4nex_modlinkage);
2921 	if (rc != 0) {
2922 		return (rc);
2923 	}
2924 
2925 	mutex_destroy(&t4_adapter_list_lock);
2926 	list_destroy(&t4_adapter_list);
2927 	ddi_soft_state_fini(&t4_soft_state);
2928 	t4_debug_fini();
2929 
2930 	return (0);
2931 }
2932 
2933 int
_info(struct modinfo * mi)2934 _info(struct modinfo *mi)
2935 {
2936 	return (mod_info(&t4nex_modlinkage, mi));
2937 }
2938