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