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