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