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