xref: /illumos-gate/usr/src/uts/common/io/ib/adapters/hermon/hermon.c (revision 07a48826732249fcd3aa8dd53c8389595e9f1fbc)
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 		state->hs_fm_degraded_reason = HCA_FW_MISC; /* not fw reason */
1175 		return (DDI_FAILURE);
1176 
1177 	} else {
1178 		state->hs_operational_mode = 0;	/* invalid operational mode */
1179 		HERMON_FMANOTE(state, HERMON_FMA_PCIID);
1180 		HERMON_WARNING(state, "unexpected device type detected");
1181 		return (DDI_FAILURE);
1182 	}
1183 
1184 	/*
1185 	 * Initialize the Hermon hardware.
1186 	 *
1187 	 * Note:  If this routine returns an error, it is often a reasonably
1188 	 * good indication that something Hermon firmware-related has caused
1189 	 * the failure or some HW related errors have caused the failure.
1190 	 * (also there are few possibilities that SW (e.g. SW resource
1191 	 * shortage) can cause the failure, but the majority case is due to
1192 	 * either a firmware related error or a HW related one) In order to
1193 	 * give the user an opportunity (if desired) to update or reflash
1194 	 * the Hermon firmware image, we set "hs_operational_mode" flag
1195 	 * (described above) to indicate that we wish to enter maintenance
1196 	 * mode in case of the firmware-related issue.
1197 	 */
1198 	status = hermon_hw_init(state);
1199 	if (status != DDI_SUCCESS) {
1200 		cmn_err(CE_NOTE, "hermon%d: error during attach: %s", instance,
1201 		    state->hs_attach_buf);
1202 		return (DDI_FAILURE);
1203 	}
1204 
1205 	/*
1206 	 * Now that the ISR has been setup, arm all the EQs for event
1207 	 * generation.
1208 	 */
1209 
1210 	status = hermon_eq_arm_all(state);
1211 	if (status != DDI_SUCCESS) {
1212 		cmn_err(CE_NOTE, "EQ Arm All failed\n");
1213 		hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL);
1214 		return (DDI_FAILURE);
1215 	}
1216 
1217 	/* test interrupts and event queues */
1218 	status = hermon_nop_post(state, 0x0, 0x0);
1219 	if (status != DDI_SUCCESS) {
1220 		cmn_err(CE_NOTE, "Interrupts/EQs failed\n");
1221 		hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL);
1222 		return (DDI_FAILURE);
1223 	}
1224 
1225 	/* Initialize Hermon softstate */
1226 	status = hermon_soft_state_init(state);
1227 	if (status != DDI_SUCCESS) {
1228 		cmn_err(CE_NOTE, "Failed to init soft state\n");
1229 		hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL);
1230 		return (DDI_FAILURE);
1231 	}
1232 
1233 	return (DDI_SUCCESS);
1234 }
1235 
1236 
1237 /*
1238  * hermon_drv_fini()
1239  *    Context: Only called from attach() and/or detach() path contexts
1240  */
1241 static void
1242 hermon_drv_fini(hermon_state_t *state)
1243 {
1244 	/* Cleanup Hermon softstate */
1245 	hermon_soft_state_fini(state);
1246 
1247 	/* Cleanup Hermon resources and shutdown hardware */
1248 	hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL);
1249 }
1250 
1251 
1252 /*
1253  * hermon_drv_fini2()
1254  *    Context: Only called from attach() and/or detach() path contexts
1255  */
1256 static void
1257 hermon_drv_fini2(hermon_state_t *state)
1258 {
1259 	if (state->hs_fm_poll_thread) {
1260 		ddi_periodic_delete(state->hs_fm_poll_thread);
1261 		state->hs_fm_poll_thread = NULL;
1262 	}
1263 
1264 	/* HERMON_DRV_CLEANUP_LEVEL1 */
1265 	if (state->hs_fm_cmdhdl) {
1266 		hermon_regs_map_free(state, &state->hs_fm_cmdhdl);
1267 		state->hs_fm_cmdhdl = NULL;
1268 	}
1269 
1270 	if (state->hs_reg_cmdhdl) {
1271 		ddi_regs_map_free(&state->hs_reg_cmdhdl);
1272 		state->hs_reg_cmdhdl = NULL;
1273 	}
1274 
1275 	/* HERMON_DRV_CLEANUP_LEVEL0 */
1276 	if (state->hs_msix_tbl_entries) {
1277 		kmem_free(state->hs_msix_tbl_entries,
1278 		    state->hs_msix_tbl_size);
1279 		state->hs_msix_tbl_entries = NULL;
1280 	}
1281 
1282 	if (state->hs_msix_pba_entries) {
1283 		kmem_free(state->hs_msix_pba_entries,
1284 		    state->hs_msix_pba_size);
1285 		state->hs_msix_pba_entries = NULL;
1286 	}
1287 
1288 	if (state->hs_fm_msix_tblhdl) {
1289 		hermon_regs_map_free(state, &state->hs_fm_msix_tblhdl);
1290 		state->hs_fm_msix_tblhdl = NULL;
1291 	}
1292 
1293 	if (state->hs_reg_msix_tblhdl) {
1294 		ddi_regs_map_free(&state->hs_reg_msix_tblhdl);
1295 		state->hs_reg_msix_tblhdl = NULL;
1296 	}
1297 
1298 	if (state->hs_fm_msix_pbahdl) {
1299 		hermon_regs_map_free(state, &state->hs_fm_msix_pbahdl);
1300 		state->hs_fm_msix_pbahdl = NULL;
1301 	}
1302 
1303 	if (state->hs_reg_msix_pbahdl) {
1304 		ddi_regs_map_free(&state->hs_reg_msix_pbahdl);
1305 		state->hs_reg_msix_pbahdl = NULL;
1306 	}
1307 
1308 	if (state->hs_fm_pcihdl) {
1309 		hermon_pci_config_teardown(state, &state->hs_fm_pcihdl);
1310 		state->hs_fm_pcihdl = NULL;
1311 	}
1312 
1313 	if (state->hs_reg_pcihdl) {
1314 		pci_config_teardown(&state->hs_reg_pcihdl);
1315 		state->hs_reg_pcihdl = NULL;
1316 	}
1317 }
1318 
1319 
1320 /*
1321  * hermon_isr_init()
1322  *    Context: Only called from attach() path context
1323  */
1324 static int
1325 hermon_isr_init(hermon_state_t *state)
1326 {
1327 	int	status;
1328 	int	intr;
1329 
1330 	for (intr = 0; intr < state->hs_intrmsi_allocd; intr++) {
1331 
1332 		/*
1333 		 * Add a handler for the interrupt or MSI
1334 		 */
1335 		status = ddi_intr_add_handler(state->hs_intrmsi_hdl[intr],
1336 		    hermon_isr, (caddr_t)state, (void *)(uintptr_t)intr);
1337 		if (status  != DDI_SUCCESS) {
1338 			return (DDI_FAILURE);
1339 		}
1340 
1341 		/*
1342 		 * Enable the software interrupt.  Note: depending on the value
1343 		 * returned in the capability flag, we have to call either
1344 		 * ddi_intr_block_enable() or ddi_intr_enable().
1345 		 */
1346 		if (state->hs_intrmsi_cap & DDI_INTR_FLAG_BLOCK) {
1347 			status = ddi_intr_block_enable(
1348 			    &state->hs_intrmsi_hdl[intr], 1);
1349 			if (status != DDI_SUCCESS) {
1350 				return (DDI_FAILURE);
1351 			}
1352 		} else {
1353 			status = ddi_intr_enable(state->hs_intrmsi_hdl[intr]);
1354 			if (status != DDI_SUCCESS) {
1355 				return (DDI_FAILURE);
1356 			}
1357 		}
1358 	}
1359 
1360 	/*
1361 	 * Now that the ISR has been enabled, defer arm_all  EQs for event
1362 	 * generation until later, in case MSIX is enabled
1363 	 */
1364 	return (DDI_SUCCESS);
1365 }
1366 
1367 
1368 /*
1369  * hermon_isr_fini()
1370  *    Context: Only called from attach() and/or detach() path contexts
1371  */
1372 static void
1373 hermon_isr_fini(hermon_state_t *state)
1374 {
1375 	int	intr;
1376 
1377 	for (intr = 0; intr < state->hs_intrmsi_allocd; intr++) {
1378 		/* Disable the software interrupt */
1379 		if (state->hs_intrmsi_cap & DDI_INTR_FLAG_BLOCK) {
1380 			(void) ddi_intr_block_disable(
1381 			    &state->hs_intrmsi_hdl[intr], 1);
1382 		} else {
1383 			(void) ddi_intr_disable(state->hs_intrmsi_hdl[intr]);
1384 		}
1385 
1386 		/*
1387 		 * Remove the software handler for the interrupt or MSI
1388 		 */
1389 		(void) ddi_intr_remove_handler(state->hs_intrmsi_hdl[intr]);
1390 	}
1391 }
1392 
1393 
1394 /*
1395  * Sum of ICM configured values:
1396  *     cMPT, dMPT, MTT, QPC, SRQC, RDB, CQC, ALTC, AUXC, EQC, MCG
1397  *
1398  */
1399 static uint64_t
1400 hermon_size_icm(hermon_state_t *state)
1401 {
1402 	hermon_hw_querydevlim_t	*devlim;
1403 	hermon_cfg_profile_t	*cfg;
1404 	uint64_t		num_cmpts, num_dmpts, num_mtts;
1405 	uint64_t		num_qpcs, num_srqc, num_rdbs;
1406 #ifndef HERMON_FW_WORKAROUND
1407 	uint64_t		num_auxc;
1408 #endif
1409 	uint64_t		num_cqcs, num_altc;
1410 	uint64_t		num_eqcs, num_mcgs;
1411 	uint64_t		size;
1412 
1413 	devlim = &state->hs_devlim;
1414 	cfg = state->hs_cfg_profile;
1415 	/* number of respective entries */
1416 	num_cmpts = (uint64_t)0x1 << cfg->cp_log_num_cmpt;
1417 	num_mtts = (uint64_t)0x1 << cfg->cp_log_num_mtt;
1418 	num_dmpts = (uint64_t)0x1 << cfg->cp_log_num_dmpt;
1419 	num_qpcs = (uint64_t)0x1 << cfg->cp_log_num_qp;
1420 	num_srqc = (uint64_t)0x1 << cfg->cp_log_num_srq;
1421 	num_rdbs = (uint64_t)0x1 << cfg->cp_log_num_rdb;
1422 	num_cqcs = (uint64_t)0x1 << cfg->cp_log_num_cq;
1423 	num_altc = (uint64_t)0x1 << cfg->cp_log_num_qp;
1424 #ifndef HERMON_FW_WORKAROUND
1425 	num_auxc = (uint64_t)0x1 << cfg->cp_log_num_qp;
1426 #endif
1427 	num_eqcs = (uint64_t)0x1 << cfg->cp_log_num_eq;
1428 	num_mcgs = (uint64_t)0x1 << cfg->cp_log_num_mcg;
1429 
1430 	size =
1431 	    num_cmpts 	* devlim->cmpt_entry_sz +
1432 	    num_dmpts	* devlim->dmpt_entry_sz +
1433 	    num_mtts	* devlim->mtt_entry_sz +
1434 	    num_qpcs	* devlim->qpc_entry_sz +
1435 	    num_srqc	* devlim->srq_entry_sz +
1436 	    num_rdbs	* devlim->rdmardc_entry_sz +
1437 	    num_cqcs	* devlim->cqc_entry_sz +
1438 	    num_altc	* devlim->altc_entry_sz +
1439 #ifdef HERMON_FW_WORKAROUND
1440 	    0x80000000ull +
1441 #else
1442 	    num_auxc	* devlim->aux_entry_sz	+
1443 #endif
1444 	    num_eqcs	* devlim->eqc_entry_sz +
1445 	    num_mcgs	* HERMON_MCGMEM_SZ(state);
1446 	return (size);
1447 }
1448 
1449 
1450 /*
1451  * hermon_hw_init()
1452  *    Context: Only called from attach() path context
1453  */
1454 static int
1455 hermon_hw_init(hermon_state_t *state)
1456 {
1457 	hermon_drv_cleanup_level_t	cleanup;
1458 	sm_nodeinfo_t			nodeinfo;
1459 	uint64_t			clr_intr_offset;
1460 	int				status;
1461 	uint32_t			fw_size;	/* in page */
1462 	uint64_t			offset;
1463 
1464 	/* This is where driver initialization begins */
1465 	cleanup = HERMON_DRV_CLEANUP_LEVEL0;
1466 
1467 	/* Setup device access attributes */
1468 	state->hs_reg_accattr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
1469 	state->hs_reg_accattr.devacc_attr_endian_flags = DDI_STRUCTURE_BE_ACC;
1470 	state->hs_reg_accattr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1471 	state->hs_reg_accattr.devacc_attr_access = DDI_DEFAULT_ACC;
1472 
1473 	/* Setup fma-protected access attributes */
1474 	state->hs_fm_accattr.devacc_attr_version =
1475 	    hermon_devacc_attr_version(state);
1476 	state->hs_fm_accattr.devacc_attr_endian_flags = DDI_STRUCTURE_BE_ACC;
1477 	state->hs_fm_accattr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1478 	/* set acc err protection type */
1479 	state->hs_fm_accattr.devacc_attr_access =
1480 	    hermon_devacc_attr_access(state);
1481 
1482 	/* Setup for PCI config read/write of HCA device */
1483 	status = hermon_pci_config_setup(state, &state->hs_fm_pcihdl);
1484 	if (status != DDI_SUCCESS) {
1485 		hermon_hw_fini(state, cleanup);
1486 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1487 		    "hw_init_PCI_config_space_regmap_fail");
1488 		/* This case is not the degraded one */
1489 		return (DDI_FAILURE);
1490 	}
1491 
1492 	/* Map PCI config space and MSI-X tables/pba */
1493 	hermon_set_msix_info(state);
1494 
1495 	/* Map in Hermon registers (CMD, UAR, MSIX) and setup offsets */
1496 	status = hermon_regs_map_setup(state, HERMON_CMD_BAR,
1497 	    &state->hs_reg_cmd_baseaddr, 0, 0, &state->hs_fm_accattr,
1498 	    &state->hs_fm_cmdhdl);
1499 	if (status != DDI_SUCCESS) {
1500 		hermon_hw_fini(state, cleanup);
1501 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1502 		    "hw_init_CMD_BAR_regmap_fail");
1503 		/* This case is not the degraded one */
1504 		return (DDI_FAILURE);
1505 	}
1506 
1507 	cleanup = HERMON_DRV_CLEANUP_LEVEL1;
1508 	/*
1509 	 * We defer UAR-BAR mapping until later.  Need to know if
1510 	 * blueflame mapping is to be done, and don't know that until after
1511 	 * we get the dev_caps, so do it right after that
1512 	 */
1513 
1514 	/*
1515 	 * There is a third BAR defined for Hermon - it is for MSIX
1516 	 *
1517 	 * Will need to explore it's possible need/use w/ Mellanox
1518 	 * [es] Temporary mapping maybe
1519 	 */
1520 
1521 #ifdef HERMON_SUPPORTS_MSIX_BAR
1522 	status = ddi_regs_map_setup(state->hs_dip, HERMON_MSIX_BAR,
1523 	    &state->hs_reg_msi_baseaddr, 0, 0, &state->hs_reg_accattr,
1524 	    &state->hs_reg_msihdl);
1525 	if (status != DDI_SUCCESS) {
1526 		hermon_hw_fini(state, cleanup);
1527 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1528 		    "hw_init_MSIX_BAR_regmap_fail");
1529 		/* This case is not the degraded one */
1530 		return (DDI_FAILURE);
1531 	}
1532 #endif
1533 
1534 	cleanup = HERMON_DRV_CLEANUP_LEVEL2;
1535 
1536 	/*
1537 	 * Save interesting registers away. The offsets of the first two
1538 	 * here (HCR and sw_reset) are detailed in the PRM, the others are
1539 	 * derived from values in the QUERY_FW output, so we'll save them
1540 	 * off later.
1541 	 */
1542 	/* Host Command Register (HCR) */
1543 	state->hs_cmd_regs.hcr = (hermon_hw_hcr_t *)
1544 	    ((uintptr_t)state->hs_reg_cmd_baseaddr + HERMON_CMD_HCR_OFFSET);
1545 	state->hs_cmd_toggle = 0;	/* initialize it for use */
1546 
1547 	/* Software Reset register (sw_reset) and semaphore */
1548 	state->hs_cmd_regs.sw_reset = (uint32_t *)
1549 	    ((uintptr_t)state->hs_reg_cmd_baseaddr +
1550 	    HERMON_CMD_SW_RESET_OFFSET);
1551 	state->hs_cmd_regs.sw_semaphore = (uint32_t *)
1552 	    ((uintptr_t)state->hs_reg_cmd_baseaddr +
1553 	    HERMON_CMD_SW_SEMAPHORE_OFFSET);
1554 
1555 	/* Retrieve PCI device, vendor and rev IDs */
1556 	state->hs_vendor_id	 = HERMON_GET_VENDOR_ID(state->hs_dip);
1557 	state->hs_device_id	 = HERMON_GET_DEVICE_ID(state->hs_dip);
1558 	state->hs_revision_id	 = HERMON_GET_REVISION_ID(state->hs_dip);
1559 
1560 	/* make sure init'd before we start filling things in */
1561 	bzero(&state->hs_hcaparams, sizeof (struct hermon_hw_initqueryhca_s));
1562 
1563 	/* Initialize the Phase1 configuration profile */
1564 	status = hermon_cfg_profile_init_phase1(state);
1565 	if (status != DDI_SUCCESS) {
1566 		hermon_hw_fini(state, cleanup);
1567 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1568 		    "hw_init_cfginit1_fail");
1569 		/* This case is not the degraded one */
1570 		return (DDI_FAILURE);
1571 	}
1572 	cleanup = HERMON_DRV_CLEANUP_LEVEL3;
1573 
1574 	/* Do a software reset of the adapter to ensure proper state */
1575 	status = hermon_sw_reset(state);
1576 	if (status != HERMON_CMD_SUCCESS) {
1577 		hermon_hw_fini(state, cleanup);
1578 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1579 		    "hw_init_sw_reset_fail");
1580 		/* This case is not the degraded one */
1581 		return (DDI_FAILURE);
1582 	}
1583 
1584 	/* Initialize mailboxes */
1585 	status = hermon_rsrc_init_phase1(state);
1586 	if (status != DDI_SUCCESS) {
1587 		hermon_hw_fini(state, cleanup);
1588 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1589 		    "hw_init_rsrcinit1_fail");
1590 		/* This case is not the degraded one */
1591 		return (DDI_FAILURE);
1592 	}
1593 	cleanup = HERMON_DRV_CLEANUP_LEVEL4;
1594 
1595 	/* Post QUERY_FW */
1596 	status = hermon_cmn_query_cmd_post(state, QUERY_FW, 0, 0, &state->hs_fw,
1597 	    sizeof (hermon_hw_queryfw_t), HERMON_CMD_NOSLEEP_SPIN);
1598 	if (status != HERMON_CMD_SUCCESS) {
1599 		cmn_err(CE_NOTE, "QUERY_FW command failed: %08x\n", status);
1600 		hermon_hw_fini(state, cleanup);
1601 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1602 		    "hw_init_query_fw_cmd_fail");
1603 		/* This case is not the degraded one */
1604 		return (DDI_FAILURE);
1605 	}
1606 
1607 	/* Validate what/that HERMON FW version is appropriate */
1608 
1609 	status = hermon_fw_version_check(state);
1610 	if (status != DDI_SUCCESS) {
1611 		HERMON_FMANOTE(state, HERMON_FMA_FWVER);
1612 		if (state->hs_operational_mode == HERMON_HCA_MODE) {
1613 			cmn_err(CE_CONT, "Unsupported Hermon FW version: "
1614 			    "expected: %04d.%04d.%04d, "
1615 			    "actual: %04d.%04d.%04d\n",
1616 			    HERMON_FW_VER_MAJOR,
1617 			    HERMON_FW_VER_MINOR,
1618 			    HERMON_FW_VER_SUBMINOR,
1619 			    state->hs_fw.fw_rev_major,
1620 			    state->hs_fw.fw_rev_minor,
1621 			    state->hs_fw.fw_rev_subminor);
1622 		} else {
1623 			cmn_err(CE_CONT, "Unsupported FW version: "
1624 			    "%04d.%04d.%04d\n",
1625 			    state->hs_fw.fw_rev_major,
1626 			    state->hs_fw.fw_rev_minor,
1627 			    state->hs_fw.fw_rev_subminor);
1628 		}
1629 		state->hs_operational_mode = HERMON_MAINTENANCE_MODE;
1630 		state->hs_fm_degraded_reason = HCA_FW_MISMATCH;
1631 		hermon_hw_fini(state, cleanup);
1632 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1633 		    "hw_init_checkfwver_fail");
1634 		/* This case is the degraded one */
1635 		return (HERMON_CMD_BAD_NVMEM);
1636 	}
1637 
1638 	/*
1639 	 * Save off the rest of the interesting registers that we'll be using.
1640 	 * Setup the offsets for the other registers.
1641 	 */
1642 
1643 	/*
1644 	 * Hermon does the intr_offset from the BAR - technically should get the
1645 	 * BAR info from the response, but PRM says it's from BAR0-1, which is
1646 	 * for us the CMD BAR
1647 	 */
1648 
1649 	clr_intr_offset	 = state->hs_fw.clr_intr_offs & HERMON_CMD_OFFSET_MASK;
1650 
1651 	/* Save Clear Interrupt address */
1652 	state->hs_cmd_regs.clr_intr = (uint64_t *)
1653 	    (uintptr_t)(state->hs_reg_cmd_baseaddr + clr_intr_offset);
1654 
1655 	/*
1656 	 * Set the error buffer also into the structure - used in hermon_event.c
1657 	 * to check for internal error on the HCA, not reported in eqe or
1658 	 * (necessarily) by interrupt
1659 	 */
1660 	state->hs_cmd_regs.fw_err_buf = (uint32_t *)(uintptr_t)
1661 	    (state->hs_reg_cmd_baseaddr + state->hs_fw.error_buf_addr);
1662 
1663 	/*
1664 	 * Invoke a polling thread to check the error buffer periodically.
1665 	 */
1666 	if (!hermon_no_inter_err_chk) {
1667 		state->hs_fm_poll_thread = ddi_periodic_add(
1668 		    hermon_inter_err_chk, (void *)state, FM_POLL_INTERVAL,
1669 		    DDI_IPL_0);
1670 	}
1671 
1672 	cleanup = HERMON_DRV_CLEANUP_LEVEL5;
1673 
1674 	/*
1675 	 * Allocate, map, and run the HCA Firmware.
1676 	 */
1677 
1678 	/* Allocate memory for the firmware to load into and map it */
1679 
1680 	/* get next higher power of 2 */
1681 	fw_size = 1 << highbit(state->hs_fw.fw_pages);
1682 	state->hs_fw_dma.length = fw_size << HERMON_PAGESHIFT;
1683 	status = hermon_dma_alloc(state, &state->hs_fw_dma, MAP_FA);
1684 	if (status != DDI_SUCCESS) {
1685 		cmn_err(CE_NOTE, "FW alloc failed\n");
1686 		hermon_hw_fini(state, cleanup);
1687 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1688 		    "hw_init_dma_alloc_fw_fail");
1689 		/* This case is not the degraded one */
1690 		return (DDI_FAILURE);
1691 	}
1692 
1693 	cleanup = HERMON_DRV_CLEANUP_LEVEL6;
1694 
1695 	/* Invoke the RUN_FW cmd to run the firmware */
1696 	status = hermon_run_fw_cmd_post(state);
1697 	if (status != DDI_SUCCESS) {
1698 		cmn_err(CE_NOTE, "RUN_FW command failed: 0x%08x\n", status);
1699 		if (status == HERMON_CMD_BAD_NVMEM) {
1700 			state->hs_operational_mode = HERMON_MAINTENANCE_MODE;
1701 			state->hs_fm_degraded_reason = HCA_FW_CORRUPT;
1702 		}
1703 		hermon_hw_fini(state, cleanup);
1704 		HERMON_ATTACH_MSG(state->hs_attach_buf, "hw_init_run_fw_fail");
1705 		/*
1706 		 * If the status is HERMON_CMD_BAD_NVMEM, it's likely the
1707 		 * firmware is corrupted, so the mode falls into the
1708 		 * maintenance mode.
1709 		 */
1710 		return (status == HERMON_CMD_BAD_NVMEM ? HERMON_CMD_BAD_NVMEM :
1711 		    DDI_FAILURE);
1712 	}
1713 
1714 
1715 	/*
1716 	 * QUERY DEVICE LIMITS/CAPABILITIES
1717 	 * NOTE - in Hermon, the command is changed to QUERY_DEV_CAP,
1718 	 * but for familiarity we have kept the structure name the
1719 	 * same as Tavor/Arbel
1720 	 */
1721 
1722 	status = hermon_cmn_query_cmd_post(state, QUERY_DEV_CAP, 0, 0,
1723 	    &state->hs_devlim, sizeof (hermon_hw_querydevlim_t),
1724 	    HERMON_CMD_NOSLEEP_SPIN);
1725 	if (status != HERMON_CMD_SUCCESS) {
1726 		cmn_err(CE_NOTE, "QUERY_DEV_CAP command failed: 0x%08x\n",
1727 		    status);
1728 		hermon_hw_fini(state, cleanup);
1729 		HERMON_ATTACH_MSG(state->hs_attach_buf, "hw_init_devcap_fail");
1730 		/* This case is not the degraded one */
1731 		return (DDI_FAILURE);
1732 	}
1733 
1734 	state->hs_devlim.num_rsvd_eq = max(state->hs_devlim.num_rsvd_eq,
1735 	    (4 * state->hs_devlim.num_rsvd_uar));	/* lesser of resvd's */
1736 
1737 	/* now we have enough info to map in the UAR BAR */
1738 	/*
1739 	 * First, we figure out how to map the BAR for UAR - use only half if
1740 	 * BlueFlame is enabled - in that case the mapped length is 1/2 the
1741 	 * log_max_uar_sz (max__uar - 1) * 1MB ( +20).
1742 	 */
1743 
1744 	if (state->hs_devlim.blu_flm) {		/* Blue Flame Enabled */
1745 		offset = (uint64_t)1 << (state->hs_devlim.log_max_uar_sz + 20);
1746 	} else {
1747 		offset = 0;	/* a zero length means map the whole thing */
1748 	}
1749 	status = hermon_regs_map_setup(state, HERMON_UAR_BAR,
1750 	    &state->hs_reg_uar_baseaddr, 0, offset, &state->hs_fm_accattr,
1751 	    &state->hs_fm_uarhdl);
1752 	if (status != DDI_SUCCESS) {
1753 		HERMON_ATTACH_MSG(state->hs_attach_buf, "UAR BAR mapping");
1754 		/* This case is not the degraded one */
1755 		return (DDI_FAILURE);
1756 	}
1757 
1758 	/* and if BlueFlame is enabled, map the other half there */
1759 	if (state->hs_devlim.blu_flm) {		/* Blue Flame Enabled */
1760 		offset = (uint64_t)1 << (state->hs_devlim.log_max_uar_sz + 20);
1761 		status = ddi_regs_map_setup(state->hs_dip, HERMON_UAR_BAR,
1762 		    &state->hs_reg_bf_baseaddr, offset, offset,
1763 		    &state->hs_reg_accattr, &state->hs_reg_bfhdl);
1764 		if (status != DDI_SUCCESS) {
1765 			HERMON_ATTACH_MSG(state->hs_attach_buf,
1766 			    "BlueFlame BAR mapping");
1767 			/* This case is not the degraded one */
1768 			return (DDI_FAILURE);
1769 		}
1770 		/* This will be used in hw_fini if we fail to init. */
1771 		state->hs_bf_offset = offset;
1772 	}
1773 	cleanup = HERMON_DRV_CLEANUP_LEVEL7;
1774 
1775 	/* Hermon has a couple of things needed for phase 2 in query port */
1776 
1777 	status = hermon_cmn_query_cmd_post(state, QUERY_PORT, 0, 0x01,
1778 	    &state->hs_queryport, sizeof (hermon_hw_query_port_t),
1779 	    HERMON_CMD_NOSLEEP_SPIN);
1780 	if (status != HERMON_CMD_SUCCESS) {
1781 		cmn_err(CE_NOTE, "QUERY_PORT command failed: 0x%08x\n",
1782 		    status);
1783 		hermon_hw_fini(state, cleanup);
1784 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1785 		    "hw_init_queryport_fail");
1786 		/* This case is not the degraded one */
1787 		return (DDI_FAILURE);
1788 	}
1789 
1790 	/* Initialize the Phase2 Hermon configuration profile */
1791 	status = hermon_cfg_profile_init_phase2(state);
1792 	if (status != DDI_SUCCESS) {
1793 		cmn_err(CE_NOTE, "CFG phase 2 failed: 0x%08x\n", status);
1794 		hermon_hw_fini(state, cleanup);
1795 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1796 		    "hw_init_cfginit2_fail");
1797 		/* This case is not the degraded one */
1798 		return (DDI_FAILURE);
1799 	}
1800 
1801 	/* Determine and set the ICM size */
1802 	state->hs_icm_sz = hermon_size_icm(state);
1803 	status		 = hermon_set_icm_size_cmd_post(state);
1804 	if (status != DDI_SUCCESS) {
1805 		cmn_err(CE_NOTE, "Hermon: SET_ICM_SIZE cmd failed: 0x%08x\n",
1806 		    status);
1807 		hermon_hw_fini(state, cleanup);
1808 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1809 		    "hw_init_seticmsz_fail");
1810 		/* This case is not the degraded one */
1811 		return (DDI_FAILURE);
1812 	}
1813 	/* alloc icm aux physical memory and map it */
1814 
1815 	state->hs_icma_dma.length = 1 << highbit(state->hs_icma_sz);
1816 
1817 	status = hermon_dma_alloc(state, &state->hs_icma_dma, MAP_ICM_AUX);
1818 	if (status != DDI_SUCCESS) {
1819 		cmn_err(CE_NOTE, "failed to alloc (0x%llx) bytes for ICMA\n",
1820 		    (longlong_t)state->hs_icma_dma.length);
1821 		hermon_hw_fini(state, cleanup);
1822 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1823 		    "hw_init_dma_alloc_icm_aux_fail");
1824 		/* This case is not the degraded one */
1825 		return (DDI_FAILURE);
1826 	}
1827 	cleanup = HERMON_DRV_CLEANUP_LEVEL8;
1828 
1829 	cleanup = HERMON_DRV_CLEANUP_LEVEL9;
1830 
1831 	/* Allocate an array of structures to house the ICM tables */
1832 	state->hs_icm = kmem_zalloc(HERMON_NUM_ICM_RESOURCES *
1833 	    sizeof (hermon_icm_table_t), KM_SLEEP);
1834 
1835 	/* Set up the ICM address space and the INIT_HCA command input */
1836 	status = hermon_icm_config_setup(state, &state->hs_hcaparams);
1837 	if (status != HERMON_CMD_SUCCESS) {
1838 		cmn_err(CE_NOTE, "ICM configuration failed\n");
1839 		hermon_hw_fini(state, cleanup);
1840 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1841 		    "hw_init_icm_config_setup_fail");
1842 		/* This case is not the degraded one */
1843 		return (DDI_FAILURE);
1844 	}
1845 	cleanup = HERMON_DRV_CLEANUP_LEVEL10;
1846 
1847 	/* Initialize the adapter with the INIT_HCA cmd */
1848 	status = hermon_init_hca_cmd_post(state, &state->hs_hcaparams,
1849 	    HERMON_CMD_NOSLEEP_SPIN);
1850 	if (status != HERMON_CMD_SUCCESS) {
1851 		cmn_err(CE_NOTE, "INIT_HCA command failed: %08x\n", status);
1852 		hermon_hw_fini(state, cleanup);
1853 		HERMON_ATTACH_MSG(state->hs_attach_buf, "hw_init_hca_fail");
1854 		/* This case is not the degraded one */
1855 		return (DDI_FAILURE);
1856 	}
1857 	cleanup = HERMON_DRV_CLEANUP_LEVEL11;
1858 
1859 	/* Enter the second phase of init for Hermon configuration/resources */
1860 	status = hermon_rsrc_init_phase2(state);
1861 	if (status != DDI_SUCCESS) {
1862 		hermon_hw_fini(state, cleanup);
1863 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1864 		    "hw_init_rsrcinit2_fail");
1865 		/* This case is not the degraded one */
1866 		return (DDI_FAILURE);
1867 	}
1868 	cleanup = HERMON_DRV_CLEANUP_LEVEL12;
1869 
1870 	/* Query the adapter via QUERY_ADAPTER */
1871 	status = hermon_cmn_query_cmd_post(state, QUERY_ADAPTER, 0, 0,
1872 	    &state->hs_adapter, sizeof (hermon_hw_queryadapter_t),
1873 	    HERMON_CMD_NOSLEEP_SPIN);
1874 	if (status != HERMON_CMD_SUCCESS) {
1875 		cmn_err(CE_NOTE, "Hermon: QUERY_ADAPTER command failed: %08x\n",
1876 		    status);
1877 		hermon_hw_fini(state, cleanup);
1878 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1879 		    "hw_init_query_adapter_fail");
1880 		/* This case is not the degraded one */
1881 		return (DDI_FAILURE);
1882 	}
1883 
1884 	/* Allocate protection domain (PD) for Hermon internal use */
1885 	status = hermon_pd_alloc(state, &state->hs_pdhdl_internal,
1886 	    HERMON_SLEEP);
1887 	if (status != DDI_SUCCESS) {
1888 		cmn_err(CE_NOTE, "failed to alloc internal PD\n");
1889 		hermon_hw_fini(state, cleanup);
1890 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1891 		    "hw_init_internal_pd_alloc_fail");
1892 		/* This case is not the degraded one */
1893 		return (DDI_FAILURE);
1894 	}
1895 	cleanup = HERMON_DRV_CLEANUP_LEVEL13;
1896 
1897 	/* Setup UAR page for kernel use */
1898 	status = hermon_internal_uarpg_init(state);
1899 	if (status != DDI_SUCCESS) {
1900 		cmn_err(CE_NOTE, "failed to setup internal UAR\n");
1901 		hermon_hw_fini(state, cleanup);
1902 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1903 		    "hw_init_internal_uarpg_alloc_fail");
1904 		/* This case is not the degraded one */
1905 		return (DDI_FAILURE);
1906 	}
1907 	cleanup = HERMON_DRV_CLEANUP_LEVEL14;
1908 
1909 	/* Query and initialize the Hermon interrupt/MSI information */
1910 	status = hermon_intr_or_msi_init(state);
1911 	if (status != DDI_SUCCESS) {
1912 		cmn_err(CE_NOTE, "failed to setup INTR/MSI\n");
1913 		hermon_hw_fini(state, cleanup);
1914 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1915 		    "hw_init_intr_or_msi_init_fail");
1916 		/* This case is not the degraded one */
1917 		return (DDI_FAILURE);
1918 	}
1919 	cleanup = HERMON_DRV_CLEANUP_LEVEL15;
1920 
1921 	status = hermon_isr_init(state);	/* set up the isr */
1922 	if (status != DDI_SUCCESS) {
1923 		cmn_err(CE_NOTE, "failed to init isr\n");
1924 		hermon_hw_fini(state, cleanup);
1925 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1926 		    "hw_init_isrinit_fail");
1927 		/* This case is not the degraded one */
1928 		return (DDI_FAILURE);
1929 	}
1930 	cleanup = HERMON_DRV_CLEANUP_LEVEL16;
1931 
1932 	/* Setup the event queues */
1933 	status = hermon_eq_init_all(state);
1934 	if (status != DDI_SUCCESS) {
1935 		cmn_err(CE_NOTE, "failed to init EQs\n");
1936 		hermon_hw_fini(state, cleanup);
1937 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1938 		    "hw_init_eqinitall_fail");
1939 		/* This case is not the degraded one */
1940 		return (DDI_FAILURE);
1941 	}
1942 	cleanup = HERMON_DRV_CLEANUP_LEVEL17;
1943 
1944 
1945 
1946 	/* Reserve contexts for QP0 and QP1 */
1947 	status = hermon_special_qp_contexts_reserve(state);
1948 	if (status != DDI_SUCCESS) {
1949 		cmn_err(CE_NOTE, "failed to init special QPs\n");
1950 		hermon_hw_fini(state, cleanup);
1951 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1952 		    "hw_init_rsrv_sqp_fail");
1953 		/* This case is not the degraded one */
1954 		return (DDI_FAILURE);
1955 	}
1956 	cleanup = HERMON_DRV_CLEANUP_LEVEL18;
1957 
1958 	/* Initialize for multicast group handling */
1959 	status = hermon_mcg_init(state);
1960 	if (status != DDI_SUCCESS) {
1961 		cmn_err(CE_NOTE, "failed to init multicast\n");
1962 		hermon_hw_fini(state, cleanup);
1963 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1964 		    "hw_init_mcg_init_fail");
1965 		/* This case is not the degraded one */
1966 		return (DDI_FAILURE);
1967 	}
1968 	cleanup = HERMON_DRV_CLEANUP_LEVEL19;
1969 
1970 	/* Initialize the Hermon IB port(s) */
1971 	status = hermon_hca_port_init(state);
1972 	if (status != DDI_SUCCESS) {
1973 		cmn_err(CE_NOTE, "failed to init HCA Port\n");
1974 		hermon_hw_fini(state, cleanup);
1975 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1976 		    "hw_init_hca_port_init_fail");
1977 		/* This case is not the degraded one */
1978 		return (DDI_FAILURE);
1979 	}
1980 
1981 	cleanup = HERMON_DRV_CLEANUP_ALL;
1982 
1983 	/* Determine NodeGUID and SystemImageGUID */
1984 	status = hermon_getnodeinfo_cmd_post(state, HERMON_CMD_NOSLEEP_SPIN,
1985 	    &nodeinfo);
1986 	if (status != HERMON_CMD_SUCCESS) {
1987 		cmn_err(CE_NOTE, "GetNodeInfo command failed: %08x\n", status);
1988 		hermon_hw_fini(state, cleanup);
1989 		HERMON_ATTACH_MSG(state->hs_attach_buf,
1990 		    "hw_init_getnodeinfo_cmd_fail");
1991 		/* This case is not the degraded one */
1992 		return (DDI_FAILURE);
1993 	}
1994 
1995 	/*
1996 	 * If the NodeGUID value was set in OBP properties, then we use that
1997 	 * value.  But we still print a message if the value we queried from
1998 	 * firmware does not match this value.
1999 	 *
2000 	 * Otherwise if OBP value is not set then we use the value from
2001 	 * firmware unconditionally.
2002 	 */
2003 	if (state->hs_cfg_profile->cp_nodeguid) {
2004 		state->hs_nodeguid   = state->hs_cfg_profile->cp_nodeguid;
2005 	} else {
2006 		state->hs_nodeguid = nodeinfo.NodeGUID;
2007 	}
2008 
2009 	if (state->hs_nodeguid != nodeinfo.NodeGUID) {
2010 		cmn_err(CE_NOTE, "!NodeGUID value queried from firmware "
2011 		    "does not match value set by device property");
2012 	}
2013 
2014 	/*
2015 	 * If the SystemImageGUID value was set in OBP properties, then we use
2016 	 * that value.  But we still print a message if the value we queried
2017 	 * from firmware does not match this value.
2018 	 *
2019 	 * Otherwise if OBP value is not set then we use the value from
2020 	 * firmware unconditionally.
2021 	 */
2022 	if (state->hs_cfg_profile->cp_sysimgguid) {
2023 		state->hs_sysimgguid = state->hs_cfg_profile->cp_sysimgguid;
2024 	} else {
2025 		state->hs_sysimgguid = nodeinfo.SystemImageGUID;
2026 	}
2027 
2028 	if (state->hs_sysimgguid != nodeinfo.SystemImageGUID) {
2029 		cmn_err(CE_NOTE, "!SystemImageGUID value queried from firmware "
2030 		    "does not match value set by device property");
2031 	}
2032 
2033 	/* Get NodeDescription */
2034 	status = hermon_getnodedesc_cmd_post(state, HERMON_CMD_NOSLEEP_SPIN,
2035 	    (sm_nodedesc_t *)&state->hs_nodedesc);
2036 	if (status != HERMON_CMD_SUCCESS) {
2037 		cmn_err(CE_CONT, "GetNodeDesc command failed: %08x\n", status);
2038 		hermon_hw_fini(state, cleanup);
2039 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2040 		    "hw_init_getnodedesc_cmd_fail");
2041 		/* This case is not the degraded one */
2042 		return (DDI_FAILURE);
2043 	}
2044 
2045 	return (DDI_SUCCESS);
2046 }
2047 
2048 
2049 /*
2050  * hermon_hw_fini()
2051  *    Context: Only called from attach() and/or detach() path contexts
2052  */
2053 static void
2054 hermon_hw_fini(hermon_state_t *state, hermon_drv_cleanup_level_t cleanup)
2055 {
2056 	uint_t		num_ports;
2057 	int		i, status;
2058 
2059 
2060 	/*
2061 	 * JBDB - We might not want to run these returns in all cases of
2062 	 * Bad News. We should still attempt to free all of the DMA memory
2063 	 * resources...  This needs to be worked last, after all allocations
2064 	 * are implemented. For now, and possibly for later, this works.
2065 	 */
2066 
2067 	switch (cleanup) {
2068 	/*
2069 	 * If we add more driver initialization steps that should be cleaned
2070 	 * up here, we need to ensure that HERMON_DRV_CLEANUP_ALL is still the
2071 	 * first entry (i.e. corresponds to the last init step).
2072 	 */
2073 	case HERMON_DRV_CLEANUP_ALL:
2074 		/* Shutdown the Hermon IB port(s) */
2075 		num_ports = state->hs_cfg_profile->cp_num_ports;
2076 		(void) hermon_hca_ports_shutdown(state, num_ports);
2077 		/* FALLTHROUGH */
2078 
2079 	case HERMON_DRV_CLEANUP_LEVEL19:
2080 		/* Teardown resources used for multicast group handling */
2081 		hermon_mcg_fini(state);
2082 		/* FALLTHROUGH */
2083 
2084 	case HERMON_DRV_CLEANUP_LEVEL18:
2085 		/* Unreserve the special QP contexts */
2086 		hermon_special_qp_contexts_unreserve(state);
2087 		/* FALLTHROUGH */
2088 
2089 	case HERMON_DRV_CLEANUP_LEVEL17:
2090 		/*
2091 		 * Attempt to teardown all event queues (EQ).  If we fail
2092 		 * here then print a warning message and return.  Something
2093 		 * (either in HW or SW) has gone seriously wrong.
2094 		 */
2095 		status = hermon_eq_fini_all(state);
2096 		if (status != DDI_SUCCESS) {
2097 			HERMON_WARNING(state, "failed to teardown EQs");
2098 			return;
2099 		}
2100 		/* FALLTHROUGH */
2101 	case HERMON_DRV_CLEANUP_LEVEL16:
2102 		/* Teardown Hermon interrupts */
2103 		hermon_isr_fini(state);
2104 		/* FALLTHROUGH */
2105 
2106 	case HERMON_DRV_CLEANUP_LEVEL15:
2107 		status = hermon_intr_or_msi_fini(state);
2108 		if (status != DDI_SUCCESS) {
2109 			HERMON_WARNING(state, "failed to free intr/MSI");
2110 			return;
2111 		}
2112 		/* FALLTHROUGH */
2113 
2114 	case HERMON_DRV_CLEANUP_LEVEL14:
2115 		/* Free the resources for the Hermon internal UAR pages */
2116 		hermon_internal_uarpg_fini(state);
2117 		/* FALLTHROUGH */
2118 
2119 	case HERMON_DRV_CLEANUP_LEVEL13:
2120 		/*
2121 		 * Free the PD that was used internally by Hermon software.  If
2122 		 * we fail here then print a warning and return.  Something
2123 		 * (probably software-related, but perhaps HW) has gone wrong.
2124 		 */
2125 		status = hermon_pd_free(state, &state->hs_pdhdl_internal);
2126 		if (status != DDI_SUCCESS) {
2127 			HERMON_WARNING(state, "failed to free internal PD");
2128 			return;
2129 		}
2130 		/* FALLTHROUGH */
2131 
2132 	case HERMON_DRV_CLEANUP_LEVEL12:
2133 		/* Cleanup all the phase2 resources first */
2134 		hermon_rsrc_fini(state, HERMON_RSRC_CLEANUP_ALL);
2135 		/* FALLTHROUGH */
2136 
2137 	case HERMON_DRV_CLEANUP_LEVEL11:
2138 		/* LEVEL11 is after INIT_HCA */
2139 		/* FALLTHROUGH */
2140 
2141 
2142 	case HERMON_DRV_CLEANUP_LEVEL10:
2143 		/*
2144 		 * Unmap the ICM memory area with UNMAP_ICM command.
2145 		 */
2146 		status = hermon_unmap_icm_cmd_post(state, NULL);
2147 		if (status != DDI_SUCCESS) {
2148 			cmn_err(CE_WARN,
2149 			    "hermon_hw_fini: failed to unmap ICM\n");
2150 		}
2151 
2152 		/* Free the initial ICM DMA handles */
2153 		hermon_icm_dma_fini(state);
2154 
2155 		/* Free the ICM table structures */
2156 		hermon_icm_tables_fini(state);
2157 
2158 		/* Free the ICM table handles */
2159 		kmem_free(state->hs_icm, HERMON_NUM_ICM_RESOURCES *
2160 		    sizeof (hermon_icm_table_t));
2161 
2162 		/* FALLTHROUGH */
2163 
2164 	case HERMON_DRV_CLEANUP_LEVEL9:
2165 		/*
2166 		 * Unmap the ICM Aux memory area with UNMAP_ICM_AUX command.
2167 		 */
2168 		status = hermon_unmap_icm_aux_cmd_post(state);
2169 		if (status != HERMON_CMD_SUCCESS) {
2170 			cmn_err(CE_NOTE,
2171 			    "hermon_hw_fini: failed to unmap ICMA\n");
2172 		}
2173 		/* FALLTHROUGH */
2174 
2175 	case HERMON_DRV_CLEANUP_LEVEL8:
2176 		/*
2177 		 * Deallocate ICM Aux DMA memory.
2178 		 */
2179 		hermon_dma_free(&state->hs_icma_dma);
2180 		/* FALLTHROUGH */
2181 
2182 	case HERMON_DRV_CLEANUP_LEVEL7:
2183 		if (state->hs_fm_uarhdl) {
2184 			hermon_regs_map_free(state, &state->hs_fm_uarhdl);
2185 			state->hs_fm_uarhdl = NULL;
2186 		}
2187 
2188 		if (state->hs_reg_uarhdl) {
2189 			ddi_regs_map_free(&state->hs_reg_uarhdl);
2190 			state->hs_reg_uarhdl = NULL;
2191 		}
2192 
2193 		if (state->hs_bf_offset != 0 && state->hs_reg_bfhdl) {
2194 			ddi_regs_map_free(&state->hs_reg_bfhdl);
2195 			state->hs_reg_bfhdl = NULL;
2196 		}
2197 
2198 		for (i = 0; i < HERMON_MAX_PORTS; i++) {
2199 			if (state->hs_pkey[i]) {
2200 				kmem_free(state->hs_pkey[i], (1 <<
2201 				    state->hs_cfg_profile->cp_log_max_pkeytbl) *
2202 				    sizeof (ib_pkey_t));
2203 				state->hs_pkey[i] = NULL;
2204 			}
2205 			if (state->hs_guid[i]) {
2206 				kmem_free(state->hs_guid[i], (1 <<
2207 				    state->hs_cfg_profile->cp_log_max_gidtbl) *
2208 				    sizeof (ib_guid_t));
2209 				state->hs_guid[i] = NULL;
2210 			}
2211 		}
2212 		/* FALLTHROUGH */
2213 
2214 	case HERMON_DRV_CLEANUP_LEVEL6:
2215 		/*
2216 		 * Unmap the firmware memory area with UNMAP_FA command.
2217 		 */
2218 		status = hermon_unmap_fa_cmd_post(state);
2219 
2220 		if (status != HERMON_CMD_SUCCESS) {
2221 			cmn_err(CE_NOTE,
2222 			    "hermon_hw_fini: failed to unmap FW\n");
2223 		}
2224 
2225 		/*
2226 		 * Deallocate firmware DMA memory.
2227 		 */
2228 		hermon_dma_free(&state->hs_fw_dma);
2229 		/* FALLTHROUGH */
2230 
2231 	case HERMON_DRV_CLEANUP_LEVEL5:
2232 		/* stop the poll thread */
2233 		if (state->hs_fm_poll_thread) {
2234 			ddi_periodic_delete(state->hs_fm_poll_thread);
2235 			state->hs_fm_poll_thread = NULL;
2236 		}
2237 		/* FALLTHROUGH */
2238 
2239 	case HERMON_DRV_CLEANUP_LEVEL4:
2240 		/* Then cleanup the phase1 resources */
2241 		hermon_rsrc_fini(state, HERMON_RSRC_CLEANUP_PHASE1_COMPLETE);
2242 		/* FALLTHROUGH */
2243 
2244 	case HERMON_DRV_CLEANUP_LEVEL3:
2245 		/* Teardown any resources allocated for the config profile */
2246 		hermon_cfg_profile_fini(state);
2247 		/* FALLTHROUGH */
2248 
2249 	case HERMON_DRV_CLEANUP_LEVEL2:
2250 #ifdef HERMON_SUPPORTS_MSIX_BAR
2251 		/*
2252 		 * unmap 3rd BAR, MSIX BAR
2253 		 */
2254 		if (state->hs_reg_msihdl) {
2255 			ddi_regs_map_free(&state->hs_reg_msihdl);
2256 			state->hs_reg_msihdl = NULL;
2257 		}
2258 		/* FALLTHROUGH */
2259 #endif
2260 	case HERMON_DRV_CLEANUP_LEVEL1:
2261 	case HERMON_DRV_CLEANUP_LEVEL0:
2262 		/*
2263 		 * LEVEL1 and LEVEL0 resources are freed in
2264 		 * hermon_drv_fini2().
2265 		 */
2266 		break;
2267 
2268 	default:
2269 		HERMON_WARNING(state, "unexpected driver cleanup level");
2270 		return;
2271 	}
2272 }
2273 
2274 
2275 /*
2276  * hermon_soft_state_init()
2277  *    Context: Only called from attach() path context
2278  */
2279 static int
2280 hermon_soft_state_init(hermon_state_t *state)
2281 {
2282 	ibt_hca_attr_t		*hca_attr;
2283 	uint64_t		maxval, val;
2284 	ibt_hca_flags_t		caps = IBT_HCA_NO_FLAGS;
2285 	ibt_hca_flags2_t	caps2 = IBT_HCA2_NO_FLAGS;
2286 	int			status;
2287 	int			max_send_wqe_bytes;
2288 	int			max_recv_wqe_bytes;
2289 
2290 	/*
2291 	 * The ibc_hca_info_t struct is passed to the IBTF.  This is the
2292 	 * routine where we initialize it.  Many of the init values come from
2293 	 * either configuration variables or successful queries of the Hermon
2294 	 * hardware abilities
2295 	 */
2296 	state->hs_ibtfinfo.hca_ci_vers	= IBCI_V3;
2297 	state->hs_ibtfinfo.hca_dip	= state->hs_dip;
2298 	state->hs_ibtfinfo.hca_handle	= (ibc_hca_hdl_t)state;
2299 	state->hs_ibtfinfo.hca_ops	= &hermon_ibc_ops;
2300 
2301 	hca_attr = kmem_zalloc(sizeof (ibt_hca_attr_t), KM_SLEEP);
2302 	state->hs_ibtfinfo.hca_attr = hca_attr;
2303 
2304 	hca_attr->hca_fw_major_version = state->hs_fw.fw_rev_major;
2305 	hca_attr->hca_fw_minor_version = state->hs_fw.fw_rev_minor;
2306 	hca_attr->hca_fw_micro_version = state->hs_fw.fw_rev_subminor;
2307 
2308 	/* CQ interrupt moderation maximums - each limited to 16 bits */
2309 	hca_attr->hca_max_cq_mod_count = 0xFFFF;
2310 	hca_attr->hca_max_cq_mod_usec = 0xFFFF;
2311 
2312 	/* CQ relocation to other EQs - change when multiple MSI-Xs are used */
2313 	hca_attr->hca_max_cq_handlers = 1;
2314 
2315 	/*
2316 	 * Determine HCA capabilities:
2317 	 * No default support for IBT_HCA_RD, IBT_HCA_RAW_MULTICAST,
2318 	 *    IBT_HCA_ATOMICS_GLOBAL, IBT_HCA_RESIZE_CHAN, IBT_HCA_INIT_TYPE,
2319 	 *    or IBT_HCA_SHUTDOWN_PORT
2320 	 * But IBT_HCA_AH_PORT_CHECK, IBT_HCA_SQD_RTS_PORT, IBT_HCA_SI_GUID,
2321 	 *    IBT_HCA_RNR_NAK, IBT_HCA_CURRENT_QP_STATE, IBT_HCA_PORT_UP,
2322 	 *    IBT_HCA_SRQ, IBT_HCA_RESIZE_SRQ and IBT_HCA_FMR are always
2323 	 *    supported
2324 	 * All other features are conditionally supported, depending on the
2325 	 *    status return by the Hermon HCA in QUERY_DEV_LIM.
2326 	 */
2327 	if (state->hs_devlim.ud_multi) {
2328 		caps |= IBT_HCA_UD_MULTICAST;
2329 	}
2330 	if (state->hs_devlim.atomic) {
2331 		caps |= IBT_HCA_ATOMICS_HCA;
2332 	}
2333 	if (state->hs_devlim.apm) {
2334 		caps |= IBT_HCA_AUTO_PATH_MIG;
2335 	}
2336 	if (state->hs_devlim.pkey_v) {
2337 		caps |= IBT_HCA_PKEY_CNTR;
2338 	}
2339 	if (state->hs_devlim.qkey_v) {
2340 		caps |= IBT_HCA_QKEY_CNTR;
2341 	}
2342 	if (state->hs_devlim.ipoib_cksm) {
2343 		caps |= IBT_HCA_CKSUM_FULL;
2344 		caps2 |= IBT_HCA2_IP_CLASS;
2345 	}
2346 	if (state->hs_devlim.mod_wr_srq) {
2347 		caps |= IBT_HCA_RESIZE_SRQ;
2348 	}
2349 	if (state->hs_devlim.lif) {
2350 		caps |= IBT_HCA_LOCAL_INVAL_FENCE;
2351 	}
2352 	if (state->hs_devlim.reserved_lkey) {
2353 		caps2 |= IBT_HCA2_RES_LKEY;
2354 		hca_attr->hca_reserved_lkey = state->hs_devlim.rsv_lkey;
2355 	}
2356 	if (state->hs_devlim.local_inv && state->hs_devlim.remote_inv &&
2357 	    state->hs_devlim.fast_reg_wr) {	/* fw needs to be >= 2.6.636 */
2358 		if (state->hs_fw.fw_rev_major > 2)
2359 			caps2 |= IBT_HCA2_MEM_MGT_EXT;
2360 		else if (state->hs_fw.fw_rev_major == 2)
2361 			if (state->hs_fw.fw_rev_minor > 6)
2362 				caps2 |= IBT_HCA2_MEM_MGT_EXT;
2363 			else if (state->hs_fw.fw_rev_minor == 6)
2364 				if (state->hs_fw.fw_rev_subminor >= 636)
2365 					caps2 |= IBT_HCA2_MEM_MGT_EXT;
2366 	}
2367 	if (state->hs_devlim.mps) {
2368 		caps |= IBT_HCA_ZERO_BASED_VA;
2369 	}
2370 	if (state->hs_devlim.zb) {
2371 		caps |= IBT_HCA_MULT_PAGE_SZ_MR;
2372 	}
2373 	caps |= (IBT_HCA_AH_PORT_CHECK | IBT_HCA_SQD_SQD_PORT |
2374 	    IBT_HCA_SI_GUID | IBT_HCA_RNR_NAK | IBT_HCA_CURRENT_QP_STATE |
2375 	    IBT_HCA_PORT_UP | IBT_HCA_RC_SRQ | IBT_HCA_UD_SRQ | IBT_HCA_FMR);
2376 
2377 	if (state->hs_devlim.log_max_gso_sz) {
2378 		hca_attr->hca_max_lso_size =
2379 		    (1 << state->hs_devlim.log_max_gso_sz);
2380 		/* More work needed in hermon_post_send for larger values */
2381 		hca_attr->hca_max_lso_hdr_size = 0x2c;	/* IPv4 only */
2382 	}
2383 
2384 	caps |= IBT_HCA_WQE_SIZE_INFO;
2385 	max_send_wqe_bytes = state->hs_devlim.max_desc_sz_sq;
2386 	max_recv_wqe_bytes = state->hs_devlim.max_desc_sz_rq;
2387 	hca_attr->hca_ud_send_sgl_sz = (max_send_wqe_bytes / 16) - 4;
2388 	hca_attr->hca_conn_send_sgl_sz = (max_send_wqe_bytes / 16) - 1;
2389 	hca_attr->hca_conn_rdma_sgl_overhead = 1;
2390 	hca_attr->hca_recv_sgl_sz = max_recv_wqe_bytes / 16;
2391 
2392 	/* We choose not to support "inline" unless it improves performance */
2393 	hca_attr->hca_max_inline_size = 0;
2394 	hca_attr->hca_ud_send_inline_sz = 0;
2395 	hca_attr->hca_conn_send_inline_sz = 0;
2396 	hca_attr->hca_conn_rdmaw_inline_overhead = 4;
2397 
2398 	hca_attr->hca_flags = caps;
2399 	hca_attr->hca_flags2 = caps2;
2400 
2401 	/*
2402 	 * Set hca_attr's IDs
2403 	 */
2404 	hca_attr->hca_vendor_id	 = state->hs_vendor_id;
2405 	hca_attr->hca_device_id	 = state->hs_device_id;
2406 	hca_attr->hca_version_id = state->hs_revision_id;
2407 
2408 	/*
2409 	 * Determine number of available QPs and max QP size.  Number of
2410 	 * available QPs is determined by subtracting the number of
2411 	 * "reserved QPs" (i.e. reserved for firmware use) from the
2412 	 * total number configured.
2413 	 */
2414 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_qp);
2415 	hca_attr->hca_max_qp = val - ((uint64_t)1 <<
2416 	    state->hs_devlim.log_rsvd_qp);
2417 	maxval	= ((uint64_t)1 << state->hs_devlim.log_max_qp_sz);
2418 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_qp_sz);
2419 	if (val > maxval) {
2420 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2421 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2422 		    "soft_state_init_maxqpsz_toobig_fail");
2423 		return (DDI_FAILURE);
2424 	}
2425 	/* we need to reduce this by the max space needed for headroom */
2426 	hca_attr->hca_max_qp_sz = (uint_t)val - (HERMON_QP_OH_SIZE >>
2427 	    HERMON_QP_WQE_LOG_MINIMUM) - 1;
2428 
2429 	/*
2430 	 * Determine max scatter-gather size in WQEs. The HCA has split
2431 	 * the max sgl into rec'v Q and send Q values. Use the least.
2432 	 *
2433 	 * This is mainly useful for legacy clients.  Smart clients
2434 	 * such as IPoIB will use the IBT_HCA_WQE_SIZE_INFO sgl info.
2435 	 */
2436 	if (state->hs_devlim.max_sg_rq <= state->hs_devlim.max_sg_sq) {
2437 		maxval = state->hs_devlim.max_sg_rq;
2438 	} else {
2439 		maxval = state->hs_devlim.max_sg_sq;
2440 	}
2441 	val	= state->hs_cfg_profile->cp_wqe_max_sgl;
2442 	if (val > maxval) {
2443 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2444 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2445 		    "soft_state_init_toomanysgl_fail");
2446 		return (DDI_FAILURE);
2447 	}
2448 	/* If the rounded value for max SGL is too large, cap it */
2449 	if (state->hs_cfg_profile->cp_wqe_real_max_sgl > maxval) {
2450 		state->hs_cfg_profile->cp_wqe_real_max_sgl = (uint32_t)maxval;
2451 		val = maxval;
2452 	} else {
2453 		val = state->hs_cfg_profile->cp_wqe_real_max_sgl;
2454 	}
2455 
2456 	hca_attr->hca_max_sgl	 = (uint_t)val;
2457 	hca_attr->hca_max_rd_sgl = 0;	/* zero because RD is unsupported */
2458 
2459 	/*
2460 	 * Determine number of available CQs and max CQ size. Number of
2461 	 * available CQs is determined by subtracting the number of
2462 	 * "reserved CQs" (i.e. reserved for firmware use) from the
2463 	 * total number configured.
2464 	 */
2465 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_cq);
2466 	hca_attr->hca_max_cq = val - ((uint64_t)1 <<
2467 	    state->hs_devlim.log_rsvd_cq);
2468 	maxval	= ((uint64_t)1 << state->hs_devlim.log_max_cq_sz);
2469 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_cq_sz) - 1;
2470 	if (val > maxval) {
2471 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2472 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2473 		    "soft_state_init_maxcqsz_toobig_fail");
2474 		return (DDI_FAILURE);
2475 	}
2476 	hca_attr->hca_max_cq_sz = (uint_t)val;
2477 
2478 	/*
2479 	 * Determine number of available SRQs and max SRQ size. Number of
2480 	 * available SRQs is determined by subtracting the number of
2481 	 * "reserved SRQs" (i.e. reserved for firmware use) from the
2482 	 * total number configured.
2483 	 */
2484 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_srq);
2485 	hca_attr->hca_max_srqs = val - ((uint64_t)1 <<
2486 	    state->hs_devlim.log_rsvd_srq);
2487 	maxval  = ((uint64_t)1 << state->hs_devlim.log_max_srq_sz);
2488 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_srq_sz);
2489 
2490 	if (val > maxval) {
2491 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2492 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2493 		    "soft_state_init_maxsrqsz_toobig_fail");
2494 		return (DDI_FAILURE);
2495 	}
2496 	hca_attr->hca_max_srqs_sz = (uint_t)val;
2497 
2498 	val	= hca_attr->hca_recv_sgl_sz - 1; /* SRQ has a list link */
2499 	maxval	= state->hs_devlim.max_sg_rq - 1;
2500 	if (val > maxval) {
2501 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2502 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2503 		    "soft_state_init_toomanysrqsgl_fail");
2504 		return (DDI_FAILURE);
2505 	}
2506 	hca_attr->hca_max_srq_sgl = (uint_t)val;
2507 
2508 	/*
2509 	 * Determine supported HCA page sizes
2510 	 * XXX
2511 	 * For now we simply return the system pagesize as the only supported
2512 	 * pagesize
2513 	 */
2514 	hca_attr->hca_page_sz = ((PAGESIZE == (1 << 13)) ? IBT_PAGE_8K :
2515 	    IBT_PAGE_4K);
2516 
2517 	/*
2518 	 * Determine number of available MemReg, MemWin, and their max size.
2519 	 * Number of available MRs and MWs is determined by subtracting
2520 	 * the number of "reserved MPTs" (i.e. reserved for firmware use)
2521 	 * from the total number configured for each.
2522 	 */
2523 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_dmpt);
2524 	hca_attr->hca_max_memr	  = val - ((uint64_t)1 <<
2525 	    state->hs_devlim.log_rsvd_dmpt);
2526 	hca_attr->hca_max_mem_win = state->hs_devlim.mem_win ? (val -
2527 	    ((uint64_t)1 << state->hs_devlim.log_rsvd_dmpt)) : 0;
2528 	maxval	= state->hs_devlim.log_max_mrw_sz;
2529 	val	= state->hs_cfg_profile->cp_log_max_mrw_sz;
2530 	if (val > maxval) {
2531 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2532 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2533 		    "soft_state_init_maxmrwsz_toobig_fail");
2534 		return (DDI_FAILURE);
2535 	}
2536 	hca_attr->hca_max_memr_len = ((uint64_t)1 << val);
2537 
2538 	/* Determine RDMA/Atomic properties */
2539 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_rdb);
2540 	hca_attr->hca_max_rsc = (uint_t)val;
2541 	val = state->hs_cfg_profile->cp_hca_max_rdma_in_qp;
2542 	hca_attr->hca_max_rdma_in_qp  = (uint8_t)val;
2543 	val = state->hs_cfg_profile->cp_hca_max_rdma_out_qp;
2544 	hca_attr->hca_max_rdma_out_qp = (uint8_t)val;
2545 	hca_attr->hca_max_rdma_in_ee  = 0;
2546 	hca_attr->hca_max_rdma_out_ee = 0;
2547 
2548 	/*
2549 	 * Determine maximum number of raw IPv6 and Ether QPs.  Set to 0
2550 	 * because neither type of raw QP is supported
2551 	 */
2552 	hca_attr->hca_max_ipv6_qp  = 0;
2553 	hca_attr->hca_max_ether_qp = 0;
2554 
2555 	/* Determine max number of MCGs and max QP-per-MCG */
2556 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_qp);
2557 	hca_attr->hca_max_mcg_qps   = (uint_t)val;
2558 	val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_mcg);
2559 	hca_attr->hca_max_mcg	    = (uint_t)val;
2560 	val = state->hs_cfg_profile->cp_num_qp_per_mcg;
2561 	hca_attr->hca_max_qp_per_mcg = (uint_t)val;
2562 
2563 	/* Determine max number partitions (i.e. PKeys) */
2564 	maxval	= ((uint64_t)state->hs_cfg_profile->cp_num_ports <<
2565 	    state->hs_queryport.log_max_pkey);
2566 	val	= ((uint64_t)state->hs_cfg_profile->cp_num_ports <<
2567 	    state->hs_cfg_profile->cp_log_max_pkeytbl);
2568 
2569 	if (val > maxval) {
2570 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2571 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2572 		    "soft_state_init_toomanypkey_fail");
2573 		return (DDI_FAILURE);
2574 	}
2575 	hca_attr->hca_max_partitions = (uint16_t)val;
2576 
2577 	/* Determine number of ports */
2578 	maxval = state->hs_devlim.num_ports;
2579 	val = state->hs_cfg_profile->cp_num_ports;
2580 	if ((val > maxval) || (val == 0)) {
2581 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2582 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2583 		    "soft_state_init_toomanyports_fail");
2584 		return (DDI_FAILURE);
2585 	}
2586 	hca_attr->hca_nports = (uint8_t)val;
2587 
2588 	/* Copy NodeGUID and SystemImageGUID from softstate */
2589 	hca_attr->hca_node_guid = state->hs_nodeguid;
2590 	hca_attr->hca_si_guid	= state->hs_sysimgguid;
2591 
2592 	/*
2593 	 * Determine local ACK delay.  Use the value suggested by the Hermon
2594 	 * hardware (from the QUERY_DEV_CAP command)
2595 	 */
2596 	hca_attr->hca_local_ack_delay = state->hs_devlim.ca_ack_delay;
2597 
2598 	/* Determine max SGID table and PKey table sizes */
2599 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_gidtbl);
2600 	hca_attr->hca_max_port_sgid_tbl_sz = (uint_t)val;
2601 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_pkeytbl);
2602 	hca_attr->hca_max_port_pkey_tbl_sz = (uint16_t)val;
2603 
2604 	/* Determine max number of PDs */
2605 	maxval	= ((uint64_t)1 << state->hs_devlim.log_max_pd);
2606 	val	= ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_pd);
2607 	if (val > maxval) {
2608 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2609 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2610 		    "soft_state_init_toomanypd_fail");
2611 		return (DDI_FAILURE);
2612 	}
2613 	hca_attr->hca_max_pd = (uint_t)val;
2614 
2615 	/* Determine max number of Address Handles (NOT IN ARBEL or HERMON) */
2616 	hca_attr->hca_max_ah = 0;
2617 
2618 	/* No RDDs or EECs (since Reliable Datagram is not supported) */
2619 	hca_attr->hca_max_rdd = 0;
2620 	hca_attr->hca_max_eec = 0;
2621 
2622 	/* Initialize lock for reserved UAR page access */
2623 	mutex_init(&state->hs_uar_lock, NULL, MUTEX_DRIVER,
2624 	    DDI_INTR_PRI(state->hs_intrmsi_pri));
2625 
2626 	/* Initialize the flash fields */
2627 	state->hs_fw_flashstarted = 0;
2628 	mutex_init(&state->hs_fw_flashlock, NULL, MUTEX_DRIVER,
2629 	    DDI_INTR_PRI(state->hs_intrmsi_pri));
2630 
2631 	/* Initialize the lock for the info ioctl */
2632 	mutex_init(&state->hs_info_lock, NULL, MUTEX_DRIVER,
2633 	    DDI_INTR_PRI(state->hs_intrmsi_pri));
2634 
2635 	/* Initialize the AVL tree for QP number support */
2636 	hermon_qpn_avl_init(state);
2637 
2638 	/* Initialize the kstat info structure */
2639 	status = hermon_kstat_init(state);
2640 	if (status != DDI_SUCCESS) {
2641 		hermon_qpn_avl_fini(state);
2642 		mutex_destroy(&state->hs_info_lock);
2643 		mutex_destroy(&state->hs_fw_flashlock);
2644 		mutex_destroy(&state->hs_uar_lock);
2645 		kmem_free(hca_attr, sizeof (ibt_hca_attr_t));
2646 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2647 		    "soft_state_init_kstatinit_fail");
2648 		return (DDI_FAILURE);
2649 	}
2650 
2651 	return (DDI_SUCCESS);
2652 }
2653 
2654 
2655 /*
2656  * hermon_soft_state_fini()
2657  *    Context: Called only from detach() path context
2658  */
2659 static void
2660 hermon_soft_state_fini(hermon_state_t *state)
2661 {
2662 
2663 	/* Teardown the kstat info */
2664 	hermon_kstat_fini(state);
2665 
2666 	/* Teardown the AVL tree for QP number support */
2667 	hermon_qpn_avl_fini(state);
2668 
2669 	/* Free up info ioctl mutex */
2670 	mutex_destroy(&state->hs_info_lock);
2671 
2672 	/* Free up flash mutex */
2673 	mutex_destroy(&state->hs_fw_flashlock);
2674 
2675 	/* Free up the UAR page access mutex */
2676 	mutex_destroy(&state->hs_uar_lock);
2677 
2678 	/* Free up the hca_attr struct */
2679 	kmem_free(state->hs_ibtfinfo.hca_attr, sizeof (ibt_hca_attr_t));
2680 
2681 }
2682 
2683 /*
2684  * hermon_icm_config_setup()
2685  *    Context: Only called from attach() path context
2686  */
2687 static int
2688 hermon_icm_config_setup(hermon_state_t *state,
2689     hermon_hw_initqueryhca_t *inithca)
2690 {
2691 	hermon_hw_querydevlim_t	*devlim;
2692 	hermon_cfg_profile_t	*cfg;
2693 	hermon_icm_table_t	*icm_p[HERMON_NUM_ICM_RESOURCES];
2694 	hermon_icm_table_t	*icm;
2695 	hermon_icm_table_t	*tmp;
2696 	uint64_t		icm_addr;
2697 	uint64_t		icm_size;
2698 	int			status, i, j;
2699 
2700 
2701 	/* Bring in local devlims, cfg_profile and hs_icm table list */
2702 	devlim = &state->hs_devlim;
2703 	cfg = state->hs_cfg_profile;
2704 	icm = state->hs_icm;
2705 
2706 	/*
2707 	 * Assign each ICM table's entry size from data in the devlims,
2708 	 * except for RDB and MCG sizes, which are not returned in devlims
2709 	 * but do have a fixed size, and the UAR context entry size, which
2710 	 * we determine. For this, we use the "cp_num_pgs_per_uce" value
2711 	 * from our hs_cfg_profile.
2712 	 */
2713 	icm[HERMON_CMPT].object_size	= devlim->cmpt_entry_sz;
2714 	icm[HERMON_CMPT_QPC].object_size	= devlim->cmpt_entry_sz;
2715 	icm[HERMON_CMPT_SRQC].object_size	= devlim->cmpt_entry_sz;
2716 	icm[HERMON_CMPT_CQC].object_size	= devlim->cmpt_entry_sz;
2717 	icm[HERMON_CMPT_EQC].object_size	= devlim->cmpt_entry_sz;
2718 	icm[HERMON_MTT].object_size	= devlim->mtt_entry_sz;
2719 	icm[HERMON_DMPT].object_size	= devlim->dmpt_entry_sz;
2720 	icm[HERMON_QPC].object_size	= devlim->qpc_entry_sz;
2721 	icm[HERMON_CQC].object_size	= devlim->cqc_entry_sz;
2722 	icm[HERMON_SRQC].object_size	= devlim->srq_entry_sz;
2723 	icm[HERMON_EQC].object_size	= devlim->eqc_entry_sz;
2724 	icm[HERMON_RDB].object_size	= devlim->rdmardc_entry_sz *
2725 	    cfg->cp_hca_max_rdma_in_qp;
2726 	icm[HERMON_MCG].object_size	= HERMON_MCGMEM_SZ(state);
2727 	icm[HERMON_ALTC].object_size	= devlim->altc_entry_sz;
2728 	icm[HERMON_AUXC].object_size	= devlim->aux_entry_sz;
2729 
2730 	/* Assign each ICM table's log2 number of entries */
2731 	icm[HERMON_CMPT].log_num_entries = cfg->cp_log_num_cmpt;
2732 	icm[HERMON_CMPT_QPC].log_num_entries = cfg->cp_log_num_qp;
2733 	icm[HERMON_CMPT_SRQC].log_num_entries = cfg->cp_log_num_srq;
2734 	icm[HERMON_CMPT_CQC].log_num_entries = cfg->cp_log_num_cq;
2735 	icm[HERMON_CMPT_EQC].log_num_entries = HERMON_NUM_EQ_SHIFT;
2736 	icm[HERMON_MTT].log_num_entries	= cfg->cp_log_num_mtt;
2737 	icm[HERMON_DMPT].log_num_entries = cfg->cp_log_num_dmpt;
2738 	icm[HERMON_QPC].log_num_entries	= cfg->cp_log_num_qp;
2739 	icm[HERMON_SRQC].log_num_entries = cfg->cp_log_num_srq;
2740 	icm[HERMON_CQC].log_num_entries	= cfg->cp_log_num_cq;
2741 	icm[HERMON_EQC].log_num_entries	= HERMON_NUM_EQ_SHIFT;
2742 	icm[HERMON_RDB].log_num_entries	= cfg->cp_log_num_qp;
2743 	icm[HERMON_MCG].log_num_entries	= cfg->cp_log_num_mcg;
2744 	icm[HERMON_ALTC].log_num_entries = cfg->cp_log_num_qp;
2745 	icm[HERMON_AUXC].log_num_entries = cfg->cp_log_num_qp;
2746 
2747 	/* Initialize the ICM tables */
2748 	hermon_icm_tables_init(state);
2749 
2750 	/*
2751 	 * ICM tables must be aligned on their size in the ICM address
2752 	 * space. So, here we order the tables from largest total table
2753 	 * size to the smallest. All tables are a power of 2 in size, so
2754 	 * this will ensure that all tables are aligned on their own size
2755 	 * without wasting space in the ICM.
2756 	 *
2757 	 * In order to easily set the ICM addresses without needing to
2758 	 * worry about the ordering of our table indices as relates to
2759 	 * the hermon_rsrc_type_t enum, we will use a list of pointers
2760 	 * representing the tables for the sort, then assign ICM addresses
2761 	 * below using it.
2762 	 */
2763 	for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) {
2764 		icm_p[i] = &icm[i];
2765 	}
2766 	for (i = HERMON_NUM_ICM_RESOURCES; i > 0; i--) {
2767 		switch (i) {
2768 		case HERMON_CMPT_QPC:
2769 		case HERMON_CMPT_SRQC:
2770 		case HERMON_CMPT_CQC:
2771 		case HERMON_CMPT_EQC:
2772 			continue;
2773 		}
2774 		for (j = 1; j < i; j++) {
2775 			if (icm_p[j]->table_size > icm_p[j - 1]->table_size) {
2776 				tmp		= icm_p[j];
2777 				icm_p[j]	= icm_p[j - 1];
2778 				icm_p[j - 1]	= tmp;
2779 			}
2780 		}
2781 	}
2782 
2783 	/* Initialize the ICM address and ICM size */
2784 	icm_addr = icm_size = 0;
2785 
2786 	/*
2787 	 * Set the ICM base address of each table, using our sorted
2788 	 * list of pointers from above.
2789 	 */
2790 	for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) {
2791 		j = icm_p[i]->icm_type;
2792 		switch (j) {
2793 		case HERMON_CMPT_QPC:
2794 		case HERMON_CMPT_SRQC:
2795 		case HERMON_CMPT_CQC:
2796 		case HERMON_CMPT_EQC:
2797 			continue;
2798 		}
2799 		if (icm[j].table_size) {
2800 			/*
2801 			 * Set the ICM base address in the table, save the
2802 			 * ICM offset in the rsrc pool and increment the
2803 			 * total ICM allocation.
2804 			 */
2805 			icm[j].icm_baseaddr = icm_addr;
2806 			if (hermon_verbose) {
2807 				IBTF_DPRINTF_L2("ICMADDR", "rsrc %x @ %p"
2808 				    " size %llx", j, icm[j].icm_baseaddr,
2809 				    icm[j].table_size);
2810 			}
2811 			icm_size += icm[j].table_size;
2812 		}
2813 
2814 		/* Verify that we don't exceed maximum ICM size */
2815 		if (icm_size > devlim->max_icm_size) {
2816 			/* free the ICM table memory resources */
2817 			hermon_icm_tables_fini(state);
2818 			cmn_err(CE_WARN, "ICM configuration exceeds maximum "
2819 			    "configuration: max (0x%lx) requested (0x%lx)\n",
2820 			    (ulong_t)devlim->max_icm_size, (ulong_t)icm_size);
2821 			HERMON_ATTACH_MSG(state->hs_attach_buf,
2822 			    "icm_config_toobig_fail");
2823 			return (DDI_FAILURE);
2824 		}
2825 
2826 		/* assign address to the 4 pieces of the CMPT */
2827 		if (j == HERMON_CMPT) {
2828 			uint64_t cmpt_size = icm[j].table_size >> 2;
2829 #define	init_cmpt_icm_baseaddr(rsrc, indx)				\
2830 	icm[rsrc].icm_baseaddr	= icm_addr + (indx * cmpt_size);
2831 			init_cmpt_icm_baseaddr(HERMON_CMPT_QPC, 0);
2832 			init_cmpt_icm_baseaddr(HERMON_CMPT_SRQC, 1);
2833 			init_cmpt_icm_baseaddr(HERMON_CMPT_CQC, 2);
2834 			init_cmpt_icm_baseaddr(HERMON_CMPT_EQC, 3);
2835 		}
2836 
2837 		/* Increment the ICM address for the next table */
2838 		icm_addr += icm[j].table_size;
2839 	}
2840 
2841 	/* Populate the structure for the INIT_HCA command */
2842 	hermon_inithca_set(state, inithca);
2843 
2844 	/*
2845 	 * Prior to invoking INIT_HCA, we must have ICM memory in place
2846 	 * for the reserved objects in each table. We will allocate and map
2847 	 * this initial ICM memory here. Note that given the assignment
2848 	 * of span_size above, tables that are smaller or equal in total
2849 	 * size to the default span_size will be mapped in full.
2850 	 */
2851 	status = hermon_icm_dma_init(state);
2852 	if (status != DDI_SUCCESS) {
2853 		/* free the ICM table memory resources */
2854 		hermon_icm_tables_fini(state);
2855 		HERMON_WARNING(state, "Failed to allocate initial ICM");
2856 		HERMON_ATTACH_MSG(state->hs_attach_buf,
2857 		    "icm_config_dma_init_fail");
2858 		return (DDI_FAILURE);
2859 	}
2860 
2861 	return (DDI_SUCCESS);
2862 }
2863 
2864 /*
2865  * hermon_inithca_set()
2866  *    Context: Only called from attach() path context
2867  */
2868 static void
2869 hermon_inithca_set(hermon_state_t *state, hermon_hw_initqueryhca_t *inithca)
2870 {
2871 	hermon_cfg_profile_t	*cfg;
2872 	hermon_icm_table_t	*icm;
2873 	int			i;
2874 
2875 
2876 	/* Populate the INIT_HCA structure */
2877 	icm = state->hs_icm;
2878 	cfg = state->hs_cfg_profile;
2879 
2880 	/* set version */
2881 	inithca->version = 0x02;	/* PRM 0.36 */
2882 	/* set cacheline - log2 in 16-byte chunks */
2883 	inithca->log2_cacheline = 0x2;	/* optimized for 64 byte cache */
2884 
2885 	/* we need to update the inithca info with thie UAR info too */
2886 	inithca->uar.log_max_uars = highbit(cfg->cp_log_num_uar);
2887 	inithca->uar.uar_pg_sz = PAGESHIFT - HERMON_PAGESHIFT;
2888 
2889 	/* Set endianess */
2890 #ifdef	_LITTLE_ENDIAN
2891 	inithca->big_endian	= 0;
2892 #else
2893 	inithca->big_endian	= 1;
2894 #endif
2895 
2896 	/* Port Checking is on by default */
2897 	inithca->udav_port_chk	= HERMON_UDAV_PORTCHK_ENABLED;
2898 
2899 	/* Enable IPoIB checksum */
2900 	if (state->hs_devlim.ipoib_cksm)
2901 		inithca->chsum_en = 1;
2902 
2903 	/* Set each ICM table's attributes */
2904 	for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) {
2905 		switch (icm[i].icm_type) {
2906 		case HERMON_CMPT:
2907 			inithca->tpt.cmpt_baseaddr = icm[i].icm_baseaddr;
2908 			break;
2909 
2910 		case HERMON_MTT:
2911 			inithca->tpt.mtt_baseaddr = icm[i].icm_baseaddr;
2912 			break;
2913 
2914 		case HERMON_DMPT:
2915 			inithca->tpt.dmpt_baseaddr = icm[i].icm_baseaddr;
2916 			inithca->tpt.log_dmpt_sz   = icm[i].log_num_entries;
2917 			inithca->tpt.pgfault_rnr_to = 0; /* just in case */
2918 			break;
2919 
2920 		case HERMON_QPC:
2921 			inithca->context.log_num_qp = icm[i].log_num_entries;
2922 			inithca->context.qpc_baseaddr_h =
2923 			    icm[i].icm_baseaddr >> 32;
2924 			inithca->context.qpc_baseaddr_l =
2925 			    (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5;
2926 			break;
2927 
2928 		case HERMON_CQC:
2929 			inithca->context.log_num_cq = icm[i].log_num_entries;
2930 			inithca->context.cqc_baseaddr_h =
2931 			    icm[i].icm_baseaddr >> 32;
2932 			inithca->context.cqc_baseaddr_l =
2933 			    (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5;
2934 			break;
2935 
2936 		case HERMON_SRQC:
2937 			inithca->context.log_num_srq = icm[i].log_num_entries;
2938 			inithca->context.srqc_baseaddr_h =
2939 			    icm[i].icm_baseaddr >> 32;
2940 			inithca->context.srqc_baseaddr_l =
2941 			    (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5;
2942 			break;
2943 
2944 		case HERMON_EQC:
2945 			inithca->context.log_num_eq = icm[i].log_num_entries;
2946 			inithca->context.eqc_baseaddr_h =
2947 			    icm[i].icm_baseaddr >> 32;
2948 			inithca->context.eqc_baseaddr_l =
2949 			    (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5;
2950 			break;
2951 
2952 		case HERMON_RDB:
2953 			inithca->context.rdmardc_baseaddr_h =
2954 			    icm[i].icm_baseaddr >> 32;
2955 			inithca->context.rdmardc_baseaddr_l =
2956 			    (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5;
2957 			inithca->context.log_num_rdmardc =
2958 			    icm[i].log_num_entries;
2959 			break;
2960 
2961 		case HERMON_MCG:
2962 			inithca->multi.mc_baseaddr    = icm[i].icm_baseaddr;
2963 			inithca->multi.log_mc_tbl_sz  = icm[i].log_num_entries;
2964 			inithca->multi.log_mc_tbl_ent =
2965 			    highbit(HERMON_MCGMEM_SZ(state)) - 1;
2966 			inithca->multi.log_mc_tbl_hash_sz =
2967 			    cfg->cp_log_num_mcg_hash;
2968 			inithca->multi.mc_hash_fn = HERMON_MCG_DEFAULT_HASH_FN;
2969 			break;
2970 
2971 		case HERMON_ALTC:
2972 			inithca->context.altc_baseaddr = icm[i].icm_baseaddr;
2973 			break;
2974 
2975 		case HERMON_AUXC:
2976 			inithca->context.auxc_baseaddr = icm[i].icm_baseaddr;
2977 			break;
2978 
2979 		default:
2980 			break;
2981 
2982 		}
2983 	}
2984 
2985 }
2986 
2987 /*
2988  * hermon_icm_tables_init()
2989  *    Context: Only called from attach() path context
2990  *
2991  * Dynamic ICM breaks the various ICM tables into "span_size" chunks
2992  * to enable allocation of backing memory on demand.  Arbel used a
2993  * fixed size ARBEL_ICM_SPAN_SIZE (initially was 512KB) as the
2994  * span_size for all ICM chunks.  Hermon has other considerations,
2995  * so the span_size used differs from Arbel.
2996  *
2997  * The basic considerations for why Hermon differs are:
2998  *
2999  *	1) ICM memory is in units of HERMON pages.
3000  *
3001  *	2) The AUXC table is approximately 1 byte per QP.
3002  *
3003  *	3) ICM memory for AUXC, ALTC, and RDB is allocated when
3004  *	the ICM memory for the corresponding QPC is allocated.
3005  *
3006  *	4) ICM memory for the CMPT corresponding to the various primary
3007  *	resources (QPC, SRQC, CQC, and EQC) is allocated when the ICM
3008  *	memory for the primary resource is allocated.
3009  *
3010  * One HERMON page (4KB) would typically map 4K QPs worth of AUXC.
3011  * So, the minimum chunk for the various QPC related ICM memory should
3012  * all be allocated to support the 4K QPs.  Currently, this means the
3013  * amount of memory for the various QP chunks is:
3014  *
3015  *	QPC	256*4K bytes
3016  *	RDB	128*4K bytes
3017  *	CMPT	 64*4K bytes
3018  *	ALTC	 64*4K bytes
3019  *	AUXC	  1*4K bytes
3020  *
3021  * The span_size chosen for the QP resource is 4KB of AUXC entries,
3022  * or 1 HERMON_PAGESIZE worth, which is the minimum ICM mapping size.
3023  *
3024  * Other ICM resources can have their span_size be more arbitrary.
3025  * This is 4K (HERMON_ICM_SPAN), except for MTTs because they are tiny.
3026  */
3027 
3028 /* macro to make the code below cleaner */
3029 #define	init_dependent(rsrc, dep)				\
3030 	icm[dep].span		= icm[rsrc].span;		\
3031 	icm[dep].num_spans	= icm[rsrc].num_spans;		\
3032 	icm[dep].split_shift	= icm[rsrc].split_shift;	\
3033 	icm[dep].span_mask	= icm[rsrc].span_mask;		\
3034 	icm[dep].span_shift	= icm[rsrc].span_shift;		\
3035 	icm[dep].rsrc_mask	= icm[rsrc].rsrc_mask;		\
3036 	if (hermon_verbose) {					\
3037 		IBTF_DPRINTF_L2("hermon", "tables_init: "	\
3038 		    "rsrc (0x%x) size (0x%lx) span (0x%x) "	\
3039 		    "num_spans (0x%x)", dep, icm[dep].table_size, \
3040 		    icm[dep].span, icm[dep].num_spans);		\
3041 		IBTF_DPRINTF_L2("hermon", "tables_init: "	\
3042 		    "span_shift (0x%x) split_shift (0x%x)",	\
3043 		    icm[dep].span_shift, icm[dep].split_shift);	\
3044 		IBTF_DPRINTF_L2("hermon", "tables_init: "	\
3045 		    "span_mask (0x%x)  rsrc_mask   (0x%x)",	\
3046 		    icm[dep].span_mask, icm[dep].rsrc_mask);	\
3047 	}
3048 
3049 static void
3050 hermon_icm_tables_init(hermon_state_t *state)
3051 {
3052 	hermon_icm_table_t	*icm;
3053 	int			i, k;
3054 	uint32_t		per_split;
3055 
3056 
3057 	icm = state->hs_icm;
3058 
3059 	for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) {
3060 		icm[i].icm_type		= i;
3061 		icm[i].num_entries	= 1 << icm[i].log_num_entries;
3062 		icm[i].log_object_size	= highbit(icm[i].object_size) - 1;
3063 		icm[i].table_size	= icm[i].num_entries <<
3064 		    icm[i].log_object_size;
3065 
3066 		/* deal with "dependent" resource types */
3067 		switch (i) {
3068 		case HERMON_AUXC:
3069 #ifdef HERMON_FW_WORKAROUND
3070 			icm[i].table_size = 0x80000000ull;
3071 			/* FALLTHROUGH */
3072 #endif
3073 		case HERMON_CMPT_QPC:
3074 		case HERMON_RDB:
3075 		case HERMON_ALTC:
3076 			init_dependent(HERMON_QPC, i);
3077 			continue;
3078 		case HERMON_CMPT_SRQC:
3079 			init_dependent(HERMON_SRQC, i);
3080 			continue;
3081 		case HERMON_CMPT_CQC:
3082 			init_dependent(HERMON_CQC, i);
3083 			continue;
3084 		case HERMON_CMPT_EQC:
3085 			init_dependent(HERMON_EQC, i);
3086 			continue;
3087 		}
3088 
3089 		icm[i].span = HERMON_ICM_SPAN;	/* default #rsrc's in 1 span */
3090 		if (i == HERMON_MTT) /* Alloc enough MTTs to map 256MB */
3091 			icm[i].span = HERMON_ICM_SPAN * 16;
3092 		icm[i].num_spans = icm[i].num_entries / icm[i].span;
3093 		if (icm[i].num_spans == 0) {
3094 			icm[i].span = icm[i].num_entries;
3095 			per_split = 1;
3096 			icm[i].num_spans = icm[i].num_entries / icm[i].span;
3097 		} else {
3098 			per_split = icm[i].num_spans / HERMON_ICM_SPLIT;
3099 			if (per_split == 0) {
3100 				per_split = 1;
3101 			}
3102 		}
3103 		if (hermon_verbose)
3104 			IBTF_DPRINTF_L2("ICM", "rsrc %x  span %x  num_spans %x",
3105 			    i, icm[i].span, icm[i].num_spans);
3106 
3107 		/*
3108 		 * Ensure a minimum table size of an ICM page, and a
3109 		 * maximum span size of the ICM table size.  This ensures
3110 		 * that we don't have less than an ICM page to map, which is
3111 		 * impossible, and that we will map an entire table at
3112 		 * once if it's total size is less than the span size.
3113 		 */
3114 		icm[i].table_size = max(icm[i].table_size, HERMON_PAGESIZE);
3115 
3116 		icm[i].span_shift = 0;
3117 		for (k = icm[i].span; k != 1; k >>= 1)
3118 			icm[i].span_shift++;
3119 		icm[i].split_shift = icm[i].span_shift;
3120 		for (k = per_split; k != 1; k >>= 1)
3121 			icm[i].split_shift++;
3122 		icm[i].span_mask = (1 << icm[i].split_shift) -
3123 		    (1 << icm[i].span_shift);
3124 		icm[i].rsrc_mask = (1 << icm[i].span_shift) - 1;
3125 
3126 
3127 		/* Initialize the table lock */
3128 		mutex_init(&icm[i].icm_table_lock, NULL, MUTEX_DRIVER,
3129 		    DDI_INTR_PRI(state->hs_intrmsi_pri));
3130 		cv_init(&icm[i].icm_table_cv, NULL, CV_DRIVER, NULL);
3131 
3132 		if (hermon_verbose) {
3133 			IBTF_DPRINTF_L2("hermon", "tables_init: "
3134 			    "rsrc (0x%x) size (0x%lx)", i, icm[i].table_size);
3135 			IBTF_DPRINTF_L2("hermon", "tables_init: "
3136 			    "span (0x%x) num_spans (0x%x)",
3137 			    icm[i].span, icm[i].num_spans);
3138 			IBTF_DPRINTF_L2("hermon", "tables_init: "
3139 			    "span_shift (0x%x) split_shift (0x%x)",
3140 			    icm[i].span_shift, icm[i].split_shift);
3141 			IBTF_DPRINTF_L2("hermon", "tables_init: "
3142 			    "span_mask (0x%x)  rsrc_mask   (0x%x)",
3143 			    icm[i].span_mask, icm[i].rsrc_mask);
3144 		}
3145 	}
3146 
3147 }
3148 
3149 /*
3150  * hermon_icm_tables_fini()
3151  *    Context: Only called from attach() path context
3152  *
3153  * Clean up all icm_tables.  Free the bitmap and dma_info arrays.
3154  */
3155 static void
3156 hermon_icm_tables_fini(hermon_state_t *state)
3157 {
3158 	hermon_icm_table_t	*icm;
3159 	int			nspans;
3160 	int			i, j;
3161 
3162 
3163 	icm = state->hs_icm;
3164 
3165 	for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) {
3166 
3167 		mutex_enter(&icm[i].icm_table_lock);
3168 		nspans = icm[i].num_spans;
3169 
3170 		for (j = 0; j < HERMON_ICM_SPLIT; j++) {
3171 			if (icm[i].icm_dma[j])
3172 				/* Free the ICM DMA slots */
3173 				kmem_free(icm[i].icm_dma[j],
3174 				    nspans * sizeof (hermon_dma_info_t));
3175 
3176 			if (icm[i].icm_bitmap[j])
3177 				/* Free the table bitmap */
3178 				kmem_free(icm[i].icm_bitmap[j],
3179 				    (nspans + 7) / 8);
3180 		}
3181 		/* Destroy the table lock */
3182 		cv_destroy(&icm[i].icm_table_cv);
3183 		mutex_exit(&icm[i].icm_table_lock);
3184 		mutex_destroy(&icm[i].icm_table_lock);
3185 	}
3186 
3187 }
3188 
3189 /*
3190  * hermon_icm_dma_init()
3191  *    Context: Only called from attach() path context
3192  */
3193 static int
3194 hermon_icm_dma_init(hermon_state_t *state)
3195 {
3196 	hermon_icm_table_t	*icm;
3197 	hermon_rsrc_type_t	type;
3198 	int			status;
3199 
3200 
3201 	/*
3202 	 * This routine will allocate initial ICM DMA resources for ICM
3203 	 * tables that have reserved ICM objects. This is the only routine
3204 	 * where we should have to allocate ICM outside of hermon_rsrc_alloc().
3205 	 * We need to allocate ICM here explicitly, rather than in
3206 	 * hermon_rsrc_alloc(), because we've not yet completed the resource
3207 	 * pool initialization. When the resource pools are initialized
3208 	 * (in hermon_rsrc_init_phase2(), see hermon_rsrc.c for more
3209 	 * information), resource preallocations will be invoked to match
3210 	 * the ICM allocations seen here. We will then be able to use the
3211 	 * normal allocation path.  Note we don't need to set a refcnt on
3212 	 * these initial allocations because that will be done in the calls
3213 	 * to hermon_rsrc_alloc() from hermon_hw_entries_init() for the
3214 	 * "prealloc" objects (see hermon_rsrc.c for more information).
3215 	 */
3216 	for (type = 0; type < HERMON_NUM_ICM_RESOURCES; type++) {
3217 
3218 		/* ICM for these is allocated within hermon_icm_alloc() */
3219 		switch (type) {
3220 		case HERMON_CMPT:
3221 		case HERMON_CMPT_QPC:
3222 		case HERMON_CMPT_SRQC:
3223 		case HERMON_CMPT_CQC:
3224 		case HERMON_CMPT_EQC:
3225 		case HERMON_AUXC:
3226 		case HERMON_ALTC:
3227 		case HERMON_RDB:
3228 			continue;
3229 		}
3230 
3231 		icm = &state->hs_icm[type];
3232 
3233 		mutex_enter(&icm->icm_table_lock);
3234 		status = hermon_icm_alloc(state, type, 0, 0);
3235 		mutex_exit(&icm->icm_table_lock);
3236 		if (status != DDI_SUCCESS) {
3237 			while (type--) {
3238 				icm = &state->hs_icm[type];
3239 				mutex_enter(&icm->icm_table_lock);
3240 				hermon_icm_free(state, type, 0, 0);
3241 				mutex_exit(&icm->icm_table_lock);
3242 			}
3243 			return (DDI_FAILURE);
3244 		}
3245 
3246 		if (hermon_verbose) {
3247 			IBTF_DPRINTF_L2("hermon", "hermon_icm_dma_init: "
3248 			    "table (0x%x) index (0x%x) allocated", type, 0);
3249 		}
3250 	}
3251 
3252 	return (DDI_SUCCESS);
3253 }
3254 
3255 /*
3256  * hermon_icm_dma_fini()
3257  *    Context: Only called from attach() path context
3258  *
3259  * ICM has been completely unmapped.  We just free the memory here.
3260  */
3261 static void
3262 hermon_icm_dma_fini(hermon_state_t *state)
3263 {
3264 	hermon_icm_table_t	*icm;
3265 	hermon_dma_info_t	*dma_info;
3266 	hermon_rsrc_type_t	type;
3267 	int			index1, index2;
3268 
3269 
3270 	for (type = 0; type < HERMON_NUM_ICM_RESOURCES; type++) {
3271 		icm = &state->hs_icm[type];
3272 		for (index1 = 0; index1 < HERMON_ICM_SPLIT; index1++) {
3273 			dma_info = icm->icm_dma[index1];
3274 			if (dma_info == NULL)
3275 				continue;
3276 			for (index2 = 0; index2 < icm->num_spans; index2++) {
3277 				if (dma_info[index2].dma_hdl)
3278 					hermon_dma_free(&dma_info[index2]);
3279 				dma_info[index2].dma_hdl = NULL;
3280 			}
3281 		}
3282 	}
3283 
3284 }
3285 
3286 /*
3287  * hermon_hca_port_init()
3288  *    Context: Only called from attach() path context
3289  */
3290 static int
3291 hermon_hca_port_init(hermon_state_t *state)
3292 {
3293 	hermon_hw_set_port_t	*portinits, *initport;
3294 	hermon_cfg_profile_t	*cfgprof;
3295 	uint_t			num_ports;
3296 	int			i = 0, status;
3297 	uint64_t		maxval, val;
3298 	uint64_t		sysimgguid, nodeguid, portguid;
3299 
3300 
3301 	cfgprof = state->hs_cfg_profile;
3302 
3303 	/* Get number of HCA ports */
3304 	num_ports = cfgprof->cp_num_ports;
3305 
3306 	/* Allocate space for Hermon set port  struct(s) */
3307 	portinits = (hermon_hw_set_port_t *)kmem_zalloc(num_ports *
3308 	    sizeof (hermon_hw_set_port_t), KM_SLEEP);
3309 
3310 
3311 
3312 	/* Post commands to initialize each Hermon HCA port */
3313 	/*
3314 	 * In Hermon, the process is different than in previous HCAs.
3315 	 * Here, you have to:
3316 	 *	QUERY_PORT - to get basic information from the HCA
3317 	 *	set the fields accordingly
3318 	 *	SET_PORT - to change/set everything as desired
3319 	 *	INIT_PORT - to bring the port up
3320 	 *
3321 	 * Needs to be done for each port in turn
3322 	 */
3323 
3324 	for (i = 0; i < num_ports; i++) {
3325 		bzero(&state->hs_queryport, sizeof (hermon_hw_query_port_t));
3326 		status = hermon_cmn_query_cmd_post(state, QUERY_PORT, 0,
3327 		    (i + 1), &state->hs_queryport,
3328 		    sizeof (hermon_hw_query_port_t), HERMON_CMD_NOSLEEP_SPIN);
3329 		if (status != HERMON_CMD_SUCCESS) {
3330 			cmn_err(CE_CONT, "Hermon: QUERY_PORT (port %02d) "
3331 			    "command failed: %08x\n", i + 1, status);
3332 			goto init_ports_fail;
3333 		}
3334 		initport = &portinits[i];
3335 		state->hs_initport = &portinits[i];
3336 
3337 		bzero(initport, sizeof (hermon_hw_query_port_t));
3338 
3339 		/*
3340 		 * Determine whether we need to override the firmware's
3341 		 * default SystemImageGUID setting.
3342 		 */
3343 		sysimgguid = cfgprof->cp_sysimgguid;
3344 		if (sysimgguid != 0) {
3345 			initport->sig		= 1;
3346 			initport->sys_img_guid	= sysimgguid;
3347 		}
3348 
3349 		/*
3350 		 * Determine whether we need to override the firmware's
3351 		 * default NodeGUID setting.
3352 		 */
3353 		nodeguid = cfgprof->cp_nodeguid;
3354 		if (nodeguid != 0) {
3355 			initport->ng		= 1;
3356 			initport->node_guid	= nodeguid;
3357 		}
3358 
3359 		/*
3360 		 * Determine whether we need to override the firmware's
3361 		 * default PortGUID setting.
3362 		 */
3363 		portguid = cfgprof->cp_portguid[i];
3364 		if (portguid != 0) {
3365 			initport->g0		= 1;
3366 			initport->guid0		= portguid;
3367 		}
3368 
3369 		/* Validate max MTU size */
3370 		maxval  = state->hs_queryport.ib_mtu;
3371 		val	= cfgprof->cp_max_mtu;
3372 		if (val > maxval) {
3373 			goto init_ports_fail;
3374 		}
3375 
3376 		/* Validate the max port width */
3377 		maxval  = state->hs_queryport.ib_port_wid;
3378 		val	= cfgprof->cp_max_port_width;
3379 		if (val > maxval) {
3380 			goto init_ports_fail;
3381 		}
3382 
3383 		/* Validate max VL cap size */
3384 		maxval  = state->hs_queryport.max_vl;
3385 		val	= cfgprof->cp_max_vlcap;
3386 		if (val > maxval) {
3387 			goto init_ports_fail;
3388 		}
3389 
3390 		/* Validate max GID table size */
3391 		maxval  = ((uint64_t)1 << state->hs_queryport.log_max_gid);
3392 		val	= ((uint64_t)1 << cfgprof->cp_log_max_gidtbl);
3393 		if (val > maxval) {
3394 			goto init_ports_fail;
3395 		}
3396 		initport->max_guid = (uint16_t)val;
3397 		initport->mg = 1;
3398 
3399 		/* Validate max PKey table size */
3400 		maxval	= ((uint64_t)1 << state->hs_queryport.log_max_pkey);
3401 		val	= ((uint64_t)1 << cfgprof->cp_log_max_pkeytbl);
3402 		if (val > maxval) {
3403 			goto init_ports_fail;
3404 		}
3405 		initport->max_pkey = (uint16_t)val;
3406 		initport->mp = 1;
3407 		/*
3408 		 * Post the SET_PORT cmd to Hermon firmware. This sets
3409 		 * the parameters of the port.
3410 		 */
3411 		status = hermon_set_port_cmd_post(state, initport, i + 1,
3412 		    HERMON_CMD_NOSLEEP_SPIN);
3413 		if (status != HERMON_CMD_SUCCESS) {
3414 			cmn_err(CE_CONT, "Hermon: SET_PORT (port %02d) command "
3415 			    "failed: %08x\n", i + 1, status);
3416 			goto init_ports_fail;
3417 		}
3418 		/* issue another SET_PORT cmd - performance fix/workaround */
3419 		/* XXX - need to discuss with Mellanox */
3420 		bzero(initport, sizeof (hermon_hw_query_port_t));
3421 		initport->cap_mask = 0x02500868;
3422 		status = hermon_set_port_cmd_post(state, initport, i + 1,
3423 		    HERMON_CMD_NOSLEEP_SPIN);
3424 		if (status != HERMON_CMD_SUCCESS) {
3425 			cmn_err(CE_CONT, "Hermon: SET_PORT (port %02d) command "
3426 			    "failed: %08x\n", i + 1, status);
3427 			goto init_ports_fail;
3428 		}
3429 	}
3430 
3431 	/*
3432 	 * Finally, do the INIT_PORT for each port in turn
3433 	 * When this command completes, the corresponding Hermon port
3434 	 * will be physically "Up" and initialized.
3435 	 */
3436 	for (i = 0; i < num_ports; i++) {
3437 		status = hermon_init_port_cmd_post(state, i + 1,
3438 		    HERMON_CMD_NOSLEEP_SPIN);
3439 		if (status != HERMON_CMD_SUCCESS) {
3440 			cmn_err(CE_CONT, "Hermon: INIT_PORT (port %02d) "
3441 			    "comman failed: %08x\n", i + 1, status);
3442 			goto init_ports_fail;
3443 		}
3444 	}
3445 
3446 	/* Free up the memory for Hermon port init struct(s), return success */
3447 	kmem_free(portinits, num_ports * sizeof (hermon_hw_set_port_t));
3448 	return (DDI_SUCCESS);
3449 
3450 init_ports_fail:
3451 	/*
3452 	 * Free up the memory for Hermon port init struct(s), shutdown any
3453 	 * successfully initialized ports, and return failure
3454 	 */
3455 	kmem_free(portinits, num_ports * sizeof (hermon_hw_set_port_t));
3456 	(void) hermon_hca_ports_shutdown(state, i);
3457 
3458 	return (DDI_FAILURE);
3459 }
3460 
3461 
3462 /*
3463  * hermon_hca_ports_shutdown()
3464  *    Context: Only called from attach() and/or detach() path contexts
3465  */
3466 static int
3467 hermon_hca_ports_shutdown(hermon_state_t *state, uint_t num_init)
3468 {
3469 	int	i, status;
3470 
3471 	/*
3472 	 * Post commands to shutdown all init'd Hermon HCA ports.  Note: if
3473 	 * any of these commands fail for any reason, it would be entirely
3474 	 * unexpected and probably indicative a serious problem (HW or SW).
3475 	 * Although we do return void from this function, this type of failure
3476 	 * should not go unreported.  That is why we have the warning message.
3477 	 */
3478 	for (i = 0; i < num_init; i++) {
3479 		status = hermon_close_port_cmd_post(state, i + 1,
3480 		    HERMON_CMD_NOSLEEP_SPIN);
3481 		if (status != HERMON_CMD_SUCCESS) {
3482 			HERMON_WARNING(state, "failed to shutdown HCA port");
3483 			return (status);
3484 		}
3485 	}
3486 	return (HERMON_CMD_SUCCESS);
3487 }
3488 
3489 
3490 /*
3491  * hermon_internal_uarpg_init
3492  *    Context: Only called from attach() path context
3493  */
3494 static int
3495 hermon_internal_uarpg_init(hermon_state_t *state)
3496 {
3497 	int	status;
3498 	hermon_dbr_info_t 	*info;
3499 
3500 	/*
3501 	 * Allocate the UAR page for kernel use. This UAR page is
3502 	 * the privileged UAR page through which all kernel generated
3503 	 * doorbells will be rung. There are a number of UAR pages
3504 	 * reserved by hardware at the front of the UAR BAR, indicated
3505 	 * by DEVCAP.num_rsvd_uar, which we have already allocated. So,
3506 	 * the kernel page, or UAR page index num_rsvd_uar, will be
3507 	 * allocated here for kernel use.
3508 	 */
3509 
3510 	status = hermon_rsrc_alloc(state, HERMON_UARPG, 1, HERMON_SLEEP,
3511 	    &state->hs_uarkpg_rsrc);
3512 	if (status != DDI_SUCCESS) {
3513 		return (DDI_FAILURE);
3514 	}
3515 
3516 	/* Setup pointer to kernel UAR page */
3517 	state->hs_uar = (hermon_hw_uar_t *)state->hs_uarkpg_rsrc->hr_addr;
3518 
3519 	/* need to set up DBr tracking as well */
3520 	status = hermon_dbr_page_alloc(state, &info);
3521 	if (status != DDI_SUCCESS) {
3522 		return (DDI_FAILURE);
3523 	}
3524 	state->hs_kern_dbr = info;
3525 	return (DDI_SUCCESS);
3526 }
3527 
3528 
3529 /*
3530  * hermon_internal_uarpg_fini
3531  *    Context: Only called from attach() and/or detach() path contexts
3532  */
3533 static void
3534 hermon_internal_uarpg_fini(hermon_state_t *state)
3535 {
3536 	/* Free up Hermon UAR page #1 (kernel driver doorbells) */
3537 	hermon_rsrc_free(state, &state->hs_uarkpg_rsrc);
3538 }
3539 
3540 
3541 /*
3542  * hermon_special_qp_contexts_reserve()
3543  *    Context: Only called from attach() path context
3544  */
3545 static int
3546 hermon_special_qp_contexts_reserve(hermon_state_t *state)
3547 {
3548 	hermon_rsrc_t	*qp0_rsrc, *qp1_rsrc, *qp_resvd;
3549 	int		status;
3550 
3551 	/* Initialize the lock used for special QP rsrc management */
3552 	mutex_init(&state->hs_spec_qplock, NULL, MUTEX_DRIVER,
3553 	    DDI_INTR_PRI(state->hs_intrmsi_pri));
3554 
3555 	/*
3556 	 * Reserve contexts for QP0.  These QP contexts will be setup to
3557 	 * act as aliases for the real QP0.  Note: We are required to grab
3558 	 * two QPs (one per port) even if we are operating in single-port
3559 	 * mode.
3560 	 */
3561 	status = hermon_rsrc_alloc(state, HERMON_QPC, 2,
3562 	    HERMON_SLEEP, &qp0_rsrc);
3563 	if (status != DDI_SUCCESS) {
3564 		mutex_destroy(&state->hs_spec_qplock);
3565 		return (DDI_FAILURE);
3566 	}
3567 	state->hs_spec_qp0 = qp0_rsrc;
3568 
3569 	/*
3570 	 * Reserve contexts for QP1.  These QP contexts will be setup to
3571 	 * act as aliases for the real QP1.  Note: We are required to grab
3572 	 * two QPs (one per port) even if we are operating in single-port
3573 	 * mode.
3574 	 */
3575 	status = hermon_rsrc_alloc(state, HERMON_QPC, 2,
3576 	    HERMON_SLEEP, &qp1_rsrc);
3577 	if (status != DDI_SUCCESS) {
3578 		hermon_rsrc_free(state, &qp0_rsrc);
3579 		mutex_destroy(&state->hs_spec_qplock);
3580 		return (DDI_FAILURE);
3581 	}
3582 	state->hs_spec_qp1 = qp1_rsrc;
3583 
3584 	status = hermon_rsrc_alloc(state, HERMON_QPC, 4,
3585 	    HERMON_SLEEP, &qp_resvd);
3586 	if (status != DDI_SUCCESS) {
3587 		hermon_rsrc_free(state, &qp1_rsrc);
3588 		hermon_rsrc_free(state, &qp0_rsrc);
3589 		mutex_destroy(&state->hs_spec_qplock);
3590 		return (DDI_FAILURE);
3591 	}
3592 	state->hs_spec_qp_unused = qp_resvd;
3593 
3594 	return (DDI_SUCCESS);
3595 }
3596 
3597 
3598 /*
3599  * hermon_special_qp_contexts_unreserve()
3600  *    Context: Only called from attach() and/or detach() path contexts
3601  */
3602 static void
3603 hermon_special_qp_contexts_unreserve(hermon_state_t *state)
3604 {
3605 
3606 	/* Unreserve contexts for spec_qp_unused */
3607 	hermon_rsrc_free(state, &state->hs_spec_qp_unused);
3608 
3609 	/* Unreserve contexts for QP1 */
3610 	hermon_rsrc_free(state, &state->hs_spec_qp1);
3611 
3612 	/* Unreserve contexts for QP0 */
3613 	hermon_rsrc_free(state, &state->hs_spec_qp0);
3614 
3615 	/* Destroy the lock used for special QP rsrc management */
3616 	mutex_destroy(&state->hs_spec_qplock);
3617 
3618 }
3619 
3620 
3621 /*
3622  * hermon_sw_reset()
3623  *    Context: Currently called only from attach() path context
3624  */
3625 static int
3626 hermon_sw_reset(hermon_state_t *state)
3627 {
3628 	ddi_acc_handle_t	hdl = hermon_get_pcihdl(state);
3629 	ddi_acc_handle_t	cmdhdl = hermon_get_cmdhdl(state);
3630 	uint32_t		reset_delay;
3631 	int			status, i;
3632 	uint32_t		sem;
3633 	uint_t			offset;
3634 	uint32_t		data32;		/* for devctl & linkctl */
3635 	int			loopcnt;
3636 
3637 	/* initialize the FMA retry loop */
3638 	hermon_pio_init(fm_loop_cnt, fm_status, fm_test);
3639 	hermon_pio_init(fm_loop_cnt2, fm_status2, fm_test2);
3640 
3641 	/*
3642 	 * If the configured software reset delay is set to zero, then we
3643 	 * will not attempt a software reset of the Hermon device.
3644 	 */
3645 	reset_delay = state->hs_cfg_profile->cp_sw_reset_delay;
3646 	if (reset_delay == 0) {
3647 		return (DDI_SUCCESS);
3648 	}
3649 
3650 	/* the FMA retry loop starts. */
3651 	hermon_pio_start(state, cmdhdl, pio_error, fm_loop_cnt, fm_status,
3652 	    fm_test);
3653 	hermon_pio_start(state, hdl, pio_error2, fm_loop_cnt2, fm_status2,
3654 	    fm_test2);
3655 
3656 	/* Query the PCI capabilities of the HCA device */
3657 	/* but don't process the VPD until after reset */
3658 	status = hermon_pci_capability_list(state, hdl);
3659 	if (status != DDI_SUCCESS) {
3660 		cmn_err(CE_NOTE, "failed to get pci capabilities list(0x%x)\n",
3661 		    status);
3662 		return (DDI_FAILURE);
3663 	}
3664 
3665 	/*
3666 	 * Read all PCI config info (reg0...reg63).  Note: According to the
3667 	 * Hermon software reset application note, we should not read or
3668 	 * restore the values in reg22 and reg23.
3669 	 * NOTE:  For Hermon (and Arbel too) it says to restore the command
3670 	 * register LAST, and technically, you need to restore the
3671 	 * PCIE Capability "device control" and "link control" (word-sized,
3672 	 * at offsets 0x08 and 0x10 from the capbility ID respectively).
3673 	 * We hold off restoring the command register - offset 0x4 - till last
3674 	 */
3675 
3676 	/* 1st, wait for the semaphore assure accessibility - per PRM */
3677 	status = -1;
3678 	for (i = 0; i < NANOSEC/MICROSEC /* 1sec timeout */; i++) {
3679 		sem = ddi_get32(cmdhdl, state->hs_cmd_regs.sw_semaphore);
3680 		if (sem == 0) {
3681 			status = 0;
3682 			break;
3683 		}
3684 		drv_usecwait(1);
3685 	}
3686 
3687 	/* Check if timeout happens */
3688 	if (status == -1) {
3689 		/*
3690 		 * Remove this acc handle from Hermon, then log
3691 		 * the error.
3692 		 */
3693 		hermon_pci_config_teardown(state, &hdl);
3694 
3695 		cmn_err(CE_WARN, "hermon_sw_reset timeout: "
3696 		    "failed to get the semaphore(0x%p)\n",
3697 		    (void *)state->hs_cmd_regs.sw_semaphore);
3698 
3699 		hermon_fm_ereport(state, HCA_IBA_ERR, HCA_ERR_NON_FATAL);
3700 		return (DDI_FAILURE);
3701 	}
3702 
3703 	for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) {
3704 		if ((i != HERMON_SW_RESET_REG22_RSVD) &&
3705 		    (i != HERMON_SW_RESET_REG23_RSVD)) {
3706 			state->hs_cfg_data[i]  = pci_config_get32(hdl, i << 2);
3707 		}
3708 	}
3709 
3710 	/*
3711 	 * Perform the software reset (by writing 1 at offset 0xF0010)
3712 	 */
3713 	ddi_put32(cmdhdl, state->hs_cmd_regs.sw_reset, HERMON_SW_RESET_START);
3714 
3715 	/*
3716 	 * This delay is required so as not to cause a panic here. If the
3717 	 * device is accessed too soon after reset it will not respond to
3718 	 * config cycles, causing a Master Abort and panic.
3719 	 */
3720 	drv_usecwait(reset_delay);
3721 
3722 	/*
3723 	 * Poll waiting for the device to finish resetting.
3724 	 */
3725 	loopcnt = 100;	/* 100 times @ 100 usec - total delay 10 msec */
3726 	while ((pci_config_get32(hdl, 0) & 0x0000FFFF) != PCI_VENID_MLX) {
3727 		drv_usecwait(HERMON_SW_RESET_POLL_DELAY);
3728 		if (--loopcnt == 0)
3729 			break;	/* just in case, break and go on */
3730 	}
3731 	if (loopcnt == 0)
3732 		cmn_err(CE_CONT, "!Never see VEND_ID - read == %X",
3733 		    pci_config_get32(hdl, 0));
3734 
3735 	/*
3736 	 * Restore the config info
3737 	 */
3738 	for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) {
3739 		if (i == 1) continue;	/* skip the status/ctrl reg */
3740 		if ((i != HERMON_SW_RESET_REG22_RSVD) &&
3741 		    (i != HERMON_SW_RESET_REG23_RSVD)) {
3742 			pci_config_put32(hdl, i << 2, state->hs_cfg_data[i]);
3743 		}
3744 	}
3745 
3746 	/*
3747 	 * PCI Express Capability - we saved during capability list, and
3748 	 * we'll restore them here.
3749 	 */
3750 	offset = state->hs_pci_cap_offset;
3751 	data32 = state->hs_pci_cap_devctl;
3752 	pci_config_put32(hdl, offset + HERMON_PCI_CAP_DEV_OFFS, data32);
3753 	data32 = state->hs_pci_cap_lnkctl;
3754 	pci_config_put32(hdl, offset + HERMON_PCI_CAP_LNK_OFFS, data32);
3755 
3756 	pci_config_put32(hdl, 0x04, (state->hs_cfg_data[1] | 0x0006));
3757 
3758 	/* the FMA retry loop ends. */
3759 	hermon_pio_end(state, hdl, pio_error2, fm_loop_cnt2, fm_status2,
3760 	    fm_test2);
3761 	hermon_pio_end(state, cmdhdl, pio_error, fm_loop_cnt, fm_status,
3762 	    fm_test);
3763 
3764 	return (DDI_SUCCESS);
3765 
3766 pio_error2:
3767 	/* fall through */
3768 pio_error:
3769 	hermon_fm_ereport(state, HCA_SYS_ERR, HCA_ERR_NON_FATAL);
3770 	return (DDI_FAILURE);
3771 }
3772 
3773 
3774 /*
3775  * hermon_mcg_init()
3776  *    Context: Only called from attach() path context
3777  */
3778 static int
3779 hermon_mcg_init(hermon_state_t *state)
3780 {
3781 	uint_t		mcg_tmp_sz;
3782 
3783 
3784 	/*
3785 	 * Allocate space for the MCG temporary copy buffer.  This is
3786 	 * used by the Attach/Detach Multicast Group code
3787 	 */
3788 	mcg_tmp_sz = HERMON_MCGMEM_SZ(state);
3789 	state->hs_mcgtmp = kmem_zalloc(mcg_tmp_sz, KM_SLEEP);
3790 
3791 	/*
3792 	 * Initialize the multicast group mutex.  This ensures atomic
3793 	 * access to add, modify, and remove entries in the multicast
3794 	 * group hash lists.
3795 	 */
3796 	mutex_init(&state->hs_mcglock, NULL, MUTEX_DRIVER,
3797 	    DDI_INTR_PRI(state->hs_intrmsi_pri));
3798 
3799 	return (DDI_SUCCESS);
3800 }
3801 
3802 
3803 /*
3804  * hermon_mcg_fini()
3805  *    Context: Only called from attach() and/or detach() path contexts
3806  */
3807 static void
3808 hermon_mcg_fini(hermon_state_t *state)
3809 {
3810 	uint_t		mcg_tmp_sz;
3811 
3812 
3813 	/* Free up the space used for the MCG temporary copy buffer */
3814 	mcg_tmp_sz = HERMON_MCGMEM_SZ(state);
3815 	kmem_free(state->hs_mcgtmp, mcg_tmp_sz);
3816 
3817 	/* Destroy the multicast group mutex */
3818 	mutex_destroy(&state->hs_mcglock);
3819 
3820 }
3821 
3822 
3823 /*
3824  * hermon_fw_version_check()
3825  *    Context: Only called from attach() path context
3826  */
3827 static int
3828 hermon_fw_version_check(hermon_state_t *state)
3829 {
3830 
3831 	uint_t	hermon_fw_ver_major;
3832 	uint_t	hermon_fw_ver_minor;
3833 	uint_t	hermon_fw_ver_subminor;
3834 
3835 #ifdef FMA_TEST
3836 	if (hermon_test_num == -1) {
3837 		return (DDI_FAILURE);
3838 	}
3839 #endif
3840 
3841 	/*
3842 	 * Depending on which version of driver we have attached, and which
3843 	 * HCA we've attached, the firmware version checks will be different.
3844 	 * We set up the comparison values for both Arbel and Sinai HCAs.
3845 	 */
3846 	switch (state->hs_operational_mode) {
3847 	case HERMON_HCA_MODE:
3848 		hermon_fw_ver_major = HERMON_FW_VER_MAJOR;
3849 		hermon_fw_ver_minor = HERMON_FW_VER_MINOR;
3850 		hermon_fw_ver_subminor = HERMON_FW_VER_SUBMINOR;
3851 		break;
3852 
3853 	default:
3854 		return (DDI_FAILURE);
3855 	}
3856 
3857 	/*
3858 	 * If FW revision major number is less than acceptable,
3859 	 * return failure, else if greater return success.  If
3860 	 * the major numbers are equal than check the minor number
3861 	 */
3862 	if (state->hs_fw.fw_rev_major < hermon_fw_ver_major) {
3863 		return (DDI_FAILURE);
3864 	} else if (state->hs_fw.fw_rev_major > hermon_fw_ver_major) {
3865 		return (DDI_SUCCESS);
3866 	}
3867 
3868 	/*
3869 	 * Do the same check as above, except for minor revision numbers
3870 	 * If the minor numbers are equal than check the subminor number
3871 	 */
3872 	if (state->hs_fw.fw_rev_minor < hermon_fw_ver_minor) {
3873 		return (DDI_FAILURE);
3874 	} else if (state->hs_fw.fw_rev_minor > hermon_fw_ver_minor) {
3875 		return (DDI_SUCCESS);
3876 	}
3877 
3878 	/*
3879 	 * Once again we do the same check as above, except for the subminor
3880 	 * revision number.  If the subminor numbers are equal here, then
3881 	 * these are the same firmware version, return success
3882 	 */
3883 	if (state->hs_fw.fw_rev_subminor < hermon_fw_ver_subminor) {
3884 		return (DDI_FAILURE);
3885 	} else if (state->hs_fw.fw_rev_subminor > hermon_fw_ver_subminor) {
3886 		return (DDI_SUCCESS);
3887 	}
3888 
3889 	return (DDI_SUCCESS);
3890 }
3891 
3892 
3893 /*
3894  * hermon_device_info_report()
3895  *    Context: Only called from attach() path context
3896  */
3897 static void
3898 hermon_device_info_report(hermon_state_t *state)
3899 {
3900 
3901 	cmn_err(CE_CONT, "?hermon%d: FW ver: %04d.%04d.%04d, "
3902 	    "HW rev: %02d\n", state->hs_instance, state->hs_fw.fw_rev_major,
3903 	    state->hs_fw.fw_rev_minor, state->hs_fw.fw_rev_subminor,
3904 	    state->hs_revision_id);
3905 	cmn_err(CE_CONT, "?hermon%d: %64s (0x%016" PRIx64 ")\n",
3906 	    state->hs_instance, state->hs_nodedesc, state->hs_nodeguid);
3907 
3908 }
3909 
3910 
3911 /*
3912  * hermon_pci_capability_list()
3913  *    Context: Only called from attach() path context
3914  */
3915 static int
3916 hermon_pci_capability_list(hermon_state_t *state, ddi_acc_handle_t hdl)
3917 {
3918 	uint_t		offset, data;
3919 	uint32_t	data32;
3920 
3921 	state->hs_pci_cap_offset = 0;		/* make sure it's cleared */
3922 
3923 	/*
3924 	 * Check for the "PCI Capabilities" bit in the "Status Register".
3925 	 * Bit 4 in this register indicates the presence of a "PCI
3926 	 * Capabilities" list.
3927 	 *
3928 	 * PCI-Express requires this bit to be set to 1.
3929 	 */
3930 	data = pci_config_get16(hdl, 0x06);
3931 	if ((data & 0x10) == 0) {
3932 		return (DDI_FAILURE);
3933 	}
3934 
3935 	/*
3936 	 * Starting from offset 0x34 in PCI config space, find the
3937 	 * head of "PCI capabilities" list, and walk the list.  If
3938 	 * capabilities of a known type are encountered (e.g.
3939 	 * "PCI-X Capability"), then call the appropriate handler
3940 	 * function.
3941 	 */
3942 	offset = pci_config_get8(hdl, 0x34);
3943 	while (offset != 0x0) {
3944 		data = pci_config_get8(hdl, offset);
3945 		/*
3946 		 * Check for known capability types.  Hermon has the
3947 		 * following:
3948 		 *    o Power Mgmt	 (0x02)
3949 		 *    o VPD Capability   (0x03)
3950 		 *    o PCI-E Capability (0x10)
3951 		 *    o MSIX Capability  (0x11)
3952 		 */
3953 		switch (data) {
3954 		case 0x01:
3955 			/* power mgmt handling */
3956 			break;
3957 		case 0x03:
3958 
3959 /*
3960  * Reading the PCIe VPD is inconsistent - that is, sometimes causes
3961  * problems on (mostly) X64, though we've also seen problems w/ Sparc
3962  * and Tavor --- so, for now until it's root caused, don't try and
3963  * read it
3964  */
3965 #ifdef HERMON_VPD_WORKS
3966 			hermon_pci_capability_vpd(state, hdl, offset);
3967 #else
3968 			delay(100);
3969 			hermon_pci_capability_vpd(state, hdl, offset);
3970 #endif
3971 			break;
3972 		case 0x10:
3973 			/*
3974 			 * PCI Express Capability - save offset & contents
3975 			 * for later in reset
3976 			 */
3977 			state->hs_pci_cap_offset = offset;
3978 			data32 = pci_config_get32(hdl,
3979 			    offset + HERMON_PCI_CAP_DEV_OFFS);
3980 			state->hs_pci_cap_devctl = data32;
3981 			data32 = pci_config_get32(hdl,
3982 			    offset + HERMON_PCI_CAP_LNK_OFFS);
3983 			state->hs_pci_cap_lnkctl = data32;
3984 			break;
3985 		case 0x11:
3986 			/*
3987 			 * MSIX support - nothing to do, taken care of in the
3988 			 * MSI/MSIX interrupt frameworkd
3989 			 */
3990 			break;
3991 		default:
3992 			/* just go on to the next */
3993 			break;
3994 		}
3995 
3996 		/* Get offset of next entry in list */
3997 		offset = pci_config_get8(hdl, offset + 1);
3998 	}
3999 
4000 	return (DDI_SUCCESS);
4001 }
4002 
4003 /*
4004  * hermon_pci_read_vpd()
4005  *    Context: Only called from attach() path context
4006  *    utility routine for hermon_pci_capability_vpd()
4007  */
4008 static int
4009 hermon_pci_read_vpd(ddi_acc_handle_t hdl, uint_t offset, uint32_t addr,
4010     uint32_t *data)
4011 {
4012 	int		retry = 40;  /* retry counter for EEPROM poll */
4013 	uint32_t	val;
4014 	int		vpd_addr = offset + 2;
4015 	int		vpd_data = offset + 4;
4016 
4017 	/*
4018 	 * In order to read a 32-bit value from VPD, we are to write down
4019 	 * the address (offset in the VPD itself) to the address register.
4020 	 * To signal the read, we also clear bit 31.  We then poll on bit 31
4021 	 * and when it is set, we can then read our 4 bytes from the data
4022 	 * register.
4023 	 */
4024 	(void) pci_config_put32(hdl, offset, addr << 16);
4025 	do {
4026 		drv_usecwait(1000);
4027 		val = pci_config_get16(hdl, vpd_addr);
4028 		if (val & 0x8000) {		/* flag bit set */
4029 			*data = pci_config_get32(hdl, vpd_data);
4030 			return (DDI_SUCCESS);
4031 		}
4032 	} while (--retry);
4033 	/* read of flag failed write one message but count the failures */
4034 	if (debug_vpd == 0)
4035 		cmn_err(CE_NOTE,
4036 		    "!Failed to see flag bit after VPD addr write\n");
4037 	debug_vpd++;
4038 
4039 
4040 vpd_read_fail:
4041 	return (DDI_FAILURE);
4042 }
4043 
4044 
4045 
4046 /*
4047  *   hermon_pci_capability_vpd()
4048  *    Context: Only called from attach() path context
4049  */
4050 static void
4051 hermon_pci_capability_vpd(hermon_state_t *state, ddi_acc_handle_t hdl,
4052     uint_t offset)
4053 {
4054 	uint8_t			name_length;
4055 	uint8_t			pn_length;
4056 	int			i, err = 0;
4057 	int			vpd_str_id = 0;
4058 	int			vpd_ro_desc;
4059 	int			vpd_ro_pn_desc;
4060 #ifdef _BIG_ENDIAN
4061 	uint32_t		data32;
4062 #endif /* _BIG_ENDIAN */
4063 	union {
4064 		uint32_t	vpd_int[HERMON_VPD_HDR_DWSIZE];
4065 		uchar_t		vpd_char[HERMON_VPD_HDR_BSIZE];
4066 	} vpd;
4067 
4068 
4069 	/*
4070 	 * Read in the Vital Product Data (VPD) to the extend needed
4071 	 * by the fwflash utility
4072 	 */
4073 	for (i = 0; i < HERMON_VPD_HDR_DWSIZE; i++) {
4074 		err = hermon_pci_read_vpd(hdl, offset, i << 2, &vpd.vpd_int[i]);
4075 		if (err != DDI_SUCCESS) {
4076 			cmn_err(CE_NOTE, "!VPD read failed\n");
4077 			goto out;
4078 		}
4079 	}
4080 
4081 #ifdef _BIG_ENDIAN
4082 	/* Need to swap bytes for big endian. */
4083 	for (i = 0; i < HERMON_VPD_HDR_DWSIZE; i++) {
4084 		data32 = vpd.vpd_int[i];
4085 		vpd.vpd_char[(i << 2) + 3] =
4086 		    (uchar_t)((data32 & 0xFF000000) >> 24);
4087 		vpd.vpd_char[(i << 2) + 2] =
4088 		    (uchar_t)((data32 & 0x00FF0000) >> 16);
4089 		vpd.vpd_char[(i << 2) + 1] =
4090 		    (uchar_t)((data32 & 0x0000FF00) >> 8);
4091 		vpd.vpd_char[i << 2] = (uchar_t)(data32 & 0x000000FF);
4092 	}
4093 #endif	/* _BIG_ENDIAN */
4094 
4095 	/* Check for VPD String ID Tag */
4096 	if (vpd.vpd_char[vpd_str_id] == 0x82) {
4097 		/* get the product name */
4098 		name_length = (uint8_t)vpd.vpd_char[vpd_str_id + 1];
4099 		if (name_length > sizeof (state->hs_hca_name)) {
4100 			cmn_err(CE_NOTE, "!VPD name too large (0x%x)\n",
4101 			    name_length);
4102 			goto out;
4103 		}
4104 		(void) memcpy(state->hs_hca_name, &vpd.vpd_char[vpd_str_id + 3],
4105 		    name_length);
4106 		state->hs_hca_name[name_length] = 0;
4107 
4108 		/* get the part number */
4109 		vpd_ro_desc = name_length + 3; /* read-only tag location */
4110 		vpd_ro_pn_desc = vpd_ro_desc + 3; /* P/N keyword location */
4111 
4112 		/* Verify read-only tag and Part Number keyword. */
4113 		if (vpd.vpd_char[vpd_ro_desc] != 0x90 ||
4114 		    (vpd.vpd_char[vpd_ro_pn_desc] != 'P' &&
4115 		    vpd.vpd_char[vpd_ro_pn_desc + 1] != 'N')) {
4116 			cmn_err(CE_NOTE, "!VPD Part Number not found\n");
4117 			goto out;
4118 		}
4119 
4120 		pn_length = (uint8_t)vpd.vpd_char[vpd_ro_pn_desc + 2];
4121 		if (pn_length > sizeof (state->hs_hca_pn)) {
4122 			cmn_err(CE_NOTE, "!VPD part number too large (0x%x)\n",
4123 			    name_length);
4124 			goto out;
4125 		}
4126 		(void) memcpy(state->hs_hca_pn,
4127 		    &vpd.vpd_char[vpd_ro_pn_desc + 3],
4128 		    pn_length);
4129 		state->hs_hca_pn[pn_length] = 0;
4130 		state->hs_hca_pn_len = pn_length;
4131 		cmn_err(CE_CONT, "!vpd %s\n", state->hs_hca_pn);
4132 	} else {
4133 		/* Wrong VPD String ID Tag */
4134 		cmn_err(CE_NOTE, "!VPD String ID Tag not found, tag: %02x\n",
4135 		    vpd.vpd_char[0]);
4136 		goto out;
4137 	}
4138 	return;
4139 out:
4140 	state->hs_hca_pn_len = 0;
4141 }
4142 
4143 
4144 
4145 /*
4146  * hermon_intr_or_msi_init()
4147  *    Context: Only called from attach() path context
4148  */
4149 static int
4150 hermon_intr_or_msi_init(hermon_state_t *state)
4151 {
4152 	int	status;
4153 
4154 
4155 	/* Query for the list of supported interrupt event types */
4156 	status = ddi_intr_get_supported_types(state->hs_dip,
4157 	    &state->hs_intr_types_avail);
4158 	if (status != DDI_SUCCESS) {
4159 		return (DDI_FAILURE);
4160 	}
4161 
4162 	/*
4163 	 * If Hermon supports MSI-X in this system (and, if it
4164 	 * hasn't been overridden by a configuration variable), then
4165 	 * the default behavior is to use a single MSI-X.  Otherwise,
4166 	 * fallback to using legacy interrupts.  Also, if MSI-X is chosen,
4167 	 * but fails for whatever reasons, then next try MSI
4168 	 */
4169 	if ((state->hs_cfg_profile->cp_use_msi_if_avail != 0) &&
4170 	    (state->hs_intr_types_avail & DDI_INTR_TYPE_MSIX)) {
4171 		status = hermon_add_intrs(state, DDI_INTR_TYPE_MSIX);
4172 		if (status == DDI_SUCCESS) {
4173 			state->hs_intr_type_chosen = DDI_INTR_TYPE_MSIX;
4174 			return (DDI_SUCCESS);
4175 		}
4176 	}
4177 
4178 	/*
4179 	 * If Hermon supports MSI in this system (and, if it
4180 	 * hasn't been overridden by a configuration variable), then
4181 	 * the default behavior is to use a single MSIX.  Otherwise,
4182 	 * fallback to using legacy interrupts.  Also, if MSI is chosen,
4183 	 * but fails for whatever reasons, then fallback to using legacy
4184 	 * interrupts.
4185 	 */
4186 	if ((state->hs_cfg_profile->cp_use_msi_if_avail != 0) &&
4187 	    (state->hs_intr_types_avail & DDI_INTR_TYPE_MSI)) {
4188 		status = hermon_add_intrs(state, DDI_INTR_TYPE_MSI);
4189 		if (status == DDI_SUCCESS) {
4190 			state->hs_intr_type_chosen = DDI_INTR_TYPE_MSI;
4191 			return (DDI_SUCCESS);
4192 		}
4193 	}
4194 
4195 	/*
4196 	 * MSI interrupt allocation failed, or was not available.  Fallback to
4197 	 * legacy interrupt support.
4198 	 */
4199 	if (state->hs_intr_types_avail & DDI_INTR_TYPE_FIXED) {
4200 		status = hermon_add_intrs(state, DDI_INTR_TYPE_FIXED);
4201 		if (status == DDI_SUCCESS) {
4202 			state->hs_intr_type_chosen = DDI_INTR_TYPE_FIXED;
4203 			return (DDI_SUCCESS);
4204 		}
4205 	}
4206 
4207 	/*
4208 	 * None of MSI, MSI-X, nor legacy interrupts were successful.
4209 	 * Return failure.
4210 	 */
4211 	return (DDI_FAILURE);
4212 }
4213 
4214 /*
4215  * hermon_add_intrs()
4216  *    Context: Only called from attach() patch context
4217  */
4218 static int
4219 hermon_add_intrs(hermon_state_t *state, int intr_type)
4220 {
4221 	int	status;
4222 
4223 
4224 	/* Get number of interrupts/MSI supported */
4225 	status = ddi_intr_get_nintrs(state->hs_dip, intr_type,
4226 	    &state->hs_intrmsi_count);
4227 	if (status != DDI_SUCCESS) {
4228 		return (DDI_FAILURE);
4229 	}
4230 
4231 	/* Get number of available interrupts/MSI */
4232 	status = ddi_intr_get_navail(state->hs_dip, intr_type,
4233 	    &state->hs_intrmsi_avail);
4234 	if (status != DDI_SUCCESS) {
4235 		return (DDI_FAILURE);
4236 	}
4237 
4238 	/* Ensure that we have at least one (1) usable MSI or interrupt */
4239 	if ((state->hs_intrmsi_avail < 1) || (state->hs_intrmsi_count < 1)) {
4240 		return (DDI_FAILURE);
4241 	}
4242 
4243 	/* Attempt to allocate the maximum #interrupt/MSI handles */
4244 	status = ddi_intr_alloc(state->hs_dip, &state->hs_intrmsi_hdl[0],
4245 	    intr_type, 0, min(HERMON_MSIX_MAX, state->hs_intrmsi_avail),
4246 	    &state->hs_intrmsi_allocd, DDI_INTR_ALLOC_NORMAL);
4247 	if (status != DDI_SUCCESS) {
4248 		return (DDI_FAILURE);
4249 	}
4250 
4251 	/* Ensure that we have allocated at least one (1) MSI or interrupt */
4252 	if (state->hs_intrmsi_allocd < 1) {
4253 		return (DDI_FAILURE);
4254 	}
4255 	state->hs_eq_dist = state->hs_intrmsi_allocd - 1; /* start at 0 */
4256 
4257 	/*
4258 	 * Extract the priority for the allocated interrupt/MSI.  This
4259 	 * will be used later when initializing certain mutexes.
4260 	 */
4261 	status = ddi_intr_get_pri(state->hs_intrmsi_hdl[0],
4262 	    &state->hs_intrmsi_pri);
4263 	if (status != DDI_SUCCESS) {
4264 		/* Free the allocated interrupt/MSI handle */
4265 		(void) ddi_intr_free(state->hs_intrmsi_hdl[0]);
4266 
4267 		return (DDI_FAILURE);
4268 	}
4269 
4270 	/* Make sure the interrupt/MSI priority is below 'high level' */
4271 	if (state->hs_intrmsi_pri >= ddi_intr_get_hilevel_pri()) {
4272 		/* Free the allocated interrupt/MSI handle */
4273 		(void) ddi_intr_free(state->hs_intrmsi_hdl[0]);
4274 
4275 		return (DDI_FAILURE);
4276 	}
4277 
4278 	/* Get add'l capability information regarding interrupt/MSI */
4279 	status = ddi_intr_get_cap(state->hs_intrmsi_hdl[0],
4280 	    &state->hs_intrmsi_cap);
4281 	if (status != DDI_SUCCESS) {
4282 		/* Free the allocated interrupt/MSI handle */
4283 		(void) ddi_intr_free(state->hs_intrmsi_hdl[0]);
4284 
4285 		return (DDI_FAILURE);
4286 	}
4287 
4288 	return (DDI_SUCCESS);
4289 }
4290 
4291 
4292 /*
4293  * hermon_intr_or_msi_fini()
4294  *    Context: Only called from attach() and/or detach() path contexts
4295  */
4296 static int
4297 hermon_intr_or_msi_fini(hermon_state_t *state)
4298 {
4299 	int	status;
4300 	int	intr;
4301 
4302 	for (intr = 0; intr < state->hs_intrmsi_allocd; intr++) {
4303 
4304 		/* Free the allocated interrupt/MSI handle */
4305 		status = ddi_intr_free(state->hs_intrmsi_hdl[intr]);
4306 		if (status != DDI_SUCCESS) {
4307 			return (DDI_FAILURE);
4308 		}
4309 	}
4310 	return (DDI_SUCCESS);
4311 }
4312 
4313 
4314 /*ARGSUSED*/
4315 void
4316 hermon_pci_capability_msix(hermon_state_t *state, ddi_acc_handle_t hdl,
4317     uint_t offset)
4318 {
4319 	uint32_t	msix_data;
4320 	uint16_t	msg_cntr;
4321 	uint32_t	t_offset;	/* table offset */
4322 	uint32_t	t_bir;
4323 	uint32_t	p_offset;	/* pba */
4324 	uint32_t	p_bir;
4325 	int		t_size;		/* size in entries - each is 4 dwords */
4326 
4327 	/* come in with offset pointing at the capability structure */
4328 
4329 	msix_data = pci_config_get32(hdl, offset);
4330 	cmn_err(CE_CONT, "Full cap structure dword = %X\n", msix_data);
4331 	msg_cntr =  pci_config_get16(hdl, offset+2);
4332 	cmn_err(CE_CONT, "MSIX msg_control = %X\n", msg_cntr);
4333 	offset += 4;
4334 	msix_data = pci_config_get32(hdl, offset);	/* table info */
4335 	t_offset = (msix_data & 0xFFF8) >> 3;
4336 	t_bir = msix_data & 0x07;
4337 	offset += 4;
4338 	cmn_err(CE_CONT, "  table %X --offset = %X, bir(bar) = %X\n",
4339 	    msix_data, t_offset, t_bir);
4340 	msix_data = pci_config_get32(hdl, offset);	/* PBA info */
4341 	p_offset = (msix_data & 0xFFF8) >> 3;
4342 	p_bir = msix_data & 0x07;
4343 
4344 	cmn_err(CE_CONT, "  PBA   %X --offset = %X, bir(bar) = %X\n",
4345 	    msix_data, p_offset, p_bir);
4346 	t_size = msg_cntr & 0x7FF;		/* low eleven bits */
4347 	cmn_err(CE_CONT, "    table size = %X entries\n", t_size);
4348 
4349 	offset = t_offset;		/* reuse this for offset from BAR */
4350 #ifdef HERMON_SUPPORTS_MSIX_BAR
4351 	cmn_err(CE_CONT, "First 2 table entries behind BAR2 \n");
4352 	for (i = 0; i < 2; i++) {
4353 		for (j = 0; j < 4; j++, offset += 4) {
4354 			msix_data = ddi_get32(state->hs_reg_msihdl,
4355 			    (uint32_t *)((uintptr_t)state->hs_reg_msi_baseaddr
4356 			    + offset));
4357 			cmn_err(CE_CONT, "MSI table entry %d, dword %d == %X\n",
4358 			    i, j, msix_data);
4359 		}
4360 	}
4361 #endif
4362 
4363 }
4364 
4365 /*
4366  * X86 fastreboot support functions.
4367  * These functions are used to save/restore MSI-X table/PBA and also
4368  * to disable MSI-X interrupts in hermon_quiesce().
4369  */
4370 
4371 /* Return the message control for MSI-X */
4372 static ushort_t
4373 get_msix_ctrl(dev_info_t *dip)
4374 {
4375 	ushort_t msix_ctrl = 0, caps_ctrl = 0;
4376 	hermon_state_t *state = ddi_get_soft_state(hermon_statep,
4377 	    DEVI(dip)->devi_instance);
4378 	ddi_acc_handle_t pci_cfg_hdl = hermon_get_pcihdl(state);
4379 	ASSERT(pci_cfg_hdl != NULL);
4380 
4381 	if ((PCI_CAP_LOCATE(pci_cfg_hdl,
4382 	    PCI_CAP_ID_MSI_X, &caps_ctrl) == DDI_SUCCESS)) {
4383 		if ((msix_ctrl = PCI_CAP_GET16(pci_cfg_hdl, NULL, caps_ctrl,
4384 		    PCI_MSIX_CTRL)) == PCI_CAP_EINVAL16)
4385 			return (0);
4386 	}
4387 	ASSERT(msix_ctrl != 0);
4388 
4389 	return (msix_ctrl);
4390 }
4391 
4392 /* Return the MSI-X table size */
4393 static size_t
4394 get_msix_tbl_size(dev_info_t *dip)
4395 {
4396 	ushort_t msix_ctrl = get_msix_ctrl(dip);
4397 	ASSERT(msix_ctrl != 0);
4398 
4399 	return (((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 1) *
4400 	    PCI_MSIX_VECTOR_SIZE);
4401 }
4402 
4403 /* Return the MSI-X PBA size */
4404 static size_t
4405 get_msix_pba_size(dev_info_t *dip)
4406 {
4407 	ushort_t msix_ctrl = get_msix_ctrl(dip);
4408 	ASSERT(msix_ctrl != 0);
4409 
4410 	return (((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 64) / 64 * 8);
4411 }
4412 
4413 /* Set up the MSI-X table/PBA save area */
4414 static void
4415 hermon_set_msix_info(hermon_state_t *state)
4416 {
4417 	uint_t			rnumber, breg, nregs;
4418 	ushort_t		caps_ctrl, msix_ctrl;
4419 	pci_regspec_t		*rp;
4420 	int			reg_size, addr_space, offset, *regs_list, i;
4421 
4422 	/*
4423 	 * MSI-X BIR Index Table:
4424 	 * BAR indicator register (BIR) to Base Address register.
4425 	 */
4426 	uchar_t pci_msix_bir_index[8] = {0x10, 0x14, 0x18, 0x1c,
4427 	    0x20, 0x24, 0xff, 0xff};
4428 
4429 	/* Fastreboot data access  attribute */
4430 	ddi_device_acc_attr_t	dev_attr = {
4431 		0,				/* version */
4432 		DDI_STRUCTURE_LE_ACC,
4433 		DDI_STRICTORDER_ACC,		/* attr access */
4434 		0
4435 	};
4436 
4437 	ddi_acc_handle_t pci_cfg_hdl = hermon_get_pcihdl(state);
4438 	ASSERT(pci_cfg_hdl != NULL);
4439 
4440 	if ((PCI_CAP_LOCATE(pci_cfg_hdl,
4441 	    PCI_CAP_ID_MSI_X, &caps_ctrl) == DDI_SUCCESS)) {
4442 		if ((msix_ctrl = PCI_CAP_GET16(pci_cfg_hdl, NULL, caps_ctrl,
4443 		    PCI_MSIX_CTRL)) == PCI_CAP_EINVAL16)
4444 			return;
4445 	}
4446 	ASSERT(msix_ctrl != 0);
4447 
4448 	state->hs_msix_tbl_offset = PCI_CAP_GET32(pci_cfg_hdl, NULL, caps_ctrl,
4449 	    PCI_MSIX_TBL_OFFSET);
4450 
4451 	/* Get the BIR for MSI-X table */
4452 	breg = pci_msix_bir_index[state->hs_msix_tbl_offset &
4453 	    PCI_MSIX_TBL_BIR_MASK];
4454 	ASSERT(breg != 0xFF);
4455 
4456 	/* Set the MSI-X table offset */
4457 	state->hs_msix_tbl_offset = state->hs_msix_tbl_offset &
4458 	    ~PCI_MSIX_TBL_BIR_MASK;
4459 
4460 	/* Set the MSI-X table size */
4461 	state->hs_msix_tbl_size = ((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 1) *
4462 	    PCI_MSIX_VECTOR_SIZE;
4463 
4464 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, state->hs_dip,
4465 	    DDI_PROP_DONTPASS, "reg", (int **)&regs_list, &nregs) !=
4466 	    DDI_PROP_SUCCESS) {
4467 		return;
4468 	}
4469 	reg_size = sizeof (pci_regspec_t) / sizeof (int);
4470 
4471 	/* Check the register number for MSI-X table */
4472 	for (i = 1, rnumber = 0; i < nregs/reg_size; i++) {
4473 		rp = (pci_regspec_t *)&regs_list[i * reg_size];
4474 		addr_space = rp->pci_phys_hi & PCI_ADDR_MASK;
4475 		offset = PCI_REG_REG_G(rp->pci_phys_hi);
4476 
4477 		if ((offset == breg) && ((addr_space == PCI_ADDR_MEM32) ||
4478 		    (addr_space == PCI_ADDR_MEM64))) {
4479 			rnumber = i;
4480 			break;
4481 		}
4482 	}
4483 	ASSERT(rnumber != 0);
4484 	state->hs_msix_tbl_rnumber = rnumber;
4485 
4486 	/* Set device attribute version and access according to Hermon FM */
4487 	dev_attr.devacc_attr_version = hermon_devacc_attr_version(state);
4488 	dev_attr.devacc_attr_access = hermon_devacc_attr_access(state);
4489 
4490 	/* Map the entire MSI-X vector table */
4491 	if (hermon_regs_map_setup(state, state->hs_msix_tbl_rnumber,
4492 	    (caddr_t *)&state->hs_msix_tbl_addr, state->hs_msix_tbl_offset,
4493 	    state->hs_msix_tbl_size, &dev_attr,
4494 	    &state->hs_fm_msix_tblhdl) != DDI_SUCCESS) {
4495 		return;
4496 	}
4497 
4498 	state->hs_msix_pba_offset = PCI_CAP_GET32(pci_cfg_hdl, NULL, caps_ctrl,
4499 	    PCI_MSIX_PBA_OFFSET);
4500 
4501 	/* Get the BIR for MSI-X PBA */
4502 	breg = pci_msix_bir_index[state->hs_msix_pba_offset &
4503 	    PCI_MSIX_PBA_BIR_MASK];
4504 	ASSERT(breg != 0xFF);
4505 
4506 	/* Set the MSI-X PBA offset */
4507 	state->hs_msix_pba_offset = state->hs_msix_pba_offset &
4508 	    ~PCI_MSIX_PBA_BIR_MASK;
4509 
4510 	/* Set the MSI-X PBA size */
4511 	state->hs_msix_pba_size =
4512 	    ((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 64) / 64 * 8;
4513 
4514 	/* Check the register number for MSI-X PBA */
4515 	for (i = 1, rnumber = 0; i < nregs/reg_size; i++) {
4516 		rp = (pci_regspec_t *)&regs_list[i * reg_size];
4517 		addr_space = rp->pci_phys_hi & PCI_ADDR_MASK;
4518 		offset = PCI_REG_REG_G(rp->pci_phys_hi);
4519 
4520 		if ((offset == breg) && ((addr_space == PCI_ADDR_MEM32) ||
4521 		    (addr_space == PCI_ADDR_MEM64))) {
4522 			rnumber = i;
4523 			break;
4524 		}
4525 	}
4526 	ASSERT(rnumber != 0);
4527 	state->hs_msix_pba_rnumber = rnumber;
4528 
4529 	/* Map in the MSI-X Pending Bit Array */
4530 	if (hermon_regs_map_setup(state, state->hs_msix_pba_rnumber,
4531 	    (caddr_t *)&state->hs_msix_pba_addr, state->hs_msix_pba_offset,
4532 	    state->hs_msix_pba_size, &dev_attr,
4533 	    &state->hs_fm_msix_pbahdl) != DDI_SUCCESS) {
4534 		hermon_regs_map_free(state, &state->hs_fm_msix_tblhdl);
4535 		state->hs_fm_msix_tblhdl = NULL;
4536 		return;
4537 	}
4538 
4539 	/* Set the MSI-X table save area */
4540 	state->hs_msix_tbl_entries = kmem_alloc(state->hs_msix_tbl_size,
4541 	    KM_SLEEP);
4542 
4543 	/* Set the MSI-X PBA save area */
4544 	state->hs_msix_pba_entries = kmem_alloc(state->hs_msix_pba_size,
4545 	    KM_SLEEP);
4546 }
4547 
4548 /* Disable Hermon interrupts */
4549 static int
4550 hermon_intr_disable(hermon_state_t *state)
4551 {
4552 	ushort_t msix_ctrl = 0, caps_ctrl = 0;
4553 	ddi_acc_handle_t pci_cfg_hdl = hermon_get_pcihdl(state);
4554 	ddi_acc_handle_t msix_tblhdl = hermon_get_msix_tblhdl(state);
4555 	int i, j;
4556 	ASSERT(pci_cfg_hdl != NULL && msix_tblhdl != NULL);
4557 	ASSERT(state->hs_intr_types_avail &
4558 	    (DDI_INTR_TYPE_FIXED | DDI_INTR_TYPE_MSI | DDI_INTR_TYPE_MSIX));
4559 
4560 	/*
4561 	 * Check if MSI-X interrupts are used. If so, disable MSI-X interupts.
4562 	 * If not, since Hermon doesn't support MSI interrupts, assuming the
4563 	 * legacy interrupt is used instead, disable the legacy interrupt.
4564 	 */
4565 	if ((state->hs_cfg_profile->cp_use_msi_if_avail != 0) &&
4566 	    (state->hs_intr_types_avail & DDI_INTR_TYPE_MSIX)) {
4567 
4568 		if ((PCI_CAP_LOCATE(pci_cfg_hdl,
4569 		    PCI_CAP_ID_MSI_X, &caps_ctrl) == DDI_SUCCESS)) {
4570 			if ((msix_ctrl = PCI_CAP_GET16(pci_cfg_hdl, NULL,
4571 			    caps_ctrl, PCI_MSIX_CTRL)) == PCI_CAP_EINVAL16)
4572 				return (DDI_FAILURE);
4573 		}
4574 		ASSERT(msix_ctrl != 0);
4575 
4576 		if (!(msix_ctrl & PCI_MSIX_ENABLE_BIT))
4577 			return (DDI_SUCCESS);
4578 
4579 		/* Clear all inums in MSI-X table */
4580 		for (i = 0; i < get_msix_tbl_size(state->hs_dip);
4581 		    i += PCI_MSIX_VECTOR_SIZE) {
4582 			for (j = 0; j < PCI_MSIX_VECTOR_SIZE; j += 4) {
4583 				char *addr = state->hs_msix_tbl_addr + i + j;
4584 				ddi_put32(msix_tblhdl,
4585 				    (uint32_t *)(uintptr_t)addr, 0x0);
4586 			}
4587 		}
4588 
4589 		/* Disable MSI-X interrupts */
4590 		msix_ctrl &= ~PCI_MSIX_ENABLE_BIT;
4591 		PCI_CAP_PUT16(pci_cfg_hdl, NULL, caps_ctrl, PCI_MSIX_CTRL,
4592 		    msix_ctrl);
4593 
4594 	} else {
4595 		uint16_t cmdreg = pci_config_get16(pci_cfg_hdl, PCI_CONF_COMM);
4596 		ASSERT(state->hs_intr_types_avail & DDI_INTR_TYPE_FIXED);
4597 
4598 		/* Disable the legacy interrupts */
4599 		cmdreg |= PCI_COMM_INTX_DISABLE;
4600 		pci_config_put16(pci_cfg_hdl, PCI_CONF_COMM, cmdreg);
4601 	}
4602 
4603 	return (DDI_SUCCESS);
4604 }
4605 
4606 /* Hermon quiesce(9F) entry */
4607 static int
4608 hermon_quiesce(dev_info_t *dip)
4609 {
4610 	hermon_state_t *state = ddi_get_soft_state(hermon_statep,
4611 	    DEVI(dip)->devi_instance);
4612 	ddi_acc_handle_t pcihdl = hermon_get_pcihdl(state);
4613 	ddi_acc_handle_t cmdhdl = hermon_get_cmdhdl(state);
4614 	ddi_acc_handle_t msix_tbl_hdl = hermon_get_msix_tblhdl(state);
4615 	ddi_acc_handle_t msix_pba_hdl = hermon_get_msix_pbahdl(state);
4616 	uint32_t sem, reset_delay = state->hs_cfg_profile->cp_sw_reset_delay;
4617 	uint64_t data64;
4618 	uint32_t data32;
4619 	int status, i, j, loopcnt;
4620 	uint_t offset;
4621 
4622 	ASSERT(state != NULL);
4623 
4624 	/* start fastreboot */
4625 	state->hs_quiescing = B_TRUE;
4626 
4627 	/* If it's in maintenance mode, do nothing but return with SUCCESS */
4628 	if (!HERMON_IS_OPERATIONAL(state->hs_operational_mode)) {
4629 		return (DDI_SUCCESS);
4630 	}
4631 
4632 	/* suppress Hermon FM ereports */
4633 	if (hermon_get_state(state) & HCA_EREPORT_FM) {
4634 		hermon_clr_state_nolock(state, HCA_EREPORT_FM);
4635 	}
4636 
4637 	/* Shutdown HCA ports */
4638 	if (hermon_hca_ports_shutdown(state,
4639 	    state->hs_cfg_profile->cp_num_ports) != HERMON_CMD_SUCCESS) {
4640 		state->hs_quiescing = B_FALSE;
4641 		return (DDI_FAILURE);
4642 	}
4643 
4644 	/* Close HCA */
4645 	if (hermon_close_hca_cmd_post(state, HERMON_CMD_NOSLEEP_SPIN) !=
4646 	    HERMON_CMD_SUCCESS) {
4647 		state->hs_quiescing = B_FALSE;
4648 		return (DDI_FAILURE);
4649 	}
4650 
4651 	/* Disable interrupts */
4652 	if (hermon_intr_disable(state) != DDI_SUCCESS) {
4653 		state->hs_quiescing = B_FALSE;
4654 		return (DDI_FAILURE);
4655 	}
4656 
4657 	/*
4658 	 * Query the PCI capabilities of the HCA device, but don't process
4659 	 * the VPD until after reset.
4660 	 */
4661 	if (hermon_pci_capability_list(state, pcihdl) != DDI_SUCCESS) {
4662 		state->hs_quiescing = B_FALSE;
4663 		return (DDI_FAILURE);
4664 	}
4665 
4666 	/*
4667 	 * Read all PCI config info (reg0...reg63).  Note: According to the
4668 	 * Hermon software reset application note, we should not read or
4669 	 * restore the values in reg22 and reg23.
4670 	 * NOTE:  For Hermon (and Arbel too) it says to restore the command
4671 	 * register LAST, and technically, you need to restore the
4672 	 * PCIE Capability "device control" and "link control" (word-sized,
4673 	 * at offsets 0x08 and 0x10 from the capbility ID respectively).
4674 	 * We hold off restoring the command register - offset 0x4 - till last
4675 	 */
4676 
4677 	/* 1st, wait for the semaphore assure accessibility - per PRM */
4678 	status = -1;
4679 	for (i = 0; i < NANOSEC/MICROSEC /* 1sec timeout */; i++) {
4680 		sem = ddi_get32(cmdhdl, state->hs_cmd_regs.sw_semaphore);
4681 		if (sem == 0) {
4682 			status = 0;
4683 			break;
4684 		}
4685 		drv_usecwait(1);
4686 	}
4687 
4688 	/* Check if timeout happens */
4689 	if (status == -1) {
4690 		state->hs_quiescing = B_FALSE;
4691 		return (DDI_FAILURE);
4692 	}
4693 
4694 	/* MSI-X interrupts are used, save the MSI-X table */
4695 	if (msix_tbl_hdl && msix_pba_hdl) {
4696 		/* save MSI-X table */
4697 		for (i = 0; i < get_msix_tbl_size(state->hs_dip);
4698 		    i += PCI_MSIX_VECTOR_SIZE) {
4699 			for (j = 0; j < PCI_MSIX_VECTOR_SIZE; j += 4) {
4700 				char *addr = state->hs_msix_tbl_addr + i + j;
4701 				data32 = ddi_get32(msix_tbl_hdl,
4702 				    (uint32_t *)(uintptr_t)addr);
4703 				*(uint32_t *)(uintptr_t)(state->
4704 				    hs_msix_tbl_entries + i + j) = data32;
4705 			}
4706 		}
4707 		/* save MSI-X PBA */
4708 		for (i = 0; i < get_msix_pba_size(state->hs_dip); i += 8) {
4709 			char *addr = state->hs_msix_pba_addr + i;
4710 			data64 = ddi_get64(msix_pba_hdl,
4711 			    (uint64_t *)(uintptr_t)addr);
4712 			*(uint64_t *)(uintptr_t)(state->
4713 			    hs_msix_pba_entries + i) = data64;
4714 		}
4715 	}
4716 
4717 	/* save PCI config space */
4718 	for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) {
4719 		if ((i != HERMON_SW_RESET_REG22_RSVD) &&
4720 		    (i != HERMON_SW_RESET_REG23_RSVD)) {
4721 			state->hs_cfg_data[i]  =
4722 			    pci_config_get32(pcihdl, i << 2);
4723 		}
4724 	}
4725 
4726 	/* SW-reset HCA */
4727 	ddi_put32(cmdhdl, state->hs_cmd_regs.sw_reset, HERMON_SW_RESET_START);
4728 
4729 	/*
4730 	 * This delay is required so as not to cause a panic here. If the
4731 	 * device is accessed too soon after reset it will not respond to
4732 	 * config cycles, causing a Master Abort and panic.
4733 	 */
4734 	drv_usecwait(reset_delay);
4735 
4736 	/* Poll waiting for the device to finish resetting */
4737 	loopcnt = 100;	/* 100 times @ 100 usec - total delay 10 msec */
4738 	while ((pci_config_get32(pcihdl, 0) & 0x0000FFFF) != PCI_VENID_MLX) {
4739 		drv_usecwait(HERMON_SW_RESET_POLL_DELAY);
4740 		if (--loopcnt == 0)
4741 			break;	/* just in case, break and go on */
4742 	}
4743 	if (loopcnt == 0) {
4744 		state->hs_quiescing = B_FALSE;
4745 		return (DDI_FAILURE);
4746 	}
4747 
4748 	/* Restore the config info */
4749 	for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) {
4750 		if (i == 1) continue;	/* skip the status/ctrl reg */
4751 		if ((i != HERMON_SW_RESET_REG22_RSVD) &&
4752 		    (i != HERMON_SW_RESET_REG23_RSVD)) {
4753 			pci_config_put32(pcihdl, i << 2, state->hs_cfg_data[i]);
4754 		}
4755 	}
4756 
4757 	/* If MSI-X interrupts are used, restore the MSI-X table */
4758 	if (msix_tbl_hdl && msix_pba_hdl) {
4759 		/* restore MSI-X PBA */
4760 		for (i = 0; i < get_msix_pba_size(state->hs_dip); i += 8) {
4761 			char *addr = state->hs_msix_pba_addr + i;
4762 			data64 = *(uint64_t *)(uintptr_t)
4763 			    (state->hs_msix_pba_entries + i);
4764 			ddi_put64(msix_pba_hdl,
4765 			    (uint64_t *)(uintptr_t)addr, data64);
4766 		}
4767 		/* restore MSI-X table */
4768 		for (i = 0; i < get_msix_tbl_size(state->hs_dip);
4769 		    i += PCI_MSIX_VECTOR_SIZE) {
4770 			for (j = 0; j < PCI_MSIX_VECTOR_SIZE; j += 4) {
4771 				char *addr = state->hs_msix_tbl_addr + i + j;
4772 				data32 = *(uint32_t *)(uintptr_t)
4773 				    (state->hs_msix_tbl_entries + i + j);
4774 				ddi_put32(msix_tbl_hdl,
4775 				    (uint32_t *)(uintptr_t)addr, data32);
4776 			}
4777 		}
4778 	}
4779 
4780 	/*
4781 	 * PCI Express Capability - we saved during capability list, and
4782 	 * we'll restore them here.
4783 	 */
4784 	offset = state->hs_pci_cap_offset;
4785 	data32 = state->hs_pci_cap_devctl;
4786 	pci_config_put32(pcihdl, offset + HERMON_PCI_CAP_DEV_OFFS, data32);
4787 	data32 = state->hs_pci_cap_lnkctl;
4788 	pci_config_put32(pcihdl, offset + HERMON_PCI_CAP_LNK_OFFS, data32);
4789 
4790 	/* restore the command register */
4791 	pci_config_put32(pcihdl, 0x04, (state->hs_cfg_data[1] | 0x0006));
4792 
4793 	return (DDI_SUCCESS);
4794 }
4795