GNU Linux-libre 5.15.54-gnu
[releases.git] / drivers / scsi / lpfc / lpfc_hbadisc.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2021 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23
24 #include <linux/blkdev.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/pci.h>
28 #include <linux/kthread.h>
29 #include <linux/interrupt.h>
30 #include <linux/lockdep.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_nl.h"
42 #include "lpfc_disc.h"
43 #include "lpfc_sli.h"
44 #include "lpfc_sli4.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_vport.h"
51 #include "lpfc_debugfs.h"
52
53 /* AlpaArray for assignment of scsid for scan-down and bind_method */
54 static uint8_t lpfcAlpaArray[] = {
55         0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
56         0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
57         0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
58         0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
59         0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
60         0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
61         0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
62         0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
63         0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
64         0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
65         0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
66         0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
67         0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
68 };
69
70 static void lpfc_disc_timeout_handler(struct lpfc_vport *);
71 static void lpfc_disc_flush_list(struct lpfc_vport *vport);
72 static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
73 static int lpfc_fcf_inuse(struct lpfc_hba *);
74 static void lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *, LPFC_MBOXQ_t *);
75 static void lpfc_check_inactive_vmid(struct lpfc_hba *phba);
76 static void lpfc_check_vmid_qfpa_issue(struct lpfc_hba *phba);
77
78 static int
79 lpfc_valid_xpt_node(struct lpfc_nodelist *ndlp)
80 {
81         if (ndlp->nlp_fc4_type ||
82             ndlp->nlp_type & NLP_FABRIC)
83                 return 1;
84         return 0;
85 }
86 /* The source of a terminate rport I/O is either a dev_loss_tmo
87  * event or a call to fc_remove_host.  While the rport should be
88  * valid during these downcalls, the transport can call twice
89  * in a single event.  This routine provides somoe protection
90  * as the NDLP isn't really free, just released to the pool.
91  */
92 static int
93 lpfc_rport_invalid(struct fc_rport *rport)
94 {
95         struct lpfc_rport_data *rdata;
96         struct lpfc_nodelist *ndlp;
97
98         if (!rport) {
99                 pr_err("**** %s: NULL rport, exit.\n", __func__);
100                 return -EINVAL;
101         }
102
103         rdata = rport->dd_data;
104         if (!rdata) {
105                 pr_err("**** %s: NULL dd_data on rport x%px SID x%x\n",
106                        __func__, rport, rport->scsi_target_id);
107                 return -EINVAL;
108         }
109
110         ndlp = rdata->pnode;
111         if (!rdata->pnode) {
112                 pr_err("**** %s: NULL ndlp on rport x%px SID x%x\n",
113                        __func__, rport, rport->scsi_target_id);
114                 return -EINVAL;
115         }
116
117         if (!ndlp->vport) {
118                 pr_err("**** %s: Null vport on ndlp x%px, DID x%x rport x%px "
119                        "SID x%x\n", __func__, ndlp, ndlp->nlp_DID, rport,
120                        rport->scsi_target_id);
121                 return -EINVAL;
122         }
123         return 0;
124 }
125
126 void
127 lpfc_terminate_rport_io(struct fc_rport *rport)
128 {
129         struct lpfc_rport_data *rdata;
130         struct lpfc_nodelist *ndlp;
131         struct lpfc_vport *vport;
132
133         if (lpfc_rport_invalid(rport))
134                 return;
135
136         rdata = rport->dd_data;
137         ndlp = rdata->pnode;
138         vport = ndlp->vport;
139         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
140                               "rport terminate: sid:x%x did:x%x flg:x%x",
141                               ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
142
143         if (ndlp->nlp_sid != NLP_NO_SID)
144                 lpfc_sli_abort_iocb(vport, ndlp->nlp_sid, 0, LPFC_CTX_TGT);
145 }
146
147 /*
148  * This function will be called when dev_loss_tmo fire.
149  */
150 void
151 lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
152 {
153         struct lpfc_nodelist *ndlp;
154         struct lpfc_vport *vport;
155         struct lpfc_hba   *phba;
156         struct lpfc_work_evt *evtp;
157         unsigned long iflags;
158
159         ndlp = ((struct lpfc_rport_data *)rport->dd_data)->pnode;
160         if (!ndlp)
161                 return;
162
163         vport = ndlp->vport;
164         phba  = vport->phba;
165
166         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
167                 "rport devlosscb: sid:x%x did:x%x flg:x%x",
168                 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
169
170         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
171                          "3181 dev_loss_callbk x%06x, rport x%px flg x%x "
172                          "load_flag x%x refcnt %d\n",
173                          ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag,
174                          vport->load_flag, kref_read(&ndlp->kref));
175
176         /* Don't schedule a worker thread event if the vport is going down.
177          * The teardown process cleans up the node via lpfc_drop_node.
178          */
179         if (vport->load_flag & FC_UNLOADING) {
180                 ((struct lpfc_rport_data *)rport->dd_data)->pnode = NULL;
181                 ndlp->rport = NULL;
182
183                 ndlp->fc4_xpt_flags &= ~SCSI_XPT_REGD;
184
185                 /* Remove the node reference from remote_port_add now.
186                  * The driver will not call remote_port_delete.
187                  */
188                 lpfc_nlp_put(ndlp);
189                 return;
190         }
191
192         if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
193                 return;
194
195         if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn))
196                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
197                                  "6789 rport name %llx != node port name %llx",
198                                  rport->port_name,
199                                  wwn_to_u64(ndlp->nlp_portname.u.wwn));
200
201         evtp = &ndlp->dev_loss_evt;
202
203         if (!list_empty(&evtp->evt_listp)) {
204                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
205                                  "6790 rport name %llx dev_loss_evt pending\n",
206                                  rport->port_name);
207                 return;
208         }
209
210         spin_lock_irqsave(&ndlp->lock, iflags);
211         ndlp->nlp_flag |= NLP_IN_DEV_LOSS;
212
213         /* If there is a PLOGI in progress, and we are in a
214          * NLP_NPR_2B_DISC state, don't turn off the flag.
215          */
216         if (ndlp->nlp_state != NLP_STE_PLOGI_ISSUE)
217                 ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
218
219         /*
220          * The backend does not expect any more calls associated with this
221          * rport. Remove the association between rport and ndlp.
222          */
223         ndlp->fc4_xpt_flags &= ~SCSI_XPT_REGD;
224         ((struct lpfc_rport_data *)rport->dd_data)->pnode = NULL;
225         ndlp->rport = NULL;
226         spin_unlock_irqrestore(&ndlp->lock, iflags);
227
228         /* We need to hold the node by incrementing the reference
229          * count until this queued work is done
230          */
231         evtp->evt_arg1 = lpfc_nlp_get(ndlp);
232
233         spin_lock_irqsave(&phba->hbalock, iflags);
234         if (evtp->evt_arg1) {
235                 evtp->evt = LPFC_EVT_DEV_LOSS;
236                 list_add_tail(&evtp->evt_listp, &phba->work_list);
237                 lpfc_worker_wake_up(phba);
238         }
239         spin_unlock_irqrestore(&phba->hbalock, iflags);
240
241         return;
242 }
243
244 /**
245  * lpfc_check_inactive_vmid_one - VMID inactivity checker for a vport
246  * @vport: Pointer to vport context object.
247  *
248  * This function checks for idle VMID entries related to a particular vport. If
249  * found unused/idle, free them accordingly.
250  **/
251 static void lpfc_check_inactive_vmid_one(struct lpfc_vport *vport)
252 {
253         u16 keep;
254         u32 difftime = 0, r, bucket;
255         u64 *lta;
256         int cpu;
257         struct lpfc_vmid *vmp;
258
259         write_lock(&vport->vmid_lock);
260
261         if (!vport->cur_vmid_cnt)
262                 goto out;
263
264         /* iterate through the table */
265         hash_for_each(vport->hash_table, bucket, vmp, hnode) {
266                 keep = 0;
267                 if (vmp->flag & LPFC_VMID_REGISTERED) {
268                         /* check if the particular VMID is in use */
269                         /* for all available per cpu variable */
270                         for_each_possible_cpu(cpu) {
271                                 /* if last access time is less than timeout */
272                                 lta = per_cpu_ptr(vmp->last_io_time, cpu);
273                                 if (!lta)
274                                         continue;
275                                 difftime = (jiffies) - (*lta);
276                                 if ((vport->vmid_inactivity_timeout *
277                                      JIFFIES_PER_HR) > difftime) {
278                                         keep = 1;
279                                         break;
280                                 }
281                         }
282
283                         /* if none of the cpus have been used by the vm, */
284                         /*  remove the entry if already registered */
285                         if (!keep) {
286                                 /* mark the entry for deregistration */
287                                 vmp->flag = LPFC_VMID_DE_REGISTER;
288                                 write_unlock(&vport->vmid_lock);
289                                 if (vport->vmid_priority_tagging)
290                                         r = lpfc_vmid_uvem(vport, vmp, false);
291                                 else
292                                         r = lpfc_vmid_cmd(vport,
293                                                           SLI_CTAS_DAPP_IDENT,
294                                                           vmp);
295
296                                 /* decrement number of active vms and mark */
297                                 /* entry in slot as free */
298                                 write_lock(&vport->vmid_lock);
299                                 if (!r) {
300                                         struct lpfc_vmid *ht = vmp;
301
302                                         vport->cur_vmid_cnt--;
303                                         ht->flag = LPFC_VMID_SLOT_FREE;
304                                         free_percpu(ht->last_io_time);
305                                         ht->last_io_time = NULL;
306                                         hash_del(&ht->hnode);
307                                 }
308                         }
309                 }
310         }
311  out:
312         write_unlock(&vport->vmid_lock);
313 }
314
315 /**
316  * lpfc_check_inactive_vmid - VMID inactivity checker
317  * @phba: Pointer to hba context object.
318  *
319  * This function is called from the worker thread to determine if an entry in
320  * the VMID table can be released since there was no I/O activity seen from that
321  * particular VM for the specified time. When this happens, the entry in the
322  * table is released and also the resources on the switch cleared.
323  **/
324
325 static void lpfc_check_inactive_vmid(struct lpfc_hba *phba)
326 {
327         struct lpfc_vport *vport;
328         struct lpfc_vport **vports;
329         int i;
330
331         vports = lpfc_create_vport_work_array(phba);
332         if (!vports)
333                 return;
334
335         for (i = 0; i <= phba->max_vports; i++) {
336                 if ((!vports[i]) && (i == 0))
337                         vport = phba->pport;
338                 else
339                         vport = vports[i];
340                 if (!vport)
341                         break;
342
343                 lpfc_check_inactive_vmid_one(vport);
344         }
345         lpfc_destroy_vport_work_array(phba, vports);
346 }
347
348 /**
349  * lpfc_check_nlp_post_devloss - Check to restore ndlp refcnt after devloss
350  * @vport: Pointer to vport object.
351  * @ndlp: Pointer to remote node object.
352  *
353  * If NLP_IN_RECOV_POST_DEV_LOSS flag was set due to outstanding recovery of
354  * node during dev_loss_tmo processing, then this function restores the nlp_put
355  * kref decrement from lpfc_dev_loss_tmo_handler.
356  **/
357 void
358 lpfc_check_nlp_post_devloss(struct lpfc_vport *vport,
359                             struct lpfc_nodelist *ndlp)
360 {
361         unsigned long iflags;
362
363         spin_lock_irqsave(&ndlp->lock, iflags);
364         if (ndlp->save_flags & NLP_IN_RECOV_POST_DEV_LOSS) {
365                 ndlp->save_flags &= ~NLP_IN_RECOV_POST_DEV_LOSS;
366                 spin_unlock_irqrestore(&ndlp->lock, iflags);
367                 lpfc_nlp_get(ndlp);
368                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY | LOG_NODE,
369                                  "8438 Devloss timeout reversed on DID x%x "
370                                  "refcnt %d ndlp %p flag x%x "
371                                  "port_state = x%x\n",
372                                  ndlp->nlp_DID, kref_read(&ndlp->kref), ndlp,
373                                  ndlp->nlp_flag, vport->port_state);
374                 spin_lock_irqsave(&ndlp->lock, iflags);
375         }
376         spin_unlock_irqrestore(&ndlp->lock, iflags);
377 }
378
379 /**
380  * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
381  * @ndlp: Pointer to remote node object.
382  *
383  * This function is called from the worker thread when devloss timeout timer
384  * expires. For SLI4 host, this routine shall return 1 when at lease one
385  * remote node, including this @ndlp, is still in use of FCF; otherwise, this
386  * routine shall return 0 when there is no remote node is still in use of FCF
387  * when devloss timeout happened to this @ndlp.
388  **/
389 static int
390 lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
391 {
392         struct lpfc_vport *vport;
393         struct lpfc_hba   *phba;
394         uint8_t *name;
395         int warn_on = 0;
396         int fcf_inuse = 0;
397         bool recovering = false;
398         struct fc_vport *fc_vport = NULL;
399         unsigned long iflags;
400
401         vport = ndlp->vport;
402         name = (uint8_t *)&ndlp->nlp_portname;
403         phba = vport->phba;
404
405         spin_lock_irqsave(&ndlp->lock, iflags);
406         ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
407         spin_unlock_irqrestore(&ndlp->lock, iflags);
408
409         if (phba->sli_rev == LPFC_SLI_REV4)
410                 fcf_inuse = lpfc_fcf_inuse(phba);
411
412         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
413                               "rport devlosstmo:did:x%x type:x%x id:x%x",
414                               ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_sid);
415
416         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
417                          "3182 %s x%06x, nflag x%x xflags x%x refcnt %d\n",
418                          __func__, ndlp->nlp_DID, ndlp->nlp_flag,
419                          ndlp->fc4_xpt_flags, kref_read(&ndlp->kref));
420
421         /* If the driver is recovering the rport, ignore devloss. */
422         if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
423                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
424                                  "0284 Devloss timeout Ignored on "
425                                  "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
426                                  "NPort x%x\n",
427                                  *name, *(name+1), *(name+2), *(name+3),
428                                  *(name+4), *(name+5), *(name+6), *(name+7),
429                                  ndlp->nlp_DID);
430                 return fcf_inuse;
431         }
432
433         /* Fabric nodes are done. */
434         if (ndlp->nlp_type & NLP_FABRIC) {
435                 spin_lock_irqsave(&ndlp->lock, iflags);
436                 /* In massive vport configuration settings, it's possible
437                  * dev_loss_tmo fired during node recovery.  So, check if
438                  * fabric nodes are in discovery states outstanding.
439                  */
440                 switch (ndlp->nlp_DID) {
441                 case Fabric_DID:
442                         fc_vport = vport->fc_vport;
443                         if (fc_vport &&
444                             fc_vport->vport_state == FC_VPORT_INITIALIZING)
445                                 recovering = true;
446                         break;
447                 case Fabric_Cntl_DID:
448                         if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
449                                 recovering = true;
450                         break;
451                 case FDMI_DID:
452                         fallthrough;
453                 case NameServer_DID:
454                         if (ndlp->nlp_state >= NLP_STE_PLOGI_ISSUE &&
455                             ndlp->nlp_state <= NLP_STE_REG_LOGIN_ISSUE)
456                                 recovering = true;
457                         break;
458                 }
459                 spin_unlock_irqrestore(&ndlp->lock, iflags);
460
461                 /* Mark an NLP_IN_RECOV_POST_DEV_LOSS flag to know if reversing
462                  * the following lpfc_nlp_put is necessary after fabric node is
463                  * recovered.
464                  */
465                 if (recovering) {
466                         lpfc_printf_vlog(vport, KERN_INFO,
467                                          LOG_DISCOVERY | LOG_NODE,
468                                          "8436 Devloss timeout marked on "
469                                          "DID x%x refcnt %d ndlp %p "
470                                          "flag x%x port_state = x%x\n",
471                                          ndlp->nlp_DID, kref_read(&ndlp->kref),
472                                          ndlp, ndlp->nlp_flag,
473                                          vport->port_state);
474                         spin_lock_irqsave(&ndlp->lock, iflags);
475                         ndlp->save_flags |= NLP_IN_RECOV_POST_DEV_LOSS;
476                         spin_unlock_irqrestore(&ndlp->lock, iflags);
477                 } else if (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
478                         /* Fabric node fully recovered before this dev_loss_tmo
479                          * queue work is processed.  Thus, ignore the
480                          * dev_loss_tmo event.
481                          */
482                         lpfc_printf_vlog(vport, KERN_INFO,
483                                          LOG_DISCOVERY | LOG_NODE,
484                                          "8437 Devloss timeout ignored on "
485                                          "DID x%x refcnt %d ndlp %p "
486                                          "flag x%x port_state = x%x\n",
487                                          ndlp->nlp_DID, kref_read(&ndlp->kref),
488                                          ndlp, ndlp->nlp_flag,
489                                          vport->port_state);
490                         return fcf_inuse;
491                 }
492
493                 lpfc_nlp_put(ndlp);
494                 return fcf_inuse;
495         }
496
497         if (ndlp->nlp_sid != NLP_NO_SID) {
498                 warn_on = 1;
499                 lpfc_sli_abort_iocb(vport, ndlp->nlp_sid, 0, LPFC_CTX_TGT);
500         }
501
502         if (warn_on) {
503                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
504                                  "0203 Devloss timeout on "
505                                  "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
506                                  "NPort x%06x Data: x%x x%x x%x\n",
507                                  *name, *(name+1), *(name+2), *(name+3),
508                                  *(name+4), *(name+5), *(name+6), *(name+7),
509                                  ndlp->nlp_DID, ndlp->nlp_flag,
510                                  ndlp->nlp_state, ndlp->nlp_rpi);
511         } else {
512                 lpfc_printf_vlog(vport, KERN_INFO, LOG_TRACE_EVENT,
513                                  "0204 Devloss timeout on "
514                                  "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
515                                  "NPort x%06x Data: x%x x%x x%x\n",
516                                  *name, *(name+1), *(name+2), *(name+3),
517                                  *(name+4), *(name+5), *(name+6), *(name+7),
518                                  ndlp->nlp_DID, ndlp->nlp_flag,
519                                  ndlp->nlp_state, ndlp->nlp_rpi);
520         }
521
522         /* If we are devloss, but we are in the process of rediscovering the
523          * ndlp, don't issue a NLP_EVT_DEVICE_RM event.
524          */
525         if (ndlp->nlp_state >= NLP_STE_PLOGI_ISSUE &&
526             ndlp->nlp_state <= NLP_STE_PRLI_ISSUE) {
527                 return fcf_inuse;
528         }
529
530         if (!(ndlp->fc4_xpt_flags & NVME_XPT_REGD))
531                 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
532
533         return fcf_inuse;
534 }
535
536 static void lpfc_check_vmid_qfpa_issue(struct lpfc_hba *phba)
537 {
538         struct lpfc_vport *vport;
539         struct lpfc_vport **vports;
540         int i;
541
542         vports = lpfc_create_vport_work_array(phba);
543         if (!vports)
544                 return;
545
546         for (i = 0; i <= phba->max_vports; i++) {
547                 if ((!vports[i]) && (i == 0))
548                         vport = phba->pport;
549                 else
550                         vport = vports[i];
551                 if (!vport)
552                         break;
553
554                 if (vport->vmid_flag & LPFC_VMID_ISSUE_QFPA) {
555                         if (!lpfc_issue_els_qfpa(vport))
556                                 vport->vmid_flag &= ~LPFC_VMID_ISSUE_QFPA;
557                 }
558         }
559         lpfc_destroy_vport_work_array(phba, vports);
560 }
561
562 /**
563  * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
564  * @phba: Pointer to hba context object.
565  * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
566  * @nlp_did: remote node identifer with devloss timeout.
567  *
568  * This function is called from the worker thread after invoking devloss
569  * timeout handler and releasing the reference count for the ndlp with
570  * which the devloss timeout was handled for SLI4 host. For the devloss
571  * timeout of the last remote node which had been in use of FCF, when this
572  * routine is invoked, it shall be guaranteed that none of the remote are
573  * in-use of FCF. When devloss timeout to the last remote using the FCF,
574  * if the FIP engine is neither in FCF table scan process nor roundrobin
575  * failover process, the in-use FCF shall be unregistered. If the FIP
576  * engine is in FCF discovery process, the devloss timeout state shall
577  * be set for either the FCF table scan process or roundrobin failover
578  * process to unregister the in-use FCF.
579  **/
580 static void
581 lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
582                                     uint32_t nlp_did)
583 {
584         /* If devloss timeout happened to a remote node when FCF had no
585          * longer been in-use, do nothing.
586          */
587         if (!fcf_inuse)
588                 return;
589
590         if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
591                 spin_lock_irq(&phba->hbalock);
592                 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
593                         if (phba->hba_flag & HBA_DEVLOSS_TMO) {
594                                 spin_unlock_irq(&phba->hbalock);
595                                 return;
596                         }
597                         phba->hba_flag |= HBA_DEVLOSS_TMO;
598                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
599                                         "2847 Last remote node (x%x) using "
600                                         "FCF devloss tmo\n", nlp_did);
601                 }
602                 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
603                         spin_unlock_irq(&phba->hbalock);
604                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
605                                         "2868 Devloss tmo to FCF rediscovery "
606                                         "in progress\n");
607                         return;
608                 }
609                 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
610                         spin_unlock_irq(&phba->hbalock);
611                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
612                                         "2869 Devloss tmo to idle FIP engine, "
613                                         "unreg in-use FCF and rescan.\n");
614                         /* Unregister in-use FCF and rescan */
615                         lpfc_unregister_fcf_rescan(phba);
616                         return;
617                 }
618                 spin_unlock_irq(&phba->hbalock);
619                 if (phba->hba_flag & FCF_TS_INPROG)
620                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
621                                         "2870 FCF table scan in progress\n");
622                 if (phba->hba_flag & FCF_RR_INPROG)
623                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
624                                         "2871 FLOGI roundrobin FCF failover "
625                                         "in progress\n");
626         }
627         lpfc_unregister_unused_fcf(phba);
628 }
629
630 /**
631  * lpfc_alloc_fast_evt - Allocates data structure for posting event
632  * @phba: Pointer to hba context object.
633  *
634  * This function is called from the functions which need to post
635  * events from interrupt context. This function allocates data
636  * structure required for posting event. It also keeps track of
637  * number of events pending and prevent event storm when there are
638  * too many events.
639  **/
640 struct lpfc_fast_path_event *
641 lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
642         struct lpfc_fast_path_event *ret;
643
644         /* If there are lot of fast event do not exhaust memory due to this */
645         if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
646                 return NULL;
647
648         ret = kzalloc(sizeof(struct lpfc_fast_path_event),
649                         GFP_ATOMIC);
650         if (ret) {
651                 atomic_inc(&phba->fast_event_count);
652                 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
653                 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
654         }
655         return ret;
656 }
657
658 /**
659  * lpfc_free_fast_evt - Frees event data structure
660  * @phba: Pointer to hba context object.
661  * @evt:  Event object which need to be freed.
662  *
663  * This function frees the data structure required for posting
664  * events.
665  **/
666 void
667 lpfc_free_fast_evt(struct lpfc_hba *phba,
668                 struct lpfc_fast_path_event *evt) {
669
670         atomic_dec(&phba->fast_event_count);
671         kfree(evt);
672 }
673
674 /**
675  * lpfc_send_fastpath_evt - Posts events generated from fast path
676  * @phba: Pointer to hba context object.
677  * @evtp: Event data structure.
678  *
679  * This function is called from worker thread, when the interrupt
680  * context need to post an event. This function posts the event
681  * to fc transport netlink interface.
682  **/
683 static void
684 lpfc_send_fastpath_evt(struct lpfc_hba *phba,
685                 struct lpfc_work_evt *evtp)
686 {
687         unsigned long evt_category, evt_sub_category;
688         struct lpfc_fast_path_event *fast_evt_data;
689         char *evt_data;
690         uint32_t evt_data_size;
691         struct Scsi_Host *shost;
692
693         fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
694                 work_evt);
695
696         evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
697         evt_sub_category = (unsigned long) fast_evt_data->un.
698                         fabric_evt.subcategory;
699         shost = lpfc_shost_from_vport(fast_evt_data->vport);
700         if (evt_category == FC_REG_FABRIC_EVENT) {
701                 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
702                         evt_data = (char *) &fast_evt_data->un.read_check_error;
703                         evt_data_size = sizeof(fast_evt_data->un.
704                                 read_check_error);
705                 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
706                         (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
707                         evt_data = (char *) &fast_evt_data->un.fabric_evt;
708                         evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
709                 } else {
710                         lpfc_free_fast_evt(phba, fast_evt_data);
711                         return;
712                 }
713         } else if (evt_category == FC_REG_SCSI_EVENT) {
714                 switch (evt_sub_category) {
715                 case LPFC_EVENT_QFULL:
716                 case LPFC_EVENT_DEVBSY:
717                         evt_data = (char *) &fast_evt_data->un.scsi_evt;
718                         evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
719                         break;
720                 case LPFC_EVENT_CHECK_COND:
721                         evt_data = (char *) &fast_evt_data->un.check_cond_evt;
722                         evt_data_size =  sizeof(fast_evt_data->un.
723                                 check_cond_evt);
724                         break;
725                 case LPFC_EVENT_VARQUEDEPTH:
726                         evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
727                         evt_data_size = sizeof(fast_evt_data->un.
728                                 queue_depth_evt);
729                         break;
730                 default:
731                         lpfc_free_fast_evt(phba, fast_evt_data);
732                         return;
733                 }
734         } else {
735                 lpfc_free_fast_evt(phba, fast_evt_data);
736                 return;
737         }
738
739         if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
740                 fc_host_post_vendor_event(shost,
741                         fc_get_event_number(),
742                         evt_data_size,
743                         evt_data,
744                         LPFC_NL_VENDOR_ID);
745
746         lpfc_free_fast_evt(phba, fast_evt_data);
747         return;
748 }
749
750 static void
751 lpfc_work_list_done(struct lpfc_hba *phba)
752 {
753         struct lpfc_work_evt  *evtp = NULL;
754         struct lpfc_nodelist  *ndlp;
755         int free_evt;
756         int fcf_inuse;
757         uint32_t nlp_did;
758
759         spin_lock_irq(&phba->hbalock);
760         while (!list_empty(&phba->work_list)) {
761                 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
762                                  evt_listp);
763                 spin_unlock_irq(&phba->hbalock);
764                 free_evt = 1;
765                 switch (evtp->evt) {
766                 case LPFC_EVT_ELS_RETRY:
767                         ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
768                         lpfc_els_retry_delay_handler(ndlp);
769                         free_evt = 0; /* evt is part of ndlp */
770                         /* decrement the node reference count held
771                          * for this queued work
772                          */
773                         lpfc_nlp_put(ndlp);
774                         break;
775                 case LPFC_EVT_DEV_LOSS:
776                         ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
777                         fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
778                         free_evt = 0;
779                         /* decrement the node reference count held for
780                          * this queued work
781                          */
782                         nlp_did = ndlp->nlp_DID;
783                         lpfc_nlp_put(ndlp);
784                         if (phba->sli_rev == LPFC_SLI_REV4)
785                                 lpfc_sli4_post_dev_loss_tmo_handler(phba,
786                                                                     fcf_inuse,
787                                                                     nlp_did);
788                         break;
789                 case LPFC_EVT_RECOVER_PORT:
790                         ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
791                         lpfc_sli_abts_recover_port(ndlp->vport, ndlp);
792                         free_evt = 0;
793                         /* decrement the node reference count held for
794                          * this queued work
795                          */
796                         lpfc_nlp_put(ndlp);
797                         break;
798                 case LPFC_EVT_ONLINE:
799                         if (phba->link_state < LPFC_LINK_DOWN)
800                                 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
801                         else
802                                 *(int *) (evtp->evt_arg1) = 0;
803                         complete((struct completion *)(evtp->evt_arg2));
804                         break;
805                 case LPFC_EVT_OFFLINE_PREP:
806                         if (phba->link_state >= LPFC_LINK_DOWN)
807                                 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
808                         *(int *)(evtp->evt_arg1) = 0;
809                         complete((struct completion *)(evtp->evt_arg2));
810                         break;
811                 case LPFC_EVT_OFFLINE:
812                         lpfc_offline(phba);
813                         lpfc_sli_brdrestart(phba);
814                         *(int *)(evtp->evt_arg1) =
815                                 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
816                         lpfc_unblock_mgmt_io(phba);
817                         complete((struct completion *)(evtp->evt_arg2));
818                         break;
819                 case LPFC_EVT_WARM_START:
820                         lpfc_offline(phba);
821                         lpfc_reset_barrier(phba);
822                         lpfc_sli_brdreset(phba);
823                         lpfc_hba_down_post(phba);
824                         *(int *)(evtp->evt_arg1) =
825                                 lpfc_sli_brdready(phba, HS_MBRDY);
826                         lpfc_unblock_mgmt_io(phba);
827                         complete((struct completion *)(evtp->evt_arg2));
828                         break;
829                 case LPFC_EVT_KILL:
830                         lpfc_offline(phba);
831                         *(int *)(evtp->evt_arg1)
832                                 = (phba->pport->stopped)
833                                         ? 0 : lpfc_sli_brdkill(phba);
834                         lpfc_unblock_mgmt_io(phba);
835                         complete((struct completion *)(evtp->evt_arg2));
836                         break;
837                 case LPFC_EVT_FASTPATH_MGMT_EVT:
838                         lpfc_send_fastpath_evt(phba, evtp);
839                         free_evt = 0;
840                         break;
841                 case LPFC_EVT_RESET_HBA:
842                         if (!(phba->pport->load_flag & FC_UNLOADING))
843                                 lpfc_reset_hba(phba);
844                         break;
845                 }
846                 if (free_evt)
847                         kfree(evtp);
848                 spin_lock_irq(&phba->hbalock);
849         }
850         spin_unlock_irq(&phba->hbalock);
851
852 }
853
854 static void
855 lpfc_work_done(struct lpfc_hba *phba)
856 {
857         struct lpfc_sli_ring *pring;
858         uint32_t ha_copy, status, control, work_port_events;
859         struct lpfc_vport **vports;
860         struct lpfc_vport *vport;
861         int i;
862
863         spin_lock_irq(&phba->hbalock);
864         ha_copy = phba->work_ha;
865         phba->work_ha = 0;
866         spin_unlock_irq(&phba->hbalock);
867
868         /* First, try to post the next mailbox command to SLI4 device */
869         if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
870                 lpfc_sli4_post_async_mbox(phba);
871
872         if (ha_copy & HA_ERATT) {
873                 /* Handle the error attention event */
874                 lpfc_handle_eratt(phba);
875
876                 if (phba->fw_dump_cmpl) {
877                         complete(phba->fw_dump_cmpl);
878                         phba->fw_dump_cmpl = NULL;
879                 }
880         }
881
882         if (ha_copy & HA_MBATT)
883                 lpfc_sli_handle_mb_event(phba);
884
885         if (ha_copy & HA_LATT)
886                 lpfc_handle_latt(phba);
887
888         /* Handle VMID Events */
889         if (lpfc_is_vmid_enabled(phba)) {
890                 if (phba->pport->work_port_events &
891                     WORKER_CHECK_VMID_ISSUE_QFPA) {
892                         lpfc_check_vmid_qfpa_issue(phba);
893                         phba->pport->work_port_events &=
894                                 ~WORKER_CHECK_VMID_ISSUE_QFPA;
895                 }
896                 if (phba->pport->work_port_events &
897                     WORKER_CHECK_INACTIVE_VMID) {
898                         lpfc_check_inactive_vmid(phba);
899                         phba->pport->work_port_events &=
900                             ~WORKER_CHECK_INACTIVE_VMID;
901                 }
902         }
903
904         /* Process SLI4 events */
905         if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
906                 if (phba->hba_flag & HBA_RRQ_ACTIVE)
907                         lpfc_handle_rrq_active(phba);
908                 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
909                         lpfc_sli4_els_xri_abort_event_proc(phba);
910                 if (phba->hba_flag & ASYNC_EVENT)
911                         lpfc_sli4_async_event_proc(phba);
912                 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
913                         spin_lock_irq(&phba->hbalock);
914                         phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
915                         spin_unlock_irq(&phba->hbalock);
916                         lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
917                 }
918                 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
919                         lpfc_sli4_fcf_redisc_event_proc(phba);
920         }
921
922         vports = lpfc_create_vport_work_array(phba);
923         if (vports != NULL)
924                 for (i = 0; i <= phba->max_vports; i++) {
925                         /*
926                          * We could have no vports in array if unloading, so if
927                          * this happens then just use the pport
928                          */
929                         if (vports[i] == NULL && i == 0)
930                                 vport = phba->pport;
931                         else
932                                 vport = vports[i];
933                         if (vport == NULL)
934                                 break;
935                         spin_lock_irq(&vport->work_port_lock);
936                         work_port_events = vport->work_port_events;
937                         vport->work_port_events &= ~work_port_events;
938                         spin_unlock_irq(&vport->work_port_lock);
939                         if (work_port_events & WORKER_DISC_TMO)
940                                 lpfc_disc_timeout_handler(vport);
941                         if (work_port_events & WORKER_ELS_TMO)
942                                 lpfc_els_timeout_handler(vport);
943                         if (work_port_events & WORKER_HB_TMO)
944                                 lpfc_hb_timeout_handler(phba);
945                         if (work_port_events & WORKER_MBOX_TMO)
946                                 lpfc_mbox_timeout_handler(phba);
947                         if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
948                                 lpfc_unblock_fabric_iocbs(phba);
949                         if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
950                                 lpfc_ramp_down_queue_handler(phba);
951                         if (work_port_events & WORKER_DELAYED_DISC_TMO)
952                                 lpfc_delayed_disc_timeout_handler(vport);
953                 }
954         lpfc_destroy_vport_work_array(phba, vports);
955
956         pring = lpfc_phba_elsring(phba);
957         status = (ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
958         status >>= (4*LPFC_ELS_RING);
959         if (pring && (status & HA_RXMASK ||
960                       pring->flag & LPFC_DEFERRED_RING_EVENT ||
961                       phba->hba_flag & HBA_SP_QUEUE_EVT)) {
962                 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
963                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
964                         /* Preserve legacy behavior. */
965                         if (!(phba->hba_flag & HBA_SP_QUEUE_EVT))
966                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
967                 } else {
968                         /* Driver could have abort request completed in queue
969                          * when link goes down.  Allow for this transition.
970                          */
971                         if (phba->link_state >= LPFC_LINK_DOWN ||
972                             phba->link_flag & LS_MDS_LOOPBACK) {
973                                 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
974                                 lpfc_sli_handle_slow_ring_event(phba, pring,
975                                                                 (status &
976                                                                 HA_RXMASK));
977                         }
978                 }
979                 if (phba->sli_rev == LPFC_SLI_REV4)
980                         lpfc_drain_txq(phba);
981                 /*
982                  * Turn on Ring interrupts
983                  */
984                 if (phba->sli_rev <= LPFC_SLI_REV3) {
985                         spin_lock_irq(&phba->hbalock);
986                         control = readl(phba->HCregaddr);
987                         if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
988                                 lpfc_debugfs_slow_ring_trc(phba,
989                                         "WRK Enable ring: cntl:x%x hacopy:x%x",
990                                         control, ha_copy, 0);
991
992                                 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
993                                 writel(control, phba->HCregaddr);
994                                 readl(phba->HCregaddr); /* flush */
995                         } else {
996                                 lpfc_debugfs_slow_ring_trc(phba,
997                                         "WRK Ring ok:     cntl:x%x hacopy:x%x",
998                                         control, ha_copy, 0);
999                         }
1000                         spin_unlock_irq(&phba->hbalock);
1001                 }
1002         }
1003         lpfc_work_list_done(phba);
1004 }
1005
1006 int
1007 lpfc_do_work(void *p)
1008 {
1009         struct lpfc_hba *phba = p;
1010         int rc;
1011
1012         set_user_nice(current, MIN_NICE);
1013         current->flags |= PF_NOFREEZE;
1014         phba->data_flags = 0;
1015
1016         while (!kthread_should_stop()) {
1017                 /* wait and check worker queue activities */
1018                 rc = wait_event_interruptible(phba->work_waitq,
1019                                         (test_and_clear_bit(LPFC_DATA_READY,
1020                                                             &phba->data_flags)
1021                                          || kthread_should_stop()));
1022                 /* Signal wakeup shall terminate the worker thread */
1023                 if (rc) {
1024                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1025                                         "0433 Wakeup on signal: rc=x%x\n", rc);
1026                         break;
1027                 }
1028
1029                 /* Attend pending lpfc data processing */
1030                 lpfc_work_done(phba);
1031         }
1032         phba->worker_thread = NULL;
1033         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1034                         "0432 Worker thread stopped.\n");
1035         return 0;
1036 }
1037
1038 /*
1039  * This is only called to handle FC worker events. Since this a rare
1040  * occurrence, we allocate a struct lpfc_work_evt structure here instead of
1041  * embedding it in the IOCB.
1042  */
1043 int
1044 lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
1045                       uint32_t evt)
1046 {
1047         struct lpfc_work_evt  *evtp;
1048         unsigned long flags;
1049
1050         /*
1051          * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
1052          * be queued to worker thread for processing
1053          */
1054         evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
1055         if (!evtp)
1056                 return 0;
1057
1058         evtp->evt_arg1  = arg1;
1059         evtp->evt_arg2  = arg2;
1060         evtp->evt       = evt;
1061
1062         spin_lock_irqsave(&phba->hbalock, flags);
1063         list_add_tail(&evtp->evt_listp, &phba->work_list);
1064         spin_unlock_irqrestore(&phba->hbalock, flags);
1065
1066         lpfc_worker_wake_up(phba);
1067
1068         return 1;
1069 }
1070
1071 void
1072 lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
1073 {
1074         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1075         struct lpfc_hba  *phba = vport->phba;
1076         struct lpfc_nodelist *ndlp, *next_ndlp;
1077
1078         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1079                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1080                         continue;
1081
1082                 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
1083                     ((vport->port_type == LPFC_NPIV_PORT) &&
1084                      ((ndlp->nlp_DID == NameServer_DID) ||
1085                       (ndlp->nlp_DID == FDMI_DID) ||
1086                       (ndlp->nlp_DID == Fabric_Cntl_DID))))
1087                         lpfc_unreg_rpi(vport, ndlp);
1088
1089                 /* Leave Fabric nodes alone on link down */
1090                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
1091                     (!remove && ndlp->nlp_type & NLP_FABRIC))
1092                         continue;
1093
1094                 /* Notify transport of connectivity loss to trigger cleanup. */
1095                 if (phba->nvmet_support &&
1096                     ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
1097                         lpfc_nvmet_invalidate_host(phba, ndlp);
1098
1099                 lpfc_disc_state_machine(vport, ndlp, NULL,
1100                                         remove
1101                                         ? NLP_EVT_DEVICE_RM
1102                                         : NLP_EVT_DEVICE_RECOVERY);
1103         }
1104         if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
1105                 if (phba->sli_rev == LPFC_SLI_REV4)
1106                         lpfc_sli4_unreg_all_rpis(vport);
1107                 lpfc_mbx_unreg_vpi(vport);
1108                 spin_lock_irq(shost->host_lock);
1109                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1110                 spin_unlock_irq(shost->host_lock);
1111         }
1112 }
1113
1114 void
1115 lpfc_port_link_failure(struct lpfc_vport *vport)
1116 {
1117         lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
1118
1119         /* Cleanup any outstanding received buffers */
1120         lpfc_cleanup_rcv_buffers(vport);
1121
1122         /* Cleanup any outstanding RSCN activity */
1123         lpfc_els_flush_rscn(vport);
1124
1125         /* Cleanup any outstanding ELS commands */
1126         lpfc_els_flush_cmd(vport);
1127
1128         lpfc_cleanup_rpis(vport, 0);
1129
1130         /* Turn off discovery timer if its running */
1131         lpfc_can_disctmo(vport);
1132 }
1133
1134 void
1135 lpfc_linkdown_port(struct lpfc_vport *vport)
1136 {
1137         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1138
1139         if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1140                 fc_host_post_event(shost, fc_get_event_number(),
1141                                    FCH_EVT_LINKDOWN, 0);
1142
1143         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1144                 "Link Down:       state:x%x rtry:x%x flg:x%x",
1145                 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
1146
1147         lpfc_port_link_failure(vport);
1148
1149         /* Stop delayed Nport discovery */
1150         spin_lock_irq(shost->host_lock);
1151         vport->fc_flag &= ~FC_DISC_DELAYED;
1152         spin_unlock_irq(shost->host_lock);
1153         del_timer_sync(&vport->delayed_disc_tmo);
1154 }
1155
1156 int
1157 lpfc_linkdown(struct lpfc_hba *phba)
1158 {
1159         struct lpfc_vport *vport = phba->pport;
1160         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1161         struct lpfc_vport **vports;
1162         LPFC_MBOXQ_t          *mb;
1163         int i;
1164
1165         if (phba->link_state == LPFC_LINK_DOWN)
1166                 return 0;
1167
1168         /* Block all SCSI stack I/Os */
1169         lpfc_scsi_dev_block(phba);
1170
1171         phba->defer_flogi_acc_flag = false;
1172
1173         spin_lock_irq(&phba->hbalock);
1174         phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1175         spin_unlock_irq(&phba->hbalock);
1176         if (phba->link_state > LPFC_LINK_DOWN) {
1177                 phba->link_state = LPFC_LINK_DOWN;
1178                 if (phba->sli4_hba.conf_trunk) {
1179                         phba->trunk_link.link0.state = 0;
1180                         phba->trunk_link.link1.state = 0;
1181                         phba->trunk_link.link2.state = 0;
1182                         phba->trunk_link.link3.state = 0;
1183                         phba->sli4_hba.link_state.logical_speed =
1184                                                 LPFC_LINK_SPEED_UNKNOWN;
1185                 }
1186                 spin_lock_irq(shost->host_lock);
1187                 phba->pport->fc_flag &= ~FC_LBIT;
1188                 spin_unlock_irq(shost->host_lock);
1189         }
1190         vports = lpfc_create_vport_work_array(phba);
1191         if (vports != NULL) {
1192                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1193                         /* Issue a LINK DOWN event to all nodes */
1194                         lpfc_linkdown_port(vports[i]);
1195
1196                         vports[i]->fc_myDID = 0;
1197
1198                         if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1199                             (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
1200                                 if (phba->nvmet_support)
1201                                         lpfc_nvmet_update_targetport(phba);
1202                                 else
1203                                         lpfc_nvme_update_localport(vports[i]);
1204                         }
1205                 }
1206         }
1207         lpfc_destroy_vport_work_array(phba, vports);
1208
1209         /* Clean up any SLI3 firmware default rpi's */
1210         if (phba->sli_rev > LPFC_SLI_REV3)
1211                 goto skip_unreg_did;
1212
1213         mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1214         if (mb) {
1215                 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
1216                 mb->vport = vport;
1217                 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1218                 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
1219                     == MBX_NOT_FINISHED) {
1220                         mempool_free(mb, phba->mbox_mem_pool);
1221                 }
1222         }
1223
1224  skip_unreg_did:
1225         /* Setup myDID for link up if we are in pt2pt mode */
1226         if (phba->pport->fc_flag & FC_PT2PT) {
1227                 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1228                 if (mb) {
1229                         lpfc_config_link(phba, mb);
1230                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1231                         mb->vport = vport;
1232                         if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
1233                             == MBX_NOT_FINISHED) {
1234                                 mempool_free(mb, phba->mbox_mem_pool);
1235                         }
1236                 }
1237                 spin_lock_irq(shost->host_lock);
1238                 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
1239                 phba->pport->rcv_flogi_cnt = 0;
1240                 spin_unlock_irq(shost->host_lock);
1241         }
1242         return 0;
1243 }
1244
1245 static void
1246 lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
1247 {
1248         struct lpfc_nodelist *ndlp;
1249
1250         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
1251                 ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
1252
1253                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1254                         continue;
1255                 if (ndlp->nlp_type & NLP_FABRIC) {
1256                         /* On Linkup its safe to clean up the ndlp
1257                          * from Fabric connections.
1258                          */
1259                         if (ndlp->nlp_DID != Fabric_DID)
1260                                 lpfc_unreg_rpi(vport, ndlp);
1261                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1262                 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
1263                         /* Fail outstanding IO now since device is
1264                          * marked for PLOGI.
1265                          */
1266                         lpfc_unreg_rpi(vport, ndlp);
1267                 }
1268         }
1269 }
1270
1271 static void
1272 lpfc_linkup_port(struct lpfc_vport *vport)
1273 {
1274         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1275         struct lpfc_hba  *phba = vport->phba;
1276
1277         if ((vport->load_flag & FC_UNLOADING) != 0)
1278                 return;
1279
1280         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1281                 "Link Up:         top:x%x speed:x%x flg:x%x",
1282                 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
1283
1284         /* If NPIV is not enabled, only bring the physical port up */
1285         if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1286                 (vport != phba->pport))
1287                 return;
1288
1289         if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1290                 fc_host_post_event(shost, fc_get_event_number(),
1291                                    FCH_EVT_LINKUP, 0);
1292
1293         spin_lock_irq(shost->host_lock);
1294         vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
1295                             FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1296         vport->fc_flag |= FC_NDISC_ACTIVE;
1297         vport->fc_ns_retry = 0;
1298         spin_unlock_irq(shost->host_lock);
1299
1300         lpfc_linkup_cleanup_nodes(vport);
1301 }
1302
1303 static int
1304 lpfc_linkup(struct lpfc_hba *phba)
1305 {
1306         struct lpfc_vport **vports;
1307         int i;
1308         struct Scsi_Host  *shost = lpfc_shost_from_vport(phba->pport);
1309
1310         phba->link_state = LPFC_LINK_UP;
1311
1312         /* Unblock fabric iocbs if they are blocked */
1313         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1314         del_timer_sync(&phba->fabric_block_timer);
1315
1316         vports = lpfc_create_vport_work_array(phba);
1317         if (vports != NULL)
1318                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1319                         lpfc_linkup_port(vports[i]);
1320         lpfc_destroy_vport_work_array(phba, vports);
1321
1322         /* Clear the pport flogi counter in case the link down was
1323          * absorbed without an ACQE. No lock here - in worker thread
1324          * and discovery is synchronized.
1325          */
1326         spin_lock_irq(shost->host_lock);
1327         phba->pport->rcv_flogi_cnt = 0;
1328         spin_unlock_irq(shost->host_lock);
1329
1330         /* reinitialize initial FLOGI flag */
1331         phba->hba_flag &= ~(HBA_FLOGI_ISSUED);
1332         phba->defer_flogi_acc_flag = false;
1333
1334         return 0;
1335 }
1336
1337 /*
1338  * This routine handles processing a CLEAR_LA mailbox
1339  * command upon completion. It is setup in the LPFC_MBOXQ
1340  * as the completion routine when the command is
1341  * handed off to the SLI layer. SLI3 only.
1342  */
1343 static void
1344 lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1345 {
1346         struct lpfc_vport *vport = pmb->vport;
1347         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1348         struct lpfc_sli   *psli = &phba->sli;
1349         MAILBOX_t *mb = &pmb->u.mb;
1350         uint32_t control;
1351
1352         /* Since we don't do discovery right now, turn these off here */
1353         psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1354         psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1355
1356         /* Check for error */
1357         if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
1358                 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
1359                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1360                                  "0320 CLEAR_LA mbxStatus error x%x hba "
1361                                  "state x%x\n",
1362                                  mb->mbxStatus, vport->port_state);
1363                 phba->link_state = LPFC_HBA_ERROR;
1364                 goto out;
1365         }
1366
1367         if (vport->port_type == LPFC_PHYSICAL_PORT)
1368                 phba->link_state = LPFC_HBA_READY;
1369
1370         spin_lock_irq(&phba->hbalock);
1371         psli->sli_flag |= LPFC_PROCESS_LA;
1372         control = readl(phba->HCregaddr);
1373         control |= HC_LAINT_ENA;
1374         writel(control, phba->HCregaddr);
1375         readl(phba->HCregaddr); /* flush */
1376         spin_unlock_irq(&phba->hbalock);
1377         mempool_free(pmb, phba->mbox_mem_pool);
1378         return;
1379
1380 out:
1381         /* Device Discovery completes */
1382         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1383                          "0225 Device Discovery completes\n");
1384         mempool_free(pmb, phba->mbox_mem_pool);
1385
1386         spin_lock_irq(shost->host_lock);
1387         vport->fc_flag &= ~FC_ABORT_DISCOVERY;
1388         spin_unlock_irq(shost->host_lock);
1389
1390         lpfc_can_disctmo(vport);
1391
1392         /* turn on Link Attention interrupts */
1393
1394         spin_lock_irq(&phba->hbalock);
1395         psli->sli_flag |= LPFC_PROCESS_LA;
1396         control = readl(phba->HCregaddr);
1397         control |= HC_LAINT_ENA;
1398         writel(control, phba->HCregaddr);
1399         readl(phba->HCregaddr); /* flush */
1400         spin_unlock_irq(&phba->hbalock);
1401
1402         return;
1403 }
1404
1405 void
1406 lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1407 {
1408         struct lpfc_vport *vport = pmb->vport;
1409         LPFC_MBOXQ_t *sparam_mb;
1410         struct lpfc_dmabuf *sparam_mp;
1411         u16 status = pmb->u.mb.mbxStatus;
1412         int rc;
1413
1414         mempool_free(pmb, phba->mbox_mem_pool);
1415
1416         if (status)
1417                 goto out;
1418
1419         /* don't perform discovery for SLI4 loopback diagnostic test */
1420         if ((phba->sli_rev == LPFC_SLI_REV4) &&
1421             !(phba->hba_flag & HBA_FCOE_MODE) &&
1422             (phba->link_flag & LS_LOOPBACK_MODE))
1423                 return;
1424
1425         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
1426             vport->fc_flag & FC_PUBLIC_LOOP &&
1427             !(vport->fc_flag & FC_LBIT)) {
1428                         /* Need to wait for FAN - use discovery timer
1429                          * for timeout.  port_state is identically
1430                          * LPFC_LOCAL_CFG_LINK while waiting for FAN
1431                          */
1432                         lpfc_set_disctmo(vport);
1433                         return;
1434         }
1435
1436         /* Start discovery by sending a FLOGI. port_state is identically
1437          * LPFC_FLOGI while waiting for FLOGI cmpl.
1438          */
1439         if (vport->port_state != LPFC_FLOGI) {
1440                 /* Issue MBX_READ_SPARAM to update CSPs before FLOGI if
1441                  * bb-credit recovery is in place.
1442                  */
1443                 if (phba->bbcredit_support && phba->cfg_enable_bbcr &&
1444                     !(phba->link_flag & LS_LOOPBACK_MODE)) {
1445                         sparam_mb = mempool_alloc(phba->mbox_mem_pool,
1446                                                   GFP_KERNEL);
1447                         if (!sparam_mb)
1448                                 goto sparam_out;
1449
1450                         rc = lpfc_read_sparam(phba, sparam_mb, 0);
1451                         if (rc) {
1452                                 mempool_free(sparam_mb, phba->mbox_mem_pool);
1453                                 goto sparam_out;
1454                         }
1455                         sparam_mb->vport = vport;
1456                         sparam_mb->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
1457                         rc = lpfc_sli_issue_mbox(phba, sparam_mb, MBX_NOWAIT);
1458                         if (rc == MBX_NOT_FINISHED) {
1459                                 sparam_mp = (struct lpfc_dmabuf *)
1460                                                 sparam_mb->ctx_buf;
1461                                 lpfc_mbuf_free(phba, sparam_mp->virt,
1462                                                sparam_mp->phys);
1463                                 kfree(sparam_mp);
1464                                 sparam_mb->ctx_buf = NULL;
1465                                 mempool_free(sparam_mb, phba->mbox_mem_pool);
1466                                 goto sparam_out;
1467                         }
1468
1469                         phba->hba_flag |= HBA_DEFER_FLOGI;
1470                 }  else {
1471                         lpfc_initial_flogi(vport);
1472                 }
1473         } else {
1474                 if (vport->fc_flag & FC_PT2PT)
1475                         lpfc_disc_start(vport);
1476         }
1477         return;
1478
1479 out:
1480         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1481                          "0306 CONFIG_LINK mbxStatus error x%x HBA state x%x\n",
1482                          status, vport->port_state);
1483
1484 sparam_out:
1485         lpfc_linkdown(phba);
1486
1487         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1488                          "0200 CONFIG_LINK bad hba state x%x\n",
1489                          vport->port_state);
1490
1491         lpfc_issue_clear_la(phba, vport);
1492         return;
1493 }
1494
1495 /**
1496  * lpfc_sli4_clear_fcf_rr_bmask
1497  * @phba: pointer to the struct lpfc_hba for this port.
1498  * This fucnction resets the round robin bit mask and clears the
1499  * fcf priority list. The list deletions are done while holding the
1500  * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1501  * from the lpfc_fcf_pri record.
1502  **/
1503 void
1504 lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1505 {
1506         struct lpfc_fcf_pri *fcf_pri;
1507         struct lpfc_fcf_pri *next_fcf_pri;
1508         memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1509         spin_lock_irq(&phba->hbalock);
1510         list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1511                                 &phba->fcf.fcf_pri_list, list) {
1512                 list_del_init(&fcf_pri->list);
1513                 fcf_pri->fcf_rec.flag = 0;
1514         }
1515         spin_unlock_irq(&phba->hbalock);
1516 }
1517 static void
1518 lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1519 {
1520         struct lpfc_vport *vport = mboxq->vport;
1521
1522         if (mboxq->u.mb.mbxStatus) {
1523                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1524                                  "2017 REG_FCFI mbxStatus error x%x "
1525                                  "HBA state x%x\n", mboxq->u.mb.mbxStatus,
1526                                  vport->port_state);
1527                 goto fail_out;
1528         }
1529
1530         /* Start FCoE discovery by sending a FLOGI. */
1531         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1532         /* Set the FCFI registered flag */
1533         spin_lock_irq(&phba->hbalock);
1534         phba->fcf.fcf_flag |= FCF_REGISTERED;
1535         spin_unlock_irq(&phba->hbalock);
1536
1537         /* If there is a pending FCoE event, restart FCF table scan. */
1538         if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1539                 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
1540                 goto fail_out;
1541
1542         /* Mark successful completion of FCF table scan */
1543         spin_lock_irq(&phba->hbalock);
1544         phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1545         phba->hba_flag &= ~FCF_TS_INPROG;
1546         if (vport->port_state != LPFC_FLOGI) {
1547                 phba->hba_flag |= FCF_RR_INPROG;
1548                 spin_unlock_irq(&phba->hbalock);
1549                 lpfc_issue_init_vfi(vport);
1550                 goto out;
1551         }
1552         spin_unlock_irq(&phba->hbalock);
1553         goto out;
1554
1555 fail_out:
1556         spin_lock_irq(&phba->hbalock);
1557         phba->hba_flag &= ~FCF_RR_INPROG;
1558         spin_unlock_irq(&phba->hbalock);
1559 out:
1560         mempool_free(mboxq, phba->mbox_mem_pool);
1561 }
1562
1563 /**
1564  * lpfc_fab_name_match - Check if the fcf fabric name match.
1565  * @fab_name: pointer to fabric name.
1566  * @new_fcf_record: pointer to fcf record.
1567  *
1568  * This routine compare the fcf record's fabric name with provided
1569  * fabric name. If the fabric name are identical this function
1570  * returns 1 else return 0.
1571  **/
1572 static uint32_t
1573 lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1574 {
1575         if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
1576                 return 0;
1577         if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1578                 return 0;
1579         if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1580                 return 0;
1581         if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1582                 return 0;
1583         if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1584                 return 0;
1585         if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1586                 return 0;
1587         if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1588                 return 0;
1589         if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1590                 return 0;
1591         return 1;
1592 }
1593
1594 /**
1595  * lpfc_sw_name_match - Check if the fcf switch name match.
1596  * @sw_name: pointer to switch name.
1597  * @new_fcf_record: pointer to fcf record.
1598  *
1599  * This routine compare the fcf record's switch name with provided
1600  * switch name. If the switch name are identical this function
1601  * returns 1 else return 0.
1602  **/
1603 static uint32_t
1604 lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1605 {
1606         if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
1607                 return 0;
1608         if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1609                 return 0;
1610         if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1611                 return 0;
1612         if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1613                 return 0;
1614         if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1615                 return 0;
1616         if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1617                 return 0;
1618         if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1619                 return 0;
1620         if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1621                 return 0;
1622         return 1;
1623 }
1624
1625 /**
1626  * lpfc_mac_addr_match - Check if the fcf mac address match.
1627  * @mac_addr: pointer to mac address.
1628  * @new_fcf_record: pointer to fcf record.
1629  *
1630  * This routine compare the fcf record's mac address with HBA's
1631  * FCF mac address. If the mac addresses are identical this function
1632  * returns 1 else return 0.
1633  **/
1634 static uint32_t
1635 lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
1636 {
1637         if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
1638                 return 0;
1639         if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1640                 return 0;
1641         if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1642                 return 0;
1643         if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1644                 return 0;
1645         if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1646                 return 0;
1647         if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1648                 return 0;
1649         return 1;
1650 }
1651
1652 static bool
1653 lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1654 {
1655         return (curr_vlan_id == new_vlan_id);
1656 }
1657
1658 /**
1659  * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1660  * @phba: pointer to lpfc hba data structure.
1661  * @fcf_index: Index for the lpfc_fcf_record.
1662  * @new_fcf_record: pointer to hba fcf record.
1663  *
1664  * This routine updates the driver FCF priority record from the new HBA FCF
1665  * record. The hbalock is asserted held in the code path calling this
1666  * routine.
1667  **/
1668 static void
1669 __lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1670                                  struct fcf_record *new_fcf_record
1671                                  )
1672 {
1673         struct lpfc_fcf_pri *fcf_pri;
1674
1675         fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1676         fcf_pri->fcf_rec.fcf_index = fcf_index;
1677         /* FCF record priority */
1678         fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1679
1680 }
1681
1682 /**
1683  * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1684  * @fcf_rec: pointer to driver fcf record.
1685  * @new_fcf_record: pointer to fcf record.
1686  *
1687  * This routine copies the FCF information from the FCF
1688  * record to lpfc_hba data structure.
1689  **/
1690 static void
1691 lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1692                      struct fcf_record *new_fcf_record)
1693 {
1694         /* Fabric name */
1695         fcf_rec->fabric_name[0] =
1696                 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1697         fcf_rec->fabric_name[1] =
1698                 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1699         fcf_rec->fabric_name[2] =
1700                 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1701         fcf_rec->fabric_name[3] =
1702                 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1703         fcf_rec->fabric_name[4] =
1704                 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1705         fcf_rec->fabric_name[5] =
1706                 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1707         fcf_rec->fabric_name[6] =
1708                 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1709         fcf_rec->fabric_name[7] =
1710                 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1711         /* Mac address */
1712         fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1713         fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1714         fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1715         fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1716         fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1717         fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1718         /* FCF record index */
1719         fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1720         /* FCF record priority */
1721         fcf_rec->priority = new_fcf_record->fip_priority;
1722         /* Switch name */
1723         fcf_rec->switch_name[0] =
1724                 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1725         fcf_rec->switch_name[1] =
1726                 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1727         fcf_rec->switch_name[2] =
1728                 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1729         fcf_rec->switch_name[3] =
1730                 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1731         fcf_rec->switch_name[4] =
1732                 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1733         fcf_rec->switch_name[5] =
1734                 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1735         fcf_rec->switch_name[6] =
1736                 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1737         fcf_rec->switch_name[7] =
1738                 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
1739 }
1740
1741 /**
1742  * __lpfc_update_fcf_record - Update driver fcf record
1743  * @phba: pointer to lpfc hba data structure.
1744  * @fcf_rec: pointer to driver fcf record.
1745  * @new_fcf_record: pointer to hba fcf record.
1746  * @addr_mode: address mode to be set to the driver fcf record.
1747  * @vlan_id: vlan tag to be set to the driver fcf record.
1748  * @flag: flag bits to be set to the driver fcf record.
1749  *
1750  * This routine updates the driver FCF record from the new HBA FCF record
1751  * together with the address mode, vlan_id, and other informations. This
1752  * routine is called with the hbalock held.
1753  **/
1754 static void
1755 __lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1756                        struct fcf_record *new_fcf_record, uint32_t addr_mode,
1757                        uint16_t vlan_id, uint32_t flag)
1758 {
1759         lockdep_assert_held(&phba->hbalock);
1760
1761         /* Copy the fields from the HBA's FCF record */
1762         lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1763         /* Update other fields of driver FCF record */
1764         fcf_rec->addr_mode = addr_mode;
1765         fcf_rec->vlan_id = vlan_id;
1766         fcf_rec->flag |= (flag | RECORD_VALID);
1767         __lpfc_update_fcf_record_pri(phba,
1768                 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1769                                  new_fcf_record);
1770 }
1771
1772 /**
1773  * lpfc_register_fcf - Register the FCF with hba.
1774  * @phba: pointer to lpfc hba data structure.
1775  *
1776  * This routine issues a register fcfi mailbox command to register
1777  * the fcf with HBA.
1778  **/
1779 static void
1780 lpfc_register_fcf(struct lpfc_hba *phba)
1781 {
1782         LPFC_MBOXQ_t *fcf_mbxq;
1783         int rc;
1784
1785         spin_lock_irq(&phba->hbalock);
1786         /* If the FCF is not available do nothing. */
1787         if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
1788                 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1789                 spin_unlock_irq(&phba->hbalock);
1790                 return;
1791         }
1792
1793         /* The FCF is already registered, start discovery */
1794         if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1795                 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1796                 phba->hba_flag &= ~FCF_TS_INPROG;
1797                 if (phba->pport->port_state != LPFC_FLOGI &&
1798                     phba->pport->fc_flag & FC_FABRIC) {
1799                         phba->hba_flag |= FCF_RR_INPROG;
1800                         spin_unlock_irq(&phba->hbalock);
1801                         lpfc_initial_flogi(phba->pport);
1802                         return;
1803                 }
1804                 spin_unlock_irq(&phba->hbalock);
1805                 return;
1806         }
1807         spin_unlock_irq(&phba->hbalock);
1808
1809         fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1810         if (!fcf_mbxq) {
1811                 spin_lock_irq(&phba->hbalock);
1812                 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1813                 spin_unlock_irq(&phba->hbalock);
1814                 return;
1815         }
1816
1817         lpfc_reg_fcfi(phba, fcf_mbxq);
1818         fcf_mbxq->vport = phba->pport;
1819         fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1820         rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
1821         if (rc == MBX_NOT_FINISHED) {
1822                 spin_lock_irq(&phba->hbalock);
1823                 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1824                 spin_unlock_irq(&phba->hbalock);
1825                 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
1826         }
1827
1828         return;
1829 }
1830
1831 /**
1832  * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1833  * @phba: pointer to lpfc hba data structure.
1834  * @new_fcf_record: pointer to fcf record.
1835  * @boot_flag: Indicates if this record used by boot bios.
1836  * @addr_mode: The address mode to be used by this FCF
1837  * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
1838  *
1839  * This routine compare the fcf record with connect list obtained from the
1840  * config region to decide if this FCF can be used for SAN discovery. It returns
1841  * 1 if this record can be used for SAN discovery else return zero. If this FCF
1842  * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1843  * is used by boot bios and addr_mode will indicate the addressing mode to be
1844  * used for this FCF when the function returns.
1845  * If the FCF record need to be used with a particular vlan id, the vlan is
1846  * set in the vlan_id on return of the function. If not VLAN tagging need to
1847  * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
1848  **/
1849 static int
1850 lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1851                         struct fcf_record *new_fcf_record,
1852                         uint32_t *boot_flag, uint32_t *addr_mode,
1853                         uint16_t *vlan_id)
1854 {
1855         struct lpfc_fcf_conn_entry *conn_entry;
1856         int i, j, fcf_vlan_id = 0;
1857
1858         /* Find the lowest VLAN id in the FCF record */
1859         for (i = 0; i < 512; i++) {
1860                 if (new_fcf_record->vlan_bitmap[i]) {
1861                         fcf_vlan_id = i * 8;
1862                         j = 0;
1863                         while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1864                                 j++;
1865                                 fcf_vlan_id++;
1866                         }
1867                         break;
1868                 }
1869         }
1870
1871         /* FCF not valid/available or solicitation in progress */
1872         if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1873             !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1874             bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
1875                 return 0;
1876
1877         if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
1878                 *boot_flag = 0;
1879                 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1880                                 new_fcf_record);
1881                 if (phba->valid_vlan)
1882                         *vlan_id = phba->vlan_id;
1883                 else
1884                         *vlan_id = LPFC_FCOE_NULL_VID;
1885                 return 1;
1886         }
1887
1888         /*
1889          * If there are no FCF connection table entry, driver connect to all
1890          * FCFs.
1891          */
1892         if (list_empty(&phba->fcf_conn_rec_list)) {
1893                 *boot_flag = 0;
1894                 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1895                         new_fcf_record);
1896
1897                 /*
1898                  * When there are no FCF connect entries, use driver's default
1899                  * addressing mode - FPMA.
1900                  */
1901                 if (*addr_mode & LPFC_FCF_FPMA)
1902                         *addr_mode = LPFC_FCF_FPMA;
1903
1904                 /* If FCF record report a vlan id use that vlan id */
1905                 if (fcf_vlan_id)
1906                         *vlan_id = fcf_vlan_id;
1907                 else
1908                         *vlan_id = LPFC_FCOE_NULL_VID;
1909                 return 1;
1910         }
1911
1912         list_for_each_entry(conn_entry,
1913                             &phba->fcf_conn_rec_list, list) {
1914                 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1915                         continue;
1916
1917                 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1918                         !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
1919                                              new_fcf_record))
1920                         continue;
1921                 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1922                         !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1923                                             new_fcf_record))
1924                         continue;
1925                 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1926                         /*
1927                          * If the vlan bit map does not have the bit set for the
1928                          * vlan id to be used, then it is not a match.
1929                          */
1930                         if (!(new_fcf_record->vlan_bitmap
1931                                 [conn_entry->conn_rec.vlan_tag / 8] &
1932                                 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1933                                 continue;
1934                 }
1935
1936                 /*
1937                  * If connection record does not support any addressing mode,
1938                  * skip the FCF record.
1939                  */
1940                 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1941                         & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1942                         continue;
1943
1944                 /*
1945                  * Check if the connection record specifies a required
1946                  * addressing mode.
1947                  */
1948                 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1949                         !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1950
1951                         /*
1952                          * If SPMA required but FCF not support this continue.
1953                          */
1954                         if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1955                                 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1956                                         new_fcf_record) & LPFC_FCF_SPMA))
1957                                 continue;
1958
1959                         /*
1960                          * If FPMA required but FCF not support this continue.
1961                          */
1962                         if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1963                                 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1964                                 new_fcf_record) & LPFC_FCF_FPMA))
1965                                 continue;
1966                 }
1967
1968                 /*
1969                  * This fcf record matches filtering criteria.
1970                  */
1971                 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1972                         *boot_flag = 1;
1973                 else
1974                         *boot_flag = 0;
1975
1976                 /*
1977                  * If user did not specify any addressing mode, or if the
1978                  * preferred addressing mode specified by user is not supported
1979                  * by FCF, allow fabric to pick the addressing mode.
1980                  */
1981                 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1982                                 new_fcf_record);
1983                 /*
1984                  * If the user specified a required address mode, assign that
1985                  * address mode
1986                  */
1987                 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1988                         (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1989                         *addr_mode = (conn_entry->conn_rec.flags &
1990                                 FCFCNCT_AM_SPMA) ?
1991                                 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1992                 /*
1993                  * If the user specified a preferred address mode, use the
1994                  * addr mode only if FCF support the addr_mode.
1995                  */
1996                 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1997                         (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1998                         (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1999                         (*addr_mode & LPFC_FCF_SPMA))
2000                                 *addr_mode = LPFC_FCF_SPMA;
2001                 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
2002                         (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
2003                         !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
2004                         (*addr_mode & LPFC_FCF_FPMA))
2005                                 *addr_mode = LPFC_FCF_FPMA;
2006
2007                 /* If matching connect list has a vlan id, use it */
2008                 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
2009                         *vlan_id = conn_entry->conn_rec.vlan_tag;
2010                 /*
2011                  * If no vlan id is specified in connect list, use the vlan id
2012                  * in the FCF record
2013                  */
2014                 else if (fcf_vlan_id)
2015                         *vlan_id = fcf_vlan_id;
2016                 else
2017                         *vlan_id = LPFC_FCOE_NULL_VID;
2018
2019                 return 1;
2020         }
2021
2022         return 0;
2023 }
2024
2025 /**
2026  * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
2027  * @phba: pointer to lpfc hba data structure.
2028  * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
2029  *
2030  * This function check if there is any fcoe event pending while driver
2031  * scan FCF entries. If there is any pending event, it will restart the
2032  * FCF saning and return 1 else return 0.
2033  */
2034 int
2035 lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
2036 {
2037         /*
2038          * If the Link is up and no FCoE events while in the
2039          * FCF discovery, no need to restart FCF discovery.
2040          */
2041         if ((phba->link_state  >= LPFC_LINK_UP) &&
2042             (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
2043                 return 0;
2044
2045         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2046                         "2768 Pending link or FCF event during current "
2047                         "handling of the previous event: link_state:x%x, "
2048                         "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
2049                         phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
2050                         phba->fcoe_eventtag);
2051
2052         spin_lock_irq(&phba->hbalock);
2053         phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
2054         spin_unlock_irq(&phba->hbalock);
2055
2056         if (phba->link_state >= LPFC_LINK_UP) {
2057                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2058                                 "2780 Restart FCF table scan due to "
2059                                 "pending FCF event:evt_tag_at_scan:x%x, "
2060                                 "evt_tag_current:x%x\n",
2061                                 phba->fcoe_eventtag_at_fcf_scan,
2062                                 phba->fcoe_eventtag);
2063                 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
2064         } else {
2065                 /*
2066                  * Do not continue FCF discovery and clear FCF_TS_INPROG
2067                  * flag
2068                  */
2069                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2070                                 "2833 Stop FCF discovery process due to link "
2071                                 "state change (x%x)\n", phba->link_state);
2072                 spin_lock_irq(&phba->hbalock);
2073                 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
2074                 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
2075                 spin_unlock_irq(&phba->hbalock);
2076         }
2077
2078         /* Unregister the currently registered FCF if required */
2079         if (unreg_fcf) {
2080                 spin_lock_irq(&phba->hbalock);
2081                 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
2082                 spin_unlock_irq(&phba->hbalock);
2083                 lpfc_sli4_unregister_fcf(phba);
2084         }
2085         return 1;
2086 }
2087
2088 /**
2089  * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
2090  * @phba: pointer to lpfc hba data structure.
2091  * @fcf_cnt: number of eligible fcf record seen so far.
2092  *
2093  * This function makes an running random selection decision on FCF record to
2094  * use through a sequence of @fcf_cnt eligible FCF records with equal
2095  * probability. To perform integer manunipulation of random numbers with
2096  * size unit32_t, the lower 16 bits of the 32-bit random number returned
2097  * from prandom_u32() are taken as the random random number generated.
2098  *
2099  * Returns true when outcome is for the newly read FCF record should be
2100  * chosen; otherwise, return false when outcome is for keeping the previously
2101  * chosen FCF record.
2102  **/
2103 static bool
2104 lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
2105 {
2106         uint32_t rand_num;
2107
2108         /* Get 16-bit uniform random number */
2109         rand_num = 0xFFFF & prandom_u32();
2110
2111         /* Decision with probability 1/fcf_cnt */
2112         if ((fcf_cnt * rand_num) < 0xFFFF)
2113                 return true;
2114         else
2115                 return false;
2116 }
2117
2118 /**
2119  * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
2120  * @phba: pointer to lpfc hba data structure.
2121  * @mboxq: pointer to mailbox object.
2122  * @next_fcf_index: pointer to holder of next fcf index.
2123  *
2124  * This routine parses the non-embedded fcf mailbox command by performing the
2125  * necessarily error checking, non-embedded read FCF record mailbox command
2126  * SGE parsing, and endianness swapping.
2127  *
2128  * Returns the pointer to the new FCF record in the non-embedded mailbox
2129  * command DMA memory if successfully, other NULL.
2130  */
2131 static struct fcf_record *
2132 lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
2133                              uint16_t *next_fcf_index)
2134 {
2135         void *virt_addr;
2136         struct lpfc_mbx_sge sge;
2137         struct lpfc_mbx_read_fcf_tbl *read_fcf;
2138         uint32_t shdr_status, shdr_add_status, if_type;
2139         union lpfc_sli4_cfg_shdr *shdr;
2140         struct fcf_record *new_fcf_record;
2141
2142         /* Get the first SGE entry from the non-embedded DMA memory. This
2143          * routine only uses a single SGE.
2144          */
2145         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
2146         if (unlikely(!mboxq->sge_array)) {
2147                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2148                                 "2524 Failed to get the non-embedded SGE "
2149                                 "virtual address\n");
2150                 return NULL;
2151         }
2152         virt_addr = mboxq->sge_array->addr[0];
2153
2154         shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
2155         lpfc_sli_pcimem_bcopy(shdr, shdr,
2156                               sizeof(union lpfc_sli4_cfg_shdr));
2157         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2158         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
2159         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2160         if (shdr_status || shdr_add_status) {
2161                 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
2162                                         if_type == LPFC_SLI_INTF_IF_TYPE_2)
2163                         lpfc_printf_log(phba, KERN_ERR,
2164                                         LOG_TRACE_EVENT,
2165                                         "2726 READ_FCF_RECORD Indicates empty "
2166                                         "FCF table.\n");
2167                 else
2168                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2169                                         "2521 READ_FCF_RECORD mailbox failed "
2170                                         "with status x%x add_status x%x, "
2171                                         "mbx\n", shdr_status, shdr_add_status);
2172                 return NULL;
2173         }
2174
2175         /* Interpreting the returned information of the FCF record */
2176         read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
2177         lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
2178                               sizeof(struct lpfc_mbx_read_fcf_tbl));
2179         *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
2180         new_fcf_record = (struct fcf_record *)(virt_addr +
2181                           sizeof(struct lpfc_mbx_read_fcf_tbl));
2182         lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
2183                                 offsetof(struct fcf_record, vlan_bitmap));
2184         new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
2185         new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
2186
2187         return new_fcf_record;
2188 }
2189
2190 /**
2191  * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
2192  * @phba: pointer to lpfc hba data structure.
2193  * @fcf_record: pointer to the fcf record.
2194  * @vlan_id: the lowest vlan identifier associated to this fcf record.
2195  * @next_fcf_index: the index to the next fcf record in hba's fcf table.
2196  *
2197  * This routine logs the detailed FCF record if the LOG_FIP loggin is
2198  * enabled.
2199  **/
2200 static void
2201 lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
2202                               struct fcf_record *fcf_record,
2203                               uint16_t vlan_id,
2204                               uint16_t next_fcf_index)
2205 {
2206         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2207                         "2764 READ_FCF_RECORD:\n"
2208                         "\tFCF_Index     : x%x\n"
2209                         "\tFCF_Avail     : x%x\n"
2210                         "\tFCF_Valid     : x%x\n"
2211                         "\tFCF_SOL       : x%x\n"
2212                         "\tFIP_Priority  : x%x\n"
2213                         "\tMAC_Provider  : x%x\n"
2214                         "\tLowest VLANID : x%x\n"
2215                         "\tFCF_MAC Addr  : x%x:%x:%x:%x:%x:%x\n"
2216                         "\tFabric_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
2217                         "\tSwitch_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
2218                         "\tNext_FCF_Index: x%x\n",
2219                         bf_get(lpfc_fcf_record_fcf_index, fcf_record),
2220                         bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
2221                         bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
2222                         bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
2223                         fcf_record->fip_priority,
2224                         bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
2225                         vlan_id,
2226                         bf_get(lpfc_fcf_record_mac_0, fcf_record),
2227                         bf_get(lpfc_fcf_record_mac_1, fcf_record),
2228                         bf_get(lpfc_fcf_record_mac_2, fcf_record),
2229                         bf_get(lpfc_fcf_record_mac_3, fcf_record),
2230                         bf_get(lpfc_fcf_record_mac_4, fcf_record),
2231                         bf_get(lpfc_fcf_record_mac_5, fcf_record),
2232                         bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
2233                         bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
2234                         bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
2235                         bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
2236                         bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
2237                         bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
2238                         bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
2239                         bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
2240                         bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
2241                         bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
2242                         bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
2243                         bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
2244                         bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
2245                         bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
2246                         bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
2247                         bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
2248                         next_fcf_index);
2249 }
2250
2251 /**
2252  * lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
2253  * @phba: pointer to lpfc hba data structure.
2254  * @fcf_rec: pointer to an existing FCF record.
2255  * @new_fcf_record: pointer to a new FCF record.
2256  * @new_vlan_id: vlan id from the new FCF record.
2257  *
2258  * This function performs matching test of a new FCF record against an existing
2259  * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
2260  * will not be used as part of the FCF record matching criteria.
2261  *
2262  * Returns true if all the fields matching, otherwise returns false.
2263  */
2264 static bool
2265 lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
2266                            struct lpfc_fcf_rec *fcf_rec,
2267                            struct fcf_record *new_fcf_record,
2268                            uint16_t new_vlan_id)
2269 {
2270         if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
2271                 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
2272                         return false;
2273         if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
2274                 return false;
2275         if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
2276                 return false;
2277         if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
2278                 return false;
2279         if (fcf_rec->priority != new_fcf_record->fip_priority)
2280                 return false;
2281         return true;
2282 }
2283
2284 /**
2285  * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
2286  * @vport: Pointer to vport object.
2287  * @fcf_index: index to next fcf.
2288  *
2289  * This function processing the roundrobin fcf failover to next fcf index.
2290  * When this function is invoked, there will be a current fcf registered
2291  * for flogi.
2292  * Return: 0 for continue retrying flogi on currently registered fcf;
2293  *         1 for stop flogi on currently registered fcf;
2294  */
2295 int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
2296 {
2297         struct lpfc_hba *phba = vport->phba;
2298         int rc;
2299
2300         if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
2301                 spin_lock_irq(&phba->hbalock);
2302                 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2303                         spin_unlock_irq(&phba->hbalock);
2304                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2305                                         "2872 Devloss tmo with no eligible "
2306                                         "FCF, unregister in-use FCF (x%x) "
2307                                         "and rescan FCF table\n",
2308                                         phba->fcf.current_rec.fcf_indx);
2309                         lpfc_unregister_fcf_rescan(phba);
2310                         goto stop_flogi_current_fcf;
2311                 }
2312                 /* Mark the end to FLOGI roundrobin failover */
2313                 phba->hba_flag &= ~FCF_RR_INPROG;
2314                 /* Allow action to new fcf asynchronous event */
2315                 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2316                 spin_unlock_irq(&phba->hbalock);
2317                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2318                                 "2865 No FCF available, stop roundrobin FCF "
2319                                 "failover and change port state:x%x/x%x\n",
2320                                 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
2321                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2322
2323                 if (!phba->fcf.fcf_redisc_attempted) {
2324                         lpfc_unregister_fcf(phba);
2325
2326                         rc = lpfc_sli4_redisc_fcf_table(phba);
2327                         if (!rc) {
2328                                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2329                                                 "3195 Rediscover FCF table\n");
2330                                 phba->fcf.fcf_redisc_attempted = 1;
2331                                 lpfc_sli4_clear_fcf_rr_bmask(phba);
2332                         } else {
2333                                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2334                                                 "3196 Rediscover FCF table "
2335                                                 "failed. Status:x%x\n", rc);
2336                         }
2337                 } else {
2338                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2339                                         "3197 Already rediscover FCF table "
2340                                         "attempted. No more retry\n");
2341                 }
2342                 goto stop_flogi_current_fcf;
2343         } else {
2344                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
2345                                 "2794 Try FLOGI roundrobin FCF failover to "
2346                                 "(x%x)\n", fcf_index);
2347                 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
2348                 if (rc)
2349                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
2350                                         "2761 FLOGI roundrobin FCF failover "
2351                                         "failed (rc:x%x) to read FCF (x%x)\n",
2352                                         rc, phba->fcf.current_rec.fcf_indx);
2353                 else
2354                         goto stop_flogi_current_fcf;
2355         }
2356         return 0;
2357
2358 stop_flogi_current_fcf:
2359         lpfc_can_disctmo(vport);
2360         return 1;
2361 }
2362
2363 /**
2364  * lpfc_sli4_fcf_pri_list_del
2365  * @phba: pointer to lpfc hba data structure.
2366  * @fcf_index: the index of the fcf record to delete
2367  * This routine checks the on list flag of the fcf_index to be deleted.
2368  * If it is one the list then it is removed from the list, and the flag
2369  * is cleared. This routine grab the hbalock before removing the fcf
2370  * record from the list.
2371  **/
2372 static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2373                         uint16_t fcf_index)
2374 {
2375         struct lpfc_fcf_pri *new_fcf_pri;
2376
2377         new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2378         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2379                 "3058 deleting idx x%x pri x%x flg x%x\n",
2380                 fcf_index, new_fcf_pri->fcf_rec.priority,
2381                  new_fcf_pri->fcf_rec.flag);
2382         spin_lock_irq(&phba->hbalock);
2383         if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2384                 if (phba->fcf.current_rec.priority ==
2385                                 new_fcf_pri->fcf_rec.priority)
2386                         phba->fcf.eligible_fcf_cnt--;
2387                 list_del_init(&new_fcf_pri->list);
2388                 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2389         }
2390         spin_unlock_irq(&phba->hbalock);
2391 }
2392
2393 /**
2394  * lpfc_sli4_set_fcf_flogi_fail
2395  * @phba: pointer to lpfc hba data structure.
2396  * @fcf_index: the index of the fcf record to update
2397  * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2398  * flag so the the round robin slection for the particular priority level
2399  * will try a different fcf record that does not have this bit set.
2400  * If the fcf record is re-read for any reason this flag is cleared brfore
2401  * adding it to the priority list.
2402  **/
2403 void
2404 lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2405 {
2406         struct lpfc_fcf_pri *new_fcf_pri;
2407         new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2408         spin_lock_irq(&phba->hbalock);
2409         new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2410         spin_unlock_irq(&phba->hbalock);
2411 }
2412
2413 /**
2414  * lpfc_sli4_fcf_pri_list_add
2415  * @phba: pointer to lpfc hba data structure.
2416  * @fcf_index: the index of the fcf record to add
2417  * @new_fcf_record: pointer to a new FCF record.
2418  * This routine checks the priority of the fcf_index to be added.
2419  * If it is a lower priority than the current head of the fcf_pri list
2420  * then it is added to the list in the right order.
2421  * If it is the same priority as the current head of the list then it
2422  * is added to the head of the list and its bit in the rr_bmask is set.
2423  * If the fcf_index to be added is of a higher priority than the current
2424  * head of the list then the rr_bmask is cleared, its bit is set in the
2425  * rr_bmask and it is added to the head of the list.
2426  * returns:
2427  * 0=success 1=failure
2428  **/
2429 static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2430         uint16_t fcf_index,
2431         struct fcf_record *new_fcf_record)
2432 {
2433         uint16_t current_fcf_pri;
2434         uint16_t last_index;
2435         struct lpfc_fcf_pri *fcf_pri;
2436         struct lpfc_fcf_pri *next_fcf_pri;
2437         struct lpfc_fcf_pri *new_fcf_pri;
2438         int ret;
2439
2440         new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2441         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2442                 "3059 adding idx x%x pri x%x flg x%x\n",
2443                 fcf_index, new_fcf_record->fip_priority,
2444                  new_fcf_pri->fcf_rec.flag);
2445         spin_lock_irq(&phba->hbalock);
2446         if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2447                 list_del_init(&new_fcf_pri->list);
2448         new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2449         new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2450         if (list_empty(&phba->fcf.fcf_pri_list)) {
2451                 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2452                 ret = lpfc_sli4_fcf_rr_index_set(phba,
2453                                 new_fcf_pri->fcf_rec.fcf_index);
2454                 goto out;
2455         }
2456
2457         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2458                                 LPFC_SLI4_FCF_TBL_INDX_MAX);
2459         if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2460                 ret = 0; /* Empty rr list */
2461                 goto out;
2462         }
2463         current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2464         if (new_fcf_pri->fcf_rec.priority <=  current_fcf_pri) {
2465                 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2466                 if (new_fcf_pri->fcf_rec.priority <  current_fcf_pri) {
2467                         memset(phba->fcf.fcf_rr_bmask, 0,
2468                                 sizeof(*phba->fcf.fcf_rr_bmask));
2469                         /* fcfs_at_this_priority_level = 1; */
2470                         phba->fcf.eligible_fcf_cnt = 1;
2471                 } else
2472                         /* fcfs_at_this_priority_level++; */
2473                         phba->fcf.eligible_fcf_cnt++;
2474                 ret = lpfc_sli4_fcf_rr_index_set(phba,
2475                                 new_fcf_pri->fcf_rec.fcf_index);
2476                 goto out;
2477         }
2478
2479         list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2480                                 &phba->fcf.fcf_pri_list, list) {
2481                 if (new_fcf_pri->fcf_rec.priority <=
2482                                 fcf_pri->fcf_rec.priority) {
2483                         if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2484                                 list_add(&new_fcf_pri->list,
2485                                                 &phba->fcf.fcf_pri_list);
2486                         else
2487                                 list_add(&new_fcf_pri->list,
2488                                          &((struct lpfc_fcf_pri *)
2489                                         fcf_pri->list.prev)->list);
2490                         ret = 0;
2491                         goto out;
2492                 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2493                         || new_fcf_pri->fcf_rec.priority <
2494                                 next_fcf_pri->fcf_rec.priority) {
2495                         list_add(&new_fcf_pri->list, &fcf_pri->list);
2496                         ret = 0;
2497                         goto out;
2498                 }
2499                 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2500                         continue;
2501
2502         }
2503         ret = 1;
2504 out:
2505         /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2506         new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2507         spin_unlock_irq(&phba->hbalock);
2508         return ret;
2509 }
2510
2511 /**
2512  * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2513  * @phba: pointer to lpfc hba data structure.
2514  * @mboxq: pointer to mailbox object.
2515  *
2516  * This function iterates through all the fcf records available in
2517  * HBA and chooses the optimal FCF record for discovery. After finding
2518  * the FCF for discovery it registers the FCF record and kicks start
2519  * discovery.
2520  * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2521  * use an FCF record which matches fabric name and mac address of the
2522  * currently used FCF record.
2523  * If the driver supports only one FCF, it will try to use the FCF record
2524  * used by BOOT_BIOS.
2525  */
2526 void
2527 lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2528 {
2529         struct fcf_record *new_fcf_record;
2530         uint32_t boot_flag, addr_mode;
2531         uint16_t fcf_index, next_fcf_index;
2532         struct lpfc_fcf_rec *fcf_rec = NULL;
2533         uint16_t vlan_id = LPFC_FCOE_NULL_VID;
2534         bool select_new_fcf;
2535         int rc;
2536
2537         /* If there is pending FCoE event restart FCF table scan */
2538         if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
2539                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2540                 return;
2541         }
2542
2543         /* Parse the FCF record from the non-embedded mailbox command */
2544         new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2545                                                       &next_fcf_index);
2546         if (!new_fcf_record) {
2547                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2548                                 "2765 Mailbox command READ_FCF_RECORD "
2549                                 "failed to retrieve a FCF record.\n");
2550                 /* Let next new FCF event trigger fast failover */
2551                 spin_lock_irq(&phba->hbalock);
2552                 phba->hba_flag &= ~FCF_TS_INPROG;
2553                 spin_unlock_irq(&phba->hbalock);
2554                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2555                 return;
2556         }
2557
2558         /* Check the FCF record against the connection list */
2559         rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2560                                       &addr_mode, &vlan_id);
2561
2562         /* Log the FCF record information if turned on */
2563         lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2564                                       next_fcf_index);
2565
2566         /*
2567          * If the fcf record does not match with connect list entries
2568          * read the next entry; otherwise, this is an eligible FCF
2569          * record for roundrobin FCF failover.
2570          */
2571         if (!rc) {
2572                 lpfc_sli4_fcf_pri_list_del(phba,
2573                                         bf_get(lpfc_fcf_record_fcf_index,
2574                                                new_fcf_record));
2575                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2576                                 "2781 FCF (x%x) failed connection "
2577                                 "list check: (x%x/x%x/%x)\n",
2578                                 bf_get(lpfc_fcf_record_fcf_index,
2579                                        new_fcf_record),
2580                                 bf_get(lpfc_fcf_record_fcf_avail,
2581                                        new_fcf_record),
2582                                 bf_get(lpfc_fcf_record_fcf_valid,
2583                                        new_fcf_record),
2584                                 bf_get(lpfc_fcf_record_fcf_sol,
2585                                        new_fcf_record));
2586                 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2587                     lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2588                     new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
2589                         if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2590                             phba->fcf.current_rec.fcf_indx) {
2591                                 lpfc_printf_log(phba, KERN_ERR,
2592                                                 LOG_TRACE_EVENT,
2593                                         "2862 FCF (x%x) matches property "
2594                                         "of in-use FCF (x%x)\n",
2595                                         bf_get(lpfc_fcf_record_fcf_index,
2596                                                new_fcf_record),
2597                                         phba->fcf.current_rec.fcf_indx);
2598                                 goto read_next_fcf;
2599                         }
2600                         /*
2601                          * In case the current in-use FCF record becomes
2602                          * invalid/unavailable during FCF discovery that
2603                          * was not triggered by fast FCF failover process,
2604                          * treat it as fast FCF failover.
2605                          */
2606                         if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2607                             !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2608                                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2609                                                 "2835 Invalid in-use FCF "
2610                                                 "(x%x), enter FCF failover "
2611                                                 "table scan.\n",
2612                                                 phba->fcf.current_rec.fcf_indx);
2613                                 spin_lock_irq(&phba->hbalock);
2614                                 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2615                                 spin_unlock_irq(&phba->hbalock);
2616                                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2617                                 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2618                                                 LPFC_FCOE_FCF_GET_FIRST);
2619                                 return;
2620                         }
2621                 }
2622                 goto read_next_fcf;
2623         } else {
2624                 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2625                 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2626                                                         new_fcf_record);
2627                 if (rc)
2628                         goto read_next_fcf;
2629         }
2630
2631         /*
2632          * If this is not the first FCF discovery of the HBA, use last
2633          * FCF record for the discovery. The condition that a rescan
2634          * matches the in-use FCF record: fabric name, switch name, mac
2635          * address, and vlan_id.
2636          */
2637         spin_lock_irq(&phba->hbalock);
2638         if (phba->fcf.fcf_flag & FCF_IN_USE) {
2639                 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2640                         lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2641                     new_fcf_record, vlan_id)) {
2642                         if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2643                             phba->fcf.current_rec.fcf_indx) {
2644                                 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2645                                 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2646                                         /* Stop FCF redisc wait timer */
2647                                         __lpfc_sli4_stop_fcf_redisc_wait_timer(
2648                                                                         phba);
2649                                 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2650                                         /* Fast failover, mark completed */
2651                                         phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2652                                 spin_unlock_irq(&phba->hbalock);
2653                                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2654                                                 "2836 New FCF matches in-use "
2655                                                 "FCF (x%x), port_state:x%x, "
2656                                                 "fc_flag:x%x\n",
2657                                                 phba->fcf.current_rec.fcf_indx,
2658                                                 phba->pport->port_state,
2659                                                 phba->pport->fc_flag);
2660                                 goto out;
2661                         } else
2662                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2663                                         "2863 New FCF (x%x) matches "
2664                                         "property of in-use FCF (x%x)\n",
2665                                         bf_get(lpfc_fcf_record_fcf_index,
2666                                                new_fcf_record),
2667                                         phba->fcf.current_rec.fcf_indx);
2668                 }
2669                 /*
2670                  * Read next FCF record from HBA searching for the matching
2671                  * with in-use record only if not during the fast failover
2672                  * period. In case of fast failover period, it shall try to
2673                  * determine whether the FCF record just read should be the
2674                  * next candidate.
2675                  */
2676                 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2677                         spin_unlock_irq(&phba->hbalock);
2678                         goto read_next_fcf;
2679                 }
2680         }
2681         /*
2682          * Update on failover FCF record only if it's in FCF fast-failover
2683          * period; otherwise, update on current FCF record.
2684          */
2685         if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2686                 fcf_rec = &phba->fcf.failover_rec;
2687         else
2688                 fcf_rec = &phba->fcf.current_rec;
2689
2690         if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2691                 /*
2692                  * If the driver FCF record does not have boot flag
2693                  * set and new hba fcf record has boot flag set, use
2694                  * the new hba fcf record.
2695                  */
2696                 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2697                         /* Choose this FCF record */
2698                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2699                                         "2837 Update current FCF record "
2700                                         "(x%x) with new FCF record (x%x)\n",
2701                                         fcf_rec->fcf_indx,
2702                                         bf_get(lpfc_fcf_record_fcf_index,
2703                                         new_fcf_record));
2704                         __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2705                                         addr_mode, vlan_id, BOOT_ENABLE);
2706                         spin_unlock_irq(&phba->hbalock);
2707                         goto read_next_fcf;
2708                 }
2709                 /*
2710                  * If the driver FCF record has boot flag set and the
2711                  * new hba FCF record does not have boot flag, read
2712                  * the next FCF record.
2713                  */
2714                 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
2715                         spin_unlock_irq(&phba->hbalock);
2716                         goto read_next_fcf;
2717                 }
2718                 /*
2719                  * If the new hba FCF record has lower priority value
2720                  * than the driver FCF record, use the new record.
2721                  */
2722                 if (new_fcf_record->fip_priority < fcf_rec->priority) {
2723                         /* Choose the new FCF record with lower priority */
2724                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2725                                         "2838 Update current FCF record "
2726                                         "(x%x) with new FCF record (x%x)\n",
2727                                         fcf_rec->fcf_indx,
2728                                         bf_get(lpfc_fcf_record_fcf_index,
2729                                                new_fcf_record));
2730                         __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2731                                         addr_mode, vlan_id, 0);
2732                         /* Reset running random FCF selection count */
2733                         phba->fcf.eligible_fcf_cnt = 1;
2734                 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2735                         /* Update running random FCF selection count */
2736                         phba->fcf.eligible_fcf_cnt++;
2737                         select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2738                                                 phba->fcf.eligible_fcf_cnt);
2739                         if (select_new_fcf) {
2740                                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2741                                         "2839 Update current FCF record "
2742                                         "(x%x) with new FCF record (x%x)\n",
2743                                         fcf_rec->fcf_indx,
2744                                         bf_get(lpfc_fcf_record_fcf_index,
2745                                                new_fcf_record));
2746                                 /* Choose the new FCF by random selection */
2747                                 __lpfc_update_fcf_record(phba, fcf_rec,
2748                                                          new_fcf_record,
2749                                                          addr_mode, vlan_id, 0);
2750                         }
2751                 }
2752                 spin_unlock_irq(&phba->hbalock);
2753                 goto read_next_fcf;
2754         }
2755         /*
2756          * This is the first suitable FCF record, choose this record for
2757          * initial best-fit FCF.
2758          */
2759         if (fcf_rec) {
2760                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2761                                 "2840 Update initial FCF candidate "
2762                                 "with FCF (x%x)\n",
2763                                 bf_get(lpfc_fcf_record_fcf_index,
2764                                        new_fcf_record));
2765                 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2766                                          addr_mode, vlan_id, (boot_flag ?
2767                                          BOOT_ENABLE : 0));
2768                 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2769                 /* Setup initial running random FCF selection count */
2770                 phba->fcf.eligible_fcf_cnt = 1;
2771         }
2772         spin_unlock_irq(&phba->hbalock);
2773         goto read_next_fcf;
2774
2775 read_next_fcf:
2776         lpfc_sli4_mbox_cmd_free(phba, mboxq);
2777         if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2778                 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2779                         /*
2780                          * Case of FCF fast failover scan
2781                          */
2782
2783                         /*
2784                          * It has not found any suitable FCF record, cancel
2785                          * FCF scan inprogress, and do nothing
2786                          */
2787                         if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
2788                                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2789                                                "2782 No suitable FCF found: "
2790                                                "(x%x/x%x)\n",
2791                                                phba->fcoe_eventtag_at_fcf_scan,
2792                                                bf_get(lpfc_fcf_record_fcf_index,
2793                                                       new_fcf_record));
2794                                 spin_lock_irq(&phba->hbalock);
2795                                 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2796                                         phba->hba_flag &= ~FCF_TS_INPROG;
2797                                         spin_unlock_irq(&phba->hbalock);
2798                                         /* Unregister in-use FCF and rescan */
2799                                         lpfc_printf_log(phba, KERN_INFO,
2800                                                         LOG_FIP,
2801                                                         "2864 On devloss tmo "
2802                                                         "unreg in-use FCF and "
2803                                                         "rescan FCF table\n");
2804                                         lpfc_unregister_fcf_rescan(phba);
2805                                         return;
2806                                 }
2807                                 /*
2808                                  * Let next new FCF event trigger fast failover
2809                                  */
2810                                 phba->hba_flag &= ~FCF_TS_INPROG;
2811                                 spin_unlock_irq(&phba->hbalock);
2812                                 return;
2813                         }
2814                         /*
2815                          * It has found a suitable FCF record that is not
2816                          * the same as in-use FCF record, unregister the
2817                          * in-use FCF record, replace the in-use FCF record
2818                          * with the new FCF record, mark FCF fast failover
2819                          * completed, and then start register the new FCF
2820                          * record.
2821                          */
2822
2823                         /* Unregister the current in-use FCF record */
2824                         lpfc_unregister_fcf(phba);
2825
2826                         /* Replace in-use record with the new record */
2827                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2828                                         "2842 Replace in-use FCF (x%x) "
2829                                         "with failover FCF (x%x)\n",
2830                                         phba->fcf.current_rec.fcf_indx,
2831                                         phba->fcf.failover_rec.fcf_indx);
2832                         memcpy(&phba->fcf.current_rec,
2833                                &phba->fcf.failover_rec,
2834                                sizeof(struct lpfc_fcf_rec));
2835                         /*
2836                          * Mark the fast FCF failover rediscovery completed
2837                          * and the start of the first round of the roundrobin
2838                          * FCF failover.
2839                          */
2840                         spin_lock_irq(&phba->hbalock);
2841                         phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2842                         spin_unlock_irq(&phba->hbalock);
2843                         /* Register to the new FCF record */
2844                         lpfc_register_fcf(phba);
2845                 } else {
2846                         /*
2847                          * In case of transaction period to fast FCF failover,
2848                          * do nothing when search to the end of the FCF table.
2849                          */
2850                         if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2851                             (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2852                                 return;
2853
2854                         if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2855                                 phba->fcf.fcf_flag & FCF_IN_USE) {
2856                                 /*
2857                                  * In case the current in-use FCF record no
2858                                  * longer existed during FCF discovery that
2859                                  * was not triggered by fast FCF failover
2860                                  * process, treat it as fast FCF failover.
2861                                  */
2862                                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2863                                                 "2841 In-use FCF record (x%x) "
2864                                                 "not reported, entering fast "
2865                                                 "FCF failover mode scanning.\n",
2866                                                 phba->fcf.current_rec.fcf_indx);
2867                                 spin_lock_irq(&phba->hbalock);
2868                                 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2869                                 spin_unlock_irq(&phba->hbalock);
2870                                 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2871                                                 LPFC_FCOE_FCF_GET_FIRST);
2872                                 return;
2873                         }
2874                         /* Register to the new FCF record */
2875                         lpfc_register_fcf(phba);
2876                 }
2877         } else
2878                 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
2879         return;
2880
2881 out:
2882         lpfc_sli4_mbox_cmd_free(phba, mboxq);
2883         lpfc_register_fcf(phba);
2884
2885         return;
2886 }
2887
2888 /**
2889  * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
2890  * @phba: pointer to lpfc hba data structure.
2891  * @mboxq: pointer to mailbox object.
2892  *
2893  * This is the callback function for FLOGI failure roundrobin FCF failover
2894  * read FCF record mailbox command from the eligible FCF record bmask for
2895  * performing the failover. If the FCF read back is not valid/available, it
2896  * fails through to retrying FLOGI to the currently registered FCF again.
2897  * Otherwise, if the FCF read back is valid and available, it will set the
2898  * newly read FCF record to the failover FCF record, unregister currently
2899  * registered FCF record, copy the failover FCF record to the current
2900  * FCF record, and then register the current FCF record before proceeding
2901  * to trying FLOGI on the new failover FCF.
2902  */
2903 void
2904 lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2905 {
2906         struct fcf_record *new_fcf_record;
2907         uint32_t boot_flag, addr_mode;
2908         uint16_t next_fcf_index, fcf_index;
2909         uint16_t current_fcf_index;
2910         uint16_t vlan_id;
2911         int rc;
2912
2913         /* If link state is not up, stop the roundrobin failover process */
2914         if (phba->link_state < LPFC_LINK_UP) {
2915                 spin_lock_irq(&phba->hbalock);
2916                 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
2917                 phba->hba_flag &= ~FCF_RR_INPROG;
2918                 spin_unlock_irq(&phba->hbalock);
2919                 goto out;
2920         }
2921
2922         /* Parse the FCF record from the non-embedded mailbox command */
2923         new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2924                                                       &next_fcf_index);
2925         if (!new_fcf_record) {
2926                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2927                                 "2766 Mailbox command READ_FCF_RECORD "
2928                                 "failed to retrieve a FCF record. "
2929                                 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2930                                 phba->fcf.fcf_flag);
2931                 lpfc_unregister_fcf_rescan(phba);
2932                 goto out;
2933         }
2934
2935         /* Get the needed parameters from FCF record */
2936         rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2937                                       &addr_mode, &vlan_id);
2938
2939         /* Log the FCF record information if turned on */
2940         lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2941                                       next_fcf_index);
2942
2943         fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2944         if (!rc) {
2945                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2946                                 "2848 Remove ineligible FCF (x%x) from "
2947                                 "from roundrobin bmask\n", fcf_index);
2948                 /* Clear roundrobin bmask bit for ineligible FCF */
2949                 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2950                 /* Perform next round of roundrobin FCF failover */
2951                 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2952                 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2953                 if (rc)
2954                         goto out;
2955                 goto error_out;
2956         }
2957
2958         if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2959                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2960                                 "2760 Perform FLOGI roundrobin FCF failover: "
2961                                 "FCF (x%x) back to FCF (x%x)\n",
2962                                 phba->fcf.current_rec.fcf_indx, fcf_index);
2963                 /* Wait 500 ms before retrying FLOGI to current FCF */
2964                 msleep(500);
2965                 lpfc_issue_init_vfi(phba->pport);
2966                 goto out;
2967         }
2968
2969         /* Upload new FCF record to the failover FCF record */
2970         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2971                         "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2972                         phba->fcf.failover_rec.fcf_indx, fcf_index);
2973         spin_lock_irq(&phba->hbalock);
2974         __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2975                                  new_fcf_record, addr_mode, vlan_id,
2976                                  (boot_flag ? BOOT_ENABLE : 0));
2977         spin_unlock_irq(&phba->hbalock);
2978
2979         current_fcf_index = phba->fcf.current_rec.fcf_indx;
2980
2981         /* Unregister the current in-use FCF record */
2982         lpfc_unregister_fcf(phba);
2983
2984         /* Replace in-use record with the new record */
2985         memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2986                sizeof(struct lpfc_fcf_rec));
2987
2988         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2989                         "2783 Perform FLOGI roundrobin FCF failover: FCF "
2990                         "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
2991
2992 error_out:
2993         lpfc_register_fcf(phba);
2994 out:
2995         lpfc_sli4_mbox_cmd_free(phba, mboxq);
2996 }
2997
2998 /**
2999  * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
3000  * @phba: pointer to lpfc hba data structure.
3001  * @mboxq: pointer to mailbox object.
3002  *
3003  * This is the callback function of read FCF record mailbox command for
3004  * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
3005  * failover when a new FCF event happened. If the FCF read back is
3006  * valid/available and it passes the connection list check, it updates
3007  * the bmask for the eligible FCF record for roundrobin failover.
3008  */
3009 void
3010 lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3011 {
3012         struct fcf_record *new_fcf_record;
3013         uint32_t boot_flag, addr_mode;
3014         uint16_t fcf_index, next_fcf_index;
3015         uint16_t vlan_id;
3016         int rc;
3017
3018         /* If link state is not up, no need to proceed */
3019         if (phba->link_state < LPFC_LINK_UP)
3020                 goto out;
3021
3022         /* If FCF discovery period is over, no need to proceed */
3023         if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
3024                 goto out;
3025
3026         /* Parse the FCF record from the non-embedded mailbox command */
3027         new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
3028                                                       &next_fcf_index);
3029         if (!new_fcf_record) {
3030                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3031                                 "2767 Mailbox command READ_FCF_RECORD "
3032                                 "failed to retrieve a FCF record.\n");
3033                 goto out;
3034         }
3035
3036         /* Check the connection list for eligibility */
3037         rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
3038                                       &addr_mode, &vlan_id);
3039
3040         /* Log the FCF record information if turned on */
3041         lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
3042                                       next_fcf_index);
3043
3044         if (!rc)
3045                 goto out;
3046
3047         /* Update the eligible FCF record index bmask */
3048         fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
3049
3050         rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
3051
3052 out:
3053         lpfc_sli4_mbox_cmd_free(phba, mboxq);
3054 }
3055
3056 /**
3057  * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
3058  * @phba: pointer to lpfc hba data structure.
3059  * @mboxq: pointer to mailbox data structure.
3060  *
3061  * This function handles completion of init vfi mailbox command.
3062  */
3063 static void
3064 lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3065 {
3066         struct lpfc_vport *vport = mboxq->vport;
3067
3068         /*
3069          * VFI not supported on interface type 0, just do the flogi
3070          * Also continue if the VFI is in use - just use the same one.
3071          */
3072         if (mboxq->u.mb.mbxStatus &&
3073             (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3074                         LPFC_SLI_INTF_IF_TYPE_0) &&
3075             mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
3076                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3077                                  "2891 Init VFI mailbox failed 0x%x\n",
3078                                  mboxq->u.mb.mbxStatus);
3079                 mempool_free(mboxq, phba->mbox_mem_pool);
3080                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3081                 return;
3082         }
3083
3084         lpfc_initial_flogi(vport);
3085         mempool_free(mboxq, phba->mbox_mem_pool);
3086         return;
3087 }
3088
3089 /**
3090  * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
3091  * @vport: pointer to lpfc_vport data structure.
3092  *
3093  * This function issue a init_vfi mailbox command to initialize the VFI and
3094  * VPI for the physical port.
3095  */
3096 void
3097 lpfc_issue_init_vfi(struct lpfc_vport *vport)
3098 {
3099         LPFC_MBOXQ_t *mboxq;
3100         int rc;
3101         struct lpfc_hba *phba = vport->phba;
3102
3103         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3104         if (!mboxq) {
3105                 lpfc_printf_vlog(vport, KERN_ERR,
3106                         LOG_TRACE_EVENT, "2892 Failed to allocate "
3107                         "init_vfi mailbox\n");
3108                 return;
3109         }
3110         lpfc_init_vfi(mboxq, vport);
3111         mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
3112         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
3113         if (rc == MBX_NOT_FINISHED) {
3114                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3115                                  "2893 Failed to issue init_vfi mailbox\n");
3116                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
3117         }
3118 }
3119
3120 /**
3121  * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
3122  * @phba: pointer to lpfc hba data structure.
3123  * @mboxq: pointer to mailbox data structure.
3124  *
3125  * This function handles completion of init vpi mailbox command.
3126  */
3127 void
3128 lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3129 {
3130         struct lpfc_vport *vport = mboxq->vport;
3131         struct lpfc_nodelist *ndlp;
3132         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3133
3134         if (mboxq->u.mb.mbxStatus) {
3135                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3136                                  "2609 Init VPI mailbox failed 0x%x\n",
3137                                  mboxq->u.mb.mbxStatus);
3138                 mempool_free(mboxq, phba->mbox_mem_pool);
3139                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3140                 return;
3141         }
3142         spin_lock_irq(shost->host_lock);
3143         vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
3144         spin_unlock_irq(shost->host_lock);
3145
3146         /* If this port is physical port or FDISC is done, do reg_vpi */
3147         if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
3148                         ndlp = lpfc_findnode_did(vport, Fabric_DID);
3149                         if (!ndlp)
3150                                 lpfc_printf_vlog(vport, KERN_ERR,
3151                                         LOG_TRACE_EVENT,
3152                                         "2731 Cannot find fabric "
3153                                         "controller node\n");
3154                         else
3155                                 lpfc_register_new_vport(phba, vport, ndlp);
3156                         mempool_free(mboxq, phba->mbox_mem_pool);
3157                         return;
3158         }
3159
3160         if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
3161                 lpfc_initial_fdisc(vport);
3162         else {
3163                 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
3164                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3165                                  "2606 No NPIV Fabric support\n");
3166         }
3167         mempool_free(mboxq, phba->mbox_mem_pool);
3168         return;
3169 }
3170
3171 /**
3172  * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
3173  * @vport: pointer to lpfc_vport data structure.
3174  *
3175  * This function issue a init_vpi mailbox command to initialize
3176  * VPI for the vport.
3177  */
3178 void
3179 lpfc_issue_init_vpi(struct lpfc_vport *vport)
3180 {
3181         LPFC_MBOXQ_t *mboxq;
3182         int rc, vpi;
3183
3184         if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
3185                 vpi = lpfc_alloc_vpi(vport->phba);
3186                 if (!vpi) {
3187                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3188                                          "3303 Failed to obtain vport vpi\n");
3189                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3190                         return;
3191                 }
3192                 vport->vpi = vpi;
3193         }
3194
3195         mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
3196         if (!mboxq) {
3197                 lpfc_printf_vlog(vport, KERN_ERR,
3198                         LOG_TRACE_EVENT, "2607 Failed to allocate "
3199                         "init_vpi mailbox\n");
3200                 return;
3201         }
3202         lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
3203         mboxq->vport = vport;
3204         mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
3205         rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
3206         if (rc == MBX_NOT_FINISHED) {
3207                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3208                                  "2608 Failed to issue init_vpi mailbox\n");
3209                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
3210         }
3211 }
3212
3213 /**
3214  * lpfc_start_fdiscs - send fdiscs for each vports on this port.
3215  * @phba: pointer to lpfc hba data structure.
3216  *
3217  * This function loops through the list of vports on the @phba and issues an
3218  * FDISC if possible.
3219  */
3220 void
3221 lpfc_start_fdiscs(struct lpfc_hba *phba)
3222 {
3223         struct lpfc_vport **vports;
3224         int i;
3225
3226         vports = lpfc_create_vport_work_array(phba);
3227         if (vports != NULL) {
3228                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3229                         if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
3230                                 continue;
3231                         /* There are no vpi for this vport */
3232                         if (vports[i]->vpi > phba->max_vpi) {
3233                                 lpfc_vport_set_state(vports[i],
3234                                                      FC_VPORT_FAILED);
3235                                 continue;
3236                         }
3237                         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3238                                 lpfc_vport_set_state(vports[i],
3239                                                      FC_VPORT_LINKDOWN);
3240                                 continue;
3241                         }
3242                         if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
3243                                 lpfc_issue_init_vpi(vports[i]);
3244                                 continue;
3245                         }
3246                         if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
3247                                 lpfc_initial_fdisc(vports[i]);
3248                         else {
3249                                 lpfc_vport_set_state(vports[i],
3250                                                      FC_VPORT_NO_FABRIC_SUPP);
3251                                 lpfc_printf_vlog(vports[i], KERN_ERR,
3252                                                  LOG_TRACE_EVENT,
3253                                                  "0259 No NPIV "
3254                                                  "Fabric support\n");
3255                         }
3256                 }
3257         }
3258         lpfc_destroy_vport_work_array(phba, vports);
3259 }
3260
3261 void
3262 lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3263 {
3264         struct lpfc_dmabuf *dmabuf = mboxq->ctx_buf;
3265         struct lpfc_vport *vport = mboxq->vport;
3266         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3267
3268         /*
3269          * VFI not supported for interface type 0, so ignore any mailbox
3270          * error (except VFI in use) and continue with the discovery.
3271          */
3272         if (mboxq->u.mb.mbxStatus &&
3273             (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3274                         LPFC_SLI_INTF_IF_TYPE_0) &&
3275             mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
3276                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3277                                  "2018 REG_VFI mbxStatus error x%x "
3278                                  "HBA state x%x\n",
3279                                  mboxq->u.mb.mbxStatus, vport->port_state);
3280                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3281                         /* FLOGI failed, use loop map to make discovery list */
3282                         lpfc_disc_list_loopmap(vport);
3283                         /* Start discovery */
3284                         lpfc_disc_start(vport);
3285                         goto out_free_mem;
3286                 }
3287                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3288                 goto out_free_mem;
3289         }
3290
3291         /* If the VFI is already registered, there is nothing else to do
3292          * Unless this was a VFI update and we are in PT2PT mode, then
3293          * we should drop through to set the port state to ready.
3294          */
3295         if (vport->fc_flag & FC_VFI_REGISTERED)
3296                 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
3297                       vport->fc_flag & FC_PT2PT))
3298                         goto out_free_mem;
3299
3300         /* The VPI is implicitly registered when the VFI is registered */
3301         spin_lock_irq(shost->host_lock);
3302         vport->vpi_state |= LPFC_VPI_REGISTERED;
3303         vport->fc_flag |= FC_VFI_REGISTERED;
3304         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
3305         vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
3306         spin_unlock_irq(shost->host_lock);
3307
3308         /* In case SLI4 FC loopback test, we are ready */
3309         if ((phba->sli_rev == LPFC_SLI_REV4) &&
3310             (phba->link_flag & LS_LOOPBACK_MODE)) {
3311                 phba->link_state = LPFC_HBA_READY;
3312                 goto out_free_mem;
3313         }
3314
3315         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3316                          "3313 cmpl reg vfi  port_state:%x fc_flag:%x myDid:%x "
3317                          "alpacnt:%d LinkState:%x topology:%x\n",
3318                          vport->port_state, vport->fc_flag, vport->fc_myDID,
3319                          vport->phba->alpa_map[0],
3320                          phba->link_state, phba->fc_topology);
3321
3322         if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
3323                 /*
3324                  * For private loop or for NPort pt2pt,
3325                  * just start discovery and we are done.
3326                  */
3327                 if ((vport->fc_flag & FC_PT2PT) ||
3328                     ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
3329                     !(vport->fc_flag & FC_PUBLIC_LOOP))) {
3330
3331                         /* Use loop map to make discovery list */
3332                         lpfc_disc_list_loopmap(vport);
3333                         /* Start discovery */
3334                         if (vport->fc_flag & FC_PT2PT)
3335                                 vport->port_state = LPFC_VPORT_READY;
3336                         else
3337                                 lpfc_disc_start(vport);
3338                 } else {
3339                         lpfc_start_fdiscs(phba);
3340                         lpfc_do_scr_ns_plogi(phba, vport);
3341                 }
3342         }
3343
3344 out_free_mem:
3345         mempool_free(mboxq, phba->mbox_mem_pool);
3346         if (dmabuf) {
3347                 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
3348                 kfree(dmabuf);
3349         }
3350         return;
3351 }
3352
3353 static void
3354 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3355 {
3356         MAILBOX_t *mb = &pmb->u.mb;
3357         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3358         struct lpfc_vport  *vport = pmb->vport;
3359         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3360         struct serv_parm *sp = &vport->fc_sparam;
3361         uint32_t ed_tov;
3362
3363         /* Check for error */
3364         if (mb->mbxStatus) {
3365                 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
3366                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3367                                  "0319 READ_SPARAM mbxStatus error x%x "
3368                                  "hba state x%x>\n",
3369                                  mb->mbxStatus, vport->port_state);
3370                 lpfc_linkdown(phba);
3371                 goto out;
3372         }
3373
3374         memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
3375                sizeof (struct serv_parm));
3376
3377         ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3378         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
3379                 ed_tov = (ed_tov + 999999) / 1000000;
3380
3381         phba->fc_edtov = ed_tov;
3382         phba->fc_ratov = (2 * ed_tov) / 1000;
3383         if (phba->fc_ratov < FF_DEF_RATOV) {
3384                 /* RA_TOV should be atleast 10sec for initial flogi */
3385                 phba->fc_ratov = FF_DEF_RATOV;
3386         }
3387
3388         lpfc_update_vport_wwn(vport);
3389         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
3390         if (vport->port_type == LPFC_PHYSICAL_PORT) {
3391                 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3392                 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3393         }
3394
3395         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3396         kfree(mp);
3397         mempool_free(pmb, phba->mbox_mem_pool);
3398
3399         /* Check if sending the FLOGI is being deferred to after we get
3400          * up to date CSPs from MBX_READ_SPARAM.
3401          */
3402         if (phba->hba_flag & HBA_DEFER_FLOGI) {
3403                 lpfc_initial_flogi(vport);
3404                 phba->hba_flag &= ~HBA_DEFER_FLOGI;
3405         }
3406         return;
3407
3408 out:
3409         pmb->ctx_buf = NULL;
3410         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3411         kfree(mp);
3412         lpfc_issue_clear_la(phba, vport);
3413         mempool_free(pmb, phba->mbox_mem_pool);
3414         return;
3415 }
3416
3417 static void
3418 lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
3419 {
3420         struct lpfc_vport *vport = phba->pport;
3421         LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
3422         struct Scsi_Host *shost;
3423         int i;
3424         struct lpfc_dmabuf *mp;
3425         int rc;
3426         struct fcf_record *fcf_record;
3427         uint32_t fc_flags = 0;
3428         unsigned long iflags;
3429
3430         spin_lock_irqsave(&phba->hbalock, iflags);
3431         phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3432
3433         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3434                 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3435                 case LPFC_LINK_SPEED_1GHZ:
3436                 case LPFC_LINK_SPEED_2GHZ:
3437                 case LPFC_LINK_SPEED_4GHZ:
3438                 case LPFC_LINK_SPEED_8GHZ:
3439                 case LPFC_LINK_SPEED_10GHZ:
3440                 case LPFC_LINK_SPEED_16GHZ:
3441                 case LPFC_LINK_SPEED_32GHZ:
3442                 case LPFC_LINK_SPEED_64GHZ:
3443                 case LPFC_LINK_SPEED_128GHZ:
3444                 case LPFC_LINK_SPEED_256GHZ:
3445                         break;
3446                 default:
3447                         phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3448                         break;
3449                 }
3450         }
3451
3452         if (phba->fc_topology &&
3453             phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3454                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3455                                 "3314 Toplogy changed was 0x%x is 0x%x\n",
3456                                 phba->fc_topology,
3457                                 bf_get(lpfc_mbx_read_top_topology, la));
3458                 phba->fc_topology_changed = 1;
3459         }
3460
3461         phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
3462         phba->link_flag &= ~(LS_NPIV_FAB_SUPPORTED | LS_CT_VEN_RPA);
3463
3464         shost = lpfc_shost_from_vport(vport);
3465         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3466                 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
3467
3468                 /* if npiv is enabled and this adapter supports npiv log
3469                  * a message that npiv is not supported in this topology
3470                  */
3471                 if (phba->cfg_enable_npiv && phba->max_vpi)
3472                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3473                                 "1309 Link Up Event npiv not supported in loop "
3474                                 "topology\n");
3475                                 /* Get Loop Map information */
3476                 if (bf_get(lpfc_mbx_read_top_il, la))
3477                         fc_flags |= FC_LBIT;
3478
3479                 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3480                 i = la->lilpBde64.tus.f.bdeSize;
3481
3482                 if (i == 0) {
3483                         phba->alpa_map[0] = 0;
3484                 } else {
3485                         if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
3486                                 int numalpa, j, k;
3487                                 union {
3488                                         uint8_t pamap[16];
3489                                         struct {
3490                                                 uint32_t wd1;
3491                                                 uint32_t wd2;
3492                                                 uint32_t wd3;
3493                                                 uint32_t wd4;
3494                                         } pa;
3495                                 } un;
3496                                 numalpa = phba->alpa_map[0];
3497                                 j = 0;
3498                                 while (j < numalpa) {
3499                                         memset(un.pamap, 0, 16);
3500                                         for (k = 1; j < numalpa; k++) {
3501                                                 un.pamap[k - 1] =
3502                                                         phba->alpa_map[j + 1];
3503                                                 j++;
3504                                                 if (k == 16)
3505                                                         break;
3506                                         }
3507                                         /* Link Up Event ALPA map */
3508                                         lpfc_printf_log(phba,
3509                                                         KERN_WARNING,
3510                                                         LOG_LINK_EVENT,
3511                                                         "1304 Link Up Event "
3512                                                         "ALPA map Data: x%x "
3513                                                         "x%x x%x x%x\n",
3514                                                         un.pa.wd1, un.pa.wd2,
3515                                                         un.pa.wd3, un.pa.wd4);
3516                                 }
3517                         }
3518                 }
3519         } else {
3520                 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
3521                         if (phba->max_vpi && phba->cfg_enable_npiv &&
3522                            (phba->sli_rev >= LPFC_SLI_REV3))
3523                                 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3524                 }
3525                 vport->fc_myDID = phba->fc_pref_DID;
3526                 fc_flags |= FC_LBIT;
3527         }
3528         spin_unlock_irqrestore(&phba->hbalock, iflags);
3529
3530         if (fc_flags) {
3531                 spin_lock_irqsave(shost->host_lock, iflags);
3532                 vport->fc_flag |= fc_flags;
3533                 spin_unlock_irqrestore(shost->host_lock, iflags);
3534         }
3535
3536         lpfc_linkup(phba);
3537         sparam_mbox = NULL;
3538
3539         sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3540         if (!sparam_mbox)
3541                 goto out;
3542
3543         rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3544         if (rc) {
3545                 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3546                 goto out;
3547         }
3548         sparam_mbox->vport = vport;
3549         sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3550         rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3551         if (rc == MBX_NOT_FINISHED) {
3552                 mp = (struct lpfc_dmabuf *)sparam_mbox->ctx_buf;
3553                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3554                 kfree(mp);
3555                 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3556                 goto out;
3557         }
3558
3559         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3560                 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3561                 if (!cfglink_mbox)
3562                         goto out;
3563                 vport->port_state = LPFC_LOCAL_CFG_LINK;
3564                 lpfc_config_link(phba, cfglink_mbox);
3565                 cfglink_mbox->vport = vport;
3566                 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
3567                 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
3568                 if (rc == MBX_NOT_FINISHED) {
3569                         mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3570                         goto out;
3571                 }
3572         } else {
3573                 vport->port_state = LPFC_VPORT_UNKNOWN;
3574                 /*
3575                  * Add the driver's default FCF record at FCF index 0 now. This
3576                  * is phase 1 implementation that support FCF index 0 and driver
3577                  * defaults.
3578                  */
3579                 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
3580                         fcf_record = kzalloc(sizeof(struct fcf_record),
3581                                         GFP_KERNEL);
3582                         if (unlikely(!fcf_record)) {
3583                                 lpfc_printf_log(phba, KERN_ERR,
3584                                         LOG_TRACE_EVENT,
3585                                         "2554 Could not allocate memory for "
3586                                         "fcf record\n");
3587                                 rc = -ENODEV;
3588                                 goto out;
3589                         }
3590
3591                         lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3592                                                 LPFC_FCOE_FCF_DEF_INDEX);
3593                         rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3594                         if (unlikely(rc)) {
3595                                 lpfc_printf_log(phba, KERN_ERR,
3596                                         LOG_TRACE_EVENT,
3597                                         "2013 Could not manually add FCF "
3598                                         "record 0, status %d\n", rc);
3599                                 rc = -ENODEV;
3600                                 kfree(fcf_record);
3601                                 goto out;
3602                         }
3603                         kfree(fcf_record);
3604                 }
3605                 /*
3606                  * The driver is expected to do FIP/FCF. Call the port
3607                  * and get the FCF Table.
3608                  */
3609                 spin_lock_irqsave(&phba->hbalock, iflags);
3610                 if (phba->hba_flag & FCF_TS_INPROG) {
3611                         spin_unlock_irqrestore(&phba->hbalock, iflags);
3612                         return;
3613                 }
3614                 /* This is the initial FCF discovery scan */
3615                 phba->fcf.fcf_flag |= FCF_INIT_DISC;
3616                 spin_unlock_irqrestore(&phba->hbalock, iflags);
3617                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3618                                 "2778 Start FCF table scan at linkup\n");
3619                 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3620                                                      LPFC_FCOE_FCF_GET_FIRST);
3621                 if (rc) {
3622                         spin_lock_irqsave(&phba->hbalock, iflags);
3623                         phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3624                         spin_unlock_irqrestore(&phba->hbalock, iflags);
3625                         goto out;
3626                 }
3627                 /* Reset FCF roundrobin bmask for new discovery */
3628                 lpfc_sli4_clear_fcf_rr_bmask(phba);
3629         }
3630
3631         /* Prepare for LINK up registrations */
3632         memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
3633         scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
3634                   init_utsname()->nodename);
3635         return;
3636 out:
3637         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3638         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3639                          "0263 Discovery Mailbox error: state: 0x%x : x%px x%px\n",
3640                          vport->port_state, sparam_mbox, cfglink_mbox);
3641         lpfc_issue_clear_la(phba, vport);
3642         return;
3643 }
3644
3645 static void
3646 lpfc_enable_la(struct lpfc_hba *phba)
3647 {
3648         uint32_t control;
3649         struct lpfc_sli *psli = &phba->sli;
3650         spin_lock_irq(&phba->hbalock);
3651         psli->sli_flag |= LPFC_PROCESS_LA;
3652         if (phba->sli_rev <= LPFC_SLI_REV3) {
3653                 control = readl(phba->HCregaddr);
3654                 control |= HC_LAINT_ENA;
3655                 writel(control, phba->HCregaddr);
3656                 readl(phba->HCregaddr); /* flush */
3657         }
3658         spin_unlock_irq(&phba->hbalock);
3659 }
3660
3661 static void
3662 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3663 {
3664         lpfc_linkdown(phba);
3665         lpfc_enable_la(phba);
3666         lpfc_unregister_unused_fcf(phba);
3667         /* turn on Link Attention interrupts - no CLEAR_LA needed */
3668 }
3669
3670
3671 /*
3672  * This routine handles processing a READ_TOPOLOGY mailbox
3673  * command upon completion. It is setup in the LPFC_MBOXQ
3674  * as the completion routine when the command is
3675  * handed off to the SLI layer. SLI4 only.
3676  */
3677 void
3678 lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3679 {
3680         struct lpfc_vport *vport = pmb->vport;
3681         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3682         struct lpfc_mbx_read_top *la;
3683         struct lpfc_sli_ring *pring;
3684         MAILBOX_t *mb = &pmb->u.mb;
3685         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3686         uint8_t attn_type;
3687         unsigned long iflags;
3688
3689         /* Unblock ELS traffic */
3690         pring = lpfc_phba_elsring(phba);
3691         if (pring)
3692                 pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3693
3694         /* Check for error */
3695         if (mb->mbxStatus) {
3696                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3697                                 "1307 READ_LA mbox error x%x state x%x\n",
3698                                 mb->mbxStatus, vport->port_state);
3699                 lpfc_mbx_issue_link_down(phba);
3700                 phba->link_state = LPFC_HBA_ERROR;
3701                 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
3702         }
3703
3704         la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3705         attn_type = bf_get(lpfc_mbx_read_top_att_type, la);
3706
3707         memcpy(&phba->alpa_map[0], mp->virt, 128);
3708
3709         spin_lock_irqsave(shost->host_lock, iflags);
3710         if (bf_get(lpfc_mbx_read_top_pb, la))
3711                 vport->fc_flag |= FC_BYPASSED_MODE;
3712         else
3713                 vport->fc_flag &= ~FC_BYPASSED_MODE;
3714         spin_unlock_irqrestore(shost->host_lock, iflags);
3715
3716         if (phba->fc_eventTag <= la->eventTag) {
3717                 phba->fc_stat.LinkMultiEvent++;
3718                 if (attn_type == LPFC_ATT_LINK_UP)
3719                         if (phba->fc_eventTag != 0)
3720                                 lpfc_linkdown(phba);
3721         }
3722
3723         phba->fc_eventTag = la->eventTag;
3724         if (phba->sli_rev < LPFC_SLI_REV4) {
3725                 spin_lock_irqsave(&phba->hbalock, iflags);
3726                 if (bf_get(lpfc_mbx_read_top_mm, la))
3727                         phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3728                 else
3729                         phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3730                 spin_unlock_irqrestore(&phba->hbalock, iflags);
3731         }
3732
3733         phba->link_events++;
3734         if ((attn_type == LPFC_ATT_LINK_UP) &&
3735             !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
3736                 phba->fc_stat.LinkUp++;
3737                 if (phba->link_flag & LS_LOOPBACK_MODE) {
3738                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3739                                         "1306 Link Up Event in loop back mode "
3740                                         "x%x received Data: x%x x%x x%x x%x\n",
3741                                         la->eventTag, phba->fc_eventTag,
3742                                         bf_get(lpfc_mbx_read_top_alpa_granted,
3743                                                la),
3744                                         bf_get(lpfc_mbx_read_top_link_spd, la),
3745                                         phba->alpa_map[0]);
3746                 } else {
3747                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3748                                         "1303 Link Up Event x%x received "
3749                                         "Data: x%x x%x x%x x%x x%x x%x %d\n",
3750                                         la->eventTag, phba->fc_eventTag,
3751                                         bf_get(lpfc_mbx_read_top_alpa_granted,
3752                                                la),
3753                                         bf_get(lpfc_mbx_read_top_link_spd, la),
3754                                         phba->alpa_map[0],
3755                                         bf_get(lpfc_mbx_read_top_mm, la),
3756                                         bf_get(lpfc_mbx_read_top_fa, la),
3757                                         phba->wait_4_mlo_maint_flg);
3758                 }
3759                 lpfc_mbx_process_link_up(phba, la);
3760
3761                 if (phba->cmf_active_mode != LPFC_CFG_OFF)
3762                         lpfc_cmf_signal_init(phba);
3763
3764         } else if (attn_type == LPFC_ATT_LINK_DOWN ||
3765                    attn_type == LPFC_ATT_UNEXP_WWPN) {
3766                 phba->fc_stat.LinkDown++;
3767                 if (phba->link_flag & LS_LOOPBACK_MODE)
3768                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3769                                 "1308 Link Down Event in loop back mode "
3770                                 "x%x received "
3771                                 "Data: x%x x%x x%x\n",
3772                                 la->eventTag, phba->fc_eventTag,
3773                                 phba->pport->port_state, vport->fc_flag);
3774                 else if (attn_type == LPFC_ATT_UNEXP_WWPN)
3775                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3776                                 "1313 Link Down Unexpected FA WWPN Event x%x "
3777                                 "received Data: x%x x%x x%x x%x x%x\n",
3778                                 la->eventTag, phba->fc_eventTag,
3779                                 phba->pport->port_state, vport->fc_flag,
3780                                 bf_get(lpfc_mbx_read_top_mm, la),
3781                                 bf_get(lpfc_mbx_read_top_fa, la));
3782                 else
3783                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3784                                 "1305 Link Down Event x%x received "
3785                                 "Data: x%x x%x x%x x%x x%x\n",
3786                                 la->eventTag, phba->fc_eventTag,
3787                                 phba->pport->port_state, vport->fc_flag,
3788                                 bf_get(lpfc_mbx_read_top_mm, la),
3789                                 bf_get(lpfc_mbx_read_top_fa, la));
3790                 lpfc_mbx_issue_link_down(phba);
3791         }
3792         if (phba->sli.sli_flag & LPFC_MENLO_MAINT &&
3793             attn_type == LPFC_ATT_LINK_UP) {
3794                 if (phba->link_state != LPFC_LINK_DOWN) {
3795                         phba->fc_stat.LinkDown++;
3796                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3797                                 "1312 Link Down Event x%x received "
3798                                 "Data: x%x x%x x%x\n",
3799                                 la->eventTag, phba->fc_eventTag,
3800                                 phba->pport->port_state, vport->fc_flag);
3801                         lpfc_mbx_issue_link_down(phba);
3802                 } else
3803                         lpfc_enable_la(phba);
3804
3805                 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3806                                 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3807                                 "Data: x%x x%x x%x\n",
3808                                 la->eventTag, phba->fc_eventTag,
3809                                 phba->pport->port_state, vport->fc_flag);
3810                 /*
3811                  * The cmnd that triggered this will be waiting for this
3812                  * signal.
3813                  */
3814                 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3815                 if (phba->wait_4_mlo_maint_flg) {
3816                         phba->wait_4_mlo_maint_flg = 0;
3817                         wake_up_interruptible(&phba->wait_4_mlo_m_q);
3818                 }
3819         }
3820
3821         if ((phba->sli_rev < LPFC_SLI_REV4) &&
3822             bf_get(lpfc_mbx_read_top_fa, la)) {
3823                 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
3824                         lpfc_issue_clear_la(phba, vport);
3825                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3826                                 "1311 fa %d\n",
3827                                 bf_get(lpfc_mbx_read_top_fa, la));
3828         }
3829
3830 lpfc_mbx_cmpl_read_topology_free_mbuf:
3831         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3832         kfree(mp);
3833         mempool_free(pmb, phba->mbox_mem_pool);
3834         return;
3835 }
3836
3837 /*
3838  * This routine handles processing a REG_LOGIN mailbox
3839  * command upon completion. It is setup in the LPFC_MBOXQ
3840  * as the completion routine when the command is
3841  * handed off to the SLI layer.
3842  */
3843 void
3844 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3845 {
3846         struct lpfc_vport  *vport = pmb->vport;
3847         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3848         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3849
3850         pmb->ctx_buf = NULL;
3851         pmb->ctx_ndlp = NULL;
3852
3853         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NODE | LOG_DISCOVERY,
3854                          "0002 rpi:%x DID:%x flg:%x %d x%px\n",
3855                          ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3856                          kref_read(&ndlp->kref),
3857                          ndlp);
3858         if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3859                 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3860
3861         if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3862             ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
3863                 /* We rcvd a rscn after issuing this
3864                  * mbox reg login, we may have cycled
3865                  * back through the state and be
3866                  * back at reg login state so this
3867                  * mbox needs to be ignored becase
3868                  * there is another reg login in
3869                  * process.
3870                  */
3871                 spin_lock_irq(&ndlp->lock);
3872                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3873                 spin_unlock_irq(&ndlp->lock);
3874
3875                 /*
3876                  * We cannot leave the RPI registered because
3877                  * if we go thru discovery again for this ndlp
3878                  * a subsequent REG_RPI will fail.
3879                  */
3880                 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3881                 lpfc_unreg_rpi(vport, ndlp);
3882         }
3883
3884         /* Call state machine */
3885         lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
3886
3887         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3888         kfree(mp);
3889         mempool_free(pmb, phba->mbox_mem_pool);
3890         /* decrement the node reference count held for this callback
3891          * function.
3892          */
3893         lpfc_nlp_put(ndlp);
3894
3895         return;
3896 }
3897
3898 static void
3899 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3900 {
3901         MAILBOX_t *mb = &pmb->u.mb;
3902         struct lpfc_vport *vport = pmb->vport;
3903         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3904
3905         switch (mb->mbxStatus) {
3906         case 0x0011:
3907         case 0x0020:
3908                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3909                                  "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3910                                  mb->mbxStatus);
3911                 break;
3912         /* If VPI is busy, reset the HBA */
3913         case 0x9700:
3914                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3915                         "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3916                         vport->vpi, mb->mbxStatus);
3917                 if (!(phba->pport->load_flag & FC_UNLOADING))
3918                         lpfc_workq_post_event(phba, NULL, NULL,
3919                                 LPFC_EVT_RESET_HBA);
3920         }
3921         spin_lock_irq(shost->host_lock);
3922         vport->vpi_state &= ~LPFC_VPI_REGISTERED;
3923         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3924         spin_unlock_irq(shost->host_lock);
3925         vport->unreg_vpi_cmpl = VPORT_OK;
3926         mempool_free(pmb, phba->mbox_mem_pool);
3927         lpfc_cleanup_vports_rrqs(vport, NULL);
3928         /*
3929          * This shost reference might have been taken at the beginning of
3930          * lpfc_vport_delete()
3931          */
3932         if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
3933                 scsi_host_put(shost);
3934 }
3935
3936 int
3937 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3938 {
3939         struct lpfc_hba  *phba = vport->phba;
3940         LPFC_MBOXQ_t *mbox;
3941         int rc;
3942
3943         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3944         if (!mbox)
3945                 return 1;
3946
3947         lpfc_unreg_vpi(phba, vport->vpi, mbox);
3948         mbox->vport = vport;
3949         mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
3950         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3951         if (rc == MBX_NOT_FINISHED) {
3952                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3953                                  "1800 Could not issue unreg_vpi\n");
3954                 mempool_free(mbox, phba->mbox_mem_pool);
3955                 vport->unreg_vpi_cmpl = VPORT_ERROR;
3956                 return rc;
3957         }
3958         return 0;
3959 }
3960
3961 static void
3962 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3963 {
3964         struct lpfc_vport *vport = pmb->vport;
3965         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3966         MAILBOX_t *mb = &pmb->u.mb;
3967
3968         switch (mb->mbxStatus) {
3969         case 0x0011:
3970         case 0x9601:
3971         case 0x9602:
3972                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3973                                  "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3974                                  mb->mbxStatus);
3975                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3976                 spin_lock_irq(shost->host_lock);
3977                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3978                 spin_unlock_irq(shost->host_lock);
3979                 vport->fc_myDID = 0;
3980
3981                 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3982                     (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3983                         if (phba->nvmet_support)
3984                                 lpfc_nvmet_update_targetport(phba);
3985                         else
3986                                 lpfc_nvme_update_localport(vport);
3987                 }
3988                 goto out;
3989         }
3990
3991         spin_lock_irq(shost->host_lock);
3992         vport->vpi_state |= LPFC_VPI_REGISTERED;
3993         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
3994         spin_unlock_irq(shost->host_lock);
3995         vport->num_disc_nodes = 0;
3996         /* go thru NPR list and issue ELS PLOGIs */
3997         if (vport->fc_npr_cnt)
3998                 lpfc_els_disc_plogi(vport);
3999
4000         if (!vport->num_disc_nodes) {
4001                 spin_lock_irq(shost->host_lock);
4002                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4003                 spin_unlock_irq(shost->host_lock);
4004                 lpfc_can_disctmo(vport);
4005         }
4006         vport->port_state = LPFC_VPORT_READY;
4007
4008 out:
4009         mempool_free(pmb, phba->mbox_mem_pool);
4010         return;
4011 }
4012
4013 /**
4014  * lpfc_create_static_vport - Read HBA config region to create static vports.
4015  * @phba: pointer to lpfc hba data structure.
4016  *
4017  * This routine issue a DUMP mailbox command for config region 22 to get
4018  * the list of static vports to be created. The function create vports
4019  * based on the information returned from the HBA.
4020  **/
4021 void
4022 lpfc_create_static_vport(struct lpfc_hba *phba)
4023 {
4024         LPFC_MBOXQ_t *pmb = NULL;
4025         MAILBOX_t *mb;
4026         struct static_vport_info *vport_info;
4027         int mbx_wait_rc = 0, i;
4028         struct fc_vport_identifiers vport_id;
4029         struct fc_vport *new_fc_vport;
4030         struct Scsi_Host *shost;
4031         struct lpfc_vport *vport;
4032         uint16_t offset = 0;
4033         uint8_t *vport_buff;
4034         struct lpfc_dmabuf *mp;
4035         uint32_t byte_count = 0;
4036
4037         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4038         if (!pmb) {
4039                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4040                                 "0542 lpfc_create_static_vport failed to"
4041                                 " allocate mailbox memory\n");
4042                 return;
4043         }
4044         memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
4045         mb = &pmb->u.mb;
4046
4047         vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
4048         if (!vport_info) {
4049                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4050                                 "0543 lpfc_create_static_vport failed to"
4051                                 " allocate vport_info\n");
4052                 mempool_free(pmb, phba->mbox_mem_pool);
4053                 return;
4054         }
4055
4056         vport_buff = (uint8_t *) vport_info;
4057         do {
4058                 /* free dma buffer from previous round */
4059                 if (pmb->ctx_buf) {
4060                         mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
4061                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4062                         kfree(mp);
4063                 }
4064                 if (lpfc_dump_static_vport(phba, pmb, offset))
4065                         goto out;
4066
4067                 pmb->vport = phba->pport;
4068                 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
4069                                                         LPFC_MBOX_TMO);
4070
4071                 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
4072                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4073                                 "0544 lpfc_create_static_vport failed to"
4074                                 " issue dump mailbox command ret 0x%x "
4075                                 "status 0x%x\n",
4076                                 mbx_wait_rc, mb->mbxStatus);
4077                         goto out;
4078                 }
4079
4080                 if (phba->sli_rev == LPFC_SLI_REV4) {
4081                         byte_count = pmb->u.mqe.un.mb_words[5];
4082                         mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
4083                         if (byte_count > sizeof(struct static_vport_info) -
4084                                         offset)
4085                                 byte_count = sizeof(struct static_vport_info)
4086                                         - offset;
4087                         memcpy(vport_buff + offset, mp->virt, byte_count);
4088                         offset += byte_count;
4089                 } else {
4090                         if (mb->un.varDmp.word_cnt >
4091                                 sizeof(struct static_vport_info) - offset)
4092                                 mb->un.varDmp.word_cnt =
4093                                         sizeof(struct static_vport_info)
4094                                                 - offset;
4095                         byte_count = mb->un.varDmp.word_cnt;
4096                         lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
4097                                 vport_buff + offset,
4098                                 byte_count);
4099
4100                         offset += byte_count;
4101                 }
4102
4103         } while (byte_count &&
4104                 offset < sizeof(struct static_vport_info));
4105
4106
4107         if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
4108                 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
4109                         != VPORT_INFO_REV)) {
4110                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4111                                 "0545 lpfc_create_static_vport bad"
4112                                 " information header 0x%x 0x%x\n",
4113                                 le32_to_cpu(vport_info->signature),
4114                                 le32_to_cpu(vport_info->rev) &
4115                                 VPORT_INFO_REV_MASK);
4116
4117                 goto out;
4118         }
4119
4120         shost = lpfc_shost_from_vport(phba->pport);
4121
4122         for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
4123                 memset(&vport_id, 0, sizeof(vport_id));
4124                 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
4125                 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
4126                 if (!vport_id.port_name || !vport_id.node_name)
4127                         continue;
4128
4129                 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
4130                 vport_id.vport_type = FC_PORTTYPE_NPIV;
4131                 vport_id.disable = false;
4132                 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
4133
4134                 if (!new_fc_vport) {
4135                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4136                                 "0546 lpfc_create_static_vport failed to"
4137                                 " create vport\n");
4138                         continue;
4139                 }
4140
4141                 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
4142                 vport->vport_flag |= STATIC_VPORT;
4143         }
4144
4145 out:
4146         kfree(vport_info);
4147         if (mbx_wait_rc != MBX_TIMEOUT) {
4148                 if (pmb->ctx_buf) {
4149                         mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
4150                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4151                         kfree(mp);
4152                 }
4153                 mempool_free(pmb, phba->mbox_mem_pool);
4154         }
4155
4156         return;
4157 }
4158
4159 /*
4160  * This routine handles processing a Fabric REG_LOGIN mailbox
4161  * command upon completion. It is setup in the LPFC_MBOXQ
4162  * as the completion routine when the command is
4163  * handed off to the SLI layer.
4164  */
4165 void
4166 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4167 {
4168         struct lpfc_vport *vport = pmb->vport;
4169         MAILBOX_t *mb = &pmb->u.mb;
4170         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
4171         struct lpfc_nodelist *ndlp;
4172         struct Scsi_Host *shost;
4173
4174         ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4175         pmb->ctx_ndlp = NULL;
4176         pmb->ctx_buf = NULL;
4177
4178         if (mb->mbxStatus) {
4179                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4180                                  "0258 Register Fabric login error: 0x%x\n",
4181                                  mb->mbxStatus);
4182                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4183                 kfree(mp);
4184                 mempool_free(pmb, phba->mbox_mem_pool);
4185
4186                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4187                         /* FLOGI failed, use loop map to make discovery list */
4188                         lpfc_disc_list_loopmap(vport);
4189
4190                         /* Start discovery */
4191                         lpfc_disc_start(vport);
4192                         /* Decrement the reference count to ndlp after the
4193                          * reference to the ndlp are done.
4194                          */
4195                         lpfc_nlp_put(ndlp);
4196                         return;
4197                 }
4198
4199                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4200                 /* Decrement the reference count to ndlp after the reference
4201                  * to the ndlp are done.
4202                  */
4203                 lpfc_nlp_put(ndlp);
4204                 return;
4205         }
4206
4207         if (phba->sli_rev < LPFC_SLI_REV4)
4208                 ndlp->nlp_rpi = mb->un.varWords[0];
4209         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4210         ndlp->nlp_type |= NLP_FABRIC;
4211         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4212
4213         if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
4214                 /* when physical port receive logo donot start
4215                  * vport discovery */
4216                 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
4217                         lpfc_start_fdiscs(phba);
4218                 else {
4219                         shost = lpfc_shost_from_vport(vport);
4220                         spin_lock_irq(shost->host_lock);
4221                         vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
4222                         spin_unlock_irq(shost->host_lock);
4223                 }
4224                 lpfc_do_scr_ns_plogi(phba, vport);
4225         }
4226
4227         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4228         kfree(mp);
4229         mempool_free(pmb, phba->mbox_mem_pool);
4230
4231         /* Drop the reference count from the mbox at the end after
4232          * all the current reference to the ndlp have been done.
4233          */
4234         lpfc_nlp_put(ndlp);
4235         return;
4236 }
4237
4238  /*
4239   * This routine will issue a GID_FT for each FC4 Type supported
4240   * by the driver. ALL GID_FTs must complete before discovery is started.
4241   */
4242 int
4243 lpfc_issue_gidft(struct lpfc_vport *vport)
4244 {
4245         /* Good status, issue CT Request to NameServer */
4246         if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4247             (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
4248                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
4249                         /* Cannot issue NameServer FCP Query, so finish up
4250                          * discovery
4251                          */
4252                         lpfc_printf_vlog(vport, KERN_ERR,
4253                                          LOG_TRACE_EVENT,
4254                                          "0604 %s FC TYPE %x %s\n",
4255                                          "Failed to issue GID_FT to ",
4256                                          FC_TYPE_FCP,
4257                                          "Finishing discovery.");
4258                         return 0;
4259                 }
4260                 vport->gidft_inp++;
4261         }
4262
4263         if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4264             (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
4265                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
4266                         /* Cannot issue NameServer NVME Query, so finish up
4267                          * discovery
4268                          */
4269                         lpfc_printf_vlog(vport, KERN_ERR,
4270                                          LOG_TRACE_EVENT,
4271                                          "0605 %s FC_TYPE %x %s %d\n",
4272                                          "Failed to issue GID_FT to ",
4273                                          FC_TYPE_NVME,
4274                                          "Finishing discovery: gidftinp ",
4275                                          vport->gidft_inp);
4276                         if (vport->gidft_inp == 0)
4277                                 return 0;
4278                 } else
4279                         vport->gidft_inp++;
4280         }
4281         return vport->gidft_inp;
4282 }
4283
4284 /**
4285  * lpfc_issue_gidpt - issue a GID_PT for all N_Ports
4286  * @vport: The virtual port for which this call is being executed.
4287  *
4288  * This routine will issue a GID_PT to get a list of all N_Ports
4289  *
4290  * Return value :
4291  *   0 - Failure to issue a GID_PT
4292  *   1 - GID_PT issued
4293  **/
4294 int
4295 lpfc_issue_gidpt(struct lpfc_vport *vport)
4296 {
4297         /* Good status, issue CT Request to NameServer */
4298         if (lpfc_ns_cmd(vport, SLI_CTNS_GID_PT, 0, GID_PT_N_PORT)) {
4299                 /* Cannot issue NameServer FCP Query, so finish up
4300                  * discovery
4301                  */
4302                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4303                                  "0606 %s Port TYPE %x %s\n",
4304                                  "Failed to issue GID_PT to ",
4305                                  GID_PT_N_PORT,
4306                                  "Finishing discovery.");
4307                 return 0;
4308         }
4309         vport->gidft_inp++;
4310         return 1;
4311 }
4312
4313 /*
4314  * This routine handles processing a NameServer REG_LOGIN mailbox
4315  * command upon completion. It is setup in the LPFC_MBOXQ
4316  * as the completion routine when the command is
4317  * handed off to the SLI layer.
4318  */
4319 void
4320 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4321 {
4322         MAILBOX_t *mb = &pmb->u.mb;
4323         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
4324         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4325         struct lpfc_vport *vport = pmb->vport;
4326         int rc;
4327
4328         pmb->ctx_buf = NULL;
4329         pmb->ctx_ndlp = NULL;
4330         vport->gidft_inp = 0;
4331
4332         if (mb->mbxStatus) {
4333                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4334                                  "0260 Register NameServer error: 0x%x\n",
4335                                  mb->mbxStatus);
4336
4337 out:
4338                 /* decrement the node reference count held for this
4339                  * callback function.
4340                  */
4341                 lpfc_nlp_put(ndlp);
4342                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4343                 kfree(mp);
4344                 mempool_free(pmb, phba->mbox_mem_pool);
4345
4346                 /* If the node is not registered with the scsi or nvme
4347                  * transport, remove the fabric node.  The failed reg_login
4348                  * is terminal.
4349                  */
4350                 if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
4351                         spin_lock_irq(&ndlp->lock);
4352                         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
4353                         spin_unlock_irq(&ndlp->lock);
4354                         lpfc_nlp_not_used(ndlp);
4355                 }
4356
4357                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4358                         /*
4359                          * RegLogin failed, use loop map to make discovery
4360                          * list
4361                          */
4362                         lpfc_disc_list_loopmap(vport);
4363
4364                         /* Start discovery */
4365                         lpfc_disc_start(vport);
4366                         return;
4367                 }
4368                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4369                 return;
4370         }
4371
4372         if (phba->sli_rev < LPFC_SLI_REV4)
4373                 ndlp->nlp_rpi = mb->un.varWords[0];
4374         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4375         ndlp->nlp_type |= NLP_FABRIC;
4376         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4377         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
4378                          "0003 rpi:%x DID:%x flg:%x %d x%px\n",
4379                          ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4380                          kref_read(&ndlp->kref),
4381                          ndlp);
4382
4383         if (vport->port_state < LPFC_VPORT_READY) {
4384                 /* Link up discovery requires Fabric registration. */
4385                 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
4386                 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
4387                 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
4388                 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
4389
4390                 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4391                     (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
4392                         lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
4393
4394                 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4395                     (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
4396                         lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
4397                                     FC_TYPE_NVME);
4398
4399                 /* Issue SCR just before NameServer GID_FT Query */
4400                 lpfc_issue_els_scr(vport, 0);
4401
4402                 /* Link was bounced or a Fabric LOGO occurred.  Start EDC
4403                  * with initial FW values provided the congestion mode is
4404                  * not off.  Note that signals may or may not be supported
4405                  * by the adapter but FPIN is provided by default for 1
4406                  * or both missing signals support.
4407                  */
4408                 if (phba->cmf_active_mode != LPFC_CFG_OFF) {
4409                         phba->cgn_reg_fpin = phba->cgn_init_reg_fpin;
4410                         phba->cgn_reg_signal = phba->cgn_init_reg_signal;
4411                         rc = lpfc_issue_els_edc(vport, 0);
4412                         lpfc_printf_log(phba, KERN_INFO,
4413                                         LOG_INIT | LOG_ELS | LOG_DISCOVERY,
4414                                         "4220 EDC issue error x%x, Data: x%x\n",
4415                                         rc, phba->cgn_init_reg_signal);
4416                 } else {
4417                         lpfc_issue_els_rdf(vport, 0);
4418                 }
4419         }
4420
4421         vport->fc_ns_retry = 0;
4422         if (lpfc_issue_gidft(vport) == 0)
4423                 goto out;
4424
4425         /*
4426          * At this point in time we may need to wait for multiple
4427          * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
4428          *
4429          * decrement the node reference count held for this
4430          * callback function.
4431          */
4432         lpfc_nlp_put(ndlp);
4433         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4434         kfree(mp);
4435         mempool_free(pmb, phba->mbox_mem_pool);
4436
4437         return;
4438 }
4439
4440 /*
4441  * This routine handles processing a Fabric Controller REG_LOGIN mailbox
4442  * command upon completion. It is setup in the LPFC_MBOXQ
4443  * as the completion routine when the command is handed off to the SLI layer.
4444  */
4445 void
4446 lpfc_mbx_cmpl_fc_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4447 {
4448         struct lpfc_vport *vport = pmb->vport;
4449         MAILBOX_t *mb = &pmb->u.mb;
4450         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
4451         struct lpfc_nodelist *ndlp;
4452
4453         ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4454         pmb->ctx_ndlp = NULL;
4455         pmb->ctx_buf = NULL;
4456
4457         if (mb->mbxStatus) {
4458                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4459                                  "0933 %s: Register FC login error: 0x%x\n",
4460                                  __func__, mb->mbxStatus);
4461                 goto out;
4462         }
4463
4464         lpfc_check_nlp_post_devloss(vport, ndlp);
4465
4466         if (phba->sli_rev < LPFC_SLI_REV4)
4467                 ndlp->nlp_rpi = mb->un.varWords[0];
4468
4469         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4470                          "0934 %s: Complete FC x%x RegLogin rpi x%x ste x%x\n",
4471                          __func__, ndlp->nlp_DID, ndlp->nlp_rpi,
4472                          ndlp->nlp_state);
4473
4474         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4475         ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
4476         ndlp->nlp_type |= NLP_FABRIC;
4477         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4478
4479  out:
4480         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4481         kfree(mp);
4482         mempool_free(pmb, phba->mbox_mem_pool);
4483
4484         /* Drop the reference count from the mbox at the end after
4485          * all the current reference to the ndlp have been done.
4486          */
4487         lpfc_nlp_put(ndlp);
4488 }
4489
4490 static void
4491 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4492 {
4493         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4494         struct fc_rport  *rport;
4495         struct lpfc_rport_data *rdata;
4496         struct fc_rport_identifiers rport_ids;
4497         struct lpfc_hba  *phba = vport->phba;
4498         unsigned long flags;
4499
4500         if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4501                 return;
4502
4503         /* Remote port has reappeared. Re-register w/ FC transport */
4504         rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4505         rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
4506         rport_ids.port_id = ndlp->nlp_DID;
4507         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
4508
4509
4510         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4511                               "rport add:       did:x%x flg:x%x type x%x",
4512                               ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4513
4514         /* Don't add the remote port if unloading. */
4515         if (vport->load_flag & FC_UNLOADING)
4516                 return;
4517
4518         /*
4519          * Disassociate any older association between this ndlp and rport
4520          */
4521         if (ndlp->rport) {
4522                 rdata = ndlp->rport->dd_data;
4523                 rdata->pnode = NULL;
4524         }
4525
4526         ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
4527         if (!rport) {
4528                 dev_printk(KERN_WARNING, &phba->pcidev->dev,
4529                            "Warning: fc_remote_port_add failed\n");
4530                 return;
4531         }
4532
4533         /* Successful port add.  Complete initializing node data */
4534         rport->maxframe_size = ndlp->nlp_maxframe;
4535         rport->supported_classes = ndlp->nlp_class_sup;
4536         rdata = rport->dd_data;
4537         rdata->pnode = lpfc_nlp_get(ndlp);
4538         if (!rdata->pnode) {
4539                 dev_warn(&phba->pcidev->dev,
4540                          "Warning - node ref failed. Unreg rport\n");
4541                 fc_remote_port_delete(rport);
4542                 ndlp->rport = NULL;
4543                 return;
4544         }
4545
4546         spin_lock_irqsave(&ndlp->lock, flags);
4547         ndlp->fc4_xpt_flags |= SCSI_XPT_REGD;
4548         spin_unlock_irqrestore(&ndlp->lock, flags);
4549
4550         if (ndlp->nlp_type & NLP_FCP_TARGET)
4551                 rport_ids.roles |= FC_PORT_ROLE_FCP_TARGET;
4552         if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4553                 rport_ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
4554         if (ndlp->nlp_type & NLP_NVME_INITIATOR)
4555                 rport_ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
4556         if (ndlp->nlp_type & NLP_NVME_TARGET)
4557                 rport_ids.roles |= FC_PORT_ROLE_NVME_TARGET;
4558         if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
4559                 rport_ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
4560
4561         if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
4562                 fc_remote_port_rolechg(rport, rport_ids.roles);
4563
4564         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4565                          "3183 %s rport x%px DID x%x, role x%x refcnt %d\n",
4566                          __func__, rport, rport->port_id, rport->roles,
4567                          kref_read(&ndlp->kref));
4568
4569         if ((rport->scsi_target_id != -1) &&
4570             (rport->scsi_target_id < LPFC_MAX_TARGET)) {
4571                 ndlp->nlp_sid = rport->scsi_target_id;
4572         }
4573
4574         return;
4575 }
4576
4577 static void
4578 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
4579 {
4580         struct fc_rport *rport = ndlp->rport;
4581         struct lpfc_vport *vport = ndlp->vport;
4582
4583         if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4584                 return;
4585
4586         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4587                 "rport delete:    did:x%x flg:x%x type x%x",
4588                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4589
4590         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4591                          "3184 rport unregister x%06x, rport x%px "
4592                          "xptflg x%x refcnt %d\n",
4593                          ndlp->nlp_DID, rport, ndlp->fc4_xpt_flags,
4594                          kref_read(&ndlp->kref));
4595
4596         fc_remote_port_delete(rport);
4597         lpfc_nlp_put(ndlp);
4598 }
4599
4600 static void
4601 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
4602 {
4603         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4604         unsigned long iflags;
4605
4606         spin_lock_irqsave(shost->host_lock, iflags);
4607         switch (state) {
4608         case NLP_STE_UNUSED_NODE:
4609                 vport->fc_unused_cnt += count;
4610                 break;
4611         case NLP_STE_PLOGI_ISSUE:
4612                 vport->fc_plogi_cnt += count;
4613                 break;
4614         case NLP_STE_ADISC_ISSUE:
4615                 vport->fc_adisc_cnt += count;
4616                 break;
4617         case NLP_STE_REG_LOGIN_ISSUE:
4618                 vport->fc_reglogin_cnt += count;
4619                 break;
4620         case NLP_STE_PRLI_ISSUE:
4621                 vport->fc_prli_cnt += count;
4622                 break;
4623         case NLP_STE_UNMAPPED_NODE:
4624                 vport->fc_unmap_cnt += count;
4625                 break;
4626         case NLP_STE_MAPPED_NODE:
4627                 vport->fc_map_cnt += count;
4628                 break;
4629         case NLP_STE_NPR_NODE:
4630                 if (vport->fc_npr_cnt == 0 && count == -1)
4631                         vport->fc_npr_cnt = 0;
4632                 else
4633                         vport->fc_npr_cnt += count;
4634                 break;
4635         }
4636         spin_unlock_irqrestore(shost->host_lock, iflags);
4637 }
4638
4639 /* Register a node with backend if not already done */
4640 void
4641 lpfc_nlp_reg_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4642 {
4643         unsigned long iflags;
4644
4645         lpfc_check_nlp_post_devloss(vport, ndlp);
4646
4647         spin_lock_irqsave(&ndlp->lock, iflags);
4648         if (ndlp->fc4_xpt_flags & NLP_XPT_REGD) {
4649                 /* Already registered with backend, trigger rescan */
4650                 spin_unlock_irqrestore(&ndlp->lock, iflags);
4651
4652                 if (ndlp->fc4_xpt_flags & NVME_XPT_REGD &&
4653                     ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY)) {
4654                         lpfc_nvme_rescan_port(vport, ndlp);
4655                 }
4656                 return;
4657         }
4658
4659         ndlp->fc4_xpt_flags |= NLP_XPT_REGD;
4660         spin_unlock_irqrestore(&ndlp->lock, iflags);
4661
4662         if (lpfc_valid_xpt_node(ndlp)) {
4663                 vport->phba->nport_event_cnt++;
4664                 /*
4665                  * Tell the fc transport about the port, if we haven't
4666                  * already. If we have, and it's a scsi entity, be
4667                  */
4668                 lpfc_register_remote_port(vport, ndlp);
4669         }
4670
4671         /* We are done if we do not have any NVME remote node */
4672         if (!(ndlp->nlp_fc4_type & NLP_FC4_NVME))
4673                 return;
4674
4675         /* Notify the NVME transport of this new rport. */
4676         if (vport->phba->sli_rev >= LPFC_SLI_REV4 &&
4677                         ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4678                 if (vport->phba->nvmet_support == 0) {
4679                         /* Register this rport with the transport.
4680                          * Only NVME Target Rports are registered with
4681                          * the transport.
4682                          */
4683                         if (ndlp->nlp_type & NLP_NVME_TARGET) {
4684                                 vport->phba->nport_event_cnt++;
4685                                 lpfc_nvme_register_port(vport, ndlp);
4686                         }
4687                 } else {
4688                         /* Just take an NDLP ref count since the
4689                          * target does not register rports.
4690                          */
4691                         lpfc_nlp_get(ndlp);
4692                 }
4693         }
4694 }
4695
4696 /* Unregister a node with backend if not already done */
4697 void
4698 lpfc_nlp_unreg_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4699 {
4700         unsigned long iflags;
4701
4702         spin_lock_irqsave(&ndlp->lock, iflags);
4703         if (!(ndlp->fc4_xpt_flags & NLP_XPT_REGD)) {
4704                 spin_unlock_irqrestore(&ndlp->lock, iflags);
4705                 return;
4706         }
4707
4708         ndlp->fc4_xpt_flags &= ~NLP_XPT_REGD;
4709         spin_unlock_irqrestore(&ndlp->lock, iflags);
4710
4711         if (ndlp->rport &&
4712             ndlp->fc4_xpt_flags & SCSI_XPT_REGD) {
4713                 vport->phba->nport_event_cnt++;
4714                 lpfc_unregister_remote_port(ndlp);
4715         }
4716
4717         if (ndlp->fc4_xpt_flags & NVME_XPT_REGD) {
4718                 vport->phba->nport_event_cnt++;
4719                 if (vport->phba->nvmet_support == 0) {
4720                         /* Start devloss if target. */
4721                         if (ndlp->nlp_type & NLP_NVME_TARGET)
4722                                 lpfc_nvme_unregister_port(vport, ndlp);
4723                 } else {
4724                         /* NVMET has no upcall. */
4725                         lpfc_nlp_put(ndlp);
4726                 }
4727         }
4728
4729 }
4730
4731 /*
4732  * Adisc state change handling
4733  */
4734 static void
4735 lpfc_handle_adisc_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4736                 int new_state)
4737 {
4738         switch (new_state) {
4739         /*
4740          * Any state to ADISC_ISSUE
4741          * Do nothing, adisc cmpl handling will trigger state changes
4742          */
4743         case NLP_STE_ADISC_ISSUE:
4744                 break;
4745
4746         /*
4747          * ADISC_ISSUE to mapped states
4748          * Trigger a registration with backend, it will be nop if
4749          * already registered
4750          */
4751         case NLP_STE_UNMAPPED_NODE:
4752                 ndlp->nlp_type |= NLP_FC_NODE;
4753                 fallthrough;
4754         case NLP_STE_MAPPED_NODE:
4755                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4756                 lpfc_nlp_reg_node(vport, ndlp);
4757                 break;
4758
4759         /*
4760          * ADISC_ISSUE to non-mapped states
4761          * We are moving from ADISC_ISSUE to a non-mapped state because
4762          * ADISC failed, we would have skipped unregistering with
4763          * backend, attempt it now
4764          */
4765         case NLP_STE_NPR_NODE:
4766                 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
4767                 fallthrough;
4768         default:
4769                 lpfc_nlp_unreg_node(vport, ndlp);
4770                 break;
4771         }
4772
4773 }
4774
4775 static void
4776 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4777                        int old_state, int new_state)
4778 {
4779         /* Trap ADISC changes here */
4780         if (new_state == NLP_STE_ADISC_ISSUE ||
4781             old_state == NLP_STE_ADISC_ISSUE) {
4782                 lpfc_handle_adisc_state(vport, ndlp, new_state);
4783                 return;
4784         }
4785
4786         if (new_state == NLP_STE_UNMAPPED_NODE) {
4787                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4788                 ndlp->nlp_type |= NLP_FC_NODE;
4789         }
4790         if (new_state == NLP_STE_MAPPED_NODE)
4791                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4792         if (new_state == NLP_STE_NPR_NODE)
4793                 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
4794
4795         /* Reg/Unreg for FCP and NVME Transport interface */
4796         if ((old_state == NLP_STE_MAPPED_NODE ||
4797              old_state == NLP_STE_UNMAPPED_NODE)) {
4798                 /* For nodes marked for ADISC, Handle unreg in ADISC cmpl */
4799                 if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
4800                         lpfc_nlp_unreg_node(vport, ndlp);
4801         }
4802
4803         if (new_state ==  NLP_STE_MAPPED_NODE ||
4804             new_state == NLP_STE_UNMAPPED_NODE)
4805                 lpfc_nlp_reg_node(vport, ndlp);
4806
4807         if ((new_state ==  NLP_STE_MAPPED_NODE) &&
4808                 (vport->stat_data_enabled)) {
4809                 /*
4810                  * A new target is discovered, if there is no buffer for
4811                  * statistical data collection allocate buffer.
4812                  */
4813                 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4814                                          sizeof(struct lpfc_scsicmd_bkt),
4815                                          GFP_KERNEL);
4816
4817                 if (!ndlp->lat_data)
4818                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4819                                 "0286 lpfc_nlp_state_cleanup failed to "
4820                                 "allocate statistical data buffer DID "
4821                                 "0x%x\n", ndlp->nlp_DID);
4822         }
4823         /*
4824          * If the node just added to Mapped list was an FCP target,
4825          * but the remote port registration failed or assigned a target
4826          * id outside the presentable range - move the node to the
4827          * Unmapped List.
4828          */
4829         if ((new_state == NLP_STE_MAPPED_NODE) &&
4830             (ndlp->nlp_type & NLP_FCP_TARGET) &&
4831             (!ndlp->rport ||
4832              ndlp->rport->scsi_target_id == -1 ||
4833              ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
4834                 spin_lock_irq(&ndlp->lock);
4835                 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
4836                 spin_unlock_irq(&ndlp->lock);
4837                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4838         }
4839 }
4840
4841 static char *
4842 lpfc_nlp_state_name(char *buffer, size_t size, int state)
4843 {
4844         static char *states[] = {
4845                 [NLP_STE_UNUSED_NODE] = "UNUSED",
4846                 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4847                 [NLP_STE_ADISC_ISSUE] = "ADISC",
4848                 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4849                 [NLP_STE_PRLI_ISSUE] = "PRLI",
4850                 [NLP_STE_LOGO_ISSUE] = "LOGO",
4851                 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4852                 [NLP_STE_MAPPED_NODE] = "MAPPED",
4853                 [NLP_STE_NPR_NODE] = "NPR",
4854         };
4855
4856         if (state < NLP_STE_MAX_STATE && states[state])
4857                 strlcpy(buffer, states[state], size);
4858         else
4859                 snprintf(buffer, size, "unknown (%d)", state);
4860         return buffer;
4861 }
4862
4863 void
4864 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4865                    int state)
4866 {
4867         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4868         int  old_state = ndlp->nlp_state;
4869         int node_dropped = ndlp->nlp_flag & NLP_DROPPED;
4870         char name1[16], name2[16];
4871
4872         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4873                          "0904 NPort state transition x%06x, %s -> %s\n",
4874                          ndlp->nlp_DID,
4875                          lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4876                          lpfc_nlp_state_name(name2, sizeof(name2), state));
4877
4878         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4879                 "node statechg    did:x%x old:%d ste:%d",
4880                 ndlp->nlp_DID, old_state, state);
4881
4882         if (node_dropped && old_state == NLP_STE_UNUSED_NODE &&
4883             state != NLP_STE_UNUSED_NODE) {
4884                 ndlp->nlp_flag &= ~NLP_DROPPED;
4885                 lpfc_nlp_get(ndlp);
4886         }
4887
4888         if (old_state == NLP_STE_NPR_NODE &&
4889             state != NLP_STE_NPR_NODE)
4890                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
4891         if (old_state == NLP_STE_UNMAPPED_NODE) {
4892                 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4893                 ndlp->nlp_type &= ~NLP_FC_NODE;
4894         }
4895
4896         if (list_empty(&ndlp->nlp_listp)) {
4897                 spin_lock_irq(shost->host_lock);
4898                 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4899                 spin_unlock_irq(shost->host_lock);
4900         } else if (old_state)
4901                 lpfc_nlp_counters(vport, old_state, -1);
4902
4903         ndlp->nlp_state = state;
4904         lpfc_nlp_counters(vport, state, 1);
4905         lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
4906 }
4907
4908 void
4909 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4910 {
4911         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4912
4913         if (list_empty(&ndlp->nlp_listp)) {
4914                 spin_lock_irq(shost->host_lock);
4915                 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4916                 spin_unlock_irq(shost->host_lock);
4917         }
4918 }
4919
4920 void
4921 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4922 {
4923         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4924
4925         lpfc_cancel_retry_delay_tmo(vport, ndlp);
4926         if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4927                 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4928         spin_lock_irq(shost->host_lock);
4929         list_del_init(&ndlp->nlp_listp);
4930         spin_unlock_irq(shost->host_lock);
4931         lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4932                                 NLP_STE_UNUSED_NODE);
4933 }
4934
4935 /**
4936  * lpfc_initialize_node - Initialize all fields of node object
4937  * @vport: Pointer to Virtual Port object.
4938  * @ndlp: Pointer to FC node object.
4939  * @did: FC_ID of the node.
4940  *
4941  * This function is always called when node object need to be initialized.
4942  * It initializes all the fields of the node object. Although the reference
4943  * to phba from @ndlp can be obtained indirectly through it's reference to
4944  * @vport, a direct reference to phba is taken here by @ndlp. This is due
4945  * to the life-span of the @ndlp might go beyond the existence of @vport as
4946  * the final release of ndlp is determined by its reference count. And, the
4947  * operation on @ndlp needs the reference to phba.
4948  **/
4949 static inline void
4950 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4951         uint32_t did)
4952 {
4953         INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4954         INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4955         timer_setup(&ndlp->nlp_delayfunc, lpfc_els_retry_delay, 0);
4956         INIT_LIST_HEAD(&ndlp->recovery_evt.evt_listp);
4957
4958         ndlp->nlp_DID = did;
4959         ndlp->vport = vport;
4960         ndlp->phba = vport->phba;
4961         ndlp->nlp_sid = NLP_NO_SID;
4962         ndlp->nlp_fc4_type = NLP_FC4_NONE;
4963         kref_init(&ndlp->kref);
4964         atomic_set(&ndlp->cmd_pending, 0);
4965         ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
4966         ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4967 }
4968
4969 void
4970 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4971 {
4972         /*
4973          * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
4974          * be used if we wish to issue the "last" lpfc_nlp_put() to remove
4975          * the ndlp from the vport. The ndlp marked as UNUSED on the list
4976          * until ALL other outstanding threads have completed. We check
4977          * that the ndlp not already in the UNUSED state before we proceed.
4978          */
4979         if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4980                 return;
4981         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
4982         ndlp->nlp_flag |= NLP_DROPPED;
4983         if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4984                 lpfc_cleanup_vports_rrqs(vport, ndlp);
4985                 lpfc_unreg_rpi(vport, ndlp);
4986         }
4987
4988         lpfc_nlp_put(ndlp);
4989         return;
4990 }
4991
4992 /*
4993  * Start / ReStart rescue timer for Discovery / RSCN handling
4994  */
4995 void
4996 lpfc_set_disctmo(struct lpfc_vport *vport)
4997 {
4998         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4999         struct lpfc_hba  *phba = vport->phba;
5000         uint32_t tmo;
5001
5002         if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
5003                 /* For FAN, timeout should be greater than edtov */
5004                 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
5005         } else {
5006                 /* Normal discovery timeout should be > than ELS/CT timeout
5007                  * FC spec states we need 3 * ratov for CT requests
5008                  */
5009                 tmo = ((phba->fc_ratov * 3) + 3);
5010         }
5011
5012
5013         if (!timer_pending(&vport->fc_disctmo)) {
5014                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5015                         "set disc timer:  tmo:x%x state:x%x flg:x%x",
5016                         tmo, vport->port_state, vport->fc_flag);
5017         }
5018
5019         mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
5020         spin_lock_irq(shost->host_lock);
5021         vport->fc_flag |= FC_DISC_TMO;
5022         spin_unlock_irq(shost->host_lock);
5023
5024         /* Start Discovery Timer state <hba_state> */
5025         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5026                          "0247 Start Discovery Timer state x%x "
5027                          "Data: x%x x%lx x%x x%x\n",
5028                          vport->port_state, tmo,
5029                          (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
5030                          vport->fc_adisc_cnt);
5031
5032         return;
5033 }
5034
5035 /*
5036  * Cancel rescue timer for Discovery / RSCN handling
5037  */
5038 int
5039 lpfc_can_disctmo(struct lpfc_vport *vport)
5040 {
5041         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5042         unsigned long iflags;
5043
5044         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5045                 "can disc timer:  state:x%x rtry:x%x flg:x%x",
5046                 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5047
5048         /* Turn off discovery timer if its running */
5049         if (vport->fc_flag & FC_DISC_TMO) {
5050                 spin_lock_irqsave(shost->host_lock, iflags);
5051                 vport->fc_flag &= ~FC_DISC_TMO;
5052                 spin_unlock_irqrestore(shost->host_lock, iflags);
5053                 del_timer_sync(&vport->fc_disctmo);
5054                 spin_lock_irqsave(&vport->work_port_lock, iflags);
5055                 vport->work_port_events &= ~WORKER_DISC_TMO;
5056                 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
5057         }
5058
5059         /* Cancel Discovery Timer state <hba_state> */
5060         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5061                          "0248 Cancel Discovery Timer state x%x "
5062                          "Data: x%x x%x x%x\n",
5063                          vport->port_state, vport->fc_flag,
5064                          vport->fc_plogi_cnt, vport->fc_adisc_cnt);
5065         return 0;
5066 }
5067
5068 /*
5069  * Check specified ring for outstanding IOCB on the SLI queue
5070  * Return true if iocb matches the specified nport
5071  */
5072 int
5073 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
5074                     struct lpfc_sli_ring *pring,
5075                     struct lpfc_iocbq *iocb,
5076                     struct lpfc_nodelist *ndlp)
5077 {
5078         IOCB_t *icmd = &iocb->iocb;
5079         struct lpfc_vport    *vport = ndlp->vport;
5080
5081         if (iocb->vport != vport)
5082                 return 0;
5083
5084         if (pring->ringno == LPFC_ELS_RING) {
5085                 switch (icmd->ulpCommand) {
5086                 case CMD_GEN_REQUEST64_CR:
5087                         if (iocb->context_un.ndlp == ndlp)
5088                                 return 1;
5089                         fallthrough;
5090                 case CMD_ELS_REQUEST64_CR:
5091                         if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
5092                                 return 1;
5093                         fallthrough;
5094                 case CMD_XMIT_ELS_RSP64_CX:
5095                         if (iocb->context1 == (uint8_t *) ndlp)
5096                                 return 1;
5097                 }
5098         } else if (pring->ringno == LPFC_FCP_RING) {
5099                 /* Skip match check if waiting to relogin to FCP target */
5100                 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
5101                     (ndlp->nlp_flag & NLP_DELAY_TMO)) {
5102                         return 0;
5103                 }
5104                 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
5105                         return 1;
5106                 }
5107         }
5108         return 0;
5109 }
5110
5111 static void
5112 __lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
5113                 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
5114                 struct list_head *dequeue_list)
5115 {
5116         struct lpfc_iocbq *iocb, *next_iocb;
5117
5118         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5119                 /* Check to see if iocb matches the nport */
5120                 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
5121                         /* match, dequeue */
5122                         list_move_tail(&iocb->list, dequeue_list);
5123         }
5124 }
5125
5126 static void
5127 lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
5128                 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
5129 {
5130         struct lpfc_sli *psli = &phba->sli;
5131         uint32_t i;
5132
5133         spin_lock_irq(&phba->hbalock);
5134         for (i = 0; i < psli->num_rings; i++)
5135                 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
5136                                                 dequeue_list);
5137         spin_unlock_irq(&phba->hbalock);
5138 }
5139
5140 static void
5141 lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
5142                 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
5143 {
5144         struct lpfc_sli_ring *pring;
5145         struct lpfc_queue *qp = NULL;
5146
5147         spin_lock_irq(&phba->hbalock);
5148         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
5149                 pring = qp->pring;
5150                 if (!pring)
5151                         continue;
5152                 spin_lock(&pring->ring_lock);
5153                 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
5154                 spin_unlock(&pring->ring_lock);
5155         }
5156         spin_unlock_irq(&phba->hbalock);
5157 }
5158
5159 /*
5160  * Free resources / clean up outstanding I/Os
5161  * associated with nlp_rpi in the LPFC_NODELIST entry.
5162  */
5163 static int
5164 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
5165 {
5166         LIST_HEAD(completions);
5167
5168         lpfc_fabric_abort_nport(ndlp);
5169
5170         /*
5171          * Everything that matches on txcmplq will be returned
5172          * by firmware with a no rpi error.
5173          */
5174         if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5175                 if (phba->sli_rev != LPFC_SLI_REV4)
5176                         lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
5177                 else
5178                         lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
5179         }
5180
5181         /* Cancel all the IOCBs from the completions list */
5182         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5183                               IOERR_SLI_ABORTED);
5184
5185         return 0;
5186 }
5187
5188 /**
5189  * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
5190  * @phba: Pointer to HBA context object.
5191  * @pmb: Pointer to mailbox object.
5192  *
5193  * This function will issue an ELS LOGO command after completing
5194  * the UNREG_RPI.
5195  **/
5196 static void
5197 lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5198 {
5199         struct lpfc_vport  *vport = pmb->vport;
5200         struct lpfc_nodelist *ndlp;
5201
5202         ndlp = (struct lpfc_nodelist *)(pmb->ctx_ndlp);
5203         if (!ndlp)
5204                 return;
5205         lpfc_issue_els_logo(vport, ndlp, 0);
5206         mempool_free(pmb, phba->mbox_mem_pool);
5207
5208         /* Check to see if there are any deferred events to process */
5209         if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
5210             (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
5211                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5212                                  "1434 UNREG cmpl deferred logo x%x "
5213                                  "on NPort x%x Data: x%x x%px\n",
5214                                  ndlp->nlp_rpi, ndlp->nlp_DID,
5215                                  ndlp->nlp_defer_did, ndlp);
5216
5217                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
5218                 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
5219                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5220         } else {
5221                 /* NLP_RELEASE_RPI is only set for SLI4 ports. */
5222                 if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
5223                         lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
5224                         spin_lock_irq(&ndlp->lock);
5225                         ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
5226                         ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
5227                         spin_unlock_irq(&ndlp->lock);
5228                 }
5229                 spin_lock_irq(&ndlp->lock);
5230                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
5231                 spin_unlock_irq(&ndlp->lock);
5232         }
5233 }
5234
5235 /*
5236  * Sets the mailbox completion handler to be used for the
5237  * unreg_rpi command. The handler varies based on the state of
5238  * the port and what will be happening to the rpi next.
5239  */
5240 static void
5241 lpfc_set_unreg_login_mbx_cmpl(struct lpfc_hba *phba, struct lpfc_vport *vport,
5242         struct lpfc_nodelist *ndlp, LPFC_MBOXQ_t *mbox)
5243 {
5244         unsigned long iflags;
5245
5246         /* Driver always gets a reference on the mailbox job
5247          * in support of async jobs.
5248          */
5249         mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
5250         if (!mbox->ctx_ndlp)
5251                 return;
5252
5253         if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
5254                 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
5255
5256         } else if (phba->sli_rev == LPFC_SLI_REV4 &&
5257                    (!(vport->load_flag & FC_UNLOADING)) &&
5258                     (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
5259                                       LPFC_SLI_INTF_IF_TYPE_2) &&
5260                     (kref_read(&ndlp->kref) > 0)) {
5261                 mbox->mbox_cmpl = lpfc_sli4_unreg_rpi_cmpl_clr;
5262         } else {
5263                 if (vport->load_flag & FC_UNLOADING) {
5264                         if (phba->sli_rev == LPFC_SLI_REV4) {
5265                                 spin_lock_irqsave(&ndlp->lock, iflags);
5266                                 ndlp->nlp_flag |= NLP_RELEASE_RPI;
5267                                 spin_unlock_irqrestore(&ndlp->lock, iflags);
5268                         }
5269                 }
5270                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5271         }
5272 }
5273
5274 /*
5275  * Free rpi associated with LPFC_NODELIST entry.
5276  * This routine is called from lpfc_freenode(), when we are removing
5277  * a LPFC_NODELIST entry. It is also called if the driver initiates a
5278  * LOGO that completes successfully, and we are waiting to PLOGI back
5279  * to the remote NPort. In addition, it is called after we receive
5280  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
5281  * we are waiting to PLOGI back to the remote NPort.
5282  */
5283 int
5284 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5285 {
5286         struct lpfc_hba *phba = vport->phba;
5287         LPFC_MBOXQ_t    *mbox;
5288         int rc, acc_plogi = 1;
5289         uint16_t rpi;
5290
5291         if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
5292             ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
5293                 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
5294                         lpfc_printf_vlog(vport, KERN_INFO,
5295                                          LOG_NODE | LOG_DISCOVERY,
5296                                          "3366 RPI x%x needs to be "
5297                                          "unregistered nlp_flag x%x "
5298                                          "did x%x\n",
5299                                          ndlp->nlp_rpi, ndlp->nlp_flag,
5300                                          ndlp->nlp_DID);
5301
5302                 /* If there is already an UNREG in progress for this ndlp,
5303                  * no need to queue up another one.
5304                  */
5305                 if (ndlp->nlp_flag & NLP_UNREG_INP) {
5306                         lpfc_printf_vlog(vport, KERN_INFO,
5307                                          LOG_NODE | LOG_DISCOVERY,
5308                                          "1436 unreg_rpi SKIP UNREG x%x on "
5309                                          "NPort x%x deferred x%x  flg x%x "
5310                                          "Data: x%px\n",
5311                                          ndlp->nlp_rpi, ndlp->nlp_DID,
5312                                          ndlp->nlp_defer_did,
5313                                          ndlp->nlp_flag, ndlp);
5314                         goto out;
5315                 }
5316
5317                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5318                 if (mbox) {
5319                         /* SLI4 ports require the physical rpi value. */
5320                         rpi = ndlp->nlp_rpi;
5321                         if (phba->sli_rev == LPFC_SLI_REV4)
5322                                 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
5323
5324                         lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
5325                         mbox->vport = vport;
5326                         lpfc_set_unreg_login_mbx_cmpl(phba, vport, ndlp, mbox);
5327                         if (!mbox->ctx_ndlp) {
5328                                 mempool_free(mbox, phba->mbox_mem_pool);
5329                                 return 1;
5330                         }
5331
5332                         if (mbox->mbox_cmpl == lpfc_sli4_unreg_rpi_cmpl_clr)
5333                                 /*
5334                                  * accept PLOGIs after unreg_rpi_cmpl
5335                                  */
5336                                 acc_plogi = 0;
5337                         if (((ndlp->nlp_DID & Fabric_DID_MASK) !=
5338                             Fabric_DID_MASK) &&
5339                             (!(vport->fc_flag & FC_OFFLINE_MODE)))
5340                                 ndlp->nlp_flag |= NLP_UNREG_INP;
5341
5342                         lpfc_printf_vlog(vport, KERN_INFO,
5343                                          LOG_NODE | LOG_DISCOVERY,
5344                                          "1433 unreg_rpi UNREG x%x on "
5345                                          "NPort x%x deferred flg x%x "
5346                                          "Data:x%px\n",
5347                                          ndlp->nlp_rpi, ndlp->nlp_DID,
5348                                          ndlp->nlp_flag, ndlp);
5349
5350                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5351                         if (rc == MBX_NOT_FINISHED) {
5352                                 mempool_free(mbox, phba->mbox_mem_pool);
5353                                 acc_plogi = 1;
5354                         }
5355                 } else {
5356                         lpfc_printf_vlog(vport, KERN_INFO,
5357                                          LOG_NODE | LOG_DISCOVERY,
5358                                          "1444 Failed to allocate mempool "
5359                                          "unreg_rpi UNREG x%x, "
5360                                          "DID x%x, flag x%x, "
5361                                          "ndlp x%px\n",
5362                                          ndlp->nlp_rpi, ndlp->nlp_DID,
5363                                          ndlp->nlp_flag, ndlp);
5364
5365                         /* Because mempool_alloc failed, we
5366                          * will issue a LOGO here and keep the rpi alive if
5367                          * not unloading.
5368                          */
5369                         if (!(vport->load_flag & FC_UNLOADING)) {
5370                                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
5371                                 lpfc_issue_els_logo(vport, ndlp, 0);
5372                                 ndlp->nlp_prev_state = ndlp->nlp_state;
5373                                 lpfc_nlp_set_state(vport, ndlp,
5374                                                    NLP_STE_NPR_NODE);
5375                         }
5376
5377                         return 1;
5378                 }
5379                 lpfc_no_rpi(phba, ndlp);
5380 out:
5381                 if (phba->sli_rev != LPFC_SLI_REV4)
5382                         ndlp->nlp_rpi = 0;
5383                 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
5384                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
5385                 if (acc_plogi)
5386                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5387                 return 1;
5388         }
5389         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5390         return 0;
5391 }
5392
5393 /**
5394  * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
5395  * @phba: pointer to lpfc hba data structure.
5396  *
5397  * This routine is invoked to unregister all the currently registered RPIs
5398  * to the HBA.
5399  **/
5400 void
5401 lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
5402 {
5403         struct lpfc_vport **vports;
5404         struct lpfc_nodelist *ndlp;
5405         struct Scsi_Host *shost;
5406         int i;
5407
5408         vports = lpfc_create_vport_work_array(phba);
5409         if (!vports) {
5410                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5411                                 "2884 Vport array allocation failed \n");
5412                 return;
5413         }
5414         for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5415                 shost = lpfc_shost_from_vport(vports[i]);
5416                 spin_lock_irq(shost->host_lock);
5417                 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5418                         if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5419                                 /* The mempool_alloc might sleep */
5420                                 spin_unlock_irq(shost->host_lock);
5421                                 lpfc_unreg_rpi(vports[i], ndlp);
5422                                 spin_lock_irq(shost->host_lock);
5423                         }
5424                 }
5425                 spin_unlock_irq(shost->host_lock);
5426         }
5427         lpfc_destroy_vport_work_array(phba, vports);
5428 }
5429
5430 void
5431 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
5432 {
5433         struct lpfc_hba  *phba  = vport->phba;
5434         LPFC_MBOXQ_t     *mbox;
5435         int rc;
5436
5437         if (phba->sli_rev == LPFC_SLI_REV4) {
5438                 lpfc_sli4_unreg_all_rpis(vport);
5439                 return;
5440         }
5441
5442         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5443         if (mbox) {
5444                 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
5445                                  mbox);
5446                 mbox->vport = vport;
5447                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5448                 mbox->ctx_ndlp = NULL;
5449                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5450                 if (rc != MBX_TIMEOUT)
5451                         mempool_free(mbox, phba->mbox_mem_pool);
5452
5453                 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5454                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5455                                          "1836 Could not issue "
5456                                          "unreg_login(all_rpis) status %d\n",
5457                                          rc);
5458         }
5459 }
5460
5461 void
5462 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
5463 {
5464         struct lpfc_hba  *phba  = vport->phba;
5465         LPFC_MBOXQ_t     *mbox;
5466         int rc;
5467
5468         /* Unreg DID is an SLI3 operation. */
5469         if (phba->sli_rev > LPFC_SLI_REV3)
5470                 return;
5471
5472         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5473         if (mbox) {
5474                 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
5475                                mbox);
5476                 mbox->vport = vport;
5477                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5478                 mbox->ctx_ndlp = NULL;
5479                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5480                 if (rc != MBX_TIMEOUT)
5481                         mempool_free(mbox, phba->mbox_mem_pool);
5482
5483                 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5484                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5485                                          "1815 Could not issue "
5486                                          "unreg_did (default rpis) status %d\n",
5487                                          rc);
5488         }
5489 }
5490
5491 /*
5492  * Free resources associated with LPFC_NODELIST entry
5493  * so it can be freed.
5494  */
5495 static int
5496 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5497 {
5498         struct lpfc_hba  *phba = vport->phba;
5499         LPFC_MBOXQ_t *mb, *nextmb;
5500         struct lpfc_dmabuf *mp;
5501
5502         /* Cleanup node for NPort <nlp_DID> */
5503         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5504                          "0900 Cleanup node for NPort x%x "
5505                          "Data: x%x x%x x%x\n",
5506                          ndlp->nlp_DID, ndlp->nlp_flag,
5507                          ndlp->nlp_state, ndlp->nlp_rpi);
5508         lpfc_dequeue_node(vport, ndlp);
5509
5510         /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
5511
5512         /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
5513         if ((mb = phba->sli.mbox_active)) {
5514                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5515                    !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5516                    (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5517                         mb->ctx_ndlp = NULL;
5518                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5519                 }
5520         }
5521
5522         spin_lock_irq(&phba->hbalock);
5523         /* Cleanup REG_LOGIN completions which are not yet processed */
5524         list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
5525                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
5526                         (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
5527                         (ndlp != (struct lpfc_nodelist *)mb->ctx_ndlp))
5528                         continue;
5529
5530                 mb->ctx_ndlp = NULL;
5531                 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5532         }
5533
5534         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
5535                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5536                    !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5537                     (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5538                         mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
5539                         if (mp) {
5540                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
5541                                 kfree(mp);
5542                         }
5543                         list_del(&mb->list);
5544                         mempool_free(mb, phba->mbox_mem_pool);
5545                         /* We shall not invoke the lpfc_nlp_put to decrement
5546                          * the ndlp reference count as we are in the process
5547                          * of lpfc_nlp_release.
5548                          */
5549                 }
5550         }
5551         spin_unlock_irq(&phba->hbalock);
5552
5553         lpfc_els_abort(phba, ndlp);
5554
5555         spin_lock_irq(&ndlp->lock);
5556         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
5557         spin_unlock_irq(&ndlp->lock);
5558
5559         ndlp->nlp_last_elscmd = 0;
5560         del_timer_sync(&ndlp->nlp_delayfunc);
5561
5562         list_del_init(&ndlp->els_retry_evt.evt_listp);
5563         list_del_init(&ndlp->dev_loss_evt.evt_listp);
5564         list_del_init(&ndlp->recovery_evt.evt_listp);
5565         lpfc_cleanup_vports_rrqs(vport, ndlp);
5566
5567         if (phba->sli_rev == LPFC_SLI_REV4)
5568                 ndlp->nlp_flag |= NLP_RELEASE_RPI;
5569
5570         return 0;
5571 }
5572
5573 static int
5574 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5575               uint32_t did)
5576 {
5577         D_ID mydid, ndlpdid, matchdid;
5578
5579         if (did == Bcast_DID)
5580                 return 0;
5581
5582         /* First check for Direct match */
5583         if (ndlp->nlp_DID == did)
5584                 return 1;
5585
5586         /* Next check for area/domain identically equals 0 match */
5587         mydid.un.word = vport->fc_myDID;
5588         if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
5589                 return 0;
5590         }
5591
5592         matchdid.un.word = did;
5593         ndlpdid.un.word = ndlp->nlp_DID;
5594         if (matchdid.un.b.id == ndlpdid.un.b.id) {
5595                 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5596                     (mydid.un.b.area == matchdid.un.b.area)) {
5597                         /* This code is supposed to match the ID
5598                          * for a private loop device that is
5599                          * connect to fl_port. But we need to
5600                          * check that the port did not just go
5601                          * from pt2pt to fabric or we could end
5602                          * up matching ndlp->nlp_DID 000001 to
5603                          * fabric DID 0x20101
5604                          */
5605                         if ((ndlpdid.un.b.domain == 0) &&
5606                             (ndlpdid.un.b.area == 0)) {
5607                                 if (ndlpdid.un.b.id &&
5608                                     vport->phba->fc_topology ==
5609                                     LPFC_TOPOLOGY_LOOP)
5610                                         return 1;
5611                         }
5612                         return 0;
5613                 }
5614
5615                 matchdid.un.word = ndlp->nlp_DID;
5616                 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5617                     (mydid.un.b.area == ndlpdid.un.b.area)) {
5618                         if ((matchdid.un.b.domain == 0) &&
5619                             (matchdid.un.b.area == 0)) {
5620                                 if (matchdid.un.b.id)
5621                                         return 1;
5622                         }
5623                 }
5624         }
5625         return 0;
5626 }
5627
5628 /* Search for a nodelist entry */
5629 static struct lpfc_nodelist *
5630 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5631 {
5632         struct lpfc_nodelist *ndlp;
5633         uint32_t data1;
5634
5635         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5636                 if (lpfc_matchdid(vport, ndlp, did)) {
5637                         data1 = (((uint32_t)ndlp->nlp_state << 24) |
5638                                  ((uint32_t)ndlp->nlp_xri << 16) |
5639                                  ((uint32_t)ndlp->nlp_type << 8)
5640                                  );
5641                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5642                                          "0929 FIND node DID "
5643                                          "Data: x%px x%x x%x x%x x%x x%px\n",
5644                                          ndlp, ndlp->nlp_DID,
5645                                          ndlp->nlp_flag, data1, ndlp->nlp_rpi,
5646                                          ndlp->active_rrqs_xri_bitmap);
5647                         return ndlp;
5648                 }
5649         }
5650
5651         /* FIND node did <did> NOT FOUND */
5652         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5653                          "0932 FIND node did x%x NOT FOUND.\n", did);
5654         return NULL;
5655 }
5656
5657 struct lpfc_nodelist *
5658 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5659 {
5660         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5661         struct lpfc_nodelist *ndlp;
5662         unsigned long iflags;
5663
5664         spin_lock_irqsave(shost->host_lock, iflags);
5665         ndlp = __lpfc_findnode_did(vport, did);
5666         spin_unlock_irqrestore(shost->host_lock, iflags);
5667         return ndlp;
5668 }
5669
5670 struct lpfc_nodelist *
5671 lpfc_findnode_mapped(struct lpfc_vport *vport)
5672 {
5673         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5674         struct lpfc_nodelist *ndlp;
5675         uint32_t data1;
5676         unsigned long iflags;
5677
5678         spin_lock_irqsave(shost->host_lock, iflags);
5679
5680         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5681                 if (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
5682                     ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
5683                         data1 = (((uint32_t)ndlp->nlp_state << 24) |
5684                                  ((uint32_t)ndlp->nlp_xri << 16) |
5685                                  ((uint32_t)ndlp->nlp_type << 8) |
5686                                  ((uint32_t)ndlp->nlp_rpi & 0xff));
5687                         spin_unlock_irqrestore(shost->host_lock, iflags);
5688                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5689                                          "2025 FIND node DID "
5690                                          "Data: x%px x%x x%x x%x x%px\n",
5691                                          ndlp, ndlp->nlp_DID,
5692                                          ndlp->nlp_flag, data1,
5693                                          ndlp->active_rrqs_xri_bitmap);
5694                         return ndlp;
5695                 }
5696         }
5697         spin_unlock_irqrestore(shost->host_lock, iflags);
5698
5699         /* FIND node did <did> NOT FOUND */
5700         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5701                          "2026 FIND mapped did NOT FOUND.\n");
5702         return NULL;
5703 }
5704
5705 struct lpfc_nodelist *
5706 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5707 {
5708         struct lpfc_nodelist *ndlp;
5709
5710         ndlp = lpfc_findnode_did(vport, did);
5711         if (!ndlp) {
5712                 if (vport->phba->nvmet_support)
5713                         return NULL;
5714                 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5715                     lpfc_rscn_payload_check(vport, did) == 0)
5716                         return NULL;
5717                 ndlp = lpfc_nlp_init(vport, did);
5718                 if (!ndlp)
5719                         return NULL;
5720                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5721
5722                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5723                                  "6453 Setup New Node 2B_DISC x%x "
5724                                  "Data:x%x x%x x%x\n",
5725                                  ndlp->nlp_DID, ndlp->nlp_flag,
5726                                  ndlp->nlp_state, vport->fc_flag);
5727
5728                 spin_lock_irq(&ndlp->lock);
5729                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5730                 spin_unlock_irq(&ndlp->lock);
5731                 return ndlp;
5732         }
5733
5734         /* The NVME Target does not want to actively manage an rport.
5735          * The goal is to allow the target to reset its state and clear
5736          * pending IO in preparation for the initiator to recover.
5737          */
5738         if ((vport->fc_flag & FC_RSCN_MODE) &&
5739             !(vport->fc_flag & FC_NDISC_ACTIVE)) {
5740                 if (lpfc_rscn_payload_check(vport, did)) {
5741
5742                         /* Since this node is marked for discovery,
5743                          * delay timeout is not needed.
5744                          */
5745                         lpfc_cancel_retry_delay_tmo(vport, ndlp);
5746
5747                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5748                                          "6455 Setup RSCN Node 2B_DISC x%x "
5749                                          "Data:x%x x%x x%x\n",
5750                                          ndlp->nlp_DID, ndlp->nlp_flag,
5751                                          ndlp->nlp_state, vport->fc_flag);
5752
5753                         /* NVME Target mode waits until rport is known to be
5754                          * impacted by the RSCN before it transitions.  No
5755                          * active management - just go to NPR provided the
5756                          * node had a valid login.
5757                          */
5758                         if (vport->phba->nvmet_support)
5759                                 return ndlp;
5760
5761                         /* If we've already received a PLOGI from this NPort
5762                          * we don't need to try to discover it again.
5763                          */
5764                         if (ndlp->nlp_flag & NLP_RCV_PLOGI &&
5765                             !(ndlp->nlp_type &
5766                              (NLP_FCP_TARGET | NLP_NVME_TARGET)))
5767                                 return NULL;
5768
5769                         ndlp->nlp_prev_state = ndlp->nlp_state;
5770                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5771
5772                         spin_lock_irq(&ndlp->lock);
5773                         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5774                         spin_unlock_irq(&ndlp->lock);
5775                 } else {
5776                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5777                                          "6456 Skip Setup RSCN Node x%x "
5778                                          "Data:x%x x%x x%x\n",
5779                                          ndlp->nlp_DID, ndlp->nlp_flag,
5780                                          ndlp->nlp_state, vport->fc_flag);
5781                         ndlp = NULL;
5782                 }
5783         } else {
5784                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5785                                  "6457 Setup Active Node 2B_DISC x%x "
5786                                  "Data:x%x x%x x%x\n",
5787                                  ndlp->nlp_DID, ndlp->nlp_flag,
5788                                  ndlp->nlp_state, vport->fc_flag);
5789
5790                 /* If the initiator received a PLOGI from this NPort or if the
5791                  * initiator is already in the process of discovery on it,
5792                  * there's no need to try to discover it again.
5793                  */
5794                 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
5795                     ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5796                     (!vport->phba->nvmet_support &&
5797                      ndlp->nlp_flag & NLP_RCV_PLOGI))
5798                         return NULL;
5799
5800                 if (vport->phba->nvmet_support)
5801                         return ndlp;
5802
5803                 /* Moving to NPR state clears unsolicited flags and
5804                  * allows for rediscovery
5805                  */
5806                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5807
5808                 spin_lock_irq(&ndlp->lock);
5809                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5810                 spin_unlock_irq(&ndlp->lock);
5811         }
5812         return ndlp;
5813 }
5814
5815 /* Build a list of nodes to discover based on the loopmap */
5816 void
5817 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
5818 {
5819         struct lpfc_hba  *phba = vport->phba;
5820         int j;
5821         uint32_t alpa, index;
5822
5823         if (!lpfc_is_link_up(phba))
5824                 return;
5825
5826         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
5827                 return;
5828
5829         /* Check for loop map present or not */
5830         if (phba->alpa_map[0]) {
5831                 for (j = 1; j <= phba->alpa_map[0]; j++) {
5832                         alpa = phba->alpa_map[j];
5833                         if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
5834                                 continue;
5835                         lpfc_setup_disc_node(vport, alpa);
5836                 }
5837         } else {
5838                 /* No alpamap, so try all alpa's */
5839                 for (j = 0; j < FC_MAXLOOP; j++) {
5840                         /* If cfg_scan_down is set, start from highest
5841                          * ALPA (0xef) to lowest (0x1).
5842                          */
5843                         if (vport->cfg_scan_down)
5844                                 index = j;
5845                         else
5846                                 index = FC_MAXLOOP - j - 1;
5847                         alpa = lpfcAlpaArray[index];
5848                         if ((vport->fc_myDID & 0xff) == alpa)
5849                                 continue;
5850                         lpfc_setup_disc_node(vport, alpa);
5851                 }
5852         }
5853         return;
5854 }
5855
5856 /* SLI3 only */
5857 void
5858 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
5859 {
5860         LPFC_MBOXQ_t *mbox;
5861         struct lpfc_sli *psli = &phba->sli;
5862         struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5863         struct lpfc_sli_ring *fcp_ring   = &psli->sli3_ring[LPFC_FCP_RING];
5864         int  rc;
5865
5866         /*
5867          * if it's not a physical port or if we already send
5868          * clear_la then don't send it.
5869          */
5870         if ((phba->link_state >= LPFC_CLEAR_LA) ||
5871             (vport->port_type != LPFC_PHYSICAL_PORT) ||
5872                 (phba->sli_rev == LPFC_SLI_REV4))
5873                 return;
5874
5875                         /* Link up discovery */
5876         if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5877                 phba->link_state = LPFC_CLEAR_LA;
5878                 lpfc_clear_la(phba, mbox);
5879                 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5880                 mbox->vport = vport;
5881                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5882                 if (rc == MBX_NOT_FINISHED) {
5883                         mempool_free(mbox, phba->mbox_mem_pool);
5884                         lpfc_disc_flush_list(vport);
5885                         extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5886                         fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5887                         phba->link_state = LPFC_HBA_ERROR;
5888                 }
5889         }
5890 }
5891
5892 /* Reg_vpi to tell firmware to resume normal operations */
5893 void
5894 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5895 {
5896         LPFC_MBOXQ_t *regvpimbox;
5897
5898         regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5899         if (regvpimbox) {
5900                 lpfc_reg_vpi(vport, regvpimbox);
5901                 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5902                 regvpimbox->vport = vport;
5903                 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
5904                                         == MBX_NOT_FINISHED) {
5905                         mempool_free(regvpimbox, phba->mbox_mem_pool);
5906                 }
5907         }
5908 }
5909
5910 /* Start Link up / RSCN discovery on NPR nodes */
5911 void
5912 lpfc_disc_start(struct lpfc_vport *vport)
5913 {
5914         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5915         struct lpfc_hba  *phba = vport->phba;
5916         uint32_t num_sent;
5917         uint32_t clear_la_pending;
5918
5919         if (!lpfc_is_link_up(phba)) {
5920                 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5921                                  "3315 Link is not up %x\n",
5922                                  phba->link_state);
5923                 return;
5924         }
5925
5926         if (phba->link_state == LPFC_CLEAR_LA)
5927                 clear_la_pending = 1;
5928         else
5929                 clear_la_pending = 0;
5930
5931         if (vport->port_state < LPFC_VPORT_READY)
5932                 vport->port_state = LPFC_DISC_AUTH;
5933
5934         lpfc_set_disctmo(vport);
5935
5936         vport->fc_prevDID = vport->fc_myDID;
5937         vport->num_disc_nodes = 0;
5938
5939         /* Start Discovery state <hba_state> */
5940         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5941                          "0202 Start Discovery port state x%x "
5942                          "flg x%x Data: x%x x%x x%x\n",
5943                          vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5944                          vport->fc_adisc_cnt, vport->fc_npr_cnt);
5945
5946         /* First do ADISCs - if any */
5947         num_sent = lpfc_els_disc_adisc(vport);
5948
5949         if (num_sent)
5950                 return;
5951
5952         /* Register the VPI for SLI3, NPIV only. */
5953         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5954             !(vport->fc_flag & FC_PT2PT) &&
5955             !(vport->fc_flag & FC_RSCN_MODE) &&
5956             (phba->sli_rev < LPFC_SLI_REV4)) {
5957                 lpfc_issue_clear_la(phba, vport);
5958                 lpfc_issue_reg_vpi(phba, vport);
5959                 return;
5960         }
5961
5962         /*
5963          * For SLI2, we need to set port_state to READY and continue
5964          * discovery.
5965          */
5966         if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5967                 /* If we get here, there is nothing to ADISC */
5968                 lpfc_issue_clear_la(phba, vport);
5969
5970                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
5971                         vport->num_disc_nodes = 0;
5972                         /* go thru NPR nodes and issue ELS PLOGIs */
5973                         if (vport->fc_npr_cnt)
5974                                 lpfc_els_disc_plogi(vport);
5975
5976                         if (!vport->num_disc_nodes) {
5977                                 spin_lock_irq(shost->host_lock);
5978                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5979                                 spin_unlock_irq(shost->host_lock);
5980                                 lpfc_can_disctmo(vport);
5981                         }
5982                 }
5983                 vport->port_state = LPFC_VPORT_READY;
5984         } else {
5985                 /* Next do PLOGIs - if any */
5986                 num_sent = lpfc_els_disc_plogi(vport);
5987
5988                 if (num_sent)
5989                         return;
5990
5991                 if (vport->fc_flag & FC_RSCN_MODE) {
5992                         /* Check to see if more RSCNs came in while we
5993                          * were processing this one.
5994                          */
5995                         if ((vport->fc_rscn_id_cnt == 0) &&
5996                             (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5997                                 spin_lock_irq(shost->host_lock);
5998                                 vport->fc_flag &= ~FC_RSCN_MODE;
5999                                 spin_unlock_irq(shost->host_lock);
6000                                 lpfc_can_disctmo(vport);
6001                         } else
6002                                 lpfc_els_handle_rscn(vport);
6003                 }
6004         }
6005         return;
6006 }
6007
6008 /*
6009  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
6010  *  ring the match the sppecified nodelist.
6011  */
6012 static void
6013 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
6014 {
6015         LIST_HEAD(completions);
6016         IOCB_t     *icmd;
6017         struct lpfc_iocbq    *iocb, *next_iocb;
6018         struct lpfc_sli_ring *pring;
6019
6020         pring = lpfc_phba_elsring(phba);
6021         if (unlikely(!pring))
6022                 return;
6023
6024         /* Error matching iocb on txq or txcmplq
6025          * First check the txq.
6026          */
6027         spin_lock_irq(&phba->hbalock);
6028         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6029                 if (iocb->context1 != ndlp) {
6030                         continue;
6031                 }
6032                 icmd = &iocb->iocb;
6033                 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
6034                     (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
6035
6036                         list_move_tail(&iocb->list, &completions);
6037                 }
6038         }
6039
6040         /* Next check the txcmplq */
6041         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
6042                 if (iocb->context1 != ndlp) {
6043                         continue;
6044                 }
6045                 icmd = &iocb->iocb;
6046                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
6047                     icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
6048                         lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
6049                 }
6050         }
6051         spin_unlock_irq(&phba->hbalock);
6052
6053         /* Make sure HBA is alive */
6054         lpfc_issue_hb_tmo(phba);
6055
6056         /* Cancel all the IOCBs from the completions list */
6057         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6058                               IOERR_SLI_ABORTED);
6059 }
6060
6061 static void
6062 lpfc_disc_flush_list(struct lpfc_vport *vport)
6063 {
6064         struct lpfc_nodelist *ndlp, *next_ndlp;
6065         struct lpfc_hba *phba = vport->phba;
6066
6067         if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
6068                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
6069                                          nlp_listp) {
6070                         if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
6071                             ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
6072                                 lpfc_free_tx(phba, ndlp);
6073                         }
6074                 }
6075         }
6076 }
6077
6078 void
6079 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
6080 {
6081         lpfc_els_flush_rscn(vport);
6082         lpfc_els_flush_cmd(vport);
6083         lpfc_disc_flush_list(vport);
6084 }
6085
6086 /*****************************************************************************/
6087 /*
6088  * NAME:     lpfc_disc_timeout
6089  *
6090  * FUNCTION: Fibre Channel driver discovery timeout routine.
6091  *
6092  * EXECUTION ENVIRONMENT: interrupt only
6093  *
6094  * CALLED FROM:
6095  *      Timer function
6096  *
6097  * RETURNS:
6098  *      none
6099  */
6100 /*****************************************************************************/
6101 void
6102 lpfc_disc_timeout(struct timer_list *t)
6103 {
6104         struct lpfc_vport *vport = from_timer(vport, t, fc_disctmo);
6105         struct lpfc_hba   *phba = vport->phba;
6106         uint32_t tmo_posted;
6107         unsigned long flags = 0;
6108
6109         if (unlikely(!phba))
6110                 return;
6111
6112         spin_lock_irqsave(&vport->work_port_lock, flags);
6113         tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
6114         if (!tmo_posted)
6115                 vport->work_port_events |= WORKER_DISC_TMO;
6116         spin_unlock_irqrestore(&vport->work_port_lock, flags);
6117
6118         if (!tmo_posted)
6119                 lpfc_worker_wake_up(phba);
6120         return;
6121 }
6122
6123 static void
6124 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
6125 {
6126         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6127         struct lpfc_hba  *phba = vport->phba;
6128         struct lpfc_sli  *psli = &phba->sli;
6129         struct lpfc_nodelist *ndlp, *next_ndlp;
6130         LPFC_MBOXQ_t *initlinkmbox;
6131         int rc, clrlaerr = 0;
6132
6133         if (!(vport->fc_flag & FC_DISC_TMO))
6134                 return;
6135
6136         spin_lock_irq(shost->host_lock);
6137         vport->fc_flag &= ~FC_DISC_TMO;
6138         spin_unlock_irq(shost->host_lock);
6139
6140         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6141                 "disc timeout:    state:x%x rtry:x%x flg:x%x",
6142                 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
6143
6144         switch (vport->port_state) {
6145
6146         case LPFC_LOCAL_CFG_LINK:
6147                 /*
6148                  * port_state is identically  LPFC_LOCAL_CFG_LINK while
6149                  * waiting for FAN timeout
6150                  */
6151                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
6152                                  "0221 FAN timeout\n");
6153
6154                 /* Start discovery by sending FLOGI, clean up old rpis */
6155                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
6156                                          nlp_listp) {
6157                         if (ndlp->nlp_state != NLP_STE_NPR_NODE)
6158                                 continue;
6159                         if (ndlp->nlp_type & NLP_FABRIC) {
6160                                 /* Clean up the ndlp on Fabric connections */
6161                                 lpfc_drop_node(vport, ndlp);
6162
6163                         } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
6164                                 /* Fail outstanding IO now since device
6165                                  * is marked for PLOGI.
6166                                  */
6167                                 lpfc_unreg_rpi(vport, ndlp);
6168                         }
6169                 }
6170                 if (vport->port_state != LPFC_FLOGI) {
6171                         if (phba->sli_rev <= LPFC_SLI_REV3)
6172                                 lpfc_initial_flogi(vport);
6173                         else
6174                                 lpfc_issue_init_vfi(vport);
6175                         return;
6176                 }
6177                 break;
6178
6179         case LPFC_FDISC:
6180         case LPFC_FLOGI:
6181         /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
6182                 /* Initial FLOGI timeout */
6183                 lpfc_printf_vlog(vport, KERN_ERR,
6184                                  LOG_TRACE_EVENT,
6185                                  "0222 Initial %s timeout\n",
6186                                  vport->vpi ? "FDISC" : "FLOGI");
6187
6188                 /* Assume no Fabric and go on with discovery.
6189                  * Check for outstanding ELS FLOGI to abort.
6190                  */
6191
6192                 /* FLOGI failed, so just use loop map to make discovery list */
6193                 lpfc_disc_list_loopmap(vport);
6194
6195                 /* Start discovery */
6196                 lpfc_disc_start(vport);
6197                 break;
6198
6199         case LPFC_FABRIC_CFG_LINK:
6200         /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
6201            NameServer login */
6202                 lpfc_printf_vlog(vport, KERN_ERR,
6203                                  LOG_TRACE_EVENT,
6204                                  "0223 Timeout while waiting for "
6205                                  "NameServer login\n");
6206                 /* Next look for NameServer ndlp */
6207                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
6208                 if (ndlp)
6209                         lpfc_els_abort(phba, ndlp);
6210
6211                 /* ReStart discovery */
6212                 goto restart_disc;
6213
6214         case LPFC_NS_QRY:
6215         /* Check for wait for NameServer Rsp timeout */
6216                 lpfc_printf_vlog(vport, KERN_ERR,
6217                                  LOG_TRACE_EVENT,
6218                                  "0224 NameServer Query timeout "
6219                                  "Data: x%x x%x\n",
6220                                  vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
6221
6222                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
6223                         /* Try it one more time */
6224                         vport->fc_ns_retry++;
6225                         vport->gidft_inp = 0;
6226                         rc = lpfc_issue_gidft(vport);
6227                         if (rc == 0)
6228                                 break;
6229                 }
6230                 vport->fc_ns_retry = 0;
6231
6232 restart_disc:
6233                 /*
6234                  * Discovery is over.
6235                  * set port_state to PORT_READY if SLI2.
6236                  * cmpl_reg_vpi will set port_state to READY for SLI3.
6237                  */
6238                 if (phba->sli_rev < LPFC_SLI_REV4) {
6239                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
6240                                 lpfc_issue_reg_vpi(phba, vport);
6241                         else  {
6242                                 lpfc_issue_clear_la(phba, vport);
6243                                 vport->port_state = LPFC_VPORT_READY;
6244                         }
6245                 }
6246
6247                 /* Setup and issue mailbox INITIALIZE LINK command */
6248                 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6249                 if (!initlinkmbox) {
6250                         lpfc_printf_vlog(vport, KERN_ERR,
6251                                          LOG_TRACE_EVENT,
6252                                          "0206 Device Discovery "
6253                                          "completion error\n");
6254                         phba->link_state = LPFC_HBA_ERROR;
6255                         break;
6256                 }
6257
6258                 lpfc_linkdown(phba);
6259                 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
6260                                phba->cfg_link_speed);
6261                 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
6262                 initlinkmbox->vport = vport;
6263                 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6264                 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
6265                 lpfc_set_loopback_flag(phba);
6266                 if (rc == MBX_NOT_FINISHED)
6267                         mempool_free(initlinkmbox, phba->mbox_mem_pool);
6268
6269                 break;
6270
6271         case LPFC_DISC_AUTH:
6272         /* Node Authentication timeout */
6273                 lpfc_printf_vlog(vport, KERN_ERR,
6274                                  LOG_TRACE_EVENT,
6275                                  "0227 Node Authentication timeout\n");
6276                 lpfc_disc_flush_list(vport);
6277
6278                 /*
6279                  * set port_state to PORT_READY if SLI2.
6280                  * cmpl_reg_vpi will set port_state to READY for SLI3.
6281                  */
6282                 if (phba->sli_rev < LPFC_SLI_REV4) {
6283                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
6284                                 lpfc_issue_reg_vpi(phba, vport);
6285                         else  { /* NPIV Not enabled */
6286                                 lpfc_issue_clear_la(phba, vport);
6287                                 vport->port_state = LPFC_VPORT_READY;
6288                         }
6289                 }
6290                 break;
6291
6292         case LPFC_VPORT_READY:
6293                 if (vport->fc_flag & FC_RSCN_MODE) {
6294                         lpfc_printf_vlog(vport, KERN_ERR,
6295                                          LOG_TRACE_EVENT,
6296                                          "0231 RSCN timeout Data: x%x "
6297                                          "x%x\n",
6298                                          vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
6299
6300                         /* Cleanup any outstanding ELS commands */
6301                         lpfc_els_flush_cmd(vport);
6302
6303                         lpfc_els_flush_rscn(vport);
6304                         lpfc_disc_flush_list(vport);
6305                 }
6306                 break;
6307
6308         default:
6309                 lpfc_printf_vlog(vport, KERN_ERR,
6310                                  LOG_TRACE_EVENT,
6311                                  "0273 Unexpected discovery timeout, "
6312                                  "vport State x%x\n", vport->port_state);
6313                 break;
6314         }
6315
6316         switch (phba->link_state) {
6317         case LPFC_CLEAR_LA:
6318                                 /* CLEAR LA timeout */
6319                 lpfc_printf_vlog(vport, KERN_ERR,
6320                                  LOG_TRACE_EVENT,
6321                                  "0228 CLEAR LA timeout\n");
6322                 clrlaerr = 1;
6323                 break;
6324
6325         case LPFC_LINK_UP:
6326                 lpfc_issue_clear_la(phba, vport);
6327                 fallthrough;
6328         case LPFC_LINK_UNKNOWN:
6329         case LPFC_WARM_START:
6330         case LPFC_INIT_START:
6331         case LPFC_INIT_MBX_CMDS:
6332         case LPFC_LINK_DOWN:
6333         case LPFC_HBA_ERROR:
6334                 lpfc_printf_vlog(vport, KERN_ERR,
6335                                  LOG_TRACE_EVENT,
6336                                  "0230 Unexpected timeout, hba link "
6337                                  "state x%x\n", phba->link_state);
6338                 clrlaerr = 1;
6339                 break;
6340
6341         case LPFC_HBA_READY:
6342                 break;
6343         }
6344
6345         if (clrlaerr) {
6346                 lpfc_disc_flush_list(vport);
6347                 if (phba->sli_rev != LPFC_SLI_REV4) {
6348                         psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
6349                                 ~LPFC_STOP_IOCB_EVENT;
6350                         psli->sli3_ring[LPFC_FCP_RING].flag &=
6351                                 ~LPFC_STOP_IOCB_EVENT;
6352                 }
6353                 vport->port_state = LPFC_VPORT_READY;
6354         }
6355         return;
6356 }
6357
6358 /*
6359  * This routine handles processing a NameServer REG_LOGIN mailbox
6360  * command upon completion. It is setup in the LPFC_MBOXQ
6361  * as the completion routine when the command is
6362  * handed off to the SLI layer.
6363  */
6364 void
6365 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6366 {
6367         MAILBOX_t *mb = &pmb->u.mb;
6368         struct lpfc_dmabuf   *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
6369         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
6370         struct lpfc_vport    *vport = pmb->vport;
6371
6372         pmb->ctx_buf = NULL;
6373         pmb->ctx_ndlp = NULL;
6374
6375         if (phba->sli_rev < LPFC_SLI_REV4)
6376                 ndlp->nlp_rpi = mb->un.varWords[0];
6377         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
6378         ndlp->nlp_type |= NLP_FABRIC;
6379         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
6380         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
6381                          "0004 rpi:%x DID:%x flg:%x %d x%px\n",
6382                          ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
6383                          kref_read(&ndlp->kref),
6384                          ndlp);
6385         /*
6386          * Start issuing Fabric-Device Management Interface (FDMI) command to
6387          * 0xfffffa (FDMI well known port).
6388          * DHBA -> DPRT -> RHBA -> RPA  (physical port)
6389          * DPRT -> RPRT (vports)
6390          */
6391         if (vport->port_type == LPFC_PHYSICAL_PORT) {
6392                 phba->link_flag &= ~LS_CT_VEN_RPA; /* For extra Vendor RPA */
6393                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
6394         } else {
6395                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
6396         }
6397
6398
6399         /* decrement the node reference count held for this callback
6400          * function.
6401          */
6402         lpfc_nlp_put(ndlp);
6403         lpfc_mbuf_free(phba, mp->virt, mp->phys);
6404         kfree(mp);
6405         mempool_free(pmb, phba->mbox_mem_pool);
6406
6407         return;
6408 }
6409
6410 static int
6411 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
6412 {
6413         uint16_t *rpi = param;
6414
6415         return ndlp->nlp_rpi == *rpi;
6416 }
6417
6418 static int
6419 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
6420 {
6421         return memcmp(&ndlp->nlp_portname, param,
6422                       sizeof(ndlp->nlp_portname)) == 0;
6423 }
6424
6425 static struct lpfc_nodelist *
6426 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
6427 {
6428         struct lpfc_nodelist *ndlp;
6429
6430         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
6431                 if (filter(ndlp, param)) {
6432                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6433                                          "3185 FIND node filter %ps DID "
6434                                          "ndlp x%px did x%x flg x%x st x%x "
6435                                          "xri x%x type x%x rpi x%x\n",
6436                                          filter, ndlp, ndlp->nlp_DID,
6437                                          ndlp->nlp_flag, ndlp->nlp_state,
6438                                          ndlp->nlp_xri, ndlp->nlp_type,
6439                                          ndlp->nlp_rpi);
6440                         return ndlp;
6441                 }
6442         }
6443         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6444                          "3186 FIND node filter %ps NOT FOUND.\n", filter);
6445         return NULL;
6446 }
6447
6448 /*
6449  * This routine looks up the ndlp lists for the given RPI. If rpi found it
6450  * returns the node list element pointer else return NULL.
6451  */
6452 struct lpfc_nodelist *
6453 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6454 {
6455         return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
6456 }
6457
6458 /*
6459  * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
6460  * returns the node element list pointer else return NULL.
6461  */
6462 struct lpfc_nodelist *
6463 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
6464 {
6465         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6466         struct lpfc_nodelist *ndlp;
6467
6468         spin_lock_irq(shost->host_lock);
6469         ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
6470         spin_unlock_irq(shost->host_lock);
6471         return ndlp;
6472 }
6473
6474 /*
6475  * This routine looks up the ndlp lists for the given RPI. If the rpi
6476  * is found, the routine returns the node element list pointer else
6477  * return NULL.
6478  */
6479 struct lpfc_nodelist *
6480 lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6481 {
6482         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6483         struct lpfc_nodelist *ndlp;
6484         unsigned long flags;
6485
6486         spin_lock_irqsave(shost->host_lock, flags);
6487         ndlp = __lpfc_findnode_rpi(vport, rpi);
6488         spin_unlock_irqrestore(shost->host_lock, flags);
6489         return ndlp;
6490 }
6491
6492 /**
6493  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6494  * @phba: pointer to lpfc hba data structure.
6495  * @vpi: the physical host virtual N_Port identifier.
6496  *
6497  * This routine finds a vport on a HBA (referred by @phba) through a
6498  * @vpi. The function walks the HBA's vport list and returns the address
6499  * of the vport with the matching @vpi.
6500  *
6501  * Return code
6502  *    NULL - No vport with the matching @vpi found
6503  *    Otherwise - Address to the vport with the matching @vpi.
6504  **/
6505 struct lpfc_vport *
6506 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6507 {
6508         struct lpfc_vport *vport;
6509         unsigned long flags;
6510         int i = 0;
6511
6512         /* The physical ports are always vpi 0 - translate is unnecessary. */
6513         if (vpi > 0) {
6514                 /*
6515                  * Translate the physical vpi to the logical vpi.  The
6516                  * vport stores the logical vpi.
6517                  */
6518                 for (i = 0; i <= phba->max_vpi; i++) {
6519                         if (vpi == phba->vpi_ids[i])
6520                                 break;
6521                 }
6522
6523                 if (i > phba->max_vpi) {
6524                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6525                                         "2936 Could not find Vport mapped "
6526                                         "to vpi %d\n", vpi);
6527                         return NULL;
6528                 }
6529         }
6530
6531         spin_lock_irqsave(&phba->port_list_lock, flags);
6532         list_for_each_entry(vport, &phba->port_list, listentry) {
6533                 if (vport->vpi == i) {
6534                         spin_unlock_irqrestore(&phba->port_list_lock, flags);
6535                         return vport;
6536                 }
6537         }
6538         spin_unlock_irqrestore(&phba->port_list_lock, flags);
6539         return NULL;
6540 }
6541
6542 struct lpfc_nodelist *
6543 lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
6544 {
6545         struct lpfc_nodelist *ndlp;
6546         int rpi = LPFC_RPI_ALLOC_ERROR;
6547
6548         if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6549                 rpi = lpfc_sli4_alloc_rpi(vport->phba);
6550                 if (rpi == LPFC_RPI_ALLOC_ERROR)
6551                         return NULL;
6552         }
6553
6554         ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
6555         if (!ndlp) {
6556                 if (vport->phba->sli_rev == LPFC_SLI_REV4)
6557                         lpfc_sli4_free_rpi(vport->phba, rpi);
6558                 return NULL;
6559         }
6560
6561         memset(ndlp, 0, sizeof (struct lpfc_nodelist));
6562
6563         spin_lock_init(&ndlp->lock);
6564
6565         lpfc_initialize_node(vport, ndlp, did);
6566         INIT_LIST_HEAD(&ndlp->nlp_listp);
6567         if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6568                 ndlp->nlp_rpi = rpi;
6569                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
6570                                  "0007 Init New ndlp x%px, rpi:x%x DID:%x "
6571                                  "flg:x%x refcnt:%d\n",
6572                                  ndlp, ndlp->nlp_rpi, ndlp->nlp_DID,
6573                                  ndlp->nlp_flag, kref_read(&ndlp->kref));
6574
6575                 ndlp->active_rrqs_xri_bitmap =
6576                                 mempool_alloc(vport->phba->active_rrq_pool,
6577                                               GFP_KERNEL);
6578                 if (ndlp->active_rrqs_xri_bitmap)
6579                         memset(ndlp->active_rrqs_xri_bitmap, 0,
6580                                ndlp->phba->cfg_rrq_xri_bitmap_sz);
6581         }
6582
6583
6584
6585         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
6586                 "node init:       did:x%x",
6587                 ndlp->nlp_DID, 0, 0);
6588
6589         return ndlp;
6590 }
6591
6592 /* This routine releases all resources associated with a specifc NPort's ndlp
6593  * and mempool_free's the nodelist.
6594  */
6595 static void
6596 lpfc_nlp_release(struct kref *kref)
6597 {
6598         struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
6599                                                   kref);
6600         struct lpfc_vport *vport = ndlp->vport;
6601
6602         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6603                 "node release:    did:x%x flg:x%x type:x%x",
6604                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
6605
6606         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6607                          "0279 %s: ndlp: x%px did %x refcnt:%d rpi:%x\n",
6608                          __func__, ndlp, ndlp->nlp_DID,
6609                          kref_read(&ndlp->kref), ndlp->nlp_rpi);
6610
6611         /* remove ndlp from action. */
6612         lpfc_cancel_retry_delay_tmo(vport, ndlp);
6613         lpfc_cleanup_node(vport, ndlp);
6614
6615         /* Not all ELS transactions have registered the RPI with the port.
6616          * In these cases the rpi usage is temporary and the node is
6617          * released when the WQE is completed.  Catch this case to free the
6618          * RPI to the pool.  Because this node is in the release path, a lock
6619          * is unnecessary.  All references are gone and the node has been
6620          * dequeued.
6621          */
6622         if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
6623                 if (ndlp->nlp_rpi != LPFC_RPI_ALLOC_ERROR &&
6624                     !(ndlp->nlp_flag & (NLP_RPI_REGISTERED | NLP_UNREG_INP))) {
6625                         lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
6626                         ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
6627                 }
6628         }
6629
6630         /* The node is not freed back to memory, it is released to a pool so
6631          * the node fields need to be cleaned up.
6632          */
6633         ndlp->vport = NULL;
6634         ndlp->nlp_state = NLP_STE_FREED_NODE;
6635         ndlp->nlp_flag = 0;
6636         ndlp->fc4_xpt_flags = 0;
6637
6638         /* free ndlp memory for final ndlp release */
6639         kfree(ndlp->lat_data);
6640         if (ndlp->phba->sli_rev == LPFC_SLI_REV4)
6641                 mempool_free(ndlp->active_rrqs_xri_bitmap,
6642                                 ndlp->phba->active_rrq_pool);
6643         mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
6644 }
6645
6646 /* This routine bumps the reference count for a ndlp structure to ensure
6647  * that one discovery thread won't free a ndlp while another discovery thread
6648  * is using it.
6649  */
6650 struct lpfc_nodelist *
6651 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6652 {
6653         unsigned long flags;
6654
6655         if (ndlp) {
6656                 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6657                         "node get:        did:x%x flg:x%x refcnt:x%x",
6658                         ndlp->nlp_DID, ndlp->nlp_flag,
6659                         kref_read(&ndlp->kref));
6660
6661                 /* The check of ndlp usage to prevent incrementing the
6662                  * ndlp reference count that is in the process of being
6663                  * released.
6664                  */
6665                 spin_lock_irqsave(&ndlp->lock, flags);
6666                 if (!kref_get_unless_zero(&ndlp->kref)) {
6667                         spin_unlock_irqrestore(&ndlp->lock, flags);
6668                         lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6669                                 "0276 %s: ndlp:x%px refcnt:%d\n",
6670                                 __func__, (void *)ndlp, kref_read(&ndlp->kref));
6671                         return NULL;
6672                 }
6673                 spin_unlock_irqrestore(&ndlp->lock, flags);
6674         } else {
6675                 WARN_ONCE(!ndlp, "**** %s, get ref on NULL ndlp!", __func__);
6676         }
6677
6678         return ndlp;
6679 }
6680
6681 /* This routine decrements the reference count for a ndlp structure. If the
6682  * count goes to 0, this indicates the associated nodelist should be freed.
6683  */
6684 int
6685 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6686 {
6687         if (ndlp) {
6688                 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6689                                 "node put:        did:x%x flg:x%x refcnt:x%x",
6690                                 ndlp->nlp_DID, ndlp->nlp_flag,
6691                                 kref_read(&ndlp->kref));
6692         } else {
6693                 WARN_ONCE(!ndlp, "**** %s, put ref on NULL ndlp!", __func__);
6694         }
6695
6696         return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
6697 }
6698
6699 /* This routine free's the specified nodelist if it is not in use
6700  * by any other discovery thread. This routine returns 1 if the
6701  * ndlp has been freed. A return value of 0 indicates the ndlp is
6702  * not yet been released.
6703  */
6704 int
6705 lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6706 {
6707         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6708                 "node not used:   did:x%x flg:x%x refcnt:x%x",
6709                 ndlp->nlp_DID, ndlp->nlp_flag,
6710                 kref_read(&ndlp->kref));
6711
6712         if (kref_read(&ndlp->kref) == 1)
6713                 if (lpfc_nlp_put(ndlp))
6714                         return 1;
6715         return 0;
6716 }
6717
6718 /**
6719  * lpfc_fcf_inuse - Check if FCF can be unregistered.
6720  * @phba: Pointer to hba context object.
6721  *
6722  * This function iterate through all FC nodes associated
6723  * will all vports to check if there is any node with
6724  * fc_rports associated with it. If there is an fc_rport
6725  * associated with the node, then the node is either in
6726  * discovered state or its devloss_timer is pending.
6727  */
6728 static int
6729 lpfc_fcf_inuse(struct lpfc_hba *phba)
6730 {
6731         struct lpfc_vport **vports;
6732         int i, ret = 0;
6733         struct lpfc_nodelist *ndlp;
6734         struct Scsi_Host  *shost;
6735
6736         vports = lpfc_create_vport_work_array(phba);
6737
6738         /* If driver cannot allocate memory, indicate fcf is in use */
6739         if (!vports)
6740                 return 1;
6741
6742         for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6743                 shost = lpfc_shost_from_vport(vports[i]);
6744                 spin_lock_irq(shost->host_lock);
6745                 /*
6746                  * IF the CVL_RCVD bit is not set then we have sent the
6747                  * flogi.
6748                  * If dev_loss fires while we are waiting we do not want to
6749                  * unreg the fcf.
6750                  */
6751                 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6752                         spin_unlock_irq(shost->host_lock);
6753                         ret =  1;
6754                         goto out;
6755                 }
6756                 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6757                         if (ndlp->rport &&
6758                           (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6759                                 ret = 1;
6760                                 spin_unlock_irq(shost->host_lock);
6761                                 goto out;
6762                         } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6763                                 ret = 1;
6764                                 lpfc_printf_log(phba, KERN_INFO,
6765                                                 LOG_NODE | LOG_DISCOVERY,
6766                                                 "2624 RPI %x DID %x flag %x "
6767                                                 "still logged in\n",
6768                                                 ndlp->nlp_rpi, ndlp->nlp_DID,
6769                                                 ndlp->nlp_flag);
6770                         }
6771                 }
6772                 spin_unlock_irq(shost->host_lock);
6773         }
6774 out:
6775         lpfc_destroy_vport_work_array(phba, vports);
6776         return ret;
6777 }
6778
6779 /**
6780  * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6781  * @phba: Pointer to hba context object.
6782  * @mboxq: Pointer to mailbox object.
6783  *
6784  * This function frees memory associated with the mailbox command.
6785  */
6786 void
6787 lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6788 {
6789         struct lpfc_vport *vport = mboxq->vport;
6790         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6791
6792         if (mboxq->u.mb.mbxStatus) {
6793                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6794                                 "2555 UNREG_VFI mbxStatus error x%x "
6795                                 "HBA state x%x\n",
6796                                 mboxq->u.mb.mbxStatus, vport->port_state);
6797         }
6798         spin_lock_irq(shost->host_lock);
6799         phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6800         spin_unlock_irq(shost->host_lock);
6801         mempool_free(mboxq, phba->mbox_mem_pool);
6802         return;
6803 }
6804
6805 /**
6806  * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6807  * @phba: Pointer to hba context object.
6808  * @mboxq: Pointer to mailbox object.
6809  *
6810  * This function frees memory associated with the mailbox command.
6811  */
6812 static void
6813 lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6814 {
6815         struct lpfc_vport *vport = mboxq->vport;
6816
6817         if (mboxq->u.mb.mbxStatus) {
6818                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6819                                 "2550 UNREG_FCFI mbxStatus error x%x "
6820                                 "HBA state x%x\n",
6821                                 mboxq->u.mb.mbxStatus, vport->port_state);
6822         }
6823         mempool_free(mboxq, phba->mbox_mem_pool);
6824         return;
6825 }
6826
6827 /**
6828  * lpfc_unregister_fcf_prep - Unregister fcf record preparation
6829  * @phba: Pointer to hba context object.
6830  *
6831  * This function prepare the HBA for unregistering the currently registered
6832  * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6833  * VFIs.
6834  */
6835 int
6836 lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
6837 {
6838         struct lpfc_vport **vports;
6839         struct lpfc_nodelist *ndlp;
6840         struct Scsi_Host *shost;
6841         int i = 0, rc;
6842
6843         /* Unregister RPIs */
6844         if (lpfc_fcf_inuse(phba))
6845                 lpfc_unreg_hba_rpis(phba);
6846
6847         /* At this point, all discovery is aborted */
6848         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
6849
6850         /* Unregister VPIs */
6851         vports = lpfc_create_vport_work_array(phba);
6852         if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
6853                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6854                         /* Stop FLOGI/FDISC retries */
6855                         ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6856                         if (ndlp)
6857                                 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
6858                         lpfc_cleanup_pending_mbox(vports[i]);
6859                         if (phba->sli_rev == LPFC_SLI_REV4)
6860                                 lpfc_sli4_unreg_all_rpis(vports[i]);
6861                         lpfc_mbx_unreg_vpi(vports[i]);
6862                         shost = lpfc_shost_from_vport(vports[i]);
6863                         spin_lock_irq(shost->host_lock);
6864                         vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6865                         vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
6866                         spin_unlock_irq(shost->host_lock);
6867                 }
6868         lpfc_destroy_vport_work_array(phba, vports);
6869         if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6870                 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6871                 if (ndlp)
6872                         lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6873                 lpfc_cleanup_pending_mbox(phba->pport);
6874                 if (phba->sli_rev == LPFC_SLI_REV4)
6875                         lpfc_sli4_unreg_all_rpis(phba->pport);
6876                 lpfc_mbx_unreg_vpi(phba->pport);
6877                 shost = lpfc_shost_from_vport(phba->pport);
6878                 spin_lock_irq(shost->host_lock);
6879                 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6880                 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6881                 spin_unlock_irq(shost->host_lock);
6882         }
6883
6884         /* Cleanup any outstanding ELS commands */
6885         lpfc_els_flush_all_cmd(phba);
6886
6887         /* Unregister the physical port VFI */
6888         rc = lpfc_issue_unreg_vfi(phba->pport);
6889         return rc;
6890 }
6891
6892 /**
6893  * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6894  * @phba: Pointer to hba context object.
6895  *
6896  * This function issues synchronous unregister FCF mailbox command to HBA to
6897  * unregister the currently registered FCF record. The driver does not reset
6898  * the driver FCF usage state flags.
6899  *
6900  * Return 0 if successfully issued, none-zero otherwise.
6901  */
6902 int
6903 lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6904 {
6905         LPFC_MBOXQ_t *mbox;
6906         int rc;
6907
6908         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6909         if (!mbox) {
6910                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6911                                 "2551 UNREG_FCFI mbox allocation failed"
6912                                 "HBA state x%x\n", phba->pport->port_state);
6913                 return -ENOMEM;
6914         }
6915         lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6916         mbox->vport = phba->pport;
6917         mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6918         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6919
6920         if (rc == MBX_NOT_FINISHED) {
6921                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6922                                 "2552 Unregister FCFI command failed rc x%x "
6923                                 "HBA state x%x\n",
6924                                 rc, phba->pport->port_state);
6925                 return -EINVAL;
6926         }
6927         return 0;
6928 }
6929
6930 /**
6931  * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6932  * @phba: Pointer to hba context object.
6933  *
6934  * This function unregisters the currently reigstered FCF. This function
6935  * also tries to find another FCF for discovery by rescan the HBA FCF table.
6936  */
6937 void
6938 lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6939 {
6940         int rc;
6941
6942         /* Preparation for unregistering fcf */
6943         rc = lpfc_unregister_fcf_prep(phba);
6944         if (rc) {
6945                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6946                                 "2748 Failed to prepare for unregistering "
6947                                 "HBA's FCF record: rc=%d\n", rc);
6948                 return;
6949         }
6950
6951         /* Now, unregister FCF record and reset HBA FCF state */
6952         rc = lpfc_sli4_unregister_fcf(phba);
6953         if (rc)
6954                 return;
6955         /* Reset HBA FCF states after successful unregister FCF */
6956         phba->fcf.fcf_flag = 0;
6957         phba->fcf.current_rec.flag = 0;
6958
6959         /*
6960          * If driver is not unloading, check if there is any other
6961          * FCF record that can be used for discovery.
6962          */
6963         if ((phba->pport->load_flag & FC_UNLOADING) ||
6964             (phba->link_state < LPFC_LINK_UP))
6965                 return;
6966
6967         /* This is considered as the initial FCF discovery scan */
6968         spin_lock_irq(&phba->hbalock);
6969         phba->fcf.fcf_flag |= FCF_INIT_DISC;
6970         spin_unlock_irq(&phba->hbalock);
6971
6972         /* Reset FCF roundrobin bmask for new discovery */
6973         lpfc_sli4_clear_fcf_rr_bmask(phba);
6974
6975         rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
6976
6977         if (rc) {
6978                 spin_lock_irq(&phba->hbalock);
6979                 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6980                 spin_unlock_irq(&phba->hbalock);
6981                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6982                                 "2553 lpfc_unregister_unused_fcf failed "
6983                                 "to read FCF record HBA state x%x\n",
6984                                 phba->pport->port_state);
6985         }
6986 }
6987
6988 /**
6989  * lpfc_unregister_fcf - Unregister the currently registered fcf record
6990  * @phba: Pointer to hba context object.
6991  *
6992  * This function just unregisters the currently reigstered FCF. It does not
6993  * try to find another FCF for discovery.
6994  */
6995 void
6996 lpfc_unregister_fcf(struct lpfc_hba *phba)
6997 {
6998         int rc;
6999
7000         /* Preparation for unregistering fcf */
7001         rc = lpfc_unregister_fcf_prep(phba);
7002         if (rc) {
7003                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7004                                 "2749 Failed to prepare for unregistering "
7005                                 "HBA's FCF record: rc=%d\n", rc);
7006                 return;
7007         }
7008
7009         /* Now, unregister FCF record and reset HBA FCF state */
7010         rc = lpfc_sli4_unregister_fcf(phba);
7011         if (rc)
7012                 return;
7013         /* Set proper HBA FCF states after successful unregister FCF */
7014         spin_lock_irq(&phba->hbalock);
7015         phba->fcf.fcf_flag &= ~FCF_REGISTERED;
7016         spin_unlock_irq(&phba->hbalock);
7017 }
7018
7019 /**
7020  * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
7021  * @phba: Pointer to hba context object.
7022  *
7023  * This function check if there are any connected remote port for the FCF and
7024  * if all the devices are disconnected, this function unregister FCFI.
7025  * This function also tries to use another FCF for discovery.
7026  */
7027 void
7028 lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
7029 {
7030         /*
7031          * If HBA is not running in FIP mode, if HBA does not support
7032          * FCoE, if FCF discovery is ongoing, or if FCF has not been
7033          * registered, do nothing.
7034          */
7035         spin_lock_irq(&phba->hbalock);
7036         if (!(phba->hba_flag & HBA_FCOE_MODE) ||
7037             !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
7038             !(phba->hba_flag & HBA_FIP_SUPPORT) ||
7039             (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
7040             (phba->pport->port_state == LPFC_FLOGI)) {
7041                 spin_unlock_irq(&phba->hbalock);
7042                 return;
7043         }
7044         spin_unlock_irq(&phba->hbalock);
7045
7046         if (lpfc_fcf_inuse(phba))
7047                 return;
7048
7049         lpfc_unregister_fcf_rescan(phba);
7050 }
7051
7052 /**
7053  * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
7054  * @phba: Pointer to hba context object.
7055  * @buff: Buffer containing the FCF connection table as in the config
7056  *         region.
7057  * This function create driver data structure for the FCF connection
7058  * record table read from config region 23.
7059  */
7060 static void
7061 lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
7062         uint8_t *buff)
7063 {
7064         struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
7065         struct lpfc_fcf_conn_hdr *conn_hdr;
7066         struct lpfc_fcf_conn_rec *conn_rec;
7067         uint32_t record_count;
7068         int i;
7069
7070         /* Free the current connect table */
7071         list_for_each_entry_safe(conn_entry, next_conn_entry,
7072                 &phba->fcf_conn_rec_list, list) {
7073                 list_del_init(&conn_entry->list);
7074                 kfree(conn_entry);
7075         }
7076
7077         conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
7078         record_count = conn_hdr->length * sizeof(uint32_t)/
7079                 sizeof(struct lpfc_fcf_conn_rec);
7080
7081         conn_rec = (struct lpfc_fcf_conn_rec *)
7082                 (buff + sizeof(struct lpfc_fcf_conn_hdr));
7083
7084         for (i = 0; i < record_count; i++) {
7085                 if (!(conn_rec[i].flags & FCFCNCT_VALID))
7086                         continue;
7087                 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
7088                         GFP_KERNEL);
7089                 if (!conn_entry) {
7090                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7091                                         "2566 Failed to allocate connection"
7092                                         " table entry\n");
7093                         return;
7094                 }
7095
7096                 memcpy(&conn_entry->conn_rec, &conn_rec[i],
7097                         sizeof(struct lpfc_fcf_conn_rec));
7098                 list_add_tail(&conn_entry->list,
7099                         &phba->fcf_conn_rec_list);
7100         }
7101
7102         if (!list_empty(&phba->fcf_conn_rec_list)) {
7103                 i = 0;
7104                 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
7105                                     list) {
7106                         conn_rec = &conn_entry->conn_rec;
7107                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7108                                         "3345 FCF connection list rec[%02d]: "
7109                                         "flags:x%04x, vtag:x%04x, "
7110                                         "fabric_name:x%02x:%02x:%02x:%02x:"
7111                                         "%02x:%02x:%02x:%02x, "
7112                                         "switch_name:x%02x:%02x:%02x:%02x:"
7113                                         "%02x:%02x:%02x:%02x\n", i++,
7114                                         conn_rec->flags, conn_rec->vlan_tag,
7115                                         conn_rec->fabric_name[0],
7116                                         conn_rec->fabric_name[1],
7117                                         conn_rec->fabric_name[2],
7118                                         conn_rec->fabric_name[3],
7119                                         conn_rec->fabric_name[4],
7120                                         conn_rec->fabric_name[5],
7121                                         conn_rec->fabric_name[6],
7122                                         conn_rec->fabric_name[7],
7123                                         conn_rec->switch_name[0],
7124                                         conn_rec->switch_name[1],
7125                                         conn_rec->switch_name[2],
7126                                         conn_rec->switch_name[3],
7127                                         conn_rec->switch_name[4],
7128                                         conn_rec->switch_name[5],
7129                                         conn_rec->switch_name[6],
7130                                         conn_rec->switch_name[7]);
7131                 }
7132         }
7133 }
7134
7135 /**
7136  * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
7137  * @phba: Pointer to hba context object.
7138  * @buff: Buffer containing the FCoE parameter data structure.
7139  *
7140  *  This function update driver data structure with config
7141  *  parameters read from config region 23.
7142  */
7143 static void
7144 lpfc_read_fcoe_param(struct lpfc_hba *phba,
7145                         uint8_t *buff)
7146 {
7147         struct lpfc_fip_param_hdr *fcoe_param_hdr;
7148         struct lpfc_fcoe_params *fcoe_param;
7149
7150         fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
7151                 buff;
7152         fcoe_param = (struct lpfc_fcoe_params *)
7153                 (buff + sizeof(struct lpfc_fip_param_hdr));
7154
7155         if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
7156                 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
7157                 return;
7158
7159         if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
7160                 phba->valid_vlan = 1;
7161                 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
7162                         0xFFF;
7163         }
7164
7165         phba->fc_map[0] = fcoe_param->fc_map[0];
7166         phba->fc_map[1] = fcoe_param->fc_map[1];
7167         phba->fc_map[2] = fcoe_param->fc_map[2];
7168         return;
7169 }
7170
7171 /**
7172  * lpfc_get_rec_conf23 - Get a record type in config region data.
7173  * @buff: Buffer containing config region 23 data.
7174  * @size: Size of the data buffer.
7175  * @rec_type: Record type to be searched.
7176  *
7177  * This function searches config region data to find the beginning
7178  * of the record specified by record_type. If record found, this
7179  * function return pointer to the record else return NULL.
7180  */
7181 static uint8_t *
7182 lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
7183 {
7184         uint32_t offset = 0, rec_length;
7185
7186         if ((buff[0] == LPFC_REGION23_LAST_REC) ||
7187                 (size < sizeof(uint32_t)))
7188                 return NULL;
7189
7190         rec_length = buff[offset + 1];
7191
7192         /*
7193          * One TLV record has one word header and number of data words
7194          * specified in the rec_length field of the record header.
7195          */
7196         while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
7197                 <= size) {
7198                 if (buff[offset] == rec_type)
7199                         return &buff[offset];
7200
7201                 if (buff[offset] == LPFC_REGION23_LAST_REC)
7202                         return NULL;
7203
7204                 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
7205                 rec_length = buff[offset + 1];
7206         }
7207         return NULL;
7208 }
7209
7210 /**
7211  * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
7212  * @phba: Pointer to lpfc_hba data structure.
7213  * @buff: Buffer containing config region 23 data.
7214  * @size: Size of the data buffer.
7215  *
7216  * This function parses the FCoE config parameters in config region 23 and
7217  * populate driver data structure with the parameters.
7218  */
7219 void
7220 lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
7221                 uint8_t *buff,
7222                 uint32_t size)
7223 {
7224         uint32_t offset = 0;
7225         uint8_t *rec_ptr;
7226
7227         /*
7228          * If data size is less than 2 words signature and version cannot be
7229          * verified.
7230          */
7231         if (size < 2*sizeof(uint32_t))
7232                 return;
7233
7234         /* Check the region signature first */
7235         if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
7236                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7237                         "2567 Config region 23 has bad signature\n");
7238                 return;
7239         }
7240
7241         offset += 4;
7242
7243         /* Check the data structure version */
7244         if (buff[offset] != LPFC_REGION23_VERSION) {
7245                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7246                                 "2568 Config region 23 has bad version\n");
7247                 return;
7248         }
7249         offset += 4;
7250
7251         /* Read FCoE param record */
7252         rec_ptr = lpfc_get_rec_conf23(&buff[offset],
7253                         size - offset, FCOE_PARAM_TYPE);
7254         if (rec_ptr)
7255                 lpfc_read_fcoe_param(phba, rec_ptr);
7256
7257         /* Read FCF connection table */
7258         rec_ptr = lpfc_get_rec_conf23(&buff[offset],
7259                 size - offset, FCOE_CONN_TBL_TYPE);
7260         if (rec_ptr)
7261                 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
7262
7263 }