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