blob: 3c06aa54a3e5104e5760f699ef9f69858cd6fb46 [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 Smart4d9ab992009-10-02 15:16:39 -04001021 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1022 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001023 if (vport->port_state != LPFC_FLOGI) {
1024 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001025 spin_unlock_irqrestore(&phba->hbalock, flags);
1026 lpfc_initial_flogi(vport);
1027 }
1028
1029 mempool_free(mboxq, phba->mbox_mem_pool);
1030 return;
1031}
1032
1033/**
1034 * lpfc_fab_name_match - Check if the fcf fabric name match.
1035 * @fab_name: pointer to fabric name.
1036 * @new_fcf_record: pointer to fcf record.
1037 *
1038 * This routine compare the fcf record's fabric name with provided
1039 * fabric name. If the fabric name are identical this function
1040 * returns 1 else return 0.
1041 **/
1042static uint32_t
1043lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1044{
1045 if ((fab_name[0] ==
1046 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record)) &&
1047 (fab_name[1] ==
1048 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record)) &&
1049 (fab_name[2] ==
1050 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record)) &&
1051 (fab_name[3] ==
1052 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record)) &&
1053 (fab_name[4] ==
1054 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record)) &&
1055 (fab_name[5] ==
1056 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record)) &&
1057 (fab_name[6] ==
1058 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record)) &&
1059 (fab_name[7] ==
1060 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record)))
1061 return 1;
1062 else
1063 return 0;
1064}
1065
1066/**
James Smart8fa38512009-07-19 10:01:03 -04001067 * lpfc_sw_name_match - Check if the fcf switch name match.
1068 * @fab_name: pointer to fabric name.
1069 * @new_fcf_record: pointer to fcf record.
1070 *
1071 * This routine compare the fcf record's switch name with provided
1072 * switch name. If the switch name are identical this function
1073 * returns 1 else return 0.
1074 **/
1075static uint32_t
1076lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1077{
1078 if ((sw_name[0] ==
1079 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record)) &&
1080 (sw_name[1] ==
1081 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record)) &&
1082 (sw_name[2] ==
1083 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record)) &&
1084 (sw_name[3] ==
1085 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record)) &&
1086 (sw_name[4] ==
1087 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record)) &&
1088 (sw_name[5] ==
1089 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record)) &&
1090 (sw_name[6] ==
1091 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record)) &&
1092 (sw_name[7] ==
1093 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record)))
1094 return 1;
1095 else
1096 return 0;
1097}
1098
1099/**
James Smart6fb120a2009-05-22 14:52:59 -04001100 * lpfc_mac_addr_match - Check if the fcf mac address match.
1101 * @phba: pointer to lpfc hba data structure.
1102 * @new_fcf_record: pointer to fcf record.
1103 *
1104 * This routine compare the fcf record's mac address with HBA's
1105 * FCF mac address. If the mac addresses are identical this function
1106 * returns 1 else return 0.
1107 **/
1108static uint32_t
1109lpfc_mac_addr_match(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1110{
1111 if ((phba->fcf.mac_addr[0] ==
1112 bf_get(lpfc_fcf_record_mac_0, new_fcf_record)) &&
1113 (phba->fcf.mac_addr[1] ==
1114 bf_get(lpfc_fcf_record_mac_1, new_fcf_record)) &&
1115 (phba->fcf.mac_addr[2] ==
1116 bf_get(lpfc_fcf_record_mac_2, new_fcf_record)) &&
1117 (phba->fcf.mac_addr[3] ==
1118 bf_get(lpfc_fcf_record_mac_3, new_fcf_record)) &&
1119 (phba->fcf.mac_addr[4] ==
1120 bf_get(lpfc_fcf_record_mac_4, new_fcf_record)) &&
1121 (phba->fcf.mac_addr[5] ==
1122 bf_get(lpfc_fcf_record_mac_5, new_fcf_record)))
1123 return 1;
1124 else
1125 return 0;
1126}
1127
1128/**
1129 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1130 * @phba: pointer to lpfc hba data structure.
1131 * @new_fcf_record: pointer to fcf record.
1132 *
1133 * This routine copies the FCF information from the FCF
1134 * record to lpfc_hba data structure.
1135 **/
1136static void
1137lpfc_copy_fcf_record(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1138{
1139 phba->fcf.fabric_name[0] =
1140 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1141 phba->fcf.fabric_name[1] =
1142 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1143 phba->fcf.fabric_name[2] =
1144 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1145 phba->fcf.fabric_name[3] =
1146 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1147 phba->fcf.fabric_name[4] =
1148 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1149 phba->fcf.fabric_name[5] =
1150 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1151 phba->fcf.fabric_name[6] =
1152 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1153 phba->fcf.fabric_name[7] =
1154 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1155 phba->fcf.mac_addr[0] =
1156 bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1157 phba->fcf.mac_addr[1] =
1158 bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1159 phba->fcf.mac_addr[2] =
1160 bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1161 phba->fcf.mac_addr[3] =
1162 bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1163 phba->fcf.mac_addr[4] =
1164 bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1165 phba->fcf.mac_addr[5] =
1166 bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1167 phba->fcf.fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1168 phba->fcf.priority = new_fcf_record->fip_priority;
James Smart8fa38512009-07-19 10:01:03 -04001169 phba->fcf.switch_name[0] =
1170 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1171 phba->fcf.switch_name[1] =
1172 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1173 phba->fcf.switch_name[2] =
1174 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1175 phba->fcf.switch_name[3] =
1176 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1177 phba->fcf.switch_name[4] =
1178 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1179 phba->fcf.switch_name[5] =
1180 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1181 phba->fcf.switch_name[6] =
1182 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1183 phba->fcf.switch_name[7] =
1184 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001185}
1186
1187/**
1188 * lpfc_register_fcf - Register the FCF with hba.
1189 * @phba: pointer to lpfc hba data structure.
1190 *
1191 * This routine issues a register fcfi mailbox command to register
1192 * the fcf with HBA.
1193 **/
1194static void
1195lpfc_register_fcf(struct lpfc_hba *phba)
1196{
1197 LPFC_MBOXQ_t *fcf_mbxq;
1198 int rc;
1199 unsigned long flags;
1200
1201 spin_lock_irqsave(&phba->hbalock, flags);
1202
1203 /* If the FCF is not availabe do nothing. */
1204 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smart4d9ab992009-10-02 15:16:39 -04001205 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001206 spin_unlock_irqrestore(&phba->hbalock, flags);
1207 return;
1208 }
1209
1210 /* The FCF is already registered, start discovery */
1211 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1212 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
James Smart32b97932009-07-19 10:01:21 -04001213 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001214 spin_unlock_irqrestore(&phba->hbalock, flags);
1215 if (phba->pport->port_state != LPFC_FLOGI)
1216 lpfc_initial_flogi(phba->pport);
1217 return;
1218 }
1219 spin_unlock_irqrestore(&phba->hbalock, flags);
1220
1221 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1222 GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001223 if (!fcf_mbxq) {
1224 spin_lock_irqsave(&phba->hbalock, flags);
1225 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1226 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001227 return;
James Smart4d9ab992009-10-02 15:16:39 -04001228 }
James Smart6fb120a2009-05-22 14:52:59 -04001229
1230 lpfc_reg_fcfi(phba, fcf_mbxq);
1231 fcf_mbxq->vport = phba->pport;
1232 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1233 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001234 if (rc == MBX_NOT_FINISHED) {
1235 spin_lock_irqsave(&phba->hbalock, flags);
1236 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1237 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001238 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001239 }
James Smart6fb120a2009-05-22 14:52:59 -04001240
1241 return;
1242}
1243
1244/**
1245 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1246 * @phba: pointer to lpfc hba data structure.
1247 * @new_fcf_record: pointer to fcf record.
1248 * @boot_flag: Indicates if this record used by boot bios.
1249 * @addr_mode: The address mode to be used by this FCF
1250 *
1251 * This routine compare the fcf record with connect list obtained from the
1252 * config region to decide if this FCF can be used for SAN discovery. It returns
1253 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1254 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1255 * is used by boot bios and addr_mode will indicate the addressing mode to be
1256 * used for this FCF when the function returns.
1257 * If the FCF record need to be used with a particular vlan id, the vlan is
1258 * set in the vlan_id on return of the function. If not VLAN tagging need to
1259 * be used with the FCF vlan_id will be set to 0xFFFF;
1260 **/
1261static int
1262lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1263 struct fcf_record *new_fcf_record,
1264 uint32_t *boot_flag, uint32_t *addr_mode,
1265 uint16_t *vlan_id)
1266{
1267 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001268 int i, j, fcf_vlan_id = 0;
1269
1270 /* Find the lowest VLAN id in the FCF record */
1271 for (i = 0; i < 512; i++) {
1272 if (new_fcf_record->vlan_bitmap[i]) {
1273 fcf_vlan_id = i * 8;
1274 j = 0;
1275 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1276 j++;
1277 fcf_vlan_id++;
1278 }
1279 break;
1280 }
1281 }
James Smart6fb120a2009-05-22 14:52:59 -04001282
James Smart0c287582009-06-10 17:22:56 -04001283 /* If FCF not available return 0 */
1284 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1285 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1286 return 0;
1287
James Smart45ed1192009-10-02 15:17:02 -04001288 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001289 *boot_flag = 0;
1290 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1291 new_fcf_record);
1292 if (phba->valid_vlan)
1293 *vlan_id = phba->vlan_id;
1294 else
1295 *vlan_id = 0xFFFF;
1296 return 1;
1297 }
1298
1299 /*
1300 * If there are no FCF connection table entry, driver connect to all
1301 * FCFs.
1302 */
1303 if (list_empty(&phba->fcf_conn_rec_list)) {
1304 *boot_flag = 0;
1305 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1306 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001307
1308 /*
1309 * When there are no FCF connect entries, use driver's default
1310 * addressing mode - FPMA.
1311 */
1312 if (*addr_mode & LPFC_FCF_FPMA)
1313 *addr_mode = LPFC_FCF_FPMA;
1314
James Smart4d9ab992009-10-02 15:16:39 -04001315 /* If FCF record report a vlan id use that vlan id */
1316 if (fcf_vlan_id)
1317 *vlan_id = fcf_vlan_id;
1318 else
1319 *vlan_id = 0xFFFF;
James Smart6fb120a2009-05-22 14:52:59 -04001320 return 1;
1321 }
1322
1323 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list, list) {
1324 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1325 continue;
1326
1327 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1328 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001329 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001330 continue;
James Smart8fa38512009-07-19 10:01:03 -04001331 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1332 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1333 new_fcf_record))
1334 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001335 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1336 /*
1337 * If the vlan bit map does not have the bit set for the
1338 * vlan id to be used, then it is not a match.
1339 */
1340 if (!(new_fcf_record->vlan_bitmap
1341 [conn_entry->conn_rec.vlan_tag / 8] &
1342 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1343 continue;
1344 }
1345
1346 /*
James Smart0c287582009-06-10 17:22:56 -04001347 * If connection record does not support any addressing mode,
1348 * skip the FCF record.
1349 */
1350 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1351 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1352 continue;
1353
1354 /*
James Smart6fb120a2009-05-22 14:52:59 -04001355 * Check if the connection record specifies a required
1356 * addressing mode.
1357 */
1358 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1359 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1360
1361 /*
1362 * If SPMA required but FCF not support this continue.
1363 */
1364 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1365 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1366 new_fcf_record) & LPFC_FCF_SPMA))
1367 continue;
1368
1369 /*
1370 * If FPMA required but FCF not support this continue.
1371 */
1372 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1373 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1374 new_fcf_record) & LPFC_FCF_FPMA))
1375 continue;
1376 }
1377
1378 /*
1379 * This fcf record matches filtering criteria.
1380 */
1381 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1382 *boot_flag = 1;
1383 else
1384 *boot_flag = 0;
1385
James Smart0c287582009-06-10 17:22:56 -04001386 /*
1387 * If user did not specify any addressing mode, or if the
1388 * prefered addressing mode specified by user is not supported
1389 * by FCF, allow fabric to pick the addressing mode.
1390 */
James Smart6fb120a2009-05-22 14:52:59 -04001391 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1392 new_fcf_record);
1393 /*
1394 * If the user specified a required address mode, assign that
1395 * address mode
1396 */
1397 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1398 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1399 *addr_mode = (conn_entry->conn_rec.flags &
1400 FCFCNCT_AM_SPMA) ?
1401 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1402 /*
1403 * If the user specified a prefered address mode, use the
1404 * addr mode only if FCF support the addr_mode.
1405 */
1406 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1407 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1408 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1409 (*addr_mode & LPFC_FCF_SPMA))
1410 *addr_mode = LPFC_FCF_SPMA;
1411 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1412 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1413 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1414 (*addr_mode & LPFC_FCF_FPMA))
1415 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001416
James Smart4d9ab992009-10-02 15:16:39 -04001417 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001418 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1419 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001420 /*
1421 * If no vlan id is specified in connect list, use the vlan id
1422 * in the FCF record
1423 */
1424 else if (fcf_vlan_id)
1425 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001426 else
1427 *vlan_id = 0xFFFF;
1428
1429 return 1;
1430 }
1431
1432 return 0;
1433}
1434
1435/**
James Smart32b97932009-07-19 10:01:21 -04001436 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1437 * @phba: pointer to lpfc hba data structure.
1438 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1439 *
1440 * This function check if there is any fcoe event pending while driver
1441 * scan FCF entries. If there is any pending event, it will restart the
1442 * FCF saning and return 1 else return 0.
1443 */
1444int
1445lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1446{
1447 LPFC_MBOXQ_t *mbox;
1448 int rc;
1449 /*
1450 * If the Link is up and no FCoE events while in the
1451 * FCF discovery, no need to restart FCF discovery.
1452 */
1453 if ((phba->link_state >= LPFC_LINK_UP) &&
1454 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1455 return 0;
1456
1457 spin_lock_irq(&phba->hbalock);
1458 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1459 spin_unlock_irq(&phba->hbalock);
1460
1461 if (phba->link_state >= LPFC_LINK_UP)
1462 lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart4d9ab992009-10-02 15:16:39 -04001463 else
1464 /*
1465 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1466 * flag
1467 */
1468 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart32b97932009-07-19 10:01:21 -04001469
1470 if (unreg_fcf) {
1471 spin_lock_irq(&phba->hbalock);
1472 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1473 spin_unlock_irq(&phba->hbalock);
1474 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1475 if (!mbox) {
1476 lpfc_printf_log(phba, KERN_ERR,
1477 LOG_DISCOVERY|LOG_MBOX,
1478 "2610 UNREG_FCFI mbox allocation failed\n");
1479 return 1;
1480 }
1481 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
1482 mbox->vport = phba->pport;
1483 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
1484 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1485 if (rc == MBX_NOT_FINISHED) {
1486 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
1487 "2611 UNREG_FCFI issue mbox failed\n");
1488 mempool_free(mbox, phba->mbox_mem_pool);
1489 }
1490 }
1491
1492 return 1;
1493}
1494
1495/**
James Smart6fb120a2009-05-22 14:52:59 -04001496 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
1497 * @phba: pointer to lpfc hba data structure.
1498 * @mboxq: pointer to mailbox object.
1499 *
1500 * This function iterate through all the fcf records available in
1501 * HBA and choose the optimal FCF record for discovery. After finding
1502 * the FCF for discovery it register the FCF record and kick start
1503 * discovery.
1504 * If FCF_IN_USE flag is set in currently used FCF, the routine try to
1505 * use a FCF record which match fabric name and mac address of the
1506 * currently used FCF record.
1507 * If the driver support only one FCF, it will try to use the FCF record
1508 * used by BOOT_BIOS.
1509 */
1510void
1511lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1512{
1513 void *virt_addr;
1514 dma_addr_t phys_addr;
1515 uint8_t *bytep;
1516 struct lpfc_mbx_sge sge;
1517 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1518 uint32_t shdr_status, shdr_add_status;
1519 union lpfc_sli4_cfg_shdr *shdr;
1520 struct fcf_record *new_fcf_record;
1521 int rc;
1522 uint32_t boot_flag, addr_mode;
1523 uint32_t next_fcf_index;
1524 unsigned long flags;
1525 uint16_t vlan_id;
1526
James Smart32b97932009-07-19 10:01:21 -04001527 /* If there is pending FCoE event restart FCF table scan */
1528 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1529 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1530 return;
1531 }
1532
James Smart6fb120a2009-05-22 14:52:59 -04001533 /* Get the first SGE entry from the non-embedded DMA memory. This
1534 * routine only uses a single SGE.
1535 */
1536 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1537 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1538 if (unlikely(!mboxq->sge_array)) {
1539 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1540 "2524 Failed to get the non-embedded SGE "
1541 "virtual address\n");
1542 goto out;
1543 }
1544 virt_addr = mboxq->sge_array->addr[0];
1545
1546 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1547 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1548 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
1549 &shdr->response);
1550 /*
1551 * The FCF Record was read and there is no reason for the driver
1552 * to maintain the FCF record data or memory. Instead, just need
1553 * to book keeping the FCFIs can be used.
1554 */
1555 if (shdr_status || shdr_add_status) {
1556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1557 "2521 READ_FCF_RECORD mailbox failed "
1558 "with status x%x add_status x%x, mbx\n",
1559 shdr_status, shdr_add_status);
1560 goto out;
1561 }
1562 /* Interpreting the returned information of FCF records */
1563 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1564 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1565 sizeof(struct lpfc_mbx_read_fcf_tbl));
1566 next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1567
1568 new_fcf_record = (struct fcf_record *)(virt_addr +
1569 sizeof(struct lpfc_mbx_read_fcf_tbl));
1570 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1571 sizeof(struct fcf_record));
1572 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1573
1574 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record,
1575 &boot_flag, &addr_mode,
1576 &vlan_id);
1577 /*
1578 * If the fcf record does not match with connect list entries
1579 * read the next entry.
1580 */
1581 if (!rc)
1582 goto read_next_fcf;
1583 /*
1584 * If this is not the first FCF discovery of the HBA, use last
1585 * FCF record for the discovery.
1586 */
1587 spin_lock_irqsave(&phba->hbalock, flags);
1588 if (phba->fcf.fcf_flag & FCF_IN_USE) {
1589 if (lpfc_fab_name_match(phba->fcf.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001590 new_fcf_record) &&
1591 lpfc_sw_name_match(phba->fcf.switch_name,
1592 new_fcf_record) &&
James Smart6fb120a2009-05-22 14:52:59 -04001593 lpfc_mac_addr_match(phba, new_fcf_record)) {
1594 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1595 spin_unlock_irqrestore(&phba->hbalock, flags);
1596 goto out;
1597 }
1598 spin_unlock_irqrestore(&phba->hbalock, flags);
1599 goto read_next_fcf;
1600 }
1601 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1602 /*
1603 * If the current FCF record does not have boot flag
1604 * set and new fcf record has boot flag set, use the
1605 * new fcf record.
1606 */
1607 if (boot_flag && !(phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1608 /* Use this FCF record */
1609 lpfc_copy_fcf_record(phba, new_fcf_record);
1610 phba->fcf.addr_mode = addr_mode;
1611 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1612 if (vlan_id != 0xFFFF) {
1613 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1614 phba->fcf.vlan_id = vlan_id;
1615 }
1616 spin_unlock_irqrestore(&phba->hbalock, flags);
1617 goto read_next_fcf;
1618 }
1619 /*
1620 * If the current FCF record has boot flag set and the
1621 * new FCF record does not have boot flag, read the next
1622 * FCF record.
1623 */
1624 if (!boot_flag && (phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1625 spin_unlock_irqrestore(&phba->hbalock, flags);
1626 goto read_next_fcf;
1627 }
1628 /*
1629 * If there is a record with lower priority value for
1630 * the current FCF, use that record.
1631 */
James Smart8fa38512009-07-19 10:01:03 -04001632 if (lpfc_fab_name_match(phba->fcf.fabric_name,
1633 new_fcf_record) &&
1634 (new_fcf_record->fip_priority < phba->fcf.priority)) {
James Smart6fb120a2009-05-22 14:52:59 -04001635 /* Use this FCF record */
1636 lpfc_copy_fcf_record(phba, new_fcf_record);
1637 phba->fcf.addr_mode = addr_mode;
1638 if (vlan_id != 0xFFFF) {
1639 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1640 phba->fcf.vlan_id = vlan_id;
1641 }
1642 spin_unlock_irqrestore(&phba->hbalock, flags);
1643 goto read_next_fcf;
1644 }
1645 spin_unlock_irqrestore(&phba->hbalock, flags);
1646 goto read_next_fcf;
1647 }
1648 /*
1649 * This is the first available FCF record, use this
1650 * record.
1651 */
1652 lpfc_copy_fcf_record(phba, new_fcf_record);
1653 phba->fcf.addr_mode = addr_mode;
1654 if (boot_flag)
1655 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1656 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1657 if (vlan_id != 0xFFFF) {
1658 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1659 phba->fcf.vlan_id = vlan_id;
1660 }
1661 spin_unlock_irqrestore(&phba->hbalock, flags);
1662 goto read_next_fcf;
1663
1664read_next_fcf:
1665 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1666 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0)
1667 lpfc_register_fcf(phba);
1668 else
1669 lpfc_sli4_read_fcf_record(phba, next_fcf_index);
1670 return;
1671
1672out:
1673 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1674 lpfc_register_fcf(phba);
1675
1676 return;
1677}
1678
1679/**
James Smart1c6834a2009-07-19 10:01:26 -04001680 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
1681 * @phba: pointer to lpfc hba data structure.
1682 * @mboxq: pointer to mailbox data structure.
1683 *
1684 * This function handles completion of init vpi mailbox command.
1685 */
1686static void
1687lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1688{
1689 struct lpfc_vport *vport = mboxq->vport;
1690 if (mboxq->u.mb.mbxStatus) {
1691 lpfc_printf_vlog(vport, KERN_ERR,
1692 LOG_MBOX,
1693 "2609 Init VPI mailbox failed 0x%x\n",
1694 mboxq->u.mb.mbxStatus);
1695 mempool_free(mboxq, phba->mbox_mem_pool);
1696 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1697 return;
1698 }
1699 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
1700
1701 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1702 lpfc_initial_fdisc(vport);
1703 else {
1704 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04001705 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1706 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04001707 }
1708 return;
1709}
1710
1711/**
James Smart6fb120a2009-05-22 14:52:59 -04001712 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
1713 * @phba: pointer to lpfc hba data structure.
1714 *
1715 * This function loops through the list of vports on the @phba and issues an
1716 * FDISC if possible.
1717 */
1718void
1719lpfc_start_fdiscs(struct lpfc_hba *phba)
1720{
1721 struct lpfc_vport **vports;
1722 int i;
James Smart1c6834a2009-07-19 10:01:26 -04001723 LPFC_MBOXQ_t *mboxq;
1724 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001725
1726 vports = lpfc_create_vport_work_array(phba);
1727 if (vports != NULL) {
1728 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1729 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1730 continue;
1731 /* There are no vpi for this vport */
1732 if (vports[i]->vpi > phba->max_vpi) {
1733 lpfc_vport_set_state(vports[i],
1734 FC_VPORT_FAILED);
1735 continue;
1736 }
1737 if (phba->fc_topology == TOPOLOGY_LOOP) {
1738 lpfc_vport_set_state(vports[i],
1739 FC_VPORT_LINKDOWN);
1740 continue;
1741 }
James Smart1c6834a2009-07-19 10:01:26 -04001742 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
1743 mboxq = mempool_alloc(phba->mbox_mem_pool,
1744 GFP_KERNEL);
1745 if (!mboxq) {
1746 lpfc_printf_vlog(vports[i], KERN_ERR,
1747 LOG_MBOX, "2607 Failed to allocate "
1748 "init_vpi mailbox\n");
1749 continue;
1750 }
1751 lpfc_init_vpi(phba, mboxq, vports[i]->vpi);
1752 mboxq->vport = vports[i];
1753 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
1754 rc = lpfc_sli_issue_mbox(phba, mboxq,
1755 MBX_NOWAIT);
1756 if (rc == MBX_NOT_FINISHED) {
1757 lpfc_printf_vlog(vports[i], KERN_ERR,
1758 LOG_MBOX, "2608 Failed to issue "
1759 "init_vpi mailbox\n");
1760 mempool_free(mboxq,
1761 phba->mbox_mem_pool);
1762 }
1763 continue;
1764 }
James Smart6fb120a2009-05-22 14:52:59 -04001765 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1766 lpfc_initial_fdisc(vports[i]);
1767 else {
1768 lpfc_vport_set_state(vports[i],
1769 FC_VPORT_NO_FABRIC_SUPP);
1770 lpfc_printf_vlog(vports[i], KERN_ERR,
1771 LOG_ELS,
1772 "0259 No NPIV "
1773 "Fabric support\n");
1774 }
1775 }
1776 }
1777 lpfc_destroy_vport_work_array(phba, vports);
1778}
1779
1780void
1781lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1782{
1783 struct lpfc_dmabuf *dmabuf = mboxq->context1;
1784 struct lpfc_vport *vport = mboxq->vport;
1785
1786 if (mboxq->u.mb.mbxStatus) {
1787 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1788 "2018 REG_VFI mbxStatus error x%x "
1789 "HBA state x%x\n",
1790 mboxq->u.mb.mbxStatus, vport->port_state);
1791 if (phba->fc_topology == TOPOLOGY_LOOP) {
1792 /* FLOGI failed, use loop map to make discovery list */
1793 lpfc_disc_list_loopmap(vport);
1794 /* Start discovery */
1795 lpfc_disc_start(vport);
1796 goto fail_free_mem;
1797 }
1798 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1799 goto fail_free_mem;
1800 }
James Smartc8685952009-11-18 15:39:16 -05001801 /* The VPI is implicitly registered when the VFI is registered */
1802 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart6fb120a2009-05-22 14:52:59 -04001803
1804 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1805 lpfc_start_fdiscs(phba);
1806 lpfc_do_scr_ns_plogi(phba, vport);
1807 }
1808
1809fail_free_mem:
1810 mempool_free(mboxq, phba->mbox_mem_pool);
1811 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1812 kfree(dmabuf);
1813 return;
1814}
1815
1816static void
James Smart2e0fef82007-06-17 19:56:36 -05001817lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001818{
James Smart6fb120a2009-05-22 14:52:59 -04001819 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001820 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05001821 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001822
1823
1824 /* Check for error */
1825 if (mb->mbxStatus) {
1826 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001827 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1828 "0319 READ_SPARAM mbxStatus error x%x "
1829 "hba state x%x>\n",
1830 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001831 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05001832 goto out;
1833 }
1834
James Smart2e0fef82007-06-17 19:56:36 -05001835 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05001836 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05001837 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05001838 u64_to_wwn(phba->cfg_soft_wwnn,
1839 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04001840 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05001841 u64_to_wwn(phba->cfg_soft_wwpn,
1842 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05001843 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1844 sizeof(vport->fc_nodename));
1845 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1846 sizeof(vport->fc_portname));
1847 if (vport->port_type == LPFC_PHYSICAL_PORT) {
1848 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1849 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1850 }
1851
dea31012005-04-17 16:05:31 -05001852 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1853 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001854 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001855 return;
1856
1857out:
1858 pmb->context1 = NULL;
1859 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1860 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05001861 lpfc_issue_clear_la(phba, vport);
1862 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001863 return;
1864}
1865
1866static void
James Smart92d7f7b2007-06-17 19:56:38 -05001867lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05001868{
James Smart92d7f7b2007-06-17 19:56:38 -05001869 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04001870 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05001871 int i;
James Smart14691152006-12-02 13:34:28 -05001872 struct lpfc_dmabuf *mp;
1873 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001874 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05001875
dea31012005-04-17 16:05:31 -05001876 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001877
James Smart92d7f7b2007-06-17 19:56:38 -05001878 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001879 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05001880 case LA_1GHZ_LINK:
1881 phba->fc_linkspeed = LA_1GHZ_LINK;
1882 break;
1883 case LA_2GHZ_LINK:
1884 phba->fc_linkspeed = LA_2GHZ_LINK;
1885 break;
1886 case LA_4GHZ_LINK:
1887 phba->fc_linkspeed = LA_4GHZ_LINK;
1888 break;
1889 case LA_8GHZ_LINK:
1890 phba->fc_linkspeed = LA_8GHZ_LINK;
1891 break;
James Smartf4b4c682009-05-22 14:53:12 -04001892 case LA_10GHZ_LINK:
1893 phba->fc_linkspeed = LA_10GHZ_LINK;
1894 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001895 default:
1896 phba->fc_linkspeed = LA_UNKNW_LINK;
1897 break;
dea31012005-04-17 16:05:31 -05001898 }
1899
1900 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05001901 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05001902
1903 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05001904 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05001905
James Smart6a9c52c2009-10-02 15:16:51 -04001906 /* if npiv is enabled and this adapter supports npiv log
1907 * a message that npiv is not supported in this topology
1908 */
1909 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04001910 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1911 "1309 Link Up Event npiv not supported in loop "
1912 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001913 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05001914 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05001915 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001916
James Smart2e0fef82007-06-17 19:56:36 -05001917 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05001918 i = la->un.lilpBde64.tus.f.bdeSize;
1919
1920 if (i == 0) {
1921 phba->alpa_map[0] = 0;
1922 } else {
James Smarte8b62012007-08-02 11:10:09 -04001923 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05001924 int numalpa, j, k;
1925 union {
1926 uint8_t pamap[16];
1927 struct {
1928 uint32_t wd1;
1929 uint32_t wd2;
1930 uint32_t wd3;
1931 uint32_t wd4;
1932 } pa;
1933 } un;
1934 numalpa = phba->alpa_map[0];
1935 j = 0;
1936 while (j < numalpa) {
1937 memset(un.pamap, 0, 16);
1938 for (k = 1; j < numalpa; k++) {
1939 un.pamap[k - 1] =
1940 phba->alpa_map[j + 1];
1941 j++;
1942 if (k == 16)
1943 break;
1944 }
1945 /* Link Up Event ALPA map */
1946 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05001947 KERN_WARNING,
1948 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001949 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05001950 "ALPA map Data: x%x "
1951 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001952 un.pa.wd1, un.pa.wd2,
1953 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05001954 }
1955 }
1956 }
1957 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05001958 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04001959 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05001960 (phba->sli_rev == 3))
1961 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1962 }
James Smart2e0fef82007-06-17 19:56:36 -05001963 vport->fc_myDID = phba->fc_pref_DID;
1964 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001965 }
James Smart92d7f7b2007-06-17 19:56:38 -05001966 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001967
1968 lpfc_linkup(phba);
1969 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001970 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05001971 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05001972 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04001973 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05001974 if (rc == MBX_NOT_FINISHED) {
1975 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1976 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1977 kfree(mp);
1978 mempool_free(sparam_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001979 goto out;
James Smart14691152006-12-02 13:34:28 -05001980 }
dea31012005-04-17 16:05:31 -05001981 }
1982
James Smart6fb120a2009-05-22 14:52:59 -04001983 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
1984 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1985 if (!cfglink_mbox)
1986 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05001987 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05001988 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05001989 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001990 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04001991 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04001992 if (rc == MBX_NOT_FINISHED) {
1993 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
1994 goto out;
1995 }
1996 } else {
James Smart32b97932009-07-19 10:01:21 -04001997 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04001998 /*
1999 * Add the driver's default FCF record at FCF index 0 now. This
2000 * is phase 1 implementation that support FCF index 0 and driver
2001 * defaults.
2002 */
James Smart45ed1192009-10-02 15:17:02 -04002003 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002004 fcf_record = kzalloc(sizeof(struct fcf_record),
2005 GFP_KERNEL);
2006 if (unlikely(!fcf_record)) {
2007 lpfc_printf_log(phba, KERN_ERR,
2008 LOG_MBOX | LOG_SLI,
2009 "2554 Could not allocate memmory for "
2010 "fcf record\n");
2011 rc = -ENODEV;
2012 goto out;
2013 }
2014
2015 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2016 LPFC_FCOE_FCF_DEF_INDEX);
2017 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2018 if (unlikely(rc)) {
2019 lpfc_printf_log(phba, KERN_ERR,
2020 LOG_MBOX | LOG_SLI,
2021 "2013 Could not manually add FCF "
2022 "record 0, status %d\n", rc);
2023 rc = -ENODEV;
2024 kfree(fcf_record);
2025 goto out;
2026 }
2027 kfree(fcf_record);
2028 }
2029 /*
2030 * The driver is expected to do FIP/FCF. Call the port
2031 * and get the FCF Table.
2032 */
James Smart32b97932009-07-19 10:01:21 -04002033 spin_lock_irq(&phba->hbalock);
2034 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2035 spin_unlock_irq(&phba->hbalock);
2036 return;
2037 }
2038 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002039 rc = lpfc_sli4_read_fcf_record(phba,
2040 LPFC_FCOE_FCF_GET_FIRST);
2041 if (rc)
2042 goto out;
dea31012005-04-17 16:05:31 -05002043 }
James Smart6fb120a2009-05-22 14:52:59 -04002044
2045 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002046out:
2047 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002048 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2049 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2050 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002051 lpfc_issue_clear_la(phba, vport);
2052 return;
dea31012005-04-17 16:05:31 -05002053}
2054
2055static void
James Smart84774a42008-08-24 21:50:06 -04002056lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002057{
dea31012005-04-17 16:05:31 -05002058 uint32_t control;
2059 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002060 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002061 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002062 if (phba->sli_rev <= LPFC_SLI_REV3) {
2063 control = readl(phba->HCregaddr);
2064 control |= HC_LAINT_ENA;
2065 writel(control, phba->HCregaddr);
2066 readl(phba->HCregaddr); /* flush */
2067 }
James Smart2e0fef82007-06-17 19:56:36 -05002068 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002069}
2070
James Smart84774a42008-08-24 21:50:06 -04002071static void
2072lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2073{
2074 lpfc_linkdown(phba);
2075 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002076 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002077 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2078}
2079
2080
dea31012005-04-17 16:05:31 -05002081/*
2082 * This routine handles processing a READ_LA mailbox
2083 * command upon completion. It is setup in the LPFC_MBOXQ
2084 * as the completion routine when the command is
2085 * handed off to the SLI layer.
2086 */
2087void
James Smart2e0fef82007-06-17 19:56:36 -05002088lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002089{
James Smart2e0fef82007-06-17 19:56:36 -05002090 struct lpfc_vport *vport = pmb->vport;
2091 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002092 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002093 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002094 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2095
James Smart0d2b6b82008-06-14 22:52:47 -04002096 /* Unblock ELS traffic */
2097 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002098 /* Check for error */
2099 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002100 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002101 "1307 READ_LA mbox error x%x state x%x\n",
2102 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002103 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002104 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002105 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2106 }
2107
James Smart04c68492009-05-22 14:52:52 -04002108 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002109
2110 memcpy(&phba->alpa_map[0], mp->virt, 128);
2111
James Smart2e0fef82007-06-17 19:56:36 -05002112 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002113 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002114 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002115 else
James Smart2e0fef82007-06-17 19:56:36 -05002116 vport->fc_flag &= ~FC_BYPASSED_MODE;
2117 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002118
James Smart0c287582009-06-10 17:22:56 -04002119 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002120 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002121 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002122 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002123 if (phba->fc_eventTag != 0)
2124 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002125 }
dea31012005-04-17 16:05:31 -05002126
2127 phba->fc_eventTag = la->eventTag;
James Smart84774a42008-08-24 21:50:06 -04002128 if (la->mm)
2129 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2130 else
2131 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
dea31012005-04-17 16:05:31 -05002132
James Smart4d9ab992009-10-02 15:16:39 -04002133 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002134 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002135 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002136 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002137 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002138 "1306 Link Up Event in loop back mode "
2139 "x%x received Data: x%x x%x x%x x%x\n",
2140 la->eventTag, phba->fc_eventTag,
2141 la->granted_AL_PA, la->UlnkSpeed,
2142 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002143 } else {
2144 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002145 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002146 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002147 la->eventTag, phba->fc_eventTag,
2148 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002149 phba->alpa_map[0],
2150 la->mm, la->fa,
2151 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002152 }
James Smart92d7f7b2007-06-17 19:56:38 -05002153 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002154 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002155 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002156 if (phba->link_flag & LS_LOOPBACK_MODE) {
2157 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2158 "1308 Link Down Event in loop back mode "
2159 "x%x received "
2160 "Data: x%x x%x x%x\n",
2161 la->eventTag, phba->fc_eventTag,
2162 phba->pport->port_state, vport->fc_flag);
2163 }
2164 else {
2165 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002166 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002167 "Data: x%x x%x x%x x%x x%x\n",
2168 la->eventTag, phba->fc_eventTag,
2169 phba->pport->port_state, vport->fc_flag,
2170 la->mm, la->fa);
2171 }
2172 lpfc_mbx_issue_link_down(phba);
2173 }
2174 if (la->mm && la->attType == AT_LINK_UP) {
2175 if (phba->link_state != LPFC_LINK_DOWN) {
2176 phba->fc_stat.LinkDown++;
2177 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2178 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002179 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002180 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002181 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002182 lpfc_mbx_issue_link_down(phba);
2183 } else
2184 lpfc_enable_la(phba);
2185
2186 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2187 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2188 "Data: x%x x%x x%x\n",
2189 la->eventTag, phba->fc_eventTag,
2190 phba->pport->port_state, vport->fc_flag);
2191 /*
2192 * The cmnd that triggered this will be waiting for this
2193 * signal.
2194 */
2195 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2196 if (phba->wait_4_mlo_maint_flg) {
2197 phba->wait_4_mlo_maint_flg = 0;
2198 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002199 }
James Smart84774a42008-08-24 21:50:06 -04002200 }
2201
2202 if (la->fa) {
2203 if (la->mm)
2204 lpfc_issue_clear_la(phba, vport);
2205 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2206 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002207 }
2208
2209lpfc_mbx_cmpl_read_la_free_mbuf:
2210 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2211 kfree(mp);
2212 mempool_free(pmb, phba->mbox_mem_pool);
2213 return;
2214}
2215
2216/*
2217 * This routine handles processing a REG_LOGIN mailbox
2218 * command upon completion. It is setup in the LPFC_MBOXQ
2219 * as the completion routine when the command is
2220 * handed off to the SLI layer.
2221 */
2222void
James Smart2e0fef82007-06-17 19:56:36 -05002223lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002224{
James Smart2e0fef82007-06-17 19:56:36 -05002225 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002226 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002227 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05002228
dea31012005-04-17 16:05:31 -05002229 pmb->context1 = NULL;
2230
2231 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05002232 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05002233 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2234 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002235 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002236 /* decrement the node reference count held for this callback
2237 * function.
2238 */
James Smart329f9bc2007-04-25 09:53:01 -04002239 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002240
2241 return;
2242}
2243
James Smart92d7f7b2007-06-17 19:56:38 -05002244static void
2245lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2246{
James Smart04c68492009-05-22 14:52:52 -04002247 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002248 struct lpfc_vport *vport = pmb->vport;
2249 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2250
2251 switch (mb->mbxStatus) {
2252 case 0x0011:
2253 case 0x0020:
2254 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04002255 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2256 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2257 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002258 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002259 }
James Smartc8685952009-11-18 15:39:16 -05002260 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart92d7f7b2007-06-17 19:56:38 -05002261 vport->unreg_vpi_cmpl = VPORT_OK;
2262 mempool_free(pmb, phba->mbox_mem_pool);
2263 /*
2264 * This shost reference might have been taken at the beginning of
2265 * lpfc_vport_delete()
2266 */
2267 if (vport->load_flag & FC_UNLOADING)
2268 scsi_host_put(shost);
2269}
2270
James Smartd7c255b2008-08-24 21:50:00 -04002271int
James Smart92d7f7b2007-06-17 19:56:38 -05002272lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2273{
2274 struct lpfc_hba *phba = vport->phba;
2275 LPFC_MBOXQ_t *mbox;
2276 int rc;
2277
2278 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2279 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002280 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002281
2282 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2283 mbox->vport = vport;
2284 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002285 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002286 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002287 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2288 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002289 mempool_free(mbox, phba->mbox_mem_pool);
2290 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002291 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002292 }
James Smartd7c255b2008-08-24 21:50:00 -04002293 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002294}
2295
2296static void
2297lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2298{
2299 struct lpfc_vport *vport = pmb->vport;
2300 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002301 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002302
2303 switch (mb->mbxStatus) {
2304 case 0x0011:
2305 case 0x9601:
2306 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002307 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2308 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2309 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002310 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2311 spin_lock_irq(shost->host_lock);
2312 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2313 spin_unlock_irq(shost->host_lock);
2314 vport->fc_myDID = 0;
2315 goto out;
2316 }
James Smart92d7f7b2007-06-17 19:56:38 -05002317
James Smartc8685952009-11-18 15:39:16 -05002318 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart92d7f7b2007-06-17 19:56:38 -05002319 vport->num_disc_nodes = 0;
2320 /* go thru NPR list and issue ELS PLOGIs */
2321 if (vport->fc_npr_cnt)
2322 lpfc_els_disc_plogi(vport);
2323
2324 if (!vport->num_disc_nodes) {
2325 spin_lock_irq(shost->host_lock);
2326 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2327 spin_unlock_irq(shost->host_lock);
2328 lpfc_can_disctmo(vport);
2329 }
2330 vport->port_state = LPFC_VPORT_READY;
2331
2332out:
2333 mempool_free(pmb, phba->mbox_mem_pool);
2334 return;
2335}
2336
James Smart21e9a0a2009-05-22 14:53:21 -04002337/**
2338 * lpfc_create_static_vport - Read HBA config region to create static vports.
2339 * @phba: pointer to lpfc hba data structure.
2340 *
2341 * This routine issue a DUMP mailbox command for config region 22 to get
2342 * the list of static vports to be created. The function create vports
2343 * based on the information returned from the HBA.
2344 **/
2345void
2346lpfc_create_static_vport(struct lpfc_hba *phba)
2347{
2348 LPFC_MBOXQ_t *pmb = NULL;
2349 MAILBOX_t *mb;
2350 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04002351 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04002352 struct fc_vport_identifiers vport_id;
2353 struct fc_vport *new_fc_vport;
2354 struct Scsi_Host *shost;
2355 struct lpfc_vport *vport;
2356 uint16_t offset = 0;
2357 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04002358 struct lpfc_dmabuf *mp;
2359 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04002360
2361 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2362 if (!pmb) {
2363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2364 "0542 lpfc_create_static_vport failed to"
2365 " allocate mailbox memory\n");
2366 return;
2367 }
2368
2369 mb = &pmb->u.mb;
2370
2371 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2372 if (!vport_info) {
2373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2374 "0543 lpfc_create_static_vport failed to"
2375 " allocate vport_info\n");
2376 mempool_free(pmb, phba->mbox_mem_pool);
2377 return;
2378 }
2379
2380 vport_buff = (uint8_t *) vport_info;
2381 do {
James Smart1c6834a2009-07-19 10:01:26 -04002382 if (lpfc_dump_static_vport(phba, pmb, offset))
2383 goto out;
2384
James Smart21e9a0a2009-05-22 14:53:21 -04002385 pmb->vport = phba->pport;
2386 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2387
2388 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2389 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2390 "0544 lpfc_create_static_vport failed to"
2391 " issue dump mailbox command ret 0x%x "
2392 "status 0x%x\n",
2393 rc, mb->mbxStatus);
2394 goto out;
2395 }
2396
James Smart1c6834a2009-07-19 10:01:26 -04002397 if (phba->sli_rev == LPFC_SLI_REV4) {
2398 byte_count = pmb->u.mqe.un.mb_words[5];
2399 mp = (struct lpfc_dmabuf *) pmb->context2;
2400 if (byte_count > sizeof(struct static_vport_info) -
2401 offset)
2402 byte_count = sizeof(struct static_vport_info)
2403 - offset;
2404 memcpy(vport_buff + offset, mp->virt, byte_count);
2405 offset += byte_count;
2406 } else {
2407 if (mb->un.varDmp.word_cnt >
2408 sizeof(struct static_vport_info) - offset)
2409 mb->un.varDmp.word_cnt =
2410 sizeof(struct static_vport_info)
2411 - offset;
2412 byte_count = mb->un.varDmp.word_cnt;
2413 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2414 vport_buff + offset,
2415 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04002416
James Smart1c6834a2009-07-19 10:01:26 -04002417 offset += byte_count;
2418 }
James Smart21e9a0a2009-05-22 14:53:21 -04002419
James Smart1c6834a2009-07-19 10:01:26 -04002420 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04002421 offset < sizeof(struct static_vport_info));
2422
2423
2424 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2425 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2426 != VPORT_INFO_REV)) {
2427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2428 "0545 lpfc_create_static_vport bad"
2429 " information header 0x%x 0x%x\n",
2430 le32_to_cpu(vport_info->signature),
2431 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2432
2433 goto out;
2434 }
2435
2436 shost = lpfc_shost_from_vport(phba->pport);
2437
2438 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2439 memset(&vport_id, 0, sizeof(vport_id));
2440 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2441 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2442 if (!vport_id.port_name || !vport_id.node_name)
2443 continue;
2444
2445 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2446 vport_id.vport_type = FC_PORTTYPE_NPIV;
2447 vport_id.disable = false;
2448 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2449
2450 if (!new_fc_vport) {
2451 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2452 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04002453 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04002454 continue;
2455 }
2456
2457 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2458 vport->vport_flag |= STATIC_VPORT;
2459 }
2460
2461out:
James Smart21e9a0a2009-05-22 14:53:21 -04002462 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04002463 if (rc != MBX_TIMEOUT) {
2464 if (pmb->context2) {
2465 mp = (struct lpfc_dmabuf *) pmb->context2;
2466 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2467 kfree(mp);
2468 }
James Smart21e9a0a2009-05-22 14:53:21 -04002469 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002470 }
James Smart21e9a0a2009-05-22 14:53:21 -04002471
2472 return;
2473}
2474
dea31012005-04-17 16:05:31 -05002475/*
2476 * This routine handles processing a Fabric REG_LOGIN mailbox
2477 * command upon completion. It is setup in the LPFC_MBOXQ
2478 * as the completion routine when the command is
2479 * handed off to the SLI layer.
2480 */
2481void
James Smart2e0fef82007-06-17 19:56:36 -05002482lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002483{
James Smart92d7f7b2007-06-17 19:56:38 -05002484 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04002485 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002486 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05002487 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002488
James Smart549e55c2007-08-02 11:09:51 -04002489 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04002490 pmb->context1 = NULL;
2491 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05002492 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04002493 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2494 "0258 Register Fabric login error: 0x%x\n",
2495 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05002496 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2497 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002498 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002499
James Smart92d7f7b2007-06-17 19:56:38 -05002500 if (phba->fc_topology == TOPOLOGY_LOOP) {
2501 /* FLOGI failed, use loop map to make discovery list */
2502 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002503
James Smart92d7f7b2007-06-17 19:56:38 -05002504 /* Start discovery */
2505 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05002506 /* Decrement the reference count to ndlp after the
2507 * reference to the ndlp are done.
2508 */
2509 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05002510 return;
2511 }
2512
2513 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05002514 /* Decrement the reference count to ndlp after the reference
2515 * to the ndlp are done.
2516 */
2517 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002518 return;
2519 }
2520
dea31012005-04-17 16:05:31 -05002521 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04002522 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05002523 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002524 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002525
James Smart2e0fef82007-06-17 19:56:36 -05002526 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart21e9a0a2009-05-22 14:53:21 -04002527 lpfc_start_fdiscs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002528 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05002529 }
2530
2531 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2532 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002533 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002534
2535 /* Drop the reference count from the mbox at the end after
2536 * all the current reference to the ndlp have been done.
2537 */
2538 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002539 return;
2540}
2541
2542/*
2543 * This routine handles processing a NameServer REG_LOGIN mailbox
2544 * command upon completion. It is setup in the LPFC_MBOXQ
2545 * as the completion routine when the command is
2546 * handed off to the SLI layer.
2547 */
2548void
James Smart2e0fef82007-06-17 19:56:36 -05002549lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002550{
James Smart21e9a0a2009-05-22 14:53:21 -04002551 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002552 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2553 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2554 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002555
2556 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05002557out:
James Smart21e9a0a2009-05-22 14:53:21 -04002558 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2559 "0260 Register NameServer error: 0x%x\n",
2560 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05002561 /* decrement the node reference count held for this
2562 * callback function.
2563 */
James Smart329f9bc2007-04-25 09:53:01 -04002564 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002565 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2566 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04002567 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04002568
2569 /* If no other thread is using the ndlp, free it */
2570 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05002571
James Smart92d7f7b2007-06-17 19:56:38 -05002572 if (phba->fc_topology == TOPOLOGY_LOOP) {
2573 /*
2574 * RegLogin failed, use loop map to make discovery
2575 * list
2576 */
2577 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002578
James Smart92d7f7b2007-06-17 19:56:38 -05002579 /* Start discovery */
2580 lpfc_disc_start(vport);
2581 return;
2582 }
2583 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05002584 return;
2585 }
2586
2587 pmb->context1 = NULL;
2588
dea31012005-04-17 16:05:31 -05002589 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04002590 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05002591 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002592 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002593
James Smart2e0fef82007-06-17 19:56:36 -05002594 if (vport->port_state < LPFC_VPORT_READY) {
2595 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05002596 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
2597 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
2598 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2599 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
2600 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
2601
2602 /* Issue SCR just before NameServer GID_FT Query */
2603 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05002604 }
2605
James Smart2e0fef82007-06-17 19:56:36 -05002606 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002607 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05002608 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05002609 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05002610 goto out;
dea31012005-04-17 16:05:31 -05002611 }
2612
James Smartfa4066b2008-01-11 01:53:27 -05002613 /* decrement the node reference count held for this
2614 * callback function.
2615 */
James Smart329f9bc2007-04-25 09:53:01 -04002616 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002617 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2618 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002619 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002620
2621 return;
2622}
2623
2624static void
James Smart2e0fef82007-06-17 19:56:36 -05002625lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002626{
James Smart2e0fef82007-06-17 19:56:36 -05002627 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2628 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05002629 struct lpfc_rport_data *rdata;
2630 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05002631 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002632
2633 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07002634 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2635 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05002636 rport_ids.port_id = ndlp->nlp_DID;
2637 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05002638
James Smart329f9bc2007-04-25 09:53:01 -04002639 /*
2640 * We leave our node pointer in rport->dd_data when we unregister a
2641 * FCP target port. But fc_remote_port_add zeros the space to which
2642 * rport->dd_data points. So, if we're reusing a previously
2643 * registered port, drop the reference that we took the last time we
2644 * registered the port.
2645 */
2646 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05002647 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04002648 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05002649
2650 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
2651 "rport add: did:x%x flg:x%x type x%x",
2652 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2653
James Smart2e0fef82007-06-17 19:56:36 -05002654 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04002655 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05002656 dev_printk(KERN_WARNING, &phba->pcidev->dev,
2657 "Warning: fc_remote_port_add failed\n");
2658 return;
2659 }
2660
2661 /* initialize static port data */
2662 rport->maxframe_size = ndlp->nlp_maxframe;
2663 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05002664 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04002665 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05002666
2667 if (ndlp->nlp_type & NLP_FCP_TARGET)
2668 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2669 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
2670 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2671
2672
2673 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
2674 fc_remote_port_rolechg(rport, rport_ids.roles);
2675
James Smart071fbd3d2006-04-15 11:53:20 -04002676 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002677 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04002678 ndlp->nlp_sid = rport->scsi_target_id;
2679 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002680 return;
2681}
2682
2683static void
James Smart2e0fef82007-06-17 19:56:36 -05002684lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002685{
2686 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04002687
James Smart858c9f62007-06-17 19:56:39 -05002688 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
2689 "rport delete: did:x%x flg:x%x type x%x",
2690 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2691
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002692 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05002693
2694 return;
2695}
2696
James Smartde0c5b32007-04-25 09:52:27 -04002697static void
James Smart2e0fef82007-06-17 19:56:36 -05002698lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04002699{
James Smart2e0fef82007-06-17 19:56:36 -05002700 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2701
2702 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002703 switch (state) {
2704 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002705 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002706 break;
2707 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002708 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002709 break;
2710 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002711 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002712 break;
2713 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002714 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002715 break;
2716 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002717 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002718 break;
2719 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002720 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002721 break;
2722 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002723 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002724 break;
2725 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002726 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002727 break;
2728 }
James Smart2e0fef82007-06-17 19:56:36 -05002729 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002730}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002731
James Smartde0c5b32007-04-25 09:52:27 -04002732static void
James Smart2e0fef82007-06-17 19:56:36 -05002733lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04002734 int old_state, int new_state)
2735{
James Smart2e0fef82007-06-17 19:56:36 -05002736 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2737
James Smartde0c5b32007-04-25 09:52:27 -04002738 if (new_state == NLP_STE_UNMAPPED_NODE) {
2739 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2740 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2741 ndlp->nlp_type |= NLP_FC_NODE;
2742 }
2743 if (new_state == NLP_STE_MAPPED_NODE)
2744 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2745 if (new_state == NLP_STE_NPR_NODE)
2746 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05002747
James Smartde0c5b32007-04-25 09:52:27 -04002748 /* Transport interface */
2749 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
2750 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002751 vport->phba->nport_event_cnt++;
2752 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002753 }
dea31012005-04-17 16:05:31 -05002754
James Smartde0c5b32007-04-25 09:52:27 -04002755 if (new_state == NLP_STE_MAPPED_NODE ||
2756 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05002757 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05002758 /*
2759 * Tell the fc transport about the port, if we haven't
2760 * already. If we have, and it's a scsi entity, be
2761 * sure to unblock any attached scsi devices
2762 */
2763 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002764 }
James Smartea2151b2008-09-07 11:52:10 -04002765 if ((new_state == NLP_STE_MAPPED_NODE) &&
2766 (vport->stat_data_enabled)) {
2767 /*
2768 * A new target is discovered, if there is no buffer for
2769 * statistical data collection allocate buffer.
2770 */
2771 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
2772 sizeof(struct lpfc_scsicmd_bkt),
2773 GFP_KERNEL);
2774
2775 if (!ndlp->lat_data)
2776 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2777 "0286 lpfc_nlp_state_cleanup failed to "
2778 "allocate statistical data buffer DID "
2779 "0x%x\n", ndlp->nlp_DID);
2780 }
James Smart858c9f62007-06-17 19:56:39 -05002781 /*
2782 * if we added to Mapped list, but the remote port
2783 * registration failed or assigned a target id outside
2784 * our presentable range - move the node to the
2785 * Unmapped List
2786 */
James Smartde0c5b32007-04-25 09:52:27 -04002787 if (new_state == NLP_STE_MAPPED_NODE &&
2788 (!ndlp->rport ||
2789 ndlp->rport->scsi_target_id == -1 ||
2790 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05002791 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002792 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05002793 spin_unlock_irq(shost->host_lock);
2794 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002795 }
James Smartde0c5b32007-04-25 09:52:27 -04002796}
2797
James Smart685f0bf2007-04-25 09:53:08 -04002798static char *
2799lpfc_nlp_state_name(char *buffer, size_t size, int state)
2800{
2801 static char *states[] = {
2802 [NLP_STE_UNUSED_NODE] = "UNUSED",
2803 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
2804 [NLP_STE_ADISC_ISSUE] = "ADISC",
2805 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
2806 [NLP_STE_PRLI_ISSUE] = "PRLI",
2807 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
2808 [NLP_STE_MAPPED_NODE] = "MAPPED",
2809 [NLP_STE_NPR_NODE] = "NPR",
2810 };
2811
James Smart311464e2007-08-02 11:10:37 -04002812 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04002813 strlcpy(buffer, states[state], size);
2814 else
2815 snprintf(buffer, size, "unknown (%d)", state);
2816 return buffer;
2817}
2818
James Smartde0c5b32007-04-25 09:52:27 -04002819void
James Smart2e0fef82007-06-17 19:56:36 -05002820lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2821 int state)
James Smartde0c5b32007-04-25 09:52:27 -04002822{
James Smart2e0fef82007-06-17 19:56:36 -05002823 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04002824 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04002825 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04002826
James Smarte8b62012007-08-02 11:10:09 -04002827 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2828 "0904 NPort state transition x%06x, %s -> %s\n",
2829 ndlp->nlp_DID,
2830 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
2831 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05002832
2833 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2834 "node statechg did:x%x old:%d ste:%d",
2835 ndlp->nlp_DID, old_state, state);
2836
James Smartde0c5b32007-04-25 09:52:27 -04002837 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04002838 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05002839 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002840 if (old_state == NLP_STE_UNMAPPED_NODE) {
2841 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
2842 ndlp->nlp_type &= ~NLP_FC_NODE;
2843 }
2844
James Smart685f0bf2007-04-25 09:53:08 -04002845 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05002846 spin_lock_irq(shost->host_lock);
2847 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2848 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002849 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05002850 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04002851
2852 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05002853 lpfc_nlp_counters(vport, state, 1);
2854 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04002855}
2856
2857void
James Smarte47c9092008-02-08 18:49:26 -05002858lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2859{
2860 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2861
2862 if (list_empty(&ndlp->nlp_listp)) {
2863 spin_lock_irq(shost->host_lock);
2864 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2865 spin_unlock_irq(shost->host_lock);
2866 }
2867}
2868
2869void
James Smart2e0fef82007-06-17 19:56:36 -05002870lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04002871{
James Smart2e0fef82007-06-17 19:56:36 -05002872 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2873
James Smart0d2b6b82008-06-14 22:52:47 -04002874 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002875 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05002876 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2877 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002878 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05002879 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002880 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05002881 NLP_STE_UNUSED_NODE);
2882}
2883
Adrian Bunk4d9db012008-02-14 23:24:02 +02002884static void
James Smarte47c9092008-02-08 18:49:26 -05002885lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2886{
James Smart0d2b6b82008-06-14 22:52:47 -04002887 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05002888 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2889 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2890 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2891 NLP_STE_UNUSED_NODE);
2892}
James Smart109f6ed2008-12-04 22:39:08 -05002893/**
James Smart3621a712009-04-06 18:47:14 -04002894 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05002895 * @vport: Pointer to Virtual Port object.
2896 * @ndlp: Pointer to FC node object.
2897 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04002898 *
2899 * This function is always called when node object need to be initialized.
2900 * It initializes all the fields of the node object. Although the reference
2901 * to phba from @ndlp can be obtained indirectly through it's reference to
2902 * @vport, a direct reference to phba is taken here by @ndlp. This is due
2903 * to the life-span of the @ndlp might go beyond the existence of @vport as
2904 * the final release of ndlp is determined by its reference count. And, the
2905 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05002906 **/
2907static inline void
2908lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2909 uint32_t did)
2910{
2911 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2912 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2913 init_timer(&ndlp->nlp_delayfunc);
2914 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2915 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2916 ndlp->nlp_DID = did;
2917 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04002918 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05002919 ndlp->nlp_sid = NLP_NO_SID;
2920 kref_init(&ndlp->kref);
2921 NLP_INT_NODE_ACT(ndlp);
2922 atomic_set(&ndlp->cmd_pending, 0);
2923 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2924}
James Smarte47c9092008-02-08 18:49:26 -05002925
2926struct lpfc_nodelist *
2927lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2928 int state)
2929{
2930 struct lpfc_hba *phba = vport->phba;
2931 uint32_t did;
2932 unsigned long flags;
2933
2934 if (!ndlp)
2935 return NULL;
2936
2937 spin_lock_irqsave(&phba->ndlp_lock, flags);
2938 /* The ndlp should not be in memory free mode */
2939 if (NLP_CHK_FREE_REQ(ndlp)) {
2940 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2941 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2942 "0277 lpfc_enable_node: ndlp:x%p "
2943 "usgmap:x%x refcnt:%d\n",
2944 (void *)ndlp, ndlp->nlp_usg_map,
2945 atomic_read(&ndlp->kref.refcount));
2946 return NULL;
2947 }
2948 /* The ndlp should not already be in active mode */
2949 if (NLP_CHK_NODE_ACT(ndlp)) {
2950 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2951 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2952 "0278 lpfc_enable_node: ndlp:x%p "
2953 "usgmap:x%x refcnt:%d\n",
2954 (void *)ndlp, ndlp->nlp_usg_map,
2955 atomic_read(&ndlp->kref.refcount));
2956 return NULL;
2957 }
2958
2959 /* Keep the original DID */
2960 did = ndlp->nlp_DID;
2961
2962 /* re-initialize ndlp except of ndlp linked list pointer */
2963 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
2964 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05002965 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05002966
2967 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2968
2969 if (state != NLP_STE_UNUSED_NODE)
2970 lpfc_nlp_set_state(vport, ndlp, state);
2971
2972 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2973 "node enable: did:x%x",
2974 ndlp->nlp_DID, 0, 0);
2975 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04002976}
2977
2978void
James Smart2e0fef82007-06-17 19:56:36 -05002979lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04002980{
James Smart87af33f2007-10-27 13:37:43 -04002981 /*
James Smartfa4066b2008-01-11 01:53:27 -05002982 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04002983 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05002984 * the ndlp from the vport. The ndlp marked as UNUSED on the list
2985 * until ALL other outstanding threads have completed. We check
2986 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04002987 */
James Smartfa4066b2008-01-11 01:53:27 -05002988 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2989 return;
James Smart51ef4c22007-08-02 11:10:31 -04002990 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04002991 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04002992 return;
dea31012005-04-17 16:05:31 -05002993}
2994
2995/*
2996 * Start / ReStart rescue timer for Discovery / RSCN handling
2997 */
2998void
James Smart2e0fef82007-06-17 19:56:36 -05002999lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003000{
James Smart2e0fef82007-06-17 19:56:36 -05003001 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3002 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003003 uint32_t tmo;
3004
James Smart2e0fef82007-06-17 19:56:36 -05003005 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003006 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003007 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3008 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003009 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003010 * FC spec states we need 3 * ratov for CT requests
3011 */
3012 tmo = ((phba->fc_ratov * 3) + 3);
3013 }
dea31012005-04-17 16:05:31 -05003014
James Smart858c9f62007-06-17 19:56:39 -05003015
3016 if (!timer_pending(&vport->fc_disctmo)) {
3017 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3018 "set disc timer: tmo:x%x state:x%x flg:x%x",
3019 tmo, vport->port_state, vport->fc_flag);
3020 }
3021
James Smart2e0fef82007-06-17 19:56:36 -05003022 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3023 spin_lock_irq(shost->host_lock);
3024 vport->fc_flag |= FC_DISC_TMO;
3025 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003026
3027 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003028 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3029 "0247 Start Discovery Timer state x%x "
3030 "Data: x%x x%lx x%x x%x\n",
3031 vport->port_state, tmo,
3032 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3033 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003034
3035 return;
3036}
3037
3038/*
3039 * Cancel rescue timer for Discovery / RSCN handling
3040 */
3041int
James Smart2e0fef82007-06-17 19:56:36 -05003042lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003043{
James Smart2e0fef82007-06-17 19:56:36 -05003044 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003045 unsigned long iflags;
3046
James Smart858c9f62007-06-17 19:56:39 -05003047 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3048 "can disc timer: state:x%x rtry:x%x flg:x%x",
3049 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3050
dea31012005-04-17 16:05:31 -05003051 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003052 if (vport->fc_flag & FC_DISC_TMO) {
3053 spin_lock_irqsave(shost->host_lock, iflags);
3054 vport->fc_flag &= ~FC_DISC_TMO;
3055 spin_unlock_irqrestore(shost->host_lock, iflags);
3056 del_timer_sync(&vport->fc_disctmo);
3057 spin_lock_irqsave(&vport->work_port_lock, iflags);
3058 vport->work_port_events &= ~WORKER_DISC_TMO;
3059 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003060 }
3061
3062 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003063 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3064 "0248 Cancel Discovery Timer state x%x "
3065 "Data: x%x x%x x%x\n",
3066 vport->port_state, vport->fc_flag,
3067 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003068 return 0;
dea31012005-04-17 16:05:31 -05003069}
3070
3071/*
3072 * Check specified ring for outstanding IOCB on the SLI queue
3073 * Return true if iocb matches the specified nport
3074 */
3075int
James Smart2e0fef82007-06-17 19:56:36 -05003076lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3077 struct lpfc_sli_ring *pring,
3078 struct lpfc_iocbq *iocb,
3079 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003080{
James Smart2e0fef82007-06-17 19:56:36 -05003081 struct lpfc_sli *psli = &phba->sli;
3082 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003083 struct lpfc_vport *vport = ndlp->vport;
3084
3085 if (iocb->vport != vport)
3086 return 0;
3087
dea31012005-04-17 16:05:31 -05003088 if (pring->ringno == LPFC_ELS_RING) {
3089 switch (icmd->ulpCommand) {
3090 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003091 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003092 return 1;
dea31012005-04-17 16:05:31 -05003093 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003094 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3095 return 1;
dea31012005-04-17 16:05:31 -05003096 case CMD_XMIT_ELS_RSP64_CX:
3097 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003098 return 1;
dea31012005-04-17 16:05:31 -05003099 }
James Smarta4bc3372006-12-02 13:34:16 -05003100 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003101
3102 } else if (pring->ringno == psli->fcp_ring) {
3103 /* Skip match check if waiting to relogin to FCP target */
3104 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003105 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003106 return 0;
dea31012005-04-17 16:05:31 -05003107 }
3108 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003109 return 1;
dea31012005-04-17 16:05:31 -05003110 }
3111 } else if (pring->ringno == psli->next_ring) {
3112
3113 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003114 return 0;
dea31012005-04-17 16:05:31 -05003115}
3116
3117/*
3118 * Free resources / clean up outstanding I/Os
3119 * associated with nlp_rpi in the LPFC_NODELIST entry.
3120 */
3121static int
James Smart2e0fef82007-06-17 19:56:36 -05003122lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003123{
James Smart2534ba72007-04-25 09:52:20 -04003124 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003125 struct lpfc_sli *psli;
3126 struct lpfc_sli_ring *pring;
3127 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003128 uint32_t i;
dea31012005-04-17 16:05:31 -05003129
James Smart92d7f7b2007-06-17 19:56:38 -05003130 lpfc_fabric_abort_nport(ndlp);
3131
dea31012005-04-17 16:05:31 -05003132 /*
3133 * Everything that matches on txcmplq will be returned
3134 * by firmware with a no rpi error.
3135 */
3136 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003137 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003138 /* Now process each ring */
3139 for (i = 0; i < psli->num_rings; i++) {
3140 pring = &psli->ring[i];
3141
James Smart2e0fef82007-06-17 19:56:36 -05003142 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003143 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003144 list) {
dea31012005-04-17 16:05:31 -05003145 /*
3146 * Check to see if iocb matches the nport we are
3147 * looking for
3148 */
James Smart92d7f7b2007-06-17 19:56:38 -05003149 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3150 ndlp))) {
dea31012005-04-17 16:05:31 -05003151 /* It matches, so deque and call compl
3152 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003153 list_move_tail(&iocb->list,
3154 &completions);
dea31012005-04-17 16:05:31 -05003155 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003156 }
3157 }
James Smart2e0fef82007-06-17 19:56:36 -05003158 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003159 }
3160 }
James Smart2534ba72007-04-25 09:52:20 -04003161
James Smarta257bf92009-04-06 18:48:10 -04003162 /* Cancel all the IOCBs from the completions list */
3163 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3164 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003165
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003166 return 0;
dea31012005-04-17 16:05:31 -05003167}
3168
3169/*
3170 * Free rpi associated with LPFC_NODELIST entry.
3171 * This routine is called from lpfc_freenode(), when we are removing
3172 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3173 * LOGO that completes successfully, and we are waiting to PLOGI back
3174 * to the remote NPort. In addition, it is called after we receive
3175 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3176 * we are waiting to PLOGI back to the remote NPort.
3177 */
3178int
James Smart2e0fef82007-06-17 19:56:36 -05003179lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003180{
James Smart2e0fef82007-06-17 19:56:36 -05003181 struct lpfc_hba *phba = vport->phba;
3182 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003183 int rc;
3184
James Smart21e9a0a2009-05-22 14:53:21 -04003185 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003186 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3187 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003188 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003189 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003190 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003191 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003192 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003193 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003194 }
dea31012005-04-17 16:05:31 -05003195 lpfc_no_rpi(phba, ndlp);
3196 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003197 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003198 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003199 return 1;
3200 }
3201 return 0;
3202}
3203
James Smart92d7f7b2007-06-17 19:56:38 -05003204void
3205lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3206{
3207 struct lpfc_hba *phba = vport->phba;
3208 LPFC_MBOXQ_t *mbox;
3209 int rc;
3210
3211 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3212 if (mbox) {
3213 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3214 mbox->vport = vport;
3215 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003216 mbox->context1 = NULL;
3217 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003218 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003219 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003220
3221 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3222 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3223 "1836 Could not issue "
3224 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003225 }
3226}
3227
3228void
3229lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3230{
3231 struct lpfc_hba *phba = vport->phba;
3232 LPFC_MBOXQ_t *mbox;
3233 int rc;
3234
3235 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3236 if (mbox) {
3237 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3238 mbox->vport = vport;
3239 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003240 mbox->context1 = NULL;
3241 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003242 if (rc != MBX_TIMEOUT)
3243 mempool_free(mbox, phba->mbox_mem_pool);
3244
3245 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003246 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3247 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003248 "unreg_did (default rpis) status %d\n",
3249 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003250 }
3251}
3252
dea31012005-04-17 16:05:31 -05003253/*
3254 * Free resources associated with LPFC_NODELIST entry
3255 * so it can be freed.
3256 */
3257static int
James Smart2e0fef82007-06-17 19:56:36 -05003258lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003259{
James Smart2e0fef82007-06-17 19:56:36 -05003260 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3261 struct lpfc_hba *phba = vport->phba;
3262 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003263 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003264
3265 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003266 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3267 "0900 Cleanup node for NPort x%x "
3268 "Data: x%x x%x x%x\n",
3269 ndlp->nlp_DID, ndlp->nlp_flag,
3270 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003271 if (NLP_CHK_FREE_REQ(ndlp)) {
3272 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3273 "0280 lpfc_cleanup_node: ndlp:x%p "
3274 "usgmap:x%x refcnt:%d\n",
3275 (void *)ndlp, ndlp->nlp_usg_map,
3276 atomic_read(&ndlp->kref.refcount));
3277 lpfc_dequeue_node(vport, ndlp);
3278 } else {
3279 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3280 "0281 lpfc_cleanup_node: ndlp:x%p "
3281 "usgmap:x%x refcnt:%d\n",
3282 (void *)ndlp, ndlp->nlp_usg_map,
3283 atomic_read(&ndlp->kref.refcount));
3284 lpfc_disable_node(vport, ndlp);
3285 }
dea31012005-04-17 16:05:31 -05003286
dea31012005-04-17 16:05:31 -05003287 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3288 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003289 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003290 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3291 mb->context2 = NULL;
3292 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3293 }
3294 }
James Smart33ccf8d2006-08-17 11:57:58 -04003295
James Smart2e0fef82007-06-17 19:56:36 -05003296 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003297 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003298 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003299 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003300 mp = (struct lpfc_dmabuf *) (mb->context1);
3301 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003302 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003303 kfree(mp);
3304 }
3305 list_del(&mb->list);
3306 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003307 /* We shall not invoke the lpfc_nlp_put to decrement
3308 * the ndlp reference count as we are in the process
3309 * of lpfc_nlp_release.
3310 */
dea31012005-04-17 16:05:31 -05003311 }
3312 }
James Smart2e0fef82007-06-17 19:56:36 -05003313 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003314
James Smarte47c9092008-02-08 18:49:26 -05003315 lpfc_els_abort(phba, ndlp);
3316
James Smart2e0fef82007-06-17 19:56:36 -05003317 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003318 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003319 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003320
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003321 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003322 del_timer_sync(&ndlp->nlp_delayfunc);
3323
James Smart0d2b6b82008-06-14 22:52:47 -04003324 list_del_init(&ndlp->els_retry_evt.evt_listp);
3325 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003326
James Smart2e0fef82007-06-17 19:56:36 -05003327 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003328
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003329 return 0;
dea31012005-04-17 16:05:31 -05003330}
3331
3332/*
3333 * Check to see if we can free the nlp back to the freelist.
3334 * If we are in the middle of using the nlp in the discovery state
3335 * machine, defer the free till we reach the end of the state machine.
3336 */
James Smart329f9bc2007-04-25 09:53:01 -04003337static void
James Smart2e0fef82007-06-17 19:56:36 -05003338lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003339{
James Smarta8adb832007-10-27 13:37:53 -04003340 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003341 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003342 LPFC_MBOXQ_t *mbox;
3343 int rc;
dea31012005-04-17 16:05:31 -05003344
James Smart0d2b6b82008-06-14 22:52:47 -04003345 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003346 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3347 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003348 /* For this case we need to cleanup the default rpi
3349 * allocated by the firmware.
3350 */
3351 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3352 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003353 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003354 (uint8_t *) &vport->fc_sparam, mbox, 0);
3355 if (rc) {
3356 mempool_free(mbox, phba->mbox_mem_pool);
3357 }
3358 else {
3359 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3360 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3361 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003362 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003363 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3364 if (rc == MBX_NOT_FINISHED) {
3365 mempool_free(mbox, phba->mbox_mem_pool);
3366 }
3367 }
3368 }
3369 }
James Smart2e0fef82007-06-17 19:56:36 -05003370 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003371
James Smart2e0fef82007-06-17 19:56:36 -05003372 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003373 * We can get here with a non-NULL ndlp->rport because when we
3374 * unregister a rport we don't break the rport/node linkage. So if we
3375 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003376 */
James Smart92d7f7b2007-06-17 19:56:38 -05003377 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003378 rdata = ndlp->rport->dd_data;
3379 rdata->pnode = NULL;
3380 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003381 }
dea31012005-04-17 16:05:31 -05003382}
3383
3384static int
James Smart2e0fef82007-06-17 19:56:36 -05003385lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3386 uint32_t did)
dea31012005-04-17 16:05:31 -05003387{
James Smart2e0fef82007-06-17 19:56:36 -05003388 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003389
3390 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003391 return 0;
dea31012005-04-17 16:05:31 -05003392
dea31012005-04-17 16:05:31 -05003393 /* First check for Direct match */
3394 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003395 return 1;
dea31012005-04-17 16:05:31 -05003396
3397 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003398 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003399 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003400 return 0;
dea31012005-04-17 16:05:31 -05003401 }
3402
3403 matchdid.un.word = did;
3404 ndlpdid.un.word = ndlp->nlp_DID;
3405 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3406 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3407 (mydid.un.b.area == matchdid.un.b.area)) {
3408 if ((ndlpdid.un.b.domain == 0) &&
3409 (ndlpdid.un.b.area == 0)) {
3410 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003411 return 1;
dea31012005-04-17 16:05:31 -05003412 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003413 return 0;
dea31012005-04-17 16:05:31 -05003414 }
3415
3416 matchdid.un.word = ndlp->nlp_DID;
3417 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3418 (mydid.un.b.area == ndlpdid.un.b.area)) {
3419 if ((matchdid.un.b.domain == 0) &&
3420 (matchdid.un.b.area == 0)) {
3421 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003422 return 1;
dea31012005-04-17 16:05:31 -05003423 }
3424 }
3425 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003426 return 0;
dea31012005-04-17 16:05:31 -05003427}
3428
James Smart685f0bf2007-04-25 09:53:08 -04003429/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003430static struct lpfc_nodelist *
3431__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003432{
James Smart2fb9bd82006-12-02 13:33:57 -05003433 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003434 uint32_t data1;
3435
James Smart2e0fef82007-06-17 19:56:36 -05003436 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3437 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04003438 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3439 ((uint32_t) ndlp->nlp_xri << 16) |
3440 ((uint32_t) ndlp->nlp_type << 8) |
3441 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04003442 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3443 "0929 FIND node DID "
3444 "Data: x%p x%x x%x x%x\n",
3445 ndlp, ndlp->nlp_DID,
3446 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04003447 return ndlp;
dea31012005-04-17 16:05:31 -05003448 }
3449 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003450
dea31012005-04-17 16:05:31 -05003451 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04003452 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3453 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05003454 return NULL;
3455}
3456
3457struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003458lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003459{
James Smart2e0fef82007-06-17 19:56:36 -05003460 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003461 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003462
James Smart2e0fef82007-06-17 19:56:36 -05003463 spin_lock_irq(shost->host_lock);
3464 ndlp = __lpfc_findnode_did(vport, did);
3465 spin_unlock_irq(shost->host_lock);
3466 return ndlp;
3467}
3468
3469struct lpfc_nodelist *
3470lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3471{
3472 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3473 struct lpfc_nodelist *ndlp;
3474
3475 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003476 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05003477 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
3478 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05003479 return NULL;
3480 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05003481 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003482 if (!ndlp)
3483 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003484 lpfc_nlp_init(vport, ndlp, did);
3485 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3486 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003487 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003488 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003489 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05003490 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3491 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
3492 if (!ndlp)
3493 return NULL;
3494 spin_lock_irq(shost->host_lock);
3495 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3496 spin_unlock_irq(shost->host_lock);
3497 return ndlp;
dea31012005-04-17 16:05:31 -05003498 }
James Smarte47c9092008-02-08 18:49:26 -05003499
James Smart58da1ff2008-04-07 10:15:56 -04003500 if ((vport->fc_flag & FC_RSCN_MODE) &&
3501 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003502 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04003503 /* If we've already recieved a PLOGI from this NPort
3504 * we don't need to try to discover it again.
3505 */
3506 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
3507 return NULL;
3508
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003509 /* Since this node is marked for discovery,
3510 * delay timeout is not needed.
3511 */
James Smart0d2b6b82008-06-14 22:52:47 -04003512 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04003513 spin_lock_irq(shost->host_lock);
3514 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3515 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04003516 } else
dea31012005-04-17 16:05:31 -05003517 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003518 } else {
James Smart87af33f2007-10-27 13:37:43 -04003519 /* If we've already recieved a PLOGI from this NPort,
3520 * or we are already in the process of discovery on it,
3521 * we don't need to try to discover it again.
3522 */
James Smart685f0bf2007-04-25 09:53:08 -04003523 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04003524 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3525 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05003526 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003527 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3528 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003529 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003530 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003531 }
3532 return ndlp;
3533}
3534
3535/* Build a list of nodes to discover based on the loopmap */
3536void
James Smart2e0fef82007-06-17 19:56:36 -05003537lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003538{
James Smart2e0fef82007-06-17 19:56:36 -05003539 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003540 int j;
3541 uint32_t alpa, index;
3542
James Smart2e0fef82007-06-17 19:56:36 -05003543 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003544 return;
James Smart2e0fef82007-06-17 19:56:36 -05003545
3546 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05003547 return;
dea31012005-04-17 16:05:31 -05003548
3549 /* Check for loop map present or not */
3550 if (phba->alpa_map[0]) {
3551 for (j = 1; j <= phba->alpa_map[0]; j++) {
3552 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05003553 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05003554 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003555 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003556 }
3557 } else {
3558 /* No alpamap, so try all alpa's */
3559 for (j = 0; j < FC_MAXLOOP; j++) {
3560 /* If cfg_scan_down is set, start from highest
3561 * ALPA (0xef) to lowest (0x1).
3562 */
James Smart3de2a652007-08-02 11:09:59 -04003563 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05003564 index = j;
3565 else
3566 index = FC_MAXLOOP - j - 1;
3567 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05003568 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05003569 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003570 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003571 }
3572 }
3573 return;
3574}
3575
dea31012005-04-17 16:05:31 -05003576void
James Smart2e0fef82007-06-17 19:56:36 -05003577lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003578{
dea31012005-04-17 16:05:31 -05003579 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05003580 struct lpfc_sli *psli = &phba->sli;
3581 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
3582 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
3583 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
3584 int rc;
3585
James Smart92d7f7b2007-06-17 19:56:38 -05003586 /*
3587 * if it's not a physical port or if we already send
3588 * clear_la then don't send it.
3589 */
3590 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04003591 (vport->port_type != LPFC_PHYSICAL_PORT) ||
3592 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05003593 return;
3594
James Smart2e0fef82007-06-17 19:56:36 -05003595 /* Link up discovery */
3596 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
3597 phba->link_state = LPFC_CLEAR_LA;
3598 lpfc_clear_la(phba, mbox);
3599 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
3600 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003601 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05003602 if (rc == MBX_NOT_FINISHED) {
3603 mempool_free(mbox, phba->mbox_mem_pool);
3604 lpfc_disc_flush_list(vport);
3605 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3606 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3607 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05003608 phba->link_state = LPFC_HBA_ERROR;
3609 }
3610 }
3611}
3612
3613/* Reg_vpi to tell firmware to resume normal operations */
3614void
3615lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
3616{
3617 LPFC_MBOXQ_t *regvpimbox;
3618
3619 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3620 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04003621 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003622 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
3623 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003624 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05003625 == MBX_NOT_FINISHED) {
3626 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003627 }
3628 }
3629}
3630
3631/* Start Link up / RSCN discovery on NPR nodes */
3632void
3633lpfc_disc_start(struct lpfc_vport *vport)
3634{
3635 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3636 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04003637 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05003638 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04003639 int did_changed;
dea31012005-04-17 16:05:31 -05003640
James Smart2e0fef82007-06-17 19:56:36 -05003641 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003642 return;
James Smart2e0fef82007-06-17 19:56:36 -05003643
3644 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05003645 clear_la_pending = 1;
3646 else
3647 clear_la_pending = 0;
3648
James Smart2e0fef82007-06-17 19:56:36 -05003649 if (vport->port_state < LPFC_VPORT_READY)
3650 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05003651
James Smart2e0fef82007-06-17 19:56:36 -05003652 lpfc_set_disctmo(vport);
3653
3654 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05003655 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003656 else
dea31012005-04-17 16:05:31 -05003657 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003658
3659 vport->fc_prevDID = vport->fc_myDID;
3660 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05003661
3662 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003663 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3664 "0202 Start Discovery hba state x%x "
3665 "Data: x%x x%x x%x\n",
3666 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
3667 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003668
3669 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003670 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05003671
3672 if (num_sent)
3673 return;
3674
James Smart92d7f7b2007-06-17 19:56:38 -05003675 /*
3676 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
3677 * continue discovery.
3678 */
3679 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05003680 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04003681 !(vport->fc_flag & FC_RSCN_MODE) &&
3682 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003683 lpfc_issue_reg_vpi(phba, vport);
3684 return;
3685 }
James Smart2e0fef82007-06-17 19:56:36 -05003686
James Smart92d7f7b2007-06-17 19:56:38 -05003687 /*
3688 * For SLI2, we need to set port_state to READY and continue
3689 * discovery.
3690 */
3691 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
3692 /* If we get here, there is nothing to ADISC */
3693 if (vport->port_type == LPFC_PHYSICAL_PORT)
3694 lpfc_issue_clear_la(phba, vport);
3695
3696 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05003697 vport->num_disc_nodes = 0;
3698 /* go thru NPR nodes and issue ELS PLOGIs */
3699 if (vport->fc_npr_cnt)
3700 lpfc_els_disc_plogi(vport);
3701
3702 if (!vport->num_disc_nodes) {
3703 spin_lock_irq(shost->host_lock);
3704 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3705 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003706 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05003707 }
3708 }
James Smart92d7f7b2007-06-17 19:56:38 -05003709 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05003710 } else {
3711 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003712 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05003713
3714 if (num_sent)
3715 return;
3716
James Smart2e0fef82007-06-17 19:56:36 -05003717 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05003718 /* Check to see if more RSCNs came in while we
3719 * were processing this one.
3720 */
James Smart2e0fef82007-06-17 19:56:36 -05003721 if ((vport->fc_rscn_id_cnt == 0) &&
3722 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
3723 spin_lock_irq(shost->host_lock);
3724 vport->fc_flag &= ~FC_RSCN_MODE;
3725 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003726 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003727 } else
James Smart2e0fef82007-06-17 19:56:36 -05003728 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05003729 }
3730 }
3731 return;
3732}
3733
3734/*
3735 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
3736 * ring the match the sppecified nodelist.
3737 */
3738static void
James Smart2e0fef82007-06-17 19:56:36 -05003739lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003740{
James Smart2534ba72007-04-25 09:52:20 -04003741 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003742 struct lpfc_sli *psli;
3743 IOCB_t *icmd;
3744 struct lpfc_iocbq *iocb, *next_iocb;
3745 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003746
3747 psli = &phba->sli;
3748 pring = &psli->ring[LPFC_ELS_RING];
3749
3750 /* Error matching iocb on txq or txcmplq
3751 * First check the txq.
3752 */
James Smart2e0fef82007-06-17 19:56:36 -05003753 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003754 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3755 if (iocb->context1 != ndlp) {
3756 continue;
3757 }
3758 icmd = &iocb->iocb;
3759 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
3760 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
3761
James Smart2534ba72007-04-25 09:52:20 -04003762 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05003763 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003764 }
3765 }
3766
3767 /* Next check the txcmplq */
3768 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
3769 if (iocb->context1 != ndlp) {
3770 continue;
3771 }
3772 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05003773 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
3774 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04003775 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05003776 }
3777 }
James Smart2e0fef82007-06-17 19:56:36 -05003778 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003779
James Smarta257bf92009-04-06 18:48:10 -04003780 /* Cancel all the IOCBs from the completions list */
3781 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3782 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003783}
3784
Adrian Bunka6ababd2007-11-05 18:07:33 +01003785static void
James Smart2e0fef82007-06-17 19:56:36 -05003786lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003787{
3788 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05003789 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003790
James Smart2e0fef82007-06-17 19:56:36 -05003791 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
3792 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04003793 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003794 if (!NLP_CHK_NODE_ACT(ndlp))
3795 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003796 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3797 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
3798 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04003799 }
dea31012005-04-17 16:05:31 -05003800 }
3801 }
dea31012005-04-17 16:05:31 -05003802}
3803
James Smart92d7f7b2007-06-17 19:56:38 -05003804void
3805lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
3806{
3807 lpfc_els_flush_rscn(vport);
3808 lpfc_els_flush_cmd(vport);
3809 lpfc_disc_flush_list(vport);
3810}
3811
dea31012005-04-17 16:05:31 -05003812/*****************************************************************************/
3813/*
3814 * NAME: lpfc_disc_timeout
3815 *
3816 * FUNCTION: Fibre Channel driver discovery timeout routine.
3817 *
3818 * EXECUTION ENVIRONMENT: interrupt only
3819 *
3820 * CALLED FROM:
3821 * Timer function
3822 *
3823 * RETURNS:
3824 * none
3825 */
3826/*****************************************************************************/
3827void
3828lpfc_disc_timeout(unsigned long ptr)
3829{
James Smart2e0fef82007-06-17 19:56:36 -05003830 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
3831 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04003832 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003833 unsigned long flags = 0;
3834
3835 if (unlikely(!phba))
3836 return;
3837
James Smart5e9d9b82008-06-14 22:52:53 -04003838 spin_lock_irqsave(&vport->work_port_lock, flags);
3839 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
3840 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05003841 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04003842 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05003843
James Smart5e9d9b82008-06-14 22:52:53 -04003844 if (!tmo_posted)
3845 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003846 return;
3847}
3848
3849static void
James Smart2e0fef82007-06-17 19:56:36 -05003850lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003851{
James Smart2e0fef82007-06-17 19:56:36 -05003852 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3853 struct lpfc_hba *phba = vport->phba;
3854 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003855 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05003856 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05003857 int rc, clrlaerr = 0;
3858
James Smart2e0fef82007-06-17 19:56:36 -05003859 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05003860 return;
3861
James Smart2e0fef82007-06-17 19:56:36 -05003862 spin_lock_irq(shost->host_lock);
3863 vport->fc_flag &= ~FC_DISC_TMO;
3864 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003865
James Smart858c9f62007-06-17 19:56:39 -05003866 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3867 "disc timeout: state:x%x rtry:x%x flg:x%x",
3868 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3869
James Smart2e0fef82007-06-17 19:56:36 -05003870 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05003871
3872 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05003873 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
3874 * FAN
3875 */
3876 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04003877 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3878 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003879 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05003880 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04003881 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003882 if (!NLP_CHK_NODE_ACT(ndlp))
3883 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003884 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3885 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003886 if (ndlp->nlp_type & NLP_FABRIC) {
3887 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05003888 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04003889
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003890 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003891 /* Fail outstanding IO now since device
3892 * is marked for PLOGI.
3893 */
James Smart2e0fef82007-06-17 19:56:36 -05003894 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003895 }
3896 }
James Smart92d7f7b2007-06-17 19:56:38 -05003897 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05003898 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05003899 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003900 }
dea31012005-04-17 16:05:31 -05003901 break;
3902
James Smart92d7f7b2007-06-17 19:56:38 -05003903 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05003904 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05003905 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05003906 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04003907 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3908 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04003909 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05003910
3911 /* Assume no Fabric and go on with discovery.
3912 * Check for outstanding ELS FLOGI to abort.
3913 */
3914
3915 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05003916 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003917
3918 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05003919 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05003920 break;
3921
3922 case LPFC_FABRIC_CFG_LINK:
3923 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
3924 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04003925 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3926 "0223 Timeout while waiting for "
3927 "NameServer login\n");
dea31012005-04-17 16:05:31 -05003928 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05003929 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05003930 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04003931 lpfc_els_abort(phba, ndlp);
3932
3933 /* ReStart discovery */
3934 goto restart_disc;
dea31012005-04-17 16:05:31 -05003935
3936 case LPFC_NS_QRY:
3937 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04003938 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3939 "0224 NameServer Query timeout "
3940 "Data: x%x x%x\n",
3941 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05003942
James Smart92d7f7b2007-06-17 19:56:38 -05003943 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
3944 /* Try it one more time */
3945 vport->fc_ns_retry++;
3946 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
3947 vport->fc_ns_retry, 0);
3948 if (rc == 0)
3949 break;
dea31012005-04-17 16:05:31 -05003950 }
James Smart92d7f7b2007-06-17 19:56:38 -05003951 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003952
James Smart87af33f2007-10-27 13:37:43 -04003953restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05003954 /*
3955 * Discovery is over.
3956 * set port_state to PORT_READY if SLI2.
3957 * cmpl_reg_vpi will set port_state to READY for SLI3.
3958 */
James Smart3772a992009-05-22 14:50:54 -04003959 if (phba->sli_rev < LPFC_SLI_REV4) {
3960 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3961 lpfc_issue_reg_vpi(phba, vport);
3962 else { /* NPIV Not enabled */
3963 lpfc_issue_clear_la(phba, vport);
3964 vport->port_state = LPFC_VPORT_READY;
3965 }
dea31012005-04-17 16:05:31 -05003966 }
3967
3968 /* Setup and issue mailbox INITIALIZE LINK command */
3969 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3970 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04003971 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3972 "0206 Device Discovery "
3973 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05003974 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003975 break;
3976 }
3977
3978 lpfc_linkdown(phba);
3979 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
3980 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04003981 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05003982 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003983 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003984 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04003985 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05003986 if (rc == MBX_NOT_FINISHED)
3987 mempool_free(initlinkmbox, phba->mbox_mem_pool);
3988
3989 break;
3990
3991 case LPFC_DISC_AUTH:
3992 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04003993 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3994 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05003995 lpfc_disc_flush_list(vport);
3996
James Smart92d7f7b2007-06-17 19:56:38 -05003997 /*
3998 * set port_state to PORT_READY if SLI2.
3999 * cmpl_reg_vpi will set port_state to READY for SLI3.
4000 */
James Smart3772a992009-05-22 14:50:54 -04004001 if (phba->sli_rev < LPFC_SLI_REV4) {
4002 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4003 lpfc_issue_reg_vpi(phba, vport);
4004 else { /* NPIV Not enabled */
4005 lpfc_issue_clear_la(phba, vport);
4006 vport->port_state = LPFC_VPORT_READY;
4007 }
dea31012005-04-17 16:05:31 -05004008 }
4009 break;
4010
James Smart2e0fef82007-06-17 19:56:36 -05004011 case LPFC_VPORT_READY:
4012 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004013 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4014 "0231 RSCN timeout Data: x%x "
4015 "x%x\n",
4016 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004017
4018 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004019 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004020
James Smart2e0fef82007-06-17 19:56:36 -05004021 lpfc_els_flush_rscn(vport);
4022 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004023 }
4024 break;
James Smart2e0fef82007-06-17 19:56:36 -05004025
James Smart92d7f7b2007-06-17 19:56:38 -05004026 default:
James Smarte8b62012007-08-02 11:10:09 -04004027 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004028 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004029 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004030 break;
4031 }
4032
4033 switch (phba->link_state) {
4034 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004035 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004036 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4037 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004038 clrlaerr = 1;
4039 break;
4040
James Smart09372822008-01-11 01:52:54 -05004041 case LPFC_LINK_UP:
4042 lpfc_issue_clear_la(phba, vport);
4043 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004044 case LPFC_LINK_UNKNOWN:
4045 case LPFC_WARM_START:
4046 case LPFC_INIT_START:
4047 case LPFC_INIT_MBX_CMDS:
4048 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004049 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004050 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4051 "0230 Unexpected timeout, hba link "
4052 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004053 clrlaerr = 1;
4054 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004055
4056 case LPFC_HBA_READY:
4057 break;
dea31012005-04-17 16:05:31 -05004058 }
4059
4060 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004061 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004062 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004063 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4064 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004065 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004066 }
4067
4068 return;
4069}
4070
dea31012005-04-17 16:05:31 -05004071/*
4072 * This routine handles processing a NameServer REG_LOGIN mailbox
4073 * command upon completion. It is setup in the LPFC_MBOXQ
4074 * as the completion routine when the command is
4075 * handed off to the SLI layer.
4076 */
4077void
James Smart2e0fef82007-06-17 19:56:36 -05004078lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004079{
James Smart04c68492009-05-22 14:52:52 -04004080 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004081 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4082 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4083 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004084
4085 pmb->context1 = NULL;
4086
dea31012005-04-17 16:05:31 -05004087 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004088 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004089 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004090 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004091
James Smart2e0fef82007-06-17 19:56:36 -05004092 /*
4093 * Start issuing Fabric-Device Management Interface (FDMI) command to
4094 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4095 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004096 */
James Smart2e0fef82007-06-17 19:56:36 -05004097
James Smart3de2a652007-08-02 11:09:59 -04004098 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004099 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4100 else
4101 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004102
James Smartfa4066b2008-01-11 01:53:27 -05004103 /* decrement the node reference count held for this callback
4104 * function.
4105 */
James Smart329f9bc2007-04-25 09:53:01 -04004106 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004107 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4108 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004109 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004110
4111 return;
4112}
4113
James Smart685f0bf2007-04-25 09:53:08 -04004114static int
4115lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4116{
4117 uint16_t *rpi = param;
4118
4119 return ndlp->nlp_rpi == *rpi;
4120}
4121
4122static int
4123lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4124{
4125 return memcmp(&ndlp->nlp_portname, param,
4126 sizeof(ndlp->nlp_portname)) == 0;
4127}
4128
Adrian Bunka6ababd2007-11-05 18:07:33 +01004129static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004130__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004131{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004132 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004133
James Smart2e0fef82007-06-17 19:56:36 -05004134 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004135 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004136 return ndlp;
4137 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004138 return NULL;
dea31012005-04-17 16:05:31 -05004139}
4140
James Smart685f0bf2007-04-25 09:53:08 -04004141/*
4142 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004143 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004144 */
4145struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004146__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004147{
James Smart2e0fef82007-06-17 19:56:36 -05004148 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004149}
4150
James Smart488d1462006-03-07 15:02:37 -05004151/*
James Smart685f0bf2007-04-25 09:53:08 -04004152 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004153 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004154 */
4155struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004156lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004157{
James Smart2e0fef82007-06-17 19:56:36 -05004158 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004159 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004160
James Smart2e0fef82007-06-17 19:56:36 -05004161 spin_lock_irq(shost->host_lock);
4162 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4163 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004164 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004165}
4166
dea31012005-04-17 16:05:31 -05004167void
James Smart2e0fef82007-06-17 19:56:36 -05004168lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4169 uint32_t did)
dea31012005-04-17 16:05:31 -05004170{
4171 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004172
4173 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004174 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004175
4176 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4177 "node init: did:x%x",
4178 ndlp->nlp_DID, 0, 0);
4179
dea31012005-04-17 16:05:31 -05004180 return;
4181}
James Smart329f9bc2007-04-25 09:53:01 -04004182
James Smart98c9ea52007-10-27 13:37:33 -04004183/* This routine releases all resources associated with a specifc NPort's ndlp
4184 * and mempool_free's the nodelist.
4185 */
James Smart311464e2007-08-02 11:10:37 -04004186static void
James Smart329f9bc2007-04-25 09:53:01 -04004187lpfc_nlp_release(struct kref *kref)
4188{
James Smarte47c9092008-02-08 18:49:26 -05004189 struct lpfc_hba *phba;
4190 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004191 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4192 kref);
James Smart858c9f62007-06-17 19:56:39 -05004193
4194 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4195 "node release: did:x%x flg:x%x type:x%x",
4196 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4197
James Smarte47c9092008-02-08 18:49:26 -05004198 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4199 "0279 lpfc_nlp_release: ndlp:x%p "
4200 "usgmap:x%x refcnt:%d\n",
4201 (void *)ndlp, ndlp->nlp_usg_map,
4202 atomic_read(&ndlp->kref.refcount));
4203
4204 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004205 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004206
4207 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004208 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004209 spin_lock_irqsave(&phba->ndlp_lock, flags);
4210 NLP_CLR_NODE_ACT(ndlp);
4211 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4212
4213 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004214 if (NLP_CHK_FREE_REQ(ndlp)) {
4215 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004216 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004217 }
James Smart329f9bc2007-04-25 09:53:01 -04004218}
4219
James Smart98c9ea52007-10-27 13:37:33 -04004220/* This routine bumps the reference count for a ndlp structure to ensure
4221 * that one discovery thread won't free a ndlp while another discovery thread
4222 * is using it.
4223 */
James Smart329f9bc2007-04-25 09:53:01 -04004224struct lpfc_nodelist *
4225lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4226{
James Smarte47c9092008-02-08 18:49:26 -05004227 struct lpfc_hba *phba;
4228 unsigned long flags;
4229
James Smart98c9ea52007-10-27 13:37:33 -04004230 if (ndlp) {
4231 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4232 "node get: did:x%x flg:x%x refcnt:x%x",
4233 ndlp->nlp_DID, ndlp->nlp_flag,
4234 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004235 /* The check of ndlp usage to prevent incrementing the
4236 * ndlp reference count that is in the process of being
4237 * released.
4238 */
James Smarta257bf92009-04-06 18:48:10 -04004239 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004240 spin_lock_irqsave(&phba->ndlp_lock, flags);
4241 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4242 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4243 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4244 "0276 lpfc_nlp_get: ndlp:x%p "
4245 "usgmap:x%x refcnt:%d\n",
4246 (void *)ndlp, ndlp->nlp_usg_map,
4247 atomic_read(&ndlp->kref.refcount));
4248 return NULL;
4249 } else
4250 kref_get(&ndlp->kref);
4251 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004252 }
James Smart329f9bc2007-04-25 09:53:01 -04004253 return ndlp;
4254}
4255
James Smart98c9ea52007-10-27 13:37:33 -04004256/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004257 * count goes to 0, this indicates the the associated nodelist should be
4258 * freed. Returning 1 indicates the ndlp resource has been released; on the
4259 * other hand, returning 0 indicates the ndlp resource has not been released
4260 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004261 */
James Smart329f9bc2007-04-25 09:53:01 -04004262int
4263lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4264{
James Smarte47c9092008-02-08 18:49:26 -05004265 struct lpfc_hba *phba;
4266 unsigned long flags;
4267
4268 if (!ndlp)
4269 return 1;
4270
4271 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4272 "node put: did:x%x flg:x%x refcnt:x%x",
4273 ndlp->nlp_DID, ndlp->nlp_flag,
4274 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004275 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004276 spin_lock_irqsave(&phba->ndlp_lock, flags);
4277 /* Check the ndlp memory free acknowledge flag to avoid the
4278 * possible race condition that kref_put got invoked again
4279 * after previous one has done ndlp memory free.
4280 */
4281 if (NLP_CHK_FREE_ACK(ndlp)) {
4282 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4283 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4284 "0274 lpfc_nlp_put: ndlp:x%p "
4285 "usgmap:x%x refcnt:%d\n",
4286 (void *)ndlp, ndlp->nlp_usg_map,
4287 atomic_read(&ndlp->kref.refcount));
4288 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004289 }
James Smarte47c9092008-02-08 18:49:26 -05004290 /* Check the ndlp inactivate log flag to avoid the possible
4291 * race condition that kref_put got invoked again after ndlp
4292 * is already in inactivating state.
4293 */
4294 if (NLP_CHK_IACT_REQ(ndlp)) {
4295 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4296 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4297 "0275 lpfc_nlp_put: ndlp:x%p "
4298 "usgmap:x%x refcnt:%d\n",
4299 (void *)ndlp, ndlp->nlp_usg_map,
4300 atomic_read(&ndlp->kref.refcount));
4301 return 1;
4302 }
4303 /* For last put, mark the ndlp usage flags to make sure no
4304 * other kref_get and kref_put on the same ndlp shall get
4305 * in between the process when the final kref_put has been
4306 * invoked on this ndlp.
4307 */
4308 if (atomic_read(&ndlp->kref.refcount) == 1) {
4309 /* Indicate ndlp is put to inactive state. */
4310 NLP_SET_IACT_REQ(ndlp);
4311 /* Acknowledge ndlp memory free has been seen. */
4312 if (NLP_CHK_FREE_REQ(ndlp))
4313 NLP_SET_FREE_ACK(ndlp);
4314 }
4315 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4316 /* Note, the kref_put returns 1 when decrementing a reference
4317 * count that was 1, it invokes the release callback function,
4318 * but it still left the reference count as 1 (not actually
4319 * performs the last decrementation). Otherwise, it actually
4320 * decrements the reference count and returns 0.
4321 */
4322 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004323}
James Smart98c9ea52007-10-27 13:37:33 -04004324
4325/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004326 * by any other discovery thread. This routine returns 1 if the
4327 * ndlp has been freed. A return value of 0 indicates the ndlp is
4328 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004329 */
4330int
4331lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4332{
4333 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4334 "node not used: did:x%x flg:x%x refcnt:x%x",
4335 ndlp->nlp_DID, ndlp->nlp_flag,
4336 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004337 if (atomic_read(&ndlp->kref.refcount) == 1)
4338 if (lpfc_nlp_put(ndlp))
4339 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004340 return 0;
4341}
James Smart6fb120a2009-05-22 14:52:59 -04004342
4343/**
4344 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4345 * @phba: Pointer to hba context object.
4346 *
4347 * This function iterate through all FC nodes associated
4348 * will all vports to check if there is any node with
4349 * fc_rports associated with it. If there is an fc_rport
4350 * associated with the node, then the node is either in
4351 * discovered state or its devloss_timer is pending.
4352 */
4353static int
4354lpfc_fcf_inuse(struct lpfc_hba *phba)
4355{
4356 struct lpfc_vport **vports;
4357 int i, ret = 0;
4358 struct lpfc_nodelist *ndlp;
4359 struct Scsi_Host *shost;
4360
4361 vports = lpfc_create_vport_work_array(phba);
4362
4363 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4364 shost = lpfc_shost_from_vport(vports[i]);
4365 spin_lock_irq(shost->host_lock);
4366 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4367 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4368 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4369 ret = 1;
4370 spin_unlock_irq(shost->host_lock);
4371 goto out;
4372 }
4373 }
4374 spin_unlock_irq(shost->host_lock);
4375 }
4376out:
4377 lpfc_destroy_vport_work_array(phba, vports);
4378 return ret;
4379}
4380
4381/**
4382 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4383 * @phba: Pointer to hba context object.
4384 * @mboxq: Pointer to mailbox object.
4385 *
4386 * This function frees memory associated with the mailbox command.
4387 */
4388static void
4389lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4390{
4391 struct lpfc_vport *vport = mboxq->vport;
4392
4393 if (mboxq->u.mb.mbxStatus) {
4394 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4395 "2555 UNREG_VFI mbxStatus error x%x "
4396 "HBA state x%x\n",
4397 mboxq->u.mb.mbxStatus, vport->port_state);
4398 }
4399 mempool_free(mboxq, phba->mbox_mem_pool);
4400 return;
4401}
4402
4403/**
4404 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4405 * @phba: Pointer to hba context object.
4406 * @mboxq: Pointer to mailbox object.
4407 *
4408 * This function frees memory associated with the mailbox command.
4409 */
4410static void
4411lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4412{
4413 struct lpfc_vport *vport = mboxq->vport;
4414
4415 if (mboxq->u.mb.mbxStatus) {
4416 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4417 "2550 UNREG_FCFI mbxStatus error x%x "
4418 "HBA state x%x\n",
4419 mboxq->u.mb.mbxStatus, vport->port_state);
4420 }
4421 mempool_free(mboxq, phba->mbox_mem_pool);
4422 return;
4423}
4424
4425/**
4426 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
4427 * @phba: Pointer to hba context object.
4428 *
4429 * This function check if there are any connected remote port for the FCF and
4430 * if all the devices are disconnected, this function unregister FCFI.
4431 * This function also tries to use another FCF for discovery.
4432 */
4433void
4434lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
4435{
4436 LPFC_MBOXQ_t *mbox;
4437 int rc;
4438 struct lpfc_vport **vports;
4439 int i;
4440
4441 spin_lock_irq(&phba->hbalock);
4442 /*
4443 * If HBA is not running in FIP mode or
4444 * If HBA does not support FCoE or
4445 * If FCF is not registered.
4446 * do nothing.
4447 */
4448 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
4449 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smart45ed1192009-10-02 15:17:02 -04004450 (!(phba->hba_flag & HBA_FIP_SUPPORT))) {
James Smart6fb120a2009-05-22 14:52:59 -04004451 spin_unlock_irq(&phba->hbalock);
4452 return;
4453 }
4454 spin_unlock_irq(&phba->hbalock);
4455
4456 if (lpfc_fcf_inuse(phba))
4457 return;
4458
James Smart4d9ab992009-10-02 15:16:39 -04004459 /* At this point, all discovery is aborted */
4460 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04004461
4462 /* Unregister VPIs */
4463 vports = lpfc_create_vport_work_array(phba);
4464 if (vports &&
4465 (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
4466 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4467 lpfc_mbx_unreg_vpi(vports[i]);
4468 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smartc8685952009-11-18 15:39:16 -05004469 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart6fb120a2009-05-22 14:52:59 -04004470 }
4471 lpfc_destroy_vport_work_array(phba, vports);
4472
4473 /* Unregister VFI */
4474 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4475 if (!mbox) {
4476 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4477 "2556 UNREG_VFI mbox allocation failed"
4478 "HBA state x%x\n",
4479 phba->pport->port_state);
4480 return;
4481 }
4482
James Smart6669f9b2009-10-02 15:16:45 -04004483 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04004484 mbox->vport = phba->pport;
4485 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
4486
4487 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4488 if (rc == MBX_NOT_FINISHED) {
4489 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4490 "2557 UNREG_VFI issue mbox failed rc x%x "
4491 "HBA state x%x\n",
4492 rc, phba->pport->port_state);
4493 mempool_free(mbox, phba->mbox_mem_pool);
4494 return;
4495 }
4496
4497 /* Unregister FCF */
4498 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4499 if (!mbox) {
4500 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4501 "2551 UNREG_FCFI mbox allocation failed"
4502 "HBA state x%x\n",
4503 phba->pport->port_state);
4504 return;
4505 }
4506
4507 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
4508 mbox->vport = phba->pport;
4509 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
4510 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4511
4512 if (rc == MBX_NOT_FINISHED) {
4513 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4514 "2552 UNREG_FCFI issue mbox failed rc x%x "
4515 "HBA state x%x\n",
4516 rc, phba->pport->port_state);
4517 mempool_free(mbox, phba->mbox_mem_pool);
4518 return;
4519 }
4520
4521 spin_lock_irq(&phba->hbalock);
4522 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_REGISTERED |
4523 FCF_DISCOVERED | FCF_BOOT_ENABLE | FCF_IN_USE |
4524 FCF_VALID_VLAN);
4525 spin_unlock_irq(&phba->hbalock);
4526
4527 /*
4528 * If driver is not unloading, check if there is any other
4529 * FCF record that can be used for discovery.
4530 */
4531 if ((phba->pport->load_flag & FC_UNLOADING) ||
4532 (phba->link_state < LPFC_LINK_UP))
4533 return;
4534
4535 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
4536
4537 if (rc)
4538 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4539 "2553 lpfc_unregister_unused_fcf failed to read FCF"
4540 " record HBA state x%x\n",
4541 phba->pport->port_state);
4542}
4543
4544/**
4545 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
4546 * @phba: Pointer to hba context object.
4547 * @buff: Buffer containing the FCF connection table as in the config
4548 * region.
4549 * This function create driver data structure for the FCF connection
4550 * record table read from config region 23.
4551 */
4552static void
4553lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
4554 uint8_t *buff)
4555{
4556 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4557 struct lpfc_fcf_conn_hdr *conn_hdr;
4558 struct lpfc_fcf_conn_rec *conn_rec;
4559 uint32_t record_count;
4560 int i;
4561
4562 /* Free the current connect table */
4563 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004564 &phba->fcf_conn_rec_list, list) {
4565 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04004566 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004567 }
James Smart6fb120a2009-05-22 14:52:59 -04004568
4569 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
4570 record_count = conn_hdr->length * sizeof(uint32_t)/
4571 sizeof(struct lpfc_fcf_conn_rec);
4572
4573 conn_rec = (struct lpfc_fcf_conn_rec *)
4574 (buff + sizeof(struct lpfc_fcf_conn_hdr));
4575
4576 for (i = 0; i < record_count; i++) {
4577 if (!(conn_rec[i].flags & FCFCNCT_VALID))
4578 continue;
4579 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
4580 GFP_KERNEL);
4581 if (!conn_entry) {
4582 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4583 "2566 Failed to allocate connection"
4584 " table entry\n");
4585 return;
4586 }
4587
4588 memcpy(&conn_entry->conn_rec, &conn_rec[i],
4589 sizeof(struct lpfc_fcf_conn_rec));
4590 conn_entry->conn_rec.vlan_tag =
4591 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
4592 conn_entry->conn_rec.flags =
4593 le16_to_cpu(conn_entry->conn_rec.flags);
4594 list_add_tail(&conn_entry->list,
4595 &phba->fcf_conn_rec_list);
4596 }
4597}
4598
4599/**
4600 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
4601 * @phba: Pointer to hba context object.
4602 * @buff: Buffer containing the FCoE parameter data structure.
4603 *
4604 * This function update driver data structure with config
4605 * parameters read from config region 23.
4606 */
4607static void
4608lpfc_read_fcoe_param(struct lpfc_hba *phba,
4609 uint8_t *buff)
4610{
4611 struct lpfc_fip_param_hdr *fcoe_param_hdr;
4612 struct lpfc_fcoe_params *fcoe_param;
4613
4614 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
4615 buff;
4616 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04004617 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04004618
4619 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
4620 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
4621 return;
4622
James Smart6fb120a2009-05-22 14:52:59 -04004623 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
4624 phba->valid_vlan = 1;
4625 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
4626 0xFFF;
4627 }
4628
4629 phba->fc_map[0] = fcoe_param->fc_map[0];
4630 phba->fc_map[1] = fcoe_param->fc_map[1];
4631 phba->fc_map[2] = fcoe_param->fc_map[2];
4632 return;
4633}
4634
4635/**
4636 * lpfc_get_rec_conf23 - Get a record type in config region data.
4637 * @buff: Buffer containing config region 23 data.
4638 * @size: Size of the data buffer.
4639 * @rec_type: Record type to be searched.
4640 *
4641 * This function searches config region data to find the begining
4642 * of the record specified by record_type. If record found, this
4643 * function return pointer to the record else return NULL.
4644 */
4645static uint8_t *
4646lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
4647{
4648 uint32_t offset = 0, rec_length;
4649
4650 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
4651 (size < sizeof(uint32_t)))
4652 return NULL;
4653
4654 rec_length = buff[offset + 1];
4655
4656 /*
4657 * One TLV record has one word header and number of data words
4658 * specified in the rec_length field of the record header.
4659 */
4660 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
4661 <= size) {
4662 if (buff[offset] == rec_type)
4663 return &buff[offset];
4664
4665 if (buff[offset] == LPFC_REGION23_LAST_REC)
4666 return NULL;
4667
4668 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
4669 rec_length = buff[offset + 1];
4670 }
4671 return NULL;
4672}
4673
4674/**
4675 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
4676 * @phba: Pointer to lpfc_hba data structure.
4677 * @buff: Buffer containing config region 23 data.
4678 * @size: Size of the data buffer.
4679 *
4680 * This fuction parse the FCoE config parameters in config region 23 and
4681 * populate driver data structure with the parameters.
4682 */
4683void
4684lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
4685 uint8_t *buff,
4686 uint32_t size)
4687{
4688 uint32_t offset = 0, rec_length;
4689 uint8_t *rec_ptr;
4690
4691 /*
4692 * If data size is less than 2 words signature and version cannot be
4693 * verified.
4694 */
4695 if (size < 2*sizeof(uint32_t))
4696 return;
4697
4698 /* Check the region signature first */
4699 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
4700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4701 "2567 Config region 23 has bad signature\n");
4702 return;
4703 }
4704
4705 offset += 4;
4706
4707 /* Check the data structure version */
4708 if (buff[offset] != LPFC_REGION23_VERSION) {
4709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4710 "2568 Config region 23 has bad version\n");
4711 return;
4712 }
4713 offset += 4;
4714
4715 rec_length = buff[offset + 1];
4716
4717 /* Read FCoE param record */
4718 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4719 size - offset, FCOE_PARAM_TYPE);
4720 if (rec_ptr)
4721 lpfc_read_fcoe_param(phba, rec_ptr);
4722
4723 /* Read FCF connection table */
4724 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4725 size - offset, FCOE_CONN_TBL_TYPE);
4726 if (rec_ptr)
4727 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
4728
4729}