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  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
27  */
28 /*
29  * Fibre channel Transport Library (fctl)
30  *
31  * Function naming conventions:
32  *		Functions called from ULPs begin with fc_ulp_
33  *		Functions called from FCAs begin with fc_fca_
34  *		Internal functions begin with fctl_
35  *
36  * Fibre channel packet layout:
37  *	  +---------------------+<--------+
38  *	  |			|	  |
39  *	  | ULP Packet private	|	  |
40  *	  |			|	  |
41  *	  +---------------------+	  |
42  *	  |			|---------+
43  *	  |  struct  fc_packet	|---------+
44  *	  |			|	  |
45  *	  +---------------------+<--------+
46  *	  |			|
47  *	  | FCA Packet private	|
48  *	  |			|
49  *	  +---------------------+
50  *
51  * So you  loved  the  ascii  art ?  It's  strongly  desirable	to  cache
52  * allocate the entire packet in one common  place.  So we define a set a
53  * of rules.  In a  contiguous	block of memory,  the top  portion of the
54  * block points to ulp packet  private	area, next follows the	fc_packet
55  * structure used  extensively by all the consumers and what follows this
56  * is the FCA packet private.  Note that given a packet	 structure, it is
57  * possible  to get to the  ULP	 and  FCA  Packet  private  fields  using
58  * ulp_private and fca_private fields (which hold pointers) respectively.
59  *
60  * It should be noted with a grain of salt that ULP Packet  private  size
61  * varies  between two different  ULP types, So this poses a challenge to
62  * compute the correct	size of the whole block on a per port basis.  The
63  * transport  layer  doesn't have a problem in dealing with  FCA   packet
64  * private  sizes as it is the sole  manager of ports  underneath.  Since
65  * it's not a good idea to cache allocate  different  sizes of memory for
66  * different ULPs and have the ability to choose from one of these caches
67  * based on ULP type during every packet  allocation,  the transport some
68  * what	 wisely (?)  hands off this job of cache  allocation  to the ULPs
69  * themselves.
70  *
71  * That means FCAs need to make their  packet  private size  known to the
72  * transport   to  pass	 it  up	 to  the   ULPs.  This	is  done   during
73  * fc_fca_attach().  And the transport passes this size up to ULPs during
74  * fc_ulp_port_attach() of each ULP.
75  *
76  * This	 leaves	 us with  another  possible  question;	How  are  packets
77  * allocated for ELS's started by the transport	 itself ?  Well, the port
78  * driver  during  attach  time, cache	allocates  on a per port basis to
79  * handle ELSs too.
80  */
81 
82 #include <sys/note.h>
83 #include <sys/types.h>
84 #include <sys/varargs.h>
85 #include <sys/param.h>
86 #include <sys/errno.h>
87 #include <sys/uio.h>
88 #include <sys/buf.h>
89 #include <sys/modctl.h>
90 #include <sys/open.h>
91 #include <sys/kmem.h>
92 #include <sys/poll.h>
93 #include <sys/conf.h>
94 #include <sys/cmn_err.h>
95 #include <sys/stat.h>
96 #include <sys/ddi.h>
97 #include <sys/sunddi.h>
98 #include <sys/promif.h>
99 #include <sys/byteorder.h>
100 #include <sys/fibre-channel/fc.h>
101 #include <sys/fibre-channel/impl/fc_ulpif.h>
102 #include <sys/fibre-channel/impl/fc_fcaif.h>
103 #include <sys/fibre-channel/impl/fctl_private.h>
104 #include <sys/fibre-channel/impl/fc_portif.h>
105 
106 /* These are referenced by fp.c!  */
107 int did_table_size = D_ID_HASH_TABLE_SIZE;
108 int pwwn_table_size = PWWN_HASH_TABLE_SIZE;
109 
110 static fc_ulp_module_t	*fctl_ulp_modules;
111 static fc_fca_port_t	*fctl_fca_portlist;
112 static fc_ulp_list_t	*fctl_ulp_list;
113 
114 static char fctl_greeting[] =
115 	"fctl: %s ULP same type (0x%x) as existing module.\n";
116 
117 static char *fctl_undefined = "Undefined";
118 
119 /*
120  * This lock protects the fc_ulp_module_t linked list (i.e. mod_next field)
121  */
122 
123 static krwlock_t fctl_ulp_lock;
124 
125 /*
126  * The fctl_mod_ports_lock protects the mod_ports element in the
127  * fc_ulp_ports_t structure
128  */
129 
130 static krwlock_t fctl_mod_ports_lock;
131 
132 /*
133  * fctl_port_lock protects the linked list of local port structures
134  * (fctl_fca_portlist).	 When walking the list, this lock must be obtained
135  * prior to any local port locks.
136  */
137 
138 static kmutex_t fctl_port_lock;
139 static kmutex_t	fctl_ulp_list_mutex;
140 
141 static fctl_nwwn_list_t		*fctl_nwwn_hash_table;
142 static kmutex_t			fctl_nwwn_hash_mutex;
143 int fctl_nwwn_table_size = NWWN_HASH_TABLE_SIZE;
144 
145 #if	!defined(lint)
146 _NOTE(MUTEX_PROTECTS_DATA(fctl_nwwn_hash_mutex, fctl_nwwn_hash_table))
147 _NOTE(MUTEX_PROTECTS_DATA(fctl_ulp_list_mutex, fctl_ulp_list))
148 _NOTE(RWLOCK_PROTECTS_DATA(fctl_ulp_lock, ulp_module::mod_next))
149 _NOTE(RWLOCK_PROTECTS_DATA(fctl_mod_ports_lock, ulp_module::mod_ports
150     ulp_ports::port_handle))
151 _NOTE(DATA_READABLE_WITHOUT_LOCK(ulp_module::mod_info))
152 _NOTE(MUTEX_PROTECTS_DATA(ulp_ports::port_mutex, ulp_ports::port_statec
153     ulp_ports::port_dstate))
154 #endif /* lint */
155 
156 #define	FCTL_VERSION		"20090729-1.70"
157 #define	FCTL_NAME_VERSION	"SunFC Transport v" FCTL_VERSION
158 
159 char *fctl_version = FCTL_NAME_VERSION;
160 
161 extern struct mod_ops mod_miscops;
162 
163 static struct modlmisc modlmisc = {
164 	&mod_miscops,			/* type of module */
165 	FCTL_NAME_VERSION		/* Module name */
166 };
167 
168 static struct modlinkage modlinkage = {
169 	MODREV_1, (void *)&modlmisc, NULL
170 };
171 
172 static struct bus_ops fctl_fca_busops = {
173 	BUSO_REV,
174 	nullbusmap,			/* bus_map */
175 	NULL,				/* bus_get_intrspec */
176 	NULL,				/* bus_add_intrspec */
177 	NULL,				/* bus_remove_intrspec */
178 	i_ddi_map_fault,		/* bus_map_fault */
179 	NULL,				/* bus_dma_map */
180 	ddi_dma_allochdl,		/* bus_dma_allochdl */
181 	ddi_dma_freehdl,		/* bus_dma_freehdl */
182 	ddi_dma_bindhdl,		/* bus_dma_bindhdl */
183 	ddi_dma_unbindhdl,		/* bus_unbindhdl */
184 	ddi_dma_flush,			/* bus_dma_flush */
185 	ddi_dma_win,			/* bus_dma_win */
186 	ddi_dma_mctl,			/* bus_dma_ctl */
187 	fctl_fca_bus_ctl,		/* bus_ctl */
188 	ddi_bus_prop_op,		/* bus_prop_op */
189 	NULL,				/* bus_get_eventcookie */
190 	NULL,				/* bus_add_eventcall */
191 	NULL,				/* bus_remove_event */
192 	NULL,				/* bus_post_event */
193 	NULL,				/* bus_intr_ctl */
194 	NULL,				/* bus_config */
195 	NULL,				/* bus_unconfig */
196 	NULL,				/* bus_fm_init */
197 	NULL,				/* bus_fm_fini */
198 	NULL,				/* bus_fm_access_enter */
199 	NULL,				/* bus_fm_access_exit */
200 	NULL,				/* bus_power */
201 	NULL
202 };
203 
204 struct kmem_cache *fctl_job_cache;
205 
206 static fc_errmap_t fc_errlist [] = {
207 	{ FC_FAILURE,		"Operation failed"			},
208 	{ FC_SUCCESS,		"Operation success"			},
209 	{ FC_CAP_ERROR,		"Capability error"			},
210 	{ FC_CAP_FOUND,		"Capability found"			},
211 	{ FC_CAP_SETTABLE,	"Capability settable"			},
212 	{ FC_UNBOUND,		"Port not bound"			},
213 	{ FC_NOMEM,		"No memory"				},
214 	{ FC_BADPACKET,		"Bad packet"				},
215 	{ FC_OFFLINE,		"Port offline"				},
216 	{ FC_OLDPORT,		"Old Port"				},
217 	{ FC_NO_MAP,		"No map available"			},
218 	{ FC_TRANSPORT_ERROR,	"Transport error"			},
219 	{ FC_ELS_FREJECT,	"ELS Frejected"				},
220 	{ FC_ELS_PREJECT,	"ELS PRejected"				},
221 	{ FC_ELS_BAD,		"Bad ELS request"			},
222 	{ FC_ELS_MALFORMED,	"Malformed ELS request"			},
223 	{ FC_TOOMANY,		"Too many commands"			},
224 	{ FC_UB_BADTOKEN,	"Bad Unsolicited buffer token"		},
225 	{ FC_UB_ERROR,		"Unsolicited buffer error"		},
226 	{ FC_UB_BUSY,		"Unsolicited buffer busy"		},
227 	{ FC_BADULP,		"Bad ULP"				},
228 	{ FC_BADTYPE,		"Bad Type"				},
229 	{ FC_UNCLAIMED,		"Not Claimed"				},
230 	{ FC_ULP_SAMEMODULE,	"Same ULP Module"			},
231 	{ FC_ULP_SAMETYPE,	"Same ULP Type"				},
232 	{ FC_ABORTED,		"Command Aborted"			},
233 	{ FC_ABORT_FAILED,	"Abort Failed"				},
234 	{ FC_BADEXCHANGE,	"Bad Exchange"				},
235 	{ FC_BADWWN,		"Bad World Wide Name"			},
236 	{ FC_BADDEV,		"Bad Device"				},
237 	{ FC_BADCMD,		"Bad Command"				},
238 	{ FC_BADOBJECT,		"Bad Object"				},
239 	{ FC_BADPORT,		"Bad Port"				},
240 	{ FC_NOTTHISPORT,	"Not on this Port"			},
241 	{ FC_PREJECT,		"Operation Prejected"			},
242 	{ FC_FREJECT,		"Operation Frejected"			},
243 	{ FC_PBUSY,		"Operation Pbusyed"			},
244 	{ FC_FBUSY,		"Operation Fbusyed"			},
245 	{ FC_ALREADY,		"Already done"				},
246 	{ FC_LOGINREQ,		"PLOGI Required"			},
247 	{ FC_RESETFAIL,		"Reset operation failed"		},
248 	{ FC_INVALID_REQUEST,	"Invalid Request"			},
249 	{ FC_OUTOFBOUNDS,	"Out of Bounds"				},
250 	{ FC_TRAN_BUSY,		"Command transport Busy"		},
251 	{ FC_STATEC_BUSY,	"State change Busy"			},
252 	{ FC_DEVICE_BUSY,	"Port driver is working on this device"	}
253 };
254 
255 fc_pkt_reason_t remote_stop_reasons [] = {
256 	{ FC_REASON_ABTS,	"Abort Sequence"	},
257 	{ FC_REASON_ABTX,	"Abort Exchange"	},
258 	{ FC_REASON_INVALID,	NULL			}
259 };
260 
261 fc_pkt_reason_t general_reasons [] = {
262 	{ FC_REASON_HW_ERROR,		"Hardware Error"		},
263 	{ FC_REASON_SEQ_TIMEOUT,	"Sequence Timeout"		},
264 	{ FC_REASON_ABORTED,		"Aborted"			},
265 	{ FC_REASON_ABORT_FAILED,	"Abort Failed"			},
266 	{ FC_REASON_NO_CONNECTION,	"No Connection"			},
267 	{ FC_REASON_XCHG_DROPPED,	"Exchange Dropped"		},
268 	{ FC_REASON_ILLEGAL_FRAME,	"Illegal Frame"			},
269 	{ FC_REASON_ILLEGAL_LENGTH,	"Illegal Length"		},
270 	{ FC_REASON_UNSUPPORTED,	"Unsuported"			},
271 	{ FC_REASON_RX_BUF_TIMEOUT,	"Receive Buffer Timeout"	},
272 	{ FC_REASON_FCAL_OPN_FAIL,	"FC AL Open Failed"		},
273 	{ FC_REASON_OVERRUN,		"Over run"			},
274 	{ FC_REASON_QFULL,		"Queue Full"			},
275 	{ FC_REASON_ILLEGAL_REQ,	"Illegal Request",		},
276 	{ FC_REASON_PKT_BUSY,		"Busy"				},
277 	{ FC_REASON_OFFLINE,		"Offline"			},
278 	{ FC_REASON_BAD_XID,		"Bad Exchange Id"		},
279 	{ FC_REASON_XCHG_BSY,		"Exchange Busy"			},
280 	{ FC_REASON_NOMEM,		"No Memory"			},
281 	{ FC_REASON_BAD_SID,		"Bad S_ID"			},
282 	{ FC_REASON_NO_SEQ_INIT,	"No Sequence Initiative"	},
283 	{ FC_REASON_DIAG_BUSY,		"Diagnostic Busy"		},
284 	{ FC_REASON_DMA_ERROR,		"DMA Error"			},
285 	{ FC_REASON_CRC_ERROR,		"CRC Error"			},
286 	{ FC_REASON_ABORT_TIMEOUT,	"Abort Timeout"			},
287 	{ FC_REASON_FCA_UNIQUE,		"FCA Unique"			},
288 	{ FC_REASON_INVALID,		NULL				}
289 };
290 
291 fc_pkt_reason_t rjt_reasons [] = {
292 	{ FC_REASON_INVALID_D_ID,	"Invalid D_ID"			},
293 	{ FC_REASON_INVALID_S_ID,	"Invalid S_ID"			},
294 	{ FC_REASON_TEMP_UNAVAILABLE,	"Temporarily Unavailable"	},
295 	{ FC_REASON_PERM_UNAVAILABLE,	"Permamnently Unavailable"	},
296 	{ FC_REASON_CLASS_NOT_SUPP,	"Class Not Supported",		},
297 	{ FC_REASON_DELIMTER_USAGE_ERROR,
298 	    "Delimeter Usage Error"		},
299 	{ FC_REASON_TYPE_NOT_SUPP,	"Type Not Supported"		},
300 	{ FC_REASON_INVALID_LINK_CTRL,	"Invalid Link Control"		},
301 	{ FC_REASON_INVALID_R_CTL,	"Invalid R_CTL"			},
302 	{ FC_REASON_INVALID_F_CTL,	"Invalid F_CTL"			},
303 	{ FC_REASON_INVALID_OX_ID,	"Invalid OX_ID"			},
304 	{ FC_REASON_INVALID_RX_ID,	"Invalid RX_ID"			},
305 	{ FC_REASON_INVALID_SEQ_ID,	"Invalid Sequence ID"		},
306 	{ FC_REASON_INVALID_DF_CTL,	"Invalid DF_CTL"		},
307 	{ FC_REASON_INVALID_SEQ_CNT,	"Invalid Sequence count"	},
308 	{ FC_REASON_INVALID_PARAM,	"Invalid Parameter"		},
309 	{ FC_REASON_EXCH_ERROR,		"Exchange Error"		},
310 	{ FC_REASON_PROTOCOL_ERROR,	"Protocol Error"		},
311 	{ FC_REASON_INCORRECT_LENGTH,	"Incorrect Length"		},
312 	{ FC_REASON_UNEXPECTED_ACK,	"Unexpected Ack"		},
313 	{ FC_REASON_UNEXPECTED_LR,	"Unexpected Link reset"		},
314 	{ FC_REASON_LOGIN_REQUIRED,	"Login Required"		},
315 	{ FC_REASON_EXCESSIVE_SEQS,	"Excessive Sequences"
316 	    " Attempted"			},
317 	{ FC_REASON_EXCH_UNABLE,	"Exchange incapable"		},
318 	{ FC_REASON_ESH_NOT_SUPP,	"Expiration Security Header "
319 	    "Not Supported"			},
320 	{ FC_REASON_NO_FABRIC_PATH,	"No Fabric Path"		},
321 	{ FC_REASON_VENDOR_UNIQUE,	"Vendor Unique"			},
322 	{ FC_REASON_INVALID,		NULL				}
323 };
324 
325 fc_pkt_reason_t n_port_busy_reasons [] = {
326 	{ FC_REASON_PHYSICAL_BUSY,		"Physical Busy"		},
327 	{ FC_REASON_N_PORT_RESOURCE_BSY,	"Resource Busy"		},
328 	{ FC_REASON_N_PORT_VENDOR_UNIQUE,	"Vendor Unique"		},
329 	{ FC_REASON_INVALID,			NULL			}
330 };
331 
332 fc_pkt_reason_t f_busy_reasons [] = {
333 	{ FC_REASON_FABRIC_BSY,		"Fabric Busy"			},
334 	{ FC_REASON_N_PORT_BSY,		"N_Port Busy"			},
335 	{ FC_REASON_INVALID,		NULL				}
336 };
337 
338 fc_pkt_reason_t ls_ba_rjt_reasons [] = {
339 	{ FC_REASON_INVALID_LA_CODE,	"Invalid Link Application Code"	},
340 	{ FC_REASON_LOGICAL_ERROR,	"Logical Error"			},
341 	{ FC_REASON_LOGICAL_BSY,	"Logical Busy"			},
342 	{ FC_REASON_PROTOCOL_ERROR_RJT,	"Protocol Error Reject"		},
343 	{ FC_REASON_CMD_UNABLE,		"Unable to Perform Command"	},
344 	{ FC_REASON_CMD_UNSUPPORTED,	"Unsupported Command"		},
345 	{ FC_REASON_VU_RJT,		"Vendor Unique"			},
346 	{ FC_REASON_INVALID,		NULL				}
347 };
348 
349 fc_pkt_reason_t fs_rjt_reasons [] = {
350 	{ FC_REASON_FS_INVALID_CMD,	"Invalid Command"		},
351 	{ FC_REASON_FS_INVALID_VER,	"Invalid Version"		},
352 	{ FC_REASON_FS_LOGICAL_ERR,	"Logical Error"			},
353 	{ FC_REASON_FS_INVALID_IUSIZE,	"Invalid IU Size"		},
354 	{ FC_REASON_FS_LOGICAL_BUSY,	"Logical Busy"			},
355 	{ FC_REASON_FS_PROTOCOL_ERR,	"Protocol Error"		},
356 	{ FC_REASON_FS_CMD_UNABLE,	"Unable to Perform Command"	},
357 	{ FC_REASON_FS_CMD_UNSUPPORTED,	"Unsupported Command"		},
358 	{ FC_REASON_FS_VENDOR_UNIQUE,	"Vendor Unique"			},
359 	{ FC_REASON_INVALID,		NULL				}
360 };
361 
362 fc_pkt_action_t	n_port_busy_actions [] = {
363 	{ FC_ACTION_SEQ_TERM_RETRY,	"Retry terminated Sequence"	},
364 	{ FC_ACTION_SEQ_ACTIVE_RETRY,	"Retry Active Sequence"		},
365 	{ FC_REASON_INVALID,		NULL				}
366 };
367 
368 fc_pkt_action_t rjt_timeout_actions [] = {
369 	{ FC_ACTION_RETRYABLE,		"Retryable"			},
370 	{ FC_ACTION_NON_RETRYABLE,	"Non Retryable"			},
371 	{ FC_REASON_INVALID,		NULL				}
372 };
373 
374 fc_pkt_expln_t ba_rjt_explns [] = {
375 	{ FC_EXPLN_NONE,		"No Explanation"		},
376 	{ FC_EXPLN_INVALID_OX_RX_ID,	"Invalid X_ID"			},
377 	{ FC_EXPLN_SEQ_ABORTED,		"Sequence Aborted"		},
378 	{ FC_EXPLN_INVALID,		NULL				}
379 };
380 
381 fc_pkt_error_t fc_pkt_errlist[] = {
382 	{
383 		FC_PKT_SUCCESS,
384 		"Operation Success",
385 		NULL,
386 		NULL,
387 		NULL
388 	},
389 	{	FC_PKT_REMOTE_STOP,
390 	    "Remote Stop",
391 	    remote_stop_reasons,
392 	    NULL,
393 	    NULL
394 	},
395 	{
396 		FC_PKT_LOCAL_RJT,
397 		"Local Reject",
398 		general_reasons,
399 		rjt_timeout_actions,
400 		NULL
401 	},
402 	{
403 		FC_PKT_NPORT_RJT,
404 		"N_Port Reject",
405 		rjt_reasons,
406 		rjt_timeout_actions,
407 		NULL
408 	},
409 	{
410 		FC_PKT_FABRIC_RJT,
411 		"Fabric Reject",
412 		rjt_reasons,
413 		rjt_timeout_actions,
414 		NULL
415 	},
416 	{
417 		FC_PKT_LOCAL_BSY,
418 		"Local Busy",
419 		general_reasons,
420 		NULL,
421 		NULL,
422 	},
423 	{
424 		FC_PKT_TRAN_BSY,
425 		"Transport Busy",
426 		general_reasons,
427 		NULL,
428 		NULL,
429 	},
430 	{
431 		FC_PKT_NPORT_BSY,
432 		"N_Port Busy",
433 		n_port_busy_reasons,
434 		n_port_busy_actions,
435 		NULL
436 	},
437 	{
438 		FC_PKT_FABRIC_BSY,
439 		"Fabric Busy",
440 		f_busy_reasons,
441 		NULL,
442 		NULL,
443 	},
444 	{
445 		FC_PKT_LS_RJT,
446 		"Link Service Reject",
447 		ls_ba_rjt_reasons,
448 		NULL,
449 		NULL,
450 	},
451 	{
452 		FC_PKT_BA_RJT,
453 		"Basic Reject",
454 		ls_ba_rjt_reasons,
455 		NULL,
456 		ba_rjt_explns,
457 	},
458 	{
459 		FC_PKT_TIMEOUT,
460 		"Timeout",
461 		general_reasons,
462 		rjt_timeout_actions,
463 		NULL
464 	},
465 	{
466 		FC_PKT_FS_RJT,
467 		"Fabric Switch Reject",
468 		fs_rjt_reasons,
469 		NULL,
470 		NULL
471 	},
472 	{
473 		FC_PKT_TRAN_ERROR,
474 		"Packet Transport error",
475 		general_reasons,
476 		NULL,
477 		NULL
478 	},
479 	{
480 		FC_PKT_FAILURE,
481 		"Packet Failure",
482 		general_reasons,
483 		NULL,
484 		NULL
485 	},
486 	{
487 		FC_PKT_PORT_OFFLINE,
488 		"Port Offline",
489 		NULL,
490 		NULL,
491 		NULL
492 	},
493 	{
494 		FC_PKT_ELS_IN_PROGRESS,
495 		"ELS is in Progress",
496 		NULL,
497 		NULL,
498 		NULL
499 	}
500 };
501 
502 int
503 _init()
504 {
505 	int rval;
506 
507 	rw_init(&fctl_ulp_lock, NULL, RW_DRIVER, NULL);
508 	rw_init(&fctl_mod_ports_lock, NULL, RW_DRIVER, NULL);
509 	mutex_init(&fctl_port_lock, NULL, MUTEX_DRIVER, NULL);
510 	mutex_init(&fctl_nwwn_hash_mutex, NULL, MUTEX_DRIVER, NULL);
511 
512 	fctl_nwwn_hash_table = kmem_zalloc(sizeof (*fctl_nwwn_hash_table) *
513 	    fctl_nwwn_table_size, KM_SLEEP);
514 
515 	fctl_ulp_modules = NULL;
516 	fctl_fca_portlist = NULL;
517 
518 	fctl_job_cache = kmem_cache_create("fctl_cache",
519 	    sizeof (job_request_t), 8, fctl_cache_constructor,
520 	    fctl_cache_destructor, NULL, NULL, NULL, 0);
521 
522 	if (fctl_job_cache == NULL) {
523 		kmem_free(fctl_nwwn_hash_table,
524 		    sizeof (*fctl_nwwn_hash_table) * fctl_nwwn_table_size);
525 		mutex_destroy(&fctl_nwwn_hash_mutex);
526 		mutex_destroy(&fctl_port_lock);
527 		rw_destroy(&fctl_ulp_lock);
528 		rw_destroy(&fctl_mod_ports_lock);
529 		return (ENOMEM);
530 	}
531 
532 	if ((rval = mod_install(&modlinkage)) != 0) {
533 		kmem_cache_destroy(fctl_job_cache);
534 		kmem_free(fctl_nwwn_hash_table,
535 		    sizeof (*fctl_nwwn_hash_table) * fctl_nwwn_table_size);
536 		mutex_destroy(&fctl_nwwn_hash_mutex);
537 		mutex_destroy(&fctl_port_lock);
538 		rw_destroy(&fctl_ulp_lock);
539 		rw_destroy(&fctl_mod_ports_lock);
540 	}
541 
542 	return (rval);
543 }
544 
545 
546 /*
547  * The mod_uninstall code doesn't call _fini when
548  * there is living dependent module on fctl. So
549  * there is no need to be extra careful here ?
550  */
551 int
552 _fini()
553 {
554 	int rval;
555 
556 	if ((rval = mod_remove(&modlinkage)) != 0) {
557 		return (rval);
558 	}
559 
560 	kmem_cache_destroy(fctl_job_cache);
561 	kmem_free(fctl_nwwn_hash_table,
562 	    sizeof (*fctl_nwwn_hash_table) * fctl_nwwn_table_size);
563 	mutex_destroy(&fctl_nwwn_hash_mutex);
564 	mutex_destroy(&fctl_port_lock);
565 	rw_destroy(&fctl_ulp_lock);
566 	rw_destroy(&fctl_mod_ports_lock);
567 
568 	return (rval);
569 }
570 
571 
572 int
573 _info(struct modinfo *modinfo_p)
574 {
575 	return (mod_info(&modlinkage, modinfo_p));
576 }
577 
578 
579 /* ARGSUSED */
580 static int
581 fctl_cache_constructor(void *buf, void *cdarg, int kmflag)
582 {
583 	job_request_t *job = (job_request_t *)buf;
584 
585 	mutex_init(&job->job_mutex, NULL, MUTEX_DRIVER, NULL);
586 	sema_init(&job->job_fctl_sema, 0, NULL, SEMA_DEFAULT, NULL);
587 	sema_init(&job->job_port_sema, 0, NULL, SEMA_DEFAULT, NULL);
588 
589 	return (0);
590 }
591 
592 
593 /* ARGSUSED */
594 static void
595 fctl_cache_destructor(void *buf, void *cdarg)
596 {
597 	job_request_t *job = (job_request_t *)buf;
598 
599 	sema_destroy(&job->job_fctl_sema);
600 	sema_destroy(&job->job_port_sema);
601 	mutex_destroy(&job->job_mutex);
602 }
603 
604 
605 /*
606  * fc_ulp_add:
607  *		Add a ULP module
608  *
609  * Return Codes:
610  *		FC_ULP_SAMEMODULE
611  *		FC_SUCCESS
612  *		FC_FAILURE
613  *
614  *   fc_ulp_add	 prints	 a warning message if there is	already a
615  *   similar ULP type  attached and this is unlikely to change as
616  *   we trudge along.  Further, this  function	returns a failure
617  *   code if the same  module  attempts to add more than once for
618  *   the same FC-4 type.
619  */
620 int
621 fc_ulp_add(fc_ulp_modinfo_t *ulp_info)
622 {
623 	fc_ulp_module_t *mod;
624 	fc_ulp_module_t *prev;
625 	job_request_t	*job;
626 	fc_ulp_list_t	*new;
627 	fc_fca_port_t	*fca_port;
628 	int		ntry = 0;
629 
630 	ASSERT(ulp_info != NULL);
631 
632 	/*
633 	 * Make sure ulp_rev matches fctl version.
634 	 * Whenever non-private data structure or non-static interface changes,
635 	 * we should use an increased FCTL_ULP_MODREV_# number here and in all
636 	 * ulps to prevent version mismatch.
637 	 */
638 	if (ulp_info->ulp_rev != FCTL_ULP_MODREV_4) {
639 		cmn_err(CE_WARN, "fctl: ULP %s version mismatch;"
640 		    " ULP %s would not be loaded", ulp_info->ulp_name,
641 		    ulp_info->ulp_name);
642 		return (FC_BADULP);
643 	}
644 
645 	new = kmem_zalloc(sizeof (*new), KM_SLEEP);
646 	ASSERT(new != NULL);
647 
648 	mutex_enter(&fctl_ulp_list_mutex);
649 	new->ulp_info = ulp_info;
650 	if (fctl_ulp_list != NULL) {
651 		new->ulp_next = fctl_ulp_list;
652 	}
653 	fctl_ulp_list = new;
654 	mutex_exit(&fctl_ulp_list_mutex);
655 
656 	while (rw_tryenter(&fctl_ulp_lock, RW_WRITER) == 0) {
657 		delay(drv_usectohz(1000000));
658 		if (ntry++ > FC_ULP_ADD_RETRY_COUNT) {
659 			fc_ulp_list_t	*list;
660 			fc_ulp_list_t	*last;
661 			mutex_enter(&fctl_ulp_list_mutex);
662 			for (last = NULL, list = fctl_ulp_list; list != NULL;
663 			    list = list->ulp_next) {
664 				if (list->ulp_info == ulp_info) {
665 					break;
666 				}
667 				last = list;
668 			}
669 
670 			if (list) {
671 				if (last) {
672 					last->ulp_next = list->ulp_next;
673 				} else {
674 					fctl_ulp_list = list->ulp_next;
675 				}
676 				kmem_free(list, sizeof (*list));
677 			}
678 			mutex_exit(&fctl_ulp_list_mutex);
679 			cmn_err(CE_WARN, "fctl: ULP %s unable to load",
680 			    ulp_info->ulp_name);
681 			return (FC_FAILURE);
682 		}
683 	}
684 
685 	for (mod = fctl_ulp_modules, prev = NULL; mod; mod = mod->mod_next) {
686 		ASSERT(mod->mod_info != NULL);
687 
688 		if (ulp_info == mod->mod_info &&
689 		    ulp_info->ulp_type == mod->mod_info->ulp_type) {
690 			rw_exit(&fctl_ulp_lock);
691 			return (FC_ULP_SAMEMODULE);
692 		}
693 
694 		if (ulp_info->ulp_type == mod->mod_info->ulp_type) {
695 			cmn_err(CE_NOTE, fctl_greeting, ulp_info->ulp_name,
696 			    ulp_info->ulp_type);
697 		}
698 		prev = mod;
699 	}
700 
701 	mod = kmem_zalloc(sizeof (*mod), KM_SLEEP);
702 	mod->mod_info = ulp_info;
703 	mod->mod_next = NULL;
704 
705 	if (prev) {
706 		prev->mod_next = mod;
707 	} else {
708 		fctl_ulp_modules = mod;
709 	}
710 
711 	/*
712 	 * Schedule a job to each port's job_handler
713 	 * thread to attach their ports with this ULP.
714 	 */
715 	mutex_enter(&fctl_port_lock);
716 	for (fca_port = fctl_fca_portlist; fca_port != NULL;
717 	    fca_port = fca_port->port_next) {
718 		job = fctl_alloc_job(JOB_ATTACH_ULP, JOB_TYPE_FCTL_ASYNC,
719 		    NULL, NULL, KM_SLEEP);
720 
721 		fctl_enque_job(fca_port->port_handle, job);
722 	}
723 	mutex_exit(&fctl_port_lock);
724 
725 	rw_exit(&fctl_ulp_lock);
726 
727 	return (FC_SUCCESS);
728 }
729 
730 
731 /*
732  * fc_ulp_remove
733  *	Remove a ULP module
734  *
735  * A misbehaving ULP may call this routine while I/Os are in progress.
736  * Currently there is no mechanism to detect it to fail such a request.
737  *
738  * Return Codes:
739  *		FC_SUCCESS
740  *		FC_FAILURE
741  */
742 int
743 fc_ulp_remove(fc_ulp_modinfo_t *ulp_info)
744 {
745 	fc_ulp_module_t *mod;
746 	fc_ulp_list_t	*list;
747 	fc_ulp_list_t	*last;
748 	fc_ulp_module_t *prev;
749 
750 	mutex_enter(&fctl_ulp_list_mutex);
751 
752 	for (last = NULL, list = fctl_ulp_list; list != NULL;
753 	    list = list->ulp_next) {
754 		if (list->ulp_info == ulp_info) {
755 			break;
756 		}
757 		last = list;
758 	}
759 
760 	if (list) {
761 		if (last) {
762 			last->ulp_next = list->ulp_next;
763 		} else {
764 			fctl_ulp_list = list->ulp_next;
765 		}
766 		kmem_free(list, sizeof (*list));
767 	}
768 
769 	mutex_exit(&fctl_ulp_list_mutex);
770 
771 	rw_enter(&fctl_ulp_lock, RW_WRITER);
772 
773 	for (mod = fctl_ulp_modules, prev = NULL; mod != NULL;
774 	    mod = mod->mod_next) {
775 		if (mod->mod_info == ulp_info) {
776 			break;
777 		}
778 		prev = mod;
779 	}
780 
781 	if (mod) {
782 		fc_ulp_ports_t *next;
783 
784 		if (prev) {
785 			prev->mod_next = mod->mod_next;
786 		} else {
787 			fctl_ulp_modules = mod->mod_next;
788 		}
789 
790 		rw_enter(&fctl_mod_ports_lock, RW_WRITER);
791 
792 		while ((next = mod->mod_ports) != NULL) {
793 			mod->mod_ports = next->port_next;
794 			fctl_dealloc_ulp_port(next);
795 		}
796 
797 		rw_exit(&fctl_mod_ports_lock);
798 		rw_exit(&fctl_ulp_lock);
799 
800 		kmem_free(mod, sizeof (*mod));
801 
802 		return (FC_SUCCESS);
803 	}
804 	rw_exit(&fctl_ulp_lock);
805 
806 	return (FC_FAILURE);
807 }
808 
809 
810 /*
811  * The callers typically cache allocate the packet, complete the
812  * DMA setup for pkt_cmd and pkt_resp fields of the packet and
813  * call this function to see if the FCA is interested in doing
814  * its own intialization. For example, socal may like to initialize
815  * the soc_hdr which is pointed to by the pkt_fca_private field
816  * and sitting right below fc_packet_t in memory.
817  *
818  * The caller is required to ensure that pkt_pd is populated with the
819  * handle that it was given when the transport notified it about the
820  * device this packet is associated with.  If there is no associated
821  * device, pkt_pd must be set to NULL.	A non-NULL pkt_pd will cause an
822  * increment of the reference count for said pd.  When the packet is freed,
823  * the reference count will be decremented.  This reference count, in
824  * combination with the PD_GIVEN_TO_ULPS flag guarantees that the pd
825  * will not wink out of existence while there is a packet outstanding.
826  *
827  * This function and fca_init_pkt must not perform any operations that
828  * would result in a call back to the ULP, as the ULP may be required
829  * to hold a mutex across this call to ensure that the pd in question
830  * won't go away prior the call to fc_ulp_transport.
831  *
832  * ULPs are responsible for using the handles they are given during state
833  * change callback processing in a manner that ensures consistency.  That
834  * is, they must be aware that they could be processing a state change
835  * notification that tells them the device associated with a particular
836  * handle has gone away at the same time they are being asked to
837  * initialize a packet using that handle. ULPs must therefore ensure
838  * that their state change processing and packet initialization code
839  * paths are sufficiently synchronized to avoid the use of an
840  * invalidated handle in any fc_packet_t struct that is passed to the
841  * fc_ulp_init_packet() function.
842  */
843 int
844 fc_ulp_init_packet(opaque_t port_handle, fc_packet_t *pkt, int sleep)
845 {
846 	int rval;
847 	fc_local_port_t *port = port_handle;
848 	fc_remote_port_t *pd;
849 
850 	ASSERT(pkt != NULL);
851 
852 	pd = pkt->pkt_pd;
853 
854 	/* Call the FCA driver's fca_init_pkt entry point function. */
855 	rval = port->fp_fca_tran->fca_init_pkt(port->fp_fca_handle, pkt, sleep);
856 
857 	if ((rval == FC_SUCCESS) && (pd != NULL)) {
858 		/*
859 		 * A !NULL pd here must still be a valid
860 		 * reference to the fc_remote_port_t.
861 		 */
862 		mutex_enter(&pd->pd_mutex);
863 		ASSERT(pd->pd_ref_count >= 0);
864 		pd->pd_ref_count++;
865 		mutex_exit(&pd->pd_mutex);
866 	}
867 
868 	return (rval);
869 }
870 
871 
872 /*
873  * This function is called before destroying the cache allocated
874  * fc_packet to free up (and uninitialize) any resource specially
875  * allocated by the FCA driver during tran_init_pkt().
876  *
877  * If the pkt_pd field in the given fc_packet_t struct is not NULL, then
878  * the pd_ref_count reference count is decremented for the indicated
879  * fc_remote_port_t struct.
880  */
881 int
882 fc_ulp_uninit_packet(opaque_t port_handle, fc_packet_t *pkt)
883 {
884 	int rval;
885 	fc_local_port_t *port = port_handle;
886 	fc_remote_port_t *pd;
887 
888 	ASSERT(pkt != NULL);
889 
890 	pd = pkt->pkt_pd;
891 
892 	/* Call the FCA driver's fca_un_init_pkt entry point function */
893 	rval = port->fp_fca_tran->fca_un_init_pkt(port->fp_fca_handle, pkt);
894 
895 	if ((rval == FC_SUCCESS) && (pd != NULL)) {
896 		mutex_enter(&pd->pd_mutex);
897 
898 		ASSERT(pd->pd_ref_count > 0);
899 		pd->pd_ref_count--;
900 
901 		/*
902 		 * If at this point the state of this fc_remote_port_t
903 		 * struct is PORT_DEVICE_INVALID, it probably means somebody
904 		 * is cleaning up old (e.g. retried) packets. If the
905 		 * pd_ref_count has also dropped to zero, it's time to
906 		 * deallocate this fc_remote_port_t struct.
907 		 */
908 		if (pd->pd_state == PORT_DEVICE_INVALID &&
909 		    pd->pd_ref_count == 0) {
910 			fc_remote_node_t *node = pd->pd_remote_nodep;
911 
912 			mutex_exit(&pd->pd_mutex);
913 
914 			/*
915 			 * Also deallocate the associated fc_remote_node_t
916 			 * struct if it has no other associated
917 			 * fc_remote_port_t structs.
918 			 */
919 			if ((fctl_destroy_remote_port(port, pd) == 0) &&
920 			    (node != NULL)) {
921 				fctl_destroy_remote_node(node);
922 			}
923 			return (rval);
924 		}
925 
926 		mutex_exit(&pd->pd_mutex);
927 	}
928 
929 	return (rval);
930 }
931 
932 
933 int
934 fc_ulp_getportmap(opaque_t port_handle, fc_portmap_t **map, uint32_t *len,
935     int flag)
936 {
937 	int		job_code;
938 	fc_local_port_t *port;
939 	job_request_t	*job;
940 	fc_portmap_t	*tmp_map;
941 	uint32_t	tmp_len;
942 	fc_portmap_t	*change_list = NULL;
943 	uint32_t	listlen = 0;
944 
945 	port = port_handle;
946 
947 	mutex_enter(&port->fp_mutex);
948 	if (port->fp_statec_busy) {
949 		mutex_exit(&port->fp_mutex);
950 		return (FC_STATEC_BUSY);
951 	}
952 
953 	if (FC_PORT_STATE_MASK(port->fp_state) == FC_STATE_OFFLINE) {
954 		mutex_exit(&port->fp_mutex);
955 		return (FC_OFFLINE);
956 	}
957 
958 	if (port->fp_dev_count && (port->fp_dev_count ==
959 	    port->fp_total_devices)) {
960 		mutex_exit(&port->fp_mutex);
961 		fctl_fillout_map(port, &change_list, &listlen, 1, 1, 0);
962 		if (listlen > *len) {
963 			tmp_map = (fc_portmap_t *)kmem_zalloc(
964 			    listlen * sizeof (fc_portmap_t), KM_NOSLEEP);
965 			if (tmp_map == NULL) {
966 				return (FC_NOMEM);
967 			}
968 			if (*map) {
969 				kmem_free(*map, (*len) * sizeof (fc_portmap_t));
970 			}
971 			*map = tmp_map;
972 		}
973 		if (change_list) {
974 			bcopy(change_list, *map,
975 			    listlen * sizeof (fc_portmap_t));
976 			kmem_free(change_list, listlen * sizeof (fc_portmap_t));
977 		}
978 		*len = listlen;
979 	} else {
980 		mutex_exit(&port->fp_mutex);
981 
982 		switch (flag) {
983 		case FC_ULP_PLOGI_DONTCARE:
984 			job_code = JOB_PORT_GETMAP;
985 			break;
986 
987 		case FC_ULP_PLOGI_PRESERVE:
988 			job_code = JOB_PORT_GETMAP_PLOGI_ALL;
989 			break;
990 
991 		default:
992 			return (FC_INVALID_REQUEST);
993 		}
994 		/*
995 		 * Submit a job request to the job handler
996 		 * thread to get the map and wait
997 		 */
998 		job = fctl_alloc_job(job_code, 0, NULL, NULL, KM_SLEEP);
999 		job->job_private = (opaque_t)map;
1000 		job->job_arg = (opaque_t)len;
1001 		fctl_enque_job(port, job);
1002 
1003 		fctl_jobwait(job);
1004 		/*
1005 		 * The result of the last I/O operation is
1006 		 * in job_code. We don't care to look at it
1007 		 * Rather we look at the number of devices
1008 		 * that are found to fill out the map for
1009 		 * ULPs.
1010 		 */
1011 		fctl_dealloc_job(job);
1012 	}
1013 
1014 	/*
1015 	 * If we're here, we're returning a map to the caller, which means
1016 	 * we'd better make sure every pd in that map has the
1017 	 * PD_GIVEN_TO_ULPS flag set.
1018 	 */
1019 
1020 	tmp_len = *len;
1021 	tmp_map = *map;
1022 
1023 	while (tmp_len-- != 0) {
1024 		if (tmp_map->map_state != PORT_DEVICE_INVALID) {
1025 			fc_remote_port_t *pd =
1026 			    (fc_remote_port_t *)tmp_map->map_pd;
1027 			mutex_enter(&pd->pd_mutex);
1028 			pd->pd_aux_flags |= PD_GIVEN_TO_ULPS;
1029 			mutex_exit(&pd->pd_mutex);
1030 		}
1031 		tmp_map++;
1032 	}
1033 
1034 	return (FC_SUCCESS);
1035 }
1036 
1037 
1038 int
1039 fc_ulp_login(opaque_t port_handle, fc_packet_t **ulp_pkt, uint32_t listlen)
1040 {
1041 	int			rval = FC_SUCCESS;
1042 	int			job_flags;
1043 	uint32_t		count;
1044 	fc_packet_t		**tmp_array;
1045 	job_request_t		*job;
1046 	fc_local_port_t		*port = port_handle;
1047 	fc_ulp_rscn_info_t	*rscnp =
1048 	    (fc_ulp_rscn_info_t *)(ulp_pkt[0])->pkt_ulp_rscn_infop;
1049 
1050 	/*
1051 	 * If the port is OFFLINE, or if the port driver is
1052 	 * being SUSPENDED/PM_SUSPENDED/DETACHED, block all
1053 	 * PLOGI operations
1054 	 */
1055 	mutex_enter(&port->fp_mutex);
1056 	if (port->fp_statec_busy) {
1057 		mutex_exit(&port->fp_mutex);
1058 		return (FC_STATEC_BUSY);
1059 	}
1060 
1061 	if ((FC_PORT_STATE_MASK(port->fp_state) == FC_STATE_OFFLINE) ||
1062 	    (port->fp_soft_state &
1063 	    (FP_SOFT_IN_DETACH | FP_SOFT_SUSPEND | FP_SOFT_POWER_DOWN))) {
1064 		mutex_exit(&port->fp_mutex);
1065 		return (FC_OFFLINE);
1066 	}
1067 
1068 	/*
1069 	 * If the rscn count in the packet is not the same as the rscn count
1070 	 * in the fc_local_port_t, then one or more new RSCNs has occurred.
1071 	 */
1072 	if ((rscnp != NULL) &&
1073 	    (rscnp->ulp_rscn_count != FC_INVALID_RSCN_COUNT) &&
1074 	    (rscnp->ulp_rscn_count != port->fp_rscn_count)) {
1075 		mutex_exit(&port->fp_mutex);
1076 		return (FC_DEVICE_BUSY_NEW_RSCN);
1077 	}
1078 
1079 	mutex_exit(&port->fp_mutex);
1080 
1081 	tmp_array = kmem_zalloc(sizeof (*tmp_array) * listlen, KM_SLEEP);
1082 	for (count = 0; count < listlen; count++) {
1083 		tmp_array[count] = ulp_pkt[count];
1084 	}
1085 
1086 	job_flags = ((ulp_pkt[0]->pkt_tran_flags) & FC_TRAN_NO_INTR)
1087 	    ? 0 : JOB_TYPE_FCTL_ASYNC;
1088 
1089 #ifdef	DEBUG
1090 	{
1091 		int next;
1092 		int count;
1093 		int polled;
1094 
1095 		polled = ((ulp_pkt[0]->pkt_tran_flags) &
1096 		    FC_TRAN_NO_INTR) ? 0 : JOB_TYPE_FCTL_ASYNC;
1097 
1098 		for (count = 0; count < listlen; count++) {
1099 			next = ((ulp_pkt[count]->pkt_tran_flags)
1100 			    & FC_TRAN_NO_INTR) ? 0 : JOB_TYPE_FCTL_ASYNC;
1101 			ASSERT(next == polled);
1102 		}
1103 	}
1104 #endif
1105 
1106 	job = fctl_alloc_job(JOB_PLOGI_GROUP, job_flags, NULL, NULL, KM_SLEEP);
1107 	job->job_ulp_pkts = tmp_array;
1108 	job->job_ulp_listlen = listlen;
1109 
1110 	while (listlen--) {
1111 		fc_packet_t *pkt;
1112 
1113 		pkt = tmp_array[listlen];
1114 		if (pkt->pkt_pd == NULL) {
1115 			pkt->pkt_state = FC_PKT_SUCCESS;
1116 			continue;
1117 		}
1118 
1119 		mutex_enter(&pkt->pkt_pd->pd_mutex);
1120 		if (pkt->pkt_pd->pd_flags == PD_ELS_IN_PROGRESS ||
1121 		    pkt->pkt_pd->pd_flags == PD_ELS_MARK) {
1122 			/*
1123 			 * Set the packet state and let the port
1124 			 * driver call the completion routine
1125 			 * from its thread
1126 			 */
1127 			mutex_exit(&pkt->pkt_pd->pd_mutex);
1128 			pkt->pkt_state = FC_PKT_ELS_IN_PROGRESS;
1129 			continue;
1130 		}
1131 
1132 		if (pkt->pkt_pd->pd_state == PORT_DEVICE_INVALID ||
1133 		    pkt->pkt_pd->pd_type == PORT_DEVICE_OLD) {
1134 			mutex_exit(&pkt->pkt_pd->pd_mutex);
1135 			pkt->pkt_state = FC_PKT_LOCAL_RJT;
1136 			continue;
1137 		}
1138 		mutex_exit(&pkt->pkt_pd->pd_mutex);
1139 		pkt->pkt_state = FC_PKT_SUCCESS;
1140 	}
1141 
1142 	fctl_enque_job(port, job);
1143 
1144 	if (!(job_flags & JOB_TYPE_FCTL_ASYNC)) {
1145 		fctl_jobwait(job);
1146 		rval = job->job_result;
1147 		fctl_dealloc_job(job);
1148 	}
1149 
1150 	return (rval);
1151 }
1152 
1153 
1154 opaque_t
1155 fc_ulp_get_remote_port(opaque_t port_handle, la_wwn_t *pwwn, int *error,
1156     int create)
1157 {
1158 	fc_local_port_t		*port;
1159 	job_request_t		*job;
1160 	fc_remote_port_t	*pd;
1161 
1162 	port = port_handle;
1163 	pd = fctl_get_remote_port_by_pwwn(port, pwwn);
1164 
1165 	if (pd != NULL) {
1166 		*error = FC_SUCCESS;
1167 		/*
1168 		 * A ULP now knows about this pd, so mark it
1169 		 */
1170 		mutex_enter(&pd->pd_mutex);
1171 		pd->pd_aux_flags |= PD_GIVEN_TO_ULPS;
1172 		mutex_exit(&pd->pd_mutex);
1173 		return (pd);
1174 	}
1175 
1176 	mutex_enter(&port->fp_mutex);
1177 	if (FC_IS_TOP_SWITCH(port->fp_topology) && create) {
1178 		uint32_t	d_id;
1179 		fctl_ns_req_t	*ns_cmd;
1180 
1181 		mutex_exit(&port->fp_mutex);
1182 
1183 		job = fctl_alloc_job(JOB_NS_CMD, 0, NULL, NULL, KM_SLEEP);
1184 
1185 		if (job == NULL) {
1186 			*error = FC_NOMEM;
1187 			return (pd);
1188 		}
1189 
1190 		ns_cmd = fctl_alloc_ns_cmd(sizeof (ns_req_gid_pn_t),
1191 		    sizeof (ns_resp_gid_pn_t), sizeof (ns_resp_gid_pn_t),
1192 		    0, KM_SLEEP);
1193 
1194 		if (ns_cmd == NULL) {
1195 			fctl_dealloc_job(job);
1196 			*error = FC_NOMEM;
1197 			return (pd);
1198 		}
1199 		ns_cmd->ns_cmd_code = NS_GID_PN;
1200 		((ns_req_gid_pn_t *)(ns_cmd->ns_cmd_buf))->pwwn = *pwwn;
1201 
1202 		job->job_result = FC_SUCCESS;
1203 		job->job_private = (void *)ns_cmd;
1204 		job->job_counter = 1;
1205 		fctl_enque_job(port, job);
1206 		fctl_jobwait(job);
1207 
1208 		if (job->job_result != FC_SUCCESS) {
1209 			*error = job->job_result;
1210 			fctl_free_ns_cmd(ns_cmd);
1211 			fctl_dealloc_job(job);
1212 			return (pd);
1213 		}
1214 		d_id = ((ns_resp_gid_pn_t *)ns_cmd->ns_data_buf)->pid.port_id;
1215 		fctl_free_ns_cmd(ns_cmd);
1216 
1217 		ns_cmd = fctl_alloc_ns_cmd(sizeof (ns_req_gan_t),
1218 		    sizeof (ns_resp_gan_t), 0, FCTL_NS_CREATE_DEVICE,
1219 		    KM_SLEEP);
1220 		ASSERT(ns_cmd != NULL);
1221 
1222 		ns_cmd->ns_gan_max = 1;
1223 		ns_cmd->ns_cmd_code = NS_GA_NXT;
1224 		ns_cmd->ns_gan_sid = FCTL_GAN_START_ID;
1225 		((ns_req_gan_t *)(ns_cmd->ns_cmd_buf))->pid.port_id = d_id - 1;
1226 		((ns_req_gan_t *)(ns_cmd->ns_cmd_buf))->pid.priv_lilp_posit = 0;
1227 
1228 		job->job_result = FC_SUCCESS;
1229 		job->job_private = (void *)ns_cmd;
1230 		job->job_counter = 1;
1231 		fctl_enque_job(port, job);
1232 		fctl_jobwait(job);
1233 
1234 		fctl_free_ns_cmd(ns_cmd);
1235 		if (job->job_result != FC_SUCCESS) {
1236 			*error = job->job_result;
1237 			fctl_dealloc_job(job);
1238 			return (pd);
1239 		}
1240 		fctl_dealloc_job(job);
1241 
1242 		/*
1243 		 * Check if the port device is created now.
1244 		 */
1245 		pd = fctl_get_remote_port_by_pwwn(port, pwwn);
1246 
1247 		if (pd == NULL) {
1248 			*error = FC_FAILURE;
1249 		} else {
1250 			*error = FC_SUCCESS;
1251 
1252 			/*
1253 			 * A ULP now knows about this pd, so mark it
1254 			 */
1255 			mutex_enter(&pd->pd_mutex);
1256 			pd->pd_aux_flags |= PD_GIVEN_TO_ULPS;
1257 			mutex_exit(&pd->pd_mutex);
1258 		}
1259 	} else {
1260 		mutex_exit(&port->fp_mutex);
1261 		*error = FC_FAILURE;
1262 	}
1263 
1264 	return (pd);
1265 }
1266 
1267 
1268 /*
1269  * If a NS object exists in the host and query is performed
1270  * on that object, we should retrieve it from our basket
1271  * and return it right here, there by saving a request going
1272  * all the up to the Name Server.
1273  */
1274 int
1275 fc_ulp_port_ns(opaque_t port_handle, opaque_t pd, fc_ns_cmd_t *ns_req)
1276 {
1277 	int		rval;
1278 	int		fabric;
1279 	job_request_t	*job;
1280 	fctl_ns_req_t	*ns_cmd;
1281 	fc_local_port_t	*port = port_handle;
1282 
1283 	mutex_enter(&port->fp_mutex);
1284 	fabric = FC_IS_TOP_SWITCH(port->fp_topology) ? 1 : 0;
1285 	mutex_exit(&port->fp_mutex);
1286 
1287 	/*
1288 	 * Name server query can't be performed for devices not in Fabric
1289 	 */
1290 	if (!fabric && pd) {
1291 		return (FC_BADOBJECT);
1292 	}
1293 
1294 	if (FC_IS_CMD_A_REG(ns_req->ns_cmd)) {
1295 		if (pd == NULL) {
1296 			rval = fctl_update_host_ns_values(port, ns_req);
1297 			if (rval != FC_SUCCESS) {
1298 				return (rval);
1299 			}
1300 		} else {
1301 			/*
1302 			 * Guess what, FC-GS-2 currently prohibits (not
1303 			 * in the strongest language though) setting of
1304 			 * NS object values by other ports. But we might
1305 			 * get that changed to at least accommodate setting
1306 			 * symbolic node/port names - But if disks/tapes
1307 			 * were going to provide a method to set these
1308 			 * values directly (which in turn might register
1309 			 * with the NS when they come up; yep, for that
1310 			 * to happen the disks will have to be very well
1311 			 * behaved Fabric citizen) we won't need to
1312 			 * register the symbolic port/node names for
1313 			 * other ports too (rather send down SCSI commands
1314 			 * to the devices to set the names)
1315 			 *
1316 			 * Be that as it may, let's continue to fail
1317 			 * registration requests for other ports. period.
1318 			 */
1319 			return (FC_BADOBJECT);
1320 		}
1321 
1322 		if (!fabric) {
1323 			return (FC_SUCCESS);
1324 		}
1325 	} else if (!fabric) {
1326 		return (fctl_retrieve_host_ns_values(port, ns_req));
1327 	}
1328 
1329 	job = fctl_alloc_job(JOB_NS_CMD, 0, NULL, NULL, KM_SLEEP);
1330 	ASSERT(job != NULL);
1331 
1332 	ns_cmd = fctl_alloc_ns_cmd(ns_req->ns_req_len,
1333 	    ns_req->ns_resp_len, ns_req->ns_resp_len, 0, KM_SLEEP);
1334 	ASSERT(ns_cmd != NULL);
1335 	ns_cmd->ns_cmd_code = ns_req->ns_cmd;
1336 	bcopy(ns_req->ns_req_payload, ns_cmd->ns_cmd_buf,
1337 	    ns_req->ns_req_len);
1338 
1339 	job->job_private = (void *)ns_cmd;
1340 	fctl_enque_job(port, job);
1341 	fctl_jobwait(job);
1342 	rval = job->job_result;
1343 
1344 	if (ns_req->ns_resp_len >= ns_cmd->ns_data_len) {
1345 		bcopy(ns_cmd->ns_data_buf, ns_req->ns_resp_payload,
1346 		    ns_cmd->ns_data_len);
1347 	}
1348 	bcopy(&ns_cmd->ns_resp_hdr, &ns_req->ns_resp_hdr,
1349 	    sizeof (fc_ct_header_t));
1350 
1351 	fctl_free_ns_cmd(ns_cmd);
1352 	fctl_dealloc_job(job);
1353 
1354 	return (rval);
1355 }
1356 
1357 
1358 int
1359 fc_ulp_transport(opaque_t port_handle, fc_packet_t *pkt)
1360 {
1361 	int			rval;
1362 	fc_local_port_t		*port;
1363 	fc_remote_port_t	*pd, *newpd;
1364 	fc_ulp_rscn_info_t	*rscnp =
1365 	    (fc_ulp_rscn_info_t *)pkt->pkt_ulp_rscn_infop;
1366 
1367 	port = port_handle;
1368 
1369 	if (pkt->pkt_tran_flags & FC_TRAN_DUMPING) {
1370 		return (port->fp_fca_tran->fca_transport(
1371 		    port->fp_fca_handle, pkt));
1372 	}
1373 
1374 	mutex_enter(&port->fp_mutex);
1375 	if (port->fp_statec_busy) {
1376 		mutex_exit(&port->fp_mutex);
1377 		return (FC_STATEC_BUSY);
1378 	}
1379 
1380 	/* A locus of race conditions */
1381 	if (((FC_PORT_STATE_MASK(port->fp_state)) == FC_STATE_OFFLINE) ||
1382 	    (port->fp_soft_state &
1383 	    (FP_SOFT_IN_DETACH | FP_SOFT_SUSPEND | FP_SOFT_POWER_DOWN))) {
1384 		mutex_exit(&port->fp_mutex);
1385 		return (FC_OFFLINE);
1386 	}
1387 
1388 	/*
1389 	 * If the rscn count in the packet is not the same as the rscn count
1390 	 * in the fc_local_port_t, then one or more new RSCNs has occurred.
1391 	 */
1392 	if ((rscnp != NULL) &&
1393 	    (rscnp->ulp_rscn_count != FC_INVALID_RSCN_COUNT) &&
1394 	    (rscnp->ulp_rscn_count != port->fp_rscn_count)) {
1395 		mutex_exit(&port->fp_mutex);
1396 		return (FC_DEVICE_BUSY_NEW_RSCN);
1397 	}
1398 
1399 	pd = pkt->pkt_pd;
1400 	if (pd) {
1401 		if (pd->pd_type == PORT_DEVICE_OLD ||
1402 		    pd->pd_state == PORT_DEVICE_INVALID) {
1403 
1404 			newpd = fctl_get_remote_port_by_pwwn_mutex_held(port,
1405 			    &pd->pd_port_name);
1406 
1407 			/*
1408 			 * The remote port (pd) in the packet is no longer
1409 			 * usable, as the old pd still exists we can use the
1410 			 * WWN to check if we have a current pd for the device
1411 			 * we want. Either way we continue with the old logic
1412 			 * whether we have a new pd or not, as the new pd
1413 			 * could be bad, or have become unusable.
1414 			 */
1415 			if ((newpd) && (newpd != pd)) {
1416 
1417 				/*
1418 				 * There is a better remote port (pd) to try,
1419 				 * so we need to fix the reference counts, etc.
1420 				 */
1421 				mutex_enter(&newpd->pd_mutex);
1422 				newpd->pd_ref_count++;
1423 				pkt->pkt_pd = newpd;
1424 				mutex_exit(&newpd->pd_mutex);
1425 
1426 				mutex_enter(&pd->pd_mutex);
1427 				pd->pd_ref_count--;
1428 				if ((pd->pd_state == PORT_DEVICE_INVALID) &&
1429 				    (pd->pd_ref_count == 0)) {
1430 					fc_remote_node_t *node =
1431 					    pd->pd_remote_nodep;
1432 
1433 					mutex_exit(&pd->pd_mutex);
1434 					mutex_exit(&port->fp_mutex);
1435 
1436 					/*
1437 					 * This will create another PD hole
1438 					 * where we have a reference to a pd,
1439 					 * but someone else could remove it.
1440 					 */
1441 					if ((fctl_destroy_remote_port(port, pd)
1442 					    == 0) && (node != NULL)) {
1443 						fctl_destroy_remote_node(node);
1444 					}
1445 					mutex_enter(&port->fp_mutex);
1446 				} else {
1447 					mutex_exit(&pd->pd_mutex);
1448 				}
1449 				pd = newpd;
1450 			}
1451 		}
1452 
1453 		if (pd->pd_state != PORT_DEVICE_LOGGED_IN) {
1454 			rval = (pd->pd_state == PORT_DEVICE_VALID) ?
1455 			    FC_LOGINREQ : FC_BADDEV;
1456 			mutex_exit(&port->fp_mutex);
1457 			return (rval);
1458 		}
1459 
1460 		if (pd->pd_flags != PD_IDLE) {
1461 			mutex_exit(&port->fp_mutex);
1462 			return (FC_DEVICE_BUSY);
1463 		}
1464 
1465 		if (pd->pd_type == PORT_DEVICE_OLD ||
1466 		    pd->pd_state == PORT_DEVICE_INVALID) {
1467 			mutex_exit(&port->fp_mutex);
1468 			return (FC_BADDEV);
1469 		}
1470 
1471 	} else if (FC_IS_REAL_DEVICE(pkt->pkt_cmd_fhdr.d_id)) {
1472 		mutex_exit(&port->fp_mutex);
1473 		return (FC_BADPACKET);
1474 	}
1475 	mutex_exit(&port->fp_mutex);
1476 
1477 	return (port->fp_fca_tran->fca_transport(port->fp_fca_handle, pkt));
1478 }
1479 
1480 
1481 int
1482 fc_ulp_issue_els(opaque_t port_handle, fc_packet_t *pkt)
1483 {
1484 	int			rval;
1485 	fc_local_port_t		*port = port_handle;
1486 	fc_remote_port_t	*pd;
1487 	fc_ulp_rscn_info_t	*rscnp =
1488 	    (fc_ulp_rscn_info_t *)pkt->pkt_ulp_rscn_infop;
1489 
1490 	/*
1491 	 * If the port is OFFLINE, or if the port driver is
1492 	 * being SUSPENDED/PM_SUSPENDED/DETACHED, block all
1493 	 * ELS operations
1494 	 */
1495 	mutex_enter(&port->fp_mutex);
1496 	if ((FC_PORT_STATE_MASK(port->fp_state) == FC_STATE_OFFLINE) ||
1497 	    (port->fp_soft_state &
1498 	    (FP_SOFT_IN_DETACH | FP_SOFT_SUSPEND | FP_SOFT_POWER_DOWN))) {
1499 		mutex_exit(&port->fp_mutex);
1500 		return (FC_OFFLINE);
1501 	}
1502 
1503 	if (port->fp_statec_busy) {
1504 		mutex_exit(&port->fp_mutex);
1505 		return (FC_STATEC_BUSY);
1506 	}
1507 
1508 	/*
1509 	 * If the rscn count in the packet is not the same as the rscn count
1510 	 * in the fc_local_port_t, then one or more new RSCNs has occurred.
1511 	 */
1512 	if ((rscnp != NULL) &&
1513 	    (rscnp->ulp_rscn_count != FC_INVALID_RSCN_COUNT) &&
1514 	    (rscnp->ulp_rscn_count != port->fp_rscn_count)) {
1515 		mutex_exit(&port->fp_mutex);
1516 		return (FC_DEVICE_BUSY_NEW_RSCN);
1517 	}
1518 
1519 	mutex_exit(&port->fp_mutex);
1520 
1521 	if ((pd = pkt->pkt_pd) != NULL) {
1522 		mutex_enter(&pd->pd_mutex);
1523 		if (pd->pd_state != PORT_DEVICE_LOGGED_IN) {
1524 			rval = (pd->pd_state == PORT_DEVICE_VALID) ?
1525 			    FC_LOGINREQ : FC_BADDEV;
1526 			mutex_exit(&pd->pd_mutex);
1527 			return (rval);
1528 		}
1529 
1530 		if (pd->pd_flags != PD_IDLE) {
1531 			mutex_exit(&pd->pd_mutex);
1532 			return (FC_DEVICE_BUSY);
1533 		}
1534 		if (pd->pd_type == PORT_DEVICE_OLD ||
1535 		    pd->pd_state == PORT_DEVICE_INVALID) {
1536 			mutex_exit(&pd->pd_mutex);
1537 			return (FC_BADDEV);
1538 		}
1539 		mutex_exit(&pd->pd_mutex);
1540 	}
1541 
1542 	return (port->fp_fca_tran->fca_els_send(port->fp_fca_handle, pkt));
1543 }
1544 
1545 
1546 int
1547 fc_ulp_uballoc(opaque_t port_handle, uint32_t *count, uint32_t size,
1548     uint32_t type, uint64_t *tokens)
1549 {
1550 	fc_local_port_t *port = port_handle;
1551 
1552 	return (port->fp_fca_tran->fca_ub_alloc(port->fp_fca_handle,
1553 	    tokens, size, count, type));
1554 }
1555 
1556 
1557 int
1558 fc_ulp_ubfree(opaque_t port_handle, uint32_t count, uint64_t *tokens)
1559 {
1560 	fc_local_port_t *port = port_handle;
1561 
1562 	return (port->fp_fca_tran->fca_ub_free(port->fp_fca_handle,
1563 	    count, tokens));
1564 }
1565 
1566 
1567 int
1568 fc_ulp_ubrelease(opaque_t port_handle, uint32_t count, uint64_t *tokens)
1569 {
1570 	fc_local_port_t *port = port_handle;
1571 
1572 	return (port->fp_fca_tran->fca_ub_release(port->fp_fca_handle,
1573 	    count, tokens));
1574 }
1575 
1576 
1577 int
1578 fc_ulp_abort(opaque_t port_handle, fc_packet_t *pkt, int flags)
1579 {
1580 	fc_local_port_t *port = port_handle;
1581 
1582 	return (port->fp_fca_tran->fca_abort(port->fp_fca_handle, pkt, flags));
1583 }
1584 
1585 
1586 /*
1587  * Submit an asynchronous request to the job handler if the sleep
1588  * flag is set to KM_NOSLEEP, as such calls could have been made
1589  * in interrupt contexts, and the goal is to avoid busy waiting,
1590  * blocking on a conditional variable, a semaphore or any of the
1591  * synchronization primitives. A noticeable draw back with this
1592  * asynchronous request is that an FC_SUCCESS is returned long
1593  * before the reset is complete (successful or not).
1594  */
1595 int
1596 fc_ulp_linkreset(opaque_t port_handle, la_wwn_t *pwwn, int sleep)
1597 {
1598 	int		rval;
1599 	fc_local_port_t *port;
1600 	job_request_t	*job;
1601 
1602 	port = port_handle;
1603 	/*
1604 	 * Many a times, this function is called from interrupt
1605 	 * contexts and there have been several dead locks and
1606 	 * hangs - One of the simplest work arounds is to fib
1607 	 * if a RESET is in progress.
1608 	 */
1609 	mutex_enter(&port->fp_mutex);
1610 	if (port->fp_soft_state & FP_SOFT_IN_LINK_RESET) {
1611 		mutex_exit(&port->fp_mutex);
1612 		return (FC_SUCCESS);
1613 	}
1614 
1615 	/*
1616 	 * Ward off this reset if a state change is in progress.
1617 	 */
1618 	if (port->fp_statec_busy) {
1619 		mutex_exit(&port->fp_mutex);
1620 		return (FC_STATEC_BUSY);
1621 	}
1622 	port->fp_soft_state |= FP_SOFT_IN_LINK_RESET;
1623 	mutex_exit(&port->fp_mutex);
1624 
1625 	if (fctl_busy_port(port) != 0) {
1626 		mutex_enter(&port->fp_mutex);
1627 		port->fp_soft_state &= ~FP_SOFT_IN_LINK_RESET;
1628 		mutex_exit(&port->fp_mutex);
1629 		return (FC_FAILURE);
1630 	}
1631 
1632 	if (sleep == KM_SLEEP) {
1633 		job = fctl_alloc_job(JOB_LINK_RESET, 0, NULL, NULL, sleep);
1634 		ASSERT(job != NULL);
1635 
1636 		job->job_private = (void *)pwwn;
1637 		job->job_counter = 1;
1638 		fctl_enque_job(port, job);
1639 		fctl_jobwait(job);
1640 
1641 		mutex_enter(&port->fp_mutex);
1642 		port->fp_soft_state &= ~FP_SOFT_IN_LINK_RESET;
1643 		mutex_exit(&port->fp_mutex);
1644 
1645 		fctl_idle_port(port);
1646 
1647 		rval = job->job_result;
1648 		fctl_dealloc_job(job);
1649 	} else {
1650 		job = fctl_alloc_job(JOB_LINK_RESET, JOB_TYPE_FCTL_ASYNC,
1651 		    fctl_link_reset_done, port, sleep);
1652 		if (job == NULL) {
1653 			mutex_enter(&port->fp_mutex);
1654 			port->fp_soft_state &= ~FP_SOFT_IN_LINK_RESET;
1655 			mutex_exit(&port->fp_mutex);
1656 			fctl_idle_port(port);
1657 			return (FC_NOMEM);
1658 		}
1659 		job->job_private = (void *)pwwn;
1660 		job->job_counter = 1;
1661 		fctl_priority_enque_job(port, job);
1662 		rval = FC_SUCCESS;
1663 	}
1664 
1665 	return (rval);
1666 }
1667 
1668 
1669 int
1670 fc_ulp_port_reset(opaque_t port_handle, uint32_t cmd)
1671 {
1672 	int		rval = FC_SUCCESS;
1673 	fc_local_port_t *port = port_handle;
1674 
1675 	switch (cmd) {
1676 	case FC_RESET_PORT:
1677 		rval = port->fp_fca_tran->fca_reset(
1678 		    port->fp_fca_handle, FC_FCA_LINK_RESET);
1679 		break;
1680 
1681 	case FC_RESET_ADAPTER:
1682 		rval = port->fp_fca_tran->fca_reset(
1683 		    port->fp_fca_handle, FC_FCA_RESET);
1684 		break;
1685 
1686 	case FC_RESET_DUMP:
1687 		rval = port->fp_fca_tran->fca_reset(
1688 		    port->fp_fca_handle, FC_FCA_CORE);
1689 		break;
1690 
1691 	case FC_RESET_CRASH:
1692 		rval = port->fp_fca_tran->fca_reset(
1693 		    port->fp_fca_handle, FC_FCA_RESET_CORE);
1694 		break;
1695 
1696 	default:
1697 		rval = FC_FAILURE;
1698 	}
1699 
1700 	return (rval);
1701 }
1702 
1703 
1704 int
1705 fc_ulp_get_port_login_params(opaque_t port_handle, la_els_logi_t *login_params)
1706 {
1707 	fc_local_port_t *port = port_handle;
1708 
1709 	/* Copy the login parameters */
1710 	*login_params = port->fp_service_params;
1711 	return (FC_SUCCESS);
1712 }
1713 
1714 
1715 int
1716 fc_ulp_get_port_instance(opaque_t port_handle)
1717 {
1718 	fc_local_port_t *port = port_handle;
1719 
1720 	return (port->fp_instance);
1721 }
1722 
1723 
1724 opaque_t
1725 fc_ulp_get_port_handle(int port_instance)
1726 {
1727 	opaque_t	port_handle = NULL;
1728 	fc_fca_port_t	*cur;
1729 
1730 	mutex_enter(&fctl_port_lock);
1731 	for (cur = fctl_fca_portlist; cur; cur = cur->port_next) {
1732 		if (cur->port_handle->fp_instance == port_instance) {
1733 			port_handle = (opaque_t)cur->port_handle;
1734 			break;
1735 		}
1736 	}
1737 	mutex_exit(&fctl_port_lock);
1738 
1739 	return (port_handle);
1740 }
1741 
1742 
1743 int
1744 fc_ulp_error(int fc_errno, char **errmsg)
1745 {
1746 	return (fctl_error(fc_errno, errmsg));
1747 }
1748 
1749 
1750 int
1751 fc_ulp_pkt_error(fc_packet_t *pkt, char **state, char **reason,
1752     char **action, char **expln)
1753 {
1754 	return (fctl_pkt_error(pkt, state, reason, action, expln));
1755 }
1756 
1757 
1758 /*
1759  * If an ULP by the specified name exists, return FC_SUCCESS, else FC_FAILURE
1760  */
1761 int
1762 fc_ulp_is_name_present(caddr_t ulp_name)
1763 {
1764 	int		rval = FC_FAILURE;
1765 	fc_ulp_list_t	*list;
1766 
1767 	mutex_enter(&fctl_ulp_list_mutex);
1768 	for (list = fctl_ulp_list; list != NULL; list = list->ulp_next) {
1769 		if (strcmp(list->ulp_info->ulp_name, ulp_name) == 0) {
1770 			rval = FC_SUCCESS;
1771 			break;
1772 		}
1773 	}
1774 	mutex_exit(&fctl_ulp_list_mutex);
1775 
1776 	return (rval);
1777 }
1778 
1779 
1780 /*
1781  * Return port WWN for a port Identifier
1782  */
1783 int
1784 fc_ulp_get_pwwn_by_did(opaque_t port_handle, fc_portid_t d_id, la_wwn_t *pwwn)
1785 {
1786 	int			rval = FC_FAILURE;
1787 	fc_remote_port_t	*pd;
1788 	fc_local_port_t		*port = port_handle;
1789 
1790 	pd = fctl_get_remote_port_by_did(port, d_id.port_id);
1791 	if (pd != NULL) {
1792 		mutex_enter(&pd->pd_mutex);
1793 		*pwwn = pd->pd_port_name;
1794 		mutex_exit(&pd->pd_mutex);
1795 		rval = FC_SUCCESS;
1796 	}
1797 
1798 	return (rval);
1799 }
1800 
1801 
1802 /*
1803  * Return a port map for a port WWN
1804  */
1805 int
1806 fc_ulp_pwwn_to_portmap(opaque_t port_handle, la_wwn_t *bytes, fc_portmap_t *map)
1807 {
1808 	fc_local_port_t		*port = port_handle;
1809 	fc_remote_node_t	*node;
1810 	fc_remote_port_t	*pd;
1811 
1812 	pd = fctl_get_remote_port_by_pwwn(port, bytes);
1813 	if (pd == NULL) {
1814 		return (FC_FAILURE);
1815 	}
1816 
1817 	mutex_enter(&pd->pd_mutex);
1818 	map->map_pwwn = pd->pd_port_name;
1819 	map->map_did = pd->pd_port_id;
1820 	map->map_hard_addr = pd->pd_hard_addr;
1821 	map->map_state = pd->pd_state;
1822 	map->map_type = pd->pd_type;
1823 	map->map_flags = 0;
1824 
1825 	ASSERT(map->map_type <= PORT_DEVICE_DELETE);
1826 
1827 	bcopy(pd->pd_fc4types, map->map_fc4_types, sizeof (pd->pd_fc4types));
1828 
1829 	node = pd->pd_remote_nodep;
1830 	mutex_exit(&pd->pd_mutex);
1831 
1832 	if (node) {
1833 		mutex_enter(&node->fd_mutex);
1834 		map->map_nwwn = node->fd_node_name;
1835 		mutex_exit(&node->fd_mutex);
1836 	}
1837 	map->map_pd = pd;
1838 
1839 	return (FC_SUCCESS);
1840 }
1841 
1842 
1843 opaque_t
1844 fc_ulp_get_fca_device(opaque_t port_handle, fc_portid_t d_id)
1845 {
1846 	fc_local_port_t	*port = port_handle;
1847 
1848 	if (port->fp_fca_tran->fca_get_device == NULL) {
1849 		return (NULL);
1850 	}
1851 
1852 	return (port->fp_fca_tran->fca_get_device(port->fp_fca_handle, d_id));
1853 }
1854 
1855 
1856 int
1857 fc_ulp_port_notify(opaque_t port_handle, uint32_t cmd)
1858 {
1859 	int		rval = FC_SUCCESS;
1860 	fc_local_port_t	*port = port_handle;
1861 
1862 	if (port->fp_fca_tran->fca_notify) {
1863 		mutex_enter(&port->fp_mutex);
1864 		switch (cmd) {
1865 		case FC_NOTIFY_TARGET_MODE:
1866 			port->fp_options |= FP_TARGET_MODE;
1867 			break;
1868 		case FC_NOTIFY_NO_TARGET_MODE:
1869 			port->fp_options &= ~FP_TARGET_MODE;
1870 			break;
1871 		}
1872 		mutex_exit(&port->fp_mutex);
1873 		rval = port->fp_fca_tran->fca_notify(port->fp_fca_handle, cmd);
1874 	}
1875 
1876 	return (rval);
1877 }
1878 
1879 
1880 void
1881 fc_ulp_disable_relogin(opaque_t *fc_port, la_wwn_t *pwwn)
1882 {
1883 	fc_remote_port_t *pd =
1884 	    fctl_get_remote_port_by_pwwn((fc_local_port_t *)fc_port, pwwn);
1885 
1886 	if (pd) {
1887 		mutex_enter(&pd->pd_mutex);
1888 		pd->pd_aux_flags |= PD_DISABLE_RELOGIN;
1889 		mutex_exit(&pd->pd_mutex);
1890 	}
1891 }
1892 
1893 
1894 void
1895 fc_ulp_enable_relogin(opaque_t *fc_port, la_wwn_t *pwwn)
1896 {
1897 	fc_remote_port_t *pd =
1898 	    fctl_get_remote_port_by_pwwn((fc_local_port_t *)fc_port, pwwn);
1899 
1900 	if (pd) {
1901 		mutex_enter(&pd->pd_mutex);
1902 		pd->pd_aux_flags &= ~PD_DISABLE_RELOGIN;
1903 		mutex_exit(&pd->pd_mutex);
1904 	}
1905 }
1906 
1907 
1908 /*
1909  * fc_fca_init
1910  *		Overload the FCA bus_ops vector in its dev_ops with
1911  *		fctl_fca_busops to handle all the INITchilds for "sf"
1912  *		in one common place.
1913  *
1914  *		Should be called from FCA _init routine.
1915  */
1916 void
1917 fc_fca_init(struct dev_ops *fca_devops_p)
1918 {
1919 #ifndef	__lock_lint
1920 	fca_devops_p->devo_bus_ops = &fctl_fca_busops;
1921 #endif	/* __lock_lint */
1922 }
1923 
1924 
1925 /*
1926  * fc_fca_attach
1927  */
1928 int
1929 fc_fca_attach(dev_info_t *fca_dip, fc_fca_tran_t *tran)
1930 {
1931 	/*
1932 	 * When we are in a position to offer downward compatibility
1933 	 * we should change the following check to allow lower revision
1934 	 * of FCAs; But we aren't there right now.
1935 	 */
1936 	if (tran->fca_version != FCTL_FCA_MODREV_5) {
1937 		const char *name = ddi_driver_name(fca_dip);
1938 
1939 		ASSERT(name != NULL);
1940 
1941 		cmn_err(CE_WARN, "fctl: FCA %s version mismatch"
1942 		    " please upgrade %s", name, name);
1943 		return (DDI_FAILURE);
1944 	}
1945 
1946 	ddi_set_driver_private(fca_dip, (caddr_t)tran);
1947 	return (DDI_SUCCESS);
1948 }
1949 
1950 
1951 /*
1952  * fc_fca_detach
1953  */
1954 int
1955 fc_fca_detach(dev_info_t *fca_dip)
1956 {
1957 	ddi_set_driver_private(fca_dip, NULL);
1958 	return (DDI_SUCCESS);
1959 }
1960 
1961 
1962 /*
1963  * Check if the frame is a Link response Frame; Handle all cases (P_RJT,
1964  * F_RJT, P_BSY, F_BSY fall into this category). Check also for some Basic
1965  * Link Service responses such as BA_RJT and Extended Link Service response
1966  * such as LS_RJT. If the response is a Link_Data Frame or something that
1967  * this function doesn't understand return FC_FAILURE; Otherwise, fill out
1968  * various fields (state, action, reason, expln) from the response gotten
1969  * in the packet and return FC_SUCCESS.
1970  */
1971 int
1972 fc_fca_update_errors(fc_packet_t *pkt)
1973 {
1974 	int ret = FC_SUCCESS;
1975 
1976 	switch (pkt->pkt_resp_fhdr.r_ctl) {
1977 	case R_CTL_P_RJT: {
1978 		uint32_t prjt;
1979 
1980 		prjt = pkt->pkt_resp_fhdr.ro;
1981 		pkt->pkt_state = FC_PKT_NPORT_RJT;
1982 		pkt->pkt_action = (prjt & 0xFF000000) >> 24;
1983 		pkt->pkt_reason = (prjt & 0xFF0000) >> 16;
1984 		break;
1985 	}
1986 
1987 	case R_CTL_F_RJT: {
1988 		uint32_t frjt;
1989 
1990 		frjt = pkt->pkt_resp_fhdr.ro;
1991 		pkt->pkt_state = FC_PKT_FABRIC_RJT;
1992 		pkt->pkt_action = (frjt & 0xFF000000) >> 24;
1993 		pkt->pkt_reason = (frjt & 0xFF0000) >> 16;
1994 		break;
1995 	}
1996 
1997 	case R_CTL_P_BSY: {
1998 		uint32_t pbsy;
1999 
2000 		pbsy = pkt->pkt_resp_fhdr.ro;
2001 		pkt->pkt_state = FC_PKT_NPORT_BSY;
2002 		pkt->pkt_action = (pbsy & 0xFF000000) >> 24;
2003 		pkt->pkt_reason = (pbsy & 0xFF0000) >> 16;
2004 		break;
2005 	}
2006 
2007 	case R_CTL_F_BSY_LC:
2008 	case R_CTL_F_BSY_DF: {
2009 		uchar_t fbsy;
2010 
2011 		fbsy = pkt->pkt_resp_fhdr.type;
2012 		pkt->pkt_state = FC_PKT_FABRIC_BSY;
2013 		pkt->pkt_reason = (fbsy & 0xF0) >> 4;
2014 		break;
2015 	}
2016 
2017 	case R_CTL_LS_BA_RJT: {
2018 		uint32_t brjt;
2019 
2020 		brjt = *(uint32_t *)pkt->pkt_resp;
2021 		pkt->pkt_state = FC_PKT_BA_RJT;
2022 		pkt->pkt_reason = (brjt & 0xFF0000) >> 16;
2023 		pkt->pkt_expln = (brjt & 0xFF00) >> 8;
2024 		break;
2025 	}
2026 
2027 	case R_CTL_ELS_RSP: {
2028 		la_els_rjt_t *lsrjt;
2029 
2030 		lsrjt = (la_els_rjt_t *)pkt->pkt_resp;
2031 		if (lsrjt->ls_code.ls_code == LA_ELS_RJT) {
2032 			pkt->pkt_state = FC_PKT_LS_RJT;
2033 			pkt->pkt_reason = lsrjt->reason;
2034 			pkt->pkt_action = lsrjt->action;
2035 			break;
2036 		}
2037 		/* FALLTHROUGH */
2038 	}
2039 
2040 	default:
2041 		ret = FC_FAILURE;
2042 		break;
2043 	}
2044 
2045 	return (ret);
2046 }
2047 
2048 
2049 int
2050 fc_fca_error(int fc_errno, char **errmsg)
2051 {
2052 	return (fctl_error(fc_errno, errmsg));
2053 }
2054 
2055 
2056 int
2057 fc_fca_pkt_error(fc_packet_t *pkt, char **state, char **reason,
2058     char **action, char **expln)
2059 {
2060 	return (fctl_pkt_error(pkt, state, reason, action, expln));
2061 }
2062 
2063 
2064 /*
2065  * WWN to string goodie. Unpredictable results will happen
2066  * if enough memory isn't supplied in str argument. If you
2067  * are wondering how much does this routine need, it is just
2068  * (2 * WWN size + 1). So for a WWN size of 8 bytes the str
2069  * argument should have atleast 17 bytes allocated.
2070  */
2071 void
2072 fc_wwn_to_str(la_wwn_t *wwn, caddr_t str)
2073 {
2074 	int count;
2075 
2076 	for (count = 0; count < FCTL_WWN_SIZE(wwn); count++, str += 2) {
2077 		(void) sprintf(str, "%02x", wwn->raw_wwn[count]);
2078 	}
2079 	*str = '\0';
2080 }
2081 
2082 #define	FC_ATOB(x)	(((x) >= '0' && (x) <= '9') ? ((x) - '0') :	\
2083 			((x) >= 'a' && (x) <= 'f') ?			\
2084 			((x) - 'a' + 10) : ((x) - 'A' + 10))
2085 
2086 void
2087 fc_str_to_wwn(caddr_t str, la_wwn_t *wwn)
2088 {
2089 	int count = 0;
2090 	uchar_t byte;
2091 
2092 	while (*str) {
2093 		byte = FC_ATOB(*str);
2094 		str++;
2095 		byte = byte << 4 | FC_ATOB(*str);
2096 		str++;
2097 		wwn->raw_wwn[count++] = byte;
2098 	}
2099 }
2100 
2101 /*
2102  * FCA driver's intercepted bus control operations.
2103  */
2104 static int
2105 fctl_fca_bus_ctl(dev_info_t *fca_dip, dev_info_t *rip,
2106     ddi_ctl_enum_t op, void *arg, void *result)
2107 {
2108 	switch (op) {
2109 	case DDI_CTLOPS_REPORTDEV:
2110 		break;
2111 
2112 	case DDI_CTLOPS_IOMIN:
2113 		break;
2114 
2115 	case DDI_CTLOPS_INITCHILD:
2116 		return (fctl_initchild(fca_dip, (dev_info_t *)arg));
2117 
2118 	case DDI_CTLOPS_UNINITCHILD:
2119 		return (fctl_uninitchild(fca_dip, (dev_info_t *)arg));
2120 
2121 	default:
2122 		return (ddi_ctlops(fca_dip, rip, op, arg, result));
2123 	}
2124 
2125 	return (DDI_SUCCESS);
2126 }
2127 
2128 
2129 /*
2130  * FCAs indicate the maximum number of ports supported in their
2131  * tran structure. Fail the INITCHILD if the child port number
2132  * is any greater than the maximum number of ports supported
2133  * by the FCA.
2134  */
2135 static int
2136 fctl_initchild(dev_info_t *fca_dip, dev_info_t *port_dip)
2137 {
2138 	int		rval;
2139 	int		port_no;
2140 	int		port_len;
2141 	char		name[20];
2142 	fc_fca_tran_t	*tran;
2143 	dev_info_t	*dip;
2144 	int		portprop;
2145 
2146 	port_len = sizeof (port_no);
2147 
2148 	/* physical port do not has this property */
2149 	portprop = ddi_prop_get_int(DDI_DEV_T_ANY, port_dip,
2150 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
2151 	    "phyport-instance", -1);
2152 
2153 	if ((portprop == -1) && ndi_dev_is_persistent_node(port_dip)) {
2154 		/*
2155 		 * Clear any addr bindings created by fcode interpreter
2156 		 * in devi_last_addr so that a ndi_devi_find should never
2157 		 * return this fcode node.
2158 		 */
2159 		ddi_set_name_addr(port_dip, NULL);
2160 		return (DDI_FAILURE);
2161 	}
2162 
2163 	rval = ddi_prop_op(DDI_DEV_T_ANY, port_dip, PROP_LEN_AND_VAL_BUF,
2164 	    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "port",
2165 	    (caddr_t)&port_no, &port_len);
2166 
2167 	if (rval != DDI_SUCCESS) {
2168 		return (DDI_FAILURE);
2169 	}
2170 
2171 	tran = (fc_fca_tran_t *)ddi_get_driver_private(fca_dip);
2172 	ASSERT(tran != NULL);
2173 
2174 	(void) sprintf((char *)name, "%x,0", port_no);
2175 	ddi_set_name_addr(port_dip, name);
2176 
2177 	dip = ndi_devi_find(fca_dip, ddi_binding_name(port_dip), name);
2178 
2179 	/*
2180 	 * Even though we never initialize FCode nodes of fp, such a node
2181 	 * could still be there after a DR operation. There will only be
2182 	 * one FCode node, so if this is the one, clear it and issue a
2183 	 * ndi_devi_find again.
2184 	 */
2185 	if ((portprop == -1) && dip && ndi_dev_is_persistent_node(dip)) {
2186 		ddi_set_name_addr(dip, NULL);
2187 		dip = ndi_devi_find(fca_dip, ddi_binding_name(port_dip), name);
2188 	}
2189 
2190 	if ((portprop == -1) && dip && (dip != port_dip)) {
2191 		/*
2192 		 * Here we have a duplicate .conf entry. Clear the addr
2193 		 * set previously and return failure.
2194 		 */
2195 		ddi_set_name_addr(port_dip, NULL);
2196 		return (DDI_FAILURE);
2197 	}
2198 
2199 	return (DDI_SUCCESS);
2200 }
2201 
2202 
2203 /* ARGSUSED */
2204 static int
2205 fctl_uninitchild(dev_info_t *fca_dip, dev_info_t *port_dip)
2206 {
2207 	ddi_set_name_addr(port_dip, NULL);
2208 	return (DDI_SUCCESS);
2209 }
2210 
2211 
2212 static dev_info_t *
2213 fctl_findchild(dev_info_t *pdip, char *cname, char *caddr)
2214 {
2215 	dev_info_t *dip;
2216 	char *addr;
2217 
2218 	ASSERT(cname != NULL && caddr != NULL);
2219 	/* ASSERT(DEVI_BUSY_OWNED(pdip)); */
2220 
2221 	for (dip = ddi_get_child(pdip); dip != NULL;
2222 	    dip = ddi_get_next_sibling(dip)) {
2223 		if (strcmp(cname, ddi_node_name(dip)) != 0) {
2224 			continue;
2225 		}
2226 
2227 		if ((addr = ddi_get_name_addr(dip)) == NULL) {
2228 			if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
2229 			    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
2230 			    "bus-addr", &addr) == DDI_PROP_SUCCESS) {
2231 				if (strcmp(caddr, addr) == 0) {
2232 					ddi_prop_free(addr);
2233 					return (dip);
2234 				}
2235 				ddi_prop_free(addr);
2236 			}
2237 		} else {
2238 			if (strcmp(caddr, addr) == 0) {
2239 				return (dip);
2240 			}
2241 		}
2242 	}
2243 
2244 	return (NULL);
2245 }
2246 
2247 int
2248 fctl_check_npiv_portindex(dev_info_t *dip, int vindex)
2249 {
2250 	int i, instance;
2251 	fc_local_port_t *port;
2252 
2253 	instance = ddi_get_instance(dip);
2254 	port = (fc_local_port_t *)fc_ulp_get_port_handle(instance);
2255 	if ((!port) || (vindex <= 0) || (vindex >= FC_NPIV_MAX_PORT)) {
2256 		return (0);
2257 	}
2258 
2259 	i = vindex-1;
2260 	mutex_enter(&port->fp_mutex);
2261 	if (port->fp_npiv_portindex[i] == 0) {
2262 		mutex_exit(&port->fp_mutex);
2263 		return (vindex);
2264 	}
2265 	mutex_exit(&port->fp_mutex);
2266 	return (0);
2267 }
2268 
2269 int
2270 fctl_get_npiv_portindex(dev_info_t *dip)
2271 {
2272 	int i, instance;
2273 	fc_local_port_t *port;
2274 
2275 	instance = ddi_get_instance(dip);
2276 	port = (fc_local_port_t *)fc_ulp_get_port_handle(instance);
2277 	if (!port) {
2278 		return (0);
2279 	}
2280 
2281 	mutex_enter(&port->fp_mutex);
2282 	for (i = 0; i < FC_NPIV_MAX_PORT; i++) {
2283 		if (port->fp_npiv_portindex[i] == 0) {
2284 			mutex_exit(&port->fp_mutex);
2285 			return (i+1);
2286 		}
2287 	}
2288 	mutex_exit(&port->fp_mutex);
2289 	return (0);
2290 }
2291 
2292 
2293 void
2294 fctl_set_npiv_portindex(dev_info_t *dip, int index)
2295 {
2296 	int instance;
2297 	fc_local_port_t *port;
2298 
2299 	instance = ddi_get_instance(dip);
2300 	port = (fc_local_port_t *)fc_ulp_get_port_handle(instance);
2301 	if (!port) {
2302 		return;
2303 	}
2304 	mutex_enter(&port->fp_mutex);
2305 	port->fp_npiv_portindex[index - 1] = 1;
2306 	mutex_exit(&port->fp_mutex);
2307 }
2308 
2309 
2310 int
2311 fctl_fca_create_npivport(dev_info_t *parent,
2312     dev_info_t *phydip, char *nname, char *pname, uint32_t *vindex)
2313 {
2314 	int rval = 0, devstrlen;
2315 	char	*devname, *cname, *caddr, *devstr;
2316 	dev_info_t	*child = NULL;
2317 	int		portnum;
2318 
2319 	if (*vindex == 0) {
2320 		portnum = fctl_get_npiv_portindex(phydip);
2321 		*vindex = portnum;
2322 	} else {
2323 		portnum = fctl_check_npiv_portindex(phydip, *vindex);
2324 	}
2325 
2326 	if (portnum == 0) {
2327 		cmn_err(CE_WARN,
2328 		    "Cann't find valid port index, fail to create devnode");
2329 		return (NDI_FAILURE);
2330 	}
2331 
2332 	devname = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
2333 	(void) sprintf(devname, "fp@%x,0", portnum);
2334 	devstrlen = strlen(devname) + 1;
2335 	devstr = i_ddi_strdup(devname, KM_SLEEP);
2336 	i_ddi_parse_name(devstr, &cname, &caddr, NULL);
2337 
2338 	if (fctl_findchild(parent, cname, caddr) != NULL) {
2339 		rval = NDI_FAILURE;
2340 		goto freememory;
2341 	}
2342 
2343 	ndi_devi_alloc_sleep(parent, cname, DEVI_PSEUDO_NODEID, &child);
2344 	if (child == NULL) {
2345 		cmn_err(CE_WARN,
2346 		    "fctl_create_npiv_port fail to create new devinfo");
2347 		rval = NDI_FAILURE;
2348 		goto freememory;
2349 	}
2350 
2351 	if (ndi_prop_update_string(DDI_DEV_T_NONE, child,
2352 	    "bus-addr", caddr) != DDI_PROP_SUCCESS) {
2353 		cmn_err(CE_WARN, "fctl%d: prop update bus-addr %s@%s failed",
2354 		    ddi_get_instance(parent), cname, caddr);
2355 		(void) ndi_devi_free(child);
2356 		rval = NDI_FAILURE;
2357 		goto freememory;
2358 	}
2359 
2360 	if (strlen(nname) != 0) {
2361 		if (ndi_prop_update_string(DDI_DEV_T_NONE, child,
2362 		    "node-name", nname) != DDI_PROP_SUCCESS) {
2363 			(void) ndi_devi_free(child);
2364 			rval = NDI_FAILURE;
2365 			goto freememory;
2366 		}
2367 	}
2368 
2369 	if (strlen(pname) != 0) {
2370 		if (ndi_prop_update_string(DDI_DEV_T_NONE, child,
2371 		    "port-name", pname) != DDI_PROP_SUCCESS) {
2372 			(void) ndi_devi_free(child);
2373 			rval = NDI_FAILURE;
2374 			goto freememory;
2375 		}
2376 	}
2377 
2378 	if (ddi_prop_update_int(DDI_DEV_T_NONE, child,
2379 	    "port", portnum) != DDI_PROP_SUCCESS) {
2380 		cmn_err(CE_WARN, "fp%d: prop_update port %s@%s failed",
2381 		    ddi_get_instance(parent), cname, caddr);
2382 		(void) ndi_devi_free(child);
2383 		rval = NDI_FAILURE;
2384 		goto freememory;
2385 	}
2386 
2387 	if (ddi_prop_update_int(DDI_DEV_T_NONE, child,
2388 	    "phyport-instance", ddi_get_instance(phydip)) != DDI_PROP_SUCCESS) {
2389 		cmn_err(CE_WARN,
2390 		    "fp%d: prop_update phyport-instance %s@%s failed",
2391 		    ddi_get_instance(parent), cname, caddr);
2392 		(void) ndi_devi_free(child);
2393 		rval = NDI_FAILURE;
2394 		goto freememory;
2395 	}
2396 
2397 	rval = ndi_devi_online(child, NDI_ONLINE_ATTACH);
2398 	if (rval != NDI_SUCCESS) {
2399 		cmn_err(CE_WARN, "fp%d: online_driver %s failed",
2400 		    ddi_get_instance(parent), cname);
2401 		rval = NDI_FAILURE;
2402 		goto freememory;
2403 	}
2404 
2405 	fctl_set_npiv_portindex(phydip, portnum);
2406 freememory:
2407 	kmem_free(devstr, devstrlen);
2408 	kmem_free(devname, MAXNAMELEN);
2409 
2410 	return (rval);
2411 }
2412 
2413 
2414 void
2415 fctl_add_port(fc_local_port_t *port)
2416 {
2417 	fc_fca_port_t *new;
2418 
2419 	new = kmem_zalloc(sizeof (*new), KM_SLEEP);
2420 
2421 	mutex_enter(&fctl_port_lock);
2422 	new->port_handle = port;
2423 	new->port_next = fctl_fca_portlist;
2424 	fctl_fca_portlist = new;
2425 	mutex_exit(&fctl_port_lock);
2426 }
2427 
2428 
2429 void
2430 fctl_remove_port(fc_local_port_t *port)
2431 {
2432 	fc_ulp_module_t		*mod;
2433 	fc_fca_port_t		*prev;
2434 	fc_fca_port_t		*list;
2435 	fc_ulp_ports_t		*ulp_port;
2436 
2437 	rw_enter(&fctl_ulp_lock, RW_WRITER);
2438 	rw_enter(&fctl_mod_ports_lock, RW_WRITER);
2439 
2440 	for (mod = fctl_ulp_modules; mod; mod = mod->mod_next) {
2441 		ulp_port = fctl_get_ulp_port(mod, port);
2442 		if (ulp_port == NULL) {
2443 			continue;
2444 		}
2445 
2446 #ifndef	__lock_lint
2447 		ASSERT((ulp_port->port_dstate & ULP_PORT_ATTACH) == 0);
2448 #endif /* __lock_lint */
2449 
2450 		(void) fctl_remove_ulp_port(mod, port);
2451 	}
2452 
2453 	rw_exit(&fctl_mod_ports_lock);
2454 	rw_exit(&fctl_ulp_lock);
2455 
2456 	mutex_enter(&fctl_port_lock);
2457 
2458 	list = fctl_fca_portlist;
2459 	prev = NULL;
2460 	while (list != NULL) {
2461 		if (list->port_handle == port) {
2462 			if (prev == NULL) {
2463 				fctl_fca_portlist = list->port_next;
2464 			} else {
2465 				prev->port_next = list->port_next;
2466 			}
2467 			kmem_free(list, sizeof (*list));
2468 			break;
2469 		}
2470 		prev = list;
2471 		list = list->port_next;
2472 	}
2473 	mutex_exit(&fctl_port_lock);
2474 }
2475 
2476 
2477 void
2478 fctl_attach_ulps(fc_local_port_t *port, fc_attach_cmd_t cmd,
2479     struct modlinkage *linkage)
2480 {
2481 	int			rval;
2482 	uint32_t		s_id;
2483 	uint32_t		state;
2484 	fc_ulp_module_t		*mod;
2485 	fc_ulp_port_info_t	info;
2486 	fc_ulp_ports_t		*ulp_port;
2487 
2488 	ASSERT(!MUTEX_HELD(&port->fp_mutex));
2489 
2490 	info.port_linkage = linkage;
2491 	info.port_dip = port->fp_port_dip;
2492 	info.port_handle = (opaque_t)port;
2493 	info.port_dma_behavior = port->fp_dma_behavior;
2494 	info.port_fcp_dma = port->fp_fcp_dma;
2495 	info.port_acc_attr = port->fp_fca_tran->fca_acc_attr;
2496 	info.port_fca_pkt_size = port->fp_fca_tran->fca_pkt_size;
2497 	info.port_reset_action = port->fp_reset_action;
2498 
2499 	mutex_enter(&port->fp_mutex);
2500 
2501 	/*
2502 	 * It is still possible that another thread could have gotten
2503 	 * into the detach process before we got here.
2504 	 */
2505 	if (port->fp_soft_state & FP_SOFT_IN_DETACH) {
2506 		mutex_exit(&port->fp_mutex);
2507 		return;
2508 	}
2509 
2510 	s_id = port->fp_port_id.port_id;
2511 	if (port->fp_statec_busy) {
2512 		info.port_state = port->fp_bind_state;
2513 	} else {
2514 		info.port_state = port->fp_state;
2515 	}
2516 
2517 	switch (state = FC_PORT_STATE_MASK(info.port_state)) {
2518 	case FC_STATE_LOOP:
2519 	case FC_STATE_NAMESERVICE:
2520 		info.port_state &= ~state;
2521 		info.port_state |= FC_STATE_ONLINE;
2522 		break;
2523 
2524 	default:
2525 		break;
2526 	}
2527 	ASSERT((info.port_state & FC_STATE_LOOP) == 0);
2528 
2529 	info.port_flags = port->fp_topology;
2530 	info.port_pwwn = port->fp_service_params.nport_ww_name;
2531 	info.port_nwwn = port->fp_service_params.node_ww_name;
2532 	mutex_exit(&port->fp_mutex);
2533 
2534 	rw_enter(&fctl_ulp_lock, RW_READER);
2535 	rw_enter(&fctl_mod_ports_lock, RW_WRITER);
2536 
2537 	for (mod = fctl_ulp_modules; mod; mod = mod->mod_next) {
2538 		if ((port->fp_soft_state & FP_SOFT_FCA_IS_NODMA) &&
2539 		    (mod->mod_info->ulp_type == FC_TYPE_IS8802_SNAP)) {
2540 			/*
2541 			 * We don't support IP over FC on FCOE HBA
2542 			 */
2543 			continue;
2544 		}
2545 
2546 		if ((ulp_port = fctl_get_ulp_port(mod, port)) == NULL) {
2547 			ulp_port = fctl_add_ulp_port(mod, port, KM_SLEEP);
2548 			ASSERT(ulp_port != NULL);
2549 
2550 			mutex_enter(&ulp_port->port_mutex);
2551 			ulp_port->port_statec = ((info.port_state &
2552 			    FC_STATE_ONLINE) ? FC_ULP_STATEC_ONLINE :
2553 			    FC_ULP_STATEC_OFFLINE);
2554 			mutex_exit(&ulp_port->port_mutex);
2555 		}
2556 	}
2557 
2558 	rw_downgrade(&fctl_mod_ports_lock);
2559 
2560 	for (mod = fctl_ulp_modules; mod; mod = mod->mod_next) {
2561 		if ((port->fp_soft_state & FP_SOFT_FCA_IS_NODMA) &&
2562 		    (mod->mod_info->ulp_type == FC_TYPE_IS8802_SNAP)) {
2563 			/*
2564 			 * We don't support IP over FC on FCOE HBA
2565 			 */
2566 			continue;
2567 		}
2568 
2569 		ulp_port = fctl_get_ulp_port(mod, port);
2570 		ASSERT(ulp_port != NULL);
2571 
2572 		if (fctl_pre_attach(ulp_port, cmd) == FC_FAILURE) {
2573 			continue;
2574 		}
2575 
2576 		fctl_init_dma_attr(port, mod, &info);
2577 
2578 		rval = mod->mod_info->ulp_port_attach(
2579 		    mod->mod_info->ulp_handle, &info, cmd, s_id);
2580 
2581 		fctl_post_attach(mod, ulp_port, cmd, rval);
2582 
2583 		if (rval == FC_SUCCESS && cmd == FC_CMD_ATTACH &&
2584 		    strcmp(mod->mod_info->ulp_name, "fcp") == 0) {
2585 			ASSERT(ddi_get_driver_private(info.port_dip) != NULL);
2586 		}
2587 	}
2588 
2589 	rw_exit(&fctl_mod_ports_lock);
2590 	rw_exit(&fctl_ulp_lock);
2591 }
2592 
2593 
2594 static int
2595 fctl_pre_attach(fc_ulp_ports_t *ulp_port, fc_attach_cmd_t cmd)
2596 {
2597 	int rval = FC_SUCCESS;
2598 
2599 	mutex_enter(&ulp_port->port_mutex);
2600 
2601 	switch (cmd) {
2602 	case FC_CMD_ATTACH:
2603 		if (ulp_port->port_dstate & ULP_PORT_ATTACH) {
2604 			rval = FC_FAILURE;
2605 		}
2606 		break;
2607 
2608 	case FC_CMD_RESUME:
2609 		ASSERT((ulp_port->port_dstate & ULP_PORT_POWER_DOWN) == 0);
2610 		if (!(ulp_port->port_dstate & ULP_PORT_ATTACH) ||
2611 		    !(ulp_port->port_dstate & ULP_PORT_SUSPEND)) {
2612 			rval = FC_FAILURE;
2613 		}
2614 		break;
2615 
2616 	case FC_CMD_POWER_UP:
2617 		if (!(ulp_port->port_dstate & ULP_PORT_ATTACH) ||
2618 		    !(ulp_port->port_dstate & ULP_PORT_POWER_DOWN)) {
2619 			rval = FC_FAILURE;
2620 		}
2621 		break;
2622 	}
2623 
2624 	if (rval == FC_SUCCESS) {
2625 		ulp_port->port_dstate |= ULP_PORT_BUSY;
2626 	}
2627 	mutex_exit(&ulp_port->port_mutex);
2628 
2629 	return (rval);
2630 }
2631 
2632 
2633 static void
2634 fctl_post_attach(fc_ulp_module_t *mod, fc_ulp_ports_t *ulp_port,
2635     fc_attach_cmd_t cmd, int rval)
2636 {
2637 	int	be_chatty;
2638 
2639 	ASSERT(cmd == FC_CMD_ATTACH || cmd == FC_CMD_RESUME ||
2640 	    cmd == FC_CMD_POWER_UP);
2641 
2642 	mutex_enter(&ulp_port->port_mutex);
2643 	ulp_port->port_dstate &= ~ULP_PORT_BUSY;
2644 
2645 	be_chatty = (rval == FC_FAILURE_SILENT) ? 0 : 1;
2646 
2647 	if (rval != FC_SUCCESS) {
2648 		caddr_t		op;
2649 		fc_local_port_t *port = ulp_port->port_handle;
2650 
2651 		mutex_exit(&ulp_port->port_mutex);
2652 
2653 		switch (cmd) {
2654 		case FC_CMD_ATTACH:
2655 			op = "attach";
2656 			break;
2657 
2658 		case FC_CMD_RESUME:
2659 			op = "resume";
2660 			break;
2661 
2662 		case FC_CMD_POWER_UP:
2663 			op = "power up";
2664 			break;
2665 		}
2666 
2667 		if (be_chatty) {
2668 			cmn_err(CE_WARN, "!fctl(%d): %s failed for %s",
2669 			    port->fp_instance, op, mod->mod_info->ulp_name);
2670 		}
2671 
2672 		return;
2673 	}
2674 
2675 	switch (cmd) {
2676 	case FC_CMD_ATTACH:
2677 		ulp_port->port_dstate |= ULP_PORT_ATTACH;
2678 		break;
2679 
2680 	case FC_CMD_RESUME:
2681 		ulp_port->port_dstate &= ~ULP_PORT_SUSPEND;
2682 		break;
2683 
2684 	case FC_CMD_POWER_UP:
2685 		ulp_port->port_dstate &= ~ULP_PORT_POWER_DOWN;
2686 		break;
2687 	}
2688 	mutex_exit(&ulp_port->port_mutex);
2689 }
2690 
2691 
2692 int
2693 fctl_detach_ulps(fc_local_port_t *port, fc_detach_cmd_t cmd,
2694     struct modlinkage *linkage)
2695 {
2696 	int			rval = FC_SUCCESS;
2697 	fc_ulp_module_t		*mod;
2698 	fc_ulp_port_info_t	info;
2699 	fc_ulp_ports_t		*ulp_port;
2700 
2701 	ASSERT(!MUTEX_HELD(&port->fp_mutex));
2702 
2703 	info.port_linkage = linkage;
2704 	info.port_dip = port->fp_port_dip;
2705 	info.port_handle = (opaque_t)port;
2706 	info.port_acc_attr = port->fp_fca_tran->fca_acc_attr;
2707 	info.port_fca_pkt_size = port->fp_fca_tran->fca_pkt_size;
2708 
2709 	rw_enter(&fctl_ulp_lock, RW_READER);
2710 	rw_enter(&fctl_mod_ports_lock, RW_READER);
2711 
2712 	for (mod = fctl_ulp_modules; mod; mod = mod->mod_next) {
2713 		if ((ulp_port = fctl_get_ulp_port(mod, port)) == NULL) {
2714 			continue;
2715 		}
2716 
2717 		if (fctl_pre_detach(ulp_port, cmd) != FC_SUCCESS) {
2718 			continue;
2719 		}
2720 
2721 		fctl_init_dma_attr(port, mod, &info);
2722 
2723 		rval = mod->mod_info->ulp_port_detach(
2724 		    mod->mod_info->ulp_handle, &info, cmd);
2725 
2726 		fctl_post_detach(mod, ulp_port, cmd, rval);
2727 
2728 		if (rval != FC_SUCCESS) {
2729 			break;
2730 		}
2731 
2732 		if (cmd == FC_CMD_DETACH && strcmp(mod->mod_info->ulp_name,
2733 		    "fcp") == 0) {
2734 			ASSERT(ddi_get_driver_private(info.port_dip) == NULL);
2735 		}
2736 
2737 		mutex_enter(&ulp_port->port_mutex);
2738 		ulp_port->port_statec = FC_ULP_STATEC_DONT_CARE;
2739 		mutex_exit(&ulp_port->port_mutex);
2740 	}
2741 
2742 	rw_exit(&fctl_mod_ports_lock);
2743 	rw_exit(&fctl_ulp_lock);
2744 
2745 	return (rval);
2746 }
2747 
2748 static	void
2749 fctl_init_dma_attr(fc_local_port_t *port, fc_ulp_module_t *mod,
2750     fc_ulp_port_info_t	*info)
2751 {
2752 
2753 	if ((strcmp(mod->mod_info->ulp_name, "fcp") == 0) ||
2754 	    (strcmp(mod->mod_info->ulp_name, "ltct") == 0)) {
2755 		info->port_cmd_dma_attr =
2756 		    port->fp_fca_tran->fca_dma_fcp_cmd_attr;
2757 		info->port_data_dma_attr =
2758 		    port->fp_fca_tran->fca_dma_fcp_data_attr;
2759 		info->port_resp_dma_attr =
2760 		    port->fp_fca_tran->fca_dma_fcp_rsp_attr;
2761 	} else if (strcmp(mod->mod_info->ulp_name, "fcsm") == 0) {
2762 		info->port_cmd_dma_attr =
2763 		    port->fp_fca_tran->fca_dma_fcsm_cmd_attr;
2764 		info->port_data_dma_attr =
2765 		    port->fp_fca_tran->fca_dma_attr;
2766 		info->port_resp_dma_attr =
2767 		    port->fp_fca_tran->fca_dma_fcsm_rsp_attr;
2768 	} else if (strcmp(mod->mod_info->ulp_name, "fcip") == 0) {
2769 		info->port_cmd_dma_attr =
2770 		    port->fp_fca_tran->fca_dma_fcip_cmd_attr;
2771 		info->port_data_dma_attr =
2772 		    port->fp_fca_tran->fca_dma_attr;
2773 		info->port_resp_dma_attr =
2774 		    port->fp_fca_tran->fca_dma_fcip_rsp_attr;
2775 	} else {
2776 		info->port_cmd_dma_attr = info->port_data_dma_attr =
2777 		    info->port_resp_dma_attr =
2778 		    port->fp_fca_tran->fca_dma_attr; /* default */
2779 	}
2780 }
2781 
2782 static int
2783 fctl_pre_detach(fc_ulp_ports_t *ulp_port, fc_detach_cmd_t cmd)
2784 {
2785 	int rval = FC_SUCCESS;
2786 
2787 	mutex_enter(&ulp_port->port_mutex);
2788 
2789 	switch (cmd) {
2790 	case FC_CMD_DETACH:
2791 		if ((ulp_port->port_dstate & ULP_PORT_ATTACH) == 0) {
2792 			rval = FC_FAILURE;
2793 		}
2794 		break;
2795 
2796 	case FC_CMD_SUSPEND:
2797 		if (!(ulp_port->port_dstate & ULP_PORT_ATTACH) ||
2798 		    ulp_port->port_dstate & ULP_PORT_SUSPEND) {
2799 			rval = FC_FAILURE;
2800 		}
2801 		break;
2802 
2803 	case FC_CMD_POWER_DOWN:
2804 		if (!(ulp_port->port_dstate & ULP_PORT_ATTACH) ||
2805 		    ulp_port->port_dstate & ULP_PORT_POWER_DOWN) {
2806 			rval = FC_FAILURE;
2807 		}
2808 		break;
2809 	}
2810 
2811 	if (rval == FC_SUCCESS) {
2812 		ulp_port->port_dstate |= ULP_PORT_BUSY;
2813 	}
2814 	mutex_exit(&ulp_port->port_mutex);
2815 
2816 	return (rval);
2817 }
2818 
2819 
2820 static void
2821 fctl_post_detach(fc_ulp_module_t *mod, fc_ulp_ports_t *ulp_port,
2822     fc_detach_cmd_t cmd, int rval)
2823 {
2824 	ASSERT(cmd == FC_CMD_DETACH || cmd == FC_CMD_SUSPEND ||
2825 	    cmd == FC_CMD_POWER_DOWN);
2826 
2827 	mutex_enter(&ulp_port->port_mutex);
2828 	ulp_port->port_dstate &= ~ULP_PORT_BUSY;
2829 
2830 	if (rval != FC_SUCCESS) {
2831 		caddr_t		op;
2832 		fc_local_port_t *port = ulp_port->port_handle;
2833 
2834 		mutex_exit(&ulp_port->port_mutex);
2835 
2836 		switch (cmd) {
2837 		case FC_CMD_DETACH:
2838 			op = "detach";
2839 			break;
2840 
2841 		case FC_CMD_SUSPEND:
2842 			op = "suspend";
2843 			break;
2844 
2845 		case FC_CMD_POWER_DOWN:
2846 			op = "power down";
2847 			break;
2848 		}
2849 
2850 		cmn_err(CE_WARN, "!fctl(%d): %s failed for %s",
2851 		    port->fp_instance, op, mod->mod_info->ulp_name);
2852 
2853 		return;
2854 	}
2855 
2856 	switch (cmd) {
2857 	case FC_CMD_DETACH:
2858 		ulp_port->port_dstate &= ~ULP_PORT_ATTACH;
2859 		break;
2860 
2861 	case FC_CMD_SUSPEND:
2862 		ulp_port->port_dstate |= ULP_PORT_SUSPEND;
2863 		break;
2864 
2865 	case FC_CMD_POWER_DOWN:
2866 		ulp_port->port_dstate |= ULP_PORT_POWER_DOWN;
2867 		break;
2868 	}
2869 	mutex_exit(&ulp_port->port_mutex);
2870 }
2871 
2872 
2873 static fc_ulp_ports_t *
2874 fctl_add_ulp_port(fc_ulp_module_t *ulp_module, fc_local_port_t *port_handle,
2875     int sleep)
2876 {
2877 	fc_ulp_ports_t *last;
2878 	fc_ulp_ports_t *next;
2879 	fc_ulp_ports_t *new;
2880 
2881 	ASSERT(RW_READ_HELD(&fctl_ulp_lock));
2882 	ASSERT(RW_WRITE_HELD(&fctl_mod_ports_lock));
2883 
2884 	last = NULL;
2885 	next = ulp_module->mod_ports;
2886 
2887 	while (next != NULL) {
2888 		last = next;
2889 		next = next->port_next;
2890 	}
2891 
2892 	new = fctl_alloc_ulp_port(sleep);
2893 	if (new == NULL) {
2894 		return (new);
2895 	}
2896 
2897 	new->port_handle = port_handle;
2898 	if (last == NULL) {
2899 		ulp_module->mod_ports = new;
2900 	} else {
2901 		last->port_next = new;
2902 	}
2903 
2904 	return (new);
2905 }
2906 
2907 
2908 static fc_ulp_ports_t *
2909 fctl_alloc_ulp_port(int sleep)
2910 {
2911 	fc_ulp_ports_t *new;
2912 
2913 	new = kmem_zalloc(sizeof (*new), sleep);
2914 	if (new == NULL) {
2915 		return (new);
2916 	}
2917 	mutex_init(&new->port_mutex, NULL, MUTEX_DRIVER, NULL);
2918 
2919 	return (new);
2920 }
2921 
2922 
2923 static int
2924 fctl_remove_ulp_port(struct ulp_module *ulp_module,
2925     fc_local_port_t *port_handle)
2926 {
2927 	fc_ulp_ports_t *last;
2928 	fc_ulp_ports_t *next;
2929 
2930 	ASSERT(RW_WRITE_HELD(&fctl_ulp_lock));
2931 	ASSERT(RW_WRITE_HELD(&fctl_mod_ports_lock));
2932 
2933 	last = NULL;
2934 	next = ulp_module->mod_ports;
2935 
2936 	while (next != NULL) {
2937 		if (next->port_handle == port_handle) {
2938 			if (next->port_dstate & ULP_PORT_ATTACH) {
2939 				return (FC_FAILURE);
2940 			}
2941 			break;
2942 		}
2943 		last = next;
2944 		next = next->port_next;
2945 	}
2946 
2947 	if (next != NULL) {
2948 		ASSERT((next->port_dstate & ULP_PORT_ATTACH) == 0);
2949 
2950 		if (last == NULL) {
2951 			ulp_module->mod_ports = next->port_next;
2952 		} else {
2953 			last->port_next = next->port_next;
2954 		}
2955 		fctl_dealloc_ulp_port(next);
2956 
2957 		return (FC_SUCCESS);
2958 	} else {
2959 		return (FC_FAILURE);
2960 	}
2961 }
2962 
2963 
2964 static void
2965 fctl_dealloc_ulp_port(fc_ulp_ports_t *next)
2966 {
2967 	mutex_destroy(&next->port_mutex);
2968 	kmem_free(next, sizeof (*next));
2969 }
2970 
2971 
2972 static fc_ulp_ports_t *
2973 fctl_get_ulp_port(struct ulp_module *ulp_module, fc_local_port_t *port_handle)
2974 {
2975 	fc_ulp_ports_t *next;
2976 
2977 	ASSERT(RW_LOCK_HELD(&fctl_ulp_lock));
2978 	ASSERT(RW_LOCK_HELD(&fctl_mod_ports_lock));
2979 
2980 	for (next = ulp_module->mod_ports; next != NULL;
2981 	    next = next->port_next) {
2982 		if (next->port_handle == port_handle) {
2983 			return (next);
2984 		}
2985 	}
2986 
2987 	return (NULL);
2988 }
2989 
2990 
2991 /*
2992  * Pass state change notfications on to registered ULPs.
2993  *
2994  * Can issue wakeups to client callers who might be waiting for completions
2995  * on other threads.
2996  *
2997  * Caution: will silently deallocate any fc_remote_port_t and/or
2998  * fc_remote_node_t structs it finds that are not in use.
2999  */
3000 void
3001 fctl_ulp_statec_cb(void *arg)
3002 {
3003 	uint32_t		s_id;
3004 	uint32_t		new_state;
3005 	fc_local_port_t		*port;
3006 	fc_ulp_ports_t		*ulp_port;
3007 	fc_ulp_module_t		*mod;
3008 	fc_port_clist_t		*clist = (fc_port_clist_t *)arg;
3009 
3010 	ASSERT(clist != NULL);
3011 
3012 	port = clist->clist_port;
3013 
3014 	mutex_enter(&port->fp_mutex);
3015 	s_id = port->fp_port_id.port_id;
3016 	mutex_exit(&port->fp_mutex);
3017 
3018 	switch (clist->clist_state) {
3019 	case FC_STATE_ONLINE:
3020 		new_state = FC_ULP_STATEC_ONLINE;
3021 		break;
3022 
3023 	case FC_STATE_OFFLINE:
3024 		if (clist->clist_len) {
3025 			new_state = FC_ULP_STATEC_OFFLINE_TIMEOUT;
3026 		} else {
3027 			new_state = FC_ULP_STATEC_OFFLINE;
3028 		}
3029 		break;
3030 
3031 	default:
3032 		new_state = FC_ULP_STATEC_DONT_CARE;
3033 		break;
3034 	}
3035 
3036 #ifdef	DEBUG
3037 	/*
3038 	 * sanity check for presence of OLD devices in the hash lists
3039 	 */
3040 	if (clist->clist_size) {
3041 		int			count;
3042 		fc_remote_port_t	*pd;
3043 
3044 		ASSERT(clist->clist_map != NULL);
3045 		for (count = 0; count < clist->clist_len; count++) {
3046 			if (clist->clist_map[count].map_state ==
3047 			    PORT_DEVICE_INVALID) {
3048 				la_wwn_t	pwwn;
3049 				fc_portid_t	d_id;
3050 
3051 				pd = clist->clist_map[count].map_pd;
3052 				if (pd != NULL) {
3053 					mutex_enter(&pd->pd_mutex);
3054 					pwwn = pd->pd_port_name;
3055 					d_id = pd->pd_port_id;
3056 					mutex_exit(&pd->pd_mutex);
3057 
3058 					pd = fctl_get_remote_port_by_pwwn(port,
3059 					    &pwwn);
3060 
3061 					ASSERT(pd != clist->clist_map[count].
3062 					    map_pd);
3063 
3064 					pd = fctl_get_remote_port_by_did(port,
3065 					    d_id.port_id);
3066 					ASSERT(pd != clist->clist_map[count].
3067 					    map_pd);
3068 				}
3069 			}
3070 		}
3071 	}
3072 #endif
3073 
3074 	/*
3075 	 * Check for duplicate map entries
3076 	 */
3077 	if (clist->clist_size) {
3078 		int			count;
3079 		fc_remote_port_t	*pd1, *pd2;
3080 
3081 		ASSERT(clist->clist_map != NULL);
3082 		for (count = 0; count < clist->clist_len-1; count++) {
3083 			int count2;
3084 
3085 			pd1 = clist->clist_map[count].map_pd;
3086 			if (pd1 == NULL) {
3087 				continue;
3088 			}
3089 
3090 			for (count2 = count+1;
3091 			    count2 < clist->clist_len;
3092 			    count2++) {
3093 
3094 				pd2 = clist->clist_map[count2].map_pd;
3095 				if (pd2 == NULL) {
3096 					continue;
3097 				}
3098 
3099 				if (pd1 == pd2) {
3100 					clist->clist_map[count].map_flags |=
3101 					    PORT_DEVICE_DUPLICATE_MAP_ENTRY;
3102 					break;
3103 				}
3104 			}
3105 		}
3106 	}
3107 
3108 
3109 	rw_enter(&fctl_ulp_lock, RW_READER);
3110 	for (mod = fctl_ulp_modules; mod; mod = mod->mod_next) {
3111 		rw_enter(&fctl_mod_ports_lock, RW_READER);
3112 		ulp_port = fctl_get_ulp_port(mod, port);
3113 		rw_exit(&fctl_mod_ports_lock);
3114 
3115 		if (ulp_port == NULL) {
3116 			continue;
3117 		}
3118 
3119 		mutex_enter(&ulp_port->port_mutex);
3120 		if (FCTL_DISALLOW_CALLBACKS(ulp_port->port_dstate)) {
3121 			mutex_exit(&ulp_port->port_mutex);
3122 			continue;
3123 		}
3124 
3125 		switch (ulp_port->port_statec) {
3126 		case FC_ULP_STATEC_DONT_CARE:
3127 			if (ulp_port->port_statec != new_state) {
3128 				ulp_port->port_statec = new_state;
3129 			}
3130 			break;
3131 
3132 		case FC_ULP_STATEC_ONLINE:
3133 		case FC_ULP_STATEC_OFFLINE:
3134 			if (ulp_port->port_statec == new_state) {
3135 				mutex_exit(&ulp_port->port_mutex);
3136 				continue;
3137 			}
3138 			ulp_port->port_statec = new_state;
3139 			break;
3140 
3141 		case FC_ULP_STATEC_OFFLINE_TIMEOUT:
3142 			if (ulp_port->port_statec == new_state ||
3143 			    new_state == FC_ULP_STATEC_OFFLINE) {
3144 				mutex_exit(&ulp_port->port_mutex);
3145 				continue;
3146 			}
3147 			ulp_port->port_statec = new_state;
3148 			break;
3149 
3150 		default:
3151 			ASSERT(0);
3152 			break;
3153 		}
3154 
3155 		mod->mod_info->ulp_statec_callback(
3156 		    mod->mod_info->ulp_handle, (opaque_t)port,
3157 		    clist->clist_state, clist->clist_flags,
3158 		    clist->clist_map, clist->clist_len, s_id);
3159 
3160 		mutex_exit(&ulp_port->port_mutex);
3161 	}
3162 	rw_exit(&fctl_ulp_lock);
3163 
3164 	if (clist->clist_size) {
3165 		int			count;
3166 		fc_remote_node_t	*node;
3167 		fc_remote_port_t	*pd;
3168 
3169 		ASSERT(clist->clist_map != NULL);
3170 		for (count = 0; count < clist->clist_len; count++) {
3171 
3172 			if ((pd = clist->clist_map[count].map_pd) == NULL) {
3173 				continue;
3174 			}
3175 
3176 			mutex_enter(&pd->pd_mutex);
3177 
3178 			pd->pd_ref_count--;
3179 			ASSERT(pd->pd_ref_count >= 0);
3180 
3181 			if (clist->clist_map[count].map_state !=
3182 			    PORT_DEVICE_INVALID) {
3183 				mutex_exit(&pd->pd_mutex);
3184 				continue;
3185 			}
3186 
3187 			node = pd->pd_remote_nodep;
3188 			pd->pd_aux_flags &= ~PD_GIVEN_TO_ULPS;
3189 
3190 			mutex_exit(&pd->pd_mutex);
3191 
3192 			/*
3193 			 * This fc_remote_port_t is no longer referenced
3194 			 * by any ULPs. Deallocate it if its pd_ref_count
3195 			 * has reached zero.
3196 			 */
3197 			if ((fctl_destroy_remote_port(port, pd) == 0) &&
3198 			    (node != NULL)) {
3199 				fctl_destroy_remote_node(node);
3200 			}
3201 		}
3202 
3203 		kmem_free(clist->clist_map,
3204 		    sizeof (*(clist->clist_map)) * clist->clist_size);
3205 	}
3206 
3207 	if (clist->clist_wait) {
3208 		mutex_enter(&clist->clist_mutex);
3209 		clist->clist_wait = 0;
3210 		cv_signal(&clist->clist_cv);
3211 		mutex_exit(&clist->clist_mutex);
3212 	} else {
3213 		kmem_free(clist, sizeof (*clist));
3214 	}
3215 }
3216 
3217 
3218 /*
3219  * Allocate an fc_remote_node_t struct to represent a remote node for the
3220  * given nwwn.	This will also add the nwwn to the global nwwn table.
3221  *
3222  * Returns a pointer to the newly-allocated struct.  Returns NULL if
3223  * the kmem_zalloc fails or if the enlist_wwn attempt fails.
3224  */
3225 fc_remote_node_t *
3226 fctl_create_remote_node(la_wwn_t *nwwn, int sleep)
3227 {
3228 	fc_remote_node_t *rnodep;
3229 
3230 	if ((rnodep = kmem_zalloc(sizeof (*rnodep), sleep)) == NULL) {
3231 		return (NULL);
3232 	}
3233 
3234 	mutex_init(&rnodep->fd_mutex, NULL, MUTEX_DRIVER, NULL);
3235 
3236 	rnodep->fd_node_name = *nwwn;
3237 	rnodep->fd_flags = FC_REMOTE_NODE_VALID;
3238 	rnodep->fd_numports = 1;
3239 
3240 	if (fctl_enlist_nwwn_table(rnodep, sleep) != FC_SUCCESS) {
3241 		mutex_destroy(&rnodep->fd_mutex);
3242 		kmem_free(rnodep, sizeof (*rnodep));
3243 		return (NULL);
3244 	}
3245 
3246 	return (rnodep);
3247 }
3248 
3249 /*
3250  * Deconstruct and free the given fc_remote_node_t struct (remote node struct).
3251  * Silently skips the deconstruct/free if there are any fc_remote_port_t
3252  * (remote port device) structs still referenced by the given
3253  * fc_remote_node_t struct.
3254  */
3255 void
3256 fctl_destroy_remote_node(fc_remote_node_t *rnodep)
3257 {
3258 	mutex_enter(&rnodep->fd_mutex);
3259 
3260 	/*
3261 	 * Look at the count and linked list of of remote ports
3262 	 * (fc_remote_port_t structs); bail if these indicate that
3263 	 * given fc_remote_node_t may be in use.
3264 	 */
3265 	if (rnodep->fd_numports != 0 || rnodep->fd_portlistp) {
3266 		mutex_exit(&rnodep->fd_mutex);
3267 		return;
3268 	}
3269 
3270 	mutex_exit(&rnodep->fd_mutex);
3271 
3272 	mutex_destroy(&rnodep->fd_mutex);
3273 	kmem_free(rnodep, sizeof (*rnodep));
3274 }
3275 
3276 
3277 /*
3278  * Add the given fc_remote_node_t to the global fctl_nwwn_hash_table[]. This
3279  * uses the nwwn in the fd_node_name.raw_wwn of the given struct.
3280  * This only fails if the kmem_zalloc fails.  This does not check for a
3281  * unique or pre-existing nwwn in the fctl_nwwn_hash_table[].
3282  * This is only called from fctl_create_remote_node().
3283  */
3284 int
3285 fctl_enlist_nwwn_table(fc_remote_node_t *rnodep, int sleep)
3286 {
3287 	int			index;
3288 	fctl_nwwn_elem_t	*new;
3289 	fctl_nwwn_list_t	*head;
3290 
3291 	ASSERT(!MUTEX_HELD(&rnodep->fd_mutex));
3292 
3293 	if ((new = kmem_zalloc(sizeof (*new), sleep)) == NULL) {
3294 		return (FC_FAILURE);
3295 	}
3296 
3297 	mutex_enter(&fctl_nwwn_hash_mutex);
3298 	new->fne_nodep = rnodep;
3299 
3300 	mutex_enter(&rnodep->fd_mutex);
3301 	ASSERT(fctl_is_wwn_zero(&rnodep->fd_node_name) == FC_FAILURE);
3302 	index = HASH_FUNC(WWN_HASH_KEY(rnodep->fd_node_name.raw_wwn),
3303 	    fctl_nwwn_table_size);
3304 	mutex_exit(&rnodep->fd_mutex);
3305 
3306 	head = &fctl_nwwn_hash_table[index];
3307 
3308 	/* Link it in at the head of the hash list */
3309 	new->fne_nextp = head->fnl_headp;
3310 	head->fnl_headp = new;
3311 
3312 	mutex_exit(&fctl_nwwn_hash_mutex);
3313 
3314 	return (FC_SUCCESS);
3315 }
3316 
3317 
3318 /*
3319  * Remove the given fc_remote_node_t from the global fctl_nwwn_hash_table[].
3320  * This uses the nwwn in the fd_node_name.raw_wwn of the given struct.
3321  */
3322 void
3323 fctl_delist_nwwn_table(fc_remote_node_t *rnodep)
3324 {
3325 	int			index;
3326 	fctl_nwwn_list_t	*head;
3327 	fctl_nwwn_elem_t	*elem;
3328 	fctl_nwwn_elem_t	*prev;
3329 
3330 	ASSERT(MUTEX_HELD(&fctl_nwwn_hash_mutex));
3331 	ASSERT(MUTEX_HELD(&rnodep->fd_mutex));
3332 
3333 	index = HASH_FUNC(WWN_HASH_KEY(rnodep->fd_node_name.raw_wwn),
3334 	    fctl_nwwn_table_size);
3335 
3336 	head = &fctl_nwwn_hash_table[index];
3337 	elem = head->fnl_headp;
3338 	prev = NULL;
3339 
3340 	while (elem != NULL) {
3341 		if (elem->fne_nodep == rnodep) {
3342 			/*
3343 			 * Found it -- unlink it from the list & decrement
3344 			 * the count for the hash chain.
3345 			 */
3346 			if (prev == NULL) {
3347 				head->fnl_headp = elem->fne_nextp;
3348 			} else {
3349 				prev->fne_nextp = elem->fne_nextp;
3350 			}
3351 			break;
3352 		}
3353 		prev = elem;
3354 		elem = elem->fne_nextp;
3355 	}
3356 
3357 	if (elem != NULL) {
3358 		kmem_free(elem, sizeof (*elem));
3359 	}
3360 }
3361 
3362 
3363 /*
3364  * Returns a reference to an fc_remote_node_t struct for the given node_wwn.
3365  * Looks in the global fctl_nwwn_hash_table[]. Identical to the
3366  * fctl_lock_remote_node_by_nwwn() function, except that this does NOT increment
3367  * the fc_count reference count in the f_device_t before returning.
3368  *
3369  * This function is called by: fctl_create_remote_port_t().
3370  *
3371  * OLD COMMENT:
3372  * Note: The calling thread needs to make sure it isn't holding any device
3373  * mutex (more so the fc_remote_node_t that could potentially have this wwn).
3374  */
3375 fc_remote_node_t *
3376 fctl_get_remote_node_by_nwwn(la_wwn_t *node_wwn)
3377 {
3378 	int			index;
3379 	fctl_nwwn_elem_t	*elem;
3380 	fc_remote_node_t	*next;
3381 	fc_remote_node_t	*rnodep = NULL;
3382 
3383 	index = HASH_FUNC(WWN_HASH_KEY(node_wwn->raw_wwn),
3384 	    fctl_nwwn_table_size);
3385 	ASSERT(index >= 0 && index < fctl_nwwn_table_size);
3386 
3387 	mutex_enter(&fctl_nwwn_hash_mutex);
3388 	elem = fctl_nwwn_hash_table[index].fnl_headp;
3389 	while (elem != NULL) {
3390 		next = elem->fne_nodep;
3391 		if (next != NULL) {
3392 			mutex_enter(&next->fd_mutex);
3393 			if (fctl_wwn_cmp(node_wwn, &next->fd_node_name) == 0) {
3394 				rnodep = next;
3395 				mutex_exit(&next->fd_mutex);
3396 				break;
3397 			}
3398 			mutex_exit(&next->fd_mutex);
3399 		}
3400 		elem = elem->fne_nextp;
3401 	}
3402 	mutex_exit(&fctl_nwwn_hash_mutex);
3403 
3404 	return (rnodep);
3405 }
3406 
3407 
3408 /*
3409  * Returns a reference to an fc_remote_node_t struct for the given node_wwn.
3410  * Looks in the global fctl_nwwn_hash_table[]. Increments the fd_numports
3411  * reference count in the f_device_t before returning.
3412  *
3413  * This function is only called by fctl_create_remote_port_t().
3414  */
3415 fc_remote_node_t *
3416 fctl_lock_remote_node_by_nwwn(la_wwn_t *node_wwn)
3417 {
3418 	int			index;
3419 	fctl_nwwn_elem_t	*elem;
3420 	fc_remote_node_t	*next;
3421 	fc_remote_node_t	*rnodep = NULL;
3422 
3423 	index = HASH_FUNC(WWN_HASH_KEY(node_wwn->raw_wwn),
3424 	    fctl_nwwn_table_size);
3425 	ASSERT(index >= 0 && index < fctl_nwwn_table_size);
3426 
3427 	mutex_enter(&fctl_nwwn_hash_mutex);
3428 	elem = fctl_nwwn_hash_table[index].fnl_headp;
3429 	while (elem != NULL) {
3430 		next = elem->fne_nodep;
3431 		if (next != NULL) {
3432 			mutex_enter(&next->fd_mutex);
3433 			if (fctl_wwn_cmp(node_wwn, &next->fd_node_name) == 0) {
3434 				rnodep = next;
3435 				rnodep->fd_numports++;
3436 				mutex_exit(&next->fd_mutex);
3437 				break;
3438 			}
3439 			mutex_exit(&next->fd_mutex);
3440 		}
3441 		elem = elem->fne_nextp;
3442 	}
3443 	mutex_exit(&fctl_nwwn_hash_mutex);
3444 
3445 	return (rnodep);
3446 }
3447 
3448 
3449 /*
3450  * Allocate and initialize an fc_remote_port_t struct & returns a pointer to
3451  * the newly allocated struct.	Only fails if the kmem_zalloc() fails.
3452  */
3453 fc_remote_port_t *
3454 fctl_alloc_remote_port(fc_local_port_t *port, la_wwn_t *port_wwn,
3455     uint32_t d_id, uchar_t recepient, int sleep)
3456 {
3457 	fc_remote_port_t *pd;
3458 
3459 	ASSERT(MUTEX_HELD(&port->fp_mutex));
3460 	ASSERT(FC_IS_REAL_DEVICE(d_id));
3461 
3462 	if ((pd = kmem_zalloc(sizeof (*pd), sleep)) == NULL) {
3463 		return (NULL);
3464 	}
3465 	fctl_tc_constructor(&pd->pd_logo_tc, FC_LOGO_TOLERANCE_LIMIT,
3466 	    FC_LOGO_TOLERANCE_TIME_LIMIT);
3467 
3468 	mutex_init(&pd->pd_mutex, NULL, MUTEX_DRIVER, NULL);
3469 
3470 	pd->pd_port_id.port_id = d_id;
3471 	pd->pd_port_name = *port_wwn;
3472 	pd->pd_port = port;
3473 	pd->pd_state = PORT_DEVICE_VALID;
3474 	pd->pd_type = PORT_DEVICE_NEW;
3475 	pd->pd_recepient = recepient;
3476 
3477 	return (pd);
3478 }
3479 
3480 
3481 /*
3482  * Deconstruct and free the given fc_remote_port_t struct (unconditionally).
3483  */
3484 void
3485 fctl_dealloc_remote_port(fc_remote_port_t *pd)
3486 {
3487 	ASSERT(!MUTEX_HELD(&pd->pd_mutex));
3488 
3489 	fctl_tc_destructor(&pd->pd_logo_tc);
3490 	mutex_destroy(&pd->pd_mutex);
3491 	kmem_free(pd, sizeof (*pd));
3492 }
3493 
3494 /*
3495  * Add the given fc_remote_port_t onto the linked list of remote port
3496  * devices associated with the given fc_remote_node_t. Does NOT add the
3497  * fc_remote_port_t to the list if already exists on the list.
3498  */
3499 void
3500 fctl_link_remote_port_to_remote_node(fc_remote_node_t *rnodep,
3501     fc_remote_port_t *pd)
3502 {
3503 	fc_remote_port_t *last;
3504 	fc_remote_port_t *ports;
3505 
3506 	mutex_enter(&rnodep->fd_mutex);
3507 
3508 	last = NULL;
3509 	for (ports = rnodep->fd_portlistp; ports != NULL;
3510 	    ports = ports->pd_port_next) {
3511 		if (ports == pd) {
3512 			/*
3513 			 * The given fc_remote_port_t is already on the linked
3514 			 * list chain for the given remote node, so bail now.
3515 			 */
3516 			mutex_exit(&rnodep->fd_mutex);
3517 			return;
3518 		}
3519 		last = ports;
3520 	}
3521 
3522 	/* Add the fc_remote_port_t to the tail of the linked list */
3523 	if (last != NULL) {
3524 		last->pd_port_next = pd;
3525 	} else {
3526 		rnodep->fd_portlistp = pd;
3527 	}
3528 	pd->pd_port_next = NULL;
3529 
3530 	/*
3531 	 * Link the fc_remote_port_t back to the associated fc_remote_node_t.
3532 	 */
3533 	mutex_enter(&pd->pd_mutex);
3534 	pd->pd_remote_nodep = rnodep;
3535 	mutex_exit(&pd->pd_mutex);
3536 
3537 	mutex_exit(&rnodep->fd_mutex);
3538 }
3539 
3540 
3541 /*
3542  * Remove the specified fc_remote_port_t from the linked list of remote ports
3543  * for the given fc_remote_node_t.
3544  *
3545  * Returns a count of the _remaining_ fc_remote_port_t structs on the linked
3546  * list of the fc_remote_node_t.
3547  *
3548  * The fd_numports on the given fc_remote_node_t is decremented, and if
3549  * it hits zero then this function also removes the fc_remote_node_t from the
3550  * global fctl_nwwn_hash_table[]. This appears to be the ONLY WAY that entries
3551  * are removed from the fctl_nwwn_hash_table[].
3552  */
3553 int
3554 fctl_unlink_remote_port_from_remote_node(fc_remote_node_t *rnodep,
3555     fc_remote_port_t *pd)
3556 {
3557 	int			rcount = 0;
3558 	fc_remote_port_t	*last;
3559 	fc_remote_port_t	*ports;
3560 
3561 	ASSERT(!MUTEX_HELD(&rnodep->fd_mutex));
3562 	ASSERT(!MUTEX_HELD(&pd->pd_mutex));
3563 
3564 	last = NULL;
3565 
3566 	mutex_enter(&fctl_nwwn_hash_mutex);
3567 
3568 	mutex_enter(&rnodep->fd_mutex);
3569 
3570 	/*
3571 	 * Go thru the linked list of fc_remote_port_t structs for the given
3572 	 * fc_remote_node_t; try to find the specified fc_remote_port_t (pd).
3573 	 */
3574 	ports = rnodep->fd_portlistp;
3575 	while (ports != NULL) {
3576 		if (ports == pd) {
3577 			break;	/* Found the requested fc_remote_port_t */
3578 		}
3579 		last = ports;
3580 		ports = ports->pd_port_next;
3581 	}
3582 
3583 	if (ports) {
3584 		rcount = --rnodep->fd_numports;
3585 		if (rcount == 0) {
3586 			/* Note: this is only ever called from here */
3587 			fctl_delist_nwwn_table(rnodep);
3588 		}
3589 		if (last) {
3590 			last->pd_port_next = pd->pd_port_next;
3591 		} else {
3592 			rnodep->fd_portlistp = pd->pd_port_next;
3593 		}
3594 		mutex_enter(&pd->pd_mutex);
3595 		pd->pd_remote_nodep = NULL;
3596 		mutex_exit(&pd->pd_mutex);
3597 	}
3598 
3599 	pd->pd_port_next = NULL;
3600 
3601 	mut