Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_hbadisc.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/kthread.h>
25 #include <linux/interrupt.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
31
32 #include "lpfc_hw4.h"
33 #include "lpfc_hw.h"
34 #include "lpfc_nl.h"
35 #include "lpfc_disc.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_vport.h"
43 #include "lpfc_debugfs.h"
44
45 /* AlpaArray for assignment of scsid for scan-down and bind_method */
46 static uint8_t lpfcAlpaArray[] = {
47         0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
48         0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
49         0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
50         0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
51         0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
52         0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
53         0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
54         0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
55         0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
56         0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
57         0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
58         0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
59         0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
60 };
61
62 static void lpfc_disc_timeout_handler(struct lpfc_vport *);
63 static void lpfc_disc_flush_list(struct lpfc_vport *vport);
64 static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
65
66 void
67 lpfc_terminate_rport_io(struct fc_rport *rport)
68 {
69         struct lpfc_rport_data *rdata;
70         struct lpfc_nodelist * ndlp;
71         struct lpfc_hba *phba;
72
73         rdata = rport->dd_data;
74         ndlp = rdata->pnode;
75
76         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
77                 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
78                         printk(KERN_ERR "Cannot find remote node"
79                         " to terminate I/O Data x%x\n",
80                         rport->port_id);
81                 return;
82         }
83
84         phba  = ndlp->phba;
85
86         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
87                 "rport terminate: sid:x%x did:x%x flg:x%x",
88                 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
89
90         if (ndlp->nlp_sid != NLP_NO_SID) {
91                 lpfc_sli_abort_iocb(ndlp->vport,
92                         &phba->sli.ring[phba->sli.fcp_ring],
93                         ndlp->nlp_sid, 0, LPFC_CTX_TGT);
94         }
95 }
96
97 /*
98  * This function will be called when dev_loss_tmo fire.
99  */
100 void
101 lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
102 {
103         struct lpfc_rport_data *rdata;
104         struct lpfc_nodelist * ndlp;
105         struct lpfc_vport *vport;
106         struct lpfc_hba   *phba;
107         struct lpfc_work_evt *evtp;
108         int  put_node;
109         int  put_rport;
110
111         rdata = rport->dd_data;
112         ndlp = rdata->pnode;
113         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
114                 return;
115
116         vport = ndlp->vport;
117         phba  = vport->phba;
118
119         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
120                 "rport devlosscb: sid:x%x did:x%x flg:x%x",
121                 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
122
123         /* Don't defer this if we are in the process of deleting the vport
124          * or unloading the driver. The unload will cleanup the node
125          * appropriately we just need to cleanup the ndlp rport info here.
126          */
127         if (vport->load_flag & FC_UNLOADING) {
128                 put_node = rdata->pnode != NULL;
129                 put_rport = ndlp->rport != NULL;
130                 rdata->pnode = NULL;
131                 ndlp->rport = NULL;
132                 if (put_node)
133                         lpfc_nlp_put(ndlp);
134                 if (put_rport)
135                         put_device(&rport->dev);
136                 return;
137         }
138
139         if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
140                 return;
141
142         evtp = &ndlp->dev_loss_evt;
143
144         if (!list_empty(&evtp->evt_listp))
145                 return;
146
147         spin_lock_irq(&phba->hbalock);
148         /* We need to hold the node by incrementing the reference
149          * count until this queued work is done
150          */
151         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
152         if (evtp->evt_arg1) {
153                 evtp->evt = LPFC_EVT_DEV_LOSS;
154                 list_add_tail(&evtp->evt_listp, &phba->work_list);
155                 lpfc_worker_wake_up(phba);
156         }
157         spin_unlock_irq(&phba->hbalock);
158
159         return;
160 }
161
162 /*
163  * This function is called from the worker thread when dev_loss_tmo
164  * expire.
165  */
166 static void
167 lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
168 {
169         struct lpfc_rport_data *rdata;
170         struct fc_rport   *rport;
171         struct lpfc_vport *vport;
172         struct lpfc_hba   *phba;
173         uint8_t *name;
174         int  put_node;
175         int  put_rport;
176         int warn_on = 0;
177
178         rport = ndlp->rport;
179
180         if (!rport)
181                 return;
182
183         rdata = rport->dd_data;
184         name = (uint8_t *) &ndlp->nlp_portname;
185         vport = ndlp->vport;
186         phba  = vport->phba;
187
188         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
189                 "rport devlosstmo:did:x%x type:x%x id:x%x",
190                 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
191
192         /* Don't defer this if we are in the process of deleting the vport
193          * or unloading the driver. The unload will cleanup the node
194          * appropriately we just need to cleanup the ndlp rport info here.
195          */
196         if (vport->load_flag & FC_UNLOADING) {
197                 if (ndlp->nlp_sid != NLP_NO_SID) {
198                         /* flush the target */
199                         lpfc_sli_abort_iocb(vport,
200                                         &phba->sli.ring[phba->sli.fcp_ring],
201                                         ndlp->nlp_sid, 0, LPFC_CTX_TGT);
202                 }
203                 put_node = rdata->pnode != NULL;
204                 put_rport = ndlp->rport != NULL;
205                 rdata->pnode = NULL;
206                 ndlp->rport = NULL;
207                 if (put_node)
208                         lpfc_nlp_put(ndlp);
209                 if (put_rport)
210                         put_device(&rport->dev);
211                 return;
212         }
213
214         if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
215                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
216                                  "0284 Devloss timeout Ignored on "
217                                  "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
218                                  "NPort x%x\n",
219                                  *name, *(name+1), *(name+2), *(name+3),
220                                  *(name+4), *(name+5), *(name+6), *(name+7),
221                                  ndlp->nlp_DID);
222                 return;
223         }
224
225         if (ndlp->nlp_type & NLP_FABRIC) {
226                 /* We will clean up these Nodes in linkup */
227                 put_node = rdata->pnode != NULL;
228                 put_rport = ndlp->rport != NULL;
229                 rdata->pnode = NULL;
230                 ndlp->rport = NULL;
231                 if (put_node)
232                         lpfc_nlp_put(ndlp);
233                 if (put_rport)
234                         put_device(&rport->dev);
235                 return;
236         }
237
238         if (ndlp->nlp_sid != NLP_NO_SID) {
239                 warn_on = 1;
240                 /* flush the target */
241                 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
242                                     ndlp->nlp_sid, 0, LPFC_CTX_TGT);
243         }
244
245         if (warn_on) {
246                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
247                                  "0203 Devloss timeout on "
248                                  "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
249                                  "NPort x%06x Data: x%x x%x x%x\n",
250                                  *name, *(name+1), *(name+2), *(name+3),
251                                  *(name+4), *(name+5), *(name+6), *(name+7),
252                                  ndlp->nlp_DID, ndlp->nlp_flag,
253                                  ndlp->nlp_state, ndlp->nlp_rpi);
254         } else {
255                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
256                                  "0204 Devloss timeout on "
257                                  "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
258                                  "NPort x%06x Data: x%x x%x x%x\n",
259                                  *name, *(name+1), *(name+2), *(name+3),
260                                  *(name+4), *(name+5), *(name+6), *(name+7),
261                                  ndlp->nlp_DID, ndlp->nlp_flag,
262                                  ndlp->nlp_state, ndlp->nlp_rpi);
263         }
264
265         put_node = rdata->pnode != NULL;
266         put_rport = ndlp->rport != NULL;
267         rdata->pnode = NULL;
268         ndlp->rport = NULL;
269         if (put_node)
270                 lpfc_nlp_put(ndlp);
271         if (put_rport)
272                 put_device(&rport->dev);
273
274         if (!(vport->load_flag & FC_UNLOADING) &&
275             !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
276             !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
277             (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
278                 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
279
280         lpfc_unregister_unused_fcf(phba);
281 }
282
283 /**
284  * lpfc_alloc_fast_evt - Allocates data structure for posting event
285  * @phba: Pointer to hba context object.
286  *
287  * This function is called from the functions which need to post
288  * events from interrupt context. This function allocates data
289  * structure required for posting event. It also keeps track of
290  * number of events pending and prevent event storm when there are
291  * too many events.
292  **/
293 struct lpfc_fast_path_event *
294 lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
295         struct lpfc_fast_path_event *ret;
296
297         /* If there are lot of fast event do not exhaust memory due to this */
298         if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
299                 return NULL;
300
301         ret = kzalloc(sizeof(struct lpfc_fast_path_event),
302                         GFP_ATOMIC);
303         if (ret) {
304                 atomic_inc(&phba->fast_event_count);
305                 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
306                 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
307         }
308         return ret;
309 }
310
311 /**
312  * lpfc_free_fast_evt - Frees event data structure
313  * @phba: Pointer to hba context object.
314  * @evt:  Event object which need to be freed.
315  *
316  * This function frees the data structure required for posting
317  * events.
318  **/
319 void
320 lpfc_free_fast_evt(struct lpfc_hba *phba,
321                 struct lpfc_fast_path_event *evt) {
322
323         atomic_dec(&phba->fast_event_count);
324         kfree(evt);
325 }
326
327 /**
328  * lpfc_send_fastpath_evt - Posts events generated from fast path
329  * @phba: Pointer to hba context object.
330  * @evtp: Event data structure.
331  *
332  * This function is called from worker thread, when the interrupt
333  * context need to post an event. This function posts the event
334  * to fc transport netlink interface.
335  **/
336 static void
337 lpfc_send_fastpath_evt(struct lpfc_hba *phba,
338                 struct lpfc_work_evt *evtp)
339 {
340         unsigned long evt_category, evt_sub_category;
341         struct lpfc_fast_path_event *fast_evt_data;
342         char *evt_data;
343         uint32_t evt_data_size;
344         struct Scsi_Host *shost;
345
346         fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
347                 work_evt);
348
349         evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
350         evt_sub_category = (unsigned long) fast_evt_data->un.
351                         fabric_evt.subcategory;
352         shost = lpfc_shost_from_vport(fast_evt_data->vport);
353         if (evt_category == FC_REG_FABRIC_EVENT) {
354                 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
355                         evt_data = (char *) &fast_evt_data->un.read_check_error;
356                         evt_data_size = sizeof(fast_evt_data->un.
357                                 read_check_error);
358                 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
359                         (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
360                         evt_data = (char *) &fast_evt_data->un.fabric_evt;
361                         evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
362                 } else {
363                         lpfc_free_fast_evt(phba, fast_evt_data);
364                         return;
365                 }
366         } else if (evt_category == FC_REG_SCSI_EVENT) {
367                 switch (evt_sub_category) {
368                 case LPFC_EVENT_QFULL:
369                 case LPFC_EVENT_DEVBSY:
370                         evt_data = (char *) &fast_evt_data->un.scsi_evt;
371                         evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
372                         break;
373                 case LPFC_EVENT_CHECK_COND:
374                         evt_data = (char *) &fast_evt_data->un.check_cond_evt;
375                         evt_data_size =  sizeof(fast_evt_data->un.
376                                 check_cond_evt);
377                         break;
378                 case LPFC_EVENT_VARQUEDEPTH:
379                         evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
380                         evt_data_size = sizeof(fast_evt_data->un.
381                                 queue_depth_evt);
382                         break;
383                 default:
384                         lpfc_free_fast_evt(phba, fast_evt_data);
385                         return;
386                 }
387         } else {
388                 lpfc_free_fast_evt(phba, fast_evt_data);
389                 return;
390         }
391
392         fc_host_post_vendor_event(shost,
393                 fc_get_event_number(),
394                 evt_data_size,
395                 evt_data,
396                 LPFC_NL_VENDOR_ID);
397
398         lpfc_free_fast_evt(phba, fast_evt_data);
399         return;
400 }
401
402 static void
403 lpfc_work_list_done(struct lpfc_hba *phba)
404 {
405         struct lpfc_work_evt  *evtp = NULL;
406         struct lpfc_nodelist  *ndlp;
407         int free_evt;
408
409         spin_lock_irq(&phba->hbalock);
410         while (!list_empty(&phba->work_list)) {
411                 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
412                                  evt_listp);
413                 spin_unlock_irq(&phba->hbalock);
414                 free_evt = 1;
415                 switch (evtp->evt) {
416                 case LPFC_EVT_ELS_RETRY:
417                         ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
418                         lpfc_els_retry_delay_handler(ndlp);
419                         free_evt = 0; /* evt is part of ndlp */
420                         /* decrement the node reference count held
421                          * for this queued work
422                          */
423                         lpfc_nlp_put(ndlp);
424                         break;
425                 case LPFC_EVT_DEV_LOSS:
426                         ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
427                         lpfc_dev_loss_tmo_handler(ndlp);
428                         free_evt = 0;
429                         /* decrement the node reference count held for
430                          * this queued work
431                          */
432                         lpfc_nlp_put(ndlp);
433                         break;
434                 case LPFC_EVT_ONLINE:
435                         if (phba->link_state < LPFC_LINK_DOWN)
436                                 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
437                         else
438                                 *(int *) (evtp->evt_arg1) = 0;
439                         complete((struct completion *)(evtp->evt_arg2));
440                         break;
441                 case LPFC_EVT_OFFLINE_PREP:
442                         if (phba->link_state >= LPFC_LINK_DOWN)
443                                 lpfc_offline_prep(phba);
444                         *(int *)(evtp->evt_arg1) = 0;
445                         complete((struct completion *)(evtp->evt_arg2));
446                         break;
447                 case LPFC_EVT_OFFLINE:
448                         lpfc_offline(phba);
449                         lpfc_sli_brdrestart(phba);
450                         *(int *)(evtp->evt_arg1) =
451                                 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
452                         lpfc_unblock_mgmt_io(phba);
453                         complete((struct completion *)(evtp->evt_arg2));
454                         break;
455                 case LPFC_EVT_WARM_START:
456                         lpfc_offline(phba);
457                         lpfc_reset_barrier(phba);
458                         lpfc_sli_brdreset(phba);
459                         lpfc_hba_down_post(phba);
460                         *(int *)(evtp->evt_arg1) =
461                                 lpfc_sli_brdready(phba, HS_MBRDY);
462                         lpfc_unblock_mgmt_io(phba);
463                         complete((struct completion *)(evtp->evt_arg2));
464                         break;
465                 case LPFC_EVT_KILL:
466                         lpfc_offline(phba);
467                         *(int *)(evtp->evt_arg1)
468                                 = (phba->pport->stopped)
469                                         ? 0 : lpfc_sli_brdkill(phba);
470                         lpfc_unblock_mgmt_io(phba);
471                         complete((struct completion *)(evtp->evt_arg2));
472                         break;
473                 case LPFC_EVT_FASTPATH_MGMT_EVT:
474                         lpfc_send_fastpath_evt(phba, evtp);
475                         free_evt = 0;
476                         break;
477                 }
478                 if (free_evt)
479                         kfree(evtp);
480                 spin_lock_irq(&phba->hbalock);
481         }
482         spin_unlock_irq(&phba->hbalock);
483
484 }
485
486 static void
487 lpfc_work_done(struct lpfc_hba *phba)
488 {
489         struct lpfc_sli_ring *pring;
490         uint32_t ha_copy, status, control, work_port_events;
491         struct lpfc_vport **vports;
492         struct lpfc_vport *vport;
493         int i;
494
495         spin_lock_irq(&phba->hbalock);
496         ha_copy = phba->work_ha;
497         phba->work_ha = 0;
498         spin_unlock_irq(&phba->hbalock);
499
500         /* First, try to post the next mailbox command to SLI4 device */
501         if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
502                 lpfc_sli4_post_async_mbox(phba);
503
504         if (ha_copy & HA_ERATT)
505                 /* Handle the error attention event */
506                 lpfc_handle_eratt(phba);
507
508         if (ha_copy & HA_MBATT)
509                 lpfc_sli_handle_mb_event(phba);
510
511         if (ha_copy & HA_LATT)
512                 lpfc_handle_latt(phba);
513
514         /* Process SLI4 events */
515         if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
516                 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
517                         lpfc_sli4_fcp_xri_abort_event_proc(phba);
518                 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
519                         lpfc_sli4_els_xri_abort_event_proc(phba);
520                 if (phba->hba_flag & ASYNC_EVENT)
521                         lpfc_sli4_async_event_proc(phba);
522                 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
523                         spin_lock_irq(&phba->hbalock);
524                         phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
525                         spin_unlock_irq(&phba->hbalock);
526                         lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
527                 }
528         }
529
530         vports = lpfc_create_vport_work_array(phba);
531         if (vports != NULL)
532                 for (i = 0; i <= phba->max_vports; i++) {
533                         /*
534                          * We could have no vports in array if unloading, so if
535                          * this happens then just use the pport
536                          */
537                         if (vports[i] == NULL && i == 0)
538                                 vport = phba->pport;
539                         else
540                                 vport = vports[i];
541                         if (vport == NULL)
542                                 break;
543                         spin_lock_irq(&vport->work_port_lock);
544                         work_port_events = vport->work_port_events;
545                         vport->work_port_events &= ~work_port_events;
546                         spin_unlock_irq(&vport->work_port_lock);
547                         if (work_port_events & WORKER_DISC_TMO)
548                                 lpfc_disc_timeout_handler(vport);
549                         if (work_port_events & WORKER_ELS_TMO)
550                                 lpfc_els_timeout_handler(vport);
551                         if (work_port_events & WORKER_HB_TMO)
552                                 lpfc_hb_timeout_handler(phba);
553                         if (work_port_events & WORKER_MBOX_TMO)
554                                 lpfc_mbox_timeout_handler(phba);
555                         if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
556                                 lpfc_unblock_fabric_iocbs(phba);
557                         if (work_port_events & WORKER_FDMI_TMO)
558                                 lpfc_fdmi_timeout_handler(vport);
559                         if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
560                                 lpfc_ramp_down_queue_handler(phba);
561                         if (work_port_events & WORKER_RAMP_UP_QUEUE)
562                                 lpfc_ramp_up_queue_handler(phba);
563                 }
564         lpfc_destroy_vport_work_array(phba, vports);
565
566         pring = &phba->sli.ring[LPFC_ELS_RING];
567         status = (ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
568         status >>= (4*LPFC_ELS_RING);
569         if ((status & HA_RXMASK) ||
570             (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
571             (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
572                 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
573                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
574                         /* Set the lpfc data pending flag */
575                         set_bit(LPFC_DATA_READY, &phba->data_flags);
576                 } else {
577                         pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
578                         lpfc_sli_handle_slow_ring_event(phba, pring,
579                                                         (status &
580                                                          HA_RXMASK));
581                 }
582                 /*
583                  * Turn on Ring interrupts
584                  */
585                 if (phba->sli_rev <= LPFC_SLI_REV3) {
586                         spin_lock_irq(&phba->hbalock);
587                         control = readl(phba->HCregaddr);
588                         if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
589                                 lpfc_debugfs_slow_ring_trc(phba,
590                                         "WRK Enable ring: cntl:x%x hacopy:x%x",
591                                         control, ha_copy, 0);
592
593                                 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
594                                 writel(control, phba->HCregaddr);
595                                 readl(phba->HCregaddr); /* flush */
596                         } else {
597                                 lpfc_debugfs_slow_ring_trc(phba,
598                                         "WRK Ring ok:     cntl:x%x hacopy:x%x",
599                                         control, ha_copy, 0);
600                         }
601                         spin_unlock_irq(&phba->hbalock);
602                 }
603         }
604         lpfc_work_list_done(phba);
605 }
606
607 int
608 lpfc_do_work(void *p)
609 {
610         struct lpfc_hba *phba = p;
611         int rc;
612
613         set_user_nice(current, -20);
614         phba->data_flags = 0;
615
616         while (!kthread_should_stop()) {
617                 /* wait and check worker queue activities */
618                 rc = wait_event_interruptible(phba->work_waitq,
619                                         (test_and_clear_bit(LPFC_DATA_READY,
620                                                             &phba->data_flags)
621                                          || kthread_should_stop()));
622                 /* Signal wakeup shall terminate the worker thread */
623                 if (rc) {
624                         lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
625                                         "0433 Wakeup on signal: rc=x%x\n", rc);
626                         break;
627                 }
628
629                 /* Attend pending lpfc data processing */
630                 lpfc_work_done(phba);
631         }
632         phba->worker_thread = NULL;
633         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
634                         "0432 Worker thread stopped.\n");
635         return 0;
636 }
637
638 /*
639  * This is only called to handle FC worker events. Since this a rare
640  * occurance, we allocate a struct lpfc_work_evt structure here instead of
641  * embedding it in the IOCB.
642  */
643 int
644 lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
645                       uint32_t evt)
646 {
647         struct lpfc_work_evt  *evtp;
648         unsigned long flags;
649
650         /*
651          * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
652          * be queued to worker thread for processing
653          */
654         evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
655         if (!evtp)
656                 return 0;
657
658         evtp->evt_arg1  = arg1;
659         evtp->evt_arg2  = arg2;
660         evtp->evt       = evt;
661
662         spin_lock_irqsave(&phba->hbalock, flags);
663         list_add_tail(&evtp->evt_listp, &phba->work_list);
664         spin_unlock_irqrestore(&phba->hbalock, flags);
665
666         lpfc_worker_wake_up(phba);
667
668         return 1;
669 }
670
671 void
672 lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
673 {
674         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
675         struct lpfc_hba  *phba = vport->phba;
676         struct lpfc_nodelist *ndlp, *next_ndlp;
677         int  rc;
678
679         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
680                 if (!NLP_CHK_NODE_ACT(ndlp))
681                         continue;
682                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
683                         continue;
684                 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
685                         ((vport->port_type == LPFC_NPIV_PORT) &&
686                         (ndlp->nlp_DID == NameServer_DID)))
687                         lpfc_unreg_rpi(vport, ndlp);
688
689                 /* Leave Fabric nodes alone on link down */
690                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
691                     (!remove && ndlp->nlp_type & NLP_FABRIC))
692                         continue;
693                 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
694                                              remove
695                                              ? NLP_EVT_DEVICE_RM
696                                              : NLP_EVT_DEVICE_RECOVERY);
697         }
698         if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
699                 lpfc_mbx_unreg_vpi(vport);
700                 spin_lock_irq(shost->host_lock);
701                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
702                 spin_unlock_irq(shost->host_lock);
703         }
704 }
705
706 void
707 lpfc_port_link_failure(struct lpfc_vport *vport)
708 {
709         /* Cleanup any outstanding received buffers */
710         lpfc_cleanup_rcv_buffers(vport);
711
712         /* Cleanup any outstanding RSCN activity */
713         lpfc_els_flush_rscn(vport);
714
715         /* Cleanup any outstanding ELS commands */
716         lpfc_els_flush_cmd(vport);
717
718         lpfc_cleanup_rpis(vport, 0);
719
720         /* Turn off discovery timer if its running */
721         lpfc_can_disctmo(vport);
722 }
723
724 void
725 lpfc_linkdown_port(struct lpfc_vport *vport)
726 {
727         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
728
729         fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
730
731         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
732                 "Link Down:       state:x%x rtry:x%x flg:x%x",
733                 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
734
735         lpfc_port_link_failure(vport);
736
737 }
738
739 int
740 lpfc_linkdown(struct lpfc_hba *phba)
741 {
742         struct lpfc_vport *vport = phba->pport;
743         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
744         struct lpfc_vport **vports;
745         LPFC_MBOXQ_t          *mb;
746         int i;
747
748         if (phba->link_state == LPFC_LINK_DOWN)
749                 return 0;
750
751         /* Block all SCSI stack I/Os */
752         lpfc_scsi_dev_block(phba);
753
754         spin_lock_irq(&phba->hbalock);
755         phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_DISCOVERED);
756         if (phba->link_state > LPFC_LINK_DOWN) {
757                 phba->link_state = LPFC_LINK_DOWN;
758                 phba->pport->fc_flag &= ~FC_LBIT;
759         }
760         spin_unlock_irq(&phba->hbalock);
761         vports = lpfc_create_vport_work_array(phba);
762         if (vports != NULL)
763                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
764                         /* Issue a LINK DOWN event to all nodes */
765                         lpfc_linkdown_port(vports[i]);
766                 }
767         lpfc_destroy_vport_work_array(phba, vports);
768         /* Clean up any firmware default rpi's */
769         mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
770         if (mb) {
771                 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
772                 mb->vport = vport;
773                 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
774                 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
775                     == MBX_NOT_FINISHED) {
776                         mempool_free(mb, phba->mbox_mem_pool);
777                 }
778         }
779
780         /* Setup myDID for link up if we are in pt2pt mode */
781         if (phba->pport->fc_flag & FC_PT2PT) {
782                 phba->pport->fc_myDID = 0;
783                 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
784                 if (mb) {
785                         lpfc_config_link(phba, mb);
786                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
787                         mb->vport = vport;
788                         if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
789                             == MBX_NOT_FINISHED) {
790                                 mempool_free(mb, phba->mbox_mem_pool);
791                         }
792                 }
793                 spin_lock_irq(shost->host_lock);
794                 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
795                 spin_unlock_irq(shost->host_lock);
796         }
797
798         return 0;
799 }
800
801 static void
802 lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
803 {
804         struct lpfc_nodelist *ndlp;
805
806         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
807                 if (!NLP_CHK_NODE_ACT(ndlp))
808                         continue;
809                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
810                         continue;
811                 if (ndlp->nlp_type & NLP_FABRIC) {
812                         /* On Linkup its safe to clean up the ndlp
813                          * from Fabric connections.
814                          */
815                         if (ndlp->nlp_DID != Fabric_DID)
816                                 lpfc_unreg_rpi(vport, ndlp);
817                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
818                 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
819                         /* Fail outstanding IO now since device is
820                          * marked for PLOGI.
821                          */
822                         lpfc_unreg_rpi(vport, ndlp);
823                 }
824         }
825 }
826
827 static void
828 lpfc_linkup_port(struct lpfc_vport *vport)
829 {
830         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
831         struct lpfc_hba  *phba = vport->phba;
832
833         if ((vport->load_flag & FC_UNLOADING) != 0)
834                 return;
835
836         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
837                 "Link Up:         top:x%x speed:x%x flg:x%x",
838                 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
839
840         /* If NPIV is not enabled, only bring the physical port up */
841         if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
842                 (vport != phba->pport))
843                 return;
844
845         fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
846
847         spin_lock_irq(shost->host_lock);
848         vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
849                             FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
850         vport->fc_flag |= FC_NDISC_ACTIVE;
851         vport->fc_ns_retry = 0;
852         spin_unlock_irq(shost->host_lock);
853
854         if (vport->fc_flag & FC_LBIT)
855                 lpfc_linkup_cleanup_nodes(vport);
856
857 }
858
859 static int
860 lpfc_linkup(struct lpfc_hba *phba)
861 {
862         struct lpfc_vport **vports;
863         int i;
864
865         phba->link_state = LPFC_LINK_UP;
866
867         /* Unblock fabric iocbs if they are blocked */
868         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
869         del_timer_sync(&phba->fabric_block_timer);
870
871         vports = lpfc_create_vport_work_array(phba);
872         if (vports != NULL)
873                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
874                         lpfc_linkup_port(vports[i]);
875         lpfc_destroy_vport_work_array(phba, vports);
876         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
877             (phba->sli_rev < LPFC_SLI_REV4))
878                 lpfc_issue_clear_la(phba, phba->pport);
879
880         return 0;
881 }
882
883 /*
884  * This routine handles processing a CLEAR_LA mailbox
885  * command upon completion. It is setup in the LPFC_MBOXQ
886  * as the completion routine when the command is
887  * handed off to the SLI layer.
888  */
889 static void
890 lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
891 {
892         struct lpfc_vport *vport = pmb->vport;
893         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
894         struct lpfc_sli   *psli = &phba->sli;
895         MAILBOX_t *mb = &pmb->u.mb;
896         uint32_t control;
897
898         /* Since we don't do discovery right now, turn these off here */
899         psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
900         psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
901         psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
902
903         /* Check for error */
904         if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
905                 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
906                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
907                                  "0320 CLEAR_LA mbxStatus error x%x hba "
908                                  "state x%x\n",
909                                  mb->mbxStatus, vport->port_state);
910                 phba->link_state = LPFC_HBA_ERROR;
911                 goto out;
912         }
913
914         if (vport->port_type == LPFC_PHYSICAL_PORT)
915                 phba->link_state = LPFC_HBA_READY;
916
917         spin_lock_irq(&phba->hbalock);
918         psli->sli_flag |= LPFC_PROCESS_LA;
919         control = readl(phba->HCregaddr);
920         control |= HC_LAINT_ENA;
921         writel(control, phba->HCregaddr);
922         readl(phba->HCregaddr); /* flush */
923         spin_unlock_irq(&phba->hbalock);
924         mempool_free(pmb, phba->mbox_mem_pool);
925         return;
926
927 out:
928         /* Device Discovery completes */
929         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
930                          "0225 Device Discovery completes\n");
931         mempool_free(pmb, phba->mbox_mem_pool);
932
933         spin_lock_irq(shost->host_lock);
934         vport->fc_flag &= ~FC_ABORT_DISCOVERY;
935         spin_unlock_irq(shost->host_lock);
936
937         lpfc_can_disctmo(vport);
938
939         /* turn on Link Attention interrupts */
940
941         spin_lock_irq(&phba->hbalock);
942         psli->sli_flag |= LPFC_PROCESS_LA;
943         control = readl(phba->HCregaddr);
944         control |= HC_LAINT_ENA;
945         writel(control, phba->HCregaddr);
946         readl(phba->HCregaddr); /* flush */
947         spin_unlock_irq(&phba->hbalock);
948
949         return;
950 }
951
952
953 static void
954 lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
955 {
956         struct lpfc_vport *vport = pmb->vport;
957
958         if (pmb->u.mb.mbxStatus)
959                 goto out;
960
961         mempool_free(pmb, phba->mbox_mem_pool);
962
963         if (phba->fc_topology == TOPOLOGY_LOOP &&
964             vport->fc_flag & FC_PUBLIC_LOOP &&
965             !(vport->fc_flag & FC_LBIT)) {
966                         /* Need to wait for FAN - use discovery timer
967                          * for timeout.  port_state is identically
968                          * LPFC_LOCAL_CFG_LINK while waiting for FAN
969                          */
970                         lpfc_set_disctmo(vport);
971                         return;
972         }
973
974         /* Start discovery by sending a FLOGI. port_state is identically
975          * LPFC_FLOGI while waiting for FLOGI cmpl
976          */
977         if (vport->port_state != LPFC_FLOGI) {
978                 lpfc_initial_flogi(vport);
979         }
980         return;
981
982 out:
983         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
984                          "0306 CONFIG_LINK mbxStatus error x%x "
985                          "HBA state x%x\n",
986                          pmb->u.mb.mbxStatus, vport->port_state);
987         mempool_free(pmb, phba->mbox_mem_pool);
988
989         lpfc_linkdown(phba);
990
991         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
992                          "0200 CONFIG_LINK bad hba state x%x\n",
993                          vport->port_state);
994
995         lpfc_issue_clear_la(phba, vport);
996         return;
997 }
998
999 static void
1000 lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1001 {
1002         struct lpfc_vport *vport = mboxq->vport;
1003         unsigned long flags;
1004
1005         if (mboxq->u.mb.mbxStatus) {
1006                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1007                          "2017 REG_FCFI mbxStatus error x%x "
1008                          "HBA state x%x\n",
1009                          mboxq->u.mb.mbxStatus, vport->port_state);
1010                 mempool_free(mboxq, phba->mbox_mem_pool);
1011                 return;
1012         }
1013
1014         /* Start FCoE discovery by sending a FLOGI. */
1015         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1016         /* Set the FCFI registered flag */
1017         spin_lock_irqsave(&phba->hbalock, flags);
1018         phba->fcf.fcf_flag |= FCF_REGISTERED;
1019         spin_unlock_irqrestore(&phba->hbalock, flags);
1020         /* If there is a pending FCoE event, restart FCF table scan. */
1021         if (lpfc_check_pending_fcoe_event(phba, 1)) {
1022                 mempool_free(mboxq, phba->mbox_mem_pool);
1023                 return;
1024         }
1025         spin_lock_irqsave(&phba->hbalock, flags);
1026         phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1027         phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1028         spin_unlock_irqrestore(&phba->hbalock, flags);
1029         if (vport->port_state != LPFC_FLOGI)
1030                 lpfc_initial_flogi(vport);
1031
1032         mempool_free(mboxq, phba->mbox_mem_pool);
1033         return;
1034 }
1035
1036 /**
1037  * lpfc_fab_name_match - Check if the fcf fabric name match.
1038  * @fab_name: pointer to fabric name.
1039  * @new_fcf_record: pointer to fcf record.
1040  *
1041  * This routine compare the fcf record's fabric name with provided
1042  * fabric name. If the fabric name are identical this function
1043  * returns 1 else return 0.
1044  **/
1045 static uint32_t
1046 lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1047 {
1048         if ((fab_name[0] ==
1049                 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record)) &&
1050             (fab_name[1] ==
1051                 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record)) &&
1052             (fab_name[2] ==
1053                 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record)) &&
1054             (fab_name[3] ==
1055                 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record)) &&
1056             (fab_name[4] ==
1057                 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record)) &&
1058             (fab_name[5] ==
1059                 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record)) &&
1060             (fab_name[6] ==
1061                 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record)) &&
1062             (fab_name[7] ==
1063                 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record)))
1064                 return 1;
1065         else
1066                 return 0;
1067 }
1068
1069 /**
1070  * lpfc_sw_name_match - Check if the fcf switch name match.
1071  * @fab_name: pointer to fabric name.
1072  * @new_fcf_record: pointer to fcf record.
1073  *
1074  * This routine compare the fcf record's switch name with provided
1075  * switch name. If the switch name are identical this function
1076  * returns 1 else return 0.
1077  **/
1078 static uint32_t
1079 lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1080 {
1081         if ((sw_name[0] ==
1082                 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record)) &&
1083             (sw_name[1] ==
1084                 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record)) &&
1085             (sw_name[2] ==
1086                 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record)) &&
1087             (sw_name[3] ==
1088                 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record)) &&
1089             (sw_name[4] ==
1090                 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record)) &&
1091             (sw_name[5] ==
1092                 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record)) &&
1093             (sw_name[6] ==
1094                 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record)) &&
1095             (sw_name[7] ==
1096                 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record)))
1097                 return 1;
1098         else
1099                 return 0;
1100 }
1101
1102 /**
1103  * lpfc_mac_addr_match - Check if the fcf mac address match.
1104  * @phba: pointer to lpfc hba data structure.
1105  * @new_fcf_record: pointer to fcf record.
1106  *
1107  * This routine compare the fcf record's mac address with HBA's
1108  * FCF mac address. If the mac addresses are identical this function
1109  * returns 1 else return 0.
1110  **/
1111 static uint32_t
1112 lpfc_mac_addr_match(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1113 {
1114         if ((phba->fcf.mac_addr[0] ==
1115                 bf_get(lpfc_fcf_record_mac_0, new_fcf_record)) &&
1116             (phba->fcf.mac_addr[1] ==
1117                 bf_get(lpfc_fcf_record_mac_1, new_fcf_record)) &&
1118             (phba->fcf.mac_addr[2] ==
1119                 bf_get(lpfc_fcf_record_mac_2, new_fcf_record)) &&
1120             (phba->fcf.mac_addr[3] ==
1121                 bf_get(lpfc_fcf_record_mac_3, new_fcf_record)) &&
1122             (phba->fcf.mac_addr[4] ==
1123                 bf_get(lpfc_fcf_record_mac_4, new_fcf_record)) &&
1124             (phba->fcf.mac_addr[5] ==
1125                 bf_get(lpfc_fcf_record_mac_5, new_fcf_record)))
1126                 return 1;
1127         else
1128                 return 0;
1129 }
1130
1131 /**
1132  * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1133  * @phba: pointer to lpfc hba data structure.
1134  * @new_fcf_record: pointer to fcf record.
1135  *
1136  * This routine copies the FCF information from the FCF
1137  * record to lpfc_hba data structure.
1138  **/
1139 static void
1140 lpfc_copy_fcf_record(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1141 {
1142         phba->fcf.fabric_name[0] =
1143                 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1144         phba->fcf.fabric_name[1] =
1145                 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1146         phba->fcf.fabric_name[2] =
1147                 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1148         phba->fcf.fabric_name[3] =
1149                 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1150         phba->fcf.fabric_name[4] =
1151                 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1152         phba->fcf.fabric_name[5] =
1153                 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1154         phba->fcf.fabric_name[6] =
1155                 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1156         phba->fcf.fabric_name[7] =
1157                 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1158         phba->fcf.mac_addr[0] =
1159                 bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1160         phba->fcf.mac_addr[1] =
1161                 bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1162         phba->fcf.mac_addr[2] =
1163                 bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1164         phba->fcf.mac_addr[3] =
1165                 bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1166         phba->fcf.mac_addr[4] =
1167                 bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1168         phba->fcf.mac_addr[5] =
1169                 bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1170         phba->fcf.fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1171         phba->fcf.priority = new_fcf_record->fip_priority;
1172         phba->fcf.switch_name[0] =
1173                 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1174         phba->fcf.switch_name[1] =
1175                 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1176         phba->fcf.switch_name[2] =
1177                 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1178         phba->fcf.switch_name[3] =
1179                 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1180         phba->fcf.switch_name[4] =
1181                 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1182         phba->fcf.switch_name[5] =
1183                 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1184         phba->fcf.switch_name[6] =
1185                 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1186         phba->fcf.switch_name[7] =
1187                 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
1188 }
1189
1190 /**
1191  * lpfc_register_fcf - Register the FCF with hba.
1192  * @phba: pointer to lpfc hba data structure.
1193  *
1194  * This routine issues a register fcfi mailbox command to register
1195  * the fcf with HBA.
1196  **/
1197 static void
1198 lpfc_register_fcf(struct lpfc_hba *phba)
1199 {
1200         LPFC_MBOXQ_t *fcf_mbxq;
1201         int rc;
1202         unsigned long flags;
1203
1204         spin_lock_irqsave(&phba->hbalock, flags);
1205
1206         /* If the FCF is not availabe do nothing. */
1207         if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
1208                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1209                 spin_unlock_irqrestore(&phba->hbalock, flags);
1210                 return;
1211         }
1212
1213         /* The FCF is already registered, start discovery */
1214         if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1215                 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1216                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1217                 spin_unlock_irqrestore(&phba->hbalock, flags);
1218                 if (phba->pport->port_state != LPFC_FLOGI)
1219                         lpfc_initial_flogi(phba->pport);
1220                 return;
1221         }
1222         spin_unlock_irqrestore(&phba->hbalock, flags);
1223
1224         fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1225                 GFP_KERNEL);
1226         if (!fcf_mbxq) {
1227                 spin_lock_irqsave(&phba->hbalock, flags);
1228                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1229                 spin_unlock_irqrestore(&phba->hbalock, flags);
1230                 return;
1231         }
1232
1233         lpfc_reg_fcfi(phba, fcf_mbxq);
1234         fcf_mbxq->vport = phba->pport;
1235         fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1236         rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
1237         if (rc == MBX_NOT_FINISHED) {
1238                 spin_lock_irqsave(&phba->hbalock, flags);
1239                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1240                 spin_unlock_irqrestore(&phba->hbalock, flags);
1241                 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
1242         }
1243
1244         return;
1245 }
1246
1247 /**
1248  * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1249  * @phba: pointer to lpfc hba data structure.
1250  * @new_fcf_record: pointer to fcf record.
1251  * @boot_flag: Indicates if this record used by boot bios.
1252  * @addr_mode: The address mode to be used by this FCF
1253  *
1254  * This routine compare the fcf record with connect list obtained from the
1255  * config region to decide if this FCF can be used for SAN discovery. It returns
1256  * 1 if this record can be used for SAN discovery else return zero. If this FCF
1257  * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1258  * is used by boot bios and addr_mode will indicate the addressing mode to be
1259  * used for this FCF when the function returns.
1260  * If the FCF record need to be used with a particular vlan id, the vlan is
1261  * set in the vlan_id on return of the function. If not VLAN tagging need to
1262  * be used with the FCF vlan_id will be set to 0xFFFF;
1263  **/
1264 static int
1265 lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1266                         struct fcf_record *new_fcf_record,
1267                         uint32_t *boot_flag, uint32_t *addr_mode,
1268                         uint16_t *vlan_id)
1269 {
1270         struct lpfc_fcf_conn_entry *conn_entry;
1271         int i, j, fcf_vlan_id = 0;
1272
1273         /* Find the lowest VLAN id in the FCF record */
1274         for (i = 0; i < 512; i++) {
1275                 if (new_fcf_record->vlan_bitmap[i]) {
1276                         fcf_vlan_id = i * 8;
1277                         j = 0;
1278                         while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1279                                 j++;
1280                                 fcf_vlan_id++;
1281                         }
1282                         break;
1283                 }
1284         }
1285
1286         /* If FCF not available return 0 */
1287         if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1288                 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1289                 return 0;
1290
1291         if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
1292                 *boot_flag = 0;
1293                 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1294                                 new_fcf_record);
1295                 if (phba->valid_vlan)
1296                         *vlan_id = phba->vlan_id;
1297                 else
1298                         *vlan_id = 0xFFFF;
1299                 return 1;
1300         }
1301
1302         /*
1303          * If there are no FCF connection table entry, driver connect to all
1304          * FCFs.
1305          */
1306         if (list_empty(&phba->fcf_conn_rec_list)) {
1307                 *boot_flag = 0;
1308                 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1309                         new_fcf_record);
1310
1311                 /*
1312                  * When there are no FCF connect entries, use driver's default
1313                  * addressing mode - FPMA.
1314                  */
1315                 if (*addr_mode & LPFC_FCF_FPMA)
1316                         *addr_mode = LPFC_FCF_FPMA;
1317
1318                 /* If FCF record report a vlan id use that vlan id */
1319                 if (fcf_vlan_id)
1320                         *vlan_id = fcf_vlan_id;
1321                 else
1322                         *vlan_id = 0xFFFF;
1323                 return 1;
1324         }
1325
1326         list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list, list) {
1327                 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1328                         continue;
1329
1330                 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1331                         !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
1332                                              new_fcf_record))
1333                         continue;
1334                 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1335                         !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1336                                             new_fcf_record))
1337                         continue;
1338                 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1339                         /*
1340                          * If the vlan bit map does not have the bit set for the
1341                          * vlan id to be used, then it is not a match.
1342                          */
1343                         if (!(new_fcf_record->vlan_bitmap
1344                                 [conn_entry->conn_rec.vlan_tag / 8] &
1345                                 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1346                                 continue;
1347                 }
1348
1349                 /*
1350                  * If connection record does not support any addressing mode,
1351                  * skip the FCF record.
1352                  */
1353                 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1354                         & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1355                         continue;
1356
1357                 /*
1358                  * Check if the connection record specifies a required
1359                  * addressing mode.
1360                  */
1361                 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1362                         !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1363
1364                         /*
1365                          * If SPMA required but FCF not support this continue.
1366                          */
1367                         if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1368                                 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1369                                         new_fcf_record) & LPFC_FCF_SPMA))
1370                                 continue;
1371
1372                         /*
1373                          * If FPMA required but FCF not support this continue.
1374                          */
1375                         if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1376                                 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1377                                 new_fcf_record) & LPFC_FCF_FPMA))
1378                                 continue;
1379                 }
1380
1381                 /*
1382                  * This fcf record matches filtering criteria.
1383                  */
1384                 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1385                         *boot_flag = 1;
1386                 else
1387                         *boot_flag = 0;
1388
1389                 /*
1390                  * If user did not specify any addressing mode, or if the
1391                  * prefered addressing mode specified by user is not supported
1392                  * by FCF, allow fabric to pick the addressing mode.
1393                  */
1394                 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1395                                 new_fcf_record);
1396                 /*
1397                  * If the user specified a required address mode, assign that
1398                  * address mode
1399                  */
1400                 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1401                         (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1402                         *addr_mode = (conn_entry->conn_rec.flags &
1403                                 FCFCNCT_AM_SPMA) ?
1404                                 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1405                 /*
1406                  * If the user specified a prefered address mode, use the
1407                  * addr mode only if FCF support the addr_mode.
1408                  */
1409                 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1410                         (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1411                         (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1412                         (*addr_mode & LPFC_FCF_SPMA))
1413                                 *addr_mode = LPFC_FCF_SPMA;
1414                 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1415                         (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1416                         !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1417                         (*addr_mode & LPFC_FCF_FPMA))
1418                                 *addr_mode = LPFC_FCF_FPMA;
1419
1420                 /* If matching connect list has a vlan id, use it */
1421                 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1422                         *vlan_id = conn_entry->conn_rec.vlan_tag;
1423                 /*
1424                  * If no vlan id is specified in connect list, use the vlan id
1425                  * in the FCF record
1426                  */
1427                 else if (fcf_vlan_id)
1428                         *vlan_id = fcf_vlan_id;
1429                 else
1430                         *vlan_id = 0xFFFF;
1431
1432                 return 1;
1433         }
1434
1435         return 0;
1436 }
1437
1438 /**
1439  * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1440  * @phba: pointer to lpfc hba data structure.
1441  * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1442  *
1443  * This function check if there is any fcoe event pending while driver
1444  * scan FCF entries. If there is any pending event, it will restart the
1445  * FCF saning and return 1 else return 0.
1446  */
1447 int
1448 lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1449 {
1450         LPFC_MBOXQ_t *mbox;
1451         int rc;
1452         /*
1453          * If the Link is up and no FCoE events while in the
1454          * FCF discovery, no need to restart FCF discovery.
1455          */
1456         if ((phba->link_state  >= LPFC_LINK_UP) &&
1457                 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1458                 return 0;
1459
1460         spin_lock_irq(&phba->hbalock);
1461         phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1462         spin_unlock_irq(&phba->hbalock);
1463
1464         if (phba->link_state >= LPFC_LINK_UP)
1465                 lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
1466         else {
1467                 /*
1468                  * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1469                  * flag
1470                  */
1471                 spin_lock_irq(&phba->hbalock);
1472                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1473                 spin_unlock_irq(&phba->hbalock);
1474         }
1475
1476         if (unreg_fcf) {
1477                 spin_lock_irq(&phba->hbalock);
1478                 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1479                 spin_unlock_irq(&phba->hbalock);
1480                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1481                 if (!mbox) {
1482                         lpfc_printf_log(phba, KERN_ERR,
1483                                 LOG_DISCOVERY|LOG_MBOX,
1484                                 "2610 UNREG_FCFI mbox allocation failed\n");
1485                         return 1;
1486                 }
1487                 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
1488                 mbox->vport = phba->pport;
1489                 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
1490                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1491                 if (rc == MBX_NOT_FINISHED) {
1492                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
1493                                 "2611 UNREG_FCFI issue mbox failed\n");
1494                         mempool_free(mbox, phba->mbox_mem_pool);
1495                 }
1496         }
1497
1498         return 1;
1499 }
1500
1501 /**
1502  * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
1503  * @phba: pointer to lpfc hba data structure.
1504  * @mboxq: pointer to mailbox object.
1505  *
1506  * This function iterate through all the fcf records available in
1507  * HBA and choose the optimal FCF record for discovery. After finding
1508  * the FCF for discovery it register the FCF record and kick start
1509  * discovery.
1510  * If FCF_IN_USE flag is set in currently used FCF, the routine try to
1511  * use a FCF record which match fabric name and mac address of the
1512  * currently used FCF record.
1513  * If the driver support only one FCF, it will try to use the FCF record
1514  * used by BOOT_BIOS.
1515  */
1516 void
1517 lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1518 {
1519         void *virt_addr;
1520         dma_addr_t phys_addr;
1521         uint8_t *bytep;
1522         struct lpfc_mbx_sge sge;
1523         struct lpfc_mbx_read_fcf_tbl *read_fcf;
1524         uint32_t shdr_status, shdr_add_status;
1525         union lpfc_sli4_cfg_shdr *shdr;
1526         struct fcf_record *new_fcf_record;
1527         int rc;
1528         uint32_t boot_flag, addr_mode;
1529         uint32_t next_fcf_index;
1530         unsigned long flags;
1531         uint16_t vlan_id;
1532
1533         /* If there is pending FCoE event restart FCF table scan */
1534         if (lpfc_check_pending_fcoe_event(phba, 0)) {
1535                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1536                 return;
1537         }
1538
1539         /* Get the first SGE entry from the non-embedded DMA memory. This
1540          * routine only uses a single SGE.
1541          */
1542         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1543         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1544         if (unlikely(!mboxq->sge_array)) {
1545                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1546                                 "2524 Failed to get the non-embedded SGE "
1547                                 "virtual address\n");
1548                 goto out;
1549         }
1550         virt_addr = mboxq->sge_array->addr[0];
1551
1552         shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1553         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1554         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
1555                                  &shdr->response);
1556         /*
1557          * The FCF Record was read and there is no reason for the driver
1558          * to maintain the FCF record data or memory. Instead, just need
1559          * to book keeping the FCFIs can be used.
1560          */
1561         if (shdr_status || shdr_add_status) {
1562                 if (shdr_status == STATUS_FCF_TABLE_EMPTY) {
1563                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1564                                         "2726 READ_FCF_RECORD Indicates empty "
1565                                         "FCF table.\n");
1566                 } else {
1567                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1568                                         "2521 READ_FCF_RECORD mailbox failed "
1569                                         "with status x%x add_status x%x, mbx\n",
1570                                         shdr_status, shdr_add_status);
1571                 }
1572                 goto out;
1573         }
1574         /* Interpreting the returned information of FCF records */
1575         read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1576         lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1577                               sizeof(struct lpfc_mbx_read_fcf_tbl));
1578         next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1579
1580         new_fcf_record = (struct fcf_record *)(virt_addr +
1581                           sizeof(struct lpfc_mbx_read_fcf_tbl));
1582         lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1583                               sizeof(struct fcf_record));
1584         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1585
1586         rc = lpfc_match_fcf_conn_list(phba, new_fcf_record,
1587                                       &boot_flag, &addr_mode,
1588                                         &vlan_id);
1589         /*
1590          * If the fcf record does not match with connect list entries
1591          * read the next entry.
1592          */
1593         if (!rc)
1594                 goto read_next_fcf;
1595         /*
1596          * If this is not the first FCF discovery of the HBA, use last
1597          * FCF record for the discovery.
1598          */
1599         spin_lock_irqsave(&phba->hbalock, flags);
1600         if (phba->fcf.fcf_flag & FCF_IN_USE) {
1601                 if (lpfc_fab_name_match(phba->fcf.fabric_name,
1602                                         new_fcf_record) &&
1603                     lpfc_sw_name_match(phba->fcf.switch_name,
1604                                         new_fcf_record) &&
1605                     lpfc_mac_addr_match(phba, new_fcf_record)) {
1606                         phba->fcf.fcf_flag |= FCF_AVAILABLE;
1607                         spin_unlock_irqrestore(&phba->hbalock, flags);
1608                         goto out;
1609                 }
1610                 spin_unlock_irqrestore(&phba->hbalock, flags);
1611                 goto read_next_fcf;
1612         }
1613         if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1614                 /*
1615                  * If the current FCF record does not have boot flag
1616                  * set and new fcf record has boot flag set, use the
1617                  * new fcf record.
1618                  */
1619                 if (boot_flag && !(phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1620                         /* Use this FCF record */
1621                         lpfc_copy_fcf_record(phba, new_fcf_record);
1622                         phba->fcf.addr_mode = addr_mode;
1623                         phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1624                         if (vlan_id != 0xFFFF) {
1625                                 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1626                                 phba->fcf.vlan_id = vlan_id;
1627                         }
1628                         spin_unlock_irqrestore(&phba->hbalock, flags);
1629                         goto read_next_fcf;
1630                 }
1631                 /*
1632                  * If the current FCF record has boot flag set and the
1633                  * new FCF record does not have boot flag, read the next
1634                  * FCF record.
1635                  */
1636                 if (!boot_flag && (phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1637                         spin_unlock_irqrestore(&phba->hbalock, flags);
1638                         goto read_next_fcf;
1639                 }
1640                 /*
1641                  * If there is a record with lower priority value for
1642                  * the current FCF, use that record.
1643                  */
1644                 if (lpfc_fab_name_match(phba->fcf.fabric_name,
1645                                         new_fcf_record) &&
1646                     (new_fcf_record->fip_priority < phba->fcf.priority)) {
1647                         /* Use this FCF record */
1648                         lpfc_copy_fcf_record(phba, new_fcf_record);
1649                         phba->fcf.addr_mode = addr_mode;
1650                         if (vlan_id != 0xFFFF) {
1651                                 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1652                                 phba->fcf.vlan_id = vlan_id;
1653                         }
1654                         spin_unlock_irqrestore(&phba->hbalock, flags);
1655                         goto read_next_fcf;
1656                 }
1657                 spin_unlock_irqrestore(&phba->hbalock, flags);
1658                 goto read_next_fcf;
1659         }
1660         /*
1661          * This is the first available FCF record, use this
1662          * record.
1663          */
1664         lpfc_copy_fcf_record(phba, new_fcf_record);
1665         phba->fcf.addr_mode = addr_mode;
1666         if (boot_flag)
1667                 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1668         phba->fcf.fcf_flag |= FCF_AVAILABLE;
1669         if (vlan_id != 0xFFFF) {
1670                 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1671                 phba->fcf.vlan_id = vlan_id;
1672         }
1673         spin_unlock_irqrestore(&phba->hbalock, flags);
1674         goto read_next_fcf;
1675
1676 read_next_fcf:
1677         lpfc_sli4_mbox_cmd_free(phba, mboxq);
1678         if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0)
1679                 lpfc_register_fcf(phba);
1680         else
1681                 lpfc_sli4_read_fcf_record(phba, next_fcf_index);
1682         return;
1683
1684 out:
1685         lpfc_sli4_mbox_cmd_free(phba, mboxq);
1686         lpfc_register_fcf(phba);
1687
1688         return;
1689 }
1690
1691 /**
1692  * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
1693  * @phba: pointer to lpfc hba data structure.
1694  * @mboxq: pointer to mailbox data structure.
1695  *
1696  * This function handles completion of init vpi mailbox command.
1697  */
1698 static void
1699 lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1700 {
1701         struct lpfc_vport *vport = mboxq->vport;
1702         if (mboxq->u.mb.mbxStatus) {
1703                 lpfc_printf_vlog(vport, KERN_ERR,
1704                                 LOG_MBOX,
1705                                 "2609 Init VPI mailbox failed 0x%x\n",
1706                                 mboxq->u.mb.mbxStatus);
1707                 mempool_free(mboxq, phba->mbox_mem_pool);
1708                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1709                 return;
1710         }
1711         spin_lock_irq(&phba->hbalock);
1712         vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
1713         spin_unlock_irq(&phba->hbalock);
1714
1715         if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1716                 lpfc_initial_fdisc(vport);
1717         else {
1718                 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
1719                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1720                                  "2606 No NPIV Fabric support\n");
1721         }
1722         return;
1723 }
1724
1725 /**
1726  * lpfc_start_fdiscs - send fdiscs for each vports on this port.
1727  * @phba: pointer to lpfc hba data structure.
1728  *
1729  * This function loops through the list of vports on the @phba and issues an
1730  * FDISC if possible.
1731  */
1732 void
1733 lpfc_start_fdiscs(struct lpfc_hba *phba)
1734 {
1735         struct lpfc_vport **vports;
1736         int i;
1737         LPFC_MBOXQ_t *mboxq;
1738         int rc;
1739
1740         vports = lpfc_create_vport_work_array(phba);
1741         if (vports != NULL) {
1742                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1743                         if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1744                                 continue;
1745                         /* There are no vpi for this vport */
1746                         if (vports[i]->vpi > phba->max_vpi) {
1747                                 lpfc_vport_set_state(vports[i],
1748                                                      FC_VPORT_FAILED);
1749                                 continue;
1750                         }
1751                         if (phba->fc_topology == TOPOLOGY_LOOP) {
1752                                 lpfc_vport_set_state(vports[i],
1753                                                      FC_VPORT_LINKDOWN);
1754                                 continue;
1755                         }
1756                         if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
1757                                 mboxq = mempool_alloc(phba->mbox_mem_pool,
1758                                         GFP_KERNEL);
1759                                 if (!mboxq) {
1760                                         lpfc_printf_vlog(vports[i], KERN_ERR,
1761                                         LOG_MBOX, "2607 Failed to allocate "
1762                                         "init_vpi mailbox\n");
1763                                         continue;
1764                                 }
1765                                 lpfc_init_vpi(phba, mboxq, vports[i]->vpi);
1766                                 mboxq->vport = vports[i];
1767                                 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
1768                                 rc = lpfc_sli_issue_mbox(phba, mboxq,
1769                                         MBX_NOWAIT);
1770                                 if (rc == MBX_NOT_FINISHED) {
1771                                         lpfc_printf_vlog(vports[i], KERN_ERR,
1772                                         LOG_MBOX, "2608 Failed to issue "
1773                                         "init_vpi mailbox\n");
1774                                         mempool_free(mboxq,
1775                                                 phba->mbox_mem_pool);
1776                                 }
1777                                 continue;
1778                         }
1779                         if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1780                                 lpfc_initial_fdisc(vports[i]);
1781                         else {
1782                                 lpfc_vport_set_state(vports[i],
1783                                                      FC_VPORT_NO_FABRIC_SUPP);
1784                                 lpfc_printf_vlog(vports[i], KERN_ERR,
1785                                                  LOG_ELS,
1786                                                  "0259 No NPIV "
1787                                                  "Fabric support\n");
1788                         }
1789                 }
1790         }
1791         lpfc_destroy_vport_work_array(phba, vports);
1792 }
1793
1794 void
1795 lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1796 {
1797         struct lpfc_dmabuf *dmabuf = mboxq->context1;
1798         struct lpfc_vport *vport = mboxq->vport;
1799
1800         if (mboxq->u.mb.mbxStatus) {
1801                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1802                          "2018 REG_VFI mbxStatus error x%x "
1803                          "HBA state x%x\n",
1804                          mboxq->u.mb.mbxStatus, vport->port_state);
1805                 if (phba->fc_topology == TOPOLOGY_LOOP) {
1806                         /* FLOGI failed, use loop map to make discovery list */
1807                         lpfc_disc_list_loopmap(vport);
1808                         /* Start discovery */
1809                         lpfc_disc_start(vport);
1810                         goto fail_free_mem;
1811                 }
1812                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1813                 goto fail_free_mem;
1814         }
1815         /* The VPI is implicitly registered when the VFI is registered */
1816         vport->vpi_state |= LPFC_VPI_REGISTERED;
1817
1818         if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1819                 lpfc_start_fdiscs(phba);
1820                 lpfc_do_scr_ns_plogi(phba, vport);
1821         }
1822
1823 fail_free_mem:
1824         mempool_free(mboxq, phba->mbox_mem_pool);
1825         lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1826         kfree(dmabuf);
1827         return;
1828 }
1829
1830 static void
1831 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1832 {
1833         MAILBOX_t *mb = &pmb->u.mb;
1834         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
1835         struct lpfc_vport  *vport = pmb->vport;
1836
1837
1838         /* Check for error */
1839         if (mb->mbxStatus) {
1840                 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
1841                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1842                                  "0319 READ_SPARAM mbxStatus error x%x "
1843                                  "hba state x%x>\n",
1844                                  mb->mbxStatus, vport->port_state);
1845                 lpfc_linkdown(phba);
1846                 goto out;
1847         }
1848
1849         memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
1850                sizeof (struct serv_parm));
1851         if (phba->cfg_soft_wwnn)
1852                 u64_to_wwn(phba->cfg_soft_wwnn,
1853                            vport->fc_sparam.nodeName.u.wwn);
1854         if (phba->cfg_soft_wwpn)
1855                 u64_to_wwn(phba->cfg_soft_wwpn,
1856                            vport->fc_sparam.portName.u.wwn);
1857         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1858                sizeof(vport->fc_nodename));
1859         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1860                sizeof(vport->fc_portname));
1861         if (vport->port_type == LPFC_PHYSICAL_PORT) {
1862                 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1863                 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1864         }
1865
1866         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1867         kfree(mp);
1868         mempool_free(pmb, phba->mbox_mem_pool);
1869         return;
1870
1871 out:
1872         pmb->context1 = NULL;
1873         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1874         kfree(mp);
1875         lpfc_issue_clear_la(phba, vport);
1876         mempool_free(pmb, phba->mbox_mem_pool);
1877         return;
1878 }
1879
1880 static void
1881 lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
1882 {
1883         struct lpfc_vport *vport = phba->pport;
1884         LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
1885         int i;
1886         struct lpfc_dmabuf *mp;
1887         int rc;
1888         struct fcf_record *fcf_record;
1889
1890         sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1891
1892         spin_lock_irq(&phba->hbalock);
1893         switch (la->UlnkSpeed) {
1894         case LA_1GHZ_LINK:
1895                 phba->fc_linkspeed = LA_1GHZ_LINK;
1896                 break;
1897         case LA_2GHZ_LINK:
1898                 phba->fc_linkspeed = LA_2GHZ_LINK;
1899                 break;
1900         case LA_4GHZ_LINK:
1901                 phba->fc_linkspeed = LA_4GHZ_LINK;
1902                 break;
1903         case LA_8GHZ_LINK:
1904                 phba->fc_linkspeed = LA_8GHZ_LINK;
1905                 break;
1906         case LA_10GHZ_LINK:
1907                 phba->fc_linkspeed = LA_10GHZ_LINK;
1908                 break;
1909         default:
1910                 phba->fc_linkspeed = LA_UNKNW_LINK;
1911                 break;
1912         }
1913
1914         phba->fc_topology = la->topology;
1915         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
1916
1917         if (phba->fc_topology == TOPOLOGY_LOOP) {
1918                 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
1919
1920                 /* if npiv is enabled and this adapter supports npiv log
1921                  * a message that npiv is not supported in this topology
1922                  */
1923                 if (phba->cfg_enable_npiv && phba->max_vpi)
1924                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1925                                 "1309 Link Up Event npiv not supported in loop "
1926                                 "topology\n");
1927                                 /* Get Loop Map information */
1928                 if (la->il)
1929                         vport->fc_flag |= FC_LBIT;
1930
1931                 vport->fc_myDID = la->granted_AL_PA;
1932                 i = la->un.lilpBde64.tus.f.bdeSize;
1933
1934                 if (i == 0) {
1935                         phba->alpa_map[0] = 0;
1936                 } else {
1937                         if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
1938                                 int numalpa, j, k;
1939                                 union {
1940                                         uint8_t pamap[16];
1941                                         struct {
1942                                                 uint32_t wd1;
1943                                                 uint32_t wd2;
1944                                                 uint32_t wd3;
1945                                                 uint32_t wd4;
1946                                         } pa;
1947                                 } un;
1948                                 numalpa = phba->alpa_map[0];
1949                                 j = 0;
1950                                 while (j < numalpa) {
1951                                         memset(un.pamap, 0, 16);
1952                                         for (k = 1; j < numalpa; k++) {
1953                                                 un.pamap[k - 1] =
1954                                                         phba->alpa_map[j + 1];
1955                                                 j++;
1956                                                 if (k == 16)
1957                                                         break;
1958                                         }
1959                                         /* Link Up Event ALPA map */
1960                                         lpfc_printf_log(phba,
1961                                                         KERN_WARNING,
1962                                                         LOG_LINK_EVENT,
1963                                                         "1304 Link Up Event "
1964                                                         "ALPA map Data: x%x "
1965                                                         "x%x x%x x%x\n",
1966                                                         un.pa.wd1, un.pa.wd2,
1967                                                         un.pa.wd3, un.pa.wd4);
1968                                 }
1969                         }
1970                 }
1971         } else {
1972                 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
1973                         if (phba->max_vpi && phba->cfg_enable_npiv &&
1974                            (phba->sli_rev == 3))
1975                                 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1976                 }
1977                 vport->fc_myDID = phba->fc_pref_DID;
1978                 vport->fc_flag |= FC_LBIT;
1979         }
1980         spin_unlock_irq(&phba->hbalock);
1981
1982         lpfc_linkup(phba);
1983         if (sparam_mbox) {
1984                 lpfc_read_sparam(phba, sparam_mbox, 0);
1985                 sparam_mbox->vport = vport;
1986                 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
1987                 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
1988                 if (rc == MBX_NOT_FINISHED) {
1989                         mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1990                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1991                         kfree(mp);
1992                         mempool_free(sparam_mbox, phba->mbox_mem_pool);
1993                         goto out;
1994                 }
1995         }
1996
1997         if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
1998                 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1999                 if (!cfglink_mbox)
2000                         goto out;
2001                 vport->port_state = LPFC_LOCAL_CFG_LINK;
2002                 lpfc_config_link(phba, cfglink_mbox);
2003                 cfglink_mbox->vport = vport;
2004                 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
2005                 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
2006                 if (rc == MBX_NOT_FINISHED) {
2007                         mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2008                         goto out;
2009                 }
2010         } else {
2011                 vport->port_state = LPFC_VPORT_UNKNOWN;
2012                 /*
2013                  * Add the driver's default FCF record at FCF index 0 now. This
2014                  * is phase 1 implementation that support FCF index 0 and driver
2015                  * defaults.
2016                  */
2017                 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
2018                         fcf_record = kzalloc(sizeof(struct fcf_record),
2019                                         GFP_KERNEL);
2020                         if (unlikely(!fcf_record)) {
2021                                 lpfc_printf_log(phba, KERN_ERR,
2022                                         LOG_MBOX | LOG_SLI,
2023                                         "2554 Could not allocate memmory for "
2024                                         "fcf record\n");
2025                                 rc = -ENODEV;
2026                                 goto out;
2027                         }
2028
2029                         lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2030                                                 LPFC_FCOE_FCF_DEF_INDEX);
2031                         rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2032                         if (unlikely(rc)) {
2033                                 lpfc_printf_log(phba, KERN_ERR,
2034                                         LOG_MBOX | LOG_SLI,
2035                                         "2013 Could not manually add FCF "
2036                                         "record 0, status %d\n", rc);
2037                                 rc = -ENODEV;
2038                                 kfree(fcf_record);
2039                                 goto out;
2040                         }
2041                         kfree(fcf_record);
2042                 }
2043                 /*
2044                  * The driver is expected to do FIP/FCF. Call the port
2045                  * and get the FCF Table.
2046                  */
2047                 spin_lock_irq(&phba->hbalock);
2048                 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2049                         spin_unlock_irq(&phba->hbalock);
2050                         return;
2051                 }
2052                 spin_unlock_irq(&phba->hbalock);
2053                 rc = lpfc_sli4_read_fcf_record(phba,
2054                                         LPFC_FCOE_FCF_GET_FIRST);
2055                 if (rc)
2056                         goto out;
2057         }
2058
2059         return;
2060 out:
2061         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2062         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2063                          "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2064                          vport->port_state, sparam_mbox, cfglink_mbox);
2065         lpfc_issue_clear_la(phba, vport);
2066         return;
2067 }
2068
2069 static void
2070 lpfc_enable_la(struct lpfc_hba *phba)
2071 {
2072         uint32_t control;
2073         struct lpfc_sli *psli = &phba->sli;
2074         spin_lock_irq(&phba->hbalock);
2075         psli->sli_flag |= LPFC_PROCESS_LA;
2076         if (phba->sli_rev <= LPFC_SLI_REV3) {
2077                 control = readl(phba->HCregaddr);
2078                 control |= HC_LAINT_ENA;
2079                 writel(control, phba->HCregaddr);
2080                 readl(phba->HCregaddr); /* flush */
2081         }
2082         spin_unlock_irq(&phba->hbalock);
2083 }
2084
2085 static void
2086 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2087 {
2088         lpfc_linkdown(phba);
2089         lpfc_enable_la(phba);
2090         lpfc_unregister_unused_fcf(phba);
2091         /* turn on Link Attention interrupts - no CLEAR_LA needed */
2092 }
2093
2094
2095 /*
2096  * This routine handles processing a READ_LA mailbox
2097  * command upon completion. It is setup in the LPFC_MBOXQ
2098  * as the completion routine when the command is
2099  * handed off to the SLI layer.
2100  */
2101 void
2102 lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2103 {
2104         struct lpfc_vport *vport = pmb->vport;
2105         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2106         READ_LA_VAR *la;
2107         MAILBOX_t *mb = &pmb->u.mb;
2108         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2109
2110         /* Unblock ELS traffic */
2111         phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
2112         /* Check for error */
2113         if (mb->mbxStatus) {
2114                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2115                                 "1307 READ_LA mbox error x%x state x%x\n",
2116                                 mb->mbxStatus, vport->port_state);
2117                 lpfc_mbx_issue_link_down(phba);
2118                 phba->link_state = LPFC_HBA_ERROR;
2119                 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2120         }
2121
2122         la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
2123
2124         memcpy(&phba->alpa_map[0], mp->virt, 128);
2125
2126         spin_lock_irq(shost->host_lock);
2127         if (la->pb)
2128                 vport->fc_flag |= FC_BYPASSED_MODE;
2129         else
2130                 vport->fc_flag &= ~FC_BYPASSED_MODE;
2131         spin_unlock_irq(shost->host_lock);
2132
2133         if ((phba->fc_eventTag  < la->eventTag) ||
2134             (phba->fc_eventTag == la->eventTag)) {
2135                 phba->fc_stat.LinkMultiEvent++;
2136                 if (la->attType == AT_LINK_UP)
2137                         if (phba->fc_eventTag != 0)
2138                                 lpfc_linkdown(phba);
2139         }
2140
2141         phba->fc_eventTag = la->eventTag;
2142         if (la->mm)
2143                 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2144         else
2145                 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
2146
2147         phba->link_events++;
2148         if (la->attType == AT_LINK_UP && (!la->mm)) {
2149                 phba->fc_stat.LinkUp++;
2150                 if (phba->link_flag & LS_LOOPBACK_MODE) {
2151                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2152                                         "1306 Link Up Event in loop back mode "
2153                                         "x%x received Data: x%x x%x x%x x%x\n",
2154                                         la->eventTag, phba->fc_eventTag,
2155                                         la->granted_AL_PA, la->UlnkSpeed,
2156                                         phba->alpa_map[0]);
2157                 } else {
2158                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2159                                         "1303 Link Up Event x%x received "
2160                                         "Data: x%x x%x x%x x%x x%x x%x %d\n",
2161                                         la->eventTag, phba->fc_eventTag,
2162                                         la->granted_AL_PA, la->UlnkSpeed,
2163                                         phba->alpa_map[0],
2164                                         la->mm, la->fa,
2165                                         phba->wait_4_mlo_maint_flg);
2166                 }
2167                 lpfc_mbx_process_link_up(phba, la);
2168         } else if (la->attType == AT_LINK_DOWN) {
2169                 phba->fc_stat.LinkDown++;
2170                 if (phba->link_flag & LS_LOOPBACK_MODE) {
2171                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2172                                 "1308 Link Down Event in loop back mode "
2173                                 "x%x received "
2174                                 "Data: x%x x%x x%x\n",
2175                                 la->eventTag, phba->fc_eventTag,
2176                                 phba->pport->port_state, vport->fc_flag);
2177                 }
2178                 else {
2179                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2180                                 "1305 Link Down Event x%x received "
2181                                 "Data: x%x x%x x%x x%x x%x\n",
2182                                 la->eventTag, phba->fc_eventTag,
2183                                 phba->pport->port_state, vport->fc_flag,
2184                                 la->mm, la->fa);
2185                 }
2186                 lpfc_mbx_issue_link_down(phba);
2187         }
2188         if (la->mm && la->attType == AT_LINK_UP) {
2189                 if (phba->link_state != LPFC_LINK_DOWN) {
2190                         phba->fc_stat.LinkDown++;
2191                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2192                                 "1312 Link Down Event x%x received "
2193                                 "Data: x%x x%x x%x\n",
2194                                 la->eventTag, phba->fc_eventTag,
2195                                 phba->pport->port_state, vport->fc_flag);
2196                         lpfc_mbx_issue_link_down(phba);
2197                 } else
2198                         lpfc_enable_la(phba);
2199
2200                 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2201                                 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2202                                 "Data: x%x x%x x%x\n",
2203                                 la->eventTag, phba->fc_eventTag,
2204                                 phba->pport->port_state, vport->fc_flag);
2205                 /*
2206                  * The cmnd that triggered this will be waiting for this
2207                  * signal.
2208                  */
2209                 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2210                 if (phba->wait_4_mlo_maint_flg) {
2211                         phba->wait_4_mlo_maint_flg = 0;
2212                         wake_up_interruptible(&phba->wait_4_mlo_m_q);
2213                 }
2214         }
2215
2216         if (la->fa) {
2217                 if (la->mm)
2218                         lpfc_issue_clear_la(phba, vport);
2219                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2220                                 "1311 fa %d\n", la->fa);
2221         }
2222
2223 lpfc_mbx_cmpl_read_la_free_mbuf:
2224         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2225         kfree(mp);
2226         mempool_free(pmb, phba->mbox_mem_pool);
2227         return;
2228 }
2229
2230 /*
2231  * This routine handles processing a REG_LOGIN mailbox
2232  * command upon completion. It is setup in the LPFC_MBOXQ
2233  * as the completion routine when the command is
2234  * handed off to the SLI layer.
2235  */
2236 void
2237 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2238 {
2239         struct lpfc_vport  *vport = pmb->vport;
2240         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2241         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2242
2243         pmb->context1 = NULL;
2244
2245         /* Good status, call state machine */
2246         lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
2247         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2248         kfree(mp);
2249         mempool_free(pmb, phba->mbox_mem_pool);
2250         /* decrement the node reference count held for this callback
2251          * function.
2252          */
2253         lpfc_nlp_put(ndlp);
2254
2255         return;
2256 }
2257
2258 static void
2259 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2260 {
2261         MAILBOX_t *mb = &pmb->u.mb;
2262         struct lpfc_vport *vport = pmb->vport;
2263         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2264
2265         switch (mb->mbxStatus) {
2266         case 0x0011:
2267         case 0x0020:
2268         case 0x9700:
2269                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2270                                  "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2271                                  mb->mbxStatus);
2272                 break;
2273         }
2274         spin_lock_irq(&phba->hbalock);
2275         vport->vpi_state &= ~LPFC_VPI_REGISTERED;
2276         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2277         spin_unlock_irq(&phba->hbalock);
2278         vport->unreg_vpi_cmpl = VPORT_OK;
2279         mempool_free(pmb, phba->mbox_mem_pool);
2280         /*
2281          * This shost reference might have been taken at the beginning of
2282          * lpfc_vport_delete()
2283          */
2284         if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
2285                 scsi_host_put(shost);
2286 }
2287
2288 int
2289 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2290 {
2291         struct lpfc_hba  *phba = vport->phba;
2292         LPFC_MBOXQ_t *mbox;
2293         int rc;
2294
2295         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2296         if (!mbox)
2297                 return 1;
2298
2299         lpfc_unreg_vpi(phba, vport->vpi, mbox);
2300         mbox->vport = vport;
2301         mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
2302         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2303         if (rc == MBX_NOT_FINISHED) {
2304                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2305                                  "1800 Could not issue unreg_vpi\n");
2306                 mempool_free(mbox, phba->mbox_mem_pool);
2307                 vport->unreg_vpi_cmpl = VPORT_ERROR;
2308                 return rc;
2309         }
2310         return 0;
2311 }
2312
2313 static void
2314 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2315 {
2316         struct lpfc_vport *vport = pmb->vport;
2317         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2318         MAILBOX_t *mb = &pmb->u.mb;
2319
2320         switch (mb->mbxStatus) {
2321         case 0x0011:
2322         case 0x9601:
2323         case 0x9602:
2324                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2325                                  "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2326                                  mb->mbxStatus);
2327                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2328                 spin_lock_irq(shost->host_lock);
2329                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2330                 spin_unlock_irq(shost->host_lock);
2331                 vport->fc_myDID = 0;
2332                 goto out;
2333         }
2334
2335         vport->vpi_state |= LPFC_VPI_REGISTERED;
2336         vport->num_disc_nodes = 0;
2337         /* go thru NPR list and issue ELS PLOGIs */
2338         if (vport->fc_npr_cnt)
2339                 lpfc_els_disc_plogi(vport);
2340
2341         if (!vport->num_disc_nodes) {
2342                 spin_lock_irq(shost->host_lock);
2343                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2344                 spin_unlock_irq(shost->host_lock);
2345                 lpfc_can_disctmo(vport);
2346         }
2347         vport->port_state = LPFC_VPORT_READY;
2348
2349 out:
2350         mempool_free(pmb, phba->mbox_mem_pool);
2351         return;
2352 }
2353
2354 /**
2355  * lpfc_create_static_vport - Read HBA config region to create static vports.
2356  * @phba: pointer to lpfc hba data structure.
2357  *
2358  * This routine issue a DUMP mailbox command for config region 22 to get
2359  * the list of static vports to be created. The function create vports
2360  * based on the information returned from the HBA.
2361  **/
2362 void
2363 lpfc_create_static_vport(struct lpfc_hba *phba)
2364 {
2365         LPFC_MBOXQ_t *pmb = NULL;
2366         MAILBOX_t *mb;
2367         struct static_vport_info *vport_info;
2368         int rc = 0, i;
2369         struct fc_vport_identifiers vport_id;
2370         struct fc_vport *new_fc_vport;
2371         struct Scsi_Host *shost;
2372         struct lpfc_vport *vport;
2373         uint16_t offset = 0;
2374         uint8_t *vport_buff;
2375         struct lpfc_dmabuf *mp;
2376         uint32_t byte_count = 0;
2377
2378         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2379         if (!pmb) {
2380                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2381                                 "0542 lpfc_create_static_vport failed to"
2382                                 " allocate mailbox memory\n");
2383                 return;
2384         }
2385
2386         mb = &pmb->u.mb;
2387
2388         vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2389         if (!vport_info) {
2390                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2391                                 "0543 lpfc_create_static_vport failed to"
2392                                 " allocate vport_info\n");
2393                 mempool_free(pmb, phba->mbox_mem_pool);
2394                 return;
2395         }
2396
2397         vport_buff = (uint8_t *) vport_info;
2398         do {
2399                 if (lpfc_dump_static_vport(phba, pmb, offset))
2400                         goto out;
2401
2402                 pmb->vport = phba->pport;
2403                 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2404
2405                 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2406                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2407                                 "0544 lpfc_create_static_vport failed to"
2408                                 " issue dump mailbox command ret 0x%x "
2409                                 "status 0x%x\n",
2410                                 rc, mb->mbxStatus);
2411                         goto out;
2412                 }
2413
2414                 if (phba->sli_rev == LPFC_SLI_REV4) {
2415                         byte_count = pmb->u.mqe.un.mb_words[5];
2416                         mp = (struct lpfc_dmabuf *) pmb->context2;
2417                         if (byte_count > sizeof(struct static_vport_info) -
2418                                         offset)
2419                                 byte_count = sizeof(struct static_vport_info)
2420                                         - offset;
2421                         memcpy(vport_buff + offset, mp->virt, byte_count);
2422                         offset += byte_count;
2423                 } else {
2424                         if (mb->un.varDmp.word_cnt >
2425                                 sizeof(struct static_vport_info) - offset)
2426                                 mb->un.varDmp.word_cnt =
2427                                         sizeof(struct static_vport_info)
2428                                                 - offset;
2429                         byte_count = mb->un.varDmp.word_cnt;
2430                         lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2431                                 vport_buff + offset,
2432                                 byte_count);
2433
2434                         offset += byte_count;
2435                 }
2436
2437         } while (byte_count &&
2438                 offset < sizeof(struct static_vport_info));
2439
2440
2441         if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2442                 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2443                         != VPORT_INFO_REV)) {
2444                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2445                         "0545 lpfc_create_static_vport bad"
2446                         " information header 0x%x 0x%x\n",
2447                         le32_to_cpu(vport_info->signature),
2448                         le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2449
2450                 goto out;
2451         }
2452
2453         shost = lpfc_shost_from_vport(phba->pport);
2454
2455         for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2456                 memset(&vport_id, 0, sizeof(vport_id));
2457                 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2458                 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2459                 if (!vport_id.port_name || !vport_id.node_name)
2460                         continue;
2461
2462                 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2463                 vport_id.vport_type = FC_PORTTYPE_NPIV;
2464                 vport_id.disable = false;
2465                 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2466
2467                 if (!new_fc_vport) {
2468                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2469                                 "0546 lpfc_create_static_vport failed to"
2470                                 " create vport\n");
2471                         continue;
2472                 }
2473
2474                 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2475                 vport->vport_flag |= STATIC_VPORT;
2476         }
2477
2478 out:
2479         kfree(vport_info);
2480         if (rc != MBX_TIMEOUT) {
2481                 if (pmb->context2) {
2482                         mp = (struct lpfc_dmabuf *) pmb->context2;
2483                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2484                         kfree(mp);
2485                 }
2486                 mempool_free(pmb, phba->mbox_mem_pool);
2487         }
2488
2489         return;
2490 }
2491
2492 /*
2493  * This routine handles processing a Fabric REG_LOGIN mailbox
2494  * command upon completion. It is setup in the LPFC_MBOXQ
2495  * as the completion routine when the command is
2496  * handed off to the SLI layer.
2497  */
2498 void
2499 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2500 {
2501         struct lpfc_vport *vport = pmb->vport;
2502         MAILBOX_t *mb = &pmb->u.mb;
2503         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2504         struct lpfc_nodelist *ndlp;
2505
2506         ndlp = (struct lpfc_nodelist *) pmb->context2;
2507         pmb->context1 = NULL;
2508         pmb->context2 = NULL;
2509         if (mb->mbxStatus) {
2510                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2511                                  "0258 Register Fabric login error: 0x%x\n",
2512                                  mb->mbxStatus);
2513                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2514                 kfree(mp);
2515                 mempool_free(pmb, phba->mbox_mem_pool);
2516
2517                 if (phba->fc_topology == TOPOLOGY_LOOP) {
2518                         /* FLOGI failed, use loop map to make discovery list */
2519                         lpfc_disc_list_loopmap(vport);
2520
2521                         /* Start discovery */
2522                         lpfc_disc_start(vport);
2523                         /* Decrement the reference count to ndlp after the
2524                          * reference to the ndlp are done.
2525                          */
2526                         lpfc_nlp_put(ndlp);
2527                         return;
2528                 }
2529
2530                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2531                 /* Decrement the reference count to ndlp after the reference
2532                  * to the ndlp are done.
2533                  */
2534                 lpfc_nlp_put(ndlp);
2535                 return;
2536         }
2537
2538         ndlp->nlp_rpi = mb->un.varWords[0];
2539         ndlp->nlp_flag |= NLP_RPI_VALID;
2540         ndlp->nlp_type |= NLP_FABRIC;
2541         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2542
2543         if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2544                 lpfc_start_fdiscs(phba);
2545                 lpfc_do_scr_ns_plogi(phba, vport);
2546         }
2547
2548         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2549         kfree(mp);
2550         mempool_free(pmb, phba->mbox_mem_pool);
2551
2552         /* Drop the reference count from the mbox at the end after
2553          * all the current reference to the ndlp have been done.
2554          */
2555         lpfc_nlp_put(ndlp);
2556         return;
2557 }
2558
2559 /*
2560  * This routine handles processing a NameServer REG_LOGIN mailbox
2561  * command upon completion. It is setup in the LPFC_MBOXQ
2562  * as the completion routine when the command is
2563  * handed off to the SLI layer.
2564  */
2565 void
2566 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2567 {
2568         MAILBOX_t *mb = &pmb->u.mb;
2569         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2570         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2571         struct lpfc_vport *vport = pmb->vport;
2572
2573         if (mb->mbxStatus) {
2574 out:
2575                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2576                                  "0260 Register NameServer error: 0x%x\n",
2577                                  mb->mbxStatus);
2578                 /* decrement the node reference count held for this
2579                  * callback function.
2580                  */
2581                 lpfc_nlp_put(ndlp);
2582                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2583                 kfree(mp);
2584                 mempool_free(pmb, phba->mbox_mem_pool);
2585
2586                 /* If no other thread is using the ndlp, free it */
2587                 lpfc_nlp_not_used(ndlp);
2588
2589                 if (phba->fc_topology == TOPOLOGY_LOOP) {
2590                         /*
2591                          * RegLogin failed, use loop map to make discovery
2592                          * list
2593                          */
2594                         lpfc_disc_list_loopmap(vport);
2595
2596                         /* Start discovery */
2597                         lpfc_disc_start(vport);
2598                         return;
2599                 }
2600                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2601                 return;
2602         }
2603
2604         pmb->context1 = NULL;
2605
2606         ndlp->nlp_rpi = mb->un.varWords[0];
2607         ndlp->nlp_flag |= NLP_RPI_VALID;
2608         ndlp->nlp_type |= NLP_FABRIC;
2609         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2610
2611         if (vport->port_state < LPFC_VPORT_READY) {
2612                 /* Link up discovery requires Fabric registration. */
2613                 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
2614                 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
2615                 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2616                 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
2617                 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
2618
2619                 /* Issue SCR just before NameServer GID_FT Query */
2620                 lpfc_issue_els_scr(vport, SCR_DID, 0);
2621         }
2622
2623         vport->fc_ns_retry = 0;
2624         /* Good status, issue CT Request to NameServer */
2625         if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
2626                 /* Cannot issue NameServer Query, so finish up discovery */
2627                 goto out;
2628         }
2629
2630         /* decrement the node reference count held for this
2631          * callback function.
2632          */
2633         lpfc_nlp_put(ndlp);
2634         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2635         kfree(mp);
2636         mempool_free(pmb, phba->mbox_mem_pool);
2637
2638         return;
2639 }
2640
2641 static void
2642 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2643 {
2644         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2645         struct fc_rport  *rport;
2646         struct lpfc_rport_data *rdata;
2647         struct fc_rport_identifiers rport_ids;
2648         struct lpfc_hba  *phba = vport->phba;
2649
2650         /* Remote port has reappeared. Re-register w/ FC transport */
2651         rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2652         rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
2653         rport_ids.port_id = ndlp->nlp_DID;
2654         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2655
2656         /*
2657          * We leave our node pointer in rport->dd_data when we unregister a
2658          * FCP target port.  But fc_remote_port_add zeros the space to which
2659          * rport->dd_data points.  So, if we're reusing a previously
2660          * registered port, drop the reference that we took the last time we
2661          * registered the port.
2662          */
2663         if (ndlp->rport && ndlp->rport->dd_data &&
2664             ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
2665                 lpfc_nlp_put(ndlp);
2666
2667         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
2668                 "rport add:       did:x%x flg:x%x type x%x",
2669                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2670
2671         ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
2672         if (!rport || !get_device(&rport->dev)) {
2673                 dev_printk(KERN_WARNING, &phba->pcidev->dev,
2674                            "Warning: fc_remote_port_add failed\n");
2675                 return;
2676         }
2677
2678         /* initialize static port data */
2679         rport->maxframe_size = ndlp->nlp_maxframe;
2680         rport->supported_classes = ndlp->nlp_class_sup;
2681         rdata = rport->dd_data;
2682         rdata->pnode = lpfc_nlp_get(ndlp);
2683
2684         if (ndlp->nlp_type & NLP_FCP_TARGET)
2685                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2686         if (ndlp->nlp_type & NLP_FCP_INITIATOR)
2687                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2688
2689
2690         if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
2691                 fc_remote_port_rolechg(rport, rport_ids.roles);
2692
2693         if ((rport->scsi_target_id != -1) &&
2694             (rport->scsi_target_id < LPFC_MAX_TARGET)) {
2695                 ndlp->nlp_sid = rport->scsi_target_id;
2696         }
2697         return;
2698 }
2699
2700 static void
2701 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
2702 {
2703         struct fc_rport *rport = ndlp->rport;
2704
2705         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
2706                 "rport delete:    did:x%x flg:x%x type x%x",
2707                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2708
2709         fc_remote_port_delete(rport);
2710
2711         return;
2712 }
2713
2714 static void
2715 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
2716 {
2717         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2718
2719         spin_lock_irq(shost->host_lock);
2720         switch (state) {
2721         case NLP_STE_UNUSED_NODE:
2722                 vport->fc_unused_cnt += count;
2723                 break;
2724         case NLP_STE_PLOGI_ISSUE:
2725                 vport->fc_plogi_cnt += count;
2726                 break;
2727         case NLP_STE_ADISC_ISSUE:
2728                 vport->fc_adisc_cnt += count;
2729                 break;
2730         case NLP_STE_REG_LOGIN_ISSUE:
2731                 vport->fc_reglogin_cnt += count;
2732                 break;
2733         case NLP_STE_PRLI_ISSUE:
2734                 vport->fc_prli_cnt += count;
2735                 break;
2736         case NLP_STE_UNMAPPED_NODE:
2737                 vport->fc_unmap_cnt += count;
2738                 break;
2739         case NLP_STE_MAPPED_NODE:
2740                 vport->fc_map_cnt += count;
2741                 break;
2742         case NLP_STE_NPR_NODE:
2743                 vport->fc_npr_cnt += count;
2744                 break;
2745         }
2746         spin_unlock_irq(shost->host_lock);
2747 }
2748
2749 static void
2750 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2751                        int old_state, int new_state)
2752 {
2753         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2754
2755         if (new_state == NLP_STE_UNMAPPED_NODE) {
2756                 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2757                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2758                 ndlp->nlp_type |= NLP_FC_NODE;
2759         }
2760         if (new_state == NLP_STE_MAPPED_NODE)
2761                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2762         if (new_state == NLP_STE_NPR_NODE)
2763                 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
2764
2765         /* Transport interface */
2766         if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
2767                             old_state == NLP_STE_UNMAPPED_NODE)) {
2768                 vport->phba->nport_event_cnt++;
2769                 lpfc_unregister_remote_port(ndlp);
2770         }
2771
2772         if (new_state ==  NLP_STE_MAPPED_NODE ||
2773             new_state == NLP_STE_UNMAPPED_NODE) {
2774                 vport->phba->nport_event_cnt++;
2775                 /*
2776                  * Tell the fc transport about the port, if we haven't
2777                  * already. If we have, and it's a scsi entity, be
2778                  * sure to unblock any attached scsi devices
2779                  */
2780                 lpfc_register_remote_port(vport, ndlp);
2781         }
2782         if ((new_state ==  NLP_STE_MAPPED_NODE) &&
2783                 (vport->stat_data_enabled)) {
2784                 /*
2785                  * A new target is discovered, if there is no buffer for
2786                  * statistical data collection allocate buffer.
2787                  */
2788                 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
2789                                          sizeof(struct lpfc_scsicmd_bkt),
2790                                          GFP_KERNEL);
2791
2792                 if (!ndlp->lat_data)
2793                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2794                                 "0286 lpfc_nlp_state_cleanup failed to "
2795                                 "allocate statistical data buffer DID "
2796                                 "0x%x\n", ndlp->nlp_DID);
2797         }
2798         /*
2799          * if we added to Mapped list, but the remote port
2800          * registration failed or assigned a target id outside
2801          * our presentable range - move the node to the
2802          * Unmapped List
2803          */
2804         if (new_state == NLP_STE_MAPPED_NODE &&
2805             (!ndlp->rport ||
2806              ndlp->rport->scsi_target_id == -1 ||
2807              ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
2808                 spin_lock_irq(shost->host_lock);
2809                 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
2810                 spin_unlock_irq(shost->host_lock);
2811                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2812         }
2813 }
2814
2815 static char *
2816 lpfc_nlp_state_name(char *buffer, size_t size, int state)
2817 {
2818         static char *states[] = {
2819                 [NLP_STE_UNUSED_NODE] = "UNUSED",
2820                 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
2821                 [NLP_STE_ADISC_ISSUE] = "ADISC",
2822                 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
2823                 [NLP_STE_PRLI_ISSUE] = "PRLI",
2824                 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
2825                 [NLP_STE_MAPPED_NODE] = "MAPPED",
2826                 [NLP_STE_NPR_NODE] = "NPR",
2827         };
2828
2829         if (state < NLP_STE_MAX_STATE && states[state])
2830                 strlcpy(buffer, states[state], size);
2831         else
2832                 snprintf(buffer, size, "unknown (%d)", state);
2833         return buffer;
2834 }
2835
2836 void
2837 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2838                    int state)
2839 {
2840         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2841         int  old_state = ndlp->nlp_state;
2842         char name1[16], name2[16];
2843
2844         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2845                          "0904 NPort state transition x%06x, %s -> %s\n",
2846                          ndlp->nlp_DID,
2847                          lpfc_nlp_state_name(name1, sizeof(name1), old_state),
2848                          lpfc_nlp_state_name(name2, sizeof(name2), state));
2849
2850         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2851                 "node statechg    did:x%x old:%d ste:%d",
2852                 ndlp->nlp_DID, old_state, state);
2853
2854         if (old_state == NLP_STE_NPR_NODE &&
2855             state != NLP_STE_NPR_NODE)
2856                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2857         if (old_state == NLP_STE_UNMAPPED_NODE) {
2858                 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
2859                 ndlp->nlp_type &= ~NLP_FC_NODE;
2860         }
2861
2862         if (list_empty(&ndlp->nlp_listp)) {
2863                 spin_lock_irq(shost->host_lock);
2864                 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2865                 spin_unlock_irq(shost->host_lock);
2866         } else if (old_state)
2867                 lpfc_nlp_counters(vport, old_state, -1);
2868
2869         ndlp->nlp_state = state;
2870         lpfc_nlp_counters(vport, state, 1);
2871         lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
2872 }
2873
2874 void
2875 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2876 {
2877         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2878
2879         if (list_empty(&ndlp->nlp_listp)) {
2880                 spin_lock_irq(shost->host_lock);
2881                 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2882                 spin_unlock_irq(shost->host_lock);
2883         }
2884 }
2885
2886 void
2887 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2888 {
2889         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2890
2891         lpfc_cancel_retry_delay_tmo(vport, ndlp);
2892         if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2893                 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2894         spin_lock_irq(shost->host_lock);
2895         list_del_init(&ndlp->nlp_listp);
2896         spin_unlock_irq(shost->host_lock);
2897         lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2898                                 NLP_STE_UNUSED_NODE);
2899 }
2900
2901 static void
2902 lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2903 {
2904         lpfc_cancel_retry_delay_tmo(vport, ndlp);
2905         if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2906                 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2907         lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2908                                 NLP_STE_UNUSED_NODE);
2909 }
2910 /**
2911  * lpfc_initialize_node - Initialize all fields of node object
2912  * @vport: Pointer to Virtual Port object.
2913  * @ndlp: Pointer to FC node object.
2914  * @did: FC_ID of the node.
2915  *
2916  * This function is always called when node object need to be initialized.
2917  * It initializes all the fields of the node object. Although the reference
2918  * to phba from @ndlp can be obtained indirectly through it's reference to
2919  * @vport, a direct reference to phba is taken here by @ndlp. This is due
2920  * to the life-span of the @ndlp might go beyond the existence of @vport as
2921  * the final release of ndlp is determined by its reference count. And, the
2922  * operation on @ndlp needs the reference to phba.
2923  **/
2924 static inline void
2925 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2926         uint32_t did)
2927 {
2928         INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2929         INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2930         init_timer(&ndlp->nlp_delayfunc);
2931         ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2932         ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2933         ndlp->nlp_DID = did;
2934         ndlp->vport = vport;
2935         ndlp->phba = vport->phba;
2936         ndlp->nlp_sid = NLP_NO_SID;
2937         kref_init(&ndlp->kref);
2938         NLP_INT_NODE_ACT(ndlp);
2939         atomic_set(&ndlp->cmd_pending, 0);
2940         ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2941 }
2942
2943 struct lpfc_nodelist *
2944 lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2945                  int state)
2946 {
2947         struct lpfc_hba *phba = vport->phba;
2948         uint32_t did;
2949         unsigned long flags;
2950
2951         if (!ndlp)
2952                 return NULL;
2953
2954         spin_lock_irqsave(&phba->ndlp_lock, flags);
2955         /* The ndlp should not be in memory free mode */
2956         if (NLP_CHK_FREE_REQ(ndlp)) {
2957                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2958                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2959                                 "0277 lpfc_enable_node: ndlp:x%p "
2960                                 "usgmap:x%x refcnt:%d\n",
2961                                 (void *)ndlp, ndlp->nlp_usg_map,
2962                                 atomic_read(&ndlp->kref.refcount));
2963                 return NULL;
2964         }
2965         /* The ndlp should not already be in active mode */
2966         if (NLP_CHK_NODE_ACT(ndlp)) {
2967                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2968                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2969                                 "0278 lpfc_enable_node: ndlp:x%p "
2970                                 "usgmap:x%x refcnt:%d\n",
2971                                 (void *)ndlp, ndlp->nlp_usg_map,
2972                                 atomic_read(&ndlp->kref.refcount));
2973                 return NULL;
2974         }
2975
2976         /* Keep the original DID */
2977         did = ndlp->nlp_DID;
2978
2979         /* re-initialize ndlp except of ndlp linked list pointer */
2980         memset((((char *)ndlp) + sizeof (struct list_head)), 0,
2981                 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
2982         lpfc_initialize_node(vport, ndlp, did);
2983
2984         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2985
2986         if (state != NLP_STE_UNUSED_NODE)
2987                 lpfc_nlp_set_state(vport, ndlp, state);
2988
2989         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2990                 "node enable:       did:x%x",
2991                 ndlp->nlp_DID, 0, 0);
2992         return ndlp;
2993 }
2994
2995 void
2996 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2997 {
2998         /*
2999          * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
3000          * be used if we wish to issue the "last" lpfc_nlp_put() to remove
3001          * the ndlp from the vport. The ndlp marked as UNUSED on the list
3002          * until ALL other outstanding threads have completed. We check
3003          * that the ndlp not already in the UNUSED state before we proceed.
3004          */
3005         if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3006                 return;
3007         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
3008         lpfc_nlp_put(ndlp);
3009         return;
3010 }
3011
3012 /*
3013  * Start / ReStart rescue timer for Discovery / RSCN handling
3014  */
3015 void
3016 lpfc_set_disctmo(struct lpfc_vport *vport)
3017 {
3018         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3019         struct lpfc_hba  *phba = vport->phba;
3020         uint32_t tmo;
3021
3022         if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
3023                 /* For FAN, timeout should be greater than edtov */
3024                 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3025         } else {
3026                 /* Normal discovery timeout should be > than ELS/CT timeout
3027                  * FC spec states we need 3 * ratov for CT requests
3028                  */
3029                 tmo = ((phba->fc_ratov * 3) + 3);
3030         }
3031
3032
3033         if (!timer_pending(&vport->fc_disctmo)) {
3034                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3035                         "set disc timer:  tmo:x%x state:x%x flg:x%x",
3036                         tmo, vport->port_state, vport->fc_flag);
3037         }
3038
3039         mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3040         spin_lock_irq(shost->host_lock);
3041         vport->fc_flag |= FC_DISC_TMO;
3042         spin_unlock_irq(shost->host_lock);
3043
3044         /* Start Discovery Timer state <hba_state> */
3045         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,