blob: 7c4f389a2f674ea0a1048b853c8b093f3e4f44d8 [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,
James Smartcb5172e2010-03-15 11:25:07 -04001625 offsetof(struct fcf_record, vlan_bitmap));
1626 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1627 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001628
James Smart0c9ab6f2010-02-26 14:15:57 -05001629 return new_fcf_record;
1630}
1631
1632/**
1633 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1634 * @phba: pointer to lpfc hba data structure.
1635 * @fcf_record: pointer to the fcf record.
1636 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1637 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1638 *
1639 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1640 * enabled.
1641 **/
1642static void
1643lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1644 struct fcf_record *fcf_record,
1645 uint16_t vlan_id,
1646 uint16_t next_fcf_index)
1647{
1648 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1649 "2764 READ_FCF_RECORD:\n"
1650 "\tFCF_Index : x%x\n"
1651 "\tFCF_Avail : x%x\n"
1652 "\tFCF_Valid : x%x\n"
1653 "\tFIP_Priority : x%x\n"
1654 "\tMAC_Provider : x%x\n"
1655 "\tLowest VLANID : x%x\n"
1656 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1657 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1658 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1659 "\tNext_FCF_Index: x%x\n",
1660 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1661 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1662 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1663 fcf_record->fip_priority,
1664 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1665 vlan_id,
1666 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1667 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1668 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1669 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1670 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1671 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1672 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1673 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1674 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1675 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1676 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1677 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1678 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1679 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1680 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1681 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1682 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1683 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1684 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1685 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1686 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1687 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1688 next_fcf_index);
1689}
1690
1691/**
1692 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
1693 * @phba: pointer to lpfc hba data structure.
1694 * @mboxq: pointer to mailbox object.
1695 *
1696 * This function iterates through all the fcf records available in
1697 * HBA and chooses the optimal FCF record for discovery. After finding
1698 * the FCF for discovery it registers the FCF record and kicks start
1699 * discovery.
1700 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
1701 * use an FCF record which matches fabric name and mac address of the
1702 * currently used FCF record.
1703 * If the driver supports only one FCF, it will try to use the FCF record
1704 * used by BOOT_BIOS.
1705 */
1706void
1707lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1708{
1709 struct fcf_record *new_fcf_record;
1710 uint32_t boot_flag, addr_mode;
1711 uint16_t fcf_index, next_fcf_index;
1712 struct lpfc_fcf_rec *fcf_rec = NULL;
1713 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04001714 uint32_t seed;
1715 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001716 int rc;
1717
1718 /* If there is pending FCoE event restart FCF table scan */
1719 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1720 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1721 return;
1722 }
1723
1724 /* Parse the FCF record from the non-embedded mailbox command */
1725 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
1726 &next_fcf_index);
1727 if (!new_fcf_record) {
1728 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1729 "2765 Mailbox command READ_FCF_RECORD "
1730 "failed to retrieve a FCF record.\n");
1731 /* Let next new FCF event trigger fast failover */
1732 spin_lock_irq(&phba->hbalock);
1733 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1734 spin_unlock_irq(&phba->hbalock);
1735 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1736 return;
1737 }
1738
1739 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05001740 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1741 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05001742
1743 /* Log the FCF record information if turned on */
1744 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
1745 next_fcf_index);
1746
James Smart6fb120a2009-05-22 14:52:59 -04001747 /*
1748 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05001749 * read the next entry; otherwise, this is an eligible FCF
1750 * record for round robin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04001751 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001752 if (!rc) {
1753 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1754 "2781 FCF record fcf_index:x%x failed FCF "
1755 "connection list check, fcf_avail:x%x, "
1756 "fcf_valid:x%x\n",
1757 bf_get(lpfc_fcf_record_fcf_index,
1758 new_fcf_record),
1759 bf_get(lpfc_fcf_record_fcf_avail,
1760 new_fcf_record),
1761 bf_get(lpfc_fcf_record_fcf_valid,
1762 new_fcf_record));
James Smart6fb120a2009-05-22 14:52:59 -04001763 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001764 } else {
1765 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1766 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
1767 if (rc)
1768 goto read_next_fcf;
1769 }
1770
James Smart6fb120a2009-05-22 14:52:59 -04001771 /*
1772 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05001773 * FCF record for the discovery. The condition that a rescan
1774 * matches the in-use FCF record: fabric name, switch name, mac
1775 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04001776 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001777 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001778 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartecfd03c2010-02-12 14:41:27 -05001779 if (lpfc_fab_name_match(phba->fcf.current_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001780 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001781 lpfc_sw_name_match(phba->fcf.current_rec.switch_name,
James Smart8fa38512009-07-19 10:01:03 -04001782 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001783 lpfc_mac_addr_match(phba->fcf.current_rec.mac_addr,
1784 new_fcf_record) &&
1785 lpfc_vlan_id_match(phba->fcf.current_rec.vlan_id,
1786 vlan_id)) {
James Smart6fb120a2009-05-22 14:52:59 -04001787 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smartecfd03c2010-02-12 14:41:27 -05001788 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
1789 /* Stop FCF redisc wait timer if pending */
1790 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
1791 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1792 /* If in fast failover, mark it's completed */
James Smartfc2b9892010-02-26 14:15:29 -05001793 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV |
James Smart0c9ab6f2010-02-26 14:15:57 -05001794 FCF_DISCOVERY);
1795 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001796 goto out;
1797 }
James Smartecfd03c2010-02-12 14:41:27 -05001798 /*
1799 * Read next FCF record from HBA searching for the matching
1800 * with in-use record only if not during the fast failover
1801 * period. In case of fast failover period, it shall try to
1802 * determine whether the FCF record just read should be the
1803 * next candidate.
1804 */
1805 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001806 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001807 goto read_next_fcf;
1808 }
James Smart6fb120a2009-05-22 14:52:59 -04001809 }
James Smartecfd03c2010-02-12 14:41:27 -05001810 /*
1811 * Update on failover FCF record only if it's in FCF fast-failover
1812 * period; otherwise, update on current FCF record.
1813 */
James Smartfc2b9892010-02-26 14:15:29 -05001814 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1815 fcf_rec = &phba->fcf.failover_rec;
1816 else
James Smartecfd03c2010-02-12 14:41:27 -05001817 fcf_rec = &phba->fcf.current_rec;
1818
James Smart6fb120a2009-05-22 14:52:59 -04001819 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1820 /*
James Smartecfd03c2010-02-12 14:41:27 -05001821 * If the driver FCF record does not have boot flag
1822 * set and new hba fcf record has boot flag set, use
1823 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001824 */
James Smartecfd03c2010-02-12 14:41:27 -05001825 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
1826 /* Choose this FCF record */
1827 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1828 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05001829 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001830 goto read_next_fcf;
1831 }
1832 /*
James Smartecfd03c2010-02-12 14:41:27 -05001833 * If the driver FCF record has boot flag set and the
1834 * new hba FCF record does not have boot flag, read
1835 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04001836 */
James Smartecfd03c2010-02-12 14:41:27 -05001837 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001838 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001839 goto read_next_fcf;
1840 }
1841 /*
James Smartecfd03c2010-02-12 14:41:27 -05001842 * If the new hba FCF record has lower priority value
1843 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04001844 */
James Smartfc2b9892010-02-26 14:15:29 -05001845 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04001846 /* Choose the new FCF record with lower priority */
James Smartecfd03c2010-02-12 14:41:27 -05001847 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1848 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04001849 /* Reset running random FCF selection count */
1850 phba->fcf.eligible_fcf_cnt = 1;
1851 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
1852 /* Update running random FCF selection count */
1853 phba->fcf.eligible_fcf_cnt++;
1854 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
1855 phba->fcf.eligible_fcf_cnt);
1856 if (select_new_fcf)
1857 /* Choose the new FCF by random selection */
1858 __lpfc_update_fcf_record(phba, fcf_rec,
1859 new_fcf_record,
1860 addr_mode, vlan_id, 0);
James Smart6fb120a2009-05-22 14:52:59 -04001861 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001862 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001863 goto read_next_fcf;
1864 }
1865 /*
James Smartecfd03c2010-02-12 14:41:27 -05001866 * This is the first suitable FCF record, choose this record for
1867 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001868 */
James Smartecfd03c2010-02-12 14:41:27 -05001869 if (fcf_rec) {
1870 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1871 addr_mode, vlan_id, (boot_flag ?
1872 BOOT_ENABLE : 0));
1873 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04001874 /* Setup initial running random FCF selection count */
1875 phba->fcf.eligible_fcf_cnt = 1;
1876 /* Seeding the random number generator for random selection */
1877 seed = (uint32_t)(0xFFFFFFFF & jiffies);
1878 srandom32(seed);
James Smart6fb120a2009-05-22 14:52:59 -04001879 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001880 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001881 goto read_next_fcf;
1882
1883read_next_fcf:
1884 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05001885 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
1886 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
1887 /*
1888 * Case of FCF fast failover scan
1889 */
1890
1891 /*
1892 * It has not found any suitable FCF record, cancel
1893 * FCF scan inprogress, and do nothing
1894 */
1895 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001896 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1897 "2782 No suitable FCF record "
1898 "found during this round of "
1899 "post FCF rediscovery scan: "
1900 "fcf_evt_tag:x%x, fcf_index: "
1901 "x%x\n",
1902 phba->fcoe_eventtag_at_fcf_scan,
1903 bf_get(lpfc_fcf_record_fcf_index,
1904 new_fcf_record));
1905 /*
1906 * Let next new FCF event trigger fast
1907 * failover
1908 */
1909 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001910 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001911 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001912 return;
1913 }
1914 /*
1915 * It has found a suitable FCF record that is not
1916 * the same as in-use FCF record, unregister the
1917 * in-use FCF record, replace the in-use FCF record
1918 * with the new FCF record, mark FCF fast failover
1919 * completed, and then start register the new FCF
1920 * record.
1921 */
1922
James Smart0c9ab6f2010-02-26 14:15:57 -05001923 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001924 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05001925
1926 /* Replace in-use record with the new record */
James Smartecfd03c2010-02-12 14:41:27 -05001927 memcpy(&phba->fcf.current_rec,
1928 &phba->fcf.failover_rec,
1929 sizeof(struct lpfc_fcf_rec));
1930 /* mark the FCF fast failover completed */
James Smart0c9ab6f2010-02-26 14:15:57 -05001931 spin_lock_irq(&phba->hbalock);
1932 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
1933 spin_unlock_irq(&phba->hbalock);
1934 /*
1935 * Set up the initial registered FCF index for FLOGI
1936 * round robin FCF failover.
1937 */
1938 phba->fcf.fcf_rr_init_indx =
1939 phba->fcf.failover_rec.fcf_indx;
James Smartecfd03c2010-02-12 14:41:27 -05001940 /* Register to the new FCF record */
1941 lpfc_register_fcf(phba);
1942 } else {
1943 /*
1944 * In case of transaction period to fast FCF failover,
1945 * do nothing when search to the end of the FCF table.
1946 */
1947 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
1948 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
1949 return;
1950 /*
1951 * Otherwise, initial scan or post linkdown rescan,
James Smart0c9ab6f2010-02-26 14:15:57 -05001952 * register with the best FCF record found so far
1953 * through the FCF scanning process.
James Smartecfd03c2010-02-12 14:41:27 -05001954 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001955
1956 /* mark the initial FCF discovery completed */
1957 spin_lock_irq(&phba->hbalock);
1958 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
1959 spin_unlock_irq(&phba->hbalock);
1960 /*
1961 * Set up the initial registered FCF index for FLOGI
1962 * round robin FCF failover
1963 */
1964 phba->fcf.fcf_rr_init_indx =
1965 phba->fcf.current_rec.fcf_indx;
1966 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001967 lpfc_register_fcf(phba);
1968 }
1969 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05001970 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04001971 return;
1972
1973out:
1974 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1975 lpfc_register_fcf(phba);
1976
1977 return;
1978}
1979
1980/**
James Smart0c9ab6f2010-02-26 14:15:57 -05001981 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf round robin read_fcf mbox cmpl hdler
1982 * @phba: pointer to lpfc hba data structure.
1983 * @mboxq: pointer to mailbox object.
1984 *
1985 * This is the callback function for FLOGI failure round robin FCF failover
1986 * read FCF record mailbox command from the eligible FCF record bmask for
1987 * performing the failover. If the FCF read back is not valid/available, it
1988 * fails through to retrying FLOGI to the currently registered FCF again.
1989 * Otherwise, if the FCF read back is valid and available, it will set the
1990 * newly read FCF record to the failover FCF record, unregister currently
1991 * registered FCF record, copy the failover FCF record to the current
1992 * FCF record, and then register the current FCF record before proceeding
1993 * to trying FLOGI on the new failover FCF.
1994 */
1995void
1996lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1997{
1998 struct fcf_record *new_fcf_record;
1999 uint32_t boot_flag, addr_mode;
2000 uint16_t next_fcf_index;
2001 uint16_t current_fcf_index;
2002 uint16_t vlan_id;
2003
2004 /* If link state is not up, stop the round robin failover process */
2005 if (phba->link_state < LPFC_LINK_UP) {
2006 spin_lock_irq(&phba->hbalock);
2007 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
2008 spin_unlock_irq(&phba->hbalock);
2009 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2010 return;
2011 }
2012
2013 /* Parse the FCF record from the non-embedded mailbox command */
2014 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2015 &next_fcf_index);
2016 if (!new_fcf_record) {
2017 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2018 "2766 Mailbox command READ_FCF_RECORD "
2019 "failed to retrieve a FCF record.\n");
2020 goto out;
2021 }
2022
2023 /* Get the needed parameters from FCF record */
2024 lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2025 &addr_mode, &vlan_id);
2026
2027 /* Log the FCF record information if turned on */
2028 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2029 next_fcf_index);
2030
2031 /* Upload new FCF record to the failover FCF record */
2032 spin_lock_irq(&phba->hbalock);
2033 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2034 new_fcf_record, addr_mode, vlan_id,
2035 (boot_flag ? BOOT_ENABLE : 0));
2036 spin_unlock_irq(&phba->hbalock);
2037
2038 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2039
2040 /* Unregister the current in-use FCF record */
2041 lpfc_unregister_fcf(phba);
2042
2043 /* Replace in-use record with the new record */
2044 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2045 sizeof(struct lpfc_fcf_rec));
2046
2047 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2048 "2783 FLOGI round robin FCF failover from FCF "
2049 "(index:x%x) to FCF (index:x%x).\n",
2050 current_fcf_index,
2051 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
2052
2053out:
2054 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2055 lpfc_register_fcf(phba);
2056}
2057
2058/**
2059 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2060 * @phba: pointer to lpfc hba data structure.
2061 * @mboxq: pointer to mailbox object.
2062 *
2063 * This is the callback function of read FCF record mailbox command for
2064 * updating the eligible FCF bmask for FLOGI failure round robin FCF
2065 * failover when a new FCF event happened. If the FCF read back is
2066 * valid/available and it passes the connection list check, it updates
2067 * the bmask for the eligible FCF record for round robin failover.
2068 */
2069void
2070lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2071{
2072 struct fcf_record *new_fcf_record;
2073 uint32_t boot_flag, addr_mode;
2074 uint16_t fcf_index, next_fcf_index;
2075 uint16_t vlan_id;
2076 int rc;
2077
2078 /* If link state is not up, no need to proceed */
2079 if (phba->link_state < LPFC_LINK_UP)
2080 goto out;
2081
2082 /* If FCF discovery period is over, no need to proceed */
2083 if (phba->fcf.fcf_flag & FCF_DISCOVERY)
2084 goto out;
2085
2086 /* Parse the FCF record from the non-embedded mailbox command */
2087 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2088 &next_fcf_index);
2089 if (!new_fcf_record) {
2090 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2091 "2767 Mailbox command READ_FCF_RECORD "
2092 "failed to retrieve a FCF record.\n");
2093 goto out;
2094 }
2095
2096 /* Check the connection list for eligibility */
2097 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2098 &addr_mode, &vlan_id);
2099
2100 /* Log the FCF record information if turned on */
2101 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2102 next_fcf_index);
2103
2104 if (!rc)
2105 goto out;
2106
2107 /* Update the eligible FCF record index bmask */
2108 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2109 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
2110
2111out:
2112 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2113}
2114
2115/**
James Smart1c6834a2009-07-19 10:01:26 -04002116 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2117 * @phba: pointer to lpfc hba data structure.
2118 * @mboxq: pointer to mailbox data structure.
2119 *
2120 * This function handles completion of init vpi mailbox command.
2121 */
James Smart695a8142010-01-26 23:08:03 -05002122void
James Smart1c6834a2009-07-19 10:01:26 -04002123lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2124{
2125 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002126 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002127 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2128
James Smart1c6834a2009-07-19 10:01:26 -04002129 if (mboxq->u.mb.mbxStatus) {
2130 lpfc_printf_vlog(vport, KERN_ERR,
2131 LOG_MBOX,
2132 "2609 Init VPI mailbox failed 0x%x\n",
2133 mboxq->u.mb.mbxStatus);
2134 mempool_free(mboxq, phba->mbox_mem_pool);
2135 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2136 return;
2137 }
James Smart72100cc2010-02-12 14:43:01 -05002138 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002139 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002140 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002141
James Smart695a8142010-01-26 23:08:03 -05002142 /* If this port is physical port or FDISC is done, do reg_vpi */
2143 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2144 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2145 if (!ndlp)
2146 lpfc_printf_vlog(vport, KERN_ERR,
2147 LOG_DISCOVERY,
2148 "2731 Cannot find fabric "
2149 "controller node\n");
2150 else
2151 lpfc_register_new_vport(phba, vport, ndlp);
2152 mempool_free(mboxq, phba->mbox_mem_pool);
2153 return;
2154 }
2155
James Smart1c6834a2009-07-19 10:01:26 -04002156 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2157 lpfc_initial_fdisc(vport);
2158 else {
2159 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002160 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2161 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002162 }
James Smart695a8142010-01-26 23:08:03 -05002163 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002164 return;
2165}
2166
2167/**
James Smartecfd03c2010-02-12 14:41:27 -05002168 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2169 * @vport: pointer to lpfc_vport data structure.
2170 *
2171 * This function issue a init_vpi mailbox command to initialize
2172 * VPI for the vport.
2173 */
2174void
2175lpfc_issue_init_vpi(struct lpfc_vport *vport)
2176{
2177 LPFC_MBOXQ_t *mboxq;
2178 int rc;
2179
2180 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2181 if (!mboxq) {
2182 lpfc_printf_vlog(vport, KERN_ERR,
2183 LOG_MBOX, "2607 Failed to allocate "
2184 "init_vpi mailbox\n");
2185 return;
2186 }
2187 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2188 mboxq->vport = vport;
2189 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2190 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2191 if (rc == MBX_NOT_FINISHED) {
2192 lpfc_printf_vlog(vport, KERN_ERR,
2193 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2194 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2195 }
2196}
2197
2198/**
James Smart6fb120a2009-05-22 14:52:59 -04002199 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2200 * @phba: pointer to lpfc hba data structure.
2201 *
2202 * This function loops through the list of vports on the @phba and issues an
2203 * FDISC if possible.
2204 */
2205void
2206lpfc_start_fdiscs(struct lpfc_hba *phba)
2207{
2208 struct lpfc_vport **vports;
2209 int i;
2210
2211 vports = lpfc_create_vport_work_array(phba);
2212 if (vports != NULL) {
2213 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2214 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2215 continue;
2216 /* There are no vpi for this vport */
2217 if (vports[i]->vpi > phba->max_vpi) {
2218 lpfc_vport_set_state(vports[i],
2219 FC_VPORT_FAILED);
2220 continue;
2221 }
2222 if (phba->fc_topology == TOPOLOGY_LOOP) {
2223 lpfc_vport_set_state(vports[i],
2224 FC_VPORT_LINKDOWN);
2225 continue;
2226 }
James Smart1c6834a2009-07-19 10:01:26 -04002227 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002228 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002229 continue;
2230 }
James Smart6fb120a2009-05-22 14:52:59 -04002231 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2232 lpfc_initial_fdisc(vports[i]);
2233 else {
2234 lpfc_vport_set_state(vports[i],
2235 FC_VPORT_NO_FABRIC_SUPP);
2236 lpfc_printf_vlog(vports[i], KERN_ERR,
2237 LOG_ELS,
2238 "0259 No NPIV "
2239 "Fabric support\n");
2240 }
2241 }
2242 }
2243 lpfc_destroy_vport_work_array(phba, vports);
2244}
2245
2246void
2247lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2248{
2249 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2250 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002251 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002252
2253 if (mboxq->u.mb.mbxStatus) {
2254 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2255 "2018 REG_VFI mbxStatus error x%x "
2256 "HBA state x%x\n",
2257 mboxq->u.mb.mbxStatus, vport->port_state);
2258 if (phba->fc_topology == TOPOLOGY_LOOP) {
2259 /* FLOGI failed, use loop map to make discovery list */
2260 lpfc_disc_list_loopmap(vport);
2261 /* Start discovery */
2262 lpfc_disc_start(vport);
2263 goto fail_free_mem;
2264 }
2265 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2266 goto fail_free_mem;
2267 }
James Smartc8685952009-11-18 15:39:16 -05002268 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002269 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002270 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002271 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002272 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002273 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002274
2275 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2276 lpfc_start_fdiscs(phba);
2277 lpfc_do_scr_ns_plogi(phba, vport);
2278 }
2279
2280fail_free_mem:
2281 mempool_free(mboxq, phba->mbox_mem_pool);
2282 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2283 kfree(dmabuf);
2284 return;
2285}
2286
2287static void
James Smart2e0fef82007-06-17 19:56:36 -05002288lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002289{
James Smart6fb120a2009-05-22 14:52:59 -04002290 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002291 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002292 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002293
2294
2295 /* Check for error */
2296 if (mb->mbxStatus) {
2297 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002298 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2299 "0319 READ_SPARAM mbxStatus error x%x "
2300 "hba state x%x>\n",
2301 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002302 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002303 goto out;
2304 }
2305
James Smart2e0fef82007-06-17 19:56:36 -05002306 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002307 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05002308 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05002309 u64_to_wwn(phba->cfg_soft_wwnn,
2310 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04002311 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05002312 u64_to_wwn(phba->cfg_soft_wwpn,
2313 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05002314 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
2315 sizeof(vport->fc_nodename));
2316 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
2317 sizeof(vport->fc_portname));
2318 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2319 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2320 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2321 }
2322
dea31012005-04-17 16:05:31 -05002323 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2324 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002325 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002326 return;
2327
2328out:
2329 pmb->context1 = NULL;
2330 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2331 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002332 lpfc_issue_clear_la(phba, vport);
2333 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002334 return;
2335}
2336
2337static void
James Smart92d7f7b2007-06-17 19:56:38 -05002338lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05002339{
James Smart92d7f7b2007-06-17 19:56:38 -05002340 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002341 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002342 int i;
James Smart14691152006-12-02 13:34:28 -05002343 struct lpfc_dmabuf *mp;
2344 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002345 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002346
James Smart92d7f7b2007-06-17 19:56:38 -05002347 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002348 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05002349 case LA_1GHZ_LINK:
2350 phba->fc_linkspeed = LA_1GHZ_LINK;
2351 break;
2352 case LA_2GHZ_LINK:
2353 phba->fc_linkspeed = LA_2GHZ_LINK;
2354 break;
2355 case LA_4GHZ_LINK:
2356 phba->fc_linkspeed = LA_4GHZ_LINK;
2357 break;
2358 case LA_8GHZ_LINK:
2359 phba->fc_linkspeed = LA_8GHZ_LINK;
2360 break;
James Smartf4b4c682009-05-22 14:53:12 -04002361 case LA_10GHZ_LINK:
2362 phba->fc_linkspeed = LA_10GHZ_LINK;
2363 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002364 default:
2365 phba->fc_linkspeed = LA_UNKNW_LINK;
2366 break;
dea31012005-04-17 16:05:31 -05002367 }
2368
2369 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05002370 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05002371
2372 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05002373 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05002374
James Smart6a9c52c2009-10-02 15:16:51 -04002375 /* if npiv is enabled and this adapter supports npiv log
2376 * a message that npiv is not supported in this topology
2377 */
2378 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04002379 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2380 "1309 Link Up Event npiv not supported in loop "
2381 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002382 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05002383 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05002384 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002385
James Smart2e0fef82007-06-17 19:56:36 -05002386 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05002387 i = la->un.lilpBde64.tus.f.bdeSize;
2388
2389 if (i == 0) {
2390 phba->alpa_map[0] = 0;
2391 } else {
James Smarte8b62012007-08-02 11:10:09 -04002392 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05002393 int numalpa, j, k;
2394 union {
2395 uint8_t pamap[16];
2396 struct {
2397 uint32_t wd1;
2398 uint32_t wd2;
2399 uint32_t wd3;
2400 uint32_t wd4;
2401 } pa;
2402 } un;
2403 numalpa = phba->alpa_map[0];
2404 j = 0;
2405 while (j < numalpa) {
2406 memset(un.pamap, 0, 16);
2407 for (k = 1; j < numalpa; k++) {
2408 un.pamap[k - 1] =
2409 phba->alpa_map[j + 1];
2410 j++;
2411 if (k == 16)
2412 break;
2413 }
2414 /* Link Up Event ALPA map */
2415 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05002416 KERN_WARNING,
2417 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002418 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05002419 "ALPA map Data: x%x "
2420 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002421 un.pa.wd1, un.pa.wd2,
2422 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05002423 }
2424 }
2425 }
2426 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05002427 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04002428 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05002429 (phba->sli_rev == 3))
2430 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
2431 }
James Smart2e0fef82007-06-17 19:56:36 -05002432 vport->fc_myDID = phba->fc_pref_DID;
2433 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002434 }
James Smart92d7f7b2007-06-17 19:56:38 -05002435 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002436
2437 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05002438 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2439 if (!sparam_mbox)
2440 goto out;
2441
2442 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
2443 if (rc) {
2444 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2445 goto out;
2446 }
2447 sparam_mbox->vport = vport;
2448 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
2449 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
2450 if (rc == MBX_NOT_FINISHED) {
2451 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2452 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2453 kfree(mp);
2454 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2455 goto out;
dea31012005-04-17 16:05:31 -05002456 }
2457
James Smart6fb120a2009-05-22 14:52:59 -04002458 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2459 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2460 if (!cfglink_mbox)
2461 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05002462 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002463 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002464 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002465 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002466 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002467 if (rc == MBX_NOT_FINISHED) {
2468 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2469 goto out;
2470 }
2471 } else {
James Smart32b97932009-07-19 10:01:21 -04002472 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002473 /*
2474 * Add the driver's default FCF record at FCF index 0 now. This
2475 * is phase 1 implementation that support FCF index 0 and driver
2476 * defaults.
2477 */
James Smart45ed1192009-10-02 15:17:02 -04002478 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002479 fcf_record = kzalloc(sizeof(struct fcf_record),
2480 GFP_KERNEL);
2481 if (unlikely(!fcf_record)) {
2482 lpfc_printf_log(phba, KERN_ERR,
2483 LOG_MBOX | LOG_SLI,
2484 "2554 Could not allocate memmory for "
2485 "fcf record\n");
2486 rc = -ENODEV;
2487 goto out;
2488 }
2489
2490 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2491 LPFC_FCOE_FCF_DEF_INDEX);
2492 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2493 if (unlikely(rc)) {
2494 lpfc_printf_log(phba, KERN_ERR,
2495 LOG_MBOX | LOG_SLI,
2496 "2013 Could not manually add FCF "
2497 "record 0, status %d\n", rc);
2498 rc = -ENODEV;
2499 kfree(fcf_record);
2500 goto out;
2501 }
2502 kfree(fcf_record);
2503 }
2504 /*
2505 * The driver is expected to do FIP/FCF. Call the port
2506 * and get the FCF Table.
2507 */
James Smart32b97932009-07-19 10:01:21 -04002508 spin_lock_irq(&phba->hbalock);
2509 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2510 spin_unlock_irq(&phba->hbalock);
2511 return;
2512 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002513 /* This is the initial FCF discovery scan */
2514 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04002515 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002516 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2517 "2778 Start FCF table scan at linkup\n");
2518
2519 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2520 LPFC_FCOE_FCF_GET_FIRST);
2521 if (rc) {
2522 spin_lock_irq(&phba->hbalock);
2523 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
2524 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002525 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002526 }
dea31012005-04-17 16:05:31 -05002527 }
James Smart6fb120a2009-05-22 14:52:59 -04002528
2529 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002530out:
2531 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002532 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2533 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2534 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002535 lpfc_issue_clear_la(phba, vport);
2536 return;
dea31012005-04-17 16:05:31 -05002537}
2538
2539static void
James Smart84774a42008-08-24 21:50:06 -04002540lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002541{
dea31012005-04-17 16:05:31 -05002542 uint32_t control;
2543 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002544 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002545 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002546 if (phba->sli_rev <= LPFC_SLI_REV3) {
2547 control = readl(phba->HCregaddr);
2548 control |= HC_LAINT_ENA;
2549 writel(control, phba->HCregaddr);
2550 readl(phba->HCregaddr); /* flush */
2551 }
James Smart2e0fef82007-06-17 19:56:36 -05002552 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002553}
2554
James Smart84774a42008-08-24 21:50:06 -04002555static void
2556lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2557{
2558 lpfc_linkdown(phba);
2559 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002560 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002561 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2562}
2563
2564
dea31012005-04-17 16:05:31 -05002565/*
2566 * This routine handles processing a READ_LA mailbox
2567 * command upon completion. It is setup in the LPFC_MBOXQ
2568 * as the completion routine when the command is
2569 * handed off to the SLI layer.
2570 */
2571void
James Smart2e0fef82007-06-17 19:56:36 -05002572lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002573{
James Smart2e0fef82007-06-17 19:56:36 -05002574 struct lpfc_vport *vport = pmb->vport;
2575 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002576 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002577 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002578 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2579
James Smart0d2b6b82008-06-14 22:52:47 -04002580 /* Unblock ELS traffic */
2581 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002582 /* Check for error */
2583 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002584 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002585 "1307 READ_LA mbox error x%x state x%x\n",
2586 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002587 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002588 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002589 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2590 }
2591
James Smart04c68492009-05-22 14:52:52 -04002592 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002593
2594 memcpy(&phba->alpa_map[0], mp->virt, 128);
2595
James Smart2e0fef82007-06-17 19:56:36 -05002596 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002597 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002598 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002599 else
James Smart2e0fef82007-06-17 19:56:36 -05002600 vport->fc_flag &= ~FC_BYPASSED_MODE;
2601 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002602
James Smart0c287582009-06-10 17:22:56 -04002603 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002604 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002605 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002606 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002607 if (phba->fc_eventTag != 0)
2608 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002609 }
dea31012005-04-17 16:05:31 -05002610
2611 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05002612 spin_lock_irq(&phba->hbalock);
James Smart84774a42008-08-24 21:50:06 -04002613 if (la->mm)
2614 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2615 else
2616 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05002617 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002618
James Smart4d9ab992009-10-02 15:16:39 -04002619 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002620 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002621 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002622 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002623 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002624 "1306 Link Up Event in loop back mode "
2625 "x%x received Data: x%x x%x x%x x%x\n",
2626 la->eventTag, phba->fc_eventTag,
2627 la->granted_AL_PA, la->UlnkSpeed,
2628 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002629 } else {
2630 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002631 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002632 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002633 la->eventTag, phba->fc_eventTag,
2634 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002635 phba->alpa_map[0],
2636 la->mm, la->fa,
2637 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002638 }
James Smart92d7f7b2007-06-17 19:56:38 -05002639 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002640 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002641 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002642 if (phba->link_flag & LS_LOOPBACK_MODE) {
2643 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2644 "1308 Link Down Event in loop back mode "
2645 "x%x received "
2646 "Data: x%x x%x x%x\n",
2647 la->eventTag, phba->fc_eventTag,
2648 phba->pport->port_state, vport->fc_flag);
2649 }
2650 else {
2651 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002652 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002653 "Data: x%x x%x x%x x%x x%x\n",
2654 la->eventTag, phba->fc_eventTag,
2655 phba->pport->port_state, vport->fc_flag,
2656 la->mm, la->fa);
2657 }
2658 lpfc_mbx_issue_link_down(phba);
2659 }
2660 if (la->mm && la->attType == AT_LINK_UP) {
2661 if (phba->link_state != LPFC_LINK_DOWN) {
2662 phba->fc_stat.LinkDown++;
2663 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2664 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002665 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002666 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002667 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002668 lpfc_mbx_issue_link_down(phba);
2669 } else
2670 lpfc_enable_la(phba);
2671
2672 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2673 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2674 "Data: x%x x%x x%x\n",
2675 la->eventTag, phba->fc_eventTag,
2676 phba->pport->port_state, vport->fc_flag);
2677 /*
2678 * The cmnd that triggered this will be waiting for this
2679 * signal.
2680 */
2681 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2682 if (phba->wait_4_mlo_maint_flg) {
2683 phba->wait_4_mlo_maint_flg = 0;
2684 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002685 }
James Smart84774a42008-08-24 21:50:06 -04002686 }
2687
2688 if (la->fa) {
2689 if (la->mm)
2690 lpfc_issue_clear_la(phba, vport);
2691 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2692 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002693 }
2694
2695lpfc_mbx_cmpl_read_la_free_mbuf:
2696 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2697 kfree(mp);
2698 mempool_free(pmb, phba->mbox_mem_pool);
2699 return;
2700}
2701
2702/*
2703 * This routine handles processing a REG_LOGIN mailbox
2704 * command upon completion. It is setup in the LPFC_MBOXQ
2705 * as the completion routine when the command is
2706 * handed off to the SLI layer.
2707 */
2708void
James Smart2e0fef82007-06-17 19:56:36 -05002709lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002710{
James Smart2e0fef82007-06-17 19:56:36 -05002711 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002712 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002713 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05002714
dea31012005-04-17 16:05:31 -05002715 pmb->context1 = NULL;
2716
2717 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05002718 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05002719 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2720 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002721 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002722 /* decrement the node reference count held for this callback
2723 * function.
2724 */
James Smart329f9bc2007-04-25 09:53:01 -04002725 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002726
2727 return;
2728}
2729
James Smart92d7f7b2007-06-17 19:56:38 -05002730static void
2731lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2732{
James Smart04c68492009-05-22 14:52:52 -04002733 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002734 struct lpfc_vport *vport = pmb->vport;
2735 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2736
2737 switch (mb->mbxStatus) {
2738 case 0x0011:
2739 case 0x0020:
2740 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04002741 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2742 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2743 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002744 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002745 }
James Smart72100cc2010-02-12 14:43:01 -05002746 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002747 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05002748 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002749 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002750 vport->unreg_vpi_cmpl = VPORT_OK;
2751 mempool_free(pmb, phba->mbox_mem_pool);
2752 /*
2753 * This shost reference might have been taken at the beginning of
2754 * lpfc_vport_delete()
2755 */
James Smart1c6f4ef52009-11-18 15:40:49 -05002756 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05002757 scsi_host_put(shost);
2758}
2759
James Smartd7c255b2008-08-24 21:50:00 -04002760int
James Smart92d7f7b2007-06-17 19:56:38 -05002761lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2762{
2763 struct lpfc_hba *phba = vport->phba;
2764 LPFC_MBOXQ_t *mbox;
2765 int rc;
2766
2767 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2768 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002769 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002770
2771 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2772 mbox->vport = vport;
2773 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002774 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002775 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002776 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2777 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002778 mempool_free(mbox, phba->mbox_mem_pool);
2779 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002780 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002781 }
James Smartd7c255b2008-08-24 21:50:00 -04002782 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002783}
2784
2785static void
2786lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2787{
2788 struct lpfc_vport *vport = pmb->vport;
2789 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002790 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002791
2792 switch (mb->mbxStatus) {
2793 case 0x0011:
2794 case 0x9601:
2795 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002796 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2797 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2798 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002799 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2800 spin_lock_irq(shost->host_lock);
2801 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2802 spin_unlock_irq(shost->host_lock);
2803 vport->fc_myDID = 0;
2804 goto out;
2805 }
James Smart92d7f7b2007-06-17 19:56:38 -05002806
James Smart72100cc2010-02-12 14:43:01 -05002807 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002808 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002809 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002810 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002811 vport->num_disc_nodes = 0;
2812 /* go thru NPR list and issue ELS PLOGIs */
2813 if (vport->fc_npr_cnt)
2814 lpfc_els_disc_plogi(vport);
2815
2816 if (!vport->num_disc_nodes) {
2817 spin_lock_irq(shost->host_lock);
2818 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2819 spin_unlock_irq(shost->host_lock);
2820 lpfc_can_disctmo(vport);
2821 }
2822 vport->port_state = LPFC_VPORT_READY;
2823
2824out:
2825 mempool_free(pmb, phba->mbox_mem_pool);
2826 return;
2827}
2828
James Smart21e9a0a2009-05-22 14:53:21 -04002829/**
2830 * lpfc_create_static_vport - Read HBA config region to create static vports.
2831 * @phba: pointer to lpfc hba data structure.
2832 *
2833 * This routine issue a DUMP mailbox command for config region 22 to get
2834 * the list of static vports to be created. The function create vports
2835 * based on the information returned from the HBA.
2836 **/
2837void
2838lpfc_create_static_vport(struct lpfc_hba *phba)
2839{
2840 LPFC_MBOXQ_t *pmb = NULL;
2841 MAILBOX_t *mb;
2842 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04002843 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04002844 struct fc_vport_identifiers vport_id;
2845 struct fc_vport *new_fc_vport;
2846 struct Scsi_Host *shost;
2847 struct lpfc_vport *vport;
2848 uint16_t offset = 0;
2849 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04002850 struct lpfc_dmabuf *mp;
2851 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04002852
2853 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2854 if (!pmb) {
2855 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2856 "0542 lpfc_create_static_vport failed to"
2857 " allocate mailbox memory\n");
2858 return;
2859 }
2860
2861 mb = &pmb->u.mb;
2862
2863 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2864 if (!vport_info) {
2865 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2866 "0543 lpfc_create_static_vport failed to"
2867 " allocate vport_info\n");
2868 mempool_free(pmb, phba->mbox_mem_pool);
2869 return;
2870 }
2871
2872 vport_buff = (uint8_t *) vport_info;
2873 do {
James Smart1c6834a2009-07-19 10:01:26 -04002874 if (lpfc_dump_static_vport(phba, pmb, offset))
2875 goto out;
2876
James Smart21e9a0a2009-05-22 14:53:21 -04002877 pmb->vport = phba->pport;
2878 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2879
2880 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2881 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2882 "0544 lpfc_create_static_vport failed to"
2883 " issue dump mailbox command ret 0x%x "
2884 "status 0x%x\n",
2885 rc, mb->mbxStatus);
2886 goto out;
2887 }
2888
James Smart1c6834a2009-07-19 10:01:26 -04002889 if (phba->sli_rev == LPFC_SLI_REV4) {
2890 byte_count = pmb->u.mqe.un.mb_words[5];
2891 mp = (struct lpfc_dmabuf *) pmb->context2;
2892 if (byte_count > sizeof(struct static_vport_info) -
2893 offset)
2894 byte_count = sizeof(struct static_vport_info)
2895 - offset;
2896 memcpy(vport_buff + offset, mp->virt, byte_count);
2897 offset += byte_count;
2898 } else {
2899 if (mb->un.varDmp.word_cnt >
2900 sizeof(struct static_vport_info) - offset)
2901 mb->un.varDmp.word_cnt =
2902 sizeof(struct static_vport_info)
2903 - offset;
2904 byte_count = mb->un.varDmp.word_cnt;
2905 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2906 vport_buff + offset,
2907 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04002908
James Smart1c6834a2009-07-19 10:01:26 -04002909 offset += byte_count;
2910 }
James Smart21e9a0a2009-05-22 14:53:21 -04002911
James Smart1c6834a2009-07-19 10:01:26 -04002912 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04002913 offset < sizeof(struct static_vport_info));
2914
2915
2916 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2917 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2918 != VPORT_INFO_REV)) {
2919 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2920 "0545 lpfc_create_static_vport bad"
2921 " information header 0x%x 0x%x\n",
2922 le32_to_cpu(vport_info->signature),
2923 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2924
2925 goto out;
2926 }
2927
2928 shost = lpfc_shost_from_vport(phba->pport);
2929
2930 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2931 memset(&vport_id, 0, sizeof(vport_id));
2932 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2933 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2934 if (!vport_id.port_name || !vport_id.node_name)
2935 continue;
2936
2937 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2938 vport_id.vport_type = FC_PORTTYPE_NPIV;
2939 vport_id.disable = false;
2940 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2941
2942 if (!new_fc_vport) {
2943 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2944 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04002945 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04002946 continue;
2947 }
2948
2949 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2950 vport->vport_flag |= STATIC_VPORT;
2951 }
2952
2953out:
James Smart21e9a0a2009-05-22 14:53:21 -04002954 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04002955 if (rc != MBX_TIMEOUT) {
2956 if (pmb->context2) {
2957 mp = (struct lpfc_dmabuf *) pmb->context2;
2958 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2959 kfree(mp);
2960 }
James Smart21e9a0a2009-05-22 14:53:21 -04002961 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002962 }
James Smart21e9a0a2009-05-22 14:53:21 -04002963
2964 return;
2965}
2966
dea31012005-04-17 16:05:31 -05002967/*
2968 * This routine handles processing a Fabric REG_LOGIN mailbox
2969 * command upon completion. It is setup in the LPFC_MBOXQ
2970 * as the completion routine when the command is
2971 * handed off to the SLI layer.
2972 */
2973void
James Smart2e0fef82007-06-17 19:56:36 -05002974lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002975{
James Smart92d7f7b2007-06-17 19:56:38 -05002976 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04002977 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002978 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05002979 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002980
James Smart549e55c2007-08-02 11:09:51 -04002981 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04002982 pmb->context1 = NULL;
2983 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05002984 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04002985 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2986 "0258 Register Fabric login error: 0x%x\n",
2987 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05002988 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2989 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002990 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002991
James Smart92d7f7b2007-06-17 19:56:38 -05002992 if (phba->fc_topology == TOPOLOGY_LOOP) {
2993 /* FLOGI failed, use loop map to make discovery list */
2994 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002995
James Smart92d7f7b2007-06-17 19:56:38 -05002996 /* Start discovery */
2997 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05002998 /* Decrement the reference count to ndlp after the
2999 * reference to the ndlp are done.
3000 */
3001 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003002 return;
3003 }
3004
3005 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003006 /* Decrement the reference count to ndlp after the reference
3007 * to the ndlp are done.
3008 */
3009 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003010 return;
3011 }
3012
dea31012005-04-17 16:05:31 -05003013 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003014 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003015 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003016 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003017
James Smart2e0fef82007-06-17 19:56:36 -05003018 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart21e9a0a2009-05-22 14:53:21 -04003019 lpfc_start_fdiscs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003020 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003021 }
3022
3023 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3024 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003025 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003026
3027 /* Drop the reference count from the mbox at the end after
3028 * all the current reference to the ndlp have been done.
3029 */
3030 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003031 return;
3032}
3033
3034/*
3035 * This routine handles processing a NameServer REG_LOGIN mailbox
3036 * command upon completion. It is setup in the LPFC_MBOXQ
3037 * as the completion routine when the command is
3038 * handed off to the SLI layer.
3039 */
3040void
James Smart2e0fef82007-06-17 19:56:36 -05003041lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003042{
James Smart21e9a0a2009-05-22 14:53:21 -04003043 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003044 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3045 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3046 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003047
3048 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003049out:
James Smart21e9a0a2009-05-22 14:53:21 -04003050 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3051 "0260 Register NameServer error: 0x%x\n",
3052 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003053 /* decrement the node reference count held for this
3054 * callback function.
3055 */
James Smart329f9bc2007-04-25 09:53:01 -04003056 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003057 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3058 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003059 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003060
3061 /* If no other thread is using the ndlp, free it */
3062 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003063
James Smart92d7f7b2007-06-17 19:56:38 -05003064 if (phba->fc_topology == TOPOLOGY_LOOP) {
3065 /*
3066 * RegLogin failed, use loop map to make discovery
3067 * list
3068 */
3069 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003070
James Smart92d7f7b2007-06-17 19:56:38 -05003071 /* Start discovery */
3072 lpfc_disc_start(vport);
3073 return;
3074 }
3075 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003076 return;
3077 }
3078
3079 pmb->context1 = NULL;
3080
dea31012005-04-17 16:05:31 -05003081 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003082 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003083 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003084 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003085
James Smart2e0fef82007-06-17 19:56:36 -05003086 if (vport->port_state < LPFC_VPORT_READY) {
3087 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003088 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3089 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3090 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3091 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3092 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3093
3094 /* Issue SCR just before NameServer GID_FT Query */
3095 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003096 }
3097
James Smart2e0fef82007-06-17 19:56:36 -05003098 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003099 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003100 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003101 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003102 goto out;
dea31012005-04-17 16:05:31 -05003103 }
3104
James Smartfa4066b2008-01-11 01:53:27 -05003105 /* decrement the node reference count held for this
3106 * callback function.
3107 */
James Smart329f9bc2007-04-25 09:53:01 -04003108 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003109 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3110 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003111 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003112
3113 return;
3114}
3115
3116static void
James Smart2e0fef82007-06-17 19:56:36 -05003117lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003118{
James Smart2e0fef82007-06-17 19:56:36 -05003119 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3120 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003121 struct lpfc_rport_data *rdata;
3122 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003123 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003124
3125 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003126 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3127 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003128 rport_ids.port_id = ndlp->nlp_DID;
3129 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003130
James Smart329f9bc2007-04-25 09:53:01 -04003131 /*
3132 * We leave our node pointer in rport->dd_data when we unregister a
3133 * FCP target port. But fc_remote_port_add zeros the space to which
3134 * rport->dd_data points. So, if we're reusing a previously
3135 * registered port, drop the reference that we took the last time we
3136 * registered the port.
3137 */
3138 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003139 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003140 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003141
3142 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3143 "rport add: did:x%x flg:x%x type x%x",
3144 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3145
James Smart2e0fef82007-06-17 19:56:36 -05003146 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003147 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003148 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3149 "Warning: fc_remote_port_add failed\n");
3150 return;
3151 }
3152
3153 /* initialize static port data */
3154 rport->maxframe_size = ndlp->nlp_maxframe;
3155 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003156 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003157 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003158
3159 if (ndlp->nlp_type & NLP_FCP_TARGET)
3160 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3161 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3162 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3163
3164
3165 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3166 fc_remote_port_rolechg(rport, rport_ids.roles);
3167
James Smart071fbd3d2006-04-15 11:53:20 -04003168 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003169 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003170 ndlp->nlp_sid = rport->scsi_target_id;
3171 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003172 return;
3173}
3174
3175static void
James Smart2e0fef82007-06-17 19:56:36 -05003176lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003177{
3178 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003179
James Smart858c9f62007-06-17 19:56:39 -05003180 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3181 "rport delete: did:x%x flg:x%x type x%x",
3182 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3183
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003184 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003185
3186 return;
3187}
3188
James Smartde0c5b32007-04-25 09:52:27 -04003189static void
James Smart2e0fef82007-06-17 19:56:36 -05003190lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003191{
James Smart2e0fef82007-06-17 19:56:36 -05003192 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3193
3194 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003195 switch (state) {
3196 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003197 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003198 break;
3199 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003200 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003201 break;
3202 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003203 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003204 break;
3205 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003206 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003207 break;
3208 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003209 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003210 break;
3211 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003212 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003213 break;
3214 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003215 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003216 break;
3217 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003218 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003219 break;
3220 }
James Smart2e0fef82007-06-17 19:56:36 -05003221 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003222}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003223
James Smartde0c5b32007-04-25 09:52:27 -04003224static void
James Smart2e0fef82007-06-17 19:56:36 -05003225lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003226 int old_state, int new_state)
3227{
James Smart2e0fef82007-06-17 19:56:36 -05003228 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3229
James Smartde0c5b32007-04-25 09:52:27 -04003230 if (new_state == NLP_STE_UNMAPPED_NODE) {
3231 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
3232 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3233 ndlp->nlp_type |= NLP_FC_NODE;
3234 }
3235 if (new_state == NLP_STE_MAPPED_NODE)
3236 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3237 if (new_state == NLP_STE_NPR_NODE)
3238 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003239
James Smartde0c5b32007-04-25 09:52:27 -04003240 /* Transport interface */
3241 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3242 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003243 vport->phba->nport_event_cnt++;
3244 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003245 }
dea31012005-04-17 16:05:31 -05003246
James Smartde0c5b32007-04-25 09:52:27 -04003247 if (new_state == NLP_STE_MAPPED_NODE ||
3248 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003249 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003250 /*
3251 * Tell the fc transport about the port, if we haven't
3252 * already. If we have, and it's a scsi entity, be
3253 * sure to unblock any attached scsi devices
3254 */
3255 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003256 }
James Smartea2151b2008-09-07 11:52:10 -04003257 if ((new_state == NLP_STE_MAPPED_NODE) &&
3258 (vport->stat_data_enabled)) {
3259 /*
3260 * A new target is discovered, if there is no buffer for
3261 * statistical data collection allocate buffer.
3262 */
3263 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3264 sizeof(struct lpfc_scsicmd_bkt),
3265 GFP_KERNEL);
3266
3267 if (!ndlp->lat_data)
3268 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3269 "0286 lpfc_nlp_state_cleanup failed to "
3270 "allocate statistical data buffer DID "
3271 "0x%x\n", ndlp->nlp_DID);
3272 }
James Smart858c9f62007-06-17 19:56:39 -05003273 /*
3274 * if we added to Mapped list, but the remote port
3275 * registration failed or assigned a target id outside
3276 * our presentable range - move the node to the
3277 * Unmapped List
3278 */
James Smartde0c5b32007-04-25 09:52:27 -04003279 if (new_state == NLP_STE_MAPPED_NODE &&
3280 (!ndlp->rport ||
3281 ndlp->rport->scsi_target_id == -1 ||
3282 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003283 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003284 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003285 spin_unlock_irq(shost->host_lock);
3286 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003287 }
James Smartde0c5b32007-04-25 09:52:27 -04003288}
3289
James Smart685f0bf2007-04-25 09:53:08 -04003290static char *
3291lpfc_nlp_state_name(char *buffer, size_t size, int state)
3292{
3293 static char *states[] = {
3294 [NLP_STE_UNUSED_NODE] = "UNUSED",
3295 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
3296 [NLP_STE_ADISC_ISSUE] = "ADISC",
3297 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
3298 [NLP_STE_PRLI_ISSUE] = "PRLI",
3299 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
3300 [NLP_STE_MAPPED_NODE] = "MAPPED",
3301 [NLP_STE_NPR_NODE] = "NPR",
3302 };
3303
James Smart311464e2007-08-02 11:10:37 -04003304 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04003305 strlcpy(buffer, states[state], size);
3306 else
3307 snprintf(buffer, size, "unknown (%d)", state);
3308 return buffer;
3309}
3310
James Smartde0c5b32007-04-25 09:52:27 -04003311void
James Smart2e0fef82007-06-17 19:56:36 -05003312lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3313 int state)
James Smartde0c5b32007-04-25 09:52:27 -04003314{
James Smart2e0fef82007-06-17 19:56:36 -05003315 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04003316 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04003317 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04003318
James Smarte8b62012007-08-02 11:10:09 -04003319 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3320 "0904 NPort state transition x%06x, %s -> %s\n",
3321 ndlp->nlp_DID,
3322 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
3323 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05003324
3325 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3326 "node statechg did:x%x old:%d ste:%d",
3327 ndlp->nlp_DID, old_state, state);
3328
James Smartde0c5b32007-04-25 09:52:27 -04003329 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04003330 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05003331 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003332 if (old_state == NLP_STE_UNMAPPED_NODE) {
3333 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
3334 ndlp->nlp_type &= ~NLP_FC_NODE;
3335 }
3336
James Smart685f0bf2007-04-25 09:53:08 -04003337 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05003338 spin_lock_irq(shost->host_lock);
3339 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3340 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003341 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05003342 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04003343
3344 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05003345 lpfc_nlp_counters(vport, state, 1);
3346 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04003347}
3348
3349void
James Smarte47c9092008-02-08 18:49:26 -05003350lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3351{
3352 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3353
3354 if (list_empty(&ndlp->nlp_listp)) {
3355 spin_lock_irq(shost->host_lock);
3356 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3357 spin_unlock_irq(shost->host_lock);
3358 }
3359}
3360
3361void
James Smart2e0fef82007-06-17 19:56:36 -05003362lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003363{
James Smart2e0fef82007-06-17 19:56:36 -05003364 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3365
James Smart0d2b6b82008-06-14 22:52:47 -04003366 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003367 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05003368 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3369 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003370 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05003371 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003372 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05003373 NLP_STE_UNUSED_NODE);
3374}
3375
Adrian Bunk4d9db012008-02-14 23:24:02 +02003376static void
James Smarte47c9092008-02-08 18:49:26 -05003377lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3378{
James Smart0d2b6b82008-06-14 22:52:47 -04003379 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003380 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3381 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3382 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3383 NLP_STE_UNUSED_NODE);
3384}
James Smart109f6ed2008-12-04 22:39:08 -05003385/**
James Smart3621a712009-04-06 18:47:14 -04003386 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05003387 * @vport: Pointer to Virtual Port object.
3388 * @ndlp: Pointer to FC node object.
3389 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04003390 *
3391 * This function is always called when node object need to be initialized.
3392 * It initializes all the fields of the node object. Although the reference
3393 * to phba from @ndlp can be obtained indirectly through it's reference to
3394 * @vport, a direct reference to phba is taken here by @ndlp. This is due
3395 * to the life-span of the @ndlp might go beyond the existence of @vport as
3396 * the final release of ndlp is determined by its reference count. And, the
3397 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05003398 **/
3399static inline void
3400lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3401 uint32_t did)
3402{
3403 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3404 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3405 init_timer(&ndlp->nlp_delayfunc);
3406 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3407 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3408 ndlp->nlp_DID = did;
3409 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04003410 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05003411 ndlp->nlp_sid = NLP_NO_SID;
3412 kref_init(&ndlp->kref);
3413 NLP_INT_NODE_ACT(ndlp);
3414 atomic_set(&ndlp->cmd_pending, 0);
3415 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
3416}
James Smarte47c9092008-02-08 18:49:26 -05003417
3418struct lpfc_nodelist *
3419lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3420 int state)
3421{
3422 struct lpfc_hba *phba = vport->phba;
3423 uint32_t did;
3424 unsigned long flags;
3425
3426 if (!ndlp)
3427 return NULL;
3428
3429 spin_lock_irqsave(&phba->ndlp_lock, flags);
3430 /* The ndlp should not be in memory free mode */
3431 if (NLP_CHK_FREE_REQ(ndlp)) {
3432 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3433 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3434 "0277 lpfc_enable_node: ndlp:x%p "
3435 "usgmap:x%x refcnt:%d\n",
3436 (void *)ndlp, ndlp->nlp_usg_map,
3437 atomic_read(&ndlp->kref.refcount));
3438 return NULL;
3439 }
3440 /* The ndlp should not already be in active mode */
3441 if (NLP_CHK_NODE_ACT(ndlp)) {
3442 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3443 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3444 "0278 lpfc_enable_node: ndlp:x%p "
3445 "usgmap:x%x refcnt:%d\n",
3446 (void *)ndlp, ndlp->nlp_usg_map,
3447 atomic_read(&ndlp->kref.refcount));
3448 return NULL;
3449 }
3450
3451 /* Keep the original DID */
3452 did = ndlp->nlp_DID;
3453
3454 /* re-initialize ndlp except of ndlp linked list pointer */
3455 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3456 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003457 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003458
3459 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3460
3461 if (state != NLP_STE_UNUSED_NODE)
3462 lpfc_nlp_set_state(vport, ndlp, state);
3463
3464 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3465 "node enable: did:x%x",
3466 ndlp->nlp_DID, 0, 0);
3467 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003468}
3469
3470void
James Smart2e0fef82007-06-17 19:56:36 -05003471lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003472{
James Smart87af33f2007-10-27 13:37:43 -04003473 /*
James Smartfa4066b2008-01-11 01:53:27 -05003474 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003475 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003476 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3477 * until ALL other outstanding threads have completed. We check
3478 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003479 */
James Smartfa4066b2008-01-11 01:53:27 -05003480 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3481 return;
James Smart51ef4c22007-08-02 11:10:31 -04003482 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003483 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003484 return;
dea31012005-04-17 16:05:31 -05003485}
3486
3487/*
3488 * Start / ReStart rescue timer for Discovery / RSCN handling
3489 */
3490void
James Smart2e0fef82007-06-17 19:56:36 -05003491lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003492{
James Smart2e0fef82007-06-17 19:56:36 -05003493 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3494 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003495 uint32_t tmo;
3496
James Smart2e0fef82007-06-17 19:56:36 -05003497 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003498 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003499 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3500 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003501 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003502 * FC spec states we need 3 * ratov for CT requests
3503 */
3504 tmo = ((phba->fc_ratov * 3) + 3);
3505 }
dea31012005-04-17 16:05:31 -05003506
James Smart858c9f62007-06-17 19:56:39 -05003507
3508 if (!timer_pending(&vport->fc_disctmo)) {
3509 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3510 "set disc timer: tmo:x%x state:x%x flg:x%x",
3511 tmo, vport->port_state, vport->fc_flag);
3512 }
3513
James Smart2e0fef82007-06-17 19:56:36 -05003514 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3515 spin_lock_irq(shost->host_lock);
3516 vport->fc_flag |= FC_DISC_TMO;
3517 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003518
3519 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003520 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3521 "0247 Start Discovery Timer state x%x "
3522 "Data: x%x x%lx x%x x%x\n",
3523 vport->port_state, tmo,
3524 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3525 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003526
3527 return;
3528}
3529
3530/*
3531 * Cancel rescue timer for Discovery / RSCN handling
3532 */
3533int
James Smart2e0fef82007-06-17 19:56:36 -05003534lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003535{
James Smart2e0fef82007-06-17 19:56:36 -05003536 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003537 unsigned long iflags;
3538
James Smart858c9f62007-06-17 19:56:39 -05003539 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3540 "can disc timer: state:x%x rtry:x%x flg:x%x",
3541 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3542
dea31012005-04-17 16:05:31 -05003543 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003544 if (vport->fc_flag & FC_DISC_TMO) {
3545 spin_lock_irqsave(shost->host_lock, iflags);
3546 vport->fc_flag &= ~FC_DISC_TMO;
3547 spin_unlock_irqrestore(shost->host_lock, iflags);
3548 del_timer_sync(&vport->fc_disctmo);
3549 spin_lock_irqsave(&vport->work_port_lock, iflags);
3550 vport->work_port_events &= ~WORKER_DISC_TMO;
3551 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003552 }
3553
3554 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003555 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3556 "0248 Cancel Discovery Timer state x%x "
3557 "Data: x%x x%x x%x\n",
3558 vport->port_state, vport->fc_flag,
3559 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003560 return 0;
dea31012005-04-17 16:05:31 -05003561}
3562
3563/*
3564 * Check specified ring for outstanding IOCB on the SLI queue
3565 * Return true if iocb matches the specified nport
3566 */
3567int
James Smart2e0fef82007-06-17 19:56:36 -05003568lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3569 struct lpfc_sli_ring *pring,
3570 struct lpfc_iocbq *iocb,
3571 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003572{
James Smart2e0fef82007-06-17 19:56:36 -05003573 struct lpfc_sli *psli = &phba->sli;
3574 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003575 struct lpfc_vport *vport = ndlp->vport;
3576
3577 if (iocb->vport != vport)
3578 return 0;
3579
dea31012005-04-17 16:05:31 -05003580 if (pring->ringno == LPFC_ELS_RING) {
3581 switch (icmd->ulpCommand) {
3582 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003583 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003584 return 1;
dea31012005-04-17 16:05:31 -05003585 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003586 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3587 return 1;
dea31012005-04-17 16:05:31 -05003588 case CMD_XMIT_ELS_RSP64_CX:
3589 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003590 return 1;
dea31012005-04-17 16:05:31 -05003591 }
James Smarta4bc3372006-12-02 13:34:16 -05003592 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003593
3594 } else if (pring->ringno == psli->fcp_ring) {
3595 /* Skip match check if waiting to relogin to FCP target */
3596 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003597 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003598 return 0;
dea31012005-04-17 16:05:31 -05003599 }
3600 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003601 return 1;
dea31012005-04-17 16:05:31 -05003602 }
3603 } else if (pring->ringno == psli->next_ring) {
3604
3605 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003606 return 0;
dea31012005-04-17 16:05:31 -05003607}
3608
3609/*
3610 * Free resources / clean up outstanding I/Os
3611 * associated with nlp_rpi in the LPFC_NODELIST entry.
3612 */
3613static int
James Smart2e0fef82007-06-17 19:56:36 -05003614lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003615{
James Smart2534ba72007-04-25 09:52:20 -04003616 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003617 struct lpfc_sli *psli;
3618 struct lpfc_sli_ring *pring;
3619 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003620 uint32_t i;
dea31012005-04-17 16:05:31 -05003621
James Smart92d7f7b2007-06-17 19:56:38 -05003622 lpfc_fabric_abort_nport(ndlp);
3623
dea31012005-04-17 16:05:31 -05003624 /*
3625 * Everything that matches on txcmplq will be returned
3626 * by firmware with a no rpi error.
3627 */
3628 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003629 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003630 /* Now process each ring */
3631 for (i = 0; i < psli->num_rings; i++) {
3632 pring = &psli->ring[i];
3633
James Smart2e0fef82007-06-17 19:56:36 -05003634 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003635 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003636 list) {
dea31012005-04-17 16:05:31 -05003637 /*
3638 * Check to see if iocb matches the nport we are
3639 * looking for
3640 */
James Smart92d7f7b2007-06-17 19:56:38 -05003641 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3642 ndlp))) {
dea31012005-04-17 16:05:31 -05003643 /* It matches, so deque and call compl
3644 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003645 list_move_tail(&iocb->list,
3646 &completions);
dea31012005-04-17 16:05:31 -05003647 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003648 }
3649 }
James Smart2e0fef82007-06-17 19:56:36 -05003650 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003651 }
3652 }
James Smart2534ba72007-04-25 09:52:20 -04003653
James Smarta257bf92009-04-06 18:48:10 -04003654 /* Cancel all the IOCBs from the completions list */
3655 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3656 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003657
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003658 return 0;
dea31012005-04-17 16:05:31 -05003659}
3660
3661/*
3662 * Free rpi associated with LPFC_NODELIST entry.
3663 * This routine is called from lpfc_freenode(), when we are removing
3664 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3665 * LOGO that completes successfully, and we are waiting to PLOGI back
3666 * to the remote NPort. In addition, it is called after we receive
3667 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3668 * we are waiting to PLOGI back to the remote NPort.
3669 */
3670int
James Smart2e0fef82007-06-17 19:56:36 -05003671lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003672{
James Smart2e0fef82007-06-17 19:56:36 -05003673 struct lpfc_hba *phba = vport->phba;
3674 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003675 int rc;
3676
James Smart21e9a0a2009-05-22 14:53:21 -04003677 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003678 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3679 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003680 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003681 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003682 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003683 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003684 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003685 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003686 }
dea31012005-04-17 16:05:31 -05003687 lpfc_no_rpi(phba, ndlp);
3688 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003689 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003690 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003691 return 1;
3692 }
3693 return 0;
3694}
3695
James Smartecfd03c2010-02-12 14:41:27 -05003696/**
3697 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
3698 * @phba: pointer to lpfc hba data structure.
3699 *
3700 * This routine is invoked to unregister all the currently registered RPIs
3701 * to the HBA.
3702 **/
3703void
3704lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
3705{
3706 struct lpfc_vport **vports;
3707 struct lpfc_nodelist *ndlp;
3708 struct Scsi_Host *shost;
3709 int i;
3710
3711 vports = lpfc_create_vport_work_array(phba);
3712 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3713 shost = lpfc_shost_from_vport(vports[i]);
3714 spin_lock_irq(shost->host_lock);
3715 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003716 if (ndlp->nlp_flag & NLP_RPI_VALID) {
3717 /* The mempool_alloc might sleep */
3718 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05003719 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05003720 spin_lock_irq(shost->host_lock);
3721 }
James Smartecfd03c2010-02-12 14:41:27 -05003722 }
3723 spin_unlock_irq(shost->host_lock);
3724 }
3725 lpfc_destroy_vport_work_array(phba, vports);
3726}
3727
James Smart92d7f7b2007-06-17 19:56:38 -05003728void
3729lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3730{
3731 struct lpfc_hba *phba = vport->phba;
3732 LPFC_MBOXQ_t *mbox;
3733 int rc;
3734
3735 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3736 if (mbox) {
3737 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3738 mbox->vport = vport;
3739 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003740 mbox->context1 = NULL;
3741 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003742 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003743 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003744
3745 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3746 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3747 "1836 Could not issue "
3748 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003749 }
3750}
3751
3752void
3753lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3754{
3755 struct lpfc_hba *phba = vport->phba;
3756 LPFC_MBOXQ_t *mbox;
3757 int rc;
3758
3759 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3760 if (mbox) {
3761 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3762 mbox->vport = vport;
3763 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003764 mbox->context1 = NULL;
3765 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003766 if (rc != MBX_TIMEOUT)
3767 mempool_free(mbox, phba->mbox_mem_pool);
3768
3769 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003770 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3771 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003772 "unreg_did (default rpis) status %d\n",
3773 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003774 }
3775}
3776
dea31012005-04-17 16:05:31 -05003777/*
3778 * Free resources associated with LPFC_NODELIST entry
3779 * so it can be freed.
3780 */
3781static int
James Smart2e0fef82007-06-17 19:56:36 -05003782lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003783{
James Smart2e0fef82007-06-17 19:56:36 -05003784 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3785 struct lpfc_hba *phba = vport->phba;
3786 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003787 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003788
3789 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003790 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3791 "0900 Cleanup node for NPort x%x "
3792 "Data: x%x x%x x%x\n",
3793 ndlp->nlp_DID, ndlp->nlp_flag,
3794 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003795 if (NLP_CHK_FREE_REQ(ndlp)) {
3796 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3797 "0280 lpfc_cleanup_node: ndlp:x%p "
3798 "usgmap:x%x refcnt:%d\n",
3799 (void *)ndlp, ndlp->nlp_usg_map,
3800 atomic_read(&ndlp->kref.refcount));
3801 lpfc_dequeue_node(vport, ndlp);
3802 } else {
3803 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3804 "0281 lpfc_cleanup_node: ndlp:x%p "
3805 "usgmap:x%x refcnt:%d\n",
3806 (void *)ndlp, ndlp->nlp_usg_map,
3807 atomic_read(&ndlp->kref.refcount));
3808 lpfc_disable_node(vport, ndlp);
3809 }
dea31012005-04-17 16:05:31 -05003810
dea31012005-04-17 16:05:31 -05003811 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3812 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003813 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003814 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3815 mb->context2 = NULL;
3816 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3817 }
3818 }
James Smart33ccf8d2006-08-17 11:57:58 -04003819
James Smart2e0fef82007-06-17 19:56:36 -05003820 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003821 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003822 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003823 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003824 mp = (struct lpfc_dmabuf *) (mb->context1);
3825 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003826 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003827 kfree(mp);
3828 }
3829 list_del(&mb->list);
3830 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003831 /* We shall not invoke the lpfc_nlp_put to decrement
3832 * the ndlp reference count as we are in the process
3833 * of lpfc_nlp_release.
3834 */
dea31012005-04-17 16:05:31 -05003835 }
3836 }
James Smart2e0fef82007-06-17 19:56:36 -05003837 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003838
James Smarte47c9092008-02-08 18:49:26 -05003839 lpfc_els_abort(phba, ndlp);
3840
James Smart2e0fef82007-06-17 19:56:36 -05003841 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003842 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003843 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003844
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003845 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003846 del_timer_sync(&ndlp->nlp_delayfunc);
3847
James Smart0d2b6b82008-06-14 22:52:47 -04003848 list_del_init(&ndlp->els_retry_evt.evt_listp);
3849 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003850
James Smart2e0fef82007-06-17 19:56:36 -05003851 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003852
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003853 return 0;
dea31012005-04-17 16:05:31 -05003854}
3855
3856/*
3857 * Check to see if we can free the nlp back to the freelist.
3858 * If we are in the middle of using the nlp in the discovery state
3859 * machine, defer the free till we reach the end of the state machine.
3860 */
James Smart329f9bc2007-04-25 09:53:01 -04003861static void
James Smart2e0fef82007-06-17 19:56:36 -05003862lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003863{
James Smarta8adb832007-10-27 13:37:53 -04003864 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003865 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003866 LPFC_MBOXQ_t *mbox;
3867 int rc;
dea31012005-04-17 16:05:31 -05003868
James Smart0d2b6b82008-06-14 22:52:47 -04003869 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003870 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3871 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003872 /* For this case we need to cleanup the default rpi
3873 * allocated by the firmware.
3874 */
3875 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3876 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003877 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003878 (uint8_t *) &vport->fc_sparam, mbox, 0);
3879 if (rc) {
3880 mempool_free(mbox, phba->mbox_mem_pool);
3881 }
3882 else {
3883 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3884 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3885 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003886 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003887 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3888 if (rc == MBX_NOT_FINISHED) {
3889 mempool_free(mbox, phba->mbox_mem_pool);
3890 }
3891 }
3892 }
3893 }
James Smart2e0fef82007-06-17 19:56:36 -05003894 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003895
James Smart2e0fef82007-06-17 19:56:36 -05003896 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003897 * We can get here with a non-NULL ndlp->rport because when we
3898 * unregister a rport we don't break the rport/node linkage. So if we
3899 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003900 */
James Smart92d7f7b2007-06-17 19:56:38 -05003901 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003902 rdata = ndlp->rport->dd_data;
3903 rdata->pnode = NULL;
3904 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003905 }
dea31012005-04-17 16:05:31 -05003906}
3907
3908static int
James Smart2e0fef82007-06-17 19:56:36 -05003909lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3910 uint32_t did)
dea31012005-04-17 16:05:31 -05003911{
James Smart2e0fef82007-06-17 19:56:36 -05003912 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003913
3914 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003915 return 0;
dea31012005-04-17 16:05:31 -05003916
dea31012005-04-17 16:05:31 -05003917 /* First check for Direct match */
3918 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003919 return 1;
dea31012005-04-17 16:05:31 -05003920
3921 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003922 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003923 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003924 return 0;
dea31012005-04-17 16:05:31 -05003925 }
3926
3927 matchdid.un.word = did;
3928 ndlpdid.un.word = ndlp->nlp_DID;
3929 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3930 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3931 (mydid.un.b.area == matchdid.un.b.area)) {
3932 if ((ndlpdid.un.b.domain == 0) &&
3933 (ndlpdid.un.b.area == 0)) {
3934 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003935 return 1;
dea31012005-04-17 16:05:31 -05003936 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003937 return 0;
dea31012005-04-17 16:05:31 -05003938 }
3939
3940 matchdid.un.word = ndlp->nlp_DID;
3941 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3942 (mydid.un.b.area == ndlpdid.un.b.area)) {
3943 if ((matchdid.un.b.domain == 0) &&
3944 (matchdid.un.b.area == 0)) {
3945 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003946 return 1;
dea31012005-04-17 16:05:31 -05003947 }
3948 }
3949 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003950 return 0;
dea31012005-04-17 16:05:31 -05003951}
3952
James Smart685f0bf2007-04-25 09:53:08 -04003953/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003954static struct lpfc_nodelist *
3955__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003956{
James Smart2fb9bd82006-12-02 13:33:57 -05003957 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003958 uint32_t data1;
3959
James Smart2e0fef82007-06-17 19:56:36 -05003960 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3961 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04003962 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3963 ((uint32_t) ndlp->nlp_xri << 16) |
3964 ((uint32_t) ndlp->nlp_type << 8) |
3965 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04003966 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3967 "0929 FIND node DID "
3968 "Data: x%p x%x x%x x%x\n",
3969 ndlp, ndlp->nlp_DID,
3970 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04003971 return ndlp;
dea31012005-04-17 16:05:31 -05003972 }
3973 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003974
dea31012005-04-17 16:05:31 -05003975 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04003976 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3977 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05003978 return NULL;
3979}
3980
3981struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003982lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003983{
James Smart2e0fef82007-06-17 19:56:36 -05003984 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003985 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003986
James Smart2e0fef82007-06-17 19:56:36 -05003987 spin_lock_irq(shost->host_lock);
3988 ndlp = __lpfc_findnode_did(vport, did);
3989 spin_unlock_irq(shost->host_lock);
3990 return ndlp;
3991}
3992
3993struct lpfc_nodelist *
3994lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3995{
3996 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3997 struct lpfc_nodelist *ndlp;
3998
3999 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004000 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004001 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4002 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004003 return NULL;
4004 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004005 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004006 if (!ndlp)
4007 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004008 lpfc_nlp_init(vport, ndlp, did);
4009 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4010 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004011 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004012 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004013 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004014 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4015 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4016 if (!ndlp)
4017 return NULL;
4018 spin_lock_irq(shost->host_lock);
4019 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4020 spin_unlock_irq(shost->host_lock);
4021 return ndlp;
dea31012005-04-17 16:05:31 -05004022 }
James Smarte47c9092008-02-08 18:49:26 -05004023
James Smart58da1ff2008-04-07 10:15:56 -04004024 if ((vport->fc_flag & FC_RSCN_MODE) &&
4025 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004026 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04004027 /* If we've already recieved a PLOGI from this NPort
4028 * we don't need to try to discover it again.
4029 */
4030 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4031 return NULL;
4032
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004033 /* Since this node is marked for discovery,
4034 * delay timeout is not needed.
4035 */
James Smart0d2b6b82008-06-14 22:52:47 -04004036 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004037 spin_lock_irq(shost->host_lock);
4038 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4039 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004040 } else
dea31012005-04-17 16:05:31 -05004041 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004042 } else {
James Smart87af33f2007-10-27 13:37:43 -04004043 /* If we've already recieved a PLOGI from this NPort,
4044 * or we are already in the process of discovery on it,
4045 * we don't need to try to discover it again.
4046 */
James Smart685f0bf2007-04-25 09:53:08 -04004047 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004048 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4049 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004050 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004051 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4052 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004053 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004054 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004055 }
4056 return ndlp;
4057}
4058
4059/* Build a list of nodes to discover based on the loopmap */
4060void
James Smart2e0fef82007-06-17 19:56:36 -05004061lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004062{
James Smart2e0fef82007-06-17 19:56:36 -05004063 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004064 int j;
4065 uint32_t alpa, index;
4066
James Smart2e0fef82007-06-17 19:56:36 -05004067 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004068 return;
James Smart2e0fef82007-06-17 19:56:36 -05004069
4070 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004071 return;
dea31012005-04-17 16:05:31 -05004072
4073 /* Check for loop map present or not */
4074 if (phba->alpa_map[0]) {
4075 for (j = 1; j <= phba->alpa_map[0]; j++) {
4076 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004077 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004078 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004079 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004080 }
4081 } else {
4082 /* No alpamap, so try all alpa's */
4083 for (j = 0; j < FC_MAXLOOP; j++) {
4084 /* If cfg_scan_down is set, start from highest
4085 * ALPA (0xef) to lowest (0x1).
4086 */
James Smart3de2a652007-08-02 11:09:59 -04004087 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004088 index = j;
4089 else
4090 index = FC_MAXLOOP - j - 1;
4091 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004092 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004093 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004094 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004095 }
4096 }
4097 return;
4098}
4099
dea31012005-04-17 16:05:31 -05004100void
James Smart2e0fef82007-06-17 19:56:36 -05004101lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004102{
dea31012005-04-17 16:05:31 -05004103 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004104 struct lpfc_sli *psli = &phba->sli;
4105 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4106 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4107 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4108 int rc;
4109
James Smart92d7f7b2007-06-17 19:56:38 -05004110 /*
4111 * if it's not a physical port or if we already send
4112 * clear_la then don't send it.
4113 */
4114 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004115 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4116 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004117 return;
4118
James Smart2e0fef82007-06-17 19:56:36 -05004119 /* Link up discovery */
4120 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4121 phba->link_state = LPFC_CLEAR_LA;
4122 lpfc_clear_la(phba, mbox);
4123 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4124 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004125 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004126 if (rc == MBX_NOT_FINISHED) {
4127 mempool_free(mbox, phba->mbox_mem_pool);
4128 lpfc_disc_flush_list(vport);
4129 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4130 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4131 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004132 phba->link_state = LPFC_HBA_ERROR;
4133 }
4134 }
4135}
4136
4137/* Reg_vpi to tell firmware to resume normal operations */
4138void
4139lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4140{
4141 LPFC_MBOXQ_t *regvpimbox;
4142
4143 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4144 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004145 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004146 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4147 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004148 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004149 == MBX_NOT_FINISHED) {
4150 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004151 }
4152 }
4153}
4154
4155/* Start Link up / RSCN discovery on NPR nodes */
4156void
4157lpfc_disc_start(struct lpfc_vport *vport)
4158{
4159 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4160 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004161 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004162 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004163 int did_changed;
dea31012005-04-17 16:05:31 -05004164
James Smart2e0fef82007-06-17 19:56:36 -05004165 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004166 return;
James Smart2e0fef82007-06-17 19:56:36 -05004167
4168 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004169 clear_la_pending = 1;
4170 else
4171 clear_la_pending = 0;
4172
James Smart2e0fef82007-06-17 19:56:36 -05004173 if (vport->port_state < LPFC_VPORT_READY)
4174 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004175
James Smart2e0fef82007-06-17 19:56:36 -05004176 lpfc_set_disctmo(vport);
4177
4178 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004179 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004180 else
dea31012005-04-17 16:05:31 -05004181 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004182
4183 vport->fc_prevDID = vport->fc_myDID;
4184 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004185
4186 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004187 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4188 "0202 Start Discovery hba state x%x "
4189 "Data: x%x x%x x%x\n",
4190 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4191 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004192
4193 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004194 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004195
4196 if (num_sent)
4197 return;
4198
James Smart92d7f7b2007-06-17 19:56:38 -05004199 /*
4200 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
4201 * continue discovery.
4202 */
4203 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004204 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004205 !(vport->fc_flag & FC_RSCN_MODE) &&
4206 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004207 lpfc_issue_reg_vpi(phba, vport);
4208 return;
4209 }
James Smart2e0fef82007-06-17 19:56:36 -05004210
James Smart92d7f7b2007-06-17 19:56:38 -05004211 /*
4212 * For SLI2, we need to set port_state to READY and continue
4213 * discovery.
4214 */
4215 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4216 /* If we get here, there is nothing to ADISC */
4217 if (vport->port_type == LPFC_PHYSICAL_PORT)
4218 lpfc_issue_clear_la(phba, vport);
4219
4220 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004221 vport->num_disc_nodes = 0;
4222 /* go thru NPR nodes and issue ELS PLOGIs */
4223 if (vport->fc_npr_cnt)
4224 lpfc_els_disc_plogi(vport);
4225
4226 if (!vport->num_disc_nodes) {
4227 spin_lock_irq(shost->host_lock);
4228 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4229 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004230 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05004231 }
4232 }
James Smart92d7f7b2007-06-17 19:56:38 -05004233 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004234 } else {
4235 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004236 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05004237
4238 if (num_sent)
4239 return;
4240
James Smart2e0fef82007-06-17 19:56:36 -05004241 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05004242 /* Check to see if more RSCNs came in while we
4243 * were processing this one.
4244 */
James Smart2e0fef82007-06-17 19:56:36 -05004245 if ((vport->fc_rscn_id_cnt == 0) &&
4246 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
4247 spin_lock_irq(shost->host_lock);
4248 vport->fc_flag &= ~FC_RSCN_MODE;
4249 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004250 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004251 } else
James Smart2e0fef82007-06-17 19:56:36 -05004252 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05004253 }
4254 }
4255 return;
4256}
4257
4258/*
4259 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
4260 * ring the match the sppecified nodelist.
4261 */
4262static void
James Smart2e0fef82007-06-17 19:56:36 -05004263lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004264{
James Smart2534ba72007-04-25 09:52:20 -04004265 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004266 struct lpfc_sli *psli;
4267 IOCB_t *icmd;
4268 struct lpfc_iocbq *iocb, *next_iocb;
4269 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004270
4271 psli = &phba->sli;
4272 pring = &psli->ring[LPFC_ELS_RING];
4273
4274 /* Error matching iocb on txq or txcmplq
4275 * First check the txq.
4276 */
James Smart2e0fef82007-06-17 19:56:36 -05004277 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004278 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4279 if (iocb->context1 != ndlp) {
4280 continue;
4281 }
4282 icmd = &iocb->iocb;
4283 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
4284 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
4285
James Smart2534ba72007-04-25 09:52:20 -04004286 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05004287 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05004288 }
4289 }
4290
4291 /* Next check the txcmplq */
4292 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
4293 if (iocb->context1 != ndlp) {
4294 continue;
4295 }
4296 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004297 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
4298 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04004299 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05004300 }
4301 }
James Smart2e0fef82007-06-17 19:56:36 -05004302 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04004303
James Smarta257bf92009-04-06 18:48:10 -04004304 /* Cancel all the IOCBs from the completions list */
4305 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4306 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004307}
4308
Adrian Bunka6ababd2007-11-05 18:07:33 +01004309static void
James Smart2e0fef82007-06-17 19:56:36 -05004310lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004311{
4312 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05004313 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004314
James Smart2e0fef82007-06-17 19:56:36 -05004315 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
4316 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004317 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004318 if (!NLP_CHK_NODE_ACT(ndlp))
4319 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004320 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4321 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
4322 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04004323 }
dea31012005-04-17 16:05:31 -05004324 }
4325 }
dea31012005-04-17 16:05:31 -05004326}
4327
James Smart92d7f7b2007-06-17 19:56:38 -05004328void
4329lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
4330{
4331 lpfc_els_flush_rscn(vport);
4332 lpfc_els_flush_cmd(vport);
4333 lpfc_disc_flush_list(vport);
4334}
4335
dea31012005-04-17 16:05:31 -05004336/*****************************************************************************/
4337/*
4338 * NAME: lpfc_disc_timeout
4339 *
4340 * FUNCTION: Fibre Channel driver discovery timeout routine.
4341 *
4342 * EXECUTION ENVIRONMENT: interrupt only
4343 *
4344 * CALLED FROM:
4345 * Timer function
4346 *
4347 * RETURNS:
4348 * none
4349 */
4350/*****************************************************************************/
4351void
4352lpfc_disc_timeout(unsigned long ptr)
4353{
James Smart2e0fef82007-06-17 19:56:36 -05004354 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4355 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04004356 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004357 unsigned long flags = 0;
4358
4359 if (unlikely(!phba))
4360 return;
4361
James Smart5e9d9b82008-06-14 22:52:53 -04004362 spin_lock_irqsave(&vport->work_port_lock, flags);
4363 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4364 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05004365 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04004366 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05004367
James Smart5e9d9b82008-06-14 22:52:53 -04004368 if (!tmo_posted)
4369 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004370 return;
4371}
4372
4373static void
James Smart2e0fef82007-06-17 19:56:36 -05004374lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004375{
James Smart2e0fef82007-06-17 19:56:36 -05004376 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4377 struct lpfc_hba *phba = vport->phba;
4378 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004379 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05004380 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05004381 int rc, clrlaerr = 0;
4382
James Smart2e0fef82007-06-17 19:56:36 -05004383 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05004384 return;
4385
James Smart2e0fef82007-06-17 19:56:36 -05004386 spin_lock_irq(shost->host_lock);
4387 vport->fc_flag &= ~FC_DISC_TMO;
4388 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004389
James Smart858c9f62007-06-17 19:56:39 -05004390 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4391 "disc timeout: state:x%x rtry:x%x flg:x%x",
4392 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4393
James Smart2e0fef82007-06-17 19:56:36 -05004394 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05004395
4396 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05004397 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
4398 * FAN
4399 */
4400 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04004401 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4402 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004403 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05004404 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004405 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004406 if (!NLP_CHK_NODE_ACT(ndlp))
4407 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004408 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4409 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004410 if (ndlp->nlp_type & NLP_FABRIC) {
4411 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05004412 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004413
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004414 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004415 /* Fail outstanding IO now since device
4416 * is marked for PLOGI.
4417 */
James Smart2e0fef82007-06-17 19:56:36 -05004418 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004419 }
4420 }
James Smart92d7f7b2007-06-17 19:56:38 -05004421 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05004422 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05004423 return;
James Smart92d7f7b2007-06-17 19:56:38 -05004424 }
dea31012005-04-17 16:05:31 -05004425 break;
4426
James Smart92d7f7b2007-06-17 19:56:38 -05004427 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05004428 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05004429 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05004430 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04004431 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4432 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04004433 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05004434
4435 /* Assume no Fabric and go on with discovery.
4436 * Check for outstanding ELS FLOGI to abort.
4437 */
4438
4439 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05004440 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004441
4442 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05004443 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05004444 break;
4445
4446 case LPFC_FABRIC_CFG_LINK:
4447 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4448 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04004449 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4450 "0223 Timeout while waiting for "
4451 "NameServer login\n");
dea31012005-04-17 16:05:31 -05004452 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05004453 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05004454 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04004455 lpfc_els_abort(phba, ndlp);
4456
4457 /* ReStart discovery */
4458 goto restart_disc;
dea31012005-04-17 16:05:31 -05004459
4460 case LPFC_NS_QRY:
4461 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04004462 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4463 "0224 NameServer Query timeout "
4464 "Data: x%x x%x\n",
4465 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004466
James Smart92d7f7b2007-06-17 19:56:38 -05004467 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4468 /* Try it one more time */
4469 vport->fc_ns_retry++;
4470 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4471 vport->fc_ns_retry, 0);
4472 if (rc == 0)
4473 break;
dea31012005-04-17 16:05:31 -05004474 }
James Smart92d7f7b2007-06-17 19:56:38 -05004475 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05004476
James Smart87af33f2007-10-27 13:37:43 -04004477restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05004478 /*
4479 * Discovery is over.
4480 * set port_state to PORT_READY if SLI2.
4481 * cmpl_reg_vpi will set port_state to READY for SLI3.
4482 */
James Smart3772a992009-05-22 14:50:54 -04004483 if (phba->sli_rev < LPFC_SLI_REV4) {
4484 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4485 lpfc_issue_reg_vpi(phba, vport);
4486 else { /* NPIV Not enabled */
4487 lpfc_issue_clear_la(phba, vport);
4488 vport->port_state = LPFC_VPORT_READY;
4489 }
dea31012005-04-17 16:05:31 -05004490 }
4491
4492 /* Setup and issue mailbox INITIALIZE LINK command */
4493 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4494 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004495 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4496 "0206 Device Discovery "
4497 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004498 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004499 break;
4500 }
4501
4502 lpfc_linkdown(phba);
4503 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4504 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004505 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004506 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004507 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004508 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004509 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004510 if (rc == MBX_NOT_FINISHED)
4511 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4512
4513 break;
4514
4515 case LPFC_DISC_AUTH:
4516 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004517 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4518 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004519 lpfc_disc_flush_list(vport);
4520
James Smart92d7f7b2007-06-17 19:56:38 -05004521 /*
4522 * set port_state to PORT_READY if SLI2.
4523 * cmpl_reg_vpi will set port_state to READY for SLI3.
4524 */
James Smart3772a992009-05-22 14:50:54 -04004525 if (phba->sli_rev < LPFC_SLI_REV4) {
4526 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4527 lpfc_issue_reg_vpi(phba, vport);
4528 else { /* NPIV Not enabled */
4529 lpfc_issue_clear_la(phba, vport);
4530 vport->port_state = LPFC_VPORT_READY;
4531 }
dea31012005-04-17 16:05:31 -05004532 }
4533 break;
4534
James Smart2e0fef82007-06-17 19:56:36 -05004535 case LPFC_VPORT_READY:
4536 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004537 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4538 "0231 RSCN timeout Data: x%x "
4539 "x%x\n",
4540 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004541
4542 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004543 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004544
James Smart2e0fef82007-06-17 19:56:36 -05004545 lpfc_els_flush_rscn(vport);
4546 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004547 }
4548 break;
James Smart2e0fef82007-06-17 19:56:36 -05004549
James Smart92d7f7b2007-06-17 19:56:38 -05004550 default:
James Smarte8b62012007-08-02 11:10:09 -04004551 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004552 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004553 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004554 break;
4555 }
4556
4557 switch (phba->link_state) {
4558 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004559 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004560 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4561 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004562 clrlaerr = 1;
4563 break;
4564
James Smart09372822008-01-11 01:52:54 -05004565 case LPFC_LINK_UP:
4566 lpfc_issue_clear_la(phba, vport);
4567 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004568 case LPFC_LINK_UNKNOWN:
4569 case LPFC_WARM_START:
4570 case LPFC_INIT_START:
4571 case LPFC_INIT_MBX_CMDS:
4572 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004573 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004574 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4575 "0230 Unexpected timeout, hba link "
4576 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004577 clrlaerr = 1;
4578 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004579
4580 case LPFC_HBA_READY:
4581 break;
dea31012005-04-17 16:05:31 -05004582 }
4583
4584 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004585 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004586 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004587 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4588 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004589 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004590 }
4591
4592 return;
4593}
4594
dea31012005-04-17 16:05:31 -05004595/*
4596 * This routine handles processing a NameServer REG_LOGIN mailbox
4597 * command upon completion. It is setup in the LPFC_MBOXQ
4598 * as the completion routine when the command is
4599 * handed off to the SLI layer.
4600 */
4601void
James Smart2e0fef82007-06-17 19:56:36 -05004602lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004603{
James Smart04c68492009-05-22 14:52:52 -04004604 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004605 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4606 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4607 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004608
4609 pmb->context1 = NULL;
4610
dea31012005-04-17 16:05:31 -05004611 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004612 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004613 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004614 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004615
James Smart2e0fef82007-06-17 19:56:36 -05004616 /*
4617 * Start issuing Fabric-Device Management Interface (FDMI) command to
4618 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4619 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004620 */
James Smart2e0fef82007-06-17 19:56:36 -05004621
James Smart3de2a652007-08-02 11:09:59 -04004622 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004623 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4624 else
4625 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004626
James Smartfa4066b2008-01-11 01:53:27 -05004627 /* decrement the node reference count held for this callback
4628 * function.
4629 */
James Smart329f9bc2007-04-25 09:53:01 -04004630 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004631 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4632 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004633 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004634
4635 return;
4636}
4637
James Smart685f0bf2007-04-25 09:53:08 -04004638static int
4639lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4640{
4641 uint16_t *rpi = param;
4642
4643 return ndlp->nlp_rpi == *rpi;
4644}
4645
4646static int
4647lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4648{
4649 return memcmp(&ndlp->nlp_portname, param,
4650 sizeof(ndlp->nlp_portname)) == 0;
4651}
4652
Adrian Bunka6ababd2007-11-05 18:07:33 +01004653static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004654__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004655{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004656 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004657
James Smart2e0fef82007-06-17 19:56:36 -05004658 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004659 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004660 return ndlp;
4661 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004662 return NULL;
dea31012005-04-17 16:05:31 -05004663}
4664
James Smart685f0bf2007-04-25 09:53:08 -04004665/*
4666 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004667 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004668 */
4669struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004670__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004671{
James Smart2e0fef82007-06-17 19:56:36 -05004672 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004673}
4674
James Smart488d1462006-03-07 15:02:37 -05004675/*
James Smart685f0bf2007-04-25 09:53:08 -04004676 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004677 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004678 */
4679struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004680lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004681{
James Smart2e0fef82007-06-17 19:56:36 -05004682 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004683 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004684
James Smart2e0fef82007-06-17 19:56:36 -05004685 spin_lock_irq(shost->host_lock);
4686 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4687 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004688 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004689}
4690
dea31012005-04-17 16:05:31 -05004691void
James Smart2e0fef82007-06-17 19:56:36 -05004692lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4693 uint32_t did)
dea31012005-04-17 16:05:31 -05004694{
4695 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004696
4697 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004698 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004699
4700 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4701 "node init: did:x%x",
4702 ndlp->nlp_DID, 0, 0);
4703
dea31012005-04-17 16:05:31 -05004704 return;
4705}
James Smart329f9bc2007-04-25 09:53:01 -04004706
James Smart98c9ea52007-10-27 13:37:33 -04004707/* This routine releases all resources associated with a specifc NPort's ndlp
4708 * and mempool_free's the nodelist.
4709 */
James Smart311464e2007-08-02 11:10:37 -04004710static void
James Smart329f9bc2007-04-25 09:53:01 -04004711lpfc_nlp_release(struct kref *kref)
4712{
James Smarte47c9092008-02-08 18:49:26 -05004713 struct lpfc_hba *phba;
4714 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004715 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4716 kref);
James Smart858c9f62007-06-17 19:56:39 -05004717
4718 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4719 "node release: did:x%x flg:x%x type:x%x",
4720 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4721
James Smarte47c9092008-02-08 18:49:26 -05004722 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4723 "0279 lpfc_nlp_release: ndlp:x%p "
4724 "usgmap:x%x refcnt:%d\n",
4725 (void *)ndlp, ndlp->nlp_usg_map,
4726 atomic_read(&ndlp->kref.refcount));
4727
4728 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004729 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004730
4731 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004732 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004733 spin_lock_irqsave(&phba->ndlp_lock, flags);
4734 NLP_CLR_NODE_ACT(ndlp);
4735 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4736
4737 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004738 if (NLP_CHK_FREE_REQ(ndlp)) {
4739 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004740 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004741 }
James Smart329f9bc2007-04-25 09:53:01 -04004742}
4743
James Smart98c9ea52007-10-27 13:37:33 -04004744/* This routine bumps the reference count for a ndlp structure to ensure
4745 * that one discovery thread won't free a ndlp while another discovery thread
4746 * is using it.
4747 */
James Smart329f9bc2007-04-25 09:53:01 -04004748struct lpfc_nodelist *
4749lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4750{
James Smarte47c9092008-02-08 18:49:26 -05004751 struct lpfc_hba *phba;
4752 unsigned long flags;
4753
James Smart98c9ea52007-10-27 13:37:33 -04004754 if (ndlp) {
4755 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4756 "node get: did:x%x flg:x%x refcnt:x%x",
4757 ndlp->nlp_DID, ndlp->nlp_flag,
4758 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004759 /* The check of ndlp usage to prevent incrementing the
4760 * ndlp reference count that is in the process of being
4761 * released.
4762 */
James Smarta257bf92009-04-06 18:48:10 -04004763 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004764 spin_lock_irqsave(&phba->ndlp_lock, flags);
4765 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4766 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4767 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4768 "0276 lpfc_nlp_get: ndlp:x%p "
4769 "usgmap:x%x refcnt:%d\n",
4770 (void *)ndlp, ndlp->nlp_usg_map,
4771 atomic_read(&ndlp->kref.refcount));
4772 return NULL;
4773 } else
4774 kref_get(&ndlp->kref);
4775 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004776 }
James Smart329f9bc2007-04-25 09:53:01 -04004777 return ndlp;
4778}
4779
James Smart98c9ea52007-10-27 13:37:33 -04004780/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004781 * count goes to 0, this indicates the the associated nodelist should be
4782 * freed. Returning 1 indicates the ndlp resource has been released; on the
4783 * other hand, returning 0 indicates the ndlp resource has not been released
4784 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004785 */
James Smart329f9bc2007-04-25 09:53:01 -04004786int
4787lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4788{
James Smarte47c9092008-02-08 18:49:26 -05004789 struct lpfc_hba *phba;
4790 unsigned long flags;
4791
4792 if (!ndlp)
4793 return 1;
4794
4795 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4796 "node put: did:x%x flg:x%x refcnt:x%x",
4797 ndlp->nlp_DID, ndlp->nlp_flag,
4798 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004799 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004800 spin_lock_irqsave(&phba->ndlp_lock, flags);
4801 /* Check the ndlp memory free acknowledge flag to avoid the
4802 * possible race condition that kref_put got invoked again
4803 * after previous one has done ndlp memory free.
4804 */
4805 if (NLP_CHK_FREE_ACK(ndlp)) {
4806 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4807 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4808 "0274 lpfc_nlp_put: ndlp:x%p "
4809 "usgmap:x%x refcnt:%d\n",
4810 (void *)ndlp, ndlp->nlp_usg_map,
4811 atomic_read(&ndlp->kref.refcount));
4812 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004813 }
James Smarte47c9092008-02-08 18:49:26 -05004814 /* Check the ndlp inactivate log flag to avoid the possible
4815 * race condition that kref_put got invoked again after ndlp
4816 * is already in inactivating state.
4817 */
4818 if (NLP_CHK_IACT_REQ(ndlp)) {
4819 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4820 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4821 "0275 lpfc_nlp_put: ndlp:x%p "
4822 "usgmap:x%x refcnt:%d\n",
4823 (void *)ndlp, ndlp->nlp_usg_map,
4824 atomic_read(&ndlp->kref.refcount));
4825 return 1;
4826 }
4827 /* For last put, mark the ndlp usage flags to make sure no
4828 * other kref_get and kref_put on the same ndlp shall get
4829 * in between the process when the final kref_put has been
4830 * invoked on this ndlp.
4831 */
4832 if (atomic_read(&ndlp->kref.refcount) == 1) {
4833 /* Indicate ndlp is put to inactive state. */
4834 NLP_SET_IACT_REQ(ndlp);
4835 /* Acknowledge ndlp memory free has been seen. */
4836 if (NLP_CHK_FREE_REQ(ndlp))
4837 NLP_SET_FREE_ACK(ndlp);
4838 }
4839 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4840 /* Note, the kref_put returns 1 when decrementing a reference
4841 * count that was 1, it invokes the release callback function,
4842 * but it still left the reference count as 1 (not actually
4843 * performs the last decrementation). Otherwise, it actually
4844 * decrements the reference count and returns 0.
4845 */
4846 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004847}
James Smart98c9ea52007-10-27 13:37:33 -04004848
4849/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004850 * by any other discovery thread. This routine returns 1 if the
4851 * ndlp has been freed. A return value of 0 indicates the ndlp is
4852 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004853 */
4854int
4855lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4856{
4857 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4858 "node not used: did:x%x flg:x%x refcnt:x%x",
4859 ndlp->nlp_DID, ndlp->nlp_flag,
4860 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004861 if (atomic_read(&ndlp->kref.refcount) == 1)
4862 if (lpfc_nlp_put(ndlp))
4863 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004864 return 0;
4865}
James Smart6fb120a2009-05-22 14:52:59 -04004866
4867/**
4868 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4869 * @phba: Pointer to hba context object.
4870 *
4871 * This function iterate through all FC nodes associated
4872 * will all vports to check if there is any node with
4873 * fc_rports associated with it. If there is an fc_rport
4874 * associated with the node, then the node is either in
4875 * discovered state or its devloss_timer is pending.
4876 */
4877static int
4878lpfc_fcf_inuse(struct lpfc_hba *phba)
4879{
4880 struct lpfc_vport **vports;
4881 int i, ret = 0;
4882 struct lpfc_nodelist *ndlp;
4883 struct Scsi_Host *shost;
4884
4885 vports = lpfc_create_vport_work_array(phba);
4886
4887 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4888 shost = lpfc_shost_from_vport(vports[i]);
4889 spin_lock_irq(shost->host_lock);
4890 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4891 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4892 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4893 ret = 1;
4894 spin_unlock_irq(shost->host_lock);
4895 goto out;
James Smart891478a2009-11-18 15:40:23 -05004896 } else {
4897 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4898 "2624 RPI %x DID %x flg %x still "
4899 "logged in\n",
4900 ndlp->nlp_rpi, ndlp->nlp_DID,
4901 ndlp->nlp_flag);
4902 if (ndlp->nlp_flag & NLP_RPI_VALID)
4903 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04004904 }
4905 }
4906 spin_unlock_irq(shost->host_lock);
4907 }
4908out:
4909 lpfc_destroy_vport_work_array(phba, vports);
4910 return ret;
4911}
4912
4913/**
4914 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4915 * @phba: Pointer to hba context object.
4916 * @mboxq: Pointer to mailbox object.
4917 *
4918 * This function frees memory associated with the mailbox command.
4919 */
4920static void
4921lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4922{
4923 struct lpfc_vport *vport = mboxq->vport;
4924
4925 if (mboxq->u.mb.mbxStatus) {
4926 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4927 "2555 UNREG_VFI mbxStatus error x%x "
4928 "HBA state x%x\n",
4929 mboxq->u.mb.mbxStatus, vport->port_state);
4930 }
4931 mempool_free(mboxq, phba->mbox_mem_pool);
4932 return;
4933}
4934
4935/**
4936 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4937 * @phba: Pointer to hba context object.
4938 * @mboxq: Pointer to mailbox object.
4939 *
4940 * This function frees memory associated with the mailbox command.
4941 */
4942static void
4943lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4944{
4945 struct lpfc_vport *vport = mboxq->vport;
4946
4947 if (mboxq->u.mb.mbxStatus) {
4948 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4949 "2550 UNREG_FCFI mbxStatus error x%x "
4950 "HBA state x%x\n",
4951 mboxq->u.mb.mbxStatus, vport->port_state);
4952 }
4953 mempool_free(mboxq, phba->mbox_mem_pool);
4954 return;
4955}
4956
4957/**
James Smartecfd03c2010-02-12 14:41:27 -05004958 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04004959 * @phba: Pointer to hba context object.
4960 *
James Smartecfd03c2010-02-12 14:41:27 -05004961 * This function prepare the HBA for unregistering the currently registered
4962 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
4963 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04004964 */
James Smartecfd03c2010-02-12 14:41:27 -05004965int
4966lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04004967{
4968 LPFC_MBOXQ_t *mbox;
James Smart6fb120a2009-05-22 14:52:59 -04004969 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05004970 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05004971 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05004972 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04004973
James Smartecfd03c2010-02-12 14:41:27 -05004974 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04004975 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05004976 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04004977
James Smart4d9ab992009-10-02 15:16:39 -04004978 /* At this point, all discovery is aborted */
4979 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04004980
4981 /* Unregister VPIs */
4982 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05004983 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04004984 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05004985 /* Stop FLOGI/FDISC retries */
4986 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
4987 if (ndlp)
4988 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart6fb120a2009-05-22 14:52:59 -04004989 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05004990 shost = lpfc_shost_from_vport(vports[i]);
4991 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05004992 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05004993 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05004994 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04004995 }
4996 lpfc_destroy_vport_work_array(phba, vports);
4997
James Smart695a8142010-01-26 23:08:03 -05004998 /* Cleanup any outstanding ELS commands */
4999 lpfc_els_flush_all_cmd(phba);
5000
James Smart6fb120a2009-05-22 14:52:59 -04005001 /* Unregister VFI */
5002 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5003 if (!mbox) {
5004 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005005 "2556 UNREG_VFI mbox allocation failed"
5006 "HBA state x%x\n", phba->pport->port_state);
5007 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005008 }
5009
James Smart6669f9b2009-10-02 15:16:45 -04005010 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04005011 mbox->vport = phba->pport;
5012 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
5013
5014 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5015 if (rc == MBX_NOT_FINISHED) {
5016 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005017 "2557 UNREG_VFI issue mbox failed rc x%x "
5018 "HBA state x%x\n",
5019 rc, phba->pport->port_state);
James Smart6fb120a2009-05-22 14:52:59 -04005020 mempool_free(mbox, phba->mbox_mem_pool);
James Smartecfd03c2010-02-12 14:41:27 -05005021 return -EIO;
James Smart6fb120a2009-05-22 14:52:59 -04005022 }
5023
James Smart72100cc2010-02-12 14:43:01 -05005024 shost = lpfc_shost_from_vport(phba->pport);
5025 spin_lock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005026 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005027 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005028
James Smartecfd03c2010-02-12 14:41:27 -05005029 return 0;
5030}
5031
5032/**
5033 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5034 * @phba: Pointer to hba context object.
5035 *
5036 * This function issues synchronous unregister FCF mailbox command to HBA to
5037 * unregister the currently registered FCF record. The driver does not reset
5038 * the driver FCF usage state flags.
5039 *
5040 * Return 0 if successfully issued, none-zero otherwise.
5041 */
5042int
5043lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5044{
5045 LPFC_MBOXQ_t *mbox;
5046 int rc;
5047
James Smart6fb120a2009-05-22 14:52:59 -04005048 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5049 if (!mbox) {
5050 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005051 "2551 UNREG_FCFI mbox allocation failed"
5052 "HBA state x%x\n", phba->pport->port_state);
5053 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005054 }
James Smart6fb120a2009-05-22 14:52:59 -04005055 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5056 mbox->vport = phba->pport;
5057 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5058 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5059
5060 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005061 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5062 "2552 Unregister FCFI command failed rc x%x "
5063 "HBA state x%x\n",
5064 rc, phba->pport->port_state);
5065 return -EINVAL;
5066 }
5067 return 0;
5068}
5069
5070/**
5071 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5072 * @phba: Pointer to hba context object.
5073 *
5074 * This function unregisters the currently reigstered FCF. This function
5075 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5076 */
5077void
5078lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5079{
5080 int rc;
5081
5082 /* Preparation for unregistering fcf */
5083 rc = lpfc_unregister_fcf_prep(phba);
5084 if (rc) {
5085 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5086 "2748 Failed to prepare for unregistering "
5087 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005088 return;
5089 }
5090
James Smartecfd03c2010-02-12 14:41:27 -05005091 /* Now, unregister FCF record and reset HBA FCF state */
5092 rc = lpfc_sli4_unregister_fcf(phba);
5093 if (rc)
5094 return;
5095 /* Reset HBA FCF states after successful unregister FCF */
5096 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005097 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005098
5099 /*
5100 * If driver is not unloading, check if there is any other
5101 * FCF record that can be used for discovery.
5102 */
5103 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005104 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005105 return;
5106
James Smart0c9ab6f2010-02-26 14:15:57 -05005107 /* This is considered as the initial FCF discovery scan */
5108 spin_lock_irq(&phba->hbalock);
5109 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5110 spin_unlock_irq(&phba->hbalock);
5111 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005112
James Smart0c9ab6f2010-02-26 14:15:57 -05005113 if (rc) {
5114 spin_lock_irq(&phba->hbalock);
5115 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5116 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005117 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005118 "2553 lpfc_unregister_unused_fcf failed "
5119 "to read FCF record HBA state x%x\n",
5120 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005121 }
James Smartecfd03c2010-02-12 14:41:27 -05005122}
5123
5124/**
5125 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5126 * @phba: Pointer to hba context object.
5127 *
5128 * This function just unregisters the currently reigstered FCF. It does not
5129 * try to find another FCF for discovery.
5130 */
5131void
5132lpfc_unregister_fcf(struct lpfc_hba *phba)
5133{
5134 int rc;
5135
5136 /* Preparation for unregistering fcf */
5137 rc = lpfc_unregister_fcf_prep(phba);
5138 if (rc) {
5139 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5140 "2749 Failed to prepare for unregistering "
5141 "HBA's FCF record: rc=%d\n", rc);
5142 return;
5143 }
5144
5145 /* Now, unregister FCF record and reset HBA FCF state */
5146 rc = lpfc_sli4_unregister_fcf(phba);
5147 if (rc)
5148 return;
5149 /* Set proper HBA FCF states after successful unregister FCF */
5150 spin_lock_irq(&phba->hbalock);
5151 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
5152 spin_unlock_irq(&phba->hbalock);
5153}
5154
5155/**
5156 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
5157 * @phba: Pointer to hba context object.
5158 *
5159 * This function check if there are any connected remote port for the FCF and
5160 * if all the devices are disconnected, this function unregister FCFI.
5161 * This function also tries to use another FCF for discovery.
5162 */
5163void
5164lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
5165{
5166 /*
5167 * If HBA is not running in FIP mode or if HBA does not support
5168 * FCoE or if FCF is not registered, do nothing.
5169 */
5170 spin_lock_irq(&phba->hbalock);
5171 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
5172 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
5173 !(phba->hba_flag & HBA_FIP_SUPPORT)) {
5174 spin_unlock_irq(&phba->hbalock);
5175 return;
5176 }
5177 spin_unlock_irq(&phba->hbalock);
5178
5179 if (lpfc_fcf_inuse(phba))
5180 return;
5181
5182 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005183}
5184
5185/**
5186 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
5187 * @phba: Pointer to hba context object.
5188 * @buff: Buffer containing the FCF connection table as in the config
5189 * region.
5190 * This function create driver data structure for the FCF connection
5191 * record table read from config region 23.
5192 */
5193static void
5194lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
5195 uint8_t *buff)
5196{
5197 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5198 struct lpfc_fcf_conn_hdr *conn_hdr;
5199 struct lpfc_fcf_conn_rec *conn_rec;
5200 uint32_t record_count;
5201 int i;
5202
5203 /* Free the current connect table */
5204 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005205 &phba->fcf_conn_rec_list, list) {
5206 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04005207 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005208 }
James Smart6fb120a2009-05-22 14:52:59 -04005209
5210 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
5211 record_count = conn_hdr->length * sizeof(uint32_t)/
5212 sizeof(struct lpfc_fcf_conn_rec);
5213
5214 conn_rec = (struct lpfc_fcf_conn_rec *)
5215 (buff + sizeof(struct lpfc_fcf_conn_hdr));
5216
5217 for (i = 0; i < record_count; i++) {
5218 if (!(conn_rec[i].flags & FCFCNCT_VALID))
5219 continue;
5220 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
5221 GFP_KERNEL);
5222 if (!conn_entry) {
5223 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5224 "2566 Failed to allocate connection"
5225 " table entry\n");
5226 return;
5227 }
5228
5229 memcpy(&conn_entry->conn_rec, &conn_rec[i],
5230 sizeof(struct lpfc_fcf_conn_rec));
5231 conn_entry->conn_rec.vlan_tag =
5232 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
5233 conn_entry->conn_rec.flags =
5234 le16_to_cpu(conn_entry->conn_rec.flags);
5235 list_add_tail(&conn_entry->list,
5236 &phba->fcf_conn_rec_list);
5237 }
5238}
5239
5240/**
5241 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
5242 * @phba: Pointer to hba context object.
5243 * @buff: Buffer containing the FCoE parameter data structure.
5244 *
5245 * This function update driver data structure with config
5246 * parameters read from config region 23.
5247 */
5248static void
5249lpfc_read_fcoe_param(struct lpfc_hba *phba,
5250 uint8_t *buff)
5251{
5252 struct lpfc_fip_param_hdr *fcoe_param_hdr;
5253 struct lpfc_fcoe_params *fcoe_param;
5254
5255 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
5256 buff;
5257 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04005258 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04005259
5260 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
5261 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
5262 return;
5263
James Smart6fb120a2009-05-22 14:52:59 -04005264 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
5265 phba->valid_vlan = 1;
5266 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
5267 0xFFF;
5268 }
5269
5270 phba->fc_map[0] = fcoe_param->fc_map[0];
5271 phba->fc_map[1] = fcoe_param->fc_map[1];
5272 phba->fc_map[2] = fcoe_param->fc_map[2];
5273 return;
5274}
5275
5276/**
5277 * lpfc_get_rec_conf23 - Get a record type in config region data.
5278 * @buff: Buffer containing config region 23 data.
5279 * @size: Size of the data buffer.
5280 * @rec_type: Record type to be searched.
5281 *
5282 * This function searches config region data to find the begining
5283 * of the record specified by record_type. If record found, this
5284 * function return pointer to the record else return NULL.
5285 */
5286static uint8_t *
5287lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
5288{
5289 uint32_t offset = 0, rec_length;
5290
5291 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
5292 (size < sizeof(uint32_t)))
5293 return NULL;
5294
5295 rec_length = buff[offset + 1];
5296
5297 /*
5298 * One TLV record has one word header and number of data words
5299 * specified in the rec_length field of the record header.
5300 */
5301 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
5302 <= size) {
5303 if (buff[offset] == rec_type)
5304 return &buff[offset];
5305
5306 if (buff[offset] == LPFC_REGION23_LAST_REC)
5307 return NULL;
5308
5309 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
5310 rec_length = buff[offset + 1];
5311 }
5312 return NULL;
5313}
5314
5315/**
5316 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
5317 * @phba: Pointer to lpfc_hba data structure.
5318 * @buff: Buffer containing config region 23 data.
5319 * @size: Size of the data buffer.
5320 *
5321 * This fuction parse the FCoE config parameters in config region 23 and
5322 * populate driver data structure with the parameters.
5323 */
5324void
5325lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
5326 uint8_t *buff,
5327 uint32_t size)
5328{
5329 uint32_t offset = 0, rec_length;
5330 uint8_t *rec_ptr;
5331
5332 /*
5333 * If data size is less than 2 words signature and version cannot be
5334 * verified.
5335 */
5336 if (size < 2*sizeof(uint32_t))
5337 return;
5338
5339 /* Check the region signature first */
5340 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
5341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5342 "2567 Config region 23 has bad signature\n");
5343 return;
5344 }
5345
5346 offset += 4;
5347
5348 /* Check the data structure version */
5349 if (buff[offset] != LPFC_REGION23_VERSION) {
5350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5351 "2568 Config region 23 has bad version\n");
5352 return;
5353 }
5354 offset += 4;
5355
5356 rec_length = buff[offset + 1];
5357
5358 /* Read FCoE param record */
5359 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5360 size - offset, FCOE_PARAM_TYPE);
5361 if (rec_ptr)
5362 lpfc_read_fcoe_param(phba, rec_ptr);
5363
5364 /* Read FCF connection table */
5365 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5366 size - offset, FCOE_CONN_TBL_TYPE);
5367 if (rec_ptr)
5368 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5369
5370}