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