blob: 7143d71c501f8fd735dfbab55964d030c4467f81 [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 Smart695a8142010-01-26 23:08:03 -0500709 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
710
James Smart45ed1192009-10-02 15:17:02 -0400711 /* Cleanup any outstanding received buffers */
712 lpfc_cleanup_rcv_buffers(vport);
713
James Smart92d7f7b2007-06-17 19:56:38 -0500714 /* Cleanup any outstanding RSCN activity */
715 lpfc_els_flush_rscn(vport);
716
717 /* Cleanup any outstanding ELS commands */
718 lpfc_els_flush_cmd(vport);
719
720 lpfc_cleanup_rpis(vport, 0);
721
James Smart92d7f7b2007-06-17 19:56:38 -0500722 /* Turn off discovery timer if its running */
723 lpfc_can_disctmo(vport);
724}
725
James Smart3772a992009-05-22 14:50:54 -0400726void
James Smart98c9ea52007-10-27 13:37:33 -0400727lpfc_linkdown_port(struct lpfc_vport *vport)
728{
729 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
730
731 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
732
733 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
734 "Link Down: state:x%x rtry:x%x flg:x%x",
735 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
736
737 lpfc_port_link_failure(vport);
738
739}
740
dea31012005-04-17 16:05:31 -0500741int
James Smart685f0bf2007-04-25 09:53:08 -0400742lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500743{
James Smart2e0fef82007-06-17 19:56:36 -0500744 struct lpfc_vport *vport = phba->pport;
745 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400746 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400747 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400748 int i;
dea31012005-04-17 16:05:31 -0500749
James Smart3163f722008-02-08 18:50:25 -0500750 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500751 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500752
753 /* Block all SCSI stack I/Os */
754 lpfc_scsi_dev_block(phba);
755
James Smart2e0fef82007-06-17 19:56:36 -0500756 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -0400757 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_DISCOVERED);
James Smart92d7f7b2007-06-17 19:56:38 -0500758 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500759 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500760 phba->pport->fc_flag &= ~FC_LBIT;
761 }
James Smart2e0fef82007-06-17 19:56:36 -0500762 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400763 vports = lpfc_create_vport_work_array(phba);
764 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400765 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400766 /* Issue a LINK DOWN event to all nodes */
767 lpfc_linkdown_port(vports[i]);
768 }
James Smart09372822008-01-11 01:52:54 -0500769 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500770 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500771 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
772 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500773 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500774 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500775 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400776 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500777 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500778 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500779 }
780 }
781
dea31012005-04-17 16:05:31 -0500782 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500783 if (phba->pport->fc_flag & FC_PT2PT) {
784 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500785 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
786 if (mb) {
dea31012005-04-17 16:05:31 -0500787 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500788 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500789 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400790 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500791 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500792 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500793 }
794 }
James Smart2e0fef82007-06-17 19:56:36 -0500795 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500796 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500797 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500798 }
James Smart2e0fef82007-06-17 19:56:36 -0500799
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500800 return 0;
dea31012005-04-17 16:05:31 -0500801}
802
James Smart92d7f7b2007-06-17 19:56:38 -0500803static void
804lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500805{
James Smart92d7f7b2007-06-17 19:56:38 -0500806 struct lpfc_nodelist *ndlp;
807
808 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500809 if (!NLP_CHK_NODE_ACT(ndlp))
810 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500811 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
812 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500813 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500814 /* On Linkup its safe to clean up the ndlp
815 * from Fabric connections.
816 */
James Smart92d7f7b2007-06-17 19:56:38 -0500817 if (ndlp->nlp_DID != Fabric_DID)
818 lpfc_unreg_rpi(vport, ndlp);
819 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
820 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500821 /* Fail outstanding IO now since device is
822 * marked for PLOGI.
823 */
James Smart92d7f7b2007-06-17 19:56:38 -0500824 lpfc_unreg_rpi(vport, ndlp);
825 }
826 }
827}
828
829static void
830lpfc_linkup_port(struct lpfc_vport *vport)
831{
832 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500833 struct lpfc_hba *phba = vport->phba;
834
835 if ((vport->load_flag & FC_UNLOADING) != 0)
836 return;
837
James Smart858c9f62007-06-17 19:56:39 -0500838 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
839 "Link Up: top:x%x speed:x%x flg:x%x",
840 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
841
James Smart92d7f7b2007-06-17 19:56:38 -0500842 /* If NPIV is not enabled, only bring the physical port up */
843 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
844 (vport != phba->pport))
845 return;
dea31012005-04-17 16:05:31 -0500846
James Smart2e0fef82007-06-17 19:56:36 -0500847 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400848
James Smart2e0fef82007-06-17 19:56:36 -0500849 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500850 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
851 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
852 vport->fc_flag |= FC_NDISC_ACTIVE;
853 vport->fc_ns_retry = 0;
854 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500855
James Smart92d7f7b2007-06-17 19:56:38 -0500856 if (vport->fc_flag & FC_LBIT)
857 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500858
James Smart92d7f7b2007-06-17 19:56:38 -0500859}
860
861static int
862lpfc_linkup(struct lpfc_hba *phba)
863{
James Smart549e55c2007-08-02 11:09:51 -0400864 struct lpfc_vport **vports;
865 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500866
867 phba->link_state = LPFC_LINK_UP;
868
869 /* Unblock fabric iocbs if they are blocked */
870 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
871 del_timer_sync(&phba->fabric_block_timer);
872
James Smart549e55c2007-08-02 11:09:51 -0400873 vports = lpfc_create_vport_work_array(phba);
874 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400875 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400876 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500877 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400878 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
879 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500880 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500881
882 return 0;
883}
884
885/*
886 * This routine handles processing a CLEAR_LA mailbox
887 * command upon completion. It is setup in the LPFC_MBOXQ
888 * as the completion routine when the command is
889 * handed off to the SLI layer.
890 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100891static void
James Smart2e0fef82007-06-17 19:56:36 -0500892lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500893{
James Smart2e0fef82007-06-17 19:56:36 -0500894 struct lpfc_vport *vport = pmb->vport;
895 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
896 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400897 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500898 uint32_t control;
899
dea31012005-04-17 16:05:31 -0500900 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500901 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500902 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
903 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
904
905 /* Check for error */
906 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500907 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400908 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
909 "0320 CLEAR_LA mbxStatus error x%x hba "
910 "state x%x\n",
911 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500912 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500913 goto out;
914 }
915
James Smart92d7f7b2007-06-17 19:56:38 -0500916 if (vport->port_type == LPFC_PHYSICAL_PORT)
917 phba->link_state = LPFC_HBA_READY;
918
919 spin_lock_irq(&phba->hbalock);
920 psli->sli_flag |= LPFC_PROCESS_LA;
921 control = readl(phba->HCregaddr);
922 control |= HC_LAINT_ENA;
923 writel(control, phba->HCregaddr);
924 readl(phba->HCregaddr); /* flush */
925 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500926 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500927 return;
dea31012005-04-17 16:05:31 -0500928
dea31012005-04-17 16:05:31 -0500929out:
930 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400931 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
932 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500933 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500934
James Smart2e0fef82007-06-17 19:56:36 -0500935 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400936 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500937 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500938
James Smart2e0fef82007-06-17 19:56:36 -0500939 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500940
941 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500942
943 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500944 psli->sli_flag |= LPFC_PROCESS_LA;
945 control = readl(phba->HCregaddr);
946 control |= HC_LAINT_ENA;
947 writel(control, phba->HCregaddr);
948 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500949 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500950
951 return;
952}
953
James Smart2e0fef82007-06-17 19:56:36 -0500954
dea31012005-04-17 16:05:31 -0500955static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500956lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500957{
James Smart2e0fef82007-06-17 19:56:36 -0500958 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500959
James Smart04c68492009-05-22 14:52:52 -0400960 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500961 goto out;
dea31012005-04-17 16:05:31 -0500962
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500963 mempool_free(pmb, phba->mbox_mem_pool);
964
965 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500966 vport->fc_flag & FC_PUBLIC_LOOP &&
967 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500968 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500969 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500970 * LPFC_LOCAL_CFG_LINK while waiting for FAN
971 */
James Smart2e0fef82007-06-17 19:56:36 -0500972 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500973 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500974 }
dea31012005-04-17 16:05:31 -0500975
James Smart2e0fef82007-06-17 19:56:36 -0500976 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500977 * LPFC_FLOGI while waiting for FLOGI cmpl
978 */
James Smart92d7f7b2007-06-17 19:56:38 -0500979 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500980 lpfc_initial_flogi(vport);
981 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500982 return;
dea31012005-04-17 16:05:31 -0500983
984out:
James Smarte8b62012007-08-02 11:10:09 -0400985 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
986 "0306 CONFIG_LINK mbxStatus error x%x "
987 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400988 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500989 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500990
991 lpfc_linkdown(phba);
992
James Smarte8b62012007-08-02 11:10:09 -0400993 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
994 "0200 CONFIG_LINK bad hba state x%x\n",
995 vport->port_state);
dea31012005-04-17 16:05:31 -0500996
James Smart92d7f7b2007-06-17 19:56:38 -0500997 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500998 return;
999}
1000
1001static void
James Smart6fb120a2009-05-22 14:52:59 -04001002lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1003{
1004 struct lpfc_vport *vport = mboxq->vport;
1005 unsigned long flags;
1006
1007 if (mboxq->u.mb.mbxStatus) {
1008 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1009 "2017 REG_FCFI mbxStatus error x%x "
1010 "HBA state x%x\n",
1011 mboxq->u.mb.mbxStatus, vport->port_state);
1012 mempool_free(mboxq, phba->mbox_mem_pool);
1013 return;
1014 }
1015
1016 /* Start FCoE discovery by sending a FLOGI. */
1017 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1018 /* Set the FCFI registered flag */
1019 spin_lock_irqsave(&phba->hbalock, flags);
1020 phba->fcf.fcf_flag |= FCF_REGISTERED;
1021 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart32b97932009-07-19 10:01:21 -04001022 /* If there is a pending FCoE event, restart FCF table scan. */
1023 if (lpfc_check_pending_fcoe_event(phba, 1)) {
1024 mempool_free(mboxq, phba->mbox_mem_pool);
1025 return;
1026 }
James Smart1c6f4ef52009-11-18 15:40:49 -05001027 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4d9ab992009-10-02 15:16:39 -04001028 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1029 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001030 spin_unlock_irqrestore(&phba->hbalock, flags);
1031 if (vport->port_state != LPFC_FLOGI)
James Smart6fb120a2009-05-22 14:52:59 -04001032 lpfc_initial_flogi(vport);
James Smart6fb120a2009-05-22 14:52:59 -04001033
1034 mempool_free(mboxq, phba->mbox_mem_pool);
1035 return;
1036}
1037
1038/**
1039 * lpfc_fab_name_match - Check if the fcf fabric name match.
1040 * @fab_name: pointer to fabric name.
1041 * @new_fcf_record: pointer to fcf record.
1042 *
1043 * This routine compare the fcf record's fabric name with provided
1044 * fabric name. If the fabric name are identical this function
1045 * returns 1 else return 0.
1046 **/
1047static uint32_t
1048lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1049{
1050 if ((fab_name[0] ==
1051 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record)) &&
1052 (fab_name[1] ==
1053 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record)) &&
1054 (fab_name[2] ==
1055 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record)) &&
1056 (fab_name[3] ==
1057 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record)) &&
1058 (fab_name[4] ==
1059 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record)) &&
1060 (fab_name[5] ==
1061 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record)) &&
1062 (fab_name[6] ==
1063 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record)) &&
1064 (fab_name[7] ==
1065 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record)))
1066 return 1;
1067 else
1068 return 0;
1069}
1070
1071/**
James Smart8fa38512009-07-19 10:01:03 -04001072 * lpfc_sw_name_match - Check if the fcf switch name match.
1073 * @fab_name: pointer to fabric name.
1074 * @new_fcf_record: pointer to fcf record.
1075 *
1076 * This routine compare the fcf record's switch name with provided
1077 * switch name. If the switch name are identical this function
1078 * returns 1 else return 0.
1079 **/
1080static uint32_t
1081lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1082{
1083 if ((sw_name[0] ==
1084 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record)) &&
1085 (sw_name[1] ==
1086 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record)) &&
1087 (sw_name[2] ==
1088 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record)) &&
1089 (sw_name[3] ==
1090 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record)) &&
1091 (sw_name[4] ==
1092 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record)) &&
1093 (sw_name[5] ==
1094 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record)) &&
1095 (sw_name[6] ==
1096 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record)) &&
1097 (sw_name[7] ==
1098 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record)))
1099 return 1;
1100 else
1101 return 0;
1102}
1103
1104/**
James Smart6fb120a2009-05-22 14:52:59 -04001105 * lpfc_mac_addr_match - Check if the fcf mac address match.
1106 * @phba: pointer to lpfc hba data structure.
1107 * @new_fcf_record: pointer to fcf record.
1108 *
1109 * This routine compare the fcf record's mac address with HBA's
1110 * FCF mac address. If the mac addresses are identical this function
1111 * returns 1 else return 0.
1112 **/
1113static uint32_t
1114lpfc_mac_addr_match(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1115{
1116 if ((phba->fcf.mac_addr[0] ==
1117 bf_get(lpfc_fcf_record_mac_0, new_fcf_record)) &&
1118 (phba->fcf.mac_addr[1] ==
1119 bf_get(lpfc_fcf_record_mac_1, new_fcf_record)) &&
1120 (phba->fcf.mac_addr[2] ==
1121 bf_get(lpfc_fcf_record_mac_2, new_fcf_record)) &&
1122 (phba->fcf.mac_addr[3] ==
1123 bf_get(lpfc_fcf_record_mac_3, new_fcf_record)) &&
1124 (phba->fcf.mac_addr[4] ==
1125 bf_get(lpfc_fcf_record_mac_4, new_fcf_record)) &&
1126 (phba->fcf.mac_addr[5] ==
1127 bf_get(lpfc_fcf_record_mac_5, new_fcf_record)))
1128 return 1;
1129 else
1130 return 0;
1131}
1132
1133/**
1134 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1135 * @phba: pointer to lpfc hba data structure.
1136 * @new_fcf_record: pointer to fcf record.
1137 *
1138 * This routine copies the FCF information from the FCF
1139 * record to lpfc_hba data structure.
1140 **/
1141static void
1142lpfc_copy_fcf_record(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1143{
1144 phba->fcf.fabric_name[0] =
1145 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1146 phba->fcf.fabric_name[1] =
1147 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1148 phba->fcf.fabric_name[2] =
1149 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1150 phba->fcf.fabric_name[3] =
1151 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1152 phba->fcf.fabric_name[4] =
1153 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1154 phba->fcf.fabric_name[5] =
1155 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1156 phba->fcf.fabric_name[6] =
1157 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1158 phba->fcf.fabric_name[7] =
1159 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1160 phba->fcf.mac_addr[0] =
1161 bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1162 phba->fcf.mac_addr[1] =
1163 bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1164 phba->fcf.mac_addr[2] =
1165 bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1166 phba->fcf.mac_addr[3] =
1167 bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1168 phba->fcf.mac_addr[4] =
1169 bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1170 phba->fcf.mac_addr[5] =
1171 bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1172 phba->fcf.fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1173 phba->fcf.priority = new_fcf_record->fip_priority;
James Smart8fa38512009-07-19 10:01:03 -04001174 phba->fcf.switch_name[0] =
1175 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1176 phba->fcf.switch_name[1] =
1177 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1178 phba->fcf.switch_name[2] =
1179 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1180 phba->fcf.switch_name[3] =
1181 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1182 phba->fcf.switch_name[4] =
1183 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1184 phba->fcf.switch_name[5] =
1185 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1186 phba->fcf.switch_name[6] =
1187 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1188 phba->fcf.switch_name[7] =
1189 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001190}
1191
1192/**
1193 * lpfc_register_fcf - Register the FCF with hba.
1194 * @phba: pointer to lpfc hba data structure.
1195 *
1196 * This routine issues a register fcfi mailbox command to register
1197 * the fcf with HBA.
1198 **/
1199static void
1200lpfc_register_fcf(struct lpfc_hba *phba)
1201{
1202 LPFC_MBOXQ_t *fcf_mbxq;
1203 int rc;
1204 unsigned long flags;
1205
1206 spin_lock_irqsave(&phba->hbalock, flags);
1207
1208 /* If the FCF is not availabe do nothing. */
1209 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smart4d9ab992009-10-02 15:16:39 -04001210 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001211 spin_unlock_irqrestore(&phba->hbalock, flags);
1212 return;
1213 }
1214
1215 /* The FCF is already registered, start discovery */
1216 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1217 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
James Smart32b97932009-07-19 10:01:21 -04001218 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001219 spin_unlock_irqrestore(&phba->hbalock, flags);
1220 if (phba->pport->port_state != LPFC_FLOGI)
1221 lpfc_initial_flogi(phba->pport);
1222 return;
1223 }
1224 spin_unlock_irqrestore(&phba->hbalock, flags);
1225
1226 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1227 GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001228 if (!fcf_mbxq) {
1229 spin_lock_irqsave(&phba->hbalock, flags);
1230 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1231 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001232 return;
James Smart4d9ab992009-10-02 15:16:39 -04001233 }
James Smart6fb120a2009-05-22 14:52:59 -04001234
1235 lpfc_reg_fcfi(phba, fcf_mbxq);
1236 fcf_mbxq->vport = phba->pport;
1237 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1238 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001239 if (rc == MBX_NOT_FINISHED) {
1240 spin_lock_irqsave(&phba->hbalock, flags);
1241 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1242 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001243 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001244 }
James Smart6fb120a2009-05-22 14:52:59 -04001245
1246 return;
1247}
1248
1249/**
1250 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1251 * @phba: pointer to lpfc hba data structure.
1252 * @new_fcf_record: pointer to fcf record.
1253 * @boot_flag: Indicates if this record used by boot bios.
1254 * @addr_mode: The address mode to be used by this FCF
1255 *
1256 * This routine compare the fcf record with connect list obtained from the
1257 * config region to decide if this FCF can be used for SAN discovery. It returns
1258 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1259 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1260 * is used by boot bios and addr_mode will indicate the addressing mode to be
1261 * used for this FCF when the function returns.
1262 * If the FCF record need to be used with a particular vlan id, the vlan is
1263 * set in the vlan_id on return of the function. If not VLAN tagging need to
1264 * be used with the FCF vlan_id will be set to 0xFFFF;
1265 **/
1266static int
1267lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1268 struct fcf_record *new_fcf_record,
1269 uint32_t *boot_flag, uint32_t *addr_mode,
1270 uint16_t *vlan_id)
1271{
1272 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001273 int i, j, fcf_vlan_id = 0;
1274
1275 /* Find the lowest VLAN id in the FCF record */
1276 for (i = 0; i < 512; i++) {
1277 if (new_fcf_record->vlan_bitmap[i]) {
1278 fcf_vlan_id = i * 8;
1279 j = 0;
1280 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1281 j++;
1282 fcf_vlan_id++;
1283 }
1284 break;
1285 }
1286 }
James Smart6fb120a2009-05-22 14:52:59 -04001287
James Smart0c287582009-06-10 17:22:56 -04001288 /* If FCF not available return 0 */
1289 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1290 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1291 return 0;
1292
James Smart45ed1192009-10-02 15:17:02 -04001293 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001294 *boot_flag = 0;
1295 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1296 new_fcf_record);
1297 if (phba->valid_vlan)
1298 *vlan_id = phba->vlan_id;
1299 else
1300 *vlan_id = 0xFFFF;
1301 return 1;
1302 }
1303
1304 /*
1305 * If there are no FCF connection table entry, driver connect to all
1306 * FCFs.
1307 */
1308 if (list_empty(&phba->fcf_conn_rec_list)) {
1309 *boot_flag = 0;
1310 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1311 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001312
1313 /*
1314 * When there are no FCF connect entries, use driver's default
1315 * addressing mode - FPMA.
1316 */
1317 if (*addr_mode & LPFC_FCF_FPMA)
1318 *addr_mode = LPFC_FCF_FPMA;
1319
James Smart4d9ab992009-10-02 15:16:39 -04001320 /* If FCF record report a vlan id use that vlan id */
1321 if (fcf_vlan_id)
1322 *vlan_id = fcf_vlan_id;
1323 else
1324 *vlan_id = 0xFFFF;
James Smart6fb120a2009-05-22 14:52:59 -04001325 return 1;
1326 }
1327
1328 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list, list) {
1329 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1330 continue;
1331
1332 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1333 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001334 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001335 continue;
James Smart8fa38512009-07-19 10:01:03 -04001336 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1337 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1338 new_fcf_record))
1339 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001340 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1341 /*
1342 * If the vlan bit map does not have the bit set for the
1343 * vlan id to be used, then it is not a match.
1344 */
1345 if (!(new_fcf_record->vlan_bitmap
1346 [conn_entry->conn_rec.vlan_tag / 8] &
1347 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1348 continue;
1349 }
1350
1351 /*
James Smart0c287582009-06-10 17:22:56 -04001352 * If connection record does not support any addressing mode,
1353 * skip the FCF record.
1354 */
1355 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1356 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1357 continue;
1358
1359 /*
James Smart6fb120a2009-05-22 14:52:59 -04001360 * Check if the connection record specifies a required
1361 * addressing mode.
1362 */
1363 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1364 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1365
1366 /*
1367 * If SPMA required but FCF not support this continue.
1368 */
1369 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1370 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1371 new_fcf_record) & LPFC_FCF_SPMA))
1372 continue;
1373
1374 /*
1375 * If FPMA required but FCF not support this continue.
1376 */
1377 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1378 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1379 new_fcf_record) & LPFC_FCF_FPMA))
1380 continue;
1381 }
1382
1383 /*
1384 * This fcf record matches filtering criteria.
1385 */
1386 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1387 *boot_flag = 1;
1388 else
1389 *boot_flag = 0;
1390
James Smart0c287582009-06-10 17:22:56 -04001391 /*
1392 * If user did not specify any addressing mode, or if the
1393 * prefered addressing mode specified by user is not supported
1394 * by FCF, allow fabric to pick the addressing mode.
1395 */
James Smart6fb120a2009-05-22 14:52:59 -04001396 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1397 new_fcf_record);
1398 /*
1399 * If the user specified a required address mode, assign that
1400 * address mode
1401 */
1402 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1403 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1404 *addr_mode = (conn_entry->conn_rec.flags &
1405 FCFCNCT_AM_SPMA) ?
1406 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1407 /*
1408 * If the user specified a prefered address mode, use the
1409 * addr mode only if FCF support the addr_mode.
1410 */
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_SPMA))
1415 *addr_mode = LPFC_FCF_SPMA;
1416 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1417 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1418 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1419 (*addr_mode & LPFC_FCF_FPMA))
1420 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001421
James Smart4d9ab992009-10-02 15:16:39 -04001422 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001423 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1424 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001425 /*
1426 * If no vlan id is specified in connect list, use the vlan id
1427 * in the FCF record
1428 */
1429 else if (fcf_vlan_id)
1430 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001431 else
1432 *vlan_id = 0xFFFF;
1433
1434 return 1;
1435 }
1436
1437 return 0;
1438}
1439
1440/**
James Smart32b97932009-07-19 10:01:21 -04001441 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1442 * @phba: pointer to lpfc hba data structure.
1443 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1444 *
1445 * This function check if there is any fcoe event pending while driver
1446 * scan FCF entries. If there is any pending event, it will restart the
1447 * FCF saning and return 1 else return 0.
1448 */
1449int
1450lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1451{
1452 LPFC_MBOXQ_t *mbox;
1453 int rc;
1454 /*
1455 * If the Link is up and no FCoE events while in the
1456 * FCF discovery, no need to restart FCF discovery.
1457 */
1458 if ((phba->link_state >= LPFC_LINK_UP) &&
1459 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1460 return 0;
1461
1462 spin_lock_irq(&phba->hbalock);
1463 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1464 spin_unlock_irq(&phba->hbalock);
1465
1466 if (phba->link_state >= LPFC_LINK_UP)
1467 lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart1c6f4ef52009-11-18 15:40:49 -05001468 else {
James Smart4d9ab992009-10-02 15:16:39 -04001469 /*
1470 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1471 * flag
1472 */
James Smart1c6f4ef52009-11-18 15:40:49 -05001473 spin_lock_irq(&phba->hbalock);
James Smart4d9ab992009-10-02 15:16:39 -04001474 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001475 spin_unlock_irq(&phba->hbalock);
1476 }
James Smart32b97932009-07-19 10:01:21 -04001477
1478 if (unreg_fcf) {
1479 spin_lock_irq(&phba->hbalock);
1480 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1481 spin_unlock_irq(&phba->hbalock);
1482 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1483 if (!mbox) {
1484 lpfc_printf_log(phba, KERN_ERR,
1485 LOG_DISCOVERY|LOG_MBOX,
1486 "2610 UNREG_FCFI mbox allocation failed\n");
1487 return 1;
1488 }
1489 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
1490 mbox->vport = phba->pport;
1491 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
1492 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1493 if (rc == MBX_NOT_FINISHED) {
1494 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
1495 "2611 UNREG_FCFI issue mbox failed\n");
1496 mempool_free(mbox, phba->mbox_mem_pool);
1497 }
1498 }
1499
1500 return 1;
1501}
1502
1503/**
James Smart6fb120a2009-05-22 14:52:59 -04001504 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
1505 * @phba: pointer to lpfc hba data structure.
1506 * @mboxq: pointer to mailbox object.
1507 *
1508 * This function iterate through all the fcf records available in
1509 * HBA and choose the optimal FCF record for discovery. After finding
1510 * the FCF for discovery it register the FCF record and kick start
1511 * discovery.
1512 * If FCF_IN_USE flag is set in currently used FCF, the routine try to
1513 * use a FCF record which match fabric name and mac address of the
1514 * currently used FCF record.
1515 * If the driver support only one FCF, it will try to use the FCF record
1516 * used by BOOT_BIOS.
1517 */
1518void
1519lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1520{
1521 void *virt_addr;
1522 dma_addr_t phys_addr;
1523 uint8_t *bytep;
1524 struct lpfc_mbx_sge sge;
1525 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1526 uint32_t shdr_status, shdr_add_status;
1527 union lpfc_sli4_cfg_shdr *shdr;
1528 struct fcf_record *new_fcf_record;
1529 int rc;
1530 uint32_t boot_flag, addr_mode;
1531 uint32_t next_fcf_index;
1532 unsigned long flags;
1533 uint16_t vlan_id;
1534
James Smart32b97932009-07-19 10:01:21 -04001535 /* If there is pending FCoE event restart FCF table scan */
1536 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1537 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1538 return;
1539 }
1540
James Smart6fb120a2009-05-22 14:52:59 -04001541 /* Get the first SGE entry from the non-embedded DMA memory. This
1542 * routine only uses a single SGE.
1543 */
1544 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1545 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1546 if (unlikely(!mboxq->sge_array)) {
1547 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1548 "2524 Failed to get the non-embedded SGE "
1549 "virtual address\n");
1550 goto out;
1551 }
1552 virt_addr = mboxq->sge_array->addr[0];
1553
1554 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1555 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1556 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
1557 &shdr->response);
1558 /*
1559 * The FCF Record was read and there is no reason for the driver
1560 * to maintain the FCF record data or memory. Instead, just need
1561 * to book keeping the FCFIs can be used.
1562 */
1563 if (shdr_status || shdr_add_status) {
James Smartdef9c7a2009-12-21 17:02:28 -05001564 if (shdr_status == STATUS_FCF_TABLE_EMPTY) {
1565 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1566 "2726 READ_FCF_RECORD Indicates empty "
1567 "FCF table.\n");
1568 } else {
1569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1570 "2521 READ_FCF_RECORD mailbox failed "
1571 "with status x%x add_status x%x, mbx\n",
1572 shdr_status, shdr_add_status);
1573 }
James Smart6fb120a2009-05-22 14:52:59 -04001574 goto out;
1575 }
1576 /* Interpreting the returned information of FCF records */
1577 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1578 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1579 sizeof(struct lpfc_mbx_read_fcf_tbl));
1580 next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1581
1582 new_fcf_record = (struct fcf_record *)(virt_addr +
1583 sizeof(struct lpfc_mbx_read_fcf_tbl));
1584 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1585 sizeof(struct fcf_record));
1586 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1587
1588 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record,
1589 &boot_flag, &addr_mode,
1590 &vlan_id);
1591 /*
1592 * If the fcf record does not match with connect list entries
1593 * read the next entry.
1594 */
1595 if (!rc)
1596 goto read_next_fcf;
1597 /*
1598 * If this is not the first FCF discovery of the HBA, use last
1599 * FCF record for the discovery.
1600 */
1601 spin_lock_irqsave(&phba->hbalock, flags);
1602 if (phba->fcf.fcf_flag & FCF_IN_USE) {
1603 if (lpfc_fab_name_match(phba->fcf.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001604 new_fcf_record) &&
1605 lpfc_sw_name_match(phba->fcf.switch_name,
1606 new_fcf_record) &&
James Smart6fb120a2009-05-22 14:52:59 -04001607 lpfc_mac_addr_match(phba, new_fcf_record)) {
1608 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1609 spin_unlock_irqrestore(&phba->hbalock, flags);
1610 goto out;
1611 }
1612 spin_unlock_irqrestore(&phba->hbalock, flags);
1613 goto read_next_fcf;
1614 }
1615 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1616 /*
1617 * If the current FCF record does not have boot flag
1618 * set and new fcf record has boot flag set, use the
1619 * new fcf record.
1620 */
1621 if (boot_flag && !(phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1622 /* Use this FCF record */
1623 lpfc_copy_fcf_record(phba, new_fcf_record);
1624 phba->fcf.addr_mode = addr_mode;
1625 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1626 if (vlan_id != 0xFFFF) {
1627 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1628 phba->fcf.vlan_id = vlan_id;
1629 }
1630 spin_unlock_irqrestore(&phba->hbalock, flags);
1631 goto read_next_fcf;
1632 }
1633 /*
1634 * If the current FCF record has boot flag set and the
1635 * new FCF record does not have boot flag, read the next
1636 * FCF record.
1637 */
1638 if (!boot_flag && (phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1639 spin_unlock_irqrestore(&phba->hbalock, flags);
1640 goto read_next_fcf;
1641 }
1642 /*
1643 * If there is a record with lower priority value for
1644 * the current FCF, use that record.
1645 */
James Smart8fa38512009-07-19 10:01:03 -04001646 if (lpfc_fab_name_match(phba->fcf.fabric_name,
1647 new_fcf_record) &&
1648 (new_fcf_record->fip_priority < phba->fcf.priority)) {
James Smart6fb120a2009-05-22 14:52:59 -04001649 /* Use this FCF record */
1650 lpfc_copy_fcf_record(phba, new_fcf_record);
1651 phba->fcf.addr_mode = addr_mode;
1652 if (vlan_id != 0xFFFF) {
1653 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1654 phba->fcf.vlan_id = vlan_id;
1655 }
1656 spin_unlock_irqrestore(&phba->hbalock, flags);
1657 goto read_next_fcf;
1658 }
1659 spin_unlock_irqrestore(&phba->hbalock, flags);
1660 goto read_next_fcf;
1661 }
1662 /*
1663 * This is the first available FCF record, use this
1664 * record.
1665 */
1666 lpfc_copy_fcf_record(phba, new_fcf_record);
1667 phba->fcf.addr_mode = addr_mode;
1668 if (boot_flag)
1669 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1670 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1671 if (vlan_id != 0xFFFF) {
1672 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1673 phba->fcf.vlan_id = vlan_id;
1674 }
1675 spin_unlock_irqrestore(&phba->hbalock, flags);
1676 goto read_next_fcf;
1677
1678read_next_fcf:
1679 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1680 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0)
1681 lpfc_register_fcf(phba);
1682 else
1683 lpfc_sli4_read_fcf_record(phba, next_fcf_index);
1684 return;
1685
1686out:
1687 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1688 lpfc_register_fcf(phba);
1689
1690 return;
1691}
1692
1693/**
James Smart1c6834a2009-07-19 10:01:26 -04001694 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
1695 * @phba: pointer to lpfc hba data structure.
1696 * @mboxq: pointer to mailbox data structure.
1697 *
1698 * This function handles completion of init vpi mailbox command.
1699 */
James Smart695a8142010-01-26 23:08:03 -05001700void
James Smart1c6834a2009-07-19 10:01:26 -04001701lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1702{
1703 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05001704 struct lpfc_nodelist *ndlp;
James Smart1c6834a2009-07-19 10:01:26 -04001705 if (mboxq->u.mb.mbxStatus) {
1706 lpfc_printf_vlog(vport, KERN_ERR,
1707 LOG_MBOX,
1708 "2609 Init VPI mailbox failed 0x%x\n",
1709 mboxq->u.mb.mbxStatus);
1710 mempool_free(mboxq, phba->mbox_mem_pool);
1711 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1712 return;
1713 }
James Smart97957242009-12-21 17:03:15 -05001714 spin_lock_irq(&phba->hbalock);
James Smart1c6834a2009-07-19 10:01:26 -04001715 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart97957242009-12-21 17:03:15 -05001716 spin_unlock_irq(&phba->hbalock);
James Smart1c6834a2009-07-19 10:01:26 -04001717
James Smart695a8142010-01-26 23:08:03 -05001718 /* If this port is physical port or FDISC is done, do reg_vpi */
1719 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
1720 ndlp = lpfc_findnode_did(vport, Fabric_DID);
1721 if (!ndlp)
1722 lpfc_printf_vlog(vport, KERN_ERR,
1723 LOG_DISCOVERY,
1724 "2731 Cannot find fabric "
1725 "controller node\n");
1726 else
1727 lpfc_register_new_vport(phba, vport, ndlp);
1728 mempool_free(mboxq, phba->mbox_mem_pool);
1729 return;
1730 }
1731
James Smart1c6834a2009-07-19 10:01:26 -04001732 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1733 lpfc_initial_fdisc(vport);
1734 else {
1735 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04001736 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1737 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04001738 }
James Smart695a8142010-01-26 23:08:03 -05001739 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04001740 return;
1741}
1742
1743/**
James Smart6fb120a2009-05-22 14:52:59 -04001744 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
1745 * @phba: pointer to lpfc hba data structure.
1746 *
1747 * This function loops through the list of vports on the @phba and issues an
1748 * FDISC if possible.
1749 */
1750void
1751lpfc_start_fdiscs(struct lpfc_hba *phba)
1752{
1753 struct lpfc_vport **vports;
1754 int i;
James Smart1c6834a2009-07-19 10:01:26 -04001755 LPFC_MBOXQ_t *mboxq;
1756 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001757
1758 vports = lpfc_create_vport_work_array(phba);
1759 if (vports != NULL) {
1760 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1761 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1762 continue;
1763 /* There are no vpi for this vport */
1764 if (vports[i]->vpi > phba->max_vpi) {
1765 lpfc_vport_set_state(vports[i],
1766 FC_VPORT_FAILED);
1767 continue;
1768 }
1769 if (phba->fc_topology == TOPOLOGY_LOOP) {
1770 lpfc_vport_set_state(vports[i],
1771 FC_VPORT_LINKDOWN);
1772 continue;
1773 }
James Smart1c6834a2009-07-19 10:01:26 -04001774 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
1775 mboxq = mempool_alloc(phba->mbox_mem_pool,
1776 GFP_KERNEL);
1777 if (!mboxq) {
1778 lpfc_printf_vlog(vports[i], KERN_ERR,
1779 LOG_MBOX, "2607 Failed to allocate "
1780 "init_vpi mailbox\n");
1781 continue;
1782 }
1783 lpfc_init_vpi(phba, mboxq, vports[i]->vpi);
1784 mboxq->vport = vports[i];
1785 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
1786 rc = lpfc_sli_issue_mbox(phba, mboxq,
1787 MBX_NOWAIT);
1788 if (rc == MBX_NOT_FINISHED) {
1789 lpfc_printf_vlog(vports[i], KERN_ERR,
1790 LOG_MBOX, "2608 Failed to issue "
1791 "init_vpi mailbox\n");
1792 mempool_free(mboxq,
1793 phba->mbox_mem_pool);
1794 }
1795 continue;
1796 }
James Smart6fb120a2009-05-22 14:52:59 -04001797 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1798 lpfc_initial_fdisc(vports[i]);
1799 else {
1800 lpfc_vport_set_state(vports[i],
1801 FC_VPORT_NO_FABRIC_SUPP);
1802 lpfc_printf_vlog(vports[i], KERN_ERR,
1803 LOG_ELS,
1804 "0259 No NPIV "
1805 "Fabric support\n");
1806 }
1807 }
1808 }
1809 lpfc_destroy_vport_work_array(phba, vports);
1810}
1811
1812void
1813lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1814{
1815 struct lpfc_dmabuf *dmabuf = mboxq->context1;
1816 struct lpfc_vport *vport = mboxq->vport;
1817
1818 if (mboxq->u.mb.mbxStatus) {
1819 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1820 "2018 REG_VFI mbxStatus error x%x "
1821 "HBA state x%x\n",
1822 mboxq->u.mb.mbxStatus, vport->port_state);
1823 if (phba->fc_topology == TOPOLOGY_LOOP) {
1824 /* FLOGI failed, use loop map to make discovery list */
1825 lpfc_disc_list_loopmap(vport);
1826 /* Start discovery */
1827 lpfc_disc_start(vport);
1828 goto fail_free_mem;
1829 }
1830 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1831 goto fail_free_mem;
1832 }
James Smartc8685952009-11-18 15:39:16 -05001833 /* The VPI is implicitly registered when the VFI is registered */
1834 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05001835 vport->fc_flag |= FC_VFI_REGISTERED;
1836
1837 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart6fb120a2009-05-22 14:52:59 -04001838
1839 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1840 lpfc_start_fdiscs(phba);
1841 lpfc_do_scr_ns_plogi(phba, vport);
1842 }
1843
1844fail_free_mem:
1845 mempool_free(mboxq, phba->mbox_mem_pool);
1846 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1847 kfree(dmabuf);
1848 return;
1849}
1850
1851static void
James Smart2e0fef82007-06-17 19:56:36 -05001852lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001853{
James Smart6fb120a2009-05-22 14:52:59 -04001854 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001855 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05001856 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001857
1858
1859 /* Check for error */
1860 if (mb->mbxStatus) {
1861 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001862 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1863 "0319 READ_SPARAM mbxStatus error x%x "
1864 "hba state x%x>\n",
1865 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001866 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05001867 goto out;
1868 }
1869
James Smart2e0fef82007-06-17 19:56:36 -05001870 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05001871 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05001872 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05001873 u64_to_wwn(phba->cfg_soft_wwnn,
1874 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04001875 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05001876 u64_to_wwn(phba->cfg_soft_wwpn,
1877 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05001878 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1879 sizeof(vport->fc_nodename));
1880 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1881 sizeof(vport->fc_portname));
1882 if (vport->port_type == LPFC_PHYSICAL_PORT) {
1883 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1884 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1885 }
1886
dea31012005-04-17 16:05:31 -05001887 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1888 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001889 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001890 return;
1891
1892out:
1893 pmb->context1 = NULL;
1894 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1895 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05001896 lpfc_issue_clear_la(phba, vport);
1897 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001898 return;
1899}
1900
1901static void
James Smart92d7f7b2007-06-17 19:56:38 -05001902lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05001903{
James Smart92d7f7b2007-06-17 19:56:38 -05001904 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04001905 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05001906 int i;
James Smart14691152006-12-02 13:34:28 -05001907 struct lpfc_dmabuf *mp;
1908 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001909 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05001910
dea31012005-04-17 16:05:31 -05001911 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001912
James Smart92d7f7b2007-06-17 19:56:38 -05001913 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001914 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05001915 case LA_1GHZ_LINK:
1916 phba->fc_linkspeed = LA_1GHZ_LINK;
1917 break;
1918 case LA_2GHZ_LINK:
1919 phba->fc_linkspeed = LA_2GHZ_LINK;
1920 break;
1921 case LA_4GHZ_LINK:
1922 phba->fc_linkspeed = LA_4GHZ_LINK;
1923 break;
1924 case LA_8GHZ_LINK:
1925 phba->fc_linkspeed = LA_8GHZ_LINK;
1926 break;
James Smartf4b4c682009-05-22 14:53:12 -04001927 case LA_10GHZ_LINK:
1928 phba->fc_linkspeed = LA_10GHZ_LINK;
1929 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001930 default:
1931 phba->fc_linkspeed = LA_UNKNW_LINK;
1932 break;
dea31012005-04-17 16:05:31 -05001933 }
1934
1935 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05001936 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05001937
1938 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05001939 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05001940
James Smart6a9c52c2009-10-02 15:16:51 -04001941 /* if npiv is enabled and this adapter supports npiv log
1942 * a message that npiv is not supported in this topology
1943 */
1944 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04001945 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1946 "1309 Link Up Event npiv not supported in loop "
1947 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001948 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05001949 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05001950 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001951
James Smart2e0fef82007-06-17 19:56:36 -05001952 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05001953 i = la->un.lilpBde64.tus.f.bdeSize;
1954
1955 if (i == 0) {
1956 phba->alpa_map[0] = 0;
1957 } else {
James Smarte8b62012007-08-02 11:10:09 -04001958 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05001959 int numalpa, j, k;
1960 union {
1961 uint8_t pamap[16];
1962 struct {
1963 uint32_t wd1;
1964 uint32_t wd2;
1965 uint32_t wd3;
1966 uint32_t wd4;
1967 } pa;
1968 } un;
1969 numalpa = phba->alpa_map[0];
1970 j = 0;
1971 while (j < numalpa) {
1972 memset(un.pamap, 0, 16);
1973 for (k = 1; j < numalpa; k++) {
1974 un.pamap[k - 1] =
1975 phba->alpa_map[j + 1];
1976 j++;
1977 if (k == 16)
1978 break;
1979 }
1980 /* Link Up Event ALPA map */
1981 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05001982 KERN_WARNING,
1983 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001984 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05001985 "ALPA map Data: x%x "
1986 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001987 un.pa.wd1, un.pa.wd2,
1988 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05001989 }
1990 }
1991 }
1992 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05001993 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04001994 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05001995 (phba->sli_rev == 3))
1996 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1997 }
James Smart2e0fef82007-06-17 19:56:36 -05001998 vport->fc_myDID = phba->fc_pref_DID;
1999 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002000 }
James Smart92d7f7b2007-06-17 19:56:38 -05002001 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002002
2003 lpfc_linkup(phba);
2004 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05002005 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05002006 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05002007 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04002008 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05002009 if (rc == MBX_NOT_FINISHED) {
2010 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2011 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2012 kfree(mp);
2013 mempool_free(sparam_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05002014 goto out;
James Smart14691152006-12-02 13:34:28 -05002015 }
dea31012005-04-17 16:05:31 -05002016 }
2017
James Smart6fb120a2009-05-22 14:52:59 -04002018 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2019 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2020 if (!cfglink_mbox)
2021 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05002022 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002023 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002024 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002025 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002026 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002027 if (rc == MBX_NOT_FINISHED) {
2028 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2029 goto out;
2030 }
2031 } else {
James Smart32b97932009-07-19 10:01:21 -04002032 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002033 /*
2034 * Add the driver's default FCF record at FCF index 0 now. This
2035 * is phase 1 implementation that support FCF index 0 and driver
2036 * defaults.
2037 */
James Smart45ed1192009-10-02 15:17:02 -04002038 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002039 fcf_record = kzalloc(sizeof(struct fcf_record),
2040 GFP_KERNEL);
2041 if (unlikely(!fcf_record)) {
2042 lpfc_printf_log(phba, KERN_ERR,
2043 LOG_MBOX | LOG_SLI,
2044 "2554 Could not allocate memmory for "
2045 "fcf record\n");
2046 rc = -ENODEV;
2047 goto out;
2048 }
2049
2050 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2051 LPFC_FCOE_FCF_DEF_INDEX);
2052 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2053 if (unlikely(rc)) {
2054 lpfc_printf_log(phba, KERN_ERR,
2055 LOG_MBOX | LOG_SLI,
2056 "2013 Could not manually add FCF "
2057 "record 0, status %d\n", rc);
2058 rc = -ENODEV;
2059 kfree(fcf_record);
2060 goto out;
2061 }
2062 kfree(fcf_record);
2063 }
2064 /*
2065 * The driver is expected to do FIP/FCF. Call the port
2066 * and get the FCF Table.
2067 */
James Smart32b97932009-07-19 10:01:21 -04002068 spin_lock_irq(&phba->hbalock);
2069 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2070 spin_unlock_irq(&phba->hbalock);
2071 return;
2072 }
2073 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002074 rc = lpfc_sli4_read_fcf_record(phba,
2075 LPFC_FCOE_FCF_GET_FIRST);
2076 if (rc)
2077 goto out;
dea31012005-04-17 16:05:31 -05002078 }
James Smart6fb120a2009-05-22 14:52:59 -04002079
2080 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002081out:
2082 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002083 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2084 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2085 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002086 lpfc_issue_clear_la(phba, vport);
2087 return;
dea31012005-04-17 16:05:31 -05002088}
2089
2090static void
James Smart84774a42008-08-24 21:50:06 -04002091lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002092{
dea31012005-04-17 16:05:31 -05002093 uint32_t control;
2094 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002095 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002096 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002097 if (phba->sli_rev <= LPFC_SLI_REV3) {
2098 control = readl(phba->HCregaddr);
2099 control |= HC_LAINT_ENA;
2100 writel(control, phba->HCregaddr);
2101 readl(phba->HCregaddr); /* flush */
2102 }
James Smart2e0fef82007-06-17 19:56:36 -05002103 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002104}
2105
James Smart84774a42008-08-24 21:50:06 -04002106static void
2107lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2108{
2109 lpfc_linkdown(phba);
2110 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002111 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002112 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2113}
2114
2115
dea31012005-04-17 16:05:31 -05002116/*
2117 * This routine handles processing a READ_LA mailbox
2118 * command upon completion. It is setup in the LPFC_MBOXQ
2119 * as the completion routine when the command is
2120 * handed off to the SLI layer.
2121 */
2122void
James Smart2e0fef82007-06-17 19:56:36 -05002123lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002124{
James Smart2e0fef82007-06-17 19:56:36 -05002125 struct lpfc_vport *vport = pmb->vport;
2126 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002127 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002128 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002129 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2130
James Smart0d2b6b82008-06-14 22:52:47 -04002131 /* Unblock ELS traffic */
2132 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002133 /* Check for error */
2134 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002135 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002136 "1307 READ_LA mbox error x%x state x%x\n",
2137 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002138 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002139 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002140 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2141 }
2142
James Smart04c68492009-05-22 14:52:52 -04002143 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002144
2145 memcpy(&phba->alpa_map[0], mp->virt, 128);
2146
James Smart2e0fef82007-06-17 19:56:36 -05002147 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002148 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002149 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002150 else
James Smart2e0fef82007-06-17 19:56:36 -05002151 vport->fc_flag &= ~FC_BYPASSED_MODE;
2152 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002153
James Smart0c287582009-06-10 17:22:56 -04002154 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002155 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002156 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002157 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002158 if (phba->fc_eventTag != 0)
2159 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002160 }
dea31012005-04-17 16:05:31 -05002161
2162 phba->fc_eventTag = la->eventTag;
James Smart84774a42008-08-24 21:50:06 -04002163 if (la->mm)
2164 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2165 else
2166 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
dea31012005-04-17 16:05:31 -05002167
James Smart4d9ab992009-10-02 15:16:39 -04002168 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002169 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002170 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002171 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002172 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002173 "1306 Link Up Event in loop back mode "
2174 "x%x received Data: x%x x%x x%x x%x\n",
2175 la->eventTag, phba->fc_eventTag,
2176 la->granted_AL_PA, la->UlnkSpeed,
2177 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002178 } else {
2179 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002180 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002181 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002182 la->eventTag, phba->fc_eventTag,
2183 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002184 phba->alpa_map[0],
2185 la->mm, la->fa,
2186 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002187 }
James Smart92d7f7b2007-06-17 19:56:38 -05002188 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002189 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002190 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002191 if (phba->link_flag & LS_LOOPBACK_MODE) {
2192 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2193 "1308 Link Down Event in loop back mode "
2194 "x%x received "
2195 "Data: x%x x%x x%x\n",
2196 la->eventTag, phba->fc_eventTag,
2197 phba->pport->port_state, vport->fc_flag);
2198 }
2199 else {
2200 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002201 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002202 "Data: x%x x%x x%x x%x x%x\n",
2203 la->eventTag, phba->fc_eventTag,
2204 phba->pport->port_state, vport->fc_flag,
2205 la->mm, la->fa);
2206 }
2207 lpfc_mbx_issue_link_down(phba);
2208 }
2209 if (la->mm && la->attType == AT_LINK_UP) {
2210 if (phba->link_state != LPFC_LINK_DOWN) {
2211 phba->fc_stat.LinkDown++;
2212 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2213 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002214 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002215 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002216 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002217 lpfc_mbx_issue_link_down(phba);
2218 } else
2219 lpfc_enable_la(phba);
2220
2221 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2222 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2223 "Data: x%x x%x x%x\n",
2224 la->eventTag, phba->fc_eventTag,
2225 phba->pport->port_state, vport->fc_flag);
2226 /*
2227 * The cmnd that triggered this will be waiting for this
2228 * signal.
2229 */
2230 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2231 if (phba->wait_4_mlo_maint_flg) {
2232 phba->wait_4_mlo_maint_flg = 0;
2233 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002234 }
James Smart84774a42008-08-24 21:50:06 -04002235 }
2236
2237 if (la->fa) {
2238 if (la->mm)
2239 lpfc_issue_clear_la(phba, vport);
2240 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2241 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002242 }
2243
2244lpfc_mbx_cmpl_read_la_free_mbuf:
2245 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2246 kfree(mp);
2247 mempool_free(pmb, phba->mbox_mem_pool);
2248 return;
2249}
2250
2251/*
2252 * This routine handles processing a REG_LOGIN mailbox
2253 * command upon completion. It is setup in the LPFC_MBOXQ
2254 * as the completion routine when the command is
2255 * handed off to the SLI layer.
2256 */
2257void
James Smart2e0fef82007-06-17 19:56:36 -05002258lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002259{
James Smart2e0fef82007-06-17 19:56:36 -05002260 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002261 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002262 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05002263
dea31012005-04-17 16:05:31 -05002264 pmb->context1 = NULL;
2265
2266 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05002267 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05002268 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2269 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002270 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002271 /* decrement the node reference count held for this callback
2272 * function.
2273 */
James Smart329f9bc2007-04-25 09:53:01 -04002274 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002275
2276 return;
2277}
2278
James Smart92d7f7b2007-06-17 19:56:38 -05002279static void
2280lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2281{
James Smart04c68492009-05-22 14:52:52 -04002282 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002283 struct lpfc_vport *vport = pmb->vport;
2284 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2285
2286 switch (mb->mbxStatus) {
2287 case 0x0011:
2288 case 0x0020:
2289 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04002290 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2291 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2292 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002293 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002294 }
James Smart97957242009-12-21 17:03:15 -05002295 spin_lock_irq(&phba->hbalock);
James Smartc8685952009-11-18 15:39:16 -05002296 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05002297 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart97957242009-12-21 17:03:15 -05002298 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002299 vport->unreg_vpi_cmpl = VPORT_OK;
2300 mempool_free(pmb, phba->mbox_mem_pool);
2301 /*
2302 * This shost reference might have been taken at the beginning of
2303 * lpfc_vport_delete()
2304 */
James Smart1c6f4ef52009-11-18 15:40:49 -05002305 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05002306 scsi_host_put(shost);
2307}
2308
James Smartd7c255b2008-08-24 21:50:00 -04002309int
James Smart92d7f7b2007-06-17 19:56:38 -05002310lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2311{
2312 struct lpfc_hba *phba = vport->phba;
2313 LPFC_MBOXQ_t *mbox;
2314 int rc;
2315
2316 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2317 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002318 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002319
2320 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2321 mbox->vport = vport;
2322 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002323 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002324 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002325 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2326 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002327 mempool_free(mbox, phba->mbox_mem_pool);
2328 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002329 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002330 }
James Smartd7c255b2008-08-24 21:50:00 -04002331 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002332}
2333
2334static void
2335lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2336{
2337 struct lpfc_vport *vport = pmb->vport;
2338 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002339 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002340
2341 switch (mb->mbxStatus) {
2342 case 0x0011:
2343 case 0x9601:
2344 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002345 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2346 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2347 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002348 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2349 spin_lock_irq(shost->host_lock);
2350 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2351 spin_unlock_irq(shost->host_lock);
2352 vport->fc_myDID = 0;
2353 goto out;
2354 }
James Smart92d7f7b2007-06-17 19:56:38 -05002355
James Smartc8685952009-11-18 15:39:16 -05002356 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002357 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart92d7f7b2007-06-17 19:56:38 -05002358 vport->num_disc_nodes = 0;
2359 /* go thru NPR list and issue ELS PLOGIs */
2360 if (vport->fc_npr_cnt)
2361 lpfc_els_disc_plogi(vport);
2362
2363 if (!vport->num_disc_nodes) {
2364 spin_lock_irq(shost->host_lock);
2365 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2366 spin_unlock_irq(shost->host_lock);
2367 lpfc_can_disctmo(vport);
2368 }
2369 vport->port_state = LPFC_VPORT_READY;
2370
2371out:
2372 mempool_free(pmb, phba->mbox_mem_pool);
2373 return;
2374}
2375
James Smart21e9a0a2009-05-22 14:53:21 -04002376/**
2377 * lpfc_create_static_vport - Read HBA config region to create static vports.
2378 * @phba: pointer to lpfc hba data structure.
2379 *
2380 * This routine issue a DUMP mailbox command for config region 22 to get
2381 * the list of static vports to be created. The function create vports
2382 * based on the information returned from the HBA.
2383 **/
2384void
2385lpfc_create_static_vport(struct lpfc_hba *phba)
2386{
2387 LPFC_MBOXQ_t *pmb = NULL;
2388 MAILBOX_t *mb;
2389 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04002390 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04002391 struct fc_vport_identifiers vport_id;
2392 struct fc_vport *new_fc_vport;
2393 struct Scsi_Host *shost;
2394 struct lpfc_vport *vport;
2395 uint16_t offset = 0;
2396 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04002397 struct lpfc_dmabuf *mp;
2398 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04002399
2400 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2401 if (!pmb) {
2402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2403 "0542 lpfc_create_static_vport failed to"
2404 " allocate mailbox memory\n");
2405 return;
2406 }
2407
2408 mb = &pmb->u.mb;
2409
2410 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2411 if (!vport_info) {
2412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2413 "0543 lpfc_create_static_vport failed to"
2414 " allocate vport_info\n");
2415 mempool_free(pmb, phba->mbox_mem_pool);
2416 return;
2417 }
2418
2419 vport_buff = (uint8_t *) vport_info;
2420 do {
James Smart1c6834a2009-07-19 10:01:26 -04002421 if (lpfc_dump_static_vport(phba, pmb, offset))
2422 goto out;
2423
James Smart21e9a0a2009-05-22 14:53:21 -04002424 pmb->vport = phba->pport;
2425 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2426
2427 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2428 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2429 "0544 lpfc_create_static_vport failed to"
2430 " issue dump mailbox command ret 0x%x "
2431 "status 0x%x\n",
2432 rc, mb->mbxStatus);
2433 goto out;
2434 }
2435
James Smart1c6834a2009-07-19 10:01:26 -04002436 if (phba->sli_rev == LPFC_SLI_REV4) {
2437 byte_count = pmb->u.mqe.un.mb_words[5];
2438 mp = (struct lpfc_dmabuf *) pmb->context2;
2439 if (byte_count > sizeof(struct static_vport_info) -
2440 offset)
2441 byte_count = sizeof(struct static_vport_info)
2442 - offset;
2443 memcpy(vport_buff + offset, mp->virt, byte_count);
2444 offset += byte_count;
2445 } else {
2446 if (mb->un.varDmp.word_cnt >
2447 sizeof(struct static_vport_info) - offset)
2448 mb->un.varDmp.word_cnt =
2449 sizeof(struct static_vport_info)
2450 - offset;
2451 byte_count = mb->un.varDmp.word_cnt;
2452 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2453 vport_buff + offset,
2454 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04002455
James Smart1c6834a2009-07-19 10:01:26 -04002456 offset += byte_count;
2457 }
James Smart21e9a0a2009-05-22 14:53:21 -04002458
James Smart1c6834a2009-07-19 10:01:26 -04002459 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04002460 offset < sizeof(struct static_vport_info));
2461
2462
2463 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2464 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2465 != VPORT_INFO_REV)) {
2466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2467 "0545 lpfc_create_static_vport bad"
2468 " information header 0x%x 0x%x\n",
2469 le32_to_cpu(vport_info->signature),
2470 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2471
2472 goto out;
2473 }
2474
2475 shost = lpfc_shost_from_vport(phba->pport);
2476
2477 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2478 memset(&vport_id, 0, sizeof(vport_id));
2479 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2480 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2481 if (!vport_id.port_name || !vport_id.node_name)
2482 continue;
2483
2484 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2485 vport_id.vport_type = FC_PORTTYPE_NPIV;
2486 vport_id.disable = false;
2487 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2488
2489 if (!new_fc_vport) {
2490 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2491 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04002492 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04002493 continue;
2494 }
2495
2496 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2497 vport->vport_flag |= STATIC_VPORT;
2498 }
2499
2500out:
James Smart21e9a0a2009-05-22 14:53:21 -04002501 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04002502 if (rc != MBX_TIMEOUT) {
2503 if (pmb->context2) {
2504 mp = (struct lpfc_dmabuf *) pmb->context2;
2505 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2506 kfree(mp);
2507 }
James Smart21e9a0a2009-05-22 14:53:21 -04002508 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002509 }
James Smart21e9a0a2009-05-22 14:53:21 -04002510
2511 return;
2512}
2513
dea31012005-04-17 16:05:31 -05002514/*
2515 * This routine handles processing a Fabric REG_LOGIN mailbox
2516 * command upon completion. It is setup in the LPFC_MBOXQ
2517 * as the completion routine when the command is
2518 * handed off to the SLI layer.
2519 */
2520void
James Smart2e0fef82007-06-17 19:56:36 -05002521lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002522{
James Smart92d7f7b2007-06-17 19:56:38 -05002523 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04002524 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002525 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05002526 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002527
James Smart549e55c2007-08-02 11:09:51 -04002528 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04002529 pmb->context1 = NULL;
2530 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05002531 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04002532 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2533 "0258 Register Fabric login error: 0x%x\n",
2534 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05002535 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2536 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002537 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002538
James Smart92d7f7b2007-06-17 19:56:38 -05002539 if (phba->fc_topology == TOPOLOGY_LOOP) {
2540 /* FLOGI failed, use loop map to make discovery list */
2541 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002542
James Smart92d7f7b2007-06-17 19:56:38 -05002543 /* Start discovery */
2544 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05002545 /* Decrement the reference count to ndlp after the
2546 * reference to the ndlp are done.
2547 */
2548 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05002549 return;
2550 }
2551
2552 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05002553 /* Decrement the reference count to ndlp after the reference
2554 * to the ndlp are done.
2555 */
2556 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002557 return;
2558 }
2559
dea31012005-04-17 16:05:31 -05002560 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04002561 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05002562 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002563 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002564
James Smart2e0fef82007-06-17 19:56:36 -05002565 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart21e9a0a2009-05-22 14:53:21 -04002566 lpfc_start_fdiscs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002567 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05002568 }
2569
2570 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2571 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002572 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002573
2574 /* Drop the reference count from the mbox at the end after
2575 * all the current reference to the ndlp have been done.
2576 */
2577 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002578 return;
2579}
2580
2581/*
2582 * This routine handles processing a NameServer REG_LOGIN mailbox
2583 * command upon completion. It is setup in the LPFC_MBOXQ
2584 * as the completion routine when the command is
2585 * handed off to the SLI layer.
2586 */
2587void
James Smart2e0fef82007-06-17 19:56:36 -05002588lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002589{
James Smart21e9a0a2009-05-22 14:53:21 -04002590 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002591 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2592 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2593 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002594
2595 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05002596out:
James Smart21e9a0a2009-05-22 14:53:21 -04002597 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2598 "0260 Register NameServer error: 0x%x\n",
2599 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05002600 /* decrement the node reference count held for this
2601 * callback function.
2602 */
James Smart329f9bc2007-04-25 09:53:01 -04002603 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002604 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2605 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04002606 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04002607
2608 /* If no other thread is using the ndlp, free it */
2609 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05002610
James Smart92d7f7b2007-06-17 19:56:38 -05002611 if (phba->fc_topology == TOPOLOGY_LOOP) {
2612 /*
2613 * RegLogin failed, use loop map to make discovery
2614 * list
2615 */
2616 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002617
James Smart92d7f7b2007-06-17 19:56:38 -05002618 /* Start discovery */
2619 lpfc_disc_start(vport);
2620 return;
2621 }
2622 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05002623 return;
2624 }
2625
2626 pmb->context1 = NULL;
2627
dea31012005-04-17 16:05:31 -05002628 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04002629 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05002630 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002631 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002632
James Smart2e0fef82007-06-17 19:56:36 -05002633 if (vport->port_state < LPFC_VPORT_READY) {
2634 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05002635 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
2636 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
2637 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2638 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
2639 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
2640
2641 /* Issue SCR just before NameServer GID_FT Query */
2642 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05002643 }
2644
James Smart2e0fef82007-06-17 19:56:36 -05002645 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002646 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05002647 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05002648 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05002649 goto out;
dea31012005-04-17 16:05:31 -05002650 }
2651
James Smartfa4066b2008-01-11 01:53:27 -05002652 /* decrement the node reference count held for this
2653 * callback function.
2654 */
James Smart329f9bc2007-04-25 09:53:01 -04002655 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002656 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2657 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002658 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002659
2660 return;
2661}
2662
2663static void
James Smart2e0fef82007-06-17 19:56:36 -05002664lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002665{
James Smart2e0fef82007-06-17 19:56:36 -05002666 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2667 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05002668 struct lpfc_rport_data *rdata;
2669 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05002670 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002671
2672 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07002673 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2674 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05002675 rport_ids.port_id = ndlp->nlp_DID;
2676 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05002677
James Smart329f9bc2007-04-25 09:53:01 -04002678 /*
2679 * We leave our node pointer in rport->dd_data when we unregister a
2680 * FCP target port. But fc_remote_port_add zeros the space to which
2681 * rport->dd_data points. So, if we're reusing a previously
2682 * registered port, drop the reference that we took the last time we
2683 * registered the port.
2684 */
2685 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05002686 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04002687 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05002688
2689 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
2690 "rport add: did:x%x flg:x%x type x%x",
2691 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2692
James Smart2e0fef82007-06-17 19:56:36 -05002693 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04002694 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05002695 dev_printk(KERN_WARNING, &phba->pcidev->dev,
2696 "Warning: fc_remote_port_add failed\n");
2697 return;
2698 }
2699
2700 /* initialize static port data */
2701 rport->maxframe_size = ndlp->nlp_maxframe;
2702 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05002703 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04002704 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05002705
2706 if (ndlp->nlp_type & NLP_FCP_TARGET)
2707 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2708 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
2709 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2710
2711
2712 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
2713 fc_remote_port_rolechg(rport, rport_ids.roles);
2714
James Smart071fbd3d2006-04-15 11:53:20 -04002715 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002716 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04002717 ndlp->nlp_sid = rport->scsi_target_id;
2718 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002719 return;
2720}
2721
2722static void
James Smart2e0fef82007-06-17 19:56:36 -05002723lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002724{
2725 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04002726
James Smart858c9f62007-06-17 19:56:39 -05002727 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
2728 "rport delete: did:x%x flg:x%x type x%x",
2729 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2730
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002731 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05002732
2733 return;
2734}
2735
James Smartde0c5b32007-04-25 09:52:27 -04002736static void
James Smart2e0fef82007-06-17 19:56:36 -05002737lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04002738{
James Smart2e0fef82007-06-17 19:56:36 -05002739 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2740
2741 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002742 switch (state) {
2743 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002744 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002745 break;
2746 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002747 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002748 break;
2749 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002750 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002751 break;
2752 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002753 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002754 break;
2755 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002756 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002757 break;
2758 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002759 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002760 break;
2761 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002762 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002763 break;
2764 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002765 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002766 break;
2767 }
James Smart2e0fef82007-06-17 19:56:36 -05002768 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002769}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002770
James Smartde0c5b32007-04-25 09:52:27 -04002771static void
James Smart2e0fef82007-06-17 19:56:36 -05002772lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04002773 int old_state, int new_state)
2774{
James Smart2e0fef82007-06-17 19:56:36 -05002775 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2776
James Smartde0c5b32007-04-25 09:52:27 -04002777 if (new_state == NLP_STE_UNMAPPED_NODE) {
2778 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2779 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2780 ndlp->nlp_type |= NLP_FC_NODE;
2781 }
2782 if (new_state == NLP_STE_MAPPED_NODE)
2783 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2784 if (new_state == NLP_STE_NPR_NODE)
2785 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05002786
James Smartde0c5b32007-04-25 09:52:27 -04002787 /* Transport interface */
2788 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
2789 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002790 vport->phba->nport_event_cnt++;
2791 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002792 }
dea31012005-04-17 16:05:31 -05002793
James Smartde0c5b32007-04-25 09:52:27 -04002794 if (new_state == NLP_STE_MAPPED_NODE ||
2795 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05002796 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05002797 /*
2798 * Tell the fc transport about the port, if we haven't
2799 * already. If we have, and it's a scsi entity, be
2800 * sure to unblock any attached scsi devices
2801 */
2802 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002803 }
James Smartea2151b2008-09-07 11:52:10 -04002804 if ((new_state == NLP_STE_MAPPED_NODE) &&
2805 (vport->stat_data_enabled)) {
2806 /*
2807 * A new target is discovered, if there is no buffer for
2808 * statistical data collection allocate buffer.
2809 */
2810 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
2811 sizeof(struct lpfc_scsicmd_bkt),
2812 GFP_KERNEL);
2813
2814 if (!ndlp->lat_data)
2815 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2816 "0286 lpfc_nlp_state_cleanup failed to "
2817 "allocate statistical data buffer DID "
2818 "0x%x\n", ndlp->nlp_DID);
2819 }
James Smart858c9f62007-06-17 19:56:39 -05002820 /*
2821 * if we added to Mapped list, but the remote port
2822 * registration failed or assigned a target id outside
2823 * our presentable range - move the node to the
2824 * Unmapped List
2825 */
James Smartde0c5b32007-04-25 09:52:27 -04002826 if (new_state == NLP_STE_MAPPED_NODE &&
2827 (!ndlp->rport ||
2828 ndlp->rport->scsi_target_id == -1 ||
2829 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05002830 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002831 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05002832 spin_unlock_irq(shost->host_lock);
2833 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002834 }
James Smartde0c5b32007-04-25 09:52:27 -04002835}
2836
James Smart685f0bf2007-04-25 09:53:08 -04002837static char *
2838lpfc_nlp_state_name(char *buffer, size_t size, int state)
2839{
2840 static char *states[] = {
2841 [NLP_STE_UNUSED_NODE] = "UNUSED",
2842 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
2843 [NLP_STE_ADISC_ISSUE] = "ADISC",
2844 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
2845 [NLP_STE_PRLI_ISSUE] = "PRLI",
2846 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
2847 [NLP_STE_MAPPED_NODE] = "MAPPED",
2848 [NLP_STE_NPR_NODE] = "NPR",
2849 };
2850
James Smart311464e2007-08-02 11:10:37 -04002851 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04002852 strlcpy(buffer, states[state], size);
2853 else
2854 snprintf(buffer, size, "unknown (%d)", state);
2855 return buffer;
2856}
2857
James Smartde0c5b32007-04-25 09:52:27 -04002858void
James Smart2e0fef82007-06-17 19:56:36 -05002859lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2860 int state)
James Smartde0c5b32007-04-25 09:52:27 -04002861{
James Smart2e0fef82007-06-17 19:56:36 -05002862 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04002863 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04002864 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04002865
James Smarte8b62012007-08-02 11:10:09 -04002866 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2867 "0904 NPort state transition x%06x, %s -> %s\n",
2868 ndlp->nlp_DID,
2869 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
2870 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05002871
2872 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2873 "node statechg did:x%x old:%d ste:%d",
2874 ndlp->nlp_DID, old_state, state);
2875
James Smartde0c5b32007-04-25 09:52:27 -04002876 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04002877 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05002878 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002879 if (old_state == NLP_STE_UNMAPPED_NODE) {
2880 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
2881 ndlp->nlp_type &= ~NLP_FC_NODE;
2882 }
2883
James Smart685f0bf2007-04-25 09:53:08 -04002884 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05002885 spin_lock_irq(shost->host_lock);
2886 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2887 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002888 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05002889 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04002890
2891 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05002892 lpfc_nlp_counters(vport, state, 1);
2893 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04002894}
2895
2896void
James Smarte47c9092008-02-08 18:49:26 -05002897lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2898{
2899 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2900
2901 if (list_empty(&ndlp->nlp_listp)) {
2902 spin_lock_irq(shost->host_lock);
2903 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2904 spin_unlock_irq(shost->host_lock);
2905 }
2906}
2907
2908void
James Smart2e0fef82007-06-17 19:56:36 -05002909lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04002910{
James Smart2e0fef82007-06-17 19:56:36 -05002911 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2912
James Smart0d2b6b82008-06-14 22:52:47 -04002913 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002914 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05002915 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2916 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002917 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05002918 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002919 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05002920 NLP_STE_UNUSED_NODE);
2921}
2922
Adrian Bunk4d9db012008-02-14 23:24:02 +02002923static void
James Smarte47c9092008-02-08 18:49:26 -05002924lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2925{
James Smart0d2b6b82008-06-14 22:52:47 -04002926 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05002927 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2928 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2929 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2930 NLP_STE_UNUSED_NODE);
2931}
James Smart109f6ed2008-12-04 22:39:08 -05002932/**
James Smart3621a712009-04-06 18:47:14 -04002933 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05002934 * @vport: Pointer to Virtual Port object.
2935 * @ndlp: Pointer to FC node object.
2936 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04002937 *
2938 * This function is always called when node object need to be initialized.
2939 * It initializes all the fields of the node object. Although the reference
2940 * to phba from @ndlp can be obtained indirectly through it's reference to
2941 * @vport, a direct reference to phba is taken here by @ndlp. This is due
2942 * to the life-span of the @ndlp might go beyond the existence of @vport as
2943 * the final release of ndlp is determined by its reference count. And, the
2944 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05002945 **/
2946static inline void
2947lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2948 uint32_t did)
2949{
2950 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2951 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2952 init_timer(&ndlp->nlp_delayfunc);
2953 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2954 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2955 ndlp->nlp_DID = did;
2956 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04002957 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05002958 ndlp->nlp_sid = NLP_NO_SID;
2959 kref_init(&ndlp->kref);
2960 NLP_INT_NODE_ACT(ndlp);
2961 atomic_set(&ndlp->cmd_pending, 0);
2962 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2963}
James Smarte47c9092008-02-08 18:49:26 -05002964
2965struct lpfc_nodelist *
2966lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2967 int state)
2968{
2969 struct lpfc_hba *phba = vport->phba;
2970 uint32_t did;
2971 unsigned long flags;
2972
2973 if (!ndlp)
2974 return NULL;
2975
2976 spin_lock_irqsave(&phba->ndlp_lock, flags);
2977 /* The ndlp should not be in memory free mode */
2978 if (NLP_CHK_FREE_REQ(ndlp)) {
2979 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2980 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2981 "0277 lpfc_enable_node: ndlp:x%p "
2982 "usgmap:x%x refcnt:%d\n",
2983 (void *)ndlp, ndlp->nlp_usg_map,
2984 atomic_read(&ndlp->kref.refcount));
2985 return NULL;
2986 }
2987 /* The ndlp should not already be in active mode */
2988 if (NLP_CHK_NODE_ACT(ndlp)) {
2989 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2990 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2991 "0278 lpfc_enable_node: ndlp:x%p "
2992 "usgmap:x%x refcnt:%d\n",
2993 (void *)ndlp, ndlp->nlp_usg_map,
2994 atomic_read(&ndlp->kref.refcount));
2995 return NULL;
2996 }
2997
2998 /* Keep the original DID */
2999 did = ndlp->nlp_DID;
3000
3001 /* re-initialize ndlp except of ndlp linked list pointer */
3002 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3003 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003004 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003005
3006 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3007
3008 if (state != NLP_STE_UNUSED_NODE)
3009 lpfc_nlp_set_state(vport, ndlp, state);
3010
3011 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3012 "node enable: did:x%x",
3013 ndlp->nlp_DID, 0, 0);
3014 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003015}
3016
3017void
James Smart2e0fef82007-06-17 19:56:36 -05003018lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003019{
James Smart87af33f2007-10-27 13:37:43 -04003020 /*
James Smartfa4066b2008-01-11 01:53:27 -05003021 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003022 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003023 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3024 * until ALL other outstanding threads have completed. We check
3025 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003026 */
James Smartfa4066b2008-01-11 01:53:27 -05003027 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3028 return;
James Smart51ef4c22007-08-02 11:10:31 -04003029 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003030 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003031 return;
dea31012005-04-17 16:05:31 -05003032}
3033
3034/*
3035 * Start / ReStart rescue timer for Discovery / RSCN handling
3036 */
3037void
James Smart2e0fef82007-06-17 19:56:36 -05003038lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003039{
James Smart2e0fef82007-06-17 19:56:36 -05003040 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3041 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003042 uint32_t tmo;
3043
James Smart2e0fef82007-06-17 19:56:36 -05003044 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003045 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003046 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3047 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003048 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003049 * FC spec states we need 3 * ratov for CT requests
3050 */
3051 tmo = ((phba->fc_ratov * 3) + 3);
3052 }
dea31012005-04-17 16:05:31 -05003053
James Smart858c9f62007-06-17 19:56:39 -05003054
3055 if (!timer_pending(&vport->fc_disctmo)) {
3056 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3057 "set disc timer: tmo:x%x state:x%x flg:x%x",
3058 tmo, vport->port_state, vport->fc_flag);
3059 }
3060
James Smart2e0fef82007-06-17 19:56:36 -05003061 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3062 spin_lock_irq(shost->host_lock);
3063 vport->fc_flag |= FC_DISC_TMO;
3064 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003065
3066 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003067 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3068 "0247 Start Discovery Timer state x%x "
3069 "Data: x%x x%lx x%x x%x\n",
3070 vport->port_state, tmo,
3071 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3072 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003073
3074 return;
3075}
3076
3077/*
3078 * Cancel rescue timer for Discovery / RSCN handling
3079 */
3080int
James Smart2e0fef82007-06-17 19:56:36 -05003081lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003082{
James Smart2e0fef82007-06-17 19:56:36 -05003083 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003084 unsigned long iflags;
3085
James Smart858c9f62007-06-17 19:56:39 -05003086 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3087 "can disc timer: state:x%x rtry:x%x flg:x%x",
3088 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3089
dea31012005-04-17 16:05:31 -05003090 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003091 if (vport->fc_flag & FC_DISC_TMO) {
3092 spin_lock_irqsave(shost->host_lock, iflags);
3093 vport->fc_flag &= ~FC_DISC_TMO;
3094 spin_unlock_irqrestore(shost->host_lock, iflags);
3095 del_timer_sync(&vport->fc_disctmo);
3096 spin_lock_irqsave(&vport->work_port_lock, iflags);
3097 vport->work_port_events &= ~WORKER_DISC_TMO;
3098 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003099 }
3100
3101 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003102 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3103 "0248 Cancel Discovery Timer state x%x "
3104 "Data: x%x x%x x%x\n",
3105 vport->port_state, vport->fc_flag,
3106 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003107 return 0;
dea31012005-04-17 16:05:31 -05003108}
3109
3110/*
3111 * Check specified ring for outstanding IOCB on the SLI queue
3112 * Return true if iocb matches the specified nport
3113 */
3114int
James Smart2e0fef82007-06-17 19:56:36 -05003115lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3116 struct lpfc_sli_ring *pring,
3117 struct lpfc_iocbq *iocb,
3118 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003119{
James Smart2e0fef82007-06-17 19:56:36 -05003120 struct lpfc_sli *psli = &phba->sli;
3121 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003122 struct lpfc_vport *vport = ndlp->vport;
3123
3124 if (iocb->vport != vport)
3125 return 0;
3126
dea31012005-04-17 16:05:31 -05003127 if (pring->ringno == LPFC_ELS_RING) {
3128 switch (icmd->ulpCommand) {
3129 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003130 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003131 return 1;
dea31012005-04-17 16:05:31 -05003132 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003133 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3134 return 1;
dea31012005-04-17 16:05:31 -05003135 case CMD_XMIT_ELS_RSP64_CX:
3136 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003137 return 1;
dea31012005-04-17 16:05:31 -05003138 }
James Smarta4bc3372006-12-02 13:34:16 -05003139 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003140
3141 } else if (pring->ringno == psli->fcp_ring) {
3142 /* Skip match check if waiting to relogin to FCP target */
3143 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003144 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003145 return 0;
dea31012005-04-17 16:05:31 -05003146 }
3147 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003148 return 1;
dea31012005-04-17 16:05:31 -05003149 }
3150 } else if (pring->ringno == psli->next_ring) {
3151
3152 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003153 return 0;
dea31012005-04-17 16:05:31 -05003154}
3155
3156/*
3157 * Free resources / clean up outstanding I/Os
3158 * associated with nlp_rpi in the LPFC_NODELIST entry.
3159 */
3160static int
James Smart2e0fef82007-06-17 19:56:36 -05003161lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003162{
James Smart2534ba72007-04-25 09:52:20 -04003163 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003164 struct lpfc_sli *psli;
3165 struct lpfc_sli_ring *pring;
3166 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003167 uint32_t i;
dea31012005-04-17 16:05:31 -05003168
James Smart92d7f7b2007-06-17 19:56:38 -05003169 lpfc_fabric_abort_nport(ndlp);
3170
dea31012005-04-17 16:05:31 -05003171 /*
3172 * Everything that matches on txcmplq will be returned
3173 * by firmware with a no rpi error.
3174 */
3175 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003176 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003177 /* Now process each ring */
3178 for (i = 0; i < psli->num_rings; i++) {
3179 pring = &psli->ring[i];
3180
James Smart2e0fef82007-06-17 19:56:36 -05003181 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003182 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003183 list) {
dea31012005-04-17 16:05:31 -05003184 /*
3185 * Check to see if iocb matches the nport we are
3186 * looking for
3187 */
James Smart92d7f7b2007-06-17 19:56:38 -05003188 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3189 ndlp))) {
dea31012005-04-17 16:05:31 -05003190 /* It matches, so deque and call compl
3191 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003192 list_move_tail(&iocb->list,
3193 &completions);
dea31012005-04-17 16:05:31 -05003194 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003195 }
3196 }
James Smart2e0fef82007-06-17 19:56:36 -05003197 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003198 }
3199 }
James Smart2534ba72007-04-25 09:52:20 -04003200
James Smarta257bf92009-04-06 18:48:10 -04003201 /* Cancel all the IOCBs from the completions list */
3202 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3203 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003204
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003205 return 0;
dea31012005-04-17 16:05:31 -05003206}
3207
3208/*
3209 * Free rpi associated with LPFC_NODELIST entry.
3210 * This routine is called from lpfc_freenode(), when we are removing
3211 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3212 * LOGO that completes successfully, and we are waiting to PLOGI back
3213 * to the remote NPort. In addition, it is called after we receive
3214 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3215 * we are waiting to PLOGI back to the remote NPort.
3216 */
3217int
James Smart2e0fef82007-06-17 19:56:36 -05003218lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003219{
James Smart2e0fef82007-06-17 19:56:36 -05003220 struct lpfc_hba *phba = vport->phba;
3221 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003222 int rc;
3223
James Smart21e9a0a2009-05-22 14:53:21 -04003224 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003225 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3226 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003227 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003228 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003229 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003230 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003231 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003232 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003233 }
dea31012005-04-17 16:05:31 -05003234 lpfc_no_rpi(phba, ndlp);
3235 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003236 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003237 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003238 return 1;
3239 }
3240 return 0;
3241}
3242
James Smart92d7f7b2007-06-17 19:56:38 -05003243void
3244lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3245{
3246 struct lpfc_hba *phba = vport->phba;
3247 LPFC_MBOXQ_t *mbox;
3248 int rc;
3249
3250 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3251 if (mbox) {
3252 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3253 mbox->vport = vport;
3254 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003255 mbox->context1 = NULL;
3256 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003257 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003258 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003259
3260 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3261 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3262 "1836 Could not issue "
3263 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003264 }
3265}
3266
3267void
3268lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3269{
3270 struct lpfc_hba *phba = vport->phba;
3271 LPFC_MBOXQ_t *mbox;
3272 int rc;
3273
3274 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3275 if (mbox) {
3276 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3277 mbox->vport = vport;
3278 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003279 mbox->context1 = NULL;
3280 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003281 if (rc != MBX_TIMEOUT)
3282 mempool_free(mbox, phba->mbox_mem_pool);
3283
3284 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003285 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3286 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003287 "unreg_did (default rpis) status %d\n",
3288 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003289 }
3290}
3291
dea31012005-04-17 16:05:31 -05003292/*
3293 * Free resources associated with LPFC_NODELIST entry
3294 * so it can be freed.
3295 */
3296static int
James Smart2e0fef82007-06-17 19:56:36 -05003297lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003298{
James Smart2e0fef82007-06-17 19:56:36 -05003299 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3300 struct lpfc_hba *phba = vport->phba;
3301 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003302 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003303
3304 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003305 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3306 "0900 Cleanup node for NPort x%x "
3307 "Data: x%x x%x x%x\n",
3308 ndlp->nlp_DID, ndlp->nlp_flag,
3309 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003310 if (NLP_CHK_FREE_REQ(ndlp)) {
3311 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3312 "0280 lpfc_cleanup_node: ndlp:x%p "
3313 "usgmap:x%x refcnt:%d\n",
3314 (void *)ndlp, ndlp->nlp_usg_map,
3315 atomic_read(&ndlp->kref.refcount));
3316 lpfc_dequeue_node(vport, ndlp);
3317 } else {
3318 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3319 "0281 lpfc_cleanup_node: ndlp:x%p "
3320 "usgmap:x%x refcnt:%d\n",
3321 (void *)ndlp, ndlp->nlp_usg_map,
3322 atomic_read(&ndlp->kref.refcount));
3323 lpfc_disable_node(vport, ndlp);
3324 }
dea31012005-04-17 16:05:31 -05003325
dea31012005-04-17 16:05:31 -05003326 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3327 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003328 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003329 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3330 mb->context2 = NULL;
3331 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3332 }
3333 }
James Smart33ccf8d2006-08-17 11:57:58 -04003334
James Smart2e0fef82007-06-17 19:56:36 -05003335 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003336 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003337 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003338 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003339 mp = (struct lpfc_dmabuf *) (mb->context1);
3340 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003341 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003342 kfree(mp);
3343 }
3344 list_del(&mb->list);
3345 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003346 /* We shall not invoke the lpfc_nlp_put to decrement
3347 * the ndlp reference count as we are in the process
3348 * of lpfc_nlp_release.
3349 */
dea31012005-04-17 16:05:31 -05003350 }
3351 }
James Smart2e0fef82007-06-17 19:56:36 -05003352 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003353
James Smarte47c9092008-02-08 18:49:26 -05003354 lpfc_els_abort(phba, ndlp);
3355
James Smart2e0fef82007-06-17 19:56:36 -05003356 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003357 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003358 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003359
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003360 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003361 del_timer_sync(&ndlp->nlp_delayfunc);
3362
James Smart0d2b6b82008-06-14 22:52:47 -04003363 list_del_init(&ndlp->els_retry_evt.evt_listp);
3364 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003365
James Smart2e0fef82007-06-17 19:56:36 -05003366 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003367
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003368 return 0;
dea31012005-04-17 16:05:31 -05003369}
3370
3371/*
3372 * Check to see if we can free the nlp back to the freelist.
3373 * If we are in the middle of using the nlp in the discovery state
3374 * machine, defer the free till we reach the end of the state machine.
3375 */
James Smart329f9bc2007-04-25 09:53:01 -04003376static void
James Smart2e0fef82007-06-17 19:56:36 -05003377lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003378{
James Smarta8adb832007-10-27 13:37:53 -04003379 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003380 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003381 LPFC_MBOXQ_t *mbox;
3382 int rc;
dea31012005-04-17 16:05:31 -05003383
James Smart0d2b6b82008-06-14 22:52:47 -04003384 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003385 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3386 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003387 /* For this case we need to cleanup the default rpi
3388 * allocated by the firmware.
3389 */
3390 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3391 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003392 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003393 (uint8_t *) &vport->fc_sparam, mbox, 0);
3394 if (rc) {
3395 mempool_free(mbox, phba->mbox_mem_pool);
3396 }
3397 else {
3398 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3399 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3400 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003401 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003402 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3403 if (rc == MBX_NOT_FINISHED) {
3404 mempool_free(mbox, phba->mbox_mem_pool);
3405 }
3406 }
3407 }
3408 }
James Smart2e0fef82007-06-17 19:56:36 -05003409 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003410
James Smart2e0fef82007-06-17 19:56:36 -05003411 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003412 * We can get here with a non-NULL ndlp->rport because when we
3413 * unregister a rport we don't break the rport/node linkage. So if we
3414 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003415 */
James Smart92d7f7b2007-06-17 19:56:38 -05003416 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003417 rdata = ndlp->rport->dd_data;
3418 rdata->pnode = NULL;
3419 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003420 }
dea31012005-04-17 16:05:31 -05003421}
3422
3423static int
James Smart2e0fef82007-06-17 19:56:36 -05003424lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3425 uint32_t did)
dea31012005-04-17 16:05:31 -05003426{
James Smart2e0fef82007-06-17 19:56:36 -05003427 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003428
3429 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003430 return 0;
dea31012005-04-17 16:05:31 -05003431
dea31012005-04-17 16:05:31 -05003432 /* First check for Direct match */
3433 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003434 return 1;
dea31012005-04-17 16:05:31 -05003435
3436 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003437 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003438 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003439 return 0;
dea31012005-04-17 16:05:31 -05003440 }
3441
3442 matchdid.un.word = did;
3443 ndlpdid.un.word = ndlp->nlp_DID;
3444 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3445 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3446 (mydid.un.b.area == matchdid.un.b.area)) {
3447 if ((ndlpdid.un.b.domain == 0) &&
3448 (ndlpdid.un.b.area == 0)) {
3449 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003450 return 1;
dea31012005-04-17 16:05:31 -05003451 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003452 return 0;
dea31012005-04-17 16:05:31 -05003453 }
3454
3455 matchdid.un.word = ndlp->nlp_DID;
3456 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3457 (mydid.un.b.area == ndlpdid.un.b.area)) {
3458 if ((matchdid.un.b.domain == 0) &&
3459 (matchdid.un.b.area == 0)) {
3460 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003461 return 1;
dea31012005-04-17 16:05:31 -05003462 }
3463 }
3464 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003465 return 0;
dea31012005-04-17 16:05:31 -05003466}
3467
James Smart685f0bf2007-04-25 09:53:08 -04003468/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003469static struct lpfc_nodelist *
3470__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003471{
James Smart2fb9bd82006-12-02 13:33:57 -05003472 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003473 uint32_t data1;
3474
James Smart2e0fef82007-06-17 19:56:36 -05003475 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3476 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04003477 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3478 ((uint32_t) ndlp->nlp_xri << 16) |
3479 ((uint32_t) ndlp->nlp_type << 8) |
3480 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04003481 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3482 "0929 FIND node DID "
3483 "Data: x%p x%x x%x x%x\n",
3484 ndlp, ndlp->nlp_DID,
3485 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04003486 return ndlp;
dea31012005-04-17 16:05:31 -05003487 }
3488 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003489
dea31012005-04-17 16:05:31 -05003490 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04003491 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3492 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05003493 return NULL;
3494}
3495
3496struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003497lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003498{
James Smart2e0fef82007-06-17 19:56:36 -05003499 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003500 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003501
James Smart2e0fef82007-06-17 19:56:36 -05003502 spin_lock_irq(shost->host_lock);
3503 ndlp = __lpfc_findnode_did(vport, did);
3504 spin_unlock_irq(shost->host_lock);
3505 return ndlp;
3506}
3507
3508struct lpfc_nodelist *
3509lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3510{
3511 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3512 struct lpfc_nodelist *ndlp;
3513
3514 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003515 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05003516 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
3517 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05003518 return NULL;
3519 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05003520 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003521 if (!ndlp)
3522 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003523 lpfc_nlp_init(vport, ndlp, did);
3524 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3525 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003526 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003527 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003528 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05003529 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3530 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
3531 if (!ndlp)
3532 return NULL;
3533 spin_lock_irq(shost->host_lock);
3534 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3535 spin_unlock_irq(shost->host_lock);
3536 return ndlp;
dea31012005-04-17 16:05:31 -05003537 }
James Smarte47c9092008-02-08 18:49:26 -05003538
James Smart58da1ff2008-04-07 10:15:56 -04003539 if ((vport->fc_flag & FC_RSCN_MODE) &&
3540 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003541 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04003542 /* If we've already recieved a PLOGI from this NPort
3543 * we don't need to try to discover it again.
3544 */
3545 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
3546 return NULL;
3547
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003548 /* Since this node is marked for discovery,
3549 * delay timeout is not needed.
3550 */
James Smart0d2b6b82008-06-14 22:52:47 -04003551 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04003552 spin_lock_irq(shost->host_lock);
3553 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3554 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04003555 } else
dea31012005-04-17 16:05:31 -05003556 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003557 } else {
James Smart87af33f2007-10-27 13:37:43 -04003558 /* If we've already recieved a PLOGI from this NPort,
3559 * or we are already in the process of discovery on it,
3560 * we don't need to try to discover it again.
3561 */
James Smart685f0bf2007-04-25 09:53:08 -04003562 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04003563 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3564 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05003565 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003566 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3567 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003568 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003569 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003570 }
3571 return ndlp;
3572}
3573
3574/* Build a list of nodes to discover based on the loopmap */
3575void
James Smart2e0fef82007-06-17 19:56:36 -05003576lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003577{
James Smart2e0fef82007-06-17 19:56:36 -05003578 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003579 int j;
3580 uint32_t alpa, index;
3581
James Smart2e0fef82007-06-17 19:56:36 -05003582 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003583 return;
James Smart2e0fef82007-06-17 19:56:36 -05003584
3585 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05003586 return;
dea31012005-04-17 16:05:31 -05003587
3588 /* Check for loop map present or not */
3589 if (phba->alpa_map[0]) {
3590 for (j = 1; j <= phba->alpa_map[0]; j++) {
3591 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05003592 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05003593 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003594 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003595 }
3596 } else {
3597 /* No alpamap, so try all alpa's */
3598 for (j = 0; j < FC_MAXLOOP; j++) {
3599 /* If cfg_scan_down is set, start from highest
3600 * ALPA (0xef) to lowest (0x1).
3601 */
James Smart3de2a652007-08-02 11:09:59 -04003602 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05003603 index = j;
3604 else
3605 index = FC_MAXLOOP - j - 1;
3606 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05003607 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05003608 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003609 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003610 }
3611 }
3612 return;
3613}
3614
dea31012005-04-17 16:05:31 -05003615void
James Smart2e0fef82007-06-17 19:56:36 -05003616lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003617{
dea31012005-04-17 16:05:31 -05003618 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05003619 struct lpfc_sli *psli = &phba->sli;
3620 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
3621 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
3622 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
3623 int rc;
3624
James Smart92d7f7b2007-06-17 19:56:38 -05003625 /*
3626 * if it's not a physical port or if we already send
3627 * clear_la then don't send it.
3628 */
3629 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04003630 (vport->port_type != LPFC_PHYSICAL_PORT) ||
3631 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05003632 return;
3633
James Smart2e0fef82007-06-17 19:56:36 -05003634 /* Link up discovery */
3635 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
3636 phba->link_state = LPFC_CLEAR_LA;
3637 lpfc_clear_la(phba, mbox);
3638 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
3639 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003640 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05003641 if (rc == MBX_NOT_FINISHED) {
3642 mempool_free(mbox, phba->mbox_mem_pool);
3643 lpfc_disc_flush_list(vport);
3644 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3645 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3646 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05003647 phba->link_state = LPFC_HBA_ERROR;
3648 }
3649 }
3650}
3651
3652/* Reg_vpi to tell firmware to resume normal operations */
3653void
3654lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
3655{
3656 LPFC_MBOXQ_t *regvpimbox;
3657
3658 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3659 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04003660 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003661 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
3662 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003663 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05003664 == MBX_NOT_FINISHED) {
3665 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003666 }
3667 }
3668}
3669
3670/* Start Link up / RSCN discovery on NPR nodes */
3671void
3672lpfc_disc_start(struct lpfc_vport *vport)
3673{
3674 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3675 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04003676 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05003677 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04003678 int did_changed;
dea31012005-04-17 16:05:31 -05003679
James Smart2e0fef82007-06-17 19:56:36 -05003680 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003681 return;
James Smart2e0fef82007-06-17 19:56:36 -05003682
3683 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05003684 clear_la_pending = 1;
3685 else
3686 clear_la_pending = 0;
3687
James Smart2e0fef82007-06-17 19:56:36 -05003688 if (vport->port_state < LPFC_VPORT_READY)
3689 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05003690
James Smart2e0fef82007-06-17 19:56:36 -05003691 lpfc_set_disctmo(vport);
3692
3693 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05003694 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003695 else
dea31012005-04-17 16:05:31 -05003696 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003697
3698 vport->fc_prevDID = vport->fc_myDID;
3699 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05003700
3701 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003702 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3703 "0202 Start Discovery hba state x%x "
3704 "Data: x%x x%x x%x\n",
3705 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
3706 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003707
3708 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003709 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05003710
3711 if (num_sent)
3712 return;
3713
James Smart92d7f7b2007-06-17 19:56:38 -05003714 /*
3715 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
3716 * continue discovery.
3717 */
3718 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05003719 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04003720 !(vport->fc_flag & FC_RSCN_MODE) &&
3721 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003722 lpfc_issue_reg_vpi(phba, vport);
3723 return;
3724 }
James Smart2e0fef82007-06-17 19:56:36 -05003725
James Smart92d7f7b2007-06-17 19:56:38 -05003726 /*
3727 * For SLI2, we need to set port_state to READY and continue
3728 * discovery.
3729 */
3730 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
3731 /* If we get here, there is nothing to ADISC */
3732 if (vport->port_type == LPFC_PHYSICAL_PORT)
3733 lpfc_issue_clear_la(phba, vport);
3734
3735 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05003736 vport->num_disc_nodes = 0;
3737 /* go thru NPR nodes and issue ELS PLOGIs */
3738 if (vport->fc_npr_cnt)
3739 lpfc_els_disc_plogi(vport);
3740
3741 if (!vport->num_disc_nodes) {
3742 spin_lock_irq(shost->host_lock);
3743 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3744 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003745 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05003746 }
3747 }
James Smart92d7f7b2007-06-17 19:56:38 -05003748 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05003749 } else {
3750 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003751 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05003752
3753 if (num_sent)
3754 return;
3755
James Smart2e0fef82007-06-17 19:56:36 -05003756 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05003757 /* Check to see if more RSCNs came in while we
3758 * were processing this one.
3759 */
James Smart2e0fef82007-06-17 19:56:36 -05003760 if ((vport->fc_rscn_id_cnt == 0) &&
3761 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
3762 spin_lock_irq(shost->host_lock);
3763 vport->fc_flag &= ~FC_RSCN_MODE;
3764 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003765 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003766 } else
James Smart2e0fef82007-06-17 19:56:36 -05003767 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05003768 }
3769 }
3770 return;
3771}
3772
3773/*
3774 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
3775 * ring the match the sppecified nodelist.
3776 */
3777static void
James Smart2e0fef82007-06-17 19:56:36 -05003778lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003779{
James Smart2534ba72007-04-25 09:52:20 -04003780 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003781 struct lpfc_sli *psli;
3782 IOCB_t *icmd;
3783 struct lpfc_iocbq *iocb, *next_iocb;
3784 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003785
3786 psli = &phba->sli;
3787 pring = &psli->ring[LPFC_ELS_RING];
3788
3789 /* Error matching iocb on txq or txcmplq
3790 * First check the txq.
3791 */
James Smart2e0fef82007-06-17 19:56:36 -05003792 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003793 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3794 if (iocb->context1 != ndlp) {
3795 continue;
3796 }
3797 icmd = &iocb->iocb;
3798 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
3799 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
3800
James Smart2534ba72007-04-25 09:52:20 -04003801 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05003802 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003803 }
3804 }
3805
3806 /* Next check the txcmplq */
3807 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
3808 if (iocb->context1 != ndlp) {
3809 continue;
3810 }
3811 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05003812 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
3813 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04003814 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05003815 }
3816 }
James Smart2e0fef82007-06-17 19:56:36 -05003817 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003818
James Smarta257bf92009-04-06 18:48:10 -04003819 /* Cancel all the IOCBs from the completions list */
3820 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3821 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003822}
3823
Adrian Bunka6ababd2007-11-05 18:07:33 +01003824static void
James Smart2e0fef82007-06-17 19:56:36 -05003825lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003826{
3827 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05003828 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003829
James Smart2e0fef82007-06-17 19:56:36 -05003830 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
3831 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04003832 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003833 if (!NLP_CHK_NODE_ACT(ndlp))
3834 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003835 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3836 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
3837 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04003838 }
dea31012005-04-17 16:05:31 -05003839 }
3840 }
dea31012005-04-17 16:05:31 -05003841}
3842
James Smart92d7f7b2007-06-17 19:56:38 -05003843void
3844lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
3845{
3846 lpfc_els_flush_rscn(vport);
3847 lpfc_els_flush_cmd(vport);
3848 lpfc_disc_flush_list(vport);
3849}
3850
dea31012005-04-17 16:05:31 -05003851/*****************************************************************************/
3852/*
3853 * NAME: lpfc_disc_timeout
3854 *
3855 * FUNCTION: Fibre Channel driver discovery timeout routine.
3856 *
3857 * EXECUTION ENVIRONMENT: interrupt only
3858 *
3859 * CALLED FROM:
3860 * Timer function
3861 *
3862 * RETURNS:
3863 * none
3864 */
3865/*****************************************************************************/
3866void
3867lpfc_disc_timeout(unsigned long ptr)
3868{
James Smart2e0fef82007-06-17 19:56:36 -05003869 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
3870 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04003871 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003872 unsigned long flags = 0;
3873
3874 if (unlikely(!phba))
3875 return;
3876
James Smart5e9d9b82008-06-14 22:52:53 -04003877 spin_lock_irqsave(&vport->work_port_lock, flags);
3878 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
3879 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05003880 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04003881 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05003882
James Smart5e9d9b82008-06-14 22:52:53 -04003883 if (!tmo_posted)
3884 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003885 return;
3886}
3887
3888static void
James Smart2e0fef82007-06-17 19:56:36 -05003889lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003890{
James Smart2e0fef82007-06-17 19:56:36 -05003891 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3892 struct lpfc_hba *phba = vport->phba;
3893 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003894 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05003895 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05003896 int rc, clrlaerr = 0;
3897
James Smart2e0fef82007-06-17 19:56:36 -05003898 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05003899 return;
3900
James Smart2e0fef82007-06-17 19:56:36 -05003901 spin_lock_irq(shost->host_lock);
3902 vport->fc_flag &= ~FC_DISC_TMO;
3903 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003904
James Smart858c9f62007-06-17 19:56:39 -05003905 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3906 "disc timeout: state:x%x rtry:x%x flg:x%x",
3907 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3908
James Smart2e0fef82007-06-17 19:56:36 -05003909 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05003910
3911 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05003912 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
3913 * FAN
3914 */
3915 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04003916 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3917 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003918 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05003919 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04003920 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003921 if (!NLP_CHK_NODE_ACT(ndlp))
3922 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003923 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3924 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003925 if (ndlp->nlp_type & NLP_FABRIC) {
3926 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05003927 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04003928
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003929 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003930 /* Fail outstanding IO now since device
3931 * is marked for PLOGI.
3932 */
James Smart2e0fef82007-06-17 19:56:36 -05003933 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003934 }
3935 }
James Smart92d7f7b2007-06-17 19:56:38 -05003936 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05003937 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05003938 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003939 }
dea31012005-04-17 16:05:31 -05003940 break;
3941
James Smart92d7f7b2007-06-17 19:56:38 -05003942 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05003943 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05003944 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05003945 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04003946 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3947 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04003948 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05003949
3950 /* Assume no Fabric and go on with discovery.
3951 * Check for outstanding ELS FLOGI to abort.
3952 */
3953
3954 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05003955 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003956
3957 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05003958 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05003959 break;
3960
3961 case LPFC_FABRIC_CFG_LINK:
3962 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
3963 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04003964 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3965 "0223 Timeout while waiting for "
3966 "NameServer login\n");
dea31012005-04-17 16:05:31 -05003967 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05003968 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05003969 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04003970 lpfc_els_abort(phba, ndlp);
3971
3972 /* ReStart discovery */
3973 goto restart_disc;
dea31012005-04-17 16:05:31 -05003974
3975 case LPFC_NS_QRY:
3976 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04003977 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3978 "0224 NameServer Query timeout "
3979 "Data: x%x x%x\n",
3980 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05003981
James Smart92d7f7b2007-06-17 19:56:38 -05003982 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
3983 /* Try it one more time */
3984 vport->fc_ns_retry++;
3985 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
3986 vport->fc_ns_retry, 0);
3987 if (rc == 0)
3988 break;
dea31012005-04-17 16:05:31 -05003989 }
James Smart92d7f7b2007-06-17 19:56:38 -05003990 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003991
James Smart87af33f2007-10-27 13:37:43 -04003992restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05003993 /*
3994 * Discovery is over.
3995 * set port_state to PORT_READY if SLI2.
3996 * cmpl_reg_vpi will set port_state to READY for SLI3.
3997 */
James Smart3772a992009-05-22 14:50:54 -04003998 if (phba->sli_rev < LPFC_SLI_REV4) {
3999 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4000 lpfc_issue_reg_vpi(phba, vport);
4001 else { /* NPIV Not enabled */
4002 lpfc_issue_clear_la(phba, vport);
4003 vport->port_state = LPFC_VPORT_READY;
4004 }
dea31012005-04-17 16:05:31 -05004005 }
4006
4007 /* Setup and issue mailbox INITIALIZE LINK command */
4008 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4009 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004010 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4011 "0206 Device Discovery "
4012 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004013 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004014 break;
4015 }
4016
4017 lpfc_linkdown(phba);
4018 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4019 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004020 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004021 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004022 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004023 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004024 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004025 if (rc == MBX_NOT_FINISHED)
4026 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4027
4028 break;
4029
4030 case LPFC_DISC_AUTH:
4031 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004032 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4033 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004034 lpfc_disc_flush_list(vport);
4035
James Smart92d7f7b2007-06-17 19:56:38 -05004036 /*
4037 * set port_state to PORT_READY if SLI2.
4038 * cmpl_reg_vpi will set port_state to READY for SLI3.
4039 */
James Smart3772a992009-05-22 14:50:54 -04004040 if (phba->sli_rev < LPFC_SLI_REV4) {
4041 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4042 lpfc_issue_reg_vpi(phba, vport);
4043 else { /* NPIV Not enabled */
4044 lpfc_issue_clear_la(phba, vport);
4045 vport->port_state = LPFC_VPORT_READY;
4046 }
dea31012005-04-17 16:05:31 -05004047 }
4048 break;
4049
James Smart2e0fef82007-06-17 19:56:36 -05004050 case LPFC_VPORT_READY:
4051 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004052 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4053 "0231 RSCN timeout Data: x%x "
4054 "x%x\n",
4055 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004056
4057 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004058 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004059
James Smart2e0fef82007-06-17 19:56:36 -05004060 lpfc_els_flush_rscn(vport);
4061 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004062 }
4063 break;
James Smart2e0fef82007-06-17 19:56:36 -05004064
James Smart92d7f7b2007-06-17 19:56:38 -05004065 default:
James Smarte8b62012007-08-02 11:10:09 -04004066 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004067 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004068 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004069 break;
4070 }
4071
4072 switch (phba->link_state) {
4073 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004074 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004075 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4076 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004077 clrlaerr = 1;
4078 break;
4079
James Smart09372822008-01-11 01:52:54 -05004080 case LPFC_LINK_UP:
4081 lpfc_issue_clear_la(phba, vport);
4082 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004083 case LPFC_LINK_UNKNOWN:
4084 case LPFC_WARM_START:
4085 case LPFC_INIT_START:
4086 case LPFC_INIT_MBX_CMDS:
4087 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004088 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004089 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4090 "0230 Unexpected timeout, hba link "
4091 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004092 clrlaerr = 1;
4093 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004094
4095 case LPFC_HBA_READY:
4096 break;
dea31012005-04-17 16:05:31 -05004097 }
4098
4099 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004100 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004101 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004102 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4103 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004104 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004105 }
4106
4107 return;
4108}
4109
dea31012005-04-17 16:05:31 -05004110/*
4111 * This routine handles processing a NameServer REG_LOGIN mailbox
4112 * command upon completion. It is setup in the LPFC_MBOXQ
4113 * as the completion routine when the command is
4114 * handed off to the SLI layer.
4115 */
4116void
James Smart2e0fef82007-06-17 19:56:36 -05004117lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004118{
James Smart04c68492009-05-22 14:52:52 -04004119 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004120 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4121 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4122 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004123
4124 pmb->context1 = NULL;
4125
dea31012005-04-17 16:05:31 -05004126 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004127 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004128 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004129 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004130
James Smart2e0fef82007-06-17 19:56:36 -05004131 /*
4132 * Start issuing Fabric-Device Management Interface (FDMI) command to
4133 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4134 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004135 */
James Smart2e0fef82007-06-17 19:56:36 -05004136
James Smart3de2a652007-08-02 11:09:59 -04004137 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004138 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4139 else
4140 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004141
James Smartfa4066b2008-01-11 01:53:27 -05004142 /* decrement the node reference count held for this callback
4143 * function.
4144 */
James Smart329f9bc2007-04-25 09:53:01 -04004145 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004146 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4147 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004148 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004149
4150 return;
4151}
4152
James Smart685f0bf2007-04-25 09:53:08 -04004153static int
4154lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4155{
4156 uint16_t *rpi = param;
4157
4158 return ndlp->nlp_rpi == *rpi;
4159}
4160
4161static int
4162lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4163{
4164 return memcmp(&ndlp->nlp_portname, param,
4165 sizeof(ndlp->nlp_portname)) == 0;
4166}
4167
Adrian Bunka6ababd2007-11-05 18:07:33 +01004168static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004169__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004170{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004171 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004172
James Smart2e0fef82007-06-17 19:56:36 -05004173 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004174 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004175 return ndlp;
4176 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004177 return NULL;
dea31012005-04-17 16:05:31 -05004178}
4179
James Smart685f0bf2007-04-25 09:53:08 -04004180/*
4181 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004182 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004183 */
4184struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004185__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004186{
James Smart2e0fef82007-06-17 19:56:36 -05004187 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004188}
4189
James Smart488d1462006-03-07 15:02:37 -05004190/*
James Smart685f0bf2007-04-25 09:53:08 -04004191 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004192 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004193 */
4194struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004195lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004196{
James Smart2e0fef82007-06-17 19:56:36 -05004197 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004198 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004199
James Smart2e0fef82007-06-17 19:56:36 -05004200 spin_lock_irq(shost->host_lock);
4201 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4202 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004203 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004204}
4205
dea31012005-04-17 16:05:31 -05004206void
James Smart2e0fef82007-06-17 19:56:36 -05004207lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4208 uint32_t did)
dea31012005-04-17 16:05:31 -05004209{
4210 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004211
4212 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004213 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004214
4215 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4216 "node init: did:x%x",
4217 ndlp->nlp_DID, 0, 0);
4218
dea31012005-04-17 16:05:31 -05004219 return;
4220}
James Smart329f9bc2007-04-25 09:53:01 -04004221
James Smart98c9ea52007-10-27 13:37:33 -04004222/* This routine releases all resources associated with a specifc NPort's ndlp
4223 * and mempool_free's the nodelist.
4224 */
James Smart311464e2007-08-02 11:10:37 -04004225static void
James Smart329f9bc2007-04-25 09:53:01 -04004226lpfc_nlp_release(struct kref *kref)
4227{
James Smarte47c9092008-02-08 18:49:26 -05004228 struct lpfc_hba *phba;
4229 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004230 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4231 kref);
James Smart858c9f62007-06-17 19:56:39 -05004232
4233 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4234 "node release: did:x%x flg:x%x type:x%x",
4235 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4236
James Smarte47c9092008-02-08 18:49:26 -05004237 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4238 "0279 lpfc_nlp_release: ndlp:x%p "
4239 "usgmap:x%x refcnt:%d\n",
4240 (void *)ndlp, ndlp->nlp_usg_map,
4241 atomic_read(&ndlp->kref.refcount));
4242
4243 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004244 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004245
4246 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004247 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004248 spin_lock_irqsave(&phba->ndlp_lock, flags);
4249 NLP_CLR_NODE_ACT(ndlp);
4250 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4251
4252 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004253 if (NLP_CHK_FREE_REQ(ndlp)) {
4254 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004255 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004256 }
James Smart329f9bc2007-04-25 09:53:01 -04004257}
4258
James Smart98c9ea52007-10-27 13:37:33 -04004259/* This routine bumps the reference count for a ndlp structure to ensure
4260 * that one discovery thread won't free a ndlp while another discovery thread
4261 * is using it.
4262 */
James Smart329f9bc2007-04-25 09:53:01 -04004263struct lpfc_nodelist *
4264lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4265{
James Smarte47c9092008-02-08 18:49:26 -05004266 struct lpfc_hba *phba;
4267 unsigned long flags;
4268
James Smart98c9ea52007-10-27 13:37:33 -04004269 if (ndlp) {
4270 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4271 "node get: did:x%x flg:x%x refcnt:x%x",
4272 ndlp->nlp_DID, ndlp->nlp_flag,
4273 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004274 /* The check of ndlp usage to prevent incrementing the
4275 * ndlp reference count that is in the process of being
4276 * released.
4277 */
James Smarta257bf92009-04-06 18:48:10 -04004278 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004279 spin_lock_irqsave(&phba->ndlp_lock, flags);
4280 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4281 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4282 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4283 "0276 lpfc_nlp_get: ndlp:x%p "
4284 "usgmap:x%x refcnt:%d\n",
4285 (void *)ndlp, ndlp->nlp_usg_map,
4286 atomic_read(&ndlp->kref.refcount));
4287 return NULL;
4288 } else
4289 kref_get(&ndlp->kref);
4290 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004291 }
James Smart329f9bc2007-04-25 09:53:01 -04004292 return ndlp;
4293}
4294
James Smart98c9ea52007-10-27 13:37:33 -04004295/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004296 * count goes to 0, this indicates the the associated nodelist should be
4297 * freed. Returning 1 indicates the ndlp resource has been released; on the
4298 * other hand, returning 0 indicates the ndlp resource has not been released
4299 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004300 */
James Smart329f9bc2007-04-25 09:53:01 -04004301int
4302lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4303{
James Smarte47c9092008-02-08 18:49:26 -05004304 struct lpfc_hba *phba;
4305 unsigned long flags;
4306
4307 if (!ndlp)
4308 return 1;
4309
4310 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4311 "node put: did:x%x flg:x%x refcnt:x%x",
4312 ndlp->nlp_DID, ndlp->nlp_flag,
4313 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004314 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004315 spin_lock_irqsave(&phba->ndlp_lock, flags);
4316 /* Check the ndlp memory free acknowledge flag to avoid the
4317 * possible race condition that kref_put got invoked again
4318 * after previous one has done ndlp memory free.
4319 */
4320 if (NLP_CHK_FREE_ACK(ndlp)) {
4321 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4322 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4323 "0274 lpfc_nlp_put: ndlp:x%p "
4324 "usgmap:x%x refcnt:%d\n",
4325 (void *)ndlp, ndlp->nlp_usg_map,
4326 atomic_read(&ndlp->kref.refcount));
4327 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004328 }
James Smarte47c9092008-02-08 18:49:26 -05004329 /* Check the ndlp inactivate log flag to avoid the possible
4330 * race condition that kref_put got invoked again after ndlp
4331 * is already in inactivating state.
4332 */
4333 if (NLP_CHK_IACT_REQ(ndlp)) {
4334 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4335 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4336 "0275 lpfc_nlp_put: ndlp:x%p "
4337 "usgmap:x%x refcnt:%d\n",
4338 (void *)ndlp, ndlp->nlp_usg_map,
4339 atomic_read(&ndlp->kref.refcount));
4340 return 1;
4341 }
4342 /* For last put, mark the ndlp usage flags to make sure no
4343 * other kref_get and kref_put on the same ndlp shall get
4344 * in between the process when the final kref_put has been
4345 * invoked on this ndlp.
4346 */
4347 if (atomic_read(&ndlp->kref.refcount) == 1) {
4348 /* Indicate ndlp is put to inactive state. */
4349 NLP_SET_IACT_REQ(ndlp);
4350 /* Acknowledge ndlp memory free has been seen. */
4351 if (NLP_CHK_FREE_REQ(ndlp))
4352 NLP_SET_FREE_ACK(ndlp);
4353 }
4354 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4355 /* Note, the kref_put returns 1 when decrementing a reference
4356 * count that was 1, it invokes the release callback function,
4357 * but it still left the reference count as 1 (not actually
4358 * performs the last decrementation). Otherwise, it actually
4359 * decrements the reference count and returns 0.
4360 */
4361 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004362}
James Smart98c9ea52007-10-27 13:37:33 -04004363
4364/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004365 * by any other discovery thread. This routine returns 1 if the
4366 * ndlp has been freed. A return value of 0 indicates the ndlp is
4367 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004368 */
4369int
4370lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4371{
4372 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4373 "node not used: did:x%x flg:x%x refcnt:x%x",
4374 ndlp->nlp_DID, ndlp->nlp_flag,
4375 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004376 if (atomic_read(&ndlp->kref.refcount) == 1)
4377 if (lpfc_nlp_put(ndlp))
4378 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004379 return 0;
4380}
James Smart6fb120a2009-05-22 14:52:59 -04004381
4382/**
4383 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4384 * @phba: Pointer to hba context object.
4385 *
4386 * This function iterate through all FC nodes associated
4387 * will all vports to check if there is any node with
4388 * fc_rports associated with it. If there is an fc_rport
4389 * associated with the node, then the node is either in
4390 * discovered state or its devloss_timer is pending.
4391 */
4392static int
4393lpfc_fcf_inuse(struct lpfc_hba *phba)
4394{
4395 struct lpfc_vport **vports;
4396 int i, ret = 0;
4397 struct lpfc_nodelist *ndlp;
4398 struct Scsi_Host *shost;
4399
4400 vports = lpfc_create_vport_work_array(phba);
4401
4402 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4403 shost = lpfc_shost_from_vport(vports[i]);
4404 spin_lock_irq(shost->host_lock);
4405 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4406 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4407 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4408 ret = 1;
4409 spin_unlock_irq(shost->host_lock);
4410 goto out;
James Smart891478a2009-11-18 15:40:23 -05004411 } else {
4412 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4413 "2624 RPI %x DID %x flg %x still "
4414 "logged in\n",
4415 ndlp->nlp_rpi, ndlp->nlp_DID,
4416 ndlp->nlp_flag);
4417 if (ndlp->nlp_flag & NLP_RPI_VALID)
4418 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04004419 }
4420 }
4421 spin_unlock_irq(shost->host_lock);
4422 }
4423out:
4424 lpfc_destroy_vport_work_array(phba, vports);
4425 return ret;
4426}
4427
4428/**
4429 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4430 * @phba: Pointer to hba context object.
4431 * @mboxq: Pointer to mailbox object.
4432 *
4433 * This function frees memory associated with the mailbox command.
4434 */
4435static void
4436lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4437{
4438 struct lpfc_vport *vport = mboxq->vport;
4439
4440 if (mboxq->u.mb.mbxStatus) {
4441 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4442 "2555 UNREG_VFI mbxStatus error x%x "
4443 "HBA state x%x\n",
4444 mboxq->u.mb.mbxStatus, vport->port_state);
4445 }
4446 mempool_free(mboxq, phba->mbox_mem_pool);
4447 return;
4448}
4449
4450/**
4451 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4452 * @phba: Pointer to hba context object.
4453 * @mboxq: Pointer to mailbox object.
4454 *
4455 * This function frees memory associated with the mailbox command.
4456 */
4457static void
4458lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4459{
4460 struct lpfc_vport *vport = mboxq->vport;
4461
4462 if (mboxq->u.mb.mbxStatus) {
4463 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4464 "2550 UNREG_FCFI mbxStatus error x%x "
4465 "HBA state x%x\n",
4466 mboxq->u.mb.mbxStatus, vport->port_state);
4467 }
4468 mempool_free(mboxq, phba->mbox_mem_pool);
4469 return;
4470}
4471
4472/**
4473 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
4474 * @phba: Pointer to hba context object.
4475 *
4476 * This function check if there are any connected remote port for the FCF and
4477 * if all the devices are disconnected, this function unregister FCFI.
4478 * This function also tries to use another FCF for discovery.
4479 */
4480void
4481lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
4482{
4483 LPFC_MBOXQ_t *mbox;
4484 int rc;
4485 struct lpfc_vport **vports;
4486 int i;
James Smart695a8142010-01-26 23:08:03 -05004487 struct lpfc_nodelist *ndlp;
James Smart6fb120a2009-05-22 14:52:59 -04004488
4489 spin_lock_irq(&phba->hbalock);
4490 /*
4491 * If HBA is not running in FIP mode or
4492 * If HBA does not support FCoE or
4493 * If FCF is not registered.
4494 * do nothing.
4495 */
4496 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
4497 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smart45ed1192009-10-02 15:17:02 -04004498 (!(phba->hba_flag & HBA_FIP_SUPPORT))) {
James Smart6fb120a2009-05-22 14:52:59 -04004499 spin_unlock_irq(&phba->hbalock);
4500 return;
4501 }
4502 spin_unlock_irq(&phba->hbalock);
4503
4504 if (lpfc_fcf_inuse(phba))
4505 return;
4506
James Smart4d9ab992009-10-02 15:16:39 -04004507 /* At this point, all discovery is aborted */
4508 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04004509
4510 /* Unregister VPIs */
4511 vports = lpfc_create_vport_work_array(phba);
4512 if (vports &&
4513 (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
4514 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05004515 /* Stop FLOGI/FDISC retries */
4516 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
4517 if (ndlp)
4518 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart6fb120a2009-05-22 14:52:59 -04004519 lpfc_mbx_unreg_vpi(vports[i]);
James Smart97957242009-12-21 17:03:15 -05004520 spin_lock_irq(&phba->hbalock);
James Smart891478a2009-11-18 15:40:23 -05004521 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05004522 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart97957242009-12-21 17:03:15 -05004523 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04004524 }
4525 lpfc_destroy_vport_work_array(phba, vports);
4526
James Smart695a8142010-01-26 23:08:03 -05004527 /* Cleanup any outstanding ELS commands */
4528 lpfc_els_flush_all_cmd(phba);
4529
James Smart6fb120a2009-05-22 14:52:59 -04004530 /* Unregister VFI */
4531 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4532 if (!mbox) {
4533 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4534 "2556 UNREG_VFI mbox allocation failed"
4535 "HBA state x%x\n",
4536 phba->pport->port_state);
4537 return;
4538 }
4539
James Smart6669f9b2009-10-02 15:16:45 -04004540 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04004541 mbox->vport = phba->pport;
4542 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
4543
4544 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4545 if (rc == MBX_NOT_FINISHED) {
4546 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4547 "2557 UNREG_VFI issue mbox failed rc x%x "
4548 "HBA state x%x\n",
4549 rc, phba->pport->port_state);
4550 mempool_free(mbox, phba->mbox_mem_pool);
4551 return;
4552 }
4553
James Smart695a8142010-01-26 23:08:03 -05004554 spin_lock_irq(&phba->hbalock);
4555 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
4556 spin_unlock_irq(&phba->hbalock);
4557
James Smart6fb120a2009-05-22 14:52:59 -04004558 /* Unregister FCF */
4559 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4560 if (!mbox) {
4561 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4562 "2551 UNREG_FCFI mbox allocation failed"
4563 "HBA state x%x\n",
4564 phba->pport->port_state);
4565 return;
4566 }
4567
4568 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
4569 mbox->vport = phba->pport;
4570 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
4571 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4572
4573 if (rc == MBX_NOT_FINISHED) {
4574 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4575 "2552 UNREG_FCFI issue mbox failed rc x%x "
4576 "HBA state x%x\n",
4577 rc, phba->pport->port_state);
4578 mempool_free(mbox, phba->mbox_mem_pool);
4579 return;
4580 }
4581
4582 spin_lock_irq(&phba->hbalock);
4583 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_REGISTERED |
4584 FCF_DISCOVERED | FCF_BOOT_ENABLE | FCF_IN_USE |
4585 FCF_VALID_VLAN);
4586 spin_unlock_irq(&phba->hbalock);
4587
4588 /*
4589 * If driver is not unloading, check if there is any other
4590 * FCF record that can be used for discovery.
4591 */
4592 if ((phba->pport->load_flag & FC_UNLOADING) ||
4593 (phba->link_state < LPFC_LINK_UP))
4594 return;
4595
4596 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
4597
4598 if (rc)
4599 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4600 "2553 lpfc_unregister_unused_fcf failed to read FCF"
4601 " record HBA state x%x\n",
4602 phba->pport->port_state);
4603}
4604
4605/**
4606 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
4607 * @phba: Pointer to hba context object.
4608 * @buff: Buffer containing the FCF connection table as in the config
4609 * region.
4610 * This function create driver data structure for the FCF connection
4611 * record table read from config region 23.
4612 */
4613static void
4614lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
4615 uint8_t *buff)
4616{
4617 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4618 struct lpfc_fcf_conn_hdr *conn_hdr;
4619 struct lpfc_fcf_conn_rec *conn_rec;
4620 uint32_t record_count;
4621 int i;
4622
4623 /* Free the current connect table */
4624 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004625 &phba->fcf_conn_rec_list, list) {
4626 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04004627 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004628 }
James Smart6fb120a2009-05-22 14:52:59 -04004629
4630 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
4631 record_count = conn_hdr->length * sizeof(uint32_t)/
4632 sizeof(struct lpfc_fcf_conn_rec);
4633
4634 conn_rec = (struct lpfc_fcf_conn_rec *)
4635 (buff + sizeof(struct lpfc_fcf_conn_hdr));
4636
4637 for (i = 0; i < record_count; i++) {
4638 if (!(conn_rec[i].flags & FCFCNCT_VALID))
4639 continue;
4640 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
4641 GFP_KERNEL);
4642 if (!conn_entry) {
4643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4644 "2566 Failed to allocate connection"
4645 " table entry\n");
4646 return;
4647 }
4648
4649 memcpy(&conn_entry->conn_rec, &conn_rec[i],
4650 sizeof(struct lpfc_fcf_conn_rec));
4651 conn_entry->conn_rec.vlan_tag =
4652 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
4653 conn_entry->conn_rec.flags =
4654 le16_to_cpu(conn_entry->conn_rec.flags);
4655 list_add_tail(&conn_entry->list,
4656 &phba->fcf_conn_rec_list);
4657 }
4658}
4659
4660/**
4661 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
4662 * @phba: Pointer to hba context object.
4663 * @buff: Buffer containing the FCoE parameter data structure.
4664 *
4665 * This function update driver data structure with config
4666 * parameters read from config region 23.
4667 */
4668static void
4669lpfc_read_fcoe_param(struct lpfc_hba *phba,
4670 uint8_t *buff)
4671{
4672 struct lpfc_fip_param_hdr *fcoe_param_hdr;
4673 struct lpfc_fcoe_params *fcoe_param;
4674
4675 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
4676 buff;
4677 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04004678 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04004679
4680 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
4681 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
4682 return;
4683
James Smart6fb120a2009-05-22 14:52:59 -04004684 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
4685 phba->valid_vlan = 1;
4686 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
4687 0xFFF;
4688 }
4689
4690 phba->fc_map[0] = fcoe_param->fc_map[0];
4691 phba->fc_map[1] = fcoe_param->fc_map[1];
4692 phba->fc_map[2] = fcoe_param->fc_map[2];
4693 return;
4694}
4695
4696/**
4697 * lpfc_get_rec_conf23 - Get a record type in config region data.
4698 * @buff: Buffer containing config region 23 data.
4699 * @size: Size of the data buffer.
4700 * @rec_type: Record type to be searched.
4701 *
4702 * This function searches config region data to find the begining
4703 * of the record specified by record_type. If record found, this
4704 * function return pointer to the record else return NULL.
4705 */
4706static uint8_t *
4707lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
4708{
4709 uint32_t offset = 0, rec_length;
4710
4711 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
4712 (size < sizeof(uint32_t)))
4713 return NULL;
4714
4715 rec_length = buff[offset + 1];
4716
4717 /*
4718 * One TLV record has one word header and number of data words
4719 * specified in the rec_length field of the record header.
4720 */
4721 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
4722 <= size) {
4723 if (buff[offset] == rec_type)
4724 return &buff[offset];
4725
4726 if (buff[offset] == LPFC_REGION23_LAST_REC)
4727 return NULL;
4728
4729 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
4730 rec_length = buff[offset + 1];
4731 }
4732 return NULL;
4733}
4734
4735/**
4736 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
4737 * @phba: Pointer to lpfc_hba data structure.
4738 * @buff: Buffer containing config region 23 data.
4739 * @size: Size of the data buffer.
4740 *
4741 * This fuction parse the FCoE config parameters in config region 23 and
4742 * populate driver data structure with the parameters.
4743 */
4744void
4745lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
4746 uint8_t *buff,
4747 uint32_t size)
4748{
4749 uint32_t offset = 0, rec_length;
4750 uint8_t *rec_ptr;
4751
4752 /*
4753 * If data size is less than 2 words signature and version cannot be
4754 * verified.
4755 */
4756 if (size < 2*sizeof(uint32_t))
4757 return;
4758
4759 /* Check the region signature first */
4760 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
4761 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4762 "2567 Config region 23 has bad signature\n");
4763 return;
4764 }
4765
4766 offset += 4;
4767
4768 /* Check the data structure version */
4769 if (buff[offset] != LPFC_REGION23_VERSION) {
4770 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4771 "2568 Config region 23 has bad version\n");
4772 return;
4773 }
4774 offset += 4;
4775
4776 rec_length = buff[offset + 1];
4777
4778 /* Read FCoE param record */
4779 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4780 size - offset, FCOE_PARAM_TYPE);
4781 if (rec_ptr)
4782 lpfc_read_fcoe_param(phba, rec_ptr);
4783
4784 /* Read FCF connection table */
4785 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4786 size - offset, FCOE_CONN_TBL_TYPE);
4787 if (rec_ptr)
4788 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
4789
4790}