]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/scsi/lpfc/lpfc_hbadisc.c
Merge branch 'x86-mrst-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mv-sheeva.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         LPFC_MBOXQ_t *mbox;
1485         int rc;
1486         /*
1487          * If the Link is up and no FCoE events while in the
1488          * FCF discovery, no need to restart FCF discovery.
1489          */
1490         if ((phba->link_state  >= LPFC_LINK_UP) &&
1491                 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1492                 return 0;
1493
1494         spin_lock_irq(&phba->hbalock);
1495         phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1496         spin_unlock_irq(&phba->hbalock);
1497
1498         if (phba->link_state >= LPFC_LINK_UP)
1499                 lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
1500         else {
1501                 /*
1502                  * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1503                  * flag
1504                  */
1505                 spin_lock_irq(&phba->hbalock);
1506                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1507                 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
1508                 spin_unlock_irq(&phba->hbalock);
1509         }
1510
1511         if (unreg_fcf) {
1512                 spin_lock_irq(&phba->hbalock);
1513                 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1514                 spin_unlock_irq(&phba->hbalock);
1515                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1516                 if (!mbox) {
1517                         lpfc_printf_log(phba, KERN_ERR,
1518                                 LOG_DISCOVERY|LOG_MBOX,
1519                                 "2610 UNREG_FCFI mbox allocation failed\n");
1520                         return 1;
1521                 }
1522                 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
1523                 mbox->vport = phba->pport;
1524                 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
1525                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1526                 if (rc == MBX_NOT_FINISHED) {
1527                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
1528                                 "2611 UNREG_FCFI issue mbox failed\n");
1529                         mempool_free(mbox, phba->mbox_mem_pool);
1530                 }
1531         }
1532
1533         return 1;
1534 }
1535
1536 /**
1537  * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
1538  * @phba: pointer to lpfc hba data structure.
1539  * @mboxq: pointer to mailbox object.
1540  *
1541  * This function iterate through all the fcf records available in
1542  * HBA and choose the optimal FCF record for discovery. After finding
1543  * the FCF for discovery it register the FCF record and kick start
1544  * discovery.
1545  * If FCF_IN_USE flag is set in currently used FCF, the routine try to
1546  * use a FCF record which match fabric name and mac address of the
1547  * currently used FCF record.
1548  * If the driver support only one FCF, it will try to use the FCF record
1549  * used by BOOT_BIOS.
1550  */
1551 void
1552 lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1553 {
1554         void *virt_addr;
1555         dma_addr_t phys_addr;
1556         uint8_t *bytep;
1557         struct lpfc_mbx_sge sge;
1558         struct lpfc_mbx_read_fcf_tbl *read_fcf;
1559         uint32_t shdr_status, shdr_add_status;
1560         union lpfc_sli4_cfg_shdr *shdr;
1561         struct fcf_record *new_fcf_record;
1562         uint32_t boot_flag, addr_mode;
1563         uint32_t next_fcf_index;
1564         struct lpfc_fcf_rec *fcf_rec = NULL;
1565         unsigned long iflags;
1566         uint16_t vlan_id;
1567         int rc;
1568
1569         /* If there is pending FCoE event restart FCF table scan */
1570         if (lpfc_check_pending_fcoe_event(phba, 0)) {
1571                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1572                 return;
1573         }
1574
1575         /* Get the first SGE entry from the non-embedded DMA memory. This
1576          * routine only uses a single SGE.
1577          */
1578         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1579         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1580         if (unlikely(!mboxq->sge_array)) {
1581                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1582                                 "2524 Failed to get the non-embedded SGE "
1583                                 "virtual address\n");
1584                 goto out;
1585         }
1586         virt_addr = mboxq->sge_array->addr[0];
1587
1588         shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1589         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1590         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
1591                                  &shdr->response);
1592         /*
1593          * The FCF Record was read and there is no reason for the driver
1594          * to maintain the FCF record data or memory. Instead, just need
1595          * to book keeping the FCFIs can be used.
1596          */
1597         if (shdr_status || shdr_add_status) {
1598                 if (shdr_status == STATUS_FCF_TABLE_EMPTY) {
1599                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1600                                         "2726 READ_FCF_RECORD Indicates empty "
1601                                         "FCF table.\n");
1602                 } else {
1603                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1604                                         "2521 READ_FCF_RECORD mailbox failed "
1605                                         "with status x%x add_status x%x, mbx\n",
1606                                         shdr_status, shdr_add_status);
1607                 }
1608                 goto out;
1609         }
1610         /* Interpreting the returned information of FCF records */
1611         read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1612         lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1613                               sizeof(struct lpfc_mbx_read_fcf_tbl));
1614         next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1615
1616         new_fcf_record = (struct fcf_record *)(virt_addr +
1617                           sizeof(struct lpfc_mbx_read_fcf_tbl));
1618         lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1619                               sizeof(struct fcf_record));
1620         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1621
1622         rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1623                                       &addr_mode, &vlan_id);
1624         /*
1625          * If the fcf record does not match with connect list entries
1626          * read the next entry.
1627          */
1628         if (!rc)
1629                 goto read_next_fcf;
1630         /*
1631          * If this is not the first FCF discovery of the HBA, use last
1632          * FCF record for the discovery. The condition that a rescan
1633          * matches the in-use FCF record: fabric name, switch name, mac
1634          * address, and vlan_id.
1635          */
1636         spin_lock_irqsave(&phba->hbalock, iflags);
1637         if (phba->fcf.fcf_flag & FCF_IN_USE) {
1638                 if (lpfc_fab_name_match(phba->fcf.current_rec.fabric_name,
1639                                         new_fcf_record) &&
1640                     lpfc_sw_name_match(phba->fcf.current_rec.switch_name,
1641                                         new_fcf_record) &&
1642                     lpfc_mac_addr_match(phba->fcf.current_rec.mac_addr,
1643                                         new_fcf_record) &&
1644                     lpfc_vlan_id_match(phba->fcf.current_rec.vlan_id,
1645                                         vlan_id)) {
1646                         phba->fcf.fcf_flag |= FCF_AVAILABLE;
1647                         if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
1648                                 /* Stop FCF redisc wait timer if pending */
1649                                 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
1650                         else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1651                                 /* If in fast failover, mark it's completed */
1652                                 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
1653                         spin_unlock_irqrestore(&phba->hbalock, iflags);
1654                         goto out;
1655                 }
1656                 /*
1657                  * Read next FCF record from HBA searching for the matching
1658                  * with in-use record only if not during the fast failover
1659                  * period. In case of fast failover period, it shall try to
1660                  * determine whether the FCF record just read should be the
1661                  * next candidate.
1662                  */
1663                 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
1664                         spin_unlock_irqrestore(&phba->hbalock, iflags);
1665                         goto read_next_fcf;
1666                 }
1667         }
1668         /*
1669          * Update on failover FCF record only if it's in FCF fast-failover
1670          * period; otherwise, update on current FCF record.
1671          */
1672         if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
1673                 /* Fast FCF failover only to the same fabric name */
1674                 if (lpfc_fab_name_match(phba->fcf.current_rec.fabric_name,
1675                                         new_fcf_record))
1676                         fcf_rec = &phba->fcf.failover_rec;
1677                 else
1678                         goto read_next_fcf;
1679         } else
1680                 fcf_rec = &phba->fcf.current_rec;
1681
1682         if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1683                 /*
1684                  * If the driver FCF record does not have boot flag
1685                  * set and new hba fcf record has boot flag set, use
1686                  * the new hba fcf record.
1687                  */
1688                 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
1689                         /* Choose this FCF record */
1690                         __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1691                                         addr_mode, vlan_id, BOOT_ENABLE);
1692                         spin_unlock_irqrestore(&phba->hbalock, iflags);
1693                         goto read_next_fcf;
1694                 }
1695                 /*
1696                  * If the driver FCF record has boot flag set and the
1697                  * new hba FCF record does not have boot flag, read
1698                  * the next FCF record.
1699                  */
1700                 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
1701                         spin_unlock_irqrestore(&phba->hbalock, iflags);
1702                         goto read_next_fcf;
1703                 }
1704                 /*
1705                  * If the new hba FCF record has lower priority value
1706                  * than the driver FCF record, use the new record.
1707                  */
1708                 if (lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record) &&
1709                     (new_fcf_record->fip_priority < fcf_rec->priority)) {
1710                         /* Choose this FCF record */
1711                         __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1712                                         addr_mode, vlan_id, 0);
1713                 }
1714                 spin_unlock_irqrestore(&phba->hbalock, iflags);
1715                 goto read_next_fcf;
1716         }
1717         /*
1718          * This is the first suitable FCF record, choose this record for
1719          * initial best-fit FCF.
1720          */
1721         if (fcf_rec) {
1722                 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1723                                          addr_mode, vlan_id, (boot_flag ?
1724                                          BOOT_ENABLE : 0));
1725                 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1726         }
1727         spin_unlock_irqrestore(&phba->hbalock, iflags);
1728         goto read_next_fcf;
1729
1730 read_next_fcf:
1731         lpfc_sli4_mbox_cmd_free(phba, mboxq);
1732         if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
1733                 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
1734                         /*
1735                          * Case of FCF fast failover scan
1736                          */
1737
1738                         /*
1739                          * It has not found any suitable FCF record, cancel
1740                          * FCF scan inprogress, and do nothing
1741                          */
1742                         if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
1743                                 spin_lock_irqsave(&phba->hbalock, iflags);
1744                                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1745                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
1746                                 return;
1747                         }
1748                         /*
1749                          * It has found a suitable FCF record that is not
1750                          * the same as in-use FCF record, unregister the
1751                          * in-use FCF record, replace the in-use FCF record
1752                          * with the new FCF record, mark FCF fast failover
1753                          * completed, and then start register the new FCF
1754                          * record.
1755                          */
1756
1757                         /* unregister the current in-use FCF record */
1758                         lpfc_unregister_fcf(phba);
1759                         /* replace in-use record with the new record */
1760                         memcpy(&phba->fcf.current_rec,
1761                                &phba->fcf.failover_rec,
1762                                sizeof(struct lpfc_fcf_rec));
1763                         /* mark the FCF fast failover completed */
1764                         spin_lock_irqsave(&phba->hbalock, iflags);
1765                         phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
1766                         spin_unlock_irqrestore(&phba->hbalock, iflags);
1767                         /* Register to the new FCF record */
1768                         lpfc_register_fcf(phba);
1769                 } else {
1770                         /*
1771                          * In case of transaction period to fast FCF failover,
1772                          * do nothing when search to the end of the FCF table.
1773                          */
1774                         if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
1775                             (phba->fcf.fcf_flag & FCF_REDISC_PEND))
1776                                 return;
1777                         /*
1778                          * Otherwise, initial scan or post linkdown rescan,
1779                          * register with the best fit FCF record found so
1780                          * far through the scanning process.
1781                          */
1782                         lpfc_register_fcf(phba);
1783                 }
1784         } else
1785                 lpfc_sli4_read_fcf_record(phba, next_fcf_index);
1786         return;
1787
1788 out:
1789         lpfc_sli4_mbox_cmd_free(phba, mboxq);
1790         lpfc_register_fcf(phba);
1791
1792         return;
1793 }
1794
1795 /**
1796  * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
1797  * @phba: pointer to lpfc hba data structure.
1798  * @mboxq: pointer to mailbox data structure.
1799  *
1800  * This function handles completion of init vpi mailbox command.
1801  */
1802 void
1803 lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1804 {
1805         struct lpfc_vport *vport = mboxq->vport;
1806         struct lpfc_nodelist *ndlp;
1807         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1808
1809         if (mboxq->u.mb.mbxStatus) {
1810                 lpfc_printf_vlog(vport, KERN_ERR,
1811                                 LOG_MBOX,
1812                                 "2609 Init VPI mailbox failed 0x%x\n",
1813                                 mboxq->u.mb.mbxStatus);
1814                 mempool_free(mboxq, phba->mbox_mem_pool);
1815                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1816                 return;
1817         }
1818         spin_lock_irq(shost->host_lock);
1819         vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
1820         spin_unlock_irq(shost->host_lock);
1821
1822         /* If this port is physical port or FDISC is done, do reg_vpi */
1823         if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
1824                         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1825                         if (!ndlp)
1826                                 lpfc_printf_vlog(vport, KERN_ERR,
1827                                         LOG_DISCOVERY,
1828                                         "2731 Cannot find fabric "
1829                                         "controller node\n");
1830                         else
1831                                 lpfc_register_new_vport(phba, vport, ndlp);
1832                         mempool_free(mboxq, phba->mbox_mem_pool);
1833                         return;
1834         }
1835
1836         if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1837                 lpfc_initial_fdisc(vport);
1838         else {
1839                 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
1840                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1841                                  "2606 No NPIV Fabric support\n");
1842         }
1843         mempool_free(mboxq, phba->mbox_mem_pool);
1844         return;
1845 }
1846
1847 /**
1848  * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
1849  * @vport: pointer to lpfc_vport data structure.
1850  *
1851  * This function issue a init_vpi mailbox command to initialize
1852  * VPI for the vport.
1853  */
1854 void
1855 lpfc_issue_init_vpi(struct lpfc_vport *vport)
1856 {
1857         LPFC_MBOXQ_t *mboxq;
1858         int rc;
1859
1860         mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
1861         if (!mboxq) {
1862                 lpfc_printf_vlog(vport, KERN_ERR,
1863                         LOG_MBOX, "2607 Failed to allocate "
1864                         "init_vpi mailbox\n");
1865                 return;
1866         }
1867         lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
1868         mboxq->vport = vport;
1869         mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
1870         rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
1871         if (rc == MBX_NOT_FINISHED) {
1872                 lpfc_printf_vlog(vport, KERN_ERR,
1873                         LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
1874                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
1875         }
1876 }
1877
1878 /**
1879  * lpfc_start_fdiscs - send fdiscs for each vports on this port.
1880  * @phba: pointer to lpfc hba data structure.
1881  *
1882  * This function loops through the list of vports on the @phba and issues an
1883  * FDISC if possible.
1884  */
1885 void
1886 lpfc_start_fdiscs(struct lpfc_hba *phba)
1887 {
1888         struct lpfc_vport **vports;
1889         int i;
1890
1891         vports = lpfc_create_vport_work_array(phba);
1892         if (vports != NULL) {
1893                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1894                         if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1895                                 continue;
1896                         /* There are no vpi for this vport */
1897                         if (vports[i]->vpi > phba->max_vpi) {
1898                                 lpfc_vport_set_state(vports[i],
1899                                                      FC_VPORT_FAILED);
1900                                 continue;
1901                         }
1902                         if (phba->fc_topology == TOPOLOGY_LOOP) {
1903                                 lpfc_vport_set_state(vports[i],
1904                                                      FC_VPORT_LINKDOWN);
1905                                 continue;
1906                         }
1907                         if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
1908                                 lpfc_issue_init_vpi(vports[i]);
1909                                 continue;
1910                         }
1911                         if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1912                                 lpfc_initial_fdisc(vports[i]);
1913                         else {
1914                                 lpfc_vport_set_state(vports[i],
1915                                                      FC_VPORT_NO_FABRIC_SUPP);
1916                                 lpfc_printf_vlog(vports[i], KERN_ERR,
1917                                                  LOG_ELS,
1918                                                  "0259 No NPIV "
1919                                                  "Fabric support\n");
1920                         }
1921                 }
1922         }
1923         lpfc_destroy_vport_work_array(phba, vports);
1924 }
1925
1926 void
1927 lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1928 {
1929         struct lpfc_dmabuf *dmabuf = mboxq->context1;
1930         struct lpfc_vport *vport = mboxq->vport;
1931         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1932
1933         if (mboxq->u.mb.mbxStatus) {
1934                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1935                          "2018 REG_VFI mbxStatus error x%x "
1936                          "HBA state x%x\n",
1937                          mboxq->u.mb.mbxStatus, vport->port_state);
1938                 if (phba->fc_topology == TOPOLOGY_LOOP) {
1939                         /* FLOGI failed, use loop map to make discovery list */
1940                         lpfc_disc_list_loopmap(vport);
1941                         /* Start discovery */
1942                         lpfc_disc_start(vport);
1943                         goto fail_free_mem;
1944                 }
1945                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1946                 goto fail_free_mem;
1947         }
1948         /* The VPI is implicitly registered when the VFI is registered */
1949         spin_lock_irq(shost->host_lock);
1950         vport->vpi_state |= LPFC_VPI_REGISTERED;
1951         vport->fc_flag |= FC_VFI_REGISTERED;
1952         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
1953         spin_unlock_irq(shost->host_lock);
1954
1955         if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1956                 lpfc_start_fdiscs(phba);
1957                 lpfc_do_scr_ns_plogi(phba, vport);
1958         }
1959
1960 fail_free_mem:
1961         mempool_free(mboxq, phba->mbox_mem_pool);
1962         lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1963         kfree(dmabuf);
1964         return;
1965 }
1966
1967 static void
1968 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1969 {
1970         MAILBOX_t *mb = &pmb->u.mb;
1971         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
1972         struct lpfc_vport  *vport = pmb->vport;
1973
1974
1975         /* Check for error */
1976         if (mb->mbxStatus) {
1977                 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
1978                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1979                                  "0319 READ_SPARAM mbxStatus error x%x "
1980                                  "hba state x%x>\n",
1981                                  mb->mbxStatus, vport->port_state);
1982                 lpfc_linkdown(phba);
1983                 goto out;
1984         }
1985
1986         memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
1987                sizeof (struct serv_parm));
1988         if (phba->cfg_soft_wwnn)
1989                 u64_to_wwn(phba->cfg_soft_wwnn,
1990                            vport->fc_sparam.nodeName.u.wwn);
1991         if (phba->cfg_soft_wwpn)
1992                 u64_to_wwn(phba->cfg_soft_wwpn,
1993                            vport->fc_sparam.portName.u.wwn);
1994         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1995                sizeof(vport->fc_nodename));
1996         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1997                sizeof(vport->fc_portname));
1998         if (vport->port_type == LPFC_PHYSICAL_PORT) {
1999                 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2000                 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2001         }
2002
2003         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2004         kfree(mp);
2005         mempool_free(pmb, phba->mbox_mem_pool);
2006         return;
2007
2008 out:
2009         pmb->context1 = NULL;
2010         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2011         kfree(mp);
2012         lpfc_issue_clear_la(phba, vport);
2013         mempool_free(pmb, phba->mbox_mem_pool);
2014         return;
2015 }
2016
2017 static void
2018 lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
2019 {
2020         struct lpfc_vport *vport = phba->pport;
2021         LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
2022         int i;
2023         struct lpfc_dmabuf *mp;
2024         int rc;
2025         struct fcf_record *fcf_record;
2026
2027         sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2028
2029         spin_lock_irq(&phba->hbalock);
2030         switch (la->UlnkSpeed) {
2031         case LA_1GHZ_LINK:
2032                 phba->fc_linkspeed = LA_1GHZ_LINK;
2033                 break;
2034         case LA_2GHZ_LINK:
2035                 phba->fc_linkspeed = LA_2GHZ_LINK;
2036                 break;
2037         case LA_4GHZ_LINK:
2038                 phba->fc_linkspeed = LA_4GHZ_LINK;
2039                 break;
2040         case LA_8GHZ_LINK:
2041                 phba->fc_linkspeed = LA_8GHZ_LINK;
2042                 break;
2043         case LA_10GHZ_LINK:
2044                 phba->fc_linkspeed = LA_10GHZ_LINK;
2045                 break;
2046         default:
2047                 phba->fc_linkspeed = LA_UNKNW_LINK;
2048                 break;
2049         }
2050
2051         phba->fc_topology = la->topology;
2052         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
2053
2054         if (phba->fc_topology == TOPOLOGY_LOOP) {
2055                 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
2056
2057                 /* if npiv is enabled and this adapter supports npiv log
2058                  * a message that npiv is not supported in this topology
2059                  */
2060                 if (phba->cfg_enable_npiv && phba->max_vpi)
2061                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2062                                 "1309 Link Up Event npiv not supported in loop "
2063                                 "topology\n");
2064                                 /* Get Loop Map information */
2065                 if (la->il)
2066                         vport->fc_flag |= FC_LBIT;
2067
2068                 vport->fc_myDID = la->granted_AL_PA;
2069                 i = la->un.lilpBde64.tus.f.bdeSize;
2070
2071                 if (i == 0) {
2072                         phba->alpa_map[0] = 0;
2073                 } else {
2074                         if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
2075                                 int numalpa, j, k;
2076                                 union {
2077                                         uint8_t pamap[16];
2078                                         struct {
2079                                                 uint32_t wd1;
2080                                                 uint32_t wd2;
2081                                                 uint32_t wd3;
2082                                                 uint32_t wd4;
2083                                         } pa;
2084                                 } un;
2085                                 numalpa = phba->alpa_map[0];
2086                                 j = 0;
2087                                 while (j < numalpa) {
2088                                         memset(un.pamap, 0, 16);
2089                                         for (k = 1; j < numalpa; k++) {
2090                                                 un.pamap[k - 1] =
2091                                                         phba->alpa_map[j + 1];
2092                                                 j++;
2093                                                 if (k == 16)
2094                                                         break;
2095                                         }
2096                                         /* Link Up Event ALPA map */
2097                                         lpfc_printf_log(phba,
2098                                                         KERN_WARNING,
2099                                                         LOG_LINK_EVENT,
2100                                                         "1304 Link Up Event "
2101                                                         "ALPA map Data: x%x "
2102                                                         "x%x x%x x%x\n",
2103                                                         un.pa.wd1, un.pa.wd2,
2104                                                         un.pa.wd3, un.pa.wd4);
2105                                 }
2106                         }
2107                 }
2108         } else {
2109                 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
2110                         if (phba->max_vpi && phba->cfg_enable_npiv &&
2111                            (phba->sli_rev == 3))
2112                                 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
2113                 }
2114                 vport->fc_myDID = phba->fc_pref_DID;
2115                 vport->fc_flag |= FC_LBIT;
2116         }
2117         spin_unlock_irq(&phba->hbalock);
2118
2119         lpfc_linkup(phba);
2120         if (sparam_mbox) {
2121                 lpfc_read_sparam(phba, sparam_mbox, 0);
2122                 sparam_mbox->vport = vport;
2123                 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
2124                 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
2125                 if (rc == MBX_NOT_FINISHED) {
2126                         mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2127                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2128                         kfree(mp);
2129                         mempool_free(sparam_mbox, phba->mbox_mem_pool);
2130                         goto out;
2131                 }
2132         }
2133
2134         if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2135                 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2136                 if (!cfglink_mbox)
2137                         goto out;
2138                 vport->port_state = LPFC_LOCAL_CFG_LINK;
2139                 lpfc_config_link(phba, cfglink_mbox);
2140                 cfglink_mbox->vport = vport;
2141                 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
2142                 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
2143                 if (rc == MBX_NOT_FINISHED) {
2144                         mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2145                         goto out;
2146                 }
2147         } else {
2148                 vport->port_state = LPFC_VPORT_UNKNOWN;
2149                 /*
2150                  * Add the driver's default FCF record at FCF index 0 now. This
2151                  * is phase 1 implementation that support FCF index 0 and driver
2152                  * defaults.
2153                  */
2154                 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
2155                         fcf_record = kzalloc(sizeof(struct fcf_record),
2156                                         GFP_KERNEL);
2157                         if (unlikely(!fcf_record)) {
2158                                 lpfc_printf_log(phba, KERN_ERR,
2159                                         LOG_MBOX | LOG_SLI,
2160                                         "2554 Could not allocate memmory for "
2161                                         "fcf record\n");
2162                                 rc = -ENODEV;
2163                                 goto out;
2164                         }
2165
2166                         lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2167                                                 LPFC_FCOE_FCF_DEF_INDEX);
2168                         rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2169                         if (unlikely(rc)) {
2170                                 lpfc_printf_log(phba, KERN_ERR,
2171                                         LOG_MBOX | LOG_SLI,
2172                                         "2013 Could not manually add FCF "
2173                                         "record 0, status %d\n", rc);
2174                                 rc = -ENODEV;
2175                                 kfree(fcf_record);
2176                                 goto out;
2177                         }
2178                         kfree(fcf_record);
2179                 }
2180                 /*
2181                  * The driver is expected to do FIP/FCF. Call the port
2182                  * and get the FCF Table.
2183                  */
2184                 spin_lock_irq(&phba->hbalock);
2185                 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2186                         spin_unlock_irq(&phba->hbalock);
2187                         return;
2188                 }
2189                 spin_unlock_irq(&phba->hbalock);
2190                 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
2191                 if (rc)
2192                         goto out;
2193         }
2194
2195         return;
2196 out:
2197         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2198         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2199                          "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2200                          vport->port_state, sparam_mbox, cfglink_mbox);
2201         lpfc_issue_clear_la(phba, vport);
2202         return;
2203 }
2204
2205 static void
2206 lpfc_enable_la(struct lpfc_hba *phba)
2207 {
2208         uint32_t control;
2209         struct lpfc_sli *psli = &phba->sli;
2210         spin_lock_irq(&phba->hbalock);
2211         psli->sli_flag |= LPFC_PROCESS_LA;
2212         if (phba->sli_rev <= LPFC_SLI_REV3) {
2213                 control = readl(phba->HCregaddr);
2214                 control |= HC_LAINT_ENA;
2215                 writel(control, phba->HCregaddr);
2216                 readl(phba->HCregaddr); /* flush */
2217         }
2218         spin_unlock_irq(&phba->hbalock);
2219 }
2220
2221 static void
2222 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2223 {
2224         lpfc_linkdown(phba);
2225         lpfc_enable_la(phba);
2226         lpfc_unregister_unused_fcf(phba);
2227         /* turn on Link Attention interrupts - no CLEAR_LA needed */
2228 }
2229
2230
2231 /*
2232  * This routine handles processing a READ_LA mailbox
2233  * command upon completion. It is setup in the LPFC_MBOXQ
2234  * as the completion routine when the command is
2235  * handed off to the SLI layer.
2236  */
2237 void
2238 lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2239 {
2240         struct lpfc_vport *vport = pmb->vport;
2241         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2242         READ_LA_VAR *la;
2243         MAILBOX_t *mb = &pmb->u.mb;
2244         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2245
2246         /* Unblock ELS traffic */
2247         phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
2248         /* Check for error */
2249         if (mb->mbxStatus) {
2250                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2251                                 "1307 READ_LA mbox error x%x state x%x\n",
2252                                 mb->mbxStatus, vport->port_state);
2253                 lpfc_mbx_issue_link_down(phba);
2254                 phba->link_state = LPFC_HBA_ERROR;
2255                 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2256         }
2257
2258         la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
2259
2260         memcpy(&phba->alpa_map[0], mp->virt, 128);
2261
2262         spin_lock_irq(shost->host_lock);
2263         if (la->pb)
2264                 vport->fc_flag |= FC_BYPASSED_MODE;
2265         else
2266                 vport->fc_flag &= ~FC_BYPASSED_MODE;
2267         spin_unlock_irq(shost->host_lock);
2268
2269         if ((phba->fc_eventTag  < la->eventTag) ||
2270             (phba->fc_eventTag == la->eventTag)) {
2271                 phba->fc_stat.LinkMultiEvent++;
2272                 if (la->attType == AT_LINK_UP)
2273                         if (phba->fc_eventTag != 0)
2274                                 lpfc_linkdown(phba);
2275         }
2276
2277         phba->fc_eventTag = la->eventTag;
2278         spin_lock_irq(&phba->hbalock);
2279         if (la->mm)
2280                 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2281         else
2282                 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
2283         spin_unlock_irq(&phba->hbalock);
2284
2285         phba->link_events++;
2286         if (la->attType == AT_LINK_UP && (!la->mm)) {
2287                 phba->fc_stat.LinkUp++;
2288                 if (phba->link_flag & LS_LOOPBACK_MODE) {
2289                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2290                                         "1306 Link Up Event in loop back mode "
2291                                         "x%x received Data: x%x x%x x%x x%x\n",
2292                                         la->eventTag, phba->fc_eventTag,
2293                                         la->granted_AL_PA, la->UlnkSpeed,
2294                                         phba->alpa_map[0]);
2295                 } else {
2296                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2297                                         "1303 Link Up Event x%x received "
2298                                         "Data: x%x x%x x%x x%x x%x x%x %d\n",
2299                                         la->eventTag, phba->fc_eventTag,
2300                                         la->granted_AL_PA, la->UlnkSpeed,
2301                                         phba->alpa_map[0],
2302                                         la->mm, la->fa,
2303                                         phba->wait_4_mlo_maint_flg);
2304                 }
2305                 lpfc_mbx_process_link_up(phba, la);
2306         } else if (la->attType == AT_LINK_DOWN) {
2307                 phba->fc_stat.LinkDown++;
2308                 if (phba->link_flag & LS_LOOPBACK_MODE) {
2309                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2310                                 "1308 Link Down Event in loop back mode "
2311                                 "x%x received "
2312                                 "Data: x%x x%x x%x\n",
2313                                 la->eventTag, phba->fc_eventTag,
2314                                 phba->pport->port_state, vport->fc_flag);
2315                 }
2316                 else {
2317                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2318                                 "1305 Link Down Event x%x received "
2319                                 "Data: x%x x%x x%x x%x x%x\n",
2320                                 la->eventTag, phba->fc_eventTag,
2321                                 phba->pport->port_state, vport->fc_flag,
2322                                 la->mm, la->fa);
2323                 }
2324                 lpfc_mbx_issue_link_down(phba);
2325         }
2326         if (la->mm && la->attType == AT_LINK_UP) {
2327                 if (phba->link_state != LPFC_LINK_DOWN) {
2328                         phba->fc_stat.LinkDown++;
2329                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2330                                 "1312 Link Down Event x%x received "
2331                                 "Data: x%x x%x x%x\n",
2332                                 la->eventTag, phba->fc_eventTag,
2333                                 phba->pport->port_state, vport->fc_flag);
2334                         lpfc_mbx_issue_link_down(phba);
2335                 } else
2336                         lpfc_enable_la(phba);
2337
2338                 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2339                                 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2340                                 "Data: x%x x%x x%x\n",
2341                                 la->eventTag, phba->fc_eventTag,
2342                                 phba->pport->port_state, vport->fc_flag);
2343                 /*
2344                  * The cmnd that triggered this will be waiting for this
2345                  * signal.
2346                  */
2347                 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2348                 if (phba->wait_4_mlo_maint_flg) {
2349                         phba->wait_4_mlo_maint_flg = 0;
2350                         wake_up_interruptible(&phba->wait_4_mlo_m_q);
2351                 }
2352         }
2353
2354         if (la->fa) {
2355                 if (la->mm)
2356                         lpfc_issue_clear_la(phba, vport);
2357                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2358                                 "1311 fa %d\n", la->fa);
2359         }
2360
2361 lpfc_mbx_cmpl_read_la_free_mbuf:
2362         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2363         kfree(mp);
2364         mempool_free(pmb, phba->mbox_mem_pool);
2365         return;
2366 }
2367
2368 /*
2369  * This routine handles processing a REG_LOGIN mailbox
2370  * command upon completion. It is setup in the LPFC_MBOXQ
2371  * as the completion routine when the command is
2372  * handed off to the SLI layer.
2373  */
2374 void
2375 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2376 {
2377         struct lpfc_vport  *vport = pmb->vport;
2378         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2379         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2380
2381         pmb->context1 = NULL;
2382
2383         /* Good status, call state machine */
2384         lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
2385         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2386         kfree(mp);
2387         mempool_free(pmb, phba->mbox_mem_pool);
2388         /* decrement the node reference count held for this callback
2389          * function.
2390          */
2391         lpfc_nlp_put(ndlp);
2392
2393         return;
2394 }
2395
2396 static void
2397 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2398 {
2399         MAILBOX_t *mb = &pmb->u.mb;
2400         struct lpfc_vport *vport = pmb->vport;
2401         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2402
2403         switch (mb->mbxStatus) {
2404         case 0x0011:
2405         case 0x0020:
2406         case 0x9700:
2407                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2408                                  "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2409                                  mb->mbxStatus);
2410                 break;
2411         }
2412         spin_lock_irq(shost->host_lock);
2413         vport->vpi_state &= ~LPFC_VPI_REGISTERED;
2414         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2415         spin_unlock_irq(shost->host_lock);
2416         vport->unreg_vpi_cmpl = VPORT_OK;
2417         mempool_free(pmb, phba->mbox_mem_pool);
2418         /*
2419          * This shost reference might have been taken at the beginning of
2420          * lpfc_vport_delete()
2421          */
2422         if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
2423                 scsi_host_put(shost);
2424 }
2425
2426 int
2427 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2428 {
2429         struct lpfc_hba  *phba = vport->phba;
2430         LPFC_MBOXQ_t *mbox;
2431         int rc;
2432
2433         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2434         if (!mbox)
2435                 return 1;
2436
2437         lpfc_unreg_vpi(phba, vport->vpi, mbox);
2438         mbox->vport = vport;
2439         mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
2440         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2441         if (rc == MBX_NOT_FINISHED) {
2442                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2443                                  "1800 Could not issue unreg_vpi\n");
2444                 mempool_free(mbox, phba->mbox_mem_pool);
2445                 vport->unreg_vpi_cmpl = VPORT_ERROR;
2446                 return rc;
2447         }
2448         return 0;
2449 }
2450
2451 static void
2452 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2453 {
2454         struct lpfc_vport *vport = pmb->vport;
2455         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2456         MAILBOX_t *mb = &pmb->u.mb;
2457
2458         switch (mb->mbxStatus) {
2459         case 0x0011:
2460         case 0x9601:
2461         case 0x9602:
2462                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2463                                  "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2464                                  mb->mbxStatus);
2465                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2466                 spin_lock_irq(shost->host_lock);
2467                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2468                 spin_unlock_irq(shost->host_lock);
2469                 vport->fc_myDID = 0;
2470                 goto out;
2471         }
2472
2473         spin_lock_irq(shost->host_lock);
2474         vport->vpi_state |= LPFC_VPI_REGISTERED;
2475         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2476         spin_unlock_irq(shost->host_lock);
2477         vport->num_disc_nodes = 0;
2478         /* go thru NPR list and issue ELS PLOGIs */
2479         if (vport->fc_npr_cnt)
2480                 lpfc_els_disc_plogi(vport);
2481
2482         if (!vport->num_disc_nodes) {
2483                 spin_lock_irq(shost->host_lock);
2484                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2485                 spin_unlock_irq(shost->host_lock);
2486                 lpfc_can_disctmo(vport);
2487         }
2488         vport->port_state = LPFC_VPORT_READY;
2489
2490 out:
2491         mempool_free(pmb, phba->mbox_mem_pool);
2492         return;
2493 }
2494
2495 /**
2496  * lpfc_create_static_vport - Read HBA config region to create static vports.
2497  * @phba: pointer to lpfc hba data structure.
2498  *
2499  * This routine issue a DUMP mailbox command for config region 22 to get
2500  * the list of static vports to be created. The function create vports
2501  * based on the information returned from the HBA.
2502  **/
2503 void
2504 lpfc_create_static_vport(struct lpfc_hba *phba)
2505 {
2506         LPFC_MBOXQ_t *pmb = NULL;
2507         MAILBOX_t *mb;
2508         struct static_vport_info *vport_info;
2509         int rc = 0, i;
2510         struct fc_vport_identifiers vport_id;
2511         struct fc_vport *new_fc_vport;
2512         struct Scsi_Host *shost;
2513         struct lpfc_vport *vport;
2514         uint16_t offset = 0;
2515         uint8_t *vport_buff;
2516         struct lpfc_dmabuf *mp;
2517         uint32_t byte_count = 0;
2518
2519         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2520         if (!pmb) {
2521                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2522                                 "0542 lpfc_create_static_vport failed to"
2523                                 " allocate mailbox memory\n");
2524                 return;
2525         }
2526
2527         mb = &pmb->u.mb;
2528
2529         vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2530         if (!vport_info) {
2531                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2532                                 "0543 lpfc_create_static_vport failed to"
2533                                 " allocate vport_info\n");
2534                 mempool_free(pmb, phba->mbox_mem_pool);
2535                 return;
2536         }
2537
2538         vport_buff = (uint8_t *) vport_info;
2539         do {
2540                 if (lpfc_dump_static_vport(phba, pmb, offset))
2541                         goto out;
2542
2543                 pmb->vport = phba->pport;
2544                 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2545
2546                 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2547                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2548                                 "0544 lpfc_create_static_vport failed to"
2549                                 " issue dump mailbox command ret 0x%x "
2550                                 "status 0x%x\n",
2551                                 rc, mb->mbxStatus);
2552                         goto out;
2553                 }
2554
2555                 if (phba->sli_rev == LPFC_SLI_REV4) {
2556                         byte_count = pmb->u.mqe.un.mb_words[5];
2557                         mp = (struct lpfc_dmabuf *) pmb->context2;
2558                         if (byte_count > sizeof(struct static_vport_info) -
2559                                         offset)
2560                                 byte_count = sizeof(struct static_vport_info)
2561                                         - offset;
2562                         memcpy(vport_buff + offset, mp->virt, byte_count);
2563                         offset += byte_count;
2564                 } else {
2565                         if (mb->un.varDmp.word_cnt >
2566                                 sizeof(struct static_vport_info) - offset)
2567                                 mb->un.varDmp.word_cnt =
2568                                         sizeof(struct static_vport_info)
2569                                                 - offset;
2570                         byte_count = mb->un.varDmp.word_cnt;
2571                         lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2572                                 vport_buff + offset,
2573                                 byte_count);
2574
2575                         offset += byte_count;
2576                 }
2577
2578         } while (byte_count &&
2579                 offset < sizeof(struct static_vport_info));
2580
2581
2582         if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2583                 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2584                         != VPORT_INFO_REV)) {
2585                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2586                         "0545 lpfc_create_static_vport bad"
2587                         " information header 0x%x 0x%x\n",
2588                         le32_to_cpu(vport_info->signature),
2589                         le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2590
2591                 goto out;
2592         }
2593
2594         shost = lpfc_shost_from_vport(phba->pport);
2595
2596         for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2597                 memset(&vport_id, 0, sizeof(vport_id));
2598                 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2599                 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2600                 if (!vport_id.port_name || !vport_id.node_name)
2601                         continue;
2602
2603                 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2604                 vport_id.vport_type = FC_PORTTYPE_NPIV;
2605                 vport_id.disable = false;
2606                 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2607
2608                 if (!new_fc_vport) {
2609                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2610                                 "0546 lpfc_create_static_vport failed to"
2611                                 " create vport\n");
2612                         continue;
2613                 }
2614
2615                 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2616                 vport->vport_flag |= STATIC_VPORT;
2617         }
2618
2619 out:
2620         kfree(vport_info);
2621         if (rc != MBX_TIMEOUT) {
2622                 if (pmb->context2) {
2623                         mp = (struct lpfc_dmabuf *) pmb->context2;
2624                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2625                         kfree(mp);
2626                 }
2627                 mempool_free(pmb, phba->mbox_mem_pool);
2628         }
2629
2630         return;
2631 }
2632
2633 /*
2634  * This routine handles processing a Fabric REG_LOGIN mailbox
2635  * command upon completion. It is setup in the LPFC_MBOXQ
2636  * as the completion routine when the command is
2637  * handed off to the SLI layer.
2638  */
2639 void
2640 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2641 {
2642         struct lpfc_vport *vport = pmb->vport;
2643         MAILBOX_t *mb = &pmb->u.mb;
2644         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2645         struct lpfc_nodelist *ndlp;
2646
2647         ndlp = (struct lpfc_nodelist *) pmb->context2;
2648         pmb->context1 = NULL;
2649         pmb->context2 = NULL;
2650         if (mb->mbxStatus) {
2651                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2652                                  "0258 Register Fabric login error: 0x%x\n",
2653                                  mb->mbxStatus);
2654                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2655                 kfree(mp);
2656                 mempool_free(pmb, phba->mbox_mem_pool);
2657
2658                 if (phba->fc_topology == TOPOLOGY_LOOP) {
2659                         /* FLOGI failed, use loop map to make discovery list */
2660                         lpfc_disc_list_loopmap(vport);
2661
2662                         /* Start discovery */
2663                         lpfc_disc_start(vport);
2664                         /* Decrement the reference count to ndlp after the
2665                          * reference to the ndlp are done.
2666                          */
2667                         lpfc_nlp_put(ndlp);
2668                         return;
2669                 }
2670
2671                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2672                 /* Decrement the reference count to ndlp after the reference
2673                  * to the ndlp are done.
2674                  */
2675                 lpfc_nlp_put(ndlp);
2676                 return;
2677         }
2678
2679         ndlp->nlp_rpi = mb->un.varWords[0];
2680         ndlp->nlp_flag |= NLP_RPI_VALID;
2681         ndlp->nlp_type |= NLP_FABRIC;
2682         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2683
2684         if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2685                 lpfc_start_fdiscs(phba);
2686                 lpfc_do_scr_ns_plogi(phba, vport);
2687         }
2688
2689         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2690         kfree(mp);
2691         mempool_free(pmb, phba->mbox_mem_pool);
2692
2693         /* Drop the reference count from the mbox at the end after
2694          * all the current reference to the ndlp have been done.
2695          */
2696         lpfc_nlp_put(ndlp);
2697         return;
2698 }
2699
2700 /*
2701  * This routine handles processing a NameServer REG_LOGIN mailbox
2702  * command upon completion. It is setup in the LPFC_MBOXQ
2703  * as the completion routine when the command is
2704  * handed off to the SLI layer.
2705  */
2706 void
2707 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2708 {
2709         MAILBOX_t *mb = &pmb->u.mb;
2710         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2711         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2712         struct lpfc_vport *vport = pmb->vport;
2713
2714         if (mb->mbxStatus) {
2715 out:
2716                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2717                                  "0260 Register NameServer error: 0x%x\n",
2718                                  mb->mbxStatus);
2719                 /* decrement the node reference count held for this
2720                  * callback function.
2721                  */
2722                 lpfc_nlp_put(ndlp);
2723                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2724                 kfree(mp);
2725                 mempool_free(pmb, phba->mbox_mem_pool);
2726
2727                 /* If no other thread is using the ndlp, free it */
2728                 lpfc_nlp_not_used(ndlp);
2729
2730                 if (phba->fc_topology == TOPOLOGY_LOOP) {
2731                         /*
2732                          * RegLogin failed, use loop map to make discovery
2733                          * list
2734                          */
2735                         lpfc_disc_list_loopmap(vport);
2736
2737                         /* Start discovery */
2738                         lpfc_disc_start(vport);
2739                         return;
2740                 }
2741                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2742                 return;
2743         }
2744
2745         pmb->context1 = NULL;
2746
2747         ndlp->nlp_rpi = mb->un.varWords[0];
2748         ndlp->nlp_flag |= NLP_RPI_VALID;
2749         ndlp->nlp_type |= NLP_FABRIC;
2750         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2751
2752         if (vport->port_state < LPFC_VPORT_READY) {
2753                 /* Link up discovery requires Fabric registration. */
2754                 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
2755                 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
2756                 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2757                 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
2758                 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
2759
2760                 /* Issue SCR just before NameServer GID_FT Query */
2761                 lpfc_issue_els_scr(vport, SCR_DID, 0);
2762         }
2763
2764         vport->fc_ns_retry = 0;
2765         /* Good status, issue CT Request to NameServer */
2766         if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
2767                 /* Cannot issue NameServer Query, so finish up discovery */
2768                 goto out;
2769         }
2770
2771         /* decrement the node reference count held for this
2772          * callback function.
2773          */
2774         lpfc_nlp_put(ndlp);
2775         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2776         kfree(mp);
2777         mempool_free(pmb, phba->mbox_mem_pool);
2778
2779         return;
2780 }
2781
2782 static void
2783 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2784 {
2785         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2786         struct fc_rport  *rport;
2787         struct lpfc_rport_data *rdata;
2788         struct fc_rport_identifiers rport_ids;
2789         struct lpfc_hba  *phba = vport->phba;
2790
2791         /* Remote port has reappeared. Re-register w/ FC transport */
2792         rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2793         rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
2794         rport_ids.port_id = ndlp->nlp_DID;
2795         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2796
2797         /*
2798          * We leave our node pointer in rport->dd_data when we unregister a
2799          * FCP target port.  But fc_remote_port_add zeros the space to which
2800          * rport->dd_data points.  So, if we're reusing a previously
2801          * registered port, drop the reference that we took the last time we
2802          * registered the port.
2803          */
2804         if (ndlp->rport && ndlp->rport->dd_data &&
2805             ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
2806                 lpfc_nlp_put(ndlp);
2807
2808         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
2809                 "rport add:       did:x%x flg:x%x type x%x",
2810                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2811
2812         ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
2813         if (!rport || !get_device(&rport->dev)) {
2814                 dev_printk(KERN_WARNING, &phba->pcidev->dev,
2815                            "Warning: fc_remote_port_add failed\n");
2816                 return;
2817         }
2818
2819         /* initialize static port data */
2820         rport->maxframe_size = ndlp->nlp_maxframe;
2821         rport->supported_classes = ndlp->nlp_class_sup;
2822         rdata = rport->dd_data;
2823         rdata->pnode = lpfc_nlp_get(ndlp);
2824
2825         if (ndlp->nlp_type & NLP_FCP_TARGET)
2826                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2827         if (ndlp->nlp_type & NLP_FCP_INITIATOR)
2828                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2829
2830
2831         if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
2832                 fc_remote_port_rolechg(rport, rport_ids.roles);
2833
2834         if ((rport->scsi_target_id != -1) &&
2835             (rport->scsi_target_id < LPFC_MAX_TARGET)) {
2836                 ndlp->nlp_sid = rport->scsi_target_id;
2837         }
2838         return;
2839 }
2840
2841 static void
2842 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
2843 {
2844         struct fc_rport *rport = ndlp->rport;
2845
2846         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
2847                 "rport delete:    did:x%x flg:x%x type x%x",
2848                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2849
2850         fc_remote_port_delete(rport);
2851
2852         return;
2853 }
2854
2855 static void
2856 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
2857 {
2858         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2859
2860         spin_lock_irq(shost->host_lock);
2861         switch (state) {
2862         case NLP_STE_UNUSED_NODE:
2863                 vport->fc_unused_cnt += count;
2864                 break;
2865         case NLP_STE_PLOGI_ISSUE:
2866                 vport->fc_plogi_cnt += count;
2867                 break;
2868         case NLP_STE_ADISC_ISSUE:
2869                 vport->fc_adisc_cnt += count;
2870                 break;
2871         case NLP_STE_REG_LOGIN_ISSUE:
2872                 vport->fc_reglogin_cnt += count;
2873                 break;
2874         case NLP_STE_PRLI_ISSUE:
2875                 vport->fc_prli_cnt += count;
2876                 break;
2877         case NLP_STE_UNMAPPED_NODE:
2878                 vport->fc_unmap_cnt += count;
2879                 break;
2880         case NLP_STE_MAPPED_NODE:
2881                 vport->fc_map_cnt += count;
2882                 break;
2883         case NLP_STE_NPR_NODE:
2884                 vport->fc_npr_cnt += count;
2885                 break;
2886         }
2887         spin_unlock_irq(shost->host_lock);
2888 }
2889
2890 static void
2891 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2892                        int old_state, int new_state)
2893 {
2894         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2895
2896         if (new_state == NLP_STE_UNMAPPED_NODE) {
2897                 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2898                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2899                 ndlp->nlp_type |= NLP_FC_NODE;
2900         }
2901         if (new_state == NLP_STE_MAPPED_NODE)
2902                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2903         if (new_state == NLP_STE_NPR_NODE)
2904                 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
2905
2906         /* Transport interface */
2907         if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
2908                             old_state == NLP_STE_UNMAPPED_NODE)) {
2909                 vport->phba->nport_event_cnt++;
2910                 lpfc_unregister_remote_port(ndlp);
2911         }
2912
2913         if (new_state ==  NLP_STE_MAPPED_NODE ||
2914             new_state == NLP_STE_UNMAPPED_NODE) {
2915                 vport->phba->nport_event_cnt++;
2916                 /*
2917                  * Tell the fc transport about the port, if we haven't
2918                  * already. If we have, and it's a scsi entity, be
2919                  * sure to unblock any attached scsi devices
2920                  */
2921                 lpfc_register_remote_port(vport, ndlp);
2922         }
2923         if ((new_state ==  NLP_STE_MAPPED_NODE) &&
2924                 (vport->stat_data_enabled)) {
2925                 /*
2926                  * A new target is discovered, if there is no buffer for
2927                  * statistical data collection allocate buffer.
2928                  */
2929                 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
2930                                          sizeof(struct lpfc_scsicmd_bkt),
2931                                          GFP_KERNEL);
2932
2933                 if (!ndlp->lat_data)
2934                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2935                                 "0286 lpfc_nlp_state_cleanup failed to "
2936                                 "allocate statistical data buffer DID "
2937                                 "0x%x\n", ndlp->nlp_DID);
2938         }
2939         /*
2940          * if we added to Mapped list, but the remote port
2941          * registration failed or assigned a target id outside
2942          * our presentable range - move the node to the
2943          * Unmapped List
2944          */
2945         if (new_state == NLP_STE_MAPPED_NODE &&
2946             (!ndlp->rport ||
2947              ndlp->rport->scsi_target_id == -1 ||
2948              ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
2949                 spin_lock_irq(shost->host_lock);
2950                 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
2951                 spin_unlock_irq(shost->host_lock);
2952                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2953         }
2954 }
2955
2956 static char *
2957 lpfc_nlp_state_name(char *buffer, size_t size, int state)
2958 {
2959         static char *states[] = {
2960                 [NLP_STE_UNUSED_NODE] = "UNUSED",
2961                 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
2962                 [NLP_STE_ADISC_ISSUE] = "ADISC",
2963                 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
2964                 [NLP_STE_PRLI_ISSUE] = "PRLI",
2965                 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
2966                 [NLP_STE_MAPPED_NODE] = "MAPPED",
2967                 [NLP_STE_NPR_NODE] = "NPR",
2968         };
2969
2970         if (state < NLP_STE_MAX_STATE && states[state])
2971                 strlcpy(buffer, states[state], size);
2972         else
2973                 snprintf(buffer, size, "unknown (%d)", state);
2974         return buffer;
2975 }
2976
2977 void
2978 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2979                    int state)
2980 {
2981         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2982         int  old_state = ndlp->nlp_state;
2983         char name1[16], name2[16];
2984
2985         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2986                          "0904 NPort state transition x%06x, %s -> %s\n",
2987                          ndlp->nlp_DID,
2988                          lpfc_nlp_state_name(name1, sizeof(name1), old_state),
2989                          lpfc_nlp_state_name(name2, sizeof(name2), state));
2990
2991         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2992                 "node statechg    did:x%x old:%d ste:%d",
2993                 ndlp->nlp_DID, old_state, state);
2994
2995         if (old_state == NLP_STE_NPR_NODE &&
2996             state != NLP_STE_NPR_NODE)
2997                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2998         if (old_state == NLP_STE_UNMAPPED_NODE) {
2999                 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
3000                 ndlp->nlp_type &= ~NLP_FC_NODE;
3001         }
3002
3003         if (list_empty(&ndlp->nlp_listp)) {
3004                 spin_lock_irq(shost->host_lock);
3005                 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3006                 spin_unlock_irq(shost->host_lock);
3007         } else if (old_state)
3008                 lpfc_nlp_counters(vport, old_state, -1);
3009
3010         ndlp->nlp_state = state;
3011         lpfc_nlp_counters(vport, state, 1);
3012         lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
3013 }
3014
3015 void
3016 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3017 {
3018         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3019
3020         if (list_empty(&ndlp->nlp_listp)) {
3021                 spin_lock_irq(shost->host_lock);
3022                 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3023                 spin_unlock_irq(shost->host_lock);
3024         }
3025 }
3026
3027 void
3028 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3029 {
3030         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3031
3032         lpfc_cancel_retry_delay_tmo(vport, ndlp);
3033         if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3034                 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3035         spin_lock_irq(shost->host_lock);
3036         list_del_init(&ndlp->nlp_listp);
3037         spin_unlock_irq(shost->host_lock);
3038         lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3039                                 NLP_STE_UNUSED_NODE);
3040 }
3041
3042 static void
3043 lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3044 {
3045         lpfc_cancel_retry_delay_tmo(vport, ndlp);
3046         if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3047                 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3048         lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3049                                 NLP_STE_UNUSED_NODE);
3050 }
3051 /**
3052  * lpfc_initialize_node - Initialize all fields of node object
3053  * @vport: Pointer to Virtual Port object.
3054  * @ndlp: Pointer to FC node object.
3055  * @did: FC_ID of the node.
3056  *
3057  * This function is always called when node object need to be initialized.
3058  * It initializes all the fields of the node object. Although the reference
3059  * to phba from @ndlp can be obtained indirectly through it's reference to
3060  * @vport, a direct reference to phba is taken here by @ndlp. This is due
3061  * to the life-span of the @ndlp might go beyond the existence of @vport as
3062  * the final release of ndlp is determined by its reference count. And, the
3063  * operation on @ndlp needs the reference to phba.
3064  **/
3065 static inline void
3066 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3067         uint32_t did)
3068 {
3069         INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3070         INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3071         init_timer(&ndlp->nlp_delayfunc);
3072         ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3073         ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3074         ndlp->nlp_DID = did;
3075         ndlp->vport = vport;
3076         ndlp->phba = vport->phba;
3077         ndlp->nlp_sid = NLP_NO_SID;
3078         kref_init(&ndlp->kref);
3079         NLP_INT_NODE_ACT(ndlp);
3080         atomic_set(&ndlp->cmd_pending, 0);
3081         ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
3082 }
3083
3084 struct lpfc_nodelist *
3085 lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3086                  int state)
3087 {
3088         struct lpfc_hba *phba = vport->phba;
3089         uint32_t did;
3090         unsigned long flags;
3091
3092         if (!ndlp)
3093                 return NULL;
3094
3095         spin_lock_irqsave(&phba->ndlp_lock, flags);
3096         /* The ndlp should not be in memory free mode */
3097         if (NLP_CHK_FREE_REQ(ndlp)) {
3098                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3099                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3100                                 "0277 lpfc_enable_node: ndlp:x%p "
3101                                 "usgmap:x%x refcnt:%d\n",
3102                                 (void *)ndlp, ndlp->nlp_usg_map,
3103                                 atomic_read(&ndlp->kref.refcount));
3104                 return NULL;
3105         }
3106         /* The ndlp should not already be in active mode */
3107         if (NLP_CHK_NODE_ACT(ndlp)) {
3108                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3109                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3110                                 "0278 lpfc_enable_node: ndlp:x%p "
3111                                 "usgmap:x%x refcnt:%d\n",
3112                                 (void *)ndlp, ndlp->nlp_usg_map,
3113                                 atomic_read(&ndlp->kref.refcount));
3114                 return NULL;
3115         }
3116
3117         /* Keep the original DID */
3118         did = ndlp->nlp_DID;
3119
3120         /* re-initialize ndlp except of ndlp linked list pointer */
3121         memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3122                 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
3123         lpfc_initialize_node(vport, ndlp, did);
3124
3125         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3126
3127         if (state != NLP_STE_UNUSED_NODE)
3128                 lpfc_nlp_set_state(vport, ndlp, state);
3129
3130         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3131                 "node enable:       did:x%x",
3132                 ndlp->nlp_DID, 0, 0);
3133         return ndlp;
3134 }
3135
3136 void
3137 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3138 {
3139         /*
3140          * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
3141          * be used if we wish to issue the "last" lpfc_nlp_put() to remove
3142          * the ndlp from the vport. The ndlp marked as UNUSED on the list
3143          * until ALL other outstanding threads have completed. We check
3144          * that the ndlp not already in the UNUSED state before we proceed.
3145          */
3146         if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3147                 return;
3148         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
3149         lpfc_nlp_put(ndlp);
3150         return;
3151 }
3152
3153 /*
3154  * Start / ReStart rescue timer for Discovery / RSCN handling
3155  */
3156 void
3157 lpfc_set_disctmo(struct lpfc_vport *vport)
3158 {
3159         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3160         struct lpfc_hba  *phba = vport->phba;
3161         uint32_t tmo;
3162
3163         if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
3164                 /* For FAN, timeout should be greater than edtov */
3165                 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3166         } else {
3167                 /* Normal discovery timeout should be > than ELS/CT timeout
3168                  * FC spec states we need 3 * ratov for CT requests
3169                  */
3170                 tmo = ((phba->fc_ratov * 3) + 3);
3171         }
3172
3173
3174         if (!timer_pending(&vport->fc_disctmo)) {
3175                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3176                         "set disc timer:  tmo:x%x state:x%x flg:x%x",
3177                         tmo, vport->port_state, vport->fc_flag);
3178         }
3179
3180         mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3181         spin_lock_irq(shost->host_lock);
3182         vport->fc_flag |= FC_DISC_TMO;
3183         spin_unlock_irq(shost->host_lock);
3184
3185         /* Start Discovery Timer state <hba_state> */
3186         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3187                          "0247 Start Discovery Timer state x%x "
3188                          "Data: x%x x%lx x%x x%x\n",
3189                          vport->port_state, tmo,
3190                          (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3191                          vport->fc_adisc_cnt);
3192
3193         return;
3194 }
3195
3196 /*
3197  * Cancel rescue timer for Discovery / RSCN handling
3198  */
3199 int
3200 lpfc_can_disctmo(struct lpfc_vport *vport)
3201 {
3202         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3203         unsigned long iflags;
3204
3205         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3206                 "can disc timer:  state:x%x rtry:x%x flg:x%x",
3207                 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3208
3209         /* Turn off discovery timer if its running */
3210         if (vport->fc_flag & FC_DISC_TMO) {
3211                 spin_lock_irqsave(shost->host_lock, iflags);
3212                 vport->fc_flag &= ~FC_DISC_TMO;
3213                 spin_unlock_irqrestore(shost->host_lock, iflags);
3214                 del_timer_sync(&vport->fc_disctmo);
3215                 spin_lock_irqsave(&vport->work_port_lock, iflags);
3216                 vport->work_port_events &= ~WORKER_DISC_TMO;
3217                 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
3218         }
3219
3220         /* Cancel Discovery Timer state <hba_state> */
3221         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3222                          "0248 Cancel Discovery Timer state x%x "
3223                          "Data: x%x x%x x%x\n",
3224                          vport->port_state, vport->fc_flag,
3225                          vport->fc_plogi_cnt, vport->fc_adisc_cnt);
3226         return 0;
3227 }
3228
3229 /*
3230  * Check specified ring for outstanding IOCB on the SLI queue
3231  * Return true if iocb matches the specified nport
3232  */
3233 int
3234 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3235                     struct lpfc_sli_ring *pring,
3236                     struct lpfc_iocbq *iocb,
3237                     struct lpfc_nodelist *ndlp)
3238 {
3239         struct lpfc_sli *psli = &phba->sli;
3240         IOCB_t *icmd = &iocb->iocb;
3241         struct lpfc_vport    *vport = ndlp->vport;
3242
3243         if (iocb->vport != vport)
3244                 return 0;
3245
3246         if (pring->ringno == LPFC_ELS_RING) {
3247                 switch (icmd->ulpCommand) {
3248                 case CMD_GEN_REQUEST64_CR:
3249                         if (iocb->context_un.ndlp == ndlp)
3250                                 return 1;
3251                 case CMD_ELS_REQUEST64_CR:
3252                         if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3253                                 return 1;
3254                 case CMD_XMIT_ELS_RSP64_CX:
3255                         if (iocb->context1 == (uint8_t *) ndlp)
3256                                 return 1;
3257                 }
3258         } else if (pring->ringno == psli->extra_ring) {
3259
3260         } else if (pring->ringno == psli->fcp_ring) {
3261                 /* Skip match check if waiting to relogin to FCP target */
3262                 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
3263                     (ndlp->nlp_flag & NLP_DELAY_TMO)) {
3264                         return 0;
3265                 }
3266                 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
3267                         return 1;
3268                 }
3269         } else if (pring->ringno == psli->next_ring) {
3270
3271         }
3272         return 0;
3273 }
3274
3275 /*
3276  * Free resources / clean up outstanding I/Os
3277  * associated with nlp_rpi in the LPFC_NODELIST entry.
3278  */
3279 static int
3280 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
3281 {
3282         LIST_HEAD(completions);
3283         struct lpfc_sli *psli;
3284         struct lpfc_sli_ring *pring;
3285         struct lpfc_iocbq *iocb, *next_iocb;
3286         uint32_t i;
3287
3288         lpfc_fabric_abort_nport(ndlp);
3289
3290         /*
3291          * Everything that matches on txcmplq will be returned
3292          * by firmware with a no rpi error.
3293          */
3294         psli = &phba->sli;
3295         if (ndlp->nlp_flag & NLP_RPI_VALID) {
3296                 /* Now process each ring */
3297                 for (i = 0; i < psli->num_rings; i++) {
3298                         pring = &psli->ring[i];
3299
3300                         spin_lock_irq(&phba->hbalock);
3301                         list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
3302                                                  list) {
3303                                 /*
3304                                  * Check to see if iocb matches the nport we are
3305                                  * looking for
3306                                  */
3307                                 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3308                                                          ndlp))) {
3309                                         /* It matches, so deque and call compl
3310                                            with an error */
3311                                         list_move_tail(&iocb->list,
3312                                                        &completions);
3313                                         pring->txq_cnt--;
3314                                 }
3315                         }
3316                         spin_unlock_irq(&phba->hbalock);
3317                 }
3318         }
3319
3320         /* Cancel all the IOCBs from the completions list */
3321         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3322                               IOERR_SLI_ABORTED);
3323
3324         return 0;
3325 }
3326
3327 /*
3328  * Free rpi associated with LPFC_NODELIST entry.
3329  * This routine is called from lpfc_freenode(), when we are removing
3330  * a LPFC_NODELIST entry. It is also called if the driver initiates a
3331  * LOGO that completes successfully, and we are waiting to PLOGI back
3332  * to the remote NPort. In addition, it is called after we receive
3333  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3334  * we are waiting to PLOGI back to the remote NPort.
3335  */
3336 int
3337 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3338 {
3339         struct lpfc_hba *phba = vport->phba;
3340         LPFC_MBOXQ_t    *mbox;
3341         int rc;
3342
3343         if (ndlp->nlp_flag & NLP_RPI_VALID) {
3344                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3345                 if (mbox) {
3346                         lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
3347                         mbox->vport = vport;
3348                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3349                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3350                         if (rc == MBX_NOT_FINISHED)
3351                                 mempool_free(mbox, phba->mbox_mem_pool);
3352                 }
3353                 lpfc_no_rpi(phba, ndlp);
3354                 ndlp->nlp_rpi = 0;
3355                 ndlp->nlp_flag &= ~NLP_RPI_VALID;
3356                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3357                 return 1;
3358         }
3359         return 0;
3360 }
3361
3362 /**
3363  * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
3364  * @phba: pointer to lpfc hba data structure.
3365  *
3366  * This routine is invoked to unregister all the currently registered RPIs
3367  * to the HBA.
3368  **/
3369 void
3370 lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
3371 {
3372         struct lpfc_vport **vports;
3373         struct lpfc_nodelist *ndlp;
3374         struct Scsi_Host *shost;
3375         int i;
3376
3377         vports = lpfc_create_vport_work_array(phba);
3378         for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3379                 shost = lpfc_shost_from_vport(vports[i]);
3380                 spin_lock_irq(shost->host_lock);
3381                 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
3382                         if (ndlp->nlp_flag & NLP_RPI_VALID)
3383                                 lpfc_unreg_rpi(vports[i], ndlp);
3384                 }
3385                 spin_unlock_irq(shost->host_lock);
3386         }
3387         lpfc_destroy_vport_work_array(phba, vports);
3388 }
3389
3390 void
3391 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3392 {
3393         struct lpfc_hba  *phba  = vport->phba;
3394         LPFC_MBOXQ_t     *mbox;
3395         int rc;
3396
3397         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3398         if (mbox) {
3399                 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3400                 mbox->vport = vport;
3401                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3402                 mbox->context1 = NULL;
3403                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
3404                 if (rc != MBX_TIMEOUT)
3405                         mempool_free(mbox, phba->mbox_mem_pool);
3406
3407                 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3408                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3409                                 "1836 Could not issue "
3410                                 "unreg_login(all_rpis) status %d\n", rc);
3411         }
3412 }
3413
3414 void
3415 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3416 {
3417         struct lpfc_hba  *phba  = vport->phba;
3418         LPFC_MBOXQ_t     *mbox;
3419         int rc;
3420
3421         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3422         if (mbox) {
3423                 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3424                 mbox->vport = vport;
3425                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3426                 mbox->context1 = NULL;
3427                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
3428                 if (rc != MBX_TIMEOUT)
3429                         mempool_free(mbox, phba->mbox_mem_pool);
3430
3431                 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3432                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3433                                          "1815 Could not issue "
3434                                          "unreg_did (default rpis) status %d\n",
3435                                          rc);
3436         }
3437 }
3438
3439 /*
3440  * Free resources associated with LPFC_NODELIST entry
3441  * so it can be freed.
3442  */
3443 static int
3444 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3445 {
3446         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3447         struct lpfc_hba  *phba = vport->phba;
3448         LPFC_MBOXQ_t *mb, *nextmb;
3449         struct lpfc_dmabuf *mp;
3450
3451         /* Cleanup node for NPort <nlp_DID> */
3452         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3453                          "0900 Cleanup node for NPort x%x "
3454                          "Data: x%x x%x x%x\n",
3455                          ndlp->nlp_DID, ndlp->nlp_flag,
3456                          ndlp->nlp_state, ndlp->nlp_rpi);
3457         if (NLP_CHK_FREE_REQ(ndlp)) {
3458                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3459                                 "0280 lpfc_cleanup_node: ndlp:x%p "
3460                                 "usgmap:x%x refcnt:%d\n",
3461                                 (void *)ndlp, ndlp->nlp_usg_map,
3462                                 atomic_read(&ndlp->kref.refcount));
3463                 lpfc_dequeue_node(vport, ndlp);
3464         } else {
3465                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3466                                 "0281 lpfc_cleanup_node: ndlp:x%p "
3467                                 "usgmap:x%x refcnt:%d\n",
3468                                 (void *)ndlp, ndlp->nlp_usg_map,
3469                                 atomic_read(&ndlp->kref.refcount));
3470                 lpfc_disable_node(vport, ndlp);
3471         }
3472
3473         /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3474         if ((mb = phba->sli.mbox_active)) {
3475                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
3476                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3477                         mb->context2 = NULL;
3478                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3479                 }
3480         }
3481
3482         spin_lock_irq(&phba->hbalock);
3483         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
3484                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
3485                     (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3486                         mp = (struct lpfc_dmabuf *) (mb->context1);
3487                         if (mp) {
3488                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
3489                                 kfree(mp);
3490                         }
3491                         list_del(&mb->list);
3492                         mempool_free(mb, phba->mbox_mem_pool);
3493                         /* We shall not invoke the lpfc_nlp_put to decrement
3494                          * the ndlp reference count as we are in the process
3495                          * of lpfc_nlp_release.
3496                          */
3497                 }
3498         }
3499         spin_unlock_irq(&phba->hbalock);
3500
3501         lpfc_els_abort(phba, ndlp);
3502
3503         spin_lock_irq(shost->host_lock);
3504         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
3505         spin_unlock_irq(shost->host_lock);
3506
3507         ndlp->nlp_last_elscmd = 0;
3508         del_timer_sync(&ndlp->nlp_delayfunc);
3509
3510         list_del_init(&ndlp->els_retry_evt.evt_listp);
3511         list_del_init(&ndlp->dev_loss_evt.evt_listp);
3512
3513         lpfc_unreg_rpi(vport, ndlp);
3514
3515         return 0;
3516 }
3517
3518 /*
3519  * Check to see if we can free the nlp back to the freelist.
3520  * If we are in the middle of using the nlp in the discovery state
3521  * machine, defer the free till we reach the end of the state machine.
3522  */
3523 static void
3524 lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3525 {
3526         struct lpfc_hba  *phba = vport->phba;
3527         struct lpfc_rport_data *rdata;
3528         LPFC_MBOXQ_t *mbox;
3529         int rc;
3530
3531         lpfc_cancel_retry_delay_tmo(vport, ndlp);
3532         if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3533             !(ndlp->nlp_flag & NLP_RPI_VALID)) {
3534                 /* For this case we need to cleanup the default rpi
3535                  * allocated by the firmware.
3536                  */
3537                 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3538                         != NULL) {
3539                         rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
3540                             (uint8_t *) &vport->fc_sparam, mbox, 0);
3541                         if (rc) {
3542                                 mempool_free(mbox, phba->mbox_mem_pool);
3543                         }
3544                         else {
3545                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3546                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3547                                 mbox->vport = vport;
3548                                 mbox->context2 = NULL;
3549                                 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3550                                 if (rc == MBX_NOT_FINISHED) {
3551                                         mempool_free(mbox, phba->mbox_mem_pool);
3552                                 }
3553                         }
3554                 }
3555         }
3556         lpfc_cleanup_node(vport, ndlp);
3557
3558         /*
3559          * We can get here with a non-NULL ndlp->rport because when we
3560          * unregister a rport we don't break the rport/node linkage.  So if we
3561          * do, make sure we don't leaving any dangling pointers behind.
3562          */
3563         if (ndlp->rport) {
3564                 rdata = ndlp->rport->dd_data;
3565                 rdata->pnode = NULL;
3566                 ndlp->rport = NULL;
3567         }
3568 }
3569
3570 static int
3571 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3572               uint32_t did)
3573 {
3574         D_ID mydid, ndlpdid, matchdid;
3575
3576         if (did == Bcast_DID)
3577                 return 0;
3578
3579         /* First check for Direct match */
3580         if (ndlp->nlp_DID == did)
3581                 return 1;
3582
3583         /* Next check for area/domain identically equals 0 match */
3584         mydid.un.word = vport->fc_myDID;
3585         if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
3586                 return 0;
3587         }
3588
3589         matchdid.un.word = did;
3590         ndlpdid.un.word = ndlp->nlp_DID;
3591         if (matchdid.un.b.id == ndlpdid.un.b.id) {
3592                 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3593                     (mydid.un.b.area == matchdid.un.b.area)) {
3594                         if ((ndlpdid.un.b.domain == 0) &&
3595                             (ndlpdid.un.b.area == 0)) {
3596                                 if (ndlpdid.un.b.id)
3597                                         return 1;
3598                         }
3599                         return 0;
3600                 }
3601
3602                 matchdid.un.word = ndlp->nlp_DID;
3603                 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3604                     (mydid.un.b.area == ndlpdid.un.b.area)) {
3605                         if ((matchdid.un.b.domain == 0) &&
3606                             (matchdid.un.b.area == 0)) {
3607                                 if (matchdid.un.b.id)
3608                                         return 1;
3609                         }
3610                 }
3611         }
3612         return 0;
3613 }
3614
3615 /* Search for a nodelist entry */
3616 static struct lpfc_nodelist *
3617 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
3618 {
3619         struct lpfc_nodelist *ndlp;
3620         uint32_t data1;
3621
3622         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3623                 if (lpfc_matchdid(vport, ndlp, did)) {
3624                         data1 = (((uint32_t) ndlp->nlp_state << 24) |
3625                                  ((uint32_t) ndlp->nlp_xri << 16) |
3626                                  ((uint32_t) ndlp->nlp_type << 8) |
3627                                  ((uint32_t) ndlp->nlp_rpi & 0xff));
3628                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3629                                          "0929 FIND node DID "
3630                                          "Data: x%p x%x x%x x%x\n",
3631                                          ndlp, ndlp->nlp_DID,
3632                                          ndlp->nlp_flag, data1);
3633                         return ndlp;
3634                 }
3635         }
3636
3637         /* FIND node did <did> NOT FOUND */
3638         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3639                          "0932 FIND node did x%x NOT FOUND.\n", did);
3640         return NULL;
3641 }
3642
3643 struct lpfc_nodelist *
3644 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
3645 {
3646         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3647         struct lpfc_nodelist *ndlp;
3648
3649         spin_lock_irq(shost->host_lock);
3650         ndlp = __lpfc_findnode_did(vport, did);
3651         spin_unlock_irq(shost->host_lock);
3652         return ndlp;
3653 }
3654
3655 struct lpfc_nodelist *
3656 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3657 {
3658         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3659         struct lpfc_nodelist *ndlp;
3660
3661         ndlp = lpfc_findnode_did(vport, did);
3662         if (!ndlp) {
3663                 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
3664                     lpfc_rscn_payload_check(vport, did) == 0)
3665                         return NULL;
3666                 ndlp = (struct lpfc_nodelist *)
3667                      mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
3668                 if (!ndlp)
3669                         return NULL;
3670                 lpfc_nlp_init(vport, ndlp, did);
3671                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3672                 spin_lock_irq(shost->host_lock);
3673                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3674                 spin_unlock_irq(shost->host_lock);
3675                 return ndlp;
3676         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3677                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
3678                 if (!ndlp)
3679                         return NULL;
3680                 spin_lock_irq(shost->host_lock);
3681                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3682                 spin_unlock_irq(shost->host_lock);
3683                 return ndlp;
3684         }
3685
3686         if ((vport->fc_flag & FC_RSCN_MODE) &&
3687             !(vport->fc_flag & FC_NDISC_ACTIVE)) {
3688                 if (lpfc_rscn_payload_check(vport, did)) {
3689                         /* If we've already recieved a PLOGI from this NPort
3690                          * we don't need to try to discover it again.
3691                          */
3692                         if (ndlp->nlp_flag & NLP_RCV_PLOGI)
3693                                 return NULL;
3694
3695                         /* Since this node is marked for discovery,
3696                          * delay timeout is not needed.
3697                          */
3698                         lpfc_cancel_retry_delay_tmo(vport, ndlp);
3699                         spin_lock_irq(shost->host_lock);
3700                         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3701                         spin_unlock_irq(shost->host_lock);
3702                 } else
3703                         ndlp = NULL;
3704         } else {
3705                 /* If we've already recieved a PLOGI from this NPort,
3706                  * or we are already in the process of discovery on it,
3707                  * we don't need to try to discover it again.
3708                  */
3709                 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
3710                     ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3711                     ndlp->nlp_flag & NLP_RCV_PLOGI)
3712                         return NULL;
3713                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3714                 spin_lock_irq(shost->host_lock);
3715                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3716                 spin_unlock_irq(shost->host_lock);
3717         }
3718         return ndlp;
3719 }
3720
3721 /* Build a list of nodes to discover based on the loopmap */
3722 void
3723 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
3724 {
3725         struct lpfc_hba  *phba = vport->phba;
3726         int j;
3727         uint32_t alpa, index;
3728
3729         if (!lpfc_is_link_up(phba))
3730                 return;
3731
3732         if (phba->fc_topology != TOPOLOGY_LOOP)
3733                 return;
3734
3735         /* Check for loop map present or not */
3736         if (phba->alpa_map[0]) {
3737                 for (j = 1; j <= phba->alpa_map[0]; j++) {
3738                         alpa = phba->alpa_map[j];
3739                         if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
3740                                 continue;
3741                         lpfc_setup_disc_node(vport, alpa);
3742                 }
3743         } else {
3744                 /* No alpamap, so try all alpa's */
3745                 for (j = 0; j < FC_MAXLOOP; j++) {
3746                         /* If cfg_scan_down is set, start from highest
3747                          * ALPA (0xef) to lowest (0x1).
3748                          */
3749                         if (vport->cfg_scan_down)
3750                                 index = j;
3751                         else
3752                                 index = FC_MAXLOOP - j - 1;
3753                         alpa = lpfcAlpaArray[index];
3754                         if ((vport->fc_myDID & 0xff) == alpa)
3755                                 continue;
3756                         lpfc_setup_disc_node(vport, alpa);
3757                 }
3758         }
3759         return;
3760 }
3761
3762 void
3763 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
3764 {
3765         LPFC_MBOXQ_t *mbox;
3766         struct lpfc_sli *psli = &phba->sli;
3767         struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
3768         struct lpfc_sli_ring *fcp_ring   = &psli->ring[psli->fcp_ring];
3769         struct lpfc_sli_ring *next_ring  = &psli->ring[psli->next_ring];
3770         int  rc;
3771
3772         /*
3773          * if it's not a physical port or if we already send
3774          * clear_la then don't send it.
3775          */
3776         if ((phba->link_state >= LPFC_CLEAR_LA) ||
3777             (vport->port_type != LPFC_PHYSICAL_PORT) ||
3778                 (phba->sli_rev == LPFC_SLI_REV4))
3779                 return;
3780
3781                         /* Link up discovery */
3782         if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
3783                 phba->link_state = LPFC_CLEAR_LA;
3784                 lpfc_clear_la(phba, mbox);
3785                 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
3786                 mbox->vport = vport;
3787                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3788                 if (rc == MBX_NOT_FINISHED) {
3789                         mempool_free(mbox, phba->mbox_mem_pool);
3790                         lpfc_disc_flush_list(vport);
3791                         extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3792                         fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3793                         next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3794                         phba->link_state = LPFC_HBA_ERROR;
3795                 }
3796         }
3797 }
3798
3799 /* Reg_vpi to tell firmware to resume normal operations */
3800 void
3801 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
3802 {
3803         LPFC_MBOXQ_t *regvpimbox;
3804
3805         regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3806         if (regvpimbox) {
3807                 lpfc_reg_vpi(vport, regvpimbox);
3808                 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
3809                 regvpimbox->vport = vport;
3810                 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
3811                                         == MBX_NOT_FINISHED) {
3812                         mempool_free(regvpimbox, phba->mbox_mem_pool);
3813                 }
3814         }
3815 }
3816
3817 /* Start Link up / RSCN discovery on NPR nodes */
3818 void
3819 lpfc_disc_start(struct lpfc_vport *vport)
3820 {
3821         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3822         struct lpfc_hba  *phba = vport->phba;
3823         uint32_t num_sent;
3824         uint32_t clear_la_pending;
3825         int did_changed;
3826
3827         if (!lpfc_is_link_up(phba))
3828                 return;
3829
3830         if (phba->link_state == LPFC_CLEAR_LA)
3831                 clear_la_pending = 1;
3832         else
3833                 clear_la_pending = 0;
3834
3835         if (vport->port_state < LPFC_VPORT_READY)
3836                 vport->port_state = LPFC_DISC_AUTH;
3837
3838         lpfc_set_disctmo(vport);
3839
3840         if (vport->fc_prevDID == vport->fc_myDID)
3841                 did_changed = 0;
3842         else
3843                 did_changed = 1;
3844
3845         vport->fc_prevDID = vport->fc_myDID;
3846         vport->num_disc_nodes = 0;
3847
3848         /* Start Discovery state <hba_state> */
3849         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3850                          "0202 Start Discovery hba state x%x "
3851                          "Data: x%x x%x x%x\n",
3852                          vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
3853                          vport->fc_adisc_cnt);
3854
3855         /* First do ADISCs - if any */
3856         num_sent = lpfc_els_disc_adisc(vport);
3857
3858         if (num_sent)
3859                 return;
3860
3861         /*
3862          * For SLI3, cmpl_reg_vpi will set port_state to READY, and
3863          * continue discovery.
3864          */
3865         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3866             !(vport->fc_flag & FC_PT2PT) &&
3867             !(vport->fc_flag & FC_RSCN_MODE) &&
3868             (phba->sli_rev < LPFC_SLI_REV4)) {
3869                 lpfc_issue_reg_vpi(phba, vport);
3870                 return;
3871         }
3872
3873         /*
3874          * For SLI2, we need to set port_state to READY and continue
3875          * discovery.
3876          */
3877         if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
3878                 /* If we get here, there is nothing to ADISC */
3879                 if (vport->port_type == LPFC_PHYSICAL_PORT)
3880                         lpfc_issue_clear_la(phba, vport);
3881
3882                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
3883                         vport->num_disc_nodes = 0;
3884                         /* go thru NPR nodes and issue ELS PLOGIs */
3885                         if (vport->fc_npr_cnt)
3886                                 lpfc_els_disc_plogi(vport);
3887
3888                         if (!vport->num_disc_nodes) {
3889                                 spin_lock_irq(shost->host_lock);
3890                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3891                                 spin_unlock_irq(shost->host_lock);
3892                                 lpfc_can_disctmo(vport);
3893                         }
3894                 }
3895                 vport->port_state = LPFC_VPORT_READY;
3896         } else {
3897                 /* Next do PLOGIs - if any */
3898                 num_sent = lpfc_els_disc_plogi(vport);
3899
3900                 if (num_sent)
3901                         return;
3902
3903                 if (vport->fc_flag & FC_RSCN_MODE) {
3904                         /* Check to see if more RSCNs came in while we
3905                          * were processing this one.
3906                          */
3907                         if ((vport->fc_rscn_id_cnt == 0) &&
3908                             (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
3909                                 spin_lock_irq(shost->host_lock);
3910                                 vport->fc_flag &= ~FC_RSCN_MODE;
3911                                 spin_unlock_irq(shost->host_lock);
3912                                 lpfc_can_disctmo(vport);
3913                         } else
3914                                 lpfc_els_handle_rscn(vport);
3915                 }
3916         }
3917         return;
3918 }
3919
3920 /*
3921  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
3922  *  ring the match the sppecified nodelist.
3923  */
3924 static void
3925 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
3926 {
3927         LIST_HEAD(completions);
3928         struct lpfc_sli *psli;
3929         IOCB_t     *icmd;
3930         struct lpfc_iocbq    *iocb, *next_iocb;
3931         struct lpfc_sli_ring *pring;
3932
3933         psli = &phba->sli;
3934         pring = &psli->ring[LPFC_ELS_RING];
3935
3936         /* Error matching iocb on txq or txcmplq
3937          * First check the txq.
3938          */
3939         spin_lock_irq(&phba->hbalock);
3940         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3941                 if (iocb->context1 != ndlp) {
3942                         continue;
3943                 }
3944                 icmd = &iocb->iocb;
3945                 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
3946                     (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
3947
3948                         list_move_tail(&iocb->list, &completions);
3949                         pring->txq_cnt--;
3950                 }
3951         }
3952
3953         /* Next check the txcmplq */
3954         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
3955                 if (iocb->context1 != ndlp) {
3956                         continue;
3957                 }
3958                 icmd = &iocb->iocb;
3959                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
3960                     icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
3961                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3962                 }
3963         }
3964         spin_unlock_irq(&phba->hbalock);
3965
3966         /* Cancel all the IOCBs from the completions list */
3967         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3968                               IOERR_SLI_ABORTED);
3969 }
3970
3971 static void
3972 lpfc_disc_flush_list(struct lpfc_vport *vport)
3973 {
3974         struct lpfc_nodelist *ndlp, *next_ndlp;
3975         struct lpfc_hba *phba = vport->phba;
3976
3977         if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
3978                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
3979                                          nlp_listp) {
3980                         if (!NLP_CHK_NODE_ACT(ndlp))
3981                                 continue;
3982                         if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3983                             ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
3984                                 lpfc_free_tx(phba, ndlp);
3985                         }
3986                 }
3987         }
3988 }
3989
3990 void
3991 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
3992 {
3993         lpfc_els_flush_rscn(vport);
3994         lpfc_els_flush_cmd(vport);
3995         lpfc_disc_flush_list(vport);
3996 }
3997
3998 /*****************************************************************************/
3999 /*
4000  * NAME:     lpfc_disc_timeout
4001  *
4002  * FUNCTION: Fibre Channel driver discovery timeout routine.
4003  *
4004  * EXECUTION ENVIRONMENT: interrupt only
4005  *
4006  * CALLED FROM:
4007  *      Timer function
4008  *
4009  * RETURNS:
4010  *      none
4011  */
4012 /*****************************************************************************/
4013 void
4014 lpfc_disc_timeout(unsigned long ptr)
4015 {
4016         struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4017         struct lpfc_hba   *phba = vport->phba;
4018         uint32_t tmo_posted;
4019         unsigned long flags = 0;
4020
4021         if (unlikely(!phba))
4022                 return;
4023
4024         spin_lock_irqsave(&vport->work_port_lock, flags);
4025         tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4026         if (!tmo_posted)
4027                 vport->work_port_events |= WORKER_DISC_TMO;
4028         spin_unlock_irqrestore(&vport->work_port_lock, flags);
4029
4030         if (!tmo_posted)
4031                 lpfc_worker_wake_up(phba);
4032         return;
4033 }
4034
4035 static void
4036 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
4037 {
4038         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4039         struct lpfc_hba  *phba = vport->phba;
4040         struct lpfc_sli  *psli = &phba->sli;
4041         struct lpfc_nodelist *ndlp, *next_ndlp;
4042         LPFC_MBOXQ_t *initlinkmbox;
4043         int rc, clrlaerr = 0;
4044
4045         if (!(vport->fc_flag & FC_DISC_TMO))
4046                 return;
4047
4048         spin_lock_irq(shost->host_lock);
4049         vport->fc_flag &= ~FC_DISC_TMO;
4050         spin_unlock_irq(shost->host_lock);
4051
4052         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4053                 "disc timeout:    state:x%x rtry:x%x flg:x%x",
4054                 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4055
4056         switch (vport->port_state) {
4057
4058         case LPFC_LOCAL_CFG_LINK:
4059         /* port_state is identically  LPFC_LOCAL_CFG_LINK while waiting for
4060          * FAN
4061          */
4062                                 /* FAN timeout */
4063                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4064                                  "0221 FAN timeout\n");
4065                 /* Start discovery by sending FLOGI, clean up old rpis */
4066                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
4067                                          nlp_listp) {
4068                         if (!NLP_CHK_NODE_ACT(ndlp))
4069                                 continue;
4070                         if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4071                                 continue;
4072                         if (ndlp->nlp_type & NLP_FABRIC) {
4073                                 /* Clean up the ndlp on Fabric connections */
4074                                 lpfc_drop_node(vport, ndlp);
4075
4076                         } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
4077                                 /* Fail outstanding IO now since device
4078                                  * is marked for PLOGI.
4079                                  */
4080                                 lpfc_unreg_rpi(vport, ndlp);
4081                         }
4082                 }
4083                 if (vport->port_state != LPFC_FLOGI) {
4084                         lpfc_initial_flogi(vport);
4085                         return;
4086                 }
4087                 break;
4088
4089         case LPFC_FDISC:
4090         case LPFC_FLOGI:
4091         /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
4092                 /* Initial FLOGI timeout */
4093                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4094                                  "0222 Initial %s timeout\n",
4095                                  vport->vpi ? "FDISC" : "FLOGI");
4096
4097                 /* Assume no Fabric and go on with discovery.
4098                  * Check for outstanding ELS FLOGI to abort.
4099                  */
4100
4101                 /* FLOGI failed, so just use loop map to make discovery list */
4102                 lpfc_disc_list_loopmap(vport);
4103
4104                 /* Start discovery */
4105                 lpfc_disc_start(vport);
4106                 break;
4107
4108         case LPFC_FABRIC_CFG_LINK:
4109         /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4110            NameServer login */
4111                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4112                                  "0223 Timeout while waiting for "
4113                                  "NameServer login\n");
4114                 /* Next look for NameServer ndlp */
4115                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
4116                 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4117                         lpfc_els_abort(phba, ndlp);
4118
4119                 /* ReStart discovery */
4120                 goto restart_disc;
4121
4122         case LPFC_NS_QRY:
4123         /* Check for wait for NameServer Rsp timeout */
4124                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4125                                  "0224 NameServer Query timeout "
4126                                  "Data: x%x x%x\n",
4127                                  vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
4128
4129                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4130                         /* Try it one more time */
4131                         vport->fc_ns_retry++;
4132                         rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4133                                          vport->fc_ns_retry, 0);
4134                         if (rc == 0)
4135                                 break;
4136                 }
4137                 vport->fc_ns_retry = 0;
4138
4139 restart_disc:
4140                 /*
4141                  * Discovery is over.
4142                  * set port_state to PORT_READY if SLI2.
4143                  * cmpl_reg_vpi will set port_state to READY for SLI3.
4144                  */
4145                 if (phba->sli_rev < LPFC_SLI_REV4) {
4146                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4147                                 lpfc_issue_reg_vpi(phba, vport);
4148                         else  { /* NPIV Not enabled */
4149                                 lpfc_issue_clear_la(phba, vport);
4150                                 vport->port_state = LPFC_VPORT_READY;
4151                         }
4152                 }
4153
4154                 /* Setup and issue mailbox INITIALIZE LINK command */
4155                 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4156                 if (!initlinkmbox) {
4157                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4158                                          "0206 Device Discovery "
4159                                          "completion error\n");
4160                         phba->link_state = LPFC_HBA_ERROR;
4161                         break;
4162                 }
4163
4164                 lpfc_linkdown(phba);
4165                 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4166                                phba->cfg_link_speed);
4167                 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
4168                 initlinkmbox->vport = vport;
4169                 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4170                 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
4171                 lpfc_set_loopback_flag(phba);
4172                 if (rc == MBX_NOT_FINISHED)
4173                         mempool_free(initlinkmbox, phba->mbox_mem_pool);
4174
4175                 break;
4176
4177         case LPFC_DISC_AUTH:
4178         /* Node Authentication timeout */
4179                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4180                                  "0227 Node Authentication timeout\n");
4181                 lpfc_disc_flush_list(vport);
4182
4183                 /*
4184                  * set port_state to PORT_READY if SLI2.
4185                  * cmpl_reg_vpi will set port_state to READY for SLI3.
4186                  */
4187                 if (phba->sli_rev < LPFC_SLI_REV4) {
4188                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4189                                 lpfc_issue_reg_vpi(phba, vport);
4190                         else  { /* NPIV Not enabled */
4191                                 lpfc_issue_clear_la(phba, vport);
4192                                 vport->port_state = LPFC_VPORT_READY;
4193                         }
4194                 }
4195                 break;
4196
4197         case LPFC_VPORT_READY:
4198                 if (vport->fc_flag & FC_RSCN_MODE) {
4199                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4200                                          "0231 RSCN timeout Data: x%x "
4201                                          "x%x\n",
4202                                          vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
4203
4204                         /* Cleanup any outstanding ELS commands */
4205                         lpfc_els_flush_cmd(vport);
4206
4207                         lpfc_els_flush_rscn(vport);
4208                         lpfc_disc_flush_list(vport);
4209                 }
4210                 break;
4211
4212         default:
4213                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4214                                  "0273 Unexpected discovery timeout, "
4215                                  "vport State x%x\n", vport->port_state);
4216                 break;
4217         }
4218
4219         switch (phba->link_state) {
4220         case LPFC_CLEAR_LA:
4221                                 /* CLEAR LA timeout */
4222                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4223                                  "0228 CLEAR LA timeout\n");
4224                 clrlaerr = 1;
4225                 break;
4226
4227         case LPFC_LINK_UP:
4228                 lpfc_issue_clear_la(phba, vport);
4229                 /* Drop thru */
4230         case LPFC_LINK_UNKNOWN:
4231         case LPFC_WARM_START:
4232         case LPFC_INIT_START:
4233         case LPFC_INIT_MBX_CMDS:
4234         case LPFC_LINK_DOWN:
4235         case LPFC_HBA_ERROR:
4236                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4237                                  "0230 Unexpected timeout, hba link "
4238                                  "state x%x\n", phba->link_state);
4239                 clrlaerr = 1;
4240                 break;
4241
4242         case LPFC_HBA_READY:
4243                 break;
4244         }
4245
4246         if (clrlaerr) {
4247                 lpfc_disc_flush_list(vport);
4248                 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4249                 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4250                 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4251                 vport->port_state = LPFC_VPORT_READY;
4252         }
4253
4254         return;
4255 }
4256
4257 /*
4258  * This routine handles processing a NameServer REG_LOGIN mailbox
4259  * command upon completion. It is setup in the LPFC_MBOXQ
4260  * as the completion routine when the command is
4261  * handed off to the SLI layer.
4262  */
4263 void
4264 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4265 {
4266         MAILBOX_t *mb = &pmb->u.mb;
4267         struct lpfc_dmabuf   *mp = (struct lpfc_dmabuf *) (pmb->context1);
4268         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4269         struct lpfc_vport    *vport = pmb->vport;
4270
4271         pmb->context1 = NULL;
4272
4273         ndlp->nlp_rpi = mb->un.varWords[0];
4274         ndlp->nlp_flag |= NLP_RPI_VALID;
4275         ndlp->nlp_type |= NLP_FABRIC;
4276         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4277
4278         /*
4279          * Start issuing Fabric-Device Management Interface (FDMI) command to
4280          * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4281          * fdmi-on=2 (supporting RPA/hostnmae)
4282          */
4283
4284         if (vport->cfg_fdmi_on == 1)
4285                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4286         else
4287                 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
4288
4289         /* decrement the node reference count held for this callback
4290          * function.
4291          */
4292         lpfc_nlp_put(ndlp);
4293         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4294         kfree(mp);
4295         mempool_free(pmb, phba->mbox_mem_pool);
4296
4297         return;
4298 }
4299
4300 static int
4301 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4302 {
4303         uint16_t *rpi = param;
4304
4305         return ndlp->nlp_rpi == *rpi;
4306 }
4307
4308 static int
4309 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4310 {
4311         return memcmp(&ndlp->nlp_portname, param,
4312                       sizeof(ndlp->nlp_portname)) == 0;
4313 }
4314
4315 static struct lpfc_nodelist *
4316 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
4317 {
4318         struct lpfc_nodelist *ndlp;
4319
4320         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4321                 if (filter(ndlp, param))
4322                         return ndlp;
4323         }
4324         return NULL;
4325 }
4326
4327 /*
4328  * This routine looks up the ndlp lists for the given RPI. If rpi found it
4329  * returns the node list element pointer else return NULL.
4330  */
4331 struct lpfc_nodelist *
4332 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
4333 {
4334         return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
4335 }
4336
4337 /*
4338  * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
4339  * returns the node element list pointer else return NULL.
4340  */
4341 struct lpfc_nodelist *
4342 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
4343 {
4344         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4345         struct lpfc_nodelist *ndlp;
4346
4347         spin_lock_irq(shost->host_lock);
4348         ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4349         spin_unlock_irq(shost->host_lock);
4350         return ndlp;
4351 }
4352
4353 void
4354 lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4355               uint32_t did)
4356 {
4357         memset(ndlp, 0, sizeof (struct lpfc_nodelist));
4358
4359         lpfc_initialize_node(vport, ndlp, did);
4360         INIT_LIST_HEAD(&ndlp->nlp_listp);
4361
4362         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4363                 "node init:       did:x%x",
4364                 ndlp->nlp_DID, 0, 0);
4365
4366         return;
4367 }
4368
4369 /* This routine releases all resources associated with a specifc NPort's ndlp
4370  * and mempool_free's the nodelist.
4371  */
4372 static void
4373 lpfc_nlp_release(struct kref *kref)
4374 {
4375         struct lpfc_hba *phba;
4376         unsigned long flags;
4377         struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4378                                                   kref);
4379
4380         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4381                 "node release:    did:x%x flg:x%x type:x%x",
4382                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4383
4384         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4385                         "0279 lpfc_nlp_release: ndlp:x%p "
4386                         "usgmap:x%x refcnt:%d\n",
4387                         (void *)ndlp, ndlp->nlp_usg_map,
4388                         atomic_read(&ndlp->kref.refcount));
4389
4390         /* remove ndlp from action. */
4391         lpfc_nlp_remove(ndlp->vport, ndlp);
4392
4393         /* clear the ndlp active flag for all release cases */
4394         phba = ndlp->phba;
4395         spin_lock_irqsave(&phba->ndlp_lock, flags);
4396         NLP_CLR_NODE_ACT(ndlp);
4397         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4398
4399         /* free ndlp memory for final ndlp release */
4400         if (NLP_CHK_FREE_REQ(ndlp)) {
4401                 kfree(ndlp->lat_data);
4402                 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
4403         }
4404 }
4405
4406 /* This routine bumps the reference count for a ndlp structure to ensure
4407  * that one discovery thread won't free a ndlp while another discovery thread
4408  * is using it.
4409  */
4410 struct lpfc_nodelist *
4411 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4412 {
4413         struct lpfc_hba *phba;
4414         unsigned long flags;
4415
4416         if (ndlp) {
4417                 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4418                         "node get:        did:x%x flg:x%x refcnt:x%x",
4419                         ndlp->nlp_DID, ndlp->nlp_flag,
4420                         atomic_read(&ndlp->kref.refcount));
4421                 /* The check of ndlp usage to prevent incrementing the
4422                  * ndlp reference count that is in the process of being
4423                  * released.
4424                  */
4425                 phba = ndlp->phba;
4426                 spin_lock_irqsave(&phba->ndlp_lock, flags);
4427                 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4428                         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4429                         lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4430                                 "0276 lpfc_nlp_get: ndlp:x%p "
4431                                 "usgmap:x%x refcnt:%d\n",
4432                                 (void *)ndlp, ndlp->nlp_usg_map,
4433                                 atomic_read(&ndlp->kref.refcount));
4434                         return NULL;
4435                 } else
4436                         kref_get(&ndlp->kref);
4437                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4438         }
4439         return ndlp;
4440 }
4441
4442 /* This routine decrements the reference count for a ndlp structure. If the
4443  * count goes to 0, this indicates the the associated nodelist should be
4444  * freed. Returning 1 indicates the ndlp resource has been released; on the
4445  * other hand, returning 0 indicates the ndlp resource has not been released
4446  * yet.
4447  */
4448 int
4449 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4450 {
4451         struct lpfc_hba *phba;
4452         unsigned long flags;
4453
4454         if (!ndlp)
4455                 return 1;
4456
4457         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4458         "node put:        did:x%x flg:x%x refcnt:x%x",
4459                 ndlp->nlp_DID, ndlp->nlp_flag,
4460                 atomic_read(&ndlp->kref.refcount));
4461         phba = ndlp->phba;
4462         spin_lock_irqsave(&phba->ndlp_lock, flags);
4463         /* Check the ndlp memory free acknowledge flag to avoid the
4464          * possible race condition that kref_put got invoked again
4465          * after previous one has done ndlp memory free.
4466          */
4467         if (NLP_CHK_FREE_ACK(ndlp)) {
4468                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4469                 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4470                                 "0274 lpfc_nlp_put: ndlp:x%p "
4471                                 "usgmap:x%x refcnt:%d\n",
4472                                 (void *)ndlp, ndlp->nlp_usg_map,
4473                                 atomic_read(&ndlp->kref.refcount));
4474                 return 1;
4475         }
4476         /* Check the ndlp inactivate log flag to avoid the possible
4477          * race condition that kref_put got invoked again after ndlp
4478          * is already in inactivating state.
4479          */
4480         if (NLP_CHK_IACT_REQ(ndlp)) {
4481                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4482                 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4483                                 "0275 lpfc_nlp_put: ndlp:x%p "
4484                                 "usgmap:x%x refcnt:%d\n",
4485                                 (void *)ndlp, ndlp->nlp_usg_map,
4486                                 atomic_read(&ndlp->kref.refcount));
4487                 return 1;
4488         }
4489         /* For last put, mark the ndlp usage flags to make sure no
4490          * other kref_get and kref_put on the same ndlp shall get
4491          * in between the process when the final kref_put has been
4492          * invoked on this ndlp.
4493          */
4494         if (atomic_read(&ndlp->kref.refcount) == 1) {
4495                 /* Indicate ndlp is put to inactive state. */
4496                 NLP_SET_IACT_REQ(ndlp);
4497                 /* Acknowledge ndlp memory free has been seen. */
4498                 if (NLP_CHK_FREE_REQ(ndlp))
4499                         NLP_SET_FREE_ACK(ndlp);
4500         }
4501         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4502         /* Note, the kref_put returns 1 when decrementing a reference
4503          * count that was 1, it invokes the release callback function,
4504          * but it still left the reference count as 1 (not actually
4505          * performs the last decrementation). Otherwise, it actually
4506          * decrements the reference count and returns 0.
4507          */
4508         return kref_put(&ndlp->kref, lpfc_nlp_release);
4509 }
4510
4511 /* This routine free's the specified nodelist if it is not in use
4512  * by any other discovery thread. This routine returns 1 if the
4513  * ndlp has been freed. A return value of 0 indicates the ndlp is
4514  * not yet been released.
4515  */
4516 int
4517 lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4518 {
4519         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4520                 "node not used:   did:x%x flg:x%x refcnt:x%x",
4521                 ndlp->nlp_DID, ndlp->nlp_flag,
4522                 atomic_read(&ndlp->kref.refcount));
4523         if (atomic_read(&ndlp->kref.refcount) == 1)
4524                 if (lpfc_nlp_put(ndlp))
4525                         return 1;
4526         return 0;
4527 }
4528
4529 /**
4530  * lpfc_fcf_inuse - Check if FCF can be unregistered.
4531  * @phba: Pointer to hba context object.
4532  *
4533  * This function iterate through all FC nodes associated
4534  * will all vports to check if there is any node with
4535  * fc_rports associated with it. If there is an fc_rport
4536  * associated with the node, then the node is either in
4537  * discovered state or its devloss_timer is pending.
4538  */
4539 static int
4540 lpfc_fcf_inuse(struct lpfc_hba *phba)
4541 {
4542         struct lpfc_vport **vports;
4543         int i, ret = 0;
4544         struct lpfc_nodelist *ndlp;
4545         struct Scsi_Host  *shost;
4546
4547         vports = lpfc_create_vport_work_array(phba);
4548
4549         for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4550                 shost = lpfc_shost_from_vport(vports[i]);
4551                 spin_lock_irq(shost->host_lock);
4552                 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4553                         if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4554                           (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4555                                 ret = 1;
4556                                 spin_unlock_irq(shost->host_lock);
4557                                 goto out;
4558                         } else {
4559                                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4560                                         "2624 RPI %x DID %x flg %x still "
4561                                         "logged in\n",
4562                                         ndlp->nlp_rpi, ndlp->nlp_DID,
4563                                         ndlp->nlp_flag);
4564                                 if (ndlp->nlp_flag & NLP_RPI_VALID)
4565                                         ret = 1;
4566                         }
4567                 }
4568                 spin_unlock_irq(shost->host_lock);
4569         }
4570 out:
4571         lpfc_destroy_vport_work_array(phba, vports);
4572         return ret;
4573 }
4574
4575 /**
4576  * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4577  * @phba: Pointer to hba context object.
4578  * @mboxq: Pointer to mailbox object.
4579  *
4580  * This function frees memory associated with the mailbox command.
4581  */
4582 static void
4583 lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4584 {
4585         struct lpfc_vport *vport = mboxq->vport;
4586
4587         if (mboxq->u.mb.mbxStatus) {
4588                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4589                         "2555 UNREG_VFI mbxStatus error x%x "
4590                         "HBA state x%x\n",
4591                         mboxq->u.mb.mbxStatus, vport->port_state);
4592         }
4593         mempool_free(mboxq, phba->mbox_mem_pool);
4594         return;
4595 }
4596
4597 /**
4598  * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4599  * @phba: Pointer to hba context object.
4600  * @mboxq: Pointer to mailbox object.
4601  *
4602  * This function frees memory associated with the mailbox command.
4603  */
4604 static void
4605 lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4606 {
4607         struct lpfc_vport *vport = mboxq->vport;
4608
4609         if (mboxq->u.mb.mbxStatus) {
4610                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4611                         "2550 UNREG_FCFI mbxStatus error x%x "
4612                         "HBA state x%x\n",
4613                         mboxq->u.mb.mbxStatus, vport->port_state);
4614         }
4615         mempool_free(mboxq, phba->mbox_mem_pool);
4616         return;
4617 }
4618
4619 /**
4620  * lpfc_unregister_fcf_prep - Unregister fcf record preparation
4621  * @phba: Pointer to hba context object.
4622  *
4623  * This function prepare the HBA for unregistering the currently registered
4624  * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
4625  * VFIs.
4626  */
4627 int
4628 lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
4629 {
4630         LPFC_MBOXQ_t *mbox;
4631         struct lpfc_vport **vports;
4632         struct lpfc_nodelist *ndlp;
4633         struct Scsi_Host *shost;
4634         int i, rc;
4635
4636         /* Unregister RPIs */
4637         if (lpfc_fcf_inuse(phba))
4638                 lpfc_unreg_hba_rpis(phba);
4639
4640         /* At this point, all discovery is aborted */
4641         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4642
4643         /* Unregister VPIs */
4644         vports = lpfc_create_vport_work_array(phba);
4645         if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
4646                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4647                         /* Stop FLOGI/FDISC retries */
4648                         ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
4649                         if (ndlp)
4650                                 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
4651                         lpfc_mbx_unreg_vpi(vports[i]);
4652                         shost = lpfc_shost_from_vport(vports[i]);
4653                         spin_lock_irq(shost->host_lock);
4654                         vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
4655                         vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
4656                         spin_unlock_irq(shost->host_lock);
4657                 }
4658         lpfc_destroy_vport_work_array(phba, vports);
4659
4660         /* Cleanup any outstanding ELS commands */
4661         lpfc_els_flush_all_cmd(phba);
4662
4663         /* Unregister VFI */
4664         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4665         if (!mbox) {
4666                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4667                                 "2556 UNREG_VFI mbox allocation failed"
4668                                 "HBA state x%x\n", phba->pport->port_state);
4669                 return -ENOMEM;
4670         }
4671
4672         lpfc_unreg_vfi(mbox, phba->pport);
4673         mbox->vport = phba->pport;
4674         mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
4675
4676         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4677         if (rc == MBX_NOT_FINISHED) {
4678                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4679                                 "2557 UNREG_VFI issue mbox failed rc x%x "
4680                                 "HBA state x%x\n",
4681                                 rc, phba->pport->port_state);
4682                 mempool_free(mbox, phba->mbox_mem_pool);
4683                 return -EIO;
4684         }
4685
4686         shost = lpfc_shost_from_vport(phba->pport);
4687         spin_lock_irq(shost->host_lock);
4688         phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
4689         spin_unlock_irq(shost->host_lock);
4690
4691         return 0;
4692 }
4693
4694 /**
4695  * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
4696  * @phba: Pointer to hba context object.
4697  *
4698  * This function issues synchronous unregister FCF mailbox command to HBA to
4699  * unregister the currently registered FCF record. The driver does not reset
4700  * the driver FCF usage state flags.
4701  *
4702  * Return 0 if successfully issued, none-zero otherwise.
4703  */
4704 int
4705 lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
4706 {
4707         LPFC_MBOXQ_t *mbox;
4708         int rc;
4709
4710         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4711         if (!mbox) {
4712                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4713                                 "2551 UNREG_FCFI mbox allocation failed"
4714                                 "HBA state x%x\n", phba->pport->port_state);
4715                 return -ENOMEM;
4716         }
4717         lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
4718         mbox->vport = phba->pport;
4719         mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
4720         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4721
4722         if (rc == MBX_NOT_FINISHED) {
4723                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4724                                 "2552 Unregister FCFI command failed rc x%x "
4725                                 "HBA state x%x\n",
4726                                 rc, phba->pport->port_state);
4727                 return -EINVAL;
4728         }
4729         return 0;
4730 }
4731
4732 /**
4733  * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
4734  * @phba: Pointer to hba context object.
4735  *
4736  * This function unregisters the currently reigstered FCF. This function
4737  * also tries to find another FCF for discovery by rescan the HBA FCF table.
4738  */
4739 void
4740 lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
4741 {
4742         int rc;
4743
4744         /* Preparation for unregistering fcf */
4745         rc = lpfc_unregister_fcf_prep(phba);
4746         if (rc) {
4747                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4748                                 "2748 Failed to prepare for unregistering "
4749                                 "HBA's FCF record: rc=%d\n", rc);
4750                 return;
4751         }
4752
4753         /* Now, unregister FCF record and reset HBA FCF state */
4754         rc = lpfc_sli4_unregister_fcf(phba);
4755         if (rc)
4756                 return;
4757         /* Reset HBA FCF states after successful unregister FCF */
4758         phba->fcf.fcf_flag = 0;
4759
4760         /*
4761          * If driver is not unloading, check if there is any other
4762          * FCF record that can be used for discovery.
4763          */
4764         if ((phba->pport->load_flag & FC_UNLOADING) ||
4765             (phba->link_state < LPFC_LINK_UP))
4766                 return;
4767
4768         rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
4769
4770         if (rc)
4771                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4772                                 "2553 lpfc_unregister_unused_fcf failed "
4773                                 "to read FCF record HBA state x%x\n",
4774                                 phba->pport->port_state);
4775 }
4776
4777 /**
4778  * lpfc_unregister_fcf - Unregister the currently registered fcf record
4779  * @phba: Pointer to hba context object.
4780  *
4781  * This function just unregisters the currently reigstered FCF. It does not
4782  * try to find another FCF for discovery.
4783  */
4784 void
4785 lpfc_unregister_fcf(struct lpfc_hba *phba)
4786 {
4787         int rc;
4788
4789         /* Preparation for unregistering fcf */
4790         rc = lpfc_unregister_fcf_prep(phba);
4791         if (rc) {
4792                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4793                                 "2749 Failed to prepare for unregistering "
4794                                 "HBA's FCF record: rc=%d\n", rc);
4795                 return;
4796         }
4797
4798         /* Now, unregister FCF record and reset HBA FCF state */
4799         rc = lpfc_sli4_unregister_fcf(phba);
4800         if (rc)
4801                 return;
4802         /* Set proper HBA FCF states after successful unregister FCF */
4803         spin_lock_irq(&phba->hbalock);
4804         phba->fcf.fcf_flag &= ~FCF_REGISTERED;
4805         spin_unlock_irq(&phba->hbalock);
4806 }
4807
4808 /**
4809  * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
4810  * @phba: Pointer to hba context object.
4811  *
4812  * This function check if there are any connected remote port for the FCF and
4813  * if all the devices are disconnected, this function unregister FCFI.
4814  * This function also tries to use another FCF for discovery.
4815  */
4816 void
4817 lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
4818 {
4819         /*
4820          * If HBA is not running in FIP mode or if HBA does not support
4821          * FCoE or if FCF is not registered, do nothing.
4822          */
4823         spin_lock_irq(&phba->hbalock);
4824         if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
4825             !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
4826             !(phba->hba_flag & HBA_FIP_SUPPORT)) {
4827                 spin_unlock_irq(&phba->hbalock);
4828                 return;
4829         }
4830         spin_unlock_irq(&phba->hbalock);
4831
4832         if (lpfc_fcf_inuse(phba))
4833                 return;
4834
4835         lpfc_unregister_fcf_rescan(phba);
4836 }
4837
4838 /**
4839  * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
4840  * @phba: Pointer to hba context object.
4841  * @buff: Buffer containing the FCF connection table as in the config
4842  *         region.
4843  * This function create driver data structure for the FCF connection
4844  * record table read from config region 23.
4845  */
4846 static void
4847 lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
4848         uint8_t *buff)
4849 {
4850         struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4851         struct lpfc_fcf_conn_hdr *conn_hdr;
4852         struct lpfc_fcf_conn_rec *conn_rec;
4853         uint32_t record_count;
4854         int i;
4855
4856         /* Free the current connect table */
4857         list_for_each_entry_safe(conn_entry, next_conn_entry,
4858                 &phba->fcf_conn_rec_list, list) {
4859                 list_del_init(&conn_entry->list);
4860                 kfree(conn_entry);
4861         }
4862
4863         conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
4864         record_count = conn_hdr->length * sizeof(uint32_t)/
4865                 sizeof(struct lpfc_fcf_conn_rec);
4866
4867         conn_rec = (struct lpfc_fcf_conn_rec *)
4868                 (buff + sizeof(struct lpfc_fcf_conn_hdr));
4869
4870         for (i = 0; i < record_count; i++) {
4871                 if (!(conn_rec[i].flags & FCFCNCT_VALID))
4872                         continue;
4873                 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
4874                         GFP_KERNEL);
4875                 if (!conn_entry) {
4876                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4877                                 "2566 Failed to allocate connection"
4878                                 " table entry\n");
4879                         return;
4880                 }
4881
4882                 memcpy(&conn_entry->conn_rec, &conn_rec[i],
4883                         sizeof(struct lpfc_fcf_conn_rec));
4884                 conn_entry->conn_rec.vlan_tag =
4885                         le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
4886                 conn_entry->conn_rec.flags =
4887                         le16_to_cpu(conn_entry->conn_rec.flags);
4888                 list_add_tail(&conn_entry->list,
4889                         &phba->fcf_conn_rec_list);
4890         }
4891 }
4892
4893 /**
4894  * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
4895  * @phba: Pointer to hba context object.
4896  * @buff: Buffer containing the FCoE parameter data structure.
4897  *
4898  *  This function update driver data structure with config
4899  *  parameters read from config region 23.
4900  */
4901 static void
4902 lpfc_read_fcoe_param(struct lpfc_hba *phba,
4903                         uint8_t *buff)
4904 {
4905         struct lpfc_fip_param_hdr *fcoe_param_hdr;
4906         struct lpfc_fcoe_params *fcoe_param;
4907
4908         fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
4909                 buff;
4910         fcoe_param = (struct lpfc_fcoe_params *)
4911                 (buff + sizeof(struct lpfc_fip_param_hdr));
4912
4913         if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
4914                 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
4915                 return;
4916
4917         if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
4918                 phba->valid_vlan = 1;
4919                 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
4920                         0xFFF;
4921         }
4922
4923         phba->fc_map[0] = fcoe_param->fc_map[0];
4924         phba->fc_map[1] = fcoe_param->fc_map[1];
4925         phba->fc_map[2] = fcoe_param->fc_map[2];
4926         return;
4927 }
4928
4929 /**
4930  * lpfc_get_rec_conf23 - Get a record type in config region data.
4931  * @buff: Buffer containing config region 23 data.
4932  * @size: Size of the data buffer.
4933  * @rec_type: Record type to be searched.
4934  *
4935  * This function searches config region data to find the begining
4936  * of the record specified by record_type. If record found, this
4937  * function return pointer to the record else return NULL.
4938  */
4939 static uint8_t *
4940 lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
4941 {
4942         uint32_t offset = 0, rec_length;
4943
4944         if ((buff[0] == LPFC_REGION23_LAST_REC) ||
4945                 (size < sizeof(uint32_t)))
4946                 return NULL;
4947
4948         rec_length = buff[offset + 1];
4949
4950         /*
4951          * One TLV record has one word header and number of data words
4952          * specified in the rec_length field of the record header.
4953          */
4954         while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
4955                 <= size) {
4956                 if (buff[offset] == rec_type)
4957                         return &buff[offset];
4958
4959                 if (buff[offset] == LPFC_REGION23_LAST_REC)
4960                         return NULL;
4961
4962                 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
4963                 rec_length = buff[offset + 1];
4964         }
4965         return NULL;
4966 }
4967
4968 /**
4969  * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
4970  * @phba: Pointer to lpfc_hba data structure.
4971  * @buff: Buffer containing config region 23 data.
4972  * @size: Size of the data buffer.
4973  *
4974  * This fuction parse the FCoE config parameters in config region 23 and
4975  * populate driver data structure with the parameters.
4976  */
4977 void
4978 lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
4979                 uint8_t *buff,
4980                 uint32_t size)
4981 {
4982         uint32_t offset = 0, rec_length;
4983         uint8_t *rec_ptr;
4984
4985         /*
4986          * If data size is less than 2 words signature and version cannot be
4987          * verified.
4988          */
4989         if (size < 2*sizeof(uint32_t))
4990                 return;
4991
4992         /* Check the region signature first */
4993         if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
4994                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4995                         "2567 Config region 23 has bad signature\n");
4996                 return;
4997         }
4998
4999         offset += 4;
5000
5001         /* Check the data structure version */
5002         if (buff[offset] != LPFC_REGION23_VERSION) {
5003                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5004                         "2568 Config region 23 has bad version\n");
5005                 return;
5006         }
5007         offset += 4;
5008
5009         rec_length = buff[offset + 1];
5010
5011         /* Read FCoE param record */
5012         rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5013                         size - offset, FCOE_PARAM_TYPE);
5014         if (rec_ptr)
5015                 lpfc_read_fcoe_param(phba, rec_ptr);
5016
5017         /* Read FCF connection table */
5018         rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5019                 size - offset, FCOE_CONN_TBL_TYPE);
5020         if (rec_ptr)
5021                 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5022
5023 }