]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/lpfc/lpfc_ct.c
scsi: lpfc: add Trunking support
[linux.git] / drivers / scsi / lpfc / lpfc_ct.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  *                                                                 *
10  * This program is free software; you can redistribute it and/or   *
11  * modify it under the terms of version 2 of the GNU General       *
12  * Public License as published by the Free Software Foundation.    *
13  * This program is distributed in the hope that it will be useful. *
14  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
15  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
16  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
17  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
18  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
19  * more details, a copy of which can be found in the file COPYING  *
20  * included with this package.                                     *
21  *******************************************************************/
22
23 /*
24  * Fibre Channel SCSI LAN Device Driver CT support: FC Generic Services FC-GS
25  */
26
27 #include <linux/blkdev.h>
28 #include <linux/pci.h>
29 #include <linux/interrupt.h>
30 #include <linux/slab.h>
31 #include <linux/utsname.h>
32
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <scsi/fc/fc_fs.h>
38
39 #include "lpfc_hw4.h"
40 #include "lpfc_hw.h"
41 #include "lpfc_sli.h"
42 #include "lpfc_sli4.h"
43 #include "lpfc_nl.h"
44 #include "lpfc_disc.h"
45 #include "lpfc.h"
46 #include "lpfc_scsi.h"
47 #include "lpfc_nvme.h"
48 #include "lpfc_logmsg.h"
49 #include "lpfc_crtn.h"
50 #include "lpfc_version.h"
51 #include "lpfc_vport.h"
52 #include "lpfc_debugfs.h"
53
54 /* FDMI Port Speed definitions - FC-GS-7 */
55 #define HBA_PORTSPEED_1GFC              0x00000001      /* 1G FC */
56 #define HBA_PORTSPEED_2GFC              0x00000002      /* 2G FC */
57 #define HBA_PORTSPEED_4GFC              0x00000008      /* 4G FC */
58 #define HBA_PORTSPEED_10GFC             0x00000004      /* 10G FC */
59 #define HBA_PORTSPEED_8GFC              0x00000010      /* 8G FC */
60 #define HBA_PORTSPEED_16GFC             0x00000020      /* 16G FC */
61 #define HBA_PORTSPEED_32GFC             0x00000040      /* 32G FC */
62 #define HBA_PORTSPEED_20GFC             0x00000080      /* 20G FC */
63 #define HBA_PORTSPEED_40GFC             0x00000100      /* 40G FC */
64 #define HBA_PORTSPEED_128GFC            0x00000200      /* 128G FC */
65 #define HBA_PORTSPEED_64GFC             0x00000400      /* 64G FC */
66 #define HBA_PORTSPEED_256GFC            0x00000800      /* 256G FC */
67 #define HBA_PORTSPEED_UNKNOWN           0x00008000      /* Unknown */
68 #define HBA_PORTSPEED_10GE              0x00010000      /* 10G E */
69 #define HBA_PORTSPEED_40GE              0x00020000      /* 40G E */
70 #define HBA_PORTSPEED_100GE             0x00040000      /* 100G E */
71 #define HBA_PORTSPEED_25GE              0x00080000      /* 25G E */
72 #define HBA_PORTSPEED_50GE              0x00100000      /* 50G E */
73 #define HBA_PORTSPEED_400GE             0x00200000      /* 400G E */
74
75 #define FOURBYTES       4
76
77
78 static char *lpfc_release_version = LPFC_DRIVER_VERSION;
79
80 static void
81 lpfc_ct_ignore_hbq_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
82                           struct lpfc_dmabuf *mp, uint32_t size)
83 {
84         if (!mp) {
85                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
86                                 "0146 Ignoring unsolicited CT No HBQ "
87                                 "status = x%x\n",
88                                 piocbq->iocb.ulpStatus);
89         }
90         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
91                         "0145 Ignoring unsolicted CT HBQ Size:%d "
92                         "status = x%x\n",
93                         size, piocbq->iocb.ulpStatus);
94 }
95
96 static void
97 lpfc_ct_unsol_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
98                      struct lpfc_dmabuf *mp, uint32_t size)
99 {
100         lpfc_ct_ignore_hbq_buffer(phba, piocbq, mp, size);
101 }
102
103 void
104 lpfc_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
105                     struct lpfc_iocbq *piocbq)
106 {
107         struct lpfc_dmabuf *mp = NULL;
108         IOCB_t *icmd = &piocbq->iocb;
109         int i;
110         struct lpfc_iocbq *iocbq;
111         dma_addr_t paddr;
112         uint32_t size;
113         struct list_head head;
114         struct lpfc_dmabuf *bdeBuf;
115
116         if (lpfc_bsg_ct_unsol_event(phba, pring, piocbq) == 0)
117                 return;
118
119         if (unlikely(icmd->ulpStatus == IOSTAT_NEED_BUFFER)) {
120                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
121         } else if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
122                    ((icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
123                    IOERR_RCV_BUFFER_WAITING)) {
124                 /* Not enough posted buffers; Try posting more buffers */
125                 phba->fc_stat.NoRcvBuf++;
126                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
127                         lpfc_post_buffer(phba, pring, 2);
128                 return;
129         }
130
131         /* If there are no BDEs associated with this IOCB,
132          * there is nothing to do.
133          */
134         if (icmd->ulpBdeCount == 0)
135                 return;
136
137         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
138                 INIT_LIST_HEAD(&head);
139                 list_add_tail(&head, &piocbq->list);
140                 list_for_each_entry(iocbq, &head, list) {
141                         icmd = &iocbq->iocb;
142                         if (icmd->ulpBdeCount == 0)
143                                 continue;
144                         bdeBuf = iocbq->context2;
145                         iocbq->context2 = NULL;
146                         size  = icmd->un.cont64[0].tus.f.bdeSize;
147                         lpfc_ct_unsol_buffer(phba, piocbq, bdeBuf, size);
148                         lpfc_in_buf_free(phba, bdeBuf);
149                         if (icmd->ulpBdeCount == 2) {
150                                 bdeBuf = iocbq->context3;
151                                 iocbq->context3 = NULL;
152                                 size  = icmd->unsli3.rcvsli3.bde2.tus.f.bdeSize;
153                                 lpfc_ct_unsol_buffer(phba, piocbq, bdeBuf,
154                                                      size);
155                                 lpfc_in_buf_free(phba, bdeBuf);
156                         }
157                 }
158                 list_del(&head);
159         } else {
160                 INIT_LIST_HEAD(&head);
161                 list_add_tail(&head, &piocbq->list);
162                 list_for_each_entry(iocbq, &head, list) {
163                         icmd = &iocbq->iocb;
164                         if (icmd->ulpBdeCount == 0)
165                                 lpfc_ct_unsol_buffer(phba, iocbq, NULL, 0);
166                         for (i = 0; i < icmd->ulpBdeCount; i++) {
167                                 paddr = getPaddr(icmd->un.cont64[i].addrHigh,
168                                                  icmd->un.cont64[i].addrLow);
169                                 mp = lpfc_sli_ringpostbuf_get(phba, pring,
170                                                               paddr);
171                                 size = icmd->un.cont64[i].tus.f.bdeSize;
172                                 lpfc_ct_unsol_buffer(phba, iocbq, mp, size);
173                                 lpfc_in_buf_free(phba, mp);
174                         }
175                         lpfc_post_buffer(phba, pring, i);
176                 }
177                 list_del(&head);
178         }
179 }
180
181 /**
182  * lpfc_ct_handle_unsol_abort - ct upper level protocol abort handler
183  * @phba: Pointer to HBA context object.
184  * @dmabuf: pointer to a dmabuf that describes the FC sequence
185  *
186  * This function serves as the upper level protocol abort handler for CT
187  * protocol.
188  *
189  * Return 1 if abort has been handled, 0 otherwise.
190  **/
191 int
192 lpfc_ct_handle_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
193 {
194         int handled;
195
196         /* CT upper level goes through BSG */
197         handled = lpfc_bsg_ct_unsol_abort(phba, dmabuf);
198
199         return handled;
200 }
201
202 static void
203 lpfc_free_ct_rsp(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
204 {
205         struct lpfc_dmabuf *mlast, *next_mlast;
206
207         list_for_each_entry_safe(mlast, next_mlast, &mlist->list, list) {
208                 lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
209                 list_del(&mlast->list);
210                 kfree(mlast);
211         }
212         lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
213         kfree(mlist);
214         return;
215 }
216
217 static struct lpfc_dmabuf *
218 lpfc_alloc_ct_rsp(struct lpfc_hba *phba, int cmdcode, struct ulp_bde64 *bpl,
219                   uint32_t size, int *entries)
220 {
221         struct lpfc_dmabuf *mlist = NULL;
222         struct lpfc_dmabuf *mp;
223         int cnt, i = 0;
224
225         /* We get chunks of FCELSSIZE */
226         cnt = size > FCELSSIZE ? FCELSSIZE: size;
227
228         while (size) {
229                 /* Allocate buffer for rsp payload */
230                 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
231                 if (!mp) {
232                         if (mlist)
233                                 lpfc_free_ct_rsp(phba, mlist);
234                         return NULL;
235                 }
236
237                 INIT_LIST_HEAD(&mp->list);
238
239                 if (cmdcode == be16_to_cpu(SLI_CTNS_GID_FT) ||
240                     cmdcode == be16_to_cpu(SLI_CTNS_GFF_ID))
241                         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
242                 else
243                         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
244
245                 if (!mp->virt) {
246                         kfree(mp);
247                         if (mlist)
248                                 lpfc_free_ct_rsp(phba, mlist);
249                         return NULL;
250                 }
251
252                 /* Queue it to a linked list */
253                 if (!mlist)
254                         mlist = mp;
255                 else
256                         list_add_tail(&mp->list, &mlist->list);
257
258                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
259                 /* build buffer ptr list for IOCB */
260                 bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
261                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
262                 bpl->tus.f.bdeSize = (uint16_t) cnt;
263                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
264                 bpl++;
265
266                 i++;
267                 size -= cnt;
268         }
269
270         *entries = i;
271         return mlist;
272 }
273
274 int
275 lpfc_ct_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *ctiocb)
276 {
277         struct lpfc_dmabuf *buf_ptr;
278
279         if (ctiocb->context_un.ndlp) {
280                 lpfc_nlp_put(ctiocb->context_un.ndlp);
281                 ctiocb->context_un.ndlp = NULL;
282         }
283         if (ctiocb->context1) {
284                 buf_ptr = (struct lpfc_dmabuf *) ctiocb->context1;
285                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
286                 kfree(buf_ptr);
287                 ctiocb->context1 = NULL;
288         }
289         if (ctiocb->context2) {
290                 lpfc_free_ct_rsp(phba, (struct lpfc_dmabuf *) ctiocb->context2);
291                 ctiocb->context2 = NULL;
292         }
293
294         if (ctiocb->context3) {
295                 buf_ptr = (struct lpfc_dmabuf *) ctiocb->context3;
296                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
297                 kfree(buf_ptr);
298                 ctiocb->context3 = NULL;
299         }
300         lpfc_sli_release_iocbq(phba, ctiocb);
301         return 0;
302 }
303
304 /**
305  * lpfc_gen_req - Build and issue a GEN_REQUEST command  to the SLI Layer
306  * @vport: pointer to a host virtual N_Port data structure.
307  * @bmp: Pointer to BPL for SLI command
308  * @inp: Pointer to data buffer for response data.
309  * @outp: Pointer to data buffer that hold the CT command.
310  * @cmpl: completion routine to call when command completes
311  * @ndlp: Destination NPort nodelist entry
312  *
313  * This function as the final part for issuing a CT command.
314  */
315 static int
316 lpfc_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
317              struct lpfc_dmabuf *inp, struct lpfc_dmabuf *outp,
318              void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
319                      struct lpfc_iocbq *),
320              struct lpfc_nodelist *ndlp, uint32_t usr_flg, uint32_t num_entry,
321              uint32_t tmo, uint8_t retry)
322 {
323         struct lpfc_hba  *phba = vport->phba;
324         IOCB_t *icmd;
325         struct lpfc_iocbq *geniocb;
326         int rc;
327
328         /* Allocate buffer for  command iocb */
329         geniocb = lpfc_sli_get_iocbq(phba);
330
331         if (geniocb == NULL)
332                 return 1;
333
334         icmd = &geniocb->iocb;
335         icmd->un.genreq64.bdl.ulpIoTag32 = 0;
336         icmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
337         icmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
338         icmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
339         icmd->un.genreq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
340
341         if (usr_flg)
342                 geniocb->context3 = NULL;
343         else
344                 geniocb->context3 = (uint8_t *) bmp;
345
346         /* Save for completion so we can release these resources */
347         geniocb->context1 = (uint8_t *) inp;
348         geniocb->context2 = (uint8_t *) outp;
349         geniocb->context_un.ndlp = lpfc_nlp_get(ndlp);
350
351         /* Fill in payload, bp points to frame payload */
352         icmd->ulpCommand = CMD_GEN_REQUEST64_CR;
353
354         /* Fill in rest of iocb */
355         icmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
356         icmd->un.genreq64.w5.hcsw.Dfctl = 0;
357         icmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
358         icmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
359
360         if (!tmo) {
361                  /* FC spec states we need 3 * ratov for CT requests */
362                 tmo = (3 * phba->fc_ratov);
363         }
364         icmd->ulpTimeout = tmo;
365         icmd->ulpBdeCount = 1;
366         icmd->ulpLe = 1;
367         icmd->ulpClass = CLASS3;
368         icmd->ulpContext = ndlp->nlp_rpi;
369         if (phba->sli_rev == LPFC_SLI_REV4)
370                 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
371
372         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
373                 /* For GEN_REQUEST64_CR, use the RPI */
374                 icmd->ulpCt_h = 0;
375                 icmd->ulpCt_l = 0;
376         }
377
378         /* Issue GEN REQ IOCB for NPORT <did> */
379         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
380                          "0119 Issue GEN REQ IOCB to NPORT x%x "
381                          "Data: x%x x%x\n",
382                          ndlp->nlp_DID, icmd->ulpIoTag,
383                          vport->port_state);
384         geniocb->iocb_cmpl = cmpl;
385         geniocb->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
386         geniocb->vport = vport;
387         geniocb->retry = retry;
388         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, geniocb, 0);
389
390         if (rc == IOCB_ERROR) {
391                 lpfc_sli_release_iocbq(phba, geniocb);
392                 return 1;
393         }
394
395         return 0;
396 }
397
398 /**
399  * lpfc_ct_cmd - Build and issue a CT command
400  * @vport: pointer to a host virtual N_Port data structure.
401  * @inmp: Pointer to data buffer for response data.
402  * @bmp: Pointer to BPL for SLI command
403  * @ndlp: Destination NPort nodelist entry
404  * @cmpl: completion routine to call when command completes
405  *
406  * This function is called for issuing a CT command.
407  */
408 static int
409 lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
410             struct lpfc_dmabuf *bmp, struct lpfc_nodelist *ndlp,
411             void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
412                           struct lpfc_iocbq *),
413             uint32_t rsp_size, uint8_t retry)
414 {
415         struct lpfc_hba  *phba = vport->phba;
416         struct ulp_bde64 *bpl = (struct ulp_bde64 *) bmp->virt;
417         struct lpfc_dmabuf *outmp;
418         int cnt = 0, status;
419         int cmdcode = ((struct lpfc_sli_ct_request *) inmp->virt)->
420                 CommandResponse.bits.CmdRsp;
421
422         bpl++;                  /* Skip past ct request */
423
424         /* Put buffer(s) for ct rsp in bpl */
425         outmp = lpfc_alloc_ct_rsp(phba, cmdcode, bpl, rsp_size, &cnt);
426         if (!outmp)
427                 return -ENOMEM;
428         /*
429          * Form the CT IOCB.  The total number of BDEs in this IOCB
430          * is the single command plus response count from
431          * lpfc_alloc_ct_rsp.
432          */
433         cnt += 1;
434         status = lpfc_gen_req(vport, bmp, inmp, outmp, cmpl, ndlp, 0,
435                               cnt, 0, retry);
436         if (status) {
437                 lpfc_free_ct_rsp(phba, outmp);
438                 return -ENOMEM;
439         }
440         return 0;
441 }
442
443 struct lpfc_vport *
444 lpfc_find_vport_by_did(struct lpfc_hba *phba, uint32_t did) {
445         struct lpfc_vport *vport_curr;
446         unsigned long flags;
447
448         spin_lock_irqsave(&phba->port_list_lock, flags);
449         list_for_each_entry(vport_curr, &phba->port_list, listentry) {
450                 if ((vport_curr->fc_myDID) && (vport_curr->fc_myDID == did)) {
451                         spin_unlock_irqrestore(&phba->port_list_lock, flags);
452                         return vport_curr;
453                 }
454         }
455         spin_unlock_irqrestore(&phba->port_list_lock, flags);
456         return NULL;
457 }
458
459 static void
460 lpfc_prep_node_fc4type(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
461 {
462         struct lpfc_nodelist *ndlp;
463
464         if ((vport->port_type != LPFC_NPIV_PORT) ||
465             !(vport->ct_flags & FC_CT_RFF_ID) || !vport->cfg_restrict_login) {
466
467                 ndlp = lpfc_setup_disc_node(vport, Did);
468
469                 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
470                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
471                                 "Parse GID_FTrsp: did:x%x flg:x%x x%x",
472                                 Did, ndlp->nlp_flag, vport->fc_flag);
473
474                         /* By default, the driver expects to support FCP FC4 */
475                         if (fc4_type == FC_TYPE_FCP)
476                                 ndlp->nlp_fc4_type |= NLP_FC4_FCP;
477
478                         if (fc4_type == FC_TYPE_NVME)
479                                 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
480
481                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
482                                          "0238 Process x%06x NameServer Rsp "
483                                          "Data: x%x x%x x%x x%x\n", Did,
484                                          ndlp->nlp_flag, ndlp->nlp_fc4_type,
485                                          vport->fc_flag,
486                                          vport->fc_rscn_id_cnt);
487                 } else {
488                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
489                                 "Skip1 GID_FTrsp: did:x%x flg:x%x cnt:%d",
490                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
491
492                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
493                                          "0239 Skip x%06x NameServer Rsp "
494                                          "Data: x%x x%x\n", Did,
495                                          vport->fc_flag,
496                                          vport->fc_rscn_id_cnt);
497                 }
498         } else {
499                 if (!(vport->fc_flag & FC_RSCN_MODE) ||
500                     lpfc_rscn_payload_check(vport, Did)) {
501                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
502                                 "Query GID_FTrsp: did:x%x flg:x%x cnt:%d",
503                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
504
505                         /*
506                          * This NPortID was previously a FCP/NVMe target,
507                          * Don't even bother to send GFF_ID.
508                          */
509                         ndlp = lpfc_findnode_did(vport, Did);
510                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
511                             (ndlp->nlp_type &
512                             (NLP_FCP_TARGET | NLP_NVME_TARGET))) {
513                                 if (fc4_type == FC_TYPE_FCP)
514                                         ndlp->nlp_fc4_type |= NLP_FC4_FCP;
515                                 if (fc4_type == FC_TYPE_NVME)
516                                         ndlp->nlp_fc4_type |= NLP_FC4_NVME;
517                                 lpfc_setup_disc_node(vport, Did);
518                         } else if (lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
519                                    0, Did) == 0)
520                                 vport->num_disc_nodes++;
521                         else
522                                 lpfc_setup_disc_node(vport, Did);
523                 } else {
524                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
525                                 "Skip2 GID_FTrsp: did:x%x flg:x%x cnt:%d",
526                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
527
528                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
529                                          "0245 Skip x%06x NameServer Rsp "
530                                          "Data: x%x x%x\n", Did,
531                                          vport->fc_flag,
532                                          vport->fc_rscn_id_cnt);
533                 }
534         }
535 }
536
537 static void
538 lpfc_ns_rsp_audit_did(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
539 {
540         struct lpfc_hba *phba = vport->phba;
541         struct lpfc_nodelist *ndlp = NULL;
542         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
543
544         /*
545          * To conserve rpi's, filter out addresses for other
546          * vports on the same physical HBAs.
547          */
548         if (Did != vport->fc_myDID &&
549             (!lpfc_find_vport_by_did(phba, Did) ||
550              vport->cfg_peer_port_login)) {
551                 if (!phba->nvmet_support) {
552                         /* FCPI/NVMEI path. Process Did */
553                         lpfc_prep_node_fc4type(vport, Did, fc4_type);
554                         return;
555                 }
556                 /* NVMET path.  NVMET only cares about NVMEI nodes. */
557                 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
558                         if (ndlp->nlp_type != NLP_NVME_INITIATOR ||
559                             ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
560                                 continue;
561                         spin_lock_irq(shost->host_lock);
562                         if (ndlp->nlp_DID == Did)
563                                 ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
564                         else
565                                 ndlp->nlp_flag |= NLP_NVMET_RECOV;
566                         spin_unlock_irq(shost->host_lock);
567                 }
568         }
569 }
570
571 static int
572 lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint8_t fc4_type,
573             uint32_t Size)
574 {
575         struct lpfc_sli_ct_request *Response =
576                 (struct lpfc_sli_ct_request *) mp->virt;
577         struct lpfc_dmabuf *mlast, *next_mp;
578         uint32_t *ctptr = (uint32_t *) & Response->un.gid.PortType;
579         uint32_t Did, CTentry;
580         int Cnt;
581         struct list_head head;
582         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
583         struct lpfc_nodelist *ndlp = NULL;
584
585         lpfc_set_disctmo(vport);
586         vport->num_disc_nodes = 0;
587         vport->fc_ns_retry = 0;
588
589
590         list_add_tail(&head, &mp->list);
591         list_for_each_entry_safe(mp, next_mp, &head, list) {
592                 mlast = mp;
593
594                 Cnt = Size  > FCELSSIZE ? FCELSSIZE : Size;
595
596                 Size -= Cnt;
597
598                 if (!ctptr) {
599                         ctptr = (uint32_t *) mlast->virt;
600                 } else
601                         Cnt -= 16;      /* subtract length of CT header */
602
603                 /* Loop through entire NameServer list of DIDs */
604                 while (Cnt >= sizeof(uint32_t)) {
605                         /* Get next DID from NameServer List */
606                         CTentry = *ctptr++;
607                         Did = ((be32_to_cpu(CTentry)) & Mask_DID);
608                         lpfc_ns_rsp_audit_did(vport, Did, fc4_type);
609                         if (CTentry & (cpu_to_be32(SLI_CT_LAST_ENTRY)))
610                                 goto nsout1;
611
612                         Cnt -= sizeof(uint32_t);
613                 }
614                 ctptr = NULL;
615
616         }
617
618         /* All GID_FT entries processed.  If the driver is running in
619          * in target mode, put impacted nodes into recovery and drop
620          * the RPI to flush outstanding IO.
621          */
622         if (vport->phba->nvmet_support) {
623                 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
624                         if (!(ndlp->nlp_flag & NLP_NVMET_RECOV))
625                                 continue;
626                         lpfc_disc_state_machine(vport, ndlp, NULL,
627                                                 NLP_EVT_DEVICE_RECOVERY);
628                         spin_lock_irq(shost->host_lock);
629                         ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
630                         spin_unlock_irq(shost->host_lock);
631                 }
632         }
633
634 nsout1:
635         list_del(&head);
636         return 0;
637 }
638
639 static void
640 lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
641                         struct lpfc_iocbq *rspiocb)
642 {
643         struct lpfc_vport *vport = cmdiocb->vport;
644         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
645         IOCB_t *irsp;
646         struct lpfc_dmabuf *outp;
647         struct lpfc_dmabuf *inp;
648         struct lpfc_sli_ct_request *CTrsp;
649         struct lpfc_sli_ct_request *CTreq;
650         struct lpfc_nodelist *ndlp;
651         int rc, type;
652
653         /* First save ndlp, before we overwrite it */
654         ndlp = cmdiocb->context_un.ndlp;
655
656         /* we pass cmdiocb to state machine which needs rspiocb as well */
657         cmdiocb->context_un.rsp_iocb = rspiocb;
658         inp = (struct lpfc_dmabuf *) cmdiocb->context1;
659         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
660         irsp = &rspiocb->iocb;
661
662         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
663                  "GID_FT cmpl:     status:x%x/x%x rtry:%d",
664                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_ns_retry);
665
666         /* Don't bother processing response if vport is being torn down. */
667         if (vport->load_flag & FC_UNLOADING) {
668                 if (vport->fc_flag & FC_RSCN_MODE)
669                         lpfc_els_flush_rscn(vport);
670                 goto out;
671         }
672
673         if (lpfc_els_chk_latt(vport)) {
674                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
675                                  "0216 Link event during NS query\n");
676                 if (vport->fc_flag & FC_RSCN_MODE)
677                         lpfc_els_flush_rscn(vport);
678                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
679                 goto out;
680         }
681         if (lpfc_error_lost_link(irsp)) {
682                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
683                                  "0226 NS query failed due to link event\n");
684                 if (vport->fc_flag & FC_RSCN_MODE)
685                         lpfc_els_flush_rscn(vport);
686                 goto out;
687         }
688
689         spin_lock_irq(shost->host_lock);
690         if (vport->fc_flag & FC_RSCN_DEFERRED) {
691                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
692                 spin_unlock_irq(shost->host_lock);
693
694                 /* This is a GID_FT completing so the gidft_inp counter was
695                  * incremented before the GID_FT was issued to the wire.
696                  */
697                 vport->gidft_inp--;
698
699                 /*
700                  * Skip processing the NS response
701                  * Re-issue the NS cmd
702                  */
703                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
704                                  "0151 Process Deferred RSCN Data: x%x x%x\n",
705                                  vport->fc_flag, vport->fc_rscn_id_cnt);
706                 lpfc_els_handle_rscn(vport);
707
708                 goto out;
709         }
710         spin_unlock_irq(shost->host_lock);
711
712         if (irsp->ulpStatus) {
713                 /* Check for retry */
714                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
715                         if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
716                             (irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
717                             IOERR_NO_RESOURCES)
718                                 vport->fc_ns_retry++;
719
720                         type = lpfc_get_gidft_type(vport, cmdiocb);
721                         if (type == 0)
722                                 goto out;
723
724                         /* CT command is being retried */
725                         vport->gidft_inp--;
726                         rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
727                                          vport->fc_ns_retry, type);
728                         if (rc == 0)
729                                 goto out;
730                 }
731                 if (vport->fc_flag & FC_RSCN_MODE)
732                         lpfc_els_flush_rscn(vport);
733                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
734                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
735                                  "0257 GID_FT Query error: 0x%x 0x%x\n",
736                                  irsp->ulpStatus, vport->fc_ns_retry);
737         } else {
738                 /* Good status, continue checking */
739                 CTreq = (struct lpfc_sli_ct_request *) inp->virt;
740                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
741                 if (CTrsp->CommandResponse.bits.CmdRsp ==
742                     cpu_to_be16(SLI_CT_RESPONSE_FS_ACC)) {
743                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
744                                          "0208 NameServer Rsp Data: x%x x%x\n",
745                                          vport->fc_flag,
746                                          CTreq->un.gid.Fc4Type);
747
748                         lpfc_ns_rsp(vport,
749                                     outp,
750                                     CTreq->un.gid.Fc4Type,
751                                     (uint32_t) (irsp->un.genreq64.bdl.bdeSize));
752                 } else if (CTrsp->CommandResponse.bits.CmdRsp ==
753                            be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
754                         /* NameServer Rsp Error */
755                         if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
756                             && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
757                                 lpfc_printf_vlog(vport, KERN_INFO,
758                                         LOG_DISCOVERY,
759                                         "0269 No NameServer Entries "
760                                         "Data: x%x x%x x%x x%x\n",
761                                         CTrsp->CommandResponse.bits.CmdRsp,
762                                         (uint32_t) CTrsp->ReasonCode,
763                                         (uint32_t) CTrsp->Explanation,
764                                         vport->fc_flag);
765
766                                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
767                                 "GID_FT no entry  cmd:x%x rsn:x%x exp:x%x",
768                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
769                                 (uint32_t) CTrsp->ReasonCode,
770                                 (uint32_t) CTrsp->Explanation);
771                         } else {
772                                 lpfc_printf_vlog(vport, KERN_INFO,
773                                         LOG_DISCOVERY,
774                                         "0240 NameServer Rsp Error "
775                                         "Data: x%x x%x x%x x%x\n",
776                                         CTrsp->CommandResponse.bits.CmdRsp,
777                                         (uint32_t) CTrsp->ReasonCode,
778                                         (uint32_t) CTrsp->Explanation,
779                                         vport->fc_flag);
780
781                                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
782                                 "GID_FT rsp err1  cmd:x%x rsn:x%x exp:x%x",
783                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
784                                 (uint32_t) CTrsp->ReasonCode,
785                                 (uint32_t) CTrsp->Explanation);
786                         }
787
788
789                 } else {
790                         /* NameServer Rsp Error */
791                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
792                                         "0241 NameServer Rsp Error "
793                                         "Data: x%x x%x x%x x%x\n",
794                                         CTrsp->CommandResponse.bits.CmdRsp,
795                                         (uint32_t) CTrsp->ReasonCode,
796                                         (uint32_t) CTrsp->Explanation,
797                                         vport->fc_flag);
798
799                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
800                                 "GID_FT rsp err2  cmd:x%x rsn:x%x exp:x%x",
801                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
802                                 (uint32_t) CTrsp->ReasonCode,
803                                 (uint32_t) CTrsp->Explanation);
804                 }
805                 vport->gidft_inp--;
806         }
807         /* Link up / RSCN discovery */
808         if ((vport->num_disc_nodes == 0) &&
809             (vport->gidft_inp == 0)) {
810                 /*
811                  * The driver has cycled through all Nports in the RSCN payload.
812                  * Complete the handling by cleaning up and marking the
813                  * current driver state.
814                  */
815                 if (vport->port_state >= LPFC_DISC_AUTH) {
816                         if (vport->fc_flag & FC_RSCN_MODE) {
817                                 lpfc_els_flush_rscn(vport);
818                                 spin_lock_irq(shost->host_lock);
819                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
820                                 spin_unlock_irq(shost->host_lock);
821                         }
822                         else
823                                 lpfc_els_flush_rscn(vport);
824                 }
825
826                 lpfc_disc_start(vport);
827         }
828 out:
829         cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
830         lpfc_ct_free_iocb(phba, cmdiocb);
831         return;
832 }
833
834 static void
835 lpfc_cmpl_ct_cmd_gid_pt(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
836                         struct lpfc_iocbq *rspiocb)
837 {
838         struct lpfc_vport *vport = cmdiocb->vport;
839         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
840         IOCB_t *irsp;
841         struct lpfc_dmabuf *outp;
842         struct lpfc_dmabuf *inp;
843         struct lpfc_sli_ct_request *CTrsp;
844         struct lpfc_sli_ct_request *CTreq;
845         struct lpfc_nodelist *ndlp;
846         int rc;
847
848         /* First save ndlp, before we overwrite it */
849         ndlp = cmdiocb->context_un.ndlp;
850
851         /* we pass cmdiocb to state machine which needs rspiocb as well */
852         cmdiocb->context_un.rsp_iocb = rspiocb;
853         inp = (struct lpfc_dmabuf *)cmdiocb->context1;
854         outp = (struct lpfc_dmabuf *)cmdiocb->context2;
855         irsp = &rspiocb->iocb;
856
857         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
858                               "GID_PT cmpl:     status:x%x/x%x rtry:%d",
859                               irsp->ulpStatus, irsp->un.ulpWord[4],
860                               vport->fc_ns_retry);
861
862         /* Don't bother processing response if vport is being torn down. */
863         if (vport->load_flag & FC_UNLOADING) {
864                 if (vport->fc_flag & FC_RSCN_MODE)
865                         lpfc_els_flush_rscn(vport);
866                 goto out;
867         }
868
869         if (lpfc_els_chk_latt(vport)) {
870                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
871                                  "4108 Link event during NS query\n");
872                 if (vport->fc_flag & FC_RSCN_MODE)
873                         lpfc_els_flush_rscn(vport);
874                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
875                 goto out;
876         }
877         if (lpfc_error_lost_link(irsp)) {
878                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
879                                  "4101 NS query failed due to link event\n");
880                 if (vport->fc_flag & FC_RSCN_MODE)
881                         lpfc_els_flush_rscn(vport);
882                 goto out;
883         }
884
885         spin_lock_irq(shost->host_lock);
886         if (vport->fc_flag & FC_RSCN_DEFERRED) {
887                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
888                 spin_unlock_irq(shost->host_lock);
889
890                 /* This is a GID_PT completing so the gidft_inp counter was
891                  * incremented before the GID_PT was issued to the wire.
892                  */
893                 vport->gidft_inp--;
894
895                 /*
896                  * Skip processing the NS response
897                  * Re-issue the NS cmd
898                  */
899                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
900                                  "4102 Process Deferred RSCN Data: x%x x%x\n",
901                                  vport->fc_flag, vport->fc_rscn_id_cnt);
902                 lpfc_els_handle_rscn(vport);
903
904                 goto out;
905         }
906         spin_unlock_irq(shost->host_lock);
907
908         if (irsp->ulpStatus) {
909                 /* Check for retry */
910                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
911                         if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
912                             (irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
913                             IOERR_NO_RESOURCES)
914                                 vport->fc_ns_retry++;
915
916                         /* CT command is being retried */
917                         vport->gidft_inp--;
918                         rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_PT,
919                                          vport->fc_ns_retry, GID_PT_N_PORT);
920                         if (rc == 0)
921                                 goto out;
922                 }
923                 if (vport->fc_flag & FC_RSCN_MODE)
924                         lpfc_els_flush_rscn(vport);
925                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
926                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
927                                  "4103 GID_FT Query error: 0x%x 0x%x\n",
928                                  irsp->ulpStatus, vport->fc_ns_retry);
929         } else {
930                 /* Good status, continue checking */
931                 CTreq = (struct lpfc_sli_ct_request *)inp->virt;
932                 CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
933                 if (CTrsp->CommandResponse.bits.CmdRsp ==
934                     cpu_to_be16(SLI_CT_RESPONSE_FS_ACC)) {
935                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
936                                          "4105 NameServer Rsp Data: x%x x%x\n",
937                                          vport->fc_flag,
938                                          CTreq->un.gid.Fc4Type);
939
940                         lpfc_ns_rsp(vport,
941                                     outp,
942                                     CTreq->un.gid.Fc4Type,
943                                     (uint32_t)(irsp->un.genreq64.bdl.bdeSize));
944                 } else if (CTrsp->CommandResponse.bits.CmdRsp ==
945                            be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
946                         /* NameServer Rsp Error */
947                         if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
948                             && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
949                                 lpfc_printf_vlog(
950                                         vport, KERN_INFO, LOG_DISCOVERY,
951                                         "4106 No NameServer Entries "
952                                         "Data: x%x x%x x%x x%x\n",
953                                         CTrsp->CommandResponse.bits.CmdRsp,
954                                         (uint32_t)CTrsp->ReasonCode,
955                                         (uint32_t)CTrsp->Explanation,
956                                         vport->fc_flag);
957
958                                 lpfc_debugfs_disc_trc(
959                                 vport, LPFC_DISC_TRC_CT,
960                                 "GID_PT no entry  cmd:x%x rsn:x%x exp:x%x",
961                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
962                                 (uint32_t)CTrsp->ReasonCode,
963                                 (uint32_t)CTrsp->Explanation);
964                         } else {
965                                 lpfc_printf_vlog(
966                                         vport, KERN_INFO, LOG_DISCOVERY,
967                                         "4107 NameServer Rsp Error "
968                                         "Data: x%x x%x x%x x%x\n",
969                                         CTrsp->CommandResponse.bits.CmdRsp,
970                                         (uint32_t)CTrsp->ReasonCode,
971                                         (uint32_t)CTrsp->Explanation,
972                                         vport->fc_flag);
973
974                                 lpfc_debugfs_disc_trc(
975                                 vport, LPFC_DISC_TRC_CT,
976                                 "GID_PT rsp err1  cmd:x%x rsn:x%x exp:x%x",
977                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
978                                 (uint32_t)CTrsp->ReasonCode,
979                                 (uint32_t)CTrsp->Explanation);
980                         }
981                 } else {
982                         /* NameServer Rsp Error */
983                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
984                                          "4109 NameServer Rsp Error "
985                                          "Data: x%x x%x x%x x%x\n",
986                                          CTrsp->CommandResponse.bits.CmdRsp,
987                                          (uint32_t)CTrsp->ReasonCode,
988                                          (uint32_t)CTrsp->Explanation,
989                                          vport->fc_flag);
990
991                         lpfc_debugfs_disc_trc(
992                                 vport, LPFC_DISC_TRC_CT,
993                                 "GID_PT rsp err2  cmd:x%x rsn:x%x exp:x%x",
994                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
995                                 (uint32_t)CTrsp->ReasonCode,
996                                 (uint32_t)CTrsp->Explanation);
997                 }
998                 vport->gidft_inp--;
999         }
1000         /* Link up / RSCN discovery */
1001         if ((vport->num_disc_nodes == 0) &&
1002             (vport->gidft_inp == 0)) {
1003                 /*
1004                  * The driver has cycled through all Nports in the RSCN payload.
1005                  * Complete the handling by cleaning up and marking the
1006                  * current driver state.
1007                  */
1008                 if (vport->port_state >= LPFC_DISC_AUTH) {
1009                         if (vport->fc_flag & FC_RSCN_MODE) {
1010                                 lpfc_els_flush_rscn(vport);
1011                                 spin_lock_irq(shost->host_lock);
1012                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1013                                 spin_unlock_irq(shost->host_lock);
1014                         } else {
1015                                 lpfc_els_flush_rscn(vport);
1016                         }
1017                 }
1018
1019                 lpfc_disc_start(vport);
1020         }
1021 out:
1022         cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
1023         lpfc_ct_free_iocb(phba, cmdiocb);
1024 }
1025
1026 static void
1027 lpfc_cmpl_ct_cmd_gff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1028                         struct lpfc_iocbq *rspiocb)
1029 {
1030         struct lpfc_vport *vport = cmdiocb->vport;
1031         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1032         IOCB_t *irsp = &rspiocb->iocb;
1033         struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *) cmdiocb->context1;
1034         struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1035         struct lpfc_sli_ct_request *CTrsp;
1036         int did, rc, retry;
1037         uint8_t fbits;
1038         struct lpfc_nodelist *ndlp;
1039
1040         did = ((struct lpfc_sli_ct_request *) inp->virt)->un.gff.PortId;
1041         did = be32_to_cpu(did);
1042
1043         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1044                 "GFF_ID cmpl:     status:x%x/x%x did:x%x",
1045                 irsp->ulpStatus, irsp->un.ulpWord[4], did);
1046
1047         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1048                 /* Good status, continue checking */
1049                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1050                 fbits = CTrsp->un.gff_acc.fbits[FCP_TYPE_FEATURE_OFFSET];
1051
1052                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1053                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
1054                         if ((fbits & FC4_FEATURE_INIT) &&
1055                             !(fbits & FC4_FEATURE_TARGET)) {
1056                                 lpfc_printf_vlog(vport, KERN_INFO,
1057                                                  LOG_DISCOVERY,
1058                                                  "0270 Skip x%x GFF "
1059                                                  "NameServer Rsp Data: (init) "
1060                                                  "x%x x%x\n", did, fbits,
1061                                                  vport->fc_rscn_id_cnt);
1062                                 goto out;
1063                         }
1064                 }
1065         }
1066         else {
1067                 /* Check for retry */
1068                 if (cmdiocb->retry < LPFC_MAX_NS_RETRY) {
1069                         retry = 1;
1070                         if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1071                                 switch ((irsp->un.ulpWord[4] &
1072                                         IOERR_PARAM_MASK)) {
1073
1074                                 case IOERR_NO_RESOURCES:
1075                                         /* We don't increment the retry
1076                                          * count for this case.
1077                                          */
1078                                         break;
1079                                 case IOERR_LINK_DOWN:
1080                                 case IOERR_SLI_ABORTED:
1081                                 case IOERR_SLI_DOWN:
1082                                         retry = 0;
1083                                         break;
1084                                 default:
1085                                         cmdiocb->retry++;
1086                                 }
1087                         }
1088                         else
1089                                 cmdiocb->retry++;
1090
1091                         if (retry) {
1092                                 /* CT command is being retried */
1093                                 rc = lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
1094                                          cmdiocb->retry, did);
1095                                 if (rc == 0) {
1096                                         /* success */
1097                                         lpfc_ct_free_iocb(phba, cmdiocb);
1098                                         return;
1099                                 }
1100                         }
1101                 }
1102                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1103                                  "0267 NameServer GFF Rsp "
1104                                  "x%x Error (%d %d) Data: x%x x%x\n",
1105                                  did, irsp->ulpStatus, irsp->un.ulpWord[4],
1106                                  vport->fc_flag, vport->fc_rscn_id_cnt);
1107         }
1108
1109         /* This is a target port, unregistered port, or the GFF_ID failed */
1110         ndlp = lpfc_setup_disc_node(vport, did);
1111         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
1112                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1113                                  "0242 Process x%x GFF "
1114                                  "NameServer Rsp Data: x%x x%x x%x\n",
1115                                  did, ndlp->nlp_flag, vport->fc_flag,
1116                                  vport->fc_rscn_id_cnt);
1117         } else {
1118                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1119                                  "0243 Skip x%x GFF "
1120                                  "NameServer Rsp Data: x%x x%x\n", did,
1121                                  vport->fc_flag, vport->fc_rscn_id_cnt);
1122         }
1123 out:
1124         /* Link up / RSCN discovery */
1125         if (vport->num_disc_nodes)
1126                 vport->num_disc_nodes--;
1127         if (vport->num_disc_nodes == 0) {
1128                 /*
1129                  * The driver has cycled through all Nports in the RSCN payload.
1130                  * Complete the handling by cleaning up and marking the
1131                  * current driver state.
1132                  */
1133                 if (vport->port_state >= LPFC_DISC_AUTH) {
1134                         if (vport->fc_flag & FC_RSCN_MODE) {
1135                                 lpfc_els_flush_rscn(vport);
1136                                 spin_lock_irq(shost->host_lock);
1137                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1138                                 spin_unlock_irq(shost->host_lock);
1139                         }
1140                         else
1141                                 lpfc_els_flush_rscn(vport);
1142                 }
1143                 lpfc_disc_start(vport);
1144         }
1145         lpfc_ct_free_iocb(phba, cmdiocb);
1146         return;
1147 }
1148
1149 static void
1150 lpfc_cmpl_ct_cmd_gft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1151                                 struct lpfc_iocbq *rspiocb)
1152 {
1153         struct lpfc_vport *vport = cmdiocb->vport;
1154         IOCB_t *irsp = &rspiocb->iocb;
1155         struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *)cmdiocb->context1;
1156         struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *)cmdiocb->context2;
1157         struct lpfc_sli_ct_request *CTrsp;
1158         int did;
1159         struct lpfc_nodelist *ndlp;
1160         uint32_t fc4_data_0, fc4_data_1;
1161
1162         did = ((struct lpfc_sli_ct_request *)inp->virt)->un.gft.PortId;
1163         did = be32_to_cpu(did);
1164
1165         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1166                               "GFT_ID cmpl: status:x%x/x%x did:x%x",
1167                               irsp->ulpStatus, irsp->un.ulpWord[4], did);
1168
1169         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1170                 /* Good status, continue checking */
1171                 CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
1172                 fc4_data_0 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[0]);
1173                 fc4_data_1 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[1]);
1174                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1175                                  "3062 DID x%06x GFT Wd0 x%08x Wd1 x%08x\n",
1176                                  did, fc4_data_0, fc4_data_1);
1177
1178                 ndlp = lpfc_findnode_did(vport, did);
1179                 if (ndlp) {
1180                         /* The bitmask value for FCP and NVME FCP types is
1181                          * the same because they are 32 bits distant from
1182                          * each other in word0 and word0.
1183                          */
1184                         if (fc4_data_0 & LPFC_FC4_TYPE_BITMASK)
1185                                 ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1186                         if (fc4_data_1 &  LPFC_FC4_TYPE_BITMASK)
1187                                 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1188                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1189                                          "3064 Setting ndlp %p, DID x%06x with "
1190                                          "FC4 x%08x, Data: x%08x x%08x\n",
1191                                          ndlp, did, ndlp->nlp_fc4_type,
1192                                          FC_TYPE_FCP, FC_TYPE_NVME);
1193                         ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1194
1195                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1196                         lpfc_issue_els_prli(vport, ndlp, 0);
1197                 }
1198         } else
1199                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1200                                  "3065 GFT_ID failed x%08x\n", irsp->ulpStatus);
1201
1202         lpfc_ct_free_iocb(phba, cmdiocb);
1203 }
1204
1205 static void
1206 lpfc_cmpl_ct(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1207              struct lpfc_iocbq *rspiocb)
1208 {
1209         struct lpfc_vport *vport = cmdiocb->vport;
1210         struct lpfc_dmabuf *inp;
1211         struct lpfc_dmabuf *outp;
1212         IOCB_t *irsp;
1213         struct lpfc_sli_ct_request *CTrsp;
1214         struct lpfc_nodelist *ndlp;
1215         int cmdcode, rc;
1216         uint8_t retry;
1217         uint32_t latt;
1218
1219         /* First save ndlp, before we overwrite it */
1220         ndlp = cmdiocb->context_un.ndlp;
1221
1222         /* we pass cmdiocb to state machine which needs rspiocb as well */
1223         cmdiocb->context_un.rsp_iocb = rspiocb;
1224
1225         inp = (struct lpfc_dmabuf *) cmdiocb->context1;
1226         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1227         irsp = &rspiocb->iocb;
1228
1229         cmdcode = be16_to_cpu(((struct lpfc_sli_ct_request *) inp->virt)->
1230                                         CommandResponse.bits.CmdRsp);
1231         CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1232
1233         latt = lpfc_els_chk_latt(vport);
1234
1235         /* RFT request completes status <ulpStatus> CmdRsp <CmdRsp> */
1236         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1237                          "0209 CT Request completes, latt %d, "
1238                          "ulpStatus x%x CmdRsp x%x, Context x%x, Tag x%x\n",
1239                          latt, irsp->ulpStatus,
1240                          CTrsp->CommandResponse.bits.CmdRsp,
1241                          cmdiocb->iocb.ulpContext, cmdiocb->iocb.ulpIoTag);
1242
1243         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1244                 "CT cmd cmpl:     status:x%x/x%x cmd:x%x",
1245                 irsp->ulpStatus, irsp->un.ulpWord[4], cmdcode);
1246
1247         if (irsp->ulpStatus) {
1248                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1249                                  "0268 NS cmd x%x Error (x%x x%x)\n",
1250                                  cmdcode, irsp->ulpStatus, irsp->un.ulpWord[4]);
1251
1252                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1253                         (((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1254                           IOERR_SLI_DOWN) ||
1255                          ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1256                           IOERR_SLI_ABORTED)))
1257                         goto out;
1258
1259                 retry = cmdiocb->retry;
1260                 if (retry >= LPFC_MAX_NS_RETRY)
1261                         goto out;
1262
1263                 retry++;
1264                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1265                                  "0250 Retrying NS cmd %x\n", cmdcode);
1266                 rc = lpfc_ns_cmd(vport, cmdcode, retry, 0);
1267                 if (rc == 0)
1268                         goto out;
1269         }
1270
1271 out:
1272         cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
1273         lpfc_ct_free_iocb(phba, cmdiocb);
1274         return;
1275 }
1276
1277 static void
1278 lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1279                         struct lpfc_iocbq *rspiocb)
1280 {
1281         IOCB_t *irsp = &rspiocb->iocb;
1282         struct lpfc_vport *vport = cmdiocb->vport;
1283
1284         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1285                 struct lpfc_dmabuf *outp;
1286                 struct lpfc_sli_ct_request *CTrsp;
1287
1288                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1289                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1290                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1291                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1292                         vport->ct_flags |= FC_CT_RFT_ID;
1293         }
1294         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1295         return;
1296 }
1297
1298 static void
1299 lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1300                         struct lpfc_iocbq *rspiocb)
1301 {
1302         IOCB_t *irsp = &rspiocb->iocb;
1303         struct lpfc_vport *vport = cmdiocb->vport;
1304
1305         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1306                 struct lpfc_dmabuf *outp;
1307                 struct lpfc_sli_ct_request *CTrsp;
1308
1309                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1310                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1311                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1312                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1313                         vport->ct_flags |= FC_CT_RNN_ID;
1314         }
1315         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1316         return;
1317 }
1318
1319 static void
1320 lpfc_cmpl_ct_cmd_rspn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1321                          struct lpfc_iocbq *rspiocb)
1322 {
1323         IOCB_t *irsp = &rspiocb->iocb;
1324         struct lpfc_vport *vport = cmdiocb->vport;
1325
1326         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1327                 struct lpfc_dmabuf *outp;
1328                 struct lpfc_sli_ct_request *CTrsp;
1329
1330                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1331                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1332                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1333                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1334                         vport->ct_flags |= FC_CT_RSPN_ID;
1335         }
1336         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1337         return;
1338 }
1339
1340 static void
1341 lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1342                          struct lpfc_iocbq *rspiocb)
1343 {
1344         IOCB_t *irsp = &rspiocb->iocb;
1345         struct lpfc_vport *vport = cmdiocb->vport;
1346
1347         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1348                 struct lpfc_dmabuf *outp;
1349                 struct lpfc_sli_ct_request *CTrsp;
1350
1351                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1352                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1353                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1354                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1355                         vport->ct_flags |= FC_CT_RSNN_NN;
1356         }
1357         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1358         return;
1359 }
1360
1361 static void
1362 lpfc_cmpl_ct_cmd_da_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1363  struct lpfc_iocbq *rspiocb)
1364 {
1365         struct lpfc_vport *vport = cmdiocb->vport;
1366
1367         /* even if it fails we will act as though it succeeded. */
1368         vport->ct_flags = 0;
1369         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1370         return;
1371 }
1372
1373 static void
1374 lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1375                         struct lpfc_iocbq *rspiocb)
1376 {
1377         IOCB_t *irsp = &rspiocb->iocb;
1378         struct lpfc_vport *vport = cmdiocb->vport;
1379
1380         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1381                 struct lpfc_dmabuf *outp;
1382                 struct lpfc_sli_ct_request *CTrsp;
1383
1384                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1385                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1386                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1387                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1388                         vport->ct_flags |= FC_CT_RFF_ID;
1389         }
1390         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1391         return;
1392 }
1393
1394 /*
1395  * Although the symbolic port name is thought to be an integer
1396  * as of January 18, 2016, leave it as a string until more of
1397  * the record state becomes defined.
1398  */
1399 int
1400 lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol,
1401         size_t size)
1402 {
1403         int n;
1404
1405         /*
1406          * Use the lpfc board number as the Symbolic Port
1407          * Name object.  NPIV is not in play so this integer
1408          * value is sufficient and unique per FC-ID.
1409          */
1410         n = snprintf(symbol, size, "%d", vport->phba->brd_no);
1411         return n;
1412 }
1413
1414
1415 int
1416 lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol,
1417         size_t size)
1418 {
1419         char fwrev[FW_REV_STR_SIZE];
1420         int n;
1421
1422         lpfc_decode_firmware_rev(vport->phba, fwrev, 0);
1423
1424         n = snprintf(symbol, size, "Emulex %s", vport->phba->ModelName);
1425         if (size < n)
1426                 return n;
1427
1428         n += snprintf(symbol + n, size - n, " FV%s", fwrev);
1429         if (size < n)
1430                 return n;
1431
1432         n += snprintf(symbol + n, size - n, " DV%s.",
1433                       lpfc_release_version);
1434         if (size < n)
1435                 return n;
1436
1437         n += snprintf(symbol + n, size - n, " HN:%s.",
1438                       init_utsname()->nodename);
1439         if (size < n)
1440                 return n;
1441
1442         /* Note :- OS name is "Linux" */
1443         n += snprintf(symbol + n, size - n, " OS:%s\n",
1444                       init_utsname()->sysname);
1445         return n;
1446 }
1447
1448 static uint32_t
1449 lpfc_find_map_node(struct lpfc_vport *vport)
1450 {
1451         struct lpfc_nodelist *ndlp, *next_ndlp;
1452         struct Scsi_Host  *shost;
1453         uint32_t cnt = 0;
1454
1455         shost = lpfc_shost_from_vport(vport);
1456         spin_lock_irq(shost->host_lock);
1457         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1458                 if (ndlp->nlp_type & NLP_FABRIC)
1459                         continue;
1460                 if ((ndlp->nlp_state == NLP_STE_MAPPED_NODE) ||
1461                     (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE))
1462                         cnt++;
1463         }
1464         spin_unlock_irq(shost->host_lock);
1465         return cnt;
1466 }
1467
1468 /*
1469  * This routine will return the FC4 Type associated with the CT
1470  * GID_FT command.
1471  */
1472 int
1473 lpfc_get_gidft_type(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb)
1474 {
1475         struct lpfc_sli_ct_request *CtReq;
1476         struct lpfc_dmabuf *mp;
1477         uint32_t type;
1478
1479         mp = cmdiocb->context1;
1480         if (mp == NULL)
1481                 return 0;
1482         CtReq = (struct lpfc_sli_ct_request *)mp->virt;
1483         type = (uint32_t)CtReq->un.gid.Fc4Type;
1484         if ((type != SLI_CTPT_FCP) && (type != SLI_CTPT_NVME))
1485                 return 0;
1486         return type;
1487 }
1488
1489 /*
1490  * lpfc_ns_cmd
1491  * Description:
1492  *    Issue Cmd to NameServer
1493  *       SLI_CTNS_GID_FT
1494  *       LI_CTNS_RFT_ID
1495  */
1496 int
1497 lpfc_ns_cmd(struct lpfc_vport *vport, int cmdcode,
1498             uint8_t retry, uint32_t context)
1499 {
1500         struct lpfc_nodelist * ndlp;
1501         struct lpfc_hba *phba = vport->phba;
1502         struct lpfc_dmabuf *mp, *bmp;
1503         struct lpfc_sli_ct_request *CtReq;
1504         struct ulp_bde64 *bpl;
1505         void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
1506                       struct lpfc_iocbq *) = NULL;
1507         uint32_t rsp_size = 1024;
1508         size_t   size;
1509         int rc = 0;
1510
1511         ndlp = lpfc_findnode_did(vport, NameServer_DID);
1512         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)
1513             || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) {
1514                 rc=1;
1515                 goto ns_cmd_exit;
1516         }
1517
1518         /* fill in BDEs for command */
1519         /* Allocate buffer for command payload */
1520         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1521         if (!mp) {
1522                 rc=2;
1523                 goto ns_cmd_exit;
1524         }
1525
1526         INIT_LIST_HEAD(&mp->list);
1527         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
1528         if (!mp->virt) {
1529                 rc=3;
1530                 goto ns_cmd_free_mp;
1531         }
1532
1533         /* Allocate buffer for Buffer ptr list */
1534         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1535         if (!bmp) {
1536                 rc=4;
1537                 goto ns_cmd_free_mpvirt;
1538         }
1539
1540         INIT_LIST_HEAD(&bmp->list);
1541         bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(bmp->phys));
1542         if (!bmp->virt) {
1543                 rc=5;
1544                 goto ns_cmd_free_bmp;
1545         }
1546
1547         /* NameServer Req */
1548         lpfc_printf_vlog(vport, KERN_INFO ,LOG_DISCOVERY,
1549                          "0236 NameServer Req Data: x%x x%x x%x x%x\n",
1550                          cmdcode, vport->fc_flag, vport->fc_rscn_id_cnt,
1551                          context);
1552
1553         bpl = (struct ulp_bde64 *) bmp->virt;
1554         memset(bpl, 0, sizeof(struct ulp_bde64));
1555         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
1556         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
1557         bpl->tus.f.bdeFlags = 0;
1558         if (cmdcode == SLI_CTNS_GID_FT)
1559                 bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1560         else if (cmdcode == SLI_CTNS_GID_PT)
1561                 bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1562         else if (cmdcode == SLI_CTNS_GFF_ID)
1563                 bpl->tus.f.bdeSize = GFF_REQUEST_SZ;
1564         else if (cmdcode == SLI_CTNS_GFT_ID)
1565                 bpl->tus.f.bdeSize = GFT_REQUEST_SZ;
1566         else if (cmdcode == SLI_CTNS_RFT_ID)
1567                 bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
1568         else if (cmdcode == SLI_CTNS_RNN_ID)
1569                 bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
1570         else if (cmdcode == SLI_CTNS_RSPN_ID)
1571                 bpl->tus.f.bdeSize = RSPN_REQUEST_SZ;
1572         else if (cmdcode == SLI_CTNS_RSNN_NN)
1573                 bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
1574         else if (cmdcode == SLI_CTNS_DA_ID)
1575                 bpl->tus.f.bdeSize = DA_ID_REQUEST_SZ;
1576         else if (cmdcode == SLI_CTNS_RFF_ID)
1577                 bpl->tus.f.bdeSize = RFF_REQUEST_SZ;
1578         else
1579                 bpl->tus.f.bdeSize = 0;
1580         bpl->tus.w = le32_to_cpu(bpl->tus.w);
1581
1582         CtReq = (struct lpfc_sli_ct_request *) mp->virt;
1583         memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
1584         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
1585         CtReq->RevisionId.bits.InId = 0;
1586         CtReq->FsType = SLI_CT_DIRECTORY_SERVICE;
1587         CtReq->FsSubType = SLI_CT_DIRECTORY_NAME_SERVER;
1588         CtReq->CommandResponse.bits.Size = 0;
1589         switch (cmdcode) {
1590         case SLI_CTNS_GID_FT:
1591                 CtReq->CommandResponse.bits.CmdRsp =
1592                     cpu_to_be16(SLI_CTNS_GID_FT);
1593                 CtReq->un.gid.Fc4Type = context;
1594
1595                 if (vport->port_state < LPFC_NS_QRY)
1596                         vport->port_state = LPFC_NS_QRY;
1597                 lpfc_set_disctmo(vport);
1598                 cmpl = lpfc_cmpl_ct_cmd_gid_ft;
1599                 rsp_size = FC_MAX_NS_RSP;
1600                 break;
1601
1602         case SLI_CTNS_GID_PT:
1603                 CtReq->CommandResponse.bits.CmdRsp =
1604                     cpu_to_be16(SLI_CTNS_GID_PT);
1605                 CtReq->un.gid.PortType = context;
1606
1607                 if (vport->port_state < LPFC_NS_QRY)
1608                         vport->port_state = LPFC_NS_QRY;
1609                 lpfc_set_disctmo(vport);
1610                 cmpl = lpfc_cmpl_ct_cmd_gid_pt;
1611                 rsp_size = FC_MAX_NS_RSP;
1612                 break;
1613
1614         case SLI_CTNS_GFF_ID:
1615                 CtReq->CommandResponse.bits.CmdRsp =
1616                         cpu_to_be16(SLI_CTNS_GFF_ID);
1617                 CtReq->un.gff.PortId = cpu_to_be32(context);
1618                 cmpl = lpfc_cmpl_ct_cmd_gff_id;
1619                 break;
1620
1621         case SLI_CTNS_GFT_ID:
1622                 CtReq->CommandResponse.bits.CmdRsp =
1623                         cpu_to_be16(SLI_CTNS_GFT_ID);
1624                 CtReq->un.gft.PortId = cpu_to_be32(context);
1625                 cmpl = lpfc_cmpl_ct_cmd_gft_id;
1626                 break;
1627
1628         case SLI_CTNS_RFT_ID:
1629                 vport->ct_flags &= ~FC_CT_RFT_ID;
1630                 CtReq->CommandResponse.bits.CmdRsp =
1631                     cpu_to_be16(SLI_CTNS_RFT_ID);
1632                 CtReq->un.rft.PortId = cpu_to_be32(vport->fc_myDID);
1633
1634                 /* Register FC4 FCP type if enabled.  */
1635                 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1636                     (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
1637                         CtReq->un.rft.fcpReg = 1;
1638
1639                 /* Register NVME type if enabled.  Defined LE and swapped.
1640                  * rsvd[0] is used as word1 because of the hard-coded
1641                  * word0 usage in the ct_request data structure.
1642                  */
1643                 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1644                     (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
1645                         CtReq->un.rft.rsvd[0] = cpu_to_be32(0x00000100);
1646
1647                 cmpl = lpfc_cmpl_ct_cmd_rft_id;
1648                 break;
1649
1650         case SLI_CTNS_RNN_ID:
1651                 vport->ct_flags &= ~FC_CT_RNN_ID;
1652                 CtReq->CommandResponse.bits.CmdRsp =
1653                     cpu_to_be16(SLI_CTNS_RNN_ID);
1654                 CtReq->un.rnn.PortId = cpu_to_be32(vport->fc_myDID);
1655                 memcpy(CtReq->un.rnn.wwnn,  &vport->fc_nodename,
1656                        sizeof(struct lpfc_name));
1657                 cmpl = lpfc_cmpl_ct_cmd_rnn_id;
1658                 break;
1659
1660         case SLI_CTNS_RSPN_ID:
1661                 vport->ct_flags &= ~FC_CT_RSPN_ID;
1662                 CtReq->CommandResponse.bits.CmdRsp =
1663                     cpu_to_be16(SLI_CTNS_RSPN_ID);
1664                 CtReq->un.rspn.PortId = cpu_to_be32(vport->fc_myDID);
1665                 size = sizeof(CtReq->un.rspn.symbname);
1666                 CtReq->un.rspn.len =
1667                         lpfc_vport_symbolic_port_name(vport,
1668                         CtReq->un.rspn.symbname, size);
1669                 cmpl = lpfc_cmpl_ct_cmd_rspn_id;
1670                 break;
1671         case SLI_CTNS_RSNN_NN:
1672                 vport->ct_flags &= ~FC_CT_RSNN_NN;
1673                 CtReq->CommandResponse.bits.CmdRsp =
1674                     cpu_to_be16(SLI_CTNS_RSNN_NN);
1675                 memcpy(CtReq->un.rsnn.wwnn, &vport->fc_nodename,
1676                        sizeof(struct lpfc_name));
1677                 size = sizeof(CtReq->un.rsnn.symbname);
1678                 CtReq->un.rsnn.len =
1679                         lpfc_vport_symbolic_node_name(vport,
1680                         CtReq->un.rsnn.symbname, size);
1681                 cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
1682                 break;
1683         case SLI_CTNS_DA_ID:
1684                 /* Implement DA_ID Nameserver request */
1685                 CtReq->CommandResponse.bits.CmdRsp =
1686                         cpu_to_be16(SLI_CTNS_DA_ID);
1687                 CtReq->un.da_id.port_id = cpu_to_be32(vport->fc_myDID);
1688                 cmpl = lpfc_cmpl_ct_cmd_da_id;
1689                 break;
1690         case SLI_CTNS_RFF_ID:
1691                 vport->ct_flags &= ~FC_CT_RFF_ID;
1692                 CtReq->CommandResponse.bits.CmdRsp =
1693                     cpu_to_be16(SLI_CTNS_RFF_ID);
1694                 CtReq->un.rff.PortId = cpu_to_be32(vport->fc_myDID);
1695                 CtReq->un.rff.fbits = FC4_FEATURE_INIT;
1696
1697                 /* The driver always supports FC_TYPE_FCP.  However, the
1698                  * caller can specify NVME (type x28) as well.  But only
1699                  * these that FC4 type is supported.
1700                  */
1701                 if (((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1702                      (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
1703                     (context == FC_TYPE_NVME)) {
1704                         if ((vport == phba->pport) && phba->nvmet_support) {
1705                                 CtReq->un.rff.fbits = (FC4_FEATURE_TARGET |
1706                                         FC4_FEATURE_NVME_DISC);
1707                                 lpfc_nvmet_update_targetport(phba);
1708                         } else {
1709                                 lpfc_nvme_update_localport(vport);
1710                         }
1711                         CtReq->un.rff.type_code = context;
1712
1713                 } else if (((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1714                             (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) &&
1715                            (context == FC_TYPE_FCP))
1716                         CtReq->un.rff.type_code = context;
1717
1718                 else
1719                         goto ns_cmd_free_bmpvirt;
1720
1721                 cmpl = lpfc_cmpl_ct_cmd_rff_id;
1722                 break;
1723         }
1724         /* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
1725          * to hold ndlp reference for the corresponding callback function.
1726          */
1727         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry)) {
1728                 /* On success, The cmpl function will free the buffers */
1729                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1730                         "Issue CT cmd:    cmd:x%x did:x%x",
1731                         cmdcode, ndlp->nlp_DID, 0);
1732                 return 0;
1733         }
1734         rc=6;
1735
1736         /* Decrement ndlp reference count to release ndlp reference held
1737          * for the failed command's callback function.
1738          */
1739         lpfc_nlp_put(ndlp);
1740
1741 ns_cmd_free_bmpvirt:
1742         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1743 ns_cmd_free_bmp:
1744         kfree(bmp);
1745 ns_cmd_free_mpvirt:
1746         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1747 ns_cmd_free_mp:
1748         kfree(mp);
1749 ns_cmd_exit:
1750         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1751                          "0266 Issue NameServer Req x%x err %d Data: x%x x%x\n",
1752                          cmdcode, rc, vport->fc_flag, vport->fc_rscn_id_cnt);
1753         return 1;
1754 }
1755
1756 /**
1757  * lpfc_cmpl_ct_disc_fdmi - Handle a discovery FDMI completion
1758  * @phba: Pointer to HBA context object.
1759  * @cmdiocb: Pointer to the command IOCBQ.
1760  * @rspiocb: Pointer to the response IOCBQ.
1761  *
1762  * This function to handle the completion of a driver initiated FDMI
1763  * CT command issued during discovery.
1764  */
1765 static void
1766 lpfc_cmpl_ct_disc_fdmi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1767                        struct lpfc_iocbq *rspiocb)
1768 {
1769         struct lpfc_vport *vport = cmdiocb->vport;
1770         struct lpfc_dmabuf *inp = cmdiocb->context1;
1771         struct lpfc_dmabuf *outp = cmdiocb->context2;
1772         struct lpfc_sli_ct_request *CTcmd = inp->virt;
1773         struct lpfc_sli_ct_request *CTrsp = outp->virt;
1774         uint16_t fdmi_cmd = CTcmd->CommandResponse.bits.CmdRsp;
1775         uint16_t fdmi_rsp = CTrsp->CommandResponse.bits.CmdRsp;
1776         IOCB_t *irsp = &rspiocb->iocb;
1777         struct lpfc_nodelist *ndlp;
1778         uint32_t latt, cmd, err;
1779
1780         latt = lpfc_els_chk_latt(vport);
1781         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1782                 "FDMI cmpl:       status:x%x/x%x latt:%d",
1783                 irsp->ulpStatus, irsp->un.ulpWord[4], latt);
1784
1785         if (latt || irsp->ulpStatus) {
1786
1787                 /* Look for a retryable error */
1788                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1789                         switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
1790                         case IOERR_SLI_ABORTED:
1791                         case IOERR_ABORT_IN_PROGRESS:
1792                         case IOERR_SEQUENCE_TIMEOUT:
1793                         case IOERR_ILLEGAL_FRAME:
1794                         case IOERR_NO_RESOURCES:
1795                         case IOERR_ILLEGAL_COMMAND:
1796                                 cmdiocb->retry++;
1797                                 if (cmdiocb->retry >= LPFC_FDMI_MAX_RETRY)
1798                                         break;
1799
1800                                 /* Retry the same FDMI command */
1801                                 err = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING,
1802                                                           cmdiocb, 0);
1803                                 if (err == IOCB_ERROR)
1804                                         break;
1805                                 return;
1806                         default:
1807                                 break;
1808                         }
1809                 }
1810
1811                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1812                                  "0229 FDMI cmd %04x failed, latt = %d "
1813                                  "ulpStatus: x%x, rid x%x\n",
1814                                  be16_to_cpu(fdmi_cmd), latt, irsp->ulpStatus,
1815                                  irsp->un.ulpWord[4]);
1816         }
1817         lpfc_ct_free_iocb(phba, cmdiocb);
1818
1819         ndlp = lpfc_findnode_did(vport, FDMI_DID);
1820         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
1821                 return;
1822
1823         /* Check for a CT LS_RJT response */
1824         cmd =  be16_to_cpu(fdmi_cmd);
1825         if (fdmi_rsp == cpu_to_be16(SLI_CT_RESPONSE_FS_RJT)) {
1826                 /* FDMI rsp failed */
1827                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1828                                  "0220 FDMI cmd failed FS_RJT Data: x%x", cmd);
1829
1830                 /* Should we fallback to FDMI-2 / FDMI-1 ? */
1831                 switch (cmd) {
1832                 case SLI_MGMT_RHBA:
1833                         if (vport->fdmi_hba_mask == LPFC_FDMI2_HBA_ATTR) {
1834                                 /* Fallback to FDMI-1 */
1835                                 vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
1836                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
1837                                 /* Start over */
1838                                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
1839                         }
1840                         return;
1841
1842                 case SLI_MGMT_RPRT:
1843                         if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
1844                                 /* Fallback to FDMI-1 */
1845                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
1846                                 /* Start over */
1847                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
1848                         }
1849                         if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
1850                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
1851                                 /* Retry the same command */
1852                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
1853                         }
1854                         return;
1855
1856                 case SLI_MGMT_RPA:
1857                         if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
1858                                 /* Fallback to FDMI-1 */
1859                                 vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
1860                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
1861                                 /* Start over */
1862                                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
1863                         }
1864                         if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
1865                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
1866                                 /* Retry the same command */
1867                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
1868                         }
1869                         return;
1870                 }
1871         }
1872
1873         /*
1874          * On success, need to cycle thru FDMI registration for discovery
1875          * DHBA -> DPRT -> RHBA -> RPA  (physical port)
1876          * DPRT -> RPRT (vports)
1877          */
1878         switch (cmd) {
1879         case SLI_MGMT_RHBA:
1880                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA, 0);
1881                 break;
1882
1883         case SLI_MGMT_DHBA:
1884                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
1885                 break;
1886
1887         case SLI_MGMT_DPRT:
1888                 if (vport->port_type == LPFC_PHYSICAL_PORT)
1889                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RHBA, 0);
1890                 else
1891                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT, 0);
1892                 break;
1893         }
1894         return;
1895 }
1896
1897
1898 /**
1899  * lpfc_fdmi_num_disc_check - Check how many mapped NPorts we are connected to
1900  * @vport: pointer to a host virtual N_Port data structure.
1901  *
1902  * Called from hbeat timeout routine to check if the number of discovered
1903  * ports has changed. If so, re-register thar port Attribute.
1904  */
1905 void
1906 lpfc_fdmi_num_disc_check(struct lpfc_vport *vport)
1907 {
1908         struct lpfc_hba *phba = vport->phba;
1909         struct lpfc_nodelist *ndlp;
1910         uint16_t cnt;
1911
1912         if (!lpfc_is_link_up(phba))
1913                 return;
1914
1915         /* Must be connected to a Fabric */
1916         if (!(vport->fc_flag & FC_FABRIC))
1917                 return;
1918
1919         if (!(vport->fdmi_port_mask & LPFC_FDMI_PORT_ATTR_num_disc))
1920                 return;
1921
1922         cnt = lpfc_find_map_node(vport);
1923         if (cnt == vport->fdmi_num_disc)
1924                 return;
1925
1926         ndlp = lpfc_findnode_did(vport, FDMI_DID);
1927         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
1928                 return;
1929
1930         if (vport->port_type == LPFC_PHYSICAL_PORT) {
1931                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA,
1932                               LPFC_FDMI_PORT_ATTR_num_disc);
1933         } else {
1934                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT,
1935                               LPFC_FDMI_PORT_ATTR_num_disc);
1936         }
1937 }
1938
1939 /* Routines for all individual HBA attributes */
1940 static int
1941 lpfc_fdmi_hba_attr_wwnn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
1942 {
1943         struct lpfc_fdmi_attr_entry *ae;
1944         uint32_t size;
1945
1946         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
1947         memset(ae, 0, sizeof(struct lpfc_name));
1948
1949         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
1950                sizeof(struct lpfc_name));
1951         size = FOURBYTES + sizeof(struct lpfc_name);
1952         ad->AttrLen = cpu_to_be16(size);
1953         ad->AttrType = cpu_to_be16(RHBA_NODENAME);
1954         return size;
1955 }
1956 static int
1957 lpfc_fdmi_hba_attr_manufacturer(struct lpfc_vport *vport,
1958                                 struct lpfc_fdmi_attr_def *ad)
1959 {
1960         struct lpfc_fdmi_attr_entry *ae;
1961         uint32_t len, size;
1962
1963         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
1964         memset(ae, 0, 256);
1965
1966         strncpy(ae->un.AttrString,
1967                 "Emulex Corporation",
1968                        sizeof(ae->un.AttrString));
1969         len = strnlen(ae->un.AttrString,
1970                           sizeof(ae->un.AttrString));
1971         len += (len & 3) ? (4 - (len & 3)) : 4;
1972         size = FOURBYTES + len;
1973         ad->AttrLen = cpu_to_be16(size);
1974         ad->AttrType = cpu_to_be16(RHBA_MANUFACTURER);
1975         return size;
1976 }
1977
1978 static int
1979 lpfc_fdmi_hba_attr_sn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
1980 {
1981         struct lpfc_hba *phba = vport->phba;
1982         struct lpfc_fdmi_attr_entry *ae;
1983         uint32_t len, size;
1984
1985         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
1986         memset(ae, 0, 256);
1987
1988         strncpy(ae->un.AttrString, phba->SerialNumber,
1989                 sizeof(ae->un.AttrString));
1990         len = strnlen(ae->un.AttrString,
1991                           sizeof(ae->un.AttrString));
1992         len += (len & 3) ? (4 - (len & 3)) : 4;
1993         size = FOURBYTES + len;
1994         ad->AttrLen = cpu_to_be16(size);
1995         ad->AttrType = cpu_to_be16(RHBA_SERIAL_NUMBER);
1996         return size;
1997 }
1998
1999 static int
2000 lpfc_fdmi_hba_attr_model(struct lpfc_vport *vport,
2001                          struct lpfc_fdmi_attr_def *ad)
2002 {
2003         struct lpfc_hba *phba = vport->phba;
2004         struct lpfc_fdmi_attr_entry *ae;
2005         uint32_t len, size;
2006
2007         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2008         memset(ae, 0, 256);
2009
2010         strncpy(ae->un.AttrString, phba->ModelName,
2011                 sizeof(ae->un.AttrString));
2012         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2013         len += (len & 3) ? (4 - (len & 3)) : 4;
2014         size = FOURBYTES + len;
2015         ad->AttrLen = cpu_to_be16(size);
2016         ad->AttrType = cpu_to_be16(RHBA_MODEL);
2017         return size;
2018 }
2019
2020 static int
2021 lpfc_fdmi_hba_attr_description(struct lpfc_vport *vport,
2022                                struct lpfc_fdmi_attr_def *ad)
2023 {
2024         struct lpfc_hba *phba = vport->phba;
2025         struct lpfc_fdmi_attr_entry *ae;
2026         uint32_t len, size;
2027
2028         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2029         memset(ae, 0, 256);
2030
2031         strncpy(ae->un.AttrString, phba->ModelDesc,
2032                 sizeof(ae->un.AttrString));
2033         len = strnlen(ae->un.AttrString,
2034                                   sizeof(ae->un.AttrString));
2035         len += (len & 3) ? (4 - (len & 3)) : 4;
2036         size = FOURBYTES + len;
2037         ad->AttrLen = cpu_to_be16(size);
2038         ad->AttrType = cpu_to_be16(RHBA_MODEL_DESCRIPTION);
2039         return size;
2040 }
2041
2042 static int
2043 lpfc_fdmi_hba_attr_hdw_ver(struct lpfc_vport *vport,
2044                            struct lpfc_fdmi_attr_def *ad)
2045 {
2046         struct lpfc_hba *phba = vport->phba;
2047         lpfc_vpd_t *vp = &phba->vpd;
2048         struct lpfc_fdmi_attr_entry *ae;
2049         uint32_t i, j, incr, size;
2050
2051         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2052         memset(ae, 0, 256);
2053
2054         /* Convert JEDEC ID to ascii for hardware version */
2055         incr = vp->rev.biuRev;
2056         for (i = 0; i < 8; i++) {
2057                 j = (incr & 0xf);
2058                 if (j <= 9)
2059                         ae->un.AttrString[7 - i] =
2060                             (char)((uint8_t) 0x30 +
2061                                    (uint8_t) j);
2062                 else
2063                         ae->un.AttrString[7 - i] =
2064                             (char)((uint8_t) 0x61 +
2065                                    (uint8_t) (j - 10));
2066                 incr = (incr >> 4);
2067         }
2068         size = FOURBYTES + 8;
2069         ad->AttrLen = cpu_to_be16(size);
2070         ad->AttrType = cpu_to_be16(RHBA_HARDWARE_VERSION);
2071         return size;
2072 }
2073
2074 static int
2075 lpfc_fdmi_hba_attr_drvr_ver(struct lpfc_vport *vport,
2076                             struct lpfc_fdmi_attr_def *ad)
2077 {
2078         struct lpfc_fdmi_attr_entry *ae;
2079         uint32_t len, size;
2080
2081         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2082         memset(ae, 0, 256);
2083
2084         strncpy(ae->un.AttrString, lpfc_release_version,
2085                 sizeof(ae->un.AttrString));
2086         len = strnlen(ae->un.AttrString,
2087                           sizeof(ae->un.AttrString));
2088         len += (len & 3) ? (4 - (len & 3)) : 4;
2089         size = FOURBYTES + len;
2090         ad->AttrLen = cpu_to_be16(size);
2091         ad->AttrType = cpu_to_be16(RHBA_DRIVER_VERSION);
2092         return size;
2093 }
2094
2095 static int
2096 lpfc_fdmi_hba_attr_rom_ver(struct lpfc_vport *vport,
2097                            struct lpfc_fdmi_attr_def *ad)
2098 {
2099         struct lpfc_hba *phba = vport->phba;
2100         struct lpfc_fdmi_attr_entry *ae;
2101         uint32_t len, size;
2102
2103         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2104         memset(ae, 0, 256);
2105
2106         if (phba->sli_rev == LPFC_SLI_REV4)
2107                 lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2108         else
2109                 strncpy(ae->un.AttrString, phba->OptionROMVersion,
2110                         sizeof(ae->un.AttrString));
2111         len = strnlen(ae->un.AttrString,
2112                           sizeof(ae->un.AttrString));
2113         len += (len & 3) ? (4 - (len & 3)) : 4;
2114         size = FOURBYTES + len;
2115         ad->AttrLen = cpu_to_be16(size);
2116         ad->AttrType = cpu_to_be16(RHBA_OPTION_ROM_VERSION);
2117         return size;
2118 }
2119
2120 static int
2121 lpfc_fdmi_hba_attr_fmw_ver(struct lpfc_vport *vport,
2122                            struct lpfc_fdmi_attr_def *ad)
2123 {
2124         struct lpfc_hba *phba = vport->phba;
2125         struct lpfc_fdmi_attr_entry *ae;
2126         uint32_t len, size;
2127
2128         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2129         memset(ae, 0, 256);
2130
2131         lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2132         len = strnlen(ae->un.AttrString,
2133                           sizeof(ae->un.AttrString));
2134         len += (len & 3) ? (4 - (len & 3)) : 4;
2135         size = FOURBYTES + len;
2136         ad->AttrLen = cpu_to_be16(size);
2137         ad->AttrType = cpu_to_be16(RHBA_FIRMWARE_VERSION);
2138         return size;
2139 }
2140
2141 static int
2142 lpfc_fdmi_hba_attr_os_ver(struct lpfc_vport *vport,
2143                           struct lpfc_fdmi_attr_def *ad)
2144 {
2145         struct lpfc_fdmi_attr_entry *ae;
2146         uint32_t len, size;
2147
2148         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2149         memset(ae, 0, 256);
2150
2151         snprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s %s %s",
2152                  init_utsname()->sysname,
2153                  init_utsname()->release,
2154                  init_utsname()->version);
2155
2156         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2157         len += (len & 3) ? (4 - (len & 3)) : 4;
2158         size = FOURBYTES + len;
2159         ad->AttrLen = cpu_to_be16(size);
2160         ad->AttrType = cpu_to_be16(RHBA_OS_NAME_VERSION);
2161         return size;
2162 }
2163
2164 static int
2165 lpfc_fdmi_hba_attr_ct_len(struct lpfc_vport *vport,
2166                           struct lpfc_fdmi_attr_def *ad)
2167 {
2168         struct lpfc_fdmi_attr_entry *ae;
2169         uint32_t size;
2170
2171         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2172
2173         ae->un.AttrInt =  cpu_to_be32(LPFC_MAX_CT_SIZE);
2174         size = FOURBYTES + sizeof(uint32_t);
2175         ad->AttrLen = cpu_to_be16(size);
2176         ad->AttrType = cpu_to_be16(RHBA_MAX_CT_PAYLOAD_LEN);
2177         return size;
2178 }
2179
2180 static int
2181 lpfc_fdmi_hba_attr_symbolic_name(struct lpfc_vport *vport,
2182                                  struct lpfc_fdmi_attr_def *ad)
2183 {
2184         struct lpfc_fdmi_attr_entry *ae;
2185         uint32_t len, size;
2186
2187         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2188         memset(ae, 0, 256);
2189
2190         len = lpfc_vport_symbolic_node_name(vport,
2191                                 ae->un.AttrString, 256);
2192         len += (len & 3) ? (4 - (len & 3)) : 4;
2193         size = FOURBYTES + len;
2194         ad->AttrLen = cpu_to_be16(size);
2195         ad->AttrType = cpu_to_be16(RHBA_SYM_NODENAME);
2196         return size;
2197 }
2198
2199 static int
2200 lpfc_fdmi_hba_attr_vendor_info(struct lpfc_vport *vport,
2201                                struct lpfc_fdmi_attr_def *ad)
2202 {
2203         struct lpfc_fdmi_attr_entry *ae;
2204         uint32_t size;
2205
2206         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2207
2208         /* Nothing is defined for this currently */
2209         ae->un.AttrInt =  cpu_to_be32(0);
2210         size = FOURBYTES + sizeof(uint32_t);
2211         ad->AttrLen = cpu_to_be16(size);
2212         ad->AttrType = cpu_to_be16(RHBA_VENDOR_INFO);
2213         return size;
2214 }
2215
2216 static int
2217 lpfc_fdmi_hba_attr_num_ports(struct lpfc_vport *vport,
2218                              struct lpfc_fdmi_attr_def *ad)
2219 {
2220         struct lpfc_fdmi_attr_entry *ae;
2221         uint32_t size;
2222
2223         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2224
2225         /* Each driver instance corresponds to a single port */
2226         ae->un.AttrInt =  cpu_to_be32(1);
2227         size = FOURBYTES + sizeof(uint32_t);
2228         ad->AttrLen = cpu_to_be16(size);
2229         ad->AttrType = cpu_to_be16(RHBA_NUM_PORTS);
2230         return size;
2231 }
2232
2233 static int
2234 lpfc_fdmi_hba_attr_fabric_wwnn(struct lpfc_vport *vport,
2235                                struct lpfc_fdmi_attr_def *ad)
2236 {
2237         struct lpfc_fdmi_attr_entry *ae;
2238         uint32_t size;
2239
2240         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2241         memset(ae, 0, sizeof(struct lpfc_name));
2242
2243         memcpy(&ae->un.AttrWWN, &vport->fabric_nodename,
2244                sizeof(struct lpfc_name));
2245         size = FOURBYTES + sizeof(struct lpfc_name);
2246         ad->AttrLen = cpu_to_be16(size);
2247         ad->AttrType = cpu_to_be16(RHBA_FABRIC_WWNN);
2248         return size;
2249 }
2250
2251 static int
2252 lpfc_fdmi_hba_attr_bios_ver(struct lpfc_vport *vport,
2253                             struct lpfc_fdmi_attr_def *ad)
2254 {
2255         struct lpfc_hba *phba = vport->phba;
2256         struct lpfc_fdmi_attr_entry *ae;
2257         uint32_t len, size;
2258
2259         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2260         memset(ae, 0, 256);
2261
2262         lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2263         len = strnlen(ae->un.AttrString,
2264                           sizeof(ae->un.AttrString));
2265         len += (len & 3) ? (4 - (len & 3)) : 4;
2266         size = FOURBYTES + len;
2267         ad->AttrLen = cpu_to_be16(size);
2268         ad->AttrType = cpu_to_be16(RHBA_BIOS_VERSION);
2269         return size;
2270 }
2271
2272 static int
2273 lpfc_fdmi_hba_attr_bios_state(struct lpfc_vport *vport,
2274                               struct lpfc_fdmi_attr_def *ad)
2275 {
2276         struct lpfc_fdmi_attr_entry *ae;
2277         uint32_t size;
2278
2279         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2280
2281         /* Driver doesn't have access to this information */
2282         ae->un.AttrInt =  cpu_to_be32(0);
2283         size = FOURBYTES + sizeof(uint32_t);
2284         ad->AttrLen = cpu_to_be16(size);
2285         ad->AttrType = cpu_to_be16(RHBA_BIOS_STATE);
2286         return size;
2287 }
2288
2289 static int
2290 lpfc_fdmi_hba_attr_vendor_id(struct lpfc_vport *vport,
2291                              struct lpfc_fdmi_attr_def *ad)
2292 {
2293         struct lpfc_fdmi_attr_entry *ae;
2294         uint32_t len, size;
2295
2296         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2297         memset(ae, 0, 256);
2298
2299         strncpy(ae->un.AttrString, "EMULEX",
2300                 sizeof(ae->un.AttrString));
2301         len = strnlen(ae->un.AttrString,
2302                           sizeof(ae->un.AttrString));
2303         len += (len & 3) ? (4 - (len & 3)) : 4;
2304         size = FOURBYTES + len;
2305         ad->AttrLen = cpu_to_be16(size);
2306         ad->AttrType = cpu_to_be16(RHBA_VENDOR_ID);
2307         return size;
2308 }
2309
2310 /* Routines for all individual PORT attributes */
2311 static int
2312 lpfc_fdmi_port_attr_fc4type(struct lpfc_vport *vport,
2313                             struct lpfc_fdmi_attr_def *ad)
2314 {
2315         struct lpfc_fdmi_attr_entry *ae;
2316         uint32_t size;
2317
2318         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2319         memset(ae, 0, 32);
2320
2321         ae->un.AttrTypes[3] = 0x02; /* Type 1 - ELS */
2322         ae->un.AttrTypes[2] = 0x01; /* Type 8 - FCP */
2323         ae->un.AttrTypes[6] = 0x01; /* Type 40 - NVME */
2324         ae->un.AttrTypes[7] = 0x01; /* Type 32 - CT */
2325         size = FOURBYTES + 32;
2326         ad->AttrLen = cpu_to_be16(size);
2327         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_FC4_TYPES);
2328         return size;
2329 }
2330
2331 static int
2332 lpfc_fdmi_port_attr_support_speed(struct lpfc_vport *vport,
2333                                   struct lpfc_fdmi_attr_def *ad)
2334 {
2335         struct lpfc_hba   *phba = vport->phba;
2336         struct lpfc_fdmi_attr_entry *ae;
2337         uint32_t size;
2338
2339         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2340
2341         ae->un.AttrInt = 0;
2342         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2343                 if (phba->lmt & LMT_128Gb)
2344                         ae->un.AttrInt |= HBA_PORTSPEED_128GFC;
2345                 if (phba->lmt & LMT_64Gb)
2346                         ae->un.AttrInt |= HBA_PORTSPEED_64GFC;
2347                 if (phba->lmt & LMT_32Gb)
2348                         ae->un.AttrInt |= HBA_PORTSPEED_32GFC;
2349                 if (phba->lmt & LMT_16Gb)
2350                         ae->un.AttrInt |= HBA_PORTSPEED_16GFC;
2351                 if (phba->lmt & LMT_10Gb)
2352                         ae->un.AttrInt |= HBA_PORTSPEED_10GFC;
2353                 if (phba->lmt & LMT_8Gb)
2354                         ae->un.AttrInt |= HBA_PORTSPEED_8GFC;
2355                 if (phba->lmt & LMT_4Gb)
2356                         ae->un.AttrInt |= HBA_PORTSPEED_4GFC;
2357                 if (phba->lmt & LMT_2Gb)
2358                         ae->un.AttrInt |= HBA_PORTSPEED_2GFC;
2359                 if (phba->lmt & LMT_1Gb)
2360                         ae->un.AttrInt |= HBA_PORTSPEED_1GFC;
2361         } else {
2362                 /* FCoE links support only one speed */
2363                 switch (phba->fc_linkspeed) {
2364                 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2365                         ae->un.AttrInt = HBA_PORTSPEED_10GE;
2366                         break;
2367                 case LPFC_ASYNC_LINK_SPEED_25GBPS:
2368                         ae->un.AttrInt = HBA_PORTSPEED_25GE;
2369                         break;
2370                 case LPFC_ASYNC_LINK_SPEED_40GBPS:
2371                         ae->un.AttrInt = HBA_PORTSPEED_40GE;
2372                         break;
2373                 case LPFC_ASYNC_LINK_SPEED_100GBPS:
2374                         ae->un.AttrInt = HBA_PORTSPEED_100GE;
2375                         break;
2376                 }
2377         }
2378         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2379         size = FOURBYTES + sizeof(uint32_t);
2380         ad->AttrLen = cpu_to_be16(size);
2381         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_SPEED);
2382         return size;
2383 }
2384
2385 static int
2386 lpfc_fdmi_port_attr_speed(struct lpfc_vport *vport,
2387                           struct lpfc_fdmi_attr_def *ad)
2388 {
2389         struct lpfc_hba   *phba = vport->phba;
2390         struct lpfc_fdmi_attr_entry *ae;
2391         uint32_t size;
2392
2393         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2394
2395         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2396                 switch (phba->fc_linkspeed) {
2397                 case LPFC_LINK_SPEED_1GHZ:
2398                         ae->un.AttrInt = HBA_PORTSPEED_1GFC;
2399                         break;
2400                 case LPFC_LINK_SPEED_2GHZ:
2401                         ae->un.AttrInt = HBA_PORTSPEED_2GFC;
2402                         break;
2403                 case LPFC_LINK_SPEED_4GHZ:
2404                         ae->un.AttrInt = HBA_PORTSPEED_4GFC;
2405                         break;
2406                 case LPFC_LINK_SPEED_8GHZ:
2407                         ae->un.AttrInt = HBA_PORTSPEED_8GFC;
2408                         break;
2409                 case LPFC_LINK_SPEED_10GHZ:
2410                         ae->un.AttrInt = HBA_PORTSPEED_10GFC;
2411                         break;
2412                 case LPFC_LINK_SPEED_16GHZ:
2413                         ae->un.AttrInt = HBA_PORTSPEED_16GFC;
2414                         break;
2415                 case LPFC_LINK_SPEED_32GHZ:
2416                         ae->un.AttrInt = HBA_PORTSPEED_32GFC;
2417                         break;
2418                 case LPFC_LINK_SPEED_64GHZ:
2419                         ae->un.AttrInt = HBA_PORTSPEED_64GFC;
2420                         break;
2421                 case LPFC_LINK_SPEED_128GHZ:
2422                         ae->un.AttrInt = HBA_PORTSPEED_128GFC;
2423                         break;
2424                 default:
2425                         ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2426                         break;
2427                 }
2428         } else {
2429                 switch (phba->fc_linkspeed) {
2430                 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2431                         ae->un.AttrInt = HBA_PORTSPEED_10GE;
2432                         break;
2433                 case LPFC_ASYNC_LINK_SPEED_25GBPS:
2434                         ae->un.AttrInt = HBA_PORTSPEED_25GE;
2435                         break;
2436                 case LPFC_ASYNC_LINK_SPEED_40GBPS:
2437                         ae->un.AttrInt = HBA_PORTSPEED_40GE;
2438                         break;
2439                 case LPFC_ASYNC_LINK_SPEED_100GBPS:
2440                         ae->un.AttrInt = HBA_PORTSPEED_100GE;
2441                         break;
2442                 default:
2443                         ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2444                         break;
2445                 }
2446         }
2447
2448         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2449         size = FOURBYTES + sizeof(uint32_t);
2450         ad->AttrLen = cpu_to_be16(size);
2451         ad->AttrType = cpu_to_be16(RPRT_PORT_SPEED);
2452         return size;
2453 }
2454
2455 static int
2456 lpfc_fdmi_port_attr_max_frame(struct lpfc_vport *vport,
2457                               struct lpfc_fdmi_attr_def *ad)
2458 {
2459         struct serv_parm *hsp;
2460         struct lpfc_fdmi_attr_entry *ae;
2461         uint32_t size;
2462
2463         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2464
2465         hsp = (struct serv_parm *)&vport->fc_sparam;
2466         ae->un.AttrInt = (((uint32_t) hsp->cmn.bbRcvSizeMsb) << 8) |
2467                           (uint32_t) hsp->cmn.bbRcvSizeLsb;
2468         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2469         size = FOURBYTES + sizeof(uint32_t);
2470         ad->AttrLen = cpu_to_be16(size);
2471         ad->AttrType = cpu_to_be16(RPRT_MAX_FRAME_SIZE);
2472         return size;
2473 }
2474
2475 static int
2476 lpfc_fdmi_port_attr_os_devname(struct lpfc_vport *vport,
2477                                struct lpfc_fdmi_attr_def *ad)
2478 {
2479         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2480         struct lpfc_fdmi_attr_entry *ae;
2481         uint32_t len, size;
2482
2483         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2484         memset(ae, 0, 256);
2485
2486         snprintf(ae->un.AttrString, sizeof(ae->un.AttrString),
2487                  "/sys/class/scsi_host/host%d", shost->host_no);
2488         len = strnlen((char *)ae->un.AttrString,
2489                           sizeof(ae->un.AttrString));
2490         len += (len & 3) ? (4 - (len & 3)) : 4;
2491         size = FOURBYTES + len;
2492         ad->AttrLen = cpu_to_be16(size);
2493         ad->AttrType = cpu_to_be16(RPRT_OS_DEVICE_NAME);
2494         return size;
2495 }
2496
2497 static int
2498 lpfc_fdmi_port_attr_host_name(struct lpfc_vport *vport,
2499                               struct lpfc_fdmi_attr_def *ad)
2500 {
2501         struct lpfc_fdmi_attr_entry *ae;
2502         uint32_t len, size;
2503
2504         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2505         memset(ae, 0, 256);
2506
2507         snprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s",
2508                  init_utsname()->nodename);
2509
2510         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2511         len += (len & 3) ? (4 - (len & 3)) : 4;
2512         size = FOURBYTES + len;
2513         ad->AttrLen = cpu_to_be16(size);
2514         ad->AttrType = cpu_to_be16(RPRT_HOST_NAME);
2515         return size;
2516 }
2517
2518 static int
2519 lpfc_fdmi_port_attr_wwnn(struct lpfc_vport *vport,
2520                          struct lpfc_fdmi_attr_def *ad)
2521 {
2522         struct lpfc_fdmi_attr_entry *ae;
2523         uint32_t size;
2524
2525         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2526         memset(ae, 0,  sizeof(struct lpfc_name));
2527
2528         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
2529                sizeof(struct lpfc_name));
2530         size = FOURBYTES + sizeof(struct lpfc_name);
2531         ad->AttrLen = cpu_to_be16(size);
2532         ad->AttrType = cpu_to_be16(RPRT_NODENAME);
2533         return size;
2534 }
2535
2536 static int
2537 lpfc_fdmi_port_attr_wwpn(struct lpfc_vport *vport,
2538                          struct lpfc_fdmi_attr_def *ad)
2539 {
2540         struct lpfc_fdmi_attr_entry *ae;
2541         uint32_t size;
2542
2543         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2544         memset(ae, 0,  sizeof(struct lpfc_name));
2545
2546         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.portName,
2547                sizeof(struct lpfc_name));
2548         size = FOURBYTES + sizeof(struct lpfc_name);
2549         ad->AttrLen = cpu_to_be16(size);
2550         ad->AttrType = cpu_to_be16(RPRT_PORTNAME);
2551         return size;
2552 }
2553
2554 static int
2555 lpfc_fdmi_port_attr_symbolic_name(struct lpfc_vport *vport,
2556                                   struct lpfc_fdmi_attr_def *ad)
2557 {
2558         struct lpfc_fdmi_attr_entry *ae;
2559         uint32_t len, size;
2560
2561         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2562         memset(ae, 0, 256);
2563
2564         len = lpfc_vport_symbolic_port_name(vport, ae->un.AttrString, 256);
2565         len += (len & 3) ? (4 - (len & 3)) : 4;
2566         size = FOURBYTES + len;
2567         ad->AttrLen = cpu_to_be16(size);
2568         ad->AttrType = cpu_to_be16(RPRT_SYM_PORTNAME);
2569         return size;
2570 }
2571
2572 static int
2573 lpfc_fdmi_port_attr_port_type(struct lpfc_vport *vport,
2574                               struct lpfc_fdmi_attr_def *ad)
2575 {
2576         struct lpfc_hba *phba = vport->phba;
2577         struct lpfc_fdmi_attr_entry *ae;
2578         uint32_t size;
2579
2580         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2581         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP)
2582                 ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NLPORT);
2583         else
2584                 ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NPORT);
2585         size = FOURBYTES + sizeof(uint32_t);
2586         ad->AttrLen = cpu_to_be16(size);
2587         ad->AttrType = cpu_to_be16(RPRT_PORT_TYPE);
2588         return size;
2589 }
2590
2591 static int
2592 lpfc_fdmi_port_attr_class(struct lpfc_vport *vport,
2593                           struct lpfc_fdmi_attr_def *ad)
2594 {
2595         struct lpfc_fdmi_attr_entry *ae;
2596         uint32_t size;
2597
2598         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2599         ae->un.AttrInt = cpu_to_be32(FC_COS_CLASS2 | FC_COS_CLASS3);
2600         size = FOURBYTES + sizeof(uint32_t);
2601         ad->AttrLen = cpu_to_be16(size);
2602         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_CLASS);
2603         return size;
2604 }
2605
2606 static int
2607 lpfc_fdmi_port_attr_fabric_wwpn(struct lpfc_vport *vport,
2608                                 struct lpfc_fdmi_attr_def *ad)
2609 {
2610         struct lpfc_fdmi_attr_entry *ae;
2611         uint32_t size;
2612
2613         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2614         memset(ae, 0,  sizeof(struct lpfc_name));
2615
2616         memcpy(&ae->un.AttrWWN, &vport->fabric_portname,
2617                sizeof(struct lpfc_name));
2618         size = FOURBYTES + sizeof(struct lpfc_name);
2619         ad->AttrLen = cpu_to_be16(size);
2620         ad->AttrType = cpu_to_be16(RPRT_FABRICNAME);
2621         return size;
2622 }
2623
2624 static int
2625 lpfc_fdmi_port_attr_active_fc4type(struct lpfc_vport *vport,
2626                                    struct lpfc_fdmi_attr_def *ad)
2627 {
2628         struct lpfc_fdmi_attr_entry *ae;
2629         uint32_t size;
2630
2631         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2632         memset(ae, 0, 32);
2633
2634         ae->un.AttrTypes[3] = 0x02; /* Type 1 - ELS */
2635         ae->un.AttrTypes[2] = 0x01; /* Type 8 - FCP */
2636         ae->un.AttrTypes[7] = 0x01; /* Type 32 - CT */
2637         size = FOURBYTES + 32;
2638         ad->AttrLen = cpu_to_be16(size);
2639         ad->AttrType = cpu_to_be16(RPRT_ACTIVE_FC4_TYPES);
2640         return size;
2641 }
2642
2643 static int
2644 lpfc_fdmi_port_attr_port_state(struct lpfc_vport *vport,
2645                                struct lpfc_fdmi_attr_def *ad)
2646 {
2647         struct lpfc_fdmi_attr_entry *ae;
2648         uint32_t size;
2649
2650         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2651         /* Link Up - operational */
2652         ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTSTATE_ONLINE);
2653         size = FOURBYTES + sizeof(uint32_t);
2654         ad->AttrLen = cpu_to_be16(size);
2655         ad->AttrType = cpu_to_be16(RPRT_PORT_STATE);
2656         return size;
2657 }
2658
2659 static int
2660 lpfc_fdmi_port_attr_num_disc(struct lpfc_vport *vport,
2661                              struct lpfc_fdmi_attr_def *ad)
2662 {
2663         struct lpfc_fdmi_attr_entry *ae;
2664         uint32_t size;
2665
2666         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2667         vport->fdmi_num_disc = lpfc_find_map_node(vport);
2668         ae->un.AttrInt = cpu_to_be32(vport->fdmi_num_disc);
2669         size = FOURBYTES + sizeof(uint32_t);
2670         ad->AttrLen = cpu_to_be16(size);
2671         ad->AttrType = cpu_to_be16(RPRT_DISC_PORT);
2672         return size;
2673 }
2674
2675 static int
2676 lpfc_fdmi_port_attr_nportid(struct lpfc_vport *vport,
2677                             struct lpfc_fdmi_attr_def *ad)
2678 {
2679         struct lpfc_fdmi_attr_entry *ae;
2680         uint32_t size;
2681
2682         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2683         ae->un.AttrInt =  cpu_to_be32(vport->fc_myDID);
2684         size = FOURBYTES + sizeof(uint32_t);
2685         ad->AttrLen = cpu_to_be16(size);
2686         ad->AttrType = cpu_to_be16(RPRT_PORT_ID);
2687         return size;
2688 }
2689
2690 static int
2691 lpfc_fdmi_smart_attr_service(struct lpfc_vport *vport,
2692                              struct lpfc_fdmi_attr_def *ad)
2693 {
2694         struct lpfc_fdmi_attr_entry *ae;
2695         uint32_t len, size;
2696
2697         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2698         memset(ae, 0, 256);
2699
2700         strncpy(ae->un.AttrString, "Smart SAN Initiator",
2701                 sizeof(ae->un.AttrString));
2702         len = strnlen(ae->un.AttrString,
2703                           sizeof(ae->un.AttrString));
2704         len += (len & 3) ? (4 - (len & 3)) : 4;
2705         size = FOURBYTES + len;
2706         ad->AttrLen = cpu_to_be16(size);
2707         ad->AttrType = cpu_to_be16(RPRT_SMART_SERVICE);
2708         return size;
2709 }
2710
2711 static int
2712 lpfc_fdmi_smart_attr_guid(struct lpfc_vport *vport,
2713                           struct lpfc_fdmi_attr_def *ad)
2714 {
2715         struct lpfc_fdmi_attr_entry *ae;
2716         uint32_t size;
2717
2718         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2719         memset(ae, 0, 256);
2720
2721         memcpy(&ae->un.AttrString, &vport->fc_sparam.nodeName,
2722                sizeof(struct lpfc_name));
2723         memcpy((((uint8_t *)&ae->un.AttrString) +
2724                 sizeof(struct lpfc_name)),
2725                 &vport->fc_sparam.portName, sizeof(struct lpfc_name));
2726         size = FOURBYTES + (2 * sizeof(struct lpfc_name));
2727         ad->AttrLen =  cpu_to_be16(size);
2728         ad->AttrType = cpu_to_be16(RPRT_SMART_GUID);
2729         return size;
2730 }
2731
2732 static int
2733 lpfc_fdmi_smart_attr_version(struct lpfc_vport *vport,
2734                              struct lpfc_fdmi_attr_def *ad)
2735 {
2736         struct lpfc_fdmi_attr_entry *ae;
2737         uint32_t len, size;
2738
2739         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2740         memset(ae, 0, 256);
2741
2742         strncpy(ae->un.AttrString, "Smart SAN Version 2.0",
2743                 sizeof(ae->un.AttrString));
2744         len = strnlen(ae->un.AttrString,
2745                           sizeof(ae->un.AttrString));
2746         len += (len & 3) ? (4 - (len & 3)) : 4;
2747         size = FOURBYTES + len;
2748         ad->AttrLen =  cpu_to_be16(size);
2749         ad->AttrType = cpu_to_be16(RPRT_SMART_VERSION);
2750         return size;
2751 }
2752
2753 static int
2754 lpfc_fdmi_smart_attr_model(struct lpfc_vport *vport,
2755                            struct lpfc_fdmi_attr_def *ad)
2756 {
2757         struct lpfc_hba *phba = vport->phba;
2758         struct lpfc_fdmi_attr_entry *ae;
2759         uint32_t len, size;
2760
2761         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2762         memset(ae, 0, 256);
2763
2764         strncpy(ae->un.AttrString, phba->ModelName,
2765                 sizeof(ae->un.AttrString));
2766         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2767         len += (len & 3) ? (4 - (len & 3)) : 4;
2768         size = FOURBYTES + len;
2769         ad->AttrLen = cpu_to_be16(size);
2770         ad->AttrType = cpu_to_be16(RPRT_SMART_MODEL);
2771         return size;
2772 }
2773
2774 static int
2775 lpfc_fdmi_smart_attr_port_info(struct lpfc_vport *vport,
2776                                struct lpfc_fdmi_attr_def *ad)
2777 {
2778         struct lpfc_fdmi_attr_entry *ae;
2779         uint32_t size;
2780
2781         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2782
2783         /* SRIOV (type 3) is not supported */
2784         if (vport->vpi)
2785                 ae->un.AttrInt =  cpu_to_be32(2);  /* NPIV */
2786         else
2787                 ae->un.AttrInt =  cpu_to_be32(1);  /* Physical */
2788         size = FOURBYTES + sizeof(uint32_t);
2789         ad->AttrLen = cpu_to_be16(size);
2790         ad->AttrType = cpu_to_be16(RPRT_SMART_PORT_INFO);
2791         return size;
2792 }
2793
2794 static int
2795 lpfc_fdmi_smart_attr_qos(struct lpfc_vport *vport,
2796                          struct lpfc_fdmi_attr_def *ad)
2797 {
2798         struct lpfc_fdmi_attr_entry *ae;
2799         uint32_t size;
2800
2801         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2802         ae->un.AttrInt =  cpu_to_be32(0);
2803         size = FOURBYTES + sizeof(uint32_t);
2804         ad->AttrLen = cpu_to_be16(size);
2805         ad->AttrType = cpu_to_be16(RPRT_SMART_QOS);
2806         return size;
2807 }
2808
2809 static int
2810 lpfc_fdmi_smart_attr_security(struct lpfc_vport *vport,
2811                               struct lpfc_fdmi_attr_def *ad)
2812 {
2813         struct lpfc_fdmi_attr_entry *ae;
2814         uint32_t size;
2815
2816         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2817         ae->un.AttrInt =  cpu_to_be32(1);
2818         size = FOURBYTES + sizeof(uint32_t);
2819         ad->AttrLen = cpu_to_be16(size);
2820         ad->AttrType = cpu_to_be16(RPRT_SMART_SECURITY);
2821         return size;
2822 }
2823
2824 /* RHBA attribute jump table */
2825 int (*lpfc_fdmi_hba_action[])
2826         (struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
2827         /* Action routine                 Mask bit     Attribute type */
2828         lpfc_fdmi_hba_attr_wwnn,          /* bit0     RHBA_NODENAME           */
2829         lpfc_fdmi_hba_attr_manufacturer,  /* bit1     RHBA_MANUFACTURER       */
2830         lpfc_fdmi_hba_attr_sn,            /* bit2     RHBA_SERIAL_NUMBER      */
2831         lpfc_fdmi_hba_attr_model,         /* bit3     RHBA_MODEL              */
2832         lpfc_fdmi_hba_attr_description,   /* bit4     RHBA_MODEL_DESCRIPTION  */
2833         lpfc_fdmi_hba_attr_hdw_ver,       /* bit5     RHBA_HARDWARE_VERSION   */
2834         lpfc_fdmi_hba_attr_drvr_ver,      /* bit6     RHBA_DRIVER_VERSION     */
2835         lpfc_fdmi_hba_attr_rom_ver,       /* bit7     RHBA_OPTION_ROM_VERSION */
2836         lpfc_fdmi_hba_attr_fmw_ver,       /* bit8     RHBA_FIRMWARE_VERSION   */
2837         lpfc_fdmi_hba_attr_os_ver,        /* bit9     RHBA_OS_NAME_VERSION    */
2838         lpfc_fdmi_hba_attr_ct_len,        /* bit10    RHBA_MAX_CT_PAYLOAD_LEN */
2839         lpfc_fdmi_hba_attr_symbolic_name, /* bit11    RHBA_SYM_NODENAME       */
2840         lpfc_fdmi_hba_attr_vendor_info,   /* bit12    RHBA_VENDOR_INFO        */
2841         lpfc_fdmi_hba_attr_num_ports,     /* bit13    RHBA_NUM_PORTS          */
2842         lpfc_fdmi_hba_attr_fabric_wwnn,   /* bit14    RHBA_FABRIC_WWNN        */
2843         lpfc_fdmi_hba_attr_bios_ver,      /* bit15    RHBA_BIOS_VERSION       */
2844         lpfc_fdmi_hba_attr_bios_state,    /* bit16    RHBA_BIOS_STATE         */
2845         lpfc_fdmi_hba_attr_vendor_id,     /* bit17    RHBA_VENDOR_ID          */
2846 };
2847
2848 /* RPA / RPRT attribute jump table */
2849 int (*lpfc_fdmi_port_action[])
2850         (struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
2851         /* Action routine                   Mask bit   Attribute type */
2852         lpfc_fdmi_port_attr_fc4type,        /* bit0   RPRT_SUPPORT_FC4_TYPES  */
2853         lpfc_fdmi_port_attr_support_speed,  /* bit1   RPRT_SUPPORTED_SPEED    */
2854         lpfc_fdmi_port_attr_speed,          /* bit2   RPRT_PORT_SPEED         */
2855         lpfc_fdmi_port_attr_max_frame,      /* bit3   RPRT_MAX_FRAME_SIZE     */
2856         lpfc_fdmi_port_attr_os_devname,     /* bit4   RPRT_OS_DEVICE_NAME     */
2857         lpfc_fdmi_port_attr_host_name,      /* bit5   RPRT_HOST_NAME          */
2858         lpfc_fdmi_port_attr_wwnn,           /* bit6   RPRT_NODENAME           */
2859         lpfc_fdmi_port_attr_wwpn,           /* bit7   RPRT_PORTNAME           */
2860         lpfc_fdmi_port_attr_symbolic_name,  /* bit8   RPRT_SYM_PORTNAME       */
2861         lpfc_fdmi_port_attr_port_type,      /* bit9   RPRT_PORT_TYPE          */
2862         lpfc_fdmi_port_attr_class,          /* bit10  RPRT_SUPPORTED_CLASS    */
2863         lpfc_fdmi_port_attr_fabric_wwpn,    /* bit11  RPRT_FABRICNAME         */
2864         lpfc_fdmi_port_attr_active_fc4type, /* bit12  RPRT_ACTIVE_FC4_TYPES   */
2865         lpfc_fdmi_port_attr_port_state,     /* bit13  RPRT_PORT_STATE         */
2866         lpfc_fdmi_port_attr_num_disc,       /* bit14  RPRT_DISC_PORT          */
2867         lpfc_fdmi_port_attr_nportid,        /* bit15  RPRT_PORT_ID            */
2868         lpfc_fdmi_smart_attr_service,       /* bit16  RPRT_SMART_SERVICE      */
2869         lpfc_fdmi_smart_attr_guid,          /* bit17  RPRT_SMART_GUID         */
2870         lpfc_fdmi_smart_attr_version,       /* bit18  RPRT_SMART_VERSION      */
2871         lpfc_fdmi_smart_attr_model,         /* bit19  RPRT_SMART_MODEL        */
2872         lpfc_fdmi_smart_attr_port_info,     /* bit20  RPRT_SMART_PORT_INFO    */
2873         lpfc_fdmi_smart_attr_qos,           /* bit21  RPRT_SMART_QOS          */
2874         lpfc_fdmi_smart_attr_security,      /* bit22  RPRT_SMART_SECURITY     */
2875 };
2876
2877 /**
2878  * lpfc_fdmi_cmd - Build and send a FDMI cmd to the specified NPort
2879  * @vport: pointer to a host virtual N_Port data structure.
2880  * @ndlp: ndlp to send FDMI cmd to (if NULL use FDMI_DID)
2881  * cmdcode: FDMI command to send
2882  * mask: Mask of HBA or PORT Attributes to send
2883  *
2884  * Builds and sends a FDMI command using the CT subsystem.
2885  */
2886 int
2887 lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2888               int cmdcode, uint32_t new_mask)
2889 {
2890         struct lpfc_hba *phba = vport->phba;
2891         struct lpfc_dmabuf *mp, *bmp;
2892         struct lpfc_sli_ct_request *CtReq;
2893         struct ulp_bde64 *bpl;
2894         uint32_t bit_pos;
2895         uint32_t size;
2896         uint32_t rsp_size;
2897         uint32_t mask;
2898         struct lpfc_fdmi_reg_hba *rh;
2899         struct lpfc_fdmi_port_entry *pe;
2900         struct lpfc_fdmi_reg_portattr *pab = NULL;
2901         struct lpfc_fdmi_attr_block *ab = NULL;
2902         int  (*func)(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad);
2903         void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
2904                      struct lpfc_iocbq *);
2905
2906         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2907                 return 0;
2908
2909         cmpl = lpfc_cmpl_ct_disc_fdmi; /* called from discovery */
2910
2911         /* fill in BDEs for command */
2912         /* Allocate buffer for command payload */
2913         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2914         if (!mp)
2915                 goto fdmi_cmd_exit;
2916
2917         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
2918         if (!mp->virt)
2919                 goto fdmi_cmd_free_mp;
2920
2921         /* Allocate buffer for Buffer ptr list */
2922         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2923         if (!bmp)
2924                 goto fdmi_cmd_free_mpvirt;
2925
2926         bmp->virt = lpfc_mbuf_alloc(phba, 0, &(bmp->phys));
2927         if (!bmp->virt)
2928                 goto fdmi_cmd_free_bmp;
2929
2930         INIT_LIST_HEAD(&mp->list);
2931         INIT_LIST_HEAD(&bmp->list);
2932
2933         /* FDMI request */
2934         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2935                          "0218 FDMI Request Data: x%x x%x x%x\n",
2936                          vport->fc_flag, vport->port_state, cmdcode);
2937         CtReq = (struct lpfc_sli_ct_request *)mp->virt;
2938
2939         /* First populate the CT_IU preamble */
2940         memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
2941         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
2942         CtReq->RevisionId.bits.InId = 0;
2943
2944         CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
2945         CtReq->FsSubType = SLI_CT_FDMI_Subtypes;
2946
2947         CtReq->CommandResponse.bits.CmdRsp = cpu_to_be16(cmdcode);
2948         rsp_size = LPFC_BPL_SIZE;
2949         size = 0;
2950
2951         /* Next fill in the specific FDMI cmd information */
2952         switch (cmdcode) {
2953         case SLI_MGMT_RHAT:
2954         case SLI_MGMT_RHBA:
2955                 rh = (struct lpfc_fdmi_reg_hba *)&CtReq->un.PortID;
2956                 /* HBA Identifier */
2957                 memcpy(&rh->hi.PortName, &phba->pport->fc_sparam.portName,
2958                        sizeof(struct lpfc_name));
2959
2960                 if (cmdcode == SLI_MGMT_RHBA) {
2961                         /* Registered Port List */
2962                         /* One entry (port) per adapter */
2963                         rh->rpl.EntryCnt = cpu_to_be32(1);
2964                         memcpy(&rh->rpl.pe, &phba->pport->fc_sparam.portName,
2965                                sizeof(struct lpfc_name));
2966
2967                         /* point to the HBA attribute block */
2968                         size = 2 * sizeof(struct lpfc_name) +
2969                                 FOURBYTES;
2970                 } else {
2971                         size = sizeof(struct lpfc_name);
2972                 }
2973                 ab = (struct lpfc_fdmi_attr_block *)((uint8_t *)rh + size);
2974                 ab->EntryCnt = 0;
2975                 size += FOURBYTES;
2976                 bit_pos = 0;
2977                 if (new_mask)
2978                         mask = new_mask;
2979                 else
2980                         mask = vport->fdmi_hba_mask;
2981
2982                 /* Mask will dictate what attributes to build in the request */
2983                 while (mask) {
2984                         if (mask & 0x1) {
2985                                 func = lpfc_fdmi_hba_action[bit_pos];
2986                                 size += func(vport,
2987                                              (struct lpfc_fdmi_attr_def *)
2988                                              ((uint8_t *)rh + size));
2989                                 ab->EntryCnt++;
2990                                 if ((size + 256) >
2991                                     (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
2992                                         goto hba_out;
2993                         }
2994                         mask = mask >> 1;
2995                         bit_pos++;
2996                 }
2997 hba_out:
2998                 ab->EntryCnt = cpu_to_be32(ab->EntryCnt);
2999                 /* Total size */
3000                 size = GID_REQUEST_SZ - 4 + size;
3001                 break;
3002
3003         case SLI_MGMT_RPRT:
3004         case SLI_MGMT_RPA:
3005                 pab = (struct lpfc_fdmi_reg_portattr *)&CtReq->un.PortID;
3006                 if (cmdcode == SLI_MGMT_RPRT) {
3007                         rh = (struct lpfc_fdmi_reg_hba *)pab;
3008                         /* HBA Identifier */
3009                         memcpy(&rh->hi.PortName,
3010                                &phba->pport->fc_sparam.portName,
3011                                sizeof(struct lpfc_name));
3012                         pab = (struct lpfc_fdmi_reg_portattr *)
3013                                 ((uint8_t *)pab +  sizeof(struct lpfc_name));
3014                 }
3015
3016                 memcpy((uint8_t *)&pab->PortName,
3017                        (uint8_t *)&vport->fc_sparam.portName,
3018                        sizeof(struct lpfc_name));
3019                 size += sizeof(struct lpfc_name) + FOURBYTES;
3020                 pab->ab.EntryCnt = 0;
3021                 bit_pos = 0;
3022                 if (new_mask)
3023                         mask = new_mask;
3024                 else
3025                         mask = vport->fdmi_port_mask;
3026
3027                 /* Mask will dictate what attributes to build in the request */
3028                 while (mask) {
3029                         if (mask & 0x1) {
3030                                 func = lpfc_fdmi_port_action[bit_pos];
3031                                 size += func(vport,
3032                                              (struct lpfc_fdmi_attr_def *)
3033                                              ((uint8_t *)pab + size));
3034                                 pab->ab.EntryCnt++;
3035                                 if ((size + 256) >
3036                                     (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3037                                         goto port_out;
3038                         }
3039                         mask = mask >> 1;
3040                         bit_pos++;
3041                 }
3042 port_out:
3043                 pab->ab.EntryCnt = cpu_to_be32(pab->ab.EntryCnt);
3044                 /* Total size */
3045                 if (cmdcode == SLI_MGMT_RPRT)
3046                         size += sizeof(struct lpfc_name);
3047                 size = GID_REQUEST_SZ - 4 + size;
3048                 break;
3049
3050         case SLI_MGMT_GHAT:
3051         case SLI_MGMT_GRPL:
3052                 rsp_size = FC_MAX_NS_RSP;
3053         case SLI_MGMT_DHBA:
3054         case SLI_MGMT_DHAT:
3055                 pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3056                 memcpy((uint8_t *)&pe->PortName,
3057                        (uint8_t *)&vport->fc_sparam.portName,
3058                        sizeof(struct lpfc_name));
3059                 size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3060                 break;
3061
3062         case SLI_MGMT_GPAT:
3063         case SLI_MGMT_GPAS:
3064                 rsp_size = FC_MAX_NS_RSP;
3065         case SLI_MGMT_DPRT:
3066         case SLI_MGMT_DPA:
3067                 pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3068                 memcpy((uint8_t *)&pe->PortName,
3069                        (uint8_t *)&vport->fc_sparam.portName,
3070                        sizeof(struct lpfc_name));
3071                 size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3072                 break;
3073         case SLI_MGMT_GRHL:
3074                 size = GID_REQUEST_SZ - 4;
3075                 break;
3076         default:
3077                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3078                                  "0298 FDMI cmdcode x%x not supported\n",
3079                                  cmdcode);
3080                 goto fdmi_cmd_free_bmpvirt;
3081         }
3082         CtReq->CommandResponse.bits.Size = cpu_to_be16(rsp_size);
3083
3084         bpl = (struct ulp_bde64 *)bmp->virt;
3085         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
3086         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
3087         bpl->tus.f.bdeFlags = 0;
3088         bpl->tus.f.bdeSize = size;
3089
3090         /*
3091          * The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
3092          * to hold ndlp reference for the corresponding callback function.
3093          */
3094         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, 0))
3095                 return 0;
3096
3097         /*
3098          * Decrement ndlp reference count to release ndlp reference held
3099          * for the failed command's callback function.
3100          */
3101         lpfc_nlp_put(ndlp);
3102
3103 fdmi_cmd_free_bmpvirt:
3104         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3105 fdmi_cmd_free_bmp:
3106         kfree(bmp);
3107 fdmi_cmd_free_mpvirt:
3108         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3109 fdmi_cmd_free_mp:
3110         kfree(mp);
3111 fdmi_cmd_exit:
3112         /* Issue FDMI request failed */
3113         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3114                          "0244 Issue FDMI request failed Data: x%x\n",
3115                          cmdcode);
3116         return 1;
3117 }
3118
3119 /**
3120  * lpfc_delayed_disc_tmo - Timeout handler for delayed discovery timer.
3121  * @ptr - Context object of the timer.
3122  *
3123  * This function set the WORKER_DELAYED_DISC_TMO flag and wake up
3124  * the worker thread.
3125  **/
3126 void
3127 lpfc_delayed_disc_tmo(struct timer_list *t)
3128 {
3129         struct lpfc_vport *vport = from_timer(vport, t, delayed_disc_tmo);
3130         struct lpfc_hba   *phba = vport->phba;
3131         uint32_t tmo_posted;
3132         unsigned long iflag;
3133
3134         spin_lock_irqsave(&vport->work_port_lock, iflag);
3135         tmo_posted = vport->work_port_events & WORKER_DELAYED_DISC_TMO;
3136         if (!tmo_posted)
3137                 vport->work_port_events |= WORKER_DELAYED_DISC_TMO;
3138         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3139
3140         if (!tmo_posted)
3141                 lpfc_worker_wake_up(phba);
3142         return;
3143 }
3144
3145 /**
3146  * lpfc_delayed_disc_timeout_handler - Function called by worker thread to
3147  *      handle delayed discovery.
3148  * @vport: pointer to a host virtual N_Port data structure.
3149  *
3150  * This function start nport discovery of the vport.
3151  **/
3152 void
3153 lpfc_delayed_disc_timeout_handler(struct lpfc_vport *vport)
3154 {
3155         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3156
3157         spin_lock_irq(shost->host_lock);
3158         if (!(vport->fc_flag & FC_DISC_DELAYED)) {
3159                 spin_unlock_irq(shost->host_lock);
3160                 return;
3161         }
3162         vport->fc_flag &= ~FC_DISC_DELAYED;
3163         spin_unlock_irq(shost->host_lock);
3164
3165         lpfc_do_scr_ns_plogi(vport->phba, vport);
3166 }
3167
3168 void
3169 lpfc_decode_firmware_rev(struct lpfc_hba *phba, char *fwrevision, int flag)
3170 {
3171         struct lpfc_sli *psli = &phba->sli;
3172         lpfc_vpd_t *vp = &phba->vpd;
3173         uint32_t b1, b2, b3, b4, i, rev;
3174         char c;
3175         uint32_t *ptr, str[4];
3176         uint8_t *fwname;
3177
3178         if (phba->sli_rev == LPFC_SLI_REV4)
3179                 snprintf(fwrevision, FW_REV_STR_SIZE, "%s", vp->rev.opFwName);
3180         else if (vp->rev.rBit) {
3181                 if (psli->sli_flag & LPFC_SLI_ACTIVE)
3182                         rev = vp->rev.sli2FwRev;
3183                 else
3184                         rev = vp->rev.sli1FwRev;
3185
3186                 b1 = (rev & 0x0000f000) >> 12;
3187                 b2 = (rev & 0x00000f00) >> 8;
3188                 b3 = (rev & 0x000000c0) >> 6;
3189                 b4 = (rev & 0x00000030) >> 4;
3190
3191                 switch (b4) {
3192                 case 0:
3193                         c = 'N';
3194                         break;
3195                 case 1:
3196                         c = 'A';
3197                         break;
3198                 case 2:
3199                         c = 'B';
3200                         break;
3201                 case 3:
3202                         c = 'X';
3203                         break;
3204                 default:
3205                         c = 0;
3206                         break;
3207                 }
3208                 b4 = (rev & 0x0000000f);
3209
3210                 if (psli->sli_flag & LPFC_SLI_ACTIVE)
3211                         fwname = vp->rev.sli2FwName;
3212                 else
3213                         fwname = vp->rev.sli1FwName;
3214
3215                 for (i = 0; i < 16; i++)
3216                         if (fwname[i] == 0x20)
3217                                 fwname[i] = 0;
3218
3219                 ptr = (uint32_t*)fwname;
3220
3221                 for (i = 0; i < 3; i++)
3222                         str[i] = be32_to_cpu(*ptr++);
3223
3224                 if (c == 0) {
3225                         if (flag)
3226                                 sprintf(fwrevision, "%d.%d%d (%s)",
3227                                         b1, b2, b3, (char *)str);
3228                         else
3229                                 sprintf(fwrevision, "%d.%d%d", b1,
3230                                         b2, b3);
3231                 } else {
3232                         if (flag)
3233                                 sprintf(fwrevision, "%d.%d%d%c%d (%s)",
3234                                         b1, b2, b3, c,
3235                                         b4, (char *)str);
3236                         else
3237                                 sprintf(fwrevision, "%d.%d%d%c%d",
3238                                         b1, b2, b3, c, b4);
3239                 }
3240         } else {
3241                 rev = vp->rev.smFwRev;
3242
3243                 b1 = (rev & 0xff000000) >> 24;
3244                 b2 = (rev & 0x00f00000) >> 20;
3245                 b3 = (rev & 0x000f0000) >> 16;
3246                 c  = (rev & 0x0000ff00) >> 8;
3247                 b4 = (rev & 0x000000ff);
3248
3249                 sprintf(fwrevision, "%d.%d%d%c%d", b1, b2, b3, c, b4);
3250         }
3251         return;
3252 }