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