]> Git Repo - linux.git/blob - drivers/scsi/lpfc/lpfc_hbadisc.c
ASoC: simple-card: Use snd_soc_of_parse_aux_devs()
[linux.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                         /* fall through */
4732                 case CMD_ELS_REQUEST64_CR:
4733                         if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4734                                 return 1;
4735                         /* fall through */
4736                 case CMD_XMIT_ELS_RSP64_CX:
4737                         if (iocb->context1 == (uint8_t *) ndlp)
4738                                 return 1;
4739                         /* fall through */
4740                 }
4741         } else if (pring->ringno == LPFC_FCP_RING) {
4742                 /* Skip match check if waiting to relogin to FCP target */
4743                 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
4744                     (ndlp->nlp_flag & NLP_DELAY_TMO)) {
4745                         return 0;
4746                 }
4747                 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
4748                         return 1;
4749                 }
4750         }
4751         return 0;
4752 }
4753
4754 static void
4755 __lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4756                 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4757                 struct list_head *dequeue_list)
4758 {
4759         struct lpfc_iocbq *iocb, *next_iocb;
4760
4761         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4762                 /* Check to see if iocb matches the nport */
4763                 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4764                         /* match, dequeue */
4765                         list_move_tail(&iocb->list, dequeue_list);
4766         }
4767 }
4768
4769 static void
4770 lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4771                 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4772 {
4773         struct lpfc_sli *psli = &phba->sli;
4774         uint32_t i;
4775
4776         spin_lock_irq(&phba->hbalock);
4777         for (i = 0; i < psli->num_rings; i++)
4778                 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4779                                                 dequeue_list);
4780         spin_unlock_irq(&phba->hbalock);
4781 }
4782
4783 static void
4784 lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4785                 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4786 {
4787         struct lpfc_sli_ring *pring;
4788         struct lpfc_queue *qp = NULL;
4789
4790         spin_lock_irq(&phba->hbalock);
4791         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4792                 pring = qp->pring;
4793                 if (!pring)
4794                         continue;
4795                 spin_lock(&pring->ring_lock);
4796                 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
4797                 spin_unlock(&pring->ring_lock);
4798         }
4799         spin_unlock_irq(&phba->hbalock);
4800 }
4801
4802 /*
4803  * Free resources / clean up outstanding I/Os
4804  * associated with nlp_rpi in the LPFC_NODELIST entry.
4805  */
4806 static int
4807 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
4808 {
4809         LIST_HEAD(completions);
4810
4811         lpfc_fabric_abort_nport(ndlp);
4812
4813         /*
4814          * Everything that matches on txcmplq will be returned
4815          * by firmware with a no rpi error.
4816          */
4817         if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
4818                 if (phba->sli_rev != LPFC_SLI_REV4)
4819                         lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4820                 else
4821                         lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
4822         }
4823
4824         /* Cancel all the IOCBs from the completions list */
4825         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4826                               IOERR_SLI_ABORTED);
4827
4828         return 0;
4829 }
4830
4831 /**
4832  * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4833  * @phba: Pointer to HBA context object.
4834  * @pmb: Pointer to mailbox object.
4835  *
4836  * This function will issue an ELS LOGO command after completing
4837  * the UNREG_RPI.
4838  **/
4839 static void
4840 lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4841 {
4842         struct lpfc_vport  *vport = pmb->vport;
4843         struct lpfc_nodelist *ndlp;
4844
4845         ndlp = (struct lpfc_nodelist *)(pmb->ctx_ndlp);
4846         if (!ndlp)
4847                 return;
4848         lpfc_issue_els_logo(vport, ndlp, 0);
4849         mempool_free(pmb, phba->mbox_mem_pool);
4850
4851         /* Check to see if there are any deferred events to process */
4852         if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
4853             (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
4854                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4855                                  "1434 UNREG cmpl deferred logo x%x "
4856                                  "on NPort x%x Data: x%x x%px\n",
4857                                  ndlp->nlp_rpi, ndlp->nlp_DID,
4858                                  ndlp->nlp_defer_did, ndlp);
4859
4860                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
4861                 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4862                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4863         } else {
4864                 if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
4865                         lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
4866                         ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
4867                         ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
4868                 }
4869                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
4870         }
4871 }
4872
4873 /*
4874  * Sets the mailbox completion handler to be used for the
4875  * unreg_rpi command. The handler varies based on the state of
4876  * the port and what will be happening to the rpi next.
4877  */
4878 static void
4879 lpfc_set_unreg_login_mbx_cmpl(struct lpfc_hba *phba, struct lpfc_vport *vport,
4880         struct lpfc_nodelist *ndlp, LPFC_MBOXQ_t *mbox)
4881 {
4882         unsigned long iflags;
4883
4884         if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4885                 mbox->ctx_ndlp = ndlp;
4886                 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4887
4888         } else if (phba->sli_rev == LPFC_SLI_REV4 &&
4889                    (!(vport->load_flag & FC_UNLOADING)) &&
4890                     (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
4891                                       LPFC_SLI_INTF_IF_TYPE_2) &&
4892                     (kref_read(&ndlp->kref) > 0)) {
4893                 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
4894                 mbox->mbox_cmpl = lpfc_sli4_unreg_rpi_cmpl_clr;
4895         } else {
4896                 if (vport->load_flag & FC_UNLOADING) {
4897                         if (phba->sli_rev == LPFC_SLI_REV4) {
4898                                 spin_lock_irqsave(&vport->phba->ndlp_lock,
4899                                                   iflags);
4900                                 ndlp->nlp_flag |= NLP_RELEASE_RPI;
4901                                 spin_unlock_irqrestore(&vport->phba->ndlp_lock,
4902                                                        iflags);
4903                         }
4904                         lpfc_nlp_get(ndlp);
4905                 }
4906                 mbox->ctx_ndlp = ndlp;
4907                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4908         }
4909 }
4910
4911 /*
4912  * Free rpi associated with LPFC_NODELIST entry.
4913  * This routine is called from lpfc_freenode(), when we are removing
4914  * a LPFC_NODELIST entry. It is also called if the driver initiates a
4915  * LOGO that completes successfully, and we are waiting to PLOGI back
4916  * to the remote NPort. In addition, it is called after we receive
4917  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4918  * we are waiting to PLOGI back to the remote NPort.
4919  */
4920 int
4921 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4922 {
4923         struct lpfc_hba *phba = vport->phba;
4924         LPFC_MBOXQ_t    *mbox;
4925         int rc, acc_plogi = 1;
4926         uint16_t rpi;
4927
4928         if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4929             ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4930                 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4931                         lpfc_printf_vlog(vport, KERN_INFO,
4932                                          LOG_NODE | LOG_DISCOVERY,
4933                                          "3366 RPI x%x needs to be "
4934                                          "unregistered nlp_flag x%x "
4935                                          "did x%x\n",
4936                                          ndlp->nlp_rpi, ndlp->nlp_flag,
4937                                          ndlp->nlp_DID);
4938
4939                 /* If there is already an UNREG in progress for this ndlp,
4940                  * no need to queue up another one.
4941                  */
4942                 if (ndlp->nlp_flag & NLP_UNREG_INP) {
4943                         lpfc_printf_vlog(vport, KERN_INFO,
4944                                          LOG_NODE | LOG_DISCOVERY,
4945                                          "1436 unreg_rpi SKIP UNREG x%x on "
4946                                          "NPort x%x deferred x%x  flg x%x "
4947                                          "Data: x%px\n",
4948                                          ndlp->nlp_rpi, ndlp->nlp_DID,
4949                                          ndlp->nlp_defer_did,
4950                                          ndlp->nlp_flag, ndlp);
4951                         goto out;
4952                 }
4953
4954                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4955                 if (mbox) {
4956                         /* SLI4 ports require the physical rpi value. */
4957                         rpi = ndlp->nlp_rpi;
4958                         if (phba->sli_rev == LPFC_SLI_REV4)
4959                                 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4960
4961                         lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
4962                         mbox->vport = vport;
4963                         lpfc_set_unreg_login_mbx_cmpl(phba, vport, ndlp, mbox);
4964                         if (mbox->mbox_cmpl == lpfc_sli4_unreg_rpi_cmpl_clr)
4965                                 /*
4966                                  * accept PLOGIs after unreg_rpi_cmpl
4967                                  */
4968                                 acc_plogi = 0;
4969                         if (((ndlp->nlp_DID & Fabric_DID_MASK) !=
4970                             Fabric_DID_MASK) &&
4971                             (!(vport->fc_flag & FC_OFFLINE_MODE)))
4972                                 ndlp->nlp_flag |= NLP_UNREG_INP;
4973
4974                         lpfc_printf_vlog(vport, KERN_INFO,
4975                                          LOG_NODE | LOG_DISCOVERY,
4976                                          "1433 unreg_rpi UNREG x%x on "
4977                                          "NPort x%x deferred flg x%x "
4978                                          "Data:x%px\n",
4979                                          ndlp->nlp_rpi, ndlp->nlp_DID,
4980                                          ndlp->nlp_flag, ndlp);
4981
4982                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4983                         if (rc == MBX_NOT_FINISHED) {
4984                                 mempool_free(mbox, phba->mbox_mem_pool);
4985                                 acc_plogi = 1;
4986                         }
4987                 }
4988                 lpfc_no_rpi(phba, ndlp);
4989 out:
4990                 if (phba->sli_rev != LPFC_SLI_REV4)
4991                         ndlp->nlp_rpi = 0;
4992                 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
4993                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
4994                 if (acc_plogi)
4995                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4996                 return 1;
4997         }
4998         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4999         return 0;
5000 }
5001
5002 /**
5003  * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
5004  * @phba: pointer to lpfc hba data structure.
5005  *
5006  * This routine is invoked to unregister all the currently registered RPIs
5007  * to the HBA.
5008  **/
5009 void
5010 lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
5011 {
5012         struct lpfc_vport **vports;
5013         struct lpfc_nodelist *ndlp;
5014         struct Scsi_Host *shost;
5015         int i;
5016
5017         vports = lpfc_create_vport_work_array(phba);
5018         if (!vports) {
5019                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5020                                 "2884 Vport array allocation failed \n");
5021                 return;
5022         }
5023         for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5024                 shost = lpfc_shost_from_vport(vports[i]);
5025                 spin_lock_irq(shost->host_lock);
5026                 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5027                         if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5028                                 /* The mempool_alloc might sleep */
5029                                 spin_unlock_irq(shost->host_lock);
5030                                 lpfc_unreg_rpi(vports[i], ndlp);
5031                                 spin_lock_irq(shost->host_lock);
5032                         }
5033                 }
5034                 spin_unlock_irq(shost->host_lock);
5035         }
5036         lpfc_destroy_vport_work_array(phba, vports);
5037 }
5038
5039 void
5040 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
5041 {
5042         struct lpfc_hba  *phba  = vport->phba;
5043         LPFC_MBOXQ_t     *mbox;
5044         int rc;
5045
5046         if (phba->sli_rev == LPFC_SLI_REV4) {
5047                 lpfc_sli4_unreg_all_rpis(vport);
5048                 return;
5049         }
5050
5051         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5052         if (mbox) {
5053                 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
5054                                  mbox);
5055                 mbox->vport = vport;
5056                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5057                 mbox->ctx_ndlp = NULL;
5058                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5059                 if (rc != MBX_TIMEOUT)
5060                         mempool_free(mbox, phba->mbox_mem_pool);
5061
5062                 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5063                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5064                                          "1836 Could not issue "
5065                                          "unreg_login(all_rpis) status %d\n",
5066                                          rc);
5067         }
5068 }
5069
5070 void
5071 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
5072 {
5073         struct lpfc_hba  *phba  = vport->phba;
5074         LPFC_MBOXQ_t     *mbox;
5075         int rc;
5076
5077         /* Unreg DID is an SLI3 operation. */
5078         if (phba->sli_rev > LPFC_SLI_REV3)
5079                 return;
5080
5081         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5082         if (mbox) {
5083                 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
5084                                mbox);
5085                 mbox->vport = vport;
5086                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5087                 mbox->ctx_ndlp = NULL;
5088                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5089                 if (rc != MBX_TIMEOUT)
5090                         mempool_free(mbox, phba->mbox_mem_pool);
5091
5092                 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5093                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5094                                          "1815 Could not issue "
5095                                          "unreg_did (default rpis) status %d\n",
5096                                          rc);
5097         }
5098 }
5099
5100 /*
5101  * Free resources associated with LPFC_NODELIST entry
5102  * so it can be freed.
5103  */
5104 static int
5105 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5106 {
5107         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5108         struct lpfc_hba  *phba = vport->phba;
5109         LPFC_MBOXQ_t *mb, *nextmb;
5110         struct lpfc_dmabuf *mp;
5111         unsigned long iflags;
5112
5113         /* Cleanup node for NPort <nlp_DID> */
5114         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5115                          "0900 Cleanup node for NPort x%x "
5116                          "Data: x%x x%x x%x\n",
5117                          ndlp->nlp_DID, ndlp->nlp_flag,
5118                          ndlp->nlp_state, ndlp->nlp_rpi);
5119         if (NLP_CHK_FREE_REQ(ndlp)) {
5120                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5121                                 "0280 %s: ndlp:x%px "
5122                                 "usgmap:x%x refcnt:%d\n",
5123                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
5124                                 kref_read(&ndlp->kref));
5125                 lpfc_dequeue_node(vport, ndlp);
5126         } else {
5127                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5128                                 "0281 %s: ndlp:x%px "
5129                                 "usgmap:x%x refcnt:%d\n",
5130                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
5131                                 kref_read(&ndlp->kref));
5132                 lpfc_disable_node(vport, ndlp);
5133         }
5134
5135
5136         /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
5137
5138         /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
5139         if ((mb = phba->sli.mbox_active)) {
5140                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5141                    !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5142                    (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5143                         mb->ctx_ndlp = NULL;
5144                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5145                 }
5146         }
5147
5148         spin_lock_irq(&phba->hbalock);
5149         /* Cleanup REG_LOGIN completions which are not yet processed */
5150         list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
5151                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
5152                         (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
5153                         (ndlp != (struct lpfc_nodelist *)mb->ctx_ndlp))
5154                         continue;
5155
5156                 mb->ctx_ndlp = NULL;
5157                 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5158         }
5159
5160         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
5161                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5162                    !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5163                     (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5164                         mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
5165                         if (mp) {
5166                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
5167                                 kfree(mp);
5168                         }
5169                         list_del(&mb->list);
5170                         mempool_free(mb, phba->mbox_mem_pool);
5171                         /* We shall not invoke the lpfc_nlp_put to decrement
5172                          * the ndlp reference count as we are in the process
5173                          * of lpfc_nlp_release.
5174                          */
5175                 }
5176         }
5177         spin_unlock_irq(&phba->hbalock);
5178
5179         lpfc_els_abort(phba, ndlp);
5180
5181         spin_lock_irq(shost->host_lock);
5182         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
5183         spin_unlock_irq(shost->host_lock);
5184
5185         ndlp->nlp_last_elscmd = 0;
5186         del_timer_sync(&ndlp->nlp_delayfunc);
5187
5188         list_del_init(&ndlp->els_retry_evt.evt_listp);
5189         list_del_init(&ndlp->dev_loss_evt.evt_listp);
5190         lpfc_cleanup_vports_rrqs(vport, ndlp);
5191         if (phba->sli_rev == LPFC_SLI_REV4)
5192                 ndlp->nlp_flag |= NLP_RELEASE_RPI;
5193         if (!lpfc_unreg_rpi(vport, ndlp)) {
5194                 /* Clean up unregistered and non freed rpis */
5195                 if ((ndlp->nlp_flag & NLP_RELEASE_RPI) &&
5196                     !(ndlp->nlp_rpi == LPFC_RPI_ALLOC_ERROR)) {
5197                         lpfc_sli4_free_rpi(vport->phba,
5198                                            ndlp->nlp_rpi);
5199                         spin_lock_irqsave(&vport->phba->ndlp_lock,
5200                                           iflags);
5201                         ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
5202                         ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
5203                         spin_unlock_irqrestore(&vport->phba->ndlp_lock,
5204                                                iflags);
5205                 }
5206         }
5207         return 0;
5208 }
5209
5210 /*
5211  * Check to see if we can free the nlp back to the freelist.
5212  * If we are in the middle of using the nlp in the discovery state
5213  * machine, defer the free till we reach the end of the state machine.
5214  */
5215 static void
5216 lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5217 {
5218         struct lpfc_hba  *phba = vport->phba;
5219         struct lpfc_rport_data *rdata;
5220         struct fc_rport *rport;
5221         LPFC_MBOXQ_t *mbox;
5222         int rc;
5223
5224         lpfc_cancel_retry_delay_tmo(vport, ndlp);
5225         if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
5226             !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
5227             !(ndlp->nlp_flag & NLP_RPI_REGISTERED) &&
5228             phba->sli_rev != LPFC_SLI_REV4) {
5229                 /* For this case we need to cleanup the default rpi
5230                  * allocated by the firmware.
5231                  */
5232                 lpfc_printf_vlog(vport, KERN_INFO,
5233                                  LOG_NODE | LOG_DISCOVERY,
5234                                  "0005 Cleanup Default rpi:x%x DID:x%x flg:x%x "
5235                                  "ref %d map:x%x ndlp x%px\n",
5236                                  ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5237                                  kref_read(&ndlp->kref),
5238                                  ndlp->nlp_usg_map, ndlp);
5239                 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
5240                         != NULL) {
5241                         rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
5242                             (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
5243                         if (rc) {
5244                                 mempool_free(mbox, phba->mbox_mem_pool);
5245                         }
5246                         else {
5247                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
5248                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
5249                                 mbox->vport = vport;
5250                                 mbox->ctx_ndlp = ndlp;
5251                                 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5252                                 if (rc == MBX_NOT_FINISHED) {
5253                                         mempool_free(mbox, phba->mbox_mem_pool);
5254                                 }
5255                         }
5256                 }
5257         }
5258         lpfc_cleanup_node(vport, ndlp);
5259
5260         /*
5261          * ndlp->rport must be set to NULL before it reaches here
5262          * i.e. break rport/node link before doing lpfc_nlp_put for
5263          * registered rport and then drop the reference of rport.
5264          */
5265         if (ndlp->rport) {
5266                 /*
5267                  * extra lpfc_nlp_put dropped the reference of ndlp
5268                  * for registered rport so need to cleanup rport
5269                  */
5270                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5271                                 "0940 removed node x%px DID x%x "
5272                                 "rpi %d rport not null x%px\n",
5273                                  ndlp, ndlp->nlp_DID, ndlp->nlp_rpi,
5274                                  ndlp->rport);
5275                 rport = ndlp->rport;
5276                 rdata = rport->dd_data;
5277                 rdata->pnode = NULL;
5278                 ndlp->rport = NULL;
5279                 put_device(&rport->dev);
5280         }
5281 }
5282
5283 static int
5284 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5285               uint32_t did)
5286 {
5287         D_ID mydid, ndlpdid, matchdid;
5288
5289         if (did == Bcast_DID)
5290                 return 0;
5291
5292         /* First check for Direct match */
5293         if (ndlp->nlp_DID == did)
5294                 return 1;
5295
5296         /* Next check for area/domain identically equals 0 match */
5297         mydid.un.word = vport->fc_myDID;
5298         if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
5299                 return 0;
5300         }
5301
5302         matchdid.un.word = did;
5303         ndlpdid.un.word = ndlp->nlp_DID;
5304         if (matchdid.un.b.id == ndlpdid.un.b.id) {
5305                 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5306                     (mydid.un.b.area == matchdid.un.b.area)) {
5307                         /* This code is supposed to match the ID
5308                          * for a private loop device that is
5309                          * connect to fl_port. But we need to
5310                          * check that the port did not just go
5311                          * from pt2pt to fabric or we could end
5312                          * up matching ndlp->nlp_DID 000001 to
5313                          * fabric DID 0x20101
5314                          */
5315                         if ((ndlpdid.un.b.domain == 0) &&
5316                             (ndlpdid.un.b.area == 0)) {
5317                                 if (ndlpdid.un.b.id &&
5318                                     vport->phba->fc_topology ==
5319                                     LPFC_TOPOLOGY_LOOP)
5320                                         return 1;
5321                         }
5322                         return 0;
5323                 }
5324
5325                 matchdid.un.word = ndlp->nlp_DID;
5326                 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5327                     (mydid.un.b.area == ndlpdid.un.b.area)) {
5328                         if ((matchdid.un.b.domain == 0) &&
5329                             (matchdid.un.b.area == 0)) {
5330                                 if (matchdid.un.b.id)
5331                                         return 1;
5332                         }
5333                 }
5334         }
5335         return 0;
5336 }
5337
5338 /* Search for a nodelist entry */
5339 static struct lpfc_nodelist *
5340 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5341 {
5342         struct lpfc_nodelist *ndlp;
5343         uint32_t data1;
5344
5345         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5346                 if (lpfc_matchdid(vport, ndlp, did)) {
5347                         data1 = (((uint32_t)ndlp->nlp_state << 24) |
5348                                  ((uint32_t)ndlp->nlp_xri << 16) |
5349                                  ((uint32_t)ndlp->nlp_type << 8) |
5350                                  ((uint32_t)ndlp->nlp_usg_map & 0xff));
5351                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5352                                          "0929 FIND node DID "
5353                                          "Data: x%px x%x x%x x%x x%x x%px\n",
5354                                          ndlp, ndlp->nlp_DID,
5355                                          ndlp->nlp_flag, data1, ndlp->nlp_rpi,
5356                                          ndlp->active_rrqs_xri_bitmap);
5357                         return ndlp;
5358                 }
5359         }
5360
5361         /* FIND node did <did> NOT FOUND */
5362         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5363                          "0932 FIND node did x%x NOT FOUND.\n", did);
5364         return NULL;
5365 }
5366
5367 struct lpfc_nodelist *
5368 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5369 {
5370         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5371         struct lpfc_nodelist *ndlp;
5372         unsigned long iflags;
5373
5374         spin_lock_irqsave(shost->host_lock, iflags);
5375         ndlp = __lpfc_findnode_did(vport, did);
5376         spin_unlock_irqrestore(shost->host_lock, iflags);
5377         return ndlp;
5378 }
5379
5380 struct lpfc_nodelist *
5381 lpfc_findnode_mapped(struct lpfc_vport *vport)
5382 {
5383         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5384         struct lpfc_nodelist *ndlp;
5385         uint32_t data1;
5386         unsigned long iflags;
5387
5388         spin_lock_irqsave(shost->host_lock, iflags);
5389
5390         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5391                 if (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
5392                     ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
5393                         data1 = (((uint32_t)ndlp->nlp_state << 24) |
5394                                  ((uint32_t)ndlp->nlp_xri << 16) |
5395                                  ((uint32_t)ndlp->nlp_type << 8) |
5396                                  ((uint32_t)ndlp->nlp_rpi & 0xff));
5397                         spin_unlock_irqrestore(shost->host_lock, iflags);
5398                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5399                                          "2025 FIND node DID "
5400                                          "Data: x%px x%x x%x x%x x%px\n",
5401                                          ndlp, ndlp->nlp_DID,
5402                                          ndlp->nlp_flag, data1,
5403                                          ndlp->active_rrqs_xri_bitmap);
5404                         return ndlp;
5405                 }
5406         }
5407         spin_unlock_irqrestore(shost->host_lock, iflags);
5408
5409         /* FIND node did <did> NOT FOUND */
5410         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5411                          "2026 FIND mapped did NOT FOUND.\n");
5412         return NULL;
5413 }
5414
5415 struct lpfc_nodelist *
5416 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5417 {
5418         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5419         struct lpfc_nodelist *ndlp;
5420
5421         ndlp = lpfc_findnode_did(vport, did);
5422         if (!ndlp) {
5423                 if (vport->phba->nvmet_support)
5424                         return NULL;
5425                 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5426                     lpfc_rscn_payload_check(vport, did) == 0)
5427                         return NULL;
5428                 ndlp = lpfc_nlp_init(vport, did);
5429                 if (!ndlp)
5430                         return NULL;
5431                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5432
5433                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5434                                  "6453 Setup New Node 2B_DISC x%x "
5435                                  "Data:x%x x%x x%x\n",
5436                                  ndlp->nlp_DID, ndlp->nlp_flag,
5437                                  ndlp->nlp_state, vport->fc_flag);
5438
5439                 spin_lock_irq(shost->host_lock);
5440                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5441                 spin_unlock_irq(shost->host_lock);
5442                 return ndlp;
5443         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5444                 if (vport->phba->nvmet_support)
5445                         return NULL;
5446                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
5447                 if (!ndlp) {
5448                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
5449                                          "0014 Could not enable ndlp\n");
5450                         return NULL;
5451                 }
5452                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5453                                  "6454 Setup Enabled Node 2B_DISC x%x "
5454                                  "Data:x%x x%x x%x\n",
5455                                  ndlp->nlp_DID, ndlp->nlp_flag,
5456                                  ndlp->nlp_state, vport->fc_flag);
5457
5458                 spin_lock_irq(shost->host_lock);
5459                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5460                 spin_unlock_irq(shost->host_lock);
5461                 return ndlp;
5462         }
5463
5464         /* The NVME Target does not want to actively manage an rport.
5465          * The goal is to allow the target to reset its state and clear
5466          * pending IO in preparation for the initiator to recover.
5467          */
5468         if ((vport->fc_flag & FC_RSCN_MODE) &&
5469             !(vport->fc_flag & FC_NDISC_ACTIVE)) {
5470                 if (lpfc_rscn_payload_check(vport, did)) {
5471
5472                         /* Since this node is marked for discovery,
5473                          * delay timeout is not needed.
5474                          */
5475                         lpfc_cancel_retry_delay_tmo(vport, ndlp);
5476
5477                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5478                                          "6455 Setup RSCN Node 2B_DISC x%x "
5479                                          "Data:x%x x%x x%x\n",
5480                                          ndlp->nlp_DID, ndlp->nlp_flag,
5481                                          ndlp->nlp_state, vport->fc_flag);
5482
5483                         /* NVME Target mode waits until rport is known to be
5484                          * impacted by the RSCN before it transitions.  No
5485                          * active management - just go to NPR provided the
5486                          * node had a valid login.
5487                          */
5488                         if (vport->phba->nvmet_support)
5489                                 return ndlp;
5490
5491                         /* If we've already received a PLOGI from this NPort
5492                          * we don't need to try to discover it again.
5493                          */
5494                         if (ndlp->nlp_flag & NLP_RCV_PLOGI &&
5495                             !(ndlp->nlp_type &
5496                              (NLP_FCP_TARGET | NLP_NVME_TARGET)))
5497                                 return NULL;
5498
5499                         ndlp->nlp_prev_state = ndlp->nlp_state;
5500                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5501
5502                         spin_lock_irq(shost->host_lock);
5503                         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5504                         spin_unlock_irq(shost->host_lock);
5505                 } else {
5506                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5507                                          "6456 Skip Setup RSCN Node x%x "
5508                                          "Data:x%x x%x x%x\n",
5509                                          ndlp->nlp_DID, ndlp->nlp_flag,
5510                                          ndlp->nlp_state, vport->fc_flag);
5511                         ndlp = NULL;
5512                 }
5513         } else {
5514                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5515                                  "6457 Setup Active Node 2B_DISC x%x "
5516                                  "Data:x%x x%x x%x\n",
5517                                  ndlp->nlp_DID, ndlp->nlp_flag,
5518                                  ndlp->nlp_state, vport->fc_flag);
5519
5520                 /* If the initiator received a PLOGI from this NPort or if the
5521                  * initiator is already in the process of discovery on it,
5522                  * there's no need to try to discover it again.
5523                  */
5524                 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
5525                     ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5526                     (!vport->phba->nvmet_support &&
5527                      ndlp->nlp_flag & NLP_RCV_PLOGI))
5528                         return NULL;
5529
5530                 if (vport->phba->nvmet_support)
5531                         return ndlp;
5532
5533                 /* Moving to NPR state clears unsolicited flags and
5534                  * allows for rediscovery
5535                  */
5536                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5537
5538                 spin_lock_irq(shost->host_lock);
5539                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5540                 spin_unlock_irq(shost->host_lock);
5541         }
5542         return ndlp;
5543 }
5544
5545 /* Build a list of nodes to discover based on the loopmap */
5546 void
5547 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
5548 {
5549         struct lpfc_hba  *phba = vport->phba;
5550         int j;
5551         uint32_t alpa, index;
5552
5553         if (!lpfc_is_link_up(phba))
5554                 return;
5555
5556         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
5557                 return;
5558
5559         /* Check for loop map present or not */
5560         if (phba->alpa_map[0]) {
5561                 for (j = 1; j <= phba->alpa_map[0]; j++) {
5562                         alpa = phba->alpa_map[j];
5563                         if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
5564                                 continue;
5565                         lpfc_setup_disc_node(vport, alpa);
5566                 }
5567         } else {
5568                 /* No alpamap, so try all alpa's */
5569                 for (j = 0; j < FC_MAXLOOP; j++) {
5570                         /* If cfg_scan_down is set, start from highest
5571                          * ALPA (0xef) to lowest (0x1).
5572                          */
5573                         if (vport->cfg_scan_down)
5574                                 index = j;
5575                         else
5576                                 index = FC_MAXLOOP - j - 1;
5577                         alpa = lpfcAlpaArray[index];
5578                         if ((vport->fc_myDID & 0xff) == alpa)
5579                                 continue;
5580                         lpfc_setup_disc_node(vport, alpa);
5581                 }
5582         }
5583         return;
5584 }
5585
5586 /* SLI3 only */
5587 void
5588 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
5589 {
5590         LPFC_MBOXQ_t *mbox;
5591         struct lpfc_sli *psli = &phba->sli;
5592         struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5593         struct lpfc_sli_ring *fcp_ring   = &psli->sli3_ring[LPFC_FCP_RING];
5594         int  rc;
5595
5596         /*
5597          * if it's not a physical port or if we already send
5598          * clear_la then don't send it.
5599          */
5600         if ((phba->link_state >= LPFC_CLEAR_LA) ||
5601             (vport->port_type != LPFC_PHYSICAL_PORT) ||
5602                 (phba->sli_rev == LPFC_SLI_REV4))
5603                 return;
5604
5605                         /* Link up discovery */
5606         if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5607                 phba->link_state = LPFC_CLEAR_LA;
5608                 lpfc_clear_la(phba, mbox);
5609                 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5610                 mbox->vport = vport;
5611                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5612                 if (rc == MBX_NOT_FINISHED) {
5613                         mempool_free(mbox, phba->mbox_mem_pool);
5614                         lpfc_disc_flush_list(vport);
5615                         extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5616                         fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5617                         phba->link_state = LPFC_HBA_ERROR;
5618                 }
5619         }
5620 }
5621
5622 /* Reg_vpi to tell firmware to resume normal operations */
5623 void
5624 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5625 {
5626         LPFC_MBOXQ_t *regvpimbox;
5627
5628         regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5629         if (regvpimbox) {
5630                 lpfc_reg_vpi(vport, regvpimbox);
5631                 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5632                 regvpimbox->vport = vport;
5633                 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
5634                                         == MBX_NOT_FINISHED) {
5635                         mempool_free(regvpimbox, phba->mbox_mem_pool);
5636                 }
5637         }
5638 }
5639
5640 /* Start Link up / RSCN discovery on NPR nodes */
5641 void
5642 lpfc_disc_start(struct lpfc_vport *vport)
5643 {
5644         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5645         struct lpfc_hba  *phba = vport->phba;
5646         uint32_t num_sent;
5647         uint32_t clear_la_pending;
5648
5649         if (!lpfc_is_link_up(phba)) {
5650                 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5651                                  "3315 Link is not up %x\n",
5652                                  phba->link_state);
5653                 return;
5654         }
5655
5656         if (phba->link_state == LPFC_CLEAR_LA)
5657                 clear_la_pending = 1;
5658         else
5659                 clear_la_pending = 0;
5660
5661         if (vport->port_state < LPFC_VPORT_READY)
5662                 vport->port_state = LPFC_DISC_AUTH;
5663
5664         lpfc_set_disctmo(vport);
5665
5666         vport->fc_prevDID = vport->fc_myDID;
5667         vport->num_disc_nodes = 0;
5668
5669         /* Start Discovery state <hba_state> */
5670         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5671                          "0202 Start Discovery port state x%x "
5672                          "flg x%x Data: x%x x%x x%x\n",
5673                          vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5674                          vport->fc_adisc_cnt, vport->fc_npr_cnt);
5675
5676         /* First do ADISCs - if any */
5677         num_sent = lpfc_els_disc_adisc(vport);
5678
5679         if (num_sent)
5680                 return;
5681
5682         /* Register the VPI for SLI3, NPIV only. */
5683         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5684             !(vport->fc_flag & FC_PT2PT) &&
5685             !(vport->fc_flag & FC_RSCN_MODE) &&
5686             (phba->sli_rev < LPFC_SLI_REV4)) {
5687                 lpfc_issue_clear_la(phba, vport);
5688                 lpfc_issue_reg_vpi(phba, vport);
5689                 return;
5690         }
5691
5692         /*
5693          * For SLI2, we need to set port_state to READY and continue
5694          * discovery.
5695          */
5696         if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5697                 /* If we get here, there is nothing to ADISC */
5698                 lpfc_issue_clear_la(phba, vport);
5699
5700                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
5701                         vport->num_disc_nodes = 0;
5702                         /* go thru NPR nodes and issue ELS PLOGIs */
5703                         if (vport->fc_npr_cnt)
5704                                 lpfc_els_disc_plogi(vport);
5705
5706                         if (!vport->num_disc_nodes) {
5707                                 spin_lock_irq(shost->host_lock);
5708                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5709                                 spin_unlock_irq(shost->host_lock);
5710                                 lpfc_can_disctmo(vport);
5711                         }
5712                 }
5713                 vport->port_state = LPFC_VPORT_READY;
5714         } else {
5715                 /* Next do PLOGIs - if any */
5716                 num_sent = lpfc_els_disc_plogi(vport);
5717
5718                 if (num_sent)
5719                         return;
5720
5721                 if (vport->fc_flag & FC_RSCN_MODE) {
5722                         /* Check to see if more RSCNs came in while we
5723                          * were processing this one.
5724                          */
5725                         if ((vport->fc_rscn_id_cnt == 0) &&
5726                             (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5727                                 spin_lock_irq(shost->host_lock);
5728                                 vport->fc_flag &= ~FC_RSCN_MODE;
5729                                 spin_unlock_irq(shost->host_lock);
5730                                 lpfc_can_disctmo(vport);
5731                         } else
5732                                 lpfc_els_handle_rscn(vport);
5733                 }
5734         }
5735         return;
5736 }
5737
5738 /*
5739  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5740  *  ring the match the sppecified nodelist.
5741  */
5742 static void
5743 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
5744 {
5745         LIST_HEAD(completions);
5746         IOCB_t     *icmd;
5747         struct lpfc_iocbq    *iocb, *next_iocb;
5748         struct lpfc_sli_ring *pring;
5749
5750         pring = lpfc_phba_elsring(phba);
5751         if (unlikely(!pring))
5752                 return;
5753
5754         /* Error matching iocb on txq or txcmplq
5755          * First check the txq.
5756          */
5757         spin_lock_irq(&phba->hbalock);
5758         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5759                 if (iocb->context1 != ndlp) {
5760                         continue;
5761                 }
5762                 icmd = &iocb->iocb;
5763                 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5764                     (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5765
5766                         list_move_tail(&iocb->list, &completions);
5767                 }
5768         }
5769
5770         /* Next check the txcmplq */
5771         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5772                 if (iocb->context1 != ndlp) {
5773                         continue;
5774                 }
5775                 icmd = &iocb->iocb;
5776                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5777                     icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
5778                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
5779                 }
5780         }
5781         spin_unlock_irq(&phba->hbalock);
5782
5783         /* Cancel all the IOCBs from the completions list */
5784         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5785                               IOERR_SLI_ABORTED);
5786 }
5787
5788 static void
5789 lpfc_disc_flush_list(struct lpfc_vport *vport)
5790 {
5791         struct lpfc_nodelist *ndlp, *next_ndlp;
5792         struct lpfc_hba *phba = vport->phba;
5793
5794         if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5795                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
5796                                          nlp_listp) {
5797                         if (!NLP_CHK_NODE_ACT(ndlp))
5798                                 continue;
5799                         if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5800                             ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5801                                 lpfc_free_tx(phba, ndlp);
5802                         }
5803                 }
5804         }
5805 }
5806
5807 void
5808 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5809 {
5810         lpfc_els_flush_rscn(vport);
5811         lpfc_els_flush_cmd(vport);
5812         lpfc_disc_flush_list(vport);
5813 }
5814
5815 /*****************************************************************************/
5816 /*
5817  * NAME:     lpfc_disc_timeout
5818  *
5819  * FUNCTION: Fibre Channel driver discovery timeout routine.
5820  *
5821  * EXECUTION ENVIRONMENT: interrupt only
5822  *
5823  * CALLED FROM:
5824  *      Timer function
5825  *
5826  * RETURNS:
5827  *      none
5828  */
5829 /*****************************************************************************/
5830 void
5831 lpfc_disc_timeout(struct timer_list *t)
5832 {
5833         struct lpfc_vport *vport = from_timer(vport, t, fc_disctmo);
5834         struct lpfc_hba   *phba = vport->phba;
5835         uint32_t tmo_posted;
5836         unsigned long flags = 0;
5837
5838         if (unlikely(!phba))
5839                 return;
5840
5841         spin_lock_irqsave(&vport->work_port_lock, flags);
5842         tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5843         if (!tmo_posted)
5844                 vport->work_port_events |= WORKER_DISC_TMO;
5845         spin_unlock_irqrestore(&vport->work_port_lock, flags);
5846
5847         if (!tmo_posted)
5848                 lpfc_worker_wake_up(phba);
5849         return;
5850 }
5851
5852 static void
5853 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
5854 {
5855         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5856         struct lpfc_hba  *phba = vport->phba;
5857         struct lpfc_sli  *psli = &phba->sli;
5858         struct lpfc_nodelist *ndlp, *next_ndlp;
5859         LPFC_MBOXQ_t *initlinkmbox;
5860         int rc, clrlaerr = 0;
5861
5862         if (!(vport->fc_flag & FC_DISC_TMO))
5863                 return;
5864
5865         spin_lock_irq(shost->host_lock);
5866         vport->fc_flag &= ~FC_DISC_TMO;
5867         spin_unlock_irq(shost->host_lock);
5868
5869         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5870                 "disc timeout:    state:x%x rtry:x%x flg:x%x",
5871                 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5872
5873         switch (vport->port_state) {
5874
5875         case LPFC_LOCAL_CFG_LINK:
5876                 /*
5877                  * port_state is identically  LPFC_LOCAL_CFG_LINK while
5878                  * waiting for FAN timeout
5879                  */
5880                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5881                                  "0221 FAN timeout\n");
5882
5883                 /* Start discovery by sending FLOGI, clean up old rpis */
5884                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
5885                                          nlp_listp) {
5886                         if (!NLP_CHK_NODE_ACT(ndlp))
5887                                 continue;
5888                         if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5889                                 continue;
5890                         if (ndlp->nlp_type & NLP_FABRIC) {
5891                                 /* Clean up the ndlp on Fabric connections */
5892                                 lpfc_drop_node(vport, ndlp);
5893
5894                         } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
5895                                 /* Fail outstanding IO now since device
5896                                  * is marked for PLOGI.
5897                                  */
5898                                 lpfc_unreg_rpi(vport, ndlp);
5899                         }
5900                 }
5901                 if (vport->port_state != LPFC_FLOGI) {
5902                         if (phba->sli_rev <= LPFC_SLI_REV3)
5903                                 lpfc_initial_flogi(vport);
5904                         else
5905                                 lpfc_issue_init_vfi(vport);
5906                         return;
5907                 }
5908                 break;
5909
5910         case LPFC_FDISC:
5911         case LPFC_FLOGI:
5912         /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
5913                 /* Initial FLOGI timeout */
5914                 lpfc_printf_vlog(vport, KERN_ERR,
5915                                  LOG_TRACE_EVENT,
5916                                  "0222 Initial %s timeout\n",
5917                                  vport->vpi ? "FDISC" : "FLOGI");
5918
5919                 /* Assume no Fabric and go on with discovery.
5920                  * Check for outstanding ELS FLOGI to abort.
5921                  */
5922
5923                 /* FLOGI failed, so just use loop map to make discovery list */
5924                 lpfc_disc_list_loopmap(vport);
5925
5926                 /* Start discovery */
5927                 lpfc_disc_start(vport);
5928                 break;
5929
5930         case LPFC_FABRIC_CFG_LINK:
5931         /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5932            NameServer login */
5933                 lpfc_printf_vlog(vport, KERN_ERR,
5934                                  LOG_TRACE_EVENT,
5935                                  "0223 Timeout while waiting for "
5936                                  "NameServer login\n");
5937                 /* Next look for NameServer ndlp */
5938                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
5939                 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
5940                         lpfc_els_abort(phba, ndlp);
5941
5942                 /* ReStart discovery */
5943                 goto restart_disc;
5944
5945         case LPFC_NS_QRY:
5946         /* Check for wait for NameServer Rsp timeout */
5947                 lpfc_printf_vlog(vport, KERN_ERR,
5948                                  LOG_TRACE_EVENT,
5949                                  "0224 NameServer Query timeout "
5950                                  "Data: x%x x%x\n",
5951                                  vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
5952
5953                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5954                         /* Try it one more time */
5955                         vport->fc_ns_retry++;
5956                         vport->gidft_inp = 0;
5957                         rc = lpfc_issue_gidft(vport);
5958                         if (rc == 0)
5959                                 break;
5960                 }
5961                 vport->fc_ns_retry = 0;
5962
5963 restart_disc:
5964                 /*
5965                  * Discovery is over.
5966                  * set port_state to PORT_READY if SLI2.
5967                  * cmpl_reg_vpi will set port_state to READY for SLI3.
5968                  */
5969                 if (phba->sli_rev < LPFC_SLI_REV4) {
5970                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5971                                 lpfc_issue_reg_vpi(phba, vport);
5972                         else  {
5973                                 lpfc_issue_clear_la(phba, vport);
5974                                 vport->port_state = LPFC_VPORT_READY;
5975                         }
5976                 }
5977
5978                 /* Setup and issue mailbox INITIALIZE LINK command */
5979                 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5980                 if (!initlinkmbox) {
5981                         lpfc_printf_vlog(vport, KERN_ERR,
5982                                          LOG_TRACE_EVENT,
5983                                          "0206 Device Discovery "
5984                                          "completion error\n");
5985                         phba->link_state = LPFC_HBA_ERROR;
5986                         break;
5987                 }
5988
5989                 lpfc_linkdown(phba);
5990                 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5991                                phba->cfg_link_speed);
5992                 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
5993                 initlinkmbox->vport = vport;
5994                 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5995                 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
5996                 lpfc_set_loopback_flag(phba);
5997                 if (rc == MBX_NOT_FINISHED)
5998                         mempool_free(initlinkmbox, phba->mbox_mem_pool);
5999
6000                 break;
6001
6002         case LPFC_DISC_AUTH:
6003         /* Node Authentication timeout */
6004                 lpfc_printf_vlog(vport, KERN_ERR,
6005                                  LOG_TRACE_EVENT,
6006                                  "0227 Node Authentication timeout\n");
6007                 lpfc_disc_flush_list(vport);
6008
6009                 /*
6010                  * set port_state to PORT_READY if SLI2.
6011                  * cmpl_reg_vpi will set port_state to READY for SLI3.
6012                  */
6013                 if (phba->sli_rev < LPFC_SLI_REV4) {
6014                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
6015                                 lpfc_issue_reg_vpi(phba, vport);
6016                         else  { /* NPIV Not enabled */
6017                                 lpfc_issue_clear_la(phba, vport);
6018                                 vport->port_state = LPFC_VPORT_READY;
6019                         }
6020                 }
6021                 break;
6022
6023         case LPFC_VPORT_READY:
6024                 if (vport->fc_flag & FC_RSCN_MODE) {
6025                         lpfc_printf_vlog(vport, KERN_ERR,
6026                                          LOG_TRACE_EVENT,
6027                                          "0231 RSCN timeout Data: x%x "
6028                                          "x%x\n",
6029                                          vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
6030
6031                         /* Cleanup any outstanding ELS commands */
6032                         lpfc_els_flush_cmd(vport);
6033
6034                         lpfc_els_flush_rscn(vport);
6035                         lpfc_disc_flush_list(vport);
6036                 }
6037                 break;
6038
6039         default:
6040                 lpfc_printf_vlog(vport, KERN_ERR,
6041                                  LOG_TRACE_EVENT,
6042                                  "0273 Unexpected discovery timeout, "
6043                                  "vport State x%x\n", vport->port_state);
6044                 break;
6045         }
6046
6047         switch (phba->link_state) {
6048         case LPFC_CLEAR_LA:
6049                                 /* CLEAR LA timeout */
6050                 lpfc_printf_vlog(vport, KERN_ERR,
6051                                  LOG_TRACE_EVENT,
6052                                  "0228 CLEAR LA timeout\n");
6053                 clrlaerr = 1;
6054                 break;
6055
6056         case LPFC_LINK_UP:
6057                 lpfc_issue_clear_la(phba, vport);
6058                 /* fall through */
6059         case LPFC_LINK_UNKNOWN:
6060         case LPFC_WARM_START:
6061         case LPFC_INIT_START:
6062         case LPFC_INIT_MBX_CMDS:
6063         case LPFC_LINK_DOWN:
6064         case LPFC_HBA_ERROR:
6065                 lpfc_printf_vlog(vport, KERN_ERR,
6066                                  LOG_TRACE_EVENT,
6067                                  "0230 Unexpected timeout, hba link "
6068                                  "state x%x\n", phba->link_state);
6069                 clrlaerr = 1;
6070                 break;
6071
6072         case LPFC_HBA_READY:
6073                 break;
6074         }
6075
6076         if (clrlaerr) {
6077                 lpfc_disc_flush_list(vport);
6078                 if (phba->sli_rev != LPFC_SLI_REV4) {
6079                         psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
6080                                 ~LPFC_STOP_IOCB_EVENT;
6081                         psli->sli3_ring[LPFC_FCP_RING].flag &=
6082                                 ~LPFC_STOP_IOCB_EVENT;
6083                 }
6084                 vport->port_state = LPFC_VPORT_READY;
6085         }
6086         return;
6087 }
6088
6089 /*
6090  * This routine handles processing a NameServer REG_LOGIN mailbox
6091  * command upon completion. It is setup in the LPFC_MBOXQ
6092  * as the completion routine when the command is
6093  * handed off to the SLI layer.
6094  */
6095 void
6096 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6097 {
6098         MAILBOX_t *mb = &pmb->u.mb;
6099         struct lpfc_dmabuf   *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
6100         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
6101         struct lpfc_vport    *vport = pmb->vport;
6102
6103         pmb->ctx_buf = NULL;
6104         pmb->ctx_ndlp = NULL;
6105
6106         if (phba->sli_rev < LPFC_SLI_REV4)
6107                 ndlp->nlp_rpi = mb->un.varWords[0];
6108         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
6109         ndlp->nlp_type |= NLP_FABRIC;
6110         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
6111         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
6112                          "0004 rpi:%x DID:%x flg:%x %d map:%x x%px\n",
6113                          ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
6114                          kref_read(&ndlp->kref),
6115                          ndlp->nlp_usg_map, ndlp);
6116         /*
6117          * Start issuing Fabric-Device Management Interface (FDMI) command to
6118          * 0xfffffa (FDMI well known port).
6119          * DHBA -> DPRT -> RHBA -> RPA  (physical port)
6120          * DPRT -> RPRT (vports)
6121          */
6122         if (vport->port_type == LPFC_PHYSICAL_PORT)
6123                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
6124         else
6125                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
6126
6127
6128         /* decrement the node reference count held for this callback
6129          * function.
6130          */
6131         lpfc_nlp_put(ndlp);
6132         lpfc_mbuf_free(phba, mp->virt, mp->phys);
6133         kfree(mp);
6134         mempool_free(pmb, phba->mbox_mem_pool);
6135
6136         return;
6137 }
6138
6139 static int
6140 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
6141 {
6142         uint16_t *rpi = param;
6143
6144         /* check for active node */
6145         if (!NLP_CHK_NODE_ACT(ndlp))
6146                 return 0;
6147
6148         return ndlp->nlp_rpi == *rpi;
6149 }
6150
6151 static int
6152 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
6153 {
6154         return memcmp(&ndlp->nlp_portname, param,
6155                       sizeof(ndlp->nlp_portname)) == 0;
6156 }
6157
6158 static struct lpfc_nodelist *
6159 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
6160 {
6161         struct lpfc_nodelist *ndlp;
6162
6163         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
6164                 if (filter(ndlp, param)) {
6165                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6166                                          "3185 FIND node filter %ps DID "
6167                                          "ndlp x%px did x%x flg x%x st x%x "
6168                                          "xri x%x type x%x rpi x%x\n",
6169                                          filter, ndlp, ndlp->nlp_DID,
6170                                          ndlp->nlp_flag, ndlp->nlp_state,
6171                                          ndlp->nlp_xri, ndlp->nlp_type,
6172                                          ndlp->nlp_rpi);
6173                         return ndlp;
6174                 }
6175         }
6176         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6177                          "3186 FIND node filter %ps NOT FOUND.\n", filter);
6178         return NULL;
6179 }
6180
6181 /*
6182  * This routine looks up the ndlp lists for the given RPI. If rpi found it
6183  * returns the node list element pointer else return NULL.
6184  */
6185 struct lpfc_nodelist *
6186 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6187 {
6188         return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
6189 }
6190
6191 /*
6192  * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
6193  * returns the node element list pointer else return NULL.
6194  */
6195 struct lpfc_nodelist *
6196 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
6197 {
6198         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6199         struct lpfc_nodelist *ndlp;
6200
6201         spin_lock_irq(shost->host_lock);
6202         ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
6203         spin_unlock_irq(shost->host_lock);
6204         return ndlp;
6205 }
6206
6207 /*
6208  * This routine looks up the ndlp lists for the given RPI. If the rpi
6209  * is found, the routine returns the node element list pointer else
6210  * return NULL.
6211  */
6212 struct lpfc_nodelist *
6213 lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6214 {
6215         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6216         struct lpfc_nodelist *ndlp;
6217         unsigned long flags;
6218
6219         spin_lock_irqsave(shost->host_lock, flags);
6220         ndlp = __lpfc_findnode_rpi(vport, rpi);
6221         spin_unlock_irqrestore(shost->host_lock, flags);
6222         return ndlp;
6223 }
6224
6225 /**
6226  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6227  * @phba: pointer to lpfc hba data structure.
6228  * @vpi: the physical host virtual N_Port identifier.
6229  *
6230  * This routine finds a vport on a HBA (referred by @phba) through a
6231  * @vpi. The function walks the HBA's vport list and returns the address
6232  * of the vport with the matching @vpi.
6233  *
6234  * Return code
6235  *    NULL - No vport with the matching @vpi found
6236  *    Otherwise - Address to the vport with the matching @vpi.
6237  **/
6238 struct lpfc_vport *
6239 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6240 {
6241         struct lpfc_vport *vport;
6242         unsigned long flags;
6243         int i = 0;
6244
6245         /* The physical ports are always vpi 0 - translate is unnecessary. */
6246         if (vpi > 0) {
6247                 /*
6248                  * Translate the physical vpi to the logical vpi.  The
6249                  * vport stores the logical vpi.
6250                  */
6251                 for (i = 0; i < phba->max_vpi; i++) {
6252                         if (vpi == phba->vpi_ids[i])
6253                                 break;
6254                 }
6255
6256                 if (i >= phba->max_vpi) {
6257                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6258                                         "2936 Could not find Vport mapped "
6259                                         "to vpi %d\n", vpi);
6260                         return NULL;
6261                 }
6262         }
6263
6264         spin_lock_irqsave(&phba->port_list_lock, flags);
6265         list_for_each_entry(vport, &phba->port_list, listentry) {
6266                 if (vport->vpi == i) {
6267                         spin_unlock_irqrestore(&phba->port_list_lock, flags);
6268                         return vport;
6269                 }
6270         }
6271         spin_unlock_irqrestore(&phba->port_list_lock, flags);
6272         return NULL;
6273 }
6274
6275 struct lpfc_nodelist *
6276 lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
6277 {
6278         struct lpfc_nodelist *ndlp;
6279         int rpi = LPFC_RPI_ALLOC_ERROR;
6280
6281         if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6282                 rpi = lpfc_sli4_alloc_rpi(vport->phba);
6283                 if (rpi == LPFC_RPI_ALLOC_ERROR)
6284                         return NULL;
6285         }
6286
6287         ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
6288         if (!ndlp) {
6289                 if (vport->phba->sli_rev == LPFC_SLI_REV4)
6290                         lpfc_sli4_free_rpi(vport->phba, rpi);
6291                 return NULL;
6292         }
6293
6294         memset(ndlp, 0, sizeof (struct lpfc_nodelist));
6295
6296         lpfc_initialize_node(vport, ndlp, did);
6297         INIT_LIST_HEAD(&ndlp->nlp_listp);
6298         if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6299                 ndlp->nlp_rpi = rpi;
6300                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
6301                                  "0007 Init New ndlp x%px, rpi:x%x DID:%x "
6302                                  "flg:x%x refcnt:%d map:x%x\n",
6303                                  ndlp, ndlp->nlp_rpi, ndlp->nlp_DID,
6304                                  ndlp->nlp_flag, kref_read(&ndlp->kref),
6305                                  ndlp->nlp_usg_map);
6306
6307                 ndlp->active_rrqs_xri_bitmap =
6308                                 mempool_alloc(vport->phba->active_rrq_pool,
6309                                               GFP_KERNEL);
6310                 if (ndlp->active_rrqs_xri_bitmap)
6311                         memset(ndlp->active_rrqs_xri_bitmap, 0,
6312                                ndlp->phba->cfg_rrq_xri_bitmap_sz);
6313         }
6314
6315
6316
6317         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
6318                 "node init:       did:x%x",
6319                 ndlp->nlp_DID, 0, 0);
6320
6321         return ndlp;
6322 }
6323
6324 /* This routine releases all resources associated with a specifc NPort's ndlp
6325  * and mempool_free's the nodelist.
6326  */
6327 static void
6328 lpfc_nlp_release(struct kref *kref)
6329 {
6330         struct lpfc_hba *phba;
6331         unsigned long flags;
6332         struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
6333                                                   kref);
6334
6335         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6336                 "node release:    did:x%x flg:x%x type:x%x",
6337                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
6338
6339         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
6340                         "0279 %s: ndlp:x%px did %x "
6341                         "usgmap:x%x refcnt:%d rpi:%x\n",
6342                         __func__,
6343                         (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
6344                         kref_read(&ndlp->kref), ndlp->nlp_rpi);
6345
6346         /* remove ndlp from action. */
6347         lpfc_nlp_remove(ndlp->vport, ndlp);
6348
6349         /* clear the ndlp active flag for all release cases */
6350         phba = ndlp->phba;
6351         spin_lock_irqsave(&phba->ndlp_lock, flags);
6352         NLP_CLR_NODE_ACT(ndlp);
6353         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6354
6355         /* free ndlp memory for final ndlp release */
6356         if (NLP_CHK_FREE_REQ(ndlp)) {
6357                 kfree(ndlp->lat_data);
6358                 if (phba->sli_rev == LPFC_SLI_REV4)
6359                         mempool_free(ndlp->active_rrqs_xri_bitmap,
6360                                      ndlp->phba->active_rrq_pool);
6361                 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
6362         }
6363 }
6364
6365 /* This routine bumps the reference count for a ndlp structure to ensure
6366  * that one discovery thread won't free a ndlp while another discovery thread
6367  * is using it.
6368  */
6369 struct lpfc_nodelist *
6370 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6371 {
6372         struct lpfc_hba *phba;
6373         unsigned long flags;
6374
6375         if (ndlp) {
6376                 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6377                         "node get:        did:x%x flg:x%x refcnt:x%x",
6378                         ndlp->nlp_DID, ndlp->nlp_flag,
6379                         kref_read(&ndlp->kref));
6380                 /* The check of ndlp usage to prevent incrementing the
6381                  * ndlp reference count that is in the process of being
6382                  * released.
6383                  */
6384                 phba = ndlp->phba;
6385                 spin_lock_irqsave(&phba->ndlp_lock, flags);
6386                 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
6387                         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6388                         lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6389                                 "0276 %s: ndlp:x%px "
6390                                 "usgmap:x%x refcnt:%d\n",
6391                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
6392                                 kref_read(&ndlp->kref));
6393                         return NULL;
6394                 } else
6395                         kref_get(&ndlp->kref);
6396                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6397         }
6398         return ndlp;
6399 }
6400
6401 /* This routine decrements the reference count for a ndlp structure. If the
6402  * count goes to 0, this indicates the the associated nodelist should be
6403  * freed. Returning 1 indicates the ndlp resource has been released; on the
6404  * other hand, returning 0 indicates the ndlp resource has not been released
6405  * yet.
6406  */
6407 int
6408 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6409 {
6410         struct lpfc_hba *phba;
6411         unsigned long flags;
6412
6413         if (!ndlp)
6414                 return 1;
6415
6416         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6417                         "node put:        did:x%x flg:x%x refcnt:x%x",
6418                         ndlp->nlp_DID, ndlp->nlp_flag,
6419                         kref_read(&ndlp->kref));
6420         phba = ndlp->phba;
6421         spin_lock_irqsave(&phba->ndlp_lock, flags);
6422         /* Check the ndlp memory free acknowledge flag to avoid the
6423          * possible race condition that kref_put got invoked again
6424          * after previous one has done ndlp memory free.
6425          */
6426         if (NLP_CHK_FREE_ACK(ndlp)) {
6427                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6428                 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6429                                 "0274 %s: ndlp:x%px "
6430                                 "usgmap:x%x refcnt:%d\n",
6431                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
6432                                 kref_read(&ndlp->kref));
6433                 return 1;
6434         }
6435         /* Check the ndlp inactivate log flag to avoid the possible
6436          * race condition that kref_put got invoked again after ndlp
6437          * is already in inactivating state.
6438          */
6439         if (NLP_CHK_IACT_REQ(ndlp)) {
6440                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6441                 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6442                                 "0275 %s: ndlp:x%px "
6443                                 "usgmap:x%x refcnt:%d\n",
6444                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
6445                                 kref_read(&ndlp->kref));
6446                 return 1;
6447         }
6448         /* For last put, mark the ndlp usage flags to make sure no
6449          * other kref_get and kref_put on the same ndlp shall get
6450          * in between the process when the final kref_put has been
6451          * invoked on this ndlp.
6452          */
6453         if (kref_read(&ndlp->kref) == 1) {
6454                 /* Indicate ndlp is put to inactive state. */
6455                 NLP_SET_IACT_REQ(ndlp);
6456                 /* Acknowledge ndlp memory free has been seen. */
6457                 if (NLP_CHK_FREE_REQ(ndlp))
6458                         NLP_SET_FREE_ACK(ndlp);
6459         }
6460         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6461         /* Note, the kref_put returns 1 when decrementing a reference
6462          * count that was 1, it invokes the release callback function,
6463          * but it still left the reference count as 1 (not actually
6464          * performs the last decrementation). Otherwise, it actually
6465          * decrements the reference count and returns 0.
6466          */
6467         return kref_put(&ndlp->kref, lpfc_nlp_release);
6468 }
6469
6470 /* This routine free's the specified nodelist if it is not in use
6471  * by any other discovery thread. This routine returns 1 if the
6472  * ndlp has been freed. A return value of 0 indicates the ndlp is
6473  * not yet been released.
6474  */
6475 int
6476 lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6477 {
6478         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6479                 "node not used:   did:x%x flg:x%x refcnt:x%x",
6480                 ndlp->nlp_DID, ndlp->nlp_flag,
6481                 kref_read(&ndlp->kref));
6482         if (kref_read(&ndlp->kref) == 1)
6483                 if (lpfc_nlp_put(ndlp))
6484                         return 1;
6485         return 0;
6486 }
6487
6488 /**
6489  * lpfc_fcf_inuse - Check if FCF can be unregistered.
6490  * @phba: Pointer to hba context object.
6491  *
6492  * This function iterate through all FC nodes associated
6493  * will all vports to check if there is any node with
6494  * fc_rports associated with it. If there is an fc_rport
6495  * associated with the node, then the node is either in
6496  * discovered state or its devloss_timer is pending.
6497  */
6498 static int
6499 lpfc_fcf_inuse(struct lpfc_hba *phba)
6500 {
6501         struct lpfc_vport **vports;
6502         int i, ret = 0;
6503         struct lpfc_nodelist *ndlp;
6504         struct Scsi_Host  *shost;
6505
6506         vports = lpfc_create_vport_work_array(phba);
6507
6508         /* If driver cannot allocate memory, indicate fcf is in use */
6509         if (!vports)
6510                 return 1;
6511
6512         for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6513                 shost = lpfc_shost_from_vport(vports[i]);
6514                 spin_lock_irq(shost->host_lock);
6515                 /*
6516                  * IF the CVL_RCVD bit is not set then we have sent the
6517                  * flogi.
6518                  * If dev_loss fires while we are waiting we do not want to
6519                  * unreg the fcf.
6520                  */
6521                 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6522                         spin_unlock_irq(shost->host_lock);
6523                         ret =  1;
6524                         goto out;
6525                 }
6526                 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6527                         if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6528                           (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6529                                 ret = 1;
6530                                 spin_unlock_irq(shost->host_lock);
6531                                 goto out;
6532                         } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6533                                 ret = 1;
6534                                 lpfc_printf_log(phba, KERN_INFO,
6535                                                 LOG_NODE | LOG_DISCOVERY,
6536                                                 "2624 RPI %x DID %x flag %x "
6537                                                 "still logged in\n",
6538                                                 ndlp->nlp_rpi, ndlp->nlp_DID,
6539                                                 ndlp->nlp_flag);
6540                         }
6541                 }
6542                 spin_unlock_irq(shost->host_lock);
6543         }
6544 out:
6545         lpfc_destroy_vport_work_array(phba, vports);
6546         return ret;
6547 }
6548
6549 /**
6550  * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6551  * @phba: Pointer to hba context object.
6552  * @mboxq: Pointer to mailbox object.
6553  *
6554  * This function frees memory associated with the mailbox command.
6555  */
6556 void
6557 lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6558 {
6559         struct lpfc_vport *vport = mboxq->vport;
6560         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6561
6562         if (mboxq->u.mb.mbxStatus) {
6563                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6564                                 "2555 UNREG_VFI mbxStatus error x%x "
6565                                 "HBA state x%x\n",
6566                                 mboxq->u.mb.mbxStatus, vport->port_state);
6567         }
6568         spin_lock_irq(shost->host_lock);
6569         phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6570         spin_unlock_irq(shost->host_lock);
6571         mempool_free(mboxq, phba->mbox_mem_pool);
6572         return;
6573 }
6574
6575 /**
6576  * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6577  * @phba: Pointer to hba context object.
6578  * @mboxq: Pointer to mailbox object.
6579  *
6580  * This function frees memory associated with the mailbox command.
6581  */
6582 static void
6583 lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6584 {
6585         struct lpfc_vport *vport = mboxq->vport;
6586
6587         if (mboxq->u.mb.mbxStatus) {
6588                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6589                                 "2550 UNREG_FCFI mbxStatus error x%x "
6590                                 "HBA state x%x\n",
6591                                 mboxq->u.mb.mbxStatus, vport->port_state);
6592         }
6593         mempool_free(mboxq, phba->mbox_mem_pool);
6594         return;
6595 }
6596
6597 /**
6598  * lpfc_unregister_fcf_prep - Unregister fcf record preparation
6599  * @phba: Pointer to hba context object.
6600  *
6601  * This function prepare the HBA for unregistering the currently registered
6602  * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6603  * VFIs.
6604  */
6605 int
6606 lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
6607 {
6608         struct lpfc_vport **vports;
6609         struct lpfc_nodelist *ndlp;
6610         struct Scsi_Host *shost;
6611         int i = 0, rc;
6612
6613         /* Unregister RPIs */
6614         if (lpfc_fcf_inuse(phba))
6615                 lpfc_unreg_hba_rpis(phba);
6616
6617         /* At this point, all discovery is aborted */
6618         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
6619
6620         /* Unregister VPIs */
6621         vports = lpfc_create_vport_work_array(phba);
6622         if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
6623                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6624                         /* Stop FLOGI/FDISC retries */
6625                         ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6626                         if (ndlp)
6627                                 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
6628                         lpfc_cleanup_pending_mbox(vports[i]);
6629                         if (phba->sli_rev == LPFC_SLI_REV4)
6630                                 lpfc_sli4_unreg_all_rpis(vports[i]);
6631                         lpfc_mbx_unreg_vpi(vports[i]);
6632                         shost = lpfc_shost_from_vport(vports[i]);
6633                         spin_lock_irq(shost->host_lock);
6634                         vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6635                         vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
6636                         spin_unlock_irq(shost->host_lock);
6637                 }
6638         lpfc_destroy_vport_work_array(phba, vports);
6639         if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6640                 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6641                 if (ndlp)
6642                         lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6643                 lpfc_cleanup_pending_mbox(phba->pport);
6644                 if (phba->sli_rev == LPFC_SLI_REV4)
6645                         lpfc_sli4_unreg_all_rpis(phba->pport);
6646                 lpfc_mbx_unreg_vpi(phba->pport);
6647                 shost = lpfc_shost_from_vport(phba->pport);
6648                 spin_lock_irq(shost->host_lock);
6649                 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6650                 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6651                 spin_unlock_irq(shost->host_lock);
6652         }
6653
6654         /* Cleanup any outstanding ELS commands */
6655         lpfc_els_flush_all_cmd(phba);
6656
6657         /* Unregister the physical port VFI */
6658         rc = lpfc_issue_unreg_vfi(phba->pport);
6659         return rc;
6660 }
6661
6662 /**
6663  * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6664  * @phba: Pointer to hba context object.
6665  *
6666  * This function issues synchronous unregister FCF mailbox command to HBA to
6667  * unregister the currently registered FCF record. The driver does not reset
6668  * the driver FCF usage state flags.
6669  *
6670  * Return 0 if successfully issued, none-zero otherwise.
6671  */
6672 int
6673 lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6674 {
6675         LPFC_MBOXQ_t *mbox;
6676         int rc;
6677
6678         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6679         if (!mbox) {
6680                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6681                                 "2551 UNREG_FCFI mbox allocation failed"
6682                                 "HBA state x%x\n", phba->pport->port_state);
6683                 return -ENOMEM;
6684         }
6685         lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6686         mbox->vport = phba->pport;
6687         mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6688         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6689
6690         if (rc == MBX_NOT_FINISHED) {
6691                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6692                                 "2552 Unregister FCFI command failed rc x%x "
6693                                 "HBA state x%x\n",
6694                                 rc, phba->pport->port_state);
6695                 return -EINVAL;
6696         }
6697         return 0;
6698 }
6699
6700 /**
6701  * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6702  * @phba: Pointer to hba context object.
6703  *
6704  * This function unregisters the currently reigstered FCF. This function
6705  * also tries to find another FCF for discovery by rescan the HBA FCF table.
6706  */
6707 void
6708 lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6709 {
6710         int rc;
6711
6712         /* Preparation for unregistering fcf */
6713         rc = lpfc_unregister_fcf_prep(phba);
6714         if (rc) {
6715                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6716                                 "2748 Failed to prepare for unregistering "
6717                                 "HBA's FCF record: rc=%d\n", rc);
6718                 return;
6719         }
6720
6721         /* Now, unregister FCF record and reset HBA FCF state */
6722         rc = lpfc_sli4_unregister_fcf(phba);
6723         if (rc)
6724                 return;
6725         /* Reset HBA FCF states after successful unregister FCF */
6726         phba->fcf.fcf_flag = 0;
6727         phba->fcf.current_rec.flag = 0;
6728
6729         /*
6730          * If driver is not unloading, check if there is any other
6731          * FCF record that can be used for discovery.
6732          */
6733         if ((phba->pport->load_flag & FC_UNLOADING) ||
6734             (phba->link_state < LPFC_LINK_UP))
6735                 return;
6736
6737         /* This is considered as the initial FCF discovery scan */
6738         spin_lock_irq(&phba->hbalock);
6739         phba->fcf.fcf_flag |= FCF_INIT_DISC;
6740         spin_unlock_irq(&phba->hbalock);
6741
6742         /* Reset FCF roundrobin bmask for new discovery */
6743         lpfc_sli4_clear_fcf_rr_bmask(phba);
6744
6745         rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
6746
6747         if (rc) {
6748                 spin_lock_irq(&phba->hbalock);
6749                 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6750                 spin_unlock_irq(&phba->hbalock);
6751                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6752                                 "2553 lpfc_unregister_unused_fcf failed "
6753                                 "to read FCF record HBA state x%x\n",
6754                                 phba->pport->port_state);
6755         }
6756 }
6757
6758 /**
6759  * lpfc_unregister_fcf - Unregister the currently registered fcf record
6760  * @phba: Pointer to hba context object.
6761  *
6762  * This function just unregisters the currently reigstered FCF. It does not
6763  * try to find another FCF for discovery.
6764  */
6765 void
6766 lpfc_unregister_fcf(struct lpfc_hba *phba)
6767 {
6768         int rc;
6769
6770         /* Preparation for unregistering fcf */
6771         rc = lpfc_unregister_fcf_prep(phba);
6772         if (rc) {
6773                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6774                                 "2749 Failed to prepare for unregistering "
6775                                 "HBA's FCF record: rc=%d\n", rc);
6776                 return;
6777         }
6778
6779         /* Now, unregister FCF record and reset HBA FCF state */
6780         rc = lpfc_sli4_unregister_fcf(phba);
6781         if (rc)
6782                 return;
6783         /* Set proper HBA FCF states after successful unregister FCF */
6784         spin_lock_irq(&phba->hbalock);
6785         phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6786         spin_unlock_irq(&phba->hbalock);
6787 }
6788
6789 /**
6790  * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6791  * @phba: Pointer to hba context object.
6792  *
6793  * This function check if there are any connected remote port for the FCF and
6794  * if all the devices are disconnected, this function unregister FCFI.
6795  * This function also tries to use another FCF for discovery.
6796  */
6797 void
6798 lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6799 {
6800         /*
6801          * If HBA is not running in FIP mode, if HBA does not support
6802          * FCoE, if FCF discovery is ongoing, or if FCF has not been
6803          * registered, do nothing.
6804          */
6805         spin_lock_irq(&phba->hbalock);
6806         if (!(phba->hba_flag & HBA_FCOE_MODE) ||
6807             !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
6808             !(phba->hba_flag & HBA_FIP_SUPPORT) ||
6809             (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
6810             (phba->pport->port_state == LPFC_FLOGI)) {
6811                 spin_unlock_irq(&phba->hbalock);
6812                 return;
6813         }
6814         spin_unlock_irq(&phba->hbalock);
6815
6816         if (lpfc_fcf_inuse(phba))
6817                 return;
6818
6819         lpfc_unregister_fcf_rescan(phba);
6820 }
6821
6822 /**
6823  * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6824  * @phba: Pointer to hba context object.
6825  * @buff: Buffer containing the FCF connection table as in the config
6826  *         region.
6827  * This function create driver data structure for the FCF connection
6828  * record table read from config region 23.
6829  */
6830 static void
6831 lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6832         uint8_t *buff)
6833 {
6834         struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6835         struct lpfc_fcf_conn_hdr *conn_hdr;
6836         struct lpfc_fcf_conn_rec *conn_rec;
6837         uint32_t record_count;
6838         int i;
6839
6840         /* Free the current connect table */
6841         list_for_each_entry_safe(conn_entry, next_conn_entry,
6842                 &phba->fcf_conn_rec_list, list) {
6843                 list_del_init(&conn_entry->list);
6844                 kfree(conn_entry);
6845         }
6846
6847         conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6848         record_count = conn_hdr->length * sizeof(uint32_t)/
6849                 sizeof(struct lpfc_fcf_conn_rec);
6850
6851         conn_rec = (struct lpfc_fcf_conn_rec *)
6852                 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6853
6854         for (i = 0; i < record_count; i++) {
6855                 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6856                         continue;
6857                 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6858                         GFP_KERNEL);
6859                 if (!conn_entry) {
6860                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6861                                         "2566 Failed to allocate connection"
6862                                         " table entry\n");
6863                         return;
6864                 }
6865
6866                 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6867                         sizeof(struct lpfc_fcf_conn_rec));
6868                 list_add_tail(&conn_entry->list,
6869                         &phba->fcf_conn_rec_list);
6870         }
6871
6872         if (!list_empty(&phba->fcf_conn_rec_list)) {
6873                 i = 0;
6874                 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6875                                     list) {
6876                         conn_rec = &conn_entry->conn_rec;
6877                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6878                                         "3345 FCF connection list rec[%02d]: "
6879                                         "flags:x%04x, vtag:x%04x, "
6880                                         "fabric_name:x%02x:%02x:%02x:%02x:"
6881                                         "%02x:%02x:%02x:%02x, "
6882                                         "switch_name:x%02x:%02x:%02x:%02x:"
6883                                         "%02x:%02x:%02x:%02x\n", i++,
6884                                         conn_rec->flags, conn_rec->vlan_tag,
6885                                         conn_rec->fabric_name[0],
6886                                         conn_rec->fabric_name[1],
6887                                         conn_rec->fabric_name[2],
6888                                         conn_rec->fabric_name[3],
6889                                         conn_rec->fabric_name[4],
6890                                         conn_rec->fabric_name[5],
6891                                         conn_rec->fabric_name[6],
6892                                         conn_rec->fabric_name[7],
6893                                         conn_rec->switch_name[0],
6894                                         conn_rec->switch_name[1],
6895                                         conn_rec->switch_name[2],
6896                                         conn_rec->switch_name[3],
6897                                         conn_rec->switch_name[4],
6898                                         conn_rec->switch_name[5],
6899                                         conn_rec->switch_name[6],
6900                                         conn_rec->switch_name[7]);
6901                 }
6902         }
6903 }
6904
6905 /**
6906  * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6907  * @phba: Pointer to hba context object.
6908  * @buff: Buffer containing the FCoE parameter data structure.
6909  *
6910  *  This function update driver data structure with config
6911  *  parameters read from config region 23.
6912  */
6913 static void
6914 lpfc_read_fcoe_param(struct lpfc_hba *phba,
6915                         uint8_t *buff)
6916 {
6917         struct lpfc_fip_param_hdr *fcoe_param_hdr;
6918         struct lpfc_fcoe_params *fcoe_param;
6919
6920         fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6921                 buff;
6922         fcoe_param = (struct lpfc_fcoe_params *)
6923                 (buff + sizeof(struct lpfc_fip_param_hdr));
6924
6925         if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6926                 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6927                 return;
6928
6929         if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6930                 phba->valid_vlan = 1;
6931                 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6932                         0xFFF;
6933         }
6934
6935         phba->fc_map[0] = fcoe_param->fc_map[0];
6936         phba->fc_map[1] = fcoe_param->fc_map[1];
6937         phba->fc_map[2] = fcoe_param->fc_map[2];
6938         return;
6939 }
6940
6941 /**
6942  * lpfc_get_rec_conf23 - Get a record type in config region data.
6943  * @buff: Buffer containing config region 23 data.
6944  * @size: Size of the data buffer.
6945  * @rec_type: Record type to be searched.
6946  *
6947  * This function searches config region data to find the beginning
6948  * of the record specified by record_type. If record found, this
6949  * function return pointer to the record else return NULL.
6950  */
6951 static uint8_t *
6952 lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6953 {
6954         uint32_t offset = 0, rec_length;
6955
6956         if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6957                 (size < sizeof(uint32_t)))
6958                 return NULL;
6959
6960         rec_length = buff[offset + 1];
6961
6962         /*
6963          * One TLV record has one word header and number of data words
6964          * specified in the rec_length field of the record header.
6965          */
6966         while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6967                 <= size) {
6968                 if (buff[offset] == rec_type)
6969                         return &buff[offset];
6970
6971                 if (buff[offset] == LPFC_REGION23_LAST_REC)
6972                         return NULL;
6973
6974                 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6975                 rec_length = buff[offset + 1];
6976         }
6977         return NULL;
6978 }
6979
6980 /**
6981  * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6982  * @phba: Pointer to lpfc_hba data structure.
6983  * @buff: Buffer containing config region 23 data.
6984  * @size: Size of the data buffer.
6985  *
6986  * This function parses the FCoE config parameters in config region 23 and
6987  * populate driver data structure with the parameters.
6988  */
6989 void
6990 lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6991                 uint8_t *buff,
6992                 uint32_t size)
6993 {
6994         uint32_t offset = 0;
6995         uint8_t *rec_ptr;
6996
6997         /*
6998          * If data size is less than 2 words signature and version cannot be
6999          * verified.
7000          */
7001         if (size < 2*sizeof(uint32_t))
7002                 return;
7003
7004         /* Check the region signature first */
7005         if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
7006                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7007                         "2567 Config region 23 has bad signature\n");
7008                 return;
7009         }
7010
7011         offset += 4;
7012
7013         /* Check the data structure version */
7014         if (buff[offset] != LPFC_REGION23_VERSION) {
7015                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7016                                 "2568 Config region 23 has bad version\n");
7017                 return;
7018         }
7019         offset += 4;
7020
7021         /* Read FCoE param record */
7022         rec_ptr = lpfc_get_rec_conf23(&buff[offset],
7023                         size - offset, FCOE_PARAM_TYPE);
7024         if (rec_ptr)
7025                 lpfc_read_fcoe_param(phba, rec_ptr);
7026
7027         /* Read FCF connection table */
7028         rec_ptr = lpfc_get_rec_conf23(&buff[offset],
7029                 size - offset, FCOE_CONN_TBL_TYPE);
7030         if (rec_ptr)
7031                 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
7032
7033 }
This page took 0.454881 seconds and 4 git commands to generate.