blob: c555e3b7f202cf25dc8683b7363a2a1a0d9dcd50 [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>
23#include <linux/pci.h>
24#include <linux/kthread.h>
25#include <linux/interrupt.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_device.h>
29#include <scsi/scsi_host.h>
30#include <scsi/scsi_transport_fc.h>
31
James Smartda0436e2009-05-22 14:51:39 -040032#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050033#include "lpfc_hw.h"
James Smartea2151b2008-09-07 11:52:10 -040034#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050035#include "lpfc_disc.h"
36#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_sli4.h"
dea31012005-04-17 16:05:31 -050038#include "lpfc_scsi.h"
39#include "lpfc.h"
40#include "lpfc_logmsg.h"
41#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050042#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050043#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050044
45/* AlpaArray for assignment of scsid for scan-down and bind_method */
46static uint8_t lpfcAlpaArray[] = {
47 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
48 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
49 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
50 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
51 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
52 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
53 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
54 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
55 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
56 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
57 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
58 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
59 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
60};
61
James Smart2e0fef82007-06-17 19:56:36 -050062static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010063static void lpfc_disc_flush_list(struct lpfc_vport *vport);
James Smart32b97932009-07-19 10:01:21 -040064static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
dea31012005-04-17 16:05:31 -050065
James Smartc01f3202006-08-18 17:47:08 -040066void
67lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050068{
James Smartc01f3202006-08-18 17:47:08 -040069 struct lpfc_rport_data *rdata;
70 struct lpfc_nodelist * ndlp;
71 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050072
James Smartc01f3202006-08-18 17:47:08 -040073 rdata = rport->dd_data;
74 ndlp = rdata->pnode;
75
James Smart58da1ff2008-04-07 10:15:56 -040076 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040077 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
78 printk(KERN_ERR "Cannot find remote node"
79 " to terminate I/O Data x%x\n",
80 rport->port_id);
dea31012005-04-17 16:05:31 -050081 return;
82 }
83
James Smarta257bf92009-04-06 18:48:10 -040084 phba = ndlp->phba;
James Smart1a169682006-03-07 15:04:06 -050085
James Smart858c9f62007-06-17 19:56:39 -050086 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
87 "rport terminate: sid:x%x did:x%x flg:x%x",
88 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
89
James Smartc01f3202006-08-18 17:47:08 -040090 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040091 lpfc_sli_abort_iocb(ndlp->vport,
92 &phba->sli.ring[phba->sli.fcp_ring],
93 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040094 }
James Smartc01f3202006-08-18 17:47:08 -040095}
96
97/*
98 * This function will be called when dev_loss_tmo fire.
99 */
100void
101lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
102{
103 struct lpfc_rport_data *rdata;
104 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500105 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500106 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500107 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400108 int put_node;
109 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400110
111 rdata = rport->dd_data;
112 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400113 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400114 return;
James Smartc01f3202006-08-18 17:47:08 -0400115
James Smart858c9f62007-06-17 19:56:39 -0500116 vport = ndlp->vport;
117 phba = vport->phba;
118
119 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
120 "rport devlosscb: sid:x%x did:x%x flg:x%x",
121 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
122
James Smarta8adb832007-10-27 13:37:53 -0400123 /* Don't defer this if we are in the process of deleting the vport
124 * or unloading the driver. The unload will cleanup the node
125 * appropriately we just need to cleanup the ndlp rport info here.
126 */
127 if (vport->load_flag & FC_UNLOADING) {
128 put_node = rdata->pnode != NULL;
129 put_rport = ndlp->rport != NULL;
130 rdata->pnode = NULL;
131 ndlp->rport = NULL;
132 if (put_node)
133 lpfc_nlp_put(ndlp);
134 if (put_rport)
135 put_device(&rport->dev);
136 return;
137 }
138
139 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
140 return;
141
James Smart858c9f62007-06-17 19:56:39 -0500142 evtp = &ndlp->dev_loss_evt;
143
144 if (!list_empty(&evtp->evt_listp))
145 return;
146
147 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500148 /* We need to hold the node by incrementing the reference
149 * count until this queued work is done
150 */
151 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart5e9d9b82008-06-14 22:52:53 -0400152 if (evtp->evt_arg1) {
153 evtp->evt = LPFC_EVT_DEV_LOSS;
154 list_add_tail(&evtp->evt_listp, &phba->work_list);
155 lpfc_worker_wake_up(phba);
156 }
James Smart858c9f62007-06-17 19:56:39 -0500157 spin_unlock_irq(&phba->hbalock);
158
James Smart858c9f62007-06-17 19:56:39 -0500159 return;
160}
161
162/*
163 * This function is called from the worker thread when dev_loss_tmo
164 * expire.
165 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100166static void
James Smart858c9f62007-06-17 19:56:39 -0500167lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
168{
169 struct lpfc_rport_data *rdata;
170 struct fc_rport *rport;
171 struct lpfc_vport *vport;
172 struct lpfc_hba *phba;
173 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400174 int put_node;
175 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500176 int warn_on = 0;
177
178 rport = ndlp->rport;
179
180 if (!rport)
181 return;
182
183 rdata = rport->dd_data;
184 name = (uint8_t *) &ndlp->nlp_portname;
185 vport = ndlp->vport;
186 phba = vport->phba;
187
188 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
189 "rport devlosstmo:did:x%x type:x%x id:x%x",
190 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
191
James Smarta8adb832007-10-27 13:37:53 -0400192 /* Don't defer this if we are in the process of deleting the vport
193 * or unloading the driver. The unload will cleanup the node
194 * appropriately we just need to cleanup the ndlp rport info here.
195 */
196 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500197 if (ndlp->nlp_sid != NLP_NO_SID) {
198 /* flush the target */
199 lpfc_sli_abort_iocb(vport,
200 &phba->sli.ring[phba->sli.fcp_ring],
201 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
202 }
James Smarta8adb832007-10-27 13:37:53 -0400203 put_node = rdata->pnode != NULL;
204 put_rport = ndlp->rport != NULL;
205 rdata->pnode = NULL;
206 ndlp->rport = NULL;
207 if (put_node)
208 lpfc_nlp_put(ndlp);
209 if (put_rport)
210 put_device(&rport->dev);
211 return;
212 }
213
James Smartd7c255b2008-08-24 21:50:00 -0400214 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
215 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
216 "0284 Devloss timeout Ignored on "
217 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
218 "NPort x%x\n",
219 *name, *(name+1), *(name+2), *(name+3),
220 *(name+4), *(name+5), *(name+6), *(name+7),
221 ndlp->nlp_DID);
James Smart858c9f62007-06-17 19:56:39 -0500222 return;
James Smartd7c255b2008-08-24 21:50:00 -0400223 }
James Smart858c9f62007-06-17 19:56:39 -0500224
James Smart92d7f7b2007-06-17 19:56:38 -0500225 if (ndlp->nlp_type & NLP_FABRIC) {
226 /* We will clean up these Nodes in linkup */
227 put_node = rdata->pnode != NULL;
228 put_rport = ndlp->rport != NULL;
229 rdata->pnode = NULL;
230 ndlp->rport = NULL;
231 if (put_node)
232 lpfc_nlp_put(ndlp);
233 if (put_rport)
234 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400235 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500236 }
James Smart82085712007-04-25 09:52:41 -0400237
dea31012005-04-17 16:05:31 -0500238 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400239 warn_on = 1;
dea31012005-04-17 16:05:31 -0500240 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400241 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
242 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500243 }
James Smartc01f3202006-08-18 17:47:08 -0400244
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400245 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400246 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
247 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400248 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
249 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400250 *name, *(name+1), *(name+2), *(name+3),
251 *(name+4), *(name+5), *(name+6), *(name+7),
252 ndlp->nlp_DID, ndlp->nlp_flag,
253 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400254 } else {
James Smarte8b62012007-08-02 11:10:09 -0400255 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
256 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400257 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
258 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400259 *name, *(name+1), *(name+2), *(name+3),
260 *(name+4), *(name+5), *(name+6), *(name+7),
261 ndlp->nlp_DID, ndlp->nlp_flag,
262 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400263 }
264
James Smart87af33f2007-10-27 13:37:43 -0400265 put_node = rdata->pnode != NULL;
266 put_rport = ndlp->rport != NULL;
267 rdata->pnode = NULL;
268 ndlp->rport = NULL;
269 if (put_node)
270 lpfc_nlp_put(ndlp);
271 if (put_rport)
272 put_device(&rport->dev);
273
James Smart2e0fef82007-06-17 19:56:36 -0500274 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400275 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400276 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smarte47c9092008-02-08 18:49:26 -0500277 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
James Smart2e0fef82007-06-17 19:56:36 -0500278 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400279
280 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500281}
James Smartc01f3202006-08-18 17:47:08 -0400282
James Smartea2151b2008-09-07 11:52:10 -0400283/**
James Smart3621a712009-04-06 18:47:14 -0400284 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400285 * @phba: Pointer to hba context object.
286 *
287 * This function is called from the functions which need to post
288 * events from interrupt context. This function allocates data
289 * structure required for posting event. It also keeps track of
290 * number of events pending and prevent event storm when there are
291 * too many events.
292 **/
293struct lpfc_fast_path_event *
294lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
295 struct lpfc_fast_path_event *ret;
296
297 /* If there are lot of fast event do not exhaust memory due to this */
298 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
299 return NULL;
300
301 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
302 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400303 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400304 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400305 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
306 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
307 }
James Smartea2151b2008-09-07 11:52:10 -0400308 return ret;
309}
310
311/**
James Smart3621a712009-04-06 18:47:14 -0400312 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400313 * @phba: Pointer to hba context object.
314 * @evt: Event object which need to be freed.
315 *
316 * This function frees the data structure required for posting
317 * events.
318 **/
319void
320lpfc_free_fast_evt(struct lpfc_hba *phba,
321 struct lpfc_fast_path_event *evt) {
322
323 atomic_dec(&phba->fast_event_count);
324 kfree(evt);
325}
326
327/**
James Smart3621a712009-04-06 18:47:14 -0400328 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400329 * @phba: Pointer to hba context object.
330 * @evtp: Event data structure.
331 *
332 * This function is called from worker thread, when the interrupt
333 * context need to post an event. This function posts the event
334 * to fc transport netlink interface.
335 **/
336static void
337lpfc_send_fastpath_evt(struct lpfc_hba *phba,
338 struct lpfc_work_evt *evtp)
339{
340 unsigned long evt_category, evt_sub_category;
341 struct lpfc_fast_path_event *fast_evt_data;
342 char *evt_data;
343 uint32_t evt_data_size;
344 struct Scsi_Host *shost;
345
346 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
347 work_evt);
348
349 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
350 evt_sub_category = (unsigned long) fast_evt_data->un.
351 fabric_evt.subcategory;
352 shost = lpfc_shost_from_vport(fast_evt_data->vport);
353 if (evt_category == FC_REG_FABRIC_EVENT) {
354 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
355 evt_data = (char *) &fast_evt_data->un.read_check_error;
356 evt_data_size = sizeof(fast_evt_data->un.
357 read_check_error);
358 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500359 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400360 evt_data = (char *) &fast_evt_data->un.fabric_evt;
361 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
362 } else {
363 lpfc_free_fast_evt(phba, fast_evt_data);
364 return;
365 }
366 } else if (evt_category == FC_REG_SCSI_EVENT) {
367 switch (evt_sub_category) {
368 case LPFC_EVENT_QFULL:
369 case LPFC_EVENT_DEVBSY:
370 evt_data = (char *) &fast_evt_data->un.scsi_evt;
371 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
372 break;
373 case LPFC_EVENT_CHECK_COND:
374 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
375 evt_data_size = sizeof(fast_evt_data->un.
376 check_cond_evt);
377 break;
378 case LPFC_EVENT_VARQUEDEPTH:
379 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
380 evt_data_size = sizeof(fast_evt_data->un.
381 queue_depth_evt);
382 break;
383 default:
384 lpfc_free_fast_evt(phba, fast_evt_data);
385 return;
386 }
387 } else {
388 lpfc_free_fast_evt(phba, fast_evt_data);
389 return;
390 }
391
392 fc_host_post_vendor_event(shost,
393 fc_get_event_number(),
394 evt_data_size,
395 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500396 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400397
398 lpfc_free_fast_evt(phba, fast_evt_data);
399 return;
400}
401
dea31012005-04-17 16:05:31 -0500402static void
James Smart2e0fef82007-06-17 19:56:36 -0500403lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500404{
405 struct lpfc_work_evt *evtp = NULL;
406 struct lpfc_nodelist *ndlp;
407 int free_evt;
408
James Smart2e0fef82007-06-17 19:56:36 -0500409 spin_lock_irq(&phba->hbalock);
410 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500411 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
412 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500413 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500414 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500415 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500416 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500417 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500418 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500419 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500420 /* decrement the node reference count held
421 * for this queued work
422 */
423 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500424 break;
James Smart858c9f62007-06-17 19:56:39 -0500425 case LPFC_EVT_DEV_LOSS:
426 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500427 lpfc_dev_loss_tmo_handler(ndlp);
428 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500429 /* decrement the node reference count held for
430 * this queued work
431 */
James Smart858c9f62007-06-17 19:56:39 -0500432 lpfc_nlp_put(ndlp);
433 break;
dea31012005-04-17 16:05:31 -0500434 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500435 if (phba->link_state < LPFC_LINK_DOWN)
436 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500437 else
James Smart2e0fef82007-06-17 19:56:36 -0500438 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500439 complete((struct completion *)(evtp->evt_arg2));
440 break;
James Smart46fa3112007-04-25 09:51:45 -0400441 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500442 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400443 lpfc_offline_prep(phba);
444 *(int *)(evtp->evt_arg1) = 0;
445 complete((struct completion *)(evtp->evt_arg2));
446 break;
447 case LPFC_EVT_OFFLINE:
448 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500449 lpfc_sli_brdrestart(phba);
450 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400451 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
452 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500453 complete((struct completion *)(evtp->evt_arg2));
454 break;
455 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400456 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500457 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500458 lpfc_sli_brdreset(phba);
459 lpfc_hba_down_post(phba);
460 *(int *)(evtp->evt_arg1) =
461 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400462 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500463 complete((struct completion *)(evtp->evt_arg2));
464 break;
465 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400466 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500467 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500468 = (phba->pport->stopped)
469 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400470 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500471 complete((struct completion *)(evtp->evt_arg2));
472 break;
James Smartea2151b2008-09-07 11:52:10 -0400473 case LPFC_EVT_FASTPATH_MGMT_EVT:
474 lpfc_send_fastpath_evt(phba, evtp);
475 free_evt = 0;
476 break;
dea31012005-04-17 16:05:31 -0500477 }
478 if (free_evt)
479 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500480 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500481 }
James Smart2e0fef82007-06-17 19:56:36 -0500482 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500483
484}
485
James Smart311464e2007-08-02 11:10:37 -0400486static void
James Smart2e0fef82007-06-17 19:56:36 -0500487lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500488{
489 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500490 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400491 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400492 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400493 int i;
dea31012005-04-17 16:05:31 -0500494
James Smart2e0fef82007-06-17 19:56:36 -0500495 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500496 ha_copy = phba->work_ha;
497 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500498 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500499
James Smartda0436e2009-05-22 14:51:39 -0400500 /* First, try to post the next mailbox command to SLI4 device */
501 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
502 lpfc_sli4_post_async_mbox(phba);
503
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500504 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400505 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500506 lpfc_handle_eratt(phba);
507
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500508 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500509 lpfc_sli_handle_mb_event(phba);
510
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500511 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500512 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400513
James Smartda0436e2009-05-22 14:51:39 -0400514 /* Process SLI4 events */
515 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
516 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
517 lpfc_sli4_fcp_xri_abort_event_proc(phba);
518 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
519 lpfc_sli4_els_xri_abort_event_proc(phba);
520 if (phba->hba_flag & ASYNC_EVENT)
521 lpfc_sli4_async_event_proc(phba);
522 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
523 spin_lock_irq(&phba->hbalock);
524 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
525 spin_unlock_irq(&phba->hbalock);
526 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
527 }
James Smartecfd03c2010-02-12 14:41:27 -0500528 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
529 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400530 }
531
James Smart549e55c2007-08-02 11:09:51 -0400532 vports = lpfc_create_vport_work_array(phba);
533 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400534 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400535 /*
536 * We could have no vports in array if unloading, so if
537 * this happens then just use the pport
538 */
539 if (vports[i] == NULL && i == 0)
540 vport = phba->pport;
541 else
542 vport = vports[i];
543 if (vport == NULL)
544 break;
James Smart58da1ff2008-04-07 10:15:56 -0400545 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400546 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400547 vport->work_port_events &= ~work_port_events;
548 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400549 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400550 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400551 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400552 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400553 if (work_port_events & WORKER_HB_TMO)
554 lpfc_hb_timeout_handler(phba);
555 if (work_port_events & WORKER_MBOX_TMO)
556 lpfc_mbox_timeout_handler(phba);
557 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
558 lpfc_unblock_fabric_iocbs(phba);
559 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400560 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400561 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
562 lpfc_ramp_down_queue_handler(phba);
563 if (work_port_events & WORKER_RAMP_UP_QUEUE)
564 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500565 }
James Smart09372822008-01-11 01:52:54 -0500566 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500567
James Smart858c9f62007-06-17 19:56:39 -0500568 pring = &phba->sli.ring[LPFC_ELS_RING];
569 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
570 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400571 if ((status & HA_RXMASK) ||
572 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400573 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400574 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500575 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400576 /* Set the lpfc data pending flag */
577 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500578 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400579 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500580 lpfc_sli_handle_slow_ring_event(phba, pring,
581 (status &
582 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500583 }
584 /*
585 * Turn on Ring interrupts
586 */
James Smart3772a992009-05-22 14:50:54 -0400587 if (phba->sli_rev <= LPFC_SLI_REV3) {
588 spin_lock_irq(&phba->hbalock);
589 control = readl(phba->HCregaddr);
590 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
591 lpfc_debugfs_slow_ring_trc(phba,
592 "WRK Enable ring: cntl:x%x hacopy:x%x",
593 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400594
James Smart3772a992009-05-22 14:50:54 -0400595 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
596 writel(control, phba->HCregaddr);
597 readl(phba->HCregaddr); /* flush */
598 } else {
599 lpfc_debugfs_slow_ring_trc(phba,
600 "WRK Ring ok: cntl:x%x hacopy:x%x",
601 control, ha_copy, 0);
602 }
603 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500604 }
605 }
James Smart2e0fef82007-06-17 19:56:36 -0500606 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500607}
608
dea31012005-04-17 16:05:31 -0500609int
610lpfc_do_work(void *p)
611{
612 struct lpfc_hba *phba = p;
613 int rc;
dea31012005-04-17 16:05:31 -0500614
615 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400616 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500617
James Smart3a55b532008-12-04 22:38:54 -0500618 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400619 /* wait and check worker queue activities */
620 rc = wait_event_interruptible(phba->work_waitq,
621 (test_and_clear_bit(LPFC_DATA_READY,
622 &phba->data_flags)
623 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500624 /* Signal wakeup shall terminate the worker thread */
625 if (rc) {
626 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
627 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500628 break;
James Smart3a55b532008-12-04 22:38:54 -0500629 }
dea31012005-04-17 16:05:31 -0500630
James Smart5e9d9b82008-06-14 22:52:53 -0400631 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500632 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500633 }
James Smart3a55b532008-12-04 22:38:54 -0500634 phba->worker_thread = NULL;
635 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
636 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500637 return 0;
638}
639
640/*
641 * This is only called to handle FC worker events. Since this a rare
642 * occurance, we allocate a struct lpfc_work_evt structure here instead of
643 * embedding it in the IOCB.
644 */
645int
James Smart2e0fef82007-06-17 19:56:36 -0500646lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500647 uint32_t evt)
648{
649 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500650 unsigned long flags;
dea31012005-04-17 16:05:31 -0500651
652 /*
653 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
654 * be queued to worker thread for processing
655 */
James Smart92d7f7b2007-06-17 19:56:38 -0500656 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500657 if (!evtp)
658 return 0;
659
660 evtp->evt_arg1 = arg1;
661 evtp->evt_arg2 = arg2;
662 evtp->evt = evt;
663
James Smarted957682007-06-17 19:56:37 -0500664 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400665 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500666 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500667
James Smart5e9d9b82008-06-14 22:52:53 -0400668 lpfc_worker_wake_up(phba);
669
dea31012005-04-17 16:05:31 -0500670 return 1;
671}
672
James Smart92d7f7b2007-06-17 19:56:38 -0500673void
674lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
675{
James Smart09372822008-01-11 01:52:54 -0500676 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500677 struct lpfc_hba *phba = vport->phba;
678 struct lpfc_nodelist *ndlp, *next_ndlp;
679 int rc;
680
681 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500682 if (!NLP_CHK_NODE_ACT(ndlp))
683 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500684 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
685 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400686 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
687 ((vport->port_type == LPFC_NPIV_PORT) &&
688 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500689 lpfc_unreg_rpi(vport, ndlp);
690
691 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400692 if ((phba->sli_rev < LPFC_SLI_REV4) &&
693 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500694 continue;
695 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
696 remove
697 ? NLP_EVT_DEVICE_RM
698 : NLP_EVT_DEVICE_RECOVERY);
699 }
700 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
701 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500702 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500703 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500704 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500705 }
706}
707
James Smart87af33f2007-10-27 13:37:43 -0400708void
James Smart98c9ea52007-10-27 13:37:33 -0400709lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500710{
James Smart695a8142010-01-26 23:08:03 -0500711 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
712
James Smart45ed1192009-10-02 15:17:02 -0400713 /* Cleanup any outstanding received buffers */
714 lpfc_cleanup_rcv_buffers(vport);
715
James Smart92d7f7b2007-06-17 19:56:38 -0500716 /* Cleanup any outstanding RSCN activity */
717 lpfc_els_flush_rscn(vport);
718
719 /* Cleanup any outstanding ELS commands */
720 lpfc_els_flush_cmd(vport);
721
722 lpfc_cleanup_rpis(vport, 0);
723
James Smart92d7f7b2007-06-17 19:56:38 -0500724 /* Turn off discovery timer if its running */
725 lpfc_can_disctmo(vport);
726}
727
James Smart3772a992009-05-22 14:50:54 -0400728void
James Smart98c9ea52007-10-27 13:37:33 -0400729lpfc_linkdown_port(struct lpfc_vport *vport)
730{
731 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
732
733 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
734
735 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
736 "Link Down: state:x%x rtry:x%x flg:x%x",
737 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
738
739 lpfc_port_link_failure(vport);
740
741}
742
dea31012005-04-17 16:05:31 -0500743int
James Smart685f0bf2007-04-25 09:53:08 -0400744lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500745{
James Smart2e0fef82007-06-17 19:56:36 -0500746 struct lpfc_vport *vport = phba->pport;
747 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400748 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400749 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400750 int i;
dea31012005-04-17 16:05:31 -0500751
James Smart3163f722008-02-08 18:50:25 -0500752 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500753 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500754
755 /* Block all SCSI stack I/Os */
756 lpfc_scsi_dev_block(phba);
757
James Smart2e0fef82007-06-17 19:56:36 -0500758 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500759 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500760 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500761 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500762 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500763 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500764 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500765 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500766 }
James Smart549e55c2007-08-02 11:09:51 -0400767 vports = lpfc_create_vport_work_array(phba);
768 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400769 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400770 /* Issue a LINK DOWN event to all nodes */
771 lpfc_linkdown_port(vports[i]);
772 }
James Smart09372822008-01-11 01:52:54 -0500773 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500774 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500775 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
776 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500777 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500778 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500779 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400780 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500781 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500782 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500783 }
784 }
785
dea31012005-04-17 16:05:31 -0500786 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500787 if (phba->pport->fc_flag & FC_PT2PT) {
788 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500789 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
790 if (mb) {
dea31012005-04-17 16:05:31 -0500791 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500792 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500793 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400794 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500795 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500796 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500797 }
798 }
James Smart2e0fef82007-06-17 19:56:36 -0500799 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500800 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500801 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500802 }
James Smart2e0fef82007-06-17 19:56:36 -0500803
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500804 return 0;
dea31012005-04-17 16:05:31 -0500805}
806
James Smart92d7f7b2007-06-17 19:56:38 -0500807static void
808lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500809{
James Smart92d7f7b2007-06-17 19:56:38 -0500810 struct lpfc_nodelist *ndlp;
811
812 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500813 if (!NLP_CHK_NODE_ACT(ndlp))
814 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500815 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
816 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500817 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500818 /* On Linkup its safe to clean up the ndlp
819 * from Fabric connections.
820 */
James Smart92d7f7b2007-06-17 19:56:38 -0500821 if (ndlp->nlp_DID != Fabric_DID)
822 lpfc_unreg_rpi(vport, ndlp);
823 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
824 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500825 /* Fail outstanding IO now since device is
826 * marked for PLOGI.
827 */
James Smart92d7f7b2007-06-17 19:56:38 -0500828 lpfc_unreg_rpi(vport, ndlp);
829 }
830 }
831}
832
833static void
834lpfc_linkup_port(struct lpfc_vport *vport)
835{
836 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500837 struct lpfc_hba *phba = vport->phba;
838
839 if ((vport->load_flag & FC_UNLOADING) != 0)
840 return;
841
James Smart858c9f62007-06-17 19:56:39 -0500842 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
843 "Link Up: top:x%x speed:x%x flg:x%x",
844 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
845
James Smart92d7f7b2007-06-17 19:56:38 -0500846 /* If NPIV is not enabled, only bring the physical port up */
847 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
848 (vport != phba->pport))
849 return;
dea31012005-04-17 16:05:31 -0500850
James Smart2e0fef82007-06-17 19:56:36 -0500851 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400852
James Smart2e0fef82007-06-17 19:56:36 -0500853 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500854 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
855 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
856 vport->fc_flag |= FC_NDISC_ACTIVE;
857 vport->fc_ns_retry = 0;
858 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500859
James Smart92d7f7b2007-06-17 19:56:38 -0500860 if (vport->fc_flag & FC_LBIT)
861 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500862
James Smart92d7f7b2007-06-17 19:56:38 -0500863}
864
865static int
866lpfc_linkup(struct lpfc_hba *phba)
867{
James Smart549e55c2007-08-02 11:09:51 -0400868 struct lpfc_vport **vports;
869 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500870
871 phba->link_state = LPFC_LINK_UP;
872
873 /* Unblock fabric iocbs if they are blocked */
874 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
875 del_timer_sync(&phba->fabric_block_timer);
876
James Smart549e55c2007-08-02 11:09:51 -0400877 vports = lpfc_create_vport_work_array(phba);
878 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400879 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400880 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500881 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400882 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
883 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500884 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500885
886 return 0;
887}
888
889/*
890 * This routine handles processing a CLEAR_LA mailbox
891 * command upon completion. It is setup in the LPFC_MBOXQ
892 * as the completion routine when the command is
893 * handed off to the SLI layer.
894 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100895static void
James Smart2e0fef82007-06-17 19:56:36 -0500896lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500897{
James Smart2e0fef82007-06-17 19:56:36 -0500898 struct lpfc_vport *vport = pmb->vport;
899 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
900 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400901 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500902 uint32_t control;
903
dea31012005-04-17 16:05:31 -0500904 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500905 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500906 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
907 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
908
909 /* Check for error */
910 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500911 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400912 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
913 "0320 CLEAR_LA mbxStatus error x%x hba "
914 "state x%x\n",
915 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500916 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500917 goto out;
918 }
919
James Smart92d7f7b2007-06-17 19:56:38 -0500920 if (vport->port_type == LPFC_PHYSICAL_PORT)
921 phba->link_state = LPFC_HBA_READY;
922
923 spin_lock_irq(&phba->hbalock);
924 psli->sli_flag |= LPFC_PROCESS_LA;
925 control = readl(phba->HCregaddr);
926 control |= HC_LAINT_ENA;
927 writel(control, phba->HCregaddr);
928 readl(phba->HCregaddr); /* flush */
929 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500930 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500931 return;
dea31012005-04-17 16:05:31 -0500932
dea31012005-04-17 16:05:31 -0500933out:
934 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400935 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
936 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500937 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500938
James Smart2e0fef82007-06-17 19:56:36 -0500939 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400940 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500941 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500942
James Smart2e0fef82007-06-17 19:56:36 -0500943 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500944
945 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500946
947 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500948 psli->sli_flag |= LPFC_PROCESS_LA;
949 control = readl(phba->HCregaddr);
950 control |= HC_LAINT_ENA;
951 writel(control, phba->HCregaddr);
952 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500953 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500954
955 return;
956}
957
James Smart2e0fef82007-06-17 19:56:36 -0500958
dea31012005-04-17 16:05:31 -0500959static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500960lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500961{
James Smart2e0fef82007-06-17 19:56:36 -0500962 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500963
James Smart04c68492009-05-22 14:52:52 -0400964 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500965 goto out;
dea31012005-04-17 16:05:31 -0500966
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500967 mempool_free(pmb, phba->mbox_mem_pool);
968
969 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500970 vport->fc_flag & FC_PUBLIC_LOOP &&
971 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500972 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500973 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500974 * LPFC_LOCAL_CFG_LINK while waiting for FAN
975 */
James Smart2e0fef82007-06-17 19:56:36 -0500976 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500977 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500978 }
dea31012005-04-17 16:05:31 -0500979
James Smart2e0fef82007-06-17 19:56:36 -0500980 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500981 * LPFC_FLOGI while waiting for FLOGI cmpl
982 */
James Smart92d7f7b2007-06-17 19:56:38 -0500983 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500984 lpfc_initial_flogi(vport);
985 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500986 return;
dea31012005-04-17 16:05:31 -0500987
988out:
James Smarte8b62012007-08-02 11:10:09 -0400989 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
990 "0306 CONFIG_LINK mbxStatus error x%x "
991 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400992 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500993 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500994
995 lpfc_linkdown(phba);
996
James Smarte8b62012007-08-02 11:10:09 -0400997 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
998 "0200 CONFIG_LINK bad hba state x%x\n",
999 vport->port_state);
dea31012005-04-17 16:05:31 -05001000
James Smart92d7f7b2007-06-17 19:56:38 -05001001 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001002 return;
1003}
1004
1005static void
James Smart6fb120a2009-05-22 14:52:59 -04001006lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1007{
1008 struct lpfc_vport *vport = mboxq->vport;
1009 unsigned long flags;
1010
1011 if (mboxq->u.mb.mbxStatus) {
1012 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1013 "2017 REG_FCFI mbxStatus error x%x "
1014 "HBA state x%x\n",
1015 mboxq->u.mb.mbxStatus, vport->port_state);
1016 mempool_free(mboxq, phba->mbox_mem_pool);
1017 return;
1018 }
1019
1020 /* Start FCoE discovery by sending a FLOGI. */
1021 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1022 /* Set the FCFI registered flag */
1023 spin_lock_irqsave(&phba->hbalock, flags);
1024 phba->fcf.fcf_flag |= FCF_REGISTERED;
1025 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart32b97932009-07-19 10:01:21 -04001026 /* If there is a pending FCoE event, restart FCF table scan. */
1027 if (lpfc_check_pending_fcoe_event(phba, 1)) {
1028 mempool_free(mboxq, phba->mbox_mem_pool);
1029 return;
1030 }
James Smart1c6f4ef52009-11-18 15:40:49 -05001031 spin_lock_irqsave(&phba->hbalock, flags);
James Smartecfd03c2010-02-12 14:41:27 -05001032 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart4d9ab992009-10-02 15:16:39 -04001033 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001034 spin_unlock_irqrestore(&phba->hbalock, flags);
1035 if (vport->port_state != LPFC_FLOGI)
James Smart6fb120a2009-05-22 14:52:59 -04001036 lpfc_initial_flogi(vport);
James Smart6fb120a2009-05-22 14:52:59 -04001037
1038 mempool_free(mboxq, phba->mbox_mem_pool);
1039 return;
1040}
1041
1042/**
1043 * lpfc_fab_name_match - Check if the fcf fabric name match.
1044 * @fab_name: pointer to fabric name.
1045 * @new_fcf_record: pointer to fcf record.
1046 *
1047 * This routine compare the fcf record's fabric name with provided
1048 * fabric name. If the fabric name are identical this function
1049 * returns 1 else return 0.
1050 **/
1051static uint32_t
1052lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1053{
James Smartecfd03c2010-02-12 14:41:27 -05001054 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001055 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001056 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1057 return 0;
1058 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1059 return 0;
1060 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1061 return 0;
1062 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1063 return 0;
1064 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1065 return 0;
1066 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1067 return 0;
1068 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1069 return 0;
1070 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001071}
1072
1073/**
James Smart8fa38512009-07-19 10:01:03 -04001074 * lpfc_sw_name_match - Check if the fcf switch name match.
1075 * @fab_name: pointer to fabric name.
1076 * @new_fcf_record: pointer to fcf record.
1077 *
1078 * This routine compare the fcf record's switch name with provided
1079 * switch name. If the switch name are identical this function
1080 * returns 1 else return 0.
1081 **/
1082static uint32_t
1083lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1084{
James Smartecfd03c2010-02-12 14:41:27 -05001085 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001086 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001087 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1088 return 0;
1089 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1090 return 0;
1091 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1092 return 0;
1093 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1094 return 0;
1095 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1096 return 0;
1097 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1098 return 0;
1099 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1100 return 0;
1101 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001102}
1103
1104/**
James Smart6fb120a2009-05-22 14:52:59 -04001105 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001106 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001107 * @new_fcf_record: pointer to fcf record.
1108 *
1109 * This routine compare the fcf record's mac address with HBA's
1110 * FCF mac address. If the mac addresses are identical this function
1111 * returns 1 else return 0.
1112 **/
1113static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001114lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001115{
James Smartecfd03c2010-02-12 14:41:27 -05001116 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001117 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001118 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1119 return 0;
1120 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1121 return 0;
1122 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1123 return 0;
1124 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1125 return 0;
1126 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1127 return 0;
1128 return 1;
1129}
1130
1131static bool
1132lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1133{
1134 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001135}
1136
1137/**
1138 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001139 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001140 * @new_fcf_record: pointer to fcf record.
1141 *
1142 * This routine copies the FCF information from the FCF
1143 * record to lpfc_hba data structure.
1144 **/
1145static void
James Smartecfd03c2010-02-12 14:41:27 -05001146lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1147 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001148{
James Smartecfd03c2010-02-12 14:41:27 -05001149 /* Fabric name */
1150 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001151 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001152 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001153 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001154 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001155 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001156 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001157 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001158 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001159 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001160 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001161 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001162 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001163 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001164 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001165 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001166 /* Mac address */
1167 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1168 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1169 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1170 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1171 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1172 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1173 /* FCF record index */
1174 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1175 /* FCF record priority */
1176 fcf_rec->priority = new_fcf_record->fip_priority;
1177 /* Switch name */
1178 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001179 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001180 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001181 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001182 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001183 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001184 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001185 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001186 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001187 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001188 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001189 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001190 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001191 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001192 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001193 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001194}
1195
1196/**
James Smartecfd03c2010-02-12 14:41:27 -05001197 * lpfc_update_fcf_record - Update driver fcf record
1198 * @phba: pointer to lpfc hba data structure.
1199 * @fcf_rec: pointer to driver fcf record.
1200 * @new_fcf_record: pointer to hba fcf record.
1201 * @addr_mode: address mode to be set to the driver fcf record.
1202 * @vlan_id: vlan tag to be set to the driver fcf record.
1203 * @flag: flag bits to be set to the driver fcf record.
1204 *
1205 * This routine updates the driver FCF record from the new HBA FCF record
1206 * together with the address mode, vlan_id, and other informations. This
1207 * routine is called with the host lock held.
1208 **/
1209static void
1210__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1211 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1212 uint16_t vlan_id, uint32_t flag)
1213{
1214 /* Copy the fields from the HBA's FCF record */
1215 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1216 /* Update other fields of driver FCF record */
1217 fcf_rec->addr_mode = addr_mode;
1218 fcf_rec->vlan_id = vlan_id;
1219 fcf_rec->flag |= (flag | RECORD_VALID);
1220}
1221
1222/**
James Smart6fb120a2009-05-22 14:52:59 -04001223 * lpfc_register_fcf - Register the FCF with hba.
1224 * @phba: pointer to lpfc hba data structure.
1225 *
1226 * This routine issues a register fcfi mailbox command to register
1227 * the fcf with HBA.
1228 **/
1229static void
1230lpfc_register_fcf(struct lpfc_hba *phba)
1231{
1232 LPFC_MBOXQ_t *fcf_mbxq;
1233 int rc;
1234 unsigned long flags;
1235
1236 spin_lock_irqsave(&phba->hbalock, flags);
1237
1238 /* If the FCF is not availabe do nothing. */
1239 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smart4d9ab992009-10-02 15:16:39 -04001240 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001241 spin_unlock_irqrestore(&phba->hbalock, flags);
1242 return;
1243 }
1244
1245 /* The FCF is already registered, start discovery */
1246 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001247 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart32b97932009-07-19 10:01:21 -04001248 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001249 spin_unlock_irqrestore(&phba->hbalock, flags);
1250 if (phba->pport->port_state != LPFC_FLOGI)
1251 lpfc_initial_flogi(phba->pport);
1252 return;
1253 }
1254 spin_unlock_irqrestore(&phba->hbalock, flags);
1255
1256 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1257 GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001258 if (!fcf_mbxq) {
1259 spin_lock_irqsave(&phba->hbalock, flags);
1260 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1261 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001262 return;
James Smart4d9ab992009-10-02 15:16:39 -04001263 }
James Smart6fb120a2009-05-22 14:52:59 -04001264
1265 lpfc_reg_fcfi(phba, fcf_mbxq);
1266 fcf_mbxq->vport = phba->pport;
1267 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1268 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001269 if (rc == MBX_NOT_FINISHED) {
1270 spin_lock_irqsave(&phba->hbalock, flags);
1271 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1272 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001273 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001274 }
James Smart6fb120a2009-05-22 14:52:59 -04001275
1276 return;
1277}
1278
1279/**
1280 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1281 * @phba: pointer to lpfc hba data structure.
1282 * @new_fcf_record: pointer to fcf record.
1283 * @boot_flag: Indicates if this record used by boot bios.
1284 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001285 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001286 *
1287 * This routine compare the fcf record with connect list obtained from the
1288 * config region to decide if this FCF can be used for SAN discovery. It returns
1289 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1290 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1291 * is used by boot bios and addr_mode will indicate the addressing mode to be
1292 * used for this FCF when the function returns.
1293 * If the FCF record need to be used with a particular vlan id, the vlan is
1294 * set in the vlan_id on return of the function. If not VLAN tagging need to
1295 * be used with the FCF vlan_id will be set to 0xFFFF;
1296 **/
1297static int
1298lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1299 struct fcf_record *new_fcf_record,
1300 uint32_t *boot_flag, uint32_t *addr_mode,
1301 uint16_t *vlan_id)
1302{
1303 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001304 int i, j, fcf_vlan_id = 0;
1305
1306 /* Find the lowest VLAN id in the FCF record */
1307 for (i = 0; i < 512; i++) {
1308 if (new_fcf_record->vlan_bitmap[i]) {
1309 fcf_vlan_id = i * 8;
1310 j = 0;
1311 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1312 j++;
1313 fcf_vlan_id++;
1314 }
1315 break;
1316 }
1317 }
James Smart6fb120a2009-05-22 14:52:59 -04001318
James Smart0c287582009-06-10 17:22:56 -04001319 /* If FCF not available return 0 */
1320 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1321 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1322 return 0;
1323
James Smart45ed1192009-10-02 15:17:02 -04001324 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001325 *boot_flag = 0;
1326 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1327 new_fcf_record);
1328 if (phba->valid_vlan)
1329 *vlan_id = phba->vlan_id;
1330 else
1331 *vlan_id = 0xFFFF;
1332 return 1;
1333 }
1334
1335 /*
1336 * If there are no FCF connection table entry, driver connect to all
1337 * FCFs.
1338 */
1339 if (list_empty(&phba->fcf_conn_rec_list)) {
1340 *boot_flag = 0;
1341 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1342 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001343
1344 /*
1345 * When there are no FCF connect entries, use driver's default
1346 * addressing mode - FPMA.
1347 */
1348 if (*addr_mode & LPFC_FCF_FPMA)
1349 *addr_mode = LPFC_FCF_FPMA;
1350
James Smart4d9ab992009-10-02 15:16:39 -04001351 /* If FCF record report a vlan id use that vlan id */
1352 if (fcf_vlan_id)
1353 *vlan_id = fcf_vlan_id;
1354 else
1355 *vlan_id = 0xFFFF;
James Smart6fb120a2009-05-22 14:52:59 -04001356 return 1;
1357 }
1358
James Smartecfd03c2010-02-12 14:41:27 -05001359 list_for_each_entry(conn_entry,
1360 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001361 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1362 continue;
1363
1364 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1365 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001366 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001367 continue;
James Smart8fa38512009-07-19 10:01:03 -04001368 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1369 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1370 new_fcf_record))
1371 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001372 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1373 /*
1374 * If the vlan bit map does not have the bit set for the
1375 * vlan id to be used, then it is not a match.
1376 */
1377 if (!(new_fcf_record->vlan_bitmap
1378 [conn_entry->conn_rec.vlan_tag / 8] &
1379 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1380 continue;
1381 }
1382
1383 /*
James Smart0c287582009-06-10 17:22:56 -04001384 * If connection record does not support any addressing mode,
1385 * skip the FCF record.
1386 */
1387 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1388 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1389 continue;
1390
1391 /*
James Smart6fb120a2009-05-22 14:52:59 -04001392 * Check if the connection record specifies a required
1393 * addressing mode.
1394 */
1395 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1396 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1397
1398 /*
1399 * If SPMA required but FCF not support this continue.
1400 */
1401 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1402 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1403 new_fcf_record) & LPFC_FCF_SPMA))
1404 continue;
1405
1406 /*
1407 * If FPMA 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_FPMA))
1412 continue;
1413 }
1414
1415 /*
1416 * This fcf record matches filtering criteria.
1417 */
1418 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1419 *boot_flag = 1;
1420 else
1421 *boot_flag = 0;
1422
James Smart0c287582009-06-10 17:22:56 -04001423 /*
1424 * If user did not specify any addressing mode, or if the
1425 * prefered addressing mode specified by user is not supported
1426 * by FCF, allow fabric to pick the addressing mode.
1427 */
James Smart6fb120a2009-05-22 14:52:59 -04001428 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1429 new_fcf_record);
1430 /*
1431 * If the user specified a required address mode, assign that
1432 * address mode
1433 */
1434 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1435 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1436 *addr_mode = (conn_entry->conn_rec.flags &
1437 FCFCNCT_AM_SPMA) ?
1438 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1439 /*
1440 * If the user specified a prefered address mode, use the
1441 * addr mode only if FCF support the addr_mode.
1442 */
1443 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1444 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1445 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1446 (*addr_mode & LPFC_FCF_SPMA))
1447 *addr_mode = LPFC_FCF_SPMA;
1448 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1449 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1450 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1451 (*addr_mode & LPFC_FCF_FPMA))
1452 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001453
James Smart4d9ab992009-10-02 15:16:39 -04001454 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001455 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1456 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001457 /*
1458 * If no vlan id is specified in connect list, use the vlan id
1459 * in the FCF record
1460 */
1461 else if (fcf_vlan_id)
1462 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001463 else
1464 *vlan_id = 0xFFFF;
1465
1466 return 1;
1467 }
1468
1469 return 0;
1470}
1471
1472/**
James Smart32b97932009-07-19 10:01:21 -04001473 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1474 * @phba: pointer to lpfc hba data structure.
1475 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1476 *
1477 * This function check if there is any fcoe event pending while driver
1478 * scan FCF entries. If there is any pending event, it will restart the
1479 * FCF saning and return 1 else return 0.
1480 */
1481int
1482lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1483{
James Smart32b97932009-07-19 10:01:21 -04001484 /*
1485 * If the Link is up and no FCoE events while in the
1486 * FCF discovery, no need to restart FCF discovery.
1487 */
1488 if ((phba->link_state >= LPFC_LINK_UP) &&
1489 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1490 return 0;
1491
James Smart0c9ab6f2010-02-26 14:15:57 -05001492 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1493 "2768 Pending link or FCF event during current "
1494 "handling of the previous event: link_state:x%x, "
1495 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1496 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1497 phba->fcoe_eventtag);
1498
James Smart32b97932009-07-19 10:01:21 -04001499 spin_lock_irq(&phba->hbalock);
1500 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1501 spin_unlock_irq(&phba->hbalock);
1502
James Smart0c9ab6f2010-02-26 14:15:57 -05001503 if (phba->link_state >= LPFC_LINK_UP) {
1504 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1505 "2780 Restart FCF table scan due to "
1506 "pending FCF event:evt_tag_at_scan:x%x, "
1507 "evt_tag_current:x%x\n",
1508 phba->fcoe_eventtag_at_fcf_scan,
1509 phba->fcoe_eventtag);
1510 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1511 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001512 /*
1513 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1514 * flag
1515 */
James Smart1c6f4ef52009-11-18 15:40:49 -05001516 spin_lock_irq(&phba->hbalock);
James Smart4d9ab992009-10-02 15:16:39 -04001517 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001518 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001519 spin_unlock_irq(&phba->hbalock);
1520 }
James Smart32b97932009-07-19 10:01:21 -04001521
James Smart0c9ab6f2010-02-26 14:15:57 -05001522 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001523 if (unreg_fcf) {
1524 spin_lock_irq(&phba->hbalock);
1525 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1526 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001527 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001528 }
James Smart32b97932009-07-19 10:01:21 -04001529 return 1;
1530}
1531
1532/**
James Smart0c9ab6f2010-02-26 14:15:57 -05001533 * lpfc_sli4_fcf_rec_mbox_parse - parse non-embedded fcf record mailbox command
James Smart6fb120a2009-05-22 14:52:59 -04001534 * @phba: pointer to lpfc hba data structure.
1535 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001536 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001537 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001538 * This routine parses the non-embedded fcf mailbox command by performing the
1539 * necessarily error checking, non-embedded read FCF record mailbox command
1540 * SGE parsing, and endianness swapping.
1541 *
1542 * Returns the pointer to the new FCF record in the non-embedded mailbox
1543 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001544 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001545static struct fcf_record *
1546lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1547 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001548{
1549 void *virt_addr;
1550 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001551 struct lpfc_mbx_sge sge;
1552 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1553 uint32_t shdr_status, shdr_add_status;
1554 union lpfc_sli4_cfg_shdr *shdr;
1555 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001556
James Smart6fb120a2009-05-22 14:52:59 -04001557 /* Get the first SGE entry from the non-embedded DMA memory. This
1558 * routine only uses a single SGE.
1559 */
1560 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1561 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1562 if (unlikely(!mboxq->sge_array)) {
1563 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1564 "2524 Failed to get the non-embedded SGE "
1565 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001566 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001567 }
1568 virt_addr = mboxq->sge_array->addr[0];
1569
1570 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1571 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001572 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001573 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001574 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1575 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001576 "2726 READ_FCF_RECORD Indicates empty "
1577 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001578 else
1579 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001580 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001581 "with status x%x add_status x%x, "
1582 "mbx\n", shdr_status, shdr_add_status);
1583 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001584 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001585
1586 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001587 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1588 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1589 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001590 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001591 new_fcf_record = (struct fcf_record *)(virt_addr +
1592 sizeof(struct lpfc_mbx_read_fcf_tbl));
1593 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1594 sizeof(struct fcf_record));
James Smart6fb120a2009-05-22 14:52:59 -04001595
James Smart0c9ab6f2010-02-26 14:15:57 -05001596 return new_fcf_record;
1597}
1598
1599/**
1600 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1601 * @phba: pointer to lpfc hba data structure.
1602 * @fcf_record: pointer to the fcf record.
1603 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1604 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1605 *
1606 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1607 * enabled.
1608 **/
1609static void
1610lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1611 struct fcf_record *fcf_record,
1612 uint16_t vlan_id,
1613 uint16_t next_fcf_index)
1614{
1615 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1616 "2764 READ_FCF_RECORD:\n"
1617 "\tFCF_Index : x%x\n"
1618 "\tFCF_Avail : x%x\n"
1619 "\tFCF_Valid : x%x\n"
1620 "\tFIP_Priority : x%x\n"
1621 "\tMAC_Provider : x%x\n"
1622 "\tLowest VLANID : x%x\n"
1623 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1624 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1625 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1626 "\tNext_FCF_Index: x%x\n",
1627 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1628 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1629 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1630 fcf_record->fip_priority,
1631 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1632 vlan_id,
1633 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1634 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1635 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1636 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1637 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1638 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1639 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1640 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1641 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1642 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1643 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1644 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1645 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1646 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1647 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1648 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1649 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1650 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1651 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1652 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1653 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1654 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1655 next_fcf_index);
1656}
1657
1658/**
1659 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
1660 * @phba: pointer to lpfc hba data structure.
1661 * @mboxq: pointer to mailbox object.
1662 *
1663 * This function iterates through all the fcf records available in
1664 * HBA and chooses the optimal FCF record for discovery. After finding
1665 * the FCF for discovery it registers the FCF record and kicks start
1666 * discovery.
1667 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
1668 * use an FCF record which matches fabric name and mac address of the
1669 * currently used FCF record.
1670 * If the driver supports only one FCF, it will try to use the FCF record
1671 * used by BOOT_BIOS.
1672 */
1673void
1674lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1675{
1676 struct fcf_record *new_fcf_record;
1677 uint32_t boot_flag, addr_mode;
1678 uint16_t fcf_index, next_fcf_index;
1679 struct lpfc_fcf_rec *fcf_rec = NULL;
1680 uint16_t vlan_id;
1681 int rc;
1682
1683 /* If there is pending FCoE event restart FCF table scan */
1684 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1685 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1686 return;
1687 }
1688
1689 /* Parse the FCF record from the non-embedded mailbox command */
1690 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
1691 &next_fcf_index);
1692 if (!new_fcf_record) {
1693 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1694 "2765 Mailbox command READ_FCF_RECORD "
1695 "failed to retrieve a FCF record.\n");
1696 /* Let next new FCF event trigger fast failover */
1697 spin_lock_irq(&phba->hbalock);
1698 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1699 spin_unlock_irq(&phba->hbalock);
1700 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1701 return;
1702 }
1703
1704 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05001705 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1706 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05001707
1708 /* Log the FCF record information if turned on */
1709 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
1710 next_fcf_index);
1711
James Smart6fb120a2009-05-22 14:52:59 -04001712 /*
1713 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05001714 * read the next entry; otherwise, this is an eligible FCF
1715 * record for round robin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04001716 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001717 if (!rc) {
1718 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1719 "2781 FCF record fcf_index:x%x failed FCF "
1720 "connection list check, fcf_avail:x%x, "
1721 "fcf_valid:x%x\n",
1722 bf_get(lpfc_fcf_record_fcf_index,
1723 new_fcf_record),
1724 bf_get(lpfc_fcf_record_fcf_avail,
1725 new_fcf_record),
1726 bf_get(lpfc_fcf_record_fcf_valid,
1727 new_fcf_record));
James Smart6fb120a2009-05-22 14:52:59 -04001728 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001729 } else {
1730 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1731 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
1732 if (rc)
1733 goto read_next_fcf;
1734 }
1735
James Smart6fb120a2009-05-22 14:52:59 -04001736 /*
1737 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05001738 * FCF record for the discovery. The condition that a rescan
1739 * matches the in-use FCF record: fabric name, switch name, mac
1740 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04001741 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001742 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001743 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartecfd03c2010-02-12 14:41:27 -05001744 if (lpfc_fab_name_match(phba->fcf.current_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001745 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001746 lpfc_sw_name_match(phba->fcf.current_rec.switch_name,
James Smart8fa38512009-07-19 10:01:03 -04001747 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001748 lpfc_mac_addr_match(phba->fcf.current_rec.mac_addr,
1749 new_fcf_record) &&
1750 lpfc_vlan_id_match(phba->fcf.current_rec.vlan_id,
1751 vlan_id)) {
James Smart6fb120a2009-05-22 14:52:59 -04001752 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smartecfd03c2010-02-12 14:41:27 -05001753 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
1754 /* Stop FCF redisc wait timer if pending */
1755 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
1756 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1757 /* If in fast failover, mark it's completed */
James Smartfc2b9892010-02-26 14:15:29 -05001758 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV |
James Smart0c9ab6f2010-02-26 14:15:57 -05001759 FCF_DISCOVERY);
1760 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001761 goto out;
1762 }
James Smartecfd03c2010-02-12 14:41:27 -05001763 /*
1764 * Read next FCF record from HBA searching for the matching
1765 * with in-use record only if not during the fast failover
1766 * period. In case of fast failover period, it shall try to
1767 * determine whether the FCF record just read should be the
1768 * next candidate.
1769 */
1770 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001771 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001772 goto read_next_fcf;
1773 }
James Smart6fb120a2009-05-22 14:52:59 -04001774 }
James Smartecfd03c2010-02-12 14:41:27 -05001775 /*
1776 * Update on failover FCF record only if it's in FCF fast-failover
1777 * period; otherwise, update on current FCF record.
1778 */
James Smartfc2b9892010-02-26 14:15:29 -05001779 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1780 fcf_rec = &phba->fcf.failover_rec;
1781 else
James Smartecfd03c2010-02-12 14:41:27 -05001782 fcf_rec = &phba->fcf.current_rec;
1783
James Smart6fb120a2009-05-22 14:52:59 -04001784 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1785 /*
James Smartecfd03c2010-02-12 14:41:27 -05001786 * If the driver FCF record does not have boot flag
1787 * set and new hba fcf record has boot flag set, use
1788 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001789 */
James Smartecfd03c2010-02-12 14:41:27 -05001790 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
1791 /* Choose this FCF record */
1792 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1793 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05001794 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001795 goto read_next_fcf;
1796 }
1797 /*
James Smartecfd03c2010-02-12 14:41:27 -05001798 * If the driver FCF record has boot flag set and the
1799 * new hba FCF record does not have boot flag, read
1800 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04001801 */
James Smartecfd03c2010-02-12 14:41:27 -05001802 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001803 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001804 goto read_next_fcf;
1805 }
1806 /*
James Smartecfd03c2010-02-12 14:41:27 -05001807 * If the new hba FCF record has lower priority value
1808 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04001809 */
James Smartfc2b9892010-02-26 14:15:29 -05001810 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smartecfd03c2010-02-12 14:41:27 -05001811 /* Choose this FCF record */
1812 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1813 addr_mode, vlan_id, 0);
James Smart6fb120a2009-05-22 14:52:59 -04001814 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001815 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001816 goto read_next_fcf;
1817 }
1818 /*
James Smartecfd03c2010-02-12 14:41:27 -05001819 * This is the first suitable FCF record, choose this record for
1820 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001821 */
James Smartecfd03c2010-02-12 14:41:27 -05001822 if (fcf_rec) {
1823 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1824 addr_mode, vlan_id, (boot_flag ?
1825 BOOT_ENABLE : 0));
1826 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart6fb120a2009-05-22 14:52:59 -04001827 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001828 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001829 goto read_next_fcf;
1830
1831read_next_fcf:
1832 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05001833 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
1834 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
1835 /*
1836 * Case of FCF fast failover scan
1837 */
1838
1839 /*
1840 * It has not found any suitable FCF record, cancel
1841 * FCF scan inprogress, and do nothing
1842 */
1843 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001844 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1845 "2782 No suitable FCF record "
1846 "found during this round of "
1847 "post FCF rediscovery scan: "
1848 "fcf_evt_tag:x%x, fcf_index: "
1849 "x%x\n",
1850 phba->fcoe_eventtag_at_fcf_scan,
1851 bf_get(lpfc_fcf_record_fcf_index,
1852 new_fcf_record));
1853 /*
1854 * Let next new FCF event trigger fast
1855 * failover
1856 */
1857 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001858 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart0c9ab6f2010-02-26 14:15:57 -05001859 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001860 return;
1861 }
1862 /*
1863 * It has found a suitable FCF record that is not
1864 * the same as in-use FCF record, unregister the
1865 * in-use FCF record, replace the in-use FCF record
1866 * with the new FCF record, mark FCF fast failover
1867 * completed, and then start register the new FCF
1868 * record.
1869 */
1870
James Smart0c9ab6f2010-02-26 14:15:57 -05001871 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001872 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05001873
1874 /* Replace in-use record with the new record */
James Smartecfd03c2010-02-12 14:41:27 -05001875 memcpy(&phba->fcf.current_rec,
1876 &phba->fcf.failover_rec,
1877 sizeof(struct lpfc_fcf_rec));
1878 /* mark the FCF fast failover completed */
James Smart0c9ab6f2010-02-26 14:15:57 -05001879 spin_lock_irq(&phba->hbalock);
1880 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
1881 spin_unlock_irq(&phba->hbalock);
1882 /*
1883 * Set up the initial registered FCF index for FLOGI
1884 * round robin FCF failover.
1885 */
1886 phba->fcf.fcf_rr_init_indx =
1887 phba->fcf.failover_rec.fcf_indx;
James Smartecfd03c2010-02-12 14:41:27 -05001888 /* Register to the new FCF record */
1889 lpfc_register_fcf(phba);
1890 } else {
1891 /*
1892 * In case of transaction period to fast FCF failover,
1893 * do nothing when search to the end of the FCF table.
1894 */
1895 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
1896 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
1897 return;
1898 /*
1899 * Otherwise, initial scan or post linkdown rescan,
James Smart0c9ab6f2010-02-26 14:15:57 -05001900 * register with the best FCF record found so far
1901 * through the FCF scanning process.
James Smartecfd03c2010-02-12 14:41:27 -05001902 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001903
1904 /* mark the initial FCF discovery completed */
1905 spin_lock_irq(&phba->hbalock);
1906 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
1907 spin_unlock_irq(&phba->hbalock);
1908 /*
1909 * Set up the initial registered FCF index for FLOGI
1910 * round robin FCF failover
1911 */
1912 phba->fcf.fcf_rr_init_indx =
1913 phba->fcf.current_rec.fcf_indx;
1914 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05001915 lpfc_register_fcf(phba);
1916 }
1917 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05001918 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04001919 return;
1920
1921out:
1922 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1923 lpfc_register_fcf(phba);
1924
1925 return;
1926}
1927
1928/**
James Smart0c9ab6f2010-02-26 14:15:57 -05001929 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf round robin read_fcf mbox cmpl hdler
1930 * @phba: pointer to lpfc hba data structure.
1931 * @mboxq: pointer to mailbox object.
1932 *
1933 * This is the callback function for FLOGI failure round robin FCF failover
1934 * read FCF record mailbox command from the eligible FCF record bmask for
1935 * performing the failover. If the FCF read back is not valid/available, it
1936 * fails through to retrying FLOGI to the currently registered FCF again.
1937 * Otherwise, if the FCF read back is valid and available, it will set the
1938 * newly read FCF record to the failover FCF record, unregister currently
1939 * registered FCF record, copy the failover FCF record to the current
1940 * FCF record, and then register the current FCF record before proceeding
1941 * to trying FLOGI on the new failover FCF.
1942 */
1943void
1944lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1945{
1946 struct fcf_record *new_fcf_record;
1947 uint32_t boot_flag, addr_mode;
1948 uint16_t next_fcf_index;
1949 uint16_t current_fcf_index;
1950 uint16_t vlan_id;
1951
1952 /* If link state is not up, stop the round robin failover process */
1953 if (phba->link_state < LPFC_LINK_UP) {
1954 spin_lock_irq(&phba->hbalock);
1955 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1956 spin_unlock_irq(&phba->hbalock);
1957 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1958 return;
1959 }
1960
1961 /* Parse the FCF record from the non-embedded mailbox command */
1962 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
1963 &next_fcf_index);
1964 if (!new_fcf_record) {
1965 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1966 "2766 Mailbox command READ_FCF_RECORD "
1967 "failed to retrieve a FCF record.\n");
1968 goto out;
1969 }
1970
1971 /* Get the needed parameters from FCF record */
1972 lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1973 &addr_mode, &vlan_id);
1974
1975 /* Log the FCF record information if turned on */
1976 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
1977 next_fcf_index);
1978
1979 /* Upload new FCF record to the failover FCF record */
1980 spin_lock_irq(&phba->hbalock);
1981 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
1982 new_fcf_record, addr_mode, vlan_id,
1983 (boot_flag ? BOOT_ENABLE : 0));
1984 spin_unlock_irq(&phba->hbalock);
1985
1986 current_fcf_index = phba->fcf.current_rec.fcf_indx;
1987
1988 /* Unregister the current in-use FCF record */
1989 lpfc_unregister_fcf(phba);
1990
1991 /* Replace in-use record with the new record */
1992 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
1993 sizeof(struct lpfc_fcf_rec));
1994
1995 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1996 "2783 FLOGI round robin FCF failover from FCF "
1997 "(index:x%x) to FCF (index:x%x).\n",
1998 current_fcf_index,
1999 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
2000
2001out:
2002 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2003 lpfc_register_fcf(phba);
2004}
2005
2006/**
2007 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2008 * @phba: pointer to lpfc hba data structure.
2009 * @mboxq: pointer to mailbox object.
2010 *
2011 * This is the callback function of read FCF record mailbox command for
2012 * updating the eligible FCF bmask for FLOGI failure round robin FCF
2013 * failover when a new FCF event happened. If the FCF read back is
2014 * valid/available and it passes the connection list check, it updates
2015 * the bmask for the eligible FCF record for round robin failover.
2016 */
2017void
2018lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2019{
2020 struct fcf_record *new_fcf_record;
2021 uint32_t boot_flag, addr_mode;
2022 uint16_t fcf_index, next_fcf_index;
2023 uint16_t vlan_id;
2024 int rc;
2025
2026 /* If link state is not up, no need to proceed */
2027 if (phba->link_state < LPFC_LINK_UP)
2028 goto out;
2029
2030 /* If FCF discovery period is over, no need to proceed */
2031 if (phba->fcf.fcf_flag & FCF_DISCOVERY)
2032 goto out;
2033
2034 /* Parse the FCF record from the non-embedded mailbox command */
2035 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2036 &next_fcf_index);
2037 if (!new_fcf_record) {
2038 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2039 "2767 Mailbox command READ_FCF_RECORD "
2040 "failed to retrieve a FCF record.\n");
2041 goto out;
2042 }
2043
2044 /* Check the connection list for eligibility */
2045 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2046 &addr_mode, &vlan_id);
2047
2048 /* Log the FCF record information if turned on */
2049 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2050 next_fcf_index);
2051
2052 if (!rc)
2053 goto out;
2054
2055 /* Update the eligible FCF record index bmask */
2056 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2057 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
2058
2059out:
2060 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2061}
2062
2063/**
James Smart1c6834a2009-07-19 10:01:26 -04002064 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2065 * @phba: pointer to lpfc hba data structure.
2066 * @mboxq: pointer to mailbox data structure.
2067 *
2068 * This function handles completion of init vpi mailbox command.
2069 */
James Smart695a8142010-01-26 23:08:03 -05002070void
James Smart1c6834a2009-07-19 10:01:26 -04002071lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2072{
2073 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002074 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002075 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2076
James Smart1c6834a2009-07-19 10:01:26 -04002077 if (mboxq->u.mb.mbxStatus) {
2078 lpfc_printf_vlog(vport, KERN_ERR,
2079 LOG_MBOX,
2080 "2609 Init VPI mailbox failed 0x%x\n",
2081 mboxq->u.mb.mbxStatus);
2082 mempool_free(mboxq, phba->mbox_mem_pool);
2083 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2084 return;
2085 }
James Smart72100cc2010-02-12 14:43:01 -05002086 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002087 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002088 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002089
James Smart695a8142010-01-26 23:08:03 -05002090 /* If this port is physical port or FDISC is done, do reg_vpi */
2091 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2092 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2093 if (!ndlp)
2094 lpfc_printf_vlog(vport, KERN_ERR,
2095 LOG_DISCOVERY,
2096 "2731 Cannot find fabric "
2097 "controller node\n");
2098 else
2099 lpfc_register_new_vport(phba, vport, ndlp);
2100 mempool_free(mboxq, phba->mbox_mem_pool);
2101 return;
2102 }
2103
James Smart1c6834a2009-07-19 10:01:26 -04002104 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2105 lpfc_initial_fdisc(vport);
2106 else {
2107 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002108 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2109 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002110 }
James Smart695a8142010-01-26 23:08:03 -05002111 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002112 return;
2113}
2114
2115/**
James Smartecfd03c2010-02-12 14:41:27 -05002116 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2117 * @vport: pointer to lpfc_vport data structure.
2118 *
2119 * This function issue a init_vpi mailbox command to initialize
2120 * VPI for the vport.
2121 */
2122void
2123lpfc_issue_init_vpi(struct lpfc_vport *vport)
2124{
2125 LPFC_MBOXQ_t *mboxq;
2126 int rc;
2127
2128 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2129 if (!mboxq) {
2130 lpfc_printf_vlog(vport, KERN_ERR,
2131 LOG_MBOX, "2607 Failed to allocate "
2132 "init_vpi mailbox\n");
2133 return;
2134 }
2135 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2136 mboxq->vport = vport;
2137 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2138 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2139 if (rc == MBX_NOT_FINISHED) {
2140 lpfc_printf_vlog(vport, KERN_ERR,
2141 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2142 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2143 }
2144}
2145
2146/**
James Smart6fb120a2009-05-22 14:52:59 -04002147 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2148 * @phba: pointer to lpfc hba data structure.
2149 *
2150 * This function loops through the list of vports on the @phba and issues an
2151 * FDISC if possible.
2152 */
2153void
2154lpfc_start_fdiscs(struct lpfc_hba *phba)
2155{
2156 struct lpfc_vport **vports;
2157 int i;
2158
2159 vports = lpfc_create_vport_work_array(phba);
2160 if (vports != NULL) {
2161 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2162 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2163 continue;
2164 /* There are no vpi for this vport */
2165 if (vports[i]->vpi > phba->max_vpi) {
2166 lpfc_vport_set_state(vports[i],
2167 FC_VPORT_FAILED);
2168 continue;
2169 }
2170 if (phba->fc_topology == TOPOLOGY_LOOP) {
2171 lpfc_vport_set_state(vports[i],
2172 FC_VPORT_LINKDOWN);
2173 continue;
2174 }
James Smart1c6834a2009-07-19 10:01:26 -04002175 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002176 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002177 continue;
2178 }
James Smart6fb120a2009-05-22 14:52:59 -04002179 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2180 lpfc_initial_fdisc(vports[i]);
2181 else {
2182 lpfc_vport_set_state(vports[i],
2183 FC_VPORT_NO_FABRIC_SUPP);
2184 lpfc_printf_vlog(vports[i], KERN_ERR,
2185 LOG_ELS,
2186 "0259 No NPIV "
2187 "Fabric support\n");
2188 }
2189 }
2190 }
2191 lpfc_destroy_vport_work_array(phba, vports);
2192}
2193
2194void
2195lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2196{
2197 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2198 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002199 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002200
2201 if (mboxq->u.mb.mbxStatus) {
2202 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2203 "2018 REG_VFI mbxStatus error x%x "
2204 "HBA state x%x\n",
2205 mboxq->u.mb.mbxStatus, vport->port_state);
2206 if (phba->fc_topology == TOPOLOGY_LOOP) {
2207 /* FLOGI failed, use loop map to make discovery list */
2208 lpfc_disc_list_loopmap(vport);
2209 /* Start discovery */
2210 lpfc_disc_start(vport);
2211 goto fail_free_mem;
2212 }
2213 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2214 goto fail_free_mem;
2215 }
James Smartc8685952009-11-18 15:39:16 -05002216 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002217 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002218 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002219 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002220 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002221 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002222
2223 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2224 lpfc_start_fdiscs(phba);
2225 lpfc_do_scr_ns_plogi(phba, vport);
2226 }
2227
2228fail_free_mem:
2229 mempool_free(mboxq, phba->mbox_mem_pool);
2230 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2231 kfree(dmabuf);
2232 return;
2233}
2234
2235static void
James Smart2e0fef82007-06-17 19:56:36 -05002236lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002237{
James Smart6fb120a2009-05-22 14:52:59 -04002238 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002239 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002240 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002241
2242
2243 /* Check for error */
2244 if (mb->mbxStatus) {
2245 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002246 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2247 "0319 READ_SPARAM mbxStatus error x%x "
2248 "hba state x%x>\n",
2249 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002250 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002251 goto out;
2252 }
2253
James Smart2e0fef82007-06-17 19:56:36 -05002254 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002255 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05002256 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05002257 u64_to_wwn(phba->cfg_soft_wwnn,
2258 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04002259 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05002260 u64_to_wwn(phba->cfg_soft_wwpn,
2261 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05002262 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
2263 sizeof(vport->fc_nodename));
2264 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
2265 sizeof(vport->fc_portname));
2266 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2267 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2268 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2269 }
2270
dea31012005-04-17 16:05:31 -05002271 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2272 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002273 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002274 return;
2275
2276out:
2277 pmb->context1 = NULL;
2278 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2279 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002280 lpfc_issue_clear_la(phba, vport);
2281 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002282 return;
2283}
2284
2285static void
James Smart92d7f7b2007-06-17 19:56:38 -05002286lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05002287{
James Smart92d7f7b2007-06-17 19:56:38 -05002288 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002289 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002290 int i;
James Smart14691152006-12-02 13:34:28 -05002291 struct lpfc_dmabuf *mp;
2292 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002293 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002294
James Smart92d7f7b2007-06-17 19:56:38 -05002295 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002296 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05002297 case LA_1GHZ_LINK:
2298 phba->fc_linkspeed = LA_1GHZ_LINK;
2299 break;
2300 case LA_2GHZ_LINK:
2301 phba->fc_linkspeed = LA_2GHZ_LINK;
2302 break;
2303 case LA_4GHZ_LINK:
2304 phba->fc_linkspeed = LA_4GHZ_LINK;
2305 break;
2306 case LA_8GHZ_LINK:
2307 phba->fc_linkspeed = LA_8GHZ_LINK;
2308 break;
James Smartf4b4c682009-05-22 14:53:12 -04002309 case LA_10GHZ_LINK:
2310 phba->fc_linkspeed = LA_10GHZ_LINK;
2311 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002312 default:
2313 phba->fc_linkspeed = LA_UNKNW_LINK;
2314 break;
dea31012005-04-17 16:05:31 -05002315 }
2316
2317 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05002318 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05002319
2320 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05002321 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05002322
James Smart6a9c52c2009-10-02 15:16:51 -04002323 /* if npiv is enabled and this adapter supports npiv log
2324 * a message that npiv is not supported in this topology
2325 */
2326 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04002327 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2328 "1309 Link Up Event npiv not supported in loop "
2329 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002330 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05002331 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05002332 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002333
James Smart2e0fef82007-06-17 19:56:36 -05002334 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05002335 i = la->un.lilpBde64.tus.f.bdeSize;
2336
2337 if (i == 0) {
2338 phba->alpa_map[0] = 0;
2339 } else {
James Smarte8b62012007-08-02 11:10:09 -04002340 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05002341 int numalpa, j, k;
2342 union {
2343 uint8_t pamap[16];
2344 struct {
2345 uint32_t wd1;
2346 uint32_t wd2;
2347 uint32_t wd3;
2348 uint32_t wd4;
2349 } pa;
2350 } un;
2351 numalpa = phba->alpa_map[0];
2352 j = 0;
2353 while (j < numalpa) {
2354 memset(un.pamap, 0, 16);
2355 for (k = 1; j < numalpa; k++) {
2356 un.pamap[k - 1] =
2357 phba->alpa_map[j + 1];
2358 j++;
2359 if (k == 16)
2360 break;
2361 }
2362 /* Link Up Event ALPA map */
2363 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05002364 KERN_WARNING,
2365 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002366 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05002367 "ALPA map Data: x%x "
2368 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002369 un.pa.wd1, un.pa.wd2,
2370 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05002371 }
2372 }
2373 }
2374 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05002375 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04002376 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05002377 (phba->sli_rev == 3))
2378 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
2379 }
James Smart2e0fef82007-06-17 19:56:36 -05002380 vport->fc_myDID = phba->fc_pref_DID;
2381 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002382 }
James Smart92d7f7b2007-06-17 19:56:38 -05002383 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002384
2385 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05002386 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2387 if (!sparam_mbox)
2388 goto out;
2389
2390 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
2391 if (rc) {
2392 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2393 goto out;
2394 }
2395 sparam_mbox->vport = vport;
2396 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
2397 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
2398 if (rc == MBX_NOT_FINISHED) {
2399 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2400 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2401 kfree(mp);
2402 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2403 goto out;
dea31012005-04-17 16:05:31 -05002404 }
2405
James Smart6fb120a2009-05-22 14:52:59 -04002406 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2407 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2408 if (!cfglink_mbox)
2409 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05002410 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002411 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002412 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002413 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002414 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002415 if (rc == MBX_NOT_FINISHED) {
2416 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2417 goto out;
2418 }
2419 } else {
James Smart32b97932009-07-19 10:01:21 -04002420 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002421 /*
2422 * Add the driver's default FCF record at FCF index 0 now. This
2423 * is phase 1 implementation that support FCF index 0 and driver
2424 * defaults.
2425 */
James Smart45ed1192009-10-02 15:17:02 -04002426 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002427 fcf_record = kzalloc(sizeof(struct fcf_record),
2428 GFP_KERNEL);
2429 if (unlikely(!fcf_record)) {
2430 lpfc_printf_log(phba, KERN_ERR,
2431 LOG_MBOX | LOG_SLI,
2432 "2554 Could not allocate memmory for "
2433 "fcf record\n");
2434 rc = -ENODEV;
2435 goto out;
2436 }
2437
2438 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2439 LPFC_FCOE_FCF_DEF_INDEX);
2440 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2441 if (unlikely(rc)) {
2442 lpfc_printf_log(phba, KERN_ERR,
2443 LOG_MBOX | LOG_SLI,
2444 "2013 Could not manually add FCF "
2445 "record 0, status %d\n", rc);
2446 rc = -ENODEV;
2447 kfree(fcf_record);
2448 goto out;
2449 }
2450 kfree(fcf_record);
2451 }
2452 /*
2453 * The driver is expected to do FIP/FCF. Call the port
2454 * and get the FCF Table.
2455 */
James Smart32b97932009-07-19 10:01:21 -04002456 spin_lock_irq(&phba->hbalock);
2457 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2458 spin_unlock_irq(&phba->hbalock);
2459 return;
2460 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002461 /* This is the initial FCF discovery scan */
2462 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04002463 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002464 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2465 "2778 Start FCF table scan at linkup\n");
2466
2467 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2468 LPFC_FCOE_FCF_GET_FIRST);
2469 if (rc) {
2470 spin_lock_irq(&phba->hbalock);
2471 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
2472 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002473 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002474 }
dea31012005-04-17 16:05:31 -05002475 }
James Smart6fb120a2009-05-22 14:52:59 -04002476
2477 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002478out:
2479 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002480 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2481 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2482 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002483 lpfc_issue_clear_la(phba, vport);
2484 return;
dea31012005-04-17 16:05:31 -05002485}
2486
2487static void
James Smart84774a42008-08-24 21:50:06 -04002488lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002489{
dea31012005-04-17 16:05:31 -05002490 uint32_t control;
2491 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002492 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002493 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002494 if (phba->sli_rev <= LPFC_SLI_REV3) {
2495 control = readl(phba->HCregaddr);
2496 control |= HC_LAINT_ENA;
2497 writel(control, phba->HCregaddr);
2498 readl(phba->HCregaddr); /* flush */
2499 }
James Smart2e0fef82007-06-17 19:56:36 -05002500 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002501}
2502
James Smart84774a42008-08-24 21:50:06 -04002503static void
2504lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2505{
2506 lpfc_linkdown(phba);
2507 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002508 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002509 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2510}
2511
2512
dea31012005-04-17 16:05:31 -05002513/*
2514 * This routine handles processing a READ_LA mailbox
2515 * command upon completion. It is setup in the LPFC_MBOXQ
2516 * as the completion routine when the command is
2517 * handed off to the SLI layer.
2518 */
2519void
James Smart2e0fef82007-06-17 19:56:36 -05002520lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002521{
James Smart2e0fef82007-06-17 19:56:36 -05002522 struct lpfc_vport *vport = pmb->vport;
2523 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002524 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002525 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002526 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2527
James Smart0d2b6b82008-06-14 22:52:47 -04002528 /* Unblock ELS traffic */
2529 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002530 /* Check for error */
2531 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002532 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002533 "1307 READ_LA mbox error x%x state x%x\n",
2534 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002535 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002536 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002537 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2538 }
2539
James Smart04c68492009-05-22 14:52:52 -04002540 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002541
2542 memcpy(&phba->alpa_map[0], mp->virt, 128);
2543
James Smart2e0fef82007-06-17 19:56:36 -05002544 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002545 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002546 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002547 else
James Smart2e0fef82007-06-17 19:56:36 -05002548 vport->fc_flag &= ~FC_BYPASSED_MODE;
2549 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002550
James Smart0c287582009-06-10 17:22:56 -04002551 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002552 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002553 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002554 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002555 if (phba->fc_eventTag != 0)
2556 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002557 }
dea31012005-04-17 16:05:31 -05002558
2559 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05002560 spin_lock_irq(&phba->hbalock);
James Smart84774a42008-08-24 21:50:06 -04002561 if (la->mm)
2562 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2563 else
2564 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05002565 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002566
James Smart4d9ab992009-10-02 15:16:39 -04002567 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002568 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002569 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002570 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002571 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002572 "1306 Link Up Event in loop back mode "
2573 "x%x received Data: x%x x%x x%x x%x\n",
2574 la->eventTag, phba->fc_eventTag,
2575 la->granted_AL_PA, la->UlnkSpeed,
2576 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002577 } else {
2578 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002579 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002580 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002581 la->eventTag, phba->fc_eventTag,
2582 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002583 phba->alpa_map[0],
2584 la->mm, la->fa,
2585 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002586 }
James Smart92d7f7b2007-06-17 19:56:38 -05002587 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002588 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002589 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002590 if (phba->link_flag & LS_LOOPBACK_MODE) {
2591 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2592 "1308 Link Down Event in loop back mode "
2593 "x%x received "
2594 "Data: x%x x%x x%x\n",
2595 la->eventTag, phba->fc_eventTag,
2596 phba->pport->port_state, vport->fc_flag);
2597 }
2598 else {
2599 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002600 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002601 "Data: x%x x%x x%x x%x x%x\n",
2602 la->eventTag, phba->fc_eventTag,
2603 phba->pport->port_state, vport->fc_flag,
2604 la->mm, la->fa);
2605 }
2606 lpfc_mbx_issue_link_down(phba);
2607 }
2608 if (la->mm && la->attType == AT_LINK_UP) {
2609 if (phba->link_state != LPFC_LINK_DOWN) {
2610 phba->fc_stat.LinkDown++;
2611 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2612 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002613 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002614 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002615 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002616 lpfc_mbx_issue_link_down(phba);
2617 } else
2618 lpfc_enable_la(phba);
2619
2620 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2621 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2622 "Data: x%x x%x x%x\n",
2623 la->eventTag, phba->fc_eventTag,
2624 phba->pport->port_state, vport->fc_flag);
2625 /*
2626 * The cmnd that triggered this will be waiting for this
2627 * signal.
2628 */
2629 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2630 if (phba->wait_4_mlo_maint_flg) {
2631 phba->wait_4_mlo_maint_flg = 0;
2632 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002633 }
James Smart84774a42008-08-24 21:50:06 -04002634 }
2635
2636 if (la->fa) {
2637 if (la->mm)
2638 lpfc_issue_clear_la(phba, vport);
2639 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2640 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002641 }
2642
2643lpfc_mbx_cmpl_read_la_free_mbuf:
2644 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2645 kfree(mp);
2646 mempool_free(pmb, phba->mbox_mem_pool);
2647 return;
2648}
2649
2650/*
2651 * This routine handles processing a REG_LOGIN mailbox
2652 * command upon completion. It is setup in the LPFC_MBOXQ
2653 * as the completion routine when the command is
2654 * handed off to the SLI layer.
2655 */
2656void
James Smart2e0fef82007-06-17 19:56:36 -05002657lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002658{
James Smart2e0fef82007-06-17 19:56:36 -05002659 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002660 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002661 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05002662
dea31012005-04-17 16:05:31 -05002663 pmb->context1 = NULL;
2664
2665 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05002666 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05002667 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2668 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002669 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002670 /* decrement the node reference count held for this callback
2671 * function.
2672 */
James Smart329f9bc2007-04-25 09:53:01 -04002673 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002674
2675 return;
2676}
2677
James Smart92d7f7b2007-06-17 19:56:38 -05002678static void
2679lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2680{
James Smart04c68492009-05-22 14:52:52 -04002681 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002682 struct lpfc_vport *vport = pmb->vport;
2683 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2684
2685 switch (mb->mbxStatus) {
2686 case 0x0011:
2687 case 0x0020:
2688 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04002689 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2690 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2691 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002692 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002693 }
James Smart72100cc2010-02-12 14:43:01 -05002694 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002695 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05002696 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002697 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002698 vport->unreg_vpi_cmpl = VPORT_OK;
2699 mempool_free(pmb, phba->mbox_mem_pool);
2700 /*
2701 * This shost reference might have been taken at the beginning of
2702 * lpfc_vport_delete()
2703 */
James Smart1c6f4ef52009-11-18 15:40:49 -05002704 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05002705 scsi_host_put(shost);
2706}
2707
James Smartd7c255b2008-08-24 21:50:00 -04002708int
James Smart92d7f7b2007-06-17 19:56:38 -05002709lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2710{
2711 struct lpfc_hba *phba = vport->phba;
2712 LPFC_MBOXQ_t *mbox;
2713 int rc;
2714
2715 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2716 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002717 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002718
2719 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2720 mbox->vport = vport;
2721 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002722 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002723 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002724 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2725 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002726 mempool_free(mbox, phba->mbox_mem_pool);
2727 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002728 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002729 }
James Smartd7c255b2008-08-24 21:50:00 -04002730 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002731}
2732
2733static void
2734lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2735{
2736 struct lpfc_vport *vport = pmb->vport;
2737 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002738 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002739
2740 switch (mb->mbxStatus) {
2741 case 0x0011:
2742 case 0x9601:
2743 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002744 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2745 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2746 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002747 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2748 spin_lock_irq(shost->host_lock);
2749 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2750 spin_unlock_irq(shost->host_lock);
2751 vport->fc_myDID = 0;
2752 goto out;
2753 }
James Smart92d7f7b2007-06-17 19:56:38 -05002754
James Smart72100cc2010-02-12 14:43:01 -05002755 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002756 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002757 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002758 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002759 vport->num_disc_nodes = 0;
2760 /* go thru NPR list and issue ELS PLOGIs */
2761 if (vport->fc_npr_cnt)
2762 lpfc_els_disc_plogi(vport);
2763
2764 if (!vport->num_disc_nodes) {
2765 spin_lock_irq(shost->host_lock);
2766 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2767 spin_unlock_irq(shost->host_lock);
2768 lpfc_can_disctmo(vport);
2769 }
2770 vport->port_state = LPFC_VPORT_READY;
2771
2772out:
2773 mempool_free(pmb, phba->mbox_mem_pool);
2774 return;
2775}
2776
James Smart21e9a0a2009-05-22 14:53:21 -04002777/**
2778 * lpfc_create_static_vport - Read HBA config region to create static vports.
2779 * @phba: pointer to lpfc hba data structure.
2780 *
2781 * This routine issue a DUMP mailbox command for config region 22 to get
2782 * the list of static vports to be created. The function create vports
2783 * based on the information returned from the HBA.
2784 **/
2785void
2786lpfc_create_static_vport(struct lpfc_hba *phba)
2787{
2788 LPFC_MBOXQ_t *pmb = NULL;
2789 MAILBOX_t *mb;
2790 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04002791 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04002792 struct fc_vport_identifiers vport_id;
2793 struct fc_vport *new_fc_vport;
2794 struct Scsi_Host *shost;
2795 struct lpfc_vport *vport;
2796 uint16_t offset = 0;
2797 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04002798 struct lpfc_dmabuf *mp;
2799 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04002800
2801 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2802 if (!pmb) {
2803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2804 "0542 lpfc_create_static_vport failed to"
2805 " allocate mailbox memory\n");
2806 return;
2807 }
2808
2809 mb = &pmb->u.mb;
2810
2811 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2812 if (!vport_info) {
2813 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2814 "0543 lpfc_create_static_vport failed to"
2815 " allocate vport_info\n");
2816 mempool_free(pmb, phba->mbox_mem_pool);
2817 return;
2818 }
2819
2820 vport_buff = (uint8_t *) vport_info;
2821 do {
James Smart1c6834a2009-07-19 10:01:26 -04002822 if (lpfc_dump_static_vport(phba, pmb, offset))
2823 goto out;
2824
James Smart21e9a0a2009-05-22 14:53:21 -04002825 pmb->vport = phba->pport;
2826 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2827
2828 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2829 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2830 "0544 lpfc_create_static_vport failed to"
2831 " issue dump mailbox command ret 0x%x "
2832 "status 0x%x\n",
2833 rc, mb->mbxStatus);
2834 goto out;
2835 }
2836
James Smart1c6834a2009-07-19 10:01:26 -04002837 if (phba->sli_rev == LPFC_SLI_REV4) {
2838 byte_count = pmb->u.mqe.un.mb_words[5];
2839 mp = (struct lpfc_dmabuf *) pmb->context2;
2840 if (byte_count > sizeof(struct static_vport_info) -
2841 offset)
2842 byte_count = sizeof(struct static_vport_info)
2843 - offset;
2844 memcpy(vport_buff + offset, mp->virt, byte_count);
2845 offset += byte_count;
2846 } else {
2847 if (mb->un.varDmp.word_cnt >
2848 sizeof(struct static_vport_info) - offset)
2849 mb->un.varDmp.word_cnt =
2850 sizeof(struct static_vport_info)
2851 - offset;
2852 byte_count = mb->un.varDmp.word_cnt;
2853 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2854 vport_buff + offset,
2855 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04002856
James Smart1c6834a2009-07-19 10:01:26 -04002857 offset += byte_count;
2858 }
James Smart21e9a0a2009-05-22 14:53:21 -04002859
James Smart1c6834a2009-07-19 10:01:26 -04002860 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04002861 offset < sizeof(struct static_vport_info));
2862
2863
2864 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2865 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2866 != VPORT_INFO_REV)) {
2867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2868 "0545 lpfc_create_static_vport bad"
2869 " information header 0x%x 0x%x\n",
2870 le32_to_cpu(vport_info->signature),
2871 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2872
2873 goto out;
2874 }
2875
2876 shost = lpfc_shost_from_vport(phba->pport);
2877
2878 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2879 memset(&vport_id, 0, sizeof(vport_id));
2880 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2881 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2882 if (!vport_id.port_name || !vport_id.node_name)
2883 continue;
2884
2885 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2886 vport_id.vport_type = FC_PORTTYPE_NPIV;
2887 vport_id.disable = false;
2888 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2889
2890 if (!new_fc_vport) {
2891 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2892 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04002893 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04002894 continue;
2895 }
2896
2897 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2898 vport->vport_flag |= STATIC_VPORT;
2899 }
2900
2901out:
James Smart21e9a0a2009-05-22 14:53:21 -04002902 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04002903 if (rc != MBX_TIMEOUT) {
2904 if (pmb->context2) {
2905 mp = (struct lpfc_dmabuf *) pmb->context2;
2906 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2907 kfree(mp);
2908 }
James Smart21e9a0a2009-05-22 14:53:21 -04002909 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002910 }
James Smart21e9a0a2009-05-22 14:53:21 -04002911
2912 return;
2913}
2914
dea31012005-04-17 16:05:31 -05002915/*
2916 * This routine handles processing a Fabric REG_LOGIN mailbox
2917 * command upon completion. It is setup in the LPFC_MBOXQ
2918 * as the completion routine when the command is
2919 * handed off to the SLI layer.
2920 */
2921void
James Smart2e0fef82007-06-17 19:56:36 -05002922lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002923{
James Smart92d7f7b2007-06-17 19:56:38 -05002924 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04002925 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002926 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05002927 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002928
James Smart549e55c2007-08-02 11:09:51 -04002929 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04002930 pmb->context1 = NULL;
2931 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05002932 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04002933 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2934 "0258 Register Fabric login error: 0x%x\n",
2935 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05002936 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2937 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002938 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002939
James Smart92d7f7b2007-06-17 19:56:38 -05002940 if (phba->fc_topology == TOPOLOGY_LOOP) {
2941 /* FLOGI failed, use loop map to make discovery list */
2942 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002943
James Smart92d7f7b2007-06-17 19:56:38 -05002944 /* Start discovery */
2945 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05002946 /* Decrement the reference count to ndlp after the
2947 * reference to the ndlp are done.
2948 */
2949 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05002950 return;
2951 }
2952
2953 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05002954 /* Decrement the reference count to ndlp after the reference
2955 * to the ndlp are done.
2956 */
2957 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002958 return;
2959 }
2960
dea31012005-04-17 16:05:31 -05002961 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04002962 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05002963 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002964 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002965
James Smart2e0fef82007-06-17 19:56:36 -05002966 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart21e9a0a2009-05-22 14:53:21 -04002967 lpfc_start_fdiscs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002968 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05002969 }
2970
2971 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2972 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002973 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002974
2975 /* Drop the reference count from the mbox at the end after
2976 * all the current reference to the ndlp have been done.
2977 */
2978 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002979 return;
2980}
2981
2982/*
2983 * This routine handles processing a NameServer REG_LOGIN mailbox
2984 * command upon completion. It is setup in the LPFC_MBOXQ
2985 * as the completion routine when the command is
2986 * handed off to the SLI layer.
2987 */
2988void
James Smart2e0fef82007-06-17 19:56:36 -05002989lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002990{
James Smart21e9a0a2009-05-22 14:53:21 -04002991 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002992 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2993 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2994 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002995
2996 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05002997out:
James Smart21e9a0a2009-05-22 14:53:21 -04002998 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2999 "0260 Register NameServer error: 0x%x\n",
3000 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003001 /* decrement the node reference count held for this
3002 * callback function.
3003 */
James Smart329f9bc2007-04-25 09:53:01 -04003004 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003005 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3006 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003007 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003008
3009 /* If no other thread is using the ndlp, free it */
3010 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003011
James Smart92d7f7b2007-06-17 19:56:38 -05003012 if (phba->fc_topology == TOPOLOGY_LOOP) {
3013 /*
3014 * RegLogin failed, use loop map to make discovery
3015 * list
3016 */
3017 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003018
James Smart92d7f7b2007-06-17 19:56:38 -05003019 /* Start discovery */
3020 lpfc_disc_start(vport);
3021 return;
3022 }
3023 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003024 return;
3025 }
3026
3027 pmb->context1 = NULL;
3028
dea31012005-04-17 16:05:31 -05003029 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003030 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003031 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003032 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003033
James Smart2e0fef82007-06-17 19:56:36 -05003034 if (vport->port_state < LPFC_VPORT_READY) {
3035 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003036 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3037 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3038 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3039 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3040 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3041
3042 /* Issue SCR just before NameServer GID_FT Query */
3043 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003044 }
3045
James Smart2e0fef82007-06-17 19:56:36 -05003046 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003047 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003048 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003049 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003050 goto out;
dea31012005-04-17 16:05:31 -05003051 }
3052
James Smartfa4066b2008-01-11 01:53:27 -05003053 /* decrement the node reference count held for this
3054 * callback function.
3055 */
James Smart329f9bc2007-04-25 09:53:01 -04003056 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003057 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3058 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003059 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003060
3061 return;
3062}
3063
3064static void
James Smart2e0fef82007-06-17 19:56:36 -05003065lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003066{
James Smart2e0fef82007-06-17 19:56:36 -05003067 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3068 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003069 struct lpfc_rport_data *rdata;
3070 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003071 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003072
3073 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003074 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3075 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003076 rport_ids.port_id = ndlp->nlp_DID;
3077 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003078
James Smart329f9bc2007-04-25 09:53:01 -04003079 /*
3080 * We leave our node pointer in rport->dd_data when we unregister a
3081 * FCP target port. But fc_remote_port_add zeros the space to which
3082 * rport->dd_data points. So, if we're reusing a previously
3083 * registered port, drop the reference that we took the last time we
3084 * registered the port.
3085 */
3086 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003087 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003088 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003089
3090 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3091 "rport add: did:x%x flg:x%x type x%x",
3092 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3093
James Smart2e0fef82007-06-17 19:56:36 -05003094 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003095 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003096 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3097 "Warning: fc_remote_port_add failed\n");
3098 return;
3099 }
3100
3101 /* initialize static port data */
3102 rport->maxframe_size = ndlp->nlp_maxframe;
3103 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003104 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003105 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003106
3107 if (ndlp->nlp_type & NLP_FCP_TARGET)
3108 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3109 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3110 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3111
3112
3113 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3114 fc_remote_port_rolechg(rport, rport_ids.roles);
3115
James Smart071fbd3d2006-04-15 11:53:20 -04003116 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003117 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003118 ndlp->nlp_sid = rport->scsi_target_id;
3119 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003120 return;
3121}
3122
3123static void
James Smart2e0fef82007-06-17 19:56:36 -05003124lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003125{
3126 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003127
James Smart858c9f62007-06-17 19:56:39 -05003128 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3129 "rport delete: did:x%x flg:x%x type x%x",
3130 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3131
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003132 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003133
3134 return;
3135}
3136
James Smartde0c5b32007-04-25 09:52:27 -04003137static void
James Smart2e0fef82007-06-17 19:56:36 -05003138lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003139{
James Smart2e0fef82007-06-17 19:56:36 -05003140 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3141
3142 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003143 switch (state) {
3144 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003145 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003146 break;
3147 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003148 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003149 break;
3150 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003151 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003152 break;
3153 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003154 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003155 break;
3156 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003157 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003158 break;
3159 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003160 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003161 break;
3162 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003163 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003164 break;
3165 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003166 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003167 break;
3168 }
James Smart2e0fef82007-06-17 19:56:36 -05003169 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003170}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003171
James Smartde0c5b32007-04-25 09:52:27 -04003172static void
James Smart2e0fef82007-06-17 19:56:36 -05003173lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003174 int old_state, int new_state)
3175{
James Smart2e0fef82007-06-17 19:56:36 -05003176 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3177
James Smartde0c5b32007-04-25 09:52:27 -04003178 if (new_state == NLP_STE_UNMAPPED_NODE) {
3179 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
3180 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3181 ndlp->nlp_type |= NLP_FC_NODE;
3182 }
3183 if (new_state == NLP_STE_MAPPED_NODE)
3184 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3185 if (new_state == NLP_STE_NPR_NODE)
3186 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003187
James Smartde0c5b32007-04-25 09:52:27 -04003188 /* Transport interface */
3189 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3190 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003191 vport->phba->nport_event_cnt++;
3192 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003193 }
dea31012005-04-17 16:05:31 -05003194
James Smartde0c5b32007-04-25 09:52:27 -04003195 if (new_state == NLP_STE_MAPPED_NODE ||
3196 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003197 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003198 /*
3199 * Tell the fc transport about the port, if we haven't
3200 * already. If we have, and it's a scsi entity, be
3201 * sure to unblock any attached scsi devices
3202 */
3203 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003204 }
James Smartea2151b2008-09-07 11:52:10 -04003205 if ((new_state == NLP_STE_MAPPED_NODE) &&
3206 (vport->stat_data_enabled)) {
3207 /*
3208 * A new target is discovered, if there is no buffer for
3209 * statistical data collection allocate buffer.
3210 */
3211 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3212 sizeof(struct lpfc_scsicmd_bkt),
3213 GFP_KERNEL);
3214
3215 if (!ndlp->lat_data)
3216 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3217 "0286 lpfc_nlp_state_cleanup failed to "
3218 "allocate statistical data buffer DID "
3219 "0x%x\n", ndlp->nlp_DID);
3220 }
James Smart858c9f62007-06-17 19:56:39 -05003221 /*
3222 * if we added to Mapped list, but the remote port
3223 * registration failed or assigned a target id outside
3224 * our presentable range - move the node to the
3225 * Unmapped List
3226 */
James Smartde0c5b32007-04-25 09:52:27 -04003227 if (new_state == NLP_STE_MAPPED_NODE &&
3228 (!ndlp->rport ||
3229 ndlp->rport->scsi_target_id == -1 ||
3230 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003231 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003232 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003233 spin_unlock_irq(shost->host_lock);
3234 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003235 }
James Smartde0c5b32007-04-25 09:52:27 -04003236}
3237
James Smart685f0bf2007-04-25 09:53:08 -04003238static char *
3239lpfc_nlp_state_name(char *buffer, size_t size, int state)
3240{
3241 static char *states[] = {
3242 [NLP_STE_UNUSED_NODE] = "UNUSED",
3243 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
3244 [NLP_STE_ADISC_ISSUE] = "ADISC",
3245 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
3246 [NLP_STE_PRLI_ISSUE] = "PRLI",
3247 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
3248 [NLP_STE_MAPPED_NODE] = "MAPPED",
3249 [NLP_STE_NPR_NODE] = "NPR",
3250 };
3251
James Smart311464e2007-08-02 11:10:37 -04003252 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04003253 strlcpy(buffer, states[state], size);
3254 else
3255 snprintf(buffer, size, "unknown (%d)", state);
3256 return buffer;
3257}
3258
James Smartde0c5b32007-04-25 09:52:27 -04003259void
James Smart2e0fef82007-06-17 19:56:36 -05003260lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3261 int state)
James Smartde0c5b32007-04-25 09:52:27 -04003262{
James Smart2e0fef82007-06-17 19:56:36 -05003263 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04003264 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04003265 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04003266
James Smarte8b62012007-08-02 11:10:09 -04003267 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3268 "0904 NPort state transition x%06x, %s -> %s\n",
3269 ndlp->nlp_DID,
3270 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
3271 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05003272
3273 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3274 "node statechg did:x%x old:%d ste:%d",
3275 ndlp->nlp_DID, old_state, state);
3276
James Smartde0c5b32007-04-25 09:52:27 -04003277 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04003278 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05003279 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003280 if (old_state == NLP_STE_UNMAPPED_NODE) {
3281 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
3282 ndlp->nlp_type &= ~NLP_FC_NODE;
3283 }
3284
James Smart685f0bf2007-04-25 09:53:08 -04003285 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05003286 spin_lock_irq(shost->host_lock);
3287 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3288 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003289 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05003290 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04003291
3292 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05003293 lpfc_nlp_counters(vport, state, 1);
3294 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04003295}
3296
3297void
James Smarte47c9092008-02-08 18:49:26 -05003298lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3299{
3300 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3301
3302 if (list_empty(&ndlp->nlp_listp)) {
3303 spin_lock_irq(shost->host_lock);
3304 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3305 spin_unlock_irq(shost->host_lock);
3306 }
3307}
3308
3309void
James Smart2e0fef82007-06-17 19:56:36 -05003310lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003311{
James Smart2e0fef82007-06-17 19:56:36 -05003312 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3313
James Smart0d2b6b82008-06-14 22:52:47 -04003314 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003315 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05003316 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3317 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003318 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05003319 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003320 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05003321 NLP_STE_UNUSED_NODE);
3322}
3323
Adrian Bunk4d9db012008-02-14 23:24:02 +02003324static void
James Smarte47c9092008-02-08 18:49:26 -05003325lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3326{
James Smart0d2b6b82008-06-14 22:52:47 -04003327 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003328 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3329 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3330 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3331 NLP_STE_UNUSED_NODE);
3332}
James Smart109f6ed2008-12-04 22:39:08 -05003333/**
James Smart3621a712009-04-06 18:47:14 -04003334 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05003335 * @vport: Pointer to Virtual Port object.
3336 * @ndlp: Pointer to FC node object.
3337 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04003338 *
3339 * This function is always called when node object need to be initialized.
3340 * It initializes all the fields of the node object. Although the reference
3341 * to phba from @ndlp can be obtained indirectly through it's reference to
3342 * @vport, a direct reference to phba is taken here by @ndlp. This is due
3343 * to the life-span of the @ndlp might go beyond the existence of @vport as
3344 * the final release of ndlp is determined by its reference count. And, the
3345 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05003346 **/
3347static inline void
3348lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3349 uint32_t did)
3350{
3351 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3352 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3353 init_timer(&ndlp->nlp_delayfunc);
3354 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3355 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3356 ndlp->nlp_DID = did;
3357 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04003358 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05003359 ndlp->nlp_sid = NLP_NO_SID;
3360 kref_init(&ndlp->kref);
3361 NLP_INT_NODE_ACT(ndlp);
3362 atomic_set(&ndlp->cmd_pending, 0);
3363 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
3364}
James Smarte47c9092008-02-08 18:49:26 -05003365
3366struct lpfc_nodelist *
3367lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3368 int state)
3369{
3370 struct lpfc_hba *phba = vport->phba;
3371 uint32_t did;
3372 unsigned long flags;
3373
3374 if (!ndlp)
3375 return NULL;
3376
3377 spin_lock_irqsave(&phba->ndlp_lock, flags);
3378 /* The ndlp should not be in memory free mode */
3379 if (NLP_CHK_FREE_REQ(ndlp)) {
3380 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3381 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3382 "0277 lpfc_enable_node: ndlp:x%p "
3383 "usgmap:x%x refcnt:%d\n",
3384 (void *)ndlp, ndlp->nlp_usg_map,
3385 atomic_read(&ndlp->kref.refcount));
3386 return NULL;
3387 }
3388 /* The ndlp should not already be in active mode */
3389 if (NLP_CHK_NODE_ACT(ndlp)) {
3390 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3391 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3392 "0278 lpfc_enable_node: ndlp:x%p "
3393 "usgmap:x%x refcnt:%d\n",
3394 (void *)ndlp, ndlp->nlp_usg_map,
3395 atomic_read(&ndlp->kref.refcount));
3396 return NULL;
3397 }
3398
3399 /* Keep the original DID */
3400 did = ndlp->nlp_DID;
3401
3402 /* re-initialize ndlp except of ndlp linked list pointer */
3403 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3404 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003405 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003406
3407 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3408
3409 if (state != NLP_STE_UNUSED_NODE)
3410 lpfc_nlp_set_state(vport, ndlp, state);
3411
3412 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3413 "node enable: did:x%x",
3414 ndlp->nlp_DID, 0, 0);
3415 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003416}
3417
3418void
James Smart2e0fef82007-06-17 19:56:36 -05003419lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003420{
James Smart87af33f2007-10-27 13:37:43 -04003421 /*
James Smartfa4066b2008-01-11 01:53:27 -05003422 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003423 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003424 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3425 * until ALL other outstanding threads have completed. We check
3426 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003427 */
James Smartfa4066b2008-01-11 01:53:27 -05003428 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3429 return;
James Smart51ef4c22007-08-02 11:10:31 -04003430 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003431 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003432 return;
dea31012005-04-17 16:05:31 -05003433}
3434
3435/*
3436 * Start / ReStart rescue timer for Discovery / RSCN handling
3437 */
3438void
James Smart2e0fef82007-06-17 19:56:36 -05003439lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003440{
James Smart2e0fef82007-06-17 19:56:36 -05003441 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3442 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003443 uint32_t tmo;
3444
James Smart2e0fef82007-06-17 19:56:36 -05003445 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003446 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003447 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3448 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003449 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003450 * FC spec states we need 3 * ratov for CT requests
3451 */
3452 tmo = ((phba->fc_ratov * 3) + 3);
3453 }
dea31012005-04-17 16:05:31 -05003454
James Smart858c9f62007-06-17 19:56:39 -05003455
3456 if (!timer_pending(&vport->fc_disctmo)) {
3457 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3458 "set disc timer: tmo:x%x state:x%x flg:x%x",
3459 tmo, vport->port_state, vport->fc_flag);
3460 }
3461
James Smart2e0fef82007-06-17 19:56:36 -05003462 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3463 spin_lock_irq(shost->host_lock);
3464 vport->fc_flag |= FC_DISC_TMO;
3465 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003466
3467 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003468 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3469 "0247 Start Discovery Timer state x%x "
3470 "Data: x%x x%lx x%x x%x\n",
3471 vport->port_state, tmo,
3472 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3473 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003474
3475 return;
3476}
3477
3478/*
3479 * Cancel rescue timer for Discovery / RSCN handling
3480 */
3481int
James Smart2e0fef82007-06-17 19:56:36 -05003482lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003483{
James Smart2e0fef82007-06-17 19:56:36 -05003484 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003485 unsigned long iflags;
3486
James Smart858c9f62007-06-17 19:56:39 -05003487 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3488 "can disc timer: state:x%x rtry:x%x flg:x%x",
3489 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3490
dea31012005-04-17 16:05:31 -05003491 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003492 if (vport->fc_flag & FC_DISC_TMO) {
3493 spin_lock_irqsave(shost->host_lock, iflags);
3494 vport->fc_flag &= ~FC_DISC_TMO;
3495 spin_unlock_irqrestore(shost->host_lock, iflags);
3496 del_timer_sync(&vport->fc_disctmo);
3497 spin_lock_irqsave(&vport->work_port_lock, iflags);
3498 vport->work_port_events &= ~WORKER_DISC_TMO;
3499 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003500 }
3501
3502 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003503 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3504 "0248 Cancel Discovery Timer state x%x "
3505 "Data: x%x x%x x%x\n",
3506 vport->port_state, vport->fc_flag,
3507 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003508 return 0;
dea31012005-04-17 16:05:31 -05003509}
3510
3511/*
3512 * Check specified ring for outstanding IOCB on the SLI queue
3513 * Return true if iocb matches the specified nport
3514 */
3515int
James Smart2e0fef82007-06-17 19:56:36 -05003516lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3517 struct lpfc_sli_ring *pring,
3518 struct lpfc_iocbq *iocb,
3519 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003520{
James Smart2e0fef82007-06-17 19:56:36 -05003521 struct lpfc_sli *psli = &phba->sli;
3522 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003523 struct lpfc_vport *vport = ndlp->vport;
3524
3525 if (iocb->vport != vport)
3526 return 0;
3527
dea31012005-04-17 16:05:31 -05003528 if (pring->ringno == LPFC_ELS_RING) {
3529 switch (icmd->ulpCommand) {
3530 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003531 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003532 return 1;
dea31012005-04-17 16:05:31 -05003533 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003534 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3535 return 1;
dea31012005-04-17 16:05:31 -05003536 case CMD_XMIT_ELS_RSP64_CX:
3537 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003538 return 1;
dea31012005-04-17 16:05:31 -05003539 }
James Smarta4bc3372006-12-02 13:34:16 -05003540 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003541
3542 } else if (pring->ringno == psli->fcp_ring) {
3543 /* Skip match check if waiting to relogin to FCP target */
3544 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003545 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003546 return 0;
dea31012005-04-17 16:05:31 -05003547 }
3548 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003549 return 1;
dea31012005-04-17 16:05:31 -05003550 }
3551 } else if (pring->ringno == psli->next_ring) {
3552
3553 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003554 return 0;
dea31012005-04-17 16:05:31 -05003555}
3556
3557/*
3558 * Free resources / clean up outstanding I/Os
3559 * associated with nlp_rpi in the LPFC_NODELIST entry.
3560 */
3561static int
James Smart2e0fef82007-06-17 19:56:36 -05003562lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003563{
James Smart2534ba72007-04-25 09:52:20 -04003564 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003565 struct lpfc_sli *psli;
3566 struct lpfc_sli_ring *pring;
3567 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003568 uint32_t i;
dea31012005-04-17 16:05:31 -05003569
James Smart92d7f7b2007-06-17 19:56:38 -05003570 lpfc_fabric_abort_nport(ndlp);
3571
dea31012005-04-17 16:05:31 -05003572 /*
3573 * Everything that matches on txcmplq will be returned
3574 * by firmware with a no rpi error.
3575 */
3576 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003577 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003578 /* Now process each ring */
3579 for (i = 0; i < psli->num_rings; i++) {
3580 pring = &psli->ring[i];
3581
James Smart2e0fef82007-06-17 19:56:36 -05003582 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003583 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003584 list) {
dea31012005-04-17 16:05:31 -05003585 /*
3586 * Check to see if iocb matches the nport we are
3587 * looking for
3588 */
James Smart92d7f7b2007-06-17 19:56:38 -05003589 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3590 ndlp))) {
dea31012005-04-17 16:05:31 -05003591 /* It matches, so deque and call compl
3592 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003593 list_move_tail(&iocb->list,
3594 &completions);
dea31012005-04-17 16:05:31 -05003595 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003596 }
3597 }
James Smart2e0fef82007-06-17 19:56:36 -05003598 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003599 }
3600 }
James Smart2534ba72007-04-25 09:52:20 -04003601
James Smarta257bf92009-04-06 18:48:10 -04003602 /* Cancel all the IOCBs from the completions list */
3603 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3604 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003605
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003606 return 0;
dea31012005-04-17 16:05:31 -05003607}
3608
3609/*
3610 * Free rpi associated with LPFC_NODELIST entry.
3611 * This routine is called from lpfc_freenode(), when we are removing
3612 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3613 * LOGO that completes successfully, and we are waiting to PLOGI back
3614 * to the remote NPort. In addition, it is called after we receive
3615 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3616 * we are waiting to PLOGI back to the remote NPort.
3617 */
3618int
James Smart2e0fef82007-06-17 19:56:36 -05003619lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003620{
James Smart2e0fef82007-06-17 19:56:36 -05003621 struct lpfc_hba *phba = vport->phba;
3622 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003623 int rc;
3624
James Smart21e9a0a2009-05-22 14:53:21 -04003625 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003626 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3627 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003628 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003629 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003630 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003631 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003632 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003633 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003634 }
dea31012005-04-17 16:05:31 -05003635 lpfc_no_rpi(phba, ndlp);
3636 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003637 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003638 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003639 return 1;
3640 }
3641 return 0;
3642}
3643
James Smartecfd03c2010-02-12 14:41:27 -05003644/**
3645 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
3646 * @phba: pointer to lpfc hba data structure.
3647 *
3648 * This routine is invoked to unregister all the currently registered RPIs
3649 * to the HBA.
3650 **/
3651void
3652lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
3653{
3654 struct lpfc_vport **vports;
3655 struct lpfc_nodelist *ndlp;
3656 struct Scsi_Host *shost;
3657 int i;
3658
3659 vports = lpfc_create_vport_work_array(phba);
3660 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3661 shost = lpfc_shost_from_vport(vports[i]);
3662 spin_lock_irq(shost->host_lock);
3663 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003664 if (ndlp->nlp_flag & NLP_RPI_VALID) {
3665 /* The mempool_alloc might sleep */
3666 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05003667 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05003668 spin_lock_irq(shost->host_lock);
3669 }
James Smartecfd03c2010-02-12 14:41:27 -05003670 }
3671 spin_unlock_irq(shost->host_lock);
3672 }
3673 lpfc_destroy_vport_work_array(phba, vports);
3674}
3675
James Smart92d7f7b2007-06-17 19:56:38 -05003676void
3677lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3678{
3679 struct lpfc_hba *phba = vport->phba;
3680 LPFC_MBOXQ_t *mbox;
3681 int rc;
3682
3683 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3684 if (mbox) {
3685 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3686 mbox->vport = vport;
3687 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003688 mbox->context1 = NULL;
3689 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003690 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003691 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003692
3693 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3694 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3695 "1836 Could not issue "
3696 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003697 }
3698}
3699
3700void
3701lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3702{
3703 struct lpfc_hba *phba = vport->phba;
3704 LPFC_MBOXQ_t *mbox;
3705 int rc;
3706
3707 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3708 if (mbox) {
3709 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3710 mbox->vport = vport;
3711 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003712 mbox->context1 = NULL;
3713 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003714 if (rc != MBX_TIMEOUT)
3715 mempool_free(mbox, phba->mbox_mem_pool);
3716
3717 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003718 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3719 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003720 "unreg_did (default rpis) status %d\n",
3721 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003722 }
3723}
3724
dea31012005-04-17 16:05:31 -05003725/*
3726 * Free resources associated with LPFC_NODELIST entry
3727 * so it can be freed.
3728 */
3729static int
James Smart2e0fef82007-06-17 19:56:36 -05003730lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003731{
James Smart2e0fef82007-06-17 19:56:36 -05003732 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3733 struct lpfc_hba *phba = vport->phba;
3734 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003735 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003736
3737 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003738 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3739 "0900 Cleanup node for NPort x%x "
3740 "Data: x%x x%x x%x\n",
3741 ndlp->nlp_DID, ndlp->nlp_flag,
3742 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003743 if (NLP_CHK_FREE_REQ(ndlp)) {
3744 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3745 "0280 lpfc_cleanup_node: ndlp:x%p "
3746 "usgmap:x%x refcnt:%d\n",
3747 (void *)ndlp, ndlp->nlp_usg_map,
3748 atomic_read(&ndlp->kref.refcount));
3749 lpfc_dequeue_node(vport, ndlp);
3750 } else {
3751 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3752 "0281 lpfc_cleanup_node: ndlp:x%p "
3753 "usgmap:x%x refcnt:%d\n",
3754 (void *)ndlp, ndlp->nlp_usg_map,
3755 atomic_read(&ndlp->kref.refcount));
3756 lpfc_disable_node(vport, ndlp);
3757 }
dea31012005-04-17 16:05:31 -05003758
dea31012005-04-17 16:05:31 -05003759 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3760 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003761 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003762 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3763 mb->context2 = NULL;
3764 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3765 }
3766 }
James Smart33ccf8d2006-08-17 11:57:58 -04003767
James Smart2e0fef82007-06-17 19:56:36 -05003768 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003769 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003770 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003771 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003772 mp = (struct lpfc_dmabuf *) (mb->context1);
3773 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003774 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003775 kfree(mp);
3776 }
3777 list_del(&mb->list);
3778 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003779 /* We shall not invoke the lpfc_nlp_put to decrement
3780 * the ndlp reference count as we are in the process
3781 * of lpfc_nlp_release.
3782 */
dea31012005-04-17 16:05:31 -05003783 }
3784 }
James Smart2e0fef82007-06-17 19:56:36 -05003785 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003786
James Smarte47c9092008-02-08 18:49:26 -05003787 lpfc_els_abort(phba, ndlp);
3788
James Smart2e0fef82007-06-17 19:56:36 -05003789 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003790 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003791 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003792
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003793 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003794 del_timer_sync(&ndlp->nlp_delayfunc);
3795
James Smart0d2b6b82008-06-14 22:52:47 -04003796 list_del_init(&ndlp->els_retry_evt.evt_listp);
3797 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003798
James Smart2e0fef82007-06-17 19:56:36 -05003799 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003800
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003801 return 0;
dea31012005-04-17 16:05:31 -05003802}
3803
3804/*
3805 * Check to see if we can free the nlp back to the freelist.
3806 * If we are in the middle of using the nlp in the discovery state
3807 * machine, defer the free till we reach the end of the state machine.
3808 */
James Smart329f9bc2007-04-25 09:53:01 -04003809static void
James Smart2e0fef82007-06-17 19:56:36 -05003810lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003811{
James Smarta8adb832007-10-27 13:37:53 -04003812 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003813 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003814 LPFC_MBOXQ_t *mbox;
3815 int rc;
dea31012005-04-17 16:05:31 -05003816
James Smart0d2b6b82008-06-14 22:52:47 -04003817 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003818 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3819 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003820 /* For this case we need to cleanup the default rpi
3821 * allocated by the firmware.
3822 */
3823 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3824 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003825 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003826 (uint8_t *) &vport->fc_sparam, mbox, 0);
3827 if (rc) {
3828 mempool_free(mbox, phba->mbox_mem_pool);
3829 }
3830 else {
3831 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3832 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3833 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003834 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003835 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3836 if (rc == MBX_NOT_FINISHED) {
3837 mempool_free(mbox, phba->mbox_mem_pool);
3838 }
3839 }
3840 }
3841 }
James Smart2e0fef82007-06-17 19:56:36 -05003842 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003843
James Smart2e0fef82007-06-17 19:56:36 -05003844 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003845 * We can get here with a non-NULL ndlp->rport because when we
3846 * unregister a rport we don't break the rport/node linkage. So if we
3847 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003848 */
James Smart92d7f7b2007-06-17 19:56:38 -05003849 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003850 rdata = ndlp->rport->dd_data;
3851 rdata->pnode = NULL;
3852 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003853 }
dea31012005-04-17 16:05:31 -05003854}
3855
3856static int
James Smart2e0fef82007-06-17 19:56:36 -05003857lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3858 uint32_t did)
dea31012005-04-17 16:05:31 -05003859{
James Smart2e0fef82007-06-17 19:56:36 -05003860 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003861
3862 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003863 return 0;
dea31012005-04-17 16:05:31 -05003864
dea31012005-04-17 16:05:31 -05003865 /* First check for Direct match */
3866 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003867 return 1;
dea31012005-04-17 16:05:31 -05003868
3869 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003870 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003871 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003872 return 0;
dea31012005-04-17 16:05:31 -05003873 }
3874
3875 matchdid.un.word = did;
3876 ndlpdid.un.word = ndlp->nlp_DID;
3877 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3878 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3879 (mydid.un.b.area == matchdid.un.b.area)) {
3880 if ((ndlpdid.un.b.domain == 0) &&
3881 (ndlpdid.un.b.area == 0)) {
3882 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003883 return 1;
dea31012005-04-17 16:05:31 -05003884 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003885 return 0;
dea31012005-04-17 16:05:31 -05003886 }
3887
3888 matchdid.un.word = ndlp->nlp_DID;
3889 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3890 (mydid.un.b.area == ndlpdid.un.b.area)) {
3891 if ((matchdid.un.b.domain == 0) &&
3892 (matchdid.un.b.area == 0)) {
3893 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003894 return 1;
dea31012005-04-17 16:05:31 -05003895 }
3896 }
3897 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003898 return 0;
dea31012005-04-17 16:05:31 -05003899}
3900
James Smart685f0bf2007-04-25 09:53:08 -04003901/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003902static struct lpfc_nodelist *
3903__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003904{
James Smart2fb9bd82006-12-02 13:33:57 -05003905 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003906 uint32_t data1;
3907
James Smart2e0fef82007-06-17 19:56:36 -05003908 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3909 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04003910 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3911 ((uint32_t) ndlp->nlp_xri << 16) |
3912 ((uint32_t) ndlp->nlp_type << 8) |
3913 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04003914 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3915 "0929 FIND node DID "
3916 "Data: x%p x%x x%x x%x\n",
3917 ndlp, ndlp->nlp_DID,
3918 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04003919 return ndlp;
dea31012005-04-17 16:05:31 -05003920 }
3921 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003922
dea31012005-04-17 16:05:31 -05003923 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04003924 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3925 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05003926 return NULL;
3927}
3928
3929struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003930lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003931{
James Smart2e0fef82007-06-17 19:56:36 -05003932 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003933 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003934
James Smart2e0fef82007-06-17 19:56:36 -05003935 spin_lock_irq(shost->host_lock);
3936 ndlp = __lpfc_findnode_did(vport, did);
3937 spin_unlock_irq(shost->host_lock);
3938 return ndlp;
3939}
3940
3941struct lpfc_nodelist *
3942lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3943{
3944 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3945 struct lpfc_nodelist *ndlp;
3946
3947 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003948 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05003949 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
3950 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05003951 return NULL;
3952 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05003953 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003954 if (!ndlp)
3955 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003956 lpfc_nlp_init(vport, ndlp, did);
3957 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3958 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003959 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003960 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003961 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05003962 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3963 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
3964 if (!ndlp)
3965 return NULL;
3966 spin_lock_irq(shost->host_lock);
3967 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3968 spin_unlock_irq(shost->host_lock);
3969 return ndlp;
dea31012005-04-17 16:05:31 -05003970 }
James Smarte47c9092008-02-08 18:49:26 -05003971
James Smart58da1ff2008-04-07 10:15:56 -04003972 if ((vport->fc_flag & FC_RSCN_MODE) &&
3973 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003974 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04003975 /* If we've already recieved a PLOGI from this NPort
3976 * we don't need to try to discover it again.
3977 */
3978 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
3979 return NULL;
3980
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003981 /* Since this node is marked for discovery,
3982 * delay timeout is not needed.
3983 */
James Smart0d2b6b82008-06-14 22:52:47 -04003984 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04003985 spin_lock_irq(shost->host_lock);
3986 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3987 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04003988 } else
dea31012005-04-17 16:05:31 -05003989 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003990 } else {
James Smart87af33f2007-10-27 13:37:43 -04003991 /* If we've already recieved a PLOGI from this NPort,
3992 * or we are already in the process of discovery on it,
3993 * we don't need to try to discover it again.
3994 */
James Smart685f0bf2007-04-25 09:53:08 -04003995 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04003996 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3997 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05003998 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003999 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4000 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004001 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004002 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004003 }
4004 return ndlp;
4005}
4006
4007/* Build a list of nodes to discover based on the loopmap */
4008void
James Smart2e0fef82007-06-17 19:56:36 -05004009lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004010{
James Smart2e0fef82007-06-17 19:56:36 -05004011 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004012 int j;
4013 uint32_t alpa, index;
4014
James Smart2e0fef82007-06-17 19:56:36 -05004015 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004016 return;
James Smart2e0fef82007-06-17 19:56:36 -05004017
4018 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004019 return;
dea31012005-04-17 16:05:31 -05004020
4021 /* Check for loop map present or not */
4022 if (phba->alpa_map[0]) {
4023 for (j = 1; j <= phba->alpa_map[0]; j++) {
4024 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004025 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004026 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004027 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004028 }
4029 } else {
4030 /* No alpamap, so try all alpa's */
4031 for (j = 0; j < FC_MAXLOOP; j++) {
4032 /* If cfg_scan_down is set, start from highest
4033 * ALPA (0xef) to lowest (0x1).
4034 */
James Smart3de2a652007-08-02 11:09:59 -04004035 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004036 index = j;
4037 else
4038 index = FC_MAXLOOP - j - 1;
4039 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004040 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004041 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004042 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004043 }
4044 }
4045 return;
4046}
4047
dea31012005-04-17 16:05:31 -05004048void
James Smart2e0fef82007-06-17 19:56:36 -05004049lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004050{
dea31012005-04-17 16:05:31 -05004051 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004052 struct lpfc_sli *psli = &phba->sli;
4053 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4054 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4055 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4056 int rc;
4057
James Smart92d7f7b2007-06-17 19:56:38 -05004058 /*
4059 * if it's not a physical port or if we already send
4060 * clear_la then don't send it.
4061 */
4062 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004063 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4064 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004065 return;
4066
James Smart2e0fef82007-06-17 19:56:36 -05004067 /* Link up discovery */
4068 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4069 phba->link_state = LPFC_CLEAR_LA;
4070 lpfc_clear_la(phba, mbox);
4071 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4072 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004073 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004074 if (rc == MBX_NOT_FINISHED) {
4075 mempool_free(mbox, phba->mbox_mem_pool);
4076 lpfc_disc_flush_list(vport);
4077 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4078 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4079 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004080 phba->link_state = LPFC_HBA_ERROR;
4081 }
4082 }
4083}
4084
4085/* Reg_vpi to tell firmware to resume normal operations */
4086void
4087lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4088{
4089 LPFC_MBOXQ_t *regvpimbox;
4090
4091 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4092 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004093 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004094 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4095 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004096 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004097 == MBX_NOT_FINISHED) {
4098 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004099 }
4100 }
4101}
4102
4103/* Start Link up / RSCN discovery on NPR nodes */
4104void
4105lpfc_disc_start(struct lpfc_vport *vport)
4106{
4107 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4108 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004109 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004110 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004111 int did_changed;
dea31012005-04-17 16:05:31 -05004112
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->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004117 clear_la_pending = 1;
4118 else
4119 clear_la_pending = 0;
4120
James Smart2e0fef82007-06-17 19:56:36 -05004121 if (vport->port_state < LPFC_VPORT_READY)
4122 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004123
James Smart2e0fef82007-06-17 19:56:36 -05004124 lpfc_set_disctmo(vport);
4125
4126 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004127 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004128 else
dea31012005-04-17 16:05:31 -05004129 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004130
4131 vport->fc_prevDID = vport->fc_myDID;
4132 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004133
4134 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004135 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4136 "0202 Start Discovery hba state x%x "
4137 "Data: x%x x%x x%x\n",
4138 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4139 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004140
4141 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004142 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004143
4144 if (num_sent)
4145 return;
4146
James Smart92d7f7b2007-06-17 19:56:38 -05004147 /*
4148 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
4149 * continue discovery.
4150 */
4151 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004152 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004153 !(vport->fc_flag & FC_RSCN_MODE) &&
4154 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004155 lpfc_issue_reg_vpi(phba, vport);
4156 return;
4157 }
James Smart2e0fef82007-06-17 19:56:36 -05004158
James Smart92d7f7b2007-06-17 19:56:38 -05004159 /*
4160 * For SLI2, we need to set port_state to READY and continue
4161 * discovery.
4162 */
4163 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4164 /* If we get here, there is nothing to ADISC */
4165 if (vport->port_type == LPFC_PHYSICAL_PORT)
4166 lpfc_issue_clear_la(phba, vport);
4167
4168 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004169 vport->num_disc_nodes = 0;
4170 /* go thru NPR nodes and issue ELS PLOGIs */
4171 if (vport->fc_npr_cnt)
4172 lpfc_els_disc_plogi(vport);
4173
4174 if (!vport->num_disc_nodes) {
4175 spin_lock_irq(shost->host_lock);
4176 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4177 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004178 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05004179 }
4180 }
James Smart92d7f7b2007-06-17 19:56:38 -05004181 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004182 } else {
4183 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004184 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05004185
4186 if (num_sent)
4187 return;
4188
James Smart2e0fef82007-06-17 19:56:36 -05004189 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05004190 /* Check to see if more RSCNs came in while we
4191 * were processing this one.
4192 */
James Smart2e0fef82007-06-17 19:56:36 -05004193 if ((vport->fc_rscn_id_cnt == 0) &&
4194 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
4195 spin_lock_irq(shost->host_lock);
4196 vport->fc_flag &= ~FC_RSCN_MODE;
4197 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004198 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004199 } else
James Smart2e0fef82007-06-17 19:56:36 -05004200 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05004201 }
4202 }
4203 return;
4204}
4205
4206/*
4207 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
4208 * ring the match the sppecified nodelist.
4209 */
4210static void
James Smart2e0fef82007-06-17 19:56:36 -05004211lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004212{
James Smart2534ba72007-04-25 09:52:20 -04004213 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004214 struct lpfc_sli *psli;
4215 IOCB_t *icmd;
4216 struct lpfc_iocbq *iocb, *next_iocb;
4217 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004218
4219 psli = &phba->sli;
4220 pring = &psli->ring[LPFC_ELS_RING];
4221
4222 /* Error matching iocb on txq or txcmplq
4223 * First check the txq.
4224 */
James Smart2e0fef82007-06-17 19:56:36 -05004225 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004226 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4227 if (iocb->context1 != ndlp) {
4228 continue;
4229 }
4230 icmd = &iocb->iocb;
4231 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
4232 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
4233
James Smart2534ba72007-04-25 09:52:20 -04004234 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05004235 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05004236 }
4237 }
4238
4239 /* Next check the txcmplq */
4240 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
4241 if (iocb->context1 != ndlp) {
4242 continue;
4243 }
4244 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004245 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
4246 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04004247 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05004248 }
4249 }
James Smart2e0fef82007-06-17 19:56:36 -05004250 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04004251
James Smarta257bf92009-04-06 18:48:10 -04004252 /* Cancel all the IOCBs from the completions list */
4253 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4254 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004255}
4256
Adrian Bunka6ababd2007-11-05 18:07:33 +01004257static void
James Smart2e0fef82007-06-17 19:56:36 -05004258lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004259{
4260 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05004261 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004262
James Smart2e0fef82007-06-17 19:56:36 -05004263 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
4264 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004265 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004266 if (!NLP_CHK_NODE_ACT(ndlp))
4267 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004268 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4269 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
4270 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04004271 }
dea31012005-04-17 16:05:31 -05004272 }
4273 }
dea31012005-04-17 16:05:31 -05004274}
4275
James Smart92d7f7b2007-06-17 19:56:38 -05004276void
4277lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
4278{
4279 lpfc_els_flush_rscn(vport);
4280 lpfc_els_flush_cmd(vport);
4281 lpfc_disc_flush_list(vport);
4282}
4283
dea31012005-04-17 16:05:31 -05004284/*****************************************************************************/
4285/*
4286 * NAME: lpfc_disc_timeout
4287 *
4288 * FUNCTION: Fibre Channel driver discovery timeout routine.
4289 *
4290 * EXECUTION ENVIRONMENT: interrupt only
4291 *
4292 * CALLED FROM:
4293 * Timer function
4294 *
4295 * RETURNS:
4296 * none
4297 */
4298/*****************************************************************************/
4299void
4300lpfc_disc_timeout(unsigned long ptr)
4301{
James Smart2e0fef82007-06-17 19:56:36 -05004302 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4303 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04004304 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004305 unsigned long flags = 0;
4306
4307 if (unlikely(!phba))
4308 return;
4309
James Smart5e9d9b82008-06-14 22:52:53 -04004310 spin_lock_irqsave(&vport->work_port_lock, flags);
4311 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4312 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05004313 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04004314 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05004315
James Smart5e9d9b82008-06-14 22:52:53 -04004316 if (!tmo_posted)
4317 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004318 return;
4319}
4320
4321static void
James Smart2e0fef82007-06-17 19:56:36 -05004322lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004323{
James Smart2e0fef82007-06-17 19:56:36 -05004324 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4325 struct lpfc_hba *phba = vport->phba;
4326 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004327 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05004328 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05004329 int rc, clrlaerr = 0;
4330
James Smart2e0fef82007-06-17 19:56:36 -05004331 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05004332 return;
4333
James Smart2e0fef82007-06-17 19:56:36 -05004334 spin_lock_irq(shost->host_lock);
4335 vport->fc_flag &= ~FC_DISC_TMO;
4336 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004337
James Smart858c9f62007-06-17 19:56:39 -05004338 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4339 "disc timeout: state:x%x rtry:x%x flg:x%x",
4340 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4341
James Smart2e0fef82007-06-17 19:56:36 -05004342 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05004343
4344 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05004345 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
4346 * FAN
4347 */
4348 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04004349 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4350 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004351 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05004352 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004353 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004354 if (!NLP_CHK_NODE_ACT(ndlp))
4355 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004356 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4357 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004358 if (ndlp->nlp_type & NLP_FABRIC) {
4359 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05004360 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004361
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004362 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004363 /* Fail outstanding IO now since device
4364 * is marked for PLOGI.
4365 */
James Smart2e0fef82007-06-17 19:56:36 -05004366 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004367 }
4368 }
James Smart92d7f7b2007-06-17 19:56:38 -05004369 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05004370 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05004371 return;
James Smart92d7f7b2007-06-17 19:56:38 -05004372 }
dea31012005-04-17 16:05:31 -05004373 break;
4374
James Smart92d7f7b2007-06-17 19:56:38 -05004375 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05004376 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05004377 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05004378 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04004379 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4380 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04004381 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05004382
4383 /* Assume no Fabric and go on with discovery.
4384 * Check for outstanding ELS FLOGI to abort.
4385 */
4386
4387 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05004388 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004389
4390 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05004391 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05004392 break;
4393
4394 case LPFC_FABRIC_CFG_LINK:
4395 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4396 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04004397 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4398 "0223 Timeout while waiting for "
4399 "NameServer login\n");
dea31012005-04-17 16:05:31 -05004400 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05004401 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05004402 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04004403 lpfc_els_abort(phba, ndlp);
4404
4405 /* ReStart discovery */
4406 goto restart_disc;
dea31012005-04-17 16:05:31 -05004407
4408 case LPFC_NS_QRY:
4409 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04004410 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4411 "0224 NameServer Query timeout "
4412 "Data: x%x x%x\n",
4413 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004414
James Smart92d7f7b2007-06-17 19:56:38 -05004415 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4416 /* Try it one more time */
4417 vport->fc_ns_retry++;
4418 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4419 vport->fc_ns_retry, 0);
4420 if (rc == 0)
4421 break;
dea31012005-04-17 16:05:31 -05004422 }
James Smart92d7f7b2007-06-17 19:56:38 -05004423 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05004424
James Smart87af33f2007-10-27 13:37:43 -04004425restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05004426 /*
4427 * Discovery is over.
4428 * set port_state to PORT_READY if SLI2.
4429 * cmpl_reg_vpi will set port_state to READY for SLI3.
4430 */
James Smart3772a992009-05-22 14:50:54 -04004431 if (phba->sli_rev < LPFC_SLI_REV4) {
4432 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4433 lpfc_issue_reg_vpi(phba, vport);
4434 else { /* NPIV Not enabled */
4435 lpfc_issue_clear_la(phba, vport);
4436 vport->port_state = LPFC_VPORT_READY;
4437 }
dea31012005-04-17 16:05:31 -05004438 }
4439
4440 /* Setup and issue mailbox INITIALIZE LINK command */
4441 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4442 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004443 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4444 "0206 Device Discovery "
4445 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004446 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004447 break;
4448 }
4449
4450 lpfc_linkdown(phba);
4451 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4452 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004453 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004454 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004455 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004456 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004457 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004458 if (rc == MBX_NOT_FINISHED)
4459 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4460
4461 break;
4462
4463 case LPFC_DISC_AUTH:
4464 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004465 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4466 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004467 lpfc_disc_flush_list(vport);
4468
James Smart92d7f7b2007-06-17 19:56:38 -05004469 /*
4470 * set port_state to PORT_READY if SLI2.
4471 * cmpl_reg_vpi will set port_state to READY for SLI3.
4472 */
James Smart3772a992009-05-22 14:50:54 -04004473 if (phba->sli_rev < LPFC_SLI_REV4) {
4474 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4475 lpfc_issue_reg_vpi(phba, vport);
4476 else { /* NPIV Not enabled */
4477 lpfc_issue_clear_la(phba, vport);
4478 vport->port_state = LPFC_VPORT_READY;
4479 }
dea31012005-04-17 16:05:31 -05004480 }
4481 break;
4482
James Smart2e0fef82007-06-17 19:56:36 -05004483 case LPFC_VPORT_READY:
4484 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004485 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4486 "0231 RSCN timeout Data: x%x "
4487 "x%x\n",
4488 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004489
4490 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004491 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004492
James Smart2e0fef82007-06-17 19:56:36 -05004493 lpfc_els_flush_rscn(vport);
4494 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004495 }
4496 break;
James Smart2e0fef82007-06-17 19:56:36 -05004497
James Smart92d7f7b2007-06-17 19:56:38 -05004498 default:
James Smarte8b62012007-08-02 11:10:09 -04004499 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004500 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004501 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004502 break;
4503 }
4504
4505 switch (phba->link_state) {
4506 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004507 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004508 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4509 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004510 clrlaerr = 1;
4511 break;
4512
James Smart09372822008-01-11 01:52:54 -05004513 case LPFC_LINK_UP:
4514 lpfc_issue_clear_la(phba, vport);
4515 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004516 case LPFC_LINK_UNKNOWN:
4517 case LPFC_WARM_START:
4518 case LPFC_INIT_START:
4519 case LPFC_INIT_MBX_CMDS:
4520 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004521 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004522 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4523 "0230 Unexpected timeout, hba link "
4524 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004525 clrlaerr = 1;
4526 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004527
4528 case LPFC_HBA_READY:
4529 break;
dea31012005-04-17 16:05:31 -05004530 }
4531
4532 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004533 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004534 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004535 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4536 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004537 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004538 }
4539
4540 return;
4541}
4542
dea31012005-04-17 16:05:31 -05004543/*
4544 * This routine handles processing a NameServer REG_LOGIN mailbox
4545 * command upon completion. It is setup in the LPFC_MBOXQ
4546 * as the completion routine when the command is
4547 * handed off to the SLI layer.
4548 */
4549void
James Smart2e0fef82007-06-17 19:56:36 -05004550lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004551{
James Smart04c68492009-05-22 14:52:52 -04004552 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004553 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4554 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4555 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004556
4557 pmb->context1 = NULL;
4558
dea31012005-04-17 16:05:31 -05004559 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004560 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004561 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004562 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004563
James Smart2e0fef82007-06-17 19:56:36 -05004564 /*
4565 * Start issuing Fabric-Device Management Interface (FDMI) command to
4566 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4567 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004568 */
James Smart2e0fef82007-06-17 19:56:36 -05004569
James Smart3de2a652007-08-02 11:09:59 -04004570 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004571 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4572 else
4573 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004574
James Smartfa4066b2008-01-11 01:53:27 -05004575 /* decrement the node reference count held for this callback
4576 * function.
4577 */
James Smart329f9bc2007-04-25 09:53:01 -04004578 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004579 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4580 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004581 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004582
4583 return;
4584}
4585
James Smart685f0bf2007-04-25 09:53:08 -04004586static int
4587lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4588{
4589 uint16_t *rpi = param;
4590
4591 return ndlp->nlp_rpi == *rpi;
4592}
4593
4594static int
4595lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4596{
4597 return memcmp(&ndlp->nlp_portname, param,
4598 sizeof(ndlp->nlp_portname)) == 0;
4599}
4600
Adrian Bunka6ababd2007-11-05 18:07:33 +01004601static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004602__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004603{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004604 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004605
James Smart2e0fef82007-06-17 19:56:36 -05004606 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004607 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004608 return ndlp;
4609 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004610 return NULL;
dea31012005-04-17 16:05:31 -05004611}
4612
James Smart685f0bf2007-04-25 09:53:08 -04004613/*
4614 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004615 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004616 */
4617struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004618__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004619{
James Smart2e0fef82007-06-17 19:56:36 -05004620 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004621}
4622
James Smart488d1462006-03-07 15:02:37 -05004623/*
James Smart685f0bf2007-04-25 09:53:08 -04004624 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004625 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004626 */
4627struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004628lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004629{
James Smart2e0fef82007-06-17 19:56:36 -05004630 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004631 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004632
James Smart2e0fef82007-06-17 19:56:36 -05004633 spin_lock_irq(shost->host_lock);
4634 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4635 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004636 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004637}
4638
dea31012005-04-17 16:05:31 -05004639void
James Smart2e0fef82007-06-17 19:56:36 -05004640lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4641 uint32_t did)
dea31012005-04-17 16:05:31 -05004642{
4643 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004644
4645 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004646 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004647
4648 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4649 "node init: did:x%x",
4650 ndlp->nlp_DID, 0, 0);
4651
dea31012005-04-17 16:05:31 -05004652 return;
4653}
James Smart329f9bc2007-04-25 09:53:01 -04004654
James Smart98c9ea52007-10-27 13:37:33 -04004655/* This routine releases all resources associated with a specifc NPort's ndlp
4656 * and mempool_free's the nodelist.
4657 */
James Smart311464e2007-08-02 11:10:37 -04004658static void
James Smart329f9bc2007-04-25 09:53:01 -04004659lpfc_nlp_release(struct kref *kref)
4660{
James Smarte47c9092008-02-08 18:49:26 -05004661 struct lpfc_hba *phba;
4662 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004663 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4664 kref);
James Smart858c9f62007-06-17 19:56:39 -05004665
4666 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4667 "node release: did:x%x flg:x%x type:x%x",
4668 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4669
James Smarte47c9092008-02-08 18:49:26 -05004670 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4671 "0279 lpfc_nlp_release: ndlp:x%p "
4672 "usgmap:x%x refcnt:%d\n",
4673 (void *)ndlp, ndlp->nlp_usg_map,
4674 atomic_read(&ndlp->kref.refcount));
4675
4676 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004677 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004678
4679 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004680 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004681 spin_lock_irqsave(&phba->ndlp_lock, flags);
4682 NLP_CLR_NODE_ACT(ndlp);
4683 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4684
4685 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004686 if (NLP_CHK_FREE_REQ(ndlp)) {
4687 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004688 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004689 }
James Smart329f9bc2007-04-25 09:53:01 -04004690}
4691
James Smart98c9ea52007-10-27 13:37:33 -04004692/* This routine bumps the reference count for a ndlp structure to ensure
4693 * that one discovery thread won't free a ndlp while another discovery thread
4694 * is using it.
4695 */
James Smart329f9bc2007-04-25 09:53:01 -04004696struct lpfc_nodelist *
4697lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4698{
James Smarte47c9092008-02-08 18:49:26 -05004699 struct lpfc_hba *phba;
4700 unsigned long flags;
4701
James Smart98c9ea52007-10-27 13:37:33 -04004702 if (ndlp) {
4703 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4704 "node get: did:x%x flg:x%x refcnt:x%x",
4705 ndlp->nlp_DID, ndlp->nlp_flag,
4706 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004707 /* The check of ndlp usage to prevent incrementing the
4708 * ndlp reference count that is in the process of being
4709 * released.
4710 */
James Smarta257bf92009-04-06 18:48:10 -04004711 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004712 spin_lock_irqsave(&phba->ndlp_lock, flags);
4713 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4714 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4715 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4716 "0276 lpfc_nlp_get: ndlp:x%p "
4717 "usgmap:x%x refcnt:%d\n",
4718 (void *)ndlp, ndlp->nlp_usg_map,
4719 atomic_read(&ndlp->kref.refcount));
4720 return NULL;
4721 } else
4722 kref_get(&ndlp->kref);
4723 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004724 }
James Smart329f9bc2007-04-25 09:53:01 -04004725 return ndlp;
4726}
4727
James Smart98c9ea52007-10-27 13:37:33 -04004728/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004729 * count goes to 0, this indicates the the associated nodelist should be
4730 * freed. Returning 1 indicates the ndlp resource has been released; on the
4731 * other hand, returning 0 indicates the ndlp resource has not been released
4732 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004733 */
James Smart329f9bc2007-04-25 09:53:01 -04004734int
4735lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4736{
James Smarte47c9092008-02-08 18:49:26 -05004737 struct lpfc_hba *phba;
4738 unsigned long flags;
4739
4740 if (!ndlp)
4741 return 1;
4742
4743 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4744 "node put: did:x%x flg:x%x refcnt:x%x",
4745 ndlp->nlp_DID, ndlp->nlp_flag,
4746 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004747 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004748 spin_lock_irqsave(&phba->ndlp_lock, flags);
4749 /* Check the ndlp memory free acknowledge flag to avoid the
4750 * possible race condition that kref_put got invoked again
4751 * after previous one has done ndlp memory free.
4752 */
4753 if (NLP_CHK_FREE_ACK(ndlp)) {
4754 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4755 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4756 "0274 lpfc_nlp_put: ndlp:x%p "
4757 "usgmap:x%x refcnt:%d\n",
4758 (void *)ndlp, ndlp->nlp_usg_map,
4759 atomic_read(&ndlp->kref.refcount));
4760 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004761 }
James Smarte47c9092008-02-08 18:49:26 -05004762 /* Check the ndlp inactivate log flag to avoid the possible
4763 * race condition that kref_put got invoked again after ndlp
4764 * is already in inactivating state.
4765 */
4766 if (NLP_CHK_IACT_REQ(ndlp)) {
4767 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4768 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4769 "0275 lpfc_nlp_put: ndlp:x%p "
4770 "usgmap:x%x refcnt:%d\n",
4771 (void *)ndlp, ndlp->nlp_usg_map,
4772 atomic_read(&ndlp->kref.refcount));
4773 return 1;
4774 }
4775 /* For last put, mark the ndlp usage flags to make sure no
4776 * other kref_get and kref_put on the same ndlp shall get
4777 * in between the process when the final kref_put has been
4778 * invoked on this ndlp.
4779 */
4780 if (atomic_read(&ndlp->kref.refcount) == 1) {
4781 /* Indicate ndlp is put to inactive state. */
4782 NLP_SET_IACT_REQ(ndlp);
4783 /* Acknowledge ndlp memory free has been seen. */
4784 if (NLP_CHK_FREE_REQ(ndlp))
4785 NLP_SET_FREE_ACK(ndlp);
4786 }
4787 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4788 /* Note, the kref_put returns 1 when decrementing a reference
4789 * count that was 1, it invokes the release callback function,
4790 * but it still left the reference count as 1 (not actually
4791 * performs the last decrementation). Otherwise, it actually
4792 * decrements the reference count and returns 0.
4793 */
4794 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004795}
James Smart98c9ea52007-10-27 13:37:33 -04004796
4797/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004798 * by any other discovery thread. This routine returns 1 if the
4799 * ndlp has been freed. A return value of 0 indicates the ndlp is
4800 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004801 */
4802int
4803lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4804{
4805 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4806 "node not used: did:x%x flg:x%x refcnt:x%x",
4807 ndlp->nlp_DID, ndlp->nlp_flag,
4808 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004809 if (atomic_read(&ndlp->kref.refcount) == 1)
4810 if (lpfc_nlp_put(ndlp))
4811 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004812 return 0;
4813}
James Smart6fb120a2009-05-22 14:52:59 -04004814
4815/**
4816 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4817 * @phba: Pointer to hba context object.
4818 *
4819 * This function iterate through all FC nodes associated
4820 * will all vports to check if there is any node with
4821 * fc_rports associated with it. If there is an fc_rport
4822 * associated with the node, then the node is either in
4823 * discovered state or its devloss_timer is pending.
4824 */
4825static int
4826lpfc_fcf_inuse(struct lpfc_hba *phba)
4827{
4828 struct lpfc_vport **vports;
4829 int i, ret = 0;
4830 struct lpfc_nodelist *ndlp;
4831 struct Scsi_Host *shost;
4832
4833 vports = lpfc_create_vport_work_array(phba);
4834
4835 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4836 shost = lpfc_shost_from_vport(vports[i]);
4837 spin_lock_irq(shost->host_lock);
4838 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4839 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4840 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4841 ret = 1;
4842 spin_unlock_irq(shost->host_lock);
4843 goto out;
James Smart891478a2009-11-18 15:40:23 -05004844 } else {
4845 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4846 "2624 RPI %x DID %x flg %x still "
4847 "logged in\n",
4848 ndlp->nlp_rpi, ndlp->nlp_DID,
4849 ndlp->nlp_flag);
4850 if (ndlp->nlp_flag & NLP_RPI_VALID)
4851 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04004852 }
4853 }
4854 spin_unlock_irq(shost->host_lock);
4855 }
4856out:
4857 lpfc_destroy_vport_work_array(phba, vports);
4858 return ret;
4859}
4860
4861/**
4862 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4863 * @phba: Pointer to hba context object.
4864 * @mboxq: Pointer to mailbox object.
4865 *
4866 * This function frees memory associated with the mailbox command.
4867 */
4868static void
4869lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4870{
4871 struct lpfc_vport *vport = mboxq->vport;
4872
4873 if (mboxq->u.mb.mbxStatus) {
4874 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4875 "2555 UNREG_VFI mbxStatus error x%x "
4876 "HBA state x%x\n",
4877 mboxq->u.mb.mbxStatus, vport->port_state);
4878 }
4879 mempool_free(mboxq, phba->mbox_mem_pool);
4880 return;
4881}
4882
4883/**
4884 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4885 * @phba: Pointer to hba context object.
4886 * @mboxq: Pointer to mailbox object.
4887 *
4888 * This function frees memory associated with the mailbox command.
4889 */
4890static void
4891lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4892{
4893 struct lpfc_vport *vport = mboxq->vport;
4894
4895 if (mboxq->u.mb.mbxStatus) {
4896 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4897 "2550 UNREG_FCFI mbxStatus error x%x "
4898 "HBA state x%x\n",
4899 mboxq->u.mb.mbxStatus, vport->port_state);
4900 }
4901 mempool_free(mboxq, phba->mbox_mem_pool);
4902 return;
4903}
4904
4905/**
James Smartecfd03c2010-02-12 14:41:27 -05004906 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04004907 * @phba: Pointer to hba context object.
4908 *
James Smartecfd03c2010-02-12 14:41:27 -05004909 * This function prepare the HBA for unregistering the currently registered
4910 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
4911 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04004912 */
James Smartecfd03c2010-02-12 14:41:27 -05004913int
4914lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04004915{
4916 LPFC_MBOXQ_t *mbox;
James Smart6fb120a2009-05-22 14:52:59 -04004917 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05004918 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05004919 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05004920 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04004921
James Smartecfd03c2010-02-12 14:41:27 -05004922 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04004923 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05004924 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04004925
James Smart4d9ab992009-10-02 15:16:39 -04004926 /* At this point, all discovery is aborted */
4927 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04004928
4929 /* Unregister VPIs */
4930 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05004931 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04004932 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05004933 /* Stop FLOGI/FDISC retries */
4934 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
4935 if (ndlp)
4936 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart6fb120a2009-05-22 14:52:59 -04004937 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05004938 shost = lpfc_shost_from_vport(vports[i]);
4939 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05004940 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05004941 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05004942 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04004943 }
4944 lpfc_destroy_vport_work_array(phba, vports);
4945
James Smart695a8142010-01-26 23:08:03 -05004946 /* Cleanup any outstanding ELS commands */
4947 lpfc_els_flush_all_cmd(phba);
4948
James Smart6fb120a2009-05-22 14:52:59 -04004949 /* Unregister VFI */
4950 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4951 if (!mbox) {
4952 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05004953 "2556 UNREG_VFI mbox allocation failed"
4954 "HBA state x%x\n", phba->pport->port_state);
4955 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04004956 }
4957
James Smart6669f9b2009-10-02 15:16:45 -04004958 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04004959 mbox->vport = phba->pport;
4960 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
4961
4962 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4963 if (rc == MBX_NOT_FINISHED) {
4964 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05004965 "2557 UNREG_VFI issue mbox failed rc x%x "
4966 "HBA state x%x\n",
4967 rc, phba->pport->port_state);
James Smart6fb120a2009-05-22 14:52:59 -04004968 mempool_free(mbox, phba->mbox_mem_pool);
James Smartecfd03c2010-02-12 14:41:27 -05004969 return -EIO;
James Smart6fb120a2009-05-22 14:52:59 -04004970 }
4971
James Smart72100cc2010-02-12 14:43:01 -05004972 shost = lpfc_shost_from_vport(phba->pport);
4973 spin_lock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004974 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05004975 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004976
James Smartecfd03c2010-02-12 14:41:27 -05004977 return 0;
4978}
4979
4980/**
4981 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
4982 * @phba: Pointer to hba context object.
4983 *
4984 * This function issues synchronous unregister FCF mailbox command to HBA to
4985 * unregister the currently registered FCF record. The driver does not reset
4986 * the driver FCF usage state flags.
4987 *
4988 * Return 0 if successfully issued, none-zero otherwise.
4989 */
4990int
4991lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
4992{
4993 LPFC_MBOXQ_t *mbox;
4994 int rc;
4995
James Smart6fb120a2009-05-22 14:52:59 -04004996 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4997 if (!mbox) {
4998 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05004999 "2551 UNREG_FCFI mbox allocation failed"
5000 "HBA state x%x\n", phba->pport->port_state);
5001 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005002 }
James Smart6fb120a2009-05-22 14:52:59 -04005003 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5004 mbox->vport = phba->pport;
5005 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5006 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5007
5008 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5010 "2552 Unregister FCFI command failed rc x%x "
5011 "HBA state x%x\n",
5012 rc, phba->pport->port_state);
5013 return -EINVAL;
5014 }
5015 return 0;
5016}
5017
5018/**
5019 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5020 * @phba: Pointer to hba context object.
5021 *
5022 * This function unregisters the currently reigstered FCF. This function
5023 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5024 */
5025void
5026lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5027{
5028 int rc;
5029
5030 /* Preparation for unregistering fcf */
5031 rc = lpfc_unregister_fcf_prep(phba);
5032 if (rc) {
5033 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5034 "2748 Failed to prepare for unregistering "
5035 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005036 return;
5037 }
5038
James Smartecfd03c2010-02-12 14:41:27 -05005039 /* Now, unregister FCF record and reset HBA FCF state */
5040 rc = lpfc_sli4_unregister_fcf(phba);
5041 if (rc)
5042 return;
5043 /* Reset HBA FCF states after successful unregister FCF */
5044 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005045 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005046
5047 /*
5048 * If driver is not unloading, check if there is any other
5049 * FCF record that can be used for discovery.
5050 */
5051 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005052 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005053 return;
5054
James Smart0c9ab6f2010-02-26 14:15:57 -05005055 /* This is considered as the initial FCF discovery scan */
5056 spin_lock_irq(&phba->hbalock);
5057 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5058 spin_unlock_irq(&phba->hbalock);
5059 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005060
James Smart0c9ab6f2010-02-26 14:15:57 -05005061 if (rc) {
5062 spin_lock_irq(&phba->hbalock);
5063 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5064 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005065 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005066 "2553 lpfc_unregister_unused_fcf failed "
5067 "to read FCF record HBA state x%x\n",
5068 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005069 }
James Smartecfd03c2010-02-12 14:41:27 -05005070}
5071
5072/**
5073 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5074 * @phba: Pointer to hba context object.
5075 *
5076 * This function just unregisters the currently reigstered FCF. It does not
5077 * try to find another FCF for discovery.
5078 */
5079void
5080lpfc_unregister_fcf(struct lpfc_hba *phba)
5081{
5082 int rc;
5083
5084 /* Preparation for unregistering fcf */
5085 rc = lpfc_unregister_fcf_prep(phba);
5086 if (rc) {
5087 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5088 "2749 Failed to prepare for unregistering "
5089 "HBA's FCF record: rc=%d\n", rc);
5090 return;
5091 }
5092
5093 /* Now, unregister FCF record and reset HBA FCF state */
5094 rc = lpfc_sli4_unregister_fcf(phba);
5095 if (rc)
5096 return;
5097 /* Set proper HBA FCF states after successful unregister FCF */
5098 spin_lock_irq(&phba->hbalock);
5099 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
5100 spin_unlock_irq(&phba->hbalock);
5101}
5102
5103/**
5104 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
5105 * @phba: Pointer to hba context object.
5106 *
5107 * This function check if there are any connected remote port for the FCF and
5108 * if all the devices are disconnected, this function unregister FCFI.
5109 * This function also tries to use another FCF for discovery.
5110 */
5111void
5112lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
5113{
5114 /*
5115 * If HBA is not running in FIP mode or if HBA does not support
5116 * FCoE or if FCF is not registered, do nothing.
5117 */
5118 spin_lock_irq(&phba->hbalock);
5119 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
5120 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
5121 !(phba->hba_flag & HBA_FIP_SUPPORT)) {
5122 spin_unlock_irq(&phba->hbalock);
5123 return;
5124 }
5125 spin_unlock_irq(&phba->hbalock);
5126
5127 if (lpfc_fcf_inuse(phba))
5128 return;
5129
5130 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005131}
5132
5133/**
5134 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
5135 * @phba: Pointer to hba context object.
5136 * @buff: Buffer containing the FCF connection table as in the config
5137 * region.
5138 * This function create driver data structure for the FCF connection
5139 * record table read from config region 23.
5140 */
5141static void
5142lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
5143 uint8_t *buff)
5144{
5145 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5146 struct lpfc_fcf_conn_hdr *conn_hdr;
5147 struct lpfc_fcf_conn_rec *conn_rec;
5148 uint32_t record_count;
5149 int i;
5150
5151 /* Free the current connect table */
5152 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005153 &phba->fcf_conn_rec_list, list) {
5154 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04005155 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005156 }
James Smart6fb120a2009-05-22 14:52:59 -04005157
5158 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
5159 record_count = conn_hdr->length * sizeof(uint32_t)/
5160 sizeof(struct lpfc_fcf_conn_rec);
5161
5162 conn_rec = (struct lpfc_fcf_conn_rec *)
5163 (buff + sizeof(struct lpfc_fcf_conn_hdr));
5164
5165 for (i = 0; i < record_count; i++) {
5166 if (!(conn_rec[i].flags & FCFCNCT_VALID))
5167 continue;
5168 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
5169 GFP_KERNEL);
5170 if (!conn_entry) {
5171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5172 "2566 Failed to allocate connection"
5173 " table entry\n");
5174 return;
5175 }
5176
5177 memcpy(&conn_entry->conn_rec, &conn_rec[i],
5178 sizeof(struct lpfc_fcf_conn_rec));
5179 conn_entry->conn_rec.vlan_tag =
5180 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
5181 conn_entry->conn_rec.flags =
5182 le16_to_cpu(conn_entry->conn_rec.flags);
5183 list_add_tail(&conn_entry->list,
5184 &phba->fcf_conn_rec_list);
5185 }
5186}
5187
5188/**
5189 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
5190 * @phba: Pointer to hba context object.
5191 * @buff: Buffer containing the FCoE parameter data structure.
5192 *
5193 * This function update driver data structure with config
5194 * parameters read from config region 23.
5195 */
5196static void
5197lpfc_read_fcoe_param(struct lpfc_hba *phba,
5198 uint8_t *buff)
5199{
5200 struct lpfc_fip_param_hdr *fcoe_param_hdr;
5201 struct lpfc_fcoe_params *fcoe_param;
5202
5203 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
5204 buff;
5205 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04005206 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04005207
5208 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
5209 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
5210 return;
5211
James Smart6fb120a2009-05-22 14:52:59 -04005212 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
5213 phba->valid_vlan = 1;
5214 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
5215 0xFFF;
5216 }
5217
5218 phba->fc_map[0] = fcoe_param->fc_map[0];
5219 phba->fc_map[1] = fcoe_param->fc_map[1];
5220 phba->fc_map[2] = fcoe_param->fc_map[2];
5221 return;
5222}
5223
5224/**
5225 * lpfc_get_rec_conf23 - Get a record type in config region data.
5226 * @buff: Buffer containing config region 23 data.
5227 * @size: Size of the data buffer.
5228 * @rec_type: Record type to be searched.
5229 *
5230 * This function searches config region data to find the begining
5231 * of the record specified by record_type. If record found, this
5232 * function return pointer to the record else return NULL.
5233 */
5234static uint8_t *
5235lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
5236{
5237 uint32_t offset = 0, rec_length;
5238
5239 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
5240 (size < sizeof(uint32_t)))
5241 return NULL;
5242
5243 rec_length = buff[offset + 1];
5244
5245 /*
5246 * One TLV record has one word header and number of data words
5247 * specified in the rec_length field of the record header.
5248 */
5249 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
5250 <= size) {
5251 if (buff[offset] == rec_type)
5252 return &buff[offset];
5253
5254 if (buff[offset] == LPFC_REGION23_LAST_REC)
5255 return NULL;
5256
5257 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
5258 rec_length = buff[offset + 1];
5259 }
5260 return NULL;
5261}
5262
5263/**
5264 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
5265 * @phba: Pointer to lpfc_hba data structure.
5266 * @buff: Buffer containing config region 23 data.
5267 * @size: Size of the data buffer.
5268 *
5269 * This fuction parse the FCoE config parameters in config region 23 and
5270 * populate driver data structure with the parameters.
5271 */
5272void
5273lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
5274 uint8_t *buff,
5275 uint32_t size)
5276{
5277 uint32_t offset = 0, rec_length;
5278 uint8_t *rec_ptr;
5279
5280 /*
5281 * If data size is less than 2 words signature and version cannot be
5282 * verified.
5283 */
5284 if (size < 2*sizeof(uint32_t))
5285 return;
5286
5287 /* Check the region signature first */
5288 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
5289 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5290 "2567 Config region 23 has bad signature\n");
5291 return;
5292 }
5293
5294 offset += 4;
5295
5296 /* Check the data structure version */
5297 if (buff[offset] != LPFC_REGION23_VERSION) {
5298 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5299 "2568 Config region 23 has bad version\n");
5300 return;
5301 }
5302 offset += 4;
5303
5304 rec_length = buff[offset + 1];
5305
5306 /* Read FCoE param record */
5307 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5308 size - offset, FCOE_PARAM_TYPE);
5309 if (rec_ptr)
5310 lpfc_read_fcoe_param(phba, rec_ptr);
5311
5312 /* Read FCF connection table */
5313 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5314 size - offset, FCOE_CONN_TBL_TYPE);
5315 if (rec_ptr)
5316 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5317
5318}