blob: a610464da16ed81b0a1b2d3d94959b1a9e84ea86 [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 Smartffc95492010-06-07 15:23:17 -0400278 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
James Smart589a52d2010-07-14 15:30:54 -0400279 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
280 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500281 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400282
283 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500284}
James Smartc01f3202006-08-18 17:47:08 -0400285
James Smartea2151b2008-09-07 11:52:10 -0400286/**
James Smart3621a712009-04-06 18:47:14 -0400287 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400288 * @phba: Pointer to hba context object.
289 *
290 * This function is called from the functions which need to post
291 * events from interrupt context. This function allocates data
292 * structure required for posting event. It also keeps track of
293 * number of events pending and prevent event storm when there are
294 * too many events.
295 **/
296struct lpfc_fast_path_event *
297lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
298 struct lpfc_fast_path_event *ret;
299
300 /* If there are lot of fast event do not exhaust memory due to this */
301 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
302 return NULL;
303
304 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
305 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400306 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400307 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400308 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
309 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
310 }
James Smartea2151b2008-09-07 11:52:10 -0400311 return ret;
312}
313
314/**
James Smart3621a712009-04-06 18:47:14 -0400315 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400316 * @phba: Pointer to hba context object.
317 * @evt: Event object which need to be freed.
318 *
319 * This function frees the data structure required for posting
320 * events.
321 **/
322void
323lpfc_free_fast_evt(struct lpfc_hba *phba,
324 struct lpfc_fast_path_event *evt) {
325
326 atomic_dec(&phba->fast_event_count);
327 kfree(evt);
328}
329
330/**
James Smart3621a712009-04-06 18:47:14 -0400331 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400332 * @phba: Pointer to hba context object.
333 * @evtp: Event data structure.
334 *
335 * This function is called from worker thread, when the interrupt
336 * context need to post an event. This function posts the event
337 * to fc transport netlink interface.
338 **/
339static void
340lpfc_send_fastpath_evt(struct lpfc_hba *phba,
341 struct lpfc_work_evt *evtp)
342{
343 unsigned long evt_category, evt_sub_category;
344 struct lpfc_fast_path_event *fast_evt_data;
345 char *evt_data;
346 uint32_t evt_data_size;
347 struct Scsi_Host *shost;
348
349 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
350 work_evt);
351
352 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
353 evt_sub_category = (unsigned long) fast_evt_data->un.
354 fabric_evt.subcategory;
355 shost = lpfc_shost_from_vport(fast_evt_data->vport);
356 if (evt_category == FC_REG_FABRIC_EVENT) {
357 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
358 evt_data = (char *) &fast_evt_data->un.read_check_error;
359 evt_data_size = sizeof(fast_evt_data->un.
360 read_check_error);
361 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500362 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400363 evt_data = (char *) &fast_evt_data->un.fabric_evt;
364 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
365 } else {
366 lpfc_free_fast_evt(phba, fast_evt_data);
367 return;
368 }
369 } else if (evt_category == FC_REG_SCSI_EVENT) {
370 switch (evt_sub_category) {
371 case LPFC_EVENT_QFULL:
372 case LPFC_EVENT_DEVBSY:
373 evt_data = (char *) &fast_evt_data->un.scsi_evt;
374 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
375 break;
376 case LPFC_EVENT_CHECK_COND:
377 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
378 evt_data_size = sizeof(fast_evt_data->un.
379 check_cond_evt);
380 break;
381 case LPFC_EVENT_VARQUEDEPTH:
382 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
383 evt_data_size = sizeof(fast_evt_data->un.
384 queue_depth_evt);
385 break;
386 default:
387 lpfc_free_fast_evt(phba, fast_evt_data);
388 return;
389 }
390 } else {
391 lpfc_free_fast_evt(phba, fast_evt_data);
392 return;
393 }
394
395 fc_host_post_vendor_event(shost,
396 fc_get_event_number(),
397 evt_data_size,
398 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500399 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400400
401 lpfc_free_fast_evt(phba, fast_evt_data);
402 return;
403}
404
dea31012005-04-17 16:05:31 -0500405static void
James Smart2e0fef82007-06-17 19:56:36 -0500406lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500407{
408 struct lpfc_work_evt *evtp = NULL;
409 struct lpfc_nodelist *ndlp;
410 int free_evt;
411
James Smart2e0fef82007-06-17 19:56:36 -0500412 spin_lock_irq(&phba->hbalock);
413 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500414 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
415 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500416 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500417 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500418 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500419 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500420 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500421 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500422 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500423 /* decrement the node reference count held
424 * for this queued work
425 */
426 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500427 break;
James Smart858c9f62007-06-17 19:56:39 -0500428 case LPFC_EVT_DEV_LOSS:
429 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500430 lpfc_dev_loss_tmo_handler(ndlp);
431 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500432 /* decrement the node reference count held for
433 * this queued work
434 */
James Smart858c9f62007-06-17 19:56:39 -0500435 lpfc_nlp_put(ndlp);
436 break;
dea31012005-04-17 16:05:31 -0500437 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500438 if (phba->link_state < LPFC_LINK_DOWN)
439 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500440 else
James Smart2e0fef82007-06-17 19:56:36 -0500441 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500442 complete((struct completion *)(evtp->evt_arg2));
443 break;
James Smart46fa3112007-04-25 09:51:45 -0400444 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500445 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400446 lpfc_offline_prep(phba);
447 *(int *)(evtp->evt_arg1) = 0;
448 complete((struct completion *)(evtp->evt_arg2));
449 break;
450 case LPFC_EVT_OFFLINE:
451 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500452 lpfc_sli_brdrestart(phba);
453 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400454 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
455 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500456 complete((struct completion *)(evtp->evt_arg2));
457 break;
458 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400459 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500460 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500461 lpfc_sli_brdreset(phba);
462 lpfc_hba_down_post(phba);
463 *(int *)(evtp->evt_arg1) =
464 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400465 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500466 complete((struct completion *)(evtp->evt_arg2));
467 break;
468 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400469 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500470 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500471 = (phba->pport->stopped)
472 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400473 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500474 complete((struct completion *)(evtp->evt_arg2));
475 break;
James Smartea2151b2008-09-07 11:52:10 -0400476 case LPFC_EVT_FASTPATH_MGMT_EVT:
477 lpfc_send_fastpath_evt(phba, evtp);
478 free_evt = 0;
479 break;
James Smart78730cf2010-04-06 15:06:30 -0400480 case LPFC_EVT_RESET_HBA:
481 if (!(phba->pport->load_flag & FC_UNLOADING))
482 lpfc_reset_hba(phba);
483 break;
dea31012005-04-17 16:05:31 -0500484 }
485 if (free_evt)
486 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500487 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500488 }
James Smart2e0fef82007-06-17 19:56:36 -0500489 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500490
491}
492
James Smart311464e2007-08-02 11:10:37 -0400493static void
James Smart2e0fef82007-06-17 19:56:36 -0500494lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500495{
496 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500497 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400498 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400499 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400500 int i;
dea31012005-04-17 16:05:31 -0500501
James Smart2e0fef82007-06-17 19:56:36 -0500502 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500503 ha_copy = phba->work_ha;
504 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500505 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500506
James Smartda0436e2009-05-22 14:51:39 -0400507 /* First, try to post the next mailbox command to SLI4 device */
508 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
509 lpfc_sli4_post_async_mbox(phba);
510
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500511 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400512 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500513 lpfc_handle_eratt(phba);
514
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500515 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500516 lpfc_sli_handle_mb_event(phba);
517
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500518 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500519 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400520
James Smartda0436e2009-05-22 14:51:39 -0400521 /* Process SLI4 events */
522 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
523 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
524 lpfc_sli4_fcp_xri_abort_event_proc(phba);
525 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
526 lpfc_sli4_els_xri_abort_event_proc(phba);
527 if (phba->hba_flag & ASYNC_EVENT)
528 lpfc_sli4_async_event_proc(phba);
529 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
530 spin_lock_irq(&phba->hbalock);
531 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
532 spin_unlock_irq(&phba->hbalock);
533 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
534 }
James Smartecfd03c2010-02-12 14:41:27 -0500535 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
536 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400537 }
538
James Smart549e55c2007-08-02 11:09:51 -0400539 vports = lpfc_create_vport_work_array(phba);
540 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400541 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400542 /*
543 * We could have no vports in array if unloading, so if
544 * this happens then just use the pport
545 */
546 if (vports[i] == NULL && i == 0)
547 vport = phba->pport;
548 else
549 vport = vports[i];
550 if (vport == NULL)
551 break;
James Smart58da1ff2008-04-07 10:15:56 -0400552 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400553 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400554 vport->work_port_events &= ~work_port_events;
555 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400556 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400557 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400558 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400559 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400560 if (work_port_events & WORKER_HB_TMO)
561 lpfc_hb_timeout_handler(phba);
562 if (work_port_events & WORKER_MBOX_TMO)
563 lpfc_mbox_timeout_handler(phba);
564 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
565 lpfc_unblock_fabric_iocbs(phba);
566 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400567 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400568 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
569 lpfc_ramp_down_queue_handler(phba);
570 if (work_port_events & WORKER_RAMP_UP_QUEUE)
571 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500572 }
James Smart09372822008-01-11 01:52:54 -0500573 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500574
James Smart858c9f62007-06-17 19:56:39 -0500575 pring = &phba->sli.ring[LPFC_ELS_RING];
576 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
577 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400578 if ((status & HA_RXMASK) ||
579 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400580 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400581 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500582 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400583 /* Set the lpfc data pending flag */
584 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500585 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400586 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500587 lpfc_sli_handle_slow_ring_event(phba, pring,
588 (status &
589 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500590 }
James Smart589a52d2010-07-14 15:30:54 -0400591 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400592 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500593 /*
594 * Turn on Ring interrupts
595 */
James Smart3772a992009-05-22 14:50:54 -0400596 if (phba->sli_rev <= LPFC_SLI_REV3) {
597 spin_lock_irq(&phba->hbalock);
598 control = readl(phba->HCregaddr);
599 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
600 lpfc_debugfs_slow_ring_trc(phba,
601 "WRK Enable ring: cntl:x%x hacopy:x%x",
602 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400603
James Smart3772a992009-05-22 14:50:54 -0400604 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
605 writel(control, phba->HCregaddr);
606 readl(phba->HCregaddr); /* flush */
607 } else {
608 lpfc_debugfs_slow_ring_trc(phba,
609 "WRK Ring ok: cntl:x%x hacopy:x%x",
610 control, ha_copy, 0);
611 }
612 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500613 }
614 }
James Smart2e0fef82007-06-17 19:56:36 -0500615 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500616}
617
dea31012005-04-17 16:05:31 -0500618int
619lpfc_do_work(void *p)
620{
621 struct lpfc_hba *phba = p;
622 int rc;
dea31012005-04-17 16:05:31 -0500623
624 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400625 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500626
James Smart3a55b532008-12-04 22:38:54 -0500627 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400628 /* wait and check worker queue activities */
629 rc = wait_event_interruptible(phba->work_waitq,
630 (test_and_clear_bit(LPFC_DATA_READY,
631 &phba->data_flags)
632 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500633 /* Signal wakeup shall terminate the worker thread */
634 if (rc) {
635 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
636 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500637 break;
James Smart3a55b532008-12-04 22:38:54 -0500638 }
dea31012005-04-17 16:05:31 -0500639
James Smart5e9d9b82008-06-14 22:52:53 -0400640 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500641 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500642 }
James Smart3a55b532008-12-04 22:38:54 -0500643 phba->worker_thread = NULL;
644 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
645 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500646 return 0;
647}
648
649/*
650 * This is only called to handle FC worker events. Since this a rare
651 * occurance, we allocate a struct lpfc_work_evt structure here instead of
652 * embedding it in the IOCB.
653 */
654int
James Smart2e0fef82007-06-17 19:56:36 -0500655lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500656 uint32_t evt)
657{
658 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500659 unsigned long flags;
dea31012005-04-17 16:05:31 -0500660
661 /*
662 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
663 * be queued to worker thread for processing
664 */
James Smart92d7f7b2007-06-17 19:56:38 -0500665 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500666 if (!evtp)
667 return 0;
668
669 evtp->evt_arg1 = arg1;
670 evtp->evt_arg2 = arg2;
671 evtp->evt = evt;
672
James Smarted957682007-06-17 19:56:37 -0500673 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400674 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500675 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500676
James Smart5e9d9b82008-06-14 22:52:53 -0400677 lpfc_worker_wake_up(phba);
678
dea31012005-04-17 16:05:31 -0500679 return 1;
680}
681
James Smart92d7f7b2007-06-17 19:56:38 -0500682void
683lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
684{
James Smart09372822008-01-11 01:52:54 -0500685 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500686 struct lpfc_hba *phba = vport->phba;
687 struct lpfc_nodelist *ndlp, *next_ndlp;
688 int rc;
689
690 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500691 if (!NLP_CHK_NODE_ACT(ndlp))
692 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500693 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
694 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400695 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
696 ((vport->port_type == LPFC_NPIV_PORT) &&
697 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500698 lpfc_unreg_rpi(vport, ndlp);
699
700 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400701 if ((phba->sli_rev < LPFC_SLI_REV4) &&
702 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500703 continue;
704 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
705 remove
706 ? NLP_EVT_DEVICE_RM
707 : NLP_EVT_DEVICE_RECOVERY);
708 }
709 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
710 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500711 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500712 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500713 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500714 }
715}
716
James Smart87af33f2007-10-27 13:37:43 -0400717void
James Smart98c9ea52007-10-27 13:37:33 -0400718lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500719{
James Smart695a8142010-01-26 23:08:03 -0500720 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
721
James Smart45ed1192009-10-02 15:17:02 -0400722 /* Cleanup any outstanding received buffers */
723 lpfc_cleanup_rcv_buffers(vport);
724
James Smart92d7f7b2007-06-17 19:56:38 -0500725 /* Cleanup any outstanding RSCN activity */
726 lpfc_els_flush_rscn(vport);
727
728 /* Cleanup any outstanding ELS commands */
729 lpfc_els_flush_cmd(vport);
730
731 lpfc_cleanup_rpis(vport, 0);
732
James Smart92d7f7b2007-06-17 19:56:38 -0500733 /* Turn off discovery timer if its running */
734 lpfc_can_disctmo(vport);
735}
736
James Smart3772a992009-05-22 14:50:54 -0400737void
James Smart98c9ea52007-10-27 13:37:33 -0400738lpfc_linkdown_port(struct lpfc_vport *vport)
739{
740 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
741
742 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
743
744 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
745 "Link Down: state:x%x rtry:x%x flg:x%x",
746 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
747
748 lpfc_port_link_failure(vport);
749
750}
751
dea31012005-04-17 16:05:31 -0500752int
James Smart685f0bf2007-04-25 09:53:08 -0400753lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500754{
James Smart2e0fef82007-06-17 19:56:36 -0500755 struct lpfc_vport *vport = phba->pport;
756 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400757 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400758 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400759 int i;
dea31012005-04-17 16:05:31 -0500760
James Smart3163f722008-02-08 18:50:25 -0500761 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500762 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500763
764 /* Block all SCSI stack I/Os */
765 lpfc_scsi_dev_block(phba);
766
James Smart2e0fef82007-06-17 19:56:36 -0500767 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500768 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500769 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500770 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500771 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500772 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500773 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500774 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500775 }
James Smart549e55c2007-08-02 11:09:51 -0400776 vports = lpfc_create_vport_work_array(phba);
777 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400778 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400779 /* Issue a LINK DOWN event to all nodes */
780 lpfc_linkdown_port(vports[i]);
781 }
James Smart09372822008-01-11 01:52:54 -0500782 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500783 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500784 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
785 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500786 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500787 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500788 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400789 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500790 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500791 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500792 }
793 }
794
dea31012005-04-17 16:05:31 -0500795 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500796 if (phba->pport->fc_flag & FC_PT2PT) {
797 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500798 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
799 if (mb) {
dea31012005-04-17 16:05:31 -0500800 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500801 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500802 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400803 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500804 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500805 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500806 }
807 }
James Smart2e0fef82007-06-17 19:56:36 -0500808 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500809 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500810 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500811 }
James Smart2e0fef82007-06-17 19:56:36 -0500812
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500813 return 0;
dea31012005-04-17 16:05:31 -0500814}
815
James Smart92d7f7b2007-06-17 19:56:38 -0500816static void
817lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500818{
James Smart92d7f7b2007-06-17 19:56:38 -0500819 struct lpfc_nodelist *ndlp;
820
821 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500822 if (!NLP_CHK_NODE_ACT(ndlp))
823 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500824 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
825 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500826 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500827 /* On Linkup its safe to clean up the ndlp
828 * from Fabric connections.
829 */
James Smart92d7f7b2007-06-17 19:56:38 -0500830 if (ndlp->nlp_DID != Fabric_DID)
831 lpfc_unreg_rpi(vport, ndlp);
832 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
833 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500834 /* Fail outstanding IO now since device is
835 * marked for PLOGI.
836 */
James Smart92d7f7b2007-06-17 19:56:38 -0500837 lpfc_unreg_rpi(vport, ndlp);
838 }
839 }
840}
841
842static void
843lpfc_linkup_port(struct lpfc_vport *vport)
844{
845 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500846 struct lpfc_hba *phba = vport->phba;
847
848 if ((vport->load_flag & FC_UNLOADING) != 0)
849 return;
850
James Smart858c9f62007-06-17 19:56:39 -0500851 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
852 "Link Up: top:x%x speed:x%x flg:x%x",
853 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
854
James Smart92d7f7b2007-06-17 19:56:38 -0500855 /* If NPIV is not enabled, only bring the physical port up */
856 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
857 (vport != phba->pport))
858 return;
dea31012005-04-17 16:05:31 -0500859
James Smart2e0fef82007-06-17 19:56:36 -0500860 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400861
James Smart2e0fef82007-06-17 19:56:36 -0500862 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500863 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
864 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
865 vport->fc_flag |= FC_NDISC_ACTIVE;
866 vport->fc_ns_retry = 0;
867 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500868
James Smart92d7f7b2007-06-17 19:56:38 -0500869 if (vport->fc_flag & FC_LBIT)
870 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500871
James Smart92d7f7b2007-06-17 19:56:38 -0500872}
873
874static int
875lpfc_linkup(struct lpfc_hba *phba)
876{
James Smart549e55c2007-08-02 11:09:51 -0400877 struct lpfc_vport **vports;
878 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500879
880 phba->link_state = LPFC_LINK_UP;
881
882 /* Unblock fabric iocbs if they are blocked */
883 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
884 del_timer_sync(&phba->fabric_block_timer);
885
James Smart549e55c2007-08-02 11:09:51 -0400886 vports = lpfc_create_vport_work_array(phba);
887 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400888 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400889 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500890 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400891 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
892 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500893 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500894
895 return 0;
896}
897
898/*
899 * This routine handles processing a CLEAR_LA mailbox
900 * command upon completion. It is setup in the LPFC_MBOXQ
901 * as the completion routine when the command is
902 * handed off to the SLI layer.
903 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100904static void
James Smart2e0fef82007-06-17 19:56:36 -0500905lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500906{
James Smart2e0fef82007-06-17 19:56:36 -0500907 struct lpfc_vport *vport = pmb->vport;
908 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
909 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400910 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500911 uint32_t control;
912
dea31012005-04-17 16:05:31 -0500913 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500914 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500915 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
916 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
917
918 /* Check for error */
919 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500920 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400921 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
922 "0320 CLEAR_LA mbxStatus error x%x hba "
923 "state x%x\n",
924 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500925 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500926 goto out;
927 }
928
James Smart92d7f7b2007-06-17 19:56:38 -0500929 if (vport->port_type == LPFC_PHYSICAL_PORT)
930 phba->link_state = LPFC_HBA_READY;
931
932 spin_lock_irq(&phba->hbalock);
933 psli->sli_flag |= LPFC_PROCESS_LA;
934 control = readl(phba->HCregaddr);
935 control |= HC_LAINT_ENA;
936 writel(control, phba->HCregaddr);
937 readl(phba->HCregaddr); /* flush */
938 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500939 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500940 return;
dea31012005-04-17 16:05:31 -0500941
dea31012005-04-17 16:05:31 -0500942out:
943 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400944 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
945 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500946 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500947
James Smart2e0fef82007-06-17 19:56:36 -0500948 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400949 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500950 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500951
James Smart2e0fef82007-06-17 19:56:36 -0500952 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500953
954 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500955
956 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500957 psli->sli_flag |= LPFC_PROCESS_LA;
958 control = readl(phba->HCregaddr);
959 control |= HC_LAINT_ENA;
960 writel(control, phba->HCregaddr);
961 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500962 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500963
964 return;
965}
966
James Smart2e0fef82007-06-17 19:56:36 -0500967
dea31012005-04-17 16:05:31 -0500968static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500969lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500970{
James Smart2e0fef82007-06-17 19:56:36 -0500971 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500972
James Smart04c68492009-05-22 14:52:52 -0400973 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500974 goto out;
dea31012005-04-17 16:05:31 -0500975
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500976 mempool_free(pmb, phba->mbox_mem_pool);
977
978 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500979 vport->fc_flag & FC_PUBLIC_LOOP &&
980 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500981 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500982 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500983 * LPFC_LOCAL_CFG_LINK while waiting for FAN
984 */
James Smart2e0fef82007-06-17 19:56:36 -0500985 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500986 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500987 }
dea31012005-04-17 16:05:31 -0500988
James Smart2e0fef82007-06-17 19:56:36 -0500989 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500990 * LPFC_FLOGI while waiting for FLOGI cmpl
991 */
James Smart92d7f7b2007-06-17 19:56:38 -0500992 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500993 lpfc_initial_flogi(vport);
994 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500995 return;
dea31012005-04-17 16:05:31 -0500996
997out:
James Smarte8b62012007-08-02 11:10:09 -0400998 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
999 "0306 CONFIG_LINK mbxStatus error x%x "
1000 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001001 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001002 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001003
1004 lpfc_linkdown(phba);
1005
James Smarte8b62012007-08-02 11:10:09 -04001006 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1007 "0200 CONFIG_LINK bad hba state x%x\n",
1008 vport->port_state);
dea31012005-04-17 16:05:31 -05001009
James Smart92d7f7b2007-06-17 19:56:38 -05001010 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001011 return;
1012}
1013
1014static void
James Smart6fb120a2009-05-22 14:52:59 -04001015lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1016{
1017 struct lpfc_vport *vport = mboxq->vport;
1018 unsigned long flags;
1019
1020 if (mboxq->u.mb.mbxStatus) {
1021 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1022 "2017 REG_FCFI mbxStatus error x%x "
1023 "HBA state x%x\n",
1024 mboxq->u.mb.mbxStatus, vport->port_state);
1025 mempool_free(mboxq, phba->mbox_mem_pool);
1026 return;
1027 }
1028
1029 /* Start FCoE discovery by sending a FLOGI. */
1030 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1031 /* Set the FCFI registered flag */
1032 spin_lock_irqsave(&phba->hbalock, flags);
1033 phba->fcf.fcf_flag |= FCF_REGISTERED;
1034 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart32b97932009-07-19 10:01:21 -04001035 /* If there is a pending FCoE event, restart FCF table scan. */
1036 if (lpfc_check_pending_fcoe_event(phba, 1)) {
1037 mempool_free(mboxq, phba->mbox_mem_pool);
1038 return;
1039 }
James Smart1c6f4ef52009-11-18 15:40:49 -05001040 spin_lock_irqsave(&phba->hbalock, flags);
James Smartecfd03c2010-02-12 14:41:27 -05001041 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart4d9ab992009-10-02 15:16:39 -04001042 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001043 spin_unlock_irqrestore(&phba->hbalock, flags);
1044 if (vport->port_state != LPFC_FLOGI)
James Smart6fb120a2009-05-22 14:52:59 -04001045 lpfc_initial_flogi(vport);
James Smart6fb120a2009-05-22 14:52:59 -04001046
1047 mempool_free(mboxq, phba->mbox_mem_pool);
1048 return;
1049}
1050
1051/**
1052 * lpfc_fab_name_match - Check if the fcf fabric name match.
1053 * @fab_name: pointer to fabric name.
1054 * @new_fcf_record: pointer to fcf record.
1055 *
1056 * This routine compare the fcf record's fabric name with provided
1057 * fabric name. If the fabric name are identical this function
1058 * returns 1 else return 0.
1059 **/
1060static uint32_t
1061lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1062{
James Smartecfd03c2010-02-12 14:41:27 -05001063 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001064 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001065 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1066 return 0;
1067 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1068 return 0;
1069 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1070 return 0;
1071 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1072 return 0;
1073 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1074 return 0;
1075 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1076 return 0;
1077 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1078 return 0;
1079 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001080}
1081
1082/**
James Smart8fa38512009-07-19 10:01:03 -04001083 * lpfc_sw_name_match - Check if the fcf switch name match.
1084 * @fab_name: pointer to fabric name.
1085 * @new_fcf_record: pointer to fcf record.
1086 *
1087 * This routine compare the fcf record's switch name with provided
1088 * switch name. If the switch name are identical this function
1089 * returns 1 else return 0.
1090 **/
1091static uint32_t
1092lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1093{
James Smartecfd03c2010-02-12 14:41:27 -05001094 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001095 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001096 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1097 return 0;
1098 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1099 return 0;
1100 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1101 return 0;
1102 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1103 return 0;
1104 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1105 return 0;
1106 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1107 return 0;
1108 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1109 return 0;
1110 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001111}
1112
1113/**
James Smart6fb120a2009-05-22 14:52:59 -04001114 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001115 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001116 * @new_fcf_record: pointer to fcf record.
1117 *
1118 * This routine compare the fcf record's mac address with HBA's
1119 * FCF mac address. If the mac addresses are identical this function
1120 * returns 1 else return 0.
1121 **/
1122static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001123lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001124{
James Smartecfd03c2010-02-12 14:41:27 -05001125 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001126 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001127 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1128 return 0;
1129 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1130 return 0;
1131 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1132 return 0;
1133 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1134 return 0;
1135 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1136 return 0;
1137 return 1;
1138}
1139
1140static bool
1141lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1142{
1143 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001144}
1145
1146/**
1147 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001148 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001149 * @new_fcf_record: pointer to fcf record.
1150 *
1151 * This routine copies the FCF information from the FCF
1152 * record to lpfc_hba data structure.
1153 **/
1154static void
James Smartecfd03c2010-02-12 14:41:27 -05001155lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1156 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001157{
James Smartecfd03c2010-02-12 14:41:27 -05001158 /* Fabric name */
1159 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001160 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001161 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001162 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001163 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001164 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001165 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001166 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001167 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001168 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001169 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001170 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001171 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001172 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001173 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001174 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001175 /* Mac address */
1176 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1177 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1178 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1179 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1180 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1181 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1182 /* FCF record index */
1183 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1184 /* FCF record priority */
1185 fcf_rec->priority = new_fcf_record->fip_priority;
1186 /* Switch name */
1187 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001188 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001189 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001190 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001191 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001192 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001193 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001194 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001195 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001196 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001197 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001198 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001199 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001200 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001201 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001202 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001203}
1204
1205/**
James Smartecfd03c2010-02-12 14:41:27 -05001206 * lpfc_update_fcf_record - Update driver fcf record
1207 * @phba: pointer to lpfc hba data structure.
1208 * @fcf_rec: pointer to driver fcf record.
1209 * @new_fcf_record: pointer to hba fcf record.
1210 * @addr_mode: address mode to be set to the driver fcf record.
1211 * @vlan_id: vlan tag to be set to the driver fcf record.
1212 * @flag: flag bits to be set to the driver fcf record.
1213 *
1214 * This routine updates the driver FCF record from the new HBA FCF record
1215 * together with the address mode, vlan_id, and other informations. This
1216 * routine is called with the host lock held.
1217 **/
1218static void
1219__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1220 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1221 uint16_t vlan_id, uint32_t flag)
1222{
1223 /* Copy the fields from the HBA's FCF record */
1224 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1225 /* Update other fields of driver FCF record */
1226 fcf_rec->addr_mode = addr_mode;
1227 fcf_rec->vlan_id = vlan_id;
1228 fcf_rec->flag |= (flag | RECORD_VALID);
1229}
1230
1231/**
James Smart6fb120a2009-05-22 14:52:59 -04001232 * lpfc_register_fcf - Register the FCF with hba.
1233 * @phba: pointer to lpfc hba data structure.
1234 *
1235 * This routine issues a register fcfi mailbox command to register
1236 * the fcf with HBA.
1237 **/
1238static void
1239lpfc_register_fcf(struct lpfc_hba *phba)
1240{
1241 LPFC_MBOXQ_t *fcf_mbxq;
1242 int rc;
1243 unsigned long flags;
1244
1245 spin_lock_irqsave(&phba->hbalock, flags);
1246
1247 /* If the FCF is not availabe do nothing. */
1248 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smart4d9ab992009-10-02 15:16:39 -04001249 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001250 spin_unlock_irqrestore(&phba->hbalock, flags);
1251 return;
1252 }
1253
1254 /* The FCF is already registered, start discovery */
1255 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001256 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart32b97932009-07-19 10:01:21 -04001257 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001258 spin_unlock_irqrestore(&phba->hbalock, flags);
1259 if (phba->pport->port_state != LPFC_FLOGI)
1260 lpfc_initial_flogi(phba->pport);
1261 return;
1262 }
1263 spin_unlock_irqrestore(&phba->hbalock, flags);
1264
1265 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1266 GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001267 if (!fcf_mbxq) {
1268 spin_lock_irqsave(&phba->hbalock, flags);
1269 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1270 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001271 return;
James Smart4d9ab992009-10-02 15:16:39 -04001272 }
James Smart6fb120a2009-05-22 14:52:59 -04001273
1274 lpfc_reg_fcfi(phba, fcf_mbxq);
1275 fcf_mbxq->vport = phba->pport;
1276 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1277 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001278 if (rc == MBX_NOT_FINISHED) {
1279 spin_lock_irqsave(&phba->hbalock, flags);
1280 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1281 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001282 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001283 }
James Smart6fb120a2009-05-22 14:52:59 -04001284
1285 return;
1286}
1287
1288/**
1289 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1290 * @phba: pointer to lpfc hba data structure.
1291 * @new_fcf_record: pointer to fcf record.
1292 * @boot_flag: Indicates if this record used by boot bios.
1293 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001294 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001295 *
1296 * This routine compare the fcf record with connect list obtained from the
1297 * config region to decide if this FCF can be used for SAN discovery. It returns
1298 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1299 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1300 * is used by boot bios and addr_mode will indicate the addressing mode to be
1301 * used for this FCF when the function returns.
1302 * If the FCF record need to be used with a particular vlan id, the vlan is
1303 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001304 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001305 **/
1306static int
1307lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1308 struct fcf_record *new_fcf_record,
1309 uint32_t *boot_flag, uint32_t *addr_mode,
1310 uint16_t *vlan_id)
1311{
1312 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001313 int i, j, fcf_vlan_id = 0;
1314
1315 /* Find the lowest VLAN id in the FCF record */
1316 for (i = 0; i < 512; i++) {
1317 if (new_fcf_record->vlan_bitmap[i]) {
1318 fcf_vlan_id = i * 8;
1319 j = 0;
1320 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1321 j++;
1322 fcf_vlan_id++;
1323 }
1324 break;
1325 }
1326 }
James Smart6fb120a2009-05-22 14:52:59 -04001327
James Smart0c287582009-06-10 17:22:56 -04001328 /* If FCF not available return 0 */
1329 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1330 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1331 return 0;
1332
James Smart45ed1192009-10-02 15:17:02 -04001333 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001334 *boot_flag = 0;
1335 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1336 new_fcf_record);
1337 if (phba->valid_vlan)
1338 *vlan_id = phba->vlan_id;
1339 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001340 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001341 return 1;
1342 }
1343
1344 /*
1345 * If there are no FCF connection table entry, driver connect to all
1346 * FCFs.
1347 */
1348 if (list_empty(&phba->fcf_conn_rec_list)) {
1349 *boot_flag = 0;
1350 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1351 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001352
1353 /*
1354 * When there are no FCF connect entries, use driver's default
1355 * addressing mode - FPMA.
1356 */
1357 if (*addr_mode & LPFC_FCF_FPMA)
1358 *addr_mode = LPFC_FCF_FPMA;
1359
James Smart4d9ab992009-10-02 15:16:39 -04001360 /* If FCF record report a vlan id use that vlan id */
1361 if (fcf_vlan_id)
1362 *vlan_id = fcf_vlan_id;
1363 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001364 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001365 return 1;
1366 }
1367
James Smartecfd03c2010-02-12 14:41:27 -05001368 list_for_each_entry(conn_entry,
1369 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001370 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1371 continue;
1372
1373 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1374 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001375 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001376 continue;
James Smart8fa38512009-07-19 10:01:03 -04001377 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1378 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1379 new_fcf_record))
1380 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001381 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1382 /*
1383 * If the vlan bit map does not have the bit set for the
1384 * vlan id to be used, then it is not a match.
1385 */
1386 if (!(new_fcf_record->vlan_bitmap
1387 [conn_entry->conn_rec.vlan_tag / 8] &
1388 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1389 continue;
1390 }
1391
1392 /*
James Smart0c287582009-06-10 17:22:56 -04001393 * If connection record does not support any addressing mode,
1394 * skip the FCF record.
1395 */
1396 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1397 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1398 continue;
1399
1400 /*
James Smart6fb120a2009-05-22 14:52:59 -04001401 * Check if the connection record specifies a required
1402 * addressing mode.
1403 */
1404 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1405 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1406
1407 /*
1408 * If SPMA 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_SPMA))
1413 continue;
1414
1415 /*
1416 * If FPMA required but FCF not support this continue.
1417 */
1418 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1419 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1420 new_fcf_record) & LPFC_FCF_FPMA))
1421 continue;
1422 }
1423
1424 /*
1425 * This fcf record matches filtering criteria.
1426 */
1427 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1428 *boot_flag = 1;
1429 else
1430 *boot_flag = 0;
1431
James Smart0c287582009-06-10 17:22:56 -04001432 /*
1433 * If user did not specify any addressing mode, or if the
1434 * prefered addressing mode specified by user is not supported
1435 * by FCF, allow fabric to pick the addressing mode.
1436 */
James Smart6fb120a2009-05-22 14:52:59 -04001437 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1438 new_fcf_record);
1439 /*
1440 * If the user specified a required address mode, assign that
1441 * address mode
1442 */
1443 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1444 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1445 *addr_mode = (conn_entry->conn_rec.flags &
1446 FCFCNCT_AM_SPMA) ?
1447 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1448 /*
1449 * If the user specified a prefered address mode, use the
1450 * addr mode only if FCF support the addr_mode.
1451 */
1452 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1453 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1454 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1455 (*addr_mode & LPFC_FCF_SPMA))
1456 *addr_mode = LPFC_FCF_SPMA;
1457 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1458 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1459 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1460 (*addr_mode & LPFC_FCF_FPMA))
1461 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001462
James Smart4d9ab992009-10-02 15:16:39 -04001463 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001464 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1465 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001466 /*
1467 * If no vlan id is specified in connect list, use the vlan id
1468 * in the FCF record
1469 */
1470 else if (fcf_vlan_id)
1471 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001472 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001473 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001474
1475 return 1;
1476 }
1477
1478 return 0;
1479}
1480
1481/**
James Smart32b97932009-07-19 10:01:21 -04001482 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1483 * @phba: pointer to lpfc hba data structure.
1484 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1485 *
1486 * This function check if there is any fcoe event pending while driver
1487 * scan FCF entries. If there is any pending event, it will restart the
1488 * FCF saning and return 1 else return 0.
1489 */
1490int
1491lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1492{
James Smart32b97932009-07-19 10:01:21 -04001493 /*
1494 * If the Link is up and no FCoE events while in the
1495 * FCF discovery, no need to restart FCF discovery.
1496 */
1497 if ((phba->link_state >= LPFC_LINK_UP) &&
1498 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1499 return 0;
1500
James Smart0c9ab6f2010-02-26 14:15:57 -05001501 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1502 "2768 Pending link or FCF event during current "
1503 "handling of the previous event: link_state:x%x, "
1504 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1505 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1506 phba->fcoe_eventtag);
1507
James Smart32b97932009-07-19 10:01:21 -04001508 spin_lock_irq(&phba->hbalock);
1509 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1510 spin_unlock_irq(&phba->hbalock);
1511
James Smart0c9ab6f2010-02-26 14:15:57 -05001512 if (phba->link_state >= LPFC_LINK_UP) {
1513 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1514 "2780 Restart FCF table scan due to "
1515 "pending FCF event:evt_tag_at_scan:x%x, "
1516 "evt_tag_current:x%x\n",
1517 phba->fcoe_eventtag_at_fcf_scan,
1518 phba->fcoe_eventtag);
1519 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1520 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001521 /*
1522 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1523 * flag
1524 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001525 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1526 "2833 Stop FCF discovery process due to link "
1527 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001528 spin_lock_irq(&phba->hbalock);
James Smart4d9ab992009-10-02 15:16:39 -04001529 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001530 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001531 spin_unlock_irq(&phba->hbalock);
1532 }
James Smart32b97932009-07-19 10:01:21 -04001533
James Smart0c9ab6f2010-02-26 14:15:57 -05001534 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001535 if (unreg_fcf) {
1536 spin_lock_irq(&phba->hbalock);
1537 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1538 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001539 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001540 }
James Smart32b97932009-07-19 10:01:21 -04001541 return 1;
1542}
1543
1544/**
James Smart999d8132010-03-15 11:24:56 -04001545 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1546 * @phba: pointer to lpfc hba data structure.
1547 * @fcf_cnt: number of eligible fcf record seen so far.
1548 *
1549 * This function makes an running random selection decision on FCF record to
1550 * use through a sequence of @fcf_cnt eligible FCF records with equal
1551 * probability. To perform integer manunipulation of random numbers with
1552 * size unit32_t, the lower 16 bits of the 32-bit random number returned
1553 * from random32() are taken as the random random number generated.
1554 *
1555 * Returns true when outcome is for the newly read FCF record should be
1556 * chosen; otherwise, return false when outcome is for keeping the previously
1557 * chosen FCF record.
1558 **/
1559static bool
1560lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1561{
1562 uint32_t rand_num;
1563
1564 /* Get 16-bit uniform random number */
1565 rand_num = (0xFFFF & random32());
1566
1567 /* Decision with probability 1/fcf_cnt */
1568 if ((fcf_cnt * rand_num) < 0xFFFF)
1569 return true;
1570 else
1571 return false;
1572}
1573
1574/**
1575 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
James Smart6fb120a2009-05-22 14:52:59 -04001576 * @phba: pointer to lpfc hba data structure.
1577 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001578 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001579 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001580 * This routine parses the non-embedded fcf mailbox command by performing the
1581 * necessarily error checking, non-embedded read FCF record mailbox command
1582 * SGE parsing, and endianness swapping.
1583 *
1584 * Returns the pointer to the new FCF record in the non-embedded mailbox
1585 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001586 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001587static struct fcf_record *
1588lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1589 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001590{
1591 void *virt_addr;
1592 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001593 struct lpfc_mbx_sge sge;
1594 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1595 uint32_t shdr_status, shdr_add_status;
1596 union lpfc_sli4_cfg_shdr *shdr;
1597 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001598
James Smart6fb120a2009-05-22 14:52:59 -04001599 /* Get the first SGE entry from the non-embedded DMA memory. This
1600 * routine only uses a single SGE.
1601 */
1602 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1603 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1604 if (unlikely(!mboxq->sge_array)) {
1605 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1606 "2524 Failed to get the non-embedded SGE "
1607 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001608 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001609 }
1610 virt_addr = mboxq->sge_array->addr[0];
1611
1612 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1613 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001614 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001615 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001616 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1617 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001618 "2726 READ_FCF_RECORD Indicates empty "
1619 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001620 else
1621 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001622 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001623 "with status x%x add_status x%x, "
1624 "mbx\n", shdr_status, shdr_add_status);
1625 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001626 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001627
1628 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001629 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1630 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1631 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001632 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001633 new_fcf_record = (struct fcf_record *)(virt_addr +
1634 sizeof(struct lpfc_mbx_read_fcf_tbl));
1635 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001636 offsetof(struct fcf_record, vlan_bitmap));
1637 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1638 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001639
James Smart0c9ab6f2010-02-26 14:15:57 -05001640 return new_fcf_record;
1641}
1642
1643/**
1644 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1645 * @phba: pointer to lpfc hba data structure.
1646 * @fcf_record: pointer to the fcf record.
1647 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1648 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1649 *
1650 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1651 * enabled.
1652 **/
1653static void
1654lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1655 struct fcf_record *fcf_record,
1656 uint16_t vlan_id,
1657 uint16_t next_fcf_index)
1658{
1659 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1660 "2764 READ_FCF_RECORD:\n"
1661 "\tFCF_Index : x%x\n"
1662 "\tFCF_Avail : x%x\n"
1663 "\tFCF_Valid : x%x\n"
1664 "\tFIP_Priority : x%x\n"
1665 "\tMAC_Provider : x%x\n"
1666 "\tLowest VLANID : x%x\n"
1667 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1668 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1669 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1670 "\tNext_FCF_Index: x%x\n",
1671 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1672 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1673 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1674 fcf_record->fip_priority,
1675 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1676 vlan_id,
1677 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1678 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1679 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1680 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1681 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1682 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1683 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1684 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1685 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1686 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1687 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1688 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1689 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1690 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1691 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1692 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1693 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1694 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1695 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1696 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1697 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1698 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1699 next_fcf_index);
1700}
1701
1702/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001703 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1704 * @phba: pointer to lpfc hba data structure.
1705 * @fcf_rec: pointer to an existing FCF record.
1706 * @new_fcf_record: pointer to a new FCF record.
1707 * @new_vlan_id: vlan id from the new FCF record.
1708 *
1709 * This function performs matching test of a new FCF record against an existing
1710 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1711 * will not be used as part of the FCF record matching criteria.
1712 *
1713 * Returns true if all the fields matching, otherwise returns false.
1714 */
1715static bool
1716lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1717 struct lpfc_fcf_rec *fcf_rec,
1718 struct fcf_record *new_fcf_record,
1719 uint16_t new_vlan_id)
1720{
1721 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1722 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1723 return false;
1724 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1725 return false;
1726 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1727 return false;
1728 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1729 return false;
1730 return true;
1731}
1732
1733/**
James Smart0c9ab6f2010-02-26 14:15:57 -05001734 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
1735 * @phba: pointer to lpfc hba data structure.
1736 * @mboxq: pointer to mailbox object.
1737 *
1738 * This function iterates through all the fcf records available in
1739 * HBA and chooses the optimal FCF record for discovery. After finding
1740 * the FCF for discovery it registers the FCF record and kicks start
1741 * discovery.
1742 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
1743 * use an FCF record which matches fabric name and mac address of the
1744 * currently used FCF record.
1745 * If the driver supports only one FCF, it will try to use the FCF record
1746 * used by BOOT_BIOS.
1747 */
1748void
1749lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1750{
1751 struct fcf_record *new_fcf_record;
1752 uint32_t boot_flag, addr_mode;
1753 uint16_t fcf_index, next_fcf_index;
1754 struct lpfc_fcf_rec *fcf_rec = NULL;
1755 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04001756 uint32_t seed;
1757 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001758 int rc;
1759
1760 /* If there is pending FCoE event restart FCF table scan */
1761 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1762 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1763 return;
1764 }
1765
1766 /* Parse the FCF record from the non-embedded mailbox command */
1767 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
1768 &next_fcf_index);
1769 if (!new_fcf_record) {
1770 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1771 "2765 Mailbox command READ_FCF_RECORD "
1772 "failed to retrieve a FCF record.\n");
1773 /* Let next new FCF event trigger fast failover */
1774 spin_lock_irq(&phba->hbalock);
1775 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1776 spin_unlock_irq(&phba->hbalock);
1777 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1778 return;
1779 }
1780
1781 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05001782 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1783 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05001784
1785 /* Log the FCF record information if turned on */
1786 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
1787 next_fcf_index);
1788
James Smart6fb120a2009-05-22 14:52:59 -04001789 /*
1790 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05001791 * read the next entry; otherwise, this is an eligible FCF
1792 * record for round robin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04001793 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001794 if (!rc) {
1795 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smartdbb6b3a2010-06-08 18:31:37 -04001796 "2781 FCF record (x%x) failed FCF "
James Smart0c9ab6f2010-02-26 14:15:57 -05001797 "connection list check, fcf_avail:x%x, "
1798 "fcf_valid:x%x\n",
1799 bf_get(lpfc_fcf_record_fcf_index,
1800 new_fcf_record),
1801 bf_get(lpfc_fcf_record_fcf_avail,
1802 new_fcf_record),
1803 bf_get(lpfc_fcf_record_fcf_valid,
1804 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04001805 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
1806 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
1807 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
1808 /*
1809 * In case the current in-use FCF record becomes
1810 * invalid/unavailable during FCF discovery that
1811 * was not triggered by fast FCF failover process,
1812 * treat it as fast FCF failover.
1813 */
1814 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
1815 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
1816 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1817 "2835 Invalid in-use FCF "
1818 "record (x%x) reported, "
1819 "entering fast FCF failover "
1820 "mode scanning.\n",
1821 phba->fcf.current_rec.fcf_indx);
1822 spin_lock_irq(&phba->hbalock);
1823 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
1824 spin_unlock_irq(&phba->hbalock);
1825 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1826 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
1827 LPFC_FCOE_FCF_GET_FIRST);
1828 return;
1829 }
1830 }
James Smart6fb120a2009-05-22 14:52:59 -04001831 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001832 } else {
1833 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1834 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
1835 if (rc)
1836 goto read_next_fcf;
1837 }
1838
James Smart6fb120a2009-05-22 14:52:59 -04001839 /*
1840 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05001841 * FCF record for the discovery. The condition that a rescan
1842 * matches the in-use FCF record: fabric name, switch name, mac
1843 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04001844 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001845 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001846 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04001847 if (lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
1848 new_fcf_record, vlan_id)) {
James Smart6fb120a2009-05-22 14:52:59 -04001849 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smartecfd03c2010-02-12 14:41:27 -05001850 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
1851 /* Stop FCF redisc wait timer if pending */
1852 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
1853 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1854 /* If in fast failover, mark it's completed */
James Smartfc2b9892010-02-26 14:15:29 -05001855 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV |
James Smart0c9ab6f2010-02-26 14:15:57 -05001856 FCF_DISCOVERY);
1857 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04001858 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1859 "2836 The new FCF record (x%x) "
1860 "matches the in-use FCF record "
1861 "(x%x)\n",
1862 phba->fcf.current_rec.fcf_indx,
1863 bf_get(lpfc_fcf_record_fcf_index,
1864 new_fcf_record));
James Smart6fb120a2009-05-22 14:52:59 -04001865 goto out;
1866 }
James Smartecfd03c2010-02-12 14:41:27 -05001867 /*
1868 * Read next FCF record from HBA searching for the matching
1869 * with in-use record only if not during the fast failover
1870 * period. In case of fast failover period, it shall try to
1871 * determine whether the FCF record just read should be the
1872 * next candidate.
1873 */
1874 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001875 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001876 goto read_next_fcf;
1877 }
James Smart6fb120a2009-05-22 14:52:59 -04001878 }
James Smartecfd03c2010-02-12 14:41:27 -05001879 /*
1880 * Update on failover FCF record only if it's in FCF fast-failover
1881 * period; otherwise, update on current FCF record.
1882 */
James Smartfc2b9892010-02-26 14:15:29 -05001883 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1884 fcf_rec = &phba->fcf.failover_rec;
1885 else
James Smartecfd03c2010-02-12 14:41:27 -05001886 fcf_rec = &phba->fcf.current_rec;
1887
James Smart6fb120a2009-05-22 14:52:59 -04001888 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1889 /*
James Smartecfd03c2010-02-12 14:41:27 -05001890 * If the driver FCF record does not have boot flag
1891 * set and new hba fcf record has boot flag set, use
1892 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001893 */
James Smartecfd03c2010-02-12 14:41:27 -05001894 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
1895 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04001896 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1897 "2837 Update current FCF record "
1898 "(x%x) with new FCF record (x%x)\n",
1899 fcf_rec->fcf_indx,
1900 bf_get(lpfc_fcf_record_fcf_index,
1901 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05001902 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1903 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05001904 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001905 goto read_next_fcf;
1906 }
1907 /*
James Smartecfd03c2010-02-12 14:41:27 -05001908 * If the driver FCF record has boot flag set and the
1909 * new hba FCF record does not have boot flag, read
1910 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04001911 */
James Smartecfd03c2010-02-12 14:41:27 -05001912 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001913 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001914 goto read_next_fcf;
1915 }
1916 /*
James Smartecfd03c2010-02-12 14:41:27 -05001917 * If the new hba FCF record has lower priority value
1918 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04001919 */
James Smartfc2b9892010-02-26 14:15:29 -05001920 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04001921 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04001922 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1923 "2838 Update current FCF record "
1924 "(x%x) with new FCF record (x%x)\n",
1925 fcf_rec->fcf_indx,
1926 bf_get(lpfc_fcf_record_fcf_index,
1927 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05001928 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1929 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04001930 /* Reset running random FCF selection count */
1931 phba->fcf.eligible_fcf_cnt = 1;
1932 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
1933 /* Update running random FCF selection count */
1934 phba->fcf.eligible_fcf_cnt++;
1935 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
1936 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04001937 if (select_new_fcf) {
1938 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1939 "2839 Update current FCF record "
1940 "(x%x) with new FCF record (x%x)\n",
1941 fcf_rec->fcf_indx,
1942 bf_get(lpfc_fcf_record_fcf_index,
1943 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04001944 /* Choose the new FCF by random selection */
1945 __lpfc_update_fcf_record(phba, fcf_rec,
1946 new_fcf_record,
1947 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04001948 }
James Smart6fb120a2009-05-22 14:52:59 -04001949 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001950 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001951 goto read_next_fcf;
1952 }
1953 /*
James Smartecfd03c2010-02-12 14:41:27 -05001954 * This is the first suitable FCF record, choose this record for
1955 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001956 */
James Smartecfd03c2010-02-12 14:41:27 -05001957 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04001958 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1959 "2840 Update current FCF record "
1960 "with initial FCF record (x%x)\n",
1961 bf_get(lpfc_fcf_record_fcf_index,
1962 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05001963 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1964 addr_mode, vlan_id, (boot_flag ?
1965 BOOT_ENABLE : 0));
1966 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04001967 /* Setup initial running random FCF selection count */
1968 phba->fcf.eligible_fcf_cnt = 1;
1969 /* Seeding the random number generator for random selection */
1970 seed = (uint32_t)(0xFFFFFFFF & jiffies);
1971 srandom32(seed);
James Smart6fb120a2009-05-22 14:52:59 -04001972 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001973 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001974 goto read_next_fcf;
1975
1976read_next_fcf:
1977 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05001978 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
1979 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
1980 /*
1981 * Case of FCF fast failover scan
1982 */
1983
1984 /*
1985 * It has not found any suitable FCF record, cancel
1986 * FCF scan inprogress, and do nothing
1987 */
1988 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001989 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1990 "2782 No suitable FCF record "
1991 "found during this round of "
1992 "post FCF rediscovery scan: "
1993 "fcf_evt_tag:x%x, fcf_index: "
1994 "x%x\n",
1995 phba->fcoe_eventtag_at_fcf_scan,
1996 bf_get(lpfc_fcf_record_fcf_index,
1997 new_fcf_record));
1998 /*
1999 * Let next new FCF event trigger fast
2000 * failover
2001 */
2002 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002003 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05002004 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002005 return;
2006 }
2007 /*
2008 * It has found a suitable FCF record that is not
2009 * the same as in-use FCF record, unregister the
2010 * in-use FCF record, replace the in-use FCF record
2011 * with the new FCF record, mark FCF fast failover
2012 * completed, and then start register the new FCF
2013 * record.
2014 */
2015
James Smart0c9ab6f2010-02-26 14:15:57 -05002016 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002017 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002018
2019 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002020 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2021 "2842 Replace the current in-use "
2022 "FCF record (x%x) with failover FCF "
2023 "record (x%x)\n",
2024 phba->fcf.current_rec.fcf_indx,
2025 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002026 memcpy(&phba->fcf.current_rec,
2027 &phba->fcf.failover_rec,
2028 sizeof(struct lpfc_fcf_rec));
2029 /* mark the FCF fast failover completed */
James Smart0c9ab6f2010-02-26 14:15:57 -05002030 spin_lock_irq(&phba->hbalock);
2031 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2032 spin_unlock_irq(&phba->hbalock);
2033 /*
2034 * Set up the initial registered FCF index for FLOGI
2035 * round robin FCF failover.
2036 */
2037 phba->fcf.fcf_rr_init_indx =
2038 phba->fcf.failover_rec.fcf_indx;
James Smartecfd03c2010-02-12 14:41:27 -05002039 /* Register to the new FCF record */
2040 lpfc_register_fcf(phba);
2041 } else {
2042 /*
2043 * In case of transaction period to fast FCF failover,
2044 * do nothing when search to the end of the FCF table.
2045 */
2046 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2047 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2048 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002049
2050 if (phba->fcf.fcf_flag & FCF_IN_USE) {
2051 /*
2052 * In case the current in-use FCF record no
2053 * longer existed during FCF discovery that
2054 * was not triggered by fast FCF failover
2055 * process, treat it as fast FCF failover.
2056 */
2057 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2058 "2841 In-use FCF record (x%x) "
2059 "not reported, entering fast "
2060 "FCF failover mode scanning.\n",
2061 phba->fcf.current_rec.fcf_indx);
2062 spin_lock_irq(&phba->hbalock);
2063 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2064 spin_unlock_irq(&phba->hbalock);
2065 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2066 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2067 LPFC_FCOE_FCF_GET_FIRST);
2068 return;
2069 }
2070
James Smartecfd03c2010-02-12 14:41:27 -05002071 /*
2072 * Otherwise, initial scan or post linkdown rescan,
James Smart0c9ab6f2010-02-26 14:15:57 -05002073 * register with the best FCF record found so far
2074 * through the FCF scanning process.
James Smartecfd03c2010-02-12 14:41:27 -05002075 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002076
2077 /* mark the initial FCF discovery completed */
2078 spin_lock_irq(&phba->hbalock);
2079 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
2080 spin_unlock_irq(&phba->hbalock);
2081 /*
2082 * Set up the initial registered FCF index for FLOGI
2083 * round robin FCF failover
2084 */
2085 phba->fcf.fcf_rr_init_indx =
2086 phba->fcf.current_rec.fcf_indx;
2087 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002088 lpfc_register_fcf(phba);
2089 }
2090 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002091 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002092 return;
2093
2094out:
2095 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2096 lpfc_register_fcf(phba);
2097
2098 return;
2099}
2100
2101/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002102 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf round robin read_fcf mbox cmpl hdler
2103 * @phba: pointer to lpfc hba data structure.
2104 * @mboxq: pointer to mailbox object.
2105 *
2106 * This is the callback function for FLOGI failure round robin FCF failover
2107 * read FCF record mailbox command from the eligible FCF record bmask for
2108 * performing the failover. If the FCF read back is not valid/available, it
2109 * fails through to retrying FLOGI to the currently registered FCF again.
2110 * Otherwise, if the FCF read back is valid and available, it will set the
2111 * newly read FCF record to the failover FCF record, unregister currently
2112 * registered FCF record, copy the failover FCF record to the current
2113 * FCF record, and then register the current FCF record before proceeding
2114 * to trying FLOGI on the new failover FCF.
2115 */
2116void
2117lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2118{
2119 struct fcf_record *new_fcf_record;
2120 uint32_t boot_flag, addr_mode;
2121 uint16_t next_fcf_index;
2122 uint16_t current_fcf_index;
2123 uint16_t vlan_id;
2124
2125 /* If link state is not up, stop the round robin failover process */
2126 if (phba->link_state < LPFC_LINK_UP) {
2127 spin_lock_irq(&phba->hbalock);
2128 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
2129 spin_unlock_irq(&phba->hbalock);
2130 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2131 return;
2132 }
2133
2134 /* Parse the FCF record from the non-embedded mailbox command */
2135 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2136 &next_fcf_index);
2137 if (!new_fcf_record) {
2138 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2139 "2766 Mailbox command READ_FCF_RECORD "
2140 "failed to retrieve a FCF record.\n");
2141 goto out;
2142 }
2143
2144 /* Get the needed parameters from FCF record */
2145 lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2146 &addr_mode, &vlan_id);
2147
2148 /* Log the FCF record information if turned on */
2149 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2150 next_fcf_index);
2151
2152 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002153 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2154 "2834 Update the current FCF record (x%x) "
2155 "with the next FCF record (x%x)\n",
2156 phba->fcf.failover_rec.fcf_indx,
2157 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002158 spin_lock_irq(&phba->hbalock);
2159 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2160 new_fcf_record, addr_mode, vlan_id,
2161 (boot_flag ? BOOT_ENABLE : 0));
2162 spin_unlock_irq(&phba->hbalock);
2163
2164 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2165
2166 /* Unregister the current in-use FCF record */
2167 lpfc_unregister_fcf(phba);
2168
2169 /* Replace in-use record with the new record */
2170 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2171 sizeof(struct lpfc_fcf_rec));
2172
2173 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2174 "2783 FLOGI round robin FCF failover from FCF "
James Smartdbb6b3a2010-06-08 18:31:37 -04002175 "(x%x) to FCF (x%x).\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002176 current_fcf_index,
2177 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
2178
2179out:
2180 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2181 lpfc_register_fcf(phba);
2182}
2183
2184/**
2185 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2186 * @phba: pointer to lpfc hba data structure.
2187 * @mboxq: pointer to mailbox object.
2188 *
2189 * This is the callback function of read FCF record mailbox command for
2190 * updating the eligible FCF bmask for FLOGI failure round robin FCF
2191 * failover when a new FCF event happened. If the FCF read back is
2192 * valid/available and it passes the connection list check, it updates
2193 * the bmask for the eligible FCF record for round robin failover.
2194 */
2195void
2196lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2197{
2198 struct fcf_record *new_fcf_record;
2199 uint32_t boot_flag, addr_mode;
2200 uint16_t fcf_index, next_fcf_index;
2201 uint16_t vlan_id;
2202 int rc;
2203
2204 /* If link state is not up, no need to proceed */
2205 if (phba->link_state < LPFC_LINK_UP)
2206 goto out;
2207
2208 /* If FCF discovery period is over, no need to proceed */
2209 if (phba->fcf.fcf_flag & FCF_DISCOVERY)
2210 goto out;
2211
2212 /* Parse the FCF record from the non-embedded mailbox command */
2213 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2214 &next_fcf_index);
2215 if (!new_fcf_record) {
2216 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2217 "2767 Mailbox command READ_FCF_RECORD "
2218 "failed to retrieve a FCF record.\n");
2219 goto out;
2220 }
2221
2222 /* Check the connection list for eligibility */
2223 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2224 &addr_mode, &vlan_id);
2225
2226 /* Log the FCF record information if turned on */
2227 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2228 next_fcf_index);
2229
2230 if (!rc)
2231 goto out;
2232
2233 /* Update the eligible FCF record index bmask */
2234 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2235 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
2236
2237out:
2238 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2239}
2240
2241/**
James Smart1c6834a2009-07-19 10:01:26 -04002242 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2243 * @phba: pointer to lpfc hba data structure.
2244 * @mboxq: pointer to mailbox data structure.
2245 *
2246 * This function handles completion of init vpi mailbox command.
2247 */
James Smart695a8142010-01-26 23:08:03 -05002248void
James Smart1c6834a2009-07-19 10:01:26 -04002249lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2250{
2251 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002252 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002253 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2254
James Smart1c6834a2009-07-19 10:01:26 -04002255 if (mboxq->u.mb.mbxStatus) {
2256 lpfc_printf_vlog(vport, KERN_ERR,
2257 LOG_MBOX,
2258 "2609 Init VPI mailbox failed 0x%x\n",
2259 mboxq->u.mb.mbxStatus);
2260 mempool_free(mboxq, phba->mbox_mem_pool);
2261 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2262 return;
2263 }
James Smart72100cc2010-02-12 14:43:01 -05002264 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002265 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002266 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002267
James Smart695a8142010-01-26 23:08:03 -05002268 /* If this port is physical port or FDISC is done, do reg_vpi */
2269 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2270 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2271 if (!ndlp)
2272 lpfc_printf_vlog(vport, KERN_ERR,
2273 LOG_DISCOVERY,
2274 "2731 Cannot find fabric "
2275 "controller node\n");
2276 else
2277 lpfc_register_new_vport(phba, vport, ndlp);
2278 mempool_free(mboxq, phba->mbox_mem_pool);
2279 return;
2280 }
2281
James Smart1c6834a2009-07-19 10:01:26 -04002282 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2283 lpfc_initial_fdisc(vport);
2284 else {
2285 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002286 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2287 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002288 }
James Smart695a8142010-01-26 23:08:03 -05002289 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002290 return;
2291}
2292
2293/**
James Smartecfd03c2010-02-12 14:41:27 -05002294 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2295 * @vport: pointer to lpfc_vport data structure.
2296 *
2297 * This function issue a init_vpi mailbox command to initialize
2298 * VPI for the vport.
2299 */
2300void
2301lpfc_issue_init_vpi(struct lpfc_vport *vport)
2302{
2303 LPFC_MBOXQ_t *mboxq;
2304 int rc;
2305
2306 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2307 if (!mboxq) {
2308 lpfc_printf_vlog(vport, KERN_ERR,
2309 LOG_MBOX, "2607 Failed to allocate "
2310 "init_vpi mailbox\n");
2311 return;
2312 }
2313 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2314 mboxq->vport = vport;
2315 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2316 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2317 if (rc == MBX_NOT_FINISHED) {
2318 lpfc_printf_vlog(vport, KERN_ERR,
2319 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2320 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2321 }
2322}
2323
2324/**
James Smart6fb120a2009-05-22 14:52:59 -04002325 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2326 * @phba: pointer to lpfc hba data structure.
2327 *
2328 * This function loops through the list of vports on the @phba and issues an
2329 * FDISC if possible.
2330 */
2331void
2332lpfc_start_fdiscs(struct lpfc_hba *phba)
2333{
2334 struct lpfc_vport **vports;
2335 int i;
2336
2337 vports = lpfc_create_vport_work_array(phba);
2338 if (vports != NULL) {
2339 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2340 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2341 continue;
2342 /* There are no vpi for this vport */
2343 if (vports[i]->vpi > phba->max_vpi) {
2344 lpfc_vport_set_state(vports[i],
2345 FC_VPORT_FAILED);
2346 continue;
2347 }
2348 if (phba->fc_topology == TOPOLOGY_LOOP) {
2349 lpfc_vport_set_state(vports[i],
2350 FC_VPORT_LINKDOWN);
2351 continue;
2352 }
James Smart1c6834a2009-07-19 10:01:26 -04002353 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002354 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002355 continue;
2356 }
James Smart6fb120a2009-05-22 14:52:59 -04002357 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2358 lpfc_initial_fdisc(vports[i]);
2359 else {
2360 lpfc_vport_set_state(vports[i],
2361 FC_VPORT_NO_FABRIC_SUPP);
2362 lpfc_printf_vlog(vports[i], KERN_ERR,
2363 LOG_ELS,
2364 "0259 No NPIV "
2365 "Fabric support\n");
2366 }
2367 }
2368 }
2369 lpfc_destroy_vport_work_array(phba, vports);
2370}
2371
2372void
2373lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2374{
2375 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2376 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002377 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002378
2379 if (mboxq->u.mb.mbxStatus) {
2380 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2381 "2018 REG_VFI mbxStatus error x%x "
2382 "HBA state x%x\n",
2383 mboxq->u.mb.mbxStatus, vport->port_state);
2384 if (phba->fc_topology == TOPOLOGY_LOOP) {
2385 /* FLOGI failed, use loop map to make discovery list */
2386 lpfc_disc_list_loopmap(vport);
2387 /* Start discovery */
2388 lpfc_disc_start(vport);
2389 goto fail_free_mem;
2390 }
2391 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2392 goto fail_free_mem;
2393 }
James Smartc8685952009-11-18 15:39:16 -05002394 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002395 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002396 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002397 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002398 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002399 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002400
2401 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2402 lpfc_start_fdiscs(phba);
2403 lpfc_do_scr_ns_plogi(phba, vport);
2404 }
2405
2406fail_free_mem:
2407 mempool_free(mboxq, phba->mbox_mem_pool);
2408 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2409 kfree(dmabuf);
2410 return;
2411}
2412
2413static void
James Smart2e0fef82007-06-17 19:56:36 -05002414lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002415{
James Smart6fb120a2009-05-22 14:52:59 -04002416 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002417 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002418 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002419
2420
2421 /* Check for error */
2422 if (mb->mbxStatus) {
2423 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002424 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2425 "0319 READ_SPARAM mbxStatus error x%x "
2426 "hba state x%x>\n",
2427 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002428 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002429 goto out;
2430 }
2431
James Smart2e0fef82007-06-17 19:56:36 -05002432 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002433 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05002434 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05002435 u64_to_wwn(phba->cfg_soft_wwnn,
2436 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04002437 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05002438 u64_to_wwn(phba->cfg_soft_wwpn,
2439 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05002440 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
2441 sizeof(vport->fc_nodename));
2442 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
2443 sizeof(vport->fc_portname));
2444 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2445 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2446 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2447 }
2448
dea31012005-04-17 16:05:31 -05002449 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2450 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002451 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002452 return;
2453
2454out:
2455 pmb->context1 = NULL;
2456 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2457 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002458 lpfc_issue_clear_la(phba, vport);
2459 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002460 return;
2461}
2462
2463static void
James Smart92d7f7b2007-06-17 19:56:38 -05002464lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05002465{
James Smart92d7f7b2007-06-17 19:56:38 -05002466 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002467 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002468 int i;
James Smart14691152006-12-02 13:34:28 -05002469 struct lpfc_dmabuf *mp;
2470 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002471 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002472
James Smart92d7f7b2007-06-17 19:56:38 -05002473 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002474 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05002475 case LA_1GHZ_LINK:
2476 phba->fc_linkspeed = LA_1GHZ_LINK;
2477 break;
2478 case LA_2GHZ_LINK:
2479 phba->fc_linkspeed = LA_2GHZ_LINK;
2480 break;
2481 case LA_4GHZ_LINK:
2482 phba->fc_linkspeed = LA_4GHZ_LINK;
2483 break;
2484 case LA_8GHZ_LINK:
2485 phba->fc_linkspeed = LA_8GHZ_LINK;
2486 break;
James Smartf4b4c682009-05-22 14:53:12 -04002487 case LA_10GHZ_LINK:
2488 phba->fc_linkspeed = LA_10GHZ_LINK;
2489 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002490 default:
2491 phba->fc_linkspeed = LA_UNKNW_LINK;
2492 break;
dea31012005-04-17 16:05:31 -05002493 }
2494
2495 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05002496 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05002497
2498 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05002499 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05002500
James Smart6a9c52c2009-10-02 15:16:51 -04002501 /* if npiv is enabled and this adapter supports npiv log
2502 * a message that npiv is not supported in this topology
2503 */
2504 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04002505 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2506 "1309 Link Up Event npiv not supported in loop "
2507 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002508 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05002509 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05002510 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002511
James Smart2e0fef82007-06-17 19:56:36 -05002512 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05002513 i = la->un.lilpBde64.tus.f.bdeSize;
2514
2515 if (i == 0) {
2516 phba->alpa_map[0] = 0;
2517 } else {
James Smarte8b62012007-08-02 11:10:09 -04002518 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05002519 int numalpa, j, k;
2520 union {
2521 uint8_t pamap[16];
2522 struct {
2523 uint32_t wd1;
2524 uint32_t wd2;
2525 uint32_t wd3;
2526 uint32_t wd4;
2527 } pa;
2528 } un;
2529 numalpa = phba->alpa_map[0];
2530 j = 0;
2531 while (j < numalpa) {
2532 memset(un.pamap, 0, 16);
2533 for (k = 1; j < numalpa; k++) {
2534 un.pamap[k - 1] =
2535 phba->alpa_map[j + 1];
2536 j++;
2537 if (k == 16)
2538 break;
2539 }
2540 /* Link Up Event ALPA map */
2541 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05002542 KERN_WARNING,
2543 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002544 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05002545 "ALPA map Data: x%x "
2546 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002547 un.pa.wd1, un.pa.wd2,
2548 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05002549 }
2550 }
2551 }
2552 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05002553 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04002554 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05002555 (phba->sli_rev == 3))
2556 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
2557 }
James Smart2e0fef82007-06-17 19:56:36 -05002558 vport->fc_myDID = phba->fc_pref_DID;
2559 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002560 }
James Smart92d7f7b2007-06-17 19:56:38 -05002561 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002562
2563 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05002564 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2565 if (!sparam_mbox)
2566 goto out;
2567
2568 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
2569 if (rc) {
2570 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2571 goto out;
2572 }
2573 sparam_mbox->vport = vport;
2574 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
2575 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
2576 if (rc == MBX_NOT_FINISHED) {
2577 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2578 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2579 kfree(mp);
2580 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2581 goto out;
dea31012005-04-17 16:05:31 -05002582 }
2583
James Smart6fb120a2009-05-22 14:52:59 -04002584 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2585 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2586 if (!cfglink_mbox)
2587 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05002588 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002589 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002590 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002591 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002592 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002593 if (rc == MBX_NOT_FINISHED) {
2594 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2595 goto out;
2596 }
2597 } else {
James Smart32b97932009-07-19 10:01:21 -04002598 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002599 /*
2600 * Add the driver's default FCF record at FCF index 0 now. This
2601 * is phase 1 implementation that support FCF index 0 and driver
2602 * defaults.
2603 */
James Smart45ed1192009-10-02 15:17:02 -04002604 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002605 fcf_record = kzalloc(sizeof(struct fcf_record),
2606 GFP_KERNEL);
2607 if (unlikely(!fcf_record)) {
2608 lpfc_printf_log(phba, KERN_ERR,
2609 LOG_MBOX | LOG_SLI,
2610 "2554 Could not allocate memmory for "
2611 "fcf record\n");
2612 rc = -ENODEV;
2613 goto out;
2614 }
2615
2616 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2617 LPFC_FCOE_FCF_DEF_INDEX);
2618 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2619 if (unlikely(rc)) {
2620 lpfc_printf_log(phba, KERN_ERR,
2621 LOG_MBOX | LOG_SLI,
2622 "2013 Could not manually add FCF "
2623 "record 0, status %d\n", rc);
2624 rc = -ENODEV;
2625 kfree(fcf_record);
2626 goto out;
2627 }
2628 kfree(fcf_record);
2629 }
2630 /*
2631 * The driver is expected to do FIP/FCF. Call the port
2632 * and get the FCF Table.
2633 */
James Smart32b97932009-07-19 10:01:21 -04002634 spin_lock_irq(&phba->hbalock);
2635 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2636 spin_unlock_irq(&phba->hbalock);
2637 return;
2638 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002639 /* This is the initial FCF discovery scan */
2640 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04002641 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002642 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2643 "2778 Start FCF table scan at linkup\n");
2644
2645 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2646 LPFC_FCOE_FCF_GET_FIRST);
2647 if (rc) {
2648 spin_lock_irq(&phba->hbalock);
2649 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
2650 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002651 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002652 }
dea31012005-04-17 16:05:31 -05002653 }
James Smart6fb120a2009-05-22 14:52:59 -04002654
2655 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002656out:
2657 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002658 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2659 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2660 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002661 lpfc_issue_clear_la(phba, vport);
2662 return;
dea31012005-04-17 16:05:31 -05002663}
2664
2665static void
James Smart84774a42008-08-24 21:50:06 -04002666lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002667{
dea31012005-04-17 16:05:31 -05002668 uint32_t control;
2669 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002670 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002671 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002672 if (phba->sli_rev <= LPFC_SLI_REV3) {
2673 control = readl(phba->HCregaddr);
2674 control |= HC_LAINT_ENA;
2675 writel(control, phba->HCregaddr);
2676 readl(phba->HCregaddr); /* flush */
2677 }
James Smart2e0fef82007-06-17 19:56:36 -05002678 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002679}
2680
James Smart84774a42008-08-24 21:50:06 -04002681static void
2682lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2683{
2684 lpfc_linkdown(phba);
2685 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002686 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002687 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2688}
2689
2690
dea31012005-04-17 16:05:31 -05002691/*
2692 * This routine handles processing a READ_LA mailbox
2693 * command upon completion. It is setup in the LPFC_MBOXQ
2694 * as the completion routine when the command is
2695 * handed off to the SLI layer.
2696 */
2697void
James Smart2e0fef82007-06-17 19:56:36 -05002698lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002699{
James Smart2e0fef82007-06-17 19:56:36 -05002700 struct lpfc_vport *vport = pmb->vport;
2701 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002702 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002703 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002704 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2705
James Smart0d2b6b82008-06-14 22:52:47 -04002706 /* Unblock ELS traffic */
2707 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002708 /* Check for error */
2709 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002710 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002711 "1307 READ_LA mbox error x%x state x%x\n",
2712 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002713 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002714 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002715 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2716 }
2717
James Smart04c68492009-05-22 14:52:52 -04002718 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002719
2720 memcpy(&phba->alpa_map[0], mp->virt, 128);
2721
James Smart2e0fef82007-06-17 19:56:36 -05002722 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002723 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002724 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002725 else
James Smart2e0fef82007-06-17 19:56:36 -05002726 vport->fc_flag &= ~FC_BYPASSED_MODE;
2727 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002728
James Smart0c287582009-06-10 17:22:56 -04002729 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002730 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002731 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002732 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002733 if (phba->fc_eventTag != 0)
2734 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002735 }
dea31012005-04-17 16:05:31 -05002736
2737 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05002738 spin_lock_irq(&phba->hbalock);
James Smart84774a42008-08-24 21:50:06 -04002739 if (la->mm)
2740 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2741 else
2742 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05002743 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002744
James Smart4d9ab992009-10-02 15:16:39 -04002745 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002746 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002747 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002748 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002749 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002750 "1306 Link Up Event in loop back mode "
2751 "x%x received Data: x%x x%x x%x x%x\n",
2752 la->eventTag, phba->fc_eventTag,
2753 la->granted_AL_PA, la->UlnkSpeed,
2754 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002755 } else {
2756 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002757 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002758 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002759 la->eventTag, phba->fc_eventTag,
2760 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002761 phba->alpa_map[0],
2762 la->mm, la->fa,
2763 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002764 }
James Smart92d7f7b2007-06-17 19:56:38 -05002765 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002766 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002767 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002768 if (phba->link_flag & LS_LOOPBACK_MODE) {
2769 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2770 "1308 Link Down Event in loop back mode "
2771 "x%x received "
2772 "Data: x%x x%x x%x\n",
2773 la->eventTag, phba->fc_eventTag,
2774 phba->pport->port_state, vport->fc_flag);
2775 }
2776 else {
2777 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002778 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002779 "Data: x%x x%x x%x x%x x%x\n",
2780 la->eventTag, phba->fc_eventTag,
2781 phba->pport->port_state, vport->fc_flag,
2782 la->mm, la->fa);
2783 }
2784 lpfc_mbx_issue_link_down(phba);
2785 }
2786 if (la->mm && la->attType == AT_LINK_UP) {
2787 if (phba->link_state != LPFC_LINK_DOWN) {
2788 phba->fc_stat.LinkDown++;
2789 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2790 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002791 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002792 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002793 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002794 lpfc_mbx_issue_link_down(phba);
2795 } else
2796 lpfc_enable_la(phba);
2797
2798 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2799 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2800 "Data: x%x x%x x%x\n",
2801 la->eventTag, phba->fc_eventTag,
2802 phba->pport->port_state, vport->fc_flag);
2803 /*
2804 * The cmnd that triggered this will be waiting for this
2805 * signal.
2806 */
2807 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2808 if (phba->wait_4_mlo_maint_flg) {
2809 phba->wait_4_mlo_maint_flg = 0;
2810 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002811 }
James Smart84774a42008-08-24 21:50:06 -04002812 }
2813
2814 if (la->fa) {
2815 if (la->mm)
2816 lpfc_issue_clear_la(phba, vport);
2817 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2818 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002819 }
2820
2821lpfc_mbx_cmpl_read_la_free_mbuf:
2822 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2823 kfree(mp);
2824 mempool_free(pmb, phba->mbox_mem_pool);
2825 return;
2826}
2827
2828/*
2829 * This routine handles processing a REG_LOGIN mailbox
2830 * command upon completion. It is setup in the LPFC_MBOXQ
2831 * as the completion routine when the command is
2832 * handed off to the SLI layer.
2833 */
2834void
James Smart2e0fef82007-06-17 19:56:36 -05002835lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002836{
James Smart2e0fef82007-06-17 19:56:36 -05002837 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002838 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002839 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04002840 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002841
dea31012005-04-17 16:05:31 -05002842 pmb->context1 = NULL;
2843
James Smartffc95492010-06-07 15:23:17 -04002844 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
2845 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
2846
2847 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
2848 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
2849 /* We rcvd a rscn after issuing this
2850 * mbox reg login, we may have cycled
2851 * back through the state and be
2852 * back at reg login state so this
2853 * mbox needs to be ignored becase
2854 * there is another reg login in
2855 * proccess.
2856 */
2857 spin_lock_irq(shost->host_lock);
2858 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
2859 spin_unlock_irq(shost->host_lock);
2860 if (phba->sli_rev == LPFC_SLI_REV4)
2861 lpfc_sli4_free_rpi(phba,
2862 pmb->u.mb.un.varRegLogin.rpi);
2863
2864 } else
2865 /* Good status, call state machine */
2866 lpfc_disc_state_machine(vport, ndlp, pmb,
2867 NLP_EVT_CMPL_REG_LOGIN);
2868
dea31012005-04-17 16:05:31 -05002869 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2870 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002871 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002872 /* decrement the node reference count held for this callback
2873 * function.
2874 */
James Smart329f9bc2007-04-25 09:53:01 -04002875 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002876
2877 return;
2878}
2879
James Smart92d7f7b2007-06-17 19:56:38 -05002880static void
2881lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2882{
James Smart04c68492009-05-22 14:52:52 -04002883 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002884 struct lpfc_vport *vport = pmb->vport;
2885 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2886
2887 switch (mb->mbxStatus) {
2888 case 0x0011:
2889 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04002890 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2891 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2892 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002893 break;
James Smart78730cf2010-04-06 15:06:30 -04002894 /* If VPI is busy, reset the HBA */
2895 case 0x9700:
2896 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2897 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
2898 vport->vpi, mb->mbxStatus);
2899 if (!(phba->pport->load_flag & FC_UNLOADING))
2900 lpfc_workq_post_event(phba, NULL, NULL,
2901 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05002902 }
James Smart72100cc2010-02-12 14:43:01 -05002903 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002904 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05002905 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002906 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002907 vport->unreg_vpi_cmpl = VPORT_OK;
2908 mempool_free(pmb, phba->mbox_mem_pool);
2909 /*
2910 * This shost reference might have been taken at the beginning of
2911 * lpfc_vport_delete()
2912 */
James Smart1c6f4ef52009-11-18 15:40:49 -05002913 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05002914 scsi_host_put(shost);
2915}
2916
James Smartd7c255b2008-08-24 21:50:00 -04002917int
James Smart92d7f7b2007-06-17 19:56:38 -05002918lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2919{
2920 struct lpfc_hba *phba = vport->phba;
2921 LPFC_MBOXQ_t *mbox;
2922 int rc;
2923
2924 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2925 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002926 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002927
2928 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2929 mbox->vport = vport;
2930 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002931 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002932 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002933 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2934 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002935 mempool_free(mbox, phba->mbox_mem_pool);
2936 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002937 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002938 }
James Smartd7c255b2008-08-24 21:50:00 -04002939 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002940}
2941
2942static void
2943lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2944{
2945 struct lpfc_vport *vport = pmb->vport;
2946 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002947 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002948
2949 switch (mb->mbxStatus) {
2950 case 0x0011:
2951 case 0x9601:
2952 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002953 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2954 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2955 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002956 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2957 spin_lock_irq(shost->host_lock);
2958 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2959 spin_unlock_irq(shost->host_lock);
2960 vport->fc_myDID = 0;
2961 goto out;
2962 }
James Smart92d7f7b2007-06-17 19:56:38 -05002963
James Smart72100cc2010-02-12 14:43:01 -05002964 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002965 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002966 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002967 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002968 vport->num_disc_nodes = 0;
2969 /* go thru NPR list and issue ELS PLOGIs */
2970 if (vport->fc_npr_cnt)
2971 lpfc_els_disc_plogi(vport);
2972
2973 if (!vport->num_disc_nodes) {
2974 spin_lock_irq(shost->host_lock);
2975 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2976 spin_unlock_irq(shost->host_lock);
2977 lpfc_can_disctmo(vport);
2978 }
2979 vport->port_state = LPFC_VPORT_READY;
2980
2981out:
2982 mempool_free(pmb, phba->mbox_mem_pool);
2983 return;
2984}
2985
James Smart21e9a0a2009-05-22 14:53:21 -04002986/**
2987 * lpfc_create_static_vport - Read HBA config region to create static vports.
2988 * @phba: pointer to lpfc hba data structure.
2989 *
2990 * This routine issue a DUMP mailbox command for config region 22 to get
2991 * the list of static vports to be created. The function create vports
2992 * based on the information returned from the HBA.
2993 **/
2994void
2995lpfc_create_static_vport(struct lpfc_hba *phba)
2996{
2997 LPFC_MBOXQ_t *pmb = NULL;
2998 MAILBOX_t *mb;
2999 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04003000 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003001 struct fc_vport_identifiers vport_id;
3002 struct fc_vport *new_fc_vport;
3003 struct Scsi_Host *shost;
3004 struct lpfc_vport *vport;
3005 uint16_t offset = 0;
3006 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003007 struct lpfc_dmabuf *mp;
3008 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003009
3010 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3011 if (!pmb) {
3012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3013 "0542 lpfc_create_static_vport failed to"
3014 " allocate mailbox memory\n");
3015 return;
3016 }
3017
3018 mb = &pmb->u.mb;
3019
3020 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3021 if (!vport_info) {
3022 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3023 "0543 lpfc_create_static_vport failed to"
3024 " allocate vport_info\n");
3025 mempool_free(pmb, phba->mbox_mem_pool);
3026 return;
3027 }
3028
3029 vport_buff = (uint8_t *) vport_info;
3030 do {
James Smart1c6834a2009-07-19 10:01:26 -04003031 if (lpfc_dump_static_vport(phba, pmb, offset))
3032 goto out;
3033
James Smart21e9a0a2009-05-22 14:53:21 -04003034 pmb->vport = phba->pport;
3035 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
3036
3037 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
3038 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3039 "0544 lpfc_create_static_vport failed to"
3040 " issue dump mailbox command ret 0x%x "
3041 "status 0x%x\n",
3042 rc, mb->mbxStatus);
3043 goto out;
3044 }
3045
James Smart1c6834a2009-07-19 10:01:26 -04003046 if (phba->sli_rev == LPFC_SLI_REV4) {
3047 byte_count = pmb->u.mqe.un.mb_words[5];
3048 mp = (struct lpfc_dmabuf *) pmb->context2;
3049 if (byte_count > sizeof(struct static_vport_info) -
3050 offset)
3051 byte_count = sizeof(struct static_vport_info)
3052 - offset;
3053 memcpy(vport_buff + offset, mp->virt, byte_count);
3054 offset += byte_count;
3055 } else {
3056 if (mb->un.varDmp.word_cnt >
3057 sizeof(struct static_vport_info) - offset)
3058 mb->un.varDmp.word_cnt =
3059 sizeof(struct static_vport_info)
3060 - offset;
3061 byte_count = mb->un.varDmp.word_cnt;
3062 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3063 vport_buff + offset,
3064 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003065
James Smart1c6834a2009-07-19 10:01:26 -04003066 offset += byte_count;
3067 }
James Smart21e9a0a2009-05-22 14:53:21 -04003068
James Smart1c6834a2009-07-19 10:01:26 -04003069 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003070 offset < sizeof(struct static_vport_info));
3071
3072
3073 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3074 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3075 != VPORT_INFO_REV)) {
3076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3077 "0545 lpfc_create_static_vport bad"
3078 " information header 0x%x 0x%x\n",
3079 le32_to_cpu(vport_info->signature),
3080 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3081
3082 goto out;
3083 }
3084
3085 shost = lpfc_shost_from_vport(phba->pport);
3086
3087 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3088 memset(&vport_id, 0, sizeof(vport_id));
3089 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3090 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3091 if (!vport_id.port_name || !vport_id.node_name)
3092 continue;
3093
3094 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3095 vport_id.vport_type = FC_PORTTYPE_NPIV;
3096 vport_id.disable = false;
3097 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3098
3099 if (!new_fc_vport) {
3100 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3101 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003102 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003103 continue;
3104 }
3105
3106 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3107 vport->vport_flag |= STATIC_VPORT;
3108 }
3109
3110out:
James Smart21e9a0a2009-05-22 14:53:21 -04003111 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04003112 if (rc != MBX_TIMEOUT) {
3113 if (pmb->context2) {
3114 mp = (struct lpfc_dmabuf *) pmb->context2;
3115 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3116 kfree(mp);
3117 }
James Smart21e9a0a2009-05-22 14:53:21 -04003118 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003119 }
James Smart21e9a0a2009-05-22 14:53:21 -04003120
3121 return;
3122}
3123
dea31012005-04-17 16:05:31 -05003124/*
3125 * This routine handles processing a Fabric REG_LOGIN mailbox
3126 * command upon completion. It is setup in the LPFC_MBOXQ
3127 * as the completion routine when the command is
3128 * handed off to the SLI layer.
3129 */
3130void
James Smart2e0fef82007-06-17 19:56:36 -05003131lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003132{
James Smart92d7f7b2007-06-17 19:56:38 -05003133 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003134 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003135 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003136 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003137
James Smart549e55c2007-08-02 11:09:51 -04003138 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003139 pmb->context1 = NULL;
3140 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003141 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003142 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3143 "0258 Register Fabric login error: 0x%x\n",
3144 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003145 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3146 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003147 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003148
James Smart92d7f7b2007-06-17 19:56:38 -05003149 if (phba->fc_topology == TOPOLOGY_LOOP) {
3150 /* FLOGI failed, use loop map to make discovery list */
3151 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003152
James Smart92d7f7b2007-06-17 19:56:38 -05003153 /* Start discovery */
3154 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003155 /* Decrement the reference count to ndlp after the
3156 * reference to the ndlp are done.
3157 */
3158 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003159 return;
3160 }
3161
3162 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003163 /* Decrement the reference count to ndlp after the reference
3164 * to the ndlp are done.
3165 */
3166 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003167 return;
3168 }
3169
dea31012005-04-17 16:05:31 -05003170 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003171 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003172 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003173 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003174
James Smart2e0fef82007-06-17 19:56:36 -05003175 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003176 /* when physical port receive logo donot start
3177 * vport discovery */
3178 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3179 lpfc_start_fdiscs(phba);
3180 else
3181 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smart92d7f7b2007-06-17 19:56:38 -05003182 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003183 }
3184
3185 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3186 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003187 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003188
3189 /* Drop the reference count from the mbox at the end after
3190 * all the current reference to the ndlp have been done.
3191 */
3192 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003193 return;
3194}
3195
3196/*
3197 * This routine handles processing a NameServer REG_LOGIN mailbox
3198 * command upon completion. It is setup in the LPFC_MBOXQ
3199 * as the completion routine when the command is
3200 * handed off to the SLI layer.
3201 */
3202void
James Smart2e0fef82007-06-17 19:56:36 -05003203lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003204{
James Smart21e9a0a2009-05-22 14:53:21 -04003205 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003206 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3207 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3208 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003209
3210 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003211out:
James Smart21e9a0a2009-05-22 14:53:21 -04003212 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3213 "0260 Register NameServer error: 0x%x\n",
3214 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003215 /* decrement the node reference count held for this
3216 * callback function.
3217 */
James Smart329f9bc2007-04-25 09:53:01 -04003218 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003219 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3220 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003221 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003222
3223 /* If no other thread is using the ndlp, free it */
3224 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003225
James Smart92d7f7b2007-06-17 19:56:38 -05003226 if (phba->fc_topology == TOPOLOGY_LOOP) {
3227 /*
3228 * RegLogin failed, use loop map to make discovery
3229 * list
3230 */
3231 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003232
James Smart92d7f7b2007-06-17 19:56:38 -05003233 /* Start discovery */
3234 lpfc_disc_start(vport);
3235 return;
3236 }
3237 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003238 return;
3239 }
3240
3241 pmb->context1 = NULL;
3242
dea31012005-04-17 16:05:31 -05003243 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003244 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003245 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003246 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003247
James Smart2e0fef82007-06-17 19:56:36 -05003248 if (vport->port_state < LPFC_VPORT_READY) {
3249 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003250 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3251 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3252 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3253 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3254 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3255
3256 /* Issue SCR just before NameServer GID_FT Query */
3257 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003258 }
3259
James Smart2e0fef82007-06-17 19:56:36 -05003260 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003261 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003262 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003263 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003264 goto out;
dea31012005-04-17 16:05:31 -05003265 }
3266
James Smartfa4066b2008-01-11 01:53:27 -05003267 /* decrement the node reference count held for this
3268 * callback function.
3269 */
James Smart329f9bc2007-04-25 09:53:01 -04003270 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003271 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3272 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003273 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003274
3275 return;
3276}
3277
3278static void
James Smart2e0fef82007-06-17 19:56:36 -05003279lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003280{
James Smart2e0fef82007-06-17 19:56:36 -05003281 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3282 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003283 struct lpfc_rport_data *rdata;
3284 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003285 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003286
3287 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003288 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3289 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003290 rport_ids.port_id = ndlp->nlp_DID;
3291 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003292
James Smart329f9bc2007-04-25 09:53:01 -04003293 /*
3294 * We leave our node pointer in rport->dd_data when we unregister a
3295 * FCP target port. But fc_remote_port_add zeros the space to which
3296 * rport->dd_data points. So, if we're reusing a previously
3297 * registered port, drop the reference that we took the last time we
3298 * registered the port.
3299 */
3300 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003301 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003302 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003303
3304 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3305 "rport add: did:x%x flg:x%x type x%x",
3306 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3307
James Smart2e0fef82007-06-17 19:56:36 -05003308 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003309 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003310 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3311 "Warning: fc_remote_port_add failed\n");
3312 return;
3313 }
3314
3315 /* initialize static port data */
3316 rport->maxframe_size = ndlp->nlp_maxframe;
3317 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003318 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003319 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003320
3321 if (ndlp->nlp_type & NLP_FCP_TARGET)
3322 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3323 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3324 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3325
3326
3327 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3328 fc_remote_port_rolechg(rport, rport_ids.roles);
3329
James Smart071fbd3d2006-04-15 11:53:20 -04003330 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003331 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003332 ndlp->nlp_sid = rport->scsi_target_id;
3333 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003334 return;
3335}
3336
3337static void
James Smart2e0fef82007-06-17 19:56:36 -05003338lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003339{
3340 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003341
James Smart858c9f62007-06-17 19:56:39 -05003342 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3343 "rport delete: did:x%x flg:x%x type x%x",
3344 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3345
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003346 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003347
3348 return;
3349}
3350
James Smartde0c5b32007-04-25 09:52:27 -04003351static void
James Smart2e0fef82007-06-17 19:56:36 -05003352lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003353{
James Smart2e0fef82007-06-17 19:56:36 -05003354 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3355
3356 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003357 switch (state) {
3358 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003359 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003360 break;
3361 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003362 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003363 break;
3364 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003365 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003366 break;
3367 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003368 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003369 break;
3370 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003371 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003372 break;
3373 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003374 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003375 break;
3376 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003377 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003378 break;
3379 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003380 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003381 break;
3382 }
James Smart2e0fef82007-06-17 19:56:36 -05003383 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003384}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003385
James Smartde0c5b32007-04-25 09:52:27 -04003386static void
James Smart2e0fef82007-06-17 19:56:36 -05003387lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003388 int old_state, int new_state)
3389{
James Smart2e0fef82007-06-17 19:56:36 -05003390 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3391
James Smartde0c5b32007-04-25 09:52:27 -04003392 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04003393 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3394 ndlp->nlp_type |= NLP_FC_NODE;
3395 }
3396 if (new_state == NLP_STE_MAPPED_NODE)
3397 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3398 if (new_state == NLP_STE_NPR_NODE)
3399 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003400
James Smartde0c5b32007-04-25 09:52:27 -04003401 /* Transport interface */
3402 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3403 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003404 vport->phba->nport_event_cnt++;
3405 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003406 }
dea31012005-04-17 16:05:31 -05003407
James Smartde0c5b32007-04-25 09:52:27 -04003408 if (new_state == NLP_STE_MAPPED_NODE ||
3409 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003410 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003411 /*
3412 * Tell the fc transport about the port, if we haven't
3413 * already. If we have, and it's a scsi entity, be
3414 * sure to unblock any attached scsi devices
3415 */
3416 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003417 }
James Smartea2151b2008-09-07 11:52:10 -04003418 if ((new_state == NLP_STE_MAPPED_NODE) &&
3419 (vport->stat_data_enabled)) {
3420 /*
3421 * A new target is discovered, if there is no buffer for
3422 * statistical data collection allocate buffer.
3423 */
3424 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3425 sizeof(struct lpfc_scsicmd_bkt),
3426 GFP_KERNEL);
3427
3428 if (!ndlp->lat_data)
3429 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3430 "0286 lpfc_nlp_state_cleanup failed to "
3431 "allocate statistical data buffer DID "
3432 "0x%x\n", ndlp->nlp_DID);
3433 }
James Smart858c9f62007-06-17 19:56:39 -05003434 /*
3435 * if we added to Mapped list, but the remote port
3436 * registration failed or assigned a target id outside
3437 * our presentable range - move the node to the
3438 * Unmapped List
3439 */
James Smartde0c5b32007-04-25 09:52:27 -04003440 if (new_state == NLP_STE_MAPPED_NODE &&
3441 (!ndlp->rport ||
3442 ndlp->rport->scsi_target_id == -1 ||
3443 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003444 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003445 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003446 spin_unlock_irq(shost->host_lock);
3447 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003448 }
James Smartde0c5b32007-04-25 09:52:27 -04003449}
3450
James Smart685f0bf2007-04-25 09:53:08 -04003451static char *
3452lpfc_nlp_state_name(char *buffer, size_t size, int state)
3453{
3454 static char *states[] = {
3455 [NLP_STE_UNUSED_NODE] = "UNUSED",
3456 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
3457 [NLP_STE_ADISC_ISSUE] = "ADISC",
3458 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
3459 [NLP_STE_PRLI_ISSUE] = "PRLI",
3460 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
3461 [NLP_STE_MAPPED_NODE] = "MAPPED",
3462 [NLP_STE_NPR_NODE] = "NPR",
3463 };
3464
James Smart311464e2007-08-02 11:10:37 -04003465 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04003466 strlcpy(buffer, states[state], size);
3467 else
3468 snprintf(buffer, size, "unknown (%d)", state);
3469 return buffer;
3470}
3471
James Smartde0c5b32007-04-25 09:52:27 -04003472void
James Smart2e0fef82007-06-17 19:56:36 -05003473lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3474 int state)
James Smartde0c5b32007-04-25 09:52:27 -04003475{
James Smart2e0fef82007-06-17 19:56:36 -05003476 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04003477 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04003478 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04003479
James Smarte8b62012007-08-02 11:10:09 -04003480 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3481 "0904 NPort state transition x%06x, %s -> %s\n",
3482 ndlp->nlp_DID,
3483 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
3484 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05003485
3486 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3487 "node statechg did:x%x old:%d ste:%d",
3488 ndlp->nlp_DID, old_state, state);
3489
James Smartde0c5b32007-04-25 09:52:27 -04003490 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04003491 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05003492 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003493 if (old_state == NLP_STE_UNMAPPED_NODE) {
3494 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
3495 ndlp->nlp_type &= ~NLP_FC_NODE;
3496 }
3497
James Smart685f0bf2007-04-25 09:53:08 -04003498 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05003499 spin_lock_irq(shost->host_lock);
3500 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3501 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003502 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05003503 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04003504
3505 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05003506 lpfc_nlp_counters(vport, state, 1);
3507 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04003508}
3509
3510void
James Smarte47c9092008-02-08 18:49:26 -05003511lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3512{
3513 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3514
3515 if (list_empty(&ndlp->nlp_listp)) {
3516 spin_lock_irq(shost->host_lock);
3517 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3518 spin_unlock_irq(shost->host_lock);
3519 }
3520}
3521
3522void
James Smart2e0fef82007-06-17 19:56:36 -05003523lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003524{
James Smart2e0fef82007-06-17 19:56:36 -05003525 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3526
James Smart0d2b6b82008-06-14 22:52:47 -04003527 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003528 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05003529 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3530 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003531 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05003532 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003533 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05003534 NLP_STE_UNUSED_NODE);
3535}
3536
Adrian Bunk4d9db012008-02-14 23:24:02 +02003537static void
James Smarte47c9092008-02-08 18:49:26 -05003538lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3539{
James Smart0d2b6b82008-06-14 22:52:47 -04003540 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003541 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3542 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3543 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3544 NLP_STE_UNUSED_NODE);
3545}
James Smart109f6ed2008-12-04 22:39:08 -05003546/**
James Smart3621a712009-04-06 18:47:14 -04003547 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05003548 * @vport: Pointer to Virtual Port object.
3549 * @ndlp: Pointer to FC node object.
3550 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04003551 *
3552 * This function is always called when node object need to be initialized.
3553 * It initializes all the fields of the node object. Although the reference
3554 * to phba from @ndlp can be obtained indirectly through it's reference to
3555 * @vport, a direct reference to phba is taken here by @ndlp. This is due
3556 * to the life-span of the @ndlp might go beyond the existence of @vport as
3557 * the final release of ndlp is determined by its reference count. And, the
3558 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05003559 **/
3560static inline void
3561lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3562 uint32_t did)
3563{
3564 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3565 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3566 init_timer(&ndlp->nlp_delayfunc);
3567 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3568 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3569 ndlp->nlp_DID = did;
3570 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04003571 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05003572 ndlp->nlp_sid = NLP_NO_SID;
3573 kref_init(&ndlp->kref);
3574 NLP_INT_NODE_ACT(ndlp);
3575 atomic_set(&ndlp->cmd_pending, 0);
3576 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
3577}
James Smarte47c9092008-02-08 18:49:26 -05003578
3579struct lpfc_nodelist *
3580lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3581 int state)
3582{
3583 struct lpfc_hba *phba = vport->phba;
3584 uint32_t did;
3585 unsigned long flags;
3586
3587 if (!ndlp)
3588 return NULL;
3589
3590 spin_lock_irqsave(&phba->ndlp_lock, flags);
3591 /* The ndlp should not be in memory free mode */
3592 if (NLP_CHK_FREE_REQ(ndlp)) {
3593 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3594 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3595 "0277 lpfc_enable_node: ndlp:x%p "
3596 "usgmap:x%x refcnt:%d\n",
3597 (void *)ndlp, ndlp->nlp_usg_map,
3598 atomic_read(&ndlp->kref.refcount));
3599 return NULL;
3600 }
3601 /* The ndlp should not already be in active mode */
3602 if (NLP_CHK_NODE_ACT(ndlp)) {
3603 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3604 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3605 "0278 lpfc_enable_node: ndlp:x%p "
3606 "usgmap:x%x refcnt:%d\n",
3607 (void *)ndlp, ndlp->nlp_usg_map,
3608 atomic_read(&ndlp->kref.refcount));
3609 return NULL;
3610 }
3611
3612 /* Keep the original DID */
3613 did = ndlp->nlp_DID;
3614
3615 /* re-initialize ndlp except of ndlp linked list pointer */
3616 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3617 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003618 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003619
3620 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3621
3622 if (state != NLP_STE_UNUSED_NODE)
3623 lpfc_nlp_set_state(vport, ndlp, state);
3624
3625 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3626 "node enable: did:x%x",
3627 ndlp->nlp_DID, 0, 0);
3628 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003629}
3630
3631void
James Smart2e0fef82007-06-17 19:56:36 -05003632lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003633{
James Smart87af33f2007-10-27 13:37:43 -04003634 /*
James Smartfa4066b2008-01-11 01:53:27 -05003635 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003636 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003637 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3638 * until ALL other outstanding threads have completed. We check
3639 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003640 */
James Smartfa4066b2008-01-11 01:53:27 -05003641 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3642 return;
James Smart51ef4c22007-08-02 11:10:31 -04003643 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003644 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003645 return;
dea31012005-04-17 16:05:31 -05003646}
3647
3648/*
3649 * Start / ReStart rescue timer for Discovery / RSCN handling
3650 */
3651void
James Smart2e0fef82007-06-17 19:56:36 -05003652lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003653{
James Smart2e0fef82007-06-17 19:56:36 -05003654 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3655 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003656 uint32_t tmo;
3657
James Smart2e0fef82007-06-17 19:56:36 -05003658 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003659 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003660 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3661 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003662 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003663 * FC spec states we need 3 * ratov for CT requests
3664 */
3665 tmo = ((phba->fc_ratov * 3) + 3);
3666 }
dea31012005-04-17 16:05:31 -05003667
James Smart858c9f62007-06-17 19:56:39 -05003668
3669 if (!timer_pending(&vport->fc_disctmo)) {
3670 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3671 "set disc timer: tmo:x%x state:x%x flg:x%x",
3672 tmo, vport->port_state, vport->fc_flag);
3673 }
3674
James Smart2e0fef82007-06-17 19:56:36 -05003675 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3676 spin_lock_irq(shost->host_lock);
3677 vport->fc_flag |= FC_DISC_TMO;
3678 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003679
3680 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003681 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3682 "0247 Start Discovery Timer state x%x "
3683 "Data: x%x x%lx x%x x%x\n",
3684 vport->port_state, tmo,
3685 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3686 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003687
3688 return;
3689}
3690
3691/*
3692 * Cancel rescue timer for Discovery / RSCN handling
3693 */
3694int
James Smart2e0fef82007-06-17 19:56:36 -05003695lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003696{
James Smart2e0fef82007-06-17 19:56:36 -05003697 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003698 unsigned long iflags;
3699
James Smart858c9f62007-06-17 19:56:39 -05003700 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3701 "can disc timer: state:x%x rtry:x%x flg:x%x",
3702 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3703
dea31012005-04-17 16:05:31 -05003704 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003705 if (vport->fc_flag & FC_DISC_TMO) {
3706 spin_lock_irqsave(shost->host_lock, iflags);
3707 vport->fc_flag &= ~FC_DISC_TMO;
3708 spin_unlock_irqrestore(shost->host_lock, iflags);
3709 del_timer_sync(&vport->fc_disctmo);
3710 spin_lock_irqsave(&vport->work_port_lock, iflags);
3711 vport->work_port_events &= ~WORKER_DISC_TMO;
3712 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003713 }
3714
3715 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003716 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3717 "0248 Cancel Discovery Timer state x%x "
3718 "Data: x%x x%x x%x\n",
3719 vport->port_state, vport->fc_flag,
3720 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003721 return 0;
dea31012005-04-17 16:05:31 -05003722}
3723
3724/*
3725 * Check specified ring for outstanding IOCB on the SLI queue
3726 * Return true if iocb matches the specified nport
3727 */
3728int
James Smart2e0fef82007-06-17 19:56:36 -05003729lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3730 struct lpfc_sli_ring *pring,
3731 struct lpfc_iocbq *iocb,
3732 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003733{
James Smart2e0fef82007-06-17 19:56:36 -05003734 struct lpfc_sli *psli = &phba->sli;
3735 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003736 struct lpfc_vport *vport = ndlp->vport;
3737
3738 if (iocb->vport != vport)
3739 return 0;
3740
dea31012005-04-17 16:05:31 -05003741 if (pring->ringno == LPFC_ELS_RING) {
3742 switch (icmd->ulpCommand) {
3743 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003744 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003745 return 1;
dea31012005-04-17 16:05:31 -05003746 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003747 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3748 return 1;
dea31012005-04-17 16:05:31 -05003749 case CMD_XMIT_ELS_RSP64_CX:
3750 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003751 return 1;
dea31012005-04-17 16:05:31 -05003752 }
James Smarta4bc3372006-12-02 13:34:16 -05003753 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003754
3755 } else if (pring->ringno == psli->fcp_ring) {
3756 /* Skip match check if waiting to relogin to FCP target */
3757 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003758 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003759 return 0;
dea31012005-04-17 16:05:31 -05003760 }
3761 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003762 return 1;
dea31012005-04-17 16:05:31 -05003763 }
3764 } else if (pring->ringno == psli->next_ring) {
3765
3766 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003767 return 0;
dea31012005-04-17 16:05:31 -05003768}
3769
3770/*
3771 * Free resources / clean up outstanding I/Os
3772 * associated with nlp_rpi in the LPFC_NODELIST entry.
3773 */
3774static int
James Smart2e0fef82007-06-17 19:56:36 -05003775lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003776{
James Smart2534ba72007-04-25 09:52:20 -04003777 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003778 struct lpfc_sli *psli;
3779 struct lpfc_sli_ring *pring;
3780 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003781 uint32_t i;
dea31012005-04-17 16:05:31 -05003782
James Smart92d7f7b2007-06-17 19:56:38 -05003783 lpfc_fabric_abort_nport(ndlp);
3784
dea31012005-04-17 16:05:31 -05003785 /*
3786 * Everything that matches on txcmplq will be returned
3787 * by firmware with a no rpi error.
3788 */
3789 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003790 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003791 /* Now process each ring */
3792 for (i = 0; i < psli->num_rings; i++) {
3793 pring = &psli->ring[i];
3794
James Smart2e0fef82007-06-17 19:56:36 -05003795 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003796 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003797 list) {
dea31012005-04-17 16:05:31 -05003798 /*
3799 * Check to see if iocb matches the nport we are
3800 * looking for
3801 */
James Smart92d7f7b2007-06-17 19:56:38 -05003802 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3803 ndlp))) {
dea31012005-04-17 16:05:31 -05003804 /* It matches, so deque and call compl
3805 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003806 list_move_tail(&iocb->list,
3807 &completions);
dea31012005-04-17 16:05:31 -05003808 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003809 }
3810 }
James Smart2e0fef82007-06-17 19:56:36 -05003811 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003812 }
3813 }
James Smart2534ba72007-04-25 09:52:20 -04003814
James Smarta257bf92009-04-06 18:48:10 -04003815 /* Cancel all the IOCBs from the completions list */
3816 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3817 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003818
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003819 return 0;
dea31012005-04-17 16:05:31 -05003820}
3821
3822/*
3823 * Free rpi associated with LPFC_NODELIST entry.
3824 * This routine is called from lpfc_freenode(), when we are removing
3825 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3826 * LOGO that completes successfully, and we are waiting to PLOGI back
3827 * to the remote NPort. In addition, it is called after we receive
3828 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3829 * we are waiting to PLOGI back to the remote NPort.
3830 */
3831int
James Smart2e0fef82007-06-17 19:56:36 -05003832lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003833{
James Smart2e0fef82007-06-17 19:56:36 -05003834 struct lpfc_hba *phba = vport->phba;
3835 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003836 int rc;
3837
James Smart21e9a0a2009-05-22 14:53:21 -04003838 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003839 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3840 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003841 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003842 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003843 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003844 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003845 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003846 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003847 }
dea31012005-04-17 16:05:31 -05003848 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04003849
dea31012005-04-17 16:05:31 -05003850 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003851 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003852 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003853 return 1;
3854 }
3855 return 0;
3856}
3857
James Smartecfd03c2010-02-12 14:41:27 -05003858/**
3859 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
3860 * @phba: pointer to lpfc hba data structure.
3861 *
3862 * This routine is invoked to unregister all the currently registered RPIs
3863 * to the HBA.
3864 **/
3865void
3866lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
3867{
3868 struct lpfc_vport **vports;
3869 struct lpfc_nodelist *ndlp;
3870 struct Scsi_Host *shost;
3871 int i;
3872
3873 vports = lpfc_create_vport_work_array(phba);
3874 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3875 shost = lpfc_shost_from_vport(vports[i]);
3876 spin_lock_irq(shost->host_lock);
3877 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003878 if (ndlp->nlp_flag & NLP_RPI_VALID) {
3879 /* The mempool_alloc might sleep */
3880 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05003881 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05003882 spin_lock_irq(shost->host_lock);
3883 }
James Smartecfd03c2010-02-12 14:41:27 -05003884 }
3885 spin_unlock_irq(shost->host_lock);
3886 }
3887 lpfc_destroy_vport_work_array(phba, vports);
3888}
3889
James Smart92d7f7b2007-06-17 19:56:38 -05003890void
3891lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3892{
3893 struct lpfc_hba *phba = vport->phba;
3894 LPFC_MBOXQ_t *mbox;
3895 int rc;
3896
3897 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3898 if (mbox) {
3899 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3900 mbox->vport = vport;
3901 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003902 mbox->context1 = NULL;
3903 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003904 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003905 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003906
3907 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3908 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3909 "1836 Could not issue "
3910 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003911 }
3912}
3913
3914void
3915lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3916{
3917 struct lpfc_hba *phba = vport->phba;
3918 LPFC_MBOXQ_t *mbox;
3919 int rc;
3920
3921 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3922 if (mbox) {
3923 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3924 mbox->vport = vport;
3925 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003926 mbox->context1 = NULL;
3927 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003928 if (rc != MBX_TIMEOUT)
3929 mempool_free(mbox, phba->mbox_mem_pool);
3930
3931 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003932 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3933 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003934 "unreg_did (default rpis) status %d\n",
3935 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003936 }
3937}
3938
dea31012005-04-17 16:05:31 -05003939/*
3940 * Free resources associated with LPFC_NODELIST entry
3941 * so it can be freed.
3942 */
3943static int
James Smart2e0fef82007-06-17 19:56:36 -05003944lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003945{
James Smart2e0fef82007-06-17 19:56:36 -05003946 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3947 struct lpfc_hba *phba = vport->phba;
3948 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003949 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003950
3951 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003952 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3953 "0900 Cleanup node for NPort x%x "
3954 "Data: x%x x%x x%x\n",
3955 ndlp->nlp_DID, ndlp->nlp_flag,
3956 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003957 if (NLP_CHK_FREE_REQ(ndlp)) {
3958 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3959 "0280 lpfc_cleanup_node: ndlp:x%p "
3960 "usgmap:x%x refcnt:%d\n",
3961 (void *)ndlp, ndlp->nlp_usg_map,
3962 atomic_read(&ndlp->kref.refcount));
3963 lpfc_dequeue_node(vport, ndlp);
3964 } else {
3965 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3966 "0281 lpfc_cleanup_node: ndlp:x%p "
3967 "usgmap:x%x refcnt:%d\n",
3968 (void *)ndlp, ndlp->nlp_usg_map,
3969 atomic_read(&ndlp->kref.refcount));
3970 lpfc_disable_node(vport, ndlp);
3971 }
dea31012005-04-17 16:05:31 -05003972
dea31012005-04-17 16:05:31 -05003973 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3974 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003975 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003976 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3977 mb->context2 = NULL;
3978 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3979 }
3980 }
James Smart33ccf8d2006-08-17 11:57:58 -04003981
James Smart2e0fef82007-06-17 19:56:36 -05003982 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003983 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003984 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003985 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003986 mp = (struct lpfc_dmabuf *) (mb->context1);
3987 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003988 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003989 kfree(mp);
3990 }
3991 list_del(&mb->list);
James Smartffc95492010-06-07 15:23:17 -04003992 if (phba->sli_rev == LPFC_SLI_REV4)
3993 lpfc_sli4_free_rpi(phba,
3994 mb->u.mb.un.varRegLogin.rpi);
dea31012005-04-17 16:05:31 -05003995 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003996 /* We shall not invoke the lpfc_nlp_put to decrement
3997 * the ndlp reference count as we are in the process
3998 * of lpfc_nlp_release.
3999 */
dea31012005-04-17 16:05:31 -05004000 }
4001 }
James Smart2e0fef82007-06-17 19:56:36 -05004002 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004003
James Smarte47c9092008-02-08 18:49:26 -05004004 lpfc_els_abort(phba, ndlp);
4005
James Smart2e0fef82007-06-17 19:56:36 -05004006 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004007 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004008 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004009
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004010 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004011 del_timer_sync(&ndlp->nlp_delayfunc);
4012
James Smart0d2b6b82008-06-14 22:52:47 -04004013 list_del_init(&ndlp->els_retry_evt.evt_listp);
4014 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05004015
James Smart2e0fef82007-06-17 19:56:36 -05004016 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004017
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004018 return 0;
dea31012005-04-17 16:05:31 -05004019}
4020
4021/*
4022 * Check to see if we can free the nlp back to the freelist.
4023 * If we are in the middle of using the nlp in the discovery state
4024 * machine, defer the free till we reach the end of the state machine.
4025 */
James Smart329f9bc2007-04-25 09:53:01 -04004026static void
James Smart2e0fef82007-06-17 19:56:36 -05004027lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004028{
James Smarta8adb832007-10-27 13:37:53 -04004029 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004030 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04004031 LPFC_MBOXQ_t *mbox;
4032 int rc;
dea31012005-04-17 16:05:31 -05004033
James Smart0d2b6b82008-06-14 22:52:47 -04004034 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004035 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smartffc95492010-06-07 15:23:17 -04004036 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
James Smart21e9a0a2009-05-22 14:53:21 -04004037 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04004038 /* For this case we need to cleanup the default rpi
4039 * allocated by the firmware.
4040 */
4041 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4042 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004043 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04004044 (uint8_t *) &vport->fc_sparam, mbox, 0);
4045 if (rc) {
4046 mempool_free(mbox, phba->mbox_mem_pool);
4047 }
4048 else {
4049 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4050 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4051 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05004052 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04004053 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4054 if (rc == MBX_NOT_FINISHED) {
4055 mempool_free(mbox, phba->mbox_mem_pool);
4056 }
4057 }
4058 }
4059 }
James Smart2e0fef82007-06-17 19:56:36 -05004060 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004061
James Smart2e0fef82007-06-17 19:56:36 -05004062 /*
James Smart92d7f7b2007-06-17 19:56:38 -05004063 * We can get here with a non-NULL ndlp->rport because when we
4064 * unregister a rport we don't break the rport/node linkage. So if we
4065 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05004066 */
James Smart92d7f7b2007-06-17 19:56:38 -05004067 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04004068 rdata = ndlp->rport->dd_data;
4069 rdata->pnode = NULL;
4070 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05004071 }
dea31012005-04-17 16:05:31 -05004072}
4073
4074static int
James Smart2e0fef82007-06-17 19:56:36 -05004075lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4076 uint32_t did)
dea31012005-04-17 16:05:31 -05004077{
James Smart2e0fef82007-06-17 19:56:36 -05004078 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004079
4080 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004081 return 0;
dea31012005-04-17 16:05:31 -05004082
dea31012005-04-17 16:05:31 -05004083 /* First check for Direct match */
4084 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004085 return 1;
dea31012005-04-17 16:05:31 -05004086
4087 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004088 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004089 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004090 return 0;
dea31012005-04-17 16:05:31 -05004091 }
4092
4093 matchdid.un.word = did;
4094 ndlpdid.un.word = ndlp->nlp_DID;
4095 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4096 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4097 (mydid.un.b.area == matchdid.un.b.area)) {
4098 if ((ndlpdid.un.b.domain == 0) &&
4099 (ndlpdid.un.b.area == 0)) {
4100 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004101 return 1;
dea31012005-04-17 16:05:31 -05004102 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004103 return 0;
dea31012005-04-17 16:05:31 -05004104 }
4105
4106 matchdid.un.word = ndlp->nlp_DID;
4107 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4108 (mydid.un.b.area == ndlpdid.un.b.area)) {
4109 if ((matchdid.un.b.domain == 0) &&
4110 (matchdid.un.b.area == 0)) {
4111 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004112 return 1;
dea31012005-04-17 16:05:31 -05004113 }
4114 }
4115 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004116 return 0;
dea31012005-04-17 16:05:31 -05004117}
4118
James Smart685f0bf2007-04-25 09:53:08 -04004119/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004120static struct lpfc_nodelist *
4121__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004122{
James Smart2fb9bd82006-12-02 13:33:57 -05004123 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004124 uint32_t data1;
4125
James Smart2e0fef82007-06-17 19:56:36 -05004126 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4127 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004128 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4129 ((uint32_t) ndlp->nlp_xri << 16) |
4130 ((uint32_t) ndlp->nlp_type << 8) |
4131 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004132 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4133 "0929 FIND node DID "
4134 "Data: x%p x%x x%x x%x\n",
4135 ndlp, ndlp->nlp_DID,
4136 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04004137 return ndlp;
dea31012005-04-17 16:05:31 -05004138 }
4139 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004140
dea31012005-04-17 16:05:31 -05004141 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004142 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4143 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004144 return NULL;
4145}
4146
4147struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004148lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004149{
James Smart2e0fef82007-06-17 19:56:36 -05004150 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004151 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004152
James Smart2e0fef82007-06-17 19:56:36 -05004153 spin_lock_irq(shost->host_lock);
4154 ndlp = __lpfc_findnode_did(vport, did);
4155 spin_unlock_irq(shost->host_lock);
4156 return ndlp;
4157}
4158
4159struct lpfc_nodelist *
4160lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4161{
4162 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4163 struct lpfc_nodelist *ndlp;
4164
4165 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004166 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004167 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4168 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004169 return NULL;
4170 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004171 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004172 if (!ndlp)
4173 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004174 lpfc_nlp_init(vport, ndlp, did);
4175 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4176 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004177 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004178 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004179 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004180 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4181 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4182 if (!ndlp)
4183 return NULL;
4184 spin_lock_irq(shost->host_lock);
4185 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4186 spin_unlock_irq(shost->host_lock);
4187 return ndlp;
dea31012005-04-17 16:05:31 -05004188 }
James Smarte47c9092008-02-08 18:49:26 -05004189
James Smart58da1ff2008-04-07 10:15:56 -04004190 if ((vport->fc_flag & FC_RSCN_MODE) &&
4191 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004192 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04004193 /* If we've already recieved a PLOGI from this NPort
4194 * we don't need to try to discover it again.
4195 */
4196 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4197 return NULL;
4198
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004199 /* Since this node is marked for discovery,
4200 * delay timeout is not needed.
4201 */
James Smart0d2b6b82008-06-14 22:52:47 -04004202 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004203 spin_lock_irq(shost->host_lock);
4204 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4205 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004206 } else
dea31012005-04-17 16:05:31 -05004207 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004208 } else {
James Smart87af33f2007-10-27 13:37:43 -04004209 /* If we've already recieved a PLOGI from this NPort,
4210 * or we are already in the process of discovery on it,
4211 * we don't need to try to discover it again.
4212 */
James Smart685f0bf2007-04-25 09:53:08 -04004213 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004214 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4215 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004216 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004217 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4218 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004219 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004220 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004221 }
4222 return ndlp;
4223}
4224
4225/* Build a list of nodes to discover based on the loopmap */
4226void
James Smart2e0fef82007-06-17 19:56:36 -05004227lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004228{
James Smart2e0fef82007-06-17 19:56:36 -05004229 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004230 int j;
4231 uint32_t alpa, index;
4232
James Smart2e0fef82007-06-17 19:56:36 -05004233 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004234 return;
James Smart2e0fef82007-06-17 19:56:36 -05004235
4236 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004237 return;
dea31012005-04-17 16:05:31 -05004238
4239 /* Check for loop map present or not */
4240 if (phba->alpa_map[0]) {
4241 for (j = 1; j <= phba->alpa_map[0]; j++) {
4242 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004243 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004244 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004245 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004246 }
4247 } else {
4248 /* No alpamap, so try all alpa's */
4249 for (j = 0; j < FC_MAXLOOP; j++) {
4250 /* If cfg_scan_down is set, start from highest
4251 * ALPA (0xef) to lowest (0x1).
4252 */
James Smart3de2a652007-08-02 11:09:59 -04004253 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004254 index = j;
4255 else
4256 index = FC_MAXLOOP - j - 1;
4257 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004258 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004259 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004260 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004261 }
4262 }
4263 return;
4264}
4265
dea31012005-04-17 16:05:31 -05004266void
James Smart2e0fef82007-06-17 19:56:36 -05004267lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004268{
dea31012005-04-17 16:05:31 -05004269 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004270 struct lpfc_sli *psli = &phba->sli;
4271 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4272 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4273 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4274 int rc;
4275
James Smart92d7f7b2007-06-17 19:56:38 -05004276 /*
4277 * if it's not a physical port or if we already send
4278 * clear_la then don't send it.
4279 */
4280 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004281 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4282 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004283 return;
4284
James Smart2e0fef82007-06-17 19:56:36 -05004285 /* Link up discovery */
4286 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4287 phba->link_state = LPFC_CLEAR_LA;
4288 lpfc_clear_la(phba, mbox);
4289 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4290 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004291 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004292 if (rc == MBX_NOT_FINISHED) {
4293 mempool_free(mbox, phba->mbox_mem_pool);
4294 lpfc_disc_flush_list(vport);
4295 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4296 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4297 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004298 phba->link_state = LPFC_HBA_ERROR;
4299 }
4300 }
4301}
4302
4303/* Reg_vpi to tell firmware to resume normal operations */
4304void
4305lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4306{
4307 LPFC_MBOXQ_t *regvpimbox;
4308
4309 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4310 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004311 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004312 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4313 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004314 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004315 == MBX_NOT_FINISHED) {
4316 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004317 }
4318 }
4319}
4320
4321/* Start Link up / RSCN discovery on NPR nodes */
4322void
4323lpfc_disc_start(struct lpfc_vport *vport)
4324{
4325 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4326 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004327 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004328 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004329 int did_changed;
dea31012005-04-17 16:05:31 -05004330
James Smart2e0fef82007-06-17 19:56:36 -05004331 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004332 return;
James Smart2e0fef82007-06-17 19:56:36 -05004333
4334 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004335 clear_la_pending = 1;
4336 else
4337 clear_la_pending = 0;
4338
James Smart2e0fef82007-06-17 19:56:36 -05004339 if (vport->port_state < LPFC_VPORT_READY)
4340 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004341
James Smart2e0fef82007-06-17 19:56:36 -05004342 lpfc_set_disctmo(vport);
4343
4344 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004345 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004346 else
dea31012005-04-17 16:05:31 -05004347 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004348
4349 vport->fc_prevDID = vport->fc_myDID;
4350 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004351
4352 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004353 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4354 "0202 Start Discovery hba state x%x "
4355 "Data: x%x x%x x%x\n",
4356 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4357 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004358
4359 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004360 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004361
4362 if (num_sent)
4363 return;
4364
James Smart92d7f7b2007-06-17 19:56:38 -05004365 /*
4366 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
4367 * continue discovery.
4368 */
4369 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004370 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004371 !(vport->fc_flag & FC_RSCN_MODE) &&
4372 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004373 lpfc_issue_reg_vpi(phba, vport);
4374 return;
4375 }
James Smart2e0fef82007-06-17 19:56:36 -05004376
James Smart92d7f7b2007-06-17 19:56:38 -05004377 /*
4378 * For SLI2, we need to set port_state to READY and continue
4379 * discovery.
4380 */
4381 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4382 /* If we get here, there is nothing to ADISC */
4383 if (vport->port_type == LPFC_PHYSICAL_PORT)
4384 lpfc_issue_clear_la(phba, vport);
4385
4386 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004387 vport->num_disc_nodes = 0;
4388 /* go thru NPR nodes and issue ELS PLOGIs */
4389 if (vport->fc_npr_cnt)
4390 lpfc_els_disc_plogi(vport);
4391
4392 if (!vport->num_disc_nodes) {
4393 spin_lock_irq(shost->host_lock);
4394 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4395 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004396 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05004397 }
4398 }
James Smart92d7f7b2007-06-17 19:56:38 -05004399 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004400 } else {
4401 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004402 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05004403
4404 if (num_sent)
4405 return;
4406
James Smart2e0fef82007-06-17 19:56:36 -05004407 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05004408 /* Check to see if more RSCNs came in while we
4409 * were processing this one.
4410 */
James Smart2e0fef82007-06-17 19:56:36 -05004411 if ((vport->fc_rscn_id_cnt == 0) &&
4412 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
4413 spin_lock_irq(shost->host_lock);
4414 vport->fc_flag &= ~FC_RSCN_MODE;
4415 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004416 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004417 } else
James Smart2e0fef82007-06-17 19:56:36 -05004418 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05004419 }
4420 }
4421 return;
4422}
4423
4424/*
4425 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
4426 * ring the match the sppecified nodelist.
4427 */
4428static void
James Smart2e0fef82007-06-17 19:56:36 -05004429lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004430{
James Smart2534ba72007-04-25 09:52:20 -04004431 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004432 struct lpfc_sli *psli;
4433 IOCB_t *icmd;
4434 struct lpfc_iocbq *iocb, *next_iocb;
4435 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004436
4437 psli = &phba->sli;
4438 pring = &psli->ring[LPFC_ELS_RING];
4439
4440 /* Error matching iocb on txq or txcmplq
4441 * First check the txq.
4442 */
James Smart2e0fef82007-06-17 19:56:36 -05004443 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004444 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4445 if (iocb->context1 != ndlp) {
4446 continue;
4447 }
4448 icmd = &iocb->iocb;
4449 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
4450 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
4451
James Smart2534ba72007-04-25 09:52:20 -04004452 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05004453 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05004454 }
4455 }
4456
4457 /* Next check the txcmplq */
4458 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
4459 if (iocb->context1 != ndlp) {
4460 continue;
4461 }
4462 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004463 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
4464 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04004465 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05004466 }
4467 }
James Smart2e0fef82007-06-17 19:56:36 -05004468 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04004469
James Smarta257bf92009-04-06 18:48:10 -04004470 /* Cancel all the IOCBs from the completions list */
4471 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4472 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004473}
4474
Adrian Bunka6ababd2007-11-05 18:07:33 +01004475static void
James Smart2e0fef82007-06-17 19:56:36 -05004476lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004477{
4478 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05004479 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004480
James Smart2e0fef82007-06-17 19:56:36 -05004481 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
4482 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004483 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004484 if (!NLP_CHK_NODE_ACT(ndlp))
4485 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004486 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4487 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
4488 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04004489 }
dea31012005-04-17 16:05:31 -05004490 }
4491 }
dea31012005-04-17 16:05:31 -05004492}
4493
James Smart92d7f7b2007-06-17 19:56:38 -05004494void
4495lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
4496{
4497 lpfc_els_flush_rscn(vport);
4498 lpfc_els_flush_cmd(vport);
4499 lpfc_disc_flush_list(vport);
4500}
4501
dea31012005-04-17 16:05:31 -05004502/*****************************************************************************/
4503/*
4504 * NAME: lpfc_disc_timeout
4505 *
4506 * FUNCTION: Fibre Channel driver discovery timeout routine.
4507 *
4508 * EXECUTION ENVIRONMENT: interrupt only
4509 *
4510 * CALLED FROM:
4511 * Timer function
4512 *
4513 * RETURNS:
4514 * none
4515 */
4516/*****************************************************************************/
4517void
4518lpfc_disc_timeout(unsigned long ptr)
4519{
James Smart2e0fef82007-06-17 19:56:36 -05004520 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4521 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04004522 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004523 unsigned long flags = 0;
4524
4525 if (unlikely(!phba))
4526 return;
4527
James Smart5e9d9b82008-06-14 22:52:53 -04004528 spin_lock_irqsave(&vport->work_port_lock, flags);
4529 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4530 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05004531 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04004532 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05004533
James Smart5e9d9b82008-06-14 22:52:53 -04004534 if (!tmo_posted)
4535 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004536 return;
4537}
4538
4539static void
James Smart2e0fef82007-06-17 19:56:36 -05004540lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004541{
James Smart2e0fef82007-06-17 19:56:36 -05004542 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4543 struct lpfc_hba *phba = vport->phba;
4544 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004545 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05004546 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05004547 int rc, clrlaerr = 0;
4548
James Smart2e0fef82007-06-17 19:56:36 -05004549 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05004550 return;
4551
James Smart2e0fef82007-06-17 19:56:36 -05004552 spin_lock_irq(shost->host_lock);
4553 vport->fc_flag &= ~FC_DISC_TMO;
4554 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004555
James Smart858c9f62007-06-17 19:56:39 -05004556 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4557 "disc timeout: state:x%x rtry:x%x flg:x%x",
4558 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4559
James Smart2e0fef82007-06-17 19:56:36 -05004560 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05004561
4562 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05004563 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
4564 * FAN
4565 */
4566 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04004567 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4568 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004569 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05004570 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004571 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004572 if (!NLP_CHK_NODE_ACT(ndlp))
4573 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004574 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4575 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004576 if (ndlp->nlp_type & NLP_FABRIC) {
4577 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05004578 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004579
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004580 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004581 /* Fail outstanding IO now since device
4582 * is marked for PLOGI.
4583 */
James Smart2e0fef82007-06-17 19:56:36 -05004584 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004585 }
4586 }
James Smart92d7f7b2007-06-17 19:56:38 -05004587 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05004588 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05004589 return;
James Smart92d7f7b2007-06-17 19:56:38 -05004590 }
dea31012005-04-17 16:05:31 -05004591 break;
4592
James Smart92d7f7b2007-06-17 19:56:38 -05004593 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05004594 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05004595 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05004596 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04004597 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4598 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04004599 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05004600
4601 /* Assume no Fabric and go on with discovery.
4602 * Check for outstanding ELS FLOGI to abort.
4603 */
4604
4605 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05004606 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004607
4608 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05004609 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05004610 break;
4611
4612 case LPFC_FABRIC_CFG_LINK:
4613 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4614 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04004615 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4616 "0223 Timeout while waiting for "
4617 "NameServer login\n");
dea31012005-04-17 16:05:31 -05004618 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05004619 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05004620 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04004621 lpfc_els_abort(phba, ndlp);
4622
4623 /* ReStart discovery */
4624 goto restart_disc;
dea31012005-04-17 16:05:31 -05004625
4626 case LPFC_NS_QRY:
4627 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04004628 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4629 "0224 NameServer Query timeout "
4630 "Data: x%x x%x\n",
4631 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004632
James Smart92d7f7b2007-06-17 19:56:38 -05004633 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4634 /* Try it one more time */
4635 vport->fc_ns_retry++;
4636 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4637 vport->fc_ns_retry, 0);
4638 if (rc == 0)
4639 break;
dea31012005-04-17 16:05:31 -05004640 }
James Smart92d7f7b2007-06-17 19:56:38 -05004641 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05004642
James Smart87af33f2007-10-27 13:37:43 -04004643restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05004644 /*
4645 * Discovery is over.
4646 * set port_state to PORT_READY if SLI2.
4647 * cmpl_reg_vpi will set port_state to READY for SLI3.
4648 */
James Smart3772a992009-05-22 14:50:54 -04004649 if (phba->sli_rev < LPFC_SLI_REV4) {
4650 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4651 lpfc_issue_reg_vpi(phba, vport);
4652 else { /* NPIV Not enabled */
4653 lpfc_issue_clear_la(phba, vport);
4654 vport->port_state = LPFC_VPORT_READY;
4655 }
dea31012005-04-17 16:05:31 -05004656 }
4657
4658 /* Setup and issue mailbox INITIALIZE LINK command */
4659 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4660 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004661 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4662 "0206 Device Discovery "
4663 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004664 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004665 break;
4666 }
4667
4668 lpfc_linkdown(phba);
4669 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4670 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004671 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004672 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004673 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004674 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004675 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004676 if (rc == MBX_NOT_FINISHED)
4677 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4678
4679 break;
4680
4681 case LPFC_DISC_AUTH:
4682 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004683 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4684 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004685 lpfc_disc_flush_list(vport);
4686
James Smart92d7f7b2007-06-17 19:56:38 -05004687 /*
4688 * set port_state to PORT_READY if SLI2.
4689 * cmpl_reg_vpi will set port_state to READY for SLI3.
4690 */
James Smart3772a992009-05-22 14:50:54 -04004691 if (phba->sli_rev < LPFC_SLI_REV4) {
4692 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4693 lpfc_issue_reg_vpi(phba, vport);
4694 else { /* NPIV Not enabled */
4695 lpfc_issue_clear_la(phba, vport);
4696 vport->port_state = LPFC_VPORT_READY;
4697 }
dea31012005-04-17 16:05:31 -05004698 }
4699 break;
4700
James Smart2e0fef82007-06-17 19:56:36 -05004701 case LPFC_VPORT_READY:
4702 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004703 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4704 "0231 RSCN timeout Data: x%x "
4705 "x%x\n",
4706 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004707
4708 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004709 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004710
James Smart2e0fef82007-06-17 19:56:36 -05004711 lpfc_els_flush_rscn(vport);
4712 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004713 }
4714 break;
James Smart2e0fef82007-06-17 19:56:36 -05004715
James Smart92d7f7b2007-06-17 19:56:38 -05004716 default:
James Smarte8b62012007-08-02 11:10:09 -04004717 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004718 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004719 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004720 break;
4721 }
4722
4723 switch (phba->link_state) {
4724 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004725 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004726 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4727 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004728 clrlaerr = 1;
4729 break;
4730
James Smart09372822008-01-11 01:52:54 -05004731 case LPFC_LINK_UP:
4732 lpfc_issue_clear_la(phba, vport);
4733 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004734 case LPFC_LINK_UNKNOWN:
4735 case LPFC_WARM_START:
4736 case LPFC_INIT_START:
4737 case LPFC_INIT_MBX_CMDS:
4738 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004739 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004740 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4741 "0230 Unexpected timeout, hba link "
4742 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004743 clrlaerr = 1;
4744 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004745
4746 case LPFC_HBA_READY:
4747 break;
dea31012005-04-17 16:05:31 -05004748 }
4749
4750 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004751 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004752 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004753 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4754 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004755 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004756 }
4757
4758 return;
4759}
4760
dea31012005-04-17 16:05:31 -05004761/*
4762 * This routine handles processing a NameServer REG_LOGIN mailbox
4763 * command upon completion. It is setup in the LPFC_MBOXQ
4764 * as the completion routine when the command is
4765 * handed off to the SLI layer.
4766 */
4767void
James Smart2e0fef82007-06-17 19:56:36 -05004768lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004769{
James Smart04c68492009-05-22 14:52:52 -04004770 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004771 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4772 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4773 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004774
4775 pmb->context1 = NULL;
4776
dea31012005-04-17 16:05:31 -05004777 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004778 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004779 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004780 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004781
James Smart2e0fef82007-06-17 19:56:36 -05004782 /*
4783 * Start issuing Fabric-Device Management Interface (FDMI) command to
4784 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4785 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004786 */
James Smart2e0fef82007-06-17 19:56:36 -05004787
James Smart3de2a652007-08-02 11:09:59 -04004788 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004789 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4790 else
4791 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004792
James Smartfa4066b2008-01-11 01:53:27 -05004793 /* decrement the node reference count held for this callback
4794 * function.
4795 */
James Smart329f9bc2007-04-25 09:53:01 -04004796 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004797 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4798 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004799 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004800
4801 return;
4802}
4803
James Smart685f0bf2007-04-25 09:53:08 -04004804static int
4805lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4806{
4807 uint16_t *rpi = param;
4808
4809 return ndlp->nlp_rpi == *rpi;
4810}
4811
4812static int
4813lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4814{
4815 return memcmp(&ndlp->nlp_portname, param,
4816 sizeof(ndlp->nlp_portname)) == 0;
4817}
4818
Adrian Bunka6ababd2007-11-05 18:07:33 +01004819static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004820__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004821{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004822 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004823
James Smart2e0fef82007-06-17 19:56:36 -05004824 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004825 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004826 return ndlp;
4827 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004828 return NULL;
dea31012005-04-17 16:05:31 -05004829}
4830
James Smart685f0bf2007-04-25 09:53:08 -04004831/*
4832 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004833 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004834 */
4835struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004836__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004837{
James Smart2e0fef82007-06-17 19:56:36 -05004838 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004839}
4840
James Smart488d1462006-03-07 15:02:37 -05004841/*
James Smart685f0bf2007-04-25 09:53:08 -04004842 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004843 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004844 */
4845struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004846lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004847{
James Smart2e0fef82007-06-17 19:56:36 -05004848 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004849 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004850
James Smart2e0fef82007-06-17 19:56:36 -05004851 spin_lock_irq(shost->host_lock);
4852 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4853 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004854 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004855}
4856
dea31012005-04-17 16:05:31 -05004857void
James Smart2e0fef82007-06-17 19:56:36 -05004858lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4859 uint32_t did)
dea31012005-04-17 16:05:31 -05004860{
4861 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004862
4863 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004864 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004865
4866 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4867 "node init: did:x%x",
4868 ndlp->nlp_DID, 0, 0);
4869
dea31012005-04-17 16:05:31 -05004870 return;
4871}
James Smart329f9bc2007-04-25 09:53:01 -04004872
James Smart98c9ea52007-10-27 13:37:33 -04004873/* This routine releases all resources associated with a specifc NPort's ndlp
4874 * and mempool_free's the nodelist.
4875 */
James Smart311464e2007-08-02 11:10:37 -04004876static void
James Smart329f9bc2007-04-25 09:53:01 -04004877lpfc_nlp_release(struct kref *kref)
4878{
James Smarte47c9092008-02-08 18:49:26 -05004879 struct lpfc_hba *phba;
4880 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004881 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4882 kref);
James Smart858c9f62007-06-17 19:56:39 -05004883
4884 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4885 "node release: did:x%x flg:x%x type:x%x",
4886 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4887
James Smarte47c9092008-02-08 18:49:26 -05004888 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4889 "0279 lpfc_nlp_release: ndlp:x%p "
4890 "usgmap:x%x refcnt:%d\n",
4891 (void *)ndlp, ndlp->nlp_usg_map,
4892 atomic_read(&ndlp->kref.refcount));
4893
4894 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004895 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004896
4897 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004898 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004899 spin_lock_irqsave(&phba->ndlp_lock, flags);
4900 NLP_CLR_NODE_ACT(ndlp);
4901 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4902
4903 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004904 if (NLP_CHK_FREE_REQ(ndlp)) {
4905 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004906 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004907 }
James Smart329f9bc2007-04-25 09:53:01 -04004908}
4909
James Smart98c9ea52007-10-27 13:37:33 -04004910/* This routine bumps the reference count for a ndlp structure to ensure
4911 * that one discovery thread won't free a ndlp while another discovery thread
4912 * is using it.
4913 */
James Smart329f9bc2007-04-25 09:53:01 -04004914struct lpfc_nodelist *
4915lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4916{
James Smarte47c9092008-02-08 18:49:26 -05004917 struct lpfc_hba *phba;
4918 unsigned long flags;
4919
James Smart98c9ea52007-10-27 13:37:33 -04004920 if (ndlp) {
4921 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4922 "node get: did:x%x flg:x%x refcnt:x%x",
4923 ndlp->nlp_DID, ndlp->nlp_flag,
4924 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004925 /* The check of ndlp usage to prevent incrementing the
4926 * ndlp reference count that is in the process of being
4927 * released.
4928 */
James Smarta257bf92009-04-06 18:48:10 -04004929 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004930 spin_lock_irqsave(&phba->ndlp_lock, flags);
4931 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4932 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4933 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4934 "0276 lpfc_nlp_get: ndlp:x%p "
4935 "usgmap:x%x refcnt:%d\n",
4936 (void *)ndlp, ndlp->nlp_usg_map,
4937 atomic_read(&ndlp->kref.refcount));
4938 return NULL;
4939 } else
4940 kref_get(&ndlp->kref);
4941 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004942 }
James Smart329f9bc2007-04-25 09:53:01 -04004943 return ndlp;
4944}
4945
James Smart98c9ea52007-10-27 13:37:33 -04004946/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004947 * count goes to 0, this indicates the the associated nodelist should be
4948 * freed. Returning 1 indicates the ndlp resource has been released; on the
4949 * other hand, returning 0 indicates the ndlp resource has not been released
4950 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004951 */
James Smart329f9bc2007-04-25 09:53:01 -04004952int
4953lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4954{
James Smarte47c9092008-02-08 18:49:26 -05004955 struct lpfc_hba *phba;
4956 unsigned long flags;
4957
4958 if (!ndlp)
4959 return 1;
4960
4961 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4962 "node put: did:x%x flg:x%x refcnt:x%x",
4963 ndlp->nlp_DID, ndlp->nlp_flag,
4964 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004965 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004966 spin_lock_irqsave(&phba->ndlp_lock, flags);
4967 /* Check the ndlp memory free acknowledge flag to avoid the
4968 * possible race condition that kref_put got invoked again
4969 * after previous one has done ndlp memory free.
4970 */
4971 if (NLP_CHK_FREE_ACK(ndlp)) {
4972 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4973 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4974 "0274 lpfc_nlp_put: ndlp:x%p "
4975 "usgmap:x%x refcnt:%d\n",
4976 (void *)ndlp, ndlp->nlp_usg_map,
4977 atomic_read(&ndlp->kref.refcount));
4978 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004979 }
James Smarte47c9092008-02-08 18:49:26 -05004980 /* Check the ndlp inactivate log flag to avoid the possible
4981 * race condition that kref_put got invoked again after ndlp
4982 * is already in inactivating state.
4983 */
4984 if (NLP_CHK_IACT_REQ(ndlp)) {
4985 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4986 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4987 "0275 lpfc_nlp_put: ndlp:x%p "
4988 "usgmap:x%x refcnt:%d\n",
4989 (void *)ndlp, ndlp->nlp_usg_map,
4990 atomic_read(&ndlp->kref.refcount));
4991 return 1;
4992 }
4993 /* For last put, mark the ndlp usage flags to make sure no
4994 * other kref_get and kref_put on the same ndlp shall get
4995 * in between the process when the final kref_put has been
4996 * invoked on this ndlp.
4997 */
4998 if (atomic_read(&ndlp->kref.refcount) == 1) {
4999 /* Indicate ndlp is put to inactive state. */
5000 NLP_SET_IACT_REQ(ndlp);
5001 /* Acknowledge ndlp memory free has been seen. */
5002 if (NLP_CHK_FREE_REQ(ndlp))
5003 NLP_SET_FREE_ACK(ndlp);
5004 }
5005 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5006 /* Note, the kref_put returns 1 when decrementing a reference
5007 * count that was 1, it invokes the release callback function,
5008 * but it still left the reference count as 1 (not actually
5009 * performs the last decrementation). Otherwise, it actually
5010 * decrements the reference count and returns 0.
5011 */
5012 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04005013}
James Smart98c9ea52007-10-27 13:37:33 -04005014
5015/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05005016 * by any other discovery thread. This routine returns 1 if the
5017 * ndlp has been freed. A return value of 0 indicates the ndlp is
5018 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04005019 */
5020int
5021lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5022{
5023 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5024 "node not used: did:x%x flg:x%x refcnt:x%x",
5025 ndlp->nlp_DID, ndlp->nlp_flag,
5026 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005027 if (atomic_read(&ndlp->kref.refcount) == 1)
5028 if (lpfc_nlp_put(ndlp))
5029 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005030 return 0;
5031}
James Smart6fb120a2009-05-22 14:52:59 -04005032
5033/**
5034 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5035 * @phba: Pointer to hba context object.
5036 *
5037 * This function iterate through all FC nodes associated
5038 * will all vports to check if there is any node with
5039 * fc_rports associated with it. If there is an fc_rport
5040 * associated with the node, then the node is either in
5041 * discovered state or its devloss_timer is pending.
5042 */
5043static int
5044lpfc_fcf_inuse(struct lpfc_hba *phba)
5045{
5046 struct lpfc_vport **vports;
5047 int i, ret = 0;
5048 struct lpfc_nodelist *ndlp;
5049 struct Scsi_Host *shost;
5050
5051 vports = lpfc_create_vport_work_array(phba);
5052
5053 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5054 shost = lpfc_shost_from_vport(vports[i]);
5055 spin_lock_irq(shost->host_lock);
5056 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5057 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5058 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5059 ret = 1;
5060 spin_unlock_irq(shost->host_lock);
5061 goto out;
James Smart891478a2009-11-18 15:40:23 -05005062 } else {
5063 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
5064 "2624 RPI %x DID %x flg %x still "
5065 "logged in\n",
5066 ndlp->nlp_rpi, ndlp->nlp_DID,
5067 ndlp->nlp_flag);
5068 if (ndlp->nlp_flag & NLP_RPI_VALID)
5069 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04005070 }
5071 }
5072 spin_unlock_irq(shost->host_lock);
5073 }
5074out:
5075 lpfc_destroy_vport_work_array(phba, vports);
5076 return ret;
5077}
5078
5079/**
5080 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5081 * @phba: Pointer to hba context object.
5082 * @mboxq: Pointer to mailbox object.
5083 *
5084 * This function frees memory associated with the mailbox command.
5085 */
5086static void
5087lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5088{
5089 struct lpfc_vport *vport = mboxq->vport;
5090
5091 if (mboxq->u.mb.mbxStatus) {
5092 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5093 "2555 UNREG_VFI mbxStatus error x%x "
5094 "HBA state x%x\n",
5095 mboxq->u.mb.mbxStatus, vport->port_state);
5096 }
5097 mempool_free(mboxq, phba->mbox_mem_pool);
5098 return;
5099}
5100
5101/**
5102 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
5103 * @phba: Pointer to hba context object.
5104 * @mboxq: Pointer to mailbox object.
5105 *
5106 * This function frees memory associated with the mailbox command.
5107 */
5108static void
5109lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5110{
5111 struct lpfc_vport *vport = mboxq->vport;
5112
5113 if (mboxq->u.mb.mbxStatus) {
5114 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5115 "2550 UNREG_FCFI mbxStatus error x%x "
5116 "HBA state x%x\n",
5117 mboxq->u.mb.mbxStatus, vport->port_state);
5118 }
5119 mempool_free(mboxq, phba->mbox_mem_pool);
5120 return;
5121}
5122
5123/**
James Smartecfd03c2010-02-12 14:41:27 -05005124 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04005125 * @phba: Pointer to hba context object.
5126 *
James Smartecfd03c2010-02-12 14:41:27 -05005127 * This function prepare the HBA for unregistering the currently registered
5128 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5129 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04005130 */
James Smartecfd03c2010-02-12 14:41:27 -05005131int
5132lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04005133{
5134 LPFC_MBOXQ_t *mbox;
James Smart6fb120a2009-05-22 14:52:59 -04005135 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05005136 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05005137 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05005138 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04005139
James Smartecfd03c2010-02-12 14:41:27 -05005140 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04005141 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05005142 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005143
James Smart4d9ab992009-10-02 15:16:39 -04005144 /* At this point, all discovery is aborted */
5145 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04005146
5147 /* Unregister VPIs */
5148 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05005149 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04005150 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05005151 /* Stop FLOGI/FDISC retries */
5152 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5153 if (ndlp)
5154 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005155 lpfc_cleanup_pending_mbox(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04005156 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05005157 shost = lpfc_shost_from_vport(vports[i]);
5158 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05005159 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05005160 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005161 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005162 }
5163 lpfc_destroy_vport_work_array(phba, vports);
5164
James Smart695a8142010-01-26 23:08:03 -05005165 /* Cleanup any outstanding ELS commands */
5166 lpfc_els_flush_all_cmd(phba);
5167
James Smart6fb120a2009-05-22 14:52:59 -04005168 /* Unregister VFI */
5169 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5170 if (!mbox) {
5171 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005172 "2556 UNREG_VFI mbox allocation failed"
5173 "HBA state x%x\n", phba->pport->port_state);
5174 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005175 }
5176
James Smart6669f9b2009-10-02 15:16:45 -04005177 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04005178 mbox->vport = phba->pport;
5179 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
5180
5181 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5182 if (rc == MBX_NOT_FINISHED) {
5183 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005184 "2557 UNREG_VFI issue mbox failed rc x%x "
5185 "HBA state x%x\n",
5186 rc, phba->pport->port_state);
James Smart6fb120a2009-05-22 14:52:59 -04005187 mempool_free(mbox, phba->mbox_mem_pool);
James Smartecfd03c2010-02-12 14:41:27 -05005188 return -EIO;
James Smart6fb120a2009-05-22 14:52:59 -04005189 }
5190
James Smart72100cc2010-02-12 14:43:01 -05005191 shost = lpfc_shost_from_vport(phba->pport);
5192 spin_lock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005193 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005194 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005195
James Smartecfd03c2010-02-12 14:41:27 -05005196 return 0;
5197}
5198
5199/**
5200 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5201 * @phba: Pointer to hba context object.
5202 *
5203 * This function issues synchronous unregister FCF mailbox command to HBA to
5204 * unregister the currently registered FCF record. The driver does not reset
5205 * the driver FCF usage state flags.
5206 *
5207 * Return 0 if successfully issued, none-zero otherwise.
5208 */
5209int
5210lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5211{
5212 LPFC_MBOXQ_t *mbox;
5213 int rc;
5214
James Smart6fb120a2009-05-22 14:52:59 -04005215 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5216 if (!mbox) {
5217 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005218 "2551 UNREG_FCFI mbox allocation failed"
5219 "HBA state x%x\n", phba->pport->port_state);
5220 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005221 }
James Smart6fb120a2009-05-22 14:52:59 -04005222 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5223 mbox->vport = phba->pport;
5224 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5225 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5226
5227 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005228 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5229 "2552 Unregister FCFI command failed rc x%x "
5230 "HBA state x%x\n",
5231 rc, phba->pport->port_state);
5232 return -EINVAL;
5233 }
5234 return 0;
5235}
5236
5237/**
5238 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5239 * @phba: Pointer to hba context object.
5240 *
5241 * This function unregisters the currently reigstered FCF. This function
5242 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5243 */
5244void
5245lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5246{
5247 int rc;
5248
5249 /* Preparation for unregistering fcf */
5250 rc = lpfc_unregister_fcf_prep(phba);
5251 if (rc) {
5252 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5253 "2748 Failed to prepare for unregistering "
5254 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005255 return;
5256 }
5257
James Smartecfd03c2010-02-12 14:41:27 -05005258 /* Now, unregister FCF record and reset HBA FCF state */
5259 rc = lpfc_sli4_unregister_fcf(phba);
5260 if (rc)
5261 return;
5262 /* Reset HBA FCF states after successful unregister FCF */
5263 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005264 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005265
5266 /*
5267 * If driver is not unloading, check if there is any other
5268 * FCF record that can be used for discovery.
5269 */
5270 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005271 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005272 return;
5273
James Smart0c9ab6f2010-02-26 14:15:57 -05005274 /* This is considered as the initial FCF discovery scan */
5275 spin_lock_irq(&phba->hbalock);
5276 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5277 spin_unlock_irq(&phba->hbalock);
5278 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005279
James Smart0c9ab6f2010-02-26 14:15:57 -05005280 if (rc) {
5281 spin_lock_irq(&phba->hbalock);
5282 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5283 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005284 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005285 "2553 lpfc_unregister_unused_fcf failed "
5286 "to read FCF record HBA state x%x\n",
5287 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005288 }
James Smartecfd03c2010-02-12 14:41:27 -05005289}
5290
5291/**
5292 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5293 * @phba: Pointer to hba context object.
5294 *
5295 * This function just unregisters the currently reigstered FCF. It does not
5296 * try to find another FCF for discovery.
5297 */
5298void
5299lpfc_unregister_fcf(struct lpfc_hba *phba)
5300{
5301 int rc;
5302
5303 /* Preparation for unregistering fcf */
5304 rc = lpfc_unregister_fcf_prep(phba);
5305 if (rc) {
5306 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5307 "2749 Failed to prepare for unregistering "
5308 "HBA's FCF record: rc=%d\n", rc);
5309 return;
5310 }
5311
5312 /* Now, unregister FCF record and reset HBA FCF state */
5313 rc = lpfc_sli4_unregister_fcf(phba);
5314 if (rc)
5315 return;
5316 /* Set proper HBA FCF states after successful unregister FCF */
5317 spin_lock_irq(&phba->hbalock);
5318 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
5319 spin_unlock_irq(&phba->hbalock);
5320}
5321
5322/**
5323 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
5324 * @phba: Pointer to hba context object.
5325 *
5326 * This function check if there are any connected remote port for the FCF and
5327 * if all the devices are disconnected, this function unregister FCFI.
5328 * This function also tries to use another FCF for discovery.
5329 */
5330void
5331lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
5332{
5333 /*
5334 * If HBA is not running in FIP mode or if HBA does not support
5335 * FCoE or if FCF is not registered, do nothing.
5336 */
5337 spin_lock_irq(&phba->hbalock);
5338 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
5339 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04005340 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
5341 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05005342 spin_unlock_irq(&phba->hbalock);
5343 return;
5344 }
5345 spin_unlock_irq(&phba->hbalock);
5346
5347 if (lpfc_fcf_inuse(phba))
5348 return;
5349
5350 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005351}
5352
5353/**
5354 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
5355 * @phba: Pointer to hba context object.
5356 * @buff: Buffer containing the FCF connection table as in the config
5357 * region.
5358 * This function create driver data structure for the FCF connection
5359 * record table read from config region 23.
5360 */
5361static void
5362lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
5363 uint8_t *buff)
5364{
5365 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5366 struct lpfc_fcf_conn_hdr *conn_hdr;
5367 struct lpfc_fcf_conn_rec *conn_rec;
5368 uint32_t record_count;
5369 int i;
5370
5371 /* Free the current connect table */
5372 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005373 &phba->fcf_conn_rec_list, list) {
5374 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04005375 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005376 }
James Smart6fb120a2009-05-22 14:52:59 -04005377
5378 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
5379 record_count = conn_hdr->length * sizeof(uint32_t)/
5380 sizeof(struct lpfc_fcf_conn_rec);
5381
5382 conn_rec = (struct lpfc_fcf_conn_rec *)
5383 (buff + sizeof(struct lpfc_fcf_conn_hdr));
5384
5385 for (i = 0; i < record_count; i++) {
5386 if (!(conn_rec[i].flags & FCFCNCT_VALID))
5387 continue;
5388 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
5389 GFP_KERNEL);
5390 if (!conn_entry) {
5391 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5392 "2566 Failed to allocate connection"
5393 " table entry\n");
5394 return;
5395 }
5396
5397 memcpy(&conn_entry->conn_rec, &conn_rec[i],
5398 sizeof(struct lpfc_fcf_conn_rec));
5399 conn_entry->conn_rec.vlan_tag =
5400 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
5401 conn_entry->conn_rec.flags =
5402 le16_to_cpu(conn_entry->conn_rec.flags);
5403 list_add_tail(&conn_entry->list,
5404 &phba->fcf_conn_rec_list);
5405 }
5406}
5407
5408/**
5409 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
5410 * @phba: Pointer to hba context object.
5411 * @buff: Buffer containing the FCoE parameter data structure.
5412 *
5413 * This function update driver data structure with config
5414 * parameters read from config region 23.
5415 */
5416static void
5417lpfc_read_fcoe_param(struct lpfc_hba *phba,
5418 uint8_t *buff)
5419{
5420 struct lpfc_fip_param_hdr *fcoe_param_hdr;
5421 struct lpfc_fcoe_params *fcoe_param;
5422
5423 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
5424 buff;
5425 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04005426 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04005427
5428 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
5429 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
5430 return;
5431
James Smart6fb120a2009-05-22 14:52:59 -04005432 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
5433 phba->valid_vlan = 1;
5434 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
5435 0xFFF;
5436 }
5437
5438 phba->fc_map[0] = fcoe_param->fc_map[0];
5439 phba->fc_map[1] = fcoe_param->fc_map[1];
5440 phba->fc_map[2] = fcoe_param->fc_map[2];
5441 return;
5442}
5443
5444/**
5445 * lpfc_get_rec_conf23 - Get a record type in config region data.
5446 * @buff: Buffer containing config region 23 data.
5447 * @size: Size of the data buffer.
5448 * @rec_type: Record type to be searched.
5449 *
5450 * This function searches config region data to find the begining
5451 * of the record specified by record_type. If record found, this
5452 * function return pointer to the record else return NULL.
5453 */
5454static uint8_t *
5455lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
5456{
5457 uint32_t offset = 0, rec_length;
5458
5459 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
5460 (size < sizeof(uint32_t)))
5461 return NULL;
5462
5463 rec_length = buff[offset + 1];
5464
5465 /*
5466 * One TLV record has one word header and number of data words
5467 * specified in the rec_length field of the record header.
5468 */
5469 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
5470 <= size) {
5471 if (buff[offset] == rec_type)
5472 return &buff[offset];
5473
5474 if (buff[offset] == LPFC_REGION23_LAST_REC)
5475 return NULL;
5476
5477 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
5478 rec_length = buff[offset + 1];
5479 }
5480 return NULL;
5481}
5482
5483/**
5484 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
5485 * @phba: Pointer to lpfc_hba data structure.
5486 * @buff: Buffer containing config region 23 data.
5487 * @size: Size of the data buffer.
5488 *
5489 * This fuction parse the FCoE config parameters in config region 23 and
5490 * populate driver data structure with the parameters.
5491 */
5492void
5493lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
5494 uint8_t *buff,
5495 uint32_t size)
5496{
5497 uint32_t offset = 0, rec_length;
5498 uint8_t *rec_ptr;
5499
5500 /*
5501 * If data size is less than 2 words signature and version cannot be
5502 * verified.
5503 */
5504 if (size < 2*sizeof(uint32_t))
5505 return;
5506
5507 /* Check the region signature first */
5508 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
5509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5510 "2567 Config region 23 has bad signature\n");
5511 return;
5512 }
5513
5514 offset += 4;
5515
5516 /* Check the data structure version */
5517 if (buff[offset] != LPFC_REGION23_VERSION) {
5518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5519 "2568 Config region 23 has bad version\n");
5520 return;
5521 }
5522 offset += 4;
5523
5524 rec_length = buff[offset + 1];
5525
5526 /* Read FCoE param record */
5527 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5528 size - offset, FCOE_PARAM_TYPE);
5529 if (rec_ptr)
5530 lpfc_read_fcoe_param(phba, rec_ptr);
5531
5532 /* Read FCF connection table */
5533 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5534 size - offset, FCOE_CONN_TBL_TYPE);
5535 if (rec_ptr)
5536 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5537
5538}