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