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