scsi: lpfc: vmid: Implement CT commands for appid
[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                                 lpfc_printf_log(phba, KERN_ERR,
2292                                                 LOG_DISCOVERY | LOG_ELS,
2293                                                 "6460 VEN FDMI RPA failure\n");
2294                                 return;
2295                         }
2296                         if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
2297                                 /* Fallback to FDMI-1 */
2298                                 vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
2299                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
2300                                 /* Start over */
2301                                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2302                                 return;
2303                         }
2304                         if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
2305                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
2306                                 /* Retry the same command */
2307                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
2308                         }
2309                         return;
2310                 }
2311         }
2312
2313         /*
2314          * On success, need to cycle thru FDMI registration for discovery
2315          * DHBA -> DPRT -> RHBA -> RPA  (physical port)
2316          * DPRT -> RPRT (vports)
2317          */
2318         switch (cmd) {
2319         case SLI_MGMT_RHBA:
2320                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA, 0);
2321                 break;
2322
2323         case SLI_MGMT_DHBA:
2324                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
2325                 break;
2326
2327         case SLI_MGMT_DPRT:
2328                 if (vport->port_type == LPFC_PHYSICAL_PORT)
2329                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RHBA, 0);
2330                 else
2331                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT, 0);
2332                 break;
2333         case SLI_MGMT_RPA:
2334                 if (vport->port_type == LPFC_PHYSICAL_PORT &&
2335                     phba->cfg_enable_mi &&
2336                     phba->sli4_hba.pc_sli4_params.mi_ver > LPFC_MIB1_SUPPORT) {
2337                         /* mi is only for the phyical port, no vports */
2338                         if (phba->link_flag & LS_CT_VEN_RPA) {
2339                                 lpfc_printf_vlog(vport, KERN_INFO,
2340                                                  LOG_DISCOVERY | LOG_ELS,
2341                                                  "6449 VEN RPA FDMI Success\n");
2342                                 phba->link_flag &= ~LS_CT_VEN_RPA;
2343                                 break;
2344                         }
2345
2346                         if (lpfc_fdmi_cmd(vport, ndlp, cmd,
2347                                           LPFC_FDMI_VENDOR_ATTR_mi) == 0)
2348                                 phba->link_flag |= LS_CT_VEN_RPA;
2349                         lpfc_printf_log(phba, KERN_INFO,
2350                                         LOG_DISCOVERY | LOG_ELS,
2351                                         "6458 Send MI FDMI:%x Flag x%x\n",
2352                                         phba->sli4_hba.pc_sli4_params.mi_value,
2353                                         phba->link_flag);
2354                 } else {
2355                         lpfc_printf_log(phba, KERN_INFO,
2356                                         LOG_DISCOVERY | LOG_ELS,
2357                                         "6459 No FDMI VEN MI support - "
2358                                         "RPA Success\n");
2359                 }
2360                 break;
2361         }
2362         return;
2363 }
2364
2365
2366 /**
2367  * lpfc_fdmi_change_check - Check for changed FDMI parameters
2368  * @vport: pointer to a host virtual N_Port data structure.
2369  *
2370  * Check how many mapped NPorts we are connected to
2371  * Check if our hostname changed
2372  * Called from hbeat timeout routine to check if any FDMI parameters
2373  * changed. If so, re-register those Attributes.
2374  */
2375 void
2376 lpfc_fdmi_change_check(struct lpfc_vport *vport)
2377 {
2378         struct lpfc_hba *phba = vport->phba;
2379         struct lpfc_nodelist *ndlp;
2380         uint16_t cnt;
2381
2382         if (!lpfc_is_link_up(phba))
2383                 return;
2384
2385         /* Must be connected to a Fabric */
2386         if (!(vport->fc_flag & FC_FABRIC))
2387                 return;
2388
2389         ndlp = lpfc_findnode_did(vport, FDMI_DID);
2390         if (!ndlp)
2391                 return;
2392
2393         /* Check if system hostname changed */
2394         if (strcmp(phba->os_host_name, init_utsname()->nodename)) {
2395                 memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
2396                 scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
2397                           init_utsname()->nodename);
2398                 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2399
2400                 /* Since this effects multiple HBA and PORT attributes, we need
2401                  * de-register and go thru the whole FDMI registration cycle.
2402                  * DHBA -> DPRT -> RHBA -> RPA  (physical port)
2403                  * DPRT -> RPRT (vports)
2404                  */
2405                 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2406                         /* For extra Vendor RPA */
2407                         phba->link_flag &= ~LS_CT_VEN_RPA;
2408                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2409                 } else {
2410                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
2411                 }
2412
2413                 /* Since this code path registers all the port attributes
2414                  * we can just return without further checking.
2415                  */
2416                 return;
2417         }
2418
2419         if (!(vport->fdmi_port_mask & LPFC_FDMI_PORT_ATTR_num_disc))
2420                 return;
2421
2422         /* Check if the number of mapped NPorts changed */
2423         cnt = lpfc_find_map_node(vport);
2424         if (cnt == vport->fdmi_num_disc)
2425                 return;
2426
2427         if (vport->port_type == LPFC_PHYSICAL_PORT) {
2428                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA,
2429                               LPFC_FDMI_PORT_ATTR_num_disc);
2430         } else {
2431                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT,
2432                               LPFC_FDMI_PORT_ATTR_num_disc);
2433         }
2434 }
2435
2436 /* Routines for all individual HBA attributes */
2437 static int
2438 lpfc_fdmi_hba_attr_wwnn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2439 {
2440         struct lpfc_fdmi_attr_entry *ae;
2441         uint32_t size;
2442
2443         ae = &ad->AttrValue;
2444         memset(ae, 0, sizeof(*ae));
2445
2446         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
2447                sizeof(struct lpfc_name));
2448         size = FOURBYTES + sizeof(struct lpfc_name);
2449         ad->AttrLen = cpu_to_be16(size);
2450         ad->AttrType = cpu_to_be16(RHBA_NODENAME);
2451         return size;
2452 }
2453 static int
2454 lpfc_fdmi_hba_attr_manufacturer(struct lpfc_vport *vport,
2455                                 struct lpfc_fdmi_attr_def *ad)
2456 {
2457         struct lpfc_fdmi_attr_entry *ae;
2458         uint32_t len, size;
2459
2460         ae = &ad->AttrValue;
2461         memset(ae, 0, sizeof(*ae));
2462
2463         /* This string MUST be consistent with other FC platforms
2464          * supported by Broadcom.
2465          */
2466         strncpy(ae->un.AttrString,
2467                 "Emulex Corporation",
2468                        sizeof(ae->un.AttrString));
2469         len = strnlen(ae->un.AttrString,
2470                           sizeof(ae->un.AttrString));
2471         len += (len & 3) ? (4 - (len & 3)) : 4;
2472         size = FOURBYTES + len;
2473         ad->AttrLen = cpu_to_be16(size);
2474         ad->AttrType = cpu_to_be16(RHBA_MANUFACTURER);
2475         return size;
2476 }
2477
2478 static int
2479 lpfc_fdmi_hba_attr_sn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2480 {
2481         struct lpfc_hba *phba = vport->phba;
2482         struct lpfc_fdmi_attr_entry *ae;
2483         uint32_t len, size;
2484
2485         ae = &ad->AttrValue;
2486         memset(ae, 0, sizeof(*ae));
2487
2488         strncpy(ae->un.AttrString, phba->SerialNumber,
2489                 sizeof(ae->un.AttrString));
2490         len = strnlen(ae->un.AttrString,
2491                           sizeof(ae->un.AttrString));
2492         len += (len & 3) ? (4 - (len & 3)) : 4;
2493         size = FOURBYTES + len;
2494         ad->AttrLen = cpu_to_be16(size);
2495         ad->AttrType = cpu_to_be16(RHBA_SERIAL_NUMBER);
2496         return size;
2497 }
2498
2499 static int
2500 lpfc_fdmi_hba_attr_model(struct lpfc_vport *vport,
2501                          struct lpfc_fdmi_attr_def *ad)
2502 {
2503         struct lpfc_hba *phba = vport->phba;
2504         struct lpfc_fdmi_attr_entry *ae;
2505         uint32_t len, size;
2506
2507         ae = &ad->AttrValue;
2508         memset(ae, 0, sizeof(*ae));
2509
2510         strncpy(ae->un.AttrString, phba->ModelName,
2511                 sizeof(ae->un.AttrString));
2512         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2513         len += (len & 3) ? (4 - (len & 3)) : 4;
2514         size = FOURBYTES + len;
2515         ad->AttrLen = cpu_to_be16(size);
2516         ad->AttrType = cpu_to_be16(RHBA_MODEL);
2517         return size;
2518 }
2519
2520 static int
2521 lpfc_fdmi_hba_attr_description(struct lpfc_vport *vport,
2522                                struct lpfc_fdmi_attr_def *ad)
2523 {
2524         struct lpfc_hba *phba = vport->phba;
2525         struct lpfc_fdmi_attr_entry *ae;
2526         uint32_t len, size;
2527
2528         ae = &ad->AttrValue;
2529         memset(ae, 0, sizeof(*ae));
2530
2531         strncpy(ae->un.AttrString, phba->ModelDesc,
2532                 sizeof(ae->un.AttrString));
2533         len = strnlen(ae->un.AttrString,
2534                                   sizeof(ae->un.AttrString));
2535         len += (len & 3) ? (4 - (len & 3)) : 4;
2536         size = FOURBYTES + len;
2537         ad->AttrLen = cpu_to_be16(size);
2538         ad->AttrType = cpu_to_be16(RHBA_MODEL_DESCRIPTION);
2539         return size;
2540 }
2541
2542 static int
2543 lpfc_fdmi_hba_attr_hdw_ver(struct lpfc_vport *vport,
2544                            struct lpfc_fdmi_attr_def *ad)
2545 {
2546         struct lpfc_hba *phba = vport->phba;
2547         lpfc_vpd_t *vp = &phba->vpd;
2548         struct lpfc_fdmi_attr_entry *ae;
2549         uint32_t i, j, incr, size;
2550
2551         ae = &ad->AttrValue;
2552         memset(ae, 0, sizeof(*ae));
2553
2554         /* Convert JEDEC ID to ascii for hardware version */
2555         incr = vp->rev.biuRev;
2556         for (i = 0; i < 8; i++) {
2557                 j = (incr & 0xf);
2558                 if (j <= 9)
2559                         ae->un.AttrString[7 - i] =
2560                             (char)((uint8_t) 0x30 +
2561                                    (uint8_t) j);
2562                 else
2563                         ae->un.AttrString[7 - i] =
2564                             (char)((uint8_t) 0x61 +
2565                                    (uint8_t) (j - 10));
2566                 incr = (incr >> 4);
2567         }
2568         size = FOURBYTES + 8;
2569         ad->AttrLen = cpu_to_be16(size);
2570         ad->AttrType = cpu_to_be16(RHBA_HARDWARE_VERSION);
2571         return size;
2572 }
2573
2574 static int
2575 lpfc_fdmi_hba_attr_drvr_ver(struct lpfc_vport *vport,
2576                             struct lpfc_fdmi_attr_def *ad)
2577 {
2578         struct lpfc_fdmi_attr_entry *ae;
2579         uint32_t len, size;
2580
2581         ae = &ad->AttrValue;
2582         memset(ae, 0, sizeof(*ae));
2583
2584         strncpy(ae->un.AttrString, lpfc_release_version,
2585                 sizeof(ae->un.AttrString));
2586         len = strnlen(ae->un.AttrString,
2587                           sizeof(ae->un.AttrString));
2588         len += (len & 3) ? (4 - (len & 3)) : 4;
2589         size = FOURBYTES + len;
2590         ad->AttrLen = cpu_to_be16(size);
2591         ad->AttrType = cpu_to_be16(RHBA_DRIVER_VERSION);
2592         return size;
2593 }
2594
2595 static int
2596 lpfc_fdmi_hba_attr_rom_ver(struct lpfc_vport *vport,
2597                            struct lpfc_fdmi_attr_def *ad)
2598 {
2599         struct lpfc_hba *phba = vport->phba;
2600         struct lpfc_fdmi_attr_entry *ae;
2601         uint32_t len, size;
2602
2603         ae = &ad->AttrValue;
2604         memset(ae, 0, sizeof(*ae));
2605
2606         if (phba->sli_rev == LPFC_SLI_REV4)
2607                 lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2608         else
2609                 strncpy(ae->un.AttrString, phba->OptionROMVersion,
2610                         sizeof(ae->un.AttrString));
2611         len = strnlen(ae->un.AttrString,
2612                           sizeof(ae->un.AttrString));
2613         len += (len & 3) ? (4 - (len & 3)) : 4;
2614         size = FOURBYTES + len;
2615         ad->AttrLen = cpu_to_be16(size);
2616         ad->AttrType = cpu_to_be16(RHBA_OPTION_ROM_VERSION);
2617         return size;
2618 }
2619
2620 static int
2621 lpfc_fdmi_hba_attr_fmw_ver(struct lpfc_vport *vport,
2622                            struct lpfc_fdmi_attr_def *ad)
2623 {
2624         struct lpfc_hba *phba = vport->phba;
2625         struct lpfc_fdmi_attr_entry *ae;
2626         uint32_t len, size;
2627
2628         ae = &ad->AttrValue;
2629         memset(ae, 0, sizeof(*ae));
2630
2631         lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2632         len = strnlen(ae->un.AttrString,
2633                           sizeof(ae->un.AttrString));
2634         len += (len & 3) ? (4 - (len & 3)) : 4;
2635         size = FOURBYTES + len;
2636         ad->AttrLen = cpu_to_be16(size);
2637         ad->AttrType = cpu_to_be16(RHBA_FIRMWARE_VERSION);
2638         return size;
2639 }
2640
2641 static int
2642 lpfc_fdmi_hba_attr_os_ver(struct lpfc_vport *vport,
2643                           struct lpfc_fdmi_attr_def *ad)
2644 {
2645         struct lpfc_fdmi_attr_entry *ae;
2646         uint32_t len, size;
2647
2648         ae = &ad->AttrValue;
2649         memset(ae, 0, sizeof(*ae));
2650
2651         snprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s %s %s",
2652                  init_utsname()->sysname,
2653                  init_utsname()->release,
2654                  init_utsname()->version);
2655
2656         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2657         len += (len & 3) ? (4 - (len & 3)) : 4;
2658         size = FOURBYTES + len;
2659         ad->AttrLen = cpu_to_be16(size);
2660         ad->AttrType = cpu_to_be16(RHBA_OS_NAME_VERSION);
2661         return size;
2662 }
2663
2664 static int
2665 lpfc_fdmi_hba_attr_ct_len(struct lpfc_vport *vport,
2666                           struct lpfc_fdmi_attr_def *ad)
2667 {
2668         struct lpfc_fdmi_attr_entry *ae;
2669         uint32_t size;
2670
2671         ae = &ad->AttrValue;
2672
2673         ae->un.AttrInt =  cpu_to_be32(LPFC_MAX_CT_SIZE);
2674         size = FOURBYTES + sizeof(uint32_t);
2675         ad->AttrLen = cpu_to_be16(size);
2676         ad->AttrType = cpu_to_be16(RHBA_MAX_CT_PAYLOAD_LEN);
2677         return size;
2678 }
2679
2680 static int
2681 lpfc_fdmi_hba_attr_symbolic_name(struct lpfc_vport *vport,
2682                                  struct lpfc_fdmi_attr_def *ad)
2683 {
2684         struct lpfc_fdmi_attr_entry *ae;
2685         uint32_t len, size;
2686
2687         ae = &ad->AttrValue;
2688         memset(ae, 0, sizeof(*ae));
2689
2690         len = lpfc_vport_symbolic_node_name(vport,
2691                                 ae->un.AttrString, 256);
2692         len += (len & 3) ? (4 - (len & 3)) : 4;
2693         size = FOURBYTES + len;
2694         ad->AttrLen = cpu_to_be16(size);
2695         ad->AttrType = cpu_to_be16(RHBA_SYM_NODENAME);
2696         return size;
2697 }
2698
2699 static int
2700 lpfc_fdmi_hba_attr_vendor_info(struct lpfc_vport *vport,
2701                                struct lpfc_fdmi_attr_def *ad)
2702 {
2703         struct lpfc_fdmi_attr_entry *ae;
2704         uint32_t size;
2705
2706         ae = &ad->AttrValue;
2707
2708         /* Nothing is defined for this currently */
2709         ae->un.AttrInt =  cpu_to_be32(0);
2710         size = FOURBYTES + sizeof(uint32_t);
2711         ad->AttrLen = cpu_to_be16(size);
2712         ad->AttrType = cpu_to_be16(RHBA_VENDOR_INFO);
2713         return size;
2714 }
2715
2716 static int
2717 lpfc_fdmi_hba_attr_num_ports(struct lpfc_vport *vport,
2718                              struct lpfc_fdmi_attr_def *ad)
2719 {
2720         struct lpfc_fdmi_attr_entry *ae;
2721         uint32_t size;
2722
2723         ae = &ad->AttrValue;
2724
2725         /* Each driver instance corresponds to a single port */
2726         ae->un.AttrInt =  cpu_to_be32(1);
2727         size = FOURBYTES + sizeof(uint32_t);
2728         ad->AttrLen = cpu_to_be16(size);
2729         ad->AttrType = cpu_to_be16(RHBA_NUM_PORTS);
2730         return size;
2731 }
2732
2733 static int
2734 lpfc_fdmi_hba_attr_fabric_wwnn(struct lpfc_vport *vport,
2735                                struct lpfc_fdmi_attr_def *ad)
2736 {
2737         struct lpfc_fdmi_attr_entry *ae;
2738         uint32_t size;
2739
2740         ae = &ad->AttrValue;
2741         memset(ae, 0, sizeof(*ae));
2742
2743         memcpy(&ae->un.AttrWWN, &vport->fabric_nodename,
2744                sizeof(struct lpfc_name));
2745         size = FOURBYTES + sizeof(struct lpfc_name);
2746         ad->AttrLen = cpu_to_be16(size);
2747         ad->AttrType = cpu_to_be16(RHBA_FABRIC_WWNN);
2748         return size;
2749 }
2750
2751 static int
2752 lpfc_fdmi_hba_attr_bios_ver(struct lpfc_vport *vport,
2753                             struct lpfc_fdmi_attr_def *ad)
2754 {
2755         struct lpfc_hba *phba = vport->phba;
2756         struct lpfc_fdmi_attr_entry *ae;
2757         uint32_t len, size;
2758
2759         ae = &ad->AttrValue;
2760         memset(ae, 0, sizeof(*ae));
2761
2762         strlcat(ae->un.AttrString, phba->BIOSVersion,
2763                 sizeof(ae->un.AttrString));
2764         len = strnlen(ae->un.AttrString,
2765                           sizeof(ae->un.AttrString));
2766         len += (len & 3) ? (4 - (len & 3)) : 4;
2767         size = FOURBYTES + len;
2768         ad->AttrLen = cpu_to_be16(size);
2769         ad->AttrType = cpu_to_be16(RHBA_BIOS_VERSION);
2770         return size;
2771 }
2772
2773 static int
2774 lpfc_fdmi_hba_attr_bios_state(struct lpfc_vport *vport,
2775                               struct lpfc_fdmi_attr_def *ad)
2776 {
2777         struct lpfc_fdmi_attr_entry *ae;
2778         uint32_t size;
2779
2780         ae = &ad->AttrValue;
2781
2782         /* Driver doesn't have access to this information */
2783         ae->un.AttrInt =  cpu_to_be32(0);
2784         size = FOURBYTES + sizeof(uint32_t);
2785         ad->AttrLen = cpu_to_be16(size);
2786         ad->AttrType = cpu_to_be16(RHBA_BIOS_STATE);
2787         return size;
2788 }
2789
2790 static int
2791 lpfc_fdmi_hba_attr_vendor_id(struct lpfc_vport *vport,
2792                              struct lpfc_fdmi_attr_def *ad)
2793 {
2794         struct lpfc_fdmi_attr_entry *ae;
2795         uint32_t len, size;
2796
2797         ae = &ad->AttrValue;
2798         memset(ae, 0, sizeof(*ae));
2799
2800         strncpy(ae->un.AttrString, "EMULEX",
2801                 sizeof(ae->un.AttrString));
2802         len = strnlen(ae->un.AttrString,
2803                           sizeof(ae->un.AttrString));
2804         len += (len & 3) ? (4 - (len & 3)) : 4;
2805         size = FOURBYTES + len;
2806         ad->AttrLen = cpu_to_be16(size);
2807         ad->AttrType = cpu_to_be16(RHBA_VENDOR_ID);
2808         return size;
2809 }
2810
2811 /* Routines for all individual PORT attributes */
2812 static int
2813 lpfc_fdmi_port_attr_fc4type(struct lpfc_vport *vport,
2814                             struct lpfc_fdmi_attr_def *ad)
2815 {
2816         struct lpfc_hba   *phba = vport->phba;
2817         struct lpfc_fdmi_attr_entry *ae;
2818         uint32_t size;
2819
2820         ae = &ad->AttrValue;
2821         memset(ae, 0, sizeof(*ae));
2822
2823         ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
2824         ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
2825
2826         /* Check to see if Firmware supports NVME and on physical port */
2827         if ((phba->sli_rev == LPFC_SLI_REV4) && (vport == phba->pport) &&
2828             phba->sli4_hba.pc_sli4_params.nvme)
2829                 ae->un.AttrTypes[6] = 0x01; /* Type 0x28 - NVME */
2830
2831         size = FOURBYTES + 32;
2832         ad->AttrLen = cpu_to_be16(size);
2833         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_FC4_TYPES);
2834         return size;
2835 }
2836
2837 static int
2838 lpfc_fdmi_port_attr_support_speed(struct lpfc_vport *vport,
2839                                   struct lpfc_fdmi_attr_def *ad)
2840 {
2841         struct lpfc_hba   *phba = vport->phba;
2842         struct lpfc_fdmi_attr_entry *ae;
2843         uint32_t size;
2844
2845         ae = &ad->AttrValue;
2846
2847         ae->un.AttrInt = 0;
2848         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2849                 if (phba->lmt & LMT_128Gb)
2850                         ae->un.AttrInt |= HBA_PORTSPEED_128GFC;
2851                 if (phba->lmt & LMT_64Gb)
2852                         ae->un.AttrInt |= HBA_PORTSPEED_64GFC;
2853                 if (phba->lmt & LMT_32Gb)
2854                         ae->un.AttrInt |= HBA_PORTSPEED_32GFC;
2855                 if (phba->lmt & LMT_16Gb)
2856                         ae->un.AttrInt |= HBA_PORTSPEED_16GFC;
2857                 if (phba->lmt & LMT_10Gb)
2858                         ae->un.AttrInt |= HBA_PORTSPEED_10GFC;
2859                 if (phba->lmt & LMT_8Gb)
2860                         ae->un.AttrInt |= HBA_PORTSPEED_8GFC;
2861                 if (phba->lmt & LMT_4Gb)
2862                         ae->un.AttrInt |= HBA_PORTSPEED_4GFC;
2863                 if (phba->lmt & LMT_2Gb)
2864                         ae->un.AttrInt |= HBA_PORTSPEED_2GFC;
2865                 if (phba->lmt & LMT_1Gb)
2866                         ae->un.AttrInt |= HBA_PORTSPEED_1GFC;
2867         } else {
2868                 /* FCoE links support only one speed */
2869                 switch (phba->fc_linkspeed) {
2870                 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2871                         ae->un.AttrInt = HBA_PORTSPEED_10GE;
2872                         break;
2873                 case LPFC_ASYNC_LINK_SPEED_25GBPS:
2874                         ae->un.AttrInt = HBA_PORTSPEED_25GE;
2875                         break;
2876                 case LPFC_ASYNC_LINK_SPEED_40GBPS:
2877                         ae->un.AttrInt = HBA_PORTSPEED_40GE;
2878                         break;
2879                 case LPFC_ASYNC_LINK_SPEED_100GBPS:
2880                         ae->un.AttrInt = HBA_PORTSPEED_100GE;
2881                         break;
2882                 }
2883         }
2884         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2885         size = FOURBYTES + sizeof(uint32_t);
2886         ad->AttrLen = cpu_to_be16(size);
2887         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_SPEED);
2888         return size;
2889 }
2890
2891 static int
2892 lpfc_fdmi_port_attr_speed(struct lpfc_vport *vport,
2893                           struct lpfc_fdmi_attr_def *ad)
2894 {
2895         struct lpfc_hba   *phba = vport->phba;
2896         struct lpfc_fdmi_attr_entry *ae;
2897         uint32_t size;
2898
2899         ae = &ad->AttrValue;
2900
2901         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2902                 switch (phba->fc_linkspeed) {
2903                 case LPFC_LINK_SPEED_1GHZ:
2904                         ae->un.AttrInt = HBA_PORTSPEED_1GFC;
2905                         break;
2906                 case LPFC_LINK_SPEED_2GHZ:
2907                         ae->un.AttrInt = HBA_PORTSPEED_2GFC;
2908                         break;
2909                 case LPFC_LINK_SPEED_4GHZ:
2910                         ae->un.AttrInt = HBA_PORTSPEED_4GFC;
2911                         break;
2912                 case LPFC_LINK_SPEED_8GHZ:
2913                         ae->un.AttrInt = HBA_PORTSPEED_8GFC;
2914                         break;
2915                 case LPFC_LINK_SPEED_10GHZ:
2916                         ae->un.AttrInt = HBA_PORTSPEED_10GFC;
2917                         break;
2918                 case LPFC_LINK_SPEED_16GHZ:
2919                         ae->un.AttrInt = HBA_PORTSPEED_16GFC;
2920                         break;
2921                 case LPFC_LINK_SPEED_32GHZ:
2922                         ae->un.AttrInt = HBA_PORTSPEED_32GFC;
2923                         break;
2924                 case LPFC_LINK_SPEED_64GHZ:
2925                         ae->un.AttrInt = HBA_PORTSPEED_64GFC;
2926                         break;
2927                 case LPFC_LINK_SPEED_128GHZ:
2928                         ae->un.AttrInt = HBA_PORTSPEED_128GFC;
2929                         break;
2930                 default:
2931                         ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2932                         break;
2933                 }
2934         } else {
2935                 switch (phba->fc_linkspeed) {
2936                 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2937                         ae->un.AttrInt = HBA_PORTSPEED_10GE;
2938                         break;
2939                 case LPFC_ASYNC_LINK_SPEED_25GBPS:
2940                         ae->un.AttrInt = HBA_PORTSPEED_25GE;
2941                         break;
2942                 case LPFC_ASYNC_LINK_SPEED_40GBPS:
2943                         ae->un.AttrInt = HBA_PORTSPEED_40GE;
2944                         break;
2945                 case LPFC_ASYNC_LINK_SPEED_100GBPS:
2946                         ae->un.AttrInt = HBA_PORTSPEED_100GE;
2947                         break;
2948                 default:
2949                         ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2950                         break;
2951                 }
2952         }
2953
2954         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2955         size = FOURBYTES + sizeof(uint32_t);
2956         ad->AttrLen = cpu_to_be16(size);
2957         ad->AttrType = cpu_to_be16(RPRT_PORT_SPEED);
2958         return size;
2959 }
2960
2961 static int
2962 lpfc_fdmi_port_attr_max_frame(struct lpfc_vport *vport,
2963                               struct lpfc_fdmi_attr_def *ad)
2964 {
2965         struct serv_parm *hsp;
2966         struct lpfc_fdmi_attr_entry *ae;
2967         uint32_t size;
2968
2969         ae = &ad->AttrValue;
2970
2971         hsp = (struct serv_parm *)&vport->fc_sparam;
2972         ae->un.AttrInt = (((uint32_t) hsp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
2973                           (uint32_t) hsp->cmn.bbRcvSizeLsb;
2974         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2975         size = FOURBYTES + sizeof(uint32_t);
2976         ad->AttrLen = cpu_to_be16(size);
2977         ad->AttrType = cpu_to_be16(RPRT_MAX_FRAME_SIZE);
2978         return size;
2979 }
2980
2981 static int
2982 lpfc_fdmi_port_attr_os_devname(struct lpfc_vport *vport,
2983                                struct lpfc_fdmi_attr_def *ad)
2984 {
2985         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2986         struct lpfc_fdmi_attr_entry *ae;
2987         uint32_t len, size;
2988
2989         ae = &ad->AttrValue;
2990         memset(ae, 0, sizeof(*ae));
2991
2992         snprintf(ae->un.AttrString, sizeof(ae->un.AttrString),
2993                  "/sys/class/scsi_host/host%d", shost->host_no);
2994         len = strnlen((char *)ae->un.AttrString,
2995                           sizeof(ae->un.AttrString));
2996         len += (len & 3) ? (4 - (len & 3)) : 4;
2997         size = FOURBYTES + len;
2998         ad->AttrLen = cpu_to_be16(size);
2999         ad->AttrType = cpu_to_be16(RPRT_OS_DEVICE_NAME);
3000         return size;
3001 }
3002
3003 static int
3004 lpfc_fdmi_port_attr_host_name(struct lpfc_vport *vport,
3005                               struct lpfc_fdmi_attr_def *ad)
3006 {
3007         struct lpfc_fdmi_attr_entry *ae;
3008         uint32_t len, size;
3009
3010         ae = &ad->AttrValue;
3011         memset(ae, 0, sizeof(*ae));
3012
3013         scnprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s",
3014                   vport->phba->os_host_name);
3015
3016         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
3017         len += (len & 3) ? (4 - (len & 3)) : 4;
3018         size = FOURBYTES + len;
3019         ad->AttrLen = cpu_to_be16(size);
3020         ad->AttrType = cpu_to_be16(RPRT_HOST_NAME);
3021         return size;
3022 }
3023
3024 static int
3025 lpfc_fdmi_port_attr_wwnn(struct lpfc_vport *vport,
3026                          struct lpfc_fdmi_attr_def *ad)
3027 {
3028         struct lpfc_fdmi_attr_entry *ae;
3029         uint32_t size;
3030
3031         ae = &ad->AttrValue;
3032         memset(ae, 0, sizeof(*ae));
3033
3034         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
3035                sizeof(struct lpfc_name));
3036         size = FOURBYTES + sizeof(struct lpfc_name);
3037         ad->AttrLen = cpu_to_be16(size);
3038         ad->AttrType = cpu_to_be16(RPRT_NODENAME);
3039         return size;
3040 }
3041
3042 static int
3043 lpfc_fdmi_port_attr_wwpn(struct lpfc_vport *vport,
3044                          struct lpfc_fdmi_attr_def *ad)
3045 {
3046         struct lpfc_fdmi_attr_entry *ae;
3047         uint32_t size;
3048
3049         ae = &ad->AttrValue;
3050         memset(ae, 0, sizeof(*ae));
3051
3052         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.portName,
3053                sizeof(struct lpfc_name));
3054         size = FOURBYTES + sizeof(struct lpfc_name);
3055         ad->AttrLen = cpu_to_be16(size);
3056         ad->AttrType = cpu_to_be16(RPRT_PORTNAME);
3057         return size;
3058 }
3059
3060 static int
3061 lpfc_fdmi_port_attr_symbolic_name(struct lpfc_vport *vport,
3062                                   struct lpfc_fdmi_attr_def *ad)
3063 {
3064         struct lpfc_fdmi_attr_entry *ae;
3065         uint32_t len, size;
3066
3067         ae = &ad->AttrValue;
3068         memset(ae, 0, sizeof(*ae));
3069
3070         len = lpfc_vport_symbolic_port_name(vport, ae->un.AttrString, 256);
3071         len += (len & 3) ? (4 - (len & 3)) : 4;
3072         size = FOURBYTES + len;
3073         ad->AttrLen = cpu_to_be16(size);
3074         ad->AttrType = cpu_to_be16(RPRT_SYM_PORTNAME);
3075         return size;
3076 }
3077
3078 static int
3079 lpfc_fdmi_port_attr_port_type(struct lpfc_vport *vport,
3080                               struct lpfc_fdmi_attr_def *ad)
3081 {
3082         struct lpfc_hba *phba = vport->phba;
3083         struct lpfc_fdmi_attr_entry *ae;
3084         uint32_t size;
3085
3086         ae = &ad->AttrValue;
3087         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP)
3088                 ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NLPORT);
3089         else
3090                 ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NPORT);
3091         size = FOURBYTES + sizeof(uint32_t);
3092         ad->AttrLen = cpu_to_be16(size);
3093         ad->AttrType = cpu_to_be16(RPRT_PORT_TYPE);
3094         return size;
3095 }
3096
3097 static int
3098 lpfc_fdmi_port_attr_class(struct lpfc_vport *vport,
3099                           struct lpfc_fdmi_attr_def *ad)
3100 {
3101         struct lpfc_fdmi_attr_entry *ae;
3102         uint32_t size;
3103
3104         ae = &ad->AttrValue;
3105         ae->un.AttrInt = cpu_to_be32(FC_COS_CLASS2 | FC_COS_CLASS3);
3106         size = FOURBYTES + sizeof(uint32_t);
3107         ad->AttrLen = cpu_to_be16(size);
3108         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_CLASS);
3109         return size;
3110 }
3111
3112 static int
3113 lpfc_fdmi_port_attr_fabric_wwpn(struct lpfc_vport *vport,
3114                                 struct lpfc_fdmi_attr_def *ad)
3115 {
3116         struct lpfc_fdmi_attr_entry *ae;
3117         uint32_t size;
3118
3119         ae = &ad->AttrValue;
3120         memset(ae, 0, sizeof(*ae));
3121
3122         memcpy(&ae->un.AttrWWN, &vport->fabric_portname,
3123                sizeof(struct lpfc_name));
3124         size = FOURBYTES + sizeof(struct lpfc_name);
3125         ad->AttrLen = cpu_to_be16(size);
3126         ad->AttrType = cpu_to_be16(RPRT_FABRICNAME);
3127         return size;
3128 }
3129
3130 static int
3131 lpfc_fdmi_port_attr_active_fc4type(struct lpfc_vport *vport,
3132                                    struct lpfc_fdmi_attr_def *ad)
3133 {
3134         struct lpfc_fdmi_attr_entry *ae;
3135         uint32_t size;
3136
3137         ae = &ad->AttrValue;
3138         memset(ae, 0, sizeof(*ae));
3139
3140         ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
3141         ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
3142
3143         /* Check to see if NVME is configured or not */
3144         if (vport->phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3145                 ae->un.AttrTypes[6] = 0x1; /* Type 0x28 - NVME */
3146
3147         size = FOURBYTES + 32;
3148         ad->AttrLen = cpu_to_be16(size);
3149         ad->AttrType = cpu_to_be16(RPRT_ACTIVE_FC4_TYPES);
3150         return size;
3151 }
3152
3153 static int
3154 lpfc_fdmi_port_attr_port_state(struct lpfc_vport *vport,
3155                                struct lpfc_fdmi_attr_def *ad)
3156 {
3157         struct lpfc_fdmi_attr_entry *ae;
3158         uint32_t size;
3159
3160         ae = &ad->AttrValue;
3161         /* Link Up - operational */
3162         ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTSTATE_ONLINE);
3163         size = FOURBYTES + sizeof(uint32_t);
3164         ad->AttrLen = cpu_to_be16(size);
3165         ad->AttrType = cpu_to_be16(RPRT_PORT_STATE);
3166         return size;
3167 }
3168
3169 static int
3170 lpfc_fdmi_port_attr_num_disc(struct lpfc_vport *vport,
3171                              struct lpfc_fdmi_attr_def *ad)
3172 {
3173         struct lpfc_fdmi_attr_entry *ae;
3174         uint32_t size;
3175
3176         ae = &ad->AttrValue;
3177         vport->fdmi_num_disc = lpfc_find_map_node(vport);
3178         ae->un.AttrInt = cpu_to_be32(vport->fdmi_num_disc);
3179         size = FOURBYTES + sizeof(uint32_t);
3180         ad->AttrLen = cpu_to_be16(size);
3181         ad->AttrType = cpu_to_be16(RPRT_DISC_PORT);
3182         return size;
3183 }
3184
3185 static int
3186 lpfc_fdmi_port_attr_nportid(struct lpfc_vport *vport,
3187                             struct lpfc_fdmi_attr_def *ad)
3188 {
3189         struct lpfc_fdmi_attr_entry *ae;
3190         uint32_t size;
3191
3192         ae = &ad->AttrValue;
3193         ae->un.AttrInt =  cpu_to_be32(vport->fc_myDID);
3194         size = FOURBYTES + sizeof(uint32_t);
3195         ad->AttrLen = cpu_to_be16(size);
3196         ad->AttrType = cpu_to_be16(RPRT_PORT_ID);
3197         return size;
3198 }
3199
3200 static int
3201 lpfc_fdmi_smart_attr_service(struct lpfc_vport *vport,
3202                              struct lpfc_fdmi_attr_def *ad)
3203 {
3204         struct lpfc_fdmi_attr_entry *ae;
3205         uint32_t len, size;
3206
3207         ae = &ad->AttrValue;
3208         memset(ae, 0, sizeof(*ae));
3209
3210         strncpy(ae->un.AttrString, "Smart SAN Initiator",
3211                 sizeof(ae->un.AttrString));
3212         len = strnlen(ae->un.AttrString,
3213                           sizeof(ae->un.AttrString));
3214         len += (len & 3) ? (4 - (len & 3)) : 4;
3215         size = FOURBYTES + len;
3216         ad->AttrLen = cpu_to_be16(size);
3217         ad->AttrType = cpu_to_be16(RPRT_SMART_SERVICE);
3218         return size;
3219 }
3220
3221 static int
3222 lpfc_fdmi_smart_attr_guid(struct lpfc_vport *vport,
3223                           struct lpfc_fdmi_attr_def *ad)
3224 {
3225         struct lpfc_fdmi_attr_entry *ae;
3226         uint32_t size;
3227
3228         ae = &ad->AttrValue;
3229         memset(ae, 0, sizeof(*ae));
3230
3231         memcpy(&ae->un.AttrString, &vport->fc_sparam.nodeName,
3232                sizeof(struct lpfc_name));
3233         memcpy((((uint8_t *)&ae->un.AttrString) +
3234                 sizeof(struct lpfc_name)),
3235                 &vport->fc_sparam.portName, sizeof(struct lpfc_name));
3236         size = FOURBYTES + (2 * sizeof(struct lpfc_name));
3237         ad->AttrLen =  cpu_to_be16(size);
3238         ad->AttrType = cpu_to_be16(RPRT_SMART_GUID);
3239         return size;
3240 }
3241
3242 static int
3243 lpfc_fdmi_smart_attr_version(struct lpfc_vport *vport,
3244                              struct lpfc_fdmi_attr_def *ad)
3245 {
3246         struct lpfc_fdmi_attr_entry *ae;
3247         uint32_t len, size;
3248
3249         ae = &ad->AttrValue;
3250         memset(ae, 0, sizeof(*ae));
3251
3252         strncpy(ae->un.AttrString, "Smart SAN Version 2.0",
3253                 sizeof(ae->un.AttrString));
3254         len = strnlen(ae->un.AttrString,
3255                           sizeof(ae->un.AttrString));
3256         len += (len & 3) ? (4 - (len & 3)) : 4;
3257         size = FOURBYTES + len;
3258         ad->AttrLen =  cpu_to_be16(size);
3259         ad->AttrType = cpu_to_be16(RPRT_SMART_VERSION);
3260         return size;
3261 }
3262
3263 static int
3264 lpfc_fdmi_smart_attr_model(struct lpfc_vport *vport,
3265                            struct lpfc_fdmi_attr_def *ad)
3266 {
3267         struct lpfc_hba *phba = vport->phba;
3268         struct lpfc_fdmi_attr_entry *ae;
3269         uint32_t len, size;
3270
3271         ae = &ad->AttrValue;
3272         memset(ae, 0, sizeof(*ae));
3273
3274         strncpy(ae->un.AttrString, phba->ModelName,
3275                 sizeof(ae->un.AttrString));
3276         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
3277         len += (len & 3) ? (4 - (len & 3)) : 4;
3278         size = FOURBYTES + len;
3279         ad->AttrLen = cpu_to_be16(size);
3280         ad->AttrType = cpu_to_be16(RPRT_SMART_MODEL);
3281         return size;
3282 }
3283
3284 static int
3285 lpfc_fdmi_smart_attr_port_info(struct lpfc_vport *vport,
3286                                struct lpfc_fdmi_attr_def *ad)
3287 {
3288         struct lpfc_fdmi_attr_entry *ae;
3289         uint32_t size;
3290
3291         ae = &ad->AttrValue;
3292
3293         /* SRIOV (type 3) is not supported */
3294         if (vport->vpi)
3295                 ae->un.AttrInt =  cpu_to_be32(2);  /* NPIV */
3296         else
3297                 ae->un.AttrInt =  cpu_to_be32(1);  /* Physical */
3298         size = FOURBYTES + sizeof(uint32_t);
3299         ad->AttrLen = cpu_to_be16(size);
3300         ad->AttrType = cpu_to_be16(RPRT_SMART_PORT_INFO);
3301         return size;
3302 }
3303
3304 static int
3305 lpfc_fdmi_smart_attr_qos(struct lpfc_vport *vport,
3306                          struct lpfc_fdmi_attr_def *ad)
3307 {
3308         struct lpfc_fdmi_attr_entry *ae;
3309         uint32_t size;
3310
3311         ae = &ad->AttrValue;
3312         ae->un.AttrInt =  cpu_to_be32(0);
3313         size = FOURBYTES + sizeof(uint32_t);
3314         ad->AttrLen = cpu_to_be16(size);
3315         ad->AttrType = cpu_to_be16(RPRT_SMART_QOS);
3316         return size;
3317 }
3318
3319 static int
3320 lpfc_fdmi_smart_attr_security(struct lpfc_vport *vport,
3321                               struct lpfc_fdmi_attr_def *ad)
3322 {
3323         struct lpfc_fdmi_attr_entry *ae;
3324         uint32_t size;
3325
3326         ae = &ad->AttrValue;
3327         ae->un.AttrInt =  cpu_to_be32(1);
3328         size = FOURBYTES + sizeof(uint32_t);
3329         ad->AttrLen = cpu_to_be16(size);
3330         ad->AttrType = cpu_to_be16(RPRT_SMART_SECURITY);
3331         return size;
3332 }
3333
3334 static int
3335 lpfc_fdmi_vendor_attr_mi(struct lpfc_vport *vport,
3336                           struct lpfc_fdmi_attr_def *ad)
3337 {
3338         struct lpfc_hba *phba = vport->phba;
3339         struct lpfc_fdmi_attr_entry *ae;
3340         uint32_t len, size;
3341         char mibrevision[16];
3342
3343         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
3344         memset(ae, 0, 256);
3345         sprintf(mibrevision, "ELXE2EM:%04d",
3346                 phba->sli4_hba.pc_sli4_params.mi_value);
3347         strncpy(ae->un.AttrString, &mibrevision[0], sizeof(ae->un.AttrString));
3348         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
3349         len += (len & 3) ? (4 - (len & 3)) : 4;
3350         size = FOURBYTES + len;
3351         ad->AttrLen = cpu_to_be16(size);
3352         ad->AttrType = cpu_to_be16(RPRT_VENDOR_MI);
3353         return size;
3354 }
3355
3356 /* RHBA attribute jump table */
3357 int (*lpfc_fdmi_hba_action[])
3358         (struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
3359         /* Action routine                 Mask bit     Attribute type */
3360         lpfc_fdmi_hba_attr_wwnn,          /* bit0     RHBA_NODENAME           */
3361         lpfc_fdmi_hba_attr_manufacturer,  /* bit1     RHBA_MANUFACTURER       */
3362         lpfc_fdmi_hba_attr_sn,            /* bit2     RHBA_SERIAL_NUMBER      */
3363         lpfc_fdmi_hba_attr_model,         /* bit3     RHBA_MODEL              */
3364         lpfc_fdmi_hba_attr_description,   /* bit4     RHBA_MODEL_DESCRIPTION  */
3365         lpfc_fdmi_hba_attr_hdw_ver,       /* bit5     RHBA_HARDWARE_VERSION   */
3366         lpfc_fdmi_hba_attr_drvr_ver,      /* bit6     RHBA_DRIVER_VERSION     */
3367         lpfc_fdmi_hba_attr_rom_ver,       /* bit7     RHBA_OPTION_ROM_VERSION */
3368         lpfc_fdmi_hba_attr_fmw_ver,       /* bit8     RHBA_FIRMWARE_VERSION   */
3369         lpfc_fdmi_hba_attr_os_ver,        /* bit9     RHBA_OS_NAME_VERSION    */
3370         lpfc_fdmi_hba_attr_ct_len,        /* bit10    RHBA_MAX_CT_PAYLOAD_LEN */
3371         lpfc_fdmi_hba_attr_symbolic_name, /* bit11    RHBA_SYM_NODENAME       */
3372         lpfc_fdmi_hba_attr_vendor_info,   /* bit12    RHBA_VENDOR_INFO        */
3373         lpfc_fdmi_hba_attr_num_ports,     /* bit13    RHBA_NUM_PORTS          */
3374         lpfc_fdmi_hba_attr_fabric_wwnn,   /* bit14    RHBA_FABRIC_WWNN        */
3375         lpfc_fdmi_hba_attr_bios_ver,      /* bit15    RHBA_BIOS_VERSION       */
3376         lpfc_fdmi_hba_attr_bios_state,    /* bit16    RHBA_BIOS_STATE         */
3377         lpfc_fdmi_hba_attr_vendor_id,     /* bit17    RHBA_VENDOR_ID          */
3378 };
3379
3380 /* RPA / RPRT attribute jump table */
3381 int (*lpfc_fdmi_port_action[])
3382         (struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
3383         /* Action routine                   Mask bit   Attribute type */
3384         lpfc_fdmi_port_attr_fc4type,        /* bit0   RPRT_SUPPORT_FC4_TYPES  */
3385         lpfc_fdmi_port_attr_support_speed,  /* bit1   RPRT_SUPPORTED_SPEED    */
3386         lpfc_fdmi_port_attr_speed,          /* bit2   RPRT_PORT_SPEED         */
3387         lpfc_fdmi_port_attr_max_frame,      /* bit3   RPRT_MAX_FRAME_SIZE     */
3388         lpfc_fdmi_port_attr_os_devname,     /* bit4   RPRT_OS_DEVICE_NAME     */
3389         lpfc_fdmi_port_attr_host_name,      /* bit5   RPRT_HOST_NAME          */
3390         lpfc_fdmi_port_attr_wwnn,           /* bit6   RPRT_NODENAME           */
3391         lpfc_fdmi_port_attr_wwpn,           /* bit7   RPRT_PORTNAME           */
3392         lpfc_fdmi_port_attr_symbolic_name,  /* bit8   RPRT_SYM_PORTNAME       */
3393         lpfc_fdmi_port_attr_port_type,      /* bit9   RPRT_PORT_TYPE          */
3394         lpfc_fdmi_port_attr_class,          /* bit10  RPRT_SUPPORTED_CLASS    */
3395         lpfc_fdmi_port_attr_fabric_wwpn,    /* bit11  RPRT_FABRICNAME         */
3396         lpfc_fdmi_port_attr_active_fc4type, /* bit12  RPRT_ACTIVE_FC4_TYPES   */
3397         lpfc_fdmi_port_attr_port_state,     /* bit13  RPRT_PORT_STATE         */
3398         lpfc_fdmi_port_attr_num_disc,       /* bit14  RPRT_DISC_PORT          */
3399         lpfc_fdmi_port_attr_nportid,        /* bit15  RPRT_PORT_ID            */
3400         lpfc_fdmi_smart_attr_service,       /* bit16  RPRT_SMART_SERVICE      */
3401         lpfc_fdmi_smart_attr_guid,          /* bit17  RPRT_SMART_GUID         */
3402         lpfc_fdmi_smart_attr_version,       /* bit18  RPRT_SMART_VERSION      */
3403         lpfc_fdmi_smart_attr_model,         /* bit19  RPRT_SMART_MODEL        */
3404         lpfc_fdmi_smart_attr_port_info,     /* bit20  RPRT_SMART_PORT_INFO    */
3405         lpfc_fdmi_smart_attr_qos,           /* bit21  RPRT_SMART_QOS          */
3406         lpfc_fdmi_smart_attr_security,      /* bit22  RPRT_SMART_SECURITY     */
3407         lpfc_fdmi_vendor_attr_mi,           /* bit23  RPRT_VENDOR_MI          */
3408 };
3409
3410 /**
3411  * lpfc_fdmi_cmd - Build and send a FDMI cmd to the specified NPort
3412  * @vport: pointer to a host virtual N_Port data structure.
3413  * @ndlp: ndlp to send FDMI cmd to (if NULL use FDMI_DID)
3414  * @cmdcode: FDMI command to send
3415  * @new_mask: Mask of HBA or PORT Attributes to send
3416  *
3417  * Builds and sends a FDMI command using the CT subsystem.
3418  */
3419 int
3420 lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3421               int cmdcode, uint32_t new_mask)
3422 {
3423         struct lpfc_hba *phba = vport->phba;
3424         struct lpfc_dmabuf *mp, *bmp;
3425         struct lpfc_sli_ct_request *CtReq;
3426         struct ulp_bde64 *bpl;
3427         uint32_t bit_pos;
3428         uint32_t size;
3429         uint32_t rsp_size;
3430         uint32_t mask;
3431         struct lpfc_fdmi_reg_hba *rh;
3432         struct lpfc_fdmi_port_entry *pe;
3433         struct lpfc_fdmi_reg_portattr *pab = NULL;
3434         struct lpfc_fdmi_attr_block *ab = NULL;
3435         int  (*func)(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad);
3436         void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
3437                      struct lpfc_iocbq *);
3438
3439         if (!ndlp)
3440                 return 0;
3441
3442         cmpl = lpfc_cmpl_ct_disc_fdmi; /* called from discovery */
3443
3444         /* fill in BDEs for command */
3445         /* Allocate buffer for command payload */
3446         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3447         if (!mp)
3448                 goto fdmi_cmd_exit;
3449
3450         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
3451         if (!mp->virt)
3452                 goto fdmi_cmd_free_mp;
3453
3454         /* Allocate buffer for Buffer ptr list */
3455         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3456         if (!bmp)
3457                 goto fdmi_cmd_free_mpvirt;
3458
3459         bmp->virt = lpfc_mbuf_alloc(phba, 0, &(bmp->phys));
3460         if (!bmp->virt)
3461                 goto fdmi_cmd_free_bmp;
3462
3463         INIT_LIST_HEAD(&mp->list);
3464         INIT_LIST_HEAD(&bmp->list);
3465
3466         /* FDMI request */
3467         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3468                          "0218 FDMI Request Data: x%x x%x x%x\n",
3469                          vport->fc_flag, vport->port_state, cmdcode);
3470         CtReq = (struct lpfc_sli_ct_request *)mp->virt;
3471
3472         /* First populate the CT_IU preamble */
3473         memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
3474         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
3475         CtReq->RevisionId.bits.InId = 0;
3476
3477         CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
3478         CtReq->FsSubType = SLI_CT_FDMI_Subtypes;
3479
3480         CtReq->CommandResponse.bits.CmdRsp = cpu_to_be16(cmdcode);
3481         rsp_size = LPFC_BPL_SIZE;
3482         size = 0;
3483
3484         /* Next fill in the specific FDMI cmd information */
3485         switch (cmdcode) {
3486         case SLI_MGMT_RHAT:
3487         case SLI_MGMT_RHBA:
3488                 rh = (struct lpfc_fdmi_reg_hba *)&CtReq->un.PortID;
3489                 /* HBA Identifier */
3490                 memcpy(&rh->hi.PortName, &phba->pport->fc_sparam.portName,
3491                        sizeof(struct lpfc_name));
3492
3493                 if (cmdcode == SLI_MGMT_RHBA) {
3494                         /* Registered Port List */
3495                         /* One entry (port) per adapter */
3496                         rh->rpl.EntryCnt = cpu_to_be32(1);
3497                         memcpy(&rh->rpl.pe.PortName,
3498                                &phba->pport->fc_sparam.portName,
3499                                sizeof(struct lpfc_name));
3500
3501                         /* point to the HBA attribute block */
3502                         size = 2 * sizeof(struct lpfc_name) +
3503                                 FOURBYTES;
3504                 } else {
3505                         size = sizeof(struct lpfc_name);
3506                 }
3507                 ab = (struct lpfc_fdmi_attr_block *)((uint8_t *)rh + size);
3508                 ab->EntryCnt = 0;
3509                 size += FOURBYTES;
3510                 bit_pos = 0;
3511                 if (new_mask)
3512                         mask = new_mask;
3513                 else
3514                         mask = vport->fdmi_hba_mask;
3515
3516                 /* Mask will dictate what attributes to build in the request */
3517                 while (mask) {
3518                         if (mask & 0x1) {
3519                                 func = lpfc_fdmi_hba_action[bit_pos];
3520                                 size += func(vport,
3521                                              (struct lpfc_fdmi_attr_def *)
3522                                              ((uint8_t *)rh + size));
3523                                 ab->EntryCnt++;
3524                                 if ((size + 256) >
3525                                     (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3526                                         goto hba_out;
3527                         }
3528                         mask = mask >> 1;
3529                         bit_pos++;
3530                 }
3531 hba_out:
3532                 ab->EntryCnt = cpu_to_be32(ab->EntryCnt);
3533                 /* Total size */
3534                 size = GID_REQUEST_SZ - 4 + size;
3535                 break;
3536
3537         case SLI_MGMT_RPRT:
3538         case SLI_MGMT_RPA:
3539                 pab = (struct lpfc_fdmi_reg_portattr *)&CtReq->un.PortID;
3540                 if (cmdcode == SLI_MGMT_RPRT) {
3541                         rh = (struct lpfc_fdmi_reg_hba *)pab;
3542                         /* HBA Identifier */
3543                         memcpy(&rh->hi.PortName,
3544                                &phba->pport->fc_sparam.portName,
3545                                sizeof(struct lpfc_name));
3546                         pab = (struct lpfc_fdmi_reg_portattr *)
3547                                 ((uint8_t *)pab +  sizeof(struct lpfc_name));
3548                 }
3549
3550                 memcpy((uint8_t *)&pab->PortName,
3551                        (uint8_t *)&vport->fc_sparam.portName,
3552                        sizeof(struct lpfc_name));
3553                 size += sizeof(struct lpfc_name) + FOURBYTES;
3554                 pab->ab.EntryCnt = 0;
3555                 bit_pos = 0;
3556                 if (new_mask)
3557                         mask = new_mask;
3558                 else
3559                         mask = vport->fdmi_port_mask;
3560
3561                 /* Mask will dictate what attributes to build in the request */
3562                 while (mask) {
3563                         if (mask & 0x1) {
3564                                 func = lpfc_fdmi_port_action[bit_pos];
3565                                 size += func(vport,
3566                                              (struct lpfc_fdmi_attr_def *)
3567                                              ((uint8_t *)pab + size));
3568                                 pab->ab.EntryCnt++;
3569                                 if ((size + 256) >
3570                                     (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3571                                         goto port_out;
3572                         }
3573                         mask = mask >> 1;
3574                         bit_pos++;
3575                 }
3576 port_out:
3577                 pab->ab.EntryCnt = cpu_to_be32(pab->ab.EntryCnt);
3578                 /* Total size */
3579                 if (cmdcode == SLI_MGMT_RPRT)
3580                         size += sizeof(struct lpfc_name);
3581                 size = GID_REQUEST_SZ - 4 + size;
3582                 break;
3583
3584         case SLI_MGMT_GHAT:
3585         case SLI_MGMT_GRPL:
3586                 rsp_size = FC_MAX_NS_RSP;
3587                 fallthrough;
3588         case SLI_MGMT_DHBA:
3589         case SLI_MGMT_DHAT:
3590                 pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3591                 memcpy((uint8_t *)&pe->PortName,
3592                        (uint8_t *)&vport->fc_sparam.portName,
3593                        sizeof(struct lpfc_name));
3594                 size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3595                 break;
3596
3597         case SLI_MGMT_GPAT:
3598         case SLI_MGMT_GPAS:
3599                 rsp_size = FC_MAX_NS_RSP;
3600                 fallthrough;
3601         case SLI_MGMT_DPRT:
3602         case SLI_MGMT_DPA:
3603                 pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3604                 memcpy((uint8_t *)&pe->PortName,
3605                        (uint8_t *)&vport->fc_sparam.portName,
3606                        sizeof(struct lpfc_name));
3607                 size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3608                 break;
3609         case SLI_MGMT_GRHL:
3610                 size = GID_REQUEST_SZ - 4;
3611                 break;
3612         default:
3613                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3614                                  "0298 FDMI cmdcode x%x not supported\n",
3615                                  cmdcode);
3616                 goto fdmi_cmd_free_bmpvirt;
3617         }
3618         CtReq->CommandResponse.bits.Size = cpu_to_be16(rsp_size);
3619
3620         bpl = (struct ulp_bde64 *)bmp->virt;
3621         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
3622         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
3623         bpl->tus.f.bdeFlags = 0;
3624         bpl->tus.f.bdeSize = size;
3625
3626         /*
3627          * The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
3628          * to hold ndlp reference for the corresponding callback function.
3629          */
3630         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, 0))
3631                 return 0;
3632
3633 fdmi_cmd_free_bmpvirt:
3634         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3635 fdmi_cmd_free_bmp:
3636         kfree(bmp);
3637 fdmi_cmd_free_mpvirt:
3638         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3639 fdmi_cmd_free_mp:
3640         kfree(mp);
3641 fdmi_cmd_exit:
3642         /* Issue FDMI request failed */
3643         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3644                          "0244 Issue FDMI request failed Data: x%x\n",
3645                          cmdcode);
3646         return 1;
3647 }
3648
3649 /**
3650  * lpfc_delayed_disc_tmo - Timeout handler for delayed discovery timer.
3651  * @t: Context object of the timer.
3652  *
3653  * This function set the WORKER_DELAYED_DISC_TMO flag and wake up
3654  * the worker thread.
3655  **/
3656 void
3657 lpfc_delayed_disc_tmo(struct timer_list *t)
3658 {
3659         struct lpfc_vport *vport = from_timer(vport, t, delayed_disc_tmo);
3660         struct lpfc_hba   *phba = vport->phba;
3661         uint32_t tmo_posted;
3662         unsigned long iflag;
3663
3664         spin_lock_irqsave(&vport->work_port_lock, iflag);
3665         tmo_posted = vport->work_port_events & WORKER_DELAYED_DISC_TMO;
3666         if (!tmo_posted)
3667                 vport->work_port_events |= WORKER_DELAYED_DISC_TMO;
3668         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3669
3670         if (!tmo_posted)
3671                 lpfc_worker_wake_up(phba);
3672         return;
3673 }
3674
3675 /**
3676  * lpfc_delayed_disc_timeout_handler - Function called by worker thread to
3677  *      handle delayed discovery.
3678  * @vport: pointer to a host virtual N_Port data structure.
3679  *
3680  * This function start nport discovery of the vport.
3681  **/
3682 void
3683 lpfc_delayed_disc_timeout_handler(struct lpfc_vport *vport)
3684 {
3685         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3686
3687         spin_lock_irq(shost->host_lock);
3688         if (!(vport->fc_flag & FC_DISC_DELAYED)) {
3689                 spin_unlock_irq(shost->host_lock);
3690                 return;
3691         }
3692         vport->fc_flag &= ~FC_DISC_DELAYED;
3693         spin_unlock_irq(shost->host_lock);
3694
3695         lpfc_do_scr_ns_plogi(vport->phba, vport);
3696 }
3697
3698 void
3699 lpfc_decode_firmware_rev(struct lpfc_hba *phba, char *fwrevision, int flag)
3700 {
3701         struct lpfc_sli *psli = &phba->sli;
3702         lpfc_vpd_t *vp = &phba->vpd;
3703         uint32_t b1, b2, b3, b4, i, rev;
3704         char c;
3705         uint32_t *ptr, str[4];
3706         uint8_t *fwname;
3707
3708         if (phba->sli_rev == LPFC_SLI_REV4)
3709                 snprintf(fwrevision, FW_REV_STR_SIZE, "%s", vp->rev.opFwName);
3710         else if (vp->rev.rBit) {
3711                 if (psli->sli_flag & LPFC_SLI_ACTIVE)
3712                         rev = vp->rev.sli2FwRev;
3713                 else
3714                         rev = vp->rev.sli1FwRev;
3715
3716                 b1 = (rev & 0x0000f000) >> 12;
3717                 b2 = (rev & 0x00000f00) >> 8;
3718                 b3 = (rev & 0x000000c0) >> 6;
3719                 b4 = (rev & 0x00000030) >> 4;
3720
3721                 switch (b4) {
3722                 case 0:
3723                         c = 'N';
3724                         break;
3725                 case 1:
3726                         c = 'A';
3727                         break;
3728                 case 2:
3729                         c = 'B';
3730                         break;
3731                 case 3:
3732                         c = 'X';
3733                         break;
3734                 default:
3735                         c = 0;
3736                         break;
3737                 }
3738                 b4 = (rev & 0x0000000f);
3739
3740                 if (psli->sli_flag & LPFC_SLI_ACTIVE)
3741                         fwname = vp->rev.sli2FwName;
3742                 else
3743                         fwname = vp->rev.sli1FwName;
3744
3745                 for (i = 0; i < 16; i++)
3746                         if (fwname[i] == 0x20)
3747                                 fwname[i] = 0;
3748
3749                 ptr = (uint32_t*)fwname;
3750
3751                 for (i = 0; i < 3; i++)
3752                         str[i] = be32_to_cpu(*ptr++);
3753
3754                 if (c == 0) {
3755                         if (flag)
3756                                 sprintf(fwrevision, "%d.%d%d (%s)",
3757                                         b1, b2, b3, (char *)str);
3758                         else
3759                                 sprintf(fwrevision, "%d.%d%d", b1,
3760                                         b2, b3);
3761                 } else {
3762                         if (flag)
3763                                 sprintf(fwrevision, "%d.%d%d%c%d (%s)",
3764                                         b1, b2, b3, c,
3765                                         b4, (char *)str);
3766                         else
3767                                 sprintf(fwrevision, "%d.%d%d%c%d",
3768                                         b1, b2, b3, c, b4);
3769                 }
3770         } else {
3771                 rev = vp->rev.smFwRev;
3772
3773                 b1 = (rev & 0xff000000) >> 24;
3774                 b2 = (rev & 0x00f00000) >> 20;
3775                 b3 = (rev & 0x000f0000) >> 16;
3776                 c  = (rev & 0x0000ff00) >> 8;
3777                 b4 = (rev & 0x000000ff);
3778
3779                 sprintf(fwrevision, "%d.%d%d%c%d", b1, b2, b3, c, b4);
3780         }
3781         return;
3782 }
3783
3784 static void
3785 lpfc_cmpl_ct_cmd_vmid(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3786                       struct lpfc_iocbq *rspiocb)
3787 {
3788         struct lpfc_vport *vport = cmdiocb->vport;
3789         struct lpfc_dmabuf *inp = cmdiocb->context1;
3790         struct lpfc_dmabuf *outp = cmdiocb->context2;
3791         struct lpfc_sli_ct_request *ctcmd = inp->virt;
3792         struct lpfc_sli_ct_request *ctrsp = outp->virt;
3793         u16 rsp = ctrsp->CommandResponse.bits.CmdRsp;
3794         struct app_id_object *app;
3795         u32 cmd, hash, bucket;
3796         struct lpfc_vmid *vmp, *cur;
3797         u8 *data = outp->virt;
3798         int i;
3799
3800         cmd = be16_to_cpu(ctcmd->CommandResponse.bits.CmdRsp);
3801         if (cmd == SLI_CTAS_DALLAPP_ID)
3802                 lpfc_ct_free_iocb(phba, cmdiocb);
3803
3804         if (lpfc_els_chk_latt(vport) || rspiocb->iocb.ulpStatus) {
3805                 if (cmd != SLI_CTAS_DALLAPP_ID)
3806                         return;
3807         }
3808         /* Check for a CT LS_RJT response */
3809         if (rsp == be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
3810                 if (cmd != SLI_CTAS_DALLAPP_ID)
3811                         lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3812                                          "3306 VMID FS_RJT Data: x%x x%x x%x\n",
3813                                          cmd, ctrsp->ReasonCode,
3814                                          ctrsp->Explanation);
3815                 if ((cmd != SLI_CTAS_DALLAPP_ID) ||
3816                     (ctrsp->ReasonCode != SLI_CT_UNABLE_TO_PERFORM_REQ) ||
3817                     (ctrsp->Explanation != SLI_CT_APP_ID_NOT_AVAILABLE)) {
3818                         /* If DALLAPP_ID failed retry later */
3819                         if (cmd == SLI_CTAS_DALLAPP_ID)
3820                                 vport->load_flag |= FC_DEREGISTER_ALL_APP_ID;
3821                         return;
3822                 }
3823         }
3824
3825         switch (cmd) {
3826         case SLI_CTAS_RAPP_IDENT:
3827                 app = (struct app_id_object *)(RAPP_IDENT_OFFSET + data);
3828                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3829                                  "6712 RAPP_IDENT app id %d  port id x%x id "
3830                                  "len %d\n", be32_to_cpu(app->app_id),
3831                                  be32_to_cpu(app->port_id),
3832                                  app->obj.entity_id_len);
3833
3834                 if (app->obj.entity_id_len == 0 || app->port_id == 0)
3835                         return;
3836
3837                 hash = lpfc_vmid_hash_fn(app->obj.entity_id,
3838                                          app->obj.entity_id_len);
3839                 vmp = lpfc_get_vmid_from_hashtable(vport, hash,
3840                                                   app->obj.entity_id);
3841                 if (vmp) {
3842                         write_lock(&vport->vmid_lock);
3843                         vmp->un.app_id = be32_to_cpu(app->app_id);
3844                         vmp->flag |= LPFC_VMID_REGISTERED;
3845                         vmp->flag &= ~LPFC_VMID_REQ_REGISTER;
3846                         write_unlock(&vport->vmid_lock);
3847                         /* Set IN USE flag */
3848                         vport->vmid_flag |= LPFC_VMID_IN_USE;
3849                 } else {
3850                         lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3851                                          "6901 No entry found %s hash %d\n",
3852                                          app->obj.entity_id, hash);
3853                 }
3854                 break;
3855         case SLI_CTAS_DAPP_IDENT:
3856                 app = (struct app_id_object *)(DAPP_IDENT_OFFSET + data);
3857                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3858                                  "6713 DAPP_IDENT app id %d  port id x%x\n",
3859                                  be32_to_cpu(app->app_id),
3860                                  be32_to_cpu(app->port_id));
3861                 break;
3862         case SLI_CTAS_DALLAPP_ID:
3863                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3864                                  "8856 Deregistered all app ids\n");
3865                 read_lock(&vport->vmid_lock);
3866                 for (i = 0; i < phba->cfg_max_vmid; i++) {
3867                         vmp = &vport->vmid[i];
3868                         if (vmp->flag != LPFC_VMID_SLOT_FREE)
3869                                 memset(vmp, 0, sizeof(struct lpfc_vmid));
3870                 }
3871                 read_unlock(&vport->vmid_lock);
3872                 /* for all elements in the hash table */
3873                 if (!hash_empty(vport->hash_table))
3874                         hash_for_each(vport->hash_table, bucket, cur, hnode)
3875                                 hash_del(&cur->hnode);
3876                 vport->load_flag |= FC_ALLOW_VMID;
3877                 break;
3878         default:
3879                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3880                                  "8857 Invalid command code\n");
3881         }
3882 }
3883
3884 /**
3885  * lpfc_vmid_cmd - Build and send a FDMI cmd to the specified NPort
3886  * @vport: pointer to a host virtual N_Port data structure.
3887  * @ndlp: ndlp to send FDMI cmd to (if NULL use FDMI_DID)
3888  * cmdcode: FDMI command to send
3889  * mask: Mask of HBA or PORT Attributes to send
3890  *
3891  * Builds and sends a FDMI command using the CT subsystem.
3892  */
3893 int
3894 lpfc_vmid_cmd(struct lpfc_vport *vport,
3895               int cmdcode, struct lpfc_vmid *vmid)
3896 {
3897         struct lpfc_hba *phba = vport->phba;
3898         struct lpfc_dmabuf *mp, *bmp;
3899         struct lpfc_sli_ct_request *ctreq;
3900         struct ulp_bde64 *bpl;
3901         u32 size;
3902         u32 rsp_size;
3903         u8 *data;
3904         struct lpfc_vmid_rapp_ident_list *rap;
3905         struct lpfc_vmid_dapp_ident_list *dap;
3906         u8 retry = 0;
3907         struct lpfc_nodelist *ndlp;
3908
3909         void (*cmpl)(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3910                      struct lpfc_iocbq *rspiocb);
3911
3912         ndlp = lpfc_findnode_did(vport, FDMI_DID);
3913         if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
3914                 return 0;
3915
3916         cmpl = lpfc_cmpl_ct_cmd_vmid;
3917
3918         /* fill in BDEs for command */
3919         /* Allocate buffer for command payload */
3920         mp = kmalloc(sizeof(*mp), GFP_KERNEL);
3921         if (!mp)
3922                 goto vmid_free_mp_exit;
3923
3924         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3925         if (!mp->virt)
3926                 goto vmid_free_mp_virt_exit;
3927
3928         /* Allocate buffer for Buffer ptr list */
3929         bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
3930         if (!bmp)
3931                 goto vmid_free_bmp_exit;
3932
3933         bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
3934         if (!bmp->virt)
3935                 goto vmid_free_bmp_virt_exit;
3936
3937         INIT_LIST_HEAD(&mp->list);
3938         INIT_LIST_HEAD(&bmp->list);
3939
3940         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3941                          "3275 VMID Request Data: x%x x%x x%x\n",
3942                          vport->fc_flag, vport->port_state, cmdcode);
3943         ctreq = (struct lpfc_sli_ct_request *)mp->virt;
3944         data = mp->virt;
3945         /* First populate the CT_IU preamble */
3946         memset(data, 0, LPFC_BPL_SIZE);
3947         ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3948         ctreq->RevisionId.bits.InId = 0;
3949
3950         ctreq->FsType = SLI_CT_MANAGEMENT_SERVICE;
3951         ctreq->FsSubType = SLI_CT_APP_SEV_Subtypes;
3952
3953         ctreq->CommandResponse.bits.CmdRsp = cpu_to_be16(cmdcode);
3954         rsp_size = LPFC_BPL_SIZE;
3955         size = 0;
3956
3957         switch (cmdcode) {
3958         case SLI_CTAS_RAPP_IDENT:
3959                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3960                                  "1329 RAPP_IDENT for %s\n", vmid->host_vmid);
3961                 ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
3962                 rap = (struct lpfc_vmid_rapp_ident_list *)
3963                         (DAPP_IDENT_OFFSET + data);
3964                 rap->no_of_objects = cpu_to_be32(1);
3965                 rap->obj[0].entity_id_len = vmid->vmid_len;
3966                 memcpy(rap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
3967                 size = RAPP_IDENT_OFFSET +
3968                         sizeof(struct lpfc_vmid_rapp_ident_list);
3969                 retry = 1;
3970                 break;
3971
3972         case SLI_CTAS_GALLAPPIA_ID:
3973                 ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
3974                 size = GALLAPPIA_ID_SIZE;
3975                 break;
3976
3977         case SLI_CTAS_DAPP_IDENT:
3978                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
3979                                  "1469 DAPP_IDENT for %s\n", vmid->host_vmid);
3980                 ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
3981                 dap = (struct lpfc_vmid_dapp_ident_list *)
3982                         (DAPP_IDENT_OFFSET + data);
3983                 dap->no_of_objects = cpu_to_be32(1);
3984                 dap->obj[0].entity_id_len = vmid->vmid_len;
3985                 memcpy(dap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
3986                 size = DAPP_IDENT_OFFSET +
3987                         sizeof(struct lpfc_vmid_dapp_ident_list);
3988                 write_lock(&vport->vmid_lock);
3989                 vmid->flag &= ~LPFC_VMID_REGISTERED;
3990                 write_unlock(&vport->vmid_lock);
3991                 retry = 1;
3992                 break;
3993
3994         case SLI_CTAS_DALLAPP_ID:
3995                 ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
3996                 size = DALLAPP_ID_SIZE;
3997                 break;
3998
3999         default:
4000                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
4001                                  "7062 VMID cmdcode x%x not supported\n",
4002                                  cmdcode);
4003                 goto vmid_free_all_mem;
4004         }
4005
4006         ctreq->CommandResponse.bits.Size = cpu_to_be16(rsp_size);
4007
4008         bpl = (struct ulp_bde64 *)bmp->virt;
4009         bpl->addrHigh = putPaddrHigh(mp->phys);
4010         bpl->addrLow = putPaddrLow(mp->phys);
4011         bpl->tus.f.bdeFlags = 0;
4012         bpl->tus.f.bdeSize = size;
4013
4014         /* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
4015          * to hold ndlp reference for the corresponding callback function.
4016          */
4017         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry))
4018                 return 0;
4019
4020  vmid_free_all_mem:
4021         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
4022  vmid_free_bmp_virt_exit:
4023         kfree(bmp);
4024  vmid_free_bmp_exit:
4025         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4026  vmid_free_mp_virt_exit:
4027         kfree(mp);
4028  vmid_free_mp_exit:
4029
4030         /* Issue CT request failed */
4031         lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
4032                          "3276 VMID CT request failed Data: x%x\n", cmdcode);
4033         return -EIO;
4034 }