xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_ct.c (revision 05bcf503)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2010 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  *                                                                 *
8  * This program is free software; you can redistribute it and/or   *
9  * modify it under the terms of version 2 of the GNU General       *
10  * Public License as published by the Free Software Foundation.    *
11  * This program is distributed in the hope that it will be useful. *
12  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
13  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
14  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
15  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
17  * more details, a copy of which can be found in the file COPYING  *
18  * included with this package.                                     *
19  *******************************************************************/
20 
21 /*
22  * Fibre Channel SCSI LAN Device Driver CT support: FC Generic Services FC-GS
23  */
24 
25 #include <linux/blkdev.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 #include <linux/slab.h>
29 #include <linux/utsname.h>
30 
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_transport_fc.h>
35 #include <scsi/fc/fc_fs.h>
36 
37 #include "lpfc_hw4.h"
38 #include "lpfc_hw.h"
39 #include "lpfc_sli.h"
40 #include "lpfc_sli4.h"
41 #include "lpfc_nl.h"
42 #include "lpfc_disc.h"
43 #include "lpfc_scsi.h"
44 #include "lpfc.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_crtn.h"
47 #include "lpfc_version.h"
48 #include "lpfc_vport.h"
49 #include "lpfc_debugfs.h"
50 
51 /* FDMI Port Speed definitions */
52 #define HBA_PORTSPEED_1GBIT		0x0001	/* 1 GBit/sec */
53 #define HBA_PORTSPEED_2GBIT		0x0002	/* 2 GBit/sec */
54 #define HBA_PORTSPEED_4GBIT		0x0008	/* 4 GBit/sec */
55 #define HBA_PORTSPEED_10GBIT		0x0004	/* 10 GBit/sec */
56 #define HBA_PORTSPEED_8GBIT		0x0010	/* 8 GBit/sec */
57 #define HBA_PORTSPEED_16GBIT		0x0020	/* 16 GBit/sec */
58 #define HBA_PORTSPEED_UNKNOWN		0x0800	/* Unknown */
59 
60 #define FOURBYTES	4
61 
62 
63 static char *lpfc_release_version = LPFC_DRIVER_VERSION;
64 
65 static void
66 lpfc_ct_ignore_hbq_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
67 			  struct lpfc_dmabuf *mp, uint32_t size)
68 {
69 	if (!mp) {
70 		lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
71 				"0146 Ignoring unsolicited CT No HBQ "
72 				"status = x%x\n",
73 				piocbq->iocb.ulpStatus);
74 	}
75 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
76 			"0145 Ignoring unsolicted CT HBQ Size:%d "
77 			"status = x%x\n",
78 			size, piocbq->iocb.ulpStatus);
79 }
80 
81 static void
82 lpfc_ct_unsol_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
83 		     struct lpfc_dmabuf *mp, uint32_t size)
84 {
85 	lpfc_ct_ignore_hbq_buffer(phba, piocbq, mp, size);
86 }
87 
88 void
89 lpfc_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
90 		    struct lpfc_iocbq *piocbq)
91 {
92 	struct lpfc_dmabuf *mp = NULL;
93 	IOCB_t *icmd = &piocbq->iocb;
94 	int i;
95 	struct lpfc_iocbq *iocbq;
96 	dma_addr_t paddr;
97 	uint32_t size;
98 	struct list_head head;
99 	struct lpfc_dmabuf *bdeBuf;
100 
101 	if (lpfc_bsg_ct_unsol_event(phba, pring, piocbq) == 0)
102 		return;
103 
104 	if (unlikely(icmd->ulpStatus == IOSTAT_NEED_BUFFER)) {
105 		lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
106 	} else if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
107 		   ((icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
108 		   IOERR_RCV_BUFFER_WAITING)) {
109 		/* Not enough posted buffers; Try posting more buffers */
110 		phba->fc_stat.NoRcvBuf++;
111 		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
112 			lpfc_post_buffer(phba, pring, 2);
113 		return;
114 	}
115 
116 	/* If there are no BDEs associated with this IOCB,
117 	 * there is nothing to do.
118 	 */
119 	if (icmd->ulpBdeCount == 0)
120 		return;
121 
122 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
123 		INIT_LIST_HEAD(&head);
124 		list_add_tail(&head, &piocbq->list);
125 		list_for_each_entry(iocbq, &head, list) {
126 			icmd = &iocbq->iocb;
127 			if (icmd->ulpBdeCount == 0)
128 				continue;
129 			bdeBuf = iocbq->context2;
130 			iocbq->context2 = NULL;
131 			size  = icmd->un.cont64[0].tus.f.bdeSize;
132 			lpfc_ct_unsol_buffer(phba, piocbq, bdeBuf, size);
133 			lpfc_in_buf_free(phba, bdeBuf);
134 			if (icmd->ulpBdeCount == 2) {
135 				bdeBuf = iocbq->context3;
136 				iocbq->context3 = NULL;
137 				size  = icmd->unsli3.rcvsli3.bde2.tus.f.bdeSize;
138 				lpfc_ct_unsol_buffer(phba, piocbq, bdeBuf,
139 						     size);
140 				lpfc_in_buf_free(phba, bdeBuf);
141 			}
142 		}
143 		list_del(&head);
144 	} else {
145 		INIT_LIST_HEAD(&head);
146 		list_add_tail(&head, &piocbq->list);
147 		list_for_each_entry(iocbq, &head, list) {
148 			icmd = &iocbq->iocb;
149 			if (icmd->ulpBdeCount == 0)
150 				lpfc_ct_unsol_buffer(phba, iocbq, NULL, 0);
151 			for (i = 0; i < icmd->ulpBdeCount; i++) {
152 				paddr = getPaddr(icmd->un.cont64[i].addrHigh,
153 						 icmd->un.cont64[i].addrLow);
154 				mp = lpfc_sli_ringpostbuf_get(phba, pring,
155 							      paddr);
156 				size = icmd->un.cont64[i].tus.f.bdeSize;
157 				lpfc_ct_unsol_buffer(phba, iocbq, mp, size);
158 				lpfc_in_buf_free(phba, mp);
159 			}
160 			lpfc_post_buffer(phba, pring, i);
161 		}
162 		list_del(&head);
163 	}
164 }
165 
166 /**
167  * lpfc_sli4_ct_abort_unsol_event - Default handle for sli4 unsol abort
168  * @phba: Pointer to HBA context object.
169  * @pring: Pointer to the driver internal I/O ring.
170  * @piocbq: Pointer to the IOCBQ.
171  *
172  * This function serves as the default handler for the sli4 unsolicited
173  * abort event. It shall be invoked when there is no application interface
174  * registered unsolicited abort handler. This handler does nothing but
175  * just simply releases the dma buffer used by the unsol abort event.
176  **/
177 void
178 lpfc_sli4_ct_abort_unsol_event(struct lpfc_hba *phba,
179 			       struct lpfc_sli_ring *pring,
180 			       struct lpfc_iocbq *piocbq)
181 {
182 	IOCB_t *icmd = &piocbq->iocb;
183 	struct lpfc_dmabuf *bdeBuf;
184 	uint32_t size;
185 
186 	/* Forward abort event to any process registered to receive ct event */
187 	if (lpfc_bsg_ct_unsol_event(phba, pring, piocbq) == 0)
188 		return;
189 
190 	/* If there is no BDE associated with IOCB, there is nothing to do */
191 	if (icmd->ulpBdeCount == 0)
192 		return;
193 	bdeBuf = piocbq->context2;
194 	piocbq->context2 = NULL;
195 	size  = icmd->un.cont64[0].tus.f.bdeSize;
196 	lpfc_ct_unsol_buffer(phba, piocbq, bdeBuf, size);
197 	lpfc_in_buf_free(phba, bdeBuf);
198 }
199 
200 static void
201 lpfc_free_ct_rsp(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
202 {
203 	struct lpfc_dmabuf *mlast, *next_mlast;
204 
205 	list_for_each_entry_safe(mlast, next_mlast, &mlist->list, list) {
206 		lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
207 		list_del(&mlast->list);
208 		kfree(mlast);
209 	}
210 	lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
211 	kfree(mlist);
212 	return;
213 }
214 
215 static struct lpfc_dmabuf *
216 lpfc_alloc_ct_rsp(struct lpfc_hba *phba, int cmdcode, struct ulp_bde64 *bpl,
217 		  uint32_t size, int *entries)
218 {
219 	struct lpfc_dmabuf *mlist = NULL;
220 	struct lpfc_dmabuf *mp;
221 	int cnt, i = 0;
222 
223 	/* We get chunks of FCELSSIZE */
224 	cnt = size > FCELSSIZE ? FCELSSIZE: size;
225 
226 	while (size) {
227 		/* Allocate buffer for rsp payload */
228 		mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
229 		if (!mp) {
230 			if (mlist)
231 				lpfc_free_ct_rsp(phba, mlist);
232 			return NULL;
233 		}
234 
235 		INIT_LIST_HEAD(&mp->list);
236 
237 		if (cmdcode == be16_to_cpu(SLI_CTNS_GID_FT) ||
238 		    cmdcode == be16_to_cpu(SLI_CTNS_GFF_ID))
239 			mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
240 		else
241 			mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
242 
243 		if (!mp->virt) {
244 			kfree(mp);
245 			if (mlist)
246 				lpfc_free_ct_rsp(phba, mlist);
247 			return NULL;
248 		}
249 
250 		/* Queue it to a linked list */
251 		if (!mlist)
252 			mlist = mp;
253 		else
254 			list_add_tail(&mp->list, &mlist->list);
255 
256 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
257 		/* build buffer ptr list for IOCB */
258 		bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
259 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
260 		bpl->tus.f.bdeSize = (uint16_t) cnt;
261 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
262 		bpl++;
263 
264 		i++;
265 		size -= cnt;
266 	}
267 
268 	*entries = i;
269 	return mlist;
270 }
271 
272 int
273 lpfc_ct_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *ctiocb)
274 {
275 	struct lpfc_dmabuf *buf_ptr;
276 
277 	if (ctiocb->context_un.ndlp) {
278 		lpfc_nlp_put(ctiocb->context_un.ndlp);
279 		ctiocb->context_un.ndlp = NULL;
280 	}
281 	if (ctiocb->context1) {
282 		buf_ptr = (struct lpfc_dmabuf *) ctiocb->context1;
283 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
284 		kfree(buf_ptr);
285 		ctiocb->context1 = NULL;
286 	}
287 	if (ctiocb->context2) {
288 		lpfc_free_ct_rsp(phba, (struct lpfc_dmabuf *) ctiocb->context2);
289 		ctiocb->context2 = NULL;
290 	}
291 
292 	if (ctiocb->context3) {
293 		buf_ptr = (struct lpfc_dmabuf *) ctiocb->context3;
294 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
295 		kfree(buf_ptr);
296 		ctiocb->context1 = NULL;
297 	}
298 	lpfc_sli_release_iocbq(phba, ctiocb);
299 	return 0;
300 }
301 
302 static int
303 lpfc_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
304 	     struct lpfc_dmabuf *inp, struct lpfc_dmabuf *outp,
305 	     void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
306 		     struct lpfc_iocbq *),
307 	     struct lpfc_nodelist *ndlp, uint32_t usr_flg, uint32_t num_entry,
308 	     uint32_t tmo, uint8_t retry)
309 {
310 	struct lpfc_hba  *phba = vport->phba;
311 	IOCB_t *icmd;
312 	struct lpfc_iocbq *geniocb;
313 	int rc;
314 
315 	/* Allocate buffer for  command iocb */
316 	geniocb = lpfc_sli_get_iocbq(phba);
317 
318 	if (geniocb == NULL)
319 		return 1;
320 
321 	icmd = &geniocb->iocb;
322 	icmd->un.genreq64.bdl.ulpIoTag32 = 0;
323 	icmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
324 	icmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
325 	icmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
326 	icmd->un.genreq64.bdl.bdeSize = (num_entry * sizeof (struct ulp_bde64));
327 
328 	if (usr_flg)
329 		geniocb->context3 = NULL;
330 	else
331 		geniocb->context3 = (uint8_t *) bmp;
332 
333 	/* Save for completion so we can release these resources */
334 	geniocb->context1 = (uint8_t *) inp;
335 	geniocb->context2 = (uint8_t *) outp;
336 	geniocb->context_un.ndlp = lpfc_nlp_get(ndlp);
337 
338 	/* Fill in payload, bp points to frame payload */
339 	icmd->ulpCommand = CMD_GEN_REQUEST64_CR;
340 
341 	/* Fill in rest of iocb */
342 	icmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
343 	icmd->un.genreq64.w5.hcsw.Dfctl = 0;
344 	icmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
345 	icmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
346 
347 	if (!tmo) {
348 		 /* FC spec states we need 3 * ratov for CT requests */
349 		tmo = (3 * phba->fc_ratov);
350 	}
351 	icmd->ulpTimeout = tmo;
352 	icmd->ulpBdeCount = 1;
353 	icmd->ulpLe = 1;
354 	icmd->ulpClass = CLASS3;
355 	icmd->ulpContext = ndlp->nlp_rpi;
356 	if (phba->sli_rev == LPFC_SLI_REV4)
357 		icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
358 
359 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
360 		/* For GEN_REQUEST64_CR, use the RPI */
361 		icmd->ulpCt_h = 0;
362 		icmd->ulpCt_l = 0;
363 	}
364 
365 	/* Issue GEN REQ IOCB for NPORT <did> */
366 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
367 			 "0119 Issue GEN REQ IOCB to NPORT x%x "
368 			 "Data: x%x x%x\n",
369 			 ndlp->nlp_DID, icmd->ulpIoTag,
370 			 vport->port_state);
371 	geniocb->iocb_cmpl = cmpl;
372 	geniocb->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
373 	geniocb->vport = vport;
374 	geniocb->retry = retry;
375 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, geniocb, 0);
376 
377 	if (rc == IOCB_ERROR) {
378 		lpfc_sli_release_iocbq(phba, geniocb);
379 		return 1;
380 	}
381 
382 	return 0;
383 }
384 
385 static int
386 lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
387 	    struct lpfc_dmabuf *bmp, struct lpfc_nodelist *ndlp,
388 	    void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
389 			  struct lpfc_iocbq *),
390 	    uint32_t rsp_size, uint8_t retry)
391 {
392 	struct lpfc_hba  *phba = vport->phba;
393 	struct ulp_bde64 *bpl = (struct ulp_bde64 *) bmp->virt;
394 	struct lpfc_dmabuf *outmp;
395 	int cnt = 0, status;
396 	int cmdcode = ((struct lpfc_sli_ct_request *) inmp->virt)->
397 		CommandResponse.bits.CmdRsp;
398 
399 	bpl++;			/* Skip past ct request */
400 
401 	/* Put buffer(s) for ct rsp in bpl */
402 	outmp = lpfc_alloc_ct_rsp(phba, cmdcode, bpl, rsp_size, &cnt);
403 	if (!outmp)
404 		return -ENOMEM;
405 	/*
406 	 * Form the CT IOCB.  The total number of BDEs in this IOCB
407 	 * is the single command plus response count from
408 	 * lpfc_alloc_ct_rsp.
409 	 */
410 	cnt += 1;
411 	status = lpfc_gen_req(vport, bmp, inmp, outmp, cmpl, ndlp, 0,
412 			      cnt, 0, retry);
413 	if (status) {
414 		lpfc_free_ct_rsp(phba, outmp);
415 		return -ENOMEM;
416 	}
417 	return 0;
418 }
419 
420 struct lpfc_vport *
421 lpfc_find_vport_by_did(struct lpfc_hba *phba, uint32_t did) {
422 	struct lpfc_vport *vport_curr;
423 	unsigned long flags;
424 
425 	spin_lock_irqsave(&phba->hbalock, flags);
426 	list_for_each_entry(vport_curr, &phba->port_list, listentry) {
427 		if ((vport_curr->fc_myDID) && (vport_curr->fc_myDID == did)) {
428 			spin_unlock_irqrestore(&phba->hbalock, flags);
429 			return vport_curr;
430 		}
431 	}
432 	spin_unlock_irqrestore(&phba->hbalock, flags);
433 	return NULL;
434 }
435 
436 static int
437 lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint32_t Size)
438 {
439 	struct lpfc_hba  *phba = vport->phba;
440 	struct lpfc_sli_ct_request *Response =
441 		(struct lpfc_sli_ct_request *) mp->virt;
442 	struct lpfc_nodelist *ndlp = NULL;
443 	struct lpfc_dmabuf *mlast, *next_mp;
444 	uint32_t *ctptr = (uint32_t *) & Response->un.gid.PortType;
445 	uint32_t Did, CTentry;
446 	int Cnt;
447 	struct list_head head;
448 
449 	lpfc_set_disctmo(vport);
450 	vport->num_disc_nodes = 0;
451 	vport->fc_ns_retry = 0;
452 
453 
454 	list_add_tail(&head, &mp->list);
455 	list_for_each_entry_safe(mp, next_mp, &head, list) {
456 		mlast = mp;
457 
458 		Cnt = Size  > FCELSSIZE ? FCELSSIZE : Size;
459 
460 		Size -= Cnt;
461 
462 		if (!ctptr) {
463 			ctptr = (uint32_t *) mlast->virt;
464 		} else
465 			Cnt -= 16;	/* subtract length of CT header */
466 
467 		/* Loop through entire NameServer list of DIDs */
468 		while (Cnt >= sizeof (uint32_t)) {
469 			/* Get next DID from NameServer List */
470 			CTentry = *ctptr++;
471 			Did = ((be32_to_cpu(CTentry)) & Mask_DID);
472 
473 			ndlp = NULL;
474 
475 			/*
476 			 * Check for rscn processing or not
477 			 * To conserve rpi's, filter out addresses for other
478 			 * vports on the same physical HBAs.
479 			 */
480 			if ((Did != vport->fc_myDID) &&
481 			    ((lpfc_find_vport_by_did(phba, Did) == NULL) ||
482 			     vport->cfg_peer_port_login)) {
483 				if ((vport->port_type != LPFC_NPIV_PORT) ||
484 				    (!(vport->ct_flags & FC_CT_RFF_ID)) ||
485 				    (!vport->cfg_restrict_login)) {
486 					ndlp = lpfc_setup_disc_node(vport, Did);
487 					if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
488 						lpfc_debugfs_disc_trc(vport,
489 						LPFC_DISC_TRC_CT,
490 						"Parse GID_FTrsp: "
491 						"did:x%x flg:x%x x%x",
492 						Did, ndlp->nlp_flag,
493 						vport->fc_flag);
494 
495 						lpfc_printf_vlog(vport,
496 							KERN_INFO,
497 							LOG_DISCOVERY,
498 							"0238 Process "
499 							"x%x NameServer Rsp"
500 							"Data: x%x x%x x%x\n",
501 							Did, ndlp->nlp_flag,
502 							vport->fc_flag,
503 							vport->fc_rscn_id_cnt);
504 					} else {
505 						lpfc_debugfs_disc_trc(vport,
506 						LPFC_DISC_TRC_CT,
507 						"Skip1 GID_FTrsp: "
508 						"did:x%x flg:x%x cnt:%d",
509 						Did, vport->fc_flag,
510 						vport->fc_rscn_id_cnt);
511 
512 						lpfc_printf_vlog(vport,
513 							KERN_INFO,
514 							LOG_DISCOVERY,
515 							"0239 Skip x%x "
516 							"NameServer Rsp Data: "
517 							"x%x x%x\n",
518 							Did, vport->fc_flag,
519 							vport->fc_rscn_id_cnt);
520 					}
521 
522 				} else {
523 					if (!(vport->fc_flag & FC_RSCN_MODE) ||
524 					(lpfc_rscn_payload_check(vport, Did))) {
525 						lpfc_debugfs_disc_trc(vport,
526 						LPFC_DISC_TRC_CT,
527 						"Query GID_FTrsp: "
528 						"did:x%x flg:x%x cnt:%d",
529 						Did, vport->fc_flag,
530 						vport->fc_rscn_id_cnt);
531 
532 						/* This NPortID was previously
533 						 * a FCP target, * Don't even
534 						 * bother to send GFF_ID.
535 						 */
536 						ndlp = lpfc_findnode_did(vport,
537 							Did);
538 						if (ndlp &&
539 						    NLP_CHK_NODE_ACT(ndlp)
540 						    && (ndlp->nlp_type &
541 						     NLP_FCP_TARGET))
542 							lpfc_setup_disc_node
543 								(vport, Did);
544 						else if (lpfc_ns_cmd(vport,
545 							SLI_CTNS_GFF_ID,
546 							0, Did) == 0)
547 							vport->num_disc_nodes++;
548 						else
549 							lpfc_setup_disc_node
550 								(vport, Did);
551 					}
552 					else {
553 						lpfc_debugfs_disc_trc(vport,
554 						LPFC_DISC_TRC_CT,
555 						"Skip2 GID_FTrsp: "
556 						"did:x%x flg:x%x cnt:%d",
557 						Did, vport->fc_flag,
558 						vport->fc_rscn_id_cnt);
559 
560 						lpfc_printf_vlog(vport,
561 							KERN_INFO,
562 							LOG_DISCOVERY,
563 							"0245 Skip x%x "
564 							"NameServer Rsp Data: "
565 							"x%x x%x\n",
566 							Did, vport->fc_flag,
567 							vport->fc_rscn_id_cnt);
568 					}
569 				}
570 			}
571 			if (CTentry & (be32_to_cpu(SLI_CT_LAST_ENTRY)))
572 				goto nsout1;
573 			Cnt -= sizeof (uint32_t);
574 		}
575 		ctptr = NULL;
576 
577 	}
578 
579 nsout1:
580 	list_del(&head);
581 	return 0;
582 }
583 
584 static void
585 lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
586 			struct lpfc_iocbq *rspiocb)
587 {
588 	struct lpfc_vport *vport = cmdiocb->vport;
589 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
590 	IOCB_t *irsp;
591 	struct lpfc_dmabuf *bmp;
592 	struct lpfc_dmabuf *outp;
593 	struct lpfc_sli_ct_request *CTrsp;
594 	struct lpfc_nodelist *ndlp;
595 	int rc;
596 
597 	/* First save ndlp, before we overwrite it */
598 	ndlp = cmdiocb->context_un.ndlp;
599 
600 	/* we pass cmdiocb to state machine which needs rspiocb as well */
601 	cmdiocb->context_un.rsp_iocb = rspiocb;
602 
603 	outp = (struct lpfc_dmabuf *) cmdiocb->context2;
604 	bmp = (struct lpfc_dmabuf *) cmdiocb->context3;
605 	irsp = &rspiocb->iocb;
606 
607 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
608 		 "GID_FT cmpl:     status:x%x/x%x rtry:%d",
609 		irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_ns_retry);
610 
611 	/* Don't bother processing response if vport is being torn down. */
612 	if (vport->load_flag & FC_UNLOADING) {
613 		if (vport->fc_flag & FC_RSCN_MODE)
614 			lpfc_els_flush_rscn(vport);
615 		goto out;
616 	}
617 
618 	if (lpfc_els_chk_latt(vport)) {
619 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
620 				 "0216 Link event during NS query\n");
621 		if (vport->fc_flag & FC_RSCN_MODE)
622 			lpfc_els_flush_rscn(vport);
623 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
624 		goto out;
625 	}
626 	if (lpfc_error_lost_link(irsp)) {
627 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
628 				 "0226 NS query failed due to link event\n");
629 		if (vport->fc_flag & FC_RSCN_MODE)
630 			lpfc_els_flush_rscn(vport);
631 		goto out;
632 	}
633 	if (irsp->ulpStatus) {
634 		/* Check for retry */
635 		if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
636 			if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
637 			    (irsp->un.ulpWord[4] && IOERR_PARAM_MASK) !=
638 			    IOERR_NO_RESOURCES)
639 				vport->fc_ns_retry++;
640 
641 			/* CT command is being retried */
642 			rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
643 					 vport->fc_ns_retry, 0);
644 			if (rc == 0)
645 				goto out;
646 		}
647 		if (vport->fc_flag & FC_RSCN_MODE)
648 			lpfc_els_flush_rscn(vport);
649 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
650 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
651 				 "0257 GID_FT Query error: 0x%x 0x%x\n",
652 				 irsp->ulpStatus, vport->fc_ns_retry);
653 	} else {
654 		/* Good status, continue checking */
655 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
656 		if (CTrsp->CommandResponse.bits.CmdRsp ==
657 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
658 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
659 					 "0208 NameServer Rsp Data: x%x\n",
660 					 vport->fc_flag);
661 			lpfc_ns_rsp(vport, outp,
662 				    (uint32_t) (irsp->un.genreq64.bdl.bdeSize));
663 		} else if (CTrsp->CommandResponse.bits.CmdRsp ==
664 			   be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
665 			/* NameServer Rsp Error */
666 			if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
667 			    && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
668 				lpfc_printf_vlog(vport, KERN_INFO,
669 					LOG_DISCOVERY,
670 					"0269 No NameServer Entries "
671 					"Data: x%x x%x x%x x%x\n",
672 					CTrsp->CommandResponse.bits.CmdRsp,
673 					(uint32_t) CTrsp->ReasonCode,
674 					(uint32_t) CTrsp->Explanation,
675 					vport->fc_flag);
676 
677 				lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
678 				"GID_FT no entry  cmd:x%x rsn:x%x exp:x%x",
679 				(uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
680 				(uint32_t) CTrsp->ReasonCode,
681 				(uint32_t) CTrsp->Explanation);
682 			} else {
683 				lpfc_printf_vlog(vport, KERN_INFO,
684 					LOG_DISCOVERY,
685 					"0240 NameServer Rsp Error "
686 					"Data: x%x x%x x%x x%x\n",
687 					CTrsp->CommandResponse.bits.CmdRsp,
688 					(uint32_t) CTrsp->ReasonCode,
689 					(uint32_t) CTrsp->Explanation,
690 					vport->fc_flag);
691 
692 				lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
693 				"GID_FT rsp err1  cmd:x%x rsn:x%x exp:x%x",
694 				(uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
695 				(uint32_t) CTrsp->ReasonCode,
696 				(uint32_t) CTrsp->Explanation);
697 			}
698 
699 
700 		} else {
701 			/* NameServer Rsp Error */
702 			lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
703 					"0241 NameServer Rsp Error "
704 					"Data: x%x x%x x%x x%x\n",
705 					CTrsp->CommandResponse.bits.CmdRsp,
706 					(uint32_t) CTrsp->ReasonCode,
707 					(uint32_t) CTrsp->Explanation,
708 					vport->fc_flag);
709 
710 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
711 				"GID_FT rsp err2  cmd:x%x rsn:x%x exp:x%x",
712 				(uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
713 				(uint32_t) CTrsp->ReasonCode,
714 				(uint32_t) CTrsp->Explanation);
715 		}
716 	}
717 	/* Link up / RSCN discovery */
718 	if (vport->num_disc_nodes == 0) {
719 		/*
720 		 * The driver has cycled through all Nports in the RSCN payload.
721 		 * Complete the handling by cleaning up and marking the
722 		 * current driver state.
723 		 */
724 		if (vport->port_state >= LPFC_DISC_AUTH) {
725 			if (vport->fc_flag & FC_RSCN_MODE) {
726 				lpfc_els_flush_rscn(vport);
727 				spin_lock_irq(shost->host_lock);
728 				vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
729 				spin_unlock_irq(shost->host_lock);
730 			}
731 			else
732 				lpfc_els_flush_rscn(vport);
733 		}
734 
735 		lpfc_disc_start(vport);
736 	}
737 out:
738 	cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
739 	lpfc_ct_free_iocb(phba, cmdiocb);
740 	return;
741 }
742 
743 static void
744 lpfc_cmpl_ct_cmd_gff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
745 			struct lpfc_iocbq *rspiocb)
746 {
747 	struct lpfc_vport *vport = cmdiocb->vport;
748 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
749 	IOCB_t *irsp = &rspiocb->iocb;
750 	struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *) cmdiocb->context1;
751 	struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *) cmdiocb->context2;
752 	struct lpfc_sli_ct_request *CTrsp;
753 	int did, rc, retry;
754 	uint8_t fbits;
755 	struct lpfc_nodelist *ndlp;
756 
757 	did = ((struct lpfc_sli_ct_request *) inp->virt)->un.gff.PortId;
758 	did = be32_to_cpu(did);
759 
760 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
761 		"GFF_ID cmpl:     status:x%x/x%x did:x%x",
762 		irsp->ulpStatus, irsp->un.ulpWord[4], did);
763 
764 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
765 		/* Good status, continue checking */
766 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
767 		fbits = CTrsp->un.gff_acc.fbits[FCP_TYPE_FEATURE_OFFSET];
768 
769 		if (CTrsp->CommandResponse.bits.CmdRsp ==
770 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
771 			if ((fbits & FC4_FEATURE_INIT) &&
772 			    !(fbits & FC4_FEATURE_TARGET)) {
773 				lpfc_printf_vlog(vport, KERN_INFO,
774 						 LOG_DISCOVERY,
775 						 "0270 Skip x%x GFF "
776 						 "NameServer Rsp Data: (init) "
777 						 "x%x x%x\n", did, fbits,
778 						 vport->fc_rscn_id_cnt);
779 				goto out;
780 			}
781 		}
782 	}
783 	else {
784 		/* Check for retry */
785 		if (cmdiocb->retry < LPFC_MAX_NS_RETRY) {
786 			retry = 1;
787 			if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
788 				switch ((irsp->un.ulpWord[4] &
789 					IOERR_PARAM_MASK)) {
790 
791 				case IOERR_NO_RESOURCES:
792 					/* We don't increment the retry
793 					 * count for this case.
794 					 */
795 					break;
796 				case IOERR_LINK_DOWN:
797 				case IOERR_SLI_ABORTED:
798 				case IOERR_SLI_DOWN:
799 					retry = 0;
800 					break;
801 				default:
802 					cmdiocb->retry++;
803 				}
804 			}
805 			else
806 				cmdiocb->retry++;
807 
808 			if (retry) {
809 				/* CT command is being retried */
810 				rc = lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
811 					 cmdiocb->retry, did);
812 				if (rc == 0) {
813 					/* success */
814 					lpfc_ct_free_iocb(phba, cmdiocb);
815 					return;
816 				}
817 			}
818 		}
819 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
820 				 "0267 NameServer GFF Rsp "
821 				 "x%x Error (%d %d) Data: x%x x%x\n",
822 				 did, irsp->ulpStatus, irsp->un.ulpWord[4],
823 				 vport->fc_flag, vport->fc_rscn_id_cnt);
824 	}
825 
826 	/* This is a target port, unregistered port, or the GFF_ID failed */
827 	ndlp = lpfc_setup_disc_node(vport, did);
828 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
829 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
830 				 "0242 Process x%x GFF "
831 				 "NameServer Rsp Data: x%x x%x x%x\n",
832 				 did, ndlp->nlp_flag, vport->fc_flag,
833 				 vport->fc_rscn_id_cnt);
834 	} else {
835 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
836 				 "0243 Skip x%x GFF "
837 				 "NameServer Rsp Data: x%x x%x\n", did,
838 				 vport->fc_flag, vport->fc_rscn_id_cnt);
839 	}
840 out:
841 	/* Link up / RSCN discovery */
842 	if (vport->num_disc_nodes)
843 		vport->num_disc_nodes--;
844 	if (vport->num_disc_nodes == 0) {
845 		/*
846 		 * The driver has cycled through all Nports in the RSCN payload.
847 		 * Complete the handling by cleaning up and marking the
848 		 * current driver state.
849 		 */
850 		if (vport->port_state >= LPFC_DISC_AUTH) {
851 			if (vport->fc_flag & FC_RSCN_MODE) {
852 				lpfc_els_flush_rscn(vport);
853 				spin_lock_irq(shost->host_lock);
854 				vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
855 				spin_unlock_irq(shost->host_lock);
856 			}
857 			else
858 				lpfc_els_flush_rscn(vport);
859 		}
860 		lpfc_disc_start(vport);
861 	}
862 	lpfc_ct_free_iocb(phba, cmdiocb);
863 	return;
864 }
865 
866 
867 static void
868 lpfc_cmpl_ct(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
869 	     struct lpfc_iocbq *rspiocb)
870 {
871 	struct lpfc_vport *vport = cmdiocb->vport;
872 	struct lpfc_dmabuf *inp;
873 	struct lpfc_dmabuf *outp;
874 	IOCB_t *irsp;
875 	struct lpfc_sli_ct_request *CTrsp;
876 	struct lpfc_nodelist *ndlp;
877 	int cmdcode, rc;
878 	uint8_t retry;
879 	uint32_t latt;
880 
881 	/* First save ndlp, before we overwrite it */
882 	ndlp = cmdiocb->context_un.ndlp;
883 
884 	/* we pass cmdiocb to state machine which needs rspiocb as well */
885 	cmdiocb->context_un.rsp_iocb = rspiocb;
886 
887 	inp = (struct lpfc_dmabuf *) cmdiocb->context1;
888 	outp = (struct lpfc_dmabuf *) cmdiocb->context2;
889 	irsp = &rspiocb->iocb;
890 
891 	cmdcode = be16_to_cpu(((struct lpfc_sli_ct_request *) inp->virt)->
892 					CommandResponse.bits.CmdRsp);
893 	CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
894 
895 	latt = lpfc_els_chk_latt(vport);
896 
897 	/* RFT request completes status <ulpStatus> CmdRsp <CmdRsp> */
898 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
899 			 "0209 CT Request completes, latt %d, "
900 			 "ulpStatus x%x CmdRsp x%x, Context x%x, Tag x%x\n",
901 			 latt, irsp->ulpStatus,
902 			 CTrsp->CommandResponse.bits.CmdRsp,
903 			 cmdiocb->iocb.ulpContext, cmdiocb->iocb.ulpIoTag);
904 
905 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
906 		"CT cmd cmpl:     status:x%x/x%x cmd:x%x",
907 		irsp->ulpStatus, irsp->un.ulpWord[4], cmdcode);
908 
909 	if (irsp->ulpStatus) {
910 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
911 				 "0268 NS cmd %x Error (%d %d)\n",
912 				 cmdcode, irsp->ulpStatus, irsp->un.ulpWord[4]);
913 
914 		if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
915 			(((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
916 			  IOERR_SLI_DOWN) ||
917 			 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
918 			  IOERR_SLI_ABORTED)))
919 			goto out;
920 
921 		retry = cmdiocb->retry;
922 		if (retry >= LPFC_MAX_NS_RETRY)
923 			goto out;
924 
925 		retry++;
926 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
927 				 "0250 Retrying NS cmd %x\n", cmdcode);
928 		rc = lpfc_ns_cmd(vport, cmdcode, retry, 0);
929 		if (rc == 0)
930 			goto out;
931 	}
932 
933 out:
934 	cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
935 	lpfc_ct_free_iocb(phba, cmdiocb);
936 	return;
937 }
938 
939 static void
940 lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
941 			struct lpfc_iocbq *rspiocb)
942 {
943 	IOCB_t *irsp = &rspiocb->iocb;
944 	struct lpfc_vport *vport = cmdiocb->vport;
945 
946 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
947 		struct lpfc_dmabuf *outp;
948 		struct lpfc_sli_ct_request *CTrsp;
949 
950 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
951 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
952 		if (CTrsp->CommandResponse.bits.CmdRsp ==
953 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
954 			vport->ct_flags |= FC_CT_RFT_ID;
955 	}
956 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
957 	return;
958 }
959 
960 static void
961 lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
962 			struct lpfc_iocbq *rspiocb)
963 {
964 	IOCB_t *irsp = &rspiocb->iocb;
965 	struct lpfc_vport *vport = cmdiocb->vport;
966 
967 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
968 		struct lpfc_dmabuf *outp;
969 		struct lpfc_sli_ct_request *CTrsp;
970 
971 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
972 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
973 		if (CTrsp->CommandResponse.bits.CmdRsp ==
974 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
975 			vport->ct_flags |= FC_CT_RNN_ID;
976 	}
977 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
978 	return;
979 }
980 
981 static void
982 lpfc_cmpl_ct_cmd_rspn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
983 			 struct lpfc_iocbq *rspiocb)
984 {
985 	IOCB_t *irsp = &rspiocb->iocb;
986 	struct lpfc_vport *vport = cmdiocb->vport;
987 
988 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
989 		struct lpfc_dmabuf *outp;
990 		struct lpfc_sli_ct_request *CTrsp;
991 
992 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
993 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
994 		if (CTrsp->CommandResponse.bits.CmdRsp ==
995 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
996 			vport->ct_flags |= FC_CT_RSPN_ID;
997 	}
998 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
999 	return;
1000 }
1001 
1002 static void
1003 lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1004 			 struct lpfc_iocbq *rspiocb)
1005 {
1006 	IOCB_t *irsp = &rspiocb->iocb;
1007 	struct lpfc_vport *vport = cmdiocb->vport;
1008 
1009 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1010 		struct lpfc_dmabuf *outp;
1011 		struct lpfc_sli_ct_request *CTrsp;
1012 
1013 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1014 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1015 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1016 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1017 			vport->ct_flags |= FC_CT_RSNN_NN;
1018 	}
1019 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1020 	return;
1021 }
1022 
1023 static void
1024 lpfc_cmpl_ct_cmd_da_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1025  struct lpfc_iocbq *rspiocb)
1026 {
1027 	struct lpfc_vport *vport = cmdiocb->vport;
1028 
1029 	/* even if it fails we will act as though it succeeded. */
1030 	vport->ct_flags = 0;
1031 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1032 	return;
1033 }
1034 
1035 static void
1036 lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1037 			struct lpfc_iocbq *rspiocb)
1038 {
1039 	IOCB_t *irsp = &rspiocb->iocb;
1040 	struct lpfc_vport *vport = cmdiocb->vport;
1041 
1042 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1043 		struct lpfc_dmabuf *outp;
1044 		struct lpfc_sli_ct_request *CTrsp;
1045 
1046 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1047 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1048 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1049 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1050 			vport->ct_flags |= FC_CT_RFF_ID;
1051 	}
1052 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1053 	return;
1054 }
1055 
1056 int
1057 lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol,
1058 	size_t size)
1059 {
1060 	int n;
1061 	uint8_t *wwn = vport->phba->wwpn;
1062 
1063 	n = snprintf(symbol, size,
1064 		     "Emulex PPN-%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1065 		     wwn[0], wwn[1], wwn[2], wwn[3],
1066 		     wwn[4], wwn[5], wwn[6], wwn[7]);
1067 
1068 	if (vport->port_type == LPFC_PHYSICAL_PORT)
1069 		return n;
1070 
1071 	if (n < size)
1072 		n += snprintf(symbol + n, size - n, " VPort-%d", vport->vpi);
1073 
1074 	if (n < size &&
1075 	    strlen(vport->fc_vport->symbolic_name))
1076 		n += snprintf(symbol + n, size - n, " VName-%s",
1077 			      vport->fc_vport->symbolic_name);
1078 	return n;
1079 }
1080 
1081 int
1082 lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol,
1083 	size_t size)
1084 {
1085 	char fwrev[FW_REV_STR_SIZE];
1086 	int n;
1087 
1088 	lpfc_decode_firmware_rev(vport->phba, fwrev, 0);
1089 
1090 	n = snprintf(symbol, size, "Emulex %s FV%s DV%s",
1091 		vport->phba->ModelName, fwrev, lpfc_release_version);
1092 	return n;
1093 }
1094 
1095 /*
1096  * lpfc_ns_cmd
1097  * Description:
1098  *    Issue Cmd to NameServer
1099  *       SLI_CTNS_GID_FT
1100  *       LI_CTNS_RFT_ID
1101  */
1102 int
1103 lpfc_ns_cmd(struct lpfc_vport *vport, int cmdcode,
1104 	    uint8_t retry, uint32_t context)
1105 {
1106 	struct lpfc_nodelist * ndlp;
1107 	struct lpfc_hba *phba = vport->phba;
1108 	struct lpfc_dmabuf *mp, *bmp;
1109 	struct lpfc_sli_ct_request *CtReq;
1110 	struct ulp_bde64 *bpl;
1111 	void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
1112 		      struct lpfc_iocbq *) = NULL;
1113 	uint32_t rsp_size = 1024;
1114 	size_t   size;
1115 	int rc = 0;
1116 
1117 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
1118 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)
1119 	    || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) {
1120 		rc=1;
1121 		goto ns_cmd_exit;
1122 	}
1123 
1124 	/* fill in BDEs for command */
1125 	/* Allocate buffer for command payload */
1126 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1127 	if (!mp) {
1128 		rc=2;
1129 		goto ns_cmd_exit;
1130 	}
1131 
1132 	INIT_LIST_HEAD(&mp->list);
1133 	mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
1134 	if (!mp->virt) {
1135 		rc=3;
1136 		goto ns_cmd_free_mp;
1137 	}
1138 
1139 	/* Allocate buffer for Buffer ptr list */
1140 	bmp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1141 	if (!bmp) {
1142 		rc=4;
1143 		goto ns_cmd_free_mpvirt;
1144 	}
1145 
1146 	INIT_LIST_HEAD(&bmp->list);
1147 	bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(bmp->phys));
1148 	if (!bmp->virt) {
1149 		rc=5;
1150 		goto ns_cmd_free_bmp;
1151 	}
1152 
1153 	/* NameServer Req */
1154 	lpfc_printf_vlog(vport, KERN_INFO ,LOG_DISCOVERY,
1155 			 "0236 NameServer Req Data: x%x x%x x%x\n",
1156 			 cmdcode, vport->fc_flag, vport->fc_rscn_id_cnt);
1157 
1158 	bpl = (struct ulp_bde64 *) bmp->virt;
1159 	memset(bpl, 0, sizeof(struct ulp_bde64));
1160 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
1161 	bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
1162 	bpl->tus.f.bdeFlags = 0;
1163 	if (cmdcode == SLI_CTNS_GID_FT)
1164 		bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1165 	else if (cmdcode == SLI_CTNS_GFF_ID)
1166 		bpl->tus.f.bdeSize = GFF_REQUEST_SZ;
1167 	else if (cmdcode == SLI_CTNS_RFT_ID)
1168 		bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
1169 	else if (cmdcode == SLI_CTNS_RNN_ID)
1170 		bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
1171 	else if (cmdcode == SLI_CTNS_RSPN_ID)
1172 		bpl->tus.f.bdeSize = RSPN_REQUEST_SZ;
1173 	else if (cmdcode == SLI_CTNS_RSNN_NN)
1174 		bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
1175 	else if (cmdcode == SLI_CTNS_DA_ID)
1176 		bpl->tus.f.bdeSize = DA_ID_REQUEST_SZ;
1177 	else if (cmdcode == SLI_CTNS_RFF_ID)
1178 		bpl->tus.f.bdeSize = RFF_REQUEST_SZ;
1179 	else
1180 		bpl->tus.f.bdeSize = 0;
1181 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
1182 
1183 	CtReq = (struct lpfc_sli_ct_request *) mp->virt;
1184 	memset(CtReq, 0, sizeof (struct lpfc_sli_ct_request));
1185 	CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
1186 	CtReq->RevisionId.bits.InId = 0;
1187 	CtReq->FsType = SLI_CT_DIRECTORY_SERVICE;
1188 	CtReq->FsSubType = SLI_CT_DIRECTORY_NAME_SERVER;
1189 	CtReq->CommandResponse.bits.Size = 0;
1190 	switch (cmdcode) {
1191 	case SLI_CTNS_GID_FT:
1192 		CtReq->CommandResponse.bits.CmdRsp =
1193 		    be16_to_cpu(SLI_CTNS_GID_FT);
1194 		CtReq->un.gid.Fc4Type = SLI_CTPT_FCP;
1195 		if (vport->port_state < LPFC_NS_QRY)
1196 			vport->port_state = LPFC_NS_QRY;
1197 		lpfc_set_disctmo(vport);
1198 		cmpl = lpfc_cmpl_ct_cmd_gid_ft;
1199 		rsp_size = FC_MAX_NS_RSP;
1200 		break;
1201 
1202 	case SLI_CTNS_GFF_ID:
1203 		CtReq->CommandResponse.bits.CmdRsp =
1204 			be16_to_cpu(SLI_CTNS_GFF_ID);
1205 		CtReq->un.gff.PortId = cpu_to_be32(context);
1206 		cmpl = lpfc_cmpl_ct_cmd_gff_id;
1207 		break;
1208 
1209 	case SLI_CTNS_RFT_ID:
1210 		vport->ct_flags &= ~FC_CT_RFT_ID;
1211 		CtReq->CommandResponse.bits.CmdRsp =
1212 		    be16_to_cpu(SLI_CTNS_RFT_ID);
1213 		CtReq->un.rft.PortId = cpu_to_be32(vport->fc_myDID);
1214 		CtReq->un.rft.fcpReg = 1;
1215 		cmpl = lpfc_cmpl_ct_cmd_rft_id;
1216 		break;
1217 
1218 	case SLI_CTNS_RNN_ID:
1219 		vport->ct_flags &= ~FC_CT_RNN_ID;
1220 		CtReq->CommandResponse.bits.CmdRsp =
1221 		    be16_to_cpu(SLI_CTNS_RNN_ID);
1222 		CtReq->un.rnn.PortId = cpu_to_be32(vport->fc_myDID);
1223 		memcpy(CtReq->un.rnn.wwnn,  &vport->fc_nodename,
1224 		       sizeof (struct lpfc_name));
1225 		cmpl = lpfc_cmpl_ct_cmd_rnn_id;
1226 		break;
1227 
1228 	case SLI_CTNS_RSPN_ID:
1229 		vport->ct_flags &= ~FC_CT_RSPN_ID;
1230 		CtReq->CommandResponse.bits.CmdRsp =
1231 		    be16_to_cpu(SLI_CTNS_RSPN_ID);
1232 		CtReq->un.rspn.PortId = cpu_to_be32(vport->fc_myDID);
1233 		size = sizeof(CtReq->un.rspn.symbname);
1234 		CtReq->un.rspn.len =
1235 			lpfc_vport_symbolic_port_name(vport,
1236 			CtReq->un.rspn.symbname, size);
1237 		cmpl = lpfc_cmpl_ct_cmd_rspn_id;
1238 		break;
1239 	case SLI_CTNS_RSNN_NN:
1240 		vport->ct_flags &= ~FC_CT_RSNN_NN;
1241 		CtReq->CommandResponse.bits.CmdRsp =
1242 		    be16_to_cpu(SLI_CTNS_RSNN_NN);
1243 		memcpy(CtReq->un.rsnn.wwnn, &vport->fc_nodename,
1244 		       sizeof (struct lpfc_name));
1245 		size = sizeof(CtReq->un.rsnn.symbname);
1246 		CtReq->un.rsnn.len =
1247 			lpfc_vport_symbolic_node_name(vport,
1248 			CtReq->un.rsnn.symbname, size);
1249 		cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
1250 		break;
1251 	case SLI_CTNS_DA_ID:
1252 		/* Implement DA_ID Nameserver request */
1253 		CtReq->CommandResponse.bits.CmdRsp =
1254 			be16_to_cpu(SLI_CTNS_DA_ID);
1255 		CtReq->un.da_id.port_id = cpu_to_be32(vport->fc_myDID);
1256 		cmpl = lpfc_cmpl_ct_cmd_da_id;
1257 		break;
1258 	case SLI_CTNS_RFF_ID:
1259 		vport->ct_flags &= ~FC_CT_RFF_ID;
1260 		CtReq->CommandResponse.bits.CmdRsp =
1261 		    be16_to_cpu(SLI_CTNS_RFF_ID);
1262 		CtReq->un.rff.PortId = cpu_to_be32(vport->fc_myDID);
1263 		CtReq->un.rff.fbits = FC4_FEATURE_INIT;
1264 		CtReq->un.rff.type_code = FC_TYPE_FCP;
1265 		cmpl = lpfc_cmpl_ct_cmd_rff_id;
1266 		break;
1267 	}
1268 	/* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
1269 	 * to hold ndlp reference for the corresponding callback function.
1270 	 */
1271 	if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry)) {
1272 		/* On success, The cmpl function will free the buffers */
1273 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1274 			"Issue CT cmd:    cmd:x%x did:x%x",
1275 			cmdcode, ndlp->nlp_DID, 0);
1276 		return 0;
1277 	}
1278 	rc=6;
1279 
1280 	/* Decrement ndlp reference count to release ndlp reference held
1281 	 * for the failed command's callback function.
1282 	 */
1283 	lpfc_nlp_put(ndlp);
1284 
1285 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1286 ns_cmd_free_bmp:
1287 	kfree(bmp);
1288 ns_cmd_free_mpvirt:
1289 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
1290 ns_cmd_free_mp:
1291 	kfree(mp);
1292 ns_cmd_exit:
1293 	lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1294 			 "0266 Issue NameServer Req x%x err %d Data: x%x x%x\n",
1295 			 cmdcode, rc, vport->fc_flag, vport->fc_rscn_id_cnt);
1296 	return 1;
1297 }
1298 
1299 static void
1300 lpfc_cmpl_ct_cmd_fdmi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1301 		      struct lpfc_iocbq * rspiocb)
1302 {
1303 	struct lpfc_dmabuf *inp = cmdiocb->context1;
1304 	struct lpfc_dmabuf *outp = cmdiocb->context2;
1305 	struct lpfc_sli_ct_request *CTrsp = outp->virt;
1306 	struct lpfc_sli_ct_request *CTcmd = inp->virt;
1307 	struct lpfc_nodelist *ndlp;
1308 	uint16_t fdmi_cmd = CTcmd->CommandResponse.bits.CmdRsp;
1309 	uint16_t fdmi_rsp = CTrsp->CommandResponse.bits.CmdRsp;
1310 	struct lpfc_vport *vport = cmdiocb->vport;
1311 	IOCB_t *irsp = &rspiocb->iocb;
1312 	uint32_t latt;
1313 
1314 	latt = lpfc_els_chk_latt(vport);
1315 
1316 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1317 		"FDMI cmpl:       status:x%x/x%x latt:%d",
1318 		irsp->ulpStatus, irsp->un.ulpWord[4], latt);
1319 
1320 	if (latt || irsp->ulpStatus) {
1321 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1322 				 "0229 FDMI cmd %04x failed, latt = %d "
1323 				 "ulpStatus: x%x, rid x%x\n",
1324 				 be16_to_cpu(fdmi_cmd), latt, irsp->ulpStatus,
1325 				 irsp->un.ulpWord[4]);
1326 		lpfc_ct_free_iocb(phba, cmdiocb);
1327 		return;
1328 	}
1329 
1330 	ndlp = lpfc_findnode_did(vport, FDMI_DID);
1331 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
1332 		goto fail_out;
1333 
1334 	if (fdmi_rsp == be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
1335 		/* FDMI rsp failed */
1336 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1337 				 "0220 FDMI rsp failed Data: x%x\n",
1338 				 be16_to_cpu(fdmi_cmd));
1339 	}
1340 
1341 	switch (be16_to_cpu(fdmi_cmd)) {
1342 	case SLI_MGMT_RHBA:
1343 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA);
1344 		break;
1345 
1346 	case SLI_MGMT_RPA:
1347 		break;
1348 
1349 	case SLI_MGMT_DHBA:
1350 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT);
1351 		break;
1352 
1353 	case SLI_MGMT_DPRT:
1354 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RHBA);
1355 		break;
1356 	}
1357 
1358 fail_out:
1359 	lpfc_ct_free_iocb(phba, cmdiocb);
1360 	return;
1361 }
1362 
1363 int
1364 lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp, int cmdcode)
1365 {
1366 	struct lpfc_hba *phba = vport->phba;
1367 	struct lpfc_dmabuf *mp, *bmp;
1368 	struct lpfc_sli_ct_request *CtReq;
1369 	struct ulp_bde64 *bpl;
1370 	uint32_t size;
1371 	REG_HBA *rh;
1372 	PORT_ENTRY *pe;
1373 	REG_PORT_ATTRIBUTE *pab;
1374 	ATTRIBUTE_BLOCK *ab;
1375 	ATTRIBUTE_ENTRY *ae;
1376 	void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
1377 		      struct lpfc_iocbq *);
1378 
1379 
1380 	/* fill in BDEs for command */
1381 	/* Allocate buffer for command payload */
1382 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1383 	if (!mp)
1384 		goto fdmi_cmd_exit;
1385 
1386 	mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
1387 	if (!mp->virt)
1388 		goto fdmi_cmd_free_mp;
1389 
1390 	/* Allocate buffer for Buffer ptr list */
1391 	bmp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1392 	if (!bmp)
1393 		goto fdmi_cmd_free_mpvirt;
1394 
1395 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &(bmp->phys));
1396 	if (!bmp->virt)
1397 		goto fdmi_cmd_free_bmp;
1398 
1399 	INIT_LIST_HEAD(&mp->list);
1400 	INIT_LIST_HEAD(&bmp->list);
1401 
1402 	/* FDMI request */
1403 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1404 			 "0218 FDMI Request Data: x%x x%x x%x\n",
1405 			 vport->fc_flag, vport->port_state, cmdcode);
1406 	CtReq = (struct lpfc_sli_ct_request *) mp->virt;
1407 
1408 	memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
1409 	CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
1410 	CtReq->RevisionId.bits.InId = 0;
1411 
1412 	CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
1413 	CtReq->FsSubType = SLI_CT_FDMI_Subtypes;
1414 	size = 0;
1415 
1416 	switch (cmdcode) {
1417 	case SLI_MGMT_RHBA:
1418 		{
1419 			lpfc_vpd_t *vp = &phba->vpd;
1420 			uint32_t i, j, incr;
1421 			int len;
1422 
1423 			CtReq->CommandResponse.bits.CmdRsp =
1424 			    be16_to_cpu(SLI_MGMT_RHBA);
1425 			CtReq->CommandResponse.bits.Size = 0;
1426 			rh = (REG_HBA *) & CtReq->un.PortID;
1427 			memcpy(&rh->hi.PortName, &vport->fc_sparam.portName,
1428 			       sizeof (struct lpfc_name));
1429 			/* One entry (port) per adapter */
1430 			rh->rpl.EntryCnt = be32_to_cpu(1);
1431 			memcpy(&rh->rpl.pe, &vport->fc_sparam.portName,
1432 			       sizeof (struct lpfc_name));
1433 
1434 			/* point to the HBA attribute block */
1435 			size = 2 * sizeof (struct lpfc_name) + FOURBYTES;
1436 			ab = (ATTRIBUTE_BLOCK *) ((uint8_t *) rh + size);
1437 			ab->EntryCnt = 0;
1438 
1439 			/* Point to the beginning of the first HBA attribute
1440 			   entry */
1441 			/* #1 HBA attribute entry */
1442 			size += FOURBYTES;
1443 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1444 			ae->ad.bits.AttrType = be16_to_cpu(NODE_NAME);
1445 			ae->ad.bits.AttrLen =  be16_to_cpu(FOURBYTES
1446 						+ sizeof (struct lpfc_name));
1447 			memcpy(&ae->un.NodeName, &vport->fc_sparam.nodeName,
1448 			       sizeof (struct lpfc_name));
1449 			ab->EntryCnt++;
1450 			size += FOURBYTES + sizeof (struct lpfc_name);
1451 
1452 			/* #2 HBA attribute entry */
1453 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1454 			ae->ad.bits.AttrType = be16_to_cpu(MANUFACTURER);
1455 			strcpy(ae->un.Manufacturer, "Emulex Corporation");
1456 			len = strlen(ae->un.Manufacturer);
1457 			len += (len & 3) ? (4 - (len & 3)) : 4;
1458 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1459 			ab->EntryCnt++;
1460 			size += FOURBYTES + len;
1461 
1462 			/* #3 HBA attribute entry */
1463 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1464 			ae->ad.bits.AttrType = be16_to_cpu(SERIAL_NUMBER);
1465 			strcpy(ae->un.SerialNumber, phba->SerialNumber);
1466 			len = strlen(ae->un.SerialNumber);
1467 			len += (len & 3) ? (4 - (len & 3)) : 4;
1468 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1469 			ab->EntryCnt++;
1470 			size += FOURBYTES + len;
1471 
1472 			/* #4 HBA attribute entry */
1473 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1474 			ae->ad.bits.AttrType = be16_to_cpu(MODEL);
1475 			strcpy(ae->un.Model, phba->ModelName);
1476 			len = strlen(ae->un.Model);
1477 			len += (len & 3) ? (4 - (len & 3)) : 4;
1478 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1479 			ab->EntryCnt++;
1480 			size += FOURBYTES + len;
1481 
1482 			/* #5 HBA attribute entry */
1483 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1484 			ae->ad.bits.AttrType = be16_to_cpu(MODEL_DESCRIPTION);
1485 			strcpy(ae->un.ModelDescription, phba->ModelDesc);
1486 			len = strlen(ae->un.ModelDescription);
1487 			len += (len & 3) ? (4 - (len & 3)) : 4;
1488 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1489 			ab->EntryCnt++;
1490 			size += FOURBYTES + len;
1491 
1492 			/* #6 HBA attribute entry */
1493 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1494 			ae->ad.bits.AttrType = be16_to_cpu(HARDWARE_VERSION);
1495 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 8);
1496 			/* Convert JEDEC ID to ascii for hardware version */
1497 			incr = vp->rev.biuRev;
1498 			for (i = 0; i < 8; i++) {
1499 				j = (incr & 0xf);
1500 				if (j <= 9)
1501 					ae->un.HardwareVersion[7 - i] =
1502 					    (char)((uint8_t) 0x30 +
1503 						   (uint8_t) j);
1504 				else
1505 					ae->un.HardwareVersion[7 - i] =
1506 					    (char)((uint8_t) 0x61 +
1507 						   (uint8_t) (j - 10));
1508 				incr = (incr >> 4);
1509 			}
1510 			ab->EntryCnt++;
1511 			size += FOURBYTES + 8;
1512 
1513 			/* #7 HBA attribute entry */
1514 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1515 			ae->ad.bits.AttrType = be16_to_cpu(DRIVER_VERSION);
1516 			strcpy(ae->un.DriverVersion, lpfc_release_version);
1517 			len = strlen(ae->un.DriverVersion);
1518 			len += (len & 3) ? (4 - (len & 3)) : 4;
1519 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1520 			ab->EntryCnt++;
1521 			size += FOURBYTES + len;
1522 
1523 			/* #8 HBA attribute entry */
1524 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1525 			ae->ad.bits.AttrType = be16_to_cpu(OPTION_ROM_VERSION);
1526 			strcpy(ae->un.OptionROMVersion, phba->OptionROMVersion);
1527 			len = strlen(ae->un.OptionROMVersion);
1528 			len += (len & 3) ? (4 - (len & 3)) : 4;
1529 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1530 			ab->EntryCnt++;
1531 			size += FOURBYTES + len;
1532 
1533 			/* #9 HBA attribute entry */
1534 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1535 			ae->ad.bits.AttrType = be16_to_cpu(FIRMWARE_VERSION);
1536 			lpfc_decode_firmware_rev(phba, ae->un.FirmwareVersion,
1537 				1);
1538 			len = strlen(ae->un.FirmwareVersion);
1539 			len += (len & 3) ? (4 - (len & 3)) : 4;
1540 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1541 			ab->EntryCnt++;
1542 			size += FOURBYTES + len;
1543 
1544 			/* #10 HBA attribute entry */
1545 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1546 			ae->ad.bits.AttrType = be16_to_cpu(OS_NAME_VERSION);
1547 			sprintf(ae->un.OsNameVersion, "%s %s %s",
1548 				init_utsname()->sysname,
1549 				init_utsname()->release,
1550 				init_utsname()->version);
1551 			len = strlen(ae->un.OsNameVersion);
1552 			len += (len & 3) ? (4 - (len & 3)) : 4;
1553 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1554 			ab->EntryCnt++;
1555 			size += FOURBYTES + len;
1556 
1557 			/* #11 HBA attribute entry */
1558 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1559 			ae->ad.bits.AttrType = be16_to_cpu(MAX_CT_PAYLOAD_LEN);
1560 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1561 			ae->un.MaxCTPayloadLen = (65 * 4096);
1562 			ab->EntryCnt++;
1563 			size += FOURBYTES + 4;
1564 
1565 			ab->EntryCnt = be32_to_cpu(ab->EntryCnt);
1566 			/* Total size */
1567 			size = GID_REQUEST_SZ - 4 + size;
1568 		}
1569 		break;
1570 
1571 	case SLI_MGMT_RPA:
1572 		{
1573 			lpfc_vpd_t *vp;
1574 			struct serv_parm *hsp;
1575 			int len;
1576 
1577 			vp = &phba->vpd;
1578 
1579 			CtReq->CommandResponse.bits.CmdRsp =
1580 			    be16_to_cpu(SLI_MGMT_RPA);
1581 			CtReq->CommandResponse.bits.Size = 0;
1582 			pab = (REG_PORT_ATTRIBUTE *) & CtReq->un.PortID;
1583 			size = sizeof (struct lpfc_name) + FOURBYTES;
1584 			memcpy((uint8_t *) & pab->PortName,
1585 			       (uint8_t *) & vport->fc_sparam.portName,
1586 			       sizeof (struct lpfc_name));
1587 			pab->ab.EntryCnt = 0;
1588 
1589 			/* #1 Port attribute entry */
1590 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1591 			ae->ad.bits.AttrType = be16_to_cpu(SUPPORTED_FC4_TYPES);
1592 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 32);
1593 			ae->un.SupportFC4Types[2] = 1;
1594 			ae->un.SupportFC4Types[7] = 1;
1595 			pab->ab.EntryCnt++;
1596 			size += FOURBYTES + 32;
1597 
1598 			/* #2 Port attribute entry */
1599 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1600 			ae->ad.bits.AttrType = be16_to_cpu(SUPPORTED_SPEED);
1601 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1602 
1603 			ae->un.SupportSpeed = 0;
1604 			if (phba->lmt & LMT_16Gb)
1605 				ae->un.SupportSpeed |= HBA_PORTSPEED_16GBIT;
1606 			if (phba->lmt & LMT_10Gb)
1607 				ae->un.SupportSpeed |= HBA_PORTSPEED_10GBIT;
1608 			if (phba->lmt & LMT_8Gb)
1609 				ae->un.SupportSpeed |= HBA_PORTSPEED_8GBIT;
1610 			if (phba->lmt & LMT_4Gb)
1611 				ae->un.SupportSpeed |= HBA_PORTSPEED_4GBIT;
1612 			if (phba->lmt & LMT_2Gb)
1613 				ae->un.SupportSpeed |= HBA_PORTSPEED_2GBIT;
1614 			if (phba->lmt & LMT_1Gb)
1615 				ae->un.SupportSpeed |= HBA_PORTSPEED_1GBIT;
1616 
1617 			pab->ab.EntryCnt++;
1618 			size += FOURBYTES + 4;
1619 
1620 			/* #3 Port attribute entry */
1621 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1622 			ae->ad.bits.AttrType = be16_to_cpu(PORT_SPEED);
1623 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1624 			switch(phba->fc_linkspeed) {
1625 			case LPFC_LINK_SPEED_1GHZ:
1626 				ae->un.PortSpeed = HBA_PORTSPEED_1GBIT;
1627 				break;
1628 			case LPFC_LINK_SPEED_2GHZ:
1629 				ae->un.PortSpeed = HBA_PORTSPEED_2GBIT;
1630 				break;
1631 			case LPFC_LINK_SPEED_4GHZ:
1632 				ae->un.PortSpeed = HBA_PORTSPEED_4GBIT;
1633 				break;
1634 			case LPFC_LINK_SPEED_8GHZ:
1635 				ae->un.PortSpeed = HBA_PORTSPEED_8GBIT;
1636 				break;
1637 			case LPFC_LINK_SPEED_10GHZ:
1638 				ae->un.PortSpeed = HBA_PORTSPEED_10GBIT;
1639 				break;
1640 			case LPFC_LINK_SPEED_16GHZ:
1641 				ae->un.PortSpeed = HBA_PORTSPEED_16GBIT;
1642 				break;
1643 			default:
1644 				ae->un.PortSpeed = HBA_PORTSPEED_UNKNOWN;
1645 				break;
1646 			}
1647 			pab->ab.EntryCnt++;
1648 			size += FOURBYTES + 4;
1649 
1650 			/* #4 Port attribute entry */
1651 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1652 			ae->ad.bits.AttrType = be16_to_cpu(MAX_FRAME_SIZE);
1653 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1654 			hsp = (struct serv_parm *) & vport->fc_sparam;
1655 			ae->un.MaxFrameSize =
1656 			    (((uint32_t) hsp->cmn.
1657 			      bbRcvSizeMsb) << 8) | (uint32_t) hsp->cmn.
1658 			    bbRcvSizeLsb;
1659 			pab->ab.EntryCnt++;
1660 			size += FOURBYTES + 4;
1661 
1662 			/* #5 Port attribute entry */
1663 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1664 			ae->ad.bits.AttrType = be16_to_cpu(OS_DEVICE_NAME);
1665 			strcpy((char *)ae->un.OsDeviceName, LPFC_DRIVER_NAME);
1666 			len = strlen((char *)ae->un.OsDeviceName);
1667 			len += (len & 3) ? (4 - (len & 3)) : 4;
1668 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1669 			pab->ab.EntryCnt++;
1670 			size += FOURBYTES + len;
1671 
1672 			if (vport->cfg_fdmi_on == 2) {
1673 				/* #6 Port attribute entry */
1674 				ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab +
1675 							  size);
1676 				ae->ad.bits.AttrType = be16_to_cpu(HOST_NAME);
1677 				sprintf(ae->un.HostName, "%s",
1678 					init_utsname()->nodename);
1679 				len = strlen(ae->un.HostName);
1680 				len += (len & 3) ? (4 - (len & 3)) : 4;
1681 				ae->ad.bits.AttrLen =
1682 				    be16_to_cpu(FOURBYTES + len);
1683 				pab->ab.EntryCnt++;
1684 				size += FOURBYTES + len;
1685 			}
1686 
1687 			pab->ab.EntryCnt = be32_to_cpu(pab->ab.EntryCnt);
1688 			/* Total size */
1689 			size = GID_REQUEST_SZ - 4 + size;
1690 		}
1691 		break;
1692 
1693 	case SLI_MGMT_DHBA:
1694 		CtReq->CommandResponse.bits.CmdRsp = be16_to_cpu(SLI_MGMT_DHBA);
1695 		CtReq->CommandResponse.bits.Size = 0;
1696 		pe = (PORT_ENTRY *) & CtReq->un.PortID;
1697 		memcpy((uint8_t *) & pe->PortName,
1698 		       (uint8_t *) & vport->fc_sparam.portName,
1699 		       sizeof (struct lpfc_name));
1700 		size = GID_REQUEST_SZ - 4 + sizeof (struct lpfc_name);
1701 		break;
1702 
1703 	case SLI_MGMT_DPRT:
1704 		CtReq->CommandResponse.bits.CmdRsp = be16_to_cpu(SLI_MGMT_DPRT);
1705 		CtReq->CommandResponse.bits.Size = 0;
1706 		pe = (PORT_ENTRY *) & CtReq->un.PortID;
1707 		memcpy((uint8_t *) & pe->PortName,
1708 		       (uint8_t *) & vport->fc_sparam.portName,
1709 		       sizeof (struct lpfc_name));
1710 		size = GID_REQUEST_SZ - 4 + sizeof (struct lpfc_name);
1711 		break;
1712 	}
1713 
1714 	bpl = (struct ulp_bde64 *) bmp->virt;
1715 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
1716 	bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
1717 	bpl->tus.f.bdeFlags = 0;
1718 	bpl->tus.f.bdeSize = size;
1719 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
1720 
1721 	cmpl = lpfc_cmpl_ct_cmd_fdmi;
1722 
1723 	/* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
1724 	 * to hold ndlp reference for the corresponding callback function.
1725 	 */
1726 	if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, FC_MAX_NS_RSP, 0))
1727 		return 0;
1728 
1729 	/* Decrement ndlp reference count to release ndlp reference held
1730 	 * for the failed command's callback function.
1731 	 */
1732 	lpfc_nlp_put(ndlp);
1733 
1734 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1735 fdmi_cmd_free_bmp:
1736 	kfree(bmp);
1737 fdmi_cmd_free_mpvirt:
1738 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
1739 fdmi_cmd_free_mp:
1740 	kfree(mp);
1741 fdmi_cmd_exit:
1742 	/* Issue FDMI request failed */
1743 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1744 			 "0244 Issue FDMI request failed Data: x%x\n",
1745 			 cmdcode);
1746 	return 1;
1747 }
1748 
1749 /**
1750  * lpfc_delayed_disc_tmo - Timeout handler for delayed discovery timer.
1751  * @ptr - Context object of the timer.
1752  *
1753  * This function set the WORKER_DELAYED_DISC_TMO flag and wake up
1754  * the worker thread.
1755  **/
1756 void
1757 lpfc_delayed_disc_tmo(unsigned long ptr)
1758 {
1759 	struct lpfc_vport *vport = (struct lpfc_vport *)ptr;
1760 	struct lpfc_hba   *phba = vport->phba;
1761 	uint32_t tmo_posted;
1762 	unsigned long iflag;
1763 
1764 	spin_lock_irqsave(&vport->work_port_lock, iflag);
1765 	tmo_posted = vport->work_port_events & WORKER_DELAYED_DISC_TMO;
1766 	if (!tmo_posted)
1767 		vport->work_port_events |= WORKER_DELAYED_DISC_TMO;
1768 	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
1769 
1770 	if (!tmo_posted)
1771 		lpfc_worker_wake_up(phba);
1772 	return;
1773 }
1774 
1775 /**
1776  * lpfc_delayed_disc_timeout_handler - Function called by worker thread to
1777  *      handle delayed discovery.
1778  * @vport: pointer to a host virtual N_Port data structure.
1779  *
1780  * This function start nport discovery of the vport.
1781  **/
1782 void
1783 lpfc_delayed_disc_timeout_handler(struct lpfc_vport *vport)
1784 {
1785 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1786 
1787 	spin_lock_irq(shost->host_lock);
1788 	if (!(vport->fc_flag & FC_DISC_DELAYED)) {
1789 		spin_unlock_irq(shost->host_lock);
1790 		return;
1791 	}
1792 	vport->fc_flag &= ~FC_DISC_DELAYED;
1793 	spin_unlock_irq(shost->host_lock);
1794 
1795 	lpfc_do_scr_ns_plogi(vport->phba, vport);
1796 }
1797 
1798 void
1799 lpfc_fdmi_tmo(unsigned long ptr)
1800 {
1801 	struct lpfc_vport *vport = (struct lpfc_vport *)ptr;
1802 	struct lpfc_hba   *phba = vport->phba;
1803 	uint32_t tmo_posted;
1804 	unsigned long iflag;
1805 
1806 	spin_lock_irqsave(&vport->work_port_lock, iflag);
1807 	tmo_posted = vport->work_port_events & WORKER_FDMI_TMO;
1808 	if (!tmo_posted)
1809 		vport->work_port_events |= WORKER_FDMI_TMO;
1810 	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
1811 
1812 	if (!tmo_posted)
1813 		lpfc_worker_wake_up(phba);
1814 	return;
1815 }
1816 
1817 void
1818 lpfc_fdmi_timeout_handler(struct lpfc_vport *vport)
1819 {
1820 	struct lpfc_nodelist *ndlp;
1821 
1822 	ndlp = lpfc_findnode_did(vport, FDMI_DID);
1823 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
1824 		if (init_utsname()->nodename[0] != '\0')
1825 			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
1826 		else
1827 			mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
1828 	}
1829 	return;
1830 }
1831 
1832 void
1833 lpfc_decode_firmware_rev(struct lpfc_hba *phba, char *fwrevision, int flag)
1834 {
1835 	struct lpfc_sli *psli = &phba->sli;
1836 	lpfc_vpd_t *vp = &phba->vpd;
1837 	uint32_t b1, b2, b3, b4, i, rev;
1838 	char c;
1839 	uint32_t *ptr, str[4];
1840 	uint8_t *fwname;
1841 
1842 	if (phba->sli_rev == LPFC_SLI_REV4)
1843 		snprintf(fwrevision, FW_REV_STR_SIZE, "%s", vp->rev.opFwName);
1844 	else if (vp->rev.rBit) {
1845 		if (psli->sli_flag & LPFC_SLI_ACTIVE)
1846 			rev = vp->rev.sli2FwRev;
1847 		else
1848 			rev = vp->rev.sli1FwRev;
1849 
1850 		b1 = (rev & 0x0000f000) >> 12;
1851 		b2 = (rev & 0x00000f00) >> 8;
1852 		b3 = (rev & 0x000000c0) >> 6;
1853 		b4 = (rev & 0x00000030) >> 4;
1854 
1855 		switch (b4) {
1856 		case 0:
1857 			c = 'N';
1858 			break;
1859 		case 1:
1860 			c = 'A';
1861 			break;
1862 		case 2:
1863 			c = 'B';
1864 			break;
1865 		case 3:
1866 			c = 'X';
1867 			break;
1868 		default:
1869 			c = 0;
1870 			break;
1871 		}
1872 		b4 = (rev & 0x0000000f);
1873 
1874 		if (psli->sli_flag & LPFC_SLI_ACTIVE)
1875 			fwname = vp->rev.sli2FwName;
1876 		else
1877 			fwname = vp->rev.sli1FwName;
1878 
1879 		for (i = 0; i < 16; i++)
1880 			if (fwname[i] == 0x20)
1881 				fwname[i] = 0;
1882 
1883 		ptr = (uint32_t*)fwname;
1884 
1885 		for (i = 0; i < 3; i++)
1886 			str[i] = be32_to_cpu(*ptr++);
1887 
1888 		if (c == 0) {
1889 			if (flag)
1890 				sprintf(fwrevision, "%d.%d%d (%s)",
1891 					b1, b2, b3, (char *)str);
1892 			else
1893 				sprintf(fwrevision, "%d.%d%d", b1,
1894 					b2, b3);
1895 		} else {
1896 			if (flag)
1897 				sprintf(fwrevision, "%d.%d%d%c%d (%s)",
1898 					b1, b2, b3, c,
1899 					b4, (char *)str);
1900 			else
1901 				sprintf(fwrevision, "%d.%d%d%c%d",
1902 					b1, b2, b3, c, b4);
1903 		}
1904 	} else {
1905 		rev = vp->rev.smFwRev;
1906 
1907 		b1 = (rev & 0xff000000) >> 24;
1908 		b2 = (rev & 0x00f00000) >> 20;
1909 		b3 = (rev & 0x000f0000) >> 16;
1910 		c  = (rev & 0x0000ff00) >> 8;
1911 		b4 = (rev & 0x000000ff);
1912 
1913 		sprintf(fwrevision, "%d.%d%d%c%d", b1, b2, b3, c, b4);
1914 	}
1915 	return;
1916 }
1917