Linux-libre 5.7.6-gnu
[librecmc/linux-libre.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-2020 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 x%x\n", Did,
484                                          ndlp->nlp_flag, ndlp->nlp_fc4_type,
485                                          ndlp->nlp_state, vport->fc_flag,
486                                          vport->fc_rscn_id_cnt);
487
488                         /* if ndlp needs to be discovered and prior
489                          * state of ndlp hit devloss, change state to
490                          * allow rediscovery.
491                          */
492                         if (ndlp->nlp_flag & NLP_NPR_2B_DISC &&
493                             ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
494                                 lpfc_nlp_set_state(vport, ndlp,
495                                                    NLP_STE_NPR_NODE);
496                         }
497                 } else {
498                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
499                                 "Skip1 GID_FTrsp: did:x%x flg:x%x cnt:%d",
500                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
501
502                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
503                                          "0239 Skip x%06x NameServer Rsp "
504                                          "Data: x%x x%x %p\n",
505                                          Did, vport->fc_flag,
506                                          vport->fc_rscn_id_cnt, ndlp);
507                 }
508         } else {
509                 if (!(vport->fc_flag & FC_RSCN_MODE) ||
510                     lpfc_rscn_payload_check(vport, Did)) {
511                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
512                                 "Query GID_FTrsp: did:x%x flg:x%x cnt:%d",
513                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
514
515                         /*
516                          * This NPortID was previously a FCP/NVMe target,
517                          * Don't even bother to send GFF_ID.
518                          */
519                         ndlp = lpfc_findnode_did(vport, Did);
520                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
521                             (ndlp->nlp_type &
522                             (NLP_FCP_TARGET | NLP_NVME_TARGET))) {
523                                 if (fc4_type == FC_TYPE_FCP)
524                                         ndlp->nlp_fc4_type |= NLP_FC4_FCP;
525                                 if (fc4_type == FC_TYPE_NVME)
526                                         ndlp->nlp_fc4_type |= NLP_FC4_NVME;
527                                 lpfc_setup_disc_node(vport, Did);
528                         } else if (lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
529                                    0, Did) == 0)
530                                 vport->num_disc_nodes++;
531                         else
532                                 lpfc_setup_disc_node(vport, Did);
533                 } else {
534                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
535                                 "Skip2 GID_FTrsp: did:x%x flg:x%x cnt:%d",
536                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
537
538                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
539                                          "0245 Skip x%06x NameServer Rsp "
540                                          "Data: x%x x%x\n", Did,
541                                          vport->fc_flag,
542                                          vport->fc_rscn_id_cnt);
543                 }
544         }
545 }
546
547 static void
548 lpfc_ns_rsp_audit_did(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
549 {
550         struct lpfc_hba *phba = vport->phba;
551         struct lpfc_nodelist *ndlp = NULL;
552         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
553         char *str;
554
555         if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT)
556                 str = "GID_FT";
557         else
558                 str = "GID_PT";
559         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
560                          "6430 Process %s rsp for %08x type %x %s %s\n",
561                          str, Did, fc4_type,
562                          (fc4_type == FC_TYPE_FCP) ?  "FCP" : " ",
563                          (fc4_type == FC_TYPE_NVME) ?  "NVME" : " ");
564         /*
565          * To conserve rpi's, filter out addresses for other
566          * vports on the same physical HBAs.
567          */
568         if (Did != vport->fc_myDID &&
569             (!lpfc_find_vport_by_did(phba, Did) ||
570              vport->cfg_peer_port_login)) {
571                 if (!phba->nvmet_support) {
572                         /* FCPI/NVMEI path. Process Did */
573                         lpfc_prep_node_fc4type(vport, Did, fc4_type);
574                         return;
575                 }
576                 /* NVMET path.  NVMET only cares about NVMEI nodes. */
577                 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
578                         if (ndlp->nlp_type != NLP_NVME_INITIATOR ||
579                             ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
580                                 continue;
581                         spin_lock_irq(shost->host_lock);
582                         if (ndlp->nlp_DID == Did)
583                                 ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
584                         else
585                                 ndlp->nlp_flag |= NLP_NVMET_RECOV;
586                         spin_unlock_irq(shost->host_lock);
587                 }
588         }
589 }
590
591 static int
592 lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint8_t fc4_type,
593             uint32_t Size)
594 {
595         struct lpfc_sli_ct_request *Response =
596                 (struct lpfc_sli_ct_request *) mp->virt;
597         struct lpfc_dmabuf *mlast, *next_mp;
598         uint32_t *ctptr = (uint32_t *) & Response->un.gid.PortType;
599         uint32_t Did, CTentry;
600         int Cnt;
601         struct list_head head;
602         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
603         struct lpfc_nodelist *ndlp = NULL;
604
605         lpfc_set_disctmo(vport);
606         vport->num_disc_nodes = 0;
607         vport->fc_ns_retry = 0;
608
609
610         list_add_tail(&head, &mp->list);
611         list_for_each_entry_safe(mp, next_mp, &head, list) {
612                 mlast = mp;
613
614                 Cnt = Size  > FCELSSIZE ? FCELSSIZE : Size;
615
616                 Size -= Cnt;
617
618                 if (!ctptr) {
619                         ctptr = (uint32_t *) mlast->virt;
620                 } else
621                         Cnt -= 16;      /* subtract length of CT header */
622
623                 /* Loop through entire NameServer list of DIDs */
624                 while (Cnt >= sizeof(uint32_t)) {
625                         /* Get next DID from NameServer List */
626                         CTentry = *ctptr++;
627                         Did = ((be32_to_cpu(CTentry)) & Mask_DID);
628                         lpfc_ns_rsp_audit_did(vport, Did, fc4_type);
629                         if (CTentry & (cpu_to_be32(SLI_CT_LAST_ENTRY)))
630                                 goto nsout1;
631
632                         Cnt -= sizeof(uint32_t);
633                 }
634                 ctptr = NULL;
635
636         }
637
638         /* All GID_FT entries processed.  If the driver is running in
639          * in target mode, put impacted nodes into recovery and drop
640          * the RPI to flush outstanding IO.
641          */
642         if (vport->phba->nvmet_support) {
643                 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
644                         if (!(ndlp->nlp_flag & NLP_NVMET_RECOV))
645                                 continue;
646                         lpfc_disc_state_machine(vport, ndlp, NULL,
647                                                 NLP_EVT_DEVICE_RECOVERY);
648                         spin_lock_irq(shost->host_lock);
649                         ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
650                         spin_unlock_irq(shost->host_lock);
651                 }
652         }
653
654 nsout1:
655         list_del(&head);
656         return 0;
657 }
658
659 static void
660 lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
661                         struct lpfc_iocbq *rspiocb)
662 {
663         struct lpfc_vport *vport = cmdiocb->vport;
664         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
665         IOCB_t *irsp;
666         struct lpfc_dmabuf *outp;
667         struct lpfc_dmabuf *inp;
668         struct lpfc_sli_ct_request *CTrsp;
669         struct lpfc_sli_ct_request *CTreq;
670         struct lpfc_nodelist *ndlp;
671         int rc, type;
672
673         /* First save ndlp, before we overwrite it */
674         ndlp = cmdiocb->context_un.ndlp;
675
676         /* we pass cmdiocb to state machine which needs rspiocb as well */
677         cmdiocb->context_un.rsp_iocb = rspiocb;
678         inp = (struct lpfc_dmabuf *) cmdiocb->context1;
679         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
680         irsp = &rspiocb->iocb;
681
682         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
683                  "GID_FT cmpl:     status:x%x/x%x rtry:%d",
684                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_ns_retry);
685
686         /* Don't bother processing response if vport is being torn down. */
687         if (vport->load_flag & FC_UNLOADING) {
688                 if (vport->fc_flag & FC_RSCN_MODE)
689                         lpfc_els_flush_rscn(vport);
690                 goto out;
691         }
692
693         if (lpfc_els_chk_latt(vport)) {
694                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
695                                  "0216 Link event during NS query\n");
696                 if (vport->fc_flag & FC_RSCN_MODE)
697                         lpfc_els_flush_rscn(vport);
698                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
699                 goto out;
700         }
701         if (lpfc_error_lost_link(irsp)) {
702                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
703                                  "0226 NS query failed due to link event\n");
704                 if (vport->fc_flag & FC_RSCN_MODE)
705                         lpfc_els_flush_rscn(vport);
706                 goto out;
707         }
708
709         spin_lock_irq(shost->host_lock);
710         if (vport->fc_flag & FC_RSCN_DEFERRED) {
711                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
712                 spin_unlock_irq(shost->host_lock);
713
714                 /* This is a GID_FT completing so the gidft_inp counter was
715                  * incremented before the GID_FT was issued to the wire.
716                  */
717                 vport->gidft_inp--;
718
719                 /*
720                  * Skip processing the NS response
721                  * Re-issue the NS cmd
722                  */
723                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
724                                  "0151 Process Deferred RSCN Data: x%x x%x\n",
725                                  vport->fc_flag, vport->fc_rscn_id_cnt);
726                 lpfc_els_handle_rscn(vport);
727
728                 goto out;
729         }
730         spin_unlock_irq(shost->host_lock);
731
732         if (irsp->ulpStatus) {
733                 /* Check for retry */
734                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
735                         if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
736                             (irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
737                             IOERR_NO_RESOURCES)
738                                 vport->fc_ns_retry++;
739
740                         type = lpfc_get_gidft_type(vport, cmdiocb);
741                         if (type == 0)
742                                 goto out;
743
744                         /* CT command is being retried */
745                         vport->gidft_inp--;
746                         rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
747                                          vport->fc_ns_retry, type);
748                         if (rc == 0)
749                                 goto out;
750                 }
751                 if (vport->fc_flag & FC_RSCN_MODE)
752                         lpfc_els_flush_rscn(vport);
753                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
754                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
755                                  "0257 GID_FT Query error: 0x%x 0x%x\n",
756                                  irsp->ulpStatus, vport->fc_ns_retry);
757         } else {
758                 /* Good status, continue checking */
759                 CTreq = (struct lpfc_sli_ct_request *) inp->virt;
760                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
761                 if (CTrsp->CommandResponse.bits.CmdRsp ==
762                     cpu_to_be16(SLI_CT_RESPONSE_FS_ACC)) {
763                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
764                                          "0208 NameServer Rsp Data: x%x x%x "
765                                          "x%x x%x sz x%x\n",
766                                          vport->fc_flag,
767                                          CTreq->un.gid.Fc4Type,
768                                          vport->num_disc_nodes,
769                                          vport->gidft_inp,
770                                          irsp->un.genreq64.bdl.bdeSize);
771
772                         lpfc_ns_rsp(vport,
773                                     outp,
774                                     CTreq->un.gid.Fc4Type,
775                                     (uint32_t) (irsp->un.genreq64.bdl.bdeSize));
776                 } else if (CTrsp->CommandResponse.bits.CmdRsp ==
777                            be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
778                         /* NameServer Rsp Error */
779                         if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
780                             && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
781                                 lpfc_printf_vlog(vport, KERN_INFO,
782                                         LOG_DISCOVERY,
783                                         "0269 No NameServer Entries "
784                                         "Data: x%x x%x x%x x%x\n",
785                                         CTrsp->CommandResponse.bits.CmdRsp,
786                                         (uint32_t) CTrsp->ReasonCode,
787                                         (uint32_t) CTrsp->Explanation,
788                                         vport->fc_flag);
789
790                                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
791                                 "GID_FT no entry  cmd:x%x rsn:x%x exp:x%x",
792                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
793                                 (uint32_t) CTrsp->ReasonCode,
794                                 (uint32_t) CTrsp->Explanation);
795                         } else {
796                                 lpfc_printf_vlog(vport, KERN_INFO,
797                                         LOG_DISCOVERY,
798                                         "0240 NameServer Rsp Error "
799                                         "Data: x%x x%x x%x x%x\n",
800                                         CTrsp->CommandResponse.bits.CmdRsp,
801                                         (uint32_t) CTrsp->ReasonCode,
802                                         (uint32_t) CTrsp->Explanation,
803                                         vport->fc_flag);
804
805                                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
806                                 "GID_FT rsp err1  cmd:x%x rsn:x%x exp:x%x",
807                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
808                                 (uint32_t) CTrsp->ReasonCode,
809                                 (uint32_t) CTrsp->Explanation);
810                         }
811
812
813                 } else {
814                         /* NameServer Rsp Error */
815                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
816                                         "0241 NameServer Rsp Error "
817                                         "Data: x%x x%x x%x x%x\n",
818                                         CTrsp->CommandResponse.bits.CmdRsp,
819                                         (uint32_t) CTrsp->ReasonCode,
820                                         (uint32_t) CTrsp->Explanation,
821                                         vport->fc_flag);
822
823                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
824                                 "GID_FT rsp err2  cmd:x%x rsn:x%x exp:x%x",
825                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
826                                 (uint32_t) CTrsp->ReasonCode,
827                                 (uint32_t) CTrsp->Explanation);
828                 }
829                 vport->gidft_inp--;
830         }
831
832         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
833                          "4216 GID_FT cmpl inp %d disc %d\n",
834                          vport->gidft_inp, vport->num_disc_nodes);
835
836         /* Link up / RSCN discovery */
837         if ((vport->num_disc_nodes == 0) &&
838             (vport->gidft_inp == 0)) {
839                 /*
840                  * The driver has cycled through all Nports in the RSCN payload.
841                  * Complete the handling by cleaning up and marking the
842                  * current driver state.
843                  */
844                 if (vport->port_state >= LPFC_DISC_AUTH) {
845                         if (vport->fc_flag & FC_RSCN_MODE) {
846                                 lpfc_els_flush_rscn(vport);
847                                 spin_lock_irq(shost->host_lock);
848                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
849                                 spin_unlock_irq(shost->host_lock);
850                         }
851                         else
852                                 lpfc_els_flush_rscn(vport);
853                 }
854
855                 lpfc_disc_start(vport);
856         }
857 out:
858         cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
859         lpfc_ct_free_iocb(phba, cmdiocb);
860         return;
861 }
862
863 static void
864 lpfc_cmpl_ct_cmd_gid_pt(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
865                         struct lpfc_iocbq *rspiocb)
866 {
867         struct lpfc_vport *vport = cmdiocb->vport;
868         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
869         IOCB_t *irsp;
870         struct lpfc_dmabuf *outp;
871         struct lpfc_dmabuf *inp;
872         struct lpfc_sli_ct_request *CTrsp;
873         struct lpfc_sli_ct_request *CTreq;
874         struct lpfc_nodelist *ndlp;
875         int rc;
876
877         /* First save ndlp, before we overwrite it */
878         ndlp = cmdiocb->context_un.ndlp;
879
880         /* we pass cmdiocb to state machine which needs rspiocb as well */
881         cmdiocb->context_un.rsp_iocb = rspiocb;
882         inp = (struct lpfc_dmabuf *)cmdiocb->context1;
883         outp = (struct lpfc_dmabuf *)cmdiocb->context2;
884         irsp = &rspiocb->iocb;
885
886         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
887                               "GID_PT cmpl:     status:x%x/x%x rtry:%d",
888                               irsp->ulpStatus, irsp->un.ulpWord[4],
889                               vport->fc_ns_retry);
890
891         /* Don't bother processing response if vport is being torn down. */
892         if (vport->load_flag & FC_UNLOADING) {
893                 if (vport->fc_flag & FC_RSCN_MODE)
894                         lpfc_els_flush_rscn(vport);
895                 goto out;
896         }
897
898         if (lpfc_els_chk_latt(vport)) {
899                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
900                                  "4108 Link event during NS query\n");
901                 if (vport->fc_flag & FC_RSCN_MODE)
902                         lpfc_els_flush_rscn(vport);
903                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
904                 goto out;
905         }
906         if (lpfc_error_lost_link(irsp)) {
907                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
908                                  "4166 NS query failed due to link event\n");
909                 if (vport->fc_flag & FC_RSCN_MODE)
910                         lpfc_els_flush_rscn(vport);
911                 goto out;
912         }
913
914         spin_lock_irq(shost->host_lock);
915         if (vport->fc_flag & FC_RSCN_DEFERRED) {
916                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
917                 spin_unlock_irq(shost->host_lock);
918
919                 /* This is a GID_PT completing so the gidft_inp counter was
920                  * incremented before the GID_PT was issued to the wire.
921                  */
922                 vport->gidft_inp--;
923
924                 /*
925                  * Skip processing the NS response
926                  * Re-issue the NS cmd
927                  */
928                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
929                                  "4167 Process Deferred RSCN Data: x%x x%x\n",
930                                  vport->fc_flag, vport->fc_rscn_id_cnt);
931                 lpfc_els_handle_rscn(vport);
932
933                 goto out;
934         }
935         spin_unlock_irq(shost->host_lock);
936
937         if (irsp->ulpStatus) {
938                 /* Check for retry */
939                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
940                         if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
941                             (irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
942                             IOERR_NO_RESOURCES)
943                                 vport->fc_ns_retry++;
944
945                         /* CT command is being retried */
946                         vport->gidft_inp--;
947                         rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_PT,
948                                          vport->fc_ns_retry, GID_PT_N_PORT);
949                         if (rc == 0)
950                                 goto out;
951                 }
952                 if (vport->fc_flag & FC_RSCN_MODE)
953                         lpfc_els_flush_rscn(vport);
954                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
955                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
956                                  "4103 GID_FT Query error: 0x%x 0x%x\n",
957                                  irsp->ulpStatus, vport->fc_ns_retry);
958         } else {
959                 /* Good status, continue checking */
960                 CTreq = (struct lpfc_sli_ct_request *)inp->virt;
961                 CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
962                 if (CTrsp->CommandResponse.bits.CmdRsp ==
963                     cpu_to_be16(SLI_CT_RESPONSE_FS_ACC)) {
964                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
965                                          "4105 NameServer Rsp Data: x%x x%x "
966                                          "x%x x%x sz x%x\n",
967                                          vport->fc_flag,
968                                          CTreq->un.gid.Fc4Type,
969                                          vport->num_disc_nodes,
970                                          vport->gidft_inp,
971                                          irsp->un.genreq64.bdl.bdeSize);
972
973                         lpfc_ns_rsp(vport,
974                                     outp,
975                                     CTreq->un.gid.Fc4Type,
976                                     (uint32_t)(irsp->un.genreq64.bdl.bdeSize));
977                 } else if (CTrsp->CommandResponse.bits.CmdRsp ==
978                            be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
979                         /* NameServer Rsp Error */
980                         if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
981                             && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
982                                 lpfc_printf_vlog(
983                                         vport, KERN_INFO, LOG_DISCOVERY,
984                                         "4106 No NameServer Entries "
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 no entry  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                         } else {
998                                 lpfc_printf_vlog(
999                                         vport, KERN_INFO, LOG_DISCOVERY,
1000                                         "4107 NameServer Rsp Error "
1001                                         "Data: x%x x%x x%x x%x\n",
1002                                         CTrsp->CommandResponse.bits.CmdRsp,
1003                                         (uint32_t)CTrsp->ReasonCode,
1004                                         (uint32_t)CTrsp->Explanation,
1005                                         vport->fc_flag);
1006
1007                                 lpfc_debugfs_disc_trc(
1008                                 vport, LPFC_DISC_TRC_CT,
1009                                 "GID_PT rsp err1  cmd:x%x rsn:x%x exp:x%x",
1010                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1011                                 (uint32_t)CTrsp->ReasonCode,
1012                                 (uint32_t)CTrsp->Explanation);
1013                         }
1014                 } else {
1015                         /* NameServer Rsp Error */
1016                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1017                                          "4109 NameServer Rsp Error "
1018                                          "Data: x%x x%x x%x x%x\n",
1019                                          CTrsp->CommandResponse.bits.CmdRsp,
1020                                          (uint32_t)CTrsp->ReasonCode,
1021                                          (uint32_t)CTrsp->Explanation,
1022                                          vport->fc_flag);
1023
1024                         lpfc_debugfs_disc_trc(
1025                                 vport, LPFC_DISC_TRC_CT,
1026                                 "GID_PT rsp err2  cmd:x%x rsn:x%x exp:x%x",
1027                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1028                                 (uint32_t)CTrsp->ReasonCode,
1029                                 (uint32_t)CTrsp->Explanation);
1030                 }
1031                 vport->gidft_inp--;
1032         }
1033
1034         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1035                          "6450 GID_PT cmpl inp %d disc %d\n",
1036                          vport->gidft_inp, vport->num_disc_nodes);
1037
1038         /* Link up / RSCN discovery */
1039         if ((vport->num_disc_nodes == 0) &&
1040             (vport->gidft_inp == 0)) {
1041                 /*
1042                  * The driver has cycled through all Nports in the RSCN payload.
1043                  * Complete the handling by cleaning up and marking the
1044                  * current driver state.
1045                  */
1046                 if (vport->port_state >= LPFC_DISC_AUTH) {
1047                         if (vport->fc_flag & FC_RSCN_MODE) {
1048                                 lpfc_els_flush_rscn(vport);
1049                                 spin_lock_irq(shost->host_lock);
1050                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1051                                 spin_unlock_irq(shost->host_lock);
1052                         } else {
1053                                 lpfc_els_flush_rscn(vport);
1054                         }
1055                 }
1056
1057                 lpfc_disc_start(vport);
1058         }
1059 out:
1060         cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
1061         lpfc_ct_free_iocb(phba, cmdiocb);
1062 }
1063
1064 static void
1065 lpfc_cmpl_ct_cmd_gff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1066                         struct lpfc_iocbq *rspiocb)
1067 {
1068         struct lpfc_vport *vport = cmdiocb->vport;
1069         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1070         IOCB_t *irsp = &rspiocb->iocb;
1071         struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *) cmdiocb->context1;
1072         struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1073         struct lpfc_sli_ct_request *CTrsp;
1074         int did, rc, retry;
1075         uint8_t fbits;
1076         struct lpfc_nodelist *ndlp;
1077
1078         did = ((struct lpfc_sli_ct_request *) inp->virt)->un.gff.PortId;
1079         did = be32_to_cpu(did);
1080
1081         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1082                 "GFF_ID cmpl:     status:x%x/x%x did:x%x",
1083                 irsp->ulpStatus, irsp->un.ulpWord[4], did);
1084
1085         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1086                 /* Good status, continue checking */
1087                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1088                 fbits = CTrsp->un.gff_acc.fbits[FCP_TYPE_FEATURE_OFFSET];
1089
1090                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1091                                  "6431 Process GFF_ID rsp for %08x "
1092                                  "fbits %02x %s %s\n",
1093                                  did, fbits,
1094                                  (fbits & FC4_FEATURE_INIT) ? "Initiator" : " ",
1095                                  (fbits & FC4_FEATURE_TARGET) ? "Target" : " ");
1096
1097                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1098                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
1099                         if ((fbits & FC4_FEATURE_INIT) &&
1100                             !(fbits & FC4_FEATURE_TARGET)) {
1101                                 lpfc_printf_vlog(vport, KERN_INFO,
1102                                                  LOG_DISCOVERY,
1103                                                  "0270 Skip x%x GFF "
1104                                                  "NameServer Rsp Data: (init) "
1105                                                  "x%x x%x\n", did, fbits,
1106                                                  vport->fc_rscn_id_cnt);
1107                                 goto out;
1108                         }
1109                 }
1110         }
1111         else {
1112                 /* Check for retry */
1113                 if (cmdiocb->retry < LPFC_MAX_NS_RETRY) {
1114                         retry = 1;
1115                         if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1116                                 switch ((irsp->un.ulpWord[4] &
1117                                         IOERR_PARAM_MASK)) {
1118
1119                                 case IOERR_NO_RESOURCES:
1120                                         /* We don't increment the retry
1121                                          * count for this case.
1122                                          */
1123                                         break;
1124                                 case IOERR_LINK_DOWN:
1125                                 case IOERR_SLI_ABORTED:
1126                                 case IOERR_SLI_DOWN:
1127                                         retry = 0;
1128                                         break;
1129                                 default:
1130                                         cmdiocb->retry++;
1131                                 }
1132                         }
1133                         else
1134                                 cmdiocb->retry++;
1135
1136                         if (retry) {
1137                                 /* CT command is being retried */
1138                                 rc = lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
1139                                          cmdiocb->retry, did);
1140                                 if (rc == 0) {
1141                                         /* success */
1142                                         lpfc_ct_free_iocb(phba, cmdiocb);
1143                                         return;
1144                                 }
1145                         }
1146                 }
1147                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1148                                  "0267 NameServer GFF Rsp "
1149                                  "x%x Error (%d %d) Data: x%x x%x\n",
1150                                  did, irsp->ulpStatus, irsp->un.ulpWord[4],
1151                                  vport->fc_flag, vport->fc_rscn_id_cnt);
1152         }
1153
1154         /* This is a target port, unregistered port, or the GFF_ID failed */
1155         ndlp = lpfc_setup_disc_node(vport, did);
1156         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
1157                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1158                                  "0242 Process x%x GFF "
1159                                  "NameServer Rsp Data: x%x x%x x%x\n",
1160                                  did, ndlp->nlp_flag, vport->fc_flag,
1161                                  vport->fc_rscn_id_cnt);
1162         } else {
1163                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1164                                  "0243 Skip x%x GFF "
1165                                  "NameServer Rsp Data: x%x x%x\n", did,
1166                                  vport->fc_flag, vport->fc_rscn_id_cnt);
1167         }
1168 out:
1169         /* Link up / RSCN discovery */
1170         if (vport->num_disc_nodes)
1171                 vport->num_disc_nodes--;
1172
1173         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1174                          "6451 GFF_ID cmpl inp %d disc %d\n",
1175                          vport->gidft_inp, vport->num_disc_nodes);
1176
1177         if (vport->num_disc_nodes == 0) {
1178                 /*
1179                  * The driver has cycled through all Nports in the RSCN payload.
1180                  * Complete the handling by cleaning up and marking the
1181                  * current driver state.
1182                  */
1183                 if (vport->port_state >= LPFC_DISC_AUTH) {
1184                         if (vport->fc_flag & FC_RSCN_MODE) {
1185                                 lpfc_els_flush_rscn(vport);
1186                                 spin_lock_irq(shost->host_lock);
1187                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1188                                 spin_unlock_irq(shost->host_lock);
1189                         }
1190                         else
1191                                 lpfc_els_flush_rscn(vport);
1192                 }
1193                 lpfc_disc_start(vport);
1194         }
1195         lpfc_ct_free_iocb(phba, cmdiocb);
1196         return;
1197 }
1198
1199 static void
1200 lpfc_cmpl_ct_cmd_gft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1201                                 struct lpfc_iocbq *rspiocb)
1202 {
1203         struct lpfc_vport *vport = cmdiocb->vport;
1204         IOCB_t *irsp = &rspiocb->iocb;
1205         struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *)cmdiocb->context1;
1206         struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *)cmdiocb->context2;
1207         struct lpfc_sli_ct_request *CTrsp;
1208         int did;
1209         struct lpfc_nodelist *ndlp;
1210         uint32_t fc4_data_0, fc4_data_1;
1211
1212         did = ((struct lpfc_sli_ct_request *)inp->virt)->un.gft.PortId;
1213         did = be32_to_cpu(did);
1214
1215         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1216                               "GFT_ID cmpl: status:x%x/x%x did:x%x",
1217                               irsp->ulpStatus, irsp->un.ulpWord[4], did);
1218
1219         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1220                 /* Good status, continue checking */
1221                 CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
1222                 fc4_data_0 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[0]);
1223                 fc4_data_1 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[1]);
1224
1225                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1226                                  "6432 Process GFT_ID rsp for %08x "
1227                                  "Data %08x %08x %s %s\n",
1228                                  did, fc4_data_0, fc4_data_1,
1229                                  (fc4_data_0 & LPFC_FC4_TYPE_BITMASK) ?
1230                                   "FCP" : " ",
1231                                  (fc4_data_1 & LPFC_FC4_TYPE_BITMASK) ?
1232                                   "NVME" : " ");
1233
1234                 ndlp = lpfc_findnode_did(vport, did);
1235                 if (ndlp) {
1236                         /* The bitmask value for FCP and NVME FCP types is
1237                          * the same because they are 32 bits distant from
1238                          * each other in word0 and word0.
1239                          */
1240                         if (fc4_data_0 & LPFC_FC4_TYPE_BITMASK)
1241                                 ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1242                         if (fc4_data_1 &  LPFC_FC4_TYPE_BITMASK)
1243                                 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1244                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1245                                          "3064 Setting ndlp x%px, DID x%06x "
1246                                          "with FC4 x%08x, Data: x%08x x%08x "
1247                                          "%d\n",
1248                                          ndlp, did, ndlp->nlp_fc4_type,
1249                                          FC_TYPE_FCP, FC_TYPE_NVME,
1250                                          ndlp->nlp_state);
1251
1252                         if (ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE &&
1253                             ndlp->nlp_fc4_type) {
1254                                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1255
1256                                 lpfc_nlp_set_state(vport, ndlp,
1257                                                    NLP_STE_PRLI_ISSUE);
1258                                 lpfc_issue_els_prli(vport, ndlp, 0);
1259                         } else if (!ndlp->nlp_fc4_type) {
1260                                 /* If fc4 type is still unknown, then LOGO */
1261                                 lpfc_printf_vlog(vport, KERN_INFO,
1262                                                  LOG_DISCOVERY,
1263                                                  "6443 Sending LOGO ndlp x%px,"
1264                                                  "DID x%06x with fc4_type: "
1265                                                  "x%08x, state: %d\n",
1266                                                  ndlp, did, ndlp->nlp_fc4_type,
1267                                                  ndlp->nlp_state);
1268                                 lpfc_issue_els_logo(vport, ndlp, 0);
1269                                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1270                                 lpfc_nlp_set_state(vport, ndlp,
1271                                                    NLP_STE_NPR_NODE);
1272                         }
1273                 }
1274         } else
1275                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1276                                  "3065 GFT_ID failed x%08x\n", irsp->ulpStatus);
1277
1278         lpfc_ct_free_iocb(phba, cmdiocb);
1279 }
1280
1281 static void
1282 lpfc_cmpl_ct(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1283              struct lpfc_iocbq *rspiocb)
1284 {
1285         struct lpfc_vport *vport = cmdiocb->vport;
1286         struct lpfc_dmabuf *inp;
1287         struct lpfc_dmabuf *outp;
1288         IOCB_t *irsp;
1289         struct lpfc_sli_ct_request *CTrsp;
1290         struct lpfc_nodelist *ndlp;
1291         int cmdcode, rc;
1292         uint8_t retry;
1293         uint32_t latt;
1294
1295         /* First save ndlp, before we overwrite it */
1296         ndlp = cmdiocb->context_un.ndlp;
1297
1298         /* we pass cmdiocb to state machine which needs rspiocb as well */
1299         cmdiocb->context_un.rsp_iocb = rspiocb;
1300
1301         inp = (struct lpfc_dmabuf *) cmdiocb->context1;
1302         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1303         irsp = &rspiocb->iocb;
1304
1305         cmdcode = be16_to_cpu(((struct lpfc_sli_ct_request *) inp->virt)->
1306                                         CommandResponse.bits.CmdRsp);
1307         CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1308
1309         latt = lpfc_els_chk_latt(vport);
1310
1311         /* RFT request completes status <ulpStatus> CmdRsp <CmdRsp> */
1312         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1313                          "0209 CT Request completes, latt %d, "
1314                          "ulpStatus x%x CmdRsp x%x, Context x%x, Tag x%x\n",
1315                          latt, irsp->ulpStatus,
1316                          CTrsp->CommandResponse.bits.CmdRsp,
1317                          cmdiocb->iocb.ulpContext, cmdiocb->iocb.ulpIoTag);
1318
1319         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1320                 "CT cmd cmpl:     status:x%x/x%x cmd:x%x",
1321                 irsp->ulpStatus, irsp->un.ulpWord[4], cmdcode);
1322
1323         if (irsp->ulpStatus) {
1324                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1325                                  "0268 NS cmd x%x Error (x%x x%x)\n",
1326                                  cmdcode, irsp->ulpStatus, irsp->un.ulpWord[4]);
1327
1328                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1329                         (((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1330                           IOERR_SLI_DOWN) ||
1331                          ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1332                           IOERR_SLI_ABORTED)))
1333                         goto out;
1334
1335                 retry = cmdiocb->retry;
1336                 if (retry >= LPFC_MAX_NS_RETRY)
1337                         goto out;
1338
1339                 retry++;
1340                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1341                                  "0250 Retrying NS cmd %x\n", cmdcode);
1342                 rc = lpfc_ns_cmd(vport, cmdcode, retry, 0);
1343                 if (rc == 0)
1344                         goto out;
1345         }
1346
1347 out:
1348         cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
1349         lpfc_ct_free_iocb(phba, cmdiocb);
1350         return;
1351 }
1352
1353 static void
1354 lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1355                         struct lpfc_iocbq *rspiocb)
1356 {
1357         IOCB_t *irsp = &rspiocb->iocb;
1358         struct lpfc_vport *vport = cmdiocb->vport;
1359
1360         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1361                 struct lpfc_dmabuf *outp;
1362                 struct lpfc_sli_ct_request *CTrsp;
1363
1364                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1365                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1366                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1367                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1368                         vport->ct_flags |= FC_CT_RFT_ID;
1369         }
1370         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1371         return;
1372 }
1373
1374 static void
1375 lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1376                         struct lpfc_iocbq *rspiocb)
1377 {
1378         IOCB_t *irsp = &rspiocb->iocb;
1379         struct lpfc_vport *vport = cmdiocb->vport;
1380
1381         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1382                 struct lpfc_dmabuf *outp;
1383                 struct lpfc_sli_ct_request *CTrsp;
1384
1385                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1386                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1387                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1388                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1389                         vport->ct_flags |= FC_CT_RNN_ID;
1390         }
1391         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1392         return;
1393 }
1394
1395 static void
1396 lpfc_cmpl_ct_cmd_rspn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1397                          struct lpfc_iocbq *rspiocb)
1398 {
1399         IOCB_t *irsp = &rspiocb->iocb;
1400         struct lpfc_vport *vport = cmdiocb->vport;
1401
1402         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1403                 struct lpfc_dmabuf *outp;
1404                 struct lpfc_sli_ct_request *CTrsp;
1405
1406                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1407                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1408                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1409                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1410                         vport->ct_flags |= FC_CT_RSPN_ID;
1411         }
1412         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1413         return;
1414 }
1415
1416 static void
1417 lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1418                          struct lpfc_iocbq *rspiocb)
1419 {
1420         IOCB_t *irsp = &rspiocb->iocb;
1421         struct lpfc_vport *vport = cmdiocb->vport;
1422
1423         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1424                 struct lpfc_dmabuf *outp;
1425                 struct lpfc_sli_ct_request *CTrsp;
1426
1427                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1428                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1429                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1430                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1431                         vport->ct_flags |= FC_CT_RSNN_NN;
1432         }
1433         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1434         return;
1435 }
1436
1437 static void
1438 lpfc_cmpl_ct_cmd_da_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1439  struct lpfc_iocbq *rspiocb)
1440 {
1441         struct lpfc_vport *vport = cmdiocb->vport;
1442
1443         /* even if it fails we will act as though it succeeded. */
1444         vport->ct_flags = 0;
1445         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1446         return;
1447 }
1448
1449 static void
1450 lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1451                         struct lpfc_iocbq *rspiocb)
1452 {
1453         IOCB_t *irsp = &rspiocb->iocb;
1454         struct lpfc_vport *vport = cmdiocb->vport;
1455
1456         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1457                 struct lpfc_dmabuf *outp;
1458                 struct lpfc_sli_ct_request *CTrsp;
1459
1460                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1461                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1462                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1463                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1464                         vport->ct_flags |= FC_CT_RFF_ID;
1465         }
1466         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1467         return;
1468 }
1469
1470 /*
1471  * Although the symbolic port name is thought to be an integer
1472  * as of January 18, 2016, leave it as a string until more of
1473  * the record state becomes defined.
1474  */
1475 int
1476 lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol,
1477         size_t size)
1478 {
1479         int n;
1480
1481         /*
1482          * Use the lpfc board number as the Symbolic Port
1483          * Name object.  NPIV is not in play so this integer
1484          * value is sufficient and unique per FC-ID.
1485          */
1486         n = scnprintf(symbol, size, "%d", vport->phba->brd_no);
1487         return n;
1488 }
1489
1490
1491 int
1492 lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol,
1493         size_t size)
1494 {
1495         char fwrev[FW_REV_STR_SIZE] = {0};
1496         char tmp[MAXHOSTNAMELEN] = {0};
1497
1498         memset(symbol, 0, size);
1499
1500         scnprintf(tmp, sizeof(tmp), "Emulex %s", vport->phba->ModelName);
1501         if (strlcat(symbol, tmp, size) >= size)
1502                 goto buffer_done;
1503
1504         lpfc_decode_firmware_rev(vport->phba, fwrev, 0);
1505         scnprintf(tmp, sizeof(tmp), " FV%s", fwrev);
1506         if (strlcat(symbol, tmp, size) >= size)
1507                 goto buffer_done;
1508
1509         scnprintf(tmp, sizeof(tmp), " DV%s", lpfc_release_version);
1510         if (strlcat(symbol, tmp, size) >= size)
1511                 goto buffer_done;
1512
1513         scnprintf(tmp, sizeof(tmp), " HN:%s", vport->phba->os_host_name);
1514         if (strlcat(symbol, tmp, size) >= size)
1515                 goto buffer_done;
1516
1517         /* Note :- OS name is "Linux" */
1518         scnprintf(tmp, sizeof(tmp), " OS:%s", init_utsname()->sysname);
1519         strlcat(symbol, tmp, size);
1520
1521 buffer_done:
1522         return strnlen(symbol, size);
1523
1524 }
1525
1526 static uint32_t
1527 lpfc_find_map_node(struct lpfc_vport *vport)
1528 {
1529         struct lpfc_nodelist *ndlp, *next_ndlp;
1530         struct Scsi_Host  *shost;
1531         uint32_t cnt = 0;
1532
1533         shost = lpfc_shost_from_vport(vport);
1534         spin_lock_irq(shost->host_lock);
1535         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1536                 if (ndlp->nlp_type & NLP_FABRIC)
1537                         continue;
1538                 if ((ndlp->nlp_state == NLP_STE_MAPPED_NODE) ||
1539                     (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE))
1540                         cnt++;
1541         }
1542         spin_unlock_irq(shost->host_lock);
1543         return cnt;
1544 }
1545
1546 /*
1547  * This routine will return the FC4 Type associated with the CT
1548  * GID_FT command.
1549  */
1550 int
1551 lpfc_get_gidft_type(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb)
1552 {
1553         struct lpfc_sli_ct_request *CtReq;
1554         struct lpfc_dmabuf *mp;
1555         uint32_t type;
1556
1557         mp = cmdiocb->context1;
1558         if (mp == NULL)
1559                 return 0;
1560         CtReq = (struct lpfc_sli_ct_request *)mp->virt;
1561         type = (uint32_t)CtReq->un.gid.Fc4Type;
1562         if ((type != SLI_CTPT_FCP) && (type != SLI_CTPT_NVME))
1563                 return 0;
1564         return type;
1565 }
1566
1567 /*
1568  * lpfc_ns_cmd
1569  * Description:
1570  *    Issue Cmd to NameServer
1571  *       SLI_CTNS_GID_FT
1572  *       LI_CTNS_RFT_ID
1573  */
1574 int
1575 lpfc_ns_cmd(struct lpfc_vport *vport, int cmdcode,
1576             uint8_t retry, uint32_t context)
1577 {
1578         struct lpfc_nodelist * ndlp;
1579         struct lpfc_hba *phba = vport->phba;
1580         struct lpfc_dmabuf *mp, *bmp;
1581         struct lpfc_sli_ct_request *CtReq;
1582         struct ulp_bde64 *bpl;
1583         void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
1584                       struct lpfc_iocbq *) = NULL;
1585         uint32_t *ptr;
1586         uint32_t rsp_size = 1024;
1587         size_t   size;
1588         int rc = 0;
1589
1590         ndlp = lpfc_findnode_did(vport, NameServer_DID);
1591         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)
1592             || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) {
1593                 rc=1;
1594                 goto ns_cmd_exit;
1595         }
1596
1597         /* fill in BDEs for command */
1598         /* Allocate buffer for command payload */
1599         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1600         if (!mp) {
1601                 rc=2;
1602                 goto ns_cmd_exit;
1603         }
1604
1605         INIT_LIST_HEAD(&mp->list);
1606         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
1607         if (!mp->virt) {
1608                 rc=3;
1609                 goto ns_cmd_free_mp;
1610         }
1611
1612         /* Allocate buffer for Buffer ptr list */
1613         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1614         if (!bmp) {
1615                 rc=4;
1616                 goto ns_cmd_free_mpvirt;
1617         }
1618
1619         INIT_LIST_HEAD(&bmp->list);
1620         bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(bmp->phys));
1621         if (!bmp->virt) {
1622                 rc=5;
1623                 goto ns_cmd_free_bmp;
1624         }
1625
1626         /* NameServer Req */
1627         lpfc_printf_vlog(vport, KERN_INFO ,LOG_DISCOVERY,
1628                          "0236 NameServer Req Data: x%x x%x x%x x%x\n",
1629                          cmdcode, vport->fc_flag, vport->fc_rscn_id_cnt,
1630                          context);
1631
1632         bpl = (struct ulp_bde64 *) bmp->virt;
1633         memset(bpl, 0, sizeof(struct ulp_bde64));
1634         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
1635         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
1636         bpl->tus.f.bdeFlags = 0;
1637         if (cmdcode == SLI_CTNS_GID_FT)
1638                 bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1639         else if (cmdcode == SLI_CTNS_GID_PT)
1640                 bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1641         else if (cmdcode == SLI_CTNS_GFF_ID)
1642                 bpl->tus.f.bdeSize = GFF_REQUEST_SZ;
1643         else if (cmdcode == SLI_CTNS_GFT_ID)
1644                 bpl->tus.f.bdeSize = GFT_REQUEST_SZ;
1645         else if (cmdcode == SLI_CTNS_RFT_ID)
1646                 bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
1647         else if (cmdcode == SLI_CTNS_RNN_ID)
1648                 bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
1649         else if (cmdcode == SLI_CTNS_RSPN_ID)
1650                 bpl->tus.f.bdeSize = RSPN_REQUEST_SZ;
1651         else if (cmdcode == SLI_CTNS_RSNN_NN)
1652                 bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
1653         else if (cmdcode == SLI_CTNS_DA_ID)
1654                 bpl->tus.f.bdeSize = DA_ID_REQUEST_SZ;
1655         else if (cmdcode == SLI_CTNS_RFF_ID)
1656                 bpl->tus.f.bdeSize = RFF_REQUEST_SZ;
1657         else
1658                 bpl->tus.f.bdeSize = 0;
1659         bpl->tus.w = le32_to_cpu(bpl->tus.w);
1660
1661         CtReq = (struct lpfc_sli_ct_request *) mp->virt;
1662         memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
1663         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
1664         CtReq->RevisionId.bits.InId = 0;
1665         CtReq->FsType = SLI_CT_DIRECTORY_SERVICE;
1666         CtReq->FsSubType = SLI_CT_DIRECTORY_NAME_SERVER;
1667         CtReq->CommandResponse.bits.Size = 0;
1668         switch (cmdcode) {
1669         case SLI_CTNS_GID_FT:
1670                 CtReq->CommandResponse.bits.CmdRsp =
1671                     cpu_to_be16(SLI_CTNS_GID_FT);
1672                 CtReq->un.gid.Fc4Type = context;
1673
1674                 if (vport->port_state < LPFC_NS_QRY)
1675                         vport->port_state = LPFC_NS_QRY;
1676                 lpfc_set_disctmo(vport);
1677                 cmpl = lpfc_cmpl_ct_cmd_gid_ft;
1678                 rsp_size = FC_MAX_NS_RSP;
1679                 break;
1680
1681         case SLI_CTNS_GID_PT:
1682                 CtReq->CommandResponse.bits.CmdRsp =
1683                     cpu_to_be16(SLI_CTNS_GID_PT);
1684                 CtReq->un.gid.PortType = context;
1685
1686                 if (vport->port_state < LPFC_NS_QRY)
1687                         vport->port_state = LPFC_NS_QRY;
1688                 lpfc_set_disctmo(vport);
1689                 cmpl = lpfc_cmpl_ct_cmd_gid_pt;
1690                 rsp_size = FC_MAX_NS_RSP;
1691                 break;
1692
1693         case SLI_CTNS_GFF_ID:
1694                 CtReq->CommandResponse.bits.CmdRsp =
1695                         cpu_to_be16(SLI_CTNS_GFF_ID);
1696                 CtReq->un.gff.PortId = cpu_to_be32(context);
1697                 cmpl = lpfc_cmpl_ct_cmd_gff_id;
1698                 break;
1699
1700         case SLI_CTNS_GFT_ID:
1701                 CtReq->CommandResponse.bits.CmdRsp =
1702                         cpu_to_be16(SLI_CTNS_GFT_ID);
1703                 CtReq->un.gft.PortId = cpu_to_be32(context);
1704                 cmpl = lpfc_cmpl_ct_cmd_gft_id;
1705                 break;
1706
1707         case SLI_CTNS_RFT_ID:
1708                 vport->ct_flags &= ~FC_CT_RFT_ID;
1709                 CtReq->CommandResponse.bits.CmdRsp =
1710                     cpu_to_be16(SLI_CTNS_RFT_ID);
1711                 CtReq->un.rft.PortId = cpu_to_be32(vport->fc_myDID);
1712
1713                 /* Register FC4 FCP type if enabled.  */
1714                 if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
1715                     vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)
1716                         CtReq->un.rft.fcpReg = 1;
1717
1718                 /* Register NVME type if enabled.  Defined LE and swapped.
1719                  * rsvd[0] is used as word1 because of the hard-coded
1720                  * word0 usage in the ct_request data structure.
1721                  */
1722                 if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
1723                     vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
1724                         CtReq->un.rft.rsvd[0] =
1725                                 cpu_to_be32(LPFC_FC4_TYPE_BITMASK);
1726
1727                 ptr = (uint32_t *)CtReq;
1728                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1729                                  "6433 Issue RFT (%s %s): %08x %08x %08x %08x "
1730                                  "%08x %08x %08x %08x\n",
1731                                  CtReq->un.rft.fcpReg ? "FCP" : " ",
1732                                  CtReq->un.rft.rsvd[0] ? "NVME" : " ",
1733                                  *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
1734                                  *(ptr + 4), *(ptr + 5),
1735                                  *(ptr + 6), *(ptr + 7));
1736                 cmpl = lpfc_cmpl_ct_cmd_rft_id;
1737                 break;
1738
1739         case SLI_CTNS_RNN_ID:
1740                 vport->ct_flags &= ~FC_CT_RNN_ID;
1741                 CtReq->CommandResponse.bits.CmdRsp =
1742                     cpu_to_be16(SLI_CTNS_RNN_ID);
1743                 CtReq->un.rnn.PortId = cpu_to_be32(vport->fc_myDID);
1744                 memcpy(CtReq->un.rnn.wwnn,  &vport->fc_nodename,
1745                        sizeof(struct lpfc_name));
1746                 cmpl = lpfc_cmpl_ct_cmd_rnn_id;
1747                 break;
1748
1749         case SLI_CTNS_RSPN_ID:
1750                 vport->ct_flags &= ~FC_CT_RSPN_ID;
1751                 CtReq->CommandResponse.bits.CmdRsp =
1752                     cpu_to_be16(SLI_CTNS_RSPN_ID);
1753                 CtReq->un.rspn.PortId = cpu_to_be32(vport->fc_myDID);
1754                 size = sizeof(CtReq->un.rspn.symbname);
1755                 CtReq->un.rspn.len =
1756                         lpfc_vport_symbolic_port_name(vport,
1757                         CtReq->un.rspn.symbname, size);
1758                 cmpl = lpfc_cmpl_ct_cmd_rspn_id;
1759                 break;
1760         case SLI_CTNS_RSNN_NN:
1761                 vport->ct_flags &= ~FC_CT_RSNN_NN;
1762                 CtReq->CommandResponse.bits.CmdRsp =
1763                     cpu_to_be16(SLI_CTNS_RSNN_NN);
1764                 memcpy(CtReq->un.rsnn.wwnn, &vport->fc_nodename,
1765                        sizeof(struct lpfc_name));
1766                 size = sizeof(CtReq->un.rsnn.symbname);
1767                 CtReq->un.rsnn.len =
1768                         lpfc_vport_symbolic_node_name(vport,
1769                         CtReq->un.rsnn.symbname, size);
1770                 cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
1771                 break;
1772         case SLI_CTNS_DA_ID:
1773                 /* Implement DA_ID Nameserver request */
1774                 CtReq->CommandResponse.bits.CmdRsp =
1775                         cpu_to_be16(SLI_CTNS_DA_ID);
1776                 CtReq->un.da_id.port_id = cpu_to_be32(vport->fc_myDID);
1777                 cmpl = lpfc_cmpl_ct_cmd_da_id;
1778                 break;
1779         case SLI_CTNS_RFF_ID:
1780                 vport->ct_flags &= ~FC_CT_RFF_ID;
1781                 CtReq->CommandResponse.bits.CmdRsp =
1782                     cpu_to_be16(SLI_CTNS_RFF_ID);
1783                 CtReq->un.rff.PortId = cpu_to_be32(vport->fc_myDID);
1784                 CtReq->un.rff.fbits = FC4_FEATURE_INIT;
1785
1786                 /* The driver always supports FC_TYPE_FCP.  However, the
1787                  * caller can specify NVME (type x28) as well.  But only
1788                  * these that FC4 type is supported.
1789                  */
1790                 if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1791                      (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
1792                     (context == FC_TYPE_NVME)) {
1793                         if ((vport == phba->pport) && phba->nvmet_support) {
1794                                 CtReq->un.rff.fbits = (FC4_FEATURE_TARGET |
1795                                         FC4_FEATURE_NVME_DISC);
1796                                 lpfc_nvmet_update_targetport(phba);
1797                         } else {
1798                                 lpfc_nvme_update_localport(vport);
1799                         }
1800                         CtReq->un.rff.type_code = context;
1801
1802                 } else if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1803                             (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) &&
1804                            (context == FC_TYPE_FCP))
1805                         CtReq->un.rff.type_code = context;
1806
1807                 else
1808                         goto ns_cmd_free_bmpvirt;
1809
1810                 ptr = (uint32_t *)CtReq;
1811                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1812                                  "6434 Issue RFF (%s): %08x %08x %08x %08x "
1813                                  "%08x %08x %08x %08x\n",
1814                                  (context == FC_TYPE_NVME) ? "NVME" : "FCP",
1815                                  *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
1816                                  *(ptr + 4), *(ptr + 5),
1817                                  *(ptr + 6), *(ptr + 7));
1818                 cmpl = lpfc_cmpl_ct_cmd_rff_id;
1819                 break;
1820         }
1821         /* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
1822          * to hold ndlp reference for the corresponding callback function.
1823          */
1824         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry)) {
1825                 /* On success, The cmpl function will free the buffers */
1826                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1827                         "Issue CT cmd:    cmd:x%x did:x%x",
1828                         cmdcode, ndlp->nlp_DID, 0);
1829                 return 0;
1830         }
1831         rc=6;
1832
1833         /* Decrement ndlp reference count to release ndlp reference held
1834          * for the failed command's callback function.
1835          */
1836         lpfc_nlp_put(ndlp);
1837
1838 ns_cmd_free_bmpvirt:
1839         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1840 ns_cmd_free_bmp:
1841         kfree(bmp);
1842 ns_cmd_free_mpvirt:
1843         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1844 ns_cmd_free_mp:
1845         kfree(mp);
1846 ns_cmd_exit:
1847         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1848                          "0266 Issue NameServer Req x%x err %d Data: x%x x%x\n",
1849                          cmdcode, rc, vport->fc_flag, vport->fc_rscn_id_cnt);
1850         return 1;
1851 }
1852
1853 /**
1854  * lpfc_cmpl_ct_disc_fdmi - Handle a discovery FDMI completion
1855  * @phba: Pointer to HBA context object.
1856  * @cmdiocb: Pointer to the command IOCBQ.
1857  * @rspiocb: Pointer to the response IOCBQ.
1858  *
1859  * This function to handle the completion of a driver initiated FDMI
1860  * CT command issued during discovery.
1861  */
1862 static void
1863 lpfc_cmpl_ct_disc_fdmi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1864                        struct lpfc_iocbq *rspiocb)
1865 {
1866         struct lpfc_vport *vport = cmdiocb->vport;
1867         struct lpfc_dmabuf *inp = cmdiocb->context1;
1868         struct lpfc_dmabuf *outp = cmdiocb->context2;
1869         struct lpfc_sli_ct_request *CTcmd = inp->virt;
1870         struct lpfc_sli_ct_request *CTrsp = outp->virt;
1871         uint16_t fdmi_cmd = CTcmd->CommandResponse.bits.CmdRsp;
1872         uint16_t fdmi_rsp = CTrsp->CommandResponse.bits.CmdRsp;
1873         IOCB_t *irsp = &rspiocb->iocb;
1874         struct lpfc_nodelist *ndlp;
1875         uint32_t latt, cmd, err;
1876
1877         latt = lpfc_els_chk_latt(vport);
1878         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1879                 "FDMI cmpl:       status:x%x/x%x latt:%d",
1880                 irsp->ulpStatus, irsp->un.ulpWord[4], latt);
1881
1882         if (latt || irsp->ulpStatus) {
1883
1884                 /* Look for a retryable error */
1885                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1886                         switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
1887                         case IOERR_SLI_ABORTED:
1888                         case IOERR_SLI_DOWN:
1889                                 /* Driver aborted this IO.  No retry as error
1890                                  * is likely Offline->Online or some adapter
1891                                  * error.  Recovery will try again.
1892                                  */
1893                                 break;
1894                         case IOERR_ABORT_IN_PROGRESS:
1895                         case IOERR_SEQUENCE_TIMEOUT:
1896                         case IOERR_ILLEGAL_FRAME:
1897                         case IOERR_NO_RESOURCES:
1898                         case IOERR_ILLEGAL_COMMAND:
1899                                 cmdiocb->retry++;
1900                                 if (cmdiocb->retry >= LPFC_FDMI_MAX_RETRY)
1901                                         break;
1902
1903                                 /* Retry the same FDMI command */
1904                                 err = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING,
1905                                                           cmdiocb, 0);
1906                                 if (err == IOCB_ERROR)
1907                                         break;
1908                                 return;
1909                         default:
1910                                 break;
1911                         }
1912                 }
1913
1914                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1915                                  "0229 FDMI cmd %04x failed, latt = %d "
1916                                  "ulpStatus: x%x, rid x%x\n",
1917                                  be16_to_cpu(fdmi_cmd), latt, irsp->ulpStatus,
1918                                  irsp->un.ulpWord[4]);
1919         }
1920         lpfc_ct_free_iocb(phba, cmdiocb);
1921
1922         ndlp = lpfc_findnode_did(vport, FDMI_DID);
1923         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
1924                 return;
1925
1926         /* Check for a CT LS_RJT response */
1927         cmd =  be16_to_cpu(fdmi_cmd);
1928         if (fdmi_rsp == cpu_to_be16(SLI_CT_RESPONSE_FS_RJT)) {
1929                 /* FDMI rsp failed */
1930                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1931                                  "0220 FDMI cmd failed FS_RJT Data: x%x", cmd);
1932
1933                 /* Should we fallback to FDMI-2 / FDMI-1 ? */
1934                 switch (cmd) {
1935                 case SLI_MGMT_RHBA:
1936                         if (vport->fdmi_hba_mask == LPFC_FDMI2_HBA_ATTR) {
1937                                 /* Fallback to FDMI-1 */
1938                                 vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
1939                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
1940                                 /* Start over */
1941                                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
1942                         }
1943                         return;
1944
1945                 case SLI_MGMT_RPRT:
1946                         if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
1947                                 /* Fallback to FDMI-1 */
1948                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
1949                                 /* Start over */
1950                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
1951                         }
1952                         if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
1953                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
1954                                 /* Retry the same command */
1955                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
1956                         }
1957                         return;
1958
1959                 case SLI_MGMT_RPA:
1960                         if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
1961                                 /* Fallback to FDMI-1 */
1962                                 vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
1963                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
1964                                 /* Start over */
1965                                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
1966                         }
1967                         if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
1968                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
1969                                 /* Retry the same command */
1970                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
1971                         }
1972                         return;
1973                 }
1974         }
1975
1976         /*
1977          * On success, need to cycle thru FDMI registration for discovery
1978          * DHBA -> DPRT -> RHBA -> RPA  (physical port)
1979          * DPRT -> RPRT (vports)
1980          */
1981         switch (cmd) {
1982         case SLI_MGMT_RHBA:
1983                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA, 0);
1984                 break;
1985
1986         case SLI_MGMT_DHBA:
1987                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
1988                 break;
1989
1990         case SLI_MGMT_DPRT:
1991                 if (vport->port_type == LPFC_PHYSICAL_PORT)
1992                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RHBA, 0);
1993                 else
1994                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT, 0);
1995                 break;
1996         }
1997         return;
1998 }
1999
2000
2001 /**
2002  * lpfc_fdmi_change_check - Check for changed FDMI parameters
2003  * @vport: pointer to a host virtual N_Port data structure.
2004  *
2005  * Check how many mapped NPorts we are connected to
2006  * Check if our hostname changed
2007  * Called from hbeat timeout routine to check if any FDMI parameters
2008  * changed. If so, re-register those Attributes.
2009  */
2010 void
2011 lpfc_fdmi_change_check(struct lpfc_vport *vport)
2012 {
2013         struct lpfc_hba *phba = vport->phba;
2014         struct lpfc_nodelist *ndlp;
2015         uint16_t cnt;
2016
2017         if (!lpfc_is_link_up(phba))
2018                 return;
2019
2020         /* Must be connected to a Fabric */
2021         if (!(vport->fc_flag & FC_FABRIC))
2022                 return;
2023
2024         ndlp = lpfc_findnode_did(vport, FDMI_DID);
2025         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2026                 return;
2027
2028         /* Check if system hostname changed */
2029         if (strcmp(phba->os_host_name, init_utsname()->nodename)) {
2030                 memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
2031                 scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
2032                           init_utsname()->nodename);
2033                 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2034
2035                 /* Since this effects multiple HBA and PORT attributes, we need
2036                  * de-register and go thru the whole FDMI registration cycle.
2037                  * DHBA -> DPRT -> RHBA -> RPA  (physical port)
2038                  * DPRT -> RPRT (vports)
2039                  */
2040                 if (vport->port_type == LPFC_PHYSICAL_PORT)
2041                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2042                 else
2043                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
2044
2045                 /* Since this code path registers all the port attributes
2046                  * we can just return without further checking.
2047                  */
2048                 return;
2049         }
2050
2051         if (!(vport->fdmi_port_mask & LPFC_FDMI_PORT_ATTR_num_disc))
2052                 return;
2053
2054         /* Check if the number of mapped NPorts changed */
2055         cnt = lpfc_find_map_node(vport);
2056         if (cnt == vport->fdmi_num_disc)
2057                 return;
2058
2059         if (vport->port_type == LPFC_PHYSICAL_PORT) {
2060                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA,
2061                               LPFC_FDMI_PORT_ATTR_num_disc);
2062         } else {
2063                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT,
2064                               LPFC_FDMI_PORT_ATTR_num_disc);
2065         }
2066 }
2067
2068 /* Routines for all individual HBA attributes */
2069 static int
2070 lpfc_fdmi_hba_attr_wwnn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2071 {
2072         struct lpfc_fdmi_attr_entry *ae;
2073         uint32_t size;
2074
2075         ae = &ad->AttrValue;
2076         memset(ae, 0, sizeof(*ae));
2077
2078         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
2079                sizeof(struct lpfc_name));
2080         size = FOURBYTES + sizeof(struct lpfc_name);
2081         ad->AttrLen = cpu_to_be16(size);
2082         ad->AttrType = cpu_to_be16(RHBA_NODENAME);
2083         return size;
2084 }
2085 static int
2086 lpfc_fdmi_hba_attr_manufacturer(struct lpfc_vport *vport,
2087                                 struct lpfc_fdmi_attr_def *ad)
2088 {
2089         struct lpfc_fdmi_attr_entry *ae;
2090         uint32_t len, size;
2091
2092         ae = &ad->AttrValue;
2093         memset(ae, 0, sizeof(*ae));
2094
2095         /* This string MUST be consistent with other FC platforms
2096          * supported by Broadcom.
2097          */
2098         strncpy(ae->un.AttrString,
2099                 "Emulex Corporation",
2100                        sizeof(ae->un.AttrString));
2101         len = strnlen(ae->un.AttrString,
2102                           sizeof(ae->un.AttrString));
2103         len += (len & 3) ? (4 - (len & 3)) : 4;
2104         size = FOURBYTES + len;
2105         ad->AttrLen = cpu_to_be16(size);
2106         ad->AttrType = cpu_to_be16(RHBA_MANUFACTURER);
2107         return size;
2108 }
2109
2110 static int
2111 lpfc_fdmi_hba_attr_sn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2112 {
2113         struct lpfc_hba *phba = vport->phba;
2114         struct lpfc_fdmi_attr_entry *ae;
2115         uint32_t len, size;
2116
2117         ae = &ad->AttrValue;
2118         memset(ae, 0, sizeof(*ae));
2119
2120         strncpy(ae->un.AttrString, phba->SerialNumber,
2121                 sizeof(ae->un.AttrString));
2122         len = strnlen(ae->un.AttrString,
2123                           sizeof(ae->un.AttrString));
2124         len += (len & 3) ? (4 - (len & 3)) : 4;
2125         size = FOURBYTES + len;
2126         ad->AttrLen = cpu_to_be16(size);
2127         ad->AttrType = cpu_to_be16(RHBA_SERIAL_NUMBER);
2128         return size;
2129 }
2130
2131 static int
2132 lpfc_fdmi_hba_attr_model(struct lpfc_vport *vport,
2133                          struct lpfc_fdmi_attr_def *ad)
2134 {
2135         struct lpfc_hba *phba = vport->phba;
2136         struct lpfc_fdmi_attr_entry *ae;
2137         uint32_t len, size;
2138
2139         ae = &ad->AttrValue;
2140         memset(ae, 0, sizeof(*ae));
2141
2142         strncpy(ae->un.AttrString, phba->ModelName,
2143                 sizeof(ae->un.AttrString));
2144         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2145         len += (len & 3) ? (4 - (len & 3)) : 4;
2146         size = FOURBYTES + len;
2147         ad->AttrLen = cpu_to_be16(size);
2148         ad->AttrType = cpu_to_be16(RHBA_MODEL);
2149         return size;
2150 }
2151
2152 static int
2153 lpfc_fdmi_hba_attr_description(struct lpfc_vport *vport,
2154                                struct lpfc_fdmi_attr_def *ad)
2155 {
2156         struct lpfc_hba *phba = vport->phba;
2157         struct lpfc_fdmi_attr_entry *ae;
2158         uint32_t len, size;
2159
2160         ae = &ad->AttrValue;
2161         memset(ae, 0, sizeof(*ae));
2162
2163         strncpy(ae->un.AttrString, phba->ModelDesc,
2164                 sizeof(ae->un.AttrString));
2165         len = strnlen(ae->un.AttrString,
2166                                   sizeof(ae->un.AttrString));
2167         len += (len & 3) ? (4 - (len & 3)) : 4;
2168         size = FOURBYTES + len;
2169         ad->AttrLen = cpu_to_be16(size);
2170         ad->AttrType = cpu_to_be16(RHBA_MODEL_DESCRIPTION);
2171         return size;
2172 }
2173
2174 static int
2175 lpfc_fdmi_hba_attr_hdw_ver(struct lpfc_vport *vport,
2176                            struct lpfc_fdmi_attr_def *ad)
2177 {
2178         struct lpfc_hba *phba = vport->phba;
2179         lpfc_vpd_t *vp = &phba->vpd;
2180         struct lpfc_fdmi_attr_entry *ae;
2181         uint32_t i, j, incr, size;
2182
2183         ae = &ad->AttrValue;
2184         memset(ae, 0, sizeof(*ae));
2185
2186         /* Convert JEDEC ID to ascii for hardware version */
2187         incr = vp->rev.biuRev;
2188         for (i = 0; i < 8; i++) {
2189                 j = (incr & 0xf);
2190                 if (j <= 9)
2191                         ae->un.AttrString[7 - i] =
2192                             (char)((uint8_t) 0x30 +
2193                                    (uint8_t) j);
2194                 else
2195                         ae->un.AttrString[7 - i] =
2196                             (char)((uint8_t) 0x61 +
2197                                    (uint8_t) (j - 10));
2198                 incr = (incr >> 4);
2199         }
2200         size = FOURBYTES + 8;
2201         ad->AttrLen = cpu_to_be16(size);
2202         ad->AttrType = cpu_to_be16(RHBA_HARDWARE_VERSION);
2203         return size;
2204 }
2205
2206 static int
2207 lpfc_fdmi_hba_attr_drvr_ver(struct lpfc_vport *vport,
2208                             struct lpfc_fdmi_attr_def *ad)
2209 {
2210         struct lpfc_fdmi_attr_entry *ae;
2211         uint32_t len, size;
2212
2213         ae = &ad->AttrValue;
2214         memset(ae, 0, sizeof(*ae));
2215
2216         strncpy(ae->un.AttrString, lpfc_release_version,
2217                 sizeof(ae->un.AttrString));
2218         len = strnlen(ae->un.AttrString,
2219                           sizeof(ae->un.AttrString));
2220         len += (len & 3) ? (4 - (len & 3)) : 4;
2221         size = FOURBYTES + len;
2222         ad->AttrLen = cpu_to_be16(size);
2223         ad->AttrType = cpu_to_be16(RHBA_DRIVER_VERSION);
2224         return size;
2225 }
2226
2227 static int
2228 lpfc_fdmi_hba_attr_rom_ver(struct lpfc_vport *vport,
2229                            struct lpfc_fdmi_attr_def *ad)
2230 {
2231         struct lpfc_hba *phba = vport->phba;
2232         struct lpfc_fdmi_attr_entry *ae;
2233         uint32_t len, size;
2234
2235         ae = &ad->AttrValue;
2236         memset(ae, 0, sizeof(*ae));
2237
2238         if (phba->sli_rev == LPFC_SLI_REV4)
2239                 lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2240         else
2241                 strncpy(ae->un.AttrString, phba->OptionROMVersion,
2242                         sizeof(ae->un.AttrString));
2243         len = strnlen(ae->un.AttrString,
2244                           sizeof(ae->un.AttrString));
2245         len += (len & 3) ? (4 - (len & 3)) : 4;
2246         size = FOURBYTES + len;
2247         ad->AttrLen = cpu_to_be16(size);
2248         ad->AttrType = cpu_to_be16(RHBA_OPTION_ROM_VERSION);
2249         return size;
2250 }
2251
2252 static int
2253 lpfc_fdmi_hba_attr_fmw_ver(struct lpfc_vport *vport,
2254                            struct lpfc_fdmi_attr_def *ad)
2255 {
2256         struct lpfc_hba *phba = vport->phba;
2257         struct lpfc_fdmi_attr_entry *ae;
2258         uint32_t len, size;
2259
2260         ae = &ad->AttrValue;
2261         memset(ae, 0, sizeof(*ae));
2262
2263         lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2264         len = strnlen(ae->un.AttrString,
2265                           sizeof(ae->un.AttrString));
2266         len += (len & 3) ? (4 - (len & 3)) : 4;
2267         size = FOURBYTES + len;
2268         ad->AttrLen = cpu_to_be16(size);
2269         ad->AttrType = cpu_to_be16(RHBA_FIRMWARE_VERSION);
2270         return size;
2271 }
2272
2273 static int
2274 lpfc_fdmi_hba_attr_os_ver(struct lpfc_vport *vport,
2275                           struct lpfc_fdmi_attr_def *ad)
2276 {
2277         struct lpfc_fdmi_attr_entry *ae;
2278         uint32_t len, size;
2279
2280         ae = &ad->AttrValue;
2281         memset(ae, 0, sizeof(*ae));
2282
2283         snprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s %s %s",
2284                  init_utsname()->sysname,
2285                  init_utsname()->release,
2286                  init_utsname()->version);
2287
2288         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2289         len += (len & 3) ? (4 - (len & 3)) : 4;
2290         size = FOURBYTES + len;
2291         ad->AttrLen = cpu_to_be16(size);
2292         ad->AttrType = cpu_to_be16(RHBA_OS_NAME_VERSION);
2293         return size;
2294 }
2295
2296 static int
2297 lpfc_fdmi_hba_attr_ct_len(struct lpfc_vport *vport,
2298                           struct lpfc_fdmi_attr_def *ad)
2299 {
2300         struct lpfc_fdmi_attr_entry *ae;
2301         uint32_t size;
2302
2303         ae = &ad->AttrValue;
2304
2305         ae->un.AttrInt =  cpu_to_be32(LPFC_MAX_CT_SIZE);
2306         size = FOURBYTES + sizeof(uint32_t);
2307         ad->AttrLen = cpu_to_be16(size);
2308         ad->AttrType = cpu_to_be16(RHBA_MAX_CT_PAYLOAD_LEN);
2309         return size;
2310 }
2311
2312 static int
2313 lpfc_fdmi_hba_attr_symbolic_name(struct lpfc_vport *vport,
2314                                  struct lpfc_fdmi_attr_def *ad)
2315 {
2316         struct lpfc_fdmi_attr_entry *ae;
2317         uint32_t len, size;
2318
2319         ae = &ad->AttrValue;
2320         memset(ae, 0, sizeof(*ae));
2321
2322         len = lpfc_vport_symbolic_node_name(vport,
2323                                 ae->un.AttrString, 256);
2324         len += (len & 3) ? (4 - (len & 3)) : 4;
2325         size = FOURBYTES + len;
2326         ad->AttrLen = cpu_to_be16(size);
2327         ad->AttrType = cpu_to_be16(RHBA_SYM_NODENAME);
2328         return size;
2329 }
2330
2331 static int
2332 lpfc_fdmi_hba_attr_vendor_info(struct lpfc_vport *vport,
2333                                struct lpfc_fdmi_attr_def *ad)
2334 {
2335         struct lpfc_fdmi_attr_entry *ae;
2336         uint32_t size;
2337
2338         ae = &ad->AttrValue;
2339
2340         /* Nothing is defined for this currently */
2341         ae->un.AttrInt =  cpu_to_be32(0);
2342         size = FOURBYTES + sizeof(uint32_t);
2343         ad->AttrLen = cpu_to_be16(size);
2344         ad->AttrType = cpu_to_be16(RHBA_VENDOR_INFO);
2345         return size;
2346 }
2347
2348 static int
2349 lpfc_fdmi_hba_attr_num_ports(struct lpfc_vport *vport,
2350                              struct lpfc_fdmi_attr_def *ad)
2351 {
2352         struct lpfc_fdmi_attr_entry *ae;
2353         uint32_t size;
2354
2355         ae = &ad->AttrValue;
2356
2357         /* Each driver instance corresponds to a single port */
2358         ae->un.AttrInt =  cpu_to_be32(1);
2359         size = FOURBYTES + sizeof(uint32_t);
2360         ad->AttrLen = cpu_to_be16(size);
2361         ad->AttrType = cpu_to_be16(RHBA_NUM_PORTS);
2362         return size;
2363 }
2364
2365 static int
2366 lpfc_fdmi_hba_attr_fabric_wwnn(struct lpfc_vport *vport,
2367                                struct lpfc_fdmi_attr_def *ad)
2368 {
2369         struct lpfc_fdmi_attr_entry *ae;
2370         uint32_t size;
2371
2372         ae = &ad->AttrValue;
2373         memset(ae, 0, sizeof(*ae));
2374
2375         memcpy(&ae->un.AttrWWN, &vport->fabric_nodename,
2376                sizeof(struct lpfc_name));
2377         size = FOURBYTES + sizeof(struct lpfc_name);
2378         ad->AttrLen = cpu_to_be16(size);
2379         ad->AttrType = cpu_to_be16(RHBA_FABRIC_WWNN);
2380         return size;
2381 }
2382
2383 static int
2384 lpfc_fdmi_hba_attr_bios_ver(struct lpfc_vport *vport,
2385                             struct lpfc_fdmi_attr_def *ad)
2386 {
2387         struct lpfc_hba *phba = vport->phba;
2388         struct lpfc_fdmi_attr_entry *ae;
2389         uint32_t len, size;
2390
2391         ae = &ad->AttrValue;
2392         memset(ae, 0, sizeof(*ae));
2393
2394         strlcat(ae->un.AttrString, phba->BIOSVersion,
2395                 sizeof(ae->un.AttrString));
2396         len = strnlen(ae->un.AttrString,
2397                           sizeof(ae->un.AttrString));
2398         len += (len & 3) ? (4 - (len & 3)) : 4;
2399         size = FOURBYTES + len;
2400         ad->AttrLen = cpu_to_be16(size);
2401         ad->AttrType = cpu_to_be16(RHBA_BIOS_VERSION);
2402         return size;
2403 }
2404
2405 static int
2406 lpfc_fdmi_hba_attr_bios_state(struct lpfc_vport *vport,
2407                               struct lpfc_fdmi_attr_def *ad)
2408 {
2409         struct lpfc_fdmi_attr_entry *ae;
2410         uint32_t size;
2411
2412         ae = &ad->AttrValue;
2413
2414         /* Driver doesn't have access to this information */
2415         ae->un.AttrInt =  cpu_to_be32(0);
2416         size = FOURBYTES + sizeof(uint32_t);
2417         ad->AttrLen = cpu_to_be16(size);
2418         ad->AttrType = cpu_to_be16(RHBA_BIOS_STATE);
2419         return size;
2420 }
2421
2422 static int
2423 lpfc_fdmi_hba_attr_vendor_id(struct lpfc_vport *vport,
2424                              struct lpfc_fdmi_attr_def *ad)
2425 {
2426         struct lpfc_fdmi_attr_entry *ae;
2427         uint32_t len, size;
2428
2429         ae = &ad->AttrValue;
2430         memset(ae, 0, sizeof(*ae));
2431
2432         strncpy(ae->un.AttrString, "EMULEX",
2433                 sizeof(ae->un.AttrString));
2434         len = strnlen(ae->un.AttrString,
2435                           sizeof(ae->un.AttrString));
2436         len += (len & 3) ? (4 - (len & 3)) : 4;
2437         size = FOURBYTES + len;
2438         ad->AttrLen = cpu_to_be16(size);
2439         ad->AttrType = cpu_to_be16(RHBA_VENDOR_ID);
2440         return size;
2441 }
2442
2443 /* Routines for all individual PORT attributes */
2444 static int
2445 lpfc_fdmi_port_attr_fc4type(struct lpfc_vport *vport,
2446                             struct lpfc_fdmi_attr_def *ad)
2447 {
2448         struct lpfc_hba   *phba = vport->phba;
2449         struct lpfc_fdmi_attr_entry *ae;
2450         uint32_t size;
2451
2452         ae = &ad->AttrValue;
2453         memset(ae, 0, sizeof(*ae));
2454
2455         ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
2456         ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
2457
2458         /* Check to see if Firmware supports NVME and on physical port */
2459         if ((phba->sli_rev == LPFC_SLI_REV4) && (vport == phba->pport) &&
2460             phba->sli4_hba.pc_sli4_params.nvme)
2461                 ae->un.AttrTypes[6] = 0x01; /* Type 0x28 - NVME */
2462
2463         size = FOURBYTES + 32;
2464         ad->AttrLen = cpu_to_be16(size);
2465         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_FC4_TYPES);
2466         return size;
2467 }
2468
2469 static int
2470 lpfc_fdmi_port_attr_support_speed(struct lpfc_vport *vport,
2471                                   struct lpfc_fdmi_attr_def *ad)
2472 {
2473         struct lpfc_hba   *phba = vport->phba;
2474         struct lpfc_fdmi_attr_entry *ae;
2475         uint32_t size;
2476
2477         ae = &ad->AttrValue;
2478
2479         ae->un.AttrInt = 0;
2480         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2481                 if (phba->lmt & LMT_128Gb)
2482                         ae->un.AttrInt |= HBA_PORTSPEED_128GFC;
2483                 if (phba->lmt & LMT_64Gb)
2484                         ae->un.AttrInt |= HBA_PORTSPEED_64GFC;
2485                 if (phba->lmt & LMT_32Gb)
2486                         ae->un.AttrInt |= HBA_PORTSPEED_32GFC;
2487                 if (phba->lmt & LMT_16Gb)
2488                         ae->un.AttrInt |= HBA_PORTSPEED_16GFC;
2489                 if (phba->lmt & LMT_10Gb)
2490                         ae->un.AttrInt |= HBA_PORTSPEED_10GFC;
2491                 if (phba->lmt & LMT_8Gb)
2492                         ae->un.AttrInt |= HBA_PORTSPEED_8GFC;
2493                 if (phba->lmt & LMT_4Gb)
2494                         ae->un.AttrInt |= HBA_PORTSPEED_4GFC;
2495                 if (phba->lmt & LMT_2Gb)
2496                         ae->un.AttrInt |= HBA_PORTSPEED_2GFC;
2497                 if (phba->lmt & LMT_1Gb)
2498                         ae->un.AttrInt |= HBA_PORTSPEED_1GFC;
2499         } else {
2500                 /* FCoE links support only one speed */
2501                 switch (phba->fc_linkspeed) {
2502                 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2503                         ae->un.AttrInt = HBA_PORTSPEED_10GE;
2504                         break;
2505                 case LPFC_ASYNC_LINK_SPEED_25GBPS:
2506                         ae->un.AttrInt = HBA_PORTSPEED_25GE;
2507                         break;
2508                 case LPFC_ASYNC_LINK_SPEED_40GBPS:
2509                         ae->un.AttrInt = HBA_PORTSPEED_40GE;
2510                         break;
2511                 case LPFC_ASYNC_LINK_SPEED_100GBPS:
2512                         ae->un.AttrInt = HBA_PORTSPEED_100GE;
2513                         break;
2514                 }
2515         }
2516         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2517         size = FOURBYTES + sizeof(uint32_t);
2518         ad->AttrLen = cpu_to_be16(size);
2519         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_SPEED);
2520         return size;
2521 }
2522
2523 static int
2524 lpfc_fdmi_port_attr_speed(struct lpfc_vport *vport,
2525                           struct lpfc_fdmi_attr_def *ad)
2526 {
2527         struct lpfc_hba   *phba = vport->phba;
2528         struct lpfc_fdmi_attr_entry *ae;
2529         uint32_t size;
2530
2531         ae = &ad->AttrValue;
2532
2533         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2534                 switch (phba->fc_linkspeed) {
2535                 case LPFC_LINK_SPEED_1GHZ:
2536                         ae->un.AttrInt = HBA_PORTSPEED_1GFC;
2537                         break;
2538                 case LPFC_LINK_SPEED_2GHZ:
2539                         ae->un.AttrInt = HBA_PORTSPEED_2GFC;
2540                         break;
2541                 case LPFC_LINK_SPEED_4GHZ:
2542                         ae->un.AttrInt = HBA_PORTSPEED_4GFC;
2543                         break;
2544                 case LPFC_LINK_SPEED_8GHZ:
2545                         ae->un.AttrInt = HBA_PORTSPEED_8GFC;
2546                         break;
2547                 case LPFC_LINK_SPEED_10GHZ:
2548                         ae->un.AttrInt = HBA_PORTSPEED_10GFC;
2549                         break;
2550                 case LPFC_LINK_SPEED_16GHZ:
2551                         ae->un.AttrInt = HBA_PORTSPEED_16GFC;
2552                         break;
2553                 case LPFC_LINK_SPEED_32GHZ:
2554                         ae->un.AttrInt = HBA_PORTSPEED_32GFC;
2555                         break;
2556                 case LPFC_LINK_SPEED_64GHZ:
2557                         ae->un.AttrInt = HBA_PORTSPEED_64GFC;
2558                         break;
2559                 case LPFC_LINK_SPEED_128GHZ:
2560                         ae->un.AttrInt = HBA_PORTSPEED_128GFC;
2561                         break;
2562                 default:
2563                         ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2564                         break;
2565                 }
2566         } else {
2567                 switch (phba->fc_linkspeed) {
2568                 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2569                         ae->un.AttrInt = HBA_PORTSPEED_10GE;
2570                         break;
2571                 case LPFC_ASYNC_LINK_SPEED_25GBPS:
2572                         ae->un.AttrInt = HBA_PORTSPEED_25GE;
2573                         break;
2574                 case LPFC_ASYNC_LINK_SPEED_40GBPS:
2575                         ae->un.AttrInt = HBA_PORTSPEED_40GE;
2576                         break;
2577                 case LPFC_ASYNC_LINK_SPEED_100GBPS:
2578                         ae->un.AttrInt = HBA_PORTSPEED_100GE;
2579                         break;
2580                 default:
2581                         ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2582                         break;
2583                 }
2584         }
2585
2586         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2587         size = FOURBYTES + sizeof(uint32_t);
2588         ad->AttrLen = cpu_to_be16(size);
2589         ad->AttrType = cpu_to_be16(RPRT_PORT_SPEED);
2590         return size;
2591 }
2592
2593 static int
2594 lpfc_fdmi_port_attr_max_frame(struct lpfc_vport *vport,
2595                               struct lpfc_fdmi_attr_def *ad)
2596 {
2597         struct serv_parm *hsp;
2598         struct lpfc_fdmi_attr_entry *ae;
2599         uint32_t size;
2600
2601         ae = &ad->AttrValue;
2602
2603         hsp = (struct serv_parm *)&vport->fc_sparam;
2604         ae->un.AttrInt = (((uint32_t) hsp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
2605                           (uint32_t) hsp->cmn.bbRcvSizeLsb;
2606         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2607         size = FOURBYTES + sizeof(uint32_t);
2608         ad->AttrLen = cpu_to_be16(size);
2609         ad->AttrType = cpu_to_be16(RPRT_MAX_FRAME_SIZE);
2610         return size;
2611 }
2612
2613 static int
2614 lpfc_fdmi_port_attr_os_devname(struct lpfc_vport *vport,
2615                                struct lpfc_fdmi_attr_def *ad)
2616 {
2617         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2618         struct lpfc_fdmi_attr_entry *ae;
2619         uint32_t len, size;
2620
2621         ae = &ad->AttrValue;
2622         memset(ae, 0, sizeof(*ae));
2623
2624         snprintf(ae->un.AttrString, sizeof(ae->un.AttrString),
2625                  "/sys/class/scsi_host/host%d", shost->host_no);
2626         len = strnlen((char *)ae->un.AttrString,
2627                           sizeof(ae->un.AttrString));
2628         len += (len & 3) ? (4 - (len & 3)) : 4;
2629         size = FOURBYTES + len;
2630         ad->AttrLen = cpu_to_be16(size);
2631         ad->AttrType = cpu_to_be16(RPRT_OS_DEVICE_NAME);
2632         return size;
2633 }
2634
2635 static int
2636 lpfc_fdmi_port_attr_host_name(struct lpfc_vport *vport,
2637                               struct lpfc_fdmi_attr_def *ad)
2638 {
2639         struct lpfc_fdmi_attr_entry *ae;
2640         uint32_t len, size;
2641
2642         ae = &ad->AttrValue;
2643         memset(ae, 0, sizeof(*ae));
2644
2645         scnprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s",
2646                   vport->phba->os_host_name);
2647
2648         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2649         len += (len & 3) ? (4 - (len & 3)) : 4;
2650         size = FOURBYTES + len;
2651         ad->AttrLen = cpu_to_be16(size);
2652         ad->AttrType = cpu_to_be16(RPRT_HOST_NAME);
2653         return size;
2654 }
2655
2656 static int
2657 lpfc_fdmi_port_attr_wwnn(struct lpfc_vport *vport,
2658                          struct lpfc_fdmi_attr_def *ad)
2659 {
2660         struct lpfc_fdmi_attr_entry *ae;
2661         uint32_t size;
2662
2663         ae = &ad->AttrValue;
2664         memset(ae, 0, sizeof(*ae));
2665
2666         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
2667                sizeof(struct lpfc_name));
2668         size = FOURBYTES + sizeof(struct lpfc_name);
2669         ad->AttrLen = cpu_to_be16(size);
2670         ad->AttrType = cpu_to_be16(RPRT_NODENAME);
2671         return size;
2672 }
2673
2674 static int
2675 lpfc_fdmi_port_attr_wwpn(struct lpfc_vport *vport,
2676                          struct lpfc_fdmi_attr_def *ad)
2677 {
2678         struct lpfc_fdmi_attr_entry *ae;
2679         uint32_t size;
2680
2681         ae = &ad->AttrValue;
2682         memset(ae, 0, sizeof(*ae));
2683
2684         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.portName,
2685                sizeof(struct lpfc_name));
2686         size = FOURBYTES + sizeof(struct lpfc_name);
2687         ad->AttrLen = cpu_to_be16(size);
2688         ad->AttrType = cpu_to_be16(RPRT_PORTNAME);
2689         return size;
2690 }
2691
2692 static int
2693 lpfc_fdmi_port_attr_symbolic_name(struct lpfc_vport *vport,
2694                                   struct lpfc_fdmi_attr_def *ad)
2695 {
2696         struct lpfc_fdmi_attr_entry *ae;
2697         uint32_t len, size;
2698
2699         ae = &ad->AttrValue;
2700         memset(ae, 0, sizeof(*ae));
2701
2702         len = lpfc_vport_symbolic_port_name(vport, ae->un.AttrString, 256);
2703         len += (len & 3) ? (4 - (len & 3)) : 4;
2704         size = FOURBYTES + len;
2705         ad->AttrLen = cpu_to_be16(size);
2706         ad->AttrType = cpu_to_be16(RPRT_SYM_PORTNAME);
2707         return size;
2708 }
2709
2710 static int
2711 lpfc_fdmi_port_attr_port_type(struct lpfc_vport *vport,
2712                               struct lpfc_fdmi_attr_def *ad)
2713 {
2714         struct lpfc_hba *phba = vport->phba;
2715         struct lpfc_fdmi_attr_entry *ae;
2716         uint32_t size;
2717
2718         ae = &ad->AttrValue;
2719         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP)
2720                 ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NLPORT);
2721         else
2722                 ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NPORT);
2723         size = FOURBYTES + sizeof(uint32_t);
2724         ad->AttrLen = cpu_to_be16(size);
2725         ad->AttrType = cpu_to_be16(RPRT_PORT_TYPE);
2726         return size;
2727 }
2728
2729 static int
2730 lpfc_fdmi_port_attr_class(struct lpfc_vport *vport,
2731                           struct lpfc_fdmi_attr_def *ad)
2732 {
2733         struct lpfc_fdmi_attr_entry *ae;
2734         uint32_t size;
2735
2736         ae = &ad->AttrValue;
2737         ae->un.AttrInt = cpu_to_be32(FC_COS_CLASS2 | FC_COS_CLASS3);
2738         size = FOURBYTES + sizeof(uint32_t);
2739         ad->AttrLen = cpu_to_be16(size);
2740         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_CLASS);
2741         return size;
2742 }
2743
2744 static int
2745 lpfc_fdmi_port_attr_fabric_wwpn(struct lpfc_vport *vport,
2746                                 struct lpfc_fdmi_attr_def *ad)
2747 {
2748         struct lpfc_fdmi_attr_entry *ae;
2749         uint32_t size;
2750
2751         ae = &ad->AttrValue;
2752         memset(ae, 0, sizeof(*ae));
2753
2754         memcpy(&ae->un.AttrWWN, &vport->fabric_portname,
2755                sizeof(struct lpfc_name));
2756         size = FOURBYTES + sizeof(struct lpfc_name);
2757         ad->AttrLen = cpu_to_be16(size);
2758         ad->AttrType = cpu_to_be16(RPRT_FABRICNAME);
2759         return size;
2760 }
2761
2762 static int
2763 lpfc_fdmi_port_attr_active_fc4type(struct lpfc_vport *vport,
2764                                    struct lpfc_fdmi_attr_def *ad)
2765 {
2766         struct lpfc_fdmi_attr_entry *ae;
2767         uint32_t size;
2768
2769         ae = &ad->AttrValue;
2770         memset(ae, 0, sizeof(*ae));
2771
2772         ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
2773         ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
2774
2775         /* Check to see if NVME is configured or not */
2776         if (vport->phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
2777                 ae->un.AttrTypes[6] = 0x1; /* Type 0x28 - NVME */
2778
2779         size = FOURBYTES + 32;
2780         ad->AttrLen = cpu_to_be16(size);
2781         ad->AttrType = cpu_to_be16(RPRT_ACTIVE_FC4_TYPES);
2782         return size;
2783 }
2784
2785 static int
2786 lpfc_fdmi_port_attr_port_state(struct lpfc_vport *vport,
2787                                struct lpfc_fdmi_attr_def *ad)
2788 {
2789         struct lpfc_fdmi_attr_entry *ae;
2790         uint32_t size;
2791
2792         ae = &ad->AttrValue;
2793         /* Link Up - operational */
2794         ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTSTATE_ONLINE);
2795         size = FOURBYTES + sizeof(uint32_t);
2796         ad->AttrLen = cpu_to_be16(size);
2797         ad->AttrType = cpu_to_be16(RPRT_PORT_STATE);
2798         return size;
2799 }
2800
2801 static int
2802 lpfc_fdmi_port_attr_num_disc(struct lpfc_vport *vport,
2803                              struct lpfc_fdmi_attr_def *ad)
2804 {
2805         struct lpfc_fdmi_attr_entry *ae;
2806         uint32_t size;
2807
2808         ae = &ad->AttrValue;
2809         vport->fdmi_num_disc = lpfc_find_map_node(vport);
2810         ae->un.AttrInt = cpu_to_be32(vport->fdmi_num_disc);
2811         size = FOURBYTES + sizeof(uint32_t);
2812         ad->AttrLen = cpu_to_be16(size);
2813         ad->AttrType = cpu_to_be16(RPRT_DISC_PORT);
2814         return size;
2815 }
2816
2817 static int
2818 lpfc_fdmi_port_attr_nportid(struct lpfc_vport *vport,
2819                             struct lpfc_fdmi_attr_def *ad)
2820 {
2821         struct lpfc_fdmi_attr_entry *ae;
2822         uint32_t size;
2823
2824         ae = &ad->AttrValue;
2825         ae->un.AttrInt =  cpu_to_be32(vport->fc_myDID);
2826         size = FOURBYTES + sizeof(uint32_t);
2827         ad->AttrLen = cpu_to_be16(size);
2828         ad->AttrType = cpu_to_be16(RPRT_PORT_ID);
2829         return size;
2830 }
2831
2832 static int
2833 lpfc_fdmi_smart_attr_service(struct lpfc_vport *vport,
2834                              struct lpfc_fdmi_attr_def *ad)
2835 {
2836         struct lpfc_fdmi_attr_entry *ae;
2837         uint32_t len, size;
2838
2839         ae = &ad->AttrValue;
2840         memset(ae, 0, sizeof(*ae));
2841
2842         strncpy(ae->un.AttrString, "Smart SAN Initiator",
2843                 sizeof(ae->un.AttrString));
2844         len = strnlen(ae->un.AttrString,
2845                           sizeof(ae->un.AttrString));
2846         len += (len & 3) ? (4 - (len & 3)) : 4;
2847         size = FOURBYTES + len;
2848         ad->AttrLen = cpu_to_be16(size);
2849         ad->AttrType = cpu_to_be16(RPRT_SMART_SERVICE);
2850         return size;
2851 }
2852
2853 static int
2854 lpfc_fdmi_smart_attr_guid(struct lpfc_vport *vport,
2855                           struct lpfc_fdmi_attr_def *ad)
2856 {
2857         struct lpfc_fdmi_attr_entry *ae;
2858         uint32_t size;
2859
2860         ae = &ad->AttrValue;
2861         memset(ae, 0, sizeof(*ae));
2862
2863         memcpy(&ae->un.AttrString, &vport->fc_sparam.nodeName,
2864                sizeof(struct lpfc_name));
2865         memcpy((((uint8_t *)&ae->un.AttrString) +
2866                 sizeof(struct lpfc_name)),
2867                 &vport->fc_sparam.portName, sizeof(struct lpfc_name));
2868         size = FOURBYTES + (2 * sizeof(struct lpfc_name));
2869         ad->AttrLen =  cpu_to_be16(size);
2870         ad->AttrType = cpu_to_be16(RPRT_SMART_GUID);
2871         return size;
2872 }
2873
2874 static int
2875 lpfc_fdmi_smart_attr_version(struct lpfc_vport *vport,
2876                              struct lpfc_fdmi_attr_def *ad)
2877 {
2878         struct lpfc_fdmi_attr_entry *ae;
2879         uint32_t len, size;
2880
2881         ae = &ad->AttrValue;
2882         memset(ae, 0, sizeof(*ae));
2883
2884         strncpy(ae->un.AttrString, "Smart SAN Version 2.0",
2885                 sizeof(ae->un.AttrString));
2886         len = strnlen(ae->un.AttrString,
2887                           sizeof(ae->un.AttrString));
2888         len += (len & 3) ? (4 - (len & 3)) : 4;
2889         size = FOURBYTES + len;
2890         ad->AttrLen =  cpu_to_be16(size);
2891         ad->AttrType = cpu_to_be16(RPRT_SMART_VERSION);
2892         return size;
2893 }
2894
2895 static int
2896 lpfc_fdmi_smart_attr_model(struct lpfc_vport *vport,
2897                            struct lpfc_fdmi_attr_def *ad)
2898 {
2899         struct lpfc_hba *phba = vport->phba;
2900         struct lpfc_fdmi_attr_entry *ae;
2901         uint32_t len, size;
2902
2903         ae = &ad->AttrValue;
2904         memset(ae, 0, sizeof(*ae));
2905
2906         strncpy(ae->un.AttrString, phba->ModelName,
2907                 sizeof(ae->un.AttrString));
2908         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2909         len += (len & 3) ? (4 - (len & 3)) : 4;
2910         size = FOURBYTES + len;
2911         ad->AttrLen = cpu_to_be16(size);
2912         ad->AttrType = cpu_to_be16(RPRT_SMART_MODEL);
2913         return size;
2914 }
2915
2916 static int
2917 lpfc_fdmi_smart_attr_port_info(struct lpfc_vport *vport,
2918                                struct lpfc_fdmi_attr_def *ad)
2919 {
2920         struct lpfc_fdmi_attr_entry *ae;
2921         uint32_t size;
2922
2923         ae = &ad->AttrValue;
2924
2925         /* SRIOV (type 3) is not supported */
2926         if (vport->vpi)
2927                 ae->un.AttrInt =  cpu_to_be32(2);  /* NPIV */
2928         else
2929                 ae->un.AttrInt =  cpu_to_be32(1);  /* Physical */
2930         size = FOURBYTES + sizeof(uint32_t);
2931         ad->AttrLen = cpu_to_be16(size);
2932         ad->AttrType = cpu_to_be16(RPRT_SMART_PORT_INFO);
2933         return size;
2934 }
2935
2936 static int
2937 lpfc_fdmi_smart_attr_qos(struct lpfc_vport *vport,
2938                          struct lpfc_fdmi_attr_def *ad)
2939 {
2940         struct lpfc_fdmi_attr_entry *ae;
2941         uint32_t size;
2942
2943         ae = &ad->AttrValue;
2944         ae->un.AttrInt =  cpu_to_be32(0);
2945         size = FOURBYTES + sizeof(uint32_t);
2946         ad->AttrLen = cpu_to_be16(size);
2947         ad->AttrType = cpu_to_be16(RPRT_SMART_QOS);
2948         return size;
2949 }
2950
2951 static int
2952 lpfc_fdmi_smart_attr_security(struct lpfc_vport *vport,
2953                               struct lpfc_fdmi_attr_def *ad)
2954 {
2955         struct lpfc_fdmi_attr_entry *ae;
2956         uint32_t size;
2957
2958         ae = &ad->AttrValue;
2959         ae->un.AttrInt =  cpu_to_be32(1);
2960         size = FOURBYTES + sizeof(uint32_t);
2961         ad->AttrLen = cpu_to_be16(size);
2962         ad->AttrType = cpu_to_be16(RPRT_SMART_SECURITY);
2963         return size;
2964 }
2965
2966 /* RHBA attribute jump table */
2967 int (*lpfc_fdmi_hba_action[])
2968         (struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
2969         /* Action routine                 Mask bit     Attribute type */
2970         lpfc_fdmi_hba_attr_wwnn,          /* bit0     RHBA_NODENAME           */
2971         lpfc_fdmi_hba_attr_manufacturer,  /* bit1     RHBA_MANUFACTURER       */
2972         lpfc_fdmi_hba_attr_sn,            /* bit2     RHBA_SERIAL_NUMBER      */
2973         lpfc_fdmi_hba_attr_model,         /* bit3     RHBA_MODEL              */
2974         lpfc_fdmi_hba_attr_description,   /* bit4     RHBA_MODEL_DESCRIPTION  */
2975         lpfc_fdmi_hba_attr_hdw_ver,       /* bit5     RHBA_HARDWARE_VERSION   */
2976         lpfc_fdmi_hba_attr_drvr_ver,      /* bit6     RHBA_DRIVER_VERSION     */
2977         lpfc_fdmi_hba_attr_rom_ver,       /* bit7     RHBA_OPTION_ROM_VERSION */
2978         lpfc_fdmi_hba_attr_fmw_ver,       /* bit8     RHBA_FIRMWARE_VERSION   */
2979         lpfc_fdmi_hba_attr_os_ver,        /* bit9     RHBA_OS_NAME_VERSION    */
2980         lpfc_fdmi_hba_attr_ct_len,        /* bit10    RHBA_MAX_CT_PAYLOAD_LEN */
2981         lpfc_fdmi_hba_attr_symbolic_name, /* bit11    RHBA_SYM_NODENAME       */
2982         lpfc_fdmi_hba_attr_vendor_info,   /* bit12    RHBA_VENDOR_INFO        */
2983         lpfc_fdmi_hba_attr_num_ports,     /* bit13    RHBA_NUM_PORTS          */
2984         lpfc_fdmi_hba_attr_fabric_wwnn,   /* bit14    RHBA_FABRIC_WWNN        */
2985         lpfc_fdmi_hba_attr_bios_ver,      /* bit15    RHBA_BIOS_VERSION       */
2986         lpfc_fdmi_hba_attr_bios_state,    /* bit16    RHBA_BIOS_STATE         */
2987         lpfc_fdmi_hba_attr_vendor_id,     /* bit17    RHBA_VENDOR_ID          */
2988 };
2989
2990 /* RPA / RPRT attribute jump table */
2991 int (*lpfc_fdmi_port_action[])
2992         (struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
2993         /* Action routine                   Mask bit   Attribute type */
2994         lpfc_fdmi_port_attr_fc4type,        /* bit0   RPRT_SUPPORT_FC4_TYPES  */
2995         lpfc_fdmi_port_attr_support_speed,  /* bit1   RPRT_SUPPORTED_SPEED    */
2996         lpfc_fdmi_port_attr_speed,          /* bit2   RPRT_PORT_SPEED         */
2997         lpfc_fdmi_port_attr_max_frame,      /* bit3   RPRT_MAX_FRAME_SIZE     */
2998         lpfc_fdmi_port_attr_os_devname,     /* bit4   RPRT_OS_DEVICE_NAME     */
2999         lpfc_fdmi_port_attr_host_name,      /* bit5   RPRT_HOST_NAME          */
3000         lpfc_fdmi_port_attr_wwnn,           /* bit6   RPRT_NODENAME           */
3001         lpfc_fdmi_port_attr_wwpn,           /* bit7   RPRT_PORTNAME           */
3002         lpfc_fdmi_port_attr_symbolic_name,  /* bit8   RPRT_SYM_PORTNAME       */
3003         lpfc_fdmi_port_attr_port_type,      /* bit9   RPRT_PORT_TYPE          */
3004         lpfc_fdmi_port_attr_class,          /* bit10  RPRT_SUPPORTED_CLASS    */
3005         lpfc_fdmi_port_attr_fabric_wwpn,    /* bit11  RPRT_FABRICNAME         */
3006         lpfc_fdmi_port_attr_active_fc4type, /* bit12  RPRT_ACTIVE_FC4_TYPES   */
3007         lpfc_fdmi_port_attr_port_state,     /* bit13  RPRT_PORT_STATE         */
3008         lpfc_fdmi_port_attr_num_disc,       /* bit14  RPRT_DISC_PORT          */
3009         lpfc_fdmi_port_attr_nportid,        /* bit15  RPRT_PORT_ID            */
3010         lpfc_fdmi_smart_attr_service,       /* bit16  RPRT_SMART_SERVICE      */
3011         lpfc_fdmi_smart_attr_guid,          /* bit17  RPRT_SMART_GUID         */
3012         lpfc_fdmi_smart_attr_version,       /* bit18  RPRT_SMART_VERSION      */
3013         lpfc_fdmi_smart_attr_model,         /* bit19  RPRT_SMART_MODEL        */
3014         lpfc_fdmi_smart_attr_port_info,     /* bit20  RPRT_SMART_PORT_INFO    */
3015         lpfc_fdmi_smart_attr_qos,           /* bit21  RPRT_SMART_QOS          */
3016         lpfc_fdmi_smart_attr_security,      /* bit22  RPRT_SMART_SECURITY     */
3017 };
3018
3019 /**
3020  * lpfc_fdmi_cmd - Build and send a FDMI cmd to the specified NPort
3021  * @vport: pointer to a host virtual N_Port data structure.
3022  * @ndlp: ndlp to send FDMI cmd to (if NULL use FDMI_DID)
3023  * cmdcode: FDMI command to send
3024  * mask: Mask of HBA or PORT Attributes to send
3025  *
3026  * Builds and sends a FDMI command using the CT subsystem.
3027  */
3028 int
3029 lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3030               int cmdcode, uint32_t new_mask)
3031 {
3032         struct lpfc_hba *phba = vport->phba;
3033         struct lpfc_dmabuf *mp, *bmp;
3034         struct lpfc_sli_ct_request *CtReq;
3035         struct ulp_bde64 *bpl;
3036         uint32_t bit_pos;
3037         uint32_t size;
3038         uint32_t rsp_size;
3039         uint32_t mask;
3040         struct lpfc_fdmi_reg_hba *rh;
3041         struct lpfc_fdmi_port_entry *pe;
3042         struct lpfc_fdmi_reg_portattr *pab = NULL;
3043         struct lpfc_fdmi_attr_block *ab = NULL;
3044         int  (*func)(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad);
3045         void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
3046                      struct lpfc_iocbq *);
3047
3048         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
3049                 return 0;
3050
3051         cmpl = lpfc_cmpl_ct_disc_fdmi; /* called from discovery */
3052
3053         /* fill in BDEs for command */
3054         /* Allocate buffer for command payload */
3055         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3056         if (!mp)
3057                 goto fdmi_cmd_exit;
3058
3059         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
3060         if (!mp->virt)
3061                 goto fdmi_cmd_free_mp;
3062
3063         /* Allocate buffer for Buffer ptr list */
3064         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3065         if (!bmp)
3066                 goto fdmi_cmd_free_mpvirt;
3067
3068         bmp->virt = lpfc_mbuf_alloc(phba, 0, &(bmp->phys));
3069         if (!bmp->virt)
3070                 goto fdmi_cmd_free_bmp;
3071
3072         INIT_LIST_HEAD(&mp->list);
3073         INIT_LIST_HEAD(&bmp->list);
3074
3075         /* FDMI request */
3076         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3077                          "0218 FDMI Request Data: x%x x%x x%x\n",
3078                          vport->fc_flag, vport->port_state, cmdcode);
3079         CtReq = (struct lpfc_sli_ct_request *)mp->virt;
3080
3081         /* First populate the CT_IU preamble */
3082         memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
3083         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
3084         CtReq->RevisionId.bits.InId = 0;
3085
3086         CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
3087         CtReq->FsSubType = SLI_CT_FDMI_Subtypes;
3088
3089         CtReq->CommandResponse.bits.CmdRsp = cpu_to_be16(cmdcode);
3090         rsp_size = LPFC_BPL_SIZE;
3091         size = 0;
3092
3093         /* Next fill in the specific FDMI cmd information */
3094         switch (cmdcode) {
3095         case SLI_MGMT_RHAT:
3096         case SLI_MGMT_RHBA:
3097                 rh = (struct lpfc_fdmi_reg_hba *)&CtReq->un.PortID;
3098                 /* HBA Identifier */
3099                 memcpy(&rh->hi.PortName, &phba->pport->fc_sparam.portName,
3100                        sizeof(struct lpfc_name));
3101
3102                 if (cmdcode == SLI_MGMT_RHBA) {
3103                         /* Registered Port List */
3104                         /* One entry (port) per adapter */
3105                         rh->rpl.EntryCnt = cpu_to_be32(1);
3106                         memcpy(&rh->rpl.pe.PortName,
3107                                &phba->pport->fc_sparam.portName,
3108                                sizeof(struct lpfc_name));
3109
3110                         /* point to the HBA attribute block */
3111                         size = 2 * sizeof(struct lpfc_name) +
3112                                 FOURBYTES;
3113                 } else {
3114                         size = sizeof(struct lpfc_name);
3115                 }
3116                 ab = (struct lpfc_fdmi_attr_block *)((uint8_t *)rh + size);
3117                 ab->EntryCnt = 0;
3118                 size += FOURBYTES;
3119                 bit_pos = 0;
3120                 if (new_mask)
3121                         mask = new_mask;
3122                 else
3123                         mask = vport->fdmi_hba_mask;
3124
3125                 /* Mask will dictate what attributes to build in the request */
3126                 while (mask) {
3127                         if (mask & 0x1) {
3128                                 func = lpfc_fdmi_hba_action[bit_pos];
3129                                 size += func(vport,
3130                                              (struct lpfc_fdmi_attr_def *)
3131                                              ((uint8_t *)rh + size));
3132                                 ab->EntryCnt++;
3133                                 if ((size + 256) >
3134                                     (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3135                                         goto hba_out;
3136                         }
3137                         mask = mask >> 1;
3138                         bit_pos++;
3139                 }
3140 hba_out:
3141                 ab->EntryCnt = cpu_to_be32(ab->EntryCnt);
3142                 /* Total size */
3143                 size = GID_REQUEST_SZ - 4 + size;
3144                 break;
3145
3146         case SLI_MGMT_RPRT:
3147         case SLI_MGMT_RPA:
3148                 pab = (struct lpfc_fdmi_reg_portattr *)&CtReq->un.PortID;
3149                 if (cmdcode == SLI_MGMT_RPRT) {
3150                         rh = (struct lpfc_fdmi_reg_hba *)pab;
3151                         /* HBA Identifier */
3152                         memcpy(&rh->hi.PortName,
3153                                &phba->pport->fc_sparam.portName,
3154                                sizeof(struct lpfc_name));
3155                         pab = (struct lpfc_fdmi_reg_portattr *)
3156                                 ((uint8_t *)pab +  sizeof(struct lpfc_name));
3157                 }
3158
3159                 memcpy((uint8_t *)&pab->PortName,
3160                        (uint8_t *)&vport->fc_sparam.portName,
3161                        sizeof(struct lpfc_name));
3162                 size += sizeof(struct lpfc_name) + FOURBYTES;
3163                 pab->ab.EntryCnt = 0;
3164                 bit_pos = 0;
3165                 if (new_mask)
3166                         mask = new_mask;
3167                 else
3168                         mask = vport->fdmi_port_mask;
3169
3170                 /* Mask will dictate what attributes to build in the request */
3171                 while (mask) {
3172                         if (mask & 0x1) {
3173                                 func = lpfc_fdmi_port_action[bit_pos];
3174                                 size += func(vport,
3175                                              (struct lpfc_fdmi_attr_def *)
3176                                              ((uint8_t *)pab + size));
3177                                 pab->ab.EntryCnt++;
3178                                 if ((size + 256) >
3179                                     (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3180                                         goto port_out;
3181                         }
3182                         mask = mask >> 1;
3183                         bit_pos++;
3184                 }
3185 port_out:
3186                 pab->ab.EntryCnt = cpu_to_be32(pab->ab.EntryCnt);
3187                 /* Total size */
3188                 if (cmdcode == SLI_MGMT_RPRT)
3189                         size += sizeof(struct lpfc_name);
3190                 size = GID_REQUEST_SZ - 4 + size;
3191                 break;
3192
3193         case SLI_MGMT_GHAT:
3194         case SLI_MGMT_GRPL:
3195                 rsp_size = FC_MAX_NS_RSP;
3196                 /* fall through */
3197         case SLI_MGMT_DHBA:
3198         case SLI_MGMT_DHAT:
3199                 pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3200                 memcpy((uint8_t *)&pe->PortName,
3201                        (uint8_t *)&vport->fc_sparam.portName,
3202                        sizeof(struct lpfc_name));
3203                 size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3204                 break;
3205
3206         case SLI_MGMT_GPAT:
3207         case SLI_MGMT_GPAS:
3208                 rsp_size = FC_MAX_NS_RSP;
3209                 /* fall through */
3210         case SLI_MGMT_DPRT:
3211         case SLI_MGMT_DPA:
3212                 pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3213                 memcpy((uint8_t *)&pe->PortName,
3214                        (uint8_t *)&vport->fc_sparam.portName,
3215                        sizeof(struct lpfc_name));
3216                 size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3217                 break;
3218         case SLI_MGMT_GRHL:
3219                 size = GID_REQUEST_SZ - 4;
3220                 break;
3221         default:
3222                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3223                                  "0298 FDMI cmdcode x%x not supported\n",
3224                                  cmdcode);
3225                 goto fdmi_cmd_free_bmpvirt;
3226         }
3227         CtReq->CommandResponse.bits.Size = cpu_to_be16(rsp_size);
3228
3229         bpl = (struct ulp_bde64 *)bmp->virt;
3230         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
3231         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
3232         bpl->tus.f.bdeFlags = 0;
3233         bpl->tus.f.bdeSize = size;
3234
3235         /*
3236          * The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
3237          * to hold ndlp reference for the corresponding callback function.
3238          */
3239         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, 0))
3240                 return 0;
3241
3242         /*
3243          * Decrement ndlp reference count to release ndlp reference held
3244          * for the failed command's callback function.
3245          */
3246         lpfc_nlp_put(ndlp);
3247
3248 fdmi_cmd_free_bmpvirt:
3249         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3250 fdmi_cmd_free_bmp:
3251         kfree(bmp);
3252 fdmi_cmd_free_mpvirt:
3253         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3254 fdmi_cmd_free_mp:
3255         kfree(mp);
3256 fdmi_cmd_exit:
3257         /* Issue FDMI request failed */
3258         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3259                          "0244 Issue FDMI request failed Data: x%x\n",
3260                          cmdcode);
3261         return 1;
3262 }
3263
3264 /**
3265  * lpfc_delayed_disc_tmo - Timeout handler for delayed discovery timer.
3266  * @ptr - Context object of the timer.
3267  *
3268  * This function set the WORKER_DELAYED_DISC_TMO flag and wake up
3269  * the worker thread.
3270  **/
3271 void
3272 lpfc_delayed_disc_tmo(struct timer_list *t)
3273 {
3274         struct lpfc_vport *vport = from_timer(vport, t, delayed_disc_tmo);
3275         struct lpfc_hba   *phba = vport->phba;
3276         uint32_t tmo_posted;
3277         unsigned long iflag;
3278
3279         spin_lock_irqsave(&vport->work_port_lock, iflag);
3280         tmo_posted = vport->work_port_events & WORKER_DELAYED_DISC_TMO;
3281         if (!tmo_posted)
3282                 vport->work_port_events |= WORKER_DELAYED_DISC_TMO;
3283         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3284
3285         if (!tmo_posted)
3286                 lpfc_worker_wake_up(phba);
3287         return;
3288 }
3289
3290 /**
3291  * lpfc_delayed_disc_timeout_handler - Function called by worker thread to
3292  *      handle delayed discovery.
3293  * @vport: pointer to a host virtual N_Port data structure.
3294  *
3295  * This function start nport discovery of the vport.
3296  **/
3297 void
3298 lpfc_delayed_disc_timeout_handler(struct lpfc_vport *vport)
3299 {
3300         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3301
3302         spin_lock_irq(shost->host_lock);
3303         if (!(vport->fc_flag & FC_DISC_DELAYED)) {
3304                 spin_unlock_irq(shost->host_lock);
3305                 return;
3306         }
3307         vport->fc_flag &= ~FC_DISC_DELAYED;
3308         spin_unlock_irq(shost->host_lock);
3309
3310         lpfc_do_scr_ns_plogi(vport->phba, vport);
3311 }
3312
3313 void
3314 lpfc_decode_firmware_rev(struct lpfc_hba *phba, char *fwrevision, int flag)
3315 {
3316         struct lpfc_sli *psli = &phba->sli;
3317         lpfc_vpd_t *vp = &phba->vpd;
3318         uint32_t b1, b2, b3, b4, i, rev;
3319         char c;
3320         uint32_t *ptr, str[4];
3321         uint8_t *fwname;
3322
3323         if (phba->sli_rev == LPFC_SLI_REV4)
3324                 snprintf(fwrevision, FW_REV_STR_SIZE, "%s", vp->rev.opFwName);
3325         else if (vp->rev.rBit) {
3326                 if (psli->sli_flag & LPFC_SLI_ACTIVE)
3327                         rev = vp->rev.sli2FwRev;
3328                 else
3329                         rev = vp->rev.sli1FwRev;
3330
3331                 b1 = (rev & 0x0000f000) >> 12;
3332                 b2 = (rev & 0x00000f00) >> 8;
3333                 b3 = (rev & 0x000000c0) >> 6;
3334                 b4 = (rev & 0x00000030) >> 4;
3335
3336                 switch (b4) {
3337                 case 0:
3338                         c = 'N';
3339                         break;
3340                 case 1:
3341                         c = 'A';
3342                         break;
3343                 case 2:
3344                         c = 'B';
3345                         break;
3346                 case 3:
3347                         c = 'X';
3348                         break;
3349                 default:
3350                         c = 0;
3351                         break;
3352                 }
3353                 b4 = (rev & 0x0000000f);
3354
3355                 if (psli->sli_flag & LPFC_SLI_ACTIVE)
3356                         fwname = vp->rev.sli2FwName;
3357                 else
3358                         fwname = vp->rev.sli1FwName;
3359
3360                 for (i = 0; i < 16; i++)
3361                         if (fwname[i] == 0x20)
3362                                 fwname[i] = 0;
3363
3364                 ptr = (uint32_t*)fwname;
3365
3366                 for (i = 0; i < 3; i++)
3367                         str[i] = be32_to_cpu(*ptr++);
3368
3369                 if (c == 0) {
3370                         if (flag)
3371                                 sprintf(fwrevision, "%d.%d%d (%s)",
3372                                         b1, b2, b3, (char *)str);
3373                         else
3374                                 sprintf(fwrevision, "%d.%d%d", b1,
3375                                         b2, b3);
3376                 } else {
3377                         if (flag)
3378                                 sprintf(fwrevision, "%d.%d%d%c%d (%s)",
3379                                         b1, b2, b3, c,
3380                                         b4, (char *)str);
3381                         else
3382                                 sprintf(fwrevision, "%d.%d%d%c%d",
3383                                         b1, b2, b3, c, b4);
3384                 }
3385         } else {
3386                 rev = vp->rev.smFwRev;
3387
3388                 b1 = (rev & 0xff000000) >> 24;
3389                 b2 = (rev & 0x00f00000) >> 20;
3390                 b3 = (rev & 0x000f0000) >> 16;
3391                 c  = (rev & 0x0000ff00) >> 8;
3392                 b4 = (rev & 0x000000ff);
3393
3394                 sprintf(fwrevision, "%d.%d%d%c%d", b1, b2, b3, c, b4);
3395         }
3396         return;
3397 }