blob: 8082d69ea73077e60495b5d48649089e63851811 [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) &&
279 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500280 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400281
282 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500283}
James Smartc01f3202006-08-18 17:47:08 -0400284
James Smartea2151b2008-09-07 11:52:10 -0400285/**
James Smart3621a712009-04-06 18:47:14 -0400286 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400287 * @phba: Pointer to hba context object.
288 *
289 * This function is called from the functions which need to post
290 * events from interrupt context. This function allocates data
291 * structure required for posting event. It also keeps track of
292 * number of events pending and prevent event storm when there are
293 * too many events.
294 **/
295struct lpfc_fast_path_event *
296lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
297 struct lpfc_fast_path_event *ret;
298
299 /* If there are lot of fast event do not exhaust memory due to this */
300 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
301 return NULL;
302
303 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
304 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400305 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400306 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400307 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
308 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
309 }
James Smartea2151b2008-09-07 11:52:10 -0400310 return ret;
311}
312
313/**
James Smart3621a712009-04-06 18:47:14 -0400314 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400315 * @phba: Pointer to hba context object.
316 * @evt: Event object which need to be freed.
317 *
318 * This function frees the data structure required for posting
319 * events.
320 **/
321void
322lpfc_free_fast_evt(struct lpfc_hba *phba,
323 struct lpfc_fast_path_event *evt) {
324
325 atomic_dec(&phba->fast_event_count);
326 kfree(evt);
327}
328
329/**
James Smart3621a712009-04-06 18:47:14 -0400330 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400331 * @phba: Pointer to hba context object.
332 * @evtp: Event data structure.
333 *
334 * This function is called from worker thread, when the interrupt
335 * context need to post an event. This function posts the event
336 * to fc transport netlink interface.
337 **/
338static void
339lpfc_send_fastpath_evt(struct lpfc_hba *phba,
340 struct lpfc_work_evt *evtp)
341{
342 unsigned long evt_category, evt_sub_category;
343 struct lpfc_fast_path_event *fast_evt_data;
344 char *evt_data;
345 uint32_t evt_data_size;
346 struct Scsi_Host *shost;
347
348 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
349 work_evt);
350
351 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
352 evt_sub_category = (unsigned long) fast_evt_data->un.
353 fabric_evt.subcategory;
354 shost = lpfc_shost_from_vport(fast_evt_data->vport);
355 if (evt_category == FC_REG_FABRIC_EVENT) {
356 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
357 evt_data = (char *) &fast_evt_data->un.read_check_error;
358 evt_data_size = sizeof(fast_evt_data->un.
359 read_check_error);
360 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500361 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400362 evt_data = (char *) &fast_evt_data->un.fabric_evt;
363 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
364 } else {
365 lpfc_free_fast_evt(phba, fast_evt_data);
366 return;
367 }
368 } else if (evt_category == FC_REG_SCSI_EVENT) {
369 switch (evt_sub_category) {
370 case LPFC_EVENT_QFULL:
371 case LPFC_EVENT_DEVBSY:
372 evt_data = (char *) &fast_evt_data->un.scsi_evt;
373 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
374 break;
375 case LPFC_EVENT_CHECK_COND:
376 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
377 evt_data_size = sizeof(fast_evt_data->un.
378 check_cond_evt);
379 break;
380 case LPFC_EVENT_VARQUEDEPTH:
381 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
382 evt_data_size = sizeof(fast_evt_data->un.
383 queue_depth_evt);
384 break;
385 default:
386 lpfc_free_fast_evt(phba, fast_evt_data);
387 return;
388 }
389 } else {
390 lpfc_free_fast_evt(phba, fast_evt_data);
391 return;
392 }
393
394 fc_host_post_vendor_event(shost,
395 fc_get_event_number(),
396 evt_data_size,
397 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500398 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400399
400 lpfc_free_fast_evt(phba, fast_evt_data);
401 return;
402}
403
dea31012005-04-17 16:05:31 -0500404static void
James Smart2e0fef82007-06-17 19:56:36 -0500405lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500406{
407 struct lpfc_work_evt *evtp = NULL;
408 struct lpfc_nodelist *ndlp;
409 int free_evt;
410
James Smart2e0fef82007-06-17 19:56:36 -0500411 spin_lock_irq(&phba->hbalock);
412 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500413 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
414 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500415 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500416 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500417 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500418 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500419 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500420 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500421 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500422 /* decrement the node reference count held
423 * for this queued work
424 */
425 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500426 break;
James Smart858c9f62007-06-17 19:56:39 -0500427 case LPFC_EVT_DEV_LOSS:
428 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500429 lpfc_dev_loss_tmo_handler(ndlp);
430 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500431 /* decrement the node reference count held for
432 * this queued work
433 */
James Smart858c9f62007-06-17 19:56:39 -0500434 lpfc_nlp_put(ndlp);
435 break;
dea31012005-04-17 16:05:31 -0500436 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500437 if (phba->link_state < LPFC_LINK_DOWN)
438 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500439 else
James Smart2e0fef82007-06-17 19:56:36 -0500440 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500441 complete((struct completion *)(evtp->evt_arg2));
442 break;
James Smart46fa3112007-04-25 09:51:45 -0400443 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500444 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400445 lpfc_offline_prep(phba);
446 *(int *)(evtp->evt_arg1) = 0;
447 complete((struct completion *)(evtp->evt_arg2));
448 break;
449 case LPFC_EVT_OFFLINE:
450 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500451 lpfc_sli_brdrestart(phba);
452 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400453 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
454 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500455 complete((struct completion *)(evtp->evt_arg2));
456 break;
457 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400458 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500459 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500460 lpfc_sli_brdreset(phba);
461 lpfc_hba_down_post(phba);
462 *(int *)(evtp->evt_arg1) =
463 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400464 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500465 complete((struct completion *)(evtp->evt_arg2));
466 break;
467 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400468 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500469 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500470 = (phba->pport->stopped)
471 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400472 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500473 complete((struct completion *)(evtp->evt_arg2));
474 break;
James Smartea2151b2008-09-07 11:52:10 -0400475 case LPFC_EVT_FASTPATH_MGMT_EVT:
476 lpfc_send_fastpath_evt(phba, evtp);
477 free_evt = 0;
478 break;
James Smart78730cf2010-04-06 15:06:30 -0400479 case LPFC_EVT_RESET_HBA:
480 if (!(phba->pport->load_flag & FC_UNLOADING))
481 lpfc_reset_hba(phba);
482 break;
dea31012005-04-17 16:05:31 -0500483 }
484 if (free_evt)
485 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500486 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500487 }
James Smart2e0fef82007-06-17 19:56:36 -0500488 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500489
490}
491
James Smart311464e2007-08-02 11:10:37 -0400492static void
James Smart2e0fef82007-06-17 19:56:36 -0500493lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500494{
495 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500496 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400497 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400498 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400499 int i;
dea31012005-04-17 16:05:31 -0500500
James Smart2e0fef82007-06-17 19:56:36 -0500501 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500502 ha_copy = phba->work_ha;
503 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500504 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500505
James Smartda0436e2009-05-22 14:51:39 -0400506 /* First, try to post the next mailbox command to SLI4 device */
507 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
508 lpfc_sli4_post_async_mbox(phba);
509
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500510 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400511 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500512 lpfc_handle_eratt(phba);
513
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500514 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500515 lpfc_sli_handle_mb_event(phba);
516
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500517 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500518 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400519
James Smartda0436e2009-05-22 14:51:39 -0400520 /* Process SLI4 events */
521 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
522 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
523 lpfc_sli4_fcp_xri_abort_event_proc(phba);
524 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
525 lpfc_sli4_els_xri_abort_event_proc(phba);
526 if (phba->hba_flag & ASYNC_EVENT)
527 lpfc_sli4_async_event_proc(phba);
528 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
529 spin_lock_irq(&phba->hbalock);
530 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
531 spin_unlock_irq(&phba->hbalock);
532 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
533 }
James Smartecfd03c2010-02-12 14:41:27 -0500534 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
535 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400536 }
537
James Smart549e55c2007-08-02 11:09:51 -0400538 vports = lpfc_create_vport_work_array(phba);
539 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400540 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400541 /*
542 * We could have no vports in array if unloading, so if
543 * this happens then just use the pport
544 */
545 if (vports[i] == NULL && i == 0)
546 vport = phba->pport;
547 else
548 vport = vports[i];
549 if (vport == NULL)
550 break;
James Smart58da1ff2008-04-07 10:15:56 -0400551 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400552 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400553 vport->work_port_events &= ~work_port_events;
554 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400555 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400556 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400557 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400558 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400559 if (work_port_events & WORKER_HB_TMO)
560 lpfc_hb_timeout_handler(phba);
561 if (work_port_events & WORKER_MBOX_TMO)
562 lpfc_mbox_timeout_handler(phba);
563 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
564 lpfc_unblock_fabric_iocbs(phba);
565 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400566 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400567 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
568 lpfc_ramp_down_queue_handler(phba);
569 if (work_port_events & WORKER_RAMP_UP_QUEUE)
570 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500571 }
James Smart09372822008-01-11 01:52:54 -0500572 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500573
James Smart858c9f62007-06-17 19:56:39 -0500574 pring = &phba->sli.ring[LPFC_ELS_RING];
575 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
576 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400577 if ((status & HA_RXMASK) ||
578 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400579 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400580 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500581 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400582 /* Set the lpfc data pending flag */
583 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500584 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400585 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500586 lpfc_sli_handle_slow_ring_event(phba, pring,
587 (status &
588 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500589 }
590 /*
591 * Turn on Ring interrupts
592 */
James Smart3772a992009-05-22 14:50:54 -0400593 if (phba->sli_rev <= LPFC_SLI_REV3) {
594 spin_lock_irq(&phba->hbalock);
595 control = readl(phba->HCregaddr);
596 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
597 lpfc_debugfs_slow_ring_trc(phba,
598 "WRK Enable ring: cntl:x%x hacopy:x%x",
599 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400600
James Smart3772a992009-05-22 14:50:54 -0400601 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
602 writel(control, phba->HCregaddr);
603 readl(phba->HCregaddr); /* flush */
604 } else {
605 lpfc_debugfs_slow_ring_trc(phba,
606 "WRK Ring ok: cntl:x%x hacopy:x%x",
607 control, ha_copy, 0);
608 }
609 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500610 }
611 }
James Smart2e0fef82007-06-17 19:56:36 -0500612 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500613}
614
dea31012005-04-17 16:05:31 -0500615int
616lpfc_do_work(void *p)
617{
618 struct lpfc_hba *phba = p;
619 int rc;
dea31012005-04-17 16:05:31 -0500620
621 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400622 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500623
James Smart3a55b532008-12-04 22:38:54 -0500624 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400625 /* wait and check worker queue activities */
626 rc = wait_event_interruptible(phba->work_waitq,
627 (test_and_clear_bit(LPFC_DATA_READY,
628 &phba->data_flags)
629 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500630 /* Signal wakeup shall terminate the worker thread */
631 if (rc) {
632 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
633 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500634 break;
James Smart3a55b532008-12-04 22:38:54 -0500635 }
dea31012005-04-17 16:05:31 -0500636
James Smart5e9d9b82008-06-14 22:52:53 -0400637 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500638 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500639 }
James Smart3a55b532008-12-04 22:38:54 -0500640 phba->worker_thread = NULL;
641 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
642 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500643 return 0;
644}
645
646/*
647 * This is only called to handle FC worker events. Since this a rare
648 * occurance, we allocate a struct lpfc_work_evt structure here instead of
649 * embedding it in the IOCB.
650 */
651int
James Smart2e0fef82007-06-17 19:56:36 -0500652lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500653 uint32_t evt)
654{
655 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500656 unsigned long flags;
dea31012005-04-17 16:05:31 -0500657
658 /*
659 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
660 * be queued to worker thread for processing
661 */
James Smart92d7f7b2007-06-17 19:56:38 -0500662 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500663 if (!evtp)
664 return 0;
665
666 evtp->evt_arg1 = arg1;
667 evtp->evt_arg2 = arg2;
668 evtp->evt = evt;
669
James Smarted957682007-06-17 19:56:37 -0500670 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400671 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500672 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500673
James Smart5e9d9b82008-06-14 22:52:53 -0400674 lpfc_worker_wake_up(phba);
675
dea31012005-04-17 16:05:31 -0500676 return 1;
677}
678
James Smart92d7f7b2007-06-17 19:56:38 -0500679void
680lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
681{
James Smart09372822008-01-11 01:52:54 -0500682 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500683 struct lpfc_hba *phba = vport->phba;
684 struct lpfc_nodelist *ndlp, *next_ndlp;
685 int rc;
686
687 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500688 if (!NLP_CHK_NODE_ACT(ndlp))
689 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500690 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
691 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400692 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
693 ((vport->port_type == LPFC_NPIV_PORT) &&
694 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500695 lpfc_unreg_rpi(vport, ndlp);
696
697 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400698 if ((phba->sli_rev < LPFC_SLI_REV4) &&
699 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500700 continue;
701 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
702 remove
703 ? NLP_EVT_DEVICE_RM
704 : NLP_EVT_DEVICE_RECOVERY);
705 }
706 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
707 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500708 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500709 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500710 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500711 }
712}
713
James Smart87af33f2007-10-27 13:37:43 -0400714void
James Smart98c9ea52007-10-27 13:37:33 -0400715lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500716{
James Smart695a8142010-01-26 23:08:03 -0500717 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
718
James Smart45ed1192009-10-02 15:17:02 -0400719 /* Cleanup any outstanding received buffers */
720 lpfc_cleanup_rcv_buffers(vport);
721
James Smart92d7f7b2007-06-17 19:56:38 -0500722 /* Cleanup any outstanding RSCN activity */
723 lpfc_els_flush_rscn(vport);
724
725 /* Cleanup any outstanding ELS commands */
726 lpfc_els_flush_cmd(vport);
727
728 lpfc_cleanup_rpis(vport, 0);
729
James Smart92d7f7b2007-06-17 19:56:38 -0500730 /* Turn off discovery timer if its running */
731 lpfc_can_disctmo(vport);
732}
733
James Smart3772a992009-05-22 14:50:54 -0400734void
James Smart98c9ea52007-10-27 13:37:33 -0400735lpfc_linkdown_port(struct lpfc_vport *vport)
736{
737 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
738
739 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
740
741 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
742 "Link Down: state:x%x rtry:x%x flg:x%x",
743 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
744
745 lpfc_port_link_failure(vport);
746
747}
748
dea31012005-04-17 16:05:31 -0500749int
James Smart685f0bf2007-04-25 09:53:08 -0400750lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500751{
James Smart2e0fef82007-06-17 19:56:36 -0500752 struct lpfc_vport *vport = phba->pport;
753 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400754 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400755 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400756 int i;
dea31012005-04-17 16:05:31 -0500757
James Smart3163f722008-02-08 18:50:25 -0500758 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500759 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500760
761 /* Block all SCSI stack I/Os */
762 lpfc_scsi_dev_block(phba);
763
James Smart2e0fef82007-06-17 19:56:36 -0500764 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500765 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500766 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500767 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500768 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500769 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500770 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500771 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500772 }
James Smart549e55c2007-08-02 11:09:51 -0400773 vports = lpfc_create_vport_work_array(phba);
774 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400775 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400776 /* Issue a LINK DOWN event to all nodes */
777 lpfc_linkdown_port(vports[i]);
778 }
James Smart09372822008-01-11 01:52:54 -0500779 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500780 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500781 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
782 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500783 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500784 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500785 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400786 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500787 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500788 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500789 }
790 }
791
dea31012005-04-17 16:05:31 -0500792 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500793 if (phba->pport->fc_flag & FC_PT2PT) {
794 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500795 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
796 if (mb) {
dea31012005-04-17 16:05:31 -0500797 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500798 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500799 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400800 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500801 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500802 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500803 }
804 }
James Smart2e0fef82007-06-17 19:56:36 -0500805 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500806 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500807 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500808 }
James Smart2e0fef82007-06-17 19:56:36 -0500809
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500810 return 0;
dea31012005-04-17 16:05:31 -0500811}
812
James Smart92d7f7b2007-06-17 19:56:38 -0500813static void
814lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500815{
James Smart92d7f7b2007-06-17 19:56:38 -0500816 struct lpfc_nodelist *ndlp;
817
818 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500819 if (!NLP_CHK_NODE_ACT(ndlp))
820 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500821 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
822 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500823 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500824 /* On Linkup its safe to clean up the ndlp
825 * from Fabric connections.
826 */
James Smart92d7f7b2007-06-17 19:56:38 -0500827 if (ndlp->nlp_DID != Fabric_DID)
828 lpfc_unreg_rpi(vport, ndlp);
829 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
830 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500831 /* Fail outstanding IO now since device is
832 * marked for PLOGI.
833 */
James Smart92d7f7b2007-06-17 19:56:38 -0500834 lpfc_unreg_rpi(vport, ndlp);
835 }
836 }
837}
838
839static void
840lpfc_linkup_port(struct lpfc_vport *vport)
841{
842 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500843 struct lpfc_hba *phba = vport->phba;
844
845 if ((vport->load_flag & FC_UNLOADING) != 0)
846 return;
847
James Smart858c9f62007-06-17 19:56:39 -0500848 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
849 "Link Up: top:x%x speed:x%x flg:x%x",
850 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
851
James Smart92d7f7b2007-06-17 19:56:38 -0500852 /* If NPIV is not enabled, only bring the physical port up */
853 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
854 (vport != phba->pport))
855 return;
dea31012005-04-17 16:05:31 -0500856
James Smart2e0fef82007-06-17 19:56:36 -0500857 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400858
James Smart2e0fef82007-06-17 19:56:36 -0500859 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500860 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
861 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
862 vport->fc_flag |= FC_NDISC_ACTIVE;
863 vport->fc_ns_retry = 0;
864 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500865
James Smart92d7f7b2007-06-17 19:56:38 -0500866 if (vport->fc_flag & FC_LBIT)
867 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500868
James Smart92d7f7b2007-06-17 19:56:38 -0500869}
870
871static int
872lpfc_linkup(struct lpfc_hba *phba)
873{
James Smart549e55c2007-08-02 11:09:51 -0400874 struct lpfc_vport **vports;
875 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500876
877 phba->link_state = LPFC_LINK_UP;
878
879 /* Unblock fabric iocbs if they are blocked */
880 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
881 del_timer_sync(&phba->fabric_block_timer);
882
James Smart549e55c2007-08-02 11:09:51 -0400883 vports = lpfc_create_vport_work_array(phba);
884 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400885 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400886 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500887 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400888 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
889 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500890 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500891
892 return 0;
893}
894
895/*
896 * This routine handles processing a CLEAR_LA mailbox
897 * command upon completion. It is setup in the LPFC_MBOXQ
898 * as the completion routine when the command is
899 * handed off to the SLI layer.
900 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100901static void
James Smart2e0fef82007-06-17 19:56:36 -0500902lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500903{
James Smart2e0fef82007-06-17 19:56:36 -0500904 struct lpfc_vport *vport = pmb->vport;
905 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
906 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400907 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500908 uint32_t control;
909
dea31012005-04-17 16:05:31 -0500910 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500911 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500912 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
913 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
914
915 /* Check for error */
916 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500917 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400918 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
919 "0320 CLEAR_LA mbxStatus error x%x hba "
920 "state x%x\n",
921 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500922 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500923 goto out;
924 }
925
James Smart92d7f7b2007-06-17 19:56:38 -0500926 if (vport->port_type == LPFC_PHYSICAL_PORT)
927 phba->link_state = LPFC_HBA_READY;
928
929 spin_lock_irq(&phba->hbalock);
930 psli->sli_flag |= LPFC_PROCESS_LA;
931 control = readl(phba->HCregaddr);
932 control |= HC_LAINT_ENA;
933 writel(control, phba->HCregaddr);
934 readl(phba->HCregaddr); /* flush */
935 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500936 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500937 return;
dea31012005-04-17 16:05:31 -0500938
dea31012005-04-17 16:05:31 -0500939out:
940 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400941 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
942 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500943 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500944
James Smart2e0fef82007-06-17 19:56:36 -0500945 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400946 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500947 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500948
James Smart2e0fef82007-06-17 19:56:36 -0500949 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500950
951 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500952
953 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500954 psli->sli_flag |= LPFC_PROCESS_LA;
955 control = readl(phba->HCregaddr);
956 control |= HC_LAINT_ENA;
957 writel(control, phba->HCregaddr);
958 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500959 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500960
961 return;
962}
963
James Smart2e0fef82007-06-17 19:56:36 -0500964
dea31012005-04-17 16:05:31 -0500965static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500966lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500967{
James Smart2e0fef82007-06-17 19:56:36 -0500968 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500969
James Smart04c68492009-05-22 14:52:52 -0400970 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500971 goto out;
dea31012005-04-17 16:05:31 -0500972
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500973 mempool_free(pmb, phba->mbox_mem_pool);
974
975 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500976 vport->fc_flag & FC_PUBLIC_LOOP &&
977 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500978 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500979 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500980 * LPFC_LOCAL_CFG_LINK while waiting for FAN
981 */
James Smart2e0fef82007-06-17 19:56:36 -0500982 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500983 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500984 }
dea31012005-04-17 16:05:31 -0500985
James Smart2e0fef82007-06-17 19:56:36 -0500986 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500987 * LPFC_FLOGI while waiting for FLOGI cmpl
988 */
James Smart92d7f7b2007-06-17 19:56:38 -0500989 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500990 lpfc_initial_flogi(vport);
991 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500992 return;
dea31012005-04-17 16:05:31 -0500993
994out:
James Smarte8b62012007-08-02 11:10:09 -0400995 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
996 "0306 CONFIG_LINK mbxStatus error x%x "
997 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400998 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500999 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001000
1001 lpfc_linkdown(phba);
1002
James Smarte8b62012007-08-02 11:10:09 -04001003 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1004 "0200 CONFIG_LINK bad hba state x%x\n",
1005 vport->port_state);
dea31012005-04-17 16:05:31 -05001006
James Smart92d7f7b2007-06-17 19:56:38 -05001007 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001008 return;
1009}
1010
1011static void
James Smart6fb120a2009-05-22 14:52:59 -04001012lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1013{
1014 struct lpfc_vport *vport = mboxq->vport;
1015 unsigned long flags;
1016
1017 if (mboxq->u.mb.mbxStatus) {
1018 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1019 "2017 REG_FCFI mbxStatus error x%x "
1020 "HBA state x%x\n",
1021 mboxq->u.mb.mbxStatus, vport->port_state);
1022 mempool_free(mboxq, phba->mbox_mem_pool);
1023 return;
1024 }
1025
1026 /* Start FCoE discovery by sending a FLOGI. */
1027 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1028 /* Set the FCFI registered flag */
1029 spin_lock_irqsave(&phba->hbalock, flags);
1030 phba->fcf.fcf_flag |= FCF_REGISTERED;
1031 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart32b97932009-07-19 10:01:21 -04001032 /* If there is a pending FCoE event, restart FCF table scan. */
1033 if (lpfc_check_pending_fcoe_event(phba, 1)) {
1034 mempool_free(mboxq, phba->mbox_mem_pool);
1035 return;
1036 }
James Smart1c6f4ef52009-11-18 15:40:49 -05001037 spin_lock_irqsave(&phba->hbalock, flags);
James Smartecfd03c2010-02-12 14:41:27 -05001038 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart4d9ab992009-10-02 15:16:39 -04001039 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001040 spin_unlock_irqrestore(&phba->hbalock, flags);
1041 if (vport->port_state != LPFC_FLOGI)
James Smart6fb120a2009-05-22 14:52:59 -04001042 lpfc_initial_flogi(vport);
James Smart6fb120a2009-05-22 14:52:59 -04001043
1044 mempool_free(mboxq, phba->mbox_mem_pool);
1045 return;
1046}
1047
1048/**
1049 * lpfc_fab_name_match - Check if the fcf fabric name match.
1050 * @fab_name: pointer to fabric name.
1051 * @new_fcf_record: pointer to fcf record.
1052 *
1053 * This routine compare the fcf record's fabric name with provided
1054 * fabric name. If the fabric name are identical this function
1055 * returns 1 else return 0.
1056 **/
1057static uint32_t
1058lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1059{
James Smartecfd03c2010-02-12 14:41:27 -05001060 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001061 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001062 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1063 return 0;
1064 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1065 return 0;
1066 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1067 return 0;
1068 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1069 return 0;
1070 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1071 return 0;
1072 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1073 return 0;
1074 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1075 return 0;
1076 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001077}
1078
1079/**
James Smart8fa38512009-07-19 10:01:03 -04001080 * lpfc_sw_name_match - Check if the fcf switch name match.
1081 * @fab_name: pointer to fabric name.
1082 * @new_fcf_record: pointer to fcf record.
1083 *
1084 * This routine compare the fcf record's switch name with provided
1085 * switch name. If the switch name are identical this function
1086 * returns 1 else return 0.
1087 **/
1088static uint32_t
1089lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1090{
James Smartecfd03c2010-02-12 14:41:27 -05001091 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001092 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001093 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1094 return 0;
1095 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1096 return 0;
1097 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1098 return 0;
1099 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1100 return 0;
1101 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1102 return 0;
1103 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1104 return 0;
1105 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1106 return 0;
1107 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001108}
1109
1110/**
James Smart6fb120a2009-05-22 14:52:59 -04001111 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001112 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001113 * @new_fcf_record: pointer to fcf record.
1114 *
1115 * This routine compare the fcf record's mac address with HBA's
1116 * FCF mac address. If the mac addresses are identical this function
1117 * returns 1 else return 0.
1118 **/
1119static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001120lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001121{
James Smartecfd03c2010-02-12 14:41:27 -05001122 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001123 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001124 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1125 return 0;
1126 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1127 return 0;
1128 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1129 return 0;
1130 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1131 return 0;
1132 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1133 return 0;
1134 return 1;
1135}
1136
1137static bool
1138lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1139{
1140 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001141}
1142
1143/**
1144 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001145 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001146 * @new_fcf_record: pointer to fcf record.
1147 *
1148 * This routine copies the FCF information from the FCF
1149 * record to lpfc_hba data structure.
1150 **/
1151static void
James Smartecfd03c2010-02-12 14:41:27 -05001152lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1153 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001154{
James Smartecfd03c2010-02-12 14:41:27 -05001155 /* Fabric name */
1156 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001157 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001158 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001159 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001160 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001161 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001162 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001163 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001164 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001165 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001166 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001167 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001168 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001169 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001170 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001171 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001172 /* Mac address */
1173 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1174 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1175 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1176 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1177 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1178 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1179 /* FCF record index */
1180 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1181 /* FCF record priority */
1182 fcf_rec->priority = new_fcf_record->fip_priority;
1183 /* Switch name */
1184 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001185 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001186 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001187 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001188 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001189 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001190 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001191 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001192 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001193 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001194 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001195 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001196 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001197 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001198 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001199 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001200}
1201
1202/**
James Smartecfd03c2010-02-12 14:41:27 -05001203 * lpfc_update_fcf_record - Update driver fcf record
1204 * @phba: pointer to lpfc hba data structure.
1205 * @fcf_rec: pointer to driver fcf record.
1206 * @new_fcf_record: pointer to hba fcf record.
1207 * @addr_mode: address mode to be set to the driver fcf record.
1208 * @vlan_id: vlan tag to be set to the driver fcf record.
1209 * @flag: flag bits to be set to the driver fcf record.
1210 *
1211 * This routine updates the driver FCF record from the new HBA FCF record
1212 * together with the address mode, vlan_id, and other informations. This
1213 * routine is called with the host lock held.
1214 **/
1215static void
1216__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1217 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1218 uint16_t vlan_id, uint32_t flag)
1219{
1220 /* Copy the fields from the HBA's FCF record */
1221 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1222 /* Update other fields of driver FCF record */
1223 fcf_rec->addr_mode = addr_mode;
1224 fcf_rec->vlan_id = vlan_id;
1225 fcf_rec->flag |= (flag | RECORD_VALID);
1226}
1227
1228/**
James Smart6fb120a2009-05-22 14:52:59 -04001229 * lpfc_register_fcf - Register the FCF with hba.
1230 * @phba: pointer to lpfc hba data structure.
1231 *
1232 * This routine issues a register fcfi mailbox command to register
1233 * the fcf with HBA.
1234 **/
1235static void
1236lpfc_register_fcf(struct lpfc_hba *phba)
1237{
1238 LPFC_MBOXQ_t *fcf_mbxq;
1239 int rc;
1240 unsigned long flags;
1241
1242 spin_lock_irqsave(&phba->hbalock, flags);
1243
1244 /* If the FCF is not availabe do nothing. */
1245 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smart4d9ab992009-10-02 15:16:39 -04001246 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001247 spin_unlock_irqrestore(&phba->hbalock, flags);
1248 return;
1249 }
1250
1251 /* The FCF is already registered, start discovery */
1252 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001253 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart32b97932009-07-19 10:01:21 -04001254 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001255 spin_unlock_irqrestore(&phba->hbalock, flags);
1256 if (phba->pport->port_state != LPFC_FLOGI)
1257 lpfc_initial_flogi(phba->pport);
1258 return;
1259 }
1260 spin_unlock_irqrestore(&phba->hbalock, flags);
1261
1262 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1263 GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001264 if (!fcf_mbxq) {
1265 spin_lock_irqsave(&phba->hbalock, flags);
1266 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1267 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001268 return;
James Smart4d9ab992009-10-02 15:16:39 -04001269 }
James Smart6fb120a2009-05-22 14:52:59 -04001270
1271 lpfc_reg_fcfi(phba, fcf_mbxq);
1272 fcf_mbxq->vport = phba->pport;
1273 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1274 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001275 if (rc == MBX_NOT_FINISHED) {
1276 spin_lock_irqsave(&phba->hbalock, flags);
1277 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1278 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001279 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001280 }
James Smart6fb120a2009-05-22 14:52:59 -04001281
1282 return;
1283}
1284
1285/**
1286 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1287 * @phba: pointer to lpfc hba data structure.
1288 * @new_fcf_record: pointer to fcf record.
1289 * @boot_flag: Indicates if this record used by boot bios.
1290 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001291 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001292 *
1293 * This routine compare the fcf record with connect list obtained from the
1294 * config region to decide if this FCF can be used for SAN discovery. It returns
1295 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1296 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1297 * is used by boot bios and addr_mode will indicate the addressing mode to be
1298 * used for this FCF when the function returns.
1299 * If the FCF record need to be used with a particular vlan id, the vlan is
1300 * set in the vlan_id on return of the function. If not VLAN tagging need to
1301 * be used with the FCF vlan_id will be set to 0xFFFF;
1302 **/
1303static int
1304lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1305 struct fcf_record *new_fcf_record,
1306 uint32_t *boot_flag, uint32_t *addr_mode,
1307 uint16_t *vlan_id)
1308{
1309 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001310 int i, j, fcf_vlan_id = 0;
1311
1312 /* Find the lowest VLAN id in the FCF record */
1313 for (i = 0; i < 512; i++) {
1314 if (new_fcf_record->vlan_bitmap[i]) {
1315 fcf_vlan_id = i * 8;
1316 j = 0;
1317 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1318 j++;
1319 fcf_vlan_id++;
1320 }
1321 break;
1322 }
1323 }
James Smart6fb120a2009-05-22 14:52:59 -04001324
James Smart0c287582009-06-10 17:22:56 -04001325 /* If FCF not available return 0 */
1326 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1327 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1328 return 0;
1329
James Smart45ed1192009-10-02 15:17:02 -04001330 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001331 *boot_flag = 0;
1332 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1333 new_fcf_record);
1334 if (phba->valid_vlan)
1335 *vlan_id = phba->vlan_id;
1336 else
1337 *vlan_id = 0xFFFF;
1338 return 1;
1339 }
1340
1341 /*
1342 * If there are no FCF connection table entry, driver connect to all
1343 * FCFs.
1344 */
1345 if (list_empty(&phba->fcf_conn_rec_list)) {
1346 *boot_flag = 0;
1347 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1348 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001349
1350 /*
1351 * When there are no FCF connect entries, use driver's default
1352 * addressing mode - FPMA.
1353 */
1354 if (*addr_mode & LPFC_FCF_FPMA)
1355 *addr_mode = LPFC_FCF_FPMA;
1356
James Smart4d9ab992009-10-02 15:16:39 -04001357 /* If FCF record report a vlan id use that vlan id */
1358 if (fcf_vlan_id)
1359 *vlan_id = fcf_vlan_id;
1360 else
1361 *vlan_id = 0xFFFF;
James Smart6fb120a2009-05-22 14:52:59 -04001362 return 1;
1363 }
1364
James Smartecfd03c2010-02-12 14:41:27 -05001365 list_for_each_entry(conn_entry,
1366 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001367 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1368 continue;
1369
1370 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1371 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001372 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001373 continue;
James Smart8fa38512009-07-19 10:01:03 -04001374 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1375 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1376 new_fcf_record))
1377 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001378 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1379 /*
1380 * If the vlan bit map does not have the bit set for the
1381 * vlan id to be used, then it is not a match.
1382 */
1383 if (!(new_fcf_record->vlan_bitmap
1384 [conn_entry->conn_rec.vlan_tag / 8] &
1385 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1386 continue;
1387 }
1388
1389 /*
James Smart0c287582009-06-10 17:22:56 -04001390 * If connection record does not support any addressing mode,
1391 * skip the FCF record.
1392 */
1393 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1394 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1395 continue;
1396
1397 /*
James Smart6fb120a2009-05-22 14:52:59 -04001398 * Check if the connection record specifies a required
1399 * addressing mode.
1400 */
1401 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1402 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1403
1404 /*
1405 * If SPMA required but FCF not support this continue.
1406 */
1407 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1408 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1409 new_fcf_record) & LPFC_FCF_SPMA))
1410 continue;
1411
1412 /*
1413 * If FPMA required but FCF not support this continue.
1414 */
1415 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1416 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1417 new_fcf_record) & LPFC_FCF_FPMA))
1418 continue;
1419 }
1420
1421 /*
1422 * This fcf record matches filtering criteria.
1423 */
1424 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1425 *boot_flag = 1;
1426 else
1427 *boot_flag = 0;
1428
James Smart0c287582009-06-10 17:22:56 -04001429 /*
1430 * If user did not specify any addressing mode, or if the
1431 * prefered addressing mode specified by user is not supported
1432 * by FCF, allow fabric to pick the addressing mode.
1433 */
James Smart6fb120a2009-05-22 14:52:59 -04001434 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1435 new_fcf_record);
1436 /*
1437 * If the user specified a required address mode, assign that
1438 * address mode
1439 */
1440 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1441 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1442 *addr_mode = (conn_entry->conn_rec.flags &
1443 FCFCNCT_AM_SPMA) ?
1444 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1445 /*
1446 * If the user specified a prefered address mode, use the
1447 * addr mode only if FCF support the addr_mode.
1448 */
1449 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1450 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1451 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1452 (*addr_mode & LPFC_FCF_SPMA))
1453 *addr_mode = LPFC_FCF_SPMA;
1454 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1455 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1456 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1457 (*addr_mode & LPFC_FCF_FPMA))
1458 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001459
James Smart4d9ab992009-10-02 15:16:39 -04001460 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001461 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1462 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001463 /*
1464 * If no vlan id is specified in connect list, use the vlan id
1465 * in the FCF record
1466 */
1467 else if (fcf_vlan_id)
1468 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001469 else
1470 *vlan_id = 0xFFFF;
1471
1472 return 1;
1473 }
1474
1475 return 0;
1476}
1477
1478/**
James Smart32b97932009-07-19 10:01:21 -04001479 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1480 * @phba: pointer to lpfc hba data structure.
1481 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1482 *
1483 * This function check if there is any fcoe event pending while driver
1484 * scan FCF entries. If there is any pending event, it will restart the
1485 * FCF saning and return 1 else return 0.
1486 */
1487int
1488lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1489{
James Smart32b97932009-07-19 10:01:21 -04001490 /*
1491 * If the Link is up and no FCoE events while in the
1492 * FCF discovery, no need to restart FCF discovery.
1493 */
1494 if ((phba->link_state >= LPFC_LINK_UP) &&
1495 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1496 return 0;
1497
James Smart0c9ab6f2010-02-26 14:15:57 -05001498 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1499 "2768 Pending link or FCF event during current "
1500 "handling of the previous event: link_state:x%x, "
1501 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1502 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1503 phba->fcoe_eventtag);
1504
James Smart32b97932009-07-19 10:01:21 -04001505 spin_lock_irq(&phba->hbalock);
1506 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1507 spin_unlock_irq(&phba->hbalock);
1508
James Smart0c9ab6f2010-02-26 14:15:57 -05001509 if (phba->link_state >= LPFC_LINK_UP) {
1510 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1511 "2780 Restart FCF table scan due to "
1512 "pending FCF event:evt_tag_at_scan:x%x, "
1513 "evt_tag_current:x%x\n",
1514 phba->fcoe_eventtag_at_fcf_scan,
1515 phba->fcoe_eventtag);
1516 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1517 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001518 /*
1519 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1520 * flag
1521 */
James Smart1c6f4ef52009-11-18 15:40:49 -05001522 spin_lock_irq(&phba->hbalock);
James Smart4d9ab992009-10-02 15:16:39 -04001523 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001524 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001525 spin_unlock_irq(&phba->hbalock);
1526 }
James Smart32b97932009-07-19 10:01:21 -04001527
James Smart0c9ab6f2010-02-26 14:15:57 -05001528 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001529 if (unreg_fcf) {
1530 spin_lock_irq(&phba->hbalock);
1531 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1532 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001533 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001534 }
James Smart32b97932009-07-19 10:01:21 -04001535 return 1;
1536}
1537
1538/**
James Smart999d8132010-03-15 11:24:56 -04001539 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1540 * @phba: pointer to lpfc hba data structure.
1541 * @fcf_cnt: number of eligible fcf record seen so far.
1542 *
1543 * This function makes an running random selection decision on FCF record to
1544 * use through a sequence of @fcf_cnt eligible FCF records with equal
1545 * probability. To perform integer manunipulation of random numbers with
1546 * size unit32_t, the lower 16 bits of the 32-bit random number returned
1547 * from random32() are taken as the random random number generated.
1548 *
1549 * Returns true when outcome is for the newly read FCF record should be
1550 * chosen; otherwise, return false when outcome is for keeping the previously
1551 * chosen FCF record.
1552 **/
1553static bool
1554lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1555{
1556 uint32_t rand_num;
1557
1558 /* Get 16-bit uniform random number */
1559 rand_num = (0xFFFF & random32());
1560
1561 /* Decision with probability 1/fcf_cnt */
1562 if ((fcf_cnt * rand_num) < 0xFFFF)
1563 return true;
1564 else
1565 return false;
1566}
1567
1568/**
1569 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
James Smart6fb120a2009-05-22 14:52:59 -04001570 * @phba: pointer to lpfc hba data structure.
1571 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001572 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001573 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001574 * This routine parses the non-embedded fcf mailbox command by performing the
1575 * necessarily error checking, non-embedded read FCF record mailbox command
1576 * SGE parsing, and endianness swapping.
1577 *
1578 * Returns the pointer to the new FCF record in the non-embedded mailbox
1579 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001580 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001581static struct fcf_record *
1582lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1583 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001584{
1585 void *virt_addr;
1586 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001587 struct lpfc_mbx_sge sge;
1588 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1589 uint32_t shdr_status, shdr_add_status;
1590 union lpfc_sli4_cfg_shdr *shdr;
1591 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001592
James Smart6fb120a2009-05-22 14:52:59 -04001593 /* Get the first SGE entry from the non-embedded DMA memory. This
1594 * routine only uses a single SGE.
1595 */
1596 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1597 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1598 if (unlikely(!mboxq->sge_array)) {
1599 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1600 "2524 Failed to get the non-embedded SGE "
1601 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001602 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001603 }
1604 virt_addr = mboxq->sge_array->addr[0];
1605
1606 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1607 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001608 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001609 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001610 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1611 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001612 "2726 READ_FCF_RECORD Indicates empty "
1613 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001614 else
1615 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001616 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001617 "with status x%x add_status x%x, "
1618 "mbx\n", shdr_status, shdr_add_status);
1619 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001620 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001621
1622 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001623 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1624 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1625 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001626 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001627 new_fcf_record = (struct fcf_record *)(virt_addr +
1628 sizeof(struct lpfc_mbx_read_fcf_tbl));
1629 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001630 offsetof(struct fcf_record, vlan_bitmap));
1631 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1632 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001633
James Smart0c9ab6f2010-02-26 14:15:57 -05001634 return new_fcf_record;
1635}
1636
1637/**
1638 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1639 * @phba: pointer to lpfc hba data structure.
1640 * @fcf_record: pointer to the fcf record.
1641 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1642 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1643 *
1644 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1645 * enabled.
1646 **/
1647static void
1648lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1649 struct fcf_record *fcf_record,
1650 uint16_t vlan_id,
1651 uint16_t next_fcf_index)
1652{
1653 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1654 "2764 READ_FCF_RECORD:\n"
1655 "\tFCF_Index : x%x\n"
1656 "\tFCF_Avail : x%x\n"
1657 "\tFCF_Valid : x%x\n"
1658 "\tFIP_Priority : x%x\n"
1659 "\tMAC_Provider : x%x\n"
1660 "\tLowest VLANID : x%x\n"
1661 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1662 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1663 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1664 "\tNext_FCF_Index: x%x\n",
1665 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1666 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1667 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1668 fcf_record->fip_priority,
1669 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1670 vlan_id,
1671 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1672 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1673 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1674 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1675 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1676 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1677 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1678 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1679 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1680 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1681 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1682 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1683 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1684 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1685 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1686 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1687 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1688 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1689 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1690 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1691 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1692 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1693 next_fcf_index);
1694}
1695
1696/**
1697 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
1698 * @phba: pointer to lpfc hba data structure.
1699 * @mboxq: pointer to mailbox object.
1700 *
1701 * This function iterates through all the fcf records available in
1702 * HBA and chooses the optimal FCF record for discovery. After finding
1703 * the FCF for discovery it registers the FCF record and kicks start
1704 * discovery.
1705 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
1706 * use an FCF record which matches fabric name and mac address of the
1707 * currently used FCF record.
1708 * If the driver supports only one FCF, it will try to use the FCF record
1709 * used by BOOT_BIOS.
1710 */
1711void
1712lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1713{
1714 struct fcf_record *new_fcf_record;
1715 uint32_t boot_flag, addr_mode;
1716 uint16_t fcf_index, next_fcf_index;
1717 struct lpfc_fcf_rec *fcf_rec = NULL;
1718 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04001719 uint32_t seed;
1720 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001721 int rc;
1722
1723 /* If there is pending FCoE event restart FCF table scan */
1724 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1725 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1726 return;
1727 }
1728
1729 /* Parse the FCF record from the non-embedded mailbox command */
1730 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
1731 &next_fcf_index);
1732 if (!new_fcf_record) {
1733 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1734 "2765 Mailbox command READ_FCF_RECORD "
1735 "failed to retrieve a FCF record.\n");
1736 /* Let next new FCF event trigger fast failover */
1737 spin_lock_irq(&phba->hbalock);
1738 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1739 spin_unlock_irq(&phba->hbalock);
1740 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1741 return;
1742 }
1743
1744 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05001745 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1746 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05001747
1748 /* Log the FCF record information if turned on */
1749 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
1750 next_fcf_index);
1751
James Smart6fb120a2009-05-22 14:52:59 -04001752 /*
1753 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05001754 * read the next entry; otherwise, this is an eligible FCF
1755 * record for round robin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04001756 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001757 if (!rc) {
1758 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1759 "2781 FCF record fcf_index:x%x failed FCF "
1760 "connection list check, fcf_avail:x%x, "
1761 "fcf_valid:x%x\n",
1762 bf_get(lpfc_fcf_record_fcf_index,
1763 new_fcf_record),
1764 bf_get(lpfc_fcf_record_fcf_avail,
1765 new_fcf_record),
1766 bf_get(lpfc_fcf_record_fcf_valid,
1767 new_fcf_record));
James Smart6fb120a2009-05-22 14:52:59 -04001768 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001769 } else {
1770 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1771 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
1772 if (rc)
1773 goto read_next_fcf;
1774 }
1775
James Smart6fb120a2009-05-22 14:52:59 -04001776 /*
1777 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05001778 * FCF record for the discovery. The condition that a rescan
1779 * matches the in-use FCF record: fabric name, switch name, mac
1780 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04001781 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001782 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001783 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartecfd03c2010-02-12 14:41:27 -05001784 if (lpfc_fab_name_match(phba->fcf.current_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001785 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001786 lpfc_sw_name_match(phba->fcf.current_rec.switch_name,
James Smart8fa38512009-07-19 10:01:03 -04001787 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001788 lpfc_mac_addr_match(phba->fcf.current_rec.mac_addr,
1789 new_fcf_record) &&
1790 lpfc_vlan_id_match(phba->fcf.current_rec.vlan_id,
1791 vlan_id)) {
James Smart6fb120a2009-05-22 14:52:59 -04001792 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smartecfd03c2010-02-12 14:41:27 -05001793 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
1794 /* Stop FCF redisc wait timer if pending */
1795 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
1796 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1797 /* If in fast failover, mark it's completed */
James Smartfc2b9892010-02-26 14:15:29 -05001798 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV |
James Smart0c9ab6f2010-02-26 14:15:57 -05001799 FCF_DISCOVERY);
1800 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001801 goto out;
1802 }
James Smartecfd03c2010-02-12 14:41:27 -05001803 /*
1804 * Read next FCF record from HBA searching for the matching
1805 * with in-use record only if not during the fast failover
1806 * period. In case of fast failover period, it shall try to
1807 * determine whether the FCF record just read should be the
1808 * next candidate.
1809 */
1810 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001811 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001812 goto read_next_fcf;
1813 }
James Smart6fb120a2009-05-22 14:52:59 -04001814 }
James Smartecfd03c2010-02-12 14:41:27 -05001815 /*
1816 * Update on failover FCF record only if it's in FCF fast-failover
1817 * period; otherwise, update on current FCF record.
1818 */
James Smartfc2b9892010-02-26 14:15:29 -05001819 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1820 fcf_rec = &phba->fcf.failover_rec;
1821 else
James Smartecfd03c2010-02-12 14:41:27 -05001822 fcf_rec = &phba->fcf.current_rec;
1823
James Smart6fb120a2009-05-22 14:52:59 -04001824 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1825 /*
James Smartecfd03c2010-02-12 14:41:27 -05001826 * If the driver FCF record does not have boot flag
1827 * set and new hba fcf record has boot flag set, use
1828 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001829 */
James Smartecfd03c2010-02-12 14:41:27 -05001830 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
1831 /* Choose this FCF record */
1832 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1833 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05001834 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001835 goto read_next_fcf;
1836 }
1837 /*
James Smartecfd03c2010-02-12 14:41:27 -05001838 * If the driver FCF record has boot flag set and the
1839 * new hba FCF record does not have boot flag, read
1840 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04001841 */
James Smartecfd03c2010-02-12 14:41:27 -05001842 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001843 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001844 goto read_next_fcf;
1845 }
1846 /*
James Smartecfd03c2010-02-12 14:41:27 -05001847 * If the new hba FCF record has lower priority value
1848 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04001849 */
James Smartfc2b9892010-02-26 14:15:29 -05001850 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04001851 /* Choose the new FCF record with lower priority */
James Smartecfd03c2010-02-12 14:41:27 -05001852 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1853 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04001854 /* Reset running random FCF selection count */
1855 phba->fcf.eligible_fcf_cnt = 1;
1856 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
1857 /* Update running random FCF selection count */
1858 phba->fcf.eligible_fcf_cnt++;
1859 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
1860 phba->fcf.eligible_fcf_cnt);
1861 if (select_new_fcf)
1862 /* Choose the new FCF by random selection */
1863 __lpfc_update_fcf_record(phba, fcf_rec,
1864 new_fcf_record,
1865 addr_mode, vlan_id, 0);
James Smart6fb120a2009-05-22 14:52:59 -04001866 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001867 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001868 goto read_next_fcf;
1869 }
1870 /*
James Smartecfd03c2010-02-12 14:41:27 -05001871 * This is the first suitable FCF record, choose this record for
1872 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001873 */
James Smartecfd03c2010-02-12 14:41:27 -05001874 if (fcf_rec) {
1875 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1876 addr_mode, vlan_id, (boot_flag ?
1877 BOOT_ENABLE : 0));
1878 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04001879 /* Setup initial running random FCF selection count */
1880 phba->fcf.eligible_fcf_cnt = 1;
1881 /* Seeding the random number generator for random selection */
1882 seed = (uint32_t)(0xFFFFFFFF & jiffies);
1883 srandom32(seed);
James Smart6fb120a2009-05-22 14:52:59 -04001884 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001885 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001886 goto read_next_fcf;
1887
1888read_next_fcf:
1889 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05001890 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
1891 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
1892 /*
1893 * Case of FCF fast failover scan
1894 */
1895
1896 /*
1897 * It has not found any suitable FCF record, cancel
1898 * FCF scan inprogress, and do nothing
1899 */
1900 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001901 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1902 "2782 No suitable FCF record "
1903 "found during this round of "
1904 "post FCF rediscovery scan: "
1905 "fcf_evt_tag:x%x, fcf_index: "
1906 "x%x\n",
1907 phba->fcoe_eventtag_at_fcf_scan,
1908 bf_get(lpfc_fcf_record_fcf_index,
1909 new_fcf_record));
1910 /*
1911 * Let next new FCF event trigger fast
1912 * failover
1913 */
1914 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001915 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001916 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001917 return;
1918 }
1919 /*
1920 * It has found a suitable FCF record that is not
1921 * the same as in-use FCF record, unregister the
1922 * in-use FCF record, replace the in-use FCF record
1923 * with the new FCF record, mark FCF fast failover
1924 * completed, and then start register the new FCF
1925 * record.
1926 */
1927
James Smart0c9ab6f2010-02-26 14:15:57 -05001928 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001929 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05001930
1931 /* Replace in-use record with the new record */
James Smartecfd03c2010-02-12 14:41:27 -05001932 memcpy(&phba->fcf.current_rec,
1933 &phba->fcf.failover_rec,
1934 sizeof(struct lpfc_fcf_rec));
1935 /* mark the FCF fast failover completed */
James Smart0c9ab6f2010-02-26 14:15:57 -05001936 spin_lock_irq(&phba->hbalock);
1937 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
1938 spin_unlock_irq(&phba->hbalock);
1939 /*
1940 * Set up the initial registered FCF index for FLOGI
1941 * round robin FCF failover.
1942 */
1943 phba->fcf.fcf_rr_init_indx =
1944 phba->fcf.failover_rec.fcf_indx;
James Smartecfd03c2010-02-12 14:41:27 -05001945 /* Register to the new FCF record */
1946 lpfc_register_fcf(phba);
1947 } else {
1948 /*
1949 * In case of transaction period to fast FCF failover,
1950 * do nothing when search to the end of the FCF table.
1951 */
1952 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
1953 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
1954 return;
1955 /*
1956 * Otherwise, initial scan or post linkdown rescan,
James Smart0c9ab6f2010-02-26 14:15:57 -05001957 * register with the best FCF record found so far
1958 * through the FCF scanning process.
James Smartecfd03c2010-02-12 14:41:27 -05001959 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001960
1961 /* mark the initial FCF discovery completed */
1962 spin_lock_irq(&phba->hbalock);
1963 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
1964 spin_unlock_irq(&phba->hbalock);
1965 /*
1966 * Set up the initial registered FCF index for FLOGI
1967 * round robin FCF failover
1968 */
1969 phba->fcf.fcf_rr_init_indx =
1970 phba->fcf.current_rec.fcf_indx;
1971 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001972 lpfc_register_fcf(phba);
1973 }
1974 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05001975 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04001976 return;
1977
1978out:
1979 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1980 lpfc_register_fcf(phba);
1981
1982 return;
1983}
1984
1985/**
James Smart0c9ab6f2010-02-26 14:15:57 -05001986 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf round robin read_fcf mbox cmpl hdler
1987 * @phba: pointer to lpfc hba data structure.
1988 * @mboxq: pointer to mailbox object.
1989 *
1990 * This is the callback function for FLOGI failure round robin FCF failover
1991 * read FCF record mailbox command from the eligible FCF record bmask for
1992 * performing the failover. If the FCF read back is not valid/available, it
1993 * fails through to retrying FLOGI to the currently registered FCF again.
1994 * Otherwise, if the FCF read back is valid and available, it will set the
1995 * newly read FCF record to the failover FCF record, unregister currently
1996 * registered FCF record, copy the failover FCF record to the current
1997 * FCF record, and then register the current FCF record before proceeding
1998 * to trying FLOGI on the new failover FCF.
1999 */
2000void
2001lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2002{
2003 struct fcf_record *new_fcf_record;
2004 uint32_t boot_flag, addr_mode;
2005 uint16_t next_fcf_index;
2006 uint16_t current_fcf_index;
2007 uint16_t vlan_id;
2008
2009 /* If link state is not up, stop the round robin failover process */
2010 if (phba->link_state < LPFC_LINK_UP) {
2011 spin_lock_irq(&phba->hbalock);
2012 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
2013 spin_unlock_irq(&phba->hbalock);
2014 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2015 return;
2016 }
2017
2018 /* Parse the FCF record from the non-embedded mailbox command */
2019 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2020 &next_fcf_index);
2021 if (!new_fcf_record) {
2022 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2023 "2766 Mailbox command READ_FCF_RECORD "
2024 "failed to retrieve a FCF record.\n");
2025 goto out;
2026 }
2027
2028 /* Get the needed parameters from FCF record */
2029 lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2030 &addr_mode, &vlan_id);
2031
2032 /* Log the FCF record information if turned on */
2033 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2034 next_fcf_index);
2035
2036 /* Upload new FCF record to the failover FCF record */
2037 spin_lock_irq(&phba->hbalock);
2038 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2039 new_fcf_record, addr_mode, vlan_id,
2040 (boot_flag ? BOOT_ENABLE : 0));
2041 spin_unlock_irq(&phba->hbalock);
2042
2043 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2044
2045 /* Unregister the current in-use FCF record */
2046 lpfc_unregister_fcf(phba);
2047
2048 /* Replace in-use record with the new record */
2049 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2050 sizeof(struct lpfc_fcf_rec));
2051
2052 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2053 "2783 FLOGI round robin FCF failover from FCF "
2054 "(index:x%x) to FCF (index:x%x).\n",
2055 current_fcf_index,
2056 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
2057
2058out:
2059 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2060 lpfc_register_fcf(phba);
2061}
2062
2063/**
2064 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2065 * @phba: pointer to lpfc hba data structure.
2066 * @mboxq: pointer to mailbox object.
2067 *
2068 * This is the callback function of read FCF record mailbox command for
2069 * updating the eligible FCF bmask for FLOGI failure round robin FCF
2070 * failover when a new FCF event happened. If the FCF read back is
2071 * valid/available and it passes the connection list check, it updates
2072 * the bmask for the eligible FCF record for round robin failover.
2073 */
2074void
2075lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2076{
2077 struct fcf_record *new_fcf_record;
2078 uint32_t boot_flag, addr_mode;
2079 uint16_t fcf_index, next_fcf_index;
2080 uint16_t vlan_id;
2081 int rc;
2082
2083 /* If link state is not up, no need to proceed */
2084 if (phba->link_state < LPFC_LINK_UP)
2085 goto out;
2086
2087 /* If FCF discovery period is over, no need to proceed */
2088 if (phba->fcf.fcf_flag & FCF_DISCOVERY)
2089 goto out;
2090
2091 /* Parse the FCF record from the non-embedded mailbox command */
2092 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2093 &next_fcf_index);
2094 if (!new_fcf_record) {
2095 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2096 "2767 Mailbox command READ_FCF_RECORD "
2097 "failed to retrieve a FCF record.\n");
2098 goto out;
2099 }
2100
2101 /* Check the connection list for eligibility */
2102 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2103 &addr_mode, &vlan_id);
2104
2105 /* Log the FCF record information if turned on */
2106 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2107 next_fcf_index);
2108
2109 if (!rc)
2110 goto out;
2111
2112 /* Update the eligible FCF record index bmask */
2113 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2114 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
2115
2116out:
2117 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2118}
2119
2120/**
James Smart1c6834a2009-07-19 10:01:26 -04002121 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2122 * @phba: pointer to lpfc hba data structure.
2123 * @mboxq: pointer to mailbox data structure.
2124 *
2125 * This function handles completion of init vpi mailbox command.
2126 */
James Smart695a8142010-01-26 23:08:03 -05002127void
James Smart1c6834a2009-07-19 10:01:26 -04002128lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2129{
2130 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002131 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002132 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2133
James Smart1c6834a2009-07-19 10:01:26 -04002134 if (mboxq->u.mb.mbxStatus) {
2135 lpfc_printf_vlog(vport, KERN_ERR,
2136 LOG_MBOX,
2137 "2609 Init VPI mailbox failed 0x%x\n",
2138 mboxq->u.mb.mbxStatus);
2139 mempool_free(mboxq, phba->mbox_mem_pool);
2140 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2141 return;
2142 }
James Smart72100cc2010-02-12 14:43:01 -05002143 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002144 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002145 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002146
James Smart695a8142010-01-26 23:08:03 -05002147 /* If this port is physical port or FDISC is done, do reg_vpi */
2148 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2149 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2150 if (!ndlp)
2151 lpfc_printf_vlog(vport, KERN_ERR,
2152 LOG_DISCOVERY,
2153 "2731 Cannot find fabric "
2154 "controller node\n");
2155 else
2156 lpfc_register_new_vport(phba, vport, ndlp);
2157 mempool_free(mboxq, phba->mbox_mem_pool);
2158 return;
2159 }
2160
James Smart1c6834a2009-07-19 10:01:26 -04002161 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2162 lpfc_initial_fdisc(vport);
2163 else {
2164 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002165 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2166 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002167 }
James Smart695a8142010-01-26 23:08:03 -05002168 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002169 return;
2170}
2171
2172/**
James Smartecfd03c2010-02-12 14:41:27 -05002173 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2174 * @vport: pointer to lpfc_vport data structure.
2175 *
2176 * This function issue a init_vpi mailbox command to initialize
2177 * VPI for the vport.
2178 */
2179void
2180lpfc_issue_init_vpi(struct lpfc_vport *vport)
2181{
2182 LPFC_MBOXQ_t *mboxq;
2183 int rc;
2184
2185 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2186 if (!mboxq) {
2187 lpfc_printf_vlog(vport, KERN_ERR,
2188 LOG_MBOX, "2607 Failed to allocate "
2189 "init_vpi mailbox\n");
2190 return;
2191 }
2192 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2193 mboxq->vport = vport;
2194 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2195 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2196 if (rc == MBX_NOT_FINISHED) {
2197 lpfc_printf_vlog(vport, KERN_ERR,
2198 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2199 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2200 }
2201}
2202
2203/**
James Smart6fb120a2009-05-22 14:52:59 -04002204 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2205 * @phba: pointer to lpfc hba data structure.
2206 *
2207 * This function loops through the list of vports on the @phba and issues an
2208 * FDISC if possible.
2209 */
2210void
2211lpfc_start_fdiscs(struct lpfc_hba *phba)
2212{
2213 struct lpfc_vport **vports;
2214 int i;
2215
2216 vports = lpfc_create_vport_work_array(phba);
2217 if (vports != NULL) {
2218 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2219 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2220 continue;
2221 /* There are no vpi for this vport */
2222 if (vports[i]->vpi > phba->max_vpi) {
2223 lpfc_vport_set_state(vports[i],
2224 FC_VPORT_FAILED);
2225 continue;
2226 }
2227 if (phba->fc_topology == TOPOLOGY_LOOP) {
2228 lpfc_vport_set_state(vports[i],
2229 FC_VPORT_LINKDOWN);
2230 continue;
2231 }
James Smart1c6834a2009-07-19 10:01:26 -04002232 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002233 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002234 continue;
2235 }
James Smart6fb120a2009-05-22 14:52:59 -04002236 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2237 lpfc_initial_fdisc(vports[i]);
2238 else {
2239 lpfc_vport_set_state(vports[i],
2240 FC_VPORT_NO_FABRIC_SUPP);
2241 lpfc_printf_vlog(vports[i], KERN_ERR,
2242 LOG_ELS,
2243 "0259 No NPIV "
2244 "Fabric support\n");
2245 }
2246 }
2247 }
2248 lpfc_destroy_vport_work_array(phba, vports);
2249}
2250
2251void
2252lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2253{
2254 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2255 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002256 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002257
2258 if (mboxq->u.mb.mbxStatus) {
2259 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2260 "2018 REG_VFI mbxStatus error x%x "
2261 "HBA state x%x\n",
2262 mboxq->u.mb.mbxStatus, vport->port_state);
2263 if (phba->fc_topology == TOPOLOGY_LOOP) {
2264 /* FLOGI failed, use loop map to make discovery list */
2265 lpfc_disc_list_loopmap(vport);
2266 /* Start discovery */
2267 lpfc_disc_start(vport);
2268 goto fail_free_mem;
2269 }
2270 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2271 goto fail_free_mem;
2272 }
James Smartc8685952009-11-18 15:39:16 -05002273 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002274 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002275 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002276 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002277 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002278 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002279
2280 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2281 lpfc_start_fdiscs(phba);
2282 lpfc_do_scr_ns_plogi(phba, vport);
2283 }
2284
2285fail_free_mem:
2286 mempool_free(mboxq, phba->mbox_mem_pool);
2287 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2288 kfree(dmabuf);
2289 return;
2290}
2291
2292static void
James Smart2e0fef82007-06-17 19:56:36 -05002293lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002294{
James Smart6fb120a2009-05-22 14:52:59 -04002295 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002296 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002297 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002298
2299
2300 /* Check for error */
2301 if (mb->mbxStatus) {
2302 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002303 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2304 "0319 READ_SPARAM mbxStatus error x%x "
2305 "hba state x%x>\n",
2306 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002307 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002308 goto out;
2309 }
2310
James Smart2e0fef82007-06-17 19:56:36 -05002311 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002312 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05002313 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05002314 u64_to_wwn(phba->cfg_soft_wwnn,
2315 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04002316 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05002317 u64_to_wwn(phba->cfg_soft_wwpn,
2318 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05002319 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
2320 sizeof(vport->fc_nodename));
2321 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
2322 sizeof(vport->fc_portname));
2323 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2324 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2325 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2326 }
2327
dea31012005-04-17 16:05:31 -05002328 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2329 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002330 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002331 return;
2332
2333out:
2334 pmb->context1 = NULL;
2335 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2336 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002337 lpfc_issue_clear_la(phba, vport);
2338 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002339 return;
2340}
2341
2342static void
James Smart92d7f7b2007-06-17 19:56:38 -05002343lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05002344{
James Smart92d7f7b2007-06-17 19:56:38 -05002345 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002346 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002347 int i;
James Smart14691152006-12-02 13:34:28 -05002348 struct lpfc_dmabuf *mp;
2349 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002350 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002351
James Smart92d7f7b2007-06-17 19:56:38 -05002352 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002353 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05002354 case LA_1GHZ_LINK:
2355 phba->fc_linkspeed = LA_1GHZ_LINK;
2356 break;
2357 case LA_2GHZ_LINK:
2358 phba->fc_linkspeed = LA_2GHZ_LINK;
2359 break;
2360 case LA_4GHZ_LINK:
2361 phba->fc_linkspeed = LA_4GHZ_LINK;
2362 break;
2363 case LA_8GHZ_LINK:
2364 phba->fc_linkspeed = LA_8GHZ_LINK;
2365 break;
James Smartf4b4c682009-05-22 14:53:12 -04002366 case LA_10GHZ_LINK:
2367 phba->fc_linkspeed = LA_10GHZ_LINK;
2368 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002369 default:
2370 phba->fc_linkspeed = LA_UNKNW_LINK;
2371 break;
dea31012005-04-17 16:05:31 -05002372 }
2373
2374 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05002375 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05002376
2377 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05002378 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05002379
James Smart6a9c52c2009-10-02 15:16:51 -04002380 /* if npiv is enabled and this adapter supports npiv log
2381 * a message that npiv is not supported in this topology
2382 */
2383 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04002384 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2385 "1309 Link Up Event npiv not supported in loop "
2386 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002387 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05002388 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05002389 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002390
James Smart2e0fef82007-06-17 19:56:36 -05002391 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05002392 i = la->un.lilpBde64.tus.f.bdeSize;
2393
2394 if (i == 0) {
2395 phba->alpa_map[0] = 0;
2396 } else {
James Smarte8b62012007-08-02 11:10:09 -04002397 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05002398 int numalpa, j, k;
2399 union {
2400 uint8_t pamap[16];
2401 struct {
2402 uint32_t wd1;
2403 uint32_t wd2;
2404 uint32_t wd3;
2405 uint32_t wd4;
2406 } pa;
2407 } un;
2408 numalpa = phba->alpa_map[0];
2409 j = 0;
2410 while (j < numalpa) {
2411 memset(un.pamap, 0, 16);
2412 for (k = 1; j < numalpa; k++) {
2413 un.pamap[k - 1] =
2414 phba->alpa_map[j + 1];
2415 j++;
2416 if (k == 16)
2417 break;
2418 }
2419 /* Link Up Event ALPA map */
2420 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05002421 KERN_WARNING,
2422 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002423 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05002424 "ALPA map Data: x%x "
2425 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002426 un.pa.wd1, un.pa.wd2,
2427 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05002428 }
2429 }
2430 }
2431 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05002432 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04002433 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05002434 (phba->sli_rev == 3))
2435 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
2436 }
James Smart2e0fef82007-06-17 19:56:36 -05002437 vport->fc_myDID = phba->fc_pref_DID;
2438 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002439 }
James Smart92d7f7b2007-06-17 19:56:38 -05002440 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002441
2442 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05002443 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2444 if (!sparam_mbox)
2445 goto out;
2446
2447 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
2448 if (rc) {
2449 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2450 goto out;
2451 }
2452 sparam_mbox->vport = vport;
2453 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
2454 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
2455 if (rc == MBX_NOT_FINISHED) {
2456 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2457 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2458 kfree(mp);
2459 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2460 goto out;
dea31012005-04-17 16:05:31 -05002461 }
2462
James Smart6fb120a2009-05-22 14:52:59 -04002463 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2464 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2465 if (!cfglink_mbox)
2466 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05002467 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002468 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002469 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002470 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002471 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002472 if (rc == MBX_NOT_FINISHED) {
2473 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2474 goto out;
2475 }
2476 } else {
James Smart32b97932009-07-19 10:01:21 -04002477 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002478 /*
2479 * Add the driver's default FCF record at FCF index 0 now. This
2480 * is phase 1 implementation that support FCF index 0 and driver
2481 * defaults.
2482 */
James Smart45ed1192009-10-02 15:17:02 -04002483 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002484 fcf_record = kzalloc(sizeof(struct fcf_record),
2485 GFP_KERNEL);
2486 if (unlikely(!fcf_record)) {
2487 lpfc_printf_log(phba, KERN_ERR,
2488 LOG_MBOX | LOG_SLI,
2489 "2554 Could not allocate memmory for "
2490 "fcf record\n");
2491 rc = -ENODEV;
2492 goto out;
2493 }
2494
2495 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2496 LPFC_FCOE_FCF_DEF_INDEX);
2497 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2498 if (unlikely(rc)) {
2499 lpfc_printf_log(phba, KERN_ERR,
2500 LOG_MBOX | LOG_SLI,
2501 "2013 Could not manually add FCF "
2502 "record 0, status %d\n", rc);
2503 rc = -ENODEV;
2504 kfree(fcf_record);
2505 goto out;
2506 }
2507 kfree(fcf_record);
2508 }
2509 /*
2510 * The driver is expected to do FIP/FCF. Call the port
2511 * and get the FCF Table.
2512 */
James Smart32b97932009-07-19 10:01:21 -04002513 spin_lock_irq(&phba->hbalock);
2514 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2515 spin_unlock_irq(&phba->hbalock);
2516 return;
2517 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002518 /* This is the initial FCF discovery scan */
2519 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04002520 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002521 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2522 "2778 Start FCF table scan at linkup\n");
2523
2524 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2525 LPFC_FCOE_FCF_GET_FIRST);
2526 if (rc) {
2527 spin_lock_irq(&phba->hbalock);
2528 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
2529 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002530 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002531 }
dea31012005-04-17 16:05:31 -05002532 }
James Smart6fb120a2009-05-22 14:52:59 -04002533
2534 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002535out:
2536 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002537 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2538 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2539 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002540 lpfc_issue_clear_la(phba, vport);
2541 return;
dea31012005-04-17 16:05:31 -05002542}
2543
2544static void
James Smart84774a42008-08-24 21:50:06 -04002545lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002546{
dea31012005-04-17 16:05:31 -05002547 uint32_t control;
2548 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002549 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002550 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002551 if (phba->sli_rev <= LPFC_SLI_REV3) {
2552 control = readl(phba->HCregaddr);
2553 control |= HC_LAINT_ENA;
2554 writel(control, phba->HCregaddr);
2555 readl(phba->HCregaddr); /* flush */
2556 }
James Smart2e0fef82007-06-17 19:56:36 -05002557 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002558}
2559
James Smart84774a42008-08-24 21:50:06 -04002560static void
2561lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2562{
2563 lpfc_linkdown(phba);
2564 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002565 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002566 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2567}
2568
2569
dea31012005-04-17 16:05:31 -05002570/*
2571 * This routine handles processing a READ_LA mailbox
2572 * command upon completion. It is setup in the LPFC_MBOXQ
2573 * as the completion routine when the command is
2574 * handed off to the SLI layer.
2575 */
2576void
James Smart2e0fef82007-06-17 19:56:36 -05002577lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002578{
James Smart2e0fef82007-06-17 19:56:36 -05002579 struct lpfc_vport *vport = pmb->vport;
2580 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002581 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002582 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002583 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2584
James Smart0d2b6b82008-06-14 22:52:47 -04002585 /* Unblock ELS traffic */
2586 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002587 /* Check for error */
2588 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002589 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002590 "1307 READ_LA mbox error x%x state x%x\n",
2591 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002592 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002593 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002594 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2595 }
2596
James Smart04c68492009-05-22 14:52:52 -04002597 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002598
2599 memcpy(&phba->alpa_map[0], mp->virt, 128);
2600
James Smart2e0fef82007-06-17 19:56:36 -05002601 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002602 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002603 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002604 else
James Smart2e0fef82007-06-17 19:56:36 -05002605 vport->fc_flag &= ~FC_BYPASSED_MODE;
2606 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002607
James Smart0c287582009-06-10 17:22:56 -04002608 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002609 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002610 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002611 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002612 if (phba->fc_eventTag != 0)
2613 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002614 }
dea31012005-04-17 16:05:31 -05002615
2616 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05002617 spin_lock_irq(&phba->hbalock);
James Smart84774a42008-08-24 21:50:06 -04002618 if (la->mm)
2619 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2620 else
2621 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05002622 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002623
James Smart4d9ab992009-10-02 15:16:39 -04002624 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002625 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002626 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002627 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002628 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002629 "1306 Link Up Event in loop back mode "
2630 "x%x received Data: x%x x%x x%x x%x\n",
2631 la->eventTag, phba->fc_eventTag,
2632 la->granted_AL_PA, la->UlnkSpeed,
2633 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002634 } else {
2635 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002636 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002637 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002638 la->eventTag, phba->fc_eventTag,
2639 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002640 phba->alpa_map[0],
2641 la->mm, la->fa,
2642 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002643 }
James Smart92d7f7b2007-06-17 19:56:38 -05002644 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002645 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002646 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002647 if (phba->link_flag & LS_LOOPBACK_MODE) {
2648 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2649 "1308 Link Down Event in loop back mode "
2650 "x%x received "
2651 "Data: x%x x%x x%x\n",
2652 la->eventTag, phba->fc_eventTag,
2653 phba->pport->port_state, vport->fc_flag);
2654 }
2655 else {
2656 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002657 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002658 "Data: x%x x%x x%x x%x x%x\n",
2659 la->eventTag, phba->fc_eventTag,
2660 phba->pport->port_state, vport->fc_flag,
2661 la->mm, la->fa);
2662 }
2663 lpfc_mbx_issue_link_down(phba);
2664 }
2665 if (la->mm && la->attType == AT_LINK_UP) {
2666 if (phba->link_state != LPFC_LINK_DOWN) {
2667 phba->fc_stat.LinkDown++;
2668 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2669 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002670 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002671 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002672 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002673 lpfc_mbx_issue_link_down(phba);
2674 } else
2675 lpfc_enable_la(phba);
2676
2677 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2678 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2679 "Data: x%x x%x x%x\n",
2680 la->eventTag, phba->fc_eventTag,
2681 phba->pport->port_state, vport->fc_flag);
2682 /*
2683 * The cmnd that triggered this will be waiting for this
2684 * signal.
2685 */
2686 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2687 if (phba->wait_4_mlo_maint_flg) {
2688 phba->wait_4_mlo_maint_flg = 0;
2689 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002690 }
James Smart84774a42008-08-24 21:50:06 -04002691 }
2692
2693 if (la->fa) {
2694 if (la->mm)
2695 lpfc_issue_clear_la(phba, vport);
2696 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2697 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002698 }
2699
2700lpfc_mbx_cmpl_read_la_free_mbuf:
2701 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2702 kfree(mp);
2703 mempool_free(pmb, phba->mbox_mem_pool);
2704 return;
2705}
2706
2707/*
2708 * This routine handles processing a REG_LOGIN mailbox
2709 * command upon completion. It is setup in the LPFC_MBOXQ
2710 * as the completion routine when the command is
2711 * handed off to the SLI layer.
2712 */
2713void
James Smart2e0fef82007-06-17 19:56:36 -05002714lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002715{
James Smart2e0fef82007-06-17 19:56:36 -05002716 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002717 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002718 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04002719 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002720
dea31012005-04-17 16:05:31 -05002721 pmb->context1 = NULL;
2722
James Smartffc95492010-06-07 15:23:17 -04002723 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
2724 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
2725
2726 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
2727 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
2728 /* We rcvd a rscn after issuing this
2729 * mbox reg login, we may have cycled
2730 * back through the state and be
2731 * back at reg login state so this
2732 * mbox needs to be ignored becase
2733 * there is another reg login in
2734 * proccess.
2735 */
2736 spin_lock_irq(shost->host_lock);
2737 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
2738 spin_unlock_irq(shost->host_lock);
2739 if (phba->sli_rev == LPFC_SLI_REV4)
2740 lpfc_sli4_free_rpi(phba,
2741 pmb->u.mb.un.varRegLogin.rpi);
2742
2743 } else
2744 /* Good status, call state machine */
2745 lpfc_disc_state_machine(vport, ndlp, pmb,
2746 NLP_EVT_CMPL_REG_LOGIN);
2747
dea31012005-04-17 16:05:31 -05002748 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2749 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002750 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002751 /* decrement the node reference count held for this callback
2752 * function.
2753 */
James Smart329f9bc2007-04-25 09:53:01 -04002754 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002755
2756 return;
2757}
2758
James Smart92d7f7b2007-06-17 19:56:38 -05002759static void
2760lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2761{
James Smart04c68492009-05-22 14:52:52 -04002762 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002763 struct lpfc_vport *vport = pmb->vport;
2764 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2765
2766 switch (mb->mbxStatus) {
2767 case 0x0011:
2768 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04002769 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2770 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2771 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002772 break;
James Smart78730cf2010-04-06 15:06:30 -04002773 /* If VPI is busy, reset the HBA */
2774 case 0x9700:
2775 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2776 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
2777 vport->vpi, mb->mbxStatus);
2778 if (!(phba->pport->load_flag & FC_UNLOADING))
2779 lpfc_workq_post_event(phba, NULL, NULL,
2780 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05002781 }
James Smart72100cc2010-02-12 14:43:01 -05002782 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002783 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05002784 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002785 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002786 vport->unreg_vpi_cmpl = VPORT_OK;
2787 mempool_free(pmb, phba->mbox_mem_pool);
2788 /*
2789 * This shost reference might have been taken at the beginning of
2790 * lpfc_vport_delete()
2791 */
James Smart1c6f4ef52009-11-18 15:40:49 -05002792 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05002793 scsi_host_put(shost);
2794}
2795
James Smartd7c255b2008-08-24 21:50:00 -04002796int
James Smart92d7f7b2007-06-17 19:56:38 -05002797lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2798{
2799 struct lpfc_hba *phba = vport->phba;
2800 LPFC_MBOXQ_t *mbox;
2801 int rc;
2802
2803 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2804 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002805 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002806
2807 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2808 mbox->vport = vport;
2809 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002810 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002811 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002812 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2813 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002814 mempool_free(mbox, phba->mbox_mem_pool);
2815 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002816 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002817 }
James Smartd7c255b2008-08-24 21:50:00 -04002818 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002819}
2820
2821static void
2822lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2823{
2824 struct lpfc_vport *vport = pmb->vport;
2825 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002826 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002827
2828 switch (mb->mbxStatus) {
2829 case 0x0011:
2830 case 0x9601:
2831 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002832 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2833 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2834 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002835 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2836 spin_lock_irq(shost->host_lock);
2837 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2838 spin_unlock_irq(shost->host_lock);
2839 vport->fc_myDID = 0;
2840 goto out;
2841 }
James Smart92d7f7b2007-06-17 19:56:38 -05002842
James Smart72100cc2010-02-12 14:43:01 -05002843 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002844 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002845 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002846 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002847 vport->num_disc_nodes = 0;
2848 /* go thru NPR list and issue ELS PLOGIs */
2849 if (vport->fc_npr_cnt)
2850 lpfc_els_disc_plogi(vport);
2851
2852 if (!vport->num_disc_nodes) {
2853 spin_lock_irq(shost->host_lock);
2854 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2855 spin_unlock_irq(shost->host_lock);
2856 lpfc_can_disctmo(vport);
2857 }
2858 vport->port_state = LPFC_VPORT_READY;
2859
2860out:
2861 mempool_free(pmb, phba->mbox_mem_pool);
2862 return;
2863}
2864
James Smart21e9a0a2009-05-22 14:53:21 -04002865/**
2866 * lpfc_create_static_vport - Read HBA config region to create static vports.
2867 * @phba: pointer to lpfc hba data structure.
2868 *
2869 * This routine issue a DUMP mailbox command for config region 22 to get
2870 * the list of static vports to be created. The function create vports
2871 * based on the information returned from the HBA.
2872 **/
2873void
2874lpfc_create_static_vport(struct lpfc_hba *phba)
2875{
2876 LPFC_MBOXQ_t *pmb = NULL;
2877 MAILBOX_t *mb;
2878 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04002879 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04002880 struct fc_vport_identifiers vport_id;
2881 struct fc_vport *new_fc_vport;
2882 struct Scsi_Host *shost;
2883 struct lpfc_vport *vport;
2884 uint16_t offset = 0;
2885 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04002886 struct lpfc_dmabuf *mp;
2887 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04002888
2889 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2890 if (!pmb) {
2891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2892 "0542 lpfc_create_static_vport failed to"
2893 " allocate mailbox memory\n");
2894 return;
2895 }
2896
2897 mb = &pmb->u.mb;
2898
2899 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2900 if (!vport_info) {
2901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2902 "0543 lpfc_create_static_vport failed to"
2903 " allocate vport_info\n");
2904 mempool_free(pmb, phba->mbox_mem_pool);
2905 return;
2906 }
2907
2908 vport_buff = (uint8_t *) vport_info;
2909 do {
James Smart1c6834a2009-07-19 10:01:26 -04002910 if (lpfc_dump_static_vport(phba, pmb, offset))
2911 goto out;
2912
James Smart21e9a0a2009-05-22 14:53:21 -04002913 pmb->vport = phba->pport;
2914 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2915
2916 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2917 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2918 "0544 lpfc_create_static_vport failed to"
2919 " issue dump mailbox command ret 0x%x "
2920 "status 0x%x\n",
2921 rc, mb->mbxStatus);
2922 goto out;
2923 }
2924
James Smart1c6834a2009-07-19 10:01:26 -04002925 if (phba->sli_rev == LPFC_SLI_REV4) {
2926 byte_count = pmb->u.mqe.un.mb_words[5];
2927 mp = (struct lpfc_dmabuf *) pmb->context2;
2928 if (byte_count > sizeof(struct static_vport_info) -
2929 offset)
2930 byte_count = sizeof(struct static_vport_info)
2931 - offset;
2932 memcpy(vport_buff + offset, mp->virt, byte_count);
2933 offset += byte_count;
2934 } else {
2935 if (mb->un.varDmp.word_cnt >
2936 sizeof(struct static_vport_info) - offset)
2937 mb->un.varDmp.word_cnt =
2938 sizeof(struct static_vport_info)
2939 - offset;
2940 byte_count = mb->un.varDmp.word_cnt;
2941 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2942 vport_buff + offset,
2943 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04002944
James Smart1c6834a2009-07-19 10:01:26 -04002945 offset += byte_count;
2946 }
James Smart21e9a0a2009-05-22 14:53:21 -04002947
James Smart1c6834a2009-07-19 10:01:26 -04002948 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04002949 offset < sizeof(struct static_vport_info));
2950
2951
2952 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2953 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2954 != VPORT_INFO_REV)) {
2955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2956 "0545 lpfc_create_static_vport bad"
2957 " information header 0x%x 0x%x\n",
2958 le32_to_cpu(vport_info->signature),
2959 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2960
2961 goto out;
2962 }
2963
2964 shost = lpfc_shost_from_vport(phba->pport);
2965
2966 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2967 memset(&vport_id, 0, sizeof(vport_id));
2968 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2969 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2970 if (!vport_id.port_name || !vport_id.node_name)
2971 continue;
2972
2973 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2974 vport_id.vport_type = FC_PORTTYPE_NPIV;
2975 vport_id.disable = false;
2976 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2977
2978 if (!new_fc_vport) {
2979 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2980 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04002981 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04002982 continue;
2983 }
2984
2985 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2986 vport->vport_flag |= STATIC_VPORT;
2987 }
2988
2989out:
James Smart21e9a0a2009-05-22 14:53:21 -04002990 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04002991 if (rc != MBX_TIMEOUT) {
2992 if (pmb->context2) {
2993 mp = (struct lpfc_dmabuf *) pmb->context2;
2994 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2995 kfree(mp);
2996 }
James Smart21e9a0a2009-05-22 14:53:21 -04002997 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002998 }
James Smart21e9a0a2009-05-22 14:53:21 -04002999
3000 return;
3001}
3002
dea31012005-04-17 16:05:31 -05003003/*
3004 * This routine handles processing a Fabric REG_LOGIN mailbox
3005 * command upon completion. It is setup in the LPFC_MBOXQ
3006 * as the completion routine when the command is
3007 * handed off to the SLI layer.
3008 */
3009void
James Smart2e0fef82007-06-17 19:56:36 -05003010lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003011{
James Smart92d7f7b2007-06-17 19:56:38 -05003012 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003013 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003014 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003015 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003016
James Smart549e55c2007-08-02 11:09:51 -04003017 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003018 pmb->context1 = NULL;
3019 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003020 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003021 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3022 "0258 Register Fabric login error: 0x%x\n",
3023 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003024 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3025 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003026 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003027
James Smart92d7f7b2007-06-17 19:56:38 -05003028 if (phba->fc_topology == TOPOLOGY_LOOP) {
3029 /* FLOGI failed, use loop map to make discovery list */
3030 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003031
James Smart92d7f7b2007-06-17 19:56:38 -05003032 /* Start discovery */
3033 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003034 /* Decrement the reference count to ndlp after the
3035 * reference to the ndlp are done.
3036 */
3037 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003038 return;
3039 }
3040
3041 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003042 /* Decrement the reference count to ndlp after the reference
3043 * to the ndlp are done.
3044 */
3045 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003046 return;
3047 }
3048
dea31012005-04-17 16:05:31 -05003049 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003050 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003051 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003052 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003053
James Smart2e0fef82007-06-17 19:56:36 -05003054 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003055 /* when physical port receive logo donot start
3056 * vport discovery */
3057 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3058 lpfc_start_fdiscs(phba);
3059 else
3060 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smart92d7f7b2007-06-17 19:56:38 -05003061 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003062 }
3063
3064 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3065 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003066 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003067
3068 /* Drop the reference count from the mbox at the end after
3069 * all the current reference to the ndlp have been done.
3070 */
3071 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003072 return;
3073}
3074
3075/*
3076 * This routine handles processing a NameServer REG_LOGIN mailbox
3077 * command upon completion. It is setup in the LPFC_MBOXQ
3078 * as the completion routine when the command is
3079 * handed off to the SLI layer.
3080 */
3081void
James Smart2e0fef82007-06-17 19:56:36 -05003082lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003083{
James Smart21e9a0a2009-05-22 14:53:21 -04003084 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003085 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3086 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3087 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003088
3089 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003090out:
James Smart21e9a0a2009-05-22 14:53:21 -04003091 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3092 "0260 Register NameServer error: 0x%x\n",
3093 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003094 /* decrement the node reference count held for this
3095 * callback function.
3096 */
James Smart329f9bc2007-04-25 09:53:01 -04003097 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003098 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3099 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003100 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003101
3102 /* If no other thread is using the ndlp, free it */
3103 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003104
James Smart92d7f7b2007-06-17 19:56:38 -05003105 if (phba->fc_topology == TOPOLOGY_LOOP) {
3106 /*
3107 * RegLogin failed, use loop map to make discovery
3108 * list
3109 */
3110 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003111
James Smart92d7f7b2007-06-17 19:56:38 -05003112 /* Start discovery */
3113 lpfc_disc_start(vport);
3114 return;
3115 }
3116 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003117 return;
3118 }
3119
3120 pmb->context1 = NULL;
3121
dea31012005-04-17 16:05:31 -05003122 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003123 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003124 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003125 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003126
James Smart2e0fef82007-06-17 19:56:36 -05003127 if (vport->port_state < LPFC_VPORT_READY) {
3128 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003129 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3130 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3131 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3132 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3133 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3134
3135 /* Issue SCR just before NameServer GID_FT Query */
3136 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003137 }
3138
James Smart2e0fef82007-06-17 19:56:36 -05003139 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003140 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003141 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003142 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003143 goto out;
dea31012005-04-17 16:05:31 -05003144 }
3145
James Smartfa4066b2008-01-11 01:53:27 -05003146 /* decrement the node reference count held for this
3147 * callback function.
3148 */
James Smart329f9bc2007-04-25 09:53:01 -04003149 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003150 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3151 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003152 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003153
3154 return;
3155}
3156
3157static void
James Smart2e0fef82007-06-17 19:56:36 -05003158lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003159{
James Smart2e0fef82007-06-17 19:56:36 -05003160 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3161 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003162 struct lpfc_rport_data *rdata;
3163 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003164 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003165
3166 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003167 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3168 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003169 rport_ids.port_id = ndlp->nlp_DID;
3170 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003171
James Smart329f9bc2007-04-25 09:53:01 -04003172 /*
3173 * We leave our node pointer in rport->dd_data when we unregister a
3174 * FCP target port. But fc_remote_port_add zeros the space to which
3175 * rport->dd_data points. So, if we're reusing a previously
3176 * registered port, drop the reference that we took the last time we
3177 * registered the port.
3178 */
3179 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003180 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003181 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003182
3183 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3184 "rport add: did:x%x flg:x%x type x%x",
3185 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3186
James Smart2e0fef82007-06-17 19:56:36 -05003187 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003188 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003189 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3190 "Warning: fc_remote_port_add failed\n");
3191 return;
3192 }
3193
3194 /* initialize static port data */
3195 rport->maxframe_size = ndlp->nlp_maxframe;
3196 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003197 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003198 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003199
3200 if (ndlp->nlp_type & NLP_FCP_TARGET)
3201 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3202 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3203 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3204
3205
3206 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3207 fc_remote_port_rolechg(rport, rport_ids.roles);
3208
James Smart071fbd3d2006-04-15 11:53:20 -04003209 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003210 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003211 ndlp->nlp_sid = rport->scsi_target_id;
3212 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003213 return;
3214}
3215
3216static void
James Smart2e0fef82007-06-17 19:56:36 -05003217lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003218{
3219 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003220
James Smart858c9f62007-06-17 19:56:39 -05003221 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3222 "rport delete: did:x%x flg:x%x type x%x",
3223 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3224
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003225 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003226
3227 return;
3228}
3229
James Smartde0c5b32007-04-25 09:52:27 -04003230static void
James Smart2e0fef82007-06-17 19:56:36 -05003231lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003232{
James Smart2e0fef82007-06-17 19:56:36 -05003233 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3234
3235 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003236 switch (state) {
3237 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003238 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003239 break;
3240 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003241 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003242 break;
3243 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003244 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003245 break;
3246 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003247 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003248 break;
3249 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003250 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003251 break;
3252 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003253 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003254 break;
3255 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003256 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003257 break;
3258 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003259 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003260 break;
3261 }
James Smart2e0fef82007-06-17 19:56:36 -05003262 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003263}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003264
James Smartde0c5b32007-04-25 09:52:27 -04003265static void
James Smart2e0fef82007-06-17 19:56:36 -05003266lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003267 int old_state, int new_state)
3268{
James Smart2e0fef82007-06-17 19:56:36 -05003269 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3270
James Smartde0c5b32007-04-25 09:52:27 -04003271 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04003272 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3273 ndlp->nlp_type |= NLP_FC_NODE;
3274 }
3275 if (new_state == NLP_STE_MAPPED_NODE)
3276 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3277 if (new_state == NLP_STE_NPR_NODE)
3278 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003279
James Smartde0c5b32007-04-25 09:52:27 -04003280 /* Transport interface */
3281 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3282 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003283 vport->phba->nport_event_cnt++;
3284 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003285 }
dea31012005-04-17 16:05:31 -05003286
James Smartde0c5b32007-04-25 09:52:27 -04003287 if (new_state == NLP_STE_MAPPED_NODE ||
3288 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003289 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003290 /*
3291 * Tell the fc transport about the port, if we haven't
3292 * already. If we have, and it's a scsi entity, be
3293 * sure to unblock any attached scsi devices
3294 */
3295 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003296 }
James Smartea2151b2008-09-07 11:52:10 -04003297 if ((new_state == NLP_STE_MAPPED_NODE) &&
3298 (vport->stat_data_enabled)) {
3299 /*
3300 * A new target is discovered, if there is no buffer for
3301 * statistical data collection allocate buffer.
3302 */
3303 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3304 sizeof(struct lpfc_scsicmd_bkt),
3305 GFP_KERNEL);
3306
3307 if (!ndlp->lat_data)
3308 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3309 "0286 lpfc_nlp_state_cleanup failed to "
3310 "allocate statistical data buffer DID "
3311 "0x%x\n", ndlp->nlp_DID);
3312 }
James Smart858c9f62007-06-17 19:56:39 -05003313 /*
3314 * if we added to Mapped list, but the remote port
3315 * registration failed or assigned a target id outside
3316 * our presentable range - move the node to the
3317 * Unmapped List
3318 */
James Smartde0c5b32007-04-25 09:52:27 -04003319 if (new_state == NLP_STE_MAPPED_NODE &&
3320 (!ndlp->rport ||
3321 ndlp->rport->scsi_target_id == -1 ||
3322 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003323 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003324 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003325 spin_unlock_irq(shost->host_lock);
3326 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003327 }
James Smartde0c5b32007-04-25 09:52:27 -04003328}
3329
James Smart685f0bf2007-04-25 09:53:08 -04003330static char *
3331lpfc_nlp_state_name(char *buffer, size_t size, int state)
3332{
3333 static char *states[] = {
3334 [NLP_STE_UNUSED_NODE] = "UNUSED",
3335 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
3336 [NLP_STE_ADISC_ISSUE] = "ADISC",
3337 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
3338 [NLP_STE_PRLI_ISSUE] = "PRLI",
3339 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
3340 [NLP_STE_MAPPED_NODE] = "MAPPED",
3341 [NLP_STE_NPR_NODE] = "NPR",
3342 };
3343
James Smart311464e2007-08-02 11:10:37 -04003344 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04003345 strlcpy(buffer, states[state], size);
3346 else
3347 snprintf(buffer, size, "unknown (%d)", state);
3348 return buffer;
3349}
3350
James Smartde0c5b32007-04-25 09:52:27 -04003351void
James Smart2e0fef82007-06-17 19:56:36 -05003352lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3353 int state)
James Smartde0c5b32007-04-25 09:52:27 -04003354{
James Smart2e0fef82007-06-17 19:56:36 -05003355 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04003356 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04003357 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04003358
James Smarte8b62012007-08-02 11:10:09 -04003359 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3360 "0904 NPort state transition x%06x, %s -> %s\n",
3361 ndlp->nlp_DID,
3362 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
3363 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05003364
3365 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3366 "node statechg did:x%x old:%d ste:%d",
3367 ndlp->nlp_DID, old_state, state);
3368
James Smartde0c5b32007-04-25 09:52:27 -04003369 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04003370 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05003371 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003372 if (old_state == NLP_STE_UNMAPPED_NODE) {
3373 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
3374 ndlp->nlp_type &= ~NLP_FC_NODE;
3375 }
3376
James Smart685f0bf2007-04-25 09:53:08 -04003377 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05003378 spin_lock_irq(shost->host_lock);
3379 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3380 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003381 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05003382 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04003383
3384 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05003385 lpfc_nlp_counters(vport, state, 1);
3386 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04003387}
3388
3389void
James Smarte47c9092008-02-08 18:49:26 -05003390lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3391{
3392 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3393
3394 if (list_empty(&ndlp->nlp_listp)) {
3395 spin_lock_irq(shost->host_lock);
3396 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3397 spin_unlock_irq(shost->host_lock);
3398 }
3399}
3400
3401void
James Smart2e0fef82007-06-17 19:56:36 -05003402lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003403{
James Smart2e0fef82007-06-17 19:56:36 -05003404 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3405
James Smart0d2b6b82008-06-14 22:52:47 -04003406 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003407 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05003408 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3409 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003410 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05003411 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003412 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05003413 NLP_STE_UNUSED_NODE);
3414}
3415
Adrian Bunk4d9db012008-02-14 23:24:02 +02003416static void
James Smarte47c9092008-02-08 18:49:26 -05003417lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3418{
James Smart0d2b6b82008-06-14 22:52:47 -04003419 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003420 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3421 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3422 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3423 NLP_STE_UNUSED_NODE);
3424}
James Smart109f6ed2008-12-04 22:39:08 -05003425/**
James Smart3621a712009-04-06 18:47:14 -04003426 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05003427 * @vport: Pointer to Virtual Port object.
3428 * @ndlp: Pointer to FC node object.
3429 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04003430 *
3431 * This function is always called when node object need to be initialized.
3432 * It initializes all the fields of the node object. Although the reference
3433 * to phba from @ndlp can be obtained indirectly through it's reference to
3434 * @vport, a direct reference to phba is taken here by @ndlp. This is due
3435 * to the life-span of the @ndlp might go beyond the existence of @vport as
3436 * the final release of ndlp is determined by its reference count. And, the
3437 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05003438 **/
3439static inline void
3440lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3441 uint32_t did)
3442{
3443 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3444 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3445 init_timer(&ndlp->nlp_delayfunc);
3446 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3447 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3448 ndlp->nlp_DID = did;
3449 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04003450 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05003451 ndlp->nlp_sid = NLP_NO_SID;
3452 kref_init(&ndlp->kref);
3453 NLP_INT_NODE_ACT(ndlp);
3454 atomic_set(&ndlp->cmd_pending, 0);
3455 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
3456}
James Smarte47c9092008-02-08 18:49:26 -05003457
3458struct lpfc_nodelist *
3459lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3460 int state)
3461{
3462 struct lpfc_hba *phba = vport->phba;
3463 uint32_t did;
3464 unsigned long flags;
3465
3466 if (!ndlp)
3467 return NULL;
3468
3469 spin_lock_irqsave(&phba->ndlp_lock, flags);
3470 /* The ndlp should not be in memory free mode */
3471 if (NLP_CHK_FREE_REQ(ndlp)) {
3472 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3473 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3474 "0277 lpfc_enable_node: ndlp:x%p "
3475 "usgmap:x%x refcnt:%d\n",
3476 (void *)ndlp, ndlp->nlp_usg_map,
3477 atomic_read(&ndlp->kref.refcount));
3478 return NULL;
3479 }
3480 /* The ndlp should not already be in active mode */
3481 if (NLP_CHK_NODE_ACT(ndlp)) {
3482 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3483 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3484 "0278 lpfc_enable_node: ndlp:x%p "
3485 "usgmap:x%x refcnt:%d\n",
3486 (void *)ndlp, ndlp->nlp_usg_map,
3487 atomic_read(&ndlp->kref.refcount));
3488 return NULL;
3489 }
3490
3491 /* Keep the original DID */
3492 did = ndlp->nlp_DID;
3493
3494 /* re-initialize ndlp except of ndlp linked list pointer */
3495 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3496 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003497 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003498
3499 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3500
3501 if (state != NLP_STE_UNUSED_NODE)
3502 lpfc_nlp_set_state(vport, ndlp, state);
3503
3504 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3505 "node enable: did:x%x",
3506 ndlp->nlp_DID, 0, 0);
3507 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003508}
3509
3510void
James Smart2e0fef82007-06-17 19:56:36 -05003511lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003512{
James Smart87af33f2007-10-27 13:37:43 -04003513 /*
James Smartfa4066b2008-01-11 01:53:27 -05003514 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003515 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003516 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3517 * until ALL other outstanding threads have completed. We check
3518 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003519 */
James Smartfa4066b2008-01-11 01:53:27 -05003520 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3521 return;
James Smart51ef4c22007-08-02 11:10:31 -04003522 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003523 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003524 return;
dea31012005-04-17 16:05:31 -05003525}
3526
3527/*
3528 * Start / ReStart rescue timer for Discovery / RSCN handling
3529 */
3530void
James Smart2e0fef82007-06-17 19:56:36 -05003531lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003532{
James Smart2e0fef82007-06-17 19:56:36 -05003533 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3534 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003535 uint32_t tmo;
3536
James Smart2e0fef82007-06-17 19:56:36 -05003537 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003538 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003539 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3540 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003541 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003542 * FC spec states we need 3 * ratov for CT requests
3543 */
3544 tmo = ((phba->fc_ratov * 3) + 3);
3545 }
dea31012005-04-17 16:05:31 -05003546
James Smart858c9f62007-06-17 19:56:39 -05003547
3548 if (!timer_pending(&vport->fc_disctmo)) {
3549 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3550 "set disc timer: tmo:x%x state:x%x flg:x%x",
3551 tmo, vport->port_state, vport->fc_flag);
3552 }
3553
James Smart2e0fef82007-06-17 19:56:36 -05003554 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3555 spin_lock_irq(shost->host_lock);
3556 vport->fc_flag |= FC_DISC_TMO;
3557 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003558
3559 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003560 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3561 "0247 Start Discovery Timer state x%x "
3562 "Data: x%x x%lx x%x x%x\n",
3563 vport->port_state, tmo,
3564 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3565 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003566
3567 return;
3568}
3569
3570/*
3571 * Cancel rescue timer for Discovery / RSCN handling
3572 */
3573int
James Smart2e0fef82007-06-17 19:56:36 -05003574lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003575{
James Smart2e0fef82007-06-17 19:56:36 -05003576 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003577 unsigned long iflags;
3578
James Smart858c9f62007-06-17 19:56:39 -05003579 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3580 "can disc timer: state:x%x rtry:x%x flg:x%x",
3581 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3582
dea31012005-04-17 16:05:31 -05003583 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003584 if (vport->fc_flag & FC_DISC_TMO) {
3585 spin_lock_irqsave(shost->host_lock, iflags);
3586 vport->fc_flag &= ~FC_DISC_TMO;
3587 spin_unlock_irqrestore(shost->host_lock, iflags);
3588 del_timer_sync(&vport->fc_disctmo);
3589 spin_lock_irqsave(&vport->work_port_lock, iflags);
3590 vport->work_port_events &= ~WORKER_DISC_TMO;
3591 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003592 }
3593
3594 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003595 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3596 "0248 Cancel Discovery Timer state x%x "
3597 "Data: x%x x%x x%x\n",
3598 vport->port_state, vport->fc_flag,
3599 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003600 return 0;
dea31012005-04-17 16:05:31 -05003601}
3602
3603/*
3604 * Check specified ring for outstanding IOCB on the SLI queue
3605 * Return true if iocb matches the specified nport
3606 */
3607int
James Smart2e0fef82007-06-17 19:56:36 -05003608lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3609 struct lpfc_sli_ring *pring,
3610 struct lpfc_iocbq *iocb,
3611 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003612{
James Smart2e0fef82007-06-17 19:56:36 -05003613 struct lpfc_sli *psli = &phba->sli;
3614 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003615 struct lpfc_vport *vport = ndlp->vport;
3616
3617 if (iocb->vport != vport)
3618 return 0;
3619
dea31012005-04-17 16:05:31 -05003620 if (pring->ringno == LPFC_ELS_RING) {
3621 switch (icmd->ulpCommand) {
3622 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003623 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003624 return 1;
dea31012005-04-17 16:05:31 -05003625 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003626 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3627 return 1;
dea31012005-04-17 16:05:31 -05003628 case CMD_XMIT_ELS_RSP64_CX:
3629 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003630 return 1;
dea31012005-04-17 16:05:31 -05003631 }
James Smarta4bc3372006-12-02 13:34:16 -05003632 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003633
3634 } else if (pring->ringno == psli->fcp_ring) {
3635 /* Skip match check if waiting to relogin to FCP target */
3636 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003637 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003638 return 0;
dea31012005-04-17 16:05:31 -05003639 }
3640 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003641 return 1;
dea31012005-04-17 16:05:31 -05003642 }
3643 } else if (pring->ringno == psli->next_ring) {
3644
3645 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003646 return 0;
dea31012005-04-17 16:05:31 -05003647}
3648
3649/*
3650 * Free resources / clean up outstanding I/Os
3651 * associated with nlp_rpi in the LPFC_NODELIST entry.
3652 */
3653static int
James Smart2e0fef82007-06-17 19:56:36 -05003654lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003655{
James Smart2534ba72007-04-25 09:52:20 -04003656 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003657 struct lpfc_sli *psli;
3658 struct lpfc_sli_ring *pring;
3659 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003660 uint32_t i;
dea31012005-04-17 16:05:31 -05003661
James Smart92d7f7b2007-06-17 19:56:38 -05003662 lpfc_fabric_abort_nport(ndlp);
3663
dea31012005-04-17 16:05:31 -05003664 /*
3665 * Everything that matches on txcmplq will be returned
3666 * by firmware with a no rpi error.
3667 */
3668 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003669 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003670 /* Now process each ring */
3671 for (i = 0; i < psli->num_rings; i++) {
3672 pring = &psli->ring[i];
3673
James Smart2e0fef82007-06-17 19:56:36 -05003674 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003675 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003676 list) {
dea31012005-04-17 16:05:31 -05003677 /*
3678 * Check to see if iocb matches the nport we are
3679 * looking for
3680 */
James Smart92d7f7b2007-06-17 19:56:38 -05003681 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3682 ndlp))) {
dea31012005-04-17 16:05:31 -05003683 /* It matches, so deque and call compl
3684 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003685 list_move_tail(&iocb->list,
3686 &completions);
dea31012005-04-17 16:05:31 -05003687 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003688 }
3689 }
James Smart2e0fef82007-06-17 19:56:36 -05003690 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003691 }
3692 }
James Smart2534ba72007-04-25 09:52:20 -04003693
James Smarta257bf92009-04-06 18:48:10 -04003694 /* Cancel all the IOCBs from the completions list */
3695 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3696 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003697
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003698 return 0;
dea31012005-04-17 16:05:31 -05003699}
3700
3701/*
3702 * Free rpi associated with LPFC_NODELIST entry.
3703 * This routine is called from lpfc_freenode(), when we are removing
3704 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3705 * LOGO that completes successfully, and we are waiting to PLOGI back
3706 * to the remote NPort. In addition, it is called after we receive
3707 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3708 * we are waiting to PLOGI back to the remote NPort.
3709 */
3710int
James Smart2e0fef82007-06-17 19:56:36 -05003711lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003712{
James Smart2e0fef82007-06-17 19:56:36 -05003713 struct lpfc_hba *phba = vport->phba;
3714 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003715 int rc;
3716
James Smart21e9a0a2009-05-22 14:53:21 -04003717 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003718 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3719 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003720 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003721 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003722 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003723 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003724 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003725 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003726 }
dea31012005-04-17 16:05:31 -05003727 lpfc_no_rpi(phba, ndlp);
3728 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003729 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003730 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003731 return 1;
3732 }
3733 return 0;
3734}
3735
James Smartecfd03c2010-02-12 14:41:27 -05003736/**
3737 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
3738 * @phba: pointer to lpfc hba data structure.
3739 *
3740 * This routine is invoked to unregister all the currently registered RPIs
3741 * to the HBA.
3742 **/
3743void
3744lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
3745{
3746 struct lpfc_vport **vports;
3747 struct lpfc_nodelist *ndlp;
3748 struct Scsi_Host *shost;
3749 int i;
3750
3751 vports = lpfc_create_vport_work_array(phba);
3752 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3753 shost = lpfc_shost_from_vport(vports[i]);
3754 spin_lock_irq(shost->host_lock);
3755 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003756 if (ndlp->nlp_flag & NLP_RPI_VALID) {
3757 /* The mempool_alloc might sleep */
3758 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05003759 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05003760 spin_lock_irq(shost->host_lock);
3761 }
James Smartecfd03c2010-02-12 14:41:27 -05003762 }
3763 spin_unlock_irq(shost->host_lock);
3764 }
3765 lpfc_destroy_vport_work_array(phba, vports);
3766}
3767
James Smart92d7f7b2007-06-17 19:56:38 -05003768void
3769lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3770{
3771 struct lpfc_hba *phba = vport->phba;
3772 LPFC_MBOXQ_t *mbox;
3773 int rc;
3774
3775 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3776 if (mbox) {
3777 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3778 mbox->vport = vport;
3779 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003780 mbox->context1 = NULL;
3781 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003782 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003783 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003784
3785 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3786 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3787 "1836 Could not issue "
3788 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003789 }
3790}
3791
3792void
3793lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3794{
3795 struct lpfc_hba *phba = vport->phba;
3796 LPFC_MBOXQ_t *mbox;
3797 int rc;
3798
3799 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3800 if (mbox) {
3801 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3802 mbox->vport = vport;
3803 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003804 mbox->context1 = NULL;
3805 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003806 if (rc != MBX_TIMEOUT)
3807 mempool_free(mbox, phba->mbox_mem_pool);
3808
3809 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003810 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3811 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003812 "unreg_did (default rpis) status %d\n",
3813 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003814 }
3815}
3816
dea31012005-04-17 16:05:31 -05003817/*
3818 * Free resources associated with LPFC_NODELIST entry
3819 * so it can be freed.
3820 */
3821static int
James Smart2e0fef82007-06-17 19:56:36 -05003822lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003823{
James Smart2e0fef82007-06-17 19:56:36 -05003824 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3825 struct lpfc_hba *phba = vport->phba;
3826 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003827 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003828
3829 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003830 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3831 "0900 Cleanup node for NPort x%x "
3832 "Data: x%x x%x x%x\n",
3833 ndlp->nlp_DID, ndlp->nlp_flag,
3834 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003835 if (NLP_CHK_FREE_REQ(ndlp)) {
3836 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3837 "0280 lpfc_cleanup_node: ndlp:x%p "
3838 "usgmap:x%x refcnt:%d\n",
3839 (void *)ndlp, ndlp->nlp_usg_map,
3840 atomic_read(&ndlp->kref.refcount));
3841 lpfc_dequeue_node(vport, ndlp);
3842 } else {
3843 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3844 "0281 lpfc_cleanup_node: ndlp:x%p "
3845 "usgmap:x%x refcnt:%d\n",
3846 (void *)ndlp, ndlp->nlp_usg_map,
3847 atomic_read(&ndlp->kref.refcount));
3848 lpfc_disable_node(vport, ndlp);
3849 }
dea31012005-04-17 16:05:31 -05003850
dea31012005-04-17 16:05:31 -05003851 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3852 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003853 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003854 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3855 mb->context2 = NULL;
3856 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3857 }
3858 }
James Smart33ccf8d2006-08-17 11:57:58 -04003859
James Smart2e0fef82007-06-17 19:56:36 -05003860 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003861 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003862 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003863 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003864 mp = (struct lpfc_dmabuf *) (mb->context1);
3865 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003866 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003867 kfree(mp);
3868 }
3869 list_del(&mb->list);
James Smartffc95492010-06-07 15:23:17 -04003870 if (phba->sli_rev == LPFC_SLI_REV4)
3871 lpfc_sli4_free_rpi(phba,
3872 mb->u.mb.un.varRegLogin.rpi);
dea31012005-04-17 16:05:31 -05003873 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003874 /* We shall not invoke the lpfc_nlp_put to decrement
3875 * the ndlp reference count as we are in the process
3876 * of lpfc_nlp_release.
3877 */
dea31012005-04-17 16:05:31 -05003878 }
3879 }
James Smart2e0fef82007-06-17 19:56:36 -05003880 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003881
James Smarte47c9092008-02-08 18:49:26 -05003882 lpfc_els_abort(phba, ndlp);
3883
James Smart2e0fef82007-06-17 19:56:36 -05003884 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003885 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003886 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003887
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003888 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003889 del_timer_sync(&ndlp->nlp_delayfunc);
3890
James Smart0d2b6b82008-06-14 22:52:47 -04003891 list_del_init(&ndlp->els_retry_evt.evt_listp);
3892 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003893
James Smart2e0fef82007-06-17 19:56:36 -05003894 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003895
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003896 return 0;
dea31012005-04-17 16:05:31 -05003897}
3898
3899/*
3900 * Check to see if we can free the nlp back to the freelist.
3901 * If we are in the middle of using the nlp in the discovery state
3902 * machine, defer the free till we reach the end of the state machine.
3903 */
James Smart329f9bc2007-04-25 09:53:01 -04003904static void
James Smart2e0fef82007-06-17 19:56:36 -05003905lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003906{
James Smarta8adb832007-10-27 13:37:53 -04003907 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003908 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003909 LPFC_MBOXQ_t *mbox;
3910 int rc;
dea31012005-04-17 16:05:31 -05003911
James Smart0d2b6b82008-06-14 22:52:47 -04003912 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003913 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smartffc95492010-06-07 15:23:17 -04003914 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
James Smart21e9a0a2009-05-22 14:53:21 -04003915 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003916 /* For this case we need to cleanup the default rpi
3917 * allocated by the firmware.
3918 */
3919 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3920 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003921 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003922 (uint8_t *) &vport->fc_sparam, mbox, 0);
3923 if (rc) {
3924 mempool_free(mbox, phba->mbox_mem_pool);
3925 }
3926 else {
3927 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3928 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3929 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003930 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003931 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3932 if (rc == MBX_NOT_FINISHED) {
3933 mempool_free(mbox, phba->mbox_mem_pool);
3934 }
3935 }
3936 }
3937 }
James Smart2e0fef82007-06-17 19:56:36 -05003938 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003939
James Smart2e0fef82007-06-17 19:56:36 -05003940 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003941 * We can get here with a non-NULL ndlp->rport because when we
3942 * unregister a rport we don't break the rport/node linkage. So if we
3943 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003944 */
James Smart92d7f7b2007-06-17 19:56:38 -05003945 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003946 rdata = ndlp->rport->dd_data;
3947 rdata->pnode = NULL;
3948 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003949 }
dea31012005-04-17 16:05:31 -05003950}
3951
3952static int
James Smart2e0fef82007-06-17 19:56:36 -05003953lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3954 uint32_t did)
dea31012005-04-17 16:05:31 -05003955{
James Smart2e0fef82007-06-17 19:56:36 -05003956 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003957
3958 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003959 return 0;
dea31012005-04-17 16:05:31 -05003960
dea31012005-04-17 16:05:31 -05003961 /* First check for Direct match */
3962 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003963 return 1;
dea31012005-04-17 16:05:31 -05003964
3965 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003966 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003967 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003968 return 0;
dea31012005-04-17 16:05:31 -05003969 }
3970
3971 matchdid.un.word = did;
3972 ndlpdid.un.word = ndlp->nlp_DID;
3973 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3974 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3975 (mydid.un.b.area == matchdid.un.b.area)) {
3976 if ((ndlpdid.un.b.domain == 0) &&
3977 (ndlpdid.un.b.area == 0)) {
3978 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003979 return 1;
dea31012005-04-17 16:05:31 -05003980 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003981 return 0;
dea31012005-04-17 16:05:31 -05003982 }
3983
3984 matchdid.un.word = ndlp->nlp_DID;
3985 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3986 (mydid.un.b.area == ndlpdid.un.b.area)) {
3987 if ((matchdid.un.b.domain == 0) &&
3988 (matchdid.un.b.area == 0)) {
3989 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003990 return 1;
dea31012005-04-17 16:05:31 -05003991 }
3992 }
3993 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003994 return 0;
dea31012005-04-17 16:05:31 -05003995}
3996
James Smart685f0bf2007-04-25 09:53:08 -04003997/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003998static struct lpfc_nodelist *
3999__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004000{
James Smart2fb9bd82006-12-02 13:33:57 -05004001 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004002 uint32_t data1;
4003
James Smart2e0fef82007-06-17 19:56:36 -05004004 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4005 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004006 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4007 ((uint32_t) ndlp->nlp_xri << 16) |
4008 ((uint32_t) ndlp->nlp_type << 8) |
4009 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004010 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4011 "0929 FIND node DID "
4012 "Data: x%p x%x x%x x%x\n",
4013 ndlp, ndlp->nlp_DID,
4014 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04004015 return ndlp;
dea31012005-04-17 16:05:31 -05004016 }
4017 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004018
dea31012005-04-17 16:05:31 -05004019 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004020 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4021 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004022 return NULL;
4023}
4024
4025struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004026lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004027{
James Smart2e0fef82007-06-17 19:56:36 -05004028 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004029 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004030
James Smart2e0fef82007-06-17 19:56:36 -05004031 spin_lock_irq(shost->host_lock);
4032 ndlp = __lpfc_findnode_did(vport, did);
4033 spin_unlock_irq(shost->host_lock);
4034 return ndlp;
4035}
4036
4037struct lpfc_nodelist *
4038lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4039{
4040 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4041 struct lpfc_nodelist *ndlp;
4042
4043 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004044 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004045 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4046 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004047 return NULL;
4048 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004049 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004050 if (!ndlp)
4051 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004052 lpfc_nlp_init(vport, ndlp, did);
4053 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4054 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004055 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004056 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004057 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004058 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4059 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4060 if (!ndlp)
4061 return NULL;
4062 spin_lock_irq(shost->host_lock);
4063 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4064 spin_unlock_irq(shost->host_lock);
4065 return ndlp;
dea31012005-04-17 16:05:31 -05004066 }
James Smarte47c9092008-02-08 18:49:26 -05004067
James Smart58da1ff2008-04-07 10:15:56 -04004068 if ((vport->fc_flag & FC_RSCN_MODE) &&
4069 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004070 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04004071 /* If we've already recieved a PLOGI from this NPort
4072 * we don't need to try to discover it again.
4073 */
4074 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4075 return NULL;
4076
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004077 /* Since this node is marked for discovery,
4078 * delay timeout is not needed.
4079 */
James Smart0d2b6b82008-06-14 22:52:47 -04004080 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004081 spin_lock_irq(shost->host_lock);
4082 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4083 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004084 } else
dea31012005-04-17 16:05:31 -05004085 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004086 } else {
James Smart87af33f2007-10-27 13:37:43 -04004087 /* If we've already recieved a PLOGI from this NPort,
4088 * or we are already in the process of discovery on it,
4089 * we don't need to try to discover it again.
4090 */
James Smart685f0bf2007-04-25 09:53:08 -04004091 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004092 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4093 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004094 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004095 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4096 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004097 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004098 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004099 }
4100 return ndlp;
4101}
4102
4103/* Build a list of nodes to discover based on the loopmap */
4104void
James Smart2e0fef82007-06-17 19:56:36 -05004105lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004106{
James Smart2e0fef82007-06-17 19:56:36 -05004107 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004108 int j;
4109 uint32_t alpa, index;
4110
James Smart2e0fef82007-06-17 19:56:36 -05004111 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004112 return;
James Smart2e0fef82007-06-17 19:56:36 -05004113
4114 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004115 return;
dea31012005-04-17 16:05:31 -05004116
4117 /* Check for loop map present or not */
4118 if (phba->alpa_map[0]) {
4119 for (j = 1; j <= phba->alpa_map[0]; j++) {
4120 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004121 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004122 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004123 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004124 }
4125 } else {
4126 /* No alpamap, so try all alpa's */
4127 for (j = 0; j < FC_MAXLOOP; j++) {
4128 /* If cfg_scan_down is set, start from highest
4129 * ALPA (0xef) to lowest (0x1).
4130 */
James Smart3de2a652007-08-02 11:09:59 -04004131 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004132 index = j;
4133 else
4134 index = FC_MAXLOOP - j - 1;
4135 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004136 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004137 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004138 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004139 }
4140 }
4141 return;
4142}
4143
dea31012005-04-17 16:05:31 -05004144void
James Smart2e0fef82007-06-17 19:56:36 -05004145lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004146{
dea31012005-04-17 16:05:31 -05004147 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004148 struct lpfc_sli *psli = &phba->sli;
4149 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4150 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4151 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4152 int rc;
4153
James Smart92d7f7b2007-06-17 19:56:38 -05004154 /*
4155 * if it's not a physical port or if we already send
4156 * clear_la then don't send it.
4157 */
4158 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004159 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4160 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004161 return;
4162
James Smart2e0fef82007-06-17 19:56:36 -05004163 /* Link up discovery */
4164 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4165 phba->link_state = LPFC_CLEAR_LA;
4166 lpfc_clear_la(phba, mbox);
4167 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4168 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004169 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004170 if (rc == MBX_NOT_FINISHED) {
4171 mempool_free(mbox, phba->mbox_mem_pool);
4172 lpfc_disc_flush_list(vport);
4173 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4174 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4175 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004176 phba->link_state = LPFC_HBA_ERROR;
4177 }
4178 }
4179}
4180
4181/* Reg_vpi to tell firmware to resume normal operations */
4182void
4183lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4184{
4185 LPFC_MBOXQ_t *regvpimbox;
4186
4187 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4188 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004189 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004190 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4191 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004192 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004193 == MBX_NOT_FINISHED) {
4194 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004195 }
4196 }
4197}
4198
4199/* Start Link up / RSCN discovery on NPR nodes */
4200void
4201lpfc_disc_start(struct lpfc_vport *vport)
4202{
4203 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4204 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004205 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004206 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004207 int did_changed;
dea31012005-04-17 16:05:31 -05004208
James Smart2e0fef82007-06-17 19:56:36 -05004209 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004210 return;
James Smart2e0fef82007-06-17 19:56:36 -05004211
4212 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004213 clear_la_pending = 1;
4214 else
4215 clear_la_pending = 0;
4216
James Smart2e0fef82007-06-17 19:56:36 -05004217 if (vport->port_state < LPFC_VPORT_READY)
4218 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004219
James Smart2e0fef82007-06-17 19:56:36 -05004220 lpfc_set_disctmo(vport);
4221
4222 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004223 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004224 else
dea31012005-04-17 16:05:31 -05004225 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004226
4227 vport->fc_prevDID = vport->fc_myDID;
4228 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004229
4230 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004231 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4232 "0202 Start Discovery hba state x%x "
4233 "Data: x%x x%x x%x\n",
4234 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4235 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004236
4237 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004238 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004239
4240 if (num_sent)
4241 return;
4242
James Smart92d7f7b2007-06-17 19:56:38 -05004243 /*
4244 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
4245 * continue discovery.
4246 */
4247 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004248 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004249 !(vport->fc_flag & FC_RSCN_MODE) &&
4250 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004251 lpfc_issue_reg_vpi(phba, vport);
4252 return;
4253 }
James Smart2e0fef82007-06-17 19:56:36 -05004254
James Smart92d7f7b2007-06-17 19:56:38 -05004255 /*
4256 * For SLI2, we need to set port_state to READY and continue
4257 * discovery.
4258 */
4259 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4260 /* If we get here, there is nothing to ADISC */
4261 if (vport->port_type == LPFC_PHYSICAL_PORT)
4262 lpfc_issue_clear_la(phba, vport);
4263
4264 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004265 vport->num_disc_nodes = 0;
4266 /* go thru NPR nodes and issue ELS PLOGIs */
4267 if (vport->fc_npr_cnt)
4268 lpfc_els_disc_plogi(vport);
4269
4270 if (!vport->num_disc_nodes) {
4271 spin_lock_irq(shost->host_lock);
4272 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4273 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004274 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05004275 }
4276 }
James Smart92d7f7b2007-06-17 19:56:38 -05004277 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004278 } else {
4279 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004280 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05004281
4282 if (num_sent)
4283 return;
4284
James Smart2e0fef82007-06-17 19:56:36 -05004285 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05004286 /* Check to see if more RSCNs came in while we
4287 * were processing this one.
4288 */
James Smart2e0fef82007-06-17 19:56:36 -05004289 if ((vport->fc_rscn_id_cnt == 0) &&
4290 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
4291 spin_lock_irq(shost->host_lock);
4292 vport->fc_flag &= ~FC_RSCN_MODE;
4293 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004294 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004295 } else
James Smart2e0fef82007-06-17 19:56:36 -05004296 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05004297 }
4298 }
4299 return;
4300}
4301
4302/*
4303 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
4304 * ring the match the sppecified nodelist.
4305 */
4306static void
James Smart2e0fef82007-06-17 19:56:36 -05004307lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004308{
James Smart2534ba72007-04-25 09:52:20 -04004309 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004310 struct lpfc_sli *psli;
4311 IOCB_t *icmd;
4312 struct lpfc_iocbq *iocb, *next_iocb;
4313 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004314
4315 psli = &phba->sli;
4316 pring = &psli->ring[LPFC_ELS_RING];
4317
4318 /* Error matching iocb on txq or txcmplq
4319 * First check the txq.
4320 */
James Smart2e0fef82007-06-17 19:56:36 -05004321 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004322 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4323 if (iocb->context1 != ndlp) {
4324 continue;
4325 }
4326 icmd = &iocb->iocb;
4327 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
4328 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
4329
James Smart2534ba72007-04-25 09:52:20 -04004330 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05004331 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05004332 }
4333 }
4334
4335 /* Next check the txcmplq */
4336 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
4337 if (iocb->context1 != ndlp) {
4338 continue;
4339 }
4340 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004341 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
4342 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04004343 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05004344 }
4345 }
James Smart2e0fef82007-06-17 19:56:36 -05004346 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04004347
James Smarta257bf92009-04-06 18:48:10 -04004348 /* Cancel all the IOCBs from the completions list */
4349 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4350 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004351}
4352
Adrian Bunka6ababd2007-11-05 18:07:33 +01004353static void
James Smart2e0fef82007-06-17 19:56:36 -05004354lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004355{
4356 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05004357 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004358
James Smart2e0fef82007-06-17 19:56:36 -05004359 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
4360 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004361 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004362 if (!NLP_CHK_NODE_ACT(ndlp))
4363 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004364 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4365 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
4366 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04004367 }
dea31012005-04-17 16:05:31 -05004368 }
4369 }
dea31012005-04-17 16:05:31 -05004370}
4371
James Smart92d7f7b2007-06-17 19:56:38 -05004372void
4373lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
4374{
4375 lpfc_els_flush_rscn(vport);
4376 lpfc_els_flush_cmd(vport);
4377 lpfc_disc_flush_list(vport);
4378}
4379
dea31012005-04-17 16:05:31 -05004380/*****************************************************************************/
4381/*
4382 * NAME: lpfc_disc_timeout
4383 *
4384 * FUNCTION: Fibre Channel driver discovery timeout routine.
4385 *
4386 * EXECUTION ENVIRONMENT: interrupt only
4387 *
4388 * CALLED FROM:
4389 * Timer function
4390 *
4391 * RETURNS:
4392 * none
4393 */
4394/*****************************************************************************/
4395void
4396lpfc_disc_timeout(unsigned long ptr)
4397{
James Smart2e0fef82007-06-17 19:56:36 -05004398 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4399 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04004400 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004401 unsigned long flags = 0;
4402
4403 if (unlikely(!phba))
4404 return;
4405
James Smart5e9d9b82008-06-14 22:52:53 -04004406 spin_lock_irqsave(&vport->work_port_lock, flags);
4407 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4408 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05004409 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04004410 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05004411
James Smart5e9d9b82008-06-14 22:52:53 -04004412 if (!tmo_posted)
4413 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004414 return;
4415}
4416
4417static void
James Smart2e0fef82007-06-17 19:56:36 -05004418lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004419{
James Smart2e0fef82007-06-17 19:56:36 -05004420 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4421 struct lpfc_hba *phba = vport->phba;
4422 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004423 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05004424 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05004425 int rc, clrlaerr = 0;
4426
James Smart2e0fef82007-06-17 19:56:36 -05004427 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05004428 return;
4429
James Smart2e0fef82007-06-17 19:56:36 -05004430 spin_lock_irq(shost->host_lock);
4431 vport->fc_flag &= ~FC_DISC_TMO;
4432 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004433
James Smart858c9f62007-06-17 19:56:39 -05004434 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4435 "disc timeout: state:x%x rtry:x%x flg:x%x",
4436 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4437
James Smart2e0fef82007-06-17 19:56:36 -05004438 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05004439
4440 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05004441 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
4442 * FAN
4443 */
4444 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04004445 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4446 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004447 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05004448 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004449 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004450 if (!NLP_CHK_NODE_ACT(ndlp))
4451 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004452 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4453 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004454 if (ndlp->nlp_type & NLP_FABRIC) {
4455 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05004456 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004457
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004458 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004459 /* Fail outstanding IO now since device
4460 * is marked for PLOGI.
4461 */
James Smart2e0fef82007-06-17 19:56:36 -05004462 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004463 }
4464 }
James Smart92d7f7b2007-06-17 19:56:38 -05004465 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05004466 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05004467 return;
James Smart92d7f7b2007-06-17 19:56:38 -05004468 }
dea31012005-04-17 16:05:31 -05004469 break;
4470
James Smart92d7f7b2007-06-17 19:56:38 -05004471 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05004472 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05004473 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05004474 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04004475 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4476 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04004477 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05004478
4479 /* Assume no Fabric and go on with discovery.
4480 * Check for outstanding ELS FLOGI to abort.
4481 */
4482
4483 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05004484 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004485
4486 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05004487 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05004488 break;
4489
4490 case LPFC_FABRIC_CFG_LINK:
4491 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4492 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04004493 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4494 "0223 Timeout while waiting for "
4495 "NameServer login\n");
dea31012005-04-17 16:05:31 -05004496 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05004497 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05004498 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04004499 lpfc_els_abort(phba, ndlp);
4500
4501 /* ReStart discovery */
4502 goto restart_disc;
dea31012005-04-17 16:05:31 -05004503
4504 case LPFC_NS_QRY:
4505 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04004506 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4507 "0224 NameServer Query timeout "
4508 "Data: x%x x%x\n",
4509 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004510
James Smart92d7f7b2007-06-17 19:56:38 -05004511 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4512 /* Try it one more time */
4513 vport->fc_ns_retry++;
4514 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4515 vport->fc_ns_retry, 0);
4516 if (rc == 0)
4517 break;
dea31012005-04-17 16:05:31 -05004518 }
James Smart92d7f7b2007-06-17 19:56:38 -05004519 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05004520
James Smart87af33f2007-10-27 13:37:43 -04004521restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05004522 /*
4523 * Discovery is over.
4524 * set port_state to PORT_READY if SLI2.
4525 * cmpl_reg_vpi will set port_state to READY for SLI3.
4526 */
James Smart3772a992009-05-22 14:50:54 -04004527 if (phba->sli_rev < LPFC_SLI_REV4) {
4528 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4529 lpfc_issue_reg_vpi(phba, vport);
4530 else { /* NPIV Not enabled */
4531 lpfc_issue_clear_la(phba, vport);
4532 vport->port_state = LPFC_VPORT_READY;
4533 }
dea31012005-04-17 16:05:31 -05004534 }
4535
4536 /* Setup and issue mailbox INITIALIZE LINK command */
4537 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4538 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004539 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4540 "0206 Device Discovery "
4541 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004542 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004543 break;
4544 }
4545
4546 lpfc_linkdown(phba);
4547 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4548 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004549 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004550 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004551 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004552 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004553 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004554 if (rc == MBX_NOT_FINISHED)
4555 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4556
4557 break;
4558
4559 case LPFC_DISC_AUTH:
4560 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004561 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4562 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004563 lpfc_disc_flush_list(vport);
4564
James Smart92d7f7b2007-06-17 19:56:38 -05004565 /*
4566 * set port_state to PORT_READY if SLI2.
4567 * cmpl_reg_vpi will set port_state to READY for SLI3.
4568 */
James Smart3772a992009-05-22 14:50:54 -04004569 if (phba->sli_rev < LPFC_SLI_REV4) {
4570 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4571 lpfc_issue_reg_vpi(phba, vport);
4572 else { /* NPIV Not enabled */
4573 lpfc_issue_clear_la(phba, vport);
4574 vport->port_state = LPFC_VPORT_READY;
4575 }
dea31012005-04-17 16:05:31 -05004576 }
4577 break;
4578
James Smart2e0fef82007-06-17 19:56:36 -05004579 case LPFC_VPORT_READY:
4580 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004581 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4582 "0231 RSCN timeout Data: x%x "
4583 "x%x\n",
4584 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004585
4586 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004587 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004588
James Smart2e0fef82007-06-17 19:56:36 -05004589 lpfc_els_flush_rscn(vport);
4590 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004591 }
4592 break;
James Smart2e0fef82007-06-17 19:56:36 -05004593
James Smart92d7f7b2007-06-17 19:56:38 -05004594 default:
James Smarte8b62012007-08-02 11:10:09 -04004595 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004596 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004597 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004598 break;
4599 }
4600
4601 switch (phba->link_state) {
4602 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004603 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004604 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4605 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004606 clrlaerr = 1;
4607 break;
4608
James Smart09372822008-01-11 01:52:54 -05004609 case LPFC_LINK_UP:
4610 lpfc_issue_clear_la(phba, vport);
4611 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004612 case LPFC_LINK_UNKNOWN:
4613 case LPFC_WARM_START:
4614 case LPFC_INIT_START:
4615 case LPFC_INIT_MBX_CMDS:
4616 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004617 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004618 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4619 "0230 Unexpected timeout, hba link "
4620 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004621 clrlaerr = 1;
4622 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004623
4624 case LPFC_HBA_READY:
4625 break;
dea31012005-04-17 16:05:31 -05004626 }
4627
4628 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004629 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004630 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004631 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4632 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004633 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004634 }
4635
4636 return;
4637}
4638
dea31012005-04-17 16:05:31 -05004639/*
4640 * This routine handles processing a NameServer REG_LOGIN mailbox
4641 * command upon completion. It is setup in the LPFC_MBOXQ
4642 * as the completion routine when the command is
4643 * handed off to the SLI layer.
4644 */
4645void
James Smart2e0fef82007-06-17 19:56:36 -05004646lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004647{
James Smart04c68492009-05-22 14:52:52 -04004648 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004649 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4650 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4651 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004652
4653 pmb->context1 = NULL;
4654
dea31012005-04-17 16:05:31 -05004655 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004656 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004657 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004658 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004659
James Smart2e0fef82007-06-17 19:56:36 -05004660 /*
4661 * Start issuing Fabric-Device Management Interface (FDMI) command to
4662 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4663 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004664 */
James Smart2e0fef82007-06-17 19:56:36 -05004665
James Smart3de2a652007-08-02 11:09:59 -04004666 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004667 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4668 else
4669 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004670
James Smartfa4066b2008-01-11 01:53:27 -05004671 /* decrement the node reference count held for this callback
4672 * function.
4673 */
James Smart329f9bc2007-04-25 09:53:01 -04004674 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004675 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4676 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004677 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004678
4679 return;
4680}
4681
James Smart685f0bf2007-04-25 09:53:08 -04004682static int
4683lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4684{
4685 uint16_t *rpi = param;
4686
4687 return ndlp->nlp_rpi == *rpi;
4688}
4689
4690static int
4691lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4692{
4693 return memcmp(&ndlp->nlp_portname, param,
4694 sizeof(ndlp->nlp_portname)) == 0;
4695}
4696
Adrian Bunka6ababd2007-11-05 18:07:33 +01004697static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004698__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004699{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004700 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004701
James Smart2e0fef82007-06-17 19:56:36 -05004702 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004703 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004704 return ndlp;
4705 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004706 return NULL;
dea31012005-04-17 16:05:31 -05004707}
4708
James Smart685f0bf2007-04-25 09:53:08 -04004709/*
4710 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004711 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004712 */
4713struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004714__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004715{
James Smart2e0fef82007-06-17 19:56:36 -05004716 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004717}
4718
James Smart488d1462006-03-07 15:02:37 -05004719/*
James Smart685f0bf2007-04-25 09:53:08 -04004720 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004721 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004722 */
4723struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004724lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004725{
James Smart2e0fef82007-06-17 19:56:36 -05004726 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004727 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004728
James Smart2e0fef82007-06-17 19:56:36 -05004729 spin_lock_irq(shost->host_lock);
4730 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4731 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004732 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004733}
4734
dea31012005-04-17 16:05:31 -05004735void
James Smart2e0fef82007-06-17 19:56:36 -05004736lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4737 uint32_t did)
dea31012005-04-17 16:05:31 -05004738{
4739 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004740
4741 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004742 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004743
4744 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4745 "node init: did:x%x",
4746 ndlp->nlp_DID, 0, 0);
4747
dea31012005-04-17 16:05:31 -05004748 return;
4749}
James Smart329f9bc2007-04-25 09:53:01 -04004750
James Smart98c9ea52007-10-27 13:37:33 -04004751/* This routine releases all resources associated with a specifc NPort's ndlp
4752 * and mempool_free's the nodelist.
4753 */
James Smart311464e2007-08-02 11:10:37 -04004754static void
James Smart329f9bc2007-04-25 09:53:01 -04004755lpfc_nlp_release(struct kref *kref)
4756{
James Smarte47c9092008-02-08 18:49:26 -05004757 struct lpfc_hba *phba;
4758 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004759 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4760 kref);
James Smart858c9f62007-06-17 19:56:39 -05004761
4762 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4763 "node release: did:x%x flg:x%x type:x%x",
4764 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4765
James Smarte47c9092008-02-08 18:49:26 -05004766 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4767 "0279 lpfc_nlp_release: ndlp:x%p "
4768 "usgmap:x%x refcnt:%d\n",
4769 (void *)ndlp, ndlp->nlp_usg_map,
4770 atomic_read(&ndlp->kref.refcount));
4771
4772 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004773 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004774
4775 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004776 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004777 spin_lock_irqsave(&phba->ndlp_lock, flags);
4778 NLP_CLR_NODE_ACT(ndlp);
4779 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4780
4781 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004782 if (NLP_CHK_FREE_REQ(ndlp)) {
4783 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004784 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004785 }
James Smart329f9bc2007-04-25 09:53:01 -04004786}
4787
James Smart98c9ea52007-10-27 13:37:33 -04004788/* This routine bumps the reference count for a ndlp structure to ensure
4789 * that one discovery thread won't free a ndlp while another discovery thread
4790 * is using it.
4791 */
James Smart329f9bc2007-04-25 09:53:01 -04004792struct lpfc_nodelist *
4793lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4794{
James Smarte47c9092008-02-08 18:49:26 -05004795 struct lpfc_hba *phba;
4796 unsigned long flags;
4797
James Smart98c9ea52007-10-27 13:37:33 -04004798 if (ndlp) {
4799 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4800 "node get: did:x%x flg:x%x refcnt:x%x",
4801 ndlp->nlp_DID, ndlp->nlp_flag,
4802 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004803 /* The check of ndlp usage to prevent incrementing the
4804 * ndlp reference count that is in the process of being
4805 * released.
4806 */
James Smarta257bf92009-04-06 18:48:10 -04004807 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004808 spin_lock_irqsave(&phba->ndlp_lock, flags);
4809 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4810 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4811 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4812 "0276 lpfc_nlp_get: ndlp:x%p "
4813 "usgmap:x%x refcnt:%d\n",
4814 (void *)ndlp, ndlp->nlp_usg_map,
4815 atomic_read(&ndlp->kref.refcount));
4816 return NULL;
4817 } else
4818 kref_get(&ndlp->kref);
4819 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004820 }
James Smart329f9bc2007-04-25 09:53:01 -04004821 return ndlp;
4822}
4823
James Smart98c9ea52007-10-27 13:37:33 -04004824/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004825 * count goes to 0, this indicates the the associated nodelist should be
4826 * freed. Returning 1 indicates the ndlp resource has been released; on the
4827 * other hand, returning 0 indicates the ndlp resource has not been released
4828 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004829 */
James Smart329f9bc2007-04-25 09:53:01 -04004830int
4831lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4832{
James Smarte47c9092008-02-08 18:49:26 -05004833 struct lpfc_hba *phba;
4834 unsigned long flags;
4835
4836 if (!ndlp)
4837 return 1;
4838
4839 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4840 "node put: did:x%x flg:x%x refcnt:x%x",
4841 ndlp->nlp_DID, ndlp->nlp_flag,
4842 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004843 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004844 spin_lock_irqsave(&phba->ndlp_lock, flags);
4845 /* Check the ndlp memory free acknowledge flag to avoid the
4846 * possible race condition that kref_put got invoked again
4847 * after previous one has done ndlp memory free.
4848 */
4849 if (NLP_CHK_FREE_ACK(ndlp)) {
4850 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4851 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4852 "0274 lpfc_nlp_put: ndlp:x%p "
4853 "usgmap:x%x refcnt:%d\n",
4854 (void *)ndlp, ndlp->nlp_usg_map,
4855 atomic_read(&ndlp->kref.refcount));
4856 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004857 }
James Smarte47c9092008-02-08 18:49:26 -05004858 /* Check the ndlp inactivate log flag to avoid the possible
4859 * race condition that kref_put got invoked again after ndlp
4860 * is already in inactivating state.
4861 */
4862 if (NLP_CHK_IACT_REQ(ndlp)) {
4863 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4864 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4865 "0275 lpfc_nlp_put: ndlp:x%p "
4866 "usgmap:x%x refcnt:%d\n",
4867 (void *)ndlp, ndlp->nlp_usg_map,
4868 atomic_read(&ndlp->kref.refcount));
4869 return 1;
4870 }
4871 /* For last put, mark the ndlp usage flags to make sure no
4872 * other kref_get and kref_put on the same ndlp shall get
4873 * in between the process when the final kref_put has been
4874 * invoked on this ndlp.
4875 */
4876 if (atomic_read(&ndlp->kref.refcount) == 1) {
4877 /* Indicate ndlp is put to inactive state. */
4878 NLP_SET_IACT_REQ(ndlp);
4879 /* Acknowledge ndlp memory free has been seen. */
4880 if (NLP_CHK_FREE_REQ(ndlp))
4881 NLP_SET_FREE_ACK(ndlp);
4882 }
4883 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4884 /* Note, the kref_put returns 1 when decrementing a reference
4885 * count that was 1, it invokes the release callback function,
4886 * but it still left the reference count as 1 (not actually
4887 * performs the last decrementation). Otherwise, it actually
4888 * decrements the reference count and returns 0.
4889 */
4890 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004891}
James Smart98c9ea52007-10-27 13:37:33 -04004892
4893/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004894 * by any other discovery thread. This routine returns 1 if the
4895 * ndlp has been freed. A return value of 0 indicates the ndlp is
4896 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004897 */
4898int
4899lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4900{
4901 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4902 "node not used: did:x%x flg:x%x refcnt:x%x",
4903 ndlp->nlp_DID, ndlp->nlp_flag,
4904 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004905 if (atomic_read(&ndlp->kref.refcount) == 1)
4906 if (lpfc_nlp_put(ndlp))
4907 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004908 return 0;
4909}
James Smart6fb120a2009-05-22 14:52:59 -04004910
4911/**
4912 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4913 * @phba: Pointer to hba context object.
4914 *
4915 * This function iterate through all FC nodes associated
4916 * will all vports to check if there is any node with
4917 * fc_rports associated with it. If there is an fc_rport
4918 * associated with the node, then the node is either in
4919 * discovered state or its devloss_timer is pending.
4920 */
4921static int
4922lpfc_fcf_inuse(struct lpfc_hba *phba)
4923{
4924 struct lpfc_vport **vports;
4925 int i, ret = 0;
4926 struct lpfc_nodelist *ndlp;
4927 struct Scsi_Host *shost;
4928
4929 vports = lpfc_create_vport_work_array(phba);
4930
4931 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4932 shost = lpfc_shost_from_vport(vports[i]);
4933 spin_lock_irq(shost->host_lock);
4934 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4935 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4936 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4937 ret = 1;
4938 spin_unlock_irq(shost->host_lock);
4939 goto out;
James Smart891478a2009-11-18 15:40:23 -05004940 } else {
4941 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4942 "2624 RPI %x DID %x flg %x still "
4943 "logged in\n",
4944 ndlp->nlp_rpi, ndlp->nlp_DID,
4945 ndlp->nlp_flag);
4946 if (ndlp->nlp_flag & NLP_RPI_VALID)
4947 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04004948 }
4949 }
4950 spin_unlock_irq(shost->host_lock);
4951 }
4952out:
4953 lpfc_destroy_vport_work_array(phba, vports);
4954 return ret;
4955}
4956
4957/**
4958 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4959 * @phba: Pointer to hba context object.
4960 * @mboxq: Pointer to mailbox object.
4961 *
4962 * This function frees memory associated with the mailbox command.
4963 */
4964static void
4965lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4966{
4967 struct lpfc_vport *vport = mboxq->vport;
4968
4969 if (mboxq->u.mb.mbxStatus) {
4970 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4971 "2555 UNREG_VFI mbxStatus error x%x "
4972 "HBA state x%x\n",
4973 mboxq->u.mb.mbxStatus, vport->port_state);
4974 }
4975 mempool_free(mboxq, phba->mbox_mem_pool);
4976 return;
4977}
4978
4979/**
4980 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4981 * @phba: Pointer to hba context object.
4982 * @mboxq: Pointer to mailbox object.
4983 *
4984 * This function frees memory associated with the mailbox command.
4985 */
4986static void
4987lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4988{
4989 struct lpfc_vport *vport = mboxq->vport;
4990
4991 if (mboxq->u.mb.mbxStatus) {
4992 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4993 "2550 UNREG_FCFI mbxStatus error x%x "
4994 "HBA state x%x\n",
4995 mboxq->u.mb.mbxStatus, vport->port_state);
4996 }
4997 mempool_free(mboxq, phba->mbox_mem_pool);
4998 return;
4999}
5000
5001/**
James Smartecfd03c2010-02-12 14:41:27 -05005002 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04005003 * @phba: Pointer to hba context object.
5004 *
James Smartecfd03c2010-02-12 14:41:27 -05005005 * This function prepare the HBA for unregistering the currently registered
5006 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5007 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04005008 */
James Smartecfd03c2010-02-12 14:41:27 -05005009int
5010lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04005011{
5012 LPFC_MBOXQ_t *mbox;
James Smart6fb120a2009-05-22 14:52:59 -04005013 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05005014 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05005015 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05005016 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04005017
James Smartecfd03c2010-02-12 14:41:27 -05005018 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04005019 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05005020 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005021
James Smart4d9ab992009-10-02 15:16:39 -04005022 /* At this point, all discovery is aborted */
5023 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04005024
5025 /* Unregister VPIs */
5026 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05005027 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04005028 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05005029 /* Stop FLOGI/FDISC retries */
5030 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5031 if (ndlp)
5032 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005033 lpfc_cleanup_pending_mbox(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04005034 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05005035 shost = lpfc_shost_from_vport(vports[i]);
5036 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05005037 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05005038 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005039 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005040 }
5041 lpfc_destroy_vport_work_array(phba, vports);
5042
James Smart695a8142010-01-26 23:08:03 -05005043 /* Cleanup any outstanding ELS commands */
5044 lpfc_els_flush_all_cmd(phba);
5045
James Smart6fb120a2009-05-22 14:52:59 -04005046 /* Unregister VFI */
5047 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5048 if (!mbox) {
5049 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005050 "2556 UNREG_VFI mbox allocation failed"
5051 "HBA state x%x\n", phba->pport->port_state);
5052 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005053 }
5054
James Smart6669f9b2009-10-02 15:16:45 -04005055 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04005056 mbox->vport = phba->pport;
5057 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
5058
5059 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5060 if (rc == MBX_NOT_FINISHED) {
5061 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005062 "2557 UNREG_VFI issue mbox failed rc x%x "
5063 "HBA state x%x\n",
5064 rc, phba->pport->port_state);
James Smart6fb120a2009-05-22 14:52:59 -04005065 mempool_free(mbox, phba->mbox_mem_pool);
James Smartecfd03c2010-02-12 14:41:27 -05005066 return -EIO;
James Smart6fb120a2009-05-22 14:52:59 -04005067 }
5068
James Smart72100cc2010-02-12 14:43:01 -05005069 shost = lpfc_shost_from_vport(phba->pport);
5070 spin_lock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005071 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005072 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005073
James Smartecfd03c2010-02-12 14:41:27 -05005074 return 0;
5075}
5076
5077/**
5078 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5079 * @phba: Pointer to hba context object.
5080 *
5081 * This function issues synchronous unregister FCF mailbox command to HBA to
5082 * unregister the currently registered FCF record. The driver does not reset
5083 * the driver FCF usage state flags.
5084 *
5085 * Return 0 if successfully issued, none-zero otherwise.
5086 */
5087int
5088lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5089{
5090 LPFC_MBOXQ_t *mbox;
5091 int rc;
5092
James Smart6fb120a2009-05-22 14:52:59 -04005093 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5094 if (!mbox) {
5095 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005096 "2551 UNREG_FCFI mbox allocation failed"
5097 "HBA state x%x\n", phba->pport->port_state);
5098 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005099 }
James Smart6fb120a2009-05-22 14:52:59 -04005100 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5101 mbox->vport = phba->pport;
5102 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5103 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5104
5105 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005106 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5107 "2552 Unregister FCFI command failed rc x%x "
5108 "HBA state x%x\n",
5109 rc, phba->pport->port_state);
5110 return -EINVAL;
5111 }
5112 return 0;
5113}
5114
5115/**
5116 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5117 * @phba: Pointer to hba context object.
5118 *
5119 * This function unregisters the currently reigstered FCF. This function
5120 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5121 */
5122void
5123lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5124{
5125 int rc;
5126
5127 /* Preparation for unregistering fcf */
5128 rc = lpfc_unregister_fcf_prep(phba);
5129 if (rc) {
5130 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5131 "2748 Failed to prepare for unregistering "
5132 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005133 return;
5134 }
5135
James Smartecfd03c2010-02-12 14:41:27 -05005136 /* Now, unregister FCF record and reset HBA FCF state */
5137 rc = lpfc_sli4_unregister_fcf(phba);
5138 if (rc)
5139 return;
5140 /* Reset HBA FCF states after successful unregister FCF */
5141 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005142 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005143
5144 /*
5145 * If driver is not unloading, check if there is any other
5146 * FCF record that can be used for discovery.
5147 */
5148 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005149 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005150 return;
5151
James Smart0c9ab6f2010-02-26 14:15:57 -05005152 /* This is considered as the initial FCF discovery scan */
5153 spin_lock_irq(&phba->hbalock);
5154 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5155 spin_unlock_irq(&phba->hbalock);
5156 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005157
James Smart0c9ab6f2010-02-26 14:15:57 -05005158 if (rc) {
5159 spin_lock_irq(&phba->hbalock);
5160 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5161 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005162 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005163 "2553 lpfc_unregister_unused_fcf failed "
5164 "to read FCF record HBA state x%x\n",
5165 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005166 }
James Smartecfd03c2010-02-12 14:41:27 -05005167}
5168
5169/**
5170 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5171 * @phba: Pointer to hba context object.
5172 *
5173 * This function just unregisters the currently reigstered FCF. It does not
5174 * try to find another FCF for discovery.
5175 */
5176void
5177lpfc_unregister_fcf(struct lpfc_hba *phba)
5178{
5179 int rc;
5180
5181 /* Preparation for unregistering fcf */
5182 rc = lpfc_unregister_fcf_prep(phba);
5183 if (rc) {
5184 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5185 "2749 Failed to prepare for unregistering "
5186 "HBA's FCF record: rc=%d\n", rc);
5187 return;
5188 }
5189
5190 /* Now, unregister FCF record and reset HBA FCF state */
5191 rc = lpfc_sli4_unregister_fcf(phba);
5192 if (rc)
5193 return;
5194 /* Set proper HBA FCF states after successful unregister FCF */
5195 spin_lock_irq(&phba->hbalock);
5196 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
5197 spin_unlock_irq(&phba->hbalock);
5198}
5199
5200/**
5201 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
5202 * @phba: Pointer to hba context object.
5203 *
5204 * This function check if there are any connected remote port for the FCF and
5205 * if all the devices are disconnected, this function unregister FCFI.
5206 * This function also tries to use another FCF for discovery.
5207 */
5208void
5209lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
5210{
5211 /*
5212 * If HBA is not running in FIP mode or if HBA does not support
5213 * FCoE or if FCF is not registered, do nothing.
5214 */
5215 spin_lock_irq(&phba->hbalock);
5216 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
5217 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
5218 !(phba->hba_flag & HBA_FIP_SUPPORT)) {
5219 spin_unlock_irq(&phba->hbalock);
5220 return;
5221 }
5222 spin_unlock_irq(&phba->hbalock);
5223
5224 if (lpfc_fcf_inuse(phba))
5225 return;
5226
5227 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005228}
5229
5230/**
5231 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
5232 * @phba: Pointer to hba context object.
5233 * @buff: Buffer containing the FCF connection table as in the config
5234 * region.
5235 * This function create driver data structure for the FCF connection
5236 * record table read from config region 23.
5237 */
5238static void
5239lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
5240 uint8_t *buff)
5241{
5242 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5243 struct lpfc_fcf_conn_hdr *conn_hdr;
5244 struct lpfc_fcf_conn_rec *conn_rec;
5245 uint32_t record_count;
5246 int i;
5247
5248 /* Free the current connect table */
5249 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005250 &phba->fcf_conn_rec_list, list) {
5251 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04005252 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005253 }
James Smart6fb120a2009-05-22 14:52:59 -04005254
5255 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
5256 record_count = conn_hdr->length * sizeof(uint32_t)/
5257 sizeof(struct lpfc_fcf_conn_rec);
5258
5259 conn_rec = (struct lpfc_fcf_conn_rec *)
5260 (buff + sizeof(struct lpfc_fcf_conn_hdr));
5261
5262 for (i = 0; i < record_count; i++) {
5263 if (!(conn_rec[i].flags & FCFCNCT_VALID))
5264 continue;
5265 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
5266 GFP_KERNEL);
5267 if (!conn_entry) {
5268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5269 "2566 Failed to allocate connection"
5270 " table entry\n");
5271 return;
5272 }
5273
5274 memcpy(&conn_entry->conn_rec, &conn_rec[i],
5275 sizeof(struct lpfc_fcf_conn_rec));
5276 conn_entry->conn_rec.vlan_tag =
5277 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
5278 conn_entry->conn_rec.flags =
5279 le16_to_cpu(conn_entry->conn_rec.flags);
5280 list_add_tail(&conn_entry->list,
5281 &phba->fcf_conn_rec_list);
5282 }
5283}
5284
5285/**
5286 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
5287 * @phba: Pointer to hba context object.
5288 * @buff: Buffer containing the FCoE parameter data structure.
5289 *
5290 * This function update driver data structure with config
5291 * parameters read from config region 23.
5292 */
5293static void
5294lpfc_read_fcoe_param(struct lpfc_hba *phba,
5295 uint8_t *buff)
5296{
5297 struct lpfc_fip_param_hdr *fcoe_param_hdr;
5298 struct lpfc_fcoe_params *fcoe_param;
5299
5300 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
5301 buff;
5302 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04005303 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04005304
5305 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
5306 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
5307 return;
5308
James Smart6fb120a2009-05-22 14:52:59 -04005309 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
5310 phba->valid_vlan = 1;
5311 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
5312 0xFFF;
5313 }
5314
5315 phba->fc_map[0] = fcoe_param->fc_map[0];
5316 phba->fc_map[1] = fcoe_param->fc_map[1];
5317 phba->fc_map[2] = fcoe_param->fc_map[2];
5318 return;
5319}
5320
5321/**
5322 * lpfc_get_rec_conf23 - Get a record type in config region data.
5323 * @buff: Buffer containing config region 23 data.
5324 * @size: Size of the data buffer.
5325 * @rec_type: Record type to be searched.
5326 *
5327 * This function searches config region data to find the begining
5328 * of the record specified by record_type. If record found, this
5329 * function return pointer to the record else return NULL.
5330 */
5331static uint8_t *
5332lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
5333{
5334 uint32_t offset = 0, rec_length;
5335
5336 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
5337 (size < sizeof(uint32_t)))
5338 return NULL;
5339
5340 rec_length = buff[offset + 1];
5341
5342 /*
5343 * One TLV record has one word header and number of data words
5344 * specified in the rec_length field of the record header.
5345 */
5346 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
5347 <= size) {
5348 if (buff[offset] == rec_type)
5349 return &buff[offset];
5350
5351 if (buff[offset] == LPFC_REGION23_LAST_REC)
5352 return NULL;
5353
5354 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
5355 rec_length = buff[offset + 1];
5356 }
5357 return NULL;
5358}
5359
5360/**
5361 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
5362 * @phba: Pointer to lpfc_hba data structure.
5363 * @buff: Buffer containing config region 23 data.
5364 * @size: Size of the data buffer.
5365 *
5366 * This fuction parse the FCoE config parameters in config region 23 and
5367 * populate driver data structure with the parameters.
5368 */
5369void
5370lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
5371 uint8_t *buff,
5372 uint32_t size)
5373{
5374 uint32_t offset = 0, rec_length;
5375 uint8_t *rec_ptr;
5376
5377 /*
5378 * If data size is less than 2 words signature and version cannot be
5379 * verified.
5380 */
5381 if (size < 2*sizeof(uint32_t))
5382 return;
5383
5384 /* Check the region signature first */
5385 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
5386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5387 "2567 Config region 23 has bad signature\n");
5388 return;
5389 }
5390
5391 offset += 4;
5392
5393 /* Check the data structure version */
5394 if (buff[offset] != LPFC_REGION23_VERSION) {
5395 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5396 "2568 Config region 23 has bad version\n");
5397 return;
5398 }
5399 offset += 4;
5400
5401 rec_length = buff[offset + 1];
5402
5403 /* Read FCoE param record */
5404 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5405 size - offset, FCOE_PARAM_TYPE);
5406 if (rec_ptr)
5407 lpfc_read_fcoe_param(phba, rec_ptr);
5408
5409 /* Read FCF connection table */
5410 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5411 size - offset, FCOE_CONN_TBL_TYPE);
5412 if (rec_ptr)
5413 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5414
5415}