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