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