xref: /illumos-gate/usr/src/uts/common/io/ib/adapters/hermon/hermon.c (revision dd9e16da4243358c2e9251a4ca5d50f56e0adc68)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * hermon.c
29  *    Hermon (InfiniBand) HCA Driver attach/detach Routines
30  *
31  *    Implements all the routines necessary for the attach, setup,
32  *    initialization (and subsequent possible teardown and detach) of the
33  *    Hermon InfiniBand HCA driver.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/file.h>
38 #include <sys/open.h>
39 #include <sys/conf.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/modctl.h>
43 #include <sys/stat.h>
44 #include <sys/pci.h>
45 #include <sys/pci_cap.h>
46 #include <sys/bitmap.h>
47 #include <sys/policy.h>
48 
49 #include <sys/ib/adapters/hermon/hermon.h>
50 
51 /* The following works around a problem in pre-2_7_000 firmware. */
52 #define	HERMON_FW_WORKAROUND
53 
54 int hermon_verbose = 0;
55 
56 /* Hermon HCA State Pointer */
57 void *hermon_statep;
58 
59 int debug_vpd = 0;
60 
61 /* Disable the internal error-check polling thread */
62 int hermon_no_inter_err_chk = 0;
63 
64 /*
65  * The Hermon "userland resource database" is common to instances of the
66  * Hermon HCA driver.  This structure "hermon_userland_rsrc_db" contains all
67  * the necessary information to maintain it.
68  */
69 hermon_umap_db_t hermon_userland_rsrc_db;
70 
71 static int hermon_attach(dev_info_t *, ddi_attach_cmd_t);
72 static int hermon_detach(dev_info_t *, ddi_detach_cmd_t);
73 static int hermon_open(dev_t *, int, int, cred_t *);
74 static int hermon_close(dev_t, int, int, cred_t *);
75 static int hermon_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
76 
77 static int hermon_drv_init(hermon_state_t *state, dev_info_t *dip,
78     int instance);
79 static void hermon_drv_fini(hermon_state_t *state);
80 static void hermon_drv_fini2(hermon_state_t *state);
81 static int hermon_isr_init(hermon_state_t *state);
82 static void hermon_isr_fini(hermon_state_t *state);
83 
84 static int hermon_hw_init(hermon_state_t *state);
85 
86 static void hermon_hw_fini(hermon_state_t *state,
87     hermon_drv_cleanup_level_t cleanup);
88 static int hermon_soft_state_init(hermon_state_t *state);
89 static void hermon_soft_state_fini(hermon_state_t *state);
90 static int hermon_icm_config_setup(hermon_state_t *state,
91     hermon_hw_initqueryhca_t *inithca);
92 static void hermon_icm_tables_init(hermon_state_t *state);
93 static void hermon_icm_tables_fini(hermon_state_t *state);
94 static int hermon_icm_dma_init(hermon_state_t *state);
95 static void hermon_icm_dma_fini(hermon_state_t *state);
96 static void hermon_inithca_set(hermon_state_t *state,
97     hermon_hw_initqueryhca_t *inithca);
98 static int hermon_hca_port_init(hermon_state_t *state);
99 static int hermon_hca_ports_shutdown(hermon_state_t *state, uint_t num_init);
100 static int hermon_internal_uarpg_init(hermon_state_t *state);
101 static void hermon_internal_uarpg_fini(hermon_state_t *state);
102 static int hermon_special_qp_contexts_reserve(hermon_state_t *state);
103 static void hermon_special_qp_contexts_unreserve(hermon_state_t *state);
104 static int hermon_sw_reset(hermon_state_t *state);
105 static int hermon_mcg_init(hermon_state_t *state);
106 static void hermon_mcg_fini(hermon_state_t *state);
107 static int hermon_fw_version_check(hermon_state_t *state);
108 static void hermon_device_info_report(hermon_state_t *state);
109 static int hermon_pci_capability_list(hermon_state_t *state,
110     ddi_acc_handle_t hdl);
111 static void hermon_pci_capability_vpd(hermon_state_t *state,
112     ddi_acc_handle_t hdl, uint_t offset);
113 static int hermon_pci_read_vpd(ddi_acc_handle_t hdl, uint_t offset,
114     uint32_t addr, uint32_t *data);
115 static int hermon_intr_or_msi_init(hermon_state_t *state);
116 static int hermon_add_intrs(hermon_state_t *state, int intr_type);
117 static int hermon_intr_or_msi_fini(hermon_state_t *state);
118 void hermon_pci_capability_msix(hermon_state_t *state, ddi_acc_handle_t hdl,
119     uint_t offset);
120 
121 static uint64_t hermon_size_icm(hermon_state_t *state);
122 
123 /* X86 fastreboot support */
124 static ushort_t get_msix_ctrl(dev_info_t *);
125 static size_t get_msix_tbl_size(dev_info_t *);
126 static size_t get_msix_pba_size(dev_info_t *);
127 static void hermon_set_msix_info(hermon_state_t *);
128 static int hermon_intr_disable(hermon_state_t *);
129 static int hermon_quiesce(dev_info_t *);
130 
131 
132 /* Character/Block Operations */
133 static struct cb_ops hermon_cb_ops = {
134 	hermon_open,		/* open */
135 	hermon_close,		/* close */
136 	nodev,			/* strategy (block) */
137 	nodev,			/* print (block) */
138 	nodev,			/* dump (block) */
139 	nodev,			/* read */
140 	nodev,			/* write */
141 	hermon_ioctl,		/* ioctl */
142 	hermon_devmap,		/* devmap */
143 	NULL,			/* mmap */
144 	nodev,			/* segmap */
145 	nochpoll,		/* chpoll */
146 	ddi_prop_op,		/* prop_op */
147 	NULL,			/* streams */
148 	D_NEW | D_MP |
149 	D_64BIT | /* D_HOTPLUG | */
150 	D_DEVMAP,		/* flags */
151 	CB_REV			/* rev */
152 };
153 
154 /* Driver Operations */
155 static struct dev_ops hermon_ops = {
156 	DEVO_REV,		/* struct rev */
157 	0,			/* refcnt */
158 	hermon_getinfo,		/* getinfo */
159 	nulldev,		/* identify */
160 	nulldev,		/* probe */
161 	hermon_attach,		/* attach */
162 	hermon_detach,		/* detach */
163 	nodev,			/* reset */
164 	&hermon_cb_ops,		/* cb_ops */
165 	NULL,			/* bus_ops */
166 	nodev,			/* power */
167 	hermon_quiesce,		/* devo_quiesce */
168 };
169 
170 /* Module Driver Info */
171 static struct modldrv hermon_modldrv = {
172 	&mod_driverops,
173 	"ConnectX IB Driver",
174 	&hermon_ops
175 };
176 
177 /* Module Linkage */
178 static struct modlinkage hermon_modlinkage = {
179 	MODREV_1,
180 	&hermon_modldrv,
181 	NULL
182 };
183 
184 /*
185  * This extern refers to the ibc_operations_t function vector that is defined
186  * in the hermon_ci.c file.
187  */
188 extern ibc_operations_t	hermon_ibc_ops;
189 
190 /*
191  * _init()
192  */
193 int
194 _init()
195 {
196 	int	status;
197 
198 	status = ddi_soft_state_init(&hermon_statep, sizeof (hermon_state_t),
199 	    (size_t)HERMON_INITIAL_STATES);
200 	if (status != 0) {
201 		return (status);
202 	}
203 
204 	status = ibc_init(&hermon_modlinkage);
205 	if (status != 0) {
206 		ddi_soft_state_fini(&hermon_statep);
207 		return (status);
208 	}
209 
210 	status = mod_install(&hermon_modlinkage);
211 	if (status != 0) {
212 		ibc_fini(&hermon_modlinkage);
213 		ddi_soft_state_fini(&hermon_statep);
214 		return (status);
215 	}
216 
217 	/* Initialize the Hermon "userland resources database" */
218 	hermon_umap_db_init();
219 
220 	return (status);
221 }
222 
223 
224 /*
225  * _info()
226  */
227 int
228 _info(struct modinfo *modinfop)
229 {
230 	int	status;
231 
232 	status = mod_info(&hermon_modlinkage, modinfop);
233 	return (status);
234 }
235 
236 
237 /*
238  * _fini()
239  */
240 int
241 _fini()
242 {
243 	int	status;
244 
245 	status = mod_remove(&hermon_modlinkage);
246 	if (status != 0) {
247 		return (status);
248 	}
249 
250 	/* Destroy the Hermon "userland resources database" */
251 	hermon_umap_db_fini();
252 
253 	ibc_fini(&hermon_modlinkage);
254 	ddi_soft_state_fini(&hermon_statep);
255 
256 	return (status);
257 }
258 
259 
260 /*
261  * hermon_getinfo()
262  */
263 /* ARGSUSED */
264 static int
265 hermon_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
266 {
267 	dev_t		dev;
268 	hermon_state_t 	*state;
269 	minor_t		instance;
270 
271 	switch (cmd) {
272 	case DDI_INFO_DEVT2DEVINFO:
273 		dev = (dev_t)arg;
274 		instance = HERMON_DEV_INSTANCE(dev);
275 		state = ddi_get_soft_state(hermon_statep, instance);
276 		if (state == NULL) {
277 			return (DDI_FAILURE);
278 		}
279 		*result = (void *)state->hs_dip;
280 		return (DDI_SUCCESS);
281 
282 	case DDI_INFO_DEVT2INSTANCE:
283 		dev = (dev_t)arg;
284 		instance = HERMON_DEV_INSTANCE(dev);
285 		*result = (void *)(uintptr_t)instance;
286 		return (DDI_SUCCESS);
287 
288 	default:
289 		break;
290 	}
291 
292 	return (DDI_FAILURE);
293 }
294 
295 
296 /*
297  * hermon_open()
298  */
299 /* ARGSUSED */
300 static int
301 hermon_open(dev_t *devp, int flag, int otyp, cred_t *credp)
302 {
303 	hermon_state_t		*state;
304 	hermon_rsrc_t 		*rsrcp;
305 	hermon_umap_db_entry_t	*umapdb, *umapdb2;
306 	minor_t			instance;
307 	uint64_t		key, value;
308 	uint_t			hr_indx;
309 	dev_t			dev;
310 	int			status;
311 
312 	instance = HERMON_DEV_INSTANCE(*devp);
313 	state = ddi_get_soft_state(hermon_statep, instance);
314 	if (state == NULL) {
315 		return (ENXIO);
316 	}
317 
318 	/*
319 	 * Only allow driver to be opened for character access, and verify
320 	 * whether exclusive access is allowed.
321 	 */
322 	if ((otyp != OTYP_CHR) || ((flag & FEXCL) &&
323 	    secpolicy_excl_open(credp) != 0)) {
324 		return (EINVAL);
325 	}
326 
327 	/*
328 	 * Search for the current process PID in the "userland resources
329 	 * database".  If it is not found, then attempt to allocate a UAR
330 	 * page and add the ("key", "value") pair to the database.
331 	 * Note:  As a last step we always return a devp appropriate for
332 	 * the open.  Either we return a new minor number (based on the
333 	 * instance and the UAR page index) or we return the current minor
334 	 * number for the given client process.
335 	 *
336 	 * We also add an entry to the database to allow for lookup from
337 	 * "dev_t" to the current process PID.  This is necessary because,
338 	 * under certain circumstance, the process PID that calls the Hermon
339 	 * close() entry point may not be the same as the one who called
340 	 * open().  Specifically, this can happen if a child process calls
341 	 * the Hermon's open() entry point, gets a UAR page, maps it out (using
342 	 * mmap()), and then exits without calling munmap().  Because mmap()
343 	 * adds a reference to the file descriptor, at the exit of the child
344 	 * process the file descriptor is "inherited" by the parent (and will
345 	 * be close()'d by the parent's PID only when it exits).
346 	 *
347 	 * Note: We use the hermon_umap_db_find_nolock() and
348 	 * hermon_umap_db_add_nolock() database access routines below (with
349 	 * an explicit mutex_enter of the database lock - "hdl_umapdb_lock")
350 	 * to ensure that the multiple accesses (in this case searching for,
351 	 * and then adding _two_ database entries) can be done atomically.
352 	 */
353 	key = ddi_get_pid();
354 	mutex_enter(&hermon_userland_rsrc_db.hdl_umapdb_lock);
355 	status = hermon_umap_db_find_nolock(instance, key,
356 	    MLNX_UMAP_UARPG_RSRC, &value, 0, NULL);
357 	if (status != DDI_SUCCESS) {
358 		/*
359 		 * If we are in 'maintenance mode', we cannot alloc a UAR page.
360 		 * But we still need some rsrcp value, and a mostly unique
361 		 * hr_indx value.  So we set rsrcp to NULL for maintenance
362 		 * mode, and use a rolling count for hr_indx.  The field
363 		 * 'hs_open_hr_indx' is used only in this maintenance mode
364 		 * condition.
365 		 *
366 		 * Otherwise, if we are in operational mode then we allocate
367 		 * the UAR page as normal, and use the rsrcp value and tr_indx
368 		 * value from that allocation.
369 		 */
370 		if (!HERMON_IS_OPERATIONAL(state->hs_operational_mode)) {
371 			rsrcp = NULL;
372 			hr_indx = state->hs_open_ar_indx++;
373 		} else {
374 			/* Allocate a new UAR page for this process */
375 			status = hermon_rsrc_alloc(state, HERMON_UARPG, 1,
376 			    HERMON_NOSLEEP, &rsrcp);
377 			if (status != DDI_SUCCESS) {
378 				mutex_exit(
379 				    &hermon_userland_rsrc_db.hdl_umapdb_lock);
380 				return (EAGAIN);
381 			}
382 
383 			hr_indx = rsrcp->hr_indx;
384 		}
385 
386 		/*
387 		 * Allocate an entry to track the UAR page resource in the
388 		 * "userland resources database".
389 		 */
390 		umapdb = hermon_umap_db_alloc(instance, key,
391 		    MLNX_UMAP_UARPG_RSRC, (uint64_t)(uintptr_t)rsrcp);
392 		if (umapdb == NULL) {
393 			mutex_exit(&hermon_userland_rsrc_db.hdl_umapdb_lock);
394 			/* If in "maintenance mode", don't free the rsrc */
395 			if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) {
396 				hermon_rsrc_free(state, &rsrcp);
397 			}
398 			return (EAGAIN);
399 		}
400 
401 		/*
402 		 * Create a new device number.  Minor number is a function of
403 		 * the UAR page index (15 bits) and the device instance number
404 		 * (3 bits).
405 		 */
406 		dev = makedevice(getmajor(*devp), (hr_indx <<
407 		    HERMON_MINORNUM_SHIFT) | instance);
408 
409 		/*
410 		 * Allocate another entry in the "userland resources database"
411 		 * to track the association of the device number (above) to
412 		 * the current process ID (in "key").
413 		 */
414 		umapdb2 = hermon_umap_db_alloc(instance, dev,
415 		    MLNX_UMAP_PID_RSRC, (uint64_t)key);
416 		if (umapdb2 == NULL) {
417 			mutex_exit(&hermon_userland_rsrc_db.hdl_umapdb_lock);
418 			hermon_umap_db_free(umapdb);
419 			/* If in "maintenance mode", don't free the rsrc */
420 			if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) {
421 				hermon_rsrc_free(state, &rsrcp);
422 			}
423 			return (EAGAIN);
424 		}
425 
426 		/* Add the entries to the database */
427 		hermon_umap_db_add_nolock(umapdb);
428 		hermon_umap_db_add_nolock(umapdb2);
429 
430 	} else {
431 		/*
432 		 * Return the same device number as on the original open()
433 		 * call.  This was calculated as a function of the UAR page
434 		 * index (top 16 bits) and the device instance number
435 		 */
436 		rsrcp = (hermon_rsrc_t *)(uintptr_t)value;
437 		dev = makedevice(getmajor(*devp), (rsrcp->hr_indx <<
438 		    HERMON_MINORNUM_SHIFT) | instance);
439 	}
440 	mutex_exit(&hermon_userland_rsrc_db.hdl_umapdb_lock);
441 
442 	*devp = dev;
443 
444 	return (0);
445 }
446 
447 
448 /*
449  * hermon_close()
450  */
451 /* ARGSUSED */
452 static int
453 hermon_close(dev_t dev, int flag, int otyp, cred_t *credp)
454 {
455 	hermon_state_t		*state;
456 	hermon_rsrc_t		*rsrcp;
457 	hermon_umap_db_entry_t	*umapdb;
458 	hermon_umap_db_priv_t	*priv;
459 	minor_t			instance;
460 	uint64_t		key, value;
461 	int			status, reset_status = 0;
462 
463 	instance = HERMON_DEV_INSTANCE(dev);
464 	state = ddi_get_soft_state(hermon_statep, instance);
465 	if (state == NULL) {
466 		return (ENXIO);
467 	}
468 
469 	/*
470 	 * Search for "dev_t" in the "userland resources database".  As
471 	 * explained above in hermon_open(), we can't depend on using the
472 	 * current process ID here to do the lookup because the process
473 	 * that ultimately closes may not be the same one who opened
474 	 * (because of inheritance).
475 	 * So we lookup the "dev_t" (which points to the PID of the process
476 	 * that opened), and we remove the entry from the database (and free
477 	 * it up).  Then we do another query based on the PID value.  And when
478 	 * we find that database entry, we free it up too and then free the
479 	 * Hermon UAR page resource.
480 	 *
481 	 * Note: We use the hermon_umap_db_find_nolock() database access
482 	 * routine below (with an explicit mutex_enter of the database lock)
483 	 * to ensure that the multiple accesses (which attempt to remove the
484 	 * two database entries) can be done atomically.
485 	 *
486 	 * This works the same in both maintenance mode and HCA mode, except
487 	 * for the call to hermon_rsrc_free().  In the case of maintenance mode,
488 	 * this call is not needed, as it was not allocated in hermon_open()
489 	 * above.
490 	 */
491 	key = dev;
492 	mutex_enter(&hermon_userland_rsrc_db.hdl_umapdb_lock);
493 	status = hermon_umap_db_find_nolock(instance, key, MLNX_UMAP_PID_RSRC,
494 	    &value, HERMON_UMAP_DB_REMOVE, &umapdb);
495 	if (status == DDI_SUCCESS) {
496 		/*
497 		 * If the "hdb_priv" field is non-NULL, it indicates that
498 		 * some "on close" handling is still necessary.  Call
499 		 * hermon_umap_db_handle_onclose_cb() to do the handling (i.e.
500 		 * to invoke all the registered callbacks).  Then free up
501 		 * the resources associated with "hdb_priv" and continue
502 		 * closing.
503 		 */
504 		priv = (hermon_umap_db_priv_t *)umapdb->hdbe_common.hdb_priv;
505 		if (priv != NULL) {
506 			reset_status = hermon_umap_db_handle_onclose_cb(priv);
507 			kmem_free(priv, sizeof (hermon_umap_db_priv_t));
508 			umapdb->hdbe_common.hdb_priv = (void *)NULL;
509 		}
510 
511 		hermon_umap_db_free(umapdb);
512 
513 		/*
514 		 * Now do another lookup using PID as the key (copy it from
515 		 * "value").  When this lookup is complete, the "value" field
516 		 * will contain the hermon_rsrc_t pointer for the UAR page
517 		 * resource.
518 		 */
519 		key = value;
520 		status = hermon_umap_db_find_nolock(instance, key,
521 		    MLNX_UMAP_UARPG_RSRC, &value, HERMON_UMAP_DB_REMOVE,
522 		    &umapdb);
523 		if (status == DDI_SUCCESS) {
524 			hermon_umap_db_free(umapdb);
525 			/* If in "maintenance mode", don't free the rsrc */
526 			if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) {
527 				rsrcp = (hermon_rsrc_t *)(uintptr_t)value;
528 				hermon_rsrc_free(state, &rsrcp);
529 			}
530 		}
531 	}
532 	mutex_exit(&hermon_userland_rsrc_db.hdl_umapdb_lock);
533 	return (reset_status);
534 }
535 
536 
537 /*
538  * hermon_attach()
539  *    Context: Only called from attach() path context
540  */
541 static int
542 hermon_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
543 {
544 	hermon_state_t	*state;
545 	ibc_clnt_hdl_t	tmp_ibtfpriv;
546 	ibc_status_t	ibc_status;
547 	int		instance;
548 	int		status;
549 
550 #ifdef __lock_lint
551 	(void) hermon_quiesce(dip);
552 #endif
553 
554 	switch (cmd) {
555 	case DDI_ATTACH:
556 		instance = ddi_get_instance(dip);
557 		status = ddi_soft_state_zalloc(hermon_statep, instance);
558 		if (status != DDI_SUCCESS) {
559 			cmn_err(CE_NOTE, "hermon%d: driver failed to attach: "
560 			    "attach_ssz_fail", instance);
561 			goto fail_attach_nomsg;
562 
563 		}
564 		state = ddi_get_soft_state(hermon_statep, instance);
565 		if (state == NULL) {
566 			ddi_soft_state_free(hermon_statep, instance);
567 			cmn_err(CE_NOTE, "hermon%d: driver failed to attach: "
568 			    "attach_gss_fail", instance);
569 			goto fail_attach_nomsg;
570 		}
571 
572 		/* clear the attach error buffer */
573 		HERMON_ATTACH_MSG_INIT(state->hs_attach_buf);
574 
575 		/* Save away devinfo and instance before hermon_fm_init() */
576 		state->hs_dip = dip;
577 		state->hs_instance = instance;
578 
579 		hermon_fm_init(state);
580 
581 		/*
582 		 * Initialize Hermon driver and hardware.
583 		 *
584 		 * Note: If this initialization fails we may still wish to
585 		 * create a device node and remain operational so that Hermon
586 		 * firmware can be updated/flashed (i.e. "maintenance mode").
587 		 * If this is the case, then "hs_operational_mode" will be
588 		 * equal to HERMON_MAINTENANCE_MODE.  We will not attempt to
589 		 * attach to the IBTF or register with the IBMF (i.e. no
590 		 * InfiniBand interfaces will be enabled).
591 		 */
592 		status = hermon_drv_init(state, dip, instance);
593 		if ((status != DDI_SUCCESS) &&
594 		    (HERMON_IS_OPERATIONAL(state->hs_operational_mode))) {
595 			goto fail_attach;
596 		}
597 
598 		/*
599 		 * Change the Hermon FM mode
600 		 */
601 		if ((hermon_get_state(state) & HCA_PIO_FM) &&
602 		    HERMON_IS_OPERATIONAL(state->hs_operational_mode)) {
603 			/*
604 			 * Now we wait for 50ms to give an opportunity
605 			 * to Solaris FMA so that HW errors can be notified.
606 			 * Then check if there are HW errors or not. If
607 			 * a HW error is detected, the Hermon attachment
608 			 * must be failed.
609 			 */
610 			delay(drv_usectohz(50000));
611 			if (hermon_init_failure(state)) {
612 				hermon_drv_fini(state);
613 				HERMON_WARNING(state, "unable to "
614 				    "attach Hermon due to a HW error");
615 				HERMON_ATTACH_MSG(state->hs_attach_buf,
616 				    "hermon_attach_failure");
617 				goto fail_attach;
618 			}
619 
620 			/*
621 			 * There seems no HW errors during the attachment,
622 			 * so let's change the Hermon FM state to the
623 			 * ereport only mode.
624 			 */
625 			if (hermon_fm_ereport_init(state) != DDI_SUCCESS) {
626 				/* unwind the resources */
627 				hermon_drv_fini(state);
628 				HERMON_ATTACH_MSG(state->hs_attach_buf,
629 				    "hermon_attach_failure");
630 				goto fail_attach;
631 			}
632 		}
633 
634 		/* Create the minor node for device */
635 		status = ddi_create_minor_node(dip, "devctl", S_IFCHR, instance,
636 		    DDI_PSEUDO, 0);
637 		if (status != DDI_SUCCESS) {
638 			hermon_drv_fini(state);
639 			HERMON_ATTACH_MSG(state->hs_attach_buf,
640 			    "attach_create_mn_fail");
641 			goto fail_attach;
642 		}
643 
644 		/*
645 		 * If we are in "maintenance mode", then we don't want to
646 		 * register with the IBTF.  All InfiniBand interfaces are
647 		 * uninitialized, and the device is only capable of handling
648 		 * requests to update/flash firmware (or test/debug requests).
649 		 */
650 		if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) {
651 			cmn_err(CE_NOTE, "!Hermon is operational\n");
652 
653 			/* Attach to InfiniBand Transport Framework (IBTF) */
654 			ibc_status = ibc_attach(&tmp_ibtfpriv,
655 			    &state->hs_ibtfinfo);
656 			if (ibc_status != IBC_SUCCESS) {
657 				cmn_err(CE_CONT, "hermon_attach: ibc_attach "
658 				    "failed\n");
659 				ddi_remove_minor_node(dip, "devctl");
660 				hermon_drv_fini(state);
661 				HERMON_ATTACH_MSG(state->hs_attach_buf,
662 				    "attach_ibcattach_fail");
663 				goto fail_attach;
664 			}
665 
666 			/*
667 			 * Now that we've successfully attached to the IBTF,
668 			 * we enable all appropriate asynch and CQ events to
669 			 * be forwarded to the IBTF.
670 			 */
671 			HERMON_ENABLE_IBTF_CALLB(state, tmp_ibtfpriv);
672 
673 			ibc_post_attach(state->hs_ibtfpriv);
674 
675 			/* Register agents with IB Mgmt Framework (IBMF) */
676 			status = hermon_agent_handlers_init(state);
677 			if (status != DDI_SUCCESS) {
678 				(void) ibc_pre_detach(tmp_ibtfpriv, DDI_DETACH);
679 				HERMON_QUIESCE_IBTF_CALLB(state);
680 				if (state->hs_in_evcallb != 0) {
681 					HERMON_WARNING(state, "unable to "
682 					    "quiesce Hermon IBTF callbacks");
683 				}
684 				ibc_detach(tmp_ibtfpriv);
685 				ddi_remove_minor_node(dip, "devctl");
686 				hermon_drv_fini(state);
687 				HERMON_ATTACH_MSG(state->hs_attach_buf,
688 				    "attach_agentinit_fail");
689 				goto fail_attach;
690 			}
691 		}
692 
693 		/* Report attach in maintenance mode, if appropriate */
694 		if (!(HERMON_IS_OPERATIONAL(state->hs_operational_mode))) {
695 			cmn_err(CE_NOTE, "hermon%d: driver attached "
696 			    "(for maintenance mode only)", state->hs_instance);
697 			hermon_fm_ereport(state, HCA_IBA_ERR, HCA_ERR_DEGRADED);
698 		}
699 
700 		/* Report that driver was loaded */
701 		ddi_report_dev(dip);
702 
703 		/* Send device information to log file */
704 		hermon_device_info_report(state);
705 
706 		/* DEBUG PRINT */
707 		cmn_err(CE_CONT, "!Hermon attach complete\n");
708 		return (DDI_SUCCESS);
709 
710 	case DDI_RESUME:
711 		/* Add code here for DDI_RESUME XXX */
712 		return (DDI_FAILURE);
713 
714 	default:
715 		cmn_err(CE_WARN, "hermon_attach: unknown cmd (0x%x)\n", cmd);
716 		break;
717 	}
718 
719 fail_attach:
720 	cmn_err(CE_NOTE, "hermon%d: driver failed to attach: %s", instance,
721 	    state->hs_attach_buf);
722 	if (hermon_get_state(state) & HCA_EREPORT_FM) {
723 		hermon_fm_ereport(state, HCA_SYS_ERR, HCA_ERR_SRV_LOST);
724 	}
725 	hermon_drv_fini2(state);
726 	hermon_fm_fini(state);
727 	ddi_soft_state_free(hermon_statep, instance);
728 
729 fail_attach_nomsg:
730 	return (DDI_FAILURE);
731 }
732 
733 
734 /*
735  * hermon_detach()
736  *    Context: Only called from detach() path context
737  */
738 static int
739 hermon_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
740 {
741 	hermon_state_t	*state;
742 	ibc_clnt_hdl_t	tmp_ibtfpriv;
743 	ibc_status_t	ibc_status;
744 	int		instance, status;
745 
746 	instance = ddi_get_instance(dip);
747 	state = ddi_get_soft_state(hermon_statep, instance);
748 	if (state == NULL) {
749 		return (DDI_FAILURE);
750 	}
751 
752 	switch (cmd) {
753 	case DDI_DETACH:
754 		/*
755 		 * If we are in "maintenance mode", then we do not want to
756 		 * do teardown for any of the InfiniBand interfaces.
757 		 * Specifically, this means not detaching from IBTF (we never
758 		 * attached to begin with) and not deregistering from IBMF.
759 		 */
760 		if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) {
761 			/* Unregister agents from IB Mgmt Framework (IBMF) */
762 			status = hermon_agent_handlers_fini(state);
763 			if (status != DDI_SUCCESS) {
764 				return (DDI_FAILURE);
765 			}
766 
767 			/*
768 			 * Attempt the "pre-detach" from InfiniBand Transport
769 			 * Framework (IBTF).  At this point the IBTF is still
770 			 * capable of handling incoming asynch and completion
771 			 * events.  This "pre-detach" is primarily a mechanism
772 			 * to notify the appropriate IBTF clients that the
773 			 * HCA is being removed/offlined.
774 			 */
775 			ibc_status = ibc_pre_detach(state->hs_ibtfpriv, cmd);
776 			if (ibc_status != IBC_SUCCESS) {
777 				status = hermon_agent_handlers_init(state);
778 				if (status != DDI_SUCCESS) {
779 					HERMON_WARNING(state, "failed to "
780 					    "restart Hermon agents");
781 				}
782 				return (DDI_FAILURE);
783 			}
784 
785 			/*
786 			 * Before we can fully detach from the IBTF we need to
787 			 * ensure that we have handled all outstanding event
788 			 * callbacks.  This is accomplished by quiescing the
789 			 * event callback mechanism.  Note: if we are unable
790 			 * to successfully quiesce the callbacks, then this is
791 			 * an indication that something has probably gone
792 			 * seriously wrong.  We print out a warning, but
793 			 * continue.
794 			 */
795 			tmp_ibtfpriv = state->hs_ibtfpriv;
796 			HERMON_QUIESCE_IBTF_CALLB(state);
797 			if (state->hs_in_evcallb != 0) {
798 				HERMON_WARNING(state, "unable to quiesce "
799 				    "Hermon IBTF callbacks");
800 			}
801 
802 			/* Complete the detach from the IBTF */
803 			ibc_detach(tmp_ibtfpriv);
804 		}
805 
806 		/* Remove the minor node for device */
807 		ddi_remove_minor_node(dip, "devctl");
808 
809 		/*
810 		 * Only call hermon_drv_fini() if we are in Hermon HCA mode.
811 		 * (Because if we are in "maintenance mode", then we never
812 		 * successfully finished init.)  Only report successful
813 		 * detach for normal HCA mode.
814 		 */
815 		if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) {
816 			/* Cleanup driver resources and shutdown hardware */
817 			hermon_drv_fini(state);
818 			cmn_err(CE_CONT, "!Hermon driver successfully "
819 			    "detached\n");
820 		}
821 
822 		hermon_drv_fini2(state);
823 		hermon_fm_fini(state);
824 		ddi_soft_state_free(hermon_statep, instance);
825 
826 		return (DDI_SUCCESS);
827 
828 	case DDI_SUSPEND:
829 		/* Add code here for DDI_SUSPEND XXX */
830 		return (DDI_FAILURE);
831 
832 	default:
833 		cmn_err(CE_WARN, "hermon_detach: unknown cmd (0x%x)\n", cmd);
834 		break;
835 	}
836 
837 	return (DDI_FAILURE);
838 }
839 
840 /*
841  * hermon_dma_attr_init()
842  *    Context: Can be called from interrupt or base context.
843  */
844 
845 /* ARGSUSED */
846 void
847 hermon_dma_attr_init(hermon_state_t *state, ddi_dma_attr_t *dma_attr)
848 {
849 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*dma_attr))
850 
851 	dma_attr->dma_attr_version	= DMA_ATTR_V0;
852 	dma_attr->dma_attr_addr_lo	= 0;
853 	dma_attr->dma_attr_addr_hi	= 0xFFFFFFFFFFFFFFFFull;
854 	dma_attr->dma_attr_count_max	= 0xFFFFFFFFFFFFFFFFull;
855 	dma_attr->dma_attr_align	= HERMON_PAGESIZE;  /* default 4K */
856 	dma_attr->dma_attr_burstsizes	= 0x3FF;
857 	dma_attr->dma_attr_minxfer	= 1;
858 	dma_attr->dma_attr_maxxfer	= 0xFFFFFFFFFFFFFFFFull;
859 	dma_attr->dma_attr_seg		= 0xFFFFFFFFFFFFFFFFull;
860 	dma_attr->dma_attr_sgllen	= 0x7FFFFFFF;
861 	dma_attr->dma_attr_granular	= 1;
862 	dma_attr->dma_attr_flags	= 0;
863 }
864 
865 /*
866  * hermon_dma_alloc()
867  *    Context: Can be called from base context.
868  */
869 int
870 hermon_dma_alloc(hermon_state_t *state, hermon_dma_info_t *dma_info,
871     uint16_t opcode)
872 {
873 	ddi_dma_handle_t	dma_hdl;
874 	ddi_dma_attr_t		dma_attr;
875 	ddi_acc_handle_t	acc_hdl;
876 	ddi_dma_cookie_t	cookie;
877 	uint64_t		kaddr;
878 	uint64_t		real_len;
879 	uint_t			ccount;
880 	int			status;
881 
882 	hermon_dma_attr_init(state, &dma_attr);
883 
884 	/* Allocate a DMA handle */
885 	status = ddi_dma_alloc_handle(state->hs_dip, &dma_attr, DDI_DMA_SLEEP,
886 	    NULL, &dma_hdl);
887 	if (status != DDI_SUCCESS) {
888 		IBTF_DPRINTF_L2("DMA", "alloc handle failed: %d", status);
889 		cmn_err(CE_CONT, "DMA alloc handle failed(status %d)", status);
890 		return (DDI_FAILURE);
891 	}
892 
893 	/* Allocate DMA memory */
894 	status = ddi_dma_mem_alloc(dma_hdl, dma_info->length,
895 	    &state->hs_reg_accattr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
896 	    (caddr_t *)&kaddr, (size_t *)&real_len, &acc_hdl);
897 	if (status != DDI_SUCCESS) {
898 		ddi_dma_free_handle(&dma_hdl);
899 		IBTF_DPRINTF_L2("DMA", "memory alloc failed: %d", status);
900 		cmn_err(CE_CONT, "DMA memory alloc failed(status %d)", status);
901 		return (DDI_FAILURE);
902 	}
903 	bzero((caddr_t)(uintptr_t)kaddr, real_len);
904 
905 	/* Bind the memory to the handle */
906 	status = ddi_dma_addr_bind_handle(dma_hdl, NULL,
907 	    (caddr_t)(uintptr_t)kaddr, (size_t)real_len, DDI_DMA_RDWR |
908 	    DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &cookie, &ccount);
909 	if (status != DDI_SUCCESS) {
910 		ddi_dma_mem_free(&acc_hdl);
911 		ddi_dma_free_handle(&dma_hdl);
912 		IBTF_DPRINTF_L2("DMA", "bind handle failed: %d", status);
913 		cmn_err(CE_CONT, "DMA bind handle failed(status %d)", status);
914 		return (DDI_FAILURE);
915 	}
916 
917 	/* Package the hermon_dma_info contents and return */
918 	dma_info->vaddr   = kaddr;
919 	dma_info->dma_hdl = dma_hdl;
920 	dma_info->acc_hdl = acc_hdl;
921 
922 	/* Pass the mapping information to the firmware */
923 	status = hermon_map_cmd_post(state, dma_info, opcode, cookie, ccount);
924 	if (status != DDI_SUCCESS) {
925 		char *s;
926 		hermon_dma_free(dma_info);
927 		switch (opcode) {
928 		case MAP_ICM:
929 			s = "MAP_ICM";
930 			break;
931 		case MAP_FA:
932 			s = "MAP_FA";
933 			break;
934 		case MAP_ICM_AUX:
935 			s = "MAP_ICM_AUX";
936 			break;
937 		default:
938 			s = "UNKNOWN";
939 		}
940 		cmn_err(CE_NOTE, "Map cmd '%s' failed, status %08x\n",
941 		    s, status);
942 		return (DDI_FAILURE);
943 	}
944 
945 	return (DDI_SUCCESS);
946 }
947 
948 /*
949  * hermon_dma_free()
950  *    Context: Can be called from base context.
951  */
952 void
953 hermon_dma_free(hermon_dma_info_t *info)
954 {
955 	/* Unbind the handles and free the memory */
956 	(void) ddi_dma_unbind_handle(info->dma_hdl);
957 	ddi_dma_mem_free(&info->acc_hdl);
958 	ddi_dma_free_handle(&info->dma_hdl);
959 }
960 
961 /* These macros are valid for use only in hermon_icm_alloc/hermon_icm_free. */
962 #define	HERMON_ICM_ALLOC(rsrc) \
963 	hermon_icm_alloc(state, rsrc, index1, index2)
964 #define	HERMON_ICM_FREE(rsrc) \
965 	hermon_icm_free(state, rsrc, index1, index2)
966 
967 /*
968  * hermon_icm_alloc()
969  *    Context: Can be called from base context.
970  *
971  * Only one thread can be here for a given hermon_rsrc_type_t "type".
972  */
973 int
974 hermon_icm_alloc(hermon_state_t *state, hermon_rsrc_type_t type,
975     uint32_t index1, uint32_t index2)
976 {
977 	hermon_icm_table_t	*icm;
978 	hermon_dma_info_t	*dma_info;
979 	uint8_t			*bitmap;
980 	int			status;
981 
982 	if (hermon_verbose) {
983 		IBTF_DPRINTF_L2("hermon", "hermon_icm_alloc: rsrc_type (0x%x) "
984 		    "index1/2 (0x%x/0x%x)", type, index1, index2);
985 	}
986 
987 	icm = &state->hs_icm[type];
988 
989 	switch (type) {
990 	case HERMON_QPC:
991 		status = HERMON_ICM_ALLOC(HERMON_CMPT_QPC);
992 		if (status != DDI_SUCCESS) {
993 			return (status);
994 		}
995 		status = HERMON_ICM_ALLOC(HERMON_RDB);
996 		if (status != DDI_SUCCESS) {	/* undo icm_alloc's */
997 			HERMON_ICM_FREE(HERMON_CMPT_QPC);
998 			return (status);
999 		}
1000 		status = HERMON_ICM_ALLOC(HERMON_ALTC);
1001 		if (status != DDI_SUCCESS) {	/* undo icm_alloc's */
1002 			HERMON_ICM_FREE(HERMON_RDB);
1003 			HERMON_ICM_FREE(HERMON_CMPT_QPC);
1004 			return (status);
1005 		}
1006 		status = HERMON_ICM_ALLOC(HERMON_AUXC);
1007 		if (status != DDI_SUCCESS) {	/* undo icm_alloc's */
1008 			HERMON_ICM_FREE(HERMON_ALTC);
1009 			HERMON_ICM_FREE(HERMON_RDB);
1010 			HERMON_ICM_FREE(HERMON_CMPT_QPC);
1011 			return (status);
1012 		}
1013 		break;
1014 	case HERMON_SRQC:
1015 		status = HERMON_ICM_ALLOC(HERMON_CMPT_SRQC);
1016 		if (status != DDI_SUCCESS) {
1017 			return (status);
1018 		}
1019 		break;
1020 	case HERMON_CQC:
1021 		status = HERMON_ICM_ALLOC(HERMON_CMPT_CQC);
1022 		if (status != DDI_SUCCESS) {
1023 			return (status);
1024 		}
1025 		break;
1026 	case HERMON_EQC:
1027 		status = HERMON_ICM_ALLOC(HERMON_CMPT_EQC);
1028 		if (status != DDI_SUCCESS) {	/* undo icm_alloc's */
1029 			return (status);
1030 		}
1031 		break;
1032 	}
1033 
1034 	/* ensure existence of bitmap and dmainfo, sets "dma_info" */
1035 	hermon_bitmap(bitmap, dma_info, icm, index1);
1036 
1037 	/* Set up the DMA handle for allocation and mapping */
1038 	dma_info = icm->icm_dma[index1] + index2;
1039 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*dma_info))
1040 	dma_info->length  = icm->span << icm->log_object_size;
1041 	dma_info->icmaddr = icm->icm_baseaddr +
1042 	    (((index1 << icm->split_shift) +
1043 	    (index2 << icm->span_shift)) << icm->log_object_size);
1044 
1045 	if (hermon_verbose) {
1046 		IBTF_DPRINTF_L2("hermon", "alloc DMA: "
1047 		    "rsrc (0x%x) index (%x, %x) "
1048 		    "icm_addr/len (%llx/%x) bitmap %p", type, index1, index2,
1049 		    (longlong_t)dma_info->icmaddr, dma_info->length, bitmap);
1050 	}
1051 
1052 	/* Allocate and map memory for this span */
1053 	status = hermon_dma_alloc(state, dma_info, MAP_ICM);
1054 	if (status != DDI_SUCCESS) {
1055 		IBTF_DPRINTF_L2("hermon", "hermon_icm_alloc: DMA "
1056 		    "allocation failed, status 0x%x", status);
1057 		switch (type) {
1058 		case HERMON_QPC:
1059 			HERMON_ICM_FREE(HERMON_AUXC);
1060 			HERMON_ICM_FREE(HERMON_ALTC);
1061 			HERMON_ICM_FREE(HERMON_RDB);
1062 			HERMON_ICM_FREE(HERMON_CMPT_QPC);
1063 			break;
1064 		case HERMON_SRQC:
1065 			HERMON_ICM_FREE(HERMON_CMPT_SRQC);
1066 			break;
1067 		case HERMON_CQC:
1068 			HERMON_ICM_FREE(HERMON_CMPT_CQC);
1069 			break;
1070 		case HERMON_EQC:
1071 			HERMON_ICM_FREE(HERMON_CMPT_EQC);
1072 			break;
1073 		}
1074 
1075 		return (DDI_FAILURE);
1076 	}
1077 	if (hermon_verbose) {
1078 		IBTF_DPRINTF_L2("hermon", "hermon_icm_alloc: mapping ICM: "
1079 		    "rsrc_type (0x%x) index (0x%x, 0x%x) alloc length (0x%x) "
1080 		    "icm_addr (0x%lx)", type, index1, index2, dma_info->length,
1081 		    dma_info->icmaddr);
1082 	}
1083 
1084 	/* Set the bit for this slot in the table bitmap */
1085 	HERMON_BMAP_BIT_SET(icm->icm_bitmap[index1], index2);
1086 
1087 	return (DDI_SUCCESS);
1088 }
1089 
1090 /*
1091  * hermon_icm_free()
1092  *    Context: Can be called from base context.
1093  *
1094  * ICM resources have been successfully returned from hermon_icm_alloc().
1095  * Associated dma_info is no longer in use.  Free the ICM backing memory.
1096  */
1097 void
1098 hermon_icm_free(hermon_state_t *state, hermon_rsrc_type_t type,
1099     uint32_t index1, uint32_t index2)
1100 {
1101 	hermon_icm_table_t	*icm;
1102 	hermon_dma_info_t	*dma_info;
1103 	int			status;
1104 
1105 	icm = &state->hs_icm[type];
1106 	ASSERT(icm->icm_dma[index1][index2].icm_refcnt == 0);
1107 
1108 	if (hermon_verbose) {
1109 		IBTF_DPRINTF_L2("hermon", "hermon_icm_free: rsrc_type (0x%x) "
1110 		    "index (0x%x, 0x%x)", type, index1, index2);
1111 	}
1112 
1113 	dma_info = icm->icm_dma[index1] + index2;
1114 
1115 	/* The following only happens if attach() is failing. */
1116 	if (dma_info == NULL)
1117 		return;
1118 
1119 	/* Unmap the ICM allocation, then free the backing DMA memory */
1120 	status = hermon_unmap_icm_cmd_post(state, dma_info);
1121 	if (status != DDI_SUCCESS) {
1122 		HERMON_WARNING(state, "UNMAP_ICM failure");
1123 	}
1124 	hermon_dma_free(dma_info);
1125 
1126 	/* Clear the bit in the ICM table bitmap */
1127 	HERMON_BMAP_BIT_CLR(icm->icm_bitmap[index1], index2);
1128 
1129 	switch (type) {
1130 	case HERMON_QPC:
1131 		HERMON_ICM_FREE(HERMON_AUXC);
1132 		HERMON_ICM_FREE(HERMON_ALTC);
1133 		HERMON_ICM_FREE(HERMON_RDB);
1134 		HERMON_ICM_FREE(HERMON_CMPT_QPC);
1135 		break;
1136 	case HERMON_SRQC:
1137 		HERMON_ICM_FREE(HERMON_CMPT_SRQC);
1138 		break;
1139 	case HERMON_CQC:
1140 		HERMON_ICM_FREE(HERMON_CMPT_CQC);
1141 		break;
1142 	case HERMON_EQC:
1143 		HERMON_ICM_FREE(HERMON_CMPT_EQC);
1144 		break;
1145 
1146 	}
1147 }
1148 
1149 /*
1150  * hermon_drv_init()
1151  *    Context: Only called from attach() path context
1152  */
1153 /* ARGSUSED */
1154 static int
1155 hermon_drv_init(hermon_state_t *state, dev_info_t *dip, int instance)
1156 {
1157 	int	status;
1158 
1159 	/*
1160 	 * Check and set the operational mode of the device. If the driver is
1161 	 * bound to the Hermon device in "maintenance mode", then this generally
1162 	 * means that either the device has been specifically jumpered to
1163 	 * start in this mode or the firmware boot process has failed to
1164 	 * successfully load either the primary or the secondary firmware
1165 	 * image.
1166 	 */
1167 	if (HERMON_IS_HCA_MODE(state->hs_dip)) {
1168 		state->hs_operational_mode = HERMON_HCA_MODE;
1169 		state->hs_cfg_profile_setting = HERMON_CFG_MEMFREE;
1170 
1171 	} else if (HERMON_IS_MAINTENANCE_MODE(state->hs_dip)) {
1172 		HERMON_FMANOTE(state, HERMON_FMA_MAINT);
1173 		state->hs_operational_mode = HERMON_MAINTENANCE_MODE;
1174 		return (DDI_FAILURE);
1175 
1176 	} else {
1177 		state->hs_operational_mode = 0;	/* invalid operational mode */
1178 		HERMON_FMANOTE(state, HERMON_FMA_PCIID);
1179 		HERMON_WARNING(state, "unexpected device type detected");
1180 		return (DDI_FAILURE);
1181 	}
1182 
1183 	/*
1184 	 * Initialize the Hermon hardware.
1185 	 *
1186 	 * Note:  If this routine returns an error, it is often a reasonably
1187 	 * good indication that something Hermon firmware-related has caused
1188 	 * the failure or some HW related errors have caused the failure.
1189 	 * (also there are few possibilities that SW (e.g. SW resource
1190 	 * shortage) can cause the failure, but the majority case is due to
1191 	 * either a firmware related error or a HW related one) In order to
1192 	 * give the user an opportunity (if desired) to update or reflash
1193 	 * the Hermon firmware image, we set "hs_operational_mode" flag
1194 	 * (described above) to indicate that we wish to enter maintenance
1195 	 * mode in case of the firmware-related issue.
1196 	 */
1197 	status = hermon_hw_init(state);
1198 	if (status != DDI_SUCCESS) {
1199 		cmn_err(CE_NOTE, "hermon%d: error during attach: %s", instance,
1200 		    state->hs_attach_buf);
1201 		return (DDI_FAILURE);
1202 	}
1203 
1204 	/*
1205 	 * Now that the ISR has been setup, arm all the EQs for event
1206 	 * generation.
1207 	 */
1208 
1209 	status = hermon_eq_arm_all(state);
1210 	if (status != DDI_SUCCESS) {
1211 		cmn_err(CE_NOTE, "EQ Arm All failed\n");
1212 		hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL);
1213 		return (DDI_FAILURE);
1214 	}
1215 
1216 	/* test interrupts and event queues */
1217 	status = hermon_nop_post(state, 0x0, 0x0);
1218 	if (status != DDI_SUCCESS) {
1219 		cmn_err(CE_NOTE, "Interrupts/EQs failed\n");
1220 		hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL);
1221 		return (DDI_FAILURE);
1222 	}
1223 
1224 	/* Initialize Hermon softstate */
1225 	status = hermon_soft_state_init(state);
1226 	if (status != DDI_SUCCESS) {
1227 		cmn_err(CE_NOTE, "Failed to init soft state\n");
1228 		hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL);
1229 		return (DDI_FAILURE);
1230 	}
1231 
1232 	return (DDI_SUCCESS);
1233 }
1234 
1235 
1236 /*
1237  * hermon_drv_fini()
1238  *    Context: Only called from attach() and/or detach() path contexts
1239  */
1240 static void
1241 hermon_drv_fini(hermon_state_t *state)
1242 {
1243 	/* Cleanup Hermon softstate */
1244 	hermon_soft_state_fini(state);
1245 
1246 	/* Cleanup Hermon resources and shutdown hardware */
1247 	hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL);
1248 }
1249 
1250 
1251 /*
1252  * hermon_drv_fini2()
1253  *    Context: Only called from attach() and/or detach() path contexts
1254  */
1255 static void
1256 hermon_drv_fini2(hermon_state_t *state)
1257 {
1258 	if (state->hs_fm_poll_thread) {
1259 		ddi_periodic_delete(state->hs_fm_poll_thread);
1260 		state->hs_fm_poll_thread = NULL;
1261 	}
1262 
1263 	/* HERMON_DRV_CLEANUP_LEVEL1 */
1264 	if (state->hs_fm_cmdhdl) {
1265 		hermon_regs_map_free(state, &state->hs_fm_cmdhdl);
1266 		state->hs_fm_cmdhdl = NULL;
1267 	}
1268 
1269 	if (state->hs_reg_cmdhdl) {
1270 		ddi_regs_map_free(&state->hs_reg_cmdhdl);
1271 		state->hs_reg_cmdhdl = NULL;
1272 	}
1273 
1274 	/* HERMON_DRV_CLEANUP_LEVEL0 */
1275 	if (state->hs_msix_tbl_entries) {
1276 		kmem_free(state->hs_msix_tbl_entries,
1277 		    state->hs_msix_tbl_size);
1278 		state->hs_msix_tbl_entries = NULL;
1279 	}
1280 
1281 	if (state->hs_msix_pba_entries) {
1282 		kmem_free(state->hs_msix_pba_entries,
1283 		    state->hs_msix_pba_size);
1284 		state->hs_msix_pba_entries = NULL;
1285 	}
1286 
1287 	if (state->hs_fm_msix_tblhdl) {
1288 		hermon_regs_map_free(state, &state->hs_fm_msix_tblhdl);
1289 		state->hs_fm_msix_tblhdl = NULL;
1290 	}
1291 
1292 	if (state->hs_reg_msix_tblhdl) {
1293 		ddi_regs_map_free(&state->hs_reg_msix_tblhdl);
1294 		state->hs_reg_msix_tblhdl = NULL;
1295 	}
1296 
1297 	if (state->hs_fm_msix_pbahdl) {
1298 		hermon_regs_map_free(state, &state->hs_fm_msix_pbahdl);
1299 		state->hs_fm_msix_pbahdl = NULL;
1300 	}
1301 
1302 	if (state->hs_reg_msix_pbahdl) {
1303 		ddi_regs_map_free(&state->hs_reg_msix_pbahdl);
1304 		state->hs_reg_msix_pbahdl = NULL;
1305 	}
1306 
1307 	if (state->hs_fm_pcihdl) {
1308 		hermon_pci_config_teardown(state, &state->hs_fm_pcihdl);
1309 		state->hs_fm_pcihdl = NULL;
1310 	}
1311 
1312 	if (state->hs_reg_pcihdl) {
1313 		pci_config_teardown(&state->hs_reg_pcihdl);
1314 		state->hs_reg_pcihdl = NULL;
1315 	}
1316 }
1317 
1318 
1319 /*
1320  * hermon_isr_init()
1321  *    Context: Only called from attach() path context
1322  */
1323 static int
1324 hermon_isr_init(hermon_state_t *state)
1325 {
1326 	int	status;
1327 	int	intr;
1328 
1329 	for (intr = 0; intr < state->hs_intrmsi_allocd; intr++) {
1330 
1331 		/*
1332 		 * Add a handler for the interrupt or MSI
1333 		 */
1334 		status = ddi_intr_add_handler(state->hs_intrmsi_hdl[intr],
1335 		    hermon_isr, (caddr_t)state, (void *)(uintptr_t)intr);
1336 		if (status  != DDI_SUCCESS) {
1337 			return (DDI_FAILURE);
1338 		}
1339 
1340 		/*
1341 		 * Enable the software interrupt.  Note: depending on the value
1342 		 * returned in the capability flag, we have to call either
1343 		 * ddi_intr_block_enable() or ddi_intr_enable().
1344 		 */
1345 		if (state->hs_intrmsi_cap & DDI_INTR_FLAG_BLOCK) {
1346 			status = ddi_intr_block_enable(
1347 			    &state->hs_intrmsi_hdl[intr], 1);
1348 			if (status != DDI_SUCCESS) {
1349 				return (DDI_FAILURE);
1350 			}
1351 		} else {
1352 			status = ddi_intr_enable(state->hs_intrmsi_hdl[intr]);
1353 			if (status != DDI_SUCCESS) {
1354 				return (DDI_FAILURE);
1355 			}
1356 		}
1357 	}
1358 
1359 	/*
1360 	 * Now that the ISR has been enabled, defer arm_all  EQs for event
1361 	 * generation until later, in case MSIX is enabled
1362 	 */
1363 	return (DDI_SUCCESS);
1364 }
1365 
1366 
1367 /*
1368  * hermon_isr_fini()
1369  *    Context: Only called from attach() and/or detach() path contexts
1370  */
1371 static void
1372 hermon_isr_fini(hermon_state_t *state)
1373 {
1374 	int	intr;
1375 
1376 	for (intr = 0; intr < state->hs_intrmsi_allocd; intr++) {
1377 		/* Disable the software interrupt */
1378 		if (state->hs_intrmsi_cap & DDI_INTR_FLAG_BLOCK) {
1379 			(void) ddi_intr_block_disable(
1380 			    &state->hs_intrmsi_hdl[intr], 1);
1381 		} else {
1382 			(void) ddi_intr_disable(state->hs_intrmsi_hdl[intr]);
1383 		}
1384 
1385 		/*
1386 		 * Remove the software handler for the interrupt or MSI
1387 		 */
1388 		(void) ddi_intr_remove_handler(state->hs_intrmsi_hdl[intr]);
1389 	}
1390 }
1391 
1392 
1393 /*
1394  * Sum of ICM configured values:
1395  *     cMPT, dMPT, MTT, QPC, SRQC, RDB, CQC, ALTC, AUXC, EQC, MCG
1396  *
1397  */
1398 static uint64_t
1399 hermon_size_icm(hermon_state_t *state)
1400 {
1401 	hermon_hw_querydevlim_t	*devlim;
1402 	hermon_cfg_profile_t	*cfg;
1403 	uint64_t		num_cmpts, num_dmpts, num_mtts;
1404 	uint64_t		num_qpcs, num_srqc, num_rdbs;
1405 #ifndef HERMON_FW_WORKAROUND
1406 	uint64_t		num_auxc;
1407 #endif
1408 	uint64_t		num_cqcs, num_altc;
1409 	uint64_t		num_eqcs, num_mcgs;
1410 	uint64_t		size;
1411 
1412 	devlim = &state->hs_devlim;
1413 	cfg = state->hs_cfg_profile;
1414 	/* number of respective entries */
1415 	num_cmpts = (uint64_t)0x1 << cfg->cp_log_num_cmpt;
1416 	num_mtts = (uint64_t)0x1 << cfg->cp_log_num_mtt;
1417 	num_dmpts = (uint64_t)0x1 << cfg->cp_log_num_dmpt;
1418 	num_qpcs = (uint64_t)0x1 << cfg->cp_log_num_qp;
1419 	num_srqc = (uint64_t)0x1 << cfg->cp_log_num_srq;
1420 	num_rdbs = (uint64_t)0x1 << cfg->cp_log_num_rdb;
1421 	num_cqcs = (uint64_t)0x1 << cfg->cp_log_num_cq;
1422 	num_altc = (uint64_t)0x1 << cfg->cp_log_num_qp;
1423 #ifndef HERMON_FW_WORKAROUND
1424 	num_auxc = (uint64_t)0x1 << cfg->cp_log_num_qp;
1425 #endif
1426 	num_eqcs = (uint64_t)0x1 << cfg->cp_log_num_eq;
1427 	num_mcgs = (uint64_t)0x1 << cfg->cp_log_num_mcg;
1428 
1429 	size =
1430 	    num_cmpts 	* devlim->cmpt_entry_sz +
1431 	    num_dmpts	* devlim->dmpt_entry_sz +
1432 	    num_mtts	* devlim->mtt_entry_sz +
1433 	    num_qpcs	* devlim->qpc_entry_sz +
1434 	    num_srqc	* devlim->srq_entry_sz +
1435 	    num_rdbs	* devlim->rdmardc_entry_sz +
1436 	    num_cqcs	* devlim->cqc_entry_sz +
1437 	    num_altc	* devlim->altc_entry_sz +
1438 #ifdef HERMON_FW_WORKAROUND
1439 	    0x80000000ull +
1440 #else
1441 	    num_auxc	* devlim->aux_entry_sz	+
1442 #endif
1443 	    num_eqcs	* devlim->eqc_entry_sz +
1444 	    num_mcgs	* HERMON_MCGMEM_SZ(state);
1445 	return (size);
1446 }
1447 
1448 
1449 /*
1450  * hermon_hw_init()
1451  *    Context: Only called from attach() path context
1452  */
1453 static int
1454 hermon_hw_init(hermon_state_t *state)
1455 {
1456 	hermon_drv_cleanup_level_t	cleanup;
1457 	sm_nodeinfo_t			nodeinfo;
1458 	uint64_t			clr_intr_offset;
1459 	int				status;
1460 	uint32_t			fw_size;	/* in page */
1461 	uint64_t			offset;
1462 
1463 	/* This is where driver initialization begins */
1464 	cleanup = HERMON_DRV_CLEANUP_LEVEL0;
1465 
1466 	/* Setup device access attributes */
1467 	state->hs_reg_accattr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
1468 	state->hs_reg_accattr.devacc_attr_endian_flags = DDI_STRUCTURE_BE_ACC;
1469 	state->hs_reg_accattr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1470 	state->hs_reg_accattr.devacc_attr_access = DDI_DEFAULT_ACC;
1471 
1472 	/* Setup fma-protected access attributes */
1473 	state->hs_fm_accattr.devacc_attr_version =
1474 	    hermon_devacc_attr_version(state);
1475 	state->hs_fm_accattr.devacc_attr_endian_flags = DDI_STRUCTURE_BE_ACC;
1476 	state->hs_fm_accattr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1477 	/* set acc err protection type */
1478 	state->hs_fm_accattr.devacc_attr_access =
1479 	    hermon_devacc_attr_access(state);
1480 
1481 	/* Setup for PCI config read/write of HCA device */
1482 	status = hermon_pci_config_setup(state, &state->hs_fm_pcihdl);
1483 	if (status != DDI_SUCCESS) {
1484 		hermon_hw_fini(state, cleanup);
1485 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1486 		    "hw_init_PCI_config_space_regmap_fail");
1487 		/* This case is not the degraded one */
1488 		return (DDI_FAILURE);
1489 	}
1490 
1491 	/* Map PCI config space and MSI-X tables/pba */
1492 	hermon_set_msix_info(state);
1493 
1494 	/* Map in Hermon registers (CMD, UAR, MSIX) and setup offsets */
1495 	status = hermon_regs_map_setup(state, HERMON_CMD_BAR,
1496 	    &state->hs_reg_cmd_baseaddr, 0, 0, &state->hs_fm_accattr,
1497 	    &state->hs_fm_cmdhdl);
1498 	if (status != DDI_SUCCESS) {
1499 		hermon_hw_fini(state, cleanup);
1500 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1501 		    "hw_init_CMD_BAR_regmap_fail");
1502 		/* This case is not the degraded one */
1503 		return (DDI_FAILURE);
1504 	}
1505 
1506 	cleanup = HERMON_DRV_CLEANUP_LEVEL1;
1507 	/*
1508 	 * We defer UAR-BAR mapping until later.  Need to know if
1509 	 * blueflame mapping is to be done, and don't know that until after
1510 	 * we get the dev_caps, so do it right after that
1511 	 */
1512 
1513 	/*
1514 	 * There is a third BAR defined for Hermon - it is for MSIX
1515 	 *
1516 	 * Will need to explore it's possible need/use w/ Mellanox
1517 	 * [es] Temporary mapping maybe
1518 	 */
1519 
1520 #ifdef HERMON_SUPPORTS_MSIX_BAR
1521 	status = ddi_regs_map_setup(state->hs_dip, HERMON_MSIX_BAR,
1522 	    &state->hs_reg_msi_baseaddr, 0, 0, &state->hs_reg_accattr,
1523 	    &state->hs_reg_msihdl);
1524 	if (status != DDI_SUCCESS) {
1525 		hermon_hw_fini(state, cleanup);
1526 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1527 		    "hw_init_MSIX_BAR_regmap_fail");
1528 		/* This case is not the degraded one */
1529 		return (DDI_FAILURE);
1530 	}
1531 #endif
1532 
1533 	cleanup = HERMON_DRV_CLEANUP_LEVEL2;
1534 
1535 	/*
1536 	 * Save interesting registers away. The offsets of the first two
1537 	 * here (HCR and sw_reset) are detailed in the PRM, the others are
1538 	 * derived from values in the QUERY_FW output, so we'll save them
1539 	 * off later.
1540 	 */
1541 	/* Host Command Register (HCR) */
1542 	state->hs_cmd_regs.hcr = (hermon_hw_hcr_t *)
1543 	    ((uintptr_t)state->hs_reg_cmd_baseaddr + HERMON_CMD_HCR_OFFSET);
1544 	state->hs_cmd_toggle = 0;	/* initialize it for use */
1545 
1546 	/* Software Reset register (sw_reset) and semaphore */
1547 	state->hs_cmd_regs.sw_reset = (uint32_t *)
1548 	    ((uintptr_t)state->hs_reg_cmd_baseaddr +
1549 	    HERMON_CMD_SW_RESET_OFFSET);
1550 	state->hs_cmd_regs.sw_semaphore = (uint32_t *)
1551 	    ((uintptr_t)state->hs_reg_cmd_baseaddr +
1552 	    HERMON_CMD_SW_SEMAPHORE_OFFSET);
1553 
1554 	/* Retrieve PCI device, vendor and rev IDs */
1555 	state->hs_vendor_id	 = HERMON_GET_VENDOR_ID(state->hs_dip);
1556 	state->hs_device_id	 = HERMON_GET_DEVICE_ID(state->hs_dip);
1557 	state->hs_revision_id	 = HERMON_GET_REVISION_ID(state->hs_dip);
1558 
1559 	/* make sure init'd before we start filling things in */
1560 	bzero(&state->hs_hcaparams, sizeof (struct hermon_hw_initqueryhca_s));
1561 
1562 	/* Initialize the Phase1 configuration profile */
1563 	status = hermon_cfg_profile_init_phase1(state);
1564 	if (status != DDI_SUCCESS) {
1565 		hermon_hw_fini(state, cleanup);
1566 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1567 		    "hw_init_cfginit1_fail");
1568 		/* This case is not the degraded one */
1569 		return (DDI_FAILURE);
1570 	}
1571 	cleanup = HERMON_DRV_CLEANUP_LEVEL3;
1572 
1573 	/* Do a software reset of the adapter to ensure proper state */
1574 	status = hermon_sw_reset(state);
1575 	if (status != HERMON_CMD_SUCCESS) {
1576 		hermon_hw_fini(state, cleanup);
1577 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1578 		    "hw_init_sw_reset_fail");
1579 		/* This case is not the degraded one */
1580 		return (DDI_FAILURE);
1581 	}
1582 
1583 	/* Initialize mailboxes */
1584 	status = hermon_rsrc_init_phase1(state);
1585 	if (status != DDI_SUCCESS) {
1586 		hermon_hw_fini(state, cleanup);
1587 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1588 		    "hw_init_rsrcinit1_fail");
1589 		/* This case is not the degraded one */
1590 		return (DDI_FAILURE);
1591 	}
1592 	cleanup = HERMON_DRV_CLEANUP_LEVEL4;
1593 
1594 	/* Post QUERY_FW */
1595 	status = hermon_cmn_query_cmd_post(state, QUERY_FW, 0, 0, &state->hs_fw,
1596 	    sizeof (hermon_hw_queryfw_t), HERMON_CMD_NOSLEEP_SPIN);
1597 	if (status != HERMON_CMD_SUCCESS) {
1598 		cmn_err(CE_NOTE, "QUERY_FW command failed: %08x\n", status);
1599 		hermon_hw_fini(state, cleanup);
1600 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1601 		    "hw_init_query_fw_cmd_fail");
1602 		/* This case is not the degraded one */
1603 		return (DDI_FAILURE);
1604 	}
1605 
1606 	/* Validate what/that HERMON FW version is appropriate */
1607 
1608 	status = hermon_fw_version_check(state);
1609 	if (status != DDI_SUCCESS) {
1610 		HERMON_FMANOTE(state, HERMON_FMA_FWVER);
1611 		if (state->hs_operational_mode == HERMON_HCA_MODE) {
1612 			cmn_err(CE_CONT, "Unsupported Hermon FW version: "
1613 			    "expected: %04d.%04d.%04d, "
1614 			    "actual: %04d.%04d.%04d\n",
1615 			    HERMON_FW_VER_MAJOR,
1616 			    HERMON_FW_VER_MINOR,
1617 			    HERMON_FW_VER_SUBMINOR,
1618 			    state->hs_fw.fw_rev_major,
1619 			    state->hs_fw.fw_rev_minor,
1620 			    state->hs_fw.fw_rev_subminor);
1621 		} else {
1622 			cmn_err(CE_CONT, "Unsupported FW version: "
1623 			    "%04d.%04d.%04d\n",
1624 			    state->hs_fw.fw_rev_major,
1625 			    state->hs_fw.fw_rev_minor,
1626 			    state->hs_fw.fw_rev_subminor);
1627 		}
1628 		state->hs_operational_mode = HERMON_MAINTENANCE_MODE;
1629 		hermon_hw_fini(state, cleanup);
1630 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1631 		    "hw_init_checkfwver_fail");
1632 		/* This case is the degraded one */
1633 		return (HERMON_CMD_BAD_NVMEM);
1634 	}
1635 
1636 	/*
1637 	 * Save off the rest of the interesting registers that we'll be using.
1638 	 * Setup the offsets for the other registers.
1639 	 */
1640 
1641 	/*
1642 	 * Hermon does the intr_offset from the BAR - technically should get the
1643 	 * BAR info from the response, but PRM says it's from BAR0-1, which is
1644 	 * for us the CMD BAR
1645 	 */
1646 
1647 	clr_intr_offset	 = state->hs_fw.clr_intr_offs & HERMON_CMD_OFFSET_MASK;
1648 
1649 	/* Save Clear Interrupt address */
1650 	state->hs_cmd_regs.clr_intr = (uint64_t *)
1651 	    (uintptr_t)(state->hs_reg_cmd_baseaddr + clr_intr_offset);
1652 
1653 	/*
1654 	 * Set the error buffer also into the structure - used in hermon_event.c
1655 	 * to check for internal error on the HCA, not reported in eqe or
1656 	 * (necessarily) by interrupt
1657 	 */
1658 	state->hs_cmd_regs.fw_err_buf = (uint32_t *)(uintptr_t)
1659 	    (state->hs_reg_cmd_baseaddr + state->hs_fw.error_buf_addr);
1660 
1661 	/*
1662 	 * Invoke a polling thread to check the error buffer periodically.
1663 	 */
1664 	if (!hermon_no_inter_err_chk) {
1665 		state->hs_fm_poll_thread = ddi_periodic_add(
1666 		    hermon_inter_err_chk, (void *)state, FM_POLL_INTERVAL,
1667 		    DDI_IPL_0);
1668 	}
1669 
1670 	cleanup = HERMON_DRV_CLEANUP_LEVEL5;
1671 
1672 	/*
1673 	 * Allocate, map, and run the HCA Firmware.
1674 	 */
1675 
1676 	/* Allocate memory for the firmware to load into and map it */
1677 
1678 	/* get next higher power of 2 */
1679 	fw_size = 1 << highbit(state->hs_fw.fw_pages);
1680 	state->hs_fw_dma.length = fw_size << HERMON_PAGESHIFT;
1681 	status = hermon_dma_alloc(state, &state->hs_fw_dma, MAP_FA);
1682 	if (status != DDI_SUCCESS) {
1683 		cmn_err(CE_NOTE, "FW alloc failed\n");
1684 		hermon_hw_fini(state, cleanup);
1685 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1686 		    "hw_init_dma_alloc_fw_fail");
1687 		/* This case is not the degraded one */
1688 		return (DDI_FAILURE);
1689 	}
1690 
1691 	cleanup = HERMON_DRV_CLEANUP_LEVEL6;
1692 
1693 	/* Invoke the RUN_FW cmd to run the firmware */
1694 	status = hermon_run_fw_cmd_post(state);
1695 	if (status != DDI_SUCCESS) {
1696 		cmn_err(CE_NOTE, "RUN_FW command failed: 0x%08x\n", status);
1697 		if (status == HERMON_CMD_BAD_NVMEM) {
1698 			state->hs_operational_mode = HERMON_MAINTENANCE_MODE;
1699 		}
1700 		hermon_hw_fini(state, cleanup);
1701 		HERMON_ATTACH_MSG(state->hs_attach_buf, "hw_init_run_fw_fail");
1702 		/*
1703 		 * If the status is HERMON_CMD_BAD_NVMEM, it's likely the
1704 		 * firmware is corrupted, so the mode falls into the
1705 		 * maintenance mode.
1706 		 */
1707 		return (status == HERMON_CMD_BAD_NVMEM ? HERMON_CMD_BAD_NVMEM :
1708 		    DDI_FAILURE);
1709 	}
1710 
1711 
1712 	/*
1713 	 * QUERY DEVICE LIMITS/CAPABILITIES
1714 	 * NOTE - in Hermon, the command is changed to QUERY_DEV_CAP,
1715 	 * but for familiarity we have kept the structure name the
1716 	 * same as Tavor/Arbel
1717 	 */
1718 
1719 	status = hermon_cmn_query_cmd_post(state, QUERY_DEV_CAP, 0, 0,
1720 	    &state->hs_devlim, sizeof (hermon_hw_querydevlim_t),
1721 	    HERMON_CMD_NOSLEEP_SPIN);
1722 	if (status != HERMON_CMD_SUCCESS) {
1723 		cmn_err(CE_NOTE, "QUERY_DEV_CAP command failed: 0x%08x\n",
1724 		    status);
1725 		hermon_hw_fini(state, cleanup);
1726 		HERMON_ATTACH_MSG(state->hs_attach_buf, "hw_init_devcap_fail");
1727 		/* This case is not the degraded one */
1728 		return (DDI_FAILURE);
1729 	}
1730 
1731 	state->hs_devlim.num_rsvd_eq = max(state->hs_devlim.num_rsvd_eq,
1732 	    (4 * state->hs_devlim.num_rsvd_uar));	/* lesser of resvd's */
1733 
1734 	/* now we have enough info to map in the UAR BAR */
1735 	/*
1736 	 * First, we figure out how to map the BAR for UAR - use only half if
1737 	 * BlueFlame is enabled - in that case the mapped length is 1/2 the
1738 	 * log_max_uar_sz (max__uar - 1) * 1MB ( +20).
1739 	 */
1740 
1741 	if (state->hs_devlim.blu_flm) {		/* Blue Flame Enabled */
1742 		offset = (uint64_t)1 << (state->hs_devlim.log_max_uar_sz + 20);
1743 	} else {
1744 		offset = 0;	/* a zero length means map the whole thing */
1745 	}
1746 	status = hermon_regs_map_setup(state, HERMON_UAR_BAR,
1747 	    &state->hs_reg_uar_baseaddr, 0, offset, &state->hs_fm_accattr,
1748 	    &state->hs_fm_uarhdl);
1749 	if (status != DDI_SUCCESS) {
1750 		HERMON_ATTACH_MSG(state->hs_attach_buf, "UAR BAR mapping");
1751 		/* This case is not the degraded one */
1752 		return (DDI_FAILURE);
1753 	}
1754 
1755 	/* and if BlueFlame is enabled, map the other half there */
1756 	if (state->hs_devlim.blu_flm) {		/* Blue Flame Enabled */
1757 		offset = (uint64_t)1 << (state->hs_devlim.log_max_uar_sz + 20);
1758 		status = ddi_regs_map_setup(state->hs_dip, HERMON_UAR_BAR,
1759 		    &state->hs_reg_bf_baseaddr, offset, offset,
1760 		    &state->hs_reg_accattr, &state->hs_reg_bfhdl);
1761 		if (status != DDI_SUCCESS) {
1762 			HERMON_ATTACH_MSG(state->hs_attach_buf,
1763 			    "BlueFlame BAR mapping");
1764 			/* This case is not the degraded one */
1765 			return (DDI_FAILURE);
1766 		}
1767 		/* This will be used in hw_fini if we fail to init. */
1768 		state->hs_bf_offset = offset;
1769 	}
1770 	cleanup = HERMON_DRV_CLEANUP_LEVEL7;
1771 
1772 	/* Hermon has a couple of things needed for phase 2 in query port */
1773 
1774 	status = hermon_cmn_query_cmd_post(state, QUERY_PORT, 0, 0x01,
1775 	    &state->hs_queryport, sizeof (hermon_hw_query_port_t),
1776 	    HERMON_CMD_NOSLEEP_SPIN);
1777 	if (status != HERMON_CMD_SUCCESS) {
1778 		cmn_err(CE_NOTE, "QUERY_PORT command failed: 0x%08x\n",
1779 		    status);
1780 		hermon_hw_fini(state, cleanup);
1781 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1782 		    "hw_init_queryport_fail");
1783 		/* This case is not the degraded one */
1784 		return (DDI_FAILURE);
1785 	}
1786 
1787 	/* Initialize the Phase2 Hermon configuration profile */
1788 	status = hermon_cfg_profile_init_phase2(state);
1789 	if (status != DDI_SUCCESS) {
1790 		cmn_err(CE_NOTE, "CFG phase 2 failed: 0x%08x\n", status);
1791 		hermon_hw_fini(state, cleanup);
1792 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1793 		    "hw_init_cfginit2_fail");
1794 		/* This case is not the degraded one */
1795 		return (DDI_FAILURE);
1796 	}
1797 
1798 	/* Determine and set the ICM size */
1799 	state->hs_icm_sz = hermon_size_icm(state);
1800 	status		 = hermon_set_icm_size_cmd_post(state);
1801 	if (status != DDI_SUCCESS) {
1802 		cmn_err(CE_NOTE, "Hermon: SET_ICM_SIZE cmd failed: 0x%08x\n",
1803 		    status);
1804 		hermon_hw_fini(state, cleanup);
1805 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1806 		    "hw_init_seticmsz_fail");
1807 		/* This case is not the degraded one */
1808 		return (DDI_FAILURE);
1809 	}
1810 	/* alloc icm aux physical memory and map it */
1811 
1812 	state->hs_icma_dma.length = 1 << highbit(state->hs_icma_sz);
1813 
1814 	status = hermon_dma_alloc(state, &state->hs_icma_dma, MAP_ICM_AUX);
1815 	if (status != DDI_SUCCESS) {
1816 		cmn_err(CE_NOTE, "failed to alloc (0x%llx) bytes for ICMA\n",
1817 		    (longlong_t)state->hs_icma_dma.length);
1818 		hermon_hw_fini(state, cleanup);
1819 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1820 		    "hw_init_dma_alloc_icm_aux_fail");
1821 		/* This case is not the degraded one */
1822 		return (DDI_FAILURE);
1823 	}
1824 	cleanup = HERMON_DRV_CLEANUP_LEVEL8;
1825 
1826 	cleanup = HERMON_DRV_CLEANUP_LEVEL9;
1827 
1828 	/* Allocate an array of structures to house the ICM tables */
1829 	state->hs_icm = kmem_zalloc(HERMON_NUM_ICM_RESOURCES *
1830 	    sizeof (hermon_icm_table_t), KM_SLEEP);
1831 
1832 	/* Set up the ICM address space and the INIT_HCA command input */
1833 	status = hermon_icm_config_setup(state, &state->hs_hcaparams);
1834 	if (status != HERMON_CMD_SUCCESS) {
1835 		cmn_err(CE_NOTE, "ICM configuration failed\n");
1836 		hermon_hw_fini(state, cleanup);
1837 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1838 		    "hw_init_icm_config_setup_fail");
1839 		/* This case is not the degraded one */
1840 		return (DDI_FAILURE);
1841 	}
1842 	cleanup = HERMON_DRV_CLEANUP_LEVEL10;
1843 
1844 	/* Initialize the adapter with the INIT_HCA cmd */
1845 	status = hermon_init_hca_cmd_post(state, &state->hs_hcaparams,
1846 	    HERMON_CMD_NOSLEEP_SPIN);
1847 	if (status != HERMON_CMD_SUCCESS) {
1848 		cmn_err(CE_NOTE, "INIT_HCA command failed: %08x\n", status);
1849 		hermon_hw_fini(state, cleanup);
1850 		HERMON_ATTACH_MSG(state->hs_attach_buf, "hw_init_hca_fail");
1851 		/* This case is not the degraded one */
1852 		return (DDI_FAILURE);
1853 	}
1854 	cleanup = HERMON_DRV_CLEANUP_LEVEL11;
1855 
1856 	/* Enter the second phase of init for Hermon configuration/resources */
1857 	status = hermon_rsrc_init_phase2(state);
1858 	if (status != DDI_SUCCESS) {
1859 		hermon_hw_fini(state, cleanup);
1860 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1861 		    "hw_init_rsrcinit2_fail");
1862 		/* This case is not the degraded one */
1863 		return (DDI_FAILURE);
1864 	}
1865 	cleanup = HERMON_DRV_CLEANUP_LEVEL12;
1866 
1867 	/* Query the adapter via QUERY_ADAPTER */
1868 	status = hermon_cmn_query_cmd_post(state, QUERY_ADAPTER, 0, 0,
1869 	    &state->hs_adapter, sizeof (hermon_hw_queryadapter_t),
1870 	    HERMON_CMD_NOSLEEP_SPIN);
1871 	if (status != HERMON_CMD_SUCCESS) {
1872 		cmn_err(CE_NOTE, "Hermon: QUERY_ADAPTER command failed: %08x\n",
1873 		    status);
1874 		hermon_hw_fini(state, cleanup);
1875 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1876 		    "hw_init_query_adapter_fail");
1877 		/* This case is not the degraded one */
1878 		return (DDI_FAILURE);
1879 	}
1880 
1881 	/* Allocate protection domain (PD) for Hermon internal use */
1882 	status = hermon_pd_alloc(state, &state->hs_pdhdl_internal,
1883 	    HERMON_SLEEP);
1884 	if (status != DDI_SUCCESS) {
1885 		cmn_err(CE_NOTE, "failed to alloc internal PD\n");
1886 		hermon_hw_fini(state, cleanup);
1887 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1888 		    "hw_init_internal_pd_alloc_fail");
1889 		/* This case is not the degraded one */
1890 		return (DDI_FAILURE);
1891 	}
1892 	cleanup = HERMON_DRV_CLEANUP_LEVEL13;
1893 
1894 	/* Setup UAR page for kernel use */
1895 	status = hermon_internal_uarpg_init(state);
1896 	if (status != DDI_SUCCESS) {
1897 		cmn_err(CE_NOTE, "failed to setup internal UAR\n");
1898 		hermon_hw_fini(state, cleanup);
1899 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1900 		    "hw_init_internal_uarpg_alloc_fail");
1901 		/* This case is not the degraded one */
1902 		return (DDI_FAILURE);
1903 	}
1904 	cleanup = HERMON_DRV_CLEANUP_LEVEL14;
1905 
1906 	/* Query and initialize the Hermon interrupt/MSI information */
1907 	status = hermon_intr_or_msi_init(state);
1908 	if (status != DDI_SUCCESS) {
1909 		cmn_err(CE_NOTE, "failed to setup INTR/MSI\n");
1910 		hermon_hw_fini(state, cleanup);
1911 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1912 		    "hw_init_intr_or_msi_init_fail");
1913 		/* This case is not the degraded one */
1914 		return (DDI_FAILURE);
1915 	}
1916 	cleanup = HERMON_DRV_CLEANUP_LEVEL15;
1917 
1918 	status = hermon_isr_init(state);	/* set up the isr */
1919 	if (status != DDI_SUCCESS) {
1920 		cmn_err(CE_NOTE, "failed to init isr\n");
1921 		hermon_hw_fini(state, cleanup);
1922 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1923 		    "hw_init_isrinit_fail");
1924 		/* This case is not the degraded one */
1925 		return (DDI_FAILURE);
1926 	}
1927 	cleanup = HERMON_DRV_CLEANUP_LEVEL16;
1928 
1929 	/* Setup the event queues */
1930 	status = hermon_eq_init_all(state);
1931 	if (status != DDI_SUCCESS) {
1932 		cmn_err(CE_NOTE, "failed to init EQs\n");
1933 		hermon_hw_fini(state, cleanup);
1934 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1935 		    "hw_init_eqinitall_fail");
1936 		/* This case is not the degraded one */
1937 		return (DDI_FAILURE);
1938 	}
1939 	cleanup = HERMON_DRV_CLEANUP_LEVEL17;
1940 
1941 
1942 
1943 	/* Reserve contexts for QP0 and QP1 */
1944 	status = hermon_special_qp_contexts_reserve(state);
1945 	if (status != DDI_SUCCESS) {
1946 		cmn_err(CE_NOTE, "failed to init special QPs\n");
1947 		hermon_hw_fini(state, cleanup);
1948 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1949 		    "hw_init_rsrv_sqp_fail");
1950 		/* This case is not the degraded one */
1951 		return (DDI_FAILURE);
1952 	}
1953 	cleanup = HERMON_DRV_CLEANUP_LEVEL18;
1954 
1955 	/* Initialize for multicast group handling */
1956 	status = hermon_mcg_init(state);
1957 	if (status != DDI_SUCCESS) {
1958 		cmn_err(CE_NOTE, "failed to init multicast\n");
1959 		hermon_hw_fini(state, cleanup);
1960 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1961 		    "hw_init_mcg_init_fail");
1962 		/* This case is not the degraded one */
1963 		return (DDI_FAILURE);
1964 	}
1965 	cleanup = HERMON_DRV_CLEANUP_LEVEL19;
1966 
1967 	/* Initialize the Hermon IB port(s) */
1968 	status = hermon_hca_port_init(state);
1969 	if (status != DDI_SUCCESS) {
1970 		cmn_err(CE_NOTE, "failed to init HCA Port\n");
1971 		hermon_hw_fini(state, cleanup);
1972 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1973 		    "hw_init_hca_port_init_fail");
1974 		/* This case is not the degraded one */
1975 		return (DDI_FAILURE);
1976 	}
1977 
1978 	cleanup = HERMON_DRV_CLEANUP_ALL;
1979 
1980 	/* Determine NodeGUID and SystemImageGUID */
1981 	status = hermon_getnodeinfo_cmd_post(state, HERMON_CMD_NOSLEEP_SPIN,
1982 	    &nodeinfo);
1983 	if (status != HERMON_CMD_SUCCESS) {
1984 		cmn_err(CE_NOTE, "GetNodeInfo command failed: %08x\n", status);
1985 		hermon_hw_fini(state, cleanup);
1986 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1987 		    "hw_init_getnodeinfo_cmd_fail");
1988 		/* This case is not the degraded one */
1989 		return (DDI_FAILURE);
1990 	}
1991 
1992 	/*
1993 	 * If the NodeGUID value was set in OBP properties, then we use that
1994 	 * value.  But we still print a message if the value we queried from
1995 	 * firmware does not match this value.
1996 	 *
1997 	 * Otherwise if OBP value is not set then we use the value from
1998 	 * firmware unconditionally.
1999 	 */
2000 	if (state->hs_cfg_profile->cp_nodeguid) {
2001 		state->hs_nodeguid   = state->hs_cfg_profile->cp_nodeguid;
2002 	} else {
2003 		state->hs_nodeguid = nodeinfo.NodeGUID;
2004 	}
2005 
2006 	if (state->hs_nodeguid != nodeinfo.NodeGUID) {
2007 		cmn_err(CE_NOTE, "!NodeGUID value queried from firmware "
2008 		    "does not match value set by device property");
2009 	}
2010 
2011 	/*
2012 	 * If the SystemImageGUID value was set in OBP properties, then we use
2013 	 * that value.  But we still print a message if the value we queried
2014 	 * from firmware does not match this value.
2015 	 *
2016 	 * Otherwise if OBP value is not set then we use the value from
2017 	 * firmware unconditionally.
2018 	 */
2019 	if (state->hs_cfg_profile->cp_sysimgguid) {
2020 		state->hs_sysimgguid = state->hs_cfg_profile->cp_sysimgguid;
2021 	} else {
2022 		state->hs_sysimgguid = nodeinfo.SystemImageGUID;
2023 	}
2024 
2025 	if (state->hs_sysimgguid != nodeinfo.SystemImageGUID) {
2026 		cmn_err(CE_NOTE, "!SystemImageGUID value queried from firmware "
2027 		    "does not match value set by device property");
2028 	}
2029 
2030 	/* Get NodeDescription */
2031 	status = hermon_getnodedesc_cmd_post(state, HERMON_CMD_NOSLEEP_SPIN,
2032 	    (sm_nodedesc_t *)&state->hs_nodedesc);
2033 	if (status != HERMON_CMD_SUCCESS) {
2034 		cmn_err(CE_CONT, "GetNodeDesc command failed: %08x\n", status);
2035 		hermon_hw_fini(state, cleanup);
2036 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2037 		    "hw_init_getnodedesc_cmd_fail");
2038 		/* This case is not the degraded one */
2039 		return (DDI_FAILURE);
2040 	}
2041 
2042 	return (DDI_SUCCESS);
2043 }
2044 
2045 
2046 /*
2047  * hermon_hw_fini()
2048  *    Context: Only called from attach() and/or detach() path contexts
2049  */
2050 static void
2051 hermon_hw_fini(hermon_state_t *state, hermon_drv_cleanup_level_t cleanup)
2052 {
2053 	uint_t		num_ports;
2054 	int		i, status;
2055 
2056 
2057 	/*
2058 	 * JBDB - We might not want to run these returns in all cases of
2059 	 * Bad News. We should still attempt to free all of the DMA memory
2060 	 * resources...  This needs to be worked last, after all allocations
2061 	 * are implemented. For now, and possibly for later, this works.
2062 	 */
2063 
2064 	switch (cleanup) {
2065 	/*
2066 	 * If we add more driver initialization steps that should be cleaned
2067 	 * up here, we need to ensure that HERMON_DRV_CLEANUP_ALL is still the
2068 	 * first entry (i.e. corresponds to the last init step).
2069 	 */
2070 	case HERMON_DRV_CLEANUP_ALL:
2071 		/* Shutdown the Hermon IB port(s) */
2072 		num_ports = state->hs_cfg_profile->cp_num_ports;
2073 		(void) hermon_hca_ports_shutdown(state, num_ports);
2074 		/* FALLTHROUGH */
2075 
2076 	case HERMON_DRV_CLEANUP_LEVEL19:
2077 		/* Teardown resources used for multicast group handling */
2078 		hermon_mcg_fini(state);
2079 		/* FALLTHROUGH */
2080 
2081 	case HERMON_DRV_CLEANUP_LEVEL18:
2082 		/* Unreserve the special QP contexts */
2083 		hermon_special_qp_contexts_unreserve(state);
2084 		/* FALLTHROUGH */
2085 
2086 	case HERMON_DRV_CLEANUP_LEVEL17:
2087 		/*
2088 		 * Attempt to teardown all event queues (EQ).  If we fail
2089 		 * here then print a warning message and return.  Something
2090 		 * (either in HW or SW) has gone seriously wrong.
2091 		 */
2092 		status = hermon_eq_fini_all(state);
2093 		if (status != DDI_SUCCESS) {
2094 			HERMON_WARNING(state, "failed to teardown EQs");
2095 			return;
2096 		}
2097 		/* FALLTHROUGH */
2098 	case HERMON_DRV_CLEANUP_LEVEL16:
2099 		/* Teardown Hermon interrupts */
2100 		hermon_isr_fini(state);
2101 		/* FALLTHROUGH */
2102 
2103 	case HERMON_DRV_CLEANUP_LEVEL15:
2104 		status = hermon_intr_or_msi_fini(state);
2105 		if (status != DDI_SUCCESS) {
2106 			HERMON_WARNING(state, "failed to free intr/MSI");
2107 			return;
2108 		}
2109 		/* FALLTHROUGH */
2110 
2111 	case HERMON_DRV_CLEANUP_LEVEL14:
2112 		/* Free the resources for the Hermon internal UAR pages */
2113 		hermon_internal_uarpg_fini(state);
2114 		/* FALLTHROUGH */
2115 
2116 	case HERMON_DRV_CLEANUP_LEVEL13:
2117 		/*
2118 		 * Free the PD that was used internally by Hermon software.  If
2119 		 * we fail here then print a warning and return.  Something
2120 		 * (probably software-related, but perhaps HW) has gone wrong.
2121 		 */
2122 		status = hermon_pd_free(state, &state->hs_pdhdl_internal);
2123 		if (status != DDI_SUCCESS) {
2124 			HERMON_WARNING(state, "failed to free internal PD");
2125 			return;
2126 		}
2127 		/* FALLTHROUGH */
2128 
2129 	case HERMON_DRV_CLEANUP_LEVEL12:
2130 		/* Cleanup all the phase2 resources first */
2131 		hermon_rsrc_fini(state, HERMON_RSRC_CLEANUP_ALL);
2132 		/* FALLTHROUGH */
2133 
2134 	case HERMON_DRV_CLEANUP_LEVEL11:
2135 		/* LEVEL11 is after INIT_HCA */
2136 		/* FALLTHROUGH */
2137 
2138 
2139 	case HERMON_DRV_CLEANUP_LEVEL10:
2140 		/*
2141 		 * Unmap the ICM memory area with UNMAP_ICM command.
2142 		 */
2143 		status = hermon_unmap_icm_cmd_post(state, NULL);
2144 		if (status != DDI_SUCCESS) {
2145 			cmn_err(CE_WARN,
2146 			    "hermon_hw_fini: failed to unmap ICM\n");
2147 		}
2148 
2149 		/* Free the initial ICM DMA handles */
2150 		hermon_icm_dma_fini(state);
2151 
2152 		/* Free the ICM table structures */
2153 		hermon_icm_tables_fini(state);
2154 
2155 		/* Free the ICM table handles */
2156 		kmem_free(state->hs_icm, HERMON_NUM_ICM_RESOURCES *
2157 		    sizeof (hermon_icm_table_t));
2158 
2159 		/* FALLTHROUGH */
2160 
2161 	case HERMON_DRV_CLEANUP_LEVEL9:
2162 		/*
2163 		 * Unmap the ICM Aux memory area with UNMAP_ICM_AUX command.
2164 		 */
2165 		status = hermon_unmap_icm_aux_cmd_post(state);
2166 		if (status != HERMON_CMD_SUCCESS) {
2167 			cmn_err(CE_NOTE,
2168 			    "hermon_hw_fini: failed to unmap ICMA\n");
2169 		}
2170 		/* FALLTHROUGH */
2171 
2172 	case HERMON_DRV_CLEANUP_LEVEL8:
2173 		/*
2174 		 * Deallocate ICM Aux DMA memory.
2175 		 */
2176 		hermon_dma_free(&state->hs_icma_dma);
2177 		/* FALLTHROUGH */
2178 
2179 	case HERMON_DRV_CLEANUP_LEVEL7:
2180 		if (state->hs_fm_uarhdl) {
2181 			hermon_regs_map_free(state, &state->hs_fm_uarhdl);
2182 			state->hs_fm_uarhdl = NULL;
2183 		}
2184 
2185 		if (state->hs_reg_uarhdl) {
2186 			ddi_regs_map_free(&state->hs_reg_uarhdl);
2187 			state->hs_reg_uarhdl = NULL;
2188 		}
2189 
2190 		if (state->hs_bf_offset != 0 && state->hs_reg_bfhdl) {
2191 			ddi_regs_map_free(&state->hs_reg_bfhdl);
2192 			state->hs_reg_bfhdl = NULL;
2193 		}
2194 
2195 		for (i = 0; i < HERMON_MAX_PORTS; i++) {
2196 			if (state->hs_pkey[i]) {
2197 				kmem_free(state->hs_pkey[i], (1 <<
2198 				    state->hs_cfg_profile->cp_log_max_pkeytbl) *
2199 				    sizeof (ib_pkey_t));
2200 				state->hs_pkey[i] = NULL;
2201 			}
2202 			if (state->hs_guid[i]) {
2203 				kmem_free(state->hs_guid[i], (1 <<
2204 				    state->hs_cfg_profile->cp_log_max_gidtbl) *
2205 				    sizeof (ib_guid_t));
2206 				state->hs_guid[i] = NULL;
2207 			}
2208 		}
2209 		/* FALLTHROUGH */
2210 
2211 	case HERMON_DRV_CLEANUP_LEVEL6:
2212 		/*
2213 		 * Unmap the firmware memory area with UNMAP_FA command.
2214 		 */
2215 		status = hermon_unmap_fa_cmd_post(state);
2216 
2217 		if (status != HERMON_CMD_SUCCESS) {
2218 			cmn_err(CE_NOTE,
2219 			    "hermon_hw_fini: failed to unmap FW\n");
2220 		}
2221 
2222 		/*
2223 		 * Deallocate firmware DMA memory.
2224 		 */
2225 		hermon_dma_free(&state->hs_fw_dma);
2226 		/* FALLTHROUGH */
2227 
2228 	case HERMON_DRV_CLEANUP_LEVEL5:
2229 		/* stop the poll thread */
2230 		if (state->hs_fm_poll_thread) {
2231 			ddi_periodic_delete(state->hs_fm_poll_thread);
2232 			state->hs_fm_poll_thread = NULL;
2233 		}
2234 		/* FALLTHROUGH */
2235 
2236 	case HERMON_DRV_CLEANUP_LEVEL4:
2237 		/* Then cleanup the phase1 resources */
2238 		hermon_rsrc_fini(state, HERMON_RSRC_CLEANUP_PHASE1_COMPLETE);
2239 		/* FALLTHROUGH */
2240 
2241 	case HERMON_DRV_CLEANUP_LEVEL3:
2242 		/* Teardown any resources allocated for the config profile */
2243 		hermon_cfg_profile_fini(state);
2244 		/* FALLTHROUGH */
2245 
2246 	case HERMON_DRV_CLEANUP_LEVEL2:
2247 #ifdef HERMON_SUPPORTS_MSIX_BAR
2248 		/*
2249 		 * unmap 3rd BAR, MSIX BAR
2250 		 */
2251 		if (state->hs_reg_msihdl) {
2252 			ddi_regs_map_free(&state->hs_reg_msihdl);
2253 			state->hs_reg_msihdl = NULL;
2254 		}
2255 		/* FALLTHROUGH */
2256 #endif
2257 	case HERMON_DRV_CLEANUP_LEVEL1:
2258 	case HERMON_DRV_CLEANUP_LEVEL0:
2259 		/*
2260 		 * LEVEL1 and LEVEL0 resources are freed in
2261 		 * hermon_drv_fini2().
2262 		 */
2263 		break;
2264 
2265 	default:
2266 		HERMON_WARNING(state, "unexpected driver cleanup level");
2267 		return;
2268 	}
2269 }
2270 
2271 
2272 /*
2273  * hermon_soft_state_init()
2274  *    Context: Only called from attach() path context
2275  */
2276 static int
2277 hermon_soft_state_init(hermon_state_t *state)
2278 {
2279 	ibt_hca_attr_t		*hca_attr;
2280 	uint64_t		maxval, val;
2281 	ibt_hca_flags_t		caps = IBT_HCA_NO_FLAGS;
2282 	ibt_hca_flags2_t	caps2 = IBT_HCA2_NO_FLAGS;
2283 	int			status;
2284 	int			max_send_wqe_bytes;
2285 	int			max_recv_wqe_bytes;
2286 
2287 	/*
2288 	 * The ibc_hca_info_t struct is passed to the IBTF.  This is the
2289 	 * routine where we initialize it.  Many of the init values come from
2290 	 * either configuration variables or successful queries of the Hermon
2291 	 * hardware abilities
2292 	 */
2293 	state->hs_ibtfinfo.hca_ci_vers	= IBCI_V3;
2294 	state->hs_ibtfinfo.hca_dip	= state->hs_dip;
2295 	state->hs_ibtfinfo.hca_handle	= (ibc_hca_hdl_t)state;
2296 	state->hs_ibtfinfo.hca_ops	= &hermon_ibc_ops;
2297 
2298 	hca_attr = kmem_zalloc(sizeof (ibt_hca_attr_t), KM_SLEEP);
2299 	state->hs_ibtfinfo.hca_attr = hca_attr;
2300 
2301 	hca_attr->hca_fw_major_version = state->hs_fw.fw_rev_major;
2302 	hca_attr->hca_fw_minor_version = state->hs_fw.fw_rev_minor;
2303 	hca_attr->hca_fw_micro_version = state->hs_fw.fw_rev_subminor;
2304 
2305 	/* CQ interrupt moderation maximums - each limited to 16 bits */
2306 	hca_attr->hca_max_cq_mod_count = 0xFFFF;
2307 	hca_attr->hca_max_cq_mod_usec = 0xFFFF;
2308 
2309 	/* CQ relocation to other EQs - change when multiple MSI-Xs are used */
2310 	hca_attr->hca_max_cq_handlers = 1;
2311 
2312 	/*
2313 	 * Determine HCA capabilities:
2314 	 * No default support for IBT_HCA_RD, IBT_HCA_RAW_MULTICAST,
2315 	 *    IBT_HCA_ATOMICS_GLOBAL, IBT_HCA_RESIZE_CHAN, IBT_HCA_INIT_TYPE,
2316 	 *    or IBT_HCA_SHUTDOWN_PORT
2317 	 * But IBT_HCA_AH_PORT_CHECK, IBT_HCA_SQD_RTS_PORT, IBT_HCA_SI_GUID,
2318 	 *    IBT_HCA_RNR_NAK, IBT_HCA_CURRENT_QP_STATE, IBT_HCA_PORT_UP,
2319 	 *    IBT_HCA_SRQ, IBT_HCA_RESIZE_SRQ and IBT_HCA_FMR are always
2320 	 *    supported
2321 	 * All other features are conditionally supported, depending on the
2322 	 *    status return by the Hermon HCA in QUERY_DEV_LIM.
2323 	 */
2324 	if (state->hs_devlim.ud_multi) {
2325 		caps |= IBT_HCA_UD_MULTICAST;
2326 	}
2327 	if (state->hs_devlim.atomic) {
2328 		caps |= IBT_HCA_ATOMICS_HCA;
2329 	}
2330 	if (state->hs_devlim.apm) {
2331 		caps |= IBT_HCA_AUTO_PATH_MIG;
2332 	}
2333 	if (state->hs_devlim.pkey_v) {
2334 		caps |= IBT_HCA_PKEY_CNTR;
2335 	}
2336 	if (state->hs_devlim.qkey_v) {
2337 		caps |= IBT_HCA_QKEY_CNTR;
2338 	}
2339 	if (state->hs_devlim.ipoib_cksm) {
2340 		caps |= IBT_HCA_CKSUM_FULL;
2341 		caps2 |= IBT_HCA2_IP_CLASS;
2342 	}
2343 	if (state->hs_devlim.mod_wr_srq) {
2344 		caps |= IBT_HCA_RESIZE_SRQ;
2345 	}
2346 	if (state->hs_devlim.lif) {
2347 		caps |= IBT_HCA_LOCAL_INVAL_FENCE;
2348 	}
2349 	if (state->hs_devlim.reserved_lkey) {
2350 		caps2 |= IBT_HCA2_RES_LKEY;
2351 		hca_attr->hca_reserved_lkey = state->hs_devlim.rsv_lkey;
2352 	}
2353 	if (state->hs_devlim.local_inv && state->hs_devlim.remote_inv &&
2354 	    state->hs_devlim.fast_reg_wr) {	/* fw needs to be >= 2.6.636 */
2355 		if (state->hs_fw.fw_rev_major > 2)
2356 			caps2 |= IBT_HCA2_MEM_MGT_EXT;
2357 		else if (state->hs_fw.fw_rev_major == 2)
2358 			if (state->hs_fw.fw_rev_minor > 6)
2359 				caps2 |= IBT_HCA2_MEM_MGT_EXT;
2360 			else if (state->hs_fw.fw_rev_minor == 6)
2361 				if (state->hs_fw.fw_rev_subminor >= 636)
2362 					caps2 |= IBT_HCA2_MEM_MGT_EXT;
2363 	}
2364 	if (state->hs_devlim.mps) {
2365 		caps |= IBT_HCA_ZERO_BASED_VA;
2366 	}
2367 	if (state->hs_devlim.zb) {
2368 		caps |= IBT_HCA_MULT_PAGE_SZ_MR;
2369 	}
2370 	caps |= (IBT_HCA_AH_PORT_CHECK | IBT_HCA_SQD_SQD_PORT |
2371 	    IBT_HCA_SI_GUID | IBT_HCA_RNR_NAK | IBT_HCA_CURRENT_QP_STATE |
2372 	    IBT_HCA_PORT_UP | IBT_HCA_RC_SRQ | IBT_HCA_UD_SRQ | IBT_HCA_FMR);
2373 
2374 	if (state->hs_devlim.log_max_gso_sz) {
2375 		hca_attr->hca_max_lso_size =
2376 		    (1 << state->hs_devlim.log_max_gso_sz);
2377 		/* More work needed in hermon_post_send for larger values */
2378 		hca_attr->hca_max_lso_hdr_size = 0x2c;	/* IPv4 only */
2379 	}
2380 
2381 	caps |= IBT_HCA_WQE_SIZE_INFO;
2382 	max_send_wqe_bytes = state->hs_devlim.max_desc_sz_sq;
2383 	max_recv_wqe_bytes = state->hs_devlim.max_desc_sz_rq;
2384 	hca_attr->hca_ud_send_sgl_sz = (max_send_wqe_bytes / 16) - 4;
2385 	hca_attr->hca_conn_send_sgl_sz = (max_send_wqe_bytes / 16) - 1;
2386 	hca_attr->hca_conn_rdma_sgl_overhead = 1;
2387 	hca_attr->hca_recv_sgl_sz = max_recv_wqe_bytes / 16;
2388 
2389 	/* We choose not to support "inline" unless it improves performance */
2390 	hca_attr->hca_max_inline_size = 0;
2391 	hca_attr->hca_ud_send_inline_sz = 0;
2392 	hca_attr->hca_conn_send_inline_sz = 0;
2393 	hca_attr->hca_conn_rdmaw_inline_overhead = 4;
2394 
2395 	hca_attr->hca_flags = caps;
2396 	hca_attr->hca_flags2 = caps2;
2397 
2398 	/*
2399 	 * Set hca_attr's IDs
2400 	 */
2401 	hca_attr->hca_vendor_id	 = state->hs_vendor_id;
2402 	hca_attr->hca_device_id	 = state->hs_device_id;
2403 	hca_attr->hca_version_id = state->hs_revision_id;
2404 
2405 	/*
2406 	 * Determine number of available QPs and max QP size.  Number of
2407 	 * available QPs is determined by subtracting the number of
2408 	 * "reserved QPs" (i.e. reserved for firmware use) from the
2409 	 * total number configured.
2410 	 */
2411 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_qp);
2412 	hca_attr->hca_max_qp = val - ((uint64_t)1 <<
2413 	    state->hs_devlim.log_rsvd_qp);
2414 	maxval	= ((uint64_t)1 << state->hs_devlim.log_max_qp_sz);
2415 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_qp_sz);
2416 	if (val > maxval) {
2417 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2418 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2419 		    "soft_state_init_maxqpsz_toobig_fail");
2420 		return (DDI_FAILURE);
2421 	}
2422 	/* we need to reduce this by the max space needed for headroom */
2423 	hca_attr->hca_max_qp_sz = (uint_t)val - (HERMON_QP_OH_SIZE >>
2424 	    HERMON_QP_WQE_LOG_MINIMUM) - 1;
2425 
2426 	/*
2427 	 * Determine max scatter-gather size in WQEs. The HCA has split
2428 	 * the max sgl into rec'v Q and send Q values. Use the least.
2429 	 *
2430 	 * This is mainly useful for legacy clients.  Smart clients
2431 	 * such as IPoIB will use the IBT_HCA_WQE_SIZE_INFO sgl info.
2432 	 */
2433 	if (state->hs_devlim.max_sg_rq <= state->hs_devlim.max_sg_sq) {
2434 		maxval = state->hs_devlim.max_sg_rq;
2435 	} else {
2436 		maxval = state->hs_devlim.max_sg_sq;
2437 	}
2438 	val	= state->hs_cfg_profile->cp_wqe_max_sgl;
2439 	if (val > maxval) {
2440 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2441 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2442 		    "soft_state_init_toomanysgl_fail");
2443 		return (DDI_FAILURE);
2444 	}
2445 	/* If the rounded value for max SGL is too large, cap it */
2446 	if (state->hs_cfg_profile->cp_wqe_real_max_sgl > maxval) {
2447 		state->hs_cfg_profile->cp_wqe_real_max_sgl = (uint32_t)maxval;
2448 		val = maxval;
2449 	} else {
2450 		val = state->hs_cfg_profile->cp_wqe_real_max_sgl;
2451 	}
2452 
2453 	hca_attr->hca_max_sgl	 = (uint_t)val;
2454 	hca_attr->hca_max_rd_sgl = 0;	/* zero because RD is unsupported */
2455 
2456 	/*
2457 	 * Determine number of available CQs and max CQ size. Number of
2458 	 * available CQs is determined by subtracting the number of
2459 	 * "reserved CQs" (i.e. reserved for firmware use) from the
2460 	 * total number configured.
2461 	 */
2462 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_cq);
2463 	hca_attr->hca_max_cq = val - ((uint64_t)1 <<
2464 	    state->hs_devlim.log_rsvd_cq);
2465 	maxval	= ((uint64_t)1 << state->hs_devlim.log_max_cq_sz);
2466 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_cq_sz) - 1;
2467 	if (val > maxval) {
2468 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2469 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2470 		    "soft_state_init_maxcqsz_toobig_fail");
2471 		return (DDI_FAILURE);
2472 	}
2473 	hca_attr->hca_max_cq_sz = (uint_t)val;
2474 
2475 	/*
2476 	 * Determine number of available SRQs and max SRQ size. Number of
2477 	 * available SRQs is determined by subtracting the number of
2478 	 * "reserved SRQs" (i.e. reserved for firmware use) from the
2479 	 * total number configured.
2480 	 */
2481 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_srq);
2482 	hca_attr->hca_max_srqs = val - ((uint64_t)1 <<
2483 	    state->hs_devlim.log_rsvd_srq);
2484 	maxval  = ((uint64_t)1 << state->hs_devlim.log_max_srq_sz);
2485 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_srq_sz);
2486 
2487 	if (val > maxval) {
2488 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2489 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2490 		    "soft_state_init_maxsrqsz_toobig_fail");
2491 		return (DDI_FAILURE);
2492 	}
2493 	hca_attr->hca_max_srqs_sz = (uint_t)val;
2494 
2495 	val	= hca_attr->hca_recv_sgl_sz - 1; /* SRQ has a list link */
2496 	maxval	= state->hs_devlim.max_sg_rq - 1;
2497 	if (val > maxval) {
2498 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2499 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2500 		    "soft_state_init_toomanysrqsgl_fail");
2501 		return (DDI_FAILURE);
2502 	}
2503 	hca_attr->hca_max_srq_sgl = (uint_t)val;
2504 
2505 	/*
2506 	 * Determine supported HCA page sizes
2507 	 * XXX
2508 	 * For now we simply return the system pagesize as the only supported
2509 	 * pagesize
2510 	 */
2511 	hca_attr->hca_page_sz = ((PAGESIZE == (1 << 13)) ? IBT_PAGE_8K :
2512 	    IBT_PAGE_4K);
2513 
2514 	/*
2515 	 * Determine number of available MemReg, MemWin, and their max size.
2516 	 * Number of available MRs and MWs is determined by subtracting
2517 	 * the number of "reserved MPTs" (i.e. reserved for firmware use)
2518 	 * from the total number configured for each.
2519 	 */
2520 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_dmpt);
2521 	hca_attr->hca_max_memr	  = val - ((uint64_t)1 <<
2522 	    state->hs_devlim.log_rsvd_dmpt);
2523 	hca_attr->hca_max_mem_win = state->hs_devlim.mem_win ? (val -
2524 	    ((uint64_t)1 << state->hs_devlim.log_rsvd_dmpt)) : 0;
2525 	maxval	= state->hs_devlim.log_max_mrw_sz;
2526 	val	= state->hs_cfg_profile->cp_log_max_mrw_sz;
2527 	if (val > maxval) {
2528 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2529 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2530 		    "soft_state_init_maxmrwsz_toobig_fail");
2531 		return (DDI_FAILURE);
2532 	}
2533 	hca_attr->hca_max_memr_len = ((uint64_t)1 << val);
2534 
2535 	/* Determine RDMA/Atomic properties */
2536 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_rdb);
2537 	hca_attr->hca_max_rsc = (uint_t)val;
2538 	val = state->hs_cfg_profile->cp_hca_max_rdma_in_qp;
2539 	hca_attr->hca_max_rdma_in_qp  = (uint8_t)val;
2540 	val = state->hs_cfg_profile->cp_hca_max_rdma_out_qp;
2541 	hca_attr->hca_max_rdma_out_qp = (uint8_t)val;
2542 	hca_attr->hca_max_rdma_in_ee  = 0;
2543 	hca_attr->hca_max_rdma_out_ee = 0;
2544 
2545 	/*
2546 	 * Determine maximum number of raw IPv6 and Ether QPs.  Set to 0
2547 	 * because neither type of raw QP is supported
2548 	 */
2549 	hca_attr->hca_max_ipv6_qp  = 0;
2550 	hca_attr->hca_max_ether_qp = 0;
2551 
2552 	/* Determine max number of MCGs and max QP-per-MCG */
2553 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_qp);
2554 	hca_attr->hca_max_mcg_qps   = (uint_t)val;
2555 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_mcg);
2556 	hca_attr->hca_max_mcg	    = (uint_t)val;
2557 	val = state->hs_cfg_profile->cp_num_qp_per_mcg;
2558 	hca_attr->hca_max_qp_per_mcg = (uint_t)val;
2559 
2560 	/* Determine max number partitions (i.e. PKeys) */
2561 	maxval	= ((uint64_t)state->hs_cfg_profile->cp_num_ports <<
2562 	    state->hs_queryport.log_max_pkey);
2563 	val	= ((uint64_t)state->hs_cfg_profile->cp_num_ports <<
2564 	    state->hs_cfg_profile->cp_log_max_pkeytbl);
2565 
2566 	if (val > maxval) {
2567 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2568 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2569 		    "soft_state_init_toomanypkey_fail");
2570 		return (DDI_FAILURE);
2571 	}
2572 	hca_attr->hca_max_partitions = (uint16_t)val;
2573 
2574 	/* Determine number of ports */
2575 	maxval = state->hs_devlim.num_ports;
2576 	val = state->hs_cfg_profile->cp_num_ports;
2577 	if ((val > maxval) || (val == 0)) {
2578 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2579 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2580 		    "soft_state_init_toomanyports_fail");
2581 		return (DDI_FAILURE);
2582 	}
2583 	hca_attr->hca_nports = (uint8_t)val;
2584 
2585 	/* Copy NodeGUID and SystemImageGUID from softstate */
2586 	hca_attr->hca_node_guid = state->hs_nodeguid;
2587 	hca_attr->hca_si_guid	= state->hs_sysimgguid;
2588 
2589 	/*
2590 	 * Determine local ACK delay.  Use the value suggested by the Hermon
2591 	 * hardware (from the QUERY_DEV_CAP command)
2592 	 */
2593 	hca_attr->hca_local_ack_delay = state->hs_devlim.ca_ack_delay;
2594 
2595 	/* Determine max SGID table and PKey table sizes */
2596 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_gidtbl);
2597 	hca_attr->hca_max_port_sgid_tbl_sz = (uint_t)val;
2598 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_pkeytbl);
2599 	hca_attr->hca_max_port_pkey_tbl_sz = (uint16_t)val;
2600 
2601 	/* Determine max number of PDs */
2602 	maxval	= ((uint64_t)1 << state->hs_devlim.log_max_pd);
2603 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_pd);
2604 	if (val > maxval) {
2605 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2606 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2607 		    "soft_state_init_toomanypd_fail");
2608 		return (DDI_FAILURE);
2609 	}
2610 	hca_attr->hca_max_pd = (uint_t)val;
2611 
2612 	/* Determine max number of Address Handles (NOT IN ARBEL or HERMON) */
2613 	hca_attr->hca_max_ah = 0;
2614 
2615 	/* No RDDs or EECs (since Reliable Datagram is not supported) */
2616 	hca_attr->hca_max_rdd = 0;
2617 	hca_attr->hca_max_eec = 0;
2618 
2619 	/* Initialize lock for reserved UAR page access */
2620 	mutex_init(&state->hs_uar_lock, NULL, MUTEX_DRIVER,
2621 	    DDI_INTR_PRI(state->hs_intrmsi_pri));
2622 
2623 	/* Initialize the flash fields */
2624 	state->hs_fw_flashstarted = 0;
2625 	mutex_init(&state->hs_fw_flashlock, NULL, MUTEX_DRIVER,
2626 	    DDI_INTR_PRI(state->hs_intrmsi_pri));
2627 
2628 	/* Initialize the lock for the info ioctl */
2629 	mutex_init(&state->hs_info_lock, NULL, MUTEX_DRIVER,
2630 	    DDI_INTR_PRI(state->hs_intrmsi_pri));
2631 
2632 	/* Initialize the AVL tree for QP number support */
2633 	hermon_qpn_avl_init(state);
2634 
2635 	/* Initialize the kstat info structure */
2636 	status = hermon_kstat_init(state);
2637 	if (status != DDI_SUCCESS) {
2638 		hermon_qpn_avl_fini(state);
2639 		mutex_destroy(&state->hs_info_lock);
2640 		mutex_destroy(&state->hs_fw_flashlock);
2641 		mutex_destroy(&state->hs_uar_lock);
2642 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2643 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2644 		    "soft_state_init_kstatinit_fail");
2645 		return (DDI_FAILURE);
2646 	}
2647 
2648 	return (DDI_SUCCESS);
2649 }
2650 
2651 
2652 /*
2653  * hermon_soft_state_fini()
2654  *    Context: Called only from detach() path context
2655  */
2656 static void
2657 hermon_soft_state_fini(hermon_state_t *state)
2658 {
2659 
2660 	/* Teardown the kstat info */
2661 	hermon_kstat_fini(state);
2662 
2663 	/* Teardown the AVL tree for QP number support */
2664 	hermon_qpn_avl_fini(state);
2665 
2666 	/* Free up info ioctl mutex */
2667 	mutex_destroy(&state->hs_info_lock);
2668 
2669 	/* Free up flash mutex */
2670 	mutex_destroy(&state->hs_fw_flashlock);
2671 
2672 	/* Free up the UAR page access mutex */
2673 	mutex_destroy(&state->hs_uar_lock);
2674 
2675 	/* Free up the hca_attr struct */
2676 	kmem_free(state->hs_ibtfinfo.hca_attr, sizeof (ibt_hca_attr_t));
2677 
2678 }
2679 
2680 /*
2681  * hermon_icm_config_setup()
2682  *    Context: Only called from attach() path context
2683  */
2684 static int
2685 hermon_icm_config_setup(hermon_state_t *state,
2686     hermon_hw_initqueryhca_t *inithca)
2687 {
2688 	hermon_hw_querydevlim_t	*devlim;
2689 	hermon_cfg_profile_t	*cfg;
2690 	hermon_icm_table_t	*icm_p[HERMON_NUM_ICM_RESOURCES];
2691 	hermon_icm_table_t	*icm;
2692 	hermon_icm_table_t	*tmp;
2693 	uint64_t		icm_addr;
2694 	uint64_t		icm_size;
2695 	int			status, i, j;
2696 
2697 
2698 	/* Bring in local devlims, cfg_profile and hs_icm table list */
2699 	devlim = &state->hs_devlim;
2700 	cfg = state->hs_cfg_profile;
2701 	icm = state->hs_icm;
2702 
2703 	/*
2704 	 * Assign each ICM table's entry size from data in the devlims,
2705 	 * except for RDB and MCG sizes, which are not returned in devlims
2706 	 * but do have a fixed size, and the UAR context entry size, which
2707 	 * we determine. For this, we use the "cp_num_pgs_per_uce" value
2708 	 * from our hs_cfg_profile.
2709 	 */
2710 	icm[HERMON_CMPT].object_size	= devlim->cmpt_entry_sz;
2711 	icm[HERMON_CMPT_QPC].object_size	= devlim->cmpt_entry_sz;
2712 	icm[HERMON_CMPT_SRQC].object_size	= devlim->cmpt_entry_sz;
2713 	icm[HERMON_CMPT_CQC].object_size	= devlim->cmpt_entry_sz;
2714 	icm[HERMON_CMPT_EQC].object_size	= devlim->cmpt_entry_sz;
2715 	icm[HERMON_MTT].object_size	= devlim->mtt_entry_sz;
2716 	icm[HERMON_DMPT].object_size	= devlim->dmpt_entry_sz;
2717 	icm[HERMON_QPC].object_size	= devlim->qpc_entry_sz;
2718 	icm[HERMON_CQC].object_size	= devlim->cqc_entry_sz;
2719 	icm[HERMON_SRQC].object_size	= devlim->srq_entry_sz;
2720 	icm[HERMON_EQC].object_size	= devlim->eqc_entry_sz;
2721 	icm[HERMON_RDB].object_size	= devlim->rdmardc_entry_sz *
2722 	    cfg->cp_hca_max_rdma_in_qp;
2723 	icm[HERMON_MCG].object_size	= HERMON_MCGMEM_SZ(state);
2724 	icm[HERMON_ALTC].object_size	= devlim->altc_entry_sz;
2725 	icm[HERMON_AUXC].object_size	= devlim->aux_entry_sz;
2726 
2727 	/* Assign each ICM table's log2 number of entries */
2728 	icm[HERMON_CMPT].log_num_entries = cfg->cp_log_num_cmpt;
2729 	icm[HERMON_CMPT_QPC].log_num_entries = cfg->cp_log_num_qp;
2730 	icm[HERMON_CMPT_SRQC].log_num_entries = cfg->cp_log_num_srq;
2731 	icm[HERMON_CMPT_CQC].log_num_entries = cfg->cp_log_num_cq;
2732 	icm[HERMON_CMPT_EQC].log_num_entries = HERMON_NUM_EQ_SHIFT;
2733 	icm[HERMON_MTT].log_num_entries	= cfg->cp_log_num_mtt;
2734 	icm[HERMON_DMPT].log_num_entries = cfg->cp_log_num_dmpt;
2735 	icm[HERMON_QPC].log_num_entries	= cfg->cp_log_num_qp;
2736 	icm[HERMON_SRQC].log_num_entries = cfg->cp_log_num_srq;
2737 	icm[HERMON_CQC].log_num_entries	= cfg->cp_log_num_cq;
2738 	icm[HERMON_EQC].log_num_entries	= HERMON_NUM_EQ_SHIFT;
2739 	icm[HERMON_RDB].log_num_entries	= cfg->cp_log_num_qp;
2740 	icm[HERMON_MCG].log_num_entries	= cfg->cp_log_num_mcg;
2741 	icm[HERMON_ALTC].log_num_entries = cfg->cp_log_num_qp;
2742 	icm[HERMON_AUXC].log_num_entries = cfg->cp_log_num_qp;
2743 
2744 	/* Initialize the ICM tables */
2745 	hermon_icm_tables_init(state);
2746 
2747 	/*
2748 	 * ICM tables must be aligned on their size in the ICM address
2749 	 * space. So, here we order the tables from largest total table
2750 	 * size to the smallest. All tables are a power of 2 in size, so
2751 	 * this will ensure that all tables are aligned on their own size
2752 	 * without wasting space in the ICM.
2753 	 *
2754 	 * In order to easily set the ICM addresses without needing to
2755 	 * worry about the ordering of our table indices as relates to
2756 	 * the hermon_rsrc_type_t enum, we will use a list of pointers
2757 	 * representing the tables for the sort, then assign ICM addresses
2758 	 * below using it.
2759 	 */
2760 	for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) {
2761 		icm_p[i] = &icm[i];
2762 	}
2763 	for (i = HERMON_NUM_ICM_RESOURCES; i > 0; i--) {
2764 		switch (i) {
2765 		case HERMON_CMPT_QPC:
2766 		case HERMON_CMPT_SRQC:
2767 		case HERMON_CMPT_CQC:
2768 		case HERMON_CMPT_EQC:
2769 			continue;
2770 		}
2771 		for (j = 1; j < i; j++) {
2772 			if (icm_p[j]->table_size > icm_p[j - 1]->table_size) {
2773 				tmp		= icm_p[j];
2774 				icm_p[j]	= icm_p[j - 1];
2775 				icm_p[j - 1]	= tmp;
2776 			}
2777 		}
2778 	}
2779 
2780 	/* Initialize the ICM address and ICM size */
2781 	icm_addr = icm_size = 0;
2782 
2783 	/*
2784 	 * Set the ICM base address of each table, using our sorted
2785 	 * list of pointers from above.
2786 	 */
2787 	for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) {
2788 		j = icm_p[i]->icm_type;
2789 		switch (j) {
2790 		case HERMON_CMPT_QPC:
2791 		case HERMON_CMPT_SRQC:
2792 		case HERMON_CMPT_CQC:
2793 		case HERMON_CMPT_EQC:
2794 			continue;
2795 		}
2796 		if (icm[j].table_size) {
2797 			/*
2798 			 * Set the ICM base address in the table, save the
2799 			 * ICM offset in the rsrc pool and increment the
2800 			 * total ICM allocation.
2801 			 */
2802 			icm[j].icm_baseaddr = icm_addr;
2803 			if (hermon_verbose) {
2804 				IBTF_DPRINTF_L2("ICMADDR", "rsrc %x @ %p"
2805 				    " size %llx", j, icm[j].icm_baseaddr,
2806 				    icm[j].table_size);
2807 			}
2808 			icm_size += icm[j].table_size;
2809 		}
2810 
2811 		/* Verify that we don't exceed maximum ICM size */
2812 		if (icm_size > devlim->max_icm_size) {
2813 			/* free the ICM table memory resources */
2814 			hermon_icm_tables_fini(state);
2815 			cmn_err(CE_WARN, "ICM configuration exceeds maximum "
2816 			    "configuration: max (0x%lx) requested (0x%lx)\n",
2817 			    (ulong_t)devlim->max_icm_size, (ulong_t)icm_size);
2818 			HERMON_ATTACH_MSG(state->hs_attach_buf,
2819 			    "icm_config_toobig_fail");
2820 			return (DDI_FAILURE);
2821 		}
2822 
2823 		/* assign address to the 4 pieces of the CMPT */
2824 		if (j == HERMON_CMPT) {
2825 			uint64_t cmpt_size = icm[j].table_size >> 2;
2826 #define	init_cmpt_icm_baseaddr(rsrc, indx)				\
2827 	icm[rsrc].icm_baseaddr	= icm_addr + (indx * cmpt_size);
2828 			init_cmpt_icm_baseaddr(HERMON_CMPT_QPC, 0);
2829 			init_cmpt_icm_baseaddr(HERMON_CMPT_SRQC, 1);
2830 			init_cmpt_icm_baseaddr(HERMON_CMPT_CQC, 2);
2831 			init_cmpt_icm_baseaddr(HERMON_CMPT_EQC, 3);
2832 		}
2833 
2834 		/* Increment the ICM address for the next table */
2835 		icm_addr += icm[j].table_size;
2836 	}
2837 
2838 	/* Populate the structure for the INIT_HCA command */
2839 	hermon_inithca_set(state, inithca);
2840 
2841 	/*
2842 	 * Prior to invoking INIT_HCA, we must have ICM memory in place
2843 	 * for the reserved objects in each table. We will allocate and map
2844 	 * this initial ICM memory here. Note that given the assignment
2845 	 * of span_size above, tables that are smaller or equal in total
2846 	 * size to the default span_size will be mapped in full.
2847 	 */
2848 	status = hermon_icm_dma_init(state);
2849 	if (status != DDI_SUCCESS) {
2850 		/* free the ICM table memory resources */
2851 		hermon_icm_tables_fini(state);
2852 		HERMON_WARNING(state, "Failed to allocate initial ICM");
2853 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2854 		    "icm_config_dma_init_fail");
2855 		return (DDI_FAILURE);
2856 	}
2857 
2858 	return (DDI_SUCCESS);
2859 }
2860 
2861 /*
2862  * hermon_inithca_set()
2863  *    Context: Only called from attach() path context
2864  */
2865 static void
2866 hermon_inithca_set(hermon_state_t *state, hermon_hw_initqueryhca_t *inithca)
2867 {
2868 	hermon_cfg_profile_t	*cfg;
2869 	hermon_icm_table_t	*icm;
2870 	int			i;
2871 
2872 
2873 	/* Populate the INIT_HCA structure */
2874 	icm = state->hs_icm;
2875 	cfg = state->hs_cfg_profile;
2876 
2877 	/* set version */
2878 	inithca->version = 0x02;	/* PRM 0.36 */
2879 	/* set cacheline - log2 in 16-byte chunks */
2880 	inithca->log2_cacheline = 0x2;	/* optimized for 64 byte cache */
2881 
2882 	/* we need to update the inithca info with thie UAR info too */
2883 	inithca->uar.log_max_uars = highbit(cfg->cp_log_num_uar);
2884 	inithca->uar.uar_pg_sz = PAGESHIFT - HERMON_PAGESHIFT;
2885 
2886 	/* Set endianess */
2887 #ifdef	_LITTLE_ENDIAN
2888 	inithca->big_endian	= 0;
2889 #else
2890 	inithca->big_endian	= 1;
2891 #endif
2892 
2893 	/* Port Checking is on by default */
2894 	inithca->udav_port_chk	= HERMON_UDAV_PORTCHK_ENABLED;
2895 
2896 	/* Enable IPoIB checksum */
2897 	if (state->hs_devlim.ipoib_cksm)
2898 		inithca->chsum_en = 1;
2899 
2900 	/* Set each ICM table's attributes */
2901 	for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) {
2902 		switch (icm[i].icm_type) {
2903 		case HERMON_CMPT:
2904 			inithca->tpt.cmpt_baseaddr = icm[i].icm_baseaddr;
2905 			break;
2906 
2907 		case HERMON_MTT:
2908 			inithca->tpt.mtt_baseaddr = icm[i].icm_baseaddr;
2909 			break;
2910 
2911 		case HERMON_DMPT:
2912 			inithca->tpt.dmpt_baseaddr = icm[i].icm_baseaddr;
2913 			inithca->tpt.log_dmpt_sz   = icm[i].log_num_entries;
2914 			inithca->tpt.pgfault_rnr_to = 0; /* just in case */
2915 			break;
2916 
2917 		case HERMON_QPC:
2918 			inithca->context.log_num_qp = icm[i].log_num_entries;
2919 			inithca->context.qpc_baseaddr_h =
2920 			    icm[i].icm_baseaddr >> 32;
2921 			inithca->context.qpc_baseaddr_l =
2922 			    (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5;
2923 			break;
2924 
2925 		case HERMON_CQC:
2926 			inithca->context.log_num_cq = icm[i].log_num_entries;
2927 			inithca->context.cqc_baseaddr_h =
2928 			    icm[i].icm_baseaddr >> 32;
2929 			inithca->context.cqc_baseaddr_l =
2930 			    (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5;
2931 			break;
2932 
2933 		case HERMON_SRQC:
2934 			inithca->context.log_num_srq = icm[i].log_num_entries;
2935 			inithca->context.srqc_baseaddr_h =
2936 			    icm[i].icm_baseaddr >> 32;
2937 			inithca->context.srqc_baseaddr_l =
2938 			    (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5;
2939 			break;
2940 
2941 		case HERMON_EQC:
2942 			inithca->context.log_num_eq = icm[i].log_num_entries;
2943 			inithca->context.eqc_baseaddr_h =
2944 			    icm[i].icm_baseaddr >> 32;
2945 			inithca->context.eqc_baseaddr_l =
2946 			    (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5;
2947 			break;
2948 
2949 		case HERMON_RDB:
2950 			inithca->context.rdmardc_baseaddr_h =
2951 			    icm[i].icm_baseaddr >> 32;
2952 			inithca->context.rdmardc_baseaddr_l =
2953 			    (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5;
2954 			inithca->context.log_num_rdmardc =
2955 			    icm[i].log_num_entries;
2956 			break;
2957 
2958 		case HERMON_MCG:
2959 			inithca->multi.mc_baseaddr    = icm[i].icm_baseaddr;
2960 			inithca->multi.log_mc_tbl_sz  = icm[i].log_num_entries;
2961 			inithca->multi.log_mc_tbl_ent =
2962 			    highbit(HERMON_MCGMEM_SZ(state)) - 1;
2963 			inithca->multi.log_mc_tbl_hash_sz =
2964 			    cfg->cp_log_num_mcg_hash;
2965 			inithca->multi.mc_hash_fn = HERMON_MCG_DEFAULT_HASH_FN;
2966 			break;
2967 
2968 		case HERMON_ALTC:
2969 			inithca->context.altc_baseaddr = icm[i].icm_baseaddr;
2970 			break;
2971 
2972 		case HERMON_AUXC:
2973 			inithca->context.auxc_baseaddr = icm[i].icm_baseaddr;
2974 			break;
2975 
2976 		default:
2977 			break;
2978 
2979 		}
2980 	}
2981 
2982 }
2983 
2984 /*
2985  * hermon_icm_tables_init()
2986  *    Context: Only called from attach() path context
2987  *
2988  * Dynamic ICM breaks the various ICM tables into "span_size" chunks
2989  * to enable allocation of backing memory on demand.  Arbel used a
2990  * fixed size ARBEL_ICM_SPAN_SIZE (initially was 512KB) as the
2991  * span_size for all ICM chunks.  Hermon has other considerations,
2992  * so the span_size used differs from Arbel.
2993  *
2994  * The basic considerations for why Hermon differs are:
2995  *
2996  *	1) ICM memory is in units of HERMON pages.
2997  *
2998  *	2) The AUXC table is approximately 1 byte per QP.
2999  *
3000  *	3) ICM memory for AUXC, ALTC, and RDB is allocated when
3001  *	the ICM memory for the corresponding QPC is allocated.
3002  *
3003  *	4) ICM memory for the CMPT corresponding to the various primary
3004  *	resources (QPC, SRQC, CQC, and EQC) is allocated when the ICM
3005  *	memory for the primary resource is allocated.
3006  *
3007  * One HERMON page (4KB) would typically map 4K QPs worth of AUXC.
3008  * So, the minimum chunk for the various QPC related ICM memory should
3009  * all be allocated to support the 4K QPs.  Currently, this means the
3010  * amount of memory for the various QP chunks is:
3011  *
3012  *	QPC	256*4K bytes
3013  *	RDB	128*4K bytes
3014  *	CMPT	 64*4K bytes
3015  *	ALTC	 64*4K bytes
3016  *	AUXC	  1*4K bytes
3017  *
3018  * The span_size chosen for the QP resource is 4KB of AUXC entries,
3019  * or 1 HERMON_PAGESIZE worth, which is the minimum ICM mapping size.
3020  *
3021  * Other ICM resources can have their span_size be more arbitrary.
3022  * This is 4K (HERMON_ICM_SPAN), except for MTTs because they are tiny.
3023  */
3024 
3025 /* macro to make the code below cleaner */
3026 #define	init_dependent(rsrc, dep)				\
3027 	icm[dep].span		= icm[rsrc].span;		\
3028 	icm[dep].num_spans	= icm[rsrc].num_spans;		\
3029 	icm[dep].split_shift	= icm[rsrc].split_shift;	\
3030 	icm[dep].span_mask	= icm[rsrc].span_mask;		\
3031 	icm[dep].span_shift	= icm[rsrc].span_shift;		\
3032 	icm[dep].rsrc_mask	= icm[rsrc].rsrc_mask;		\
3033 	if (hermon_verbose) {					\
3034 		IBTF_DPRINTF_L2("hermon", "tables_init: "	\
3035 		    "rsrc (0x%x) size (0x%lx) span (0x%x) "	\
3036 		    "num_spans (0x%x)", dep, icm[dep].table_size, \
3037 		    icm[dep].span, icm[dep].num_spans);		\
3038 		IBTF_DPRINTF_L2("hermon", "tables_init: "	\
3039 		    "span_shift (0x%x) split_shift (0x%x)",	\
3040 		    icm[dep].span_shift, icm[dep].split_shift);	\
3041 		IBTF_DPRINTF_L2("hermon", "tables_init: "	\
3042 		    "span_mask (0x%x)  rsrc_mask   (0x%x)",	\
3043 		    icm[dep].span_mask, icm[dep].rsrc_mask);	\
3044 	}
3045 
3046 static void
3047 hermon_icm_tables_init(hermon_state_t *state)
3048 {
3049 	hermon_icm_table_t	*icm;
3050 	int			i, k;
3051 	uint32_t		per_split;
3052 
3053 
3054 	icm = state->hs_icm;
3055 
3056 	for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) {
3057 		icm[i].icm_type		= i;
3058 		icm[i].num_entries	= 1 << icm[i].log_num_entries;
3059 		icm[i].log_object_size	= highbit(icm[i].object_size) - 1;
3060 		icm[i].table_size	= icm[i].num_entries <<
3061 		    icm[i].log_object_size;
3062 
3063 		/* deal with "dependent" resource types */
3064 		switch (i) {
3065 		case HERMON_AUXC:
3066 #ifdef HERMON_FW_WORKAROUND
3067 			icm[i].table_size = 0x80000000ull;
3068 			/* FALLTHROUGH */
3069 #endif
3070 		case HERMON_CMPT_QPC:
3071 		case HERMON_RDB:
3072 		case HERMON_ALTC:
3073 			init_dependent(HERMON_QPC, i);
3074 			continue;
3075 		case HERMON_CMPT_SRQC:
3076 			init_dependent(HERMON_SRQC, i);
3077 			continue;
3078 		case HERMON_CMPT_CQC:
3079 			init_dependent(HERMON_CQC, i);
3080 			continue;
3081 		case HERMON_CMPT_EQC:
3082 			init_dependent(HERMON_EQC, i);
3083 			continue;
3084 		}
3085 
3086 		icm[i].span = HERMON_ICM_SPAN;	/* default #rsrc's in 1 span */
3087 		if (i == HERMON_MTT) /* Alloc enough MTTs to map 256MB */
3088 			icm[i].span = HERMON_ICM_SPAN * 16;
3089 		icm[i].num_spans = icm[i].num_entries / icm[i].span;
3090 		if (icm[i].num_spans == 0) {
3091 			icm[i].span = icm[i].num_entries;
3092 			per_split = 1;
3093 			icm[i].num_spans = icm[i].num_entries / icm[i].span;
3094 		} else {
3095 			per_split = icm[i].num_spans / HERMON_ICM_SPLIT;
3096 			if (per_split == 0) {
3097 				per_split = 1;
3098 			}
3099 		}
3100 		if (hermon_verbose)
3101 			IBTF_DPRINTF_L2("ICM", "rsrc %x  span %x  num_spans %x",
3102 			    i, icm[i].span, icm[i].num_spans);
3103 
3104 		/*
3105 		 * Ensure a minimum table size of an ICM page, and a
3106 		 * maximum span size of the ICM table size.  This ensures
3107 		 * that we don't have less than an ICM page to map, which is
3108 		 * impossible, and that we will map an entire table at
3109 		 * once if it's total size is less than the span size.
3110 		 */
3111 		icm[i].table_size = max(icm[i].table_size, HERMON_PAGESIZE);
3112 
3113 		icm[i].span_shift = 0;
3114 		for (k = icm[i].span; k != 1; k >>= 1)
3115 			icm[i].span_shift++;
3116 		icm[i].split_shift = icm[i].span_shift;
3117 		for (k = per_split; k != 1; k >>= 1)
3118 			icm[i].split_shift++;
3119 		icm[i].span_mask = (1 << icm[i].split_shift) -
3120 		    (1 << icm[i].span_shift);
3121 		icm[i].rsrc_mask = (1 << icm[i].span_shift) - 1;
3122 
3123 
3124 		/* Initialize the table lock */
3125 		mutex_init(&icm[i].icm_table_lock, NULL, MUTEX_DRIVER,
3126 		    DDI_INTR_PRI(state->hs_intrmsi_pri));
3127 		cv_init(&icm[i].icm_table_cv, NULL, CV_DRIVER, NULL);
3128 
3129 		if (hermon_verbose) {
3130 			IBTF_DPRINTF_L2("hermon", "tables_init: "
3131 			    "rsrc (0x%x) size (0x%lx)", i, icm[i].table_size);
3132 			IBTF_DPRINTF_L2("hermon", "tables_init: "
3133 			    "span (0x%x) num_spans (0x%x)",
3134 			    icm[i].span, icm[i].num_spans);
3135 			IBTF_DPRINTF_L2("hermon", "tables_init: "
3136 			    "span_shift (0x%x) split_shift (0x%x)",
3137 			    icm[i].span_shift, icm[i].split_shift);
3138 			IBTF_DPRINTF_L2("hermon", "tables_init: "
3139 			    "span_mask (0x%x)  rsrc_mask   (0x%x)",
3140 			    icm[i].span_mask, icm[i].rsrc_mask);
3141 		}
3142 	}
3143 
3144 }
3145 
3146 /*
3147  * hermon_icm_tables_fini()
3148  *    Context: Only called from attach() path context
3149  *
3150  * Clean up all icm_tables.  Free the bitmap and dma_info arrays.
3151  */
3152 static void
3153 hermon_icm_tables_fini(hermon_state_t *state)
3154 {
3155 	hermon_icm_table_t	*icm;
3156 	int			nspans;
3157 	int			i, j;
3158 
3159 
3160 	icm = state->hs_icm;
3161 
3162 	for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) {
3163 
3164 		mutex_enter(&icm[i].icm_table_lock);
3165 		nspans = icm[i].num_spans;
3166 
3167 		for (j = 0; j < HERMON_ICM_SPLIT; j++) {
3168 			if (icm[i].icm_dma[j])
3169 				/* Free the ICM DMA slots */
3170 				kmem_free(icm[i].icm_dma[j],
3171 				    nspans * sizeof (hermon_dma_info_t));
3172 
3173 			if (icm[i].icm_bitmap[j])
3174 				/* Free the table bitmap */
3175 				kmem_free(icm[i].icm_bitmap[j],
3176 				    (nspans + 7) / 8);
3177 		}
3178 		/* Destroy the table lock */
3179 		cv_destroy(&icm[i].icm_table_cv);
3180 		mutex_exit(&icm[i].icm_table_lock);
3181 		mutex_destroy(&icm[i].icm_table_lock);
3182 	}
3183 
3184 }
3185 
3186 /*
3187  * hermon_icm_dma_init()
3188  *    Context: Only called from attach() path context
3189  */
3190 static int
3191 hermon_icm_dma_init(hermon_state_t *state)
3192 {
3193 	hermon_icm_table_t	*icm;
3194 	hermon_rsrc_type_t	type;
3195 	int			status;
3196 
3197 
3198 	/*
3199 	 * This routine will allocate initial ICM DMA resources for ICM
3200 	 * tables that have reserved ICM objects. This is the only routine
3201 	 * where we should have to allocate ICM outside of hermon_rsrc_alloc().
3202 	 * We need to allocate ICM here explicitly, rather than in
3203 	 * hermon_rsrc_alloc(), because we've not yet completed the resource
3204 	 * pool initialization. When the resource pools are initialized
3205 	 * (in hermon_rsrc_init_phase2(), see hermon_rsrc.c for more
3206 	 * information), resource preallocations will be invoked to match
3207 	 * the ICM allocations seen here. We will then be able to use the
3208 	 * normal allocation path.  Note we don't need to set a refcnt on
3209 	 * these initial allocations because that will be done in the calls
3210 	 * to hermon_rsrc_alloc() from hermon_hw_entries_init() for the
3211 	 * "prealloc" objects (see hermon_rsrc.c for more information).
3212 	 */
3213 	for (type = 0; type < HERMON_NUM_ICM_RESOURCES; type++) {
3214 
3215 		/* ICM for these is allocated within hermon_icm_alloc() */
3216 		switch (type) {
3217 		case HERMON_CMPT:
3218 		case HERMON_CMPT_QPC:
3219 		case HERMON_CMPT_SRQC:
3220 		case HERMON_CMPT_CQC:
3221 		case HERMON_CMPT_EQC:
3222 		case HERMON_AUXC:
3223 		case HERMON_ALTC:
3224 		case HERMON_RDB:
3225 			continue;
3226 		}
3227 
3228 		icm = &state->hs_icm[type];
3229 
3230 		mutex_enter(&icm->icm_table_lock);
3231 		status = hermon_icm_alloc(state, type, 0, 0);
3232 		mutex_exit(&icm->icm_table_lock);
3233 		if (status != DDI_SUCCESS) {
3234 			while (type--) {
3235 				icm = &state->hs_icm[type];
3236 				mutex_enter(&icm->icm_table_lock);
3237 				hermon_icm_free(state, type, 0, 0);
3238 				mutex_exit(&icm->icm_table_lock);
3239 			}
3240 			return (DDI_FAILURE);
3241 		}
3242 
3243 		if (hermon_verbose) {
3244 			IBTF_DPRINTF_L2("hermon", "hermon_icm_dma_init: "
3245 			    "table (0x%x) index (0x%x) allocated", type, 0);
3246 		}
3247 	}
3248 
3249 	return (DDI_SUCCESS);
3250 }
3251 
3252 /*
3253  * hermon_icm_dma_fini()
3254  *    Context: Only called from attach() path context
3255  *
3256  * ICM has been completely unmapped.  We just free the memory here.
3257  */
3258 static void
3259 hermon_icm_dma_fini(hermon_state_t *state)
3260 {
3261 	hermon_icm_table_t	*icm;
3262 	hermon_dma_info_t	*dma_info;
3263 	hermon_rsrc_type_t	type;
3264 	int			index1, index2;
3265 
3266 
3267 	for (type = 0; type < HERMON_NUM_ICM_RESOURCES; type++) {
3268 		icm = &state->hs_icm[type];
3269 		for (index1 = 0; index1 < HERMON_ICM_SPLIT; index1++) {
3270 			dma_info = icm->icm_dma[index1];
3271 			if (dma_info == NULL)
3272 				continue;
3273 			for (index2 = 0; index2 < icm->num_spans; index2++) {
3274 				if (dma_info[index2].dma_hdl)
3275 					hermon_dma_free(&dma_info[index2]);
3276 				dma_info[index2].dma_hdl = NULL;
3277 			}
3278 		}
3279 	}
3280 
3281 }
3282 
3283 /*
3284  * hermon_hca_port_init()
3285  *    Context: Only called from attach() path context
3286  */
3287 static int
3288 hermon_hca_port_init(hermon_state_t *state)
3289 {
3290 	hermon_hw_set_port_t	*portinits, *initport;
3291 	hermon_cfg_profile_t	*cfgprof;
3292 	uint_t			num_ports;
3293 	int			i = 0, status;
3294 	uint64_t		maxval, val;
3295 	uint64_t		sysimgguid, nodeguid, portguid;
3296 
3297 
3298 	cfgprof = state->hs_cfg_profile;
3299 
3300 	/* Get number of HCA ports */
3301 	num_ports = cfgprof->cp_num_ports;
3302 
3303 	/* Allocate space for Hermon set port  struct(s) */
3304 	portinits = (hermon_hw_set_port_t *)kmem_zalloc(num_ports *
3305 	    sizeof (hermon_hw_set_port_t), KM_SLEEP);
3306 
3307 
3308 
3309 	/* Post commands to initialize each Hermon HCA port */
3310 	/*
3311 	 * In Hermon, the process is different than in previous HCAs.
3312 	 * Here, you have to:
3313 	 *	QUERY_PORT - to get basic information from the HCA
3314 	 *	set the fields accordingly
3315 	 *	SET_PORT - to change/set everything as desired
3316 	 *	INIT_PORT - to bring the port up
3317 	 *
3318 	 * Needs to be done for each port in turn
3319 	 */
3320 
3321 	for (i = 0; i < num_ports; i++) {
3322 		bzero(&state->hs_queryport, sizeof (hermon_hw_query_port_t));
3323 		status = hermon_cmn_query_cmd_post(state, QUERY_PORT, 0,
3324 		    (i + 1), &state->hs_queryport,
3325 		    sizeof (hermon_hw_query_port_t), HERMON_CMD_NOSLEEP_SPIN);
3326 		if (status != HERMON_CMD_SUCCESS) {
3327 			cmn_err(CE_CONT, "Hermon: QUERY_PORT (port %02d) "
3328 			    "command failed: %08x\n", i + 1, status);
3329 			goto init_ports_fail;
3330 		}
3331 		initport = &portinits[i];
3332 		state->hs_initport = &portinits[i];
3333 
3334 		bzero(initport, sizeof (hermon_hw_query_port_t));
3335 
3336 		/*
3337 		 * Determine whether we need to override the firmware's
3338 		 * default SystemImageGUID setting.
3339 		 */
3340 		sysimgguid = cfgprof->cp_sysimgguid;
3341 		if (sysimgguid != 0) {
3342 			initport->sig		= 1;
3343 			initport->sys_img_guid	= sysimgguid;
3344 		}
3345 
3346 		/*
3347 		 * Determine whether we need to override the firmware's
3348 		 * default NodeGUID setting.
3349 		 */
3350 		nodeguid = cfgprof->cp_nodeguid;
3351 		if (nodeguid != 0) {
3352 			initport->ng		= 1;
3353 			initport->node_guid	= nodeguid;
3354 		}
3355 
3356 		/*
3357 		 * Determine whether we need to override the firmware's
3358 		 * default PortGUID setting.
3359 		 */
3360 		portguid = cfgprof->cp_portguid[i];
3361 		if (portguid != 0) {
3362 			initport->g0		= 1;
3363 			initport->guid0		= portguid;
3364 		}
3365 
3366 		/* Validate max MTU size */
3367 		maxval  = state->hs_queryport.ib_mtu;
3368 		val	= cfgprof->cp_max_mtu;
3369 		if (val > maxval) {
3370 			goto init_ports_fail;
3371 		}
3372 
3373 		/* Validate the max port width */
3374 		maxval  = state->hs_queryport.ib_port_wid;
3375 		val	= cfgprof->cp_max_port_width;
3376 		if (val > maxval) {
3377 			goto init_ports_fail;
3378 		}
3379 
3380 		/* Validate max VL cap size */
3381 		maxval  = state->hs_queryport.max_vl;
3382 		val	= cfgprof->cp_max_vlcap;
3383 		if (val > maxval) {
3384 			goto init_ports_fail;
3385 		}
3386 
3387 		/* Validate max GID table size */
3388 		maxval  = ((uint64_t)1 << state->hs_queryport.log_max_gid);
3389 		val	= ((uint64_t)1 << cfgprof->cp_log_max_gidtbl);
3390 		if (val > maxval) {
3391 			goto init_ports_fail;
3392 		}
3393 		initport->max_guid = (uint16_t)val;
3394 		initport->mg = 1;
3395 
3396 		/* Validate max PKey table size */
3397 		maxval	= ((uint64_t)1 << state->hs_queryport.log_max_pkey);
3398 		val	= ((uint64_t)1 << cfgprof->cp_log_max_pkeytbl);
3399 		if (val > maxval) {
3400 			goto init_ports_fail;
3401 		}
3402 		initport->max_pkey = (uint16_t)val;
3403 		initport->mp = 1;
3404 		/*
3405 		 * Post the SET_PORT cmd to Hermon firmware. This sets
3406 		 * the parameters of the port.
3407 		 */
3408 		status = hermon_set_port_cmd_post(state, initport, i + 1,
3409 		    HERMON_CMD_NOSLEEP_SPIN);
3410 		if (status != HERMON_CMD_SUCCESS) {
3411 			cmn_err(CE_CONT, "Hermon: SET_PORT (port %02d) command "
3412 			    "failed: %08x\n", i + 1, status);
3413 			goto init_ports_fail;
3414 		}
3415 		/* issue another SET_PORT cmd - performance fix/workaround */
3416 		/* XXX - need to discuss with Mellanox */
3417 		bzero(initport, sizeof (hermon_hw_query_port_t));
3418 		initport->cap_mask = 0x02500868;
3419 		status = hermon_set_port_cmd_post(state, initport, i + 1,
3420 		    HERMON_CMD_NOSLEEP_SPIN);
3421 		if (status != HERMON_CMD_SUCCESS) {
3422 			cmn_err(CE_CONT, "Hermon: SET_PORT (port %02d) command "
3423 			    "failed: %08x\n", i + 1, status);
3424 			goto init_ports_fail;
3425 		}
3426 	}
3427 
3428 	/*
3429 	 * Finally, do the INIT_PORT for each port in turn
3430 	 * When this command completes, the corresponding Hermon port
3431 	 * will be physically "Up" and initialized.
3432 	 */
3433 	for (i = 0; i < num_ports; i++) {
3434 		status = hermon_init_port_cmd_post(state, i + 1,
3435 		    HERMON_CMD_NOSLEEP_SPIN);
3436 		if (status != HERMON_CMD_SUCCESS) {
3437 			cmn_err(CE_CONT, "Hermon: INIT_PORT (port %02d) "
3438 			    "comman failed: %08x\n", i + 1, status);
3439 			goto init_ports_fail;
3440 		}
3441 	}
3442 
3443 	/* Free up the memory for Hermon port init struct(s), return success */
3444 	kmem_free(portinits, num_ports * sizeof (hermon_hw_set_port_t));
3445 	return (DDI_SUCCESS);
3446 
3447 init_ports_fail:
3448 	/*
3449 	 * Free up the memory for Hermon port init struct(s), shutdown any
3450 	 * successfully initialized ports, and return failure
3451 	 */
3452 	kmem_free(portinits, num_ports * sizeof (hermon_hw_set_port_t));
3453 	(void) hermon_hca_ports_shutdown(state, i);
3454 
3455 	return (DDI_FAILURE);
3456 }
3457 
3458 
3459 /*
3460  * hermon_hca_ports_shutdown()
3461  *    Context: Only called from attach() and/or detach() path contexts
3462  */
3463 static int
3464 hermon_hca_ports_shutdown(hermon_state_t *state, uint_t num_init)
3465 {
3466 	int	i, status;
3467 
3468 	/*
3469 	 * Post commands to shutdown all init'd Hermon HCA ports.  Note: if
3470 	 * any of these commands fail for any reason, it would be entirely
3471 	 * unexpected and probably indicative a serious problem (HW or SW).
3472 	 * Although we do return void from this function, this type of failure
3473 	 * should not go unreported.  That is why we have the warning message.
3474 	 */
3475 	for (i = 0; i < num_init; i++) {
3476 		status = hermon_close_port_cmd_post(state, i + 1,
3477 		    HERMON_CMD_NOSLEEP_SPIN);
3478 		if (status != HERMON_CMD_SUCCESS) {
3479 			HERMON_WARNING(state, "failed to shutdown HCA port");
3480 			return (status);
3481 		}
3482 	}
3483 	return (HERMON_CMD_SUCCESS);
3484 }
3485 
3486 
3487 /*
3488  * hermon_internal_uarpg_init
3489  *    Context: Only called from attach() path context
3490  */
3491 static int
3492 hermon_internal_uarpg_init(hermon_state_t *state)
3493 {
3494 	int	status;
3495 	hermon_dbr_info_t 	*info;
3496 
3497 	/*
3498 	 * Allocate the UAR page for kernel use. This UAR page is
3499 	 * the privileged UAR page through which all kernel generated
3500 	 * doorbells will be rung. There are a number of UAR pages
3501 	 * reserved by hardware at the front of the UAR BAR, indicated
3502 	 * by DEVCAP.num_rsvd_uar, which we have already allocated. So,
3503 	 * the kernel page, or UAR page index num_rsvd_uar, will be
3504 	 * allocated here for kernel use.
3505 	 */
3506 
3507 	status = hermon_rsrc_alloc(state, HERMON_UARPG, 1, HERMON_SLEEP,
3508 	    &state->hs_uarkpg_rsrc);
3509 	if (status != DDI_SUCCESS) {
3510 		return (DDI_FAILURE);
3511 	}
3512 
3513 	/* Setup pointer to kernel UAR page */
3514 	state->hs_uar = (hermon_hw_uar_t *)state->hs_uarkpg_rsrc->hr_addr;
3515 
3516 	/* need to set up DBr tracking as well */
3517 	status = hermon_dbr_page_alloc(state, &info);
3518 	if (status != DDI_SUCCESS) {
3519 		return (DDI_FAILURE);
3520 	}
3521 	state->hs_kern_dbr = info;
3522 	return (DDI_SUCCESS);
3523 }
3524 
3525 
3526 /*
3527  * hermon_internal_uarpg_fini
3528  *    Context: Only called from attach() and/or detach() path contexts
3529  */
3530 static void
3531 hermon_internal_uarpg_fini(hermon_state_t *state)
3532 {
3533 	/* Free up Hermon UAR page #1 (kernel driver doorbells) */
3534 	hermon_rsrc_free(state, &state->hs_uarkpg_rsrc);
3535 }
3536 
3537 
3538 /*
3539  * hermon_special_qp_contexts_reserve()
3540  *    Context: Only called from attach() path context
3541  */
3542 static int
3543 hermon_special_qp_contexts_reserve(hermon_state_t *state)
3544 {
3545 	hermon_rsrc_t	*qp0_rsrc, *qp1_rsrc, *qp_resvd;
3546 	int		status;
3547 
3548 	/* Initialize the lock used for special QP rsrc management */
3549 	mutex_init(&state->hs_spec_qplock, NULL, MUTEX_DRIVER,
3550 	    DDI_INTR_PRI(state->hs_intrmsi_pri));
3551 
3552 	/*
3553 	 * Reserve contexts for QP0.  These QP contexts will be setup to
3554 	 * act as aliases for the real QP0.  Note: We are required to grab
3555 	 * two QPs (one per port) even if we are operating in single-port
3556 	 * mode.
3557 	 */
3558 	status = hermon_rsrc_alloc(state, HERMON_QPC, 2,
3559 	    HERMON_SLEEP, &qp0_rsrc);
3560 	if (status != DDI_SUCCESS) {
3561 		mutex_destroy(&state->hs_spec_qplock);
3562 		return (DDI_FAILURE);
3563 	}
3564 	state->hs_spec_qp0 = qp0_rsrc;
3565 
3566 	/*
3567 	 * Reserve contexts for QP1.  These QP contexts will be setup to
3568 	 * act as aliases for the real QP1.  Note: We are required to grab
3569 	 * two QPs (one per port) even if we are operating in single-port
3570 	 * mode.
3571 	 */
3572 	status = hermon_rsrc_alloc(state, HERMON_QPC, 2,
3573 	    HERMON_SLEEP, &qp1_rsrc);
3574 	if (status != DDI_SUCCESS) {
3575 		hermon_rsrc_free(state, &qp0_rsrc);
3576 		mutex_destroy(&state->hs_spec_qplock);
3577 		return (DDI_FAILURE);
3578 	}
3579 	state->hs_spec_qp1 = qp1_rsrc;
3580 
3581 	status = hermon_rsrc_alloc(state, HERMON_QPC, 4,
3582 	    HERMON_SLEEP, &qp_resvd);
3583 	if (status != DDI_SUCCESS) {
3584 		hermon_rsrc_free(state, &qp1_rsrc);
3585 		hermon_rsrc_free(state, &qp0_rsrc);
3586 		mutex_destroy(&state->hs_spec_qplock);
3587 		return (DDI_FAILURE);
3588 	}
3589 	state->hs_spec_qp_unused = qp_resvd;
3590 
3591 	return (DDI_SUCCESS);
3592 }
3593 
3594 
3595 /*
3596  * hermon_special_qp_contexts_unreserve()
3597  *    Context: Only called from attach() and/or detach() path contexts
3598  */
3599 static void
3600 hermon_special_qp_contexts_unreserve(hermon_state_t *state)
3601 {
3602 
3603 	/* Unreserve contexts for spec_qp_unused */
3604 	hermon_rsrc_free(state, &state->hs_spec_qp_unused);
3605 
3606 	/* Unreserve contexts for QP1 */
3607 	hermon_rsrc_free(state, &state->hs_spec_qp1);
3608 
3609 	/* Unreserve contexts for QP0 */
3610 	hermon_rsrc_free(state, &state->hs_spec_qp0);
3611 
3612 	/* Destroy the lock used for special QP rsrc management */
3613 	mutex_destroy(&state->hs_spec_qplock);
3614 
3615 }
3616 
3617 
3618 /*
3619  * hermon_sw_reset()
3620  *    Context: Currently called only from attach() path context
3621  */
3622 static int
3623 hermon_sw_reset(hermon_state_t *state)
3624 {
3625 	ddi_acc_handle_t	hdl = hermon_get_pcihdl(state);
3626 	ddi_acc_handle_t	cmdhdl = hermon_get_cmdhdl(state);
3627 	uint32_t		reset_delay;
3628 	int			status, i;
3629 	uint32_t		sem;
3630 	uint_t			offset;
3631 	uint32_t		data32;		/* for devctl & linkctl */
3632 	int			loopcnt;
3633 
3634 	/* initialize the FMA retry loop */
3635 	hermon_pio_init(fm_loop_cnt, fm_status, fm_test);
3636 	hermon_pio_init(fm_loop_cnt2, fm_status2, fm_test2);
3637 
3638 	/*
3639 	 * If the configured software reset delay is set to zero, then we
3640 	 * will not attempt a software reset of the Hermon device.
3641 	 */
3642 	reset_delay = state->hs_cfg_profile->cp_sw_reset_delay;
3643 	if (reset_delay == 0) {
3644 		return (DDI_SUCCESS);
3645 	}
3646 
3647 	/* the FMA retry loop starts. */
3648 	hermon_pio_start(state, cmdhdl, pio_error, fm_loop_cnt, fm_status,
3649 	    fm_test);
3650 	hermon_pio_start(state, hdl, pio_error2, fm_loop_cnt2, fm_status2,
3651 	    fm_test2);
3652 
3653 	/* Query the PCI capabilities of the HCA device */
3654 	/* but don't process the VPD until after reset */
3655 	status = hermon_pci_capability_list(state, hdl);
3656 	if (status != DDI_SUCCESS) {
3657 		cmn_err(CE_NOTE, "failed to get pci capabilities list(0x%x)\n",
3658 		    status);
3659 		return (DDI_FAILURE);
3660 	}
3661 
3662 	/*
3663 	 * Read all PCI config info (reg0...reg63).  Note: According to the
3664 	 * Hermon software reset application note, we should not read or
3665 	 * restore the values in reg22 and reg23.
3666 	 * NOTE:  For Hermon (and Arbel too) it says to restore the command
3667 	 * register LAST, and technically, you need to restore the
3668 	 * PCIE Capability "device control" and "link control" (word-sized,
3669 	 * at offsets 0x08 and 0x10 from the capbility ID respectively).
3670 	 * We hold off restoring the command register - offset 0x4 - till last
3671 	 */
3672 
3673 	/* 1st, wait for the semaphore assure accessibility - per PRM */
3674 	status = -1;
3675 	for (i = 0; i < NANOSEC/MICROSEC /* 1sec timeout */; i++) {
3676 		sem = ddi_get32(cmdhdl, state->hs_cmd_regs.sw_semaphore);
3677 		if (sem == 0) {
3678 			status = 0;
3679 			break;
3680 		}
3681 		drv_usecwait(1);
3682 	}
3683 
3684 	/* Check if timeout happens */
3685 	if (status == -1) {
3686 		/*
3687 		 * Remove this acc handle from Hermon, then log
3688 		 * the error.
3689 		 */
3690 		hermon_pci_config_teardown(state, &hdl);
3691 
3692 		cmn_err(CE_WARN, "hermon_sw_reset timeout: "
3693 		    "failed to get the semaphore(0x%p)\n",
3694 		    (void *)state->hs_cmd_regs.sw_semaphore);
3695 
3696 		hermon_fm_ereport(state, HCA_IBA_ERR, HCA_ERR_NON_FATAL);
3697 		return (DDI_FAILURE);
3698 	}
3699 
3700 	for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) {
3701 		if ((i != HERMON_SW_RESET_REG22_RSVD) &&
3702 		    (i != HERMON_SW_RESET_REG23_RSVD)) {
3703 			state->hs_cfg_data[i]  = pci_config_get32(hdl, i << 2);
3704 		}
3705 	}
3706 
3707 	/*
3708 	 * Perform the software reset (by writing 1 at offset 0xF0010)
3709 	 */
3710 	ddi_put32(cmdhdl, state->hs_cmd_regs.sw_reset, HERMON_SW_RESET_START);
3711 
3712 	/*
3713 	 * This delay is required so as not to cause a panic here. If the
3714 	 * device is accessed too soon after reset it will not respond to
3715 	 * config cycles, causing a Master Abort and panic.
3716 	 */
3717 	drv_usecwait(reset_delay);
3718 
3719 	/*
3720 	 * Poll waiting for the device to finish resetting.
3721 	 */
3722 	loopcnt = 100;	/* 100 times @ 100 usec - total delay 10 msec */
3723 	while ((pci_config_get32(hdl, 0) & 0x0000FFFF) != PCI_VENID_MLX) {
3724 		drv_usecwait(HERMON_SW_RESET_POLL_DELAY);
3725 		if (--loopcnt == 0)
3726 			break;	/* just in case, break and go on */
3727 	}
3728 	if (loopcnt == 0)
3729 		cmn_err(CE_CONT, "!Never see VEND_ID - read == %X",
3730 		    pci_config_get32(hdl, 0));
3731 
3732 	/*
3733 	 * Restore the config info
3734 	 */
3735 	for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) {
3736 		if (i == 1) continue;	/* skip the status/ctrl reg */
3737 		if ((i != HERMON_SW_RESET_REG22_RSVD) &&
3738 		    (i != HERMON_SW_RESET_REG23_RSVD)) {
3739 			pci_config_put32(hdl, i << 2, state->hs_cfg_data[i]);
3740 		}
3741 	}
3742 
3743 	/*
3744 	 * PCI Express Capability - we saved during capability list, and
3745 	 * we'll restore them here.
3746 	 */
3747 	offset = state->hs_pci_cap_offset;
3748 	data32 = state->hs_pci_cap_devctl;
3749 	pci_config_put32(hdl, offset + HERMON_PCI_CAP_DEV_OFFS, data32);
3750 	data32 = state->hs_pci_cap_lnkctl;
3751 	pci_config_put32(hdl, offset + HERMON_PCI_CAP_LNK_OFFS, data32);
3752 
3753 	pci_config_put32(hdl, 0x04, (state->hs_cfg_data[1] | 0x0006));
3754 
3755 	/* the FMA retry loop ends. */
3756 	hermon_pio_end(state, hdl, pio_error2, fm_loop_cnt2, fm_status2,
3757 	    fm_test2);
3758 	hermon_pio_end(state, cmdhdl, pio_error, fm_loop_cnt, fm_status,
3759 	    fm_test);
3760 
3761 	return (DDI_SUCCESS);
3762 
3763 pio_error2:
3764 	/* fall through */
3765 pio_error:
3766 	hermon_fm_ereport(state, HCA_SYS_ERR, HCA_ERR_NON_FATAL);
3767 	return (DDI_FAILURE);
3768 }
3769 
3770 
3771 /*
3772  * hermon_mcg_init()
3773  *    Context: Only called from attach() path context
3774  */
3775 static int
3776 hermon_mcg_init(hermon_state_t *state)
3777 {
3778 	uint_t		mcg_tmp_sz;
3779 
3780 
3781 	/*
3782 	 * Allocate space for the MCG temporary copy buffer.  This is
3783 	 * used by the Attach/Detach Multicast Group code
3784 	 */
3785 	mcg_tmp_sz = HERMON_MCGMEM_SZ(state);
3786 	state->hs_mcgtmp = kmem_zalloc(mcg_tmp_sz, KM_SLEEP);
3787 
3788 	/*
3789 	 * Initialize the multicast group mutex.  This ensures atomic
3790 	 * access to add, modify, and remove entries in the multicast
3791 	 * group hash lists.
3792 	 */
3793 	mutex_init(&state->hs_mcglock, NULL, MUTEX_DRIVER,
3794 	    DDI_INTR_PRI(state->hs_intrmsi_pri));
3795 
3796 	return (DDI_SUCCESS);
3797 }
3798 
3799 
3800 /*
3801  * hermon_mcg_fini()
3802  *    Context: Only called from attach() and/or detach() path contexts
3803  */
3804 static void
3805 hermon_mcg_fini(hermon_state_t *state)
3806 {
3807 	uint_t		mcg_tmp_sz;
3808 
3809 
3810 	/* Free up the space used for the MCG temporary copy buffer */
3811 	mcg_tmp_sz = HERMON_MCGMEM_SZ(state);
3812 	kmem_free(state->hs_mcgtmp, mcg_tmp_sz);
3813 
3814 	/* Destroy the multicast group mutex */
3815 	mutex_destroy(&state->hs_mcglock);
3816 
3817 }
3818 
3819 
3820 /*
3821  * hermon_fw_version_check()
3822  *    Context: Only called from attach() path context
3823  */
3824 static int
3825 hermon_fw_version_check(hermon_state_t *state)
3826 {
3827 
3828 	uint_t	hermon_fw_ver_major;
3829 	uint_t	hermon_fw_ver_minor;
3830 	uint_t	hermon_fw_ver_subminor;
3831 
3832 #ifdef FMA_TEST
3833 	if (hermon_test_num == -1) {
3834 		return (DDI_FAILURE);
3835 	}
3836 #endif
3837 
3838 	/*
3839 	 * Depending on which version of driver we have attached, and which
3840 	 * HCA we've attached, the firmware version checks will be different.
3841 	 * We set up the comparison values for both Arbel and Sinai HCAs.
3842 	 */
3843 	switch (state->hs_operational_mode) {
3844 	case HERMON_HCA_MODE:
3845 		hermon_fw_ver_major = HERMON_FW_VER_MAJOR;
3846 		hermon_fw_ver_minor = HERMON_FW_VER_MINOR;
3847 		hermon_fw_ver_subminor = HERMON_FW_VER_SUBMINOR;
3848 		break;
3849 
3850 	default:
3851 		return (DDI_FAILURE);
3852 	}
3853 
3854 	/*
3855 	 * If FW revision major number is less than acceptable,
3856 	 * return failure, else if greater return success.  If
3857 	 * the major numbers are equal than check the minor number
3858 	 */
3859 	if (state->hs_fw.fw_rev_major < hermon_fw_ver_major) {
3860 		return (DDI_FAILURE);
3861 	} else if (state->hs_fw.fw_rev_major > hermon_fw_ver_major) {
3862 		return (DDI_SUCCESS);
3863 	}
3864 
3865 	/*
3866 	 * Do the same check as above, except for minor revision numbers
3867 	 * If the minor numbers are equal than check the subminor number
3868 	 */
3869 	if (state->hs_fw.fw_rev_minor < hermon_fw_ver_minor) {
3870 		return (DDI_FAILURE);
3871 	} else if (state->hs_fw.fw_rev_minor > hermon_fw_ver_minor) {
3872 		return (DDI_SUCCESS);
3873 	}
3874 
3875 	/*
3876 	 * Once again we do the same check as above, except for the subminor
3877 	 * revision number.  If the subminor numbers are equal here, then
3878 	 * these are the same firmware version, return success
3879 	 */
3880 	if (state->hs_fw.fw_rev_subminor < hermon_fw_ver_subminor) {
3881 		return (DDI_FAILURE);
3882 	} else if (state->hs_fw.fw_rev_subminor > hermon_fw_ver_subminor) {
3883 		return (DDI_SUCCESS);
3884 	}
3885 
3886 	return (DDI_SUCCESS);
3887 }
3888 
3889 
3890 /*
3891  * hermon_device_info_report()
3892  *    Context: Only called from attach() path context
3893  */
3894 static void
3895 hermon_device_info_report(hermon_state_t *state)
3896 {
3897 
3898 	cmn_err(CE_CONT, "?hermon%d: FW ver: %04d.%04d.%04d, "
3899 	    "HW rev: %02d\n", state->hs_instance, state->hs_fw.fw_rev_major,
3900 	    state->hs_fw.fw_rev_minor, state->hs_fw.fw_rev_subminor,
3901 	    state->hs_revision_id);
3902 	cmn_err(CE_CONT, "?hermon%d: %64s (0x%016" PRIx64 ")\n",
3903 	    state->hs_instance, state->hs_nodedesc, state->hs_nodeguid);
3904 
3905 }
3906 
3907 
3908 /*
3909  * hermon_pci_capability_list()
3910  *    Context: Only called from attach() path context
3911  */
3912 static int
3913 hermon_pci_capability_list(hermon_state_t *state, ddi_acc_handle_t hdl)
3914 {
3915 	uint_t		offset, data;
3916 	uint32_t	data32;
3917 
3918 	state->hs_pci_cap_offset = 0;		/* make sure it's cleared */
3919 
3920 	/*
3921 	 * Check for the "PCI Capabilities" bit in the "Status Register".
3922 	 * Bit 4 in this register indicates the presence of a "PCI
3923 	 * Capabilities" list.
3924 	 *
3925 	 * PCI-Express requires this bit to be set to 1.
3926 	 */
3927 	data = pci_config_get16(hdl, 0x06);
3928 	if ((data & 0x10) == 0) {
3929 		return (DDI_FAILURE);
3930 	}
3931 
3932 	/*
3933 	 * Starting from offset 0x34 in PCI config space, find the
3934 	 * head of "PCI capabilities" list, and walk the list.  If
3935 	 * capabilities of a known type are encountered (e.g.
3936 	 * "PCI-X Capability"), then call the appropriate handler
3937 	 * function.
3938 	 */
3939 	offset = pci_config_get8(hdl, 0x34);
3940 	while (offset != 0x0) {
3941 		data = pci_config_get8(hdl, offset);
3942 		/*
3943 		 * Check for known capability types.  Hermon has the
3944 		 * following:
3945 		 *    o Power Mgmt	 (0x02)
3946 		 *    o VPD Capability   (0x03)
3947 		 *    o PCI-E Capability (0x10)
3948 		 *    o MSIX Capability  (0x11)
3949 		 */
3950 		switch (data) {
3951 		case 0x01:
3952 			/* power mgmt handling */
3953 			break;
3954 		case 0x03:
3955 
3956 /*
3957  * Reading the PCIe VPD is inconsistent - that is, sometimes causes
3958  * problems on (mostly) X64, though we've also seen problems w/ Sparc
3959  * and Tavor --- so, for now until it's root caused, don't try and
3960  * read it
3961  */
3962 #ifdef HERMON_VPD_WORKS
3963 			hermon_pci_capability_vpd(state, hdl, offset);
3964 #else
3965 			delay(100);
3966 			hermon_pci_capability_vpd(state, hdl, offset);
3967 #endif
3968 			break;
3969 		case 0x10:
3970 			/*
3971 			 * PCI Express Capability - save offset & contents
3972 			 * for later in reset
3973 			 */
3974 			state->hs_pci_cap_offset = offset;
3975 			data32 = pci_config_get32(hdl,
3976 			    offset + HERMON_PCI_CAP_DEV_OFFS);
3977 			state->hs_pci_cap_devctl = data32;
3978 			data32 = pci_config_get32(hdl,
3979 			    offset + HERMON_PCI_CAP_LNK_OFFS);
3980 			state->hs_pci_cap_lnkctl = data32;
3981 			break;
3982 		case 0x11:
3983 			/*
3984 			 * MSIX support - nothing to do, taken care of in the
3985 			 * MSI/MSIX interrupt frameworkd
3986 			 */
3987 			break;
3988 		default:
3989 			/* just go on to the next */
3990 			break;
3991 		}
3992 
3993 		/* Get offset of next entry in list */
3994 		offset = pci_config_get8(hdl, offset + 1);
3995 	}
3996 
3997 	return (DDI_SUCCESS);
3998 }
3999 
4000 /*
4001  * hermon_pci_read_vpd()
4002  *    Context: Only called from attach() path context
4003  *    utility routine for hermon_pci_capability_vpd()
4004  */
4005 static int
4006 hermon_pci_read_vpd(ddi_acc_handle_t hdl, uint_t offset, uint32_t addr,
4007     uint32_t *data)
4008 {
4009 	int		retry = 40;  /* retry counter for EEPROM poll */
4010 	uint32_t	val;
4011 	int		vpd_addr = offset + 2;
4012 	int		vpd_data = offset + 4;
4013 
4014 	/*
4015 	 * In order to read a 32-bit value from VPD, we are to write down
4016 	 * the address (offset in the VPD itself) to the address register.
4017 	 * To signal the read, we also clear bit 31.  We then poll on bit 31
4018 	 * and when it is set, we can then read our 4 bytes from the data
4019 	 * register.
4020 	 */
4021 	(void) pci_config_put32(hdl, offset, addr << 16);
4022 	do {
4023 		drv_usecwait(1000);
4024 		val = pci_config_get16(hdl, vpd_addr);
4025 		if (val & 0x8000) {		/* flag bit set */
4026 			*data = pci_config_get32(hdl, vpd_data);
4027 			return (DDI_SUCCESS);
4028 		}
4029 	} while (--retry);
4030 	/* read of flag failed write one message but count the failures */
4031 	if (debug_vpd == 0)
4032 		cmn_err(CE_NOTE,
4033 		    "!Failed to see flag bit after VPD addr write\n");
4034 	debug_vpd++;
4035 
4036 
4037 vpd_read_fail:
4038 	return (DDI_FAILURE);
4039 }
4040 
4041 
4042 
4043 /*
4044  *   hermon_pci_capability_vpd()
4045  *    Context: Only called from attach() path context
4046  */
4047 static void
4048 hermon_pci_capability_vpd(hermon_state_t *state, ddi_acc_handle_t hdl,
4049     uint_t offset)
4050 {
4051 	uint8_t			name_length;
4052 	uint8_t			pn_length;
4053 	int			i, err = 0;
4054 	int			vpd_str_id = 0;
4055 	int			vpd_ro_desc;
4056 	int			vpd_ro_pn_desc;
4057 #ifdef _BIG_ENDIAN
4058 	uint32_t		data32;
4059 #endif /* _BIG_ENDIAN */
4060 	union {
4061 		uint32_t	vpd_int[HERMON_VPD_HDR_DWSIZE];
4062 		uchar_t		vpd_char[HERMON_VPD_HDR_BSIZE];
4063 	} vpd;
4064 
4065 
4066 	/*
4067 	 * Read in the Vital Product Data (VPD) to the extend needed
4068 	 * by the fwflash utility
4069 	 */
4070 	for (i = 0; i < HERMON_VPD_HDR_DWSIZE; i++) {
4071 		err = hermon_pci_read_vpd(hdl, offset, i << 2, &vpd.vpd_int[i]);
4072 		if (err != DDI_SUCCESS) {
4073 			cmn_err(CE_NOTE, "!VPD read failed\n");
4074 			goto out;
4075 		}
4076 	}
4077 
4078 #ifdef _BIG_ENDIAN
4079 	/* Need to swap bytes for big endian. */
4080 	for (i = 0; i < HERMON_VPD_HDR_DWSIZE; i++) {
4081 		data32 = vpd.vpd_int[i];
4082 		vpd.vpd_char[(i << 2) + 3] =
4083 		    (uchar_t)((data32 & 0xFF000000) >> 24);
4084 		vpd.vpd_char[(i << 2) + 2] =
4085 		    (uchar_t)((data32 & 0x00FF0000) >> 16);
4086 		vpd.vpd_char[(i << 2) + 1] =
4087 		    (uchar_t)((data32 & 0x0000FF00) >> 8);
4088 		vpd.vpd_char[i << 2] = (uchar_t)(data32 & 0x000000FF);
4089 	}
4090 #endif	/* _BIG_ENDIAN */
4091 
4092 	/* Check for VPD String ID Tag */
4093 	if (vpd.vpd_char[vpd_str_id] == 0x82) {
4094 		/* get the product name */
4095 		name_length = (uint8_t)vpd.vpd_char[vpd_str_id + 1];
4096 		if (name_length > sizeof (state->hs_hca_name)) {
4097 			cmn_err(CE_NOTE, "!VPD name too large (0x%x)\n",
4098 			    name_length);
4099 			goto out;
4100 		}
4101 		(void) memcpy(state->hs_hca_name, &vpd.vpd_char[vpd_str_id + 3],
4102 		    name_length);
4103 		state->hs_hca_name[name_length] = 0;
4104 
4105 		/* get the part number */
4106 		vpd_ro_desc = name_length + 3; /* read-only tag location */
4107 		vpd_ro_pn_desc = vpd_ro_desc + 3; /* P/N keyword location */
4108 
4109 		/* Verify read-only tag and Part Number keyword. */
4110 		if (vpd.vpd_char[vpd_ro_desc] != 0x90 ||
4111 		    (vpd.vpd_char[vpd_ro_pn_desc] != 'P' &&
4112 		    vpd.vpd_char[vpd_ro_pn_desc + 1] != 'N')) {
4113 			cmn_err(CE_NOTE, "!VPD Part Number not found\n");
4114 			goto out;
4115 		}
4116 
4117 		pn_length = (uint8_t)vpd.vpd_char[vpd_ro_pn_desc + 2];
4118 		if (pn_length > sizeof (state->hs_hca_pn)) {
4119 			cmn_err(CE_NOTE, "!VPD part number too large (0x%x)\n",
4120 			    name_length);
4121 			goto out;
4122 		}
4123 		(void) memcpy(state->hs_hca_pn,
4124 		    &vpd.vpd_char[vpd_ro_pn_desc + 3],
4125 		    pn_length);
4126 		state->hs_hca_pn[pn_length] = 0;
4127 		state->hs_hca_pn_len = pn_length;
4128 		cmn_err(CE_CONT, "!vpd %s\n", state->hs_hca_pn);
4129 	} else {
4130 		/* Wrong VPD String ID Tag */
4131 		cmn_err(CE_NOTE, "!VPD String ID Tag not found, tag: %02x\n",
4132 		    vpd.vpd_char[0]);
4133 		goto out;
4134 	}
4135 	return;
4136 out:
4137 	state->hs_hca_pn_len = 0;
4138 }
4139 
4140 
4141 
4142 /*
4143  * hermon_intr_or_msi_init()
4144  *    Context: Only called from attach() path context
4145  */
4146 static int
4147 hermon_intr_or_msi_init(hermon_state_t *state)
4148 {
4149 	int	status;
4150 
4151 
4152 	/* Query for the list of supported interrupt event types */
4153 	status = ddi_intr_get_supported_types(state->hs_dip,
4154 	    &state->hs_intr_types_avail);
4155 	if (status != DDI_SUCCESS) {
4156 		return (DDI_FAILURE);
4157 	}
4158 
4159 	/*
4160 	 * If Hermon supports MSI-X in this system (and, if it
4161 	 * hasn't been overridden by a configuration variable), then
4162 	 * the default behavior is to use a single MSI-X.  Otherwise,
4163 	 * fallback to using legacy interrupts.  Also, if MSI-X is chosen,
4164 	 * but fails for whatever reasons, then next try MSI
4165 	 */
4166 	if ((state->hs_cfg_profile->cp_use_msi_if_avail != 0) &&
4167 	    (state->hs_intr_types_avail & DDI_INTR_TYPE_MSIX)) {
4168 		status = hermon_add_intrs(state, DDI_INTR_TYPE_MSIX);
4169 		if (status == DDI_SUCCESS) {
4170 			state->hs_intr_type_chosen = DDI_INTR_TYPE_MSIX;
4171 			return (DDI_SUCCESS);
4172 		}
4173 	}
4174 
4175 	/*
4176 	 * If Hermon supports MSI in this system (and, if it
4177 	 * hasn't been overridden by a configuration variable), then
4178 	 * the default behavior is to use a single MSIX.  Otherwise,
4179 	 * fallback to using legacy interrupts.  Also, if MSI is chosen,
4180 	 * but fails for whatever reasons, then fallback to using legacy
4181 	 * interrupts.
4182 	 */
4183 	if ((state->hs_cfg_profile->cp_use_msi_if_avail != 0) &&
4184 	    (state->hs_intr_types_avail & DDI_INTR_TYPE_MSI)) {
4185 		status = hermon_add_intrs(state, DDI_INTR_TYPE_MSI);
4186 		if (status == DDI_SUCCESS) {
4187 			state->hs_intr_type_chosen = DDI_INTR_TYPE_MSI;
4188 			return (DDI_SUCCESS);
4189 		}
4190 	}
4191 
4192 	/*
4193 	 * MSI interrupt allocation failed, or was not available.  Fallback to
4194 	 * legacy interrupt support.
4195 	 */
4196 	if (state->hs_intr_types_avail & DDI_INTR_TYPE_FIXED) {
4197 		status = hermon_add_intrs(state, DDI_INTR_TYPE_FIXED);
4198 		if (status == DDI_SUCCESS) {
4199 			state->hs_intr_type_chosen = DDI_INTR_TYPE_FIXED;
4200 			return (DDI_SUCCESS);
4201 		}
4202 	}
4203 
4204 	/*
4205 	 * None of MSI, MSI-X, nor legacy interrupts were successful.
4206 	 * Return failure.
4207 	 */
4208 	return (DDI_FAILURE);
4209 }
4210 
4211 /*
4212  * hermon_add_intrs()
4213  *    Context: Only called from attach() patch context
4214  */
4215 static int
4216 hermon_add_intrs(hermon_state_t *state, int intr_type)
4217 {
4218 	int	status;
4219 
4220 
4221 	/* Get number of interrupts/MSI supported */
4222 	status = ddi_intr_get_nintrs(state->hs_dip, intr_type,
4223 	    &state->hs_intrmsi_count);
4224 	if (status != DDI_SUCCESS) {
4225 		return (DDI_FAILURE);
4226 	}
4227 
4228 	/* Get number of available interrupts/MSI */
4229 	status = ddi_intr_get_navail(state->hs_dip, intr_type,
4230 	    &state->hs_intrmsi_avail);
4231 	if (status != DDI_SUCCESS) {
4232 		return (DDI_FAILURE);
4233 	}
4234 
4235 	/* Ensure that we have at least one (1) usable MSI or interrupt */
4236 	if ((state->hs_intrmsi_avail < 1) || (state->hs_intrmsi_count < 1)) {
4237 		return (DDI_FAILURE);
4238 	}
4239 
4240 	/* Attempt to allocate the maximum #interrupt/MSI handles */
4241 	status = ddi_intr_alloc(state->hs_dip, &state->hs_intrmsi_hdl[0],
4242 	    intr_type, 0, min(HERMON_MSIX_MAX, state->hs_intrmsi_avail),
4243 	    &state->hs_intrmsi_allocd, DDI_INTR_ALLOC_NORMAL);
4244 	if (status != DDI_SUCCESS) {
4245 		return (DDI_FAILURE);
4246 	}
4247 
4248 	/* Ensure that we have allocated at least one (1) MSI or interrupt */
4249 	if (state->hs_intrmsi_allocd < 1) {
4250 		return (DDI_FAILURE);
4251 	}
4252 	state->hs_eq_dist = state->hs_intrmsi_allocd - 1; /* start at 0 */
4253 
4254 	/*
4255 	 * Extract the priority for the allocated interrupt/MSI.  This
4256 	 * will be used later when initializing certain mutexes.
4257 	 */
4258 	status = ddi_intr_get_pri(state->hs_intrmsi_hdl[0],
4259 	    &state->hs_intrmsi_pri);
4260 	if (status != DDI_SUCCESS) {
4261 		/* Free the allocated interrupt/MSI handle */
4262 		(void) ddi_intr_free(state->hs_intrmsi_hdl[0]);
4263 
4264 		return (DDI_FAILURE);
4265 	}
4266 
4267 	/* Make sure the interrupt/MSI priority is below 'high level' */
4268 	if (state->hs_intrmsi_pri >= ddi_intr_get_hilevel_pri()) {
4269 		/* Free the allocated interrupt/MSI handle */
4270 		(void) ddi_intr_free(state->hs_intrmsi_hdl[0]);
4271 
4272 		return (DDI_FAILURE);
4273 	}
4274 
4275 	/* Get add'l capability information regarding interrupt/MSI */
4276 	status = ddi_intr_get_cap(state->hs_intrmsi_hdl[0],
4277 	    &state->hs_intrmsi_cap);
4278 	if (status != DDI_SUCCESS) {
4279 		/* Free the allocated interrupt/MSI handle */
4280 		(void) ddi_intr_free(state->hs_intrmsi_hdl[0]);
4281 
4282 		return (DDI_FAILURE);
4283 	}
4284 
4285 	return (DDI_SUCCESS);
4286 }
4287 
4288 
4289 /*
4290  * hermon_intr_or_msi_fini()
4291  *    Context: Only called from attach() and/or detach() path contexts
4292  */
4293 static int
4294 hermon_intr_or_msi_fini(hermon_state_t *state)
4295 {
4296 	int	status;
4297 	int	intr;
4298 
4299 	for (intr = 0; intr < state->hs_intrmsi_allocd; intr++) {
4300 
4301 		/* Free the allocated interrupt/MSI handle */
4302 		status = ddi_intr_free(state->hs_intrmsi_hdl[intr]);
4303 		if (status != DDI_SUCCESS) {
4304 			return (DDI_FAILURE);
4305 		}
4306 	}
4307 	return (DDI_SUCCESS);
4308 }
4309 
4310 
4311 /*ARGSUSED*/
4312 void
4313 hermon_pci_capability_msix(hermon_state_t *state, ddi_acc_handle_t hdl,
4314     uint_t offset)
4315 {
4316 	uint32_t	msix_data;
4317 	uint16_t	msg_cntr;
4318 	uint32_t	t_offset;	/* table offset */
4319 	uint32_t	t_bir;
4320 	uint32_t	p_offset;	/* pba */
4321 	uint32_t	p_bir;
4322 	int		t_size;		/* size in entries - each is 4 dwords */
4323 
4324 	/* come in with offset pointing at the capability structure */
4325 
4326 	msix_data = pci_config_get32(hdl, offset);
4327 	cmn_err(CE_CONT, "Full cap structure dword = %X\n", msix_data);
4328 	msg_cntr =  pci_config_get16(hdl, offset+2);
4329 	cmn_err(CE_CONT, "MSIX msg_control = %X\n", msg_cntr);
4330 	offset += 4;
4331 	msix_data = pci_config_get32(hdl, offset);	/* table info */
4332 	t_offset = (msix_data & 0xFFF8) >> 3;
4333 	t_bir = msix_data & 0x07;
4334 	offset += 4;
4335 	cmn_err(CE_CONT, "  table %X --offset = %X, bir(bar) = %X\n",
4336 	    msix_data, t_offset, t_bir);
4337 	msix_data = pci_config_get32(hdl, offset);	/* PBA info */
4338 	p_offset = (msix_data & 0xFFF8) >> 3;
4339 	p_bir = msix_data & 0x07;
4340 
4341 	cmn_err(CE_CONT, "  PBA   %X --offset = %X, bir(bar) = %X\n",
4342 	    msix_data, p_offset, p_bir);
4343 	t_size = msg_cntr & 0x7FF;		/* low eleven bits */
4344 	cmn_err(CE_CONT, "    table size = %X entries\n", t_size);
4345 
4346 	offset = t_offset;		/* reuse this for offset from BAR */
4347 #ifdef HERMON_SUPPORTS_MSIX_BAR
4348 	cmn_err(CE_CONT, "First 2 table entries behind BAR2 \n");
4349 	for (i = 0; i < 2; i++) {
4350 		for (j = 0; j < 4; j++, offset += 4) {
4351 			msix_data = ddi_get32(state->hs_reg_msihdl,
4352 			    (uint32_t *)((uintptr_t)state->hs_reg_msi_baseaddr
4353 			    + offset));
4354 			cmn_err(CE_CONT, "MSI table entry %d, dword %d == %X\n",
4355 			    i, j, msix_data);
4356 		}
4357 	}
4358 #endif
4359 
4360 }
4361 
4362 /*
4363  * X86 fastreboot support functions.
4364  * These functions are used to save/restore MSI-X table/PBA and also
4365  * to disable MSI-X interrupts in hermon_quiesce().
4366  */
4367 
4368 /* Return the message control for MSI-X */
4369 static ushort_t
4370 get_msix_ctrl(dev_info_t *dip)
4371 {
4372 	ushort_t msix_ctrl = 0, caps_ctrl = 0;
4373 	hermon_state_t *state = ddi_get_soft_state(hermon_statep,
4374 	    DEVI(dip)->devi_instance);
4375 	ddi_acc_handle_t pci_cfg_hdl = hermon_get_pcihdl(state);
4376 	ASSERT(pci_cfg_hdl != NULL);
4377 
4378 	if ((PCI_CAP_LOCATE(pci_cfg_hdl,
4379 	    PCI_CAP_ID_MSI_X, &caps_ctrl) == DDI_SUCCESS)) {
4380 		if ((msix_ctrl = PCI_CAP_GET16(pci_cfg_hdl, NULL, caps_ctrl,
4381 		    PCI_MSIX_CTRL)) == PCI_CAP_EINVAL16)
4382 			return (0);
4383 	}
4384 	ASSERT(msix_ctrl != 0);
4385 
4386 	return (msix_ctrl);
4387 }
4388 
4389 /* Return the MSI-X table size */
4390 static size_t
4391 get_msix_tbl_size(dev_info_t *dip)
4392 {
4393 	ushort_t msix_ctrl = get_msix_ctrl(dip);
4394 	ASSERT(msix_ctrl != 0);
4395 
4396 	return (((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 1) *
4397 	    PCI_MSIX_VECTOR_SIZE);
4398 }
4399 
4400 /* Return the MSI-X PBA size */
4401 static size_t
4402 get_msix_pba_size(dev_info_t *dip)
4403 {
4404 	ushort_t msix_ctrl = get_msix_ctrl(dip);
4405 	ASSERT(msix_ctrl != 0);
4406 
4407 	return (((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 64) / 64 * 8);
4408 }
4409 
4410 /* Set up the MSI-X table/PBA save area */
4411 static void
4412 hermon_set_msix_info(hermon_state_t *state)
4413 {
4414 	uint_t			rnumber, breg, nregs;
4415 	ushort_t		caps_ctrl, msix_ctrl;
4416 	pci_regspec_t		*rp;
4417 	int			reg_size, addr_space, offset, *regs_list, i;
4418 
4419 	/*
4420 	 * MSI-X BIR Index Table:
4421 	 * BAR indicator register (BIR) to Base Address register.
4422 	 */
4423 	uchar_t pci_msix_bir_index[8] = {0x10, 0x14, 0x18, 0x1c,
4424 	    0x20, 0x24, 0xff, 0xff};
4425 
4426 	/* Fastreboot data access  attribute */
4427 	ddi_device_acc_attr_t	dev_attr = {
4428 		0,				/* version */
4429 		DDI_STRUCTURE_LE_ACC,
4430 		DDI_STRICTORDER_ACC,		/* attr access */
4431 		0
4432 	};
4433 
4434 	ddi_acc_handle_t pci_cfg_hdl = hermon_get_pcihdl(state);
4435 	ASSERT(pci_cfg_hdl != NULL);
4436 
4437 	if ((PCI_CAP_LOCATE(pci_cfg_hdl,
4438 	    PCI_CAP_ID_MSI_X, &caps_ctrl) == DDI_SUCCESS)) {
4439 		if ((msix_ctrl = PCI_CAP_GET16(pci_cfg_hdl, NULL, caps_ctrl,
4440 		    PCI_MSIX_CTRL)) == PCI_CAP_EINVAL16)
4441 			return;
4442 	}
4443 	ASSERT(msix_ctrl != 0);
4444 
4445 	state->hs_msix_tbl_offset = PCI_CAP_GET32(pci_cfg_hdl, NULL, caps_ctrl,
4446 	    PCI_MSIX_TBL_OFFSET);
4447 
4448 	/* Get the BIR for MSI-X table */
4449 	breg = pci_msix_bir_index[state->hs_msix_tbl_offset &
4450 	    PCI_MSIX_TBL_BIR_MASK];
4451 	ASSERT(breg != 0xFF);
4452 
4453 	/* Set the MSI-X table offset */
4454 	state->hs_msix_tbl_offset = state->hs_msix_tbl_offset &
4455 	    ~PCI_MSIX_TBL_BIR_MASK;
4456 
4457 	/* Set the MSI-X table size */
4458 	state->hs_msix_tbl_size = ((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 1) *
4459 	    PCI_MSIX_VECTOR_SIZE;
4460 
4461 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, state->hs_dip,
4462 	    DDI_PROP_DONTPASS, "reg", (int **)&regs_list, &nregs) !=
4463 	    DDI_PROP_SUCCESS) {
4464 		return;
4465 	}
4466 	reg_size = sizeof (pci_regspec_t) / sizeof (int);
4467 
4468 	/* Check the register number for MSI-X table */
4469 	for (i = 1, rnumber = 0; i < nregs/reg_size; i++) {
4470 		rp = (pci_regspec_t *)&regs_list[i * reg_size];
4471 		addr_space = rp->pci_phys_hi & PCI_ADDR_MASK;
4472 		offset = PCI_REG_REG_G(rp->pci_phys_hi);
4473 
4474 		if ((offset == breg) && ((addr_space == PCI_ADDR_MEM32) ||
4475 		    (addr_space == PCI_ADDR_MEM64))) {
4476 			rnumber = i;
4477 			break;
4478 		}
4479 	}
4480 	ASSERT(rnumber != 0);
4481 	state->hs_msix_tbl_rnumber = rnumber;
4482 
4483 	/* Set device attribute version and access according to Hermon FM */
4484 	dev_attr.devacc_attr_version = hermon_devacc_attr_version(state);
4485 	dev_attr.devacc_attr_access = hermon_devacc_attr_access(state);
4486 
4487 	/* Map the entire MSI-X vector table */
4488 	if (hermon_regs_map_setup(state, state->hs_msix_tbl_rnumber,
4489 	    (caddr_t *)&state->hs_msix_tbl_addr, state->hs_msix_tbl_offset,
4490 	    state->hs_msix_tbl_size, &dev_attr,
4491 	    &state->hs_fm_msix_tblhdl) != DDI_SUCCESS) {
4492 		return;
4493 	}
4494 
4495 	state->hs_msix_pba_offset = PCI_CAP_GET32(pci_cfg_hdl, NULL, caps_ctrl,
4496 	    PCI_MSIX_PBA_OFFSET);
4497 
4498 	/* Get the BIR for MSI-X PBA */
4499 	breg = pci_msix_bir_index[state->hs_msix_pba_offset &
4500 	    PCI_MSIX_PBA_BIR_MASK];
4501 	ASSERT(breg != 0xFF);
4502 
4503 	/* Set the MSI-X PBA offset */
4504 	state->hs_msix_pba_offset = state->hs_msix_pba_offset &
4505 	    ~PCI_MSIX_PBA_BIR_MASK;
4506 
4507 	/* Set the MSI-X PBA size */
4508 	state->hs_msix_pba_size =
4509 	    ((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 64) / 64 * 8;
4510 
4511 	/* Check the register number for MSI-X PBA */
4512 	for (i = 1, rnumber = 0; i < nregs/reg_size; i++) {
4513 		rp = (pci_regspec_t *)&regs_list[i * reg_size];
4514 		addr_space = rp->pci_phys_hi & PCI_ADDR_MASK;
4515 		offset = PCI_REG_REG_G(rp->pci_phys_hi);
4516 
4517 		if ((offset == breg) && ((addr_space == PCI_ADDR_MEM32) ||
4518 		    (addr_space == PCI_ADDR_MEM64))) {
4519 			rnumber = i;
4520 			break;
4521 		}
4522 	}
4523 	ASSERT(rnumber != 0);
4524 	state->hs_msix_pba_rnumber = rnumber;
4525 
4526 	/* Map in the MSI-X Pending Bit Array */
4527 	if (hermon_regs_map_setup(state, state->hs_msix_pba_rnumber,
4528 	    (caddr_t *)&state->hs_msix_pba_addr, state->hs_msix_pba_offset,
4529 	    state->hs_msix_pba_size, &dev_attr,
4530 	    &state->hs_fm_msix_pbahdl) != DDI_SUCCESS) {
4531 		hermon_regs_map_free(state, &state->hs_fm_msix_tblhdl);
4532 		state->hs_fm_msix_tblhdl = NULL;
4533 		return;
4534 	}
4535 
4536 	/* Set the MSI-X table save area */
4537 	state->hs_msix_tbl_entries = kmem_alloc(state->hs_msix_tbl_size,
4538 	    KM_SLEEP);
4539 
4540 	/* Set the MSI-X PBA save area */
4541 	state->hs_msix_pba_entries = kmem_alloc(state->hs_msix_pba_size,
4542 	    KM_SLEEP);
4543 }
4544 
4545 /* Disable Hermon interrupts */
4546 static int
4547 hermon_intr_disable(hermon_state_t *state)
4548 {
4549 	ushort_t msix_ctrl = 0, caps_ctrl = 0;
4550 	ddi_acc_handle_t pci_cfg_hdl = hermon_get_pcihdl(state);
4551 	ddi_acc_handle_t msix_tblhdl = hermon_get_msix_tblhdl(state);
4552 	int i, j;
4553 	ASSERT(pci_cfg_hdl != NULL && msix_tblhdl != NULL);
4554 	ASSERT(state->hs_intr_types_avail &
4555 	    (DDI_INTR_TYPE_FIXED | DDI_INTR_TYPE_MSI | DDI_INTR_TYPE_MSIX));
4556 
4557 	/*
4558 	 * Check if MSI-X interrupts are used. If so, disable MSI-X interupts.
4559 	 * If not, since Hermon doesn't support MSI interrupts, assuming the
4560 	 * legacy interrupt is used instead, disable the legacy interrupt.
4561 	 */
4562 	if ((state->hs_cfg_profile->cp_use_msi_if_avail != 0) &&
4563 	    (state->hs_intr_types_avail & DDI_INTR_TYPE_MSIX)) {
4564 
4565 		if ((PCI_CAP_LOCATE(pci_cfg_hdl,
4566 		    PCI_CAP_ID_MSI_X, &caps_ctrl) == DDI_SUCCESS)) {
4567 			if ((msix_ctrl = PCI_CAP_GET16(pci_cfg_hdl, NULL,
4568 			    caps_ctrl, PCI_MSIX_CTRL)) == PCI_CAP_EINVAL16)
4569 				return (DDI_FAILURE);
4570 		}
4571 		ASSERT(msix_ctrl != 0);
4572 
4573 		if (!(msix_ctrl & PCI_MSIX_ENABLE_BIT))
4574 			return (DDI_SUCCESS);
4575 
4576 		/* Clear all inums in MSI-X table */
4577 		for (i = 0; i < get_msix_tbl_size(state->hs_dip);
4578 		    i += PCI_MSIX_VECTOR_SIZE) {
4579 			for (j = 0; j < PCI_MSIX_VECTOR_SIZE; j += 4) {
4580 				char *addr = state->hs_msix_tbl_addr + i + j;
4581 				ddi_put32(msix_tblhdl,
4582 				    (uint32_t *)(uintptr_t)addr, 0x0);
4583 			}
4584 		}
4585 
4586 		/* Disable MSI-X interrupts */
4587 		msix_ctrl &= ~PCI_MSIX_ENABLE_BIT;
4588 		PCI_CAP_PUT16(pci_cfg_hdl, NULL, caps_ctrl, PCI_MSIX_CTRL,
4589 		    msix_ctrl);
4590 
4591 	} else {
4592 		uint16_t cmdreg = pci_config_get16(pci_cfg_hdl, PCI_CONF_COMM);
4593 		ASSERT(state->hs_intr_types_avail & DDI_INTR_TYPE_FIXED);
4594 
4595 		/* Disable the legacy interrupts */
4596 		cmdreg |= PCI_COMM_INTX_DISABLE;
4597 		pci_config_put16(pci_cfg_hdl, PCI_CONF_COMM, cmdreg);
4598 	}
4599 
4600 	return (DDI_SUCCESS);
4601 }
4602 
4603 /* Hermon quiesce(9F) entry */
4604 static int
4605 hermon_quiesce(dev_info_t *dip)
4606 {
4607 	hermon_state_t *state = ddi_get_soft_state(hermon_statep,
4608 	    DEVI(dip)->devi_instance);
4609 	ddi_acc_handle_t pcihdl = hermon_get_pcihdl(state);
4610 	ddi_acc_handle_t cmdhdl = hermon_get_cmdhdl(state);
4611 	ddi_acc_handle_t msix_tbl_hdl = hermon_get_msix_tblhdl(state);
4612 	ddi_acc_handle_t msix_pba_hdl = hermon_get_msix_pbahdl(state);
4613 	uint32_t sem, reset_delay = state->hs_cfg_profile->cp_sw_reset_delay;
4614 	uint64_t data64;
4615 	uint32_t data32;
4616 	int status, i, j, loopcnt;
4617 	uint_t offset;
4618 
4619 	ASSERT(state != NULL);
4620 
4621 	/* start fastreboot */
4622 	state->hs_quiescing = B_TRUE;
4623 
4624 	/* If it's in maintenance mode, do nothing but return with SUCCESS */
4625 	if (!HERMON_IS_OPERATIONAL(state->hs_operational_mode)) {
4626 		return (DDI_SUCCESS);
4627 	}
4628 
4629 	/* suppress Hermon FM ereports */
4630 	if (hermon_get_state(state) & HCA_EREPORT_FM) {
4631 		hermon_clr_state_nolock(state, HCA_EREPORT_FM);
4632 	}
4633 
4634 	/* Shutdown HCA ports */
4635 	if (hermon_hca_ports_shutdown(state,
4636 	    state->hs_cfg_profile->cp_num_ports) != HERMON_CMD_SUCCESS) {
4637 		state->hs_quiescing = B_FALSE;
4638 		return (DDI_FAILURE);
4639 	}
4640 
4641 	/* Close HCA */
4642 	if (hermon_close_hca_cmd_post(state, HERMON_CMD_NOSLEEP_SPIN) !=
4643 	    HERMON_CMD_SUCCESS) {
4644 		state->hs_quiescing = B_FALSE;
4645 		return (DDI_FAILURE);
4646 	}
4647 
4648 	/* Disable interrupts */
4649 	if (hermon_intr_disable(state) != DDI_SUCCESS) {
4650 		state->hs_quiescing = B_FALSE;
4651 		return (DDI_FAILURE);
4652 	}
4653 
4654 	/*
4655 	 * Query the PCI capabilities of the HCA device, but don't process
4656 	 * the VPD until after reset.
4657 	 */
4658 	if (hermon_pci_capability_list(state, pcihdl) != DDI_SUCCESS) {
4659 		state->hs_quiescing = B_FALSE;
4660 		return (DDI_FAILURE);
4661 	}
4662 
4663 	/*
4664 	 * Read all PCI config info (reg0...reg63).  Note: According to the
4665 	 * Hermon software reset application note, we should not read or
4666 	 * restore the values in reg22 and reg23.
4667 	 * NOTE:  For Hermon (and Arbel too) it says to restore the command
4668 	 * register LAST, and technically, you need to restore the
4669 	 * PCIE Capability "device control" and "link control" (word-sized,
4670 	 * at offsets 0x08 and 0x10 from the capbility ID respectively).
4671 	 * We hold off restoring the command register - offset 0x4 - till last
4672 	 */
4673 
4674 	/* 1st, wait for the semaphore assure accessibility - per PRM */
4675 	status = -1;
4676 	for (i = 0; i < NANOSEC/MICROSEC /* 1sec timeout */; i++) {
4677 		sem = ddi_get32(cmdhdl, state->hs_cmd_regs.sw_semaphore);
4678 		if (sem == 0) {
4679 			status = 0;
4680 			break;
4681 		}
4682 		drv_usecwait(1);
4683 	}
4684 
4685 	/* Check if timeout happens */
4686 	if (status == -1) {
4687 		state->hs_quiescing = B_FALSE;
4688 		return (DDI_FAILURE);
4689 	}
4690 
4691 	/* MSI-X interrupts are used, save the MSI-X table */
4692 	if (msix_tbl_hdl && msix_pba_hdl) {
4693 		/* save MSI-X table */
4694 		for (i = 0; i < get_msix_tbl_size(state->hs_dip);
4695 		    i += PCI_MSIX_VECTOR_SIZE) {
4696 			for (j = 0; j < PCI_MSIX_VECTOR_SIZE; j += 4) {
4697 				char *addr = state->hs_msix_tbl_addr + i + j;
4698 				data32 = ddi_get32(msix_tbl_hdl,
4699 				    (uint32_t *)(uintptr_t)addr);
4700 				*(uint32_t *)(uintptr_t)(state->
4701 				    hs_msix_tbl_entries + i + j) = data32;
4702 			}
4703 		}
4704 		/* save MSI-X PBA */
4705 		for (i = 0; i < get_msix_pba_size(state->hs_dip); i += 8) {
4706 			char *addr = state->hs_msix_pba_addr + i;
4707 			data64 = ddi_get64(msix_pba_hdl,
4708 			    (uint64_t *)(uintptr_t)addr);
4709 			*(uint64_t *)(uintptr_t)(state->
4710 			    hs_msix_pba_entries + i) = data64;
4711 		}
4712 	}
4713 
4714 	/* save PCI config space */
4715 	for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) {
4716 		if ((i != HERMON_SW_RESET_REG22_RSVD) &&
4717 		    (i != HERMON_SW_RESET_REG23_RSVD)) {
4718 			state->hs_cfg_data[i]  =
4719 			    pci_config_get32(pcihdl, i << 2);
4720 		}
4721 	}
4722 
4723 	/* SW-reset HCA */
4724 	ddi_put32(cmdhdl, state->hs_cmd_regs.sw_reset, HERMON_SW_RESET_START);
4725 
4726 	/*
4727 	 * This delay is required so as not to cause a panic here. If the
4728 	 * device is accessed too soon after reset it will not respond to
4729 	 * config cycles, causing a Master Abort and panic.
4730 	 */
4731 	drv_usecwait(reset_delay);
4732 
4733 	/* Poll waiting for the device to finish resetting */
4734 	loopcnt = 100;	/* 100 times @ 100 usec - total delay 10 msec */
4735 	while ((pci_config_get32(pcihdl, 0) & 0x0000FFFF) != PCI_VENID_MLX) {
4736 		drv_usecwait(HERMON_SW_RESET_POLL_DELAY);
4737 		if (--loopcnt == 0)
4738 			break;	/* just in case, break and go on */
4739 	}
4740 	if (loopcnt == 0) {
4741 		state->hs_quiescing = B_FALSE;
4742 		return (DDI_FAILURE);
4743 	}
4744 
4745 	/* Restore the config info */
4746 	for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) {
4747 		if (i == 1) continue;	/* skip the status/ctrl reg */
4748 		if ((i != HERMON_SW_RESET_REG22_RSVD) &&
4749 		    (i != HERMON_SW_RESET_REG23_RSVD)) {
4750 			pci_config_put32(pcihdl, i << 2, state->hs_cfg_data[i]);
4751 		}
4752 	}
4753 
4754 	/* If MSI-X interrupts are used, restore the MSI-X table */
4755 	if (msix_tbl_hdl && msix_pba_hdl) {
4756 		/* restore MSI-X PBA */
4757 		for (i = 0; i < get_msix_pba_size(state->hs_dip); i += 8) {
4758 			char *addr = state->hs_msix_pba_addr + i;
4759 			data64 = *(uint64_t *)(uintptr_t)
4760 			    (state->hs_msix_pba_entries + i);
4761 			ddi_put64(msix_pba_hdl,
4762 			    (uint64_t *)(uintptr_t)addr, data64);
4763 		}
4764 		/* restore MSI-X table */
4765 		for (i = 0; i < get_msix_tbl_size(state->hs_dip);
4766 		    i += PCI_MSIX_VECTOR_SIZE) {
4767 			for (j = 0; j < PCI_MSIX_VECTOR_SIZE; j += 4) {
4768 				char *addr = state->hs_msix_tbl_addr + i + j;
4769 				data32 = *(uint32_t *)(uintptr_t)
4770 				    (state->hs_msix_tbl_entries + i + j);
4771 				ddi_put32(msix_tbl_hdl,
4772 				    (uint32_t *)(uintptr_t)addr, data32);
4773 			}
4774 		}
4775 	}
4776 
4777 	/*
4778 	 * PCI Express Capability - we saved during capability list, and
4779 	 * we'll restore them here.
4780 	 */
4781 	offset = state->hs_pci_cap_offset;
4782 	data32 = state->hs_pci_cap_devctl;
4783 	pci_config_put32(pcihdl, offset + HERMON_PCI_CAP_DEV_OFFS, data32);
4784 	data32 = state->hs_pci_cap_lnkctl;
4785 	pci_config_put32(pcihdl, offset + HERMON_PCI_CAP_LNK_OFFS, data32);
4786 
4787 	/* restore the command register */
4788 	pci_config_put32(pcihdl, 0x04, (state->hs_cfg_data[1] | 0x0006));
4789 
4790 	return (DDI_SUCCESS);
4791 }
4792