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