blob: 362730b6dd85641ddb0f28fcde37666b7a2a316f [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;
dea31012005-04-17 16:05:31 -0500478 }
479 if (free_evt)
480 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500481 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500482 }
James Smart2e0fef82007-06-17 19:56:36 -0500483 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500484
485}
486
James Smart311464e2007-08-02 11:10:37 -0400487static void
James Smart2e0fef82007-06-17 19:56:36 -0500488lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500489{
490 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500491 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400492 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400493 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400494 int i;
dea31012005-04-17 16:05:31 -0500495
James Smart2e0fef82007-06-17 19:56:36 -0500496 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500497 ha_copy = phba->work_ha;
498 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500499 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500500
James Smartda0436e2009-05-22 14:51:39 -0400501 /* First, try to post the next mailbox command to SLI4 device */
502 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
503 lpfc_sli4_post_async_mbox(phba);
504
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500505 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400506 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500507 lpfc_handle_eratt(phba);
508
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500509 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500510 lpfc_sli_handle_mb_event(phba);
511
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500512 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500513 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400514
James Smartda0436e2009-05-22 14:51:39 -0400515 /* Process SLI4 events */
516 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
517 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
518 lpfc_sli4_fcp_xri_abort_event_proc(phba);
519 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
520 lpfc_sli4_els_xri_abort_event_proc(phba);
521 if (phba->hba_flag & ASYNC_EVENT)
522 lpfc_sli4_async_event_proc(phba);
523 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
524 spin_lock_irq(&phba->hbalock);
525 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
526 spin_unlock_irq(&phba->hbalock);
527 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
528 }
James Smartecfd03c2010-02-12 14:41:27 -0500529 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
530 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400531 }
532
James Smart549e55c2007-08-02 11:09:51 -0400533 vports = lpfc_create_vport_work_array(phba);
534 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400535 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400536 /*
537 * We could have no vports in array if unloading, so if
538 * this happens then just use the pport
539 */
540 if (vports[i] == NULL && i == 0)
541 vport = phba->pport;
542 else
543 vport = vports[i];
544 if (vport == NULL)
545 break;
James Smart58da1ff2008-04-07 10:15:56 -0400546 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400547 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400548 vport->work_port_events &= ~work_port_events;
549 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400550 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400551 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400552 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400553 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400554 if (work_port_events & WORKER_HB_TMO)
555 lpfc_hb_timeout_handler(phba);
556 if (work_port_events & WORKER_MBOX_TMO)
557 lpfc_mbox_timeout_handler(phba);
558 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
559 lpfc_unblock_fabric_iocbs(phba);
560 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400561 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400562 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
563 lpfc_ramp_down_queue_handler(phba);
564 if (work_port_events & WORKER_RAMP_UP_QUEUE)
565 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500566 }
James Smart09372822008-01-11 01:52:54 -0500567 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500568
James Smart858c9f62007-06-17 19:56:39 -0500569 pring = &phba->sli.ring[LPFC_ELS_RING];
570 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
571 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400572 if ((status & HA_RXMASK) ||
573 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400574 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400575 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500576 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400577 /* Set the lpfc data pending flag */
578 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500579 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400580 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500581 lpfc_sli_handle_slow_ring_event(phba, pring,
582 (status &
583 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500584 }
585 /*
586 * Turn on Ring interrupts
587 */
James Smart3772a992009-05-22 14:50:54 -0400588 if (phba->sli_rev <= LPFC_SLI_REV3) {
589 spin_lock_irq(&phba->hbalock);
590 control = readl(phba->HCregaddr);
591 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
592 lpfc_debugfs_slow_ring_trc(phba,
593 "WRK Enable ring: cntl:x%x hacopy:x%x",
594 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400595
James Smart3772a992009-05-22 14:50:54 -0400596 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
597 writel(control, phba->HCregaddr);
598 readl(phba->HCregaddr); /* flush */
599 } else {
600 lpfc_debugfs_slow_ring_trc(phba,
601 "WRK Ring ok: cntl:x%x hacopy:x%x",
602 control, ha_copy, 0);
603 }
604 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500605 }
606 }
James Smart2e0fef82007-06-17 19:56:36 -0500607 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500608}
609
dea31012005-04-17 16:05:31 -0500610int
611lpfc_do_work(void *p)
612{
613 struct lpfc_hba *phba = p;
614 int rc;
dea31012005-04-17 16:05:31 -0500615
616 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400617 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500618
James Smart3a55b532008-12-04 22:38:54 -0500619 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400620 /* wait and check worker queue activities */
621 rc = wait_event_interruptible(phba->work_waitq,
622 (test_and_clear_bit(LPFC_DATA_READY,
623 &phba->data_flags)
624 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500625 /* Signal wakeup shall terminate the worker thread */
626 if (rc) {
627 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
628 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500629 break;
James Smart3a55b532008-12-04 22:38:54 -0500630 }
dea31012005-04-17 16:05:31 -0500631
James Smart5e9d9b82008-06-14 22:52:53 -0400632 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500633 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500634 }
James Smart3a55b532008-12-04 22:38:54 -0500635 phba->worker_thread = NULL;
636 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
637 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500638 return 0;
639}
640
641/*
642 * This is only called to handle FC worker events. Since this a rare
643 * occurance, we allocate a struct lpfc_work_evt structure here instead of
644 * embedding it in the IOCB.
645 */
646int
James Smart2e0fef82007-06-17 19:56:36 -0500647lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500648 uint32_t evt)
649{
650 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500651 unsigned long flags;
dea31012005-04-17 16:05:31 -0500652
653 /*
654 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
655 * be queued to worker thread for processing
656 */
James Smart92d7f7b2007-06-17 19:56:38 -0500657 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500658 if (!evtp)
659 return 0;
660
661 evtp->evt_arg1 = arg1;
662 evtp->evt_arg2 = arg2;
663 evtp->evt = evt;
664
James Smarted957682007-06-17 19:56:37 -0500665 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400666 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500667 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500668
James Smart5e9d9b82008-06-14 22:52:53 -0400669 lpfc_worker_wake_up(phba);
670
dea31012005-04-17 16:05:31 -0500671 return 1;
672}
673
James Smart92d7f7b2007-06-17 19:56:38 -0500674void
675lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
676{
James Smart09372822008-01-11 01:52:54 -0500677 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500678 struct lpfc_hba *phba = vport->phba;
679 struct lpfc_nodelist *ndlp, *next_ndlp;
680 int rc;
681
682 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500683 if (!NLP_CHK_NODE_ACT(ndlp))
684 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500685 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
686 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400687 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
688 ((vport->port_type == LPFC_NPIV_PORT) &&
689 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500690 lpfc_unreg_rpi(vport, ndlp);
691
692 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400693 if ((phba->sli_rev < LPFC_SLI_REV4) &&
694 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500695 continue;
696 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
697 remove
698 ? NLP_EVT_DEVICE_RM
699 : NLP_EVT_DEVICE_RECOVERY);
700 }
701 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
702 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500703 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500704 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500705 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500706 }
707}
708
James Smart87af33f2007-10-27 13:37:43 -0400709void
James Smart98c9ea52007-10-27 13:37:33 -0400710lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500711{
James Smart695a8142010-01-26 23:08:03 -0500712 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
713
James Smart45ed1192009-10-02 15:17:02 -0400714 /* Cleanup any outstanding received buffers */
715 lpfc_cleanup_rcv_buffers(vport);
716
James Smart92d7f7b2007-06-17 19:56:38 -0500717 /* Cleanup any outstanding RSCN activity */
718 lpfc_els_flush_rscn(vport);
719
720 /* Cleanup any outstanding ELS commands */
721 lpfc_els_flush_cmd(vport);
722
723 lpfc_cleanup_rpis(vport, 0);
724
James Smart92d7f7b2007-06-17 19:56:38 -0500725 /* Turn off discovery timer if its running */
726 lpfc_can_disctmo(vport);
727}
728
James Smart3772a992009-05-22 14:50:54 -0400729void
James Smart98c9ea52007-10-27 13:37:33 -0400730lpfc_linkdown_port(struct lpfc_vport *vport)
731{
732 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
733
734 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
735
736 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
737 "Link Down: state:x%x rtry:x%x flg:x%x",
738 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
739
740 lpfc_port_link_failure(vport);
741
742}
743
dea31012005-04-17 16:05:31 -0500744int
James Smart685f0bf2007-04-25 09:53:08 -0400745lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500746{
James Smart2e0fef82007-06-17 19:56:36 -0500747 struct lpfc_vport *vport = phba->pport;
748 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400749 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400750 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400751 int i;
dea31012005-04-17 16:05:31 -0500752
James Smart3163f722008-02-08 18:50:25 -0500753 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500754 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500755
756 /* Block all SCSI stack I/Os */
757 lpfc_scsi_dev_block(phba);
758
James Smart2e0fef82007-06-17 19:56:36 -0500759 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500760 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500761 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500762 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500763 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500764 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500765 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500766 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500767 }
James Smart549e55c2007-08-02 11:09:51 -0400768 vports = lpfc_create_vport_work_array(phba);
769 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400770 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400771 /* Issue a LINK DOWN event to all nodes */
772 lpfc_linkdown_port(vports[i]);
773 }
James Smart09372822008-01-11 01:52:54 -0500774 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500775 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500776 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
777 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500778 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500779 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500780 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400781 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500782 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500783 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500784 }
785 }
786
dea31012005-04-17 16:05:31 -0500787 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500788 if (phba->pport->fc_flag & FC_PT2PT) {
789 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500790 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
791 if (mb) {
dea31012005-04-17 16:05:31 -0500792 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500793 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500794 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400795 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500796 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500797 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500798 }
799 }
James Smart2e0fef82007-06-17 19:56:36 -0500800 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500801 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500802 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500803 }
James Smart2e0fef82007-06-17 19:56:36 -0500804
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500805 return 0;
dea31012005-04-17 16:05:31 -0500806}
807
James Smart92d7f7b2007-06-17 19:56:38 -0500808static void
809lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500810{
James Smart92d7f7b2007-06-17 19:56:38 -0500811 struct lpfc_nodelist *ndlp;
812
813 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500814 if (!NLP_CHK_NODE_ACT(ndlp))
815 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500816 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
817 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500818 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500819 /* On Linkup its safe to clean up the ndlp
820 * from Fabric connections.
821 */
James Smart92d7f7b2007-06-17 19:56:38 -0500822 if (ndlp->nlp_DID != Fabric_DID)
823 lpfc_unreg_rpi(vport, ndlp);
824 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
825 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500826 /* Fail outstanding IO now since device is
827 * marked for PLOGI.
828 */
James Smart92d7f7b2007-06-17 19:56:38 -0500829 lpfc_unreg_rpi(vport, ndlp);
830 }
831 }
832}
833
834static void
835lpfc_linkup_port(struct lpfc_vport *vport)
836{
837 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500838 struct lpfc_hba *phba = vport->phba;
839
840 if ((vport->load_flag & FC_UNLOADING) != 0)
841 return;
842
James Smart858c9f62007-06-17 19:56:39 -0500843 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
844 "Link Up: top:x%x speed:x%x flg:x%x",
845 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
846
James Smart92d7f7b2007-06-17 19:56:38 -0500847 /* If NPIV is not enabled, only bring the physical port up */
848 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
849 (vport != phba->pport))
850 return;
dea31012005-04-17 16:05:31 -0500851
James Smart2e0fef82007-06-17 19:56:36 -0500852 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400853
James Smart2e0fef82007-06-17 19:56:36 -0500854 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500855 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
856 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
857 vport->fc_flag |= FC_NDISC_ACTIVE;
858 vport->fc_ns_retry = 0;
859 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500860
James Smart92d7f7b2007-06-17 19:56:38 -0500861 if (vport->fc_flag & FC_LBIT)
862 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500863
James Smart92d7f7b2007-06-17 19:56:38 -0500864}
865
866static int
867lpfc_linkup(struct lpfc_hba *phba)
868{
James Smart549e55c2007-08-02 11:09:51 -0400869 struct lpfc_vport **vports;
870 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500871
872 phba->link_state = LPFC_LINK_UP;
873
874 /* Unblock fabric iocbs if they are blocked */
875 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
876 del_timer_sync(&phba->fabric_block_timer);
877
James Smart549e55c2007-08-02 11:09:51 -0400878 vports = lpfc_create_vport_work_array(phba);
879 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400880 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400881 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500882 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400883 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
884 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500885 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500886
887 return 0;
888}
889
890/*
891 * This routine handles processing a CLEAR_LA mailbox
892 * command upon completion. It is setup in the LPFC_MBOXQ
893 * as the completion routine when the command is
894 * handed off to the SLI layer.
895 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100896static void
James Smart2e0fef82007-06-17 19:56:36 -0500897lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500898{
James Smart2e0fef82007-06-17 19:56:36 -0500899 struct lpfc_vport *vport = pmb->vport;
900 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
901 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400902 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500903 uint32_t control;
904
dea31012005-04-17 16:05:31 -0500905 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500906 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500907 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
908 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
909
910 /* Check for error */
911 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500912 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400913 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
914 "0320 CLEAR_LA mbxStatus error x%x hba "
915 "state x%x\n",
916 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500917 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500918 goto out;
919 }
920
James Smart92d7f7b2007-06-17 19:56:38 -0500921 if (vport->port_type == LPFC_PHYSICAL_PORT)
922 phba->link_state = LPFC_HBA_READY;
923
924 spin_lock_irq(&phba->hbalock);
925 psli->sli_flag |= LPFC_PROCESS_LA;
926 control = readl(phba->HCregaddr);
927 control |= HC_LAINT_ENA;
928 writel(control, phba->HCregaddr);
929 readl(phba->HCregaddr); /* flush */
930 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500931 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500932 return;
dea31012005-04-17 16:05:31 -0500933
dea31012005-04-17 16:05:31 -0500934out:
935 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400936 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
937 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500938 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500939
James Smart2e0fef82007-06-17 19:56:36 -0500940 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400941 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500942 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500943
James Smart2e0fef82007-06-17 19:56:36 -0500944 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500945
946 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500947
948 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500949 psli->sli_flag |= LPFC_PROCESS_LA;
950 control = readl(phba->HCregaddr);
951 control |= HC_LAINT_ENA;
952 writel(control, phba->HCregaddr);
953 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500954 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500955
956 return;
957}
958
James Smart2e0fef82007-06-17 19:56:36 -0500959
dea31012005-04-17 16:05:31 -0500960static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500961lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500962{
James Smart2e0fef82007-06-17 19:56:36 -0500963 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500964
James Smart04c68492009-05-22 14:52:52 -0400965 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500966 goto out;
dea31012005-04-17 16:05:31 -0500967
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500968 mempool_free(pmb, phba->mbox_mem_pool);
969
970 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500971 vport->fc_flag & FC_PUBLIC_LOOP &&
972 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500973 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500974 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500975 * LPFC_LOCAL_CFG_LINK while waiting for FAN
976 */
James Smart2e0fef82007-06-17 19:56:36 -0500977 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500978 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500979 }
dea31012005-04-17 16:05:31 -0500980
James Smart2e0fef82007-06-17 19:56:36 -0500981 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500982 * LPFC_FLOGI while waiting for FLOGI cmpl
983 */
James Smart92d7f7b2007-06-17 19:56:38 -0500984 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500985 lpfc_initial_flogi(vport);
986 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500987 return;
dea31012005-04-17 16:05:31 -0500988
989out:
James Smarte8b62012007-08-02 11:10:09 -0400990 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
991 "0306 CONFIG_LINK mbxStatus error x%x "
992 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400993 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500994 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500995
996 lpfc_linkdown(phba);
997
James Smarte8b62012007-08-02 11:10:09 -0400998 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
999 "0200 CONFIG_LINK bad hba state x%x\n",
1000 vport->port_state);
dea31012005-04-17 16:05:31 -05001001
James Smart92d7f7b2007-06-17 19:56:38 -05001002 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001003 return;
1004}
1005
1006static void
James Smart6fb120a2009-05-22 14:52:59 -04001007lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1008{
1009 struct lpfc_vport *vport = mboxq->vport;
1010 unsigned long flags;
1011
1012 if (mboxq->u.mb.mbxStatus) {
1013 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1014 "2017 REG_FCFI mbxStatus error x%x "
1015 "HBA state x%x\n",
1016 mboxq->u.mb.mbxStatus, vport->port_state);
1017 mempool_free(mboxq, phba->mbox_mem_pool);
1018 return;
1019 }
1020
1021 /* Start FCoE discovery by sending a FLOGI. */
1022 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1023 /* Set the FCFI registered flag */
1024 spin_lock_irqsave(&phba->hbalock, flags);
1025 phba->fcf.fcf_flag |= FCF_REGISTERED;
1026 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart32b97932009-07-19 10:01:21 -04001027 /* If there is a pending FCoE event, restart FCF table scan. */
1028 if (lpfc_check_pending_fcoe_event(phba, 1)) {
1029 mempool_free(mboxq, phba->mbox_mem_pool);
1030 return;
1031 }
James Smart1c6f4ef52009-11-18 15:40:49 -05001032 spin_lock_irqsave(&phba->hbalock, flags);
James Smartecfd03c2010-02-12 14:41:27 -05001033 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart4d9ab992009-10-02 15:16:39 -04001034 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001035 spin_unlock_irqrestore(&phba->hbalock, flags);
1036 if (vport->port_state != LPFC_FLOGI)
James Smart6fb120a2009-05-22 14:52:59 -04001037 lpfc_initial_flogi(vport);
James Smart6fb120a2009-05-22 14:52:59 -04001038
1039 mempool_free(mboxq, phba->mbox_mem_pool);
1040 return;
1041}
1042
1043/**
1044 * lpfc_fab_name_match - Check if the fcf fabric name match.
1045 * @fab_name: pointer to fabric name.
1046 * @new_fcf_record: pointer to fcf record.
1047 *
1048 * This routine compare the fcf record's fabric name with provided
1049 * fabric name. If the fabric name are identical this function
1050 * returns 1 else return 0.
1051 **/
1052static uint32_t
1053lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1054{
James Smartecfd03c2010-02-12 14:41:27 -05001055 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001056 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001057 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1058 return 0;
1059 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1060 return 0;
1061 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1062 return 0;
1063 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1064 return 0;
1065 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1066 return 0;
1067 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1068 return 0;
1069 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1070 return 0;
1071 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001072}
1073
1074/**
James Smart8fa38512009-07-19 10:01:03 -04001075 * lpfc_sw_name_match - Check if the fcf switch name match.
1076 * @fab_name: pointer to fabric name.
1077 * @new_fcf_record: pointer to fcf record.
1078 *
1079 * This routine compare the fcf record's switch name with provided
1080 * switch name. If the switch name are identical this function
1081 * returns 1 else return 0.
1082 **/
1083static uint32_t
1084lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1085{
James Smartecfd03c2010-02-12 14:41:27 -05001086 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001087 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001088 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1089 return 0;
1090 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1091 return 0;
1092 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1093 return 0;
1094 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1095 return 0;
1096 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1097 return 0;
1098 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1099 return 0;
1100 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1101 return 0;
1102 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001103}
1104
1105/**
James Smart6fb120a2009-05-22 14:52:59 -04001106 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001107 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001108 * @new_fcf_record: pointer to fcf record.
1109 *
1110 * This routine compare the fcf record's mac address with HBA's
1111 * FCF mac address. If the mac addresses are identical this function
1112 * returns 1 else return 0.
1113 **/
1114static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001115lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001116{
James Smartecfd03c2010-02-12 14:41:27 -05001117 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001118 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001119 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1120 return 0;
1121 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1122 return 0;
1123 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1124 return 0;
1125 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1126 return 0;
1127 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1128 return 0;
1129 return 1;
1130}
1131
1132static bool
1133lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1134{
1135 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001136}
1137
1138/**
1139 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001140 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001141 * @new_fcf_record: pointer to fcf record.
1142 *
1143 * This routine copies the FCF information from the FCF
1144 * record to lpfc_hba data structure.
1145 **/
1146static void
James Smartecfd03c2010-02-12 14:41:27 -05001147lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1148 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001149{
James Smartecfd03c2010-02-12 14:41:27 -05001150 /* Fabric name */
1151 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001152 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001153 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001154 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001155 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001156 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001157 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001158 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001159 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001160 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001161 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001162 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001163 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001164 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001165 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001166 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001167 /* Mac address */
1168 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1169 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1170 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1171 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1172 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1173 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1174 /* FCF record index */
1175 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1176 /* FCF record priority */
1177 fcf_rec->priority = new_fcf_record->fip_priority;
1178 /* Switch name */
1179 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001180 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001181 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001182 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001183 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001184 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001185 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001186 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001187 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001188 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001189 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001190 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001191 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001192 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001193 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001194 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001195}
1196
1197/**
James Smartecfd03c2010-02-12 14:41:27 -05001198 * lpfc_update_fcf_record - Update driver fcf record
1199 * @phba: pointer to lpfc hba data structure.
1200 * @fcf_rec: pointer to driver fcf record.
1201 * @new_fcf_record: pointer to hba fcf record.
1202 * @addr_mode: address mode to be set to the driver fcf record.
1203 * @vlan_id: vlan tag to be set to the driver fcf record.
1204 * @flag: flag bits to be set to the driver fcf record.
1205 *
1206 * This routine updates the driver FCF record from the new HBA FCF record
1207 * together with the address mode, vlan_id, and other informations. This
1208 * routine is called with the host lock held.
1209 **/
1210static void
1211__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1212 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1213 uint16_t vlan_id, uint32_t flag)
1214{
1215 /* Copy the fields from the HBA's FCF record */
1216 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1217 /* Update other fields of driver FCF record */
1218 fcf_rec->addr_mode = addr_mode;
1219 fcf_rec->vlan_id = vlan_id;
1220 fcf_rec->flag |= (flag | RECORD_VALID);
1221}
1222
1223/**
James Smart6fb120a2009-05-22 14:52:59 -04001224 * lpfc_register_fcf - Register the FCF with hba.
1225 * @phba: pointer to lpfc hba data structure.
1226 *
1227 * This routine issues a register fcfi mailbox command to register
1228 * the fcf with HBA.
1229 **/
1230static void
1231lpfc_register_fcf(struct lpfc_hba *phba)
1232{
1233 LPFC_MBOXQ_t *fcf_mbxq;
1234 int rc;
1235 unsigned long flags;
1236
1237 spin_lock_irqsave(&phba->hbalock, flags);
1238
1239 /* If the FCF is not availabe do nothing. */
1240 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smart4d9ab992009-10-02 15:16:39 -04001241 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001242 spin_unlock_irqrestore(&phba->hbalock, flags);
1243 return;
1244 }
1245
1246 /* The FCF is already registered, start discovery */
1247 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001248 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart32b97932009-07-19 10:01:21 -04001249 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001250 spin_unlock_irqrestore(&phba->hbalock, flags);
1251 if (phba->pport->port_state != LPFC_FLOGI)
1252 lpfc_initial_flogi(phba->pport);
1253 return;
1254 }
1255 spin_unlock_irqrestore(&phba->hbalock, flags);
1256
1257 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1258 GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001259 if (!fcf_mbxq) {
1260 spin_lock_irqsave(&phba->hbalock, flags);
1261 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1262 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001263 return;
James Smart4d9ab992009-10-02 15:16:39 -04001264 }
James Smart6fb120a2009-05-22 14:52:59 -04001265
1266 lpfc_reg_fcfi(phba, fcf_mbxq);
1267 fcf_mbxq->vport = phba->pport;
1268 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1269 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001270 if (rc == MBX_NOT_FINISHED) {
1271 spin_lock_irqsave(&phba->hbalock, flags);
1272 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1273 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001274 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001275 }
James Smart6fb120a2009-05-22 14:52:59 -04001276
1277 return;
1278}
1279
1280/**
1281 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1282 * @phba: pointer to lpfc hba data structure.
1283 * @new_fcf_record: pointer to fcf record.
1284 * @boot_flag: Indicates if this record used by boot bios.
1285 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001286 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001287 *
1288 * This routine compare the fcf record with connect list obtained from the
1289 * config region to decide if this FCF can be used for SAN discovery. It returns
1290 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1291 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1292 * is used by boot bios and addr_mode will indicate the addressing mode to be
1293 * used for this FCF when the function returns.
1294 * If the FCF record need to be used with a particular vlan id, the vlan is
1295 * set in the vlan_id on return of the function. If not VLAN tagging need to
1296 * be used with the FCF vlan_id will be set to 0xFFFF;
1297 **/
1298static int
1299lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1300 struct fcf_record *new_fcf_record,
1301 uint32_t *boot_flag, uint32_t *addr_mode,
1302 uint16_t *vlan_id)
1303{
1304 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001305 int i, j, fcf_vlan_id = 0;
1306
1307 /* Find the lowest VLAN id in the FCF record */
1308 for (i = 0; i < 512; i++) {
1309 if (new_fcf_record->vlan_bitmap[i]) {
1310 fcf_vlan_id = i * 8;
1311 j = 0;
1312 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1313 j++;
1314 fcf_vlan_id++;
1315 }
1316 break;
1317 }
1318 }
James Smart6fb120a2009-05-22 14:52:59 -04001319
James Smart0c287582009-06-10 17:22:56 -04001320 /* If FCF not available return 0 */
1321 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1322 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1323 return 0;
1324
James Smart45ed1192009-10-02 15:17:02 -04001325 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001326 *boot_flag = 0;
1327 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1328 new_fcf_record);
1329 if (phba->valid_vlan)
1330 *vlan_id = phba->vlan_id;
1331 else
1332 *vlan_id = 0xFFFF;
1333 return 1;
1334 }
1335
1336 /*
1337 * If there are no FCF connection table entry, driver connect to all
1338 * FCFs.
1339 */
1340 if (list_empty(&phba->fcf_conn_rec_list)) {
1341 *boot_flag = 0;
1342 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1343 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001344
1345 /*
1346 * When there are no FCF connect entries, use driver's default
1347 * addressing mode - FPMA.
1348 */
1349 if (*addr_mode & LPFC_FCF_FPMA)
1350 *addr_mode = LPFC_FCF_FPMA;
1351
James Smart4d9ab992009-10-02 15:16:39 -04001352 /* If FCF record report a vlan id use that vlan id */
1353 if (fcf_vlan_id)
1354 *vlan_id = fcf_vlan_id;
1355 else
1356 *vlan_id = 0xFFFF;
James Smart6fb120a2009-05-22 14:52:59 -04001357 return 1;
1358 }
1359
James Smartecfd03c2010-02-12 14:41:27 -05001360 list_for_each_entry(conn_entry,
1361 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001362 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1363 continue;
1364
1365 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1366 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001367 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001368 continue;
James Smart8fa38512009-07-19 10:01:03 -04001369 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1370 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1371 new_fcf_record))
1372 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001373 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1374 /*
1375 * If the vlan bit map does not have the bit set for the
1376 * vlan id to be used, then it is not a match.
1377 */
1378 if (!(new_fcf_record->vlan_bitmap
1379 [conn_entry->conn_rec.vlan_tag / 8] &
1380 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1381 continue;
1382 }
1383
1384 /*
James Smart0c287582009-06-10 17:22:56 -04001385 * If connection record does not support any addressing mode,
1386 * skip the FCF record.
1387 */
1388 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1389 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1390 continue;
1391
1392 /*
James Smart6fb120a2009-05-22 14:52:59 -04001393 * Check if the connection record specifies a required
1394 * addressing mode.
1395 */
1396 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1397 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1398
1399 /*
1400 * If SPMA required but FCF not support this continue.
1401 */
1402 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1403 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1404 new_fcf_record) & LPFC_FCF_SPMA))
1405 continue;
1406
1407 /*
1408 * If FPMA required but FCF not support this continue.
1409 */
1410 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1411 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1412 new_fcf_record) & LPFC_FCF_FPMA))
1413 continue;
1414 }
1415
1416 /*
1417 * This fcf record matches filtering criteria.
1418 */
1419 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1420 *boot_flag = 1;
1421 else
1422 *boot_flag = 0;
1423
James Smart0c287582009-06-10 17:22:56 -04001424 /*
1425 * If user did not specify any addressing mode, or if the
1426 * prefered addressing mode specified by user is not supported
1427 * by FCF, allow fabric to pick the addressing mode.
1428 */
James Smart6fb120a2009-05-22 14:52:59 -04001429 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1430 new_fcf_record);
1431 /*
1432 * If the user specified a required address mode, assign that
1433 * address mode
1434 */
1435 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1436 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1437 *addr_mode = (conn_entry->conn_rec.flags &
1438 FCFCNCT_AM_SPMA) ?
1439 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1440 /*
1441 * If the user specified a prefered address mode, use the
1442 * addr mode only if FCF support the addr_mode.
1443 */
1444 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1445 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1446 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1447 (*addr_mode & LPFC_FCF_SPMA))
1448 *addr_mode = LPFC_FCF_SPMA;
1449 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1450 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1451 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1452 (*addr_mode & LPFC_FCF_FPMA))
1453 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001454
James Smart4d9ab992009-10-02 15:16:39 -04001455 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001456 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1457 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001458 /*
1459 * If no vlan id is specified in connect list, use the vlan id
1460 * in the FCF record
1461 */
1462 else if (fcf_vlan_id)
1463 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001464 else
1465 *vlan_id = 0xFFFF;
1466
1467 return 1;
1468 }
1469
1470 return 0;
1471}
1472
1473/**
James Smart32b97932009-07-19 10:01:21 -04001474 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1475 * @phba: pointer to lpfc hba data structure.
1476 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1477 *
1478 * This function check if there is any fcoe event pending while driver
1479 * scan FCF entries. If there is any pending event, it will restart the
1480 * FCF saning and return 1 else return 0.
1481 */
1482int
1483lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1484{
James Smart32b97932009-07-19 10:01:21 -04001485 /*
1486 * If the Link is up and no FCoE events while in the
1487 * FCF discovery, no need to restart FCF discovery.
1488 */
1489 if ((phba->link_state >= LPFC_LINK_UP) &&
1490 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1491 return 0;
1492
James Smart0c9ab6f2010-02-26 14:15:57 -05001493 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1494 "2768 Pending link or FCF event during current "
1495 "handling of the previous event: link_state:x%x, "
1496 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1497 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1498 phba->fcoe_eventtag);
1499
James Smart32b97932009-07-19 10:01:21 -04001500 spin_lock_irq(&phba->hbalock);
1501 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1502 spin_unlock_irq(&phba->hbalock);
1503
James Smart0c9ab6f2010-02-26 14:15:57 -05001504 if (phba->link_state >= LPFC_LINK_UP) {
1505 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1506 "2780 Restart FCF table scan due to "
1507 "pending FCF event:evt_tag_at_scan:x%x, "
1508 "evt_tag_current:x%x\n",
1509 phba->fcoe_eventtag_at_fcf_scan,
1510 phba->fcoe_eventtag);
1511 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1512 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001513 /*
1514 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1515 * flag
1516 */
James Smart1c6f4ef52009-11-18 15:40:49 -05001517 spin_lock_irq(&phba->hbalock);
James Smart4d9ab992009-10-02 15:16:39 -04001518 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001519 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001520 spin_unlock_irq(&phba->hbalock);
1521 }
James Smart32b97932009-07-19 10:01:21 -04001522
James Smart0c9ab6f2010-02-26 14:15:57 -05001523 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001524 if (unreg_fcf) {
1525 spin_lock_irq(&phba->hbalock);
1526 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1527 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001528 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001529 }
James Smart32b97932009-07-19 10:01:21 -04001530 return 1;
1531}
1532
1533/**
James Smart999d8132010-03-15 11:24:56 -04001534 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1535 * @phba: pointer to lpfc hba data structure.
1536 * @fcf_cnt: number of eligible fcf record seen so far.
1537 *
1538 * This function makes an running random selection decision on FCF record to
1539 * use through a sequence of @fcf_cnt eligible FCF records with equal
1540 * probability. To perform integer manunipulation of random numbers with
1541 * size unit32_t, the lower 16 bits of the 32-bit random number returned
1542 * from random32() are taken as the random random number generated.
1543 *
1544 * Returns true when outcome is for the newly read FCF record should be
1545 * chosen; otherwise, return false when outcome is for keeping the previously
1546 * chosen FCF record.
1547 **/
1548static bool
1549lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1550{
1551 uint32_t rand_num;
1552
1553 /* Get 16-bit uniform random number */
1554 rand_num = (0xFFFF & random32());
1555
1556 /* Decision with probability 1/fcf_cnt */
1557 if ((fcf_cnt * rand_num) < 0xFFFF)
1558 return true;
1559 else
1560 return false;
1561}
1562
1563/**
1564 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
James Smart6fb120a2009-05-22 14:52:59 -04001565 * @phba: pointer to lpfc hba data structure.
1566 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001567 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001568 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001569 * This routine parses the non-embedded fcf mailbox command by performing the
1570 * necessarily error checking, non-embedded read FCF record mailbox command
1571 * SGE parsing, and endianness swapping.
1572 *
1573 * Returns the pointer to the new FCF record in the non-embedded mailbox
1574 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001575 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001576static struct fcf_record *
1577lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1578 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001579{
1580 void *virt_addr;
1581 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001582 struct lpfc_mbx_sge sge;
1583 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1584 uint32_t shdr_status, shdr_add_status;
1585 union lpfc_sli4_cfg_shdr *shdr;
1586 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001587
James Smart6fb120a2009-05-22 14:52:59 -04001588 /* Get the first SGE entry from the non-embedded DMA memory. This
1589 * routine only uses a single SGE.
1590 */
1591 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1592 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1593 if (unlikely(!mboxq->sge_array)) {
1594 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1595 "2524 Failed to get the non-embedded SGE "
1596 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001597 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001598 }
1599 virt_addr = mboxq->sge_array->addr[0];
1600
1601 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1602 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001603 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001604 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001605 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1606 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001607 "2726 READ_FCF_RECORD Indicates empty "
1608 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001609 else
1610 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001611 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001612 "with status x%x add_status x%x, "
1613 "mbx\n", shdr_status, shdr_add_status);
1614 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001615 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001616
1617 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001618 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1619 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1620 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001621 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001622 new_fcf_record = (struct fcf_record *)(virt_addr +
1623 sizeof(struct lpfc_mbx_read_fcf_tbl));
1624 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1625 sizeof(struct fcf_record));
James Smart6fb120a2009-05-22 14:52:59 -04001626
James Smart0c9ab6f2010-02-26 14:15:57 -05001627 return new_fcf_record;
1628}
1629
1630/**
1631 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1632 * @phba: pointer to lpfc hba data structure.
1633 * @fcf_record: pointer to the fcf record.
1634 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1635 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1636 *
1637 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1638 * enabled.
1639 **/
1640static void
1641lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1642 struct fcf_record *fcf_record,
1643 uint16_t vlan_id,
1644 uint16_t next_fcf_index)
1645{
1646 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1647 "2764 READ_FCF_RECORD:\n"
1648 "\tFCF_Index : x%x\n"
1649 "\tFCF_Avail : x%x\n"
1650 "\tFCF_Valid : x%x\n"
1651 "\tFIP_Priority : x%x\n"
1652 "\tMAC_Provider : x%x\n"
1653 "\tLowest VLANID : x%x\n"
1654 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1655 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1656 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1657 "\tNext_FCF_Index: x%x\n",
1658 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1659 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1660 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1661 fcf_record->fip_priority,
1662 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1663 vlan_id,
1664 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1665 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1666 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1667 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1668 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1669 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1670 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1671 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1672 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1673 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1674 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1675 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1676 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1677 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1678 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1679 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1680 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1681 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1682 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1683 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1684 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1685 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1686 next_fcf_index);
1687}
1688
1689/**
1690 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
1691 * @phba: pointer to lpfc hba data structure.
1692 * @mboxq: pointer to mailbox object.
1693 *
1694 * This function iterates through all the fcf records available in
1695 * HBA and chooses the optimal FCF record for discovery. After finding
1696 * the FCF for discovery it registers the FCF record and kicks start
1697 * discovery.
1698 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
1699 * use an FCF record which matches fabric name and mac address of the
1700 * currently used FCF record.
1701 * If the driver supports only one FCF, it will try to use the FCF record
1702 * used by BOOT_BIOS.
1703 */
1704void
1705lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1706{
1707 struct fcf_record *new_fcf_record;
1708 uint32_t boot_flag, addr_mode;
1709 uint16_t fcf_index, next_fcf_index;
1710 struct lpfc_fcf_rec *fcf_rec = NULL;
1711 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04001712 uint32_t seed;
1713 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001714 int rc;
1715
1716 /* If there is pending FCoE event restart FCF table scan */
1717 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1718 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1719 return;
1720 }
1721
1722 /* Parse the FCF record from the non-embedded mailbox command */
1723 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
1724 &next_fcf_index);
1725 if (!new_fcf_record) {
1726 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1727 "2765 Mailbox command READ_FCF_RECORD "
1728 "failed to retrieve a FCF record.\n");
1729 /* Let next new FCF event trigger fast failover */
1730 spin_lock_irq(&phba->hbalock);
1731 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1732 spin_unlock_irq(&phba->hbalock);
1733 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1734 return;
1735 }
1736
1737 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05001738 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1739 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05001740
1741 /* Log the FCF record information if turned on */
1742 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
1743 next_fcf_index);
1744
James Smart6fb120a2009-05-22 14:52:59 -04001745 /*
1746 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05001747 * read the next entry; otherwise, this is an eligible FCF
1748 * record for round robin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04001749 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001750 if (!rc) {
1751 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1752 "2781 FCF record fcf_index:x%x failed FCF "
1753 "connection list check, fcf_avail:x%x, "
1754 "fcf_valid:x%x\n",
1755 bf_get(lpfc_fcf_record_fcf_index,
1756 new_fcf_record),
1757 bf_get(lpfc_fcf_record_fcf_avail,
1758 new_fcf_record),
1759 bf_get(lpfc_fcf_record_fcf_valid,
1760 new_fcf_record));
James Smart6fb120a2009-05-22 14:52:59 -04001761 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001762 } else {
1763 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1764 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
1765 if (rc)
1766 goto read_next_fcf;
1767 }
1768
James Smart6fb120a2009-05-22 14:52:59 -04001769 /*
1770 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05001771 * FCF record for the discovery. The condition that a rescan
1772 * matches the in-use FCF record: fabric name, switch name, mac
1773 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04001774 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001775 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001776 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartecfd03c2010-02-12 14:41:27 -05001777 if (lpfc_fab_name_match(phba->fcf.current_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001778 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001779 lpfc_sw_name_match(phba->fcf.current_rec.switch_name,
James Smart8fa38512009-07-19 10:01:03 -04001780 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001781 lpfc_mac_addr_match(phba->fcf.current_rec.mac_addr,
1782 new_fcf_record) &&
1783 lpfc_vlan_id_match(phba->fcf.current_rec.vlan_id,
1784 vlan_id)) {
James Smart6fb120a2009-05-22 14:52:59 -04001785 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smartecfd03c2010-02-12 14:41:27 -05001786 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
1787 /* Stop FCF redisc wait timer if pending */
1788 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
1789 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1790 /* If in fast failover, mark it's completed */
James Smartfc2b9892010-02-26 14:15:29 -05001791 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV |
James Smart0c9ab6f2010-02-26 14:15:57 -05001792 FCF_DISCOVERY);
1793 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001794 goto out;
1795 }
James Smartecfd03c2010-02-12 14:41:27 -05001796 /*
1797 * Read next FCF record from HBA searching for the matching
1798 * with in-use record only if not during the fast failover
1799 * period. In case of fast failover period, it shall try to
1800 * determine whether the FCF record just read should be the
1801 * next candidate.
1802 */
1803 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001804 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001805 goto read_next_fcf;
1806 }
James Smart6fb120a2009-05-22 14:52:59 -04001807 }
James Smartecfd03c2010-02-12 14:41:27 -05001808 /*
1809 * Update on failover FCF record only if it's in FCF fast-failover
1810 * period; otherwise, update on current FCF record.
1811 */
James Smartfc2b9892010-02-26 14:15:29 -05001812 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1813 fcf_rec = &phba->fcf.failover_rec;
1814 else
James Smartecfd03c2010-02-12 14:41:27 -05001815 fcf_rec = &phba->fcf.current_rec;
1816
James Smart6fb120a2009-05-22 14:52:59 -04001817 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1818 /*
James Smartecfd03c2010-02-12 14:41:27 -05001819 * If the driver FCF record does not have boot flag
1820 * set and new hba fcf record has boot flag set, use
1821 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001822 */
James Smartecfd03c2010-02-12 14:41:27 -05001823 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
1824 /* Choose this FCF record */
1825 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1826 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05001827 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001828 goto read_next_fcf;
1829 }
1830 /*
James Smartecfd03c2010-02-12 14:41:27 -05001831 * If the driver FCF record has boot flag set and the
1832 * new hba FCF record does not have boot flag, read
1833 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04001834 */
James Smartecfd03c2010-02-12 14:41:27 -05001835 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001836 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001837 goto read_next_fcf;
1838 }
1839 /*
James Smartecfd03c2010-02-12 14:41:27 -05001840 * If the new hba FCF record has lower priority value
1841 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04001842 */
James Smartfc2b9892010-02-26 14:15:29 -05001843 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04001844 /* Choose the new FCF record with lower priority */
James Smartecfd03c2010-02-12 14:41:27 -05001845 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1846 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04001847 /* Reset running random FCF selection count */
1848 phba->fcf.eligible_fcf_cnt = 1;
1849 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
1850 /* Update running random FCF selection count */
1851 phba->fcf.eligible_fcf_cnt++;
1852 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
1853 phba->fcf.eligible_fcf_cnt);
1854 if (select_new_fcf)
1855 /* Choose the new FCF by random selection */
1856 __lpfc_update_fcf_record(phba, fcf_rec,
1857 new_fcf_record,
1858 addr_mode, vlan_id, 0);
James Smart6fb120a2009-05-22 14:52:59 -04001859 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001860 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001861 goto read_next_fcf;
1862 }
1863 /*
James Smartecfd03c2010-02-12 14:41:27 -05001864 * This is the first suitable FCF record, choose this record for
1865 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001866 */
James Smartecfd03c2010-02-12 14:41:27 -05001867 if (fcf_rec) {
1868 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1869 addr_mode, vlan_id, (boot_flag ?
1870 BOOT_ENABLE : 0));
1871 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04001872 /* Setup initial running random FCF selection count */
1873 phba->fcf.eligible_fcf_cnt = 1;
1874 /* Seeding the random number generator for random selection */
1875 seed = (uint32_t)(0xFFFFFFFF & jiffies);
1876 srandom32(seed);
James Smart6fb120a2009-05-22 14:52:59 -04001877 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001878 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001879 goto read_next_fcf;
1880
1881read_next_fcf:
1882 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05001883 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
1884 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
1885 /*
1886 * Case of FCF fast failover scan
1887 */
1888
1889 /*
1890 * It has not found any suitable FCF record, cancel
1891 * FCF scan inprogress, and do nothing
1892 */
1893 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001894 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1895 "2782 No suitable FCF record "
1896 "found during this round of "
1897 "post FCF rediscovery scan: "
1898 "fcf_evt_tag:x%x, fcf_index: "
1899 "x%x\n",
1900 phba->fcoe_eventtag_at_fcf_scan,
1901 bf_get(lpfc_fcf_record_fcf_index,
1902 new_fcf_record));
1903 /*
1904 * Let next new FCF event trigger fast
1905 * failover
1906 */
1907 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001908 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001909 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001910 return;
1911 }
1912 /*
1913 * It has found a suitable FCF record that is not
1914 * the same as in-use FCF record, unregister the
1915 * in-use FCF record, replace the in-use FCF record
1916 * with the new FCF record, mark FCF fast failover
1917 * completed, and then start register the new FCF
1918 * record.
1919 */
1920
James Smart0c9ab6f2010-02-26 14:15:57 -05001921 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001922 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05001923
1924 /* Replace in-use record with the new record */
James Smartecfd03c2010-02-12 14:41:27 -05001925 memcpy(&phba->fcf.current_rec,
1926 &phba->fcf.failover_rec,
1927 sizeof(struct lpfc_fcf_rec));
1928 /* mark the FCF fast failover completed */
James Smart0c9ab6f2010-02-26 14:15:57 -05001929 spin_lock_irq(&phba->hbalock);
1930 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
1931 spin_unlock_irq(&phba->hbalock);
1932 /*
1933 * Set up the initial registered FCF index for FLOGI
1934 * round robin FCF failover.
1935 */
1936 phba->fcf.fcf_rr_init_indx =
1937 phba->fcf.failover_rec.fcf_indx;
James Smartecfd03c2010-02-12 14:41:27 -05001938 /* Register to the new FCF record */
1939 lpfc_register_fcf(phba);
1940 } else {
1941 /*
1942 * In case of transaction period to fast FCF failover,
1943 * do nothing when search to the end of the FCF table.
1944 */
1945 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
1946 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
1947 return;
1948 /*
1949 * Otherwise, initial scan or post linkdown rescan,
James Smart0c9ab6f2010-02-26 14:15:57 -05001950 * register with the best FCF record found so far
1951 * through the FCF scanning process.
James Smartecfd03c2010-02-12 14:41:27 -05001952 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001953
1954 /* mark the initial FCF discovery completed */
1955 spin_lock_irq(&phba->hbalock);
1956 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
1957 spin_unlock_irq(&phba->hbalock);
1958 /*
1959 * Set up the initial registered FCF index for FLOGI
1960 * round robin FCF failover
1961 */
1962 phba->fcf.fcf_rr_init_indx =
1963 phba->fcf.current_rec.fcf_indx;
1964 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001965 lpfc_register_fcf(phba);
1966 }
1967 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05001968 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04001969 return;
1970
1971out:
1972 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1973 lpfc_register_fcf(phba);
1974
1975 return;
1976}
1977
1978/**
James Smart0c9ab6f2010-02-26 14:15:57 -05001979 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf round robin read_fcf mbox cmpl hdler
1980 * @phba: pointer to lpfc hba data structure.
1981 * @mboxq: pointer to mailbox object.
1982 *
1983 * This is the callback function for FLOGI failure round robin FCF failover
1984 * read FCF record mailbox command from the eligible FCF record bmask for
1985 * performing the failover. If the FCF read back is not valid/available, it
1986 * fails through to retrying FLOGI to the currently registered FCF again.
1987 * Otherwise, if the FCF read back is valid and available, it will set the
1988 * newly read FCF record to the failover FCF record, unregister currently
1989 * registered FCF record, copy the failover FCF record to the current
1990 * FCF record, and then register the current FCF record before proceeding
1991 * to trying FLOGI on the new failover FCF.
1992 */
1993void
1994lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1995{
1996 struct fcf_record *new_fcf_record;
1997 uint32_t boot_flag, addr_mode;
1998 uint16_t next_fcf_index;
1999 uint16_t current_fcf_index;
2000 uint16_t vlan_id;
2001
2002 /* If link state is not up, stop the round robin failover process */
2003 if (phba->link_state < LPFC_LINK_UP) {
2004 spin_lock_irq(&phba->hbalock);
2005 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
2006 spin_unlock_irq(&phba->hbalock);
2007 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2008 return;
2009 }
2010
2011 /* Parse the FCF record from the non-embedded mailbox command */
2012 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2013 &next_fcf_index);
2014 if (!new_fcf_record) {
2015 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2016 "2766 Mailbox command READ_FCF_RECORD "
2017 "failed to retrieve a FCF record.\n");
2018 goto out;
2019 }
2020
2021 /* Get the needed parameters from FCF record */
2022 lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2023 &addr_mode, &vlan_id);
2024
2025 /* Log the FCF record information if turned on */
2026 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2027 next_fcf_index);
2028
2029 /* Upload new FCF record to the failover FCF record */
2030 spin_lock_irq(&phba->hbalock);
2031 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2032 new_fcf_record, addr_mode, vlan_id,
2033 (boot_flag ? BOOT_ENABLE : 0));
2034 spin_unlock_irq(&phba->hbalock);
2035
2036 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2037
2038 /* Unregister the current in-use FCF record */
2039 lpfc_unregister_fcf(phba);
2040
2041 /* Replace in-use record with the new record */
2042 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2043 sizeof(struct lpfc_fcf_rec));
2044
2045 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2046 "2783 FLOGI round robin FCF failover from FCF "
2047 "(index:x%x) to FCF (index:x%x).\n",
2048 current_fcf_index,
2049 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
2050
2051out:
2052 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2053 lpfc_register_fcf(phba);
2054}
2055
2056/**
2057 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2058 * @phba: pointer to lpfc hba data structure.
2059 * @mboxq: pointer to mailbox object.
2060 *
2061 * This is the callback function of read FCF record mailbox command for
2062 * updating the eligible FCF bmask for FLOGI failure round robin FCF
2063 * failover when a new FCF event happened. If the FCF read back is
2064 * valid/available and it passes the connection list check, it updates
2065 * the bmask for the eligible FCF record for round robin failover.
2066 */
2067void
2068lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2069{
2070 struct fcf_record *new_fcf_record;
2071 uint32_t boot_flag, addr_mode;
2072 uint16_t fcf_index, next_fcf_index;
2073 uint16_t vlan_id;
2074 int rc;
2075
2076 /* If link state is not up, no need to proceed */
2077 if (phba->link_state < LPFC_LINK_UP)
2078 goto out;
2079
2080 /* If FCF discovery period is over, no need to proceed */
2081 if (phba->fcf.fcf_flag & FCF_DISCOVERY)
2082 goto out;
2083
2084 /* Parse the FCF record from the non-embedded mailbox command */
2085 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2086 &next_fcf_index);
2087 if (!new_fcf_record) {
2088 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2089 "2767 Mailbox command READ_FCF_RECORD "
2090 "failed to retrieve a FCF record.\n");
2091 goto out;
2092 }
2093
2094 /* Check the connection list for eligibility */
2095 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2096 &addr_mode, &vlan_id);
2097
2098 /* Log the FCF record information if turned on */
2099 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2100 next_fcf_index);
2101
2102 if (!rc)
2103 goto out;
2104
2105 /* Update the eligible FCF record index bmask */
2106 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2107 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
2108
2109out:
2110 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2111}
2112
2113/**
James Smart1c6834a2009-07-19 10:01:26 -04002114 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2115 * @phba: pointer to lpfc hba data structure.
2116 * @mboxq: pointer to mailbox data structure.
2117 *
2118 * This function handles completion of init vpi mailbox command.
2119 */
James Smart695a8142010-01-26 23:08:03 -05002120void
James Smart1c6834a2009-07-19 10:01:26 -04002121lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2122{
2123 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002124 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002125 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2126
James Smart1c6834a2009-07-19 10:01:26 -04002127 if (mboxq->u.mb.mbxStatus) {
2128 lpfc_printf_vlog(vport, KERN_ERR,
2129 LOG_MBOX,
2130 "2609 Init VPI mailbox failed 0x%x\n",
2131 mboxq->u.mb.mbxStatus);
2132 mempool_free(mboxq, phba->mbox_mem_pool);
2133 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2134 return;
2135 }
James Smart72100cc2010-02-12 14:43:01 -05002136 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002137 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002138 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002139
James Smart695a8142010-01-26 23:08:03 -05002140 /* If this port is physical port or FDISC is done, do reg_vpi */
2141 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2142 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2143 if (!ndlp)
2144 lpfc_printf_vlog(vport, KERN_ERR,
2145 LOG_DISCOVERY,
2146 "2731 Cannot find fabric "
2147 "controller node\n");
2148 else
2149 lpfc_register_new_vport(phba, vport, ndlp);
2150 mempool_free(mboxq, phba->mbox_mem_pool);
2151 return;
2152 }
2153
James Smart1c6834a2009-07-19 10:01:26 -04002154 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2155 lpfc_initial_fdisc(vport);
2156 else {
2157 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002158 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2159 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002160 }
James Smart695a8142010-01-26 23:08:03 -05002161 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002162 return;
2163}
2164
2165/**
James Smartecfd03c2010-02-12 14:41:27 -05002166 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2167 * @vport: pointer to lpfc_vport data structure.
2168 *
2169 * This function issue a init_vpi mailbox command to initialize
2170 * VPI for the vport.
2171 */
2172void
2173lpfc_issue_init_vpi(struct lpfc_vport *vport)
2174{
2175 LPFC_MBOXQ_t *mboxq;
2176 int rc;
2177
2178 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2179 if (!mboxq) {
2180 lpfc_printf_vlog(vport, KERN_ERR,
2181 LOG_MBOX, "2607 Failed to allocate "
2182 "init_vpi mailbox\n");
2183 return;
2184 }
2185 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2186 mboxq->vport = vport;
2187 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2188 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2189 if (rc == MBX_NOT_FINISHED) {
2190 lpfc_printf_vlog(vport, KERN_ERR,
2191 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2192 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2193 }
2194}
2195
2196/**
James Smart6fb120a2009-05-22 14:52:59 -04002197 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2198 * @phba: pointer to lpfc hba data structure.
2199 *
2200 * This function loops through the list of vports on the @phba and issues an
2201 * FDISC if possible.
2202 */
2203void
2204lpfc_start_fdiscs(struct lpfc_hba *phba)
2205{
2206 struct lpfc_vport **vports;
2207 int i;
2208
2209 vports = lpfc_create_vport_work_array(phba);
2210 if (vports != NULL) {
2211 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2212 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2213 continue;
2214 /* There are no vpi for this vport */
2215 if (vports[i]->vpi > phba->max_vpi) {
2216 lpfc_vport_set_state(vports[i],
2217 FC_VPORT_FAILED);
2218 continue;
2219 }
2220 if (phba->fc_topology == TOPOLOGY_LOOP) {
2221 lpfc_vport_set_state(vports[i],
2222 FC_VPORT_LINKDOWN);
2223 continue;
2224 }
James Smart1c6834a2009-07-19 10:01:26 -04002225 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002226 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002227 continue;
2228 }
James Smart6fb120a2009-05-22 14:52:59 -04002229 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2230 lpfc_initial_fdisc(vports[i]);
2231 else {
2232 lpfc_vport_set_state(vports[i],
2233 FC_VPORT_NO_FABRIC_SUPP);
2234 lpfc_printf_vlog(vports[i], KERN_ERR,
2235 LOG_ELS,
2236 "0259 No NPIV "
2237 "Fabric support\n");
2238 }
2239 }
2240 }
2241 lpfc_destroy_vport_work_array(phba, vports);
2242}
2243
2244void
2245lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2246{
2247 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2248 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002249 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002250
2251 if (mboxq->u.mb.mbxStatus) {
2252 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2253 "2018 REG_VFI mbxStatus error x%x "
2254 "HBA state x%x\n",
2255 mboxq->u.mb.mbxStatus, vport->port_state);
2256 if (phba->fc_topology == TOPOLOGY_LOOP) {
2257 /* FLOGI failed, use loop map to make discovery list */
2258 lpfc_disc_list_loopmap(vport);
2259 /* Start discovery */
2260 lpfc_disc_start(vport);
2261 goto fail_free_mem;
2262 }
2263 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2264 goto fail_free_mem;
2265 }
James Smartc8685952009-11-18 15:39:16 -05002266 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002267 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002268 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002269 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002270 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002271 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002272
2273 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2274 lpfc_start_fdiscs(phba);
2275 lpfc_do_scr_ns_plogi(phba, vport);
2276 }
2277
2278fail_free_mem:
2279 mempool_free(mboxq, phba->mbox_mem_pool);
2280 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2281 kfree(dmabuf);
2282 return;
2283}
2284
2285static void
James Smart2e0fef82007-06-17 19:56:36 -05002286lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002287{
James Smart6fb120a2009-05-22 14:52:59 -04002288 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002289 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002290 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002291
2292
2293 /* Check for error */
2294 if (mb->mbxStatus) {
2295 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002296 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2297 "0319 READ_SPARAM mbxStatus error x%x "
2298 "hba state x%x>\n",
2299 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002300 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002301 goto out;
2302 }
2303
James Smart2e0fef82007-06-17 19:56:36 -05002304 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002305 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05002306 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05002307 u64_to_wwn(phba->cfg_soft_wwnn,
2308 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04002309 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05002310 u64_to_wwn(phba->cfg_soft_wwpn,
2311 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05002312 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
2313 sizeof(vport->fc_nodename));
2314 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
2315 sizeof(vport->fc_portname));
2316 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2317 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2318 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2319 }
2320
dea31012005-04-17 16:05:31 -05002321 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2322 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002323 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002324 return;
2325
2326out:
2327 pmb->context1 = NULL;
2328 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2329 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002330 lpfc_issue_clear_la(phba, vport);
2331 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002332 return;
2333}
2334
2335static void
James Smart92d7f7b2007-06-17 19:56:38 -05002336lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05002337{
James Smart92d7f7b2007-06-17 19:56:38 -05002338 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002339 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002340 int i;
James Smart14691152006-12-02 13:34:28 -05002341 struct lpfc_dmabuf *mp;
2342 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002343 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002344
James Smart92d7f7b2007-06-17 19:56:38 -05002345 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002346 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05002347 case LA_1GHZ_LINK:
2348 phba->fc_linkspeed = LA_1GHZ_LINK;
2349 break;
2350 case LA_2GHZ_LINK:
2351 phba->fc_linkspeed = LA_2GHZ_LINK;
2352 break;
2353 case LA_4GHZ_LINK:
2354 phba->fc_linkspeed = LA_4GHZ_LINK;
2355 break;
2356 case LA_8GHZ_LINK:
2357 phba->fc_linkspeed = LA_8GHZ_LINK;
2358 break;
James Smartf4b4c682009-05-22 14:53:12 -04002359 case LA_10GHZ_LINK:
2360 phba->fc_linkspeed = LA_10GHZ_LINK;
2361 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002362 default:
2363 phba->fc_linkspeed = LA_UNKNW_LINK;
2364 break;
dea31012005-04-17 16:05:31 -05002365 }
2366
2367 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05002368 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05002369
2370 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05002371 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05002372
James Smart6a9c52c2009-10-02 15:16:51 -04002373 /* if npiv is enabled and this adapter supports npiv log
2374 * a message that npiv is not supported in this topology
2375 */
2376 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04002377 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2378 "1309 Link Up Event npiv not supported in loop "
2379 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002380 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05002381 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05002382 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002383
James Smart2e0fef82007-06-17 19:56:36 -05002384 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05002385 i = la->un.lilpBde64.tus.f.bdeSize;
2386
2387 if (i == 0) {
2388 phba->alpa_map[0] = 0;
2389 } else {
James Smarte8b62012007-08-02 11:10:09 -04002390 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05002391 int numalpa, j, k;
2392 union {
2393 uint8_t pamap[16];
2394 struct {
2395 uint32_t wd1;
2396 uint32_t wd2;
2397 uint32_t wd3;
2398 uint32_t wd4;
2399 } pa;
2400 } un;
2401 numalpa = phba->alpa_map[0];
2402 j = 0;
2403 while (j < numalpa) {
2404 memset(un.pamap, 0, 16);
2405 for (k = 1; j < numalpa; k++) {
2406 un.pamap[k - 1] =
2407 phba->alpa_map[j + 1];
2408 j++;
2409 if (k == 16)
2410 break;
2411 }
2412 /* Link Up Event ALPA map */
2413 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05002414 KERN_WARNING,
2415 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002416 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05002417 "ALPA map Data: x%x "
2418 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002419 un.pa.wd1, un.pa.wd2,
2420 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05002421 }
2422 }
2423 }
2424 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05002425 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04002426 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05002427 (phba->sli_rev == 3))
2428 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
2429 }
James Smart2e0fef82007-06-17 19:56:36 -05002430 vport->fc_myDID = phba->fc_pref_DID;
2431 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002432 }
James Smart92d7f7b2007-06-17 19:56:38 -05002433 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002434
2435 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05002436 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2437 if (!sparam_mbox)
2438 goto out;
2439
2440 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
2441 if (rc) {
2442 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2443 goto out;
2444 }
2445 sparam_mbox->vport = vport;
2446 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
2447 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
2448 if (rc == MBX_NOT_FINISHED) {
2449 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2450 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2451 kfree(mp);
2452 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2453 goto out;
dea31012005-04-17 16:05:31 -05002454 }
2455
James Smart6fb120a2009-05-22 14:52:59 -04002456 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2457 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2458 if (!cfglink_mbox)
2459 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05002460 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002461 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002462 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002463 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002464 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002465 if (rc == MBX_NOT_FINISHED) {
2466 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2467 goto out;
2468 }
2469 } else {
James Smart32b97932009-07-19 10:01:21 -04002470 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002471 /*
2472 * Add the driver's default FCF record at FCF index 0 now. This
2473 * is phase 1 implementation that support FCF index 0 and driver
2474 * defaults.
2475 */
James Smart45ed1192009-10-02 15:17:02 -04002476 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002477 fcf_record = kzalloc(sizeof(struct fcf_record),
2478 GFP_KERNEL);
2479 if (unlikely(!fcf_record)) {
2480 lpfc_printf_log(phba, KERN_ERR,
2481 LOG_MBOX | LOG_SLI,
2482 "2554 Could not allocate memmory for "
2483 "fcf record\n");
2484 rc = -ENODEV;
2485 goto out;
2486 }
2487
2488 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2489 LPFC_FCOE_FCF_DEF_INDEX);
2490 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2491 if (unlikely(rc)) {
2492 lpfc_printf_log(phba, KERN_ERR,
2493 LOG_MBOX | LOG_SLI,
2494 "2013 Could not manually add FCF "
2495 "record 0, status %d\n", rc);
2496 rc = -ENODEV;
2497 kfree(fcf_record);
2498 goto out;
2499 }
2500 kfree(fcf_record);
2501 }
2502 /*
2503 * The driver is expected to do FIP/FCF. Call the port
2504 * and get the FCF Table.
2505 */
James Smart32b97932009-07-19 10:01:21 -04002506 spin_lock_irq(&phba->hbalock);
2507 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2508 spin_unlock_irq(&phba->hbalock);
2509 return;
2510 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002511 /* This is the initial FCF discovery scan */
2512 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04002513 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002514 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2515 "2778 Start FCF table scan at linkup\n");
2516
2517 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2518 LPFC_FCOE_FCF_GET_FIRST);
2519 if (rc) {
2520 spin_lock_irq(&phba->hbalock);
2521 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
2522 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002523 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002524 }
dea31012005-04-17 16:05:31 -05002525 }
James Smart6fb120a2009-05-22 14:52:59 -04002526
2527 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002528out:
2529 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002530 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2531 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2532 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002533 lpfc_issue_clear_la(phba, vport);
2534 return;
dea31012005-04-17 16:05:31 -05002535}
2536
2537static void
James Smart84774a42008-08-24 21:50:06 -04002538lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002539{
dea31012005-04-17 16:05:31 -05002540 uint32_t control;
2541 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002542 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002543 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002544 if (phba->sli_rev <= LPFC_SLI_REV3) {
2545 control = readl(phba->HCregaddr);
2546 control |= HC_LAINT_ENA;
2547 writel(control, phba->HCregaddr);
2548 readl(phba->HCregaddr); /* flush */
2549 }
James Smart2e0fef82007-06-17 19:56:36 -05002550 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002551}
2552
James Smart84774a42008-08-24 21:50:06 -04002553static void
2554lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2555{
2556 lpfc_linkdown(phba);
2557 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002558 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002559 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2560}
2561
2562
dea31012005-04-17 16:05:31 -05002563/*
2564 * This routine handles processing a READ_LA mailbox
2565 * command upon completion. It is setup in the LPFC_MBOXQ
2566 * as the completion routine when the command is
2567 * handed off to the SLI layer.
2568 */
2569void
James Smart2e0fef82007-06-17 19:56:36 -05002570lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002571{
James Smart2e0fef82007-06-17 19:56:36 -05002572 struct lpfc_vport *vport = pmb->vport;
2573 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002574 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002575 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002576 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2577
James Smart0d2b6b82008-06-14 22:52:47 -04002578 /* Unblock ELS traffic */
2579 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002580 /* Check for error */
2581 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002582 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002583 "1307 READ_LA mbox error x%x state x%x\n",
2584 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002585 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002586 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002587 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2588 }
2589
James Smart04c68492009-05-22 14:52:52 -04002590 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002591
2592 memcpy(&phba->alpa_map[0], mp->virt, 128);
2593
James Smart2e0fef82007-06-17 19:56:36 -05002594 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002595 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002596 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002597 else
James Smart2e0fef82007-06-17 19:56:36 -05002598 vport->fc_flag &= ~FC_BYPASSED_MODE;
2599 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002600
James Smart0c287582009-06-10 17:22:56 -04002601 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002602 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002603 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002604 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002605 if (phba->fc_eventTag != 0)
2606 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002607 }
dea31012005-04-17 16:05:31 -05002608
2609 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05002610 spin_lock_irq(&phba->hbalock);
James Smart84774a42008-08-24 21:50:06 -04002611 if (la->mm)
2612 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2613 else
2614 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05002615 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002616
James Smart4d9ab992009-10-02 15:16:39 -04002617 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002618 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002619 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002620 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002621 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002622 "1306 Link Up Event in loop back mode "
2623 "x%x received Data: x%x x%x x%x x%x\n",
2624 la->eventTag, phba->fc_eventTag,
2625 la->granted_AL_PA, la->UlnkSpeed,
2626 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002627 } else {
2628 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002629 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002630 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002631 la->eventTag, phba->fc_eventTag,
2632 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002633 phba->alpa_map[0],
2634 la->mm, la->fa,
2635 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002636 }
James Smart92d7f7b2007-06-17 19:56:38 -05002637 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002638 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002639 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002640 if (phba->link_flag & LS_LOOPBACK_MODE) {
2641 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2642 "1308 Link Down Event in loop back mode "
2643 "x%x received "
2644 "Data: x%x x%x x%x\n",
2645 la->eventTag, phba->fc_eventTag,
2646 phba->pport->port_state, vport->fc_flag);
2647 }
2648 else {
2649 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002650 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002651 "Data: x%x x%x x%x x%x x%x\n",
2652 la->eventTag, phba->fc_eventTag,
2653 phba->pport->port_state, vport->fc_flag,
2654 la->mm, la->fa);
2655 }
2656 lpfc_mbx_issue_link_down(phba);
2657 }
2658 if (la->mm && la->attType == AT_LINK_UP) {
2659 if (phba->link_state != LPFC_LINK_DOWN) {
2660 phba->fc_stat.LinkDown++;
2661 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2662 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002663 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002664 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002665 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002666 lpfc_mbx_issue_link_down(phba);
2667 } else
2668 lpfc_enable_la(phba);
2669
2670 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2671 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2672 "Data: x%x x%x x%x\n",
2673 la->eventTag, phba->fc_eventTag,
2674 phba->pport->port_state, vport->fc_flag);
2675 /*
2676 * The cmnd that triggered this will be waiting for this
2677 * signal.
2678 */
2679 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2680 if (phba->wait_4_mlo_maint_flg) {
2681 phba->wait_4_mlo_maint_flg = 0;
2682 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002683 }
James Smart84774a42008-08-24 21:50:06 -04002684 }
2685
2686 if (la->fa) {
2687 if (la->mm)
2688 lpfc_issue_clear_la(phba, vport);
2689 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2690 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002691 }
2692
2693lpfc_mbx_cmpl_read_la_free_mbuf:
2694 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2695 kfree(mp);
2696 mempool_free(pmb, phba->mbox_mem_pool);
2697 return;
2698}
2699
2700/*
2701 * This routine handles processing a REG_LOGIN mailbox
2702 * command upon completion. It is setup in the LPFC_MBOXQ
2703 * as the completion routine when the command is
2704 * handed off to the SLI layer.
2705 */
2706void
James Smart2e0fef82007-06-17 19:56:36 -05002707lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002708{
James Smart2e0fef82007-06-17 19:56:36 -05002709 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002710 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002711 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05002712
dea31012005-04-17 16:05:31 -05002713 pmb->context1 = NULL;
2714
2715 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05002716 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05002717 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2718 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002719 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002720 /* decrement the node reference count held for this callback
2721 * function.
2722 */
James Smart329f9bc2007-04-25 09:53:01 -04002723 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002724
2725 return;
2726}
2727
James Smart92d7f7b2007-06-17 19:56:38 -05002728static void
2729lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2730{
James Smart04c68492009-05-22 14:52:52 -04002731 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002732 struct lpfc_vport *vport = pmb->vport;
2733 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2734
2735 switch (mb->mbxStatus) {
2736 case 0x0011:
2737 case 0x0020:
2738 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04002739 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2740 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2741 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002742 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002743 }
James Smart72100cc2010-02-12 14:43:01 -05002744 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002745 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05002746 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002747 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002748 vport->unreg_vpi_cmpl = VPORT_OK;
2749 mempool_free(pmb, phba->mbox_mem_pool);
2750 /*
2751 * This shost reference might have been taken at the beginning of
2752 * lpfc_vport_delete()
2753 */
James Smart1c6f4ef52009-11-18 15:40:49 -05002754 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05002755 scsi_host_put(shost);
2756}
2757
James Smartd7c255b2008-08-24 21:50:00 -04002758int
James Smart92d7f7b2007-06-17 19:56:38 -05002759lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2760{
2761 struct lpfc_hba *phba = vport->phba;
2762 LPFC_MBOXQ_t *mbox;
2763 int rc;
2764
2765 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2766 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002767 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002768
2769 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2770 mbox->vport = vport;
2771 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002772 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002773 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002774 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2775 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002776 mempool_free(mbox, phba->mbox_mem_pool);
2777 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002778 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002779 }
James Smartd7c255b2008-08-24 21:50:00 -04002780 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002781}
2782
2783static void
2784lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2785{
2786 struct lpfc_vport *vport = pmb->vport;
2787 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002788 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002789
2790 switch (mb->mbxStatus) {
2791 case 0x0011:
2792 case 0x9601:
2793 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002794 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2795 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2796 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002797 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2798 spin_lock_irq(shost->host_lock);
2799 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2800 spin_unlock_irq(shost->host_lock);
2801 vport->fc_myDID = 0;
2802 goto out;
2803 }
James Smart92d7f7b2007-06-17 19:56:38 -05002804
James Smart72100cc2010-02-12 14:43:01 -05002805 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002806 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002807 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002808 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002809 vport->num_disc_nodes = 0;
2810 /* go thru NPR list and issue ELS PLOGIs */
2811 if (vport->fc_npr_cnt)
2812 lpfc_els_disc_plogi(vport);
2813
2814 if (!vport->num_disc_nodes) {
2815 spin_lock_irq(shost->host_lock);
2816 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2817 spin_unlock_irq(shost->host_lock);
2818 lpfc_can_disctmo(vport);
2819 }
2820 vport->port_state = LPFC_VPORT_READY;
2821
2822out:
2823 mempool_free(pmb, phba->mbox_mem_pool);
2824 return;
2825}
2826
James Smart21e9a0a2009-05-22 14:53:21 -04002827/**
2828 * lpfc_create_static_vport - Read HBA config region to create static vports.
2829 * @phba: pointer to lpfc hba data structure.
2830 *
2831 * This routine issue a DUMP mailbox command for config region 22 to get
2832 * the list of static vports to be created. The function create vports
2833 * based on the information returned from the HBA.
2834 **/
2835void
2836lpfc_create_static_vport(struct lpfc_hba *phba)
2837{
2838 LPFC_MBOXQ_t *pmb = NULL;
2839 MAILBOX_t *mb;
2840 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04002841 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04002842 struct fc_vport_identifiers vport_id;
2843 struct fc_vport *new_fc_vport;
2844 struct Scsi_Host *shost;
2845 struct lpfc_vport *vport;
2846 uint16_t offset = 0;
2847 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04002848 struct lpfc_dmabuf *mp;
2849 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04002850
2851 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2852 if (!pmb) {
2853 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2854 "0542 lpfc_create_static_vport failed to"
2855 " allocate mailbox memory\n");
2856 return;
2857 }
2858
2859 mb = &pmb->u.mb;
2860
2861 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2862 if (!vport_info) {
2863 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2864 "0543 lpfc_create_static_vport failed to"
2865 " allocate vport_info\n");
2866 mempool_free(pmb, phba->mbox_mem_pool);
2867 return;
2868 }
2869
2870 vport_buff = (uint8_t *) vport_info;
2871 do {
James Smart1c6834a2009-07-19 10:01:26 -04002872 if (lpfc_dump_static_vport(phba, pmb, offset))
2873 goto out;
2874
James Smart21e9a0a2009-05-22 14:53:21 -04002875 pmb->vport = phba->pport;
2876 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2877
2878 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2879 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2880 "0544 lpfc_create_static_vport failed to"
2881 " issue dump mailbox command ret 0x%x "
2882 "status 0x%x\n",
2883 rc, mb->mbxStatus);
2884 goto out;
2885 }
2886
James Smart1c6834a2009-07-19 10:01:26 -04002887 if (phba->sli_rev == LPFC_SLI_REV4) {
2888 byte_count = pmb->u.mqe.un.mb_words[5];
2889 mp = (struct lpfc_dmabuf *) pmb->context2;
2890 if (byte_count > sizeof(struct static_vport_info) -
2891 offset)
2892 byte_count = sizeof(struct static_vport_info)
2893 - offset;
2894 memcpy(vport_buff + offset, mp->virt, byte_count);
2895 offset += byte_count;
2896 } else {
2897 if (mb->un.varDmp.word_cnt >
2898 sizeof(struct static_vport_info) - offset)
2899 mb->un.varDmp.word_cnt =
2900 sizeof(struct static_vport_info)
2901 - offset;
2902 byte_count = mb->un.varDmp.word_cnt;
2903 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2904 vport_buff + offset,
2905 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04002906
James Smart1c6834a2009-07-19 10:01:26 -04002907 offset += byte_count;
2908 }
James Smart21e9a0a2009-05-22 14:53:21 -04002909
James Smart1c6834a2009-07-19 10:01:26 -04002910 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04002911 offset < sizeof(struct static_vport_info));
2912
2913
2914 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2915 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2916 != VPORT_INFO_REV)) {
2917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2918 "0545 lpfc_create_static_vport bad"
2919 " information header 0x%x 0x%x\n",
2920 le32_to_cpu(vport_info->signature),
2921 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2922
2923 goto out;
2924 }
2925
2926 shost = lpfc_shost_from_vport(phba->pport);
2927
2928 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2929 memset(&vport_id, 0, sizeof(vport_id));
2930 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2931 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2932 if (!vport_id.port_name || !vport_id.node_name)
2933 continue;
2934
2935 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2936 vport_id.vport_type = FC_PORTTYPE_NPIV;
2937 vport_id.disable = false;
2938 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2939
2940 if (!new_fc_vport) {
2941 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2942 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04002943 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04002944 continue;
2945 }
2946
2947 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2948 vport->vport_flag |= STATIC_VPORT;
2949 }
2950
2951out:
James Smart21e9a0a2009-05-22 14:53:21 -04002952 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04002953 if (rc != MBX_TIMEOUT) {
2954 if (pmb->context2) {
2955 mp = (struct lpfc_dmabuf *) pmb->context2;
2956 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2957 kfree(mp);
2958 }
James Smart21e9a0a2009-05-22 14:53:21 -04002959 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002960 }
James Smart21e9a0a2009-05-22 14:53:21 -04002961
2962 return;
2963}
2964
dea31012005-04-17 16:05:31 -05002965/*
2966 * This routine handles processing a Fabric REG_LOGIN mailbox
2967 * command upon completion. It is setup in the LPFC_MBOXQ
2968 * as the completion routine when the command is
2969 * handed off to the SLI layer.
2970 */
2971void
James Smart2e0fef82007-06-17 19:56:36 -05002972lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002973{
James Smart92d7f7b2007-06-17 19:56:38 -05002974 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04002975 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002976 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05002977 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002978
James Smart549e55c2007-08-02 11:09:51 -04002979 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04002980 pmb->context1 = NULL;
2981 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05002982 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04002983 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2984 "0258 Register Fabric login error: 0x%x\n",
2985 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05002986 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2987 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002988 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002989
James Smart92d7f7b2007-06-17 19:56:38 -05002990 if (phba->fc_topology == TOPOLOGY_LOOP) {
2991 /* FLOGI failed, use loop map to make discovery list */
2992 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002993
James Smart92d7f7b2007-06-17 19:56:38 -05002994 /* Start discovery */
2995 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05002996 /* Decrement the reference count to ndlp after the
2997 * reference to the ndlp are done.
2998 */
2999 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003000 return;
3001 }
3002
3003 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003004 /* Decrement the reference count to ndlp after the reference
3005 * to the ndlp are done.
3006 */
3007 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003008 return;
3009 }
3010
dea31012005-04-17 16:05:31 -05003011 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003012 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003013 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003014 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003015
James Smart2e0fef82007-06-17 19:56:36 -05003016 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart21e9a0a2009-05-22 14:53:21 -04003017 lpfc_start_fdiscs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003018 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003019 }
3020
3021 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3022 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003023 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003024
3025 /* Drop the reference count from the mbox at the end after
3026 * all the current reference to the ndlp have been done.
3027 */
3028 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003029 return;
3030}
3031
3032/*
3033 * This routine handles processing a NameServer REG_LOGIN mailbox
3034 * command upon completion. It is setup in the LPFC_MBOXQ
3035 * as the completion routine when the command is
3036 * handed off to the SLI layer.
3037 */
3038void
James Smart2e0fef82007-06-17 19:56:36 -05003039lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003040{
James Smart21e9a0a2009-05-22 14:53:21 -04003041 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003042 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3043 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3044 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003045
3046 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003047out:
James Smart21e9a0a2009-05-22 14:53:21 -04003048 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3049 "0260 Register NameServer error: 0x%x\n",
3050 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003051 /* decrement the node reference count held for this
3052 * callback function.
3053 */
James Smart329f9bc2007-04-25 09:53:01 -04003054 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003055 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3056 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003057 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003058
3059 /* If no other thread is using the ndlp, free it */
3060 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003061
James Smart92d7f7b2007-06-17 19:56:38 -05003062 if (phba->fc_topology == TOPOLOGY_LOOP) {
3063 /*
3064 * RegLogin failed, use loop map to make discovery
3065 * list
3066 */
3067 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003068
James Smart92d7f7b2007-06-17 19:56:38 -05003069 /* Start discovery */
3070 lpfc_disc_start(vport);
3071 return;
3072 }
3073 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003074 return;
3075 }
3076
3077 pmb->context1 = NULL;
3078
dea31012005-04-17 16:05:31 -05003079 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003080 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003081 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003082 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003083
James Smart2e0fef82007-06-17 19:56:36 -05003084 if (vport->port_state < LPFC_VPORT_READY) {
3085 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003086 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3087 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3088 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3089 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3090 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3091
3092 /* Issue SCR just before NameServer GID_FT Query */
3093 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003094 }
3095
James Smart2e0fef82007-06-17 19:56:36 -05003096 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003097 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003098 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003099 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003100 goto out;
dea31012005-04-17 16:05:31 -05003101 }
3102
James Smartfa4066b2008-01-11 01:53:27 -05003103 /* decrement the node reference count held for this
3104 * callback function.
3105 */
James Smart329f9bc2007-04-25 09:53:01 -04003106 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003107 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3108 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003109 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003110
3111 return;
3112}
3113
3114static void
James Smart2e0fef82007-06-17 19:56:36 -05003115lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003116{
James Smart2e0fef82007-06-17 19:56:36 -05003117 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3118 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003119 struct lpfc_rport_data *rdata;
3120 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003121 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003122
3123 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003124 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3125 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003126 rport_ids.port_id = ndlp->nlp_DID;
3127 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003128
James Smart329f9bc2007-04-25 09:53:01 -04003129 /*
3130 * We leave our node pointer in rport->dd_data when we unregister a
3131 * FCP target port. But fc_remote_port_add zeros the space to which
3132 * rport->dd_data points. So, if we're reusing a previously
3133 * registered port, drop the reference that we took the last time we
3134 * registered the port.
3135 */
3136 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003137 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003138 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003139
3140 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3141 "rport add: did:x%x flg:x%x type x%x",
3142 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3143
James Smart2e0fef82007-06-17 19:56:36 -05003144 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003145 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003146 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3147 "Warning: fc_remote_port_add failed\n");
3148 return;
3149 }
3150
3151 /* initialize static port data */
3152 rport->maxframe_size = ndlp->nlp_maxframe;
3153 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003154 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003155 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003156
3157 if (ndlp->nlp_type & NLP_FCP_TARGET)
3158 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3159 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3160 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3161
3162
3163 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3164 fc_remote_port_rolechg(rport, rport_ids.roles);
3165
James Smart071fbd3d2006-04-15 11:53:20 -04003166 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003167 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003168 ndlp->nlp_sid = rport->scsi_target_id;
3169 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003170 return;
3171}
3172
3173static void
James Smart2e0fef82007-06-17 19:56:36 -05003174lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003175{
3176 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003177
James Smart858c9f62007-06-17 19:56:39 -05003178 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3179 "rport delete: did:x%x flg:x%x type x%x",
3180 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3181
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003182 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003183
3184 return;
3185}
3186
James Smartde0c5b32007-04-25 09:52:27 -04003187static void
James Smart2e0fef82007-06-17 19:56:36 -05003188lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003189{
James Smart2e0fef82007-06-17 19:56:36 -05003190 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3191
3192 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003193 switch (state) {
3194 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003195 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003196 break;
3197 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003198 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003199 break;
3200 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003201 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003202 break;
3203 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003204 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003205 break;
3206 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003207 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003208 break;
3209 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003210 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003211 break;
3212 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003213 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003214 break;
3215 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003216 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003217 break;
3218 }
James Smart2e0fef82007-06-17 19:56:36 -05003219 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003220}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003221
James Smartde0c5b32007-04-25 09:52:27 -04003222static void
James Smart2e0fef82007-06-17 19:56:36 -05003223lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003224 int old_state, int new_state)
3225{
James Smart2e0fef82007-06-17 19:56:36 -05003226 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3227
James Smartde0c5b32007-04-25 09:52:27 -04003228 if (new_state == NLP_STE_UNMAPPED_NODE) {
3229 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
3230 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3231 ndlp->nlp_type |= NLP_FC_NODE;
3232 }
3233 if (new_state == NLP_STE_MAPPED_NODE)
3234 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3235 if (new_state == NLP_STE_NPR_NODE)
3236 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003237
James Smartde0c5b32007-04-25 09:52:27 -04003238 /* Transport interface */
3239 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3240 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003241 vport->phba->nport_event_cnt++;
3242 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003243 }
dea31012005-04-17 16:05:31 -05003244
James Smartde0c5b32007-04-25 09:52:27 -04003245 if (new_state == NLP_STE_MAPPED_NODE ||
3246 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003247 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003248 /*
3249 * Tell the fc transport about the port, if we haven't
3250 * already. If we have, and it's a scsi entity, be
3251 * sure to unblock any attached scsi devices
3252 */
3253 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003254 }
James Smartea2151b2008-09-07 11:52:10 -04003255 if ((new_state == NLP_STE_MAPPED_NODE) &&
3256 (vport->stat_data_enabled)) {
3257 /*
3258 * A new target is discovered, if there is no buffer for
3259 * statistical data collection allocate buffer.
3260 */
3261 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3262 sizeof(struct lpfc_scsicmd_bkt),
3263 GFP_KERNEL);
3264
3265 if (!ndlp->lat_data)
3266 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3267 "0286 lpfc_nlp_state_cleanup failed to "
3268 "allocate statistical data buffer DID "
3269 "0x%x\n", ndlp->nlp_DID);
3270 }
James Smart858c9f62007-06-17 19:56:39 -05003271 /*
3272 * if we added to Mapped list, but the remote port
3273 * registration failed or assigned a target id outside
3274 * our presentable range - move the node to the
3275 * Unmapped List
3276 */
James Smartde0c5b32007-04-25 09:52:27 -04003277 if (new_state == NLP_STE_MAPPED_NODE &&
3278 (!ndlp->rport ||
3279 ndlp->rport->scsi_target_id == -1 ||
3280 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003281 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003282 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003283 spin_unlock_irq(shost->host_lock);
3284 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003285 }
James Smartde0c5b32007-04-25 09:52:27 -04003286}
3287
James Smart685f0bf2007-04-25 09:53:08 -04003288static char *
3289lpfc_nlp_state_name(char *buffer, size_t size, int state)
3290{
3291 static char *states[] = {
3292 [NLP_STE_UNUSED_NODE] = "UNUSED",
3293 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
3294 [NLP_STE_ADISC_ISSUE] = "ADISC",
3295 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
3296 [NLP_STE_PRLI_ISSUE] = "PRLI",
3297 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
3298 [NLP_STE_MAPPED_NODE] = "MAPPED",
3299 [NLP_STE_NPR_NODE] = "NPR",
3300 };
3301
James Smart311464e2007-08-02 11:10:37 -04003302 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04003303 strlcpy(buffer, states[state], size);
3304 else
3305 snprintf(buffer, size, "unknown (%d)", state);
3306 return buffer;
3307}
3308
James Smartde0c5b32007-04-25 09:52:27 -04003309void
James Smart2e0fef82007-06-17 19:56:36 -05003310lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3311 int state)
James Smartde0c5b32007-04-25 09:52:27 -04003312{
James Smart2e0fef82007-06-17 19:56:36 -05003313 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04003314 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04003315 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04003316
James Smarte8b62012007-08-02 11:10:09 -04003317 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3318 "0904 NPort state transition x%06x, %s -> %s\n",
3319 ndlp->nlp_DID,
3320 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
3321 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05003322
3323 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3324 "node statechg did:x%x old:%d ste:%d",
3325 ndlp->nlp_DID, old_state, state);
3326
James Smartde0c5b32007-04-25 09:52:27 -04003327 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04003328 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05003329 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003330 if (old_state == NLP_STE_UNMAPPED_NODE) {
3331 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
3332 ndlp->nlp_type &= ~NLP_FC_NODE;
3333 }
3334
James Smart685f0bf2007-04-25 09:53:08 -04003335 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05003336 spin_lock_irq(shost->host_lock);
3337 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3338 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003339 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05003340 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04003341
3342 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05003343 lpfc_nlp_counters(vport, state, 1);
3344 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04003345}
3346
3347void
James Smarte47c9092008-02-08 18:49:26 -05003348lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3349{
3350 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3351
3352 if (list_empty(&ndlp->nlp_listp)) {
3353 spin_lock_irq(shost->host_lock);
3354 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3355 spin_unlock_irq(shost->host_lock);
3356 }
3357}
3358
3359void
James Smart2e0fef82007-06-17 19:56:36 -05003360lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003361{
James Smart2e0fef82007-06-17 19:56:36 -05003362 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3363
James Smart0d2b6b82008-06-14 22:52:47 -04003364 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003365 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05003366 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3367 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003368 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05003369 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003370 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05003371 NLP_STE_UNUSED_NODE);
3372}
3373
Adrian Bunk4d9db012008-02-14 23:24:02 +02003374static void
James Smarte47c9092008-02-08 18:49:26 -05003375lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3376{
James Smart0d2b6b82008-06-14 22:52:47 -04003377 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003378 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3379 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3380 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3381 NLP_STE_UNUSED_NODE);
3382}
James Smart109f6ed2008-12-04 22:39:08 -05003383/**
James Smart3621a712009-04-06 18:47:14 -04003384 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05003385 * @vport: Pointer to Virtual Port object.
3386 * @ndlp: Pointer to FC node object.
3387 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04003388 *
3389 * This function is always called when node object need to be initialized.
3390 * It initializes all the fields of the node object. Although the reference
3391 * to phba from @ndlp can be obtained indirectly through it's reference to
3392 * @vport, a direct reference to phba is taken here by @ndlp. This is due
3393 * to the life-span of the @ndlp might go beyond the existence of @vport as
3394 * the final release of ndlp is determined by its reference count. And, the
3395 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05003396 **/
3397static inline void
3398lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3399 uint32_t did)
3400{
3401 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3402 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3403 init_timer(&ndlp->nlp_delayfunc);
3404 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3405 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3406 ndlp->nlp_DID = did;
3407 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04003408 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05003409 ndlp->nlp_sid = NLP_NO_SID;
3410 kref_init(&ndlp->kref);
3411 NLP_INT_NODE_ACT(ndlp);
3412 atomic_set(&ndlp->cmd_pending, 0);
3413 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
3414}
James Smarte47c9092008-02-08 18:49:26 -05003415
3416struct lpfc_nodelist *
3417lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3418 int state)
3419{
3420 struct lpfc_hba *phba = vport->phba;
3421 uint32_t did;
3422 unsigned long flags;
3423
3424 if (!ndlp)
3425 return NULL;
3426
3427 spin_lock_irqsave(&phba->ndlp_lock, flags);
3428 /* The ndlp should not be in memory free mode */
3429 if (NLP_CHK_FREE_REQ(ndlp)) {
3430 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3431 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3432 "0277 lpfc_enable_node: ndlp:x%p "
3433 "usgmap:x%x refcnt:%d\n",
3434 (void *)ndlp, ndlp->nlp_usg_map,
3435 atomic_read(&ndlp->kref.refcount));
3436 return NULL;
3437 }
3438 /* The ndlp should not already be in active mode */
3439 if (NLP_CHK_NODE_ACT(ndlp)) {
3440 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3441 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3442 "0278 lpfc_enable_node: ndlp:x%p "
3443 "usgmap:x%x refcnt:%d\n",
3444 (void *)ndlp, ndlp->nlp_usg_map,
3445 atomic_read(&ndlp->kref.refcount));
3446 return NULL;
3447 }
3448
3449 /* Keep the original DID */
3450 did = ndlp->nlp_DID;
3451
3452 /* re-initialize ndlp except of ndlp linked list pointer */
3453 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3454 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003455 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003456
3457 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3458
3459 if (state != NLP_STE_UNUSED_NODE)
3460 lpfc_nlp_set_state(vport, ndlp, state);
3461
3462 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3463 "node enable: did:x%x",
3464 ndlp->nlp_DID, 0, 0);
3465 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003466}
3467
3468void
James Smart2e0fef82007-06-17 19:56:36 -05003469lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003470{
James Smart87af33f2007-10-27 13:37:43 -04003471 /*
James Smartfa4066b2008-01-11 01:53:27 -05003472 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003473 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003474 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3475 * until ALL other outstanding threads have completed. We check
3476 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003477 */
James Smartfa4066b2008-01-11 01:53:27 -05003478 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3479 return;
James Smart51ef4c22007-08-02 11:10:31 -04003480 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003481 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003482 return;
dea31012005-04-17 16:05:31 -05003483}
3484
3485/*
3486 * Start / ReStart rescue timer for Discovery / RSCN handling
3487 */
3488void
James Smart2e0fef82007-06-17 19:56:36 -05003489lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003490{
James Smart2e0fef82007-06-17 19:56:36 -05003491 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3492 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003493 uint32_t tmo;
3494
James Smart2e0fef82007-06-17 19:56:36 -05003495 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003496 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003497 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3498 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003499 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003500 * FC spec states we need 3 * ratov for CT requests
3501 */
3502 tmo = ((phba->fc_ratov * 3) + 3);
3503 }
dea31012005-04-17 16:05:31 -05003504
James Smart858c9f62007-06-17 19:56:39 -05003505
3506 if (!timer_pending(&vport->fc_disctmo)) {
3507 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3508 "set disc timer: tmo:x%x state:x%x flg:x%x",
3509 tmo, vport->port_state, vport->fc_flag);
3510 }
3511
James Smart2e0fef82007-06-17 19:56:36 -05003512 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3513 spin_lock_irq(shost->host_lock);
3514 vport->fc_flag |= FC_DISC_TMO;
3515 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003516
3517 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003518 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3519 "0247 Start Discovery Timer state x%x "
3520 "Data: x%x x%lx x%x x%x\n",
3521 vport->port_state, tmo,
3522 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3523 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003524
3525 return;
3526}
3527
3528/*
3529 * Cancel rescue timer for Discovery / RSCN handling
3530 */
3531int
James Smart2e0fef82007-06-17 19:56:36 -05003532lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003533{
James Smart2e0fef82007-06-17 19:56:36 -05003534 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003535 unsigned long iflags;
3536
James Smart858c9f62007-06-17 19:56:39 -05003537 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3538 "can disc timer: state:x%x rtry:x%x flg:x%x",
3539 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3540
dea31012005-04-17 16:05:31 -05003541 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003542 if (vport->fc_flag & FC_DISC_TMO) {
3543 spin_lock_irqsave(shost->host_lock, iflags);
3544 vport->fc_flag &= ~FC_DISC_TMO;
3545 spin_unlock_irqrestore(shost->host_lock, iflags);
3546 del_timer_sync(&vport->fc_disctmo);
3547 spin_lock_irqsave(&vport->work_port_lock, iflags);
3548 vport->work_port_events &= ~WORKER_DISC_TMO;
3549 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003550 }
3551
3552 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003553 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3554 "0248 Cancel Discovery Timer state x%x "
3555 "Data: x%x x%x x%x\n",
3556 vport->port_state, vport->fc_flag,
3557 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003558 return 0;
dea31012005-04-17 16:05:31 -05003559}
3560
3561/*
3562 * Check specified ring for outstanding IOCB on the SLI queue
3563 * Return true if iocb matches the specified nport
3564 */
3565int
James Smart2e0fef82007-06-17 19:56:36 -05003566lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3567 struct lpfc_sli_ring *pring,
3568 struct lpfc_iocbq *iocb,
3569 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003570{
James Smart2e0fef82007-06-17 19:56:36 -05003571 struct lpfc_sli *psli = &phba->sli;
3572 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003573 struct lpfc_vport *vport = ndlp->vport;
3574
3575 if (iocb->vport != vport)
3576 return 0;
3577
dea31012005-04-17 16:05:31 -05003578 if (pring->ringno == LPFC_ELS_RING) {
3579 switch (icmd->ulpCommand) {
3580 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003581 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003582 return 1;
dea31012005-04-17 16:05:31 -05003583 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003584 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3585 return 1;
dea31012005-04-17 16:05:31 -05003586 case CMD_XMIT_ELS_RSP64_CX:
3587 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003588 return 1;
dea31012005-04-17 16:05:31 -05003589 }
James Smarta4bc3372006-12-02 13:34:16 -05003590 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003591
3592 } else if (pring->ringno == psli->fcp_ring) {
3593 /* Skip match check if waiting to relogin to FCP target */
3594 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003595 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003596 return 0;
dea31012005-04-17 16:05:31 -05003597 }
3598 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003599 return 1;
dea31012005-04-17 16:05:31 -05003600 }
3601 } else if (pring->ringno == psli->next_ring) {
3602
3603 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003604 return 0;
dea31012005-04-17 16:05:31 -05003605}
3606
3607/*
3608 * Free resources / clean up outstanding I/Os
3609 * associated with nlp_rpi in the LPFC_NODELIST entry.
3610 */
3611static int
James Smart2e0fef82007-06-17 19:56:36 -05003612lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003613{
James Smart2534ba72007-04-25 09:52:20 -04003614 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003615 struct lpfc_sli *psli;
3616 struct lpfc_sli_ring *pring;
3617 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003618 uint32_t i;
dea31012005-04-17 16:05:31 -05003619
James Smart92d7f7b2007-06-17 19:56:38 -05003620 lpfc_fabric_abort_nport(ndlp);
3621
dea31012005-04-17 16:05:31 -05003622 /*
3623 * Everything that matches on txcmplq will be returned
3624 * by firmware with a no rpi error.
3625 */
3626 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003627 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003628 /* Now process each ring */
3629 for (i = 0; i < psli->num_rings; i++) {
3630 pring = &psli->ring[i];
3631
James Smart2e0fef82007-06-17 19:56:36 -05003632 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003633 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003634 list) {
dea31012005-04-17 16:05:31 -05003635 /*
3636 * Check to see if iocb matches the nport we are
3637 * looking for
3638 */
James Smart92d7f7b2007-06-17 19:56:38 -05003639 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3640 ndlp))) {
dea31012005-04-17 16:05:31 -05003641 /* It matches, so deque and call compl
3642 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003643 list_move_tail(&iocb->list,
3644 &completions);
dea31012005-04-17 16:05:31 -05003645 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003646 }
3647 }
James Smart2e0fef82007-06-17 19:56:36 -05003648 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003649 }
3650 }
James Smart2534ba72007-04-25 09:52:20 -04003651
James Smarta257bf92009-04-06 18:48:10 -04003652 /* Cancel all the IOCBs from the completions list */
3653 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3654 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003655
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003656 return 0;
dea31012005-04-17 16:05:31 -05003657}
3658
3659/*
3660 * Free rpi associated with LPFC_NODELIST entry.
3661 * This routine is called from lpfc_freenode(), when we are removing
3662 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3663 * LOGO that completes successfully, and we are waiting to PLOGI back
3664 * to the remote NPort. In addition, it is called after we receive
3665 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3666 * we are waiting to PLOGI back to the remote NPort.
3667 */
3668int
James Smart2e0fef82007-06-17 19:56:36 -05003669lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003670{
James Smart2e0fef82007-06-17 19:56:36 -05003671 struct lpfc_hba *phba = vport->phba;
3672 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003673 int rc;
3674
James Smart21e9a0a2009-05-22 14:53:21 -04003675 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003676 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3677 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003678 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003679 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003680 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003681 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003682 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003683 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003684 }
dea31012005-04-17 16:05:31 -05003685 lpfc_no_rpi(phba, ndlp);
3686 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003687 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003688 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003689 return 1;
3690 }
3691 return 0;
3692}
3693
James Smartecfd03c2010-02-12 14:41:27 -05003694/**
3695 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
3696 * @phba: pointer to lpfc hba data structure.
3697 *
3698 * This routine is invoked to unregister all the currently registered RPIs
3699 * to the HBA.
3700 **/
3701void
3702lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
3703{
3704 struct lpfc_vport **vports;
3705 struct lpfc_nodelist *ndlp;
3706 struct Scsi_Host *shost;
3707 int i;
3708
3709 vports = lpfc_create_vport_work_array(phba);
3710 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3711 shost = lpfc_shost_from_vport(vports[i]);
3712 spin_lock_irq(shost->host_lock);
3713 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003714 if (ndlp->nlp_flag & NLP_RPI_VALID) {
3715 /* The mempool_alloc might sleep */
3716 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05003717 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05003718 spin_lock_irq(shost->host_lock);
3719 }
James Smartecfd03c2010-02-12 14:41:27 -05003720 }
3721 spin_unlock_irq(shost->host_lock);
3722 }
3723 lpfc_destroy_vport_work_array(phba, vports);
3724}
3725
James Smart92d7f7b2007-06-17 19:56:38 -05003726void
3727lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3728{
3729 struct lpfc_hba *phba = vport->phba;
3730 LPFC_MBOXQ_t *mbox;
3731 int rc;
3732
3733 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3734 if (mbox) {
3735 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3736 mbox->vport = vport;
3737 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003738 mbox->context1 = NULL;
3739 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003740 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003741 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003742
3743 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3744 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3745 "1836 Could not issue "
3746 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003747 }
3748}
3749
3750void
3751lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3752{
3753 struct lpfc_hba *phba = vport->phba;
3754 LPFC_MBOXQ_t *mbox;
3755 int rc;
3756
3757 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3758 if (mbox) {
3759 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3760 mbox->vport = vport;
3761 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003762 mbox->context1 = NULL;
3763 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003764 if (rc != MBX_TIMEOUT)
3765 mempool_free(mbox, phba->mbox_mem_pool);
3766
3767 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003768 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3769 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003770 "unreg_did (default rpis) status %d\n",
3771 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003772 }
3773}
3774
dea31012005-04-17 16:05:31 -05003775/*
3776 * Free resources associated with LPFC_NODELIST entry
3777 * so it can be freed.
3778 */
3779static int
James Smart2e0fef82007-06-17 19:56:36 -05003780lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003781{
James Smart2e0fef82007-06-17 19:56:36 -05003782 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3783 struct lpfc_hba *phba = vport->phba;
3784 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003785 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003786
3787 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003788 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3789 "0900 Cleanup node for NPort x%x "
3790 "Data: x%x x%x x%x\n",
3791 ndlp->nlp_DID, ndlp->nlp_flag,
3792 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003793 if (NLP_CHK_FREE_REQ(ndlp)) {
3794 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3795 "0280 lpfc_cleanup_node: ndlp:x%p "
3796 "usgmap:x%x refcnt:%d\n",
3797 (void *)ndlp, ndlp->nlp_usg_map,
3798 atomic_read(&ndlp->kref.refcount));
3799 lpfc_dequeue_node(vport, ndlp);
3800 } else {
3801 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3802 "0281 lpfc_cleanup_node: ndlp:x%p "
3803 "usgmap:x%x refcnt:%d\n",
3804 (void *)ndlp, ndlp->nlp_usg_map,
3805 atomic_read(&ndlp->kref.refcount));
3806 lpfc_disable_node(vport, ndlp);
3807 }
dea31012005-04-17 16:05:31 -05003808
dea31012005-04-17 16:05:31 -05003809 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3810 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003811 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003812 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3813 mb->context2 = NULL;
3814 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3815 }
3816 }
James Smart33ccf8d2006-08-17 11:57:58 -04003817
James Smart2e0fef82007-06-17 19:56:36 -05003818 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003819 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003820 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003821 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003822 mp = (struct lpfc_dmabuf *) (mb->context1);
3823 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003824 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003825 kfree(mp);
3826 }
3827 list_del(&mb->list);
3828 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003829 /* We shall not invoke the lpfc_nlp_put to decrement
3830 * the ndlp reference count as we are in the process
3831 * of lpfc_nlp_release.
3832 */
dea31012005-04-17 16:05:31 -05003833 }
3834 }
James Smart2e0fef82007-06-17 19:56:36 -05003835 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003836
James Smarte47c9092008-02-08 18:49:26 -05003837 lpfc_els_abort(phba, ndlp);
3838
James Smart2e0fef82007-06-17 19:56:36 -05003839 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003840 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003841 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003842
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003843 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003844 del_timer_sync(&ndlp->nlp_delayfunc);
3845
James Smart0d2b6b82008-06-14 22:52:47 -04003846 list_del_init(&ndlp->els_retry_evt.evt_listp);
3847 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003848
James Smart2e0fef82007-06-17 19:56:36 -05003849 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003850
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003851 return 0;
dea31012005-04-17 16:05:31 -05003852}
3853
3854/*
3855 * Check to see if we can free the nlp back to the freelist.
3856 * If we are in the middle of using the nlp in the discovery state
3857 * machine, defer the free till we reach the end of the state machine.
3858 */
James Smart329f9bc2007-04-25 09:53:01 -04003859static void
James Smart2e0fef82007-06-17 19:56:36 -05003860lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003861{
James Smarta8adb832007-10-27 13:37:53 -04003862 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003863 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003864 LPFC_MBOXQ_t *mbox;
3865 int rc;
dea31012005-04-17 16:05:31 -05003866
James Smart0d2b6b82008-06-14 22:52:47 -04003867 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003868 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3869 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003870 /* For this case we need to cleanup the default rpi
3871 * allocated by the firmware.
3872 */
3873 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3874 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003875 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003876 (uint8_t *) &vport->fc_sparam, mbox, 0);
3877 if (rc) {
3878 mempool_free(mbox, phba->mbox_mem_pool);
3879 }
3880 else {
3881 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3882 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3883 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003884 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003885 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3886 if (rc == MBX_NOT_FINISHED) {
3887 mempool_free(mbox, phba->mbox_mem_pool);
3888 }
3889 }
3890 }
3891 }
James Smart2e0fef82007-06-17 19:56:36 -05003892 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003893
James Smart2e0fef82007-06-17 19:56:36 -05003894 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003895 * We can get here with a non-NULL ndlp->rport because when we
3896 * unregister a rport we don't break the rport/node linkage. So if we
3897 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003898 */
James Smart92d7f7b2007-06-17 19:56:38 -05003899 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003900 rdata = ndlp->rport->dd_data;
3901 rdata->pnode = NULL;
3902 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003903 }
dea31012005-04-17 16:05:31 -05003904}
3905
3906static int
James Smart2e0fef82007-06-17 19:56:36 -05003907lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3908 uint32_t did)
dea31012005-04-17 16:05:31 -05003909{
James Smart2e0fef82007-06-17 19:56:36 -05003910 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003911
3912 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003913 return 0;
dea31012005-04-17 16:05:31 -05003914
dea31012005-04-17 16:05:31 -05003915 /* First check for Direct match */
3916 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003917 return 1;
dea31012005-04-17 16:05:31 -05003918
3919 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003920 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003921 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003922 return 0;
dea31012005-04-17 16:05:31 -05003923 }
3924
3925 matchdid.un.word = did;
3926 ndlpdid.un.word = ndlp->nlp_DID;
3927 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3928 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3929 (mydid.un.b.area == matchdid.un.b.area)) {
3930 if ((ndlpdid.un.b.domain == 0) &&
3931 (ndlpdid.un.b.area == 0)) {
3932 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003933 return 1;
dea31012005-04-17 16:05:31 -05003934 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003935 return 0;
dea31012005-04-17 16:05:31 -05003936 }
3937
3938 matchdid.un.word = ndlp->nlp_DID;
3939 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3940 (mydid.un.b.area == ndlpdid.un.b.area)) {
3941 if ((matchdid.un.b.domain == 0) &&
3942 (matchdid.un.b.area == 0)) {
3943 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003944 return 1;
dea31012005-04-17 16:05:31 -05003945 }
3946 }
3947 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003948 return 0;
dea31012005-04-17 16:05:31 -05003949}
3950
James Smart685f0bf2007-04-25 09:53:08 -04003951/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003952static struct lpfc_nodelist *
3953__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003954{
James Smart2fb9bd82006-12-02 13:33:57 -05003955 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003956 uint32_t data1;
3957
James Smart2e0fef82007-06-17 19:56:36 -05003958 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3959 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04003960 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3961 ((uint32_t) ndlp->nlp_xri << 16) |
3962 ((uint32_t) ndlp->nlp_type << 8) |
3963 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04003964 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3965 "0929 FIND node DID "
3966 "Data: x%p x%x x%x x%x\n",
3967 ndlp, ndlp->nlp_DID,
3968 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04003969 return ndlp;
dea31012005-04-17 16:05:31 -05003970 }
3971 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003972
dea31012005-04-17 16:05:31 -05003973 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04003974 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3975 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05003976 return NULL;
3977}
3978
3979struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003980lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003981{
James Smart2e0fef82007-06-17 19:56:36 -05003982 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003983 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003984
James Smart2e0fef82007-06-17 19:56:36 -05003985 spin_lock_irq(shost->host_lock);
3986 ndlp = __lpfc_findnode_did(vport, did);
3987 spin_unlock_irq(shost->host_lock);
3988 return ndlp;
3989}
3990
3991struct lpfc_nodelist *
3992lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3993{
3994 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3995 struct lpfc_nodelist *ndlp;
3996
3997 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003998 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05003999 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4000 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004001 return NULL;
4002 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004003 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004004 if (!ndlp)
4005 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004006 lpfc_nlp_init(vport, ndlp, did);
4007 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4008 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004009 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004010 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004011 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004012 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4013 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4014 if (!ndlp)
4015 return NULL;
4016 spin_lock_irq(shost->host_lock);
4017 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4018 spin_unlock_irq(shost->host_lock);
4019 return ndlp;
dea31012005-04-17 16:05:31 -05004020 }
James Smarte47c9092008-02-08 18:49:26 -05004021
James Smart58da1ff2008-04-07 10:15:56 -04004022 if ((vport->fc_flag & FC_RSCN_MODE) &&
4023 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004024 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04004025 /* If we've already recieved a PLOGI from this NPort
4026 * we don't need to try to discover it again.
4027 */
4028 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4029 return NULL;
4030
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004031 /* Since this node is marked for discovery,
4032 * delay timeout is not needed.
4033 */
James Smart0d2b6b82008-06-14 22:52:47 -04004034 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004035 spin_lock_irq(shost->host_lock);
4036 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4037 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004038 } else
dea31012005-04-17 16:05:31 -05004039 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004040 } else {
James Smart87af33f2007-10-27 13:37:43 -04004041 /* If we've already recieved a PLOGI from this NPort,
4042 * or we are already in the process of discovery on it,
4043 * we don't need to try to discover it again.
4044 */
James Smart685f0bf2007-04-25 09:53:08 -04004045 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004046 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4047 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004048 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004049 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4050 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004051 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004052 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004053 }
4054 return ndlp;
4055}
4056
4057/* Build a list of nodes to discover based on the loopmap */
4058void
James Smart2e0fef82007-06-17 19:56:36 -05004059lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004060{
James Smart2e0fef82007-06-17 19:56:36 -05004061 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004062 int j;
4063 uint32_t alpa, index;
4064
James Smart2e0fef82007-06-17 19:56:36 -05004065 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004066 return;
James Smart2e0fef82007-06-17 19:56:36 -05004067
4068 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004069 return;
dea31012005-04-17 16:05:31 -05004070
4071 /* Check for loop map present or not */
4072 if (phba->alpa_map[0]) {
4073 for (j = 1; j <= phba->alpa_map[0]; j++) {
4074 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004075 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004076 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004077 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004078 }
4079 } else {
4080 /* No alpamap, so try all alpa's */
4081 for (j = 0; j < FC_MAXLOOP; j++) {
4082 /* If cfg_scan_down is set, start from highest
4083 * ALPA (0xef) to lowest (0x1).
4084 */
James Smart3de2a652007-08-02 11:09:59 -04004085 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004086 index = j;
4087 else
4088 index = FC_MAXLOOP - j - 1;
4089 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004090 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004091 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004092 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004093 }
4094 }
4095 return;
4096}
4097
dea31012005-04-17 16:05:31 -05004098void
James Smart2e0fef82007-06-17 19:56:36 -05004099lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004100{
dea31012005-04-17 16:05:31 -05004101 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004102 struct lpfc_sli *psli = &phba->sli;
4103 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4104 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4105 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4106 int rc;
4107
James Smart92d7f7b2007-06-17 19:56:38 -05004108 /*
4109 * if it's not a physical port or if we already send
4110 * clear_la then don't send it.
4111 */
4112 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004113 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4114 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004115 return;
4116
James Smart2e0fef82007-06-17 19:56:36 -05004117 /* Link up discovery */
4118 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4119 phba->link_state = LPFC_CLEAR_LA;
4120 lpfc_clear_la(phba, mbox);
4121 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4122 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004123 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004124 if (rc == MBX_NOT_FINISHED) {
4125 mempool_free(mbox, phba->mbox_mem_pool);
4126 lpfc_disc_flush_list(vport);
4127 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4128 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4129 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004130 phba->link_state = LPFC_HBA_ERROR;
4131 }
4132 }
4133}
4134
4135/* Reg_vpi to tell firmware to resume normal operations */
4136void
4137lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4138{
4139 LPFC_MBOXQ_t *regvpimbox;
4140
4141 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4142 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004143 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004144 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4145 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004146 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004147 == MBX_NOT_FINISHED) {
4148 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004149 }
4150 }
4151}
4152
4153/* Start Link up / RSCN discovery on NPR nodes */
4154void
4155lpfc_disc_start(struct lpfc_vport *vport)
4156{
4157 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4158 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004159 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004160 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004161 int did_changed;
dea31012005-04-17 16:05:31 -05004162
James Smart2e0fef82007-06-17 19:56:36 -05004163 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004164 return;
James Smart2e0fef82007-06-17 19:56:36 -05004165
4166 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004167 clear_la_pending = 1;
4168 else
4169 clear_la_pending = 0;
4170
James Smart2e0fef82007-06-17 19:56:36 -05004171 if (vport->port_state < LPFC_VPORT_READY)
4172 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004173
James Smart2e0fef82007-06-17 19:56:36 -05004174 lpfc_set_disctmo(vport);
4175
4176 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004177 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004178 else
dea31012005-04-17 16:05:31 -05004179 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004180
4181 vport->fc_prevDID = vport->fc_myDID;
4182 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004183
4184 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004185 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4186 "0202 Start Discovery hba state x%x "
4187 "Data: x%x x%x x%x\n",
4188 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4189 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004190
4191 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004192 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004193
4194 if (num_sent)
4195 return;
4196
James Smart92d7f7b2007-06-17 19:56:38 -05004197 /*
4198 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
4199 * continue discovery.
4200 */
4201 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004202 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004203 !(vport->fc_flag & FC_RSCN_MODE) &&
4204 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004205 lpfc_issue_reg_vpi(phba, vport);
4206 return;
4207 }
James Smart2e0fef82007-06-17 19:56:36 -05004208
James Smart92d7f7b2007-06-17 19:56:38 -05004209 /*
4210 * For SLI2, we need to set port_state to READY and continue
4211 * discovery.
4212 */
4213 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4214 /* If we get here, there is nothing to ADISC */
4215 if (vport->port_type == LPFC_PHYSICAL_PORT)
4216 lpfc_issue_clear_la(phba, vport);
4217
4218 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004219 vport->num_disc_nodes = 0;
4220 /* go thru NPR nodes and issue ELS PLOGIs */
4221 if (vport->fc_npr_cnt)
4222 lpfc_els_disc_plogi(vport);
4223
4224 if (!vport->num_disc_nodes) {
4225 spin_lock_irq(shost->host_lock);
4226 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4227 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004228 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05004229 }
4230 }
James Smart92d7f7b2007-06-17 19:56:38 -05004231 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004232 } else {
4233 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004234 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05004235
4236 if (num_sent)
4237 return;
4238
James Smart2e0fef82007-06-17 19:56:36 -05004239 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05004240 /* Check to see if more RSCNs came in while we
4241 * were processing this one.
4242 */
James Smart2e0fef82007-06-17 19:56:36 -05004243 if ((vport->fc_rscn_id_cnt == 0) &&
4244 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
4245 spin_lock_irq(shost->host_lock);
4246 vport->fc_flag &= ~FC_RSCN_MODE;
4247 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004248 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004249 } else
James Smart2e0fef82007-06-17 19:56:36 -05004250 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05004251 }
4252 }
4253 return;
4254}
4255
4256/*
4257 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
4258 * ring the match the sppecified nodelist.
4259 */
4260static void
James Smart2e0fef82007-06-17 19:56:36 -05004261lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004262{
James Smart2534ba72007-04-25 09:52:20 -04004263 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004264 struct lpfc_sli *psli;
4265 IOCB_t *icmd;
4266 struct lpfc_iocbq *iocb, *next_iocb;
4267 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004268
4269 psli = &phba->sli;
4270 pring = &psli->ring[LPFC_ELS_RING];
4271
4272 /* Error matching iocb on txq or txcmplq
4273 * First check the txq.
4274 */
James Smart2e0fef82007-06-17 19:56:36 -05004275 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004276 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4277 if (iocb->context1 != ndlp) {
4278 continue;
4279 }
4280 icmd = &iocb->iocb;
4281 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
4282 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
4283
James Smart2534ba72007-04-25 09:52:20 -04004284 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05004285 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05004286 }
4287 }
4288
4289 /* Next check the txcmplq */
4290 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
4291 if (iocb->context1 != ndlp) {
4292 continue;
4293 }
4294 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004295 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
4296 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04004297 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05004298 }
4299 }
James Smart2e0fef82007-06-17 19:56:36 -05004300 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04004301
James Smarta257bf92009-04-06 18:48:10 -04004302 /* Cancel all the IOCBs from the completions list */
4303 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4304 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004305}
4306
Adrian Bunka6ababd2007-11-05 18:07:33 +01004307static void
James Smart2e0fef82007-06-17 19:56:36 -05004308lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004309{
4310 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05004311 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004312
James Smart2e0fef82007-06-17 19:56:36 -05004313 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
4314 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004315 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004316 if (!NLP_CHK_NODE_ACT(ndlp))
4317 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004318 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4319 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
4320 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04004321 }
dea31012005-04-17 16:05:31 -05004322 }
4323 }
dea31012005-04-17 16:05:31 -05004324}
4325
James Smart92d7f7b2007-06-17 19:56:38 -05004326void
4327lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
4328{
4329 lpfc_els_flush_rscn(vport);
4330 lpfc_els_flush_cmd(vport);
4331 lpfc_disc_flush_list(vport);
4332}
4333
dea31012005-04-17 16:05:31 -05004334/*****************************************************************************/
4335/*
4336 * NAME: lpfc_disc_timeout
4337 *
4338 * FUNCTION: Fibre Channel driver discovery timeout routine.
4339 *
4340 * EXECUTION ENVIRONMENT: interrupt only
4341 *
4342 * CALLED FROM:
4343 * Timer function
4344 *
4345 * RETURNS:
4346 * none
4347 */
4348/*****************************************************************************/
4349void
4350lpfc_disc_timeout(unsigned long ptr)
4351{
James Smart2e0fef82007-06-17 19:56:36 -05004352 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4353 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04004354 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004355 unsigned long flags = 0;
4356
4357 if (unlikely(!phba))
4358 return;
4359
James Smart5e9d9b82008-06-14 22:52:53 -04004360 spin_lock_irqsave(&vport->work_port_lock, flags);
4361 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4362 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05004363 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04004364 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05004365
James Smart5e9d9b82008-06-14 22:52:53 -04004366 if (!tmo_posted)
4367 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004368 return;
4369}
4370
4371static void
James Smart2e0fef82007-06-17 19:56:36 -05004372lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004373{
James Smart2e0fef82007-06-17 19:56:36 -05004374 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4375 struct lpfc_hba *phba = vport->phba;
4376 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004377 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05004378 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05004379 int rc, clrlaerr = 0;
4380
James Smart2e0fef82007-06-17 19:56:36 -05004381 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05004382 return;
4383
James Smart2e0fef82007-06-17 19:56:36 -05004384 spin_lock_irq(shost->host_lock);
4385 vport->fc_flag &= ~FC_DISC_TMO;
4386 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004387
James Smart858c9f62007-06-17 19:56:39 -05004388 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4389 "disc timeout: state:x%x rtry:x%x flg:x%x",
4390 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4391
James Smart2e0fef82007-06-17 19:56:36 -05004392 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05004393
4394 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05004395 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
4396 * FAN
4397 */
4398 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04004399 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4400 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004401 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05004402 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004403 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004404 if (!NLP_CHK_NODE_ACT(ndlp))
4405 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004406 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4407 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004408 if (ndlp->nlp_type & NLP_FABRIC) {
4409 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05004410 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004411
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004412 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004413 /* Fail outstanding IO now since device
4414 * is marked for PLOGI.
4415 */
James Smart2e0fef82007-06-17 19:56:36 -05004416 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004417 }
4418 }
James Smart92d7f7b2007-06-17 19:56:38 -05004419 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05004420 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05004421 return;
James Smart92d7f7b2007-06-17 19:56:38 -05004422 }
dea31012005-04-17 16:05:31 -05004423 break;
4424
James Smart92d7f7b2007-06-17 19:56:38 -05004425 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05004426 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05004427 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05004428 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04004429 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4430 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04004431 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05004432
4433 /* Assume no Fabric and go on with discovery.
4434 * Check for outstanding ELS FLOGI to abort.
4435 */
4436
4437 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05004438 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004439
4440 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05004441 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05004442 break;
4443
4444 case LPFC_FABRIC_CFG_LINK:
4445 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4446 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04004447 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4448 "0223 Timeout while waiting for "
4449 "NameServer login\n");
dea31012005-04-17 16:05:31 -05004450 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05004451 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05004452 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04004453 lpfc_els_abort(phba, ndlp);
4454
4455 /* ReStart discovery */
4456 goto restart_disc;
dea31012005-04-17 16:05:31 -05004457
4458 case LPFC_NS_QRY:
4459 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04004460 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4461 "0224 NameServer Query timeout "
4462 "Data: x%x x%x\n",
4463 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004464
James Smart92d7f7b2007-06-17 19:56:38 -05004465 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4466 /* Try it one more time */
4467 vport->fc_ns_retry++;
4468 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4469 vport->fc_ns_retry, 0);
4470 if (rc == 0)
4471 break;
dea31012005-04-17 16:05:31 -05004472 }
James Smart92d7f7b2007-06-17 19:56:38 -05004473 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05004474
James Smart87af33f2007-10-27 13:37:43 -04004475restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05004476 /*
4477 * Discovery is over.
4478 * set port_state to PORT_READY if SLI2.
4479 * cmpl_reg_vpi will set port_state to READY for SLI3.
4480 */
James Smart3772a992009-05-22 14:50:54 -04004481 if (phba->sli_rev < LPFC_SLI_REV4) {
4482 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4483 lpfc_issue_reg_vpi(phba, vport);
4484 else { /* NPIV Not enabled */
4485 lpfc_issue_clear_la(phba, vport);
4486 vport->port_state = LPFC_VPORT_READY;
4487 }
dea31012005-04-17 16:05:31 -05004488 }
4489
4490 /* Setup and issue mailbox INITIALIZE LINK command */
4491 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4492 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004493 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4494 "0206 Device Discovery "
4495 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004496 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004497 break;
4498 }
4499
4500 lpfc_linkdown(phba);
4501 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4502 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004503 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004504 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004505 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004506 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004507 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004508 if (rc == MBX_NOT_FINISHED)
4509 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4510
4511 break;
4512
4513 case LPFC_DISC_AUTH:
4514 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004515 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4516 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004517 lpfc_disc_flush_list(vport);
4518
James Smart92d7f7b2007-06-17 19:56:38 -05004519 /*
4520 * set port_state to PORT_READY if SLI2.
4521 * cmpl_reg_vpi will set port_state to READY for SLI3.
4522 */
James Smart3772a992009-05-22 14:50:54 -04004523 if (phba->sli_rev < LPFC_SLI_REV4) {
4524 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4525 lpfc_issue_reg_vpi(phba, vport);
4526 else { /* NPIV Not enabled */
4527 lpfc_issue_clear_la(phba, vport);
4528 vport->port_state = LPFC_VPORT_READY;
4529 }
dea31012005-04-17 16:05:31 -05004530 }
4531 break;
4532
James Smart2e0fef82007-06-17 19:56:36 -05004533 case LPFC_VPORT_READY:
4534 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004535 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4536 "0231 RSCN timeout Data: x%x "
4537 "x%x\n",
4538 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004539
4540 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004541 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004542
James Smart2e0fef82007-06-17 19:56:36 -05004543 lpfc_els_flush_rscn(vport);
4544 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004545 }
4546 break;
James Smart2e0fef82007-06-17 19:56:36 -05004547
James Smart92d7f7b2007-06-17 19:56:38 -05004548 default:
James Smarte8b62012007-08-02 11:10:09 -04004549 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004550 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004551 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004552 break;
4553 }
4554
4555 switch (phba->link_state) {
4556 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004557 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004558 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4559 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004560 clrlaerr = 1;
4561 break;
4562
James Smart09372822008-01-11 01:52:54 -05004563 case LPFC_LINK_UP:
4564 lpfc_issue_clear_la(phba, vport);
4565 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004566 case LPFC_LINK_UNKNOWN:
4567 case LPFC_WARM_START:
4568 case LPFC_INIT_START:
4569 case LPFC_INIT_MBX_CMDS:
4570 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004571 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004572 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4573 "0230 Unexpected timeout, hba link "
4574 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004575 clrlaerr = 1;
4576 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004577
4578 case LPFC_HBA_READY:
4579 break;
dea31012005-04-17 16:05:31 -05004580 }
4581
4582 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004583 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004584 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004585 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4586 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004587 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004588 }
4589
4590 return;
4591}
4592
dea31012005-04-17 16:05:31 -05004593/*
4594 * This routine handles processing a NameServer REG_LOGIN mailbox
4595 * command upon completion. It is setup in the LPFC_MBOXQ
4596 * as the completion routine when the command is
4597 * handed off to the SLI layer.
4598 */
4599void
James Smart2e0fef82007-06-17 19:56:36 -05004600lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004601{
James Smart04c68492009-05-22 14:52:52 -04004602 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004603 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4604 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4605 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004606
4607 pmb->context1 = NULL;
4608
dea31012005-04-17 16:05:31 -05004609 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004610 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004611 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004612 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004613
James Smart2e0fef82007-06-17 19:56:36 -05004614 /*
4615 * Start issuing Fabric-Device Management Interface (FDMI) command to
4616 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4617 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004618 */
James Smart2e0fef82007-06-17 19:56:36 -05004619
James Smart3de2a652007-08-02 11:09:59 -04004620 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004621 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4622 else
4623 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004624
James Smartfa4066b2008-01-11 01:53:27 -05004625 /* decrement the node reference count held for this callback
4626 * function.
4627 */
James Smart329f9bc2007-04-25 09:53:01 -04004628 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004629 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4630 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004631 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004632
4633 return;
4634}
4635
James Smart685f0bf2007-04-25 09:53:08 -04004636static int
4637lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4638{
4639 uint16_t *rpi = param;
4640
4641 return ndlp->nlp_rpi == *rpi;
4642}
4643
4644static int
4645lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4646{
4647 return memcmp(&ndlp->nlp_portname, param,
4648 sizeof(ndlp->nlp_portname)) == 0;
4649}
4650
Adrian Bunka6ababd2007-11-05 18:07:33 +01004651static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004652__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004653{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004654 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004655
James Smart2e0fef82007-06-17 19:56:36 -05004656 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004657 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004658 return ndlp;
4659 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004660 return NULL;
dea31012005-04-17 16:05:31 -05004661}
4662
James Smart685f0bf2007-04-25 09:53:08 -04004663/*
4664 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004665 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004666 */
4667struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004668__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004669{
James Smart2e0fef82007-06-17 19:56:36 -05004670 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004671}
4672
James Smart488d1462006-03-07 15:02:37 -05004673/*
James Smart685f0bf2007-04-25 09:53:08 -04004674 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004675 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004676 */
4677struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004678lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004679{
James Smart2e0fef82007-06-17 19:56:36 -05004680 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004681 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004682
James Smart2e0fef82007-06-17 19:56:36 -05004683 spin_lock_irq(shost->host_lock);
4684 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4685 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004686 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004687}
4688
dea31012005-04-17 16:05:31 -05004689void
James Smart2e0fef82007-06-17 19:56:36 -05004690lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4691 uint32_t did)
dea31012005-04-17 16:05:31 -05004692{
4693 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004694
4695 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004696 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004697
4698 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4699 "node init: did:x%x",
4700 ndlp->nlp_DID, 0, 0);
4701
dea31012005-04-17 16:05:31 -05004702 return;
4703}
James Smart329f9bc2007-04-25 09:53:01 -04004704
James Smart98c9ea52007-10-27 13:37:33 -04004705/* This routine releases all resources associated with a specifc NPort's ndlp
4706 * and mempool_free's the nodelist.
4707 */
James Smart311464e2007-08-02 11:10:37 -04004708static void
James Smart329f9bc2007-04-25 09:53:01 -04004709lpfc_nlp_release(struct kref *kref)
4710{
James Smarte47c9092008-02-08 18:49:26 -05004711 struct lpfc_hba *phba;
4712 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004713 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4714 kref);
James Smart858c9f62007-06-17 19:56:39 -05004715
4716 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4717 "node release: did:x%x flg:x%x type:x%x",
4718 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4719
James Smarte47c9092008-02-08 18:49:26 -05004720 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4721 "0279 lpfc_nlp_release: ndlp:x%p "
4722 "usgmap:x%x refcnt:%d\n",
4723 (void *)ndlp, ndlp->nlp_usg_map,
4724 atomic_read(&ndlp->kref.refcount));
4725
4726 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004727 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004728
4729 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004730 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004731 spin_lock_irqsave(&phba->ndlp_lock, flags);
4732 NLP_CLR_NODE_ACT(ndlp);
4733 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4734
4735 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004736 if (NLP_CHK_FREE_REQ(ndlp)) {
4737 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004738 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004739 }
James Smart329f9bc2007-04-25 09:53:01 -04004740}
4741
James Smart98c9ea52007-10-27 13:37:33 -04004742/* This routine bumps the reference count for a ndlp structure to ensure
4743 * that one discovery thread won't free a ndlp while another discovery thread
4744 * is using it.
4745 */
James Smart329f9bc2007-04-25 09:53:01 -04004746struct lpfc_nodelist *
4747lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4748{
James Smarte47c9092008-02-08 18:49:26 -05004749 struct lpfc_hba *phba;
4750 unsigned long flags;
4751
James Smart98c9ea52007-10-27 13:37:33 -04004752 if (ndlp) {
4753 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4754 "node get: did:x%x flg:x%x refcnt:x%x",
4755 ndlp->nlp_DID, ndlp->nlp_flag,
4756 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004757 /* The check of ndlp usage to prevent incrementing the
4758 * ndlp reference count that is in the process of being
4759 * released.
4760 */
James Smarta257bf92009-04-06 18:48:10 -04004761 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004762 spin_lock_irqsave(&phba->ndlp_lock, flags);
4763 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4764 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4765 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4766 "0276 lpfc_nlp_get: ndlp:x%p "
4767 "usgmap:x%x refcnt:%d\n",
4768 (void *)ndlp, ndlp->nlp_usg_map,
4769 atomic_read(&ndlp->kref.refcount));
4770 return NULL;
4771 } else
4772 kref_get(&ndlp->kref);
4773 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004774 }
James Smart329f9bc2007-04-25 09:53:01 -04004775 return ndlp;
4776}
4777
James Smart98c9ea52007-10-27 13:37:33 -04004778/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004779 * count goes to 0, this indicates the the associated nodelist should be
4780 * freed. Returning 1 indicates the ndlp resource has been released; on the
4781 * other hand, returning 0 indicates the ndlp resource has not been released
4782 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004783 */
James Smart329f9bc2007-04-25 09:53:01 -04004784int
4785lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4786{
James Smarte47c9092008-02-08 18:49:26 -05004787 struct lpfc_hba *phba;
4788 unsigned long flags;
4789
4790 if (!ndlp)
4791 return 1;
4792
4793 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4794 "node put: did:x%x flg:x%x refcnt:x%x",
4795 ndlp->nlp_DID, ndlp->nlp_flag,
4796 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004797 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004798 spin_lock_irqsave(&phba->ndlp_lock, flags);
4799 /* Check the ndlp memory free acknowledge flag to avoid the
4800 * possible race condition that kref_put got invoked again
4801 * after previous one has done ndlp memory free.
4802 */
4803 if (NLP_CHK_FREE_ACK(ndlp)) {
4804 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4805 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4806 "0274 lpfc_nlp_put: ndlp:x%p "
4807 "usgmap:x%x refcnt:%d\n",
4808 (void *)ndlp, ndlp->nlp_usg_map,
4809 atomic_read(&ndlp->kref.refcount));
4810 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004811 }
James Smarte47c9092008-02-08 18:49:26 -05004812 /* Check the ndlp inactivate log flag to avoid the possible
4813 * race condition that kref_put got invoked again after ndlp
4814 * is already in inactivating state.
4815 */
4816 if (NLP_CHK_IACT_REQ(ndlp)) {
4817 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4818 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4819 "0275 lpfc_nlp_put: ndlp:x%p "
4820 "usgmap:x%x refcnt:%d\n",
4821 (void *)ndlp, ndlp->nlp_usg_map,
4822 atomic_read(&ndlp->kref.refcount));
4823 return 1;
4824 }
4825 /* For last put, mark the ndlp usage flags to make sure no
4826 * other kref_get and kref_put on the same ndlp shall get
4827 * in between the process when the final kref_put has been
4828 * invoked on this ndlp.
4829 */
4830 if (atomic_read(&ndlp->kref.refcount) == 1) {
4831 /* Indicate ndlp is put to inactive state. */
4832 NLP_SET_IACT_REQ(ndlp);
4833 /* Acknowledge ndlp memory free has been seen. */
4834 if (NLP_CHK_FREE_REQ(ndlp))
4835 NLP_SET_FREE_ACK(ndlp);
4836 }
4837 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4838 /* Note, the kref_put returns 1 when decrementing a reference
4839 * count that was 1, it invokes the release callback function,
4840 * but it still left the reference count as 1 (not actually
4841 * performs the last decrementation). Otherwise, it actually
4842 * decrements the reference count and returns 0.
4843 */
4844 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004845}
James Smart98c9ea52007-10-27 13:37:33 -04004846
4847/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004848 * by any other discovery thread. This routine returns 1 if the
4849 * ndlp has been freed. A return value of 0 indicates the ndlp is
4850 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004851 */
4852int
4853lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4854{
4855 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4856 "node not used: did:x%x flg:x%x refcnt:x%x",
4857 ndlp->nlp_DID, ndlp->nlp_flag,
4858 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004859 if (atomic_read(&ndlp->kref.refcount) == 1)
4860 if (lpfc_nlp_put(ndlp))
4861 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004862 return 0;
4863}
James Smart6fb120a2009-05-22 14:52:59 -04004864
4865/**
4866 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4867 * @phba: Pointer to hba context object.
4868 *
4869 * This function iterate through all FC nodes associated
4870 * will all vports to check if there is any node with
4871 * fc_rports associated with it. If there is an fc_rport
4872 * associated with the node, then the node is either in
4873 * discovered state or its devloss_timer is pending.
4874 */
4875static int
4876lpfc_fcf_inuse(struct lpfc_hba *phba)
4877{
4878 struct lpfc_vport **vports;
4879 int i, ret = 0;
4880 struct lpfc_nodelist *ndlp;
4881 struct Scsi_Host *shost;
4882
4883 vports = lpfc_create_vport_work_array(phba);
4884
4885 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4886 shost = lpfc_shost_from_vport(vports[i]);
4887 spin_lock_irq(shost->host_lock);
4888 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4889 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4890 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4891 ret = 1;
4892 spin_unlock_irq(shost->host_lock);
4893 goto out;
James Smart891478a2009-11-18 15:40:23 -05004894 } else {
4895 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4896 "2624 RPI %x DID %x flg %x still "
4897 "logged in\n",
4898 ndlp->nlp_rpi, ndlp->nlp_DID,
4899 ndlp->nlp_flag);
4900 if (ndlp->nlp_flag & NLP_RPI_VALID)
4901 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04004902 }
4903 }
4904 spin_unlock_irq(shost->host_lock);
4905 }
4906out:
4907 lpfc_destroy_vport_work_array(phba, vports);
4908 return ret;
4909}
4910
4911/**
4912 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4913 * @phba: Pointer to hba context object.
4914 * @mboxq: Pointer to mailbox object.
4915 *
4916 * This function frees memory associated with the mailbox command.
4917 */
4918static void
4919lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4920{
4921 struct lpfc_vport *vport = mboxq->vport;
4922
4923 if (mboxq->u.mb.mbxStatus) {
4924 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4925 "2555 UNREG_VFI mbxStatus error x%x "
4926 "HBA state x%x\n",
4927 mboxq->u.mb.mbxStatus, vport->port_state);
4928 }
4929 mempool_free(mboxq, phba->mbox_mem_pool);
4930 return;
4931}
4932
4933/**
4934 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4935 * @phba: Pointer to hba context object.
4936 * @mboxq: Pointer to mailbox object.
4937 *
4938 * This function frees memory associated with the mailbox command.
4939 */
4940static void
4941lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4942{
4943 struct lpfc_vport *vport = mboxq->vport;
4944
4945 if (mboxq->u.mb.mbxStatus) {
4946 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4947 "2550 UNREG_FCFI mbxStatus error x%x "
4948 "HBA state x%x\n",
4949 mboxq->u.mb.mbxStatus, vport->port_state);
4950 }
4951 mempool_free(mboxq, phba->mbox_mem_pool);
4952 return;
4953}
4954
4955/**
James Smartecfd03c2010-02-12 14:41:27 -05004956 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04004957 * @phba: Pointer to hba context object.
4958 *
James Smartecfd03c2010-02-12 14:41:27 -05004959 * This function prepare the HBA for unregistering the currently registered
4960 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
4961 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04004962 */
James Smartecfd03c2010-02-12 14:41:27 -05004963int
4964lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04004965{
4966 LPFC_MBOXQ_t *mbox;
James Smart6fb120a2009-05-22 14:52:59 -04004967 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05004968 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05004969 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05004970 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04004971
James Smartecfd03c2010-02-12 14:41:27 -05004972 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04004973 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05004974 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04004975
James Smart4d9ab992009-10-02 15:16:39 -04004976 /* At this point, all discovery is aborted */
4977 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04004978
4979 /* Unregister VPIs */
4980 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05004981 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04004982 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05004983 /* Stop FLOGI/FDISC retries */
4984 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
4985 if (ndlp)
4986 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart6fb120a2009-05-22 14:52:59 -04004987 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05004988 shost = lpfc_shost_from_vport(vports[i]);
4989 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05004990 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05004991 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05004992 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04004993 }
4994 lpfc_destroy_vport_work_array(phba, vports);
4995
James Smart695a8142010-01-26 23:08:03 -05004996 /* Cleanup any outstanding ELS commands */
4997 lpfc_els_flush_all_cmd(phba);
4998
James Smart6fb120a2009-05-22 14:52:59 -04004999 /* Unregister VFI */
5000 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5001 if (!mbox) {
5002 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005003 "2556 UNREG_VFI mbox allocation failed"
5004 "HBA state x%x\n", phba->pport->port_state);
5005 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005006 }
5007
James Smart6669f9b2009-10-02 15:16:45 -04005008 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04005009 mbox->vport = phba->pport;
5010 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
5011
5012 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5013 if (rc == MBX_NOT_FINISHED) {
5014 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005015 "2557 UNREG_VFI issue mbox failed rc x%x "
5016 "HBA state x%x\n",
5017 rc, phba->pport->port_state);
James Smart6fb120a2009-05-22 14:52:59 -04005018 mempool_free(mbox, phba->mbox_mem_pool);
James Smartecfd03c2010-02-12 14:41:27 -05005019 return -EIO;
James Smart6fb120a2009-05-22 14:52:59 -04005020 }
5021
James Smart72100cc2010-02-12 14:43:01 -05005022 shost = lpfc_shost_from_vport(phba->pport);
5023 spin_lock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005024 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005025 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005026
James Smartecfd03c2010-02-12 14:41:27 -05005027 return 0;
5028}
5029
5030/**
5031 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5032 * @phba: Pointer to hba context object.
5033 *
5034 * This function issues synchronous unregister FCF mailbox command to HBA to
5035 * unregister the currently registered FCF record. The driver does not reset
5036 * the driver FCF usage state flags.
5037 *
5038 * Return 0 if successfully issued, none-zero otherwise.
5039 */
5040int
5041lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5042{
5043 LPFC_MBOXQ_t *mbox;
5044 int rc;
5045
James Smart6fb120a2009-05-22 14:52:59 -04005046 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5047 if (!mbox) {
5048 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005049 "2551 UNREG_FCFI mbox allocation failed"
5050 "HBA state x%x\n", phba->pport->port_state);
5051 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005052 }
James Smart6fb120a2009-05-22 14:52:59 -04005053 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5054 mbox->vport = phba->pport;
5055 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5056 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5057
5058 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005059 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5060 "2552 Unregister FCFI command failed rc x%x "
5061 "HBA state x%x\n",
5062 rc, phba->pport->port_state);
5063 return -EINVAL;
5064 }
5065 return 0;
5066}
5067
5068/**
5069 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5070 * @phba: Pointer to hba context object.
5071 *
5072 * This function unregisters the currently reigstered FCF. This function
5073 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5074 */
5075void
5076lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5077{
5078 int rc;
5079
5080 /* Preparation for unregistering fcf */
5081 rc = lpfc_unregister_fcf_prep(phba);
5082 if (rc) {
5083 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5084 "2748 Failed to prepare for unregistering "
5085 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005086 return;
5087 }
5088
James Smartecfd03c2010-02-12 14:41:27 -05005089 /* Now, unregister FCF record and reset HBA FCF state */
5090 rc = lpfc_sli4_unregister_fcf(phba);
5091 if (rc)
5092 return;
5093 /* Reset HBA FCF states after successful unregister FCF */
5094 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005095 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005096
5097 /*
5098 * If driver is not unloading, check if there is any other
5099 * FCF record that can be used for discovery.
5100 */
5101 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005102 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005103 return;
5104
James Smart0c9ab6f2010-02-26 14:15:57 -05005105 /* This is considered as the initial FCF discovery scan */
5106 spin_lock_irq(&phba->hbalock);
5107 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5108 spin_unlock_irq(&phba->hbalock);
5109 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005110
James Smart0c9ab6f2010-02-26 14:15:57 -05005111 if (rc) {
5112 spin_lock_irq(&phba->hbalock);
5113 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5114 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005115 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005116 "2553 lpfc_unregister_unused_fcf failed "
5117 "to read FCF record HBA state x%x\n",
5118 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005119 }
James Smartecfd03c2010-02-12 14:41:27 -05005120}
5121
5122/**
5123 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5124 * @phba: Pointer to hba context object.
5125 *
5126 * This function just unregisters the currently reigstered FCF. It does not
5127 * try to find another FCF for discovery.
5128 */
5129void
5130lpfc_unregister_fcf(struct lpfc_hba *phba)
5131{
5132 int rc;
5133
5134 /* Preparation for unregistering fcf */
5135 rc = lpfc_unregister_fcf_prep(phba);
5136 if (rc) {
5137 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5138 "2749 Failed to prepare for unregistering "
5139 "HBA's FCF record: rc=%d\n", rc);
5140 return;
5141 }
5142
5143 /* Now, unregister FCF record and reset HBA FCF state */
5144 rc = lpfc_sli4_unregister_fcf(phba);
5145 if (rc)
5146 return;
5147 /* Set proper HBA FCF states after successful unregister FCF */
5148 spin_lock_irq(&phba->hbalock);
5149 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
5150 spin_unlock_irq(&phba->hbalock);
5151}
5152
5153/**
5154 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
5155 * @phba: Pointer to hba context object.
5156 *
5157 * This function check if there are any connected remote port for the FCF and
5158 * if all the devices are disconnected, this function unregister FCFI.
5159 * This function also tries to use another FCF for discovery.
5160 */
5161void
5162lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
5163{
5164 /*
5165 * If HBA is not running in FIP mode or if HBA does not support
5166 * FCoE or if FCF is not registered, do nothing.
5167 */
5168 spin_lock_irq(&phba->hbalock);
5169 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
5170 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
5171 !(phba->hba_flag & HBA_FIP_SUPPORT)) {
5172 spin_unlock_irq(&phba->hbalock);
5173 return;
5174 }
5175 spin_unlock_irq(&phba->hbalock);
5176
5177 if (lpfc_fcf_inuse(phba))
5178 return;
5179
5180 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005181}
5182
5183/**
5184 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
5185 * @phba: Pointer to hba context object.
5186 * @buff: Buffer containing the FCF connection table as in the config
5187 * region.
5188 * This function create driver data structure for the FCF connection
5189 * record table read from config region 23.
5190 */
5191static void
5192lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
5193 uint8_t *buff)
5194{
5195 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5196 struct lpfc_fcf_conn_hdr *conn_hdr;
5197 struct lpfc_fcf_conn_rec *conn_rec;
5198 uint32_t record_count;
5199 int i;
5200
5201 /* Free the current connect table */
5202 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005203 &phba->fcf_conn_rec_list, list) {
5204 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04005205 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005206 }
James Smart6fb120a2009-05-22 14:52:59 -04005207
5208 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
5209 record_count = conn_hdr->length * sizeof(uint32_t)/
5210 sizeof(struct lpfc_fcf_conn_rec);
5211
5212 conn_rec = (struct lpfc_fcf_conn_rec *)
5213 (buff + sizeof(struct lpfc_fcf_conn_hdr));
5214
5215 for (i = 0; i < record_count; i++) {
5216 if (!(conn_rec[i].flags & FCFCNCT_VALID))
5217 continue;
5218 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
5219 GFP_KERNEL);
5220 if (!conn_entry) {
5221 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5222 "2566 Failed to allocate connection"
5223 " table entry\n");
5224 return;
5225 }
5226
5227 memcpy(&conn_entry->conn_rec, &conn_rec[i],
5228 sizeof(struct lpfc_fcf_conn_rec));
5229 conn_entry->conn_rec.vlan_tag =
5230 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
5231 conn_entry->conn_rec.flags =
5232 le16_to_cpu(conn_entry->conn_rec.flags);
5233 list_add_tail(&conn_entry->list,
5234 &phba->fcf_conn_rec_list);
5235 }
5236}
5237
5238/**
5239 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
5240 * @phba: Pointer to hba context object.
5241 * @buff: Buffer containing the FCoE parameter data structure.
5242 *
5243 * This function update driver data structure with config
5244 * parameters read from config region 23.
5245 */
5246static void
5247lpfc_read_fcoe_param(struct lpfc_hba *phba,
5248 uint8_t *buff)
5249{
5250 struct lpfc_fip_param_hdr *fcoe_param_hdr;
5251 struct lpfc_fcoe_params *fcoe_param;
5252
5253 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
5254 buff;
5255 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04005256 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04005257
5258 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
5259 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
5260 return;
5261
James Smart6fb120a2009-05-22 14:52:59 -04005262 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
5263 phba->valid_vlan = 1;
5264 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
5265 0xFFF;
5266 }
5267
5268 phba->fc_map[0] = fcoe_param->fc_map[0];
5269 phba->fc_map[1] = fcoe_param->fc_map[1];
5270 phba->fc_map[2] = fcoe_param->fc_map[2];
5271 return;
5272}
5273
5274/**
5275 * lpfc_get_rec_conf23 - Get a record type in config region data.
5276 * @buff: Buffer containing config region 23 data.
5277 * @size: Size of the data buffer.
5278 * @rec_type: Record type to be searched.
5279 *
5280 * This function searches config region data to find the begining
5281 * of the record specified by record_type. If record found, this
5282 * function return pointer to the record else return NULL.
5283 */
5284static uint8_t *
5285lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
5286{
5287 uint32_t offset = 0, rec_length;
5288
5289 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
5290 (size < sizeof(uint32_t)))
5291 return NULL;
5292
5293 rec_length = buff[offset + 1];
5294
5295 /*
5296 * One TLV record has one word header and number of data words
5297 * specified in the rec_length field of the record header.
5298 */
5299 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
5300 <= size) {
5301 if (buff[offset] == rec_type)
5302 return &buff[offset];
5303
5304 if (buff[offset] == LPFC_REGION23_LAST_REC)
5305 return NULL;
5306
5307 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
5308 rec_length = buff[offset + 1];
5309 }
5310 return NULL;
5311}
5312
5313/**
5314 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
5315 * @phba: Pointer to lpfc_hba data structure.
5316 * @buff: Buffer containing config region 23 data.
5317 * @size: Size of the data buffer.
5318 *
5319 * This fuction parse the FCoE config parameters in config region 23 and
5320 * populate driver data structure with the parameters.
5321 */
5322void
5323lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
5324 uint8_t *buff,
5325 uint32_t size)
5326{
5327 uint32_t offset = 0, rec_length;
5328 uint8_t *rec_ptr;
5329
5330 /*
5331 * If data size is less than 2 words signature and version cannot be
5332 * verified.
5333 */
5334 if (size < 2*sizeof(uint32_t))
5335 return;
5336
5337 /* Check the region signature first */
5338 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
5339 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5340 "2567 Config region 23 has bad signature\n");
5341 return;
5342 }
5343
5344 offset += 4;
5345
5346 /* Check the data structure version */
5347 if (buff[offset] != LPFC_REGION23_VERSION) {
5348 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5349 "2568 Config region 23 has bad version\n");
5350 return;
5351 }
5352 offset += 4;
5353
5354 rec_length = buff[offset + 1];
5355
5356 /* Read FCoE param record */
5357 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5358 size - offset, FCOE_PARAM_TYPE);
5359 if (rec_ptr)
5360 lpfc_read_fcoe_param(phba, rec_ptr);
5361
5362 /* Read FCF connection table */
5363 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5364 size - offset, FCOE_CONN_TBL_TYPE);
5365 if (rec_ptr)
5366 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5367
5368}