xref: /linux/drivers/scsi/lpfc/lpfc_ct.c (revision f24e9f586b377749dff37554696cf3a105540c94)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2006 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
23  */
24 
25 #include <linux/blkdev.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 #include <linux/utsname.h>
29 
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_transport_fc.h>
34 
35 #include "lpfc_hw.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_version.h"
43 
44 #define HBA_PORTSPEED_UNKNOWN               0	/* Unknown - transceiver
45 						 * incapable of reporting */
46 #define HBA_PORTSPEED_1GBIT                 1	/* 1 GBit/sec */
47 #define HBA_PORTSPEED_2GBIT                 2	/* 2 GBit/sec */
48 #define HBA_PORTSPEED_4GBIT                 8   /* 4 GBit/sec */
49 #define HBA_PORTSPEED_8GBIT                16   /* 8 GBit/sec */
50 #define HBA_PORTSPEED_10GBIT                4	/* 10 GBit/sec */
51 #define HBA_PORTSPEED_NOT_NEGOTIATED        5	/* Speed not established */
52 
53 #define FOURBYTES	4
54 
55 
56 static char *lpfc_release_version = LPFC_DRIVER_VERSION;
57 
58 /*
59  * lpfc_ct_unsol_event
60  */
61 void
62 lpfc_ct_unsol_event(struct lpfc_hba * phba,
63 		    struct lpfc_sli_ring * pring, struct lpfc_iocbq * piocbq)
64 {
65 
66 	struct lpfc_iocbq *next_piocbq;
67 	struct lpfc_dmabuf *pmbuf = NULL;
68 	struct lpfc_dmabuf *matp, *next_matp;
69 	uint32_t ctx = 0, size = 0, cnt = 0;
70 	IOCB_t *icmd = &piocbq->iocb;
71 	IOCB_t *save_icmd = icmd;
72 	int i, go_exit = 0;
73 	struct list_head head;
74 
75 	if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
76 		((icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING)) {
77 		/* Not enough posted buffers; Try posting more buffers */
78 		phba->fc_stat.NoRcvBuf++;
79 		lpfc_post_buffer(phba, pring, 0, 1);
80 		return;
81 	}
82 
83 	/* If there are no BDEs associated with this IOCB,
84 	 * there is nothing to do.
85 	 */
86 	if (icmd->ulpBdeCount == 0)
87 		return;
88 
89 	INIT_LIST_HEAD(&head);
90 	list_add_tail(&head, &piocbq->list);
91 
92 	list_for_each_entry_safe(piocbq, next_piocbq, &head, list) {
93 		icmd = &piocbq->iocb;
94 		if (ctx == 0)
95 			ctx = (uint32_t) (icmd->ulpContext);
96 		if (icmd->ulpBdeCount == 0)
97 			continue;
98 
99 		for (i = 0; i < icmd->ulpBdeCount; i++) {
100 			matp = lpfc_sli_ringpostbuf_get(phba, pring,
101 							getPaddr(icmd->un.
102 								 cont64[i].
103 								 addrHigh,
104 								 icmd->un.
105 								 cont64[i].
106 								 addrLow));
107 			if (!matp) {
108 				/* Insert lpfc log message here */
109 				lpfc_post_buffer(phba, pring, cnt, 1);
110 				go_exit = 1;
111 				goto ct_unsol_event_exit_piocbq;
112 			}
113 
114 			/* Typically for Unsolicited CT requests */
115 			if (!pmbuf) {
116 				pmbuf = matp;
117 				INIT_LIST_HEAD(&pmbuf->list);
118 			} else
119 				list_add_tail(&matp->list, &pmbuf->list);
120 
121 			size += icmd->un.cont64[i].tus.f.bdeSize;
122 			cnt++;
123 		}
124 
125 		icmd->ulpBdeCount = 0;
126 	}
127 
128 	lpfc_post_buffer(phba, pring, cnt, 1);
129 	if (save_icmd->ulpStatus) {
130 		go_exit = 1;
131 	}
132 
133 ct_unsol_event_exit_piocbq:
134 	list_del(&head);
135 	if (pmbuf) {
136 		list_for_each_entry_safe(matp, next_matp, &pmbuf->list, list) {
137 			lpfc_mbuf_free(phba, matp->virt, matp->phys);
138 			list_del(&matp->list);
139 			kfree(matp);
140 		}
141 		lpfc_mbuf_free(phba, pmbuf->virt, pmbuf->phys);
142 		kfree(pmbuf);
143 	}
144 	return;
145 }
146 
147 static void
148 lpfc_free_ct_rsp(struct lpfc_hba * phba, struct lpfc_dmabuf * mlist)
149 {
150 	struct lpfc_dmabuf *mlast, *next_mlast;
151 
152 	list_for_each_entry_safe(mlast, next_mlast, &mlist->list, list) {
153 		lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
154 		list_del(&mlast->list);
155 		kfree(mlast);
156 	}
157 	lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
158 	kfree(mlist);
159 	return;
160 }
161 
162 static struct lpfc_dmabuf *
163 lpfc_alloc_ct_rsp(struct lpfc_hba * phba, int cmdcode, struct ulp_bde64 * bpl,
164 		  uint32_t size, int *entries)
165 {
166 	struct lpfc_dmabuf *mlist = NULL;
167 	struct lpfc_dmabuf *mp;
168 	int cnt, i = 0;
169 
170 	/* We get chucks of FCELSSIZE */
171 	cnt = size > FCELSSIZE ? FCELSSIZE: size;
172 
173 	while (size) {
174 		/* Allocate buffer for rsp payload */
175 		mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
176 		if (!mp) {
177 			if (mlist)
178 				lpfc_free_ct_rsp(phba, mlist);
179 			return NULL;
180 		}
181 
182 		INIT_LIST_HEAD(&mp->list);
183 
184 		if (cmdcode == be16_to_cpu(SLI_CTNS_GID_FT))
185 			mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
186 		else
187 			mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
188 
189 		if (!mp->virt) {
190 			kfree(mp);
191 			lpfc_free_ct_rsp(phba, mlist);
192 			return NULL;
193 		}
194 
195 		/* Queue it to a linked list */
196 		if (!mlist)
197 			mlist = mp;
198 		else
199 			list_add_tail(&mp->list, &mlist->list);
200 
201 		bpl->tus.f.bdeFlags = BUFF_USE_RCV;
202 		/* build buffer ptr list for IOCB */
203 		bpl->addrLow = le32_to_cpu( putPaddrLow(mp->phys) );
204 		bpl->addrHigh = le32_to_cpu( putPaddrHigh(mp->phys) );
205 		bpl->tus.f.bdeSize = (uint16_t) cnt;
206 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
207 		bpl++;
208 
209 		i++;
210 		size -= cnt;
211 	}
212 
213 	*entries = i;
214 	return mlist;
215 }
216 
217 static int
218 lpfc_gen_req(struct lpfc_hba *phba, struct lpfc_dmabuf *bmp,
219 	     struct lpfc_dmabuf *inp, struct lpfc_dmabuf *outp,
220 	     void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
221 		     struct lpfc_iocbq *),
222 	     struct lpfc_nodelist *ndlp, uint32_t usr_flg, uint32_t num_entry,
223 	     uint32_t tmo)
224 {
225 
226 	struct lpfc_sli *psli = &phba->sli;
227 	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
228 	IOCB_t *icmd;
229 	struct lpfc_iocbq *geniocb;
230 
231 	/* Allocate buffer for  command iocb */
232 	spin_lock_irq(phba->host->host_lock);
233 	geniocb = lpfc_sli_get_iocbq(phba);
234 	spin_unlock_irq(phba->host->host_lock);
235 
236 	if (geniocb == NULL)
237 		return 1;
238 
239 	icmd = &geniocb->iocb;
240 	icmd->un.genreq64.bdl.ulpIoTag32 = 0;
241 	icmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
242 	icmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
243 	icmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BDL;
244 	icmd->un.genreq64.bdl.bdeSize = (num_entry * sizeof (struct ulp_bde64));
245 
246 	if (usr_flg)
247 		geniocb->context3 = NULL;
248 	else
249 		geniocb->context3 = (uint8_t *) bmp;
250 
251 	/* Save for completion so we can release these resources */
252 	geniocb->context1 = (uint8_t *) inp;
253 	geniocb->context2 = (uint8_t *) outp;
254 
255 	/* Fill in payload, bp points to frame payload */
256 	icmd->ulpCommand = CMD_GEN_REQUEST64_CR;
257 
258 	/* Fill in rest of iocb */
259 	icmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
260 	icmd->un.genreq64.w5.hcsw.Dfctl = 0;
261 	icmd->un.genreq64.w5.hcsw.Rctl = FC_UNSOL_CTL;
262 	icmd->un.genreq64.w5.hcsw.Type = FC_COMMON_TRANSPORT_ULP;
263 
264 	if (!tmo) {
265 		 /* FC spec states we need 3 * ratov for CT requests */
266 		tmo = (3 * phba->fc_ratov);
267 	}
268 	icmd->ulpTimeout = tmo;
269 	icmd->ulpBdeCount = 1;
270 	icmd->ulpLe = 1;
271 	icmd->ulpClass = CLASS3;
272 	icmd->ulpContext = ndlp->nlp_rpi;
273 
274 	/* Issue GEN REQ IOCB for NPORT <did> */
275 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
276 			"%d:0119 Issue GEN REQ IOCB for NPORT x%x "
277 			"Data: x%x x%x\n", phba->brd_no, icmd->un.ulpWord[5],
278 			icmd->ulpIoTag, phba->hba_state);
279 	geniocb->iocb_cmpl = cmpl;
280 	geniocb->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
281 	spin_lock_irq(phba->host->host_lock);
282 	if (lpfc_sli_issue_iocb(phba, pring, geniocb, 0) == IOCB_ERROR) {
283 		lpfc_sli_release_iocbq(phba, geniocb);
284 		spin_unlock_irq(phba->host->host_lock);
285 		return 1;
286 	}
287 	spin_unlock_irq(phba->host->host_lock);
288 
289 	return 0;
290 }
291 
292 static int
293 lpfc_ct_cmd(struct lpfc_hba *phba, struct lpfc_dmabuf *inmp,
294 	    struct lpfc_dmabuf *bmp, struct lpfc_nodelist *ndlp,
295 	    void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
296 			  struct lpfc_iocbq *),
297 	    uint32_t rsp_size)
298 {
299 	struct ulp_bde64 *bpl = (struct ulp_bde64 *) bmp->virt;
300 	struct lpfc_dmabuf *outmp;
301 	int cnt = 0, status;
302 	int cmdcode = ((struct lpfc_sli_ct_request *) inmp->virt)->
303 		CommandResponse.bits.CmdRsp;
304 
305 	bpl++;			/* Skip past ct request */
306 
307 	/* Put buffer(s) for ct rsp in bpl */
308 	outmp = lpfc_alloc_ct_rsp(phba, cmdcode, bpl, rsp_size, &cnt);
309 	if (!outmp)
310 		return -ENOMEM;
311 
312 	status = lpfc_gen_req(phba, bmp, inmp, outmp, cmpl, ndlp, 0,
313 			      cnt+1, 0);
314 	if (status) {
315 		lpfc_free_ct_rsp(phba, outmp);
316 		return -ENOMEM;
317 	}
318 	return 0;
319 }
320 
321 static int
322 lpfc_ns_rsp(struct lpfc_hba * phba, struct lpfc_dmabuf * mp, uint32_t Size)
323 {
324 	struct lpfc_sli_ct_request *Response =
325 		(struct lpfc_sli_ct_request *) mp->virt;
326 	struct lpfc_nodelist *ndlp = NULL;
327 	struct lpfc_nodelist *next_ndlp;
328 	struct lpfc_dmabuf *mlast, *next_mp;
329 	uint32_t *ctptr = (uint32_t *) & Response->un.gid.PortType;
330 	uint32_t Did;
331 	uint32_t CTentry;
332 	int Cnt;
333 	struct list_head head;
334 
335 	lpfc_set_disctmo(phba);
336 
337 	Cnt = Size  > FCELSSIZE ? FCELSSIZE : Size;
338 
339 	list_add_tail(&head, &mp->list);
340 	list_for_each_entry_safe(mp, next_mp, &head, list) {
341 		mlast = mp;
342 
343 		Size -= Cnt;
344 
345 		if (!ctptr)
346 			ctptr = (uint32_t *) mlast->virt;
347 		else
348 			Cnt -= 16;	/* subtract length of CT header */
349 
350 		/* Loop through entire NameServer list of DIDs */
351 		while (Cnt) {
352 
353 			/* Get next DID from NameServer List */
354 			CTentry = *ctptr++;
355 			Did = ((be32_to_cpu(CTentry)) & Mask_DID);
356 
357 			ndlp = NULL;
358 			if (Did != phba->fc_myDID) {
359 				/* Check for rscn processing or not */
360 				ndlp = lpfc_setup_disc_node(phba, Did);
361 			}
362 			/* Mark all node table entries that are in the
363 			   Nameserver */
364 			if (ndlp) {
365 				/* NameServer Rsp */
366 				lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
367 						"%d:0238 Process x%x NameServer"
368 						" Rsp Data: x%x x%x x%x\n",
369 						phba->brd_no,
370 						Did, ndlp->nlp_flag,
371 						phba->fc_flag,
372 						phba->fc_rscn_id_cnt);
373 			} else {
374 				/* NameServer Rsp */
375 				lpfc_printf_log(phba,
376 						KERN_INFO,
377 						LOG_DISCOVERY,
378 						"%d:0239 Skip x%x NameServer "
379 						"Rsp Data: x%x x%x x%x\n",
380 						phba->brd_no,
381 						Did, Size, phba->fc_flag,
382 						phba->fc_rscn_id_cnt);
383 			}
384 
385 			if (CTentry & (be32_to_cpu(SLI_CT_LAST_ENTRY)))
386 				goto nsout1;
387 			Cnt -= sizeof (uint32_t);
388 		}
389 		ctptr = NULL;
390 
391 	}
392 
393 nsout1:
394 	list_del(&head);
395 
396 	/*
397  	 * The driver has cycled through all Nports in the RSCN payload.
398  	 * Complete the handling by cleaning up and marking the
399  	 * current driver state.
400  	 */
401 	if (phba->hba_state == LPFC_HBA_READY) {
402 
403 		/*
404 		 * Switch ports that connect a loop of multiple targets need
405 		 * special consideration.  The driver wants to unregister the
406 		 * rpi only on the target that was pulled from the loop.  On
407 		 * RSCN, the driver wants to rediscover an NPort only if the
408 		 * driver flagged it as NLP_NPR_2B_DISC.  Provided adisc is
409 		 * not enabled and the NPort is not capable of retransmissions
410 		 * (FC Tape) prevent timing races with the scsi error handler by
411 		 * unregistering the Nport's RPI.  This action causes all
412 		 * outstanding IO to flush back to the midlayer.
413 		 */
414 		list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
415 					 nlp_listp) {
416 			if (!(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
417 			    (lpfc_rscn_payload_check(phba, ndlp->nlp_DID))) {
418 				if ((phba->cfg_use_adisc == 0) &&
419 				    !(ndlp->nlp_fcp_info &
420 				      NLP_FCP_2_DEVICE)) {
421 					lpfc_unreg_rpi(phba, ndlp);
422 					ndlp->nlp_flag &= ~NLP_NPR_ADISC;
423 				}
424 			}
425 		}
426 		lpfc_els_flush_rscn(phba);
427 		spin_lock_irq(phba->host->host_lock);
428 		phba->fc_flag |= FC_RSCN_MODE; /* we are still in RSCN mode */
429 		spin_unlock_irq(phba->host->host_lock);
430 	}
431 	return 0;
432 }
433 
434 
435 
436 
437 static void
438 lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
439 			struct lpfc_iocbq * rspiocb)
440 {
441 	IOCB_t *irsp;
442 	struct lpfc_sli *psli;
443 	struct lpfc_dmabuf *bmp;
444 	struct lpfc_dmabuf *inp;
445 	struct lpfc_dmabuf *outp;
446 	struct lpfc_nodelist *ndlp;
447 	struct lpfc_sli_ct_request *CTrsp;
448 
449 	psli = &phba->sli;
450 	/* we pass cmdiocb to state machine which needs rspiocb as well */
451 	cmdiocb->context_un.rsp_iocb = rspiocb;
452 
453 	inp = (struct lpfc_dmabuf *) cmdiocb->context1;
454 	outp = (struct lpfc_dmabuf *) cmdiocb->context2;
455 	bmp = (struct lpfc_dmabuf *) cmdiocb->context3;
456 
457 	irsp = &rspiocb->iocb;
458 	if (irsp->ulpStatus) {
459 		if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
460 			((irsp->un.ulpWord[4] == IOERR_SLI_DOWN) ||
461 			 (irsp->un.ulpWord[4] == IOERR_SLI_ABORTED))) {
462 			goto out;
463 		}
464 
465 		/* Check for retry */
466 		if (phba->fc_ns_retry < LPFC_MAX_NS_RETRY) {
467 			phba->fc_ns_retry++;
468 			/* CT command is being retried */
469 			ndlp =
470 			    lpfc_findnode_did(phba, NLP_SEARCH_UNMAPPED,
471 					      NameServer_DID);
472 			if (ndlp) {
473 				if (lpfc_ns_cmd(phba, ndlp, SLI_CTNS_GID_FT) ==
474 				    0) {
475 					goto out;
476 				}
477 			}
478 		}
479 	} else {
480 		/* Good status, continue checking */
481 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
482 		if (CTrsp->CommandResponse.bits.CmdRsp ==
483 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
484 			lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
485 					"%d:0208 NameServer Rsp "
486 					"Data: x%x\n",
487 					phba->brd_no,
488 					phba->fc_flag);
489 			lpfc_ns_rsp(phba, outp,
490 				    (uint32_t) (irsp->un.genreq64.bdl.bdeSize));
491 		} else if (CTrsp->CommandResponse.bits.CmdRsp ==
492 			   be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
493 			/* NameServer Rsp Error */
494 			lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
495 					"%d:0240 NameServer Rsp Error "
496 					"Data: x%x x%x x%x x%x\n",
497 					phba->brd_no,
498 					CTrsp->CommandResponse.bits.CmdRsp,
499 					(uint32_t) CTrsp->ReasonCode,
500 					(uint32_t) CTrsp->Explanation,
501 					phba->fc_flag);
502 		} else {
503 			/* NameServer Rsp Error */
504 			lpfc_printf_log(phba,
505 					KERN_INFO,
506 					LOG_DISCOVERY,
507 					"%d:0241 NameServer Rsp Error "
508 					"Data: x%x x%x x%x x%x\n",
509 					phba->brd_no,
510 					CTrsp->CommandResponse.bits.CmdRsp,
511 					(uint32_t) CTrsp->ReasonCode,
512 					(uint32_t) CTrsp->Explanation,
513 					phba->fc_flag);
514 		}
515 	}
516 	/* Link up / RSCN discovery */
517 	lpfc_disc_start(phba);
518 out:
519 	lpfc_free_ct_rsp(phba, outp);
520 	lpfc_mbuf_free(phba, inp->virt, inp->phys);
521 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
522 	kfree(inp);
523 	kfree(bmp);
524 	spin_lock_irq(phba->host->host_lock);
525 	lpfc_sli_release_iocbq(phba, cmdiocb);
526 	spin_unlock_irq(phba->host->host_lock);
527 	return;
528 }
529 
530 static void
531 lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
532 			struct lpfc_iocbq * rspiocb)
533 {
534 	struct lpfc_sli *psli;
535 	struct lpfc_dmabuf *bmp;
536 	struct lpfc_dmabuf *inp;
537 	struct lpfc_dmabuf *outp;
538 	IOCB_t *irsp;
539 	struct lpfc_sli_ct_request *CTrsp;
540 
541 	psli = &phba->sli;
542 	/* we pass cmdiocb to state machine which needs rspiocb as well */
543 	cmdiocb->context_un.rsp_iocb = rspiocb;
544 
545 	inp = (struct lpfc_dmabuf *) cmdiocb->context1;
546 	outp = (struct lpfc_dmabuf *) cmdiocb->context2;
547 	bmp = (struct lpfc_dmabuf *) cmdiocb->context3;
548 	irsp = &rspiocb->iocb;
549 
550 	CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
551 
552 	/* RFT request completes status <ulpStatus> CmdRsp <CmdRsp> */
553 	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
554 			"%d:0209 RFT request completes ulpStatus x%x "
555 			"CmdRsp x%x\n", phba->brd_no, irsp->ulpStatus,
556 			CTrsp->CommandResponse.bits.CmdRsp);
557 
558 	lpfc_free_ct_rsp(phba, outp);
559 	lpfc_mbuf_free(phba, inp->virt, inp->phys);
560 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
561 	kfree(inp);
562 	kfree(bmp);
563 	spin_lock_irq(phba->host->host_lock);
564 	lpfc_sli_release_iocbq(phba, cmdiocb);
565 	spin_unlock_irq(phba->host->host_lock);
566 	return;
567 }
568 
569 static void
570 lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
571 			struct lpfc_iocbq * rspiocb)
572 {
573 	lpfc_cmpl_ct_cmd_rft_id(phba, cmdiocb, rspiocb);
574 	return;
575 }
576 
577 static void
578 lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
579 			 struct lpfc_iocbq * rspiocb)
580 {
581 	lpfc_cmpl_ct_cmd_rft_id(phba, cmdiocb, rspiocb);
582 	return;
583 }
584 
585 void
586 lpfc_get_hba_sym_node_name(struct lpfc_hba * phba, uint8_t * symbp)
587 {
588 	char fwrev[16];
589 
590 	lpfc_decode_firmware_rev(phba, fwrev, 0);
591 
592 	sprintf(symbp, "Emulex %s FV%s DV%s", phba->ModelName,
593 		fwrev, lpfc_release_version);
594 	return;
595 }
596 
597 /*
598  * lpfc_ns_cmd
599  * Description:
600  *    Issue Cmd to NameServer
601  *       SLI_CTNS_GID_FT
602  *       LI_CTNS_RFT_ID
603  */
604 int
605 lpfc_ns_cmd(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp, int cmdcode)
606 {
607 	struct lpfc_dmabuf *mp, *bmp;
608 	struct lpfc_sli_ct_request *CtReq;
609 	struct ulp_bde64 *bpl;
610 	void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
611 		      struct lpfc_iocbq *) = NULL;
612 	uint32_t rsp_size = 1024;
613 
614 	/* fill in BDEs for command */
615 	/* Allocate buffer for command payload */
616 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
617 	if (!mp)
618 		goto ns_cmd_exit;
619 
620 	INIT_LIST_HEAD(&mp->list);
621 	mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
622 	if (!mp->virt)
623 		goto ns_cmd_free_mp;
624 
625 	/* Allocate buffer for Buffer ptr list */
626 	bmp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
627 	if (!bmp)
628 		goto ns_cmd_free_mpvirt;
629 
630 	INIT_LIST_HEAD(&bmp->list);
631 	bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(bmp->phys));
632 	if (!bmp->virt)
633 		goto ns_cmd_free_bmp;
634 
635 	/* NameServer Req */
636 	lpfc_printf_log(phba,
637 			KERN_INFO,
638 			LOG_DISCOVERY,
639 			"%d:0236 NameServer Req Data: x%x x%x x%x\n",
640 			phba->brd_no, cmdcode, phba->fc_flag,
641 			phba->fc_rscn_id_cnt);
642 
643 	bpl = (struct ulp_bde64 *) bmp->virt;
644 	memset(bpl, 0, sizeof(struct ulp_bde64));
645 	bpl->addrHigh = le32_to_cpu( putPaddrHigh(mp->phys) );
646 	bpl->addrLow = le32_to_cpu( putPaddrLow(mp->phys) );
647 	bpl->tus.f.bdeFlags = 0;
648 	if (cmdcode == SLI_CTNS_GID_FT)
649 		bpl->tus.f.bdeSize = GID_REQUEST_SZ;
650 	else if (cmdcode == SLI_CTNS_RFT_ID)
651 		bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
652 	else if (cmdcode == SLI_CTNS_RNN_ID)
653 		bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
654 	else if (cmdcode == SLI_CTNS_RSNN_NN)
655 		bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
656 	else
657 		bpl->tus.f.bdeSize = 0;
658 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
659 
660 	CtReq = (struct lpfc_sli_ct_request *) mp->virt;
661 	memset(CtReq, 0, sizeof (struct lpfc_sli_ct_request));
662 	CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
663 	CtReq->RevisionId.bits.InId = 0;
664 	CtReq->FsType = SLI_CT_DIRECTORY_SERVICE;
665 	CtReq->FsSubType = SLI_CT_DIRECTORY_NAME_SERVER;
666 	CtReq->CommandResponse.bits.Size = 0;
667 	switch (cmdcode) {
668 	case SLI_CTNS_GID_FT:
669 		CtReq->CommandResponse.bits.CmdRsp =
670 		    be16_to_cpu(SLI_CTNS_GID_FT);
671 		CtReq->un.gid.Fc4Type = SLI_CTPT_FCP;
672 		if (phba->hba_state < LPFC_HBA_READY)
673 			phba->hba_state = LPFC_NS_QRY;
674 		lpfc_set_disctmo(phba);
675 		cmpl = lpfc_cmpl_ct_cmd_gid_ft;
676 		rsp_size = FC_MAX_NS_RSP;
677 		break;
678 
679 	case SLI_CTNS_RFT_ID:
680 		CtReq->CommandResponse.bits.CmdRsp =
681 		    be16_to_cpu(SLI_CTNS_RFT_ID);
682 		CtReq->un.rft.PortId = be32_to_cpu(phba->fc_myDID);
683 		CtReq->un.rft.fcpReg = 1;
684 		cmpl = lpfc_cmpl_ct_cmd_rft_id;
685 		break;
686 
687 	case SLI_CTNS_RNN_ID:
688 		CtReq->CommandResponse.bits.CmdRsp =
689 		    be16_to_cpu(SLI_CTNS_RNN_ID);
690 		CtReq->un.rnn.PortId = be32_to_cpu(phba->fc_myDID);
691 		memcpy(CtReq->un.rnn.wwnn,  &phba->fc_nodename,
692 		       sizeof (struct lpfc_name));
693 		cmpl = lpfc_cmpl_ct_cmd_rnn_id;
694 		break;
695 
696 	case SLI_CTNS_RSNN_NN:
697 		CtReq->CommandResponse.bits.CmdRsp =
698 		    be16_to_cpu(SLI_CTNS_RSNN_NN);
699 		memcpy(CtReq->un.rsnn.wwnn, &phba->fc_nodename,
700 		       sizeof (struct lpfc_name));
701 		lpfc_get_hba_sym_node_name(phba, CtReq->un.rsnn.symbname);
702 		CtReq->un.rsnn.len = strlen(CtReq->un.rsnn.symbname);
703 		cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
704 		break;
705 	}
706 
707 	if (!lpfc_ct_cmd(phba, mp, bmp, ndlp, cmpl, rsp_size))
708 		/* On success, The cmpl function will free the buffers */
709 		return 0;
710 
711 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
712 ns_cmd_free_bmp:
713 	kfree(bmp);
714 ns_cmd_free_mpvirt:
715 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
716 ns_cmd_free_mp:
717 	kfree(mp);
718 ns_cmd_exit:
719 	return 1;
720 }
721 
722 static void
723 lpfc_cmpl_ct_cmd_fdmi(struct lpfc_hba * phba,
724 		      struct lpfc_iocbq * cmdiocb, struct lpfc_iocbq * rspiocb)
725 {
726 	struct lpfc_dmabuf *bmp = cmdiocb->context3;
727 	struct lpfc_dmabuf *inp = cmdiocb->context1;
728 	struct lpfc_dmabuf *outp = cmdiocb->context2;
729 	struct lpfc_sli_ct_request *CTrsp = outp->virt;
730 	struct lpfc_sli_ct_request *CTcmd = inp->virt;
731 	struct lpfc_nodelist *ndlp;
732 	uint16_t fdmi_cmd = CTcmd->CommandResponse.bits.CmdRsp;
733 	uint16_t fdmi_rsp = CTrsp->CommandResponse.bits.CmdRsp;
734 
735 	ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, FDMI_DID);
736 	if (fdmi_rsp == be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
737 		/* FDMI rsp failed */
738 		lpfc_printf_log(phba,
739 			        KERN_INFO,
740 			        LOG_DISCOVERY,
741 			        "%d:0220 FDMI rsp failed Data: x%x\n",
742 			        phba->brd_no,
743 			       be16_to_cpu(fdmi_cmd));
744 	}
745 
746 	switch (be16_to_cpu(fdmi_cmd)) {
747 	case SLI_MGMT_RHBA:
748 		lpfc_fdmi_cmd(phba, ndlp, SLI_MGMT_RPA);
749 		break;
750 
751 	case SLI_MGMT_RPA:
752 		break;
753 
754 	case SLI_MGMT_DHBA:
755 		lpfc_fdmi_cmd(phba, ndlp, SLI_MGMT_DPRT);
756 		break;
757 
758 	case SLI_MGMT_DPRT:
759 		lpfc_fdmi_cmd(phba, ndlp, SLI_MGMT_RHBA);
760 		break;
761 	}
762 
763 	lpfc_free_ct_rsp(phba, outp);
764 	lpfc_mbuf_free(phba, inp->virt, inp->phys);
765 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
766 	kfree(inp);
767 	kfree(bmp);
768 	spin_lock_irq(phba->host->host_lock);
769 	lpfc_sli_release_iocbq(phba, cmdiocb);
770 	spin_unlock_irq(phba->host->host_lock);
771 	return;
772 }
773 int
774 lpfc_fdmi_cmd(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp, int cmdcode)
775 {
776 	struct lpfc_dmabuf *mp, *bmp;
777 	struct lpfc_sli_ct_request *CtReq;
778 	struct ulp_bde64 *bpl;
779 	uint32_t size;
780 	REG_HBA *rh;
781 	PORT_ENTRY *pe;
782 	REG_PORT_ATTRIBUTE *pab;
783 	ATTRIBUTE_BLOCK *ab;
784 	ATTRIBUTE_ENTRY *ae;
785 	void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
786 		      struct lpfc_iocbq *);
787 
788 
789 	/* fill in BDEs for command */
790 	/* Allocate buffer for command payload */
791 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
792 	if (!mp)
793 		goto fdmi_cmd_exit;
794 
795 	mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
796 	if (!mp->virt)
797 		goto fdmi_cmd_free_mp;
798 
799 	/* Allocate buffer for Buffer ptr list */
800 	bmp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
801 	if (!bmp)
802 		goto fdmi_cmd_free_mpvirt;
803 
804 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &(bmp->phys));
805 	if (!bmp->virt)
806 		goto fdmi_cmd_free_bmp;
807 
808 	INIT_LIST_HEAD(&mp->list);
809 	INIT_LIST_HEAD(&bmp->list);
810 
811 	/* FDMI request */
812 	lpfc_printf_log(phba,
813 		        KERN_INFO,
814 		        LOG_DISCOVERY,
815 		        "%d:0218 FDMI Request Data: x%x x%x x%x\n",
816 		        phba->brd_no,
817 		       phba->fc_flag, phba->hba_state, cmdcode);
818 
819 	CtReq = (struct lpfc_sli_ct_request *) mp->virt;
820 
821 	memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
822 	CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
823 	CtReq->RevisionId.bits.InId = 0;
824 
825 	CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
826 	CtReq->FsSubType = SLI_CT_FDMI_Subtypes;
827 	size = 0;
828 
829 	switch (cmdcode) {
830 	case SLI_MGMT_RHBA:
831 		{
832 			lpfc_vpd_t *vp = &phba->vpd;
833 			uint32_t i, j, incr;
834 			int len;
835 
836 			CtReq->CommandResponse.bits.CmdRsp =
837 			    be16_to_cpu(SLI_MGMT_RHBA);
838 			CtReq->CommandResponse.bits.Size = 0;
839 			rh = (REG_HBA *) & CtReq->un.PortID;
840 			memcpy(&rh->hi.PortName, &phba->fc_sparam.portName,
841 			       sizeof (struct lpfc_name));
842 			/* One entry (port) per adapter */
843 			rh->rpl.EntryCnt = be32_to_cpu(1);
844 			memcpy(&rh->rpl.pe, &phba->fc_sparam.portName,
845 			       sizeof (struct lpfc_name));
846 
847 			/* point to the HBA attribute block */
848 			size = 2 * sizeof (struct lpfc_name) + FOURBYTES;
849 			ab = (ATTRIBUTE_BLOCK *) ((uint8_t *) rh + size);
850 			ab->EntryCnt = 0;
851 
852 			/* Point to the beginning of the first HBA attribute
853 			   entry */
854 			/* #1 HBA attribute entry */
855 			size += FOURBYTES;
856 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
857 			ae->ad.bits.AttrType = be16_to_cpu(NODE_NAME);
858 			ae->ad.bits.AttrLen =  be16_to_cpu(FOURBYTES
859 						+ sizeof (struct lpfc_name));
860 			memcpy(&ae->un.NodeName, &phba->fc_sparam.nodeName,
861 			       sizeof (struct lpfc_name));
862 			ab->EntryCnt++;
863 			size += FOURBYTES + sizeof (struct lpfc_name);
864 
865 			/* #2 HBA attribute entry */
866 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
867 			ae->ad.bits.AttrType = be16_to_cpu(MANUFACTURER);
868 			strcpy(ae->un.Manufacturer, "Emulex Corporation");
869 			len = strlen(ae->un.Manufacturer);
870 			len += (len & 3) ? (4 - (len & 3)) : 4;
871 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
872 			ab->EntryCnt++;
873 			size += FOURBYTES + len;
874 
875 			/* #3 HBA attribute entry */
876 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
877 			ae->ad.bits.AttrType = be16_to_cpu(SERIAL_NUMBER);
878 			strcpy(ae->un.SerialNumber, phba->SerialNumber);
879 			len = strlen(ae->un.SerialNumber);
880 			len += (len & 3) ? (4 - (len & 3)) : 4;
881 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
882 			ab->EntryCnt++;
883 			size += FOURBYTES + len;
884 
885 			/* #4 HBA attribute entry */
886 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
887 			ae->ad.bits.AttrType = be16_to_cpu(MODEL);
888 			strcpy(ae->un.Model, phba->ModelName);
889 			len = strlen(ae->un.Model);
890 			len += (len & 3) ? (4 - (len & 3)) : 4;
891 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
892 			ab->EntryCnt++;
893 			size += FOURBYTES + len;
894 
895 			/* #5 HBA attribute entry */
896 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
897 			ae->ad.bits.AttrType = be16_to_cpu(MODEL_DESCRIPTION);
898 			strcpy(ae->un.ModelDescription, phba->ModelDesc);
899 			len = strlen(ae->un.ModelDescription);
900 			len += (len & 3) ? (4 - (len & 3)) : 4;
901 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
902 			ab->EntryCnt++;
903 			size += FOURBYTES + len;
904 
905 			/* #6 HBA attribute entry */
906 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
907 			ae->ad.bits.AttrType = be16_to_cpu(HARDWARE_VERSION);
908 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 8);
909 			/* Convert JEDEC ID to ascii for hardware version */
910 			incr = vp->rev.biuRev;
911 			for (i = 0; i < 8; i++) {
912 				j = (incr & 0xf);
913 				if (j <= 9)
914 					ae->un.HardwareVersion[7 - i] =
915 					    (char)((uint8_t) 0x30 +
916 						   (uint8_t) j);
917 				else
918 					ae->un.HardwareVersion[7 - i] =
919 					    (char)((uint8_t) 0x61 +
920 						   (uint8_t) (j - 10));
921 				incr = (incr >> 4);
922 			}
923 			ab->EntryCnt++;
924 			size += FOURBYTES + 8;
925 
926 			/* #7 HBA attribute entry */
927 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
928 			ae->ad.bits.AttrType = be16_to_cpu(DRIVER_VERSION);
929 			strcpy(ae->un.DriverVersion, lpfc_release_version);
930 			len = strlen(ae->un.DriverVersion);
931 			len += (len & 3) ? (4 - (len & 3)) : 4;
932 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
933 			ab->EntryCnt++;
934 			size += FOURBYTES + len;
935 
936 			/* #8 HBA attribute entry */
937 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
938 			ae->ad.bits.AttrType = be16_to_cpu(OPTION_ROM_VERSION);
939 			strcpy(ae->un.OptionROMVersion, phba->OptionROMVersion);
940 			len = strlen(ae->un.OptionROMVersion);
941 			len += (len & 3) ? (4 - (len & 3)) : 4;
942 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
943 			ab->EntryCnt++;
944 			size += FOURBYTES + len;
945 
946 			/* #9 HBA attribute entry */
947 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
948 			ae->ad.bits.AttrType = be16_to_cpu(FIRMWARE_VERSION);
949 			lpfc_decode_firmware_rev(phba, ae->un.FirmwareVersion,
950 				1);
951 			len = strlen(ae->un.FirmwareVersion);
952 			len += (len & 3) ? (4 - (len & 3)) : 4;
953 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
954 			ab->EntryCnt++;
955 			size += FOURBYTES + len;
956 
957 			/* #10 HBA attribute entry */
958 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
959 			ae->ad.bits.AttrType = be16_to_cpu(OS_NAME_VERSION);
960 			sprintf(ae->un.OsNameVersion, "%s %s %s",
961 				system_utsname.sysname, system_utsname.release,
962 				system_utsname.version);
963 			len = strlen(ae->un.OsNameVersion);
964 			len += (len & 3) ? (4 - (len & 3)) : 4;
965 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
966 			ab->EntryCnt++;
967 			size += FOURBYTES + len;
968 
969 			/* #11 HBA attribute entry */
970 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
971 			ae->ad.bits.AttrType = be16_to_cpu(MAX_CT_PAYLOAD_LEN);
972 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
973 			ae->un.MaxCTPayloadLen = (65 * 4096);
974 			ab->EntryCnt++;
975 			size += FOURBYTES + 4;
976 
977 			ab->EntryCnt = be32_to_cpu(ab->EntryCnt);
978 			/* Total size */
979 			size = GID_REQUEST_SZ - 4 + size;
980 		}
981 		break;
982 
983 	case SLI_MGMT_RPA:
984 		{
985 			lpfc_vpd_t *vp;
986 			struct serv_parm *hsp;
987 			int len;
988 
989 			vp = &phba->vpd;
990 
991 			CtReq->CommandResponse.bits.CmdRsp =
992 			    be16_to_cpu(SLI_MGMT_RPA);
993 			CtReq->CommandResponse.bits.Size = 0;
994 			pab = (REG_PORT_ATTRIBUTE *) & CtReq->un.PortID;
995 			size = sizeof (struct lpfc_name) + FOURBYTES;
996 			memcpy((uint8_t *) & pab->PortName,
997 			       (uint8_t *) & phba->fc_sparam.portName,
998 			       sizeof (struct lpfc_name));
999 			pab->ab.EntryCnt = 0;
1000 
1001 			/* #1 Port attribute entry */
1002 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1003 			ae->ad.bits.AttrType = be16_to_cpu(SUPPORTED_FC4_TYPES);
1004 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 32);
1005 			ae->un.SupportFC4Types[2] = 1;
1006 			ae->un.SupportFC4Types[7] = 1;
1007 			pab->ab.EntryCnt++;
1008 			size += FOURBYTES + 32;
1009 
1010 			/* #2 Port attribute entry */
1011 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1012 			ae->ad.bits.AttrType = be16_to_cpu(SUPPORTED_SPEED);
1013 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1014 
1015 			ae->un.SupportSpeed = 0;
1016 			if (phba->lmt & LMT_10Gb)
1017 				ae->un.SupportSpeed = HBA_PORTSPEED_10GBIT;
1018 			if (phba->lmt & LMT_8Gb)
1019 				ae->un.SupportSpeed |= HBA_PORTSPEED_8GBIT;
1020 			if (phba->lmt & LMT_4Gb)
1021 				ae->un.SupportSpeed |= HBA_PORTSPEED_4GBIT;
1022 			if (phba->lmt & LMT_2Gb)
1023 				ae->un.SupportSpeed |= HBA_PORTSPEED_2GBIT;
1024 			if (phba->lmt & LMT_1Gb)
1025 				ae->un.SupportSpeed |= HBA_PORTSPEED_1GBIT;
1026 
1027 			pab->ab.EntryCnt++;
1028 			size += FOURBYTES + 4;
1029 
1030 			/* #3 Port attribute entry */
1031 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1032 			ae->ad.bits.AttrType = be16_to_cpu(PORT_SPEED);
1033 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1034 			switch(phba->fc_linkspeed) {
1035 				case LA_1GHZ_LINK:
1036 					ae->un.PortSpeed = HBA_PORTSPEED_1GBIT;
1037 				break;
1038 				case LA_2GHZ_LINK:
1039 					ae->un.PortSpeed = HBA_PORTSPEED_2GBIT;
1040 				break;
1041 				case LA_4GHZ_LINK:
1042 					ae->un.PortSpeed = HBA_PORTSPEED_4GBIT;
1043 				break;
1044 				default:
1045 					ae->un.PortSpeed =
1046 						HBA_PORTSPEED_UNKNOWN;
1047 				break;
1048 			}
1049 			pab->ab.EntryCnt++;
1050 			size += FOURBYTES + 4;
1051 
1052 			/* #4 Port attribute entry */
1053 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1054 			ae->ad.bits.AttrType = be16_to_cpu(MAX_FRAME_SIZE);
1055 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1056 			hsp = (struct serv_parm *) & phba->fc_sparam;
1057 			ae->un.MaxFrameSize =
1058 			    (((uint32_t) hsp->cmn.
1059 			      bbRcvSizeMsb) << 8) | (uint32_t) hsp->cmn.
1060 			    bbRcvSizeLsb;
1061 			pab->ab.EntryCnt++;
1062 			size += FOURBYTES + 4;
1063 
1064 			/* #5 Port attribute entry */
1065 			ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1066 			ae->ad.bits.AttrType = be16_to_cpu(OS_DEVICE_NAME);
1067 			strcpy((char *)ae->un.OsDeviceName, LPFC_DRIVER_NAME);
1068 			len = strlen((char *)ae->un.OsDeviceName);
1069 			len += (len & 3) ? (4 - (len & 3)) : 4;
1070 			ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1071 			pab->ab.EntryCnt++;
1072 			size += FOURBYTES + len;
1073 
1074 			if (phba->cfg_fdmi_on == 2) {
1075 				/* #6 Port attribute entry */
1076 				ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab +
1077 							  size);
1078 				ae->ad.bits.AttrType = be16_to_cpu(HOST_NAME);
1079 				sprintf(ae->un.HostName, "%s",
1080 					system_utsname.nodename);
1081 				len = strlen(ae->un.HostName);
1082 				len += (len & 3) ? (4 - (len & 3)) : 4;
1083 				ae->ad.bits.AttrLen =
1084 				    be16_to_cpu(FOURBYTES + len);
1085 				pab->ab.EntryCnt++;
1086 				size += FOURBYTES + len;
1087 			}
1088 
1089 			pab->ab.EntryCnt = be32_to_cpu(pab->ab.EntryCnt);
1090 			/* Total size */
1091 			size = GID_REQUEST_SZ - 4 + size;
1092 		}
1093 		break;
1094 
1095 	case SLI_MGMT_DHBA:
1096 		CtReq->CommandResponse.bits.CmdRsp = be16_to_cpu(SLI_MGMT_DHBA);
1097 		CtReq->CommandResponse.bits.Size = 0;
1098 		pe = (PORT_ENTRY *) & CtReq->un.PortID;
1099 		memcpy((uint8_t *) & pe->PortName,
1100 		       (uint8_t *) & phba->fc_sparam.portName,
1101 		       sizeof (struct lpfc_name));
1102 		size = GID_REQUEST_SZ - 4 + sizeof (struct lpfc_name);
1103 		break;
1104 
1105 	case SLI_MGMT_DPRT:
1106 		CtReq->CommandResponse.bits.CmdRsp = be16_to_cpu(SLI_MGMT_DPRT);
1107 		CtReq->CommandResponse.bits.Size = 0;
1108 		pe = (PORT_ENTRY *) & CtReq->un.PortID;
1109 		memcpy((uint8_t *) & pe->PortName,
1110 		       (uint8_t *) & phba->fc_sparam.portName,
1111 		       sizeof (struct lpfc_name));
1112 		size = GID_REQUEST_SZ - 4 + sizeof (struct lpfc_name);
1113 		break;
1114 	}
1115 
1116 	bpl = (struct ulp_bde64 *) bmp->virt;
1117 	bpl->addrHigh = le32_to_cpu( putPaddrHigh(mp->phys) );
1118 	bpl->addrLow = le32_to_cpu( putPaddrLow(mp->phys) );
1119 	bpl->tus.f.bdeFlags = 0;
1120 	bpl->tus.f.bdeSize = size;
1121 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
1122 
1123 	cmpl = lpfc_cmpl_ct_cmd_fdmi;
1124 
1125 	if (!lpfc_ct_cmd(phba, mp, bmp, ndlp, cmpl, FC_MAX_NS_RSP))
1126 		return 0;
1127 
1128 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1129 fdmi_cmd_free_bmp:
1130 	kfree(bmp);
1131 fdmi_cmd_free_mpvirt:
1132 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
1133 fdmi_cmd_free_mp:
1134 	kfree(mp);
1135 fdmi_cmd_exit:
1136 	/* Issue FDMI request failed */
1137 	lpfc_printf_log(phba,
1138 		        KERN_INFO,
1139 		        LOG_DISCOVERY,
1140 		        "%d:0244 Issue FDMI request failed Data: x%x\n",
1141 		        phba->brd_no,
1142 			cmdcode);
1143 	return 1;
1144 }
1145 
1146 void
1147 lpfc_fdmi_tmo(unsigned long ptr)
1148 {
1149 	struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
1150 	unsigned long iflag;
1151 
1152 	spin_lock_irqsave(phba->host->host_lock, iflag);
1153 	if (!(phba->work_hba_events & WORKER_FDMI_TMO)) {
1154 		phba->work_hba_events |= WORKER_FDMI_TMO;
1155 		if (phba->work_wait)
1156 			wake_up(phba->work_wait);
1157 	}
1158 	spin_unlock_irqrestore(phba->host->host_lock,iflag);
1159 }
1160 
1161 void
1162 lpfc_fdmi_tmo_handler(struct lpfc_hba *phba)
1163 {
1164 	struct lpfc_nodelist *ndlp;
1165 
1166 	ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, FDMI_DID);
1167 	if (ndlp) {
1168 		if (system_utsname.nodename[0] != '\0') {
1169 			lpfc_fdmi_cmd(phba, ndlp, SLI_MGMT_DHBA);
1170 		} else {
1171 			mod_timer(&phba->fc_fdmitmo, jiffies + HZ * 60);
1172 		}
1173 	}
1174 	return;
1175 }
1176 
1177 
1178 void
1179 lpfc_decode_firmware_rev(struct lpfc_hba * phba, char *fwrevision, int flag)
1180 {
1181 	struct lpfc_sli *psli = &phba->sli;
1182 	lpfc_vpd_t *vp = &phba->vpd;
1183 	uint32_t b1, b2, b3, b4, i, rev;
1184 	char c;
1185 	uint32_t *ptr, str[4];
1186 	uint8_t *fwname;
1187 
1188 	if (vp->rev.rBit) {
1189 		if (psli->sli_flag & LPFC_SLI2_ACTIVE)
1190 			rev = vp->rev.sli2FwRev;
1191 		else
1192 			rev = vp->rev.sli1FwRev;
1193 
1194 		b1 = (rev & 0x0000f000) >> 12;
1195 		b2 = (rev & 0x00000f00) >> 8;
1196 		b3 = (rev & 0x000000c0) >> 6;
1197 		b4 = (rev & 0x00000030) >> 4;
1198 
1199 		switch (b4) {
1200 		case 0:
1201 			c = 'N';
1202 			break;
1203 		case 1:
1204 			c = 'A';
1205 			break;
1206 		case 2:
1207 			c = 'B';
1208 			break;
1209 		default:
1210 			c = 0;
1211 			break;
1212 		}
1213 		b4 = (rev & 0x0000000f);
1214 
1215 		if (psli->sli_flag & LPFC_SLI2_ACTIVE)
1216 			fwname = vp->rev.sli2FwName;
1217 		else
1218 			fwname = vp->rev.sli1FwName;
1219 
1220 		for (i = 0; i < 16; i++)
1221 			if (fwname[i] == 0x20)
1222 				fwname[i] = 0;
1223 
1224 		ptr = (uint32_t*)fwname;
1225 
1226 		for (i = 0; i < 3; i++)
1227 			str[i] = be32_to_cpu(*ptr++);
1228 
1229 		if (c == 0) {
1230 			if (flag)
1231 				sprintf(fwrevision, "%d.%d%d (%s)",
1232 					b1, b2, b3, (char *)str);
1233 			else
1234 				sprintf(fwrevision, "%d.%d%d", b1,
1235 					b2, b3);
1236 		} else {
1237 			if (flag)
1238 				sprintf(fwrevision, "%d.%d%d%c%d (%s)",
1239 					b1, b2, b3, c,
1240 					b4, (char *)str);
1241 			else
1242 				sprintf(fwrevision, "%d.%d%d%c%d",
1243 					b1, b2, b3, c, b4);
1244 		}
1245 	} else {
1246 		rev = vp->rev.smFwRev;
1247 
1248 		b1 = (rev & 0xff000000) >> 24;
1249 		b2 = (rev & 0x00f00000) >> 20;
1250 		b3 = (rev & 0x000f0000) >> 16;
1251 		c  = (rev & 0x0000ff00) >> 8;
1252 		b4 = (rev & 0x000000ff);
1253 
1254 		if (flag)
1255 			sprintf(fwrevision, "%d.%d%d%c%d ", b1,
1256 				b2, b3, c, b4);
1257 		else
1258 			sprintf(fwrevision, "%d.%d%d%c%d ", b1,
1259 				b2, b3, c, b4);
1260 	}
1261 	return;
1262 }
1263