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