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