blob: d1c5c52b1c251232699df345ce66b43679b34d79 [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 }
James Smart2a9bf3d2010-06-07 15:24:45 -0400590 if (phba->pport->work_port_events & WORKER_SERVICE_TXQ)
591 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500592 /*
593 * Turn on Ring interrupts
594 */
James Smart3772a992009-05-22 14:50:54 -0400595 if (phba->sli_rev <= LPFC_SLI_REV3) {
596 spin_lock_irq(&phba->hbalock);
597 control = readl(phba->HCregaddr);
598 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
599 lpfc_debugfs_slow_ring_trc(phba,
600 "WRK Enable ring: cntl:x%x hacopy:x%x",
601 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400602
James Smart3772a992009-05-22 14:50:54 -0400603 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
604 writel(control, phba->HCregaddr);
605 readl(phba->HCregaddr); /* flush */
606 } else {
607 lpfc_debugfs_slow_ring_trc(phba,
608 "WRK Ring ok: cntl:x%x hacopy:x%x",
609 control, ha_copy, 0);
610 }
611 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500612 }
613 }
James Smart2e0fef82007-06-17 19:56:36 -0500614 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500615}
616
dea31012005-04-17 16:05:31 -0500617int
618lpfc_do_work(void *p)
619{
620 struct lpfc_hba *phba = p;
621 int rc;
dea31012005-04-17 16:05:31 -0500622
623 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400624 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500625
James Smart3a55b532008-12-04 22:38:54 -0500626 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400627 /* wait and check worker queue activities */
628 rc = wait_event_interruptible(phba->work_waitq,
629 (test_and_clear_bit(LPFC_DATA_READY,
630 &phba->data_flags)
631 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500632 /* Signal wakeup shall terminate the worker thread */
633 if (rc) {
634 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
635 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500636 break;
James Smart3a55b532008-12-04 22:38:54 -0500637 }
dea31012005-04-17 16:05:31 -0500638
James Smart5e9d9b82008-06-14 22:52:53 -0400639 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500640 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500641 }
James Smart3a55b532008-12-04 22:38:54 -0500642 phba->worker_thread = NULL;
643 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
644 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500645 return 0;
646}
647
648/*
649 * This is only called to handle FC worker events. Since this a rare
650 * occurance, we allocate a struct lpfc_work_evt structure here instead of
651 * embedding it in the IOCB.
652 */
653int
James Smart2e0fef82007-06-17 19:56:36 -0500654lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500655 uint32_t evt)
656{
657 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500658 unsigned long flags;
dea31012005-04-17 16:05:31 -0500659
660 /*
661 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
662 * be queued to worker thread for processing
663 */
James Smart92d7f7b2007-06-17 19:56:38 -0500664 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500665 if (!evtp)
666 return 0;
667
668 evtp->evt_arg1 = arg1;
669 evtp->evt_arg2 = arg2;
670 evtp->evt = evt;
671
James Smarted957682007-06-17 19:56:37 -0500672 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400673 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500674 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500675
James Smart5e9d9b82008-06-14 22:52:53 -0400676 lpfc_worker_wake_up(phba);
677
dea31012005-04-17 16:05:31 -0500678 return 1;
679}
680
James Smart92d7f7b2007-06-17 19:56:38 -0500681void
682lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
683{
James Smart09372822008-01-11 01:52:54 -0500684 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500685 struct lpfc_hba *phba = vport->phba;
686 struct lpfc_nodelist *ndlp, *next_ndlp;
687 int rc;
688
689 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500690 if (!NLP_CHK_NODE_ACT(ndlp))
691 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500692 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
693 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400694 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
695 ((vport->port_type == LPFC_NPIV_PORT) &&
696 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500697 lpfc_unreg_rpi(vport, ndlp);
698
699 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400700 if ((phba->sli_rev < LPFC_SLI_REV4) &&
701 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500702 continue;
703 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
704 remove
705 ? NLP_EVT_DEVICE_RM
706 : NLP_EVT_DEVICE_RECOVERY);
707 }
708 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
709 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500710 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500711 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500712 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500713 }
714}
715
James Smart87af33f2007-10-27 13:37:43 -0400716void
James Smart98c9ea52007-10-27 13:37:33 -0400717lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500718{
James Smart695a8142010-01-26 23:08:03 -0500719 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
720
James Smart45ed1192009-10-02 15:17:02 -0400721 /* Cleanup any outstanding received buffers */
722 lpfc_cleanup_rcv_buffers(vport);
723
James Smart92d7f7b2007-06-17 19:56:38 -0500724 /* Cleanup any outstanding RSCN activity */
725 lpfc_els_flush_rscn(vport);
726
727 /* Cleanup any outstanding ELS commands */
728 lpfc_els_flush_cmd(vport);
729
730 lpfc_cleanup_rpis(vport, 0);
731
James Smart92d7f7b2007-06-17 19:56:38 -0500732 /* Turn off discovery timer if its running */
733 lpfc_can_disctmo(vport);
734}
735
James Smart3772a992009-05-22 14:50:54 -0400736void
James Smart98c9ea52007-10-27 13:37:33 -0400737lpfc_linkdown_port(struct lpfc_vport *vport)
738{
739 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
740
741 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
742
743 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
744 "Link Down: state:x%x rtry:x%x flg:x%x",
745 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
746
747 lpfc_port_link_failure(vport);
748
749}
750
dea31012005-04-17 16:05:31 -0500751int
James Smart685f0bf2007-04-25 09:53:08 -0400752lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500753{
James Smart2e0fef82007-06-17 19:56:36 -0500754 struct lpfc_vport *vport = phba->pport;
755 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400756 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400757 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400758 int i;
dea31012005-04-17 16:05:31 -0500759
James Smart3163f722008-02-08 18:50:25 -0500760 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500761 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500762
763 /* Block all SCSI stack I/Os */
764 lpfc_scsi_dev_block(phba);
765
James Smart2e0fef82007-06-17 19:56:36 -0500766 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500767 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500768 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500769 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500770 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500771 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500772 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500773 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500774 }
James Smart549e55c2007-08-02 11:09:51 -0400775 vports = lpfc_create_vport_work_array(phba);
776 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400777 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400778 /* Issue a LINK DOWN event to all nodes */
779 lpfc_linkdown_port(vports[i]);
780 }
James Smart09372822008-01-11 01:52:54 -0500781 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500782 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500783 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
784 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500785 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500786 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500787 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400788 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500789 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500790 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500791 }
792 }
793
dea31012005-04-17 16:05:31 -0500794 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500795 if (phba->pport->fc_flag & FC_PT2PT) {
796 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500797 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
798 if (mb) {
dea31012005-04-17 16:05:31 -0500799 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500800 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500801 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400802 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500803 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500804 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500805 }
806 }
James Smart2e0fef82007-06-17 19:56:36 -0500807 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500808 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500809 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500810 }
James Smart2e0fef82007-06-17 19:56:36 -0500811
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500812 return 0;
dea31012005-04-17 16:05:31 -0500813}
814
James Smart92d7f7b2007-06-17 19:56:38 -0500815static void
816lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500817{
James Smart92d7f7b2007-06-17 19:56:38 -0500818 struct lpfc_nodelist *ndlp;
819
820 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500821 if (!NLP_CHK_NODE_ACT(ndlp))
822 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500823 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
824 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500825 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500826 /* On Linkup its safe to clean up the ndlp
827 * from Fabric connections.
828 */
James Smart92d7f7b2007-06-17 19:56:38 -0500829 if (ndlp->nlp_DID != Fabric_DID)
830 lpfc_unreg_rpi(vport, ndlp);
831 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
832 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500833 /* Fail outstanding IO now since device is
834 * marked for PLOGI.
835 */
James Smart92d7f7b2007-06-17 19:56:38 -0500836 lpfc_unreg_rpi(vport, ndlp);
837 }
838 }
839}
840
841static void
842lpfc_linkup_port(struct lpfc_vport *vport)
843{
844 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500845 struct lpfc_hba *phba = vport->phba;
846
847 if ((vport->load_flag & FC_UNLOADING) != 0)
848 return;
849
James Smart858c9f62007-06-17 19:56:39 -0500850 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
851 "Link Up: top:x%x speed:x%x flg:x%x",
852 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
853
James Smart92d7f7b2007-06-17 19:56:38 -0500854 /* If NPIV is not enabled, only bring the physical port up */
855 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
856 (vport != phba->pport))
857 return;
dea31012005-04-17 16:05:31 -0500858
James Smart2e0fef82007-06-17 19:56:36 -0500859 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400860
James Smart2e0fef82007-06-17 19:56:36 -0500861 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500862 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
863 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
864 vport->fc_flag |= FC_NDISC_ACTIVE;
865 vport->fc_ns_retry = 0;
866 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500867
James Smart92d7f7b2007-06-17 19:56:38 -0500868 if (vport->fc_flag & FC_LBIT)
869 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500870
James Smart92d7f7b2007-06-17 19:56:38 -0500871}
872
873static int
874lpfc_linkup(struct lpfc_hba *phba)
875{
James Smart549e55c2007-08-02 11:09:51 -0400876 struct lpfc_vport **vports;
877 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500878
879 phba->link_state = LPFC_LINK_UP;
880
881 /* Unblock fabric iocbs if they are blocked */
882 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
883 del_timer_sync(&phba->fabric_block_timer);
884
James Smart549e55c2007-08-02 11:09:51 -0400885 vports = lpfc_create_vport_work_array(phba);
886 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400887 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400888 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500889 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400890 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
891 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500892 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500893
894 return 0;
895}
896
897/*
898 * This routine handles processing a CLEAR_LA mailbox
899 * command upon completion. It is setup in the LPFC_MBOXQ
900 * as the completion routine when the command is
901 * handed off to the SLI layer.
902 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100903static void
James Smart2e0fef82007-06-17 19:56:36 -0500904lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500905{
James Smart2e0fef82007-06-17 19:56:36 -0500906 struct lpfc_vport *vport = pmb->vport;
907 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
908 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400909 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500910 uint32_t control;
911
dea31012005-04-17 16:05:31 -0500912 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500913 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500914 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
915 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
916
917 /* Check for error */
918 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500919 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400920 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
921 "0320 CLEAR_LA mbxStatus error x%x hba "
922 "state x%x\n",
923 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500924 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500925 goto out;
926 }
927
James Smart92d7f7b2007-06-17 19:56:38 -0500928 if (vport->port_type == LPFC_PHYSICAL_PORT)
929 phba->link_state = LPFC_HBA_READY;
930
931 spin_lock_irq(&phba->hbalock);
932 psli->sli_flag |= LPFC_PROCESS_LA;
933 control = readl(phba->HCregaddr);
934 control |= HC_LAINT_ENA;
935 writel(control, phba->HCregaddr);
936 readl(phba->HCregaddr); /* flush */
937 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500938 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500939 return;
dea31012005-04-17 16:05:31 -0500940
dea31012005-04-17 16:05:31 -0500941out:
942 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400943 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
944 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500945 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500946
James Smart2e0fef82007-06-17 19:56:36 -0500947 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400948 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500949 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500950
James Smart2e0fef82007-06-17 19:56:36 -0500951 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500952
953 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500954
955 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500956 psli->sli_flag |= LPFC_PROCESS_LA;
957 control = readl(phba->HCregaddr);
958 control |= HC_LAINT_ENA;
959 writel(control, phba->HCregaddr);
960 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500961 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500962
963 return;
964}
965
James Smart2e0fef82007-06-17 19:56:36 -0500966
dea31012005-04-17 16:05:31 -0500967static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500968lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500969{
James Smart2e0fef82007-06-17 19:56:36 -0500970 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500971
James Smart04c68492009-05-22 14:52:52 -0400972 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500973 goto out;
dea31012005-04-17 16:05:31 -0500974
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500975 mempool_free(pmb, phba->mbox_mem_pool);
976
977 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500978 vport->fc_flag & FC_PUBLIC_LOOP &&
979 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500980 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500981 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500982 * LPFC_LOCAL_CFG_LINK while waiting for FAN
983 */
James Smart2e0fef82007-06-17 19:56:36 -0500984 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500985 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500986 }
dea31012005-04-17 16:05:31 -0500987
James Smart2e0fef82007-06-17 19:56:36 -0500988 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500989 * LPFC_FLOGI while waiting for FLOGI cmpl
990 */
James Smart92d7f7b2007-06-17 19:56:38 -0500991 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500992 lpfc_initial_flogi(vport);
993 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500994 return;
dea31012005-04-17 16:05:31 -0500995
996out:
James Smarte8b62012007-08-02 11:10:09 -0400997 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
998 "0306 CONFIG_LINK mbxStatus error x%x "
999 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001000 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001001 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001002
1003 lpfc_linkdown(phba);
1004
James Smarte8b62012007-08-02 11:10:09 -04001005 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1006 "0200 CONFIG_LINK bad hba state x%x\n",
1007 vport->port_state);
dea31012005-04-17 16:05:31 -05001008
James Smart92d7f7b2007-06-17 19:56:38 -05001009 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001010 return;
1011}
1012
1013static void
James Smart6fb120a2009-05-22 14:52:59 -04001014lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1015{
1016 struct lpfc_vport *vport = mboxq->vport;
1017 unsigned long flags;
1018
1019 if (mboxq->u.mb.mbxStatus) {
1020 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1021 "2017 REG_FCFI mbxStatus error x%x "
1022 "HBA state x%x\n",
1023 mboxq->u.mb.mbxStatus, vport->port_state);
1024 mempool_free(mboxq, phba->mbox_mem_pool);
1025 return;
1026 }
1027
1028 /* Start FCoE discovery by sending a FLOGI. */
1029 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1030 /* Set the FCFI registered flag */
1031 spin_lock_irqsave(&phba->hbalock, flags);
1032 phba->fcf.fcf_flag |= FCF_REGISTERED;
1033 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart32b97932009-07-19 10:01:21 -04001034 /* If there is a pending FCoE event, restart FCF table scan. */
1035 if (lpfc_check_pending_fcoe_event(phba, 1)) {
1036 mempool_free(mboxq, phba->mbox_mem_pool);
1037 return;
1038 }
James Smart1c6f4ef52009-11-18 15:40:49 -05001039 spin_lock_irqsave(&phba->hbalock, flags);
James Smartecfd03c2010-02-12 14:41:27 -05001040 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart4d9ab992009-10-02 15:16:39 -04001041 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001042 spin_unlock_irqrestore(&phba->hbalock, flags);
1043 if (vport->port_state != LPFC_FLOGI)
James Smart6fb120a2009-05-22 14:52:59 -04001044 lpfc_initial_flogi(vport);
James Smart6fb120a2009-05-22 14:52:59 -04001045
1046 mempool_free(mboxq, phba->mbox_mem_pool);
1047 return;
1048}
1049
1050/**
1051 * lpfc_fab_name_match - Check if the fcf fabric name match.
1052 * @fab_name: pointer to fabric name.
1053 * @new_fcf_record: pointer to fcf record.
1054 *
1055 * This routine compare the fcf record's fabric name with provided
1056 * fabric name. If the fabric name are identical this function
1057 * returns 1 else return 0.
1058 **/
1059static uint32_t
1060lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1061{
James Smartecfd03c2010-02-12 14:41:27 -05001062 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001063 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001064 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1065 return 0;
1066 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1067 return 0;
1068 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1069 return 0;
1070 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1071 return 0;
1072 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1073 return 0;
1074 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1075 return 0;
1076 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1077 return 0;
1078 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001079}
1080
1081/**
James Smart8fa38512009-07-19 10:01:03 -04001082 * lpfc_sw_name_match - Check if the fcf switch name match.
1083 * @fab_name: pointer to fabric name.
1084 * @new_fcf_record: pointer to fcf record.
1085 *
1086 * This routine compare the fcf record's switch name with provided
1087 * switch name. If the switch name are identical this function
1088 * returns 1 else return 0.
1089 **/
1090static uint32_t
1091lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1092{
James Smartecfd03c2010-02-12 14:41:27 -05001093 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001094 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001095 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1096 return 0;
1097 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1098 return 0;
1099 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1100 return 0;
1101 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1102 return 0;
1103 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1104 return 0;
1105 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1106 return 0;
1107 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1108 return 0;
1109 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001110}
1111
1112/**
James Smart6fb120a2009-05-22 14:52:59 -04001113 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001114 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001115 * @new_fcf_record: pointer to fcf record.
1116 *
1117 * This routine compare the fcf record's mac address with HBA's
1118 * FCF mac address. If the mac addresses are identical this function
1119 * returns 1 else return 0.
1120 **/
1121static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001122lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001123{
James Smartecfd03c2010-02-12 14:41:27 -05001124 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001125 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001126 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1127 return 0;
1128 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1129 return 0;
1130 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1131 return 0;
1132 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1133 return 0;
1134 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1135 return 0;
1136 return 1;
1137}
1138
1139static bool
1140lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1141{
1142 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001143}
1144
1145/**
1146 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001147 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001148 * @new_fcf_record: pointer to fcf record.
1149 *
1150 * This routine copies the FCF information from the FCF
1151 * record to lpfc_hba data structure.
1152 **/
1153static void
James Smartecfd03c2010-02-12 14:41:27 -05001154lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1155 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001156{
James Smartecfd03c2010-02-12 14:41:27 -05001157 /* Fabric name */
1158 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001159 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001160 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001161 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001162 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001163 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001164 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001165 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001166 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001167 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001168 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001169 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001170 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001171 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001172 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001173 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001174 /* Mac address */
1175 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1176 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1177 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1178 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1179 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1180 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1181 /* FCF record index */
1182 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1183 /* FCF record priority */
1184 fcf_rec->priority = new_fcf_record->fip_priority;
1185 /* Switch name */
1186 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001187 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001188 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001189 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001190 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001191 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001192 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001193 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001194 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001195 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001196 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001197 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001198 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001199 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001200 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001201 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001202}
1203
1204/**
James Smartecfd03c2010-02-12 14:41:27 -05001205 * lpfc_update_fcf_record - Update driver fcf record
1206 * @phba: pointer to lpfc hba data structure.
1207 * @fcf_rec: pointer to driver fcf record.
1208 * @new_fcf_record: pointer to hba fcf record.
1209 * @addr_mode: address mode to be set to the driver fcf record.
1210 * @vlan_id: vlan tag to be set to the driver fcf record.
1211 * @flag: flag bits to be set to the driver fcf record.
1212 *
1213 * This routine updates the driver FCF record from the new HBA FCF record
1214 * together with the address mode, vlan_id, and other informations. This
1215 * routine is called with the host lock held.
1216 **/
1217static void
1218__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1219 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1220 uint16_t vlan_id, uint32_t flag)
1221{
1222 /* Copy the fields from the HBA's FCF record */
1223 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1224 /* Update other fields of driver FCF record */
1225 fcf_rec->addr_mode = addr_mode;
1226 fcf_rec->vlan_id = vlan_id;
1227 fcf_rec->flag |= (flag | RECORD_VALID);
1228}
1229
1230/**
James Smart6fb120a2009-05-22 14:52:59 -04001231 * lpfc_register_fcf - Register the FCF with hba.
1232 * @phba: pointer to lpfc hba data structure.
1233 *
1234 * This routine issues a register fcfi mailbox command to register
1235 * the fcf with HBA.
1236 **/
1237static void
1238lpfc_register_fcf(struct lpfc_hba *phba)
1239{
1240 LPFC_MBOXQ_t *fcf_mbxq;
1241 int rc;
1242 unsigned long flags;
1243
1244 spin_lock_irqsave(&phba->hbalock, flags);
1245
1246 /* If the FCF is not availabe do nothing. */
1247 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smart4d9ab992009-10-02 15:16:39 -04001248 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001249 spin_unlock_irqrestore(&phba->hbalock, flags);
1250 return;
1251 }
1252
1253 /* The FCF is already registered, start discovery */
1254 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001255 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart32b97932009-07-19 10:01:21 -04001256 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001257 spin_unlock_irqrestore(&phba->hbalock, flags);
1258 if (phba->pport->port_state != LPFC_FLOGI)
1259 lpfc_initial_flogi(phba->pport);
1260 return;
1261 }
1262 spin_unlock_irqrestore(&phba->hbalock, flags);
1263
1264 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1265 GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001266 if (!fcf_mbxq) {
1267 spin_lock_irqsave(&phba->hbalock, flags);
1268 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1269 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001270 return;
James Smart4d9ab992009-10-02 15:16:39 -04001271 }
James Smart6fb120a2009-05-22 14:52:59 -04001272
1273 lpfc_reg_fcfi(phba, fcf_mbxq);
1274 fcf_mbxq->vport = phba->pport;
1275 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1276 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001277 if (rc == MBX_NOT_FINISHED) {
1278 spin_lock_irqsave(&phba->hbalock, flags);
1279 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1280 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001281 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001282 }
James Smart6fb120a2009-05-22 14:52:59 -04001283
1284 return;
1285}
1286
1287/**
1288 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1289 * @phba: pointer to lpfc hba data structure.
1290 * @new_fcf_record: pointer to fcf record.
1291 * @boot_flag: Indicates if this record used by boot bios.
1292 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001293 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001294 *
1295 * This routine compare the fcf record with connect list obtained from the
1296 * config region to decide if this FCF can be used for SAN discovery. It returns
1297 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1298 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1299 * is used by boot bios and addr_mode will indicate the addressing mode to be
1300 * used for this FCF when the function returns.
1301 * If the FCF record need to be used with a particular vlan id, the vlan is
1302 * set in the vlan_id on return of the function. If not VLAN tagging need to
1303 * be used with the FCF vlan_id will be set to 0xFFFF;
1304 **/
1305static int
1306lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1307 struct fcf_record *new_fcf_record,
1308 uint32_t *boot_flag, uint32_t *addr_mode,
1309 uint16_t *vlan_id)
1310{
1311 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001312 int i, j, fcf_vlan_id = 0;
1313
1314 /* Find the lowest VLAN id in the FCF record */
1315 for (i = 0; i < 512; i++) {
1316 if (new_fcf_record->vlan_bitmap[i]) {
1317 fcf_vlan_id = i * 8;
1318 j = 0;
1319 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1320 j++;
1321 fcf_vlan_id++;
1322 }
1323 break;
1324 }
1325 }
James Smart6fb120a2009-05-22 14:52:59 -04001326
James Smart0c287582009-06-10 17:22:56 -04001327 /* If FCF not available return 0 */
1328 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1329 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1330 return 0;
1331
James Smart45ed1192009-10-02 15:17:02 -04001332 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001333 *boot_flag = 0;
1334 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1335 new_fcf_record);
1336 if (phba->valid_vlan)
1337 *vlan_id = phba->vlan_id;
1338 else
1339 *vlan_id = 0xFFFF;
1340 return 1;
1341 }
1342
1343 /*
1344 * If there are no FCF connection table entry, driver connect to all
1345 * FCFs.
1346 */
1347 if (list_empty(&phba->fcf_conn_rec_list)) {
1348 *boot_flag = 0;
1349 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1350 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001351
1352 /*
1353 * When there are no FCF connect entries, use driver's default
1354 * addressing mode - FPMA.
1355 */
1356 if (*addr_mode & LPFC_FCF_FPMA)
1357 *addr_mode = LPFC_FCF_FPMA;
1358
James Smart4d9ab992009-10-02 15:16:39 -04001359 /* If FCF record report a vlan id use that vlan id */
1360 if (fcf_vlan_id)
1361 *vlan_id = fcf_vlan_id;
1362 else
1363 *vlan_id = 0xFFFF;
James Smart6fb120a2009-05-22 14:52:59 -04001364 return 1;
1365 }
1366
James Smartecfd03c2010-02-12 14:41:27 -05001367 list_for_each_entry(conn_entry,
1368 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001369 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1370 continue;
1371
1372 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1373 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001374 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001375 continue;
James Smart8fa38512009-07-19 10:01:03 -04001376 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1377 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1378 new_fcf_record))
1379 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001380 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1381 /*
1382 * If the vlan bit map does not have the bit set for the
1383 * vlan id to be used, then it is not a match.
1384 */
1385 if (!(new_fcf_record->vlan_bitmap
1386 [conn_entry->conn_rec.vlan_tag / 8] &
1387 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1388 continue;
1389 }
1390
1391 /*
James Smart0c287582009-06-10 17:22:56 -04001392 * If connection record does not support any addressing mode,
1393 * skip the FCF record.
1394 */
1395 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1396 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1397 continue;
1398
1399 /*
James Smart6fb120a2009-05-22 14:52:59 -04001400 * Check if the connection record specifies a required
1401 * addressing mode.
1402 */
1403 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1404 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1405
1406 /*
1407 * If SPMA required but FCF not support this continue.
1408 */
1409 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1410 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1411 new_fcf_record) & LPFC_FCF_SPMA))
1412 continue;
1413
1414 /*
1415 * If FPMA required but FCF not support this continue.
1416 */
1417 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1418 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1419 new_fcf_record) & LPFC_FCF_FPMA))
1420 continue;
1421 }
1422
1423 /*
1424 * This fcf record matches filtering criteria.
1425 */
1426 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1427 *boot_flag = 1;
1428 else
1429 *boot_flag = 0;
1430
James Smart0c287582009-06-10 17:22:56 -04001431 /*
1432 * If user did not specify any addressing mode, or if the
1433 * prefered addressing mode specified by user is not supported
1434 * by FCF, allow fabric to pick the addressing mode.
1435 */
James Smart6fb120a2009-05-22 14:52:59 -04001436 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1437 new_fcf_record);
1438 /*
1439 * If the user specified a required address mode, assign that
1440 * address mode
1441 */
1442 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1443 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1444 *addr_mode = (conn_entry->conn_rec.flags &
1445 FCFCNCT_AM_SPMA) ?
1446 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1447 /*
1448 * If the user specified a prefered address mode, use the
1449 * addr mode only if FCF support the addr_mode.
1450 */
1451 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1452 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1453 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1454 (*addr_mode & LPFC_FCF_SPMA))
1455 *addr_mode = LPFC_FCF_SPMA;
1456 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1457 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1458 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1459 (*addr_mode & LPFC_FCF_FPMA))
1460 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001461
James Smart4d9ab992009-10-02 15:16:39 -04001462 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001463 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1464 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001465 /*
1466 * If no vlan id is specified in connect list, use the vlan id
1467 * in the FCF record
1468 */
1469 else if (fcf_vlan_id)
1470 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001471 else
1472 *vlan_id = 0xFFFF;
1473
1474 return 1;
1475 }
1476
1477 return 0;
1478}
1479
1480/**
James Smart32b97932009-07-19 10:01:21 -04001481 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1482 * @phba: pointer to lpfc hba data structure.
1483 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1484 *
1485 * This function check if there is any fcoe event pending while driver
1486 * scan FCF entries. If there is any pending event, it will restart the
1487 * FCF saning and return 1 else return 0.
1488 */
1489int
1490lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1491{
James Smart32b97932009-07-19 10:01:21 -04001492 /*
1493 * If the Link is up and no FCoE events while in the
1494 * FCF discovery, no need to restart FCF discovery.
1495 */
1496 if ((phba->link_state >= LPFC_LINK_UP) &&
1497 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1498 return 0;
1499
James Smart0c9ab6f2010-02-26 14:15:57 -05001500 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1501 "2768 Pending link or FCF event during current "
1502 "handling of the previous event: link_state:x%x, "
1503 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1504 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1505 phba->fcoe_eventtag);
1506
James Smart32b97932009-07-19 10:01:21 -04001507 spin_lock_irq(&phba->hbalock);
1508 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1509 spin_unlock_irq(&phba->hbalock);
1510
James Smart0c9ab6f2010-02-26 14:15:57 -05001511 if (phba->link_state >= LPFC_LINK_UP) {
1512 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1513 "2780 Restart FCF table scan due to "
1514 "pending FCF event:evt_tag_at_scan:x%x, "
1515 "evt_tag_current:x%x\n",
1516 phba->fcoe_eventtag_at_fcf_scan,
1517 phba->fcoe_eventtag);
1518 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1519 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001520 /*
1521 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1522 * flag
1523 */
James Smart1c6f4ef52009-11-18 15:40:49 -05001524 spin_lock_irq(&phba->hbalock);
James Smart4d9ab992009-10-02 15:16:39 -04001525 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001526 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001527 spin_unlock_irq(&phba->hbalock);
1528 }
James Smart32b97932009-07-19 10:01:21 -04001529
James Smart0c9ab6f2010-02-26 14:15:57 -05001530 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001531 if (unreg_fcf) {
1532 spin_lock_irq(&phba->hbalock);
1533 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1534 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001535 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001536 }
James Smart32b97932009-07-19 10:01:21 -04001537 return 1;
1538}
1539
1540/**
James Smart999d8132010-03-15 11:24:56 -04001541 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1542 * @phba: pointer to lpfc hba data structure.
1543 * @fcf_cnt: number of eligible fcf record seen so far.
1544 *
1545 * This function makes an running random selection decision on FCF record to
1546 * use through a sequence of @fcf_cnt eligible FCF records with equal
1547 * probability. To perform integer manunipulation of random numbers with
1548 * size unit32_t, the lower 16 bits of the 32-bit random number returned
1549 * from random32() are taken as the random random number generated.
1550 *
1551 * Returns true when outcome is for the newly read FCF record should be
1552 * chosen; otherwise, return false when outcome is for keeping the previously
1553 * chosen FCF record.
1554 **/
1555static bool
1556lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1557{
1558 uint32_t rand_num;
1559
1560 /* Get 16-bit uniform random number */
1561 rand_num = (0xFFFF & random32());
1562
1563 /* Decision with probability 1/fcf_cnt */
1564 if ((fcf_cnt * rand_num) < 0xFFFF)
1565 return true;
1566 else
1567 return false;
1568}
1569
1570/**
1571 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
James Smart6fb120a2009-05-22 14:52:59 -04001572 * @phba: pointer to lpfc hba data structure.
1573 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001574 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001575 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001576 * This routine parses the non-embedded fcf mailbox command by performing the
1577 * necessarily error checking, non-embedded read FCF record mailbox command
1578 * SGE parsing, and endianness swapping.
1579 *
1580 * Returns the pointer to the new FCF record in the non-embedded mailbox
1581 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001582 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001583static struct fcf_record *
1584lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1585 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001586{
1587 void *virt_addr;
1588 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001589 struct lpfc_mbx_sge sge;
1590 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1591 uint32_t shdr_status, shdr_add_status;
1592 union lpfc_sli4_cfg_shdr *shdr;
1593 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001594
James Smart6fb120a2009-05-22 14:52:59 -04001595 /* Get the first SGE entry from the non-embedded DMA memory. This
1596 * routine only uses a single SGE.
1597 */
1598 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1599 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1600 if (unlikely(!mboxq->sge_array)) {
1601 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1602 "2524 Failed to get the non-embedded SGE "
1603 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001604 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001605 }
1606 virt_addr = mboxq->sge_array->addr[0];
1607
1608 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1609 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001610 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001611 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001612 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1613 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001614 "2726 READ_FCF_RECORD Indicates empty "
1615 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001616 else
1617 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001618 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001619 "with status x%x add_status x%x, "
1620 "mbx\n", shdr_status, shdr_add_status);
1621 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001622 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001623
1624 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001625 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1626 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1627 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001628 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001629 new_fcf_record = (struct fcf_record *)(virt_addr +
1630 sizeof(struct lpfc_mbx_read_fcf_tbl));
1631 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001632 offsetof(struct fcf_record, vlan_bitmap));
1633 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1634 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001635
James Smart0c9ab6f2010-02-26 14:15:57 -05001636 return new_fcf_record;
1637}
1638
1639/**
1640 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1641 * @phba: pointer to lpfc hba data structure.
1642 * @fcf_record: pointer to the fcf record.
1643 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1644 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1645 *
1646 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1647 * enabled.
1648 **/
1649static void
1650lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1651 struct fcf_record *fcf_record,
1652 uint16_t vlan_id,
1653 uint16_t next_fcf_index)
1654{
1655 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1656 "2764 READ_FCF_RECORD:\n"
1657 "\tFCF_Index : x%x\n"
1658 "\tFCF_Avail : x%x\n"
1659 "\tFCF_Valid : x%x\n"
1660 "\tFIP_Priority : x%x\n"
1661 "\tMAC_Provider : x%x\n"
1662 "\tLowest VLANID : x%x\n"
1663 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1664 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1665 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1666 "\tNext_FCF_Index: x%x\n",
1667 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1668 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1669 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1670 fcf_record->fip_priority,
1671 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1672 vlan_id,
1673 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1674 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1675 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1676 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1677 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1678 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1679 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1680 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1681 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1682 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1683 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1684 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1685 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1686 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1687 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1688 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1689 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1690 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1691 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1692 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1693 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1694 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1695 next_fcf_index);
1696}
1697
1698/**
1699 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
1700 * @phba: pointer to lpfc hba data structure.
1701 * @mboxq: pointer to mailbox object.
1702 *
1703 * This function iterates through all the fcf records available in
1704 * HBA and chooses the optimal FCF record for discovery. After finding
1705 * the FCF for discovery it registers the FCF record and kicks start
1706 * discovery.
1707 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
1708 * use an FCF record which matches fabric name and mac address of the
1709 * currently used FCF record.
1710 * If the driver supports only one FCF, it will try to use the FCF record
1711 * used by BOOT_BIOS.
1712 */
1713void
1714lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1715{
1716 struct fcf_record *new_fcf_record;
1717 uint32_t boot_flag, addr_mode;
1718 uint16_t fcf_index, next_fcf_index;
1719 struct lpfc_fcf_rec *fcf_rec = NULL;
1720 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04001721 uint32_t seed;
1722 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001723 int rc;
1724
1725 /* If there is pending FCoE event restart FCF table scan */
1726 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1727 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1728 return;
1729 }
1730
1731 /* Parse the FCF record from the non-embedded mailbox command */
1732 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
1733 &next_fcf_index);
1734 if (!new_fcf_record) {
1735 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1736 "2765 Mailbox command READ_FCF_RECORD "
1737 "failed to retrieve a FCF record.\n");
1738 /* Let next new FCF event trigger fast failover */
1739 spin_lock_irq(&phba->hbalock);
1740 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1741 spin_unlock_irq(&phba->hbalock);
1742 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1743 return;
1744 }
1745
1746 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05001747 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1748 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05001749
1750 /* Log the FCF record information if turned on */
1751 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
1752 next_fcf_index);
1753
James Smart6fb120a2009-05-22 14:52:59 -04001754 /*
1755 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05001756 * read the next entry; otherwise, this is an eligible FCF
1757 * record for round robin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04001758 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001759 if (!rc) {
1760 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1761 "2781 FCF record fcf_index:x%x failed FCF "
1762 "connection list check, fcf_avail:x%x, "
1763 "fcf_valid:x%x\n",
1764 bf_get(lpfc_fcf_record_fcf_index,
1765 new_fcf_record),
1766 bf_get(lpfc_fcf_record_fcf_avail,
1767 new_fcf_record),
1768 bf_get(lpfc_fcf_record_fcf_valid,
1769 new_fcf_record));
James Smart6fb120a2009-05-22 14:52:59 -04001770 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001771 } else {
1772 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1773 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
1774 if (rc)
1775 goto read_next_fcf;
1776 }
1777
James Smart6fb120a2009-05-22 14:52:59 -04001778 /*
1779 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05001780 * FCF record for the discovery. The condition that a rescan
1781 * matches the in-use FCF record: fabric name, switch name, mac
1782 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04001783 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001784 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001785 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartecfd03c2010-02-12 14:41:27 -05001786 if (lpfc_fab_name_match(phba->fcf.current_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001787 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001788 lpfc_sw_name_match(phba->fcf.current_rec.switch_name,
James Smart8fa38512009-07-19 10:01:03 -04001789 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001790 lpfc_mac_addr_match(phba->fcf.current_rec.mac_addr,
1791 new_fcf_record) &&
1792 lpfc_vlan_id_match(phba->fcf.current_rec.vlan_id,
1793 vlan_id)) {
James Smart6fb120a2009-05-22 14:52:59 -04001794 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smartecfd03c2010-02-12 14:41:27 -05001795 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
1796 /* Stop FCF redisc wait timer if pending */
1797 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
1798 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1799 /* If in fast failover, mark it's completed */
James Smartfc2b9892010-02-26 14:15:29 -05001800 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV |
James Smart0c9ab6f2010-02-26 14:15:57 -05001801 FCF_DISCOVERY);
1802 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001803 goto out;
1804 }
James Smartecfd03c2010-02-12 14:41:27 -05001805 /*
1806 * Read next FCF record from HBA searching for the matching
1807 * with in-use record only if not during the fast failover
1808 * period. In case of fast failover period, it shall try to
1809 * determine whether the FCF record just read should be the
1810 * next candidate.
1811 */
1812 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001813 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001814 goto read_next_fcf;
1815 }
James Smart6fb120a2009-05-22 14:52:59 -04001816 }
James Smartecfd03c2010-02-12 14:41:27 -05001817 /*
1818 * Update on failover FCF record only if it's in FCF fast-failover
1819 * period; otherwise, update on current FCF record.
1820 */
James Smartfc2b9892010-02-26 14:15:29 -05001821 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1822 fcf_rec = &phba->fcf.failover_rec;
1823 else
James Smartecfd03c2010-02-12 14:41:27 -05001824 fcf_rec = &phba->fcf.current_rec;
1825
James Smart6fb120a2009-05-22 14:52:59 -04001826 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1827 /*
James Smartecfd03c2010-02-12 14:41:27 -05001828 * If the driver FCF record does not have boot flag
1829 * set and new hba fcf record has boot flag set, use
1830 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001831 */
James Smartecfd03c2010-02-12 14:41:27 -05001832 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
1833 /* Choose this FCF record */
1834 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1835 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05001836 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001837 goto read_next_fcf;
1838 }
1839 /*
James Smartecfd03c2010-02-12 14:41:27 -05001840 * If the driver FCF record has boot flag set and the
1841 * new hba FCF record does not have boot flag, read
1842 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04001843 */
James Smartecfd03c2010-02-12 14:41:27 -05001844 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001845 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001846 goto read_next_fcf;
1847 }
1848 /*
James Smartecfd03c2010-02-12 14:41:27 -05001849 * If the new hba FCF record has lower priority value
1850 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04001851 */
James Smartfc2b9892010-02-26 14:15:29 -05001852 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04001853 /* Choose the new FCF record with lower priority */
James Smartecfd03c2010-02-12 14:41:27 -05001854 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1855 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04001856 /* Reset running random FCF selection count */
1857 phba->fcf.eligible_fcf_cnt = 1;
1858 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
1859 /* Update running random FCF selection count */
1860 phba->fcf.eligible_fcf_cnt++;
1861 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
1862 phba->fcf.eligible_fcf_cnt);
1863 if (select_new_fcf)
1864 /* Choose the new FCF by random selection */
1865 __lpfc_update_fcf_record(phba, fcf_rec,
1866 new_fcf_record,
1867 addr_mode, vlan_id, 0);
James Smart6fb120a2009-05-22 14:52:59 -04001868 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001869 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001870 goto read_next_fcf;
1871 }
1872 /*
James Smartecfd03c2010-02-12 14:41:27 -05001873 * This is the first suitable FCF record, choose this record for
1874 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001875 */
James Smartecfd03c2010-02-12 14:41:27 -05001876 if (fcf_rec) {
1877 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1878 addr_mode, vlan_id, (boot_flag ?
1879 BOOT_ENABLE : 0));
1880 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04001881 /* Setup initial running random FCF selection count */
1882 phba->fcf.eligible_fcf_cnt = 1;
1883 /* Seeding the random number generator for random selection */
1884 seed = (uint32_t)(0xFFFFFFFF & jiffies);
1885 srandom32(seed);
James Smart6fb120a2009-05-22 14:52:59 -04001886 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001887 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001888 goto read_next_fcf;
1889
1890read_next_fcf:
1891 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05001892 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
1893 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
1894 /*
1895 * Case of FCF fast failover scan
1896 */
1897
1898 /*
1899 * It has not found any suitable FCF record, cancel
1900 * FCF scan inprogress, and do nothing
1901 */
1902 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001903 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1904 "2782 No suitable FCF record "
1905 "found during this round of "
1906 "post FCF rediscovery scan: "
1907 "fcf_evt_tag:x%x, fcf_index: "
1908 "x%x\n",
1909 phba->fcoe_eventtag_at_fcf_scan,
1910 bf_get(lpfc_fcf_record_fcf_index,
1911 new_fcf_record));
1912 /*
1913 * Let next new FCF event trigger fast
1914 * failover
1915 */
1916 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001917 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001918 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001919 return;
1920 }
1921 /*
1922 * It has found a suitable FCF record that is not
1923 * the same as in-use FCF record, unregister the
1924 * in-use FCF record, replace the in-use FCF record
1925 * with the new FCF record, mark FCF fast failover
1926 * completed, and then start register the new FCF
1927 * record.
1928 */
1929
James Smart0c9ab6f2010-02-26 14:15:57 -05001930 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001931 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05001932
1933 /* Replace in-use record with the new record */
James Smartecfd03c2010-02-12 14:41:27 -05001934 memcpy(&phba->fcf.current_rec,
1935 &phba->fcf.failover_rec,
1936 sizeof(struct lpfc_fcf_rec));
1937 /* mark the FCF fast failover completed */
James Smart0c9ab6f2010-02-26 14:15:57 -05001938 spin_lock_irq(&phba->hbalock);
1939 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
1940 spin_unlock_irq(&phba->hbalock);
1941 /*
1942 * Set up the initial registered FCF index for FLOGI
1943 * round robin FCF failover.
1944 */
1945 phba->fcf.fcf_rr_init_indx =
1946 phba->fcf.failover_rec.fcf_indx;
James Smartecfd03c2010-02-12 14:41:27 -05001947 /* Register to the new FCF record */
1948 lpfc_register_fcf(phba);
1949 } else {
1950 /*
1951 * In case of transaction period to fast FCF failover,
1952 * do nothing when search to the end of the FCF table.
1953 */
1954 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
1955 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
1956 return;
1957 /*
1958 * Otherwise, initial scan or post linkdown rescan,
James Smart0c9ab6f2010-02-26 14:15:57 -05001959 * register with the best FCF record found so far
1960 * through the FCF scanning process.
James Smartecfd03c2010-02-12 14:41:27 -05001961 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001962
1963 /* mark the initial FCF discovery completed */
1964 spin_lock_irq(&phba->hbalock);
1965 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
1966 spin_unlock_irq(&phba->hbalock);
1967 /*
1968 * Set up the initial registered FCF index for FLOGI
1969 * round robin FCF failover
1970 */
1971 phba->fcf.fcf_rr_init_indx =
1972 phba->fcf.current_rec.fcf_indx;
1973 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001974 lpfc_register_fcf(phba);
1975 }
1976 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05001977 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04001978 return;
1979
1980out:
1981 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1982 lpfc_register_fcf(phba);
1983
1984 return;
1985}
1986
1987/**
James Smart0c9ab6f2010-02-26 14:15:57 -05001988 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf round robin read_fcf mbox cmpl hdler
1989 * @phba: pointer to lpfc hba data structure.
1990 * @mboxq: pointer to mailbox object.
1991 *
1992 * This is the callback function for FLOGI failure round robin FCF failover
1993 * read FCF record mailbox command from the eligible FCF record bmask for
1994 * performing the failover. If the FCF read back is not valid/available, it
1995 * fails through to retrying FLOGI to the currently registered FCF again.
1996 * Otherwise, if the FCF read back is valid and available, it will set the
1997 * newly read FCF record to the failover FCF record, unregister currently
1998 * registered FCF record, copy the failover FCF record to the current
1999 * FCF record, and then register the current FCF record before proceeding
2000 * to trying FLOGI on the new failover FCF.
2001 */
2002void
2003lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2004{
2005 struct fcf_record *new_fcf_record;
2006 uint32_t boot_flag, addr_mode;
2007 uint16_t next_fcf_index;
2008 uint16_t current_fcf_index;
2009 uint16_t vlan_id;
2010
2011 /* If link state is not up, stop the round robin failover process */
2012 if (phba->link_state < LPFC_LINK_UP) {
2013 spin_lock_irq(&phba->hbalock);
2014 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
2015 spin_unlock_irq(&phba->hbalock);
2016 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2017 return;
2018 }
2019
2020 /* Parse the FCF record from the non-embedded mailbox command */
2021 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2022 &next_fcf_index);
2023 if (!new_fcf_record) {
2024 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2025 "2766 Mailbox command READ_FCF_RECORD "
2026 "failed to retrieve a FCF record.\n");
2027 goto out;
2028 }
2029
2030 /* Get the needed parameters from FCF record */
2031 lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2032 &addr_mode, &vlan_id);
2033
2034 /* Log the FCF record information if turned on */
2035 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2036 next_fcf_index);
2037
2038 /* Upload new FCF record to the failover FCF record */
2039 spin_lock_irq(&phba->hbalock);
2040 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2041 new_fcf_record, addr_mode, vlan_id,
2042 (boot_flag ? BOOT_ENABLE : 0));
2043 spin_unlock_irq(&phba->hbalock);
2044
2045 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2046
2047 /* Unregister the current in-use FCF record */
2048 lpfc_unregister_fcf(phba);
2049
2050 /* Replace in-use record with the new record */
2051 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2052 sizeof(struct lpfc_fcf_rec));
2053
2054 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2055 "2783 FLOGI round robin FCF failover from FCF "
2056 "(index:x%x) to FCF (index:x%x).\n",
2057 current_fcf_index,
2058 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
2059
2060out:
2061 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2062 lpfc_register_fcf(phba);
2063}
2064
2065/**
2066 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2067 * @phba: pointer to lpfc hba data structure.
2068 * @mboxq: pointer to mailbox object.
2069 *
2070 * This is the callback function of read FCF record mailbox command for
2071 * updating the eligible FCF bmask for FLOGI failure round robin FCF
2072 * failover when a new FCF event happened. If the FCF read back is
2073 * valid/available and it passes the connection list check, it updates
2074 * the bmask for the eligible FCF record for round robin failover.
2075 */
2076void
2077lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2078{
2079 struct fcf_record *new_fcf_record;
2080 uint32_t boot_flag, addr_mode;
2081 uint16_t fcf_index, next_fcf_index;
2082 uint16_t vlan_id;
2083 int rc;
2084
2085 /* If link state is not up, no need to proceed */
2086 if (phba->link_state < LPFC_LINK_UP)
2087 goto out;
2088
2089 /* If FCF discovery period is over, no need to proceed */
2090 if (phba->fcf.fcf_flag & FCF_DISCOVERY)
2091 goto out;
2092
2093 /* Parse the FCF record from the non-embedded mailbox command */
2094 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2095 &next_fcf_index);
2096 if (!new_fcf_record) {
2097 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2098 "2767 Mailbox command READ_FCF_RECORD "
2099 "failed to retrieve a FCF record.\n");
2100 goto out;
2101 }
2102
2103 /* Check the connection list for eligibility */
2104 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2105 &addr_mode, &vlan_id);
2106
2107 /* Log the FCF record information if turned on */
2108 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2109 next_fcf_index);
2110
2111 if (!rc)
2112 goto out;
2113
2114 /* Update the eligible FCF record index bmask */
2115 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2116 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
2117
2118out:
2119 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2120}
2121
2122/**
James Smart1c6834a2009-07-19 10:01:26 -04002123 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2124 * @phba: pointer to lpfc hba data structure.
2125 * @mboxq: pointer to mailbox data structure.
2126 *
2127 * This function handles completion of init vpi mailbox command.
2128 */
James Smart695a8142010-01-26 23:08:03 -05002129void
James Smart1c6834a2009-07-19 10:01:26 -04002130lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2131{
2132 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002133 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002134 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2135
James Smart1c6834a2009-07-19 10:01:26 -04002136 if (mboxq->u.mb.mbxStatus) {
2137 lpfc_printf_vlog(vport, KERN_ERR,
2138 LOG_MBOX,
2139 "2609 Init VPI mailbox failed 0x%x\n",
2140 mboxq->u.mb.mbxStatus);
2141 mempool_free(mboxq, phba->mbox_mem_pool);
2142 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2143 return;
2144 }
James Smart72100cc2010-02-12 14:43:01 -05002145 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002146 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002147 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002148
James Smart695a8142010-01-26 23:08:03 -05002149 /* If this port is physical port or FDISC is done, do reg_vpi */
2150 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2151 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2152 if (!ndlp)
2153 lpfc_printf_vlog(vport, KERN_ERR,
2154 LOG_DISCOVERY,
2155 "2731 Cannot find fabric "
2156 "controller node\n");
2157 else
2158 lpfc_register_new_vport(phba, vport, ndlp);
2159 mempool_free(mboxq, phba->mbox_mem_pool);
2160 return;
2161 }
2162
James Smart1c6834a2009-07-19 10:01:26 -04002163 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2164 lpfc_initial_fdisc(vport);
2165 else {
2166 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002167 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2168 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002169 }
James Smart695a8142010-01-26 23:08:03 -05002170 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002171 return;
2172}
2173
2174/**
James Smartecfd03c2010-02-12 14:41:27 -05002175 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2176 * @vport: pointer to lpfc_vport data structure.
2177 *
2178 * This function issue a init_vpi mailbox command to initialize
2179 * VPI for the vport.
2180 */
2181void
2182lpfc_issue_init_vpi(struct lpfc_vport *vport)
2183{
2184 LPFC_MBOXQ_t *mboxq;
2185 int rc;
2186
2187 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2188 if (!mboxq) {
2189 lpfc_printf_vlog(vport, KERN_ERR,
2190 LOG_MBOX, "2607 Failed to allocate "
2191 "init_vpi mailbox\n");
2192 return;
2193 }
2194 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2195 mboxq->vport = vport;
2196 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2197 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2198 if (rc == MBX_NOT_FINISHED) {
2199 lpfc_printf_vlog(vport, KERN_ERR,
2200 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2201 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2202 }
2203}
2204
2205/**
James Smart6fb120a2009-05-22 14:52:59 -04002206 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2207 * @phba: pointer to lpfc hba data structure.
2208 *
2209 * This function loops through the list of vports on the @phba and issues an
2210 * FDISC if possible.
2211 */
2212void
2213lpfc_start_fdiscs(struct lpfc_hba *phba)
2214{
2215 struct lpfc_vport **vports;
2216 int i;
2217
2218 vports = lpfc_create_vport_work_array(phba);
2219 if (vports != NULL) {
2220 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2221 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2222 continue;
2223 /* There are no vpi for this vport */
2224 if (vports[i]->vpi > phba->max_vpi) {
2225 lpfc_vport_set_state(vports[i],
2226 FC_VPORT_FAILED);
2227 continue;
2228 }
2229 if (phba->fc_topology == TOPOLOGY_LOOP) {
2230 lpfc_vport_set_state(vports[i],
2231 FC_VPORT_LINKDOWN);
2232 continue;
2233 }
James Smart1c6834a2009-07-19 10:01:26 -04002234 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002235 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002236 continue;
2237 }
James Smart6fb120a2009-05-22 14:52:59 -04002238 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2239 lpfc_initial_fdisc(vports[i]);
2240 else {
2241 lpfc_vport_set_state(vports[i],
2242 FC_VPORT_NO_FABRIC_SUPP);
2243 lpfc_printf_vlog(vports[i], KERN_ERR,
2244 LOG_ELS,
2245 "0259 No NPIV "
2246 "Fabric support\n");
2247 }
2248 }
2249 }
2250 lpfc_destroy_vport_work_array(phba, vports);
2251}
2252
2253void
2254lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2255{
2256 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2257 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002258 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002259
2260 if (mboxq->u.mb.mbxStatus) {
2261 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2262 "2018 REG_VFI mbxStatus error x%x "
2263 "HBA state x%x\n",
2264 mboxq->u.mb.mbxStatus, vport->port_state);
2265 if (phba->fc_topology == TOPOLOGY_LOOP) {
2266 /* FLOGI failed, use loop map to make discovery list */
2267 lpfc_disc_list_loopmap(vport);
2268 /* Start discovery */
2269 lpfc_disc_start(vport);
2270 goto fail_free_mem;
2271 }
2272 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2273 goto fail_free_mem;
2274 }
James Smartc8685952009-11-18 15:39:16 -05002275 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002276 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002277 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002278 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002279 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002280 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002281
2282 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2283 lpfc_start_fdiscs(phba);
2284 lpfc_do_scr_ns_plogi(phba, vport);
2285 }
2286
2287fail_free_mem:
2288 mempool_free(mboxq, phba->mbox_mem_pool);
2289 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2290 kfree(dmabuf);
2291 return;
2292}
2293
2294static void
James Smart2e0fef82007-06-17 19:56:36 -05002295lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002296{
James Smart6fb120a2009-05-22 14:52:59 -04002297 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002298 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002299 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002300
2301
2302 /* Check for error */
2303 if (mb->mbxStatus) {
2304 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002305 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2306 "0319 READ_SPARAM mbxStatus error x%x "
2307 "hba state x%x>\n",
2308 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002309 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002310 goto out;
2311 }
2312
James Smart2e0fef82007-06-17 19:56:36 -05002313 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002314 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05002315 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05002316 u64_to_wwn(phba->cfg_soft_wwnn,
2317 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04002318 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05002319 u64_to_wwn(phba->cfg_soft_wwpn,
2320 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05002321 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
2322 sizeof(vport->fc_nodename));
2323 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
2324 sizeof(vport->fc_portname));
2325 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2326 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2327 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2328 }
2329
dea31012005-04-17 16:05:31 -05002330 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2331 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002332 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002333 return;
2334
2335out:
2336 pmb->context1 = NULL;
2337 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2338 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002339 lpfc_issue_clear_la(phba, vport);
2340 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002341 return;
2342}
2343
2344static void
James Smart92d7f7b2007-06-17 19:56:38 -05002345lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05002346{
James Smart92d7f7b2007-06-17 19:56:38 -05002347 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002348 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002349 int i;
James Smart14691152006-12-02 13:34:28 -05002350 struct lpfc_dmabuf *mp;
2351 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002352 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002353
James Smart92d7f7b2007-06-17 19:56:38 -05002354 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002355 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05002356 case LA_1GHZ_LINK:
2357 phba->fc_linkspeed = LA_1GHZ_LINK;
2358 break;
2359 case LA_2GHZ_LINK:
2360 phba->fc_linkspeed = LA_2GHZ_LINK;
2361 break;
2362 case LA_4GHZ_LINK:
2363 phba->fc_linkspeed = LA_4GHZ_LINK;
2364 break;
2365 case LA_8GHZ_LINK:
2366 phba->fc_linkspeed = LA_8GHZ_LINK;
2367 break;
James Smartf4b4c682009-05-22 14:53:12 -04002368 case LA_10GHZ_LINK:
2369 phba->fc_linkspeed = LA_10GHZ_LINK;
2370 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002371 default:
2372 phba->fc_linkspeed = LA_UNKNW_LINK;
2373 break;
dea31012005-04-17 16:05:31 -05002374 }
2375
2376 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05002377 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05002378
2379 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05002380 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05002381
James Smart6a9c52c2009-10-02 15:16:51 -04002382 /* if npiv is enabled and this adapter supports npiv log
2383 * a message that npiv is not supported in this topology
2384 */
2385 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04002386 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2387 "1309 Link Up Event npiv not supported in loop "
2388 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002389 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05002390 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05002391 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002392
James Smart2e0fef82007-06-17 19:56:36 -05002393 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05002394 i = la->un.lilpBde64.tus.f.bdeSize;
2395
2396 if (i == 0) {
2397 phba->alpa_map[0] = 0;
2398 } else {
James Smarte8b62012007-08-02 11:10:09 -04002399 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05002400 int numalpa, j, k;
2401 union {
2402 uint8_t pamap[16];
2403 struct {
2404 uint32_t wd1;
2405 uint32_t wd2;
2406 uint32_t wd3;
2407 uint32_t wd4;
2408 } pa;
2409 } un;
2410 numalpa = phba->alpa_map[0];
2411 j = 0;
2412 while (j < numalpa) {
2413 memset(un.pamap, 0, 16);
2414 for (k = 1; j < numalpa; k++) {
2415 un.pamap[k - 1] =
2416 phba->alpa_map[j + 1];
2417 j++;
2418 if (k == 16)
2419 break;
2420 }
2421 /* Link Up Event ALPA map */
2422 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05002423 KERN_WARNING,
2424 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002425 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05002426 "ALPA map Data: x%x "
2427 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002428 un.pa.wd1, un.pa.wd2,
2429 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05002430 }
2431 }
2432 }
2433 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05002434 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04002435 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05002436 (phba->sli_rev == 3))
2437 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
2438 }
James Smart2e0fef82007-06-17 19:56:36 -05002439 vport->fc_myDID = phba->fc_pref_DID;
2440 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002441 }
James Smart92d7f7b2007-06-17 19:56:38 -05002442 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002443
2444 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05002445 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2446 if (!sparam_mbox)
2447 goto out;
2448
2449 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
2450 if (rc) {
2451 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2452 goto out;
2453 }
2454 sparam_mbox->vport = vport;
2455 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
2456 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
2457 if (rc == MBX_NOT_FINISHED) {
2458 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2459 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2460 kfree(mp);
2461 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2462 goto out;
dea31012005-04-17 16:05:31 -05002463 }
2464
James Smart6fb120a2009-05-22 14:52:59 -04002465 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2466 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2467 if (!cfglink_mbox)
2468 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05002469 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002470 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002471 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002472 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002473 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002474 if (rc == MBX_NOT_FINISHED) {
2475 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2476 goto out;
2477 }
2478 } else {
James Smart32b97932009-07-19 10:01:21 -04002479 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002480 /*
2481 * Add the driver's default FCF record at FCF index 0 now. This
2482 * is phase 1 implementation that support FCF index 0 and driver
2483 * defaults.
2484 */
James Smart45ed1192009-10-02 15:17:02 -04002485 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002486 fcf_record = kzalloc(sizeof(struct fcf_record),
2487 GFP_KERNEL);
2488 if (unlikely(!fcf_record)) {
2489 lpfc_printf_log(phba, KERN_ERR,
2490 LOG_MBOX | LOG_SLI,
2491 "2554 Could not allocate memmory for "
2492 "fcf record\n");
2493 rc = -ENODEV;
2494 goto out;
2495 }
2496
2497 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2498 LPFC_FCOE_FCF_DEF_INDEX);
2499 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2500 if (unlikely(rc)) {
2501 lpfc_printf_log(phba, KERN_ERR,
2502 LOG_MBOX | LOG_SLI,
2503 "2013 Could not manually add FCF "
2504 "record 0, status %d\n", rc);
2505 rc = -ENODEV;
2506 kfree(fcf_record);
2507 goto out;
2508 }
2509 kfree(fcf_record);
2510 }
2511 /*
2512 * The driver is expected to do FIP/FCF. Call the port
2513 * and get the FCF Table.
2514 */
James Smart32b97932009-07-19 10:01:21 -04002515 spin_lock_irq(&phba->hbalock);
2516 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2517 spin_unlock_irq(&phba->hbalock);
2518 return;
2519 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002520 /* This is the initial FCF discovery scan */
2521 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04002522 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002523 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2524 "2778 Start FCF table scan at linkup\n");
2525
2526 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2527 LPFC_FCOE_FCF_GET_FIRST);
2528 if (rc) {
2529 spin_lock_irq(&phba->hbalock);
2530 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
2531 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002532 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002533 }
dea31012005-04-17 16:05:31 -05002534 }
James Smart6fb120a2009-05-22 14:52:59 -04002535
2536 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002537out:
2538 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002539 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2540 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2541 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002542 lpfc_issue_clear_la(phba, vport);
2543 return;
dea31012005-04-17 16:05:31 -05002544}
2545
2546static void
James Smart84774a42008-08-24 21:50:06 -04002547lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002548{
dea31012005-04-17 16:05:31 -05002549 uint32_t control;
2550 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002551 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002552 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002553 if (phba->sli_rev <= LPFC_SLI_REV3) {
2554 control = readl(phba->HCregaddr);
2555 control |= HC_LAINT_ENA;
2556 writel(control, phba->HCregaddr);
2557 readl(phba->HCregaddr); /* flush */
2558 }
James Smart2e0fef82007-06-17 19:56:36 -05002559 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002560}
2561
James Smart84774a42008-08-24 21:50:06 -04002562static void
2563lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2564{
2565 lpfc_linkdown(phba);
2566 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002567 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002568 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2569}
2570
2571
dea31012005-04-17 16:05:31 -05002572/*
2573 * This routine handles processing a READ_LA mailbox
2574 * command upon completion. It is setup in the LPFC_MBOXQ
2575 * as the completion routine when the command is
2576 * handed off to the SLI layer.
2577 */
2578void
James Smart2e0fef82007-06-17 19:56:36 -05002579lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002580{
James Smart2e0fef82007-06-17 19:56:36 -05002581 struct lpfc_vport *vport = pmb->vport;
2582 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002583 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002584 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002585 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2586
James Smart0d2b6b82008-06-14 22:52:47 -04002587 /* Unblock ELS traffic */
2588 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002589 /* Check for error */
2590 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002591 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002592 "1307 READ_LA mbox error x%x state x%x\n",
2593 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002594 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002595 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002596 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2597 }
2598
James Smart04c68492009-05-22 14:52:52 -04002599 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002600
2601 memcpy(&phba->alpa_map[0], mp->virt, 128);
2602
James Smart2e0fef82007-06-17 19:56:36 -05002603 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002604 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002605 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002606 else
James Smart2e0fef82007-06-17 19:56:36 -05002607 vport->fc_flag &= ~FC_BYPASSED_MODE;
2608 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002609
James Smart0c287582009-06-10 17:22:56 -04002610 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002611 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002612 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002613 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002614 if (phba->fc_eventTag != 0)
2615 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002616 }
dea31012005-04-17 16:05:31 -05002617
2618 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05002619 spin_lock_irq(&phba->hbalock);
James Smart84774a42008-08-24 21:50:06 -04002620 if (la->mm)
2621 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2622 else
2623 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05002624 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002625
James Smart4d9ab992009-10-02 15:16:39 -04002626 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002627 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002628 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002629 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002630 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002631 "1306 Link Up Event in loop back mode "
2632 "x%x received Data: x%x x%x x%x x%x\n",
2633 la->eventTag, phba->fc_eventTag,
2634 la->granted_AL_PA, la->UlnkSpeed,
2635 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002636 } else {
2637 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002638 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002639 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002640 la->eventTag, phba->fc_eventTag,
2641 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002642 phba->alpa_map[0],
2643 la->mm, la->fa,
2644 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002645 }
James Smart92d7f7b2007-06-17 19:56:38 -05002646 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002647 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002648 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002649 if (phba->link_flag & LS_LOOPBACK_MODE) {
2650 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2651 "1308 Link Down Event in loop back mode "
2652 "x%x received "
2653 "Data: x%x x%x x%x\n",
2654 la->eventTag, phba->fc_eventTag,
2655 phba->pport->port_state, vport->fc_flag);
2656 }
2657 else {
2658 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002659 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002660 "Data: x%x x%x x%x x%x x%x\n",
2661 la->eventTag, phba->fc_eventTag,
2662 phba->pport->port_state, vport->fc_flag,
2663 la->mm, la->fa);
2664 }
2665 lpfc_mbx_issue_link_down(phba);
2666 }
2667 if (la->mm && la->attType == AT_LINK_UP) {
2668 if (phba->link_state != LPFC_LINK_DOWN) {
2669 phba->fc_stat.LinkDown++;
2670 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2671 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002672 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002673 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002674 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002675 lpfc_mbx_issue_link_down(phba);
2676 } else
2677 lpfc_enable_la(phba);
2678
2679 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2680 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2681 "Data: x%x x%x x%x\n",
2682 la->eventTag, phba->fc_eventTag,
2683 phba->pport->port_state, vport->fc_flag);
2684 /*
2685 * The cmnd that triggered this will be waiting for this
2686 * signal.
2687 */
2688 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2689 if (phba->wait_4_mlo_maint_flg) {
2690 phba->wait_4_mlo_maint_flg = 0;
2691 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002692 }
James Smart84774a42008-08-24 21:50:06 -04002693 }
2694
2695 if (la->fa) {
2696 if (la->mm)
2697 lpfc_issue_clear_la(phba, vport);
2698 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2699 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002700 }
2701
2702lpfc_mbx_cmpl_read_la_free_mbuf:
2703 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2704 kfree(mp);
2705 mempool_free(pmb, phba->mbox_mem_pool);
2706 return;
2707}
2708
2709/*
2710 * This routine handles processing a REG_LOGIN mailbox
2711 * command upon completion. It is setup in the LPFC_MBOXQ
2712 * as the completion routine when the command is
2713 * handed off to the SLI layer.
2714 */
2715void
James Smart2e0fef82007-06-17 19:56:36 -05002716lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002717{
James Smart2e0fef82007-06-17 19:56:36 -05002718 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002719 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002720 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04002721 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002722
dea31012005-04-17 16:05:31 -05002723 pmb->context1 = NULL;
2724
James Smartffc95492010-06-07 15:23:17 -04002725 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
2726 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
2727
2728 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
2729 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
2730 /* We rcvd a rscn after issuing this
2731 * mbox reg login, we may have cycled
2732 * back through the state and be
2733 * back at reg login state so this
2734 * mbox needs to be ignored becase
2735 * there is another reg login in
2736 * proccess.
2737 */
2738 spin_lock_irq(shost->host_lock);
2739 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
2740 spin_unlock_irq(shost->host_lock);
2741 if (phba->sli_rev == LPFC_SLI_REV4)
2742 lpfc_sli4_free_rpi(phba,
2743 pmb->u.mb.un.varRegLogin.rpi);
2744
2745 } else
2746 /* Good status, call state machine */
2747 lpfc_disc_state_machine(vport, ndlp, pmb,
2748 NLP_EVT_CMPL_REG_LOGIN);
2749
dea31012005-04-17 16:05:31 -05002750 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2751 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002752 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002753 /* decrement the node reference count held for this callback
2754 * function.
2755 */
James Smart329f9bc2007-04-25 09:53:01 -04002756 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002757
2758 return;
2759}
2760
James Smart92d7f7b2007-06-17 19:56:38 -05002761static void
2762lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2763{
James Smart04c68492009-05-22 14:52:52 -04002764 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002765 struct lpfc_vport *vport = pmb->vport;
2766 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2767
2768 switch (mb->mbxStatus) {
2769 case 0x0011:
2770 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04002771 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2772 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2773 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002774 break;
James Smart78730cf2010-04-06 15:06:30 -04002775 /* If VPI is busy, reset the HBA */
2776 case 0x9700:
2777 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2778 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
2779 vport->vpi, mb->mbxStatus);
2780 if (!(phba->pport->load_flag & FC_UNLOADING))
2781 lpfc_workq_post_event(phba, NULL, NULL,
2782 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05002783 }
James Smart72100cc2010-02-12 14:43:01 -05002784 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002785 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05002786 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002787 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002788 vport->unreg_vpi_cmpl = VPORT_OK;
2789 mempool_free(pmb, phba->mbox_mem_pool);
2790 /*
2791 * This shost reference might have been taken at the beginning of
2792 * lpfc_vport_delete()
2793 */
James Smart1c6f4ef52009-11-18 15:40:49 -05002794 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05002795 scsi_host_put(shost);
2796}
2797
James Smartd7c255b2008-08-24 21:50:00 -04002798int
James Smart92d7f7b2007-06-17 19:56:38 -05002799lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2800{
2801 struct lpfc_hba *phba = vport->phba;
2802 LPFC_MBOXQ_t *mbox;
2803 int rc;
2804
2805 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2806 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002807 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002808
2809 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2810 mbox->vport = vport;
2811 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002812 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002813 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002814 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2815 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002816 mempool_free(mbox, phba->mbox_mem_pool);
2817 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002818 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002819 }
James Smartd7c255b2008-08-24 21:50:00 -04002820 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002821}
2822
2823static void
2824lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2825{
2826 struct lpfc_vport *vport = pmb->vport;
2827 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002828 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002829
2830 switch (mb->mbxStatus) {
2831 case 0x0011:
2832 case 0x9601:
2833 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002834 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2835 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2836 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002837 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2838 spin_lock_irq(shost->host_lock);
2839 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2840 spin_unlock_irq(shost->host_lock);
2841 vport->fc_myDID = 0;
2842 goto out;
2843 }
James Smart92d7f7b2007-06-17 19:56:38 -05002844
James Smart72100cc2010-02-12 14:43:01 -05002845 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002846 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002847 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002848 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002849 vport->num_disc_nodes = 0;
2850 /* go thru NPR list and issue ELS PLOGIs */
2851 if (vport->fc_npr_cnt)
2852 lpfc_els_disc_plogi(vport);
2853
2854 if (!vport->num_disc_nodes) {
2855 spin_lock_irq(shost->host_lock);
2856 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2857 spin_unlock_irq(shost->host_lock);
2858 lpfc_can_disctmo(vport);
2859 }
2860 vport->port_state = LPFC_VPORT_READY;
2861
2862out:
2863 mempool_free(pmb, phba->mbox_mem_pool);
2864 return;
2865}
2866
James Smart21e9a0a2009-05-22 14:53:21 -04002867/**
2868 * lpfc_create_static_vport - Read HBA config region to create static vports.
2869 * @phba: pointer to lpfc hba data structure.
2870 *
2871 * This routine issue a DUMP mailbox command for config region 22 to get
2872 * the list of static vports to be created. The function create vports
2873 * based on the information returned from the HBA.
2874 **/
2875void
2876lpfc_create_static_vport(struct lpfc_hba *phba)
2877{
2878 LPFC_MBOXQ_t *pmb = NULL;
2879 MAILBOX_t *mb;
2880 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04002881 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04002882 struct fc_vport_identifiers vport_id;
2883 struct fc_vport *new_fc_vport;
2884 struct Scsi_Host *shost;
2885 struct lpfc_vport *vport;
2886 uint16_t offset = 0;
2887 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04002888 struct lpfc_dmabuf *mp;
2889 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04002890
2891 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2892 if (!pmb) {
2893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2894 "0542 lpfc_create_static_vport failed to"
2895 " allocate mailbox memory\n");
2896 return;
2897 }
2898
2899 mb = &pmb->u.mb;
2900
2901 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2902 if (!vport_info) {
2903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2904 "0543 lpfc_create_static_vport failed to"
2905 " allocate vport_info\n");
2906 mempool_free(pmb, phba->mbox_mem_pool);
2907 return;
2908 }
2909
2910 vport_buff = (uint8_t *) vport_info;
2911 do {
James Smart1c6834a2009-07-19 10:01:26 -04002912 if (lpfc_dump_static_vport(phba, pmb, offset))
2913 goto out;
2914
James Smart21e9a0a2009-05-22 14:53:21 -04002915 pmb->vport = phba->pport;
2916 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2917
2918 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2919 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2920 "0544 lpfc_create_static_vport failed to"
2921 " issue dump mailbox command ret 0x%x "
2922 "status 0x%x\n",
2923 rc, mb->mbxStatus);
2924 goto out;
2925 }
2926
James Smart1c6834a2009-07-19 10:01:26 -04002927 if (phba->sli_rev == LPFC_SLI_REV4) {
2928 byte_count = pmb->u.mqe.un.mb_words[5];
2929 mp = (struct lpfc_dmabuf *) pmb->context2;
2930 if (byte_count > sizeof(struct static_vport_info) -
2931 offset)
2932 byte_count = sizeof(struct static_vport_info)
2933 - offset;
2934 memcpy(vport_buff + offset, mp->virt, byte_count);
2935 offset += byte_count;
2936 } else {
2937 if (mb->un.varDmp.word_cnt >
2938 sizeof(struct static_vport_info) - offset)
2939 mb->un.varDmp.word_cnt =
2940 sizeof(struct static_vport_info)
2941 - offset;
2942 byte_count = mb->un.varDmp.word_cnt;
2943 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2944 vport_buff + offset,
2945 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04002946
James Smart1c6834a2009-07-19 10:01:26 -04002947 offset += byte_count;
2948 }
James Smart21e9a0a2009-05-22 14:53:21 -04002949
James Smart1c6834a2009-07-19 10:01:26 -04002950 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04002951 offset < sizeof(struct static_vport_info));
2952
2953
2954 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2955 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2956 != VPORT_INFO_REV)) {
2957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2958 "0545 lpfc_create_static_vport bad"
2959 " information header 0x%x 0x%x\n",
2960 le32_to_cpu(vport_info->signature),
2961 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2962
2963 goto out;
2964 }
2965
2966 shost = lpfc_shost_from_vport(phba->pport);
2967
2968 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2969 memset(&vport_id, 0, sizeof(vport_id));
2970 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2971 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2972 if (!vport_id.port_name || !vport_id.node_name)
2973 continue;
2974
2975 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2976 vport_id.vport_type = FC_PORTTYPE_NPIV;
2977 vport_id.disable = false;
2978 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2979
2980 if (!new_fc_vport) {
2981 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2982 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04002983 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04002984 continue;
2985 }
2986
2987 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2988 vport->vport_flag |= STATIC_VPORT;
2989 }
2990
2991out:
James Smart21e9a0a2009-05-22 14:53:21 -04002992 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04002993 if (rc != MBX_TIMEOUT) {
2994 if (pmb->context2) {
2995 mp = (struct lpfc_dmabuf *) pmb->context2;
2996 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2997 kfree(mp);
2998 }
James Smart21e9a0a2009-05-22 14:53:21 -04002999 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003000 }
James Smart21e9a0a2009-05-22 14:53:21 -04003001
3002 return;
3003}
3004
dea31012005-04-17 16:05:31 -05003005/*
3006 * This routine handles processing a Fabric REG_LOGIN mailbox
3007 * command upon completion. It is setup in the LPFC_MBOXQ
3008 * as the completion routine when the command is
3009 * handed off to the SLI layer.
3010 */
3011void
James Smart2e0fef82007-06-17 19:56:36 -05003012lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003013{
James Smart92d7f7b2007-06-17 19:56:38 -05003014 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003015 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003016 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003017 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003018
James Smart549e55c2007-08-02 11:09:51 -04003019 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003020 pmb->context1 = NULL;
3021 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003022 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003023 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3024 "0258 Register Fabric login error: 0x%x\n",
3025 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003026 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3027 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003028 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003029
James Smart92d7f7b2007-06-17 19:56:38 -05003030 if (phba->fc_topology == TOPOLOGY_LOOP) {
3031 /* FLOGI failed, use loop map to make discovery list */
3032 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003033
James Smart92d7f7b2007-06-17 19:56:38 -05003034 /* Start discovery */
3035 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003036 /* Decrement the reference count to ndlp after the
3037 * reference to the ndlp are done.
3038 */
3039 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003040 return;
3041 }
3042
3043 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003044 /* Decrement the reference count to ndlp after the reference
3045 * to the ndlp are done.
3046 */
3047 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003048 return;
3049 }
3050
dea31012005-04-17 16:05:31 -05003051 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003052 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003053 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003054 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003055
James Smart2e0fef82007-06-17 19:56:36 -05003056 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003057 /* when physical port receive logo donot start
3058 * vport discovery */
3059 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3060 lpfc_start_fdiscs(phba);
3061 else
3062 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smart92d7f7b2007-06-17 19:56:38 -05003063 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003064 }
3065
3066 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3067 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003068 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003069
3070 /* Drop the reference count from the mbox at the end after
3071 * all the current reference to the ndlp have been done.
3072 */
3073 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003074 return;
3075}
3076
3077/*
3078 * This routine handles processing a NameServer REG_LOGIN mailbox
3079 * command upon completion. It is setup in the LPFC_MBOXQ
3080 * as the completion routine when the command is
3081 * handed off to the SLI layer.
3082 */
3083void
James Smart2e0fef82007-06-17 19:56:36 -05003084lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003085{
James Smart21e9a0a2009-05-22 14:53:21 -04003086 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003087 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3088 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3089 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003090
3091 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003092out:
James Smart21e9a0a2009-05-22 14:53:21 -04003093 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3094 "0260 Register NameServer error: 0x%x\n",
3095 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003096 /* decrement the node reference count held for this
3097 * callback function.
3098 */
James Smart329f9bc2007-04-25 09:53:01 -04003099 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003100 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3101 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003102 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003103
3104 /* If no other thread is using the ndlp, free it */
3105 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003106
James Smart92d7f7b2007-06-17 19:56:38 -05003107 if (phba->fc_topology == TOPOLOGY_LOOP) {
3108 /*
3109 * RegLogin failed, use loop map to make discovery
3110 * list
3111 */
3112 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003113
James Smart92d7f7b2007-06-17 19:56:38 -05003114 /* Start discovery */
3115 lpfc_disc_start(vport);
3116 return;
3117 }
3118 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003119 return;
3120 }
3121
3122 pmb->context1 = NULL;
3123
dea31012005-04-17 16:05:31 -05003124 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003125 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003126 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003127 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003128
James Smart2e0fef82007-06-17 19:56:36 -05003129 if (vport->port_state < LPFC_VPORT_READY) {
3130 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003131 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3132 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3133 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3134 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3135 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3136
3137 /* Issue SCR just before NameServer GID_FT Query */
3138 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003139 }
3140
James Smart2e0fef82007-06-17 19:56:36 -05003141 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003142 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003143 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003144 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003145 goto out;
dea31012005-04-17 16:05:31 -05003146 }
3147
James Smartfa4066b2008-01-11 01:53:27 -05003148 /* decrement the node reference count held for this
3149 * callback function.
3150 */
James Smart329f9bc2007-04-25 09:53:01 -04003151 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003152 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3153 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003154 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003155
3156 return;
3157}
3158
3159static void
James Smart2e0fef82007-06-17 19:56:36 -05003160lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003161{
James Smart2e0fef82007-06-17 19:56:36 -05003162 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3163 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003164 struct lpfc_rport_data *rdata;
3165 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003166 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003167
3168 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003169 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3170 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003171 rport_ids.port_id = ndlp->nlp_DID;
3172 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003173
James Smart329f9bc2007-04-25 09:53:01 -04003174 /*
3175 * We leave our node pointer in rport->dd_data when we unregister a
3176 * FCP target port. But fc_remote_port_add zeros the space to which
3177 * rport->dd_data points. So, if we're reusing a previously
3178 * registered port, drop the reference that we took the last time we
3179 * registered the port.
3180 */
3181 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003182 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003183 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003184
3185 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3186 "rport add: did:x%x flg:x%x type x%x",
3187 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3188
James Smart2e0fef82007-06-17 19:56:36 -05003189 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003190 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003191 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3192 "Warning: fc_remote_port_add failed\n");
3193 return;
3194 }
3195
3196 /* initialize static port data */
3197 rport->maxframe_size = ndlp->nlp_maxframe;
3198 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003199 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003200 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003201
3202 if (ndlp->nlp_type & NLP_FCP_TARGET)
3203 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3204 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3205 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3206
3207
3208 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3209 fc_remote_port_rolechg(rport, rport_ids.roles);
3210
James Smart071fbd3d2006-04-15 11:53:20 -04003211 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003212 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003213 ndlp->nlp_sid = rport->scsi_target_id;
3214 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003215 return;
3216}
3217
3218static void
James Smart2e0fef82007-06-17 19:56:36 -05003219lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003220{
3221 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003222
James Smart858c9f62007-06-17 19:56:39 -05003223 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3224 "rport delete: did:x%x flg:x%x type x%x",
3225 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3226
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003227 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003228
3229 return;
3230}
3231
James Smartde0c5b32007-04-25 09:52:27 -04003232static void
James Smart2e0fef82007-06-17 19:56:36 -05003233lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003234{
James Smart2e0fef82007-06-17 19:56:36 -05003235 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3236
3237 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003238 switch (state) {
3239 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003240 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003241 break;
3242 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003243 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003244 break;
3245 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003246 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003247 break;
3248 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003249 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003250 break;
3251 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003252 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003253 break;
3254 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003255 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003256 break;
3257 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003258 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003259 break;
3260 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003261 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003262 break;
3263 }
James Smart2e0fef82007-06-17 19:56:36 -05003264 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003265}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003266
James Smartde0c5b32007-04-25 09:52:27 -04003267static void
James Smart2e0fef82007-06-17 19:56:36 -05003268lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003269 int old_state, int new_state)
3270{
James Smart2e0fef82007-06-17 19:56:36 -05003271 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3272
James Smartde0c5b32007-04-25 09:52:27 -04003273 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04003274 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3275 ndlp->nlp_type |= NLP_FC_NODE;
3276 }
3277 if (new_state == NLP_STE_MAPPED_NODE)
3278 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3279 if (new_state == NLP_STE_NPR_NODE)
3280 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003281
James Smartde0c5b32007-04-25 09:52:27 -04003282 /* Transport interface */
3283 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3284 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003285 vport->phba->nport_event_cnt++;
3286 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003287 }
dea31012005-04-17 16:05:31 -05003288
James Smartde0c5b32007-04-25 09:52:27 -04003289 if (new_state == NLP_STE_MAPPED_NODE ||
3290 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003291 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003292 /*
3293 * Tell the fc transport about the port, if we haven't
3294 * already. If we have, and it's a scsi entity, be
3295 * sure to unblock any attached scsi devices
3296 */
3297 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003298 }
James Smartea2151b2008-09-07 11:52:10 -04003299 if ((new_state == NLP_STE_MAPPED_NODE) &&
3300 (vport->stat_data_enabled)) {
3301 /*
3302 * A new target is discovered, if there is no buffer for
3303 * statistical data collection allocate buffer.
3304 */
3305 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3306 sizeof(struct lpfc_scsicmd_bkt),
3307 GFP_KERNEL);
3308
3309 if (!ndlp->lat_data)
3310 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3311 "0286 lpfc_nlp_state_cleanup failed to "
3312 "allocate statistical data buffer DID "
3313 "0x%x\n", ndlp->nlp_DID);
3314 }
James Smart858c9f62007-06-17 19:56:39 -05003315 /*
3316 * if we added to Mapped list, but the remote port
3317 * registration failed or assigned a target id outside
3318 * our presentable range - move the node to the
3319 * Unmapped List
3320 */
James Smartde0c5b32007-04-25 09:52:27 -04003321 if (new_state == NLP_STE_MAPPED_NODE &&
3322 (!ndlp->rport ||
3323 ndlp->rport->scsi_target_id == -1 ||
3324 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003325 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003326 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003327 spin_unlock_irq(shost->host_lock);
3328 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003329 }
James Smartde0c5b32007-04-25 09:52:27 -04003330}
3331
James Smart685f0bf2007-04-25 09:53:08 -04003332static char *
3333lpfc_nlp_state_name(char *buffer, size_t size, int state)
3334{
3335 static char *states[] = {
3336 [NLP_STE_UNUSED_NODE] = "UNUSED",
3337 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
3338 [NLP_STE_ADISC_ISSUE] = "ADISC",
3339 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
3340 [NLP_STE_PRLI_ISSUE] = "PRLI",
3341 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
3342 [NLP_STE_MAPPED_NODE] = "MAPPED",
3343 [NLP_STE_NPR_NODE] = "NPR",
3344 };
3345
James Smart311464e2007-08-02 11:10:37 -04003346 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04003347 strlcpy(buffer, states[state], size);
3348 else
3349 snprintf(buffer, size, "unknown (%d)", state);
3350 return buffer;
3351}
3352
James Smartde0c5b32007-04-25 09:52:27 -04003353void
James Smart2e0fef82007-06-17 19:56:36 -05003354lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3355 int state)
James Smartde0c5b32007-04-25 09:52:27 -04003356{
James Smart2e0fef82007-06-17 19:56:36 -05003357 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04003358 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04003359 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04003360
James Smarte8b62012007-08-02 11:10:09 -04003361 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3362 "0904 NPort state transition x%06x, %s -> %s\n",
3363 ndlp->nlp_DID,
3364 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
3365 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05003366
3367 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3368 "node statechg did:x%x old:%d ste:%d",
3369 ndlp->nlp_DID, old_state, state);
3370
James Smartde0c5b32007-04-25 09:52:27 -04003371 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04003372 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05003373 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003374 if (old_state == NLP_STE_UNMAPPED_NODE) {
3375 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
3376 ndlp->nlp_type &= ~NLP_FC_NODE;
3377 }
3378
James Smart685f0bf2007-04-25 09:53:08 -04003379 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05003380 spin_lock_irq(shost->host_lock);
3381 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3382 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003383 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05003384 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04003385
3386 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05003387 lpfc_nlp_counters(vport, state, 1);
3388 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04003389}
3390
3391void
James Smarte47c9092008-02-08 18:49:26 -05003392lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3393{
3394 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3395
3396 if (list_empty(&ndlp->nlp_listp)) {
3397 spin_lock_irq(shost->host_lock);
3398 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3399 spin_unlock_irq(shost->host_lock);
3400 }
3401}
3402
3403void
James Smart2e0fef82007-06-17 19:56:36 -05003404lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003405{
James Smart2e0fef82007-06-17 19:56:36 -05003406 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3407
James Smart0d2b6b82008-06-14 22:52:47 -04003408 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003409 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05003410 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3411 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003412 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05003413 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003414 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05003415 NLP_STE_UNUSED_NODE);
3416}
3417
Adrian Bunk4d9db012008-02-14 23:24:02 +02003418static void
James Smarte47c9092008-02-08 18:49:26 -05003419lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3420{
James Smart0d2b6b82008-06-14 22:52:47 -04003421 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003422 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3423 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3424 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3425 NLP_STE_UNUSED_NODE);
3426}
James Smart109f6ed2008-12-04 22:39:08 -05003427/**
James Smart3621a712009-04-06 18:47:14 -04003428 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05003429 * @vport: Pointer to Virtual Port object.
3430 * @ndlp: Pointer to FC node object.
3431 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04003432 *
3433 * This function is always called when node object need to be initialized.
3434 * It initializes all the fields of the node object. Although the reference
3435 * to phba from @ndlp can be obtained indirectly through it's reference to
3436 * @vport, a direct reference to phba is taken here by @ndlp. This is due
3437 * to the life-span of the @ndlp might go beyond the existence of @vport as
3438 * the final release of ndlp is determined by its reference count. And, the
3439 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05003440 **/
3441static inline void
3442lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3443 uint32_t did)
3444{
3445 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3446 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3447 init_timer(&ndlp->nlp_delayfunc);
3448 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3449 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3450 ndlp->nlp_DID = did;
3451 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04003452 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05003453 ndlp->nlp_sid = NLP_NO_SID;
3454 kref_init(&ndlp->kref);
3455 NLP_INT_NODE_ACT(ndlp);
3456 atomic_set(&ndlp->cmd_pending, 0);
3457 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
3458}
James Smarte47c9092008-02-08 18:49:26 -05003459
3460struct lpfc_nodelist *
3461lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3462 int state)
3463{
3464 struct lpfc_hba *phba = vport->phba;
3465 uint32_t did;
3466 unsigned long flags;
3467
3468 if (!ndlp)
3469 return NULL;
3470
3471 spin_lock_irqsave(&phba->ndlp_lock, flags);
3472 /* The ndlp should not be in memory free mode */
3473 if (NLP_CHK_FREE_REQ(ndlp)) {
3474 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3475 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3476 "0277 lpfc_enable_node: ndlp:x%p "
3477 "usgmap:x%x refcnt:%d\n",
3478 (void *)ndlp, ndlp->nlp_usg_map,
3479 atomic_read(&ndlp->kref.refcount));
3480 return NULL;
3481 }
3482 /* The ndlp should not already be in active mode */
3483 if (NLP_CHK_NODE_ACT(ndlp)) {
3484 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3485 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3486 "0278 lpfc_enable_node: ndlp:x%p "
3487 "usgmap:x%x refcnt:%d\n",
3488 (void *)ndlp, ndlp->nlp_usg_map,
3489 atomic_read(&ndlp->kref.refcount));
3490 return NULL;
3491 }
3492
3493 /* Keep the original DID */
3494 did = ndlp->nlp_DID;
3495
3496 /* re-initialize ndlp except of ndlp linked list pointer */
3497 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3498 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003499 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003500
3501 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3502
3503 if (state != NLP_STE_UNUSED_NODE)
3504 lpfc_nlp_set_state(vport, ndlp, state);
3505
3506 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3507 "node enable: did:x%x",
3508 ndlp->nlp_DID, 0, 0);
3509 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003510}
3511
3512void
James Smart2e0fef82007-06-17 19:56:36 -05003513lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003514{
James Smart87af33f2007-10-27 13:37:43 -04003515 /*
James Smartfa4066b2008-01-11 01:53:27 -05003516 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003517 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003518 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3519 * until ALL other outstanding threads have completed. We check
3520 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003521 */
James Smartfa4066b2008-01-11 01:53:27 -05003522 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3523 return;
James Smart51ef4c22007-08-02 11:10:31 -04003524 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003525 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003526 return;
dea31012005-04-17 16:05:31 -05003527}
3528
3529/*
3530 * Start / ReStart rescue timer for Discovery / RSCN handling
3531 */
3532void
James Smart2e0fef82007-06-17 19:56:36 -05003533lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003534{
James Smart2e0fef82007-06-17 19:56:36 -05003535 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3536 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003537 uint32_t tmo;
3538
James Smart2e0fef82007-06-17 19:56:36 -05003539 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003540 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003541 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3542 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003543 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003544 * FC spec states we need 3 * ratov for CT requests
3545 */
3546 tmo = ((phba->fc_ratov * 3) + 3);
3547 }
dea31012005-04-17 16:05:31 -05003548
James Smart858c9f62007-06-17 19:56:39 -05003549
3550 if (!timer_pending(&vport->fc_disctmo)) {
3551 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3552 "set disc timer: tmo:x%x state:x%x flg:x%x",
3553 tmo, vport->port_state, vport->fc_flag);
3554 }
3555
James Smart2e0fef82007-06-17 19:56:36 -05003556 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3557 spin_lock_irq(shost->host_lock);
3558 vport->fc_flag |= FC_DISC_TMO;
3559 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003560
3561 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003562 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3563 "0247 Start Discovery Timer state x%x "
3564 "Data: x%x x%lx x%x x%x\n",
3565 vport->port_state, tmo,
3566 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3567 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003568
3569 return;
3570}
3571
3572/*
3573 * Cancel rescue timer for Discovery / RSCN handling
3574 */
3575int
James Smart2e0fef82007-06-17 19:56:36 -05003576lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003577{
James Smart2e0fef82007-06-17 19:56:36 -05003578 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003579 unsigned long iflags;
3580
James Smart858c9f62007-06-17 19:56:39 -05003581 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3582 "can disc timer: state:x%x rtry:x%x flg:x%x",
3583 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3584
dea31012005-04-17 16:05:31 -05003585 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003586 if (vport->fc_flag & FC_DISC_TMO) {
3587 spin_lock_irqsave(shost->host_lock, iflags);
3588 vport->fc_flag &= ~FC_DISC_TMO;
3589 spin_unlock_irqrestore(shost->host_lock, iflags);
3590 del_timer_sync(&vport->fc_disctmo);
3591 spin_lock_irqsave(&vport->work_port_lock, iflags);
3592 vport->work_port_events &= ~WORKER_DISC_TMO;
3593 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003594 }
3595
3596 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003597 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3598 "0248 Cancel Discovery Timer state x%x "
3599 "Data: x%x x%x x%x\n",
3600 vport->port_state, vport->fc_flag,
3601 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003602 return 0;
dea31012005-04-17 16:05:31 -05003603}
3604
3605/*
3606 * Check specified ring for outstanding IOCB on the SLI queue
3607 * Return true if iocb matches the specified nport
3608 */
3609int
James Smart2e0fef82007-06-17 19:56:36 -05003610lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3611 struct lpfc_sli_ring *pring,
3612 struct lpfc_iocbq *iocb,
3613 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003614{
James Smart2e0fef82007-06-17 19:56:36 -05003615 struct lpfc_sli *psli = &phba->sli;
3616 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003617 struct lpfc_vport *vport = ndlp->vport;
3618
3619 if (iocb->vport != vport)
3620 return 0;
3621
dea31012005-04-17 16:05:31 -05003622 if (pring->ringno == LPFC_ELS_RING) {
3623 switch (icmd->ulpCommand) {
3624 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003625 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003626 return 1;
dea31012005-04-17 16:05:31 -05003627 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003628 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3629 return 1;
dea31012005-04-17 16:05:31 -05003630 case CMD_XMIT_ELS_RSP64_CX:
3631 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003632 return 1;
dea31012005-04-17 16:05:31 -05003633 }
James Smarta4bc3372006-12-02 13:34:16 -05003634 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003635
3636 } else if (pring->ringno == psli->fcp_ring) {
3637 /* Skip match check if waiting to relogin to FCP target */
3638 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003639 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003640 return 0;
dea31012005-04-17 16:05:31 -05003641 }
3642 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003643 return 1;
dea31012005-04-17 16:05:31 -05003644 }
3645 } else if (pring->ringno == psli->next_ring) {
3646
3647 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003648 return 0;
dea31012005-04-17 16:05:31 -05003649}
3650
3651/*
3652 * Free resources / clean up outstanding I/Os
3653 * associated with nlp_rpi in the LPFC_NODELIST entry.
3654 */
3655static int
James Smart2e0fef82007-06-17 19:56:36 -05003656lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003657{
James Smart2534ba72007-04-25 09:52:20 -04003658 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003659 struct lpfc_sli *psli;
3660 struct lpfc_sli_ring *pring;
3661 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003662 uint32_t i;
dea31012005-04-17 16:05:31 -05003663
James Smart92d7f7b2007-06-17 19:56:38 -05003664 lpfc_fabric_abort_nport(ndlp);
3665
dea31012005-04-17 16:05:31 -05003666 /*
3667 * Everything that matches on txcmplq will be returned
3668 * by firmware with a no rpi error.
3669 */
3670 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003671 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003672 /* Now process each ring */
3673 for (i = 0; i < psli->num_rings; i++) {
3674 pring = &psli->ring[i];
3675
James Smart2e0fef82007-06-17 19:56:36 -05003676 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003677 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003678 list) {
dea31012005-04-17 16:05:31 -05003679 /*
3680 * Check to see if iocb matches the nport we are
3681 * looking for
3682 */
James Smart92d7f7b2007-06-17 19:56:38 -05003683 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3684 ndlp))) {
dea31012005-04-17 16:05:31 -05003685 /* It matches, so deque and call compl
3686 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003687 list_move_tail(&iocb->list,
3688 &completions);
dea31012005-04-17 16:05:31 -05003689 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003690 }
3691 }
James Smart2e0fef82007-06-17 19:56:36 -05003692 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003693 }
3694 }
James Smart2534ba72007-04-25 09:52:20 -04003695
James Smarta257bf92009-04-06 18:48:10 -04003696 /* Cancel all the IOCBs from the completions list */
3697 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3698 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003699
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003700 return 0;
dea31012005-04-17 16:05:31 -05003701}
3702
3703/*
3704 * Free rpi associated with LPFC_NODELIST entry.
3705 * This routine is called from lpfc_freenode(), when we are removing
3706 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3707 * LOGO that completes successfully, and we are waiting to PLOGI back
3708 * to the remote NPort. In addition, it is called after we receive
3709 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3710 * we are waiting to PLOGI back to the remote NPort.
3711 */
3712int
James Smart2e0fef82007-06-17 19:56:36 -05003713lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003714{
James Smart2e0fef82007-06-17 19:56:36 -05003715 struct lpfc_hba *phba = vport->phba;
3716 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003717 int rc;
3718
James Smart21e9a0a2009-05-22 14:53:21 -04003719 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003720 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3721 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003722 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003723 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003724 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003725 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003726 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003727 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003728 }
dea31012005-04-17 16:05:31 -05003729 lpfc_no_rpi(phba, ndlp);
3730 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003731 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003732 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003733 return 1;
3734 }
3735 return 0;
3736}
3737
James Smartecfd03c2010-02-12 14:41:27 -05003738/**
3739 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
3740 * @phba: pointer to lpfc hba data structure.
3741 *
3742 * This routine is invoked to unregister all the currently registered RPIs
3743 * to the HBA.
3744 **/
3745void
3746lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
3747{
3748 struct lpfc_vport **vports;
3749 struct lpfc_nodelist *ndlp;
3750 struct Scsi_Host *shost;
3751 int i;
3752
3753 vports = lpfc_create_vport_work_array(phba);
3754 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3755 shost = lpfc_shost_from_vport(vports[i]);
3756 spin_lock_irq(shost->host_lock);
3757 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003758 if (ndlp->nlp_flag & NLP_RPI_VALID) {
3759 /* The mempool_alloc might sleep */
3760 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05003761 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05003762 spin_lock_irq(shost->host_lock);
3763 }
James Smartecfd03c2010-02-12 14:41:27 -05003764 }
3765 spin_unlock_irq(shost->host_lock);
3766 }
3767 lpfc_destroy_vport_work_array(phba, vports);
3768}
3769
James Smart92d7f7b2007-06-17 19:56:38 -05003770void
3771lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3772{
3773 struct lpfc_hba *phba = vport->phba;
3774 LPFC_MBOXQ_t *mbox;
3775 int rc;
3776
3777 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3778 if (mbox) {
3779 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3780 mbox->vport = vport;
3781 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003782 mbox->context1 = NULL;
3783 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003784 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003785 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003786
3787 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3788 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3789 "1836 Could not issue "
3790 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003791 }
3792}
3793
3794void
3795lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3796{
3797 struct lpfc_hba *phba = vport->phba;
3798 LPFC_MBOXQ_t *mbox;
3799 int rc;
3800
3801 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3802 if (mbox) {
3803 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3804 mbox->vport = vport;
3805 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003806 mbox->context1 = NULL;
3807 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003808 if (rc != MBX_TIMEOUT)
3809 mempool_free(mbox, phba->mbox_mem_pool);
3810
3811 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003812 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3813 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003814 "unreg_did (default rpis) status %d\n",
3815 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003816 }
3817}
3818
dea31012005-04-17 16:05:31 -05003819/*
3820 * Free resources associated with LPFC_NODELIST entry
3821 * so it can be freed.
3822 */
3823static int
James Smart2e0fef82007-06-17 19:56:36 -05003824lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003825{
James Smart2e0fef82007-06-17 19:56:36 -05003826 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3827 struct lpfc_hba *phba = vport->phba;
3828 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003829 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003830
3831 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003832 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3833 "0900 Cleanup node for NPort x%x "
3834 "Data: x%x x%x x%x\n",
3835 ndlp->nlp_DID, ndlp->nlp_flag,
3836 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003837 if (NLP_CHK_FREE_REQ(ndlp)) {
3838 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3839 "0280 lpfc_cleanup_node: ndlp:x%p "
3840 "usgmap:x%x refcnt:%d\n",
3841 (void *)ndlp, ndlp->nlp_usg_map,
3842 atomic_read(&ndlp->kref.refcount));
3843 lpfc_dequeue_node(vport, ndlp);
3844 } else {
3845 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3846 "0281 lpfc_cleanup_node: ndlp:x%p "
3847 "usgmap:x%x refcnt:%d\n",
3848 (void *)ndlp, ndlp->nlp_usg_map,
3849 atomic_read(&ndlp->kref.refcount));
3850 lpfc_disable_node(vport, ndlp);
3851 }
dea31012005-04-17 16:05:31 -05003852
dea31012005-04-17 16:05:31 -05003853 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3854 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003855 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003856 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3857 mb->context2 = NULL;
3858 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3859 }
3860 }
James Smart33ccf8d2006-08-17 11:57:58 -04003861
James Smart2e0fef82007-06-17 19:56:36 -05003862 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003863 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003864 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003865 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003866 mp = (struct lpfc_dmabuf *) (mb->context1);
3867 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003868 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003869 kfree(mp);
3870 }
3871 list_del(&mb->list);
James Smartffc95492010-06-07 15:23:17 -04003872 if (phba->sli_rev == LPFC_SLI_REV4)
3873 lpfc_sli4_free_rpi(phba,
3874 mb->u.mb.un.varRegLogin.rpi);
dea31012005-04-17 16:05:31 -05003875 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003876 /* We shall not invoke the lpfc_nlp_put to decrement
3877 * the ndlp reference count as we are in the process
3878 * of lpfc_nlp_release.
3879 */
dea31012005-04-17 16:05:31 -05003880 }
3881 }
James Smart2e0fef82007-06-17 19:56:36 -05003882 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003883
James Smarte47c9092008-02-08 18:49:26 -05003884 lpfc_els_abort(phba, ndlp);
3885
James Smart2e0fef82007-06-17 19:56:36 -05003886 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003887 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003888 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003889
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003890 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003891 del_timer_sync(&ndlp->nlp_delayfunc);
3892
James Smart0d2b6b82008-06-14 22:52:47 -04003893 list_del_init(&ndlp->els_retry_evt.evt_listp);
3894 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003895
James Smart2e0fef82007-06-17 19:56:36 -05003896 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003897
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003898 return 0;
dea31012005-04-17 16:05:31 -05003899}
3900
3901/*
3902 * Check to see if we can free the nlp back to the freelist.
3903 * If we are in the middle of using the nlp in the discovery state
3904 * machine, defer the free till we reach the end of the state machine.
3905 */
James Smart329f9bc2007-04-25 09:53:01 -04003906static void
James Smart2e0fef82007-06-17 19:56:36 -05003907lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003908{
James Smarta8adb832007-10-27 13:37:53 -04003909 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003910 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003911 LPFC_MBOXQ_t *mbox;
3912 int rc;
dea31012005-04-17 16:05:31 -05003913
James Smart0d2b6b82008-06-14 22:52:47 -04003914 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003915 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smartffc95492010-06-07 15:23:17 -04003916 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
James Smart21e9a0a2009-05-22 14:53:21 -04003917 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003918 /* For this case we need to cleanup the default rpi
3919 * allocated by the firmware.
3920 */
3921 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3922 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003923 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003924 (uint8_t *) &vport->fc_sparam, mbox, 0);
3925 if (rc) {
3926 mempool_free(mbox, phba->mbox_mem_pool);
3927 }
3928 else {
3929 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3930 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3931 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003932 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003933 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3934 if (rc == MBX_NOT_FINISHED) {
3935 mempool_free(mbox, phba->mbox_mem_pool);
3936 }
3937 }
3938 }
3939 }
James Smart2e0fef82007-06-17 19:56:36 -05003940 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003941
James Smart2e0fef82007-06-17 19:56:36 -05003942 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003943 * We can get here with a non-NULL ndlp->rport because when we
3944 * unregister a rport we don't break the rport/node linkage. So if we
3945 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003946 */
James Smart92d7f7b2007-06-17 19:56:38 -05003947 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003948 rdata = ndlp->rport->dd_data;
3949 rdata->pnode = NULL;
3950 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003951 }
dea31012005-04-17 16:05:31 -05003952}
3953
3954static int
James Smart2e0fef82007-06-17 19:56:36 -05003955lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3956 uint32_t did)
dea31012005-04-17 16:05:31 -05003957{
James Smart2e0fef82007-06-17 19:56:36 -05003958 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003959
3960 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003961 return 0;
dea31012005-04-17 16:05:31 -05003962
dea31012005-04-17 16:05:31 -05003963 /* First check for Direct match */
3964 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003965 return 1;
dea31012005-04-17 16:05:31 -05003966
3967 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003968 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003969 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003970 return 0;
dea31012005-04-17 16:05:31 -05003971 }
3972
3973 matchdid.un.word = did;
3974 ndlpdid.un.word = ndlp->nlp_DID;
3975 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3976 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3977 (mydid.un.b.area == matchdid.un.b.area)) {
3978 if ((ndlpdid.un.b.domain == 0) &&
3979 (ndlpdid.un.b.area == 0)) {
3980 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003981 return 1;
dea31012005-04-17 16:05:31 -05003982 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003983 return 0;
dea31012005-04-17 16:05:31 -05003984 }
3985
3986 matchdid.un.word = ndlp->nlp_DID;
3987 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3988 (mydid.un.b.area == ndlpdid.un.b.area)) {
3989 if ((matchdid.un.b.domain == 0) &&
3990 (matchdid.un.b.area == 0)) {
3991 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003992 return 1;
dea31012005-04-17 16:05:31 -05003993 }
3994 }
3995 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003996 return 0;
dea31012005-04-17 16:05:31 -05003997}
3998
James Smart685f0bf2007-04-25 09:53:08 -04003999/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004000static struct lpfc_nodelist *
4001__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004002{
James Smart2fb9bd82006-12-02 13:33:57 -05004003 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004004 uint32_t data1;
4005
James Smart2e0fef82007-06-17 19:56:36 -05004006 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4007 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004008 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4009 ((uint32_t) ndlp->nlp_xri << 16) |
4010 ((uint32_t) ndlp->nlp_type << 8) |
4011 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004012 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4013 "0929 FIND node DID "
4014 "Data: x%p x%x x%x x%x\n",
4015 ndlp, ndlp->nlp_DID,
4016 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04004017 return ndlp;
dea31012005-04-17 16:05:31 -05004018 }
4019 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004020
dea31012005-04-17 16:05:31 -05004021 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004022 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4023 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004024 return NULL;
4025}
4026
4027struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004028lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004029{
James Smart2e0fef82007-06-17 19:56:36 -05004030 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004031 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004032
James Smart2e0fef82007-06-17 19:56:36 -05004033 spin_lock_irq(shost->host_lock);
4034 ndlp = __lpfc_findnode_did(vport, did);
4035 spin_unlock_irq(shost->host_lock);
4036 return ndlp;
4037}
4038
4039struct lpfc_nodelist *
4040lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4041{
4042 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4043 struct lpfc_nodelist *ndlp;
4044
4045 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004046 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004047 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4048 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004049 return NULL;
4050 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004051 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004052 if (!ndlp)
4053 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004054 lpfc_nlp_init(vport, ndlp, did);
4055 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4056 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004057 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004058 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004059 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004060 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4061 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4062 if (!ndlp)
4063 return NULL;
4064 spin_lock_irq(shost->host_lock);
4065 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4066 spin_unlock_irq(shost->host_lock);
4067 return ndlp;
dea31012005-04-17 16:05:31 -05004068 }
James Smarte47c9092008-02-08 18:49:26 -05004069
James Smart58da1ff2008-04-07 10:15:56 -04004070 if ((vport->fc_flag & FC_RSCN_MODE) &&
4071 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004072 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04004073 /* If we've already recieved a PLOGI from this NPort
4074 * we don't need to try to discover it again.
4075 */
4076 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4077 return NULL;
4078
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004079 /* Since this node is marked for discovery,
4080 * delay timeout is not needed.
4081 */
James Smart0d2b6b82008-06-14 22:52:47 -04004082 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004083 spin_lock_irq(shost->host_lock);
4084 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4085 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004086 } else
dea31012005-04-17 16:05:31 -05004087 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004088 } else {
James Smart87af33f2007-10-27 13:37:43 -04004089 /* If we've already recieved a PLOGI from this NPort,
4090 * or we are already in the process of discovery on it,
4091 * we don't need to try to discover it again.
4092 */
James Smart685f0bf2007-04-25 09:53:08 -04004093 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004094 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4095 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004096 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004097 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4098 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004099 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004100 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004101 }
4102 return ndlp;
4103}
4104
4105/* Build a list of nodes to discover based on the loopmap */
4106void
James Smart2e0fef82007-06-17 19:56:36 -05004107lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004108{
James Smart2e0fef82007-06-17 19:56:36 -05004109 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004110 int j;
4111 uint32_t alpa, index;
4112
James Smart2e0fef82007-06-17 19:56:36 -05004113 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004114 return;
James Smart2e0fef82007-06-17 19:56:36 -05004115
4116 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004117 return;
dea31012005-04-17 16:05:31 -05004118
4119 /* Check for loop map present or not */
4120 if (phba->alpa_map[0]) {
4121 for (j = 1; j <= phba->alpa_map[0]; j++) {
4122 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004123 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004124 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004125 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004126 }
4127 } else {
4128 /* No alpamap, so try all alpa's */
4129 for (j = 0; j < FC_MAXLOOP; j++) {
4130 /* If cfg_scan_down is set, start from highest
4131 * ALPA (0xef) to lowest (0x1).
4132 */
James Smart3de2a652007-08-02 11:09:59 -04004133 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004134 index = j;
4135 else
4136 index = FC_MAXLOOP - j - 1;
4137 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004138 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004139 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004140 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004141 }
4142 }
4143 return;
4144}
4145
dea31012005-04-17 16:05:31 -05004146void
James Smart2e0fef82007-06-17 19:56:36 -05004147lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004148{
dea31012005-04-17 16:05:31 -05004149 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004150 struct lpfc_sli *psli = &phba->sli;
4151 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4152 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4153 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4154 int rc;
4155
James Smart92d7f7b2007-06-17 19:56:38 -05004156 /*
4157 * if it's not a physical port or if we already send
4158 * clear_la then don't send it.
4159 */
4160 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004161 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4162 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004163 return;
4164
James Smart2e0fef82007-06-17 19:56:36 -05004165 /* Link up discovery */
4166 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4167 phba->link_state = LPFC_CLEAR_LA;
4168 lpfc_clear_la(phba, mbox);
4169 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4170 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004171 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004172 if (rc == MBX_NOT_FINISHED) {
4173 mempool_free(mbox, phba->mbox_mem_pool);
4174 lpfc_disc_flush_list(vport);
4175 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4176 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4177 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004178 phba->link_state = LPFC_HBA_ERROR;
4179 }
4180 }
4181}
4182
4183/* Reg_vpi to tell firmware to resume normal operations */
4184void
4185lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4186{
4187 LPFC_MBOXQ_t *regvpimbox;
4188
4189 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4190 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004191 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004192 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4193 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004194 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004195 == MBX_NOT_FINISHED) {
4196 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004197 }
4198 }
4199}
4200
4201/* Start Link up / RSCN discovery on NPR nodes */
4202void
4203lpfc_disc_start(struct lpfc_vport *vport)
4204{
4205 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4206 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004207 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004208 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004209 int did_changed;
dea31012005-04-17 16:05:31 -05004210
James Smart2e0fef82007-06-17 19:56:36 -05004211 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004212 return;
James Smart2e0fef82007-06-17 19:56:36 -05004213
4214 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004215 clear_la_pending = 1;
4216 else
4217 clear_la_pending = 0;
4218
James Smart2e0fef82007-06-17 19:56:36 -05004219 if (vport->port_state < LPFC_VPORT_READY)
4220 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004221
James Smart2e0fef82007-06-17 19:56:36 -05004222 lpfc_set_disctmo(vport);
4223
4224 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004225 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004226 else
dea31012005-04-17 16:05:31 -05004227 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004228
4229 vport->fc_prevDID = vport->fc_myDID;
4230 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004231
4232 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004233 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4234 "0202 Start Discovery hba state x%x "
4235 "Data: x%x x%x x%x\n",
4236 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4237 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004238
4239 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004240 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004241
4242 if (num_sent)
4243 return;
4244
James Smart92d7f7b2007-06-17 19:56:38 -05004245 /*
4246 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
4247 * continue discovery.
4248 */
4249 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004250 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004251 !(vport->fc_flag & FC_RSCN_MODE) &&
4252 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004253 lpfc_issue_reg_vpi(phba, vport);
4254 return;
4255 }
James Smart2e0fef82007-06-17 19:56:36 -05004256
James Smart92d7f7b2007-06-17 19:56:38 -05004257 /*
4258 * For SLI2, we need to set port_state to READY and continue
4259 * discovery.
4260 */
4261 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4262 /* If we get here, there is nothing to ADISC */
4263 if (vport->port_type == LPFC_PHYSICAL_PORT)
4264 lpfc_issue_clear_la(phba, vport);
4265
4266 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004267 vport->num_disc_nodes = 0;
4268 /* go thru NPR nodes and issue ELS PLOGIs */
4269 if (vport->fc_npr_cnt)
4270 lpfc_els_disc_plogi(vport);
4271
4272 if (!vport->num_disc_nodes) {
4273 spin_lock_irq(shost->host_lock);
4274 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4275 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004276 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05004277 }
4278 }
James Smart92d7f7b2007-06-17 19:56:38 -05004279 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004280 } else {
4281 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004282 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05004283
4284 if (num_sent)
4285 return;
4286
James Smart2e0fef82007-06-17 19:56:36 -05004287 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05004288 /* Check to see if more RSCNs came in while we
4289 * were processing this one.
4290 */
James Smart2e0fef82007-06-17 19:56:36 -05004291 if ((vport->fc_rscn_id_cnt == 0) &&
4292 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
4293 spin_lock_irq(shost->host_lock);
4294 vport->fc_flag &= ~FC_RSCN_MODE;
4295 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004296 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004297 } else
James Smart2e0fef82007-06-17 19:56:36 -05004298 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05004299 }
4300 }
4301 return;
4302}
4303
4304/*
4305 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
4306 * ring the match the sppecified nodelist.
4307 */
4308static void
James Smart2e0fef82007-06-17 19:56:36 -05004309lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004310{
James Smart2534ba72007-04-25 09:52:20 -04004311 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004312 struct lpfc_sli *psli;
4313 IOCB_t *icmd;
4314 struct lpfc_iocbq *iocb, *next_iocb;
4315 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004316
4317 psli = &phba->sli;
4318 pring = &psli->ring[LPFC_ELS_RING];
4319
4320 /* Error matching iocb on txq or txcmplq
4321 * First check the txq.
4322 */
James Smart2e0fef82007-06-17 19:56:36 -05004323 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004324 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4325 if (iocb->context1 != ndlp) {
4326 continue;
4327 }
4328 icmd = &iocb->iocb;
4329 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
4330 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
4331
James Smart2534ba72007-04-25 09:52:20 -04004332 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05004333 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05004334 }
4335 }
4336
4337 /* Next check the txcmplq */
4338 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
4339 if (iocb->context1 != ndlp) {
4340 continue;
4341 }
4342 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004343 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
4344 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04004345 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05004346 }
4347 }
James Smart2e0fef82007-06-17 19:56:36 -05004348 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04004349
James Smarta257bf92009-04-06 18:48:10 -04004350 /* Cancel all the IOCBs from the completions list */
4351 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4352 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004353}
4354
Adrian Bunka6ababd2007-11-05 18:07:33 +01004355static void
James Smart2e0fef82007-06-17 19:56:36 -05004356lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004357{
4358 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05004359 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004360
James Smart2e0fef82007-06-17 19:56:36 -05004361 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
4362 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004363 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004364 if (!NLP_CHK_NODE_ACT(ndlp))
4365 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004366 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4367 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
4368 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04004369 }
dea31012005-04-17 16:05:31 -05004370 }
4371 }
dea31012005-04-17 16:05:31 -05004372}
4373
James Smart92d7f7b2007-06-17 19:56:38 -05004374void
4375lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
4376{
4377 lpfc_els_flush_rscn(vport);
4378 lpfc_els_flush_cmd(vport);
4379 lpfc_disc_flush_list(vport);
4380}
4381
dea31012005-04-17 16:05:31 -05004382/*****************************************************************************/
4383/*
4384 * NAME: lpfc_disc_timeout
4385 *
4386 * FUNCTION: Fibre Channel driver discovery timeout routine.
4387 *
4388 * EXECUTION ENVIRONMENT: interrupt only
4389 *
4390 * CALLED FROM:
4391 * Timer function
4392 *
4393 * RETURNS:
4394 * none
4395 */
4396/*****************************************************************************/
4397void
4398lpfc_disc_timeout(unsigned long ptr)
4399{
James Smart2e0fef82007-06-17 19:56:36 -05004400 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4401 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04004402 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004403 unsigned long flags = 0;
4404
4405 if (unlikely(!phba))
4406 return;
4407
James Smart5e9d9b82008-06-14 22:52:53 -04004408 spin_lock_irqsave(&vport->work_port_lock, flags);
4409 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4410 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05004411 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04004412 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05004413
James Smart5e9d9b82008-06-14 22:52:53 -04004414 if (!tmo_posted)
4415 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004416 return;
4417}
4418
4419static void
James Smart2e0fef82007-06-17 19:56:36 -05004420lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004421{
James Smart2e0fef82007-06-17 19:56:36 -05004422 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4423 struct lpfc_hba *phba = vport->phba;
4424 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004425 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05004426 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05004427 int rc, clrlaerr = 0;
4428
James Smart2e0fef82007-06-17 19:56:36 -05004429 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05004430 return;
4431
James Smart2e0fef82007-06-17 19:56:36 -05004432 spin_lock_irq(shost->host_lock);
4433 vport->fc_flag &= ~FC_DISC_TMO;
4434 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004435
James Smart858c9f62007-06-17 19:56:39 -05004436 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4437 "disc timeout: state:x%x rtry:x%x flg:x%x",
4438 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4439
James Smart2e0fef82007-06-17 19:56:36 -05004440 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05004441
4442 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05004443 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
4444 * FAN
4445 */
4446 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04004447 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4448 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004449 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05004450 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004451 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004452 if (!NLP_CHK_NODE_ACT(ndlp))
4453 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004454 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4455 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004456 if (ndlp->nlp_type & NLP_FABRIC) {
4457 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05004458 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004459
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004460 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004461 /* Fail outstanding IO now since device
4462 * is marked for PLOGI.
4463 */
James Smart2e0fef82007-06-17 19:56:36 -05004464 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004465 }
4466 }
James Smart92d7f7b2007-06-17 19:56:38 -05004467 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05004468 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05004469 return;
James Smart92d7f7b2007-06-17 19:56:38 -05004470 }
dea31012005-04-17 16:05:31 -05004471 break;
4472
James Smart92d7f7b2007-06-17 19:56:38 -05004473 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05004474 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05004475 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05004476 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04004477 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4478 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04004479 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05004480
4481 /* Assume no Fabric and go on with discovery.
4482 * Check for outstanding ELS FLOGI to abort.
4483 */
4484
4485 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05004486 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004487
4488 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05004489 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05004490 break;
4491
4492 case LPFC_FABRIC_CFG_LINK:
4493 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4494 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04004495 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4496 "0223 Timeout while waiting for "
4497 "NameServer login\n");
dea31012005-04-17 16:05:31 -05004498 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05004499 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05004500 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04004501 lpfc_els_abort(phba, ndlp);
4502
4503 /* ReStart discovery */
4504 goto restart_disc;
dea31012005-04-17 16:05:31 -05004505
4506 case LPFC_NS_QRY:
4507 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04004508 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4509 "0224 NameServer Query timeout "
4510 "Data: x%x x%x\n",
4511 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004512
James Smart92d7f7b2007-06-17 19:56:38 -05004513 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4514 /* Try it one more time */
4515 vport->fc_ns_retry++;
4516 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4517 vport->fc_ns_retry, 0);
4518 if (rc == 0)
4519 break;
dea31012005-04-17 16:05:31 -05004520 }
James Smart92d7f7b2007-06-17 19:56:38 -05004521 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05004522
James Smart87af33f2007-10-27 13:37:43 -04004523restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05004524 /*
4525 * Discovery is over.
4526 * set port_state to PORT_READY if SLI2.
4527 * cmpl_reg_vpi will set port_state to READY for SLI3.
4528 */
James Smart3772a992009-05-22 14:50:54 -04004529 if (phba->sli_rev < LPFC_SLI_REV4) {
4530 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4531 lpfc_issue_reg_vpi(phba, vport);
4532 else { /* NPIV Not enabled */
4533 lpfc_issue_clear_la(phba, vport);
4534 vport->port_state = LPFC_VPORT_READY;
4535 }
dea31012005-04-17 16:05:31 -05004536 }
4537
4538 /* Setup and issue mailbox INITIALIZE LINK command */
4539 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4540 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004541 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4542 "0206 Device Discovery "
4543 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004544 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004545 break;
4546 }
4547
4548 lpfc_linkdown(phba);
4549 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4550 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004551 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004552 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004553 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004554 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004555 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004556 if (rc == MBX_NOT_FINISHED)
4557 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4558
4559 break;
4560
4561 case LPFC_DISC_AUTH:
4562 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004563 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4564 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004565 lpfc_disc_flush_list(vport);
4566
James Smart92d7f7b2007-06-17 19:56:38 -05004567 /*
4568 * set port_state to PORT_READY if SLI2.
4569 * cmpl_reg_vpi will set port_state to READY for SLI3.
4570 */
James Smart3772a992009-05-22 14:50:54 -04004571 if (phba->sli_rev < LPFC_SLI_REV4) {
4572 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4573 lpfc_issue_reg_vpi(phba, vport);
4574 else { /* NPIV Not enabled */
4575 lpfc_issue_clear_la(phba, vport);
4576 vport->port_state = LPFC_VPORT_READY;
4577 }
dea31012005-04-17 16:05:31 -05004578 }
4579 break;
4580
James Smart2e0fef82007-06-17 19:56:36 -05004581 case LPFC_VPORT_READY:
4582 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004583 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4584 "0231 RSCN timeout Data: x%x "
4585 "x%x\n",
4586 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004587
4588 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004589 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004590
James Smart2e0fef82007-06-17 19:56:36 -05004591 lpfc_els_flush_rscn(vport);
4592 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004593 }
4594 break;
James Smart2e0fef82007-06-17 19:56:36 -05004595
James Smart92d7f7b2007-06-17 19:56:38 -05004596 default:
James Smarte8b62012007-08-02 11:10:09 -04004597 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004598 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004599 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004600 break;
4601 }
4602
4603 switch (phba->link_state) {
4604 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004605 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004606 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4607 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004608 clrlaerr = 1;
4609 break;
4610
James Smart09372822008-01-11 01:52:54 -05004611 case LPFC_LINK_UP:
4612 lpfc_issue_clear_la(phba, vport);
4613 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004614 case LPFC_LINK_UNKNOWN:
4615 case LPFC_WARM_START:
4616 case LPFC_INIT_START:
4617 case LPFC_INIT_MBX_CMDS:
4618 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004619 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004620 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4621 "0230 Unexpected timeout, hba link "
4622 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004623 clrlaerr = 1;
4624 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004625
4626 case LPFC_HBA_READY:
4627 break;
dea31012005-04-17 16:05:31 -05004628 }
4629
4630 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004631 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004632 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004633 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4634 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004635 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004636 }
4637
4638 return;
4639}
4640
dea31012005-04-17 16:05:31 -05004641/*
4642 * This routine handles processing a NameServer REG_LOGIN mailbox
4643 * command upon completion. It is setup in the LPFC_MBOXQ
4644 * as the completion routine when the command is
4645 * handed off to the SLI layer.
4646 */
4647void
James Smart2e0fef82007-06-17 19:56:36 -05004648lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004649{
James Smart04c68492009-05-22 14:52:52 -04004650 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004651 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4652 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4653 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004654
4655 pmb->context1 = NULL;
4656
dea31012005-04-17 16:05:31 -05004657 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004658 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004659 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004660 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004661
James Smart2e0fef82007-06-17 19:56:36 -05004662 /*
4663 * Start issuing Fabric-Device Management Interface (FDMI) command to
4664 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4665 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004666 */
James Smart2e0fef82007-06-17 19:56:36 -05004667
James Smart3de2a652007-08-02 11:09:59 -04004668 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004669 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4670 else
4671 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004672
James Smartfa4066b2008-01-11 01:53:27 -05004673 /* decrement the node reference count held for this callback
4674 * function.
4675 */
James Smart329f9bc2007-04-25 09:53:01 -04004676 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004677 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4678 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004679 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004680
4681 return;
4682}
4683
James Smart685f0bf2007-04-25 09:53:08 -04004684static int
4685lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4686{
4687 uint16_t *rpi = param;
4688
4689 return ndlp->nlp_rpi == *rpi;
4690}
4691
4692static int
4693lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4694{
4695 return memcmp(&ndlp->nlp_portname, param,
4696 sizeof(ndlp->nlp_portname)) == 0;
4697}
4698
Adrian Bunka6ababd2007-11-05 18:07:33 +01004699static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004700__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004701{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004702 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004703
James Smart2e0fef82007-06-17 19:56:36 -05004704 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004705 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004706 return ndlp;
4707 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004708 return NULL;
dea31012005-04-17 16:05:31 -05004709}
4710
James Smart685f0bf2007-04-25 09:53:08 -04004711/*
4712 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004713 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004714 */
4715struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004716__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004717{
James Smart2e0fef82007-06-17 19:56:36 -05004718 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004719}
4720
James Smart488d1462006-03-07 15:02:37 -05004721/*
James Smart685f0bf2007-04-25 09:53:08 -04004722 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004723 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004724 */
4725struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004726lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004727{
James Smart2e0fef82007-06-17 19:56:36 -05004728 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004729 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004730
James Smart2e0fef82007-06-17 19:56:36 -05004731 spin_lock_irq(shost->host_lock);
4732 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4733 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004734 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004735}
4736
dea31012005-04-17 16:05:31 -05004737void
James Smart2e0fef82007-06-17 19:56:36 -05004738lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4739 uint32_t did)
dea31012005-04-17 16:05:31 -05004740{
4741 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004742
4743 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004744 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004745
4746 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4747 "node init: did:x%x",
4748 ndlp->nlp_DID, 0, 0);
4749
dea31012005-04-17 16:05:31 -05004750 return;
4751}
James Smart329f9bc2007-04-25 09:53:01 -04004752
James Smart98c9ea52007-10-27 13:37:33 -04004753/* This routine releases all resources associated with a specifc NPort's ndlp
4754 * and mempool_free's the nodelist.
4755 */
James Smart311464e2007-08-02 11:10:37 -04004756static void
James Smart329f9bc2007-04-25 09:53:01 -04004757lpfc_nlp_release(struct kref *kref)
4758{
James Smarte47c9092008-02-08 18:49:26 -05004759 struct lpfc_hba *phba;
4760 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004761 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4762 kref);
James Smart858c9f62007-06-17 19:56:39 -05004763
4764 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4765 "node release: did:x%x flg:x%x type:x%x",
4766 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4767
James Smarte47c9092008-02-08 18:49:26 -05004768 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4769 "0279 lpfc_nlp_release: ndlp:x%p "
4770 "usgmap:x%x refcnt:%d\n",
4771 (void *)ndlp, ndlp->nlp_usg_map,
4772 atomic_read(&ndlp->kref.refcount));
4773
4774 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004775 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004776
4777 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004778 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004779 spin_lock_irqsave(&phba->ndlp_lock, flags);
4780 NLP_CLR_NODE_ACT(ndlp);
4781 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4782
4783 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004784 if (NLP_CHK_FREE_REQ(ndlp)) {
4785 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004786 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004787 }
James Smart329f9bc2007-04-25 09:53:01 -04004788}
4789
James Smart98c9ea52007-10-27 13:37:33 -04004790/* This routine bumps the reference count for a ndlp structure to ensure
4791 * that one discovery thread won't free a ndlp while another discovery thread
4792 * is using it.
4793 */
James Smart329f9bc2007-04-25 09:53:01 -04004794struct lpfc_nodelist *
4795lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4796{
James Smarte47c9092008-02-08 18:49:26 -05004797 struct lpfc_hba *phba;
4798 unsigned long flags;
4799
James Smart98c9ea52007-10-27 13:37:33 -04004800 if (ndlp) {
4801 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4802 "node get: did:x%x flg:x%x refcnt:x%x",
4803 ndlp->nlp_DID, ndlp->nlp_flag,
4804 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004805 /* The check of ndlp usage to prevent incrementing the
4806 * ndlp reference count that is in the process of being
4807 * released.
4808 */
James Smarta257bf92009-04-06 18:48:10 -04004809 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004810 spin_lock_irqsave(&phba->ndlp_lock, flags);
4811 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4812 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4813 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4814 "0276 lpfc_nlp_get: ndlp:x%p "
4815 "usgmap:x%x refcnt:%d\n",
4816 (void *)ndlp, ndlp->nlp_usg_map,
4817 atomic_read(&ndlp->kref.refcount));
4818 return NULL;
4819 } else
4820 kref_get(&ndlp->kref);
4821 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004822 }
James Smart329f9bc2007-04-25 09:53:01 -04004823 return ndlp;
4824}
4825
James Smart98c9ea52007-10-27 13:37:33 -04004826/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004827 * count goes to 0, this indicates the the associated nodelist should be
4828 * freed. Returning 1 indicates the ndlp resource has been released; on the
4829 * other hand, returning 0 indicates the ndlp resource has not been released
4830 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004831 */
James Smart329f9bc2007-04-25 09:53:01 -04004832int
4833lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4834{
James Smarte47c9092008-02-08 18:49:26 -05004835 struct lpfc_hba *phba;
4836 unsigned long flags;
4837
4838 if (!ndlp)
4839 return 1;
4840
4841 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4842 "node put: did:x%x flg:x%x refcnt:x%x",
4843 ndlp->nlp_DID, ndlp->nlp_flag,
4844 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004845 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004846 spin_lock_irqsave(&phba->ndlp_lock, flags);
4847 /* Check the ndlp memory free acknowledge flag to avoid the
4848 * possible race condition that kref_put got invoked again
4849 * after previous one has done ndlp memory free.
4850 */
4851 if (NLP_CHK_FREE_ACK(ndlp)) {
4852 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4853 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4854 "0274 lpfc_nlp_put: ndlp:x%p "
4855 "usgmap:x%x refcnt:%d\n",
4856 (void *)ndlp, ndlp->nlp_usg_map,
4857 atomic_read(&ndlp->kref.refcount));
4858 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004859 }
James Smarte47c9092008-02-08 18:49:26 -05004860 /* Check the ndlp inactivate log flag to avoid the possible
4861 * race condition that kref_put got invoked again after ndlp
4862 * is already in inactivating state.
4863 */
4864 if (NLP_CHK_IACT_REQ(ndlp)) {
4865 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4866 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4867 "0275 lpfc_nlp_put: ndlp:x%p "
4868 "usgmap:x%x refcnt:%d\n",
4869 (void *)ndlp, ndlp->nlp_usg_map,
4870 atomic_read(&ndlp->kref.refcount));
4871 return 1;
4872 }
4873 /* For last put, mark the ndlp usage flags to make sure no
4874 * other kref_get and kref_put on the same ndlp shall get
4875 * in between the process when the final kref_put has been
4876 * invoked on this ndlp.
4877 */
4878 if (atomic_read(&ndlp->kref.refcount) == 1) {
4879 /* Indicate ndlp is put to inactive state. */
4880 NLP_SET_IACT_REQ(ndlp);
4881 /* Acknowledge ndlp memory free has been seen. */
4882 if (NLP_CHK_FREE_REQ(ndlp))
4883 NLP_SET_FREE_ACK(ndlp);
4884 }
4885 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4886 /* Note, the kref_put returns 1 when decrementing a reference
4887 * count that was 1, it invokes the release callback function,
4888 * but it still left the reference count as 1 (not actually
4889 * performs the last decrementation). Otherwise, it actually
4890 * decrements the reference count and returns 0.
4891 */
4892 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004893}
James Smart98c9ea52007-10-27 13:37:33 -04004894
4895/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004896 * by any other discovery thread. This routine returns 1 if the
4897 * ndlp has been freed. A return value of 0 indicates the ndlp is
4898 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004899 */
4900int
4901lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4902{
4903 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4904 "node not used: did:x%x flg:x%x refcnt:x%x",
4905 ndlp->nlp_DID, ndlp->nlp_flag,
4906 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004907 if (atomic_read(&ndlp->kref.refcount) == 1)
4908 if (lpfc_nlp_put(ndlp))
4909 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004910 return 0;
4911}
James Smart6fb120a2009-05-22 14:52:59 -04004912
4913/**
4914 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4915 * @phba: Pointer to hba context object.
4916 *
4917 * This function iterate through all FC nodes associated
4918 * will all vports to check if there is any node with
4919 * fc_rports associated with it. If there is an fc_rport
4920 * associated with the node, then the node is either in
4921 * discovered state or its devloss_timer is pending.
4922 */
4923static int
4924lpfc_fcf_inuse(struct lpfc_hba *phba)
4925{
4926 struct lpfc_vport **vports;
4927 int i, ret = 0;
4928 struct lpfc_nodelist *ndlp;
4929 struct Scsi_Host *shost;
4930
4931 vports = lpfc_create_vport_work_array(phba);
4932
4933 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4934 shost = lpfc_shost_from_vport(vports[i]);
4935 spin_lock_irq(shost->host_lock);
4936 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4937 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4938 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4939 ret = 1;
4940 spin_unlock_irq(shost->host_lock);
4941 goto out;
James Smart891478a2009-11-18 15:40:23 -05004942 } else {
4943 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4944 "2624 RPI %x DID %x flg %x still "
4945 "logged in\n",
4946 ndlp->nlp_rpi, ndlp->nlp_DID,
4947 ndlp->nlp_flag);
4948 if (ndlp->nlp_flag & NLP_RPI_VALID)
4949 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04004950 }
4951 }
4952 spin_unlock_irq(shost->host_lock);
4953 }
4954out:
4955 lpfc_destroy_vport_work_array(phba, vports);
4956 return ret;
4957}
4958
4959/**
4960 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4961 * @phba: Pointer to hba context object.
4962 * @mboxq: Pointer to mailbox object.
4963 *
4964 * This function frees memory associated with the mailbox command.
4965 */
4966static void
4967lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4968{
4969 struct lpfc_vport *vport = mboxq->vport;
4970
4971 if (mboxq->u.mb.mbxStatus) {
4972 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4973 "2555 UNREG_VFI mbxStatus error x%x "
4974 "HBA state x%x\n",
4975 mboxq->u.mb.mbxStatus, vport->port_state);
4976 }
4977 mempool_free(mboxq, phba->mbox_mem_pool);
4978 return;
4979}
4980
4981/**
4982 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4983 * @phba: Pointer to hba context object.
4984 * @mboxq: Pointer to mailbox object.
4985 *
4986 * This function frees memory associated with the mailbox command.
4987 */
4988static void
4989lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4990{
4991 struct lpfc_vport *vport = mboxq->vport;
4992
4993 if (mboxq->u.mb.mbxStatus) {
4994 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4995 "2550 UNREG_FCFI mbxStatus error x%x "
4996 "HBA state x%x\n",
4997 mboxq->u.mb.mbxStatus, vport->port_state);
4998 }
4999 mempool_free(mboxq, phba->mbox_mem_pool);
5000 return;
5001}
5002
5003/**
James Smartecfd03c2010-02-12 14:41:27 -05005004 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04005005 * @phba: Pointer to hba context object.
5006 *
James Smartecfd03c2010-02-12 14:41:27 -05005007 * This function prepare the HBA for unregistering the currently registered
5008 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5009 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04005010 */
James Smartecfd03c2010-02-12 14:41:27 -05005011int
5012lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04005013{
5014 LPFC_MBOXQ_t *mbox;
James Smart6fb120a2009-05-22 14:52:59 -04005015 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05005016 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05005017 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05005018 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04005019
James Smartecfd03c2010-02-12 14:41:27 -05005020 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04005021 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05005022 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005023
James Smart4d9ab992009-10-02 15:16:39 -04005024 /* At this point, all discovery is aborted */
5025 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04005026
5027 /* Unregister VPIs */
5028 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05005029 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04005030 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05005031 /* Stop FLOGI/FDISC retries */
5032 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5033 if (ndlp)
5034 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005035 lpfc_cleanup_pending_mbox(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04005036 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05005037 shost = lpfc_shost_from_vport(vports[i]);
5038 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05005039 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05005040 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005041 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005042 }
5043 lpfc_destroy_vport_work_array(phba, vports);
5044
James Smart695a8142010-01-26 23:08:03 -05005045 /* Cleanup any outstanding ELS commands */
5046 lpfc_els_flush_all_cmd(phba);
5047
James Smart6fb120a2009-05-22 14:52:59 -04005048 /* Unregister VFI */
5049 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5050 if (!mbox) {
5051 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005052 "2556 UNREG_VFI mbox allocation failed"
5053 "HBA state x%x\n", phba->pport->port_state);
5054 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005055 }
5056
James Smart6669f9b2009-10-02 15:16:45 -04005057 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04005058 mbox->vport = phba->pport;
5059 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
5060
5061 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5062 if (rc == MBX_NOT_FINISHED) {
5063 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005064 "2557 UNREG_VFI issue mbox failed rc x%x "
5065 "HBA state x%x\n",
5066 rc, phba->pport->port_state);
James Smart6fb120a2009-05-22 14:52:59 -04005067 mempool_free(mbox, phba->mbox_mem_pool);
James Smartecfd03c2010-02-12 14:41:27 -05005068 return -EIO;
James Smart6fb120a2009-05-22 14:52:59 -04005069 }
5070
James Smart72100cc2010-02-12 14:43:01 -05005071 shost = lpfc_shost_from_vport(phba->pport);
5072 spin_lock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005073 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005074 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005075
James Smartecfd03c2010-02-12 14:41:27 -05005076 return 0;
5077}
5078
5079/**
5080 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5081 * @phba: Pointer to hba context object.
5082 *
5083 * This function issues synchronous unregister FCF mailbox command to HBA to
5084 * unregister the currently registered FCF record. The driver does not reset
5085 * the driver FCF usage state flags.
5086 *
5087 * Return 0 if successfully issued, none-zero otherwise.
5088 */
5089int
5090lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5091{
5092 LPFC_MBOXQ_t *mbox;
5093 int rc;
5094
James Smart6fb120a2009-05-22 14:52:59 -04005095 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5096 if (!mbox) {
5097 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005098 "2551 UNREG_FCFI mbox allocation failed"
5099 "HBA state x%x\n", phba->pport->port_state);
5100 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005101 }
James Smart6fb120a2009-05-22 14:52:59 -04005102 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5103 mbox->vport = phba->pport;
5104 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5105 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5106
5107 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005108 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5109 "2552 Unregister FCFI command failed rc x%x "
5110 "HBA state x%x\n",
5111 rc, phba->pport->port_state);
5112 return -EINVAL;
5113 }
5114 return 0;
5115}
5116
5117/**
5118 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5119 * @phba: Pointer to hba context object.
5120 *
5121 * This function unregisters the currently reigstered FCF. This function
5122 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5123 */
5124void
5125lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5126{
5127 int rc;
5128
5129 /* Preparation for unregistering fcf */
5130 rc = lpfc_unregister_fcf_prep(phba);
5131 if (rc) {
5132 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5133 "2748 Failed to prepare for unregistering "
5134 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005135 return;
5136 }
5137
James Smartecfd03c2010-02-12 14:41:27 -05005138 /* Now, unregister FCF record and reset HBA FCF state */
5139 rc = lpfc_sli4_unregister_fcf(phba);
5140 if (rc)
5141 return;
5142 /* Reset HBA FCF states after successful unregister FCF */
5143 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005144 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005145
5146 /*
5147 * If driver is not unloading, check if there is any other
5148 * FCF record that can be used for discovery.
5149 */
5150 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005151 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005152 return;
5153
James Smart0c9ab6f2010-02-26 14:15:57 -05005154 /* This is considered as the initial FCF discovery scan */
5155 spin_lock_irq(&phba->hbalock);
5156 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5157 spin_unlock_irq(&phba->hbalock);
5158 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005159
James Smart0c9ab6f2010-02-26 14:15:57 -05005160 if (rc) {
5161 spin_lock_irq(&phba->hbalock);
5162 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5163 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005164 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005165 "2553 lpfc_unregister_unused_fcf failed "
5166 "to read FCF record HBA state x%x\n",
5167 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005168 }
James Smartecfd03c2010-02-12 14:41:27 -05005169}
5170
5171/**
5172 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5173 * @phba: Pointer to hba context object.
5174 *
5175 * This function just unregisters the currently reigstered FCF. It does not
5176 * try to find another FCF for discovery.
5177 */
5178void
5179lpfc_unregister_fcf(struct lpfc_hba *phba)
5180{
5181 int rc;
5182
5183 /* Preparation for unregistering fcf */
5184 rc = lpfc_unregister_fcf_prep(phba);
5185 if (rc) {
5186 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5187 "2749 Failed to prepare for unregistering "
5188 "HBA's FCF record: rc=%d\n", rc);
5189 return;
5190 }
5191
5192 /* Now, unregister FCF record and reset HBA FCF state */
5193 rc = lpfc_sli4_unregister_fcf(phba);
5194 if (rc)
5195 return;
5196 /* Set proper HBA FCF states after successful unregister FCF */
5197 spin_lock_irq(&phba->hbalock);
5198 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
5199 spin_unlock_irq(&phba->hbalock);
5200}
5201
5202/**
5203 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
5204 * @phba: Pointer to hba context object.
5205 *
5206 * This function check if there are any connected remote port for the FCF and
5207 * if all the devices are disconnected, this function unregister FCFI.
5208 * This function also tries to use another FCF for discovery.
5209 */
5210void
5211lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
5212{
5213 /*
5214 * If HBA is not running in FIP mode or if HBA does not support
5215 * FCoE or if FCF is not registered, do nothing.
5216 */
5217 spin_lock_irq(&phba->hbalock);
5218 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
5219 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
5220 !(phba->hba_flag & HBA_FIP_SUPPORT)) {
5221 spin_unlock_irq(&phba->hbalock);
5222 return;
5223 }
5224 spin_unlock_irq(&phba->hbalock);
5225
5226 if (lpfc_fcf_inuse(phba))
5227 return;
5228
5229 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005230}
5231
5232/**
5233 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
5234 * @phba: Pointer to hba context object.
5235 * @buff: Buffer containing the FCF connection table as in the config
5236 * region.
5237 * This function create driver data structure for the FCF connection
5238 * record table read from config region 23.
5239 */
5240static void
5241lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
5242 uint8_t *buff)
5243{
5244 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5245 struct lpfc_fcf_conn_hdr *conn_hdr;
5246 struct lpfc_fcf_conn_rec *conn_rec;
5247 uint32_t record_count;
5248 int i;
5249
5250 /* Free the current connect table */
5251 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005252 &phba->fcf_conn_rec_list, list) {
5253 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04005254 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005255 }
James Smart6fb120a2009-05-22 14:52:59 -04005256
5257 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
5258 record_count = conn_hdr->length * sizeof(uint32_t)/
5259 sizeof(struct lpfc_fcf_conn_rec);
5260
5261 conn_rec = (struct lpfc_fcf_conn_rec *)
5262 (buff + sizeof(struct lpfc_fcf_conn_hdr));
5263
5264 for (i = 0; i < record_count; i++) {
5265 if (!(conn_rec[i].flags & FCFCNCT_VALID))
5266 continue;
5267 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
5268 GFP_KERNEL);
5269 if (!conn_entry) {
5270 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5271 "2566 Failed to allocate connection"
5272 " table entry\n");
5273 return;
5274 }
5275
5276 memcpy(&conn_entry->conn_rec, &conn_rec[i],
5277 sizeof(struct lpfc_fcf_conn_rec));
5278 conn_entry->conn_rec.vlan_tag =
5279 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
5280 conn_entry->conn_rec.flags =
5281 le16_to_cpu(conn_entry->conn_rec.flags);
5282 list_add_tail(&conn_entry->list,
5283 &phba->fcf_conn_rec_list);
5284 }
5285}
5286
5287/**
5288 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
5289 * @phba: Pointer to hba context object.
5290 * @buff: Buffer containing the FCoE parameter data structure.
5291 *
5292 * This function update driver data structure with config
5293 * parameters read from config region 23.
5294 */
5295static void
5296lpfc_read_fcoe_param(struct lpfc_hba *phba,
5297 uint8_t *buff)
5298{
5299 struct lpfc_fip_param_hdr *fcoe_param_hdr;
5300 struct lpfc_fcoe_params *fcoe_param;
5301
5302 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
5303 buff;
5304 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04005305 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04005306
5307 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
5308 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
5309 return;
5310
James Smart6fb120a2009-05-22 14:52:59 -04005311 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
5312 phba->valid_vlan = 1;
5313 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
5314 0xFFF;
5315 }
5316
5317 phba->fc_map[0] = fcoe_param->fc_map[0];
5318 phba->fc_map[1] = fcoe_param->fc_map[1];
5319 phba->fc_map[2] = fcoe_param->fc_map[2];
5320 return;
5321}
5322
5323/**
5324 * lpfc_get_rec_conf23 - Get a record type in config region data.
5325 * @buff: Buffer containing config region 23 data.
5326 * @size: Size of the data buffer.
5327 * @rec_type: Record type to be searched.
5328 *
5329 * This function searches config region data to find the begining
5330 * of the record specified by record_type. If record found, this
5331 * function return pointer to the record else return NULL.
5332 */
5333static uint8_t *
5334lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
5335{
5336 uint32_t offset = 0, rec_length;
5337
5338 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
5339 (size < sizeof(uint32_t)))
5340 return NULL;
5341
5342 rec_length = buff[offset + 1];
5343
5344 /*
5345 * One TLV record has one word header and number of data words
5346 * specified in the rec_length field of the record header.
5347 */
5348 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
5349 <= size) {
5350 if (buff[offset] == rec_type)
5351 return &buff[offset];
5352
5353 if (buff[offset] == LPFC_REGION23_LAST_REC)
5354 return NULL;
5355
5356 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
5357 rec_length = buff[offset + 1];
5358 }
5359 return NULL;
5360}
5361
5362/**
5363 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
5364 * @phba: Pointer to lpfc_hba data structure.
5365 * @buff: Buffer containing config region 23 data.
5366 * @size: Size of the data buffer.
5367 *
5368 * This fuction parse the FCoE config parameters in config region 23 and
5369 * populate driver data structure with the parameters.
5370 */
5371void
5372lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
5373 uint8_t *buff,
5374 uint32_t size)
5375{
5376 uint32_t offset = 0, rec_length;
5377 uint8_t *rec_ptr;
5378
5379 /*
5380 * If data size is less than 2 words signature and version cannot be
5381 * verified.
5382 */
5383 if (size < 2*sizeof(uint32_t))
5384 return;
5385
5386 /* Check the region signature first */
5387 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
5388 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5389 "2567 Config region 23 has bad signature\n");
5390 return;
5391 }
5392
5393 offset += 4;
5394
5395 /* Check the data structure version */
5396 if (buff[offset] != LPFC_REGION23_VERSION) {
5397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5398 "2568 Config region 23 has bad version\n");
5399 return;
5400 }
5401 offset += 4;
5402
5403 rec_length = buff[offset + 1];
5404
5405 /* Read FCoE param record */
5406 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5407 size - offset, FCOE_PARAM_TYPE);
5408 if (rec_ptr)
5409 lpfc_read_fcoe_param(phba, rec_ptr);
5410
5411 /* Read FCF connection table */
5412 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5413 size - offset, FCOE_CONN_TBL_TYPE);
5414 if (rec_ptr)
5415 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5416
5417}