blob: d2b55f05aa026165e95d4ad60e7fdb05d521b70d [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 Smart4b40c592010-03-15 11:25:44 -04003019 /* when physical port receive logo donot start
3020 * vport discovery */
3021 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3022 lpfc_start_fdiscs(phba);
3023 else
3024 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smart92d7f7b2007-06-17 19:56:38 -05003025 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003026 }
3027
3028 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3029 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003030 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003031
3032 /* Drop the reference count from the mbox at the end after
3033 * all the current reference to the ndlp have been done.
3034 */
3035 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003036 return;
3037}
3038
3039/*
3040 * This routine handles processing a NameServer REG_LOGIN mailbox
3041 * command upon completion. It is setup in the LPFC_MBOXQ
3042 * as the completion routine when the command is
3043 * handed off to the SLI layer.
3044 */
3045void
James Smart2e0fef82007-06-17 19:56:36 -05003046lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003047{
James Smart21e9a0a2009-05-22 14:53:21 -04003048 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003049 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3050 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3051 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003052
3053 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003054out:
James Smart21e9a0a2009-05-22 14:53:21 -04003055 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3056 "0260 Register NameServer error: 0x%x\n",
3057 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003058 /* decrement the node reference count held for this
3059 * callback function.
3060 */
James Smart329f9bc2007-04-25 09:53:01 -04003061 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003062 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3063 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003064 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003065
3066 /* If no other thread is using the ndlp, free it */
3067 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003068
James Smart92d7f7b2007-06-17 19:56:38 -05003069 if (phba->fc_topology == TOPOLOGY_LOOP) {
3070 /*
3071 * RegLogin failed, use loop map to make discovery
3072 * list
3073 */
3074 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003075
James Smart92d7f7b2007-06-17 19:56:38 -05003076 /* Start discovery */
3077 lpfc_disc_start(vport);
3078 return;
3079 }
3080 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003081 return;
3082 }
3083
3084 pmb->context1 = NULL;
3085
dea31012005-04-17 16:05:31 -05003086 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003087 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003088 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003089 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003090
James Smart2e0fef82007-06-17 19:56:36 -05003091 if (vport->port_state < LPFC_VPORT_READY) {
3092 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003093 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3094 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3095 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3096 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3097 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3098
3099 /* Issue SCR just before NameServer GID_FT Query */
3100 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003101 }
3102
James Smart2e0fef82007-06-17 19:56:36 -05003103 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003104 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003105 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003106 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003107 goto out;
dea31012005-04-17 16:05:31 -05003108 }
3109
James Smartfa4066b2008-01-11 01:53:27 -05003110 /* decrement the node reference count held for this
3111 * callback function.
3112 */
James Smart329f9bc2007-04-25 09:53:01 -04003113 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003114 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3115 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003116 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003117
3118 return;
3119}
3120
3121static void
James Smart2e0fef82007-06-17 19:56:36 -05003122lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003123{
James Smart2e0fef82007-06-17 19:56:36 -05003124 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3125 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003126 struct lpfc_rport_data *rdata;
3127 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003128 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003129
3130 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003131 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3132 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003133 rport_ids.port_id = ndlp->nlp_DID;
3134 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003135
James Smart329f9bc2007-04-25 09:53:01 -04003136 /*
3137 * We leave our node pointer in rport->dd_data when we unregister a
3138 * FCP target port. But fc_remote_port_add zeros the space to which
3139 * rport->dd_data points. So, if we're reusing a previously
3140 * registered port, drop the reference that we took the last time we
3141 * registered the port.
3142 */
3143 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003144 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003145 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003146
3147 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3148 "rport add: did:x%x flg:x%x type x%x",
3149 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3150
James Smart2e0fef82007-06-17 19:56:36 -05003151 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003152 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003153 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3154 "Warning: fc_remote_port_add failed\n");
3155 return;
3156 }
3157
3158 /* initialize static port data */
3159 rport->maxframe_size = ndlp->nlp_maxframe;
3160 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003161 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003162 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003163
3164 if (ndlp->nlp_type & NLP_FCP_TARGET)
3165 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3166 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3167 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3168
3169
3170 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3171 fc_remote_port_rolechg(rport, rport_ids.roles);
3172
James Smart071fbd3d2006-04-15 11:53:20 -04003173 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003174 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003175 ndlp->nlp_sid = rport->scsi_target_id;
3176 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003177 return;
3178}
3179
3180static void
James Smart2e0fef82007-06-17 19:56:36 -05003181lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003182{
3183 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003184
James Smart858c9f62007-06-17 19:56:39 -05003185 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3186 "rport delete: did:x%x flg:x%x type x%x",
3187 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3188
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003189 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003190
3191 return;
3192}
3193
James Smartde0c5b32007-04-25 09:52:27 -04003194static void
James Smart2e0fef82007-06-17 19:56:36 -05003195lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003196{
James Smart2e0fef82007-06-17 19:56:36 -05003197 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3198
3199 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003200 switch (state) {
3201 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003202 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003203 break;
3204 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003205 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003206 break;
3207 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003208 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003209 break;
3210 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003211 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003212 break;
3213 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003214 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003215 break;
3216 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003217 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003218 break;
3219 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003220 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003221 break;
3222 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003223 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003224 break;
3225 }
James Smart2e0fef82007-06-17 19:56:36 -05003226 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003227}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003228
James Smartde0c5b32007-04-25 09:52:27 -04003229static void
James Smart2e0fef82007-06-17 19:56:36 -05003230lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003231 int old_state, int new_state)
3232{
James Smart2e0fef82007-06-17 19:56:36 -05003233 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3234
James Smartde0c5b32007-04-25 09:52:27 -04003235 if (new_state == NLP_STE_UNMAPPED_NODE) {
3236 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
3237 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3238 ndlp->nlp_type |= NLP_FC_NODE;
3239 }
3240 if (new_state == NLP_STE_MAPPED_NODE)
3241 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3242 if (new_state == NLP_STE_NPR_NODE)
3243 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003244
James Smartde0c5b32007-04-25 09:52:27 -04003245 /* Transport interface */
3246 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3247 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003248 vport->phba->nport_event_cnt++;
3249 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003250 }
dea31012005-04-17 16:05:31 -05003251
James Smartde0c5b32007-04-25 09:52:27 -04003252 if (new_state == NLP_STE_MAPPED_NODE ||
3253 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003254 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003255 /*
3256 * Tell the fc transport about the port, if we haven't
3257 * already. If we have, and it's a scsi entity, be
3258 * sure to unblock any attached scsi devices
3259 */
3260 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003261 }
James Smartea2151b2008-09-07 11:52:10 -04003262 if ((new_state == NLP_STE_MAPPED_NODE) &&
3263 (vport->stat_data_enabled)) {
3264 /*
3265 * A new target is discovered, if there is no buffer for
3266 * statistical data collection allocate buffer.
3267 */
3268 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3269 sizeof(struct lpfc_scsicmd_bkt),
3270 GFP_KERNEL);
3271
3272 if (!ndlp->lat_data)
3273 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3274 "0286 lpfc_nlp_state_cleanup failed to "
3275 "allocate statistical data buffer DID "
3276 "0x%x\n", ndlp->nlp_DID);
3277 }
James Smart858c9f62007-06-17 19:56:39 -05003278 /*
3279 * if we added to Mapped list, but the remote port
3280 * registration failed or assigned a target id outside
3281 * our presentable range - move the node to the
3282 * Unmapped List
3283 */
James Smartde0c5b32007-04-25 09:52:27 -04003284 if (new_state == NLP_STE_MAPPED_NODE &&
3285 (!ndlp->rport ||
3286 ndlp->rport->scsi_target_id == -1 ||
3287 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003288 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003289 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003290 spin_unlock_irq(shost->host_lock);
3291 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003292 }
James Smartde0c5b32007-04-25 09:52:27 -04003293}
3294
James Smart685f0bf2007-04-25 09:53:08 -04003295static char *
3296lpfc_nlp_state_name(char *buffer, size_t size, int state)
3297{
3298 static char *states[] = {
3299 [NLP_STE_UNUSED_NODE] = "UNUSED",
3300 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
3301 [NLP_STE_ADISC_ISSUE] = "ADISC",
3302 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
3303 [NLP_STE_PRLI_ISSUE] = "PRLI",
3304 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
3305 [NLP_STE_MAPPED_NODE] = "MAPPED",
3306 [NLP_STE_NPR_NODE] = "NPR",
3307 };
3308
James Smart311464e2007-08-02 11:10:37 -04003309 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04003310 strlcpy(buffer, states[state], size);
3311 else
3312 snprintf(buffer, size, "unknown (%d)", state);
3313 return buffer;
3314}
3315
James Smartde0c5b32007-04-25 09:52:27 -04003316void
James Smart2e0fef82007-06-17 19:56:36 -05003317lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3318 int state)
James Smartde0c5b32007-04-25 09:52:27 -04003319{
James Smart2e0fef82007-06-17 19:56:36 -05003320 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04003321 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04003322 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04003323
James Smarte8b62012007-08-02 11:10:09 -04003324 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3325 "0904 NPort state transition x%06x, %s -> %s\n",
3326 ndlp->nlp_DID,
3327 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
3328 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05003329
3330 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3331 "node statechg did:x%x old:%d ste:%d",
3332 ndlp->nlp_DID, old_state, state);
3333
James Smartde0c5b32007-04-25 09:52:27 -04003334 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04003335 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05003336 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003337 if (old_state == NLP_STE_UNMAPPED_NODE) {
3338 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
3339 ndlp->nlp_type &= ~NLP_FC_NODE;
3340 }
3341
James Smart685f0bf2007-04-25 09:53:08 -04003342 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05003343 spin_lock_irq(shost->host_lock);
3344 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3345 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003346 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05003347 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04003348
3349 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05003350 lpfc_nlp_counters(vport, state, 1);
3351 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04003352}
3353
3354void
James Smarte47c9092008-02-08 18:49:26 -05003355lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3356{
3357 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3358
3359 if (list_empty(&ndlp->nlp_listp)) {
3360 spin_lock_irq(shost->host_lock);
3361 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3362 spin_unlock_irq(shost->host_lock);
3363 }
3364}
3365
3366void
James Smart2e0fef82007-06-17 19:56:36 -05003367lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003368{
James Smart2e0fef82007-06-17 19:56:36 -05003369 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3370
James Smart0d2b6b82008-06-14 22:52:47 -04003371 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003372 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05003373 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3374 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003375 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05003376 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003377 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05003378 NLP_STE_UNUSED_NODE);
3379}
3380
Adrian Bunk4d9db012008-02-14 23:24:02 +02003381static void
James Smarte47c9092008-02-08 18:49:26 -05003382lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3383{
James Smart0d2b6b82008-06-14 22:52:47 -04003384 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003385 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3386 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3387 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3388 NLP_STE_UNUSED_NODE);
3389}
James Smart109f6ed2008-12-04 22:39:08 -05003390/**
James Smart3621a712009-04-06 18:47:14 -04003391 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05003392 * @vport: Pointer to Virtual Port object.
3393 * @ndlp: Pointer to FC node object.
3394 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04003395 *
3396 * This function is always called when node object need to be initialized.
3397 * It initializes all the fields of the node object. Although the reference
3398 * to phba from @ndlp can be obtained indirectly through it's reference to
3399 * @vport, a direct reference to phba is taken here by @ndlp. This is due
3400 * to the life-span of the @ndlp might go beyond the existence of @vport as
3401 * the final release of ndlp is determined by its reference count. And, the
3402 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05003403 **/
3404static inline void
3405lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3406 uint32_t did)
3407{
3408 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3409 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3410 init_timer(&ndlp->nlp_delayfunc);
3411 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3412 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3413 ndlp->nlp_DID = did;
3414 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04003415 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05003416 ndlp->nlp_sid = NLP_NO_SID;
3417 kref_init(&ndlp->kref);
3418 NLP_INT_NODE_ACT(ndlp);
3419 atomic_set(&ndlp->cmd_pending, 0);
3420 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
3421}
James Smarte47c9092008-02-08 18:49:26 -05003422
3423struct lpfc_nodelist *
3424lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3425 int state)
3426{
3427 struct lpfc_hba *phba = vport->phba;
3428 uint32_t did;
3429 unsigned long flags;
3430
3431 if (!ndlp)
3432 return NULL;
3433
3434 spin_lock_irqsave(&phba->ndlp_lock, flags);
3435 /* The ndlp should not be in memory free mode */
3436 if (NLP_CHK_FREE_REQ(ndlp)) {
3437 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3438 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3439 "0277 lpfc_enable_node: ndlp:x%p "
3440 "usgmap:x%x refcnt:%d\n",
3441 (void *)ndlp, ndlp->nlp_usg_map,
3442 atomic_read(&ndlp->kref.refcount));
3443 return NULL;
3444 }
3445 /* The ndlp should not already be in active mode */
3446 if (NLP_CHK_NODE_ACT(ndlp)) {
3447 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3448 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3449 "0278 lpfc_enable_node: ndlp:x%p "
3450 "usgmap:x%x refcnt:%d\n",
3451 (void *)ndlp, ndlp->nlp_usg_map,
3452 atomic_read(&ndlp->kref.refcount));
3453 return NULL;
3454 }
3455
3456 /* Keep the original DID */
3457 did = ndlp->nlp_DID;
3458
3459 /* re-initialize ndlp except of ndlp linked list pointer */
3460 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3461 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003462 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003463
3464 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3465
3466 if (state != NLP_STE_UNUSED_NODE)
3467 lpfc_nlp_set_state(vport, ndlp, state);
3468
3469 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3470 "node enable: did:x%x",
3471 ndlp->nlp_DID, 0, 0);
3472 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003473}
3474
3475void
James Smart2e0fef82007-06-17 19:56:36 -05003476lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003477{
James Smart87af33f2007-10-27 13:37:43 -04003478 /*
James Smartfa4066b2008-01-11 01:53:27 -05003479 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003480 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003481 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3482 * until ALL other outstanding threads have completed. We check
3483 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003484 */
James Smartfa4066b2008-01-11 01:53:27 -05003485 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3486 return;
James Smart51ef4c22007-08-02 11:10:31 -04003487 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003488 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003489 return;
dea31012005-04-17 16:05:31 -05003490}
3491
3492/*
3493 * Start / ReStart rescue timer for Discovery / RSCN handling
3494 */
3495void
James Smart2e0fef82007-06-17 19:56:36 -05003496lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003497{
James Smart2e0fef82007-06-17 19:56:36 -05003498 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3499 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003500 uint32_t tmo;
3501
James Smart2e0fef82007-06-17 19:56:36 -05003502 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003503 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003504 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3505 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003506 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003507 * FC spec states we need 3 * ratov for CT requests
3508 */
3509 tmo = ((phba->fc_ratov * 3) + 3);
3510 }
dea31012005-04-17 16:05:31 -05003511
James Smart858c9f62007-06-17 19:56:39 -05003512
3513 if (!timer_pending(&vport->fc_disctmo)) {
3514 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3515 "set disc timer: tmo:x%x state:x%x flg:x%x",
3516 tmo, vport->port_state, vport->fc_flag);
3517 }
3518
James Smart2e0fef82007-06-17 19:56:36 -05003519 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3520 spin_lock_irq(shost->host_lock);
3521 vport->fc_flag |= FC_DISC_TMO;
3522 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003523
3524 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003525 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3526 "0247 Start Discovery Timer state x%x "
3527 "Data: x%x x%lx x%x x%x\n",
3528 vport->port_state, tmo,
3529 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3530 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003531
3532 return;
3533}
3534
3535/*
3536 * Cancel rescue timer for Discovery / RSCN handling
3537 */
3538int
James Smart2e0fef82007-06-17 19:56:36 -05003539lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003540{
James Smart2e0fef82007-06-17 19:56:36 -05003541 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003542 unsigned long iflags;
3543
James Smart858c9f62007-06-17 19:56:39 -05003544 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3545 "can disc timer: state:x%x rtry:x%x flg:x%x",
3546 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3547
dea31012005-04-17 16:05:31 -05003548 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003549 if (vport->fc_flag & FC_DISC_TMO) {
3550 spin_lock_irqsave(shost->host_lock, iflags);
3551 vport->fc_flag &= ~FC_DISC_TMO;
3552 spin_unlock_irqrestore(shost->host_lock, iflags);
3553 del_timer_sync(&vport->fc_disctmo);
3554 spin_lock_irqsave(&vport->work_port_lock, iflags);
3555 vport->work_port_events &= ~WORKER_DISC_TMO;
3556 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003557 }
3558
3559 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003560 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3561 "0248 Cancel Discovery Timer state x%x "
3562 "Data: x%x x%x x%x\n",
3563 vport->port_state, vport->fc_flag,
3564 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003565 return 0;
dea31012005-04-17 16:05:31 -05003566}
3567
3568/*
3569 * Check specified ring for outstanding IOCB on the SLI queue
3570 * Return true if iocb matches the specified nport
3571 */
3572int
James Smart2e0fef82007-06-17 19:56:36 -05003573lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3574 struct lpfc_sli_ring *pring,
3575 struct lpfc_iocbq *iocb,
3576 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003577{
James Smart2e0fef82007-06-17 19:56:36 -05003578 struct lpfc_sli *psli = &phba->sli;
3579 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003580 struct lpfc_vport *vport = ndlp->vport;
3581
3582 if (iocb->vport != vport)
3583 return 0;
3584
dea31012005-04-17 16:05:31 -05003585 if (pring->ringno == LPFC_ELS_RING) {
3586 switch (icmd->ulpCommand) {
3587 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003588 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003589 return 1;
dea31012005-04-17 16:05:31 -05003590 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003591 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3592 return 1;
dea31012005-04-17 16:05:31 -05003593 case CMD_XMIT_ELS_RSP64_CX:
3594 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003595 return 1;
dea31012005-04-17 16:05:31 -05003596 }
James Smarta4bc3372006-12-02 13:34:16 -05003597 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003598
3599 } else if (pring->ringno == psli->fcp_ring) {
3600 /* Skip match check if waiting to relogin to FCP target */
3601 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003602 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003603 return 0;
dea31012005-04-17 16:05:31 -05003604 }
3605 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003606 return 1;
dea31012005-04-17 16:05:31 -05003607 }
3608 } else if (pring->ringno == psli->next_ring) {
3609
3610 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003611 return 0;
dea31012005-04-17 16:05:31 -05003612}
3613
3614/*
3615 * Free resources / clean up outstanding I/Os
3616 * associated with nlp_rpi in the LPFC_NODELIST entry.
3617 */
3618static int
James Smart2e0fef82007-06-17 19:56:36 -05003619lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003620{
James Smart2534ba72007-04-25 09:52:20 -04003621 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003622 struct lpfc_sli *psli;
3623 struct lpfc_sli_ring *pring;
3624 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003625 uint32_t i;
dea31012005-04-17 16:05:31 -05003626
James Smart92d7f7b2007-06-17 19:56:38 -05003627 lpfc_fabric_abort_nport(ndlp);
3628
dea31012005-04-17 16:05:31 -05003629 /*
3630 * Everything that matches on txcmplq will be returned
3631 * by firmware with a no rpi error.
3632 */
3633 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003634 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003635 /* Now process each ring */
3636 for (i = 0; i < psli->num_rings; i++) {
3637 pring = &psli->ring[i];
3638
James Smart2e0fef82007-06-17 19:56:36 -05003639 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003640 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003641 list) {
dea31012005-04-17 16:05:31 -05003642 /*
3643 * Check to see if iocb matches the nport we are
3644 * looking for
3645 */
James Smart92d7f7b2007-06-17 19:56:38 -05003646 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3647 ndlp))) {
dea31012005-04-17 16:05:31 -05003648 /* It matches, so deque and call compl
3649 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003650 list_move_tail(&iocb->list,
3651 &completions);
dea31012005-04-17 16:05:31 -05003652 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003653 }
3654 }
James Smart2e0fef82007-06-17 19:56:36 -05003655 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003656 }
3657 }
James Smart2534ba72007-04-25 09:52:20 -04003658
James Smarta257bf92009-04-06 18:48:10 -04003659 /* Cancel all the IOCBs from the completions list */
3660 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3661 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003662
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003663 return 0;
dea31012005-04-17 16:05:31 -05003664}
3665
3666/*
3667 * Free rpi associated with LPFC_NODELIST entry.
3668 * This routine is called from lpfc_freenode(), when we are removing
3669 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3670 * LOGO that completes successfully, and we are waiting to PLOGI back
3671 * to the remote NPort. In addition, it is called after we receive
3672 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3673 * we are waiting to PLOGI back to the remote NPort.
3674 */
3675int
James Smart2e0fef82007-06-17 19:56:36 -05003676lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003677{
James Smart2e0fef82007-06-17 19:56:36 -05003678 struct lpfc_hba *phba = vport->phba;
3679 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003680 int rc;
3681
James Smart21e9a0a2009-05-22 14:53:21 -04003682 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003683 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3684 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003685 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003686 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003687 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003688 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003689 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003690 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003691 }
dea31012005-04-17 16:05:31 -05003692 lpfc_no_rpi(phba, ndlp);
3693 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003694 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003695 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003696 return 1;
3697 }
3698 return 0;
3699}
3700
James Smartecfd03c2010-02-12 14:41:27 -05003701/**
3702 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
3703 * @phba: pointer to lpfc hba data structure.
3704 *
3705 * This routine is invoked to unregister all the currently registered RPIs
3706 * to the HBA.
3707 **/
3708void
3709lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
3710{
3711 struct lpfc_vport **vports;
3712 struct lpfc_nodelist *ndlp;
3713 struct Scsi_Host *shost;
3714 int i;
3715
3716 vports = lpfc_create_vport_work_array(phba);
3717 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3718 shost = lpfc_shost_from_vport(vports[i]);
3719 spin_lock_irq(shost->host_lock);
3720 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003721 if (ndlp->nlp_flag & NLP_RPI_VALID) {
3722 /* The mempool_alloc might sleep */
3723 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05003724 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05003725 spin_lock_irq(shost->host_lock);
3726 }
James Smartecfd03c2010-02-12 14:41:27 -05003727 }
3728 spin_unlock_irq(shost->host_lock);
3729 }
3730 lpfc_destroy_vport_work_array(phba, vports);
3731}
3732
James Smart92d7f7b2007-06-17 19:56:38 -05003733void
3734lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3735{
3736 struct lpfc_hba *phba = vport->phba;
3737 LPFC_MBOXQ_t *mbox;
3738 int rc;
3739
3740 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3741 if (mbox) {
3742 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3743 mbox->vport = vport;
3744 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003745 mbox->context1 = NULL;
3746 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003747 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003748 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003749
3750 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3751 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3752 "1836 Could not issue "
3753 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003754 }
3755}
3756
3757void
3758lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3759{
3760 struct lpfc_hba *phba = vport->phba;
3761 LPFC_MBOXQ_t *mbox;
3762 int rc;
3763
3764 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3765 if (mbox) {
3766 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3767 mbox->vport = vport;
3768 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003769 mbox->context1 = NULL;
3770 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003771 if (rc != MBX_TIMEOUT)
3772 mempool_free(mbox, phba->mbox_mem_pool);
3773
3774 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003775 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3776 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003777 "unreg_did (default rpis) status %d\n",
3778 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003779 }
3780}
3781
dea31012005-04-17 16:05:31 -05003782/*
3783 * Free resources associated with LPFC_NODELIST entry
3784 * so it can be freed.
3785 */
3786static int
James Smart2e0fef82007-06-17 19:56:36 -05003787lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003788{
James Smart2e0fef82007-06-17 19:56:36 -05003789 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3790 struct lpfc_hba *phba = vport->phba;
3791 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003792 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003793
3794 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003795 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3796 "0900 Cleanup node for NPort x%x "
3797 "Data: x%x x%x x%x\n",
3798 ndlp->nlp_DID, ndlp->nlp_flag,
3799 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003800 if (NLP_CHK_FREE_REQ(ndlp)) {
3801 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3802 "0280 lpfc_cleanup_node: ndlp:x%p "
3803 "usgmap:x%x refcnt:%d\n",
3804 (void *)ndlp, ndlp->nlp_usg_map,
3805 atomic_read(&ndlp->kref.refcount));
3806 lpfc_dequeue_node(vport, ndlp);
3807 } else {
3808 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3809 "0281 lpfc_cleanup_node: ndlp:x%p "
3810 "usgmap:x%x refcnt:%d\n",
3811 (void *)ndlp, ndlp->nlp_usg_map,
3812 atomic_read(&ndlp->kref.refcount));
3813 lpfc_disable_node(vport, ndlp);
3814 }
dea31012005-04-17 16:05:31 -05003815
dea31012005-04-17 16:05:31 -05003816 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3817 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003818 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003819 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3820 mb->context2 = NULL;
3821 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3822 }
3823 }
James Smart33ccf8d2006-08-17 11:57:58 -04003824
James Smart2e0fef82007-06-17 19:56:36 -05003825 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003826 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003827 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003828 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003829 mp = (struct lpfc_dmabuf *) (mb->context1);
3830 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003831 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003832 kfree(mp);
3833 }
3834 list_del(&mb->list);
3835 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003836 /* We shall not invoke the lpfc_nlp_put to decrement
3837 * the ndlp reference count as we are in the process
3838 * of lpfc_nlp_release.
3839 */
dea31012005-04-17 16:05:31 -05003840 }
3841 }
James Smart2e0fef82007-06-17 19:56:36 -05003842 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003843
James Smarte47c9092008-02-08 18:49:26 -05003844 lpfc_els_abort(phba, ndlp);
3845
James Smart2e0fef82007-06-17 19:56:36 -05003846 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003847 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003848 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003849
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003850 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003851 del_timer_sync(&ndlp->nlp_delayfunc);
3852
James Smart0d2b6b82008-06-14 22:52:47 -04003853 list_del_init(&ndlp->els_retry_evt.evt_listp);
3854 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003855
James Smart2e0fef82007-06-17 19:56:36 -05003856 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003857
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003858 return 0;
dea31012005-04-17 16:05:31 -05003859}
3860
3861/*
3862 * Check to see if we can free the nlp back to the freelist.
3863 * If we are in the middle of using the nlp in the discovery state
3864 * machine, defer the free till we reach the end of the state machine.
3865 */
James Smart329f9bc2007-04-25 09:53:01 -04003866static void
James Smart2e0fef82007-06-17 19:56:36 -05003867lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003868{
James Smarta8adb832007-10-27 13:37:53 -04003869 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003870 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003871 LPFC_MBOXQ_t *mbox;
3872 int rc;
dea31012005-04-17 16:05:31 -05003873
James Smart0d2b6b82008-06-14 22:52:47 -04003874 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003875 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3876 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003877 /* For this case we need to cleanup the default rpi
3878 * allocated by the firmware.
3879 */
3880 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3881 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003882 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003883 (uint8_t *) &vport->fc_sparam, mbox, 0);
3884 if (rc) {
3885 mempool_free(mbox, phba->mbox_mem_pool);
3886 }
3887 else {
3888 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3889 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3890 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003891 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003892 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3893 if (rc == MBX_NOT_FINISHED) {
3894 mempool_free(mbox, phba->mbox_mem_pool);
3895 }
3896 }
3897 }
3898 }
James Smart2e0fef82007-06-17 19:56:36 -05003899 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003900
James Smart2e0fef82007-06-17 19:56:36 -05003901 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003902 * We can get here with a non-NULL ndlp->rport because when we
3903 * unregister a rport we don't break the rport/node linkage. So if we
3904 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003905 */
James Smart92d7f7b2007-06-17 19:56:38 -05003906 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003907 rdata = ndlp->rport->dd_data;
3908 rdata->pnode = NULL;
3909 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003910 }
dea31012005-04-17 16:05:31 -05003911}
3912
3913static int
James Smart2e0fef82007-06-17 19:56:36 -05003914lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3915 uint32_t did)
dea31012005-04-17 16:05:31 -05003916{
James Smart2e0fef82007-06-17 19:56:36 -05003917 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003918
3919 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003920 return 0;
dea31012005-04-17 16:05:31 -05003921
dea31012005-04-17 16:05:31 -05003922 /* First check for Direct match */
3923 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003924 return 1;
dea31012005-04-17 16:05:31 -05003925
3926 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003927 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003928 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003929 return 0;
dea31012005-04-17 16:05:31 -05003930 }
3931
3932 matchdid.un.word = did;
3933 ndlpdid.un.word = ndlp->nlp_DID;
3934 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3935 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3936 (mydid.un.b.area == matchdid.un.b.area)) {
3937 if ((ndlpdid.un.b.domain == 0) &&
3938 (ndlpdid.un.b.area == 0)) {
3939 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003940 return 1;
dea31012005-04-17 16:05:31 -05003941 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003942 return 0;
dea31012005-04-17 16:05:31 -05003943 }
3944
3945 matchdid.un.word = ndlp->nlp_DID;
3946 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3947 (mydid.un.b.area == ndlpdid.un.b.area)) {
3948 if ((matchdid.un.b.domain == 0) &&
3949 (matchdid.un.b.area == 0)) {
3950 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003951 return 1;
dea31012005-04-17 16:05:31 -05003952 }
3953 }
3954 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003955 return 0;
dea31012005-04-17 16:05:31 -05003956}
3957
James Smart685f0bf2007-04-25 09:53:08 -04003958/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003959static struct lpfc_nodelist *
3960__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003961{
James Smart2fb9bd82006-12-02 13:33:57 -05003962 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003963 uint32_t data1;
3964
James Smart2e0fef82007-06-17 19:56:36 -05003965 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3966 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04003967 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3968 ((uint32_t) ndlp->nlp_xri << 16) |
3969 ((uint32_t) ndlp->nlp_type << 8) |
3970 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04003971 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3972 "0929 FIND node DID "
3973 "Data: x%p x%x x%x x%x\n",
3974 ndlp, ndlp->nlp_DID,
3975 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04003976 return ndlp;
dea31012005-04-17 16:05:31 -05003977 }
3978 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003979
dea31012005-04-17 16:05:31 -05003980 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04003981 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3982 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05003983 return NULL;
3984}
3985
3986struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003987lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003988{
James Smart2e0fef82007-06-17 19:56:36 -05003989 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003990 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003991
James Smart2e0fef82007-06-17 19:56:36 -05003992 spin_lock_irq(shost->host_lock);
3993 ndlp = __lpfc_findnode_did(vport, did);
3994 spin_unlock_irq(shost->host_lock);
3995 return ndlp;
3996}
3997
3998struct lpfc_nodelist *
3999lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4000{
4001 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4002 struct lpfc_nodelist *ndlp;
4003
4004 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004005 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004006 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4007 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004008 return NULL;
4009 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004010 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004011 if (!ndlp)
4012 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004013 lpfc_nlp_init(vport, ndlp, did);
4014 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4015 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004016 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004017 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004018 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004019 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4020 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4021 if (!ndlp)
4022 return NULL;
4023 spin_lock_irq(shost->host_lock);
4024 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4025 spin_unlock_irq(shost->host_lock);
4026 return ndlp;
dea31012005-04-17 16:05:31 -05004027 }
James Smarte47c9092008-02-08 18:49:26 -05004028
James Smart58da1ff2008-04-07 10:15:56 -04004029 if ((vport->fc_flag & FC_RSCN_MODE) &&
4030 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004031 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04004032 /* If we've already recieved a PLOGI from this NPort
4033 * we don't need to try to discover it again.
4034 */
4035 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4036 return NULL;
4037
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004038 /* Since this node is marked for discovery,
4039 * delay timeout is not needed.
4040 */
James Smart0d2b6b82008-06-14 22:52:47 -04004041 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004042 spin_lock_irq(shost->host_lock);
4043 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4044 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004045 } else
dea31012005-04-17 16:05:31 -05004046 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004047 } else {
James Smart87af33f2007-10-27 13:37:43 -04004048 /* If we've already recieved a PLOGI from this NPort,
4049 * or we are already in the process of discovery on it,
4050 * we don't need to try to discover it again.
4051 */
James Smart685f0bf2007-04-25 09:53:08 -04004052 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004053 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4054 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004055 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004056 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4057 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004058 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004059 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004060 }
4061 return ndlp;
4062}
4063
4064/* Build a list of nodes to discover based on the loopmap */
4065void
James Smart2e0fef82007-06-17 19:56:36 -05004066lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004067{
James Smart2e0fef82007-06-17 19:56:36 -05004068 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004069 int j;
4070 uint32_t alpa, index;
4071
James Smart2e0fef82007-06-17 19:56:36 -05004072 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004073 return;
James Smart2e0fef82007-06-17 19:56:36 -05004074
4075 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004076 return;
dea31012005-04-17 16:05:31 -05004077
4078 /* Check for loop map present or not */
4079 if (phba->alpa_map[0]) {
4080 for (j = 1; j <= phba->alpa_map[0]; j++) {
4081 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004082 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004083 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004084 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004085 }
4086 } else {
4087 /* No alpamap, so try all alpa's */
4088 for (j = 0; j < FC_MAXLOOP; j++) {
4089 /* If cfg_scan_down is set, start from highest
4090 * ALPA (0xef) to lowest (0x1).
4091 */
James Smart3de2a652007-08-02 11:09:59 -04004092 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004093 index = j;
4094 else
4095 index = FC_MAXLOOP - j - 1;
4096 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004097 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004098 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004099 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004100 }
4101 }
4102 return;
4103}
4104
dea31012005-04-17 16:05:31 -05004105void
James Smart2e0fef82007-06-17 19:56:36 -05004106lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004107{
dea31012005-04-17 16:05:31 -05004108 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004109 struct lpfc_sli *psli = &phba->sli;
4110 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4111 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4112 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4113 int rc;
4114
James Smart92d7f7b2007-06-17 19:56:38 -05004115 /*
4116 * if it's not a physical port or if we already send
4117 * clear_la then don't send it.
4118 */
4119 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004120 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4121 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004122 return;
4123
James Smart2e0fef82007-06-17 19:56:36 -05004124 /* Link up discovery */
4125 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4126 phba->link_state = LPFC_CLEAR_LA;
4127 lpfc_clear_la(phba, mbox);
4128 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4129 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004130 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004131 if (rc == MBX_NOT_FINISHED) {
4132 mempool_free(mbox, phba->mbox_mem_pool);
4133 lpfc_disc_flush_list(vport);
4134 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4135 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4136 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004137 phba->link_state = LPFC_HBA_ERROR;
4138 }
4139 }
4140}
4141
4142/* Reg_vpi to tell firmware to resume normal operations */
4143void
4144lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4145{
4146 LPFC_MBOXQ_t *regvpimbox;
4147
4148 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4149 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004150 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004151 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4152 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004153 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004154 == MBX_NOT_FINISHED) {
4155 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004156 }
4157 }
4158}
4159
4160/* Start Link up / RSCN discovery on NPR nodes */
4161void
4162lpfc_disc_start(struct lpfc_vport *vport)
4163{
4164 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4165 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004166 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004167 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004168 int did_changed;
dea31012005-04-17 16:05:31 -05004169
James Smart2e0fef82007-06-17 19:56:36 -05004170 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004171 return;
James Smart2e0fef82007-06-17 19:56:36 -05004172
4173 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004174 clear_la_pending = 1;
4175 else
4176 clear_la_pending = 0;
4177
James Smart2e0fef82007-06-17 19:56:36 -05004178 if (vport->port_state < LPFC_VPORT_READY)
4179 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004180
James Smart2e0fef82007-06-17 19:56:36 -05004181 lpfc_set_disctmo(vport);
4182
4183 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004184 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004185 else
dea31012005-04-17 16:05:31 -05004186 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004187
4188 vport->fc_prevDID = vport->fc_myDID;
4189 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004190
4191 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004192 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4193 "0202 Start Discovery hba state x%x "
4194 "Data: x%x x%x x%x\n",
4195 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4196 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004197
4198 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004199 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004200
4201 if (num_sent)
4202 return;
4203
James Smart92d7f7b2007-06-17 19:56:38 -05004204 /*
4205 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
4206 * continue discovery.
4207 */
4208 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004209 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004210 !(vport->fc_flag & FC_RSCN_MODE) &&
4211 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004212 lpfc_issue_reg_vpi(phba, vport);
4213 return;
4214 }
James Smart2e0fef82007-06-17 19:56:36 -05004215
James Smart92d7f7b2007-06-17 19:56:38 -05004216 /*
4217 * For SLI2, we need to set port_state to READY and continue
4218 * discovery.
4219 */
4220 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4221 /* If we get here, there is nothing to ADISC */
4222 if (vport->port_type == LPFC_PHYSICAL_PORT)
4223 lpfc_issue_clear_la(phba, vport);
4224
4225 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004226 vport->num_disc_nodes = 0;
4227 /* go thru NPR nodes and issue ELS PLOGIs */
4228 if (vport->fc_npr_cnt)
4229 lpfc_els_disc_plogi(vport);
4230
4231 if (!vport->num_disc_nodes) {
4232 spin_lock_irq(shost->host_lock);
4233 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4234 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004235 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05004236 }
4237 }
James Smart92d7f7b2007-06-17 19:56:38 -05004238 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004239 } else {
4240 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004241 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05004242
4243 if (num_sent)
4244 return;
4245
James Smart2e0fef82007-06-17 19:56:36 -05004246 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05004247 /* Check to see if more RSCNs came in while we
4248 * were processing this one.
4249 */
James Smart2e0fef82007-06-17 19:56:36 -05004250 if ((vport->fc_rscn_id_cnt == 0) &&
4251 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
4252 spin_lock_irq(shost->host_lock);
4253 vport->fc_flag &= ~FC_RSCN_MODE;
4254 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004255 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004256 } else
James Smart2e0fef82007-06-17 19:56:36 -05004257 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05004258 }
4259 }
4260 return;
4261}
4262
4263/*
4264 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
4265 * ring the match the sppecified nodelist.
4266 */
4267static void
James Smart2e0fef82007-06-17 19:56:36 -05004268lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004269{
James Smart2534ba72007-04-25 09:52:20 -04004270 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004271 struct lpfc_sli *psli;
4272 IOCB_t *icmd;
4273 struct lpfc_iocbq *iocb, *next_iocb;
4274 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004275
4276 psli = &phba->sli;
4277 pring = &psli->ring[LPFC_ELS_RING];
4278
4279 /* Error matching iocb on txq or txcmplq
4280 * First check the txq.
4281 */
James Smart2e0fef82007-06-17 19:56:36 -05004282 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004283 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4284 if (iocb->context1 != ndlp) {
4285 continue;
4286 }
4287 icmd = &iocb->iocb;
4288 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
4289 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
4290
James Smart2534ba72007-04-25 09:52:20 -04004291 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05004292 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05004293 }
4294 }
4295
4296 /* Next check the txcmplq */
4297 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
4298 if (iocb->context1 != ndlp) {
4299 continue;
4300 }
4301 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004302 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
4303 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04004304 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05004305 }
4306 }
James Smart2e0fef82007-06-17 19:56:36 -05004307 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04004308
James Smarta257bf92009-04-06 18:48:10 -04004309 /* Cancel all the IOCBs from the completions list */
4310 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4311 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004312}
4313
Adrian Bunka6ababd2007-11-05 18:07:33 +01004314static void
James Smart2e0fef82007-06-17 19:56:36 -05004315lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004316{
4317 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05004318 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004319
James Smart2e0fef82007-06-17 19:56:36 -05004320 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
4321 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004322 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004323 if (!NLP_CHK_NODE_ACT(ndlp))
4324 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004325 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4326 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
4327 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04004328 }
dea31012005-04-17 16:05:31 -05004329 }
4330 }
dea31012005-04-17 16:05:31 -05004331}
4332
James Smart92d7f7b2007-06-17 19:56:38 -05004333void
4334lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
4335{
4336 lpfc_els_flush_rscn(vport);
4337 lpfc_els_flush_cmd(vport);
4338 lpfc_disc_flush_list(vport);
4339}
4340
dea31012005-04-17 16:05:31 -05004341/*****************************************************************************/
4342/*
4343 * NAME: lpfc_disc_timeout
4344 *
4345 * FUNCTION: Fibre Channel driver discovery timeout routine.
4346 *
4347 * EXECUTION ENVIRONMENT: interrupt only
4348 *
4349 * CALLED FROM:
4350 * Timer function
4351 *
4352 * RETURNS:
4353 * none
4354 */
4355/*****************************************************************************/
4356void
4357lpfc_disc_timeout(unsigned long ptr)
4358{
James Smart2e0fef82007-06-17 19:56:36 -05004359 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4360 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04004361 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004362 unsigned long flags = 0;
4363
4364 if (unlikely(!phba))
4365 return;
4366
James Smart5e9d9b82008-06-14 22:52:53 -04004367 spin_lock_irqsave(&vport->work_port_lock, flags);
4368 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4369 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05004370 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04004371 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05004372
James Smart5e9d9b82008-06-14 22:52:53 -04004373 if (!tmo_posted)
4374 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004375 return;
4376}
4377
4378static void
James Smart2e0fef82007-06-17 19:56:36 -05004379lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004380{
James Smart2e0fef82007-06-17 19:56:36 -05004381 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4382 struct lpfc_hba *phba = vport->phba;
4383 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004384 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05004385 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05004386 int rc, clrlaerr = 0;
4387
James Smart2e0fef82007-06-17 19:56:36 -05004388 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05004389 return;
4390
James Smart2e0fef82007-06-17 19:56:36 -05004391 spin_lock_irq(shost->host_lock);
4392 vport->fc_flag &= ~FC_DISC_TMO;
4393 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004394
James Smart858c9f62007-06-17 19:56:39 -05004395 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4396 "disc timeout: state:x%x rtry:x%x flg:x%x",
4397 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4398
James Smart2e0fef82007-06-17 19:56:36 -05004399 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05004400
4401 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05004402 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
4403 * FAN
4404 */
4405 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04004406 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4407 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004408 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05004409 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004410 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004411 if (!NLP_CHK_NODE_ACT(ndlp))
4412 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004413 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4414 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004415 if (ndlp->nlp_type & NLP_FABRIC) {
4416 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05004417 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004418
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004419 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004420 /* Fail outstanding IO now since device
4421 * is marked for PLOGI.
4422 */
James Smart2e0fef82007-06-17 19:56:36 -05004423 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004424 }
4425 }
James Smart92d7f7b2007-06-17 19:56:38 -05004426 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05004427 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05004428 return;
James Smart92d7f7b2007-06-17 19:56:38 -05004429 }
dea31012005-04-17 16:05:31 -05004430 break;
4431
James Smart92d7f7b2007-06-17 19:56:38 -05004432 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05004433 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05004434 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05004435 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04004436 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4437 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04004438 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05004439
4440 /* Assume no Fabric and go on with discovery.
4441 * Check for outstanding ELS FLOGI to abort.
4442 */
4443
4444 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05004445 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004446
4447 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05004448 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05004449 break;
4450
4451 case LPFC_FABRIC_CFG_LINK:
4452 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4453 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04004454 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4455 "0223 Timeout while waiting for "
4456 "NameServer login\n");
dea31012005-04-17 16:05:31 -05004457 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05004458 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05004459 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04004460 lpfc_els_abort(phba, ndlp);
4461
4462 /* ReStart discovery */
4463 goto restart_disc;
dea31012005-04-17 16:05:31 -05004464
4465 case LPFC_NS_QRY:
4466 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04004467 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4468 "0224 NameServer Query timeout "
4469 "Data: x%x x%x\n",
4470 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004471
James Smart92d7f7b2007-06-17 19:56:38 -05004472 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4473 /* Try it one more time */
4474 vport->fc_ns_retry++;
4475 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4476 vport->fc_ns_retry, 0);
4477 if (rc == 0)
4478 break;
dea31012005-04-17 16:05:31 -05004479 }
James Smart92d7f7b2007-06-17 19:56:38 -05004480 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05004481
James Smart87af33f2007-10-27 13:37:43 -04004482restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05004483 /*
4484 * Discovery is over.
4485 * set port_state to PORT_READY if SLI2.
4486 * cmpl_reg_vpi will set port_state to READY for SLI3.
4487 */
James Smart3772a992009-05-22 14:50:54 -04004488 if (phba->sli_rev < LPFC_SLI_REV4) {
4489 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4490 lpfc_issue_reg_vpi(phba, vport);
4491 else { /* NPIV Not enabled */
4492 lpfc_issue_clear_la(phba, vport);
4493 vport->port_state = LPFC_VPORT_READY;
4494 }
dea31012005-04-17 16:05:31 -05004495 }
4496
4497 /* Setup and issue mailbox INITIALIZE LINK command */
4498 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4499 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004500 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4501 "0206 Device Discovery "
4502 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004503 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004504 break;
4505 }
4506
4507 lpfc_linkdown(phba);
4508 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4509 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004510 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004511 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004512 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004513 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004514 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004515 if (rc == MBX_NOT_FINISHED)
4516 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4517
4518 break;
4519
4520 case LPFC_DISC_AUTH:
4521 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004522 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4523 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004524 lpfc_disc_flush_list(vport);
4525
James Smart92d7f7b2007-06-17 19:56:38 -05004526 /*
4527 * set port_state to PORT_READY if SLI2.
4528 * cmpl_reg_vpi will set port_state to READY for SLI3.
4529 */
James Smart3772a992009-05-22 14:50:54 -04004530 if (phba->sli_rev < LPFC_SLI_REV4) {
4531 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4532 lpfc_issue_reg_vpi(phba, vport);
4533 else { /* NPIV Not enabled */
4534 lpfc_issue_clear_la(phba, vport);
4535 vport->port_state = LPFC_VPORT_READY;
4536 }
dea31012005-04-17 16:05:31 -05004537 }
4538 break;
4539
James Smart2e0fef82007-06-17 19:56:36 -05004540 case LPFC_VPORT_READY:
4541 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004542 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4543 "0231 RSCN timeout Data: x%x "
4544 "x%x\n",
4545 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004546
4547 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004548 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004549
James Smart2e0fef82007-06-17 19:56:36 -05004550 lpfc_els_flush_rscn(vport);
4551 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004552 }
4553 break;
James Smart2e0fef82007-06-17 19:56:36 -05004554
James Smart92d7f7b2007-06-17 19:56:38 -05004555 default:
James Smarte8b62012007-08-02 11:10:09 -04004556 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004557 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004558 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004559 break;
4560 }
4561
4562 switch (phba->link_state) {
4563 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004564 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004565 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4566 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004567 clrlaerr = 1;
4568 break;
4569
James Smart09372822008-01-11 01:52:54 -05004570 case LPFC_LINK_UP:
4571 lpfc_issue_clear_la(phba, vport);
4572 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004573 case LPFC_LINK_UNKNOWN:
4574 case LPFC_WARM_START:
4575 case LPFC_INIT_START:
4576 case LPFC_INIT_MBX_CMDS:
4577 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004578 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004579 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4580 "0230 Unexpected timeout, hba link "
4581 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004582 clrlaerr = 1;
4583 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004584
4585 case LPFC_HBA_READY:
4586 break;
dea31012005-04-17 16:05:31 -05004587 }
4588
4589 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004590 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004591 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004592 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4593 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004594 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004595 }
4596
4597 return;
4598}
4599
dea31012005-04-17 16:05:31 -05004600/*
4601 * This routine handles processing a NameServer REG_LOGIN mailbox
4602 * command upon completion. It is setup in the LPFC_MBOXQ
4603 * as the completion routine when the command is
4604 * handed off to the SLI layer.
4605 */
4606void
James Smart2e0fef82007-06-17 19:56:36 -05004607lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004608{
James Smart04c68492009-05-22 14:52:52 -04004609 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004610 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4611 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4612 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004613
4614 pmb->context1 = NULL;
4615
dea31012005-04-17 16:05:31 -05004616 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004617 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004618 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004619 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004620
James Smart2e0fef82007-06-17 19:56:36 -05004621 /*
4622 * Start issuing Fabric-Device Management Interface (FDMI) command to
4623 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4624 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004625 */
James Smart2e0fef82007-06-17 19:56:36 -05004626
James Smart3de2a652007-08-02 11:09:59 -04004627 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004628 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4629 else
4630 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004631
James Smartfa4066b2008-01-11 01:53:27 -05004632 /* decrement the node reference count held for this callback
4633 * function.
4634 */
James Smart329f9bc2007-04-25 09:53:01 -04004635 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004636 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4637 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004638 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004639
4640 return;
4641}
4642
James Smart685f0bf2007-04-25 09:53:08 -04004643static int
4644lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4645{
4646 uint16_t *rpi = param;
4647
4648 return ndlp->nlp_rpi == *rpi;
4649}
4650
4651static int
4652lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4653{
4654 return memcmp(&ndlp->nlp_portname, param,
4655 sizeof(ndlp->nlp_portname)) == 0;
4656}
4657
Adrian Bunka6ababd2007-11-05 18:07:33 +01004658static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004659__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004660{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004661 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004662
James Smart2e0fef82007-06-17 19:56:36 -05004663 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004664 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004665 return ndlp;
4666 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004667 return NULL;
dea31012005-04-17 16:05:31 -05004668}
4669
James Smart685f0bf2007-04-25 09:53:08 -04004670/*
4671 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004672 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004673 */
4674struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004675__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004676{
James Smart2e0fef82007-06-17 19:56:36 -05004677 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004678}
4679
James Smart488d1462006-03-07 15:02:37 -05004680/*
James Smart685f0bf2007-04-25 09:53:08 -04004681 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004682 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004683 */
4684struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004685lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004686{
James Smart2e0fef82007-06-17 19:56:36 -05004687 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004688 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004689
James Smart2e0fef82007-06-17 19:56:36 -05004690 spin_lock_irq(shost->host_lock);
4691 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4692 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004693 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004694}
4695
dea31012005-04-17 16:05:31 -05004696void
James Smart2e0fef82007-06-17 19:56:36 -05004697lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4698 uint32_t did)
dea31012005-04-17 16:05:31 -05004699{
4700 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004701
4702 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004703 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004704
4705 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4706 "node init: did:x%x",
4707 ndlp->nlp_DID, 0, 0);
4708
dea31012005-04-17 16:05:31 -05004709 return;
4710}
James Smart329f9bc2007-04-25 09:53:01 -04004711
James Smart98c9ea52007-10-27 13:37:33 -04004712/* This routine releases all resources associated with a specifc NPort's ndlp
4713 * and mempool_free's the nodelist.
4714 */
James Smart311464e2007-08-02 11:10:37 -04004715static void
James Smart329f9bc2007-04-25 09:53:01 -04004716lpfc_nlp_release(struct kref *kref)
4717{
James Smarte47c9092008-02-08 18:49:26 -05004718 struct lpfc_hba *phba;
4719 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004720 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4721 kref);
James Smart858c9f62007-06-17 19:56:39 -05004722
4723 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4724 "node release: did:x%x flg:x%x type:x%x",
4725 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4726
James Smarte47c9092008-02-08 18:49:26 -05004727 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4728 "0279 lpfc_nlp_release: ndlp:x%p "
4729 "usgmap:x%x refcnt:%d\n",
4730 (void *)ndlp, ndlp->nlp_usg_map,
4731 atomic_read(&ndlp->kref.refcount));
4732
4733 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004734 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004735
4736 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004737 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004738 spin_lock_irqsave(&phba->ndlp_lock, flags);
4739 NLP_CLR_NODE_ACT(ndlp);
4740 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4741
4742 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004743 if (NLP_CHK_FREE_REQ(ndlp)) {
4744 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004745 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004746 }
James Smart329f9bc2007-04-25 09:53:01 -04004747}
4748
James Smart98c9ea52007-10-27 13:37:33 -04004749/* This routine bumps the reference count for a ndlp structure to ensure
4750 * that one discovery thread won't free a ndlp while another discovery thread
4751 * is using it.
4752 */
James Smart329f9bc2007-04-25 09:53:01 -04004753struct lpfc_nodelist *
4754lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4755{
James Smarte47c9092008-02-08 18:49:26 -05004756 struct lpfc_hba *phba;
4757 unsigned long flags;
4758
James Smart98c9ea52007-10-27 13:37:33 -04004759 if (ndlp) {
4760 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4761 "node get: did:x%x flg:x%x refcnt:x%x",
4762 ndlp->nlp_DID, ndlp->nlp_flag,
4763 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004764 /* The check of ndlp usage to prevent incrementing the
4765 * ndlp reference count that is in the process of being
4766 * released.
4767 */
James Smarta257bf92009-04-06 18:48:10 -04004768 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004769 spin_lock_irqsave(&phba->ndlp_lock, flags);
4770 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4771 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4772 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4773 "0276 lpfc_nlp_get: ndlp:x%p "
4774 "usgmap:x%x refcnt:%d\n",
4775 (void *)ndlp, ndlp->nlp_usg_map,
4776 atomic_read(&ndlp->kref.refcount));
4777 return NULL;
4778 } else
4779 kref_get(&ndlp->kref);
4780 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004781 }
James Smart329f9bc2007-04-25 09:53:01 -04004782 return ndlp;
4783}
4784
James Smart98c9ea52007-10-27 13:37:33 -04004785/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004786 * count goes to 0, this indicates the the associated nodelist should be
4787 * freed. Returning 1 indicates the ndlp resource has been released; on the
4788 * other hand, returning 0 indicates the ndlp resource has not been released
4789 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004790 */
James Smart329f9bc2007-04-25 09:53:01 -04004791int
4792lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4793{
James Smarte47c9092008-02-08 18:49:26 -05004794 struct lpfc_hba *phba;
4795 unsigned long flags;
4796
4797 if (!ndlp)
4798 return 1;
4799
4800 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4801 "node put: did:x%x flg:x%x refcnt:x%x",
4802 ndlp->nlp_DID, ndlp->nlp_flag,
4803 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004804 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004805 spin_lock_irqsave(&phba->ndlp_lock, flags);
4806 /* Check the ndlp memory free acknowledge flag to avoid the
4807 * possible race condition that kref_put got invoked again
4808 * after previous one has done ndlp memory free.
4809 */
4810 if (NLP_CHK_FREE_ACK(ndlp)) {
4811 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4812 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4813 "0274 lpfc_nlp_put: ndlp:x%p "
4814 "usgmap:x%x refcnt:%d\n",
4815 (void *)ndlp, ndlp->nlp_usg_map,
4816 atomic_read(&ndlp->kref.refcount));
4817 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004818 }
James Smarte47c9092008-02-08 18:49:26 -05004819 /* Check the ndlp inactivate log flag to avoid the possible
4820 * race condition that kref_put got invoked again after ndlp
4821 * is already in inactivating state.
4822 */
4823 if (NLP_CHK_IACT_REQ(ndlp)) {
4824 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4825 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4826 "0275 lpfc_nlp_put: ndlp:x%p "
4827 "usgmap:x%x refcnt:%d\n",
4828 (void *)ndlp, ndlp->nlp_usg_map,
4829 atomic_read(&ndlp->kref.refcount));
4830 return 1;
4831 }
4832 /* For last put, mark the ndlp usage flags to make sure no
4833 * other kref_get and kref_put on the same ndlp shall get
4834 * in between the process when the final kref_put has been
4835 * invoked on this ndlp.
4836 */
4837 if (atomic_read(&ndlp->kref.refcount) == 1) {
4838 /* Indicate ndlp is put to inactive state. */
4839 NLP_SET_IACT_REQ(ndlp);
4840 /* Acknowledge ndlp memory free has been seen. */
4841 if (NLP_CHK_FREE_REQ(ndlp))
4842 NLP_SET_FREE_ACK(ndlp);
4843 }
4844 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4845 /* Note, the kref_put returns 1 when decrementing a reference
4846 * count that was 1, it invokes the release callback function,
4847 * but it still left the reference count as 1 (not actually
4848 * performs the last decrementation). Otherwise, it actually
4849 * decrements the reference count and returns 0.
4850 */
4851 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004852}
James Smart98c9ea52007-10-27 13:37:33 -04004853
4854/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004855 * by any other discovery thread. This routine returns 1 if the
4856 * ndlp has been freed. A return value of 0 indicates the ndlp is
4857 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004858 */
4859int
4860lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4861{
4862 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4863 "node not used: did:x%x flg:x%x refcnt:x%x",
4864 ndlp->nlp_DID, ndlp->nlp_flag,
4865 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004866 if (atomic_read(&ndlp->kref.refcount) == 1)
4867 if (lpfc_nlp_put(ndlp))
4868 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004869 return 0;
4870}
James Smart6fb120a2009-05-22 14:52:59 -04004871
4872/**
4873 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4874 * @phba: Pointer to hba context object.
4875 *
4876 * This function iterate through all FC nodes associated
4877 * will all vports to check if there is any node with
4878 * fc_rports associated with it. If there is an fc_rport
4879 * associated with the node, then the node is either in
4880 * discovered state or its devloss_timer is pending.
4881 */
4882static int
4883lpfc_fcf_inuse(struct lpfc_hba *phba)
4884{
4885 struct lpfc_vport **vports;
4886 int i, ret = 0;
4887 struct lpfc_nodelist *ndlp;
4888 struct Scsi_Host *shost;
4889
4890 vports = lpfc_create_vport_work_array(phba);
4891
4892 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4893 shost = lpfc_shost_from_vport(vports[i]);
4894 spin_lock_irq(shost->host_lock);
4895 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4896 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4897 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4898 ret = 1;
4899 spin_unlock_irq(shost->host_lock);
4900 goto out;
James Smart891478a2009-11-18 15:40:23 -05004901 } else {
4902 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4903 "2624 RPI %x DID %x flg %x still "
4904 "logged in\n",
4905 ndlp->nlp_rpi, ndlp->nlp_DID,
4906 ndlp->nlp_flag);
4907 if (ndlp->nlp_flag & NLP_RPI_VALID)
4908 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04004909 }
4910 }
4911 spin_unlock_irq(shost->host_lock);
4912 }
4913out:
4914 lpfc_destroy_vport_work_array(phba, vports);
4915 return ret;
4916}
4917
4918/**
4919 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4920 * @phba: Pointer to hba context object.
4921 * @mboxq: Pointer to mailbox object.
4922 *
4923 * This function frees memory associated with the mailbox command.
4924 */
4925static void
4926lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4927{
4928 struct lpfc_vport *vport = mboxq->vport;
4929
4930 if (mboxq->u.mb.mbxStatus) {
4931 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4932 "2555 UNREG_VFI mbxStatus error x%x "
4933 "HBA state x%x\n",
4934 mboxq->u.mb.mbxStatus, vport->port_state);
4935 }
4936 mempool_free(mboxq, phba->mbox_mem_pool);
4937 return;
4938}
4939
4940/**
4941 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4942 * @phba: Pointer to hba context object.
4943 * @mboxq: Pointer to mailbox object.
4944 *
4945 * This function frees memory associated with the mailbox command.
4946 */
4947static void
4948lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4949{
4950 struct lpfc_vport *vport = mboxq->vport;
4951
4952 if (mboxq->u.mb.mbxStatus) {
4953 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4954 "2550 UNREG_FCFI mbxStatus error x%x "
4955 "HBA state x%x\n",
4956 mboxq->u.mb.mbxStatus, vport->port_state);
4957 }
4958 mempool_free(mboxq, phba->mbox_mem_pool);
4959 return;
4960}
4961
4962/**
James Smartecfd03c2010-02-12 14:41:27 -05004963 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04004964 * @phba: Pointer to hba context object.
4965 *
James Smartecfd03c2010-02-12 14:41:27 -05004966 * This function prepare the HBA for unregistering the currently registered
4967 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
4968 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04004969 */
James Smartecfd03c2010-02-12 14:41:27 -05004970int
4971lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04004972{
4973 LPFC_MBOXQ_t *mbox;
James Smart6fb120a2009-05-22 14:52:59 -04004974 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05004975 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05004976 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05004977 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04004978
James Smartecfd03c2010-02-12 14:41:27 -05004979 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04004980 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05004981 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04004982
James Smart4d9ab992009-10-02 15:16:39 -04004983 /* At this point, all discovery is aborted */
4984 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04004985
4986 /* Unregister VPIs */
4987 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05004988 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04004989 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05004990 /* Stop FLOGI/FDISC retries */
4991 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
4992 if (ndlp)
4993 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart6fb120a2009-05-22 14:52:59 -04004994 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05004995 shost = lpfc_shost_from_vport(vports[i]);
4996 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05004997 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05004998 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05004999 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005000 }
5001 lpfc_destroy_vport_work_array(phba, vports);
5002
James Smart695a8142010-01-26 23:08:03 -05005003 /* Cleanup any outstanding ELS commands */
5004 lpfc_els_flush_all_cmd(phba);
5005
James Smart6fb120a2009-05-22 14:52:59 -04005006 /* Unregister VFI */
5007 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5008 if (!mbox) {
5009 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005010 "2556 UNREG_VFI mbox allocation failed"
5011 "HBA state x%x\n", phba->pport->port_state);
5012 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005013 }
5014
James Smart6669f9b2009-10-02 15:16:45 -04005015 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04005016 mbox->vport = phba->pport;
5017 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
5018
5019 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5020 if (rc == MBX_NOT_FINISHED) {
5021 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005022 "2557 UNREG_VFI issue mbox failed rc x%x "
5023 "HBA state x%x\n",
5024 rc, phba->pport->port_state);
James Smart6fb120a2009-05-22 14:52:59 -04005025 mempool_free(mbox, phba->mbox_mem_pool);
James Smartecfd03c2010-02-12 14:41:27 -05005026 return -EIO;
James Smart6fb120a2009-05-22 14:52:59 -04005027 }
5028
James Smart72100cc2010-02-12 14:43:01 -05005029 shost = lpfc_shost_from_vport(phba->pport);
5030 spin_lock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005031 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005032 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005033
James Smartecfd03c2010-02-12 14:41:27 -05005034 return 0;
5035}
5036
5037/**
5038 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5039 * @phba: Pointer to hba context object.
5040 *
5041 * This function issues synchronous unregister FCF mailbox command to HBA to
5042 * unregister the currently registered FCF record. The driver does not reset
5043 * the driver FCF usage state flags.
5044 *
5045 * Return 0 if successfully issued, none-zero otherwise.
5046 */
5047int
5048lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5049{
5050 LPFC_MBOXQ_t *mbox;
5051 int rc;
5052
James Smart6fb120a2009-05-22 14:52:59 -04005053 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5054 if (!mbox) {
5055 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005056 "2551 UNREG_FCFI mbox allocation failed"
5057 "HBA state x%x\n", phba->pport->port_state);
5058 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005059 }
James Smart6fb120a2009-05-22 14:52:59 -04005060 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5061 mbox->vport = phba->pport;
5062 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5063 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5064
5065 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005066 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5067 "2552 Unregister FCFI command failed rc x%x "
5068 "HBA state x%x\n",
5069 rc, phba->pport->port_state);
5070 return -EINVAL;
5071 }
5072 return 0;
5073}
5074
5075/**
5076 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5077 * @phba: Pointer to hba context object.
5078 *
5079 * This function unregisters the currently reigstered FCF. This function
5080 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5081 */
5082void
5083lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5084{
5085 int rc;
5086
5087 /* Preparation for unregistering fcf */
5088 rc = lpfc_unregister_fcf_prep(phba);
5089 if (rc) {
5090 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5091 "2748 Failed to prepare for unregistering "
5092 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005093 return;
5094 }
5095
James Smartecfd03c2010-02-12 14:41:27 -05005096 /* Now, unregister FCF record and reset HBA FCF state */
5097 rc = lpfc_sli4_unregister_fcf(phba);
5098 if (rc)
5099 return;
5100 /* Reset HBA FCF states after successful unregister FCF */
5101 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005102 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005103
5104 /*
5105 * If driver is not unloading, check if there is any other
5106 * FCF record that can be used for discovery.
5107 */
5108 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005109 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005110 return;
5111
James Smart0c9ab6f2010-02-26 14:15:57 -05005112 /* This is considered as the initial FCF discovery scan */
5113 spin_lock_irq(&phba->hbalock);
5114 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5115 spin_unlock_irq(&phba->hbalock);
5116 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005117
James Smart0c9ab6f2010-02-26 14:15:57 -05005118 if (rc) {
5119 spin_lock_irq(&phba->hbalock);
5120 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5121 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005122 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005123 "2553 lpfc_unregister_unused_fcf failed "
5124 "to read FCF record HBA state x%x\n",
5125 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005126 }
James Smartecfd03c2010-02-12 14:41:27 -05005127}
5128
5129/**
5130 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5131 * @phba: Pointer to hba context object.
5132 *
5133 * This function just unregisters the currently reigstered FCF. It does not
5134 * try to find another FCF for discovery.
5135 */
5136void
5137lpfc_unregister_fcf(struct lpfc_hba *phba)
5138{
5139 int rc;
5140
5141 /* Preparation for unregistering fcf */
5142 rc = lpfc_unregister_fcf_prep(phba);
5143 if (rc) {
5144 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5145 "2749 Failed to prepare for unregistering "
5146 "HBA's FCF record: rc=%d\n", rc);
5147 return;
5148 }
5149
5150 /* Now, unregister FCF record and reset HBA FCF state */
5151 rc = lpfc_sli4_unregister_fcf(phba);
5152 if (rc)
5153 return;
5154 /* Set proper HBA FCF states after successful unregister FCF */
5155 spin_lock_irq(&phba->hbalock);
5156 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
5157 spin_unlock_irq(&phba->hbalock);
5158}
5159
5160/**
5161 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
5162 * @phba: Pointer to hba context object.
5163 *
5164 * This function check if there are any connected remote port for the FCF and
5165 * if all the devices are disconnected, this function unregister FCFI.
5166 * This function also tries to use another FCF for discovery.
5167 */
5168void
5169lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
5170{
5171 /*
5172 * If HBA is not running in FIP mode or if HBA does not support
5173 * FCoE or if FCF is not registered, do nothing.
5174 */
5175 spin_lock_irq(&phba->hbalock);
5176 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
5177 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
5178 !(phba->hba_flag & HBA_FIP_SUPPORT)) {
5179 spin_unlock_irq(&phba->hbalock);
5180 return;
5181 }
5182 spin_unlock_irq(&phba->hbalock);
5183
5184 if (lpfc_fcf_inuse(phba))
5185 return;
5186
5187 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005188}
5189
5190/**
5191 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
5192 * @phba: Pointer to hba context object.
5193 * @buff: Buffer containing the FCF connection table as in the config
5194 * region.
5195 * This function create driver data structure for the FCF connection
5196 * record table read from config region 23.
5197 */
5198static void
5199lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
5200 uint8_t *buff)
5201{
5202 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5203 struct lpfc_fcf_conn_hdr *conn_hdr;
5204 struct lpfc_fcf_conn_rec *conn_rec;
5205 uint32_t record_count;
5206 int i;
5207
5208 /* Free the current connect table */
5209 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005210 &phba->fcf_conn_rec_list, list) {
5211 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04005212 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005213 }
James Smart6fb120a2009-05-22 14:52:59 -04005214
5215 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
5216 record_count = conn_hdr->length * sizeof(uint32_t)/
5217 sizeof(struct lpfc_fcf_conn_rec);
5218
5219 conn_rec = (struct lpfc_fcf_conn_rec *)
5220 (buff + sizeof(struct lpfc_fcf_conn_hdr));
5221
5222 for (i = 0; i < record_count; i++) {
5223 if (!(conn_rec[i].flags & FCFCNCT_VALID))
5224 continue;
5225 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
5226 GFP_KERNEL);
5227 if (!conn_entry) {
5228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5229 "2566 Failed to allocate connection"
5230 " table entry\n");
5231 return;
5232 }
5233
5234 memcpy(&conn_entry->conn_rec, &conn_rec[i],
5235 sizeof(struct lpfc_fcf_conn_rec));
5236 conn_entry->conn_rec.vlan_tag =
5237 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
5238 conn_entry->conn_rec.flags =
5239 le16_to_cpu(conn_entry->conn_rec.flags);
5240 list_add_tail(&conn_entry->list,
5241 &phba->fcf_conn_rec_list);
5242 }
5243}
5244
5245/**
5246 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
5247 * @phba: Pointer to hba context object.
5248 * @buff: Buffer containing the FCoE parameter data structure.
5249 *
5250 * This function update driver data structure with config
5251 * parameters read from config region 23.
5252 */
5253static void
5254lpfc_read_fcoe_param(struct lpfc_hba *phba,
5255 uint8_t *buff)
5256{
5257 struct lpfc_fip_param_hdr *fcoe_param_hdr;
5258 struct lpfc_fcoe_params *fcoe_param;
5259
5260 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
5261 buff;
5262 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04005263 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04005264
5265 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
5266 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
5267 return;
5268
James Smart6fb120a2009-05-22 14:52:59 -04005269 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
5270 phba->valid_vlan = 1;
5271 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
5272 0xFFF;
5273 }
5274
5275 phba->fc_map[0] = fcoe_param->fc_map[0];
5276 phba->fc_map[1] = fcoe_param->fc_map[1];
5277 phba->fc_map[2] = fcoe_param->fc_map[2];
5278 return;
5279}
5280
5281/**
5282 * lpfc_get_rec_conf23 - Get a record type in config region data.
5283 * @buff: Buffer containing config region 23 data.
5284 * @size: Size of the data buffer.
5285 * @rec_type: Record type to be searched.
5286 *
5287 * This function searches config region data to find the begining
5288 * of the record specified by record_type. If record found, this
5289 * function return pointer to the record else return NULL.
5290 */
5291static uint8_t *
5292lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
5293{
5294 uint32_t offset = 0, rec_length;
5295
5296 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
5297 (size < sizeof(uint32_t)))
5298 return NULL;
5299
5300 rec_length = buff[offset + 1];
5301
5302 /*
5303 * One TLV record has one word header and number of data words
5304 * specified in the rec_length field of the record header.
5305 */
5306 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
5307 <= size) {
5308 if (buff[offset] == rec_type)
5309 return &buff[offset];
5310
5311 if (buff[offset] == LPFC_REGION23_LAST_REC)
5312 return NULL;
5313
5314 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
5315 rec_length = buff[offset + 1];
5316 }
5317 return NULL;
5318}
5319
5320/**
5321 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
5322 * @phba: Pointer to lpfc_hba data structure.
5323 * @buff: Buffer containing config region 23 data.
5324 * @size: Size of the data buffer.
5325 *
5326 * This fuction parse the FCoE config parameters in config region 23 and
5327 * populate driver data structure with the parameters.
5328 */
5329void
5330lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
5331 uint8_t *buff,
5332 uint32_t size)
5333{
5334 uint32_t offset = 0, rec_length;
5335 uint8_t *rec_ptr;
5336
5337 /*
5338 * If data size is less than 2 words signature and version cannot be
5339 * verified.
5340 */
5341 if (size < 2*sizeof(uint32_t))
5342 return;
5343
5344 /* Check the region signature first */
5345 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
5346 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5347 "2567 Config region 23 has bad signature\n");
5348 return;
5349 }
5350
5351 offset += 4;
5352
5353 /* Check the data structure version */
5354 if (buff[offset] != LPFC_REGION23_VERSION) {
5355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5356 "2568 Config region 23 has bad version\n");
5357 return;
5358 }
5359 offset += 4;
5360
5361 rec_length = buff[offset + 1];
5362
5363 /* Read FCoE param record */
5364 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5365 size - offset, FCOE_PARAM_TYPE);
5366 if (rec_ptr)
5367 lpfc_read_fcoe_param(phba, rec_ptr);
5368
5369 /* Read FCF connection table */
5370 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5371 size - offset, FCOE_CONN_TBL_TYPE);
5372 if (rec_ptr)
5373 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5374
5375}