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