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