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