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