blob: f40b9609f6e45a95799aeac89b084fb18682f16a [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 Smartda0436e2009-05-22 14:51:39 -0400528 }
529
James Smart549e55c2007-08-02 11:09:51 -0400530 vports = lpfc_create_vport_work_array(phba);
531 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400532 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400533 /*
534 * We could have no vports in array if unloading, so if
535 * this happens then just use the pport
536 */
537 if (vports[i] == NULL && i == 0)
538 vport = phba->pport;
539 else
540 vport = vports[i];
541 if (vport == NULL)
542 break;
James Smart58da1ff2008-04-07 10:15:56 -0400543 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400544 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400545 vport->work_port_events &= ~work_port_events;
546 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400547 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400548 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400549 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400550 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400551 if (work_port_events & WORKER_HB_TMO)
552 lpfc_hb_timeout_handler(phba);
553 if (work_port_events & WORKER_MBOX_TMO)
554 lpfc_mbox_timeout_handler(phba);
555 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
556 lpfc_unblock_fabric_iocbs(phba);
557 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400558 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400559 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
560 lpfc_ramp_down_queue_handler(phba);
561 if (work_port_events & WORKER_RAMP_UP_QUEUE)
562 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500563 }
James Smart09372822008-01-11 01:52:54 -0500564 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500565
James Smart858c9f62007-06-17 19:56:39 -0500566 pring = &phba->sli.ring[LPFC_ELS_RING];
567 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
568 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400569 if ((status & HA_RXMASK) ||
570 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400571 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400572 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500573 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400574 /* Set the lpfc data pending flag */
575 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500576 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400577 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500578 lpfc_sli_handle_slow_ring_event(phba, pring,
579 (status &
580 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500581 }
582 /*
583 * Turn on Ring interrupts
584 */
James Smart3772a992009-05-22 14:50:54 -0400585 if (phba->sli_rev <= LPFC_SLI_REV3) {
586 spin_lock_irq(&phba->hbalock);
587 control = readl(phba->HCregaddr);
588 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
589 lpfc_debugfs_slow_ring_trc(phba,
590 "WRK Enable ring: cntl:x%x hacopy:x%x",
591 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400592
James Smart3772a992009-05-22 14:50:54 -0400593 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
594 writel(control, phba->HCregaddr);
595 readl(phba->HCregaddr); /* flush */
596 } else {
597 lpfc_debugfs_slow_ring_trc(phba,
598 "WRK Ring ok: cntl:x%x hacopy:x%x",
599 control, ha_copy, 0);
600 }
601 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500602 }
603 }
James Smart2e0fef82007-06-17 19:56:36 -0500604 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500605}
606
dea31012005-04-17 16:05:31 -0500607int
608lpfc_do_work(void *p)
609{
610 struct lpfc_hba *phba = p;
611 int rc;
dea31012005-04-17 16:05:31 -0500612
613 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400614 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500615
James Smart3a55b532008-12-04 22:38:54 -0500616 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400617 /* wait and check worker queue activities */
618 rc = wait_event_interruptible(phba->work_waitq,
619 (test_and_clear_bit(LPFC_DATA_READY,
620 &phba->data_flags)
621 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500622 /* Signal wakeup shall terminate the worker thread */
623 if (rc) {
624 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
625 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500626 break;
James Smart3a55b532008-12-04 22:38:54 -0500627 }
dea31012005-04-17 16:05:31 -0500628
James Smart5e9d9b82008-06-14 22:52:53 -0400629 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500630 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500631 }
James Smart3a55b532008-12-04 22:38:54 -0500632 phba->worker_thread = NULL;
633 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
634 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500635 return 0;
636}
637
638/*
639 * This is only called to handle FC worker events. Since this a rare
640 * occurance, we allocate a struct lpfc_work_evt structure here instead of
641 * embedding it in the IOCB.
642 */
643int
James Smart2e0fef82007-06-17 19:56:36 -0500644lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500645 uint32_t evt)
646{
647 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500648 unsigned long flags;
dea31012005-04-17 16:05:31 -0500649
650 /*
651 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
652 * be queued to worker thread for processing
653 */
James Smart92d7f7b2007-06-17 19:56:38 -0500654 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500655 if (!evtp)
656 return 0;
657
658 evtp->evt_arg1 = arg1;
659 evtp->evt_arg2 = arg2;
660 evtp->evt = evt;
661
James Smarted957682007-06-17 19:56:37 -0500662 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400663 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500664 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500665
James Smart5e9d9b82008-06-14 22:52:53 -0400666 lpfc_worker_wake_up(phba);
667
dea31012005-04-17 16:05:31 -0500668 return 1;
669}
670
James Smart92d7f7b2007-06-17 19:56:38 -0500671void
672lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
673{
James Smart09372822008-01-11 01:52:54 -0500674 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500675 struct lpfc_hba *phba = vport->phba;
676 struct lpfc_nodelist *ndlp, *next_ndlp;
677 int rc;
678
679 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500680 if (!NLP_CHK_NODE_ACT(ndlp))
681 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500682 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
683 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400684 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
685 ((vport->port_type == LPFC_NPIV_PORT) &&
686 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500687 lpfc_unreg_rpi(vport, ndlp);
688
689 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400690 if ((phba->sli_rev < LPFC_SLI_REV4) &&
691 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500692 continue;
693 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
694 remove
695 ? NLP_EVT_DEVICE_RM
696 : NLP_EVT_DEVICE_RECOVERY);
697 }
698 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
699 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500700 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500701 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500702 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500703 }
704}
705
James Smart87af33f2007-10-27 13:37:43 -0400706void
James Smart98c9ea52007-10-27 13:37:33 -0400707lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500708{
James Smart45ed1192009-10-02 15:17:02 -0400709 /* Cleanup any outstanding received buffers */
710 lpfc_cleanup_rcv_buffers(vport);
711
James Smart92d7f7b2007-06-17 19:56:38 -0500712 /* Cleanup any outstanding RSCN activity */
713 lpfc_els_flush_rscn(vport);
714
715 /* Cleanup any outstanding ELS commands */
716 lpfc_els_flush_cmd(vport);
717
718 lpfc_cleanup_rpis(vport, 0);
719
James Smart92d7f7b2007-06-17 19:56:38 -0500720 /* Turn off discovery timer if its running */
721 lpfc_can_disctmo(vport);
722}
723
James Smart3772a992009-05-22 14:50:54 -0400724void
James Smart98c9ea52007-10-27 13:37:33 -0400725lpfc_linkdown_port(struct lpfc_vport *vport)
726{
727 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
728
729 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
730
731 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
732 "Link Down: state:x%x rtry:x%x flg:x%x",
733 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
734
735 lpfc_port_link_failure(vport);
736
737}
738
dea31012005-04-17 16:05:31 -0500739int
James Smart685f0bf2007-04-25 09:53:08 -0400740lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500741{
James Smart2e0fef82007-06-17 19:56:36 -0500742 struct lpfc_vport *vport = phba->pport;
743 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400744 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400745 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400746 int i;
dea31012005-04-17 16:05:31 -0500747
James Smart3163f722008-02-08 18:50:25 -0500748 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500749 return 0;
James Smart2e0fef82007-06-17 19:56:36 -0500750 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -0400751 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_DISCOVERED);
James Smart92d7f7b2007-06-17 19:56:38 -0500752 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500753 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500754 phba->pport->fc_flag &= ~FC_LBIT;
755 }
James Smart2e0fef82007-06-17 19:56:36 -0500756 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400757 vports = lpfc_create_vport_work_array(phba);
758 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400759 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400760 /* Issue a LINK DOWN event to all nodes */
761 lpfc_linkdown_port(vports[i]);
762 }
James Smart09372822008-01-11 01:52:54 -0500763 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500764 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500765 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
766 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500767 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500768 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500769 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400770 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500771 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500772 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500773 }
774 }
775
dea31012005-04-17 16:05:31 -0500776 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500777 if (phba->pport->fc_flag & FC_PT2PT) {
778 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500779 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
780 if (mb) {
dea31012005-04-17 16:05:31 -0500781 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500782 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500783 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400784 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500785 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500786 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500787 }
788 }
James Smart2e0fef82007-06-17 19:56:36 -0500789 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500790 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500791 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500792 }
James Smart2e0fef82007-06-17 19:56:36 -0500793
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500794 return 0;
dea31012005-04-17 16:05:31 -0500795}
796
James Smart92d7f7b2007-06-17 19:56:38 -0500797static void
798lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500799{
James Smart92d7f7b2007-06-17 19:56:38 -0500800 struct lpfc_nodelist *ndlp;
801
802 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500803 if (!NLP_CHK_NODE_ACT(ndlp))
804 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500805 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
806 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500807 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500808 /* On Linkup its safe to clean up the ndlp
809 * from Fabric connections.
810 */
James Smart92d7f7b2007-06-17 19:56:38 -0500811 if (ndlp->nlp_DID != Fabric_DID)
812 lpfc_unreg_rpi(vport, ndlp);
813 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
814 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500815 /* Fail outstanding IO now since device is
816 * marked for PLOGI.
817 */
James Smart92d7f7b2007-06-17 19:56:38 -0500818 lpfc_unreg_rpi(vport, ndlp);
819 }
820 }
821}
822
823static void
824lpfc_linkup_port(struct lpfc_vport *vport)
825{
826 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500827 struct lpfc_hba *phba = vport->phba;
828
829 if ((vport->load_flag & FC_UNLOADING) != 0)
830 return;
831
James Smart858c9f62007-06-17 19:56:39 -0500832 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
833 "Link Up: top:x%x speed:x%x flg:x%x",
834 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
835
James Smart92d7f7b2007-06-17 19:56:38 -0500836 /* If NPIV is not enabled, only bring the physical port up */
837 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
838 (vport != phba->pport))
839 return;
dea31012005-04-17 16:05:31 -0500840
James Smart2e0fef82007-06-17 19:56:36 -0500841 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400842
James Smart2e0fef82007-06-17 19:56:36 -0500843 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500844 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
845 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
846 vport->fc_flag |= FC_NDISC_ACTIVE;
847 vport->fc_ns_retry = 0;
848 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500849
James Smart92d7f7b2007-06-17 19:56:38 -0500850 if (vport->fc_flag & FC_LBIT)
851 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500852
James Smart92d7f7b2007-06-17 19:56:38 -0500853}
854
855static int
856lpfc_linkup(struct lpfc_hba *phba)
857{
James Smart549e55c2007-08-02 11:09:51 -0400858 struct lpfc_vport **vports;
859 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500860
861 phba->link_state = LPFC_LINK_UP;
862
863 /* Unblock fabric iocbs if they are blocked */
864 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
865 del_timer_sync(&phba->fabric_block_timer);
866
James Smart549e55c2007-08-02 11:09:51 -0400867 vports = lpfc_create_vport_work_array(phba);
868 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400869 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400870 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500871 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400872 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
873 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500874 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500875
876 return 0;
877}
878
879/*
880 * This routine handles processing a CLEAR_LA mailbox
881 * command upon completion. It is setup in the LPFC_MBOXQ
882 * as the completion routine when the command is
883 * handed off to the SLI layer.
884 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100885static void
James Smart2e0fef82007-06-17 19:56:36 -0500886lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500887{
James Smart2e0fef82007-06-17 19:56:36 -0500888 struct lpfc_vport *vport = pmb->vport;
889 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
890 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400891 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500892 uint32_t control;
893
dea31012005-04-17 16:05:31 -0500894 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500895 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500896 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
897 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
898
899 /* Check for error */
900 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500901 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400902 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
903 "0320 CLEAR_LA mbxStatus error x%x hba "
904 "state x%x\n",
905 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500906 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500907 goto out;
908 }
909
James Smart92d7f7b2007-06-17 19:56:38 -0500910 if (vport->port_type == LPFC_PHYSICAL_PORT)
911 phba->link_state = LPFC_HBA_READY;
912
913 spin_lock_irq(&phba->hbalock);
914 psli->sli_flag |= LPFC_PROCESS_LA;
915 control = readl(phba->HCregaddr);
916 control |= HC_LAINT_ENA;
917 writel(control, phba->HCregaddr);
918 readl(phba->HCregaddr); /* flush */
919 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500920 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500921 return;
dea31012005-04-17 16:05:31 -0500922
dea31012005-04-17 16:05:31 -0500923out:
924 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400925 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
926 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500927 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500928
James Smart2e0fef82007-06-17 19:56:36 -0500929 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400930 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500931 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500932
James Smart2e0fef82007-06-17 19:56:36 -0500933 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500934
935 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500936
937 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500938 psli->sli_flag |= LPFC_PROCESS_LA;
939 control = readl(phba->HCregaddr);
940 control |= HC_LAINT_ENA;
941 writel(control, phba->HCregaddr);
942 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500943 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500944
945 return;
946}
947
James Smart2e0fef82007-06-17 19:56:36 -0500948
dea31012005-04-17 16:05:31 -0500949static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500950lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500951{
James Smart2e0fef82007-06-17 19:56:36 -0500952 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500953
James Smart04c68492009-05-22 14:52:52 -0400954 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500955 goto out;
dea31012005-04-17 16:05:31 -0500956
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500957 mempool_free(pmb, phba->mbox_mem_pool);
958
959 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500960 vport->fc_flag & FC_PUBLIC_LOOP &&
961 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500962 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500963 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500964 * LPFC_LOCAL_CFG_LINK while waiting for FAN
965 */
James Smart2e0fef82007-06-17 19:56:36 -0500966 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500967 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500968 }
dea31012005-04-17 16:05:31 -0500969
James Smart2e0fef82007-06-17 19:56:36 -0500970 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500971 * LPFC_FLOGI while waiting for FLOGI cmpl
972 */
James Smart92d7f7b2007-06-17 19:56:38 -0500973 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500974 lpfc_initial_flogi(vport);
975 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500976 return;
dea31012005-04-17 16:05:31 -0500977
978out:
James Smarte8b62012007-08-02 11:10:09 -0400979 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
980 "0306 CONFIG_LINK mbxStatus error x%x "
981 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400982 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500983 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500984
985 lpfc_linkdown(phba);
986
James Smarte8b62012007-08-02 11:10:09 -0400987 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
988 "0200 CONFIG_LINK bad hba state x%x\n",
989 vport->port_state);
dea31012005-04-17 16:05:31 -0500990
James Smart92d7f7b2007-06-17 19:56:38 -0500991 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500992 return;
993}
994
995static void
James Smart6fb120a2009-05-22 14:52:59 -0400996lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
997{
998 struct lpfc_vport *vport = mboxq->vport;
999 unsigned long flags;
1000
1001 if (mboxq->u.mb.mbxStatus) {
1002 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1003 "2017 REG_FCFI mbxStatus error x%x "
1004 "HBA state x%x\n",
1005 mboxq->u.mb.mbxStatus, vport->port_state);
1006 mempool_free(mboxq, phba->mbox_mem_pool);
1007 return;
1008 }
1009
1010 /* Start FCoE discovery by sending a FLOGI. */
1011 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1012 /* Set the FCFI registered flag */
1013 spin_lock_irqsave(&phba->hbalock, flags);
1014 phba->fcf.fcf_flag |= FCF_REGISTERED;
1015 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart32b97932009-07-19 10:01:21 -04001016 /* If there is a pending FCoE event, restart FCF table scan. */
1017 if (lpfc_check_pending_fcoe_event(phba, 1)) {
1018 mempool_free(mboxq, phba->mbox_mem_pool);
1019 return;
1020 }
James Smart1c6f4ef52009-11-18 15:40:49 -05001021 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4d9ab992009-10-02 15:16:39 -04001022 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1023 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001024 spin_unlock_irqrestore(&phba->hbalock, flags);
1025 if (vport->port_state != LPFC_FLOGI)
James Smart6fb120a2009-05-22 14:52:59 -04001026 lpfc_initial_flogi(vport);
James Smart6fb120a2009-05-22 14:52:59 -04001027
1028 mempool_free(mboxq, phba->mbox_mem_pool);
1029 return;
1030}
1031
1032/**
1033 * lpfc_fab_name_match - Check if the fcf fabric name match.
1034 * @fab_name: pointer to fabric name.
1035 * @new_fcf_record: pointer to fcf record.
1036 *
1037 * This routine compare the fcf record's fabric name with provided
1038 * fabric name. If the fabric name are identical this function
1039 * returns 1 else return 0.
1040 **/
1041static uint32_t
1042lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1043{
1044 if ((fab_name[0] ==
1045 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record)) &&
1046 (fab_name[1] ==
1047 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record)) &&
1048 (fab_name[2] ==
1049 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record)) &&
1050 (fab_name[3] ==
1051 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record)) &&
1052 (fab_name[4] ==
1053 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record)) &&
1054 (fab_name[5] ==
1055 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record)) &&
1056 (fab_name[6] ==
1057 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record)) &&
1058 (fab_name[7] ==
1059 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record)))
1060 return 1;
1061 else
1062 return 0;
1063}
1064
1065/**
James Smart8fa38512009-07-19 10:01:03 -04001066 * lpfc_sw_name_match - Check if the fcf switch name match.
1067 * @fab_name: pointer to fabric name.
1068 * @new_fcf_record: pointer to fcf record.
1069 *
1070 * This routine compare the fcf record's switch name with provided
1071 * switch name. If the switch name are identical this function
1072 * returns 1 else return 0.
1073 **/
1074static uint32_t
1075lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1076{
1077 if ((sw_name[0] ==
1078 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record)) &&
1079 (sw_name[1] ==
1080 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record)) &&
1081 (sw_name[2] ==
1082 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record)) &&
1083 (sw_name[3] ==
1084 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record)) &&
1085 (sw_name[4] ==
1086 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record)) &&
1087 (sw_name[5] ==
1088 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record)) &&
1089 (sw_name[6] ==
1090 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record)) &&
1091 (sw_name[7] ==
1092 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record)))
1093 return 1;
1094 else
1095 return 0;
1096}
1097
1098/**
James Smart6fb120a2009-05-22 14:52:59 -04001099 * lpfc_mac_addr_match - Check if the fcf mac address match.
1100 * @phba: pointer to lpfc hba data structure.
1101 * @new_fcf_record: pointer to fcf record.
1102 *
1103 * This routine compare the fcf record's mac address with HBA's
1104 * FCF mac address. If the mac addresses are identical this function
1105 * returns 1 else return 0.
1106 **/
1107static uint32_t
1108lpfc_mac_addr_match(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1109{
1110 if ((phba->fcf.mac_addr[0] ==
1111 bf_get(lpfc_fcf_record_mac_0, new_fcf_record)) &&
1112 (phba->fcf.mac_addr[1] ==
1113 bf_get(lpfc_fcf_record_mac_1, new_fcf_record)) &&
1114 (phba->fcf.mac_addr[2] ==
1115 bf_get(lpfc_fcf_record_mac_2, new_fcf_record)) &&
1116 (phba->fcf.mac_addr[3] ==
1117 bf_get(lpfc_fcf_record_mac_3, new_fcf_record)) &&
1118 (phba->fcf.mac_addr[4] ==
1119 bf_get(lpfc_fcf_record_mac_4, new_fcf_record)) &&
1120 (phba->fcf.mac_addr[5] ==
1121 bf_get(lpfc_fcf_record_mac_5, new_fcf_record)))
1122 return 1;
1123 else
1124 return 0;
1125}
1126
1127/**
1128 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1129 * @phba: pointer to lpfc hba data structure.
1130 * @new_fcf_record: pointer to fcf record.
1131 *
1132 * This routine copies the FCF information from the FCF
1133 * record to lpfc_hba data structure.
1134 **/
1135static void
1136lpfc_copy_fcf_record(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1137{
1138 phba->fcf.fabric_name[0] =
1139 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1140 phba->fcf.fabric_name[1] =
1141 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1142 phba->fcf.fabric_name[2] =
1143 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1144 phba->fcf.fabric_name[3] =
1145 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1146 phba->fcf.fabric_name[4] =
1147 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1148 phba->fcf.fabric_name[5] =
1149 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1150 phba->fcf.fabric_name[6] =
1151 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1152 phba->fcf.fabric_name[7] =
1153 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1154 phba->fcf.mac_addr[0] =
1155 bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1156 phba->fcf.mac_addr[1] =
1157 bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1158 phba->fcf.mac_addr[2] =
1159 bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1160 phba->fcf.mac_addr[3] =
1161 bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1162 phba->fcf.mac_addr[4] =
1163 bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1164 phba->fcf.mac_addr[5] =
1165 bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1166 phba->fcf.fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1167 phba->fcf.priority = new_fcf_record->fip_priority;
James Smart8fa38512009-07-19 10:01:03 -04001168 phba->fcf.switch_name[0] =
1169 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1170 phba->fcf.switch_name[1] =
1171 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1172 phba->fcf.switch_name[2] =
1173 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1174 phba->fcf.switch_name[3] =
1175 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1176 phba->fcf.switch_name[4] =
1177 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1178 phba->fcf.switch_name[5] =
1179 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1180 phba->fcf.switch_name[6] =
1181 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1182 phba->fcf.switch_name[7] =
1183 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001184}
1185
1186/**
1187 * lpfc_register_fcf - Register the FCF with hba.
1188 * @phba: pointer to lpfc hba data structure.
1189 *
1190 * This routine issues a register fcfi mailbox command to register
1191 * the fcf with HBA.
1192 **/
1193static void
1194lpfc_register_fcf(struct lpfc_hba *phba)
1195{
1196 LPFC_MBOXQ_t *fcf_mbxq;
1197 int rc;
1198 unsigned long flags;
1199
1200 spin_lock_irqsave(&phba->hbalock, flags);
1201
1202 /* If the FCF is not availabe do nothing. */
1203 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smart4d9ab992009-10-02 15:16:39 -04001204 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001205 spin_unlock_irqrestore(&phba->hbalock, flags);
1206 return;
1207 }
1208
1209 /* The FCF is already registered, start discovery */
1210 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1211 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
James Smart32b97932009-07-19 10:01:21 -04001212 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001213 spin_unlock_irqrestore(&phba->hbalock, flags);
1214 if (phba->pport->port_state != LPFC_FLOGI)
1215 lpfc_initial_flogi(phba->pport);
1216 return;
1217 }
1218 spin_unlock_irqrestore(&phba->hbalock, flags);
1219
1220 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1221 GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001222 if (!fcf_mbxq) {
1223 spin_lock_irqsave(&phba->hbalock, flags);
1224 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1225 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001226 return;
James Smart4d9ab992009-10-02 15:16:39 -04001227 }
James Smart6fb120a2009-05-22 14:52:59 -04001228
1229 lpfc_reg_fcfi(phba, fcf_mbxq);
1230 fcf_mbxq->vport = phba->pport;
1231 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1232 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001233 if (rc == MBX_NOT_FINISHED) {
1234 spin_lock_irqsave(&phba->hbalock, flags);
1235 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1236 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001237 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001238 }
James Smart6fb120a2009-05-22 14:52:59 -04001239
1240 return;
1241}
1242
1243/**
1244 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1245 * @phba: pointer to lpfc hba data structure.
1246 * @new_fcf_record: pointer to fcf record.
1247 * @boot_flag: Indicates if this record used by boot bios.
1248 * @addr_mode: The address mode to be used by this FCF
1249 *
1250 * This routine compare the fcf record with connect list obtained from the
1251 * config region to decide if this FCF can be used for SAN discovery. It returns
1252 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1253 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1254 * is used by boot bios and addr_mode will indicate the addressing mode to be
1255 * used for this FCF when the function returns.
1256 * If the FCF record need to be used with a particular vlan id, the vlan is
1257 * set in the vlan_id on return of the function. If not VLAN tagging need to
1258 * be used with the FCF vlan_id will be set to 0xFFFF;
1259 **/
1260static int
1261lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1262 struct fcf_record *new_fcf_record,
1263 uint32_t *boot_flag, uint32_t *addr_mode,
1264 uint16_t *vlan_id)
1265{
1266 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001267 int i, j, fcf_vlan_id = 0;
1268
1269 /* Find the lowest VLAN id in the FCF record */
1270 for (i = 0; i < 512; i++) {
1271 if (new_fcf_record->vlan_bitmap[i]) {
1272 fcf_vlan_id = i * 8;
1273 j = 0;
1274 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1275 j++;
1276 fcf_vlan_id++;
1277 }
1278 break;
1279 }
1280 }
James Smart6fb120a2009-05-22 14:52:59 -04001281
James Smart0c287582009-06-10 17:22:56 -04001282 /* If FCF not available return 0 */
1283 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1284 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1285 return 0;
1286
James Smart45ed1192009-10-02 15:17:02 -04001287 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001288 *boot_flag = 0;
1289 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1290 new_fcf_record);
1291 if (phba->valid_vlan)
1292 *vlan_id = phba->vlan_id;
1293 else
1294 *vlan_id = 0xFFFF;
1295 return 1;
1296 }
1297
1298 /*
1299 * If there are no FCF connection table entry, driver connect to all
1300 * FCFs.
1301 */
1302 if (list_empty(&phba->fcf_conn_rec_list)) {
1303 *boot_flag = 0;
1304 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1305 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001306
1307 /*
1308 * When there are no FCF connect entries, use driver's default
1309 * addressing mode - FPMA.
1310 */
1311 if (*addr_mode & LPFC_FCF_FPMA)
1312 *addr_mode = LPFC_FCF_FPMA;
1313
James Smart4d9ab992009-10-02 15:16:39 -04001314 /* If FCF record report a vlan id use that vlan id */
1315 if (fcf_vlan_id)
1316 *vlan_id = fcf_vlan_id;
1317 else
1318 *vlan_id = 0xFFFF;
James Smart6fb120a2009-05-22 14:52:59 -04001319 return 1;
1320 }
1321
1322 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list, list) {
1323 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1324 continue;
1325
1326 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1327 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001328 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001329 continue;
James Smart8fa38512009-07-19 10:01:03 -04001330 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1331 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1332 new_fcf_record))
1333 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001334 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1335 /*
1336 * If the vlan bit map does not have the bit set for the
1337 * vlan id to be used, then it is not a match.
1338 */
1339 if (!(new_fcf_record->vlan_bitmap
1340 [conn_entry->conn_rec.vlan_tag / 8] &
1341 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1342 continue;
1343 }
1344
1345 /*
James Smart0c287582009-06-10 17:22:56 -04001346 * If connection record does not support any addressing mode,
1347 * skip the FCF record.
1348 */
1349 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1350 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1351 continue;
1352
1353 /*
James Smart6fb120a2009-05-22 14:52:59 -04001354 * Check if the connection record specifies a required
1355 * addressing mode.
1356 */
1357 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1358 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1359
1360 /*
1361 * If SPMA required but FCF not support this continue.
1362 */
1363 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1364 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1365 new_fcf_record) & LPFC_FCF_SPMA))
1366 continue;
1367
1368 /*
1369 * If FPMA required but FCF not support this continue.
1370 */
1371 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1372 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1373 new_fcf_record) & LPFC_FCF_FPMA))
1374 continue;
1375 }
1376
1377 /*
1378 * This fcf record matches filtering criteria.
1379 */
1380 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1381 *boot_flag = 1;
1382 else
1383 *boot_flag = 0;
1384
James Smart0c287582009-06-10 17:22:56 -04001385 /*
1386 * If user did not specify any addressing mode, or if the
1387 * prefered addressing mode specified by user is not supported
1388 * by FCF, allow fabric to pick the addressing mode.
1389 */
James Smart6fb120a2009-05-22 14:52:59 -04001390 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1391 new_fcf_record);
1392 /*
1393 * If the user specified a required address mode, assign that
1394 * address mode
1395 */
1396 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1397 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1398 *addr_mode = (conn_entry->conn_rec.flags &
1399 FCFCNCT_AM_SPMA) ?
1400 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1401 /*
1402 * If the user specified a prefered address mode, use the
1403 * addr mode only if FCF support the addr_mode.
1404 */
1405 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1406 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1407 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1408 (*addr_mode & LPFC_FCF_SPMA))
1409 *addr_mode = LPFC_FCF_SPMA;
1410 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1411 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1412 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1413 (*addr_mode & LPFC_FCF_FPMA))
1414 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001415
James Smart4d9ab992009-10-02 15:16:39 -04001416 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001417 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1418 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001419 /*
1420 * If no vlan id is specified in connect list, use the vlan id
1421 * in the FCF record
1422 */
1423 else if (fcf_vlan_id)
1424 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001425 else
1426 *vlan_id = 0xFFFF;
1427
1428 return 1;
1429 }
1430
1431 return 0;
1432}
1433
1434/**
James Smart32b97932009-07-19 10:01:21 -04001435 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1436 * @phba: pointer to lpfc hba data structure.
1437 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1438 *
1439 * This function check if there is any fcoe event pending while driver
1440 * scan FCF entries. If there is any pending event, it will restart the
1441 * FCF saning and return 1 else return 0.
1442 */
1443int
1444lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1445{
1446 LPFC_MBOXQ_t *mbox;
1447 int rc;
1448 /*
1449 * If the Link is up and no FCoE events while in the
1450 * FCF discovery, no need to restart FCF discovery.
1451 */
1452 if ((phba->link_state >= LPFC_LINK_UP) &&
1453 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1454 return 0;
1455
1456 spin_lock_irq(&phba->hbalock);
1457 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1458 spin_unlock_irq(&phba->hbalock);
1459
1460 if (phba->link_state >= LPFC_LINK_UP)
1461 lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart1c6f4ef52009-11-18 15:40:49 -05001462 else {
James Smart4d9ab992009-10-02 15:16:39 -04001463 /*
1464 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1465 * flag
1466 */
James Smart1c6f4ef52009-11-18 15:40:49 -05001467 spin_lock_irq(&phba->hbalock);
James Smart4d9ab992009-10-02 15:16:39 -04001468 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001469 spin_unlock_irq(&phba->hbalock);
1470 }
James Smart32b97932009-07-19 10:01:21 -04001471
1472 if (unreg_fcf) {
1473 spin_lock_irq(&phba->hbalock);
1474 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1475 spin_unlock_irq(&phba->hbalock);
1476 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1477 if (!mbox) {
1478 lpfc_printf_log(phba, KERN_ERR,
1479 LOG_DISCOVERY|LOG_MBOX,
1480 "2610 UNREG_FCFI mbox allocation failed\n");
1481 return 1;
1482 }
1483 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
1484 mbox->vport = phba->pport;
1485 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
1486 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1487 if (rc == MBX_NOT_FINISHED) {
1488 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
1489 "2611 UNREG_FCFI issue mbox failed\n");
1490 mempool_free(mbox, phba->mbox_mem_pool);
1491 }
1492 }
1493
1494 return 1;
1495}
1496
1497/**
James Smart6fb120a2009-05-22 14:52:59 -04001498 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
1499 * @phba: pointer to lpfc hba data structure.
1500 * @mboxq: pointer to mailbox object.
1501 *
1502 * This function iterate through all the fcf records available in
1503 * HBA and choose the optimal FCF record for discovery. After finding
1504 * the FCF for discovery it register the FCF record and kick start
1505 * discovery.
1506 * If FCF_IN_USE flag is set in currently used FCF, the routine try to
1507 * use a FCF record which match fabric name and mac address of the
1508 * currently used FCF record.
1509 * If the driver support only one FCF, it will try to use the FCF record
1510 * used by BOOT_BIOS.
1511 */
1512void
1513lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1514{
1515 void *virt_addr;
1516 dma_addr_t phys_addr;
1517 uint8_t *bytep;
1518 struct lpfc_mbx_sge sge;
1519 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1520 uint32_t shdr_status, shdr_add_status;
1521 union lpfc_sli4_cfg_shdr *shdr;
1522 struct fcf_record *new_fcf_record;
1523 int rc;
1524 uint32_t boot_flag, addr_mode;
1525 uint32_t next_fcf_index;
1526 unsigned long flags;
1527 uint16_t vlan_id;
1528
James Smart32b97932009-07-19 10:01:21 -04001529 /* If there is pending FCoE event restart FCF table scan */
1530 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1531 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1532 return;
1533 }
1534
James Smart6fb120a2009-05-22 14:52:59 -04001535 /* Get the first SGE entry from the non-embedded DMA memory. This
1536 * routine only uses a single SGE.
1537 */
1538 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1539 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1540 if (unlikely(!mboxq->sge_array)) {
1541 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1542 "2524 Failed to get the non-embedded SGE "
1543 "virtual address\n");
1544 goto out;
1545 }
1546 virt_addr = mboxq->sge_array->addr[0];
1547
1548 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1549 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1550 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
1551 &shdr->response);
1552 /*
1553 * The FCF Record was read and there is no reason for the driver
1554 * to maintain the FCF record data or memory. Instead, just need
1555 * to book keeping the FCFIs can be used.
1556 */
1557 if (shdr_status || shdr_add_status) {
James Smartdef9c7a2009-12-21 17:02:28 -05001558 if (shdr_status == STATUS_FCF_TABLE_EMPTY) {
1559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1560 "2726 READ_FCF_RECORD Indicates empty "
1561 "FCF table.\n");
1562 } else {
1563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1564 "2521 READ_FCF_RECORD mailbox failed "
1565 "with status x%x add_status x%x, mbx\n",
1566 shdr_status, shdr_add_status);
1567 }
James Smart6fb120a2009-05-22 14:52:59 -04001568 goto out;
1569 }
1570 /* Interpreting the returned information of FCF records */
1571 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1572 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1573 sizeof(struct lpfc_mbx_read_fcf_tbl));
1574 next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1575
1576 new_fcf_record = (struct fcf_record *)(virt_addr +
1577 sizeof(struct lpfc_mbx_read_fcf_tbl));
1578 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1579 sizeof(struct fcf_record));
1580 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1581
1582 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record,
1583 &boot_flag, &addr_mode,
1584 &vlan_id);
1585 /*
1586 * If the fcf record does not match with connect list entries
1587 * read the next entry.
1588 */
1589 if (!rc)
1590 goto read_next_fcf;
1591 /*
1592 * If this is not the first FCF discovery of the HBA, use last
1593 * FCF record for the discovery.
1594 */
1595 spin_lock_irqsave(&phba->hbalock, flags);
1596 if (phba->fcf.fcf_flag & FCF_IN_USE) {
1597 if (lpfc_fab_name_match(phba->fcf.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001598 new_fcf_record) &&
1599 lpfc_sw_name_match(phba->fcf.switch_name,
1600 new_fcf_record) &&
James Smart6fb120a2009-05-22 14:52:59 -04001601 lpfc_mac_addr_match(phba, new_fcf_record)) {
1602 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1603 spin_unlock_irqrestore(&phba->hbalock, flags);
1604 goto out;
1605 }
1606 spin_unlock_irqrestore(&phba->hbalock, flags);
1607 goto read_next_fcf;
1608 }
1609 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1610 /*
1611 * If the current FCF record does not have boot flag
1612 * set and new fcf record has boot flag set, use the
1613 * new fcf record.
1614 */
1615 if (boot_flag && !(phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1616 /* Use this FCF record */
1617 lpfc_copy_fcf_record(phba, new_fcf_record);
1618 phba->fcf.addr_mode = addr_mode;
1619 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1620 if (vlan_id != 0xFFFF) {
1621 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1622 phba->fcf.vlan_id = vlan_id;
1623 }
1624 spin_unlock_irqrestore(&phba->hbalock, flags);
1625 goto read_next_fcf;
1626 }
1627 /*
1628 * If the current FCF record has boot flag set and the
1629 * new FCF record does not have boot flag, read the next
1630 * FCF record.
1631 */
1632 if (!boot_flag && (phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1633 spin_unlock_irqrestore(&phba->hbalock, flags);
1634 goto read_next_fcf;
1635 }
1636 /*
1637 * If there is a record with lower priority value for
1638 * the current FCF, use that record.
1639 */
James Smart8fa38512009-07-19 10:01:03 -04001640 if (lpfc_fab_name_match(phba->fcf.fabric_name,
1641 new_fcf_record) &&
1642 (new_fcf_record->fip_priority < phba->fcf.priority)) {
James Smart6fb120a2009-05-22 14:52:59 -04001643 /* Use this FCF record */
1644 lpfc_copy_fcf_record(phba, new_fcf_record);
1645 phba->fcf.addr_mode = addr_mode;
1646 if (vlan_id != 0xFFFF) {
1647 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1648 phba->fcf.vlan_id = vlan_id;
1649 }
1650 spin_unlock_irqrestore(&phba->hbalock, flags);
1651 goto read_next_fcf;
1652 }
1653 spin_unlock_irqrestore(&phba->hbalock, flags);
1654 goto read_next_fcf;
1655 }
1656 /*
1657 * This is the first available FCF record, use this
1658 * record.
1659 */
1660 lpfc_copy_fcf_record(phba, new_fcf_record);
1661 phba->fcf.addr_mode = addr_mode;
1662 if (boot_flag)
1663 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1664 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1665 if (vlan_id != 0xFFFF) {
1666 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1667 phba->fcf.vlan_id = vlan_id;
1668 }
1669 spin_unlock_irqrestore(&phba->hbalock, flags);
1670 goto read_next_fcf;
1671
1672read_next_fcf:
1673 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1674 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0)
1675 lpfc_register_fcf(phba);
1676 else
1677 lpfc_sli4_read_fcf_record(phba, next_fcf_index);
1678 return;
1679
1680out:
1681 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1682 lpfc_register_fcf(phba);
1683
1684 return;
1685}
1686
1687/**
James Smart1c6834a2009-07-19 10:01:26 -04001688 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
1689 * @phba: pointer to lpfc hba data structure.
1690 * @mboxq: pointer to mailbox data structure.
1691 *
1692 * This function handles completion of init vpi mailbox command.
1693 */
1694static void
1695lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1696{
1697 struct lpfc_vport *vport = mboxq->vport;
1698 if (mboxq->u.mb.mbxStatus) {
1699 lpfc_printf_vlog(vport, KERN_ERR,
1700 LOG_MBOX,
1701 "2609 Init VPI mailbox failed 0x%x\n",
1702 mboxq->u.mb.mbxStatus);
1703 mempool_free(mboxq, phba->mbox_mem_pool);
1704 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1705 return;
1706 }
1707 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
1708
1709 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1710 lpfc_initial_fdisc(vport);
1711 else {
1712 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04001713 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1714 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04001715 }
1716 return;
1717}
1718
1719/**
James Smart6fb120a2009-05-22 14:52:59 -04001720 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
1721 * @phba: pointer to lpfc hba data structure.
1722 *
1723 * This function loops through the list of vports on the @phba and issues an
1724 * FDISC if possible.
1725 */
1726void
1727lpfc_start_fdiscs(struct lpfc_hba *phba)
1728{
1729 struct lpfc_vport **vports;
1730 int i;
James Smart1c6834a2009-07-19 10:01:26 -04001731 LPFC_MBOXQ_t *mboxq;
1732 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001733
1734 vports = lpfc_create_vport_work_array(phba);
1735 if (vports != NULL) {
1736 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1737 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1738 continue;
1739 /* There are no vpi for this vport */
1740 if (vports[i]->vpi > phba->max_vpi) {
1741 lpfc_vport_set_state(vports[i],
1742 FC_VPORT_FAILED);
1743 continue;
1744 }
1745 if (phba->fc_topology == TOPOLOGY_LOOP) {
1746 lpfc_vport_set_state(vports[i],
1747 FC_VPORT_LINKDOWN);
1748 continue;
1749 }
James Smart1c6834a2009-07-19 10:01:26 -04001750 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
1751 mboxq = mempool_alloc(phba->mbox_mem_pool,
1752 GFP_KERNEL);
1753 if (!mboxq) {
1754 lpfc_printf_vlog(vports[i], KERN_ERR,
1755 LOG_MBOX, "2607 Failed to allocate "
1756 "init_vpi mailbox\n");
1757 continue;
1758 }
1759 lpfc_init_vpi(phba, mboxq, vports[i]->vpi);
1760 mboxq->vport = vports[i];
1761 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
1762 rc = lpfc_sli_issue_mbox(phba, mboxq,
1763 MBX_NOWAIT);
1764 if (rc == MBX_NOT_FINISHED) {
1765 lpfc_printf_vlog(vports[i], KERN_ERR,
1766 LOG_MBOX, "2608 Failed to issue "
1767 "init_vpi mailbox\n");
1768 mempool_free(mboxq,
1769 phba->mbox_mem_pool);
1770 }
1771 continue;
1772 }
James Smart6fb120a2009-05-22 14:52:59 -04001773 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1774 lpfc_initial_fdisc(vports[i]);
1775 else {
1776 lpfc_vport_set_state(vports[i],
1777 FC_VPORT_NO_FABRIC_SUPP);
1778 lpfc_printf_vlog(vports[i], KERN_ERR,
1779 LOG_ELS,
1780 "0259 No NPIV "
1781 "Fabric support\n");
1782 }
1783 }
1784 }
1785 lpfc_destroy_vport_work_array(phba, vports);
1786}
1787
1788void
1789lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1790{
1791 struct lpfc_dmabuf *dmabuf = mboxq->context1;
1792 struct lpfc_vport *vport = mboxq->vport;
1793
1794 if (mboxq->u.mb.mbxStatus) {
1795 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1796 "2018 REG_VFI mbxStatus error x%x "
1797 "HBA state x%x\n",
1798 mboxq->u.mb.mbxStatus, vport->port_state);
1799 if (phba->fc_topology == TOPOLOGY_LOOP) {
1800 /* FLOGI failed, use loop map to make discovery list */
1801 lpfc_disc_list_loopmap(vport);
1802 /* Start discovery */
1803 lpfc_disc_start(vport);
1804 goto fail_free_mem;
1805 }
1806 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1807 goto fail_free_mem;
1808 }
James Smartc8685952009-11-18 15:39:16 -05001809 /* The VPI is implicitly registered when the VFI is registered */
1810 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart6fb120a2009-05-22 14:52:59 -04001811
1812 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1813 lpfc_start_fdiscs(phba);
1814 lpfc_do_scr_ns_plogi(phba, vport);
1815 }
1816
1817fail_free_mem:
1818 mempool_free(mboxq, phba->mbox_mem_pool);
1819 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1820 kfree(dmabuf);
1821 return;
1822}
1823
1824static void
James Smart2e0fef82007-06-17 19:56:36 -05001825lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001826{
James Smart6fb120a2009-05-22 14:52:59 -04001827 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001828 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05001829 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001830
1831
1832 /* Check for error */
1833 if (mb->mbxStatus) {
1834 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001835 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1836 "0319 READ_SPARAM mbxStatus error x%x "
1837 "hba state x%x>\n",
1838 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001839 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05001840 goto out;
1841 }
1842
James Smart2e0fef82007-06-17 19:56:36 -05001843 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05001844 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05001845 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05001846 u64_to_wwn(phba->cfg_soft_wwnn,
1847 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04001848 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05001849 u64_to_wwn(phba->cfg_soft_wwpn,
1850 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05001851 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1852 sizeof(vport->fc_nodename));
1853 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1854 sizeof(vport->fc_portname));
1855 if (vport->port_type == LPFC_PHYSICAL_PORT) {
1856 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1857 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1858 }
1859
dea31012005-04-17 16:05:31 -05001860 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1861 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001862 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001863 return;
1864
1865out:
1866 pmb->context1 = NULL;
1867 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1868 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05001869 lpfc_issue_clear_la(phba, vport);
1870 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001871 return;
1872}
1873
1874static void
James Smart92d7f7b2007-06-17 19:56:38 -05001875lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05001876{
James Smart92d7f7b2007-06-17 19:56:38 -05001877 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04001878 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05001879 int i;
James Smart14691152006-12-02 13:34:28 -05001880 struct lpfc_dmabuf *mp;
1881 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001882 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05001883
dea31012005-04-17 16:05:31 -05001884 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001885
James Smart92d7f7b2007-06-17 19:56:38 -05001886 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001887 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05001888 case LA_1GHZ_LINK:
1889 phba->fc_linkspeed = LA_1GHZ_LINK;
1890 break;
1891 case LA_2GHZ_LINK:
1892 phba->fc_linkspeed = LA_2GHZ_LINK;
1893 break;
1894 case LA_4GHZ_LINK:
1895 phba->fc_linkspeed = LA_4GHZ_LINK;
1896 break;
1897 case LA_8GHZ_LINK:
1898 phba->fc_linkspeed = LA_8GHZ_LINK;
1899 break;
James Smartf4b4c682009-05-22 14:53:12 -04001900 case LA_10GHZ_LINK:
1901 phba->fc_linkspeed = LA_10GHZ_LINK;
1902 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001903 default:
1904 phba->fc_linkspeed = LA_UNKNW_LINK;
1905 break;
dea31012005-04-17 16:05:31 -05001906 }
1907
1908 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05001909 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05001910
1911 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05001912 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05001913
James Smart6a9c52c2009-10-02 15:16:51 -04001914 /* if npiv is enabled and this adapter supports npiv log
1915 * a message that npiv is not supported in this topology
1916 */
1917 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04001918 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1919 "1309 Link Up Event npiv not supported in loop "
1920 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001921 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05001922 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05001923 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001924
James Smart2e0fef82007-06-17 19:56:36 -05001925 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05001926 i = la->un.lilpBde64.tus.f.bdeSize;
1927
1928 if (i == 0) {
1929 phba->alpa_map[0] = 0;
1930 } else {
James Smarte8b62012007-08-02 11:10:09 -04001931 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05001932 int numalpa, j, k;
1933 union {
1934 uint8_t pamap[16];
1935 struct {
1936 uint32_t wd1;
1937 uint32_t wd2;
1938 uint32_t wd3;
1939 uint32_t wd4;
1940 } pa;
1941 } un;
1942 numalpa = phba->alpa_map[0];
1943 j = 0;
1944 while (j < numalpa) {
1945 memset(un.pamap, 0, 16);
1946 for (k = 1; j < numalpa; k++) {
1947 un.pamap[k - 1] =
1948 phba->alpa_map[j + 1];
1949 j++;
1950 if (k == 16)
1951 break;
1952 }
1953 /* Link Up Event ALPA map */
1954 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05001955 KERN_WARNING,
1956 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001957 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05001958 "ALPA map Data: x%x "
1959 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001960 un.pa.wd1, un.pa.wd2,
1961 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05001962 }
1963 }
1964 }
1965 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05001966 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04001967 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05001968 (phba->sli_rev == 3))
1969 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1970 }
James Smart2e0fef82007-06-17 19:56:36 -05001971 vport->fc_myDID = phba->fc_pref_DID;
1972 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001973 }
James Smart92d7f7b2007-06-17 19:56:38 -05001974 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001975
1976 lpfc_linkup(phba);
1977 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001978 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05001979 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05001980 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04001981 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05001982 if (rc == MBX_NOT_FINISHED) {
1983 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1984 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1985 kfree(mp);
1986 mempool_free(sparam_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001987 goto out;
James Smart14691152006-12-02 13:34:28 -05001988 }
dea31012005-04-17 16:05:31 -05001989 }
1990
James Smart6fb120a2009-05-22 14:52:59 -04001991 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
1992 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1993 if (!cfglink_mbox)
1994 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05001995 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05001996 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05001997 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001998 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04001999 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002000 if (rc == MBX_NOT_FINISHED) {
2001 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2002 goto out;
2003 }
2004 } else {
James Smart32b97932009-07-19 10:01:21 -04002005 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002006 /*
2007 * Add the driver's default FCF record at FCF index 0 now. This
2008 * is phase 1 implementation that support FCF index 0 and driver
2009 * defaults.
2010 */
James Smart45ed1192009-10-02 15:17:02 -04002011 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002012 fcf_record = kzalloc(sizeof(struct fcf_record),
2013 GFP_KERNEL);
2014 if (unlikely(!fcf_record)) {
2015 lpfc_printf_log(phba, KERN_ERR,
2016 LOG_MBOX | LOG_SLI,
2017 "2554 Could not allocate memmory for "
2018 "fcf record\n");
2019 rc = -ENODEV;
2020 goto out;
2021 }
2022
2023 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2024 LPFC_FCOE_FCF_DEF_INDEX);
2025 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2026 if (unlikely(rc)) {
2027 lpfc_printf_log(phba, KERN_ERR,
2028 LOG_MBOX | LOG_SLI,
2029 "2013 Could not manually add FCF "
2030 "record 0, status %d\n", rc);
2031 rc = -ENODEV;
2032 kfree(fcf_record);
2033 goto out;
2034 }
2035 kfree(fcf_record);
2036 }
2037 /*
2038 * The driver is expected to do FIP/FCF. Call the port
2039 * and get the FCF Table.
2040 */
James Smart32b97932009-07-19 10:01:21 -04002041 spin_lock_irq(&phba->hbalock);
2042 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2043 spin_unlock_irq(&phba->hbalock);
2044 return;
2045 }
2046 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002047 rc = lpfc_sli4_read_fcf_record(phba,
2048 LPFC_FCOE_FCF_GET_FIRST);
2049 if (rc)
2050 goto out;
dea31012005-04-17 16:05:31 -05002051 }
James Smart6fb120a2009-05-22 14:52:59 -04002052
2053 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002054out:
2055 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002056 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2057 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2058 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002059 lpfc_issue_clear_la(phba, vport);
2060 return;
dea31012005-04-17 16:05:31 -05002061}
2062
2063static void
James Smart84774a42008-08-24 21:50:06 -04002064lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002065{
dea31012005-04-17 16:05:31 -05002066 uint32_t control;
2067 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002068 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002069 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002070 if (phba->sli_rev <= LPFC_SLI_REV3) {
2071 control = readl(phba->HCregaddr);
2072 control |= HC_LAINT_ENA;
2073 writel(control, phba->HCregaddr);
2074 readl(phba->HCregaddr); /* flush */
2075 }
James Smart2e0fef82007-06-17 19:56:36 -05002076 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002077}
2078
James Smart84774a42008-08-24 21:50:06 -04002079static void
2080lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2081{
2082 lpfc_linkdown(phba);
2083 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002084 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002085 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2086}
2087
2088
dea31012005-04-17 16:05:31 -05002089/*
2090 * This routine handles processing a READ_LA mailbox
2091 * command upon completion. It is setup in the LPFC_MBOXQ
2092 * as the completion routine when the command is
2093 * handed off to the SLI layer.
2094 */
2095void
James Smart2e0fef82007-06-17 19:56:36 -05002096lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002097{
James Smart2e0fef82007-06-17 19:56:36 -05002098 struct lpfc_vport *vport = pmb->vport;
2099 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002100 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002101 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002102 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2103
James Smart0d2b6b82008-06-14 22:52:47 -04002104 /* Unblock ELS traffic */
2105 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002106 /* Check for error */
2107 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002108 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002109 "1307 READ_LA mbox error x%x state x%x\n",
2110 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002111 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002112 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002113 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2114 }
2115
James Smart04c68492009-05-22 14:52:52 -04002116 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002117
2118 memcpy(&phba->alpa_map[0], mp->virt, 128);
2119
James Smart2e0fef82007-06-17 19:56:36 -05002120 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002121 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002122 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002123 else
James Smart2e0fef82007-06-17 19:56:36 -05002124 vport->fc_flag &= ~FC_BYPASSED_MODE;
2125 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002126
James Smart0c287582009-06-10 17:22:56 -04002127 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002128 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002129 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002130 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002131 if (phba->fc_eventTag != 0)
2132 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002133 }
dea31012005-04-17 16:05:31 -05002134
2135 phba->fc_eventTag = la->eventTag;
James Smart84774a42008-08-24 21:50:06 -04002136 if (la->mm)
2137 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2138 else
2139 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
dea31012005-04-17 16:05:31 -05002140
James Smart4d9ab992009-10-02 15:16:39 -04002141 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002142 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002143 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002144 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002145 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002146 "1306 Link Up Event in loop back mode "
2147 "x%x received Data: x%x x%x x%x x%x\n",
2148 la->eventTag, phba->fc_eventTag,
2149 la->granted_AL_PA, la->UlnkSpeed,
2150 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002151 } else {
2152 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002153 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002154 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002155 la->eventTag, phba->fc_eventTag,
2156 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002157 phba->alpa_map[0],
2158 la->mm, la->fa,
2159 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002160 }
James Smart92d7f7b2007-06-17 19:56:38 -05002161 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002162 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002163 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002164 if (phba->link_flag & LS_LOOPBACK_MODE) {
2165 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2166 "1308 Link Down Event in loop back mode "
2167 "x%x received "
2168 "Data: x%x x%x x%x\n",
2169 la->eventTag, phba->fc_eventTag,
2170 phba->pport->port_state, vport->fc_flag);
2171 }
2172 else {
2173 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002174 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002175 "Data: x%x x%x x%x x%x x%x\n",
2176 la->eventTag, phba->fc_eventTag,
2177 phba->pport->port_state, vport->fc_flag,
2178 la->mm, la->fa);
2179 }
2180 lpfc_mbx_issue_link_down(phba);
2181 }
2182 if (la->mm && la->attType == AT_LINK_UP) {
2183 if (phba->link_state != LPFC_LINK_DOWN) {
2184 phba->fc_stat.LinkDown++;
2185 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2186 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002187 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002188 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002189 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002190 lpfc_mbx_issue_link_down(phba);
2191 } else
2192 lpfc_enable_la(phba);
2193
2194 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2195 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2196 "Data: x%x x%x x%x\n",
2197 la->eventTag, phba->fc_eventTag,
2198 phba->pport->port_state, vport->fc_flag);
2199 /*
2200 * The cmnd that triggered this will be waiting for this
2201 * signal.
2202 */
2203 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2204 if (phba->wait_4_mlo_maint_flg) {
2205 phba->wait_4_mlo_maint_flg = 0;
2206 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002207 }
James Smart84774a42008-08-24 21:50:06 -04002208 }
2209
2210 if (la->fa) {
2211 if (la->mm)
2212 lpfc_issue_clear_la(phba, vport);
2213 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2214 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002215 }
2216
2217lpfc_mbx_cmpl_read_la_free_mbuf:
2218 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2219 kfree(mp);
2220 mempool_free(pmb, phba->mbox_mem_pool);
2221 return;
2222}
2223
2224/*
2225 * This routine handles processing a REG_LOGIN mailbox
2226 * command upon completion. It is setup in the LPFC_MBOXQ
2227 * as the completion routine when the command is
2228 * handed off to the SLI layer.
2229 */
2230void
James Smart2e0fef82007-06-17 19:56:36 -05002231lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002232{
James Smart2e0fef82007-06-17 19:56:36 -05002233 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002234 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002235 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05002236
dea31012005-04-17 16:05:31 -05002237 pmb->context1 = NULL;
2238
2239 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05002240 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05002241 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2242 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002243 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002244 /* decrement the node reference count held for this callback
2245 * function.
2246 */
James Smart329f9bc2007-04-25 09:53:01 -04002247 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002248
2249 return;
2250}
2251
James Smart92d7f7b2007-06-17 19:56:38 -05002252static void
2253lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2254{
James Smart04c68492009-05-22 14:52:52 -04002255 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002256 struct lpfc_vport *vport = pmb->vport;
2257 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2258
2259 switch (mb->mbxStatus) {
2260 case 0x0011:
2261 case 0x0020:
2262 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04002263 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2264 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2265 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002266 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002267 }
James Smartc8685952009-11-18 15:39:16 -05002268 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05002269 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart92d7f7b2007-06-17 19:56:38 -05002270 vport->unreg_vpi_cmpl = VPORT_OK;
2271 mempool_free(pmb, phba->mbox_mem_pool);
2272 /*
2273 * This shost reference might have been taken at the beginning of
2274 * lpfc_vport_delete()
2275 */
James Smart1c6f4ef52009-11-18 15:40:49 -05002276 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05002277 scsi_host_put(shost);
2278}
2279
James Smartd7c255b2008-08-24 21:50:00 -04002280int
James Smart92d7f7b2007-06-17 19:56:38 -05002281lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2282{
2283 struct lpfc_hba *phba = vport->phba;
2284 LPFC_MBOXQ_t *mbox;
2285 int rc;
2286
2287 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2288 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002289 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002290
2291 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2292 mbox->vport = vport;
2293 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002294 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002295 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002296 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2297 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002298 mempool_free(mbox, phba->mbox_mem_pool);
2299 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002300 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002301 }
James Smartd7c255b2008-08-24 21:50:00 -04002302 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002303}
2304
2305static void
2306lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2307{
2308 struct lpfc_vport *vport = pmb->vport;
2309 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002310 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002311
2312 switch (mb->mbxStatus) {
2313 case 0x0011:
2314 case 0x9601:
2315 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002316 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2317 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2318 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002319 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2320 spin_lock_irq(shost->host_lock);
2321 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2322 spin_unlock_irq(shost->host_lock);
2323 vport->fc_myDID = 0;
2324 goto out;
2325 }
James Smart92d7f7b2007-06-17 19:56:38 -05002326
James Smartc8685952009-11-18 15:39:16 -05002327 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart92d7f7b2007-06-17 19:56:38 -05002328 vport->num_disc_nodes = 0;
2329 /* go thru NPR list and issue ELS PLOGIs */
2330 if (vport->fc_npr_cnt)
2331 lpfc_els_disc_plogi(vport);
2332
2333 if (!vport->num_disc_nodes) {
2334 spin_lock_irq(shost->host_lock);
2335 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2336 spin_unlock_irq(shost->host_lock);
2337 lpfc_can_disctmo(vport);
2338 }
2339 vport->port_state = LPFC_VPORT_READY;
2340
2341out:
2342 mempool_free(pmb, phba->mbox_mem_pool);
2343 return;
2344}
2345
James Smart21e9a0a2009-05-22 14:53:21 -04002346/**
2347 * lpfc_create_static_vport - Read HBA config region to create static vports.
2348 * @phba: pointer to lpfc hba data structure.
2349 *
2350 * This routine issue a DUMP mailbox command for config region 22 to get
2351 * the list of static vports to be created. The function create vports
2352 * based on the information returned from the HBA.
2353 **/
2354void
2355lpfc_create_static_vport(struct lpfc_hba *phba)
2356{
2357 LPFC_MBOXQ_t *pmb = NULL;
2358 MAILBOX_t *mb;
2359 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04002360 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04002361 struct fc_vport_identifiers vport_id;
2362 struct fc_vport *new_fc_vport;
2363 struct Scsi_Host *shost;
2364 struct lpfc_vport *vport;
2365 uint16_t offset = 0;
2366 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04002367 struct lpfc_dmabuf *mp;
2368 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04002369
2370 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2371 if (!pmb) {
2372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2373 "0542 lpfc_create_static_vport failed to"
2374 " allocate mailbox memory\n");
2375 return;
2376 }
2377
2378 mb = &pmb->u.mb;
2379
2380 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2381 if (!vport_info) {
2382 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2383 "0543 lpfc_create_static_vport failed to"
2384 " allocate vport_info\n");
2385 mempool_free(pmb, phba->mbox_mem_pool);
2386 return;
2387 }
2388
2389 vport_buff = (uint8_t *) vport_info;
2390 do {
James Smart1c6834a2009-07-19 10:01:26 -04002391 if (lpfc_dump_static_vport(phba, pmb, offset))
2392 goto out;
2393
James Smart21e9a0a2009-05-22 14:53:21 -04002394 pmb->vport = phba->pport;
2395 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2396
2397 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2398 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2399 "0544 lpfc_create_static_vport failed to"
2400 " issue dump mailbox command ret 0x%x "
2401 "status 0x%x\n",
2402 rc, mb->mbxStatus);
2403 goto out;
2404 }
2405
James Smart1c6834a2009-07-19 10:01:26 -04002406 if (phba->sli_rev == LPFC_SLI_REV4) {
2407 byte_count = pmb->u.mqe.un.mb_words[5];
2408 mp = (struct lpfc_dmabuf *) pmb->context2;
2409 if (byte_count > sizeof(struct static_vport_info) -
2410 offset)
2411 byte_count = sizeof(struct static_vport_info)
2412 - offset;
2413 memcpy(vport_buff + offset, mp->virt, byte_count);
2414 offset += byte_count;
2415 } else {
2416 if (mb->un.varDmp.word_cnt >
2417 sizeof(struct static_vport_info) - offset)
2418 mb->un.varDmp.word_cnt =
2419 sizeof(struct static_vport_info)
2420 - offset;
2421 byte_count = mb->un.varDmp.word_cnt;
2422 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2423 vport_buff + offset,
2424 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04002425
James Smart1c6834a2009-07-19 10:01:26 -04002426 offset += byte_count;
2427 }
James Smart21e9a0a2009-05-22 14:53:21 -04002428
James Smart1c6834a2009-07-19 10:01:26 -04002429 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04002430 offset < sizeof(struct static_vport_info));
2431
2432
2433 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2434 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2435 != VPORT_INFO_REV)) {
2436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2437 "0545 lpfc_create_static_vport bad"
2438 " information header 0x%x 0x%x\n",
2439 le32_to_cpu(vport_info->signature),
2440 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2441
2442 goto out;
2443 }
2444
2445 shost = lpfc_shost_from_vport(phba->pport);
2446
2447 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2448 memset(&vport_id, 0, sizeof(vport_id));
2449 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2450 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2451 if (!vport_id.port_name || !vport_id.node_name)
2452 continue;
2453
2454 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2455 vport_id.vport_type = FC_PORTTYPE_NPIV;
2456 vport_id.disable = false;
2457 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2458
2459 if (!new_fc_vport) {
2460 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2461 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04002462 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04002463 continue;
2464 }
2465
2466 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2467 vport->vport_flag |= STATIC_VPORT;
2468 }
2469
2470out:
James Smart21e9a0a2009-05-22 14:53:21 -04002471 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04002472 if (rc != MBX_TIMEOUT) {
2473 if (pmb->context2) {
2474 mp = (struct lpfc_dmabuf *) pmb->context2;
2475 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2476 kfree(mp);
2477 }
James Smart21e9a0a2009-05-22 14:53:21 -04002478 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002479 }
James Smart21e9a0a2009-05-22 14:53:21 -04002480
2481 return;
2482}
2483
dea31012005-04-17 16:05:31 -05002484/*
2485 * This routine handles processing a Fabric REG_LOGIN mailbox
2486 * command upon completion. It is setup in the LPFC_MBOXQ
2487 * as the completion routine when the command is
2488 * handed off to the SLI layer.
2489 */
2490void
James Smart2e0fef82007-06-17 19:56:36 -05002491lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002492{
James Smart92d7f7b2007-06-17 19:56:38 -05002493 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04002494 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002495 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05002496 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002497
James Smart549e55c2007-08-02 11:09:51 -04002498 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04002499 pmb->context1 = NULL;
2500 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05002501 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04002502 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2503 "0258 Register Fabric login error: 0x%x\n",
2504 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05002505 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2506 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002507 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002508
James Smart92d7f7b2007-06-17 19:56:38 -05002509 if (phba->fc_topology == TOPOLOGY_LOOP) {
2510 /* FLOGI failed, use loop map to make discovery list */
2511 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002512
James Smart92d7f7b2007-06-17 19:56:38 -05002513 /* Start discovery */
2514 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05002515 /* Decrement the reference count to ndlp after the
2516 * reference to the ndlp are done.
2517 */
2518 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05002519 return;
2520 }
2521
2522 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05002523 /* Decrement the reference count to ndlp after the reference
2524 * to the ndlp are done.
2525 */
2526 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002527 return;
2528 }
2529
dea31012005-04-17 16:05:31 -05002530 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04002531 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05002532 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002533 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002534
James Smart2e0fef82007-06-17 19:56:36 -05002535 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart21e9a0a2009-05-22 14:53:21 -04002536 lpfc_start_fdiscs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002537 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05002538 }
2539
2540 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2541 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002542 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002543
2544 /* Drop the reference count from the mbox at the end after
2545 * all the current reference to the ndlp have been done.
2546 */
2547 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002548 return;
2549}
2550
2551/*
2552 * This routine handles processing a NameServer REG_LOGIN mailbox
2553 * command upon completion. It is setup in the LPFC_MBOXQ
2554 * as the completion routine when the command is
2555 * handed off to the SLI layer.
2556 */
2557void
James Smart2e0fef82007-06-17 19:56:36 -05002558lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002559{
James Smart21e9a0a2009-05-22 14:53:21 -04002560 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002561 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2562 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2563 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002564
2565 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05002566out:
James Smart21e9a0a2009-05-22 14:53:21 -04002567 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2568 "0260 Register NameServer error: 0x%x\n",
2569 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05002570 /* decrement the node reference count held for this
2571 * callback function.
2572 */
James Smart329f9bc2007-04-25 09:53:01 -04002573 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002574 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2575 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04002576 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04002577
2578 /* If no other thread is using the ndlp, free it */
2579 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05002580
James Smart92d7f7b2007-06-17 19:56:38 -05002581 if (phba->fc_topology == TOPOLOGY_LOOP) {
2582 /*
2583 * RegLogin failed, use loop map to make discovery
2584 * list
2585 */
2586 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002587
James Smart92d7f7b2007-06-17 19:56:38 -05002588 /* Start discovery */
2589 lpfc_disc_start(vport);
2590 return;
2591 }
2592 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05002593 return;
2594 }
2595
2596 pmb->context1 = NULL;
2597
dea31012005-04-17 16:05:31 -05002598 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04002599 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05002600 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002601 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002602
James Smart2e0fef82007-06-17 19:56:36 -05002603 if (vport->port_state < LPFC_VPORT_READY) {
2604 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05002605 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
2606 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
2607 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2608 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
2609 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
2610
2611 /* Issue SCR just before NameServer GID_FT Query */
2612 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05002613 }
2614
James Smart2e0fef82007-06-17 19:56:36 -05002615 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002616 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05002617 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05002618 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05002619 goto out;
dea31012005-04-17 16:05:31 -05002620 }
2621
James Smartfa4066b2008-01-11 01:53:27 -05002622 /* decrement the node reference count held for this
2623 * callback function.
2624 */
James Smart329f9bc2007-04-25 09:53:01 -04002625 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002626 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2627 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002628 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002629
2630 return;
2631}
2632
2633static void
James Smart2e0fef82007-06-17 19:56:36 -05002634lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002635{
James Smart2e0fef82007-06-17 19:56:36 -05002636 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2637 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05002638 struct lpfc_rport_data *rdata;
2639 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05002640 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002641
2642 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07002643 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2644 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05002645 rport_ids.port_id = ndlp->nlp_DID;
2646 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05002647
James Smart329f9bc2007-04-25 09:53:01 -04002648 /*
2649 * We leave our node pointer in rport->dd_data when we unregister a
2650 * FCP target port. But fc_remote_port_add zeros the space to which
2651 * rport->dd_data points. So, if we're reusing a previously
2652 * registered port, drop the reference that we took the last time we
2653 * registered the port.
2654 */
2655 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05002656 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04002657 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05002658
2659 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
2660 "rport add: did:x%x flg:x%x type x%x",
2661 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2662
James Smart2e0fef82007-06-17 19:56:36 -05002663 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04002664 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05002665 dev_printk(KERN_WARNING, &phba->pcidev->dev,
2666 "Warning: fc_remote_port_add failed\n");
2667 return;
2668 }
2669
2670 /* initialize static port data */
2671 rport->maxframe_size = ndlp->nlp_maxframe;
2672 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05002673 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04002674 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05002675
2676 if (ndlp->nlp_type & NLP_FCP_TARGET)
2677 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2678 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
2679 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2680
2681
2682 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
2683 fc_remote_port_rolechg(rport, rport_ids.roles);
2684
James Smart071fbd3d2006-04-15 11:53:20 -04002685 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002686 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04002687 ndlp->nlp_sid = rport->scsi_target_id;
2688 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002689 return;
2690}
2691
2692static void
James Smart2e0fef82007-06-17 19:56:36 -05002693lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002694{
2695 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04002696
James Smart858c9f62007-06-17 19:56:39 -05002697 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
2698 "rport delete: did:x%x flg:x%x type x%x",
2699 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2700
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002701 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05002702
2703 return;
2704}
2705
James Smartde0c5b32007-04-25 09:52:27 -04002706static void
James Smart2e0fef82007-06-17 19:56:36 -05002707lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04002708{
James Smart2e0fef82007-06-17 19:56:36 -05002709 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2710
2711 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002712 switch (state) {
2713 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002714 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002715 break;
2716 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002717 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002718 break;
2719 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002720 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002721 break;
2722 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002723 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002724 break;
2725 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002726 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002727 break;
2728 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002729 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002730 break;
2731 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002732 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002733 break;
2734 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002735 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002736 break;
2737 }
James Smart2e0fef82007-06-17 19:56:36 -05002738 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002739}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002740
James Smartde0c5b32007-04-25 09:52:27 -04002741static void
James Smart2e0fef82007-06-17 19:56:36 -05002742lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04002743 int old_state, int new_state)
2744{
James Smart2e0fef82007-06-17 19:56:36 -05002745 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2746
James Smartde0c5b32007-04-25 09:52:27 -04002747 if (new_state == NLP_STE_UNMAPPED_NODE) {
2748 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2749 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2750 ndlp->nlp_type |= NLP_FC_NODE;
2751 }
2752 if (new_state == NLP_STE_MAPPED_NODE)
2753 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2754 if (new_state == NLP_STE_NPR_NODE)
2755 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05002756
James Smartde0c5b32007-04-25 09:52:27 -04002757 /* Transport interface */
2758 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
2759 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002760 vport->phba->nport_event_cnt++;
2761 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002762 }
dea31012005-04-17 16:05:31 -05002763
James Smartde0c5b32007-04-25 09:52:27 -04002764 if (new_state == NLP_STE_MAPPED_NODE ||
2765 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05002766 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05002767 /*
2768 * Tell the fc transport about the port, if we haven't
2769 * already. If we have, and it's a scsi entity, be
2770 * sure to unblock any attached scsi devices
2771 */
2772 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002773 }
James Smartea2151b2008-09-07 11:52:10 -04002774 if ((new_state == NLP_STE_MAPPED_NODE) &&
2775 (vport->stat_data_enabled)) {
2776 /*
2777 * A new target is discovered, if there is no buffer for
2778 * statistical data collection allocate buffer.
2779 */
2780 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
2781 sizeof(struct lpfc_scsicmd_bkt),
2782 GFP_KERNEL);
2783
2784 if (!ndlp->lat_data)
2785 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2786 "0286 lpfc_nlp_state_cleanup failed to "
2787 "allocate statistical data buffer DID "
2788 "0x%x\n", ndlp->nlp_DID);
2789 }
James Smart858c9f62007-06-17 19:56:39 -05002790 /*
2791 * if we added to Mapped list, but the remote port
2792 * registration failed or assigned a target id outside
2793 * our presentable range - move the node to the
2794 * Unmapped List
2795 */
James Smartde0c5b32007-04-25 09:52:27 -04002796 if (new_state == NLP_STE_MAPPED_NODE &&
2797 (!ndlp->rport ||
2798 ndlp->rport->scsi_target_id == -1 ||
2799 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05002800 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002801 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05002802 spin_unlock_irq(shost->host_lock);
2803 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002804 }
James Smartde0c5b32007-04-25 09:52:27 -04002805}
2806
James Smart685f0bf2007-04-25 09:53:08 -04002807static char *
2808lpfc_nlp_state_name(char *buffer, size_t size, int state)
2809{
2810 static char *states[] = {
2811 [NLP_STE_UNUSED_NODE] = "UNUSED",
2812 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
2813 [NLP_STE_ADISC_ISSUE] = "ADISC",
2814 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
2815 [NLP_STE_PRLI_ISSUE] = "PRLI",
2816 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
2817 [NLP_STE_MAPPED_NODE] = "MAPPED",
2818 [NLP_STE_NPR_NODE] = "NPR",
2819 };
2820
James Smart311464e2007-08-02 11:10:37 -04002821 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04002822 strlcpy(buffer, states[state], size);
2823 else
2824 snprintf(buffer, size, "unknown (%d)", state);
2825 return buffer;
2826}
2827
James Smartde0c5b32007-04-25 09:52:27 -04002828void
James Smart2e0fef82007-06-17 19:56:36 -05002829lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2830 int state)
James Smartde0c5b32007-04-25 09:52:27 -04002831{
James Smart2e0fef82007-06-17 19:56:36 -05002832 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04002833 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04002834 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04002835
James Smarte8b62012007-08-02 11:10:09 -04002836 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2837 "0904 NPort state transition x%06x, %s -> %s\n",
2838 ndlp->nlp_DID,
2839 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
2840 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05002841
2842 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2843 "node statechg did:x%x old:%d ste:%d",
2844 ndlp->nlp_DID, old_state, state);
2845
James Smartde0c5b32007-04-25 09:52:27 -04002846 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04002847 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05002848 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002849 if (old_state == NLP_STE_UNMAPPED_NODE) {
2850 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
2851 ndlp->nlp_type &= ~NLP_FC_NODE;
2852 }
2853
James Smart685f0bf2007-04-25 09:53:08 -04002854 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05002855 spin_lock_irq(shost->host_lock);
2856 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2857 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002858 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05002859 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04002860
2861 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05002862 lpfc_nlp_counters(vport, state, 1);
2863 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04002864}
2865
2866void
James Smarte47c9092008-02-08 18:49:26 -05002867lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2868{
2869 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2870
2871 if (list_empty(&ndlp->nlp_listp)) {
2872 spin_lock_irq(shost->host_lock);
2873 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2874 spin_unlock_irq(shost->host_lock);
2875 }
2876}
2877
2878void
James Smart2e0fef82007-06-17 19:56:36 -05002879lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04002880{
James Smart2e0fef82007-06-17 19:56:36 -05002881 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2882
James Smart0d2b6b82008-06-14 22:52:47 -04002883 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002884 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05002885 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2886 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002887 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05002888 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002889 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05002890 NLP_STE_UNUSED_NODE);
2891}
2892
Adrian Bunk4d9db012008-02-14 23:24:02 +02002893static void
James Smarte47c9092008-02-08 18:49:26 -05002894lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2895{
James Smart0d2b6b82008-06-14 22:52:47 -04002896 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05002897 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2898 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2899 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2900 NLP_STE_UNUSED_NODE);
2901}
James Smart109f6ed2008-12-04 22:39:08 -05002902/**
James Smart3621a712009-04-06 18:47:14 -04002903 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05002904 * @vport: Pointer to Virtual Port object.
2905 * @ndlp: Pointer to FC node object.
2906 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04002907 *
2908 * This function is always called when node object need to be initialized.
2909 * It initializes all the fields of the node object. Although the reference
2910 * to phba from @ndlp can be obtained indirectly through it's reference to
2911 * @vport, a direct reference to phba is taken here by @ndlp. This is due
2912 * to the life-span of the @ndlp might go beyond the existence of @vport as
2913 * the final release of ndlp is determined by its reference count. And, the
2914 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05002915 **/
2916static inline void
2917lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2918 uint32_t did)
2919{
2920 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2921 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2922 init_timer(&ndlp->nlp_delayfunc);
2923 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2924 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2925 ndlp->nlp_DID = did;
2926 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04002927 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05002928 ndlp->nlp_sid = NLP_NO_SID;
2929 kref_init(&ndlp->kref);
2930 NLP_INT_NODE_ACT(ndlp);
2931 atomic_set(&ndlp->cmd_pending, 0);
2932 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2933}
James Smarte47c9092008-02-08 18:49:26 -05002934
2935struct lpfc_nodelist *
2936lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2937 int state)
2938{
2939 struct lpfc_hba *phba = vport->phba;
2940 uint32_t did;
2941 unsigned long flags;
2942
2943 if (!ndlp)
2944 return NULL;
2945
2946 spin_lock_irqsave(&phba->ndlp_lock, flags);
2947 /* The ndlp should not be in memory free mode */
2948 if (NLP_CHK_FREE_REQ(ndlp)) {
2949 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2950 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2951 "0277 lpfc_enable_node: ndlp:x%p "
2952 "usgmap:x%x refcnt:%d\n",
2953 (void *)ndlp, ndlp->nlp_usg_map,
2954 atomic_read(&ndlp->kref.refcount));
2955 return NULL;
2956 }
2957 /* The ndlp should not already be in active mode */
2958 if (NLP_CHK_NODE_ACT(ndlp)) {
2959 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2960 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2961 "0278 lpfc_enable_node: ndlp:x%p "
2962 "usgmap:x%x refcnt:%d\n",
2963 (void *)ndlp, ndlp->nlp_usg_map,
2964 atomic_read(&ndlp->kref.refcount));
2965 return NULL;
2966 }
2967
2968 /* Keep the original DID */
2969 did = ndlp->nlp_DID;
2970
2971 /* re-initialize ndlp except of ndlp linked list pointer */
2972 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
2973 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05002974 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05002975
2976 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2977
2978 if (state != NLP_STE_UNUSED_NODE)
2979 lpfc_nlp_set_state(vport, ndlp, state);
2980
2981 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2982 "node enable: did:x%x",
2983 ndlp->nlp_DID, 0, 0);
2984 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04002985}
2986
2987void
James Smart2e0fef82007-06-17 19:56:36 -05002988lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04002989{
James Smart87af33f2007-10-27 13:37:43 -04002990 /*
James Smartfa4066b2008-01-11 01:53:27 -05002991 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04002992 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05002993 * the ndlp from the vport. The ndlp marked as UNUSED on the list
2994 * until ALL other outstanding threads have completed. We check
2995 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04002996 */
James Smartfa4066b2008-01-11 01:53:27 -05002997 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2998 return;
James Smart51ef4c22007-08-02 11:10:31 -04002999 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003000 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003001 return;
dea31012005-04-17 16:05:31 -05003002}
3003
3004/*
3005 * Start / ReStart rescue timer for Discovery / RSCN handling
3006 */
3007void
James Smart2e0fef82007-06-17 19:56:36 -05003008lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003009{
James Smart2e0fef82007-06-17 19:56:36 -05003010 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3011 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003012 uint32_t tmo;
3013
James Smart2e0fef82007-06-17 19:56:36 -05003014 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003015 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003016 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3017 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003018 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003019 * FC spec states we need 3 * ratov for CT requests
3020 */
3021 tmo = ((phba->fc_ratov * 3) + 3);
3022 }
dea31012005-04-17 16:05:31 -05003023
James Smart858c9f62007-06-17 19:56:39 -05003024
3025 if (!timer_pending(&vport->fc_disctmo)) {
3026 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3027 "set disc timer: tmo:x%x state:x%x flg:x%x",
3028 tmo, vport->port_state, vport->fc_flag);
3029 }
3030
James Smart2e0fef82007-06-17 19:56:36 -05003031 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3032 spin_lock_irq(shost->host_lock);
3033 vport->fc_flag |= FC_DISC_TMO;
3034 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003035
3036 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003037 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3038 "0247 Start Discovery Timer state x%x "
3039 "Data: x%x x%lx x%x x%x\n",
3040 vport->port_state, tmo,
3041 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3042 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003043
3044 return;
3045}
3046
3047/*
3048 * Cancel rescue timer for Discovery / RSCN handling
3049 */
3050int
James Smart2e0fef82007-06-17 19:56:36 -05003051lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003052{
James Smart2e0fef82007-06-17 19:56:36 -05003053 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003054 unsigned long iflags;
3055
James Smart858c9f62007-06-17 19:56:39 -05003056 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3057 "can disc timer: state:x%x rtry:x%x flg:x%x",
3058 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3059
dea31012005-04-17 16:05:31 -05003060 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003061 if (vport->fc_flag & FC_DISC_TMO) {
3062 spin_lock_irqsave(shost->host_lock, iflags);
3063 vport->fc_flag &= ~FC_DISC_TMO;
3064 spin_unlock_irqrestore(shost->host_lock, iflags);
3065 del_timer_sync(&vport->fc_disctmo);
3066 spin_lock_irqsave(&vport->work_port_lock, iflags);
3067 vport->work_port_events &= ~WORKER_DISC_TMO;
3068 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003069 }
3070
3071 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003072 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3073 "0248 Cancel Discovery Timer state x%x "
3074 "Data: x%x x%x x%x\n",
3075 vport->port_state, vport->fc_flag,
3076 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003077 return 0;
dea31012005-04-17 16:05:31 -05003078}
3079
3080/*
3081 * Check specified ring for outstanding IOCB on the SLI queue
3082 * Return true if iocb matches the specified nport
3083 */
3084int
James Smart2e0fef82007-06-17 19:56:36 -05003085lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3086 struct lpfc_sli_ring *pring,
3087 struct lpfc_iocbq *iocb,
3088 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003089{
James Smart2e0fef82007-06-17 19:56:36 -05003090 struct lpfc_sli *psli = &phba->sli;
3091 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003092 struct lpfc_vport *vport = ndlp->vport;
3093
3094 if (iocb->vport != vport)
3095 return 0;
3096
dea31012005-04-17 16:05:31 -05003097 if (pring->ringno == LPFC_ELS_RING) {
3098 switch (icmd->ulpCommand) {
3099 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003100 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003101 return 1;
dea31012005-04-17 16:05:31 -05003102 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003103 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3104 return 1;
dea31012005-04-17 16:05:31 -05003105 case CMD_XMIT_ELS_RSP64_CX:
3106 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003107 return 1;
dea31012005-04-17 16:05:31 -05003108 }
James Smarta4bc3372006-12-02 13:34:16 -05003109 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003110
3111 } else if (pring->ringno == psli->fcp_ring) {
3112 /* Skip match check if waiting to relogin to FCP target */
3113 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003114 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003115 return 0;
dea31012005-04-17 16:05:31 -05003116 }
3117 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003118 return 1;
dea31012005-04-17 16:05:31 -05003119 }
3120 } else if (pring->ringno == psli->next_ring) {
3121
3122 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003123 return 0;
dea31012005-04-17 16:05:31 -05003124}
3125
3126/*
3127 * Free resources / clean up outstanding I/Os
3128 * associated with nlp_rpi in the LPFC_NODELIST entry.
3129 */
3130static int
James Smart2e0fef82007-06-17 19:56:36 -05003131lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003132{
James Smart2534ba72007-04-25 09:52:20 -04003133 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003134 struct lpfc_sli *psli;
3135 struct lpfc_sli_ring *pring;
3136 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003137 uint32_t i;
dea31012005-04-17 16:05:31 -05003138
James Smart92d7f7b2007-06-17 19:56:38 -05003139 lpfc_fabric_abort_nport(ndlp);
3140
dea31012005-04-17 16:05:31 -05003141 /*
3142 * Everything that matches on txcmplq will be returned
3143 * by firmware with a no rpi error.
3144 */
3145 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003146 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003147 /* Now process each ring */
3148 for (i = 0; i < psli->num_rings; i++) {
3149 pring = &psli->ring[i];
3150
James Smart2e0fef82007-06-17 19:56:36 -05003151 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003152 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003153 list) {
dea31012005-04-17 16:05:31 -05003154 /*
3155 * Check to see if iocb matches the nport we are
3156 * looking for
3157 */
James Smart92d7f7b2007-06-17 19:56:38 -05003158 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3159 ndlp))) {
dea31012005-04-17 16:05:31 -05003160 /* It matches, so deque and call compl
3161 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003162 list_move_tail(&iocb->list,
3163 &completions);
dea31012005-04-17 16:05:31 -05003164 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003165 }
3166 }
James Smart2e0fef82007-06-17 19:56:36 -05003167 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003168 }
3169 }
James Smart2534ba72007-04-25 09:52:20 -04003170
James Smarta257bf92009-04-06 18:48:10 -04003171 /* Cancel all the IOCBs from the completions list */
3172 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3173 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003174
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003175 return 0;
dea31012005-04-17 16:05:31 -05003176}
3177
3178/*
3179 * Free rpi associated with LPFC_NODELIST entry.
3180 * This routine is called from lpfc_freenode(), when we are removing
3181 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3182 * LOGO that completes successfully, and we are waiting to PLOGI back
3183 * to the remote NPort. In addition, it is called after we receive
3184 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3185 * we are waiting to PLOGI back to the remote NPort.
3186 */
3187int
James Smart2e0fef82007-06-17 19:56:36 -05003188lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003189{
James Smart2e0fef82007-06-17 19:56:36 -05003190 struct lpfc_hba *phba = vport->phba;
3191 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003192 int rc;
3193
James Smart21e9a0a2009-05-22 14:53:21 -04003194 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003195 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3196 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003197 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003198 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003199 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003200 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003201 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003202 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003203 }
dea31012005-04-17 16:05:31 -05003204 lpfc_no_rpi(phba, ndlp);
3205 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003206 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003207 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003208 return 1;
3209 }
3210 return 0;
3211}
3212
James Smart92d7f7b2007-06-17 19:56:38 -05003213void
3214lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3215{
3216 struct lpfc_hba *phba = vport->phba;
3217 LPFC_MBOXQ_t *mbox;
3218 int rc;
3219
3220 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3221 if (mbox) {
3222 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3223 mbox->vport = vport;
3224 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003225 mbox->context1 = NULL;
3226 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003227 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003228 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003229
3230 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3231 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3232 "1836 Could not issue "
3233 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003234 }
3235}
3236
3237void
3238lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3239{
3240 struct lpfc_hba *phba = vport->phba;
3241 LPFC_MBOXQ_t *mbox;
3242 int rc;
3243
3244 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3245 if (mbox) {
3246 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3247 mbox->vport = vport;
3248 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003249 mbox->context1 = NULL;
3250 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003251 if (rc != MBX_TIMEOUT)
3252 mempool_free(mbox, phba->mbox_mem_pool);
3253
3254 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003255 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3256 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003257 "unreg_did (default rpis) status %d\n",
3258 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003259 }
3260}
3261
dea31012005-04-17 16:05:31 -05003262/*
3263 * Free resources associated with LPFC_NODELIST entry
3264 * so it can be freed.
3265 */
3266static int
James Smart2e0fef82007-06-17 19:56:36 -05003267lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003268{
James Smart2e0fef82007-06-17 19:56:36 -05003269 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3270 struct lpfc_hba *phba = vport->phba;
3271 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003272 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003273
3274 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003275 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3276 "0900 Cleanup node for NPort x%x "
3277 "Data: x%x x%x x%x\n",
3278 ndlp->nlp_DID, ndlp->nlp_flag,
3279 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003280 if (NLP_CHK_FREE_REQ(ndlp)) {
3281 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3282 "0280 lpfc_cleanup_node: ndlp:x%p "
3283 "usgmap:x%x refcnt:%d\n",
3284 (void *)ndlp, ndlp->nlp_usg_map,
3285 atomic_read(&ndlp->kref.refcount));
3286 lpfc_dequeue_node(vport, ndlp);
3287 } else {
3288 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3289 "0281 lpfc_cleanup_node: ndlp:x%p "
3290 "usgmap:x%x refcnt:%d\n",
3291 (void *)ndlp, ndlp->nlp_usg_map,
3292 atomic_read(&ndlp->kref.refcount));
3293 lpfc_disable_node(vport, ndlp);
3294 }
dea31012005-04-17 16:05:31 -05003295
dea31012005-04-17 16:05:31 -05003296 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3297 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003298 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003299 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3300 mb->context2 = NULL;
3301 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3302 }
3303 }
James Smart33ccf8d2006-08-17 11:57:58 -04003304
James Smart2e0fef82007-06-17 19:56:36 -05003305 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003306 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003307 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003308 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003309 mp = (struct lpfc_dmabuf *) (mb->context1);
3310 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003311 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003312 kfree(mp);
3313 }
3314 list_del(&mb->list);
3315 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003316 /* We shall not invoke the lpfc_nlp_put to decrement
3317 * the ndlp reference count as we are in the process
3318 * of lpfc_nlp_release.
3319 */
dea31012005-04-17 16:05:31 -05003320 }
3321 }
James Smart2e0fef82007-06-17 19:56:36 -05003322 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003323
James Smarte47c9092008-02-08 18:49:26 -05003324 lpfc_els_abort(phba, ndlp);
3325
James Smart2e0fef82007-06-17 19:56:36 -05003326 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003327 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003328 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003329
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003330 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003331 del_timer_sync(&ndlp->nlp_delayfunc);
3332
James Smart0d2b6b82008-06-14 22:52:47 -04003333 list_del_init(&ndlp->els_retry_evt.evt_listp);
3334 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003335
James Smart2e0fef82007-06-17 19:56:36 -05003336 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003337
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003338 return 0;
dea31012005-04-17 16:05:31 -05003339}
3340
3341/*
3342 * Check to see if we can free the nlp back to the freelist.
3343 * If we are in the middle of using the nlp in the discovery state
3344 * machine, defer the free till we reach the end of the state machine.
3345 */
James Smart329f9bc2007-04-25 09:53:01 -04003346static void
James Smart2e0fef82007-06-17 19:56:36 -05003347lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003348{
James Smarta8adb832007-10-27 13:37:53 -04003349 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003350 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003351 LPFC_MBOXQ_t *mbox;
3352 int rc;
dea31012005-04-17 16:05:31 -05003353
James Smart0d2b6b82008-06-14 22:52:47 -04003354 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003355 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3356 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003357 /* For this case we need to cleanup the default rpi
3358 * allocated by the firmware.
3359 */
3360 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3361 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003362 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003363 (uint8_t *) &vport->fc_sparam, mbox, 0);
3364 if (rc) {
3365 mempool_free(mbox, phba->mbox_mem_pool);
3366 }
3367 else {
3368 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3369 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3370 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003371 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003372 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3373 if (rc == MBX_NOT_FINISHED) {
3374 mempool_free(mbox, phba->mbox_mem_pool);
3375 }
3376 }
3377 }
3378 }
James Smart2e0fef82007-06-17 19:56:36 -05003379 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003380
James Smart2e0fef82007-06-17 19:56:36 -05003381 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003382 * We can get here with a non-NULL ndlp->rport because when we
3383 * unregister a rport we don't break the rport/node linkage. So if we
3384 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003385 */
James Smart92d7f7b2007-06-17 19:56:38 -05003386 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003387 rdata = ndlp->rport->dd_data;
3388 rdata->pnode = NULL;
3389 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003390 }
dea31012005-04-17 16:05:31 -05003391}
3392
3393static int
James Smart2e0fef82007-06-17 19:56:36 -05003394lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3395 uint32_t did)
dea31012005-04-17 16:05:31 -05003396{
James Smart2e0fef82007-06-17 19:56:36 -05003397 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003398
3399 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003400 return 0;
dea31012005-04-17 16:05:31 -05003401
dea31012005-04-17 16:05:31 -05003402 /* First check for Direct match */
3403 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003404 return 1;
dea31012005-04-17 16:05:31 -05003405
3406 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003407 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003408 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003409 return 0;
dea31012005-04-17 16:05:31 -05003410 }
3411
3412 matchdid.un.word = did;
3413 ndlpdid.un.word = ndlp->nlp_DID;
3414 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3415 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3416 (mydid.un.b.area == matchdid.un.b.area)) {
3417 if ((ndlpdid.un.b.domain == 0) &&
3418 (ndlpdid.un.b.area == 0)) {
3419 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003420 return 1;
dea31012005-04-17 16:05:31 -05003421 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003422 return 0;
dea31012005-04-17 16:05:31 -05003423 }
3424
3425 matchdid.un.word = ndlp->nlp_DID;
3426 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3427 (mydid.un.b.area == ndlpdid.un.b.area)) {
3428 if ((matchdid.un.b.domain == 0) &&
3429 (matchdid.un.b.area == 0)) {
3430 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003431 return 1;
dea31012005-04-17 16:05:31 -05003432 }
3433 }
3434 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003435 return 0;
dea31012005-04-17 16:05:31 -05003436}
3437
James Smart685f0bf2007-04-25 09:53:08 -04003438/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003439static struct lpfc_nodelist *
3440__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003441{
James Smart2fb9bd82006-12-02 13:33:57 -05003442 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003443 uint32_t data1;
3444
James Smart2e0fef82007-06-17 19:56:36 -05003445 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3446 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04003447 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3448 ((uint32_t) ndlp->nlp_xri << 16) |
3449 ((uint32_t) ndlp->nlp_type << 8) |
3450 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04003451 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3452 "0929 FIND node DID "
3453 "Data: x%p x%x x%x x%x\n",
3454 ndlp, ndlp->nlp_DID,
3455 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04003456 return ndlp;
dea31012005-04-17 16:05:31 -05003457 }
3458 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003459
dea31012005-04-17 16:05:31 -05003460 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04003461 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3462 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05003463 return NULL;
3464}
3465
3466struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003467lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003468{
James Smart2e0fef82007-06-17 19:56:36 -05003469 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003470 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003471
James Smart2e0fef82007-06-17 19:56:36 -05003472 spin_lock_irq(shost->host_lock);
3473 ndlp = __lpfc_findnode_did(vport, did);
3474 spin_unlock_irq(shost->host_lock);
3475 return ndlp;
3476}
3477
3478struct lpfc_nodelist *
3479lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3480{
3481 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3482 struct lpfc_nodelist *ndlp;
3483
3484 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003485 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05003486 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
3487 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05003488 return NULL;
3489 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05003490 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003491 if (!ndlp)
3492 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003493 lpfc_nlp_init(vport, ndlp, did);
3494 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3495 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003496 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003497 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003498 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05003499 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3500 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
3501 if (!ndlp)
3502 return NULL;
3503 spin_lock_irq(shost->host_lock);
3504 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3505 spin_unlock_irq(shost->host_lock);
3506 return ndlp;
dea31012005-04-17 16:05:31 -05003507 }
James Smarte47c9092008-02-08 18:49:26 -05003508
James Smart58da1ff2008-04-07 10:15:56 -04003509 if ((vport->fc_flag & FC_RSCN_MODE) &&
3510 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003511 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04003512 /* If we've already recieved a PLOGI from this NPort
3513 * we don't need to try to discover it again.
3514 */
3515 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
3516 return NULL;
3517
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003518 /* Since this node is marked for discovery,
3519 * delay timeout is not needed.
3520 */
James Smart0d2b6b82008-06-14 22:52:47 -04003521 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04003522 spin_lock_irq(shost->host_lock);
3523 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3524 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04003525 } else
dea31012005-04-17 16:05:31 -05003526 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003527 } else {
James Smart87af33f2007-10-27 13:37:43 -04003528 /* If we've already recieved a PLOGI from this NPort,
3529 * or we are already in the process of discovery on it,
3530 * we don't need to try to discover it again.
3531 */
James Smart685f0bf2007-04-25 09:53:08 -04003532 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04003533 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3534 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05003535 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003536 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3537 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003538 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003539 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003540 }
3541 return ndlp;
3542}
3543
3544/* Build a list of nodes to discover based on the loopmap */
3545void
James Smart2e0fef82007-06-17 19:56:36 -05003546lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003547{
James Smart2e0fef82007-06-17 19:56:36 -05003548 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003549 int j;
3550 uint32_t alpa, index;
3551
James Smart2e0fef82007-06-17 19:56:36 -05003552 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003553 return;
James Smart2e0fef82007-06-17 19:56:36 -05003554
3555 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05003556 return;
dea31012005-04-17 16:05:31 -05003557
3558 /* Check for loop map present or not */
3559 if (phba->alpa_map[0]) {
3560 for (j = 1; j <= phba->alpa_map[0]; j++) {
3561 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05003562 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05003563 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003564 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003565 }
3566 } else {
3567 /* No alpamap, so try all alpa's */
3568 for (j = 0; j < FC_MAXLOOP; j++) {
3569 /* If cfg_scan_down is set, start from highest
3570 * ALPA (0xef) to lowest (0x1).
3571 */
James Smart3de2a652007-08-02 11:09:59 -04003572 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05003573 index = j;
3574 else
3575 index = FC_MAXLOOP - j - 1;
3576 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05003577 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05003578 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003579 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003580 }
3581 }
3582 return;
3583}
3584
dea31012005-04-17 16:05:31 -05003585void
James Smart2e0fef82007-06-17 19:56:36 -05003586lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003587{
dea31012005-04-17 16:05:31 -05003588 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05003589 struct lpfc_sli *psli = &phba->sli;
3590 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
3591 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
3592 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
3593 int rc;
3594
James Smart92d7f7b2007-06-17 19:56:38 -05003595 /*
3596 * if it's not a physical port or if we already send
3597 * clear_la then don't send it.
3598 */
3599 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04003600 (vport->port_type != LPFC_PHYSICAL_PORT) ||
3601 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05003602 return;
3603
James Smart2e0fef82007-06-17 19:56:36 -05003604 /* Link up discovery */
3605 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
3606 phba->link_state = LPFC_CLEAR_LA;
3607 lpfc_clear_la(phba, mbox);
3608 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
3609 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003610 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05003611 if (rc == MBX_NOT_FINISHED) {
3612 mempool_free(mbox, phba->mbox_mem_pool);
3613 lpfc_disc_flush_list(vport);
3614 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3615 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3616 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05003617 phba->link_state = LPFC_HBA_ERROR;
3618 }
3619 }
3620}
3621
3622/* Reg_vpi to tell firmware to resume normal operations */
3623void
3624lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
3625{
3626 LPFC_MBOXQ_t *regvpimbox;
3627
3628 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3629 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04003630 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003631 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
3632 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003633 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05003634 == MBX_NOT_FINISHED) {
3635 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003636 }
3637 }
3638}
3639
3640/* Start Link up / RSCN discovery on NPR nodes */
3641void
3642lpfc_disc_start(struct lpfc_vport *vport)
3643{
3644 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3645 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04003646 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05003647 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04003648 int did_changed;
dea31012005-04-17 16:05:31 -05003649
James Smart2e0fef82007-06-17 19:56:36 -05003650 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003651 return;
James Smart2e0fef82007-06-17 19:56:36 -05003652
3653 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05003654 clear_la_pending = 1;
3655 else
3656 clear_la_pending = 0;
3657
James Smart2e0fef82007-06-17 19:56:36 -05003658 if (vport->port_state < LPFC_VPORT_READY)
3659 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05003660
James Smart2e0fef82007-06-17 19:56:36 -05003661 lpfc_set_disctmo(vport);
3662
3663 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05003664 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003665 else
dea31012005-04-17 16:05:31 -05003666 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003667
3668 vport->fc_prevDID = vport->fc_myDID;
3669 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05003670
3671 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003672 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3673 "0202 Start Discovery hba state x%x "
3674 "Data: x%x x%x x%x\n",
3675 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
3676 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003677
3678 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003679 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05003680
3681 if (num_sent)
3682 return;
3683
James Smart92d7f7b2007-06-17 19:56:38 -05003684 /*
3685 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
3686 * continue discovery.
3687 */
3688 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05003689 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04003690 !(vport->fc_flag & FC_RSCN_MODE) &&
3691 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003692 lpfc_issue_reg_vpi(phba, vport);
3693 return;
3694 }
James Smart2e0fef82007-06-17 19:56:36 -05003695
James Smart92d7f7b2007-06-17 19:56:38 -05003696 /*
3697 * For SLI2, we need to set port_state to READY and continue
3698 * discovery.
3699 */
3700 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
3701 /* If we get here, there is nothing to ADISC */
3702 if (vport->port_type == LPFC_PHYSICAL_PORT)
3703 lpfc_issue_clear_la(phba, vport);
3704
3705 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05003706 vport->num_disc_nodes = 0;
3707 /* go thru NPR nodes and issue ELS PLOGIs */
3708 if (vport->fc_npr_cnt)
3709 lpfc_els_disc_plogi(vport);
3710
3711 if (!vport->num_disc_nodes) {
3712 spin_lock_irq(shost->host_lock);
3713 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3714 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003715 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05003716 }
3717 }
James Smart92d7f7b2007-06-17 19:56:38 -05003718 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05003719 } else {
3720 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003721 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05003722
3723 if (num_sent)
3724 return;
3725
James Smart2e0fef82007-06-17 19:56:36 -05003726 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05003727 /* Check to see if more RSCNs came in while we
3728 * were processing this one.
3729 */
James Smart2e0fef82007-06-17 19:56:36 -05003730 if ((vport->fc_rscn_id_cnt == 0) &&
3731 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
3732 spin_lock_irq(shost->host_lock);
3733 vport->fc_flag &= ~FC_RSCN_MODE;
3734 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003735 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003736 } else
James Smart2e0fef82007-06-17 19:56:36 -05003737 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05003738 }
3739 }
3740 return;
3741}
3742
3743/*
3744 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
3745 * ring the match the sppecified nodelist.
3746 */
3747static void
James Smart2e0fef82007-06-17 19:56:36 -05003748lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003749{
James Smart2534ba72007-04-25 09:52:20 -04003750 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003751 struct lpfc_sli *psli;
3752 IOCB_t *icmd;
3753 struct lpfc_iocbq *iocb, *next_iocb;
3754 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003755
3756 psli = &phba->sli;
3757 pring = &psli->ring[LPFC_ELS_RING];
3758
3759 /* Error matching iocb on txq or txcmplq
3760 * First check the txq.
3761 */
James Smart2e0fef82007-06-17 19:56:36 -05003762 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003763 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3764 if (iocb->context1 != ndlp) {
3765 continue;
3766 }
3767 icmd = &iocb->iocb;
3768 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
3769 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
3770
James Smart2534ba72007-04-25 09:52:20 -04003771 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05003772 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003773 }
3774 }
3775
3776 /* Next check the txcmplq */
3777 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
3778 if (iocb->context1 != ndlp) {
3779 continue;
3780 }
3781 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05003782 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
3783 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04003784 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05003785 }
3786 }
James Smart2e0fef82007-06-17 19:56:36 -05003787 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003788
James Smarta257bf92009-04-06 18:48:10 -04003789 /* Cancel all the IOCBs from the completions list */
3790 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3791 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003792}
3793
Adrian Bunka6ababd2007-11-05 18:07:33 +01003794static void
James Smart2e0fef82007-06-17 19:56:36 -05003795lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003796{
3797 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05003798 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003799
James Smart2e0fef82007-06-17 19:56:36 -05003800 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
3801 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04003802 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003803 if (!NLP_CHK_NODE_ACT(ndlp))
3804 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003805 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3806 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
3807 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04003808 }
dea31012005-04-17 16:05:31 -05003809 }
3810 }
dea31012005-04-17 16:05:31 -05003811}
3812
James Smart92d7f7b2007-06-17 19:56:38 -05003813void
3814lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
3815{
3816 lpfc_els_flush_rscn(vport);
3817 lpfc_els_flush_cmd(vport);
3818 lpfc_disc_flush_list(vport);
3819}
3820
dea31012005-04-17 16:05:31 -05003821/*****************************************************************************/
3822/*
3823 * NAME: lpfc_disc_timeout
3824 *
3825 * FUNCTION: Fibre Channel driver discovery timeout routine.
3826 *
3827 * EXECUTION ENVIRONMENT: interrupt only
3828 *
3829 * CALLED FROM:
3830 * Timer function
3831 *
3832 * RETURNS:
3833 * none
3834 */
3835/*****************************************************************************/
3836void
3837lpfc_disc_timeout(unsigned long ptr)
3838{
James Smart2e0fef82007-06-17 19:56:36 -05003839 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
3840 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04003841 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003842 unsigned long flags = 0;
3843
3844 if (unlikely(!phba))
3845 return;
3846
James Smart5e9d9b82008-06-14 22:52:53 -04003847 spin_lock_irqsave(&vport->work_port_lock, flags);
3848 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
3849 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05003850 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04003851 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05003852
James Smart5e9d9b82008-06-14 22:52:53 -04003853 if (!tmo_posted)
3854 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003855 return;
3856}
3857
3858static void
James Smart2e0fef82007-06-17 19:56:36 -05003859lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003860{
James Smart2e0fef82007-06-17 19:56:36 -05003861 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3862 struct lpfc_hba *phba = vport->phba;
3863 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003864 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05003865 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05003866 int rc, clrlaerr = 0;
3867
James Smart2e0fef82007-06-17 19:56:36 -05003868 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05003869 return;
3870
James Smart2e0fef82007-06-17 19:56:36 -05003871 spin_lock_irq(shost->host_lock);
3872 vport->fc_flag &= ~FC_DISC_TMO;
3873 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003874
James Smart858c9f62007-06-17 19:56:39 -05003875 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3876 "disc timeout: state:x%x rtry:x%x flg:x%x",
3877 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3878
James Smart2e0fef82007-06-17 19:56:36 -05003879 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05003880
3881 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05003882 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
3883 * FAN
3884 */
3885 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04003886 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3887 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003888 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05003889 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04003890 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003891 if (!NLP_CHK_NODE_ACT(ndlp))
3892 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003893 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3894 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003895 if (ndlp->nlp_type & NLP_FABRIC) {
3896 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05003897 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04003898
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003899 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003900 /* Fail outstanding IO now since device
3901 * is marked for PLOGI.
3902 */
James Smart2e0fef82007-06-17 19:56:36 -05003903 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003904 }
3905 }
James Smart92d7f7b2007-06-17 19:56:38 -05003906 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05003907 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05003908 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003909 }
dea31012005-04-17 16:05:31 -05003910 break;
3911
James Smart92d7f7b2007-06-17 19:56:38 -05003912 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05003913 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05003914 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05003915 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04003916 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3917 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04003918 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05003919
3920 /* Assume no Fabric and go on with discovery.
3921 * Check for outstanding ELS FLOGI to abort.
3922 */
3923
3924 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05003925 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003926
3927 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05003928 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05003929 break;
3930
3931 case LPFC_FABRIC_CFG_LINK:
3932 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
3933 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04003934 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3935 "0223 Timeout while waiting for "
3936 "NameServer login\n");
dea31012005-04-17 16:05:31 -05003937 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05003938 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05003939 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04003940 lpfc_els_abort(phba, ndlp);
3941
3942 /* ReStart discovery */
3943 goto restart_disc;
dea31012005-04-17 16:05:31 -05003944
3945 case LPFC_NS_QRY:
3946 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04003947 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3948 "0224 NameServer Query timeout "
3949 "Data: x%x x%x\n",
3950 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05003951
James Smart92d7f7b2007-06-17 19:56:38 -05003952 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
3953 /* Try it one more time */
3954 vport->fc_ns_retry++;
3955 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
3956 vport->fc_ns_retry, 0);
3957 if (rc == 0)
3958 break;
dea31012005-04-17 16:05:31 -05003959 }
James Smart92d7f7b2007-06-17 19:56:38 -05003960 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003961
James Smart87af33f2007-10-27 13:37:43 -04003962restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05003963 /*
3964 * Discovery is over.
3965 * set port_state to PORT_READY if SLI2.
3966 * cmpl_reg_vpi will set port_state to READY for SLI3.
3967 */
James Smart3772a992009-05-22 14:50:54 -04003968 if (phba->sli_rev < LPFC_SLI_REV4) {
3969 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3970 lpfc_issue_reg_vpi(phba, vport);
3971 else { /* NPIV Not enabled */
3972 lpfc_issue_clear_la(phba, vport);
3973 vport->port_state = LPFC_VPORT_READY;
3974 }
dea31012005-04-17 16:05:31 -05003975 }
3976
3977 /* Setup and issue mailbox INITIALIZE LINK command */
3978 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3979 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04003980 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3981 "0206 Device Discovery "
3982 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05003983 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003984 break;
3985 }
3986
3987 lpfc_linkdown(phba);
3988 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
3989 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04003990 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05003991 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003992 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003993 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04003994 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05003995 if (rc == MBX_NOT_FINISHED)
3996 mempool_free(initlinkmbox, phba->mbox_mem_pool);
3997
3998 break;
3999
4000 case LPFC_DISC_AUTH:
4001 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004002 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4003 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004004 lpfc_disc_flush_list(vport);
4005
James Smart92d7f7b2007-06-17 19:56:38 -05004006 /*
4007 * set port_state to PORT_READY if SLI2.
4008 * cmpl_reg_vpi will set port_state to READY for SLI3.
4009 */
James Smart3772a992009-05-22 14:50:54 -04004010 if (phba->sli_rev < LPFC_SLI_REV4) {
4011 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4012 lpfc_issue_reg_vpi(phba, vport);
4013 else { /* NPIV Not enabled */
4014 lpfc_issue_clear_la(phba, vport);
4015 vport->port_state = LPFC_VPORT_READY;
4016 }
dea31012005-04-17 16:05:31 -05004017 }
4018 break;
4019
James Smart2e0fef82007-06-17 19:56:36 -05004020 case LPFC_VPORT_READY:
4021 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004022 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4023 "0231 RSCN timeout Data: x%x "
4024 "x%x\n",
4025 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004026
4027 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004028 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004029
James Smart2e0fef82007-06-17 19:56:36 -05004030 lpfc_els_flush_rscn(vport);
4031 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004032 }
4033 break;
James Smart2e0fef82007-06-17 19:56:36 -05004034
James Smart92d7f7b2007-06-17 19:56:38 -05004035 default:
James Smarte8b62012007-08-02 11:10:09 -04004036 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004037 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004038 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004039 break;
4040 }
4041
4042 switch (phba->link_state) {
4043 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004044 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004045 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4046 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004047 clrlaerr = 1;
4048 break;
4049
James Smart09372822008-01-11 01:52:54 -05004050 case LPFC_LINK_UP:
4051 lpfc_issue_clear_la(phba, vport);
4052 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004053 case LPFC_LINK_UNKNOWN:
4054 case LPFC_WARM_START:
4055 case LPFC_INIT_START:
4056 case LPFC_INIT_MBX_CMDS:
4057 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004058 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004059 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4060 "0230 Unexpected timeout, hba link "
4061 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004062 clrlaerr = 1;
4063 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004064
4065 case LPFC_HBA_READY:
4066 break;
dea31012005-04-17 16:05:31 -05004067 }
4068
4069 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004070 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004071 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004072 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4073 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004074 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004075 }
4076
4077 return;
4078}
4079
dea31012005-04-17 16:05:31 -05004080/*
4081 * This routine handles processing a NameServer REG_LOGIN mailbox
4082 * command upon completion. It is setup in the LPFC_MBOXQ
4083 * as the completion routine when the command is
4084 * handed off to the SLI layer.
4085 */
4086void
James Smart2e0fef82007-06-17 19:56:36 -05004087lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004088{
James Smart04c68492009-05-22 14:52:52 -04004089 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004090 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4091 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4092 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004093
4094 pmb->context1 = NULL;
4095
dea31012005-04-17 16:05:31 -05004096 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004097 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004098 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004099 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004100
James Smart2e0fef82007-06-17 19:56:36 -05004101 /*
4102 * Start issuing Fabric-Device Management Interface (FDMI) command to
4103 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4104 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004105 */
James Smart2e0fef82007-06-17 19:56:36 -05004106
James Smart3de2a652007-08-02 11:09:59 -04004107 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004108 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4109 else
4110 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004111
James Smartfa4066b2008-01-11 01:53:27 -05004112 /* decrement the node reference count held for this callback
4113 * function.
4114 */
James Smart329f9bc2007-04-25 09:53:01 -04004115 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004116 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4117 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004118 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004119
4120 return;
4121}
4122
James Smart685f0bf2007-04-25 09:53:08 -04004123static int
4124lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4125{
4126 uint16_t *rpi = param;
4127
4128 return ndlp->nlp_rpi == *rpi;
4129}
4130
4131static int
4132lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4133{
4134 return memcmp(&ndlp->nlp_portname, param,
4135 sizeof(ndlp->nlp_portname)) == 0;
4136}
4137
Adrian Bunka6ababd2007-11-05 18:07:33 +01004138static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004139__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004140{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004141 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004142
James Smart2e0fef82007-06-17 19:56:36 -05004143 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004144 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004145 return ndlp;
4146 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004147 return NULL;
dea31012005-04-17 16:05:31 -05004148}
4149
James Smart685f0bf2007-04-25 09:53:08 -04004150/*
4151 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004152 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004153 */
4154struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004155__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004156{
James Smart2e0fef82007-06-17 19:56:36 -05004157 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004158}
4159
James Smart488d1462006-03-07 15:02:37 -05004160/*
James Smart685f0bf2007-04-25 09:53:08 -04004161 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004162 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004163 */
4164struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004165lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004166{
James Smart2e0fef82007-06-17 19:56:36 -05004167 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004168 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004169
James Smart2e0fef82007-06-17 19:56:36 -05004170 spin_lock_irq(shost->host_lock);
4171 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4172 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004173 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004174}
4175
dea31012005-04-17 16:05:31 -05004176void
James Smart2e0fef82007-06-17 19:56:36 -05004177lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4178 uint32_t did)
dea31012005-04-17 16:05:31 -05004179{
4180 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004181
4182 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004183 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004184
4185 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4186 "node init: did:x%x",
4187 ndlp->nlp_DID, 0, 0);
4188
dea31012005-04-17 16:05:31 -05004189 return;
4190}
James Smart329f9bc2007-04-25 09:53:01 -04004191
James Smart98c9ea52007-10-27 13:37:33 -04004192/* This routine releases all resources associated with a specifc NPort's ndlp
4193 * and mempool_free's the nodelist.
4194 */
James Smart311464e2007-08-02 11:10:37 -04004195static void
James Smart329f9bc2007-04-25 09:53:01 -04004196lpfc_nlp_release(struct kref *kref)
4197{
James Smarte47c9092008-02-08 18:49:26 -05004198 struct lpfc_hba *phba;
4199 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004200 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4201 kref);
James Smart858c9f62007-06-17 19:56:39 -05004202
4203 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4204 "node release: did:x%x flg:x%x type:x%x",
4205 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4206
James Smarte47c9092008-02-08 18:49:26 -05004207 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4208 "0279 lpfc_nlp_release: ndlp:x%p "
4209 "usgmap:x%x refcnt:%d\n",
4210 (void *)ndlp, ndlp->nlp_usg_map,
4211 atomic_read(&ndlp->kref.refcount));
4212
4213 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004214 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004215
4216 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004217 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004218 spin_lock_irqsave(&phba->ndlp_lock, flags);
4219 NLP_CLR_NODE_ACT(ndlp);
4220 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4221
4222 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004223 if (NLP_CHK_FREE_REQ(ndlp)) {
4224 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004225 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004226 }
James Smart329f9bc2007-04-25 09:53:01 -04004227}
4228
James Smart98c9ea52007-10-27 13:37:33 -04004229/* This routine bumps the reference count for a ndlp structure to ensure
4230 * that one discovery thread won't free a ndlp while another discovery thread
4231 * is using it.
4232 */
James Smart329f9bc2007-04-25 09:53:01 -04004233struct lpfc_nodelist *
4234lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4235{
James Smarte47c9092008-02-08 18:49:26 -05004236 struct lpfc_hba *phba;
4237 unsigned long flags;
4238
James Smart98c9ea52007-10-27 13:37:33 -04004239 if (ndlp) {
4240 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4241 "node get: did:x%x flg:x%x refcnt:x%x",
4242 ndlp->nlp_DID, ndlp->nlp_flag,
4243 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004244 /* The check of ndlp usage to prevent incrementing the
4245 * ndlp reference count that is in the process of being
4246 * released.
4247 */
James Smarta257bf92009-04-06 18:48:10 -04004248 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004249 spin_lock_irqsave(&phba->ndlp_lock, flags);
4250 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4251 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4252 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4253 "0276 lpfc_nlp_get: ndlp:x%p "
4254 "usgmap:x%x refcnt:%d\n",
4255 (void *)ndlp, ndlp->nlp_usg_map,
4256 atomic_read(&ndlp->kref.refcount));
4257 return NULL;
4258 } else
4259 kref_get(&ndlp->kref);
4260 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004261 }
James Smart329f9bc2007-04-25 09:53:01 -04004262 return ndlp;
4263}
4264
James Smart98c9ea52007-10-27 13:37:33 -04004265/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004266 * count goes to 0, this indicates the the associated nodelist should be
4267 * freed. Returning 1 indicates the ndlp resource has been released; on the
4268 * other hand, returning 0 indicates the ndlp resource has not been released
4269 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004270 */
James Smart329f9bc2007-04-25 09:53:01 -04004271int
4272lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4273{
James Smarte47c9092008-02-08 18:49:26 -05004274 struct lpfc_hba *phba;
4275 unsigned long flags;
4276
4277 if (!ndlp)
4278 return 1;
4279
4280 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4281 "node put: did:x%x flg:x%x refcnt:x%x",
4282 ndlp->nlp_DID, ndlp->nlp_flag,
4283 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004284 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004285 spin_lock_irqsave(&phba->ndlp_lock, flags);
4286 /* Check the ndlp memory free acknowledge flag to avoid the
4287 * possible race condition that kref_put got invoked again
4288 * after previous one has done ndlp memory free.
4289 */
4290 if (NLP_CHK_FREE_ACK(ndlp)) {
4291 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4292 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4293 "0274 lpfc_nlp_put: ndlp:x%p "
4294 "usgmap:x%x refcnt:%d\n",
4295 (void *)ndlp, ndlp->nlp_usg_map,
4296 atomic_read(&ndlp->kref.refcount));
4297 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004298 }
James Smarte47c9092008-02-08 18:49:26 -05004299 /* Check the ndlp inactivate log flag to avoid the possible
4300 * race condition that kref_put got invoked again after ndlp
4301 * is already in inactivating state.
4302 */
4303 if (NLP_CHK_IACT_REQ(ndlp)) {
4304 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4305 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4306 "0275 lpfc_nlp_put: ndlp:x%p "
4307 "usgmap:x%x refcnt:%d\n",
4308 (void *)ndlp, ndlp->nlp_usg_map,
4309 atomic_read(&ndlp->kref.refcount));
4310 return 1;
4311 }
4312 /* For last put, mark the ndlp usage flags to make sure no
4313 * other kref_get and kref_put on the same ndlp shall get
4314 * in between the process when the final kref_put has been
4315 * invoked on this ndlp.
4316 */
4317 if (atomic_read(&ndlp->kref.refcount) == 1) {
4318 /* Indicate ndlp is put to inactive state. */
4319 NLP_SET_IACT_REQ(ndlp);
4320 /* Acknowledge ndlp memory free has been seen. */
4321 if (NLP_CHK_FREE_REQ(ndlp))
4322 NLP_SET_FREE_ACK(ndlp);
4323 }
4324 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4325 /* Note, the kref_put returns 1 when decrementing a reference
4326 * count that was 1, it invokes the release callback function,
4327 * but it still left the reference count as 1 (not actually
4328 * performs the last decrementation). Otherwise, it actually
4329 * decrements the reference count and returns 0.
4330 */
4331 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004332}
James Smart98c9ea52007-10-27 13:37:33 -04004333
4334/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004335 * by any other discovery thread. This routine returns 1 if the
4336 * ndlp has been freed. A return value of 0 indicates the ndlp is
4337 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004338 */
4339int
4340lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4341{
4342 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4343 "node not used: did:x%x flg:x%x refcnt:x%x",
4344 ndlp->nlp_DID, ndlp->nlp_flag,
4345 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004346 if (atomic_read(&ndlp->kref.refcount) == 1)
4347 if (lpfc_nlp_put(ndlp))
4348 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004349 return 0;
4350}
James Smart6fb120a2009-05-22 14:52:59 -04004351
4352/**
4353 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4354 * @phba: Pointer to hba context object.
4355 *
4356 * This function iterate through all FC nodes associated
4357 * will all vports to check if there is any node with
4358 * fc_rports associated with it. If there is an fc_rport
4359 * associated with the node, then the node is either in
4360 * discovered state or its devloss_timer is pending.
4361 */
4362static int
4363lpfc_fcf_inuse(struct lpfc_hba *phba)
4364{
4365 struct lpfc_vport **vports;
4366 int i, ret = 0;
4367 struct lpfc_nodelist *ndlp;
4368 struct Scsi_Host *shost;
4369
4370 vports = lpfc_create_vport_work_array(phba);
4371
4372 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4373 shost = lpfc_shost_from_vport(vports[i]);
4374 spin_lock_irq(shost->host_lock);
4375 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4376 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4377 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4378 ret = 1;
4379 spin_unlock_irq(shost->host_lock);
4380 goto out;
James Smart891478a2009-11-18 15:40:23 -05004381 } else {
4382 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4383 "2624 RPI %x DID %x flg %x still "
4384 "logged in\n",
4385 ndlp->nlp_rpi, ndlp->nlp_DID,
4386 ndlp->nlp_flag);
4387 if (ndlp->nlp_flag & NLP_RPI_VALID)
4388 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04004389 }
4390 }
4391 spin_unlock_irq(shost->host_lock);
4392 }
4393out:
4394 lpfc_destroy_vport_work_array(phba, vports);
4395 return ret;
4396}
4397
4398/**
4399 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4400 * @phba: Pointer to hba context object.
4401 * @mboxq: Pointer to mailbox object.
4402 *
4403 * This function frees memory associated with the mailbox command.
4404 */
4405static void
4406lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4407{
4408 struct lpfc_vport *vport = mboxq->vport;
4409
4410 if (mboxq->u.mb.mbxStatus) {
4411 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4412 "2555 UNREG_VFI mbxStatus error x%x "
4413 "HBA state x%x\n",
4414 mboxq->u.mb.mbxStatus, vport->port_state);
4415 }
4416 mempool_free(mboxq, phba->mbox_mem_pool);
4417 return;
4418}
4419
4420/**
4421 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4422 * @phba: Pointer to hba context object.
4423 * @mboxq: Pointer to mailbox object.
4424 *
4425 * This function frees memory associated with the mailbox command.
4426 */
4427static void
4428lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4429{
4430 struct lpfc_vport *vport = mboxq->vport;
4431
4432 if (mboxq->u.mb.mbxStatus) {
4433 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4434 "2550 UNREG_FCFI mbxStatus error x%x "
4435 "HBA state x%x\n",
4436 mboxq->u.mb.mbxStatus, vport->port_state);
4437 }
4438 mempool_free(mboxq, phba->mbox_mem_pool);
4439 return;
4440}
4441
4442/**
4443 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
4444 * @phba: Pointer to hba context object.
4445 *
4446 * This function check if there are any connected remote port for the FCF and
4447 * if all the devices are disconnected, this function unregister FCFI.
4448 * This function also tries to use another FCF for discovery.
4449 */
4450void
4451lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
4452{
4453 LPFC_MBOXQ_t *mbox;
4454 int rc;
4455 struct lpfc_vport **vports;
4456 int i;
4457
4458 spin_lock_irq(&phba->hbalock);
4459 /*
4460 * If HBA is not running in FIP mode or
4461 * If HBA does not support FCoE or
4462 * If FCF is not registered.
4463 * do nothing.
4464 */
4465 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
4466 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smart45ed1192009-10-02 15:17:02 -04004467 (!(phba->hba_flag & HBA_FIP_SUPPORT))) {
James Smart6fb120a2009-05-22 14:52:59 -04004468 spin_unlock_irq(&phba->hbalock);
4469 return;
4470 }
4471 spin_unlock_irq(&phba->hbalock);
4472
4473 if (lpfc_fcf_inuse(phba))
4474 return;
4475
James Smart4d9ab992009-10-02 15:16:39 -04004476 /* At this point, all discovery is aborted */
4477 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04004478
4479 /* Unregister VPIs */
4480 vports = lpfc_create_vport_work_array(phba);
4481 if (vports &&
4482 (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
4483 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4484 lpfc_mbx_unreg_vpi(vports[i]);
James Smart891478a2009-11-18 15:40:23 -05004485 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05004486 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart6fb120a2009-05-22 14:52:59 -04004487 }
4488 lpfc_destroy_vport_work_array(phba, vports);
4489
4490 /* Unregister VFI */
4491 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4492 if (!mbox) {
4493 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4494 "2556 UNREG_VFI mbox allocation failed"
4495 "HBA state x%x\n",
4496 phba->pport->port_state);
4497 return;
4498 }
4499
James Smart6669f9b2009-10-02 15:16:45 -04004500 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04004501 mbox->vport = phba->pport;
4502 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
4503
4504 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4505 if (rc == MBX_NOT_FINISHED) {
4506 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4507 "2557 UNREG_VFI issue mbox failed rc x%x "
4508 "HBA state x%x\n",
4509 rc, phba->pport->port_state);
4510 mempool_free(mbox, phba->mbox_mem_pool);
4511 return;
4512 }
4513
4514 /* Unregister FCF */
4515 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4516 if (!mbox) {
4517 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4518 "2551 UNREG_FCFI mbox allocation failed"
4519 "HBA state x%x\n",
4520 phba->pport->port_state);
4521 return;
4522 }
4523
4524 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
4525 mbox->vport = phba->pport;
4526 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
4527 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4528
4529 if (rc == MBX_NOT_FINISHED) {
4530 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4531 "2552 UNREG_FCFI issue mbox failed rc x%x "
4532 "HBA state x%x\n",
4533 rc, phba->pport->port_state);
4534 mempool_free(mbox, phba->mbox_mem_pool);
4535 return;
4536 }
4537
4538 spin_lock_irq(&phba->hbalock);
4539 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_REGISTERED |
4540 FCF_DISCOVERED | FCF_BOOT_ENABLE | FCF_IN_USE |
4541 FCF_VALID_VLAN);
4542 spin_unlock_irq(&phba->hbalock);
4543
4544 /*
4545 * If driver is not unloading, check if there is any other
4546 * FCF record that can be used for discovery.
4547 */
4548 if ((phba->pport->load_flag & FC_UNLOADING) ||
4549 (phba->link_state < LPFC_LINK_UP))
4550 return;
4551
4552 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
4553
4554 if (rc)
4555 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4556 "2553 lpfc_unregister_unused_fcf failed to read FCF"
4557 " record HBA state x%x\n",
4558 phba->pport->port_state);
4559}
4560
4561/**
4562 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
4563 * @phba: Pointer to hba context object.
4564 * @buff: Buffer containing the FCF connection table as in the config
4565 * region.
4566 * This function create driver data structure for the FCF connection
4567 * record table read from config region 23.
4568 */
4569static void
4570lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
4571 uint8_t *buff)
4572{
4573 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4574 struct lpfc_fcf_conn_hdr *conn_hdr;
4575 struct lpfc_fcf_conn_rec *conn_rec;
4576 uint32_t record_count;
4577 int i;
4578
4579 /* Free the current connect table */
4580 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004581 &phba->fcf_conn_rec_list, list) {
4582 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04004583 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004584 }
James Smart6fb120a2009-05-22 14:52:59 -04004585
4586 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
4587 record_count = conn_hdr->length * sizeof(uint32_t)/
4588 sizeof(struct lpfc_fcf_conn_rec);
4589
4590 conn_rec = (struct lpfc_fcf_conn_rec *)
4591 (buff + sizeof(struct lpfc_fcf_conn_hdr));
4592
4593 for (i = 0; i < record_count; i++) {
4594 if (!(conn_rec[i].flags & FCFCNCT_VALID))
4595 continue;
4596 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
4597 GFP_KERNEL);
4598 if (!conn_entry) {
4599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4600 "2566 Failed to allocate connection"
4601 " table entry\n");
4602 return;
4603 }
4604
4605 memcpy(&conn_entry->conn_rec, &conn_rec[i],
4606 sizeof(struct lpfc_fcf_conn_rec));
4607 conn_entry->conn_rec.vlan_tag =
4608 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
4609 conn_entry->conn_rec.flags =
4610 le16_to_cpu(conn_entry->conn_rec.flags);
4611 list_add_tail(&conn_entry->list,
4612 &phba->fcf_conn_rec_list);
4613 }
4614}
4615
4616/**
4617 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
4618 * @phba: Pointer to hba context object.
4619 * @buff: Buffer containing the FCoE parameter data structure.
4620 *
4621 * This function update driver data structure with config
4622 * parameters read from config region 23.
4623 */
4624static void
4625lpfc_read_fcoe_param(struct lpfc_hba *phba,
4626 uint8_t *buff)
4627{
4628 struct lpfc_fip_param_hdr *fcoe_param_hdr;
4629 struct lpfc_fcoe_params *fcoe_param;
4630
4631 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
4632 buff;
4633 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04004634 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04004635
4636 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
4637 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
4638 return;
4639
James Smart6fb120a2009-05-22 14:52:59 -04004640 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
4641 phba->valid_vlan = 1;
4642 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
4643 0xFFF;
4644 }
4645
4646 phba->fc_map[0] = fcoe_param->fc_map[0];
4647 phba->fc_map[1] = fcoe_param->fc_map[1];
4648 phba->fc_map[2] = fcoe_param->fc_map[2];
4649 return;
4650}
4651
4652/**
4653 * lpfc_get_rec_conf23 - Get a record type in config region data.
4654 * @buff: Buffer containing config region 23 data.
4655 * @size: Size of the data buffer.
4656 * @rec_type: Record type to be searched.
4657 *
4658 * This function searches config region data to find the begining
4659 * of the record specified by record_type. If record found, this
4660 * function return pointer to the record else return NULL.
4661 */
4662static uint8_t *
4663lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
4664{
4665 uint32_t offset = 0, rec_length;
4666
4667 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
4668 (size < sizeof(uint32_t)))
4669 return NULL;
4670
4671 rec_length = buff[offset + 1];
4672
4673 /*
4674 * One TLV record has one word header and number of data words
4675 * specified in the rec_length field of the record header.
4676 */
4677 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
4678 <= size) {
4679 if (buff[offset] == rec_type)
4680 return &buff[offset];
4681
4682 if (buff[offset] == LPFC_REGION23_LAST_REC)
4683 return NULL;
4684
4685 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
4686 rec_length = buff[offset + 1];
4687 }
4688 return NULL;
4689}
4690
4691/**
4692 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
4693 * @phba: Pointer to lpfc_hba data structure.
4694 * @buff: Buffer containing config region 23 data.
4695 * @size: Size of the data buffer.
4696 *
4697 * This fuction parse the FCoE config parameters in config region 23 and
4698 * populate driver data structure with the parameters.
4699 */
4700void
4701lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
4702 uint8_t *buff,
4703 uint32_t size)
4704{
4705 uint32_t offset = 0, rec_length;
4706 uint8_t *rec_ptr;
4707
4708 /*
4709 * If data size is less than 2 words signature and version cannot be
4710 * verified.
4711 */
4712 if (size < 2*sizeof(uint32_t))
4713 return;
4714
4715 /* Check the region signature first */
4716 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
4717 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4718 "2567 Config region 23 has bad signature\n");
4719 return;
4720 }
4721
4722 offset += 4;
4723
4724 /* Check the data structure version */
4725 if (buff[offset] != LPFC_REGION23_VERSION) {
4726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4727 "2568 Config region 23 has bad version\n");
4728 return;
4729 }
4730 offset += 4;
4731
4732 rec_length = buff[offset + 1];
4733
4734 /* Read FCoE param record */
4735 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4736 size - offset, FCOE_PARAM_TYPE);
4737 if (rec_ptr)
4738 lpfc_read_fcoe_param(phba, rec_ptr);
4739
4740 /* Read FCF connection table */
4741 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4742 size - offset, FCOE_CONN_TBL_TYPE);
4743 if (rec_ptr)
4744 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
4745
4746}