blob: 1f87b4fb8b507604e75186ec0fa5d3fae8c109c7 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * 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. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050024#include <linux/pci.h>
25#include <linux/kthread.h>
26#include <linux/interrupt.h>
27
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040028#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
31#include <scsi/scsi_transport_fc.h>
32
James Smartda0436e2009-05-22 14:51:39 -040033#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050034#include "lpfc_hw.h"
James Smartea2151b2008-09-07 11:52:10 -040035#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050036#include "lpfc_disc.h"
37#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_sli4.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_scsi.h"
40#include "lpfc.h"
41#include "lpfc_logmsg.h"
42#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050043#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050044#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050045
46/* AlpaArray for assignment of scsid for scan-down and bind_method */
47static uint8_t lpfcAlpaArray[] = {
48 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
49 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
50 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
51 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
52 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
53 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
54 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
55 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
56 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
57 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
58 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
59 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
60 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
61};
62
James Smart2e0fef82007-06-17 19:56:36 -050063static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010064static void lpfc_disc_flush_list(struct lpfc_vport *vport);
James Smart32b97932009-07-19 10:01:21 -040065static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
dea31012005-04-17 16:05:31 -050066
James Smartc01f3202006-08-18 17:47:08 -040067void
68lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050069{
James Smartc01f3202006-08-18 17:47:08 -040070 struct lpfc_rport_data *rdata;
71 struct lpfc_nodelist * ndlp;
72 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050073
James Smartc01f3202006-08-18 17:47:08 -040074 rdata = rport->dd_data;
75 ndlp = rdata->pnode;
76
James Smart58da1ff2008-04-07 10:15:56 -040077 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040078 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
79 printk(KERN_ERR "Cannot find remote node"
80 " to terminate I/O Data x%x\n",
81 rport->port_id);
dea31012005-04-17 16:05:31 -050082 return;
83 }
84
James Smarta257bf92009-04-06 18:48:10 -040085 phba = ndlp->phba;
James Smart1a169682006-03-07 15:04:06 -050086
James Smart858c9f62007-06-17 19:56:39 -050087 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
88 "rport terminate: sid:x%x did:x%x flg:x%x",
89 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
90
James Smartc01f3202006-08-18 17:47:08 -040091 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040092 lpfc_sli_abort_iocb(ndlp->vport,
93 &phba->sli.ring[phba->sli.fcp_ring],
94 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040095 }
James Smartc01f3202006-08-18 17:47:08 -040096}
97
98/*
99 * This function will be called when dev_loss_tmo fire.
100 */
101void
102lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
103{
104 struct lpfc_rport_data *rdata;
105 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500106 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500107 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500108 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400109 int put_node;
110 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400111
112 rdata = rport->dd_data;
113 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400114 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400115 return;
James Smartc01f3202006-08-18 17:47:08 -0400116
James Smart858c9f62007-06-17 19:56:39 -0500117 vport = ndlp->vport;
118 phba = vport->phba;
119
120 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
121 "rport devlosscb: sid:x%x did:x%x flg:x%x",
122 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
123
James Smarta8adb832007-10-27 13:37:53 -0400124 /* Don't defer this if we are in the process of deleting the vport
125 * or unloading the driver. The unload will cleanup the node
126 * appropriately we just need to cleanup the ndlp rport info here.
127 */
128 if (vport->load_flag & FC_UNLOADING) {
129 put_node = rdata->pnode != NULL;
130 put_rport = ndlp->rport != NULL;
131 rdata->pnode = NULL;
132 ndlp->rport = NULL;
133 if (put_node)
134 lpfc_nlp_put(ndlp);
135 if (put_rport)
136 put_device(&rport->dev);
137 return;
138 }
139
140 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
141 return;
142
James Smart858c9f62007-06-17 19:56:39 -0500143 evtp = &ndlp->dev_loss_evt;
144
145 if (!list_empty(&evtp->evt_listp))
146 return;
147
148 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500149 /* We need to hold the node by incrementing the reference
150 * count until this queued work is done
151 */
152 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart5e9d9b82008-06-14 22:52:53 -0400153 if (evtp->evt_arg1) {
154 evtp->evt = LPFC_EVT_DEV_LOSS;
155 list_add_tail(&evtp->evt_listp, &phba->work_list);
156 lpfc_worker_wake_up(phba);
157 }
James Smart858c9f62007-06-17 19:56:39 -0500158 spin_unlock_irq(&phba->hbalock);
159
James Smart858c9f62007-06-17 19:56:39 -0500160 return;
161}
162
163/*
164 * This function is called from the worker thread when dev_loss_tmo
165 * expire.
166 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100167static void
James Smart858c9f62007-06-17 19:56:39 -0500168lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
169{
170 struct lpfc_rport_data *rdata;
171 struct fc_rport *rport;
172 struct lpfc_vport *vport;
173 struct lpfc_hba *phba;
174 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400175 int put_node;
176 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500177 int warn_on = 0;
178
179 rport = ndlp->rport;
180
181 if (!rport)
182 return;
183
184 rdata = rport->dd_data;
185 name = (uint8_t *) &ndlp->nlp_portname;
186 vport = ndlp->vport;
187 phba = vport->phba;
188
189 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
190 "rport devlosstmo:did:x%x type:x%x id:x%x",
191 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
192
James Smarta8adb832007-10-27 13:37:53 -0400193 /* Don't defer this if we are in the process of deleting the vport
194 * or unloading the driver. The unload will cleanup the node
195 * appropriately we just need to cleanup the ndlp rport info here.
196 */
197 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500198 if (ndlp->nlp_sid != NLP_NO_SID) {
199 /* flush the target */
200 lpfc_sli_abort_iocb(vport,
201 &phba->sli.ring[phba->sli.fcp_ring],
202 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
203 }
James Smarta8adb832007-10-27 13:37:53 -0400204 put_node = rdata->pnode != NULL;
205 put_rport = ndlp->rport != NULL;
206 rdata->pnode = NULL;
207 ndlp->rport = NULL;
208 if (put_node)
209 lpfc_nlp_put(ndlp);
210 if (put_rport)
211 put_device(&rport->dev);
212 return;
213 }
214
James Smartd7c255b2008-08-24 21:50:00 -0400215 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
216 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
217 "0284 Devloss timeout Ignored on "
218 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
219 "NPort x%x\n",
220 *name, *(name+1), *(name+2), *(name+3),
221 *(name+4), *(name+5), *(name+6), *(name+7),
222 ndlp->nlp_DID);
James Smart858c9f62007-06-17 19:56:39 -0500223 return;
James Smartd7c255b2008-08-24 21:50:00 -0400224 }
James Smart858c9f62007-06-17 19:56:39 -0500225
James Smart92d7f7b2007-06-17 19:56:38 -0500226 if (ndlp->nlp_type & NLP_FABRIC) {
227 /* We will clean up these Nodes in linkup */
228 put_node = rdata->pnode != NULL;
229 put_rport = ndlp->rport != NULL;
230 rdata->pnode = NULL;
231 ndlp->rport = NULL;
232 if (put_node)
233 lpfc_nlp_put(ndlp);
234 if (put_rport)
235 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400236 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500237 }
James Smart82085712007-04-25 09:52:41 -0400238
dea31012005-04-17 16:05:31 -0500239 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400240 warn_on = 1;
dea31012005-04-17 16:05:31 -0500241 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400242 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
243 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500244 }
James Smartc01f3202006-08-18 17:47:08 -0400245
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400246 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400247 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
248 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400249 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
250 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400251 *name, *(name+1), *(name+2), *(name+3),
252 *(name+4), *(name+5), *(name+6), *(name+7),
253 ndlp->nlp_DID, ndlp->nlp_flag,
254 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400255 } else {
James Smarte8b62012007-08-02 11:10:09 -0400256 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
257 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400258 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
259 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400260 *name, *(name+1), *(name+2), *(name+3),
261 *(name+4), *(name+5), *(name+6), *(name+7),
262 ndlp->nlp_DID, ndlp->nlp_flag,
263 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400264 }
265
James Smart87af33f2007-10-27 13:37:43 -0400266 put_node = rdata->pnode != NULL;
267 put_rport = ndlp->rport != NULL;
268 rdata->pnode = NULL;
269 ndlp->rport = NULL;
270 if (put_node)
271 lpfc_nlp_put(ndlp);
272 if (put_rport)
273 put_device(&rport->dev);
274
James Smart2e0fef82007-06-17 19:56:36 -0500275 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400276 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400277 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smarte47c9092008-02-08 18:49:26 -0500278 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
James Smart2e0fef82007-06-17 19:56:36 -0500279 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400280
281 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500282}
James Smartc01f3202006-08-18 17:47:08 -0400283
James Smartea2151b2008-09-07 11:52:10 -0400284/**
James Smart3621a712009-04-06 18:47:14 -0400285 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400286 * @phba: Pointer to hba context object.
287 *
288 * This function is called from the functions which need to post
289 * events from interrupt context. This function allocates data
290 * structure required for posting event. It also keeps track of
291 * number of events pending and prevent event storm when there are
292 * too many events.
293 **/
294struct lpfc_fast_path_event *
295lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
296 struct lpfc_fast_path_event *ret;
297
298 /* If there are lot of fast event do not exhaust memory due to this */
299 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
300 return NULL;
301
302 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
303 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400304 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400305 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400306 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
307 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
308 }
James Smartea2151b2008-09-07 11:52:10 -0400309 return ret;
310}
311
312/**
James Smart3621a712009-04-06 18:47:14 -0400313 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400314 * @phba: Pointer to hba context object.
315 * @evt: Event object which need to be freed.
316 *
317 * This function frees the data structure required for posting
318 * events.
319 **/
320void
321lpfc_free_fast_evt(struct lpfc_hba *phba,
322 struct lpfc_fast_path_event *evt) {
323
324 atomic_dec(&phba->fast_event_count);
325 kfree(evt);
326}
327
328/**
James Smart3621a712009-04-06 18:47:14 -0400329 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400330 * @phba: Pointer to hba context object.
331 * @evtp: Event data structure.
332 *
333 * This function is called from worker thread, when the interrupt
334 * context need to post an event. This function posts the event
335 * to fc transport netlink interface.
336 **/
337static void
338lpfc_send_fastpath_evt(struct lpfc_hba *phba,
339 struct lpfc_work_evt *evtp)
340{
341 unsigned long evt_category, evt_sub_category;
342 struct lpfc_fast_path_event *fast_evt_data;
343 char *evt_data;
344 uint32_t evt_data_size;
345 struct Scsi_Host *shost;
346
347 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
348 work_evt);
349
350 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
351 evt_sub_category = (unsigned long) fast_evt_data->un.
352 fabric_evt.subcategory;
353 shost = lpfc_shost_from_vport(fast_evt_data->vport);
354 if (evt_category == FC_REG_FABRIC_EVENT) {
355 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
356 evt_data = (char *) &fast_evt_data->un.read_check_error;
357 evt_data_size = sizeof(fast_evt_data->un.
358 read_check_error);
359 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500360 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400361 evt_data = (char *) &fast_evt_data->un.fabric_evt;
362 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
363 } else {
364 lpfc_free_fast_evt(phba, fast_evt_data);
365 return;
366 }
367 } else if (evt_category == FC_REG_SCSI_EVENT) {
368 switch (evt_sub_category) {
369 case LPFC_EVENT_QFULL:
370 case LPFC_EVENT_DEVBSY:
371 evt_data = (char *) &fast_evt_data->un.scsi_evt;
372 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
373 break;
374 case LPFC_EVENT_CHECK_COND:
375 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
376 evt_data_size = sizeof(fast_evt_data->un.
377 check_cond_evt);
378 break;
379 case LPFC_EVENT_VARQUEDEPTH:
380 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
381 evt_data_size = sizeof(fast_evt_data->un.
382 queue_depth_evt);
383 break;
384 default:
385 lpfc_free_fast_evt(phba, fast_evt_data);
386 return;
387 }
388 } else {
389 lpfc_free_fast_evt(phba, fast_evt_data);
390 return;
391 }
392
393 fc_host_post_vendor_event(shost,
394 fc_get_event_number(),
395 evt_data_size,
396 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500397 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400398
399 lpfc_free_fast_evt(phba, fast_evt_data);
400 return;
401}
402
dea31012005-04-17 16:05:31 -0500403static void
James Smart2e0fef82007-06-17 19:56:36 -0500404lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500405{
406 struct lpfc_work_evt *evtp = NULL;
407 struct lpfc_nodelist *ndlp;
408 int free_evt;
409
James Smart2e0fef82007-06-17 19:56:36 -0500410 spin_lock_irq(&phba->hbalock);
411 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500412 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
413 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500414 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500415 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500416 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500417 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500418 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500419 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500420 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500421 /* decrement the node reference count held
422 * for this queued work
423 */
424 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500425 break;
James Smart858c9f62007-06-17 19:56:39 -0500426 case LPFC_EVT_DEV_LOSS:
427 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500428 lpfc_dev_loss_tmo_handler(ndlp);
429 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500430 /* decrement the node reference count held for
431 * this queued work
432 */
James Smart858c9f62007-06-17 19:56:39 -0500433 lpfc_nlp_put(ndlp);
434 break;
dea31012005-04-17 16:05:31 -0500435 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500436 if (phba->link_state < LPFC_LINK_DOWN)
437 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500438 else
James Smart2e0fef82007-06-17 19:56:36 -0500439 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500440 complete((struct completion *)(evtp->evt_arg2));
441 break;
James Smart46fa3112007-04-25 09:51:45 -0400442 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500443 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400444 lpfc_offline_prep(phba);
445 *(int *)(evtp->evt_arg1) = 0;
446 complete((struct completion *)(evtp->evt_arg2));
447 break;
448 case LPFC_EVT_OFFLINE:
449 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500450 lpfc_sli_brdrestart(phba);
451 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400452 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
453 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500454 complete((struct completion *)(evtp->evt_arg2));
455 break;
456 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400457 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500458 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500459 lpfc_sli_brdreset(phba);
460 lpfc_hba_down_post(phba);
461 *(int *)(evtp->evt_arg1) =
462 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400463 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500464 complete((struct completion *)(evtp->evt_arg2));
465 break;
466 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400467 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500468 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500469 = (phba->pport->stopped)
470 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400471 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500472 complete((struct completion *)(evtp->evt_arg2));
473 break;
James Smartea2151b2008-09-07 11:52:10 -0400474 case LPFC_EVT_FASTPATH_MGMT_EVT:
475 lpfc_send_fastpath_evt(phba, evtp);
476 free_evt = 0;
477 break;
James Smart78730cf2010-04-06 15:06:30 -0400478 case LPFC_EVT_RESET_HBA:
479 if (!(phba->pport->load_flag & FC_UNLOADING))
480 lpfc_reset_hba(phba);
481 break;
dea31012005-04-17 16:05:31 -0500482 }
483 if (free_evt)
484 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500485 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500486 }
James Smart2e0fef82007-06-17 19:56:36 -0500487 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500488
489}
490
James Smart311464e2007-08-02 11:10:37 -0400491static void
James Smart2e0fef82007-06-17 19:56:36 -0500492lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500493{
494 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500495 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400496 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400497 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400498 int i;
dea31012005-04-17 16:05:31 -0500499
James Smart2e0fef82007-06-17 19:56:36 -0500500 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500501 ha_copy = phba->work_ha;
502 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500503 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500504
James Smartda0436e2009-05-22 14:51:39 -0400505 /* First, try to post the next mailbox command to SLI4 device */
506 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
507 lpfc_sli4_post_async_mbox(phba);
508
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500509 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400510 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500511 lpfc_handle_eratt(phba);
512
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500513 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500514 lpfc_sli_handle_mb_event(phba);
515
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500516 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500517 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400518
James Smartda0436e2009-05-22 14:51:39 -0400519 /* Process SLI4 events */
520 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
521 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
522 lpfc_sli4_fcp_xri_abort_event_proc(phba);
523 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
524 lpfc_sli4_els_xri_abort_event_proc(phba);
525 if (phba->hba_flag & ASYNC_EVENT)
526 lpfc_sli4_async_event_proc(phba);
527 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
528 spin_lock_irq(&phba->hbalock);
529 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
530 spin_unlock_irq(&phba->hbalock);
531 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
532 }
James Smartecfd03c2010-02-12 14:41:27 -0500533 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
534 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400535 }
536
James Smart549e55c2007-08-02 11:09:51 -0400537 vports = lpfc_create_vport_work_array(phba);
538 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400539 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400540 /*
541 * We could have no vports in array if unloading, so if
542 * this happens then just use the pport
543 */
544 if (vports[i] == NULL && i == 0)
545 vport = phba->pport;
546 else
547 vport = vports[i];
548 if (vport == NULL)
549 break;
James Smart58da1ff2008-04-07 10:15:56 -0400550 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400551 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400552 vport->work_port_events &= ~work_port_events;
553 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400554 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400555 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400556 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400557 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400558 if (work_port_events & WORKER_HB_TMO)
559 lpfc_hb_timeout_handler(phba);
560 if (work_port_events & WORKER_MBOX_TMO)
561 lpfc_mbox_timeout_handler(phba);
562 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
563 lpfc_unblock_fabric_iocbs(phba);
564 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400565 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400566 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
567 lpfc_ramp_down_queue_handler(phba);
568 if (work_port_events & WORKER_RAMP_UP_QUEUE)
569 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500570 }
James Smart09372822008-01-11 01:52:54 -0500571 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500572
James Smart858c9f62007-06-17 19:56:39 -0500573 pring = &phba->sli.ring[LPFC_ELS_RING];
574 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
575 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400576 if ((status & HA_RXMASK) ||
577 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400578 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400579 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500580 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400581 /* Set the lpfc data pending flag */
582 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500583 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400584 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500585 lpfc_sli_handle_slow_ring_event(phba, pring,
586 (status &
587 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500588 }
589 /*
590 * Turn on Ring interrupts
591 */
James Smart3772a992009-05-22 14:50:54 -0400592 if (phba->sli_rev <= LPFC_SLI_REV3) {
593 spin_lock_irq(&phba->hbalock);
594 control = readl(phba->HCregaddr);
595 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
596 lpfc_debugfs_slow_ring_trc(phba,
597 "WRK Enable ring: cntl:x%x hacopy:x%x",
598 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400599
James Smart3772a992009-05-22 14:50:54 -0400600 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
601 writel(control, phba->HCregaddr);
602 readl(phba->HCregaddr); /* flush */
603 } else {
604 lpfc_debugfs_slow_ring_trc(phba,
605 "WRK Ring ok: cntl:x%x hacopy:x%x",
606 control, ha_copy, 0);
607 }
608 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500609 }
610 }
James Smart2e0fef82007-06-17 19:56:36 -0500611 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500612}
613
dea31012005-04-17 16:05:31 -0500614int
615lpfc_do_work(void *p)
616{
617 struct lpfc_hba *phba = p;
618 int rc;
dea31012005-04-17 16:05:31 -0500619
620 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400621 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500622
James Smart3a55b532008-12-04 22:38:54 -0500623 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400624 /* wait and check worker queue activities */
625 rc = wait_event_interruptible(phba->work_waitq,
626 (test_and_clear_bit(LPFC_DATA_READY,
627 &phba->data_flags)
628 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500629 /* Signal wakeup shall terminate the worker thread */
630 if (rc) {
631 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
632 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500633 break;
James Smart3a55b532008-12-04 22:38:54 -0500634 }
dea31012005-04-17 16:05:31 -0500635
James Smart5e9d9b82008-06-14 22:52:53 -0400636 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500637 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500638 }
James Smart3a55b532008-12-04 22:38:54 -0500639 phba->worker_thread = NULL;
640 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
641 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500642 return 0;
643}
644
645/*
646 * This is only called to handle FC worker events. Since this a rare
647 * occurance, we allocate a struct lpfc_work_evt structure here instead of
648 * embedding it in the IOCB.
649 */
650int
James Smart2e0fef82007-06-17 19:56:36 -0500651lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500652 uint32_t evt)
653{
654 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500655 unsigned long flags;
dea31012005-04-17 16:05:31 -0500656
657 /*
658 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
659 * be queued to worker thread for processing
660 */
James Smart92d7f7b2007-06-17 19:56:38 -0500661 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500662 if (!evtp)
663 return 0;
664
665 evtp->evt_arg1 = arg1;
666 evtp->evt_arg2 = arg2;
667 evtp->evt = evt;
668
James Smarted957682007-06-17 19:56:37 -0500669 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400670 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500671 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500672
James Smart5e9d9b82008-06-14 22:52:53 -0400673 lpfc_worker_wake_up(phba);
674
dea31012005-04-17 16:05:31 -0500675 return 1;
676}
677
James Smart92d7f7b2007-06-17 19:56:38 -0500678void
679lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
680{
James Smart09372822008-01-11 01:52:54 -0500681 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500682 struct lpfc_hba *phba = vport->phba;
683 struct lpfc_nodelist *ndlp, *next_ndlp;
684 int rc;
685
686 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500687 if (!NLP_CHK_NODE_ACT(ndlp))
688 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500689 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
690 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400691 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
692 ((vport->port_type == LPFC_NPIV_PORT) &&
693 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500694 lpfc_unreg_rpi(vport, ndlp);
695
696 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400697 if ((phba->sli_rev < LPFC_SLI_REV4) &&
698 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500699 continue;
700 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
701 remove
702 ? NLP_EVT_DEVICE_RM
703 : NLP_EVT_DEVICE_RECOVERY);
704 }
705 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
706 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500707 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500708 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500709 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500710 }
711}
712
James Smart87af33f2007-10-27 13:37:43 -0400713void
James Smart98c9ea52007-10-27 13:37:33 -0400714lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500715{
James Smart695a8142010-01-26 23:08:03 -0500716 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
717
James Smart45ed1192009-10-02 15:17:02 -0400718 /* Cleanup any outstanding received buffers */
719 lpfc_cleanup_rcv_buffers(vport);
720
James Smart92d7f7b2007-06-17 19:56:38 -0500721 /* Cleanup any outstanding RSCN activity */
722 lpfc_els_flush_rscn(vport);
723
724 /* Cleanup any outstanding ELS commands */
725 lpfc_els_flush_cmd(vport);
726
727 lpfc_cleanup_rpis(vport, 0);
728
James Smart92d7f7b2007-06-17 19:56:38 -0500729 /* Turn off discovery timer if its running */
730 lpfc_can_disctmo(vport);
731}
732
James Smart3772a992009-05-22 14:50:54 -0400733void
James Smart98c9ea52007-10-27 13:37:33 -0400734lpfc_linkdown_port(struct lpfc_vport *vport)
735{
736 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
737
738 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
739
740 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
741 "Link Down: state:x%x rtry:x%x flg:x%x",
742 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
743
744 lpfc_port_link_failure(vport);
745
746}
747
dea31012005-04-17 16:05:31 -0500748int
James Smart685f0bf2007-04-25 09:53:08 -0400749lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500750{
James Smart2e0fef82007-06-17 19:56:36 -0500751 struct lpfc_vport *vport = phba->pport;
752 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400753 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400754 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400755 int i;
dea31012005-04-17 16:05:31 -0500756
James Smart3163f722008-02-08 18:50:25 -0500757 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500758 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500759
760 /* Block all SCSI stack I/Os */
761 lpfc_scsi_dev_block(phba);
762
James Smart2e0fef82007-06-17 19:56:36 -0500763 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500764 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500765 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500766 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500767 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500768 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500769 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500770 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500771 }
James Smart549e55c2007-08-02 11:09:51 -0400772 vports = lpfc_create_vport_work_array(phba);
773 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400774 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400775 /* Issue a LINK DOWN event to all nodes */
776 lpfc_linkdown_port(vports[i]);
777 }
James Smart09372822008-01-11 01:52:54 -0500778 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500779 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500780 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
781 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500782 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500783 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500784 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400785 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500786 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500787 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500788 }
789 }
790
dea31012005-04-17 16:05:31 -0500791 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500792 if (phba->pport->fc_flag & FC_PT2PT) {
793 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500794 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
795 if (mb) {
dea31012005-04-17 16:05:31 -0500796 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500797 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500798 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400799 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500800 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500801 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500802 }
803 }
James Smart2e0fef82007-06-17 19:56:36 -0500804 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500805 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500806 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500807 }
James Smart2e0fef82007-06-17 19:56:36 -0500808
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500809 return 0;
dea31012005-04-17 16:05:31 -0500810}
811
James Smart92d7f7b2007-06-17 19:56:38 -0500812static void
813lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500814{
James Smart92d7f7b2007-06-17 19:56:38 -0500815 struct lpfc_nodelist *ndlp;
816
817 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500818 if (!NLP_CHK_NODE_ACT(ndlp))
819 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500820 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
821 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500822 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500823 /* On Linkup its safe to clean up the ndlp
824 * from Fabric connections.
825 */
James Smart92d7f7b2007-06-17 19:56:38 -0500826 if (ndlp->nlp_DID != Fabric_DID)
827 lpfc_unreg_rpi(vport, ndlp);
828 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
829 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500830 /* Fail outstanding IO now since device is
831 * marked for PLOGI.
832 */
James Smart92d7f7b2007-06-17 19:56:38 -0500833 lpfc_unreg_rpi(vport, ndlp);
834 }
835 }
836}
837
838static void
839lpfc_linkup_port(struct lpfc_vport *vport)
840{
841 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500842 struct lpfc_hba *phba = vport->phba;
843
844 if ((vport->load_flag & FC_UNLOADING) != 0)
845 return;
846
James Smart858c9f62007-06-17 19:56:39 -0500847 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
848 "Link Up: top:x%x speed:x%x flg:x%x",
849 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
850
James Smart92d7f7b2007-06-17 19:56:38 -0500851 /* If NPIV is not enabled, only bring the physical port up */
852 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
853 (vport != phba->pport))
854 return;
dea31012005-04-17 16:05:31 -0500855
James Smart2e0fef82007-06-17 19:56:36 -0500856 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400857
James Smart2e0fef82007-06-17 19:56:36 -0500858 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500859 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
860 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
861 vport->fc_flag |= FC_NDISC_ACTIVE;
862 vport->fc_ns_retry = 0;
863 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500864
James Smart92d7f7b2007-06-17 19:56:38 -0500865 if (vport->fc_flag & FC_LBIT)
866 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500867
James Smart92d7f7b2007-06-17 19:56:38 -0500868}
869
870static int
871lpfc_linkup(struct lpfc_hba *phba)
872{
James Smart549e55c2007-08-02 11:09:51 -0400873 struct lpfc_vport **vports;
874 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500875
876 phba->link_state = LPFC_LINK_UP;
877
878 /* Unblock fabric iocbs if they are blocked */
879 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
880 del_timer_sync(&phba->fabric_block_timer);
881
James Smart549e55c2007-08-02 11:09:51 -0400882 vports = lpfc_create_vport_work_array(phba);
883 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400884 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400885 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500886 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400887 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
888 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500889 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500890
891 return 0;
892}
893
894/*
895 * This routine handles processing a CLEAR_LA mailbox
896 * command upon completion. It is setup in the LPFC_MBOXQ
897 * as the completion routine when the command is
898 * handed off to the SLI layer.
899 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100900static void
James Smart2e0fef82007-06-17 19:56:36 -0500901lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500902{
James Smart2e0fef82007-06-17 19:56:36 -0500903 struct lpfc_vport *vport = pmb->vport;
904 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
905 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400906 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500907 uint32_t control;
908
dea31012005-04-17 16:05:31 -0500909 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500910 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500911 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
912 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
913
914 /* Check for error */
915 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500916 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400917 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
918 "0320 CLEAR_LA mbxStatus error x%x hba "
919 "state x%x\n",
920 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500921 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500922 goto out;
923 }
924
James Smart92d7f7b2007-06-17 19:56:38 -0500925 if (vport->port_type == LPFC_PHYSICAL_PORT)
926 phba->link_state = LPFC_HBA_READY;
927
928 spin_lock_irq(&phba->hbalock);
929 psli->sli_flag |= LPFC_PROCESS_LA;
930 control = readl(phba->HCregaddr);
931 control |= HC_LAINT_ENA;
932 writel(control, phba->HCregaddr);
933 readl(phba->HCregaddr); /* flush */
934 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500935 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500936 return;
dea31012005-04-17 16:05:31 -0500937
dea31012005-04-17 16:05:31 -0500938out:
939 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400940 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
941 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500942 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500943
James Smart2e0fef82007-06-17 19:56:36 -0500944 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400945 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500946 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500947
James Smart2e0fef82007-06-17 19:56:36 -0500948 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500949
950 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500951
952 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500953 psli->sli_flag |= LPFC_PROCESS_LA;
954 control = readl(phba->HCregaddr);
955 control |= HC_LAINT_ENA;
956 writel(control, phba->HCregaddr);
957 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500958 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500959
960 return;
961}
962
James Smart2e0fef82007-06-17 19:56:36 -0500963
dea31012005-04-17 16:05:31 -0500964static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500965lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500966{
James Smart2e0fef82007-06-17 19:56:36 -0500967 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500968
James Smart04c68492009-05-22 14:52:52 -0400969 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500970 goto out;
dea31012005-04-17 16:05:31 -0500971
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500972 mempool_free(pmb, phba->mbox_mem_pool);
973
974 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500975 vport->fc_flag & FC_PUBLIC_LOOP &&
976 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500977 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500978 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500979 * LPFC_LOCAL_CFG_LINK while waiting for FAN
980 */
James Smart2e0fef82007-06-17 19:56:36 -0500981 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500982 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500983 }
dea31012005-04-17 16:05:31 -0500984
James Smart2e0fef82007-06-17 19:56:36 -0500985 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500986 * LPFC_FLOGI while waiting for FLOGI cmpl
987 */
James Smart92d7f7b2007-06-17 19:56:38 -0500988 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500989 lpfc_initial_flogi(vport);
990 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500991 return;
dea31012005-04-17 16:05:31 -0500992
993out:
James Smarte8b62012007-08-02 11:10:09 -0400994 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
995 "0306 CONFIG_LINK mbxStatus error x%x "
996 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400997 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500998 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500999
1000 lpfc_linkdown(phba);
1001
James Smarte8b62012007-08-02 11:10:09 -04001002 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1003 "0200 CONFIG_LINK bad hba state x%x\n",
1004 vport->port_state);
dea31012005-04-17 16:05:31 -05001005
James Smart92d7f7b2007-06-17 19:56:38 -05001006 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001007 return;
1008}
1009
1010static void
James Smart6fb120a2009-05-22 14:52:59 -04001011lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1012{
1013 struct lpfc_vport *vport = mboxq->vport;
1014 unsigned long flags;
1015
1016 if (mboxq->u.mb.mbxStatus) {
1017 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1018 "2017 REG_FCFI mbxStatus error x%x "
1019 "HBA state x%x\n",
1020 mboxq->u.mb.mbxStatus, vport->port_state);
1021 mempool_free(mboxq, phba->mbox_mem_pool);
1022 return;
1023 }
1024
1025 /* Start FCoE discovery by sending a FLOGI. */
1026 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1027 /* Set the FCFI registered flag */
1028 spin_lock_irqsave(&phba->hbalock, flags);
1029 phba->fcf.fcf_flag |= FCF_REGISTERED;
1030 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart32b97932009-07-19 10:01:21 -04001031 /* If there is a pending FCoE event, restart FCF table scan. */
1032 if (lpfc_check_pending_fcoe_event(phba, 1)) {
1033 mempool_free(mboxq, phba->mbox_mem_pool);
1034 return;
1035 }
James Smart1c6f4ef52009-11-18 15:40:49 -05001036 spin_lock_irqsave(&phba->hbalock, flags);
James Smartecfd03c2010-02-12 14:41:27 -05001037 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart4d9ab992009-10-02 15:16:39 -04001038 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001039 spin_unlock_irqrestore(&phba->hbalock, flags);
1040 if (vport->port_state != LPFC_FLOGI)
James Smart6fb120a2009-05-22 14:52:59 -04001041 lpfc_initial_flogi(vport);
James Smart6fb120a2009-05-22 14:52:59 -04001042
1043 mempool_free(mboxq, phba->mbox_mem_pool);
1044 return;
1045}
1046
1047/**
1048 * lpfc_fab_name_match - Check if the fcf fabric name match.
1049 * @fab_name: pointer to fabric name.
1050 * @new_fcf_record: pointer to fcf record.
1051 *
1052 * This routine compare the fcf record's fabric name with provided
1053 * fabric name. If the fabric name are identical this function
1054 * returns 1 else return 0.
1055 **/
1056static uint32_t
1057lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1058{
James Smartecfd03c2010-02-12 14:41:27 -05001059 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001060 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001061 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1062 return 0;
1063 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1064 return 0;
1065 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1066 return 0;
1067 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1068 return 0;
1069 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1070 return 0;
1071 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1072 return 0;
1073 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1074 return 0;
1075 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001076}
1077
1078/**
James Smart8fa38512009-07-19 10:01:03 -04001079 * lpfc_sw_name_match - Check if the fcf switch name match.
1080 * @fab_name: pointer to fabric name.
1081 * @new_fcf_record: pointer to fcf record.
1082 *
1083 * This routine compare the fcf record's switch name with provided
1084 * switch name. If the switch name are identical this function
1085 * returns 1 else return 0.
1086 **/
1087static uint32_t
1088lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1089{
James Smartecfd03c2010-02-12 14:41:27 -05001090 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001091 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001092 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1093 return 0;
1094 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1095 return 0;
1096 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1097 return 0;
1098 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1099 return 0;
1100 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1101 return 0;
1102 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1103 return 0;
1104 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1105 return 0;
1106 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001107}
1108
1109/**
James Smart6fb120a2009-05-22 14:52:59 -04001110 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001111 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001112 * @new_fcf_record: pointer to fcf record.
1113 *
1114 * This routine compare the fcf record's mac address with HBA's
1115 * FCF mac address. If the mac addresses are identical this function
1116 * returns 1 else return 0.
1117 **/
1118static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001119lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001120{
James Smartecfd03c2010-02-12 14:41:27 -05001121 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001122 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001123 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1124 return 0;
1125 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1126 return 0;
1127 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1128 return 0;
1129 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1130 return 0;
1131 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1132 return 0;
1133 return 1;
1134}
1135
1136static bool
1137lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1138{
1139 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001140}
1141
1142/**
1143 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001144 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001145 * @new_fcf_record: pointer to fcf record.
1146 *
1147 * This routine copies the FCF information from the FCF
1148 * record to lpfc_hba data structure.
1149 **/
1150static void
James Smartecfd03c2010-02-12 14:41:27 -05001151lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1152 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001153{
James Smartecfd03c2010-02-12 14:41:27 -05001154 /* Fabric name */
1155 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001156 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001157 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001158 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001159 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001160 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001161 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001162 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001163 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001164 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001165 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001166 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001167 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001168 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001169 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001170 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001171 /* Mac address */
1172 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1173 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1174 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1175 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1176 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1177 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1178 /* FCF record index */
1179 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1180 /* FCF record priority */
1181 fcf_rec->priority = new_fcf_record->fip_priority;
1182 /* Switch name */
1183 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001184 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001185 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001186 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001187 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001188 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001189 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001190 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001191 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001192 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001193 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001194 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001195 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001196 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001197 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001198 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001199}
1200
1201/**
James Smartecfd03c2010-02-12 14:41:27 -05001202 * lpfc_update_fcf_record - Update driver fcf record
1203 * @phba: pointer to lpfc hba data structure.
1204 * @fcf_rec: pointer to driver fcf record.
1205 * @new_fcf_record: pointer to hba fcf record.
1206 * @addr_mode: address mode to be set to the driver fcf record.
1207 * @vlan_id: vlan tag to be set to the driver fcf record.
1208 * @flag: flag bits to be set to the driver fcf record.
1209 *
1210 * This routine updates the driver FCF record from the new HBA FCF record
1211 * together with the address mode, vlan_id, and other informations. This
1212 * routine is called with the host lock held.
1213 **/
1214static void
1215__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1216 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1217 uint16_t vlan_id, uint32_t flag)
1218{
1219 /* Copy the fields from the HBA's FCF record */
1220 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1221 /* Update other fields of driver FCF record */
1222 fcf_rec->addr_mode = addr_mode;
1223 fcf_rec->vlan_id = vlan_id;
1224 fcf_rec->flag |= (flag | RECORD_VALID);
1225}
1226
1227/**
James Smart6fb120a2009-05-22 14:52:59 -04001228 * lpfc_register_fcf - Register the FCF with hba.
1229 * @phba: pointer to lpfc hba data structure.
1230 *
1231 * This routine issues a register fcfi mailbox command to register
1232 * the fcf with HBA.
1233 **/
1234static void
1235lpfc_register_fcf(struct lpfc_hba *phba)
1236{
1237 LPFC_MBOXQ_t *fcf_mbxq;
1238 int rc;
1239 unsigned long flags;
1240
1241 spin_lock_irqsave(&phba->hbalock, flags);
1242
1243 /* If the FCF is not availabe do nothing. */
1244 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smart4d9ab992009-10-02 15:16:39 -04001245 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001246 spin_unlock_irqrestore(&phba->hbalock, flags);
1247 return;
1248 }
1249
1250 /* The FCF is already registered, start discovery */
1251 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001252 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart32b97932009-07-19 10:01:21 -04001253 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001254 spin_unlock_irqrestore(&phba->hbalock, flags);
1255 if (phba->pport->port_state != LPFC_FLOGI)
1256 lpfc_initial_flogi(phba->pport);
1257 return;
1258 }
1259 spin_unlock_irqrestore(&phba->hbalock, flags);
1260
1261 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1262 GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001263 if (!fcf_mbxq) {
1264 spin_lock_irqsave(&phba->hbalock, flags);
1265 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1266 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001267 return;
James Smart4d9ab992009-10-02 15:16:39 -04001268 }
James Smart6fb120a2009-05-22 14:52:59 -04001269
1270 lpfc_reg_fcfi(phba, fcf_mbxq);
1271 fcf_mbxq->vport = phba->pport;
1272 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1273 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001274 if (rc == MBX_NOT_FINISHED) {
1275 spin_lock_irqsave(&phba->hbalock, flags);
1276 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1277 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001278 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001279 }
James Smart6fb120a2009-05-22 14:52:59 -04001280
1281 return;
1282}
1283
1284/**
1285 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1286 * @phba: pointer to lpfc hba data structure.
1287 * @new_fcf_record: pointer to fcf record.
1288 * @boot_flag: Indicates if this record used by boot bios.
1289 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001290 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001291 *
1292 * This routine compare the fcf record with connect list obtained from the
1293 * config region to decide if this FCF can be used for SAN discovery. It returns
1294 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1295 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1296 * is used by boot bios and addr_mode will indicate the addressing mode to be
1297 * used for this FCF when the function returns.
1298 * If the FCF record need to be used with a particular vlan id, the vlan is
1299 * set in the vlan_id on return of the function. If not VLAN tagging need to
1300 * be used with the FCF vlan_id will be set to 0xFFFF;
1301 **/
1302static int
1303lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1304 struct fcf_record *new_fcf_record,
1305 uint32_t *boot_flag, uint32_t *addr_mode,
1306 uint16_t *vlan_id)
1307{
1308 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001309 int i, j, fcf_vlan_id = 0;
1310
1311 /* Find the lowest VLAN id in the FCF record */
1312 for (i = 0; i < 512; i++) {
1313 if (new_fcf_record->vlan_bitmap[i]) {
1314 fcf_vlan_id = i * 8;
1315 j = 0;
1316 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1317 j++;
1318 fcf_vlan_id++;
1319 }
1320 break;
1321 }
1322 }
James Smart6fb120a2009-05-22 14:52:59 -04001323
James Smart0c287582009-06-10 17:22:56 -04001324 /* If FCF not available return 0 */
1325 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1326 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1327 return 0;
1328
James Smart45ed1192009-10-02 15:17:02 -04001329 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001330 *boot_flag = 0;
1331 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1332 new_fcf_record);
1333 if (phba->valid_vlan)
1334 *vlan_id = phba->vlan_id;
1335 else
1336 *vlan_id = 0xFFFF;
1337 return 1;
1338 }
1339
1340 /*
1341 * If there are no FCF connection table entry, driver connect to all
1342 * FCFs.
1343 */
1344 if (list_empty(&phba->fcf_conn_rec_list)) {
1345 *boot_flag = 0;
1346 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1347 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001348
1349 /*
1350 * When there are no FCF connect entries, use driver's default
1351 * addressing mode - FPMA.
1352 */
1353 if (*addr_mode & LPFC_FCF_FPMA)
1354 *addr_mode = LPFC_FCF_FPMA;
1355
James Smart4d9ab992009-10-02 15:16:39 -04001356 /* If FCF record report a vlan id use that vlan id */
1357 if (fcf_vlan_id)
1358 *vlan_id = fcf_vlan_id;
1359 else
1360 *vlan_id = 0xFFFF;
James Smart6fb120a2009-05-22 14:52:59 -04001361 return 1;
1362 }
1363
James Smartecfd03c2010-02-12 14:41:27 -05001364 list_for_each_entry(conn_entry,
1365 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001366 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1367 continue;
1368
1369 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1370 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001371 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001372 continue;
James Smart8fa38512009-07-19 10:01:03 -04001373 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1374 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1375 new_fcf_record))
1376 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001377 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1378 /*
1379 * If the vlan bit map does not have the bit set for the
1380 * vlan id to be used, then it is not a match.
1381 */
1382 if (!(new_fcf_record->vlan_bitmap
1383 [conn_entry->conn_rec.vlan_tag / 8] &
1384 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1385 continue;
1386 }
1387
1388 /*
James Smart0c287582009-06-10 17:22:56 -04001389 * If connection record does not support any addressing mode,
1390 * skip the FCF record.
1391 */
1392 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1393 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1394 continue;
1395
1396 /*
James Smart6fb120a2009-05-22 14:52:59 -04001397 * Check if the connection record specifies a required
1398 * addressing mode.
1399 */
1400 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1401 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1402
1403 /*
1404 * If SPMA required but FCF not support this continue.
1405 */
1406 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1407 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1408 new_fcf_record) & LPFC_FCF_SPMA))
1409 continue;
1410
1411 /*
1412 * If FPMA required but FCF not support this continue.
1413 */
1414 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1415 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1416 new_fcf_record) & LPFC_FCF_FPMA))
1417 continue;
1418 }
1419
1420 /*
1421 * This fcf record matches filtering criteria.
1422 */
1423 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1424 *boot_flag = 1;
1425 else
1426 *boot_flag = 0;
1427
James Smart0c287582009-06-10 17:22:56 -04001428 /*
1429 * If user did not specify any addressing mode, or if the
1430 * prefered addressing mode specified by user is not supported
1431 * by FCF, allow fabric to pick the addressing mode.
1432 */
James Smart6fb120a2009-05-22 14:52:59 -04001433 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1434 new_fcf_record);
1435 /*
1436 * If the user specified a required address mode, assign that
1437 * address mode
1438 */
1439 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1440 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1441 *addr_mode = (conn_entry->conn_rec.flags &
1442 FCFCNCT_AM_SPMA) ?
1443 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1444 /*
1445 * If the user specified a prefered address mode, use the
1446 * addr mode only if FCF support the addr_mode.
1447 */
1448 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1449 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1450 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1451 (*addr_mode & LPFC_FCF_SPMA))
1452 *addr_mode = LPFC_FCF_SPMA;
1453 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1454 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1455 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1456 (*addr_mode & LPFC_FCF_FPMA))
1457 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001458
James Smart4d9ab992009-10-02 15:16:39 -04001459 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001460 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1461 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001462 /*
1463 * If no vlan id is specified in connect list, use the vlan id
1464 * in the FCF record
1465 */
1466 else if (fcf_vlan_id)
1467 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001468 else
1469 *vlan_id = 0xFFFF;
1470
1471 return 1;
1472 }
1473
1474 return 0;
1475}
1476
1477/**
James Smart32b97932009-07-19 10:01:21 -04001478 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1479 * @phba: pointer to lpfc hba data structure.
1480 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1481 *
1482 * This function check if there is any fcoe event pending while driver
1483 * scan FCF entries. If there is any pending event, it will restart the
1484 * FCF saning and return 1 else return 0.
1485 */
1486int
1487lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1488{
James Smart32b97932009-07-19 10:01:21 -04001489 /*
1490 * If the Link is up and no FCoE events while in the
1491 * FCF discovery, no need to restart FCF discovery.
1492 */
1493 if ((phba->link_state >= LPFC_LINK_UP) &&
1494 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1495 return 0;
1496
James Smart0c9ab6f2010-02-26 14:15:57 -05001497 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1498 "2768 Pending link or FCF event during current "
1499 "handling of the previous event: link_state:x%x, "
1500 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1501 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1502 phba->fcoe_eventtag);
1503
James Smart32b97932009-07-19 10:01:21 -04001504 spin_lock_irq(&phba->hbalock);
1505 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1506 spin_unlock_irq(&phba->hbalock);
1507
James Smart0c9ab6f2010-02-26 14:15:57 -05001508 if (phba->link_state >= LPFC_LINK_UP) {
1509 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1510 "2780 Restart FCF table scan due to "
1511 "pending FCF event:evt_tag_at_scan:x%x, "
1512 "evt_tag_current:x%x\n",
1513 phba->fcoe_eventtag_at_fcf_scan,
1514 phba->fcoe_eventtag);
1515 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1516 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001517 /*
1518 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1519 * flag
1520 */
James Smart1c6f4ef52009-11-18 15:40:49 -05001521 spin_lock_irq(&phba->hbalock);
James Smart4d9ab992009-10-02 15:16:39 -04001522 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001523 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001524 spin_unlock_irq(&phba->hbalock);
1525 }
James Smart32b97932009-07-19 10:01:21 -04001526
James Smart0c9ab6f2010-02-26 14:15:57 -05001527 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001528 if (unreg_fcf) {
1529 spin_lock_irq(&phba->hbalock);
1530 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1531 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001532 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001533 }
James Smart32b97932009-07-19 10:01:21 -04001534 return 1;
1535}
1536
1537/**
James Smart999d8132010-03-15 11:24:56 -04001538 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1539 * @phba: pointer to lpfc hba data structure.
1540 * @fcf_cnt: number of eligible fcf record seen so far.
1541 *
1542 * This function makes an running random selection decision on FCF record to
1543 * use through a sequence of @fcf_cnt eligible FCF records with equal
1544 * probability. To perform integer manunipulation of random numbers with
1545 * size unit32_t, the lower 16 bits of the 32-bit random number returned
1546 * from random32() are taken as the random random number generated.
1547 *
1548 * Returns true when outcome is for the newly read FCF record should be
1549 * chosen; otherwise, return false when outcome is for keeping the previously
1550 * chosen FCF record.
1551 **/
1552static bool
1553lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1554{
1555 uint32_t rand_num;
1556
1557 /* Get 16-bit uniform random number */
1558 rand_num = (0xFFFF & random32());
1559
1560 /* Decision with probability 1/fcf_cnt */
1561 if ((fcf_cnt * rand_num) < 0xFFFF)
1562 return true;
1563 else
1564 return false;
1565}
1566
1567/**
1568 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
James Smart6fb120a2009-05-22 14:52:59 -04001569 * @phba: pointer to lpfc hba data structure.
1570 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001571 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001572 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001573 * This routine parses the non-embedded fcf mailbox command by performing the
1574 * necessarily error checking, non-embedded read FCF record mailbox command
1575 * SGE parsing, and endianness swapping.
1576 *
1577 * Returns the pointer to the new FCF record in the non-embedded mailbox
1578 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001579 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001580static struct fcf_record *
1581lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1582 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001583{
1584 void *virt_addr;
1585 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001586 struct lpfc_mbx_sge sge;
1587 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1588 uint32_t shdr_status, shdr_add_status;
1589 union lpfc_sli4_cfg_shdr *shdr;
1590 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001591
James Smart6fb120a2009-05-22 14:52:59 -04001592 /* Get the first SGE entry from the non-embedded DMA memory. This
1593 * routine only uses a single SGE.
1594 */
1595 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1596 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1597 if (unlikely(!mboxq->sge_array)) {
1598 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1599 "2524 Failed to get the non-embedded SGE "
1600 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001601 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001602 }
1603 virt_addr = mboxq->sge_array->addr[0];
1604
1605 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1606 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001607 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001608 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001609 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1610 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001611 "2726 READ_FCF_RECORD Indicates empty "
1612 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001613 else
1614 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001615 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001616 "with status x%x add_status x%x, "
1617 "mbx\n", shdr_status, shdr_add_status);
1618 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001619 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001620
1621 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001622 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1623 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1624 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001625 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001626 new_fcf_record = (struct fcf_record *)(virt_addr +
1627 sizeof(struct lpfc_mbx_read_fcf_tbl));
1628 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001629 offsetof(struct fcf_record, vlan_bitmap));
1630 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1631 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001632
James Smart0c9ab6f2010-02-26 14:15:57 -05001633 return new_fcf_record;
1634}
1635
1636/**
1637 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1638 * @phba: pointer to lpfc hba data structure.
1639 * @fcf_record: pointer to the fcf record.
1640 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1641 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1642 *
1643 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1644 * enabled.
1645 **/
1646static void
1647lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1648 struct fcf_record *fcf_record,
1649 uint16_t vlan_id,
1650 uint16_t next_fcf_index)
1651{
1652 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1653 "2764 READ_FCF_RECORD:\n"
1654 "\tFCF_Index : x%x\n"
1655 "\tFCF_Avail : x%x\n"
1656 "\tFCF_Valid : x%x\n"
1657 "\tFIP_Priority : x%x\n"
1658 "\tMAC_Provider : x%x\n"
1659 "\tLowest VLANID : x%x\n"
1660 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1661 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1662 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1663 "\tNext_FCF_Index: x%x\n",
1664 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1665 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1666 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1667 fcf_record->fip_priority,
1668 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1669 vlan_id,
1670 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1671 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1672 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1673 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1674 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1675 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1676 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1677 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1678 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1679 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1680 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1681 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1682 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1683 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1684 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1685 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1686 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1687 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1688 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1689 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1690 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1691 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1692 next_fcf_index);
1693}
1694
1695/**
1696 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
1697 * @phba: pointer to lpfc hba data structure.
1698 * @mboxq: pointer to mailbox object.
1699 *
1700 * This function iterates through all the fcf records available in
1701 * HBA and chooses the optimal FCF record for discovery. After finding
1702 * the FCF for discovery it registers the FCF record and kicks start
1703 * discovery.
1704 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
1705 * use an FCF record which matches fabric name and mac address of the
1706 * currently used FCF record.
1707 * If the driver supports only one FCF, it will try to use the FCF record
1708 * used by BOOT_BIOS.
1709 */
1710void
1711lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1712{
1713 struct fcf_record *new_fcf_record;
1714 uint32_t boot_flag, addr_mode;
1715 uint16_t fcf_index, next_fcf_index;
1716 struct lpfc_fcf_rec *fcf_rec = NULL;
1717 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04001718 uint32_t seed;
1719 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001720 int rc;
1721
1722 /* If there is pending FCoE event restart FCF table scan */
1723 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1724 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1725 return;
1726 }
1727
1728 /* Parse the FCF record from the non-embedded mailbox command */
1729 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
1730 &next_fcf_index);
1731 if (!new_fcf_record) {
1732 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1733 "2765 Mailbox command READ_FCF_RECORD "
1734 "failed to retrieve a FCF record.\n");
1735 /* Let next new FCF event trigger fast failover */
1736 spin_lock_irq(&phba->hbalock);
1737 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1738 spin_unlock_irq(&phba->hbalock);
1739 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1740 return;
1741 }
1742
1743 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05001744 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1745 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05001746
1747 /* Log the FCF record information if turned on */
1748 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
1749 next_fcf_index);
1750
James Smart6fb120a2009-05-22 14:52:59 -04001751 /*
1752 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05001753 * read the next entry; otherwise, this is an eligible FCF
1754 * record for round robin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04001755 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001756 if (!rc) {
1757 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1758 "2781 FCF record fcf_index:x%x failed FCF "
1759 "connection list check, fcf_avail:x%x, "
1760 "fcf_valid:x%x\n",
1761 bf_get(lpfc_fcf_record_fcf_index,
1762 new_fcf_record),
1763 bf_get(lpfc_fcf_record_fcf_avail,
1764 new_fcf_record),
1765 bf_get(lpfc_fcf_record_fcf_valid,
1766 new_fcf_record));
James Smart6fb120a2009-05-22 14:52:59 -04001767 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001768 } else {
1769 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1770 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
1771 if (rc)
1772 goto read_next_fcf;
1773 }
1774
James Smart6fb120a2009-05-22 14:52:59 -04001775 /*
1776 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05001777 * FCF record for the discovery. The condition that a rescan
1778 * matches the in-use FCF record: fabric name, switch name, mac
1779 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04001780 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001781 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001782 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartecfd03c2010-02-12 14:41:27 -05001783 if (lpfc_fab_name_match(phba->fcf.current_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001784 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001785 lpfc_sw_name_match(phba->fcf.current_rec.switch_name,
James Smart8fa38512009-07-19 10:01:03 -04001786 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001787 lpfc_mac_addr_match(phba->fcf.current_rec.mac_addr,
1788 new_fcf_record) &&
1789 lpfc_vlan_id_match(phba->fcf.current_rec.vlan_id,
1790 vlan_id)) {
James Smart6fb120a2009-05-22 14:52:59 -04001791 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smartecfd03c2010-02-12 14:41:27 -05001792 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
1793 /* Stop FCF redisc wait timer if pending */
1794 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
1795 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1796 /* If in fast failover, mark it's completed */
James Smartfc2b9892010-02-26 14:15:29 -05001797 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV |
James Smart0c9ab6f2010-02-26 14:15:57 -05001798 FCF_DISCOVERY);
1799 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001800 goto out;
1801 }
James Smartecfd03c2010-02-12 14:41:27 -05001802 /*
1803 * Read next FCF record from HBA searching for the matching
1804 * with in-use record only if not during the fast failover
1805 * period. In case of fast failover period, it shall try to
1806 * determine whether the FCF record just read should be the
1807 * next candidate.
1808 */
1809 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001810 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001811 goto read_next_fcf;
1812 }
James Smart6fb120a2009-05-22 14:52:59 -04001813 }
James Smartecfd03c2010-02-12 14:41:27 -05001814 /*
1815 * Update on failover FCF record only if it's in FCF fast-failover
1816 * period; otherwise, update on current FCF record.
1817 */
James Smartfc2b9892010-02-26 14:15:29 -05001818 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1819 fcf_rec = &phba->fcf.failover_rec;
1820 else
James Smartecfd03c2010-02-12 14:41:27 -05001821 fcf_rec = &phba->fcf.current_rec;
1822
James Smart6fb120a2009-05-22 14:52:59 -04001823 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1824 /*
James Smartecfd03c2010-02-12 14:41:27 -05001825 * If the driver FCF record does not have boot flag
1826 * set and new hba fcf record has boot flag set, use
1827 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001828 */
James Smartecfd03c2010-02-12 14:41:27 -05001829 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
1830 /* Choose this FCF record */
1831 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1832 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05001833 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001834 goto read_next_fcf;
1835 }
1836 /*
James Smartecfd03c2010-02-12 14:41:27 -05001837 * If the driver FCF record has boot flag set and the
1838 * new hba FCF record does not have boot flag, read
1839 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04001840 */
James Smartecfd03c2010-02-12 14:41:27 -05001841 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001842 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001843 goto read_next_fcf;
1844 }
1845 /*
James Smartecfd03c2010-02-12 14:41:27 -05001846 * If the new hba FCF record has lower priority value
1847 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04001848 */
James Smartfc2b9892010-02-26 14:15:29 -05001849 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04001850 /* Choose the new FCF record with lower priority */
James Smartecfd03c2010-02-12 14:41:27 -05001851 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1852 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04001853 /* Reset running random FCF selection count */
1854 phba->fcf.eligible_fcf_cnt = 1;
1855 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
1856 /* Update running random FCF selection count */
1857 phba->fcf.eligible_fcf_cnt++;
1858 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
1859 phba->fcf.eligible_fcf_cnt);
1860 if (select_new_fcf)
1861 /* Choose the new FCF by random selection */
1862 __lpfc_update_fcf_record(phba, fcf_rec,
1863 new_fcf_record,
1864 addr_mode, vlan_id, 0);
James Smart6fb120a2009-05-22 14:52:59 -04001865 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001866 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001867 goto read_next_fcf;
1868 }
1869 /*
James Smartecfd03c2010-02-12 14:41:27 -05001870 * This is the first suitable FCF record, choose this record for
1871 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001872 */
James Smartecfd03c2010-02-12 14:41:27 -05001873 if (fcf_rec) {
1874 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1875 addr_mode, vlan_id, (boot_flag ?
1876 BOOT_ENABLE : 0));
1877 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04001878 /* Setup initial running random FCF selection count */
1879 phba->fcf.eligible_fcf_cnt = 1;
1880 /* Seeding the random number generator for random selection */
1881 seed = (uint32_t)(0xFFFFFFFF & jiffies);
1882 srandom32(seed);
James Smart6fb120a2009-05-22 14:52:59 -04001883 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001884 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001885 goto read_next_fcf;
1886
1887read_next_fcf:
1888 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05001889 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
1890 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
1891 /*
1892 * Case of FCF fast failover scan
1893 */
1894
1895 /*
1896 * It has not found any suitable FCF record, cancel
1897 * FCF scan inprogress, and do nothing
1898 */
1899 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001900 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1901 "2782 No suitable FCF record "
1902 "found during this round of "
1903 "post FCF rediscovery scan: "
1904 "fcf_evt_tag:x%x, fcf_index: "
1905 "x%x\n",
1906 phba->fcoe_eventtag_at_fcf_scan,
1907 bf_get(lpfc_fcf_record_fcf_index,
1908 new_fcf_record));
1909 /*
1910 * Let next new FCF event trigger fast
1911 * failover
1912 */
1913 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001914 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001915 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001916 return;
1917 }
1918 /*
1919 * It has found a suitable FCF record that is not
1920 * the same as in-use FCF record, unregister the
1921 * in-use FCF record, replace the in-use FCF record
1922 * with the new FCF record, mark FCF fast failover
1923 * completed, and then start register the new FCF
1924 * record.
1925 */
1926
James Smart0c9ab6f2010-02-26 14:15:57 -05001927 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001928 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05001929
1930 /* Replace in-use record with the new record */
James Smartecfd03c2010-02-12 14:41:27 -05001931 memcpy(&phba->fcf.current_rec,
1932 &phba->fcf.failover_rec,
1933 sizeof(struct lpfc_fcf_rec));
1934 /* mark the FCF fast failover completed */
James Smart0c9ab6f2010-02-26 14:15:57 -05001935 spin_lock_irq(&phba->hbalock);
1936 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
1937 spin_unlock_irq(&phba->hbalock);
1938 /*
1939 * Set up the initial registered FCF index for FLOGI
1940 * round robin FCF failover.
1941 */
1942 phba->fcf.fcf_rr_init_indx =
1943 phba->fcf.failover_rec.fcf_indx;
James Smartecfd03c2010-02-12 14:41:27 -05001944 /* Register to the new FCF record */
1945 lpfc_register_fcf(phba);
1946 } else {
1947 /*
1948 * In case of transaction period to fast FCF failover,
1949 * do nothing when search to the end of the FCF table.
1950 */
1951 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
1952 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
1953 return;
1954 /*
1955 * Otherwise, initial scan or post linkdown rescan,
James Smart0c9ab6f2010-02-26 14:15:57 -05001956 * register with the best FCF record found so far
1957 * through the FCF scanning process.
James Smartecfd03c2010-02-12 14:41:27 -05001958 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001959
1960 /* mark the initial FCF discovery completed */
1961 spin_lock_irq(&phba->hbalock);
1962 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
1963 spin_unlock_irq(&phba->hbalock);
1964 /*
1965 * Set up the initial registered FCF index for FLOGI
1966 * round robin FCF failover
1967 */
1968 phba->fcf.fcf_rr_init_indx =
1969 phba->fcf.current_rec.fcf_indx;
1970 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001971 lpfc_register_fcf(phba);
1972 }
1973 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05001974 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04001975 return;
1976
1977out:
1978 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1979 lpfc_register_fcf(phba);
1980
1981 return;
1982}
1983
1984/**
James Smart0c9ab6f2010-02-26 14:15:57 -05001985 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf round robin read_fcf mbox cmpl hdler
1986 * @phba: pointer to lpfc hba data structure.
1987 * @mboxq: pointer to mailbox object.
1988 *
1989 * This is the callback function for FLOGI failure round robin FCF failover
1990 * read FCF record mailbox command from the eligible FCF record bmask for
1991 * performing the failover. If the FCF read back is not valid/available, it
1992 * fails through to retrying FLOGI to the currently registered FCF again.
1993 * Otherwise, if the FCF read back is valid and available, it will set the
1994 * newly read FCF record to the failover FCF record, unregister currently
1995 * registered FCF record, copy the failover FCF record to the current
1996 * FCF record, and then register the current FCF record before proceeding
1997 * to trying FLOGI on the new failover FCF.
1998 */
1999void
2000lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2001{
2002 struct fcf_record *new_fcf_record;
2003 uint32_t boot_flag, addr_mode;
2004 uint16_t next_fcf_index;
2005 uint16_t current_fcf_index;
2006 uint16_t vlan_id;
2007
2008 /* If link state is not up, stop the round robin failover process */
2009 if (phba->link_state < LPFC_LINK_UP) {
2010 spin_lock_irq(&phba->hbalock);
2011 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
2012 spin_unlock_irq(&phba->hbalock);
2013 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2014 return;
2015 }
2016
2017 /* Parse the FCF record from the non-embedded mailbox command */
2018 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2019 &next_fcf_index);
2020 if (!new_fcf_record) {
2021 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2022 "2766 Mailbox command READ_FCF_RECORD "
2023 "failed to retrieve a FCF record.\n");
2024 goto out;
2025 }
2026
2027 /* Get the needed parameters from FCF record */
2028 lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2029 &addr_mode, &vlan_id);
2030
2031 /* Log the FCF record information if turned on */
2032 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2033 next_fcf_index);
2034
2035 /* Upload new FCF record to the failover FCF record */
2036 spin_lock_irq(&phba->hbalock);
2037 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2038 new_fcf_record, addr_mode, vlan_id,
2039 (boot_flag ? BOOT_ENABLE : 0));
2040 spin_unlock_irq(&phba->hbalock);
2041
2042 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2043
2044 /* Unregister the current in-use FCF record */
2045 lpfc_unregister_fcf(phba);
2046
2047 /* Replace in-use record with the new record */
2048 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2049 sizeof(struct lpfc_fcf_rec));
2050
2051 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2052 "2783 FLOGI round robin FCF failover from FCF "
2053 "(index:x%x) to FCF (index:x%x).\n",
2054 current_fcf_index,
2055 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
2056
2057out:
2058 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2059 lpfc_register_fcf(phba);
2060}
2061
2062/**
2063 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2064 * @phba: pointer to lpfc hba data structure.
2065 * @mboxq: pointer to mailbox object.
2066 *
2067 * This is the callback function of read FCF record mailbox command for
2068 * updating the eligible FCF bmask for FLOGI failure round robin FCF
2069 * failover when a new FCF event happened. If the FCF read back is
2070 * valid/available and it passes the connection list check, it updates
2071 * the bmask for the eligible FCF record for round robin failover.
2072 */
2073void
2074lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2075{
2076 struct fcf_record *new_fcf_record;
2077 uint32_t boot_flag, addr_mode;
2078 uint16_t fcf_index, next_fcf_index;
2079 uint16_t vlan_id;
2080 int rc;
2081
2082 /* If link state is not up, no need to proceed */
2083 if (phba->link_state < LPFC_LINK_UP)
2084 goto out;
2085
2086 /* If FCF discovery period is over, no need to proceed */
2087 if (phba->fcf.fcf_flag & FCF_DISCOVERY)
2088 goto out;
2089
2090 /* Parse the FCF record from the non-embedded mailbox command */
2091 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2092 &next_fcf_index);
2093 if (!new_fcf_record) {
2094 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2095 "2767 Mailbox command READ_FCF_RECORD "
2096 "failed to retrieve a FCF record.\n");
2097 goto out;
2098 }
2099
2100 /* Check the connection list for eligibility */
2101 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2102 &addr_mode, &vlan_id);
2103
2104 /* Log the FCF record information if turned on */
2105 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2106 next_fcf_index);
2107
2108 if (!rc)
2109 goto out;
2110
2111 /* Update the eligible FCF record index bmask */
2112 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2113 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
2114
2115out:
2116 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2117}
2118
2119/**
James Smart1c6834a2009-07-19 10:01:26 -04002120 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2121 * @phba: pointer to lpfc hba data structure.
2122 * @mboxq: pointer to mailbox data structure.
2123 *
2124 * This function handles completion of init vpi mailbox command.
2125 */
James Smart695a8142010-01-26 23:08:03 -05002126void
James Smart1c6834a2009-07-19 10:01:26 -04002127lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2128{
2129 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002130 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002131 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2132
James Smart1c6834a2009-07-19 10:01:26 -04002133 if (mboxq->u.mb.mbxStatus) {
2134 lpfc_printf_vlog(vport, KERN_ERR,
2135 LOG_MBOX,
2136 "2609 Init VPI mailbox failed 0x%x\n",
2137 mboxq->u.mb.mbxStatus);
2138 mempool_free(mboxq, phba->mbox_mem_pool);
2139 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2140 return;
2141 }
James Smart72100cc2010-02-12 14:43:01 -05002142 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002143 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002144 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002145
James Smart695a8142010-01-26 23:08:03 -05002146 /* If this port is physical port or FDISC is done, do reg_vpi */
2147 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2148 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2149 if (!ndlp)
2150 lpfc_printf_vlog(vport, KERN_ERR,
2151 LOG_DISCOVERY,
2152 "2731 Cannot find fabric "
2153 "controller node\n");
2154 else
2155 lpfc_register_new_vport(phba, vport, ndlp);
2156 mempool_free(mboxq, phba->mbox_mem_pool);
2157 return;
2158 }
2159
James Smart1c6834a2009-07-19 10:01:26 -04002160 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2161 lpfc_initial_fdisc(vport);
2162 else {
2163 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002164 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2165 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002166 }
James Smart695a8142010-01-26 23:08:03 -05002167 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002168 return;
2169}
2170
2171/**
James Smartecfd03c2010-02-12 14:41:27 -05002172 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2173 * @vport: pointer to lpfc_vport data structure.
2174 *
2175 * This function issue a init_vpi mailbox command to initialize
2176 * VPI for the vport.
2177 */
2178void
2179lpfc_issue_init_vpi(struct lpfc_vport *vport)
2180{
2181 LPFC_MBOXQ_t *mboxq;
2182 int rc;
2183
2184 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2185 if (!mboxq) {
2186 lpfc_printf_vlog(vport, KERN_ERR,
2187 LOG_MBOX, "2607 Failed to allocate "
2188 "init_vpi mailbox\n");
2189 return;
2190 }
2191 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2192 mboxq->vport = vport;
2193 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2194 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2195 if (rc == MBX_NOT_FINISHED) {
2196 lpfc_printf_vlog(vport, KERN_ERR,
2197 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2198 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2199 }
2200}
2201
2202/**
James Smart6fb120a2009-05-22 14:52:59 -04002203 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2204 * @phba: pointer to lpfc hba data structure.
2205 *
2206 * This function loops through the list of vports on the @phba and issues an
2207 * FDISC if possible.
2208 */
2209void
2210lpfc_start_fdiscs(struct lpfc_hba *phba)
2211{
2212 struct lpfc_vport **vports;
2213 int i;
2214
2215 vports = lpfc_create_vport_work_array(phba);
2216 if (vports != NULL) {
2217 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2218 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2219 continue;
2220 /* There are no vpi for this vport */
2221 if (vports[i]->vpi > phba->max_vpi) {
2222 lpfc_vport_set_state(vports[i],
2223 FC_VPORT_FAILED);
2224 continue;
2225 }
2226 if (phba->fc_topology == TOPOLOGY_LOOP) {
2227 lpfc_vport_set_state(vports[i],
2228 FC_VPORT_LINKDOWN);
2229 continue;
2230 }
James Smart1c6834a2009-07-19 10:01:26 -04002231 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002232 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002233 continue;
2234 }
James Smart6fb120a2009-05-22 14:52:59 -04002235 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2236 lpfc_initial_fdisc(vports[i]);
2237 else {
2238 lpfc_vport_set_state(vports[i],
2239 FC_VPORT_NO_FABRIC_SUPP);
2240 lpfc_printf_vlog(vports[i], KERN_ERR,
2241 LOG_ELS,
2242 "0259 No NPIV "
2243 "Fabric support\n");
2244 }
2245 }
2246 }
2247 lpfc_destroy_vport_work_array(phba, vports);
2248}
2249
2250void
2251lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2252{
2253 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2254 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002255 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002256
2257 if (mboxq->u.mb.mbxStatus) {
2258 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2259 "2018 REG_VFI mbxStatus error x%x "
2260 "HBA state x%x\n",
2261 mboxq->u.mb.mbxStatus, vport->port_state);
2262 if (phba->fc_topology == TOPOLOGY_LOOP) {
2263 /* FLOGI failed, use loop map to make discovery list */
2264 lpfc_disc_list_loopmap(vport);
2265 /* Start discovery */
2266 lpfc_disc_start(vport);
2267 goto fail_free_mem;
2268 }
2269 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2270 goto fail_free_mem;
2271 }
James Smartc8685952009-11-18 15:39:16 -05002272 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002273 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002274 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002275 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002276 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002277 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002278
2279 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2280 lpfc_start_fdiscs(phba);
2281 lpfc_do_scr_ns_plogi(phba, vport);
2282 }
2283
2284fail_free_mem:
2285 mempool_free(mboxq, phba->mbox_mem_pool);
2286 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2287 kfree(dmabuf);
2288 return;
2289}
2290
2291static void
James Smart2e0fef82007-06-17 19:56:36 -05002292lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002293{
James Smart6fb120a2009-05-22 14:52:59 -04002294 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002295 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002296 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002297
2298
2299 /* Check for error */
2300 if (mb->mbxStatus) {
2301 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002302 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2303 "0319 READ_SPARAM mbxStatus error x%x "
2304 "hba state x%x>\n",
2305 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002306 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002307 goto out;
2308 }
2309
James Smart2e0fef82007-06-17 19:56:36 -05002310 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002311 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05002312 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05002313 u64_to_wwn(phba->cfg_soft_wwnn,
2314 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04002315 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05002316 u64_to_wwn(phba->cfg_soft_wwpn,
2317 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05002318 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
2319 sizeof(vport->fc_nodename));
2320 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
2321 sizeof(vport->fc_portname));
2322 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2323 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2324 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2325 }
2326
dea31012005-04-17 16:05:31 -05002327 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2328 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002329 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002330 return;
2331
2332out:
2333 pmb->context1 = NULL;
2334 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2335 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002336 lpfc_issue_clear_la(phba, vport);
2337 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002338 return;
2339}
2340
2341static void
James Smart92d7f7b2007-06-17 19:56:38 -05002342lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05002343{
James Smart92d7f7b2007-06-17 19:56:38 -05002344 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002345 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002346 int i;
James Smart14691152006-12-02 13:34:28 -05002347 struct lpfc_dmabuf *mp;
2348 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002349 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002350
James Smart92d7f7b2007-06-17 19:56:38 -05002351 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002352 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05002353 case LA_1GHZ_LINK:
2354 phba->fc_linkspeed = LA_1GHZ_LINK;
2355 break;
2356 case LA_2GHZ_LINK:
2357 phba->fc_linkspeed = LA_2GHZ_LINK;
2358 break;
2359 case LA_4GHZ_LINK:
2360 phba->fc_linkspeed = LA_4GHZ_LINK;
2361 break;
2362 case LA_8GHZ_LINK:
2363 phba->fc_linkspeed = LA_8GHZ_LINK;
2364 break;
James Smartf4b4c682009-05-22 14:53:12 -04002365 case LA_10GHZ_LINK:
2366 phba->fc_linkspeed = LA_10GHZ_LINK;
2367 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002368 default:
2369 phba->fc_linkspeed = LA_UNKNW_LINK;
2370 break;
dea31012005-04-17 16:05:31 -05002371 }
2372
2373 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05002374 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05002375
2376 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05002377 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05002378
James Smart6a9c52c2009-10-02 15:16:51 -04002379 /* if npiv is enabled and this adapter supports npiv log
2380 * a message that npiv is not supported in this topology
2381 */
2382 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04002383 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2384 "1309 Link Up Event npiv not supported in loop "
2385 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002386 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05002387 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05002388 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002389
James Smart2e0fef82007-06-17 19:56:36 -05002390 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05002391 i = la->un.lilpBde64.tus.f.bdeSize;
2392
2393 if (i == 0) {
2394 phba->alpa_map[0] = 0;
2395 } else {
James Smarte8b62012007-08-02 11:10:09 -04002396 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05002397 int numalpa, j, k;
2398 union {
2399 uint8_t pamap[16];
2400 struct {
2401 uint32_t wd1;
2402 uint32_t wd2;
2403 uint32_t wd3;
2404 uint32_t wd4;
2405 } pa;
2406 } un;
2407 numalpa = phba->alpa_map[0];
2408 j = 0;
2409 while (j < numalpa) {
2410 memset(un.pamap, 0, 16);
2411 for (k = 1; j < numalpa; k++) {
2412 un.pamap[k - 1] =
2413 phba->alpa_map[j + 1];
2414 j++;
2415 if (k == 16)
2416 break;
2417 }
2418 /* Link Up Event ALPA map */
2419 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05002420 KERN_WARNING,
2421 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002422 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05002423 "ALPA map Data: x%x "
2424 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002425 un.pa.wd1, un.pa.wd2,
2426 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05002427 }
2428 }
2429 }
2430 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05002431 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04002432 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05002433 (phba->sli_rev == 3))
2434 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
2435 }
James Smart2e0fef82007-06-17 19:56:36 -05002436 vport->fc_myDID = phba->fc_pref_DID;
2437 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002438 }
James Smart92d7f7b2007-06-17 19:56:38 -05002439 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002440
2441 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05002442 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2443 if (!sparam_mbox)
2444 goto out;
2445
2446 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
2447 if (rc) {
2448 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2449 goto out;
2450 }
2451 sparam_mbox->vport = vport;
2452 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
2453 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
2454 if (rc == MBX_NOT_FINISHED) {
2455 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2456 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2457 kfree(mp);
2458 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2459 goto out;
dea31012005-04-17 16:05:31 -05002460 }
2461
James Smart6fb120a2009-05-22 14:52:59 -04002462 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2463 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2464 if (!cfglink_mbox)
2465 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05002466 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002467 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002468 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002469 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002470 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002471 if (rc == MBX_NOT_FINISHED) {
2472 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2473 goto out;
2474 }
2475 } else {
James Smart32b97932009-07-19 10:01:21 -04002476 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002477 /*
2478 * Add the driver's default FCF record at FCF index 0 now. This
2479 * is phase 1 implementation that support FCF index 0 and driver
2480 * defaults.
2481 */
James Smart45ed1192009-10-02 15:17:02 -04002482 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002483 fcf_record = kzalloc(sizeof(struct fcf_record),
2484 GFP_KERNEL);
2485 if (unlikely(!fcf_record)) {
2486 lpfc_printf_log(phba, KERN_ERR,
2487 LOG_MBOX | LOG_SLI,
2488 "2554 Could not allocate memmory for "
2489 "fcf record\n");
2490 rc = -ENODEV;
2491 goto out;
2492 }
2493
2494 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2495 LPFC_FCOE_FCF_DEF_INDEX);
2496 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2497 if (unlikely(rc)) {
2498 lpfc_printf_log(phba, KERN_ERR,
2499 LOG_MBOX | LOG_SLI,
2500 "2013 Could not manually add FCF "
2501 "record 0, status %d\n", rc);
2502 rc = -ENODEV;
2503 kfree(fcf_record);
2504 goto out;
2505 }
2506 kfree(fcf_record);
2507 }
2508 /*
2509 * The driver is expected to do FIP/FCF. Call the port
2510 * and get the FCF Table.
2511 */
James Smart32b97932009-07-19 10:01:21 -04002512 spin_lock_irq(&phba->hbalock);
2513 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2514 spin_unlock_irq(&phba->hbalock);
2515 return;
2516 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002517 /* This is the initial FCF discovery scan */
2518 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04002519 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002520 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2521 "2778 Start FCF table scan at linkup\n");
2522
2523 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2524 LPFC_FCOE_FCF_GET_FIRST);
2525 if (rc) {
2526 spin_lock_irq(&phba->hbalock);
2527 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
2528 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002529 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002530 }
dea31012005-04-17 16:05:31 -05002531 }
James Smart6fb120a2009-05-22 14:52:59 -04002532
2533 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002534out:
2535 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002536 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2537 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2538 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002539 lpfc_issue_clear_la(phba, vport);
2540 return;
dea31012005-04-17 16:05:31 -05002541}
2542
2543static void
James Smart84774a42008-08-24 21:50:06 -04002544lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002545{
dea31012005-04-17 16:05:31 -05002546 uint32_t control;
2547 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002548 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002549 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002550 if (phba->sli_rev <= LPFC_SLI_REV3) {
2551 control = readl(phba->HCregaddr);
2552 control |= HC_LAINT_ENA;
2553 writel(control, phba->HCregaddr);
2554 readl(phba->HCregaddr); /* flush */
2555 }
James Smart2e0fef82007-06-17 19:56:36 -05002556 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002557}
2558
James Smart84774a42008-08-24 21:50:06 -04002559static void
2560lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2561{
2562 lpfc_linkdown(phba);
2563 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002564 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002565 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2566}
2567
2568
dea31012005-04-17 16:05:31 -05002569/*
2570 * This routine handles processing a READ_LA mailbox
2571 * command upon completion. It is setup in the LPFC_MBOXQ
2572 * as the completion routine when the command is
2573 * handed off to the SLI layer.
2574 */
2575void
James Smart2e0fef82007-06-17 19:56:36 -05002576lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002577{
James Smart2e0fef82007-06-17 19:56:36 -05002578 struct lpfc_vport *vport = pmb->vport;
2579 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002580 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002581 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002582 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2583
James Smart0d2b6b82008-06-14 22:52:47 -04002584 /* Unblock ELS traffic */
2585 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002586 /* Check for error */
2587 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002588 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002589 "1307 READ_LA mbox error x%x state x%x\n",
2590 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002591 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002592 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002593 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2594 }
2595
James Smart04c68492009-05-22 14:52:52 -04002596 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002597
2598 memcpy(&phba->alpa_map[0], mp->virt, 128);
2599
James Smart2e0fef82007-06-17 19:56:36 -05002600 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002601 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002602 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002603 else
James Smart2e0fef82007-06-17 19:56:36 -05002604 vport->fc_flag &= ~FC_BYPASSED_MODE;
2605 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002606
James Smart0c287582009-06-10 17:22:56 -04002607 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002608 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002609 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002610 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002611 if (phba->fc_eventTag != 0)
2612 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002613 }
dea31012005-04-17 16:05:31 -05002614
2615 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05002616 spin_lock_irq(&phba->hbalock);
James Smart84774a42008-08-24 21:50:06 -04002617 if (la->mm)
2618 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2619 else
2620 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05002621 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002622
James Smart4d9ab992009-10-02 15:16:39 -04002623 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002624 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002625 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002626 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002627 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002628 "1306 Link Up Event in loop back mode "
2629 "x%x received Data: x%x x%x x%x x%x\n",
2630 la->eventTag, phba->fc_eventTag,
2631 la->granted_AL_PA, la->UlnkSpeed,
2632 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002633 } else {
2634 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002635 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002636 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002637 la->eventTag, phba->fc_eventTag,
2638 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002639 phba->alpa_map[0],
2640 la->mm, la->fa,
2641 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002642 }
James Smart92d7f7b2007-06-17 19:56:38 -05002643 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002644 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002645 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002646 if (phba->link_flag & LS_LOOPBACK_MODE) {
2647 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2648 "1308 Link Down Event in loop back mode "
2649 "x%x received "
2650 "Data: x%x x%x x%x\n",
2651 la->eventTag, phba->fc_eventTag,
2652 phba->pport->port_state, vport->fc_flag);
2653 }
2654 else {
2655 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002656 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002657 "Data: x%x x%x x%x x%x x%x\n",
2658 la->eventTag, phba->fc_eventTag,
2659 phba->pport->port_state, vport->fc_flag,
2660 la->mm, la->fa);
2661 }
2662 lpfc_mbx_issue_link_down(phba);
2663 }
2664 if (la->mm && la->attType == AT_LINK_UP) {
2665 if (phba->link_state != LPFC_LINK_DOWN) {
2666 phba->fc_stat.LinkDown++;
2667 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2668 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002669 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002670 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002671 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002672 lpfc_mbx_issue_link_down(phba);
2673 } else
2674 lpfc_enable_la(phba);
2675
2676 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2677 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2678 "Data: x%x x%x x%x\n",
2679 la->eventTag, phba->fc_eventTag,
2680 phba->pport->port_state, vport->fc_flag);
2681 /*
2682 * The cmnd that triggered this will be waiting for this
2683 * signal.
2684 */
2685 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2686 if (phba->wait_4_mlo_maint_flg) {
2687 phba->wait_4_mlo_maint_flg = 0;
2688 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002689 }
James Smart84774a42008-08-24 21:50:06 -04002690 }
2691
2692 if (la->fa) {
2693 if (la->mm)
2694 lpfc_issue_clear_la(phba, vport);
2695 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2696 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002697 }
2698
2699lpfc_mbx_cmpl_read_la_free_mbuf:
2700 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2701 kfree(mp);
2702 mempool_free(pmb, phba->mbox_mem_pool);
2703 return;
2704}
2705
2706/*
2707 * This routine handles processing a REG_LOGIN mailbox
2708 * command upon completion. It is setup in the LPFC_MBOXQ
2709 * as the completion routine when the command is
2710 * handed off to the SLI layer.
2711 */
2712void
James Smart2e0fef82007-06-17 19:56:36 -05002713lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002714{
James Smart2e0fef82007-06-17 19:56:36 -05002715 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002716 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002717 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05002718
dea31012005-04-17 16:05:31 -05002719 pmb->context1 = NULL;
2720
2721 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05002722 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05002723 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2724 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002725 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002726 /* decrement the node reference count held for this callback
2727 * function.
2728 */
James Smart329f9bc2007-04-25 09:53:01 -04002729 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002730
2731 return;
2732}
2733
James Smart92d7f7b2007-06-17 19:56:38 -05002734static void
2735lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2736{
James Smart04c68492009-05-22 14:52:52 -04002737 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002738 struct lpfc_vport *vport = pmb->vport;
2739 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2740
2741 switch (mb->mbxStatus) {
2742 case 0x0011:
2743 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04002744 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2745 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2746 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002747 break;
James Smart78730cf2010-04-06 15:06:30 -04002748 /* If VPI is busy, reset the HBA */
2749 case 0x9700:
2750 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2751 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
2752 vport->vpi, mb->mbxStatus);
2753 if (!(phba->pport->load_flag & FC_UNLOADING))
2754 lpfc_workq_post_event(phba, NULL, NULL,
2755 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05002756 }
James Smart72100cc2010-02-12 14:43:01 -05002757 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002758 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05002759 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002760 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002761 vport->unreg_vpi_cmpl = VPORT_OK;
2762 mempool_free(pmb, phba->mbox_mem_pool);
2763 /*
2764 * This shost reference might have been taken at the beginning of
2765 * lpfc_vport_delete()
2766 */
James Smart1c6f4ef52009-11-18 15:40:49 -05002767 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05002768 scsi_host_put(shost);
2769}
2770
James Smartd7c255b2008-08-24 21:50:00 -04002771int
James Smart92d7f7b2007-06-17 19:56:38 -05002772lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2773{
2774 struct lpfc_hba *phba = vport->phba;
2775 LPFC_MBOXQ_t *mbox;
2776 int rc;
2777
2778 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2779 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002780 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002781
2782 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2783 mbox->vport = vport;
2784 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002785 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002786 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002787 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2788 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002789 mempool_free(mbox, phba->mbox_mem_pool);
2790 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002791 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002792 }
James Smartd7c255b2008-08-24 21:50:00 -04002793 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002794}
2795
2796static void
2797lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2798{
2799 struct lpfc_vport *vport = pmb->vport;
2800 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002801 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002802
2803 switch (mb->mbxStatus) {
2804 case 0x0011:
2805 case 0x9601:
2806 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002807 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2808 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2809 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002810 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2811 spin_lock_irq(shost->host_lock);
2812 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2813 spin_unlock_irq(shost->host_lock);
2814 vport->fc_myDID = 0;
2815 goto out;
2816 }
James Smart92d7f7b2007-06-17 19:56:38 -05002817
James Smart72100cc2010-02-12 14:43:01 -05002818 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002819 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002820 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002821 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002822 vport->num_disc_nodes = 0;
2823 /* go thru NPR list and issue ELS PLOGIs */
2824 if (vport->fc_npr_cnt)
2825 lpfc_els_disc_plogi(vport);
2826
2827 if (!vport->num_disc_nodes) {
2828 spin_lock_irq(shost->host_lock);
2829 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2830 spin_unlock_irq(shost->host_lock);
2831 lpfc_can_disctmo(vport);
2832 }
2833 vport->port_state = LPFC_VPORT_READY;
2834
2835out:
2836 mempool_free(pmb, phba->mbox_mem_pool);
2837 return;
2838}
2839
James Smart21e9a0a2009-05-22 14:53:21 -04002840/**
2841 * lpfc_create_static_vport - Read HBA config region to create static vports.
2842 * @phba: pointer to lpfc hba data structure.
2843 *
2844 * This routine issue a DUMP mailbox command for config region 22 to get
2845 * the list of static vports to be created. The function create vports
2846 * based on the information returned from the HBA.
2847 **/
2848void
2849lpfc_create_static_vport(struct lpfc_hba *phba)
2850{
2851 LPFC_MBOXQ_t *pmb = NULL;
2852 MAILBOX_t *mb;
2853 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04002854 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04002855 struct fc_vport_identifiers vport_id;
2856 struct fc_vport *new_fc_vport;
2857 struct Scsi_Host *shost;
2858 struct lpfc_vport *vport;
2859 uint16_t offset = 0;
2860 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04002861 struct lpfc_dmabuf *mp;
2862 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04002863
2864 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2865 if (!pmb) {
2866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2867 "0542 lpfc_create_static_vport failed to"
2868 " allocate mailbox memory\n");
2869 return;
2870 }
2871
2872 mb = &pmb->u.mb;
2873
2874 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2875 if (!vport_info) {
2876 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2877 "0543 lpfc_create_static_vport failed to"
2878 " allocate vport_info\n");
2879 mempool_free(pmb, phba->mbox_mem_pool);
2880 return;
2881 }
2882
2883 vport_buff = (uint8_t *) vport_info;
2884 do {
James Smart1c6834a2009-07-19 10:01:26 -04002885 if (lpfc_dump_static_vport(phba, pmb, offset))
2886 goto out;
2887
James Smart21e9a0a2009-05-22 14:53:21 -04002888 pmb->vport = phba->pport;
2889 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2890
2891 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2892 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2893 "0544 lpfc_create_static_vport failed to"
2894 " issue dump mailbox command ret 0x%x "
2895 "status 0x%x\n",
2896 rc, mb->mbxStatus);
2897 goto out;
2898 }
2899
James Smart1c6834a2009-07-19 10:01:26 -04002900 if (phba->sli_rev == LPFC_SLI_REV4) {
2901 byte_count = pmb->u.mqe.un.mb_words[5];
2902 mp = (struct lpfc_dmabuf *) pmb->context2;
2903 if (byte_count > sizeof(struct static_vport_info) -
2904 offset)
2905 byte_count = sizeof(struct static_vport_info)
2906 - offset;
2907 memcpy(vport_buff + offset, mp->virt, byte_count);
2908 offset += byte_count;
2909 } else {
2910 if (mb->un.varDmp.word_cnt >
2911 sizeof(struct static_vport_info) - offset)
2912 mb->un.varDmp.word_cnt =
2913 sizeof(struct static_vport_info)
2914 - offset;
2915 byte_count = mb->un.varDmp.word_cnt;
2916 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2917 vport_buff + offset,
2918 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04002919
James Smart1c6834a2009-07-19 10:01:26 -04002920 offset += byte_count;
2921 }
James Smart21e9a0a2009-05-22 14:53:21 -04002922
James Smart1c6834a2009-07-19 10:01:26 -04002923 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04002924 offset < sizeof(struct static_vport_info));
2925
2926
2927 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2928 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2929 != VPORT_INFO_REV)) {
2930 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2931 "0545 lpfc_create_static_vport bad"
2932 " information header 0x%x 0x%x\n",
2933 le32_to_cpu(vport_info->signature),
2934 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2935
2936 goto out;
2937 }
2938
2939 shost = lpfc_shost_from_vport(phba->pport);
2940
2941 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2942 memset(&vport_id, 0, sizeof(vport_id));
2943 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2944 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2945 if (!vport_id.port_name || !vport_id.node_name)
2946 continue;
2947
2948 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2949 vport_id.vport_type = FC_PORTTYPE_NPIV;
2950 vport_id.disable = false;
2951 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2952
2953 if (!new_fc_vport) {
2954 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2955 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04002956 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04002957 continue;
2958 }
2959
2960 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2961 vport->vport_flag |= STATIC_VPORT;
2962 }
2963
2964out:
James Smart21e9a0a2009-05-22 14:53:21 -04002965 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04002966 if (rc != MBX_TIMEOUT) {
2967 if (pmb->context2) {
2968 mp = (struct lpfc_dmabuf *) pmb->context2;
2969 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2970 kfree(mp);
2971 }
James Smart21e9a0a2009-05-22 14:53:21 -04002972 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002973 }
James Smart21e9a0a2009-05-22 14:53:21 -04002974
2975 return;
2976}
2977
dea31012005-04-17 16:05:31 -05002978/*
2979 * This routine handles processing a Fabric REG_LOGIN mailbox
2980 * command upon completion. It is setup in the LPFC_MBOXQ
2981 * as the completion routine when the command is
2982 * handed off to the SLI layer.
2983 */
2984void
James Smart2e0fef82007-06-17 19:56:36 -05002985lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002986{
James Smart92d7f7b2007-06-17 19:56:38 -05002987 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04002988 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002989 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05002990 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002991
James Smart549e55c2007-08-02 11:09:51 -04002992 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04002993 pmb->context1 = NULL;
2994 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05002995 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04002996 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2997 "0258 Register Fabric login error: 0x%x\n",
2998 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05002999 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3000 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003001 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003002
James Smart92d7f7b2007-06-17 19:56:38 -05003003 if (phba->fc_topology == TOPOLOGY_LOOP) {
3004 /* FLOGI failed, use loop map to make discovery list */
3005 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003006
James Smart92d7f7b2007-06-17 19:56:38 -05003007 /* Start discovery */
3008 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003009 /* Decrement the reference count to ndlp after the
3010 * reference to the ndlp are done.
3011 */
3012 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003013 return;
3014 }
3015
3016 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003017 /* Decrement the reference count to ndlp after the reference
3018 * to the ndlp are done.
3019 */
3020 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003021 return;
3022 }
3023
dea31012005-04-17 16:05:31 -05003024 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003025 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003026 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003027 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003028
James Smart2e0fef82007-06-17 19:56:36 -05003029 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003030 /* when physical port receive logo donot start
3031 * vport discovery */
3032 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3033 lpfc_start_fdiscs(phba);
3034 else
3035 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smart92d7f7b2007-06-17 19:56:38 -05003036 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003037 }
3038
3039 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3040 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003041 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003042
3043 /* Drop the reference count from the mbox at the end after
3044 * all the current reference to the ndlp have been done.
3045 */
3046 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003047 return;
3048}
3049
3050/*
3051 * This routine handles processing a NameServer REG_LOGIN mailbox
3052 * command upon completion. It is setup in the LPFC_MBOXQ
3053 * as the completion routine when the command is
3054 * handed off to the SLI layer.
3055 */
3056void
James Smart2e0fef82007-06-17 19:56:36 -05003057lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003058{
James Smart21e9a0a2009-05-22 14:53:21 -04003059 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003060 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3061 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3062 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003063
3064 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003065out:
James Smart21e9a0a2009-05-22 14:53:21 -04003066 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3067 "0260 Register NameServer error: 0x%x\n",
3068 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003069 /* decrement the node reference count held for this
3070 * callback function.
3071 */
James Smart329f9bc2007-04-25 09:53:01 -04003072 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003073 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3074 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003075 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003076
3077 /* If no other thread is using the ndlp, free it */
3078 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003079
James Smart92d7f7b2007-06-17 19:56:38 -05003080 if (phba->fc_topology == TOPOLOGY_LOOP) {
3081 /*
3082 * RegLogin failed, use loop map to make discovery
3083 * list
3084 */
3085 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003086
James Smart92d7f7b2007-06-17 19:56:38 -05003087 /* Start discovery */
3088 lpfc_disc_start(vport);
3089 return;
3090 }
3091 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003092 return;
3093 }
3094
3095 pmb->context1 = NULL;
3096
dea31012005-04-17 16:05:31 -05003097 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003098 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003099 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003100 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003101
James Smart2e0fef82007-06-17 19:56:36 -05003102 if (vport->port_state < LPFC_VPORT_READY) {
3103 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003104 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3105 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3106 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3107 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3108 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3109
3110 /* Issue SCR just before NameServer GID_FT Query */
3111 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003112 }
3113
James Smart2e0fef82007-06-17 19:56:36 -05003114 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003115 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003116 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003117 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003118 goto out;
dea31012005-04-17 16:05:31 -05003119 }
3120
James Smartfa4066b2008-01-11 01:53:27 -05003121 /* decrement the node reference count held for this
3122 * callback function.
3123 */
James Smart329f9bc2007-04-25 09:53:01 -04003124 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003125 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3126 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003127 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003128
3129 return;
3130}
3131
3132static void
James Smart2e0fef82007-06-17 19:56:36 -05003133lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003134{
James Smart2e0fef82007-06-17 19:56:36 -05003135 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3136 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003137 struct lpfc_rport_data *rdata;
3138 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003139 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003140
3141 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003142 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3143 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003144 rport_ids.port_id = ndlp->nlp_DID;
3145 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003146
James Smart329f9bc2007-04-25 09:53:01 -04003147 /*
3148 * We leave our node pointer in rport->dd_data when we unregister a
3149 * FCP target port. But fc_remote_port_add zeros the space to which
3150 * rport->dd_data points. So, if we're reusing a previously
3151 * registered port, drop the reference that we took the last time we
3152 * registered the port.
3153 */
3154 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003155 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003156 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003157
3158 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3159 "rport add: did:x%x flg:x%x type x%x",
3160 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3161
James Smart2e0fef82007-06-17 19:56:36 -05003162 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003163 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003164 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3165 "Warning: fc_remote_port_add failed\n");
3166 return;
3167 }
3168
3169 /* initialize static port data */
3170 rport->maxframe_size = ndlp->nlp_maxframe;
3171 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003172 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003173 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003174
3175 if (ndlp->nlp_type & NLP_FCP_TARGET)
3176 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3177 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3178 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3179
3180
3181 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3182 fc_remote_port_rolechg(rport, rport_ids.roles);
3183
James Smart071fbd3d2006-04-15 11:53:20 -04003184 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003185 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003186 ndlp->nlp_sid = rport->scsi_target_id;
3187 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003188 return;
3189}
3190
3191static void
James Smart2e0fef82007-06-17 19:56:36 -05003192lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003193{
3194 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003195
James Smart858c9f62007-06-17 19:56:39 -05003196 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3197 "rport delete: did:x%x flg:x%x type x%x",
3198 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3199
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003200 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003201
3202 return;
3203}
3204
James Smartde0c5b32007-04-25 09:52:27 -04003205static void
James Smart2e0fef82007-06-17 19:56:36 -05003206lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003207{
James Smart2e0fef82007-06-17 19:56:36 -05003208 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3209
3210 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003211 switch (state) {
3212 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003213 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003214 break;
3215 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003216 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003217 break;
3218 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003219 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003220 break;
3221 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003222 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003223 break;
3224 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003225 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003226 break;
3227 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003228 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003229 break;
3230 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003231 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003232 break;
3233 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003234 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003235 break;
3236 }
James Smart2e0fef82007-06-17 19:56:36 -05003237 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003238}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003239
James Smartde0c5b32007-04-25 09:52:27 -04003240static void
James Smart2e0fef82007-06-17 19:56:36 -05003241lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003242 int old_state, int new_state)
3243{
James Smart2e0fef82007-06-17 19:56:36 -05003244 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3245
James Smartde0c5b32007-04-25 09:52:27 -04003246 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04003247 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3248 ndlp->nlp_type |= NLP_FC_NODE;
3249 }
3250 if (new_state == NLP_STE_MAPPED_NODE)
3251 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3252 if (new_state == NLP_STE_NPR_NODE)
3253 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003254
James Smartde0c5b32007-04-25 09:52:27 -04003255 /* Transport interface */
3256 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3257 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003258 vport->phba->nport_event_cnt++;
3259 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003260 }
dea31012005-04-17 16:05:31 -05003261
James Smartde0c5b32007-04-25 09:52:27 -04003262 if (new_state == NLP_STE_MAPPED_NODE ||
3263 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003264 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003265 /*
3266 * Tell the fc transport about the port, if we haven't
3267 * already. If we have, and it's a scsi entity, be
3268 * sure to unblock any attached scsi devices
3269 */
3270 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003271 }
James Smartea2151b2008-09-07 11:52:10 -04003272 if ((new_state == NLP_STE_MAPPED_NODE) &&
3273 (vport->stat_data_enabled)) {
3274 /*
3275 * A new target is discovered, if there is no buffer for
3276 * statistical data collection allocate buffer.
3277 */
3278 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3279 sizeof(struct lpfc_scsicmd_bkt),
3280 GFP_KERNEL);
3281
3282 if (!ndlp->lat_data)
3283 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3284 "0286 lpfc_nlp_state_cleanup failed to "
3285 "allocate statistical data buffer DID "
3286 "0x%x\n", ndlp->nlp_DID);
3287 }
James Smart858c9f62007-06-17 19:56:39 -05003288 /*
3289 * if we added to Mapped list, but the remote port
3290 * registration failed or assigned a target id outside
3291 * our presentable range - move the node to the
3292 * Unmapped List
3293 */
James Smartde0c5b32007-04-25 09:52:27 -04003294 if (new_state == NLP_STE_MAPPED_NODE &&
3295 (!ndlp->rport ||
3296 ndlp->rport->scsi_target_id == -1 ||
3297 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003298 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003299 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003300 spin_unlock_irq(shost->host_lock);
3301 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003302 }
James Smartde0c5b32007-04-25 09:52:27 -04003303}
3304
James Smart685f0bf2007-04-25 09:53:08 -04003305static char *
3306lpfc_nlp_state_name(char *buffer, size_t size, int state)
3307{
3308 static char *states[] = {
3309 [NLP_STE_UNUSED_NODE] = "UNUSED",
3310 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
3311 [NLP_STE_ADISC_ISSUE] = "ADISC",
3312 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
3313 [NLP_STE_PRLI_ISSUE] = "PRLI",
3314 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
3315 [NLP_STE_MAPPED_NODE] = "MAPPED",
3316 [NLP_STE_NPR_NODE] = "NPR",
3317 };
3318
James Smart311464e2007-08-02 11:10:37 -04003319 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04003320 strlcpy(buffer, states[state], size);
3321 else
3322 snprintf(buffer, size, "unknown (%d)", state);
3323 return buffer;
3324}
3325
James Smartde0c5b32007-04-25 09:52:27 -04003326void
James Smart2e0fef82007-06-17 19:56:36 -05003327lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3328 int state)
James Smartde0c5b32007-04-25 09:52:27 -04003329{
James Smart2e0fef82007-06-17 19:56:36 -05003330 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04003331 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04003332 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04003333
James Smarte8b62012007-08-02 11:10:09 -04003334 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3335 "0904 NPort state transition x%06x, %s -> %s\n",
3336 ndlp->nlp_DID,
3337 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
3338 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05003339
3340 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3341 "node statechg did:x%x old:%d ste:%d",
3342 ndlp->nlp_DID, old_state, state);
3343
James Smartde0c5b32007-04-25 09:52:27 -04003344 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04003345 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05003346 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003347 if (old_state == NLP_STE_UNMAPPED_NODE) {
3348 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
3349 ndlp->nlp_type &= ~NLP_FC_NODE;
3350 }
3351
James Smart685f0bf2007-04-25 09:53:08 -04003352 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05003353 spin_lock_irq(shost->host_lock);
3354 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3355 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003356 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05003357 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04003358
3359 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05003360 lpfc_nlp_counters(vport, state, 1);
3361 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04003362}
3363
3364void
James Smarte47c9092008-02-08 18:49:26 -05003365lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3366{
3367 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3368
3369 if (list_empty(&ndlp->nlp_listp)) {
3370 spin_lock_irq(shost->host_lock);
3371 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3372 spin_unlock_irq(shost->host_lock);
3373 }
3374}
3375
3376void
James Smart2e0fef82007-06-17 19:56:36 -05003377lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003378{
James Smart2e0fef82007-06-17 19:56:36 -05003379 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3380
James Smart0d2b6b82008-06-14 22:52:47 -04003381 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003382 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05003383 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3384 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003385 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05003386 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003387 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05003388 NLP_STE_UNUSED_NODE);
3389}
3390
Adrian Bunk4d9db012008-02-14 23:24:02 +02003391static void
James Smarte47c9092008-02-08 18:49:26 -05003392lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3393{
James Smart0d2b6b82008-06-14 22:52:47 -04003394 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003395 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3396 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3397 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3398 NLP_STE_UNUSED_NODE);
3399}
James Smart109f6ed2008-12-04 22:39:08 -05003400/**
James Smart3621a712009-04-06 18:47:14 -04003401 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05003402 * @vport: Pointer to Virtual Port object.
3403 * @ndlp: Pointer to FC node object.
3404 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04003405 *
3406 * This function is always called when node object need to be initialized.
3407 * It initializes all the fields of the node object. Although the reference
3408 * to phba from @ndlp can be obtained indirectly through it's reference to
3409 * @vport, a direct reference to phba is taken here by @ndlp. This is due
3410 * to the life-span of the @ndlp might go beyond the existence of @vport as
3411 * the final release of ndlp is determined by its reference count. And, the
3412 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05003413 **/
3414static inline void
3415lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3416 uint32_t did)
3417{
3418 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3419 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3420 init_timer(&ndlp->nlp_delayfunc);
3421 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3422 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3423 ndlp->nlp_DID = did;
3424 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04003425 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05003426 ndlp->nlp_sid = NLP_NO_SID;
3427 kref_init(&ndlp->kref);
3428 NLP_INT_NODE_ACT(ndlp);
3429 atomic_set(&ndlp->cmd_pending, 0);
3430 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
3431}
James Smarte47c9092008-02-08 18:49:26 -05003432
3433struct lpfc_nodelist *
3434lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3435 int state)
3436{
3437 struct lpfc_hba *phba = vport->phba;
3438 uint32_t did;
3439 unsigned long flags;
3440
3441 if (!ndlp)
3442 return NULL;
3443
3444 spin_lock_irqsave(&phba->ndlp_lock, flags);
3445 /* The ndlp should not be in memory free mode */
3446 if (NLP_CHK_FREE_REQ(ndlp)) {
3447 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3448 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3449 "0277 lpfc_enable_node: ndlp:x%p "
3450 "usgmap:x%x refcnt:%d\n",
3451 (void *)ndlp, ndlp->nlp_usg_map,
3452 atomic_read(&ndlp->kref.refcount));
3453 return NULL;
3454 }
3455 /* The ndlp should not already be in active mode */
3456 if (NLP_CHK_NODE_ACT(ndlp)) {
3457 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3458 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3459 "0278 lpfc_enable_node: ndlp:x%p "
3460 "usgmap:x%x refcnt:%d\n",
3461 (void *)ndlp, ndlp->nlp_usg_map,
3462 atomic_read(&ndlp->kref.refcount));
3463 return NULL;
3464 }
3465
3466 /* Keep the original DID */
3467 did = ndlp->nlp_DID;
3468
3469 /* re-initialize ndlp except of ndlp linked list pointer */
3470 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3471 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003472 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003473
3474 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3475
3476 if (state != NLP_STE_UNUSED_NODE)
3477 lpfc_nlp_set_state(vport, ndlp, state);
3478
3479 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3480 "node enable: did:x%x",
3481 ndlp->nlp_DID, 0, 0);
3482 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003483}
3484
3485void
James Smart2e0fef82007-06-17 19:56:36 -05003486lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003487{
James Smart87af33f2007-10-27 13:37:43 -04003488 /*
James Smartfa4066b2008-01-11 01:53:27 -05003489 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003490 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003491 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3492 * until ALL other outstanding threads have completed. We check
3493 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003494 */
James Smartfa4066b2008-01-11 01:53:27 -05003495 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3496 return;
James Smart51ef4c22007-08-02 11:10:31 -04003497 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003498 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003499 return;
dea31012005-04-17 16:05:31 -05003500}
3501
3502/*
3503 * Start / ReStart rescue timer for Discovery / RSCN handling
3504 */
3505void
James Smart2e0fef82007-06-17 19:56:36 -05003506lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003507{
James Smart2e0fef82007-06-17 19:56:36 -05003508 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3509 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003510 uint32_t tmo;
3511
James Smart2e0fef82007-06-17 19:56:36 -05003512 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003513 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003514 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3515 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003516 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003517 * FC spec states we need 3 * ratov for CT requests
3518 */
3519 tmo = ((phba->fc_ratov * 3) + 3);
3520 }
dea31012005-04-17 16:05:31 -05003521
James Smart858c9f62007-06-17 19:56:39 -05003522
3523 if (!timer_pending(&vport->fc_disctmo)) {
3524 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3525 "set disc timer: tmo:x%x state:x%x flg:x%x",
3526 tmo, vport->port_state, vport->fc_flag);
3527 }
3528
James Smart2e0fef82007-06-17 19:56:36 -05003529 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3530 spin_lock_irq(shost->host_lock);
3531 vport->fc_flag |= FC_DISC_TMO;
3532 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003533
3534 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003535 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3536 "0247 Start Discovery Timer state x%x "
3537 "Data: x%x x%lx x%x x%x\n",
3538 vport->port_state, tmo,
3539 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3540 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003541
3542 return;
3543}
3544
3545/*
3546 * Cancel rescue timer for Discovery / RSCN handling
3547 */
3548int
James Smart2e0fef82007-06-17 19:56:36 -05003549lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003550{
James Smart2e0fef82007-06-17 19:56:36 -05003551 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003552 unsigned long iflags;
3553
James Smart858c9f62007-06-17 19:56:39 -05003554 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3555 "can disc timer: state:x%x rtry:x%x flg:x%x",
3556 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3557
dea31012005-04-17 16:05:31 -05003558 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003559 if (vport->fc_flag & FC_DISC_TMO) {
3560 spin_lock_irqsave(shost->host_lock, iflags);
3561 vport->fc_flag &= ~FC_DISC_TMO;
3562 spin_unlock_irqrestore(shost->host_lock, iflags);
3563 del_timer_sync(&vport->fc_disctmo);
3564 spin_lock_irqsave(&vport->work_port_lock, iflags);
3565 vport->work_port_events &= ~WORKER_DISC_TMO;
3566 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003567 }
3568
3569 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003570 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3571 "0248 Cancel Discovery Timer state x%x "
3572 "Data: x%x x%x x%x\n",
3573 vport->port_state, vport->fc_flag,
3574 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003575 return 0;
dea31012005-04-17 16:05:31 -05003576}
3577
3578/*
3579 * Check specified ring for outstanding IOCB on the SLI queue
3580 * Return true if iocb matches the specified nport
3581 */
3582int
James Smart2e0fef82007-06-17 19:56:36 -05003583lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3584 struct lpfc_sli_ring *pring,
3585 struct lpfc_iocbq *iocb,
3586 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003587{
James Smart2e0fef82007-06-17 19:56:36 -05003588 struct lpfc_sli *psli = &phba->sli;
3589 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003590 struct lpfc_vport *vport = ndlp->vport;
3591
3592 if (iocb->vport != vport)
3593 return 0;
3594
dea31012005-04-17 16:05:31 -05003595 if (pring->ringno == LPFC_ELS_RING) {
3596 switch (icmd->ulpCommand) {
3597 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003598 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003599 return 1;
dea31012005-04-17 16:05:31 -05003600 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003601 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3602 return 1;
dea31012005-04-17 16:05:31 -05003603 case CMD_XMIT_ELS_RSP64_CX:
3604 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003605 return 1;
dea31012005-04-17 16:05:31 -05003606 }
James Smarta4bc3372006-12-02 13:34:16 -05003607 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003608
3609 } else if (pring->ringno == psli->fcp_ring) {
3610 /* Skip match check if waiting to relogin to FCP target */
3611 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003612 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003613 return 0;
dea31012005-04-17 16:05:31 -05003614 }
3615 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003616 return 1;
dea31012005-04-17 16:05:31 -05003617 }
3618 } else if (pring->ringno == psli->next_ring) {
3619
3620 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003621 return 0;
dea31012005-04-17 16:05:31 -05003622}
3623
3624/*
3625 * Free resources / clean up outstanding I/Os
3626 * associated with nlp_rpi in the LPFC_NODELIST entry.
3627 */
3628static int
James Smart2e0fef82007-06-17 19:56:36 -05003629lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003630{
James Smart2534ba72007-04-25 09:52:20 -04003631 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003632 struct lpfc_sli *psli;
3633 struct lpfc_sli_ring *pring;
3634 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003635 uint32_t i;
dea31012005-04-17 16:05:31 -05003636
James Smart92d7f7b2007-06-17 19:56:38 -05003637 lpfc_fabric_abort_nport(ndlp);
3638
dea31012005-04-17 16:05:31 -05003639 /*
3640 * Everything that matches on txcmplq will be returned
3641 * by firmware with a no rpi error.
3642 */
3643 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003644 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003645 /* Now process each ring */
3646 for (i = 0; i < psli->num_rings; i++) {
3647 pring = &psli->ring[i];
3648
James Smart2e0fef82007-06-17 19:56:36 -05003649 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003650 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003651 list) {
dea31012005-04-17 16:05:31 -05003652 /*
3653 * Check to see if iocb matches the nport we are
3654 * looking for
3655 */
James Smart92d7f7b2007-06-17 19:56:38 -05003656 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3657 ndlp))) {
dea31012005-04-17 16:05:31 -05003658 /* It matches, so deque and call compl
3659 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003660 list_move_tail(&iocb->list,
3661 &completions);
dea31012005-04-17 16:05:31 -05003662 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003663 }
3664 }
James Smart2e0fef82007-06-17 19:56:36 -05003665 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003666 }
3667 }
James Smart2534ba72007-04-25 09:52:20 -04003668
James Smarta257bf92009-04-06 18:48:10 -04003669 /* Cancel all the IOCBs from the completions list */
3670 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3671 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003672
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003673 return 0;
dea31012005-04-17 16:05:31 -05003674}
3675
3676/*
3677 * Free rpi associated with LPFC_NODELIST entry.
3678 * This routine is called from lpfc_freenode(), when we are removing
3679 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3680 * LOGO that completes successfully, and we are waiting to PLOGI back
3681 * to the remote NPort. In addition, it is called after we receive
3682 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3683 * we are waiting to PLOGI back to the remote NPort.
3684 */
3685int
James Smart2e0fef82007-06-17 19:56:36 -05003686lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003687{
James Smart2e0fef82007-06-17 19:56:36 -05003688 struct lpfc_hba *phba = vport->phba;
3689 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003690 int rc;
3691
James Smart21e9a0a2009-05-22 14:53:21 -04003692 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003693 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3694 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003695 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003696 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003697 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003698 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003699 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003700 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003701 }
dea31012005-04-17 16:05:31 -05003702 lpfc_no_rpi(phba, ndlp);
3703 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003704 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003705 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003706 return 1;
3707 }
3708 return 0;
3709}
3710
James Smartecfd03c2010-02-12 14:41:27 -05003711/**
3712 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
3713 * @phba: pointer to lpfc hba data structure.
3714 *
3715 * This routine is invoked to unregister all the currently registered RPIs
3716 * to the HBA.
3717 **/
3718void
3719lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
3720{
3721 struct lpfc_vport **vports;
3722 struct lpfc_nodelist *ndlp;
3723 struct Scsi_Host *shost;
3724 int i;
3725
3726 vports = lpfc_create_vport_work_array(phba);
3727 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3728 shost = lpfc_shost_from_vport(vports[i]);
3729 spin_lock_irq(shost->host_lock);
3730 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003731 if (ndlp->nlp_flag & NLP_RPI_VALID) {
3732 /* The mempool_alloc might sleep */
3733 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05003734 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05003735 spin_lock_irq(shost->host_lock);
3736 }
James Smartecfd03c2010-02-12 14:41:27 -05003737 }
3738 spin_unlock_irq(shost->host_lock);
3739 }
3740 lpfc_destroy_vport_work_array(phba, vports);
3741}
3742
James Smart92d7f7b2007-06-17 19:56:38 -05003743void
3744lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3745{
3746 struct lpfc_hba *phba = vport->phba;
3747 LPFC_MBOXQ_t *mbox;
3748 int rc;
3749
3750 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3751 if (mbox) {
3752 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3753 mbox->vport = vport;
3754 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003755 mbox->context1 = NULL;
3756 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003757 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003758 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003759
3760 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3761 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3762 "1836 Could not issue "
3763 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003764 }
3765}
3766
3767void
3768lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3769{
3770 struct lpfc_hba *phba = vport->phba;
3771 LPFC_MBOXQ_t *mbox;
3772 int rc;
3773
3774 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3775 if (mbox) {
3776 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3777 mbox->vport = vport;
3778 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003779 mbox->context1 = NULL;
3780 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003781 if (rc != MBX_TIMEOUT)
3782 mempool_free(mbox, phba->mbox_mem_pool);
3783
3784 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003785 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3786 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003787 "unreg_did (default rpis) status %d\n",
3788 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003789 }
3790}
3791
dea31012005-04-17 16:05:31 -05003792/*
3793 * Free resources associated with LPFC_NODELIST entry
3794 * so it can be freed.
3795 */
3796static int
James Smart2e0fef82007-06-17 19:56:36 -05003797lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003798{
James Smart2e0fef82007-06-17 19:56:36 -05003799 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3800 struct lpfc_hba *phba = vport->phba;
3801 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003802 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003803
3804 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003805 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3806 "0900 Cleanup node for NPort x%x "
3807 "Data: x%x x%x x%x\n",
3808 ndlp->nlp_DID, ndlp->nlp_flag,
3809 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003810 if (NLP_CHK_FREE_REQ(ndlp)) {
3811 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3812 "0280 lpfc_cleanup_node: ndlp:x%p "
3813 "usgmap:x%x refcnt:%d\n",
3814 (void *)ndlp, ndlp->nlp_usg_map,
3815 atomic_read(&ndlp->kref.refcount));
3816 lpfc_dequeue_node(vport, ndlp);
3817 } else {
3818 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3819 "0281 lpfc_cleanup_node: ndlp:x%p "
3820 "usgmap:x%x refcnt:%d\n",
3821 (void *)ndlp, ndlp->nlp_usg_map,
3822 atomic_read(&ndlp->kref.refcount));
3823 lpfc_disable_node(vport, ndlp);
3824 }
dea31012005-04-17 16:05:31 -05003825
dea31012005-04-17 16:05:31 -05003826 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3827 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003828 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003829 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3830 mb->context2 = NULL;
3831 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3832 }
3833 }
James Smart33ccf8d2006-08-17 11:57:58 -04003834
James Smart2e0fef82007-06-17 19:56:36 -05003835 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003836 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003837 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003838 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003839 mp = (struct lpfc_dmabuf *) (mb->context1);
3840 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003841 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003842 kfree(mp);
3843 }
3844 list_del(&mb->list);
3845 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003846 /* We shall not invoke the lpfc_nlp_put to decrement
3847 * the ndlp reference count as we are in the process
3848 * of lpfc_nlp_release.
3849 */
dea31012005-04-17 16:05:31 -05003850 }
3851 }
James Smart2e0fef82007-06-17 19:56:36 -05003852 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003853
James Smarte47c9092008-02-08 18:49:26 -05003854 lpfc_els_abort(phba, ndlp);
3855
James Smart2e0fef82007-06-17 19:56:36 -05003856 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003857 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003858 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003859
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003860 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003861 del_timer_sync(&ndlp->nlp_delayfunc);
3862
James Smart0d2b6b82008-06-14 22:52:47 -04003863 list_del_init(&ndlp->els_retry_evt.evt_listp);
3864 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003865
James Smart2e0fef82007-06-17 19:56:36 -05003866 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003867
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003868 return 0;
dea31012005-04-17 16:05:31 -05003869}
3870
3871/*
3872 * Check to see if we can free the nlp back to the freelist.
3873 * If we are in the middle of using the nlp in the discovery state
3874 * machine, defer the free till we reach the end of the state machine.
3875 */
James Smart329f9bc2007-04-25 09:53:01 -04003876static void
James Smart2e0fef82007-06-17 19:56:36 -05003877lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003878{
James Smarta8adb832007-10-27 13:37:53 -04003879 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003880 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003881 LPFC_MBOXQ_t *mbox;
3882 int rc;
dea31012005-04-17 16:05:31 -05003883
James Smart0d2b6b82008-06-14 22:52:47 -04003884 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003885 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3886 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003887 /* For this case we need to cleanup the default rpi
3888 * allocated by the firmware.
3889 */
3890 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3891 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003892 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003893 (uint8_t *) &vport->fc_sparam, mbox, 0);
3894 if (rc) {
3895 mempool_free(mbox, phba->mbox_mem_pool);
3896 }
3897 else {
3898 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3899 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3900 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003901 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003902 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3903 if (rc == MBX_NOT_FINISHED) {
3904 mempool_free(mbox, phba->mbox_mem_pool);
3905 }
3906 }
3907 }
3908 }
James Smart2e0fef82007-06-17 19:56:36 -05003909 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003910
James Smart2e0fef82007-06-17 19:56:36 -05003911 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003912 * We can get here with a non-NULL ndlp->rport because when we
3913 * unregister a rport we don't break the rport/node linkage. So if we
3914 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003915 */
James Smart92d7f7b2007-06-17 19:56:38 -05003916 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003917 rdata = ndlp->rport->dd_data;
3918 rdata->pnode = NULL;
3919 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003920 }
dea31012005-04-17 16:05:31 -05003921}
3922
3923static int
James Smart2e0fef82007-06-17 19:56:36 -05003924lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3925 uint32_t did)
dea31012005-04-17 16:05:31 -05003926{
James Smart2e0fef82007-06-17 19:56:36 -05003927 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003928
3929 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003930 return 0;
dea31012005-04-17 16:05:31 -05003931
dea31012005-04-17 16:05:31 -05003932 /* First check for Direct match */
3933 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003934 return 1;
dea31012005-04-17 16:05:31 -05003935
3936 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003937 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003938 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003939 return 0;
dea31012005-04-17 16:05:31 -05003940 }
3941
3942 matchdid.un.word = did;
3943 ndlpdid.un.word = ndlp->nlp_DID;
3944 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3945 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3946 (mydid.un.b.area == matchdid.un.b.area)) {
3947 if ((ndlpdid.un.b.domain == 0) &&
3948 (ndlpdid.un.b.area == 0)) {
3949 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003950 return 1;
dea31012005-04-17 16:05:31 -05003951 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003952 return 0;
dea31012005-04-17 16:05:31 -05003953 }
3954
3955 matchdid.un.word = ndlp->nlp_DID;
3956 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3957 (mydid.un.b.area == ndlpdid.un.b.area)) {
3958 if ((matchdid.un.b.domain == 0) &&
3959 (matchdid.un.b.area == 0)) {
3960 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003961 return 1;
dea31012005-04-17 16:05:31 -05003962 }
3963 }
3964 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003965 return 0;
dea31012005-04-17 16:05:31 -05003966}
3967
James Smart685f0bf2007-04-25 09:53:08 -04003968/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003969static struct lpfc_nodelist *
3970__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003971{
James Smart2fb9bd82006-12-02 13:33:57 -05003972 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003973 uint32_t data1;
3974
James Smart2e0fef82007-06-17 19:56:36 -05003975 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3976 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04003977 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3978 ((uint32_t) ndlp->nlp_xri << 16) |
3979 ((uint32_t) ndlp->nlp_type << 8) |
3980 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04003981 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3982 "0929 FIND node DID "
3983 "Data: x%p x%x x%x x%x\n",
3984 ndlp, ndlp->nlp_DID,
3985 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04003986 return ndlp;
dea31012005-04-17 16:05:31 -05003987 }
3988 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003989
dea31012005-04-17 16:05:31 -05003990 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04003991 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3992 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05003993 return NULL;
3994}
3995
3996struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003997lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003998{
James Smart2e0fef82007-06-17 19:56:36 -05003999 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004000 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004001
James Smart2e0fef82007-06-17 19:56:36 -05004002 spin_lock_irq(shost->host_lock);
4003 ndlp = __lpfc_findnode_did(vport, did);
4004 spin_unlock_irq(shost->host_lock);
4005 return ndlp;
4006}
4007
4008struct lpfc_nodelist *
4009lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4010{
4011 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4012 struct lpfc_nodelist *ndlp;
4013
4014 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004015 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004016 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4017 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004018 return NULL;
4019 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004020 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004021 if (!ndlp)
4022 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004023 lpfc_nlp_init(vport, ndlp, did);
4024 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4025 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004026 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004027 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004028 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004029 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4030 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4031 if (!ndlp)
4032 return NULL;
4033 spin_lock_irq(shost->host_lock);
4034 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4035 spin_unlock_irq(shost->host_lock);
4036 return ndlp;
dea31012005-04-17 16:05:31 -05004037 }
James Smarte47c9092008-02-08 18:49:26 -05004038
James Smart58da1ff2008-04-07 10:15:56 -04004039 if ((vport->fc_flag & FC_RSCN_MODE) &&
4040 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004041 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04004042 /* If we've already recieved a PLOGI from this NPort
4043 * we don't need to try to discover it again.
4044 */
4045 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4046 return NULL;
4047
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004048 /* Since this node is marked for discovery,
4049 * delay timeout is not needed.
4050 */
James Smart0d2b6b82008-06-14 22:52:47 -04004051 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004052 spin_lock_irq(shost->host_lock);
4053 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4054 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004055 } else
dea31012005-04-17 16:05:31 -05004056 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004057 } else {
James Smart87af33f2007-10-27 13:37:43 -04004058 /* If we've already recieved a PLOGI from this NPort,
4059 * or we are already in the process of discovery on it,
4060 * we don't need to try to discover it again.
4061 */
James Smart685f0bf2007-04-25 09:53:08 -04004062 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004063 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4064 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004065 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004066 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4067 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004068 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004069 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004070 }
4071 return ndlp;
4072}
4073
4074/* Build a list of nodes to discover based on the loopmap */
4075void
James Smart2e0fef82007-06-17 19:56:36 -05004076lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004077{
James Smart2e0fef82007-06-17 19:56:36 -05004078 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004079 int j;
4080 uint32_t alpa, index;
4081
James Smart2e0fef82007-06-17 19:56:36 -05004082 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004083 return;
James Smart2e0fef82007-06-17 19:56:36 -05004084
4085 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004086 return;
dea31012005-04-17 16:05:31 -05004087
4088 /* Check for loop map present or not */
4089 if (phba->alpa_map[0]) {
4090 for (j = 1; j <= phba->alpa_map[0]; j++) {
4091 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004092 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004093 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004094 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004095 }
4096 } else {
4097 /* No alpamap, so try all alpa's */
4098 for (j = 0; j < FC_MAXLOOP; j++) {
4099 /* If cfg_scan_down is set, start from highest
4100 * ALPA (0xef) to lowest (0x1).
4101 */
James Smart3de2a652007-08-02 11:09:59 -04004102 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004103 index = j;
4104 else
4105 index = FC_MAXLOOP - j - 1;
4106 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004107 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004108 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004109 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004110 }
4111 }
4112 return;
4113}
4114
dea31012005-04-17 16:05:31 -05004115void
James Smart2e0fef82007-06-17 19:56:36 -05004116lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004117{
dea31012005-04-17 16:05:31 -05004118 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004119 struct lpfc_sli *psli = &phba->sli;
4120 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4121 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4122 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4123 int rc;
4124
James Smart92d7f7b2007-06-17 19:56:38 -05004125 /*
4126 * if it's not a physical port or if we already send
4127 * clear_la then don't send it.
4128 */
4129 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004130 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4131 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004132 return;
4133
James Smart2e0fef82007-06-17 19:56:36 -05004134 /* Link up discovery */
4135 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4136 phba->link_state = LPFC_CLEAR_LA;
4137 lpfc_clear_la(phba, mbox);
4138 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4139 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004140 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004141 if (rc == MBX_NOT_FINISHED) {
4142 mempool_free(mbox, phba->mbox_mem_pool);
4143 lpfc_disc_flush_list(vport);
4144 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4145 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4146 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004147 phba->link_state = LPFC_HBA_ERROR;
4148 }
4149 }
4150}
4151
4152/* Reg_vpi to tell firmware to resume normal operations */
4153void
4154lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4155{
4156 LPFC_MBOXQ_t *regvpimbox;
4157
4158 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4159 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004160 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004161 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4162 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004163 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004164 == MBX_NOT_FINISHED) {
4165 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004166 }
4167 }
4168}
4169
4170/* Start Link up / RSCN discovery on NPR nodes */
4171void
4172lpfc_disc_start(struct lpfc_vport *vport)
4173{
4174 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4175 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004176 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004177 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004178 int did_changed;
dea31012005-04-17 16:05:31 -05004179
James Smart2e0fef82007-06-17 19:56:36 -05004180 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004181 return;
James Smart2e0fef82007-06-17 19:56:36 -05004182
4183 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004184 clear_la_pending = 1;
4185 else
4186 clear_la_pending = 0;
4187
James Smart2e0fef82007-06-17 19:56:36 -05004188 if (vport->port_state < LPFC_VPORT_READY)
4189 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004190
James Smart2e0fef82007-06-17 19:56:36 -05004191 lpfc_set_disctmo(vport);
4192
4193 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004194 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004195 else
dea31012005-04-17 16:05:31 -05004196 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004197
4198 vport->fc_prevDID = vport->fc_myDID;
4199 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004200
4201 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004202 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4203 "0202 Start Discovery hba state x%x "
4204 "Data: x%x x%x x%x\n",
4205 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4206 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004207
4208 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004209 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004210
4211 if (num_sent)
4212 return;
4213
James Smart92d7f7b2007-06-17 19:56:38 -05004214 /*
4215 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
4216 * continue discovery.
4217 */
4218 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004219 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004220 !(vport->fc_flag & FC_RSCN_MODE) &&
4221 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004222 lpfc_issue_reg_vpi(phba, vport);
4223 return;
4224 }
James Smart2e0fef82007-06-17 19:56:36 -05004225
James Smart92d7f7b2007-06-17 19:56:38 -05004226 /*
4227 * For SLI2, we need to set port_state to READY and continue
4228 * discovery.
4229 */
4230 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4231 /* If we get here, there is nothing to ADISC */
4232 if (vport->port_type == LPFC_PHYSICAL_PORT)
4233 lpfc_issue_clear_la(phba, vport);
4234
4235 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004236 vport->num_disc_nodes = 0;
4237 /* go thru NPR nodes and issue ELS PLOGIs */
4238 if (vport->fc_npr_cnt)
4239 lpfc_els_disc_plogi(vport);
4240
4241 if (!vport->num_disc_nodes) {
4242 spin_lock_irq(shost->host_lock);
4243 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4244 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004245 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05004246 }
4247 }
James Smart92d7f7b2007-06-17 19:56:38 -05004248 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004249 } else {
4250 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004251 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05004252
4253 if (num_sent)
4254 return;
4255
James Smart2e0fef82007-06-17 19:56:36 -05004256 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05004257 /* Check to see if more RSCNs came in while we
4258 * were processing this one.
4259 */
James Smart2e0fef82007-06-17 19:56:36 -05004260 if ((vport->fc_rscn_id_cnt == 0) &&
4261 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
4262 spin_lock_irq(shost->host_lock);
4263 vport->fc_flag &= ~FC_RSCN_MODE;
4264 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004265 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004266 } else
James Smart2e0fef82007-06-17 19:56:36 -05004267 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05004268 }
4269 }
4270 return;
4271}
4272
4273/*
4274 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
4275 * ring the match the sppecified nodelist.
4276 */
4277static void
James Smart2e0fef82007-06-17 19:56:36 -05004278lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004279{
James Smart2534ba72007-04-25 09:52:20 -04004280 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004281 struct lpfc_sli *psli;
4282 IOCB_t *icmd;
4283 struct lpfc_iocbq *iocb, *next_iocb;
4284 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004285
4286 psli = &phba->sli;
4287 pring = &psli->ring[LPFC_ELS_RING];
4288
4289 /* Error matching iocb on txq or txcmplq
4290 * First check the txq.
4291 */
James Smart2e0fef82007-06-17 19:56:36 -05004292 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004293 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4294 if (iocb->context1 != ndlp) {
4295 continue;
4296 }
4297 icmd = &iocb->iocb;
4298 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
4299 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
4300
James Smart2534ba72007-04-25 09:52:20 -04004301 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05004302 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05004303 }
4304 }
4305
4306 /* Next check the txcmplq */
4307 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
4308 if (iocb->context1 != ndlp) {
4309 continue;
4310 }
4311 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004312 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
4313 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04004314 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05004315 }
4316 }
James Smart2e0fef82007-06-17 19:56:36 -05004317 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04004318
James Smarta257bf92009-04-06 18:48:10 -04004319 /* Cancel all the IOCBs from the completions list */
4320 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4321 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004322}
4323
Adrian Bunka6ababd2007-11-05 18:07:33 +01004324static void
James Smart2e0fef82007-06-17 19:56:36 -05004325lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004326{
4327 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05004328 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004329
James Smart2e0fef82007-06-17 19:56:36 -05004330 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
4331 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004332 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004333 if (!NLP_CHK_NODE_ACT(ndlp))
4334 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004335 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4336 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
4337 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04004338 }
dea31012005-04-17 16:05:31 -05004339 }
4340 }
dea31012005-04-17 16:05:31 -05004341}
4342
James Smart92d7f7b2007-06-17 19:56:38 -05004343void
4344lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
4345{
4346 lpfc_els_flush_rscn(vport);
4347 lpfc_els_flush_cmd(vport);
4348 lpfc_disc_flush_list(vport);
4349}
4350
dea31012005-04-17 16:05:31 -05004351/*****************************************************************************/
4352/*
4353 * NAME: lpfc_disc_timeout
4354 *
4355 * FUNCTION: Fibre Channel driver discovery timeout routine.
4356 *
4357 * EXECUTION ENVIRONMENT: interrupt only
4358 *
4359 * CALLED FROM:
4360 * Timer function
4361 *
4362 * RETURNS:
4363 * none
4364 */
4365/*****************************************************************************/
4366void
4367lpfc_disc_timeout(unsigned long ptr)
4368{
James Smart2e0fef82007-06-17 19:56:36 -05004369 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4370 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04004371 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004372 unsigned long flags = 0;
4373
4374 if (unlikely(!phba))
4375 return;
4376
James Smart5e9d9b82008-06-14 22:52:53 -04004377 spin_lock_irqsave(&vport->work_port_lock, flags);
4378 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4379 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05004380 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04004381 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05004382
James Smart5e9d9b82008-06-14 22:52:53 -04004383 if (!tmo_posted)
4384 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004385 return;
4386}
4387
4388static void
James Smart2e0fef82007-06-17 19:56:36 -05004389lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004390{
James Smart2e0fef82007-06-17 19:56:36 -05004391 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4392 struct lpfc_hba *phba = vport->phba;
4393 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004394 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05004395 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05004396 int rc, clrlaerr = 0;
4397
James Smart2e0fef82007-06-17 19:56:36 -05004398 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05004399 return;
4400
James Smart2e0fef82007-06-17 19:56:36 -05004401 spin_lock_irq(shost->host_lock);
4402 vport->fc_flag &= ~FC_DISC_TMO;
4403 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004404
James Smart858c9f62007-06-17 19:56:39 -05004405 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4406 "disc timeout: state:x%x rtry:x%x flg:x%x",
4407 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4408
James Smart2e0fef82007-06-17 19:56:36 -05004409 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05004410
4411 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05004412 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
4413 * FAN
4414 */
4415 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04004416 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4417 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004418 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05004419 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004420 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004421 if (!NLP_CHK_NODE_ACT(ndlp))
4422 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004423 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4424 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004425 if (ndlp->nlp_type & NLP_FABRIC) {
4426 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05004427 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004428
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004429 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004430 /* Fail outstanding IO now since device
4431 * is marked for PLOGI.
4432 */
James Smart2e0fef82007-06-17 19:56:36 -05004433 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004434 }
4435 }
James Smart92d7f7b2007-06-17 19:56:38 -05004436 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05004437 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05004438 return;
James Smart92d7f7b2007-06-17 19:56:38 -05004439 }
dea31012005-04-17 16:05:31 -05004440 break;
4441
James Smart92d7f7b2007-06-17 19:56:38 -05004442 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05004443 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05004444 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05004445 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04004446 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4447 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04004448 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05004449
4450 /* Assume no Fabric and go on with discovery.
4451 * Check for outstanding ELS FLOGI to abort.
4452 */
4453
4454 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05004455 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004456
4457 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05004458 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05004459 break;
4460
4461 case LPFC_FABRIC_CFG_LINK:
4462 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4463 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04004464 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4465 "0223 Timeout while waiting for "
4466 "NameServer login\n");
dea31012005-04-17 16:05:31 -05004467 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05004468 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05004469 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04004470 lpfc_els_abort(phba, ndlp);
4471
4472 /* ReStart discovery */
4473 goto restart_disc;
dea31012005-04-17 16:05:31 -05004474
4475 case LPFC_NS_QRY:
4476 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04004477 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4478 "0224 NameServer Query timeout "
4479 "Data: x%x x%x\n",
4480 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004481
James Smart92d7f7b2007-06-17 19:56:38 -05004482 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4483 /* Try it one more time */
4484 vport->fc_ns_retry++;
4485 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4486 vport->fc_ns_retry, 0);
4487 if (rc == 0)
4488 break;
dea31012005-04-17 16:05:31 -05004489 }
James Smart92d7f7b2007-06-17 19:56:38 -05004490 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05004491
James Smart87af33f2007-10-27 13:37:43 -04004492restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05004493 /*
4494 * Discovery is over.
4495 * set port_state to PORT_READY if SLI2.
4496 * cmpl_reg_vpi will set port_state to READY for SLI3.
4497 */
James Smart3772a992009-05-22 14:50:54 -04004498 if (phba->sli_rev < LPFC_SLI_REV4) {
4499 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4500 lpfc_issue_reg_vpi(phba, vport);
4501 else { /* NPIV Not enabled */
4502 lpfc_issue_clear_la(phba, vport);
4503 vport->port_state = LPFC_VPORT_READY;
4504 }
dea31012005-04-17 16:05:31 -05004505 }
4506
4507 /* Setup and issue mailbox INITIALIZE LINK command */
4508 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4509 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004510 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4511 "0206 Device Discovery "
4512 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004513 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004514 break;
4515 }
4516
4517 lpfc_linkdown(phba);
4518 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4519 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004520 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004521 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004522 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004523 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004524 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004525 if (rc == MBX_NOT_FINISHED)
4526 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4527
4528 break;
4529
4530 case LPFC_DISC_AUTH:
4531 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004532 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4533 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004534 lpfc_disc_flush_list(vport);
4535
James Smart92d7f7b2007-06-17 19:56:38 -05004536 /*
4537 * set port_state to PORT_READY if SLI2.
4538 * cmpl_reg_vpi will set port_state to READY for SLI3.
4539 */
James Smart3772a992009-05-22 14:50:54 -04004540 if (phba->sli_rev < LPFC_SLI_REV4) {
4541 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4542 lpfc_issue_reg_vpi(phba, vport);
4543 else { /* NPIV Not enabled */
4544 lpfc_issue_clear_la(phba, vport);
4545 vport->port_state = LPFC_VPORT_READY;
4546 }
dea31012005-04-17 16:05:31 -05004547 }
4548 break;
4549
James Smart2e0fef82007-06-17 19:56:36 -05004550 case LPFC_VPORT_READY:
4551 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004552 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4553 "0231 RSCN timeout Data: x%x "
4554 "x%x\n",
4555 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004556
4557 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004558 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004559
James Smart2e0fef82007-06-17 19:56:36 -05004560 lpfc_els_flush_rscn(vport);
4561 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004562 }
4563 break;
James Smart2e0fef82007-06-17 19:56:36 -05004564
James Smart92d7f7b2007-06-17 19:56:38 -05004565 default:
James Smarte8b62012007-08-02 11:10:09 -04004566 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004567 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004568 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004569 break;
4570 }
4571
4572 switch (phba->link_state) {
4573 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004574 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004575 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4576 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004577 clrlaerr = 1;
4578 break;
4579
James Smart09372822008-01-11 01:52:54 -05004580 case LPFC_LINK_UP:
4581 lpfc_issue_clear_la(phba, vport);
4582 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004583 case LPFC_LINK_UNKNOWN:
4584 case LPFC_WARM_START:
4585 case LPFC_INIT_START:
4586 case LPFC_INIT_MBX_CMDS:
4587 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004588 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004589 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4590 "0230 Unexpected timeout, hba link "
4591 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004592 clrlaerr = 1;
4593 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004594
4595 case LPFC_HBA_READY:
4596 break;
dea31012005-04-17 16:05:31 -05004597 }
4598
4599 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004600 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004601 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004602 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4603 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004604 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004605 }
4606
4607 return;
4608}
4609
dea31012005-04-17 16:05:31 -05004610/*
4611 * This routine handles processing a NameServer REG_LOGIN mailbox
4612 * command upon completion. It is setup in the LPFC_MBOXQ
4613 * as the completion routine when the command is
4614 * handed off to the SLI layer.
4615 */
4616void
James Smart2e0fef82007-06-17 19:56:36 -05004617lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004618{
James Smart04c68492009-05-22 14:52:52 -04004619 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004620 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4621 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4622 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004623
4624 pmb->context1 = NULL;
4625
dea31012005-04-17 16:05:31 -05004626 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004627 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004628 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004629 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004630
James Smart2e0fef82007-06-17 19:56:36 -05004631 /*
4632 * Start issuing Fabric-Device Management Interface (FDMI) command to
4633 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4634 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004635 */
James Smart2e0fef82007-06-17 19:56:36 -05004636
James Smart3de2a652007-08-02 11:09:59 -04004637 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004638 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4639 else
4640 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004641
James Smartfa4066b2008-01-11 01:53:27 -05004642 /* decrement the node reference count held for this callback
4643 * function.
4644 */
James Smart329f9bc2007-04-25 09:53:01 -04004645 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004646 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4647 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004648 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004649
4650 return;
4651}
4652
James Smart685f0bf2007-04-25 09:53:08 -04004653static int
4654lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4655{
4656 uint16_t *rpi = param;
4657
4658 return ndlp->nlp_rpi == *rpi;
4659}
4660
4661static int
4662lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4663{
4664 return memcmp(&ndlp->nlp_portname, param,
4665 sizeof(ndlp->nlp_portname)) == 0;
4666}
4667
Adrian Bunka6ababd2007-11-05 18:07:33 +01004668static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004669__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004670{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004671 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004672
James Smart2e0fef82007-06-17 19:56:36 -05004673 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004674 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004675 return ndlp;
4676 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004677 return NULL;
dea31012005-04-17 16:05:31 -05004678}
4679
James Smart685f0bf2007-04-25 09:53:08 -04004680/*
4681 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004682 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004683 */
4684struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004685__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004686{
James Smart2e0fef82007-06-17 19:56:36 -05004687 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004688}
4689
James Smart488d1462006-03-07 15:02:37 -05004690/*
James Smart685f0bf2007-04-25 09:53:08 -04004691 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004692 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004693 */
4694struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004695lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004696{
James Smart2e0fef82007-06-17 19:56:36 -05004697 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004698 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004699
James Smart2e0fef82007-06-17 19:56:36 -05004700 spin_lock_irq(shost->host_lock);
4701 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4702 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004703 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004704}
4705
dea31012005-04-17 16:05:31 -05004706void
James Smart2e0fef82007-06-17 19:56:36 -05004707lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4708 uint32_t did)
dea31012005-04-17 16:05:31 -05004709{
4710 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004711
4712 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004713 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004714
4715 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4716 "node init: did:x%x",
4717 ndlp->nlp_DID, 0, 0);
4718
dea31012005-04-17 16:05:31 -05004719 return;
4720}
James Smart329f9bc2007-04-25 09:53:01 -04004721
James Smart98c9ea52007-10-27 13:37:33 -04004722/* This routine releases all resources associated with a specifc NPort's ndlp
4723 * and mempool_free's the nodelist.
4724 */
James Smart311464e2007-08-02 11:10:37 -04004725static void
James Smart329f9bc2007-04-25 09:53:01 -04004726lpfc_nlp_release(struct kref *kref)
4727{
James Smarte47c9092008-02-08 18:49:26 -05004728 struct lpfc_hba *phba;
4729 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004730 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4731 kref);
James Smart858c9f62007-06-17 19:56:39 -05004732
4733 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4734 "node release: did:x%x flg:x%x type:x%x",
4735 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4736
James Smarte47c9092008-02-08 18:49:26 -05004737 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4738 "0279 lpfc_nlp_release: ndlp:x%p "
4739 "usgmap:x%x refcnt:%d\n",
4740 (void *)ndlp, ndlp->nlp_usg_map,
4741 atomic_read(&ndlp->kref.refcount));
4742
4743 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004744 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004745
4746 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004747 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004748 spin_lock_irqsave(&phba->ndlp_lock, flags);
4749 NLP_CLR_NODE_ACT(ndlp);
4750 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4751
4752 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004753 if (NLP_CHK_FREE_REQ(ndlp)) {
4754 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004755 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004756 }
James Smart329f9bc2007-04-25 09:53:01 -04004757}
4758
James Smart98c9ea52007-10-27 13:37:33 -04004759/* This routine bumps the reference count for a ndlp structure to ensure
4760 * that one discovery thread won't free a ndlp while another discovery thread
4761 * is using it.
4762 */
James Smart329f9bc2007-04-25 09:53:01 -04004763struct lpfc_nodelist *
4764lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4765{
James Smarte47c9092008-02-08 18:49:26 -05004766 struct lpfc_hba *phba;
4767 unsigned long flags;
4768
James Smart98c9ea52007-10-27 13:37:33 -04004769 if (ndlp) {
4770 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4771 "node get: did:x%x flg:x%x refcnt:x%x",
4772 ndlp->nlp_DID, ndlp->nlp_flag,
4773 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004774 /* The check of ndlp usage to prevent incrementing the
4775 * ndlp reference count that is in the process of being
4776 * released.
4777 */
James Smarta257bf92009-04-06 18:48:10 -04004778 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004779 spin_lock_irqsave(&phba->ndlp_lock, flags);
4780 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4781 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4782 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4783 "0276 lpfc_nlp_get: ndlp:x%p "
4784 "usgmap:x%x refcnt:%d\n",
4785 (void *)ndlp, ndlp->nlp_usg_map,
4786 atomic_read(&ndlp->kref.refcount));
4787 return NULL;
4788 } else
4789 kref_get(&ndlp->kref);
4790 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004791 }
James Smart329f9bc2007-04-25 09:53:01 -04004792 return ndlp;
4793}
4794
James Smart98c9ea52007-10-27 13:37:33 -04004795/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004796 * count goes to 0, this indicates the the associated nodelist should be
4797 * freed. Returning 1 indicates the ndlp resource has been released; on the
4798 * other hand, returning 0 indicates the ndlp resource has not been released
4799 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004800 */
James Smart329f9bc2007-04-25 09:53:01 -04004801int
4802lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4803{
James Smarte47c9092008-02-08 18:49:26 -05004804 struct lpfc_hba *phba;
4805 unsigned long flags;
4806
4807 if (!ndlp)
4808 return 1;
4809
4810 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4811 "node put: did:x%x flg:x%x refcnt:x%x",
4812 ndlp->nlp_DID, ndlp->nlp_flag,
4813 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004814 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004815 spin_lock_irqsave(&phba->ndlp_lock, flags);
4816 /* Check the ndlp memory free acknowledge flag to avoid the
4817 * possible race condition that kref_put got invoked again
4818 * after previous one has done ndlp memory free.
4819 */
4820 if (NLP_CHK_FREE_ACK(ndlp)) {
4821 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4822 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4823 "0274 lpfc_nlp_put: ndlp:x%p "
4824 "usgmap:x%x refcnt:%d\n",
4825 (void *)ndlp, ndlp->nlp_usg_map,
4826 atomic_read(&ndlp->kref.refcount));
4827 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004828 }
James Smarte47c9092008-02-08 18:49:26 -05004829 /* Check the ndlp inactivate log flag to avoid the possible
4830 * race condition that kref_put got invoked again after ndlp
4831 * is already in inactivating state.
4832 */
4833 if (NLP_CHK_IACT_REQ(ndlp)) {
4834 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4835 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4836 "0275 lpfc_nlp_put: ndlp:x%p "
4837 "usgmap:x%x refcnt:%d\n",
4838 (void *)ndlp, ndlp->nlp_usg_map,
4839 atomic_read(&ndlp->kref.refcount));
4840 return 1;
4841 }
4842 /* For last put, mark the ndlp usage flags to make sure no
4843 * other kref_get and kref_put on the same ndlp shall get
4844 * in between the process when the final kref_put has been
4845 * invoked on this ndlp.
4846 */
4847 if (atomic_read(&ndlp->kref.refcount) == 1) {
4848 /* Indicate ndlp is put to inactive state. */
4849 NLP_SET_IACT_REQ(ndlp);
4850 /* Acknowledge ndlp memory free has been seen. */
4851 if (NLP_CHK_FREE_REQ(ndlp))
4852 NLP_SET_FREE_ACK(ndlp);
4853 }
4854 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4855 /* Note, the kref_put returns 1 when decrementing a reference
4856 * count that was 1, it invokes the release callback function,
4857 * but it still left the reference count as 1 (not actually
4858 * performs the last decrementation). Otherwise, it actually
4859 * decrements the reference count and returns 0.
4860 */
4861 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004862}
James Smart98c9ea52007-10-27 13:37:33 -04004863
4864/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004865 * by any other discovery thread. This routine returns 1 if the
4866 * ndlp has been freed. A return value of 0 indicates the ndlp is
4867 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004868 */
4869int
4870lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4871{
4872 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4873 "node not used: did:x%x flg:x%x refcnt:x%x",
4874 ndlp->nlp_DID, ndlp->nlp_flag,
4875 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004876 if (atomic_read(&ndlp->kref.refcount) == 1)
4877 if (lpfc_nlp_put(ndlp))
4878 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004879 return 0;
4880}
James Smart6fb120a2009-05-22 14:52:59 -04004881
4882/**
4883 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4884 * @phba: Pointer to hba context object.
4885 *
4886 * This function iterate through all FC nodes associated
4887 * will all vports to check if there is any node with
4888 * fc_rports associated with it. If there is an fc_rport
4889 * associated with the node, then the node is either in
4890 * discovered state or its devloss_timer is pending.
4891 */
4892static int
4893lpfc_fcf_inuse(struct lpfc_hba *phba)
4894{
4895 struct lpfc_vport **vports;
4896 int i, ret = 0;
4897 struct lpfc_nodelist *ndlp;
4898 struct Scsi_Host *shost;
4899
4900 vports = lpfc_create_vport_work_array(phba);
4901
4902 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4903 shost = lpfc_shost_from_vport(vports[i]);
4904 spin_lock_irq(shost->host_lock);
4905 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4906 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4907 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4908 ret = 1;
4909 spin_unlock_irq(shost->host_lock);
4910 goto out;
James Smart891478a2009-11-18 15:40:23 -05004911 } else {
4912 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4913 "2624 RPI %x DID %x flg %x still "
4914 "logged in\n",
4915 ndlp->nlp_rpi, ndlp->nlp_DID,
4916 ndlp->nlp_flag);
4917 if (ndlp->nlp_flag & NLP_RPI_VALID)
4918 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04004919 }
4920 }
4921 spin_unlock_irq(shost->host_lock);
4922 }
4923out:
4924 lpfc_destroy_vport_work_array(phba, vports);
4925 return ret;
4926}
4927
4928/**
4929 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4930 * @phba: Pointer to hba context object.
4931 * @mboxq: Pointer to mailbox object.
4932 *
4933 * This function frees memory associated with the mailbox command.
4934 */
4935static void
4936lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4937{
4938 struct lpfc_vport *vport = mboxq->vport;
4939
4940 if (mboxq->u.mb.mbxStatus) {
4941 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4942 "2555 UNREG_VFI mbxStatus error x%x "
4943 "HBA state x%x\n",
4944 mboxq->u.mb.mbxStatus, vport->port_state);
4945 }
4946 mempool_free(mboxq, phba->mbox_mem_pool);
4947 return;
4948}
4949
4950/**
4951 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4952 * @phba: Pointer to hba context object.
4953 * @mboxq: Pointer to mailbox object.
4954 *
4955 * This function frees memory associated with the mailbox command.
4956 */
4957static void
4958lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4959{
4960 struct lpfc_vport *vport = mboxq->vport;
4961
4962 if (mboxq->u.mb.mbxStatus) {
4963 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4964 "2550 UNREG_FCFI mbxStatus error x%x "
4965 "HBA state x%x\n",
4966 mboxq->u.mb.mbxStatus, vport->port_state);
4967 }
4968 mempool_free(mboxq, phba->mbox_mem_pool);
4969 return;
4970}
4971
4972/**
James Smartecfd03c2010-02-12 14:41:27 -05004973 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04004974 * @phba: Pointer to hba context object.
4975 *
James Smartecfd03c2010-02-12 14:41:27 -05004976 * This function prepare the HBA for unregistering the currently registered
4977 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
4978 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04004979 */
James Smartecfd03c2010-02-12 14:41:27 -05004980int
4981lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04004982{
4983 LPFC_MBOXQ_t *mbox;
James Smart6fb120a2009-05-22 14:52:59 -04004984 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05004985 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05004986 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05004987 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04004988
James Smartecfd03c2010-02-12 14:41:27 -05004989 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04004990 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05004991 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04004992
James Smart4d9ab992009-10-02 15:16:39 -04004993 /* At this point, all discovery is aborted */
4994 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04004995
4996 /* Unregister VPIs */
4997 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05004998 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04004999 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05005000 /* Stop FLOGI/FDISC retries */
5001 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5002 if (ndlp)
5003 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005004 lpfc_cleanup_pending_mbox(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04005005 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05005006 shost = lpfc_shost_from_vport(vports[i]);
5007 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05005008 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05005009 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005010 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005011 }
5012 lpfc_destroy_vport_work_array(phba, vports);
5013
James Smart695a8142010-01-26 23:08:03 -05005014 /* Cleanup any outstanding ELS commands */
5015 lpfc_els_flush_all_cmd(phba);
5016
James Smart6fb120a2009-05-22 14:52:59 -04005017 /* Unregister VFI */
5018 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5019 if (!mbox) {
5020 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005021 "2556 UNREG_VFI mbox allocation failed"
5022 "HBA state x%x\n", phba->pport->port_state);
5023 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005024 }
5025
James Smart6669f9b2009-10-02 15:16:45 -04005026 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04005027 mbox->vport = phba->pport;
5028 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
5029
5030 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5031 if (rc == MBX_NOT_FINISHED) {
5032 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005033 "2557 UNREG_VFI issue mbox failed rc x%x "
5034 "HBA state x%x\n",
5035 rc, phba->pport->port_state);
James Smart6fb120a2009-05-22 14:52:59 -04005036 mempool_free(mbox, phba->mbox_mem_pool);
James Smartecfd03c2010-02-12 14:41:27 -05005037 return -EIO;
James Smart6fb120a2009-05-22 14:52:59 -04005038 }
5039
James Smart72100cc2010-02-12 14:43:01 -05005040 shost = lpfc_shost_from_vport(phba->pport);
5041 spin_lock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005042 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005043 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005044
James Smartecfd03c2010-02-12 14:41:27 -05005045 return 0;
5046}
5047
5048/**
5049 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5050 * @phba: Pointer to hba context object.
5051 *
5052 * This function issues synchronous unregister FCF mailbox command to HBA to
5053 * unregister the currently registered FCF record. The driver does not reset
5054 * the driver FCF usage state flags.
5055 *
5056 * Return 0 if successfully issued, none-zero otherwise.
5057 */
5058int
5059lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5060{
5061 LPFC_MBOXQ_t *mbox;
5062 int rc;
5063
James Smart6fb120a2009-05-22 14:52:59 -04005064 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5065 if (!mbox) {
5066 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005067 "2551 UNREG_FCFI mbox allocation failed"
5068 "HBA state x%x\n", phba->pport->port_state);
5069 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005070 }
James Smart6fb120a2009-05-22 14:52:59 -04005071 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5072 mbox->vport = phba->pport;
5073 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5074 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5075
5076 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005077 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5078 "2552 Unregister FCFI command failed rc x%x "
5079 "HBA state x%x\n",
5080 rc, phba->pport->port_state);
5081 return -EINVAL;
5082 }
5083 return 0;
5084}
5085
5086/**
5087 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5088 * @phba: Pointer to hba context object.
5089 *
5090 * This function unregisters the currently reigstered FCF. This function
5091 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5092 */
5093void
5094lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5095{
5096 int rc;
5097
5098 /* Preparation for unregistering fcf */
5099 rc = lpfc_unregister_fcf_prep(phba);
5100 if (rc) {
5101 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5102 "2748 Failed to prepare for unregistering "
5103 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005104 return;
5105 }
5106
James Smartecfd03c2010-02-12 14:41:27 -05005107 /* Now, unregister FCF record and reset HBA FCF state */
5108 rc = lpfc_sli4_unregister_fcf(phba);
5109 if (rc)
5110 return;
5111 /* Reset HBA FCF states after successful unregister FCF */
5112 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005113 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005114
5115 /*
5116 * If driver is not unloading, check if there is any other
5117 * FCF record that can be used for discovery.
5118 */
5119 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005120 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005121 return;
5122
James Smart0c9ab6f2010-02-26 14:15:57 -05005123 /* This is considered as the initial FCF discovery scan */
5124 spin_lock_irq(&phba->hbalock);
5125 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5126 spin_unlock_irq(&phba->hbalock);
5127 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005128
James Smart0c9ab6f2010-02-26 14:15:57 -05005129 if (rc) {
5130 spin_lock_irq(&phba->hbalock);
5131 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5132 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005133 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005134 "2553 lpfc_unregister_unused_fcf failed "
5135 "to read FCF record HBA state x%x\n",
5136 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005137 }
James Smartecfd03c2010-02-12 14:41:27 -05005138}
5139
5140/**
5141 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5142 * @phba: Pointer to hba context object.
5143 *
5144 * This function just unregisters the currently reigstered FCF. It does not
5145 * try to find another FCF for discovery.
5146 */
5147void
5148lpfc_unregister_fcf(struct lpfc_hba *phba)
5149{
5150 int rc;
5151
5152 /* Preparation for unregistering fcf */
5153 rc = lpfc_unregister_fcf_prep(phba);
5154 if (rc) {
5155 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5156 "2749 Failed to prepare for unregistering "
5157 "HBA's FCF record: rc=%d\n", rc);
5158 return;
5159 }
5160
5161 /* Now, unregister FCF record and reset HBA FCF state */
5162 rc = lpfc_sli4_unregister_fcf(phba);
5163 if (rc)
5164 return;
5165 /* Set proper HBA FCF states after successful unregister FCF */
5166 spin_lock_irq(&phba->hbalock);
5167 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
5168 spin_unlock_irq(&phba->hbalock);
5169}
5170
5171/**
5172 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
5173 * @phba: Pointer to hba context object.
5174 *
5175 * This function check if there are any connected remote port for the FCF and
5176 * if all the devices are disconnected, this function unregister FCFI.
5177 * This function also tries to use another FCF for discovery.
5178 */
5179void
5180lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
5181{
5182 /*
5183 * If HBA is not running in FIP mode or if HBA does not support
5184 * FCoE or if FCF is not registered, do nothing.
5185 */
5186 spin_lock_irq(&phba->hbalock);
5187 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
5188 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
5189 !(phba->hba_flag & HBA_FIP_SUPPORT)) {
5190 spin_unlock_irq(&phba->hbalock);
5191 return;
5192 }
5193 spin_unlock_irq(&phba->hbalock);
5194
5195 if (lpfc_fcf_inuse(phba))
5196 return;
5197
5198 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005199}
5200
5201/**
5202 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
5203 * @phba: Pointer to hba context object.
5204 * @buff: Buffer containing the FCF connection table as in the config
5205 * region.
5206 * This function create driver data structure for the FCF connection
5207 * record table read from config region 23.
5208 */
5209static void
5210lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
5211 uint8_t *buff)
5212{
5213 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5214 struct lpfc_fcf_conn_hdr *conn_hdr;
5215 struct lpfc_fcf_conn_rec *conn_rec;
5216 uint32_t record_count;
5217 int i;
5218
5219 /* Free the current connect table */
5220 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005221 &phba->fcf_conn_rec_list, list) {
5222 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04005223 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005224 }
James Smart6fb120a2009-05-22 14:52:59 -04005225
5226 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
5227 record_count = conn_hdr->length * sizeof(uint32_t)/
5228 sizeof(struct lpfc_fcf_conn_rec);
5229
5230 conn_rec = (struct lpfc_fcf_conn_rec *)
5231 (buff + sizeof(struct lpfc_fcf_conn_hdr));
5232
5233 for (i = 0; i < record_count; i++) {
5234 if (!(conn_rec[i].flags & FCFCNCT_VALID))
5235 continue;
5236 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
5237 GFP_KERNEL);
5238 if (!conn_entry) {
5239 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5240 "2566 Failed to allocate connection"
5241 " table entry\n");
5242 return;
5243 }
5244
5245 memcpy(&conn_entry->conn_rec, &conn_rec[i],
5246 sizeof(struct lpfc_fcf_conn_rec));
5247 conn_entry->conn_rec.vlan_tag =
5248 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
5249 conn_entry->conn_rec.flags =
5250 le16_to_cpu(conn_entry->conn_rec.flags);
5251 list_add_tail(&conn_entry->list,
5252 &phba->fcf_conn_rec_list);
5253 }
5254}
5255
5256/**
5257 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
5258 * @phba: Pointer to hba context object.
5259 * @buff: Buffer containing the FCoE parameter data structure.
5260 *
5261 * This function update driver data structure with config
5262 * parameters read from config region 23.
5263 */
5264static void
5265lpfc_read_fcoe_param(struct lpfc_hba *phba,
5266 uint8_t *buff)
5267{
5268 struct lpfc_fip_param_hdr *fcoe_param_hdr;
5269 struct lpfc_fcoe_params *fcoe_param;
5270
5271 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
5272 buff;
5273 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04005274 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04005275
5276 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
5277 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
5278 return;
5279
James Smart6fb120a2009-05-22 14:52:59 -04005280 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
5281 phba->valid_vlan = 1;
5282 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
5283 0xFFF;
5284 }
5285
5286 phba->fc_map[0] = fcoe_param->fc_map[0];
5287 phba->fc_map[1] = fcoe_param->fc_map[1];
5288 phba->fc_map[2] = fcoe_param->fc_map[2];
5289 return;
5290}
5291
5292/**
5293 * lpfc_get_rec_conf23 - Get a record type in config region data.
5294 * @buff: Buffer containing config region 23 data.
5295 * @size: Size of the data buffer.
5296 * @rec_type: Record type to be searched.
5297 *
5298 * This function searches config region data to find the begining
5299 * of the record specified by record_type. If record found, this
5300 * function return pointer to the record else return NULL.
5301 */
5302static uint8_t *
5303lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
5304{
5305 uint32_t offset = 0, rec_length;
5306
5307 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
5308 (size < sizeof(uint32_t)))
5309 return NULL;
5310
5311 rec_length = buff[offset + 1];
5312
5313 /*
5314 * One TLV record has one word header and number of data words
5315 * specified in the rec_length field of the record header.
5316 */
5317 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
5318 <= size) {
5319 if (buff[offset] == rec_type)
5320 return &buff[offset];
5321
5322 if (buff[offset] == LPFC_REGION23_LAST_REC)
5323 return NULL;
5324
5325 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
5326 rec_length = buff[offset + 1];
5327 }
5328 return NULL;
5329}
5330
5331/**
5332 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
5333 * @phba: Pointer to lpfc_hba data structure.
5334 * @buff: Buffer containing config region 23 data.
5335 * @size: Size of the data buffer.
5336 *
5337 * This fuction parse the FCoE config parameters in config region 23 and
5338 * populate driver data structure with the parameters.
5339 */
5340void
5341lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
5342 uint8_t *buff,
5343 uint32_t size)
5344{
5345 uint32_t offset = 0, rec_length;
5346 uint8_t *rec_ptr;
5347
5348 /*
5349 * If data size is less than 2 words signature and version cannot be
5350 * verified.
5351 */
5352 if (size < 2*sizeof(uint32_t))
5353 return;
5354
5355 /* Check the region signature first */
5356 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
5357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5358 "2567 Config region 23 has bad signature\n");
5359 return;
5360 }
5361
5362 offset += 4;
5363
5364 /* Check the data structure version */
5365 if (buff[offset] != LPFC_REGION23_VERSION) {
5366 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5367 "2568 Config region 23 has bad version\n");
5368 return;
5369 }
5370 offset += 4;
5371
5372 rec_length = buff[offset + 1];
5373
5374 /* Read FCoE param record */
5375 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5376 size - offset, FCOE_PARAM_TYPE);
5377 if (rec_ptr)
5378 lpfc_read_fcoe_param(phba, rec_ptr);
5379
5380 /* Read FCF connection table */
5381 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5382 size - offset, FCOE_CONN_TBL_TYPE);
5383 if (rec_ptr)
5384 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5385
5386}