blob: b890e2dc15db07db5bda9e5c92c6154fec10b48c [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/kthread.h>
25#include <linux/interrupt.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_device.h>
29#include <scsi/scsi_host.h>
30#include <scsi/scsi_transport_fc.h>
31
James Smartda0436e2009-05-22 14:51:39 -040032#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050033#include "lpfc_hw.h"
James Smartea2151b2008-09-07 11:52:10 -040034#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050035#include "lpfc_disc.h"
36#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_sli4.h"
dea31012005-04-17 16:05:31 -050038#include "lpfc_scsi.h"
39#include "lpfc.h"
40#include "lpfc_logmsg.h"
41#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050042#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050043#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050044
45/* AlpaArray for assignment of scsid for scan-down and bind_method */
46static uint8_t lpfcAlpaArray[] = {
47 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
48 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
49 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
50 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
51 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
52 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
53 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
54 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
55 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
56 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
57 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
58 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
59 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
60};
61
James Smart2e0fef82007-06-17 19:56:36 -050062static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010063static void lpfc_disc_flush_list(struct lpfc_vport *vport);
James Smart32b97932009-07-19 10:01:21 -040064static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
dea31012005-04-17 16:05:31 -050065
James Smartc01f3202006-08-18 17:47:08 -040066void
67lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050068{
James Smartc01f3202006-08-18 17:47:08 -040069 struct lpfc_rport_data *rdata;
70 struct lpfc_nodelist * ndlp;
71 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050072
James Smartc01f3202006-08-18 17:47:08 -040073 rdata = rport->dd_data;
74 ndlp = rdata->pnode;
75
James Smart58da1ff2008-04-07 10:15:56 -040076 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040077 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
78 printk(KERN_ERR "Cannot find remote node"
79 " to terminate I/O Data x%x\n",
80 rport->port_id);
dea31012005-04-17 16:05:31 -050081 return;
82 }
83
James Smarta257bf92009-04-06 18:48:10 -040084 phba = ndlp->phba;
James Smart1a169682006-03-07 15:04:06 -050085
James Smart858c9f62007-06-17 19:56:39 -050086 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
87 "rport terminate: sid:x%x did:x%x flg:x%x",
88 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
89
James Smartc01f3202006-08-18 17:47:08 -040090 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040091 lpfc_sli_abort_iocb(ndlp->vport,
92 &phba->sli.ring[phba->sli.fcp_ring],
93 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040094 }
James Smartc01f3202006-08-18 17:47:08 -040095}
96
97/*
98 * This function will be called when dev_loss_tmo fire.
99 */
100void
101lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
102{
103 struct lpfc_rport_data *rdata;
104 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500105 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500106 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500107 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400108 int put_node;
109 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400110
111 rdata = rport->dd_data;
112 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400113 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400114 return;
James Smartc01f3202006-08-18 17:47:08 -0400115
James Smart858c9f62007-06-17 19:56:39 -0500116 vport = ndlp->vport;
117 phba = vport->phba;
118
119 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
120 "rport devlosscb: sid:x%x did:x%x flg:x%x",
121 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
122
James Smarta8adb832007-10-27 13:37:53 -0400123 /* Don't defer this if we are in the process of deleting the vport
124 * or unloading the driver. The unload will cleanup the node
125 * appropriately we just need to cleanup the ndlp rport info here.
126 */
127 if (vport->load_flag & FC_UNLOADING) {
128 put_node = rdata->pnode != NULL;
129 put_rport = ndlp->rport != NULL;
130 rdata->pnode = NULL;
131 ndlp->rport = NULL;
132 if (put_node)
133 lpfc_nlp_put(ndlp);
134 if (put_rport)
135 put_device(&rport->dev);
136 return;
137 }
138
139 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
140 return;
141
James Smart858c9f62007-06-17 19:56:39 -0500142 evtp = &ndlp->dev_loss_evt;
143
144 if (!list_empty(&evtp->evt_listp))
145 return;
146
147 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500148 /* We need to hold the node by incrementing the reference
149 * count until this queued work is done
150 */
151 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart5e9d9b82008-06-14 22:52:53 -0400152 if (evtp->evt_arg1) {
153 evtp->evt = LPFC_EVT_DEV_LOSS;
154 list_add_tail(&evtp->evt_listp, &phba->work_list);
155 lpfc_worker_wake_up(phba);
156 }
James Smart858c9f62007-06-17 19:56:39 -0500157 spin_unlock_irq(&phba->hbalock);
158
James Smart858c9f62007-06-17 19:56:39 -0500159 return;
160}
161
162/*
163 * This function is called from the worker thread when dev_loss_tmo
164 * expire.
165 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100166static void
James Smart858c9f62007-06-17 19:56:39 -0500167lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
168{
169 struct lpfc_rport_data *rdata;
170 struct fc_rport *rport;
171 struct lpfc_vport *vport;
172 struct lpfc_hba *phba;
173 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400174 int put_node;
175 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500176 int warn_on = 0;
177
178 rport = ndlp->rport;
179
180 if (!rport)
181 return;
182
183 rdata = rport->dd_data;
184 name = (uint8_t *) &ndlp->nlp_portname;
185 vport = ndlp->vport;
186 phba = vport->phba;
187
188 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
189 "rport devlosstmo:did:x%x type:x%x id:x%x",
190 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
191
James Smarta8adb832007-10-27 13:37:53 -0400192 /* Don't defer this if we are in the process of deleting the vport
193 * or unloading the driver. The unload will cleanup the node
194 * appropriately we just need to cleanup the ndlp rport info here.
195 */
196 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500197 if (ndlp->nlp_sid != NLP_NO_SID) {
198 /* flush the target */
199 lpfc_sli_abort_iocb(vport,
200 &phba->sli.ring[phba->sli.fcp_ring],
201 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
202 }
James Smarta8adb832007-10-27 13:37:53 -0400203 put_node = rdata->pnode != NULL;
204 put_rport = ndlp->rport != NULL;
205 rdata->pnode = NULL;
206 ndlp->rport = NULL;
207 if (put_node)
208 lpfc_nlp_put(ndlp);
209 if (put_rport)
210 put_device(&rport->dev);
211 return;
212 }
213
James Smartd7c255b2008-08-24 21:50:00 -0400214 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
215 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
216 "0284 Devloss timeout Ignored on "
217 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
218 "NPort x%x\n",
219 *name, *(name+1), *(name+2), *(name+3),
220 *(name+4), *(name+5), *(name+6), *(name+7),
221 ndlp->nlp_DID);
James Smart858c9f62007-06-17 19:56:39 -0500222 return;
James Smartd7c255b2008-08-24 21:50:00 -0400223 }
James Smart858c9f62007-06-17 19:56:39 -0500224
James Smart92d7f7b2007-06-17 19:56:38 -0500225 if (ndlp->nlp_type & NLP_FABRIC) {
226 /* We will clean up these Nodes in linkup */
227 put_node = rdata->pnode != NULL;
228 put_rport = ndlp->rport != NULL;
229 rdata->pnode = NULL;
230 ndlp->rport = NULL;
231 if (put_node)
232 lpfc_nlp_put(ndlp);
233 if (put_rport)
234 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400235 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500236 }
James Smart82085712007-04-25 09:52:41 -0400237
dea31012005-04-17 16:05:31 -0500238 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400239 warn_on = 1;
dea31012005-04-17 16:05:31 -0500240 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400241 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
242 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500243 }
James Smartc01f3202006-08-18 17:47:08 -0400244
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400245 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400246 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
247 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400248 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
249 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400250 *name, *(name+1), *(name+2), *(name+3),
251 *(name+4), *(name+5), *(name+6), *(name+7),
252 ndlp->nlp_DID, ndlp->nlp_flag,
253 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400254 } else {
James Smarte8b62012007-08-02 11:10:09 -0400255 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
256 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400257 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
258 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400259 *name, *(name+1), *(name+2), *(name+3),
260 *(name+4), *(name+5), *(name+6), *(name+7),
261 ndlp->nlp_DID, ndlp->nlp_flag,
262 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400263 }
264
James Smart87af33f2007-10-27 13:37:43 -0400265 put_node = rdata->pnode != NULL;
266 put_rport = ndlp->rport != NULL;
267 rdata->pnode = NULL;
268 ndlp->rport = NULL;
269 if (put_node)
270 lpfc_nlp_put(ndlp);
271 if (put_rport)
272 put_device(&rport->dev);
273
James Smart2e0fef82007-06-17 19:56:36 -0500274 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400275 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400276 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smarte47c9092008-02-08 18:49:26 -0500277 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
James Smart2e0fef82007-06-17 19:56:36 -0500278 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400279
280 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500281}
James Smartc01f3202006-08-18 17:47:08 -0400282
James Smartea2151b2008-09-07 11:52:10 -0400283/**
James Smart3621a712009-04-06 18:47:14 -0400284 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400285 * @phba: Pointer to hba context object.
286 *
287 * This function is called from the functions which need to post
288 * events from interrupt context. This function allocates data
289 * structure required for posting event. It also keeps track of
290 * number of events pending and prevent event storm when there are
291 * too many events.
292 **/
293struct lpfc_fast_path_event *
294lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
295 struct lpfc_fast_path_event *ret;
296
297 /* If there are lot of fast event do not exhaust memory due to this */
298 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
299 return NULL;
300
301 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
302 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400303 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400304 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400305 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
306 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
307 }
James Smartea2151b2008-09-07 11:52:10 -0400308 return ret;
309}
310
311/**
James Smart3621a712009-04-06 18:47:14 -0400312 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400313 * @phba: Pointer to hba context object.
314 * @evt: Event object which need to be freed.
315 *
316 * This function frees the data structure required for posting
317 * events.
318 **/
319void
320lpfc_free_fast_evt(struct lpfc_hba *phba,
321 struct lpfc_fast_path_event *evt) {
322
323 atomic_dec(&phba->fast_event_count);
324 kfree(evt);
325}
326
327/**
James Smart3621a712009-04-06 18:47:14 -0400328 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400329 * @phba: Pointer to hba context object.
330 * @evtp: Event data structure.
331 *
332 * This function is called from worker thread, when the interrupt
333 * context need to post an event. This function posts the event
334 * to fc transport netlink interface.
335 **/
336static void
337lpfc_send_fastpath_evt(struct lpfc_hba *phba,
338 struct lpfc_work_evt *evtp)
339{
340 unsigned long evt_category, evt_sub_category;
341 struct lpfc_fast_path_event *fast_evt_data;
342 char *evt_data;
343 uint32_t evt_data_size;
344 struct Scsi_Host *shost;
345
346 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
347 work_evt);
348
349 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
350 evt_sub_category = (unsigned long) fast_evt_data->un.
351 fabric_evt.subcategory;
352 shost = lpfc_shost_from_vport(fast_evt_data->vport);
353 if (evt_category == FC_REG_FABRIC_EVENT) {
354 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
355 evt_data = (char *) &fast_evt_data->un.read_check_error;
356 evt_data_size = sizeof(fast_evt_data->un.
357 read_check_error);
358 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500359 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400360 evt_data = (char *) &fast_evt_data->un.fabric_evt;
361 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
362 } else {
363 lpfc_free_fast_evt(phba, fast_evt_data);
364 return;
365 }
366 } else if (evt_category == FC_REG_SCSI_EVENT) {
367 switch (evt_sub_category) {
368 case LPFC_EVENT_QFULL:
369 case LPFC_EVENT_DEVBSY:
370 evt_data = (char *) &fast_evt_data->un.scsi_evt;
371 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
372 break;
373 case LPFC_EVENT_CHECK_COND:
374 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
375 evt_data_size = sizeof(fast_evt_data->un.
376 check_cond_evt);
377 break;
378 case LPFC_EVENT_VARQUEDEPTH:
379 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
380 evt_data_size = sizeof(fast_evt_data->un.
381 queue_depth_evt);
382 break;
383 default:
384 lpfc_free_fast_evt(phba, fast_evt_data);
385 return;
386 }
387 } else {
388 lpfc_free_fast_evt(phba, fast_evt_data);
389 return;
390 }
391
392 fc_host_post_vendor_event(shost,
393 fc_get_event_number(),
394 evt_data_size,
395 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500396 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400397
398 lpfc_free_fast_evt(phba, fast_evt_data);
399 return;
400}
401
dea31012005-04-17 16:05:31 -0500402static void
James Smart2e0fef82007-06-17 19:56:36 -0500403lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500404{
405 struct lpfc_work_evt *evtp = NULL;
406 struct lpfc_nodelist *ndlp;
407 int free_evt;
408
James Smart2e0fef82007-06-17 19:56:36 -0500409 spin_lock_irq(&phba->hbalock);
410 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500411 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
412 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500413 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500414 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500415 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500416 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500417 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500418 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500419 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500420 /* decrement the node reference count held
421 * for this queued work
422 */
423 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500424 break;
James Smart858c9f62007-06-17 19:56:39 -0500425 case LPFC_EVT_DEV_LOSS:
426 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500427 lpfc_dev_loss_tmo_handler(ndlp);
428 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500429 /* decrement the node reference count held for
430 * this queued work
431 */
James Smart858c9f62007-06-17 19:56:39 -0500432 lpfc_nlp_put(ndlp);
433 break;
dea31012005-04-17 16:05:31 -0500434 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500435 if (phba->link_state < LPFC_LINK_DOWN)
436 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500437 else
James Smart2e0fef82007-06-17 19:56:36 -0500438 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500439 complete((struct completion *)(evtp->evt_arg2));
440 break;
James Smart46fa3112007-04-25 09:51:45 -0400441 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500442 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400443 lpfc_offline_prep(phba);
444 *(int *)(evtp->evt_arg1) = 0;
445 complete((struct completion *)(evtp->evt_arg2));
446 break;
447 case LPFC_EVT_OFFLINE:
448 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500449 lpfc_sli_brdrestart(phba);
450 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400451 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
452 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500453 complete((struct completion *)(evtp->evt_arg2));
454 break;
455 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400456 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500457 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500458 lpfc_sli_brdreset(phba);
459 lpfc_hba_down_post(phba);
460 *(int *)(evtp->evt_arg1) =
461 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400462 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500463 complete((struct completion *)(evtp->evt_arg2));
464 break;
465 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400466 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500467 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500468 = (phba->pport->stopped)
469 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400470 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500471 complete((struct completion *)(evtp->evt_arg2));
472 break;
James Smartea2151b2008-09-07 11:52:10 -0400473 case LPFC_EVT_FASTPATH_MGMT_EVT:
474 lpfc_send_fastpath_evt(phba, evtp);
475 free_evt = 0;
476 break;
dea31012005-04-17 16:05:31 -0500477 }
478 if (free_evt)
479 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500480 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500481 }
James Smart2e0fef82007-06-17 19:56:36 -0500482 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500483
484}
485
James Smart311464e2007-08-02 11:10:37 -0400486static void
James Smart2e0fef82007-06-17 19:56:36 -0500487lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500488{
489 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500490 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400491 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400492 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400493 int i;
dea31012005-04-17 16:05:31 -0500494
James Smart2e0fef82007-06-17 19:56:36 -0500495 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500496 ha_copy = phba->work_ha;
497 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500498 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500499
James Smartda0436e2009-05-22 14:51:39 -0400500 /* First, try to post the next mailbox command to SLI4 device */
501 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
502 lpfc_sli4_post_async_mbox(phba);
503
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500504 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400505 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500506 lpfc_handle_eratt(phba);
507
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500508 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500509 lpfc_sli_handle_mb_event(phba);
510
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500511 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500512 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400513
James Smartda0436e2009-05-22 14:51:39 -0400514 /* Process SLI4 events */
515 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
516 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
517 lpfc_sli4_fcp_xri_abort_event_proc(phba);
518 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
519 lpfc_sli4_els_xri_abort_event_proc(phba);
520 if (phba->hba_flag & ASYNC_EVENT)
521 lpfc_sli4_async_event_proc(phba);
522 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
523 spin_lock_irq(&phba->hbalock);
524 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
525 spin_unlock_irq(&phba->hbalock);
526 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
527 }
James Smartecfd03c2010-02-12 14:41:27 -0500528 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
529 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400530 }
531
James Smart549e55c2007-08-02 11:09:51 -0400532 vports = lpfc_create_vport_work_array(phba);
533 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400534 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400535 /*
536 * We could have no vports in array if unloading, so if
537 * this happens then just use the pport
538 */
539 if (vports[i] == NULL && i == 0)
540 vport = phba->pport;
541 else
542 vport = vports[i];
543 if (vport == NULL)
544 break;
James Smart58da1ff2008-04-07 10:15:56 -0400545 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400546 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400547 vport->work_port_events &= ~work_port_events;
548 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400549 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400550 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400551 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400552 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400553 if (work_port_events & WORKER_HB_TMO)
554 lpfc_hb_timeout_handler(phba);
555 if (work_port_events & WORKER_MBOX_TMO)
556 lpfc_mbox_timeout_handler(phba);
557 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
558 lpfc_unblock_fabric_iocbs(phba);
559 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400560 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400561 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
562 lpfc_ramp_down_queue_handler(phba);
563 if (work_port_events & WORKER_RAMP_UP_QUEUE)
564 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500565 }
James Smart09372822008-01-11 01:52:54 -0500566 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500567
James Smart858c9f62007-06-17 19:56:39 -0500568 pring = &phba->sli.ring[LPFC_ELS_RING];
569 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
570 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400571 if ((status & HA_RXMASK) ||
572 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400573 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400574 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500575 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400576 /* Set the lpfc data pending flag */
577 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500578 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400579 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500580 lpfc_sli_handle_slow_ring_event(phba, pring,
581 (status &
582 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500583 }
584 /*
585 * Turn on Ring interrupts
586 */
James Smart3772a992009-05-22 14:50:54 -0400587 if (phba->sli_rev <= LPFC_SLI_REV3) {
588 spin_lock_irq(&phba->hbalock);
589 control = readl(phba->HCregaddr);
590 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
591 lpfc_debugfs_slow_ring_trc(phba,
592 "WRK Enable ring: cntl:x%x hacopy:x%x",
593 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400594
James Smart3772a992009-05-22 14:50:54 -0400595 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
596 writel(control, phba->HCregaddr);
597 readl(phba->HCregaddr); /* flush */
598 } else {
599 lpfc_debugfs_slow_ring_trc(phba,
600 "WRK Ring ok: cntl:x%x hacopy:x%x",
601 control, ha_copy, 0);
602 }
603 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500604 }
605 }
James Smart2e0fef82007-06-17 19:56:36 -0500606 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500607}
608
dea31012005-04-17 16:05:31 -0500609int
610lpfc_do_work(void *p)
611{
612 struct lpfc_hba *phba = p;
613 int rc;
dea31012005-04-17 16:05:31 -0500614
615 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400616 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500617
James Smart3a55b532008-12-04 22:38:54 -0500618 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400619 /* wait and check worker queue activities */
620 rc = wait_event_interruptible(phba->work_waitq,
621 (test_and_clear_bit(LPFC_DATA_READY,
622 &phba->data_flags)
623 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500624 /* Signal wakeup shall terminate the worker thread */
625 if (rc) {
626 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
627 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500628 break;
James Smart3a55b532008-12-04 22:38:54 -0500629 }
dea31012005-04-17 16:05:31 -0500630
James Smart5e9d9b82008-06-14 22:52:53 -0400631 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500632 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500633 }
James Smart3a55b532008-12-04 22:38:54 -0500634 phba->worker_thread = NULL;
635 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
636 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500637 return 0;
638}
639
640/*
641 * This is only called to handle FC worker events. Since this a rare
642 * occurance, we allocate a struct lpfc_work_evt structure here instead of
643 * embedding it in the IOCB.
644 */
645int
James Smart2e0fef82007-06-17 19:56:36 -0500646lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500647 uint32_t evt)
648{
649 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500650 unsigned long flags;
dea31012005-04-17 16:05:31 -0500651
652 /*
653 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
654 * be queued to worker thread for processing
655 */
James Smart92d7f7b2007-06-17 19:56:38 -0500656 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500657 if (!evtp)
658 return 0;
659
660 evtp->evt_arg1 = arg1;
661 evtp->evt_arg2 = arg2;
662 evtp->evt = evt;
663
James Smarted957682007-06-17 19:56:37 -0500664 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400665 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500666 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500667
James Smart5e9d9b82008-06-14 22:52:53 -0400668 lpfc_worker_wake_up(phba);
669
dea31012005-04-17 16:05:31 -0500670 return 1;
671}
672
James Smart92d7f7b2007-06-17 19:56:38 -0500673void
674lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
675{
James Smart09372822008-01-11 01:52:54 -0500676 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500677 struct lpfc_hba *phba = vport->phba;
678 struct lpfc_nodelist *ndlp, *next_ndlp;
679 int rc;
680
681 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500682 if (!NLP_CHK_NODE_ACT(ndlp))
683 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500684 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
685 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400686 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
687 ((vport->port_type == LPFC_NPIV_PORT) &&
688 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500689 lpfc_unreg_rpi(vport, ndlp);
690
691 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400692 if ((phba->sli_rev < LPFC_SLI_REV4) &&
693 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500694 continue;
695 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
696 remove
697 ? NLP_EVT_DEVICE_RM
698 : NLP_EVT_DEVICE_RECOVERY);
699 }
700 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
701 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500702 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500703 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500704 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500705 }
706}
707
James Smart87af33f2007-10-27 13:37:43 -0400708void
James Smart98c9ea52007-10-27 13:37:33 -0400709lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500710{
James Smart695a8142010-01-26 23:08:03 -0500711 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
712
James Smart45ed1192009-10-02 15:17:02 -0400713 /* Cleanup any outstanding received buffers */
714 lpfc_cleanup_rcv_buffers(vport);
715
James Smart92d7f7b2007-06-17 19:56:38 -0500716 /* Cleanup any outstanding RSCN activity */
717 lpfc_els_flush_rscn(vport);
718
719 /* Cleanup any outstanding ELS commands */
720 lpfc_els_flush_cmd(vport);
721
722 lpfc_cleanup_rpis(vport, 0);
723
James Smart92d7f7b2007-06-17 19:56:38 -0500724 /* Turn off discovery timer if its running */
725 lpfc_can_disctmo(vport);
726}
727
James Smart3772a992009-05-22 14:50:54 -0400728void
James Smart98c9ea52007-10-27 13:37:33 -0400729lpfc_linkdown_port(struct lpfc_vport *vport)
730{
731 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
732
733 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
734
735 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
736 "Link Down: state:x%x rtry:x%x flg:x%x",
737 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
738
739 lpfc_port_link_failure(vport);
740
741}
742
dea31012005-04-17 16:05:31 -0500743int
James Smart685f0bf2007-04-25 09:53:08 -0400744lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500745{
James Smart2e0fef82007-06-17 19:56:36 -0500746 struct lpfc_vport *vport = phba->pport;
747 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400748 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400749 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400750 int i;
dea31012005-04-17 16:05:31 -0500751
James Smart3163f722008-02-08 18:50:25 -0500752 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500753 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500754
755 /* Block all SCSI stack I/Os */
756 lpfc_scsi_dev_block(phba);
757
James Smart2e0fef82007-06-17 19:56:36 -0500758 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500759 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart92d7f7b2007-06-17 19:56:38 -0500760 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500761 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500762 phba->pport->fc_flag &= ~FC_LBIT;
763 }
James Smart2e0fef82007-06-17 19:56:36 -0500764 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400765 vports = lpfc_create_vport_work_array(phba);
766 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400767 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400768 /* Issue a LINK DOWN event to all nodes */
769 lpfc_linkdown_port(vports[i]);
770 }
James Smart09372822008-01-11 01:52:54 -0500771 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500772 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500773 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
774 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500775 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500776 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500777 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400778 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500779 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500780 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500781 }
782 }
783
dea31012005-04-17 16:05:31 -0500784 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500785 if (phba->pport->fc_flag & FC_PT2PT) {
786 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500787 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
788 if (mb) {
dea31012005-04-17 16:05:31 -0500789 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500790 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500791 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400792 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500793 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500794 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500795 }
796 }
James Smart2e0fef82007-06-17 19:56:36 -0500797 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500798 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500799 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500800 }
James Smart2e0fef82007-06-17 19:56:36 -0500801
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500802 return 0;
dea31012005-04-17 16:05:31 -0500803}
804
James Smart92d7f7b2007-06-17 19:56:38 -0500805static void
806lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500807{
James Smart92d7f7b2007-06-17 19:56:38 -0500808 struct lpfc_nodelist *ndlp;
809
810 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500811 if (!NLP_CHK_NODE_ACT(ndlp))
812 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500813 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
814 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500815 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500816 /* On Linkup its safe to clean up the ndlp
817 * from Fabric connections.
818 */
James Smart92d7f7b2007-06-17 19:56:38 -0500819 if (ndlp->nlp_DID != Fabric_DID)
820 lpfc_unreg_rpi(vport, ndlp);
821 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
822 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500823 /* Fail outstanding IO now since device is
824 * marked for PLOGI.
825 */
James Smart92d7f7b2007-06-17 19:56:38 -0500826 lpfc_unreg_rpi(vport, ndlp);
827 }
828 }
829}
830
831static void
832lpfc_linkup_port(struct lpfc_vport *vport)
833{
834 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500835 struct lpfc_hba *phba = vport->phba;
836
837 if ((vport->load_flag & FC_UNLOADING) != 0)
838 return;
839
James Smart858c9f62007-06-17 19:56:39 -0500840 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
841 "Link Up: top:x%x speed:x%x flg:x%x",
842 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
843
James Smart92d7f7b2007-06-17 19:56:38 -0500844 /* If NPIV is not enabled, only bring the physical port up */
845 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
846 (vport != phba->pport))
847 return;
dea31012005-04-17 16:05:31 -0500848
James Smart2e0fef82007-06-17 19:56:36 -0500849 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400850
James Smart2e0fef82007-06-17 19:56:36 -0500851 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500852 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
853 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
854 vport->fc_flag |= FC_NDISC_ACTIVE;
855 vport->fc_ns_retry = 0;
856 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500857
James Smart92d7f7b2007-06-17 19:56:38 -0500858 if (vport->fc_flag & FC_LBIT)
859 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500860
James Smart92d7f7b2007-06-17 19:56:38 -0500861}
862
863static int
864lpfc_linkup(struct lpfc_hba *phba)
865{
James Smart549e55c2007-08-02 11:09:51 -0400866 struct lpfc_vport **vports;
867 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500868
869 phba->link_state = LPFC_LINK_UP;
870
871 /* Unblock fabric iocbs if they are blocked */
872 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
873 del_timer_sync(&phba->fabric_block_timer);
874
James Smart549e55c2007-08-02 11:09:51 -0400875 vports = lpfc_create_vport_work_array(phba);
876 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400877 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400878 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500879 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400880 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
881 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500882 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500883
884 return 0;
885}
886
887/*
888 * This routine handles processing a CLEAR_LA mailbox
889 * command upon completion. It is setup in the LPFC_MBOXQ
890 * as the completion routine when the command is
891 * handed off to the SLI layer.
892 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100893static void
James Smart2e0fef82007-06-17 19:56:36 -0500894lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500895{
James Smart2e0fef82007-06-17 19:56:36 -0500896 struct lpfc_vport *vport = pmb->vport;
897 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
898 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400899 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500900 uint32_t control;
901
dea31012005-04-17 16:05:31 -0500902 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500903 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500904 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
905 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
906
907 /* Check for error */
908 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500909 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400910 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
911 "0320 CLEAR_LA mbxStatus error x%x hba "
912 "state x%x\n",
913 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500914 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500915 goto out;
916 }
917
James Smart92d7f7b2007-06-17 19:56:38 -0500918 if (vport->port_type == LPFC_PHYSICAL_PORT)
919 phba->link_state = LPFC_HBA_READY;
920
921 spin_lock_irq(&phba->hbalock);
922 psli->sli_flag |= LPFC_PROCESS_LA;
923 control = readl(phba->HCregaddr);
924 control |= HC_LAINT_ENA;
925 writel(control, phba->HCregaddr);
926 readl(phba->HCregaddr); /* flush */
927 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500928 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500929 return;
dea31012005-04-17 16:05:31 -0500930
dea31012005-04-17 16:05:31 -0500931out:
932 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400933 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
934 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500935 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500936
James Smart2e0fef82007-06-17 19:56:36 -0500937 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400938 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500939 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500940
James Smart2e0fef82007-06-17 19:56:36 -0500941 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500942
943 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500944
945 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500946 psli->sli_flag |= LPFC_PROCESS_LA;
947 control = readl(phba->HCregaddr);
948 control |= HC_LAINT_ENA;
949 writel(control, phba->HCregaddr);
950 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500951 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500952
953 return;
954}
955
James Smart2e0fef82007-06-17 19:56:36 -0500956
dea31012005-04-17 16:05:31 -0500957static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500958lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500959{
James Smart2e0fef82007-06-17 19:56:36 -0500960 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500961
James Smart04c68492009-05-22 14:52:52 -0400962 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500963 goto out;
dea31012005-04-17 16:05:31 -0500964
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500965 mempool_free(pmb, phba->mbox_mem_pool);
966
967 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500968 vport->fc_flag & FC_PUBLIC_LOOP &&
969 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500970 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500971 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500972 * LPFC_LOCAL_CFG_LINK while waiting for FAN
973 */
James Smart2e0fef82007-06-17 19:56:36 -0500974 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500975 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500976 }
dea31012005-04-17 16:05:31 -0500977
James Smart2e0fef82007-06-17 19:56:36 -0500978 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500979 * LPFC_FLOGI while waiting for FLOGI cmpl
980 */
James Smart92d7f7b2007-06-17 19:56:38 -0500981 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500982 lpfc_initial_flogi(vport);
983 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500984 return;
dea31012005-04-17 16:05:31 -0500985
986out:
James Smarte8b62012007-08-02 11:10:09 -0400987 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
988 "0306 CONFIG_LINK mbxStatus error x%x "
989 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400990 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500991 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500992
993 lpfc_linkdown(phba);
994
James Smarte8b62012007-08-02 11:10:09 -0400995 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
996 "0200 CONFIG_LINK bad hba state x%x\n",
997 vport->port_state);
dea31012005-04-17 16:05:31 -0500998
James Smart92d7f7b2007-06-17 19:56:38 -0500999 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001000 return;
1001}
1002
1003static void
James Smart6fb120a2009-05-22 14:52:59 -04001004lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1005{
1006 struct lpfc_vport *vport = mboxq->vport;
1007 unsigned long flags;
1008
1009 if (mboxq->u.mb.mbxStatus) {
1010 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1011 "2017 REG_FCFI mbxStatus error x%x "
1012 "HBA state x%x\n",
1013 mboxq->u.mb.mbxStatus, vport->port_state);
1014 mempool_free(mboxq, phba->mbox_mem_pool);
1015 return;
1016 }
1017
1018 /* Start FCoE discovery by sending a FLOGI. */
1019 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1020 /* Set the FCFI registered flag */
1021 spin_lock_irqsave(&phba->hbalock, flags);
1022 phba->fcf.fcf_flag |= FCF_REGISTERED;
1023 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart32b97932009-07-19 10:01:21 -04001024 /* If there is a pending FCoE event, restart FCF table scan. */
1025 if (lpfc_check_pending_fcoe_event(phba, 1)) {
1026 mempool_free(mboxq, phba->mbox_mem_pool);
1027 return;
1028 }
James Smart1c6f4ef52009-11-18 15:40:49 -05001029 spin_lock_irqsave(&phba->hbalock, flags);
James Smartecfd03c2010-02-12 14:41:27 -05001030 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart4d9ab992009-10-02 15:16:39 -04001031 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001032 spin_unlock_irqrestore(&phba->hbalock, flags);
1033 if (vport->port_state != LPFC_FLOGI)
James Smart6fb120a2009-05-22 14:52:59 -04001034 lpfc_initial_flogi(vport);
James Smart6fb120a2009-05-22 14:52:59 -04001035
1036 mempool_free(mboxq, phba->mbox_mem_pool);
1037 return;
1038}
1039
1040/**
1041 * lpfc_fab_name_match - Check if the fcf fabric name match.
1042 * @fab_name: pointer to fabric name.
1043 * @new_fcf_record: pointer to fcf record.
1044 *
1045 * This routine compare the fcf record's fabric name with provided
1046 * fabric name. If the fabric name are identical this function
1047 * returns 1 else return 0.
1048 **/
1049static uint32_t
1050lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1051{
James Smartecfd03c2010-02-12 14:41:27 -05001052 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001053 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001054 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1055 return 0;
1056 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1057 return 0;
1058 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1059 return 0;
1060 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1061 return 0;
1062 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1063 return 0;
1064 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1065 return 0;
1066 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1067 return 0;
1068 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001069}
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{
James Smartecfd03c2010-02-12 14:41:27 -05001083 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001084 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001085 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1086 return 0;
1087 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1088 return 0;
1089 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1090 return 0;
1091 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1092 return 0;
1093 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1094 return 0;
1095 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1096 return 0;
1097 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1098 return 0;
1099 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001100}
1101
1102/**
James Smart6fb120a2009-05-22 14:52:59 -04001103 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001104 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001105 * @new_fcf_record: pointer to fcf record.
1106 *
1107 * This routine compare the fcf record's mac address with HBA's
1108 * FCF mac address. If the mac addresses are identical this function
1109 * returns 1 else return 0.
1110 **/
1111static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001112lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001113{
James Smartecfd03c2010-02-12 14:41:27 -05001114 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001115 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001116 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1117 return 0;
1118 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1119 return 0;
1120 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1121 return 0;
1122 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1123 return 0;
1124 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1125 return 0;
1126 return 1;
1127}
1128
1129static bool
1130lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1131{
1132 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001133}
1134
1135/**
1136 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001137 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001138 * @new_fcf_record: pointer to fcf record.
1139 *
1140 * This routine copies the FCF information from the FCF
1141 * record to lpfc_hba data structure.
1142 **/
1143static void
James Smartecfd03c2010-02-12 14:41:27 -05001144lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1145 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001146{
James Smartecfd03c2010-02-12 14:41:27 -05001147 /* Fabric name */
1148 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001149 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001150 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001151 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001152 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001153 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001154 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001155 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001156 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001157 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001158 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001159 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001160 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001161 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001162 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001163 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001164 /* Mac address */
1165 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1166 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1167 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1168 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1169 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1170 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1171 /* FCF record index */
1172 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1173 /* FCF record priority */
1174 fcf_rec->priority = new_fcf_record->fip_priority;
1175 /* Switch name */
1176 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001177 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001178 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001179 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001180 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001181 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001182 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001183 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001184 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001185 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001186 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001187 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001188 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001189 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001190 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001191 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001192}
1193
1194/**
James Smartecfd03c2010-02-12 14:41:27 -05001195 * lpfc_update_fcf_record - Update driver fcf record
1196 * @phba: pointer to lpfc hba data structure.
1197 * @fcf_rec: pointer to driver fcf record.
1198 * @new_fcf_record: pointer to hba fcf record.
1199 * @addr_mode: address mode to be set to the driver fcf record.
1200 * @vlan_id: vlan tag to be set to the driver fcf record.
1201 * @flag: flag bits to be set to the driver fcf record.
1202 *
1203 * This routine updates the driver FCF record from the new HBA FCF record
1204 * together with the address mode, vlan_id, and other informations. This
1205 * routine is called with the host lock held.
1206 **/
1207static void
1208__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1209 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1210 uint16_t vlan_id, uint32_t flag)
1211{
1212 /* Copy the fields from the HBA's FCF record */
1213 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1214 /* Update other fields of driver FCF record */
1215 fcf_rec->addr_mode = addr_mode;
1216 fcf_rec->vlan_id = vlan_id;
1217 fcf_rec->flag |= (flag | RECORD_VALID);
1218}
1219
1220/**
James Smart6fb120a2009-05-22 14:52:59 -04001221 * lpfc_register_fcf - Register the FCF with hba.
1222 * @phba: pointer to lpfc hba data structure.
1223 *
1224 * This routine issues a register fcfi mailbox command to register
1225 * the fcf with HBA.
1226 **/
1227static void
1228lpfc_register_fcf(struct lpfc_hba *phba)
1229{
1230 LPFC_MBOXQ_t *fcf_mbxq;
1231 int rc;
1232 unsigned long flags;
1233
1234 spin_lock_irqsave(&phba->hbalock, flags);
1235
1236 /* If the FCF is not availabe do nothing. */
1237 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smart4d9ab992009-10-02 15:16:39 -04001238 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001239 spin_unlock_irqrestore(&phba->hbalock, flags);
1240 return;
1241 }
1242
1243 /* The FCF is already registered, start discovery */
1244 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001245 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smart32b97932009-07-19 10:01:21 -04001246 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001247 spin_unlock_irqrestore(&phba->hbalock, flags);
1248 if (phba->pport->port_state != LPFC_FLOGI)
1249 lpfc_initial_flogi(phba->pport);
1250 return;
1251 }
1252 spin_unlock_irqrestore(&phba->hbalock, flags);
1253
1254 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1255 GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001256 if (!fcf_mbxq) {
1257 spin_lock_irqsave(&phba->hbalock, flags);
1258 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1259 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001260 return;
James Smart4d9ab992009-10-02 15:16:39 -04001261 }
James Smart6fb120a2009-05-22 14:52:59 -04001262
1263 lpfc_reg_fcfi(phba, fcf_mbxq);
1264 fcf_mbxq->vport = phba->pport;
1265 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1266 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001267 if (rc == MBX_NOT_FINISHED) {
1268 spin_lock_irqsave(&phba->hbalock, flags);
1269 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1270 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001271 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001272 }
James Smart6fb120a2009-05-22 14:52:59 -04001273
1274 return;
1275}
1276
1277/**
1278 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1279 * @phba: pointer to lpfc hba data structure.
1280 * @new_fcf_record: pointer to fcf record.
1281 * @boot_flag: Indicates if this record used by boot bios.
1282 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001283 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001284 *
1285 * This routine compare the fcf record with connect list obtained from the
1286 * config region to decide if this FCF can be used for SAN discovery. It returns
1287 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1288 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1289 * is used by boot bios and addr_mode will indicate the addressing mode to be
1290 * used for this FCF when the function returns.
1291 * If the FCF record need to be used with a particular vlan id, the vlan is
1292 * set in the vlan_id on return of the function. If not VLAN tagging need to
1293 * be used with the FCF vlan_id will be set to 0xFFFF;
1294 **/
1295static int
1296lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1297 struct fcf_record *new_fcf_record,
1298 uint32_t *boot_flag, uint32_t *addr_mode,
1299 uint16_t *vlan_id)
1300{
1301 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001302 int i, j, fcf_vlan_id = 0;
1303
1304 /* Find the lowest VLAN id in the FCF record */
1305 for (i = 0; i < 512; i++) {
1306 if (new_fcf_record->vlan_bitmap[i]) {
1307 fcf_vlan_id = i * 8;
1308 j = 0;
1309 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1310 j++;
1311 fcf_vlan_id++;
1312 }
1313 break;
1314 }
1315 }
James Smart6fb120a2009-05-22 14:52:59 -04001316
James Smart0c287582009-06-10 17:22:56 -04001317 /* If FCF not available return 0 */
1318 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1319 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1320 return 0;
1321
James Smart45ed1192009-10-02 15:17:02 -04001322 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001323 *boot_flag = 0;
1324 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1325 new_fcf_record);
1326 if (phba->valid_vlan)
1327 *vlan_id = phba->vlan_id;
1328 else
1329 *vlan_id = 0xFFFF;
1330 return 1;
1331 }
1332
1333 /*
1334 * If there are no FCF connection table entry, driver connect to all
1335 * FCFs.
1336 */
1337 if (list_empty(&phba->fcf_conn_rec_list)) {
1338 *boot_flag = 0;
1339 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1340 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001341
1342 /*
1343 * When there are no FCF connect entries, use driver's default
1344 * addressing mode - FPMA.
1345 */
1346 if (*addr_mode & LPFC_FCF_FPMA)
1347 *addr_mode = LPFC_FCF_FPMA;
1348
James Smart4d9ab992009-10-02 15:16:39 -04001349 /* If FCF record report a vlan id use that vlan id */
1350 if (fcf_vlan_id)
1351 *vlan_id = fcf_vlan_id;
1352 else
1353 *vlan_id = 0xFFFF;
James Smart6fb120a2009-05-22 14:52:59 -04001354 return 1;
1355 }
1356
James Smartecfd03c2010-02-12 14:41:27 -05001357 list_for_each_entry(conn_entry,
1358 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001359 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1360 continue;
1361
1362 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1363 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001364 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001365 continue;
James Smart8fa38512009-07-19 10:01:03 -04001366 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1367 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1368 new_fcf_record))
1369 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001370 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1371 /*
1372 * If the vlan bit map does not have the bit set for the
1373 * vlan id to be used, then it is not a match.
1374 */
1375 if (!(new_fcf_record->vlan_bitmap
1376 [conn_entry->conn_rec.vlan_tag / 8] &
1377 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1378 continue;
1379 }
1380
1381 /*
James Smart0c287582009-06-10 17:22:56 -04001382 * If connection record does not support any addressing mode,
1383 * skip the FCF record.
1384 */
1385 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1386 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1387 continue;
1388
1389 /*
James Smart6fb120a2009-05-22 14:52:59 -04001390 * Check if the connection record specifies a required
1391 * addressing mode.
1392 */
1393 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1394 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1395
1396 /*
1397 * If SPMA required but FCF not support this continue.
1398 */
1399 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1400 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1401 new_fcf_record) & LPFC_FCF_SPMA))
1402 continue;
1403
1404 /*
1405 * If FPMA required but FCF not support this continue.
1406 */
1407 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1408 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1409 new_fcf_record) & LPFC_FCF_FPMA))
1410 continue;
1411 }
1412
1413 /*
1414 * This fcf record matches filtering criteria.
1415 */
1416 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1417 *boot_flag = 1;
1418 else
1419 *boot_flag = 0;
1420
James Smart0c287582009-06-10 17:22:56 -04001421 /*
1422 * If user did not specify any addressing mode, or if the
1423 * prefered addressing mode specified by user is not supported
1424 * by FCF, allow fabric to pick the addressing mode.
1425 */
James Smart6fb120a2009-05-22 14:52:59 -04001426 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1427 new_fcf_record);
1428 /*
1429 * If the user specified a required address mode, assign that
1430 * address mode
1431 */
1432 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1433 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1434 *addr_mode = (conn_entry->conn_rec.flags &
1435 FCFCNCT_AM_SPMA) ?
1436 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1437 /*
1438 * If the user specified a prefered address mode, use the
1439 * addr mode only if FCF support the addr_mode.
1440 */
1441 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1442 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1443 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1444 (*addr_mode & LPFC_FCF_SPMA))
1445 *addr_mode = LPFC_FCF_SPMA;
1446 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1447 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1448 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1449 (*addr_mode & LPFC_FCF_FPMA))
1450 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001451
James Smart4d9ab992009-10-02 15:16:39 -04001452 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001453 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1454 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001455 /*
1456 * If no vlan id is specified in connect list, use the vlan id
1457 * in the FCF record
1458 */
1459 else if (fcf_vlan_id)
1460 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001461 else
1462 *vlan_id = 0xFFFF;
1463
1464 return 1;
1465 }
1466
1467 return 0;
1468}
1469
1470/**
James Smart32b97932009-07-19 10:01:21 -04001471 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1472 * @phba: pointer to lpfc hba data structure.
1473 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1474 *
1475 * This function check if there is any fcoe event pending while driver
1476 * scan FCF entries. If there is any pending event, it will restart the
1477 * FCF saning and return 1 else return 0.
1478 */
1479int
1480lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1481{
1482 LPFC_MBOXQ_t *mbox;
1483 int rc;
1484 /*
1485 * If the Link is up and no FCoE events while in the
1486 * FCF discovery, no need to restart FCF discovery.
1487 */
1488 if ((phba->link_state >= LPFC_LINK_UP) &&
1489 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1490 return 0;
1491
1492 spin_lock_irq(&phba->hbalock);
1493 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1494 spin_unlock_irq(&phba->hbalock);
1495
1496 if (phba->link_state >= LPFC_LINK_UP)
1497 lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart1c6f4ef52009-11-18 15:40:49 -05001498 else {
James Smart4d9ab992009-10-02 15:16:39 -04001499 /*
1500 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1501 * flag
1502 */
James Smart1c6f4ef52009-11-18 15:40:49 -05001503 spin_lock_irq(&phba->hbalock);
James Smart4d9ab992009-10-02 15:16:39 -04001504 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smartecfd03c2010-02-12 14:41:27 -05001505 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart1c6f4ef52009-11-18 15:40:49 -05001506 spin_unlock_irq(&phba->hbalock);
1507 }
James Smart32b97932009-07-19 10:01:21 -04001508
1509 if (unreg_fcf) {
1510 spin_lock_irq(&phba->hbalock);
1511 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1512 spin_unlock_irq(&phba->hbalock);
1513 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1514 if (!mbox) {
1515 lpfc_printf_log(phba, KERN_ERR,
1516 LOG_DISCOVERY|LOG_MBOX,
1517 "2610 UNREG_FCFI mbox allocation failed\n");
1518 return 1;
1519 }
1520 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
1521 mbox->vport = phba->pport;
1522 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
1523 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1524 if (rc == MBX_NOT_FINISHED) {
1525 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
1526 "2611 UNREG_FCFI issue mbox failed\n");
1527 mempool_free(mbox, phba->mbox_mem_pool);
1528 }
1529 }
1530
1531 return 1;
1532}
1533
1534/**
James Smart6fb120a2009-05-22 14:52:59 -04001535 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
1536 * @phba: pointer to lpfc hba data structure.
1537 * @mboxq: pointer to mailbox object.
1538 *
1539 * This function iterate through all the fcf records available in
1540 * HBA and choose the optimal FCF record for discovery. After finding
1541 * the FCF for discovery it register the FCF record and kick start
1542 * discovery.
1543 * If FCF_IN_USE flag is set in currently used FCF, the routine try to
1544 * use a FCF record which match fabric name and mac address of the
1545 * currently used FCF record.
1546 * If the driver support only one FCF, it will try to use the FCF record
1547 * used by BOOT_BIOS.
1548 */
1549void
1550lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1551{
1552 void *virt_addr;
1553 dma_addr_t phys_addr;
1554 uint8_t *bytep;
1555 struct lpfc_mbx_sge sge;
1556 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1557 uint32_t shdr_status, shdr_add_status;
1558 union lpfc_sli4_cfg_shdr *shdr;
1559 struct fcf_record *new_fcf_record;
James Smart6fb120a2009-05-22 14:52:59 -04001560 uint32_t boot_flag, addr_mode;
1561 uint32_t next_fcf_index;
James Smartecfd03c2010-02-12 14:41:27 -05001562 struct lpfc_fcf_rec *fcf_rec = NULL;
1563 unsigned long iflags;
James Smart6fb120a2009-05-22 14:52:59 -04001564 uint16_t vlan_id;
James Smartecfd03c2010-02-12 14:41:27 -05001565 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001566
James Smart32b97932009-07-19 10:01:21 -04001567 /* If there is pending FCoE event restart FCF table scan */
1568 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1569 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1570 return;
1571 }
1572
James Smart6fb120a2009-05-22 14:52:59 -04001573 /* Get the first SGE entry from the non-embedded DMA memory. This
1574 * routine only uses a single SGE.
1575 */
1576 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1577 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1578 if (unlikely(!mboxq->sge_array)) {
1579 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1580 "2524 Failed to get the non-embedded SGE "
1581 "virtual address\n");
1582 goto out;
1583 }
1584 virt_addr = mboxq->sge_array->addr[0];
1585
1586 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1587 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1588 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
1589 &shdr->response);
1590 /*
1591 * The FCF Record was read and there is no reason for the driver
1592 * to maintain the FCF record data or memory. Instead, just need
1593 * to book keeping the FCFIs can be used.
1594 */
1595 if (shdr_status || shdr_add_status) {
James Smartdef9c7a2009-12-21 17:02:28 -05001596 if (shdr_status == STATUS_FCF_TABLE_EMPTY) {
1597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1598 "2726 READ_FCF_RECORD Indicates empty "
1599 "FCF table.\n");
1600 } else {
1601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1602 "2521 READ_FCF_RECORD mailbox failed "
1603 "with status x%x add_status x%x, mbx\n",
1604 shdr_status, shdr_add_status);
1605 }
James Smart6fb120a2009-05-22 14:52:59 -04001606 goto out;
1607 }
1608 /* Interpreting the returned information of FCF records */
1609 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1610 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1611 sizeof(struct lpfc_mbx_read_fcf_tbl));
1612 next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1613
1614 new_fcf_record = (struct fcf_record *)(virt_addr +
1615 sizeof(struct lpfc_mbx_read_fcf_tbl));
1616 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1617 sizeof(struct fcf_record));
1618 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1619
James Smartecfd03c2010-02-12 14:41:27 -05001620 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1621 &addr_mode, &vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001622 /*
1623 * If the fcf record does not match with connect list entries
1624 * read the next entry.
1625 */
1626 if (!rc)
1627 goto read_next_fcf;
1628 /*
1629 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05001630 * FCF record for the discovery. The condition that a rescan
1631 * matches the in-use FCF record: fabric name, switch name, mac
1632 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04001633 */
James Smartecfd03c2010-02-12 14:41:27 -05001634 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart6fb120a2009-05-22 14:52:59 -04001635 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartecfd03c2010-02-12 14:41:27 -05001636 if (lpfc_fab_name_match(phba->fcf.current_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001637 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001638 lpfc_sw_name_match(phba->fcf.current_rec.switch_name,
James Smart8fa38512009-07-19 10:01:03 -04001639 new_fcf_record) &&
James Smartecfd03c2010-02-12 14:41:27 -05001640 lpfc_mac_addr_match(phba->fcf.current_rec.mac_addr,
1641 new_fcf_record) &&
1642 lpfc_vlan_id_match(phba->fcf.current_rec.vlan_id,
1643 vlan_id)) {
James Smart6fb120a2009-05-22 14:52:59 -04001644 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smartecfd03c2010-02-12 14:41:27 -05001645 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
1646 /* Stop FCF redisc wait timer if pending */
1647 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
1648 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
1649 /* If in fast failover, mark it's completed */
1650 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
1651 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart6fb120a2009-05-22 14:52:59 -04001652 goto out;
1653 }
James Smartecfd03c2010-02-12 14:41:27 -05001654 /*
1655 * Read next FCF record from HBA searching for the matching
1656 * with in-use record only if not during the fast failover
1657 * period. In case of fast failover period, it shall try to
1658 * determine whether the FCF record just read should be the
1659 * next candidate.
1660 */
1661 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
1662 spin_unlock_irqrestore(&phba->hbalock, iflags);
1663 goto read_next_fcf;
1664 }
James Smart6fb120a2009-05-22 14:52:59 -04001665 }
James Smartecfd03c2010-02-12 14:41:27 -05001666 /*
1667 * Update on failover FCF record only if it's in FCF fast-failover
1668 * period; otherwise, update on current FCF record.
1669 */
1670 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
1671 /* Fast FCF failover only to the same fabric name */
1672 if (lpfc_fab_name_match(phba->fcf.current_rec.fabric_name,
1673 new_fcf_record))
1674 fcf_rec = &phba->fcf.failover_rec;
1675 else
1676 goto read_next_fcf;
1677 } else
1678 fcf_rec = &phba->fcf.current_rec;
1679
James Smart6fb120a2009-05-22 14:52:59 -04001680 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1681 /*
James Smartecfd03c2010-02-12 14:41:27 -05001682 * If the driver FCF record does not have boot flag
1683 * set and new hba fcf record has boot flag set, use
1684 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001685 */
James Smartecfd03c2010-02-12 14:41:27 -05001686 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
1687 /* Choose this FCF record */
1688 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1689 addr_mode, vlan_id, BOOT_ENABLE);
1690 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart6fb120a2009-05-22 14:52:59 -04001691 goto read_next_fcf;
1692 }
1693 /*
James Smartecfd03c2010-02-12 14:41:27 -05001694 * If the driver FCF record has boot flag set and the
1695 * new hba FCF record does not have boot flag, read
1696 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04001697 */
James Smartecfd03c2010-02-12 14:41:27 -05001698 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
1699 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart6fb120a2009-05-22 14:52:59 -04001700 goto read_next_fcf;
1701 }
1702 /*
James Smartecfd03c2010-02-12 14:41:27 -05001703 * If the new hba FCF record has lower priority value
1704 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04001705 */
James Smartecfd03c2010-02-12 14:41:27 -05001706 if (lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record) &&
1707 (new_fcf_record->fip_priority < fcf_rec->priority)) {
1708 /* Choose this FCF record */
1709 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1710 addr_mode, vlan_id, 0);
James Smart6fb120a2009-05-22 14:52:59 -04001711 }
James Smartecfd03c2010-02-12 14:41:27 -05001712 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart6fb120a2009-05-22 14:52:59 -04001713 goto read_next_fcf;
1714 }
1715 /*
James Smartecfd03c2010-02-12 14:41:27 -05001716 * This is the first suitable FCF record, choose this record for
1717 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001718 */
James Smartecfd03c2010-02-12 14:41:27 -05001719 if (fcf_rec) {
1720 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
1721 addr_mode, vlan_id, (boot_flag ?
1722 BOOT_ENABLE : 0));
1723 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart6fb120a2009-05-22 14:52:59 -04001724 }
James Smartecfd03c2010-02-12 14:41:27 -05001725 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart6fb120a2009-05-22 14:52:59 -04001726 goto read_next_fcf;
1727
1728read_next_fcf:
1729 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05001730 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
1731 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
1732 /*
1733 * Case of FCF fast failover scan
1734 */
1735
1736 /*
1737 * It has not found any suitable FCF record, cancel
1738 * FCF scan inprogress, and do nothing
1739 */
1740 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
1741 spin_lock_irqsave(&phba->hbalock, iflags);
1742 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1743 spin_unlock_irqrestore(&phba->hbalock, iflags);
1744 return;
1745 }
1746 /*
1747 * It has found a suitable FCF record that is not
1748 * the same as in-use FCF record, unregister the
1749 * in-use FCF record, replace the in-use FCF record
1750 * with the new FCF record, mark FCF fast failover
1751 * completed, and then start register the new FCF
1752 * record.
1753 */
1754
1755 /* unregister the current in-use FCF record */
1756 lpfc_unregister_fcf(phba);
1757 /* replace in-use record with the new record */
1758 memcpy(&phba->fcf.current_rec,
1759 &phba->fcf.failover_rec,
1760 sizeof(struct lpfc_fcf_rec));
1761 /* mark the FCF fast failover completed */
1762 spin_lock_irqsave(&phba->hbalock, iflags);
1763 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
1764 spin_unlock_irqrestore(&phba->hbalock, iflags);
1765 /* Register to the new FCF record */
1766 lpfc_register_fcf(phba);
1767 } else {
1768 /*
1769 * In case of transaction period to fast FCF failover,
1770 * do nothing when search to the end of the FCF table.
1771 */
1772 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
1773 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
1774 return;
1775 /*
1776 * Otherwise, initial scan or post linkdown rescan,
1777 * register with the best fit FCF record found so
1778 * far through the scanning process.
1779 */
1780 lpfc_register_fcf(phba);
1781 }
1782 } else
James Smart6fb120a2009-05-22 14:52:59 -04001783 lpfc_sli4_read_fcf_record(phba, next_fcf_index);
1784 return;
1785
1786out:
1787 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1788 lpfc_register_fcf(phba);
1789
1790 return;
1791}
1792
1793/**
James Smart1c6834a2009-07-19 10:01:26 -04001794 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
1795 * @phba: pointer to lpfc hba data structure.
1796 * @mboxq: pointer to mailbox data structure.
1797 *
1798 * This function handles completion of init vpi mailbox command.
1799 */
James Smart695a8142010-01-26 23:08:03 -05001800void
James Smart1c6834a2009-07-19 10:01:26 -04001801lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1802{
1803 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05001804 struct lpfc_nodelist *ndlp;
James Smart1c6834a2009-07-19 10:01:26 -04001805 if (mboxq->u.mb.mbxStatus) {
1806 lpfc_printf_vlog(vport, KERN_ERR,
1807 LOG_MBOX,
1808 "2609 Init VPI mailbox failed 0x%x\n",
1809 mboxq->u.mb.mbxStatus);
1810 mempool_free(mboxq, phba->mbox_mem_pool);
1811 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1812 return;
1813 }
James Smart97957242009-12-21 17:03:15 -05001814 spin_lock_irq(&phba->hbalock);
James Smart1c6834a2009-07-19 10:01:26 -04001815 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart97957242009-12-21 17:03:15 -05001816 spin_unlock_irq(&phba->hbalock);
James Smart1c6834a2009-07-19 10:01:26 -04001817
James Smart695a8142010-01-26 23:08:03 -05001818 /* If this port is physical port or FDISC is done, do reg_vpi */
1819 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
1820 ndlp = lpfc_findnode_did(vport, Fabric_DID);
1821 if (!ndlp)
1822 lpfc_printf_vlog(vport, KERN_ERR,
1823 LOG_DISCOVERY,
1824 "2731 Cannot find fabric "
1825 "controller node\n");
1826 else
1827 lpfc_register_new_vport(phba, vport, ndlp);
1828 mempool_free(mboxq, phba->mbox_mem_pool);
1829 return;
1830 }
1831
James Smart1c6834a2009-07-19 10:01:26 -04001832 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1833 lpfc_initial_fdisc(vport);
1834 else {
1835 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04001836 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1837 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04001838 }
James Smart695a8142010-01-26 23:08:03 -05001839 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04001840 return;
1841}
1842
1843/**
James Smartecfd03c2010-02-12 14:41:27 -05001844 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
1845 * @vport: pointer to lpfc_vport data structure.
1846 *
1847 * This function issue a init_vpi mailbox command to initialize
1848 * VPI for the vport.
1849 */
1850void
1851lpfc_issue_init_vpi(struct lpfc_vport *vport)
1852{
1853 LPFC_MBOXQ_t *mboxq;
1854 int rc;
1855
1856 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
1857 if (!mboxq) {
1858 lpfc_printf_vlog(vport, KERN_ERR,
1859 LOG_MBOX, "2607 Failed to allocate "
1860 "init_vpi mailbox\n");
1861 return;
1862 }
1863 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
1864 mboxq->vport = vport;
1865 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
1866 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
1867 if (rc == MBX_NOT_FINISHED) {
1868 lpfc_printf_vlog(vport, KERN_ERR,
1869 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
1870 mempool_free(mboxq, vport->phba->mbox_mem_pool);
1871 }
1872}
1873
1874/**
James Smart6fb120a2009-05-22 14:52:59 -04001875 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
1876 * @phba: pointer to lpfc hba data structure.
1877 *
1878 * This function loops through the list of vports on the @phba and issues an
1879 * FDISC if possible.
1880 */
1881void
1882lpfc_start_fdiscs(struct lpfc_hba *phba)
1883{
1884 struct lpfc_vport **vports;
1885 int i;
1886
1887 vports = lpfc_create_vport_work_array(phba);
1888 if (vports != NULL) {
1889 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1890 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1891 continue;
1892 /* There are no vpi for this vport */
1893 if (vports[i]->vpi > phba->max_vpi) {
1894 lpfc_vport_set_state(vports[i],
1895 FC_VPORT_FAILED);
1896 continue;
1897 }
1898 if (phba->fc_topology == TOPOLOGY_LOOP) {
1899 lpfc_vport_set_state(vports[i],
1900 FC_VPORT_LINKDOWN);
1901 continue;
1902 }
James Smart1c6834a2009-07-19 10:01:26 -04001903 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05001904 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04001905 continue;
1906 }
James Smart6fb120a2009-05-22 14:52:59 -04001907 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1908 lpfc_initial_fdisc(vports[i]);
1909 else {
1910 lpfc_vport_set_state(vports[i],
1911 FC_VPORT_NO_FABRIC_SUPP);
1912 lpfc_printf_vlog(vports[i], KERN_ERR,
1913 LOG_ELS,
1914 "0259 No NPIV "
1915 "Fabric support\n");
1916 }
1917 }
1918 }
1919 lpfc_destroy_vport_work_array(phba, vports);
1920}
1921
1922void
1923lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1924{
1925 struct lpfc_dmabuf *dmabuf = mboxq->context1;
1926 struct lpfc_vport *vport = mboxq->vport;
1927
1928 if (mboxq->u.mb.mbxStatus) {
1929 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1930 "2018 REG_VFI mbxStatus error x%x "
1931 "HBA state x%x\n",
1932 mboxq->u.mb.mbxStatus, vport->port_state);
1933 if (phba->fc_topology == TOPOLOGY_LOOP) {
1934 /* FLOGI failed, use loop map to make discovery list */
1935 lpfc_disc_list_loopmap(vport);
1936 /* Start discovery */
1937 lpfc_disc_start(vport);
1938 goto fail_free_mem;
1939 }
1940 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1941 goto fail_free_mem;
1942 }
James Smartc8685952009-11-18 15:39:16 -05001943 /* The VPI is implicitly registered when the VFI is registered */
1944 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05001945 vport->fc_flag |= FC_VFI_REGISTERED;
1946
1947 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart6fb120a2009-05-22 14:52:59 -04001948
1949 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1950 lpfc_start_fdiscs(phba);
1951 lpfc_do_scr_ns_plogi(phba, vport);
1952 }
1953
1954fail_free_mem:
1955 mempool_free(mboxq, phba->mbox_mem_pool);
1956 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1957 kfree(dmabuf);
1958 return;
1959}
1960
1961static void
James Smart2e0fef82007-06-17 19:56:36 -05001962lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001963{
James Smart6fb120a2009-05-22 14:52:59 -04001964 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001965 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05001966 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001967
1968
1969 /* Check for error */
1970 if (mb->mbxStatus) {
1971 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001972 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1973 "0319 READ_SPARAM mbxStatus error x%x "
1974 "hba state x%x>\n",
1975 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001976 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05001977 goto out;
1978 }
1979
James Smart2e0fef82007-06-17 19:56:36 -05001980 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05001981 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05001982 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05001983 u64_to_wwn(phba->cfg_soft_wwnn,
1984 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04001985 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05001986 u64_to_wwn(phba->cfg_soft_wwpn,
1987 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05001988 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1989 sizeof(vport->fc_nodename));
1990 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1991 sizeof(vport->fc_portname));
1992 if (vport->port_type == LPFC_PHYSICAL_PORT) {
1993 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1994 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1995 }
1996
dea31012005-04-17 16:05:31 -05001997 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1998 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001999 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002000 return;
2001
2002out:
2003 pmb->context1 = NULL;
2004 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2005 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002006 lpfc_issue_clear_la(phba, vport);
2007 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002008 return;
2009}
2010
2011static void
James Smart92d7f7b2007-06-17 19:56:38 -05002012lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05002013{
James Smart92d7f7b2007-06-17 19:56:38 -05002014 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002015 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002016 int i;
James Smart14691152006-12-02 13:34:28 -05002017 struct lpfc_dmabuf *mp;
2018 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002019 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002020
dea31012005-04-17 16:05:31 -05002021 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002022
James Smart92d7f7b2007-06-17 19:56:38 -05002023 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002024 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05002025 case LA_1GHZ_LINK:
2026 phba->fc_linkspeed = LA_1GHZ_LINK;
2027 break;
2028 case LA_2GHZ_LINK:
2029 phba->fc_linkspeed = LA_2GHZ_LINK;
2030 break;
2031 case LA_4GHZ_LINK:
2032 phba->fc_linkspeed = LA_4GHZ_LINK;
2033 break;
2034 case LA_8GHZ_LINK:
2035 phba->fc_linkspeed = LA_8GHZ_LINK;
2036 break;
James Smartf4b4c682009-05-22 14:53:12 -04002037 case LA_10GHZ_LINK:
2038 phba->fc_linkspeed = LA_10GHZ_LINK;
2039 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002040 default:
2041 phba->fc_linkspeed = LA_UNKNW_LINK;
2042 break;
dea31012005-04-17 16:05:31 -05002043 }
2044
2045 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05002046 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05002047
2048 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05002049 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05002050
James Smart6a9c52c2009-10-02 15:16:51 -04002051 /* if npiv is enabled and this adapter supports npiv log
2052 * a message that npiv is not supported in this topology
2053 */
2054 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04002055 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2056 "1309 Link Up Event npiv not supported in loop "
2057 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002058 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05002059 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05002060 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002061
James Smart2e0fef82007-06-17 19:56:36 -05002062 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05002063 i = la->un.lilpBde64.tus.f.bdeSize;
2064
2065 if (i == 0) {
2066 phba->alpa_map[0] = 0;
2067 } else {
James Smarte8b62012007-08-02 11:10:09 -04002068 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05002069 int numalpa, j, k;
2070 union {
2071 uint8_t pamap[16];
2072 struct {
2073 uint32_t wd1;
2074 uint32_t wd2;
2075 uint32_t wd3;
2076 uint32_t wd4;
2077 } pa;
2078 } un;
2079 numalpa = phba->alpa_map[0];
2080 j = 0;
2081 while (j < numalpa) {
2082 memset(un.pamap, 0, 16);
2083 for (k = 1; j < numalpa; k++) {
2084 un.pamap[k - 1] =
2085 phba->alpa_map[j + 1];
2086 j++;
2087 if (k == 16)
2088 break;
2089 }
2090 /* Link Up Event ALPA map */
2091 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05002092 KERN_WARNING,
2093 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002094 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05002095 "ALPA map Data: x%x "
2096 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002097 un.pa.wd1, un.pa.wd2,
2098 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05002099 }
2100 }
2101 }
2102 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05002103 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04002104 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05002105 (phba->sli_rev == 3))
2106 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
2107 }
James Smart2e0fef82007-06-17 19:56:36 -05002108 vport->fc_myDID = phba->fc_pref_DID;
2109 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002110 }
James Smart92d7f7b2007-06-17 19:56:38 -05002111 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002112
2113 lpfc_linkup(phba);
2114 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05002115 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05002116 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05002117 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04002118 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05002119 if (rc == MBX_NOT_FINISHED) {
2120 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2121 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2122 kfree(mp);
2123 mempool_free(sparam_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05002124 goto out;
James Smart14691152006-12-02 13:34:28 -05002125 }
dea31012005-04-17 16:05:31 -05002126 }
2127
James Smart6fb120a2009-05-22 14:52:59 -04002128 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2129 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2130 if (!cfglink_mbox)
2131 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05002132 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002133 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002134 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002135 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002136 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002137 if (rc == MBX_NOT_FINISHED) {
2138 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2139 goto out;
2140 }
2141 } else {
James Smart32b97932009-07-19 10:01:21 -04002142 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002143 /*
2144 * Add the driver's default FCF record at FCF index 0 now. This
2145 * is phase 1 implementation that support FCF index 0 and driver
2146 * defaults.
2147 */
James Smart45ed1192009-10-02 15:17:02 -04002148 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002149 fcf_record = kzalloc(sizeof(struct fcf_record),
2150 GFP_KERNEL);
2151 if (unlikely(!fcf_record)) {
2152 lpfc_printf_log(phba, KERN_ERR,
2153 LOG_MBOX | LOG_SLI,
2154 "2554 Could not allocate memmory for "
2155 "fcf record\n");
2156 rc = -ENODEV;
2157 goto out;
2158 }
2159
2160 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2161 LPFC_FCOE_FCF_DEF_INDEX);
2162 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2163 if (unlikely(rc)) {
2164 lpfc_printf_log(phba, KERN_ERR,
2165 LOG_MBOX | LOG_SLI,
2166 "2013 Could not manually add FCF "
2167 "record 0, status %d\n", rc);
2168 rc = -ENODEV;
2169 kfree(fcf_record);
2170 goto out;
2171 }
2172 kfree(fcf_record);
2173 }
2174 /*
2175 * The driver is expected to do FIP/FCF. Call the port
2176 * and get the FCF Table.
2177 */
James Smart32b97932009-07-19 10:01:21 -04002178 spin_lock_irq(&phba->hbalock);
2179 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2180 spin_unlock_irq(&phba->hbalock);
2181 return;
2182 }
2183 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002184 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04002185 if (rc)
2186 goto out;
dea31012005-04-17 16:05:31 -05002187 }
James Smart6fb120a2009-05-22 14:52:59 -04002188
2189 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002190out:
2191 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002192 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2193 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2194 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002195 lpfc_issue_clear_la(phba, vport);
2196 return;
dea31012005-04-17 16:05:31 -05002197}
2198
2199static void
James Smart84774a42008-08-24 21:50:06 -04002200lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002201{
dea31012005-04-17 16:05:31 -05002202 uint32_t control;
2203 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002204 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002205 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002206 if (phba->sli_rev <= LPFC_SLI_REV3) {
2207 control = readl(phba->HCregaddr);
2208 control |= HC_LAINT_ENA;
2209 writel(control, phba->HCregaddr);
2210 readl(phba->HCregaddr); /* flush */
2211 }
James Smart2e0fef82007-06-17 19:56:36 -05002212 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002213}
2214
James Smart84774a42008-08-24 21:50:06 -04002215static void
2216lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2217{
2218 lpfc_linkdown(phba);
2219 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002220 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002221 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2222}
2223
2224
dea31012005-04-17 16:05:31 -05002225/*
2226 * This routine handles processing a READ_LA mailbox
2227 * command upon completion. It is setup in the LPFC_MBOXQ
2228 * as the completion routine when the command is
2229 * handed off to the SLI layer.
2230 */
2231void
James Smart2e0fef82007-06-17 19:56:36 -05002232lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002233{
James Smart2e0fef82007-06-17 19:56:36 -05002234 struct lpfc_vport *vport = pmb->vport;
2235 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002236 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002237 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002238 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2239
James Smart0d2b6b82008-06-14 22:52:47 -04002240 /* Unblock ELS traffic */
2241 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002242 /* Check for error */
2243 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002244 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002245 "1307 READ_LA mbox error x%x state x%x\n",
2246 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002247 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002248 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002249 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2250 }
2251
James Smart04c68492009-05-22 14:52:52 -04002252 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002253
2254 memcpy(&phba->alpa_map[0], mp->virt, 128);
2255
James Smart2e0fef82007-06-17 19:56:36 -05002256 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002257 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002258 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002259 else
James Smart2e0fef82007-06-17 19:56:36 -05002260 vport->fc_flag &= ~FC_BYPASSED_MODE;
2261 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002262
James Smart0c287582009-06-10 17:22:56 -04002263 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002264 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002265 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002266 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002267 if (phba->fc_eventTag != 0)
2268 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002269 }
dea31012005-04-17 16:05:31 -05002270
2271 phba->fc_eventTag = la->eventTag;
James Smart84774a42008-08-24 21:50:06 -04002272 if (la->mm)
2273 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2274 else
2275 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
dea31012005-04-17 16:05:31 -05002276
James Smart4d9ab992009-10-02 15:16:39 -04002277 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002278 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002279 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002280 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002281 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002282 "1306 Link Up Event in loop back mode "
2283 "x%x received Data: x%x x%x x%x x%x\n",
2284 la->eventTag, phba->fc_eventTag,
2285 la->granted_AL_PA, la->UlnkSpeed,
2286 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002287 } else {
2288 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002289 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002290 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002291 la->eventTag, phba->fc_eventTag,
2292 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002293 phba->alpa_map[0],
2294 la->mm, la->fa,
2295 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002296 }
James Smart92d7f7b2007-06-17 19:56:38 -05002297 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002298 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002299 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002300 if (phba->link_flag & LS_LOOPBACK_MODE) {
2301 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2302 "1308 Link Down Event in loop back mode "
2303 "x%x received "
2304 "Data: x%x x%x x%x\n",
2305 la->eventTag, phba->fc_eventTag,
2306 phba->pport->port_state, vport->fc_flag);
2307 }
2308 else {
2309 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002310 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002311 "Data: x%x x%x x%x x%x x%x\n",
2312 la->eventTag, phba->fc_eventTag,
2313 phba->pport->port_state, vport->fc_flag,
2314 la->mm, la->fa);
2315 }
2316 lpfc_mbx_issue_link_down(phba);
2317 }
2318 if (la->mm && la->attType == AT_LINK_UP) {
2319 if (phba->link_state != LPFC_LINK_DOWN) {
2320 phba->fc_stat.LinkDown++;
2321 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2322 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002323 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002324 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002325 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002326 lpfc_mbx_issue_link_down(phba);
2327 } else
2328 lpfc_enable_la(phba);
2329
2330 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2331 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2332 "Data: x%x x%x x%x\n",
2333 la->eventTag, phba->fc_eventTag,
2334 phba->pport->port_state, vport->fc_flag);
2335 /*
2336 * The cmnd that triggered this will be waiting for this
2337 * signal.
2338 */
2339 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2340 if (phba->wait_4_mlo_maint_flg) {
2341 phba->wait_4_mlo_maint_flg = 0;
2342 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002343 }
James Smart84774a42008-08-24 21:50:06 -04002344 }
2345
2346 if (la->fa) {
2347 if (la->mm)
2348 lpfc_issue_clear_la(phba, vport);
2349 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2350 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002351 }
2352
2353lpfc_mbx_cmpl_read_la_free_mbuf:
2354 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2355 kfree(mp);
2356 mempool_free(pmb, phba->mbox_mem_pool);
2357 return;
2358}
2359
2360/*
2361 * This routine handles processing a REG_LOGIN mailbox
2362 * command upon completion. It is setup in the LPFC_MBOXQ
2363 * as the completion routine when the command is
2364 * handed off to the SLI layer.
2365 */
2366void
James Smart2e0fef82007-06-17 19:56:36 -05002367lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002368{
James Smart2e0fef82007-06-17 19:56:36 -05002369 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002370 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002371 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05002372
dea31012005-04-17 16:05:31 -05002373 pmb->context1 = NULL;
2374
2375 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05002376 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05002377 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2378 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002379 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002380 /* decrement the node reference count held for this callback
2381 * function.
2382 */
James Smart329f9bc2007-04-25 09:53:01 -04002383 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002384
2385 return;
2386}
2387
James Smart92d7f7b2007-06-17 19:56:38 -05002388static void
2389lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2390{
James Smart04c68492009-05-22 14:52:52 -04002391 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002392 struct lpfc_vport *vport = pmb->vport;
2393 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2394
2395 switch (mb->mbxStatus) {
2396 case 0x0011:
2397 case 0x0020:
2398 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04002399 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2400 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2401 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002402 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002403 }
James Smart97957242009-12-21 17:03:15 -05002404 spin_lock_irq(&phba->hbalock);
James Smartc8685952009-11-18 15:39:16 -05002405 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05002406 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart97957242009-12-21 17:03:15 -05002407 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002408 vport->unreg_vpi_cmpl = VPORT_OK;
2409 mempool_free(pmb, phba->mbox_mem_pool);
2410 /*
2411 * This shost reference might have been taken at the beginning of
2412 * lpfc_vport_delete()
2413 */
James Smart1c6f4ef52009-11-18 15:40:49 -05002414 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05002415 scsi_host_put(shost);
2416}
2417
James Smartd7c255b2008-08-24 21:50:00 -04002418int
James Smart92d7f7b2007-06-17 19:56:38 -05002419lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2420{
2421 struct lpfc_hba *phba = vport->phba;
2422 LPFC_MBOXQ_t *mbox;
2423 int rc;
2424
2425 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2426 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002427 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002428
2429 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2430 mbox->vport = vport;
2431 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002432 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002433 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002434 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2435 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002436 mempool_free(mbox, phba->mbox_mem_pool);
2437 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002438 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002439 }
James Smartd7c255b2008-08-24 21:50:00 -04002440 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002441}
2442
2443static void
2444lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2445{
2446 struct lpfc_vport *vport = pmb->vport;
2447 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002448 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002449
2450 switch (mb->mbxStatus) {
2451 case 0x0011:
2452 case 0x9601:
2453 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002454 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2455 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2456 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002457 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2458 spin_lock_irq(shost->host_lock);
2459 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2460 spin_unlock_irq(shost->host_lock);
2461 vport->fc_myDID = 0;
2462 goto out;
2463 }
James Smart92d7f7b2007-06-17 19:56:38 -05002464
James Smartc8685952009-11-18 15:39:16 -05002465 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002466 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart92d7f7b2007-06-17 19:56:38 -05002467 vport->num_disc_nodes = 0;
2468 /* go thru NPR list and issue ELS PLOGIs */
2469 if (vport->fc_npr_cnt)
2470 lpfc_els_disc_plogi(vport);
2471
2472 if (!vport->num_disc_nodes) {
2473 spin_lock_irq(shost->host_lock);
2474 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2475 spin_unlock_irq(shost->host_lock);
2476 lpfc_can_disctmo(vport);
2477 }
2478 vport->port_state = LPFC_VPORT_READY;
2479
2480out:
2481 mempool_free(pmb, phba->mbox_mem_pool);
2482 return;
2483}
2484
James Smart21e9a0a2009-05-22 14:53:21 -04002485/**
2486 * lpfc_create_static_vport - Read HBA config region to create static vports.
2487 * @phba: pointer to lpfc hba data structure.
2488 *
2489 * This routine issue a DUMP mailbox command for config region 22 to get
2490 * the list of static vports to be created. The function create vports
2491 * based on the information returned from the HBA.
2492 **/
2493void
2494lpfc_create_static_vport(struct lpfc_hba *phba)
2495{
2496 LPFC_MBOXQ_t *pmb = NULL;
2497 MAILBOX_t *mb;
2498 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04002499 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04002500 struct fc_vport_identifiers vport_id;
2501 struct fc_vport *new_fc_vport;
2502 struct Scsi_Host *shost;
2503 struct lpfc_vport *vport;
2504 uint16_t offset = 0;
2505 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04002506 struct lpfc_dmabuf *mp;
2507 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04002508
2509 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2510 if (!pmb) {
2511 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2512 "0542 lpfc_create_static_vport failed to"
2513 " allocate mailbox memory\n");
2514 return;
2515 }
2516
2517 mb = &pmb->u.mb;
2518
2519 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2520 if (!vport_info) {
2521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2522 "0543 lpfc_create_static_vport failed to"
2523 " allocate vport_info\n");
2524 mempool_free(pmb, phba->mbox_mem_pool);
2525 return;
2526 }
2527
2528 vport_buff = (uint8_t *) vport_info;
2529 do {
James Smart1c6834a2009-07-19 10:01:26 -04002530 if (lpfc_dump_static_vport(phba, pmb, offset))
2531 goto out;
2532
James Smart21e9a0a2009-05-22 14:53:21 -04002533 pmb->vport = phba->pport;
2534 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2535
2536 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2537 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2538 "0544 lpfc_create_static_vport failed to"
2539 " issue dump mailbox command ret 0x%x "
2540 "status 0x%x\n",
2541 rc, mb->mbxStatus);
2542 goto out;
2543 }
2544
James Smart1c6834a2009-07-19 10:01:26 -04002545 if (phba->sli_rev == LPFC_SLI_REV4) {
2546 byte_count = pmb->u.mqe.un.mb_words[5];
2547 mp = (struct lpfc_dmabuf *) pmb->context2;
2548 if (byte_count > sizeof(struct static_vport_info) -
2549 offset)
2550 byte_count = sizeof(struct static_vport_info)
2551 - offset;
2552 memcpy(vport_buff + offset, mp->virt, byte_count);
2553 offset += byte_count;
2554 } else {
2555 if (mb->un.varDmp.word_cnt >
2556 sizeof(struct static_vport_info) - offset)
2557 mb->un.varDmp.word_cnt =
2558 sizeof(struct static_vport_info)
2559 - offset;
2560 byte_count = mb->un.varDmp.word_cnt;
2561 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2562 vport_buff + offset,
2563 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04002564
James Smart1c6834a2009-07-19 10:01:26 -04002565 offset += byte_count;
2566 }
James Smart21e9a0a2009-05-22 14:53:21 -04002567
James Smart1c6834a2009-07-19 10:01:26 -04002568 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04002569 offset < sizeof(struct static_vport_info));
2570
2571
2572 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2573 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2574 != VPORT_INFO_REV)) {
2575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2576 "0545 lpfc_create_static_vport bad"
2577 " information header 0x%x 0x%x\n",
2578 le32_to_cpu(vport_info->signature),
2579 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2580
2581 goto out;
2582 }
2583
2584 shost = lpfc_shost_from_vport(phba->pport);
2585
2586 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2587 memset(&vport_id, 0, sizeof(vport_id));
2588 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2589 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2590 if (!vport_id.port_name || !vport_id.node_name)
2591 continue;
2592
2593 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2594 vport_id.vport_type = FC_PORTTYPE_NPIV;
2595 vport_id.disable = false;
2596 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2597
2598 if (!new_fc_vport) {
2599 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2600 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04002601 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04002602 continue;
2603 }
2604
2605 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2606 vport->vport_flag |= STATIC_VPORT;
2607 }
2608
2609out:
James Smart21e9a0a2009-05-22 14:53:21 -04002610 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04002611 if (rc != MBX_TIMEOUT) {
2612 if (pmb->context2) {
2613 mp = (struct lpfc_dmabuf *) pmb->context2;
2614 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2615 kfree(mp);
2616 }
James Smart21e9a0a2009-05-22 14:53:21 -04002617 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002618 }
James Smart21e9a0a2009-05-22 14:53:21 -04002619
2620 return;
2621}
2622
dea31012005-04-17 16:05:31 -05002623/*
2624 * This routine handles processing a Fabric REG_LOGIN mailbox
2625 * command upon completion. It is setup in the LPFC_MBOXQ
2626 * as the completion routine when the command is
2627 * handed off to the SLI layer.
2628 */
2629void
James Smart2e0fef82007-06-17 19:56:36 -05002630lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002631{
James Smart92d7f7b2007-06-17 19:56:38 -05002632 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04002633 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002634 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05002635 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002636
James Smart549e55c2007-08-02 11:09:51 -04002637 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04002638 pmb->context1 = NULL;
2639 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05002640 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04002641 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2642 "0258 Register Fabric login error: 0x%x\n",
2643 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05002644 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2645 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002646 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002647
James Smart92d7f7b2007-06-17 19:56:38 -05002648 if (phba->fc_topology == TOPOLOGY_LOOP) {
2649 /* FLOGI failed, use loop map to make discovery list */
2650 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002651
James Smart92d7f7b2007-06-17 19:56:38 -05002652 /* Start discovery */
2653 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05002654 /* Decrement the reference count to ndlp after the
2655 * reference to the ndlp are done.
2656 */
2657 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05002658 return;
2659 }
2660
2661 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05002662 /* Decrement the reference count to ndlp after the reference
2663 * to the ndlp are done.
2664 */
2665 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002666 return;
2667 }
2668
dea31012005-04-17 16:05:31 -05002669 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04002670 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05002671 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002672 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002673
James Smart2e0fef82007-06-17 19:56:36 -05002674 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart21e9a0a2009-05-22 14:53:21 -04002675 lpfc_start_fdiscs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002676 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05002677 }
2678
2679 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2680 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002681 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002682
2683 /* Drop the reference count from the mbox at the end after
2684 * all the current reference to the ndlp have been done.
2685 */
2686 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002687 return;
2688}
2689
2690/*
2691 * This routine handles processing a NameServer REG_LOGIN mailbox
2692 * command upon completion. It is setup in the LPFC_MBOXQ
2693 * as the completion routine when the command is
2694 * handed off to the SLI layer.
2695 */
2696void
James Smart2e0fef82007-06-17 19:56:36 -05002697lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002698{
James Smart21e9a0a2009-05-22 14:53:21 -04002699 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002700 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2701 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2702 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002703
2704 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05002705out:
James Smart21e9a0a2009-05-22 14:53:21 -04002706 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2707 "0260 Register NameServer error: 0x%x\n",
2708 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05002709 /* decrement the node reference count held for this
2710 * callback function.
2711 */
James Smart329f9bc2007-04-25 09:53:01 -04002712 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002713 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2714 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04002715 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04002716
2717 /* If no other thread is using the ndlp, free it */
2718 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05002719
James Smart92d7f7b2007-06-17 19:56:38 -05002720 if (phba->fc_topology == TOPOLOGY_LOOP) {
2721 /*
2722 * RegLogin failed, use loop map to make discovery
2723 * list
2724 */
2725 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002726
James Smart92d7f7b2007-06-17 19:56:38 -05002727 /* Start discovery */
2728 lpfc_disc_start(vport);
2729 return;
2730 }
2731 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05002732 return;
2733 }
2734
2735 pmb->context1 = NULL;
2736
dea31012005-04-17 16:05:31 -05002737 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04002738 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05002739 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002740 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002741
James Smart2e0fef82007-06-17 19:56:36 -05002742 if (vport->port_state < LPFC_VPORT_READY) {
2743 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05002744 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
2745 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
2746 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2747 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
2748 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
2749
2750 /* Issue SCR just before NameServer GID_FT Query */
2751 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05002752 }
2753
James Smart2e0fef82007-06-17 19:56:36 -05002754 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002755 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05002756 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05002757 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05002758 goto out;
dea31012005-04-17 16:05:31 -05002759 }
2760
James Smartfa4066b2008-01-11 01:53:27 -05002761 /* decrement the node reference count held for this
2762 * callback function.
2763 */
James Smart329f9bc2007-04-25 09:53:01 -04002764 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002765 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2766 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002767 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002768
2769 return;
2770}
2771
2772static void
James Smart2e0fef82007-06-17 19:56:36 -05002773lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002774{
James Smart2e0fef82007-06-17 19:56:36 -05002775 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2776 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05002777 struct lpfc_rport_data *rdata;
2778 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05002779 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002780
2781 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07002782 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2783 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05002784 rport_ids.port_id = ndlp->nlp_DID;
2785 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05002786
James Smart329f9bc2007-04-25 09:53:01 -04002787 /*
2788 * We leave our node pointer in rport->dd_data when we unregister a
2789 * FCP target port. But fc_remote_port_add zeros the space to which
2790 * rport->dd_data points. So, if we're reusing a previously
2791 * registered port, drop the reference that we took the last time we
2792 * registered the port.
2793 */
2794 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05002795 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04002796 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05002797
2798 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
2799 "rport add: did:x%x flg:x%x type x%x",
2800 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2801
James Smart2e0fef82007-06-17 19:56:36 -05002802 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04002803 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05002804 dev_printk(KERN_WARNING, &phba->pcidev->dev,
2805 "Warning: fc_remote_port_add failed\n");
2806 return;
2807 }
2808
2809 /* initialize static port data */
2810 rport->maxframe_size = ndlp->nlp_maxframe;
2811 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05002812 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04002813 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05002814
2815 if (ndlp->nlp_type & NLP_FCP_TARGET)
2816 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2817 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
2818 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2819
2820
2821 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
2822 fc_remote_port_rolechg(rport, rport_ids.roles);
2823
James Smart071fbd3d2006-04-15 11:53:20 -04002824 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002825 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04002826 ndlp->nlp_sid = rport->scsi_target_id;
2827 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002828 return;
2829}
2830
2831static void
James Smart2e0fef82007-06-17 19:56:36 -05002832lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002833{
2834 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04002835
James Smart858c9f62007-06-17 19:56:39 -05002836 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
2837 "rport delete: did:x%x flg:x%x type x%x",
2838 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2839
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002840 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05002841
2842 return;
2843}
2844
James Smartde0c5b32007-04-25 09:52:27 -04002845static void
James Smart2e0fef82007-06-17 19:56:36 -05002846lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04002847{
James Smart2e0fef82007-06-17 19:56:36 -05002848 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2849
2850 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002851 switch (state) {
2852 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002853 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002854 break;
2855 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002856 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002857 break;
2858 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002859 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002860 break;
2861 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002862 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002863 break;
2864 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002865 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002866 break;
2867 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002868 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002869 break;
2870 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002871 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002872 break;
2873 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002874 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002875 break;
2876 }
James Smart2e0fef82007-06-17 19:56:36 -05002877 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002878}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002879
James Smartde0c5b32007-04-25 09:52:27 -04002880static void
James Smart2e0fef82007-06-17 19:56:36 -05002881lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04002882 int old_state, int new_state)
2883{
James Smart2e0fef82007-06-17 19:56:36 -05002884 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2885
James Smartde0c5b32007-04-25 09:52:27 -04002886 if (new_state == NLP_STE_UNMAPPED_NODE) {
2887 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2888 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2889 ndlp->nlp_type |= NLP_FC_NODE;
2890 }
2891 if (new_state == NLP_STE_MAPPED_NODE)
2892 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2893 if (new_state == NLP_STE_NPR_NODE)
2894 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05002895
James Smartde0c5b32007-04-25 09:52:27 -04002896 /* Transport interface */
2897 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
2898 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002899 vport->phba->nport_event_cnt++;
2900 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002901 }
dea31012005-04-17 16:05:31 -05002902
James Smartde0c5b32007-04-25 09:52:27 -04002903 if (new_state == NLP_STE_MAPPED_NODE ||
2904 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05002905 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05002906 /*
2907 * Tell the fc transport about the port, if we haven't
2908 * already. If we have, and it's a scsi entity, be
2909 * sure to unblock any attached scsi devices
2910 */
2911 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002912 }
James Smartea2151b2008-09-07 11:52:10 -04002913 if ((new_state == NLP_STE_MAPPED_NODE) &&
2914 (vport->stat_data_enabled)) {
2915 /*
2916 * A new target is discovered, if there is no buffer for
2917 * statistical data collection allocate buffer.
2918 */
2919 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
2920 sizeof(struct lpfc_scsicmd_bkt),
2921 GFP_KERNEL);
2922
2923 if (!ndlp->lat_data)
2924 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2925 "0286 lpfc_nlp_state_cleanup failed to "
2926 "allocate statistical data buffer DID "
2927 "0x%x\n", ndlp->nlp_DID);
2928 }
James Smart858c9f62007-06-17 19:56:39 -05002929 /*
2930 * if we added to Mapped list, but the remote port
2931 * registration failed or assigned a target id outside
2932 * our presentable range - move the node to the
2933 * Unmapped List
2934 */
James Smartde0c5b32007-04-25 09:52:27 -04002935 if (new_state == NLP_STE_MAPPED_NODE &&
2936 (!ndlp->rport ||
2937 ndlp->rport->scsi_target_id == -1 ||
2938 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05002939 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002940 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05002941 spin_unlock_irq(shost->host_lock);
2942 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002943 }
James Smartde0c5b32007-04-25 09:52:27 -04002944}
2945
James Smart685f0bf2007-04-25 09:53:08 -04002946static char *
2947lpfc_nlp_state_name(char *buffer, size_t size, int state)
2948{
2949 static char *states[] = {
2950 [NLP_STE_UNUSED_NODE] = "UNUSED",
2951 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
2952 [NLP_STE_ADISC_ISSUE] = "ADISC",
2953 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
2954 [NLP_STE_PRLI_ISSUE] = "PRLI",
2955 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
2956 [NLP_STE_MAPPED_NODE] = "MAPPED",
2957 [NLP_STE_NPR_NODE] = "NPR",
2958 };
2959
James Smart311464e2007-08-02 11:10:37 -04002960 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04002961 strlcpy(buffer, states[state], size);
2962 else
2963 snprintf(buffer, size, "unknown (%d)", state);
2964 return buffer;
2965}
2966
James Smartde0c5b32007-04-25 09:52:27 -04002967void
James Smart2e0fef82007-06-17 19:56:36 -05002968lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2969 int state)
James Smartde0c5b32007-04-25 09:52:27 -04002970{
James Smart2e0fef82007-06-17 19:56:36 -05002971 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04002972 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04002973 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04002974
James Smarte8b62012007-08-02 11:10:09 -04002975 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2976 "0904 NPort state transition x%06x, %s -> %s\n",
2977 ndlp->nlp_DID,
2978 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
2979 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05002980
2981 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2982 "node statechg did:x%x old:%d ste:%d",
2983 ndlp->nlp_DID, old_state, state);
2984
James Smartde0c5b32007-04-25 09:52:27 -04002985 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04002986 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05002987 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002988 if (old_state == NLP_STE_UNMAPPED_NODE) {
2989 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
2990 ndlp->nlp_type &= ~NLP_FC_NODE;
2991 }
2992
James Smart685f0bf2007-04-25 09:53:08 -04002993 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05002994 spin_lock_irq(shost->host_lock);
2995 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2996 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002997 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05002998 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04002999
3000 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05003001 lpfc_nlp_counters(vport, state, 1);
3002 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04003003}
3004
3005void
James Smarte47c9092008-02-08 18:49:26 -05003006lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3007{
3008 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3009
3010 if (list_empty(&ndlp->nlp_listp)) {
3011 spin_lock_irq(shost->host_lock);
3012 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3013 spin_unlock_irq(shost->host_lock);
3014 }
3015}
3016
3017void
James Smart2e0fef82007-06-17 19:56:36 -05003018lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003019{
James Smart2e0fef82007-06-17 19:56:36 -05003020 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3021
James Smart0d2b6b82008-06-14 22:52:47 -04003022 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003023 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05003024 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3025 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003026 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05003027 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003028 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05003029 NLP_STE_UNUSED_NODE);
3030}
3031
Adrian Bunk4d9db012008-02-14 23:24:02 +02003032static void
James Smarte47c9092008-02-08 18:49:26 -05003033lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3034{
James Smart0d2b6b82008-06-14 22:52:47 -04003035 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003036 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3037 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3038 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3039 NLP_STE_UNUSED_NODE);
3040}
James Smart109f6ed2008-12-04 22:39:08 -05003041/**
James Smart3621a712009-04-06 18:47:14 -04003042 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05003043 * @vport: Pointer to Virtual Port object.
3044 * @ndlp: Pointer to FC node object.
3045 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04003046 *
3047 * This function is always called when node object need to be initialized.
3048 * It initializes all the fields of the node object. Although the reference
3049 * to phba from @ndlp can be obtained indirectly through it's reference to
3050 * @vport, a direct reference to phba is taken here by @ndlp. This is due
3051 * to the life-span of the @ndlp might go beyond the existence of @vport as
3052 * the final release of ndlp is determined by its reference count. And, the
3053 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05003054 **/
3055static inline void
3056lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3057 uint32_t did)
3058{
3059 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3060 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3061 init_timer(&ndlp->nlp_delayfunc);
3062 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3063 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3064 ndlp->nlp_DID = did;
3065 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04003066 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05003067 ndlp->nlp_sid = NLP_NO_SID;
3068 kref_init(&ndlp->kref);
3069 NLP_INT_NODE_ACT(ndlp);
3070 atomic_set(&ndlp->cmd_pending, 0);
3071 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
3072}
James Smarte47c9092008-02-08 18:49:26 -05003073
3074struct lpfc_nodelist *
3075lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3076 int state)
3077{
3078 struct lpfc_hba *phba = vport->phba;
3079 uint32_t did;
3080 unsigned long flags;
3081
3082 if (!ndlp)
3083 return NULL;
3084
3085 spin_lock_irqsave(&phba->ndlp_lock, flags);
3086 /* The ndlp should not be in memory free mode */
3087 if (NLP_CHK_FREE_REQ(ndlp)) {
3088 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3089 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3090 "0277 lpfc_enable_node: ndlp:x%p "
3091 "usgmap:x%x refcnt:%d\n",
3092 (void *)ndlp, ndlp->nlp_usg_map,
3093 atomic_read(&ndlp->kref.refcount));
3094 return NULL;
3095 }
3096 /* The ndlp should not already be in active mode */
3097 if (NLP_CHK_NODE_ACT(ndlp)) {
3098 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3099 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3100 "0278 lpfc_enable_node: ndlp:x%p "
3101 "usgmap:x%x refcnt:%d\n",
3102 (void *)ndlp, ndlp->nlp_usg_map,
3103 atomic_read(&ndlp->kref.refcount));
3104 return NULL;
3105 }
3106
3107 /* Keep the original DID */
3108 did = ndlp->nlp_DID;
3109
3110 /* re-initialize ndlp except of ndlp linked list pointer */
3111 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3112 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003113 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003114
3115 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3116
3117 if (state != NLP_STE_UNUSED_NODE)
3118 lpfc_nlp_set_state(vport, ndlp, state);
3119
3120 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3121 "node enable: did:x%x",
3122 ndlp->nlp_DID, 0, 0);
3123 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003124}
3125
3126void
James Smart2e0fef82007-06-17 19:56:36 -05003127lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003128{
James Smart87af33f2007-10-27 13:37:43 -04003129 /*
James Smartfa4066b2008-01-11 01:53:27 -05003130 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003131 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003132 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3133 * until ALL other outstanding threads have completed. We check
3134 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003135 */
James Smartfa4066b2008-01-11 01:53:27 -05003136 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3137 return;
James Smart51ef4c22007-08-02 11:10:31 -04003138 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003139 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003140 return;
dea31012005-04-17 16:05:31 -05003141}
3142
3143/*
3144 * Start / ReStart rescue timer for Discovery / RSCN handling
3145 */
3146void
James Smart2e0fef82007-06-17 19:56:36 -05003147lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003148{
James Smart2e0fef82007-06-17 19:56:36 -05003149 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3150 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003151 uint32_t tmo;
3152
James Smart2e0fef82007-06-17 19:56:36 -05003153 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003154 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003155 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3156 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003157 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003158 * FC spec states we need 3 * ratov for CT requests
3159 */
3160 tmo = ((phba->fc_ratov * 3) + 3);
3161 }
dea31012005-04-17 16:05:31 -05003162
James Smart858c9f62007-06-17 19:56:39 -05003163
3164 if (!timer_pending(&vport->fc_disctmo)) {
3165 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3166 "set disc timer: tmo:x%x state:x%x flg:x%x",
3167 tmo, vport->port_state, vport->fc_flag);
3168 }
3169
James Smart2e0fef82007-06-17 19:56:36 -05003170 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3171 spin_lock_irq(shost->host_lock);
3172 vport->fc_flag |= FC_DISC_TMO;
3173 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003174
3175 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003176 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3177 "0247 Start Discovery Timer state x%x "
3178 "Data: x%x x%lx x%x x%x\n",
3179 vport->port_state, tmo,
3180 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3181 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003182
3183 return;
3184}
3185
3186/*
3187 * Cancel rescue timer for Discovery / RSCN handling
3188 */
3189int
James Smart2e0fef82007-06-17 19:56:36 -05003190lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003191{
James Smart2e0fef82007-06-17 19:56:36 -05003192 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003193 unsigned long iflags;
3194
James Smart858c9f62007-06-17 19:56:39 -05003195 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3196 "can disc timer: state:x%x rtry:x%x flg:x%x",
3197 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3198
dea31012005-04-17 16:05:31 -05003199 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003200 if (vport->fc_flag & FC_DISC_TMO) {
3201 spin_lock_irqsave(shost->host_lock, iflags);
3202 vport->fc_flag &= ~FC_DISC_TMO;
3203 spin_unlock_irqrestore(shost->host_lock, iflags);
3204 del_timer_sync(&vport->fc_disctmo);
3205 spin_lock_irqsave(&vport->work_port_lock, iflags);
3206 vport->work_port_events &= ~WORKER_DISC_TMO;
3207 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003208 }
3209
3210 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003211 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3212 "0248 Cancel Discovery Timer state x%x "
3213 "Data: x%x x%x x%x\n",
3214 vport->port_state, vport->fc_flag,
3215 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003216 return 0;
dea31012005-04-17 16:05:31 -05003217}
3218
3219/*
3220 * Check specified ring for outstanding IOCB on the SLI queue
3221 * Return true if iocb matches the specified nport
3222 */
3223int
James Smart2e0fef82007-06-17 19:56:36 -05003224lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3225 struct lpfc_sli_ring *pring,
3226 struct lpfc_iocbq *iocb,
3227 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003228{
James Smart2e0fef82007-06-17 19:56:36 -05003229 struct lpfc_sli *psli = &phba->sli;
3230 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003231 struct lpfc_vport *vport = ndlp->vport;
3232
3233 if (iocb->vport != vport)
3234 return 0;
3235
dea31012005-04-17 16:05:31 -05003236 if (pring->ringno == LPFC_ELS_RING) {
3237 switch (icmd->ulpCommand) {
3238 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003239 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003240 return 1;
dea31012005-04-17 16:05:31 -05003241 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003242 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3243 return 1;
dea31012005-04-17 16:05:31 -05003244 case CMD_XMIT_ELS_RSP64_CX:
3245 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003246 return 1;
dea31012005-04-17 16:05:31 -05003247 }
James Smarta4bc3372006-12-02 13:34:16 -05003248 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003249
3250 } else if (pring->ringno == psli->fcp_ring) {
3251 /* Skip match check if waiting to relogin to FCP target */
3252 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003253 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003254 return 0;
dea31012005-04-17 16:05:31 -05003255 }
3256 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003257 return 1;
dea31012005-04-17 16:05:31 -05003258 }
3259 } else if (pring->ringno == psli->next_ring) {
3260
3261 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003262 return 0;
dea31012005-04-17 16:05:31 -05003263}
3264
3265/*
3266 * Free resources / clean up outstanding I/Os
3267 * associated with nlp_rpi in the LPFC_NODELIST entry.
3268 */
3269static int
James Smart2e0fef82007-06-17 19:56:36 -05003270lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003271{
James Smart2534ba72007-04-25 09:52:20 -04003272 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003273 struct lpfc_sli *psli;
3274 struct lpfc_sli_ring *pring;
3275 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003276 uint32_t i;
dea31012005-04-17 16:05:31 -05003277
James Smart92d7f7b2007-06-17 19:56:38 -05003278 lpfc_fabric_abort_nport(ndlp);
3279
dea31012005-04-17 16:05:31 -05003280 /*
3281 * Everything that matches on txcmplq will be returned
3282 * by firmware with a no rpi error.
3283 */
3284 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003285 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003286 /* Now process each ring */
3287 for (i = 0; i < psli->num_rings; i++) {
3288 pring = &psli->ring[i];
3289
James Smart2e0fef82007-06-17 19:56:36 -05003290 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003291 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003292 list) {
dea31012005-04-17 16:05:31 -05003293 /*
3294 * Check to see if iocb matches the nport we are
3295 * looking for
3296 */
James Smart92d7f7b2007-06-17 19:56:38 -05003297 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3298 ndlp))) {
dea31012005-04-17 16:05:31 -05003299 /* It matches, so deque and call compl
3300 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003301 list_move_tail(&iocb->list,
3302 &completions);
dea31012005-04-17 16:05:31 -05003303 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003304 }
3305 }
James Smart2e0fef82007-06-17 19:56:36 -05003306 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003307 }
3308 }
James Smart2534ba72007-04-25 09:52:20 -04003309
James Smarta257bf92009-04-06 18:48:10 -04003310 /* Cancel all the IOCBs from the completions list */
3311 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3312 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003313
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003314 return 0;
dea31012005-04-17 16:05:31 -05003315}
3316
3317/*
3318 * Free rpi associated with LPFC_NODELIST entry.
3319 * This routine is called from lpfc_freenode(), when we are removing
3320 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3321 * LOGO that completes successfully, and we are waiting to PLOGI back
3322 * to the remote NPort. In addition, it is called after we receive
3323 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3324 * we are waiting to PLOGI back to the remote NPort.
3325 */
3326int
James Smart2e0fef82007-06-17 19:56:36 -05003327lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003328{
James Smart2e0fef82007-06-17 19:56:36 -05003329 struct lpfc_hba *phba = vport->phba;
3330 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003331 int rc;
3332
James Smart21e9a0a2009-05-22 14:53:21 -04003333 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003334 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3335 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003336 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003337 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003338 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003339 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003340 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003341 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003342 }
dea31012005-04-17 16:05:31 -05003343 lpfc_no_rpi(phba, ndlp);
3344 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003345 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003346 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003347 return 1;
3348 }
3349 return 0;
3350}
3351
James Smartecfd03c2010-02-12 14:41:27 -05003352/**
3353 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
3354 * @phba: pointer to lpfc hba data structure.
3355 *
3356 * This routine is invoked to unregister all the currently registered RPIs
3357 * to the HBA.
3358 **/
3359void
3360lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
3361{
3362 struct lpfc_vport **vports;
3363 struct lpfc_nodelist *ndlp;
3364 struct Scsi_Host *shost;
3365 int i;
3366
3367 vports = lpfc_create_vport_work_array(phba);
3368 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3369 shost = lpfc_shost_from_vport(vports[i]);
3370 spin_lock_irq(shost->host_lock);
3371 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
3372 if (ndlp->nlp_flag & NLP_RPI_VALID)
3373 lpfc_unreg_rpi(vports[i], ndlp);
3374 }
3375 spin_unlock_irq(shost->host_lock);
3376 }
3377 lpfc_destroy_vport_work_array(phba, vports);
3378}
3379
James Smart92d7f7b2007-06-17 19:56:38 -05003380void
3381lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3382{
3383 struct lpfc_hba *phba = vport->phba;
3384 LPFC_MBOXQ_t *mbox;
3385 int rc;
3386
3387 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3388 if (mbox) {
3389 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3390 mbox->vport = vport;
3391 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003392 mbox->context1 = NULL;
3393 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003394 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003395 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003396
3397 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3398 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3399 "1836 Could not issue "
3400 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003401 }
3402}
3403
3404void
3405lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3406{
3407 struct lpfc_hba *phba = vport->phba;
3408 LPFC_MBOXQ_t *mbox;
3409 int rc;
3410
3411 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3412 if (mbox) {
3413 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3414 mbox->vport = vport;
3415 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003416 mbox->context1 = NULL;
3417 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003418 if (rc != MBX_TIMEOUT)
3419 mempool_free(mbox, phba->mbox_mem_pool);
3420
3421 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003422 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3423 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003424 "unreg_did (default rpis) status %d\n",
3425 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003426 }
3427}
3428
dea31012005-04-17 16:05:31 -05003429/*
3430 * Free resources associated with LPFC_NODELIST entry
3431 * so it can be freed.
3432 */
3433static int
James Smart2e0fef82007-06-17 19:56:36 -05003434lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003435{
James Smart2e0fef82007-06-17 19:56:36 -05003436 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3437 struct lpfc_hba *phba = vport->phba;
3438 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003439 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003440
3441 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003442 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3443 "0900 Cleanup node for NPort x%x "
3444 "Data: x%x x%x x%x\n",
3445 ndlp->nlp_DID, ndlp->nlp_flag,
3446 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003447 if (NLP_CHK_FREE_REQ(ndlp)) {
3448 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3449 "0280 lpfc_cleanup_node: ndlp:x%p "
3450 "usgmap:x%x refcnt:%d\n",
3451 (void *)ndlp, ndlp->nlp_usg_map,
3452 atomic_read(&ndlp->kref.refcount));
3453 lpfc_dequeue_node(vport, ndlp);
3454 } else {
3455 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3456 "0281 lpfc_cleanup_node: ndlp:x%p "
3457 "usgmap:x%x refcnt:%d\n",
3458 (void *)ndlp, ndlp->nlp_usg_map,
3459 atomic_read(&ndlp->kref.refcount));
3460 lpfc_disable_node(vport, ndlp);
3461 }
dea31012005-04-17 16:05:31 -05003462
dea31012005-04-17 16:05:31 -05003463 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3464 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003465 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003466 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3467 mb->context2 = NULL;
3468 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3469 }
3470 }
James Smart33ccf8d2006-08-17 11:57:58 -04003471
James Smart2e0fef82007-06-17 19:56:36 -05003472 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003473 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003474 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003475 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003476 mp = (struct lpfc_dmabuf *) (mb->context1);
3477 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003478 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003479 kfree(mp);
3480 }
3481 list_del(&mb->list);
3482 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003483 /* We shall not invoke the lpfc_nlp_put to decrement
3484 * the ndlp reference count as we are in the process
3485 * of lpfc_nlp_release.
3486 */
dea31012005-04-17 16:05:31 -05003487 }
3488 }
James Smart2e0fef82007-06-17 19:56:36 -05003489 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003490
James Smarte47c9092008-02-08 18:49:26 -05003491 lpfc_els_abort(phba, ndlp);
3492
James Smart2e0fef82007-06-17 19:56:36 -05003493 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003494 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003495 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003496
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003497 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003498 del_timer_sync(&ndlp->nlp_delayfunc);
3499
James Smart0d2b6b82008-06-14 22:52:47 -04003500 list_del_init(&ndlp->els_retry_evt.evt_listp);
3501 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003502
James Smart2e0fef82007-06-17 19:56:36 -05003503 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003504
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003505 return 0;
dea31012005-04-17 16:05:31 -05003506}
3507
3508/*
3509 * Check to see if we can free the nlp back to the freelist.
3510 * If we are in the middle of using the nlp in the discovery state
3511 * machine, defer the free till we reach the end of the state machine.
3512 */
James Smart329f9bc2007-04-25 09:53:01 -04003513static void
James Smart2e0fef82007-06-17 19:56:36 -05003514lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003515{
James Smarta8adb832007-10-27 13:37:53 -04003516 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003517 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003518 LPFC_MBOXQ_t *mbox;
3519 int rc;
dea31012005-04-17 16:05:31 -05003520
James Smart0d2b6b82008-06-14 22:52:47 -04003521 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003522 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3523 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003524 /* For this case we need to cleanup the default rpi
3525 * allocated by the firmware.
3526 */
3527 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3528 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003529 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003530 (uint8_t *) &vport->fc_sparam, mbox, 0);
3531 if (rc) {
3532 mempool_free(mbox, phba->mbox_mem_pool);
3533 }
3534 else {
3535 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3536 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3537 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003538 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003539 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3540 if (rc == MBX_NOT_FINISHED) {
3541 mempool_free(mbox, phba->mbox_mem_pool);
3542 }
3543 }
3544 }
3545 }
James Smart2e0fef82007-06-17 19:56:36 -05003546 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003547
James Smart2e0fef82007-06-17 19:56:36 -05003548 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003549 * We can get here with a non-NULL ndlp->rport because when we
3550 * unregister a rport we don't break the rport/node linkage. So if we
3551 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003552 */
James Smart92d7f7b2007-06-17 19:56:38 -05003553 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003554 rdata = ndlp->rport->dd_data;
3555 rdata->pnode = NULL;
3556 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003557 }
dea31012005-04-17 16:05:31 -05003558}
3559
3560static int
James Smart2e0fef82007-06-17 19:56:36 -05003561lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3562 uint32_t did)
dea31012005-04-17 16:05:31 -05003563{
James Smart2e0fef82007-06-17 19:56:36 -05003564 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003565
3566 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003567 return 0;
dea31012005-04-17 16:05:31 -05003568
dea31012005-04-17 16:05:31 -05003569 /* First check for Direct match */
3570 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003571 return 1;
dea31012005-04-17 16:05:31 -05003572
3573 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003574 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003575 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003576 return 0;
dea31012005-04-17 16:05:31 -05003577 }
3578
3579 matchdid.un.word = did;
3580 ndlpdid.un.word = ndlp->nlp_DID;
3581 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3582 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3583 (mydid.un.b.area == matchdid.un.b.area)) {
3584 if ((ndlpdid.un.b.domain == 0) &&
3585 (ndlpdid.un.b.area == 0)) {
3586 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003587 return 1;
dea31012005-04-17 16:05:31 -05003588 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003589 return 0;
dea31012005-04-17 16:05:31 -05003590 }
3591
3592 matchdid.un.word = ndlp->nlp_DID;
3593 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3594 (mydid.un.b.area == ndlpdid.un.b.area)) {
3595 if ((matchdid.un.b.domain == 0) &&
3596 (matchdid.un.b.area == 0)) {
3597 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003598 return 1;
dea31012005-04-17 16:05:31 -05003599 }
3600 }
3601 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003602 return 0;
dea31012005-04-17 16:05:31 -05003603}
3604
James Smart685f0bf2007-04-25 09:53:08 -04003605/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003606static struct lpfc_nodelist *
3607__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003608{
James Smart2fb9bd82006-12-02 13:33:57 -05003609 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003610 uint32_t data1;
3611
James Smart2e0fef82007-06-17 19:56:36 -05003612 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3613 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04003614 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3615 ((uint32_t) ndlp->nlp_xri << 16) |
3616 ((uint32_t) ndlp->nlp_type << 8) |
3617 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04003618 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3619 "0929 FIND node DID "
3620 "Data: x%p x%x x%x x%x\n",
3621 ndlp, ndlp->nlp_DID,
3622 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04003623 return ndlp;
dea31012005-04-17 16:05:31 -05003624 }
3625 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003626
dea31012005-04-17 16:05:31 -05003627 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04003628 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3629 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05003630 return NULL;
3631}
3632
3633struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003634lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003635{
James Smart2e0fef82007-06-17 19:56:36 -05003636 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003637 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003638
James Smart2e0fef82007-06-17 19:56:36 -05003639 spin_lock_irq(shost->host_lock);
3640 ndlp = __lpfc_findnode_did(vport, did);
3641 spin_unlock_irq(shost->host_lock);
3642 return ndlp;
3643}
3644
3645struct lpfc_nodelist *
3646lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3647{
3648 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3649 struct lpfc_nodelist *ndlp;
3650
3651 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003652 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05003653 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
3654 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05003655 return NULL;
3656 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05003657 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003658 if (!ndlp)
3659 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003660 lpfc_nlp_init(vport, ndlp, did);
3661 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3662 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003663 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003664 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003665 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05003666 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3667 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
3668 if (!ndlp)
3669 return NULL;
3670 spin_lock_irq(shost->host_lock);
3671 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3672 spin_unlock_irq(shost->host_lock);
3673 return ndlp;
dea31012005-04-17 16:05:31 -05003674 }
James Smarte47c9092008-02-08 18:49:26 -05003675
James Smart58da1ff2008-04-07 10:15:56 -04003676 if ((vport->fc_flag & FC_RSCN_MODE) &&
3677 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003678 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04003679 /* If we've already recieved a PLOGI from this NPort
3680 * we don't need to try to discover it again.
3681 */
3682 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
3683 return NULL;
3684
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003685 /* Since this node is marked for discovery,
3686 * delay timeout is not needed.
3687 */
James Smart0d2b6b82008-06-14 22:52:47 -04003688 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04003689 spin_lock_irq(shost->host_lock);
3690 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3691 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04003692 } else
dea31012005-04-17 16:05:31 -05003693 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003694 } else {
James Smart87af33f2007-10-27 13:37:43 -04003695 /* If we've already recieved a PLOGI from this NPort,
3696 * or we are already in the process of discovery on it,
3697 * we don't need to try to discover it again.
3698 */
James Smart685f0bf2007-04-25 09:53:08 -04003699 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04003700 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3701 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05003702 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003703 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3704 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003705 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003706 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003707 }
3708 return ndlp;
3709}
3710
3711/* Build a list of nodes to discover based on the loopmap */
3712void
James Smart2e0fef82007-06-17 19:56:36 -05003713lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003714{
James Smart2e0fef82007-06-17 19:56:36 -05003715 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003716 int j;
3717 uint32_t alpa, index;
3718
James Smart2e0fef82007-06-17 19:56:36 -05003719 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003720 return;
James Smart2e0fef82007-06-17 19:56:36 -05003721
3722 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05003723 return;
dea31012005-04-17 16:05:31 -05003724
3725 /* Check for loop map present or not */
3726 if (phba->alpa_map[0]) {
3727 for (j = 1; j <= phba->alpa_map[0]; j++) {
3728 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05003729 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05003730 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003731 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003732 }
3733 } else {
3734 /* No alpamap, so try all alpa's */
3735 for (j = 0; j < FC_MAXLOOP; j++) {
3736 /* If cfg_scan_down is set, start from highest
3737 * ALPA (0xef) to lowest (0x1).
3738 */
James Smart3de2a652007-08-02 11:09:59 -04003739 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05003740 index = j;
3741 else
3742 index = FC_MAXLOOP - j - 1;
3743 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05003744 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05003745 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003746 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003747 }
3748 }
3749 return;
3750}
3751
dea31012005-04-17 16:05:31 -05003752void
James Smart2e0fef82007-06-17 19:56:36 -05003753lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003754{
dea31012005-04-17 16:05:31 -05003755 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05003756 struct lpfc_sli *psli = &phba->sli;
3757 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
3758 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
3759 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
3760 int rc;
3761
James Smart92d7f7b2007-06-17 19:56:38 -05003762 /*
3763 * if it's not a physical port or if we already send
3764 * clear_la then don't send it.
3765 */
3766 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04003767 (vport->port_type != LPFC_PHYSICAL_PORT) ||
3768 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05003769 return;
3770
James Smart2e0fef82007-06-17 19:56:36 -05003771 /* Link up discovery */
3772 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
3773 phba->link_state = LPFC_CLEAR_LA;
3774 lpfc_clear_la(phba, mbox);
3775 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
3776 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003777 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05003778 if (rc == MBX_NOT_FINISHED) {
3779 mempool_free(mbox, phba->mbox_mem_pool);
3780 lpfc_disc_flush_list(vport);
3781 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3782 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3783 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05003784 phba->link_state = LPFC_HBA_ERROR;
3785 }
3786 }
3787}
3788
3789/* Reg_vpi to tell firmware to resume normal operations */
3790void
3791lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
3792{
3793 LPFC_MBOXQ_t *regvpimbox;
3794
3795 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3796 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04003797 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003798 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
3799 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003800 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05003801 == MBX_NOT_FINISHED) {
3802 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003803 }
3804 }
3805}
3806
3807/* Start Link up / RSCN discovery on NPR nodes */
3808void
3809lpfc_disc_start(struct lpfc_vport *vport)
3810{
3811 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3812 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04003813 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05003814 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04003815 int did_changed;
dea31012005-04-17 16:05:31 -05003816
James Smart2e0fef82007-06-17 19:56:36 -05003817 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003818 return;
James Smart2e0fef82007-06-17 19:56:36 -05003819
3820 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05003821 clear_la_pending = 1;
3822 else
3823 clear_la_pending = 0;
3824
James Smart2e0fef82007-06-17 19:56:36 -05003825 if (vport->port_state < LPFC_VPORT_READY)
3826 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05003827
James Smart2e0fef82007-06-17 19:56:36 -05003828 lpfc_set_disctmo(vport);
3829
3830 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05003831 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003832 else
dea31012005-04-17 16:05:31 -05003833 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003834
3835 vport->fc_prevDID = vport->fc_myDID;
3836 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05003837
3838 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003839 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3840 "0202 Start Discovery hba state x%x "
3841 "Data: x%x x%x x%x\n",
3842 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
3843 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003844
3845 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003846 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05003847
3848 if (num_sent)
3849 return;
3850
James Smart92d7f7b2007-06-17 19:56:38 -05003851 /*
3852 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
3853 * continue discovery.
3854 */
3855 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05003856 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04003857 !(vport->fc_flag & FC_RSCN_MODE) &&
3858 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003859 lpfc_issue_reg_vpi(phba, vport);
3860 return;
3861 }
James Smart2e0fef82007-06-17 19:56:36 -05003862
James Smart92d7f7b2007-06-17 19:56:38 -05003863 /*
3864 * For SLI2, we need to set port_state to READY and continue
3865 * discovery.
3866 */
3867 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
3868 /* If we get here, there is nothing to ADISC */
3869 if (vport->port_type == LPFC_PHYSICAL_PORT)
3870 lpfc_issue_clear_la(phba, vport);
3871
3872 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05003873 vport->num_disc_nodes = 0;
3874 /* go thru NPR nodes and issue ELS PLOGIs */
3875 if (vport->fc_npr_cnt)
3876 lpfc_els_disc_plogi(vport);
3877
3878 if (!vport->num_disc_nodes) {
3879 spin_lock_irq(shost->host_lock);
3880 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3881 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003882 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05003883 }
3884 }
James Smart92d7f7b2007-06-17 19:56:38 -05003885 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05003886 } else {
3887 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003888 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05003889
3890 if (num_sent)
3891 return;
3892
James Smart2e0fef82007-06-17 19:56:36 -05003893 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05003894 /* Check to see if more RSCNs came in while we
3895 * were processing this one.
3896 */
James Smart2e0fef82007-06-17 19:56:36 -05003897 if ((vport->fc_rscn_id_cnt == 0) &&
3898 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
3899 spin_lock_irq(shost->host_lock);
3900 vport->fc_flag &= ~FC_RSCN_MODE;
3901 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003902 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003903 } else
James Smart2e0fef82007-06-17 19:56:36 -05003904 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05003905 }
3906 }
3907 return;
3908}
3909
3910/*
3911 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
3912 * ring the match the sppecified nodelist.
3913 */
3914static void
James Smart2e0fef82007-06-17 19:56:36 -05003915lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003916{
James Smart2534ba72007-04-25 09:52:20 -04003917 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003918 struct lpfc_sli *psli;
3919 IOCB_t *icmd;
3920 struct lpfc_iocbq *iocb, *next_iocb;
3921 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003922
3923 psli = &phba->sli;
3924 pring = &psli->ring[LPFC_ELS_RING];
3925
3926 /* Error matching iocb on txq or txcmplq
3927 * First check the txq.
3928 */
James Smart2e0fef82007-06-17 19:56:36 -05003929 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003930 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3931 if (iocb->context1 != ndlp) {
3932 continue;
3933 }
3934 icmd = &iocb->iocb;
3935 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
3936 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
3937
James Smart2534ba72007-04-25 09:52:20 -04003938 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05003939 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003940 }
3941 }
3942
3943 /* Next check the txcmplq */
3944 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
3945 if (iocb->context1 != ndlp) {
3946 continue;
3947 }
3948 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05003949 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
3950 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04003951 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05003952 }
3953 }
James Smart2e0fef82007-06-17 19:56:36 -05003954 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003955
James Smarta257bf92009-04-06 18:48:10 -04003956 /* Cancel all the IOCBs from the completions list */
3957 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3958 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003959}
3960
Adrian Bunka6ababd2007-11-05 18:07:33 +01003961static void
James Smart2e0fef82007-06-17 19:56:36 -05003962lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003963{
3964 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05003965 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003966
James Smart2e0fef82007-06-17 19:56:36 -05003967 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
3968 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04003969 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003970 if (!NLP_CHK_NODE_ACT(ndlp))
3971 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003972 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3973 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
3974 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04003975 }
dea31012005-04-17 16:05:31 -05003976 }
3977 }
dea31012005-04-17 16:05:31 -05003978}
3979
James Smart92d7f7b2007-06-17 19:56:38 -05003980void
3981lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
3982{
3983 lpfc_els_flush_rscn(vport);
3984 lpfc_els_flush_cmd(vport);
3985 lpfc_disc_flush_list(vport);
3986}
3987
dea31012005-04-17 16:05:31 -05003988/*****************************************************************************/
3989/*
3990 * NAME: lpfc_disc_timeout
3991 *
3992 * FUNCTION: Fibre Channel driver discovery timeout routine.
3993 *
3994 * EXECUTION ENVIRONMENT: interrupt only
3995 *
3996 * CALLED FROM:
3997 * Timer function
3998 *
3999 * RETURNS:
4000 * none
4001 */
4002/*****************************************************************************/
4003void
4004lpfc_disc_timeout(unsigned long ptr)
4005{
James Smart2e0fef82007-06-17 19:56:36 -05004006 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4007 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04004008 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004009 unsigned long flags = 0;
4010
4011 if (unlikely(!phba))
4012 return;
4013
James Smart5e9d9b82008-06-14 22:52:53 -04004014 spin_lock_irqsave(&vport->work_port_lock, flags);
4015 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4016 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05004017 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04004018 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05004019
James Smart5e9d9b82008-06-14 22:52:53 -04004020 if (!tmo_posted)
4021 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004022 return;
4023}
4024
4025static void
James Smart2e0fef82007-06-17 19:56:36 -05004026lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004027{
James Smart2e0fef82007-06-17 19:56:36 -05004028 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4029 struct lpfc_hba *phba = vport->phba;
4030 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004031 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05004032 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05004033 int rc, clrlaerr = 0;
4034
James Smart2e0fef82007-06-17 19:56:36 -05004035 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05004036 return;
4037
James Smart2e0fef82007-06-17 19:56:36 -05004038 spin_lock_irq(shost->host_lock);
4039 vport->fc_flag &= ~FC_DISC_TMO;
4040 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004041
James Smart858c9f62007-06-17 19:56:39 -05004042 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4043 "disc timeout: state:x%x rtry:x%x flg:x%x",
4044 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4045
James Smart2e0fef82007-06-17 19:56:36 -05004046 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05004047
4048 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05004049 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
4050 * FAN
4051 */
4052 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04004053 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4054 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004055 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05004056 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004057 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004058 if (!NLP_CHK_NODE_ACT(ndlp))
4059 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004060 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4061 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004062 if (ndlp->nlp_type & NLP_FABRIC) {
4063 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05004064 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004065
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004066 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004067 /* Fail outstanding IO now since device
4068 * is marked for PLOGI.
4069 */
James Smart2e0fef82007-06-17 19:56:36 -05004070 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004071 }
4072 }
James Smart92d7f7b2007-06-17 19:56:38 -05004073 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05004074 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05004075 return;
James Smart92d7f7b2007-06-17 19:56:38 -05004076 }
dea31012005-04-17 16:05:31 -05004077 break;
4078
James Smart92d7f7b2007-06-17 19:56:38 -05004079 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05004080 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05004081 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05004082 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04004083 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4084 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04004085 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05004086
4087 /* Assume no Fabric and go on with discovery.
4088 * Check for outstanding ELS FLOGI to abort.
4089 */
4090
4091 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05004092 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004093
4094 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05004095 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05004096 break;
4097
4098 case LPFC_FABRIC_CFG_LINK:
4099 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4100 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04004101 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4102 "0223 Timeout while waiting for "
4103 "NameServer login\n");
dea31012005-04-17 16:05:31 -05004104 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05004105 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05004106 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04004107 lpfc_els_abort(phba, ndlp);
4108
4109 /* ReStart discovery */
4110 goto restart_disc;
dea31012005-04-17 16:05:31 -05004111
4112 case LPFC_NS_QRY:
4113 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04004114 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4115 "0224 NameServer Query timeout "
4116 "Data: x%x x%x\n",
4117 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004118
James Smart92d7f7b2007-06-17 19:56:38 -05004119 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4120 /* Try it one more time */
4121 vport->fc_ns_retry++;
4122 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4123 vport->fc_ns_retry, 0);
4124 if (rc == 0)
4125 break;
dea31012005-04-17 16:05:31 -05004126 }
James Smart92d7f7b2007-06-17 19:56:38 -05004127 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05004128
James Smart87af33f2007-10-27 13:37:43 -04004129restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05004130 /*
4131 * Discovery is over.
4132 * set port_state to PORT_READY if SLI2.
4133 * cmpl_reg_vpi will set port_state to READY for SLI3.
4134 */
James Smart3772a992009-05-22 14:50:54 -04004135 if (phba->sli_rev < LPFC_SLI_REV4) {
4136 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4137 lpfc_issue_reg_vpi(phba, vport);
4138 else { /* NPIV Not enabled */
4139 lpfc_issue_clear_la(phba, vport);
4140 vport->port_state = LPFC_VPORT_READY;
4141 }
dea31012005-04-17 16:05:31 -05004142 }
4143
4144 /* Setup and issue mailbox INITIALIZE LINK command */
4145 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4146 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004147 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4148 "0206 Device Discovery "
4149 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004150 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004151 break;
4152 }
4153
4154 lpfc_linkdown(phba);
4155 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4156 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004157 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004158 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004159 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004160 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004161 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004162 if (rc == MBX_NOT_FINISHED)
4163 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4164
4165 break;
4166
4167 case LPFC_DISC_AUTH:
4168 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004169 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4170 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004171 lpfc_disc_flush_list(vport);
4172
James Smart92d7f7b2007-06-17 19:56:38 -05004173 /*
4174 * set port_state to PORT_READY if SLI2.
4175 * cmpl_reg_vpi will set port_state to READY for SLI3.
4176 */
James Smart3772a992009-05-22 14:50:54 -04004177 if (phba->sli_rev < LPFC_SLI_REV4) {
4178 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4179 lpfc_issue_reg_vpi(phba, vport);
4180 else { /* NPIV Not enabled */
4181 lpfc_issue_clear_la(phba, vport);
4182 vport->port_state = LPFC_VPORT_READY;
4183 }
dea31012005-04-17 16:05:31 -05004184 }
4185 break;
4186
James Smart2e0fef82007-06-17 19:56:36 -05004187 case LPFC_VPORT_READY:
4188 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004189 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4190 "0231 RSCN timeout Data: x%x "
4191 "x%x\n",
4192 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004193
4194 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004195 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004196
James Smart2e0fef82007-06-17 19:56:36 -05004197 lpfc_els_flush_rscn(vport);
4198 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004199 }
4200 break;
James Smart2e0fef82007-06-17 19:56:36 -05004201
James Smart92d7f7b2007-06-17 19:56:38 -05004202 default:
James Smarte8b62012007-08-02 11:10:09 -04004203 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004204 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004205 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004206 break;
4207 }
4208
4209 switch (phba->link_state) {
4210 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004211 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004212 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4213 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004214 clrlaerr = 1;
4215 break;
4216
James Smart09372822008-01-11 01:52:54 -05004217 case LPFC_LINK_UP:
4218 lpfc_issue_clear_la(phba, vport);
4219 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004220 case LPFC_LINK_UNKNOWN:
4221 case LPFC_WARM_START:
4222 case LPFC_INIT_START:
4223 case LPFC_INIT_MBX_CMDS:
4224 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004225 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004226 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4227 "0230 Unexpected timeout, hba link "
4228 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004229 clrlaerr = 1;
4230 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004231
4232 case LPFC_HBA_READY:
4233 break;
dea31012005-04-17 16:05:31 -05004234 }
4235
4236 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004237 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004238 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004239 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4240 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004241 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004242 }
4243
4244 return;
4245}
4246
dea31012005-04-17 16:05:31 -05004247/*
4248 * This routine handles processing a NameServer REG_LOGIN mailbox
4249 * command upon completion. It is setup in the LPFC_MBOXQ
4250 * as the completion routine when the command is
4251 * handed off to the SLI layer.
4252 */
4253void
James Smart2e0fef82007-06-17 19:56:36 -05004254lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004255{
James Smart04c68492009-05-22 14:52:52 -04004256 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004257 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4258 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4259 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004260
4261 pmb->context1 = NULL;
4262
dea31012005-04-17 16:05:31 -05004263 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004264 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004265 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004266 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004267
James Smart2e0fef82007-06-17 19:56:36 -05004268 /*
4269 * Start issuing Fabric-Device Management Interface (FDMI) command to
4270 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4271 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004272 */
James Smart2e0fef82007-06-17 19:56:36 -05004273
James Smart3de2a652007-08-02 11:09:59 -04004274 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004275 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4276 else
4277 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004278
James Smartfa4066b2008-01-11 01:53:27 -05004279 /* decrement the node reference count held for this callback
4280 * function.
4281 */
James Smart329f9bc2007-04-25 09:53:01 -04004282 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004283 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4284 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004285 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004286
4287 return;
4288}
4289
James Smart685f0bf2007-04-25 09:53:08 -04004290static int
4291lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4292{
4293 uint16_t *rpi = param;
4294
4295 return ndlp->nlp_rpi == *rpi;
4296}
4297
4298static int
4299lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4300{
4301 return memcmp(&ndlp->nlp_portname, param,
4302 sizeof(ndlp->nlp_portname)) == 0;
4303}
4304
Adrian Bunka6ababd2007-11-05 18:07:33 +01004305static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004306__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004307{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004308 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004309
James Smart2e0fef82007-06-17 19:56:36 -05004310 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004311 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004312 return ndlp;
4313 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004314 return NULL;
dea31012005-04-17 16:05:31 -05004315}
4316
James Smart685f0bf2007-04-25 09:53:08 -04004317/*
4318 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004319 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004320 */
4321struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004322__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004323{
James Smart2e0fef82007-06-17 19:56:36 -05004324 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004325}
4326
James Smart488d1462006-03-07 15:02:37 -05004327/*
James Smart685f0bf2007-04-25 09:53:08 -04004328 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004329 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004330 */
4331struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004332lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004333{
James Smart2e0fef82007-06-17 19:56:36 -05004334 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004335 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004336
James Smart2e0fef82007-06-17 19:56:36 -05004337 spin_lock_irq(shost->host_lock);
4338 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4339 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004340 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004341}
4342
dea31012005-04-17 16:05:31 -05004343void
James Smart2e0fef82007-06-17 19:56:36 -05004344lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4345 uint32_t did)
dea31012005-04-17 16:05:31 -05004346{
4347 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004348
4349 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004350 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004351
4352 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4353 "node init: did:x%x",
4354 ndlp->nlp_DID, 0, 0);
4355
dea31012005-04-17 16:05:31 -05004356 return;
4357}
James Smart329f9bc2007-04-25 09:53:01 -04004358
James Smart98c9ea52007-10-27 13:37:33 -04004359/* This routine releases all resources associated with a specifc NPort's ndlp
4360 * and mempool_free's the nodelist.
4361 */
James Smart311464e2007-08-02 11:10:37 -04004362static void
James Smart329f9bc2007-04-25 09:53:01 -04004363lpfc_nlp_release(struct kref *kref)
4364{
James Smarte47c9092008-02-08 18:49:26 -05004365 struct lpfc_hba *phba;
4366 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004367 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4368 kref);
James Smart858c9f62007-06-17 19:56:39 -05004369
4370 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4371 "node release: did:x%x flg:x%x type:x%x",
4372 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4373
James Smarte47c9092008-02-08 18:49:26 -05004374 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4375 "0279 lpfc_nlp_release: ndlp:x%p "
4376 "usgmap:x%x refcnt:%d\n",
4377 (void *)ndlp, ndlp->nlp_usg_map,
4378 atomic_read(&ndlp->kref.refcount));
4379
4380 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004381 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004382
4383 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004384 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004385 spin_lock_irqsave(&phba->ndlp_lock, flags);
4386 NLP_CLR_NODE_ACT(ndlp);
4387 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4388
4389 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004390 if (NLP_CHK_FREE_REQ(ndlp)) {
4391 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004392 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004393 }
James Smart329f9bc2007-04-25 09:53:01 -04004394}
4395
James Smart98c9ea52007-10-27 13:37:33 -04004396/* This routine bumps the reference count for a ndlp structure to ensure
4397 * that one discovery thread won't free a ndlp while another discovery thread
4398 * is using it.
4399 */
James Smart329f9bc2007-04-25 09:53:01 -04004400struct lpfc_nodelist *
4401lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4402{
James Smarte47c9092008-02-08 18:49:26 -05004403 struct lpfc_hba *phba;
4404 unsigned long flags;
4405
James Smart98c9ea52007-10-27 13:37:33 -04004406 if (ndlp) {
4407 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4408 "node get: did:x%x flg:x%x refcnt:x%x",
4409 ndlp->nlp_DID, ndlp->nlp_flag,
4410 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004411 /* The check of ndlp usage to prevent incrementing the
4412 * ndlp reference count that is in the process of being
4413 * released.
4414 */
James Smarta257bf92009-04-06 18:48:10 -04004415 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004416 spin_lock_irqsave(&phba->ndlp_lock, flags);
4417 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4418 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4419 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4420 "0276 lpfc_nlp_get: ndlp:x%p "
4421 "usgmap:x%x refcnt:%d\n",
4422 (void *)ndlp, ndlp->nlp_usg_map,
4423 atomic_read(&ndlp->kref.refcount));
4424 return NULL;
4425 } else
4426 kref_get(&ndlp->kref);
4427 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004428 }
James Smart329f9bc2007-04-25 09:53:01 -04004429 return ndlp;
4430}
4431
James Smart98c9ea52007-10-27 13:37:33 -04004432/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004433 * count goes to 0, this indicates the the associated nodelist should be
4434 * freed. Returning 1 indicates the ndlp resource has been released; on the
4435 * other hand, returning 0 indicates the ndlp resource has not been released
4436 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004437 */
James Smart329f9bc2007-04-25 09:53:01 -04004438int
4439lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4440{
James Smarte47c9092008-02-08 18:49:26 -05004441 struct lpfc_hba *phba;
4442 unsigned long flags;
4443
4444 if (!ndlp)
4445 return 1;
4446
4447 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4448 "node put: did:x%x flg:x%x refcnt:x%x",
4449 ndlp->nlp_DID, ndlp->nlp_flag,
4450 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004451 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004452 spin_lock_irqsave(&phba->ndlp_lock, flags);
4453 /* Check the ndlp memory free acknowledge flag to avoid the
4454 * possible race condition that kref_put got invoked again
4455 * after previous one has done ndlp memory free.
4456 */
4457 if (NLP_CHK_FREE_ACK(ndlp)) {
4458 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4459 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4460 "0274 lpfc_nlp_put: ndlp:x%p "
4461 "usgmap:x%x refcnt:%d\n",
4462 (void *)ndlp, ndlp->nlp_usg_map,
4463 atomic_read(&ndlp->kref.refcount));
4464 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004465 }
James Smarte47c9092008-02-08 18:49:26 -05004466 /* Check the ndlp inactivate log flag to avoid the possible
4467 * race condition that kref_put got invoked again after ndlp
4468 * is already in inactivating state.
4469 */
4470 if (NLP_CHK_IACT_REQ(ndlp)) {
4471 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4472 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4473 "0275 lpfc_nlp_put: ndlp:x%p "
4474 "usgmap:x%x refcnt:%d\n",
4475 (void *)ndlp, ndlp->nlp_usg_map,
4476 atomic_read(&ndlp->kref.refcount));
4477 return 1;
4478 }
4479 /* For last put, mark the ndlp usage flags to make sure no
4480 * other kref_get and kref_put on the same ndlp shall get
4481 * in between the process when the final kref_put has been
4482 * invoked on this ndlp.
4483 */
4484 if (atomic_read(&ndlp->kref.refcount) == 1) {
4485 /* Indicate ndlp is put to inactive state. */
4486 NLP_SET_IACT_REQ(ndlp);
4487 /* Acknowledge ndlp memory free has been seen. */
4488 if (NLP_CHK_FREE_REQ(ndlp))
4489 NLP_SET_FREE_ACK(ndlp);
4490 }
4491 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4492 /* Note, the kref_put returns 1 when decrementing a reference
4493 * count that was 1, it invokes the release callback function,
4494 * but it still left the reference count as 1 (not actually
4495 * performs the last decrementation). Otherwise, it actually
4496 * decrements the reference count and returns 0.
4497 */
4498 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004499}
James Smart98c9ea52007-10-27 13:37:33 -04004500
4501/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004502 * by any other discovery thread. This routine returns 1 if the
4503 * ndlp has been freed. A return value of 0 indicates the ndlp is
4504 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004505 */
4506int
4507lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4508{
4509 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4510 "node not used: did:x%x flg:x%x refcnt:x%x",
4511 ndlp->nlp_DID, ndlp->nlp_flag,
4512 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004513 if (atomic_read(&ndlp->kref.refcount) == 1)
4514 if (lpfc_nlp_put(ndlp))
4515 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004516 return 0;
4517}
James Smart6fb120a2009-05-22 14:52:59 -04004518
4519/**
4520 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4521 * @phba: Pointer to hba context object.
4522 *
4523 * This function iterate through all FC nodes associated
4524 * will all vports to check if there is any node with
4525 * fc_rports associated with it. If there is an fc_rport
4526 * associated with the node, then the node is either in
4527 * discovered state or its devloss_timer is pending.
4528 */
4529static int
4530lpfc_fcf_inuse(struct lpfc_hba *phba)
4531{
4532 struct lpfc_vport **vports;
4533 int i, ret = 0;
4534 struct lpfc_nodelist *ndlp;
4535 struct Scsi_Host *shost;
4536
4537 vports = lpfc_create_vport_work_array(phba);
4538
4539 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4540 shost = lpfc_shost_from_vport(vports[i]);
4541 spin_lock_irq(shost->host_lock);
4542 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4543 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4544 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4545 ret = 1;
4546 spin_unlock_irq(shost->host_lock);
4547 goto out;
James Smart891478a2009-11-18 15:40:23 -05004548 } else {
4549 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4550 "2624 RPI %x DID %x flg %x still "
4551 "logged in\n",
4552 ndlp->nlp_rpi, ndlp->nlp_DID,
4553 ndlp->nlp_flag);
4554 if (ndlp->nlp_flag & NLP_RPI_VALID)
4555 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04004556 }
4557 }
4558 spin_unlock_irq(shost->host_lock);
4559 }
4560out:
4561 lpfc_destroy_vport_work_array(phba, vports);
4562 return ret;
4563}
4564
4565/**
4566 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4567 * @phba: Pointer to hba context object.
4568 * @mboxq: Pointer to mailbox object.
4569 *
4570 * This function frees memory associated with the mailbox command.
4571 */
4572static void
4573lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4574{
4575 struct lpfc_vport *vport = mboxq->vport;
4576
4577 if (mboxq->u.mb.mbxStatus) {
4578 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4579 "2555 UNREG_VFI mbxStatus error x%x "
4580 "HBA state x%x\n",
4581 mboxq->u.mb.mbxStatus, vport->port_state);
4582 }
4583 mempool_free(mboxq, phba->mbox_mem_pool);
4584 return;
4585}
4586
4587/**
4588 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4589 * @phba: Pointer to hba context object.
4590 * @mboxq: Pointer to mailbox object.
4591 *
4592 * This function frees memory associated with the mailbox command.
4593 */
4594static void
4595lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4596{
4597 struct lpfc_vport *vport = mboxq->vport;
4598
4599 if (mboxq->u.mb.mbxStatus) {
4600 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4601 "2550 UNREG_FCFI mbxStatus error x%x "
4602 "HBA state x%x\n",
4603 mboxq->u.mb.mbxStatus, vport->port_state);
4604 }
4605 mempool_free(mboxq, phba->mbox_mem_pool);
4606 return;
4607}
4608
4609/**
James Smartecfd03c2010-02-12 14:41:27 -05004610 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04004611 * @phba: Pointer to hba context object.
4612 *
James Smartecfd03c2010-02-12 14:41:27 -05004613 * This function prepare the HBA for unregistering the currently registered
4614 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
4615 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04004616 */
James Smartecfd03c2010-02-12 14:41:27 -05004617int
4618lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04004619{
4620 LPFC_MBOXQ_t *mbox;
James Smart6fb120a2009-05-22 14:52:59 -04004621 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05004622 struct lpfc_nodelist *ndlp;
James Smartecfd03c2010-02-12 14:41:27 -05004623 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04004624
James Smartecfd03c2010-02-12 14:41:27 -05004625 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04004626 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05004627 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04004628
James Smart4d9ab992009-10-02 15:16:39 -04004629 /* At this point, all discovery is aborted */
4630 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04004631
4632 /* Unregister VPIs */
4633 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05004634 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04004635 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05004636 /* Stop FLOGI/FDISC retries */
4637 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
4638 if (ndlp)
4639 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart6fb120a2009-05-22 14:52:59 -04004640 lpfc_mbx_unreg_vpi(vports[i]);
James Smart97957242009-12-21 17:03:15 -05004641 spin_lock_irq(&phba->hbalock);
James Smart891478a2009-11-18 15:40:23 -05004642 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05004643 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart97957242009-12-21 17:03:15 -05004644 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04004645 }
4646 lpfc_destroy_vport_work_array(phba, vports);
4647
James Smart695a8142010-01-26 23:08:03 -05004648 /* Cleanup any outstanding ELS commands */
4649 lpfc_els_flush_all_cmd(phba);
4650
James Smart6fb120a2009-05-22 14:52:59 -04004651 /* Unregister VFI */
4652 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4653 if (!mbox) {
4654 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05004655 "2556 UNREG_VFI mbox allocation failed"
4656 "HBA state x%x\n", phba->pport->port_state);
4657 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04004658 }
4659
James Smart6669f9b2009-10-02 15:16:45 -04004660 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04004661 mbox->vport = phba->pport;
4662 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
4663
4664 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4665 if (rc == MBX_NOT_FINISHED) {
4666 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05004667 "2557 UNREG_VFI issue mbox failed rc x%x "
4668 "HBA state x%x\n",
4669 rc, phba->pport->port_state);
James Smart6fb120a2009-05-22 14:52:59 -04004670 mempool_free(mbox, phba->mbox_mem_pool);
James Smartecfd03c2010-02-12 14:41:27 -05004671 return -EIO;
James Smart6fb120a2009-05-22 14:52:59 -04004672 }
4673
James Smart695a8142010-01-26 23:08:03 -05004674 spin_lock_irq(&phba->hbalock);
4675 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
4676 spin_unlock_irq(&phba->hbalock);
4677
James Smartecfd03c2010-02-12 14:41:27 -05004678 return 0;
4679}
4680
4681/**
4682 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
4683 * @phba: Pointer to hba context object.
4684 *
4685 * This function issues synchronous unregister FCF mailbox command to HBA to
4686 * unregister the currently registered FCF record. The driver does not reset
4687 * the driver FCF usage state flags.
4688 *
4689 * Return 0 if successfully issued, none-zero otherwise.
4690 */
4691int
4692lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
4693{
4694 LPFC_MBOXQ_t *mbox;
4695 int rc;
4696
James Smart6fb120a2009-05-22 14:52:59 -04004697 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4698 if (!mbox) {
4699 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05004700 "2551 UNREG_FCFI mbox allocation failed"
4701 "HBA state x%x\n", phba->pport->port_state);
4702 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04004703 }
James Smart6fb120a2009-05-22 14:52:59 -04004704 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
4705 mbox->vport = phba->pport;
4706 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
4707 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4708
4709 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05004710 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4711 "2552 Unregister FCFI command failed rc x%x "
4712 "HBA state x%x\n",
4713 rc, phba->pport->port_state);
4714 return -EINVAL;
4715 }
4716 return 0;
4717}
4718
4719/**
4720 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
4721 * @phba: Pointer to hba context object.
4722 *
4723 * This function unregisters the currently reigstered FCF. This function
4724 * also tries to find another FCF for discovery by rescan the HBA FCF table.
4725 */
4726void
4727lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
4728{
4729 int rc;
4730
4731 /* Preparation for unregistering fcf */
4732 rc = lpfc_unregister_fcf_prep(phba);
4733 if (rc) {
4734 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4735 "2748 Failed to prepare for unregistering "
4736 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04004737 return;
4738 }
4739
James Smartecfd03c2010-02-12 14:41:27 -05004740 /* Now, unregister FCF record and reset HBA FCF state */
4741 rc = lpfc_sli4_unregister_fcf(phba);
4742 if (rc)
4743 return;
4744 /* Reset HBA FCF states after successful unregister FCF */
4745 phba->fcf.fcf_flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04004746
4747 /*
4748 * If driver is not unloading, check if there is any other
4749 * FCF record that can be used for discovery.
4750 */
4751 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05004752 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04004753 return;
4754
4755 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
4756
4757 if (rc)
4758 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05004759 "2553 lpfc_unregister_unused_fcf failed "
4760 "to read FCF record HBA state x%x\n",
4761 phba->pport->port_state);
4762}
4763
4764/**
4765 * lpfc_unregister_fcf - Unregister the currently registered fcf record
4766 * @phba: Pointer to hba context object.
4767 *
4768 * This function just unregisters the currently reigstered FCF. It does not
4769 * try to find another FCF for discovery.
4770 */
4771void
4772lpfc_unregister_fcf(struct lpfc_hba *phba)
4773{
4774 int rc;
4775
4776 /* Preparation for unregistering fcf */
4777 rc = lpfc_unregister_fcf_prep(phba);
4778 if (rc) {
4779 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4780 "2749 Failed to prepare for unregistering "
4781 "HBA's FCF record: rc=%d\n", rc);
4782 return;
4783 }
4784
4785 /* Now, unregister FCF record and reset HBA FCF state */
4786 rc = lpfc_sli4_unregister_fcf(phba);
4787 if (rc)
4788 return;
4789 /* Set proper HBA FCF states after successful unregister FCF */
4790 spin_lock_irq(&phba->hbalock);
4791 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
4792 spin_unlock_irq(&phba->hbalock);
4793}
4794
4795/**
4796 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
4797 * @phba: Pointer to hba context object.
4798 *
4799 * This function check if there are any connected remote port for the FCF and
4800 * if all the devices are disconnected, this function unregister FCFI.
4801 * This function also tries to use another FCF for discovery.
4802 */
4803void
4804lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
4805{
4806 /*
4807 * If HBA is not running in FIP mode or if HBA does not support
4808 * FCoE or if FCF is not registered, do nothing.
4809 */
4810 spin_lock_irq(&phba->hbalock);
4811 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
4812 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
4813 !(phba->hba_flag & HBA_FIP_SUPPORT)) {
4814 spin_unlock_irq(&phba->hbalock);
4815 return;
4816 }
4817 spin_unlock_irq(&phba->hbalock);
4818
4819 if (lpfc_fcf_inuse(phba))
4820 return;
4821
4822 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04004823}
4824
4825/**
4826 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
4827 * @phba: Pointer to hba context object.
4828 * @buff: Buffer containing the FCF connection table as in the config
4829 * region.
4830 * This function create driver data structure for the FCF connection
4831 * record table read from config region 23.
4832 */
4833static void
4834lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
4835 uint8_t *buff)
4836{
4837 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4838 struct lpfc_fcf_conn_hdr *conn_hdr;
4839 struct lpfc_fcf_conn_rec *conn_rec;
4840 uint32_t record_count;
4841 int i;
4842
4843 /* Free the current connect table */
4844 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004845 &phba->fcf_conn_rec_list, list) {
4846 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04004847 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004848 }
James Smart6fb120a2009-05-22 14:52:59 -04004849
4850 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
4851 record_count = conn_hdr->length * sizeof(uint32_t)/
4852 sizeof(struct lpfc_fcf_conn_rec);
4853
4854 conn_rec = (struct lpfc_fcf_conn_rec *)
4855 (buff + sizeof(struct lpfc_fcf_conn_hdr));
4856
4857 for (i = 0; i < record_count; i++) {
4858 if (!(conn_rec[i].flags & FCFCNCT_VALID))
4859 continue;
4860 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
4861 GFP_KERNEL);
4862 if (!conn_entry) {
4863 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4864 "2566 Failed to allocate connection"
4865 " table entry\n");
4866 return;
4867 }
4868
4869 memcpy(&conn_entry->conn_rec, &conn_rec[i],
4870 sizeof(struct lpfc_fcf_conn_rec));
4871 conn_entry->conn_rec.vlan_tag =
4872 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
4873 conn_entry->conn_rec.flags =
4874 le16_to_cpu(conn_entry->conn_rec.flags);
4875 list_add_tail(&conn_entry->list,
4876 &phba->fcf_conn_rec_list);
4877 }
4878}
4879
4880/**
4881 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
4882 * @phba: Pointer to hba context object.
4883 * @buff: Buffer containing the FCoE parameter data structure.
4884 *
4885 * This function update driver data structure with config
4886 * parameters read from config region 23.
4887 */
4888static void
4889lpfc_read_fcoe_param(struct lpfc_hba *phba,
4890 uint8_t *buff)
4891{
4892 struct lpfc_fip_param_hdr *fcoe_param_hdr;
4893 struct lpfc_fcoe_params *fcoe_param;
4894
4895 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
4896 buff;
4897 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04004898 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04004899
4900 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
4901 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
4902 return;
4903
James Smart6fb120a2009-05-22 14:52:59 -04004904 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
4905 phba->valid_vlan = 1;
4906 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
4907 0xFFF;
4908 }
4909
4910 phba->fc_map[0] = fcoe_param->fc_map[0];
4911 phba->fc_map[1] = fcoe_param->fc_map[1];
4912 phba->fc_map[2] = fcoe_param->fc_map[2];
4913 return;
4914}
4915
4916/**
4917 * lpfc_get_rec_conf23 - Get a record type in config region data.
4918 * @buff: Buffer containing config region 23 data.
4919 * @size: Size of the data buffer.
4920 * @rec_type: Record type to be searched.
4921 *
4922 * This function searches config region data to find the begining
4923 * of the record specified by record_type. If record found, this
4924 * function return pointer to the record else return NULL.
4925 */
4926static uint8_t *
4927lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
4928{
4929 uint32_t offset = 0, rec_length;
4930
4931 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
4932 (size < sizeof(uint32_t)))
4933 return NULL;
4934
4935 rec_length = buff[offset + 1];
4936
4937 /*
4938 * One TLV record has one word header and number of data words
4939 * specified in the rec_length field of the record header.
4940 */
4941 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
4942 <= size) {
4943 if (buff[offset] == rec_type)
4944 return &buff[offset];
4945
4946 if (buff[offset] == LPFC_REGION23_LAST_REC)
4947 return NULL;
4948
4949 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
4950 rec_length = buff[offset + 1];
4951 }
4952 return NULL;
4953}
4954
4955/**
4956 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
4957 * @phba: Pointer to lpfc_hba data structure.
4958 * @buff: Buffer containing config region 23 data.
4959 * @size: Size of the data buffer.
4960 *
4961 * This fuction parse the FCoE config parameters in config region 23 and
4962 * populate driver data structure with the parameters.
4963 */
4964void
4965lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
4966 uint8_t *buff,
4967 uint32_t size)
4968{
4969 uint32_t offset = 0, rec_length;
4970 uint8_t *rec_ptr;
4971
4972 /*
4973 * If data size is less than 2 words signature and version cannot be
4974 * verified.
4975 */
4976 if (size < 2*sizeof(uint32_t))
4977 return;
4978
4979 /* Check the region signature first */
4980 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
4981 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4982 "2567 Config region 23 has bad signature\n");
4983 return;
4984 }
4985
4986 offset += 4;
4987
4988 /* Check the data structure version */
4989 if (buff[offset] != LPFC_REGION23_VERSION) {
4990 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4991 "2568 Config region 23 has bad version\n");
4992 return;
4993 }
4994 offset += 4;
4995
4996 rec_length = buff[offset + 1];
4997
4998 /* Read FCoE param record */
4999 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5000 size - offset, FCOE_PARAM_TYPE);
5001 if (rec_ptr)
5002 lpfc_read_fcoe_param(phba, rec_ptr);
5003
5004 /* Read FCF connection table */
5005 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5006 size - offset, FCOE_CONN_TBL_TYPE);
5007 if (rec_ptr)
5008 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5009
5010}