Merge tag 'x86-apic-2021-11-01' 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-2021 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_logmsg.h"
48 #include "lpfc_crtn.h"
49 #include "lpfc_version.h"
50 #include "lpfc_vport.h"
51 #include "lpfc_debugfs.h"
52
53 /* FDMI Port Speed definitions - FC-GS-7 */
54 #define HBA_PORTSPEED_1GFC              0x00000001      /* 1G FC */
55 #define HBA_PORTSPEED_2GFC              0x00000002      /* 2G FC */
56 #define HBA_PORTSPEED_4GFC              0x00000008      /* 4G FC */
57 #define HBA_PORTSPEED_10GFC             0x00000004      /* 10G FC */
58 #define HBA_PORTSPEED_8GFC              0x00000010      /* 8G FC */
59 #define HBA_PORTSPEED_16GFC             0x00000020      /* 16G FC */
60 #define HBA_PORTSPEED_32GFC             0x00000040      /* 32G FC */
61 #define HBA_PORTSPEED_20GFC             0x00000080      /* 20G FC */
62 #define HBA_PORTSPEED_40GFC             0x00000100      /* 40G FC */
63 #define HBA_PORTSPEED_128GFC            0x00000200      /* 128G FC */
64 #define HBA_PORTSPEED_64GFC             0x00000400      /* 64G FC */
65 #define HBA_PORTSPEED_256GFC            0x00000800      /* 256G FC */
66 #define HBA_PORTSPEED_UNKNOWN           0x00008000      /* Unknown */
67 #define HBA_PORTSPEED_10GE              0x00010000      /* 10G E */
68 #define HBA_PORTSPEED_40GE              0x00020000      /* 40G E */
69 #define HBA_PORTSPEED_100GE             0x00040000      /* 100G E */
70 #define HBA_PORTSPEED_25GE              0x00080000      /* 25G E */
71 #define HBA_PORTSPEED_50GE              0x00100000      /* 50G E */
72 #define HBA_PORTSPEED_400GE             0x00200000      /* 400G E */
73
74 #define FOURBYTES       4
75
76
77 static char *lpfc_release_version = LPFC_DRIVER_VERSION;
78 static void
79 lpfc_cmpl_ct_cmd_vmid(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
80                       struct lpfc_iocbq *rspiocb);
81
82 static void
83 lpfc_ct_ignore_hbq_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
84                           struct lpfc_dmabuf *mp, uint32_t size)
85 {
86         if (!mp) {
87                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
88                                 "0146 Ignoring unsolicited CT No HBQ "
89                                 "status = x%x\n",
90                                 piocbq->iocb.ulpStatus);
91         }
92         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
93                         "0145 Ignoring unsolicted CT HBQ Size:%d "
94                         "status = x%x\n",
95                         size, piocbq->iocb.ulpStatus);
96 }
97
98 static void
99 lpfc_ct_unsol_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
100                      struct lpfc_dmabuf *mp, uint32_t size)
101 {
102         lpfc_ct_ignore_hbq_buffer(phba, piocbq, mp, size);
103 }
104
105 /**
106  * lpfc_ct_unsol_cmpl : Completion callback function for unsol ct commands
107  * @phba : pointer to lpfc hba data structure.
108  * @cmdiocb : pointer to lpfc command iocb data structure.
109  * @rspiocb : pointer to lpfc response iocb data structure.
110  *
111  * This routine is the callback function for issuing unsol ct reject command.
112  * The memory allocated in the reject command path is freed up here.
113  **/
114 static void
115 lpfc_ct_unsol_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
116                    struct lpfc_iocbq *rspiocb)
117 {
118         struct lpfc_nodelist *ndlp;
119         struct lpfc_dmabuf *mp, *bmp;
120
121         ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
122         if (ndlp)
123                 lpfc_nlp_put(ndlp);
124
125         mp = cmdiocb->context2;
126         bmp = cmdiocb->context3;
127         if (mp) {
128                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
129                 kfree(mp);
130                 cmdiocb->context2 = NULL;
131         }
132
133         if (bmp) {
134                 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
135                 kfree(bmp);
136                 cmdiocb->context3 = NULL;
137         }
138
139         lpfc_sli_release_iocbq(phba, cmdiocb);
140 }
141
142 /**
143  * lpfc_ct_reject_event - Issue reject for unhandled CT MIB commands
144  * @ndlp: pointer to a node-list data structure.
145  * @ct_req: pointer to the CT request data structure.
146  * @rx_id: rx_id of the received UNSOL CT command
147  * @ox_id: ox_id of the UNSOL CT command
148  *
149  * This routine is invoked by the lpfc_ct_handle_mibreq routine for sending
150  * a reject response. Reject response is sent for the unhandled commands.
151  **/
152 static void
153 lpfc_ct_reject_event(struct lpfc_nodelist *ndlp,
154                      struct lpfc_sli_ct_request *ct_req,
155                      u16 rx_id, u16 ox_id)
156 {
157         struct lpfc_vport *vport = ndlp->vport;
158         struct lpfc_hba *phba = vport->phba;
159         struct lpfc_sli_ct_request *ct_rsp;
160         struct lpfc_iocbq *cmdiocbq = NULL;
161         struct lpfc_dmabuf *bmp = NULL;
162         struct lpfc_dmabuf *mp = NULL;
163         struct ulp_bde64 *bpl;
164         IOCB_t *icmd;
165         u8 rc = 0;
166
167         /* fill in BDEs for command */
168         mp = kmalloc(sizeof(*mp), GFP_KERNEL);
169         if (!mp) {
170                 rc = 1;
171                 goto ct_exit;
172         }
173
174         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp->phys);
175         if (!mp->virt) {
176                 rc = 2;
177                 goto ct_free_mp;
178         }
179
180         /* Allocate buffer for Buffer ptr list */
181         bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
182         if (!bmp) {
183                 rc = 3;
184                 goto ct_free_mpvirt;
185         }
186
187         bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &bmp->phys);
188         if (!bmp->virt) {
189                 rc = 4;
190                 goto ct_free_bmp;
191         }
192
193         INIT_LIST_HEAD(&mp->list);
194         INIT_LIST_HEAD(&bmp->list);
195
196         bpl = (struct ulp_bde64 *)bmp->virt;
197         memset(bpl, 0, sizeof(struct ulp_bde64));
198         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
199         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
200         bpl->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
201         bpl->tus.f.bdeSize = (LPFC_CT_PREAMBLE - 4);
202         bpl->tus.w = le32_to_cpu(bpl->tus.w);
203
204         ct_rsp = (struct lpfc_sli_ct_request *)mp->virt;
205         memset(ct_rsp, 0, sizeof(struct lpfc_sli_ct_request));
206
207         ct_rsp->RevisionId.bits.Revision = SLI_CT_REVISION;
208         ct_rsp->RevisionId.bits.InId = 0;
209         ct_rsp->FsType = ct_req->FsType;
210         ct_rsp->FsSubType = ct_req->FsSubType;
211         ct_rsp->CommandResponse.bits.Size = 0;
212         ct_rsp->CommandResponse.bits.CmdRsp =
213                 cpu_to_be16(SLI_CT_RESPONSE_FS_RJT);
214         ct_rsp->ReasonCode = SLI_CT_REQ_NOT_SUPPORTED;
215         ct_rsp->Explanation = SLI_CT_NO_ADDITIONAL_EXPL;
216
217         cmdiocbq = lpfc_sli_get_iocbq(phba);
218         if (!cmdiocbq) {
219                 rc = 5;
220                 goto ct_free_bmpvirt;
221         }
222
223         icmd = &cmdiocbq->iocb;
224         icmd->un.genreq64.bdl.ulpIoTag32 = 0;
225         icmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
226         icmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
227         icmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
228         icmd->un.genreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
229         icmd->un.genreq64.w5.hcsw.Fctl = (LS | LA);
230         icmd->un.genreq64.w5.hcsw.Dfctl = 0;
231         icmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
232         icmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
233         icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
234         icmd->ulpBdeCount = 1;
235         icmd->ulpLe = 1;
236         icmd->ulpClass = CLASS3;
237
238         /* Save for completion so we can release these resources */
239         cmdiocbq->context1 = lpfc_nlp_get(ndlp);
240         cmdiocbq->context2 = (uint8_t *)mp;
241         cmdiocbq->context3 = (uint8_t *)bmp;
242         cmdiocbq->iocb_cmpl = lpfc_ct_unsol_cmpl;
243         icmd->ulpContext = rx_id;  /* Xri / rx_id */
244         icmd->unsli3.rcvsli3.ox_id = ox_id;
245         icmd->un.ulpWord[3] =
246                 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
247         icmd->ulpTimeout = (3 * phba->fc_ratov);
248
249         cmdiocbq->retry = 0;
250         cmdiocbq->vport = vport;
251         cmdiocbq->context_un.ndlp = NULL;
252         cmdiocbq->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
253
254         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
255         if (!rc)
256                 return;
257
258         rc = 6;
259         lpfc_nlp_put(ndlp);
260         lpfc_sli_release_iocbq(phba, cmdiocbq);
261 ct_free_bmpvirt:
262         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
263 ct_free_bmp:
264         kfree(bmp);
265 ct_free_mpvirt:
266         lpfc_mbuf_free(phba, mp->virt, mp->phys);
267 ct_free_mp:
268         kfree(mp);
269 ct_exit:
270         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
271                          "6440 Unsol CT: Rsp err %d Data: x%x\n",
272                          rc, vport->fc_flag);
273 }
274
275 /**
276  * lpfc_ct_handle_mibreq - Process an unsolicited CT MIB request data buffer
277  * @phba: pointer to lpfc hba data structure.
278  * @ctiocbq: pointer to lpfc CT command iocb data structure.
279  *
280  * This routine is used for processing the IOCB associated with a unsolicited
281  * CT MIB request. It first determines whether there is an existing ndlp that
282  * matches the DID from the unsolicited IOCB. If not, it will return.
283  **/
284 static void
285 lpfc_ct_handle_mibreq(struct lpfc_hba *phba, struct lpfc_iocbq *ctiocbq)
286 {
287         struct lpfc_sli_ct_request *ct_req;
288         struct lpfc_nodelist *ndlp = NULL;
289         struct lpfc_vport *vport = NULL;
290         IOCB_t *icmd = &ctiocbq->iocb;
291         u32 mi_cmd, vpi;
292         u32 did = 0;
293
294         vpi = ctiocbq->iocb.unsli3.rcvsli3.vpi;
295         vport = lpfc_find_vport_by_vpid(phba, vpi);
296         if (!vport) {
297                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
298                                 "6437 Unsol CT: VPORT NULL vpi : x%x\n",
299                                 vpi);
300                 return;
301         }
302
303         did = ctiocbq->iocb.un.rcvels.remoteID;
304         if (icmd->ulpStatus) {
305                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
306                                  "6438 Unsol CT: status:x%x/x%x did : x%x\n",
307                                  icmd->ulpStatus, icmd->un.ulpWord[4], did);
308                 return;
309         }
310
311         /* Ignore traffic received during vport shutdown */
312         if (vport->fc_flag & FC_UNLOADING)
313                 return;
314
315         ndlp = lpfc_findnode_did(vport, did);
316         if (!ndlp) {
317                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
318                                  "6439 Unsol CT: NDLP Not Found for DID : x%x",
319                                  did);
320                 return;
321         }
322
323         ct_req = ((struct lpfc_sli_ct_request *)
324                  (((struct lpfc_dmabuf *)ctiocbq->context2)->virt));
325
326         mi_cmd = ct_req->CommandResponse.bits.CmdRsp;
327         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
328                          "6442 : MI Cmd : x%x Not Supported\n", mi_cmd);
329         lpfc_ct_reject_event(ndlp, ct_req,
330                              ctiocbq->iocb.ulpContext,
331                              ctiocbq->iocb.unsli3.rcvsli3.ox_id);
332 }
333
334 /**
335  * lpfc_ct_unsol_event - Process an unsolicited event from a ct sli ring
336  * @phba: pointer to lpfc hba data structure.
337  * @pring: pointer to a SLI ring.
338  * @ctiocbq: pointer to lpfc ct iocb data structure.
339  *
340  * This routine is used to process an unsolicited event received from a SLI
341  * (Service Level Interface) ring. The actual processing of the data buffer
342  * associated with the unsolicited event is done by invoking appropriate routine
343  * after properly set up the iocb buffer from the SLI ring on which the
344  * unsolicited event was received.
345  **/
346 void
347 lpfc_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
348                     struct lpfc_iocbq *ctiocbq)
349 {
350         struct lpfc_dmabuf *mp = NULL;
351         IOCB_t *icmd = &ctiocbq->iocb;
352         int i;
353         struct lpfc_iocbq *iocbq;
354         dma_addr_t dma_addr;
355         uint32_t size;
356         struct list_head head;
357         struct lpfc_sli_ct_request *ct_req;
358         struct lpfc_dmabuf *bdeBuf1 = ctiocbq->context2;
359         struct lpfc_dmabuf *bdeBuf2 = ctiocbq->context3;
360
361         ctiocbq->context1 = NULL;
362         ctiocbq->context2 = NULL;
363         ctiocbq->context3 = NULL;
364
365         if (unlikely(icmd->ulpStatus == IOSTAT_NEED_BUFFER)) {
366                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
367         } else if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
368                    ((icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
369                    IOERR_RCV_BUFFER_WAITING)) {
370                 /* Not enough posted buffers; Try posting more buffers */
371                 phba->fc_stat.NoRcvBuf++;
372                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
373                         lpfc_post_buffer(phba, pring, 2);
374                 return;
375         }
376
377         /* If there are no BDEs associated
378          * with this IOCB, there is nothing to do.
379          */
380         if (icmd->ulpBdeCount == 0)
381                 return;
382
383         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
384                 ctiocbq->context2 = bdeBuf1;
385                 if (icmd->ulpBdeCount == 2)
386                         ctiocbq->context3 = bdeBuf2;
387         } else {
388                 dma_addr = getPaddr(icmd->un.cont64[0].addrHigh,
389                                     icmd->un.cont64[0].addrLow);
390                 ctiocbq->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
391                                                              dma_addr);
392                 if (icmd->ulpBdeCount == 2) {
393                         dma_addr = getPaddr(icmd->un.cont64[1].addrHigh,
394                                             icmd->un.cont64[1].addrLow);
395                         ctiocbq->context3 = lpfc_sli_ringpostbuf_get(phba,
396                                                                      pring,
397                                                                      dma_addr);
398                 }
399         }
400
401         ct_req = ((struct lpfc_sli_ct_request *)
402                  (((struct lpfc_dmabuf *)ctiocbq->context2)->virt));
403
404         if (ct_req->FsType == SLI_CT_MANAGEMENT_SERVICE &&
405             ct_req->FsSubType == SLI_CT_MIB_Subtypes) {
406                 lpfc_ct_handle_mibreq(phba, ctiocbq);
407         } else {
408                 if (!lpfc_bsg_ct_unsol_event(phba, pring, ctiocbq))
409                         return;
410         }
411
412         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
413                 INIT_LIST_HEAD(&head);
414                 list_add_tail(&head, &ctiocbq->list);
415                 list_for_each_entry(iocbq, &head, list) {
416                         icmd = &iocbq->iocb;
417                         if (icmd->ulpBdeCount == 0)
418                                 continue;
419                         bdeBuf1 = iocbq->context2;
420                         iocbq->context2 = NULL;
421                         size  = icmd->un.cont64[0].tus.f.bdeSize;
422                         lpfc_ct_unsol_buffer(phba, ctiocbq, bdeBuf1, size);
423                         lpfc_in_buf_free(phba, bdeBuf1);
424                         if (icmd->ulpBdeCount == 2) {
425                                 bdeBuf2 = iocbq->context3;
426                                 iocbq->context3 = NULL;
427                                 size  = icmd->unsli3.rcvsli3.bde2.tus.f.bdeSize;
428                                 lpfc_ct_unsol_buffer(phba, ctiocbq, bdeBuf2,
429                                                      size);
430                                 lpfc_in_buf_free(phba, bdeBuf2);
431                         }
432                 }
433                 list_del(&head);
434         } else {
435                 INIT_LIST_HEAD(&head);
436                 list_add_tail(&head, &ctiocbq->list);
437                 list_for_each_entry(iocbq, &head, list) {
438                         icmd = &iocbq->iocb;
439                         if (icmd->ulpBdeCount == 0)
440                                 lpfc_ct_unsol_buffer(phba, iocbq, NULL, 0);
441                         for (i = 0; i < icmd->ulpBdeCount; i++) {
442                                 dma_addr = getPaddr(icmd->un.cont64[i].addrHigh,
443                                                     icmd->un.cont64[i].addrLow);
444                                 mp = lpfc_sli_ringpostbuf_get(phba, pring,
445                                                               dma_addr);
446                                 size = icmd->un.cont64[i].tus.f.bdeSize;
447                                 lpfc_ct_unsol_buffer(phba, iocbq, mp, size);
448                                 lpfc_in_buf_free(phba, mp);
449                         }
450                         lpfc_post_buffer(phba, pring, i);
451                 }
452                 list_del(&head);
453         }
454 }
455
456 /**
457  * lpfc_ct_handle_unsol_abort - ct upper level protocol abort handler
458  * @phba: Pointer to HBA context object.
459  * @dmabuf: pointer to a dmabuf that describes the FC sequence
460  *
461  * This function serves as the upper level protocol abort handler for CT
462  * protocol.
463  *
464  * Return 1 if abort has been handled, 0 otherwise.
465  **/
466 int
467 lpfc_ct_handle_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
468 {
469         int handled;
470
471         /* CT upper level goes through BSG */
472         handled = lpfc_bsg_ct_unsol_abort(phba, dmabuf);
473
474         return handled;
475 }
476
477 static void
478 lpfc_free_ct_rsp(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
479 {
480         struct lpfc_dmabuf *mlast, *next_mlast;
481
482         list_for_each_entry_safe(mlast, next_mlast, &mlist->list, list) {
483                 lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
484                 list_del(&mlast->list);
485                 kfree(mlast);
486         }
487         lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
488         kfree(mlist);
489         return;
490 }
491
492 static struct lpfc_dmabuf *
493 lpfc_alloc_ct_rsp(struct lpfc_hba *phba, int cmdcode, struct ulp_bde64 *bpl,
494                   uint32_t size, int *entries)
495 {
496         struct lpfc_dmabuf *mlist = NULL;
497         struct lpfc_dmabuf *mp;
498         int cnt, i = 0;
499
500         /* We get chunks of FCELSSIZE */
501         cnt = size > FCELSSIZE ? FCELSSIZE: size;
502
503         while (size) {
504                 /* Allocate buffer for rsp payload */
505                 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
506                 if (!mp) {
507                         if (mlist)
508                                 lpfc_free_ct_rsp(phba, mlist);
509                         return NULL;
510                 }
511
512                 INIT_LIST_HEAD(&mp->list);
513
514                 if (cmdcode == be16_to_cpu(SLI_CTNS_GID_FT) ||
515                     cmdcode == be16_to_cpu(SLI_CTNS_GFF_ID))
516                         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
517                 else
518                         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
519
520                 if (!mp->virt) {
521                         kfree(mp);
522                         if (mlist)
523                                 lpfc_free_ct_rsp(phba, mlist);
524                         return NULL;
525                 }
526
527                 /* Queue it to a linked list */
528                 if (!mlist)
529                         mlist = mp;
530                 else
531                         list_add_tail(&mp->list, &mlist->list);
532
533                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
534                 /* build buffer ptr list for IOCB */
535                 bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
536                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
537                 bpl->tus.f.bdeSize = (uint16_t) cnt;
538                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
539                 bpl++;
540
541                 i++;
542                 size -= cnt;
543         }
544
545         *entries = i;
546         return mlist;
547 }
548
549 int
550 lpfc_ct_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *ctiocb)
551 {
552         struct lpfc_dmabuf *buf_ptr;
553
554         /* I/O job is complete so context is now invalid*/
555         ctiocb->context_un.ndlp = NULL;
556         if (ctiocb->context1) {
557                 buf_ptr = (struct lpfc_dmabuf *) ctiocb->context1;
558                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
559                 kfree(buf_ptr);
560                 ctiocb->context1 = NULL;
561         }
562         if (ctiocb->context2) {
563                 lpfc_free_ct_rsp(phba, (struct lpfc_dmabuf *) ctiocb->context2);
564                 ctiocb->context2 = NULL;
565         }
566
567         if (ctiocb->context3) {
568                 buf_ptr = (struct lpfc_dmabuf *) ctiocb->context3;
569                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
570                 kfree(buf_ptr);
571                 ctiocb->context3 = NULL;
572         }
573         lpfc_sli_release_iocbq(phba, ctiocb);
574         return 0;
575 }
576
577 /*
578  * lpfc_gen_req - Build and issue a GEN_REQUEST command  to the SLI Layer
579  * @vport: pointer to a host virtual N_Port data structure.
580  * @bmp: Pointer to BPL for SLI command
581  * @inp: Pointer to data buffer for response data.
582  * @outp: Pointer to data buffer that hold the CT command.
583  * @cmpl: completion routine to call when command completes
584  * @ndlp: Destination NPort nodelist entry
585  *
586  * This function as the final part for issuing a CT command.
587  */
588 static int
589 lpfc_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
590              struct lpfc_dmabuf *inp, struct lpfc_dmabuf *outp,
591              void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
592                      struct lpfc_iocbq *),
593              struct lpfc_nodelist *ndlp, uint32_t event_tag, uint32_t num_entry,
594              uint32_t tmo, uint8_t retry)
595 {
596         struct lpfc_hba  *phba = vport->phba;
597         IOCB_t *icmd;
598         struct lpfc_iocbq *geniocb;
599         int rc;
600
601         /* Allocate buffer for  command iocb */
602         geniocb = lpfc_sli_get_iocbq(phba);
603
604         if (geniocb == NULL)
605                 return 1;
606
607         icmd = &geniocb->iocb;
608         icmd->un.genreq64.bdl.ulpIoTag32 = 0;
609         icmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
610         icmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
611         icmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
612         icmd->un.genreq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
613
614         geniocb->context3 = (uint8_t *) bmp;
615
616         /* Save for completion so we can release these resources */
617         geniocb->context1 = (uint8_t *) inp;
618         geniocb->context2 = (uint8_t *) outp;
619
620         geniocb->event_tag = event_tag;
621
622         /* Fill in payload, bp points to frame payload */
623         icmd->ulpCommand = CMD_GEN_REQUEST64_CR;
624
625         /* Fill in rest of iocb */
626         icmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
627         icmd->un.genreq64.w5.hcsw.Dfctl = 0;
628         icmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
629         icmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
630
631         if (!tmo) {
632                  /* FC spec states we need 3 * ratov for CT requests */
633                 tmo = (3 * phba->fc_ratov);
634         }
635         icmd->ulpTimeout = tmo;
636         icmd->ulpBdeCount = 1;
637         icmd->ulpLe = 1;
638         icmd->ulpClass = CLASS3;
639         icmd->ulpContext = ndlp->nlp_rpi;
640         if (phba->sli_rev == LPFC_SLI_REV4)
641                 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
642
643         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
644                 /* For GEN_REQUEST64_CR, use the RPI */
645                 icmd->ulpCt_h = 0;
646                 icmd->ulpCt_l = 0;
647         }
648
649         /* Issue GEN REQ IOCB for NPORT <did> */
650         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
651                          "0119 Issue GEN REQ IOCB to NPORT x%x "
652                          "Data: x%x x%x\n",
653                          ndlp->nlp_DID, icmd->ulpIoTag,
654                          vport->port_state);
655         geniocb->iocb_cmpl = cmpl;
656         geniocb->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
657         geniocb->vport = vport;
658         geniocb->retry = retry;
659         geniocb->context_un.ndlp = lpfc_nlp_get(ndlp);
660         if (!geniocb->context_un.ndlp)
661                 goto out;
662         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, geniocb, 0);
663
664         if (rc == IOCB_ERROR) {
665                 geniocb->context_un.ndlp = NULL;
666                 lpfc_nlp_put(ndlp);
667                 goto out;
668         }
669
670         return 0;
671 out:
672         lpfc_sli_release_iocbq(phba, geniocb);
673         return 1;
674 }
675
676 /*
677  * lpfc_ct_cmd - Build and issue a CT command
678  * @vport: pointer to a host virtual N_Port data structure.
679  * @inmp: Pointer to data buffer for response data.
680  * @bmp: Pointer to BPL for SLI command
681  * @ndlp: Destination NPort nodelist entry
682  * @cmpl: completion routine to call when command completes
683  *
684  * This function is called for issuing a CT command.
685  */
686 static int
687 lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
688             struct lpfc_dmabuf *bmp, struct lpfc_nodelist *ndlp,
689             void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
690                           struct lpfc_iocbq *),
691             uint32_t rsp_size, uint8_t retry)
692 {
693         struct lpfc_hba  *phba = vport->phba;
694         struct ulp_bde64 *bpl = (struct ulp_bde64 *) bmp->virt;
695         struct lpfc_dmabuf *outmp;
696         int cnt = 0, status;
697         int cmdcode = ((struct lpfc_sli_ct_request *) inmp->virt)->
698                 CommandResponse.bits.CmdRsp;
699
700         bpl++;                  /* Skip past ct request */
701
702         /* Put buffer(s) for ct rsp in bpl */
703         outmp = lpfc_alloc_ct_rsp(phba, cmdcode, bpl, rsp_size, &cnt);
704         if (!outmp)
705                 return -ENOMEM;
706         /*
707          * Form the CT IOCB.  The total number of BDEs in this IOCB
708          * is the single command plus response count from
709          * lpfc_alloc_ct_rsp.
710          */
711         cnt += 1;
712         status = lpfc_gen_req(vport, bmp, inmp, outmp, cmpl, ndlp,
713                         phba->fc_eventTag, cnt, 0, retry);
714         if (status) {
715                 lpfc_free_ct_rsp(phba, outmp);
716                 return -ENOMEM;
717         }
718         return 0;
719 }
720
721 struct lpfc_vport *
722 lpfc_find_vport_by_did(struct lpfc_hba *phba, uint32_t did) {
723         struct lpfc_vport *vport_curr;
724         unsigned long flags;
725
726         spin_lock_irqsave(&phba->port_list_lock, flags);
727         list_for_each_entry(vport_curr, &phba->port_list, listentry) {
728                 if ((vport_curr->fc_myDID) && (vport_curr->fc_myDID == did)) {
729                         spin_unlock_irqrestore(&phba->port_list_lock, flags);
730                         return vport_curr;
731                 }
732         }
733         spin_unlock_irqrestore(&phba->port_list_lock, flags);
734         return NULL;
735 }
736
737 static void
738 lpfc_prep_node_fc4type(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
739 {
740         struct lpfc_nodelist *ndlp;
741
742         if ((vport->port_type != LPFC_NPIV_PORT) ||
743             !(vport->ct_flags & FC_CT_RFF_ID) || !vport->cfg_restrict_login) {
744
745                 ndlp = lpfc_setup_disc_node(vport, Did);
746
747                 if (ndlp) {
748                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
749                                 "Parse GID_FTrsp: did:x%x flg:x%x x%x",
750                                 Did, ndlp->nlp_flag, vport->fc_flag);
751
752                         /* By default, the driver expects to support FCP FC4 */
753                         if (fc4_type == FC_TYPE_FCP)
754                                 ndlp->nlp_fc4_type |= NLP_FC4_FCP;
755
756                         if (fc4_type == FC_TYPE_NVME)
757                                 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
758
759                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
760                                          "0238 Process x%06x NameServer Rsp "
761                                          "Data: x%x x%x x%x x%x x%x\n", Did,
762                                          ndlp->nlp_flag, ndlp->nlp_fc4_type,
763                                          ndlp->nlp_state, vport->fc_flag,
764                                          vport->fc_rscn_id_cnt);
765
766                         /* if ndlp needs to be discovered and prior
767                          * state of ndlp hit devloss, change state to
768                          * allow rediscovery.
769                          */
770                         if (ndlp->nlp_flag & NLP_NPR_2B_DISC &&
771                             ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
772                                 lpfc_nlp_set_state(vport, ndlp,
773                                                    NLP_STE_NPR_NODE);
774                         }
775                 } else {
776                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
777                                 "Skip1 GID_FTrsp: did:x%x flg:x%x cnt:%d",
778                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
779
780                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
781                                          "0239 Skip x%06x NameServer Rsp "
782                                          "Data: x%x x%x x%px\n",
783                                          Did, vport->fc_flag,
784                                          vport->fc_rscn_id_cnt, ndlp);
785                 }
786         } else {
787                 if (!(vport->fc_flag & FC_RSCN_MODE) ||
788                     lpfc_rscn_payload_check(vport, Did)) {
789                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
790                                 "Query GID_FTrsp: did:x%x flg:x%x cnt:%d",
791                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
792
793                         /*
794                          * This NPortID was previously a FCP/NVMe target,
795                          * Don't even bother to send GFF_ID.
796                          */
797                         ndlp = lpfc_findnode_did(vport, Did);
798                         if (ndlp &&
799                             (ndlp->nlp_type &
800                             (NLP_FCP_TARGET | NLP_NVME_TARGET))) {
801                                 if (fc4_type == FC_TYPE_FCP)
802                                         ndlp->nlp_fc4_type |= NLP_FC4_FCP;
803                                 if (fc4_type == FC_TYPE_NVME)
804                                         ndlp->nlp_fc4_type |= NLP_FC4_NVME;
805                                 lpfc_setup_disc_node(vport, Did);
806                         } else if (lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
807                                    0, Did) == 0)
808                                 vport->num_disc_nodes++;
809                         else
810                                 lpfc_setup_disc_node(vport, Did);
811                 } else {
812                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
813                                 "Skip2 GID_FTrsp: did:x%x flg:x%x cnt:%d",
814                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
815
816                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
817                                          "0245 Skip x%06x NameServer Rsp "
818                                          "Data: x%x x%x\n", Did,
819                                          vport->fc_flag,
820                                          vport->fc_rscn_id_cnt);
821                 }
822         }
823 }
824
825 static void
826 lpfc_ns_rsp_audit_did(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
827 {
828         struct lpfc_hba *phba = vport->phba;
829         struct lpfc_nodelist *ndlp = NULL;
830         char *str;
831
832         if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT)
833                 str = "GID_FT";
834         else
835                 str = "GID_PT";
836         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
837                          "6430 Process %s rsp for %08x type %x %s %s\n",
838                          str, Did, fc4_type,
839                          (fc4_type == FC_TYPE_FCP) ?  "FCP" : " ",
840                          (fc4_type == FC_TYPE_NVME) ?  "NVME" : " ");
841         /*
842          * To conserve rpi's, filter out addresses for other
843          * vports on the same physical HBAs.
844          */
845         if (Did != vport->fc_myDID &&
846             (!lpfc_find_vport_by_did(phba, Did) ||
847              vport->cfg_peer_port_login)) {
848                 if (!phba->nvmet_support) {
849                         /* FCPI/NVMEI path. Process Did */
850                         lpfc_prep_node_fc4type(vport, Did, fc4_type);
851                         return;
852                 }
853                 /* NVMET path.  NVMET only cares about NVMEI nodes. */
854                 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
855                         if (ndlp->nlp_type != NLP_NVME_INITIATOR ||
856                             ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
857                                 continue;
858                         spin_lock_irq(&ndlp->lock);
859                         if (ndlp->nlp_DID == Did)
860                                 ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
861                         else
862                                 ndlp->nlp_flag |= NLP_NVMET_RECOV;
863                         spin_unlock_irq(&ndlp->lock);
864                 }
865         }
866 }
867
868 static int
869 lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint8_t fc4_type,
870             uint32_t Size)
871 {
872         struct lpfc_sli_ct_request *Response =
873                 (struct lpfc_sli_ct_request *) mp->virt;
874         struct lpfc_dmabuf *mlast, *next_mp;
875         uint32_t *ctptr = (uint32_t *) & Response->un.gid.PortType;
876         uint32_t Did, CTentry;
877         int Cnt;
878         struct list_head head;
879         struct lpfc_nodelist *ndlp = NULL;
880
881         lpfc_set_disctmo(vport);
882         vport->num_disc_nodes = 0;
883         vport->fc_ns_retry = 0;
884
885
886         list_add_tail(&head, &mp->list);
887         list_for_each_entry_safe(mp, next_mp, &head, list) {
888                 mlast = mp;
889
890                 Cnt = Size  > FCELSSIZE ? FCELSSIZE : Size;
891
892                 Size -= Cnt;
893
894                 if (!ctptr) {
895                         ctptr = (uint32_t *) mlast->virt;
896                 } else
897                         Cnt -= 16;      /* subtract length of CT header */
898
899                 /* Loop through entire NameServer list of DIDs */
900                 while (Cnt >= sizeof(uint32_t)) {
901                         /* Get next DID from NameServer List */
902                         CTentry = *ctptr++;
903                         Did = ((be32_to_cpu(CTentry)) & Mask_DID);
904                         lpfc_ns_rsp_audit_did(vport, Did, fc4_type);
905                         if (CTentry & (cpu_to_be32(SLI_CT_LAST_ENTRY)))
906                                 goto nsout1;
907
908                         Cnt -= sizeof(uint32_t);
909                 }
910                 ctptr = NULL;
911
912         }
913
914         /* All GID_FT entries processed.  If the driver is running in
915          * in target mode, put impacted nodes into recovery and drop
916          * the RPI to flush outstanding IO.
917          */
918         if (vport->phba->nvmet_support) {
919                 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
920                         if (!(ndlp->nlp_flag & NLP_NVMET_RECOV))
921                                 continue;
922                         lpfc_disc_state_machine(vport, ndlp, NULL,
923                                                 NLP_EVT_DEVICE_RECOVERY);
924                         spin_lock_irq(&ndlp->lock);
925                         ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
926                         spin_unlock_irq(&ndlp->lock);
927                 }
928         }
929
930 nsout1:
931         list_del(&head);
932         return 0;
933 }
934
935 static void
936 lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
937                         struct lpfc_iocbq *rspiocb)
938 {
939         struct lpfc_vport *vport = cmdiocb->vport;
940         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
941         IOCB_t *irsp;
942         struct lpfc_dmabuf *outp;
943         struct lpfc_dmabuf *inp;
944         struct lpfc_sli_ct_request *CTrsp;
945         struct lpfc_sli_ct_request *CTreq;
946         struct lpfc_nodelist *ndlp;
947         int rc, type;
948
949         /* First save ndlp, before we overwrite it */
950         ndlp = cmdiocb->context_un.ndlp;
951
952         /* we pass cmdiocb to state machine which needs rspiocb as well */
953         cmdiocb->context_un.rsp_iocb = rspiocb;
954         inp = (struct lpfc_dmabuf *) cmdiocb->context1;
955         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
956         irsp = &rspiocb->iocb;
957
958         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
959                  "GID_FT cmpl:     status:x%x/x%x rtry:%d",
960                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_ns_retry);
961
962         /* Ignore response if link flipped after this request was made */
963         if (cmdiocb->event_tag != phba->fc_eventTag) {
964                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
965                                  "9043 Event tag mismatch. Ignoring NS rsp\n");
966                 goto out;
967         }
968
969         /* Don't bother processing response if vport is being torn down. */
970         if (vport->load_flag & FC_UNLOADING) {
971                 if (vport->fc_flag & FC_RSCN_MODE)
972                         lpfc_els_flush_rscn(vport);
973                 goto out;
974         }
975
976         if (lpfc_els_chk_latt(vport)) {
977                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
978                                  "0216 Link event during NS query\n");
979                 if (vport->fc_flag & FC_RSCN_MODE)
980                         lpfc_els_flush_rscn(vport);
981                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
982                 goto out;
983         }
984         if (lpfc_error_lost_link(irsp)) {
985                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
986                                  "0226 NS query failed due to link event\n");
987                 if (vport->fc_flag & FC_RSCN_MODE)
988                         lpfc_els_flush_rscn(vport);
989                 goto out;
990         }
991
992         spin_lock_irq(shost->host_lock);
993         if (vport->fc_flag & FC_RSCN_DEFERRED) {
994                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
995                 spin_unlock_irq(shost->host_lock);
996
997                 /* This is a GID_FT completing so the gidft_inp counter was
998                  * incremented before the GID_FT was issued to the wire.
999                  */
1000                 if (vport->gidft_inp)
1001                         vport->gidft_inp--;
1002
1003                 /*
1004                  * Skip processing the NS response
1005                  * Re-issue the NS cmd
1006                  */
1007                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1008                                  "0151 Process Deferred RSCN Data: x%x x%x\n",
1009                                  vport->fc_flag, vport->fc_rscn_id_cnt);
1010                 lpfc_els_handle_rscn(vport);
1011
1012                 goto out;
1013         }
1014         spin_unlock_irq(shost->host_lock);
1015
1016         if (irsp->ulpStatus) {
1017                 /* Check for retry */
1018                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
1019                         if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
1020                             (irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1021                             IOERR_NO_RESOURCES)
1022                                 vport->fc_ns_retry++;
1023
1024                         type = lpfc_get_gidft_type(vport, cmdiocb);
1025                         if (type == 0)
1026                                 goto out;
1027
1028                         /* CT command is being retried */
1029                         rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
1030                                          vport->fc_ns_retry, type);
1031                         if (rc == 0)
1032                                 goto out;
1033                         else { /* Unable to send NS cmd */
1034                                 if (vport->gidft_inp)
1035                                         vport->gidft_inp--;
1036                         }
1037                 }
1038                 if (vport->fc_flag & FC_RSCN_MODE)
1039                         lpfc_els_flush_rscn(vport);
1040                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1041                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1042                                  "0257 GID_FT Query error: 0x%x 0x%x\n",
1043                                  irsp->ulpStatus, vport->fc_ns_retry);
1044         } else {
1045                 /* Good status, continue checking */
1046                 CTreq = (struct lpfc_sli_ct_request *) inp->virt;
1047                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1048                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1049                     cpu_to_be16(SLI_CT_RESPONSE_FS_ACC)) {
1050                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1051                                          "0208 NameServer Rsp Data: x%x x%x "
1052                                          "x%x x%x sz x%x\n",
1053                                          vport->fc_flag,
1054                                          CTreq->un.gid.Fc4Type,
1055                                          vport->num_disc_nodes,
1056                                          vport->gidft_inp,
1057                                          irsp->un.genreq64.bdl.bdeSize);
1058
1059                         lpfc_ns_rsp(vport,
1060                                     outp,
1061                                     CTreq->un.gid.Fc4Type,
1062                                     (uint32_t) (irsp->un.genreq64.bdl.bdeSize));
1063                 } else if (CTrsp->CommandResponse.bits.CmdRsp ==
1064                            be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
1065                         /* NameServer Rsp Error */
1066                         if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
1067                             && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
1068                                 lpfc_printf_vlog(vport, KERN_INFO,
1069                                         LOG_DISCOVERY,
1070                                         "0269 No NameServer Entries "
1071                                         "Data: x%x x%x x%x x%x\n",
1072                                         CTrsp->CommandResponse.bits.CmdRsp,
1073                                         (uint32_t) CTrsp->ReasonCode,
1074                                         (uint32_t) CTrsp->Explanation,
1075                                         vport->fc_flag);
1076
1077                                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1078                                 "GID_FT no entry  cmd:x%x rsn:x%x exp:x%x",
1079                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1080                                 (uint32_t) CTrsp->ReasonCode,
1081                                 (uint32_t) CTrsp->Explanation);
1082                         } else {
1083                                 lpfc_printf_vlog(vport, KERN_INFO,
1084                                         LOG_DISCOVERY,
1085                                         "0240 NameServer Rsp Error "
1086                                         "Data: x%x x%x x%x x%x\n",
1087                                         CTrsp->CommandResponse.bits.CmdRsp,
1088                                         (uint32_t) CTrsp->ReasonCode,
1089                                         (uint32_t) CTrsp->Explanation,
1090                                         vport->fc_flag);
1091
1092                                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1093                                 "GID_FT rsp err1  cmd:x%x rsn:x%x exp:x%x",
1094                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1095                                 (uint32_t) CTrsp->ReasonCode,
1096                                 (uint32_t) CTrsp->Explanation);
1097                         }
1098
1099
1100                 } else {
1101                         /* NameServer Rsp Error */
1102                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1103                                         "0241 NameServer Rsp Error "
1104                                         "Data: x%x x%x x%x x%x\n",
1105                                         CTrsp->CommandResponse.bits.CmdRsp,
1106                                         (uint32_t) CTrsp->ReasonCode,
1107                                         (uint32_t) CTrsp->Explanation,
1108                                         vport->fc_flag);
1109
1110                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1111                                 "GID_FT rsp err2  cmd:x%x rsn:x%x exp:x%x",
1112                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1113                                 (uint32_t) CTrsp->ReasonCode,
1114                                 (uint32_t) CTrsp->Explanation);
1115                 }
1116                 if (vport->gidft_inp)
1117                         vport->gidft_inp--;
1118         }
1119
1120         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1121                          "4216 GID_FT cmpl inp %d disc %d\n",
1122                          vport->gidft_inp, vport->num_disc_nodes);
1123
1124         /* Link up / RSCN discovery */
1125         if ((vport->num_disc_nodes == 0) &&
1126             (vport->gidft_inp == 0)) {
1127                 /*
1128                  * The driver has cycled through all Nports in the RSCN payload.
1129                  * Complete the handling by cleaning up and marking the
1130                  * current driver state.
1131                  */
1132                 if (vport->port_state >= LPFC_DISC_AUTH) {
1133                         if (vport->fc_flag & FC_RSCN_MODE) {
1134                                 lpfc_els_flush_rscn(vport);
1135                                 spin_lock_irq(shost->host_lock);
1136                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1137                                 spin_unlock_irq(shost->host_lock);
1138                         }
1139                         else
1140                                 lpfc_els_flush_rscn(vport);
1141                 }
1142
1143                 lpfc_disc_start(vport);
1144         }
1145 out:
1146         lpfc_ct_free_iocb(phba, cmdiocb);
1147         lpfc_nlp_put(ndlp);
1148         return;
1149 }
1150
1151 static void
1152 lpfc_cmpl_ct_cmd_gid_pt(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1153                         struct lpfc_iocbq *rspiocb)
1154 {
1155         struct lpfc_vport *vport = cmdiocb->vport;
1156         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1157         IOCB_t *irsp;
1158         struct lpfc_dmabuf *outp;
1159         struct lpfc_dmabuf *inp;
1160         struct lpfc_sli_ct_request *CTrsp;
1161         struct lpfc_sli_ct_request *CTreq;
1162         struct lpfc_nodelist *ndlp;
1163         int rc;
1164
1165         /* First save ndlp, before we overwrite it */
1166         ndlp = cmdiocb->context_un.ndlp;
1167
1168         /* we pass cmdiocb to state machine which needs rspiocb as well */
1169         cmdiocb->context_un.rsp_iocb = rspiocb;
1170         inp = (struct lpfc_dmabuf *)cmdiocb->context1;
1171         outp = (struct lpfc_dmabuf *)cmdiocb->context2;
1172         irsp = &rspiocb->iocb;
1173
1174         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1175                               "GID_PT cmpl:     status:x%x/x%x rtry:%d",
1176                               irsp->ulpStatus, irsp->un.ulpWord[4],
1177                               vport->fc_ns_retry);
1178
1179         /* Ignore response if link flipped after this request was made */
1180         if (cmdiocb->event_tag != phba->fc_eventTag) {
1181                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1182                                  "9044 Event tag mismatch. Ignoring NS rsp\n");
1183                 goto out;
1184         }
1185
1186         /* Don't bother processing response if vport is being torn down. */
1187         if (vport->load_flag & FC_UNLOADING) {
1188                 if (vport->fc_flag & FC_RSCN_MODE)
1189                         lpfc_els_flush_rscn(vport);
1190                 goto out;
1191         }
1192
1193         if (lpfc_els_chk_latt(vport)) {
1194                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1195                                  "4108 Link event during NS query\n");
1196                 if (vport->fc_flag & FC_RSCN_MODE)
1197                         lpfc_els_flush_rscn(vport);
1198                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1199                 goto out;
1200         }
1201         if (lpfc_error_lost_link(irsp)) {
1202                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1203                                  "4166 NS query failed due to link event\n");
1204                 if (vport->fc_flag & FC_RSCN_MODE)
1205                         lpfc_els_flush_rscn(vport);
1206                 goto out;
1207         }
1208
1209         spin_lock_irq(shost->host_lock);
1210         if (vport->fc_flag & FC_RSCN_DEFERRED) {
1211                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
1212                 spin_unlock_irq(shost->host_lock);
1213
1214                 /* This is a GID_PT completing so the gidft_inp counter was
1215                  * incremented before the GID_PT was issued to the wire.
1216                  */
1217                 if (vport->gidft_inp)
1218                         vport->gidft_inp--;
1219
1220                 /*
1221                  * Skip processing the NS response
1222                  * Re-issue the NS cmd
1223                  */
1224                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1225                                  "4167 Process Deferred RSCN Data: x%x x%x\n",
1226                                  vport->fc_flag, vport->fc_rscn_id_cnt);
1227                 lpfc_els_handle_rscn(vport);
1228
1229                 goto out;
1230         }
1231         spin_unlock_irq(shost->host_lock);
1232
1233         if (irsp->ulpStatus) {
1234                 /* Check for retry */
1235                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
1236                         if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
1237                             (irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1238                             IOERR_NO_RESOURCES)
1239                                 vport->fc_ns_retry++;
1240
1241                         /* CT command is being retried */
1242                         rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_PT,
1243                                          vport->fc_ns_retry, GID_PT_N_PORT);
1244                         if (rc == 0)
1245                                 goto out;
1246                         else { /* Unable to send NS cmd */
1247                                 if (vport->gidft_inp)
1248                                         vport->gidft_inp--;
1249                         }
1250                 }
1251                 if (vport->fc_flag & FC_RSCN_MODE)
1252                         lpfc_els_flush_rscn(vport);
1253                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1254                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1255                                  "4103 GID_FT Query error: 0x%x 0x%x\n",
1256                                  irsp->ulpStatus, vport->fc_ns_retry);
1257         } else {
1258                 /* Good status, continue checking */
1259                 CTreq = (struct lpfc_sli_ct_request *)inp->virt;
1260                 CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
1261                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1262                     cpu_to_be16(SLI_CT_RESPONSE_FS_ACC)) {
1263                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1264                                          "4105 NameServer Rsp Data: x%x x%x "
1265                                          "x%x x%x sz x%x\n",
1266                                          vport->fc_flag,
1267                                          CTreq->un.gid.Fc4Type,
1268                                          vport->num_disc_nodes,
1269                                          vport->gidft_inp,
1270                                          irsp->un.genreq64.bdl.bdeSize);
1271
1272                         lpfc_ns_rsp(vport,
1273                                     outp,
1274                                     CTreq->un.gid.Fc4Type,
1275                                     (uint32_t)(irsp->un.genreq64.bdl.bdeSize));
1276                 } else if (CTrsp->CommandResponse.bits.CmdRsp ==
1277                            be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
1278                         /* NameServer Rsp Error */
1279                         if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
1280                             && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
1281                                 lpfc_printf_vlog(
1282                                         vport, KERN_INFO, LOG_DISCOVERY,
1283                                         "4106 No NameServer Entries "
1284                                         "Data: x%x x%x x%x x%x\n",
1285                                         CTrsp->CommandResponse.bits.CmdRsp,
1286                                         (uint32_t)CTrsp->ReasonCode,
1287                                         (uint32_t)CTrsp->Explanation,
1288                                         vport->fc_flag);
1289
1290                                 lpfc_debugfs_disc_trc(
1291                                 vport, LPFC_DISC_TRC_CT,
1292                                 "GID_PT no entry  cmd:x%x rsn:x%x exp:x%x",
1293                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1294                                 (uint32_t)CTrsp->ReasonCode,
1295                                 (uint32_t)CTrsp->Explanation);
1296                         } else {
1297                                 lpfc_printf_vlog(
1298                                         vport, KERN_INFO, LOG_DISCOVERY,
1299                                         "4107 NameServer Rsp Error "
1300                                         "Data: x%x x%x x%x x%x\n",
1301                                         CTrsp->CommandResponse.bits.CmdRsp,
1302                                         (uint32_t)CTrsp->ReasonCode,
1303                                         (uint32_t)CTrsp->Explanation,
1304                                         vport->fc_flag);
1305
1306                                 lpfc_debugfs_disc_trc(
1307                                 vport, LPFC_DISC_TRC_CT,
1308                                 "GID_PT rsp err1  cmd:x%x rsn:x%x exp:x%x",
1309                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1310                                 (uint32_t)CTrsp->ReasonCode,
1311                                 (uint32_t)CTrsp->Explanation);
1312                         }
1313                 } else {
1314                         /* NameServer Rsp Error */
1315                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1316                                          "4109 NameServer Rsp Error "
1317                                          "Data: x%x x%x x%x x%x\n",
1318                                          CTrsp->CommandResponse.bits.CmdRsp,
1319                                          (uint32_t)CTrsp->ReasonCode,
1320                                          (uint32_t)CTrsp->Explanation,
1321                                          vport->fc_flag);
1322
1323                         lpfc_debugfs_disc_trc(
1324                                 vport, LPFC_DISC_TRC_CT,
1325                                 "GID_PT rsp err2  cmd:x%x rsn:x%x exp:x%x",
1326                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1327                                 (uint32_t)CTrsp->ReasonCode,
1328                                 (uint32_t)CTrsp->Explanation);
1329                 }
1330                 if (vport->gidft_inp)
1331                         vport->gidft_inp--;
1332         }
1333
1334         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1335                          "6450 GID_PT cmpl inp %d disc %d\n",
1336                          vport->gidft_inp, vport->num_disc_nodes);
1337
1338         /* Link up / RSCN discovery */
1339         if ((vport->num_disc_nodes == 0) &&
1340             (vport->gidft_inp == 0)) {
1341                 /*
1342                  * The driver has cycled through all Nports in the RSCN payload.
1343                  * Complete the handling by cleaning up and marking the
1344                  * current driver state.
1345                  */
1346                 if (vport->port_state >= LPFC_DISC_AUTH) {
1347                         if (vport->fc_flag & FC_RSCN_MODE) {
1348                                 lpfc_els_flush_rscn(vport);
1349                                 spin_lock_irq(shost->host_lock);
1350                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1351                                 spin_unlock_irq(shost->host_lock);
1352                         } else {
1353                                 lpfc_els_flush_rscn(vport);
1354                         }
1355                 }
1356
1357                 lpfc_disc_start(vport);
1358         }
1359 out:
1360         lpfc_ct_free_iocb(phba, cmdiocb);
1361         lpfc_nlp_put(ndlp);
1362 }
1363
1364 static void
1365 lpfc_cmpl_ct_cmd_gff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1366                         struct lpfc_iocbq *rspiocb)
1367 {
1368         struct lpfc_vport *vport = cmdiocb->vport;
1369         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1370         IOCB_t *irsp = &rspiocb->iocb;
1371         struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *) cmdiocb->context1;
1372         struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1373         struct lpfc_sli_ct_request *CTrsp;
1374         int did, rc, retry;
1375         uint8_t fbits;
1376         struct lpfc_nodelist *ndlp = NULL, *free_ndlp = NULL;
1377
1378         did = ((struct lpfc_sli_ct_request *) inp->virt)->un.gff.PortId;
1379         did = be32_to_cpu(did);
1380
1381         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1382                 "GFF_ID cmpl:     status:x%x/x%x did:x%x",
1383                 irsp->ulpStatus, irsp->un.ulpWord[4], did);
1384
1385         /* Ignore response if link flipped after this request was made */
1386         if (cmdiocb->event_tag != phba->fc_eventTag) {
1387                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1388                                  "9045 Event tag mismatch. Ignoring NS rsp\n");
1389                 goto iocb_free;
1390         }
1391
1392         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1393                 /* Good status, continue checking */
1394                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1395                 fbits = CTrsp->un.gff_acc.fbits[FCP_TYPE_FEATURE_OFFSET];
1396
1397                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1398                                  "6431 Process GFF_ID rsp for %08x "
1399                                  "fbits %02x %s %s\n",
1400                                  did, fbits,
1401                                  (fbits & FC4_FEATURE_INIT) ? "Initiator" : " ",
1402                                  (fbits & FC4_FEATURE_TARGET) ? "Target" : " ");
1403
1404                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1405                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
1406                         if ((fbits & FC4_FEATURE_INIT) &&
1407                             !(fbits & FC4_FEATURE_TARGET)) {
1408                                 lpfc_printf_vlog(vport, KERN_INFO,
1409                                                  LOG_DISCOVERY,
1410                                                  "0270 Skip x%x GFF "
1411                                                  "NameServer Rsp Data: (init) "
1412                                                  "x%x x%x\n", did, fbits,
1413                                                  vport->fc_rscn_id_cnt);
1414                                 goto out;
1415                         }
1416                 }
1417         }
1418         else {
1419                 /* Check for retry */
1420                 if (cmdiocb->retry < LPFC_MAX_NS_RETRY) {
1421                         retry = 1;
1422                         if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1423                                 switch ((irsp->un.ulpWord[4] &
1424                                         IOERR_PARAM_MASK)) {
1425
1426                                 case IOERR_NO_RESOURCES:
1427                                         /* We don't increment the retry
1428                                          * count for this case.
1429                                          */
1430                                         break;
1431                                 case IOERR_LINK_DOWN:
1432                                 case IOERR_SLI_ABORTED:
1433                                 case IOERR_SLI_DOWN:
1434                                         retry = 0;
1435                                         break;
1436                                 default:
1437                                         cmdiocb->retry++;
1438                                 }
1439                         }
1440                         else
1441                                 cmdiocb->retry++;
1442
1443                         if (retry) {
1444                                 /* CT command is being retried */
1445                                 rc = lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
1446                                          cmdiocb->retry, did);
1447                                 if (rc == 0) {
1448                                         /* success */
1449                                         free_ndlp = cmdiocb->context_un.ndlp;
1450                                         lpfc_ct_free_iocb(phba, cmdiocb);
1451                                         lpfc_nlp_put(free_ndlp);
1452                                         return;
1453                                 }
1454                         }
1455                 }
1456                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1457                                  "0267 NameServer GFF Rsp "
1458                                  "x%x Error (%d %d) Data: x%x x%x\n",
1459                                  did, irsp->ulpStatus, irsp->un.ulpWord[4],
1460                                  vport->fc_flag, vport->fc_rscn_id_cnt);
1461         }
1462
1463         /* This is a target port, unregistered port, or the GFF_ID failed */
1464         ndlp = lpfc_setup_disc_node(vport, did);
1465         if (ndlp) {
1466                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1467                                  "0242 Process x%x GFF "
1468                                  "NameServer Rsp Data: x%x x%x x%x\n",
1469                                  did, ndlp->nlp_flag, vport->fc_flag,
1470                                  vport->fc_rscn_id_cnt);
1471         } else {
1472                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1473                                  "0243 Skip x%x GFF "
1474                                  "NameServer Rsp Data: x%x x%x\n", did,
1475                                  vport->fc_flag, vport->fc_rscn_id_cnt);
1476         }
1477 out:
1478         /* Link up / RSCN discovery */
1479         if (vport->num_disc_nodes)
1480                 vport->num_disc_nodes--;
1481
1482         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1483                          "6451 GFF_ID cmpl inp %d disc %d\n",
1484                          vport->gidft_inp, vport->num_disc_nodes);
1485
1486         if (vport->num_disc_nodes == 0) {
1487                 /*
1488                  * The driver has cycled through all Nports in the RSCN payload.
1489                  * Complete the handling by cleaning up and marking the
1490                  * current driver state.
1491                  */
1492                 if (vport->port_state >= LPFC_DISC_AUTH) {
1493                         if (vport->fc_flag & FC_RSCN_MODE) {
1494                                 lpfc_els_flush_rscn(vport);
1495                                 spin_lock_irq(shost->host_lock);
1496                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1497                                 spin_unlock_irq(shost->host_lock);
1498                         }
1499                         else
1500                                 lpfc_els_flush_rscn(vport);
1501                 }
1502                 lpfc_disc_start(vport);
1503         }
1504
1505 iocb_free:
1506         free_ndlp = cmdiocb->context_un.ndlp;
1507         lpfc_ct_free_iocb(phba, cmdiocb);
1508         lpfc_nlp_put(free_ndlp);
1509         return;
1510 }
1511
1512 static void
1513 lpfc_cmpl_ct_cmd_gft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1514                                 struct lpfc_iocbq *rspiocb)
1515 {
1516         struct lpfc_vport *vport = cmdiocb->vport;
1517         IOCB_t *irsp = &rspiocb->iocb;
1518         struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *)cmdiocb->context1;
1519         struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *)cmdiocb->context2;
1520         struct lpfc_sli_ct_request *CTrsp;
1521         int did;
1522         struct lpfc_nodelist *ndlp = NULL;
1523         struct lpfc_nodelist *ns_ndlp = NULL;
1524         uint32_t fc4_data_0, fc4_data_1;
1525
1526         did = ((struct lpfc_sli_ct_request *)inp->virt)->un.gft.PortId;
1527         did = be32_to_cpu(did);
1528
1529         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1530                               "GFT_ID cmpl: status:x%x/x%x did:x%x",
1531                               irsp->ulpStatus, irsp->un.ulpWord[4], did);
1532
1533         /* Ignore response if link flipped after this request was made */
1534         if ((uint32_t) cmdiocb->event_tag != phba->fc_eventTag) {
1535                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1536                                  "9046 Event tag mismatch. Ignoring NS rsp\n");
1537                 goto out;
1538         }
1539
1540         /* Preserve the nameserver node to release the reference. */
1541         ns_ndlp = cmdiocb->context_un.ndlp;
1542
1543         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1544                 /* Good status, continue checking */
1545                 CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
1546                 fc4_data_0 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[0]);
1547                 fc4_data_1 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[1]);
1548
1549                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1550                                  "6432 Process GFT_ID rsp for %08x "
1551                                  "Data %08x %08x %s %s\n",
1552                                  did, fc4_data_0, fc4_data_1,
1553                                  (fc4_data_0 & LPFC_FC4_TYPE_BITMASK) ?
1554                                   "FCP" : " ",
1555                                  (fc4_data_1 & LPFC_FC4_TYPE_BITMASK) ?
1556                                   "NVME" : " ");
1557
1558                 /* Lookup the NPort_ID queried in the GFT_ID and find the
1559                  * driver's local node.  It's an error if the driver
1560                  * doesn't have one.
1561                  */
1562                 ndlp = lpfc_findnode_did(vport, did);
1563                 if (ndlp) {
1564                         /* The bitmask value for FCP and NVME FCP types is
1565                          * the same because they are 32 bits distant from
1566                          * each other in word0 and word0.
1567                          */
1568                         if (fc4_data_0 & LPFC_FC4_TYPE_BITMASK)
1569                                 ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1570                         if (fc4_data_1 &  LPFC_FC4_TYPE_BITMASK)
1571                                 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1572                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1573                                          "3064 Setting ndlp x%px, DID x%06x "
1574                                          "with FC4 x%08x, Data: x%08x x%08x "
1575                                          "%d\n",
1576                                          ndlp, did, ndlp->nlp_fc4_type,
1577                                          FC_TYPE_FCP, FC_TYPE_NVME,
1578                                          ndlp->nlp_state);
1579
1580                         if (ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE &&
1581                             ndlp->nlp_fc4_type) {
1582                                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1583
1584                                 lpfc_nlp_set_state(vport, ndlp,
1585                                                    NLP_STE_PRLI_ISSUE);
1586                                 lpfc_issue_els_prli(vport, ndlp, 0);
1587                         } else if (!ndlp->nlp_fc4_type) {
1588                                 /* If fc4 type is still unknown, then LOGO */
1589                                 lpfc_printf_vlog(vport, KERN_INFO,
1590                                                  LOG_DISCOVERY,
1591                                                  "6443 Sending LOGO ndlp x%px,"
1592                                                  "DID x%06x with fc4_type: "
1593                                                  "x%08x, state: %d\n",
1594                                                  ndlp, did, ndlp->nlp_fc4_type,
1595                                                  ndlp->nlp_state);
1596                                 lpfc_issue_els_logo(vport, ndlp, 0);
1597                                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1598                                 lpfc_nlp_set_state(vport, ndlp,
1599                                                    NLP_STE_NPR_NODE);
1600                         }
1601                 }
1602         } else
1603                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1604                                  "3065 GFT_ID failed x%08x\n", irsp->ulpStatus);
1605
1606 out:
1607         lpfc_ct_free_iocb(phba, cmdiocb);
1608         lpfc_nlp_put(ns_ndlp);
1609 }
1610
1611 static void
1612 lpfc_cmpl_ct(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1613              struct lpfc_iocbq *rspiocb)
1614 {
1615         struct lpfc_vport *vport = cmdiocb->vport;
1616         struct lpfc_dmabuf *inp;
1617         struct lpfc_dmabuf *outp;
1618         IOCB_t *irsp;
1619         struct lpfc_sli_ct_request *CTrsp;
1620         struct lpfc_nodelist *ndlp;
1621         int cmdcode, rc;
1622         uint8_t retry;
1623         uint32_t latt;
1624
1625         /* First save ndlp, before we overwrite it */
1626         ndlp = cmdiocb->context_un.ndlp;
1627
1628         /* we pass cmdiocb to state machine which needs rspiocb as well */
1629         cmdiocb->context_un.rsp_iocb = rspiocb;
1630
1631         inp = (struct lpfc_dmabuf *) cmdiocb->context1;
1632         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1633         irsp = &rspiocb->iocb;
1634
1635         cmdcode = be16_to_cpu(((struct lpfc_sli_ct_request *) inp->virt)->
1636                                         CommandResponse.bits.CmdRsp);
1637         CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1638
1639         latt = lpfc_els_chk_latt(vport);
1640
1641         /* RFT request completes status <ulpStatus> CmdRsp <CmdRsp> */
1642         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1643                          "0209 CT Request completes, latt %d, "
1644                          "ulpStatus x%x CmdRsp x%x, Context x%x, Tag x%x\n",
1645                          latt, irsp->ulpStatus,
1646                          CTrsp->CommandResponse.bits.CmdRsp,
1647                          cmdiocb->iocb.ulpContext, cmdiocb->iocb.ulpIoTag);
1648
1649         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1650                 "CT cmd cmpl:     status:x%x/x%x cmd:x%x",
1651                 irsp->ulpStatus, irsp->un.ulpWord[4], cmdcode);
1652
1653         if (irsp->ulpStatus) {
1654                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1655                                  "0268 NS cmd x%x Error (x%x x%x)\n",
1656                                  cmdcode, irsp->ulpStatus, irsp->un.ulpWord[4]);
1657
1658                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1659                         (((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1660                           IOERR_SLI_DOWN) ||
1661                          ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1662                           IOERR_SLI_ABORTED)))
1663                         goto out;
1664
1665                 retry = cmdiocb->retry;
1666                 if (retry >= LPFC_MAX_NS_RETRY)
1667                         goto out;
1668
1669                 retry++;
1670                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1671                                  "0250 Retrying NS cmd %x\n", cmdcode);
1672                 rc = lpfc_ns_cmd(vport, cmdcode, retry, 0);
1673                 if (rc == 0)
1674                         goto out;
1675         }
1676
1677 out:
1678         lpfc_ct_free_iocb(phba, cmdiocb);
1679         lpfc_nlp_put(ndlp);
1680         return;
1681 }
1682
1683 static void
1684 lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1685                         struct lpfc_iocbq *rspiocb)
1686 {
1687         IOCB_t *irsp = &rspiocb->iocb;
1688         struct lpfc_vport *vport = cmdiocb->vport;
1689
1690         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1691                 struct lpfc_dmabuf *outp;
1692                 struct lpfc_sli_ct_request *CTrsp;
1693
1694                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1695                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1696                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1697                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1698                         vport->ct_flags |= FC_CT_RFT_ID;
1699         }
1700         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1701         return;
1702 }
1703
1704 static void
1705 lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1706                         struct lpfc_iocbq *rspiocb)
1707 {
1708         IOCB_t *irsp = &rspiocb->iocb;
1709         struct lpfc_vport *vport = cmdiocb->vport;
1710
1711         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1712                 struct lpfc_dmabuf *outp;
1713                 struct lpfc_sli_ct_request *CTrsp;
1714
1715                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1716                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1717                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1718                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1719                         vport->ct_flags |= FC_CT_RNN_ID;
1720         }
1721         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1722         return;
1723 }
1724
1725 static void
1726 lpfc_cmpl_ct_cmd_rspn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1727                          struct lpfc_iocbq *rspiocb)
1728 {
1729         IOCB_t *irsp = &rspiocb->iocb;
1730         struct lpfc_vport *vport = cmdiocb->vport;
1731
1732         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1733                 struct lpfc_dmabuf *outp;
1734                 struct lpfc_sli_ct_request *CTrsp;
1735
1736                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1737                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1738                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1739                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1740                         vport->ct_flags |= FC_CT_RSPN_ID;
1741         }
1742         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1743         return;
1744 }
1745
1746 static void
1747 lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1748                          struct lpfc_iocbq *rspiocb)
1749 {
1750         IOCB_t *irsp = &rspiocb->iocb;
1751         struct lpfc_vport *vport = cmdiocb->vport;
1752
1753         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1754                 struct lpfc_dmabuf *outp;
1755                 struct lpfc_sli_ct_request *CTrsp;
1756
1757                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1758                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1759                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1760                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1761                         vport->ct_flags |= FC_CT_RSNN_NN;
1762         }
1763         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1764         return;
1765 }
1766
1767 static void
1768 lpfc_cmpl_ct_cmd_da_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1769  struct lpfc_iocbq *rspiocb)
1770 {
1771         struct lpfc_vport *vport = cmdiocb->vport;
1772
1773         /* even if it fails we will act as though it succeeded. */
1774         vport->ct_flags = 0;
1775         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1776         return;
1777 }
1778
1779 static void
1780 lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1781                         struct lpfc_iocbq *rspiocb)
1782 {
1783         IOCB_t *irsp = &rspiocb->iocb;
1784         struct lpfc_vport *vport = cmdiocb->vport;
1785
1786         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1787                 struct lpfc_dmabuf *outp;
1788                 struct lpfc_sli_ct_request *CTrsp;
1789
1790                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1791                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1792                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1793                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1794                         vport->ct_flags |= FC_CT_RFF_ID;
1795         }
1796         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1797         return;
1798 }
1799
1800 /*
1801  * Although the symbolic port name is thought to be an integer
1802  * as of January 18, 2016, leave it as a string until more of
1803  * the record state becomes defined.
1804  */
1805 int
1806 lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol,
1807         size_t size)
1808 {
1809         int n;
1810
1811         /*
1812          * Use the lpfc board number as the Symbolic Port
1813          * Name object.  NPIV is not in play so this integer
1814          * value is sufficient and unique per FC-ID.
1815          */
1816         n = scnprintf(symbol, size, "%d", vport->phba->brd_no);
1817         return n;
1818 }
1819
1820
1821 int
1822 lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol,
1823         size_t size)
1824 {
1825         char fwrev[FW_REV_STR_SIZE] = {0};
1826         char tmp[MAXHOSTNAMELEN] = {0};
1827
1828         memset(symbol, 0, size);
1829
1830         scnprintf(tmp, sizeof(tmp), "Emulex %s", vport->phba->ModelName);
1831         if (strlcat(symbol, tmp, size) >= size)
1832                 goto buffer_done;
1833
1834         lpfc_decode_firmware_rev(vport->phba, fwrev, 0);
1835         scnprintf(tmp, sizeof(tmp), " FV%s", fwrev);
1836         if (strlcat(symbol, tmp, size) >= size)
1837                 goto buffer_done;
1838
1839         scnprintf(tmp, sizeof(tmp), " DV%s", lpfc_release_version);
1840         if (strlcat(symbol, tmp, size) >= size)
1841                 goto buffer_done;
1842
1843         scnprintf(tmp, sizeof(tmp), " HN:%s", vport->phba->os_host_name);
1844         if (strlcat(symbol, tmp, size) >= size)
1845                 goto buffer_done;
1846
1847         /* Note :- OS name is "Linux" */
1848         scnprintf(tmp, sizeof(tmp), " OS:%s", init_utsname()->sysname);
1849         strlcat(symbol, tmp, size);
1850
1851 buffer_done:
1852         return strnlen(symbol, size);
1853
1854 }
1855
1856 static uint32_t
1857 lpfc_find_map_node(struct lpfc_vport *vport)
1858 {
1859         struct lpfc_nodelist *ndlp, *next_ndlp;
1860         struct Scsi_Host  *shost;
1861         uint32_t cnt = 0;
1862
1863         shost = lpfc_shost_from_vport(vport);
1864         spin_lock_irq(shost->host_lock);
1865         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1866                 if (ndlp->nlp_type & NLP_FABRIC)
1867                         continue;
1868                 if ((ndlp->nlp_state == NLP_STE_MAPPED_NODE) ||
1869                     (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE))
1870                         cnt++;
1871         }
1872         spin_unlock_irq(shost->host_lock);
1873         return cnt;
1874 }
1875
1876 /*
1877  * This routine will return the FC4 Type associated with the CT
1878  * GID_FT command.
1879  */
1880 int
1881 lpfc_get_gidft_type(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb)
1882 {
1883         struct lpfc_sli_ct_request *CtReq;
1884         struct lpfc_dmabuf *mp;
1885         uint32_t type;
1886
1887         mp = cmdiocb->context1;
1888         if (mp == NULL)
1889                 return 0;
1890         CtReq = (struct lpfc_sli_ct_request *)mp->virt;
1891         type = (uint32_t)CtReq->un.gid.Fc4Type;
1892         if ((type != SLI_CTPT_FCP) && (type != SLI_CTPT_NVME))
1893                 return 0;
1894         return type;
1895 }
1896
1897 /*
1898  * lpfc_ns_cmd
1899  * Description:
1900  *    Issue Cmd to NameServer
1901  *       SLI_CTNS_GID_FT
1902  *       LI_CTNS_RFT_ID
1903  */
1904 int
1905 lpfc_ns_cmd(struct lpfc_vport *vport, int cmdcode,
1906             uint8_t retry, uint32_t context)
1907 {
1908         struct lpfc_nodelist * ndlp;
1909         struct lpfc_hba *phba = vport->phba;
1910         struct lpfc_dmabuf *mp, *bmp;
1911         struct lpfc_sli_ct_request *CtReq;
1912         struct ulp_bde64 *bpl;
1913         void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
1914                       struct lpfc_iocbq *) = NULL;
1915         uint32_t *ptr;
1916         uint32_t rsp_size = 1024;
1917         size_t   size;
1918         int rc = 0;
1919
1920         ndlp = lpfc_findnode_did(vport, NameServer_DID);
1921         if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) {
1922                 rc=1;
1923                 goto ns_cmd_exit;
1924         }
1925
1926         /* fill in BDEs for command */
1927         /* Allocate buffer for command payload */
1928         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1929         if (!mp) {
1930                 rc=2;
1931                 goto ns_cmd_exit;
1932         }
1933
1934         INIT_LIST_HEAD(&mp->list);
1935         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
1936         if (!mp->virt) {
1937                 rc=3;
1938                 goto ns_cmd_free_mp;
1939         }
1940
1941         /* Allocate buffer for Buffer ptr list */
1942         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1943         if (!bmp) {
1944                 rc=4;
1945                 goto ns_cmd_free_mpvirt;
1946         }
1947
1948         INIT_LIST_HEAD(&bmp->list);
1949         bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(bmp->phys));
1950         if (!bmp->virt) {
1951                 rc=5;
1952                 goto ns_cmd_free_bmp;
1953         }
1954
1955         /* NameServer Req */
1956         lpfc_printf_vlog(vport, KERN_INFO ,LOG_DISCOVERY,
1957                          "0236 NameServer Req Data: x%x x%x x%x x%x\n",
1958                          cmdcode, vport->fc_flag, vport->fc_rscn_id_cnt,
1959                          context);
1960
1961         bpl = (struct ulp_bde64 *) bmp->virt;
1962         memset(bpl, 0, sizeof(struct ulp_bde64));
1963         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
1964         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
1965         bpl->tus.f.bdeFlags = 0;
1966         if (cmdcode == SLI_CTNS_GID_FT)
1967                 bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1968         else if (cmdcode == SLI_CTNS_GID_PT)
1969                 bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1970         else if (cmdcode == SLI_CTNS_GFF_ID)
1971                 bpl->tus.f.bdeSize = GFF_REQUEST_SZ;
1972         else if (cmdcode == SLI_CTNS_GFT_ID)
1973                 bpl->tus.f.bdeSize = GFT_REQUEST_SZ;
1974         else if (cmdcode == SLI_CTNS_RFT_ID)
1975                 bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
1976         else if (cmdcode == SLI_CTNS_RNN_ID)
1977                 bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
1978         else if (cmdcode == SLI_CTNS_RSPN_ID)
1979                 bpl->tus.f.bdeSize = RSPN_REQUEST_SZ;
1980         else if (cmdcode == SLI_CTNS_RSNN_NN)
1981                 bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
1982         else if (cmdcode == SLI_CTNS_DA_ID)
1983                 bpl->tus.f.bdeSize = DA_ID_REQUEST_SZ;
1984         else if (cmdcode == SLI_CTNS_RFF_ID)
1985                 bpl->tus.f.bdeSize = RFF_REQUEST_SZ;
1986         else
1987                 bpl->tus.f.bdeSize = 0;
1988         bpl->tus.w = le32_to_cpu(bpl->tus.w);
1989
1990         CtReq = (struct lpfc_sli_ct_request *) mp->virt;
1991         memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
1992         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
1993         CtReq->RevisionId.bits.InId = 0;
1994         CtReq->FsType = SLI_CT_DIRECTORY_SERVICE;
1995         CtReq->FsSubType = SLI_CT_DIRECTORY_NAME_SERVER;
1996         CtReq->CommandResponse.bits.Size = 0;
1997         switch (cmdcode) {
1998         case SLI_CTNS_GID_FT:
1999                 CtReq->CommandResponse.bits.CmdRsp =
2000                     cpu_to_be16(SLI_CTNS_GID_FT);
2001                 CtReq->un.gid.Fc4Type = context;
2002
2003                 if (vport->port_state < LPFC_NS_QRY)
2004                         vport->port_state = LPFC_NS_QRY;
2005                 lpfc_set_disctmo(vport);
2006                 cmpl = lpfc_cmpl_ct_cmd_gid_ft;
2007                 rsp_size = FC_MAX_NS_RSP;
2008                 break;
2009
2010         case SLI_CTNS_GID_PT:
2011                 CtReq->CommandResponse.bits.CmdRsp =
2012                     cpu_to_be16(SLI_CTNS_GID_PT);
2013                 CtReq->un.gid.PortType = context;
2014
2015                 if (vport->port_state < LPFC_NS_QRY)
2016                         vport->port_state = LPFC_NS_QRY;
2017                 lpfc_set_disctmo(vport);
2018                 cmpl = lpfc_cmpl_ct_cmd_gid_pt;
2019                 rsp_size = FC_MAX_NS_RSP;
2020                 break;
2021
2022         case SLI_CTNS_GFF_ID:
2023                 CtReq->CommandResponse.bits.CmdRsp =
2024                         cpu_to_be16(SLI_CTNS_GFF_ID);
2025                 CtReq->un.gff.PortId = cpu_to_be32(context);
2026                 cmpl = lpfc_cmpl_ct_cmd_gff_id;
2027                 break;
2028
2029         case SLI_CTNS_GFT_ID:
2030                 CtReq->CommandResponse.bits.CmdRsp =
2031                         cpu_to_be16(SLI_CTNS_GFT_ID);
2032                 CtReq->un.gft.PortId = cpu_to_be32(context);
2033                 cmpl = lpfc_cmpl_ct_cmd_gft_id;
2034                 break;
2035
2036         case SLI_CTNS_RFT_ID:
2037                 vport->ct_flags &= ~FC_CT_RFT_ID;
2038                 CtReq->CommandResponse.bits.CmdRsp =
2039                     cpu_to_be16(SLI_CTNS_RFT_ID);
2040                 CtReq->un.rft.PortId = cpu_to_be32(vport->fc_myDID);
2041
2042                 /* Register FC4 FCP type if enabled.  */
2043                 if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
2044                     vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)
2045                         CtReq->un.rft.fcpReg = 1;
2046
2047                 /* Register NVME type if enabled.  Defined LE and swapped.
2048                  * rsvd[0] is used as word1 because of the hard-coded
2049                  * word0 usage in the ct_request data structure.
2050                  */
2051                 if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
2052                     vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
2053                         CtReq->un.rft.rsvd[0] =
2054                                 cpu_to_be32(LPFC_FC4_TYPE_BITMASK);
2055
2056                 ptr = (uint32_t *)CtReq;
2057                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2058                                  "6433 Issue RFT (%s %s): %08x %08x %08x %08x "
2059                                  "%08x %08x %08x %08x\n",
2060                                  CtReq->un.rft.fcpReg ? "FCP" : " ",
2061                                  CtReq->un.rft.rsvd[0] ? "NVME" : " ",
2062                                  *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
2063                                  *(ptr + 4), *(ptr + 5),
2064                                  *(ptr + 6), *(ptr + 7));
2065                 cmpl = lpfc_cmpl_ct_cmd_rft_id;
2066                 break;
2067
2068         case SLI_CTNS_RNN_ID:
2069                 vport->ct_flags &= ~FC_CT_RNN_ID;
2070                 CtReq->CommandResponse.bits.CmdRsp =
2071                     cpu_to_be16(SLI_CTNS_RNN_ID);
2072                 CtReq->un.rnn.PortId = cpu_to_be32(vport->fc_myDID);
2073                 memcpy(CtReq->un.rnn.wwnn,  &vport->fc_nodename,
2074                        sizeof(struct lpfc_name));
2075                 cmpl = lpfc_cmpl_ct_cmd_rnn_id;
2076                 break;
2077
2078         case SLI_CTNS_RSPN_ID:
2079                 vport->ct_flags &= ~FC_CT_RSPN_ID;
2080                 CtReq->CommandResponse.bits.CmdRsp =
2081                     cpu_to_be16(SLI_CTNS_RSPN_ID);
2082                 CtReq->un.rspn.PortId = cpu_to_be32(vport->fc_myDID);
2083                 size = sizeof(CtReq->un.rspn.symbname);
2084                 CtReq->un.rspn.len =
2085                         lpfc_vport_symbolic_port_name(vport,
2086                         CtReq->un.rspn.symbname, size);
2087                 cmpl = lpfc_cmpl_ct_cmd_rspn_id;
2088                 break;
2089         case SLI_CTNS_RSNN_NN:
2090                 vport->ct_flags &= ~FC_CT_RSNN_NN;
2091                 CtReq->CommandResponse.bits.CmdRsp =
2092                     cpu_to_be16(SLI_CTNS_RSNN_NN);
2093                 memcpy(CtReq->un.rsnn.wwnn, &vport->fc_nodename,
2094                        sizeof(struct lpfc_name));
2095                 size = sizeof(CtReq->un.rsnn.symbname);
2096                 CtReq->un.rsnn.len =
2097                         lpfc_vport_symbolic_node_name(vport,
2098                         CtReq->un.rsnn.symbname, size);
2099                 cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
2100                 break;
2101         case SLI_CTNS_DA_ID:
2102                 /* Implement DA_ID Nameserver request */
2103                 CtReq->CommandResponse.bits.CmdRsp =
2104                         cpu_to_be16(SLI_CTNS_DA_ID);
2105                 CtReq->un.da_id.port_id = cpu_to_be32(vport->fc_myDID);
2106                 cmpl = lpfc_cmpl_ct_cmd_da_id;
2107                 break;
2108         case SLI_CTNS_RFF_ID:
2109                 vport->ct_flags &= ~FC_CT_RFF_ID;
2110                 CtReq->CommandResponse.bits.CmdRsp =
2111                     cpu_to_be16(SLI_CTNS_RFF_ID);
2112                 CtReq->un.rff.PortId = cpu_to_be32(vport->fc_myDID);
2113                 CtReq->un.rff.fbits = FC4_FEATURE_INIT;
2114
2115                 /* The driver always supports FC_TYPE_FCP.  However, the
2116                  * caller can specify NVME (type x28) as well.  But only
2117                  * these that FC4 type is supported.
2118                  */
2119                 if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
2120                      (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
2121                     (context == FC_TYPE_NVME)) {
2122                         if ((vport == phba->pport) && phba->nvmet_support) {
2123                                 CtReq->un.rff.fbits = (FC4_FEATURE_TARGET |
2124                                         FC4_FEATURE_NVME_DISC);
2125                                 lpfc_nvmet_update_targetport(phba);
2126                         } else {
2127                                 lpfc_nvme_update_localport(vport);
2128                         }
2129                         CtReq->un.rff.type_code = context;
2130
2131                 } else if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
2132                             (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) &&
2133                            (context == FC_TYPE_FCP))
2134                         CtReq->un.rff.type_code = context;
2135
2136                 else
2137                         goto ns_cmd_free_bmpvirt;
2138
2139                 ptr = (uint32_t *)CtReq;
2140                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2141                                  "6434 Issue RFF (%s): %08x %08x %08x %08x "
2142                                  "%08x %08x %08x %08x\n",
2143                                  (context == FC_TYPE_NVME) ? "NVME" : "FCP",
2144                                  *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
2145                                  *(ptr + 4), *(ptr + 5),
2146                                  *(ptr + 6), *(ptr + 7));
2147                 cmpl = lpfc_cmpl_ct_cmd_rff_id;
2148                 break;
2149         }
2150         /* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
2151          * to hold ndlp reference for the corresponding callback function.
2152          */
2153         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry)) {
2154                 /* On success, The cmpl function will free the buffers */
2155                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
2156                         "Issue CT cmd:    cmd:x%x did:x%x",
2157                         cmdcode, ndlp->nlp_DID, 0);
2158                 return 0;
2159         }
2160         rc=6;
2161
2162 ns_cmd_free_bmpvirt:
2163         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
2164 ns_cmd_free_bmp:
2165         kfree(bmp);
2166 ns_cmd_free_mpvirt:
2167         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2168 ns_cmd_free_mp:
2169         kfree(mp);
2170 ns_cmd_exit:
2171         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2172                          "0266 Issue NameServer Req x%x err %d Data: x%x x%x\n",
2173                          cmdcode, rc, vport->fc_flag, vport->fc_rscn_id_cnt);
2174         return 1;
2175 }
2176
2177 /**
2178  * lpfc_cmpl_ct_disc_fdmi - Handle a discovery FDMI completion
2179  * @phba: Pointer to HBA context object.
2180  * @cmdiocb: Pointer to the command IOCBQ.
2181  * @rspiocb: Pointer to the response IOCBQ.
2182  *
2183  * This function to handle the completion of a driver initiated FDMI
2184  * CT command issued during discovery.
2185  */
2186 static void
2187 lpfc_cmpl_ct_disc_fdmi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2188                        struct lpfc_iocbq *rspiocb)
2189 {
2190         struct lpfc_vport *vport = cmdiocb->vport;
2191         struct lpfc_dmabuf *inp = cmdiocb->context1;
2192         struct lpfc_dmabuf *outp = cmdiocb->context2;
2193         struct lpfc_sli_ct_request *CTcmd = inp->virt;
2194         struct lpfc_sli_ct_request *CTrsp = outp->virt;
2195         uint16_t fdmi_cmd = CTcmd->CommandResponse.bits.CmdRsp;
2196         uint16_t fdmi_rsp = CTrsp->CommandResponse.bits.CmdRsp;
2197         IOCB_t *irsp = &rspiocb->iocb;
2198         struct lpfc_nodelist *ndlp, *free_ndlp = NULL;
2199         uint32_t latt, cmd, err;
2200
2201         latt = lpfc_els_chk_latt(vport);
2202         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
2203                 "FDMI cmpl:       status:x%x/x%x latt:%d",
2204                 irsp->ulpStatus, irsp->un.ulpWord[4], latt);
2205
2206         if (latt || irsp->ulpStatus) {
2207
2208                 /* Look for a retryable error */
2209                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
2210                         switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
2211                         case IOERR_SLI_ABORTED:
2212                         case IOERR_SLI_DOWN:
2213                                 /* Driver aborted this IO.  No retry as error
2214                                  * is likely Offline->Online or some adapter
2215                                  * error.  Recovery will try again.
2216                                  */
2217                                 break;
2218                         case IOERR_ABORT_IN_PROGRESS:
2219                         case IOERR_SEQUENCE_TIMEOUT:
2220                         case IOERR_ILLEGAL_FRAME:
2221                         case IOERR_NO_RESOURCES:
2222                         case IOERR_ILLEGAL_COMMAND:
2223                                 cmdiocb->retry++;
2224                                 if (cmdiocb->retry >= LPFC_FDMI_MAX_RETRY)
2225                                         break;
2226
2227                                 /* Retry the same FDMI command */
2228                                 err = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING,
2229                                                           cmdiocb, 0);
2230                                 if (err == IOCB_ERROR)
2231                                         break;
2232                                 return;
2233                         default:
2234                                 break;
2235                         }
2236                 }
2237
2238                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2239                                  "0229 FDMI cmd %04x failed, latt = %d "
2240                                  "ulpStatus: x%x, rid x%x\n",
2241                                  be16_to_cpu(fdmi_cmd), latt, irsp->ulpStatus,
2242                                  irsp->un.ulpWord[4]);
2243         }
2244
2245         free_ndlp = cmdiocb->context_un.ndlp;
2246         lpfc_ct_free_iocb(phba, cmdiocb);
2247         lpfc_nlp_put(free_ndlp);
2248
2249         ndlp = lpfc_findnode_did(vport, FDMI_DID);
2250         if (!ndlp)
2251                 return;
2252
2253         /* Check for a CT LS_RJT response */
2254         cmd =  be16_to_cpu(fdmi_cmd);
2255         if (fdmi_rsp == cpu_to_be16(SLI_CT_RESPONSE_FS_RJT)) {
2256                 /* FDMI rsp failed */
2257                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2258                                  "0220 FDMI cmd failed FS_RJT Data: x%x", cmd);
2259
2260                 /* Should we fallback to FDMI-2 / FDMI-1 ? */
2261                 switch (cmd) {
2262                 case SLI_MGMT_RHBA:
2263                         if (vport->fdmi_hba_mask == LPFC_FDMI2_HBA_ATTR) {
2264                                 /* Fallback to FDMI-1 */
2265                                 vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
2266                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
2267                                 /* Start over */
2268                                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2269                         }
2270                         return;
2271
2272                 case SLI_MGMT_RPRT:
2273                         if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
2274                                 /* Fallback to FDMI-1 */
2275                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
2276                                 /* Start over */
2277                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
2278                                 return;
2279                         }
2280                         if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
2281                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
2282                                 /* Retry the same command */
2283                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
2284                         }
2285                         return;
2286
2287                 case SLI_MGMT_RPA:
2288                         /* No retry on Vendor, RPA only done on physical port */
2289                         if (phba->link_flag & LS_CT_VEN_RPA) {
2290                                 phba->link_flag &= ~LS_CT_VEN_RPA;
2291                                 if (phba->cmf_active_mode == LPFC_CFG_OFF)
2292                                         return;
2293                                 lpfc_printf_log(phba, KERN_ERR,
2294                                                 LOG_DISCOVERY | LOG_ELS,
2295                                                 "6460 VEN FDMI RPA failure\n");
2296                                 return;
2297                         }
2298                         if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
2299                                 /* Fallback to FDMI-1 */
2300                                 vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
2301                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
2302                                 /* Start over */
2303                                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2304                                 return;
2305                         }
2306                         if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
2307                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
2308                                 /* Retry the same command */
2309                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
2310                         }
2311                         return;
2312                 }
2313         }
2314
2315         /*
2316          * On success, need to cycle thru FDMI registration for discovery
2317          * DHBA -> DPRT -> RHBA -> RPA  (physical port)
2318          * DPRT -> RPRT (vports)
2319          */
2320         switch (cmd) {
2321         case SLI_MGMT_RHBA:
2322                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA, 0);
2323                 break;
2324
2325         case SLI_MGMT_DHBA:
2326                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
2327                 break;
2328
2329         case SLI_MGMT_DPRT:
2330                 if (vport->port_type == LPFC_PHYSICAL_PORT)
2331                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RHBA, 0);
2332                 else
2333                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT, 0);
2334                 break;
2335         case SLI_MGMT_RPA:
2336                 if (vport->port_type == LPFC_PHYSICAL_PORT &&
2337                     phba->sli4_hba.pc_sli4_params.mi_ver) {
2338                         /* mi is only for the phyical port, no vports */
2339                         if (phba->link_flag & LS_CT_VEN_RPA) {
2340                                 lpfc_printf_vlog(vport, KERN_INFO,
2341                                                  LOG_DISCOVERY | LOG_ELS |
2342                                                  LOG_CGN_MGMT,
2343                                                  "6449 VEN RPA FDMI Success\n");
2344                                 phba->link_flag &= ~LS_CT_VEN_RPA;
2345                                 break;
2346                         }
2347
2348                         lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
2349                                         "6210 Issue Vendor MI FDMI %x\n",
2350                                         phba->sli4_hba.pc_sli4_params.mi_ver);
2351
2352                         /* CGN is only for the physical port, no vports */
2353                         if (lpfc_fdmi_cmd(vport, ndlp, cmd,
2354                                           LPFC_FDMI_VENDOR_ATTR_mi) == 0)
2355                                 phba->link_flag |= LS_CT_VEN_RPA;
2356                         lpfc_printf_log(phba, KERN_INFO,
2357                                         LOG_DISCOVERY | LOG_ELS,
2358                                         "6458 Send MI FDMI:%x Flag x%x\n",
2359                                         phba->sli4_hba.pc_sli4_params.mi_ver,
2360                                         phba->link_flag);
2361                 } else {
2362                         lpfc_printf_log(phba, KERN_INFO,
2363                                         LOG_DISCOVERY | LOG_ELS,
2364                                         "6459 No FDMI VEN MI support - "
2365                                         "RPA Success\n");
2366                 }
2367                 break;
2368         }
2369         return;
2370 }
2371
2372
2373 /**
2374  * lpfc_fdmi_change_check - Check for changed FDMI parameters
2375  * @vport: pointer to a host virtual N_Port data structure.
2376  *
2377  * Check how many mapped NPorts we are connected to
2378  * Check if our hostname changed
2379  * Called from hbeat timeout routine to check if any FDMI parameters
2380  * changed. If so, re-register those Attributes.
2381  */
2382 void
2383 lpfc_fdmi_change_check(struct lpfc_vport *vport)
2384 {
2385         struct lpfc_hba *phba = vport->phba;
2386         struct lpfc_nodelist *ndlp;
2387         uint16_t cnt;
2388
2389         if (!lpfc_is_link_up(phba))
2390                 return;
2391
2392         /* Must be connected to a Fabric */
2393         if (!(vport->fc_flag & FC_FABRIC))
2394                 return;
2395
2396         ndlp = lpfc_findnode_did(vport, FDMI_DID);
2397         if (!ndlp)
2398                 return;
2399
2400         /* Check if system hostname changed */
2401         if (strcmp(phba->os_host_name, init_utsname()->nodename)) {
2402                 memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
2403                 scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
2404                           init_utsname()->nodename);
2405                 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2406
2407                 /* Since this effects multiple HBA and PORT attributes, we need
2408                  * de-register and go thru the whole FDMI registration cycle.
2409                  * DHBA -> DPRT -> RHBA -> RPA  (physical port)
2410                  * DPRT -> RPRT (vports)
2411                  */
2412                 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2413                         /* For extra Vendor RPA */
2414                         phba->link_flag &= ~LS_CT_VEN_RPA;
2415                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2416                 } else {
2417                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
2418                 }
2419
2420                 /* Since this code path registers all the port attributes
2421                  * we can just return without further checking.
2422                  */
2423                 return;
2424         }
2425
2426         if (!(vport->fdmi_port_mask & LPFC_FDMI_PORT_ATTR_num_disc))
2427                 return;
2428
2429         /* Check if the number of mapped NPorts changed */
2430         cnt = lpfc_find_map_node(vport);
2431         if (cnt == vport->fdmi_num_disc)
2432                 return;
2433
2434         if (vport->port_type == LPFC_PHYSICAL_PORT) {
2435                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA,
2436                               LPFC_FDMI_PORT_ATTR_num_disc);
2437         } else {
2438                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT,
2439                               LPFC_FDMI_PORT_ATTR_num_disc);
2440         }
2441 }
2442
2443 /* Routines for all individual HBA attributes */
2444 static int
2445 lpfc_fdmi_hba_attr_wwnn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2446 {
2447         struct lpfc_fdmi_attr_entry *ae;
2448         uint32_t size;
2449
2450         ae = &ad->AttrValue;
2451         memset(ae, 0, sizeof(*ae));
2452
2453         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
2454                sizeof(struct lpfc_name));
2455         size = FOURBYTES + sizeof(struct lpfc_name);
2456         ad->AttrLen = cpu_to_be16(size);
2457         ad->AttrType = cpu_to_be16(RHBA_NODENAME);
2458         return size;
2459 }
2460 static int
2461 lpfc_fdmi_hba_attr_manufacturer(struct lpfc_vport *vport,
2462                                 struct lpfc_fdmi_attr_def *ad)
2463 {
2464         struct lpfc_fdmi_attr_entry *ae;
2465         uint32_t len, size;
2466
2467         ae = &ad->AttrValue;
2468         memset(ae, 0, sizeof(*ae));
2469
2470         /* This string MUST be consistent with other FC platforms
2471          * supported by Broadcom.
2472          */
2473         strncpy(ae->un.AttrString,
2474                 "Emulex Corporation",
2475                        sizeof(ae->un.AttrString));
2476         len = strnlen(ae->un.AttrString,
2477                           sizeof(ae->un.AttrString));
2478         len += (len & 3) ? (4 - (len & 3)) : 4;
2479         size = FOURBYTES + len;
2480         ad->AttrLen = cpu_to_be16(size);
2481         ad->AttrType = cpu_to_be16(RHBA_MANUFACTURER);
2482         return size;
2483 }
2484
2485 static int
2486 lpfc_fdmi_hba_attr_sn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2487 {
2488         struct lpfc_hba *phba = vport->phba;
2489         struct lpfc_fdmi_attr_entry *ae;
2490         uint32_t len, size;
2491
2492         ae = &ad->AttrValue;
2493         memset(ae, 0, sizeof(*ae));
2494
2495         strncpy(ae->un.AttrString, phba->SerialNumber,
2496                 sizeof(ae->un.AttrString));
2497         len = strnlen(ae->un.AttrString,
2498                           sizeof(ae->un.AttrString));
2499         len += (len & 3) ? (4 - (len & 3)) : 4;
2500         size = FOURBYTES + len;
2501         ad->AttrLen = cpu_to_be16(size);
2502         ad->AttrType = cpu_to_be16(RHBA_SERIAL_NUMBER);
2503         return size;
2504 }
2505
2506 static int
2507 lpfc_fdmi_hba_attr_model(struct lpfc_vport *vport,
2508                          struct lpfc_fdmi_attr_def *ad)
2509 {
2510         struct lpfc_hba *phba = vport->phba;
2511         struct lpfc_fdmi_attr_entry *ae;
2512         uint32_t len, size;
2513
2514         ae = &ad->AttrValue;
2515         memset(ae, 0, sizeof(*ae));
2516
2517         strncpy(ae->un.AttrString, phba->ModelName,
2518                 sizeof(ae->un.AttrString));
2519         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2520         len += (len & 3) ? (4 - (len & 3)) : 4;
2521         size = FOURBYTES + len;
2522         ad->AttrLen = cpu_to_be16(size);
2523         ad->AttrType = cpu_to_be16(RHBA_MODEL);
2524         return size;
2525 }
2526
2527 static int
2528 lpfc_fdmi_hba_attr_description(struct lpfc_vport *vport,
2529                                struct lpfc_fdmi_attr_def *ad)
2530 {
2531         struct lpfc_hba *phba = vport->phba;
2532         struct lpfc_fdmi_attr_entry *ae;
2533         uint32_t len, size;
2534
2535         ae = &ad->AttrValue;
2536         memset(ae, 0, sizeof(*ae));
2537
2538         strncpy(ae->un.AttrString, phba->ModelDesc,
2539                 sizeof(ae->un.AttrString));
2540         len = strnlen(ae->un.AttrString,
2541                                   sizeof(ae->un.AttrString));
2542         len += (len & 3) ? (4 - (len & 3)) : 4;
2543         size = FOURBYTES + len;
2544         ad->AttrLen = cpu_to_be16(size);
2545         ad->AttrType = cpu_to_be16(RHBA_MODEL_DESCRIPTION);
2546         return size;
2547 }
2548
2549 static int
2550 lpfc_fdmi_hba_attr_hdw_ver(struct lpfc_vport *vport,
2551                            struct lpfc_fdmi_attr_def *ad)
2552 {
2553         struct lpfc_hba *phba = vport->phba;
2554         lpfc_vpd_t *vp = &phba->vpd;
2555         struct lpfc_fdmi_attr_entry *ae;
2556         uint32_t i, j, incr, size;
2557
2558         ae = &ad->AttrValue;
2559         memset(ae, 0, sizeof(*ae));
2560
2561         /* Convert JEDEC ID to ascii for hardware version */
2562         incr = vp->rev.biuRev;
2563         for (i = 0; i < 8; i++) {
2564                 j = (incr & 0xf);
2565                 if (j <= 9)
2566                         ae->un.AttrString[7 - i] =
2567                             (char)((uint8_t) 0x30 +
2568                                    (uint8_t) j);
2569                 else
2570                         ae->un.AttrString[7 - i] =
2571                             (char)((uint8_t) 0x61 +
2572                                    (uint8_t) (j - 10));
2573                 incr = (incr >> 4);
2574         }
2575         size = FOURBYTES + 8;
2576         ad->AttrLen = cpu_to_be16(size);
2577         ad->AttrType = cpu_to_be16(RHBA_HARDWARE_VERSION);
2578         return size;
2579 }
2580
2581 static int
2582 lpfc_fdmi_hba_attr_drvr_ver(struct lpfc_vport *vport,
2583                             struct lpfc_fdmi_attr_def *ad)
2584 {
2585         struct lpfc_fdmi_attr_entry *ae;
2586         uint32_t len, size;
2587
2588         ae = &ad->AttrValue;
2589         memset(ae, 0, sizeof(*ae));
2590
2591         strncpy(ae->un.AttrString, lpfc_release_version,
2592                 sizeof(ae->un.AttrString));
2593         len = strnlen(ae->un.AttrString,
2594                           sizeof(ae->un.AttrString));
2595         len += (len & 3) ? (4 - (len & 3)) : 4;
2596         size = FOURBYTES + len;
2597         ad->AttrLen = cpu_to_be16(size);
2598         ad->AttrType = cpu_to_be16(RHBA_DRIVER_VERSION);
2599         return size;
2600 }
2601
2602 static int
2603 lpfc_fdmi_hba_attr_rom_ver(struct lpfc_vport *vport,
2604                            struct lpfc_fdmi_attr_def *ad)
2605 {
2606         struct lpfc_hba *phba = vport->phba;
2607         struct lpfc_fdmi_attr_entry *ae;
2608         uint32_t len, size;
2609
2610         ae = &ad->AttrValue;
2611         memset(ae, 0, sizeof(*ae));
2612
2613         if (phba->sli_rev == LPFC_SLI_REV4)
2614                 lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2615         else
2616                 strncpy(ae->un.AttrString, phba->OptionROMVersion,
2617                         sizeof(ae->un.AttrString));
2618         len = strnlen(ae->un.AttrString,
2619                           sizeof(ae->un.AttrString));
2620         len += (len & 3) ? (4 - (len & 3)) : 4;
2621         size = FOURBYTES + len;
2622         ad->AttrLen = cpu_to_be16(size);
2623         ad->AttrType = cpu_to_be16(RHBA_OPTION_ROM_VERSION);
2624         return size;
2625 }
2626
2627 static int
2628 lpfc_fdmi_hba_attr_fmw_ver(struct lpfc_vport *vport,
2629                            struct lpfc_fdmi_attr_def *ad)
2630 {
2631         struct lpfc_hba *phba = vport->phba;
2632         struct lpfc_fdmi_attr_entry *ae;
2633         uint32_t len, size;
2634
2635         ae = &ad->AttrValue;
2636         memset(ae, 0, sizeof(*ae));
2637
2638         lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2639         len = strnlen(ae->un.AttrString,
2640                           sizeof(ae->un.AttrString));
2641         len += (len & 3) ? (4 - (len & 3)) : 4;
2642         size = FOURBYTES + len;
2643         ad->AttrLen = cpu_to_be16(size);
2644         ad->AttrType = cpu_to_be16(RHBA_FIRMWARE_VERSION);
2645         return size;
2646 }
2647
2648 static int
2649 lpfc_fdmi_hba_attr_os_ver(struct lpfc_vport *vport,
2650                           struct lpfc_fdmi_attr_def *ad)
2651 {
2652         struct lpfc_fdmi_attr_entry *ae;
2653         uint32_t len, size;
2654
2655         ae = &ad->AttrValue;
2656         memset(ae, 0, sizeof(*ae));
2657
2658         snprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s %s %s",
2659                  init_utsname()->sysname,
2660                  init_utsname()->release,
2661                  init_utsname()->version);
2662
2663         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2664         len += (len & 3) ? (4 - (len & 3)) : 4;
2665         size = FOURBYTES + len;
2666         ad->AttrLen = cpu_to_be16(size);
2667         ad->AttrType = cpu_to_be16(RHBA_OS_NAME_VERSION);
2668         return size;
2669 }
2670
2671 static int
2672 lpfc_fdmi_hba_attr_ct_len(struct lpfc_vport *vport,
2673                           struct lpfc_fdmi_attr_def *ad)
2674 {
2675         struct lpfc_fdmi_attr_entry *ae;
2676         uint32_t size;
2677
2678         ae = &ad->AttrValue;
2679
2680         ae->un.AttrInt =  cpu_to_be32(LPFC_MAX_CT_SIZE);
2681         size = FOURBYTES + sizeof(uint32_t);
2682         ad->AttrLen = cpu_to_be16(size);
2683         ad->AttrType = cpu_to_be16(RHBA_MAX_CT_PAYLOAD_LEN);
2684         return size;
2685 }
2686
2687 static int
2688 lpfc_fdmi_hba_attr_symbolic_name(struct lpfc_vport *vport,
2689                                  struct lpfc_fdmi_attr_def *ad)
2690 {
2691         struct lpfc_fdmi_attr_entry *ae;
2692         uint32_t len, size;
2693
2694         ae = &ad->AttrValue;
2695         memset(ae, 0, sizeof(*ae));
2696
2697         len = lpfc_vport_symbolic_node_name(vport,
2698                                 ae->un.AttrString, 256);
2699         len += (len & 3) ? (4 - (len & 3)) : 4;
2700         size = FOURBYTES + len;
2701         ad->AttrLen = cpu_to_be16(size);
2702         ad->AttrType = cpu_to_be16(RHBA_SYM_NODENAME);
2703         return size;
2704 }
2705
2706 static int
2707 lpfc_fdmi_hba_attr_vendor_info(struct lpfc_vport *vport,
2708                                struct lpfc_fdmi_attr_def *ad)
2709 {
2710         struct lpfc_fdmi_attr_entry *ae;
2711         uint32_t size;
2712
2713         ae = &ad->AttrValue;
2714
2715         /* Nothing is defined for this currently */
2716         ae->un.AttrInt =  cpu_to_be32(0);
2717         size = FOURBYTES + sizeof(uint32_t);
2718         ad->AttrLen = cpu_to_be16(size);
2719         ad->AttrType = cpu_to_be16(RHBA_VENDOR_INFO);
2720         return size;
2721 }
2722
2723 static int
2724 lpfc_fdmi_hba_attr_num_ports(struct lpfc_vport *vport,
2725                              struct lpfc_fdmi_attr_def *ad)
2726 {
2727         struct lpfc_fdmi_attr_entry *ae;
2728         uint32_t size;
2729
2730         ae = &ad->AttrValue;
2731
2732         /* Each driver instance corresponds to a single port */
2733         ae->un.AttrInt =  cpu_to_be32(1);
2734         size = FOURBYTES + sizeof(uint32_t);
2735         ad->AttrLen = cpu_to_be16(size);
2736         ad->AttrType = cpu_to_be16(RHBA_NUM_PORTS);
2737         return size;
2738 }
2739
2740 static int
2741 lpfc_fdmi_hba_attr_fabric_wwnn(struct lpfc_vport *vport,
2742                                struct lpfc_fdmi_attr_def *ad)
2743 {
2744         struct lpfc_fdmi_attr_entry *ae;
2745         uint32_t size;
2746
2747         ae = &ad->AttrValue;
2748         memset(ae, 0, sizeof(*ae));
2749
2750         memcpy(&ae->un.AttrWWN, &vport->fabric_nodename,
2751                sizeof(struct lpfc_name));
2752         size = FOURBYTES + sizeof(struct lpfc_name);
2753         ad->AttrLen = cpu_to_be16(size);
2754         ad->AttrType = cpu_to_be16(RHBA_FABRIC_WWNN);
2755         return size;
2756 }
2757
2758 static int
2759 lpfc_fdmi_hba_attr_bios_ver(struct lpfc_vport *vport,
2760                             struct lpfc_fdmi_attr_def *ad)
2761 {
2762         struct lpfc_hba *phba = vport->phba;
2763         struct lpfc_fdmi_attr_entry *ae;
2764         uint32_t len, size;
2765
2766         ae = &ad->AttrValue;
2767         memset(ae, 0, sizeof(*ae));
2768
2769         strlcat(ae->un.AttrString, phba->BIOSVersion,
2770                 sizeof(ae->un.AttrString));
2771         len = strnlen(ae->un.AttrString,
2772                           sizeof(ae->un.AttrString));
2773         len += (len & 3) ? (4 - (len & 3)) : 4;
2774         size = FOURBYTES + len;
2775         ad->AttrLen = cpu_to_be16(size);
2776         ad->AttrType = cpu_to_be16(RHBA_BIOS_VERSION);
2777         return size;
2778 }
2779
2780 static int
2781 lpfc_fdmi_hba_attr_bios_state(struct lpfc_vport *vport,
2782                               struct lpfc_fdmi_attr_def *ad)
2783 {
2784         struct lpfc_fdmi_attr_entry *ae;
2785         uint32_t size;
2786
2787         ae = &ad->AttrValue;
2788
2789         /* Driver doesn't have access to this information */
2790         ae->un.AttrInt =  cpu_to_be32(0);
2791         size = FOURBYTES + sizeof(uint32_t);
2792         ad->AttrLen = cpu_to_be16(size);
2793         ad->AttrType = cpu_to_be16(RHBA_BIOS_STATE);
2794         return size;
2795 }
2796
2797 static int
2798 lpfc_fdmi_hba_attr_vendor_id(struct lpfc_vport *vport,
2799                              struct lpfc_fdmi_attr_def *ad)
2800 {
2801         struct lpfc_fdmi_attr_entry *ae;
2802         uint32_t len, size;
2803
2804         ae = &ad->AttrValue;
2805         memset(ae, 0, sizeof(*ae));
2806
2807         strncpy(ae->un.AttrString, "EMULEX",
2808                 sizeof(ae->un.AttrString));
2809         len = strnlen(ae->un.AttrString,
2810                           sizeof(ae->un.AttrString));
2811         len += (len & 3) ? (4 - (len & 3)) : 4;
2812         size = FOURBYTES + len;
2813         ad->AttrLen = cpu_to_be16(size);
2814         ad->AttrType = cpu_to_be16(RHBA_VENDOR_ID);
2815         return size;
2816 }
2817
2818 /* Routines for all individual PORT attributes */
2819 static int
2820 lpfc_fdmi_port_attr_fc4type(struct lpfc_vport *vport,
2821                             struct lpfc_fdmi_attr_def *ad)
2822 {
2823         struct lpfc_hba   *phba = vport->phba;
2824         struct lpfc_fdmi_attr_entry *ae;
2825         uint32_t size;
2826
2827         ae = &ad->AttrValue;
2828         memset(ae, 0, sizeof(*ae));
2829
2830         ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
2831         ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
2832
2833         /* Check to see if Firmware supports NVME and on physical port */
2834         if ((phba->sli_rev == LPFC_SLI_REV4) && (vport == phba->pport) &&
2835             phba->sli4_hba.pc_sli4_params.nvme)
2836                 ae->un.AttrTypes[6] = 0x01; /* Type 0x28 - NVME */
2837
2838         size = FOURBYTES + 32;
2839         ad->AttrLen = cpu_to_be16(size);
2840         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_FC4_TYPES);
2841         return size;
2842 }
2843
2844 static int
2845 lpfc_fdmi_port_attr_support_speed(struct lpfc_vport *vport,
2846                                   struct lpfc_fdmi_attr_def *ad)
2847 {
2848         struct lpfc_hba   *phba = vport->phba;
2849         struct lpfc_fdmi_attr_entry *ae;
2850         uint32_t size;
2851
2852         ae = &ad->AttrValue;
2853
2854         ae->un.AttrInt = 0;
2855         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2856                 if (phba->lmt & LMT_256Gb)
2857                         ae->un.AttrInt |= HBA_PORTSPEED_256GFC;
2858                 if (phba->lmt & LMT_128Gb)
2859                         ae->un.AttrInt |= HBA_PORTSPEED_128GFC;
2860                 if (phba->lmt & LMT_64Gb)
2861                         ae->un.AttrInt |= HBA_PORTSPEED_64GFC;
2862                 if (phba->lmt & LMT_32Gb)
2863                         ae->un.AttrInt |= HBA_PORTSPEED_32GFC;
2864                 if (phba->lmt & LMT_16Gb)
2865                         ae->un.AttrInt |= HBA_PORTSPEED_16GFC;
2866                 if (phba->lmt & LMT_10Gb)
2867                         ae->un.AttrInt |= HBA_PORTSPEED_10GFC;
2868                 if (phba->lmt & LMT_8Gb)
2869                         ae->un.AttrInt |= HBA_PORTSPEED_8GFC;
2870                 if (phba->lmt & LMT_4Gb)
2871                         ae->un.AttrInt |= HBA_PORTSPEED_4GFC;
2872                 if (phba->lmt & LMT_2Gb)
2873                         ae->un.AttrInt |= HBA_PORTSPEED_2GFC;
2874                 if (phba->lmt & LMT_1Gb)
2875                         ae->un.AttrInt |= HBA_PORTSPEED_1GFC;
2876         } else {
2877                 /* FCoE links support only one speed */
2878                 switch (phba->fc_linkspeed) {
2879                 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2880                         ae->un.AttrInt = HBA_PORTSPEED_10GE;
2881                         break;
2882                 case LPFC_ASYNC_LINK_SPEED_25GBPS:
2883                         ae->un.AttrInt = HBA_PORTSPEED_25GE;
2884                         break;
2885                 case LPFC_ASYNC_LINK_SPEED_40GBPS:
2886                         ae->un.AttrInt = HBA_PORTSPEED_40GE;
2887                         break;
2888                 case LPFC_ASYNC_LINK_SPEED_100GBPS:
2889                         ae->un.AttrInt = HBA_PORTSPEED_100GE;
2890                         break;
2891                 }
2892         }
2893         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2894         size = FOURBYTES + sizeof(uint32_t);
2895         ad->AttrLen = cpu_to_be16(size);
2896         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_SPEED);
2897         return size;
2898 }
2899
2900 static int
2901 lpfc_fdmi_port_attr_speed(struct lpfc_vport *vport,
2902                           struct lpfc_fdmi_attr_def *ad)
2903 {
2904         struct lpfc_hba   *phba = vport->phba;
2905         struct lpfc_fdmi_attr_entry *ae;
2906         uint32_t size;
2907
2908         ae = &ad->AttrValue;
2909
2910         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2911                 switch (phba->fc_linkspeed) {
2912                 case LPFC_LINK_SPEED_1GHZ:
2913                         ae->un.AttrInt = HBA_PORTSPEED_1GFC;
2914                         break;
2915                 case LPFC_LINK_SPEED_2GHZ:
2916                         ae->un.AttrInt = HBA_PORTSPEED_2GFC;
2917                         break;
2918                 case LPFC_LINK_SPEED_4GHZ:
2919                         ae->un.AttrInt = HBA_PORTSPEED_4GFC;
2920                         break;
2921                 case LPFC_LINK_SPEED_8GHZ:
2922                         ae->un.AttrInt = HBA_PORTSPEED_8GFC;
2923                         break;
2924                 case LPFC_LINK_SPEED_10GHZ:
2925                         ae->un.AttrInt = HBA_PORTSPEED_10GFC;
2926                         break;
2927                 case LPFC_LINK_SPEED_16GHZ:
2928                         ae->un.AttrInt = HBA_PORTSPEED_16GFC;
2929                         break;
2930                 case LPFC_LINK_SPEED_32GHZ:
2931                         ae->un.AttrInt = HBA_PORTSPEED_32GFC;
2932                         break;
2933                 case LPFC_LINK_SPEED_64GHZ:
2934                         ae->un.AttrInt = HBA_PORTSPEED_64GFC;
2935                         break;
2936                 case LPFC_LINK_SPEED_128GHZ:
2937                         ae->un.AttrInt = HBA_PORTSPEED_128GFC;
2938                         break;
2939                 case LPFC_LINK_SPEED_256GHZ:
2940                         ae->un.AttrInt = HBA_PORTSPEED_256GFC;
2941                         break;
2942                 default:
2943                         ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2944                         break;
2945                 }
2946         } else {
2947                 switch (phba->fc_linkspeed) {
2948                 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2949                         ae->un.AttrInt = HBA_PORTSPEED_10GE;
2950                         break;
2951                 case LPFC_ASYNC_LINK_SPEED_25GBPS:
2952                         ae->un.AttrInt = HBA_PORTSPEED_25GE;
2953                         break;
2954                 case LPFC_ASYNC_LINK_SPEED_40GBPS:
2955                         ae->un.AttrInt = HBA_PORTSPEED_40GE;
2956                         break;
2957                 case LPFC_ASYNC_LINK_SPEED_100GBPS:
2958                         ae->un.AttrInt = HBA_PORTSPEED_100GE;
2959                         break;
2960                 default:
2961                         ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2962                         break;
2963                 }
2964         }
2965
2966         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2967         size = FOURBYTES + sizeof(uint32_t);
2968         ad->AttrLen = cpu_to_be16(size);
2969         ad->AttrType = cpu_to_be16(RPRT_PORT_SPEED);
2970         return size;
2971 }
2972
2973 static int
2974 lpfc_fdmi_port_attr_max_frame(struct lpfc_vport *vport,
2975                               struct lpfc_fdmi_attr_def *ad)
2976 {
2977         struct serv_parm *hsp;
2978         struct lpfc_fdmi_attr_entry *ae;
2979         uint32_t size;
2980
2981         ae = &ad->AttrValue;
2982
2983         hsp = (struct serv_parm *)&vport->fc_sparam;
2984         ae->un.AttrInt = (((uint32_t) hsp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
2985                           (uint32_t) hsp->cmn.bbRcvSizeLsb;
2986         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2987         size = FOURBYTES + sizeof(uint32_t);
2988         ad->AttrLen = cpu_to_be16(size);
2989         ad->AttrType = cpu_to_be16(RPRT_MAX_FRAME_SIZE);
2990         return size;
2991 }
2992
2993 static int
2994 lpfc_fdmi_port_attr_os_devname(struct lpfc_vport *vport,
2995                                struct lpfc_fdmi_attr_def *ad)
2996 {
2997         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2998         struct lpfc_fdmi_attr_entry *ae;
2999         uint32_t len, size;
3000
3001         ae = &ad->AttrValue;
3002         memset(ae, 0, sizeof(*ae));
3003
3004         snprintf(ae->un.AttrString, sizeof(ae->un.AttrString),
3005                  "/sys/class/scsi_host/host%d", shost->host_no);
3006         len = strnlen((char *)ae->un.AttrString,
3007                           sizeof(ae->un.AttrString));
3008         len += (len & 3) ? (4 - (len & 3)) : 4;
3009         size = FOURBYTES + len;
3010         ad->AttrLen = cpu_to_be16(size);
3011         ad->AttrType = cpu_to_be16(RPRT_OS_DEVICE_NAME);
3012         return size;
3013 }
3014
3015 static int
3016 lpfc_fdmi_port_attr_host_name(struct lpfc_vport *vport,
3017                               struct lpfc_fdmi_attr_def *ad)
3018 {
3019         struct lpfc_fdmi_attr_entry *ae;
3020         uint32_t len, size;
3021
3022         ae = &ad->AttrValue;
3023         memset(ae, 0, sizeof(*ae));
3024
3025         scnprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s",
3026                   vport->phba->os_host_name);
3027
3028         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
3029         len += (len & 3) ? (4 - (len & 3)) : 4;
3030         size = FOURBYTES + len;
3031         ad->AttrLen = cpu_to_be16(size);
3032         ad->AttrType = cpu_to_be16(RPRT_HOST_NAME);
3033         return size;
3034 }
3035
3036 static int
3037 lpfc_fdmi_port_attr_wwnn(struct lpfc_vport *vport,
3038                          struct lpfc_fdmi_attr_def *ad)
3039 {
3040         struct lpfc_fdmi_attr_entry *ae;
3041         uint32_t size;
3042
3043         ae = &ad->AttrValue;
3044         memset(ae, 0, sizeof(*ae));
3045
3046         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
3047                sizeof(struct lpfc_name));
3048         size = FOURBYTES + sizeof(struct lpfc_name);
3049         ad->AttrLen = cpu_to_be16(size);
3050         ad->AttrType = cpu_to_be16(RPRT_NODENAME);
3051         return size;
3052 }
3053
3054 static int
3055 lpfc_fdmi_port_attr_wwpn(struct lpfc_vport *vport,
3056                          struct lpfc_fdmi_attr_def *ad)
3057 {
3058         struct lpfc_fdmi_attr_entry *ae;
3059         uint32_t size;
3060
3061         ae = &ad->AttrValue;
3062         memset(ae, 0, sizeof(*ae));
3063
3064         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.portName,
3065                sizeof(struct lpfc_name));
3066         size = FOURBYTES + sizeof(struct lpfc_name);
3067         ad->AttrLen = cpu_to_be16(size);
3068         ad->AttrType = cpu_to_be16(RPRT_PORTNAME);
3069         return size;
3070 }
3071
3072 static int
3073 lpfc_fdmi_port_attr_symbolic_name(struct lpfc_vport *vport,
3074                                   struct lpfc_fdmi_attr_def *ad)
3075 {
3076         struct lpfc_fdmi_attr_entry *ae;
3077         uint32_t len, size;
3078
3079         ae = &ad->AttrValue;
3080         memset(ae, 0, sizeof(*ae));
3081
3082         len = lpfc_vport_symbolic_port_name(vport, ae->un.AttrString, 256);
3083         len += (len & 3) ? (4 - (len & 3)) : 4;
3084         size = FOURBYTES + len;
3085         ad->AttrLen = cpu_to_be16(size);
3086         ad->AttrType = cpu_to_be16(RPRT_SYM_PORTNAME);
3087         return size;
3088 }
3089
3090 static int
3091 lpfc_fdmi_port_attr_port_type(struct lpfc_vport *vport,
3092                               struct lpfc_fdmi_attr_def *ad)
3093 {
3094         struct lpfc_hba *phba = vport->phba;
3095         struct lpfc_fdmi_attr_entry *ae;
3096         uint32_t size;
3097
3098         ae = &ad->AttrValue;
3099         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP)
3100                 ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NLPORT);
3101         else
3102                 ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NPORT);
3103         size = FOURBYTES + sizeof(uint32_t);
3104         ad->AttrLen = cpu_to_be16(size);
3105         ad->AttrType = cpu_to_be16(RPRT_PORT_TYPE);
3106         return size;
3107 }
3108
3109 static int
3110 lpfc_fdmi_port_attr_class(struct lpfc_vport *vport,
3111                           struct lpfc_fdmi_attr_def *ad)
3112 {
3113         struct lpfc_fdmi_attr_entry *ae;
3114         uint32_t size;
3115
3116         ae = &ad->AttrValue;
3117         ae->un.AttrInt = cpu_to_be32(FC_COS_CLASS2 | FC_COS_CLASS3);
3118         size = FOURBYTES + sizeof(uint32_t);
3119         ad->AttrLen = cpu_to_be16(size);
3120         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_CLASS);
3121         return size;
3122 }
3123
3124 static int
3125 lpfc_fdmi_port_attr_fabric_wwpn(struct lpfc_vport *vport,
3126                                 struct lpfc_fdmi_attr_def *ad)
3127 {
3128         struct lpfc_fdmi_attr_entry *ae;
3129         uint32_t size;
3130
3131         ae = &ad->AttrValue;
3132         memset(ae, 0, sizeof(*ae));
3133
3134         memcpy(&ae->un.AttrWWN, &vport->fabric_portname,
3135                sizeof(struct lpfc_name));
3136         size = FOURBYTES + sizeof(struct lpfc_name);
3137         ad->AttrLen = cpu_to_be16(size);
3138         ad->AttrType = cpu_to_be16(RPRT_FABRICNAME);
3139         return size;
3140 }
3141
3142 static int
3143 lpfc_fdmi_port_attr_active_fc4type(struct lpfc_vport *vport,
3144                                    struct lpfc_fdmi_attr_def *ad)
3145 {
3146         struct lpfc_fdmi_attr_entry *ae;
3147         uint32_t size;
3148
3149         ae = &ad->AttrValue;
3150         memset(ae, 0, sizeof(*ae));
3151
3152         ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
3153         ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
3154
3155         /* Check to see if NVME is configured or not */
3156         if (vport->phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3157                 ae->un.AttrTypes[6] = 0x1; /* Type 0x28 - NVME */
3158
3159         size = FOURBYTES + 32;
3160         ad->AttrLen = cpu_to_be16(size);
3161         ad->AttrType = cpu_to_be16(RPRT_ACTIVE_FC4_TYPES);
3162         return size;
3163 }
3164
3165 static int
3166 lpfc_fdmi_port_attr_port_state(struct lpfc_vport *vport,
3167                                struct lpfc_fdmi_attr_def *ad)
3168 {
3169         struct lpfc_fdmi_attr_entry *ae;
3170         uint32_t size;
3171
3172         ae = &ad->AttrValue;
3173         /* Link Up - operational */
3174         ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTSTATE_ONLINE);
3175         size = FOURBYTES + sizeof(uint32_t);
3176         ad->AttrLen = cpu_to_be16(size);
3177         ad->AttrType = cpu_to_be16(RPRT_PORT_STATE);
3178         return size;
3179 }
3180
3181 static int
3182 lpfc_fdmi_port_attr_num_disc(struct lpfc_vport *vport,
3183                              struct lpfc_fdmi_attr_def *ad)
3184 {
3185         struct lpfc_fdmi_attr_entry *ae;
3186         uint32_t size;
3187
3188         ae = &ad->AttrValue;
3189         vport->fdmi_num_disc = lpfc_find_map_node(vport);
3190         ae->un.AttrInt = cpu_to_be32(vport->fdmi_num_disc);
3191         size = FOURBYTES + sizeof(uint32_t);
3192         ad->AttrLen = cpu_to_be16(size);
3193         ad->AttrType = cpu_to_be16(RPRT_DISC_PORT);
3194         return size;
3195 }
3196
3197 static int
3198 lpfc_fdmi_port_attr_nportid(struct lpfc_vport *vport,
3199                             struct lpfc_fdmi_attr_def *ad)
3200 {
3201         struct lpfc_fdmi_attr_entry *ae;
3202         uint32_t size;
3203
3204         ae = &ad->AttrValue;
3205         ae->un.AttrInt =  cpu_to_be32(vport->fc_myDID);
3206         size = FOURBYTES + sizeof(uint32_t);
3207         ad->AttrLen = cpu_to_be16(size);
3208         ad->AttrType = cpu_to_be16(RPRT_PORT_ID);
3209         return size;
3210 }
3211
3212 static int
3213 lpfc_fdmi_smart_attr_service(struct lpfc_vport *vport,
3214                              struct lpfc_fdmi_attr_def *ad)
3215 {
3216         struct lpfc_fdmi_attr_entry *ae;
3217         uint32_t len, size;
3218
3219         ae = &ad->AttrValue;
3220         memset(ae, 0, sizeof(*ae));
3221
3222         strncpy(ae->un.AttrString, "Smart SAN Initiator",
3223                 sizeof(ae->un.AttrString));
3224         len = strnlen(ae->un.AttrString,
3225                           sizeof(ae->un.AttrString));
3226         len += (len & 3) ? (4 - (len & 3)) : 4;
3227         size = FOURBYTES + len;
3228         ad->AttrLen = cpu_to_be16(size);
3229         ad->AttrType = cpu_to_be16(RPRT_SMART_SERVICE);
3230         return size;
3231 }
3232
3233 static int
3234 lpfc_fdmi_smart_attr_guid(struct lpfc_vport *vport,
3235                           struct lpfc_fdmi_attr_def *ad)
3236 {
3237         struct lpfc_fdmi_attr_entry *ae;
3238         uint32_t size;
3239
3240         ae = &ad->AttrValue;
3241         memset(ae, 0, sizeof(*ae));
3242
3243         memcpy(&ae->un.AttrString, &vport->fc_sparam.nodeName,
3244                sizeof(struct lpfc_name));
3245         memcpy((((uint8_t *)&ae->un.AttrString) +
3246                 sizeof(struct lpfc_name)),
3247                 &vport->fc_sparam.portName, sizeof(struct lpfc_name));
3248         size = FOURBYTES + (2 * sizeof(struct lpfc_name));
3249         ad->AttrLen =  cpu_to_be16(size);
3250         ad->AttrType = cpu_to_be16(RPRT_SMART_GUID);
3251         return size;
3252 }
3253
3254 static int
3255 lpfc_fdmi_smart_attr_version(struct lpfc_vport *vport,
3256                              struct lpfc_fdmi_attr_def *ad)
3257 {
3258         struct lpfc_fdmi_attr_entry *ae;
3259         uint32_t len, size;
3260
3261         ae = &ad->AttrValue;
3262         memset(ae, 0, sizeof(*ae));
3263
3264         strncpy(ae->un.AttrString, "Smart SAN Version 2.0",
3265                 sizeof(ae->un.AttrString));
3266         len = strnlen(ae->un.AttrString,
3267                           sizeof(ae->un.AttrString));
3268         len += (len & 3) ? (4 - (len & 3)) : 4;
3269         size = FOURBYTES + len;
3270         ad->AttrLen =  cpu_to_be16(size);
3271         ad->AttrType = cpu_to_be16(RPRT_SMART_VERSION);
3272         return size;
3273 }
3274
3275 static int
3276 lpfc_fdmi_smart_attr_model(struct lpfc_vport *vport,
3277                            struct lpfc_fdmi_attr_def *ad)
3278 {
3279         struct lpfc_hba *phba = vport->phba;
3280         struct lpfc_fdmi_attr_entry *ae;
3281         uint32_t len, size;
3282
3283         ae = &ad->AttrValue;
3284         memset(ae, 0, sizeof(*ae));
3285
3286         strncpy(ae->un.AttrString, phba->ModelName,
3287                 sizeof(ae->un.AttrString));
3288         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
3289         len += (len & 3) ? (4 - (len & 3)) : 4;
3290         size = FOURBYTES + len;
3291         ad->AttrLen = cpu_to_be16(size);
3292         ad->AttrType = cpu_to_be16(RPRT_SMART_MODEL);
3293         return size;
3294 }
3295
3296 static int
3297 lpfc_fdmi_smart_attr_port_info(struct lpfc_vport *vport,
3298                                struct lpfc_fdmi_attr_def *ad)
3299 {
3300         struct lpfc_fdmi_attr_entry *ae;
3301         uint32_t size;
3302
3303         ae = &ad->AttrValue;
3304
3305         /* SRIOV (type 3) is not supported */
3306         if (vport->vpi)
3307                 ae->un.AttrInt =  cpu_to_be32(2);  /* NPIV */
3308         else
3309                 ae->un.AttrInt =  cpu_to_be32(1);  /* Physical */
3310         size = FOURBYTES + sizeof(uint32_t);
3311         ad->AttrLen = cpu_to_be16(size);
3312         ad->AttrType = cpu_to_be16(RPRT_SMART_PORT_INFO);
3313         return size;
3314 }
3315
3316 static int
3317 lpfc_fdmi_smart_attr_qos(struct lpfc_vport *vport,
3318                          struct lpfc_fdmi_attr_def *ad)
3319 {
3320         struct lpfc_fdmi_attr_entry *ae;
3321         uint32_t size;
3322
3323         ae = &ad->AttrValue;
3324         ae->un.AttrInt =  cpu_to_be32(0);
3325         size = FOURBYTES + sizeof(uint32_t);
3326         ad->AttrLen = cpu_to_be16(size);
3327         ad->AttrType = cpu_to_be16(RPRT_SMART_QOS);
3328         return size;
3329 }
3330
3331 static int
3332 lpfc_fdmi_smart_attr_security(struct lpfc_vport *vport,
3333                               struct lpfc_fdmi_attr_def *ad)
3334 {
3335         struct lpfc_fdmi_attr_entry *ae;
3336         uint32_t size;
3337
3338         ae = &ad->AttrValue;
3339         ae->un.AttrInt =  cpu_to_be32(1);
3340         size = FOURBYTES + sizeof(uint32_t);
3341         ad->AttrLen = cpu_to_be16(size);
3342         ad->AttrType = cpu_to_be16(RPRT_SMART_SECURITY);
3343         return size;
3344 }
3345
3346 static int
3347 lpfc_fdmi_vendor_attr_mi(struct lpfc_vport *vport,
3348                           struct lpfc_fdmi_attr_def *ad)
3349 {
3350         struct lpfc_hba *phba = vport->phba;
3351         struct lpfc_fdmi_attr_entry *ae;
3352         uint32_t len, size;
3353         char mibrevision[16];
3354
3355         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
3356         memset(ae, 0, 256);
3357         sprintf(mibrevision, "ELXE2EM:%04d",
3358                 phba->sli4_hba.pc_sli4_params.mi_ver);
3359         strncpy(ae->un.AttrString, &mibrevision[0], sizeof(ae->un.AttrString));
3360         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
3361         len += (len & 3) ? (4 - (len & 3)) : 4;
3362         size = FOURBYTES + len;
3363         ad->AttrLen = cpu_to_be16(size);
3364         ad->AttrType = cpu_to_be16(RPRT_VENDOR_MI);
3365         return size;
3366 }
3367
3368 /* RHBA attribute jump table */
3369 int (*lpfc_fdmi_hba_action[])
3370         (struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
3371         /* Action routine                 Mask bit     Attribute type */
3372         lpfc_fdmi_hba_attr_wwnn,          /* bit0     RHBA_NODENAME           */
3373         lpfc_fdmi_hba_attr_manufacturer,  /* bit1     RHBA_MANUFACTURER       */
3374         lpfc_fdmi_hba_attr_sn,            /* bit2     RHBA_SERIAL_NUMBER      */
3375         lpfc_fdmi_hba_attr_model,         /* bit3     RHBA_MODEL              */
3376         lpfc_fdmi_hba_attr_description,   /* bit4     RHBA_MODEL_DESCRIPTION  */
3377         lpfc_fdmi_hba_attr_hdw_ver,       /* bit5     RHBA_HARDWARE_VERSION   */
3378         lpfc_fdmi_hba_attr_drvr_ver,      /* bit6     RHBA_DRIVER_VERSION     */
3379         lpfc_fdmi_hba_attr_rom_ver,       /* bit7     RHBA_OPTION_ROM_VERSION */
3380         lpfc_fdmi_hba_attr_fmw_ver,       /* bit8     RHBA_FIRMWARE_VERSION   */
3381         lpfc_fdmi_hba_attr_os_ver,        /* bit9     RHBA_OS_NAME_VERSION    */
3382         lpfc_fdmi_hba_attr_ct_len,        /* bit10    RHBA_MAX_CT_PAYLOAD_LEN */
3383         lpfc_fdmi_hba_attr_symbolic_name, /* bit11    RHBA_SYM_NODENAME       */
3384         lpfc_fdmi_hba_attr_vendor_info,   /* bit12    RHBA_VENDOR_INFO        */
3385         lpfc_fdmi_hba_attr_num_ports,     /* bit13    RHBA_NUM_PORTS          */
3386         lpfc_fdmi_hba_attr_fabric_wwnn,   /* bit14    RHBA_FABRIC_WWNN        */
3387         lpfc_fdmi_hba_attr_bios_ver,      /* bit15    RHBA_BIOS_VERSION       */
3388         lpfc_fdmi_hba_attr_bios_state,    /* bit16    RHBA_BIOS_STATE         */
3389         lpfc_fdmi_hba_attr_vendor_id,     /* bit17    RHBA_VENDOR_ID          */
3390 };
3391
3392 /* RPA / RPRT attribute jump table */
3393 int (*lpfc_fdmi_port_action[])
3394         (struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
3395         /* Action routine                   Mask bit   Attribute type */
3396         lpfc_fdmi_port_attr_fc4type,        /* bit0   RPRT_SUPPORT_FC4_TYPES  */
3397         lpfc_fdmi_port_attr_support_speed,  /* bit1   RPRT_SUPPORTED_SPEED    */
3398         lpfc_fdmi_port_attr_speed,          /* bit2   RPRT_PORT_SPEED         */
3399         lpfc_fdmi_port_attr_max_frame,      /* bit3   RPRT_MAX_FRAME_SIZE     */
3400         lpfc_fdmi_port_attr_os_devname,     /* bit4   RPRT_OS_DEVICE_NAME     */
3401         lpfc_fdmi_port_attr_host_name,      /* bit5   RPRT_HOST_NAME          */
3402         lpfc_fdmi_port_attr_wwnn,           /* bit6   RPRT_NODENAME           */
3403         lpfc_fdmi_port_attr_wwpn,           /* bit7   RPRT_PORTNAME           */
3404         lpfc_fdmi_port_attr_symbolic_name,  /* bit8   RPRT_SYM_PORTNAME       */
3405         lpfc_fdmi_port_attr_port_type,      /* bit9   RPRT_PORT_TYPE          */
3406         lpfc_fdmi_port_attr_class,          /* bit10  RPRT_SUPPORTED_CLASS    */
3407         lpfc_fdmi_port_attr_fabric_wwpn,    /* bit11  RPRT_FABRICNAME         */
3408         lpfc_fdmi_port_attr_active_fc4type, /* bit12  RPRT_ACTIVE_FC4_TYPES   */
3409         lpfc_fdmi_port_attr_port_state,     /* bit13  RPRT_PORT_STATE         */
3410         lpfc_fdmi_port_attr_num_disc,       /* bit14  RPRT_DISC_PORT          */
3411         lpfc_fdmi_port_attr_nportid,        /* bit15  RPRT_PORT_ID            */
3412         lpfc_fdmi_smart_attr_service,       /* bit16  RPRT_SMART_SERVICE      */
3413         lpfc_fdmi_smart_attr_guid,          /* bit17  RPRT_SMART_GUID         */
3414         lpfc_fdmi_smart_attr_version,       /* bit18  RPRT_SMART_VERSION      */
3415         lpfc_fdmi_smart_attr_model,         /* bit19  RPRT_SMART_MODEL        */
3416         lpfc_fdmi_smart_attr_port_info,     /* bit20  RPRT_SMART_PORT_INFO    */
3417         lpfc_fdmi_smart_attr_qos,           /* bit21  RPRT_SMART_QOS          */
3418         lpfc_fdmi_smart_attr_security,      /* bit22  RPRT_SMART_SECURITY     */
3419         lpfc_fdmi_vendor_attr_mi,           /* bit23  RPRT_VENDOR_MI          */
3420 };
3421
3422 /**
3423  * lpfc_fdmi_cmd - Build and send a FDMI cmd to the specified NPort
3424  * @vport: pointer to a host virtual N_Port data structure.
3425  * @ndlp: ndlp to send FDMI cmd to (if NULL use FDMI_DID)
3426  * @cmdcode: FDMI command to send
3427  * @new_mask: Mask of HBA or PORT Attributes to send
3428  *
3429  * Builds and sends a FDMI command using the CT subsystem.
3430  */
3431 int
3432 lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3433               int cmdcode, uint32_t new_mask)
3434 {
3435         struct lpfc_hba *phba = vport->phba;
3436         struct lpfc_dmabuf *mp, *bmp;
3437         struct lpfc_sli_ct_request *CtReq;
3438         struct ulp_bde64 *bpl;
3439         uint32_t bit_pos;
3440         uint32_t size;
3441         uint32_t rsp_size;
3442         uint32_t mask;
3443         struct lpfc_fdmi_reg_hba *rh;
3444         struct lpfc_fdmi_port_entry *pe;
3445         struct lpfc_fdmi_reg_portattr *pab = NULL;
3446         struct lpfc_fdmi_attr_block *ab = NULL;
3447         int  (*func)(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad);
3448         void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
3449                      struct lpfc_iocbq *);
3450
3451         if (!ndlp)
3452                 return 0;
3453
3454         cmpl = lpfc_cmpl_ct_disc_fdmi; /* called from discovery */
3455
3456         /* fill in BDEs for command */
3457         /* Allocate buffer for command payload */
3458         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3459         if (!mp)
3460                 goto fdmi_cmd_exit;
3461
3462         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
3463         if (!mp->virt)
3464                 goto fdmi_cmd_free_mp;
3465
3466         /* Allocate buffer for Buffer ptr list */
3467         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3468         if (!bmp)
3469                 goto fdmi_cmd_free_mpvirt;
3470
3471         bmp->virt = lpfc_mbuf_alloc(phba, 0, &(bmp->phys));
3472         if (!bmp->virt)
3473                 goto fdmi_cmd_free_bmp;
3474
3475         INIT_LIST_HEAD(&mp->list);
3476         INIT_LIST_HEAD(&bmp->list);
3477
3478         /* FDMI request */
3479         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3480                          "0218 FDMI Request Data: x%x x%x x%x\n",
3481                          vport->fc_flag, vport->port_state, cmdcode);
3482         CtReq = (struct lpfc_sli_ct_request *)mp->virt;
3483
3484         /* First populate the CT_IU preamble */
3485         memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
3486         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
3487         CtReq->RevisionId.bits.InId = 0;
3488
3489         CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
3490         CtReq->FsSubType = SLI_CT_FDMI_Subtypes;
3491
3492         CtReq->CommandResponse.bits.CmdRsp = cpu_to_be16(cmdcode);
3493         rsp_size = LPFC_BPL_SIZE;
3494         size = 0;
3495
3496         /* Next fill in the specific FDMI cmd information */
3497         switch (cmdcode) {
3498         case SLI_MGMT_RHAT:
3499         case SLI_MGMT_RHBA:
3500                 rh = (struct lpfc_fdmi_reg_hba *)&CtReq->un.PortID;
3501                 /* HBA Identifier */
3502                 memcpy(&rh->hi.PortName, &phba->pport->fc_sparam.portName,
3503                        sizeof(struct lpfc_name));
3504
3505                 if (cmdcode == SLI_MGMT_RHBA) {
3506                         /* Registered Port List */
3507                         /* One entry (port) per adapter */
3508                         rh->rpl.EntryCnt = cpu_to_be32(1);
3509                         memcpy(&rh->rpl.pe.PortName,
3510                                &phba->pport->fc_sparam.portName,
3511                                sizeof(struct lpfc_name));
3512
3513                         /* point to the HBA attribute block */
3514                         size = 2 * sizeof(struct lpfc_name) +
3515                                 FOURBYTES;
3516                 } else {
3517                         size = sizeof(struct lpfc_name);
3518                 }
3519                 ab = (struct lpfc_fdmi_attr_block *)((uint8_t *)rh + size);
3520                 ab->EntryCnt = 0;
3521                 size += FOURBYTES;
3522                 bit_pos = 0;
3523                 if (new_mask)
3524                         mask = new_mask;
3525                 else
3526                         mask = vport->fdmi_hba_mask;
3527
3528                 /* Mask will dictate what attributes to build in the request */
3529                 while (mask) {
3530                         if (mask & 0x1) {
3531                                 func = lpfc_fdmi_hba_action[bit_pos];
3532                                 size += func(vport,
3533                                              (struct lpfc_fdmi_attr_def *)
3534                                              ((uint8_t *)rh + size));
3535                                 ab->EntryCnt++;
3536                                 if ((size + 256) >
3537                                     (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3538                                         goto hba_out;
3539                         }
3540                         mask = mask >> 1;
3541                         bit_pos++;
3542                 }
3543 hba_out:
3544                 ab->EntryCnt = cpu_to_be32(ab->EntryCnt);
3545                 /* Total size */
3546                 size = GID_REQUEST_SZ - 4 + size;
3547                 break;
3548
3549         case SLI_MGMT_RPRT:
3550         case SLI_MGMT_RPA:
3551                 pab = (struct lpfc_fdmi_reg_portattr *)&CtReq->un.PortID;
3552                 if (cmdcode == SLI_MGMT_RPRT) {
3553                         rh = (struct lpfc_fdmi_reg_hba *)pab;
3554                         /* HBA Identifier */
3555                         memcpy(&rh->hi.PortName,
3556                                &phba->pport->fc_sparam.portName,
3557                                sizeof(struct lpfc_name));
3558                         pab = (struct lpfc_fdmi_reg_portattr *)
3559                                 ((uint8_t *)pab +  sizeof(struct lpfc_name));
3560                 }
3561
3562                 memcpy((uint8_t *)&pab->PortName,
3563                        (uint8_t *)&vport->fc_sparam.portName,
3564                        sizeof(struct lpfc_name));
3565                 size += sizeof(struct lpfc_name) + FOURBYTES;
3566                 pab->ab.EntryCnt = 0;
3567                 bit_pos = 0;
3568                 if (new_mask)
3569                         mask = new_mask;
3570                 else
3571                         mask = vport->fdmi_port_mask;
3572
3573                 /* Mask will dictate what attributes to build in the request */
3574                 while (mask) {
3575                         if (mask & 0x1) {
3576                                 func = lpfc_fdmi_port_action[bit_pos];
3577                                 size += func(vport,
3578                                              (struct lpfc_fdmi_attr_def *)
3579                                              ((uint8_t *)pab + size));
3580                                 pab->ab.EntryCnt++;
3581                                 if ((size + 256) >
3582                                     (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3583                                         goto port_out;
3584                         }
3585                         mask = mask >> 1;
3586                         bit_pos++;
3587                 }
3588 port_out:
3589                 pab->ab.EntryCnt = cpu_to_be32(pab->ab.EntryCnt);
3590                 /* Total size */
3591                 if (cmdcode == SLI_MGMT_RPRT)
3592                         size += sizeof(struct lpfc_name);
3593                 size = GID_REQUEST_SZ - 4 + size;
3594                 break;
3595
3596         case SLI_MGMT_GHAT:
3597         case SLI_MGMT_GRPL:
3598                 rsp_size = FC_MAX_NS_RSP;
3599                 fallthrough;
3600         case SLI_MGMT_DHBA:
3601         case SLI_MGMT_DHAT:
3602                 pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3603                 memcpy((uint8_t *)&pe->PortName,
3604                        (uint8_t *)&vport->fc_sparam.portName,
3605                        sizeof(struct lpfc_name));
3606                 size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3607                 break;
3608
3609         case SLI_MGMT_GPAT:
3610         case SLI_MGMT_GPAS:
3611                 rsp_size = FC_MAX_NS_RSP;
3612                 fallthrough;
3613         case SLI_MGMT_DPRT:
3614         case SLI_MGMT_DPA:
3615                 pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3616                 memcpy((uint8_t *)&pe->PortName,
3617                        (uint8_t *)&vport->fc_sparam.portName,
3618                        sizeof(struct lpfc_name));
3619                 size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3620                 break;
3621         case SLI_MGMT_GRHL:
3622                 size = GID_REQUEST_SZ - 4;
3623                 break;
3624         default:
3625                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3626                                  "0298 FDMI cmdcode x%x not supported\n",
3627                                  cmdcode);
3628                 goto fdmi_cmd_free_bmpvirt;
3629         }
3630         CtReq->CommandResponse.bits.Size = cpu_to_be16(rsp_size);
3631
3632         bpl = (struct ulp_bde64 *)bmp->virt;
3633         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
3634         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
3635         bpl->tus.f.bdeFlags = 0;
3636         bpl->tus.f.bdeSize = size;
3637
3638         /*
3639          * The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
3640          * to hold ndlp reference for the corresponding callback function.
3641          */
3642         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, 0))
3643                 return 0;
3644
3645 fdmi_cmd_free_bmpvirt:
3646         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3647 fdmi_cmd_free_bmp:
3648         kfree(bmp);
3649 fdmi_cmd_free_mpvirt:
3650         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3651 fdmi_cmd_free_mp:
3652         kfree(mp);
3653 fdmi_cmd_exit:
3654         /* Issue FDMI request failed */
3655         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3656                          "0244 Issue FDMI request failed Data: x%x\n",
3657                          cmdcode);
3658         return 1;
3659 }
3660
3661 /**
3662  * lpfc_delayed_disc_tmo - Timeout handler for delayed discovery timer.
3663  * @t: Context object of the timer.
3664  *
3665  * This function set the WORKER_DELAYED_DISC_TMO flag and wake up
3666  * the worker thread.
3667  **/
3668 void
3669 lpfc_delayed_disc_tmo(struct timer_list *t)
3670 {
3671         struct lpfc_vport *vport = from_timer(vport, t, delayed_disc_tmo);
3672         struct lpfc_hba   *phba = vport->phba;
3673         uint32_t tmo_posted;
3674         unsigned long iflag;
3675
3676         spin_lock_irqsave(&vport->work_port_lock, iflag);
3677         tmo_posted = vport->work_port_events & WORKER_DELAYED_DISC_TMO;
3678         if (!tmo_posted)
3679                 vport->work_port_events |= WORKER_DELAYED_DISC_TMO;
3680         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3681
3682         if (!tmo_posted)
3683                 lpfc_worker_wake_up(phba);
3684         return;
3685 }
3686
3687 /**
3688  * lpfc_delayed_disc_timeout_handler - Function called by worker thread to
3689  *      handle delayed discovery.
3690  * @vport: pointer to a host virtual N_Port data structure.
3691  *
3692  * This function start nport discovery of the vport.
3693  **/
3694 void
3695 lpfc_delayed_disc_timeout_handler(struct lpfc_vport *vport)
3696 {
3697         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3698
3699         spin_lock_irq(shost->host_lock);
3700         if (!(vport->fc_flag & FC_DISC_DELAYED)) {
3701                 spin_unlock_irq(shost->host_lock);
3702                 return;
3703         }
3704         vport->fc_flag &= ~FC_DISC_DELAYED;
3705         spin_unlock_irq(shost->host_lock);
3706
3707         lpfc_do_scr_ns_plogi(vport->phba, vport);
3708 }
3709
3710 void
3711 lpfc_decode_firmware_rev(struct lpfc_hba *phba, char *fwrevision, int flag)
3712 {
3713         struct lpfc_sli *psli = &phba->sli;
3714         lpfc_vpd_t *vp = &phba->vpd;
3715         uint32_t b1, b2, b3, b4, i, rev;
3716         char c;
3717         uint32_t *ptr, str[4];
3718         uint8_t *fwname;
3719
3720         if (phba->sli_rev == LPFC_SLI_REV4)
3721                 snprintf(fwrevision, FW_REV_STR_SIZE, "%s", vp->rev.opFwName);
3722         else if (vp->rev.rBit) {
3723                 if (psli->sli_flag & LPFC_SLI_ACTIVE)
3724                         rev = vp->rev.sli2FwRev;
3725                 else
3726                         rev = vp->rev.sli1FwRev;
3727
3728                 b1 = (rev & 0x0000f000) >> 12;
3729                 b2 = (rev & 0x00000f00) >> 8;
3730                 b3 = (rev & 0x000000c0) >> 6;
3731                 b4 = (rev & 0x00000030) >> 4;
3732
3733                 switch (b4) {
3734                 case 0:
3735                         c = 'N';
3736                         break;
3737                 case 1:
3738                         c = 'A';
3739                         break;
3740                 case 2:
3741                         c = 'B';
3742                         break;
3743                 case 3:
3744                         c = 'X';
3745                         break;
3746                 default:
3747                         c = 0;
3748                         break;
3749                 }
3750                 b4 = (rev & 0x0000000f);
3751
3752                 if (psli->sli_flag & LPFC_SLI_ACTIVE)
3753                         fwname = vp->rev.sli2FwName;
3754                 else
3755                         fwname = vp->rev.sli1FwName;
3756
3757                 for (i = 0; i < 16; i++)
3758                         if (fwname[i] == 0x20)
3759                                 fwname[i] = 0;
3760
3761                 ptr = (uint32_t*)fwname;
3762
3763                 for (i = 0; i < 3; i++)
3764                         str[i] = be32_to_cpu(*ptr++);
3765
3766                 if (c == 0) {
3767                         if (flag)
3768                                 sprintf(fwrevision, "%d.%d%d (%s)",
3769                                         b1, b2, b3, (char *)str);
3770                         else
3771                                 sprintf(fwrevision, "%d.%d%d", b1,
3772                                         b2, b3);
3773                 } else {
3774                         if (flag)
3775                                 sprintf(fwrevision, "%d.%d%d%c%d (%s)",
3776                                         b1, b2, b3, c,
3777                                         b4, (char *)str);
3778                         else
3779                                 sprintf(fwrevision, "%d.%d%d%c%d",
3780                                         b1, b2, b3, c, b4);
3781                 }
3782         } else {
3783                 rev = vp->rev.smFwRev;
3784
3785                 b1 = (rev & 0xff000000) >> 24;
3786                 b2 = (rev & 0x00f00000) >> 20;
3787                 b3 = (rev & 0x000f0000) >> 16;
3788                 c  = (rev & 0x0000ff00) >> 8;
3789                 b4 = (rev & 0x000000ff);
3790
3791                 sprintf(fwrevision, "%d.%d%d%c%d", b1, b2, b3, c, b4);
3792         }
3793         return;
3794 }
3795
3796 static void
3797 lpfc_cmpl_ct_cmd_vmid(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3798                       struct lpfc_iocbq *rspiocb)
3799 {
3800         struct lpfc_vport *vport = cmdiocb->vport;
3801         struct lpfc_dmabuf *inp = cmdiocb->context1;
3802         struct lpfc_dmabuf *outp = cmdiocb->context2;
3803         struct lpfc_sli_ct_request *ctcmd = inp->virt;
3804         struct lpfc_sli_ct_request *ctrsp = outp->virt;
3805         u16 rsp = ctrsp->CommandResponse.bits.CmdRsp;
3806         struct app_id_object *app;
3807         u32 cmd, hash, bucket;
3808         struct lpfc_vmid *vmp, *cur;
3809         u8 *data = outp->virt;
3810         int i;
3811
3812         cmd = be16_to_cpu(ctcmd->CommandResponse.bits.CmdRsp);
3813         if (cmd == SLI_CTAS_DALLAPP_ID)
3814                 lpfc_ct_free_iocb(phba, cmdiocb);
3815
3816         if (lpfc_els_chk_latt(vport) || rspiocb->iocb.ulpStatus) {
3817                 if (cmd != SLI_CTAS_DALLAPP_ID)
3818                         return;
3819         }
3820         /* Check for a CT LS_RJT response */
3821         if (rsp == be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
3822                 if (cmd != SLI_CTAS_DALLAPP_ID)
3823                         lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3824                                          "3306 VMID FS_RJT Data: x%x x%x x%x\n",
3825                                          cmd, ctrsp->ReasonCode,
3826                                          ctrsp->Explanation);
3827                 if ((cmd != SLI_CTAS_DALLAPP_ID) ||
3828                     (ctrsp->ReasonCode != SLI_CT_UNABLE_TO_PERFORM_REQ) ||
3829                     (ctrsp->Explanation != SLI_CT_APP_ID_NOT_AVAILABLE)) {
3830                         /* If DALLAPP_ID failed retry later */
3831                         if (cmd == SLI_CTAS_DALLAPP_ID)
3832                                 vport->load_flag |= FC_DEREGISTER_ALL_APP_ID;
3833                         return;
3834                 }
3835         }
3836
3837         switch (cmd) {
3838         case SLI_CTAS_RAPP_IDENT:
3839                 app = (struct app_id_object *)(RAPP_IDENT_OFFSET + data);
3840                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3841                                  "6712 RAPP_IDENT app id %d  port id x%x id "
3842                                  "len %d\n", be32_to_cpu(app->app_id),
3843                                  be32_to_cpu(app->port_id),
3844                                  app->obj.entity_id_len);
3845
3846                 if (app->obj.entity_id_len == 0 || app->port_id == 0)
3847                         return;
3848
3849                 hash = lpfc_vmid_hash_fn(app->obj.entity_id,
3850                                          app->obj.entity_id_len);
3851                 vmp = lpfc_get_vmid_from_hashtable(vport, hash,
3852                                                   app->obj.entity_id);
3853                 if (vmp) {
3854                         write_lock(&vport->vmid_lock);
3855                         vmp->un.app_id = be32_to_cpu(app->app_id);
3856                         vmp->flag |= LPFC_VMID_REGISTERED;
3857                         vmp->flag &= ~LPFC_VMID_REQ_REGISTER;
3858                         write_unlock(&vport->vmid_lock);
3859                         /* Set IN USE flag */
3860                         vport->vmid_flag |= LPFC_VMID_IN_USE;
3861                 } else {
3862                         lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3863                                          "6901 No entry found %s hash %d\n",
3864                                          app->obj.entity_id, hash);
3865                 }
3866                 break;
3867         case SLI_CTAS_DAPP_IDENT:
3868                 app = (struct app_id_object *)(DAPP_IDENT_OFFSET + data);
3869                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3870                                  "6713 DAPP_IDENT app id %d  port id x%x\n",
3871                                  be32_to_cpu(app->app_id),
3872                                  be32_to_cpu(app->port_id));
3873                 break;
3874         case SLI_CTAS_DALLAPP_ID:
3875                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3876                                  "8856 Deregistered all app ids\n");
3877                 read_lock(&vport->vmid_lock);
3878                 for (i = 0; i < phba->cfg_max_vmid; i++) {
3879                         vmp = &vport->vmid[i];
3880                         if (vmp->flag != LPFC_VMID_SLOT_FREE)
3881                                 memset(vmp, 0, sizeof(struct lpfc_vmid));
3882                 }
3883                 read_unlock(&vport->vmid_lock);
3884                 /* for all elements in the hash table */
3885                 if (!hash_empty(vport->hash_table))
3886                         hash_for_each(vport->hash_table, bucket, cur, hnode)
3887                                 hash_del(&cur->hnode);
3888                 vport->load_flag |= FC_ALLOW_VMID;
3889                 break;
3890         default:
3891                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3892                                  "8857 Invalid command code\n");
3893         }
3894 }
3895
3896 /**
3897  * lpfc_vmid_cmd - Build and send a FDMI cmd to the specified NPort
3898  * @vport: pointer to a host virtual N_Port data structure.
3899  * @cmdcode: application server command code to send
3900  * @vmid: pointer to vmid info structure
3901  *
3902  * Builds and sends a FDMI command using the CT subsystem.
3903  */
3904 int
3905 lpfc_vmid_cmd(struct lpfc_vport *vport,
3906               int cmdcode, struct lpfc_vmid *vmid)
3907 {
3908         struct lpfc_hba *phba = vport->phba;
3909         struct lpfc_dmabuf *mp, *bmp;
3910         struct lpfc_sli_ct_request *ctreq;
3911         struct ulp_bde64 *bpl;
3912         u32 size;
3913         u32 rsp_size;
3914         u8 *data;
3915         struct lpfc_vmid_rapp_ident_list *rap;
3916         struct lpfc_vmid_dapp_ident_list *dap;
3917         u8 retry = 0;
3918         struct lpfc_nodelist *ndlp;
3919
3920         void (*cmpl)(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3921                      struct lpfc_iocbq *rspiocb);
3922
3923         ndlp = lpfc_findnode_did(vport, FDMI_DID);
3924         if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
3925                 return 0;
3926
3927         cmpl = lpfc_cmpl_ct_cmd_vmid;
3928
3929         /* fill in BDEs for command */
3930         /* Allocate buffer for command payload */
3931         mp = kmalloc(sizeof(*mp), GFP_KERNEL);
3932         if (!mp)
3933                 goto vmid_free_mp_exit;
3934
3935         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3936         if (!mp->virt)
3937                 goto vmid_free_mp_virt_exit;
3938
3939         /* Allocate buffer for Buffer ptr list */
3940         bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
3941         if (!bmp)
3942                 goto vmid_free_bmp_exit;
3943
3944         bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
3945         if (!bmp->virt)
3946                 goto vmid_free_bmp_virt_exit;
3947
3948         INIT_LIST_HEAD(&mp->list);
3949         INIT_LIST_HEAD(&bmp->list);
3950
3951         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3952                          "3275 VMID Request Data: x%x x%x x%x\n",
3953                          vport->fc_flag, vport->port_state, cmdcode);
3954         ctreq = (struct lpfc_sli_ct_request *)mp->virt;
3955         data = mp->virt;
3956         /* First populate the CT_IU preamble */
3957         memset(data, 0, LPFC_BPL_SIZE);
3958         ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3959         ctreq->RevisionId.bits.InId = 0;
3960
3961         ctreq->FsType = SLI_CT_MANAGEMENT_SERVICE;
3962         ctreq->FsSubType = SLI_CT_APP_SEV_Subtypes;
3963
3964         ctreq->CommandResponse.bits.CmdRsp = cpu_to_be16(cmdcode);
3965         rsp_size = LPFC_BPL_SIZE;
3966         size = 0;
3967
3968         switch (cmdcode) {
3969         case SLI_CTAS_RAPP_IDENT:
3970                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3971                                  "1329 RAPP_IDENT for %s\n", vmid->host_vmid);
3972                 ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
3973                 rap = (struct lpfc_vmid_rapp_ident_list *)
3974                         (DAPP_IDENT_OFFSET + data);
3975                 rap->no_of_objects = cpu_to_be32(1);
3976                 rap->obj[0].entity_id_len = vmid->vmid_len;
3977                 memcpy(rap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
3978                 size = RAPP_IDENT_OFFSET +
3979                         sizeof(struct lpfc_vmid_rapp_ident_list);
3980                 retry = 1;
3981                 break;
3982
3983         case SLI_CTAS_GALLAPPIA_ID:
3984                 ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
3985                 size = GALLAPPIA_ID_SIZE;
3986                 break;
3987
3988         case SLI_CTAS_DAPP_IDENT:
3989                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3990                                  "1469 DAPP_IDENT for %s\n", vmid->host_vmid);
3991                 ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
3992                 dap = (struct lpfc_vmid_dapp_ident_list *)
3993                         (DAPP_IDENT_OFFSET + data);
3994                 dap->no_of_objects = cpu_to_be32(1);
3995                 dap->obj[0].entity_id_len = vmid->vmid_len;
3996                 memcpy(dap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
3997                 size = DAPP_IDENT_OFFSET +
3998                         sizeof(struct lpfc_vmid_dapp_ident_list);
3999                 write_lock(&vport->vmid_lock);
4000                 vmid->flag &= ~LPFC_VMID_REGISTERED;
4001                 write_unlock(&vport->vmid_lock);
4002                 retry = 1;
4003                 break;
4004
4005         case SLI_CTAS_DALLAPP_ID:
4006                 ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
4007                 size = DALLAPP_ID_SIZE;
4008                 break;
4009
4010         default:
4011                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
4012                                  "7062 VMID cmdcode x%x not supported\n",
4013                                  cmdcode);
4014                 goto vmid_free_all_mem;
4015         }
4016
4017         ctreq->CommandResponse.bits.Size = cpu_to_be16(rsp_size);
4018
4019         bpl = (struct ulp_bde64 *)bmp->virt;
4020         bpl->addrHigh = putPaddrHigh(mp->phys);
4021         bpl->addrLow = putPaddrLow(mp->phys);
4022         bpl->tus.f.bdeFlags = 0;
4023         bpl->tus.f.bdeSize = size;
4024
4025         /* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
4026          * to hold ndlp reference for the corresponding callback function.
4027          */
4028         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry))
4029                 return 0;
4030
4031  vmid_free_all_mem:
4032         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
4033  vmid_free_bmp_virt_exit:
4034         kfree(bmp);
4035  vmid_free_bmp_exit:
4036         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4037  vmid_free_mp_virt_exit:
4038         kfree(mp);
4039  vmid_free_mp_exit:
4040
4041         /* Issue CT request failed */
4042         lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
4043                          "3276 VMID CT request failed Data: x%x\n", cmdcode);
4044         return -EIO;
4045 }