xref: /titanic_41/usr/src/uts/common/io/fibre-channel/ulp/fcp.c (revision 26fd77009b17f8c8fb32eb362584cfd635e87ad9)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Fibre Channel SCSI ULP Mapping driver
26  */
27 
28 #include <sys/scsi/scsi.h>
29 #include <sys/types.h>
30 #include <sys/varargs.h>
31 #include <sys/devctl.h>
32 #include <sys/thread.h>
33 #include <sys/thread.h>
34 #include <sys/open.h>
35 #include <sys/file.h>
36 #include <sys/sunndi.h>
37 #include <sys/console.h>
38 #include <sys/proc.h>
39 #include <sys/time.h>
40 #include <sys/utsname.h>
41 #include <sys/scsi/impl/scsi_reset_notify.h>
42 #include <sys/ndi_impldefs.h>
43 #include <sys/byteorder.h>
44 #include <sys/fs/dv_node.h>
45 #include <sys/ctype.h>
46 #include <sys/sunmdi.h>
47 
48 #include <sys/fibre-channel/fc.h>
49 #include <sys/fibre-channel/impl/fc_ulpif.h>
50 #include <sys/fibre-channel/ulp/fcpvar.h>
51 
52 /*
53  * Discovery Process
54  * =================
55  *
56  *    The discovery process is a major function of FCP.	 In order to help
57  * understand that function a flow diagram is given here.  This diagram
58  * doesn't claim to cover all the cases and the events that can occur during
59  * the discovery process nor the subtleties of the code.  The code paths shown
60  * are simplified.  Its purpose is to help the reader (and potentially bug
61  * fixer) have an overall view of the logic of the code.  For that reason the
62  * diagram covers the simple case of the line coming up cleanly or of a new
63  * port attaching to FCP the link being up.  The reader must keep in mind
64  * that:
65  *
66  *	- There are special cases where bringing devices online and offline
67  *	  is driven by Ioctl.
68  *
69  *	- The behavior of the discovery process can be modified through the
70  *	  .conf file.
71  *
72  *	- The line can go down and come back up at any time during the
73  *	  discovery process which explains some of the complexity of the code.
74  *
75  * ............................................................................
76  *
77  * STEP 1: The line comes up or a new Fibre Channel port attaches to FCP.
78  *
79  *
80  *			+-------------------------+
81  *   fp/fctl module --->|    fcp_port_attach	  |
82  *			+-------------------------+
83  *	   |			     |
84  *	   |			     |
85  *	   |			     v
86  *	   |		+-------------------------+
87  *	   |		| fcp_handle_port_attach  |
88  *	   |		+-------------------------+
89  *	   |				|
90  *	   |				|
91  *	   +--------------------+	|
92  *				|	|
93  *				v	v
94  *			+-------------------------+
95  *			|   fcp_statec_callback	  |
96  *			+-------------------------+
97  *				    |
98  *				    |
99  *				    v
100  *			+-------------------------+
101  *			|    fcp_handle_devices	  |
102  *			+-------------------------+
103  *				    |
104  *				    |
105  *				    v
106  *			+-------------------------+
107  *			|   fcp_handle_mapflags	  |
108  *			+-------------------------+
109  *				    |
110  *				    |
111  *				    v
112  *			+-------------------------+
113  *			|     fcp_send_els	  |
114  *			|			  |
115  *			| PLOGI or PRLI To all the|
116  *			| reachable devices.	  |
117  *			+-------------------------+
118  *
119  *
120  * ............................................................................
121  *
122  * STEP 2: The callback functions of the PLOGI and/or PRLI requests sent during
123  *	   STEP 1 are called (it is actually the same function).
124  *
125  *
126  *			+-------------------------+
127  *			|    fcp_icmd_callback	  |
128  *   fp/fctl module --->|			  |
129  *			| callback for PLOGI and  |
130  *			| PRLI.			  |
131  *			+-------------------------+
132  *				     |
133  *				     |
134  *	    Received PLOGI Accept   /-\	  Received PRLI Accept
135  *		       _ _ _ _ _ _ /   \_ _ _ _ _ _
136  *		      |		   \   /	   |
137  *		      |		    \-/		   |
138  *		      |				   |
139  *		      v				   v
140  *	+-------------------------+	+-------------------------+
141  *	|     fcp_send_els	  |	|     fcp_send_scsi	  |
142  *	|			  |	|			  |
143  *	|	  PRLI		  |	|	REPORT_LUN	  |
144  *	+-------------------------+	+-------------------------+
145  *
146  * ............................................................................
147  *
148  * STEP 3: The callback functions of the SCSI commands issued by FCP are called
149  *	   (It is actually the same function).
150  *
151  *
152  *			    +-------------------------+
153  *   fp/fctl module ------->|	 fcp_scsi_callback    |
154  *			    +-------------------------+
155  *					|
156  *					|
157  *					|
158  *	Receive REPORT_LUN reply       /-\	Receive INQUIRY PAGE83 reply
159  *		  _ _ _ _ _ _ _ _ _ _ /	  \_ _ _ _ _ _ _ _ _ _ _ _
160  *		 |		      \	  /			  |
161  *		 |		       \-/			  |
162  *		 |			|			  |
163  *		 | Receive INQUIRY reply|			  |
164  *		 |			|			  |
165  *		 v			v			  v
166  * +------------------------+ +----------------------+ +----------------------+
167  * |  fcp_handle_reportlun  | |	 fcp_handle_inquiry  | |  fcp_handle_page83   |
168  * |(Called for each Target)| | (Called for each LUN)| |(Called for each LUN) |
169  * +------------------------+ +----------------------+ +----------------------+
170  *		 |			|			  |
171  *		 |			|			  |
172  *		 |			|			  |
173  *		 v			v			  |
174  *     +-----------------+	+-----------------+		  |
175  *     |  fcp_send_scsi	 |	|  fcp_send_scsi  |		  |
176  *     |		 |	|		  |		  |
177  *     |     INQUIRY	 |	| INQUIRY PAGE83  |		  |
178  *     |  (To each LUN)	 |	+-----------------+		  |
179  *     +-----------------+					  |
180  *								  |
181  *								  v
182  *						      +------------------------+
183  *						      |	 fcp_call_finish_init  |
184  *						      +------------------------+
185  *								  |
186  *								  v
187  *						 +-----------------------------+
188  *						 |  fcp_call_finish_init_held  |
189  *						 +-----------------------------+
190  *								  |
191  *								  |
192  *			   All LUNs scanned			 /-\
193  *			       _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ /   \
194  *			      |					\   /
195  *			      |					 \-/
196  *			      v					  |
197  *		     +------------------+			  |
198  *		     |	fcp_finish_tgt	|			  |
199  *		     +------------------+			  |
200  *			      |	  Target Not Offline and	  |
201  *  Target Not Offline and    |	  not marked and tgt_node_state	  |
202  *  marked		     /-\  not FCP_TGT_NODE_ON_DEMAND	  |
203  *		_ _ _ _ _ _ /	\_ _ _ _ _ _ _ _		  |
204  *	       |	    \	/		|		  |
205  *	       |	     \-/		|		  |
206  *	       v				v		  |
207  * +----------------------------+     +-------------------+	  |
208  * |	 fcp_offline_target	|     |	 fcp_create_luns  |	  |
209  * |				|     +-------------------+	  |
210  * | A structure fcp_tgt_elem	|		|		  |
211  * | is created and queued in	|		v		  |
212  * | the FCP port list		|     +-------------------+	  |
213  * | port_offline_tgts.	 It	|     |	 fcp_pass_to_hp	  |	  |
214  * | will be unqueued by the	|     |			  |	  |
215  * | watchdog timer.		|     | Called for each	  |	  |
216  * +----------------------------+     | LUN. Dispatches	  |	  |
217  *		  |		      | fcp_hp_task	  |	  |
218  *		  |		      +-------------------+	  |
219  *		  |				|		  |
220  *		  |				|		  |
221  *		  |				|		  |
222  *		  |				+---------------->|
223  *		  |						  |
224  *		  +---------------------------------------------->|
225  *								  |
226  *								  |
227  *		All the targets (devices) have been scanned	 /-\
228  *				_ _ _ _	_ _ _ _	_ _ _ _ _ _ _ _ /   \
229  *			       |				\   /
230  *			       |				 \-/
231  *	    +-------------------------------------+		  |
232  *	    |		fcp_finish_init		  |		  |
233  *	    |					  |		  |
234  *	    | Signal broadcasts the condition	  |		  |
235  *	    | variable port_config_cv of the FCP  |		  |
236  *	    | port.  One potential code sequence  |		  |
237  *	    | waiting on the condition variable	  |		  |
238  *	    | the code sequence handling	  |		  |
239  *	    | BUS_CONFIG_ALL and BUS_CONFIG_DRIVER|		  |
240  *	    | The other is in the function	  |		  |
241  *	    | fcp_reconfig_wait which is called	  |		  |
242  *	    | in the transmit path preventing IOs |		  |
243  *	    | from going through till the disco-  |		  |
244  *	    | very process is over.		  |		  |
245  *	    +-------------------------------------+		  |
246  *			       |				  |
247  *			       |				  |
248  *			       +--------------------------------->|
249  *								  |
250  *								  v
251  *								Return
252  *
253  * ............................................................................
254  *
255  * STEP 4: The hot plug task is called (for each fcp_hp_elem).
256  *
257  *
258  *			+-------------------------+
259  *			|      fcp_hp_task	  |
260  *			+-------------------------+
261  *				     |
262  *				     |
263  *				     v
264  *			+-------------------------+
265  *			|     fcp_trigger_lun	  |
266  *			+-------------------------+
267  *				     |
268  *				     |
269  *				     v
270  *		   Bring offline    /-\	 Bring online
271  *		  _ _ _ _ _ _ _ _ _/   \_ _ _ _ _ _ _ _ _ _
272  *		 |		   \   /		   |
273  *		 |		    \-/			   |
274  *		 v					   v
275  *    +---------------------+			+-----------------------+
276  *    |	 fcp_offline_child  |			|      fcp_get_cip	|
277  *    +---------------------+			|			|
278  *						| Creates a dev_info_t	|
279  *						| or a mdi_pathinfo_t	|
280  *						| depending on whether	|
281  *						| mpxio is on or off.	|
282  *						+-----------------------+
283  *							   |
284  *							   |
285  *							   v
286  *						+-----------------------+
287  *						|  fcp_online_child	|
288  *						|			|
289  *						| Set device online	|
290  *						| using NDI or MDI.	|
291  *						+-----------------------+
292  *
293  * ............................................................................
294  *
295  * STEP 5: The watchdog timer expires.	The watch dog timer does much more that
296  *	   what is described here.  We only show the target offline path.
297  *
298  *
299  *			 +--------------------------+
300  *			 |	  fcp_watch	    |
301  *			 +--------------------------+
302  *				       |
303  *				       |
304  *				       v
305  *			 +--------------------------+
306  *			 |  fcp_scan_offline_tgts   |
307  *			 +--------------------------+
308  *				       |
309  *				       |
310  *				       v
311  *			 +--------------------------+
312  *			 |  fcp_offline_target_now  |
313  *			 +--------------------------+
314  *				       |
315  *				       |
316  *				       v
317  *			 +--------------------------+
318  *			 |   fcp_offline_tgt_luns   |
319  *			 +--------------------------+
320  *				       |
321  *				       |
322  *				       v
323  *			 +--------------------------+
324  *			 |     fcp_offline_lun	    |
325  *			 +--------------------------+
326  *				       |
327  *				       |
328  *				       v
329  *		     +----------------------------------+
330  *		     |	     fcp_offline_lun_now	|
331  *		     |					|
332  *		     | A request (or two if mpxio) is	|
333  *		     | sent to the hot plug task using	|
334  *		     | a fcp_hp_elem structure.		|
335  *		     +----------------------------------+
336  */
337 
338 /*
339  * Functions registered with DDI framework
340  */
341 static int fcp_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
342 static int fcp_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
343 static int fcp_open(dev_t *devp, int flag, int otype, cred_t *credp);
344 static int fcp_close(dev_t dev, int flag, int otype, cred_t *credp);
345 static int fcp_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
346     cred_t *credp, int *rval);
347 
348 /*
349  * Functions registered with FC Transport framework
350  */
351 static int fcp_port_attach(opaque_t ulph, fc_ulp_port_info_t *pinfo,
352     fc_attach_cmd_t cmd,  uint32_t s_id);
353 static int fcp_port_detach(opaque_t ulph, fc_ulp_port_info_t *info,
354     fc_detach_cmd_t cmd);
355 static int fcp_port_ioctl(opaque_t ulph, opaque_t port_handle, dev_t dev,
356     int cmd, intptr_t data, int mode, cred_t *credp, int *rval,
357     uint32_t claimed);
358 static int fcp_els_callback(opaque_t ulph, opaque_t port_handle,
359     fc_unsol_buf_t *buf, uint32_t claimed);
360 static int fcp_data_callback(opaque_t ulph, opaque_t port_handle,
361     fc_unsol_buf_t *buf, uint32_t claimed);
362 static void fcp_statec_callback(opaque_t ulph, opaque_t port_handle,
363     uint32_t port_state, uint32_t port_top, fc_portmap_t *devlist,
364     uint32_t  dev_cnt, uint32_t port_sid);
365 
366 /*
367  * Functions registered with SCSA framework
368  */
369 static int fcp_phys_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
370     scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
371 static int fcp_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
372     scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
373 static void fcp_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
374     scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
375 static int fcp_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt);
376 static int fcp_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt);
377 static int fcp_scsi_reset(struct scsi_address *ap, int level);
378 static int fcp_scsi_getcap(struct scsi_address *ap, char *cap, int whom);
379 static int fcp_scsi_setcap(struct scsi_address *ap, char *cap, int value,
380     int whom);
381 static void fcp_pkt_teardown(struct scsi_pkt *pkt);
382 static int fcp_scsi_reset_notify(struct scsi_address *ap, int flag,
383     void (*callback)(caddr_t), caddr_t arg);
384 static int fcp_scsi_bus_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
385     char *name, ddi_eventcookie_t *event_cookiep);
386 static int fcp_scsi_bus_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
387     ddi_eventcookie_t eventid, void (*callback)(), void *arg,
388     ddi_callback_id_t *cb_id);
389 static int fcp_scsi_bus_remove_eventcall(dev_info_t *devi,
390     ddi_callback_id_t cb_id);
391 static int fcp_scsi_bus_post_event(dev_info_t *dip, dev_info_t *rdip,
392     ddi_eventcookie_t eventid, void *impldata);
393 static int fcp_scsi_bus_config(dev_info_t *parent, uint_t flag,
394     ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
395 static int fcp_scsi_bus_unconfig(dev_info_t *parent, uint_t flag,
396     ddi_bus_config_op_t op, void *arg);
397 
398 /*
399  * Internal functions
400  */
401 static int fcp_setup_device_data_ioctl(int cmd, struct fcp_ioctl *data,
402     int mode, int *rval);
403 
404 static int fcp_setup_scsi_ioctl(struct fcp_scsi_cmd *u_fscsi,
405     int mode, int *rval);
406 static int fcp_copyin_scsi_cmd(caddr_t base_addr,
407     struct fcp_scsi_cmd *fscsi, int mode);
408 static int fcp_copyout_scsi_cmd(struct fcp_scsi_cmd *fscsi,
409     caddr_t base_addr, int mode);
410 static int fcp_send_scsi_ioctl(struct fcp_scsi_cmd *fscsi);
411 
412 static struct fcp_tgt *fcp_port_create_tgt(struct fcp_port *pptr,
413     la_wwn_t *pwwn, int	*ret_val, int *fc_status, int *fc_pkt_state,
414     int *fc_pkt_reason, int *fc_pkt_action);
415 static int fcp_tgt_send_plogi(struct fcp_tgt *ptgt, int *fc_status,
416     int *fc_pkt_state, int *fc_pkt_reason, int *fc_pkt_action);
417 static int fcp_tgt_send_prli(struct fcp_tgt	*ptgt, int *fc_status,
418     int *fc_pkt_state, int *fc_pkt_reason, int *fc_pkt_action);
419 static void fcp_ipkt_sema_init(struct fcp_ipkt *icmd);
420 static int fcp_ipkt_sema_wait(struct fcp_ipkt *icmd);
421 static void fcp_ipkt_sema_callback(struct fc_packet *fpkt);
422 static void fcp_ipkt_sema_cleanup(struct fcp_ipkt *icmd);
423 
424 static void fcp_handle_devices(struct fcp_port *pptr,
425     fc_portmap_t devlist[], uint32_t dev_cnt, int link_cnt,
426     fcp_map_tag_t *map_tag, int cause);
427 static int fcp_handle_mapflags(struct fcp_port *pptr,
428     struct fcp_tgt *ptgt, fc_portmap_t *map_entry, int link_cnt,
429     int tgt_cnt, int cause);
430 static int fcp_handle_reportlun_changed(struct fcp_tgt *ptgt, int cause);
431 static int fcp_send_els(struct fcp_port *pptr, struct fcp_tgt *ptgt,
432     struct fcp_ipkt *icmd, uchar_t opcode, int lcount, int tcount, int cause);
433 static void fcp_update_state(struct fcp_port *pptr, uint32_t state,
434     int cause);
435 static void fcp_update_tgt_state(struct fcp_tgt *ptgt, int flag,
436     uint32_t state);
437 static struct fcp_port *fcp_get_port(opaque_t port_handle);
438 static void fcp_unsol_callback(fc_packet_t *fpkt);
439 static void fcp_unsol_resp_init(fc_packet_t *pkt, fc_unsol_buf_t *buf,
440     uchar_t r_ctl, uchar_t type);
441 static int fcp_unsol_prli(struct fcp_port *pptr, fc_unsol_buf_t *buf);
442 static struct fcp_ipkt *fcp_icmd_alloc(struct fcp_port *pptr,
443     struct fcp_tgt *ptgt, int cmd_len, int resp_len, int data_len,
444     int nodma, int lcount, int tcount, int cause, uint32_t rscn_count);
445 static void fcp_icmd_free(struct fcp_port *pptr, struct fcp_ipkt *icmd);
446 static int fcp_alloc_dma(struct fcp_port *pptr, struct fcp_ipkt *icmd,
447     int nodma, int flags);
448 static void fcp_free_dma(struct fcp_port *pptr, struct fcp_ipkt *icmd);
449 static struct fcp_tgt *fcp_lookup_target(struct fcp_port *pptr,
450     uchar_t *wwn);
451 static struct fcp_tgt *fcp_get_target_by_did(struct fcp_port *pptr,
452     uint32_t d_id);
453 static void fcp_icmd_callback(fc_packet_t *fpkt);
454 static int fcp_send_scsi(struct fcp_lun *plun, uchar_t opcode,
455     int len, int lcount, int tcount, int cause, uint32_t rscn_count);
456 static int fcp_check_reportlun(struct fcp_rsp *rsp, fc_packet_t *fpkt);
457 static void fcp_scsi_callback(fc_packet_t *fpkt);
458 static void fcp_retry_scsi_cmd(fc_packet_t *fpkt);
459 static void fcp_handle_inquiry(fc_packet_t *fpkt, struct fcp_ipkt *icmd);
460 static void fcp_handle_reportlun(fc_packet_t *fpkt, struct fcp_ipkt *icmd);
461 static struct fcp_lun *fcp_get_lun(struct fcp_tgt *ptgt,
462     uint16_t lun_num);
463 static int fcp_finish_tgt(struct fcp_port *pptr, struct fcp_tgt *ptgt,
464     int link_cnt, int tgt_cnt, int cause);
465 static void fcp_finish_init(struct fcp_port *pptr);
466 static void fcp_create_luns(struct fcp_tgt *ptgt, int link_cnt,
467     int tgt_cnt, int cause);
468 static int fcp_trigger_lun(struct fcp_lun *plun, child_info_t *cip,
469     int old_mpxio, int online, int link_cnt, int tgt_cnt, int flags);
470 static int fcp_offline_target(struct fcp_port *pptr, struct fcp_tgt *ptgt,
471     int link_cnt, int tgt_cnt, int nowait, int flags);
472 static void fcp_offline_target_now(struct fcp_port *pptr,
473     struct fcp_tgt *ptgt, int link_cnt, int tgt_cnt, int flags);
474 static void fcp_offline_tgt_luns(struct fcp_tgt *ptgt, int link_cnt,
475     int tgt_cnt, int flags);
476 static void fcp_offline_lun(struct fcp_lun *plun, int link_cnt, int tgt_cnt,
477     int nowait, int flags);
478 static void fcp_prepare_offline_lun(struct fcp_lun *plun, int link_cnt,
479     int tgt_cnt);
480 static void fcp_offline_lun_now(struct fcp_lun *plun, int link_cnt,
481     int tgt_cnt, int flags);
482 static void fcp_scan_offline_luns(struct fcp_port *pptr);
483 static void fcp_scan_offline_tgts(struct fcp_port *pptr);
484 static void fcp_update_offline_flags(struct fcp_lun *plun);
485 static struct fcp_pkt *fcp_scan_commands(struct fcp_lun *plun);
486 static void fcp_abort_commands(struct fcp_pkt *head, struct
487     fcp_port *pptr);
488 static void fcp_cmd_callback(fc_packet_t *fpkt);
489 static void fcp_complete_pkt(fc_packet_t *fpkt);
490 static int fcp_validate_fcp_response(struct fcp_rsp *rsp,
491     struct fcp_port *pptr);
492 static int fcp_device_changed(struct fcp_port *pptr, struct fcp_tgt *ptgt,
493     fc_portmap_t *map_entry, int link_cnt, int tgt_cnt, int cause);
494 static struct fcp_lun *fcp_alloc_lun(struct fcp_tgt *ptgt);
495 static void fcp_dealloc_lun(struct fcp_lun *plun);
496 static struct fcp_tgt *fcp_alloc_tgt(struct fcp_port *pptr,
497     fc_portmap_t *map_entry, int link_cnt);
498 static void fcp_dealloc_tgt(struct fcp_tgt *ptgt);
499 static void fcp_queue_ipkt(struct fcp_port *pptr, fc_packet_t *fpkt);
500 static int fcp_transport(opaque_t port_handle, fc_packet_t *fpkt,
501     int internal);
502 static void fcp_log(int level, dev_info_t *dip, const char *fmt, ...);
503 static int fcp_handle_port_attach(opaque_t ulph, fc_ulp_port_info_t *pinfo,
504     uint32_t s_id, int instance);
505 static int fcp_handle_port_detach(struct fcp_port *pptr, int flag,
506     int instance);
507 static void fcp_cleanup_port(struct fcp_port *pptr, int instance);
508 static int fcp_kmem_cache_constructor(struct scsi_pkt *, scsi_hba_tran_t *,
509     int);
510 static void fcp_kmem_cache_destructor(struct  scsi_pkt *, scsi_hba_tran_t *);
511 static int fcp_pkt_setup(struct scsi_pkt *, int (*)(), caddr_t);
512 static int fcp_alloc_cmd_resp(struct fcp_port *pptr, fc_packet_t *fpkt,
513     int flags);
514 static void fcp_free_cmd_resp(struct fcp_port *pptr, fc_packet_t *fpkt);
515 static int fcp_reset_target(struct scsi_address *ap, int level);
516 static int fcp_commoncap(struct scsi_address *ap, char *cap,
517     int val, int tgtonly, int doset);
518 static int fcp_scsi_get_name(struct scsi_device *sd, char *name, int len);
519 static int fcp_scsi_get_bus_addr(struct scsi_device *sd, char *name, int len);
520 static int fcp_linkreset(struct fcp_port *pptr, struct scsi_address *ap,
521     int sleep);
522 static int fcp_handle_port_resume(opaque_t ulph, fc_ulp_port_info_t *pinfo,
523     uint32_t s_id, fc_attach_cmd_t cmd, int instance);
524 static void fcp_cp_pinfo(struct fcp_port *pptr, fc_ulp_port_info_t *pinfo);
525 static void fcp_process_elem(struct fcp_hp_elem *elem, int result);
526 static child_info_t *fcp_get_cip(struct fcp_lun *plun, child_info_t *cip,
527     int lcount, int tcount);
528 static int fcp_is_dip_present(struct fcp_lun *plun, dev_info_t *cdip);
529 static int fcp_is_child_present(struct fcp_lun *plun, child_info_t *cip);
530 static dev_info_t *fcp_create_dip(struct fcp_lun *plun, int link_cnt,
531     int tgt_cnt);
532 static dev_info_t *fcp_find_existing_dip(struct fcp_lun *plun,
533     dev_info_t *pdip, caddr_t name);
534 static int fcp_online_child(struct fcp_lun *plun, child_info_t *cip,
535     int lcount, int tcount, int flags, int *circ);
536 static int fcp_offline_child(struct fcp_lun *plun, child_info_t *cip,
537     int lcount, int tcount, int flags, int *circ);
538 static void fcp_remove_child(struct fcp_lun *plun);
539 static void fcp_watch(void *arg);
540 static void fcp_check_reset_delay(struct fcp_port *pptr);
541 static void fcp_abort_all(struct fcp_port *pptr, struct fcp_tgt *ttgt,
542     struct fcp_lun *rlun, int tgt_cnt);
543 struct fcp_port *fcp_soft_state_unlink(struct fcp_port *pptr);
544 static struct fcp_lun *fcp_lookup_lun(struct fcp_port *pptr,
545     uchar_t *wwn, uint16_t lun);
546 static void fcp_prepare_pkt(struct fcp_port *pptr, struct fcp_pkt *cmd,
547     struct fcp_lun *plun);
548 static void fcp_post_callback(struct fcp_pkt *cmd);
549 static int fcp_dopoll(struct fcp_port *pptr, struct fcp_pkt *cmd);
550 static struct fcp_port *fcp_dip2port(dev_info_t *dip);
551 struct fcp_lun *fcp_get_lun_from_cip(struct fcp_port *pptr,
552     child_info_t *cip);
553 static int fcp_pass_to_hp_and_wait(struct fcp_port *pptr,
554     struct fcp_lun *plun, child_info_t *cip, int what, int link_cnt,
555     int tgt_cnt, int flags);
556 static struct fcp_hp_elem *fcp_pass_to_hp(struct fcp_port *pptr,
557     struct fcp_lun *plun, child_info_t *cip, int what, int link_cnt,
558     int tgt_cnt, int flags, int wait);
559 static void fcp_retransport_cmd(struct fcp_port *pptr,
560     struct fcp_pkt *cmd);
561 static void fcp_fail_cmd(struct fcp_pkt *cmd, uchar_t reason,
562     uint_t statistics);
563 static void fcp_queue_pkt(struct fcp_port *pptr, struct fcp_pkt *cmd);
564 static void fcp_update_targets(struct fcp_port *pptr,
565     fc_portmap_t *dev_list, uint32_t count, uint32_t state, int cause);
566 static int fcp_call_finish_init(struct fcp_port *pptr,
567     struct fcp_tgt *ptgt, int lcount, int tcount, int cause);
568 static int fcp_call_finish_init_held(struct fcp_port *pptr,
569     struct fcp_tgt *ptgt, int lcount, int tcount, int cause);
570 static void fcp_reconfigure_luns(void * tgt_handle);
571 static void fcp_free_targets(struct fcp_port *pptr);
572 static void fcp_free_target(struct fcp_tgt *ptgt);
573 static int fcp_is_retryable(struct fcp_ipkt *icmd);
574 static int fcp_create_on_demand(struct fcp_port *pptr, uchar_t *pwwn);
575 static void fcp_ascii_to_wwn(caddr_t string, uchar_t bytes[], unsigned int);
576 static void fcp_wwn_to_ascii(uchar_t bytes[], char *string);
577 static void fcp_print_error(fc_packet_t *fpkt);
578 static int fcp_handle_ipkt_errors(struct fcp_port *pptr,
579     struct fcp_tgt *ptgt, struct fcp_ipkt *icmd, int rval, caddr_t op);
580 static int fcp_outstanding_lun_cmds(struct fcp_tgt *ptgt);
581 static fc_portmap_t *fcp_construct_map(struct fcp_port *pptr,
582     uint32_t *dev_cnt);
583 static void fcp_offline_all(struct fcp_port *pptr, int lcount, int cause);
584 static int fcp_get_statec_count(struct fcp_ioctl *data, int mode, int *rval);
585 static int fcp_copyin_fcp_ioctl_data(struct fcp_ioctl *, int, int *,
586     struct fcp_ioctl *, struct fcp_port **);
587 static char *fcp_get_lun_path(struct fcp_lun *plun);
588 static int fcp_get_target_mappings(struct fcp_ioctl *data, int mode,
589     int *rval);
590 static int fcp_do_ns_registry(struct fcp_port *pptr, uint32_t s_id);
591 static void fcp_retry_ns_registry(struct fcp_port *pptr, uint32_t s_id);
592 static char *fcp_get_lun_path(struct fcp_lun *plun);
593 static int fcp_get_target_mappings(struct fcp_ioctl *data, int mode,
594     int *rval);
595 static void fcp_reconfig_wait(struct fcp_port *pptr);
596 
597 /*
598  * New functions added for mpxio support
599  */
600 static int fcp_virt_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
601     scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
602 static mdi_pathinfo_t *fcp_create_pip(struct fcp_lun *plun, int lcount,
603     int tcount);
604 static mdi_pathinfo_t *fcp_find_existing_pip(struct fcp_lun *plun,
605     dev_info_t *pdip);
606 static int fcp_is_pip_present(struct fcp_lun *plun, mdi_pathinfo_t *pip);
607 static void fcp_handle_page83(fc_packet_t *, struct fcp_ipkt *, int);
608 static void fcp_update_mpxio_path_verifybusy(struct fcp_port *pptr);
609 static int fcp_copy_guid_2_lun_block(struct fcp_lun *plun, char *guidp);
610 static int fcp_update_mpxio_path(struct fcp_lun *plun, child_info_t *cip,
611     int what);
612 static int fcp_is_reconfig_needed(struct fcp_tgt *ptgt,
613     fc_packet_t *fpkt);
614 static int fcp_symmetric_device_probe(struct fcp_lun *plun);
615 
616 /*
617  * New functions added for lun masking support
618  */
619 static void fcp_read_blacklist(dev_info_t *dip,
620     struct fcp_black_list_entry **pplun_blacklist);
621 static void fcp_mask_pwwn_lun(char *curr_pwwn, char *curr_lun,
622     struct fcp_black_list_entry **pplun_blacklist);
623 static void fcp_add_one_mask(char *curr_pwwn, uint32_t lun_id,
624     struct fcp_black_list_entry **pplun_blacklist);
625 static int fcp_should_mask(la_wwn_t *wwn, uint32_t lun_id);
626 static void fcp_cleanup_blacklist(struct fcp_black_list_entry **lun_blacklist);
627 
628 /*
629  * New functions to support software FCA (like fcoei)
630  */
631 static struct scsi_pkt *fcp_pseudo_init_pkt(
632 	struct scsi_address *ap, struct scsi_pkt *pkt,
633 	struct buf *bp, int cmdlen, int statuslen,
634 	int tgtlen, int flags, int (*callback)(), caddr_t arg);
635 static void fcp_pseudo_destroy_pkt(
636 	struct scsi_address *ap, struct scsi_pkt *pkt);
637 static void fcp_pseudo_sync_pkt(
638 	struct scsi_address *ap, struct scsi_pkt *pkt);
639 static int fcp_pseudo_start(struct scsi_address *ap, struct scsi_pkt *pkt);
640 static void fcp_pseudo_dmafree(
641 	struct scsi_address *ap, struct scsi_pkt *pkt);
642 
643 extern struct mod_ops	mod_driverops;
644 /*
645  * This variable is defined in modctl.c and set to '1' after the root driver
646  * and fs are loaded.  It serves as an indication that the root filesystem can
647  * be used.
648  */
649 extern int		modrootloaded;
650 /*
651  * This table contains strings associated with the SCSI sense key codes.  It
652  * is used by FCP to print a clear explanation of the code returned in the
653  * sense information by a device.
654  */
655 extern char		*sense_keys[];
656 /*
657  * This device is created by the SCSI pseudo nexus driver (SCSI vHCI).	It is
658  * under this device that the paths to a physical device are created when
659  * MPxIO is used.
660  */
661 extern dev_info_t	*scsi_vhci_dip;
662 
663 /*
664  * Report lun processing
665  */
666 #define	FCP_LUN_ADDRESSING		0x80
667 #define	FCP_PD_ADDRESSING		0x00
668 #define	FCP_VOLUME_ADDRESSING		0x40
669 
670 #define	FCP_SVE_THROTTLE		0x28 /* Vicom */
671 #define	MAX_INT_DMA			0x7fffffff
672 #define	FCP_MAX_SENSE_LEN		252
673 #define	FCP_MAX_RESPONSE_LEN		0xffffff
674 /*
675  * Property definitions
676  */
677 #define	NODE_WWN_PROP	(char *)fcp_node_wwn_prop
678 #define	PORT_WWN_PROP	(char *)fcp_port_wwn_prop
679 #define	TARGET_PROP	(char *)fcp_target_prop
680 #define	LUN_PROP	(char *)fcp_lun_prop
681 #define	SAM_LUN_PROP	(char *)fcp_sam_lun_prop
682 #define	CONF_WWN_PROP	(char *)fcp_conf_wwn_prop
683 #define	OBP_BOOT_WWN	(char *)fcp_obp_boot_wwn
684 #define	MANUAL_CFG_ONLY	(char *)fcp_manual_config_only
685 #define	INIT_PORT_PROP	(char *)fcp_init_port_prop
686 #define	TGT_PORT_PROP	(char *)fcp_tgt_port_prop
687 #define	LUN_BLACKLIST_PROP	(char *)fcp_lun_blacklist_prop
688 /*
689  * Short hand macros.
690  */
691 #define	LUN_PORT	(plun->lun_tgt->tgt_port)
692 #define	LUN_TGT		(plun->lun_tgt)
693 
694 /*
695  * Driver private macros
696  */
697 #define	FCP_ATOB(x)	(((x) >= '0' && (x) <= '9') ? ((x) - '0') :	\
698 			((x) >= 'a' && (x) <= 'f') ?			\
699 			((x) - 'a' + 10) : ((x) - 'A' + 10))
700 
701 #define	FCP_MAX(a, b)	((a) > (b) ? (a) : (b))
702 
703 #define	FCP_N_NDI_EVENTS						\
704 	(sizeof (fcp_ndi_event_defs) / sizeof (ndi_event_definition_t))
705 
706 #define	FCP_LINK_STATE_CHANGED(p, c)			\
707 	((p)->port_link_cnt != (c)->ipkt_link_cnt)
708 
709 #define	FCP_TGT_STATE_CHANGED(t, c)			\
710 	((t)->tgt_change_cnt != (c)->ipkt_change_cnt)
711 
712 #define	FCP_STATE_CHANGED(p, t, c)		\
713 	(FCP_TGT_STATE_CHANGED(t, c))
714 
715 #define	FCP_MUST_RETRY(fpkt)				\
716 	((fpkt)->pkt_state == FC_PKT_LOCAL_BSY ||	\
717 	(fpkt)->pkt_state == FC_PKT_LOCAL_RJT ||	\
718 	(fpkt)->pkt_state == FC_PKT_TRAN_BSY ||	\
719 	(fpkt)->pkt_state == FC_PKT_ELS_IN_PROGRESS ||	\
720 	(fpkt)->pkt_state == FC_PKT_NPORT_BSY ||	\
721 	(fpkt)->pkt_state == FC_PKT_FABRIC_BSY ||	\
722 	(fpkt)->pkt_state == FC_PKT_PORT_OFFLINE ||	\
723 	(fpkt)->pkt_reason == FC_REASON_OFFLINE)
724 
725 #define	FCP_SENSE_REPORTLUN_CHANGED(es)		\
726 	((es)->es_key == KEY_UNIT_ATTENTION &&	\
727 	(es)->es_add_code == 0x3f &&		\
728 	(es)->es_qual_code == 0x0e)
729 
730 #define	FCP_SENSE_NO_LUN(es)			\
731 	((es)->es_key == KEY_ILLEGAL_REQUEST &&	\
732 	(es)->es_add_code == 0x25 &&		\
733 	(es)->es_qual_code == 0x0)
734 
735 #define	FCP_VERSION		"20090729-1.190"
736 #define	FCP_NAME_VERSION	"SunFC FCP v" FCP_VERSION
737 
738 #define	FCP_NUM_ELEMENTS(array)			\
739 	(sizeof (array) / sizeof ((array)[0]))
740 
741 /*
742  * Debugging, Error reporting, and tracing
743  */
744 #define	FCP_LOG_SIZE		1024 * 1024
745 
746 #define	FCP_LEVEL_1		0x00001		/* attach/detach PM CPR */
747 #define	FCP_LEVEL_2		0x00002		/* failures/Invalid data */
748 #define	FCP_LEVEL_3		0x00004		/* state change, discovery */
749 #define	FCP_LEVEL_4		0x00008		/* ULP messages */
750 #define	FCP_LEVEL_5		0x00010		/* ELS/SCSI cmds */
751 #define	FCP_LEVEL_6		0x00020		/* Transport failures */
752 #define	FCP_LEVEL_7		0x00040
753 #define	FCP_LEVEL_8		0x00080		/* I/O tracing */
754 #define	FCP_LEVEL_9		0x00100		/* I/O tracing */
755 
756 
757 
758 /*
759  * Log contents to system messages file
760  */
761 #define	FCP_MSG_LEVEL_1	(FCP_LEVEL_1 | FC_TRACE_LOG_MSG)
762 #define	FCP_MSG_LEVEL_2	(FCP_LEVEL_2 | FC_TRACE_LOG_MSG)
763 #define	FCP_MSG_LEVEL_3	(FCP_LEVEL_3 | FC_TRACE_LOG_MSG)
764 #define	FCP_MSG_LEVEL_4	(FCP_LEVEL_4 | FC_TRACE_LOG_MSG)
765 #define	FCP_MSG_LEVEL_5	(FCP_LEVEL_5 | FC_TRACE_LOG_MSG)
766 #define	FCP_MSG_LEVEL_6	(FCP_LEVEL_6 | FC_TRACE_LOG_MSG)
767 #define	FCP_MSG_LEVEL_7	(FCP_LEVEL_7 | FC_TRACE_LOG_MSG)
768 #define	FCP_MSG_LEVEL_8	(FCP_LEVEL_8 | FC_TRACE_LOG_MSG)
769 #define	FCP_MSG_LEVEL_9	(FCP_LEVEL_9 | FC_TRACE_LOG_MSG)
770 
771 
772 /*
773  * Log contents to trace buffer
774  */
775 #define	FCP_BUF_LEVEL_1	(FCP_LEVEL_1 | FC_TRACE_LOG_BUF)
776 #define	FCP_BUF_LEVEL_2	(FCP_LEVEL_2 | FC_TRACE_LOG_BUF)
777 #define	FCP_BUF_LEVEL_3	(FCP_LEVEL_3 | FC_TRACE_LOG_BUF)
778 #define	FCP_BUF_LEVEL_4	(FCP_LEVEL_4 | FC_TRACE_LOG_BUF)
779 #define	FCP_BUF_LEVEL_5	(FCP_LEVEL_5 | FC_TRACE_LOG_BUF)
780 #define	FCP_BUF_LEVEL_6	(FCP_LEVEL_6 | FC_TRACE_LOG_BUF)
781 #define	FCP_BUF_LEVEL_7	(FCP_LEVEL_7 | FC_TRACE_LOG_BUF)
782 #define	FCP_BUF_LEVEL_8	(FCP_LEVEL_8 | FC_TRACE_LOG_BUF)
783 #define	FCP_BUF_LEVEL_9	(FCP_LEVEL_9 | FC_TRACE_LOG_BUF)
784 
785 
786 /*
787  * Log contents to both system messages file and trace buffer
788  */
789 #define	FCP_MSG_BUF_LEVEL_1	(FCP_LEVEL_1 | FC_TRACE_LOG_BUF |	\
790 				FC_TRACE_LOG_MSG)
791 #define	FCP_MSG_BUF_LEVEL_2	(FCP_LEVEL_2 | FC_TRACE_LOG_BUF |	\
792 				FC_TRACE_LOG_MSG)
793 #define	FCP_MSG_BUF_LEVEL_3	(FCP_LEVEL_3 | FC_TRACE_LOG_BUF |	\
794 				FC_TRACE_LOG_MSG)
795 #define	FCP_MSG_BUF_LEVEL_4	(FCP_LEVEL_4 | FC_TRACE_LOG_BUF |	\
796 				FC_TRACE_LOG_MSG)
797 #define	FCP_MSG_BUF_LEVEL_5	(FCP_LEVEL_5 | FC_TRACE_LOG_BUF |	\
798 				FC_TRACE_LOG_MSG)
799 #define	FCP_MSG_BUF_LEVEL_6	(FCP_LEVEL_6 | FC_TRACE_LOG_BUF |	\
800 				FC_TRACE_LOG_MSG)
801 #define	FCP_MSG_BUF_LEVEL_7	(FCP_LEVEL_7 | FC_TRACE_LOG_BUF |	\
802 				FC_TRACE_LOG_MSG)
803 #define	FCP_MSG_BUF_LEVEL_8	(FCP_LEVEL_8 | FC_TRACE_LOG_BUF |	\
804 				FC_TRACE_LOG_MSG)
805 #define	FCP_MSG_BUF_LEVEL_9	(FCP_LEVEL_9 | FC_TRACE_LOG_BUF |	\
806 				FC_TRACE_LOG_MSG)
807 #ifdef DEBUG
808 #define	FCP_DTRACE	fc_trace_debug
809 #else
810 #define	FCP_DTRACE
811 #endif
812 
813 #define	FCP_TRACE	fc_trace_debug
814 
815 static struct cb_ops fcp_cb_ops = {
816 	fcp_open,			/* open */
817 	fcp_close,			/* close */
818 	nodev,				/* strategy */
819 	nodev,				/* print */
820 	nodev,				/* dump */
821 	nodev,				/* read */
822 	nodev,				/* write */
823 	fcp_ioctl,			/* ioctl */
824 	nodev,				/* devmap */
825 	nodev,				/* mmap */
826 	nodev,				/* segmap */
827 	nochpoll,			/* chpoll */
828 	ddi_prop_op,			/* cb_prop_op */
829 	0,				/* streamtab */
830 	D_NEW | D_MP | D_HOTPLUG,	/* cb_flag */
831 	CB_REV,				/* rev */
832 	nodev,				/* aread */
833 	nodev				/* awrite */
834 };
835 
836 
837 static struct dev_ops fcp_ops = {
838 	DEVO_REV,
839 	0,
840 	ddi_getinfo_1to1,
841 	nulldev,		/* identify */
842 	nulldev,		/* probe */
843 	fcp_attach,		/* attach and detach are mandatory */
844 	fcp_detach,
845 	nodev,			/* reset */
846 	&fcp_cb_ops,		/* cb_ops */
847 	NULL,			/* bus_ops */
848 	NULL,			/* power */
849 };
850 
851 
852 char *fcp_version = FCP_NAME_VERSION;
853 
854 static struct modldrv modldrv = {
855 	&mod_driverops,
856 	FCP_NAME_VERSION,
857 	&fcp_ops
858 };
859 
860 
861 static struct modlinkage modlinkage = {
862 	MODREV_1,
863 	&modldrv,
864 	NULL
865 };
866 
867 
868 static fc_ulp_modinfo_t fcp_modinfo = {
869 	&fcp_modinfo,			/* ulp_handle */
870 	FCTL_ULP_MODREV_4,		/* ulp_rev */
871 	FC4_SCSI_FCP,			/* ulp_type */
872 	"fcp",				/* ulp_name */
873 	FCP_STATEC_MASK,		/* ulp_statec_mask */
874 	fcp_port_attach,		/* ulp_port_attach */
875 	fcp_port_detach,		/* ulp_port_detach */
876 	fcp_port_ioctl,			/* ulp_port_ioctl */
877 	fcp_els_callback,		/* ulp_els_callback */
878 	fcp_data_callback,		/* ulp_data_callback */
879 	fcp_statec_callback		/* ulp_statec_callback */
880 };
881 
882 #ifdef	DEBUG
883 #define	FCP_TRACE_DEFAULT	(FC_TRACE_LOG_MASK | FCP_LEVEL_1 |	\
884 				FCP_LEVEL_2 | FCP_LEVEL_3 |		\
885 				FCP_LEVEL_4 | FCP_LEVEL_5 |		\
886 				FCP_LEVEL_6 | FCP_LEVEL_7)
887 #else
888 #define	FCP_TRACE_DEFAULT	(FC_TRACE_LOG_MASK | FCP_LEVEL_1 |	\
889 				FCP_LEVEL_2 | FCP_LEVEL_3 |		\
890 				FCP_LEVEL_4 | FCP_LEVEL_5 |		\
891 				FCP_LEVEL_6 | FCP_LEVEL_7)
892 #endif
893 
894 /* FCP global variables */
895 int			fcp_bus_config_debug = 0;
896 static int		fcp_log_size = FCP_LOG_SIZE;
897 static int		fcp_trace = FCP_TRACE_DEFAULT;
898 static fc_trace_logq_t	*fcp_logq = NULL;
899 static struct fcp_black_list_entry	*fcp_lun_blacklist = NULL;
900 /*
901  * The auto-configuration is set by default.  The only way of disabling it is
902  * through the property MANUAL_CFG_ONLY in the fcp.conf file.
903  */
904 static int		fcp_enable_auto_configuration = 1;
905 static int		fcp_max_bus_config_retries	= 4;
906 static int		fcp_lun_ready_retry = 300;
907 /*
908  * The value assigned to the following variable has changed several times due
909  * to a problem with the data underruns reporting of some firmware(s).	The
910  * current value of 50 gives a timeout value of 25 seconds for a max number
911  * of 256 LUNs.
912  */
913 static int		fcp_max_target_retries = 50;
914 /*
915  * Watchdog variables
916  * ------------------
917  *
918  * fcp_watchdog_init
919  *
920  *	Indicates if the watchdog timer is running or not.  This is actually
921  *	a counter of the number of Fibre Channel ports that attached.  When
922  *	the first port attaches the watchdog is started.  When the last port
923  *	detaches the watchdog timer is stopped.
924  *
925  * fcp_watchdog_time
926  *
927  *	This is the watchdog clock counter.  It is incremented by
928  *	fcp_watchdog_time each time the watchdog timer expires.
929  *
930  * fcp_watchdog_timeout
931  *
932  *	Increment value of the variable fcp_watchdog_time as well as the
933  *	the timeout value of the watchdog timer.  The unit is 1 second.	 It
934  *	is strange that this is not a #define	but a variable since the code
935  *	never changes this value.  The reason why it can be said that the
936  *	unit is 1 second is because the number of ticks for the watchdog
937  *	timer is determined like this:
938  *
939  *	    fcp_watchdog_tick = fcp_watchdog_timeout *
940  *				  drv_usectohz(1000000);
941  *
942  *	The value 1000000 is hard coded in the code.
943  *
944  * fcp_watchdog_tick
945  *
946  *	Watchdog timer value in ticks.
947  */
948 static int		fcp_watchdog_init = 0;
949 static int		fcp_watchdog_time = 0;
950 static int		fcp_watchdog_timeout = 1;
951 static int		fcp_watchdog_tick;
952 
953 /*
954  * fcp_offline_delay is a global variable to enable customisation of
955  * the timeout on link offlines or RSCNs. The default value is set
956  * to match FCP_OFFLINE_DELAY (20sec), which is 2*RA_TOV_els as
957  * specified in FCP4 Chapter 11 (see www.t10.org).
958  *
959  * The variable fcp_offline_delay is specified in SECONDS.
960  *
961  * If we made this a static var then the user would not be able to
962  * change it. This variable is set in fcp_attach().
963  */
964 unsigned int		fcp_offline_delay = FCP_OFFLINE_DELAY;
965 
966 static void		*fcp_softstate = NULL; /* for soft state */
967 static uchar_t		fcp_oflag = FCP_IDLE; /* open flag */
968 static kmutex_t		fcp_global_mutex;
969 static kmutex_t		fcp_ioctl_mutex;
970 static dev_info_t	*fcp_global_dip = NULL;
971 static timeout_id_t	fcp_watchdog_id;
972 const char		*fcp_lun_prop = "lun";
973 const char		*fcp_sam_lun_prop = "sam-lun";
974 const char		*fcp_target_prop = "target";
975 /*
976  * NOTE: consumers of "node-wwn" property include stmsboot in ON
977  * consolidation.
978  */
979 const char		*fcp_node_wwn_prop = "node-wwn";
980 const char		*fcp_port_wwn_prop = "port-wwn";
981 const char		*fcp_conf_wwn_prop = "fc-port-wwn";
982 const char		*fcp_obp_boot_wwn = "fc-boot-dev-portwwn";
983 const char		*fcp_manual_config_only = "manual_configuration_only";
984 const char		*fcp_init_port_prop = "initiator-port";
985 const char		*fcp_tgt_port_prop = "target-port";
986 const char		*fcp_lun_blacklist_prop = "pwwn-lun-blacklist";
987 
988 static struct fcp_port	*fcp_port_head = NULL;
989 static ddi_eventcookie_t	fcp_insert_eid;
990 static ddi_eventcookie_t	fcp_remove_eid;
991 
992 static ndi_event_definition_t	fcp_ndi_event_defs[] = {
993 	{ FCP_EVENT_TAG_INSERT, FCAL_INSERT_EVENT, EPL_KERNEL },
994 	{ FCP_EVENT_TAG_REMOVE, FCAL_REMOVE_EVENT, EPL_INTERRUPT }
995 };
996 
997 /*
998  * List of valid commands for the scsi_ioctl call
999  */
1000 static uint8_t scsi_ioctl_list[] = {
1001 	SCMD_INQUIRY,
1002 	SCMD_REPORT_LUN,
1003 	SCMD_READ_CAPACITY
1004 };
1005 
1006 /*
1007  * this is used to dummy up a report lun response for cases
1008  * where the target doesn't support it
1009  */
1010 static uchar_t fcp_dummy_lun[] = {
1011 	0x00,		/* MSB length (length = no of luns * 8) */
1012 	0x00,
1013 	0x00,
1014 	0x08,		/* LSB length */
1015 	0x00,		/* MSB reserved */
1016 	0x00,
1017 	0x00,
1018 	0x00,		/* LSB reserved */
1019 	FCP_PD_ADDRESSING,
1020 	0x00,		/* LUN is ZERO at the first level */
1021 	0x00,
1022 	0x00,		/* second level is zero */
1023 	0x00,
1024 	0x00,		/* third level is zero */
1025 	0x00,
1026 	0x00		/* fourth level is zero */
1027 };
1028 
1029 static uchar_t fcp_alpa_to_switch[] = {
1030 	0x00, 0x7d, 0x7c, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x7a, 0x00,
1031 	0x00, 0x00, 0x00, 0x00, 0x00, 0x79, 0x78, 0x00, 0x00, 0x00,
1032 	0x00, 0x00, 0x00, 0x77, 0x76, 0x00, 0x00, 0x75, 0x00, 0x74,
1033 	0x73, 0x72, 0x00, 0x00, 0x00, 0x71, 0x00, 0x70, 0x6f, 0x6e,
1034 	0x00, 0x6d, 0x6c, 0x6b, 0x6a, 0x69, 0x68, 0x00, 0x00, 0x67,
1035 	0x66, 0x65, 0x64, 0x63, 0x62, 0x00, 0x00, 0x61, 0x60, 0x00,
1036 	0x5f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5e, 0x00, 0x5d,
1037 	0x5c, 0x5b, 0x00, 0x5a, 0x59, 0x58, 0x57, 0x56, 0x55, 0x00,
1038 	0x00, 0x54, 0x53, 0x52, 0x51, 0x50, 0x4f, 0x00, 0x00, 0x4e,
1039 	0x4d, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4b,
1040 	0x00, 0x4a, 0x49, 0x48, 0x00, 0x47, 0x46, 0x45, 0x44, 0x43,
1041 	0x42, 0x00, 0x00, 0x41, 0x40, 0x3f, 0x3e, 0x3d, 0x3c, 0x00,
1042 	0x00, 0x3b, 0x3a, 0x00, 0x39, 0x00, 0x00, 0x00, 0x38, 0x37,
1043 	0x36, 0x00, 0x35, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
1044 	0x00, 0x00, 0x00, 0x33, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00,
1045 	0x00, 0x31, 0x30, 0x00, 0x00, 0x2f, 0x00, 0x2e, 0x2d, 0x2c,
1046 	0x00, 0x00, 0x00, 0x2b, 0x00, 0x2a, 0x29, 0x28, 0x00, 0x27,
1047 	0x26, 0x25, 0x24, 0x23, 0x22, 0x00, 0x00, 0x21, 0x20, 0x1f,
1048 	0x1e, 0x1d, 0x1c, 0x00, 0x00, 0x1b, 0x1a, 0x00, 0x19, 0x00,
1049 	0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x17, 0x16, 0x15,
1050 	0x00, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x00, 0x00, 0x0e,
1051 	0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x00, 0x00, 0x08, 0x07, 0x00,
1052 	0x06, 0x00, 0x00, 0x00, 0x05, 0x04, 0x03, 0x00, 0x02, 0x00,
1053 	0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1054 };
1055 
1056 static caddr_t pid = "SESS01	      ";
1057 
1058 #if	!defined(lint)
1059 
1060 _NOTE(MUTEX_PROTECTS_DATA(fcp_global_mutex,
1061     fcp_port::fcp_next fcp_watchdog_id))
1062 
1063 _NOTE(DATA_READABLE_WITHOUT_LOCK(fcp_watchdog_time))
1064 
1065 _NOTE(SCHEME_PROTECTS_DATA("Unshared",
1066     fcp_insert_eid
1067     fcp_remove_eid
1068     fcp_watchdog_time))
1069 
1070 _NOTE(SCHEME_PROTECTS_DATA("Unshared",
1071     fcp_cb_ops
1072     fcp_ops
1073     callb_cpr))
1074 
1075 #endif /* lint */
1076 
1077 /*
1078  * This table is used to determine whether or not it's safe to copy in
1079  * the target node name for a lun.  Since all luns behind the same target
1080  * have the same wwnn, only tagets that do not support multiple luns are
1081  * eligible to be enumerated under mpxio if they aren't page83 compliant.
1082  */
1083 
1084 char *fcp_symmetric_disk_table[] = {
1085 	"SEAGATE ST",
1086 	"IBM	 DDYFT",
1087 	"SUNW	 SUNWGS",	/* Daktari enclosure */
1088 	"SUN	 SENA",		/* SES device */
1089 	"SUN	 SESS01"	/* VICOM SVE box */
1090 };
1091 
1092 int fcp_symmetric_disk_table_size =
1093 	sizeof (fcp_symmetric_disk_table)/sizeof (char *);
1094 
1095 /*
1096  * This structure is bogus. scsi_hba_attach_setup() requires, as in the kernel
1097  * will panic if you don't pass this in to the routine, this information.
1098  * Need to determine what the actual impact to the system is by providing
1099  * this information if any. Since dma allocation is done in pkt_init it may
1100  * not have any impact. These values are straight from the Writing Device
1101  * Driver manual.
1102  */
1103 static ddi_dma_attr_t pseudo_fca_dma_attr = {
1104 	DMA_ATTR_V0,	/* ddi_dma_attr version */
1105 	0,		/* low address */
1106 	0xffffffff,	/* high address */
1107 	0x00ffffff,	/* counter upper bound */
1108 	1,		/* alignment requirements */
1109 	0x3f,		/* burst sizes */
1110 	1,		/* minimum DMA access */
1111 	0xffffffff,	/* maximum DMA access */
1112 	(1 << 24) - 1,	/* segment boundary restrictions */
1113 	1,		/* scater/gather list length */
1114 	512,		/* device granularity */
1115 	0		/* DMA flags */
1116 };
1117 
1118 /*
1119  * The _init(9e) return value should be that of mod_install(9f). Under
1120  * some circumstances, a failure may not be related mod_install(9f) and
1121  * one would then require a return value to indicate the failure. Looking
1122  * at mod_install(9f), it is expected to return 0 for success and non-zero
1123  * for failure. mod_install(9f) for device drivers, further goes down the
1124  * calling chain and ends up in ddi_installdrv(), whose return values are
1125  * DDI_SUCCESS and DDI_FAILURE - There are also other functions in the
1126  * calling chain of mod_install(9f) which return values like EINVAL and
1127  * in some even return -1.
1128  *
1129  * To work around the vagaries of the mod_install() calling chain, return
1130  * either 0 or ENODEV depending on the success or failure of mod_install()
1131  */
1132 int
1133 _init(void)
1134 {
1135 	int rval;
1136 
1137 	/*
1138 	 * Allocate soft state and prepare to do ddi_soft_state_zalloc()
1139 	 * before registering with the transport first.
1140 	 */
1141 	if (ddi_soft_state_init(&fcp_softstate,
1142 	    sizeof (struct fcp_port), FCP_INIT_ITEMS) != 0) {
1143 		return (EINVAL);
1144 	}
1145 
1146 	mutex_init(&fcp_global_mutex, NULL, MUTEX_DRIVER, NULL);
1147 	mutex_init(&fcp_ioctl_mutex, NULL, MUTEX_DRIVER, NULL);
1148 
1149 	if ((rval = fc_ulp_add(&fcp_modinfo)) != FC_SUCCESS) {
1150 		cmn_err(CE_WARN, "fcp: fc_ulp_add failed");
1151 		mutex_destroy(&fcp_global_mutex);
1152 		mutex_destroy(&fcp_ioctl_mutex);
1153 		ddi_soft_state_fini(&fcp_softstate);
1154 		return (ENODEV);
1155 	}
1156 
1157 	fcp_logq = fc_trace_alloc_logq(fcp_log_size);
1158 
1159 	if ((rval = mod_install(&modlinkage)) != 0) {
1160 		fc_trace_free_logq(fcp_logq);
1161 		(void) fc_ulp_remove(&fcp_modinfo);
1162 		mutex_destroy(&fcp_global_mutex);
1163 		mutex_destroy(&fcp_ioctl_mutex);
1164 		ddi_soft_state_fini(&fcp_softstate);
1165 		rval = ENODEV;
1166 	}
1167 
1168 	return (rval);
1169 }
1170 
1171 
1172 /*
1173  * the system is done with us as a driver, so clean up
1174  */
1175 int
1176 _fini(void)
1177 {
1178 	int rval;
1179 
1180 	/*
1181 	 * don't start cleaning up until we know that the module remove
1182 	 * has worked  -- if this works, then we know that each instance
1183 	 * has successfully been DDI_DETACHed
1184 	 */
1185 	if ((rval = mod_remove(&modlinkage)) != 0) {
1186 		return (rval);
1187 	}
1188 
1189 	(void) fc_ulp_remove(&fcp_modinfo);
1190 
1191 	ddi_soft_state_fini(&fcp_softstate);
1192 	mutex_destroy(&fcp_global_mutex);
1193 	mutex_destroy(&fcp_ioctl_mutex);
1194 	fc_trace_free_logq(fcp_logq);
1195 
1196 	return (rval);
1197 }
1198 
1199 
1200 int
1201 _info(struct modinfo *modinfop)
1202 {
1203 	return (mod_info(&modlinkage, modinfop));
1204 }
1205 
1206 
1207 /*
1208  * attach the module
1209  */
1210 static int
1211 fcp_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1212 {
1213 	int rval = DDI_SUCCESS;
1214 
1215 	FCP_DTRACE(fcp_logq, "fcp", fcp_trace,
1216 	    FCP_BUF_LEVEL_8, 0, "fcp module attach: cmd=0x%x", cmd);
1217 
1218 	if (cmd == DDI_ATTACH) {
1219 		/* The FCP pseudo device is created here. */
1220 		mutex_enter(&fcp_global_mutex);
1221 		fcp_global_dip = devi;
1222 		mutex_exit(&fcp_global_mutex);
1223 
1224 		if (ddi_create_minor_node(fcp_global_dip, "fcp", S_IFCHR,
1225 		    0, DDI_PSEUDO, 0) == DDI_SUCCESS) {
1226 			ddi_report_dev(fcp_global_dip);
1227 		} else {
1228 			cmn_err(CE_WARN, "FCP: Cannot create minor node");
1229 			mutex_enter(&fcp_global_mutex);
1230 			fcp_global_dip = NULL;
1231 			mutex_exit(&fcp_global_mutex);
1232 
1233 			rval = DDI_FAILURE;
1234 		}
1235 		/*
1236 		 * We check the fcp_offline_delay property at this
1237 		 * point. This variable is global for the driver,
1238 		 * not specific to an instance.
1239 		 *
1240 		 * We do not recommend setting the value to less
1241 		 * than 10 seconds (RA_TOV_els), or greater than
1242 		 * 60 seconds.
1243 		 */
1244 		fcp_offline_delay = ddi_prop_get_int(DDI_DEV_T_ANY,
1245 		    devi, DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1246 		    "fcp_offline_delay", FCP_OFFLINE_DELAY);
1247 		if ((fcp_offline_delay < 10) ||
1248 		    (fcp_offline_delay > 60)) {
1249 			cmn_err(CE_WARN, "Setting fcp_offline_delay "
1250 			    "to %d second(s). This is outside the "
1251 			    "recommended range of 10..60 seconds.",
1252 			    fcp_offline_delay);
1253 		}
1254 	}
1255 
1256 	return (rval);
1257 }
1258 
1259 
1260 /*ARGSUSED*/
1261 static int
1262 fcp_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
1263 {
1264 	int	res = DDI_SUCCESS;
1265 
1266 	FCP_DTRACE(fcp_logq, "fcp", fcp_trace,
1267 	    FCP_BUF_LEVEL_8, 0,	 "module detach: cmd=0x%x", cmd);
1268 
1269 	if (cmd == DDI_DETACH) {
1270 		/*
1271 		 * Check if there are active ports/threads. If there
1272 		 * are any, we will fail, else we will succeed (there
1273 		 * should not be much to clean up)
1274 		 */
1275 		mutex_enter(&fcp_global_mutex);
1276 		FCP_DTRACE(fcp_logq, "fcp",
1277 		    fcp_trace, FCP_BUF_LEVEL_8, 0,  "port_head=%p",
1278 		    (void *) fcp_port_head);
1279 
1280 		if (fcp_port_head == NULL) {
1281 			ddi_remove_minor_node(fcp_global_dip, NULL);
1282 			fcp_global_dip = NULL;
1283 			mutex_exit(&fcp_global_mutex);
1284 		} else {
1285 			mutex_exit(&fcp_global_mutex);
1286 			res = DDI_FAILURE;
1287 		}
1288 	}
1289 	FCP_DTRACE(fcp_logq, "fcp", fcp_trace,
1290 	    FCP_BUF_LEVEL_8, 0,	 "module detach returning %d", res);
1291 
1292 	return (res);
1293 }
1294 
1295 
1296 /* ARGSUSED */
1297 static int
1298 fcp_open(dev_t *devp, int flag, int otype, cred_t *credp)
1299 {
1300 	if (otype != OTYP_CHR) {
1301 		return (EINVAL);
1302 	}
1303 
1304 	/*
1305 	 * Allow only root to talk;
1306 	 */
1307 	if (drv_priv(credp)) {
1308 		return (EPERM);
1309 	}
1310 
1311 	mutex_enter(&fcp_global_mutex);
1312 	if (fcp_oflag & FCP_EXCL) {
1313 		mutex_exit(&fcp_global_mutex);
1314 		return (EBUSY);
1315 	}
1316 
1317 	if (flag & FEXCL) {
1318 		if (fcp_oflag & FCP_OPEN) {
1319 			mutex_exit(&fcp_global_mutex);
1320 			return (EBUSY);
1321 		}
1322 		fcp_oflag |= FCP_EXCL;
1323 	}
1324 	fcp_oflag |= FCP_OPEN;
1325 	mutex_exit(&fcp_global_mutex);
1326 
1327 	return (0);
1328 }
1329 
1330 
1331 /* ARGSUSED */
1332 static int
1333 fcp_close(dev_t dev, int flag, int otype, cred_t *credp)
1334 {
1335 	if (otype != OTYP_CHR) {
1336 		return (EINVAL);
1337 	}
1338 
1339 	mutex_enter(&fcp_global_mutex);
1340 	if (!(fcp_oflag & FCP_OPEN)) {
1341 		mutex_exit(&fcp_global_mutex);
1342 		return (ENODEV);
1343 	}
1344 	fcp_oflag = FCP_IDLE;
1345 	mutex_exit(&fcp_global_mutex);
1346 
1347 	return (0);
1348 }
1349 
1350 
1351 /*
1352  * fcp_ioctl
1353  *	Entry point for the FCP ioctls
1354  *
1355  * Input:
1356  *	See ioctl(9E)
1357  *
1358  * Output:
1359  *	See ioctl(9E)
1360  *
1361  * Returns:
1362  *	See ioctl(9E)
1363  *
1364  * Context:
1365  *	Kernel context.
1366  */
1367 /* ARGSUSED */
1368 static int
1369 fcp_ioctl(dev_t dev, int cmd, intptr_t data, int mode, cred_t *credp,
1370     int *rval)
1371 {
1372 	int			ret = 0;
1373 
1374 	mutex_enter(&fcp_global_mutex);
1375 	if (!(fcp_oflag & FCP_OPEN)) {
1376 		mutex_exit(&fcp_global_mutex);
1377 		return (ENXIO);
1378 	}
1379 	mutex_exit(&fcp_global_mutex);
1380 
1381 	switch (cmd) {
1382 	case FCP_TGT_INQUIRY:
1383 	case FCP_TGT_CREATE:
1384 	case FCP_TGT_DELETE:
1385 		ret = fcp_setup_device_data_ioctl(cmd,
1386 		    (struct fcp_ioctl *)data, mode, rval);
1387 		break;
1388 
1389 	case FCP_TGT_SEND_SCSI:
1390 		mutex_enter(&fcp_ioctl_mutex);
1391 		ret = fcp_setup_scsi_ioctl(
1392 		    (struct fcp_scsi_cmd *)data, mode, rval);
1393 		mutex_exit(&fcp_ioctl_mutex);
1394 		break;
1395 
1396 	case FCP_STATE_COUNT:
1397 		ret = fcp_get_statec_count((struct fcp_ioctl *)data,
1398 		    mode, rval);
1399 		break;
1400 	case FCP_GET_TARGET_MAPPINGS:
1401 		ret = fcp_get_target_mappings((struct fcp_ioctl *)data,
1402 		    mode, rval);
1403 		break;
1404 	default:
1405 		fcp_log(CE_WARN, NULL,
1406 		    "!Invalid ioctl opcode = 0x%x", cmd);
1407 		ret	= EINVAL;
1408 	}
1409 
1410 	return (ret);
1411 }
1412 
1413 
1414 /*
1415  * fcp_setup_device_data_ioctl
1416  *	Setup handler for the "device data" style of
1417  *	ioctl for FCP.	See "fcp_util.h" for data structure
1418  *	definition.
1419  *
1420  * Input:
1421  *	cmd	= FCP ioctl command
1422  *	data	= ioctl data
1423  *	mode	= See ioctl(9E)
1424  *
1425  * Output:
1426  *	data	= ioctl data
1427  *	rval	= return value - see ioctl(9E)
1428  *
1429  * Returns:
1430  *	See ioctl(9E)
1431  *
1432  * Context:
1433  *	Kernel context.
1434  */
1435 /* ARGSUSED */
1436 static int
1437 fcp_setup_device_data_ioctl(int cmd, struct fcp_ioctl *data, int mode,
1438     int *rval)
1439 {
1440 	struct fcp_port	*pptr;
1441 	struct	device_data	*dev_data;
1442 	uint32_t		link_cnt;
1443 	la_wwn_t		*wwn_ptr = NULL;
1444 	struct fcp_tgt		*ptgt = NULL;
1445 	struct fcp_lun		*plun = NULL;
1446 	int			i, error;
1447 	struct fcp_ioctl	fioctl;
1448 
1449 #ifdef	_MULTI_DATAMODEL
1450 	switch (ddi_model_convert_from(mode & FMODELS)) {
1451 	case DDI_MODEL_ILP32: {
1452 		struct fcp32_ioctl f32_ioctl;
1453 
1454 		if (ddi_copyin((void *)data, (void *)&f32_ioctl,
1455 		    sizeof (struct fcp32_ioctl), mode)) {
1456 			return (EFAULT);
1457 		}
1458 		fioctl.fp_minor = f32_ioctl.fp_minor;
1459 		fioctl.listlen = f32_ioctl.listlen;
1460 		fioctl.list = (caddr_t)(long)f32_ioctl.list;
1461 		break;
1462 	}
1463 	case DDI_MODEL_NONE:
1464 		if (ddi_copyin((void *)data, (void *)&fioctl,
1465 		    sizeof (struct fcp_ioctl), mode)) {
1466 			return (EFAULT);
1467 		}
1468 		break;
1469 	}
1470 
1471 #else	/* _MULTI_DATAMODEL */
1472 	if (ddi_copyin((void *)data, (void *)&fioctl,
1473 	    sizeof (struct fcp_ioctl), mode)) {
1474 		return (EFAULT);
1475 	}
1476 #endif	/* _MULTI_DATAMODEL */
1477 
1478 	/*
1479 	 * Right now we can assume that the minor number matches with
1480 	 * this instance of fp. If this changes we will need to
1481 	 * revisit this logic.
1482 	 */
1483 	mutex_enter(&fcp_global_mutex);
1484 	pptr = fcp_port_head;
1485 	while (pptr) {
1486 		if (pptr->port_instance == (uint32_t)fioctl.fp_minor) {
1487 			break;
1488 		} else {
1489 			pptr = pptr->port_next;
1490 		}
1491 	}
1492 	mutex_exit(&fcp_global_mutex);
1493 	if (pptr == NULL) {
1494 		return (ENXIO);
1495 	}
1496 	mutex_enter(&pptr->port_mutex);
1497 
1498 
1499 	if ((dev_data = kmem_zalloc((sizeof (struct device_data)) *
1500 	    fioctl.listlen, KM_NOSLEEP)) == NULL) {
1501 		mutex_exit(&pptr->port_mutex);
1502 		return (ENOMEM);
1503 	}
1504 
1505 	if (ddi_copyin(fioctl.list, dev_data,
1506 	    (sizeof (struct device_data)) * fioctl.listlen, mode)) {
1507 		kmem_free(dev_data, sizeof (*dev_data) * fioctl.listlen);
1508 		mutex_exit(&pptr->port_mutex);
1509 		return (EFAULT);
1510 	}
1511 	link_cnt = pptr->port_link_cnt;
1512 
1513 	if (cmd == FCP_TGT_INQUIRY) {
1514 		wwn_ptr = (la_wwn_t *)&(dev_data[0].dev_pwwn);
1515 		if (bcmp(wwn_ptr->raw_wwn, pptr->port_pwwn.raw_wwn,
1516 		    sizeof (wwn_ptr->raw_wwn)) == 0) {
1517 			/* This ioctl is requesting INQ info of local HBA */
1518 			mutex_exit(&pptr->port_mutex);
1519 			dev_data[0].dev0_type = DTYPE_UNKNOWN;
1520 			dev_data[0].dev_status = 0;
1521 			if (ddi_copyout(dev_data, fioctl.list,
1522 			    (sizeof (struct device_data)) * fioctl.listlen,
1523 			    mode)) {
1524 				kmem_free(dev_data,
1525 				    sizeof (*dev_data) * fioctl.listlen);
1526 				return (EFAULT);
1527 			}
1528 			kmem_free(dev_data,
1529 			    sizeof (*dev_data) * fioctl.listlen);
1530 #ifdef	_MULTI_DATAMODEL
1531 			switch (ddi_model_convert_from(mode & FMODELS)) {
1532 			case DDI_MODEL_ILP32: {
1533 				struct fcp32_ioctl f32_ioctl;
1534 				f32_ioctl.fp_minor = fioctl.fp_minor;
1535 				f32_ioctl.listlen = fioctl.listlen;
1536 				f32_ioctl.list = (caddr32_t)(long)fioctl.list;
1537 				if (ddi_copyout((void *)&f32_ioctl,
1538 				    (void *)data,
1539 				    sizeof (struct fcp32_ioctl), mode)) {
1540 					return (EFAULT);
1541 				}
1542 				break;
1543 			}
1544 			case DDI_MODEL_NONE:
1545 				if (ddi_copyout((void *)&fioctl, (void *)data,
1546 				    sizeof (struct fcp_ioctl), mode)) {
1547 					return (EFAULT);
1548 				}
1549 				break;
1550 			}
1551 #else	/* _MULTI_DATAMODEL */
1552 			if (ddi_copyout((void *)&fioctl, (void *)data,
1553 			    sizeof (struct fcp_ioctl), mode)) {
1554 				return (EFAULT);
1555 			}
1556 #endif	/* _MULTI_DATAMODEL */
1557 			return (0);
1558 		}
1559 	}
1560 
1561 	if (pptr->port_state & (FCP_STATE_INIT | FCP_STATE_OFFLINE)) {
1562 		kmem_free(dev_data, sizeof (*dev_data) * fioctl.listlen);
1563 		mutex_exit(&pptr->port_mutex);
1564 		return (ENXIO);
1565 	}
1566 
1567 	for (i = 0; (i < fioctl.listlen) && (link_cnt == pptr->port_link_cnt);
1568 	    i++) {
1569 		wwn_ptr = (la_wwn_t *)&(dev_data[i].dev_pwwn);
1570 
1571 		dev_data[i].dev0_type = DTYPE_UNKNOWN;
1572 
1573 
1574 		dev_data[i].dev_status = ENXIO;
1575 
1576 		if ((ptgt = fcp_lookup_target(pptr,
1577 		    (uchar_t *)wwn_ptr)) == NULL) {
1578 			mutex_exit(&pptr->port_mutex);
1579 			if (fc_ulp_get_remote_port(pptr->port_fp_handle,
1580 			    wwn_ptr, &error, 0) == NULL) {
1581 				dev_data[i].dev_status = ENODEV;
1582 				mutex_enter(&pptr->port_mutex);
1583 				continue;
1584 			} else {
1585 
1586 				dev_data[i].dev_status = EAGAIN;
1587 
1588 				mutex_enter(&pptr->port_mutex);
1589 				continue;
1590 			}
1591 		} else {
1592 			mutex_enter(&ptgt->tgt_mutex);
1593 			if (ptgt->tgt_state & (FCP_TGT_MARK |
1594 			    FCP_TGT_BUSY)) {
1595 				dev_data[i].dev_status = EAGAIN;
1596 				mutex_exit(&ptgt->tgt_mutex);
1597 				continue;
1598 			}
1599 
1600 			if (ptgt->tgt_state & FCP_TGT_OFFLINE) {
1601 				if (ptgt->tgt_icap && !ptgt->tgt_tcap) {
1602 					dev_data[i].dev_status = ENOTSUP;
1603 				} else {
1604 					dev_data[i].dev_status = ENXIO;
1605 				}
1606 				mutex_exit(&ptgt->tgt_mutex);
1607 				continue;
1608 			}
1609 
1610 			switch (cmd) {
1611 			case FCP_TGT_INQUIRY:
1612 				/*
1613 				 * The reason we give device type of
1614 				 * lun 0 only even though in some
1615 				 * cases(like maxstrat) lun 0 device
1616 				 * type may be 0x3f(invalid) is that
1617 				 * for bridge boxes target will appear
1618 				 * as luns and the first lun could be
1619 				 * a device that utility may not care
1620 				 * about (like a tape device).
1621 				 */
1622 				dev_data[i].dev_lun_cnt = ptgt->tgt_lun_cnt;
1623 				dev_data[i].dev_status = 0;
1624 				mutex_exit(&ptgt->tgt_mutex);
1625 
1626 				if ((plun = fcp_get_lun(ptgt, 0)) == NULL) {
1627 					dev_data[i].dev0_type = DTYPE_UNKNOWN;
1628 				} else {
1629 					dev_data[i].dev0_type = plun->lun_type;
1630 				}
1631 				mutex_enter(&ptgt->tgt_mutex);
1632 				break;
1633 
1634 			case FCP_TGT_CREATE:
1635 				mutex_exit(&ptgt->tgt_mutex);
1636 				mutex_exit(&pptr->port_mutex);
1637 
1638 				/*
1639 				 * serialize state change call backs.
1640 				 * only one call back will be handled
1641 				 * at a time.
1642 				 */
1643 				mutex_enter(&fcp_global_mutex);
1644 				if (fcp_oflag & FCP_BUSY) {
1645 					mutex_exit(&fcp_global_mutex);
1646 					if (dev_data) {
1647 						kmem_free(dev_data,
1648 						    sizeof (*dev_data) *
1649 						    fioctl.listlen);
1650 					}
1651 					return (EBUSY);
1652 				}
1653 				fcp_oflag |= FCP_BUSY;
1654 				mutex_exit(&fcp_global_mutex);
1655 
1656 				dev_data[i].dev_status =
1657 				    fcp_create_on_demand(pptr,
1658 				    wwn_ptr->raw_wwn);
1659 
1660 				if (dev_data[i].dev_status != 0) {
1661 					char	buf[25];
1662 
1663 					for (i = 0; i < FC_WWN_SIZE; i++) {
1664 						(void) sprintf(&buf[i << 1],
1665 						    "%02x",
1666 						    wwn_ptr->raw_wwn[i]);
1667 					}
1668 
1669 					fcp_log(CE_WARN, pptr->port_dip,
1670 					    "!Failed to create nodes for"
1671 					    " pwwn=%s; error=%x", buf,
1672 					    dev_data[i].dev_status);
1673 				}
1674 
1675 				/* allow state change call backs again */
1676 				mutex_enter(&fcp_global_mutex);
1677 				fcp_oflag &= ~FCP_BUSY;
1678 				mutex_exit(&fcp_global_mutex);
1679 
1680 				mutex_enter(&pptr->port_mutex);
1681 				mutex_enter(&ptgt->tgt_mutex);
1682 
1683 				break;
1684 
1685 			case FCP_TGT_DELETE:
1686 				break;
1687 
1688 			default:
1689 				fcp_log(CE_WARN, pptr->port_dip,
1690 				    "!Invalid device data ioctl "
1691 				    "opcode = 0x%x", cmd);
1692 			}
1693 			mutex_exit(&ptgt->tgt_mutex);
1694 		}
1695 	}
1696 	mutex_exit(&pptr->port_mutex);
1697 
1698 	if (ddi_copyout(dev_data, fioctl.list,
1699 	    (sizeof (struct device_data)) * fioctl.listlen, mode)) {
1700 		kmem_free(dev_data, sizeof (*dev_data) * fioctl.listlen);
1701 		return (EFAULT);
1702 	}
1703 	kmem_free(dev_data, sizeof (*dev_data) * fioctl.listlen);
1704 
1705 #ifdef	_MULTI_DATAMODEL
1706 	switch (ddi_model_convert_from(mode & FMODELS)) {
1707 	case DDI_MODEL_ILP32: {
1708 		struct fcp32_ioctl f32_ioctl;
1709 
1710 		f32_ioctl.fp_minor = fioctl.fp_minor;
1711 		f32_ioctl.listlen = fioctl.listlen;
1712 		f32_ioctl.list = (caddr32_t)(long)fioctl.list;
1713 		if (ddi_copyout((void *)&f32_ioctl, (void *)data,
1714 		    sizeof (struct fcp32_ioctl), mode)) {
1715 			return (EFAULT);
1716 		}
1717 		break;
1718 	}
1719 	case DDI_MODEL_NONE:
1720 		if (ddi_copyout((void *)&fioctl, (void *)data,
1721 		    sizeof (struct fcp_ioctl), mode)) {
1722 			return (EFAULT);
1723 		}
1724 		break;
1725 	}
1726 #else	/* _MULTI_DATAMODEL */
1727 
1728 	if (ddi_copyout((void *)&fioctl, (void *)data,
1729 	    sizeof (struct fcp_ioctl), mode)) {
1730 		return (EFAULT);
1731 	}
1732 #endif	/* _MULTI_DATAMODEL */
1733 
1734 	return (0);
1735 }
1736 
1737 /*
1738  * Fetch the target mappings (path, etc.) for all LUNs
1739  * on this port.
1740  */
1741 /* ARGSUSED */
1742 static int
1743 fcp_get_target_mappings(struct fcp_ioctl *data,
1744     int mode, int *rval)
1745 {
1746 	struct fcp_port	    *pptr;
1747 	fc_hba_target_mappings_t    *mappings;
1748 	fc_hba_mapping_entry_t	    *map;
1749 	struct fcp_tgt	    *ptgt = NULL;
1750 	struct fcp_lun	    *plun = NULL;
1751 	int			    i, mapIndex, mappingSize;
1752 	int			    listlen;
1753 	struct fcp_ioctl	    fioctl;
1754 	char			    *path;
1755 	fcp_ent_addr_t		    sam_lun_addr;
1756 
1757 #ifdef	_MULTI_DATAMODEL
1758 	switch (ddi_model_convert_from(mode & FMODELS)) {
1759 	case DDI_MODEL_ILP32: {
1760 		struct fcp32_ioctl f32_ioctl;
1761 
1762 		if (ddi_copyin((void *)data, (void *)&f32_ioctl,
1763 		    sizeof (struct fcp32_ioctl), mode)) {
1764 			return (EFAULT);
1765 		}
1766 		fioctl.fp_minor = f32_ioctl.fp_minor;
1767 		fioctl.listlen = f32_ioctl.listlen;
1768 		fioctl.list = (caddr_t)(long)f32_ioctl.list;
1769 		break;
1770 	}
1771 	case DDI_MODEL_NONE:
1772 		if (ddi_copyin((void *)data, (void *)&fioctl,
1773 		    sizeof (struct fcp_ioctl), mode)) {
1774 			return (EFAULT);
1775 		}
1776 		break;
1777 	}
1778 
1779 #else	/* _MULTI_DATAMODEL */
1780 	if (ddi_copyin((void *)data, (void *)&fioctl,
1781 	    sizeof (struct fcp_ioctl), mode)) {
1782 		return (EFAULT);
1783 	}
1784 #endif	/* _MULTI_DATAMODEL */
1785 
1786 	/*
1787 	 * Right now we can assume that the minor number matches with
1788 	 * this instance of fp. If this changes we will need to
1789 	 * revisit this logic.
1790 	 */
1791 	mutex_enter(&fcp_global_mutex);
1792 	pptr = fcp_port_head;
1793 	while (pptr) {
1794 		if (pptr->port_instance == (uint32_t)fioctl.fp_minor) {
1795 			break;
1796 		} else {
1797 			pptr = pptr->port_next;
1798 		}
1799 	}
1800 	mutex_exit(&fcp_global_mutex);
1801 	if (pptr == NULL) {
1802 		cmn_err(CE_NOTE, "target mappings: unknown instance number: %d",
1803 		    fioctl.fp_minor);
1804 		return (ENXIO);
1805 	}
1806 
1807 
1808 	/* We use listlen to show the total buffer size */
1809 	mappingSize = fioctl.listlen;
1810 
1811 	/* Now calculate how many mapping entries will fit */
1812 	listlen = fioctl.listlen + sizeof (fc_hba_mapping_entry_t)
1813 	    - sizeof (fc_hba_target_mappings_t);
1814 	if (listlen <= 0) {
1815 		cmn_err(CE_NOTE, "target mappings: Insufficient buffer");
1816 		return (ENXIO);
1817 	}
1818 	listlen = listlen / sizeof (fc_hba_mapping_entry_t);
1819 
1820 	if ((mappings = kmem_zalloc(mappingSize, KM_SLEEP)) == NULL) {
1821 		return (ENOMEM);
1822 	}
1823 	mappings->version = FC_HBA_TARGET_MAPPINGS_VERSION;
1824 
1825 	/* Now get to work */
1826 	mapIndex = 0;
1827 
1828 	mutex_enter(&pptr->port_mutex);
1829 	/* Loop through all targets on this port */
1830 	for (i = 0; i < FCP_NUM_HASH; i++) {
1831 		for (ptgt = pptr->port_tgt_hash_table[i]; ptgt != NULL;
1832 		    ptgt = ptgt->tgt_next) {
1833 
1834 
1835 			/* Loop through all LUNs on this target */
1836 			for (plun = ptgt->tgt_lun; plun != NULL;
1837 			    plun = plun->lun_next) {
1838 				if (plun->lun_state & FCP_LUN_OFFLINE) {
1839 					continue;
1840 				}
1841 
1842 				path = fcp_get_lun_path(plun);
1843 				if (path == NULL) {
1844 					continue;
1845 				}
1846 
1847 				if (mapIndex >= listlen) {
1848 					mapIndex ++;
1849 					kmem_free(path, MAXPATHLEN);
1850 					continue;
1851 				}
1852 				map = &mappings->entries[mapIndex++];
1853 				bcopy(path, map->targetDriver,
1854 				    sizeof (map->targetDriver));
1855 				map->d_id = ptgt->tgt_d_id;
1856 				map->busNumber = 0;
1857 				map->targetNumber = ptgt->tgt_d_id;
1858 				map->osLUN = plun->lun_num;
1859 
1860 				/*
1861 				 * We had swapped lun when we stored it in
1862 				 * lun_addr. We need to swap it back before
1863 				 * returning it to user land
1864 				 */
1865 
1866 				sam_lun_addr.ent_addr_0 =
1867 				    BE_16(plun->lun_addr.ent_addr_0);
1868 				sam_lun_addr.ent_addr_1 =
1869 				    BE_16(plun->lun_addr.ent_addr_1);
1870 				sam_lun_addr.ent_addr_2 =
1871 				    BE_16(plun->lun_addr.ent_addr_2);
1872 				sam_lun_addr.ent_addr_3 =
1873 				    BE_16(plun->lun_addr.ent_addr_3);
1874 
1875 				bcopy(&sam_lun_addr, &map->samLUN,
1876 				    FCP_LUN_SIZE);
1877 				bcopy(ptgt->tgt_node_wwn.raw_wwn,
1878 				    map->NodeWWN.raw_wwn, sizeof (la_wwn_t));
1879 				bcopy(ptgt->tgt_port_wwn.raw_wwn,
1880 				    map->PortWWN.raw_wwn, sizeof (la_wwn_t));
1881 
1882 				if (plun->lun_guid) {
1883 
1884 					/* convert ascii wwn to bytes */
1885 					fcp_ascii_to_wwn(plun->lun_guid,
1886 					    map->guid, sizeof (map->guid));
1887 
1888 					if ((sizeof (map->guid)) <
1889 					    plun->lun_guid_size / 2) {
1890 						cmn_err(CE_WARN,
1891 						    "fcp_get_target_mappings:"
1892 						    "guid copy space "
1893 						    "insufficient."
1894 						    "Copy Truncation - "
1895 						    "available %d; need %d",
1896 						    (int)sizeof (map->guid),
1897 						    (int)
1898 						    plun->lun_guid_size / 2);
1899 					}
1900 				}
1901 				kmem_free(path, MAXPATHLEN);
1902 			}
1903 		}
1904 	}
1905 	mutex_exit(&pptr->port_mutex);
1906 	mappings->numLuns = mapIndex;
1907 
1908 	if (ddi_copyout(mappings, fioctl.list, mappingSize, mode)) {
1909 		kmem_free(mappings, mappingSize);
1910 		return (EFAULT);
1911 	}
1912 	kmem_free(mappings, mappingSize);
1913 
1914 #ifdef	_MULTI_DATAMODEL
1915 	switch (ddi_model_convert_from(mode & FMODELS)) {
1916 	case DDI_MODEL_ILP32: {
1917 		struct fcp32_ioctl f32_ioctl;
1918 
1919 		f32_ioctl.fp_minor = fioctl.fp_minor;
1920 		f32_ioctl.listlen = fioctl.listlen;
1921 		f32_ioctl.list = (caddr32_t)(long)fioctl.list;
1922 		if (ddi_copyout((void *)&f32_ioctl, (void *)data,
1923 		    sizeof (struct fcp32_ioctl), mode)) {
1924 			return (EFAULT);
1925 		}
1926 		break;
1927 	}
1928 	case DDI_MODEL_NONE:
1929 		if (ddi_copyout((void *)&fioctl, (void *)data,
1930 		    sizeof (struct fcp_ioctl), mode)) {
1931 			return (EFAULT);
1932 		}
1933 		break;
1934 	}
1935 #else	/* _MULTI_DATAMODEL */
1936 
1937 	if (ddi_copyout((void *)&fioctl, (void *)data,
1938 	    sizeof (struct fcp_ioctl), mode)) {
1939 		return (EFAULT);
1940 	}
1941 #endif	/* _MULTI_DATAMODEL */
1942 
1943 	return (0);
1944 }
1945 
1946 /*
1947  * fcp_setup_scsi_ioctl
1948  *	Setup handler for the "scsi passthru" style of
1949  *	ioctl for FCP.	See "fcp_util.h" for data structure
1950  *	definition.
1951  *
1952  * Input:
1953  *	u_fscsi	= ioctl data (user address space)
1954  *	mode	= See ioctl(9E)
1955  *
1956  * Output:
1957  *	u_fscsi	= ioctl data (user address space)
1958  *	rval	= return value - see ioctl(9E)
1959  *
1960  * Returns:
1961  *	0	= OK
1962  *	EAGAIN	= See errno.h
1963  *	EBUSY	= See errno.h
1964  *	EFAULT	= See errno.h
1965  *	EINTR	= See errno.h
1966  *	EINVAL	= See errno.h
1967  *	EIO	= See errno.h
1968  *	ENOMEM	= See errno.h
1969  *	ENXIO	= See errno.h
1970  *
1971  * Context:
1972  *	Kernel context.
1973  */
1974 /* ARGSUSED */
1975 static int
1976 fcp_setup_scsi_ioctl(struct fcp_scsi_cmd *u_fscsi,
1977     int mode, int *rval)
1978 {
1979 	int			ret		= 0;
1980 	int			temp_ret;
1981 	caddr_t			k_cdbbufaddr	= NULL;
1982 	caddr_t			k_bufaddr	= NULL;
1983 	caddr_t			k_rqbufaddr	= NULL;
1984 	caddr_t			u_cdbbufaddr;
1985 	caddr_t			u_bufaddr;
1986 	caddr_t			u_rqbufaddr;
1987 	struct fcp_scsi_cmd	k_fscsi;
1988 
1989 	/*
1990 	 * Get fcp_scsi_cmd array element from user address space
1991 	 */
1992 	if ((ret = fcp_copyin_scsi_cmd((caddr_t)u_fscsi, &k_fscsi, mode))
1993 	    != 0) {
1994 		return (ret);
1995 	}
1996 
1997 
1998 	/*
1999 	 * Even though kmem_alloc() checks the validity of the
2000 	 * buffer length, this check is needed when the
2001 	 * kmem_flags set and the zero buffer length is passed.
2002 	 */
2003 	if ((k_fscsi.scsi_cdblen <= 0) ||
2004 	    (k_fscsi.scsi_buflen <= 0) ||
2005 	    (k_fscsi.scsi_buflen > FCP_MAX_RESPONSE_LEN) ||
2006 	    (k_fscsi.scsi_rqlen <= 0) ||
2007 	    (k_fscsi.scsi_rqlen > FCP_MAX_SENSE_LEN)) {
2008 		return (EINVAL);
2009 	}
2010 
2011 	/*
2012 	 * Allocate data for fcp_scsi_cmd pointer fields
2013 	 */
2014 	if (ret == 0) {
2015 		k_cdbbufaddr = kmem_alloc(k_fscsi.scsi_cdblen, KM_NOSLEEP);
2016 		k_bufaddr    = kmem_alloc(k_fscsi.scsi_buflen, KM_NOSLEEP);
2017 		k_rqbufaddr  = kmem_alloc(k_fscsi.scsi_rqlen,  KM_NOSLEEP);
2018 
2019 		if (k_cdbbufaddr == NULL ||
2020 		    k_bufaddr	 == NULL ||
2021 		    k_rqbufaddr	 == NULL) {
2022 			ret = ENOMEM;
2023 		}
2024 	}
2025 
2026 	/*
2027 	 * Get fcp_scsi_cmd pointer fields from user
2028 	 * address space
2029 	 */
2030 	if (ret == 0) {
2031 		u_cdbbufaddr = k_fscsi.scsi_cdbbufaddr;
2032 		u_bufaddr    = k_fscsi.scsi_bufaddr;
2033 		u_rqbufaddr  = k_fscsi.scsi_rqbufaddr;
2034 
2035 		if (ddi_copyin(u_cdbbufaddr,
2036 		    k_cdbbufaddr,
2037 		    k_fscsi.scsi_cdblen,
2038 		    mode)) {
2039 			ret = EFAULT;
2040 		} else if (ddi_copyin(u_bufaddr,
2041 		    k_bufaddr,
2042 		    k_fscsi.scsi_buflen,
2043 		    mode)) {
2044 			ret = EFAULT;
2045 		} else if (ddi_copyin(u_rqbufaddr,
2046 		    k_rqbufaddr,
2047 		    k_fscsi.scsi_rqlen,
2048 		    mode)) {
2049 			ret = EFAULT;
2050 		}
2051 	}
2052 
2053 	/*
2054 	 * Send scsi command (blocking)
2055 	 */
2056 	if (ret == 0) {
2057 		/*
2058 		 * Prior to sending the scsi command, the
2059 		 * fcp_scsi_cmd data structure must contain kernel,
2060 		 * not user, addresses.
2061 		 */
2062 		k_fscsi.scsi_cdbbufaddr	= k_cdbbufaddr;
2063 		k_fscsi.scsi_bufaddr	= k_bufaddr;
2064 		k_fscsi.scsi_rqbufaddr	= k_rqbufaddr;
2065 
2066 		ret = fcp_send_scsi_ioctl(&k_fscsi);
2067 
2068 		/*
2069 		 * After sending the scsi command, the
2070 		 * fcp_scsi_cmd data structure must contain user,
2071 		 * not kernel, addresses.
2072 		 */
2073 		k_fscsi.scsi_cdbbufaddr	= u_cdbbufaddr;
2074 		k_fscsi.scsi_bufaddr	= u_bufaddr;
2075 		k_fscsi.scsi_rqbufaddr	= u_rqbufaddr;
2076 	}
2077 
2078 	/*
2079 	 * Put fcp_scsi_cmd pointer fields to user address space
2080 	 */
2081 	if (ret == 0) {
2082 		if (ddi_copyout(k_cdbbufaddr,
2083 		    u_cdbbufaddr,
2084 		    k_fscsi.scsi_cdblen,
2085 		    mode)) {
2086 			ret = EFAULT;
2087 		} else if (ddi_copyout(k_bufaddr,
2088 		    u_bufaddr,
2089 		    k_fscsi.scsi_buflen,
2090 		    mode)) {
2091 			ret = EFAULT;
2092 		} else if (ddi_copyout(k_rqbufaddr,
2093 		    u_rqbufaddr,
2094 		    k_fscsi.scsi_rqlen,
2095 		    mode)) {
2096 			ret = EFAULT;
2097 		}
2098 	}
2099 
2100 	/*
2101 	 * Free data for fcp_scsi_cmd pointer fields
2102 	 */
2103 	if (k_cdbbufaddr != NULL) {
2104 		kmem_free(k_cdbbufaddr, k_fscsi.scsi_cdblen);
2105 	}
2106 	if (k_bufaddr != NULL) {
2107 		kmem_free(k_bufaddr, k_fscsi.scsi_buflen);
2108 	}
2109 	if (k_rqbufaddr != NULL) {
2110 		kmem_free(k_rqbufaddr, k_fscsi.scsi_rqlen);
2111 	}
2112 
2113 	/*
2114 	 * Put fcp_scsi_cmd array element to user address space
2115 	 */
2116 	temp_ret = fcp_copyout_scsi_cmd(&k_fscsi, (caddr_t)u_fscsi, mode);
2117 	if (temp_ret != 0) {
2118 		ret = temp_ret;
2119 	}
2120 
2121 	/*
2122 	 * Return status
2123 	 */
2124 	return (ret);
2125 }
2126 
2127 
2128 /*
2129  * fcp_copyin_scsi_cmd
2130  *	Copy in fcp_scsi_cmd data structure from user address space.
2131  *	The data may be in 32 bit or 64 bit modes.
2132  *
2133  * Input:
2134  *	base_addr	= from address (user address space)
2135  *	mode		= See ioctl(9E) and ddi_copyin(9F)
2136  *
2137  * Output:
2138  *	fscsi		= to address (kernel address space)
2139  *
2140  * Returns:
2141  *	0	= OK
2142  *	EFAULT	= Error
2143  *
2144  * Context:
2145  *	Kernel context.
2146  */
2147 static int
2148 fcp_copyin_scsi_cmd(caddr_t base_addr, struct fcp_scsi_cmd *fscsi, int mode)
2149 {
2150 #ifdef	_MULTI_DATAMODEL
2151 	struct fcp32_scsi_cmd	f32scsi;
2152 
2153 	switch (ddi_model_convert_from(mode & FMODELS)) {
2154 	case DDI_MODEL_ILP32:
2155 		/*
2156 		 * Copy data from user address space
2157 		 */
2158 		if (ddi_copyin((void *)base_addr,
2159 		    &f32scsi,
2160 		    sizeof (struct fcp32_scsi_cmd),
2161 		    mode)) {
2162 			return (EFAULT);
2163 		}
2164 		/*
2165 		 * Convert from 32 bit to 64 bit
2166 		 */
2167 		FCP32_SCSI_CMD_TO_FCP_SCSI_CMD(&f32scsi, fscsi);
2168 		break;
2169 	case DDI_MODEL_NONE:
2170 		/*
2171 		 * Copy data from user address space
2172 		 */
2173 		if (ddi_copyin((void *)base_addr,
2174 		    fscsi,
2175 		    sizeof (struct fcp_scsi_cmd),
2176 		    mode)) {
2177 			return (EFAULT);
2178 		}
2179 		break;
2180 	}
2181 #else	/* _MULTI_DATAMODEL */
2182 	/*
2183 	 * Copy data from user address space
2184 	 */
2185 	if (ddi_copyin((void *)base_addr,
2186 	    fscsi,
2187 	    sizeof (struct fcp_scsi_cmd),
2188 	    mode)) {
2189 		return (EFAULT);
2190 	}
2191 #endif	/* _MULTI_DATAMODEL */
2192 
2193 	return (0);
2194 }
2195 
2196 
2197 /*
2198  * fcp_copyout_scsi_cmd
2199  *	Copy out fcp_scsi_cmd data structure to user address space.
2200  *	The data may be in 32 bit or 64 bit modes.
2201  *
2202  * Input:
2203  *	fscsi		= to address (kernel address space)
2204  *	mode		= See ioctl(9E) and ddi_copyin(9F)
2205  *
2206  * Output:
2207  *	base_addr	= from address (user address space)
2208  *
2209  * Returns:
2210  *	0	= OK
2211  *	EFAULT	= Error
2212  *
2213  * Context:
2214  *	Kernel context.
2215  */
2216 static int
2217 fcp_copyout_scsi_cmd(struct fcp_scsi_cmd *fscsi, caddr_t base_addr, int mode)
2218 {
2219 #ifdef	_MULTI_DATAMODEL
2220 	struct fcp32_scsi_cmd	f32scsi;
2221 
2222 	switch (ddi_model_convert_from(mode & FMODELS)) {
2223 	case DDI_MODEL_ILP32:
2224 		/*
2225 		 * Convert from 64 bit to 32 bit
2226 		 */
2227 		FCP_SCSI_CMD_TO_FCP32_SCSI_CMD(fscsi, &f32scsi);
2228 		/*
2229 		 * Copy data to user address space
2230 		 */
2231 		if (ddi_copyout(&f32scsi,
2232 		    (void *)base_addr,
2233 		    sizeof (struct fcp32_scsi_cmd),
2234 		    mode)) {
2235 			return (EFAULT);
2236 		}
2237 		break;
2238 	case DDI_MODEL_NONE:
2239 		/*
2240 		 * Copy data to user address space
2241 		 */
2242 		if (ddi_copyout(fscsi,
2243 		    (void *)base_addr,
2244 		    sizeof (struct fcp_scsi_cmd),
2245 		    mode)) {
2246 			return (EFAULT);
2247 		}
2248 		break;
2249 	}
2250 #else	/* _MULTI_DATAMODEL */
2251 	/*
2252 	 * Copy data to user address space
2253 	 */
2254 	if (ddi_copyout(fscsi,
2255 	    (void *)base_addr,
2256 	    sizeof (struct fcp_scsi_cmd),
2257 	    mode)) {
2258 		return (EFAULT);
2259 	}
2260 #endif	/* _MULTI_DATAMODEL */
2261 
2262 	return (0);
2263 }
2264 
2265 
2266 /*
2267  * fcp_send_scsi_ioctl
2268  *	Sends the SCSI command in blocking mode.
2269  *
2270  * Input:
2271  *	fscsi		= SCSI command data structure
2272  *
2273  * Output:
2274  *	fscsi		= SCSI command data structure
2275  *
2276  * Returns:
2277  *	0	= OK
2278  *	EAGAIN	= See errno.h
2279  *	EBUSY	= See errno.h
2280  *	EINTR	= See errno.h
2281  *	EINVAL	= See errno.h
2282  *	EIO	= See errno.h
2283  *	ENOMEM	= See errno.h
2284  *	ENXIO	= See errno.h
2285  *
2286  * Context:
2287  *	Kernel context.
2288  */
2289 static int
2290 fcp_send_scsi_ioctl(struct fcp_scsi_cmd *fscsi)
2291 {
2292 	struct fcp_lun	*plun		= NULL;
2293 	struct fcp_port	*pptr		= NULL;
2294 	struct fcp_tgt	*ptgt		= NULL;
2295 	fc_packet_t		*fpkt		= NULL;
2296 	struct fcp_ipkt	*icmd		= NULL;
2297 	int			target_created	= FALSE;
2298 	fc_frame_hdr_t		*hp;
2299 	struct fcp_cmd		fcp_cmd;
2300 	struct fcp_cmd		*fcmd;
2301 	union scsi_cdb		*scsi_cdb;
2302 	la_wwn_t		*wwn_ptr;
2303 	int			nodma;
2304 	struct fcp_rsp		*rsp;
2305 	struct fcp_rsp_info	*rsp_info;
2306 	caddr_t			rsp_sense;
2307 	int			buf_len;
2308 	int			info_len;
2309 	int			sense_len;
2310 	struct scsi_extended_sense	*sense_to = NULL;
2311 	timeout_id_t		tid;
2312 	uint8_t			reconfig_lun = FALSE;
2313 	uint8_t			reconfig_pending = FALSE;
2314 	uint8_t			scsi_cmd;
2315 	int			rsp_len;
2316 	int			cmd_index;
2317 	int			fc_status;
2318 	int			pkt_state;
2319 	int			pkt_action;
2320 	int			pkt_reason;
2321 	int			ret, xport_retval = ~FC_SUCCESS;
2322 	int			lcount;
2323 	int			tcount;
2324 	int			reconfig_status;
2325 	int			port_busy = FALSE;
2326 	uchar_t			*lun_string;
2327 
2328 	/*
2329 	 * Check valid SCSI command
2330 	 */
2331 	scsi_cmd = ((uint8_t *)fscsi->scsi_cdbbufaddr)[0];
2332 	ret = EINVAL;
2333 	for (cmd_index = 0;
2334 	    cmd_index < FCP_NUM_ELEMENTS(scsi_ioctl_list) &&
2335 	    ret != 0;
2336 	    cmd_index++) {
2337 		/*
2338 		 * First byte of CDB is the SCSI command
2339 		 */
2340 		if (scsi_ioctl_list[cmd_index] == scsi_cmd) {
2341 			ret = 0;
2342 		}
2343 	}
2344 
2345 	/*
2346 	 * Check inputs
2347 	 */
2348 	if (fscsi->scsi_flags != FCP_SCSI_READ) {
2349 		ret = EINVAL;
2350 	} else if (fscsi->scsi_cdblen > FCP_CDB_SIZE) {
2351 		/* no larger than */
2352 		ret = EINVAL;
2353 	}
2354 
2355 
2356 	/*
2357 	 * Find FC port
2358 	 */
2359 	if (ret == 0) {
2360 		/*
2361 		 * Acquire global mutex
2362 		 */
2363 		mutex_enter(&fcp_global_mutex);
2364 
2365 		pptr = fcp_port_head;
2366 		while (pptr) {
2367 			if (pptr->port_instance ==
2368 			    (uint32_t)fscsi->scsi_fc_port_num) {
2369 				break;
2370 			} else {
2371 				pptr = pptr->port_next;
2372 			}
2373 		}
2374 
2375 		if (pptr == NULL) {
2376 			ret = ENXIO;
2377 		} else {
2378 			/*
2379 			 * fc_ulp_busy_port can raise power
2380 			 *  so, we must not hold any mutexes involved in PM
2381 			 */
2382 			mutex_exit(&fcp_global_mutex);
2383 			ret = fc_ulp_busy_port(pptr->port_fp_handle);
2384 		}
2385 
2386 		if (ret == 0) {
2387 
2388 			/* remember port is busy, so we will release later */
2389 			port_busy = TRUE;
2390 
2391 			/*
2392 			 * If there is a reconfiguration in progress, wait
2393 			 * for it to complete.
2394 			 */
2395 
2396 			fcp_reconfig_wait(pptr);
2397 
2398 			/* reacquire mutexes in order */
2399 			mutex_enter(&fcp_global_mutex);
2400 			mutex_enter(&pptr->port_mutex);
2401 
2402 			/*
2403 			 * Will port accept DMA?
2404 			 */
2405 			nodma = (pptr->port_fcp_dma == FC_NO_DVMA_SPACE)
2406 			    ? 1 : 0;
2407 
2408 			/*
2409 			 * If init or offline, device not known
2410 			 *
2411 			 * If we are discovering (onlining), we can
2412 			 * NOT obviously provide reliable data about
2413 			 * devices until it is complete
2414 			 */
2415 			if (pptr->port_state &	  (FCP_STATE_INIT |
2416 			    FCP_STATE_OFFLINE)) {
2417 				ret = ENXIO;
2418 			} else if (pptr->port_state & FCP_STATE_ONLINING) {
2419 				ret = EBUSY;
2420 			} else {
2421 				/*
2422 				 * Find target from pwwn
2423 				 *
2424 				 * The wwn must be put into a local
2425 				 * variable to ensure alignment.
2426 				 */
2427 				wwn_ptr = (la_wwn_t *)&(fscsi->scsi_fc_pwwn);
2428 				ptgt = fcp_lookup_target(pptr,
2429 				    (uchar_t *)wwn_ptr);
2430 
2431 				/*
2432 				 * If target not found,
2433 				 */
2434 				if (ptgt == NULL) {
2435 					/*
2436 					 * Note: Still have global &
2437 					 * port mutexes
2438 					 */
2439 					mutex_exit(&pptr->port_mutex);
2440 					ptgt = fcp_port_create_tgt(pptr,
2441 					    wwn_ptr, &ret, &fc_status,
2442 					    &pkt_state, &pkt_action,
2443 					    &pkt_reason);
2444 					mutex_enter(&pptr->port_mutex);
2445 
2446 					fscsi->scsi_fc_status  = fc_status;
2447 					fscsi->scsi_pkt_state  =
2448 					    (uchar_t)pkt_state;
2449 					fscsi->scsi_pkt_reason = pkt_reason;
2450 					fscsi->scsi_pkt_action =
2451 					    (uchar_t)pkt_action;
2452 
2453 					if (ptgt != NULL) {
2454 						target_created = TRUE;
2455 					} else if (ret == 0) {
2456 						ret = ENOMEM;
2457 					}
2458 				}
2459 
2460 				if (ret == 0) {
2461 					/*
2462 					 * Acquire target
2463 					 */
2464 					mutex_enter(&ptgt->tgt_mutex);
2465 
2466 					/*
2467 					 * If target is mark or busy,
2468 					 * then target can not be used
2469 					 */
2470 					if (ptgt->tgt_state &
2471 					    (FCP_TGT_MARK |
2472 					    FCP_TGT_BUSY)) {
2473 						ret = EBUSY;
2474 					} else {
2475 						/*
2476 						 * Mark target as busy
2477 						 */
2478 						ptgt->tgt_state |=
2479 						    FCP_TGT_BUSY;
2480 					}
2481 
2482 					/*
2483 					 * Release target
2484 					 */
2485 					lcount = pptr->port_link_cnt;
2486 					tcount = ptgt->tgt_change_cnt;
2487 					mutex_exit(&ptgt->tgt_mutex);
2488 				}
2489 			}
2490 
2491 			/*
2492 			 * Release port
2493 			 */
2494 			mutex_exit(&pptr->port_mutex);
2495 		}
2496 
2497 		/*
2498 		 * Release global mutex
2499 		 */
2500 		mutex_exit(&fcp_global_mutex);
2501 	}
2502 
2503 	if (ret == 0) {
2504 		uint64_t belun = BE_64(fscsi->scsi_lun);
2505 
2506 		/*
2507 		 * If it's a target device, find lun from pwwn
2508 		 * The wwn must be put into a local
2509 		 * variable to ensure alignment.
2510 		 */
2511 		mutex_enter(&pptr->port_mutex);
2512 		wwn_ptr = (la_wwn_t *)&(fscsi->scsi_fc_pwwn);
2513 		if (!ptgt->tgt_tcap && ptgt->tgt_icap) {
2514 			/* this is not a target */
2515 			fscsi->scsi_fc_status = FC_DEVICE_NOT_TGT;
2516 			ret = ENXIO;
2517 		} else if ((belun << 16) != 0) {
2518 			/*
2519 			 * Since fcp only support PD and LU addressing method
2520 			 * so far, the last 6 bytes of a valid LUN are expected
2521 			 * to be filled with 00h.
2522 			 */
2523 			fscsi->scsi_fc_status = FC_INVALID_LUN;
2524 			cmn_err(CE_WARN, "fcp: Unsupported LUN addressing"
2525 			    " method 0x%02x with LUN number 0x%016" PRIx64,
2526 			    (uint8_t)(belun >> 62), belun);
2527 			ret = ENXIO;
2528 		} else if ((plun = fcp_lookup_lun(pptr, (uchar_t *)wwn_ptr,
2529 		    (uint16_t)((belun >> 48) & 0x3fff))) == NULL) {
2530 			/*
2531 			 * This is a SCSI target, but no LUN at this
2532 			 * address.
2533 			 *
2534 			 * In the future, we may want to send this to
2535 			 * the target, and let it respond
2536 			 * appropriately
2537 			 */
2538 			ret = ENXIO;
2539 		}
2540 		mutex_exit(&pptr->port_mutex);
2541 	}
2542 
2543 	/*
2544 	 * Finished grabbing external resources
2545 	 * Allocate internal packet (icmd)
2546 	 */
2547 	if (ret == 0) {
2548 		/*
2549 		 * Calc rsp len assuming rsp info included
2550 		 */
2551 		rsp_len = sizeof (struct fcp_rsp) +
2552 		    sizeof (struct fcp_rsp_info) + fscsi->scsi_rqlen;
2553 
2554 		icmd = fcp_icmd_alloc(pptr, ptgt,
2555 		    sizeof (struct fcp_cmd),
2556 		    rsp_len,
2557 		    fscsi->scsi_buflen,
2558 		    nodma,
2559 		    lcount,			/* ipkt_link_cnt */
2560 		    tcount,			/* ipkt_change_cnt */
2561 		    0,				/* cause */
2562 		    FC_INVALID_RSCN_COUNT);	/* invalidate the count */
2563 
2564 		if (icmd == NULL) {
2565 			ret = ENOMEM;
2566 		} else {
2567 			/*
2568 			 * Setup internal packet as sema sync
2569 			 */
2570 			fcp_ipkt_sema_init(icmd);
2571 		}
2572 	}
2573 
2574 	if (ret == 0) {
2575 		/*
2576 		 * Init fpkt pointer for use.
2577 		 */
2578 
2579 		fpkt = icmd->ipkt_fpkt;
2580 
2581 		fpkt->pkt_tran_flags	= FC_TRAN_CLASS3 | FC_TRAN_INTR;
2582 		fpkt->pkt_tran_type	= FC_PKT_FCP_READ; /* only rd for now */
2583 		fpkt->pkt_timeout	= fscsi->scsi_timeout;
2584 
2585 		/*
2586 		 * Init fcmd pointer for use by SCSI command
2587 		 */
2588 
2589 		if (nodma) {
2590 			fcmd = (struct fcp_cmd *)fpkt->pkt_cmd;
2591 		} else {
2592 			fcmd = &fcp_cmd;
2593 		}
2594 		bzero(fcmd, sizeof (struct fcp_cmd));
2595 		ptgt = plun->lun_tgt;
2596 
2597 		lun_string = (uchar_t *)&fscsi->scsi_lun;
2598 
2599 		fcmd->fcp_ent_addr.ent_addr_0 =
2600 		    BE_16(*(uint16_t *)&(lun_string[0]));
2601 		fcmd->fcp_ent_addr.ent_addr_1 =
2602 		    BE_16(*(uint16_t *)&(lun_string[2]));
2603 		fcmd->fcp_ent_addr.ent_addr_2 =
2604 		    BE_16(*(uint16_t *)&(lun_string[4]));
2605 		fcmd->fcp_ent_addr.ent_addr_3 =
2606 		    BE_16(*(uint16_t *)&(lun_string[6]));
2607 
2608 		/*
2609 		 * Setup internal packet(icmd)
2610 		 */
2611 		icmd->ipkt_lun		= plun;
2612 		icmd->ipkt_restart	= 0;
2613 		icmd->ipkt_retries	= 0;
2614 		icmd->ipkt_opcode	= 0;
2615 
2616 		/*
2617 		 * Init the frame HEADER Pointer for use
2618 		 */
2619 		hp = &fpkt->pkt_cmd_fhdr;
2620 
2621 		hp->s_id	= pptr->port_id;
2622 		hp->d_id	= ptgt->tgt_d_id;
2623 		hp->r_ctl	= R_CTL_COMMAND;
2624 		hp->type	= FC_TYPE_SCSI_FCP;
2625 		hp->f_ctl	= F_CTL_SEQ_INITIATIVE | F_CTL_FIRST_SEQ;
2626 		hp->rsvd	= 0;
2627 		hp->seq_id	= 0;
2628 		hp->seq_cnt	= 0;
2629 		hp->ox_id	= 0xffff;
2630 		hp->rx_id	= 0xffff;
2631 		hp->ro		= 0;
2632 
2633 		fcmd->fcp_cntl.cntl_qtype	= FCP_QTYPE_SIMPLE;
2634 		fcmd->fcp_cntl.cntl_read_data	= 1;	/* only rd for now */
2635 		fcmd->fcp_cntl.cntl_write_data	= 0;
2636 		fcmd->fcp_data_len	= fscsi->scsi_buflen;
2637 
2638 		scsi_cdb = (union scsi_cdb *)fcmd->fcp_cdb;
2639 		bcopy((char *)fscsi->scsi_cdbbufaddr, (char *)scsi_cdb,
2640 		    fscsi->scsi_cdblen);
2641 
2642 		if (!nodma) {
2643 			FCP_CP_OUT((uint8_t *)fcmd, fpkt->pkt_cmd,
2644 			    fpkt->pkt_cmd_acc, sizeof (struct fcp_cmd));
2645 		}
2646 
2647 		/*
2648 		 * Send SCSI command to FC transport
2649 		 */
2650 
2651 		if (ret == 0) {
2652 			mutex_enter(&ptgt->tgt_mutex);
2653 
2654 			if (!FCP_TGT_STATE_CHANGED(ptgt, icmd)) {
2655 				mutex_exit(&ptgt->tgt_mutex);
2656 				fscsi->scsi_fc_status = xport_retval =
2657 				    fc_ulp_transport(pptr->port_fp_handle,
2658 				    fpkt);
2659 				if (fscsi->scsi_fc_status != FC_SUCCESS) {
2660 					ret = EIO;
2661 				}
2662 			} else {
2663 				mutex_exit(&ptgt->tgt_mutex);
2664 				ret = EBUSY;
2665 			}
2666 		}
2667 	}
2668 
2669 	/*
2670 	 * Wait for completion only if fc_ulp_transport was called and it
2671 	 * returned a success. This is the only time callback will happen.
2672 	 * Otherwise, there is no point in waiting
2673 	 */
2674 	if ((ret == 0) && (xport_retval == FC_SUCCESS)) {
2675 		ret = fcp_ipkt_sema_wait(icmd);
2676 	}
2677 
2678 	/*
2679 	 * Copy data to IOCTL data structures
2680 	 */
2681 	rsp = NULL;
2682 	if ((ret == 0) && (xport_retval == FC_SUCCESS)) {
2683 		rsp = (struct fcp_rsp *)fpkt->pkt_resp;
2684 
2685 		if (fcp_validate_fcp_response(rsp, pptr) != FC_SUCCESS) {
2686 			fcp_log(CE_WARN, pptr->port_dip,
2687 			    "!SCSI command to d_id=0x%x lun=0x%x"
2688 			    " failed, Bad FCP response values:"
2689 			    " rsvd1=%x, rsvd2=%x, sts-rsvd1=%x,"
2690 			    " sts-rsvd2=%x, rsplen=%x, senselen=%x",
2691 			    ptgt->tgt_d_id, plun->lun_num,
2692 			    rsp->reserved_0, rsp->reserved_1,
2693 			    rsp->fcp_u.fcp_status.reserved_0,
2694 			    rsp->fcp_u.fcp_status.reserved_1,
2695 			    rsp->fcp_response_len, rsp->fcp_sense_len);
2696 
2697 			ret = EIO;
2698 		}
2699 	}
2700 
2701 	if ((ret == 0) && (rsp != NULL)) {
2702 		/*
2703 		 * Calc response lengths
2704 		 */
2705 		sense_len = 0;
2706 		info_len = 0;
2707 
2708 		if (rsp->fcp_u.fcp_status.rsp_len_set) {
2709 			info_len = rsp->fcp_response_len;
2710 		}
2711 
2712 		rsp_info   = (struct fcp_rsp_info *)
2713 		    ((uint8_t *)rsp + sizeof (struct fcp_rsp));
2714 
2715 		/*
2716 		 * Get SCSI status
2717 		 */
2718 		fscsi->scsi_bufstatus = rsp->fcp_u.fcp_status.scsi_status;
2719 		/*
2720 		 * If a lun was just added or removed and the next command
2721 		 * comes through this interface, we need to capture the check
2722 		 * condition so we can discover the new topology.
2723 		 */
2724 		if (fscsi->scsi_bufstatus != STATUS_GOOD &&
2725 		    rsp->fcp_u.fcp_status.sense_len_set) {
2726 			sense_len = rsp->fcp_sense_len;
2727 			rsp_sense  = (caddr_t)((uint8_t *)rsp_info + info_len);
2728 			sense_to = (struct scsi_extended_sense *)rsp_sense;
2729 			if ((FCP_SENSE_REPORTLUN_CHANGED(sense_to)) ||
2730 			    (FCP_SENSE_NO_LUN(sense_to))) {
2731 				reconfig_lun = TRUE;
2732 			}
2733 		}
2734 
2735 		if (fscsi->scsi_bufstatus == STATUS_GOOD && (ptgt != NULL) &&
2736 		    (reconfig_lun || (scsi_cdb->scc_cmd == SCMD_REPORT_LUN))) {
2737 			if (reconfig_lun == FALSE) {
2738 				reconfig_status =
2739 				    fcp_is_reconfig_needed(ptgt, fpkt);
2740 			}
2741 
2742 			if ((reconfig_lun == TRUE) ||
2743 			    (reconfig_status == TRUE)) {
2744 				mutex_enter(&ptgt->tgt_mutex);
2745 				if (ptgt->tgt_tid == NULL) {
2746 					/*
2747 					 * Either we've been notified the
2748 					 * REPORT_LUN data has changed, or
2749 					 * we've determined on our own that
2750 					 * we're out of date.  Kick off
2751 					 * rediscovery.
2752 					 */
2753 					tid = timeout(fcp_reconfigure_luns,
2754 					    (caddr_t)ptgt, drv_usectohz(1));
2755 
2756 					ptgt->tgt_tid = tid;
2757 					ptgt->tgt_state |= FCP_TGT_BUSY;
2758 					ret = EBUSY;
2759 					reconfig_pending = TRUE;
2760 				}
2761 				mutex_exit(&ptgt->tgt_mutex);
2762 			}
2763 		}
2764 
2765 		/*
2766 		 * Calc residuals and buffer lengths
2767 		 */
2768 
2769 		if (ret == 0) {
2770 			buf_len = fscsi->scsi_buflen;
2771 			fscsi->scsi_bufresid	= 0;
2772 			if (rsp->fcp_u.fcp_status.resid_under) {
2773 				if (rsp->fcp_resid <= fscsi->scsi_buflen) {
2774 					fscsi->scsi_bufresid = rsp->fcp_resid;
2775 				} else {
2776 					cmn_err(CE_WARN, "fcp: bad residue %x "
2777 					    "for txfer len %x", rsp->fcp_resid,
2778 					    fscsi->scsi_buflen);
2779 					fscsi->scsi_bufresid =
2780 					    fscsi->scsi_buflen;
2781 				}
2782 				buf_len -= fscsi->scsi_bufresid;
2783 			}
2784 			if (rsp->fcp_u.fcp_status.resid_over) {
2785 				fscsi->scsi_bufresid = -rsp->fcp_resid;
2786 			}
2787 
2788 			fscsi->scsi_rqresid	= fscsi->scsi_rqlen - sense_len;
2789 			if (fscsi->scsi_rqlen < sense_len) {
2790 				sense_len = fscsi->scsi_rqlen;
2791 			}
2792 
2793 			fscsi->scsi_fc_rspcode	= 0;
2794 			if (rsp->fcp_u.fcp_status.rsp_len_set) {
2795 				fscsi->scsi_fc_rspcode	= rsp_info->rsp_code;
2796 			}
2797 			fscsi->scsi_pkt_state	= fpkt->pkt_state;
2798 			fscsi->scsi_pkt_action	= fpkt->pkt_action;
2799 			fscsi->scsi_pkt_reason	= fpkt->pkt_reason;
2800 
2801 			/*
2802 			 * Copy data and request sense
2803 			 *
2804 			 * Data must be copied by using the FCP_CP_IN macro.
2805 			 * This will ensure the proper byte order since the data
2806 			 * is being copied directly from the memory mapped
2807 			 * device register.
2808 			 *
2809 			 * The response (and request sense) will be in the
2810 			 * correct byte order.	No special copy is necessary.
2811 			 */
2812 
2813 			if (buf_len) {
2814 				FCP_CP_IN(fpkt->pkt_data,
2815 				    fscsi->scsi_bufaddr,
2816 				    fpkt->pkt_data_acc,
2817 				    buf_len);
2818 			}
2819 			bcopy((void *)rsp_sense,
2820 			    (void *)fscsi->scsi_rqbufaddr,
2821 			    sense_len);
2822 		}
2823 	}
2824 
2825 	/*
2826 	 * Cleanup transport data structures if icmd was alloc-ed
2827 	 * So, cleanup happens in the same thread that icmd was alloc-ed
2828 	 */
2829 	if (icmd != NULL) {
2830 		fcp_ipkt_sema_cleanup(icmd);
2831 	}
2832 
2833 	/* restore pm busy/idle status */
2834 	if (port_busy) {
2835 		fc_ulp_idle_port(pptr->port_fp_handle);
2836 	}
2837 
2838 	/*
2839 	 * Cleanup target.  if a reconfig is pending, don't clear the BUSY
2840 	 * flag, it'll be cleared when the reconfig is complete.
2841 	 */
2842 	if ((ptgt != NULL) && !reconfig_pending) {
2843 		/*
2844 		 * If target was created,
2845 		 */
2846 		if (target_created) {
2847 			mutex_enter(&ptgt->tgt_mutex);
2848 			ptgt->tgt_state &= ~FCP_TGT_BUSY;
2849 			mutex_exit(&ptgt->tgt_mutex);
2850 		} else {
2851 			/*
2852 			 * De-mark target as busy
2853 			 */
2854 			mutex_enter(&ptgt->tgt_mutex);
2855 			ptgt->tgt_state &= ~FCP_TGT_BUSY;
2856 			mutex_exit(&ptgt->tgt_mutex);
2857 		}
2858 	}
2859 	return (ret);
2860 }
2861 
2862 
2863 static int
2864 fcp_is_reconfig_needed(struct fcp_tgt *ptgt,
2865     fc_packet_t	*fpkt)
2866 {
2867 	uchar_t			*lun_string;
2868 	uint16_t		lun_num, i;
2869 	int			num_luns;
2870 	int			actual_luns;
2871 	int			num_masked_luns;
2872 	int			lun_buflen;
2873 	struct fcp_lun	*plun	= NULL;
2874 	struct fcp_reportlun_resp	*report_lun;
2875 	uint8_t			reconfig_needed = FALSE;
2876 	uint8_t			lun_exists = FALSE;
2877 	fcp_port_t			*pptr		 = ptgt->tgt_port;
2878 
2879 	report_lun = kmem_zalloc(fpkt->pkt_datalen, KM_SLEEP);
2880 
2881 	FCP_CP_IN(fpkt->pkt_data, report_lun, fpkt->pkt_data_acc,
2882 	    fpkt->pkt_datalen);
2883 
2884 	/* get number of luns (which is supplied as LUNS * 8) */
2885 	num_luns = BE_32(report_lun->num_lun) >> 3;
2886 
2887 	/*
2888 	 * Figure out exactly how many lun strings our response buffer
2889 	 * can hold.
2890 	 */
2891 	lun_buflen = (fpkt->pkt_datalen -
2892 	    2 * sizeof (uint32_t)) / sizeof (longlong_t);
2893 
2894 	/*
2895 	 * Is our response buffer full or not? We don't want to
2896 	 * potentially walk beyond the number of luns we have.
2897 	 */
2898 	if (num_luns <= lun_buflen) {
2899 		actual_luns = num_luns;
2900 	} else {
2901 		actual_luns = lun_buflen;
2902 	}
2903 
2904 	mutex_enter(&ptgt->tgt_mutex);
2905 
2906 	/* Scan each lun to see if we have masked it. */
2907 	num_masked_luns = 0;
2908 	if (fcp_lun_blacklist != NULL) {
2909 		for (i = 0; i < actual_luns; i++) {
2910 			lun_string = (uchar_t *)&(report_lun->lun_string[i]);
2911 			switch (lun_string[0] & 0xC0) {
2912 			case FCP_LUN_ADDRESSING:
2913 			case FCP_PD_ADDRESSING:
2914 			case FCP_VOLUME_ADDRESSING:
2915 				lun_num = ((lun_string[0] & 0x3F) << 8)
2916 				    | lun_string[1];
2917 				if (fcp_should_mask(&ptgt->tgt_port_wwn,
2918 				    lun_num) == TRUE) {
2919 					num_masked_luns++;
2920 				}
2921 				break;
2922 			default:
2923 				break;
2924 			}
2925 		}
2926 	}
2927 
2928 	/*
2929 	 * The quick and easy check.  If the number of LUNs reported
2930 	 * doesn't match the number we currently know about, we need
2931 	 * to reconfigure.
2932 	 */
2933 	if (num_luns && num_luns != (ptgt->tgt_lun_cnt + num_masked_luns)) {
2934 		mutex_exit(&ptgt->tgt_mutex);
2935 		kmem_free(report_lun, fpkt->pkt_datalen);
2936 		return (TRUE);
2937 	}
2938 
2939 	/*
2940 	 * If the quick and easy check doesn't turn up anything, we walk
2941 	 * the list of luns from the REPORT_LUN response and look for
2942 	 * any luns we don't know about.  If we find one, we know we need
2943 	 * to reconfigure. We will skip LUNs that are masked because of the
2944 	 * blacklist.
2945 	 */
2946 	for (i = 0; i < actual_luns; i++) {
2947 		lun_string = (uchar_t *)&(report_lun->lun_string[i]);
2948 		lun_exists = FALSE;
2949 		switch (lun_string[0] & 0xC0) {
2950 		case FCP_LUN_ADDRESSING:
2951 		case FCP_PD_ADDRESSING:
2952 		case FCP_VOLUME_ADDRESSING:
2953 			lun_num = ((lun_string[0] & 0x3F) << 8) | lun_string[1];
2954 
2955 			if ((fcp_lun_blacklist != NULL) && (fcp_should_mask(
2956 			    &ptgt->tgt_port_wwn, lun_num) == TRUE)) {
2957 				lun_exists = TRUE;
2958 				break;
2959 			}
2960 
2961 			for (plun = ptgt->tgt_lun; plun;
2962 			    plun = plun->lun_next) {
2963 				if (plun->lun_num == lun_num) {
2964 					lun_exists = TRUE;
2965 					break;
2966 				}
2967 			}
2968 			break;
2969 		default:
2970 			break;
2971 		}
2972 
2973 		if (lun_exists == FALSE) {
2974 			reconfig_needed = TRUE;
2975 			break;
2976 		}
2977 	}
2978 
2979 	mutex_exit(&ptgt->tgt_mutex);
2980 	kmem_free(report_lun, fpkt->pkt_datalen);
2981 
2982 	return (reconfig_needed);
2983 }
2984 
2985 /*
2986  * This function is called by fcp_handle_page83 and uses inquiry response data
2987  * stored in plun->lun_inq to determine whether or not a device is a member of
2988  * the table fcp_symmetric_disk_table_size. We return 0 if it is in the table,
2989  * otherwise 1.
2990  */
2991 static int
2992 fcp_symmetric_device_probe(struct fcp_lun *plun)
2993 {
2994 	struct scsi_inquiry	*stdinq = &plun->lun_inq;
2995 	char			*devidptr;
2996 	int			i, len;
2997 
2998 	for (i = 0; i < fcp_symmetric_disk_table_size; i++) {
2999 		devidptr = fcp_symmetric_disk_table[i];
3000 		len = (int)strlen(devidptr);
3001 
3002 		if (bcmp(stdinq->inq_vid, devidptr, len) == 0) {
3003 			return (0);
3004 		}
3005 	}
3006 	return (1);
3007 }
3008 
3009 
3010 /*
3011  * This function is called by fcp_ioctl for the FCP_STATE_COUNT ioctl
3012  * It basically returns the current count of # of state change callbacks
3013  * i.e the value of tgt_change_cnt.
3014  *
3015  * INPUT:
3016  *   fcp_ioctl.fp_minor -> The minor # of the fp port
3017  *   fcp_ioctl.listlen	-> 1
3018  *   fcp_ioctl.list	-> Pointer to a 32 bit integer
3019  */
3020 /*ARGSUSED2*/
3021 static int
3022 fcp_get_statec_count(struct fcp_ioctl *data, int mode, int *rval)
3023 {
3024 	int			ret;
3025 	uint32_t		link_cnt;
3026 	struct fcp_ioctl	fioctl;
3027 	struct fcp_port	*pptr = NULL;
3028 
3029 	if ((ret = fcp_copyin_fcp_ioctl_data(data, mode, rval, &fioctl,
3030 	    &pptr)) != 0) {
3031 		return (ret);
3032 	}
3033 
3034 	ASSERT(pptr != NULL);
3035 
3036 	if (fioctl.listlen != 1) {
3037 		return (EINVAL);
3038 	}
3039 
3040 	mutex_enter(&pptr->port_mutex);
3041 	if (pptr->port_state & FCP_STATE_OFFLINE) {
3042 		mutex_exit(&pptr->port_mutex);
3043 		return (ENXIO);
3044 	}
3045 
3046 	/*
3047 	 * FCP_STATE_INIT is set in 2 cases (not sure why it is overloaded):
3048 	 * When the fcp initially attaches to the port and there are nothing
3049 	 * hanging out of the port or if there was a repeat offline state change
3050 	 * callback (refer fcp_statec_callback() FC_STATE_OFFLINE case).
3051 	 * In the latter case, port_tmp_cnt will be non-zero and that is how we
3052 	 * will differentiate the 2 cases.
3053 	 */
3054 	if ((pptr->port_state & FCP_STATE_INIT) && pptr->port_tmp_cnt) {
3055 		mutex_exit(&pptr->port_mutex);
3056 		return (ENXIO);
3057 	}
3058 
3059 	link_cnt = pptr->port_link_cnt;
3060 	mutex_exit(&pptr->port_mutex);
3061 
3062 	if (ddi_copyout(&link_cnt, fioctl.list, (sizeof (uint32_t)), mode)) {
3063 		return (EFAULT);
3064 	}
3065 
3066 #ifdef	_MULTI_DATAMODEL
3067 	switch (ddi_model_convert_from(mode & FMODELS)) {
3068 	case DDI_MODEL_ILP32: {
3069 		struct fcp32_ioctl f32_ioctl;
3070 
3071 		f32_ioctl.fp_minor = fioctl.fp_minor;
3072 		f32_ioctl.listlen = fioctl.listlen;
3073 		f32_ioctl.list = (caddr32_t)(long)fioctl.list;
3074 		if (ddi_copyout((void *)&f32_ioctl, (void *)data,
3075 		    sizeof (struct fcp32_ioctl), mode)) {
3076 			return (EFAULT);
3077 		}
3078 		break;
3079 	}
3080 	case DDI_MODEL_NONE:
3081 		if (ddi_copyout((void *)&fioctl, (void *)data,
3082 		    sizeof (struct fcp_ioctl), mode)) {
3083 			return (EFAULT);
3084 		}
3085 		break;
3086 	}
3087 #else	/* _MULTI_DATAMODEL */
3088 
3089 	if (ddi_copyout((void *)&fioctl, (void *)data,
3090 	    sizeof (struct fcp_ioctl), mode)) {
3091 		return (EFAULT);
3092 	}
3093 #endif	/* _MULTI_DATAMODEL */
3094 
3095 	return (0);
3096 }
3097 
3098 /*
3099  * This function copies the fcp_ioctl structure passed in from user land
3100  * into kernel land. Handles 32 bit applications.
3101  */
3102 /*ARGSUSED*/
3103 static int
3104 fcp_copyin_fcp_ioctl_data(struct fcp_ioctl *data, int mode, int *rval,
3105     struct fcp_ioctl *fioctl, struct fcp_port **pptr)
3106 {
3107 	struct fcp_port	*t_pptr;
3108 
3109 #ifdef	_MULTI_DATAMODEL
3110 	switch (ddi_model_convert_from(mode & FMODELS)) {
3111 	case DDI_MODEL_ILP32: {
3112 		struct fcp32_ioctl f32_ioctl;
3113 
3114 		if (ddi_copyin((void *)data, (void *)&f32_ioctl,
3115 		    sizeof (struct fcp32_ioctl), mode)) {
3116 			return (EFAULT);
3117 		}
3118 		fioctl->fp_minor = f32_ioctl.fp_minor;
3119 		fioctl->listlen = f32_ioctl.listlen;
3120 		fioctl->list = (caddr_t)(long)f32_ioctl.list;
3121 		break;
3122 	}
3123 	case DDI_MODEL_NONE:
3124 		if (ddi_copyin((void *)data, (void *)fioctl,
3125 		    sizeof (struct fcp_ioctl), mode)) {
3126 			return (EFAULT);
3127 		}
3128 		break;
3129 	}
3130 
3131 #else	/* _MULTI_DATAMODEL */
3132 	if (ddi_copyin((void *)data, (void *)fioctl,
3133 	    sizeof (struct fcp_ioctl), mode)) {
3134 		return (EFAULT);
3135 	}
3136 #endif	/* _MULTI_DATAMODEL */
3137 
3138 	/*
3139 	 * Right now we can assume that the minor number matches with
3140 	 * this instance of fp. If this changes we will need to
3141 	 * revisit this logic.
3142 	 */
3143 	mutex_enter(&fcp_global_mutex);
3144 	t_pptr = fcp_port_head;
3145 	while (t_pptr) {
3146 		if (t_pptr->port_instance == (uint32_t)fioctl->fp_minor) {
3147 			break;
3148 		} else {
3149 			t_pptr = t_pptr->port_next;
3150 		}
3151 	}
3152 	*pptr = t_pptr;
3153 	mutex_exit(&fcp_global_mutex);
3154 	if (t_pptr == NULL) {
3155 		return (ENXIO);
3156 	}
3157 
3158 	return (0);
3159 }
3160 
3161 /*
3162  *     Function: fcp_port_create_tgt
3163  *
3164  *  Description: As the name suggest this function creates the target context
3165  *		 specified by the the WWN provided by the caller.  If the
3166  *		 creation goes well and the target is known by fp/fctl a PLOGI
3167  *		 followed by a PRLI are issued.
3168  *
3169  *     Argument: pptr		fcp port structure
3170  *		 pwwn		WWN of the target
3171  *		 ret_val	Address of the return code.  It could be:
3172  *				EIO, ENOMEM or 0.
3173  *		 fc_status	PLOGI or PRLI status completion
3174  *		 fc_pkt_state	PLOGI or PRLI state completion
3175  *		 fc_pkt_reason	PLOGI or PRLI reason completion
3176  *		 fc_pkt_action	PLOGI or PRLI action completion
3177  *
3178  * Return Value: NULL if it failed
3179  *		 Target structure address if it succeeds
3180  */
3181 static struct fcp_tgt *
3182 fcp_port_create_tgt(struct fcp_port *pptr, la_wwn_t *pwwn, int *ret_val,
3183     int *fc_status, int *fc_pkt_state, int *fc_pkt_reason, int *fc_pkt_action)
3184 {
3185 	struct fcp_tgt	*ptgt = NULL;
3186 	fc_portmap_t		devlist;
3187 	int			lcount;
3188 	int			error;
3189 
3190 	*ret_val = 0;
3191 
3192 	/*
3193 	 * Check FC port device & get port map
3194 	 */
3195 	if (fc_ulp_get_remote_port(pptr->port_fp_handle, pwwn,
3196 	    &error, 1) == NULL) {
3197 		*ret_val = EIO;
3198 	} else {
3199 		if (fc_ulp_pwwn_to_portmap(pptr->port_fp_handle, pwwn,
3200 		    &devlist) != FC_SUCCESS) {
3201 			*ret_val = EIO;
3202 		}
3203 	}
3204 
3205 	/* Set port map flags */
3206 	devlist.map_type = PORT_DEVICE_USER_CREATE;
3207 
3208 	/* Allocate target */
3209 	if (*ret_val == 0) {
3210 		lcount = pptr->port_link_cnt;
3211 		ptgt = fcp_alloc_tgt(pptr, &devlist, lcount);
3212 		if (ptgt == NULL) {
3213 			fcp_log(CE_WARN, pptr->port_dip,
3214 			    "!FC target allocation failed");
3215 			*ret_val = ENOMEM;
3216 		} else {
3217 			/* Setup target */
3218 			mutex_enter(&ptgt->tgt_mutex);
3219 
3220 			ptgt->tgt_statec_cause	= FCP_CAUSE_TGT_CHANGE;
3221 			ptgt->tgt_tmp_cnt	= 1;
3222 			ptgt->tgt_d_id		= devlist.map_did.port_id;
3223 			ptgt->tgt_hard_addr	=
3224 			    devlist.map_hard_addr.hard_addr;
3225 			ptgt->tgt_pd_handle	= devlist.map_pd;
3226 			ptgt->tgt_fca_dev	= NULL;
3227 
3228 			bcopy(&devlist.map_nwwn, &ptgt->tgt_node_wwn.raw_wwn[0],
3229 			    FC_WWN_SIZE);
3230 			bcopy(&devlist.map_pwwn, &ptgt->tgt_port_wwn.raw_wwn[0],
3231 			    FC_WWN_SIZE);
3232 
3233 			mutex_exit(&ptgt->tgt_mutex);
3234 		}
3235 	}
3236 
3237 	/* Release global mutex for PLOGI and PRLI */
3238 	mutex_exit(&fcp_global_mutex);
3239 
3240 	/* Send PLOGI (If necessary) */
3241 	if (*ret_val == 0) {
3242 		*ret_val = fcp_tgt_send_plogi(ptgt, fc_status,
3243 		    fc_pkt_state, fc_pkt_reason, fc_pkt_action);
3244 	}
3245 
3246 	/* Send PRLI (If necessary) */
3247 	if (*ret_val == 0) {
3248 		*ret_val = fcp_tgt_send_prli(ptgt, fc_status,
3249 		    fc_pkt_state, fc_pkt_reason, fc_pkt_action);
3250 	}
3251 
3252 	mutex_enter(&fcp_global_mutex);
3253 
3254 	return (ptgt);
3255 }
3256 
3257 /*
3258  *     Function: fcp_tgt_send_plogi
3259  *
3260  *  Description: This function sends a PLOGI to the target specified by the
3261  *		 caller and waits till it completes.
3262  *
3263  *     Argument: ptgt		Target to send the plogi to.
3264  *		 fc_status	Status returned by fp/fctl in the PLOGI request.
3265  *		 fc_pkt_state	State returned by fp/fctl in the PLOGI request.
3266  *		 fc_pkt_reason	Reason returned by fp/fctl in the PLOGI request.
3267  *		 fc_pkt_action	Action returned by fp/fctl in the PLOGI request.
3268  *
3269  * Return Value: 0
3270  *		 ENOMEM
3271  *		 EIO
3272  *
3273  *	Context: User context.
3274  */
3275 static int
3276 fcp_tgt_send_plogi(struct fcp_tgt *ptgt, int *fc_status, int *fc_pkt_state,
3277     int *fc_pkt_reason, int *fc_pkt_action)
3278 {
3279 	struct fcp_port	*pptr;
3280 	struct fcp_ipkt	*icmd;
3281 	struct fc_packet	*fpkt;
3282 	fc_frame_hdr_t		*hp;
3283 	struct la_els_logi	logi;
3284 	int			tcount;
3285 	int			lcount;
3286 	int			ret, login_retval = ~FC_SUCCESS;
3287 
3288 	ret = 0;
3289 
3290 	pptr = ptgt->tgt_port;
3291 
3292 	lcount = pptr->port_link_cnt;
3293 	tcount = ptgt->tgt_change_cnt;
3294 
3295 	/* Alloc internal packet */
3296 	icmd = fcp_icmd_alloc(pptr, ptgt, sizeof (la_els_logi_t),
3297 	    sizeof (la_els_logi_t), 0,
3298 	    pptr->port_state & FCP_STATE_FCA_IS_NODMA,
3299 	    lcount, tcount, 0, FC_INVALID_RSCN_COUNT);
3300 
3301 	if (icmd == NULL) {
3302 		ret = ENOMEM;
3303 	} else {
3304 		/*
3305 		 * Setup internal packet as sema sync
3306 		 */
3307 		fcp_ipkt_sema_init(icmd);
3308 
3309 		/*
3310 		 * Setup internal packet (icmd)
3311 		 */
3312 		icmd->ipkt_lun		= NULL;
3313 		icmd->ipkt_restart	= 0;
3314 		icmd->ipkt_retries	= 0;
3315 		icmd->ipkt_opcode	= LA_ELS_PLOGI;
3316 
3317 		/*
3318 		 * Setup fc_packet
3319 		 */
3320 		fpkt = icmd->ipkt_fpkt;
3321 
3322 		fpkt->pkt_tran_flags	= FC_TRAN_CLASS3 | FC_TRAN_INTR;
3323 		fpkt->pkt_tran_type	= FC_PKT_EXCHANGE;
3324 		fpkt->pkt_timeout	= FCP_ELS_TIMEOUT;
3325 
3326 		/*
3327 		 * Setup FC frame header
3328 		 */
3329 		hp = &fpkt->pkt_cmd_fhdr;
3330 
3331 		hp->s_id	= pptr->port_id;	/* source ID */
3332 		hp->d_id	= ptgt->tgt_d_id;	/* dest ID */
3333 		hp->r_ctl	= R_CTL_ELS_REQ;
3334 		hp->type	= FC_TYPE_EXTENDED_LS;
3335 		hp->f_ctl	= F_CTL_SEQ_INITIATIVE | F_CTL_FIRST_SEQ;
3336 		hp->seq_id	= 0;
3337 		hp->rsvd	= 0;
3338 		hp->df_ctl	= 0;
3339 		hp->seq_cnt	= 0;
3340 		hp->ox_id	= 0xffff;		/* i.e. none */
3341 		hp->rx_id	= 0xffff;		/* i.e. none */
3342 		hp->ro		= 0;
3343 
3344 		/*
3345 		 * Setup PLOGI
3346 		 */
3347 		bzero(&logi, sizeof (struct la_els_logi));
3348 		logi.ls_code.ls_code = LA_ELS_PLOGI;
3349 
3350 		FCP_CP_OUT((uint8_t *)&logi, fpkt->pkt_cmd,
3351 		    fpkt->pkt_cmd_acc, sizeof (struct la_els_logi));
3352 
3353 		/*
3354 		 * Send PLOGI
3355 		 */
3356 		*fc_status = login_retval =
3357 		    fc_ulp_login(pptr->port_fp_handle, &fpkt, 1);
3358 		if (*fc_status != FC_SUCCESS) {
3359 			ret = EIO;
3360 		}
3361 	}
3362 
3363 	/*
3364 	 * Wait for completion
3365 	 */
3366 	if ((ret == 0) && (login_retval == FC_SUCCESS)) {
3367 		ret = fcp_ipkt_sema_wait(icmd);
3368 
3369 		*fc_pkt_state	= fpkt->pkt_state;
3370 		*fc_pkt_reason	= fpkt->pkt_reason;
3371 		*fc_pkt_action	= fpkt->pkt_action;
3372 	}
3373 
3374 	/*
3375 	 * Cleanup transport data structures if icmd was alloc-ed AND if there
3376 	 * is going to be no callback (i.e if fc_ulp_login() failed).
3377 	 * Otherwise, cleanup happens in callback routine.
3378 	 */
3379 	if (icmd != NULL) {
3380 		fcp_ipkt_sema_cleanup(icmd);
3381 	}
3382 
3383 	return (ret);
3384 }
3385 
3386 /*
3387  *     Function: fcp_tgt_send_prli
3388  *
3389  *  Description: Does nothing as of today.
3390  *
3391  *     Argument: ptgt		Target to send the prli to.
3392  *		 fc_status	Status returned by fp/fctl in the PRLI request.
3393  *		 fc_pkt_state	State returned by fp/fctl in the PRLI request.
3394  *		 fc_pkt_reason	Reason returned by fp/fctl in the PRLI request.
3395  *		 fc_pkt_action	Action returned by fp/fctl in the PRLI request.
3396  *
3397  * Return Value: 0
3398  */
3399 /*ARGSUSED*/
3400 static int
3401 fcp_tgt_send_prli(struct fcp_tgt *ptgt, int *fc_status, int *fc_pkt_state,
3402     int *fc_pkt_reason, int *fc_pkt_action)
3403 {
3404 	return (0);
3405 }
3406 
3407 /*
3408  *     Function: fcp_ipkt_sema_init
3409  *
3410  *  Description: Initializes the semaphore contained in the internal packet.
3411  *
3412  *     Argument: icmd	Internal packet the semaphore of which must be
3413  *			initialized.
3414  *
3415  * Return Value: None
3416  *
3417  *	Context: User context only.
3418  */
3419 static void
3420 fcp_ipkt_sema_init(struct fcp_ipkt *icmd)
3421 {
3422 	struct fc_packet	*fpkt;
3423 
3424 	fpkt = icmd->ipkt_fpkt;
3425 
3426 	/* Create semaphore for sync */
3427 	sema_init(&(icmd->ipkt_sema), 0, NULL, SEMA_DRIVER, NULL);
3428 
3429 	/* Setup the completion callback */
3430 	fpkt->pkt_comp = fcp_ipkt_sema_callback;
3431 }
3432 
3433 /*
3434  *     Function: fcp_ipkt_sema_wait
3435  *
3436  *  Description: Wait on the semaphore embedded in the internal packet.	 The
3437  *		 semaphore is released in the callback.
3438  *
3439  *     Argument: icmd	Internal packet to wait on for completion.
3440  *
3441  * Return Value: 0
3442  *		 EIO
3443  *		 EBUSY
3444  *		 EAGAIN
3445  *
3446  *	Context: User context only.
3447  *
3448  * This function does a conversion between the field pkt_state of the fc_packet
3449  * embedded in the internal packet (icmd) and the code it returns.
3450  */
3451 static int
3452 fcp_ipkt_sema_wait(struct fcp_ipkt *icmd)
3453 {
3454 	struct fc_packet	*fpkt;
3455 	int	ret;
3456 
3457 	ret = EIO;
3458 	fpkt = icmd->ipkt_fpkt;
3459 
3460 	/*
3461 	 * Wait on semaphore
3462 	 */
3463 	sema_p(&(icmd->ipkt_sema));
3464 
3465 	/*
3466 	 * Check the status of the FC packet
3467 	 */
3468 	switch (fpkt->pkt_state) {
3469 	case FC_PKT_SUCCESS:
3470 		ret = 0;
3471 		break;
3472 	case FC_PKT_LOCAL_RJT:
3473 		switch (fpkt->pkt_reason) {
3474 		case FC_REASON_SEQ_TIMEOUT:
3475 		case FC_REASON_RX_BUF_TIMEOUT:
3476 			ret = EAGAIN;
3477 			break;
3478 		case FC_REASON_PKT_BUSY:
3479 			ret = EBUSY;
3480 			break;
3481 		}
3482 		break;
3483 	case FC_PKT_TIMEOUT:
3484 		ret = EAGAIN;
3485 		break;
3486 	case FC_PKT_LOCAL_BSY:
3487 	case FC_PKT_TRAN_BSY:
3488 	case FC_PKT_NPORT_BSY:
3489 	case FC_PKT_FABRIC_BSY:
3490 		ret = EBUSY;
3491 		break;
3492 	case FC_PKT_LS_RJT:
3493 	case FC_PKT_BA_RJT:
3494 		switch (fpkt->pkt_reason) {
3495 		case FC_REASON_LOGICAL_BSY:
3496 			ret = EBUSY;
3497 			break;
3498 		}
3499 		break;
3500 	case FC_PKT_FS_RJT:
3501 		switch (fpkt->pkt_reason) {
3502 		case FC_REASON_FS_LOGICAL_BUSY:
3503 			ret = EBUSY;
3504 			break;
3505 		}
3506 		break;
3507 	}
3508 
3509 	return (ret);
3510 }
3511 
3512 /*
3513  *     Function: fcp_ipkt_sema_callback
3514  *
3515  *  Description: Registered as the completion callback function for the FC
3516  *		 transport when the ipkt semaphore is used for sync. This will
3517  *		 cleanup the used data structures, if necessary and wake up
3518  *		 the user thread to complete the transaction.
3519  *
3520  *     Argument: fpkt	FC packet (points to the icmd)
3521  *
3522  * Return Value: None
3523  *
3524  *	Context: User context only
3525  */
3526 static void
3527 fcp_ipkt_sema_callback(struct fc_packet *fpkt)
3528 {
3529 	struct fcp_ipkt	*icmd;
3530 
3531 	icmd = (struct fcp_ipkt *)fpkt->pkt_ulp_private;
3532 
3533 	/*
3534 	 * Wake up user thread
3535 	 */
3536 	sema_v(&(icmd->ipkt_sema));
3537 }
3538 
3539 /*
3540  *     Function: fcp_ipkt_sema_cleanup
3541  *
3542  *  Description: Called to cleanup (if necessary) the data structures used
3543  *		 when ipkt sema is used for sync.  This function will detect
3544  *		 whether the caller is the last thread (via counter) and
3545  *		 cleanup only if necessary.
3546  *
3547  *     Argument: icmd	Internal command packet
3548  *
3549  * Return Value: None
3550  *
3551  *	Context: User context only
3552  */
3553 static void
3554 fcp_ipkt_sema_cleanup(struct fcp_ipkt *icmd)
3555 {
3556 	struct fcp_tgt	*ptgt;
3557 	struct fcp_port	*pptr;
3558 
3559 	ptgt = icmd->ipkt_tgt;
3560 	pptr = icmd->ipkt_port;
3561 
3562 	/*
3563 	 * Acquire data structure
3564 	 */
3565 	mutex_enter(&ptgt->tgt_mutex);
3566 
3567 	/*
3568 	 * Destroy semaphore
3569 	 */
3570 	sema_destroy(&(icmd->ipkt_sema));
3571 
3572 	/*
3573 	 * Cleanup internal packet
3574 	 */
3575 	mutex_exit(&ptgt->tgt_mutex);
3576 	fcp_icmd_free(pptr, icmd);
3577 }
3578 
3579 /*
3580  *     Function: fcp_port_attach
3581  *
3582  *  Description: Called by the transport framework to resume, suspend or
3583  *		 attach a new port.
3584  *
3585  *     Argument: ulph		Port handle
3586  *		 *pinfo		Port information
3587  *		 cmd		Command
3588  *		 s_id		Port ID
3589  *
3590  * Return Value: FC_FAILURE or FC_SUCCESS
3591  */
3592 /*ARGSUSED*/
3593 static int
3594 fcp_port_attach(opaque_t ulph, fc_ulp_port_info_t *pinfo,
3595     fc_attach_cmd_t cmd, uint32_t s_id)
3596 {
3597 	int	instance;
3598 	int	res = FC_FAILURE; /* default result */
3599 
3600 	ASSERT(pinfo != NULL);
3601 
3602 	instance = ddi_get_instance(pinfo->port_dip);
3603 
3604 	switch (cmd) {
3605 	case FC_CMD_ATTACH:
3606 		/*
3607 		 * this port instance attaching for the first time (or after
3608 		 * being detached before)
3609 		 */
3610 		if (fcp_handle_port_attach(ulph, pinfo, s_id,
3611 		    instance) == DDI_SUCCESS) {
3612 			res = FC_SUCCESS;
3613 		} else {
3614 			ASSERT(ddi_get_soft_state(fcp_softstate,
3615 			    instance) == NULL);
3616 		}
3617 		break;
3618 
3619 	case FC_CMD_RESUME:
3620 	case FC_CMD_POWER_UP:
3621 		/*
3622 		 * this port instance was attached and the suspended and
3623 		 * will now be resumed
3624 		 */
3625 		if (fcp_handle_port_resume(ulph, pinfo, s_id, cmd,
3626 		    instance) == DDI_SUCCESS) {
3627 			res = FC_SUCCESS;
3628 		}
3629 		break;
3630 
3631 	default:
3632 		/* shouldn't happen */
3633 		FCP_TRACE(fcp_logq, "fcp",
3634 		    fcp_trace, FCP_BUF_LEVEL_2, 0,
3635 		    "port_attach: unknown cmdcommand: %d", cmd);
3636 		break;
3637 	}
3638 
3639 	/* return result */
3640 	FCP_DTRACE(fcp_logq, "fcp", fcp_trace,
3641 	    FCP_BUF_LEVEL_1, 0, "fcp_port_attach returning %d", res);
3642 
3643 	return (res);
3644 }
3645 
3646 
3647 /*
3648  * detach or suspend this port instance
3649  *
3650  * acquires and releases the global mutex
3651  *
3652  * acquires and releases the mutex for this port
3653  *
3654  * acquires and releases the hotplug mutex for this port
3655  */
3656 /*ARGSUSED*/
3657 static int
3658 fcp_port_detach(opaque_t ulph, fc_ulp_port_info_t *info,
3659     fc_detach_cmd_t cmd)
3660 {
3661 	int			flag;
3662 	int			instance;
3663 	struct fcp_port		*pptr;
3664 
3665 	instance = ddi_get_instance(info->port_dip);
3666 	pptr = ddi_get_soft_state(fcp_softstate, instance);
3667 
3668 	switch (cmd) {
3669 	case FC_CMD_SUSPEND:
3670 		FCP_DTRACE(fcp_logq, "fcp",
3671 		    fcp_trace, FCP_BUF_LEVEL_8, 0,
3672 		    "port suspend called for port %d", instance);
3673 		flag = FCP_STATE_SUSPENDED;
3674 		break;
3675 
3676 	case FC_CMD_POWER_DOWN:
3677 		FCP_DTRACE(fcp_logq, "fcp",
3678 		    fcp_trace, FCP_BUF_LEVEL_8, 0,
3679 		    "port power down called for port %d", instance);
3680 		flag = FCP_STATE_POWER_DOWN;
3681 		break;
3682 
3683 	case FC_CMD_DETACH:
3684 		FCP_DTRACE(fcp_logq, "fcp",
3685 		    fcp_trace, FCP_BUF_LEVEL_8, 0,
3686 		    "port detach called for port %d", instance);
3687 		flag = FCP_STATE_DETACHING;
3688 		break;
3689 
3690 	default:
3691 		/* shouldn't happen */
3692 		return (FC_FAILURE);
3693 	}
3694 	FCP_DTRACE(fcp_logq, "fcp", fcp_trace,
3695 	    FCP_BUF_LEVEL_1, 0, "fcp_port_detach returning");
3696 
3697 	return (fcp_handle_port_detach(pptr, flag, instance));
3698 }
3699 
3700 
3701 /*
3702  * called for ioctls on the transport's devctl interface, and the transport
3703  * has passed it to us
3704  *
3705  * this will only be called for device control ioctls (i.e. hotplugging stuff)
3706  *
3707  * return FC_SUCCESS if we decide to claim the ioctl,
3708  * else return FC_UNCLAIMED
3709  *
3710  * *rval is set iff we decide to claim the ioctl
3711  */
3712 /*ARGSUSED*/
3713 static int
3714 fcp_port_ioctl(opaque_t ulph, opaque_t port_handle, dev_t dev, int cmd,
3715     intptr_t data, int mode, cred_t *credp, int *rval, uint32_t claimed)
3716 {
3717 	int			retval = FC_UNCLAIMED;	/* return value */
3718 	struct fcp_port		*pptr = NULL;		/* our soft state */
3719 	struct devctl_iocdata	*dcp = NULL;		/* for devctl */
3720 	dev_info_t		*cdip;
3721 	mdi_pathinfo_t		*pip = NULL;
3722 	char			*ndi_nm;		/* NDI name */
3723 	char			*ndi_addr;		/* NDI addr */
3724 	int			is_mpxio, circ;
3725 	int			devi_entered = 0;
3726 	time_t			end_time;
3727 
3728 	ASSERT(rval != NULL);
3729 
3730 	FCP_DTRACE(fcp_logq, "fcp",
3731 	    fcp_trace, FCP_BUF_LEVEL_8, 0,
3732 	    "fcp_port_ioctl(cmd=0x%x, claimed=%d)", cmd, claimed);
3733 
3734 	/* if already claimed then forget it */
3735 	if (claimed) {
3736 		/*
3737 		 * for now, if this ioctl has already been claimed, then
3738 		 * we just ignore it
3739 		 */
3740 		return (retval);
3741 	}
3742 
3743 	/* get our port info */
3744 	if ((pptr = fcp_get_port(port_handle)) == NULL) {
3745 		fcp_log(CE_WARN, NULL,
3746 		    "!fcp:Invalid port handle handle in ioctl");
3747 		*rval = ENXIO;
3748 		return (retval);
3749 	}
3750 	is_mpxio = pptr->port_mpxio;
3751 
3752 	switch (cmd) {
3753 	case DEVCTL_BUS_GETSTATE:
3754 	case DEVCTL_BUS_QUIESCE:
3755 	case DEVCTL_BUS_UNQUIESCE:
3756 	case DEVCTL_BUS_RESET:
3757 	case DEVCTL_BUS_RESETALL:
3758 
3759 	case DEVCTL_BUS_DEV_CREATE:
3760 		if (ndi_dc_allochdl((void *)data, &dcp) != NDI_SUCCESS) {
3761 			return (retval);
3762 		}
3763 		break;
3764 
3765 	case DEVCTL_DEVICE_GETSTATE:
3766 	case DEVCTL_DEVICE_OFFLINE:
3767 	case DEVCTL_DEVICE_ONLINE:
3768 	case DEVCTL_DEVICE_REMOVE:
3769 	case DEVCTL_DEVICE_RESET:
3770 		if (ndi_dc_allochdl((void *)data, &dcp) != NDI_SUCCESS) {
3771 			return (retval);
3772 		}
3773 
3774 		ASSERT(dcp != NULL);
3775 
3776 		/* ensure we have a name and address */
3777 		if (((ndi_nm = ndi_dc_getname(dcp)) == NULL) ||
3778 		    ((ndi_addr = ndi_dc_getaddr(dcp)) == NULL)) {
3779 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
3780 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
3781 			    "ioctl: can't get name (%s) or addr (%s)",
3782 			    ndi_nm ? ndi_nm : "<null ptr>",
3783 			    ndi_addr ? ndi_addr : "<null ptr>");
3784 			ndi_dc_freehdl(dcp);
3785 			return (retval);
3786 		}
3787 
3788 
3789 		/* get our child's DIP */
3790 		ASSERT(pptr != NULL);
3791 		if (is_mpxio) {
3792 			mdi_devi_enter(pptr->port_dip, &circ);
3793 		} else {
3794 			ndi_devi_enter(pptr->port_dip, &circ);
3795 		}
3796 		devi_entered = 1;
3797 
3798 		if ((cdip = ndi_devi_find(pptr->port_dip, ndi_nm,
3799 		    ndi_addr)) == NULL) {
3800 			/* Look for virtually enumerated devices. */
3801 			pip = mdi_pi_find(pptr->port_dip, NULL, ndi_addr);
3802 			if (pip == NULL ||
3803 			    ((cdip = mdi_pi_get_client(pip)) == NULL)) {
3804 				*rval = ENXIO;
3805 				goto out;
3806 			}
3807 		}
3808 		break;
3809 
3810 	default:
3811 		*rval = ENOTTY;
3812 		return (retval);
3813 	}
3814 
3815 	/* this ioctl is ours -- process it */
3816 
3817 	retval = FC_SUCCESS;		/* just means we claim the ioctl */
3818 
3819 	/* we assume it will be a success; else we'll set error value */
3820 	*rval = 0;
3821 
3822 
3823 	FCP_DTRACE(fcp_logq, pptr->port_instbuf,
3824 	    fcp_trace, FCP_BUF_LEVEL_8, 0,
3825 	    "ioctl: claiming this one");
3826 
3827 	/* handle ioctls now */
3828 	switch (cmd) {
3829 	case DEVCTL_DEVICE_GETSTATE:
3830 		ASSERT(cdip != NULL);
3831 		ASSERT(dcp != NULL);
3832 		if (ndi_dc_return_dev_state(cdip, dcp) != NDI_SUCCESS) {
3833 			*rval = EFAULT;
3834 		}
3835 		break;
3836 
3837 	case DEVCTL_DEVICE_REMOVE:
3838 	case DEVCTL_DEVICE_OFFLINE: {
3839 		int			flag = 0;
3840 		int			lcount;
3841 		int			tcount;
3842 		struct fcp_pkt	*head = NULL;
3843 		struct fcp_lun	*plun;
3844 		child_info_t		*cip = CIP(cdip);
3845 		int			all = 1;
3846 		struct fcp_lun	*tplun;
3847 		struct fcp_tgt	*ptgt;
3848 
3849 		ASSERT(pptr != NULL);
3850 		ASSERT(cdip != NULL);
3851 
3852 		mutex_enter(&pptr->port_mutex);
3853 		if (pip != NULL) {
3854 			cip = CIP(pip);
3855 		}
3856 		if ((plun = fcp_get_lun_from_cip(pptr, cip)) == NULL) {
3857 			mutex_exit(&pptr->port_mutex);
3858 			*rval = ENXIO;
3859 			break;
3860 		}
3861 
3862 		head = fcp_scan_commands(plun);
3863 		if (head != NULL) {
3864 			fcp_abort_commands(head, LUN_PORT);
3865 		}
3866 		lcount = pptr->port_link_cnt;
3867 		tcount = plun->lun_tgt->tgt_change_cnt;
3868 		mutex_exit(&pptr->port_mutex);
3869 
3870 		if (cmd == DEVCTL_DEVICE_REMOVE) {
3871 			flag = NDI_DEVI_REMOVE;
3872 		}
3873 
3874 		if (is_mpxio) {
3875 			mdi_devi_exit(pptr->port_dip, circ);
3876 		} else {
3877 			ndi_devi_exit(pptr->port_dip, circ);
3878 		}
3879 		devi_entered = 0;
3880 
3881 		*rval = fcp_pass_to_hp_and_wait(pptr, plun, cip,
3882 		    FCP_OFFLINE, lcount, tcount, flag);
3883 
3884 		if (*rval != NDI_SUCCESS) {
3885 			*rval = (*rval == NDI_BUSY) ? EBUSY : EIO;
3886 			break;
3887 		}
3888 
3889 		fcp_update_offline_flags(plun);
3890 
3891 		ptgt = plun->lun_tgt;
3892 		mutex_enter(&ptgt->tgt_mutex);
3893 		for (tplun = ptgt->tgt_lun; tplun != NULL; tplun =
3894 		    tplun->lun_next) {
3895 			mutex_enter(&tplun->lun_mutex);
3896 			if (!(tplun->lun_state & FCP_LUN_OFFLINE)) {
3897 				all = 0;
3898 			}
3899 			mutex_exit(&tplun->lun_mutex);
3900 		}
3901 
3902 		if (all) {
3903 			ptgt->tgt_node_state = FCP_TGT_NODE_NONE;
3904 			/*
3905 			 * The user is unconfiguring/offlining the device.
3906 			 * If fabric and the auto configuration is set
3907 			 * then make sure the user is the only one who
3908 			 * can reconfigure the device.
3909 			 */
3910 			if (FC_TOP_EXTERNAL(pptr->port_topology) &&
3911 			    fcp_enable_auto_configuration) {
3912 				ptgt->tgt_manual_config_only = 1;
3913 			}
3914 		}
3915 		mutex_exit(&ptgt->tgt_mutex);
3916 		break;
3917 	}
3918 
3919 	case DEVCTL_DEVICE_ONLINE: {
3920 		int			lcount;
3921 		int			tcount;
3922 		struct fcp_lun	*plun;
3923 		child_info_t		*cip = CIP(cdip);
3924 
3925 		ASSERT(cdip != NULL);
3926 		ASSERT(pptr != NULL);
3927 
3928 		mutex_enter(&pptr->port_mutex);
3929 		if (pip != NULL) {
3930 			cip = CIP(pip);
3931 		}
3932 		if ((plun = fcp_get_lun_from_cip(pptr, cip)) == NULL) {
3933 			mutex_exit(&pptr->port_mutex);
3934 			*rval = ENXIO;
3935 			break;
3936 		}
3937 		lcount = pptr->port_link_cnt;
3938 		tcount = plun->lun_tgt->tgt_change_cnt;
3939 		mutex_exit(&pptr->port_mutex);
3940 
3941 		/*
3942 		 * The FCP_LUN_ONLINING flag is used in fcp_scsi_start()
3943 		 * to allow the device attach to occur when the device is
3944 		 * FCP_LUN_OFFLINE (so we don't reject the INQUIRY command
3945 		 * from the scsi_probe()).
3946 		 */
3947 		mutex_enter(&LUN_TGT->tgt_mutex);
3948 		plun->lun_state |= FCP_LUN_ONLINING;
3949 		mutex_exit(&LUN_TGT->tgt_mutex);
3950 
3951 		if (is_mpxio) {
3952 			mdi_devi_exit(pptr->port_dip, circ);
3953 		} else {
3954 			ndi_devi_exit(pptr->port_dip, circ);
3955 		}
3956 		devi_entered = 0;
3957 
3958 		*rval = fcp_pass_to_hp_and_wait(pptr, plun, cip,
3959 		    FCP_ONLINE, lcount, tcount, 0);
3960 
3961 		if (*rval != NDI_SUCCESS) {
3962 			/* Reset the FCP_LUN_ONLINING bit */
3963 			mutex_enter(&LUN_TGT->tgt_mutex);
3964 			plun->lun_state &= ~FCP_LUN_ONLINING;
3965 			mutex_exit(&LUN_TGT->tgt_mutex);
3966 			*rval = EIO;
3967 			break;
3968 		}
3969 		mutex_enter(&LUN_TGT->tgt_mutex);
3970 		plun->lun_state &= ~(FCP_LUN_OFFLINE | FCP_LUN_BUSY |
3971 		    FCP_LUN_ONLINING);
3972 		mutex_exit(&LUN_TGT->tgt_mutex);
3973 		break;
3974 	}
3975 
3976 	case DEVCTL_BUS_DEV_CREATE: {
3977 		uchar_t			*bytes = NULL;
3978 		uint_t			nbytes;
3979 		struct fcp_tgt		*ptgt = NULL;
3980 		struct fcp_lun		*plun = NULL;
3981 		dev_info_t		*useless_dip = NULL;
3982 
3983 		*rval = ndi_dc_devi_create(dcp, pptr->port_dip,
3984 		    DEVCTL_CONSTRUCT, &useless_dip);
3985 		if (*rval != 0 || useless_dip == NULL) {
3986 			break;
3987 		}
3988 
3989 		if ((ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, useless_dip,
3990 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, PORT_WWN_PROP, &bytes,
3991 		    &nbytes) != DDI_PROP_SUCCESS) || nbytes != FC_WWN_SIZE) {
3992 			*rval = EINVAL;
3993 			(void) ndi_devi_free(useless_dip);
3994 			if (bytes != NULL) {
3995 				ddi_prop_free(bytes);
3996 			}
3997 			break;
3998 		}
3999 
4000 		*rval = fcp_create_on_demand(pptr, bytes);
4001 		if (*rval == 0) {
4002 			mutex_enter(&pptr->port_mutex);
4003 			ptgt = fcp_lookup_target(pptr, (uchar_t *)bytes);
4004 			if (ptgt) {
4005 				/*
4006 				 * We now have a pointer to the target that
4007 				 * was created. Lets point to the first LUN on
4008 				 * this new target.
4009 				 */
4010 				mutex_enter(&ptgt->tgt_mutex);
4011 
4012 				plun = ptgt->tgt_lun;
4013 				/*
4014 				 * There may be stale/offline LUN entries on
4015 				 * this list (this is by design) and so we have
4016 				 * to make sure we point to the first online
4017 				 * LUN
4018 				 */
4019 				while (plun &&
4020 				    plun->lun_state & FCP_LUN_OFFLINE) {
4021 					plun = plun->lun_next;
4022 				}
4023 
4024 				mutex_exit(&ptgt->tgt_mutex);
4025 			}
4026 			mutex_exit(&pptr->port_mutex);
4027 		}
4028 
4029 		if (*rval == 0 && ptgt && plun) {
4030 			mutex_enter(&plun->lun_mutex);
4031 			/*
4032 			 * Allow up to fcp_lun_ready_retry seconds to
4033 			 * configure all the luns behind the target.
4034 			 *
4035 			 * The intent here is to allow targets with long
4036 			 * reboot/reset-recovery times to become available
4037 			 * while limiting the maximum wait time for an
4038 			 * unresponsive target.
4039 			 */
4040 			end_time = ddi_get_lbolt() +
4041 			    SEC_TO_TICK(fcp_lun_ready_retry);
4042 
4043 			while (ddi_get_lbolt() < end_time) {
4044 				retval = FC_SUCCESS;
4045 
4046 				/*
4047 				 * The new ndi interfaces for on-demand creation
4048 				 * are inflexible, Do some more work to pass on
4049 				 * a path name of some LUN (design is broken !)
4050 				 */
4051 				if (plun->lun_cip) {
4052 					if (plun->lun_mpxio == 0) {
4053 						cdip = DIP(plun->lun_cip);
4054 					} else {
4055 						cdip = mdi_pi_get_client(
4056 						    PIP(plun->lun_cip));
4057 					}
4058 					if (cdip == NULL) {
4059 						*rval = ENXIO;
4060 						break;
4061 					}
4062 
4063 					if (!i_ddi_devi_attached(cdip)) {
4064 						mutex_exit(&plun->lun_mutex);
4065 						delay(drv_usectohz(1000000));
4066 						mutex_enter(&plun->lun_mutex);
4067 					} else {
4068 						/*
4069 						 * This Lun is ready, lets
4070 						 * check the next one.
4071 						 */
4072 						mutex_exit(&plun->lun_mutex);
4073 						plun = plun->lun_next;
4074 						while (plun && (plun->lun_state
4075 						    & FCP_LUN_OFFLINE)) {
4076 							plun = plun->lun_next;
4077 						}
4078 						if (!plun) {
4079 							break;
4080 						}
4081 						mutex_enter(&plun->lun_mutex);
4082 					}
4083 				} else {
4084 					/*
4085 					 * lun_cip field for a valid lun
4086 					 * should never be NULL. Fail the
4087 					 * command.
4088 					 */
4089 					*rval = ENXIO;
4090 					break;
4091 				}
4092 			}
4093 			if (plun) {
4094 				mutex_exit(&plun->lun_mutex);
4095 			} else {
4096 				char devnm[MAXNAMELEN];
4097 				int nmlen;
4098 
4099 				nmlen = snprintf(devnm, MAXNAMELEN, "%s@%s",
4100 				    ddi_node_name(cdip),
4101 				    ddi_get_name_addr(cdip));
4102 
4103 				if (copyout(&devnm, dcp->cpyout_buf, nmlen) !=
4104 				    0) {
4105 					*rval = EFAULT;
4106 				}
4107 			}
4108 		} else {
4109 			int	i;
4110 			char	buf[25];
4111 
4112 			for (i = 0; i < FC_WWN_SIZE; i++) {
4113 				(void) sprintf(&buf[i << 1], "%02x", bytes[i]);
4114 			}
4115 
4116 			fcp_log(CE_WARN, pptr->port_dip,
4117 			    "!Failed to create nodes for pwwn=%s; error=%x",
4118 			    buf, *rval);
4119 		}
4120 
4121 		(void) ndi_devi_free(useless_dip);
4122 		ddi_prop_free(bytes);
4123 		break;
4124 	}
4125 
4126 	case DEVCTL_DEVICE_RESET: {
4127 		struct fcp_lun		*plun;
4128 		child_info_t		*cip = CIP(cdip);
4129 
4130 		ASSERT(cdip != NULL);
4131 		ASSERT(pptr != NULL);
4132 		mutex_enter(&pptr->port_mutex);
4133 		if (pip != NULL) {
4134 			cip = CIP(pip);
4135 		}
4136 		if ((plun = fcp_get_lun_from_cip(pptr, cip)) == NULL) {
4137 			mutex_exit(&pptr->port_mutex);
4138 			*rval = ENXIO;
4139 			break;
4140 		}
4141 		mutex_exit(&pptr->port_mutex);
4142 
4143 		mutex_enter(&plun->lun_tgt->tgt_mutex);
4144 		if (!(plun->lun_state & FCP_SCSI_LUN_TGT_INIT)) {
4145 			mutex_exit(&plun->lun_tgt->tgt_mutex);
4146 
4147 			*rval = ENXIO;
4148 			break;
4149 		}
4150 
4151 		if (plun->lun_sd == NULL) {
4152 			mutex_exit(&plun->lun_tgt->tgt_mutex);
4153 
4154 			*rval = ENXIO;
4155 			break;
4156 		}
4157 		mutex_exit(&plun->lun_tgt->tgt_mutex);
4158 
4159 		/*
4160 		 * set up ap so that fcp_reset can figure out
4161 		 * which target to reset
4162 		 */
4163 		if (fcp_scsi_reset(&plun->lun_sd->sd_address,
4164 		    RESET_TARGET) == FALSE) {
4165 			*rval = EIO;
4166 		}
4167 		break;
4168 	}
4169 
4170 	case DEVCTL_BUS_GETSTATE:
4171 		ASSERT(dcp != NULL);
4172 		ASSERT(pptr != NULL);
4173 		ASSERT(pptr->port_dip != NULL);
4174 		if (ndi_dc_return_bus_state(pptr->port_dip, dcp) !=
4175 		    NDI_SUCCESS) {
4176 			*rval = EFAULT;
4177 		}
4178 		break;
4179 
4180 	case DEVCTL_BUS_QUIESCE:
4181 	case DEVCTL_BUS_UNQUIESCE:
4182 		*rval = ENOTSUP;
4183 		break;
4184 
4185 	case DEVCTL_BUS_RESET:
4186 	case DEVCTL_BUS_RESETALL:
4187 		ASSERT(pptr != NULL);
4188 		(void) fcp_linkreset(pptr, NULL,  KM_SLEEP);
4189 		break;
4190 
4191 	default:
4192 		ASSERT(dcp != NULL);
4193 		*rval = ENOTTY;
4194 		break;
4195 	}
4196 
4197 	/* all done -- clean up and return */
4198 out:	if (devi_entered) {
4199 		if (is_mpxio) {
4200 			mdi_devi_exit(pptr->port_dip, circ);
4201 		} else {
4202 			ndi_devi_exit(pptr->port_dip, circ);
4203 		}
4204 	}
4205 
4206 	if (dcp != NULL) {
4207 		ndi_dc_freehdl(dcp);
4208 	}
4209 
4210 	return (retval);
4211 }
4212 
4213 
4214 /*ARGSUSED*/
4215 static int
4216 fcp_els_callback(opaque_t ulph, opaque_t port_handle, fc_unsol_buf_t *buf,
4217     uint32_t claimed)
4218 {
4219 	uchar_t			r_ctl;
4220 	uchar_t			ls_code;
4221 	struct fcp_port	*pptr;
4222 
4223 	if ((pptr = fcp_get_port(port_handle)) == NULL || claimed) {
4224 		return (FC_UNCLAIMED);
4225 	}
4226 
4227 	mutex_enter(&pptr->port_mutex);
4228 	if (pptr->port_state & (FCP_STATE_DETACHING |
4229 	    FCP_STATE_SUSPENDED | FCP_STATE_POWER_DOWN)) {
4230 		mutex_exit(&pptr->port_mutex);
4231 		return (FC_UNCLAIMED);
4232 	}
4233 	mutex_exit(&pptr->port_mutex);
4234 
4235 	r_ctl = buf->ub_frame.r_ctl;
4236 
4237 	switch (r_ctl & R_CTL_ROUTING) {
4238 	case R_CTL_EXTENDED_SVC:
4239 		if (r_ctl == R_CTL_ELS_REQ) {
4240 			ls_code = buf->ub_buffer[0];
4241 
4242 			switch (ls_code) {
4243 			case LA_ELS_PRLI:
4244 				/*
4245 				 * We really don't care if something fails.
4246 				 * If the PRLI was not sent out, then the
4247 				 * other end will time it out.
4248 				 */
4249 				if (fcp_unsol_prli(pptr, buf) == FC_SUCCESS) {
4250 					return (FC_SUCCESS);
4251 				}
4252 				return (FC_UNCLAIMED);
4253 				/* NOTREACHED */
4254 
4255 			default:
4256 				break;
4257 			}
4258 		}
4259 		/* FALLTHROUGH */
4260 
4261 	default:
4262 		return (FC_UNCLAIMED);
4263 	}
4264 }
4265 
4266 
4267 /*ARGSUSED*/
4268 static int
4269 fcp_data_callback(opaque_t ulph, opaque_t port_handle, fc_unsol_buf_t *buf,
4270     uint32_t claimed)
4271 {
4272 	return (FC_UNCLAIMED);
4273 }
4274 
4275 /*
4276  *     Function: fcp_statec_callback
4277  *
4278  *  Description: The purpose of this function is to handle a port state change.
4279  *		 It is called from fp/fctl and, in a few instances, internally.
4280  *
4281  *     Argument: ulph		fp/fctl port handle
4282  *		 port_handle	fcp_port structure
4283  *		 port_state	Physical state of the port
4284  *		 port_top	Topology
4285  *		 *devlist	Pointer to the first entry of a table
4286  *				containing the remote ports that can be
4287  *				reached.
4288  *		 dev_cnt	Number of entries pointed by devlist.
4289  *		 port_sid	Port ID of the local port.
4290  *
4291  * Return Value: None
4292  */
4293 /*ARGSUSED*/
4294 static void
4295 fcp_statec_callback(opaque_t ulph, opaque_t port_handle,
4296     uint32_t port_state, uint32_t port_top, fc_portmap_t *devlist,
4297     uint32_t dev_cnt, uint32_t port_sid)
4298 {
4299 	uint32_t		link_count;
4300 	int			map_len = 0;
4301 	struct fcp_port	*pptr;
4302 	fcp_map_tag_t		*map_tag = NULL;
4303 
4304 	if ((pptr = fcp_get_port(port_handle)) == NULL) {
4305 		fcp_log(CE_WARN, NULL, "!Invalid port handle in callback");
4306 		return;			/* nothing to work with! */
4307 	}
4308 
4309 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
4310 	    fcp_trace, FCP_BUF_LEVEL_2, 0,
4311 	    "fcp_statec_callback: port state/dev_cnt/top ="
4312 	    "%d/%d/%d", FC_PORT_STATE_MASK(port_state),
4313 	    dev_cnt, port_top);
4314 
4315 	mutex_enter(&pptr->port_mutex);
4316 
4317 	/*
4318 	 * If a thread is in detach, don't do anything.
4319 	 */
4320 	if (pptr->port_state & (FCP_STATE_DETACHING |
4321 	    FCP_STATE_SUSPENDED | FCP_STATE_POWER_DOWN)) {
4322 		mutex_exit(&pptr->port_mutex);
4323 		return;
4324 	}
4325 
4326 	/*
4327 	 * First thing we do is set the FCP_STATE_IN_CB_DEVC flag so that if
4328 	 * init_pkt is called, it knows whether or not the target's status
4329 	 * (or pd) might be changing.
4330 	 */
4331 
4332 	if (FC_PORT_STATE_MASK(port_state) == FC_STATE_DEVICE_CHANGE) {
4333 		pptr->port_state |= FCP_STATE_IN_CB_DEVC;
4334 	}
4335 
4336 	/*
4337 	 * the transport doesn't allocate or probe unless being
4338 	 * asked to by either the applications or ULPs
4339 	 *
4340 	 * in cases where the port is OFFLINE at the time of port
4341 	 * attach callback and the link comes ONLINE later, for
4342 	 * easier automatic node creation (i.e. without you having to
4343 	 * go out and run the utility to perform LOGINs) the
4344 	 * following conditional is helpful
4345 	 */
4346 	pptr->port_phys_state = port_state;
4347 
4348 	if (dev_cnt) {
4349 		mutex_exit(&pptr->port_mutex);
4350 
4351 		map_len = sizeof (*map_tag) * dev_cnt;
4352 		map_tag = kmem_alloc(map_len, KM_NOSLEEP);
4353 		if (map_tag == NULL) {
4354 			fcp_log(CE_WARN, pptr->port_dip,
4355 			    "!fcp%d: failed to allocate for map tags; "
4356 			    " state change will not be processed",
4357 			    pptr->port_instance);
4358 
4359 			mutex_enter(&pptr->port_mutex);
4360 			pptr->port_state &= ~FCP_STATE_IN_CB_DEVC;
4361 			mutex_exit(&pptr->port_mutex);
4362 
4363 			return;
4364 		}
4365 
4366 		mutex_enter(&pptr->port_mutex);
4367 	}
4368 
4369 	if (pptr->port_id != port_sid) {
4370 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
4371 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
4372 		    "fcp: Port S_ID=0x%x => 0x%x", pptr->port_id,
4373 		    port_sid);
4374 		/*
4375 		 * The local port changed ID. It is the first time a port ID
4376 		 * is assigned or something drastic happened.  We might have
4377 		 * been unplugged and replugged on another loop or fabric port
4378 		 * or somebody grabbed the AL_PA we had or somebody rezoned
4379 		 * the fabric we were plugged into.
4380 		 */
4381 		pptr->port_id = port_sid;
4382 	}
4383 
4384 	switch (FC_PORT_STATE_MASK(port_state)) {
4385 	case FC_STATE_OFFLINE:
4386 	case FC_STATE_RESET_REQUESTED:
4387 		/*
4388 		 * link has gone from online to offline -- just update the
4389 		 * state of this port to BUSY and MARKed to go offline
4390 		 */
4391 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
4392 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
4393 		    "link went offline");
4394 		if ((pptr->port_state & FCP_STATE_OFFLINE) && dev_cnt) {
4395 			/*
4396 			 * We were offline a while ago and this one
4397 			 * seems to indicate that the loop has gone
4398 			 * dead forever.
4399 			 */
4400 			pptr->port_tmp_cnt += dev_cnt;
4401 			pptr->port_state &= ~FCP_STATE_OFFLINE;
4402 			pptr->port_state |= FCP_STATE_INIT;
4403 			link_count = pptr->port_link_cnt;
4404 			fcp_handle_devices(pptr, devlist, dev_cnt,
4405 			    link_count, map_tag, FCP_CAUSE_LINK_DOWN);
4406 		} else {
4407 			pptr->port_link_cnt++;
4408 			ASSERT(!(pptr->port_state & FCP_STATE_SUSPENDED));
4409 			fcp_update_state(pptr, (FCP_LUN_BUSY |
4410 			    FCP_LUN_MARK), FCP_CAUSE_LINK_DOWN);
4411 			if (pptr->port_mpxio) {
4412 				fcp_update_mpxio_path_verifybusy(pptr);
4413 			}
4414 			pptr->port_state |= FCP_STATE_OFFLINE;
4415 			pptr->port_state &=
4416 			    ~(FCP_STATE_ONLINING | FCP_STATE_ONLINE);
4417 			pptr->port_tmp_cnt = 0;
4418 		}
4419 		mutex_exit(&pptr->port_mutex);
4420 		break;
4421 
4422 	case FC_STATE_ONLINE:
4423 	case FC_STATE_LIP:
4424 	case FC_STATE_LIP_LBIT_SET:
4425 		/*
4426 		 * link has gone from offline to online
4427 		 */
4428 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
4429 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
4430 		    "link went online");
4431 
4432 		pptr->port_link_cnt++;
4433 
4434 		while (pptr->port_ipkt_cnt) {
4435 			mutex_exit(&pptr->port_mutex);
4436 			delay(drv_usectohz(1000000));
4437 			mutex_enter(&pptr->port_mutex);
4438 		}
4439 
4440 		pptr->port_topology = port_top;
4441 
4442 		/*
4443 		 * The state of the targets and luns accessible through this
4444 		 * port is updated.
4445 		 */
4446 		fcp_update_state(pptr, FCP_LUN_BUSY | FCP_LUN_MARK,
4447 		    FCP_CAUSE_LINK_CHANGE);
4448 
4449 		pptr->port_state &= ~(FCP_STATE_INIT | FCP_STATE_OFFLINE);
4450 		pptr->port_state |= FCP_STATE_ONLINING;
4451 		pptr->port_tmp_cnt = dev_cnt;
4452 		link_count = pptr->port_link_cnt;
4453 
4454 		pptr->port_deadline = fcp_watchdog_time +
4455 		    FCP_ICMD_DEADLINE;
4456 
4457 		if (!dev_cnt) {
4458 			/*
4459 			 * We go directly to the online state if no remote
4460 			 * ports were discovered.
4461 			 */
4462 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
4463 			    fcp_trace, FCP_BUF_LEVEL_3, 0,
4464 			    "No remote ports discovered");
4465 
4466 			pptr->port_state &= ~FCP_STATE_ONLINING;
4467 			pptr->port_state |= FCP_STATE_ONLINE;
4468 		}
4469 
4470 		switch (port_top) {
4471 		case FC_TOP_FABRIC:
4472 		case FC_TOP_PUBLIC_LOOP:
4473 		case FC_TOP_PRIVATE_LOOP:
4474 		case FC_TOP_PT_PT:
4475 
4476 			if (pptr->port_state & FCP_STATE_NS_REG_FAILED) {
4477 				fcp_retry_ns_registry(pptr, port_sid);
4478 			}
4479 
4480 			fcp_handle_devices(pptr, devlist, dev_cnt, link_count,
4481 			    map_tag, FCP_CAUSE_LINK_CHANGE);
4482 			break;
4483 
4484 		default:
4485 			/*
4486 			 * We got here because we were provided with an unknown
4487 			 * topology.
4488 			 */
4489 			if (pptr->port_state & FCP_STATE_NS_REG_FAILED) {
4490 				pptr->port_state &= ~FCP_STATE_NS_REG_FAILED;
4491 			}
4492 
4493 			pptr->port_tmp_cnt -= dev_cnt;
4494 			fcp_log(CE_WARN, pptr->port_dip,
4495 			    "!unknown/unsupported topology (0x%x)", port_top);
4496 			break;
4497 		}
4498 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
4499 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
4500 		    "Notify ssd of the reset to reinstate the reservations");
4501 
4502 		scsi_hba_reset_notify_callback(&pptr->port_mutex,
4503 		    &pptr->port_reset_notify_listf);
4504 
4505 		mutex_exit(&pptr->port_mutex);
4506 
4507 		break;
4508 
4509 	case FC_STATE_RESET:
4510 		ASSERT(pptr->port_state & FCP_STATE_OFFLINE);
4511 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
4512 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
4513 		    "RESET state, waiting for Offline/Online state_cb");
4514 		mutex_exit(&pptr->port_mutex);
4515 		break;
4516 
4517 	case FC_STATE_DEVICE_CHANGE:
4518 		/*
4519 		 * We come here when an application has requested
4520 		 * Dynamic node creation/deletion in Fabric connectivity.
4521 		 */
4522 		if (pptr->port_state & (FCP_STATE_OFFLINE |
4523 		    FCP_STATE_INIT)) {
4524 			/*
4525 			 * This case can happen when the FCTL is in the
4526 			 * process of giving us on online and the host on
4527 			 * the other side issues a PLOGI/PLOGO. Ideally
4528 			 * the state changes should be serialized unless
4529 			 * they are opposite (online-offline).
4530 			 * The transport will give us a final state change
4531 			 * so we can ignore this for the time being.
4532 			 */
4533 			pptr->port_state &= ~FCP_STATE_IN_CB_DEVC;
4534 			mutex_exit(&pptr->port_mutex);
4535 			break;
4536 		}
4537 
4538 		if (pptr->port_state & FCP_STATE_NS_REG_FAILED) {
4539 			fcp_retry_ns_registry(pptr, port_sid);
4540 		}
4541 
4542 		/*
4543 		 * Extend the deadline under steady state conditions
4544 		 * to provide more time for the device-change-commands
4545 		 */
4546 		if (!pptr->port_ipkt_cnt) {
4547 			pptr->port_deadline = fcp_watchdog_time +
4548 			    FCP_ICMD_DEADLINE;
4549 		}
4550 
4551 		/*
4552 		 * There is another race condition here, where if we were
4553 		 * in ONLINEING state and a devices in the map logs out,
4554 		 * fp will give another state change as DEVICE_CHANGE
4555 		 * and OLD. This will result in that target being offlined.
4556 		 * The pd_handle is freed. If from the first statec callback
4557 		 * we were going to fire a PLOGI/PRLI, the system will
4558 		 * panic in fc_ulp_transport with invalid pd_handle.
4559 		 * The fix is to check for the link_cnt before issuing
4560 		 * any command down.
4561 		 */
4562 		fcp_update_targets(pptr, devlist, dev_cnt,
4563 		    FCP_LUN_BUSY | FCP_LUN_MARK, FCP_CAUSE_TGT_CHANGE);
4564 
4565 		link_count = pptr->port_link_cnt;
4566 
4567 		fcp_handle_devices(pptr, devlist, dev_cnt,
4568 		    link_count, map_tag, FCP_CAUSE_TGT_CHANGE);
4569 
4570 		pptr->port_state &= ~FCP_STATE_IN_CB_DEVC;
4571 
4572 		mutex_exit(&pptr->port_mutex);
4573 		break;
4574 
4575 	case FC_STATE_TARGET_PORT_RESET:
4576 		if (pptr->port_state & FCP_STATE_NS_REG_FAILED) {
4577 			fcp_retry_ns_registry(pptr, port_sid);
4578 		}
4579 
4580 		/* Do nothing else */
4581 		mutex_exit(&pptr->port_mutex);
4582 		break;
4583 
4584 	default:
4585 		fcp_log(CE_WARN, pptr->port_dip,
4586 		    "!Invalid state change=0x%x", port_state);
4587 		mutex_exit(&pptr->port_mutex);
4588 		break;
4589 	}
4590 
4591 	if (map_tag) {
4592 		kmem_free(map_tag, map_len);
4593 	}
4594 }
4595 
4596 /*
4597  *     Function: fcp_handle_devices
4598  *
4599  *  Description: This function updates the devices currently known by
4600  *		 walking the list provided by the caller.  The list passed
4601  *		 by the caller is supposed to be the list of reachable
4602  *		 devices.
4603  *
4604  *     Argument: *pptr		Fcp port structure.
4605  *		 *devlist	Pointer to the first entry of a table
4606  *				containing the remote ports that can be
4607  *				reached.
4608  *		 dev_cnt	Number of entries pointed by devlist.
4609  *		 link_cnt	Link state count.
4610  *		 *map_tag	Array of fcp_map_tag_t structures.
4611  *		 cause		What caused this function to be called.
4612  *
4613  * Return Value: None
4614  *
4615  *	  Notes: The pptr->port_mutex must be held.
4616  */
4617 static void
4618 fcp_handle_devices(struct fcp_port *pptr, fc_portmap_t devlist[],
4619     uint32_t dev_cnt, int link_cnt, fcp_map_tag_t *map_tag, int cause)
4620 {
4621 	int			i;
4622 	int			check_finish_init = 0;
4623 	fc_portmap_t		*map_entry;
4624 	struct fcp_tgt	*ptgt = NULL;
4625 
4626 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
4627 	    fcp_trace, FCP_BUF_LEVEL_3, 0,
4628 	    "fcp_handle_devices: called for %d dev(s)", dev_cnt);
4629 
4630 	if (dev_cnt) {
4631 		ASSERT(map_tag != NULL);
4632 	}
4633 
4634 	/*
4635 	 * The following code goes through the list of remote ports that are
4636 	 * accessible through this (pptr) local port (The list walked is the
4637 	 * one provided by the caller which is the list of the remote ports
4638 	 * currently reachable).  It checks if any of them was already
4639 	 * known by looking for the corresponding target structure based on
4640 	 * the world wide name.	 If a target is part of the list it is tagged
4641 	 * (ptgt->tgt_aux_state = FCP_TGT_TAGGED).
4642 	 *
4643 	 * Old comment
4644 	 * -----------
4645 	 * Before we drop port mutex; we MUST get the tags updated; This
4646 	 * two step process is somewhat slow, but more reliable.
4647 	 */
4648 	for (i = 0; (i < dev_cnt) && (pptr->port_link_cnt == link_cnt); i++) {
4649 		map_entry = &(devlist[i]);
4650 
4651 		/*
4652 		 * get ptr to this map entry in our port's
4653 		 * list (if any)
4654 		 */
4655 		ptgt = fcp_lookup_target(pptr,
4656 		    (uchar_t *)&(map_entry->map_pwwn));
4657 
4658 		if (ptgt) {
4659 			map_tag[i] = ptgt->tgt_change_cnt;
4660 			if (cause == FCP_CAUSE_LINK_CHANGE) {
4661 				ptgt->tgt_aux_state = FCP_TGT_TAGGED;
4662 			}
4663 		}
4664 	}
4665 
4666 	/*
4667 	 * At this point we know which devices of the new list were already
4668 	 * known (The field tgt_aux_state of the target structure has been
4669 	 * set to FCP_TGT_TAGGED).
4670 	 *
4671 	 * The following code goes through the list of targets currently known
4672 	 * by the local port (the list is actually a hashing table).  If a
4673 	 * target is found and is not tagged, it means the target cannot
4674 	 * be reached anymore through the local port (pptr).  It is offlined.
4675 	 * The offlining only occurs if the cause is FCP_CAUSE_LINK_CHANGE.
4676 	 */
4677 	for (i = 0; i < FCP_NUM_HASH; i++) {
4678 		for (ptgt = pptr->port_tgt_hash_table[i]; ptgt != NULL;
4679 		    ptgt = ptgt->tgt_next) {
4680 			mutex_enter(&ptgt->tgt_mutex);
4681 			if ((ptgt->tgt_aux_state != FCP_TGT_TAGGED) &&
4682 			    (cause == FCP_CAUSE_LINK_CHANGE) &&
4683 			    !(ptgt->tgt_state & FCP_TGT_OFFLINE)) {
4684 				fcp_offline_target_now(pptr, ptgt,
4685 				    link_cnt, ptgt->tgt_change_cnt, 0);
4686 			}
4687 			mutex_exit(&ptgt->tgt_mutex);
4688 		}
4689 	}
4690 
4691 	/*
4692 	 * At this point, the devices that were known but cannot be reached
4693 	 * anymore, have most likely been offlined.
4694 	 *
4695 	 * The following section of code seems to go through the list of
4696 	 * remote ports that can now be reached.  For every single one it
4697 	 * checks if it is already known or if it is a new port.
4698 	 */
4699 	for (i = 0; (i < dev_cnt) && (pptr->port_link_cnt == link_cnt); i++) {
4700 
4701 		if (check_finish_init) {
4702 			ASSERT(i > 0);
4703 			(void) fcp_call_finish_init_held(pptr, ptgt, link_cnt,
4704 			    map_tag[i - 1], cause);
4705 			check_finish_init = 0;
4706 		}
4707 
4708 		/* get a pointer to this map entry */
4709 		map_entry = &(devlist[i]);
4710 
4711 		/*
4712 		 * Check for the duplicate map entry flag. If we have marked
4713 		 * this entry as a duplicate we skip it since the correct
4714 		 * (perhaps even same) state change will be encountered
4715 		 * later in the list.
4716 		 */
4717 		if (map_entry->map_flags & PORT_DEVICE_DUPLICATE_MAP_ENTRY) {
4718 			continue;
4719 		}
4720 
4721 		/* get ptr to this map entry in our port's list (if any) */
4722 		ptgt = fcp_lookup_target(pptr,
4723 		    (uchar_t *)&(map_entry->map_pwwn));
4724 
4725 		if (ptgt) {
4726 			/*
4727 			 * This device was already known.  The field
4728 			 * tgt_aux_state is reset (was probably set to
4729 			 * FCP_TGT_TAGGED previously in this routine).
4730 			 */
4731 			ptgt->tgt_aux_state = 0;
4732 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
4733 			    fcp_trace, FCP_BUF_LEVEL_3, 0,
4734 			    "handle_devices: map did/state/type/flags = "
4735 			    "0x%x/0x%x/0x%x/0x%x, tgt_d_id=0x%x, "
4736 			    "tgt_state=%d",
4737 			    map_entry->map_did.port_id, map_entry->map_state,
4738 			    map_entry->map_type, map_entry->map_flags,
4739 			    ptgt->tgt_d_id, ptgt->tgt_state);
4740 		}
4741 
4742 		if (map_entry->map_type == PORT_DEVICE_OLD ||
4743 		    map_entry->map_type == PORT_DEVICE_NEW ||
4744 		    map_entry->map_type == PORT_DEVICE_REPORTLUN_CHANGED ||
4745 		    map_entry->map_type == PORT_DEVICE_CHANGED) {
4746 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
4747 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
4748 			    "map_type=%x, did = %x",
4749 			    map_entry->map_type,
4750 			    map_entry->map_did.port_id);
4751 		}
4752 
4753 		switch (map_entry->map_type) {
4754 		case PORT_DEVICE_NOCHANGE:
4755 		case PORT_DEVICE_USER_CREATE:
4756 		case PORT_DEVICE_USER_LOGIN:
4757 		case PORT_DEVICE_NEW:
4758 		case PORT_DEVICE_REPORTLUN_CHANGED:
4759 			FCP_TGT_TRACE(ptgt, map_tag[i], FCP_TGT_TRACE_1);
4760 
4761 			if (fcp_handle_mapflags(pptr, ptgt, map_entry,
4762 			    link_cnt, (ptgt) ? map_tag[i] : 0,
4763 			    cause) == TRUE) {
4764 
4765 				FCP_TGT_TRACE(ptgt, map_tag[i],
4766 				    FCP_TGT_TRACE_2);
4767 				check_finish_init++;
4768 			}
4769 			break;
4770 
4771 		case PORT_DEVICE_OLD:
4772 			if (ptgt != NULL) {
4773 				FCP_TGT_TRACE(ptgt, map_tag[i],
4774 				    FCP_TGT_TRACE_3);
4775 
4776 				mutex_enter(&ptgt->tgt_mutex);
4777 				if (!(ptgt->tgt_state & FCP_TGT_OFFLINE)) {
4778 					/*
4779 					 * Must do an in-line wait for I/Os
4780 					 * to get drained
4781 					 */
4782 					mutex_exit(&ptgt->tgt_mutex);
4783 					mutex_exit(&pptr->port_mutex);
4784 
4785 					mutex_enter(&ptgt->tgt_mutex);
4786 					while (ptgt->tgt_ipkt_cnt ||
4787 					    fcp_outstanding_lun_cmds(ptgt)
4788 					    == FC_SUCCESS) {
4789 						mutex_exit(&ptgt->tgt_mutex);
4790 						delay(drv_usectohz(1000000));
4791 						mutex_enter(&ptgt->tgt_mutex);
4792 					}
4793 					mutex_exit(&ptgt->tgt_mutex);
4794 
4795 					mutex_enter(&pptr->port_mutex);
4796 					mutex_enter(&ptgt->tgt_mutex);
4797 
4798 					(void) fcp_offline_target(pptr, ptgt,
4799 					    link_cnt, map_tag[i], 0, 0);
4800 				}
4801 				mutex_exit(&ptgt->tgt_mutex);
4802 			}
4803 			check_finish_init++;
4804 			break;
4805 
4806 		case PORT_DEVICE_USER_DELETE:
4807 		case PORT_DEVICE_USER_LOGOUT:
4808 			if (ptgt != NULL) {
4809 				FCP_TGT_TRACE(ptgt, map_tag[i],
4810 				    FCP_TGT_TRACE_4);
4811 
4812 				mutex_enter(&ptgt->tgt_mutex);
4813 				if (!(ptgt->tgt_state & FCP_TGT_OFFLINE)) {
4814 					(void) fcp_offline_target(pptr, ptgt,
4815 					    link_cnt, map_tag[i], 1, 0);
4816 				}
4817 				mutex_exit(&ptgt->tgt_mutex);
4818 			}
4819 			check_finish_init++;
4820 			break;
4821 
4822 		case PORT_DEVICE_CHANGED:
4823 			if (ptgt != NULL) {
4824 				FCP_TGT_TRACE(ptgt, map_tag[i],
4825 				    FCP_TGT_TRACE_5);
4826 
4827 				if (fcp_device_changed(pptr, ptgt,
4828 				    map_entry, link_cnt, map_tag[i],
4829 				    cause) == TRUE) {
4830 					check_finish_init++;
4831 				}
4832 			} else {
4833 				if (fcp_handle_mapflags(pptr, ptgt,
4834 				    map_entry, link_cnt, 0, cause) == TRUE) {
4835 					check_finish_init++;
4836 				}
4837 			}
4838 			break;
4839 
4840 		default:
4841 			fcp_log(CE_WARN, pptr->port_dip,
4842 			    "!Invalid map_type=0x%x", map_entry->map_type);
4843 			check_finish_init++;
4844 			break;
4845 		}
4846 	}
4847 
4848 	if (check_finish_init && pptr->port_link_cnt == link_cnt) {
4849 		ASSERT(i > 0);
4850 		(void) fcp_call_finish_init_held(pptr, ptgt, link_cnt,
4851 		    map_tag[i-1], cause);
4852 	} else if (dev_cnt == 0 && pptr->port_link_cnt == link_cnt) {
4853 		fcp_offline_all(pptr, link_cnt, cause);
4854 	}
4855 }
4856 
4857 static int
4858 fcp_handle_reportlun_changed(struct fcp_tgt *ptgt, int cause)
4859 {
4860 	struct fcp_lun	*plun;
4861 	struct fcp_port *pptr;
4862 	int		 rscn_count;
4863 	int		 lun0_newalloc;
4864 	int		 ret  = TRUE;
4865 
4866 	ASSERT(ptgt);
4867 	pptr = ptgt->tgt_port;
4868 	lun0_newalloc = 0;
4869 	if ((plun = fcp_get_lun(ptgt, 0)) == NULL) {
4870 		/*
4871 		 * no LUN struct for LUN 0 yet exists,
4872 		 * so create one
4873 		 */
4874 		plun = fcp_alloc_lun(ptgt);
4875 		if (plun == NULL) {
4876 			fcp_log(CE_WARN, pptr->port_dip,
4877 			    "!Failed to allocate lun 0 for"
4878 			    " D_ID=%x", ptgt->tgt_d_id);
4879 			return (ret);
4880 		}
4881 		lun0_newalloc = 1;
4882 	}
4883 
4884 	mutex_enter(&ptgt->tgt_mutex);
4885 	/*
4886 	 * consider lun 0 as device not connected if it is
4887 	 * offlined or newly allocated
4888 	 */
4889 	if ((plun->lun_state & FCP_LUN_OFFLINE) || lun0_newalloc) {
4890 		plun->lun_state |= FCP_LUN_DEVICE_NOT_CONNECTED;
4891 	}
4892 	plun->lun_state |= (FCP_LUN_BUSY | FCP_LUN_MARK);
4893 	plun->lun_state &= ~FCP_LUN_OFFLINE;
4894 	ptgt->tgt_lun_cnt = 1;
4895 	ptgt->tgt_report_lun_cnt = 0;
4896 	mutex_exit(&ptgt->tgt_mutex);
4897 
4898 	rscn_count = fc_ulp_get_rscn_count(pptr->port_fp_handle);
4899 	if (fcp_send_scsi(plun, SCMD_REPORT_LUN,
4900 	    sizeof (struct fcp_reportlun_resp), pptr->port_link_cnt,
4901 	    ptgt->tgt_change_cnt, cause, rscn_count) != DDI_SUCCESS) {
4902 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
4903 		    fcp_trace, FCP_BUF_LEVEL_3, 0, "!Failed to send REPORTLUN "
4904 		    "to D_ID=%x", ptgt->tgt_d_id);
4905 	} else {
4906 		ret = FALSE;
4907 	}
4908 
4909 	return (ret);
4910 }
4911 
4912 /*
4913  *     Function: fcp_handle_mapflags
4914  *
4915  *  Description: This function creates a target structure if the ptgt passed
4916  *		 is NULL.  It also kicks off the PLOGI if we are not logged
4917  *		 into the target yet or the PRLI if we are logged into the
4918  *		 target already.  The rest of the treatment is done in the
4919  *		 callbacks of the PLOGI or PRLI.
4920  *
4921  *     Argument: *pptr		FCP Port structure.
4922  *		 *ptgt		Target structure.
4923  *		 *map_entry	Array of fc_portmap_t structures.
4924  *		 link_cnt	Link state count.
4925  *		 tgt_cnt	Target state count.
4926  *		 cause		What caused this function to be called.
4927  *
4928  * Return Value: TRUE	Failed
4929  *		 FALSE	Succeeded
4930  *
4931  *	  Notes: pptr->port_mutex must be owned.
4932  */
4933 static int
4934 fcp_handle_mapflags(struct fcp_port	*pptr, struct fcp_tgt	*ptgt,
4935     fc_portmap_t *map_entry, int link_cnt, int tgt_cnt, int cause)
4936 {
4937 	int			lcount;
4938 	int			tcount;
4939 	int			ret = TRUE;
4940 	int			alloc;
4941 	struct fcp_ipkt	*icmd;
4942 	struct fcp_lun	*pseq_lun = NULL;
4943 	uchar_t			opcode;
4944 	int			valid_ptgt_was_passed = FALSE;
4945 
4946 	ASSERT(mutex_owned(&pptr->port_mutex));
4947 
4948 	/*
4949 	 * This case is possible where the FCTL has come up and done discovery
4950 	 * before FCP was loaded and attached. FCTL would have discovered the
4951 	 * devices and later the ULP came online. In this case ULP's would get
4952 	 * PORT_DEVICE_NOCHANGE but target would be NULL.
4953 	 */
4954 	if (ptgt == NULL) {
4955 		/* don't already have a target */
4956 		mutex_exit(&pptr->port_mutex);
4957 		ptgt = fcp_alloc_tgt(pptr, map_entry, link_cnt);
4958 		mutex_enter(&pptr->port_mutex);
4959 
4960 		if (ptgt == NULL) {
4961 			fcp_log(CE_WARN, pptr->port_dip,
4962 			    "!FC target allocation failed");
4963 			return (ret);
4964 		}
4965 		mutex_enter(&ptgt->tgt_mutex);
4966 		ptgt->tgt_statec_cause = cause;
4967 		ptgt->tgt_tmp_cnt = 1;
4968 		mutex_exit(&ptgt->tgt_mutex);
4969 	} else {
4970 		valid_ptgt_was_passed = TRUE;
4971 	}
4972 
4973 	/*
4974 	 * Copy in the target parameters
4975 	 */
4976 	mutex_enter(&ptgt->tgt_mutex);
4977 	ptgt->tgt_d_id = map_entry->map_did.port_id;
4978 	ptgt->tgt_hard_addr = map_entry->map_hard_addr.hard_addr;
4979 	ptgt->tgt_pd_handle = map_entry->map_pd;
4980 	ptgt->tgt_fca_dev = NULL;
4981 
4982 	/* Copy port and node WWNs */
4983 	bcopy(&map_entry->map_nwwn, &ptgt->tgt_node_wwn.raw_wwn[0],
4984 	    FC_WWN_SIZE);
4985 	bcopy(&map_entry->map_pwwn, &ptgt->tgt_port_wwn.raw_wwn[0],
4986 	    FC_WWN_SIZE);
4987 
4988 	if (!(map_entry->map_flags & PORT_DEVICE_NO_SKIP_DEVICE_DISCOVERY) &&
4989 	    (map_entry->map_type == PORT_DEVICE_NOCHANGE) &&
4990 	    (map_entry->map_state == PORT_DEVICE_LOGGED_IN) &&
4991 	    valid_ptgt_was_passed) {
4992 		/*
4993 		 * determine if there are any tape LUNs on this target
4994 		 */
4995 		for (pseq_lun = ptgt->tgt_lun;
4996 		    pseq_lun != NULL;
4997 		    pseq_lun = pseq_lun->lun_next) {
4998 			if ((pseq_lun->lun_type == DTYPE_SEQUENTIAL) &&
4999 			    !(pseq_lun->lun_state & FCP_LUN_OFFLINE)) {
5000 				fcp_update_tgt_state(ptgt, FCP_RESET,
5001 				    FCP_LUN_MARK);
5002 				mutex_exit(&ptgt->tgt_mutex);
5003 				return (ret);
5004 			}
5005 		}
5006 	}
5007 
5008 	/*
5009 	 * if UA'REPORT_LUN_CHANGED received,
5010 	 * send out REPORT LUN promptly, skip PLOGI/PRLI process
5011 	 */
5012 	if (map_entry->map_type == PORT_DEVICE_REPORTLUN_CHANGED) {
5013 		ptgt->tgt_state &= ~(FCP_TGT_OFFLINE | FCP_TGT_MARK);
5014 		mutex_exit(&ptgt->tgt_mutex);
5015 		mutex_exit(&pptr->port_mutex);
5016 
5017 		ret = fcp_handle_reportlun_changed(ptgt, cause);
5018 
5019 		mutex_enter(&pptr->port_mutex);
5020 		return (ret);
5021 	}
5022 
5023 	/*
5024 	 * If ptgt was NULL when this function was entered, then tgt_node_state
5025 	 * was never specifically initialized but zeroed out which means
5026 	 * FCP_TGT_NODE_NONE.
5027 	 */
5028 	switch (ptgt->tgt_node_state) {
5029 	case FCP_TGT_NODE_NONE:
5030 	case FCP_TGT_NODE_ON_DEMAND:
5031 		if (FC_TOP_EXTERNAL(pptr->port_topology) &&
5032 		    !fcp_enable_auto_configuration &&
5033 		    map_entry->map_type != PORT_DEVICE_USER_CREATE) {
5034 			ptgt->tgt_node_state = FCP_TGT_NODE_ON_DEMAND;
5035 		} else if (FC_TOP_EXTERNAL(pptr->port_topology) &&
5036 		    fcp_enable_auto_configuration &&
5037 		    (ptgt->tgt_manual_config_only == 1) &&
5038 		    map_entry->map_type != PORT_DEVICE_USER_CREATE) {
5039 			/*
5040 			 * If auto configuration is set and
5041 			 * the tgt_manual_config_only flag is set then
5042 			 * we only want the user to be able to change
5043 			 * the state through create_on_demand.
5044 			 */
5045 			ptgt->tgt_node_state = FCP_TGT_NODE_ON_DEMAND;
5046 		} else {
5047 			ptgt->tgt_node_state = FCP_TGT_NODE_NONE;
5048 		}
5049 		break;
5050 
5051 	case FCP_TGT_NODE_PRESENT:
5052 		break;
5053 	}
5054 	/*
5055 	 * If we are booting from a fabric device, make sure we
5056 	 * mark the node state appropriately for this target to be
5057 	 * enumerated
5058 	 */
5059 	if (FC_TOP_EXTERNAL(pptr->port_topology) && pptr->port_boot_wwn[0]) {
5060 		if (bcmp((caddr_t)pptr->port_boot_wwn,
5061 		    (caddr_t)&ptgt->tgt_port_wwn.raw_wwn[0],
5062 		    sizeof (ptgt->tgt_port_wwn)) == 0) {
5063 			ptgt->tgt_node_state = FCP_TGT_NODE_NONE;
5064 		}
5065 	}
5066 	mutex_exit(&ptgt->tgt_mutex);
5067 
5068 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
5069 	    fcp_trace, FCP_BUF_LEVEL_3, 0,
5070 	    "map_pd=%p, map_type=%x, did = %x, ulp_rscn_count=0x%x",
5071 	    map_entry->map_pd, map_entry->map_type, map_entry->map_did.port_id,
5072 	    map_entry->map_rscn_info.ulp_rscn_count);
5073 
5074 	mutex_enter(&ptgt->tgt_mutex);
5075 
5076 	/*
5077 	 * Reset target OFFLINE state and mark the target BUSY
5078 	 */
5079 	ptgt->tgt_state &= ~FCP_TGT_OFFLINE;
5080 	ptgt->tgt_state |= (FCP_TGT_BUSY | FCP_TGT_MARK);
5081 
5082 	tcount = tgt_cnt ? tgt_cnt : ptgt->tgt_change_cnt;
5083 	lcount = link_cnt;
5084 
5085 	mutex_exit(&ptgt->tgt_mutex);
5086 	mutex_exit(&pptr->port_mutex);
5087 
5088 	/*
5089 	 * if we are already logged in, then we do a PRLI, else
5090 	 * we do a PLOGI first (to get logged in)
5091 	 *
5092 	 * We will not check if we are the PLOGI initiator
5093 	 */
5094 	opcode = (map_entry->map_state == PORT_DEVICE_LOGGED_IN &&
5095 	    map_entry->map_pd != NULL) ? LA_ELS_PRLI : LA_ELS_PLOGI;
5096 
5097 	alloc = FCP_MAX(sizeof (la_els_logi_t), sizeof (la_els_prli_t));
5098 
5099 	icmd = fcp_icmd_alloc(pptr, ptgt, alloc, alloc, 0,
5100 	    pptr->port_state & FCP_STATE_FCA_IS_NODMA, lcount, tcount,
5101 	    cause, map_entry->map_rscn_info.ulp_rscn_count);
5102 
5103 	if (icmd == NULL) {
5104 		FCP_TGT_TRACE(ptgt, tgt_cnt, FCP_TGT_TRACE_29);
5105 		/*
5106 		 * We've exited port_mutex before calling fcp_icmd_alloc,
5107 		 * we need to make sure we reacquire it before returning.
5108 		 */
5109 		mutex_enter(&pptr->port_mutex);
5110 		return (FALSE);
5111 	}
5112 
5113 	/* TRUE is only returned while target is intended skipped */
5114 	ret = FALSE;
5115 	/* discover info about this target */
5116 	if ((fcp_send_els(pptr, ptgt, icmd, opcode,
5117 	    lcount, tcount, cause)) == DDI_SUCCESS) {
5118 		FCP_TGT_TRACE(ptgt, tgt_cnt, FCP_TGT_TRACE_9);
5119 	} else {
5120 		fcp_icmd_free(pptr, icmd);
5121 		ret = TRUE;
5122 	}
5123 	mutex_enter(&pptr->port_mutex);
5124 
5125 	return (ret);
5126 }
5127 
5128 /*
5129  *     Function: fcp_send_els
5130  *
5131  *  Description: Sends an ELS to the target specified by the caller.  Supports
5132  *		 PLOGI and PRLI.
5133  *
5134  *     Argument: *pptr		Fcp port.
5135  *		 *ptgt		Target to send the ELS to.
5136  *		 *icmd		Internal packet
5137  *		 opcode		ELS opcode
5138  *		 lcount		Link state change counter
5139  *		 tcount		Target state change counter
5140  *		 cause		What caused the call
5141  *
5142  * Return Value: DDI_SUCCESS
5143  *		 Others
5144  */
5145 static int
5146 fcp_send_els(struct fcp_port *pptr, struct fcp_tgt *ptgt,
5147     struct fcp_ipkt *icmd, uchar_t opcode, int lcount, int tcount, int cause)
5148 {
5149 	fc_packet_t		*fpkt;
5150 	fc_frame_hdr_t		*hp;
5151 	int			internal = 0;
5152 	int			alloc;
5153 	int			cmd_len;
5154 	int			resp_len;
5155 	int			res = DDI_FAILURE; /* default result */
5156 	int			rval = DDI_FAILURE;
5157 
5158 	ASSERT(opcode == LA_ELS_PLOGI || opcode == LA_ELS_PRLI);
5159 	ASSERT(ptgt->tgt_port == pptr);
5160 
5161 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
5162 	    fcp_trace, FCP_BUF_LEVEL_5, 0,
5163 	    "fcp_send_els: d_id=0x%x ELS 0x%x (%s)", ptgt->tgt_d_id, opcode,
5164 	    (opcode == LA_ELS_PLOGI) ? "PLOGI" : "PRLI");
5165 
5166 	if (opcode == LA_ELS_PLOGI) {
5167 		cmd_len = sizeof (la_els_logi_t);
5168 		resp_len = sizeof (la_els_logi_t);
5169 	} else {
5170 		ASSERT(opcode == LA_ELS_PRLI);
5171 		cmd_len = sizeof (la_els_prli_t);
5172 		resp_len = sizeof (la_els_prli_t);
5173 	}
5174 
5175 	if (icmd == NULL) {
5176 		alloc = FCP_MAX(sizeof (la_els_logi_t),
5177 		    sizeof (la_els_prli_t));
5178 		icmd = fcp_icmd_alloc(pptr, ptgt, alloc, alloc, 0,
5179 		    pptr->port_state & FCP_STATE_FCA_IS_NODMA,
5180 		    lcount, tcount, cause, FC_INVALID_RSCN_COUNT);
5181 		if (icmd == NULL) {
5182 			FCP_TGT_TRACE(ptgt, tcount, FCP_TGT_TRACE_10);
5183 			return (res);
5184 		}
5185 		internal++;
5186 	}
5187 	fpkt = icmd->ipkt_fpkt;
5188 
5189 	fpkt->pkt_cmdlen = cmd_len;
5190 	fpkt->pkt_rsplen = resp_len;
5191 	fpkt->pkt_datalen = 0;
5192 	icmd->ipkt_retries = 0;
5193 
5194 	/* fill in fpkt info */
5195 	fpkt->pkt_tran_flags = FC_TRAN_CLASS3 | FC_TRAN_INTR;
5196 	fpkt->pkt_tran_type = FC_PKT_EXCHANGE;
5197 	fpkt->pkt_timeout = FCP_ELS_TIMEOUT;
5198 
5199 	/* get ptr to frame hdr in fpkt */
5200 	hp = &fpkt->pkt_cmd_fhdr;
5201 
5202 	/*
5203 	 * fill in frame hdr
5204 	 */
5205 	hp->r_ctl = R_CTL_ELS_REQ;
5206 	hp->s_id = pptr->port_id;	/* source ID */
5207 	hp->d_id = ptgt->tgt_d_id;	/* dest ID */
5208 	hp->type = FC_TYPE_EXTENDED_LS;
5209 	hp->f_ctl = F_CTL_SEQ_INITIATIVE | F_CTL_FIRST_SEQ;
5210 	hp->seq_id = 0;
5211 	hp->rsvd = 0;
5212 	hp->df_ctl  = 0;
5213 	hp->seq_cnt = 0;
5214 	hp->ox_id = 0xffff;		/* i.e. none */
5215 	hp->rx_id = 0xffff;		/* i.e. none */
5216 	hp->ro = 0;
5217 
5218 	/*
5219 	 * at this point we have a filled in cmd pkt
5220 	 *
5221 	 * fill in the respective info, then use the transport to send
5222 	 * the packet
5223 	 *
5224 	 * for a PLOGI call fc_ulp_login(), and
5225 	 * for a PRLI call fc_ulp_issue_els()
5226 	 */
5227 	switch (opcode) {
5228 	case LA_ELS_PLOGI: {
5229 		struct la_els_logi logi;
5230 
5231 		bzero(&logi, sizeof (struct la_els_logi));
5232 
5233 		hp = &fpkt->pkt_cmd_fhdr;
5234 		hp->r_ctl = R_CTL_ELS_REQ;
5235 		logi.ls_code.ls_code = LA_ELS_PLOGI;
5236 		logi.ls_code.mbz = 0;
5237 
5238 		FCP_CP_OUT((uint8_t *)&logi, fpkt->pkt_cmd,
5239 		    fpkt->pkt_cmd_acc, sizeof (struct la_els_logi));
5240 
5241 		icmd->ipkt_opcode = LA_ELS_PLOGI;
5242 
5243 		mutex_enter(&pptr->port_mutex);
5244 		if (!FCP_TGT_STATE_CHANGED(ptgt, icmd)) {
5245 
5246 			mutex_exit(&pptr->port_mutex);
5247 
5248 			rval = fc_ulp_login(pptr->port_fp_handle, &fpkt, 1);
5249 			if (rval == FC_SUCCESS) {
5250 				res = DDI_SUCCESS;
5251 				break;
5252 			}
5253 
5254 			FCP_TGT_TRACE(ptgt, tcount, FCP_TGT_TRACE_11);
5255 
5256 			res = fcp_handle_ipkt_errors(pptr, ptgt, icmd,
5257 			    rval, "PLOGI");
5258 		} else {
5259 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
5260 			    fcp_trace, FCP_BUF_LEVEL_5, 0,
5261 			    "fcp_send_els1: state change occured"
5262 			    " for D_ID=0x%x", ptgt->tgt_d_id);
5263 			mutex_exit(&pptr->port_mutex);
5264 			FCP_TGT_TRACE(ptgt, tcount, FCP_TGT_TRACE_12);
5265 		}
5266 		break;
5267 	}
5268 
5269 	case LA_ELS_PRLI: {
5270 		struct la_els_prli	prli;
5271 		struct fcp_prli		*fprli;
5272 
5273 		bzero(&prli, sizeof (struct la_els_prli));
5274 
5275 		hp = &fpkt->pkt_cmd_fhdr;
5276 		hp->r_ctl = R_CTL_ELS_REQ;
5277 
5278 		/* fill in PRLI cmd ELS fields */
5279 		prli.ls_code = LA_ELS_PRLI;
5280 		prli.page_length = 0x10;	/* huh? */
5281 		prli.payload_length = sizeof (struct la_els_prli);
5282 
5283 		icmd->ipkt_opcode = LA_ELS_PRLI;
5284 
5285 		/* get ptr to PRLI service params */
5286 		fprli = (struct fcp_prli *)prli.service_params;
5287 
5288 		/* fill in service params */
5289 		fprli->type = 0x08;
5290 		fprli->resvd1 = 0;
5291 		fprli->orig_process_assoc_valid = 0;
5292 		fprli->resp_process_assoc_valid = 0;
5293 		fprli->establish_image_pair = 1;
5294 		fprli->resvd2 = 0;
5295 		fprli->resvd3 = 0;
5296 		fprli->obsolete_1 = 0;
5297 		fprli->obsolete_2 = 0;
5298 		fprli->data_overlay_allowed = 0;
5299 		fprli->initiator_fn = 1;
5300 		fprli->confirmed_compl_allowed = 1;
5301 
5302 		if (fc_ulp_is_name_present("ltct") == FC_SUCCESS) {
5303 			fprli->target_fn = 1;
5304 		} else {
5305 			fprli->target_fn = 0;
5306 		}
5307 
5308 		fprli->retry = 1;
5309 		fprli->read_xfer_rdy_disabled = 1;
5310 		fprli->write_xfer_rdy_disabled = 0;
5311 
5312 		FCP_CP_OUT((uint8_t *)&prli, fpkt->pkt_cmd,
5313 		    fpkt->pkt_cmd_acc, sizeof (struct la_els_prli));
5314 
5315 		/* issue the PRLI request */
5316 
5317 		mutex_enter(&pptr->port_mutex);
5318 		if (!FCP_TGT_STATE_CHANGED(ptgt, icmd)) {
5319 
5320 			mutex_exit(&pptr->port_mutex);
5321 
5322 			rval = fc_ulp_issue_els(pptr->port_fp_handle, fpkt);
5323 			if (rval == FC_SUCCESS) {
5324 				res = DDI_SUCCESS;
5325 				break;
5326 			}
5327 
5328 			FCP_TGT_TRACE(ptgt, tcount, FCP_TGT_TRACE_13);
5329 
5330 			res = fcp_handle_ipkt_errors(pptr, ptgt, icmd,
5331 			    rval, "PRLI");
5332 		} else {
5333 			mutex_exit(&pptr->port_mutex);
5334 			FCP_TGT_TRACE(ptgt, tcount, FCP_TGT_TRACE_14);
5335 		}
5336 		break;
5337 	}
5338 
5339 	default:
5340 		fcp_log(CE_WARN, NULL, "!invalid ELS opcode=0x%x", opcode);
5341 		break;
5342 	}
5343 
5344 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
5345 	    fcp_trace, FCP_BUF_LEVEL_5, 0,
5346 	    "fcp_send_els: returning %d", res);
5347 
5348 	if (res != DDI_SUCCESS) {
5349 		if (internal) {
5350 			fcp_icmd_free(pptr, icmd);
5351 		}
5352 	}
5353 
5354 	return (res);
5355 }
5356 
5357 
5358 /*
5359  * called internally update the state of all of the tgts and each LUN
5360  * for this port (i.e. each target  known to be attached to this port)
5361  * if they are not already offline
5362  *
5363  * must be called with the port mutex owned
5364  *
5365  * acquires and releases the target mutexes for each target attached
5366  * to this port
5367  */
5368 void
5369 fcp_update_state(struct fcp_port *pptr, uint32_t state, int cause)
5370 {
5371 	int i;
5372 	struct fcp_tgt *ptgt;
5373 
5374 	ASSERT(mutex_owned(&pptr->port_mutex));
5375 
5376 	for (i = 0; i < FCP_NUM_HASH; i++) {
5377 		for (ptgt = pptr->port_tgt_hash_table[i]; ptgt != NULL;
5378 		    ptgt = ptgt->tgt_next) {
5379 			mutex_enter(&ptgt->tgt_mutex);
5380 			fcp_update_tgt_state(ptgt, FCP_SET, state);
5381 			ptgt->tgt_change_cnt++;
5382 			ptgt->tgt_statec_cause = cause;
5383 			ptgt->tgt_tmp_cnt = 1;
5384 			ptgt->tgt_done = 0;
5385 			mutex_exit(&ptgt->tgt_mutex);
5386 		}
5387 	}
5388 }
5389 
5390 
5391 static void
5392 fcp_offline_all(struct fcp_port *pptr, int lcount, int cause)
5393 {
5394 	int i;
5395 	int ndevs;
5396 	struct fcp_tgt *ptgt;
5397 
5398 	ASSERT(mutex_owned(&pptr->port_mutex));
5399 
5400 	for (ndevs = 0, i = 0; i < FCP_NUM_HASH; i++) {
5401 		for (ptgt = pptr->port_tgt_hash_table[i]; ptgt != NULL;
5402 		    ptgt = ptgt->tgt_next) {
5403 			ndevs++;
5404 		}
5405 	}
5406 
5407 	if (ndevs == 0) {
5408 		return;
5409 	}
5410 	pptr->port_tmp_cnt = ndevs;
5411 
5412 	for (i = 0; i < FCP_NUM_HASH; i++) {
5413 		for (ptgt = pptr->port_tgt_hash_table[i]; ptgt != NULL;
5414 		    ptgt = ptgt->tgt_next) {
5415 			(void) fcp_call_finish_init_held(pptr, ptgt,
5416 			    lcount, ptgt->tgt_change_cnt, cause);
5417 		}
5418 	}
5419 }
5420 
5421 /*
5422  *     Function: fcp_update_tgt_state
5423  *
5424  *  Description: This function updates the field tgt_state of a target.	 That
5425  *		 field is a bitmap and which bit can be set or reset
5426  *		 individually.	The action applied to the target state is also
5427  *		 applied to all the LUNs belonging to the target (provided the
5428  *		 LUN is not offline).  A side effect of applying the state
5429  *		 modification to the target and the LUNs is the field tgt_trace
5430  *		 of the target and lun_trace of the LUNs is set to zero.
5431  *
5432  *
5433  *     Argument: *ptgt	Target structure.
5434  *		 flag	Flag indication what action to apply (set/reset).
5435  *		 state	State bits to update.
5436  *
5437  * Return Value: None
5438  *
5439  *	Context: Interrupt, Kernel or User context.
5440  *		 The mutex of the target (ptgt->tgt_mutex) must be owned when
5441  *		 calling this function.
5442  */
5443 void
5444 fcp_update_tgt_state(struct fcp_tgt *ptgt, int flag, uint32_t state)
5445 {
5446 	struct fcp_lun *plun;
5447 
5448 	ASSERT(mutex_owned(&ptgt->tgt_mutex));
5449 
5450 	if (!(ptgt->tgt_state & FCP_TGT_OFFLINE)) {
5451 		/* The target is not offline. */
5452 		if (flag == FCP_SET) {
5453 			ptgt->tgt_state |= state;
5454 			ptgt->tgt_trace = 0;
5455 		} else {
5456 			ptgt->tgt_state &= ~state;
5457 		}
5458 
5459 		for (plun = ptgt->tgt_lun; plun != NULL;
5460 		    plun = plun->lun_next) {
5461 			if (!(plun->lun_state & FCP_LUN_OFFLINE)) {
5462 				/* The LUN is not offline. */
5463 				if (flag == FCP_SET) {
5464 					plun->lun_state |= state;
5465 					plun->lun_trace = 0;
5466 				} else {
5467 					plun->lun_state &= ~state;
5468 				}
5469 			}
5470 		}
5471 	}
5472 }
5473 
5474 /*
5475  *     Function: fcp_update_tgt_state
5476  *
5477  *  Description: This function updates the field lun_state of a LUN.  That
5478  *		 field is a bitmap and which bit can be set or reset
5479  *		 individually.
5480  *
5481  *     Argument: *plun	LUN structure.
5482  *		 flag	Flag indication what action to apply (set/reset).
5483  *		 state	State bits to update.
5484  *
5485  * Return Value: None
5486  *
5487  *	Context: Interrupt, Kernel or User context.
5488  *		 The mutex of the target (ptgt->tgt_mutex) must be owned when
5489  *		 calling this function.
5490  */
5491 void
5492 fcp_update_lun_state(struct fcp_lun *plun, int flag, uint32_t state)
5493 {
5494 	struct fcp_tgt	*ptgt = plun->lun_tgt;
5495 
5496 	ASSERT(mutex_owned(&ptgt->tgt_mutex));
5497 
5498 	if (!(plun->lun_state & FCP_TGT_OFFLINE)) {
5499 		if (flag == FCP_SET) {
5500 			plun->lun_state |= state;
5501 		} else {
5502 			plun->lun_state &= ~state;
5503 		}
5504 	}
5505 }
5506 
5507 /*
5508  *     Function: fcp_get_port
5509  *
5510  *  Description: This function returns the fcp_port structure from the opaque
5511  *		 handle passed by the caller.  That opaque handle is the handle
5512  *		 used by fp/fctl to identify a particular local port.  That
5513  *		 handle has been stored in the corresponding fcp_port
5514  *		 structure.  This function is going to walk the global list of
5515  *		 fcp_port structures till one has a port_fp_handle that matches
5516  *		 the handle passed by the caller.  This function enters the
5517  *		 mutex fcp_global_mutex while walking the global list and then
5518  *		 releases it.
5519  *
5520  *     Argument: port_handle	Opaque handle that fp/fctl uses to identify a
5521  *				particular port.
5522  *
5523  * Return Value: NULL		Not found.
5524  *		 Not NULL	Pointer to the fcp_port structure.
5525  *
5526  *	Context: Interrupt, Kernel or User context.
5527  */
5528 static struct fcp_port *
5529 fcp_get_port(opaque_t port_handle)
5530 {
5531 	struct fcp_port *pptr;
5532 
5533 	ASSERT(port_handle != NULL);
5534 
5535 	mutex_enter(&fcp_global_mutex);
5536 	for (pptr = fcp_port_head; pptr != NULL; pptr = pptr->port_next) {
5537 		if (pptr->port_fp_handle == port_handle) {
5538 			break;
5539 		}
5540 	}
5541 	mutex_exit(&fcp_global_mutex);
5542 
5543 	return (pptr);
5544 }
5545 
5546 
5547 static void
5548 fcp_unsol_callback(fc_packet_t *fpkt)
5549 {
5550 	struct fcp_ipkt *icmd = (struct fcp_ipkt *)fpkt->pkt_ulp_private;
5551 	struct fcp_port *pptr = icmd->ipkt_port;
5552 
5553 	if (fpkt->pkt_state != FC_PKT_SUCCESS) {
5554 		caddr_t state, reason, action, expln;
5555 
5556 		(void) fc_ulp_pkt_error(fpkt, &state, &reason,
5557 		    &action, &expln);
5558 
5559 		fcp_log(CE_WARN, pptr->port_dip,
5560 		    "!couldn't post response to unsolicited request: "
5561 		    " state=%s reason=%s rx_id=%x ox_id=%x",
5562 		    state, reason, fpkt->pkt_cmd_fhdr.ox_id,
5563 		    fpkt->pkt_cmd_fhdr.rx_id);
5564 	}
5565 	fcp_icmd_free(pptr, icmd);
5566 }
5567 
5568 
5569 /*
5570  * Perform general purpose preparation of a response to an unsolicited request
5571  */
5572 static void
5573 fcp_unsol_resp_init(fc_packet_t *pkt, fc_unsol_buf_t *buf,
5574     uchar_t r_ctl, uchar_t type)
5575 {
5576 	pkt->pkt_cmd_fhdr.r_ctl = r_ctl;
5577 	pkt->pkt_cmd_fhdr.d_id = buf->ub_frame.s_id;
5578 	pkt->pkt_cmd_fhdr.s_id = buf->ub_frame.d_id;
5579 	pkt->pkt_cmd_fhdr.type = type;
5580 	pkt->pkt_cmd_fhdr.f_ctl = F_CTL_LAST_SEQ | F_CTL_XCHG_CONTEXT;
5581 	pkt->pkt_cmd_fhdr.seq_id = buf->ub_frame.seq_id;
5582 	pkt->pkt_cmd_fhdr.df_ctl  = buf->ub_frame.df_ctl;
5583 	pkt->pkt_cmd_fhdr.seq_cnt = buf->ub_frame.seq_cnt;
5584 	pkt->pkt_cmd_fhdr.ox_id = buf->ub_frame.ox_id;
5585 	pkt->pkt_cmd_fhdr.rx_id = buf->ub_frame.rx_id;
5586 	pkt->pkt_cmd_fhdr.ro = 0;
5587 	pkt->pkt_cmd_fhdr.rsvd = 0;
5588 	pkt->pkt_comp = fcp_unsol_callback;
5589 	pkt->pkt_pd = NULL;
5590 	pkt->pkt_ub_resp_token = (opaque_t)buf;
5591 }
5592 
5593 
5594 /*ARGSUSED*/
5595 static int
5596 fcp_unsol_prli(struct fcp_port *pptr, fc_unsol_buf_t *buf)
5597 {
5598 	fc_packet_t		*fpkt;
5599 	struct la_els_prli	prli;
5600 	struct fcp_prli		*fprli;
5601 	struct fcp_ipkt	*icmd;
5602 	struct la_els_prli	*from;
5603 	struct fcp_prli		*orig;
5604 	struct fcp_tgt	*ptgt;
5605 	int			tcount = 0;
5606 	int			lcount;
5607 
5608 	from = (struct la_els_prli *)buf->ub_buffer;
5609 	orig = (struct fcp_prli *)from->service_params;
5610 	if ((ptgt = fcp_get_target_by_did(pptr, buf->ub_frame.s_id)) !=
5611 	    NULL) {
5612 		mutex_enter(&ptgt->tgt_mutex);
5613 		tcount = ptgt->tgt_change_cnt;
5614 		mutex_exit(&ptgt->tgt_mutex);
5615 	}
5616 
5617 	mutex_enter(&pptr->port_mutex);
5618 	lcount = pptr->port_link_cnt;
5619 	mutex_exit(&pptr->port_mutex);
5620 
5621 	if ((icmd = fcp_icmd_alloc(pptr, ptgt, sizeof (la_els_prli_t),
5622 	    sizeof (la_els_prli_t), 0,
5623 	    pptr->port_state & FCP_STATE_FCA_IS_NODMA,
5624 	    lcount, tcount, 0, FC_INVALID_RSCN_COUNT)) == NULL) {
5625 		return (FC_FAILURE);
5626 	}
5627 
5628 	fpkt = icmd->ipkt_fpkt;
5629 	fpkt->pkt_tran_flags = FC_TRAN_CLASS3 | FC_TRAN_INTR;
5630 	fpkt->pkt_tran_type = FC_PKT_OUTBOUND;
5631 	fpkt->pkt_timeout = FCP_ELS_TIMEOUT;
5632 	fpkt->pkt_cmdlen = sizeof (la_els_prli_t);
5633 	fpkt->pkt_rsplen = 0;
5634 	fpkt->pkt_datalen = 0;
5635 
5636 	icmd->ipkt_opcode = LA_ELS_PRLI;
5637 
5638 	bzero(&prli, sizeof (struct la_els_prli));
5639 	fprli = (struct fcp_prli *)prli.service_params;
5640 	prli.ls_code = LA_ELS_ACC;
5641 	prli.page_length = 0x10;
5642 	prli.payload_length = sizeof (struct la_els_prli);
5643 
5644 	/* fill in service params */
5645 	fprli->type = 0x08;
5646 	fprli->resvd1 = 0;
5647 	fprli->orig_process_assoc_valid = orig->orig_process_assoc_valid;
5648 	fprli->orig_process_associator = orig->orig_process_associator;
5649 	fprli->resp_process_assoc_valid = 0;
5650 	fprli->establish_image_pair = 1;
5651 	fprli->resvd2 = 0;
5652 	fprli->resvd3 = 0;
5653 	fprli->obsolete_1 = 0;
5654 	fprli->obsolete_2 = 0;
5655 	fprli->data_overlay_allowed = 0;
5656 	fprli->initiator_fn = 1;
5657 	fprli->confirmed_compl_allowed = 1;
5658 
5659 	if (fc_ulp_is_name_present("ltct") == FC_SUCCESS) {
5660 		fprli->target_fn = 1;
5661 	} else {
5662 		fprli->target_fn = 0;
5663 	}
5664 
5665 	fprli->retry = 1;
5666 	fprli->read_xfer_rdy_disabled = 1;
5667 	fprli->write_xfer_rdy_disabled = 0;
5668 
5669 	/* save the unsol prli payload first */
5670 	FCP_CP_OUT((uint8_t *)from, fpkt->pkt_resp,
5671 	    fpkt->pkt_resp_acc, sizeof (struct la_els_prli));
5672 
5673 	FCP_CP_OUT((uint8_t *)&prli, fpkt->pkt_cmd,
5674 	    fpkt->pkt_cmd_acc, sizeof (struct la_els_prli));
5675 
5676 	fcp_unsol_resp_init(fpkt, buf, R_CTL_ELS_RSP, FC_TYPE_EXTENDED_LS);
5677 
5678 	mutex_enter(&pptr->port_mutex);
5679 	if (!FCP_LINK_STATE_CHANGED(pptr, icmd)) {
5680 		int rval;
5681 		mutex_exit(&pptr->port_mutex);
5682 
5683 		if ((rval = fc_ulp_issue_els(pptr->port_fp_handle, fpkt)) !=
5684 		    FC_SUCCESS) {
5685 			if ((rval == FC_STATEC_BUSY || rval == FC_OFFLINE) &&
5686 			    ptgt != NULL) {
5687 				fcp_queue_ipkt(pptr, fpkt);
5688 				return (FC_SUCCESS);
5689 			}
5690 			/* Let it timeout */
5691 			fcp_icmd_free(pptr, icmd);
5692 			return (FC_FAILURE);
5693 		}
5694 	} else {
5695 		mutex_exit(&pptr->port_mutex);
5696 		fcp_icmd_free(pptr, icmd);
5697 		return (FC_FAILURE);
5698 	}
5699 
5700 	(void) fc_ulp_ubrelease(pptr->port_fp_handle, 1, &buf->ub_token);
5701 
5702 	return (FC_SUCCESS);
5703 }
5704 
5705 /*
5706  *     Function: fcp_icmd_alloc
5707  *
5708  *  Description: This function allocated a fcp_ipkt structure.	The pkt_comp
5709  *		 field is initialized to fcp_icmd_callback.  Sometimes it is
5710  *		 modified by the caller (such as fcp_send_scsi).  The
5711  *		 structure is also tied to the state of the line and of the
5712  *		 target at a particular time.  That link is established by
5713  *		 setting the fields ipkt_link_cnt and ipkt_change_cnt to lcount
5714  *		 and tcount which came respectively from pptr->link_cnt and
5715  *		 ptgt->tgt_change_cnt.
5716  *
5717  *     Argument: *pptr		Fcp port.
5718  *		 *ptgt		Target (destination of the command).
5719  *		 cmd_len	Length of the command.
5720  *		 resp_len	Length of the expected response.
5721  *		 data_len	Length of the data.
5722  *		 nodma		Indicates weither the command and response.
5723  *				will be transfer through DMA or not.
5724  *		 lcount		Link state change counter.
5725  *		 tcount		Target state change counter.
5726  *		 cause		Reason that lead to this call.
5727  *
5728  * Return Value: NULL		Failed.
5729  *		 Not NULL	Internal packet address.
5730  */
5731 static struct fcp_ipkt *
5732 fcp_icmd_alloc(struct fcp_port *pptr, struct fcp_tgt *ptgt, int cmd_len,
5733     int resp_len, int data_len, int nodma, int lcount, int tcount, int cause,
5734     uint32_t rscn_count)
5735 {
5736 	int			dma_setup = 0;
5737 	fc_packet_t		*fpkt;
5738 	struct fcp_ipkt	*icmd = NULL;
5739 
5740 	icmd = kmem_zalloc(sizeof (struct fcp_ipkt) +
5741 	    pptr->port_dmacookie_sz + pptr->port_priv_pkt_len,
5742 	    KM_NOSLEEP);
5743 	if (icmd == NULL) {
5744 		fcp_log(CE_WARN, pptr->port_dip,
5745 		    "!internal packet allocation failed");
5746 		return (NULL);
5747 	}
5748 
5749 	/*
5750 	 * initialize the allocated packet
5751 	 */
5752 	icmd->ipkt_nodma = nodma;
5753 	icmd->ipkt_next = icmd->ipkt_prev = NULL;
5754 	icmd->ipkt_lun = NULL;
5755 
5756 	icmd->ipkt_link_cnt = lcount;
5757 	icmd->ipkt_change_cnt = tcount;
5758 	icmd->ipkt_cause = cause;
5759 
5760 	mutex_enter(&pptr->port_mutex);
5761 	icmd->ipkt_port = pptr;
5762 	mutex_exit(&pptr->port_mutex);
5763 
5764 	/* keep track of amt of data to be sent in pkt */
5765 	icmd->ipkt_cmdlen = cmd_len;
5766 	icmd->ipkt_resplen = resp_len;
5767 	icmd->ipkt_datalen = data_len;
5768 
5769 	/* set up pkt's ptr to the fc_packet_t struct, just after the ipkt */
5770 	icmd->ipkt_fpkt = (fc_packet_t *)(&icmd->ipkt_fc_packet);
5771 
5772 	/* set pkt's private ptr to point to cmd pkt */
5773 	icmd->ipkt_fpkt->pkt_ulp_private = (opaque_t)icmd;
5774 
5775 	/* set FCA private ptr to memory just beyond */
5776 	icmd->ipkt_fpkt->pkt_fca_private = (opaque_t)
5777 	    ((char *)icmd + sizeof (struct fcp_ipkt) +
5778 	    pptr->port_dmacookie_sz);
5779 
5780 	/* get ptr to fpkt substruct and fill it in */
5781 	fpkt = icmd->ipkt_fpkt;
5782 	fpkt->pkt_data_cookie = (ddi_dma_cookie_t *)((caddr_t)icmd +
5783 	    sizeof (struct fcp_ipkt));
5784 
5785 	if (ptgt != NULL) {
5786 		icmd->ipkt_tgt = ptgt;
5787 		fpkt->pkt_fca_device = ptgt->tgt_fca_dev;
5788 	}
5789 
5790 	fpkt->pkt_comp = fcp_icmd_callback;
5791 	fpkt->pkt_tran_flags = (FC_TRAN_CLASS3 | FC_TRAN_INTR);
5792 	fpkt->pkt_cmdlen = cmd_len;
5793 	fpkt->pkt_rsplen = resp_len;
5794 	fpkt->pkt_datalen = data_len;
5795 
5796 	/*
5797 	 * The pkt_ulp_rscn_infop (aka pkt_ulp_rsvd1) field is used to pass the
5798 	 * rscn_count as fcp knows down to the transport. If a valid count was
5799 	 * passed into this function, we allocate memory to actually pass down
5800 	 * this info.
5801 	 *
5802 	 * BTW, if the kmem_zalloc fails, we won't try too hard. This will
5803 	 * basically mean that fcp will not be able to help transport
5804 	 * distinguish if a new RSCN has come after fcp was last informed about
5805 	 * it. In such cases, it might lead to the problem mentioned in CR/bug #
5806 	 * 5068068 where the device might end up going offline in case of RSCN
5807 	 * storms.
5808 	 */
5809 	fpkt->pkt_ulp_rscn_infop = NULL;
5810 	if (rscn_count != FC_INVALID_RSCN_COUNT) {
5811 		fpkt->pkt_ulp_rscn_infop = kmem_zalloc(
5812 		    sizeof (fc_ulp_rscn_info_t), KM_NOSLEEP);
5813 		if (fpkt->pkt_ulp_rscn_infop == NULL) {
5814 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
5815 			    fcp_trace, FCP_BUF_LEVEL_6, 0,
5816 			    "Failed to alloc memory to pass rscn info");
5817 		}
5818 	}
5819 
5820 	if (fpkt->pkt_ulp_rscn_infop != NULL) {
5821 		fc_ulp_rscn_info_t	*rscnp;
5822 
5823 		rscnp = (fc_ulp_rscn_info_t *)fpkt->pkt_ulp_rscn_infop;
5824 		rscnp->ulp_rscn_count = rscn_count;
5825 	}
5826 
5827 	if (fcp_alloc_dma(pptr, icmd, nodma, KM_NOSLEEP) != FC_SUCCESS) {
5828 		goto fail;
5829 	}
5830 	dma_setup++;
5831 
5832 	/*
5833 	 * Must hold target mutex across setting of pkt_pd and call to
5834 	 * fc_ulp_init_packet to ensure the handle to the target doesn't go
5835 	 * away while we're not looking.
5836 	 */
5837 	if (ptgt != NULL) {
5838 		mutex_enter(&ptgt->tgt_mutex);
5839 		fpkt->pkt_pd = ptgt->tgt_pd_handle;
5840 
5841 		/* ask transport to do its initialization on this pkt */
5842 		if (fc_ulp_init_packet(pptr->port_fp_handle, fpkt, KM_NOSLEEP)
5843 		    != FC_SUCCESS) {
5844 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
5845 			    fcp_trace, FCP_BUF_LEVEL_6, 0,
5846 			    "fc_ulp_init_packet failed");
5847 			mutex_exit(&ptgt->tgt_mutex);
5848 			goto fail;
5849 		}
5850 		mutex_exit(&ptgt->tgt_mutex);
5851 	} else {
5852 		if (fc_ulp_init_packet(pptr->port_fp_handle, fpkt, KM_NOSLEEP)
5853 		    != FC_SUCCESS) {
5854 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
5855 			    fcp_trace, FCP_BUF_LEVEL_6, 0,
5856 			    "fc_ulp_init_packet failed");
5857 			goto fail;
5858 		}
5859 	}
5860 
5861 	mutex_enter(&pptr->port_mutex);
5862 	if (pptr->port_state & (FCP_STATE_DETACHING |
5863 	    FCP_STATE_SUSPENDED | FCP_STATE_POWER_DOWN)) {
5864 		int rval;
5865 
5866 		mutex_exit(&pptr->port_mutex);
5867 
5868 		rval = fc_ulp_uninit_packet(pptr->port_fp_handle, fpkt);
5869 		ASSERT(rval == FC_SUCCESS);
5870 
5871 		goto fail;
5872 	}
5873 
5874 	if (ptgt != NULL) {
5875 		mutex_enter(&ptgt->tgt_mutex);
5876 		ptgt->tgt_ipkt_cnt++;
5877 		mutex_exit(&ptgt->tgt_mutex);
5878 	}
5879 
5880 	pptr->port_ipkt_cnt++;
5881 
5882 	mutex_exit(&pptr->port_mutex);
5883 
5884 	return (icmd);
5885 
5886 fail:
5887 	if (fpkt->pkt_ulp_rscn_infop != NULL) {
5888 		kmem_free(fpkt->pkt_ulp_rscn_infop,
5889 		    sizeof (fc_ulp_rscn_info_t));
5890 		fpkt->pkt_ulp_rscn_infop = NULL;
5891 	}
5892 
5893 	if (dma_setup) {
5894 		fcp_free_dma(pptr, icmd);
5895 	}
5896 	kmem_free(icmd, sizeof (struct fcp_ipkt) + pptr->port_priv_pkt_len +
5897 	    (size_t)pptr->port_dmacookie_sz);
5898 
5899 	return (NULL);
5900 }
5901 
5902 /*
5903  *     Function: fcp_icmd_free
5904  *
5905  *  Description: Frees the internal command passed by the caller.
5906  *
5907  *     Argument: *pptr		Fcp port.
5908  *		 *icmd		Internal packet to free.
5909  *
5910  * Return Value: None
5911  */
5912 static void
5913 fcp_icmd_free(struct fcp_port *pptr, struct fcp_ipkt *icmd)
5914 {
5915 	struct fcp_tgt	*ptgt = icmd->ipkt_tgt;
5916 
5917 	/* Let the underlying layers do their cleanup. */
5918 	(void) fc_ulp_uninit_packet(pptr->port_fp_handle,
5919 	    icmd->ipkt_fpkt);
5920 
5921 	if (icmd->ipkt_fpkt->pkt_ulp_rscn_infop) {
5922 		kmem_free(icmd->ipkt_fpkt->pkt_ulp_rscn_infop,
5923 		    sizeof (fc_ulp_rscn_info_t));
5924 	}
5925 
5926 	fcp_free_dma(pptr, icmd);
5927 
5928 	kmem_free(icmd, sizeof (struct fcp_ipkt) + pptr->port_priv_pkt_len +
5929 	    (size_t)pptr->port_dmacookie_sz);
5930 
5931 	mutex_enter(&pptr->port_mutex);
5932 
5933 	if (ptgt) {
5934 		mutex_enter(&ptgt->tgt_mutex);
5935 		ptgt->tgt_ipkt_cnt--;
5936 		mutex_exit(&ptgt->tgt_mutex);
5937 	}
5938 
5939 	pptr->port_ipkt_cnt--;
5940 	mutex_exit(&pptr->port_mutex);
5941 }
5942 
5943 /*
5944  *     Function: fcp_alloc_dma
5945  *
5946  *  Description: Allocated the DMA resources required for the internal
5947  *		 packet.
5948  *
5949  *     Argument: *pptr	FCP port.
5950  *		 *icmd	Internal FCP packet.
5951  *		 nodma	Indicates if the Cmd and Resp will be DMAed.
5952  *		 flags	Allocation flags (Sleep or NoSleep).
5953  *
5954  * Return Value: FC_SUCCESS
5955  *		 FC_NOMEM
5956  */
5957 static int
5958 fcp_alloc_dma(struct fcp_port *pptr, struct fcp_ipkt *icmd,
5959     int nodma, int flags)
5960 {
5961 	int		rval;
5962 	size_t		real_size;
5963 	uint_t		ccount;
5964 	int		bound = 0;
5965 	int		cmd_resp = 0;
5966 	fc_packet_t	*fpkt;
5967 	ddi_dma_cookie_t	pkt_data_cookie;
5968 	ddi_dma_cookie_t	*cp;
5969 	uint32_t		cnt;
5970 
5971 	fpkt = &icmd->ipkt_fc_packet;
5972 
5973 	ASSERT(fpkt->pkt_cmd_dma == NULL && fpkt->pkt_data_dma == NULL &&
5974 	    fpkt->pkt_resp_dma == NULL);
5975 
5976 	icmd->ipkt_nodma = nodma;
5977 
5978 	if (nodma) {
5979 		fpkt->pkt_cmd = kmem_zalloc(fpkt->pkt_cmdlen, flags);
5980 		if (fpkt->pkt_cmd == NULL) {
5981 			goto fail;
5982 		}
5983 
5984 		fpkt->pkt_resp = kmem_zalloc(fpkt->pkt_rsplen, flags);
5985 		if (fpkt->pkt_resp == NULL) {
5986 			goto fail;
5987 		}
5988 	} else {
5989 		ASSERT(fpkt->pkt_cmdlen && fpkt->pkt_rsplen);
5990 
5991 		rval = fcp_alloc_cmd_resp(pptr, fpkt, flags);
5992 		if (rval == FC_FAILURE) {
5993 			ASSERT(fpkt->pkt_cmd_dma == NULL &&
5994 			    fpkt->pkt_resp_dma == NULL);
5995 			goto fail;
5996 		}
5997 		cmd_resp++;
5998 	}
5999 
6000 	if ((fpkt->pkt_datalen != 0) &&
6001 	    !(pptr->port_state & FCP_STATE_FCA_IS_NODMA)) {
6002 		/*
6003 		 * set up DMA handle and memory for the data in this packet
6004 		 */
6005 		if (ddi_dma_alloc_handle(pptr->port_dip,
6006 		    &pptr->port_data_dma_attr, DDI_DMA_DONTWAIT,
6007 		    NULL, &fpkt->pkt_data_dma) != DDI_SUCCESS) {
6008 			goto fail;
6009 		}
6010 
6011 		if (ddi_dma_mem_alloc(fpkt->pkt_data_dma, fpkt->pkt_datalen,
6012 		    &pptr->port_dma_acc_attr, DDI_DMA_CONSISTENT,
6013 		    DDI_DMA_DONTWAIT, NULL, &fpkt->pkt_data,
6014 		    &real_size, &fpkt->pkt_data_acc) != DDI_SUCCESS) {
6015 			goto fail;
6016 		}
6017 
6018 		/* was DMA mem size gotten < size asked for/needed ?? */
6019 		if (real_size < fpkt->pkt_datalen) {
6020 			goto fail;
6021 		}
6022 
6023 		/* bind DMA address and handle together */
6024 		if (ddi_dma_addr_bind_handle(fpkt->pkt_data_dma,
6025 		    NULL, fpkt->pkt_data, real_size, DDI_DMA_READ |
6026 		    DDI_DMA_CONSISTENT, DDI_DMA_DONTWAIT, NULL,
6027 		    &pkt_data_cookie, &ccount) != DDI_DMA_MAPPED) {
6028 			goto fail;
6029 		}
6030 		bound++;
6031 
6032 		if (ccount > pptr->port_data_dma_attr.dma_attr_sgllen) {
6033 			goto fail;
6034 		}
6035 
6036 		fpkt->pkt_data_cookie_cnt = ccount;
6037 
6038 		cp = fpkt->pkt_data_cookie;
6039 		*cp = pkt_data_cookie;
6040 		cp++;
6041 
6042 		for (cnt = 1; cnt < ccount; cnt++, cp++) {
6043 			ddi_dma_nextcookie(fpkt->pkt_data_dma,
6044 			    &pkt_data_cookie);
6045 			*cp = pkt_data_cookie;
6046 		}
6047 
6048 	} else if (fpkt->pkt_datalen != 0) {
6049 		/*
6050 		 * If it's a pseudo FCA, then it can't support DMA even in
6051 		 * SCSI data phase.
6052 		 */
6053 		fpkt->pkt_data = kmem_alloc(fpkt->pkt_datalen, flags);
6054 		if (fpkt->pkt_data == NULL) {
6055 			goto fail;
6056 		}
6057 
6058 	}
6059 
6060 	return (FC_SUCCESS);
6061 
6062 fail:
6063 	if (bound) {
6064 		(void) ddi_dma_unbind_handle(fpkt->pkt_data_dma);
6065 	}
6066 
6067 	if (fpkt->pkt_data_dma) {
6068 		if (fpkt->pkt_data) {
6069 			ddi_dma_mem_free(&fpkt->pkt_data_acc);
6070 		}
6071 		ddi_dma_free_handle(&fpkt->pkt_data_dma);
6072 	} else {
6073 		if (fpkt->pkt_data) {
6074 			kmem_free(fpkt->pkt_data, fpkt->pkt_datalen);
6075 		}
6076 	}
6077 
6078 	if (nodma) {
6079 		if (fpkt->pkt_cmd) {
6080 			kmem_free(fpkt->pkt_cmd, fpkt->pkt_cmdlen);
6081 		}
6082 		if (fpkt->pkt_resp) {
6083 			kmem_free(fpkt->pkt_resp, fpkt->pkt_rsplen);
6084 		}
6085 	} else {
6086 		if (cmd_resp) {
6087 			fcp_free_cmd_resp(pptr, fpkt);
6088 		}
6089 	}
6090 
6091 	return (FC_NOMEM);
6092 }
6093 
6094 
6095 static void
6096 fcp_free_dma(struct fcp_port *pptr, struct fcp_ipkt *icmd)
6097 {
6098 	fc_packet_t *fpkt = icmd->ipkt_fpkt;
6099 
6100 	if (fpkt->pkt_data_dma) {
6101 		(void) ddi_dma_unbind_handle(fpkt->pkt_data_dma);
6102 		if (fpkt->pkt_data) {
6103 			ddi_dma_mem_free(&fpkt->pkt_data_acc);
6104 		}
6105 		ddi_dma_free_handle(&fpkt->pkt_data_dma);
6106 	} else {
6107 		if (fpkt->pkt_data) {
6108 			kmem_free(fpkt->pkt_data, fpkt->pkt_datalen);
6109 		}
6110 		/*
6111 		 * Need we reset pkt_* to zero???
6112 		 */
6113 	}
6114 
6115 	if (icmd->ipkt_nodma) {
6116 		if (fpkt->pkt_cmd) {
6117 			kmem_free(fpkt->pkt_cmd, icmd->ipkt_cmdlen);
6118 		}
6119 		if (fpkt->pkt_resp) {
6120 			kmem_free(fpkt->pkt_resp, icmd->ipkt_resplen);
6121 		}
6122 	} else {
6123 		ASSERT(fpkt->pkt_resp_dma != NULL && fpkt->pkt_cmd_dma != NULL);
6124 
6125 		fcp_free_cmd_resp(pptr, fpkt);
6126 	}
6127 }
6128 
6129 /*
6130  *     Function: fcp_lookup_target
6131  *
6132  *  Description: Finds a target given a WWN.
6133  *
6134  *     Argument: *pptr	FCP port.
6135  *		 *wwn	World Wide Name of the device to look for.
6136  *
6137  * Return Value: NULL		No target found
6138  *		 Not NULL	Target structure
6139  *
6140  *	Context: Interrupt context.
6141  *		 The mutex pptr->port_mutex must be owned.
6142  */
6143 /* ARGSUSED */
6144 static struct fcp_tgt *
6145 fcp_lookup_target(struct fcp_port *pptr, uchar_t *wwn)
6146 {
6147 	int			hash;
6148 	struct fcp_tgt	*ptgt;
6149 
6150 	ASSERT(mutex_owned(&pptr->port_mutex));
6151 
6152 	hash = FCP_HASH(wwn);
6153 
6154 	for (ptgt = pptr->port_tgt_hash_table[hash]; ptgt != NULL;
6155 	    ptgt = ptgt->tgt_next) {
6156 		if (!(ptgt->tgt_state & FCP_TGT_ORPHAN) &&
6157 		    bcmp((caddr_t)wwn, (caddr_t)&ptgt->tgt_port_wwn.raw_wwn[0],
6158 		    sizeof (ptgt->tgt_port_wwn)) == 0) {
6159 			break;
6160 		}
6161 	}
6162 
6163 	return (ptgt);
6164 }
6165 
6166 
6167 /*
6168  * Find target structure given a port identifier
6169  */
6170 static struct fcp_tgt *
6171 fcp_get_target_by_did(struct fcp_port *pptr, uint32_t d_id)
6172 {
6173 	fc_portid_t		port_id;
6174 	la_wwn_t		pwwn;
6175 	struct fcp_tgt	*ptgt = NULL;
6176 
6177 	port_id.priv_lilp_posit = 0;
6178 	port_id.port_id = d_id;
6179 	if (fc_ulp_get_pwwn_by_did(pptr->port_fp_handle, port_id,
6180 	    &pwwn) == FC_SUCCESS) {
6181 		mutex_enter(&pptr->port_mutex);
6182 		ptgt = fcp_lookup_target(pptr, pwwn.raw_wwn);
6183 		mutex_exit(&pptr->port_mutex);
6184 	}
6185 
6186 	return (ptgt);
6187 }
6188 
6189 
6190 /*
6191  * the packet completion callback routine for info cmd pkts
6192  *
6193  * this means fpkt pts to a response to either a PLOGI or a PRLI
6194  *
6195  * if there is an error an attempt is made to call a routine to resend
6196  * the command that failed
6197  */
6198 static void
6199 fcp_icmd_callback(fc_packet_t *fpkt)
6200 {
6201 	struct fcp_ipkt	*icmd;
6202 	struct fcp_port	*pptr;
6203 	struct fcp_tgt	*ptgt;
6204 	struct la_els_prli	*prli;
6205 	struct la_els_prli	prli_s;
6206 	struct fcp_prli		*fprli;
6207 	struct fcp_lun	*plun;
6208 	int		free_pkt = 1;
6209 	int		rval;
6210 	ls_code_t	resp;
6211 	uchar_t		prli_acc = 0;
6212 	uint32_t	rscn_count = FC_INVALID_RSCN_COUNT;
6213 	int		lun0_newalloc;
6214 
6215 	icmd = (struct fcp_ipkt *)fpkt->pkt_ulp_private;
6216 
6217 	/* get ptrs to the port and target structs for the cmd */
6218 	pptr = icmd->ipkt_port;
6219 	ptgt = icmd->ipkt_tgt;
6220 
6221 	FCP_CP_IN(fpkt->pkt_resp, &resp, fpkt->pkt_resp_acc, sizeof (resp));
6222 
6223 	if (icmd->ipkt_opcode == LA_ELS_PRLI) {
6224 		FCP_CP_IN(fpkt->pkt_cmd, &prli_s, fpkt->pkt_cmd_acc,
6225 		    sizeof (prli_s));
6226 		prli_acc = (prli_s.ls_code == LA_ELS_ACC);
6227 	}
6228 
6229 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
6230 	    fcp_trace, FCP_BUF_LEVEL_2, 0,
6231 	    "ELS (%x) callback state=0x%x reason=0x%x for %x",
6232 	    icmd->ipkt_opcode, fpkt->pkt_state, fpkt->pkt_reason,
6233 	    ptgt->tgt_d_id);
6234 
6235 	if ((fpkt->pkt_state == FC_PKT_SUCCESS) &&
6236 	    ((resp.ls_code == LA_ELS_ACC) || prli_acc)) {
6237 
6238 		mutex_enter(&ptgt->tgt_mutex);
6239 		if (ptgt->tgt_pd_handle == NULL) {
6240 			/*
6241 			 * in a fabric environment the port device handles
6242 			 * get created only after successful LOGIN into the
6243 			 * transport, so the transport makes this port
6244 			 * device (pd) handle available in this packet, so
6245 			 * save it now
6246 			 */
6247 			ASSERT(fpkt->pkt_pd != NULL);
6248 			ptgt->tgt_pd_handle = fpkt->pkt_pd;
6249 		}
6250 		mutex_exit(&ptgt->tgt_mutex);
6251 
6252 		/* which ELS cmd is this response for ?? */
6253 		switch (icmd->ipkt_opcode) {
6254 		case LA_ELS_PLOGI:
6255 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
6256 			    fcp_trace, FCP_BUF_LEVEL_5, 0,
6257 			    "PLOGI to d_id=0x%x succeeded, wwn=%08x%08x",
6258 			    ptgt->tgt_d_id,
6259 			    *((int *)&ptgt->tgt_port_wwn.raw_wwn[0]),
6260 			    *((int *)&ptgt->tgt_port_wwn.raw_wwn[4]));
6261 
6262 			FCP_TGT_TRACE(ptgt, icmd->ipkt_change_cnt,
6263 			    FCP_TGT_TRACE_15);
6264 
6265 			/* Note that we are not allocating a new icmd */
6266 			if (fcp_send_els(pptr, ptgt, icmd, LA_ELS_PRLI,
6267 			    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
6268 			    icmd->ipkt_cause) != DDI_SUCCESS) {
6269 				FCP_TGT_TRACE(ptgt, icmd->ipkt_change_cnt,
6270 				    FCP_TGT_TRACE_16);
6271 				goto fail;
6272 			}
6273 			break;
6274 
6275 		case LA_ELS_PRLI:
6276 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
6277 			    fcp_trace, FCP_BUF_LEVEL_5, 0,
6278 			    "PRLI to d_id=0x%x succeeded", ptgt->tgt_d_id);
6279 
6280 			FCP_TGT_TRACE(ptgt, icmd->ipkt_change_cnt,
6281 			    FCP_TGT_TRACE_17);
6282 
6283 			prli = &prli_s;
6284 
6285 			FCP_CP_IN(fpkt->pkt_resp, prli, fpkt->pkt_resp_acc,
6286 			    sizeof (prli_s));
6287 
6288 			fprli = (struct fcp_prli *)prli->service_params;
6289 
6290 			mutex_enter(&ptgt->tgt_mutex);
6291 			ptgt->tgt_icap = fprli->initiator_fn;
6292 			ptgt->tgt_tcap = fprli->target_fn;
6293 			mutex_exit(&ptgt->tgt_mutex);
6294 
6295 			if ((fprli->type != 0x08) || (fprli->target_fn != 1)) {
6296 				/*
6297 				 * this FCP device does not support target mode
6298 				 */
6299 				FCP_TGT_TRACE(ptgt, icmd->ipkt_change_cnt,
6300 				    FCP_TGT_TRACE_18);
6301 				goto fail;
6302 			}
6303 			if (fprli->retry == 1) {
6304 				fc_ulp_disable_relogin(pptr->port_fp_handle,
6305 				    &ptgt->tgt_port_wwn);
6306 			}
6307 
6308 			/* target is no longer offline */
6309 			mutex_enter(&pptr->port_mutex);
6310 			mutex_enter(&ptgt->tgt_mutex);
6311 			if (!FCP_TGT_STATE_CHANGED(ptgt, icmd)) {
6312 				ptgt->tgt_state &= ~(FCP_TGT_OFFLINE |
6313 				    FCP_TGT_MARK);
6314 			} else {
6315 				FCP_TRACE(fcp_logq, pptr->port_instbuf,
6316 				    fcp_trace, FCP_BUF_LEVEL_2, 0,
6317 				    "fcp_icmd_callback,1: state change "
6318 				    " occured for D_ID=0x%x", ptgt->tgt_d_id);
6319 				mutex_exit(&ptgt->tgt_mutex);
6320 				mutex_exit(&pptr->port_mutex);
6321 				goto fail;
6322 			}
6323 			mutex_exit(&ptgt->tgt_mutex);
6324 			mutex_exit(&pptr->port_mutex);
6325 
6326 			/*
6327 			 * lun 0 should always respond to inquiry, so
6328 			 * get the LUN struct for LUN 0
6329 			 *
6330 			 * Currently we deal with first level of addressing.
6331 			 * If / when we start supporting 0x device types
6332 			 * (DTYPE_ARRAY_CTRL, i.e. array controllers)
6333 			 * this logic will need revisiting.
6334 			 */
6335 			lun0_newalloc = 0;
6336 			if ((plun = fcp_get_lun(ptgt, 0)) == NULL) {
6337 				/*
6338 				 * no LUN struct for LUN 0 yet exists,
6339 				 * so create one
6340 				 */
6341 				plun = fcp_alloc_lun(ptgt);
6342 				if (plun == NULL) {
6343 					fcp_log(CE_WARN, pptr->port_dip,
6344 					    "!Failed to allocate lun 0 for"
6345 					    " D_ID=%x", ptgt->tgt_d_id);
6346 					goto fail;
6347 				}
6348 				lun0_newalloc = 1;
6349 			}
6350 
6351 			/* fill in LUN info */
6352 			mutex_enter(&ptgt->tgt_mutex);
6353 			/*
6354 			 * consider lun 0 as device not connected if it is
6355 			 * offlined or newly allocated
6356 			 */
6357 			if ((plun->lun_state & FCP_LUN_OFFLINE) ||
6358 			    lun0_newalloc) {
6359 				plun->lun_state |= FCP_LUN_DEVICE_NOT_CONNECTED;
6360 			}
6361 			plun->lun_state |= (FCP_LUN_BUSY | FCP_LUN_MARK);
6362 			plun->lun_state &= ~FCP_LUN_OFFLINE;
6363 			ptgt->tgt_lun_cnt = 1;
6364 			ptgt->tgt_report_lun_cnt = 0;
6365 			mutex_exit(&ptgt->tgt_mutex);
6366 
6367 			/* Retrieve the rscn count (if a valid one exists) */
6368 			if (icmd->ipkt_fpkt->pkt_ulp_rscn_infop != NULL) {
6369 				rscn_count = ((fc_ulp_rscn_info_t *)
6370 				    (icmd->ipkt_fpkt->pkt_ulp_rscn_infop))
6371 				    ->ulp_rscn_count;
6372 			} else {
6373 				rscn_count = FC_INVALID_RSCN_COUNT;
6374 			}
6375 
6376 			/* send Report Lun request to target */
6377 			if (fcp_send_scsi(plun, SCMD_REPORT_LUN,
6378 			    sizeof (struct fcp_reportlun_resp),
6379 			    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
6380 			    icmd->ipkt_cause, rscn_count) != DDI_SUCCESS) {
6381 				mutex_enter(&pptr->port_mutex);
6382 				if (!FCP_TGT_STATE_CHANGED(ptgt, icmd)) {
6383 					fcp_log(CE_WARN, pptr->port_dip,
6384 					    "!Failed to send REPORT LUN to"
6385 					    "  D_ID=%x", ptgt->tgt_d_id);
6386 				} else {
6387 					FCP_TRACE(fcp_logq,
6388 					    pptr->port_instbuf, fcp_trace,
6389 					    FCP_BUF_LEVEL_5, 0,
6390 					    "fcp_icmd_callback,2:state change"
6391 					    " occured for D_ID=0x%x",
6392 					    ptgt->tgt_d_id);
6393 				}
6394 				mutex_exit(&pptr->port_mutex);
6395 
6396 				FCP_TGT_TRACE(ptgt, icmd->ipkt_change_cnt,
6397 				    FCP_TGT_TRACE_19);
6398 
6399 				goto fail;
6400 			} else {
6401 				free_pkt = 0;
6402 				fcp_icmd_free(pptr, icmd);
6403 			}
6404 			break;
6405 
6406 		default:
6407 			fcp_log(CE_WARN, pptr->port_dip,
6408 			    "!fcp_icmd_callback Invalid opcode");
6409 			goto fail;
6410 		}
6411 
6412 		return;
6413 	}
6414 
6415 
6416 	/*
6417 	 * Other PLOGI failures are not retried as the
6418 	 * transport does it already
6419 	 */
6420 	if (icmd->ipkt_opcode != LA_ELS_PLOGI) {
6421 		if (fcp_is_retryable(icmd) &&
6422 		    icmd->ipkt_retries++ < FCP_MAX_RETRIES) {
6423 
6424 			if (FCP_MUST_RETRY(fpkt)) {
6425 				fcp_queue_ipkt(pptr, fpkt);
6426 				return;
6427 			}
6428 
6429 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
6430 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
6431 			    "ELS PRLI is retried for d_id=0x%x, state=%x,"
6432 			    " reason= %x", ptgt->tgt_d_id, fpkt->pkt_state,
6433 			    fpkt->pkt_reason);
6434 
6435 			/*
6436 			 * Retry by recalling the routine that
6437 			 * originally queued this packet
6438 			 */
6439 			mutex_enter(&pptr->port_mutex);
6440 			if (!FCP_TGT_STATE_CHANGED(ptgt, icmd)) {
6441 				caddr_t msg;
6442 
6443 				mutex_exit(&pptr->port_mutex);
6444 
6445 				ASSERT(icmd->ipkt_opcode != LA_ELS_PLOGI);
6446 
6447 				if (fpkt->pkt_state == FC_PKT_TIMEOUT) {
6448 					fpkt->pkt_timeout +=
6449 					    FCP_TIMEOUT_DELTA;
6450 				}
6451 
6452 				rval = fc_ulp_issue_els(pptr->port_fp_handle,
6453 				    fpkt);
6454 				if (rval == FC_SUCCESS) {
6455 					return;
6456 				}
6457 
6458 				if (rval == FC_STATEC_BUSY ||
6459 				    rval == FC_OFFLINE) {
6460 					fcp_queue_ipkt(pptr, fpkt);
6461 					return;
6462 				}
6463 				(void) fc_ulp_error(rval, &msg);
6464 
6465 				fcp_log(CE_NOTE, pptr->port_dip,
6466 				    "!ELS 0x%x failed to d_id=0x%x;"
6467 				    " %s", icmd->ipkt_opcode,
6468 				    ptgt->tgt_d_id, msg);
6469 			} else {
6470 				FCP_TRACE(fcp_logq, pptr->port_instbuf,
6471 				    fcp_trace, FCP_BUF_LEVEL_2, 0,
6472 				    "fcp_icmd_callback,3: state change "
6473 				    " occured for D_ID=0x%x", ptgt->tgt_d_id);
6474 				mutex_exit(&pptr->port_mutex);
6475 			}
6476 		}
6477 	} else {
6478 		if (fcp_is_retryable(icmd) &&
6479 		    icmd->ipkt_retries++ < FCP_MAX_RETRIES) {
6480 			if (FCP_MUST_RETRY(fpkt)) {
6481 				fcp_queue_ipkt(pptr, fpkt);
6482 				return;
6483 			}
6484 		}
6485 		mutex_enter(&pptr->port_mutex);
6486 		if (!FCP_TGT_STATE_CHANGED(ptgt, icmd) &&
6487 		    fpkt->pkt_state != FC_PKT_PORT_OFFLINE) {
6488 			mutex_exit(&pptr->port_mutex);
6489 			fcp_print_error(fpkt);
6490 		} else {
6491 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
6492 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
6493 			    "fcp_icmd_callback,4: state change occured"
6494 			    " for D_ID=0x%x", ptgt->tgt_d_id);
6495 			mutex_exit(&pptr->port_mutex);
6496 		}
6497 	}
6498 
6499 fail:
6500 	if (free_pkt) {
6501 		(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
6502 		    icmd->ipkt_change_cnt, icmd->ipkt_cause);
6503 		fcp_icmd_free(pptr, icmd);
6504 	}
6505 }
6506 
6507 
6508 /*
6509  * called internally to send an info cmd using the transport
6510  *
6511  * sends either an INQ or a REPORT_LUN
6512  *
6513  * when the packet is completed fcp_scsi_callback is called
6514  */
6515 static int
6516 fcp_send_scsi(struct fcp_lun *plun, uchar_t opcode, int alloc_len,
6517     int lcount, int tcount, int cause, uint32_t rscn_count)
6518 {
6519 	int			nodma;
6520 	struct fcp_ipkt		*icmd;
6521 	struct fcp_tgt		*ptgt;
6522 	struct fcp_port		*pptr;
6523 	fc_frame_hdr_t		*hp;
6524 	fc_packet_t		*fpkt;
6525 	struct fcp_cmd		fcp_cmd;
6526 	struct fcp_cmd		*fcmd;
6527 	union scsi_cdb		*scsi_cdb;
6528 
6529 	ASSERT(plun != NULL);
6530 
6531 	ptgt = plun->lun_tgt;
6532 	ASSERT(ptgt != NULL);
6533 
6534 	pptr = ptgt->tgt_port;
6535 	ASSERT(pptr != NULL);
6536 
6537 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
6538 	    fcp_trace, FCP_BUF_LEVEL_5, 0,
6539 	    "fcp_send_scsi: d_id=0x%x opcode=0x%x", ptgt->tgt_d_id, opcode);
6540 
6541 	nodma = (pptr->port_fcp_dma == FC_NO_DVMA_SPACE) ? 1 : 0;
6542 	icmd = fcp_icmd_alloc(pptr, ptgt, sizeof (struct fcp_cmd),
6543 	    FCP_MAX_RSP_IU_SIZE, alloc_len, nodma, lcount, tcount, cause,
6544 	    rscn_count);
6545 
6546 	if (icmd == NULL) {
6547 		return (DDI_FAILURE);
6548 	}
6549 
6550 	fpkt = icmd->ipkt_fpkt;
6551 	fpkt->pkt_tran_flags = FC_TRAN_CLASS3 | FC_TRAN_INTR;
6552 	icmd->ipkt_retries = 0;
6553 	icmd->ipkt_opcode = opcode;
6554 	icmd->ipkt_lun = plun;
6555 
6556 	if (nodma) {
6557 		fcmd = (struct fcp_cmd *)fpkt->pkt_cmd;
6558 	} else {
6559 		fcmd = &fcp_cmd;
6560 	}
6561 	bzero(fcmd, sizeof (struct fcp_cmd));
6562 
6563 	fpkt->pkt_timeout = FCP_SCSI_CMD_TIMEOUT;
6564 
6565 	hp = &fpkt->pkt_cmd_fhdr;
6566 
6567 	hp->s_id = pptr->port_id;
6568 	hp->d_id = ptgt->tgt_d_id;
6569 	hp->r_ctl = R_CTL_COMMAND;
6570 	hp->type = FC_TYPE_SCSI_FCP;
6571 	hp->f_ctl = F_CTL_SEQ_INITIATIVE | F_CTL_FIRST_SEQ;
6572 	hp->rsvd = 0;
6573 	hp->seq_id = 0;
6574 	hp->seq_cnt = 0;
6575 	hp->ox_id = 0xffff;
6576 	hp->rx_id = 0xffff;
6577 	hp->ro = 0;
6578 
6579 	bcopy(&(plun->lun_addr), &(fcmd->fcp_ent_addr), FCP_LUN_SIZE);
6580 
6581 	/*
6582 	 * Request SCSI target for expedited processing
6583 	 */
6584 
6585 	/*
6586 	 * Set up for untagged queuing because we do not
6587 	 * know if the fibre device supports queuing.
6588 	 */
6589 	fcmd->fcp_cntl.cntl_reserved_0 = 0;
6590 	fcmd->fcp_cntl.cntl_reserved_1 = 0;
6591 	fcmd->fcp_cntl.cntl_reserved_2 = 0;
6592 	fcmd->fcp_cntl.cntl_reserved_3 = 0;
6593 	fcmd->fcp_cntl.cntl_reserved_4 = 0;
6594 	fcmd->fcp_cntl.cntl_qtype = FCP_QTYPE_UNTAGGED;
6595 	scsi_cdb = (union scsi_cdb *)fcmd->fcp_cdb;
6596 
6597 	switch (opcode) {
6598 	case SCMD_INQUIRY_PAGE83:
6599 		/*
6600 		 * Prepare to get the Inquiry VPD page 83 information
6601 		 */
6602 		fcmd->fcp_cntl.cntl_read_data = 1;
6603 		fcmd->fcp_cntl.cntl_write_data = 0;
6604 		fcmd->fcp_data_len = alloc_len;
6605 
6606 		fpkt->pkt_tran_type = FC_PKT_FCP_READ;
6607 		fpkt->pkt_comp = fcp_scsi_callback;
6608 
6609 		scsi_cdb->scc_cmd = SCMD_INQUIRY;
6610 		scsi_cdb->g0_addr2 = 0x01;
6611 		scsi_cdb->g0_addr1 = 0x83;
6612 		scsi_cdb->g0_count0 = (uchar_t)alloc_len;
6613 		break;
6614 
6615 	case SCMD_INQUIRY:
6616 		fcmd->fcp_cntl.cntl_read_data = 1;
6617 		fcmd->fcp_cntl.cntl_write_data = 0;
6618 		fcmd->fcp_data_len = alloc_len;
6619 
6620 		fpkt->pkt_tran_type = FC_PKT_FCP_READ;
6621 		fpkt->pkt_comp = fcp_scsi_callback;
6622 
6623 		scsi_cdb->scc_cmd = SCMD_INQUIRY;
6624 		scsi_cdb->g0_count0 = SUN_INQSIZE;
6625 		break;
6626 
6627 	case SCMD_REPORT_LUN: {
6628 		fc_portid_t	d_id;
6629 		opaque_t	fca_dev;
6630 
6631 		ASSERT(alloc_len >= 16);
6632 
6633 		d_id.priv_lilp_posit = 0;
6634 		d_id.port_id = ptgt->tgt_d_id;
6635 
6636 		fca_dev = fc_ulp_get_fca_device(pptr->port_fp_handle, d_id);
6637 
6638 		mutex_enter(&ptgt->tgt_mutex);
6639 		ptgt->tgt_fca_dev = fca_dev;
6640 		mutex_exit(&ptgt->tgt_mutex);
6641 
6642 		fcmd->fcp_cntl.cntl_read_data = 1;
6643 		fcmd->fcp_cntl.cntl_write_data = 0;
6644 		fcmd->fcp_data_len = alloc_len;
6645 
6646 		fpkt->pkt_tran_type = FC_PKT_FCP_READ;
6647 		fpkt->pkt_comp = fcp_scsi_callback;
6648 
6649 		scsi_cdb->scc_cmd = SCMD_REPORT_LUN;
6650 		scsi_cdb->scc5_count0 = alloc_len & 0xff;
6651 		scsi_cdb->scc5_count1 = (alloc_len >> 8) & 0xff;
6652 		scsi_cdb->scc5_count2 = (alloc_len >> 16) & 0xff;
6653 		scsi_cdb->scc5_count3 = (alloc_len >> 24) & 0xff;
6654 		break;
6655 	}
6656 
6657 	default:
6658 		fcp_log(CE_WARN, pptr->port_dip,
6659 		    "!fcp_send_scsi Invalid opcode");
6660 		break;
6661 	}
6662 
6663 	if (!nodma) {
6664 		FCP_CP_OUT((uint8_t *)fcmd, fpkt->pkt_cmd,
6665 		    fpkt->pkt_cmd_acc, sizeof (struct fcp_cmd));
6666 	}
6667 
6668 	mutex_enter(&pptr->port_mutex);
6669 	if (!FCP_TGT_STATE_CHANGED(ptgt, icmd)) {
6670 
6671 		mutex_exit(&pptr->port_mutex);
6672 		if (fcp_transport(pptr->port_fp_handle, fpkt, 1) !=
6673 		    FC_SUCCESS) {
6674 			fcp_icmd_free(pptr, icmd);
6675 			return (DDI_FAILURE);
6676 		}
6677 		return (DDI_SUCCESS);
6678 	} else {
6679 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
6680 		    fcp_trace, FCP_BUF_LEVEL_2, 0,
6681 		    "fcp_send_scsi,1: state change occured"
6682 		    " for D_ID=0x%x", ptgt->tgt_d_id);
6683 		mutex_exit(&pptr->port_mutex);
6684 		fcp_icmd_free(pptr, icmd);
6685 		return (DDI_FAILURE);
6686 	}
6687 }
6688 
6689 
6690 /*
6691  * called by fcp_scsi_callback to check to handle the case where
6692  * REPORT_LUN returns ILLEGAL REQUEST or a UNIT ATTENTION
6693  */
6694 static int
6695 fcp_check_reportlun(struct fcp_rsp *rsp, fc_packet_t *fpkt)
6696 {
6697 	uchar_t				rqlen;
6698 	int				rval = DDI_FAILURE;
6699 	struct scsi_extended_sense	sense_info, *sense;
6700 	struct fcp_ipkt		*icmd = (struct fcp_ipkt *)
6701 	    fpkt->pkt_ulp_private;
6702 	struct fcp_tgt		*ptgt = icmd->ipkt_tgt;
6703 	struct fcp_port		*pptr = ptgt->tgt_port;
6704 
6705 	ASSERT(icmd->ipkt_opcode == SCMD_REPORT_LUN);
6706 
6707 	if (rsp->fcp_u.fcp_status.scsi_status == STATUS_RESERVATION_CONFLICT) {
6708 		/*
6709 		 * SCSI-II Reserve Release support. Some older FC drives return
6710 		 * Reservation conflict for Report Luns command.
6711 		 */
6712 		if (icmd->ipkt_nodma) {
6713 			rsp->fcp_u.fcp_status.rsp_len_set = 0;
6714 			rsp->fcp_u.fcp_status.sense_len_set = 0;
6715 			rsp->fcp_u.fcp_status.scsi_status = STATUS_GOOD;
6716 		} else {
6717 			fcp_rsp_t	new_resp;
6718 
6719 			FCP_CP_IN(fpkt->pkt_resp, &new_resp,
6720 			    fpkt->pkt_resp_acc, sizeof (new_resp));
6721 
6722 			new_resp.fcp_u.fcp_status.rsp_len_set = 0;
6723 			new_resp.fcp_u.fcp_status.sense_len_set = 0;
6724 			new_resp.fcp_u.fcp_status.scsi_status = STATUS_GOOD;
6725 
6726 			FCP_CP_OUT(&new_resp, fpkt->pkt_resp,
6727 			    fpkt->pkt_resp_acc, sizeof (new_resp));
6728 		}
6729 
6730 		FCP_CP_OUT(fcp_dummy_lun, fpkt->pkt_data,
6731 		    fpkt->pkt_data_acc, sizeof (fcp_dummy_lun));
6732 
6733 		return (DDI_SUCCESS);
6734 	}
6735 
6736 	sense = &sense_info;
6737 	if (!rsp->fcp_u.fcp_status.sense_len_set) {
6738 		/* no need to continue if sense length is not set */
6739 		return (rval);
6740 	}
6741 
6742 	/* casting 64-bit integer to 8-bit */
6743 	rqlen = (uchar_t)min(rsp->fcp_sense_len,
6744 	    sizeof (struct scsi_extended_sense));
6745 
6746 	if (rqlen < 14) {
6747 		/* no need to continue if request length isn't long enough */
6748 		return (rval);
6749 	}
6750 
6751 	if (icmd->ipkt_nodma) {
6752 		/*
6753 		 * We can safely use fcp_response_len here since the
6754 		 * only path that calls fcp_check_reportlun,
6755 		 * fcp_scsi_callback, has already called
6756 		 * fcp_validate_fcp_response.
6757 		 */
6758 		sense = (struct scsi_extended_sense *)(fpkt->pkt_resp +
6759 		    sizeof (struct fcp_rsp) + rsp->fcp_response_len);
6760 	} else {
6761 		FCP_CP_IN(fpkt->pkt_resp + sizeof (struct fcp_rsp) +
6762 		    rsp->fcp_response_len, sense, fpkt->pkt_resp_acc,
6763 		    sizeof (struct scsi_extended_sense));
6764 	}
6765 
6766 	if (!FCP_SENSE_NO_LUN(sense)) {
6767 		mutex_enter(&ptgt->tgt_mutex);
6768 		/* clear the flag if any */
6769 		ptgt->tgt_state &= ~FCP_TGT_ILLREQ;
6770 		mutex_exit(&ptgt->tgt_mutex);
6771 	}
6772 
6773 	if ((sense->es_key == KEY_ILLEGAL_REQUEST) &&
6774 	    (sense->es_add_code == 0x20)) {
6775 		if (icmd->ipkt_nodma) {
6776 			rsp->fcp_u.fcp_status.rsp_len_set = 0;
6777 			rsp->fcp_u.fcp_status.sense_len_set = 0;
6778 			rsp->fcp_u.fcp_status.scsi_status = STATUS_GOOD;
6779 		} else {
6780 			fcp_rsp_t	new_resp;
6781 
6782 			FCP_CP_IN(fpkt->pkt_resp, &new_resp,
6783 			    fpkt->pkt_resp_acc, sizeof (new_resp));
6784 
6785 			new_resp.fcp_u.fcp_status.rsp_len_set = 0;
6786 			new_resp.fcp_u.fcp_status.sense_len_set = 0;
6787 			new_resp.fcp_u.fcp_status.scsi_status = STATUS_GOOD;
6788 
6789 			FCP_CP_OUT(&new_resp, fpkt->pkt_resp,
6790 			    fpkt->pkt_resp_acc, sizeof (new_resp));
6791 		}
6792 
6793 		FCP_CP_OUT(fcp_dummy_lun, fpkt->pkt_data,
6794 		    fpkt->pkt_data_acc, sizeof (fcp_dummy_lun));
6795 
6796 		return (DDI_SUCCESS);
6797 	}
6798 
6799 	/*
6800 	 * This is for the STK library which returns a check condition,
6801 	 * to indicate device is not ready, manual assistance needed.
6802 	 * This is to a report lun command when the door is open.
6803 	 */
6804 	if ((sense->es_key == KEY_NOT_READY) && (sense->es_add_code == 0x04)) {
6805 		if (icmd->ipkt_nodma) {
6806 			rsp->fcp_u.fcp_status.rsp_len_set = 0;
6807 			rsp->fcp_u.fcp_status.sense_len_set = 0;
6808 			rsp->fcp_u.fcp_status.scsi_status = STATUS_GOOD;
6809 		} else {
6810 			fcp_rsp_t	new_resp;
6811 
6812 			FCP_CP_IN(fpkt->pkt_resp, &new_resp,
6813 			    fpkt->pkt_resp_acc, sizeof (new_resp));
6814 
6815 			new_resp.fcp_u.fcp_status.rsp_len_set = 0;
6816 			new_resp.fcp_u.fcp_status.sense_len_set = 0;
6817 			new_resp.fcp_u.fcp_status.scsi_status = STATUS_GOOD;
6818 
6819 			FCP_CP_OUT(&new_resp, fpkt->pkt_resp,
6820 			    fpkt->pkt_resp_acc, sizeof (new_resp));
6821 		}
6822 
6823 		FCP_CP_OUT(fcp_dummy_lun, fpkt->pkt_data,
6824 		    fpkt->pkt_data_acc, sizeof (fcp_dummy_lun));
6825 
6826 		return (DDI_SUCCESS);
6827 	}
6828 
6829 	if ((FCP_SENSE_REPORTLUN_CHANGED(sense)) ||
6830 	    (FCP_SENSE_NO_LUN(sense))) {
6831 		mutex_enter(&ptgt->tgt_mutex);
6832 		if ((FCP_SENSE_NO_LUN(sense)) &&
6833 		    (ptgt->tgt_state & FCP_TGT_ILLREQ)) {
6834 			ptgt->tgt_state &= ~FCP_TGT_ILLREQ;
6835 			mutex_exit(&ptgt->tgt_mutex);
6836 			/*
6837 			 * reconfig was triggred by ILLEGAL REQUEST but
6838 			 * got ILLEGAL REQUEST again
6839 			 */
6840 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
6841 			    fcp_trace, FCP_BUF_LEVEL_3, 0,
6842 			    "!FCP: Unable to obtain Report Lun data"
6843 			    " target=%x", ptgt->tgt_d_id);
6844 		} else {
6845 			if (ptgt->tgt_tid == NULL) {
6846 				timeout_id_t	tid;
6847 				/*
6848 				 * REPORT LUN data has changed.	 Kick off
6849 				 * rediscovery
6850 				 */
6851 				tid = timeout(fcp_reconfigure_luns,
6852 				    (caddr_t)ptgt, (clock_t)drv_usectohz(1));
6853 
6854 				ptgt->tgt_tid = tid;
6855 				ptgt->tgt_state |= FCP_TGT_BUSY;
6856 			}
6857 			if (FCP_SENSE_NO_LUN(sense)) {
6858 				ptgt->tgt_state |= FCP_TGT_ILLREQ;
6859 			}
6860 			mutex_exit(&ptgt->tgt_mutex);
6861 			if (FCP_SENSE_REPORTLUN_CHANGED(sense)) {
6862 				FCP_TRACE(fcp_logq, pptr->port_instbuf,
6863 				    fcp_trace, FCP_BUF_LEVEL_3, 0,
6864 				    "!FCP:Report Lun Has Changed"
6865 				    " target=%x", ptgt->tgt_d_id);
6866 			} else if (FCP_SENSE_NO_LUN(sense)) {
6867 				FCP_TRACE(fcp_logq, pptr->port_instbuf,
6868 				    fcp_trace, FCP_BUF_LEVEL_3, 0,
6869 				    "!FCP:LU Not Supported"
6870 				    " target=%x", ptgt->tgt_d_id);
6871 			}
6872 		}
6873 		rval = DDI_SUCCESS;
6874 	}
6875 
6876 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
6877 	    fcp_trace, FCP_BUF_LEVEL_5, 0,
6878 	    "D_ID=%x, sense=%x, status=%x",
6879 	    fpkt->pkt_cmd_fhdr.d_id, sense->es_key,
6880 	    rsp->fcp_u.fcp_status.scsi_status);
6881 
6882 	return (rval);
6883 }
6884 
6885 /*
6886  *     Function: fcp_scsi_callback
6887  *
6888  *  Description: This is the callback routine set by fcp_send_scsi() after
6889  *		 it calls fcp_icmd_alloc().  The SCSI command completed here
6890  *		 and autogenerated by FCP are:	REPORT_LUN, INQUIRY and
6891  *		 INQUIRY_PAGE83.
6892  *
6893  *     Argument: *fpkt	 FC packet used to convey the command
6894  *
6895  * Return Value: None
6896  */
6897 static void
6898 fcp_scsi_callback(fc_packet_t *fpkt)
6899 {
6900 	struct fcp_ipkt	*icmd = (struct fcp_ipkt *)
6901 	    fpkt->pkt_ulp_private;
6902 	struct fcp_rsp_info	fcp_rsp_err, *bep;
6903 	struct fcp_port	*pptr;
6904 	struct fcp_tgt	*ptgt;
6905 	struct fcp_lun	*plun;
6906 	struct fcp_rsp		response, *rsp;
6907 
6908 	ptgt = icmd->ipkt_tgt;
6909 	pptr = ptgt->tgt_port;
6910 	plun = icmd->ipkt_lun;
6911 
6912 	if (icmd->ipkt_nodma) {
6913 		rsp = (struct fcp_rsp *)fpkt->pkt_resp;
6914 	} else {
6915 		rsp = &response;
6916 		FCP_CP_IN(fpkt->pkt_resp, rsp, fpkt->pkt_resp_acc,
6917 		    sizeof (struct fcp_rsp));
6918 	}
6919 
6920 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
6921 	    fcp_trace, FCP_BUF_LEVEL_2, 0,
6922 	    "SCSI callback state=0x%x for %x, op_code=0x%x, "
6923 	    "status=%x, lun num=%x",
6924 	    fpkt->pkt_state, ptgt->tgt_d_id, icmd->ipkt_opcode,
6925 	    rsp->fcp_u.fcp_status.scsi_status, plun->lun_num);
6926 
6927 	/*
6928 	 * Pre-init LUN GUID with NWWN if it is not a device that
6929 	 * supports multiple luns and we know it's not page83
6930 	 * compliant.  Although using a NWWN is not lun unique,
6931 	 * we will be fine since there is only one lun behind the taget
6932 	 * in this case.
6933 	 */
6934 	if ((plun->lun_guid_size == 0) &&
6935 	    (icmd->ipkt_opcode == SCMD_INQUIRY_PAGE83) &&
6936 	    (fcp_symmetric_device_probe(plun) == 0)) {
6937 
6938 		char ascii_wwn[FC_WWN_SIZE*2+1];
6939 		fcp_wwn_to_ascii(&ptgt->tgt_node_wwn.raw_wwn[0], ascii_wwn);
6940 		(void) fcp_copy_guid_2_lun_block(plun, ascii_wwn);
6941 	}
6942 
6943 	/*
6944 	 * Some old FC tapes and FC <-> SCSI bridge devices return overrun
6945 	 * when thay have more data than what is asked in CDB. An overrun
6946 	 * is really when FCP_DL is smaller than the data length in CDB.
6947 	 * In the case here we know that REPORT LUN command we formed within
6948 	 * this binary has correct FCP_DL. So this OVERRUN is due to bad device
6949 	 * behavior. In reality this is FC_SUCCESS.
6950 	 */
6951 	if ((fpkt->pkt_state != FC_PKT_SUCCESS) &&
6952 	    (fpkt->pkt_reason == FC_REASON_OVERRUN) &&
6953 	    (icmd->ipkt_opcode == SCMD_REPORT_LUN)) {
6954 		fpkt->pkt_state = FC_PKT_SUCCESS;
6955 	}
6956 
6957 	if (fpkt->pkt_state != FC_PKT_SUCCESS) {
6958 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
6959 		    fcp_trace, FCP_BUF_LEVEL_2, 0,
6960 		    "icmd failed with state=0x%x for %x", fpkt->pkt_state,
6961 		    ptgt->tgt_d_id);
6962 
6963 		if (fpkt->pkt_reason == FC_REASON_CRC_ERROR) {
6964 			/*
6965 			 * Inquiry VPD page command on A5K SES devices would
6966 			 * result in data CRC errors.
6967 			 */
6968 			if (icmd->ipkt_opcode == SCMD_INQUIRY_PAGE83) {
6969 				(void) fcp_handle_page83(fpkt, icmd, 1);
6970 				return;
6971 			}
6972 		}
6973 		if (fpkt->pkt_state == FC_PKT_TIMEOUT ||
6974 		    FCP_MUST_RETRY(fpkt)) {
6975 			fpkt->pkt_timeout += FCP_TIMEOUT_DELTA;
6976 			fcp_retry_scsi_cmd(fpkt);
6977 			return;
6978 		}
6979 
6980 		FCP_TGT_TRACE(ptgt, icmd->ipkt_change_cnt,
6981 		    FCP_TGT_TRACE_20);
6982 
6983 		mutex_enter(&pptr->port_mutex);
6984 		mutex_enter(&ptgt->tgt_mutex);
6985 		if (!FCP_STATE_CHANGED(pptr, ptgt, icmd)) {
6986 			mutex_exit(&ptgt->tgt_mutex);
6987 			mutex_exit(&pptr->port_mutex);
6988 			fcp_print_error(fpkt);
6989 		} else {
6990 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
6991 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
6992 			    "fcp_scsi_callback,1: state change occured"
6993 			    " for D_ID=0x%x", ptgt->tgt_d_id);
6994 			mutex_exit(&ptgt->tgt_mutex);
6995 			mutex_exit(&pptr->port_mutex);
6996 		}
6997 		(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
6998 		    icmd->ipkt_change_cnt, icmd->ipkt_cause);
6999 		fcp_icmd_free(pptr, icmd);
7000 		return;
7001 	}
7002 
7003 	FCP_TGT_TRACE(ptgt, icmd->ipkt_change_cnt, FCP_TGT_TRACE_21);
7004 
7005 	mutex_enter(&pptr->port_mutex);
7006 	mutex_enter(&ptgt->tgt_mutex);
7007 	if (FCP_STATE_CHANGED(pptr, ptgt, icmd)) {
7008 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
7009 		    fcp_trace, FCP_BUF_LEVEL_2, 0,
7010 		    "fcp_scsi_callback,2: state change occured"
7011 		    " for D_ID=0x%x", ptgt->tgt_d_id);
7012 		mutex_exit(&ptgt->tgt_mutex);
7013 		mutex_exit(&pptr->port_mutex);
7014 		(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
7015 		    icmd->ipkt_change_cnt, icmd->ipkt_cause);
7016 		fcp_icmd_free(pptr, icmd);
7017 		return;
7018 	}
7019 	ASSERT((ptgt->tgt_state & FCP_TGT_MARK) == 0);
7020 
7021 	mutex_exit(&ptgt->tgt_mutex);
7022 	mutex_exit(&pptr->port_mutex);
7023 
7024 	if (icmd->ipkt_nodma) {
7025 		bep = (struct fcp_rsp_info *)(fpkt->pkt_resp +
7026 		    sizeof (struct fcp_rsp));
7027 	} else {
7028 		bep = &fcp_rsp_err;
7029 		FCP_CP_IN(fpkt->pkt_resp + sizeof (struct fcp_rsp), bep,
7030 		    fpkt->pkt_resp_acc, sizeof (struct fcp_rsp_info));
7031 	}
7032 
7033 	if (fcp_validate_fcp_response(rsp, pptr) != FC_SUCCESS) {
7034 		fcp_retry_scsi_cmd(fpkt);
7035 		return;
7036 	}
7037 
7038 	if (rsp->fcp_u.fcp_status.rsp_len_set && bep->rsp_code !=
7039 	    FCP_NO_FAILURE) {
7040 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
7041 		    fcp_trace, FCP_BUF_LEVEL_2, 0,
7042 		    "rsp_code=0x%x, rsp_len_set=0x%x",
7043 		    bep->rsp_code, rsp->fcp_u.fcp_status.rsp_len_set);
7044 		fcp_retry_scsi_cmd(fpkt);
7045 		return;
7046 	}
7047 
7048 	if (rsp->fcp_u.fcp_status.scsi_status == STATUS_QFULL ||
7049 	    rsp->fcp_u.fcp_status.scsi_status == STATUS_BUSY) {
7050 		fcp_queue_ipkt(pptr, fpkt);
7051 		return;
7052 	}
7053 
7054 	/*
7055 	 * Devices that do not support INQUIRY_PAGE83, return check condition
7056 	 * with illegal request as per SCSI spec.
7057 	 * Crossbridge is one such device and Daktari's SES node is another.
7058 	 * We want to ideally enumerate these devices as a non-mpxio devices.
7059 	 * SES nodes (Daktari only currently) are an exception to this.
7060 	 */
7061 	if ((icmd->ipkt_opcode == SCMD_INQUIRY_PAGE83) &&
7062 	    (rsp->fcp_u.fcp_status.scsi_status & STATUS_CHECK)) {
7063 
7064 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
7065 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
7066 		    "INQUIRY_PAGE83 for d_id %x (dtype:0x%x) failed with "
7067 		    "check condition. May enumerate as non-mpxio device",
7068 		    ptgt->tgt_d_id, plun->lun_type);
7069 
7070 		/*
7071 		 * If we let Daktari's SES be enumerated as a non-mpxio
7072 		 * device, there will be a discrepency in that the other
7073 		 * internal FC disks will get enumerated as mpxio devices.
7074 		 * Applications like luxadm expect this to be consistent.
7075 		 *
7076 		 * So, we put in a hack here to check if this is an SES device
7077 		 * and handle it here.
7078 		 */
7079 		if (plun->lun_type == DTYPE_ESI) {
7080 			/*
7081 			 * Since, pkt_state is actually FC_PKT_SUCCESS
7082 			 * at this stage, we fake a failure here so that
7083 			 * fcp_handle_page83 will create a device path using
7084 			 * the WWN instead of the GUID which is not there anyway
7085 			 */
7086 			fpkt->pkt_state = FC_PKT_LOCAL_RJT;
7087 			(void) fcp_handle_page83(fpkt, icmd, 1);
7088 			return;
7089 		}
7090 
7091 		mutex_enter(&ptgt->tgt_mutex);
7092 		plun->lun_state &= ~(FCP_LUN_OFFLINE |
7093 		    FCP_LUN_MARK | FCP_LUN_BUSY);
7094 		mutex_exit(&ptgt->tgt_mutex);
7095 
7096 		(void) fcp_call_finish_init(pptr, ptgt,
7097 		    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
7098 		    icmd->ipkt_cause);
7099 		fcp_icmd_free(pptr, icmd);
7100 		return;
7101 	}
7102 
7103 	if (rsp->fcp_u.fcp_status.scsi_status != STATUS_GOOD) {
7104 		int rval = DDI_FAILURE;
7105 
7106 		/*
7107 		 * handle cases where report lun isn't supported
7108 		 * by faking up our own REPORT_LUN response or
7109 		 * UNIT ATTENTION
7110 		 */
7111 		if (icmd->ipkt_opcode == SCMD_REPORT_LUN) {
7112 			rval = fcp_check_reportlun(rsp, fpkt);
7113 
7114 			/*
7115 			 * fcp_check_reportlun might have modified the
7116 			 * FCP response. Copy it in again to get an updated
7117 			 * FCP response
7118 			 */
7119 			if (rval == DDI_SUCCESS && icmd->ipkt_nodma == 0) {
7120 				rsp = &response;
7121 
7122 				FCP_CP_IN(fpkt->pkt_resp, rsp,
7123 				    fpkt->pkt_resp_acc,
7124 				    sizeof (struct fcp_rsp));
7125 			}
7126 		}
7127 
7128 		if (rsp->fcp_u.fcp_status.scsi_status != STATUS_GOOD) {
7129 			if (rval == DDI_SUCCESS) {
7130 				(void) fcp_call_finish_init(pptr, ptgt,
7131 				    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
7132 				    icmd->ipkt_cause);
7133 				fcp_icmd_free(pptr, icmd);
7134 			} else {
7135 				fcp_retry_scsi_cmd(fpkt);
7136 			}
7137 
7138 			return;
7139 		}
7140 	} else {
7141 		if (icmd->ipkt_opcode == SCMD_REPORT_LUN) {
7142 			mutex_enter(&ptgt->tgt_mutex);
7143 			ptgt->tgt_state &= ~FCP_TGT_ILLREQ;
7144 			mutex_exit(&ptgt->tgt_mutex);
7145 		}
7146 	}
7147 
7148 	ASSERT(rsp->fcp_u.fcp_status.scsi_status == STATUS_GOOD);
7149 	if (!(pptr->port_state & FCP_STATE_FCA_IS_NODMA)) {
7150 		(void) ddi_dma_sync(fpkt->pkt_data_dma, 0, 0,
7151 		    DDI_DMA_SYNC_FORCPU);
7152 	}
7153 
7154 	switch (icmd->ipkt_opcode) {
7155 	case SCMD_INQUIRY:
7156 		FCP_LUN_TRACE(plun, FCP_LUN_TRACE_1);
7157 		fcp_handle_inquiry(fpkt, icmd);
7158 		break;
7159 
7160 	case SCMD_REPORT_LUN:
7161 		FCP_TGT_TRACE(ptgt, icmd->ipkt_change_cnt,
7162 		    FCP_TGT_TRACE_22);
7163 		fcp_handle_reportlun(fpkt, icmd);
7164 		break;
7165 
7166 	case SCMD_INQUIRY_PAGE83:
7167 		FCP_LUN_TRACE(plun, FCP_LUN_TRACE_2);
7168 		(void) fcp_handle_page83(fpkt, icmd, 0);
7169 		break;
7170 
7171 	default:
7172 		fcp_log(CE_WARN, NULL, "!Invalid SCSI opcode");
7173 		(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
7174 		    icmd->ipkt_change_cnt, icmd->ipkt_cause);
7175 		fcp_icmd_free(pptr, icmd);
7176 		break;
7177 	}
7178 }
7179 
7180 
7181 static void
7182 fcp_retry_scsi_cmd(fc_packet_t *fpkt)
7183 {
7184 	struct fcp_ipkt	*icmd = (struct fcp_ipkt *)
7185 	    fpkt->pkt_ulp_private;
7186 	struct fcp_tgt	*ptgt = icmd->ipkt_tgt;
7187 	struct fcp_port	*pptr = ptgt->tgt_port;
7188 
7189 	if (icmd->ipkt_retries < FCP_MAX_RETRIES &&
7190 	    fcp_is_retryable(icmd)) {
7191 		mutex_enter(&pptr->port_mutex);
7192 		if (!FCP_TGT_STATE_CHANGED(ptgt, icmd)) {
7193 			mutex_exit(&pptr->port_mutex);
7194 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
7195 			    fcp_trace, FCP_BUF_LEVEL_3, 0,
7196 			    "Retrying %s to %x; state=%x, reason=%x",
7197 			    (icmd->ipkt_opcode == SCMD_REPORT_LUN) ?
7198 			    "Report LUN" : "INQUIRY", ptgt->tgt_d_id,
7199 			    fpkt->pkt_state, fpkt->pkt_reason);
7200 
7201 			fcp_queue_ipkt(pptr, fpkt);
7202 		} else {
7203 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
7204 			    fcp_trace, FCP_BUF_LEVEL_3, 0,
7205 			    "fcp_retry_scsi_cmd,1: state change occured"
7206 			    " for D_ID=0x%x", ptgt->tgt_d_id);
7207 			mutex_exit(&pptr->port_mutex);
7208 			(void) fcp_call_finish_init(pptr, ptgt,
7209 			    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
7210 			    icmd->ipkt_cause);
7211 			fcp_icmd_free(pptr, icmd);
7212 		}
7213 	} else {
7214 		fcp_print_error(fpkt);
7215 		(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
7216 		    icmd->ipkt_change_cnt, icmd->ipkt_cause);
7217 		fcp_icmd_free(pptr, icmd);
7218 	}
7219 }
7220 
7221 /*
7222  *     Function: fcp_handle_page83
7223  *
7224  *  Description: Treats the response to INQUIRY_PAGE83.
7225  *
7226  *     Argument: *fpkt	FC packet used to convey the command.
7227  *		 *icmd	Original fcp_ipkt structure.
7228  *		 ignore_page83_data
7229  *			if it's 1, that means it's a special devices's
7230  *			page83 response, it should be enumerated under mpxio
7231  *
7232  * Return Value: None
7233  */
7234 static void
7235 fcp_handle_page83(fc_packet_t *fpkt, struct fcp_ipkt *icmd,
7236     int ignore_page83_data)
7237 {
7238 	struct fcp_port	*pptr;
7239 	struct fcp_lun	*plun;
7240 	struct fcp_tgt	*ptgt;
7241 	uchar_t			dev_id_page[SCMD_MAX_INQUIRY_PAGE83_SIZE];
7242 	int			fail = 0;
7243 	ddi_devid_t		devid;
7244 	char			*guid = NULL;
7245 	int			ret;
7246 
7247 	ASSERT(icmd != NULL && fpkt != NULL);
7248 
7249 	pptr = icmd->ipkt_port;
7250 	ptgt = icmd->ipkt_tgt;
7251 	plun = icmd->ipkt_lun;
7252 
7253 	if (fpkt->pkt_state == FC_PKT_SUCCESS) {
7254 		FCP_LUN_TRACE(plun, FCP_LUN_TRACE_7);
7255 
7256 		FCP_CP_IN(fpkt->pkt_data, dev_id_page, fpkt->pkt_data_acc,
7257 		    SCMD_MAX_INQUIRY_PAGE83_SIZE);
7258 
7259 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
7260 		    fcp_trace, FCP_BUF_LEVEL_5, 0,
7261 		    "fcp_handle_page83: port=%d, tgt D_ID=0x%x, "
7262 		    "dtype=0x%x, lun num=%x",
7263 		    pptr->port_instance, ptgt->tgt_d_id,
7264 		    dev_id_page[0], plun->lun_num);
7265 
7266 		ret = ddi_devid_scsi_encode(
7267 		    DEVID_SCSI_ENCODE_VERSION_LATEST,
7268 		    NULL,		/* driver name */
7269 		    (unsigned char *) &plun->lun_inq, /* standard inquiry */
7270 		    sizeof (plun->lun_inq), /* size of standard inquiry */
7271 		    NULL,		/* page 80 data */
7272 		    0,		/* page 80 len */
7273 		    dev_id_page,	/* page 83 data */
7274 		    SCMD_MAX_INQUIRY_PAGE83_SIZE, /* page 83 data len */
7275 		    &devid);
7276 
7277 		if (ret == DDI_SUCCESS) {
7278 
7279 			guid = ddi_devid_to_guid(devid);
7280 
7281 			if (guid) {
7282 				/*
7283 				 * Check our current guid.  If it's non null
7284 				 * and it has changed, we need to copy it into
7285 				 * lun_old_guid since we might still need it.
7286 				 */
7287 				if (plun->lun_guid &&
7288 				    strcmp(guid, plun->lun_guid)) {
7289 					unsigned int len;
7290 
7291 					/*
7292 					 * If the guid of the LUN changes,
7293 					 * reconfiguration should be triggered
7294 					 * to reflect the changes.
7295 					 * i.e. we should offline the LUN with
7296 					 * the old guid, and online the LUN with
7297 					 * the new guid.
7298 					 */
7299 					plun->lun_state |= FCP_LUN_CHANGED;
7300 
7301 					if (plun->lun_old_guid) {
7302 						kmem_free(plun->lun_old_guid,
7303 						    plun->lun_old_guid_size);
7304 					}
7305 
7306 					len = plun->lun_guid_size;
7307 					plun->lun_old_guid_size = len;
7308 
7309 					plun->lun_old_guid = kmem_zalloc(len,
7310 					    KM_NOSLEEP);
7311 
7312 					if (plun->lun_old_guid) {
7313 						/*
7314 						 * The alloc was successful then
7315 						 * let's do the copy.
7316 						 */
7317 						bcopy(plun->lun_guid,
7318 						    plun->lun_old_guid, len);
7319 					} else {
7320 						fail = 1;
7321 						plun->lun_old_guid_size = 0;
7322 					}
7323 				}
7324 				if (!fail) {
7325 					if (fcp_copy_guid_2_lun_block(
7326 					    plun, guid)) {
7327 						fail = 1;
7328 					}
7329 				}
7330 				ddi_devid_free_guid(guid);
7331 
7332 			} else {
7333 				FCP_TRACE(fcp_logq, pptr->port_instbuf,
7334 				    fcp_trace, FCP_BUF_LEVEL_2, 0,
7335 				    "fcp_handle_page83: unable to create "
7336 				    "GUID");
7337 
7338 				/* couldn't create good guid from devid */
7339 				fail = 1;
7340 			}
7341 			ddi_devid_free(devid);
7342 
7343 		} else if (ret == DDI_NOT_WELL_FORMED) {
7344 			/* NULL filled data for page 83 */
7345 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
7346 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
7347 			    "fcp_handle_page83: retry GUID");
7348 
7349 			icmd->ipkt_retries = 0;
7350 			fcp_retry_scsi_cmd(fpkt);
7351 			return;
7352 		} else {
7353 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
7354 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
7355 			    "fcp_handle_page83: bad ddi_devid_scsi_encode %x",
7356 			    ret);
7357 			/*
7358 			 * Since the page83 validation
7359 			 * introduced late, we are being
7360 			 * tolerant to the existing devices
7361 			 * that already found to be working
7362 			 * under mpxio, like A5200's SES device,
7363 			 * its page83 response will not be standard-compliant,
7364 			 * but we still want it to be enumerated under mpxio.
7365 			 */
7366 			if (fcp_symmetric_device_probe(plun) != 0) {
7367 				fail = 1;
7368 			}
7369 		}
7370 
7371 	} else {
7372 		/* bad packet state */
7373 		FCP_LUN_TRACE(plun, FCP_LUN_TRACE_8);
7374 
7375 		/*
7376 		 * For some special devices (A5K SES and Daktari's SES devices),
7377 		 * they should be enumerated under mpxio
7378 		 * or "luxadm dis" will fail
7379 		 */
7380 		if (ignore_page83_data) {
7381 			fail = 0;
7382 		} else {
7383 			fail = 1;
7384 		}
7385 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
7386 		    fcp_trace, FCP_BUF_LEVEL_2, 0,
7387 		    "!Devid page cmd failed. "
7388 		    "fpkt_state: %x fpkt_reason: %x",
7389 		    "ignore_page83: %d",
7390 		    fpkt->pkt_state, fpkt->pkt_reason,
7391 		    ignore_page83_data);
7392 	}
7393 
7394 	mutex_enter(&pptr->port_mutex);
7395 	mutex_enter(&plun->lun_mutex);
7396 	/*
7397 	 * If lun_cip is not NULL, then we needn't update lun_mpxio to avoid
7398 	 * mismatch between lun_cip and lun_mpxio.
7399 	 */
7400 	if (plun->lun_cip == NULL) {
7401 		/*
7402 		 * If we don't have a guid for this lun it's because we were
7403 		 * unable to glean one from the page 83 response.  Set the
7404 		 * control flag to 0 here to make sure that we don't attempt to
7405 		 * enumerate it under mpxio.
7406 		 */
7407 		if (fail || pptr->port_mpxio == 0) {
7408 			plun->lun_mpxio = 0;
7409 		} else {
7410 			plun->lun_mpxio = 1;
7411 		}
7412 	}
7413 	mutex_exit(&plun->lun_mutex);
7414 	mutex_exit(&pptr->port_mutex);
7415 
7416 	mutex_enter(&ptgt->tgt_mutex);
7417 	plun->lun_state &=
7418 	    ~(FCP_LUN_OFFLINE | FCP_LUN_MARK | FCP_LUN_BUSY);
7419 	mutex_exit(&ptgt->tgt_mutex);
7420 
7421 	(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
7422 	    icmd->ipkt_change_cnt, icmd->ipkt_cause);
7423 
7424 	fcp_icmd_free(pptr, icmd);
7425 }
7426 
7427 /*
7428  *     Function: fcp_handle_inquiry
7429  *
7430  *  Description: Called by fcp_scsi_callback to handle the response to an
7431  *		 INQUIRY request.
7432  *
7433  *     Argument: *fpkt	FC packet used to convey the command.
7434  *		 *icmd	Original fcp_ipkt structure.
7435  *
7436  * Return Value: None
7437  */
7438 static void
7439 fcp_handle_inquiry(fc_packet_t *fpkt, struct fcp_ipkt *icmd)
7440 {
7441 	struct fcp_port	*pptr;
7442 	struct fcp_lun	*plun;
7443 	struct fcp_tgt	*ptgt;
7444 	uchar_t		dtype;
7445 	uchar_t		pqual;
7446 	uint32_t	rscn_count = FC_INVALID_RSCN_COUNT;
7447 
7448 	ASSERT(icmd != NULL && fpkt != NULL);
7449 
7450 	pptr = icmd->ipkt_port;
7451 	ptgt = icmd->ipkt_tgt;
7452 	plun = icmd->ipkt_lun;
7453 
7454 	FCP_CP_IN(fpkt->pkt_data, &plun->lun_inq, fpkt->pkt_data_acc,
7455 	    sizeof (struct scsi_inquiry));
7456 
7457 	dtype = plun->lun_inq.inq_dtype & DTYPE_MASK;
7458 	pqual = plun->lun_inq.inq_dtype >> 5;
7459 
7460 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
7461 	    fcp_trace, FCP_BUF_LEVEL_5, 0,
7462 	    "fcp_handle_inquiry: port=%d, tgt D_ID=0x%x, lun=0x%x, "
7463 	    "dtype=0x%x pqual: 0x%x", pptr->port_instance, ptgt->tgt_d_id,
7464 	    plun->lun_num, dtype, pqual);
7465 
7466 	if (pqual != 0) {
7467 		/*
7468 		 * Non-zero peripheral qualifier
7469 		 */
7470 		fcp_log(CE_CONT, pptr->port_dip,
7471 		    "!Target 0x%x lun 0x%x: Nonzero peripheral qualifier: "
7472 		    "Device type=0x%x Peripheral qual=0x%x\n",
7473 		    ptgt->tgt_d_id, plun->lun_num, dtype, pqual);
7474 
7475 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
7476 		    fcp_trace, FCP_BUF_LEVEL_5, 0,
7477 		    "!Target 0x%x lun 0x%x: Nonzero peripheral qualifier: "
7478 		    "Device type=0x%x Peripheral qual=0x%x\n",
7479 		    ptgt->tgt_d_id, plun->lun_num, dtype, pqual);
7480 
7481 		FCP_LUN_TRACE(plun, FCP_LUN_TRACE_3);
7482 
7483 		(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
7484 		    icmd->ipkt_change_cnt, icmd->ipkt_cause);
7485 		fcp_icmd_free(pptr, icmd);
7486 		return;
7487 	}
7488 
7489 	/*
7490 	 * If the device is already initialized, check the dtype
7491 	 * for a change. If it has changed then update the flags
7492 	 * so the create_luns will offline the old device and
7493 	 * create the new device. Refer to bug: 4764752
7494 	 */
7495 	if ((plun->lun_state & FCP_LUN_INIT) && dtype != plun->lun_type) {
7496 		plun->lun_state |= FCP_LUN_CHANGED;
7497 	}
7498 	plun->lun_type = plun->lun_inq.inq_dtype;
7499 
7500 	/*
7501 	 * This code is setting/initializing the throttling in the FCA
7502 	 * driver.
7503 	 */
7504 	mutex_enter(&pptr->port_mutex);
7505 	if (!pptr->port_notify) {
7506 		if (bcmp(plun->lun_inq.inq_pid, pid, strlen(pid)) == 0) {
7507 			uint32_t cmd = 0;
7508 			cmd = ((cmd & 0xFF | FC_NOTIFY_THROTTLE) |
7509 			    ((cmd & 0xFFFFFF00 >> 8) |
7510 			    FCP_SVE_THROTTLE << 8));
7511 			pptr->port_notify = 1;
7512 			mutex_exit(&pptr->port_mutex);
7513 			(void) fc_ulp_port_notify(pptr->port_fp_handle, cmd);
7514 			mutex_enter(&pptr->port_mutex);
7515 		}
7516 	}
7517 
7518 	if (FCP_TGT_STATE_CHANGED(ptgt, icmd)) {
7519 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
7520 		    fcp_trace, FCP_BUF_LEVEL_2, 0,
7521 		    "fcp_handle_inquiry,1:state change occured"
7522 		    " for D_ID=0x%x", ptgt->tgt_d_id);
7523 		mutex_exit(&pptr->port_mutex);
7524 
7525 		FCP_LUN_TRACE(plun, FCP_LUN_TRACE_5);
7526 		(void) fcp_call_finish_init(pptr, ptgt,
7527 		    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
7528 		    icmd->ipkt_cause);
7529 		fcp_icmd_free(pptr, icmd);
7530 		return;
7531 	}
7532 	ASSERT((ptgt->tgt_state & FCP_TGT_MARK) == 0);
7533 	mutex_exit(&pptr->port_mutex);
7534 
7535 	/* Retrieve the rscn count (if a valid one exists) */
7536 	if (icmd->ipkt_fpkt->pkt_ulp_rscn_infop != NULL) {
7537 		rscn_count = ((fc_ulp_rscn_info_t *)
7538 		    (icmd->ipkt_fpkt->pkt_ulp_rscn_infop))->ulp_rscn_count;
7539 	} else {
7540 		rscn_count = FC_INVALID_RSCN_COUNT;
7541 	}
7542 
7543 	if (fcp_send_scsi(plun, SCMD_INQUIRY_PAGE83,
7544 	    SCMD_MAX_INQUIRY_PAGE83_SIZE,
7545 	    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
7546 	    icmd->ipkt_cause, rscn_count) != DDI_SUCCESS) {
7547 		fcp_log(CE_WARN, NULL, "!failed to send page 83");
7548 		FCP_LUN_TRACE(plun, FCP_LUN_TRACE_6);
7549 		(void) fcp_call_finish_init(pptr, ptgt,
7550 		    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
7551 		    icmd->ipkt_cause);
7552 	}
7553 
7554 	/*
7555 	 * Read Inquiry VPD Page 0x83 to uniquely
7556 	 * identify this logical unit.
7557 	 */
7558 	fcp_icmd_free(pptr, icmd);
7559 }
7560 
7561 /*
7562  *     Function: fcp_handle_reportlun
7563  *
7564  *  Description: Called by fcp_scsi_callback to handle the response to a
7565  *		 REPORT_LUN request.
7566  *
7567  *     Argument: *fpkt	FC packet used to convey the command.
7568  *		 *icmd	Original fcp_ipkt structure.
7569  *
7570  * Return Value: None
7571  */
7572 static void
7573 fcp_handle_reportlun(fc_packet_t *fpkt, struct fcp_ipkt *icmd)
7574 {
7575 	int				i;
7576 	int				nluns_claimed;
7577 	int				nluns_bufmax;
7578 	int				len;
7579 	uint16_t			lun_num;
7580 	uint32_t			rscn_count = FC_INVALID_RSCN_COUNT;
7581 	struct fcp_port			*pptr;
7582 	struct fcp_tgt			*ptgt;
7583 	struct fcp_lun			*plun;
7584 	struct fcp_reportlun_resp	*report_lun;
7585 
7586 	pptr = icmd->ipkt_port;
7587 	ptgt = icmd->ipkt_tgt;
7588 	len = fpkt->pkt_datalen;
7589 
7590 	if ((len < FCP_LUN_HEADER) ||
7591 	    ((report_lun = kmem_zalloc(len, KM_NOSLEEP)) == NULL)) {
7592 		(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
7593 		    icmd->ipkt_change_cnt, icmd->ipkt_cause);
7594 		fcp_icmd_free(pptr, icmd);
7595 		return;
7596 	}
7597 
7598 	FCP_CP_IN(fpkt->pkt_data, report_lun, fpkt->pkt_data_acc,
7599 	    fpkt->pkt_datalen);
7600 
7601 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
7602 	    fcp_trace, FCP_BUF_LEVEL_5, 0,
7603 	    "fcp_handle_reportlun: port=%d, tgt D_ID=0x%x",
7604 	    pptr->port_instance, ptgt->tgt_d_id);
7605 
7606 	/*
7607 	 * Get the number of luns (which is supplied as LUNS * 8) the
7608 	 * device claims it has.
7609 	 */
7610 	nluns_claimed = BE_32(report_lun->num_lun) >> 3;
7611 
7612 	/*
7613 	 * Get the maximum number of luns the buffer submitted can hold.
7614 	 */
7615 	nluns_bufmax = (fpkt->pkt_datalen - FCP_LUN_HEADER) / FCP_LUN_SIZE;
7616 
7617 	/*
7618 	 * Due to limitations of certain hardware, we support only 16 bit LUNs
7619 	 */
7620 	if (nluns_claimed > FCP_MAX_LUNS_SUPPORTED) {
7621 		kmem_free(report_lun, len);
7622 
7623 		fcp_log(CE_NOTE, pptr->port_dip, "!Can not support"
7624 		    " 0x%x number of LUNs for target=%x", nluns_claimed,
7625 		    ptgt->tgt_d_id);
7626 
7627 		(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
7628 		    icmd->ipkt_change_cnt, icmd->ipkt_cause);
7629 		fcp_icmd_free(pptr, icmd);
7630 		return;
7631 	}
7632 
7633 	/*
7634 	 * If there are more LUNs than we have allocated memory for,
7635 	 * allocate more space and send down yet another report lun if
7636 	 * the maximum number of attempts hasn't been reached.
7637 	 */
7638 	mutex_enter(&ptgt->tgt_mutex);
7639 
7640 	if ((nluns_claimed > nluns_bufmax) &&
7641 	    (ptgt->tgt_report_lun_cnt < FCP_MAX_REPORTLUNS_ATTEMPTS)) {
7642 
7643 		struct fcp_lun *plun;
7644 
7645 		ptgt->tgt_report_lun_cnt++;
7646 		plun = ptgt->tgt_lun;
7647 		ASSERT(plun != NULL);
7648 		mutex_exit(&ptgt->tgt_mutex);
7649 
7650 		kmem_free(report_lun, len);
7651 
7652 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
7653 		    fcp_trace, FCP_BUF_LEVEL_5, 0,
7654 		    "!Dynamically discovered %d LUNs for D_ID=%x",
7655 		    nluns_claimed, ptgt->tgt_d_id);
7656 
7657 		/* Retrieve the rscn count (if a valid one exists) */
7658 		if (icmd->ipkt_fpkt->pkt_ulp_rscn_infop != NULL) {
7659 			rscn_count = ((fc_ulp_rscn_info_t *)
7660 			    (icmd->ipkt_fpkt->pkt_ulp_rscn_infop))->
7661 			    ulp_rscn_count;
7662 		} else {
7663 			rscn_count = FC_INVALID_RSCN_COUNT;
7664 		}
7665 
7666 		if (fcp_send_scsi(icmd->ipkt_lun, SCMD_REPORT_LUN,
7667 		    FCP_LUN_HEADER + (nluns_claimed * FCP_LUN_SIZE),
7668 		    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
7669 		    icmd->ipkt_cause, rscn_count) != DDI_SUCCESS) {
7670 			(void) fcp_call_finish_init(pptr, ptgt,
7671 			    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
7672 			    icmd->ipkt_cause);
7673 		}
7674 
7675 		fcp_icmd_free(pptr, icmd);
7676 		return;
7677 	}
7678 
7679 	if (nluns_claimed > nluns_bufmax) {
7680 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
7681 		    fcp_trace, FCP_BUF_LEVEL_5, 0,
7682 		    "Target=%x:%x:%x:%x:%x:%x:%x:%x"
7683 		    "	 Number of LUNs lost=%x",
7684 		    ptgt->tgt_port_wwn.raw_wwn[0],
7685 		    ptgt->tgt_port_wwn.raw_wwn[1],
7686 		    ptgt->tgt_port_wwn.raw_wwn[2],
7687 		    ptgt->tgt_port_wwn.raw_wwn[3],
7688 		    ptgt->tgt_port_wwn.raw_wwn[4],
7689 		    ptgt->tgt_port_wwn.raw_wwn[5],
7690 		    ptgt->tgt_port_wwn.raw_wwn[6],
7691 		    ptgt->tgt_port_wwn.raw_wwn[7],
7692 		    nluns_claimed - nluns_bufmax);
7693 
7694 		nluns_claimed = nluns_bufmax;
7695 	}
7696 	ptgt->tgt_lun_cnt = nluns_claimed;
7697 
7698 	/*
7699 	 * Identify missing LUNs and print warning messages
7700 	 */
7701 	for (plun = ptgt->tgt_lun; plun; plun = plun->lun_next) {
7702 		int offline;
7703 		int exists = 0;
7704 
7705 		offline = (plun->lun_state & FCP_LUN_OFFLINE) ? 1 : 0;
7706 
7707 		for (i = 0; i < nluns_claimed && exists == 0; i++) {
7708 			uchar_t		*lun_string;
7709 
7710 			lun_string = (uchar_t *)&(report_lun->lun_string[i]);
7711 
7712 			switch (lun_string[0] & 0xC0) {
7713 			case FCP_LUN_ADDRESSING:
7714 			case FCP_PD_ADDRESSING:
7715 			case FCP_VOLUME_ADDRESSING:
7716 				lun_num = ((lun_string[0] & 0x3F) << 8) |
7717 				    lun_string[1];
7718 				if (plun->lun_num == lun_num) {
7719 					exists++;
7720 					break;
7721 				}
7722 				break;
7723 
7724 			default:
7725 				break;
7726 			}
7727 		}
7728 
7729 		if (!exists && !offline) {
7730 			mutex_exit(&ptgt->tgt_mutex);
7731 
7732 			mutex_enter(&pptr->port_mutex);
7733 			mutex_enter(&ptgt->tgt_mutex);
7734 			if (!FCP_STATE_CHANGED(pptr, ptgt, icmd)) {
7735 				/*
7736 				 * set disappear flag when device was connected
7737 				 */
7738 				if (!(plun->lun_state &
7739 				    FCP_LUN_DEVICE_NOT_CONNECTED)) {
7740 					plun->lun_state |= FCP_LUN_DISAPPEARED;
7741 				}
7742 				mutex_exit(&ptgt->tgt_mutex);
7743 				mutex_exit(&pptr->port_mutex);
7744 				if (!(plun->lun_state &
7745 				    FCP_LUN_DEVICE_NOT_CONNECTED)) {
7746 					fcp_log(CE_NOTE, pptr->port_dip,
7747 					    "!Lun=%x for target=%x disappeared",
7748 					    plun->lun_num, ptgt->tgt_d_id);
7749 				}
7750 				mutex_enter(&ptgt->tgt_mutex);
7751 			} else {
7752 				FCP_TRACE(fcp_logq, pptr->port_instbuf,
7753 				    fcp_trace, FCP_BUF_LEVEL_5, 0,
7754 				    "fcp_handle_reportlun,1: state change"
7755 				    " occured for D_ID=0x%x", ptgt->tgt_d_id);
7756 				mutex_exit(&ptgt->tgt_mutex);
7757 				mutex_exit(&pptr->port_mutex);
7758 				kmem_free(report_lun, len);
7759 				(void) fcp_call_finish_init(pptr, ptgt,
7760 				    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
7761 				    icmd->ipkt_cause);
7762 				fcp_icmd_free(pptr, icmd);
7763 				return;
7764 			}
7765 		} else if (exists) {
7766 			/*
7767 			 * clear FCP_LUN_DEVICE_NOT_CONNECTED when lun 0
7768 			 * actually exists in REPORT_LUN response
7769 			 */
7770 			if (plun->lun_state & FCP_LUN_DEVICE_NOT_CONNECTED) {
7771 				plun->lun_state &=
7772 				    ~FCP_LUN_DEVICE_NOT_CONNECTED;
7773 			}
7774 			if (offline || plun->lun_num == 0) {
7775 				if (plun->lun_state & FCP_LUN_DISAPPEARED)  {
7776 					plun->lun_state &= ~FCP_LUN_DISAPPEARED;
7777 					mutex_exit(&ptgt->tgt_mutex);
7778 					fcp_log(CE_NOTE, pptr->port_dip,
7779 					    "!Lun=%x for target=%x reappeared",
7780 					    plun->lun_num, ptgt->tgt_d_id);
7781 					mutex_enter(&ptgt->tgt_mutex);
7782 				}
7783 			}
7784 		}
7785 	}
7786 
7787 	ptgt->tgt_tmp_cnt = nluns_claimed ? nluns_claimed : 1;
7788 	mutex_exit(&ptgt->tgt_mutex);
7789 
7790 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
7791 	    fcp_trace, FCP_BUF_LEVEL_5, 0,
7792 	    "fcp_handle_reportlun: port=%d, tgt D_ID=0x%x, %d LUN(s)",
7793 	    pptr->port_instance, ptgt->tgt_d_id, nluns_claimed);
7794 
7795 	/* scan each lun */
7796 	for (i = 0; i < nluns_claimed; i++) {
7797 		uchar_t	*lun_string;
7798 
7799 		lun_string = (uchar_t *)&(report_lun->lun_string[i]);
7800 
7801 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
7802 		    fcp_trace, FCP_BUF_LEVEL_5, 0,
7803 		    "handle_reportlun: d_id=%x, LUN ind=%d, LUN=%d,"
7804 		    " addr=0x%x", ptgt->tgt_d_id, i, lun_string[1],
7805 		    lun_string[0]);
7806 
7807 		switch (lun_string[0] & 0xC0) {
7808 		case FCP_LUN_ADDRESSING:
7809 		case FCP_PD_ADDRESSING:
7810 		case FCP_VOLUME_ADDRESSING:
7811 			lun_num = ((lun_string[0] & 0x3F) << 8) | lun_string[1];
7812 
7813 			/* We will skip masked LUNs because of the blacklist. */
7814 			if (fcp_lun_blacklist != NULL) {
7815 				mutex_enter(&ptgt->tgt_mutex);
7816 				if (fcp_should_mask(&ptgt->tgt_port_wwn,
7817 				    lun_num) == TRUE) {
7818 					ptgt->tgt_lun_cnt--;
7819 					mutex_exit(&ptgt->tgt_mutex);
7820 					break;
7821 				}
7822 				mutex_exit(&ptgt->tgt_mutex);
7823 			}
7824 
7825 			/* see if this LUN is already allocated */
7826 			if ((plun = fcp_get_lun(ptgt, lun_num)) == NULL) {
7827 				plun = fcp_alloc_lun(ptgt);
7828 				if (plun == NULL) {
7829 					fcp_log(CE_NOTE, pptr->port_dip,
7830 					    "!Lun allocation failed"
7831 					    " target=%x lun=%x",
7832 					    ptgt->tgt_d_id, lun_num);
7833 					break;
7834 				}
7835 			}
7836 
7837 			mutex_enter(&plun->lun_tgt->tgt_mutex);
7838 			/* convert to LUN */
7839 			plun->lun_addr.ent_addr_0 =
7840 			    BE_16(*(uint16_t *)&(lun_string[0]));
7841 			plun->lun_addr.ent_addr_1 =
7842 			    BE_16(*(uint16_t *)&(lun_string[2]));
7843 			plun->lun_addr.ent_addr_2 =
7844 			    BE_16(*(uint16_t *)&(lun_string[4]));
7845 			plun->lun_addr.ent_addr_3 =
7846 			    BE_16(*(uint16_t *)&(lun_string[6]));
7847 
7848 			plun->lun_num = lun_num;
7849 			plun->lun_state |= FCP_LUN_BUSY | FCP_LUN_MARK;
7850 			plun->lun_state &= ~FCP_LUN_OFFLINE;
7851 			mutex_exit(&plun->lun_tgt->tgt_mutex);
7852 
7853 			/* Retrieve the rscn count (if a valid one exists) */
7854 			if (icmd->ipkt_fpkt->pkt_ulp_rscn_infop != NULL) {
7855 				rscn_count = ((fc_ulp_rscn_info_t *)
7856 				    (icmd->ipkt_fpkt->pkt_ulp_rscn_infop))->
7857 				    ulp_rscn_count;
7858 			} else {
7859 				rscn_count = FC_INVALID_RSCN_COUNT;
7860 			}
7861 
7862 			if (fcp_send_scsi(plun, SCMD_INQUIRY, SUN_INQSIZE,
7863 			    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
7864 			    icmd->ipkt_cause, rscn_count) != DDI_SUCCESS) {
7865 				mutex_enter(&pptr->port_mutex);
7866 				mutex_enter(&plun->lun_tgt->tgt_mutex);
7867 				if (!FCP_STATE_CHANGED(pptr, ptgt, icmd)) {
7868 					fcp_log(CE_NOTE, pptr->port_dip,
7869 					    "!failed to send INQUIRY"
7870 					    " target=%x lun=%x",
7871 					    ptgt->tgt_d_id, plun->lun_num);
7872 				} else {
7873 					FCP_TRACE(fcp_logq,
7874 					    pptr->port_instbuf, fcp_trace,
7875 					    FCP_BUF_LEVEL_5, 0,
7876 					    "fcp_handle_reportlun,2: state"
7877 					    " change occured for D_ID=0x%x",
7878 					    ptgt->tgt_d_id);
7879 				}
7880 				mutex_exit(&plun->lun_tgt->tgt_mutex);
7881 				mutex_exit(&pptr->port_mutex);
7882 			} else {
7883 				continue;
7884 			}
7885 			break;
7886 
7887 		default:
7888 			fcp_log(CE_WARN, NULL,
7889 			    "!Unsupported LUN Addressing method %x "
7890 			    "in response to REPORT_LUN", lun_string[0]);
7891 			break;
7892 		}
7893 
7894 		/*
7895 		 * each time through this loop we should decrement
7896 		 * the tmp_cnt by one -- since we go through this loop
7897 		 * one time for each LUN, the tmp_cnt should never be <=0
7898 		 */
7899 		(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
7900 		    icmd->ipkt_change_cnt, icmd->ipkt_cause);
7901 	}
7902 
7903 	if (i == 0) {
7904 		fcp_log(CE_WARN, pptr->port_dip,
7905 		    "!FCP: target=%x reported NO Luns", ptgt->tgt_d_id);
7906 		(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
7907 		    icmd->ipkt_change_cnt, icmd->ipkt_cause);
7908 	}
7909 
7910 	kmem_free(report_lun, len);
7911 	fcp_icmd_free(pptr, icmd);
7912 }
7913 
7914 
7915 /*
7916  * called internally to return a LUN given a target and a LUN number
7917  */
7918 static struct fcp_lun *
7919 fcp_get_lun(struct fcp_tgt *ptgt, uint16_t lun_num)
7920 {
7921 	struct fcp_lun	*plun;
7922 
7923 	mutex_enter(&ptgt->tgt_mutex);
7924 	for (plun = ptgt->tgt_lun; plun != NULL; plun = plun->lun_next) {
7925 		if (plun->lun_num == lun_num) {
7926 			mutex_exit(&ptgt->tgt_mutex);
7927 			return (plun);
7928 		}
7929 	}
7930 	mutex_exit(&ptgt->tgt_mutex);
7931 
7932 	return (NULL);
7933 }
7934 
7935 
7936 /*
7937  * handle finishing one target for fcp_finish_init
7938  *
7939  * return true (non-zero) if we want finish_init to continue with the
7940  * next target
7941  *
7942  * called with the port mutex held
7943  */
7944 /*ARGSUSED*/
7945 static int
7946 fcp_finish_tgt(struct fcp_port *pptr, struct fcp_tgt *ptgt,
7947     int link_cnt, int tgt_cnt, int cause)
7948 {
7949 	int	rval = 1;
7950 	ASSERT(pptr != NULL);
7951 	ASSERT(ptgt != NULL);
7952 
7953 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
7954 	    fcp_trace, FCP_BUF_LEVEL_5, 0,
7955 	    "finish_tgt: D_ID/state = 0x%x/0x%x", ptgt->tgt_d_id,
7956 	    ptgt->tgt_state);
7957 
7958 	ASSERT(mutex_owned(&pptr->port_mutex));
7959 
7960 	if ((pptr->port_link_cnt != link_cnt) ||
7961 	    (tgt_cnt && ptgt->tgt_change_cnt != tgt_cnt)) {
7962 		/*
7963 		 * oh oh -- another link reset or target change
7964 		 * must have occurred while we are in here
7965 		 */
7966 		FCP_TGT_TRACE(ptgt, tgt_cnt, FCP_TGT_TRACE_23);
7967 
7968 		return (0);
7969 	} else {
7970 		FCP_TGT_TRACE(ptgt, tgt_cnt, FCP_TGT_TRACE_24);
7971 	}
7972 
7973 	mutex_enter(&ptgt->tgt_mutex);
7974 
7975 	if (!(ptgt->tgt_state & FCP_TGT_OFFLINE)) {
7976 		/*
7977 		 * tgt is not offline -- is it marked (i.e. needs
7978 		 * to be offlined) ??
7979 		 */
7980 		if (ptgt->tgt_state & FCP_TGT_MARK) {
7981 			/*
7982 			 * this target not offline *and*
7983 			 * marked
7984 			 */
7985 			ptgt->tgt_state &= ~FCP_TGT_MARK;
7986 			rval = fcp_offline_target(pptr, ptgt, link_cnt,
7987 			    tgt_cnt, 0, 0);
7988 		} else {
7989 			ptgt->tgt_state &= ~FCP_TGT_BUSY;
7990 
7991 			/* create the LUNs */
7992 			if (ptgt->tgt_node_state != FCP_TGT_NODE_ON_DEMAND) {
7993 				ptgt->tgt_node_state = FCP_TGT_NODE_PRESENT;
7994 				fcp_create_luns(ptgt, link_cnt, tgt_cnt,
7995 				    cause);
7996 				ptgt->tgt_device_created = 1;
7997 			} else {
7998 				fcp_update_tgt_state(ptgt, FCP_RESET,
7999 				    FCP_LUN_BUSY);
8000 			}
8001 		}
8002 	}
8003 
8004 	mutex_exit(&ptgt->tgt_mutex);
8005 
8006 	return (rval);
8007 }
8008 
8009 
8010 /*
8011  * this routine is called to finish port initialization
8012  *
8013  * Each port has a "temp" counter -- when a state change happens (e.g.
8014  * port online), the temp count is set to the number of devices in the map.
8015  * Then, as each device gets "discovered", the temp counter is decremented
8016  * by one.  When this count reaches zero we know that all of the devices
8017  * in the map have been discovered (or an error has occurred), so we can
8018  * then finish initialization -- which is done by this routine (well, this
8019  * and fcp-finish_tgt())
8020  *
8021  * acquires and releases the global mutex
8022  *
8023  * called with the port mutex owned
8024  */
8025 static void
8026 fcp_finish_init(struct fcp_port *pptr)
8027 {
8028 #ifdef	DEBUG
8029 	bzero(pptr->port_finish_stack, sizeof (pptr->port_finish_stack));
8030 	pptr->port_finish_depth = getpcstack(pptr->port_finish_stack,
8031 	    FCP_STACK_DEPTH);
8032 #endif /* DEBUG */
8033 
8034 	ASSERT(mutex_owned(&pptr->port_mutex));
8035 
8036 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
8037 	    fcp_trace, FCP_BUF_LEVEL_2, 0, "finish_init:"
8038 	    " entering; ipkt count=%d", pptr->port_ipkt_cnt);
8039 
8040 	if ((pptr->port_state & FCP_STATE_ONLINING) &&
8041 	    !(pptr->port_state & (FCP_STATE_SUSPENDED |
8042 	    FCP_STATE_DETACHING | FCP_STATE_POWER_DOWN))) {
8043 		pptr->port_state &= ~FCP_STATE_ONLINING;
8044 		pptr->port_state |= FCP_STATE_ONLINE;
8045 	}
8046 
8047 	/* Wake up threads waiting on config done */
8048 	cv_broadcast(&pptr->port_config_cv);
8049 }
8050 
8051 
8052 /*
8053  * called from fcp_finish_init to create the LUNs for a target
8054  *
8055  * called with the port mutex owned
8056  */
8057 static void
8058 fcp_create_luns(struct fcp_tgt *ptgt, int link_cnt, int tgt_cnt, int cause)
8059 {
8060 	struct fcp_lun	*plun;
8061 	struct fcp_port	*pptr;
8062 	child_info_t		*cip = NULL;
8063 
8064 	ASSERT(ptgt != NULL);
8065 	ASSERT(mutex_owned(&ptgt->tgt_mutex));
8066 
8067 	pptr = ptgt->tgt_port;
8068 
8069 	ASSERT(pptr != NULL);
8070 
8071 	/* scan all LUNs for this target */
8072 	for (plun = ptgt->tgt_lun; plun != NULL; plun = plun->lun_next) {
8073 		if (plun->lun_state & FCP_LUN_OFFLINE) {
8074 			continue;
8075 		}
8076 
8077 		if (plun->lun_state & FCP_LUN_MARK) {
8078 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
8079 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
8080 			    "fcp_create_luns: offlining marked LUN!");
8081 			fcp_offline_lun(plun, link_cnt, tgt_cnt, 1, 0);
8082 			continue;
8083 		}
8084 
8085 		plun->lun_state &= ~FCP_LUN_BUSY;
8086 
8087 		/*
8088 		 * There are conditions in which FCP_LUN_INIT flag is cleared
8089 		 * but we have a valid plun->lun_cip. To cover this case also
8090 		 * CLEAR_BUSY whenever we have a valid lun_cip.
8091 		 */
8092 		if (plun->lun_mpxio && plun->lun_cip &&
8093 		    (!fcp_pass_to_hp(pptr, plun, plun->lun_cip,
8094 		    FCP_MPXIO_PATH_CLEAR_BUSY, link_cnt, tgt_cnt,
8095 		    0, 0))) {
8096 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
8097 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
8098 			    "fcp_create_luns: enable lun %p failed!",
8099 			    plun);
8100 		}
8101 
8102 		if (plun->lun_state & FCP_LUN_INIT &&
8103 		    !(plun->lun_state & FCP_LUN_CHANGED)) {
8104 			continue;
8105 		}
8106 
8107 		if (cause == FCP_CAUSE_USER_CREATE) {
8108 			continue;
8109 		}
8110 
8111 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
8112 		    fcp_trace, FCP_BUF_LEVEL_6, 0,
8113 		    "create_luns: passing ONLINE elem to HP thread");
8114 
8115 		/*
8116 		 * If lun has changed, prepare for offlining the old path.
8117 		 * Do not offline the old path right now, since it may be
8118 		 * still opened.
8119 		 */
8120 		if (plun->lun_cip && (plun->lun_state & FCP_LUN_CHANGED)) {
8121 			fcp_prepare_offline_lun(plun, link_cnt, tgt_cnt);
8122 		}
8123 
8124 		/* pass an ONLINE element to the hotplug thread */
8125 		if (!fcp_pass_to_hp(pptr, plun, cip, FCP_ONLINE,
8126 		    link_cnt, tgt_cnt, NDI_ONLINE_ATTACH, 0)) {
8127 
8128 			/*
8129 			 * We can not synchronous attach (i.e pass
8130 			 * NDI_ONLINE_ATTACH) here as we might be
8131 			 * coming from an interrupt or callback
8132 			 * thread.
8133 			 */
8134 			if (!fcp_pass_to_hp(pptr, plun, cip, FCP_ONLINE,
8135 			    link_cnt, tgt_cnt, 0, 0)) {
8136 				fcp_log(CE_CONT, pptr->port_dip,
8137 				    "Can not ONLINE LUN; D_ID=%x, LUN=%x\n",
8138 				    plun->lun_tgt->tgt_d_id, plun->lun_num);
8139 			}
8140 		}
8141 	}
8142 }
8143 
8144 
8145 /*
8146  * function to online/offline devices
8147  */
8148 static int
8149 fcp_trigger_lun(struct fcp_lun *plun, child_info_t *cip, int old_mpxio,
8150     int online, int lcount, int tcount, int flags)
8151 {
8152 	int			rval = NDI_FAILURE;
8153 	int			circ;
8154 	child_info_t		*ccip;
8155 	struct fcp_port		*pptr = plun->lun_tgt->tgt_port;
8156 	int			is_mpxio = pptr->port_mpxio;
8157 	dev_info_t		*cdip, *pdip;
8158 	char			*devname;
8159 
8160 	if ((old_mpxio != 0) && (plun->lun_mpxio != old_mpxio)) {
8161 		/*
8162 		 * When this event gets serviced, lun_cip and lun_mpxio
8163 		 * has changed, so it should be invalidated now.
8164 		 */
8165 		FCP_TRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
8166 		    FCP_BUF_LEVEL_2, 0, "fcp_trigger_lun: lun_mpxio changed: "
8167 		    "plun: %p, cip: %p, what:%d", plun, cip, online);
8168 		return (rval);
8169 	}
8170 
8171 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
8172 	    fcp_trace, FCP_BUF_LEVEL_2, 0,
8173 	    "fcp_trigger_lun: plun=%p target=%x lun=%d cip=%p what=%x "
8174 	    "flags=%x mpxio=%x\n",
8175 	    plun, LUN_TGT->tgt_d_id, plun->lun_num, cip, online, flags,
8176 	    plun->lun_mpxio);
8177 
8178 	/*
8179 	 * lun_mpxio needs checking here because we can end up in a race
8180 	 * condition where this task has been dispatched while lun_mpxio is
8181 	 * set, but an earlier FCP_ONLINE task for the same LUN tried to
8182 	 * enable MPXIO for the LUN, but was unable to, and hence cleared
8183 	 * the flag. We rely on the serialization of the tasks here. We return
8184 	 * NDI_SUCCESS so any callers continue without reporting spurious
8185 	 * errors, and the still think we're an MPXIO LUN.
8186 	 */
8187 
8188 	if (online == FCP_MPXIO_PATH_CLEAR_BUSY ||
8189 	    online == FCP_MPXIO_PATH_SET_BUSY) {
8190 		if (plun->lun_mpxio) {
8191 			rval = fcp_update_mpxio_path(plun, cip, online);
8192 		} else {
8193 			rval = NDI_SUCCESS;
8194 		}
8195 		return (rval);
8196 	}
8197 
8198 	/*
8199 	 * Explicit devfs_clean() due to ndi_devi_offline() not
8200 	 * executing devfs_clean() if parent lock is held.
8201 	 */
8202 	ASSERT(!servicing_interrupt());
8203 	if (online == FCP_OFFLINE) {
8204 		if (plun->lun_mpxio == 0) {
8205 			if (plun->lun_cip == cip) {
8206 				cdip = DIP(plun->lun_cip);
8207 			} else {
8208 				cdip = DIP(cip);
8209 			}
8210 		} else if ((plun->lun_cip == cip) && plun->lun_cip) {
8211 			cdip = mdi_pi_get_client(PIP(plun->lun_cip));
8212 		} else if ((plun->lun_cip != cip) && cip) {
8213 			/*
8214 			 * This means a DTYPE/GUID change, we shall get the
8215 			 * dip of the old cip instead of the current lun_cip.
8216 			 */
8217 			cdip = mdi_pi_get_client(PIP(cip));
8218 		}
8219 		if (cdip) {
8220 			if (i_ddi_devi_attached(cdip)) {
8221 				pdip = ddi_get_parent(cdip);
8222 				devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
8223 				ndi_devi_enter(pdip, &circ);
8224 				(void) ddi_deviname(cdip, devname);
8225 				ndi_devi_exit(pdip, circ);
8226 				/*
8227 				 * Release parent lock before calling
8228 				 * devfs_clean().
8229 				 */
8230 				rval = devfs_clean(pdip, devname + 1,
8231 				    DV_CLEAN_FORCE);
8232 				kmem_free(devname, MAXNAMELEN + 1);
8233 				/*
8234 				 * Return if devfs_clean() fails for
8235 				 * non-MPXIO case.
8236 				 * For MPXIO case, another path could be
8237 				 * offlined.
8238 				 */
8239 				if (rval && plun->lun_mpxio == 0) {
8240 					FCP_TRACE(fcp_logq, pptr->port_instbuf,
8241 					    fcp_trace, FCP_BUF_LEVEL_3, 0,
8242 					    "fcp_trigger_lun: devfs_clean "
8243 					    "failed rval=%x  dip=%p",
8244 					    rval, pdip);
8245 					return (NDI_FAILURE);
8246 				}
8247 			}
8248 		}
8249 	}
8250 
8251 	if (fc_ulp_busy_port(pptr->port_fp_handle) != 0) {
8252 		return (NDI_FAILURE);
8253 	}
8254 
8255 	if (is_mpxio) {
8256 		mdi_devi_enter(pptr->port_dip, &circ);
8257 	} else {
8258 		ndi_devi_enter(pptr->port_dip, &circ);
8259 	}
8260 
8261 	mutex_enter(&pptr->port_mutex);
8262 	mutex_enter(&plun->lun_mutex);
8263 
8264 	if (online == FCP_ONLINE) {
8265 		ccip = fcp_get_cip(plun, cip, lcount, tcount);
8266 		if (ccip == NULL) {
8267 			goto fail;
8268 		}
8269 	} else {
8270 		if (fcp_is_child_present(plun, cip) != FC_SUCCESS) {
8271 			goto fail;
8272 		}
8273 		ccip = cip;
8274 	}
8275 
8276 	if (online == FCP_ONLINE) {
8277 		rval = fcp_online_child(plun, ccip, lcount, tcount, flags,
8278 		    &circ);
8279 		fc_ulp_log_device_event(pptr->port_fp_handle,
8280 		    FC_ULP_DEVICE_ONLINE);
8281 	} else {
8282 		rval = fcp_offline_child(plun, ccip, lcount, tcount, flags,
8283 		    &circ);
8284 		fc_ulp_log_device_event(pptr->port_fp_handle,
8285 		    FC_ULP_DEVICE_OFFLINE);
8286 	}
8287 
8288 fail:	mutex_exit(&plun->lun_mutex);
8289 	mutex_exit(&pptr->port_mutex);
8290 
8291 	if (is_mpxio) {
8292 		mdi_devi_exit(pptr->port_dip, circ);
8293 	} else {
8294 		ndi_devi_exit(pptr->port_dip, circ);
8295 	}
8296 
8297 	fc_ulp_idle_port(pptr->port_fp_handle);
8298 
8299 	return (rval);
8300 }
8301 
8302 
8303 /*
8304  * take a target offline by taking all of its LUNs offline
8305  */
8306 /*ARGSUSED*/
8307 static int
8308 fcp_offline_target(struct fcp_port *pptr, struct fcp_tgt *ptgt,
8309     int link_cnt, int tgt_cnt, int nowait, int flags)
8310 {
8311 	struct fcp_tgt_elem	*elem;
8312 
8313 	ASSERT(mutex_owned(&pptr->port_mutex));
8314 	ASSERT(mutex_owned(&ptgt->tgt_mutex));
8315 
8316 	ASSERT(!(ptgt->tgt_state & FCP_TGT_OFFLINE));
8317 
8318 	if (link_cnt != pptr->port_link_cnt || (tgt_cnt && tgt_cnt !=
8319 	    ptgt->tgt_change_cnt)) {
8320 		mutex_exit(&ptgt->tgt_mutex);
8321 		FCP_TGT_TRACE(ptgt, tgt_cnt, FCP_TGT_TRACE_25);
8322 		mutex_enter(&ptgt->tgt_mutex);
8323 
8324 		return (0);
8325 	}
8326 
8327 	ptgt->tgt_pd_handle = NULL;
8328 	mutex_exit(&ptgt->tgt_mutex);
8329 	FCP_TGT_TRACE(ptgt, tgt_cnt, FCP_TGT_TRACE_26);
8330 	mutex_enter(&ptgt->tgt_mutex);
8331 
8332 	tgt_cnt = tgt_cnt ? tgt_cnt : ptgt->tgt_change_cnt;
8333 
8334 	if (ptgt->tgt_tcap &&
8335 	    (elem = kmem_zalloc(sizeof (*elem), KM_NOSLEEP)) != NULL) {
8336 		elem->flags = flags;
8337 		elem->time = fcp_watchdog_time;
8338 		if (nowait == 0) {
8339 			elem->time += fcp_offline_delay;
8340 		}
8341 		elem->ptgt = ptgt;
8342 		elem->link_cnt = link_cnt;
8343 		elem->tgt_cnt = tgt_cnt;
8344 		elem->next = pptr->port_offline_tgts;
8345 		pptr->port_offline_tgts = elem;
8346 	} else {
8347 		fcp_offline_target_now(pptr, ptgt, link_cnt, tgt_cnt, flags);
8348 	}
8349 
8350 	return (1);
8351 }
8352 
8353 
8354 static void
8355 fcp_offline_target_now(struct fcp_port *pptr, struct fcp_tgt *ptgt,
8356     int link_cnt, int tgt_cnt, int flags)
8357 {
8358 	ASSERT(mutex_owned(&pptr->port_mutex));
8359 	ASSERT(mutex_owned(&ptgt->tgt_mutex));
8360 
8361 	fc_ulp_enable_relogin(pptr->port_fp_handle, &ptgt->tgt_port_wwn);
8362 	ptgt->tgt_state = FCP_TGT_OFFLINE;
8363 	ptgt->tgt_pd_handle = NULL;
8364 	fcp_offline_tgt_luns(ptgt, link_cnt, tgt_cnt, flags);
8365 }
8366 
8367 
8368 static void
8369 fcp_offline_tgt_luns(struct fcp_tgt *ptgt, int link_cnt, int tgt_cnt,
8370     int flags)
8371 {
8372 	struct	fcp_lun	*plun;
8373 
8374 	ASSERT(mutex_owned(&ptgt->tgt_port->port_mutex));
8375 	ASSERT(mutex_owned(&ptgt->tgt_mutex));
8376 
8377 	for (plun = ptgt->tgt_lun; plun != NULL; plun = plun->lun_next) {
8378 		if (!(plun->lun_state & FCP_LUN_OFFLINE)) {
8379 			fcp_offline_lun(plun, link_cnt, tgt_cnt, 1, flags);
8380 		}
8381 	}
8382 }
8383 
8384 
8385 /*
8386  * take a LUN offline
8387  *
8388  * enters and leaves with the target mutex held, releasing it in the process
8389  *
8390  * allocates memory in non-sleep mode
8391  */
8392 static void
8393 fcp_offline_lun(struct fcp_lun *plun, int link_cnt, int tgt_cnt,
8394     int nowait, int flags)
8395 {
8396 	struct fcp_port	*pptr = plun->lun_tgt->tgt_port;
8397 	struct fcp_lun_elem	*elem;
8398 
8399 	ASSERT(plun != NULL);
8400 	ASSERT(mutex_owned(&LUN_TGT->tgt_mutex));
8401 
8402 	if (nowait) {
8403 		fcp_offline_lun_now(plun, link_cnt, tgt_cnt, flags);
8404 		return;
8405 	}
8406 
8407 	if ((elem = kmem_zalloc(sizeof (*elem), KM_NOSLEEP)) != NULL) {
8408 		elem->flags = flags;
8409 		elem->time = fcp_watchdog_time;
8410 		if (nowait == 0) {
8411 			elem->time += fcp_offline_delay;
8412 		}
8413 		elem->plun = plun;
8414 		elem->link_cnt = link_cnt;
8415 		elem->tgt_cnt = plun->lun_tgt->tgt_change_cnt;
8416 		elem->next = pptr->port_offline_luns;
8417 		pptr->port_offline_luns = elem;
8418 	} else {
8419 		fcp_offline_lun_now(plun, link_cnt, tgt_cnt, flags);
8420 	}
8421 }
8422 
8423 
8424 static void
8425 fcp_prepare_offline_lun(struct fcp_lun *plun, int link_cnt, int tgt_cnt)
8426 {
8427 	struct fcp_pkt	*head = NULL;
8428 
8429 	ASSERT(mutex_owned(&LUN_TGT->tgt_mutex));
8430 
8431 	mutex_exit(&LUN_TGT->tgt_mutex);
8432 
8433 	head = fcp_scan_commands(plun);
8434 	if (head != NULL) {
8435 		fcp_abort_commands(head, LUN_PORT);
8436 	}
8437 
8438 	mutex_enter(&LUN_TGT->tgt_mutex);
8439 
8440 	if (plun->lun_cip && plun->lun_mpxio) {
8441 		/*
8442 		 * Intimate MPxIO lun busy is cleared
8443 		 */
8444 		if (!fcp_pass_to_hp(LUN_PORT, plun, plun->lun_cip,
8445 		    FCP_MPXIO_PATH_CLEAR_BUSY, link_cnt, tgt_cnt,
8446 		    0, 0)) {
8447 			fcp_log(CE_NOTE, LUN_PORT->port_dip,
8448 			    "Can not ENABLE LUN; D_ID=%x, LUN=%x",
8449 			    LUN_TGT->tgt_d_id, plun->lun_num);
8450 		}
8451 		/*
8452 		 * Intimate MPxIO that the lun is now marked for offline
8453 		 */
8454 		mutex_exit(&LUN_TGT->tgt_mutex);
8455 		(void) mdi_pi_disable_path(PIP(plun->lun_cip), DRIVER_DISABLE);
8456 		mutex_enter(&LUN_TGT->tgt_mutex);
8457 	}
8458 }
8459 
8460 static void
8461 fcp_offline_lun_now(struct fcp_lun *plun, int link_cnt, int tgt_cnt,
8462     int flags)
8463 {
8464 	ASSERT(mutex_owned(&LUN_TGT->tgt_mutex));
8465 
8466 	mutex_exit(&LUN_TGT->tgt_mutex);
8467 	fcp_update_offline_flags(plun);
8468 	mutex_enter(&LUN_TGT->tgt_mutex);
8469 
8470 	fcp_prepare_offline_lun(plun, link_cnt, tgt_cnt);
8471 
8472 	FCP_TRACE(fcp_logq, LUN_PORT->port_instbuf,
8473 	    fcp_trace, FCP_BUF_LEVEL_4, 0,
8474 	    "offline_lun: passing OFFLINE elem to HP thread");
8475 
8476 	if (plun->lun_cip) {
8477 		fcp_log(CE_NOTE, LUN_PORT->port_dip,
8478 		    "!offlining lun=%x (trace=%x), target=%x (trace=%x)",
8479 		    plun->lun_num, plun->lun_trace, LUN_TGT->tgt_d_id,
8480 		    LUN_TGT->tgt_trace);
8481 
8482 		if (!fcp_pass_to_hp(LUN_PORT, plun, plun->lun_cip, FCP_OFFLINE,
8483 		    link_cnt, tgt_cnt, flags, 0)) {
8484 			fcp_log(CE_CONT, LUN_PORT->port_dip,
8485 			    "Can not OFFLINE LUN; D_ID=%x, LUN=%x\n",
8486 			    LUN_TGT->tgt_d_id, plun->lun_num);
8487 		}
8488 	}
8489 }
8490 
8491 static void
8492 fcp_scan_offline_luns(struct fcp_port *pptr)
8493 {
8494 	struct fcp_lun_elem	*elem;
8495 	struct fcp_lun_elem	*prev;
8496 	struct fcp_lun_elem	*next;
8497 
8498 	ASSERT(MUTEX_HELD(&pptr->port_mutex));
8499 
8500 	prev = NULL;
8501 	elem = pptr->port_offline_luns;
8502 	while (elem) {
8503 		next = elem->next;
8504 		if (elem->time <= fcp_watchdog_time) {
8505 			int			changed = 1;
8506 			struct fcp_tgt	*ptgt = elem->plun->lun_tgt;
8507 
8508 			mutex_enter(&ptgt->tgt_mutex);
8509 			if (pptr->port_link_cnt == elem->link_cnt &&
8510 			    ptgt->tgt_change_cnt == elem->tgt_cnt) {
8511 				changed = 0;
8512 			}
8513 
8514 			if (!changed &&
8515 			    !(elem->plun->lun_state & FCP_TGT_OFFLINE)) {
8516 				fcp_offline_lun_now(elem->plun,
8517 				    elem->link_cnt, elem->tgt_cnt, elem->flags);
8518 			}
8519 			mutex_exit(&ptgt->tgt_mutex);
8520 
8521 			kmem_free(elem, sizeof (*elem));
8522 
8523 			if (prev) {
8524 				prev->next = next;
8525 			} else {
8526 				pptr->port_offline_luns = next;
8527 			}
8528 		} else {
8529 			prev = elem;
8530 		}
8531 		elem = next;
8532 	}
8533 }
8534 
8535 
8536 static void
8537 fcp_scan_offline_tgts(struct fcp_port *pptr)
8538 {
8539 	struct fcp_tgt_elem	*elem;
8540 	struct fcp_tgt_elem	*prev;
8541 	struct fcp_tgt_elem	*next;
8542 
8543 	ASSERT(MUTEX_HELD(&pptr->port_mutex));
8544 
8545 	prev = NULL;
8546 	elem = pptr->port_offline_tgts;
8547 	while (elem) {
8548 		next = elem->next;
8549 		if (elem->time <= fcp_watchdog_time) {
8550 			int			changed = 1;
8551 			struct fcp_tgt	*ptgt = elem->ptgt;
8552 
8553 			if (ptgt->tgt_change_cnt == elem->tgt_cnt) {
8554 				changed = 0;
8555 			}
8556 
8557 			mutex_enter(&ptgt->tgt_mutex);
8558 			if (!changed && !(ptgt->tgt_state &
8559 			    FCP_TGT_OFFLINE)) {
8560 				fcp_offline_target_now(pptr,
8561 				    ptgt, elem->link_cnt, elem->tgt_cnt,
8562 				    elem->flags);
8563 			}
8564 			mutex_exit(&ptgt->tgt_mutex);
8565 
8566 			kmem_free(elem, sizeof (*elem));
8567 
8568 			if (prev) {
8569 				prev->next = next;
8570 			} else {
8571 				pptr->port_offline_tgts = next;
8572 			}
8573 		} else {
8574 			prev = elem;
8575 		}
8576 		elem = next;
8577 	}
8578 }
8579 
8580 
8581 static void
8582 fcp_update_offline_flags(struct fcp_lun *plun)
8583 {
8584 	struct fcp_port	*pptr = LUN_PORT;
8585 	ASSERT(plun != NULL);
8586 
8587 	mutex_enter(&LUN_TGT->tgt_mutex);
8588 	plun->lun_state |= FCP_LUN_OFFLINE;
8589 	plun->lun_state &= ~(FCP_LUN_INIT | FCP_LUN_BUSY | FCP_LUN_MARK);
8590 
8591 	mutex_enter(&plun->lun_mutex);
8592 	if (plun->lun_cip && plun->lun_state & FCP_SCSI_LUN_TGT_INIT) {
8593 		dev_info_t *cdip = NULL;
8594 
8595 		mutex_exit(&LUN_TGT->tgt_mutex);
8596 
8597 		if (plun->lun_mpxio == 0) {
8598 			cdip = DIP(plun->lun_cip);
8599 		} else if (plun->lun_cip) {
8600 			cdip = mdi_pi_get_client(PIP(plun->lun_cip));
8601 		}
8602 
8603 		mutex_exit(&plun->lun_mutex);
8604 		if (cdip) {
8605 			(void) ndi_event_retrieve_cookie(
8606 			    pptr->port_ndi_event_hdl, cdip, FCAL_REMOVE_EVENT,
8607 			    &fcp_remove_eid, NDI_EVENT_NOPASS);
8608 			(void) ndi_event_run_callbacks(
8609 			    pptr->port_ndi_event_hdl, cdip,
8610 			    fcp_remove_eid, NULL);
8611 		}
8612 	} else {
8613 		mutex_exit(&plun->lun_mutex);
8614 		mutex_exit(&LUN_TGT->tgt_mutex);
8615 	}
8616 }
8617 
8618 
8619 /*
8620  * Scan all of the command pkts for this port, moving pkts that
8621  * match our LUN onto our own list (headed by "head")
8622  */
8623 static struct fcp_pkt *
8624 fcp_scan_commands(struct fcp_lun *plun)
8625 {
8626 	struct fcp_port	*pptr = LUN_PORT;
8627 
8628 	struct fcp_pkt	*cmd = NULL;	/* pkt cmd ptr */
8629 	struct fcp_pkt	*ncmd = NULL;	/* next pkt ptr */
8630 	struct fcp_pkt	*pcmd = NULL;	/* the previous command */
8631 
8632 	struct fcp_pkt	*head = NULL;	/* head of our list */
8633 	struct fcp_pkt	*tail = NULL;	/* tail of our list */
8634 
8635 	int			cmds_found = 0;
8636 
8637 	mutex_enter(&pptr->port_pkt_mutex);
8638 	for (cmd = pptr->port_pkt_head; cmd != NULL; cmd = ncmd) {
8639 		struct fcp_lun *tlun =
8640 		    ADDR2LUN(&cmd->cmd_pkt->pkt_address);
8641 
8642 		ncmd = cmd->cmd_next;	/* set next command */
8643 
8644 		/*
8645 		 * if this pkt is for a different LUN  or the
8646 		 * command is sent down, skip it.
8647 		 */
8648 		if (tlun != plun || cmd->cmd_state == FCP_PKT_ISSUED ||
8649 		    (cmd->cmd_pkt->pkt_flags & FLAG_NOINTR)) {
8650 			pcmd = cmd;
8651 			continue;
8652 		}
8653 		cmds_found++;
8654 		if (pcmd != NULL) {
8655 			ASSERT(pptr->port_pkt_head != cmd);
8656 			pcmd->cmd_next = cmd->cmd_next;
8657 		} else {
8658 			ASSERT(cmd == pptr->port_pkt_head);
8659 			pptr->port_pkt_head = cmd->cmd_next;
8660 		}
8661 
8662 		if (cmd == pptr->port_pkt_tail) {
8663 			pptr->port_pkt_tail = pcmd;
8664 			if (pcmd) {
8665 				pcmd->cmd_next = NULL;
8666 			}
8667 		}
8668 
8669 		if (head == NULL) {
8670 			head = tail = cmd;
8671 		} else {
8672 			ASSERT(tail != NULL);
8673 
8674 			tail->cmd_next = cmd;
8675 			tail = cmd;
8676 		}
8677 		cmd->cmd_next = NULL;
8678 	}
8679 	mutex_exit(&pptr->port_pkt_mutex);
8680 
8681 	FCP_DTRACE(fcp_logq, pptr->port_instbuf,
8682 	    fcp_trace, FCP_BUF_LEVEL_8, 0,
8683 	    "scan commands: %d cmd(s) found", cmds_found);
8684 
8685 	return (head);
8686 }
8687 
8688 
8689 /*
8690  * Abort all the commands in the command queue
8691  */
8692 static void
8693 fcp_abort_commands(struct fcp_pkt *head, struct fcp_port *pptr)
8694 {
8695 	struct fcp_pkt	*cmd = NULL;	/* pkt cmd ptr */
8696 	struct	fcp_pkt	*ncmd = NULL;	/* next pkt ptr */
8697 
8698 	ASSERT(mutex_owned(&pptr->port_mutex));
8699 
8700 	/* scan through the pkts and invalid them */
8701 	for (cmd = head; cmd != NULL; cmd = ncmd) {
8702 		struct scsi_pkt *pkt = cmd->cmd_pkt;
8703 
8704 		ncmd = cmd->cmd_next;
8705 		ASSERT(pkt != NULL);
8706 
8707 		/*
8708 		 * The lun is going to be marked offline. Indicate
8709 		 * the target driver not to requeue or retry this command
8710 		 * as the device is going to be offlined pretty soon.
8711 		 */
8712 		pkt->pkt_reason = CMD_DEV_GONE;
8713 		pkt->pkt_statistics = 0;
8714 		pkt->pkt_state = 0;
8715 
8716 		/* reset cmd flags/state */
8717 		cmd->cmd_flags &= ~CFLAG_IN_QUEUE;
8718 		cmd->cmd_state = FCP_PKT_IDLE;
8719 
8720 		/*
8721 		 * ensure we have a packet completion routine,
8722 		 * then call it.
8723 		 */
8724 		ASSERT(pkt->pkt_comp != NULL);
8725 
8726 		mutex_exit(&pptr->port_mutex);
8727 		fcp_post_callback(cmd);
8728 		mutex_enter(&pptr->port_mutex);
8729 	}
8730 }
8731 
8732 
8733 /*
8734  * the pkt_comp callback for command packets
8735  */
8736 static void
8737 fcp_cmd_callback(fc_packet_t *fpkt)
8738 {
8739 	struct fcp_pkt *cmd = (struct fcp_pkt *)fpkt->pkt_ulp_private;
8740 	struct scsi_pkt *pkt = cmd->cmd_pkt;
8741 	struct fcp_port *pptr = ADDR2FCP(&pkt->pkt_address);
8742 
8743 	ASSERT(cmd->cmd_state != FCP_PKT_IDLE);
8744 
8745 	if (cmd->cmd_state == FCP_PKT_IDLE) {
8746 		cmn_err(CE_PANIC, "Packet already completed %p",
8747 		    (void *)cmd);
8748 	}
8749 
8750 	/*
8751 	 * Watch thread should be freeing the packet, ignore the pkt.
8752 	 */
8753 	if (cmd->cmd_state == FCP_PKT_ABORTING) {
8754 		fcp_log(CE_CONT, pptr->port_dip,
8755 		    "!FCP: Pkt completed while aborting\n");
8756 		return;
8757 	}
8758 	cmd->cmd_state = FCP_PKT_IDLE;
8759 
8760 	fcp_complete_pkt(fpkt);
8761 
8762 #ifdef	DEBUG
8763 	mutex_enter(&pptr->port_pkt_mutex);
8764 	pptr->port_npkts--;
8765 	mutex_exit(&pptr->port_pkt_mutex);
8766 #endif /* DEBUG */
8767 
8768 	fcp_post_callback(cmd);
8769 }
8770 
8771 
8772 static void
8773 fcp_complete_pkt(fc_packet_t *fpkt)
8774 {
8775 	int			error = 0;
8776 	struct fcp_pkt	*cmd = (struct fcp_pkt *)
8777 	    fpkt->pkt_ulp_private;
8778 	struct scsi_pkt		*pkt = cmd->cmd_pkt;
8779 	struct fcp_port		*pptr = ADDR2FCP(&pkt->pkt_address);
8780 	struct fcp_lun	*plun;
8781 	struct fcp_tgt	*ptgt;
8782 	struct fcp_rsp		*rsp;
8783 	struct scsi_address	save;
8784 
8785 #ifdef	DEBUG
8786 	save = pkt->pkt_address;
8787 #endif /* DEBUG */
8788 
8789 	rsp = (struct fcp_rsp *)cmd->cmd_fcp_rsp;
8790 
8791 	if (fpkt->pkt_state == FC_PKT_SUCCESS) {
8792 		if (pptr->port_fcp_dma != FC_NO_DVMA_SPACE) {
8793 			FCP_CP_IN(fpkt->pkt_resp, rsp, fpkt->pkt_resp_acc,
8794 			    sizeof (struct fcp_rsp));
8795 		}
8796 
8797 		pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
8798 		    STATE_SENT_CMD | STATE_GOT_STATUS;
8799 
8800 		pkt->pkt_resid = 0;
8801 
8802 		if (fpkt->pkt_datalen) {
8803 			pkt->pkt_state |= STATE_XFERRED_DATA;
8804 			if (fpkt->pkt_data_resid) {
8805 				error++;
8806 			}
8807 		}
8808 
8809 		if ((pkt->pkt_scbp != NULL) && ((*(pkt->pkt_scbp) =
8810 		    rsp->fcp_u.fcp_status.scsi_status) != STATUS_GOOD)) {
8811 			/*
8812 			 * The next two checks make sure that if there
8813 			 * is no sense data or a valid response and
8814 			 * the command came back with check condition,
8815 			 * the command should be retried.
8816 			 */
8817 			if (!rsp->fcp_u.fcp_status.rsp_len_set &&
8818 			    !rsp->fcp_u.fcp_status.sense_len_set) {
8819 				pkt->pkt_state &= ~STATE_XFERRED_DATA;
8820 				pkt->pkt_resid = cmd->cmd_dmacount;
8821 			}
8822 		}
8823 
8824 		if ((error | rsp->fcp_u.i_fcp_status | rsp->fcp_resid) == 0) {
8825 			return;
8826 		}
8827 
8828 		plun = ADDR2LUN(&pkt->pkt_address);
8829 		ptgt = plun->lun_tgt;
8830 		ASSERT(ptgt != NULL);
8831 
8832 		/*
8833 		 * Update the transfer resid, if appropriate
8834 		 */
8835 		if (rsp->fcp_u.fcp_status.resid_over ||
8836 		    rsp->fcp_u.fcp_status.resid_under) {
8837 			pkt->pkt_resid = rsp->fcp_resid;
8838 		}
8839 
8840 		/*
8841 		 * First see if we got a FCP protocol error.
8842 		 */
8843 		if (rsp->fcp_u.fcp_status.rsp_len_set) {
8844 			struct fcp_rsp_info	*bep;
8845 			bep = (struct fcp_rsp_info *)(cmd->cmd_fcp_rsp +
8846 			    sizeof (struct fcp_rsp));
8847 
8848 			if (fcp_validate_fcp_response(rsp, pptr) !=
8849 			    FC_SUCCESS) {
8850 				pkt->pkt_reason = CMD_CMPLT;
8851 				*(pkt->pkt_scbp) = STATUS_CHECK;
8852 
8853 				fcp_log(CE_WARN, pptr->port_dip,
8854 				    "!SCSI command to d_id=0x%x lun=0x%x"
8855 				    " failed, Bad FCP response values:"
8856 				    " rsvd1=%x, rsvd2=%x, sts-rsvd1=%x,"
8857 				    " sts-rsvd2=%x, rsplen=%x, senselen=%x",
8858 				    ptgt->tgt_d_id, plun->lun_num,
8859 				    rsp->reserved_0, rsp->reserved_1,
8860 				    rsp->fcp_u.fcp_status.reserved_0,
8861 				    rsp->fcp_u.fcp_status.reserved_1,
8862 				    rsp->fcp_response_len, rsp->fcp_sense_len);
8863 
8864 				return;
8865 			}
8866 
8867 			if (pptr->port_fcp_dma != FC_NO_DVMA_SPACE) {
8868 				FCP_CP_IN(fpkt->pkt_resp +
8869 				    sizeof (struct fcp_rsp), bep,
8870 				    fpkt->pkt_resp_acc,
8871 				    sizeof (struct fcp_rsp_info));
8872 			}
8873 
8874 			if (bep->rsp_code != FCP_NO_FAILURE) {
8875 				child_info_t	*cip;
8876 
8877 				pkt->pkt_reason = CMD_TRAN_ERR;
8878 
8879 				mutex_enter(&plun->lun_mutex);
8880 				cip = plun->lun_cip;
8881 				mutex_exit(&plun->lun_mutex);
8882 
8883 				FCP_TRACE(fcp_logq, pptr->port_instbuf,
8884 				    fcp_trace, FCP_BUF_LEVEL_2, 0,
8885 				    "FCP response error on cmd=%p"
8886 				    " target=0x%x, cip=%p", cmd,
8887 				    ptgt->tgt_d_id, cip);
8888 			}
8889 		}
8890 
8891 		/*
8892 		 * See if we got a SCSI error with sense data
8893 		 */
8894 		if (rsp->fcp_u.fcp_status.sense_len_set) {
8895 			uchar_t				rqlen;
8896 			caddr_t				sense_from;
8897 			child_info_t			*cip;
8898 			timeout_id_t			tid;
8899 			struct scsi_arq_status		*arq;
8900 			struct scsi_extended_sense	*sense_to;
8901 
8902 			arq = (struct scsi_arq_status *)pkt->pkt_scbp;
8903 			sense_to = &arq->sts_sensedata;
8904 
8905 			rqlen = (uchar_t)min(rsp->fcp_sense_len,
8906 			    sizeof (struct scsi_extended_sense));
8907 
8908 			sense_from = (caddr_t)fpkt->pkt_resp +
8909 			    sizeof (struct fcp_rsp) + rsp->fcp_response_len;
8910 
8911 			if (fcp_validate_fcp_response(rsp, pptr) !=
8912 			    FC_SUCCESS) {
8913 				pkt->pkt_reason = CMD_CMPLT;
8914 				*(pkt->pkt_scbp) = STATUS_CHECK;
8915 
8916 				fcp_log(CE_WARN, pptr->port_dip,
8917 				    "!SCSI command to d_id=0x%x lun=0x%x"
8918 				    " failed, Bad FCP response values:"
8919 				    " rsvd1=%x, rsvd2=%x, sts-rsvd1=%x,"
8920 				    " sts-rsvd2=%x, rsplen=%x, senselen=%x",
8921 				    ptgt->tgt_d_id, plun->lun_num,
8922 				    rsp->reserved_0, rsp->reserved_1,
8923 				    rsp->fcp_u.fcp_status.reserved_0,
8924 				    rsp->fcp_u.fcp_status.reserved_1,
8925 				    rsp->fcp_response_len, rsp->fcp_sense_len);
8926 
8927 				return;
8928 			}
8929 
8930 			/*
8931 			 * copy in sense information
8932 			 */
8933 			if (pptr->port_fcp_dma != FC_NO_DVMA_SPACE) {
8934 				FCP_CP_IN(sense_from, sense_to,
8935 				    fpkt->pkt_resp_acc, rqlen);
8936 			} else {
8937 				bcopy(sense_from, sense_to, rqlen);
8938 			}
8939 
8940 			if ((FCP_SENSE_REPORTLUN_CHANGED(sense_to)) ||
8941 			    (FCP_SENSE_NO_LUN(sense_to))) {
8942 				mutex_enter(&ptgt->tgt_mutex);
8943 				if (ptgt->tgt_tid == NULL) {
8944 					/*
8945 					 * Kick off rediscovery
8946 					 */
8947 					tid = timeout(fcp_reconfigure_luns,
8948 					    (caddr_t)ptgt, drv_usectohz(1));
8949 
8950 					ptgt->tgt_tid = tid;
8951 					ptgt->tgt_state |= FCP_TGT_BUSY;
8952 				}
8953 				mutex_exit(&ptgt->tgt_mutex);
8954 				if (FCP_SENSE_REPORTLUN_CHANGED(sense_to)) {
8955 					FCP_TRACE(fcp_logq, pptr->port_instbuf,
8956 					    fcp_trace, FCP_BUF_LEVEL_3, 0,
8957 					    "!FCP: Report Lun Has Changed"
8958 					    " target=%x", ptgt->tgt_d_id);
8959 				} else if (FCP_SENSE_NO_LUN(sense_to)) {
8960 					FCP_TRACE(fcp_logq, pptr->port_instbuf,
8961 					    fcp_trace, FCP_BUF_LEVEL_3, 0,
8962 					    "!FCP: LU Not Supported"
8963 					    " target=%x", ptgt->tgt_d_id);
8964 				}
8965 			}
8966 			ASSERT(pkt->pkt_scbp != NULL);
8967 
8968 			pkt->pkt_state |= STATE_ARQ_DONE;
8969 
8970 			arq->sts_rqpkt_resid = SENSE_LENGTH - rqlen;
8971 
8972 			*((uchar_t *)&arq->sts_rqpkt_status) = STATUS_GOOD;
8973 			arq->sts_rqpkt_reason = 0;
8974 			arq->sts_rqpkt_statistics = 0;
8975 
8976 			arq->sts_rqpkt_state = STATE_GOT_BUS |
8977 			    STATE_GOT_TARGET | STATE_SENT_CMD |
8978 			    STATE_GOT_STATUS | STATE_ARQ_DONE |
8979 			    STATE_XFERRED_DATA;
8980 
8981 			mutex_enter(&plun->lun_mutex);
8982 			cip = plun->lun_cip;
8983 			mutex_exit(&plun->lun_mutex);
8984 
8985 			FCP_DTRACE(fcp_logq, pptr->port_instbuf,
8986 			    fcp_trace, FCP_BUF_LEVEL_8, 0,
8987 			    "SCSI Check condition on cmd=%p target=0x%x"
8988 			    " LUN=%p, cmd=%x SCSI status=%x, es key=%x"
8989 			    " ASC=%x ASCQ=%x", cmd, ptgt->tgt_d_id, cip,
8990 			    cmd->cmd_fcp_cmd.fcp_cdb[0],
8991 			    rsp->fcp_u.fcp_status.scsi_status,
8992 			    sense_to->es_key, sense_to->es_add_code,
8993 			    sense_to->es_qual_code);
8994 		}
8995 	} else {
8996 		plun = ADDR2LUN(&pkt->pkt_address);
8997 		ptgt = plun->lun_tgt;
8998 		ASSERT(ptgt != NULL);
8999 
9000 		/*
9001 		 * Work harder to translate errors into target driver
9002 		 * understandable ones. Note with despair that the target
9003 		 * drivers don't decode pkt_state and pkt_reason exhaustively
9004 		 * They resort to using the big hammer most often, which
9005 		 * may not get fixed in the life time of this driver.
9006 		 */
9007 		pkt->pkt_state = 0;
9008 		pkt->pkt_statistics = 0;
9009 
9010 		switch (fpkt->pkt_state) {
9011 		case FC_PKT_TRAN_ERROR:
9012 			switch (fpkt->pkt_reason) {
9013 			case FC_REASON_OVERRUN:
9014 				pkt->pkt_reason = CMD_CMD_OVR;
9015 				pkt->pkt_statistics |= STAT_ABORTED;
9016 				break;
9017 
9018 			case FC_REASON_XCHG_BSY: {
9019 				caddr_t ptr;
9020 
9021 				pkt->pkt_reason = CMD_CMPLT;	/* Lie */
9022 
9023 				ptr = (caddr_t)pkt->pkt_scbp;
9024 				if (ptr) {
9025 					*ptr = STATUS_BUSY;
9026 				}
9027 				break;
9028 			}
9029 
9030 			case FC_REASON_ABORTED:
9031 				pkt->pkt_reason = CMD_TRAN_ERR;
9032 				pkt->pkt_statistics |= STAT_ABORTED;
9033 				break;
9034 
9035 			case FC_REASON_ABORT_FAILED:
9036 				pkt->pkt_reason = CMD_ABORT_FAIL;
9037 				break;
9038 
9039 			case FC_REASON_NO_SEQ_INIT:
9040 			case FC_REASON_CRC_ERROR:
9041 				pkt->pkt_reason = CMD_TRAN_ERR;
9042 				pkt->pkt_statistics |= STAT_ABORTED;
9043 				break;
9044 			default:
9045 				pkt->pkt_reason = CMD_TRAN_ERR;
9046 				break;
9047 			}
9048 			break;
9049 
9050 		case FC_PKT_PORT_OFFLINE: {
9051 			dev_info_t	*cdip = NULL;
9052 			caddr_t		ptr;
9053 
9054 			if (fpkt->pkt_reason == FC_REASON_LOGIN_REQUIRED) {
9055 				FCP_DTRACE(fcp_logq, pptr->port_instbuf,
9056 				    fcp_trace, FCP_BUF_LEVEL_8, 0,
9057 				    "SCSI cmd; LOGIN REQUIRED from FCA for %x",
9058 				    ptgt->tgt_d_id);
9059 			}
9060 
9061 			mutex_enter(&plun->lun_mutex);
9062 			if (plun->lun_mpxio == 0) {
9063 				cdip = DIP(plun->lun_cip);
9064 			} else if (plun->lun_cip) {
9065 				cdip = mdi_pi_get_client(PIP(plun->lun_cip));
9066 			}
9067 
9068 			mutex_exit(&plun->lun_mutex);
9069 
9070 			if (cdip) {
9071 				(void) ndi_event_retrieve_cookie(
9072 				    pptr->port_ndi_event_hdl, cdip,
9073 				    FCAL_REMOVE_EVENT, &fcp_remove_eid,
9074 				    NDI_EVENT_NOPASS);
9075 				(void) ndi_event_run_callbacks(
9076 				    pptr->port_ndi_event_hdl, cdip,
9077 				    fcp_remove_eid, NULL);
9078 			}
9079 
9080 			/*
9081 			 * If the link goes off-line for a lip,
9082 			 * this will cause a error to the ST SG
9083 			 * SGEN drivers. By setting BUSY we will
9084 			 * give the drivers the chance to retry
9085 			 * before it blows of the job. ST will
9086 			 * remember how many times it has retried.
9087 			 */
9088 
9089 			if ((plun->lun_type == DTYPE_SEQUENTIAL) ||
9090 			    (plun->lun_type == DTYPE_CHANGER)) {
9091 				pkt->pkt_reason = CMD_CMPLT;	/* Lie */
9092 				ptr = (caddr_t)pkt->pkt_scbp;
9093 				if (ptr) {
9094 					*ptr = STATUS_BUSY;
9095 				}
9096 			} else {
9097 				pkt->pkt_reason = CMD_TRAN_ERR;
9098 				pkt->pkt_statistics |= STAT_BUS_RESET;
9099 			}
9100 			break;
9101 		}
9102 
9103 		case FC_PKT_TRAN_BSY:
9104 			/*
9105 			 * Use the ssd Qfull handling here.
9106 			 */
9107 			*pkt->pkt_scbp = STATUS_INTERMEDIATE;
9108 			pkt->pkt_state = STATE_GOT_BUS;
9109 			break;
9110 
9111 		case FC_PKT_TIMEOUT:
9112 			pkt->pkt_reason = CMD_TIMEOUT;
9113 			if (fpkt->pkt_reason == FC_REASON_ABORT_FAILED) {
9114 				pkt->pkt_statistics |= STAT_TIMEOUT;
9115 			} else {
9116 				pkt->pkt_statistics |= STAT_ABORTED;
9117 			}
9118 			break;
9119 
9120 		case FC_PKT_LOCAL_RJT:
9121 			switch (fpkt->pkt_reason) {
9122 			case FC_REASON_OFFLINE: {
9123 				dev_info_t	*cdip = NULL;
9124 
9125 				mutex_enter(&plun->lun_mutex);
9126 				if (plun->lun_mpxio == 0) {
9127 					cdip = DIP(plun->lun_cip);
9128 				} else if (plun->lun_cip) {
9129 					cdip = mdi_pi_get_client(
9130 					    PIP(plun->lun_cip));
9131 				}
9132 				mutex_exit(&plun->lun_mutex);
9133 
9134 				if (cdip) {
9135 					(void) ndi_event_retrieve_cookie(
9136 					    pptr->port_ndi_event_hdl, cdip,
9137 					    FCAL_REMOVE_EVENT,
9138 					    &fcp_remove_eid,
9139 					    NDI_EVENT_NOPASS);
9140 					(void) ndi_event_run_callbacks(
9141 					    pptr->port_ndi_event_hdl,
9142 					    cdip, fcp_remove_eid, NULL);
9143 				}
9144 
9145 				pkt->pkt_reason = CMD_TRAN_ERR;
9146 				pkt->pkt_statistics |= STAT_BUS_RESET;
9147 
9148 				break;
9149 			}
9150 
9151 			case FC_REASON_NOMEM:
9152 			case FC_REASON_QFULL: {
9153 				caddr_t ptr;
9154 
9155 				pkt->pkt_reason = CMD_CMPLT;	/* Lie */
9156 				ptr = (caddr_t)pkt->pkt_scbp;
9157 				if (ptr) {
9158 					*ptr = STATUS_BUSY;
9159 				}
9160 				break;
9161 			}
9162 
9163 			case FC_REASON_DMA_ERROR:
9164 				pkt->pkt_reason = CMD_DMA_DERR;
9165 				pkt->pkt_statistics |= STAT_ABORTED;
9166 				break;
9167 
9168 			case FC_REASON_CRC_ERROR:
9169 			case FC_REASON_UNDERRUN: {
9170 				uchar_t		status;
9171 				/*
9172 				 * Work around for Bugid: 4240945.
9173 				 * IB on A5k doesn't set the Underrun bit
9174 				 * in the fcp status, when it is transferring
9175 				 * less than requested amount of data. Work
9176 				 * around the ses problem to keep luxadm
9177 				 * happy till ibfirmware is fixed.
9178 				 */
9179 				if (pptr->port_fcp_dma != FC_NO_DVMA_SPACE) {
9180 					FCP_CP_IN(fpkt->pkt_resp, rsp,
9181 					    fpkt->pkt_resp_acc,
9182 					    sizeof (struct fcp_rsp));
9183 				}
9184 				status = rsp->fcp_u.fcp_status.scsi_status;
9185 				if (((plun->lun_type & DTYPE_MASK) ==
9186 				    DTYPE_ESI) && (status == STATUS_GOOD)) {
9187 					pkt->pkt_reason = CMD_CMPLT;
9188 					*pkt->pkt_scbp = status;
9189 					pkt->pkt_resid = 0;
9190 				} else {
9191 					pkt->pkt_reason = CMD_TRAN_ERR;
9192 					pkt->pkt_statistics |= STAT_ABORTED;
9193 				}
9194 				break;
9195 			}
9196 
9197 			case FC_REASON_NO_CONNECTION:
9198 			case FC_REASON_UNSUPPORTED:
9199 			case FC_REASON_ILLEGAL_REQ:
9200 			case FC_REASON_BAD_SID:
9201 			case FC_REASON_DIAG_BUSY:
9202 			case FC_REASON_FCAL_OPN_FAIL:
9203 			case FC_REASON_BAD_XID:
9204 			default:
9205 				pkt->pkt_reason = CMD_TRAN_ERR;
9206 				pkt->pkt_statistics |= STAT_ABORTED;
9207 				break;
9208 
9209 			}
9210 			break;
9211 
9212 		case FC_PKT_NPORT_RJT:
9213 		case FC_PKT_FABRIC_RJT:
9214 		case FC_PKT_NPORT_BSY:
9215 		case FC_PKT_FABRIC_BSY:
9216 		default:
9217 			FCP_DTRACE(fcp_logq, pptr->port_instbuf,
9218 			    fcp_trace, FCP_BUF_LEVEL_8, 0,
9219 			    "FC Status 0x%x, reason 0x%x",
9220 			    fpkt->pkt_state, fpkt->pkt_reason);
9221 			pkt->pkt_reason = CMD_TRAN_ERR;
9222 			pkt->pkt_statistics |= STAT_ABORTED;
9223 			break;
9224 		}
9225 
9226 		FCP_DTRACE(fcp_logq, pptr->port_instbuf,
9227 		    fcp_trace, FCP_BUF_LEVEL_9, 0,
9228 		    "!FC error on cmd=%p target=0x%x: pkt state=0x%x "
9229 		    " pkt reason=0x%x", cmd, ptgt->tgt_d_id, fpkt->pkt_state,
9230 		    fpkt->pkt_reason);
9231 	}
9232 
9233 	ASSERT(save.a_hba_tran == pkt->pkt_address.a_hba_tran);
9234 }
9235 
9236 
9237 static int
9238 fcp_validate_fcp_response(struct fcp_rsp *rsp, struct fcp_port *pptr)
9239 {
9240 	if (rsp->reserved_0 || rsp->reserved_1 ||
9241 	    rsp->fcp_u.fcp_status.reserved_0 ||
9242 	    rsp->fcp_u.fcp_status.reserved_1) {
9243 		/*
9244 		 * These reserved fields should ideally be zero. FCP-2 does say
9245 		 * that the recipient need not check for reserved fields to be
9246 		 * zero. If they are not zero, we will not make a fuss about it
9247 		 * - just log it (in debug to both trace buffer and messages
9248 		 * file and to trace buffer only in non-debug) and move on.
9249 		 *
9250 		 * Non-zero reserved fields were seen with minnows.
9251 		 *
9252 		 * qlc takes care of some of this but we cannot assume that all
9253 		 * FCAs will do so.
9254 		 */
9255 		FCP_TRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
9256 		    FCP_BUF_LEVEL_5, 0,
9257 		    "Got fcp response packet with non-zero reserved fields "
9258 		    "rsp->reserved_0:0x%x, rsp_reserved_1:0x%x, "
9259 		    "status.reserved_0:0x%x, status.reserved_1:0x%x",
9260 		    rsp->reserved_0, rsp->reserved_1,
9261 		    rsp->fcp_u.fcp_status.reserved_0,
9262 		    rsp->fcp_u.fcp_status.reserved_1);
9263 	}
9264 
9265 	if (rsp->fcp_u.fcp_status.rsp_len_set && (rsp->fcp_response_len >
9266 	    (FCP_MAX_RSP_IU_SIZE - sizeof (struct fcp_rsp)))) {
9267 		return (FC_FAILURE);
9268 	}
9269 
9270 	if (rsp->fcp_u.fcp_status.sense_len_set && rsp->fcp_sense_len >
9271 	    (FCP_MAX_RSP_IU_SIZE - rsp->fcp_response_len -
9272 	    sizeof (struct fcp_rsp))) {
9273 		return (FC_FAILURE);
9274 	}
9275 
9276 	return (FC_SUCCESS);
9277 }
9278 
9279 
9280 /*
9281  * This is called when there is a change the in device state. The case we're
9282  * handling here is, if the d_id s does not match, offline this tgt and online
9283  * a new tgt with the new d_id.	 called from fcp_handle_devices with
9284  * port_mutex held.
9285  */
9286 static int
9287 fcp_device_changed(struct fcp_port *pptr, struct fcp_tgt *ptgt,
9288     fc_portmap_t *map_entry, int link_cnt, int tgt_cnt, int cause)
9289 {
9290 	ASSERT(mutex_owned(&pptr->port_mutex));
9291 
9292 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
9293 	    fcp_trace, FCP_BUF_LEVEL_3, 0,
9294 	    "Starting fcp_device_changed...");
9295 
9296 	/*
9297 	 * The two cases where the port_device_changed is called is
9298 	 * either it changes it's d_id or it's hard address.
9299 	 */
9300 	if ((ptgt->tgt_d_id != map_entry->map_did.port_id) ||
9301 	    (FC_TOP_EXTERNAL(pptr->port_topology) &&
9302 	    (ptgt->tgt_hard_addr != map_entry->map_hard_addr.hard_addr))) {
9303 
9304 		/* offline this target */
9305 		mutex_enter(&ptgt->tgt_mutex);
9306 		if (!(ptgt->tgt_state & FCP_TGT_OFFLINE)) {
9307 			(void) fcp_offline_target(pptr, ptgt, link_cnt,
9308 			    0, 1, NDI_DEVI_REMOVE);
9309 		}
9310 		mutex_exit(&ptgt->tgt_mutex);
9311 
9312 		fcp_log(CE_NOTE, pptr->port_dip,
9313 		    "Change in target properties: Old D_ID=%x New D_ID=%x"
9314 		    " Old HA=%x New HA=%x", ptgt->tgt_d_id,
9315 		    map_entry->map_did.port_id, ptgt->tgt_hard_addr,
9316 		    map_entry->map_hard_addr.hard_addr);
9317 	}
9318 
9319 	return (fcp_handle_mapflags(pptr, ptgt, map_entry,
9320 	    link_cnt, tgt_cnt, cause));
9321 }
9322 
9323 /*
9324  *     Function: fcp_alloc_lun
9325  *
9326  *  Description: Creates a new lun structure and adds it to the list
9327  *		 of luns of the target.
9328  *
9329  *     Argument: ptgt		Target the lun will belong to.
9330  *
9331  * Return Value: NULL		Failed
9332  *		 Not NULL	Succeeded
9333  *
9334  *	Context: Kernel context
9335  */
9336 static struct fcp_lun *
9337 fcp_alloc_lun(struct fcp_tgt *ptgt)
9338 {
9339 	struct fcp_lun *plun;
9340 
9341 	plun = kmem_zalloc(sizeof (struct fcp_lun), KM_NOSLEEP);
9342 	if (plun != NULL) {
9343 		/*
9344 		 * Initialize the mutex before putting in the target list
9345 		 * especially before releasing the target mutex.
9346 		 */
9347 		mutex_init(&plun->lun_mutex, NULL, MUTEX_DRIVER, NULL);
9348 		plun->lun_tgt = ptgt;
9349 
9350 		mutex_enter(&ptgt->tgt_mutex);
9351 		plun->lun_next = ptgt->tgt_lun;
9352 		ptgt->tgt_lun = plun;
9353 		plun->lun_old_guid = NULL;
9354 		plun->lun_old_guid_size = 0;
9355 		mutex_exit(&ptgt->tgt_mutex);
9356 	}
9357 
9358 	return (plun);
9359 }
9360 
9361 /*
9362  *     Function: fcp_dealloc_lun
9363  *
9364  *  Description: Frees the LUN structure passed by the caller.
9365  *
9366  *     Argument: plun		LUN structure to free.
9367  *
9368  * Return Value: None
9369  *
9370  *	Context: Kernel context.
9371  */
9372 static void
9373 fcp_dealloc_lun(struct fcp_lun *plun)
9374 {
9375 	mutex_enter(&plun->lun_mutex);
9376 	if (plun->lun_cip) {
9377 		fcp_remove_child(plun);
9378 	}
9379 	mutex_exit(&plun->lun_mutex);
9380 
9381 	mutex_destroy(&plun->lun_mutex);
9382 	if (plun->lun_guid) {
9383 		kmem_free(plun->lun_guid, plun->lun_guid_size);
9384 	}
9385 	if (plun->lun_old_guid) {
9386 		kmem_free(plun->lun_old_guid, plun->lun_old_guid_size);
9387 	}
9388 	kmem_free(plun, sizeof (*plun));
9389 }
9390 
9391 /*
9392  *     Function: fcp_alloc_tgt
9393  *
9394  *  Description: Creates a new target structure and adds it to the port
9395  *		 hash list.
9396  *
9397  *     Argument: pptr		fcp port structure
9398  *		 *map_entry	entry describing the target to create
9399  *		 link_cnt	Link state change counter
9400  *
9401  * Return Value: NULL		Failed
9402  *		 Not NULL	Succeeded
9403  *
9404  *	Context: Kernel context.
9405  */
9406 static struct fcp_tgt *
9407 fcp_alloc_tgt(struct fcp_port *pptr, fc_portmap_t *map_entry, int link_cnt)
9408 {
9409 	int			hash;
9410 	uchar_t			*wwn;
9411 	struct fcp_tgt	*ptgt;
9412 
9413 	ptgt = kmem_zalloc(sizeof (*ptgt), KM_NOSLEEP);
9414 	if (ptgt != NULL) {
9415 		mutex_enter(&pptr->port_mutex);
9416 		if (link_cnt != pptr->port_link_cnt) {
9417 			/*
9418 			 * oh oh -- another link reset
9419 			 * in progress -- give up
9420 			 */
9421 			mutex_exit(&pptr->port_mutex);
9422 			kmem_free(ptgt, sizeof (*ptgt));
9423 			ptgt = NULL;
9424 		} else {
9425 			/*
9426 			 * initialize the mutex before putting in the port
9427 			 * wwn list, especially before releasing the port
9428 			 * mutex.
9429 			 */
9430 			mutex_init(&ptgt->tgt_mutex, NULL, MUTEX_DRIVER, NULL);
9431 
9432 			/* add new target entry to the port's hash list */
9433 			wwn = (uchar_t *)&map_entry->map_pwwn;
9434 			hash = FCP_HASH(wwn);
9435 
9436 			ptgt->tgt_next = pptr->port_tgt_hash_table[hash];
9437 			pptr->port_tgt_hash_table[hash] = ptgt;
9438 
9439 			/* save cross-ptr */
9440 			ptgt->tgt_port = pptr;
9441 
9442 			ptgt->tgt_change_cnt = 1;
9443 
9444 			/* initialize the target manual_config_only flag */
9445 			if (fcp_enable_auto_configuration) {
9446 				ptgt->tgt_manual_config_only = 0;
9447 			} else {
9448 				ptgt->tgt_manual_config_only = 1;
9449 			}
9450 
9451 			mutex_exit(&pptr->port_mutex);
9452 		}
9453 	}
9454 
9455 	return (ptgt);
9456 }
9457 
9458 /*
9459  *     Function: fcp_dealloc_tgt
9460  *
9461  *  Description: Frees the target structure passed by the caller.
9462  *
9463  *     Argument: ptgt		Target structure to free.
9464  *
9465  * Return Value: None
9466  *
9467  *	Context: Kernel context.
9468  */
9469 static void
9470 fcp_dealloc_tgt(struct fcp_tgt *ptgt)
9471 {
9472 	mutex_destroy(&ptgt->tgt_mutex);
9473 	kmem_free(ptgt, sizeof (*ptgt));
9474 }
9475 
9476 
9477 /*
9478  * Handle STATUS_QFULL and STATUS_BUSY by performing delayed retry
9479  *
9480  *	Device discovery commands will not be retried for-ever as
9481  *	this will have repercussions on other devices that need to
9482  *	be submitted to the hotplug thread. After a quick glance
9483  *	at the SCSI-3 spec, it was found that the spec doesn't
9484  *	mandate a forever retry, rather recommends a delayed retry.
9485  *
9486  *	Since Photon IB is single threaded, STATUS_BUSY is common
9487  *	in a 4+initiator environment. Make sure the total time
9488  *	spent on retries (including command timeout) does not
9489  *	60 seconds
9490  */
9491 static void
9492 fcp_queue_ipkt(struct fcp_port *pptr, fc_packet_t *fpkt)
9493 {
9494 	struct fcp_ipkt *icmd = (struct fcp_ipkt *)fpkt->pkt_ulp_private;
9495 	struct fcp_tgt *ptgt = icmd->ipkt_tgt;
9496 
9497 	mutex_enter(&pptr->port_mutex);
9498 	mutex_enter(&ptgt->tgt_mutex);
9499 	if (FCP_STATE_CHANGED(pptr, ptgt, icmd)) {
9500 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
9501 		    fcp_trace, FCP_BUF_LEVEL_2, 0,
9502 		    "fcp_queue_ipkt,1:state change occured"
9503 		    " for D_ID=0x%x", ptgt->tgt_d_id);
9504 		mutex_exit(&ptgt->tgt_mutex);
9505 		mutex_exit(&pptr->port_mutex);
9506 		(void) fcp_call_finish_init(pptr, ptgt, icmd->ipkt_link_cnt,
9507 		    icmd->ipkt_change_cnt, icmd->ipkt_cause);
9508 		fcp_icmd_free(pptr, icmd);
9509 		return;
9510 	}
9511 	mutex_exit(&ptgt->tgt_mutex);
9512 
9513 	icmd->ipkt_restart = fcp_watchdog_time + icmd->ipkt_retries++;
9514 
9515 	if (pptr->port_ipkt_list != NULL) {
9516 		/* add pkt to front of doubly-linked list */
9517 		pptr->port_ipkt_list->ipkt_prev = icmd;
9518 		icmd->ipkt_next = pptr->port_ipkt_list;
9519 		pptr->port_ipkt_list = icmd;
9520 		icmd->ipkt_prev = NULL;
9521 	} else {
9522 		/* this is the first/only pkt on the list */
9523 		pptr->port_ipkt_list = icmd;
9524 		icmd->ipkt_next = NULL;
9525 		icmd->ipkt_prev = NULL;
9526 	}
9527 	mutex_exit(&pptr->port_mutex);
9528 }
9529 
9530 /*
9531  *     Function: fcp_transport
9532  *
9533  *  Description: This function submits the Fibre Channel packet to the transort
9534  *		 layer by calling fc_ulp_transport().  If fc_ulp_transport()
9535  *		 fails the submission, the treatment depends on the value of
9536  *		 the variable internal.
9537  *
9538  *     Argument: port_handle	fp/fctl port handle.
9539  *		 *fpkt		Packet to submit to the transport layer.
9540  *		 internal	Not zero when it's an internal packet.
9541  *
9542  * Return Value: FC_TRAN_BUSY
9543  *		 FC_STATEC_BUSY
9544  *		 FC_OFFLINE
9545  *		 FC_LOGINREQ
9546  *		 FC_DEVICE_BUSY
9547  *		 FC_SUCCESS
9548  */
9549 static int
9550 fcp_transport(opaque_t port_handle, fc_packet_t *fpkt, int internal)
9551 {
9552 	int	rval;
9553 
9554 	rval = fc_ulp_transport(port_handle, fpkt);
9555 	if (rval == FC_SUCCESS) {
9556 		return (rval);
9557 	}
9558 
9559 	/*
9560 	 * LUN isn't marked BUSY or OFFLINE, so we got here to transport
9561 	 * a command, if the underlying modules see that there is a state
9562 	 * change, or if a port is OFFLINE, that means, that state change
9563 	 * hasn't reached FCP yet, so re-queue the command for deferred
9564 	 * submission.
9565 	 */
9566 	if ((rval == FC_STATEC_BUSY) || (rval == FC_OFFLINE) ||
9567 	    (rval == FC_LOGINREQ) || (rval == FC_DEVICE_BUSY) ||
9568 	    (rval == FC_DEVICE_BUSY_NEW_RSCN) || (rval == FC_TRAN_BUSY)) {
9569 		/*
9570 		 * Defer packet re-submission. Life hang is possible on
9571 		 * internal commands if the port driver sends FC_STATEC_BUSY
9572 		 * for ever, but that shouldn't happen in a good environment.
9573 		 * Limiting re-transport for internal commands is probably a
9574 		 * good idea..
9575 		 * A race condition can happen when a port sees barrage of
9576 		 * link transitions offline to online. If the FCTL has
9577 		 * returned FC_STATEC_BUSY or FC_OFFLINE then none of the
9578 		 * internal commands should be queued to do the discovery.
9579 		 * The race condition is when an online comes and FCP starts
9580 		 * its internal discovery and the link goes offline. It is
9581 		 * possible that the statec_callback has not reached FCP
9582 		 * and FCP is carrying on with its internal discovery.
9583 		 * FC_STATEC_BUSY or FC_OFFLINE will be the first indication
9584 		 * that the link has gone offline. At this point FCP should
9585 		 * drop all the internal commands and wait for the
9586 		 * statec_callback. It will be facilitated by incrementing
9587 		 * port_link_cnt.
9588 		 *
9589 		 * For external commands, the (FC)pkt_timeout is decremented
9590 		 * by the QUEUE Delay added by our driver, Care is taken to
9591 		 * ensure that it doesn't become zero (zero means no timeout)
9592 		 * If the time expires right inside driver queue itself,
9593 		 * the watch thread will return it to the original caller
9594 		 * indicating that the command has timed-out.
9595 		 */
9596 		if (internal) {
9597 			char			*op;
9598 			struct fcp_ipkt	*icmd;
9599 
9600 			icmd = (struct fcp_ipkt *)fpkt->pkt_ulp_private;
9601 			switch (icmd->ipkt_opcode) {
9602 			case SCMD_REPORT_LUN:
9603 				op = "REPORT LUN";
9604 				break;
9605 
9606 			case SCMD_INQUIRY:
9607 				op = "INQUIRY";
9608 				break;
9609 
9610 			case SCMD_INQUIRY_PAGE83:
9611 				op = "INQUIRY-83";
9612 				break;
9613 
9614 			default:
9615 				op = "Internal SCSI COMMAND";
9616 				break;
9617 			}
9618 
9619 			if (fcp_handle_ipkt_errors(icmd->ipkt_port,
9620 			    icmd->ipkt_tgt, icmd, rval, op) == DDI_SUCCESS) {
9621 				rval = FC_SUCCESS;
9622 			}
9623 		} else {
9624 			struct fcp_pkt *cmd;
9625 			struct fcp_port *pptr;
9626 
9627 			cmd = (struct fcp_pkt *)fpkt->pkt_ulp_private;
9628 			cmd->cmd_state = FCP_PKT_IDLE;
9629 			pptr = ADDR2FCP(&cmd->cmd_pkt->pkt_address);
9630 
9631 			if (cmd->cmd_pkt->pkt_flags & FLAG_NOQUEUE) {
9632 				FCP_DTRACE(fcp_logq, pptr->port_instbuf,
9633 				    fcp_trace, FCP_BUF_LEVEL_9, 0,
9634 				    "fcp_transport: xport busy for pkt %p",
9635 				    cmd->cmd_pkt);
9636 				rval = FC_TRAN_BUSY;
9637 			} else {
9638 				fcp_queue_pkt(pptr, cmd);
9639 				rval = FC_SUCCESS;
9640 			}
9641 		}
9642 	}
9643 
9644 	return (rval);
9645 }
9646 
9647 /*VARARGS3*/
9648 static void
9649 fcp_log(int level, dev_info_t *dip, const char *fmt, ...)
9650 {
9651 	char		buf[256];
9652 	va_list		ap;
9653 
9654 	if (dip == NULL) {
9655 		dip = fcp_global_dip;
9656 	}
9657 
9658 	va_start(ap, fmt);
9659 	(void) vsprintf(buf, fmt, ap);
9660 	va_end(ap);
9661 
9662 	scsi_log(dip, "fcp", level, buf);
9663 }
9664 
9665 /*
9666  * This function retries NS registry of FC4 type.
9667  * It assumes that fcp_mutex is held.
9668  * The function does nothing if topology is not fabric
9669  * So, the topology has to be set before this function can be called
9670  */
9671 static void
9672 fcp_retry_ns_registry(struct fcp_port *pptr, uint32_t s_id)
9673 {
9674 	int	rval;
9675 
9676 	ASSERT(MUTEX_HELD(&pptr->port_mutex));
9677 
9678 	if (((pptr->port_state & FCP_STATE_NS_REG_FAILED) == 0) ||
9679 	    ((pptr->port_topology != FC_TOP_FABRIC) &&
9680 	    (pptr->port_topology != FC_TOP_PUBLIC_LOOP))) {
9681 		if (pptr->port_state & FCP_STATE_NS_REG_FAILED) {
9682 			pptr->port_state &= ~FCP_STATE_NS_REG_FAILED;
9683 		}
9684 		return;
9685 	}
9686 	mutex_exit(&pptr->port_mutex);
9687 	rval = fcp_do_ns_registry(pptr, s_id);
9688 	mutex_enter(&pptr->port_mutex);
9689 
9690 	if (rval == 0) {
9691 		/* Registry successful. Reset flag */
9692 		pptr->port_state &= ~(FCP_STATE_NS_REG_FAILED);
9693 	}
9694 }
9695 
9696 /*
9697  * This function registers the ULP with the switch by calling transport i/f
9698  */
9699 static int
9700 fcp_do_ns_registry(struct fcp_port *pptr, uint32_t s_id)
9701 {
9702 	fc_ns_cmd_t		ns_cmd;
9703 	ns_rfc_type_t		rfc;
9704 	uint32_t		types[8];
9705 
9706 	/*
9707 	 * Prepare the Name server structure to
9708 	 * register with the transport in case of
9709 	 * Fabric configuration.
9710 	 */
9711 	bzero(&rfc, sizeof (rfc));
9712 	bzero(types, sizeof (types));
9713 
9714 	types[FC4_TYPE_WORD_POS(FC_TYPE_SCSI_FCP)] =
9715 	    (1 << FC4_TYPE_BIT_POS(FC_TYPE_SCSI_FCP));
9716 
9717 	rfc.rfc_port_id.port_id = s_id;
9718 	bcopy(types, rfc.rfc_types, sizeof (types));
9719 
9720 	ns_cmd.ns_flags = 0;
9721 	ns_cmd.ns_cmd = NS_RFT_ID;
9722 	ns_cmd.ns_req_len = sizeof (rfc);
9723 	ns_cmd.ns_req_payload = (caddr_t)&rfc;
9724 	ns_cmd.ns_resp_len = 0;
9725 	ns_cmd.ns_resp_payload = NULL;
9726 
9727 	/*
9728 	 * Perform the Name Server Registration for SCSI_FCP FC4 Type.
9729 	 */
9730 	if (fc_ulp_port_ns(pptr->port_fp_handle, NULL, &ns_cmd)) {
9731 		fcp_log(CE_WARN, pptr->port_dip,
9732 		    "!ns_registry: failed name server registration");
9733 		return (1);
9734 	}
9735 
9736 	return (0);
9737 }
9738 
9739 /*
9740  *     Function: fcp_handle_port_attach
9741  *
9742  *  Description: This function is called from fcp_port_attach() to attach a
9743  *		 new port. This routine does the following:
9744  *
9745  *		1) Allocates an fcp_port structure and initializes it.
9746  *		2) Tries to register the new FC-4 (FCP) capablity with the name
9747  *		   server.
9748  *		3) Kicks off the enumeration of the targets/luns visible
9749  *		   through this new port.  That is done by calling
9750  *		   fcp_statec_callback() if the port is online.
9751  *
9752  *     Argument: ulph		fp/fctl port handle.
9753  *		 *pinfo		Port information.
9754  *		 s_id		Port ID.
9755  *		 instance	Device instance number for the local port
9756  *				(returned by ddi_get_instance()).
9757  *
9758  * Return Value: DDI_SUCCESS
9759  *		 DDI_FAILURE
9760  *
9761  *	Context: User and Kernel context.
9762  */
9763 /*ARGSUSED*/
9764 int
9765 fcp_handle_port_attach(opaque_t ulph, fc_ulp_port_info_t *pinfo,
9766     uint32_t s_id, int instance)
9767 {
9768 	int			res = DDI_FAILURE;
9769 	scsi_hba_tran_t		*tran;
9770 	int			mutex_initted = FALSE;
9771 	int			hba_attached = FALSE;
9772 	int			soft_state_linked = FALSE;
9773 	int			event_bind = FALSE;
9774 	struct fcp_port		*pptr;
9775 	fc_portmap_t		*tmp_list = NULL;
9776 	uint32_t		max_cnt, alloc_cnt;
9777 	uchar_t			*boot_wwn = NULL;
9778 	uint_t			nbytes;
9779 	int			manual_cfg;
9780 
9781 	/*
9782 	 * this port instance attaching for the first time (or after
9783 	 * being detached before)
9784 	 */
9785 	FCP_TRACE(fcp_logq, "fcp", fcp_trace,
9786 	    FCP_BUF_LEVEL_3, 0, "port attach: for port %d", instance);
9787 
9788 	if (ddi_soft_state_zalloc(fcp_softstate, instance) != DDI_SUCCESS) {
9789 		cmn_err(CE_WARN, "fcp: Softstate struct alloc failed"
9790 		    "parent dip: %p; instance: %d", (void *)pinfo->port_dip,
9791 		    instance);
9792 		return (res);
9793 	}
9794 
9795 	if ((pptr = ddi_get_soft_state(fcp_softstate, instance)) == NULL) {
9796 		/* this shouldn't happen */
9797 		ddi_soft_state_free(fcp_softstate, instance);
9798 		cmn_err(CE_WARN, "fcp: bad soft state");
9799 		return (res);
9800 	}
9801 
9802 	(void) sprintf(pptr->port_instbuf, "fcp(%d)", instance);
9803 
9804 	/*
9805 	 * Make a copy of ulp_port_info as fctl allocates
9806 	 * a temp struct.
9807 	 */
9808 	(void) fcp_cp_pinfo(pptr, pinfo);
9809 
9810 	/*
9811 	 * Check for manual_configuration_only property.
9812 	 * Enable manual configurtion if the property is
9813 	 * set to 1, otherwise disable manual configuration.
9814 	 */
9815 	if ((manual_cfg = ddi_prop_get_int(DDI_DEV_T_ANY, pptr->port_dip,
9816 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
9817 	    MANUAL_CFG_ONLY,
9818 	    -1)) != -1) {
9819 		if (manual_cfg == 1) {
9820 			char	*pathname;
9821 			pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
9822 			(void) ddi_pathname(pptr->port_dip, pathname);
9823 			cmn_err(CE_NOTE,
9824 			    "%s (%s%d) %s is enabled via %s.conf.",
9825 			    pathname,
9826 			    ddi_driver_name(pptr->port_dip),
9827 			    ddi_get_instance(pptr->port_dip),
9828 			    MANUAL_CFG_ONLY,
9829 			    ddi_driver_name(pptr->port_dip));
9830 			fcp_enable_auto_configuration = 0;
9831 			kmem_free(pathname, MAXPATHLEN);
9832 		}
9833 	}
9834 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(pptr->port_link_cnt));
9835 	pptr->port_link_cnt = 1;
9836 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(pptr->port_link_cnt));
9837 	pptr->port_id = s_id;
9838 	pptr->port_instance = instance;
9839 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(pptr->port_state));
9840 	pptr->port_state = FCP_STATE_INIT;
9841 	if (pinfo->port_acc_attr == NULL) {
9842 		/*
9843 		 * The corresponding FCA doesn't support DMA at all
9844 		 */
9845 		pptr->port_state |= FCP_STATE_FCA_IS_NODMA;
9846 	}
9847 
9848 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(pptr->port_state));
9849 
9850 	if (!(pptr->port_state & FCP_STATE_FCA_IS_NODMA)) {
9851 		/*
9852 		 * If FCA supports DMA in SCSI data phase, we need preallocate
9853 		 * dma cookie, so stash the cookie size
9854 		 */
9855 		pptr->port_dmacookie_sz = sizeof (ddi_dma_cookie_t) *
9856 		    pptr->port_data_dma_attr.dma_attr_sgllen;
9857 	}
9858 
9859 	/*
9860 	 * The two mutexes of fcp_port are initialized.	 The variable
9861 	 * mutex_initted is incremented to remember that fact.	That variable
9862 	 * is checked when the routine fails and the mutexes have to be
9863 	 * destroyed.
9864 	 */
9865 	mutex_init(&pptr->port_mutex, NULL, MUTEX_DRIVER, NULL);
9866 	mutex_init(&pptr->port_pkt_mutex, NULL, MUTEX_DRIVER, NULL);
9867 	mutex_initted++;
9868 
9869 	/*
9870 	 * The SCSI tran structure is allocate and initialized now.
9871 	 */
9872 	if ((tran = scsi_hba_tran_alloc(pptr->port_dip, 0)) == NULL) {
9873 		fcp_log(CE_WARN, pptr->port_dip,
9874 		    "!fcp%d: scsi_hba_tran_alloc failed", instance);
9875 		goto fail;
9876 	}
9877 
9878 	/* link in the transport structure then fill it in */
9879 	pptr->port_tran = tran;
9880 	tran->tran_hba_private		= pptr;
9881 	tran->tran_tgt_init		= fcp_scsi_tgt_init;
9882 	tran->tran_tgt_probe		= NULL;
9883 	tran->tran_tgt_free		= fcp_scsi_tgt_free;
9884 	tran->tran_start		= fcp_scsi_start;
9885 	tran->tran_reset		= fcp_scsi_reset;
9886 	tran->tran_abort		= fcp_scsi_abort;
9887 	tran->tran_getcap		= fcp_scsi_getcap;
9888 	tran->tran_setcap		= fcp_scsi_setcap;
9889 	tran->tran_init_pkt		= NULL;
9890 	tran->tran_destroy_pkt		= NULL;
9891 	tran->tran_dmafree		= NULL;
9892 	tran->tran_sync_pkt		= NULL;
9893 	tran->tran_reset_notify		= fcp_scsi_reset_notify;
9894 	tran->tran_get_bus_addr		= fcp_scsi_get_bus_addr;
9895 	tran->tran_get_name		= fcp_scsi_get_name;
9896 	tran->tran_clear_aca		= NULL;
9897 	tran->tran_clear_task_set	= NULL;
9898 	tran->tran_terminate_task	= NULL;
9899 	tran->tran_get_eventcookie	= fcp_scsi_bus_get_eventcookie;
9900 	tran->tran_add_eventcall	= fcp_scsi_bus_add_eventcall;
9901 	tran->tran_remove_eventcall	= fcp_scsi_bus_remove_eventcall;
9902 	tran->tran_post_event		= fcp_scsi_bus_post_event;
9903 	tran->tran_quiesce		= NULL;
9904 	tran->tran_unquiesce		= NULL;
9905 	tran->tran_bus_reset		= NULL;
9906 	tran->tran_bus_config		= fcp_scsi_bus_config;
9907 	tran->tran_bus_unconfig		= fcp_scsi_bus_unconfig;
9908 	tran->tran_bus_power		= NULL;
9909 	tran->tran_interconnect_type	= INTERCONNECT_FABRIC;
9910 
9911 	tran->tran_pkt_constructor	= fcp_kmem_cache_constructor;
9912 	tran->tran_pkt_destructor	= fcp_kmem_cache_destructor;
9913 	tran->tran_setup_pkt		= fcp_pkt_setup;
9914 	tran->tran_teardown_pkt		= fcp_pkt_teardown;
9915 	tran->tran_hba_len		= pptr->port_priv_pkt_len +
9916 	    sizeof (struct fcp_pkt) + pptr->port_dmacookie_sz;
9917 	if (pptr->port_state & FCP_STATE_FCA_IS_NODMA) {
9918 		/*
9919 		 * If FCA don't support DMA, then we use different vectors to
9920 		 * minimize the effects on DMA code flow path
9921 		 */
9922 		tran->tran_start	   = fcp_pseudo_start;
9923 		tran->tran_init_pkt	   = fcp_pseudo_init_pkt;
9924 		tran->tran_destroy_pkt	   = fcp_pseudo_destroy_pkt;
9925 		tran->tran_sync_pkt	   = fcp_pseudo_sync_pkt;
9926 		tran->tran_dmafree	   = fcp_pseudo_dmafree;
9927 		tran->tran_setup_pkt	   = NULL;
9928 		tran->tran_teardown_pkt	   = NULL;
9929 		tran->tran_pkt_constructor = NULL;
9930 		tran->tran_pkt_destructor  = NULL;
9931 		pptr->port_data_dma_attr   = pseudo_fca_dma_attr;
9932 	}
9933 
9934 	/*
9935 	 * Allocate an ndi event handle
9936 	 */
9937 	pptr->port_ndi_event_defs = (ndi_event_definition_t *)
9938 	    kmem_zalloc(sizeof (fcp_ndi_event_defs), KM_SLEEP);
9939 
9940 	bcopy(fcp_ndi_event_defs, pptr->port_ndi_event_defs,
9941 	    sizeof (fcp_ndi_event_defs));
9942 
9943 	(void) ndi_event_alloc_hdl(pptr->port_dip, NULL,
9944 	    &pptr->port_ndi_event_hdl, NDI_SLEEP);
9945 
9946 	pptr->port_ndi_events.ndi_events_version = NDI_EVENTS_REV1;
9947 	pptr->port_ndi_events.ndi_n_events = FCP_N_NDI_EVENTS;
9948 	pptr->port_ndi_events.ndi_event_defs = pptr->port_ndi_event_defs;
9949 
9950 	if (DEVI_IS_ATTACHING(pptr->port_dip) &&
9951 	    (ndi_event_bind_set(pptr->port_ndi_event_hdl,
9952 	    &pptr->port_ndi_events, NDI_SLEEP) != NDI_SUCCESS)) {
9953 		goto fail;
9954 	}
9955 	event_bind++;	/* Checked in fail case */
9956 
9957 	if (scsi_hba_attach_setup(pptr->port_dip, &pptr->port_data_dma_attr,
9958 	    tran, SCSI_HBA_ADDR_COMPLEX | SCSI_HBA_TRAN_SCB)
9959 	    != DDI_SUCCESS) {
9960 		fcp_log(CE_WARN, pptr->port_dip,
9961 		    "!fcp%d: scsi_hba_attach_setup failed", instance);
9962 		goto fail;
9963 	}
9964 	hba_attached++;	/* Checked in fail case */
9965 
9966 	pptr->port_mpxio = 0;
9967 	if (mdi_phci_register(MDI_HCI_CLASS_SCSI, pptr->port_dip, 0) ==
9968 	    MDI_SUCCESS) {
9969 		pptr->port_mpxio++;
9970 	}
9971 
9972 	/*
9973 	 * The following code is putting the new port structure in the global
9974 	 * list of ports and, if it is the first port to attach, it start the
9975 	 * fcp_watchdog_tick.
9976 	 *
9977 	 * Why put this new port in the global before we are done attaching it?
9978 	 * We are actually making the structure globally known before we are
9979 	 * done attaching it.  The reason for that is: because of the code that
9980 	 * follows.  At this point the resources to handle the port are
9981 	 * allocated.  This function is now going to do the following:
9982 	 *
9983 	 *   1) It is going to try to register with the name server advertizing
9984 	 *	the new FCP capability of the port.
9985 	 *   2) It is going to play the role of the fp/fctl layer by building
9986 	 *	a list of worlwide names reachable through this port and call
9987 	 *	itself on fcp_statec_callback().  That requires the port to
9988 	 *	be part of the global list.
9989 	 */
9990 	mutex_enter(&fcp_global_mutex);
9991 	if (fcp_port_head == NULL) {
9992 		fcp_read_blacklist(pinfo->port_dip, &fcp_lun_blacklist);
9993 	}
9994 	pptr->port_next = fcp_port_head;
9995 	fcp_port_head = pptr;
9996 	soft_state_linked++;
9997 
9998 	if (fcp_watchdog_init++ == 0) {
9999 		fcp_watchdog_tick = fcp_watchdog_timeout *
10000 		    drv_usectohz(1000000);
10001 		fcp_watchdog_id = timeout(fcp_watch, NULL,
10002 		    fcp_watchdog_tick);
10003 	}
10004 	mutex_exit(&fcp_global_mutex);
10005 
10006 	/*
10007 	 * Here an attempt is made to register with the name server, the new
10008 	 * FCP capability.  That is done using an RTF_ID to the name server.
10009 	 * It is done synchronously.  The function fcp_do_ns_registry()
10010 	 * doesn't return till the name server responded.
10011 	 * On failures, just ignore it for now and it will get retried during
10012 	 * state change callbacks. We'll set a flag to show this failure
10013 	 */
10014 	if (fcp_do_ns_registry(pptr, s_id)) {
10015 		mutex_enter(&pptr->port_mutex);
10016 		pptr->port_state |= FCP_STATE_NS_REG_FAILED;
10017 		mutex_exit(&pptr->port_mutex);
10018 	} else {
10019 		mutex_enter(&pptr->port_mutex);
10020 		pptr->port_state &= ~(FCP_STATE_NS_REG_FAILED);
10021 		mutex_exit(&pptr->port_mutex);
10022 	}
10023 
10024 	/*
10025 	 * Lookup for boot WWN property
10026 	 */
10027 	if (modrootloaded != 1) {
10028 		if ((ddi_prop_lookup_byte_array(DDI_DEV_T_ANY,
10029 		    ddi_get_parent(pinfo->port_dip),
10030 		    DDI_PROP_DONTPASS, OBP_BOOT_WWN,
10031 		    &boot_wwn, &nbytes) == DDI_PROP_SUCCESS) &&
10032 		    (nbytes == FC_WWN_SIZE)) {
10033 			bcopy(boot_wwn, pptr->port_boot_wwn, FC_WWN_SIZE);
10034 		}
10035 		if (boot_wwn) {
10036 			ddi_prop_free(boot_wwn);
10037 		}
10038 	}
10039 
10040 	/*
10041 	 * Handle various topologies and link states.
10042 	 */
10043 	switch (FC_PORT_STATE_MASK(pptr->port_phys_state)) {
10044 	case FC_STATE_OFFLINE:
10045 
10046 		/*
10047 		 * we're attaching a port where the link is offline
10048 		 *
10049 		 * Wait for ONLINE, at which time a state
10050 		 * change will cause a statec_callback
10051 		 *
10052 		 * in the mean time, do not do anything
10053 		 */
10054 		res = DDI_SUCCESS;
10055 		pptr->port_state |= FCP_STATE_OFFLINE;
10056 		break;
10057 
10058 	case FC_STATE_ONLINE: {
10059 		if (pptr->port_topology == FC_TOP_UNKNOWN) {
10060 			(void) fcp_linkreset(pptr, NULL, KM_NOSLEEP);
10061 			res = DDI_SUCCESS;
10062 			break;
10063 		}
10064 		/*
10065 		 * discover devices and create nodes (a private
10066 		 * loop or point-to-point)
10067 		 */
10068 		ASSERT(pptr->port_topology != FC_TOP_UNKNOWN);
10069 
10070 		/*
10071 		 * At this point we are going to build a list of all the ports
10072 		 * that	can be reached through this local port.	 It looks like
10073 		 * we cannot handle more than FCP_MAX_DEVICES per local port
10074 		 * (128).
10075 		 */
10076 		if ((tmp_list = (fc_portmap_t *)kmem_zalloc(
10077 		    sizeof (fc_portmap_t) * FCP_MAX_DEVICES,
10078 		    KM_NOSLEEP)) == NULL) {
10079 			fcp_log(CE_WARN, pptr->port_dip,
10080 			    "!fcp%d: failed to allocate portmap",
10081 			    instance);
10082 			goto fail;
10083 		}
10084 
10085 		/*
10086 		 * fc_ulp_getportmap() is going to provide us with the list of
10087 		 * remote ports in the buffer we just allocated.  The way the
10088 		 * list is going to be retrieved depends on the topology.
10089 		 * However, if we are connected to a Fabric, a name server
10090 		 * request may be sent to get the list of FCP capable ports.
10091 		 * It should be noted that is the case the request is
10092 		 * synchronous.	 This means we are stuck here till the name
10093 		 * server replies.  A lot of things can change during that time
10094 		 * and including, may be, being called on
10095 		 * fcp_statec_callback() for different reasons. I'm not sure
10096 		 * the code can handle that.
10097 		 */
10098 		max_cnt = FCP_MAX_DEVICES;
10099 		alloc_cnt = FCP_MAX_DEVICES;
10100 		if ((res = fc_ulp_getportmap(pptr->port_fp_handle,
10101 		    &tmp_list, &max_cnt, FC_ULP_PLOGI_PRESERVE)) !=
10102 		    FC_SUCCESS) {
10103 			caddr_t msg;
10104 
10105 			(void) fc_ulp_error(res, &msg);
10106 
10107 			/*
10108 			 * this	 just means the transport is
10109 			 * busy perhaps building a portmap so,
10110 			 * for now, succeed this port attach
10111 			 * when the transport has a new map,
10112 			 * it'll send us a state change then
10113 			 */
10114 			fcp_log(CE_WARN, pptr->port_dip,
10115 			    "!failed to get port map : %s", msg);
10116 
10117 			res = DDI_SUCCESS;
10118 			break;	/* go return result */
10119 		}
10120 		if (max_cnt > alloc_cnt) {
10121 			alloc_cnt = max_cnt;
10122 		}
10123 
10124 		/*
10125 		 * We are now going to call fcp_statec_callback() ourselves.
10126 		 * By issuing this call we are trying to kick off the enumera-
10127 		 * tion process.
10128 		 */
10129 		/*
10130 		 * let the state change callback do the SCSI device
10131 		 * discovery and create the devinfos
10132 		 */
10133 		fcp_statec_callback(ulph, pptr->port_fp_handle,
10134 		    pptr->port_phys_state, pptr->port_topology, tmp_list,
10135 		    max_cnt, pptr->port_id);
10136 
10137 		res = DDI_SUCCESS;
10138 		break;
10139 	}
10140 
10141 	default:
10142 		/* unknown port state */
10143 		fcp_log(CE_WARN, pptr->port_dip,
10144 		    "!fcp%d: invalid port state at attach=0x%x",
10145 		    instance, pptr->port_phys_state);
10146 
10147 		mutex_enter(&pptr->port_mutex);
10148 		pptr->port_phys_state = FCP_STATE_OFFLINE;
10149 		mutex_exit(&pptr->port_mutex);
10150 
10151 		res = DDI_SUCCESS;
10152 		break;
10153 	}
10154 
10155 	/* free temp list if used */
10156 	if (tmp_list != NULL) {
10157 		kmem_free(tmp_list, sizeof (fc_portmap_t) * alloc_cnt);
10158 	}
10159 
10160 	/* note the attach time */
10161 	pptr->port_attach_time = lbolt64;
10162 
10163 	/* all done */
10164 	return (res);
10165 
10166 	/* a failure we have to clean up after */
10167 fail:
10168 	fcp_log(CE_WARN, pptr->port_dip, "!failed to attach to port");
10169 
10170 	if (soft_state_linked) {
10171 		/* remove this fcp_port from the linked list */
10172 		(void) fcp_soft_state_unlink(pptr);
10173 	}
10174 
10175 	/* unbind and free event set */
10176 	if (pptr->port_ndi_event_hdl) {
10177 		if (event_bind) {
10178 			(void) ndi_event_unbind_set(pptr->port_ndi_event_hdl,
10179 			    &pptr->port_ndi_events, NDI_SLEEP);
10180 		}
10181 		(void) ndi_event_free_hdl(pptr->port_ndi_event_hdl);
10182 	}
10183 
10184 	if (pptr->port_ndi_event_defs) {
10185 		(void) kmem_free(pptr->port_ndi_event_defs,
10186 		    sizeof (fcp_ndi_event_defs));
10187 	}
10188 
10189 	/*
10190 	 * Clean up mpxio stuff
10191 	 */
10192 	if (pptr->port_mpxio) {
10193 		(void) mdi_phci_unregister(pptr->port_dip, 0);
10194 		pptr->port_mpxio--;
10195 	}
10196 
10197 	/* undo SCSI HBA setup */
10198 	if (hba_attached) {
10199 		(void) scsi_hba_detach(pptr->port_dip);
10200 	}
10201 	if (pptr->port_tran != NULL) {
10202 		scsi_hba_tran_free(pptr->port_tran);
10203 	}
10204 
10205 	mutex_enter(&fcp_global_mutex);
10206 
10207 	/*
10208 	 * We check soft_state_linked, because it is incremented right before
10209 	 * we call increment fcp_watchdog_init.	 Therefore, we know if
10210 	 * soft_state_linked is still FALSE, we do not want to decrement
10211 	 * fcp_watchdog_init or possibly call untimeout.
10212 	 */
10213 
10214 	if (soft_state_linked) {
10215 		if (--fcp_watchdog_init == 0) {
10216 			timeout_id_t	tid = fcp_watchdog_id;
10217 
10218 			mutex_exit(&fcp_global_mutex);
10219 			(void) untimeout(tid);
10220 		} else {
10221 			mutex_exit(&fcp_global_mutex);
10222 		}
10223 	} else {
10224 		mutex_exit(&fcp_global_mutex);
10225 	}
10226 
10227 	if (mutex_initted) {
10228 		mutex_destroy(&pptr->port_mutex);
10229 		mutex_destroy(&pptr->port_pkt_mutex);
10230 	}
10231 
10232 	if (tmp_list != NULL) {
10233 		kmem_free(tmp_list, sizeof (fc_portmap_t) * alloc_cnt);
10234 	}
10235 
10236 	/* this makes pptr invalid */
10237 	ddi_soft_state_free(fcp_softstate, instance);
10238 
10239 	return (DDI_FAILURE);
10240 }
10241 
10242 
10243 static int
10244 fcp_handle_port_detach(struct fcp_port *pptr, int flag, int instance)
10245 {
10246 	int count = 0;
10247 
10248 	mutex_enter(&pptr->port_mutex);
10249 
10250 	/*
10251 	 * if the port is powered down or suspended, nothing else
10252 	 * to do; just return.
10253 	 */
10254 	if (flag != FCP_STATE_DETACHING) {
10255 		if (pptr->port_state & (FCP_STATE_POWER_DOWN |
10256 		    FCP_STATE_SUSPENDED)) {
10257 			pptr->port_state |= flag;
10258 			mutex_exit(&pptr->port_mutex);
10259 			return (FC_SUCCESS);
10260 		}
10261 	}
10262 
10263 	if (pptr->port_state & FCP_STATE_IN_MDI) {
10264 		mutex_exit(&pptr->port_mutex);
10265 		return (FC_FAILURE);
10266 	}
10267 
10268 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
10269 	    fcp_trace, FCP_BUF_LEVEL_2, 0,
10270 	    "fcp_handle_port_detach: port is detaching");
10271 
10272 	pptr->port_state |= flag;
10273 
10274 	/*
10275 	 * Wait for any ongoing reconfig/ipkt to complete, that
10276 	 * ensures the freeing to targets/luns is safe.
10277 	 * No more ref to this port should happen from statec/ioctl
10278 	 * after that as it was removed from the global port list.
10279 	 */
10280 	while (pptr->port_tmp_cnt || pptr->port_ipkt_cnt ||
10281 	    (pptr->port_state & FCP_STATE_IN_WATCHDOG)) {
10282 		/*
10283 		 * Let's give sufficient time for reconfig/ipkt
10284 		 * to complete.
10285 		 */
10286 		if (count++ >= FCP_ICMD_DEADLINE) {
10287 			break;
10288 		}
10289 		mutex_exit(&pptr->port_mutex);
10290 		delay(drv_usectohz(1000000));
10291 		mutex_enter(&pptr->port_mutex);
10292 	}
10293 
10294 	/*
10295 	 * if the driver is still busy then fail to
10296 	 * suspend/power down.
10297 	 */
10298 	if (pptr->port_tmp_cnt || pptr->port_ipkt_cnt ||
10299 	    (pptr->port_state & FCP_STATE_IN_WATCHDOG)) {
10300 		pptr->port_state &= ~flag;
10301 		mutex_exit(&pptr->port_mutex);
10302 		return (FC_FAILURE);
10303 	}
10304 
10305 	if (flag == FCP_STATE_DETACHING) {
10306 		pptr = fcp_soft_state_unlink(pptr);
10307 		ASSERT(pptr != NULL);
10308 	}
10309 
10310 	pptr->port_link_cnt++;
10311 	pptr->port_state |= FCP_STATE_OFFLINE;
10312 	pptr->port_state &= ~(FCP_STATE_ONLINING | FCP_STATE_ONLINE);
10313 
10314 	fcp_update_state(pptr, (FCP_LUN_BUSY | FCP_LUN_MARK),
10315 	    FCP_CAUSE_LINK_DOWN);
10316 	mutex_exit(&pptr->port_mutex);
10317 
10318 	/* kill watch dog timer if we're the last */
10319 	mutex_enter(&fcp_global_mutex);
10320 	if (--fcp_watchdog_init == 0) {
10321 		timeout_id_t	tid = fcp_watchdog_id;
10322 		mutex_exit(&fcp_global_mutex);
10323 		(void) untimeout(tid);
10324 	} else {
10325 		mutex_exit(&fcp_global_mutex);
10326 	}
10327 
10328 	/* clean up the port structures */
10329 	if (flag == FCP_STATE_DETACHING) {
10330 		fcp_cleanup_port(pptr, instance);
10331 	}
10332 
10333 	return (FC_SUCCESS);
10334 }
10335 
10336 
10337 static void
10338 fcp_cleanup_port(struct fcp_port *pptr, int instance)
10339 {
10340 	ASSERT(pptr != NULL);
10341 
10342 	/* unbind and free event set */
10343 	if (pptr->port_ndi_event_hdl) {
10344 		(void) ndi_event_unbind_set(pptr->port_ndi_event_hdl,
10345 		    &pptr->port_ndi_events, NDI_SLEEP);
10346 		(void) ndi_event_free_hdl(pptr->port_ndi_event_hdl);
10347 	}
10348 
10349 	if (pptr->port_ndi_event_defs) {
10350 		(void) kmem_free(pptr->port_ndi_event_defs,
10351 		    sizeof (fcp_ndi_event_defs));
10352 	}
10353 
10354 	/* free the lun/target structures and devinfos */
10355 	fcp_free_targets(pptr);
10356 
10357 	/*
10358 	 * Clean up mpxio stuff
10359 	 */
10360 	if (pptr->port_mpxio) {
10361 		(void) mdi_phci_unregister(pptr->port_dip, 0);
10362 		pptr->port_mpxio--;
10363 	}
10364 
10365 	/* clean up SCSA stuff */
10366 	(void) scsi_hba_detach(pptr->port_dip);
10367 	if (pptr->port_tran != NULL) {
10368 		scsi_hba_tran_free(pptr->port_tran);
10369 	}
10370 
10371 #ifdef	KSTATS_CODE
10372 	/* clean up kstats */
10373 	if (pptr->fcp_ksp != NULL) {
10374 		kstat_delete(pptr->fcp_ksp);
10375 	}
10376 #endif
10377 
10378 	/* clean up soft state mutexes/condition variables */
10379 	mutex_destroy(&pptr->port_mutex);
10380 	mutex_destroy(&pptr->port_pkt_mutex);
10381 
10382 	/* all done with soft state */
10383 	ddi_soft_state_free(fcp_softstate, instance);
10384 }
10385 
10386 /*
10387  *     Function: fcp_kmem_cache_constructor
10388  *
10389  *  Description: This function allocates and initializes the resources required
10390  *		 to build a scsi_pkt structure the target driver.  The result
10391  *		 of the allocation and initialization will be cached in the
10392  *		 memory cache.	As DMA resources may be allocated here, that
10393  *		 means DMA resources will be tied up in the cache manager.
10394  *		 This is a tradeoff that has been made for performance reasons.
10395  *
10396  *     Argument: *buf		Memory to preinitialize.
10397  *		 *arg		FCP port structure (fcp_port).
10398  *		 kmflags	Value passed to kmem_cache_alloc() and
10399  *				propagated to the constructor.
10400  *
10401  * Return Value: 0	Allocation/Initialization was successful.
10402  *		 -1	Allocation or Initialization failed.
10403  *
10404  *
10405  * If the returned value is 0, the buffer is initialized like this:
10406  *
10407  *		    +================================+
10408  *	     +----> |	      struct scsi_pkt	     |
10409  *	     |	    |				     |
10410  *	     | +--- | pkt_ha_private		     |
10411  *	     | |    |				     |
10412  *	     | |    +================================+
10413  *	     | |
10414  *	     | |    +================================+
10415  *	     | +--> |	    struct fcp_pkt	     | <---------+
10416  *	     |	    |				     |		 |
10417  *	     +----- | cmd_pkt			     |		 |
10418  *		    |			  cmd_fp_pkt | ---+	 |
10419  *	  +-------->| cmd_fcp_rsp[]		     |	  |	 |
10420  *	  |    +--->| cmd_fcp_cmd[]		     |	  |	 |
10421  *	  |    |    |--------------------------------|	  |	 |
10422  *	  |    |    |	      struct fc_packet	     | <--+	 |
10423  *	  |    |    |				     |		 |
10424  *	  |    |    |		     pkt_ulp_private | ----------+
10425  *	  |    |    |		     pkt_fca_private | -----+
10426  *	  |    |    |		     pkt_data_cookie | ---+ |
10427  *	  |    |    | pkt_cmdlen		     |	  | |
10428  *	  |    |(a) | pkt_rsplen		     |	  | |
10429  *	  |    +----| .......... pkt_cmd ........... | ---|-|---------------+
10430  *	  |	(b) |		      pkt_cmd_cookie | ---|-|----------+    |
10431  *	  +---------| .......... pkt_resp .......... | ---|-|------+   |    |
10432  *		    |		     pkt_resp_cookie | ---|-|--+   |   |    |
10433  *		    | pkt_cmd_dma		     |	  | |  |   |   |    |
10434  *		    | pkt_cmd_acc		     |	  | |  |   |   |    |
10435  *		    +================================+	  | |  |   |   |    |
10436  *		    |	      dma_cookies	     | <--+ |  |   |   |    |
10437  *		    |				     |	    |  |   |   |    |
10438  *		    +================================+	    |  |   |   |    |
10439  *		    |	      fca_private	     | <----+  |   |   |    |
10440  *		    |				     |	       |   |   |    |
10441  *		    +================================+	       |   |   |    |
10442  *							       |   |   |    |
10443  *							       |   |   |    |
10444  *		    +================================+	 (d)   |   |   |    |
10445  *		    |	     fcp_resp cookies	     | <-------+   |   |    |
10446  *		    |				     |		   |   |    |
10447  *		    +================================+		   |   |    |
10448  *								   |   |    |
10449  *		    +================================+	 (d)	   |   |    |
10450  *		    |		fcp_resp	     | <-----------+   |    |
10451  *		    |	(DMA resources associated)   |		       |    |
10452  *		    +================================+		       |    |
10453  *								       |    |
10454  *								       |    |
10455  *								       |    |
10456  *		    +================================+	 (c)	       |    |
10457  *		    |	     fcp_cmd cookies	     | <---------------+    |
10458  *		    |				     |			    |
10459  *		    +================================+			    |
10460  *									    |
10461  *		    +================================+	 (c)		    |
10462  *		    |		 fcp_cmd	     | <--------------------+
10463  *		    |	(DMA resources associated)   |
10464  *		    +================================+
10465  *
10466  * (a) Only if DMA is NOT used for the FCP_CMD buffer.
10467  * (b) Only if DMA is NOT used for the FCP_RESP buffer
10468  * (c) Only if DMA is used for the FCP_CMD buffer.
10469  * (d) Only if DMA is used for the FCP_RESP buffer
10470  */
10471 static int
10472 fcp_kmem_cache_constructor(struct scsi_pkt *pkt, scsi_hba_tran_t *tran,
10473     int kmflags)
10474 {
10475 	struct fcp_pkt	*cmd;
10476 	struct fcp_port	*pptr;
10477 	fc_packet_t	*fpkt;
10478 
10479 	pptr = (struct fcp_port *)tran->tran_hba_private;
10480 	cmd = (struct fcp_pkt *)pkt->pkt_ha_private;
10481 	bzero(cmd, tran->tran_hba_len);
10482 
10483 	cmd->cmd_pkt = pkt;
10484 	pkt->pkt_cdbp = cmd->cmd_fcp_cmd.fcp_cdb;
10485 	fpkt = (fc_packet_t *)&cmd->cmd_fc_packet;
10486 	cmd->cmd_fp_pkt = fpkt;
10487 
10488 	cmd->cmd_pkt->pkt_ha_private = (opaque_t)cmd;
10489 	cmd->cmd_fp_pkt->pkt_ulp_private = (opaque_t)cmd;
10490 	cmd->cmd_fp_pkt->pkt_fca_private = (opaque_t)((caddr_t)cmd +
10491 	    sizeof (struct fcp_pkt) + pptr->port_dmacookie_sz);
10492 
10493 	fpkt->pkt_data_cookie = (ddi_dma_cookie_t *)((caddr_t)cmd +
10494 	    sizeof (struct fcp_pkt));
10495 
10496 	fpkt->pkt_cmdlen = sizeof (struct fcp_cmd);
10497 	fpkt->pkt_rsplen = FCP_MAX_RSP_IU_SIZE;
10498 
10499 	if (pptr->port_fcp_dma == FC_NO_DVMA_SPACE) {
10500 		/*
10501 		 * The underlying HBA doesn't want to DMA the fcp_cmd or
10502 		 * fcp_resp.  The transfer of information will be done by
10503 		 * bcopy.
10504 		 * The naming of the flags (that is actually a value) is
10505 		 * unfortunate.	 FC_NO_DVMA_SPACE doesn't mean "NO VIRTUAL
10506 		 * DMA" but instead "NO DMA".
10507 		 */
10508 		fpkt->pkt_resp_acc = fpkt->pkt_cmd_acc = NULL;
10509 		fpkt->pkt_cmd = (caddr_t)&cmd->cmd_fcp_cmd;
10510 		fpkt->pkt_resp = cmd->cmd_fcp_rsp;
10511 	} else {
10512 		/*
10513 		 * The underlying HBA will dma the fcp_cmd buffer and fcp_resp
10514 		 * buffer.  A buffer is allocated for each one the ddi_dma_*
10515 		 * interfaces.
10516 		 */
10517 		if (fcp_alloc_cmd_resp(pptr, fpkt, kmflags) != FC_SUCCESS) {
10518 			return (-1);
10519 		}
10520 	}
10521 
10522 	return (0);
10523 }
10524 
10525 /*
10526  *     Function: fcp_kmem_cache_destructor
10527  *
10528  *  Description: Called by the destructor of the cache managed by SCSA.
10529  *		 All the resources pre-allocated in fcp_pkt_constructor
10530  *		 and the data also pre-initialized in fcp_pkt_constructor
10531  *		 are freed and uninitialized here.
10532  *
10533  *     Argument: *buf		Memory to uninitialize.
10534  *		 *arg		FCP port structure (fcp_port).
10535  *
10536  * Return Value: None
10537  *
10538  *	Context: kernel
10539  */
10540 static void
10541 fcp_kmem_cache_destructor(struct scsi_pkt *pkt, scsi_hba_tran_t *tran)
10542 {
10543 	struct fcp_pkt	*cmd;
10544 	struct fcp_port	*pptr;
10545 
10546 	pptr = (struct fcp_port *)(tran->tran_hba_private);
10547 	cmd = pkt->pkt_ha_private;
10548 
10549 	if (pptr->port_fcp_dma != FC_NO_DVMA_SPACE) {
10550 		/*
10551 		 * If DMA was used to transfer the FCP_CMD and FCP_RESP, the
10552 		 * buffer and DMA resources allocated to do so are released.
10553 		 */
10554 		fcp_free_cmd_resp(pptr, cmd->cmd_fp_pkt);
10555 	}
10556 }
10557 
10558 /*
10559  *     Function: fcp_alloc_cmd_resp
10560  *
10561  *  Description: This function allocated an FCP_CMD and FCP_RESP buffer that
10562  *		 will be DMAed by the HBA.  The buffer is allocated applying
10563  *		 the DMA requirements for the HBA.  The buffers allocated will
10564  *		 also be bound.	 DMA resources are allocated in the process.
10565  *		 They will be released by fcp_free_cmd_resp().
10566  *
10567  *     Argument: *pptr	FCP port.
10568  *		 *fpkt	fc packet for which the cmd and resp packet should be
10569  *			allocated.
10570  *		 flags	Allocation flags.
10571  *
10572  * Return Value: FC_FAILURE
10573  *		 FC_SUCCESS
10574  *
10575  *	Context: User or Kernel context only if flags == KM_SLEEP.
10576  *		 Interrupt context if the KM_SLEEP is not specified.
10577  */
10578 static int
10579 fcp_alloc_cmd_resp(struct fcp_port *pptr, fc_packet_t *fpkt, int flags)
10580 {
10581 	int			rval;
10582 	int			cmd_len;
10583 	int			resp_len;
10584 	ulong_t			real_len;
10585 	int			(*cb) (caddr_t);
10586 	ddi_dma_cookie_t	pkt_cookie;
10587 	ddi_dma_cookie_t	*cp;
10588 	uint32_t		cnt;
10589 
10590 	cb = (flags == KM_SLEEP) ? DDI_DMA_SLEEP : DDI_DMA_DONTWAIT;
10591 
10592 	cmd_len = fpkt->pkt_cmdlen;
10593 	resp_len = fpkt->pkt_rsplen;
10594 
10595 	ASSERT(fpkt->pkt_cmd_dma == NULL);
10596 
10597 	/* Allocation of a DMA handle used in subsequent calls. */
10598 	if (ddi_dma_alloc_handle(pptr->port_dip, &pptr->port_cmd_dma_attr,
10599 	    cb, NULL, &fpkt->pkt_cmd_dma) != DDI_SUCCESS) {
10600 		return (FC_FAILURE);
10601 	}
10602 
10603 	/* A buffer is allocated that satisfies the DMA requirements. */
10604 	rval = ddi_dma_mem_alloc(fpkt->pkt_cmd_dma, cmd_len,
10605 	    &pptr->port_dma_acc_attr, DDI_DMA_CONSISTENT, cb, NULL,
10606 	    (caddr_t *)&fpkt->pkt_cmd, &real_len, &fpkt->pkt_cmd_acc);
10607 
10608 	if (rval != DDI_SUCCESS) {
10609 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10610 		return (FC_FAILURE);
10611 	}
10612 
10613 	if (real_len < cmd_len) {
10614 		ddi_dma_mem_free(&fpkt->pkt_cmd_acc);
10615 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10616 		return (FC_FAILURE);
10617 	}
10618 
10619 	/* The buffer allocated is DMA bound. */
10620 	rval = ddi_dma_addr_bind_handle(fpkt->pkt_cmd_dma, NULL,
10621 	    fpkt->pkt_cmd, real_len, DDI_DMA_WRITE | DDI_DMA_CONSISTENT,
10622 	    cb, NULL, &pkt_cookie, &fpkt->pkt_cmd_cookie_cnt);
10623 
10624 	if (rval != DDI_DMA_MAPPED) {
10625 		ddi_dma_mem_free(&fpkt->pkt_cmd_acc);
10626 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10627 		return (FC_FAILURE);
10628 	}
10629 
10630 	if (fpkt->pkt_cmd_cookie_cnt >
10631 	    pptr->port_cmd_dma_attr.dma_attr_sgllen) {
10632 		(void) ddi_dma_unbind_handle(fpkt->pkt_cmd_dma);
10633 		ddi_dma_mem_free(&fpkt->pkt_cmd_acc);
10634 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10635 		return (FC_FAILURE);
10636 	}
10637 
10638 	ASSERT(fpkt->pkt_cmd_cookie_cnt != 0);
10639 
10640 	/*
10641 	 * The buffer where the scatter/gather list is going to be built is
10642 	 * allocated.
10643 	 */
10644 	cp = fpkt->pkt_cmd_cookie = (ddi_dma_cookie_t *)kmem_alloc(
10645 	    fpkt->pkt_cmd_cookie_cnt * sizeof (pkt_cookie),
10646 	    KM_NOSLEEP);
10647 
10648 	if (cp == NULL) {
10649 		(void) ddi_dma_unbind_handle(fpkt->pkt_cmd_dma);
10650 		ddi_dma_mem_free(&fpkt->pkt_cmd_acc);
10651 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10652 		return (FC_FAILURE);
10653 	}
10654 
10655 	/*
10656 	 * The scatter/gather list for the buffer we just allocated is built
10657 	 * here.
10658 	 */
10659 	*cp = pkt_cookie;
10660 	cp++;
10661 
10662 	for (cnt = 1; cnt < fpkt->pkt_cmd_cookie_cnt; cnt++, cp++) {
10663 		ddi_dma_nextcookie(fpkt->pkt_cmd_dma,
10664 		    &pkt_cookie);
10665 		*cp = pkt_cookie;
10666 	}
10667 
10668 	ASSERT(fpkt->pkt_resp_dma == NULL);
10669 	if (ddi_dma_alloc_handle(pptr->port_dip, &pptr->port_resp_dma_attr,
10670 	    cb, NULL, &fpkt->pkt_resp_dma) != DDI_SUCCESS) {
10671 		(void) ddi_dma_unbind_handle(fpkt->pkt_cmd_dma);
10672 		ddi_dma_mem_free(&fpkt->pkt_cmd_acc);
10673 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10674 		return (FC_FAILURE);
10675 	}
10676 
10677 	rval = ddi_dma_mem_alloc(fpkt->pkt_resp_dma, resp_len,
10678 	    &pptr->port_dma_acc_attr, DDI_DMA_CONSISTENT, cb, NULL,
10679 	    (caddr_t *)&fpkt->pkt_resp, &real_len,
10680 	    &fpkt->pkt_resp_acc);
10681 
10682 	if (rval != DDI_SUCCESS) {
10683 		ddi_dma_free_handle(&fpkt->pkt_resp_dma);
10684 		(void) ddi_dma_unbind_handle(fpkt->pkt_cmd_dma);
10685 		ddi_dma_mem_free(&fpkt->pkt_cmd_acc);
10686 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10687 		kmem_free(fpkt->pkt_cmd_cookie,
10688 		    fpkt->pkt_cmd_cookie_cnt * sizeof (pkt_cookie));
10689 		return (FC_FAILURE);
10690 	}
10691 
10692 	if (real_len < resp_len) {
10693 		ddi_dma_mem_free(&fpkt->pkt_resp_acc);
10694 		ddi_dma_free_handle(&fpkt->pkt_resp_dma);
10695 		(void) ddi_dma_unbind_handle(fpkt->pkt_cmd_dma);
10696 		ddi_dma_mem_free(&fpkt->pkt_cmd_acc);
10697 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10698 		kmem_free(fpkt->pkt_cmd_cookie,
10699 		    fpkt->pkt_cmd_cookie_cnt * sizeof (pkt_cookie));
10700 		return (FC_FAILURE);
10701 	}
10702 
10703 	rval = ddi_dma_addr_bind_handle(fpkt->pkt_resp_dma, NULL,
10704 	    fpkt->pkt_resp, real_len, DDI_DMA_READ | DDI_DMA_CONSISTENT,
10705 	    cb, NULL, &pkt_cookie, &fpkt->pkt_resp_cookie_cnt);
10706 
10707 	if (rval != DDI_DMA_MAPPED) {
10708 		ddi_dma_mem_free(&fpkt->pkt_resp_acc);
10709 		ddi_dma_free_handle(&fpkt->pkt_resp_dma);
10710 		(void) ddi_dma_unbind_handle(fpkt->pkt_cmd_dma);
10711 		ddi_dma_mem_free(&fpkt->pkt_cmd_acc);
10712 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10713 		kmem_free(fpkt->pkt_cmd_cookie,
10714 		    fpkt->pkt_cmd_cookie_cnt * sizeof (pkt_cookie));
10715 		return (FC_FAILURE);
10716 	}
10717 
10718 	if (fpkt->pkt_resp_cookie_cnt >
10719 	    pptr->port_resp_dma_attr.dma_attr_sgllen) {
10720 		ddi_dma_mem_free(&fpkt->pkt_resp_acc);
10721 		ddi_dma_free_handle(&fpkt->pkt_resp_dma);
10722 		(void) ddi_dma_unbind_handle(fpkt->pkt_cmd_dma);
10723 		ddi_dma_mem_free(&fpkt->pkt_cmd_acc);
10724 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10725 		kmem_free(fpkt->pkt_cmd_cookie,
10726 		    fpkt->pkt_cmd_cookie_cnt * sizeof (pkt_cookie));
10727 		return (FC_FAILURE);
10728 	}
10729 
10730 	ASSERT(fpkt->pkt_resp_cookie_cnt != 0);
10731 
10732 	cp = fpkt->pkt_resp_cookie = (ddi_dma_cookie_t *)kmem_alloc(
10733 	    fpkt->pkt_resp_cookie_cnt * sizeof (pkt_cookie),
10734 	    KM_NOSLEEP);
10735 
10736 	if (cp == NULL) {
10737 		ddi_dma_mem_free(&fpkt->pkt_resp_acc);
10738 		ddi_dma_free_handle(&fpkt->pkt_resp_dma);
10739 		(void) ddi_dma_unbind_handle(fpkt->pkt_cmd_dma);
10740 		ddi_dma_mem_free(&fpkt->pkt_cmd_acc);
10741 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10742 		kmem_free(fpkt->pkt_cmd_cookie,
10743 		    fpkt->pkt_cmd_cookie_cnt * sizeof (pkt_cookie));
10744 		return (FC_FAILURE);
10745 	}
10746 
10747 	*cp = pkt_cookie;
10748 	cp++;
10749 
10750 	for (cnt = 1; cnt < fpkt->pkt_resp_cookie_cnt; cnt++, cp++) {
10751 		ddi_dma_nextcookie(fpkt->pkt_resp_dma,
10752 		    &pkt_cookie);
10753 		*cp = pkt_cookie;
10754 	}
10755 
10756 	return (FC_SUCCESS);
10757 }
10758 
10759 /*
10760  *     Function: fcp_free_cmd_resp
10761  *
10762  *  Description: This function releases the FCP_CMD and FCP_RESP buffer
10763  *		 allocated by fcp_alloc_cmd_resp() and all the resources
10764  *		 associated with them.	That includes the DMA resources and the
10765  *		 buffer allocated for the cookies of each one of them.
10766  *
10767  *     Argument: *pptr		FCP port context.
10768  *		 *fpkt		fc packet containing the cmd and resp packet
10769  *				to be released.
10770  *
10771  * Return Value: None
10772  *
10773  *	Context: Interrupt, User and Kernel context.
10774  */
10775 /* ARGSUSED */
10776 static void
10777 fcp_free_cmd_resp(struct fcp_port *pptr, fc_packet_t *fpkt)
10778 {
10779 	ASSERT(fpkt->pkt_resp_dma != NULL && fpkt->pkt_cmd_dma != NULL);
10780 
10781 	if (fpkt->pkt_resp_dma) {
10782 		(void) ddi_dma_unbind_handle(fpkt->pkt_resp_dma);
10783 		ddi_dma_mem_free(&fpkt->pkt_resp_acc);
10784 		ddi_dma_free_handle(&fpkt->pkt_resp_dma);
10785 	}
10786 
10787 	if (fpkt->pkt_resp_cookie) {
10788 		kmem_free(fpkt->pkt_resp_cookie,
10789 		    fpkt->pkt_resp_cookie_cnt * sizeof (ddi_dma_cookie_t));
10790 		fpkt->pkt_resp_cookie = NULL;
10791 	}
10792 
10793 	if (fpkt->pkt_cmd_dma) {
10794 		(void) ddi_dma_unbind_handle(fpkt->pkt_cmd_dma);
10795 		ddi_dma_mem_free(&fpkt->pkt_cmd_acc);
10796 		ddi_dma_free_handle(&fpkt->pkt_cmd_dma);
10797 	}
10798 
10799 	if (fpkt->pkt_cmd_cookie) {
10800 		kmem_free(fpkt->pkt_cmd_cookie,
10801 		    fpkt->pkt_cmd_cookie_cnt * sizeof (ddi_dma_cookie_t));
10802 		fpkt->pkt_cmd_cookie = NULL;
10803 	}
10804 }
10805 
10806 
10807 /*
10808  * called by the transport to do our own target initialization
10809  *
10810  * can acquire and release the global mutex
10811  */
10812 /* ARGSUSED */
10813 static int
10814 fcp_phys_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
10815     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
10816 {
10817 	uchar_t			*bytes;
10818 	uint_t			nbytes;
10819 	uint16_t		lun_num;
10820 	struct fcp_tgt	*ptgt;
10821 	struct fcp_lun	*plun;
10822 	struct fcp_port	*pptr = (struct fcp_port *)
10823 	    hba_tran->tran_hba_private;
10824 
10825 	ASSERT(pptr != NULL);
10826 
10827 	FCP_DTRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
10828 	    FCP_BUF_LEVEL_8, 0,
10829 	    "fcp_phys_tgt_init: called for %s (instance %d)",
10830 	    ddi_get_name(tgt_dip), ddi_get_instance(tgt_dip));
10831 
10832 	/* get our port WWN property */
10833 	bytes = NULL;
10834 	if ((scsi_device_prop_lookup_byte_array(sd, SCSI_DEVICE_PROP_PATH,
10835 	    PORT_WWN_PROP, &bytes, &nbytes) != DDI_PROP_SUCCESS) ||
10836 	    (nbytes != FC_WWN_SIZE)) {
10837 		/* no port WWN property */
10838 		FCP_DTRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
10839 		    FCP_BUF_LEVEL_8, 0,
10840 		    "fcp_phys_tgt_init: Returning DDI_NOT_WELL_FORMED"
10841 		    " for %s (instance %d): bytes=%p nbytes=%x",
10842 		    ddi_get_name(tgt_dip), ddi_get_instance(tgt_dip), bytes,
10843 		    nbytes);
10844 
10845 		if (bytes != NULL) {
10846 			scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, bytes);
10847 		}
10848 
10849 		return (DDI_NOT_WELL_FORMED);
10850 	}
10851 	ASSERT(bytes != NULL);
10852 
10853 	lun_num = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
10854 	    LUN_PROP, 0xFFFF);
10855 	if (lun_num == 0xFFFF) {
10856 		FCP_DTRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
10857 		    FCP_BUF_LEVEL_8, 0,
10858 		    "fcp_phys_tgt_init: Returning DDI_FAILURE:lun"
10859 		    " for %s (instance %d)", ddi_get_name(tgt_dip),
10860 		    ddi_get_instance(tgt_dip));
10861 
10862 		scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, bytes);
10863 		return (DDI_NOT_WELL_FORMED);
10864 	}
10865 
10866 	mutex_enter(&pptr->port_mutex);
10867 	if ((plun = fcp_lookup_lun(pptr, bytes, lun_num)) == NULL) {
10868 		mutex_exit(&pptr->port_mutex);
10869 		FCP_DTRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
10870 		    FCP_BUF_LEVEL_8, 0,
10871 		    "fcp_phys_tgt_init: Returning DDI_FAILURE: No Lun"
10872 		    " for %s (instance %d)", ddi_get_name(tgt_dip),
10873 		    ddi_get_instance(tgt_dip));
10874 
10875 		scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, bytes);
10876 		return (DDI_FAILURE);
10877 	}
10878 
10879 	ASSERT(bcmp(plun->lun_tgt->tgt_port_wwn.raw_wwn, bytes,
10880 	    FC_WWN_SIZE) == 0);
10881 	ASSERT(plun->lun_num == lun_num);
10882 
10883 	scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, bytes);
10884 
10885 	ptgt = plun->lun_tgt;
10886 
10887 	mutex_enter(&ptgt->tgt_mutex);
10888 	plun->lun_tgt_count++;
10889 	scsi_device_hba_private_set(sd, plun);
10890 	plun->lun_state |= FCP_SCSI_LUN_TGT_INIT;
10891 	plun->lun_sd = sd;
10892 	mutex_exit(&ptgt->tgt_mutex);
10893 	mutex_exit(&pptr->port_mutex);
10894 
10895 	return (DDI_SUCCESS);
10896 }
10897 
10898 /*ARGSUSED*/
10899 static int
10900 fcp_virt_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
10901     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
10902 {
10903 	uchar_t			*bytes;
10904 	uint_t			nbytes;
10905 	uint16_t		lun_num;
10906 	struct fcp_tgt	*ptgt;
10907 	struct fcp_lun	*plun;
10908 	struct fcp_port	*pptr = (struct fcp_port *)
10909 	    hba_tran->tran_hba_private;
10910 	child_info_t		*cip;
10911 
10912 	ASSERT(pptr != NULL);
10913 
10914 	FCP_DTRACE(fcp_logq, pptr->port_instbuf,
10915 	    fcp_trace, FCP_BUF_LEVEL_8, 0,
10916 	    "fcp_virt_tgt_init: called for %s (instance %d) (hba_dip %p),"
10917 	    " (tgt_dip %p)", ddi_get_name(tgt_dip),
10918 	    ddi_get_instance(tgt_dip), hba_dip, tgt_dip);
10919 
10920 	cip = (child_info_t *)sd->sd_pathinfo;
10921 	if (cip == NULL) {
10922 		FCP_DTRACE(fcp_logq, pptr->port_instbuf,
10923 		    fcp_trace, FCP_BUF_LEVEL_8, 0,
10924 		    "fcp_virt_tgt_init: Returning DDI_NOT_WELL_FORMED"
10925 		    " for %s (instance %d)", ddi_get_name(tgt_dip),
10926 		    ddi_get_instance(tgt_dip));
10927 
10928 		return (DDI_NOT_WELL_FORMED);
10929 	}
10930 
10931 	/* get our port WWN property */
10932 	bytes = NULL;
10933 	if ((scsi_device_prop_lookup_byte_array(sd, SCSI_DEVICE_PROP_PATH,
10934 	    PORT_WWN_PROP, &bytes, &nbytes) != DDI_PROP_SUCCESS) ||
10935 	    (nbytes != FC_WWN_SIZE)) {
10936 		if (bytes) {
10937 			scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, bytes);
10938 		}
10939 		return (DDI_NOT_WELL_FORMED);
10940 	}
10941 
10942 	ASSERT(bytes != NULL);
10943 
10944 	lun_num = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
10945 	    LUN_PROP, 0xFFFF);
10946 	if (lun_num == 0xFFFF) {
10947 		FCP_DTRACE(fcp_logq, pptr->port_instbuf,
10948 		    fcp_trace, FCP_BUF_LEVEL_8, 0,
10949 		    "fcp_virt_tgt_init: Returning DDI_FAILURE:lun"
10950 		    " for %s (instance %d)", ddi_get_name(tgt_dip),
10951 		    ddi_get_instance(tgt_dip));
10952 
10953 		scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, bytes);
10954 		return (DDI_NOT_WELL_FORMED);
10955 	}
10956 
10957 	mutex_enter(&pptr->port_mutex);
10958 	if ((plun = fcp_lookup_lun(pptr, bytes, lun_num)) == NULL) {
10959 		mutex_exit(&pptr->port_mutex);
10960 		FCP_DTRACE(fcp_logq, pptr->port_instbuf,
10961 		    fcp_trace, FCP_BUF_LEVEL_8, 0,
10962 		    "fcp_virt_tgt_init: Returning DDI_FAILURE: No Lun"
10963 		    " for %s (instance %d)", ddi_get_name(tgt_dip),
10964 		    ddi_get_instance(tgt_dip));
10965 
10966 		scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, bytes);
10967 		return (DDI_FAILURE);
10968 	}
10969 
10970 	ASSERT(bcmp(plun->lun_tgt->tgt_port_wwn.raw_wwn, bytes,
10971 	    FC_WWN_SIZE) == 0);
10972 	ASSERT(plun->lun_num == lun_num);
10973 
10974 	scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, bytes);
10975 
10976 	ptgt = plun->lun_tgt;
10977 
10978 	mutex_enter(&ptgt->tgt_mutex);
10979 	plun->lun_tgt_count++;
10980 	scsi_device_hba_private_set(sd, plun);
10981 	plun->lun_state |= FCP_SCSI_LUN_TGT_INIT;
10982 	plun->lun_sd = sd;
10983 	mutex_exit(&ptgt->tgt_mutex);
10984 	mutex_exit(&pptr->port_mutex);
10985 
10986 	return (DDI_SUCCESS);
10987 }
10988 
10989 
10990 /*
10991  * called by the transport to do our own target initialization
10992  *
10993  * can acquire and release the global mutex
10994  */
10995 /* ARGSUSED */
10996 static int
10997 fcp_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
10998     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
10999 {
11000 	struct fcp_port	*pptr = (struct fcp_port *)
11001 	    hba_tran->tran_hba_private;
11002 	int			rval;
11003 
11004 	ASSERT(pptr != NULL);
11005 
11006 	/*
11007 	 * Child node is getting initialized.  Look at the mpxio component
11008 	 * type on the child device to see if this device is mpxio managed
11009 	 * or not.
11010 	 */
11011 	if (mdi_component_is_client(tgt_dip, NULL) == MDI_SUCCESS) {
11012 		rval = fcp_virt_tgt_init(hba_dip, tgt_dip, hba_tran, sd);
11013 	} else {
11014 		rval = fcp_phys_tgt_init(hba_dip, tgt_dip, hba_tran, sd);
11015 	}
11016 
11017 	return (rval);
11018 }
11019 
11020 
11021 /* ARGSUSED */
11022 static void
11023 fcp_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
11024     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
11025 {
11026 	struct fcp_lun	*plun = scsi_device_hba_private_get(sd);
11027 	struct fcp_tgt	*ptgt;
11028 
11029 	FCP_DTRACE(fcp_logq, LUN_PORT->port_instbuf,
11030 	    fcp_trace, FCP_BUF_LEVEL_8, 0,
11031 	    "fcp_scsi_tgt_free: called for tran %s%d, dev %s%d",
11032 	    ddi_get_name(hba_dip), ddi_get_instance(hba_dip),
11033 	    ddi_get_name(tgt_dip), ddi_get_instance(tgt_dip));
11034 
11035 	if (plun == NULL) {
11036 		return;
11037 	}
11038 	ptgt = plun->lun_tgt;
11039 
11040 	ASSERT(ptgt != NULL);
11041 
11042 	mutex_enter(&ptgt->tgt_mutex);
11043 	ASSERT(plun->lun_tgt_count > 0);
11044 
11045 	if (--plun->lun_tgt_count == 0) {
11046 		plun->lun_state &= ~FCP_SCSI_LUN_TGT_INIT;
11047 	}
11048 	plun->lun_sd = NULL;
11049 	mutex_exit(&ptgt->tgt_mutex);
11050 }
11051 
11052 /*
11053  *     Function: fcp_scsi_start
11054  *
11055  *  Description: This function is called by the target driver to request a
11056  *		 command to be sent.
11057  *
11058  *     Argument: *ap		SCSI address of the device.
11059  *		 *pkt		SCSI packet containing the cmd to send.
11060  *
11061  * Return Value: TRAN_ACCEPT
11062  *		 TRAN_BUSY
11063  *		 TRAN_BADPKT
11064  *		 TRAN_FATAL_ERROR
11065  */
11066 static int
11067 fcp_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
11068 {
11069 	struct fcp_port	*pptr = ADDR2FCP(ap);
11070 	struct fcp_lun	*plun = ADDR2LUN(ap);
11071 	struct fcp_pkt	*cmd = PKT2CMD(pkt);
11072 	struct fcp_tgt	*ptgt = plun->lun_tgt;
11073 	int			rval;
11074 
11075 	/* ensure command isn't already issued */
11076 	ASSERT(cmd->cmd_state != FCP_PKT_ISSUED);
11077 
11078 	FCP_DTRACE(fcp_logq, pptr->port_instbuf,
11079 	    fcp_trace, FCP_BUF_LEVEL_9, 0,
11080 	    "fcp_transport Invoked for %x", plun->lun_tgt->tgt_d_id);
11081 
11082 	/*
11083 	 * It is strange that we enter the fcp_port mutex and the target
11084 	 * mutex to check the lun state (which has a mutex of its own).
11085 	 */
11086 	mutex_enter(&pptr->port_mutex);
11087 	mutex_enter(&ptgt->tgt_mutex);
11088 
11089 	/*
11090 	 * If the device is offline and is not in the process of coming
11091 	 * online, fail the request.
11092 	 */
11093 
11094 	if ((plun->lun_state & FCP_LUN_OFFLINE) &&
11095 	    !(plun->lun_state & FCP_LUN_ONLINING)) {
11096 		mutex_exit(&ptgt->tgt_mutex);
11097 		mutex_exit(&pptr->port_mutex);
11098 
11099 		if (cmd->cmd_fp_pkt->pkt_pd == NULL) {
11100 			pkt->pkt_reason = CMD_DEV_GONE;
11101 		}
11102 
11103 		return (TRAN_FATAL_ERROR);
11104 	}
11105 	cmd->cmd_fp_pkt->pkt_timeout = pkt->pkt_time;
11106 
11107 	/*
11108 	 * If we are suspended, kernel is trying to dump, so don't
11109 	 * block, fail or defer requests - send them down right away.
11110 	 * NOTE: If we are in panic (i.e. trying to dump), we can't
11111 	 * assume we have been suspended.  There is hardware such as
11112 	 * the v880 that doesn't do PM.	 Thus, the check for
11113 	 * ddi_in_panic.
11114 	 *
11115 	 * If FCP_STATE_IN_CB_DEVC is set, devices are in the process
11116 	 * of changing.	 So, if we can queue the packet, do it.	 Eventually,
11117 	 * either the device will have gone away or changed and we can fail
11118 	 * the request, or we can proceed if the device didn't change.
11119 	 *
11120 	 * If the pd in the target or the packet is NULL it's probably
11121 	 * because the device has gone away, we allow the request to be
11122 	 * put on the internal queue here in case the device comes back within
11123 	 * the offline timeout. fctl will fix up the pd's if the tgt_pd_handle
11124 	 * has gone NULL, while fcp deals cases where pkt_pd is NULL. pkt_pd
11125 	 * could be NULL because the device was disappearing during or since
11126 	 * packet initialization.
11127 	 */
11128 
11129 	if (((plun->lun_state & FCP_LUN_BUSY) && (!(pptr->port_state &
11130 	    FCP_STATE_SUSPENDED)) && !ddi_in_panic()) ||
11131 	    (pptr->port_state & (FCP_STATE_ONLINING | FCP_STATE_IN_CB_DEVC)) ||
11132 	    (ptgt->tgt_pd_handle == NULL) ||
11133 	    (cmd->cmd_fp_pkt->pkt_pd == NULL)) {
11134 		/*
11135 		 * If ((LUN is busy AND
11136 		 *	LUN not suspended AND
11137 		 *	The system is not in panic state) OR
11138 		 *	(The port is coming up))
11139 		 *
11140 		 * We check to see if the any of the flags FLAG_NOINTR or
11141 		 * FLAG_NOQUEUE is set.	 If one of them is set the value
11142 		 * returned will be TRAN_BUSY.	If not, the request is queued.
11143 		 */
11144 		mutex_exit(&ptgt->tgt_mutex);
11145 		mutex_exit(&pptr->port_mutex);
11146 
11147 		/* see if using interrupts is allowed (so queueing'll work) */
11148 		if (pkt->pkt_flags & FLAG_NOINTR) {
11149 			pkt->pkt_resid = 0;
11150 			return (TRAN_BUSY);
11151 		}
11152 		if (pkt->pkt_flags & FLAG_NOQUEUE) {
11153 			FCP_DTRACE(fcp_logq, pptr->port_instbuf,
11154 			    fcp_trace, FCP_BUF_LEVEL_9, 0,
11155 			    "fcp_scsi_start: lun busy for pkt %p", pkt);
11156 			return (TRAN_BUSY);
11157 		}
11158 #ifdef	DEBUG
11159 		mutex_enter(&pptr->port_pkt_mutex);
11160 		pptr->port_npkts++;
11161 		mutex_exit(&pptr->port_pkt_mutex);
11162 #endif /* DEBUG */
11163 
11164 		/* got queue up the pkt for later */
11165 		fcp_queue_pkt(pptr, cmd);
11166 		return (TRAN_ACCEPT);
11167 	}
11168 	cmd->cmd_state = FCP_PKT_ISSUED;
11169 
11170 	mutex_exit(&ptgt->tgt_mutex);
11171 	mutex_exit(&pptr->port_mutex);
11172 
11173 	/*
11174 	 * Now that we released the mutexes, what was protected by them can
11175 	 * change.
11176 	 */
11177 
11178 	/*
11179 	 * If there is a reconfiguration in progress, wait for it to complete.
11180 	 */
11181 	fcp_reconfig_wait(pptr);
11182 
11183 	cmd->cmd_timeout = pkt->pkt_time ? fcp_watchdog_time +
11184 	    pkt->pkt_time : 0;
11185 
11186 	/* prepare the packet */
11187 
11188 	fcp_prepare_pkt(pptr, cmd, plun);
11189 
11190 	if (cmd->cmd_pkt->pkt_time) {
11191 		cmd->cmd_fp_pkt->pkt_timeout = cmd->cmd_pkt->pkt_time;
11192 	} else {
11193 		cmd->cmd_fp_pkt->pkt_timeout = 5 * 60 * 60;
11194 	}
11195 
11196 	/*
11197 	 * if interrupts aren't allowed (e.g. at dump time) then we'll
11198 	 * have to do polled I/O
11199 	 */
11200 	if (pkt->pkt_flags & FLAG_NOINTR) {
11201 		cmd->cmd_state &= ~FCP_PKT_ISSUED;
11202 		return (fcp_dopoll(pptr, cmd));
11203 	}
11204 
11205 #ifdef	DEBUG
11206 	mutex_enter(&pptr->port_pkt_mutex);
11207 	pptr->port_npkts++;
11208 	mutex_exit(&pptr->port_pkt_mutex);
11209 #endif /* DEBUG */
11210 
11211 	rval = fcp_transport(pptr->port_fp_handle, cmd->cmd_fp_pkt, 0);
11212 	if (rval == FC_SUCCESS) {
11213 		FCP_DTRACE(fcp_logq, pptr->port_instbuf,
11214 		    fcp_trace, FCP_BUF_LEVEL_9, 0,
11215 		    "fcp_transport success for %x", plun->lun_tgt->tgt_d_id);
11216 		return (TRAN_ACCEPT);
11217 	}
11218 
11219 	cmd->cmd_state = FCP_PKT_IDLE;
11220 
11221 #ifdef	DEBUG
11222 	mutex_enter(&pptr->port_pkt_mutex);
11223 	pptr->port_npkts--;
11224 	mutex_exit(&pptr->port_pkt_mutex);
11225 #endif /* DEBUG */
11226 
11227 	/*
11228 	 * For lack of clearer definitions, choose
11229 	 * between TRAN_BUSY and TRAN_FATAL_ERROR.
11230 	 */
11231 
11232 	if (rval == FC_TRAN_BUSY) {
11233 		pkt->pkt_resid = 0;
11234 		rval = TRAN_BUSY;
11235 	} else {
11236 		mutex_enter(&ptgt->tgt_mutex);
11237 		if (plun->lun_state & FCP_LUN_OFFLINE) {
11238 			child_info_t	*cip;
11239 
11240 			mutex_enter(&plun->lun_mutex);
11241 			cip = plun->lun_cip;
11242 			mutex_exit(&plun->lun_mutex);
11243 
11244 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
11245 			    fcp_trace, FCP_BUF_LEVEL_6, 0,
11246 			    "fcp_transport failed 2 for %x: %x; dip=%p",
11247 			    plun->lun_tgt->tgt_d_id, rval, cip);
11248 
11249 			rval = TRAN_FATAL_ERROR;
11250 		} else {
11251 			if (pkt->pkt_flags & FLAG_NOQUEUE) {
11252 				FCP_DTRACE(fcp_logq, pptr->port_instbuf,
11253 				    fcp_trace, FCP_BUF_LEVEL_9, 0,
11254 				    "fcp_scsi_start: FC_BUSY for pkt %p",
11255 				    pkt);
11256 				rval = TRAN_BUSY;
11257 			} else {
11258 				rval = TRAN_ACCEPT;
11259 				fcp_queue_pkt(pptr, cmd);
11260 			}
11261 		}
11262 		mutex_exit(&ptgt->tgt_mutex);
11263 	}
11264 
11265 	return (rval);
11266 }
11267 
11268 /*
11269  * called by the transport to abort a packet
11270  */
11271 /*ARGSUSED*/
11272 static int
11273 fcp_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
11274 {
11275 	int tgt_cnt;
11276 	struct fcp_port		*pptr = ADDR2FCP(ap);
11277 	struct fcp_lun	*plun = ADDR2LUN(ap);
11278 	struct fcp_tgt	*ptgt = plun->lun_tgt;
11279 
11280 	if (pkt == NULL) {
11281 		if (ptgt) {
11282 			mutex_enter(&ptgt->tgt_mutex);
11283 			tgt_cnt = ptgt->tgt_change_cnt;
11284 			mutex_exit(&ptgt->tgt_mutex);
11285 			fcp_abort_all(pptr, ptgt, plun, tgt_cnt);
11286 			return (TRUE);
11287 		}
11288 	}
11289 	return (FALSE);
11290 }
11291 
11292 
11293 /*
11294  * Perform reset
11295  */
11296 int
11297 fcp_scsi_reset(struct scsi_address *ap, int level)
11298 {
11299 	int			rval = 0;
11300 	struct fcp_port		*pptr = ADDR2FCP(ap);
11301 	struct fcp_lun	*plun = ADDR2LUN(ap);
11302 	struct fcp_tgt	*ptgt = plun->lun_tgt;
11303 
11304 	if (level == RESET_ALL) {
11305 		if (fcp_linkreset(pptr, ap, KM_NOSLEEP) == FC_SUCCESS) {
11306 			rval = 1;
11307 		}
11308 	} else if (level == RESET_TARGET || level == RESET_LUN) {
11309 		/*
11310 		 * If we are in the middle of discovery, return
11311 		 * SUCCESS as this target will be rediscovered
11312 		 * anyway
11313 		 */
11314 		mutex_enter(&ptgt->tgt_mutex);
11315 		if (ptgt->tgt_state & (FCP_TGT_OFFLINE | FCP_TGT_BUSY)) {
11316 			mutex_exit(&ptgt->tgt_mutex);
11317 			return (1);
11318 		}
11319 		mutex_exit(&ptgt->tgt_mutex);
11320 
11321 		if (fcp_reset_target(ap, level) == FC_SUCCESS) {
11322 			rval = 1;
11323 		}
11324 	}
11325 	return (rval);
11326 }
11327 
11328 
11329 /*
11330  * called by the framework to get a SCSI capability
11331  */
11332 static int
11333 fcp_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
11334 {
11335 	return (fcp_commoncap(ap, cap, 0, whom, 0));
11336 }
11337 
11338 
11339 /*
11340  * called by the framework to set a SCSI capability
11341  */
11342 static int
11343 fcp_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
11344 {
11345 	return (fcp_commoncap(ap, cap, value, whom, 1));
11346 }
11347 
11348 /*
11349  *     Function: fcp_pkt_setup
11350  *
11351  *  Description: This function sets up the scsi_pkt structure passed by the
11352  *		 caller. This function assumes fcp_pkt_constructor has been
11353  *		 called previously for the packet passed by the caller.	 If
11354  *		 successful this call will have the following results:
11355  *
11356  *		   - The resources needed that will be constant through out
11357  *		     the whole transaction are allocated.
11358  *		   - The fields that will be constant through out the whole
11359  *		     transaction are initialized.
11360  *		   - The scsi packet will be linked to the LUN structure
11361  *		     addressed by the transaction.
11362  *
11363  *     Argument:
11364  *		 *pkt		Pointer to a scsi_pkt structure.
11365  *		 callback
11366  *		 arg
11367  *
11368  * Return Value: 0	Success
11369  *		 !0	Failure
11370  *
11371  *	Context: Kernel context or interrupt context
11372  */
11373 /* ARGSUSED */
11374 static int
11375 fcp_pkt_setup(struct scsi_pkt *pkt,
11376     int (*callback)(caddr_t arg),
11377     caddr_t arg)
11378 {
11379 	struct fcp_pkt	*cmd;
11380 	struct fcp_port	*pptr;
11381 	struct fcp_lun	*plun;
11382 	struct fcp_tgt	*ptgt;
11383 	int		kf;
11384 	fc_packet_t	*fpkt;
11385 	fc_frame_hdr_t	*hp;
11386 
11387 	pptr = ADDR2FCP(&pkt->pkt_address);
11388 	plun = ADDR2LUN(&pkt->pkt_address);
11389 	ptgt = plun->lun_tgt;
11390 
11391 	cmd = (struct fcp_pkt *)pkt->pkt_ha_private;
11392 	fpkt = cmd->cmd_fp_pkt;
11393 
11394 	/*
11395 	 * this request is for dma allocation only
11396 	 */
11397 	/*
11398 	 * First step of fcp_scsi_init_pkt: pkt allocation
11399 	 * We determine if the caller is willing to wait for the
11400 	 * resources.
11401 	 */
11402 	kf = (callback == SLEEP_FUNC) ? KM_SLEEP: KM_NOSLEEP;
11403 
11404 	/*
11405 	 * Selective zeroing of the pkt.
11406 	 */
11407 	cmd->cmd_back = NULL;
11408 	cmd->cmd_next = NULL;
11409 
11410 	/*
11411 	 * Zero out fcp command
11412 	 */
11413 	bzero(&cmd->cmd_fcp_cmd, sizeof (cmd->cmd_fcp_cmd));
11414 
11415 	cmd->cmd_state = FCP_PKT_IDLE;
11416 
11417 	fpkt = cmd->cmd_fp_pkt;
11418 	fpkt->pkt_data_acc = NULL;
11419 
11420 	/*
11421 	 * When port_state is FCP_STATE_OFFLINE, remote_port (tgt_pd_handle)
11422 	 * could be destroyed.	We need fail pkt_setup.
11423 	 */
11424 	if (pptr->port_state & FCP_STATE_OFFLINE) {
11425 		return (-1);
11426 	}
11427 
11428 	mutex_enter(&ptgt->tgt_mutex);
11429 	fpkt->pkt_pd = ptgt->tgt_pd_handle;
11430 
11431 	if (fc_ulp_init_packet(pptr->port_fp_handle, fpkt, kf)
11432 	    != FC_SUCCESS) {
11433 		mutex_exit(&ptgt->tgt_mutex);
11434 		return (-1);
11435 	}
11436 
11437 	mutex_exit(&ptgt->tgt_mutex);
11438 
11439 	/* Fill in the Fabric Channel Header */
11440 	hp = &fpkt->pkt_cmd_fhdr;
11441 	hp->r_ctl = R_CTL_COMMAND;
11442 	hp->rsvd = 0;
11443 	hp->type = FC_TYPE_SCSI_FCP;
11444 	hp->f_ctl = F_CTL_SEQ_INITIATIVE | F_CTL_FIRST_SEQ;
11445 	hp->seq_id = 0;
11446 	hp->df_ctl  = 0;
11447 	hp->seq_cnt = 0;
11448 	hp->ox_id = 0xffff;
11449 	hp->rx_id = 0xffff;
11450 	hp->ro = 0;
11451 
11452 	/*
11453 	 * A doubly linked list (cmd_forw, cmd_back) is built
11454 	 * out of every allocated packet on a per-lun basis
11455 	 *
11456 	 * The packets are maintained in the list so as to satisfy
11457 	 * scsi_abort() requests. At present (which is unlikely to
11458 	 * change in the future) nobody performs a real scsi_abort
11459 	 * in the SCSI target drivers (as they don't keep the packets
11460 	 * after doing scsi_transport - so they don't know how to
11461 	 * abort a packet other than sending a NULL to abort all
11462 	 * outstanding packets)
11463 	 */
11464 	mutex_enter(&plun->lun_mutex);
11465 	if ((cmd->cmd_forw = plun->lun_pkt_head) != NULL) {
11466 		plun->lun_pkt_head->cmd_back = cmd;
11467 	} else {
11468 		plun->lun_pkt_tail = cmd;
11469 	}
11470 	plun->lun_pkt_head = cmd;
11471 	mutex_exit(&plun->lun_mutex);
11472 	return (0);
11473 }
11474 
11475 /*
11476  *     Function: fcp_pkt_teardown
11477  *
11478  *  Description: This function releases a scsi_pkt structure and all the
11479  *		 resources attached to it.
11480  *
11481  *     Argument: *pkt		Pointer to a scsi_pkt structure.
11482  *
11483  * Return Value: None
11484  *
11485  *	Context: User, Kernel or Interrupt context.
11486  */
11487 static void
11488 fcp_pkt_teardown(struct scsi_pkt *pkt)
11489 {
11490 	struct fcp_port	*pptr = ADDR2FCP(&pkt->pkt_address);
11491 	struct fcp_lun	*plun = ADDR2LUN(&pkt->pkt_address);
11492 	struct fcp_pkt	*cmd = (struct fcp_pkt *)pkt->pkt_ha_private;
11493 
11494 	/*
11495 	 * Remove the packet from the per-lun list
11496 	 */
11497 	mutex_enter(&plun->lun_mutex);
11498 	if (cmd->cmd_back) {
11499 		ASSERT(cmd != plun->lun_pkt_head);
11500 		cmd->cmd_back->cmd_forw = cmd->cmd_forw;
11501 	} else {
11502 		ASSERT(cmd == plun->lun_pkt_head);
11503 		plun->lun_pkt_head = cmd->cmd_forw;
11504 	}
11505 
11506 	if (cmd->cmd_forw) {
11507 		cmd->cmd_forw->cmd_back = cmd->cmd_back;
11508 	} else {
11509 		ASSERT(cmd == plun->lun_pkt_tail);
11510 		plun->lun_pkt_tail = cmd->cmd_back;
11511 	}
11512 
11513 	mutex_exit(&plun->lun_mutex);
11514 
11515 	(void) fc_ulp_uninit_packet(pptr->port_fp_handle, cmd->cmd_fp_pkt);
11516 }
11517 
11518 /*
11519  * Routine for reset notification setup, to register or cancel.
11520  * This function is called by SCSA
11521  */
11522 /*ARGSUSED*/
11523 static int
11524 fcp_scsi_reset_notify(struct scsi_address *ap, int flag,
11525     void (*callback)(caddr_t), caddr_t arg)
11526 {
11527 	struct fcp_port *pptr = ADDR2FCP(ap);
11528 
11529 	return (scsi_hba_reset_notify_setup(ap, flag, callback, arg,
11530 	    &pptr->port_mutex, &pptr->port_reset_notify_listf));
11531 }
11532 
11533 
11534 static int
11535 fcp_scsi_bus_get_eventcookie(dev_info_t *dip, dev_info_t *rdip, char *name,
11536     ddi_eventcookie_t *event_cookiep)
11537 {
11538 	struct fcp_port *pptr = fcp_dip2port(dip);
11539 
11540 	if (pptr == NULL) {
11541 		return (DDI_FAILURE);
11542 	}
11543 
11544 	return (ndi_event_retrieve_cookie(pptr->port_ndi_event_hdl, rdip, name,
11545 	    event_cookiep, NDI_EVENT_NOPASS));
11546 }
11547 
11548 
11549 static int
11550 fcp_scsi_bus_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
11551     ddi_eventcookie_t eventid, void (*callback)(), void *arg,
11552     ddi_callback_id_t *cb_id)
11553 {
11554 	struct fcp_port *pptr = fcp_dip2port(dip);
11555 
11556 	if (pptr == NULL) {
11557 		return (DDI_FAILURE);
11558 	}
11559 
11560 	return (ndi_event_add_callback(pptr->port_ndi_event_hdl, rdip,
11561 	    eventid, callback, arg, NDI_SLEEP, cb_id));
11562 }
11563 
11564 
11565 static int
11566 fcp_scsi_bus_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
11567 {
11568 
11569 	struct fcp_port *pptr = fcp_dip2port(dip);
11570 
11571 	if (pptr == NULL) {
11572 		return (DDI_FAILURE);
11573 	}
11574 	return (ndi_event_remove_callback(pptr->port_ndi_event_hdl, cb_id));
11575 }
11576 
11577 
11578 /*
11579  * called by the transport to post an event
11580  */
11581 static int
11582 fcp_scsi_bus_post_event(dev_info_t *dip, dev_info_t *rdip,
11583     ddi_eventcookie_t eventid, void *impldata)
11584 {
11585 	struct fcp_port *pptr = fcp_dip2port(dip);
11586 
11587 	if (pptr == NULL) {
11588 		return (DDI_FAILURE);
11589 	}
11590 
11591 	return (ndi_event_run_callbacks(pptr->port_ndi_event_hdl, rdip,
11592 	    eventid, impldata));
11593 }
11594 
11595 
11596 /*
11597  * A target in in many cases in Fibre Channel has a one to one relation
11598  * with a port identifier (which is also known as D_ID and also as AL_PA
11599  * in private Loop) On Fibre Channel-to-SCSI bridge boxes a target reset
11600  * will most likely result in resetting all LUNs (which means a reset will
11601  * occur on all the SCSI devices connected at the other end of the bridge)
11602  * That is the latest favorite topic for discussion, for, one can debate as
11603  * hot as one likes and come up with arguably a best solution to one's
11604  * satisfaction
11605  *
11606  * To stay on track and not digress much, here are the problems stated
11607  * briefly:
11608  *
11609  *	SCSA doesn't define RESET_LUN, It defines RESET_TARGET, but the
11610  *	target drivers use RESET_TARGET even if their instance is on a
11611  *	LUN. Doesn't that sound a bit broken ?
11612  *
11613  *	FCP SCSI (the current spec) only defines RESET TARGET in the
11614  *	control fields of an FCP_CMND structure. It should have been
11615  *	fixed right there, giving flexibility to the initiators to
11616  *	minimize havoc that could be caused by resetting a target.
11617  */
11618 static int
11619 fcp_reset_target(struct scsi_address *ap, int level)
11620 {
11621 	int			rval = FC_FAILURE;
11622 	char			lun_id[25];
11623 	struct fcp_port		*pptr = ADDR2FCP(ap);
11624 	struct fcp_lun	*plun = ADDR2LUN(ap);
11625 	struct fcp_tgt	*ptgt = plun->lun_tgt;
11626 	struct scsi_pkt		*pkt;
11627 	struct fcp_pkt	*cmd;
11628 	struct fcp_rsp		*rsp;
11629 	uint32_t		tgt_cnt;
11630 	struct fcp_rsp_info	*rsp_info;
11631 	struct fcp_reset_elem	*p;
11632 	int			bval;
11633 
11634 	if ((p = kmem_alloc(sizeof (struct fcp_reset_elem),
11635 	    KM_NOSLEEP)) == NULL) {
11636 		return (rval);
11637 	}
11638 
11639 	mutex_enter(&ptgt->tgt_mutex);
11640 	if (level == RESET_TARGET) {
11641 		if (ptgt->tgt_state & (FCP_TGT_OFFLINE | FCP_TGT_BUSY)) {
11642 			mutex_exit(&ptgt->tgt_mutex);
11643 			kmem_free(p, sizeof (struct fcp_reset_elem));
11644 			return (rval);
11645 		}
11646 		fcp_update_tgt_state(ptgt, FCP_SET, FCP_LUN_BUSY);
11647 		(void) strcpy(lun_id, " ");
11648 	} else {
11649 		if (plun->lun_state & (FCP_LUN_OFFLINE | FCP_LUN_BUSY)) {
11650 			mutex_exit(&ptgt->tgt_mutex);
11651 			kmem_free(p, sizeof (struct fcp_reset_elem));
11652 			return (rval);
11653 		}
11654 		fcp_update_lun_state(plun, FCP_SET, FCP_LUN_BUSY);
11655 
11656 		(void) sprintf(lun_id, ", LUN=%d", plun->lun_num);
11657 	}
11658 	tgt_cnt = ptgt->tgt_change_cnt;
11659 
11660 	mutex_exit(&ptgt->tgt_mutex);
11661 
11662 	if ((pkt = scsi_init_pkt(ap, NULL, NULL, 0, 0,
11663 	    0, 0, NULL, 0)) == NULL) {
11664 		kmem_free(p, sizeof (struct fcp_reset_elem));
11665 		mutex_enter(&ptgt->tgt_mutex);
11666 		fcp_update_tgt_state(ptgt, FCP_RESET, FCP_LUN_BUSY);
11667 		mutex_exit(&ptgt->tgt_mutex);
11668 		return (rval);
11669 	}
11670 	pkt->pkt_time = FCP_POLL_TIMEOUT;
11671 
11672 	/* fill in cmd part of packet */
11673 	cmd = PKT2CMD(pkt);
11674 	if (level == RESET_TARGET) {
11675 		cmd->cmd_fcp_cmd.fcp_cntl.cntl_reset_tgt = 1;
11676 	} else {
11677 		cmd->cmd_fcp_cmd.fcp_cntl.cntl_reset_lun = 1;
11678 	}
11679 	cmd->cmd_fp_pkt->pkt_comp = NULL;
11680 	cmd->cmd_pkt->pkt_flags |= FLAG_NOINTR;
11681 
11682 	/* prepare a packet for transport */
11683 	fcp_prepare_pkt(pptr, cmd, plun);
11684 
11685 	if (cmd->cmd_pkt->pkt_time) {
11686 		cmd->cmd_fp_pkt->pkt_timeout = cmd->cmd_pkt->pkt_time;
11687 	} else {
11688 		cmd->cmd_fp_pkt->pkt_timeout = 5 * 60 * 60;
11689 	}
11690 
11691 	(void) fc_ulp_busy_port(pptr->port_fp_handle);
11692 	bval = fcp_dopoll(pptr, cmd);
11693 	fc_ulp_idle_port(pptr->port_fp_handle);
11694 
11695 	/* submit the packet */
11696 	if (bval == TRAN_ACCEPT) {
11697 		int error = 3;
11698 
11699 		rsp = (struct fcp_rsp *)cmd->cmd_fcp_rsp;
11700 		rsp_info = (struct fcp_rsp_info *)(cmd->cmd_fcp_rsp +
11701 		    sizeof (struct fcp_rsp));
11702 
11703 		if (rsp->fcp_u.fcp_status.rsp_len_set) {
11704 			if (fcp_validate_fcp_response(rsp, pptr) ==
11705 			    FC_SUCCESS) {
11706 				if (pptr->port_fcp_dma != FC_NO_DVMA_SPACE) {
11707 					FCP_CP_IN(cmd->cmd_fp_pkt->pkt_resp +
11708 					    sizeof (struct fcp_rsp), rsp_info,
11709 					    cmd->cmd_fp_pkt->pkt_resp_acc,
11710 					    sizeof (struct fcp_rsp_info));
11711 				}
11712 				if (rsp_info->rsp_code == FCP_NO_FAILURE) {
11713 					rval = FC_SUCCESS;
11714 					error = 0;
11715 				} else {
11716 					error = 1;
11717 				}
11718 			} else {
11719 				error = 2;
11720 			}
11721 		}
11722 
11723 		switch (error) {
11724 		case 0:
11725 			fcp_log(CE_WARN, pptr->port_dip,
11726 			    "!FCP: WWN 0x%08x%08x %s reset successfully",
11727 			    *((int *)&ptgt->tgt_port_wwn.raw_wwn[0]),
11728 			    *((int *)&ptgt->tgt_port_wwn.raw_wwn[4]), lun_id);
11729 			break;
11730 
11731 		case 1:
11732 			fcp_log(CE_WARN, pptr->port_dip,
11733 			    "!FCP: Reset to WWN	 0x%08x%08x %s failed,"
11734 			    " response code=%x",
11735 			    *((int *)&ptgt->tgt_port_wwn.raw_wwn[0]),
11736 			    *((int *)&ptgt->tgt_port_wwn.raw_wwn[4]), lun_id,
11737 			    rsp_info->rsp_code);
11738 			break;
11739 
11740 		case 2:
11741 			fcp_log(CE_WARN, pptr->port_dip,
11742 			    "!FCP: Reset to WWN 0x%08x%08x %s failed,"
11743 			    " Bad FCP response values: rsvd1=%x,"
11744 			    " rsvd2=%x, sts-rsvd1=%x, sts-rsvd2=%x,"
11745 			    " rsplen=%x, senselen=%x",
11746 			    *((int *)&ptgt->tgt_port_wwn.raw_wwn[0]),
11747 			    *((int *)&ptgt->tgt_port_wwn.raw_wwn[4]), lun_id,
11748 			    rsp->reserved_0, rsp->reserved_1,
11749 			    rsp->fcp_u.fcp_status.reserved_0,
11750 			    rsp->fcp_u.fcp_status.reserved_1,
11751 			    rsp->fcp_response_len, rsp->fcp_sense_len);
11752 			break;
11753 
11754 		default:
11755 			fcp_log(CE_WARN, pptr->port_dip,
11756 			    "!FCP: Reset to WWN	 0x%08x%08x %s failed",
11757 			    *((int *)&ptgt->tgt_port_wwn.raw_wwn[0]),
11758 			    *((int *)&ptgt->tgt_port_wwn.raw_wwn[4]), lun_id);
11759 			break;
11760 		}
11761 	}
11762 	scsi_destroy_pkt(pkt);
11763 
11764 	if (rval == FC_FAILURE) {
11765 		mutex_enter(&ptgt->tgt_mutex);
11766 		if (level == RESET_TARGET) {
11767 			fcp_update_tgt_state(ptgt, FCP_RESET, FCP_LUN_BUSY);
11768 		} else {
11769 			fcp_update_lun_state(plun, FCP_RESET, FCP_LUN_BUSY);
11770 		}
11771 		mutex_exit(&ptgt->tgt_mutex);
11772 		kmem_free(p, sizeof (struct fcp_reset_elem));
11773 		return (rval);
11774 	}
11775 
11776 	mutex_enter(&pptr->port_mutex);
11777 	if (level == RESET_TARGET) {
11778 		p->tgt = ptgt;
11779 		p->lun = NULL;
11780 	} else {
11781 		p->tgt = NULL;
11782 		p->lun = plun;
11783 	}
11784 	p->tgt = ptgt;
11785 	p->tgt_cnt = tgt_cnt;
11786 	p->timeout = fcp_watchdog_time + FCP_RESET_DELAY;
11787 	p->next = pptr->port_reset_list;
11788 	pptr->port_reset_list = p;
11789 
11790 	FCP_TRACE(fcp_logq, pptr->port_instbuf,
11791 	    fcp_trace, FCP_BUF_LEVEL_3, 0,
11792 	    "Notify ssd of the reset to reinstate the reservations");
11793 
11794 	scsi_hba_reset_notify_callback(&pptr->port_mutex,
11795 	    &pptr->port_reset_notify_listf);
11796 
11797 	mutex_exit(&pptr->port_mutex);
11798 
11799 	return (rval);
11800 }
11801 
11802 
11803 /*
11804  * called by fcp_getcap and fcp_setcap to get and set (respectively)
11805  * SCSI capabilities
11806  */
11807 /* ARGSUSED */
11808 static int
11809 fcp_commoncap(struct scsi_address *ap, char *cap,
11810     int val, int tgtonly, int doset)
11811 {
11812 	struct fcp_port		*pptr = ADDR2FCP(ap);
11813 	struct fcp_lun	*plun = ADDR2LUN(ap);
11814 	struct fcp_tgt	*ptgt = plun->lun_tgt;
11815 	int			cidx;
11816 	int			rval = FALSE;
11817 
11818 	if (cap == (char *)0) {
11819 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
11820 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
11821 		    "fcp_commoncap: invalid arg");
11822 		return (rval);
11823 	}
11824 
11825 	if ((cidx = scsi_hba_lookup_capstr(cap)) == -1) {
11826 		return (UNDEFINED);
11827 	}
11828 
11829 	/*
11830 	 * Process setcap request.
11831 	 */
11832 	if (doset) {
11833 		/*
11834 		 * At present, we can only set binary (0/1) values
11835 		 */
11836 		switch (cidx) {
11837 		case SCSI_CAP_ARQ:
11838 			if (val == 0) {
11839 				rval = FALSE;
11840 			} else {
11841 				rval = TRUE;
11842 			}
11843 			break;
11844 
11845 		case SCSI_CAP_LUN_RESET:
11846 			if (val) {
11847 				plun->lun_cap |= FCP_LUN_CAP_RESET;
11848 			} else {
11849 				plun->lun_cap &= ~FCP_LUN_CAP_RESET;
11850 			}
11851 			rval = TRUE;
11852 			break;
11853 
11854 		case SCSI_CAP_SECTOR_SIZE:
11855 			rval = TRUE;
11856 			break;
11857 		default:
11858 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
11859 			    fcp_trace, FCP_BUF_LEVEL_4, 0,
11860 			    "fcp_setcap: unsupported %d", cidx);
11861 			rval = UNDEFINED;
11862 			break;
11863 		}
11864 
11865 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
11866 		    fcp_trace, FCP_BUF_LEVEL_5, 0,
11867 		    "set cap: cap=%s, val/tgtonly/doset/rval = "
11868 		    "0x%x/0x%x/0x%x/%d",
11869 		    cap, val, tgtonly, doset, rval);
11870 
11871 	} else {
11872 		/*
11873 		 * Process getcap request.
11874 		 */
11875 		switch (cidx) {
11876 		case SCSI_CAP_DMA_MAX:
11877 			rval = (int)pptr->port_data_dma_attr.dma_attr_maxxfer;
11878 
11879 			/*
11880 			 * Need to make an adjustment qlc is uint_t 64
11881 			 * st is int, so we will make the adjustment here
11882 			 * being as nobody wants to touch this.
11883 			 * It still leaves the max single block length
11884 			 * of 2 gig. This should last .
11885 			 */
11886 
11887 			if (rval == -1) {
11888 				rval = MAX_INT_DMA;
11889 			}
11890 
11891 			break;
11892 
11893 		case SCSI_CAP_INITIATOR_ID:
11894 			rval = pptr->port_id;
11895 			break;
11896 
11897 		case SCSI_CAP_ARQ:
11898 		case SCSI_CAP_RESET_NOTIFICATION:
11899 		case SCSI_CAP_TAGGED_QING:
11900 			rval = TRUE;
11901 			break;
11902 
11903 		case SCSI_CAP_SCSI_VERSION:
11904 			rval = 3;
11905 			break;
11906 
11907 		case SCSI_CAP_INTERCONNECT_TYPE:
11908 			if (FC_TOP_EXTERNAL(pptr->port_topology) ||
11909 			    (ptgt->tgt_hard_addr == 0)) {
11910 				rval = INTERCONNECT_FABRIC;
11911 			} else {
11912 				rval = INTERCONNECT_FIBRE;
11913 			}
11914 			break;
11915 
11916 		case SCSI_CAP_LUN_RESET:
11917 			rval = ((plun->lun_cap & FCP_LUN_CAP_RESET) != 0) ?
11918 			    TRUE : FALSE;
11919 			break;
11920 
11921 		default:
11922 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
11923 			    fcp_trace, FCP_BUF_LEVEL_4, 0,
11924 			    "fcp_getcap: unsupported %d", cidx);
11925 			rval = UNDEFINED;
11926 			break;
11927 		}
11928 
11929 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
11930 		    fcp_trace, FCP_BUF_LEVEL_8, 0,
11931 		    "get cap: cap=%s, val/tgtonly/doset/rval = "
11932 		    "0x%x/0x%x/0x%x/%d",
11933 		    cap, val, tgtonly, doset, rval);
11934 	}
11935 
11936 	return (rval);
11937 }
11938 
11939 /*
11940  * called by the transport to get the port-wwn and lun
11941  * properties of this device, and to create a "name" based on them
11942  *
11943  * these properties don't exist on sun4m
11944  *
11945  * return 1 for success else return 0
11946  */
11947 /* ARGSUSED */
11948 static int
11949 fcp_scsi_get_name(struct scsi_device *sd, char *name, int len)
11950 {
11951 	int			i;
11952 	int			*lun;
11953 	int			numChars;
11954 	uint_t			nlun;
11955 	uint_t			count;
11956 	uint_t			nbytes;
11957 	uchar_t			*bytes;
11958 	uint16_t		lun_num;
11959 	uint32_t		tgt_id;
11960 	char			**conf_wwn;
11961 	char			tbuf[(FC_WWN_SIZE << 1) + 1];
11962 	uchar_t			barray[FC_WWN_SIZE];
11963 	dev_info_t		*tgt_dip;
11964 	struct fcp_tgt	*ptgt;
11965 	struct fcp_port	*pptr;
11966 	struct fcp_lun	*plun;
11967 
11968 	ASSERT(sd != NULL);
11969 	ASSERT(name != NULL);
11970 
11971 	tgt_dip = sd->sd_dev;
11972 	pptr = ddi_get_soft_state(fcp_softstate,
11973 	    ddi_get_instance(ddi_get_parent(tgt_dip)));
11974 	if (pptr == NULL) {
11975 		return (0);
11976 	}
11977 
11978 	ASSERT(tgt_dip != NULL);
11979 
11980 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, sd->sd_dev,
11981 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
11982 	    LUN_PROP, &lun, &nlun) != DDI_SUCCESS) {
11983 		name[0] = '\0';
11984 		return (0);
11985 	}
11986 
11987 	if (nlun == 0) {
11988 		ddi_prop_free(lun);
11989 		return (0);
11990 	}
11991 
11992 	lun_num = lun[0];
11993 	ddi_prop_free(lun);
11994 
11995 	/*
11996 	 * Lookup for .conf WWN property
11997 	 */
11998 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, tgt_dip,
11999 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, CONF_WWN_PROP,
12000 	    &conf_wwn, &count) == DDI_PROP_SUCCESS) {
12001 		ASSERT(count >= 1);
12002 
12003 		fcp_ascii_to_wwn(conf_wwn[0], barray, FC_WWN_SIZE);
12004 		ddi_prop_free(conf_wwn);
12005 		mutex_enter(&pptr->port_mutex);
12006 		if ((plun = fcp_lookup_lun(pptr, barray, lun_num)) == NULL) {
12007 			mutex_exit(&pptr->port_mutex);
12008 			return (0);
12009 		}
12010 		ptgt = plun->lun_tgt;
12011 		mutex_exit(&pptr->port_mutex);
12012 
12013 		(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
12014 		    tgt_dip, PORT_WWN_PROP, barray, FC_WWN_SIZE);
12015 
12016 		if (!FC_TOP_EXTERNAL(pptr->port_topology) &&
12017 		    ptgt->tgt_hard_addr != 0) {
12018 			tgt_id = (uint32_t)fcp_alpa_to_switch[
12019 			    ptgt->tgt_hard_addr];
12020 		} else {
12021 			tgt_id = ptgt->tgt_d_id;
12022 		}
12023 
12024 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, tgt_dip,
12025 		    TARGET_PROP, tgt_id);
12026 	}
12027 
12028 	/* get the our port-wwn property */
12029 	bytes = NULL;
12030 	if ((ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, tgt_dip,
12031 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, PORT_WWN_PROP, &bytes,
12032 	    &nbytes) != DDI_PROP_SUCCESS) || nbytes != FC_WWN_SIZE) {
12033 		if (bytes != NULL) {
12034 			ddi_prop_free(bytes);
12035 		}
12036 		return (0);
12037 	}
12038 
12039 	for (i = 0; i < FC_WWN_SIZE; i++) {
12040 		(void) sprintf(&tbuf[i << 1], "%02x", *(bytes + i));
12041 	}
12042 
12043 	/* Stick in the address of the form "wWWN,LUN" */
12044 	numChars = snprintf(name, len, "w%s,%x", tbuf, lun_num);
12045 
12046 	ASSERT(numChars < len);
12047 	if (numChars >= len) {
12048 		fcp_log(CE_WARN, pptr->port_dip,
12049 		    "!fcp_scsi_get_name: "
12050 		    "name parameter length too small, it needs to be %d",
12051 		    numChars+1);
12052 	}
12053 
12054 	ddi_prop_free(bytes);
12055 
12056 	return (1);
12057 }
12058 
12059 
12060 /*
12061  * called by the transport to get the SCSI target id value, returning
12062  * it in "name"
12063  *
12064  * this isn't needed/used on sun4m
12065  *
12066  * return 1 for success else return 0
12067  */
12068 /* ARGSUSED */
12069 static int
12070 fcp_scsi_get_bus_addr(struct scsi_device *sd, char *name, int len)
12071 {
12072 	struct fcp_lun	*plun = ADDR2LUN(&sd->sd_address);
12073 	struct fcp_tgt	*ptgt;
12074 	int    numChars;
12075 
12076 	if (plun == NULL) {
12077 		return (0);
12078 	}
12079 
12080 	if ((ptgt = plun->lun_tgt) == NULL) {
12081 		return (0);
12082 	}
12083 
12084 	numChars = snprintf(name, len, "%x", ptgt->tgt_d_id);
12085 
12086 	ASSERT(numChars < len);
12087 	if (numChars >= len) {
12088 		fcp_log(CE_WARN, NULL,
12089 		    "!fcp_scsi_get_bus_addr: "
12090 		    "name parameter length too small, it needs to be %d",
12091 		    numChars+1);
12092 	}
12093 
12094 	return (1);
12095 }
12096 
12097 
12098 /*
12099  * called internally to reset the link where the specified port lives
12100  */
12101 static int
12102 fcp_linkreset(struct fcp_port *pptr, struct scsi_address *ap, int sleep)
12103 {
12104 	la_wwn_t		wwn;
12105 	struct fcp_lun	*plun;
12106 	struct fcp_tgt	*ptgt;
12107 
12108 	/* disable restart of lip if we're suspended */
12109 	mutex_enter(&pptr->port_mutex);
12110 
12111 	if (pptr->port_state & (FCP_STATE_SUSPENDED |
12112 	    FCP_STATE_POWER_DOWN)) {
12113 		mutex_exit(&pptr->port_mutex);
12114 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
12115 		    fcp_trace, FCP_BUF_LEVEL_2, 0,
12116 		    "fcp_linkreset, fcp%d: link reset "
12117 		    "disabled due to DDI_SUSPEND",
12118 		    ddi_get_instance(pptr->port_dip));
12119 		return (FC_FAILURE);
12120 	}
12121 
12122 	if (pptr->port_state & (FCP_STATE_OFFLINE | FCP_STATE_ONLINING)) {
12123 		mutex_exit(&pptr->port_mutex);
12124 		return (FC_SUCCESS);
12125 	}
12126 
12127 	FCP_DTRACE(fcp_logq, pptr->port_instbuf,
12128 	    fcp_trace, FCP_BUF_LEVEL_8, 0, "Forcing link reset");
12129 
12130 	/*
12131 	 * If ap == NULL assume local link reset.
12132 	 */
12133 	if (FC_TOP_EXTERNAL(pptr->port_topology) && (ap != NULL)) {
12134 		plun = ADDR2LUN(ap);
12135 		ptgt = plun->lun_tgt;
12136 		bcopy(&ptgt->tgt_port_wwn.raw_wwn[0], &wwn, sizeof (wwn));
12137 	} else {
12138 		bzero((caddr_t)&wwn, sizeof (wwn));
12139 	}
12140 	mutex_exit(&pptr->port_mutex);
12141 
12142 	return (fc_ulp_linkreset(pptr->port_fp_handle, &wwn, sleep));
12143 }
12144 
12145 
12146 /*
12147  * called from fcp_port_attach() to resume a port
12148  * return DDI_* success/failure status
12149  * acquires and releases the global mutex
12150  * acquires and releases the port mutex
12151  */
12152 /*ARGSUSED*/
12153 
12154 static int
12155 fcp_handle_port_resume(opaque_t ulph, fc_ulp_port_info_t *pinfo,
12156     uint32_t s_id, fc_attach_cmd_t cmd, int instance)
12157 {
12158 	int			res = DDI_FAILURE; /* default result */
12159 	struct fcp_port	*pptr;		/* port state ptr */
12160 	uint32_t		alloc_cnt;
12161 	uint32_t		max_cnt;
12162 	fc_portmap_t		*tmp_list = NULL;
12163 
12164 	FCP_DTRACE(fcp_logq, "fcp", fcp_trace,
12165 	    FCP_BUF_LEVEL_8, 0, "port resume: for port %d",
12166 	    instance);
12167 
12168 	if ((pptr = ddi_get_soft_state(fcp_softstate, instance)) == NULL) {
12169 		cmn_err(CE_WARN, "fcp: bad soft state");
12170 		return (res);
12171 	}
12172 
12173 	mutex_enter(&pptr->port_mutex);
12174 	switch (cmd) {
12175 	case FC_CMD_RESUME:
12176 		ASSERT((pptr->port_state & FCP_STATE_POWER_DOWN) == 0);
12177 		pptr->port_state &= ~FCP_STATE_SUSPENDED;
12178 		break;
12179 
12180 	case FC_CMD_POWER_UP:
12181 		/*
12182 		 * If the port is DDI_SUSPENded, defer rediscovery
12183 		 * until DDI_RESUME occurs
12184 		 */
12185 		if (pptr->port_state & FCP_STATE_SUSPENDED) {
12186 			pptr->port_state &= ~FCP_STATE_POWER_DOWN;
12187 			mutex_exit(&pptr->port_mutex);
12188 			return (DDI_SUCCESS);
12189 		}
12190 		pptr->port_state &= ~FCP_STATE_POWER_DOWN;
12191 	}
12192 	pptr->port_id = s_id;
12193 	pptr->port_state = FCP_STATE_INIT;
12194 	mutex_exit(&pptr->port_mutex);
12195 
12196 	/*
12197 	 * Make a copy of ulp_port_info as fctl allocates
12198 	 * a temp struct.
12199 	 */
12200 	(void) fcp_cp_pinfo(pptr, pinfo);
12201 
12202 	mutex_enter(&fcp_global_mutex);
12203 	if (fcp_watchdog_init++ == 0) {
12204 		fcp_watchdog_tick = fcp_watchdog_timeout *
12205 		    drv_usectohz(1000000);
12206 		fcp_watchdog_id = timeout(fcp_watch,
12207 		    NULL, fcp_watchdog_tick);
12208 	}
12209 	mutex_exit(&fcp_global_mutex);
12210 
12211 	/*
12212 	 * Handle various topologies and link states.
12213 	 */
12214 	switch (FC_PORT_STATE_MASK(pptr->port_phys_state)) {
12215 	case FC_STATE_OFFLINE:
12216 		/*
12217 		 * Wait for ONLINE, at which time a state
12218 		 * change will cause a statec_callback
12219 		 */
12220 		res = DDI_SUCCESS;
12221 		break;
12222 
12223 	case FC_STATE_ONLINE:
12224 
12225 		if (pptr->port_topology == FC_TOP_UNKNOWN) {
12226 			(void) fcp_linkreset(pptr, NULL, KM_NOSLEEP);
12227 			res = DDI_SUCCESS;
12228 			break;
12229 		}
12230 
12231 		if (FC_TOP_EXTERNAL(pptr->port_topology) &&
12232 		    !fcp_enable_auto_configuration) {
12233 			tmp_list = fcp_construct_map(pptr, &alloc_cnt);
12234 			if (tmp_list == NULL) {
12235 				if (!alloc_cnt) {
12236 					res = DDI_SUCCESS;
12237 				}
12238 				break;
12239 			}
12240 			max_cnt = alloc_cnt;
12241 		} else {
12242 			ASSERT(pptr->port_topology != FC_TOP_UNKNOWN);
12243 
12244 			alloc_cnt = FCP_MAX_DEVICES;
12245 
12246 			if ((tmp_list = (fc_portmap_t *)kmem_zalloc(
12247 			    (sizeof (fc_portmap_t)) * alloc_cnt,
12248 			    KM_NOSLEEP)) == NULL) {
12249 				fcp_log(CE_WARN, pptr->port_dip,
12250 				    "!fcp%d: failed to allocate portmap",
12251 				    instance);
12252 				break;
12253 			}
12254 
12255 			max_cnt = alloc_cnt;
12256 			if ((res = fc_ulp_getportmap(pptr->port_fp_handle,
12257 			    &tmp_list, &max_cnt, FC_ULP_PLOGI_PRESERVE)) !=
12258 			    FC_SUCCESS) {
12259 				caddr_t msg;
12260 
12261 				(void) fc_ulp_error(res, &msg);
12262 
12263 				FCP_TRACE(fcp_logq, pptr->port_instbuf,
12264 				    fcp_trace, FCP_BUF_LEVEL_2, 0,
12265 				    "resume failed getportmap: reason=0x%x",
12266 				    res);
12267 
12268 				fcp_log(CE_WARN, pptr->port_dip,
12269 				    "!failed to get port map : %s", msg);
12270 				break;
12271 			}
12272 			if (max_cnt > alloc_cnt) {
12273 				alloc_cnt = max_cnt;
12274 			}
12275 		}
12276 
12277 		/*
12278 		 * do the SCSI device discovery and create
12279 		 * the devinfos
12280 		 */
12281 		fcp_statec_callback(ulph, pptr->port_fp_handle,
12282 		    pptr->port_phys_state, pptr->port_topology, tmp_list,
12283 		    max_cnt, pptr->port_id);
12284 
12285 		res = DDI_SUCCESS;
12286 		break;
12287 
12288 	default:
12289 		fcp_log(CE_WARN, pptr->port_dip,
12290 		    "!fcp%d: invalid port state at attach=0x%x",
12291 		    instance, pptr->port_phys_state);
12292 
12293 		mutex_enter(&pptr->port_mutex);
12294 		pptr->port_phys_state = FCP_STATE_OFFLINE;
12295 		mutex_exit(&pptr->port_mutex);
12296 		res = DDI_SUCCESS;
12297 
12298 		break;
12299 	}
12300 
12301 	if (tmp_list != NULL) {
12302 		kmem_free(tmp_list, sizeof (fc_portmap_t) * alloc_cnt);
12303 	}
12304 
12305 	return (res);
12306 }
12307 
12308 
12309 static void
12310 fcp_cp_pinfo(struct fcp_port *pptr, fc_ulp_port_info_t *pinfo)
12311 {
12312 	pptr->port_fp_modlinkage = *pinfo->port_linkage;
12313 	pptr->port_dip = pinfo->port_dip;
12314 	pptr->port_fp_handle = pinfo->port_handle;
12315 	if (pinfo->port_acc_attr != NULL) {
12316 		/*
12317 		 * FCA supports DMA
12318 		 */
12319 		pptr->port_data_dma_attr = *pinfo->port_data_dma_attr;
12320 		pptr->port_cmd_dma_attr = *pinfo->port_cmd_dma_attr;
12321 		pptr->port_resp_dma_attr = *pinfo->port_resp_dma_attr;
12322 		pptr->port_dma_acc_attr = *pinfo->port_acc_attr;
12323 	}
12324 	pptr->port_priv_pkt_len = pinfo->port_fca_pkt_size;
12325 	pptr->port_max_exch = pinfo->port_fca_max_exch;
12326 	pptr->port_phys_state = pinfo->port_state;
12327 	pptr->port_topology = pinfo->port_flags;
12328 	pptr->port_reset_action = pinfo->port_reset_action;
12329 	pptr->port_cmds_dma_flags = pinfo->port_dma_behavior;
12330 	pptr->port_fcp_dma = pinfo->port_fcp_dma;
12331 	bcopy(&pinfo->port_nwwn, &pptr->port_nwwn, sizeof (la_wwn_t));
12332 	bcopy(&pinfo->port_pwwn, &pptr->port_pwwn, sizeof (la_wwn_t));
12333 
12334 	/* Clear FMA caps to avoid fm-capability ereport */
12335 	if (pptr->port_cmd_dma_attr.dma_attr_flags & DDI_DMA_FLAGERR)
12336 		pptr->port_cmd_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
12337 	if (pptr->port_data_dma_attr.dma_attr_flags & DDI_DMA_FLAGERR)
12338 		pptr->port_data_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
12339 	if (pptr->port_resp_dma_attr.dma_attr_flags & DDI_DMA_FLAGERR)
12340 		pptr->port_resp_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
12341 }
12342 
12343 /*
12344  * If the elements wait field is set to 1 then
12345  * another thread is waiting for the operation to complete. Once
12346  * it is complete, the waiting thread is signaled and the element is
12347  * freed by the waiting thread. If the elements wait field is set to 0
12348  * the element is freed.
12349  */
12350 static void
12351 fcp_process_elem(struct fcp_hp_elem *elem, int result)
12352 {
12353 	ASSERT(elem != NULL);
12354 	mutex_enter(&elem->mutex);
12355 	elem->result = result;
12356 	if (elem->wait) {
12357 		elem->wait = 0;
12358 		cv_signal(&elem->cv);
12359 		mutex_exit(&elem->mutex);
12360 	} else {
12361 		mutex_exit(&elem->mutex);
12362 		cv_destroy(&elem->cv);
12363 		mutex_destroy(&elem->mutex);
12364 		kmem_free(elem, sizeof (struct fcp_hp_elem));
12365 	}
12366 }
12367 
12368 /*
12369  * This function is invoked from the taskq thread to allocate
12370  * devinfo nodes and to online/offline them.
12371  */
12372 static void
12373 fcp_hp_task(void *arg)
12374 {
12375 	struct fcp_hp_elem	*elem = (struct fcp_hp_elem *)arg;
12376 	struct fcp_lun	*plun = elem->lun;
12377 	struct fcp_port		*pptr = elem->port;
12378 	int			result;
12379 
12380 	ASSERT(elem->what == FCP_ONLINE ||
12381 	    elem->what == FCP_OFFLINE ||
12382 	    elem->what == FCP_MPXIO_PATH_CLEAR_BUSY ||
12383 	    elem->what == FCP_MPXIO_PATH_SET_BUSY);
12384 
12385 	mutex_enter(&pptr->port_mutex);
12386 	mutex_enter(&plun->lun_mutex);
12387 	if (((elem->what == FCP_ONLINE || elem->what == FCP_OFFLINE) &&
12388 	    plun->lun_event_count != elem->event_cnt) ||
12389 	    pptr->port_state & (FCP_STATE_SUSPENDED |
12390 	    FCP_STATE_DETACHING | FCP_STATE_POWER_DOWN)) {
12391 		mutex_exit(&plun->lun_mutex);
12392 		mutex_exit(&pptr->port_mutex);
12393 		fcp_process_elem(elem, NDI_FAILURE);
12394 		return;
12395 	}
12396 	mutex_exit(&plun->lun_mutex);
12397 	mutex_exit(&pptr->port_mutex);
12398 
12399 	result = fcp_trigger_lun(plun, elem->cip, elem->old_lun_mpxio,
12400 	    elem->what, elem->link_cnt, elem->tgt_cnt, elem->flags);
12401 	fcp_process_elem(elem, result);
12402 }
12403 
12404 
12405 static child_info_t *
12406 fcp_get_cip(struct fcp_lun *plun, child_info_t *cip, int lcount,
12407     int tcount)
12408 {
12409 	ASSERT(MUTEX_HELD(&plun->lun_mutex));
12410 
12411 	if (fcp_is_child_present(plun, cip) == FC_FAILURE) {
12412 		struct fcp_port *pptr = plun->lun_tgt->tgt_port;
12413 
12414 		ASSERT(MUTEX_HELD(&pptr->port_mutex));
12415 		/*
12416 		 * Child has not been created yet. Create the child device
12417 		 * based on the per-Lun flags.
12418 		 */
12419 		if (pptr->port_mpxio == 0 || plun->lun_mpxio == 0) {
12420 			plun->lun_cip =
12421 			    CIP(fcp_create_dip(plun, lcount, tcount));
12422 			plun->lun_mpxio = 0;
12423 		} else {
12424 			plun->lun_cip =
12425 			    CIP(fcp_create_pip(plun, lcount, tcount));
12426 			plun->lun_mpxio = 1;
12427 		}
12428 	} else {
12429 		plun->lun_cip = cip;
12430 	}
12431 
12432 	return (plun->lun_cip);
12433 }
12434 
12435 
12436 static int
12437 fcp_is_dip_present(struct fcp_lun *plun, dev_info_t *cdip)
12438 {
12439 	int		rval = FC_FAILURE;
12440 	dev_info_t	*pdip;
12441 	struct dev_info	*dip;
12442 	int		circular;
12443 
12444 	ASSERT(MUTEX_HELD(&plun->lun_mutex));
12445 
12446 	pdip = plun->lun_tgt->tgt_port->port_dip;
12447 
12448 	if (plun->lun_cip == NULL) {
12449 		FCP_TRACE(fcp_logq, LUN_PORT->port_instbuf,
12450 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
12451 		    "fcp_is_dip_present: plun->lun_cip is NULL: "
12452 		    "plun: %p lun state: %x num: %d target state: %x",
12453 		    plun, plun->lun_state, plun->lun_num,
12454 		    plun->lun_tgt->tgt_port->port_state);
12455 		return (rval);
12456 	}
12457 	ndi_devi_enter(pdip, &circular);
12458 	dip = DEVI(pdip)->devi_child;
12459 	while (dip) {
12460 		if (dip == DEVI(cdip)) {
12461 			rval = FC_SUCCESS;
12462 			break;
12463 		}
12464 		dip = dip->devi_sibling;
12465 	}
12466 	ndi_devi_exit(pdip, circular);
12467 	return (rval);
12468 }
12469 
12470 static int
12471 fcp_is_child_present(struct fcp_lun *plun, child_info_t *cip)
12472 {
12473 	int		rval = FC_FAILURE;
12474 
12475 	ASSERT(plun != NULL);
12476 	ASSERT(MUTEX_HELD(&plun->lun_mutex));
12477 
12478 	if (plun->lun_mpxio == 0) {
12479 		rval = fcp_is_dip_present(plun, DIP(cip));
12480 	} else {
12481 		rval = fcp_is_pip_present(plun, PIP(cip));
12482 	}
12483 
12484 	return (rval);
12485 }
12486 
12487 /*
12488  *     Function: fcp_create_dip
12489  *
12490  *  Description: Creates a dev_info_t structure for the LUN specified by the
12491  *		 caller.
12492  *
12493  *     Argument: plun		Lun structure
12494  *		 link_cnt	Link state count.
12495  *		 tgt_cnt	Target state change count.
12496  *
12497  * Return Value: NULL if it failed
12498  *		 dev_info_t structure address if it succeeded
12499  *
12500  *	Context: Kernel context
12501  */
12502 static dev_info_t *
12503 fcp_create_dip(struct fcp_lun *plun, int link_cnt, int tgt_cnt)
12504 {
12505 	int			failure = 0;
12506 	uint32_t		tgt_id;
12507 	uint64_t		sam_lun;
12508 	struct fcp_tgt	*ptgt = plun->lun_tgt;
12509 	struct fcp_port	*pptr = ptgt->tgt_port;
12510 	dev_info_t		*pdip = pptr->port_dip;
12511 	dev_info_t		*cdip = NULL;
12512 	dev_info_t		*old_dip = DIP(plun->lun_cip);
12513 	char			*nname = NULL;
12514 	char			**compatible = NULL;
12515 	int			ncompatible;
12516 	char			*scsi_binding_set;
12517 	char			t_pwwn[17];
12518 
12519 	ASSERT(MUTEX_HELD(&plun->lun_mutex));
12520 	ASSERT(MUTEX_HELD(&pptr->port_mutex));
12521 
12522 	/* get the 'scsi-binding-set' property */
12523 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip,
12524 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, "scsi-binding-set",
12525 	    &scsi_binding_set) != DDI_PROP_SUCCESS) {
12526 		scsi_binding_set = NULL;
12527 	}
12528 
12529 	/* determine the node name and compatible */
12530 	scsi_hba_nodename_compatible_get(&plun->lun_inq, scsi_binding_set,
12531 	    plun->lun_inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
12532 	if (scsi_binding_set) {
12533 		ddi_prop_free(scsi_binding_set);
12534 	}
12535 
12536 	if (nname == NULL) {
12537 #ifdef	DEBUG
12538 		cmn_err(CE_WARN, "%s%d: no driver for "
12539 		    "device @w%02x%02x%02x%02x%02x%02x%02x%02x,%d:"
12540 		    "	 compatible: %s",
12541 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
12542 		    ptgt->tgt_port_wwn.raw_wwn[0],
12543 		    ptgt->tgt_port_wwn.raw_wwn[1],
12544 		    ptgt->tgt_port_wwn.raw_wwn[2],
12545 		    ptgt->tgt_port_wwn.raw_wwn[3],
12546 		    ptgt->tgt_port_wwn.raw_wwn[4],
12547 		    ptgt->tgt_port_wwn.raw_wwn[5],
12548 		    ptgt->tgt_port_wwn.raw_wwn[6],
12549 		    ptgt->tgt_port_wwn.raw_wwn[7], plun->lun_num,
12550 		    *compatible);
12551 #endif	/* DEBUG */
12552 		failure++;
12553 		goto end_of_fcp_create_dip;
12554 	}
12555 
12556 	cdip = fcp_find_existing_dip(plun, pdip, nname);
12557 
12558 	/*
12559 	 * if the old_dip does not match the cdip, that means there is
12560 	 * some property change. since we'll be using the cdip, we need
12561 	 * to offline the old_dip. If the state contains FCP_LUN_CHANGED
12562 	 * then the dtype for the device has been updated. Offline the
12563 	 * the old device and create a new device with the new device type
12564 	 * Refer to bug: 4764752
12565 	 */
12566 	if (old_dip && (cdip != old_dip ||
12567 	    plun->lun_state & FCP_LUN_CHANGED)) {
12568 		plun->lun_state &= ~(FCP_LUN_INIT);
12569 		mutex_exit(&plun->lun_mutex);
12570 		mutex_exit(&pptr->port_mutex);
12571 
12572 		mutex_enter(&ptgt->tgt_mutex);
12573 		(void) fcp_pass_to_hp(pptr, plun, CIP(old_dip), FCP_OFFLINE,
12574 		    link_cnt, tgt_cnt, NDI_DEVI_REMOVE, 0);
12575 		mutex_exit(&ptgt->tgt_mutex);
12576 
12577 #ifdef DEBUG
12578 		if (cdip != NULL) {
12579 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
12580 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
12581 			    "Old dip=%p; New dip=%p don't match", old_dip,
12582 			    cdip);
12583 		} else {
12584 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
12585 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
12586 			    "Old dip=%p; New dip=NULL don't match", old_dip);
12587 		}
12588 #endif
12589 
12590 		mutex_enter(&pptr->port_mutex);
12591 		mutex_enter(&plun->lun_mutex);
12592 	}
12593 
12594 	if (cdip == NULL || plun->lun_state & FCP_LUN_CHANGED) {
12595 		plun->lun_state &= ~(FCP_LUN_CHANGED);
12596 		if (ndi_devi_alloc(pptr->port_dip, nname,
12597 		    DEVI_SID_NODEID, &cdip) != NDI_SUCCESS) {
12598 			failure++;
12599 			goto end_of_fcp_create_dip;
12600 		}
12601 	}
12602 
12603 	/*
12604 	 * Previously all the properties for the devinfo were destroyed here
12605 	 * with a call to ndi_prop_remove_all(). Since this may cause loss of
12606 	 * the devid property (and other properties established by the target
12607 	 * driver or framework) which the code does not always recreate, this
12608 	 * call was removed.
12609 	 * This opens a theoretical possibility that we may return with a
12610 	 * stale devid on the node if the scsi entity behind the fibre channel
12611 	 * lun has changed.
12612 	 */
12613 
12614 	/* decorate the node with compatible */
12615 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip,
12616 	    "compatible", compatible, ncompatible) != DDI_PROP_SUCCESS) {
12617 		failure++;
12618 		goto end_of_fcp_create_dip;
12619 	}
12620 
12621 	if (ndi_prop_update_byte_array(DDI_DEV_T_NONE, cdip, NODE_WWN_PROP,
12622 	    ptgt->tgt_node_wwn.raw_wwn, FC_WWN_SIZE) != DDI_PROP_SUCCESS) {
12623 		failure++;
12624 		goto end_of_fcp_create_dip;
12625 	}
12626 
12627 	if (ndi_prop_update_byte_array(DDI_DEV_T_NONE, cdip, PORT_WWN_PROP,
12628 	    ptgt->tgt_port_wwn.raw_wwn, FC_WWN_SIZE) != DDI_PROP_SUCCESS) {
12629 		failure++;
12630 		goto end_of_fcp_create_dip;
12631 	}
12632 
12633 	fcp_wwn_to_ascii(ptgt->tgt_port_wwn.raw_wwn, t_pwwn);
12634 	t_pwwn[16] = '\0';
12635 	if (ndi_prop_update_string(DDI_DEV_T_NONE, cdip, TGT_PORT_PROP, t_pwwn)
12636 	    != DDI_PROP_SUCCESS) {
12637 		failure++;
12638 		goto end_of_fcp_create_dip;
12639 	}
12640 
12641 	/*
12642 	 * If there is no hard address - We might have to deal with
12643 	 * that by using WWN - Having said that it is important to
12644 	 * recognize this problem early so ssd can be informed of
12645 	 * the right interconnect type.
12646 	 */
12647 	if (!FC_TOP_EXTERNAL(pptr->port_topology) && ptgt->tgt_hard_addr != 0) {
12648 		tgt_id = (uint32_t)fcp_alpa_to_switch[ptgt->tgt_hard_addr];
12649 	} else {
12650 		tgt_id = ptgt->tgt_d_id;
12651 	}
12652 
12653 	if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, TARGET_PROP,
12654 	    tgt_id) != DDI_PROP_SUCCESS) {
12655 		failure++;
12656 		goto end_of_fcp_create_dip;
12657 	}
12658 
12659 	if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, LUN_PROP,
12660 	    (int)plun->lun_num) != DDI_PROP_SUCCESS) {
12661 		failure++;
12662 		goto end_of_fcp_create_dip;
12663 	}
12664 	bcopy(&plun->lun_addr, &sam_lun, FCP_LUN_SIZE);
12665 	if (ndi_prop_update_int64(DDI_DEV_T_NONE, cdip, SAM_LUN_PROP,
12666 	    sam_lun) != DDI_PROP_SUCCESS) {
12667 		failure++;
12668 		goto end_of_fcp_create_dip;
12669 	}
12670 
12671 end_of_fcp_create_dip:
12672 	scsi_hba_nodename_compatible_free(nname, compatible);
12673 
12674 	if (cdip != NULL && failure) {
12675 		(void) ndi_prop_remove_all(cdip);
12676 		(void) ndi_devi_free(cdip);
12677 		cdip = NULL;
12678 	}
12679 
12680 	return (cdip);
12681 }
12682 
12683 /*
12684  *     Function: fcp_create_pip
12685  *
12686  *  Description: Creates a Path Id for the LUN specified by the caller.
12687  *
12688  *     Argument: plun		Lun structure
12689  *		 link_cnt	Link state count.
12690  *		 tgt_cnt	Target state count.
12691  *
12692  * Return Value: NULL if it failed
12693  *		 mdi_pathinfo_t structure address if it succeeded
12694  *
12695  *	Context: Kernel context
12696  */
12697 static mdi_pathinfo_t *
12698 fcp_create_pip(struct fcp_lun *plun, int lcount, int tcount)
12699 {
12700 	int			i;
12701 	char			buf[MAXNAMELEN];
12702 	char			uaddr[MAXNAMELEN];
12703 	int			failure = 0;
12704 	uint32_t		tgt_id;
12705 	uint64_t		sam_lun;
12706 	struct fcp_tgt	*ptgt = plun->lun_tgt;
12707 	struct fcp_port	*pptr = ptgt->tgt_port;
12708 	dev_info_t		*pdip = pptr->port_dip;
12709 	mdi_pathinfo_t		*pip = NULL;
12710 	mdi_pathinfo_t		*old_pip = PIP(plun->lun_cip);
12711 	char			*nname = NULL;
12712 	char			**compatible = NULL;
12713 	int			ncompatible;
12714 	char			*scsi_binding_set;
12715 	char			t_pwwn[17];
12716 
12717 	ASSERT(MUTEX_HELD(&plun->lun_mutex));
12718 	ASSERT(MUTEX_HELD(&pptr->port_mutex));
12719 
12720 	scsi_binding_set = "vhci";
12721 
12722 	/* determine the node name and compatible */
12723 	scsi_hba_nodename_compatible_get(&plun->lun_inq, scsi_binding_set,
12724 	    plun->lun_inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
12725 
12726 	if (nname == NULL) {
12727 #ifdef	DEBUG
12728 		cmn_err(CE_WARN, "fcp_create_dip: %s%d: no driver for "
12729 		    "device @w%02x%02x%02x%02x%02x%02x%02x%02x,%d:"
12730 		    "	 compatible: %s",
12731 		    ddi_driver_name(pdip), ddi_get_instance(pdip),
12732 		    ptgt->tgt_port_wwn.raw_wwn[0],
12733 		    ptgt->tgt_port_wwn.raw_wwn[1],
12734 		    ptgt->tgt_port_wwn.raw_wwn[2],
12735 		    ptgt->tgt_port_wwn.raw_wwn[3],
12736 		    ptgt->tgt_port_wwn.raw_wwn[4],
12737 		    ptgt->tgt_port_wwn.raw_wwn[5],
12738 		    ptgt->tgt_port_wwn.raw_wwn[6],
12739 		    ptgt->tgt_port_wwn.raw_wwn[7], plun->lun_num,
12740 		    *compatible);
12741 #endif	/* DEBUG */
12742 		failure++;
12743 		goto end_of_fcp_create_pip;
12744 	}
12745 
12746 	pip = fcp_find_existing_pip(plun, pdip);
12747 
12748 	/*
12749 	 * if the old_dip does not match the cdip, that means there is
12750 	 * some property change. since we'll be using the cdip, we need
12751 	 * to offline the old_dip. If the state contains FCP_LUN_CHANGED
12752 	 * then the dtype for the device has been updated. Offline the
12753 	 * the old device and create a new device with the new device type
12754 	 * Refer to bug: 4764752
12755 	 */
12756 	if (old_pip && (pip != old_pip ||
12757 	    plun->lun_state & FCP_LUN_CHANGED)) {
12758 		plun->lun_state &= ~(FCP_LUN_INIT);
12759 		mutex_exit(&plun->lun_mutex);
12760 		mutex_exit(&pptr->port_mutex);
12761 
12762 		mutex_enter(&ptgt->tgt_mutex);
12763 		(void) fcp_pass_to_hp(pptr, plun, CIP(old_pip),
12764 		    FCP_OFFLINE, lcount, tcount,
12765 		    NDI_DEVI_REMOVE, 0);
12766 		mutex_exit(&ptgt->tgt_mutex);
12767 
12768 		if (pip != NULL) {
12769 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
12770 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
12771 			    "Old pip=%p; New pip=%p don't match",
12772 			    old_pip, pip);
12773 		} else {
12774 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
12775 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
12776 			    "Old pip=%p; New pip=NULL don't match",
12777 			    old_pip);
12778 		}
12779 
12780 		mutex_enter(&pptr->port_mutex);
12781 		mutex_enter(&plun->lun_mutex);
12782 	}
12783 
12784 	/*
12785 	 * Since FC_WWN_SIZE is 8 bytes and its not like the
12786 	 * lun_guid_size which is dependent on the target, I don't
12787 	 * believe the same trancation happens here UNLESS the standards
12788 	 * change the FC_WWN_SIZE value to something larger than
12789 	 * MAXNAMELEN(currently 255 bytes).
12790 	 */
12791 
12792 	for (i = 0; i < FC_WWN_SIZE; i++) {
12793 		(void) sprintf(&buf[i << 1], "%02x",
12794 		    ptgt->tgt_port_wwn.raw_wwn[i]);
12795 	}
12796 
12797 	(void) snprintf(uaddr, MAXNAMELEN, "w%s,%x",
12798 	    buf, plun->lun_num);
12799 
12800 	if (pip == NULL || plun->lun_state & FCP_LUN_CHANGED) {
12801 		/*
12802 		 * Release the locks before calling into
12803 		 * mdi_pi_alloc_compatible() since this can result in a
12804 		 * callback into fcp which can result in a deadlock
12805 		 * (see bug # 4870272).
12806 		 *
12807 		 * Basically, what we are trying to avoid is the scenario where
12808 		 * one thread does ndi_devi_enter() and tries to grab
12809 		 * fcp_mutex and another does it the other way round.
12810 		 *
12811 		 * But before we do that, make sure that nobody releases the
12812 		 * port in the meantime. We can do this by setting a flag.
12813 		 */
12814 		plun->lun_state &= ~(FCP_LUN_CHANGED);
12815 		pptr->port_state |= FCP_STATE_IN_MDI;
12816 		mutex_exit(&plun->lun_mutex);
12817 		mutex_exit(&pptr->port_mutex);
12818 		if (mdi_pi_alloc_compatible(pdip, nname, plun->lun_guid,
12819 		    uaddr, compatible, ncompatible, 0, &pip) != MDI_SUCCESS) {
12820 			fcp_log(CE_WARN, pptr->port_dip,
12821 			    "!path alloc failed:0x%x", plun);
12822 			mutex_enter(&pptr->port_mutex);
12823 			mutex_enter(&plun->lun_mutex);
12824 			pptr->port_state &= ~FCP_STATE_IN_MDI;
12825 			failure++;
12826 			goto end_of_fcp_create_pip;
12827 		}
12828 		mutex_enter(&pptr->port_mutex);
12829 		mutex_enter(&plun->lun_mutex);
12830 		pptr->port_state &= ~FCP_STATE_IN_MDI;
12831 	} else {
12832 		(void) mdi_prop_remove(pip, NULL);
12833 	}
12834 
12835 	mdi_pi_set_phci_private(pip, (caddr_t)plun);
12836 
12837 	if (mdi_prop_update_byte_array(pip, NODE_WWN_PROP,
12838 	    ptgt->tgt_node_wwn.raw_wwn, FC_WWN_SIZE)
12839 	    != DDI_PROP_SUCCESS) {
12840 		failure++;
12841 		goto end_of_fcp_create_pip;
12842 	}
12843 
12844 	if (mdi_prop_update_byte_array(pip, PORT_WWN_PROP,
12845 	    ptgt->tgt_port_wwn.raw_wwn, FC_WWN_SIZE)
12846 	    != DDI_PROP_SUCCESS) {
12847 		failure++;
12848 		goto end_of_fcp_create_pip;
12849 	}
12850 
12851 	fcp_wwn_to_ascii(ptgt->tgt_port_wwn.raw_wwn, t_pwwn);
12852 	t_pwwn[16] = '\0';
12853 	if (mdi_prop_update_string(pip, TGT_PORT_PROP, t_pwwn)
12854 	    != DDI_PROP_SUCCESS) {
12855 		failure++;
12856 		goto end_of_fcp_create_pip;
12857 	}
12858 
12859 	/*
12860 	 * If there is no hard address - We might have to deal with
12861 	 * that by using WWN - Having said that it is important to
12862 	 * recognize this problem early so ssd can be informed of
12863 	 * the right interconnect type.
12864 	 */
12865 	if (!FC_TOP_EXTERNAL(pptr->port_topology) &&
12866 	    ptgt->tgt_hard_addr != 0) {
12867 		tgt_id = (uint32_t)
12868 		    fcp_alpa_to_switch[ptgt->tgt_hard_addr];
12869 	} else {
12870 		tgt_id = ptgt->tgt_d_id;
12871 	}
12872 
12873 	if (mdi_prop_update_int(pip, TARGET_PROP, tgt_id)
12874 	    != DDI_PROP_SUCCESS) {
12875 		failure++;
12876 		goto end_of_fcp_create_pip;
12877 	}
12878 
12879 	if (mdi_prop_update_int(pip, LUN_PROP, (int)plun->lun_num)
12880 	    != DDI_PROP_SUCCESS) {
12881 		failure++;
12882 		goto end_of_fcp_create_pip;
12883 	}
12884 	bcopy(&plun->lun_addr, &sam_lun, FCP_LUN_SIZE);
12885 	if (mdi_prop_update_int64(pip, SAM_LUN_PROP, sam_lun)
12886 	    != DDI_PROP_SUCCESS) {
12887 		failure++;
12888 		goto end_of_fcp_create_pip;
12889 	}
12890 
12891 end_of_fcp_create_pip:
12892 	scsi_hba_nodename_compatible_free(nname, compatible);
12893 
12894 	if (pip != NULL && failure) {
12895 		(void) mdi_prop_remove(pip, NULL);
12896 		mutex_exit(&plun->lun_mutex);
12897 		mutex_exit(&pptr->port_mutex);
12898 		(void) mdi_pi_free(pip, 0);
12899 		mutex_enter(&pptr->port_mutex);
12900 		mutex_enter(&plun->lun_mutex);
12901 		pip = NULL;
12902 	}
12903 
12904 	return (pip);
12905 }
12906 
12907 static dev_info_t *
12908 fcp_find_existing_dip(struct fcp_lun *plun, dev_info_t *pdip, caddr_t name)
12909 {
12910 	uint_t			nbytes;
12911 	uchar_t			*bytes;
12912 	uint_t			nwords;
12913 	uint32_t		tgt_id;
12914 	int			*words;
12915 	dev_info_t		*cdip;
12916 	dev_info_t		*ndip;
12917 	struct fcp_tgt	*ptgt = plun->lun_tgt;
12918 	struct fcp_port	*pptr = ptgt->tgt_port;
12919 	int			circular;
12920 
12921 	ndi_devi_enter(pdip, &circular);
12922 
12923 	ndip = (dev_info_t *)DEVI(pdip)->devi_child;
12924 	while ((cdip = ndip) != NULL) {
12925 		ndip = (dev_info_t *)DEVI(cdip)->devi_sibling;
12926 
12927 		if (strcmp(DEVI(cdip)->devi_node_name, name)) {
12928 			continue;
12929 		}
12930 
12931 		if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, cdip,
12932 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, NODE_WWN_PROP, &bytes,
12933 		    &nbytes) != DDI_PROP_SUCCESS) {
12934 			continue;
12935 		}
12936 
12937 		if (nbytes != FC_WWN_SIZE || bytes == NULL) {
12938 			if (bytes != NULL) {
12939 				ddi_prop_free(bytes);
12940 			}
12941 			continue;
12942 		}
12943 		ASSERT(bytes != NULL);
12944 
12945 		if (bcmp(bytes, ptgt->tgt_node_wwn.raw_wwn, nbytes) != 0) {
12946 			ddi_prop_free(bytes);
12947 			continue;
12948 		}
12949 
12950 		ddi_prop_free(bytes);
12951 
12952 		if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, cdip,
12953 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, PORT_WWN_PROP, &bytes,
12954 		    &nbytes) != DDI_PROP_SUCCESS) {
12955 			continue;
12956 		}
12957 
12958 		if (nbytes != FC_WWN_SIZE || bytes == NULL) {
12959 			if (bytes != NULL) {
12960 				ddi_prop_free(bytes);
12961 			}
12962 			continue;
12963 		}
12964 		ASSERT(bytes != NULL);
12965 
12966 		if (bcmp(bytes, ptgt->tgt_port_wwn.raw_wwn, nbytes) != 0) {
12967 			ddi_prop_free(bytes);
12968 			continue;
12969 		}
12970 
12971 		ddi_prop_free(bytes);
12972 
12973 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, cdip,
12974 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, TARGET_PROP, &words,
12975 		    &nwords) != DDI_PROP_SUCCESS) {
12976 			continue;
12977 		}
12978 
12979 		if (nwords != 1 || words == NULL) {
12980 			if (words != NULL) {
12981 				ddi_prop_free(words);
12982 			}
12983 			continue;
12984 		}
12985 		ASSERT(words != NULL);
12986 
12987 		/*
12988 		 * If there is no hard address - We might have to deal with
12989 		 * that by using WWN - Having said that it is important to
12990 		 * recognize this problem early so ssd can be informed of
12991 		 * the right interconnect type.
12992 		 */
12993 		if (!FC_TOP_EXTERNAL(pptr->port_topology) &&
12994 		    ptgt->tgt_hard_addr != 0) {
12995 			tgt_id =
12996 			    (uint32_t)fcp_alpa_to_switch[ptgt->tgt_hard_addr];
12997 		} else {
12998 			tgt_id = ptgt->tgt_d_id;
12999 		}
13000 
13001 		if (tgt_id != (uint32_t)*words) {
13002 			ddi_prop_free(words);
13003 			continue;
13004 		}
13005 		ddi_prop_free(words);
13006 
13007 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, cdip,
13008 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, LUN_PROP, &words,
13009 		    &nwords) != DDI_PROP_SUCCESS) {
13010 			continue;
13011 		}
13012 
13013 		if (nwords != 1 || words == NULL) {
13014 			if (words != NULL) {
13015 				ddi_prop_free(words);
13016 			}
13017 			continue;
13018 		}
13019 		ASSERT(words != NULL);
13020 
13021 		if (plun->lun_num == (uint16_t)*words) {
13022 			ddi_prop_free(words);
13023 			break;
13024 		}
13025 		ddi_prop_free(words);
13026 	}
13027 	ndi_devi_exit(pdip, circular);
13028 
13029 	return (cdip);
13030 }
13031 
13032 
13033 static int
13034 fcp_is_pip_present(struct fcp_lun *plun, mdi_pathinfo_t *pip)
13035 {
13036 	dev_info_t	*pdip;
13037 	char		buf[MAXNAMELEN];
13038 	char		uaddr[MAXNAMELEN];
13039 	int		rval = FC_FAILURE;
13040 
13041 	ASSERT(MUTEX_HELD(&plun->lun_mutex));
13042 
13043 	pdip = plun->lun_tgt->tgt_port->port_dip;
13044 
13045 	/*
13046 	 * Check if pip (and not plun->lun_cip) is NULL. plun->lun_cip can be
13047 	 * non-NULL even when the LUN is not there as in the case when a LUN is
13048 	 * configured and then deleted on the device end (for T3/T4 case). In
13049 	 * such cases, pip will be NULL.
13050 	 *
13051 	 * If the device generates an RSCN, it will end up getting offlined when
13052 	 * it disappeared and a new LUN will get created when it is rediscovered
13053 	 * on the device. If we check for lun_cip here, the LUN will not end
13054 	 * up getting onlined since this function will end up returning a
13055 	 * FC_SUCCESS.
13056 	 *
13057 	 * The behavior is different on other devices. For instance, on a HDS,
13058 	 * there was no RSCN generated by the device but the next I/O generated
13059 	 * a check condition and rediscovery got triggered that way. So, in
13060 	 * such cases, this path will not be exercised
13061 	 */
13062 	if (pip == NULL) {
13063 		FCP_TRACE(fcp_logq, LUN_PORT->port_instbuf,
13064 		    fcp_trace, FCP_BUF_LEVEL_4, 0,
13065 		    "fcp_is_pip_present: plun->lun_cip is NULL: "
13066 		    "plun: %p lun state: %x num: %d target state: %x",
13067 		    plun, plun->lun_state, plun->lun_num,
13068 		    plun->lun_tgt->tgt_port->port_state);
13069 		return (rval);
13070 	}
13071 
13072 	fcp_wwn_to_ascii(plun->lun_tgt->tgt_port_wwn.raw_wwn, buf);
13073 
13074 	(void) snprintf(uaddr, MAXNAMELEN, "w%s,%x", buf, plun->lun_num);
13075 
13076 	if (plun->lun_old_guid) {
13077 		if (mdi_pi_find(pdip, plun->lun_old_guid, uaddr) == pip) {
13078 			rval = FC_SUCCESS;
13079 		}
13080 	} else {
13081 		if (mdi_pi_find(pdip, plun->lun_guid, uaddr) == pip) {
13082 			rval = FC_SUCCESS;
13083 		}
13084 	}
13085 	return (rval);
13086 }
13087 
13088 static mdi_pathinfo_t *
13089 fcp_find_existing_pip(struct fcp_lun *plun, dev_info_t *pdip)
13090 {
13091 	char			buf[MAXNAMELEN];
13092 	char			uaddr[MAXNAMELEN];
13093 	mdi_pathinfo_t		*pip;
13094 	struct fcp_tgt	*ptgt = plun->lun_tgt;
13095 	struct fcp_port	*pptr = ptgt->tgt_port;
13096 
13097 	ASSERT(MUTEX_HELD(&pptr->port_mutex));
13098 
13099 	fcp_wwn_to_ascii(ptgt->tgt_port_wwn.raw_wwn, buf);
13100 	(void) snprintf(uaddr, MAXNAMELEN, "w%s,%x", buf, plun->lun_num);
13101 
13102 	pip = mdi_pi_find(pdip, plun->lun_guid, uaddr);
13103 
13104 	return (pip);
13105 }
13106 
13107 
13108 static int
13109 fcp_online_child(struct fcp_lun *plun, child_info_t *cip, int lcount,
13110     int tcount, int flags, int *circ)
13111 {
13112 	int			rval;
13113 	struct fcp_port		*pptr = plun->lun_tgt->tgt_port;
13114 	struct fcp_tgt	*ptgt = plun->lun_tgt;
13115 	dev_info_t		*cdip = NULL;
13116 
13117 	ASSERT(MUTEX_HELD(&pptr->port_mutex));
13118 	ASSERT(MUTEX_HELD(&plun->lun_mutex));
13119 
13120 	if (plun->lun_cip == NULL) {
13121 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
13122 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
13123 		    "fcp_online_child: plun->lun_cip is NULL: "
13124 		    "plun: %p state: %x num: %d target state: %x",
13125 		    plun, plun->lun_state, plun->lun_num,
13126 		    plun->lun_tgt->tgt_port->port_state);
13127 		return (NDI_FAILURE);
13128 	}
13129 again:
13130 	if (plun->lun_mpxio == 0) {
13131 		cdip = DIP(cip);
13132 		mutex_exit(&plun->lun_mutex);
13133 		mutex_exit(&pptr->port_mutex);
13134 
13135 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
13136 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
13137 		    "!Invoking ndi_devi_online for %s: target=%x lun=%x",
13138 		    ddi_get_name(cdip), ptgt->tgt_d_id, plun->lun_num);
13139 
13140 		/*
13141 		 * We could check for FCP_LUN_INIT here but chances
13142 		 * of getting here when it's already in FCP_LUN_INIT
13143 		 * is rare and a duplicate ndi_devi_online wouldn't
13144 		 * hurt either (as the node would already have been
13145 		 * in CF2)
13146 		 */
13147 		if (!i_ddi_devi_attached(ddi_get_parent(cdip))) {
13148 			rval = ndi_devi_bind_driver(cdip, flags);
13149 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
13150 			    fcp_trace, FCP_BUF_LEVEL_3, 0,
13151 			    "!Invoking ndi_devi_bind_driver: rval=%d", rval);
13152 		} else {
13153 			rval = ndi_devi_online(cdip, flags);
13154 		}
13155 
13156 		/*
13157 		 * We log the message into trace buffer if the device
13158 		 * is "ses" and into syslog for any other device
13159 		 * type. This is to prevent the ndi_devi_online failure
13160 		 * message that appears for V880/A5K ses devices.
13161 		 */
13162 		if (rval == NDI_SUCCESS) {
13163 			mutex_enter(&ptgt->tgt_mutex);
13164 			plun->lun_state |= FCP_LUN_INIT;
13165 			mutex_exit(&ptgt->tgt_mutex);
13166 		} else if (strncmp(ddi_node_name(cdip), "ses", 3) != 0) {
13167 			fcp_log(CE_NOTE, pptr->port_dip,
13168 			    "!ndi_devi_online:"
13169 			    " failed for %s: target=%x lun=%x %x",
13170 			    ddi_get_name(cdip), ptgt->tgt_d_id,
13171 			    plun->lun_num, rval);
13172 		} else {
13173 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
13174 			    fcp_trace, FCP_BUF_LEVEL_3, 0,
13175 			    " !ndi_devi_online:"
13176 			    " failed for %s: target=%x lun=%x %x",
13177 			    ddi_get_name(cdip), ptgt->tgt_d_id,
13178 			    plun->lun_num, rval);
13179 		}
13180 	} else {
13181 		cdip = mdi_pi_get_client(PIP(cip));
13182 		mutex_exit(&plun->lun_mutex);
13183 		mutex_exit(&pptr->port_mutex);
13184 
13185 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
13186 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
13187 		    "!Invoking mdi_pi_online for %s: target=%x lun=%x",
13188 		    ddi_get_name(cdip), ptgt->tgt_d_id, plun->lun_num);
13189 
13190 		/*
13191 		 * Hold path and exit phci to avoid deadlock with power
13192 		 * management code during mdi_pi_online.
13193 		 */
13194 		mdi_hold_path(PIP(cip));
13195 		mdi_devi_exit_phci(pptr->port_dip, *circ);
13196 
13197 		rval = mdi_pi_online(PIP(cip), flags);
13198 
13199 		mdi_devi_enter_phci(pptr->port_dip, circ);
13200 		mdi_rele_path(PIP(cip));
13201 
13202 		if (rval == MDI_SUCCESS) {
13203 			mutex_enter(&ptgt->tgt_mutex);
13204 			plun->lun_state |= FCP_LUN_INIT;
13205 			mutex_exit(&ptgt->tgt_mutex);
13206 
13207 			/*
13208 			 * Clear MPxIO path permanent disable in case
13209 			 * fcp hotplug dropped the offline event.
13210 			 */
13211 			(void) mdi_pi_enable_path(PIP(cip), DRIVER_DISABLE);
13212 
13213 		} else if (rval == MDI_NOT_SUPPORTED) {
13214 			child_info_t	*old_cip = cip;
13215 
13216 			/*
13217 			 * MPxIO does not support this device yet.
13218 			 * Enumerate in legacy mode.
13219 			 */
13220 			mutex_enter(&pptr->port_mutex);
13221 			mutex_enter(&plun->lun_mutex);
13222 			plun->lun_mpxio = 0;
13223 			plun->lun_cip = NULL;
13224 			cdip = fcp_create_dip(plun, lcount, tcount);
13225 			plun->lun_cip = cip = CIP(cdip);
13226 			if (cip == NULL) {
13227 				fcp_log(CE_WARN, pptr->port_dip,
13228 				    "!fcp_online_child: "
13229 				    "Create devinfo failed for LU=%p", plun);
13230 				mutex_exit(&plun->lun_mutex);
13231 
13232 				mutex_enter(&ptgt->tgt_mutex);
13233 				plun->lun_state |= FCP_LUN_OFFLINE;
13234 				mutex_exit(&ptgt->tgt_mutex);
13235 
13236 				mutex_exit(&pptr->port_mutex);
13237 
13238 				/*
13239 				 * free the mdi_pathinfo node
13240 				 */
13241 				(void) mdi_pi_free(PIP(old_cip), 0);
13242 			} else {
13243 				FCP_TRACE(fcp_logq, pptr->port_instbuf,
13244 				    fcp_trace, FCP_BUF_LEVEL_3, 0,
13245 				    "fcp_online_child: creating devinfo "
13246 				    "node 0x%p for plun 0x%p",
13247 				    cip, plun);
13248 				mutex_exit(&plun->lun_mutex);
13249 				mutex_exit(&pptr->port_mutex);
13250 				/*
13251 				 * free the mdi_pathinfo node
13252 				 */
13253 				(void) mdi_pi_free(PIP(old_cip), 0);
13254 				mutex_enter(&pptr->port_mutex);
13255 				mutex_enter(&plun->lun_mutex);
13256 				goto again;
13257 			}
13258 		} else {
13259 			if (cdip) {
13260 				fcp_log(CE_NOTE, pptr->port_dip,
13261 				    "!fcp_online_child: mdi_pi_online:"
13262 				    " failed for %s: target=%x lun=%x %x",
13263 				    ddi_get_name(cdip), ptgt->tgt_d_id,
13264 				    plun->lun_num, rval);
13265 			}
13266 		}
13267 		rval = (rval == MDI_SUCCESS) ? NDI_SUCCESS : NDI_FAILURE;
13268 	}
13269 
13270 	if (rval == NDI_SUCCESS) {
13271 		if (cdip) {
13272 			(void) ndi_event_retrieve_cookie(
13273 			    pptr->port_ndi_event_hdl, cdip, FCAL_INSERT_EVENT,
13274 			    &fcp_insert_eid, NDI_EVENT_NOPASS);
13275 			(void) ndi_event_run_callbacks(pptr->port_ndi_event_hdl,
13276 			    cdip, fcp_insert_eid, NULL);
13277 		}
13278 	}
13279 	mutex_enter(&pptr->port_mutex);
13280 	mutex_enter(&plun->lun_mutex);
13281 	return (rval);
13282 }
13283 
13284 /* ARGSUSED */
13285 static int
13286 fcp_offline_child(struct fcp_lun *plun, child_info_t *cip, int lcount,
13287     int tcount, int flags, int *circ)
13288 {
13289 	int rval;
13290 	struct fcp_port		*pptr = plun->lun_tgt->tgt_port;
13291 	struct fcp_tgt	*ptgt = plun->lun_tgt;
13292 	dev_info_t		*cdip;
13293 
13294 	ASSERT(MUTEX_HELD(&plun->lun_mutex));
13295 	ASSERT(MUTEX_HELD(&pptr->port_mutex));
13296 
13297 	if (plun->lun_cip == NULL) {
13298 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
13299 		    fcp_trace, FCP_BUF_LEVEL_3, 0,
13300 		    "fcp_offline_child: plun->lun_cip is NULL: "
13301 		    "plun: %p lun state: %x num: %d target state: %x",
13302 		    plun, plun->lun_state, plun->lun_num,
13303 		    plun->lun_tgt->tgt_port->port_state);
13304 		return (NDI_FAILURE);
13305 	}
13306 
13307 	if (plun->lun_mpxio == 0) {
13308 		cdip = DIP(cip);
13309 		mutex_exit(&plun->lun_mutex);
13310 		mutex_exit(&pptr->port_mutex);
13311 		rval = ndi_devi_offline(DIP(cip), flags);
13312 		if (rval != NDI_SUCCESS) {
13313 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
13314 			    fcp_trace, FCP_BUF_LEVEL_3, 0,
13315 			    "fcp_offline_child: ndi_devi_offline failed "
13316 			    "rval=%x cip=%p", rval, cip);
13317 		}
13318 	} else {
13319 		cdip = mdi_pi_get_client(PIP(cip));
13320 		mutex_exit(&plun->lun_mutex);
13321 		mutex_exit(&pptr->port_mutex);
13322 
13323 		/*
13324 		 * Exit phci to avoid deadlock with power management code
13325 		 * during mdi_pi_offline
13326 		 */
13327 		mdi_hold_path(PIP(cip));
13328 		mdi_devi_exit_phci(pptr->port_dip, *circ);
13329 
13330 		rval = mdi_pi_offline(PIP(cip), flags);
13331 
13332 		mdi_devi_enter_phci(pptr->port_dip, circ);
13333 		mdi_rele_path(PIP(cip));
13334 
13335 		if (rval == MDI_SUCCESS) {
13336 			/*
13337 			 * Clear MPxIO path permanent disable as the path is
13338 			 * already offlined.
13339 			 */
13340 			(void) mdi_pi_enable_path(PIP(cip), DRIVER_DISABLE);
13341 
13342 			if (flags & NDI_DEVI_REMOVE) {
13343 				(void) mdi_pi_free(PIP(cip), 0);
13344 			}
13345 		} else {
13346 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
13347 			    fcp_trace, FCP_BUF_LEVEL_3, 0,
13348 			    "fcp_offline_child: mdi_pi_offline failed "
13349 			    "rval=%x cip=%p", rval, cip);
13350 		}
13351 		rval = (rval == MDI_SUCCESS) ? NDI_SUCCESS : NDI_FAILURE;
13352 	}
13353 
13354 	mutex_enter(&ptgt->tgt_mutex);
13355 	plun->lun_state &= ~FCP_LUN_INIT;
13356 	mutex_exit(&ptgt->tgt_mutex);
13357 
13358 	mutex_enter(&pptr->port_mutex);
13359 	mutex_enter(&plun->lun_mutex);
13360 
13361 	if (rval == NDI_SUCCESS) {
13362 		cdip = NULL;
13363 		if (flags & NDI_DEVI_REMOVE) {
13364 			/*
13365 			 * If the guid of the LUN changes, lun_cip will not
13366 			 * equal to cip, and after offlining the LUN with the
13367 			 * old guid, we should keep lun_cip since it's the cip
13368 			 * of the LUN with the new guid.
13369 			 * Otherwise remove our reference to child node.
13370 			 */
13371 			if (plun->lun_cip == cip) {
13372 				plun->lun_cip = NULL;
13373 			}
13374 			if (plun->lun_old_guid) {
13375 				kmem_free(plun->lun_old_guid,
13376 				    plun->lun_old_guid_size);
13377 				plun->lun_old_guid = NULL;
13378 				plun->lun_old_guid_size = 0;
13379 			}
13380 		}
13381 	}
13382 
13383 	if (cdip) {
13384 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
13385 		    fcp_trace, FCP_BUF_LEVEL_3, 0, "!%s failed for %s:"
13386 		    " target=%x lun=%x", "ndi_offline",
13387 		    ddi_get_name(cdip), ptgt->tgt_d_id, plun->lun_num);
13388 	}
13389 
13390 	return (rval);
13391 }
13392 
13393 static void
13394 fcp_remove_child(struct fcp_lun *plun)
13395 {
13396 	ASSERT(MUTEX_HELD(&plun->lun_mutex));
13397 
13398 	if (fcp_is_child_present(plun, plun->lun_cip) == FC_SUCCESS) {
13399 		if (plun->lun_mpxio == 0) {
13400 			(void) ndi_prop_remove_all(DIP(plun->lun_cip));
13401 			(void) ndi_devi_free(DIP(plun->lun_cip));
13402 		} else {
13403 			mutex_exit(&plun->lun_mutex);
13404 			mutex_exit(&plun->lun_tgt->tgt_mutex);
13405 			mutex_exit(&plun->lun_tgt->tgt_port->port_mutex);
13406 			FCP_TRACE(fcp_logq,
13407 			    plun->lun_tgt->tgt_port->port_instbuf,
13408 			    fcp_trace, FCP_BUF_LEVEL_3, 0,
13409 			    "lun=%p pip freed %p", plun, plun->lun_cip);
13410 			(void) mdi_prop_remove(PIP(plun->lun_cip), NULL);
13411 			(void) mdi_pi_free(PIP(plun->lun_cip), 0);
13412 			mutex_enter(&plun->lun_tgt->tgt_port->port_mutex);
13413 			mutex_enter(&plun->lun_tgt->tgt_mutex);
13414 			mutex_enter(&plun->lun_mutex);
13415 		}
13416 	}
13417 
13418 	plun->lun_cip = NULL;
13419 }
13420 
13421 /*
13422  * called when a timeout occurs
13423  *
13424  * can be scheduled during an attach or resume (if not already running)
13425  *
13426  * one timeout is set up for all ports
13427  *
13428  * acquires and releases the global mutex
13429  */
13430 /*ARGSUSED*/
13431 static void
13432 fcp_watch(void *arg)
13433 {
13434 	struct fcp_port	*pptr;
13435 	struct fcp_ipkt	*icmd;
13436 	struct fcp_ipkt	*nicmd;
13437 	struct fcp_pkt	*cmd;
13438 	struct fcp_pkt	*ncmd;
13439 	struct fcp_pkt	*tail;
13440 	struct fcp_pkt	*pcmd;
13441 	struct fcp_pkt	*save_head;
13442 	struct fcp_port	*save_port;
13443 
13444 	/* increment global watchdog time */
13445 	fcp_watchdog_time += fcp_watchdog_timeout;
13446 
13447 	mutex_enter(&fcp_global_mutex);
13448 
13449 	/* scan each port in our list */
13450 	for (pptr = fcp_port_head; pptr != NULL; pptr = pptr->port_next) {
13451 		save_port = fcp_port_head;
13452 		pptr->port_state |= FCP_STATE_IN_WATCHDOG;
13453 		mutex_exit(&fcp_global_mutex);
13454 
13455 		mutex_enter(&pptr->port_mutex);
13456 		if (pptr->port_ipkt_list == NULL &&
13457 		    (pptr->port_state & (FCP_STATE_SUSPENDED |
13458 		    FCP_STATE_DETACHING | FCP_STATE_POWER_DOWN))) {
13459 			pptr->port_state &= ~FCP_STATE_IN_WATCHDOG;
13460 			mutex_exit(&pptr->port_mutex);
13461 			mutex_enter(&fcp_global_mutex);
13462 			goto end_of_watchdog;
13463 		}
13464 
13465 		/*
13466 		 * We check if a list of targets need to be offlined.
13467 		 */
13468 		if (pptr->port_offline_tgts) {
13469 			fcp_scan_offline_tgts(pptr);
13470 		}
13471 
13472 		/*
13473 		 * We check if a list of luns need to be offlined.
13474 		 */
13475 		if (pptr->port_offline_luns) {
13476 			fcp_scan_offline_luns(pptr);
13477 		}
13478 
13479 		/*
13480 		 * We check if a list of targets or luns need to be reset.
13481 		 */
13482 		if (pptr->port_reset_list) {
13483 			fcp_check_reset_delay(pptr);
13484 		}
13485 
13486 		mutex_exit(&pptr->port_mutex);
13487 
13488 		/*
13489 		 * This is where the pending commands (pkt) are checked for
13490 		 * timeout.
13491 		 */
13492 		mutex_enter(&pptr->port_pkt_mutex);
13493 		tail = pptr->port_pkt_tail;
13494 
13495 		for (pcmd = NULL, cmd = pptr->port_pkt_head;
13496 		    cmd != NULL; cmd = ncmd) {
13497 			ncmd = cmd->cmd_next;
13498 			/*
13499 			 * If a command is in this queue the bit CFLAG_IN_QUEUE
13500 			 * must be set.
13501 			 */
13502 			ASSERT(cmd->cmd_flags & CFLAG_IN_QUEUE);
13503 			/*
13504 			 * FCP_INVALID_TIMEOUT will be set for those
13505 			 * command that need to be failed. Mostly those
13506 			 * cmds that could not be queued down for the
13507 			 * "timeout" value. cmd->cmd_timeout is used
13508 			 * to try and requeue the command regularly.
13509 			 */
13510 			if (cmd->cmd_timeout >= fcp_watchdog_time) {
13511 				/*
13512 				 * This command hasn't timed out yet.  Let's
13513 				 * go to the next one.
13514 				 */
13515 				pcmd = cmd;
13516 				goto end_of_loop;
13517 			}
13518 
13519 			if (cmd == pptr->port_pkt_head) {
13520 				ASSERT(pcmd == NULL);
13521 				pptr->port_pkt_head = cmd->cmd_next;
13522 			} else {
13523 				ASSERT(pcmd != NULL);
13524 				pcmd->cmd_next = cmd->cmd_next;
13525 			}
13526 
13527 			if (cmd == pptr->port_pkt_tail) {
13528 				ASSERT(cmd->cmd_next == NULL);
13529 				pptr->port_pkt_tail = pcmd;
13530 				if (pcmd) {
13531 					pcmd->cmd_next = NULL;
13532 				}
13533 			}
13534 			cmd->cmd_next = NULL;
13535 
13536 			/*
13537 			 * save the current head before dropping the
13538 			 * mutex - If the head doesn't remain the
13539 			 * same after re acquiring the mutex, just
13540 			 * bail out and revisit on next tick.
13541 			 *
13542 			 * PS: The tail pointer can change as the commands
13543 			 * get requeued after failure to retransport
13544 			 */
13545 			save_head = pptr->port_pkt_head;
13546 			mutex_exit(&pptr->port_pkt_mutex);
13547 
13548 			if (cmd->cmd_fp_pkt->pkt_timeout ==
13549 			    FCP_INVALID_TIMEOUT) {
13550 				struct scsi_pkt		*pkt = cmd->cmd_pkt;
13551 				struct fcp_lun	*plun;
13552 				struct fcp_tgt	*ptgt;
13553 
13554 				plun = ADDR2LUN(&pkt->pkt_address);
13555 				ptgt = plun->lun_tgt;
13556 
13557 				FCP_TRACE(fcp_logq, pptr->port_instbuf,
13558 				    fcp_trace, FCP_BUF_LEVEL_2, 0,
13559 				    "SCSI cmd 0x%x to D_ID=%x timed out",
13560 				    pkt->pkt_cdbp[0], ptgt->tgt_d_id);
13561 
13562 				cmd->cmd_state == FCP_PKT_ABORTING ?
13563 				    fcp_fail_cmd(cmd, CMD_RESET,
13564 				    STAT_DEV_RESET) : fcp_fail_cmd(cmd,
13565 				    CMD_TIMEOUT, STAT_ABORTED);
13566 			} else {
13567 				fcp_retransport_cmd(pptr, cmd);
13568 			}
13569 			mutex_enter(&pptr->port_pkt_mutex);
13570 			if (save_head && save_head != pptr->port_pkt_head) {
13571 				/*
13572 				 * Looks like linked list got changed (mostly
13573 				 * happens when an an OFFLINE LUN code starts
13574 				 * returning overflow queue commands in
13575 				 * parallel. So bail out and revisit during
13576 				 * next tick
13577 				 */
13578 				break;
13579 			}
13580 		end_of_loop:
13581 			/*
13582 			 * Scan only upto the previously known tail pointer
13583 			 * to avoid excessive processing - lots of new packets
13584 			 * could have been added to the tail or the old ones
13585 			 * re-queued.
13586 			 */
13587 			if (cmd == tail) {
13588 				break;
13589 			}
13590 		}
13591 		mutex_exit(&pptr->port_pkt_mutex);
13592 
13593 		mutex_enter(&pptr->port_mutex);
13594 		for (icmd = pptr->port_ipkt_list; icmd != NULL; icmd = nicmd) {
13595 			struct fcp_tgt *ptgt = icmd->ipkt_tgt;
13596 
13597 			nicmd = icmd->ipkt_next;
13598 			if ((icmd->ipkt_restart != 0) &&
13599 			    (icmd->ipkt_restart >= fcp_watchdog_time)) {
13600 				/* packet has not timed out */
13601 				continue;
13602 			}
13603 
13604 			/* time for packet re-transport */
13605 			if (icmd == pptr->port_ipkt_list) {
13606 				pptr->port_ipkt_list = icmd->ipkt_next;
13607 				if (pptr->port_ipkt_list) {
13608 					pptr->port_ipkt_list->ipkt_prev =
13609 					    NULL;
13610 				}
13611 			} else {
13612 				icmd->ipkt_prev->ipkt_next = icmd->ipkt_next;
13613 				if (icmd->ipkt_next) {
13614 					icmd->ipkt_next->ipkt_prev =
13615 					    icmd->ipkt_prev;
13616 				}
13617 			}
13618 			icmd->ipkt_next = NULL;
13619 			icmd->ipkt_prev = NULL;
13620 			mutex_exit(&pptr->port_mutex);
13621 
13622 			if (fcp_is_retryable(icmd)) {
13623 				fc_ulp_rscn_info_t *rscnp =
13624 				    (fc_ulp_rscn_info_t *)icmd->ipkt_fpkt->
13625 				    pkt_ulp_rscn_infop;
13626 
13627 				FCP_TRACE(fcp_logq, pptr->port_instbuf,
13628 				    fcp_trace, FCP_BUF_LEVEL_2, 0,
13629 				    "%x to D_ID=%x Retrying..",
13630 				    icmd->ipkt_opcode,
13631 				    icmd->ipkt_fpkt->pkt_cmd_fhdr.d_id);
13632 
13633 				/*
13634 				 * Update the RSCN count in the packet
13635 				 * before resending.
13636 				 */
13637 
13638 				if (rscnp != NULL) {
13639 					rscnp->ulp_rscn_count =
13640 					    fc_ulp_get_rscn_count(pptr->
13641 					    port_fp_handle);
13642 				}
13643 
13644 				mutex_enter(&pptr->port_mutex);
13645 				mutex_enter(&ptgt->tgt_mutex);
13646 				if (!FCP_STATE_CHANGED(pptr, ptgt, icmd)) {
13647 					mutex_exit(&ptgt->tgt_mutex);
13648 					mutex_exit(&pptr->port_mutex);
13649 					switch (icmd->ipkt_opcode) {
13650 						int rval;
13651 					case LA_ELS_PLOGI:
13652 						if ((rval = fc_ulp_login(
13653 						    pptr->port_fp_handle,
13654 						    &icmd->ipkt_fpkt, 1)) ==
13655 						    FC_SUCCESS) {
13656 							mutex_enter(
13657 							    &pptr->port_mutex);
13658 							continue;
13659 						}
13660 						if (fcp_handle_ipkt_errors(
13661 						    pptr, ptgt, icmd, rval,
13662 						    "PLOGI") == DDI_SUCCESS) {
13663 							mutex_enter(
13664 							    &pptr->port_mutex);
13665 							continue;
13666 						}
13667 						break;
13668 
13669 					case LA_ELS_PRLI:
13670 						if ((rval = fc_ulp_issue_els(
13671 						    pptr->port_fp_handle,
13672 						    icmd->ipkt_fpkt)) ==
13673 						    FC_SUCCESS) {
13674 							mutex_enter(
13675 							    &pptr->port_mutex);
13676 							continue;
13677 						}
13678 						if (fcp_handle_ipkt_errors(
13679 						    pptr, ptgt, icmd, rval,
13680 						    "PRLI") == DDI_SUCCESS) {
13681 							mutex_enter(
13682 							    &pptr->port_mutex);
13683 							continue;
13684 						}
13685 						break;
13686 
13687 					default:
13688 						if ((rval = fcp_transport(
13689 						    pptr->port_fp_handle,
13690 						    icmd->ipkt_fpkt, 1)) ==
13691 						    FC_SUCCESS) {
13692 							mutex_enter(
13693 							    &pptr->port_mutex);
13694 							continue;
13695 						}
13696 						if (fcp_handle_ipkt_errors(
13697 						    pptr, ptgt, icmd, rval,
13698 						    "PRLI") == DDI_SUCCESS) {
13699 							mutex_enter(
13700 							    &pptr->port_mutex);
13701 							continue;
13702 						}
13703 						break;
13704 					}
13705 				} else {
13706 					mutex_exit(&ptgt->tgt_mutex);
13707 					mutex_exit(&pptr->port_mutex);
13708 				}
13709 			} else {
13710 				fcp_print_error(icmd->ipkt_fpkt);
13711 			}
13712 
13713 			(void) fcp_call_finish_init(pptr, ptgt,
13714 			    icmd->ipkt_link_cnt, icmd->ipkt_change_cnt,
13715 			    icmd->ipkt_cause);
13716 			fcp_icmd_free(pptr, icmd);
13717 			mutex_enter(&pptr->port_mutex);
13718 		}
13719 
13720 		pptr->port_state &= ~FCP_STATE_IN_WATCHDOG;
13721 		mutex_exit(&pptr->port_mutex);
13722 		mutex_enter(&fcp_global_mutex);
13723 
13724 	end_of_watchdog:
13725 		/*
13726 		 * Bail out early before getting into trouble
13727 		 */
13728 		if (save_port != fcp_port_head) {
13729 			break;
13730 		}
13731 	}
13732 
13733 	if (fcp_watchdog_init > 0) {
13734 		/* reschedule timeout to go again */
13735 		fcp_watchdog_id =
13736 		    timeout(fcp_watch, NULL, fcp_watchdog_tick);
13737 	}
13738 	mutex_exit(&fcp_global_mutex);
13739 }
13740 
13741 
13742 static void
13743 fcp_check_reset_delay(struct fcp_port *pptr)
13744 {
13745 	uint32_t		tgt_cnt;
13746 	int			level;
13747 	struct fcp_tgt	*ptgt;
13748 	struct fcp_lun	*plun;
13749 	struct fcp_reset_elem *cur = NULL;
13750 	struct fcp_reset_elem *next = NULL;
13751 	struct fcp_reset_elem *prev = NULL;
13752 
13753 	ASSERT(mutex_owned(&pptr->port_mutex));
13754 
13755 	next = pptr->port_reset_list;
13756 	while ((cur = next) != NULL) {
13757 		next = cur->next;
13758 
13759 		if (cur->timeout < fcp_watchdog_time) {
13760 			prev = cur;
13761 			continue;
13762 		}
13763 
13764 		ptgt = cur->tgt;
13765 		plun = cur->lun;
13766 		tgt_cnt = cur->tgt_cnt;
13767 
13768 		if (ptgt) {
13769 			level = RESET_TARGET;
13770 		} else {
13771 			ASSERT(plun != NULL);
13772 			level = RESET_LUN;
13773 			ptgt = plun->lun_tgt;
13774 		}
13775 		if (prev) {
13776 			prev->next = next;
13777 		} else {
13778 			/*
13779 			 * Because we drop port mutex while doing aborts for
13780 			 * packets, we can't rely on reset_list pointing to
13781 			 * our head
13782 			 */
13783 			if (cur == pptr->port_reset_list) {
13784 				pptr->port_reset_list = next;
13785 			} else {
13786 				struct fcp_reset_elem *which;
13787 
13788 				which = pptr->port_reset_list;
13789 				while (which && which->next != cur) {
13790 					which = which->next;
13791 				}
13792 				ASSERT(which != NULL);
13793 
13794 				which->next = next;
13795 				prev = which;
13796 			}
13797 		}
13798 
13799 		kmem_free(cur, sizeof (*cur));
13800 
13801 		if (tgt_cnt == ptgt->tgt_change_cnt) {
13802 			mutex_enter(&ptgt->tgt_mutex);
13803 			if (level == RESET_TARGET) {
13804 				fcp_update_tgt_state(ptgt,
13805 				    FCP_RESET, FCP_LUN_BUSY);
13806 			} else {
13807 				fcp_update_lun_state(plun,
13808 				    FCP_RESET, FCP_LUN_BUSY);
13809 			}
13810 			mutex_exit(&ptgt->tgt_mutex);
13811 
13812 			mutex_exit(&pptr->port_mutex);
13813 			fcp_abort_all(pptr, ptgt, plun, tgt_cnt);
13814 			mutex_enter(&pptr->port_mutex);
13815 		}
13816 	}
13817 }
13818 
13819 
13820 static void
13821 fcp_abort_all(struct fcp_port *pptr, struct fcp_tgt *ttgt,
13822     struct fcp_lun *rlun, int tgt_cnt)
13823 {
13824 	int			rval;
13825 	struct fcp_lun	*tlun, *nlun;
13826 	struct fcp_pkt	*pcmd = NULL, *ncmd = NULL,
13827 	    *cmd = NULL, *head = NULL,
13828 	    *tail = NULL;
13829 
13830 	mutex_enter(&pptr->port_pkt_mutex);
13831 	for (cmd = pptr->port_pkt_head; cmd != NULL; cmd = ncmd) {
13832 		struct fcp_lun *plun = ADDR2LUN(&cmd->cmd_pkt->pkt_address);
13833 		struct fcp_tgt *ptgt = plun->lun_tgt;
13834 
13835 		ncmd = cmd->cmd_next;
13836 
13837 		if (ptgt != ttgt && plun != rlun) {
13838 			pcmd = cmd;
13839 			continue;
13840 		}
13841 
13842 		if (pcmd != NULL) {
13843 			ASSERT(pptr->port_pkt_head != cmd);
13844 			pcmd->cmd_next = ncmd;
13845 		} else {
13846 			ASSERT(cmd == pptr->port_pkt_head);
13847 			pptr->port_pkt_head = ncmd;
13848 		}
13849 		if (pptr->port_pkt_tail == cmd) {
13850 			ASSERT(cmd->cmd_next == NULL);
13851 			pptr->port_pkt_tail = pcmd;
13852 			if (pcmd != NULL) {
13853 				pcmd->cmd_next = NULL;
13854 			}
13855 		}
13856 
13857 		if (head == NULL) {
13858 			head = tail = cmd;
13859 		} else {
13860 			ASSERT(tail != NULL);
13861 			tail->cmd_next = cmd;
13862 			tail = cmd;
13863 		}
13864 		cmd->cmd_next = NULL;
13865 	}
13866 	mutex_exit(&pptr->port_pkt_mutex);
13867 
13868 	for (cmd = head; cmd != NULL; cmd = ncmd) {
13869 		struct scsi_pkt *pkt = cmd->cmd_pkt;
13870 
13871 		ncmd = cmd->cmd_next;
13872 		ASSERT(pkt != NULL);
13873 
13874 		mutex_enter(&pptr->port_mutex);
13875 		if (ttgt->tgt_change_cnt == tgt_cnt) {
13876 			mutex_exit(&pptr->port_mutex);
13877 			cmd->cmd_flags &= ~CFLAG_IN_QUEUE;
13878 			pkt->pkt_reason = CMD_RESET;
13879 			pkt->pkt_statistics |= STAT_DEV_RESET;
13880 			cmd->cmd_state = FCP_PKT_IDLE;
13881 			fcp_post_callback(cmd);
13882 		} else {
13883 			mutex_exit(&pptr->port_mutex);
13884 		}
13885 	}
13886 
13887 	/*
13888 	 * If the FCA will return all the commands in its queue then our
13889 	 * work is easy, just return.
13890 	 */
13891 
13892 	if (pptr->port_reset_action == FC_RESET_RETURN_ALL) {
13893 		return;
13894 	}
13895 
13896 	/*
13897 	 * For RESET_LUN get hold of target pointer
13898 	 */
13899 	if (ttgt == NULL) {
13900 		ASSERT(rlun != NULL);
13901 
13902 		ttgt = rlun->lun_tgt;
13903 
13904 		ASSERT(ttgt != NULL);
13905 	}
13906 
13907 	/*
13908 	 * There are some severe race conditions here.
13909 	 * While we are trying to abort the pkt, it might be completing
13910 	 * so mark it aborted and if the abort does not succeed then
13911 	 * handle it in the watch thread.
13912 	 */
13913 	mutex_enter(&ttgt->tgt_mutex);
13914 	nlun = ttgt->tgt_lun;
13915 	mutex_exit(&ttgt->tgt_mutex);
13916 	while ((tlun = nlun) != NULL) {
13917 		int restart = 0;
13918 		if (rlun && rlun != tlun) {
13919 			mutex_enter(&ttgt->tgt_mutex);
13920 			nlun = tlun->lun_next;
13921 			mutex_exit(&ttgt->tgt_mutex);
13922 			continue;
13923 		}
13924 		mutex_enter(&tlun->lun_mutex);
13925 		cmd = tlun->lun_pkt_head;
13926 		while (cmd != NULL) {
13927 			if (cmd->cmd_state == FCP_PKT_ISSUED) {
13928 				struct scsi_pkt *pkt;
13929 
13930 				restart = 1;
13931 				cmd->cmd_state = FCP_PKT_ABORTING;
13932 				mutex_exit(&tlun->lun_mutex);
13933 				rval = fc_ulp_abort(pptr->port_fp_handle,
13934 				    cmd->cmd_fp_pkt, KM_SLEEP);
13935 				if (rval == FC_SUCCESS) {
13936 					pkt = cmd->cmd_pkt;
13937 					pkt->pkt_reason = CMD_RESET;
13938 					pkt->pkt_statistics |= STAT_DEV_RESET;
13939 					cmd->cmd_state = FCP_PKT_IDLE;
13940 					fcp_post_callback(cmd);
13941 				} else {
13942 					caddr_t msg;
13943 
13944 					(void) fc_ulp_error(rval, &msg);
13945 
13946 					/*
13947 					 * This part is tricky. The abort
13948 					 * failed and now the command could
13949 					 * be completing.  The cmd_state ==
13950 					 * FCP_PKT_ABORTING should save
13951 					 * us in fcp_cmd_callback. If we
13952 					 * are already aborting ignore the
13953 					 * command in fcp_cmd_callback.
13954 					 * Here we leave this packet for 20
13955 					 * sec to be aborted in the
13956 					 * fcp_watch thread.
13957 					 */
13958 					fcp_log(CE_WARN, pptr->port_dip,
13959 					    "!Abort failed after reset %s",
13960 					    msg);
13961 
13962 					cmd->cmd_timeout =
13963 					    fcp_watchdog_time +
13964 					    cmd->cmd_pkt->pkt_time +
13965 					    FCP_FAILED_DELAY;
13966 
13967 					cmd->cmd_fp_pkt->pkt_timeout =
13968 					    FCP_INVALID_TIMEOUT;
13969 					/*
13970 					 * This is a hack, cmd is put in the
13971 					 * overflow queue so that it can be
13972 					 * timed out finally
13973 					 */
13974 					cmd->cmd_flags |= CFLAG_IN_QUEUE;
13975 
13976 					mutex_enter(&pptr->port_pkt_mutex);
13977 					if (pptr->port_pkt_head) {
13978 						ASSERT(pptr->port_pkt_tail
13979 						    != NULL);
13980 						pptr->port_pkt_tail->cmd_next
13981 						    = cmd;
13982 						pptr->port_pkt_tail = cmd;
13983 					} else {
13984 						ASSERT(pptr->port_pkt_tail
13985 						    == NULL);
13986 						pptr->port_pkt_head =
13987 						    pptr->port_pkt_tail
13988 						    = cmd;
13989 					}
13990 					cmd->cmd_next = NULL;
13991 					mutex_exit(&pptr->port_pkt_mutex);
13992 				}
13993 				mutex_enter(&tlun->lun_mutex);
13994 				cmd = tlun->lun_pkt_head;
13995 			} else {
13996 				cmd = cmd->cmd_forw;
13997 			}
13998 		}
13999 		mutex_exit(&tlun->lun_mutex);
14000 
14001 		mutex_enter(&ttgt->tgt_mutex);
14002 		restart == 1 ? (nlun = ttgt->tgt_lun) : (nlun = tlun->lun_next);
14003 		mutex_exit(&ttgt->tgt_mutex);
14004 
14005 		mutex_enter(&pptr->port_mutex);
14006 		if (tgt_cnt != ttgt->tgt_change_cnt) {
14007 			mutex_exit(&pptr->port_mutex);
14008 			return;
14009 		} else {
14010 			mutex_exit(&pptr->port_mutex);
14011 		}
14012 	}
14013 }
14014 
14015 
14016 /*
14017  * unlink the soft state, returning the soft state found (if any)
14018  *
14019  * acquires and releases the global mutex
14020  */
14021 struct fcp_port *
14022 fcp_soft_state_unlink(struct fcp_port *pptr)
14023 {
14024 	struct fcp_port	*hptr;		/* ptr index */
14025 	struct fcp_port	*tptr;		/* prev hptr */
14026 
14027 	mutex_enter(&fcp_global_mutex);
14028 	for (hptr = fcp_port_head, tptr = NULL;
14029 	    hptr != NULL;
14030 	    tptr = hptr, hptr = hptr->port_next) {
14031 		if (hptr == pptr) {
14032 			/* we found a match -- remove this item */
14033 			if (tptr == NULL) {
14034 				/* we're at the head of the list */
14035 				fcp_port_head = hptr->port_next;
14036 			} else {
14037 				tptr->port_next = hptr->port_next;
14038 			}
14039 			break;			/* success */
14040 		}
14041 	}
14042 	if (fcp_port_head == NULL) {
14043 		fcp_cleanup_blacklist(&fcp_lun_blacklist);
14044 	}
14045 	mutex_exit(&fcp_global_mutex);
14046 	return (hptr);
14047 }
14048 
14049 
14050 /*
14051  * called by fcp_scsi_hba_tgt_init to find a LUN given a
14052  * WWN and a LUN number
14053  */
14054 /* ARGSUSED */
14055 static struct fcp_lun *
14056 fcp_lookup_lun(struct fcp_port *pptr, uchar_t *wwn, uint16_t lun)
14057 {
14058 	int hash;
14059 	struct fcp_tgt *ptgt;
14060 	struct fcp_lun *plun;
14061 
14062 	ASSERT(mutex_owned(&pptr->port_mutex));
14063 
14064 	hash = FCP_HASH(wwn);
14065 	for (ptgt = pptr->port_tgt_hash_table[hash]; ptgt != NULL;
14066 	    ptgt = ptgt->tgt_next) {
14067 		if (bcmp((caddr_t)wwn, (caddr_t)&ptgt->tgt_port_wwn.raw_wwn[0],
14068 		    sizeof (ptgt->tgt_port_wwn)) == 0) {
14069 			mutex_enter(&ptgt->tgt_mutex);
14070 			for (plun = ptgt->tgt_lun;
14071 			    plun != NULL;
14072 			    plun = plun->lun_next) {
14073 				if (plun->lun_num == lun) {
14074 					mutex_exit(&ptgt->tgt_mutex);
14075 					return (plun);
14076 				}
14077 			}
14078 			mutex_exit(&ptgt->tgt_mutex);
14079 			return (NULL);
14080 		}
14081 	}
14082 	return (NULL);
14083 }
14084 
14085 /*
14086  *     Function: fcp_prepare_pkt
14087  *
14088  *  Description: This function prepares the SCSI cmd pkt, passed by the caller,
14089  *		 for fcp_start(). It binds the data or partially maps it.
14090  *		 Builds the FCP header and starts the initialization of the
14091  *		 Fibre Channel header.
14092  *
14093  *     Argument: *pptr		FCP port.
14094  *		 *cmd		FCP packet.
14095  *		 *plun		LUN the command will be sent to.
14096  *
14097  *	Context: User, Kernel and Interrupt context.
14098  */
14099 static void
14100 fcp_prepare_pkt(struct fcp_port *pptr, struct fcp_pkt *cmd,
14101     struct fcp_lun *plun)
14102 {
14103 	fc_packet_t		*fpkt = cmd->cmd_fp_pkt;
14104 	struct fcp_tgt		*ptgt = plun->lun_tgt;
14105 	struct fcp_cmd		*fcmd = &cmd->cmd_fcp_cmd;
14106 
14107 	ASSERT(cmd->cmd_pkt->pkt_comp ||
14108 	    (cmd->cmd_pkt->pkt_flags & FLAG_NOINTR));
14109 
14110 	if (cmd->cmd_pkt->pkt_numcookies) {
14111 		if (cmd->cmd_pkt->pkt_dma_flags & DDI_DMA_READ) {
14112 			fcmd->fcp_cntl.cntl_read_data = 1;
14113 			fcmd->fcp_cntl.cntl_write_data = 0;
14114 			fpkt->pkt_tran_type = FC_PKT_FCP_READ;
14115 		} else {
14116 			fcmd->fcp_cntl.cntl_read_data = 0;
14117 			fcmd->fcp_cntl.cntl_write_data = 1;
14118 			fpkt->pkt_tran_type = FC_PKT_FCP_WRITE;
14119 		}
14120 
14121 		fpkt->pkt_data_cookie = cmd->cmd_pkt->pkt_cookies;
14122 
14123 		fpkt->pkt_data_cookie_cnt = cmd->cmd_pkt->pkt_numcookies;
14124 		ASSERT(fpkt->pkt_data_cookie_cnt <=
14125 		    pptr->port_data_dma_attr.dma_attr_sgllen);
14126 
14127 		cmd->cmd_dmacount = cmd->cmd_pkt->pkt_dma_len;
14128 
14129 		/* FCA needs pkt_datalen to be set */
14130 		fpkt->pkt_datalen = cmd->cmd_dmacount;
14131 		fcmd->fcp_data_len = cmd->cmd_dmacount;
14132 	} else {
14133 		fcmd->fcp_cntl.cntl_read_data = 0;
14134 		fcmd->fcp_cntl.cntl_write_data = 0;
14135 		fpkt->pkt_tran_type = FC_PKT_EXCHANGE;
14136 		fpkt->pkt_datalen = 0;
14137 		fcmd->fcp_data_len = 0;
14138 	}
14139 
14140 	/* set up the Tagged Queuing type */
14141 	if (cmd->cmd_pkt->pkt_flags & FLAG_HTAG) {
14142 		fcmd->fcp_cntl.cntl_qtype = FCP_QTYPE_HEAD_OF_Q;
14143 	} else if (cmd->cmd_pkt->pkt_flags & FLAG_OTAG) {
14144 		fcmd->fcp_cntl.cntl_qtype = FCP_QTYPE_ORDERED;
14145 	} else if (cmd->cmd_pkt->pkt_flags & FLAG_STAG) {
14146 		fcmd->fcp_cntl.cntl_qtype = FCP_QTYPE_SIMPLE;
14147 	} else {
14148 		fcmd->fcp_cntl.cntl_qtype = FCP_QTYPE_UNTAGGED;
14149 	}
14150 
14151 	fcmd->fcp_ent_addr = plun->lun_addr;
14152 
14153 	if (pptr->port_fcp_dma != FC_NO_DVMA_SPACE) {
14154 		FCP_CP_OUT((uint8_t *)fcmd, fpkt->pkt_cmd,
14155 		    fpkt->pkt_cmd_acc, sizeof (struct fcp_cmd));
14156 	} else {
14157 		ASSERT(fpkt->pkt_cmd_dma == NULL && fpkt->pkt_resp_dma == NULL);
14158 	}
14159 
14160 	cmd->cmd_pkt->pkt_reason = CMD_CMPLT;
14161 	cmd->cmd_pkt->pkt_state = 0;
14162 	cmd->cmd_pkt->pkt_statistics = 0;
14163 	cmd->cmd_pkt->pkt_resid = 0;
14164 
14165 	cmd->cmd_fp_pkt->pkt_data_dma = cmd->cmd_pkt->pkt_handle;
14166 
14167 	if (cmd->cmd_pkt->pkt_flags & FLAG_NOINTR) {
14168 		fpkt->pkt_tran_flags = (FC_TRAN_CLASS3 | FC_TRAN_NO_INTR);
14169 		fpkt->pkt_comp = NULL;
14170 	} else {
14171 		fpkt->pkt_tran_flags = (FC_TRAN_CLASS3 | FC_TRAN_INTR);
14172 		if (cmd->cmd_pkt->pkt_flags & FLAG_IMMEDIATE_CB) {
14173 			fpkt->pkt_tran_flags |= FC_TRAN_IMMEDIATE_CB;
14174 		}
14175 		fpkt->pkt_comp = fcp_cmd_callback;
14176 	}
14177 
14178 	mutex_enter(&pptr->port_mutex);
14179 	if (pptr->port_state & FCP_STATE_SUSPENDED) {
14180 		fpkt->pkt_tran_flags |= FC_TRAN_DUMPING;
14181 	}
14182 	mutex_exit(&pptr->port_mutex);
14183 
14184 	fpkt->pkt_cmd_fhdr.d_id = ptgt->tgt_d_id;
14185 	fpkt->pkt_cmd_fhdr.s_id = pptr->port_id;
14186 
14187 	/*
14188 	 * Save a few kernel cycles here
14189 	 */
14190 #ifndef	__lock_lint
14191 	fpkt->pkt_fca_device = ptgt->tgt_fca_dev;
14192 #endif /* __lock_lint */
14193 }
14194 
14195 static void
14196 fcp_post_callback(struct fcp_pkt *cmd)
14197 {
14198 	scsi_hba_pkt_comp(cmd->cmd_pkt);
14199 }
14200 
14201 
14202 /*
14203  * called to do polled I/O by fcp_start()
14204  *
14205  * return a transport status value, i.e. TRAN_ACCECPT for success
14206  */
14207 static int
14208 fcp_dopoll(struct fcp_port *pptr, struct fcp_pkt *cmd)
14209 {
14210 	int	rval;
14211 
14212 #ifdef	DEBUG
14213 	mutex_enter(&pptr->port_pkt_mutex);
14214 	pptr->port_npkts++;
14215 	mutex_exit(&pptr->port_pkt_mutex);
14216 #endif /* DEBUG */
14217 
14218 	if (cmd->cmd_fp_pkt->pkt_timeout) {
14219 		cmd->cmd_fp_pkt->pkt_timeout = cmd->cmd_pkt->pkt_time;
14220 	} else {
14221 		cmd->cmd_fp_pkt->pkt_timeout = FCP_POLL_TIMEOUT;
14222 	}
14223 
14224 	ASSERT(cmd->cmd_fp_pkt->pkt_comp == NULL);
14225 
14226 	cmd->cmd_state = FCP_PKT_ISSUED;
14227 
14228 	rval = fc_ulp_transport(pptr->port_fp_handle, cmd->cmd_fp_pkt);
14229 
14230 #ifdef	DEBUG
14231 	mutex_enter(&pptr->port_pkt_mutex);
14232 	pptr->port_npkts--;
14233 	mutex_exit(&pptr->port_pkt_mutex);
14234 #endif /* DEBUG */
14235 
14236 	cmd->cmd_state = FCP_PKT_IDLE;
14237 
14238 	switch (rval) {
14239 	case FC_SUCCESS:
14240 		if (cmd->cmd_fp_pkt->pkt_state == FC_PKT_SUCCESS) {
14241 			fcp_complete_pkt(cmd->cmd_fp_pkt);
14242 			rval = TRAN_ACCEPT;
14243 		} else {
14244 			rval = TRAN_FATAL_ERROR;
14245 		}
14246 		break;
14247 
14248 	case FC_TRAN_BUSY:
14249 		rval = TRAN_BUSY;
14250 		cmd->cmd_pkt->pkt_resid = 0;
14251 		break;
14252 
14253 	case FC_BADPACKET:
14254 		rval = TRAN_BADPKT;
14255 		break;
14256 
14257 	default:
14258 		rval = TRAN_FATAL_ERROR;
14259 		break;
14260 	}
14261 
14262 	return (rval);
14263 }
14264 
14265 
14266 /*
14267  * called by some of the following transport-called routines to convert
14268  * a supplied dip ptr to a port struct ptr (i.e. to the soft state)
14269  */
14270 static struct fcp_port *
14271 fcp_dip2port(dev_info_t *dip)
14272 {
14273 	int	instance;
14274 
14275 	instance = ddi_get_instance(dip);
14276 	return (ddi_get_soft_state(fcp_softstate, instance));
14277 }
14278 
14279 
14280 /*
14281  * called internally to return a LUN given a dip
14282  */
14283 struct fcp_lun *
14284 fcp_get_lun_from_cip(struct fcp_port *pptr, child_info_t *cip)
14285 {
14286 	struct fcp_tgt *ptgt;
14287 	struct fcp_lun *plun;
14288 	int i;
14289 
14290 
14291 	ASSERT(mutex_owned(&pptr->port_mutex));
14292 
14293 	for (i = 0; i < FCP_NUM_HASH; i++) {
14294 		for (ptgt = pptr->port_tgt_hash_table[i];
14295 		    ptgt != NULL;
14296 		    ptgt = ptgt->tgt_next) {
14297 			mutex_enter(&ptgt->tgt_mutex);
14298 			for (plun = ptgt->tgt_lun; plun != NULL;
14299 			    plun = plun->lun_next) {
14300 				mutex_enter(&plun->lun_mutex);
14301 				if (plun->lun_cip == cip) {
14302 					mutex_exit(&plun->lun_mutex);
14303 					mutex_exit(&ptgt->tgt_mutex);
14304 					return (plun); /* match found */
14305 				}
14306 				mutex_exit(&plun->lun_mutex);
14307 			}
14308 			mutex_exit(&ptgt->tgt_mutex);
14309 		}
14310 	}
14311 	return (NULL);				/* no LUN found */
14312 }
14313 
14314 /*
14315  * pass an element to the hotplug list, kick the hotplug thread
14316  * and wait for the element to get processed by the hotplug thread.
14317  * on return the element is freed.
14318  *
14319  * return zero success and non-zero on failure
14320  *
14321  * acquires/releases the target mutex
14322  *
14323  */
14324 static int
14325 fcp_pass_to_hp_and_wait(struct fcp_port *pptr, struct fcp_lun *plun,
14326     child_info_t *cip, int what, int link_cnt, int tgt_cnt, int flags)
14327 {
14328 	struct fcp_hp_elem	*elem;
14329 	int			rval;
14330 
14331 	mutex_enter(&plun->lun_tgt->tgt_mutex);
14332 	if ((elem = fcp_pass_to_hp(pptr, plun, cip,
14333 	    what, link_cnt, tgt_cnt, flags, 1)) == NULL) {
14334 		mutex_exit(&plun->lun_tgt->tgt_mutex);
14335 		fcp_log(CE_CONT, pptr->port_dip,
14336 		    "Can not pass_to_hp: what: %d; D_ID=%x, LUN=%x\n",
14337 		    what, plun->lun_tgt->tgt_d_id, plun->lun_num);
14338 		return (NDI_FAILURE);
14339 	}
14340 	mutex_exit(&plun->lun_tgt->tgt_mutex);
14341 	mutex_enter(&elem->mutex);
14342 	if (elem->wait) {
14343 		while (elem->wait) {
14344 			cv_wait(&elem->cv, &elem->mutex);
14345 		}
14346 	}
14347 	rval = (elem->result);
14348 	mutex_exit(&elem->mutex);
14349 	mutex_destroy(&elem->mutex);
14350 	cv_destroy(&elem->cv);
14351 	kmem_free(elem, sizeof (struct fcp_hp_elem));
14352 	return (rval);
14353 }
14354 
14355 /*
14356  * pass an element to the hotplug list, and then
14357  * kick the hotplug thread
14358  *
14359  * return Boolean success, i.e. non-zero if all goes well, else zero on error
14360  *
14361  * acquires/releases the hotplug mutex
14362  *
14363  * called with the target mutex owned
14364  *
14365  * memory acquired in NOSLEEP mode
14366  * NOTE: if wait is set to 1 then the caller is responsible for waiting on
14367  *	 for the hp daemon to process the request and is responsible for
14368  *	 freeing the element
14369  */
14370 static struct fcp_hp_elem *
14371 fcp_pass_to_hp(struct fcp_port *pptr, struct fcp_lun *plun,
14372     child_info_t *cip, int what, int link_cnt, int tgt_cnt, int flags, int wait)
14373 {
14374 	struct fcp_hp_elem	*elem;
14375 	dev_info_t *pdip;
14376 
14377 	ASSERT(pptr != NULL);
14378 	ASSERT(plun != NULL);
14379 	ASSERT(plun->lun_tgt != NULL);
14380 	ASSERT(mutex_owned(&plun->lun_tgt->tgt_mutex));
14381 
14382 	/* create space for a hotplug element */
14383 	if ((elem = kmem_zalloc(sizeof (struct fcp_hp_elem), KM_NOSLEEP))
14384 	    == NULL) {
14385 		fcp_log(CE_WARN, NULL,
14386 		    "!can't allocate memory for hotplug element");
14387 		return (NULL);
14388 	}
14389 
14390 	/* fill in hotplug element */
14391 	elem->port = pptr;
14392 	elem->lun = plun;
14393 	elem->cip = cip;
14394 	elem->old_lun_mpxio = plun->lun_mpxio;
14395 	elem->what = what;
14396 	elem->flags = flags;
14397 	elem->link_cnt = link_cnt;
14398 	elem->tgt_cnt = tgt_cnt;
14399 	elem->wait = wait;
14400 	mutex_init(&elem->mutex, NULL, MUTEX_DRIVER, NULL);
14401 	cv_init(&elem->cv, NULL, CV_DRIVER, NULL);
14402 
14403 	/* schedule the hotplug task */
14404 	pdip = pptr->port_dip;
14405 	mutex_enter(&plun->lun_mutex);
14406 	if (elem->what == FCP_ONLINE || elem->what == FCP_OFFLINE) {
14407 		plun->lun_event_count++;
14408 		elem->event_cnt = plun->lun_event_count;
14409 	}
14410 	mutex_exit(&plun->lun_mutex);
14411 	if (taskq_dispatch(DEVI(pdip)->devi_taskq, fcp_hp_task,
14412 	    (void *)elem, KM_NOSLEEP) == NULL) {
14413 		mutex_enter(&plun->lun_mutex);
14414 		if (elem->what == FCP_ONLINE || elem->what == FCP_OFFLINE) {
14415 			plun->lun_event_count--;
14416 		}
14417 		mutex_exit(&plun->lun_mutex);
14418 		kmem_free(elem, sizeof (*elem));
14419 		return (0);
14420 	}
14421 
14422 	return (elem);
14423 }
14424 
14425 
14426 static void
14427 fcp_retransport_cmd(struct fcp_port *pptr, struct fcp_pkt *cmd)
14428 {
14429 	int			rval;
14430 	struct scsi_address	*ap;
14431 	struct fcp_lun	*plun;
14432 	struct fcp_tgt	*ptgt;
14433 	fc_packet_t	*fpkt;
14434 
14435 	ap = &cmd->cmd_pkt->pkt_address;
14436 	plun = ADDR2LUN(ap);
14437 	ptgt = plun->lun_tgt;
14438 
14439 	ASSERT(cmd->cmd_flags & CFLAG_IN_QUEUE);
14440 
14441 	cmd->cmd_state = FCP_PKT_IDLE;
14442 
14443 	mutex_enter(&pptr->port_mutex);
14444 	mutex_enter(&ptgt->tgt_mutex);
14445 	if (((plun->lun_state & (FCP_LUN_BUSY | FCP_LUN_OFFLINE)) == 0) &&
14446 	    (!(pptr->port_state & FCP_STATE_ONLINING))) {
14447 		fc_ulp_rscn_info_t *rscnp;
14448 
14449 		cmd->cmd_state = FCP_PKT_ISSUED;
14450 
14451 		/*
14452 		 * It is possible for pkt_pd to be NULL if tgt_pd_handle was
14453 		 * originally NULL, hence we try to set it to the pd pointed
14454 		 * to by the SCSI device we're trying to get to.
14455 		 */
14456 
14457 		fpkt = cmd->cmd_fp_pkt;
14458 		if ((fpkt->pkt_pd == NULL) && (ptgt->tgt_pd_handle != NULL)) {
14459 			fpkt->pkt_pd = ptgt->tgt_pd_handle;
14460 			/*
14461 			 * We need to notify the transport that we now have a
14462 			 * reference to the remote port handle.
14463 			 */
14464 			fc_ulp_hold_remote_port(ptgt->tgt_pd_handle);
14465 		}
14466 
14467 		mutex_exit(&ptgt->tgt_mutex);
14468 		mutex_exit(&pptr->port_mutex);
14469 
14470 		ASSERT((cmd->cmd_pkt->pkt_flags & FLAG_NOINTR) == 0);
14471 
14472 		/* prepare the packet */
14473 
14474 		fcp_prepare_pkt(pptr, cmd, plun);
14475 
14476 		rscnp = (fc_ulp_rscn_info_t *)cmd->cmd_fp_pkt->
14477 		    pkt_ulp_rscn_infop;
14478 
14479 		cmd->cmd_timeout = cmd->cmd_pkt->pkt_time ?
14480 		    fcp_watchdog_time + cmd->cmd_pkt->pkt_time : 0;
14481 
14482 		if (rscnp != NULL) {
14483 			rscnp->ulp_rscn_count =
14484 			    fc_ulp_get_rscn_count(pptr->
14485 			    port_fp_handle);
14486 		}
14487 
14488 		rval = fcp_transport(pptr->port_fp_handle,
14489 		    cmd->cmd_fp_pkt, 0);
14490 
14491 		if (rval == FC_SUCCESS) {
14492 			return;
14493 		}
14494 		cmd->cmd_state &= ~FCP_PKT_ISSUED;
14495 	} else {
14496 		mutex_exit(&ptgt->tgt_mutex);
14497 		mutex_exit(&pptr->port_mutex);
14498 	}
14499 
14500 	fcp_queue_pkt(pptr, cmd);
14501 }
14502 
14503 
14504 static void
14505 fcp_fail_cmd(struct fcp_pkt *cmd, uchar_t reason, uint_t statistics)
14506 {
14507 	ASSERT(cmd->cmd_flags & CFLAG_IN_QUEUE);
14508 
14509 	cmd->cmd_flags &= ~CFLAG_IN_QUEUE;
14510 	cmd->cmd_state = FCP_PKT_IDLE;
14511 
14512 	cmd->cmd_pkt->pkt_reason = reason;
14513 	cmd->cmd_pkt->pkt_state = 0;
14514 	cmd->cmd_pkt->pkt_statistics = statistics;
14515 
14516 	fcp_post_callback(cmd);
14517 }
14518 
14519 /*
14520  *     Function: fcp_queue_pkt
14521  *
14522  *  Description: This function queues the packet passed by the caller into
14523  *		 the list of packets of the FCP port.
14524  *
14525  *     Argument: *pptr		FCP port.
14526  *		 *cmd		FCP packet to queue.
14527  *
14528  * Return Value: None
14529  *
14530  *	Context: User, Kernel and Interrupt context.
14531  */
14532 static void
14533 fcp_queue_pkt(struct fcp_port *pptr, struct fcp_pkt *cmd)
14534 {
14535 	ASSERT((cmd->cmd_pkt->pkt_flags & FLAG_NOQUEUE) == NULL);
14536 
14537 	mutex_enter(&pptr->port_pkt_mutex);
14538 	cmd->cmd_flags |= CFLAG_IN_QUEUE;
14539 	ASSERT(cmd->cmd_state != FCP_PKT_ISSUED);
14540 	cmd->cmd_timeout = fcp_watchdog_time + FCP_QUEUE_DELAY;
14541 
14542 	/*
14543 	 * zero pkt_time means hang around for ever
14544 	 */
14545 	if (cmd->cmd_pkt->pkt_time) {
14546 		if (cmd->cmd_fp_pkt->pkt_timeout > FCP_QUEUE_DELAY) {
14547 			cmd->cmd_fp_pkt->pkt_timeout -= FCP_QUEUE_DELAY;
14548 		} else {
14549 			/*
14550 			 * Indicate the watch thread to fail the
14551 			 * command by setting it to highest value
14552 			 */
14553 			cmd->cmd_timeout = fcp_watchdog_time;
14554 			cmd->cmd_fp_pkt->pkt_timeout = FCP_INVALID_TIMEOUT;
14555 		}
14556 	}
14557 
14558 	if (pptr->port_pkt_head) {
14559 		ASSERT(pptr->port_pkt_tail != NULL);
14560 
14561 		pptr->port_pkt_tail->cmd_next = cmd;
14562 		pptr->port_pkt_tail = cmd;
14563 	} else {
14564 		ASSERT(pptr->port_pkt_tail == NULL);
14565 
14566 		pptr->port_pkt_head = pptr->port_pkt_tail = cmd;
14567 	}
14568 	cmd->cmd_next = NULL;
14569 	mutex_exit(&pptr->port_pkt_mutex);
14570 }
14571 
14572 /*
14573  *     Function: fcp_update_targets
14574  *
14575  *  Description: This function applies the specified change of state to all
14576  *		 the targets listed.  The operation applied is 'set'.
14577  *
14578  *     Argument: *pptr		FCP port.
14579  *		 *dev_list	Array of fc_portmap_t structures.
14580  *		 count		Length of dev_list.
14581  *		 state		State bits to update.
14582  *		 cause		Reason for the update.
14583  *
14584  * Return Value: None
14585  *
14586  *	Context: User, Kernel and Interrupt context.
14587  *		 The mutex pptr->port_mutex must be held.
14588  */
14589 static void
14590 fcp_update_targets(struct fcp_port *pptr, fc_portmap_t *dev_list,
14591     uint32_t count, uint32_t state, int cause)
14592 {
14593 	fc_portmap_t		*map_entry;
14594 	struct fcp_tgt	*ptgt;
14595 
14596 	ASSERT(MUTEX_HELD(&pptr->port_mutex));
14597 
14598 	while (count--) {
14599 		map_entry = &(dev_list[count]);
14600 		ptgt = fcp_lookup_target(pptr,
14601 		    (uchar_t *)&(map_entry->map_pwwn));
14602 		if (ptgt == NULL) {
14603 			continue;
14604 		}
14605 
14606 		mutex_enter(&ptgt->tgt_mutex);
14607 		ptgt->tgt_trace = 0;
14608 		ptgt->tgt_change_cnt++;
14609 		ptgt->tgt_statec_cause = cause;
14610 		ptgt->tgt_tmp_cnt = 1;
14611 		fcp_update_tgt_state(ptgt, FCP_SET, state);
14612 		mutex_exit(&ptgt->tgt_mutex);
14613 	}
14614 }
14615 
14616 static int
14617 fcp_call_finish_init(struct fcp_port *pptr, struct fcp_tgt *ptgt,
14618     int lcount, int tcount, int cause)
14619 {
14620 	int rval;
14621 
14622 	mutex_enter(&pptr->port_mutex);
14623 	rval = fcp_call_finish_init_held(pptr, ptgt, lcount, tcount, cause);
14624 	mutex_exit(&pptr->port_mutex);
14625 
14626 	return (rval);
14627 }
14628 
14629 
14630 static int
14631 fcp_call_finish_init_held(struct fcp_port *pptr, struct fcp_tgt *ptgt,
14632     int lcount, int tcount, int cause)
14633 {
14634 	int	finish_init = 0;
14635 	int	finish_tgt = 0;
14636 	int	do_finish_init = 0;
14637 	int	rval = FCP_NO_CHANGE;
14638 
14639 	if (cause == FCP_CAUSE_LINK_CHANGE ||
14640 	    cause == FCP_CAUSE_LINK_DOWN) {
14641 		do_finish_init = 1;
14642 	}
14643 
14644 	if (ptgt != NULL) {
14645 		FCP_TRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
14646 		    FCP_BUF_LEVEL_2, 0,
14647 		    "link_cnt: %d,%d; tgt_cnt: %d,%d; tmp_cnt: %d,%d;"
14648 		    " cause = %d, d_id = 0x%x, tgt_done = %d",
14649 		    pptr->port_link_cnt, lcount, ptgt->tgt_change_cnt, tcount,
14650 		    pptr->port_tmp_cnt, ptgt->tgt_tmp_cnt, cause,
14651 		    ptgt->tgt_d_id, ptgt->tgt_done);
14652 
14653 		mutex_enter(&ptgt->tgt_mutex);
14654 
14655 		if (tcount && (ptgt->tgt_change_cnt != tcount)) {
14656 			rval = FCP_DEV_CHANGE;
14657 			if (do_finish_init && ptgt->tgt_done == 0) {
14658 				ptgt->tgt_done++;
14659 				finish_init = 1;
14660 			}
14661 		} else {
14662 			if (--ptgt->tgt_tmp_cnt <= 0) {
14663 				ptgt->tgt_tmp_cnt = 0;
14664 				finish_tgt = 1;
14665 
14666 				if (do_finish_init) {
14667 					finish_init = 1;
14668 				}
14669 			}
14670 		}
14671 		mutex_exit(&ptgt->tgt_mutex);
14672 	} else {
14673 		FCP_TRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
14674 		    FCP_BUF_LEVEL_2, 0,
14675 		    "Call Finish Init for NO target");
14676 
14677 		if (do_finish_init) {
14678 			finish_init = 1;
14679 		}
14680 	}
14681 
14682 	if (finish_tgt) {
14683 		ASSERT(ptgt != NULL);
14684 
14685 		mutex_enter(&ptgt->tgt_mutex);
14686 #ifdef	DEBUG
14687 		bzero(ptgt->tgt_tmp_cnt_stack,
14688 		    sizeof (ptgt->tgt_tmp_cnt_stack));
14689 
14690 		ptgt->tgt_tmp_cnt_depth = getpcstack(ptgt->tgt_tmp_cnt_stack,
14691 		    FCP_STACK_DEPTH);
14692 #endif /* DEBUG */
14693 		mutex_exit(&ptgt->tgt_mutex);
14694 
14695 		(void) fcp_finish_tgt(pptr, ptgt, lcount, tcount, cause);
14696 	}
14697 
14698 	if (finish_init && lcount == pptr->port_link_cnt) {
14699 		ASSERT(pptr->port_tmp_cnt > 0);
14700 		if (--pptr->port_tmp_cnt == 0) {
14701 			fcp_finish_init(pptr);
14702 		}
14703 	} else if (lcount != pptr->port_link_cnt) {
14704 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
14705 		    fcp_trace, FCP_BUF_LEVEL_2, 0,
14706 		    "fcp_call_finish_init_held,1: state change occured"
14707 		    " for D_ID=0x%x", (ptgt) ? ptgt->tgt_d_id : 0);
14708 	}
14709 
14710 	return (rval);
14711 }
14712 
14713 static void
14714 fcp_reconfigure_luns(void * tgt_handle)
14715 {
14716 	uint32_t		dev_cnt;
14717 	fc_portmap_t		*devlist;
14718 	struct fcp_tgt	*ptgt = (struct fcp_tgt *)tgt_handle;
14719 	struct fcp_port		*pptr = ptgt->tgt_port;
14720 
14721 	/*
14722 	 * If the timer that fires this off got canceled too late, the
14723 	 * target could have been destroyed.
14724 	 */
14725 
14726 	if (ptgt->tgt_tid == NULL) {
14727 		return;
14728 	}
14729 
14730 	devlist = kmem_zalloc(sizeof (*devlist), KM_NOSLEEP);
14731 	if (devlist == NULL) {
14732 		fcp_log(CE_WARN, pptr->port_dip,
14733 		    "!fcp%d: failed to allocate for portmap",
14734 		    pptr->port_instance);
14735 		return;
14736 	}
14737 
14738 	dev_cnt = 1;
14739 	devlist->map_pd = ptgt->tgt_pd_handle;
14740 	devlist->map_hard_addr.hard_addr = ptgt->tgt_hard_addr;
14741 	devlist->map_did.port_id = ptgt->tgt_d_id;
14742 
14743 	bcopy(&ptgt->tgt_node_wwn.raw_wwn[0], &devlist->map_nwwn, FC_WWN_SIZE);
14744 	bcopy(&ptgt->tgt_port_wwn.raw_wwn[0], &devlist->map_pwwn, FC_WWN_SIZE);
14745 
14746 	devlist->map_state = PORT_DEVICE_LOGGED_IN;
14747 	devlist->map_type = PORT_DEVICE_REPORTLUN_CHANGED;
14748 	devlist->map_flags = 0;
14749 
14750 	fcp_statec_callback(NULL, pptr->port_fp_handle, FC_STATE_DEVICE_CHANGE,
14751 	    pptr->port_topology, devlist, dev_cnt, pptr->port_id);
14752 
14753 	/*
14754 	 * Clear the tgt_tid after no more references to
14755 	 * the fcp_tgt
14756 	 */
14757 	mutex_enter(&ptgt->tgt_mutex);
14758 	ptgt->tgt_tid = NULL;
14759 	mutex_exit(&ptgt->tgt_mutex);
14760 
14761 	kmem_free(devlist, sizeof (*devlist));
14762 }
14763 
14764 
14765 static void
14766 fcp_free_targets(struct fcp_port *pptr)
14767 {
14768 	int			i;
14769 	struct fcp_tgt	*ptgt;
14770 
14771 	mutex_enter(&pptr->port_mutex);
14772 	for (i = 0; i < FCP_NUM_HASH; i++) {
14773 		ptgt = pptr->port_tgt_hash_table[i];
14774 		while (ptgt != NULL) {
14775 			struct fcp_tgt *next_tgt = ptgt->tgt_next;
14776 
14777 			fcp_free_target(ptgt);
14778 			ptgt = next_tgt;
14779 		}
14780 	}
14781 	mutex_exit(&pptr->port_mutex);
14782 }
14783 
14784 
14785 static void
14786 fcp_free_target(struct fcp_tgt *ptgt)
14787 {
14788 	struct fcp_lun	*plun;
14789 	timeout_id_t		tid;
14790 
14791 	mutex_enter(&ptgt->tgt_mutex);
14792 	tid = ptgt->tgt_tid;
14793 
14794 	/*
14795 	 * Cancel any pending timeouts for this target.
14796 	 */
14797 
14798 	if (tid != NULL) {
14799 		/*
14800 		 * Set tgt_tid to NULL first to avoid a race in the callback.
14801 		 * If tgt_tid is NULL, the callback will simply return.
14802 		 */
14803 		ptgt->tgt_tid = NULL;
14804 		mutex_exit(&ptgt->tgt_mutex);
14805 		(void) untimeout(tid);
14806 		mutex_enter(&ptgt->tgt_mutex);
14807 	}
14808 
14809 	plun = ptgt->tgt_lun;
14810 	while (plun != NULL) {
14811 		struct fcp_lun *next_lun = plun->lun_next;
14812 
14813 		fcp_dealloc_lun(plun);
14814 		plun = next_lun;
14815 	}
14816 
14817 	mutex_exit(&ptgt->tgt_mutex);
14818 	fcp_dealloc_tgt(ptgt);
14819 }
14820 
14821 /*
14822  *     Function: fcp_is_retryable
14823  *
14824  *  Description: Indicates if the internal packet is retryable.
14825  *
14826  *     Argument: *icmd		FCP internal packet.
14827  *
14828  * Return Value: 0	Not retryable
14829  *		 1	Retryable
14830  *
14831  *	Context: User, Kernel and Interrupt context
14832  */
14833 static int
14834 fcp_is_retryable(struct fcp_ipkt *icmd)
14835 {
14836 	if (icmd->ipkt_port->port_state & (FCP_STATE_SUSPENDED |
14837 	    FCP_STATE_DETACHING | FCP_STATE_POWER_DOWN)) {
14838 		return (0);
14839 	}
14840 
14841 	return (((fcp_watchdog_time + icmd->ipkt_fpkt->pkt_timeout) <
14842 	    icmd->ipkt_port->port_deadline) ? 1 : 0);
14843 }
14844 
14845 /*
14846  *     Function: fcp_create_on_demand
14847  *
14848  *     Argument: *pptr		FCP port.
14849  *		 *pwwn		Port WWN.
14850  *
14851  * Return Value: 0	Success
14852  *		 EIO
14853  *		 ENOMEM
14854  *		 EBUSY
14855  *		 EINVAL
14856  *
14857  *	Context: User and Kernel context
14858  */
14859 static int
14860 fcp_create_on_demand(struct fcp_port *pptr, uchar_t *pwwn)
14861 {
14862 	int			wait_ms;
14863 	int			tcount;
14864 	int			lcount;
14865 	int			ret;
14866 	int			error;
14867 	int			rval = EIO;
14868 	int			ntries;
14869 	fc_portmap_t		*devlist;
14870 	opaque_t		pd;
14871 	struct fcp_lun		*plun;
14872 	struct fcp_tgt		*ptgt;
14873 	int			old_manual = 0;
14874 
14875 	/* Allocates the fc_portmap_t structure. */
14876 	devlist = kmem_zalloc(sizeof (*devlist), KM_SLEEP);
14877 
14878 	/*
14879 	 * If FC_INVALID_RSCN_COUNT is non-zero, we will have to init as shown
14880 	 * in the commented statement below:
14881 	 *
14882 	 * devlist->map_rscn_info.ulp_rscn_count = FC_INVALID_RSCN_COUNT;
14883 	 *
14884 	 * Below, the deadline for the discovery process is set.
14885 	 */
14886 	mutex_enter(&pptr->port_mutex);
14887 	pptr->port_deadline = fcp_watchdog_time + FCP_ICMD_DEADLINE;
14888 	mutex_exit(&pptr->port_mutex);
14889 
14890 	/*
14891 	 * We try to find the remote port based on the WWN provided by the
14892 	 * caller.  We actually ask fp/fctl if it has it.
14893 	 */
14894 	pd = fc_ulp_get_remote_port(pptr->port_fp_handle,
14895 	    (la_wwn_t *)pwwn, &error, 1);
14896 
14897 	if (pd == NULL) {
14898 		kmem_free(devlist, sizeof (*devlist));
14899 		return (rval);
14900 	}
14901 
14902 	/*
14903 	 * The remote port was found.  We ask fp/fctl to update our
14904 	 * fc_portmap_t structure.
14905 	 */
14906 	ret = fc_ulp_pwwn_to_portmap(pptr->port_fp_handle,
14907 	    (la_wwn_t *)pwwn, devlist);
14908 	if (ret != FC_SUCCESS) {
14909 		kmem_free(devlist, sizeof (*devlist));
14910 		return (rval);
14911 	}
14912 
14913 	/*
14914 	 * The map flag field is set to indicates that the creation is being
14915 	 * done at the user request (Ioclt probably luxadm or cfgadm).
14916 	 */
14917 	devlist->map_type = PORT_DEVICE_USER_CREATE;
14918 
14919 	mutex_enter(&pptr->port_mutex);
14920 
14921 	/*
14922 	 * We check to see if fcp already has a target that describes the
14923 	 * device being created.  If not it is created.
14924 	 */
14925 	ptgt = fcp_lookup_target(pptr, pwwn);
14926 	if (ptgt == NULL) {
14927 		lcount = pptr->port_link_cnt;
14928 		mutex_exit(&pptr->port_mutex);
14929 
14930 		ptgt = fcp_alloc_tgt(pptr, devlist, lcount);
14931 		if (ptgt == NULL) {
14932 			fcp_log(CE_WARN, pptr->port_dip,
14933 			    "!FC target allocation failed");
14934 			return (ENOMEM);
14935 		}
14936 
14937 		mutex_enter(&pptr->port_mutex);
14938 	}
14939 
14940 	mutex_enter(&ptgt->tgt_mutex);
14941 	ptgt->tgt_statec_cause = FCP_CAUSE_USER_CREATE;
14942 	ptgt->tgt_tmp_cnt = 1;
14943 	ptgt->tgt_device_created = 0;
14944 	/*
14945 	 * If fabric and auto config is set but the target was
14946 	 * manually unconfigured then reset to the manual_config_only to
14947 	 * 0 so the device will get configured.
14948 	 */
14949 	if (FC_TOP_EXTERNAL(pptr->port_topology) &&
14950 	    fcp_enable_auto_configuration &&
14951 	    ptgt->tgt_manual_config_only == 1) {
14952 		old_manual = 1;
14953 		ptgt->tgt_manual_config_only = 0;
14954 	}
14955 	mutex_exit(&ptgt->tgt_mutex);
14956 
14957 	fcp_update_targets(pptr, devlist, 1,
14958 	    FCP_LUN_BUSY | FCP_LUN_MARK, FCP_CAUSE_USER_CREATE);
14959 
14960 	lcount = pptr->port_link_cnt;
14961 	tcount = ptgt->tgt_change_cnt;
14962 
14963 	if (fcp_handle_mapflags(pptr, ptgt, devlist, lcount,
14964 	    tcount, FCP_CAUSE_USER_CREATE) == TRUE) {
14965 		if (FC_TOP_EXTERNAL(pptr->port_topology) &&
14966 		    fcp_enable_auto_configuration && old_manual) {
14967 			mutex_enter(&ptgt->tgt_mutex);
14968 			ptgt->tgt_manual_config_only = 1;
14969 			mutex_exit(&ptgt->tgt_mutex);
14970 		}
14971 
14972 		if (pptr->port_link_cnt != lcount ||
14973 		    ptgt->tgt_change_cnt != tcount) {
14974 			rval = EBUSY;
14975 		}
14976 		mutex_exit(&pptr->port_mutex);
14977 
14978 		FCP_TRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
14979 		    FCP_BUF_LEVEL_3, 0,
14980 		    "fcp_create_on_demand: mapflags ptgt=%x, "
14981 		    "lcount=%x::port_link_cnt=%x, "
14982 		    "tcount=%x: tgt_change_cnt=%x, rval=%x",
14983 		    ptgt, lcount, pptr->port_link_cnt,
14984 		    tcount, ptgt->tgt_change_cnt, rval);
14985 		return (rval);
14986 	}
14987 
14988 	/*
14989 	 * Due to lack of synchronization mechanisms, we perform
14990 	 * periodic monitoring of our request; Because requests
14991 	 * get dropped when another one supercedes (either because
14992 	 * of a link change or a target change), it is difficult to
14993 	 * provide a clean synchronization mechanism (such as a
14994 	 * semaphore or a conditional variable) without exhaustively
14995 	 * rewriting the mainline discovery code of this driver.
14996 	 */
14997 	wait_ms = 500;
14998 
14999 	ntries = fcp_max_target_retries;
15000 
15001 	FCP_TRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
15002 	    FCP_BUF_LEVEL_3, 0,
15003 	    "fcp_create_on_demand(1): ntries=%x, ptgt=%x, "
15004 	    "lcount=%x::port_link_cnt=%x, "
15005 	    "tcount=%x::tgt_change_cnt=%x, rval=%x, tgt_device_created=%x "
15006 	    "tgt_tmp_cnt =%x",
15007 	    ntries, ptgt, lcount, pptr->port_link_cnt,
15008 	    tcount, ptgt->tgt_change_cnt, rval, ptgt->tgt_device_created,
15009 	    ptgt->tgt_tmp_cnt);
15010 
15011 	mutex_enter(&ptgt->tgt_mutex);
15012 	while (ntries-- != 0 && pptr->port_link_cnt == lcount &&
15013 	    ptgt->tgt_change_cnt == tcount && ptgt->tgt_device_created == 0) {
15014 		mutex_exit(&ptgt->tgt_mutex);
15015 		mutex_exit(&pptr->port_mutex);
15016 
15017 		delay(drv_usectohz(wait_ms * 1000));
15018 
15019 		mutex_enter(&pptr->port_mutex);
15020 		mutex_enter(&ptgt->tgt_mutex);
15021 	}
15022 
15023 
15024 	if (pptr->port_link_cnt != lcount || ptgt->tgt_change_cnt != tcount) {
15025 		rval = EBUSY;
15026 	} else {
15027 		if (ptgt->tgt_tmp_cnt == 0 && ptgt->tgt_node_state ==
15028 		    FCP_TGT_NODE_PRESENT) {
15029 			rval = 0;
15030 		}
15031 	}
15032 
15033 	FCP_TRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
15034 	    FCP_BUF_LEVEL_3, 0,
15035 	    "fcp_create_on_demand(2): ntries=%x, ptgt=%x, "
15036 	    "lcount=%x::port_link_cnt=%x, "
15037 	    "tcount=%x::tgt_change_cnt=%x, rval=%x, tgt_device_created=%x "
15038 	    "tgt_tmp_cnt =%x",
15039 	    ntries, ptgt, lcount, pptr->port_link_cnt,
15040 	    tcount, ptgt->tgt_change_cnt, rval, ptgt->tgt_device_created,
15041 	    ptgt->tgt_tmp_cnt);
15042 
15043 	if (rval) {
15044 		if (FC_TOP_EXTERNAL(pptr->port_topology) &&
15045 		    fcp_enable_auto_configuration && old_manual) {
15046 			ptgt->tgt_manual_config_only = 1;
15047 		}
15048 		mutex_exit(&ptgt->tgt_mutex);
15049 		mutex_exit(&pptr->port_mutex);
15050 		kmem_free(devlist, sizeof (*devlist));
15051 
15052 		FCP_TRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
15053 		    FCP_BUF_LEVEL_3, 0,
15054 		    "fcp_create_on_demand(3): ntries=%x, ptgt=%x, "
15055 		    "lcount=%x::port_link_cnt=%x, "
15056 		    "tcount=%x::tgt_change_cnt=%x, rval=%x, "
15057 		    "tgt_device_created=%x, tgt D_ID=%x",
15058 		    ntries, ptgt, lcount, pptr->port_link_cnt,
15059 		    tcount, ptgt->tgt_change_cnt, rval,
15060 		    ptgt->tgt_device_created, ptgt->tgt_d_id);
15061 		return (rval);
15062 	}
15063 
15064 	if ((plun = ptgt->tgt_lun) != NULL) {
15065 		tcount = plun->lun_tgt->tgt_change_cnt;
15066 	} else {
15067 		rval = EINVAL;
15068 	}
15069 	lcount = pptr->port_link_cnt;
15070 
15071 	/*
15072 	 * Configuring the target with no LUNs will fail. We
15073 	 * should reset the node state so that it is not
15074 	 * automatically configured when the LUNs are added
15075 	 * to this target.
15076 	 */
15077 	if (ptgt->tgt_lun_cnt == 0) {
15078 		ptgt->tgt_node_state = FCP_TGT_NODE_NONE;
15079 	}
15080 	mutex_exit(&ptgt->tgt_mutex);
15081 	mutex_exit(&pptr->port_mutex);
15082 
15083 	while (plun) {
15084 		child_info_t	*cip;
15085 
15086 		mutex_enter(&plun->lun_mutex);
15087 		cip = plun->lun_cip;
15088 		mutex_exit(&plun->lun_mutex);
15089 
15090 		mutex_enter(&ptgt->tgt_mutex);
15091 		if (!(plun->lun_state & FCP_LUN_OFFLINE)) {
15092 			mutex_exit(&ptgt->tgt_mutex);
15093 
15094 			rval = fcp_pass_to_hp_and_wait(pptr, plun, cip,
15095 			    FCP_ONLINE, lcount, tcount,
15096 			    NDI_ONLINE_ATTACH);
15097 			if (rval != NDI_SUCCESS) {
15098 				FCP_TRACE(fcp_logq,
15099 				    pptr->port_instbuf, fcp_trace,
15100 				    FCP_BUF_LEVEL_3, 0,
15101 				    "fcp_create_on_demand: "
15102 				    "pass_to_hp_and_wait failed "
15103 				    "rval=%x", rval);
15104 				rval = EIO;
15105 			} else {
15106 				mutex_enter(&LUN_TGT->tgt_mutex);
15107 				plun->lun_state &= ~(FCP_LUN_OFFLINE |
15108 				    FCP_LUN_BUSY);
15109 				mutex_exit(&LUN_TGT->tgt_mutex);
15110 			}
15111 			mutex_enter(&ptgt->tgt_mutex);
15112 		}
15113 
15114 		plun = plun->lun_next;
15115 		mutex_exit(&ptgt->tgt_mutex);
15116 	}
15117 
15118 	kmem_free(devlist, sizeof (*devlist));
15119 
15120 	if (FC_TOP_EXTERNAL(pptr->port_topology) &&
15121 	    fcp_enable_auto_configuration && old_manual) {
15122 		mutex_enter(&ptgt->tgt_mutex);
15123 		/* if successful then set manual to 0 */
15124 		if (rval == 0) {
15125 			ptgt->tgt_manual_config_only = 0;
15126 		} else {
15127 			/* reset to 1 so the user has to do the config */
15128 			ptgt->tgt_manual_config_only = 1;
15129 		}
15130 		mutex_exit(&ptgt->tgt_mutex);
15131 	}
15132 
15133 	return (rval);
15134 }
15135 
15136 
15137 static void
15138 fcp_ascii_to_wwn(caddr_t string, uchar_t bytes[], unsigned int byte_len)
15139 {
15140 	int		count;
15141 	uchar_t		byte;
15142 
15143 	count = 0;
15144 	while (*string) {
15145 		byte = FCP_ATOB(*string); string++;
15146 		byte = byte << 4 | FCP_ATOB(*string); string++;
15147 		bytes[count++] = byte;
15148 
15149 		if (count >= byte_len) {
15150 			break;
15151 		}
15152 	}
15153 }
15154 
15155 static void
15156 fcp_wwn_to_ascii(uchar_t wwn[], char *string)
15157 {
15158 	int		i;
15159 
15160 	for (i = 0; i < FC_WWN_SIZE; i++) {
15161 		(void) sprintf(string + (i * 2),
15162 		    "%02x", wwn[i]);
15163 	}
15164 
15165 }
15166 
15167 static void
15168 fcp_print_error(fc_packet_t *fpkt)
15169 {
15170 	struct fcp_ipkt	*icmd = (struct fcp_ipkt *)
15171 	    fpkt->pkt_ulp_private;
15172 	struct fcp_port	*pptr;
15173 	struct fcp_tgt	*ptgt;
15174 	struct fcp_lun	*plun;
15175 	caddr_t			buf;
15176 	int			scsi_cmd = 0;
15177 
15178 	ptgt = icmd->ipkt_tgt;
15179 	plun = icmd->ipkt_lun;
15180 	pptr = ptgt->tgt_port;
15181 
15182 	buf = kmem_zalloc(256, KM_NOSLEEP);
15183 	if (buf == NULL) {
15184 		return;
15185 	}
15186 
15187 	switch (icmd->ipkt_opcode) {
15188 	case SCMD_REPORT_LUN:
15189 		(void) sprintf(buf, "!REPORT LUN to D_ID=0x%%x"
15190 		    " lun=0x%%x failed");
15191 		scsi_cmd++;
15192 		break;
15193 
15194 	case SCMD_INQUIRY_PAGE83:
15195 		(void) sprintf(buf, "!INQUIRY-83 to D_ID=0x%%x"
15196 		    " lun=0x%%x failed");
15197 		scsi_cmd++;
15198 		break;
15199 
15200 	case SCMD_INQUIRY:
15201 		(void) sprintf(buf, "!INQUIRY to D_ID=0x%%x"
15202 		    " lun=0x%%x failed");
15203 		scsi_cmd++;
15204 		break;
15205 
15206 	case LA_ELS_PLOGI:
15207 		(void) sprintf(buf, "!PLOGI to D_ID=0x%%x failed");
15208 		break;
15209 
15210 	case LA_ELS_PRLI:
15211 		(void) sprintf(buf, "!PRLI to D_ID=0x%%x failed");
15212 		break;
15213 	}
15214 
15215 	if (scsi_cmd && fpkt->pkt_state == FC_PKT_SUCCESS) {
15216 		struct fcp_rsp		response, *rsp;
15217 		uchar_t			asc, ascq;
15218 		caddr_t			sense_key = NULL;
15219 		struct fcp_rsp_info	fcp_rsp_err, *bep;
15220 
15221 		if (icmd->ipkt_nodma) {
15222 			rsp = (struct fcp_rsp *)fpkt->pkt_resp;
15223 			bep = (struct fcp_rsp_info *)((caddr_t)rsp +
15224 			    sizeof (struct fcp_rsp));
15225 		} else {
15226 			rsp = &response;
15227 			bep = &fcp_rsp_err;
15228 
15229 			FCP_CP_IN(fpkt->pkt_resp, rsp, fpkt->pkt_resp_acc,
15230 			    sizeof (struct fcp_rsp));
15231 
15232 			FCP_CP_IN(fpkt->pkt_resp + sizeof (struct fcp_rsp),
15233 			    bep, fpkt->pkt_resp_acc,
15234 			    sizeof (struct fcp_rsp_info));
15235 		}
15236 
15237 
15238 		if (fcp_validate_fcp_response(rsp, pptr) != FC_SUCCESS) {
15239 			(void) sprintf(buf + strlen(buf),
15240 			    " : Bad FCP response values rsvd1=%%x, rsvd2=%%x,"
15241 			    " sts-rsvd1=%%x, sts-rsvd2=%%x, rsplen=%%x,"
15242 			    " senselen=%%x. Giving up");
15243 
15244 			fcp_log(CE_WARN, pptr->port_dip, buf,
15245 			    ptgt->tgt_d_id, plun->lun_num, rsp->reserved_0,
15246 			    rsp->reserved_1, rsp->fcp_u.fcp_status.reserved_0,
15247 			    rsp->fcp_u.fcp_status.reserved_1,
15248 			    rsp->fcp_response_len, rsp->fcp_sense_len);
15249 
15250 			kmem_free(buf, 256);
15251 			return;
15252 		}
15253 
15254 		if (rsp->fcp_u.fcp_status.rsp_len_set &&
15255 		    bep->rsp_code != FCP_NO_FAILURE) {
15256 			(void) sprintf(buf + strlen(buf),
15257 			    " FCP Response code = 0x%x", bep->rsp_code);
15258 		}
15259 
15260 		if (rsp->fcp_u.fcp_status.scsi_status & STATUS_CHECK) {
15261 			struct scsi_extended_sense sense_info, *sense_ptr;
15262 
15263 			if (icmd->ipkt_nodma) {
15264 				sense_ptr = (struct scsi_extended_sense *)
15265 				    ((caddr_t)fpkt->pkt_resp +
15266 				    sizeof (struct fcp_rsp) +
15267 				    rsp->fcp_response_len);
15268 			} else {
15269 				sense_ptr = &sense_info;
15270 
15271 				FCP_CP_IN(fpkt->pkt_resp +
15272 				    sizeof (struct fcp_rsp) +
15273 				    rsp->fcp_response_len, &sense_info,
15274 				    fpkt->pkt_resp_acc,
15275 				    sizeof (struct scsi_extended_sense));
15276 			}
15277 
15278 			if (sense_ptr->es_key < NUM_SENSE_KEYS +
15279 			    NUM_IMPL_SENSE_KEYS) {
15280 				sense_key = sense_keys[sense_ptr->es_key];
15281 			} else {
15282 				sense_key = "Undefined";
15283 			}
15284 
15285 			asc = sense_ptr->es_add_code;
15286 			ascq = sense_ptr->es_qual_code;
15287 
15288 			(void) sprintf(buf + strlen(buf),
15289 			    ": sense key=%%s, ASC=%%x," " ASCQ=%%x."
15290 			    " Giving up");
15291 
15292 			fcp_log(CE_WARN, pptr->port_dip, buf,
15293 			    ptgt->tgt_d_id, plun->lun_num, sense_key,
15294 			    asc, ascq);
15295 		} else {
15296 			(void) sprintf(buf + strlen(buf),
15297 			    " : SCSI status=%%x. Giving up");
15298 
15299 			fcp_log(CE_WARN, pptr->port_dip, buf,
15300 			    ptgt->tgt_d_id, plun->lun_num,
15301 			    rsp->fcp_u.fcp_status.scsi_status);
15302 		}
15303 	} else {
15304 		caddr_t state, reason, action, expln;
15305 
15306 		(void) fc_ulp_pkt_error(fpkt, &state, &reason,
15307 		    &action, &expln);
15308 
15309 		(void) sprintf(buf + strlen(buf), ": State:%%s,"
15310 		    " Reason:%%s. Giving up");
15311 
15312 		if (scsi_cmd) {
15313 			fcp_log(CE_WARN, pptr->port_dip, buf,
15314 			    ptgt->tgt_d_id, plun->lun_num, state, reason);
15315 		} else {
15316 			fcp_log(CE_WARN, pptr->port_dip, buf,
15317 			    ptgt->tgt_d_id, state, reason);
15318 		}
15319 	}
15320 
15321 	kmem_free(buf, 256);
15322 }
15323 
15324 
15325 static int
15326 fcp_handle_ipkt_errors(struct fcp_port *pptr, struct fcp_tgt *ptgt,
15327     struct fcp_ipkt *icmd, int rval, caddr_t op)
15328 {
15329 	int	ret = DDI_FAILURE;
15330 	char	*error;
15331 
15332 	switch (rval) {
15333 	case FC_DEVICE_BUSY_NEW_RSCN:
15334 		/*
15335 		 * This means that there was a new RSCN that the transport
15336 		 * knows about (which the ULP *may* know about too) but the
15337 		 * pkt that was sent down was related to an older RSCN. So, we
15338 		 * are just going to reset the retry count and deadline and
15339 		 * continue to retry. The idea is that transport is currently
15340 		 * working on the new RSCN and will soon let the ULPs know
15341 		 * about it and when it does the existing logic will kick in
15342 		 * where it will change the tcount to indicate that something
15343 		 * changed on the target. So, rediscovery will start and there
15344 		 * will not be an infinite retry.
15345 		 *
15346 		 * For a full flow of how the RSCN info is transferred back and
15347 		 * forth, see fp.c
15348 		 */
15349 		icmd->ipkt_retries = 0;
15350 		icmd->ipkt_port->port_deadline = fcp_watchdog_time +
15351 		    FCP_ICMD_DEADLINE;
15352 
15353 		FCP_TRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
15354 		    FCP_BUF_LEVEL_3, 0,
15355 		    "fcp_handle_ipkt_errors: rval=%x  for D_ID=%x",
15356 		    rval, ptgt->tgt_d_id);
15357 		/* FALLTHROUGH */
15358 
15359 	case FC_STATEC_BUSY:
15360 	case FC_DEVICE_BUSY:
15361 	case FC_PBUSY:
15362 	case FC_FBUSY:
15363 	case FC_TRAN_BUSY:
15364 	case FC_OFFLINE:
15365 		FCP_TRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
15366 		    FCP_BUF_LEVEL_3, 0,
15367 		    "fcp_handle_ipkt_errors: rval=%x  for D_ID=%x",
15368 		    rval, ptgt->tgt_d_id);
15369 		if (icmd->ipkt_retries < FCP_MAX_RETRIES &&
15370 		    fcp_is_retryable(icmd)) {
15371 			fcp_queue_ipkt(pptr, icmd->ipkt_fpkt);
15372 			ret = DDI_SUCCESS;
15373 		}
15374 		break;
15375 
15376 	case FC_LOGINREQ:
15377 		/*
15378 		 * FC_LOGINREQ used to be handled just like all the cases
15379 		 * above. It has been changed to handled a PRLI that fails
15380 		 * with FC_LOGINREQ different than other ipkts that fail
15381 		 * with FC_LOGINREQ. If a PRLI fails with FC_LOGINREQ it is
15382 		 * a simple matter to turn it into a PLOGI instead, so that's
15383 		 * exactly what we do here.
15384 		 */
15385 		if (icmd->ipkt_opcode == LA_ELS_PRLI) {
15386 			ret = fcp_send_els(icmd->ipkt_port, icmd->ipkt_tgt,
15387 			    icmd, LA_ELS_PLOGI, icmd->ipkt_link_cnt,
15388 			    icmd->ipkt_change_cnt, icmd->ipkt_cause);
15389 		} else {
15390 			FCP_TRACE(fcp_logq, pptr->port_instbuf, fcp_trace,
15391 			    FCP_BUF_LEVEL_3, 0,
15392 			    "fcp_handle_ipkt_errors: rval=%x  for D_ID=%x",
15393 			    rval, ptgt->tgt_d_id);
15394 			if (icmd->ipkt_retries < FCP_MAX_RETRIES &&
15395 			    fcp_is_retryable(icmd)) {
15396 				fcp_queue_ipkt(pptr, icmd->ipkt_fpkt);
15397 				ret = DDI_SUCCESS;
15398 			}
15399 		}
15400 		break;
15401 
15402 	default:
15403 		mutex_enter(&pptr->port_mutex);
15404 		mutex_enter(&ptgt->tgt_mutex);
15405 		if (!FCP_STATE_CHANGED(pptr, ptgt, icmd)) {
15406 			mutex_exit(&ptgt->tgt_mutex);
15407 			mutex_exit(&pptr->port_mutex);
15408 
15409 			(void) fc_ulp_error(rval, &error);
15410 			fcp_log(CE_WARN, pptr->port_dip,
15411 			    "!Failed to send %s to D_ID=%x error=%s",
15412 			    op, ptgt->tgt_d_id, error);
15413 		} else {
15414 			FCP_TRACE(fcp_logq, pptr->port_instbuf,
15415 			    fcp_trace, FCP_BUF_LEVEL_2, 0,
15416 			    "fcp_handle_ipkt_errors,1: state change occured"
15417 			    " for D_ID=0x%x", ptgt->tgt_d_id);
15418 			mutex_exit(&ptgt->tgt_mutex);
15419 			mutex_exit(&pptr->port_mutex);
15420 		}
15421 		break;
15422 	}
15423 
15424 	return (ret);
15425 }
15426 
15427 
15428 /*
15429  * Check of outstanding commands on any LUN for this target
15430  */
15431 static int
15432 fcp_outstanding_lun_cmds(struct fcp_tgt *ptgt)
15433 {
15434 	struct	fcp_lun	*plun;
15435 	struct	fcp_pkt	*cmd;
15436 
15437 	for (plun = ptgt->tgt_lun; plun != NULL; plun = plun->lun_next) {
15438 		mutex_enter(&plun->lun_mutex);
15439 		for (cmd = plun->lun_pkt_head; cmd != NULL;
15440 		    cmd = cmd->cmd_forw) {
15441 			if (cmd->cmd_state == FCP_PKT_ISSUED) {
15442 				mutex_exit(&plun->lun_mutex);
15443 				return (FC_SUCCESS);
15444 			}
15445 		}
15446 		mutex_exit(&plun->lun_mutex);
15447 	}
15448 
15449 	return (FC_FAILURE);
15450 }
15451 
15452 static fc_portmap_t *
15453 fcp_construct_map(struct fcp_port *pptr, uint32_t *dev_cnt)
15454 {
15455 	int			i;
15456 	fc_portmap_t		*devlist;
15457 	fc_portmap_t		*devptr = NULL;
15458 	struct fcp_tgt	*ptgt;
15459 
15460 	mutex_enter(&pptr->port_mutex);
15461 	for (i = 0, *dev_cnt = 0; i < FCP_NUM_HASH; i++) {
15462 		for (ptgt = pptr->port_tgt_hash_table[i]; ptgt != NULL;
15463 		    ptgt = ptgt->tgt_next) {
15464 			if (!(ptgt->tgt_state & FCP_TGT_ORPHAN)) {
15465 				++*dev_cnt;
15466 			}
15467 		}
15468 	}
15469 
15470 	devptr = devlist = kmem_zalloc(sizeof (*devlist) * *dev_cnt,
15471 	    KM_NOSLEEP);
15472 	if (devlist == NULL) {
15473 		mutex_exit(&pptr->port_mutex);
15474 		fcp_log(CE_WARN, pptr->port_dip,
15475 		    "!fcp%d: failed to allocate for portmap for construct map",
15476 		    pptr->port_instance);
15477 		return (devptr);
15478 	}
15479 
15480 	for (i = 0; i < FCP_NUM_HASH; i++) {
15481 		for (ptgt = pptr->port_tgt_hash_table[i]; ptgt != NULL;
15482 		    ptgt = ptgt->tgt_next) {
15483 			if (!(ptgt->tgt_state & FCP_TGT_ORPHAN)) {
15484 				int ret;
15485 
15486 				ret = fc_ulp_pwwn_to_portmap(
15487 				    pptr->port_fp_handle,
15488 				    (la_wwn_t *)&ptgt->tgt_port_wwn.raw_wwn[0],
15489 				    devlist);
15490 
15491 				if (ret == FC_SUCCESS) {
15492 					devlist++;
15493 					continue;
15494 				}
15495 
15496 				devlist->map_pd = NULL;
15497 				devlist->map_did.port_id = ptgt->tgt_d_id;
15498 				devlist->map_hard_addr.hard_addr =
15499 				    ptgt->tgt_hard_addr;
15500 
15501 				devlist->map_state = PORT_DEVICE_INVALID;
15502 				devlist->map_type = PORT_DEVICE_OLD;
15503 
15504 				bcopy(&ptgt->tgt_node_wwn.raw_wwn[0],
15505 				    &devlist->map_nwwn, FC_WWN_SIZE);
15506 
15507 				bcopy(&ptgt->tgt_port_wwn.raw_wwn[0],
15508 				    &devlist->map_pwwn, FC_WWN_SIZE);
15509 
15510 				devlist++;
15511 			}
15512 		}
15513 	}
15514 
15515 	mutex_exit(&pptr->port_mutex);
15516 
15517 	return (devptr);
15518 }
15519 /*
15520  * Inimate MPxIO that the lun is busy and cannot accept regular IO
15521  */
15522 static void
15523 fcp_update_mpxio_path_verifybusy(struct fcp_port *pptr)
15524 {
15525 	int i;
15526 	struct fcp_tgt	*ptgt;
15527 	struct fcp_lun	*plun;
15528 
15529 	for (i = 0; i < FCP_NUM_HASH; i++) {
15530 		for (ptgt = pptr->port_tgt_hash_table[i]; ptgt != NULL;
15531 		    ptgt = ptgt->tgt_next) {
15532 			mutex_enter(&ptgt->tgt_mutex);
15533 			for (plun = ptgt->tgt_lun; plun != NULL;
15534 			    plun = plun->lun_next) {
15535 				if (plun->lun_mpxio &&
15536 				    plun->lun_state & FCP_LUN_BUSY) {
15537 					if (!fcp_pass_to_hp(pptr, plun,
15538 					    plun->lun_cip,
15539 					    FCP_MPXIO_PATH_SET_BUSY,
15540 					    pptr->port_link_cnt,
15541 					    ptgt->tgt_change_cnt, 0, 0)) {
15542 						FCP_TRACE(fcp_logq,
15543 						    pptr->port_instbuf,
15544 						    fcp_trace,
15545 						    FCP_BUF_LEVEL_2, 0,
15546 						    "path_verifybusy: "
15547 						    "disable lun %p failed!",
15548 						    plun);
15549 					}
15550 				}
15551 			}
15552 			mutex_exit(&ptgt->tgt_mutex);
15553 		}
15554 	}
15555 }
15556 
15557 static int
15558 fcp_update_mpxio_path(struct fcp_lun *plun, child_info_t *cip, int what)
15559 {
15560 	dev_info_t		*cdip = NULL;
15561 	dev_info_t		*pdip = NULL;
15562 
15563 	ASSERT(plun);
15564 
15565 	mutex_enter(&plun->lun_mutex);
15566 	if (fcp_is_child_present(plun, cip) == FC_FAILURE) {
15567 		mutex_exit(&plun->lun_mutex);
15568 		return (NDI_FAILURE);
15569 	}
15570 	mutex_exit(&plun->lun_mutex);
15571 	cdip = mdi_pi_get_client(PIP(cip));
15572 	pdip = mdi_pi_get_phci(PIP(cip));
15573 
15574 	ASSERT(cdip != NULL);
15575 	ASSERT(pdip != NULL);
15576 
15577 	if (what == FCP_MPXIO_PATH_CLEAR_BUSY) {
15578 		/* LUN ready for IO */
15579 		(void) mdi_pi_enable_path(PIP(cip), DRIVER_DISABLE_TRANSIENT);
15580 	} else {
15581 		/* LUN busy to accept IO */
15582 		(void) mdi_pi_disable_path(PIP(cip), DRIVER_DISABLE_TRANSIENT);
15583 	}
15584 	return (NDI_SUCCESS);
15585 }
15586 
15587 /*
15588  * Caller must free the returned string of MAXPATHLEN len
15589  * If the device is offline (-1 instance number) NULL
15590  * will be returned.
15591  */
15592 static char *
15593 fcp_get_lun_path(struct fcp_lun *plun) {
15594 	dev_info_t	*dip = NULL;
15595 	char	*path = NULL;
15596 	if (plun == NULL) {
15597 		return (NULL);
15598 	}
15599 	if (plun->lun_mpxio == 0) {
15600 		dip = DIP(plun->lun_cip);
15601 	} else {
15602 		dip = mdi_pi_get_client(PIP(plun->lun_cip));
15603 	}
15604 	if (dip == NULL) {
15605 		return (NULL);
15606 	}
15607 	if (ddi_get_instance(dip) < 0) {
15608 		return (NULL);
15609 	}
15610 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
15611 	if (path == NULL) {
15612 		return (NULL);
15613 	}
15614 
15615 	(void) ddi_pathname(dip, path);
15616 	/*
15617 	 * In reality, the user wants a fully valid path (one they can open)
15618 	 * but this string is lacking the mount point, and the minor node.
15619 	 * It would be nice if we could "figure these out" somehow
15620 	 * and fill them in.  Otherwise, the userland code has to understand
15621 	 * driver specific details of which minor node is the "best" or
15622 	 * "right" one to expose.  (Ex: which slice is the whole disk, or
15623 	 * which tape doesn't rewind)
15624 	 */
15625 	return (path);
15626 }
15627 
15628 static int
15629 fcp_scsi_bus_config(dev_info_t *parent, uint_t flag,
15630     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
15631 {
15632 	int64_t reset_delay;
15633 	int rval, retry = 0;
15634 	struct fcp_port *pptr = fcp_dip2port(parent);
15635 
15636 	reset_delay = (int64_t)(USEC_TO_TICK(FCP_INIT_WAIT_TIMEOUT)) -
15637 	    (lbolt64 - pptr->port_attach_time);
15638 	if (reset_delay < 0) {
15639 		reset_delay = 0;
15640 	}
15641 
15642 	if (fcp_bus_config_debug) {
15643 		flag |= NDI_DEVI_DEBUG;
15644 	}
15645 
15646 	switch (op) {
15647 	case BUS_CONFIG_ONE:
15648 		/*
15649 		 * Retry the command since we need to ensure
15650 		 * the fabric devices are available for root
15651 		 */
15652 		while (retry++ < fcp_max_bus_config_retries) {
15653 			rval =	(ndi_busop_bus_config(parent,
15654 			    flag | NDI_MDI_FALLBACK, op,
15655 			    arg, childp, (clock_t)reset_delay));
15656 			if (rval == 0) {
15657 				return (rval);
15658 			}
15659 		}
15660 
15661 		/*
15662 		 * drain taskq to make sure nodes are created and then
15663 		 * try again.
15664 		 */
15665 		taskq_wait(DEVI(parent)->devi_taskq);
15666 		return (ndi_busop_bus_config(parent, flag | NDI_MDI_FALLBACK,
15667 		    op, arg, childp, 0));
15668 
15669 	case BUS_CONFIG_DRIVER:
15670 	case BUS_CONFIG_ALL: {
15671 		/*
15672 		 * delay till all devices report in (port_tmp_cnt == 0)
15673 		 * or FCP_INIT_WAIT_TIMEOUT
15674 		 */
15675 		mutex_enter(&pptr->port_mutex);
15676 		while ((reset_delay > 0) && pptr->port_tmp_cnt) {
15677 			(void) cv_timedwait(&pptr->port_config_cv,
15678 			    &pptr->port_mutex,
15679 			    ddi_get_lbolt() + (clock_t)reset_delay);
15680 			reset_delay =
15681 			    (int64_t)(USEC_TO_TICK(FCP_INIT_WAIT_TIMEOUT)) -
15682 			    (lbolt64 - pptr->port_attach_time);
15683 		}
15684 		mutex_exit(&pptr->port_mutex);
15685 		/* drain taskq to make sure nodes are created */
15686 		taskq_wait(DEVI(parent)->devi_taskq);
15687 		return (ndi_busop_bus_config(parent, flag, op,
15688 		    arg, childp, 0));
15689 	}
15690 
15691 	default:
15692 		return (NDI_FAILURE);
15693 	}
15694 	/*NOTREACHED*/
15695 }
15696 
15697 static int
15698 fcp_scsi_bus_unconfig(dev_info_t *parent, uint_t flag,
15699     ddi_bus_config_op_t op, void *arg)
15700 {
15701 	if (fcp_bus_config_debug) {
15702 		flag |= NDI_DEVI_DEBUG;
15703 	}
15704 
15705 	return (ndi_busop_bus_unconfig(parent, flag, op, arg));
15706 }
15707 
15708 
15709 /*
15710  * Routine to copy GUID into the lun structure.
15711  * returns 0 if copy was successful and 1 if encountered a
15712  * failure and did not copy the guid.
15713  */
15714 static int
15715 fcp_copy_guid_2_lun_block(struct fcp_lun *plun, char *guidp)
15716 {
15717 
15718 	int retval = 0;
15719 
15720 	/* add one for the null terminator */
15721 	const unsigned int len = strlen(guidp) + 1;
15722 
15723 	if ((guidp == NULL) || (plun == NULL)) {
15724 		return (1);
15725 	}
15726 
15727 	/*
15728 	 * if the plun->lun_guid already has been allocated,
15729 	 * then check the size. if the size is exact, reuse
15730 	 * it....if not free it an allocate the required size.
15731 	 * The reallocation should NOT typically happen
15732 	 * unless the GUIDs reported changes between passes.
15733 	 * We free up and alloc again even if the
15734 	 * size was more than required. This is due to the
15735 	 * fact that the field lun_guid_size - serves
15736 	 * dual role of indicating the size of the wwn
15737 	 * size and ALSO the allocation size.
15738 	 */
15739 	if (plun->lun_guid) {
15740 		if (plun->lun_guid_size != len) {
15741 			/*
15742 			 * free the allocated memory and
15743 			 * initialize the field
15744 			 * lun_guid_size to 0.
15745 			 */
15746 			kmem_free(plun->lun_guid, plun->lun_guid_size);
15747 			plun->lun_guid = NULL;
15748 			plun->lun_guid_size = 0;
15749 		}
15750 	}
15751 	/*
15752 	 * alloc only if not already done.
15753 	 */
15754 	if (plun->lun_guid == NULL) {
15755 		plun->lun_guid = kmem_zalloc(len, KM_NOSLEEP);
15756 		if (plun->lun_guid == NULL) {
15757 			cmn_err(CE_WARN, "fcp_copy_guid_2_lun_block:"
15758 			    "Unable to allocate"
15759 			    "Memory for GUID!!! size %d", len);
15760 			retval = 1;
15761 		} else {
15762 			plun->lun_guid_size = len;
15763 		}
15764 	}
15765 	if (plun->lun_guid) {
15766 		/*
15767 		 * now copy the GUID
15768 		 */
15769 		bcopy(guidp, plun->lun_guid, plun->lun_guid_size);
15770 	}
15771 	return (retval);
15772 }
15773 
15774 /*
15775  * fcp_reconfig_wait
15776  *
15777  * Wait for a rediscovery/reconfiguration to complete before continuing.
15778  */
15779 
15780 static void
15781 fcp_reconfig_wait(struct fcp_port *pptr)
15782 {
15783 	clock_t		reconfig_start, wait_timeout;
15784 
15785 	/*
15786 	 * Quick check.	 If pptr->port_tmp_cnt is 0, there is no
15787 	 * reconfiguration in progress.
15788 	 */
15789 
15790 	mutex_enter(&pptr->port_mutex);
15791 	if (pptr->port_tmp_cnt == 0) {
15792 		mutex_exit(&pptr->port_mutex);
15793 		return;
15794 	}
15795 	mutex_exit(&pptr->port_mutex);
15796 
15797 	/*
15798 	 * If we cause a reconfig by raising power, delay until all devices
15799 	 * report in (port_tmp_cnt returns to 0)
15800 	 */
15801 
15802 	reconfig_start = ddi_get_lbolt();
15803 	wait_timeout = drv_usectohz(FCP_INIT_WAIT_TIMEOUT);
15804 
15805 	mutex_enter(&pptr->port_mutex);
15806 
15807 	while (((ddi_get_lbolt() - reconfig_start) < wait_timeout) &&
15808 	    pptr->port_tmp_cnt) {
15809 
15810 		(void) cv_timedwait(&pptr->port_config_cv, &pptr->port_mutex,
15811 		    reconfig_start + wait_timeout);
15812 	}
15813 
15814 	mutex_exit(&pptr->port_mutex);
15815 
15816 	/*
15817 	 * Even if fcp_tmp_count isn't 0, continue without error.  The port
15818 	 * we want may still be ok.  If not, it will error out later
15819 	 */
15820 }
15821 
15822 /*
15823  * Read masking info from fp.conf and construct the global fcp_lun_blacklist.
15824  * We rely on the fcp_global_mutex to provide protection against changes to
15825  * the fcp_lun_blacklist.
15826  *
15827  * You can describe a list of target port WWNs and LUN numbers which will
15828  * not be configured. LUN numbers will be interpreted as decimal. White
15829  * spaces and ',' can be used in the list of LUN numbers.
15830  *
15831  * To prevent LUNs 1 and 2 from being configured for target
15832  * port 510000f010fd92a1 and target port 510000e012079df1, set:
15833  *
15834  * pwwn-lun-blacklist=
15835  * "510000f010fd92a1,1,2",
15836  * "510000e012079df1,1,2";
15837  */
15838 static void
15839 fcp_read_blacklist(dev_info_t *dip,
15840     struct fcp_black_list_entry **pplun_blacklist) {
15841 	char **prop_array	= NULL;
15842 	char *curr_pwwn		= NULL;
15843 	char *curr_lun		= NULL;
15844 	uint32_t prop_item	= 0;
15845 	int idx			= 0;
15846 	int len			= 0;
15847 
15848 	ASSERT(mutex_owned(&fcp_global_mutex));
15849 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
15850 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
15851 	    LUN_BLACKLIST_PROP, &prop_array, &prop_item) != DDI_PROP_SUCCESS) {
15852 		return;
15853 	}
15854 
15855 	for (idx = 0; idx < prop_item; idx++) {
15856 
15857 		curr_pwwn = prop_array[idx];
15858 		while (*curr_pwwn == ' ') {
15859 			curr_pwwn++;
15860 		}
15861 		if (strlen(curr_pwwn) <= (sizeof (la_wwn_t) * 2 + 1)) {
15862 			fcp_log(CE_WARN, NULL, "Invalid WWN %s in the blacklist"
15863 			    ", please check.", curr_pwwn);
15864 			continue;
15865 		}
15866 		if ((*(curr_pwwn + sizeof (la_wwn_t) * 2) != ' ') &&
15867 		    (*(curr_pwwn + sizeof (la_wwn_t) * 2) != ',')) {
15868 			fcp_log(CE_WARN, NULL, "Invalid WWN %s in the blacklist"
15869 			    ", please check.", curr_pwwn);
15870 			continue;
15871 		}
15872 		for (len = 0; len < sizeof (la_wwn_t) * 2; len++) {
15873 			if (isxdigit(curr_pwwn[len]) != TRUE) {
15874 				fcp_log(CE_WARN, NULL, "Invalid WWN %s in the "
15875 				    "blacklist, please check.", curr_pwwn);
15876 				break;
15877 			}
15878 		}
15879 		if (len != sizeof (la_wwn_t) * 2) {
15880 			continue;
15881 		}
15882 
15883 		curr_lun = curr_pwwn + sizeof (la_wwn_t) * 2 + 1;
15884 		*(curr_lun - 1) = '\0';
15885 		fcp_mask_pwwn_lun(curr_pwwn, curr_lun, pplun_blacklist);
15886 	}
15887 
15888 	ddi_prop_free(prop_array);
15889 }
15890 
15891 /*
15892  * Get the masking info about one remote target port designated by wwn.
15893  * Lun ids could be separated by ',' or white spaces.
15894  */
15895 static void
15896 fcp_mask_pwwn_lun(char *curr_pwwn, char *curr_lun,
15897     struct fcp_black_list_entry **pplun_blacklist) {
15898 	int		idx			= 0;
15899 	uint32_t	offset			= 0;
15900 	unsigned long	lun_id			= 0;
15901 	char		lunid_buf[16];
15902 	char		*pend			= NULL;
15903 	int		illegal_digit		= 0;
15904 
15905 	while (offset < strlen(curr_lun)) {
15906 		while ((curr_lun[offset + idx] != ',') &&
15907 		    (curr_lun[offset + idx] != '\0') &&
15908 		    (curr_lun[offset + idx] != ' ')) {
15909 			if (isdigit(curr_lun[offset + idx]) == 0) {
15910 				illegal_digit++;
15911 			}
15912 			idx++;
15913 		}
15914 		if (illegal_digit > 0) {
15915 			offset += (idx+1);	/* To the start of next lun */
15916 			idx = 0;
15917 			illegal_digit = 0;
15918 			fcp_log(CE_WARN, NULL, "Invalid LUN %s for WWN %s in "
15919 			    "the blacklist, please check digits.",
15920 			    curr_lun, curr_pwwn);
15921 			continue;
15922 		}
15923 		if (idx >= (sizeof (lunid_buf) / sizeof (lunid_buf[0]))) {
15924 			fcp_log(CE_WARN, NULL, "Invalid LUN %s for WWN %s in "
15925 			    "the blacklist, please check the length of LUN#.",
15926 			    curr_lun, curr_pwwn);
15927 			break;
15928 		}
15929 		if (idx == 0) {	/* ignore ' ' or ',' or '\0' */
15930 		    offset++;
15931 		    continue;
15932 		}
15933 
15934 		bcopy(curr_lun + offset, lunid_buf, idx);
15935 		lunid_buf[idx] = '\0';
15936 		if (ddi_strtoul(lunid_buf, &pend, 10, &lun_id) == 0) {
15937 			fcp_add_one_mask(curr_pwwn, lun_id, pplun_blacklist);
15938 		} else {
15939 			fcp_log(CE_WARN, NULL, "Invalid LUN %s for WWN %s in "
15940 			    "the blacklist, please check %s.",
15941 			    curr_lun, curr_pwwn, lunid_buf);
15942 		}
15943 		offset += (idx+1);	/* To the start of next lun */
15944 		idx = 0;
15945 	}
15946 }
15947 
15948 /*
15949  * Add one masking record
15950  */
15951 static void
15952 fcp_add_one_mask(char *curr_pwwn, uint32_t lun_id,
15953     struct fcp_black_list_entry **pplun_blacklist) {
15954 	struct fcp_black_list_entry	*tmp_entry	= *pplun_blacklist;
15955 	struct fcp_black_list_entry	*new_entry	= NULL;
15956 	la_wwn_t			wwn;
15957 
15958 	fcp_ascii_to_wwn(curr_pwwn, wwn.raw_wwn, sizeof (la_wwn_t));
15959 	while (tmp_entry) {
15960 		if ((bcmp(&tmp_entry->wwn, &wwn,
15961 		    sizeof (la_wwn_t)) == 0) && (tmp_entry->lun == lun_id)) {
15962 			return;
15963 		}
15964 
15965 		tmp_entry = tmp_entry->next;
15966 	}
15967 
15968 	/* add to black list */
15969 	new_entry = (struct fcp_black_list_entry *)kmem_zalloc
15970 	    (sizeof (struct fcp_black_list_entry), KM_SLEEP);
15971 	bcopy(&wwn, &new_entry->wwn, sizeof (la_wwn_t));
15972 	new_entry->lun = lun_id;
15973 	new_entry->masked = 0;
15974 	new_entry->next = *pplun_blacklist;
15975 	*pplun_blacklist = new_entry;
15976 }
15977 
15978 /*
15979  * Check if we should mask the specified lun of this fcp_tgt
15980  */
15981 static int
15982 fcp_should_mask(la_wwn_t *wwn, uint32_t lun_id) {
15983 	struct fcp_black_list_entry *remote_port;
15984 
15985 	remote_port = fcp_lun_blacklist;
15986 	while (remote_port != NULL) {
15987 		if (bcmp(wwn, &remote_port->wwn, sizeof (la_wwn_t)) == 0) {
15988 			if (remote_port->lun == lun_id) {
15989 				remote_port->masked++;
15990 				if (remote_port->masked == 1) {
15991 					fcp_log(CE_NOTE, NULL, "LUN %d of port "
15992 					    "%02x%02x%02x%02x%02x%02x%02x%02x "
15993 					    "is masked due to black listing.\n",
15994 					    lun_id, wwn->raw_wwn[0],
15995 					    wwn->raw_wwn[1], wwn->raw_wwn[2],
15996 					    wwn->raw_wwn[3], wwn->raw_wwn[4],
15997 					    wwn->raw_wwn[5], wwn->raw_wwn[6],
15998 					    wwn->raw_wwn[7]);
15999 				}
16000 				return (TRUE);
16001 			}
16002 		}
16003 		remote_port = remote_port->next;
16004 	}
16005 	return (FALSE);
16006 }
16007 
16008 /*
16009  * Release all allocated resources
16010  */
16011 static void
16012 fcp_cleanup_blacklist(struct fcp_black_list_entry **pplun_blacklist) {
16013 	struct fcp_black_list_entry	*tmp_entry	= *pplun_blacklist;
16014 	struct fcp_black_list_entry	*current_entry	= NULL;
16015 
16016 	ASSERT(mutex_owned(&fcp_global_mutex));
16017 	/*
16018 	 * Traverse all luns
16019 	 */
16020 	while (tmp_entry) {
16021 		current_entry = tmp_entry;
16022 		tmp_entry = tmp_entry->next;
16023 		kmem_free(current_entry, sizeof (struct fcp_black_list_entry));
16024 	}
16025 	*pplun_blacklist = NULL;
16026 }
16027 
16028 /*
16029  * In fcp module,
16030  *   pkt@scsi_pkt, cmd@fcp_pkt, icmd@fcp_ipkt, fpkt@fc_packet, pptr@fcp_port
16031  */
16032 static struct scsi_pkt *
16033 fcp_pseudo_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
16034     struct buf *bp, int cmdlen, int statuslen, int tgtlen,
16035     int flags, int (*callback)(), caddr_t arg)
16036 {
16037 	fcp_port_t	*pptr = ADDR2FCP(ap);
16038 	fcp_pkt_t	*cmd  = NULL;
16039 	fc_frame_hdr_t	*hp;
16040 
16041 	/*
16042 	 * First step: get the packet
16043 	 */
16044 	if (pkt == NULL) {
16045 		pkt = scsi_hba_pkt_alloc(pptr->port_dip, ap, cmdlen, statuslen,
16046 		    tgtlen, sizeof (fcp_pkt_t) + pptr->port_priv_pkt_len,
16047 		    callback, arg);
16048 		if (pkt == NULL) {
16049 			return (NULL);
16050 		}
16051 
16052 		/*
16053 		 * All fields in scsi_pkt will be initialized properly or
16054 		 * set to zero. We need do nothing for scsi_pkt.
16055 		 */
16056 		/*
16057 		 * But it's our responsibility to link other related data
16058 		 * structures. Their initialization will be done, just
16059 		 * before the scsi_pkt will be sent to FCA.
16060 		 */
16061 		cmd		= PKT2CMD(pkt);
16062 		cmd->cmd_pkt	= pkt;
16063 		cmd->cmd_fp_pkt = &cmd->cmd_fc_packet;
16064 		/*
16065 		 * fc_packet_t
16066 		 */
16067 		cmd->cmd_fp_pkt->pkt_ulp_private = (opaque_t)cmd;
16068 		cmd->cmd_fp_pkt->pkt_fca_private = (opaque_t)((caddr_t)cmd +
16069 		    sizeof (struct fcp_pkt));
16070 		cmd->cmd_fp_pkt->pkt_cmd = (caddr_t)&cmd->cmd_fcp_cmd;
16071 		cmd->cmd_fp_pkt->pkt_cmdlen = sizeof (struct fcp_cmd);
16072 		cmd->cmd_fp_pkt->pkt_resp = cmd->cmd_fcp_rsp;
16073 		cmd->cmd_fp_pkt->pkt_rsplen = FCP_MAX_RSP_IU_SIZE;
16074 		/*
16075 		 * Fill in the Fabric Channel Header
16076 		 */
16077 		hp = &cmd->cmd_fp_pkt->pkt_cmd_fhdr;
16078 		hp->r_ctl = R_CTL_COMMAND;
16079 		hp->rsvd = 0;
16080 		hp->type = FC_TYPE_SCSI_FCP;
16081 		hp->f_ctl = F_CTL_SEQ_INITIATIVE | F_CTL_FIRST_SEQ;
16082 		hp->seq_id = 0;
16083 		hp->df_ctl  = 0;
16084 		hp->seq_cnt = 0;
16085 		hp->ox_id = 0xffff;
16086 		hp->rx_id = 0xffff;
16087 		hp->ro = 0;
16088 	} else {
16089 		/*
16090 		 * We need think if we should reset any elements in
16091 		 * related data structures.
16092 		 */
16093 		FCP_TRACE(fcp_logq, pptr->port_instbuf,
16094 		    fcp_trace, FCP_BUF_LEVEL_6, 0,
16095 		    "reusing pkt, flags %d", flags);
16096 		cmd = PKT2CMD(pkt);
16097 		if (cmd->cmd_fp_pkt->pkt_pd) {
16098 			cmd->cmd_fp_pkt->pkt_pd = NULL;
16099 		}
16100 	}
16101 
16102 	/*
16103 	 * Second step:	 dma allocation/move
16104 	 */
16105 	if (bp && bp->b_bcount != 0) {
16106 		/*
16107 		 * Mark if it's read or write
16108 		 */
16109 		if (bp->b_flags & B_READ) {
16110 			cmd->cmd_flags |= CFLAG_IS_READ;
16111 		} else {
16112 			cmd->cmd_flags &= ~CFLAG_IS_READ;
16113 		}
16114 
16115 		bp_mapin(bp);
16116 		cmd->cmd_fp_pkt->pkt_data = bp->b_un.b_addr;
16117 		cmd->cmd_fp_pkt->pkt_datalen = bp->b_bcount;
16118 		cmd->cmd_fp_pkt->pkt_data_resid = 0;
16119 	} else {
16120 		/*
16121 		 * It seldom happens, except when CLUSTER or SCSI_VHCI wants
16122 		 * to send zero-length read/write.
16123 		 */
16124 		cmd->cmd_fp_pkt->pkt_data = NULL;
16125 		cmd->cmd_fp_pkt->pkt_datalen = 0;
16126 	}
16127 
16128 	return (pkt);
16129 }
16130 
16131 static void
16132 fcp_pseudo_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
16133 {
16134 	fcp_port_t	*pptr = ADDR2FCP(ap);
16135 
16136 	/*
16137 	 * First we let FCA to uninitilize private part.
16138 	 */
16139 	fc_ulp_uninit_packet(pptr->port_fp_handle, PKT2CMD(pkt)->cmd_fp_pkt);
16140 
16141 	/*
16142 	 * Then we uninitialize fc_packet.
16143 	 */
16144 
16145 	/*
16146 	 * Thirdly, we uninitializae fcp_pkt.
16147 	 */
16148 
16149 	/*
16150 	 * In the end, we free scsi_pkt.
16151 	 */
16152 	scsi_hba_pkt_free(ap, pkt);
16153 }
16154 
16155 static int
16156 fcp_pseudo_start(struct scsi_address *ap, struct scsi_pkt *pkt)
16157 {
16158 	fcp_port_t	*pptr = ADDR2FCP(ap);
16159 	fcp_lun_t	*plun = ADDR2LUN(ap);
16160 	fcp_tgt_t	*ptgt = plun->lun_tgt;
16161 	fcp_pkt_t	*cmd  = PKT2CMD(pkt);
16162 	fcp_cmd_t	*fcmd = &cmd->cmd_fcp_cmd;
16163 	fc_packet_t	*fpkt = cmd->cmd_fp_pkt;
16164 	int		 rval;
16165 
16166 	fpkt->pkt_pd = ptgt->tgt_pd_handle;
16167 	fc_ulp_init_packet(pptr->port_fp_handle, cmd->cmd_fp_pkt, 1);
16168 
16169 	/*
16170 	 * Firstly, we need initialize fcp_pkt_t
16171 	 * Secondly, we need initialize fcp_cmd_t.
16172 	 */
16173 	bcopy(pkt->pkt_cdbp, fcmd->fcp_cdb, pkt->pkt_cdblen);
16174 	fcmd->fcp_data_len = fpkt->pkt_datalen;
16175 	fcmd->fcp_ent_addr = plun->lun_addr;
16176 	if (pkt->pkt_flags & FLAG_HTAG) {
16177 		fcmd->fcp_cntl.cntl_qtype = FCP_QTYPE_HEAD_OF_Q;
16178 	} else if (pkt->pkt_flags & FLAG_OTAG) {
16179 		fcmd->fcp_cntl.cntl_qtype = FCP_QTYPE_ORDERED;
16180 	} else if (pkt->pkt_flags & FLAG_STAG) {
16181 		fcmd->fcp_cntl.cntl_qtype = FCP_QTYPE_SIMPLE;
16182 	} else {
16183 		fcmd->fcp_cntl.cntl_qtype = FCP_QTYPE_UNTAGGED;
16184 	}
16185 
16186 	if (cmd->cmd_flags & CFLAG_IS_READ) {
16187 		fcmd->fcp_cntl.cntl_read_data = 1;
16188 		fcmd->fcp_cntl.cntl_write_data = 0;
16189 	} else {
16190 		fcmd->fcp_cntl.cntl_read_data = 0;
16191 		fcmd->fcp_cntl.cntl_write_data = 1;
16192 	}
16193 
16194 	/*
16195 	 * Then we need initialize fc_packet_t too.
16196 	 */
16197 	fpkt->pkt_timeout = pkt->pkt_time + 2;
16198 	fpkt->pkt_cmd_fhdr.d_id = ptgt->tgt_d_id;
16199 	fpkt->pkt_cmd_fhdr.s_id = pptr->port_id;
16200 	if (cmd->cmd_flags & CFLAG_IS_READ) {
16201 		fpkt->pkt_tran_type = FC_PKT_FCP_READ;
16202 	} else {
16203 		fpkt->pkt_tran_type = FC_PKT_FCP_WRITE;
16204 	}
16205 
16206 	if (pkt->pkt_flags & FLAG_NOINTR) {
16207 		fpkt->pkt_comp = NULL;
16208 		fpkt->pkt_tran_flags = (FC_TRAN_CLASS3 | FC_TRAN_NO_INTR);
16209 	} else {
16210 		fpkt->pkt_comp = fcp_cmd_callback;
16211 		fpkt->pkt_tran_flags = (FC_TRAN_CLASS3 | FC_TRAN_INTR);
16212 		if (pkt->pkt_flags & FLAG_IMMEDIATE_CB) {
16213 			fpkt->pkt_tran_flags |= FC_TRAN_IMMEDIATE_CB;
16214 		}
16215 	}
16216 
16217 	/*
16218 	 * Lastly, we need initialize scsi_pkt
16219 	 */
16220 	pkt->pkt_reason = CMD_CMPLT;
16221 	pkt->pkt_state = 0;
16222 	pkt->pkt_statistics = 0;
16223 	pkt->pkt_resid = 0;
16224 
16225 	/*
16226 	 * if interrupts aren't allowed (e.g. at dump time) then we'll
16227 	 * have to do polled I/O
16228 	 */
16229 	if (pkt->pkt_flags & FLAG_NOINTR) {
16230 		return (fcp_dopoll(pptr, cmd));
16231 	}
16232 
16233 	cmd->cmd_state = FCP_PKT_ISSUED;
16234 	rval = fcp_transport(pptr->port_fp_handle, fpkt, 0);
16235 	if (rval == FC_SUCCESS) {
16236 		return (TRAN_ACCEPT);
16237 	}
16238 
16239 	/*
16240 	 * Need more consideration
16241 	 *
16242 	 * pkt->pkt_flags & FLAG_NOQUEUE could abort other pkt
16243 	 */
16244 	cmd->cmd_state = FCP_PKT_IDLE;
16245 	if (rval == FC_TRAN_BUSY) {
16246 		return (TRAN_BUSY);
16247 	} else {
16248 		return (TRAN_FATAL_ERROR);
16249 	}
16250 }
16251 
16252 /*
16253  * scsi_poll will always call tran_sync_pkt for pseudo FC-HBAs
16254  * SCSA will initialize it to scsi_sync_cache_pkt for physical FC-HBAs
16255  */
16256 static void
16257 fcp_pseudo_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
16258 {
16259 	FCP_TRACE(fcp_logq, "fcp_pseudo_sync_pkt", fcp_trace,
16260 	    FCP_BUF_LEVEL_2, 0, "ap-%p, scsi_pkt-%p", ap, pkt);
16261 }
16262 
16263 /*
16264  * scsi_dmafree will always call tran_dmafree, when STATE_ARQ_DONE
16265  */
16266 static void
16267 fcp_pseudo_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
16268 {
16269 	FCP_TRACE(fcp_logq, "fcp_pseudo_dmafree", fcp_trace,
16270 	    FCP_BUF_LEVEL_2, 0, "ap-%p, scsi_pkt-%p", ap, pkt);
16271 }
16272