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