blob: 1c2737293eb5fdfc9228097350323359490f7d5e [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/kthread.h>
25#include <linux/interrupt.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_device.h>
29#include <scsi/scsi_host.h>
30#include <scsi/scsi_transport_fc.h>
31
James Smartda0436e2009-05-22 14:51:39 -040032#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050033#include "lpfc_hw.h"
James Smartea2151b2008-09-07 11:52:10 -040034#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050035#include "lpfc_disc.h"
36#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_sli4.h"
dea31012005-04-17 16:05:31 -050038#include "lpfc_scsi.h"
39#include "lpfc.h"
40#include "lpfc_logmsg.h"
41#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050042#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050043#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050044
45/* AlpaArray for assignment of scsid for scan-down and bind_method */
46static uint8_t lpfcAlpaArray[] = {
47 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
48 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
49 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
50 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
51 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
52 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
53 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
54 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
55 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
56 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
57 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
58 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
59 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
60};
61
James Smart2e0fef82007-06-17 19:56:36 -050062static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010063static void lpfc_disc_flush_list(struct lpfc_vport *vport);
James Smart32b97932009-07-19 10:01:21 -040064static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
dea31012005-04-17 16:05:31 -050065
James Smartc01f3202006-08-18 17:47:08 -040066void
67lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050068{
James Smartc01f3202006-08-18 17:47:08 -040069 struct lpfc_rport_data *rdata;
70 struct lpfc_nodelist * ndlp;
71 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050072
James Smartc01f3202006-08-18 17:47:08 -040073 rdata = rport->dd_data;
74 ndlp = rdata->pnode;
75
James Smart58da1ff2008-04-07 10:15:56 -040076 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040077 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
78 printk(KERN_ERR "Cannot find remote node"
79 " to terminate I/O Data x%x\n",
80 rport->port_id);
dea31012005-04-17 16:05:31 -050081 return;
82 }
83
James Smarta257bf92009-04-06 18:48:10 -040084 phba = ndlp->phba;
James Smart1a169682006-03-07 15:04:06 -050085
James Smart858c9f62007-06-17 19:56:39 -050086 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
87 "rport terminate: sid:x%x did:x%x flg:x%x",
88 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
89
James Smartc01f3202006-08-18 17:47:08 -040090 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040091 lpfc_sli_abort_iocb(ndlp->vport,
92 &phba->sli.ring[phba->sli.fcp_ring],
93 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040094 }
James Smartc01f3202006-08-18 17:47:08 -040095}
96
97/*
98 * This function will be called when dev_loss_tmo fire.
99 */
100void
101lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
102{
103 struct lpfc_rport_data *rdata;
104 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500105 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500106 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500107 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400108 int put_node;
109 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400110
111 rdata = rport->dd_data;
112 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400113 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400114 return;
James Smartc01f3202006-08-18 17:47:08 -0400115
James Smart858c9f62007-06-17 19:56:39 -0500116 vport = ndlp->vport;
117 phba = vport->phba;
118
119 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
120 "rport devlosscb: sid:x%x did:x%x flg:x%x",
121 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
122
James Smarta8adb832007-10-27 13:37:53 -0400123 /* Don't defer this if we are in the process of deleting the vport
124 * or unloading the driver. The unload will cleanup the node
125 * appropriately we just need to cleanup the ndlp rport info here.
126 */
127 if (vport->load_flag & FC_UNLOADING) {
128 put_node = rdata->pnode != NULL;
129 put_rport = ndlp->rport != NULL;
130 rdata->pnode = NULL;
131 ndlp->rport = NULL;
132 if (put_node)
133 lpfc_nlp_put(ndlp);
134 if (put_rport)
135 put_device(&rport->dev);
136 return;
137 }
138
139 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
140 return;
141
James Smart858c9f62007-06-17 19:56:39 -0500142 evtp = &ndlp->dev_loss_evt;
143
144 if (!list_empty(&evtp->evt_listp))
145 return;
146
147 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500148 /* We need to hold the node by incrementing the reference
149 * count until this queued work is done
150 */
151 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart5e9d9b82008-06-14 22:52:53 -0400152 if (evtp->evt_arg1) {
153 evtp->evt = LPFC_EVT_DEV_LOSS;
154 list_add_tail(&evtp->evt_listp, &phba->work_list);
155 lpfc_worker_wake_up(phba);
156 }
James Smart858c9f62007-06-17 19:56:39 -0500157 spin_unlock_irq(&phba->hbalock);
158
James Smart858c9f62007-06-17 19:56:39 -0500159 return;
160}
161
162/*
163 * This function is called from the worker thread when dev_loss_tmo
164 * expire.
165 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100166static void
James Smart858c9f62007-06-17 19:56:39 -0500167lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
168{
169 struct lpfc_rport_data *rdata;
170 struct fc_rport *rport;
171 struct lpfc_vport *vport;
172 struct lpfc_hba *phba;
173 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400174 int put_node;
175 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500176 int warn_on = 0;
177
178 rport = ndlp->rport;
179
180 if (!rport)
181 return;
182
183 rdata = rport->dd_data;
184 name = (uint8_t *) &ndlp->nlp_portname;
185 vport = ndlp->vport;
186 phba = vport->phba;
187
188 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
189 "rport devlosstmo:did:x%x type:x%x id:x%x",
190 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
191
James Smarta8adb832007-10-27 13:37:53 -0400192 /* Don't defer this if we are in the process of deleting the vport
193 * or unloading the driver. The unload will cleanup the node
194 * appropriately we just need to cleanup the ndlp rport info here.
195 */
196 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500197 if (ndlp->nlp_sid != NLP_NO_SID) {
198 /* flush the target */
199 lpfc_sli_abort_iocb(vport,
200 &phba->sli.ring[phba->sli.fcp_ring],
201 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
202 }
James Smarta8adb832007-10-27 13:37:53 -0400203 put_node = rdata->pnode != NULL;
204 put_rport = ndlp->rport != NULL;
205 rdata->pnode = NULL;
206 ndlp->rport = NULL;
207 if (put_node)
208 lpfc_nlp_put(ndlp);
209 if (put_rport)
210 put_device(&rport->dev);
211 return;
212 }
213
James Smartd7c255b2008-08-24 21:50:00 -0400214 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
215 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
216 "0284 Devloss timeout Ignored on "
217 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
218 "NPort x%x\n",
219 *name, *(name+1), *(name+2), *(name+3),
220 *(name+4), *(name+5), *(name+6), *(name+7),
221 ndlp->nlp_DID);
James Smart858c9f62007-06-17 19:56:39 -0500222 return;
James Smartd7c255b2008-08-24 21:50:00 -0400223 }
James Smart858c9f62007-06-17 19:56:39 -0500224
James Smart92d7f7b2007-06-17 19:56:38 -0500225 if (ndlp->nlp_type & NLP_FABRIC) {
226 /* We will clean up these Nodes in linkup */
227 put_node = rdata->pnode != NULL;
228 put_rport = ndlp->rport != NULL;
229 rdata->pnode = NULL;
230 ndlp->rport = NULL;
231 if (put_node)
232 lpfc_nlp_put(ndlp);
233 if (put_rport)
234 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400235 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500236 }
James Smart82085712007-04-25 09:52:41 -0400237
dea31012005-04-17 16:05:31 -0500238 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400239 warn_on = 1;
dea31012005-04-17 16:05:31 -0500240 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400241 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
242 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500243 }
James Smartc01f3202006-08-18 17:47:08 -0400244
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400245 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400246 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
247 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400248 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
249 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400250 *name, *(name+1), *(name+2), *(name+3),
251 *(name+4), *(name+5), *(name+6), *(name+7),
252 ndlp->nlp_DID, ndlp->nlp_flag,
253 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400254 } else {
James Smarte8b62012007-08-02 11:10:09 -0400255 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
256 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400257 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
258 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400259 *name, *(name+1), *(name+2), *(name+3),
260 *(name+4), *(name+5), *(name+6), *(name+7),
261 ndlp->nlp_DID, ndlp->nlp_flag,
262 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400263 }
264
James Smart87af33f2007-10-27 13:37:43 -0400265 put_node = rdata->pnode != NULL;
266 put_rport = ndlp->rport != NULL;
267 rdata->pnode = NULL;
268 ndlp->rport = NULL;
269 if (put_node)
270 lpfc_nlp_put(ndlp);
271 if (put_rport)
272 put_device(&rport->dev);
273
James Smart2e0fef82007-06-17 19:56:36 -0500274 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400275 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400276 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smarte47c9092008-02-08 18:49:26 -0500277 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
James Smart2e0fef82007-06-17 19:56:36 -0500278 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400279
280 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500281}
James Smartc01f3202006-08-18 17:47:08 -0400282
James Smartea2151b2008-09-07 11:52:10 -0400283/**
James Smart3621a712009-04-06 18:47:14 -0400284 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400285 * @phba: Pointer to hba context object.
286 *
287 * This function is called from the functions which need to post
288 * events from interrupt context. This function allocates data
289 * structure required for posting event. It also keeps track of
290 * number of events pending and prevent event storm when there are
291 * too many events.
292 **/
293struct lpfc_fast_path_event *
294lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
295 struct lpfc_fast_path_event *ret;
296
297 /* If there are lot of fast event do not exhaust memory due to this */
298 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
299 return NULL;
300
301 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
302 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400303 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400304 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400305 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
306 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
307 }
James Smartea2151b2008-09-07 11:52:10 -0400308 return ret;
309}
310
311/**
James Smart3621a712009-04-06 18:47:14 -0400312 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400313 * @phba: Pointer to hba context object.
314 * @evt: Event object which need to be freed.
315 *
316 * This function frees the data structure required for posting
317 * events.
318 **/
319void
320lpfc_free_fast_evt(struct lpfc_hba *phba,
321 struct lpfc_fast_path_event *evt) {
322
323 atomic_dec(&phba->fast_event_count);
324 kfree(evt);
325}
326
327/**
James Smart3621a712009-04-06 18:47:14 -0400328 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400329 * @phba: Pointer to hba context object.
330 * @evtp: Event data structure.
331 *
332 * This function is called from worker thread, when the interrupt
333 * context need to post an event. This function posts the event
334 * to fc transport netlink interface.
335 **/
336static void
337lpfc_send_fastpath_evt(struct lpfc_hba *phba,
338 struct lpfc_work_evt *evtp)
339{
340 unsigned long evt_category, evt_sub_category;
341 struct lpfc_fast_path_event *fast_evt_data;
342 char *evt_data;
343 uint32_t evt_data_size;
344 struct Scsi_Host *shost;
345
346 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
347 work_evt);
348
349 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
350 evt_sub_category = (unsigned long) fast_evt_data->un.
351 fabric_evt.subcategory;
352 shost = lpfc_shost_from_vport(fast_evt_data->vport);
353 if (evt_category == FC_REG_FABRIC_EVENT) {
354 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
355 evt_data = (char *) &fast_evt_data->un.read_check_error;
356 evt_data_size = sizeof(fast_evt_data->un.
357 read_check_error);
358 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500359 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400360 evt_data = (char *) &fast_evt_data->un.fabric_evt;
361 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
362 } else {
363 lpfc_free_fast_evt(phba, fast_evt_data);
364 return;
365 }
366 } else if (evt_category == FC_REG_SCSI_EVENT) {
367 switch (evt_sub_category) {
368 case LPFC_EVENT_QFULL:
369 case LPFC_EVENT_DEVBSY:
370 evt_data = (char *) &fast_evt_data->un.scsi_evt;
371 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
372 break;
373 case LPFC_EVENT_CHECK_COND:
374 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
375 evt_data_size = sizeof(fast_evt_data->un.
376 check_cond_evt);
377 break;
378 case LPFC_EVENT_VARQUEDEPTH:
379 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
380 evt_data_size = sizeof(fast_evt_data->un.
381 queue_depth_evt);
382 break;
383 default:
384 lpfc_free_fast_evt(phba, fast_evt_data);
385 return;
386 }
387 } else {
388 lpfc_free_fast_evt(phba, fast_evt_data);
389 return;
390 }
391
392 fc_host_post_vendor_event(shost,
393 fc_get_event_number(),
394 evt_data_size,
395 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500396 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400397
398 lpfc_free_fast_evt(phba, fast_evt_data);
399 return;
400}
401
dea31012005-04-17 16:05:31 -0500402static void
James Smart2e0fef82007-06-17 19:56:36 -0500403lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500404{
405 struct lpfc_work_evt *evtp = NULL;
406 struct lpfc_nodelist *ndlp;
407 int free_evt;
408
James Smart2e0fef82007-06-17 19:56:36 -0500409 spin_lock_irq(&phba->hbalock);
410 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500411 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
412 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500413 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500414 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500415 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500416 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500417 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500418 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500419 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500420 /* decrement the node reference count held
421 * for this queued work
422 */
423 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500424 break;
James Smart858c9f62007-06-17 19:56:39 -0500425 case LPFC_EVT_DEV_LOSS:
426 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500427 lpfc_dev_loss_tmo_handler(ndlp);
428 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500429 /* decrement the node reference count held for
430 * this queued work
431 */
James Smart858c9f62007-06-17 19:56:39 -0500432 lpfc_nlp_put(ndlp);
433 break;
dea31012005-04-17 16:05:31 -0500434 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500435 if (phba->link_state < LPFC_LINK_DOWN)
436 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500437 else
James Smart2e0fef82007-06-17 19:56:36 -0500438 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500439 complete((struct completion *)(evtp->evt_arg2));
440 break;
James Smart46fa3112007-04-25 09:51:45 -0400441 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500442 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400443 lpfc_offline_prep(phba);
444 *(int *)(evtp->evt_arg1) = 0;
445 complete((struct completion *)(evtp->evt_arg2));
446 break;
447 case LPFC_EVT_OFFLINE:
448 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500449 lpfc_sli_brdrestart(phba);
450 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400451 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
452 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500453 complete((struct completion *)(evtp->evt_arg2));
454 break;
455 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400456 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500457 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500458 lpfc_sli_brdreset(phba);
459 lpfc_hba_down_post(phba);
460 *(int *)(evtp->evt_arg1) =
461 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400462 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500463 complete((struct completion *)(evtp->evt_arg2));
464 break;
465 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400466 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500467 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500468 = (phba->pport->stopped)
469 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400470 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500471 complete((struct completion *)(evtp->evt_arg2));
472 break;
James Smartea2151b2008-09-07 11:52:10 -0400473 case LPFC_EVT_FASTPATH_MGMT_EVT:
474 lpfc_send_fastpath_evt(phba, evtp);
475 free_evt = 0;
476 break;
dea31012005-04-17 16:05:31 -0500477 }
478 if (free_evt)
479 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500480 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500481 }
James Smart2e0fef82007-06-17 19:56:36 -0500482 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500483
484}
485
James Smart311464e2007-08-02 11:10:37 -0400486static void
James Smart2e0fef82007-06-17 19:56:36 -0500487lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500488{
489 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500490 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400491 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400492 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400493 int i;
dea31012005-04-17 16:05:31 -0500494
James Smart2e0fef82007-06-17 19:56:36 -0500495 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500496 ha_copy = phba->work_ha;
497 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500498 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500499
James Smartda0436e2009-05-22 14:51:39 -0400500 /* First, try to post the next mailbox command to SLI4 device */
501 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
502 lpfc_sli4_post_async_mbox(phba);
503
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500504 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400505 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500506 lpfc_handle_eratt(phba);
507
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500508 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500509 lpfc_sli_handle_mb_event(phba);
510
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500511 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500512 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400513
James Smartda0436e2009-05-22 14:51:39 -0400514 /* Process SLI4 events */
515 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
516 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
517 lpfc_sli4_fcp_xri_abort_event_proc(phba);
518 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
519 lpfc_sli4_els_xri_abort_event_proc(phba);
520 if (phba->hba_flag & ASYNC_EVENT)
521 lpfc_sli4_async_event_proc(phba);
522 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
523 spin_lock_irq(&phba->hbalock);
524 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
525 spin_unlock_irq(&phba->hbalock);
526 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
527 }
James Smartda0436e2009-05-22 14:51:39 -0400528 }
529
James Smart549e55c2007-08-02 11:09:51 -0400530 vports = lpfc_create_vport_work_array(phba);
531 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400532 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400533 /*
534 * We could have no vports in array if unloading, so if
535 * this happens then just use the pport
536 */
537 if (vports[i] == NULL && i == 0)
538 vport = phba->pport;
539 else
540 vport = vports[i];
541 if (vport == NULL)
542 break;
James Smart58da1ff2008-04-07 10:15:56 -0400543 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400544 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400545 vport->work_port_events &= ~work_port_events;
546 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400547 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400548 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400549 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400550 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400551 if (work_port_events & WORKER_HB_TMO)
552 lpfc_hb_timeout_handler(phba);
553 if (work_port_events & WORKER_MBOX_TMO)
554 lpfc_mbox_timeout_handler(phba);
555 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
556 lpfc_unblock_fabric_iocbs(phba);
557 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400558 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400559 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
560 lpfc_ramp_down_queue_handler(phba);
561 if (work_port_events & WORKER_RAMP_UP_QUEUE)
562 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500563 }
James Smart09372822008-01-11 01:52:54 -0500564 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500565
James Smart858c9f62007-06-17 19:56:39 -0500566 pring = &phba->sli.ring[LPFC_ELS_RING];
567 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
568 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400569 if ((status & HA_RXMASK) ||
570 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400571 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400572 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500573 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400574 /* Set the lpfc data pending flag */
575 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500576 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400577 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500578 lpfc_sli_handle_slow_ring_event(phba, pring,
579 (status &
580 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500581 }
582 /*
583 * Turn on Ring interrupts
584 */
James Smart3772a992009-05-22 14:50:54 -0400585 if (phba->sli_rev <= LPFC_SLI_REV3) {
586 spin_lock_irq(&phba->hbalock);
587 control = readl(phba->HCregaddr);
588 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
589 lpfc_debugfs_slow_ring_trc(phba,
590 "WRK Enable ring: cntl:x%x hacopy:x%x",
591 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400592
James Smart3772a992009-05-22 14:50:54 -0400593 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
594 writel(control, phba->HCregaddr);
595 readl(phba->HCregaddr); /* flush */
596 } else {
597 lpfc_debugfs_slow_ring_trc(phba,
598 "WRK Ring ok: cntl:x%x hacopy:x%x",
599 control, ha_copy, 0);
600 }
601 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500602 }
603 }
James Smart2e0fef82007-06-17 19:56:36 -0500604 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500605}
606
dea31012005-04-17 16:05:31 -0500607int
608lpfc_do_work(void *p)
609{
610 struct lpfc_hba *phba = p;
611 int rc;
dea31012005-04-17 16:05:31 -0500612
613 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400614 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500615
James Smart3a55b532008-12-04 22:38:54 -0500616 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400617 /* wait and check worker queue activities */
618 rc = wait_event_interruptible(phba->work_waitq,
619 (test_and_clear_bit(LPFC_DATA_READY,
620 &phba->data_flags)
621 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500622 /* Signal wakeup shall terminate the worker thread */
623 if (rc) {
624 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
625 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500626 break;
James Smart3a55b532008-12-04 22:38:54 -0500627 }
dea31012005-04-17 16:05:31 -0500628
James Smart5e9d9b82008-06-14 22:52:53 -0400629 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500630 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500631 }
James Smart3a55b532008-12-04 22:38:54 -0500632 phba->worker_thread = NULL;
633 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
634 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500635 return 0;
636}
637
638/*
639 * This is only called to handle FC worker events. Since this a rare
640 * occurance, we allocate a struct lpfc_work_evt structure here instead of
641 * embedding it in the IOCB.
642 */
643int
James Smart2e0fef82007-06-17 19:56:36 -0500644lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500645 uint32_t evt)
646{
647 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500648 unsigned long flags;
dea31012005-04-17 16:05:31 -0500649
650 /*
651 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
652 * be queued to worker thread for processing
653 */
James Smart92d7f7b2007-06-17 19:56:38 -0500654 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500655 if (!evtp)
656 return 0;
657
658 evtp->evt_arg1 = arg1;
659 evtp->evt_arg2 = arg2;
660 evtp->evt = evt;
661
James Smarted957682007-06-17 19:56:37 -0500662 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400663 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500664 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500665
James Smart5e9d9b82008-06-14 22:52:53 -0400666 lpfc_worker_wake_up(phba);
667
dea31012005-04-17 16:05:31 -0500668 return 1;
669}
670
James Smart92d7f7b2007-06-17 19:56:38 -0500671void
672lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
673{
James Smart09372822008-01-11 01:52:54 -0500674 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500675 struct lpfc_hba *phba = vport->phba;
676 struct lpfc_nodelist *ndlp, *next_ndlp;
677 int rc;
678
679 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500680 if (!NLP_CHK_NODE_ACT(ndlp))
681 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500682 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
683 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400684 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
685 ((vport->port_type == LPFC_NPIV_PORT) &&
686 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500687 lpfc_unreg_rpi(vport, ndlp);
688
689 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400690 if ((phba->sli_rev < LPFC_SLI_REV4) &&
691 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500692 continue;
693 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
694 remove
695 ? NLP_EVT_DEVICE_RM
696 : NLP_EVT_DEVICE_RECOVERY);
697 }
698 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
699 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500700 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500701 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500702 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500703 }
704}
705
James Smart87af33f2007-10-27 13:37:43 -0400706void
James Smart98c9ea52007-10-27 13:37:33 -0400707lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500708{
James Smart45ed1192009-10-02 15:17:02 -0400709 /* Cleanup any outstanding received buffers */
710 lpfc_cleanup_rcv_buffers(vport);
711
James Smart92d7f7b2007-06-17 19:56:38 -0500712 /* Cleanup any outstanding RSCN activity */
713 lpfc_els_flush_rscn(vport);
714
715 /* Cleanup any outstanding ELS commands */
716 lpfc_els_flush_cmd(vport);
717
718 lpfc_cleanup_rpis(vport, 0);
719
James Smart92d7f7b2007-06-17 19:56:38 -0500720 /* Turn off discovery timer if its running */
721 lpfc_can_disctmo(vport);
722}
723
James Smart3772a992009-05-22 14:50:54 -0400724void
James Smart98c9ea52007-10-27 13:37:33 -0400725lpfc_linkdown_port(struct lpfc_vport *vport)
726{
727 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
728
729 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
730
731 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
732 "Link Down: state:x%x rtry:x%x flg:x%x",
733 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
734
735 lpfc_port_link_failure(vport);
736
737}
738
dea31012005-04-17 16:05:31 -0500739int
James Smart685f0bf2007-04-25 09:53:08 -0400740lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500741{
James Smart2e0fef82007-06-17 19:56:36 -0500742 struct lpfc_vport *vport = phba->pport;
743 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400744 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400745 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400746 int i;
dea31012005-04-17 16:05:31 -0500747
James Smart3163f722008-02-08 18:50:25 -0500748 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500749 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500750
751 /* Block all SCSI stack I/Os */
752 lpfc_scsi_dev_block(phba);
753
James Smart2e0fef82007-06-17 19:56:36 -0500754 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -0400755 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_DISCOVERED);
James Smart92d7f7b2007-06-17 19:56:38 -0500756 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500757 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500758 phba->pport->fc_flag &= ~FC_LBIT;
759 }
James Smart2e0fef82007-06-17 19:56:36 -0500760 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400761 vports = lpfc_create_vport_work_array(phba);
762 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400763 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400764 /* Issue a LINK DOWN event to all nodes */
765 lpfc_linkdown_port(vports[i]);
766 }
James Smart09372822008-01-11 01:52:54 -0500767 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500768 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500769 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
770 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500771 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500772 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500773 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400774 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500775 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500776 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500777 }
778 }
779
dea31012005-04-17 16:05:31 -0500780 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500781 if (phba->pport->fc_flag & FC_PT2PT) {
782 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500783 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
784 if (mb) {
dea31012005-04-17 16:05:31 -0500785 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500786 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500787 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400788 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500789 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500790 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500791 }
792 }
James Smart2e0fef82007-06-17 19:56:36 -0500793 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500794 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500795 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500796 }
James Smart2e0fef82007-06-17 19:56:36 -0500797
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500798 return 0;
dea31012005-04-17 16:05:31 -0500799}
800
James Smart92d7f7b2007-06-17 19:56:38 -0500801static void
802lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500803{
James Smart92d7f7b2007-06-17 19:56:38 -0500804 struct lpfc_nodelist *ndlp;
805
806 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500807 if (!NLP_CHK_NODE_ACT(ndlp))
808 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500809 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
810 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500811 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500812 /* On Linkup its safe to clean up the ndlp
813 * from Fabric connections.
814 */
James Smart92d7f7b2007-06-17 19:56:38 -0500815 if (ndlp->nlp_DID != Fabric_DID)
816 lpfc_unreg_rpi(vport, ndlp);
817 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
818 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500819 /* Fail outstanding IO now since device is
820 * marked for PLOGI.
821 */
James Smart92d7f7b2007-06-17 19:56:38 -0500822 lpfc_unreg_rpi(vport, ndlp);
823 }
824 }
825}
826
827static void
828lpfc_linkup_port(struct lpfc_vport *vport)
829{
830 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500831 struct lpfc_hba *phba = vport->phba;
832
833 if ((vport->load_flag & FC_UNLOADING) != 0)
834 return;
835
James Smart858c9f62007-06-17 19:56:39 -0500836 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
837 "Link Up: top:x%x speed:x%x flg:x%x",
838 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
839
James Smart92d7f7b2007-06-17 19:56:38 -0500840 /* If NPIV is not enabled, only bring the physical port up */
841 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
842 (vport != phba->pport))
843 return;
dea31012005-04-17 16:05:31 -0500844
James Smart2e0fef82007-06-17 19:56:36 -0500845 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400846
James Smart2e0fef82007-06-17 19:56:36 -0500847 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500848 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
849 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
850 vport->fc_flag |= FC_NDISC_ACTIVE;
851 vport->fc_ns_retry = 0;
852 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500853
James Smart92d7f7b2007-06-17 19:56:38 -0500854 if (vport->fc_flag & FC_LBIT)
855 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500856
James Smart92d7f7b2007-06-17 19:56:38 -0500857}
858
859static int
860lpfc_linkup(struct lpfc_hba *phba)
861{
James Smart549e55c2007-08-02 11:09:51 -0400862 struct lpfc_vport **vports;
863 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500864
865 phba->link_state = LPFC_LINK_UP;
866
867 /* Unblock fabric iocbs if they are blocked */
868 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
869 del_timer_sync(&phba->fabric_block_timer);
870
James Smart549e55c2007-08-02 11:09:51 -0400871 vports = lpfc_create_vport_work_array(phba);
872 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400873 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400874 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500875 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400876 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
877 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500878 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500879
880 return 0;
881}
882
883/*
884 * This routine handles processing a CLEAR_LA mailbox
885 * command upon completion. It is setup in the LPFC_MBOXQ
886 * as the completion routine when the command is
887 * handed off to the SLI layer.
888 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100889static void
James Smart2e0fef82007-06-17 19:56:36 -0500890lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500891{
James Smart2e0fef82007-06-17 19:56:36 -0500892 struct lpfc_vport *vport = pmb->vport;
893 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
894 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400895 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500896 uint32_t control;
897
dea31012005-04-17 16:05:31 -0500898 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500899 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500900 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
901 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
902
903 /* Check for error */
904 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500905 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400906 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
907 "0320 CLEAR_LA mbxStatus error x%x hba "
908 "state x%x\n",
909 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500910 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500911 goto out;
912 }
913
James Smart92d7f7b2007-06-17 19:56:38 -0500914 if (vport->port_type == LPFC_PHYSICAL_PORT)
915 phba->link_state = LPFC_HBA_READY;
916
917 spin_lock_irq(&phba->hbalock);
918 psli->sli_flag |= LPFC_PROCESS_LA;
919 control = readl(phba->HCregaddr);
920 control |= HC_LAINT_ENA;
921 writel(control, phba->HCregaddr);
922 readl(phba->HCregaddr); /* flush */
923 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500924 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500925 return;
dea31012005-04-17 16:05:31 -0500926
dea31012005-04-17 16:05:31 -0500927out:
928 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400929 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
930 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500931 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500932
James Smart2e0fef82007-06-17 19:56:36 -0500933 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400934 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500935 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500936
James Smart2e0fef82007-06-17 19:56:36 -0500937 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500938
939 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500940
941 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500942 psli->sli_flag |= LPFC_PROCESS_LA;
943 control = readl(phba->HCregaddr);
944 control |= HC_LAINT_ENA;
945 writel(control, phba->HCregaddr);
946 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500947 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500948
949 return;
950}
951
James Smart2e0fef82007-06-17 19:56:36 -0500952
dea31012005-04-17 16:05:31 -0500953static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500954lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500955{
James Smart2e0fef82007-06-17 19:56:36 -0500956 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500957
James Smart04c68492009-05-22 14:52:52 -0400958 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500959 goto out;
dea31012005-04-17 16:05:31 -0500960
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500961 mempool_free(pmb, phba->mbox_mem_pool);
962
963 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500964 vport->fc_flag & FC_PUBLIC_LOOP &&
965 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500966 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500967 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500968 * LPFC_LOCAL_CFG_LINK while waiting for FAN
969 */
James Smart2e0fef82007-06-17 19:56:36 -0500970 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500971 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500972 }
dea31012005-04-17 16:05:31 -0500973
James Smart2e0fef82007-06-17 19:56:36 -0500974 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500975 * LPFC_FLOGI while waiting for FLOGI cmpl
976 */
James Smart92d7f7b2007-06-17 19:56:38 -0500977 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500978 lpfc_initial_flogi(vport);
979 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500980 return;
dea31012005-04-17 16:05:31 -0500981
982out:
James Smarte8b62012007-08-02 11:10:09 -0400983 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
984 "0306 CONFIG_LINK mbxStatus error x%x "
985 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400986 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500987 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500988
989 lpfc_linkdown(phba);
990
James Smarte8b62012007-08-02 11:10:09 -0400991 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
992 "0200 CONFIG_LINK bad hba state x%x\n",
993 vport->port_state);
dea31012005-04-17 16:05:31 -0500994
James Smart92d7f7b2007-06-17 19:56:38 -0500995 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500996 return;
997}
998
999static void
James Smart6fb120a2009-05-22 14:52:59 -04001000lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1001{
1002 struct lpfc_vport *vport = mboxq->vport;
1003 unsigned long flags;
1004
1005 if (mboxq->u.mb.mbxStatus) {
1006 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1007 "2017 REG_FCFI mbxStatus error x%x "
1008 "HBA state x%x\n",
1009 mboxq->u.mb.mbxStatus, vport->port_state);
1010 mempool_free(mboxq, phba->mbox_mem_pool);
1011 return;
1012 }
1013
1014 /* Start FCoE discovery by sending a FLOGI. */
1015 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1016 /* Set the FCFI registered flag */
1017 spin_lock_irqsave(&phba->hbalock, flags);
1018 phba->fcf.fcf_flag |= FCF_REGISTERED;
1019 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart32b97932009-07-19 10:01:21 -04001020 /* If there is a pending FCoE event, restart FCF table scan. */
1021 if (lpfc_check_pending_fcoe_event(phba, 1)) {
1022 mempool_free(mboxq, phba->mbox_mem_pool);
1023 return;
1024 }
James Smart1c6f4ef52009-11-18 15:40:49 -05001025 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4d9ab992009-10-02 15:16:39 -04001026 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1027 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001028 spin_unlock_irqrestore(&phba->hbalock, flags);
1029 if (vport->port_state != LPFC_FLOGI)
James Smart6fb120a2009-05-22 14:52:59 -04001030 lpfc_initial_flogi(vport);
James Smart6fb120a2009-05-22 14:52:59 -04001031
1032 mempool_free(mboxq, phba->mbox_mem_pool);
1033 return;
1034}
1035
1036/**
1037 * lpfc_fab_name_match - Check if the fcf fabric name match.
1038 * @fab_name: pointer to fabric name.
1039 * @new_fcf_record: pointer to fcf record.
1040 *
1041 * This routine compare the fcf record's fabric name with provided
1042 * fabric name. If the fabric name are identical this function
1043 * returns 1 else return 0.
1044 **/
1045static uint32_t
1046lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1047{
1048 if ((fab_name[0] ==
1049 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record)) &&
1050 (fab_name[1] ==
1051 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record)) &&
1052 (fab_name[2] ==
1053 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record)) &&
1054 (fab_name[3] ==
1055 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record)) &&
1056 (fab_name[4] ==
1057 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record)) &&
1058 (fab_name[5] ==
1059 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record)) &&
1060 (fab_name[6] ==
1061 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record)) &&
1062 (fab_name[7] ==
1063 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record)))
1064 return 1;
1065 else
1066 return 0;
1067}
1068
1069/**
James Smart8fa38512009-07-19 10:01:03 -04001070 * lpfc_sw_name_match - Check if the fcf switch name match.
1071 * @fab_name: pointer to fabric name.
1072 * @new_fcf_record: pointer to fcf record.
1073 *
1074 * This routine compare the fcf record's switch name with provided
1075 * switch name. If the switch name are identical this function
1076 * returns 1 else return 0.
1077 **/
1078static uint32_t
1079lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1080{
1081 if ((sw_name[0] ==
1082 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record)) &&
1083 (sw_name[1] ==
1084 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record)) &&
1085 (sw_name[2] ==
1086 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record)) &&
1087 (sw_name[3] ==
1088 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record)) &&
1089 (sw_name[4] ==
1090 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record)) &&
1091 (sw_name[5] ==
1092 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record)) &&
1093 (sw_name[6] ==
1094 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record)) &&
1095 (sw_name[7] ==
1096 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record)))
1097 return 1;
1098 else
1099 return 0;
1100}
1101
1102/**
James Smart6fb120a2009-05-22 14:52:59 -04001103 * lpfc_mac_addr_match - Check if the fcf mac address match.
1104 * @phba: pointer to lpfc hba data structure.
1105 * @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
1112lpfc_mac_addr_match(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1113{
1114 if ((phba->fcf.mac_addr[0] ==
1115 bf_get(lpfc_fcf_record_mac_0, new_fcf_record)) &&
1116 (phba->fcf.mac_addr[1] ==
1117 bf_get(lpfc_fcf_record_mac_1, new_fcf_record)) &&
1118 (phba->fcf.mac_addr[2] ==
1119 bf_get(lpfc_fcf_record_mac_2, new_fcf_record)) &&
1120 (phba->fcf.mac_addr[3] ==
1121 bf_get(lpfc_fcf_record_mac_3, new_fcf_record)) &&
1122 (phba->fcf.mac_addr[4] ==
1123 bf_get(lpfc_fcf_record_mac_4, new_fcf_record)) &&
1124 (phba->fcf.mac_addr[5] ==
1125 bf_get(lpfc_fcf_record_mac_5, new_fcf_record)))
1126 return 1;
1127 else
1128 return 0;
1129}
1130
1131/**
1132 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1133 * @phba: pointer to lpfc hba data structure.
1134 * @new_fcf_record: pointer to fcf record.
1135 *
1136 * This routine copies the FCF information from the FCF
1137 * record to lpfc_hba data structure.
1138 **/
1139static void
1140lpfc_copy_fcf_record(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1141{
1142 phba->fcf.fabric_name[0] =
1143 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1144 phba->fcf.fabric_name[1] =
1145 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1146 phba->fcf.fabric_name[2] =
1147 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1148 phba->fcf.fabric_name[3] =
1149 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1150 phba->fcf.fabric_name[4] =
1151 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1152 phba->fcf.fabric_name[5] =
1153 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1154 phba->fcf.fabric_name[6] =
1155 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1156 phba->fcf.fabric_name[7] =
1157 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1158 phba->fcf.mac_addr[0] =
1159 bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1160 phba->fcf.mac_addr[1] =
1161 bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1162 phba->fcf.mac_addr[2] =
1163 bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1164 phba->fcf.mac_addr[3] =
1165 bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1166 phba->fcf.mac_addr[4] =
1167 bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1168 phba->fcf.mac_addr[5] =
1169 bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1170 phba->fcf.fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1171 phba->fcf.priority = new_fcf_record->fip_priority;
James Smart8fa38512009-07-19 10:01:03 -04001172 phba->fcf.switch_name[0] =
1173 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1174 phba->fcf.switch_name[1] =
1175 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1176 phba->fcf.switch_name[2] =
1177 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1178 phba->fcf.switch_name[3] =
1179 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1180 phba->fcf.switch_name[4] =
1181 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1182 phba->fcf.switch_name[5] =
1183 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1184 phba->fcf.switch_name[6] =
1185 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1186 phba->fcf.switch_name[7] =
1187 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001188}
1189
1190/**
1191 * lpfc_register_fcf - Register the FCF with hba.
1192 * @phba: pointer to lpfc hba data structure.
1193 *
1194 * This routine issues a register fcfi mailbox command to register
1195 * the fcf with HBA.
1196 **/
1197static void
1198lpfc_register_fcf(struct lpfc_hba *phba)
1199{
1200 LPFC_MBOXQ_t *fcf_mbxq;
1201 int rc;
1202 unsigned long flags;
1203
1204 spin_lock_irqsave(&phba->hbalock, flags);
1205
1206 /* If the FCF is not availabe do nothing. */
1207 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smart4d9ab992009-10-02 15:16:39 -04001208 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001209 spin_unlock_irqrestore(&phba->hbalock, flags);
1210 return;
1211 }
1212
1213 /* The FCF is already registered, start discovery */
1214 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1215 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
James Smart32b97932009-07-19 10:01:21 -04001216 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart6fb120a2009-05-22 14:52:59 -04001217 spin_unlock_irqrestore(&phba->hbalock, flags);
1218 if (phba->pport->port_state != LPFC_FLOGI)
1219 lpfc_initial_flogi(phba->pport);
1220 return;
1221 }
1222 spin_unlock_irqrestore(&phba->hbalock, flags);
1223
1224 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1225 GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001226 if (!fcf_mbxq) {
1227 spin_lock_irqsave(&phba->hbalock, flags);
1228 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1229 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001230 return;
James Smart4d9ab992009-10-02 15:16:39 -04001231 }
James Smart6fb120a2009-05-22 14:52:59 -04001232
1233 lpfc_reg_fcfi(phba, fcf_mbxq);
1234 fcf_mbxq->vport = phba->pport;
1235 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1236 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001237 if (rc == MBX_NOT_FINISHED) {
1238 spin_lock_irqsave(&phba->hbalock, flags);
1239 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1240 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6fb120a2009-05-22 14:52:59 -04001241 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001242 }
James Smart6fb120a2009-05-22 14:52:59 -04001243
1244 return;
1245}
1246
1247/**
1248 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1249 * @phba: pointer to lpfc hba data structure.
1250 * @new_fcf_record: pointer to fcf record.
1251 * @boot_flag: Indicates if this record used by boot bios.
1252 * @addr_mode: The address mode to be used by this FCF
1253 *
1254 * This routine compare the fcf record with connect list obtained from the
1255 * config region to decide if this FCF can be used for SAN discovery. It returns
1256 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1257 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1258 * is used by boot bios and addr_mode will indicate the addressing mode to be
1259 * used for this FCF when the function returns.
1260 * If the FCF record need to be used with a particular vlan id, the vlan is
1261 * set in the vlan_id on return of the function. If not VLAN tagging need to
1262 * be used with the FCF vlan_id will be set to 0xFFFF;
1263 **/
1264static int
1265lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1266 struct fcf_record *new_fcf_record,
1267 uint32_t *boot_flag, uint32_t *addr_mode,
1268 uint16_t *vlan_id)
1269{
1270 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001271 int i, j, fcf_vlan_id = 0;
1272
1273 /* Find the lowest VLAN id in the FCF record */
1274 for (i = 0; i < 512; i++) {
1275 if (new_fcf_record->vlan_bitmap[i]) {
1276 fcf_vlan_id = i * 8;
1277 j = 0;
1278 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1279 j++;
1280 fcf_vlan_id++;
1281 }
1282 break;
1283 }
1284 }
James Smart6fb120a2009-05-22 14:52:59 -04001285
James Smart0c287582009-06-10 17:22:56 -04001286 /* If FCF not available return 0 */
1287 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1288 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1289 return 0;
1290
James Smart45ed1192009-10-02 15:17:02 -04001291 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001292 *boot_flag = 0;
1293 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1294 new_fcf_record);
1295 if (phba->valid_vlan)
1296 *vlan_id = phba->vlan_id;
1297 else
1298 *vlan_id = 0xFFFF;
1299 return 1;
1300 }
1301
1302 /*
1303 * If there are no FCF connection table entry, driver connect to all
1304 * FCFs.
1305 */
1306 if (list_empty(&phba->fcf_conn_rec_list)) {
1307 *boot_flag = 0;
1308 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1309 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001310
1311 /*
1312 * When there are no FCF connect entries, use driver's default
1313 * addressing mode - FPMA.
1314 */
1315 if (*addr_mode & LPFC_FCF_FPMA)
1316 *addr_mode = LPFC_FCF_FPMA;
1317
James Smart4d9ab992009-10-02 15:16:39 -04001318 /* If FCF record report a vlan id use that vlan id */
1319 if (fcf_vlan_id)
1320 *vlan_id = fcf_vlan_id;
1321 else
1322 *vlan_id = 0xFFFF;
James Smart6fb120a2009-05-22 14:52:59 -04001323 return 1;
1324 }
1325
1326 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list, list) {
1327 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1328 continue;
1329
1330 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1331 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001332 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001333 continue;
James Smart8fa38512009-07-19 10:01:03 -04001334 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1335 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1336 new_fcf_record))
1337 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001338 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1339 /*
1340 * If the vlan bit map does not have the bit set for the
1341 * vlan id to be used, then it is not a match.
1342 */
1343 if (!(new_fcf_record->vlan_bitmap
1344 [conn_entry->conn_rec.vlan_tag / 8] &
1345 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1346 continue;
1347 }
1348
1349 /*
James Smart0c287582009-06-10 17:22:56 -04001350 * If connection record does not support any addressing mode,
1351 * skip the FCF record.
1352 */
1353 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1354 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1355 continue;
1356
1357 /*
James Smart6fb120a2009-05-22 14:52:59 -04001358 * Check if the connection record specifies a required
1359 * addressing mode.
1360 */
1361 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1362 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1363
1364 /*
1365 * If SPMA required but FCF not support this continue.
1366 */
1367 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1368 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1369 new_fcf_record) & LPFC_FCF_SPMA))
1370 continue;
1371
1372 /*
1373 * If FPMA required but FCF not support this continue.
1374 */
1375 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1376 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1377 new_fcf_record) & LPFC_FCF_FPMA))
1378 continue;
1379 }
1380
1381 /*
1382 * This fcf record matches filtering criteria.
1383 */
1384 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1385 *boot_flag = 1;
1386 else
1387 *boot_flag = 0;
1388
James Smart0c287582009-06-10 17:22:56 -04001389 /*
1390 * If user did not specify any addressing mode, or if the
1391 * prefered addressing mode specified by user is not supported
1392 * by FCF, allow fabric to pick the addressing mode.
1393 */
James Smart6fb120a2009-05-22 14:52:59 -04001394 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1395 new_fcf_record);
1396 /*
1397 * If the user specified a required address mode, assign that
1398 * address mode
1399 */
1400 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1401 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1402 *addr_mode = (conn_entry->conn_rec.flags &
1403 FCFCNCT_AM_SPMA) ?
1404 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1405 /*
1406 * If the user specified a prefered address mode, use the
1407 * addr mode only if FCF support the addr_mode.
1408 */
1409 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1410 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1411 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1412 (*addr_mode & LPFC_FCF_SPMA))
1413 *addr_mode = LPFC_FCF_SPMA;
1414 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1415 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1416 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1417 (*addr_mode & LPFC_FCF_FPMA))
1418 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001419
James Smart4d9ab992009-10-02 15:16:39 -04001420 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001421 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1422 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001423 /*
1424 * If no vlan id is specified in connect list, use the vlan id
1425 * in the FCF record
1426 */
1427 else if (fcf_vlan_id)
1428 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001429 else
1430 *vlan_id = 0xFFFF;
1431
1432 return 1;
1433 }
1434
1435 return 0;
1436}
1437
1438/**
James Smart32b97932009-07-19 10:01:21 -04001439 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1440 * @phba: pointer to lpfc hba data structure.
1441 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1442 *
1443 * This function check if there is any fcoe event pending while driver
1444 * scan FCF entries. If there is any pending event, it will restart the
1445 * FCF saning and return 1 else return 0.
1446 */
1447int
1448lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1449{
1450 LPFC_MBOXQ_t *mbox;
1451 int rc;
1452 /*
1453 * If the Link is up and no FCoE events while in the
1454 * FCF discovery, no need to restart FCF discovery.
1455 */
1456 if ((phba->link_state >= LPFC_LINK_UP) &&
1457 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1458 return 0;
1459
1460 spin_lock_irq(&phba->hbalock);
1461 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1462 spin_unlock_irq(&phba->hbalock);
1463
1464 if (phba->link_state >= LPFC_LINK_UP)
1465 lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart1c6f4ef52009-11-18 15:40:49 -05001466 else {
James Smart4d9ab992009-10-02 15:16:39 -04001467 /*
1468 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1469 * flag
1470 */
James Smart1c6f4ef52009-11-18 15:40:49 -05001471 spin_lock_irq(&phba->hbalock);
James Smart4d9ab992009-10-02 15:16:39 -04001472 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
James Smart1c6f4ef52009-11-18 15:40:49 -05001473 spin_unlock_irq(&phba->hbalock);
1474 }
James Smart32b97932009-07-19 10:01:21 -04001475
1476 if (unreg_fcf) {
1477 spin_lock_irq(&phba->hbalock);
1478 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1479 spin_unlock_irq(&phba->hbalock);
1480 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1481 if (!mbox) {
1482 lpfc_printf_log(phba, KERN_ERR,
1483 LOG_DISCOVERY|LOG_MBOX,
1484 "2610 UNREG_FCFI mbox allocation failed\n");
1485 return 1;
1486 }
1487 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
1488 mbox->vport = phba->pport;
1489 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
1490 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1491 if (rc == MBX_NOT_FINISHED) {
1492 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
1493 "2611 UNREG_FCFI issue mbox failed\n");
1494 mempool_free(mbox, phba->mbox_mem_pool);
1495 }
1496 }
1497
1498 return 1;
1499}
1500
1501/**
James Smart6fb120a2009-05-22 14:52:59 -04001502 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
1503 * @phba: pointer to lpfc hba data structure.
1504 * @mboxq: pointer to mailbox object.
1505 *
1506 * This function iterate through all the fcf records available in
1507 * HBA and choose the optimal FCF record for discovery. After finding
1508 * the FCF for discovery it register the FCF record and kick start
1509 * discovery.
1510 * If FCF_IN_USE flag is set in currently used FCF, the routine try to
1511 * use a FCF record which match fabric name and mac address of the
1512 * currently used FCF record.
1513 * If the driver support only one FCF, it will try to use the FCF record
1514 * used by BOOT_BIOS.
1515 */
1516void
1517lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1518{
1519 void *virt_addr;
1520 dma_addr_t phys_addr;
1521 uint8_t *bytep;
1522 struct lpfc_mbx_sge sge;
1523 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1524 uint32_t shdr_status, shdr_add_status;
1525 union lpfc_sli4_cfg_shdr *shdr;
1526 struct fcf_record *new_fcf_record;
1527 int rc;
1528 uint32_t boot_flag, addr_mode;
1529 uint32_t next_fcf_index;
1530 unsigned long flags;
1531 uint16_t vlan_id;
1532
James Smart32b97932009-07-19 10:01:21 -04001533 /* If there is pending FCoE event restart FCF table scan */
1534 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1535 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1536 return;
1537 }
1538
James Smart6fb120a2009-05-22 14:52:59 -04001539 /* Get the first SGE entry from the non-embedded DMA memory. This
1540 * routine only uses a single SGE.
1541 */
1542 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1543 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1544 if (unlikely(!mboxq->sge_array)) {
1545 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1546 "2524 Failed to get the non-embedded SGE "
1547 "virtual address\n");
1548 goto out;
1549 }
1550 virt_addr = mboxq->sge_array->addr[0];
1551
1552 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1553 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1554 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
1555 &shdr->response);
1556 /*
1557 * The FCF Record was read and there is no reason for the driver
1558 * to maintain the FCF record data or memory. Instead, just need
1559 * to book keeping the FCFIs can be used.
1560 */
1561 if (shdr_status || shdr_add_status) {
James Smartdef9c7a2009-12-21 17:02:28 -05001562 if (shdr_status == STATUS_FCF_TABLE_EMPTY) {
1563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1564 "2726 READ_FCF_RECORD Indicates empty "
1565 "FCF table.\n");
1566 } else {
1567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1568 "2521 READ_FCF_RECORD mailbox failed "
1569 "with status x%x add_status x%x, mbx\n",
1570 shdr_status, shdr_add_status);
1571 }
James Smart6fb120a2009-05-22 14:52:59 -04001572 goto out;
1573 }
1574 /* Interpreting the returned information of FCF records */
1575 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1576 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1577 sizeof(struct lpfc_mbx_read_fcf_tbl));
1578 next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1579
1580 new_fcf_record = (struct fcf_record *)(virt_addr +
1581 sizeof(struct lpfc_mbx_read_fcf_tbl));
1582 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1583 sizeof(struct fcf_record));
1584 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1585
1586 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record,
1587 &boot_flag, &addr_mode,
1588 &vlan_id);
1589 /*
1590 * If the fcf record does not match with connect list entries
1591 * read the next entry.
1592 */
1593 if (!rc)
1594 goto read_next_fcf;
1595 /*
1596 * If this is not the first FCF discovery of the HBA, use last
1597 * FCF record for the discovery.
1598 */
1599 spin_lock_irqsave(&phba->hbalock, flags);
1600 if (phba->fcf.fcf_flag & FCF_IN_USE) {
1601 if (lpfc_fab_name_match(phba->fcf.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001602 new_fcf_record) &&
1603 lpfc_sw_name_match(phba->fcf.switch_name,
1604 new_fcf_record) &&
James Smart6fb120a2009-05-22 14:52:59 -04001605 lpfc_mac_addr_match(phba, new_fcf_record)) {
1606 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1607 spin_unlock_irqrestore(&phba->hbalock, flags);
1608 goto out;
1609 }
1610 spin_unlock_irqrestore(&phba->hbalock, flags);
1611 goto read_next_fcf;
1612 }
1613 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1614 /*
1615 * If the current FCF record does not have boot flag
1616 * set and new fcf record has boot flag set, use the
1617 * new fcf record.
1618 */
1619 if (boot_flag && !(phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1620 /* Use this FCF record */
1621 lpfc_copy_fcf_record(phba, new_fcf_record);
1622 phba->fcf.addr_mode = addr_mode;
1623 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1624 if (vlan_id != 0xFFFF) {
1625 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1626 phba->fcf.vlan_id = vlan_id;
1627 }
1628 spin_unlock_irqrestore(&phba->hbalock, flags);
1629 goto read_next_fcf;
1630 }
1631 /*
1632 * If the current FCF record has boot flag set and the
1633 * new FCF record does not have boot flag, read the next
1634 * FCF record.
1635 */
1636 if (!boot_flag && (phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1637 spin_unlock_irqrestore(&phba->hbalock, flags);
1638 goto read_next_fcf;
1639 }
1640 /*
1641 * If there is a record with lower priority value for
1642 * the current FCF, use that record.
1643 */
James Smart8fa38512009-07-19 10:01:03 -04001644 if (lpfc_fab_name_match(phba->fcf.fabric_name,
1645 new_fcf_record) &&
1646 (new_fcf_record->fip_priority < phba->fcf.priority)) {
James Smart6fb120a2009-05-22 14:52:59 -04001647 /* Use this FCF record */
1648 lpfc_copy_fcf_record(phba, new_fcf_record);
1649 phba->fcf.addr_mode = addr_mode;
1650 if (vlan_id != 0xFFFF) {
1651 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1652 phba->fcf.vlan_id = vlan_id;
1653 }
1654 spin_unlock_irqrestore(&phba->hbalock, flags);
1655 goto read_next_fcf;
1656 }
1657 spin_unlock_irqrestore(&phba->hbalock, flags);
1658 goto read_next_fcf;
1659 }
1660 /*
1661 * This is the first available FCF record, use this
1662 * record.
1663 */
1664 lpfc_copy_fcf_record(phba, new_fcf_record);
1665 phba->fcf.addr_mode = addr_mode;
1666 if (boot_flag)
1667 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1668 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1669 if (vlan_id != 0xFFFF) {
1670 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1671 phba->fcf.vlan_id = vlan_id;
1672 }
1673 spin_unlock_irqrestore(&phba->hbalock, flags);
1674 goto read_next_fcf;
1675
1676read_next_fcf:
1677 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1678 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0)
1679 lpfc_register_fcf(phba);
1680 else
1681 lpfc_sli4_read_fcf_record(phba, next_fcf_index);
1682 return;
1683
1684out:
1685 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1686 lpfc_register_fcf(phba);
1687
1688 return;
1689}
1690
1691/**
James Smart1c6834a2009-07-19 10:01:26 -04001692 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
1693 * @phba: pointer to lpfc hba data structure.
1694 * @mboxq: pointer to mailbox data structure.
1695 *
1696 * This function handles completion of init vpi mailbox command.
1697 */
1698static void
1699lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1700{
1701 struct lpfc_vport *vport = mboxq->vport;
1702 if (mboxq->u.mb.mbxStatus) {
1703 lpfc_printf_vlog(vport, KERN_ERR,
1704 LOG_MBOX,
1705 "2609 Init VPI mailbox failed 0x%x\n",
1706 mboxq->u.mb.mbxStatus);
1707 mempool_free(mboxq, phba->mbox_mem_pool);
1708 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1709 return;
1710 }
1711 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
1712
1713 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1714 lpfc_initial_fdisc(vport);
1715 else {
1716 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04001717 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1718 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04001719 }
1720 return;
1721}
1722
1723/**
James Smart6fb120a2009-05-22 14:52:59 -04001724 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
1725 * @phba: pointer to lpfc hba data structure.
1726 *
1727 * This function loops through the list of vports on the @phba and issues an
1728 * FDISC if possible.
1729 */
1730void
1731lpfc_start_fdiscs(struct lpfc_hba *phba)
1732{
1733 struct lpfc_vport **vports;
1734 int i;
James Smart1c6834a2009-07-19 10:01:26 -04001735 LPFC_MBOXQ_t *mboxq;
1736 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001737
1738 vports = lpfc_create_vport_work_array(phba);
1739 if (vports != NULL) {
1740 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1741 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1742 continue;
1743 /* There are no vpi for this vport */
1744 if (vports[i]->vpi > phba->max_vpi) {
1745 lpfc_vport_set_state(vports[i],
1746 FC_VPORT_FAILED);
1747 continue;
1748 }
1749 if (phba->fc_topology == TOPOLOGY_LOOP) {
1750 lpfc_vport_set_state(vports[i],
1751 FC_VPORT_LINKDOWN);
1752 continue;
1753 }
James Smart1c6834a2009-07-19 10:01:26 -04001754 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
1755 mboxq = mempool_alloc(phba->mbox_mem_pool,
1756 GFP_KERNEL);
1757 if (!mboxq) {
1758 lpfc_printf_vlog(vports[i], KERN_ERR,
1759 LOG_MBOX, "2607 Failed to allocate "
1760 "init_vpi mailbox\n");
1761 continue;
1762 }
1763 lpfc_init_vpi(phba, mboxq, vports[i]->vpi);
1764 mboxq->vport = vports[i];
1765 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
1766 rc = lpfc_sli_issue_mbox(phba, mboxq,
1767 MBX_NOWAIT);
1768 if (rc == MBX_NOT_FINISHED) {
1769 lpfc_printf_vlog(vports[i], KERN_ERR,
1770 LOG_MBOX, "2608 Failed to issue "
1771 "init_vpi mailbox\n");
1772 mempool_free(mboxq,
1773 phba->mbox_mem_pool);
1774 }
1775 continue;
1776 }
James Smart6fb120a2009-05-22 14:52:59 -04001777 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1778 lpfc_initial_fdisc(vports[i]);
1779 else {
1780 lpfc_vport_set_state(vports[i],
1781 FC_VPORT_NO_FABRIC_SUPP);
1782 lpfc_printf_vlog(vports[i], KERN_ERR,
1783 LOG_ELS,
1784 "0259 No NPIV "
1785 "Fabric support\n");
1786 }
1787 }
1788 }
1789 lpfc_destroy_vport_work_array(phba, vports);
1790}
1791
1792void
1793lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1794{
1795 struct lpfc_dmabuf *dmabuf = mboxq->context1;
1796 struct lpfc_vport *vport = mboxq->vport;
1797
1798 if (mboxq->u.mb.mbxStatus) {
1799 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1800 "2018 REG_VFI mbxStatus error x%x "
1801 "HBA state x%x\n",
1802 mboxq->u.mb.mbxStatus, vport->port_state);
1803 if (phba->fc_topology == TOPOLOGY_LOOP) {
1804 /* FLOGI failed, use loop map to make discovery list */
1805 lpfc_disc_list_loopmap(vport);
1806 /* Start discovery */
1807 lpfc_disc_start(vport);
1808 goto fail_free_mem;
1809 }
1810 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1811 goto fail_free_mem;
1812 }
James Smartc8685952009-11-18 15:39:16 -05001813 /* The VPI is implicitly registered when the VFI is registered */
1814 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart6fb120a2009-05-22 14:52:59 -04001815
1816 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1817 lpfc_start_fdiscs(phba);
1818 lpfc_do_scr_ns_plogi(phba, vport);
1819 }
1820
1821fail_free_mem:
1822 mempool_free(mboxq, phba->mbox_mem_pool);
1823 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1824 kfree(dmabuf);
1825 return;
1826}
1827
1828static void
James Smart2e0fef82007-06-17 19:56:36 -05001829lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001830{
James Smart6fb120a2009-05-22 14:52:59 -04001831 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001832 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05001833 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001834
1835
1836 /* Check for error */
1837 if (mb->mbxStatus) {
1838 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001839 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1840 "0319 READ_SPARAM mbxStatus error x%x "
1841 "hba state x%x>\n",
1842 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001843 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05001844 goto out;
1845 }
1846
James Smart2e0fef82007-06-17 19:56:36 -05001847 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05001848 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05001849 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05001850 u64_to_wwn(phba->cfg_soft_wwnn,
1851 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04001852 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05001853 u64_to_wwn(phba->cfg_soft_wwpn,
1854 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05001855 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1856 sizeof(vport->fc_nodename));
1857 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1858 sizeof(vport->fc_portname));
1859 if (vport->port_type == LPFC_PHYSICAL_PORT) {
1860 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1861 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1862 }
1863
dea31012005-04-17 16:05:31 -05001864 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1865 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001866 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001867 return;
1868
1869out:
1870 pmb->context1 = NULL;
1871 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1872 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05001873 lpfc_issue_clear_la(phba, vport);
1874 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001875 return;
1876}
1877
1878static void
James Smart92d7f7b2007-06-17 19:56:38 -05001879lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05001880{
James Smart92d7f7b2007-06-17 19:56:38 -05001881 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04001882 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05001883 int i;
James Smart14691152006-12-02 13:34:28 -05001884 struct lpfc_dmabuf *mp;
1885 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001886 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05001887
dea31012005-04-17 16:05:31 -05001888 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001889
James Smart92d7f7b2007-06-17 19:56:38 -05001890 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001891 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05001892 case LA_1GHZ_LINK:
1893 phba->fc_linkspeed = LA_1GHZ_LINK;
1894 break;
1895 case LA_2GHZ_LINK:
1896 phba->fc_linkspeed = LA_2GHZ_LINK;
1897 break;
1898 case LA_4GHZ_LINK:
1899 phba->fc_linkspeed = LA_4GHZ_LINK;
1900 break;
1901 case LA_8GHZ_LINK:
1902 phba->fc_linkspeed = LA_8GHZ_LINK;
1903 break;
James Smartf4b4c682009-05-22 14:53:12 -04001904 case LA_10GHZ_LINK:
1905 phba->fc_linkspeed = LA_10GHZ_LINK;
1906 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001907 default:
1908 phba->fc_linkspeed = LA_UNKNW_LINK;
1909 break;
dea31012005-04-17 16:05:31 -05001910 }
1911
1912 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05001913 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05001914
1915 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05001916 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05001917
James Smart6a9c52c2009-10-02 15:16:51 -04001918 /* if npiv is enabled and this adapter supports npiv log
1919 * a message that npiv is not supported in this topology
1920 */
1921 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04001922 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1923 "1309 Link Up Event npiv not supported in loop "
1924 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001925 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05001926 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05001927 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001928
James Smart2e0fef82007-06-17 19:56:36 -05001929 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05001930 i = la->un.lilpBde64.tus.f.bdeSize;
1931
1932 if (i == 0) {
1933 phba->alpa_map[0] = 0;
1934 } else {
James Smarte8b62012007-08-02 11:10:09 -04001935 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05001936 int numalpa, j, k;
1937 union {
1938 uint8_t pamap[16];
1939 struct {
1940 uint32_t wd1;
1941 uint32_t wd2;
1942 uint32_t wd3;
1943 uint32_t wd4;
1944 } pa;
1945 } un;
1946 numalpa = phba->alpa_map[0];
1947 j = 0;
1948 while (j < numalpa) {
1949 memset(un.pamap, 0, 16);
1950 for (k = 1; j < numalpa; k++) {
1951 un.pamap[k - 1] =
1952 phba->alpa_map[j + 1];
1953 j++;
1954 if (k == 16)
1955 break;
1956 }
1957 /* Link Up Event ALPA map */
1958 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05001959 KERN_WARNING,
1960 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001961 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05001962 "ALPA map Data: x%x "
1963 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001964 un.pa.wd1, un.pa.wd2,
1965 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05001966 }
1967 }
1968 }
1969 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05001970 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04001971 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05001972 (phba->sli_rev == 3))
1973 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1974 }
James Smart2e0fef82007-06-17 19:56:36 -05001975 vport->fc_myDID = phba->fc_pref_DID;
1976 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001977 }
James Smart92d7f7b2007-06-17 19:56:38 -05001978 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001979
1980 lpfc_linkup(phba);
1981 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001982 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05001983 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05001984 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04001985 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05001986 if (rc == MBX_NOT_FINISHED) {
1987 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1988 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1989 kfree(mp);
1990 mempool_free(sparam_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001991 goto out;
James Smart14691152006-12-02 13:34:28 -05001992 }
dea31012005-04-17 16:05:31 -05001993 }
1994
James Smart6fb120a2009-05-22 14:52:59 -04001995 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
1996 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1997 if (!cfglink_mbox)
1998 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05001999 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002000 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002001 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002002 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002003 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002004 if (rc == MBX_NOT_FINISHED) {
2005 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2006 goto out;
2007 }
2008 } else {
James Smart32b97932009-07-19 10:01:21 -04002009 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002010 /*
2011 * Add the driver's default FCF record at FCF index 0 now. This
2012 * is phase 1 implementation that support FCF index 0 and driver
2013 * defaults.
2014 */
James Smart45ed1192009-10-02 15:17:02 -04002015 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002016 fcf_record = kzalloc(sizeof(struct fcf_record),
2017 GFP_KERNEL);
2018 if (unlikely(!fcf_record)) {
2019 lpfc_printf_log(phba, KERN_ERR,
2020 LOG_MBOX | LOG_SLI,
2021 "2554 Could not allocate memmory for "
2022 "fcf record\n");
2023 rc = -ENODEV;
2024 goto out;
2025 }
2026
2027 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2028 LPFC_FCOE_FCF_DEF_INDEX);
2029 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2030 if (unlikely(rc)) {
2031 lpfc_printf_log(phba, KERN_ERR,
2032 LOG_MBOX | LOG_SLI,
2033 "2013 Could not manually add FCF "
2034 "record 0, status %d\n", rc);
2035 rc = -ENODEV;
2036 kfree(fcf_record);
2037 goto out;
2038 }
2039 kfree(fcf_record);
2040 }
2041 /*
2042 * The driver is expected to do FIP/FCF. Call the port
2043 * and get the FCF Table.
2044 */
James Smart32b97932009-07-19 10:01:21 -04002045 spin_lock_irq(&phba->hbalock);
2046 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2047 spin_unlock_irq(&phba->hbalock);
2048 return;
2049 }
2050 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002051 rc = lpfc_sli4_read_fcf_record(phba,
2052 LPFC_FCOE_FCF_GET_FIRST);
2053 if (rc)
2054 goto out;
dea31012005-04-17 16:05:31 -05002055 }
James Smart6fb120a2009-05-22 14:52:59 -04002056
2057 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002058out:
2059 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002060 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2061 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2062 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002063 lpfc_issue_clear_la(phba, vport);
2064 return;
dea31012005-04-17 16:05:31 -05002065}
2066
2067static void
James Smart84774a42008-08-24 21:50:06 -04002068lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002069{
dea31012005-04-17 16:05:31 -05002070 uint32_t control;
2071 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002072 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002073 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002074 if (phba->sli_rev <= LPFC_SLI_REV3) {
2075 control = readl(phba->HCregaddr);
2076 control |= HC_LAINT_ENA;
2077 writel(control, phba->HCregaddr);
2078 readl(phba->HCregaddr); /* flush */
2079 }
James Smart2e0fef82007-06-17 19:56:36 -05002080 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002081}
2082
James Smart84774a42008-08-24 21:50:06 -04002083static void
2084lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2085{
2086 lpfc_linkdown(phba);
2087 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002088 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002089 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2090}
2091
2092
dea31012005-04-17 16:05:31 -05002093/*
2094 * This routine handles processing a READ_LA mailbox
2095 * command upon completion. It is setup in the LPFC_MBOXQ
2096 * as the completion routine when the command is
2097 * handed off to the SLI layer.
2098 */
2099void
James Smart2e0fef82007-06-17 19:56:36 -05002100lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002101{
James Smart2e0fef82007-06-17 19:56:36 -05002102 struct lpfc_vport *vport = pmb->vport;
2103 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002104 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002105 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002106 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2107
James Smart0d2b6b82008-06-14 22:52:47 -04002108 /* Unblock ELS traffic */
2109 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002110 /* Check for error */
2111 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002112 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002113 "1307 READ_LA mbox error x%x state x%x\n",
2114 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002115 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002116 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002117 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2118 }
2119
James Smart04c68492009-05-22 14:52:52 -04002120 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002121
2122 memcpy(&phba->alpa_map[0], mp->virt, 128);
2123
James Smart2e0fef82007-06-17 19:56:36 -05002124 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002125 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002126 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002127 else
James Smart2e0fef82007-06-17 19:56:36 -05002128 vport->fc_flag &= ~FC_BYPASSED_MODE;
2129 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002130
James Smart0c287582009-06-10 17:22:56 -04002131 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002132 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002133 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002134 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002135 if (phba->fc_eventTag != 0)
2136 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002137 }
dea31012005-04-17 16:05:31 -05002138
2139 phba->fc_eventTag = la->eventTag;
James Smart84774a42008-08-24 21:50:06 -04002140 if (la->mm)
2141 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2142 else
2143 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
dea31012005-04-17 16:05:31 -05002144
James Smart4d9ab992009-10-02 15:16:39 -04002145 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002146 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002147 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002148 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002149 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002150 "1306 Link Up Event in loop back mode "
2151 "x%x received Data: x%x x%x x%x x%x\n",
2152 la->eventTag, phba->fc_eventTag,
2153 la->granted_AL_PA, la->UlnkSpeed,
2154 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002155 } else {
2156 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002157 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002158 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002159 la->eventTag, phba->fc_eventTag,
2160 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002161 phba->alpa_map[0],
2162 la->mm, la->fa,
2163 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002164 }
James Smart92d7f7b2007-06-17 19:56:38 -05002165 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002166 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002167 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002168 if (phba->link_flag & LS_LOOPBACK_MODE) {
2169 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2170 "1308 Link Down Event in loop back mode "
2171 "x%x received "
2172 "Data: x%x x%x x%x\n",
2173 la->eventTag, phba->fc_eventTag,
2174 phba->pport->port_state, vport->fc_flag);
2175 }
2176 else {
2177 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002178 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002179 "Data: x%x x%x x%x x%x x%x\n",
2180 la->eventTag, phba->fc_eventTag,
2181 phba->pport->port_state, vport->fc_flag,
2182 la->mm, la->fa);
2183 }
2184 lpfc_mbx_issue_link_down(phba);
2185 }
2186 if (la->mm && la->attType == AT_LINK_UP) {
2187 if (phba->link_state != LPFC_LINK_DOWN) {
2188 phba->fc_stat.LinkDown++;
2189 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2190 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002191 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002192 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002193 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002194 lpfc_mbx_issue_link_down(phba);
2195 } else
2196 lpfc_enable_la(phba);
2197
2198 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2199 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2200 "Data: x%x x%x x%x\n",
2201 la->eventTag, phba->fc_eventTag,
2202 phba->pport->port_state, vport->fc_flag);
2203 /*
2204 * The cmnd that triggered this will be waiting for this
2205 * signal.
2206 */
2207 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2208 if (phba->wait_4_mlo_maint_flg) {
2209 phba->wait_4_mlo_maint_flg = 0;
2210 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002211 }
James Smart84774a42008-08-24 21:50:06 -04002212 }
2213
2214 if (la->fa) {
2215 if (la->mm)
2216 lpfc_issue_clear_la(phba, vport);
2217 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2218 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05002219 }
2220
2221lpfc_mbx_cmpl_read_la_free_mbuf:
2222 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2223 kfree(mp);
2224 mempool_free(pmb, phba->mbox_mem_pool);
2225 return;
2226}
2227
2228/*
2229 * This routine handles processing a REG_LOGIN mailbox
2230 * command upon completion. It is setup in the LPFC_MBOXQ
2231 * as the completion routine when the command is
2232 * handed off to the SLI layer.
2233 */
2234void
James Smart2e0fef82007-06-17 19:56:36 -05002235lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002236{
James Smart2e0fef82007-06-17 19:56:36 -05002237 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002238 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05002239 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05002240
dea31012005-04-17 16:05:31 -05002241 pmb->context1 = NULL;
2242
2243 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05002244 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05002245 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2246 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002247 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05002248 /* decrement the node reference count held for this callback
2249 * function.
2250 */
James Smart329f9bc2007-04-25 09:53:01 -04002251 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002252
2253 return;
2254}
2255
James Smart92d7f7b2007-06-17 19:56:38 -05002256static void
2257lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2258{
James Smart04c68492009-05-22 14:52:52 -04002259 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002260 struct lpfc_vport *vport = pmb->vport;
2261 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2262
2263 switch (mb->mbxStatus) {
2264 case 0x0011:
2265 case 0x0020:
2266 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04002267 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2268 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2269 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002270 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002271 }
James Smartc8685952009-11-18 15:39:16 -05002272 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05002273 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart92d7f7b2007-06-17 19:56:38 -05002274 vport->unreg_vpi_cmpl = VPORT_OK;
2275 mempool_free(pmb, phba->mbox_mem_pool);
2276 /*
2277 * This shost reference might have been taken at the beginning of
2278 * lpfc_vport_delete()
2279 */
James Smart1c6f4ef52009-11-18 15:40:49 -05002280 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05002281 scsi_host_put(shost);
2282}
2283
James Smartd7c255b2008-08-24 21:50:00 -04002284int
James Smart92d7f7b2007-06-17 19:56:38 -05002285lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2286{
2287 struct lpfc_hba *phba = vport->phba;
2288 LPFC_MBOXQ_t *mbox;
2289 int rc;
2290
2291 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2292 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002293 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002294
2295 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2296 mbox->vport = vport;
2297 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002298 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002299 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002300 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2301 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002302 mempool_free(mbox, phba->mbox_mem_pool);
2303 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002304 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002305 }
James Smartd7c255b2008-08-24 21:50:00 -04002306 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002307}
2308
2309static void
2310lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2311{
2312 struct lpfc_vport *vport = pmb->vport;
2313 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002314 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002315
2316 switch (mb->mbxStatus) {
2317 case 0x0011:
2318 case 0x9601:
2319 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002320 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2321 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2322 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002323 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2324 spin_lock_irq(shost->host_lock);
2325 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2326 spin_unlock_irq(shost->host_lock);
2327 vport->fc_myDID = 0;
2328 goto out;
2329 }
James Smart92d7f7b2007-06-17 19:56:38 -05002330
James Smartc8685952009-11-18 15:39:16 -05002331 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart92d7f7b2007-06-17 19:56:38 -05002332 vport->num_disc_nodes = 0;
2333 /* go thru NPR list and issue ELS PLOGIs */
2334 if (vport->fc_npr_cnt)
2335 lpfc_els_disc_plogi(vport);
2336
2337 if (!vport->num_disc_nodes) {
2338 spin_lock_irq(shost->host_lock);
2339 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2340 spin_unlock_irq(shost->host_lock);
2341 lpfc_can_disctmo(vport);
2342 }
2343 vport->port_state = LPFC_VPORT_READY;
2344
2345out:
2346 mempool_free(pmb, phba->mbox_mem_pool);
2347 return;
2348}
2349
James Smart21e9a0a2009-05-22 14:53:21 -04002350/**
2351 * lpfc_create_static_vport - Read HBA config region to create static vports.
2352 * @phba: pointer to lpfc hba data structure.
2353 *
2354 * This routine issue a DUMP mailbox command for config region 22 to get
2355 * the list of static vports to be created. The function create vports
2356 * based on the information returned from the HBA.
2357 **/
2358void
2359lpfc_create_static_vport(struct lpfc_hba *phba)
2360{
2361 LPFC_MBOXQ_t *pmb = NULL;
2362 MAILBOX_t *mb;
2363 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04002364 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04002365 struct fc_vport_identifiers vport_id;
2366 struct fc_vport *new_fc_vport;
2367 struct Scsi_Host *shost;
2368 struct lpfc_vport *vport;
2369 uint16_t offset = 0;
2370 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04002371 struct lpfc_dmabuf *mp;
2372 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04002373
2374 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2375 if (!pmb) {
2376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2377 "0542 lpfc_create_static_vport failed to"
2378 " allocate mailbox memory\n");
2379 return;
2380 }
2381
2382 mb = &pmb->u.mb;
2383
2384 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2385 if (!vport_info) {
2386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2387 "0543 lpfc_create_static_vport failed to"
2388 " allocate vport_info\n");
2389 mempool_free(pmb, phba->mbox_mem_pool);
2390 return;
2391 }
2392
2393 vport_buff = (uint8_t *) vport_info;
2394 do {
James Smart1c6834a2009-07-19 10:01:26 -04002395 if (lpfc_dump_static_vport(phba, pmb, offset))
2396 goto out;
2397
James Smart21e9a0a2009-05-22 14:53:21 -04002398 pmb->vport = phba->pport;
2399 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2400
2401 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2402 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2403 "0544 lpfc_create_static_vport failed to"
2404 " issue dump mailbox command ret 0x%x "
2405 "status 0x%x\n",
2406 rc, mb->mbxStatus);
2407 goto out;
2408 }
2409
James Smart1c6834a2009-07-19 10:01:26 -04002410 if (phba->sli_rev == LPFC_SLI_REV4) {
2411 byte_count = pmb->u.mqe.un.mb_words[5];
2412 mp = (struct lpfc_dmabuf *) pmb->context2;
2413 if (byte_count > sizeof(struct static_vport_info) -
2414 offset)
2415 byte_count = sizeof(struct static_vport_info)
2416 - offset;
2417 memcpy(vport_buff + offset, mp->virt, byte_count);
2418 offset += byte_count;
2419 } else {
2420 if (mb->un.varDmp.word_cnt >
2421 sizeof(struct static_vport_info) - offset)
2422 mb->un.varDmp.word_cnt =
2423 sizeof(struct static_vport_info)
2424 - offset;
2425 byte_count = mb->un.varDmp.word_cnt;
2426 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2427 vport_buff + offset,
2428 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04002429
James Smart1c6834a2009-07-19 10:01:26 -04002430 offset += byte_count;
2431 }
James Smart21e9a0a2009-05-22 14:53:21 -04002432
James Smart1c6834a2009-07-19 10:01:26 -04002433 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04002434 offset < sizeof(struct static_vport_info));
2435
2436
2437 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2438 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2439 != VPORT_INFO_REV)) {
2440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2441 "0545 lpfc_create_static_vport bad"
2442 " information header 0x%x 0x%x\n",
2443 le32_to_cpu(vport_info->signature),
2444 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2445
2446 goto out;
2447 }
2448
2449 shost = lpfc_shost_from_vport(phba->pport);
2450
2451 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2452 memset(&vport_id, 0, sizeof(vport_id));
2453 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2454 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2455 if (!vport_id.port_name || !vport_id.node_name)
2456 continue;
2457
2458 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2459 vport_id.vport_type = FC_PORTTYPE_NPIV;
2460 vport_id.disable = false;
2461 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2462
2463 if (!new_fc_vport) {
2464 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2465 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04002466 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04002467 continue;
2468 }
2469
2470 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2471 vport->vport_flag |= STATIC_VPORT;
2472 }
2473
2474out:
James Smart21e9a0a2009-05-22 14:53:21 -04002475 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04002476 if (rc != MBX_TIMEOUT) {
2477 if (pmb->context2) {
2478 mp = (struct lpfc_dmabuf *) pmb->context2;
2479 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2480 kfree(mp);
2481 }
James Smart21e9a0a2009-05-22 14:53:21 -04002482 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002483 }
James Smart21e9a0a2009-05-22 14:53:21 -04002484
2485 return;
2486}
2487
dea31012005-04-17 16:05:31 -05002488/*
2489 * This routine handles processing a Fabric REG_LOGIN mailbox
2490 * command upon completion. It is setup in the LPFC_MBOXQ
2491 * as the completion routine when the command is
2492 * handed off to the SLI layer.
2493 */
2494void
James Smart2e0fef82007-06-17 19:56:36 -05002495lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002496{
James Smart92d7f7b2007-06-17 19:56:38 -05002497 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04002498 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002499 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05002500 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002501
James Smart549e55c2007-08-02 11:09:51 -04002502 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04002503 pmb->context1 = NULL;
2504 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05002505 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04002506 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2507 "0258 Register Fabric login error: 0x%x\n",
2508 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05002509 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2510 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002511 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002512
James Smart92d7f7b2007-06-17 19:56:38 -05002513 if (phba->fc_topology == TOPOLOGY_LOOP) {
2514 /* FLOGI failed, use loop map to make discovery list */
2515 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002516
James Smart92d7f7b2007-06-17 19:56:38 -05002517 /* Start discovery */
2518 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05002519 /* Decrement the reference count to ndlp after the
2520 * reference to the ndlp are done.
2521 */
2522 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05002523 return;
2524 }
2525
2526 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05002527 /* Decrement the reference count to ndlp after the reference
2528 * to the ndlp are done.
2529 */
2530 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002531 return;
2532 }
2533
dea31012005-04-17 16:05:31 -05002534 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04002535 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05002536 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002537 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002538
James Smart2e0fef82007-06-17 19:56:36 -05002539 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart21e9a0a2009-05-22 14:53:21 -04002540 lpfc_start_fdiscs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002541 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05002542 }
2543
2544 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2545 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002546 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002547
2548 /* Drop the reference count from the mbox at the end after
2549 * all the current reference to the ndlp have been done.
2550 */
2551 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002552 return;
2553}
2554
2555/*
2556 * This routine handles processing a NameServer REG_LOGIN mailbox
2557 * command upon completion. It is setup in the LPFC_MBOXQ
2558 * as the completion routine when the command is
2559 * handed off to the SLI layer.
2560 */
2561void
James Smart2e0fef82007-06-17 19:56:36 -05002562lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002563{
James Smart21e9a0a2009-05-22 14:53:21 -04002564 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05002565 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2566 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2567 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002568
2569 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05002570out:
James Smart21e9a0a2009-05-22 14:53:21 -04002571 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2572 "0260 Register NameServer error: 0x%x\n",
2573 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05002574 /* decrement the node reference count held for this
2575 * callback function.
2576 */
James Smart329f9bc2007-04-25 09:53:01 -04002577 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002578 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2579 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04002580 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04002581
2582 /* If no other thread is using the ndlp, free it */
2583 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05002584
James Smart92d7f7b2007-06-17 19:56:38 -05002585 if (phba->fc_topology == TOPOLOGY_LOOP) {
2586 /*
2587 * RegLogin failed, use loop map to make discovery
2588 * list
2589 */
2590 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002591
James Smart92d7f7b2007-06-17 19:56:38 -05002592 /* Start discovery */
2593 lpfc_disc_start(vport);
2594 return;
2595 }
2596 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05002597 return;
2598 }
2599
2600 pmb->context1 = NULL;
2601
dea31012005-04-17 16:05:31 -05002602 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04002603 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05002604 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002605 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002606
James Smart2e0fef82007-06-17 19:56:36 -05002607 if (vport->port_state < LPFC_VPORT_READY) {
2608 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05002609 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
2610 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
2611 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2612 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
2613 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
2614
2615 /* Issue SCR just before NameServer GID_FT Query */
2616 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05002617 }
2618
James Smart2e0fef82007-06-17 19:56:36 -05002619 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002620 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05002621 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05002622 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05002623 goto out;
dea31012005-04-17 16:05:31 -05002624 }
2625
James Smartfa4066b2008-01-11 01:53:27 -05002626 /* decrement the node reference count held for this
2627 * callback function.
2628 */
James Smart329f9bc2007-04-25 09:53:01 -04002629 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002630 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2631 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002632 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002633
2634 return;
2635}
2636
2637static void
James Smart2e0fef82007-06-17 19:56:36 -05002638lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002639{
James Smart2e0fef82007-06-17 19:56:36 -05002640 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2641 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05002642 struct lpfc_rport_data *rdata;
2643 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05002644 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002645
2646 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07002647 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2648 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05002649 rport_ids.port_id = ndlp->nlp_DID;
2650 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05002651
James Smart329f9bc2007-04-25 09:53:01 -04002652 /*
2653 * We leave our node pointer in rport->dd_data when we unregister a
2654 * FCP target port. But fc_remote_port_add zeros the space to which
2655 * rport->dd_data points. So, if we're reusing a previously
2656 * registered port, drop the reference that we took the last time we
2657 * registered the port.
2658 */
2659 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05002660 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04002661 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05002662
2663 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
2664 "rport add: did:x%x flg:x%x type x%x",
2665 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2666
James Smart2e0fef82007-06-17 19:56:36 -05002667 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04002668 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05002669 dev_printk(KERN_WARNING, &phba->pcidev->dev,
2670 "Warning: fc_remote_port_add failed\n");
2671 return;
2672 }
2673
2674 /* initialize static port data */
2675 rport->maxframe_size = ndlp->nlp_maxframe;
2676 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05002677 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04002678 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05002679
2680 if (ndlp->nlp_type & NLP_FCP_TARGET)
2681 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2682 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
2683 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2684
2685
2686 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
2687 fc_remote_port_rolechg(rport, rport_ids.roles);
2688
James Smart071fbd3d2006-04-15 11:53:20 -04002689 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002690 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04002691 ndlp->nlp_sid = rport->scsi_target_id;
2692 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002693 return;
2694}
2695
2696static void
James Smart2e0fef82007-06-17 19:56:36 -05002697lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002698{
2699 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04002700
James Smart858c9f62007-06-17 19:56:39 -05002701 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
2702 "rport delete: did:x%x flg:x%x type x%x",
2703 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2704
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002705 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05002706
2707 return;
2708}
2709
James Smartde0c5b32007-04-25 09:52:27 -04002710static void
James Smart2e0fef82007-06-17 19:56:36 -05002711lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04002712{
James Smart2e0fef82007-06-17 19:56:36 -05002713 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2714
2715 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002716 switch (state) {
2717 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002718 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002719 break;
2720 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002721 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002722 break;
2723 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002724 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002725 break;
2726 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002727 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002728 break;
2729 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002730 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002731 break;
2732 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002733 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002734 break;
2735 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002736 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002737 break;
2738 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002739 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002740 break;
2741 }
James Smart2e0fef82007-06-17 19:56:36 -05002742 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002743}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002744
James Smartde0c5b32007-04-25 09:52:27 -04002745static void
James Smart2e0fef82007-06-17 19:56:36 -05002746lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04002747 int old_state, int new_state)
2748{
James Smart2e0fef82007-06-17 19:56:36 -05002749 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2750
James Smartde0c5b32007-04-25 09:52:27 -04002751 if (new_state == NLP_STE_UNMAPPED_NODE) {
2752 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2753 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2754 ndlp->nlp_type |= NLP_FC_NODE;
2755 }
2756 if (new_state == NLP_STE_MAPPED_NODE)
2757 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2758 if (new_state == NLP_STE_NPR_NODE)
2759 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05002760
James Smartde0c5b32007-04-25 09:52:27 -04002761 /* Transport interface */
2762 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
2763 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002764 vport->phba->nport_event_cnt++;
2765 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002766 }
dea31012005-04-17 16:05:31 -05002767
James Smartde0c5b32007-04-25 09:52:27 -04002768 if (new_state == NLP_STE_MAPPED_NODE ||
2769 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05002770 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05002771 /*
2772 * Tell the fc transport about the port, if we haven't
2773 * already. If we have, and it's a scsi entity, be
2774 * sure to unblock any attached scsi devices
2775 */
2776 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002777 }
James Smartea2151b2008-09-07 11:52:10 -04002778 if ((new_state == NLP_STE_MAPPED_NODE) &&
2779 (vport->stat_data_enabled)) {
2780 /*
2781 * A new target is discovered, if there is no buffer for
2782 * statistical data collection allocate buffer.
2783 */
2784 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
2785 sizeof(struct lpfc_scsicmd_bkt),
2786 GFP_KERNEL);
2787
2788 if (!ndlp->lat_data)
2789 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2790 "0286 lpfc_nlp_state_cleanup failed to "
2791 "allocate statistical data buffer DID "
2792 "0x%x\n", ndlp->nlp_DID);
2793 }
James Smart858c9f62007-06-17 19:56:39 -05002794 /*
2795 * if we added to Mapped list, but the remote port
2796 * registration failed or assigned a target id outside
2797 * our presentable range - move the node to the
2798 * Unmapped List
2799 */
James Smartde0c5b32007-04-25 09:52:27 -04002800 if (new_state == NLP_STE_MAPPED_NODE &&
2801 (!ndlp->rport ||
2802 ndlp->rport->scsi_target_id == -1 ||
2803 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05002804 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002805 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05002806 spin_unlock_irq(shost->host_lock);
2807 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002808 }
James Smartde0c5b32007-04-25 09:52:27 -04002809}
2810
James Smart685f0bf2007-04-25 09:53:08 -04002811static char *
2812lpfc_nlp_state_name(char *buffer, size_t size, int state)
2813{
2814 static char *states[] = {
2815 [NLP_STE_UNUSED_NODE] = "UNUSED",
2816 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
2817 [NLP_STE_ADISC_ISSUE] = "ADISC",
2818 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
2819 [NLP_STE_PRLI_ISSUE] = "PRLI",
2820 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
2821 [NLP_STE_MAPPED_NODE] = "MAPPED",
2822 [NLP_STE_NPR_NODE] = "NPR",
2823 };
2824
James Smart311464e2007-08-02 11:10:37 -04002825 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04002826 strlcpy(buffer, states[state], size);
2827 else
2828 snprintf(buffer, size, "unknown (%d)", state);
2829 return buffer;
2830}
2831
James Smartde0c5b32007-04-25 09:52:27 -04002832void
James Smart2e0fef82007-06-17 19:56:36 -05002833lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2834 int state)
James Smartde0c5b32007-04-25 09:52:27 -04002835{
James Smart2e0fef82007-06-17 19:56:36 -05002836 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04002837 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04002838 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04002839
James Smarte8b62012007-08-02 11:10:09 -04002840 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2841 "0904 NPort state transition x%06x, %s -> %s\n",
2842 ndlp->nlp_DID,
2843 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
2844 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05002845
2846 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2847 "node statechg did:x%x old:%d ste:%d",
2848 ndlp->nlp_DID, old_state, state);
2849
James Smartde0c5b32007-04-25 09:52:27 -04002850 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04002851 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05002852 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002853 if (old_state == NLP_STE_UNMAPPED_NODE) {
2854 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
2855 ndlp->nlp_type &= ~NLP_FC_NODE;
2856 }
2857
James Smart685f0bf2007-04-25 09:53:08 -04002858 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05002859 spin_lock_irq(shost->host_lock);
2860 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2861 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002862 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05002863 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04002864
2865 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05002866 lpfc_nlp_counters(vport, state, 1);
2867 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04002868}
2869
2870void
James Smarte47c9092008-02-08 18:49:26 -05002871lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2872{
2873 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2874
2875 if (list_empty(&ndlp->nlp_listp)) {
2876 spin_lock_irq(shost->host_lock);
2877 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2878 spin_unlock_irq(shost->host_lock);
2879 }
2880}
2881
2882void
James Smart2e0fef82007-06-17 19:56:36 -05002883lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04002884{
James Smart2e0fef82007-06-17 19:56:36 -05002885 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2886
James Smart0d2b6b82008-06-14 22:52:47 -04002887 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002888 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05002889 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2890 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002891 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05002892 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002893 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05002894 NLP_STE_UNUSED_NODE);
2895}
2896
Adrian Bunk4d9db012008-02-14 23:24:02 +02002897static void
James Smarte47c9092008-02-08 18:49:26 -05002898lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2899{
James Smart0d2b6b82008-06-14 22:52:47 -04002900 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05002901 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2902 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2903 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2904 NLP_STE_UNUSED_NODE);
2905}
James Smart109f6ed2008-12-04 22:39:08 -05002906/**
James Smart3621a712009-04-06 18:47:14 -04002907 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05002908 * @vport: Pointer to Virtual Port object.
2909 * @ndlp: Pointer to FC node object.
2910 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04002911 *
2912 * This function is always called when node object need to be initialized.
2913 * It initializes all the fields of the node object. Although the reference
2914 * to phba from @ndlp can be obtained indirectly through it's reference to
2915 * @vport, a direct reference to phba is taken here by @ndlp. This is due
2916 * to the life-span of the @ndlp might go beyond the existence of @vport as
2917 * the final release of ndlp is determined by its reference count. And, the
2918 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05002919 **/
2920static inline void
2921lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2922 uint32_t did)
2923{
2924 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2925 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2926 init_timer(&ndlp->nlp_delayfunc);
2927 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2928 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2929 ndlp->nlp_DID = did;
2930 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04002931 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05002932 ndlp->nlp_sid = NLP_NO_SID;
2933 kref_init(&ndlp->kref);
2934 NLP_INT_NODE_ACT(ndlp);
2935 atomic_set(&ndlp->cmd_pending, 0);
2936 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2937}
James Smarte47c9092008-02-08 18:49:26 -05002938
2939struct lpfc_nodelist *
2940lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2941 int state)
2942{
2943 struct lpfc_hba *phba = vport->phba;
2944 uint32_t did;
2945 unsigned long flags;
2946
2947 if (!ndlp)
2948 return NULL;
2949
2950 spin_lock_irqsave(&phba->ndlp_lock, flags);
2951 /* The ndlp should not be in memory free mode */
2952 if (NLP_CHK_FREE_REQ(ndlp)) {
2953 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2954 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2955 "0277 lpfc_enable_node: ndlp:x%p "
2956 "usgmap:x%x refcnt:%d\n",
2957 (void *)ndlp, ndlp->nlp_usg_map,
2958 atomic_read(&ndlp->kref.refcount));
2959 return NULL;
2960 }
2961 /* The ndlp should not already be in active mode */
2962 if (NLP_CHK_NODE_ACT(ndlp)) {
2963 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2964 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2965 "0278 lpfc_enable_node: ndlp:x%p "
2966 "usgmap:x%x refcnt:%d\n",
2967 (void *)ndlp, ndlp->nlp_usg_map,
2968 atomic_read(&ndlp->kref.refcount));
2969 return NULL;
2970 }
2971
2972 /* Keep the original DID */
2973 did = ndlp->nlp_DID;
2974
2975 /* re-initialize ndlp except of ndlp linked list pointer */
2976 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
2977 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05002978 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05002979
2980 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2981
2982 if (state != NLP_STE_UNUSED_NODE)
2983 lpfc_nlp_set_state(vport, ndlp, state);
2984
2985 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2986 "node enable: did:x%x",
2987 ndlp->nlp_DID, 0, 0);
2988 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04002989}
2990
2991void
James Smart2e0fef82007-06-17 19:56:36 -05002992lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04002993{
James Smart87af33f2007-10-27 13:37:43 -04002994 /*
James Smartfa4066b2008-01-11 01:53:27 -05002995 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04002996 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05002997 * the ndlp from the vport. The ndlp marked as UNUSED on the list
2998 * until ALL other outstanding threads have completed. We check
2999 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003000 */
James Smartfa4066b2008-01-11 01:53:27 -05003001 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3002 return;
James Smart51ef4c22007-08-02 11:10:31 -04003003 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003004 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003005 return;
dea31012005-04-17 16:05:31 -05003006}
3007
3008/*
3009 * Start / ReStart rescue timer for Discovery / RSCN handling
3010 */
3011void
James Smart2e0fef82007-06-17 19:56:36 -05003012lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003013{
James Smart2e0fef82007-06-17 19:56:36 -05003014 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3015 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003016 uint32_t tmo;
3017
James Smart2e0fef82007-06-17 19:56:36 -05003018 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003019 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003020 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3021 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003022 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003023 * FC spec states we need 3 * ratov for CT requests
3024 */
3025 tmo = ((phba->fc_ratov * 3) + 3);
3026 }
dea31012005-04-17 16:05:31 -05003027
James Smart858c9f62007-06-17 19:56:39 -05003028
3029 if (!timer_pending(&vport->fc_disctmo)) {
3030 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3031 "set disc timer: tmo:x%x state:x%x flg:x%x",
3032 tmo, vport->port_state, vport->fc_flag);
3033 }
3034
James Smart2e0fef82007-06-17 19:56:36 -05003035 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3036 spin_lock_irq(shost->host_lock);
3037 vport->fc_flag |= FC_DISC_TMO;
3038 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003039
3040 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003041 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3042 "0247 Start Discovery Timer state x%x "
3043 "Data: x%x x%lx x%x x%x\n",
3044 vport->port_state, tmo,
3045 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3046 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003047
3048 return;
3049}
3050
3051/*
3052 * Cancel rescue timer for Discovery / RSCN handling
3053 */
3054int
James Smart2e0fef82007-06-17 19:56:36 -05003055lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003056{
James Smart2e0fef82007-06-17 19:56:36 -05003057 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003058 unsigned long iflags;
3059
James Smart858c9f62007-06-17 19:56:39 -05003060 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3061 "can disc timer: state:x%x rtry:x%x flg:x%x",
3062 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3063
dea31012005-04-17 16:05:31 -05003064 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003065 if (vport->fc_flag & FC_DISC_TMO) {
3066 spin_lock_irqsave(shost->host_lock, iflags);
3067 vport->fc_flag &= ~FC_DISC_TMO;
3068 spin_unlock_irqrestore(shost->host_lock, iflags);
3069 del_timer_sync(&vport->fc_disctmo);
3070 spin_lock_irqsave(&vport->work_port_lock, iflags);
3071 vport->work_port_events &= ~WORKER_DISC_TMO;
3072 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003073 }
3074
3075 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003076 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3077 "0248 Cancel Discovery Timer state x%x "
3078 "Data: x%x x%x x%x\n",
3079 vport->port_state, vport->fc_flag,
3080 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003081 return 0;
dea31012005-04-17 16:05:31 -05003082}
3083
3084/*
3085 * Check specified ring for outstanding IOCB on the SLI queue
3086 * Return true if iocb matches the specified nport
3087 */
3088int
James Smart2e0fef82007-06-17 19:56:36 -05003089lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3090 struct lpfc_sli_ring *pring,
3091 struct lpfc_iocbq *iocb,
3092 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003093{
James Smart2e0fef82007-06-17 19:56:36 -05003094 struct lpfc_sli *psli = &phba->sli;
3095 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003096 struct lpfc_vport *vport = ndlp->vport;
3097
3098 if (iocb->vport != vport)
3099 return 0;
3100
dea31012005-04-17 16:05:31 -05003101 if (pring->ringno == LPFC_ELS_RING) {
3102 switch (icmd->ulpCommand) {
3103 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003104 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003105 return 1;
dea31012005-04-17 16:05:31 -05003106 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003107 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3108 return 1;
dea31012005-04-17 16:05:31 -05003109 case CMD_XMIT_ELS_RSP64_CX:
3110 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003111 return 1;
dea31012005-04-17 16:05:31 -05003112 }
James Smarta4bc3372006-12-02 13:34:16 -05003113 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003114
3115 } else if (pring->ringno == psli->fcp_ring) {
3116 /* Skip match check if waiting to relogin to FCP target */
3117 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003118 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003119 return 0;
dea31012005-04-17 16:05:31 -05003120 }
3121 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003122 return 1;
dea31012005-04-17 16:05:31 -05003123 }
3124 } else if (pring->ringno == psli->next_ring) {
3125
3126 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003127 return 0;
dea31012005-04-17 16:05:31 -05003128}
3129
3130/*
3131 * Free resources / clean up outstanding I/Os
3132 * associated with nlp_rpi in the LPFC_NODELIST entry.
3133 */
3134static int
James Smart2e0fef82007-06-17 19:56:36 -05003135lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003136{
James Smart2534ba72007-04-25 09:52:20 -04003137 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003138 struct lpfc_sli *psli;
3139 struct lpfc_sli_ring *pring;
3140 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003141 uint32_t i;
dea31012005-04-17 16:05:31 -05003142
James Smart92d7f7b2007-06-17 19:56:38 -05003143 lpfc_fabric_abort_nport(ndlp);
3144
dea31012005-04-17 16:05:31 -05003145 /*
3146 * Everything that matches on txcmplq will be returned
3147 * by firmware with a no rpi error.
3148 */
3149 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003150 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003151 /* Now process each ring */
3152 for (i = 0; i < psli->num_rings; i++) {
3153 pring = &psli->ring[i];
3154
James Smart2e0fef82007-06-17 19:56:36 -05003155 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003156 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003157 list) {
dea31012005-04-17 16:05:31 -05003158 /*
3159 * Check to see if iocb matches the nport we are
3160 * looking for
3161 */
James Smart92d7f7b2007-06-17 19:56:38 -05003162 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3163 ndlp))) {
dea31012005-04-17 16:05:31 -05003164 /* It matches, so deque and call compl
3165 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003166 list_move_tail(&iocb->list,
3167 &completions);
dea31012005-04-17 16:05:31 -05003168 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003169 }
3170 }
James Smart2e0fef82007-06-17 19:56:36 -05003171 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003172 }
3173 }
James Smart2534ba72007-04-25 09:52:20 -04003174
James Smarta257bf92009-04-06 18:48:10 -04003175 /* Cancel all the IOCBs from the completions list */
3176 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3177 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04003178
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003179 return 0;
dea31012005-04-17 16:05:31 -05003180}
3181
3182/*
3183 * Free rpi associated with LPFC_NODELIST entry.
3184 * This routine is called from lpfc_freenode(), when we are removing
3185 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3186 * LOGO that completes successfully, and we are waiting to PLOGI back
3187 * to the remote NPort. In addition, it is called after we receive
3188 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3189 * we are waiting to PLOGI back to the remote NPort.
3190 */
3191int
James Smart2e0fef82007-06-17 19:56:36 -05003192lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003193{
James Smart2e0fef82007-06-17 19:56:36 -05003194 struct lpfc_hba *phba = vport->phba;
3195 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05003196 int rc;
3197
James Smart21e9a0a2009-05-22 14:53:21 -04003198 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05003199 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3200 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05003201 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05003202 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003203 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003204 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05003205 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05003206 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003207 }
dea31012005-04-17 16:05:31 -05003208 lpfc_no_rpi(phba, ndlp);
3209 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003210 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04003211 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05003212 return 1;
3213 }
3214 return 0;
3215}
3216
James Smart92d7f7b2007-06-17 19:56:38 -05003217void
3218lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3219{
3220 struct lpfc_hba *phba = vport->phba;
3221 LPFC_MBOXQ_t *mbox;
3222 int rc;
3223
3224 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3225 if (mbox) {
3226 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3227 mbox->vport = vport;
3228 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003229 mbox->context1 = NULL;
3230 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003231 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05003232 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04003233
3234 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3235 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3236 "1836 Could not issue "
3237 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003238 }
3239}
3240
3241void
3242lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3243{
3244 struct lpfc_hba *phba = vport->phba;
3245 LPFC_MBOXQ_t *mbox;
3246 int rc;
3247
3248 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3249 if (mbox) {
3250 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3251 mbox->vport = vport;
3252 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05003253 mbox->context1 = NULL;
3254 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04003255 if (rc != MBX_TIMEOUT)
3256 mempool_free(mbox, phba->mbox_mem_pool);
3257
3258 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04003259 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3260 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04003261 "unreg_did (default rpis) status %d\n",
3262 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05003263 }
3264}
3265
dea31012005-04-17 16:05:31 -05003266/*
3267 * Free resources associated with LPFC_NODELIST entry
3268 * so it can be freed.
3269 */
3270static int
James Smart2e0fef82007-06-17 19:56:36 -05003271lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003272{
James Smart2e0fef82007-06-17 19:56:36 -05003273 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3274 struct lpfc_hba *phba = vport->phba;
3275 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05003276 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05003277
3278 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04003279 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3280 "0900 Cleanup node for NPort x%x "
3281 "Data: x%x x%x x%x\n",
3282 ndlp->nlp_DID, ndlp->nlp_flag,
3283 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05003284 if (NLP_CHK_FREE_REQ(ndlp)) {
3285 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3286 "0280 lpfc_cleanup_node: ndlp:x%p "
3287 "usgmap:x%x refcnt:%d\n",
3288 (void *)ndlp, ndlp->nlp_usg_map,
3289 atomic_read(&ndlp->kref.refcount));
3290 lpfc_dequeue_node(vport, ndlp);
3291 } else {
3292 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3293 "0281 lpfc_cleanup_node: ndlp:x%p "
3294 "usgmap:x%x refcnt:%d\n",
3295 (void *)ndlp, ndlp->nlp_usg_map,
3296 atomic_read(&ndlp->kref.refcount));
3297 lpfc_disable_node(vport, ndlp);
3298 }
dea31012005-04-17 16:05:31 -05003299
dea31012005-04-17 16:05:31 -05003300 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3301 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04003302 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05003303 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3304 mb->context2 = NULL;
3305 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3306 }
3307 }
James Smart33ccf8d2006-08-17 11:57:58 -04003308
James Smart2e0fef82007-06-17 19:56:36 -05003309 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003310 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04003311 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003312 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05003313 mp = (struct lpfc_dmabuf *) (mb->context1);
3314 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05003315 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05003316 kfree(mp);
3317 }
3318 list_del(&mb->list);
3319 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003320 /* We shall not invoke the lpfc_nlp_put to decrement
3321 * the ndlp reference count as we are in the process
3322 * of lpfc_nlp_release.
3323 */
dea31012005-04-17 16:05:31 -05003324 }
3325 }
James Smart2e0fef82007-06-17 19:56:36 -05003326 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003327
James Smarte47c9092008-02-08 18:49:26 -05003328 lpfc_els_abort(phba, ndlp);
3329
James Smart2e0fef82007-06-17 19:56:36 -05003330 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04003331 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003332 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003333
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05003334 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05003335 del_timer_sync(&ndlp->nlp_delayfunc);
3336
James Smart0d2b6b82008-06-14 22:52:47 -04003337 list_del_init(&ndlp->els_retry_evt.evt_listp);
3338 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003339
James Smart2e0fef82007-06-17 19:56:36 -05003340 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05003341
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003342 return 0;
dea31012005-04-17 16:05:31 -05003343}
3344
3345/*
3346 * Check to see if we can free the nlp back to the freelist.
3347 * If we are in the middle of using the nlp in the discovery state
3348 * machine, defer the free till we reach the end of the state machine.
3349 */
James Smart329f9bc2007-04-25 09:53:01 -04003350static void
James Smart2e0fef82007-06-17 19:56:36 -05003351lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003352{
James Smarta8adb832007-10-27 13:37:53 -04003353 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04003354 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04003355 LPFC_MBOXQ_t *mbox;
3356 int rc;
dea31012005-04-17 16:05:31 -05003357
James Smart0d2b6b82008-06-14 22:52:47 -04003358 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04003359 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3360 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04003361 /* For this case we need to cleanup the default rpi
3362 * allocated by the firmware.
3363 */
3364 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3365 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04003366 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04003367 (uint8_t *) &vport->fc_sparam, mbox, 0);
3368 if (rc) {
3369 mempool_free(mbox, phba->mbox_mem_pool);
3370 }
3371 else {
3372 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3373 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3374 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05003375 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04003376 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3377 if (rc == MBX_NOT_FINISHED) {
3378 mempool_free(mbox, phba->mbox_mem_pool);
3379 }
3380 }
3381 }
3382 }
James Smart2e0fef82007-06-17 19:56:36 -05003383 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04003384
James Smart2e0fef82007-06-17 19:56:36 -05003385 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003386 * We can get here with a non-NULL ndlp->rport because when we
3387 * unregister a rport we don't break the rport/node linkage. So if we
3388 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003389 */
James Smart92d7f7b2007-06-17 19:56:38 -05003390 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003391 rdata = ndlp->rport->dd_data;
3392 rdata->pnode = NULL;
3393 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003394 }
dea31012005-04-17 16:05:31 -05003395}
3396
3397static int
James Smart2e0fef82007-06-17 19:56:36 -05003398lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3399 uint32_t did)
dea31012005-04-17 16:05:31 -05003400{
James Smart2e0fef82007-06-17 19:56:36 -05003401 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003402
3403 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003404 return 0;
dea31012005-04-17 16:05:31 -05003405
dea31012005-04-17 16:05:31 -05003406 /* First check for Direct match */
3407 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003408 return 1;
dea31012005-04-17 16:05:31 -05003409
3410 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003411 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003412 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003413 return 0;
dea31012005-04-17 16:05:31 -05003414 }
3415
3416 matchdid.un.word = did;
3417 ndlpdid.un.word = ndlp->nlp_DID;
3418 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3419 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3420 (mydid.un.b.area == matchdid.un.b.area)) {
3421 if ((ndlpdid.un.b.domain == 0) &&
3422 (ndlpdid.un.b.area == 0)) {
3423 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003424 return 1;
dea31012005-04-17 16:05:31 -05003425 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003426 return 0;
dea31012005-04-17 16:05:31 -05003427 }
3428
3429 matchdid.un.word = ndlp->nlp_DID;
3430 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3431 (mydid.un.b.area == ndlpdid.un.b.area)) {
3432 if ((matchdid.un.b.domain == 0) &&
3433 (matchdid.un.b.area == 0)) {
3434 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003435 return 1;
dea31012005-04-17 16:05:31 -05003436 }
3437 }
3438 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003439 return 0;
dea31012005-04-17 16:05:31 -05003440}
3441
James Smart685f0bf2007-04-25 09:53:08 -04003442/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003443static struct lpfc_nodelist *
3444__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003445{
James Smart2fb9bd82006-12-02 13:33:57 -05003446 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003447 uint32_t data1;
3448
James Smart2e0fef82007-06-17 19:56:36 -05003449 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3450 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04003451 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3452 ((uint32_t) ndlp->nlp_xri << 16) |
3453 ((uint32_t) ndlp->nlp_type << 8) |
3454 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04003455 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3456 "0929 FIND node DID "
3457 "Data: x%p x%x x%x x%x\n",
3458 ndlp, ndlp->nlp_DID,
3459 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04003460 return ndlp;
dea31012005-04-17 16:05:31 -05003461 }
3462 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003463
dea31012005-04-17 16:05:31 -05003464 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04003465 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3466 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05003467 return NULL;
3468}
3469
3470struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003471lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003472{
James Smart2e0fef82007-06-17 19:56:36 -05003473 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003474 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003475
James Smart2e0fef82007-06-17 19:56:36 -05003476 spin_lock_irq(shost->host_lock);
3477 ndlp = __lpfc_findnode_did(vport, did);
3478 spin_unlock_irq(shost->host_lock);
3479 return ndlp;
3480}
3481
3482struct lpfc_nodelist *
3483lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3484{
3485 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3486 struct lpfc_nodelist *ndlp;
3487
3488 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003489 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05003490 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
3491 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05003492 return NULL;
3493 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05003494 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003495 if (!ndlp)
3496 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003497 lpfc_nlp_init(vport, ndlp, did);
3498 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3499 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003500 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003501 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003502 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05003503 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3504 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
3505 if (!ndlp)
3506 return NULL;
3507 spin_lock_irq(shost->host_lock);
3508 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3509 spin_unlock_irq(shost->host_lock);
3510 return ndlp;
dea31012005-04-17 16:05:31 -05003511 }
James Smarte47c9092008-02-08 18:49:26 -05003512
James Smart58da1ff2008-04-07 10:15:56 -04003513 if ((vport->fc_flag & FC_RSCN_MODE) &&
3514 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003515 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04003516 /* If we've already recieved a PLOGI from this NPort
3517 * we don't need to try to discover it again.
3518 */
3519 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
3520 return NULL;
3521
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003522 /* Since this node is marked for discovery,
3523 * delay timeout is not needed.
3524 */
James Smart0d2b6b82008-06-14 22:52:47 -04003525 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04003526 spin_lock_irq(shost->host_lock);
3527 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3528 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04003529 } else
dea31012005-04-17 16:05:31 -05003530 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003531 } else {
James Smart87af33f2007-10-27 13:37:43 -04003532 /* If we've already recieved a PLOGI from this NPort,
3533 * or we are already in the process of discovery on it,
3534 * we don't need to try to discover it again.
3535 */
James Smart685f0bf2007-04-25 09:53:08 -04003536 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04003537 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3538 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05003539 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003540 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3541 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003542 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003543 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003544 }
3545 return ndlp;
3546}
3547
3548/* Build a list of nodes to discover based on the loopmap */
3549void
James Smart2e0fef82007-06-17 19:56:36 -05003550lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003551{
James Smart2e0fef82007-06-17 19:56:36 -05003552 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003553 int j;
3554 uint32_t alpa, index;
3555
James Smart2e0fef82007-06-17 19:56:36 -05003556 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003557 return;
James Smart2e0fef82007-06-17 19:56:36 -05003558
3559 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05003560 return;
dea31012005-04-17 16:05:31 -05003561
3562 /* Check for loop map present or not */
3563 if (phba->alpa_map[0]) {
3564 for (j = 1; j <= phba->alpa_map[0]; j++) {
3565 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05003566 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05003567 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003568 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003569 }
3570 } else {
3571 /* No alpamap, so try all alpa's */
3572 for (j = 0; j < FC_MAXLOOP; j++) {
3573 /* If cfg_scan_down is set, start from highest
3574 * ALPA (0xef) to lowest (0x1).
3575 */
James Smart3de2a652007-08-02 11:09:59 -04003576 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05003577 index = j;
3578 else
3579 index = FC_MAXLOOP - j - 1;
3580 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05003581 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05003582 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003583 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003584 }
3585 }
3586 return;
3587}
3588
dea31012005-04-17 16:05:31 -05003589void
James Smart2e0fef82007-06-17 19:56:36 -05003590lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003591{
dea31012005-04-17 16:05:31 -05003592 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05003593 struct lpfc_sli *psli = &phba->sli;
3594 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
3595 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
3596 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
3597 int rc;
3598
James Smart92d7f7b2007-06-17 19:56:38 -05003599 /*
3600 * if it's not a physical port or if we already send
3601 * clear_la then don't send it.
3602 */
3603 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04003604 (vport->port_type != LPFC_PHYSICAL_PORT) ||
3605 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05003606 return;
3607
James Smart2e0fef82007-06-17 19:56:36 -05003608 /* Link up discovery */
3609 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
3610 phba->link_state = LPFC_CLEAR_LA;
3611 lpfc_clear_la(phba, mbox);
3612 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
3613 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003614 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05003615 if (rc == MBX_NOT_FINISHED) {
3616 mempool_free(mbox, phba->mbox_mem_pool);
3617 lpfc_disc_flush_list(vport);
3618 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3619 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3620 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05003621 phba->link_state = LPFC_HBA_ERROR;
3622 }
3623 }
3624}
3625
3626/* Reg_vpi to tell firmware to resume normal operations */
3627void
3628lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
3629{
3630 LPFC_MBOXQ_t *regvpimbox;
3631
3632 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3633 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04003634 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003635 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
3636 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003637 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05003638 == MBX_NOT_FINISHED) {
3639 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003640 }
3641 }
3642}
3643
3644/* Start Link up / RSCN discovery on NPR nodes */
3645void
3646lpfc_disc_start(struct lpfc_vport *vport)
3647{
3648 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3649 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04003650 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05003651 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04003652 int did_changed;
dea31012005-04-17 16:05:31 -05003653
James Smart2e0fef82007-06-17 19:56:36 -05003654 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003655 return;
James Smart2e0fef82007-06-17 19:56:36 -05003656
3657 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05003658 clear_la_pending = 1;
3659 else
3660 clear_la_pending = 0;
3661
James Smart2e0fef82007-06-17 19:56:36 -05003662 if (vport->port_state < LPFC_VPORT_READY)
3663 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05003664
James Smart2e0fef82007-06-17 19:56:36 -05003665 lpfc_set_disctmo(vport);
3666
3667 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05003668 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003669 else
dea31012005-04-17 16:05:31 -05003670 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003671
3672 vport->fc_prevDID = vport->fc_myDID;
3673 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05003674
3675 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003676 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3677 "0202 Start Discovery hba state x%x "
3678 "Data: x%x x%x x%x\n",
3679 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
3680 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003681
3682 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003683 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05003684
3685 if (num_sent)
3686 return;
3687
James Smart92d7f7b2007-06-17 19:56:38 -05003688 /*
3689 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
3690 * continue discovery.
3691 */
3692 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05003693 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04003694 !(vport->fc_flag & FC_RSCN_MODE) &&
3695 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003696 lpfc_issue_reg_vpi(phba, vport);
3697 return;
3698 }
James Smart2e0fef82007-06-17 19:56:36 -05003699
James Smart92d7f7b2007-06-17 19:56:38 -05003700 /*
3701 * For SLI2, we need to set port_state to READY and continue
3702 * discovery.
3703 */
3704 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
3705 /* If we get here, there is nothing to ADISC */
3706 if (vport->port_type == LPFC_PHYSICAL_PORT)
3707 lpfc_issue_clear_la(phba, vport);
3708
3709 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05003710 vport->num_disc_nodes = 0;
3711 /* go thru NPR nodes and issue ELS PLOGIs */
3712 if (vport->fc_npr_cnt)
3713 lpfc_els_disc_plogi(vport);
3714
3715 if (!vport->num_disc_nodes) {
3716 spin_lock_irq(shost->host_lock);
3717 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3718 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003719 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05003720 }
3721 }
James Smart92d7f7b2007-06-17 19:56:38 -05003722 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05003723 } else {
3724 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003725 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05003726
3727 if (num_sent)
3728 return;
3729
James Smart2e0fef82007-06-17 19:56:36 -05003730 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05003731 /* Check to see if more RSCNs came in while we
3732 * were processing this one.
3733 */
James Smart2e0fef82007-06-17 19:56:36 -05003734 if ((vport->fc_rscn_id_cnt == 0) &&
3735 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
3736 spin_lock_irq(shost->host_lock);
3737 vport->fc_flag &= ~FC_RSCN_MODE;
3738 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003739 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003740 } else
James Smart2e0fef82007-06-17 19:56:36 -05003741 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05003742 }
3743 }
3744 return;
3745}
3746
3747/*
3748 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
3749 * ring the match the sppecified nodelist.
3750 */
3751static void
James Smart2e0fef82007-06-17 19:56:36 -05003752lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003753{
James Smart2534ba72007-04-25 09:52:20 -04003754 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003755 struct lpfc_sli *psli;
3756 IOCB_t *icmd;
3757 struct lpfc_iocbq *iocb, *next_iocb;
3758 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003759
3760 psli = &phba->sli;
3761 pring = &psli->ring[LPFC_ELS_RING];
3762
3763 /* Error matching iocb on txq or txcmplq
3764 * First check the txq.
3765 */
James Smart2e0fef82007-06-17 19:56:36 -05003766 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003767 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3768 if (iocb->context1 != ndlp) {
3769 continue;
3770 }
3771 icmd = &iocb->iocb;
3772 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
3773 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
3774
James Smart2534ba72007-04-25 09:52:20 -04003775 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05003776 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003777 }
3778 }
3779
3780 /* Next check the txcmplq */
3781 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
3782 if (iocb->context1 != ndlp) {
3783 continue;
3784 }
3785 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05003786 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
3787 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04003788 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05003789 }
3790 }
James Smart2e0fef82007-06-17 19:56:36 -05003791 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003792
James Smarta257bf92009-04-06 18:48:10 -04003793 /* Cancel all the IOCBs from the completions list */
3794 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3795 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003796}
3797
Adrian Bunka6ababd2007-11-05 18:07:33 +01003798static void
James Smart2e0fef82007-06-17 19:56:36 -05003799lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003800{
3801 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05003802 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003803
James Smart2e0fef82007-06-17 19:56:36 -05003804 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
3805 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04003806 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003807 if (!NLP_CHK_NODE_ACT(ndlp))
3808 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003809 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3810 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
3811 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04003812 }
dea31012005-04-17 16:05:31 -05003813 }
3814 }
dea31012005-04-17 16:05:31 -05003815}
3816
James Smart92d7f7b2007-06-17 19:56:38 -05003817void
3818lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
3819{
3820 lpfc_els_flush_rscn(vport);
3821 lpfc_els_flush_cmd(vport);
3822 lpfc_disc_flush_list(vport);
3823}
3824
dea31012005-04-17 16:05:31 -05003825/*****************************************************************************/
3826/*
3827 * NAME: lpfc_disc_timeout
3828 *
3829 * FUNCTION: Fibre Channel driver discovery timeout routine.
3830 *
3831 * EXECUTION ENVIRONMENT: interrupt only
3832 *
3833 * CALLED FROM:
3834 * Timer function
3835 *
3836 * RETURNS:
3837 * none
3838 */
3839/*****************************************************************************/
3840void
3841lpfc_disc_timeout(unsigned long ptr)
3842{
James Smart2e0fef82007-06-17 19:56:36 -05003843 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
3844 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04003845 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003846 unsigned long flags = 0;
3847
3848 if (unlikely(!phba))
3849 return;
3850
James Smart5e9d9b82008-06-14 22:52:53 -04003851 spin_lock_irqsave(&vport->work_port_lock, flags);
3852 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
3853 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05003854 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04003855 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05003856
James Smart5e9d9b82008-06-14 22:52:53 -04003857 if (!tmo_posted)
3858 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003859 return;
3860}
3861
3862static void
James Smart2e0fef82007-06-17 19:56:36 -05003863lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003864{
James Smart2e0fef82007-06-17 19:56:36 -05003865 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3866 struct lpfc_hba *phba = vport->phba;
3867 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003868 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05003869 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05003870 int rc, clrlaerr = 0;
3871
James Smart2e0fef82007-06-17 19:56:36 -05003872 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05003873 return;
3874
James Smart2e0fef82007-06-17 19:56:36 -05003875 spin_lock_irq(shost->host_lock);
3876 vport->fc_flag &= ~FC_DISC_TMO;
3877 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003878
James Smart858c9f62007-06-17 19:56:39 -05003879 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3880 "disc timeout: state:x%x rtry:x%x flg:x%x",
3881 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3882
James Smart2e0fef82007-06-17 19:56:36 -05003883 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05003884
3885 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05003886 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
3887 * FAN
3888 */
3889 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04003890 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3891 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003892 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05003893 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04003894 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003895 if (!NLP_CHK_NODE_ACT(ndlp))
3896 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003897 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3898 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003899 if (ndlp->nlp_type & NLP_FABRIC) {
3900 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05003901 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04003902
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003903 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003904 /* Fail outstanding IO now since device
3905 * is marked for PLOGI.
3906 */
James Smart2e0fef82007-06-17 19:56:36 -05003907 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003908 }
3909 }
James Smart92d7f7b2007-06-17 19:56:38 -05003910 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05003911 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05003912 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003913 }
dea31012005-04-17 16:05:31 -05003914 break;
3915
James Smart92d7f7b2007-06-17 19:56:38 -05003916 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05003917 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05003918 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05003919 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04003920 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3921 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04003922 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05003923
3924 /* Assume no Fabric and go on with discovery.
3925 * Check for outstanding ELS FLOGI to abort.
3926 */
3927
3928 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05003929 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003930
3931 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05003932 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05003933 break;
3934
3935 case LPFC_FABRIC_CFG_LINK:
3936 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
3937 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04003938 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3939 "0223 Timeout while waiting for "
3940 "NameServer login\n");
dea31012005-04-17 16:05:31 -05003941 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05003942 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05003943 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04003944 lpfc_els_abort(phba, ndlp);
3945
3946 /* ReStart discovery */
3947 goto restart_disc;
dea31012005-04-17 16:05:31 -05003948
3949 case LPFC_NS_QRY:
3950 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04003951 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3952 "0224 NameServer Query timeout "
3953 "Data: x%x x%x\n",
3954 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05003955
James Smart92d7f7b2007-06-17 19:56:38 -05003956 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
3957 /* Try it one more time */
3958 vport->fc_ns_retry++;
3959 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
3960 vport->fc_ns_retry, 0);
3961 if (rc == 0)
3962 break;
dea31012005-04-17 16:05:31 -05003963 }
James Smart92d7f7b2007-06-17 19:56:38 -05003964 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003965
James Smart87af33f2007-10-27 13:37:43 -04003966restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05003967 /*
3968 * Discovery is over.
3969 * set port_state to PORT_READY if SLI2.
3970 * cmpl_reg_vpi will set port_state to READY for SLI3.
3971 */
James Smart3772a992009-05-22 14:50:54 -04003972 if (phba->sli_rev < LPFC_SLI_REV4) {
3973 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3974 lpfc_issue_reg_vpi(phba, vport);
3975 else { /* NPIV Not enabled */
3976 lpfc_issue_clear_la(phba, vport);
3977 vport->port_state = LPFC_VPORT_READY;
3978 }
dea31012005-04-17 16:05:31 -05003979 }
3980
3981 /* Setup and issue mailbox INITIALIZE LINK command */
3982 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3983 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04003984 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3985 "0206 Device Discovery "
3986 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05003987 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003988 break;
3989 }
3990
3991 lpfc_linkdown(phba);
3992 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
3993 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04003994 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05003995 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003996 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003997 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04003998 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05003999 if (rc == MBX_NOT_FINISHED)
4000 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4001
4002 break;
4003
4004 case LPFC_DISC_AUTH:
4005 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004006 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4007 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004008 lpfc_disc_flush_list(vport);
4009
James Smart92d7f7b2007-06-17 19:56:38 -05004010 /*
4011 * set port_state to PORT_READY if SLI2.
4012 * cmpl_reg_vpi will set port_state to READY for SLI3.
4013 */
James Smart3772a992009-05-22 14:50:54 -04004014 if (phba->sli_rev < LPFC_SLI_REV4) {
4015 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4016 lpfc_issue_reg_vpi(phba, vport);
4017 else { /* NPIV Not enabled */
4018 lpfc_issue_clear_la(phba, vport);
4019 vport->port_state = LPFC_VPORT_READY;
4020 }
dea31012005-04-17 16:05:31 -05004021 }
4022 break;
4023
James Smart2e0fef82007-06-17 19:56:36 -05004024 case LPFC_VPORT_READY:
4025 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004026 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4027 "0231 RSCN timeout Data: x%x "
4028 "x%x\n",
4029 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004030
4031 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004032 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004033
James Smart2e0fef82007-06-17 19:56:36 -05004034 lpfc_els_flush_rscn(vport);
4035 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004036 }
4037 break;
James Smart2e0fef82007-06-17 19:56:36 -05004038
James Smart92d7f7b2007-06-17 19:56:38 -05004039 default:
James Smarte8b62012007-08-02 11:10:09 -04004040 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004041 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004042 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004043 break;
4044 }
4045
4046 switch (phba->link_state) {
4047 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004048 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004049 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4050 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004051 clrlaerr = 1;
4052 break;
4053
James Smart09372822008-01-11 01:52:54 -05004054 case LPFC_LINK_UP:
4055 lpfc_issue_clear_la(phba, vport);
4056 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004057 case LPFC_LINK_UNKNOWN:
4058 case LPFC_WARM_START:
4059 case LPFC_INIT_START:
4060 case LPFC_INIT_MBX_CMDS:
4061 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004062 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004063 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4064 "0230 Unexpected timeout, hba link "
4065 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004066 clrlaerr = 1;
4067 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004068
4069 case LPFC_HBA_READY:
4070 break;
dea31012005-04-17 16:05:31 -05004071 }
4072
4073 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004074 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004075 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004076 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4077 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004078 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004079 }
4080
4081 return;
4082}
4083
dea31012005-04-17 16:05:31 -05004084/*
4085 * This routine handles processing a NameServer REG_LOGIN mailbox
4086 * command upon completion. It is setup in the LPFC_MBOXQ
4087 * as the completion routine when the command is
4088 * handed off to the SLI layer.
4089 */
4090void
James Smart2e0fef82007-06-17 19:56:36 -05004091lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004092{
James Smart04c68492009-05-22 14:52:52 -04004093 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004094 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4095 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4096 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004097
4098 pmb->context1 = NULL;
4099
dea31012005-04-17 16:05:31 -05004100 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004101 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004102 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004103 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004104
James Smart2e0fef82007-06-17 19:56:36 -05004105 /*
4106 * Start issuing Fabric-Device Management Interface (FDMI) command to
4107 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4108 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004109 */
James Smart2e0fef82007-06-17 19:56:36 -05004110
James Smart3de2a652007-08-02 11:09:59 -04004111 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004112 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4113 else
4114 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004115
James Smartfa4066b2008-01-11 01:53:27 -05004116 /* decrement the node reference count held for this callback
4117 * function.
4118 */
James Smart329f9bc2007-04-25 09:53:01 -04004119 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004120 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4121 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004122 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004123
4124 return;
4125}
4126
James Smart685f0bf2007-04-25 09:53:08 -04004127static int
4128lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4129{
4130 uint16_t *rpi = param;
4131
4132 return ndlp->nlp_rpi == *rpi;
4133}
4134
4135static int
4136lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4137{
4138 return memcmp(&ndlp->nlp_portname, param,
4139 sizeof(ndlp->nlp_portname)) == 0;
4140}
4141
Adrian Bunka6ababd2007-11-05 18:07:33 +01004142static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004143__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05004144{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004145 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004146
James Smart2e0fef82007-06-17 19:56:36 -05004147 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04004148 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04004149 return ndlp;
4150 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04004151 return NULL;
dea31012005-04-17 16:05:31 -05004152}
4153
James Smart685f0bf2007-04-25 09:53:08 -04004154/*
4155 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05004156 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04004157 */
4158struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004159__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04004160{
James Smart2e0fef82007-06-17 19:56:36 -05004161 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04004162}
4163
James Smart488d1462006-03-07 15:02:37 -05004164/*
James Smart685f0bf2007-04-25 09:53:08 -04004165 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05004166 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05004167 */
4168struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004169lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05004170{
James Smart2e0fef82007-06-17 19:56:36 -05004171 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05004172 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05004173
James Smart2e0fef82007-06-17 19:56:36 -05004174 spin_lock_irq(shost->host_lock);
4175 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4176 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004177 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05004178}
4179
dea31012005-04-17 16:05:31 -05004180void
James Smart2e0fef82007-06-17 19:56:36 -05004181lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4182 uint32_t did)
dea31012005-04-17 16:05:31 -05004183{
4184 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05004185
4186 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04004187 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05004188
4189 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4190 "node init: did:x%x",
4191 ndlp->nlp_DID, 0, 0);
4192
dea31012005-04-17 16:05:31 -05004193 return;
4194}
James Smart329f9bc2007-04-25 09:53:01 -04004195
James Smart98c9ea52007-10-27 13:37:33 -04004196/* This routine releases all resources associated with a specifc NPort's ndlp
4197 * and mempool_free's the nodelist.
4198 */
James Smart311464e2007-08-02 11:10:37 -04004199static void
James Smart329f9bc2007-04-25 09:53:01 -04004200lpfc_nlp_release(struct kref *kref)
4201{
James Smarte47c9092008-02-08 18:49:26 -05004202 struct lpfc_hba *phba;
4203 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04004204 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4205 kref);
James Smart858c9f62007-06-17 19:56:39 -05004206
4207 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4208 "node release: did:x%x flg:x%x type:x%x",
4209 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4210
James Smarte47c9092008-02-08 18:49:26 -05004211 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4212 "0279 lpfc_nlp_release: ndlp:x%p "
4213 "usgmap:x%x refcnt:%d\n",
4214 (void *)ndlp, ndlp->nlp_usg_map,
4215 atomic_read(&ndlp->kref.refcount));
4216
4217 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05004218 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004219
4220 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04004221 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004222 spin_lock_irqsave(&phba->ndlp_lock, flags);
4223 NLP_CLR_NODE_ACT(ndlp);
4224 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4225
4226 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04004227 if (NLP_CHK_FREE_REQ(ndlp)) {
4228 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04004229 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04004230 }
James Smart329f9bc2007-04-25 09:53:01 -04004231}
4232
James Smart98c9ea52007-10-27 13:37:33 -04004233/* This routine bumps the reference count for a ndlp structure to ensure
4234 * that one discovery thread won't free a ndlp while another discovery thread
4235 * is using it.
4236 */
James Smart329f9bc2007-04-25 09:53:01 -04004237struct lpfc_nodelist *
4238lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4239{
James Smarte47c9092008-02-08 18:49:26 -05004240 struct lpfc_hba *phba;
4241 unsigned long flags;
4242
James Smart98c9ea52007-10-27 13:37:33 -04004243 if (ndlp) {
4244 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4245 "node get: did:x%x flg:x%x refcnt:x%x",
4246 ndlp->nlp_DID, ndlp->nlp_flag,
4247 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004248 /* The check of ndlp usage to prevent incrementing the
4249 * ndlp reference count that is in the process of being
4250 * released.
4251 */
James Smarta257bf92009-04-06 18:48:10 -04004252 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004253 spin_lock_irqsave(&phba->ndlp_lock, flags);
4254 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4255 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4256 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4257 "0276 lpfc_nlp_get: ndlp:x%p "
4258 "usgmap:x%x refcnt:%d\n",
4259 (void *)ndlp, ndlp->nlp_usg_map,
4260 atomic_read(&ndlp->kref.refcount));
4261 return NULL;
4262 } else
4263 kref_get(&ndlp->kref);
4264 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04004265 }
James Smart329f9bc2007-04-25 09:53:01 -04004266 return ndlp;
4267}
4268
James Smart98c9ea52007-10-27 13:37:33 -04004269/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05004270 * count goes to 0, this indicates the the associated nodelist should be
4271 * freed. Returning 1 indicates the ndlp resource has been released; on the
4272 * other hand, returning 0 indicates the ndlp resource has not been released
4273 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04004274 */
James Smart329f9bc2007-04-25 09:53:01 -04004275int
4276lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4277{
James Smarte47c9092008-02-08 18:49:26 -05004278 struct lpfc_hba *phba;
4279 unsigned long flags;
4280
4281 if (!ndlp)
4282 return 1;
4283
4284 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4285 "node put: did:x%x flg:x%x refcnt:x%x",
4286 ndlp->nlp_DID, ndlp->nlp_flag,
4287 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04004288 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05004289 spin_lock_irqsave(&phba->ndlp_lock, flags);
4290 /* Check the ndlp memory free acknowledge flag to avoid the
4291 * possible race condition that kref_put got invoked again
4292 * after previous one has done ndlp memory free.
4293 */
4294 if (NLP_CHK_FREE_ACK(ndlp)) {
4295 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4296 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4297 "0274 lpfc_nlp_put: ndlp:x%p "
4298 "usgmap:x%x refcnt:%d\n",
4299 (void *)ndlp, ndlp->nlp_usg_map,
4300 atomic_read(&ndlp->kref.refcount));
4301 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004302 }
James Smarte47c9092008-02-08 18:49:26 -05004303 /* Check the ndlp inactivate log flag to avoid the possible
4304 * race condition that kref_put got invoked again after ndlp
4305 * is already in inactivating state.
4306 */
4307 if (NLP_CHK_IACT_REQ(ndlp)) {
4308 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4309 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4310 "0275 lpfc_nlp_put: ndlp:x%p "
4311 "usgmap:x%x refcnt:%d\n",
4312 (void *)ndlp, ndlp->nlp_usg_map,
4313 atomic_read(&ndlp->kref.refcount));
4314 return 1;
4315 }
4316 /* For last put, mark the ndlp usage flags to make sure no
4317 * other kref_get and kref_put on the same ndlp shall get
4318 * in between the process when the final kref_put has been
4319 * invoked on this ndlp.
4320 */
4321 if (atomic_read(&ndlp->kref.refcount) == 1) {
4322 /* Indicate ndlp is put to inactive state. */
4323 NLP_SET_IACT_REQ(ndlp);
4324 /* Acknowledge ndlp memory free has been seen. */
4325 if (NLP_CHK_FREE_REQ(ndlp))
4326 NLP_SET_FREE_ACK(ndlp);
4327 }
4328 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4329 /* Note, the kref_put returns 1 when decrementing a reference
4330 * count that was 1, it invokes the release callback function,
4331 * but it still left the reference count as 1 (not actually
4332 * performs the last decrementation). Otherwise, it actually
4333 * decrements the reference count and returns 0.
4334 */
4335 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04004336}
James Smart98c9ea52007-10-27 13:37:33 -04004337
4338/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05004339 * by any other discovery thread. This routine returns 1 if the
4340 * ndlp has been freed. A return value of 0 indicates the ndlp is
4341 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04004342 */
4343int
4344lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4345{
4346 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4347 "node not used: did:x%x flg:x%x refcnt:x%x",
4348 ndlp->nlp_DID, ndlp->nlp_flag,
4349 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05004350 if (atomic_read(&ndlp->kref.refcount) == 1)
4351 if (lpfc_nlp_put(ndlp))
4352 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04004353 return 0;
4354}
James Smart6fb120a2009-05-22 14:52:59 -04004355
4356/**
4357 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4358 * @phba: Pointer to hba context object.
4359 *
4360 * This function iterate through all FC nodes associated
4361 * will all vports to check if there is any node with
4362 * fc_rports associated with it. If there is an fc_rport
4363 * associated with the node, then the node is either in
4364 * discovered state or its devloss_timer is pending.
4365 */
4366static int
4367lpfc_fcf_inuse(struct lpfc_hba *phba)
4368{
4369 struct lpfc_vport **vports;
4370 int i, ret = 0;
4371 struct lpfc_nodelist *ndlp;
4372 struct Scsi_Host *shost;
4373
4374 vports = lpfc_create_vport_work_array(phba);
4375
4376 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4377 shost = lpfc_shost_from_vport(vports[i]);
4378 spin_lock_irq(shost->host_lock);
4379 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4380 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4381 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4382 ret = 1;
4383 spin_unlock_irq(shost->host_lock);
4384 goto out;
James Smart891478a2009-11-18 15:40:23 -05004385 } else {
4386 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4387 "2624 RPI %x DID %x flg %x still "
4388 "logged in\n",
4389 ndlp->nlp_rpi, ndlp->nlp_DID,
4390 ndlp->nlp_flag);
4391 if (ndlp->nlp_flag & NLP_RPI_VALID)
4392 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04004393 }
4394 }
4395 spin_unlock_irq(shost->host_lock);
4396 }
4397out:
4398 lpfc_destroy_vport_work_array(phba, vports);
4399 return ret;
4400}
4401
4402/**
4403 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4404 * @phba: Pointer to hba context object.
4405 * @mboxq: Pointer to mailbox object.
4406 *
4407 * This function frees memory associated with the mailbox command.
4408 */
4409static void
4410lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4411{
4412 struct lpfc_vport *vport = mboxq->vport;
4413
4414 if (mboxq->u.mb.mbxStatus) {
4415 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4416 "2555 UNREG_VFI mbxStatus error x%x "
4417 "HBA state x%x\n",
4418 mboxq->u.mb.mbxStatus, vport->port_state);
4419 }
4420 mempool_free(mboxq, phba->mbox_mem_pool);
4421 return;
4422}
4423
4424/**
4425 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4426 * @phba: Pointer to hba context object.
4427 * @mboxq: Pointer to mailbox object.
4428 *
4429 * This function frees memory associated with the mailbox command.
4430 */
4431static void
4432lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4433{
4434 struct lpfc_vport *vport = mboxq->vport;
4435
4436 if (mboxq->u.mb.mbxStatus) {
4437 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4438 "2550 UNREG_FCFI mbxStatus error x%x "
4439 "HBA state x%x\n",
4440 mboxq->u.mb.mbxStatus, vport->port_state);
4441 }
4442 mempool_free(mboxq, phba->mbox_mem_pool);
4443 return;
4444}
4445
4446/**
4447 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
4448 * @phba: Pointer to hba context object.
4449 *
4450 * This function check if there are any connected remote port for the FCF and
4451 * if all the devices are disconnected, this function unregister FCFI.
4452 * This function also tries to use another FCF for discovery.
4453 */
4454void
4455lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
4456{
4457 LPFC_MBOXQ_t *mbox;
4458 int rc;
4459 struct lpfc_vport **vports;
4460 int i;
4461
4462 spin_lock_irq(&phba->hbalock);
4463 /*
4464 * If HBA is not running in FIP mode or
4465 * If HBA does not support FCoE or
4466 * If FCF is not registered.
4467 * do nothing.
4468 */
4469 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
4470 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smart45ed1192009-10-02 15:17:02 -04004471 (!(phba->hba_flag & HBA_FIP_SUPPORT))) {
James Smart6fb120a2009-05-22 14:52:59 -04004472 spin_unlock_irq(&phba->hbalock);
4473 return;
4474 }
4475 spin_unlock_irq(&phba->hbalock);
4476
4477 if (lpfc_fcf_inuse(phba))
4478 return;
4479
James Smart4d9ab992009-10-02 15:16:39 -04004480 /* At this point, all discovery is aborted */
4481 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04004482
4483 /* Unregister VPIs */
4484 vports = lpfc_create_vport_work_array(phba);
4485 if (vports &&
4486 (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
4487 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4488 lpfc_mbx_unreg_vpi(vports[i]);
James Smart891478a2009-11-18 15:40:23 -05004489 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05004490 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart6fb120a2009-05-22 14:52:59 -04004491 }
4492 lpfc_destroy_vport_work_array(phba, vports);
4493
4494 /* Unregister VFI */
4495 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4496 if (!mbox) {
4497 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4498 "2556 UNREG_VFI mbox allocation failed"
4499 "HBA state x%x\n",
4500 phba->pport->port_state);
4501 return;
4502 }
4503
James Smart6669f9b2009-10-02 15:16:45 -04004504 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04004505 mbox->vport = phba->pport;
4506 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
4507
4508 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4509 if (rc == MBX_NOT_FINISHED) {
4510 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4511 "2557 UNREG_VFI issue mbox failed rc x%x "
4512 "HBA state x%x\n",
4513 rc, phba->pport->port_state);
4514 mempool_free(mbox, phba->mbox_mem_pool);
4515 return;
4516 }
4517
4518 /* Unregister FCF */
4519 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4520 if (!mbox) {
4521 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4522 "2551 UNREG_FCFI mbox allocation failed"
4523 "HBA state x%x\n",
4524 phba->pport->port_state);
4525 return;
4526 }
4527
4528 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
4529 mbox->vport = phba->pport;
4530 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
4531 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4532
4533 if (rc == MBX_NOT_FINISHED) {
4534 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4535 "2552 UNREG_FCFI issue mbox failed rc x%x "
4536 "HBA state x%x\n",
4537 rc, phba->pport->port_state);
4538 mempool_free(mbox, phba->mbox_mem_pool);
4539 return;
4540 }
4541
4542 spin_lock_irq(&phba->hbalock);
4543 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_REGISTERED |
4544 FCF_DISCOVERED | FCF_BOOT_ENABLE | FCF_IN_USE |
4545 FCF_VALID_VLAN);
4546 spin_unlock_irq(&phba->hbalock);
4547
4548 /*
4549 * If driver is not unloading, check if there is any other
4550 * FCF record that can be used for discovery.
4551 */
4552 if ((phba->pport->load_flag & FC_UNLOADING) ||
4553 (phba->link_state < LPFC_LINK_UP))
4554 return;
4555
4556 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
4557
4558 if (rc)
4559 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4560 "2553 lpfc_unregister_unused_fcf failed to read FCF"
4561 " record HBA state x%x\n",
4562 phba->pport->port_state);
4563}
4564
4565/**
4566 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
4567 * @phba: Pointer to hba context object.
4568 * @buff: Buffer containing the FCF connection table as in the config
4569 * region.
4570 * This function create driver data structure for the FCF connection
4571 * record table read from config region 23.
4572 */
4573static void
4574lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
4575 uint8_t *buff)
4576{
4577 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4578 struct lpfc_fcf_conn_hdr *conn_hdr;
4579 struct lpfc_fcf_conn_rec *conn_rec;
4580 uint32_t record_count;
4581 int i;
4582
4583 /* Free the current connect table */
4584 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004585 &phba->fcf_conn_rec_list, list) {
4586 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04004587 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004588 }
James Smart6fb120a2009-05-22 14:52:59 -04004589
4590 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
4591 record_count = conn_hdr->length * sizeof(uint32_t)/
4592 sizeof(struct lpfc_fcf_conn_rec);
4593
4594 conn_rec = (struct lpfc_fcf_conn_rec *)
4595 (buff + sizeof(struct lpfc_fcf_conn_hdr));
4596
4597 for (i = 0; i < record_count; i++) {
4598 if (!(conn_rec[i].flags & FCFCNCT_VALID))
4599 continue;
4600 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
4601 GFP_KERNEL);
4602 if (!conn_entry) {
4603 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4604 "2566 Failed to allocate connection"
4605 " table entry\n");
4606 return;
4607 }
4608
4609 memcpy(&conn_entry->conn_rec, &conn_rec[i],
4610 sizeof(struct lpfc_fcf_conn_rec));
4611 conn_entry->conn_rec.vlan_tag =
4612 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
4613 conn_entry->conn_rec.flags =
4614 le16_to_cpu(conn_entry->conn_rec.flags);
4615 list_add_tail(&conn_entry->list,
4616 &phba->fcf_conn_rec_list);
4617 }
4618}
4619
4620/**
4621 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
4622 * @phba: Pointer to hba context object.
4623 * @buff: Buffer containing the FCoE parameter data structure.
4624 *
4625 * This function update driver data structure with config
4626 * parameters read from config region 23.
4627 */
4628static void
4629lpfc_read_fcoe_param(struct lpfc_hba *phba,
4630 uint8_t *buff)
4631{
4632 struct lpfc_fip_param_hdr *fcoe_param_hdr;
4633 struct lpfc_fcoe_params *fcoe_param;
4634
4635 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
4636 buff;
4637 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04004638 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04004639
4640 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
4641 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
4642 return;
4643
James Smart6fb120a2009-05-22 14:52:59 -04004644 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
4645 phba->valid_vlan = 1;
4646 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
4647 0xFFF;
4648 }
4649
4650 phba->fc_map[0] = fcoe_param->fc_map[0];
4651 phba->fc_map[1] = fcoe_param->fc_map[1];
4652 phba->fc_map[2] = fcoe_param->fc_map[2];
4653 return;
4654}
4655
4656/**
4657 * lpfc_get_rec_conf23 - Get a record type in config region data.
4658 * @buff: Buffer containing config region 23 data.
4659 * @size: Size of the data buffer.
4660 * @rec_type: Record type to be searched.
4661 *
4662 * This function searches config region data to find the begining
4663 * of the record specified by record_type. If record found, this
4664 * function return pointer to the record else return NULL.
4665 */
4666static uint8_t *
4667lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
4668{
4669 uint32_t offset = 0, rec_length;
4670
4671 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
4672 (size < sizeof(uint32_t)))
4673 return NULL;
4674
4675 rec_length = buff[offset + 1];
4676
4677 /*
4678 * One TLV record has one word header and number of data words
4679 * specified in the rec_length field of the record header.
4680 */
4681 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
4682 <= size) {
4683 if (buff[offset] == rec_type)
4684 return &buff[offset];
4685
4686 if (buff[offset] == LPFC_REGION23_LAST_REC)
4687 return NULL;
4688
4689 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
4690 rec_length = buff[offset + 1];
4691 }
4692 return NULL;
4693}
4694
4695/**
4696 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
4697 * @phba: Pointer to lpfc_hba data structure.
4698 * @buff: Buffer containing config region 23 data.
4699 * @size: Size of the data buffer.
4700 *
4701 * This fuction parse the FCoE config parameters in config region 23 and
4702 * populate driver data structure with the parameters.
4703 */
4704void
4705lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
4706 uint8_t *buff,
4707 uint32_t size)
4708{
4709 uint32_t offset = 0, rec_length;
4710 uint8_t *rec_ptr;
4711
4712 /*
4713 * If data size is less than 2 words signature and version cannot be
4714 * verified.
4715 */
4716 if (size < 2*sizeof(uint32_t))
4717 return;
4718
4719 /* Check the region signature first */
4720 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
4721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4722 "2567 Config region 23 has bad signature\n");
4723 return;
4724 }
4725
4726 offset += 4;
4727
4728 /* Check the data structure version */
4729 if (buff[offset] != LPFC_REGION23_VERSION) {
4730 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4731 "2568 Config region 23 has bad version\n");
4732 return;
4733 }
4734 offset += 4;
4735
4736 rec_length = buff[offset + 1];
4737
4738 /* Read FCoE param record */
4739 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4740 size - offset, FCOE_PARAM_TYPE);
4741 if (rec_ptr)
4742 lpfc_read_fcoe_param(phba, rec_ptr);
4743
4744 /* Read FCF connection table */
4745 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4746 size - offset, FCOE_CONN_TBL_TYPE);
4747 if (rec_ptr)
4748 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
4749
4750}