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