blob: 126323a4dceca1985f3c7642cf894d7702e04d44 [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);
dea31012005-04-17 16:05:31 -050064
James Smartc01f3202006-08-18 17:47:08 -040065void
66lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050067{
James Smartc01f3202006-08-18 17:47:08 -040068 struct lpfc_rport_data *rdata;
69 struct lpfc_nodelist * ndlp;
70 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050071
James Smartc01f3202006-08-18 17:47:08 -040072 rdata = rport->dd_data;
73 ndlp = rdata->pnode;
74
James Smart58da1ff2008-04-07 10:15:56 -040075 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040076 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
77 printk(KERN_ERR "Cannot find remote node"
78 " to terminate I/O Data x%x\n",
79 rport->port_id);
dea31012005-04-17 16:05:31 -050080 return;
81 }
82
James Smarta257bf92009-04-06 18:48:10 -040083 phba = ndlp->phba;
James Smart1a169682006-03-07 15:04:06 -050084
James Smart858c9f62007-06-17 19:56:39 -050085 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
86 "rport terminate: sid:x%x did:x%x flg:x%x",
87 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
88
James Smartc01f3202006-08-18 17:47:08 -040089 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040090 lpfc_sli_abort_iocb(ndlp->vport,
91 &phba->sli.ring[phba->sli.fcp_ring],
92 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040093 }
James Smartc01f3202006-08-18 17:47:08 -040094}
95
96/*
97 * This function will be called when dev_loss_tmo fire.
98 */
99void
100lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
101{
102 struct lpfc_rport_data *rdata;
103 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500104 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500105 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500106 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400107 int put_node;
108 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400109
110 rdata = rport->dd_data;
111 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400112 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400113 return;
James Smartc01f3202006-08-18 17:47:08 -0400114
James Smart858c9f62007-06-17 19:56:39 -0500115 vport = ndlp->vport;
116 phba = vport->phba;
117
118 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
119 "rport devlosscb: sid:x%x did:x%x flg:x%x",
120 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
121
James Smarta8adb832007-10-27 13:37:53 -0400122 /* Don't defer this if we are in the process of deleting the vport
123 * or unloading the driver. The unload will cleanup the node
124 * appropriately we just need to cleanup the ndlp rport info here.
125 */
126 if (vport->load_flag & FC_UNLOADING) {
127 put_node = rdata->pnode != NULL;
128 put_rport = ndlp->rport != NULL;
129 rdata->pnode = NULL;
130 ndlp->rport = NULL;
131 if (put_node)
132 lpfc_nlp_put(ndlp);
133 if (put_rport)
134 put_device(&rport->dev);
135 return;
136 }
137
138 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
139 return;
140
James Smart858c9f62007-06-17 19:56:39 -0500141 evtp = &ndlp->dev_loss_evt;
142
143 if (!list_empty(&evtp->evt_listp))
144 return;
145
146 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500147 /* We need to hold the node by incrementing the reference
148 * count until this queued work is done
149 */
150 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart5e9d9b82008-06-14 22:52:53 -0400151 if (evtp->evt_arg1) {
152 evtp->evt = LPFC_EVT_DEV_LOSS;
153 list_add_tail(&evtp->evt_listp, &phba->work_list);
154 lpfc_worker_wake_up(phba);
155 }
James Smart858c9f62007-06-17 19:56:39 -0500156 spin_unlock_irq(&phba->hbalock);
157
James Smart858c9f62007-06-17 19:56:39 -0500158 return;
159}
160
161/*
162 * This function is called from the worker thread when dev_loss_tmo
163 * expire.
164 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100165static void
James Smart858c9f62007-06-17 19:56:39 -0500166lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
167{
168 struct lpfc_rport_data *rdata;
169 struct fc_rport *rport;
170 struct lpfc_vport *vport;
171 struct lpfc_hba *phba;
172 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400173 int put_node;
174 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500175 int warn_on = 0;
176
177 rport = ndlp->rport;
178
179 if (!rport)
180 return;
181
182 rdata = rport->dd_data;
183 name = (uint8_t *) &ndlp->nlp_portname;
184 vport = ndlp->vport;
185 phba = vport->phba;
186
187 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
188 "rport devlosstmo:did:x%x type:x%x id:x%x",
189 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
190
James Smarta8adb832007-10-27 13:37:53 -0400191 /* Don't defer this if we are in the process of deleting the vport
192 * or unloading the driver. The unload will cleanup the node
193 * appropriately we just need to cleanup the ndlp rport info here.
194 */
195 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500196 if (ndlp->nlp_sid != NLP_NO_SID) {
197 /* flush the target */
198 lpfc_sli_abort_iocb(vport,
199 &phba->sli.ring[phba->sli.fcp_ring],
200 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
201 }
James Smarta8adb832007-10-27 13:37:53 -0400202 put_node = rdata->pnode != NULL;
203 put_rport = ndlp->rport != NULL;
204 rdata->pnode = NULL;
205 ndlp->rport = NULL;
206 if (put_node)
207 lpfc_nlp_put(ndlp);
208 if (put_rport)
209 put_device(&rport->dev);
210 return;
211 }
212
James Smartd7c255b2008-08-24 21:50:00 -0400213 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
214 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
215 "0284 Devloss timeout Ignored on "
216 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
217 "NPort x%x\n",
218 *name, *(name+1), *(name+2), *(name+3),
219 *(name+4), *(name+5), *(name+6), *(name+7),
220 ndlp->nlp_DID);
James Smart858c9f62007-06-17 19:56:39 -0500221 return;
James Smartd7c255b2008-08-24 21:50:00 -0400222 }
James Smart858c9f62007-06-17 19:56:39 -0500223
James Smart92d7f7b2007-06-17 19:56:38 -0500224 if (ndlp->nlp_type & NLP_FABRIC) {
225 /* We will clean up these Nodes in linkup */
226 put_node = rdata->pnode != NULL;
227 put_rport = ndlp->rport != NULL;
228 rdata->pnode = NULL;
229 ndlp->rport = NULL;
230 if (put_node)
231 lpfc_nlp_put(ndlp);
232 if (put_rport)
233 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400234 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500235 }
James Smart82085712007-04-25 09:52:41 -0400236
dea31012005-04-17 16:05:31 -0500237 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400238 warn_on = 1;
dea31012005-04-17 16:05:31 -0500239 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400240 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
241 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500242 }
James Smartc01f3202006-08-18 17:47:08 -0400243
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400244 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400245 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
246 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400247 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
248 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400249 *name, *(name+1), *(name+2), *(name+3),
250 *(name+4), *(name+5), *(name+6), *(name+7),
251 ndlp->nlp_DID, ndlp->nlp_flag,
252 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400253 } else {
James Smarte8b62012007-08-02 11:10:09 -0400254 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
255 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400256 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
257 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400258 *name, *(name+1), *(name+2), *(name+3),
259 *(name+4), *(name+5), *(name+6), *(name+7),
260 ndlp->nlp_DID, ndlp->nlp_flag,
261 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400262 }
263
James Smart87af33f2007-10-27 13:37:43 -0400264 put_node = rdata->pnode != NULL;
265 put_rport = ndlp->rport != NULL;
266 rdata->pnode = NULL;
267 ndlp->rport = NULL;
268 if (put_node)
269 lpfc_nlp_put(ndlp);
270 if (put_rport)
271 put_device(&rport->dev);
272
James Smart2e0fef82007-06-17 19:56:36 -0500273 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400274 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400275 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smarte47c9092008-02-08 18:49:26 -0500276 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
James Smart2e0fef82007-06-17 19:56:36 -0500277 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400278
279 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500280}
James Smartc01f3202006-08-18 17:47:08 -0400281
James Smartea2151b2008-09-07 11:52:10 -0400282/**
James Smart3621a712009-04-06 18:47:14 -0400283 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400284 * @phba: Pointer to hba context object.
285 *
286 * This function is called from the functions which need to post
287 * events from interrupt context. This function allocates data
288 * structure required for posting event. It also keeps track of
289 * number of events pending and prevent event storm when there are
290 * too many events.
291 **/
292struct lpfc_fast_path_event *
293lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
294 struct lpfc_fast_path_event *ret;
295
296 /* If there are lot of fast event do not exhaust memory due to this */
297 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
298 return NULL;
299
300 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
301 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400302 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400303 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400304 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
305 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
306 }
James Smartea2151b2008-09-07 11:52:10 -0400307 return ret;
308}
309
310/**
James Smart3621a712009-04-06 18:47:14 -0400311 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400312 * @phba: Pointer to hba context object.
313 * @evt: Event object which need to be freed.
314 *
315 * This function frees the data structure required for posting
316 * events.
317 **/
318void
319lpfc_free_fast_evt(struct lpfc_hba *phba,
320 struct lpfc_fast_path_event *evt) {
321
322 atomic_dec(&phba->fast_event_count);
323 kfree(evt);
324}
325
326/**
James Smart3621a712009-04-06 18:47:14 -0400327 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400328 * @phba: Pointer to hba context object.
329 * @evtp: Event data structure.
330 *
331 * This function is called from worker thread, when the interrupt
332 * context need to post an event. This function posts the event
333 * to fc transport netlink interface.
334 **/
335static void
336lpfc_send_fastpath_evt(struct lpfc_hba *phba,
337 struct lpfc_work_evt *evtp)
338{
339 unsigned long evt_category, evt_sub_category;
340 struct lpfc_fast_path_event *fast_evt_data;
341 char *evt_data;
342 uint32_t evt_data_size;
343 struct Scsi_Host *shost;
344
345 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
346 work_evt);
347
348 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
349 evt_sub_category = (unsigned long) fast_evt_data->un.
350 fabric_evt.subcategory;
351 shost = lpfc_shost_from_vport(fast_evt_data->vport);
352 if (evt_category == FC_REG_FABRIC_EVENT) {
353 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
354 evt_data = (char *) &fast_evt_data->un.read_check_error;
355 evt_data_size = sizeof(fast_evt_data->un.
356 read_check_error);
357 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500358 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400359 evt_data = (char *) &fast_evt_data->un.fabric_evt;
360 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
361 } else {
362 lpfc_free_fast_evt(phba, fast_evt_data);
363 return;
364 }
365 } else if (evt_category == FC_REG_SCSI_EVENT) {
366 switch (evt_sub_category) {
367 case LPFC_EVENT_QFULL:
368 case LPFC_EVENT_DEVBSY:
369 evt_data = (char *) &fast_evt_data->un.scsi_evt;
370 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
371 break;
372 case LPFC_EVENT_CHECK_COND:
373 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
374 evt_data_size = sizeof(fast_evt_data->un.
375 check_cond_evt);
376 break;
377 case LPFC_EVENT_VARQUEDEPTH:
378 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
379 evt_data_size = sizeof(fast_evt_data->un.
380 queue_depth_evt);
381 break;
382 default:
383 lpfc_free_fast_evt(phba, fast_evt_data);
384 return;
385 }
386 } else {
387 lpfc_free_fast_evt(phba, fast_evt_data);
388 return;
389 }
390
391 fc_host_post_vendor_event(shost,
392 fc_get_event_number(),
393 evt_data_size,
394 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500395 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400396
397 lpfc_free_fast_evt(phba, fast_evt_data);
398 return;
399}
400
dea31012005-04-17 16:05:31 -0500401static void
James Smart2e0fef82007-06-17 19:56:36 -0500402lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500403{
404 struct lpfc_work_evt *evtp = NULL;
405 struct lpfc_nodelist *ndlp;
406 int free_evt;
407
James Smart2e0fef82007-06-17 19:56:36 -0500408 spin_lock_irq(&phba->hbalock);
409 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500410 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
411 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500412 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500413 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500414 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500415 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500416 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500417 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500418 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500419 /* decrement the node reference count held
420 * for this queued work
421 */
422 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500423 break;
James Smart858c9f62007-06-17 19:56:39 -0500424 case LPFC_EVT_DEV_LOSS:
425 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500426 lpfc_dev_loss_tmo_handler(ndlp);
427 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500428 /* decrement the node reference count held for
429 * this queued work
430 */
James Smart858c9f62007-06-17 19:56:39 -0500431 lpfc_nlp_put(ndlp);
432 break;
dea31012005-04-17 16:05:31 -0500433 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500434 if (phba->link_state < LPFC_LINK_DOWN)
435 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500436 else
James Smart2e0fef82007-06-17 19:56:36 -0500437 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500438 complete((struct completion *)(evtp->evt_arg2));
439 break;
James Smart46fa3112007-04-25 09:51:45 -0400440 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500441 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400442 lpfc_offline_prep(phba);
443 *(int *)(evtp->evt_arg1) = 0;
444 complete((struct completion *)(evtp->evt_arg2));
445 break;
446 case LPFC_EVT_OFFLINE:
447 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500448 lpfc_sli_brdrestart(phba);
449 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400450 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
451 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500452 complete((struct completion *)(evtp->evt_arg2));
453 break;
454 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400455 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500456 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500457 lpfc_sli_brdreset(phba);
458 lpfc_hba_down_post(phba);
459 *(int *)(evtp->evt_arg1) =
460 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400461 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500462 complete((struct completion *)(evtp->evt_arg2));
463 break;
464 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400465 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500466 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500467 = (phba->pport->stopped)
468 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400469 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500470 complete((struct completion *)(evtp->evt_arg2));
471 break;
James Smartea2151b2008-09-07 11:52:10 -0400472 case LPFC_EVT_FASTPATH_MGMT_EVT:
473 lpfc_send_fastpath_evt(phba, evtp);
474 free_evt = 0;
475 break;
dea31012005-04-17 16:05:31 -0500476 }
477 if (free_evt)
478 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500479 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500480 }
James Smart2e0fef82007-06-17 19:56:36 -0500481 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500482
483}
484
James Smart311464e2007-08-02 11:10:37 -0400485static void
James Smart2e0fef82007-06-17 19:56:36 -0500486lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500487{
488 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500489 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400490 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400491 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400492 int i;
dea31012005-04-17 16:05:31 -0500493
James Smart2e0fef82007-06-17 19:56:36 -0500494 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500495 ha_copy = phba->work_ha;
496 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500497 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500498
James Smartda0436e2009-05-22 14:51:39 -0400499 /* First, try to post the next mailbox command to SLI4 device */
500 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
501 lpfc_sli4_post_async_mbox(phba);
502
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500503 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400504 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500505 lpfc_handle_eratt(phba);
506
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500507 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500508 lpfc_sli_handle_mb_event(phba);
509
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500510 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500511 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400512
James Smartda0436e2009-05-22 14:51:39 -0400513 /* Process SLI4 events */
514 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
515 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
516 lpfc_sli4_fcp_xri_abort_event_proc(phba);
517 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
518 lpfc_sli4_els_xri_abort_event_proc(phba);
519 if (phba->hba_flag & ASYNC_EVENT)
520 lpfc_sli4_async_event_proc(phba);
521 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
522 spin_lock_irq(&phba->hbalock);
523 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
524 spin_unlock_irq(&phba->hbalock);
525 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
526 }
527 if (phba->hba_flag & HBA_RECEIVE_BUFFER)
528 lpfc_sli4_handle_received_buffer(phba);
529 }
530
James Smart549e55c2007-08-02 11:09:51 -0400531 vports = lpfc_create_vport_work_array(phba);
532 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400533 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400534 /*
535 * We could have no vports in array if unloading, so if
536 * this happens then just use the pport
537 */
538 if (vports[i] == NULL && i == 0)
539 vport = phba->pport;
540 else
541 vport = vports[i];
542 if (vport == NULL)
543 break;
James Smart58da1ff2008-04-07 10:15:56 -0400544 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400545 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400546 vport->work_port_events &= ~work_port_events;
547 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400548 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400549 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400550 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400551 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400552 if (work_port_events & WORKER_HB_TMO)
553 lpfc_hb_timeout_handler(phba);
554 if (work_port_events & WORKER_MBOX_TMO)
555 lpfc_mbox_timeout_handler(phba);
556 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
557 lpfc_unblock_fabric_iocbs(phba);
558 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400559 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400560 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
561 lpfc_ramp_down_queue_handler(phba);
562 if (work_port_events & WORKER_RAMP_UP_QUEUE)
563 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500564 }
James Smart09372822008-01-11 01:52:54 -0500565 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500566
James Smart858c9f62007-06-17 19:56:39 -0500567 pring = &phba->sli.ring[LPFC_ELS_RING];
568 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
569 status >>= (4*LPFC_ELS_RING);
570 if ((status & HA_RXMASK)
571 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
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 */
690 if (!remove && ndlp->nlp_type & NLP_FABRIC)
691 continue;
692 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
693 remove
694 ? NLP_EVT_DEVICE_RM
695 : NLP_EVT_DEVICE_RECOVERY);
696 }
697 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
698 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500699 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500700 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500701 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500702 }
703}
704
James Smart87af33f2007-10-27 13:37:43 -0400705void
James Smart98c9ea52007-10-27 13:37:33 -0400706lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500707{
James Smart92d7f7b2007-06-17 19:56:38 -0500708 /* Cleanup any outstanding RSCN activity */
709 lpfc_els_flush_rscn(vport);
710
711 /* Cleanup any outstanding ELS commands */
712 lpfc_els_flush_cmd(vport);
713
714 lpfc_cleanup_rpis(vport, 0);
715
James Smart92d7f7b2007-06-17 19:56:38 -0500716 /* Turn off discovery timer if its running */
717 lpfc_can_disctmo(vport);
718}
719
James Smart3772a992009-05-22 14:50:54 -0400720void
James Smart98c9ea52007-10-27 13:37:33 -0400721lpfc_linkdown_port(struct lpfc_vport *vport)
722{
723 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
724
725 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
726
727 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
728 "Link Down: state:x%x rtry:x%x flg:x%x",
729 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
730
731 lpfc_port_link_failure(vport);
732
733}
734
dea31012005-04-17 16:05:31 -0500735int
James Smart685f0bf2007-04-25 09:53:08 -0400736lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500737{
James Smart2e0fef82007-06-17 19:56:36 -0500738 struct lpfc_vport *vport = phba->pport;
739 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400740 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400741 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400742 int i;
dea31012005-04-17 16:05:31 -0500743
James Smart3163f722008-02-08 18:50:25 -0500744 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500745 return 0;
James Smart2e0fef82007-06-17 19:56:36 -0500746 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -0400747 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_DISCOVERED);
James Smart92d7f7b2007-06-17 19:56:38 -0500748 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500749 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500750 phba->pport->fc_flag &= ~FC_LBIT;
751 }
James Smart2e0fef82007-06-17 19:56:36 -0500752 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400753 vports = lpfc_create_vport_work_array(phba);
754 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400755 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400756 /* Issue a LINK DOWN event to all nodes */
757 lpfc_linkdown_port(vports[i]);
758 }
James Smart09372822008-01-11 01:52:54 -0500759 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500760 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500761 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
762 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500763 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500764 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500765 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400766 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500767 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500768 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500769 }
770 }
771
dea31012005-04-17 16:05:31 -0500772 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500773 if (phba->pport->fc_flag & FC_PT2PT) {
774 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500775 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
776 if (mb) {
dea31012005-04-17 16:05:31 -0500777 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500778 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500779 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400780 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500781 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500782 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500783 }
784 }
James Smart2e0fef82007-06-17 19:56:36 -0500785 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500786 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500787 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500788 }
James Smart2e0fef82007-06-17 19:56:36 -0500789
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500790 return 0;
dea31012005-04-17 16:05:31 -0500791}
792
James Smart92d7f7b2007-06-17 19:56:38 -0500793static void
794lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500795{
James Smart92d7f7b2007-06-17 19:56:38 -0500796 struct lpfc_nodelist *ndlp;
797
798 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500799 if (!NLP_CHK_NODE_ACT(ndlp))
800 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500801 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
802 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500803 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500804 /* On Linkup its safe to clean up the ndlp
805 * from Fabric connections.
806 */
James Smart92d7f7b2007-06-17 19:56:38 -0500807 if (ndlp->nlp_DID != Fabric_DID)
808 lpfc_unreg_rpi(vport, ndlp);
809 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
810 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500811 /* Fail outstanding IO now since device is
812 * marked for PLOGI.
813 */
James Smart92d7f7b2007-06-17 19:56:38 -0500814 lpfc_unreg_rpi(vport, ndlp);
815 }
816 }
817}
818
819static void
820lpfc_linkup_port(struct lpfc_vport *vport)
821{
822 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500823 struct lpfc_hba *phba = vport->phba;
824
825 if ((vport->load_flag & FC_UNLOADING) != 0)
826 return;
827
James Smart858c9f62007-06-17 19:56:39 -0500828 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
829 "Link Up: top:x%x speed:x%x flg:x%x",
830 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
831
James Smart92d7f7b2007-06-17 19:56:38 -0500832 /* If NPIV is not enabled, only bring the physical port up */
833 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
834 (vport != phba->pport))
835 return;
dea31012005-04-17 16:05:31 -0500836
James Smart2e0fef82007-06-17 19:56:36 -0500837 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400838
James Smart2e0fef82007-06-17 19:56:36 -0500839 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500840 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
841 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
842 vport->fc_flag |= FC_NDISC_ACTIVE;
843 vport->fc_ns_retry = 0;
844 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500845
James Smart92d7f7b2007-06-17 19:56:38 -0500846 if (vport->fc_flag & FC_LBIT)
847 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500848
James Smart92d7f7b2007-06-17 19:56:38 -0500849}
850
851static int
852lpfc_linkup(struct lpfc_hba *phba)
853{
James Smart549e55c2007-08-02 11:09:51 -0400854 struct lpfc_vport **vports;
855 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500856
857 phba->link_state = LPFC_LINK_UP;
858
859 /* Unblock fabric iocbs if they are blocked */
860 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
861 del_timer_sync(&phba->fabric_block_timer);
862
James Smart549e55c2007-08-02 11:09:51 -0400863 vports = lpfc_create_vport_work_array(phba);
864 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400865 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400866 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500867 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400868 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
869 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500870 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500871
872 return 0;
873}
874
875/*
876 * This routine handles processing a CLEAR_LA mailbox
877 * command upon completion. It is setup in the LPFC_MBOXQ
878 * as the completion routine when the command is
879 * handed off to the SLI layer.
880 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100881static void
James Smart2e0fef82007-06-17 19:56:36 -0500882lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500883{
James Smart2e0fef82007-06-17 19:56:36 -0500884 struct lpfc_vport *vport = pmb->vport;
885 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
886 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400887 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500888 uint32_t control;
889
dea31012005-04-17 16:05:31 -0500890 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500891 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500892 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
893 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
894
895 /* Check for error */
896 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500897 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400898 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
899 "0320 CLEAR_LA mbxStatus error x%x hba "
900 "state x%x\n",
901 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500902 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500903 goto out;
904 }
905
James Smart92d7f7b2007-06-17 19:56:38 -0500906 if (vport->port_type == LPFC_PHYSICAL_PORT)
907 phba->link_state = LPFC_HBA_READY;
908
909 spin_lock_irq(&phba->hbalock);
910 psli->sli_flag |= LPFC_PROCESS_LA;
911 control = readl(phba->HCregaddr);
912 control |= HC_LAINT_ENA;
913 writel(control, phba->HCregaddr);
914 readl(phba->HCregaddr); /* flush */
915 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500916 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500917 return;
dea31012005-04-17 16:05:31 -0500918
dea31012005-04-17 16:05:31 -0500919out:
920 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400921 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
922 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500923 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500924
James Smart2e0fef82007-06-17 19:56:36 -0500925 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400926 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500927 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500928
James Smart2e0fef82007-06-17 19:56:36 -0500929 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500930
931 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500932
933 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500934 psli->sli_flag |= LPFC_PROCESS_LA;
935 control = readl(phba->HCregaddr);
936 control |= HC_LAINT_ENA;
937 writel(control, phba->HCregaddr);
938 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500939 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500940
941 return;
942}
943
James Smart2e0fef82007-06-17 19:56:36 -0500944
dea31012005-04-17 16:05:31 -0500945static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500946lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500947{
James Smart2e0fef82007-06-17 19:56:36 -0500948 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500949
James Smart04c68492009-05-22 14:52:52 -0400950 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500951 goto out;
dea31012005-04-17 16:05:31 -0500952
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500953 mempool_free(pmb, phba->mbox_mem_pool);
954
955 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500956 vport->fc_flag & FC_PUBLIC_LOOP &&
957 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500958 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500959 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500960 * LPFC_LOCAL_CFG_LINK while waiting for FAN
961 */
James Smart2e0fef82007-06-17 19:56:36 -0500962 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500963 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500964 }
dea31012005-04-17 16:05:31 -0500965
James Smart2e0fef82007-06-17 19:56:36 -0500966 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500967 * LPFC_FLOGI while waiting for FLOGI cmpl
968 */
James Smart92d7f7b2007-06-17 19:56:38 -0500969 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500970 lpfc_initial_flogi(vport);
971 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500972 return;
dea31012005-04-17 16:05:31 -0500973
974out:
James Smarte8b62012007-08-02 11:10:09 -0400975 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
976 "0306 CONFIG_LINK mbxStatus error x%x "
977 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400978 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500979 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500980
981 lpfc_linkdown(phba);
982
James Smarte8b62012007-08-02 11:10:09 -0400983 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
984 "0200 CONFIG_LINK bad hba state x%x\n",
985 vport->port_state);
dea31012005-04-17 16:05:31 -0500986
James Smart92d7f7b2007-06-17 19:56:38 -0500987 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500988 return;
989}
990
991static void
James Smart6fb120a2009-05-22 14:52:59 -0400992lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
993{
994 struct lpfc_vport *vport = mboxq->vport;
995 unsigned long flags;
996
997 if (mboxq->u.mb.mbxStatus) {
998 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
999 "2017 REG_FCFI mbxStatus error x%x "
1000 "HBA state x%x\n",
1001 mboxq->u.mb.mbxStatus, vport->port_state);
1002 mempool_free(mboxq, phba->mbox_mem_pool);
1003 return;
1004 }
1005
1006 /* Start FCoE discovery by sending a FLOGI. */
1007 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1008 /* Set the FCFI registered flag */
1009 spin_lock_irqsave(&phba->hbalock, flags);
1010 phba->fcf.fcf_flag |= FCF_REGISTERED;
1011 spin_unlock_irqrestore(&phba->hbalock, flags);
1012 if (vport->port_state != LPFC_FLOGI) {
1013 spin_lock_irqsave(&phba->hbalock, flags);
1014 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1015 spin_unlock_irqrestore(&phba->hbalock, flags);
1016 lpfc_initial_flogi(vport);
1017 }
1018
1019 mempool_free(mboxq, phba->mbox_mem_pool);
1020 return;
1021}
1022
1023/**
1024 * lpfc_fab_name_match - Check if the fcf fabric name match.
1025 * @fab_name: pointer to fabric name.
1026 * @new_fcf_record: pointer to fcf record.
1027 *
1028 * This routine compare the fcf record's fabric name with provided
1029 * fabric name. If the fabric name are identical this function
1030 * returns 1 else return 0.
1031 **/
1032static uint32_t
1033lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1034{
1035 if ((fab_name[0] ==
1036 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record)) &&
1037 (fab_name[1] ==
1038 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record)) &&
1039 (fab_name[2] ==
1040 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record)) &&
1041 (fab_name[3] ==
1042 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record)) &&
1043 (fab_name[4] ==
1044 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record)) &&
1045 (fab_name[5] ==
1046 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record)) &&
1047 (fab_name[6] ==
1048 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record)) &&
1049 (fab_name[7] ==
1050 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record)))
1051 return 1;
1052 else
1053 return 0;
1054}
1055
1056/**
1057 * lpfc_mac_addr_match - Check if the fcf mac address match.
1058 * @phba: pointer to lpfc hba data structure.
1059 * @new_fcf_record: pointer to fcf record.
1060 *
1061 * This routine compare the fcf record's mac address with HBA's
1062 * FCF mac address. If the mac addresses are identical this function
1063 * returns 1 else return 0.
1064 **/
1065static uint32_t
1066lpfc_mac_addr_match(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1067{
1068 if ((phba->fcf.mac_addr[0] ==
1069 bf_get(lpfc_fcf_record_mac_0, new_fcf_record)) &&
1070 (phba->fcf.mac_addr[1] ==
1071 bf_get(lpfc_fcf_record_mac_1, new_fcf_record)) &&
1072 (phba->fcf.mac_addr[2] ==
1073 bf_get(lpfc_fcf_record_mac_2, new_fcf_record)) &&
1074 (phba->fcf.mac_addr[3] ==
1075 bf_get(lpfc_fcf_record_mac_3, new_fcf_record)) &&
1076 (phba->fcf.mac_addr[4] ==
1077 bf_get(lpfc_fcf_record_mac_4, new_fcf_record)) &&
1078 (phba->fcf.mac_addr[5] ==
1079 bf_get(lpfc_fcf_record_mac_5, new_fcf_record)))
1080 return 1;
1081 else
1082 return 0;
1083}
1084
1085/**
1086 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1087 * @phba: pointer to lpfc hba data structure.
1088 * @new_fcf_record: pointer to fcf record.
1089 *
1090 * This routine copies the FCF information from the FCF
1091 * record to lpfc_hba data structure.
1092 **/
1093static void
1094lpfc_copy_fcf_record(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1095{
1096 phba->fcf.fabric_name[0] =
1097 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1098 phba->fcf.fabric_name[1] =
1099 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1100 phba->fcf.fabric_name[2] =
1101 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1102 phba->fcf.fabric_name[3] =
1103 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1104 phba->fcf.fabric_name[4] =
1105 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1106 phba->fcf.fabric_name[5] =
1107 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1108 phba->fcf.fabric_name[6] =
1109 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1110 phba->fcf.fabric_name[7] =
1111 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1112 phba->fcf.mac_addr[0] =
1113 bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1114 phba->fcf.mac_addr[1] =
1115 bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1116 phba->fcf.mac_addr[2] =
1117 bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1118 phba->fcf.mac_addr[3] =
1119 bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1120 phba->fcf.mac_addr[4] =
1121 bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1122 phba->fcf.mac_addr[5] =
1123 bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1124 phba->fcf.fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1125 phba->fcf.priority = new_fcf_record->fip_priority;
1126}
1127
1128/**
1129 * lpfc_register_fcf - Register the FCF with hba.
1130 * @phba: pointer to lpfc hba data structure.
1131 *
1132 * This routine issues a register fcfi mailbox command to register
1133 * the fcf with HBA.
1134 **/
1135static void
1136lpfc_register_fcf(struct lpfc_hba *phba)
1137{
1138 LPFC_MBOXQ_t *fcf_mbxq;
1139 int rc;
1140 unsigned long flags;
1141
1142 spin_lock_irqsave(&phba->hbalock, flags);
1143
1144 /* If the FCF is not availabe do nothing. */
1145 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
1146 spin_unlock_irqrestore(&phba->hbalock, flags);
1147 return;
1148 }
1149
1150 /* The FCF is already registered, start discovery */
1151 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1152 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1153 spin_unlock_irqrestore(&phba->hbalock, flags);
1154 if (phba->pport->port_state != LPFC_FLOGI)
1155 lpfc_initial_flogi(phba->pport);
1156 return;
1157 }
1158 spin_unlock_irqrestore(&phba->hbalock, flags);
1159
1160 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1161 GFP_KERNEL);
1162 if (!fcf_mbxq)
1163 return;
1164
1165 lpfc_reg_fcfi(phba, fcf_mbxq);
1166 fcf_mbxq->vport = phba->pport;
1167 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1168 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
1169 if (rc == MBX_NOT_FINISHED)
1170 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
1171
1172 return;
1173}
1174
1175/**
1176 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1177 * @phba: pointer to lpfc hba data structure.
1178 * @new_fcf_record: pointer to fcf record.
1179 * @boot_flag: Indicates if this record used by boot bios.
1180 * @addr_mode: The address mode to be used by this FCF
1181 *
1182 * This routine compare the fcf record with connect list obtained from the
1183 * config region to decide if this FCF can be used for SAN discovery. It returns
1184 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1185 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1186 * is used by boot bios and addr_mode will indicate the addressing mode to be
1187 * used for this FCF when the function returns.
1188 * If the FCF record need to be used with a particular vlan id, the vlan is
1189 * set in the vlan_id on return of the function. If not VLAN tagging need to
1190 * be used with the FCF vlan_id will be set to 0xFFFF;
1191 **/
1192static int
1193lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1194 struct fcf_record *new_fcf_record,
1195 uint32_t *boot_flag, uint32_t *addr_mode,
1196 uint16_t *vlan_id)
1197{
1198 struct lpfc_fcf_conn_entry *conn_entry;
1199
1200 if (!phba->cfg_enable_fip) {
1201 *boot_flag = 0;
1202 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1203 new_fcf_record);
1204 if (phba->valid_vlan)
1205 *vlan_id = phba->vlan_id;
1206 else
1207 *vlan_id = 0xFFFF;
1208 return 1;
1209 }
1210
1211 /*
1212 * If there are no FCF connection table entry, driver connect to all
1213 * FCFs.
1214 */
1215 if (list_empty(&phba->fcf_conn_rec_list)) {
1216 *boot_flag = 0;
1217 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1218 new_fcf_record);
1219 *vlan_id = 0xFFFF;
1220 return 1;
1221 }
1222
1223 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list, list) {
1224 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1225 continue;
1226
1227 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1228 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
1229 new_fcf_record))
1230 continue;
1231
1232 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1233 /*
1234 * If the vlan bit map does not have the bit set for the
1235 * vlan id to be used, then it is not a match.
1236 */
1237 if (!(new_fcf_record->vlan_bitmap
1238 [conn_entry->conn_rec.vlan_tag / 8] &
1239 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1240 continue;
1241 }
1242
1243 /*
1244 * Check if the connection record specifies a required
1245 * addressing mode.
1246 */
1247 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1248 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1249
1250 /*
1251 * If SPMA required but FCF not support this continue.
1252 */
1253 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1254 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1255 new_fcf_record) & LPFC_FCF_SPMA))
1256 continue;
1257
1258 /*
1259 * If FPMA required but FCF not support this continue.
1260 */
1261 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1262 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1263 new_fcf_record) & LPFC_FCF_FPMA))
1264 continue;
1265 }
1266
1267 /*
1268 * This fcf record matches filtering criteria.
1269 */
1270 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1271 *boot_flag = 1;
1272 else
1273 *boot_flag = 0;
1274
1275 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1276 new_fcf_record);
1277 /*
1278 * If the user specified a required address mode, assign that
1279 * address mode
1280 */
1281 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1282 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1283 *addr_mode = (conn_entry->conn_rec.flags &
1284 FCFCNCT_AM_SPMA) ?
1285 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1286 /*
1287 * If the user specified a prefered address mode, use the
1288 * addr mode only if FCF support the addr_mode.
1289 */
1290 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1291 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1292 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1293 (*addr_mode & LPFC_FCF_SPMA))
1294 *addr_mode = LPFC_FCF_SPMA;
1295 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1296 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1297 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1298 (*addr_mode & LPFC_FCF_FPMA))
1299 *addr_mode = LPFC_FCF_FPMA;
1300 /*
1301 * If user did not specify any addressing mode, use FPMA if
1302 * possible else use SPMA.
1303 */
1304 else if (*addr_mode & LPFC_FCF_FPMA)
1305 *addr_mode = LPFC_FCF_FPMA;
1306
1307 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1308 *vlan_id = conn_entry->conn_rec.vlan_tag;
1309 else
1310 *vlan_id = 0xFFFF;
1311
1312 return 1;
1313 }
1314
1315 return 0;
1316}
1317
1318/**
1319 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
1320 * @phba: pointer to lpfc hba data structure.
1321 * @mboxq: pointer to mailbox object.
1322 *
1323 * This function iterate through all the fcf records available in
1324 * HBA and choose the optimal FCF record for discovery. After finding
1325 * the FCF for discovery it register the FCF record and kick start
1326 * discovery.
1327 * If FCF_IN_USE flag is set in currently used FCF, the routine try to
1328 * use a FCF record which match fabric name and mac address of the
1329 * currently used FCF record.
1330 * If the driver support only one FCF, it will try to use the FCF record
1331 * used by BOOT_BIOS.
1332 */
1333void
1334lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1335{
1336 void *virt_addr;
1337 dma_addr_t phys_addr;
1338 uint8_t *bytep;
1339 struct lpfc_mbx_sge sge;
1340 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1341 uint32_t shdr_status, shdr_add_status;
1342 union lpfc_sli4_cfg_shdr *shdr;
1343 struct fcf_record *new_fcf_record;
1344 int rc;
1345 uint32_t boot_flag, addr_mode;
1346 uint32_t next_fcf_index;
1347 unsigned long flags;
1348 uint16_t vlan_id;
1349
1350 /* Get the first SGE entry from the non-embedded DMA memory. This
1351 * routine only uses a single SGE.
1352 */
1353 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1354 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1355 if (unlikely(!mboxq->sge_array)) {
1356 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1357 "2524 Failed to get the non-embedded SGE "
1358 "virtual address\n");
1359 goto out;
1360 }
1361 virt_addr = mboxq->sge_array->addr[0];
1362
1363 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1364 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1365 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
1366 &shdr->response);
1367 /*
1368 * The FCF Record was read and there is no reason for the driver
1369 * to maintain the FCF record data or memory. Instead, just need
1370 * to book keeping the FCFIs can be used.
1371 */
1372 if (shdr_status || shdr_add_status) {
1373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1374 "2521 READ_FCF_RECORD mailbox failed "
1375 "with status x%x add_status x%x, mbx\n",
1376 shdr_status, shdr_add_status);
1377 goto out;
1378 }
1379 /* Interpreting the returned information of FCF records */
1380 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1381 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1382 sizeof(struct lpfc_mbx_read_fcf_tbl));
1383 next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1384
1385 new_fcf_record = (struct fcf_record *)(virt_addr +
1386 sizeof(struct lpfc_mbx_read_fcf_tbl));
1387 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1388 sizeof(struct fcf_record));
1389 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1390
1391 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record,
1392 &boot_flag, &addr_mode,
1393 &vlan_id);
1394 /*
1395 * If the fcf record does not match with connect list entries
1396 * read the next entry.
1397 */
1398 if (!rc)
1399 goto read_next_fcf;
1400 /*
1401 * If this is not the first FCF discovery of the HBA, use last
1402 * FCF record for the discovery.
1403 */
1404 spin_lock_irqsave(&phba->hbalock, flags);
1405 if (phba->fcf.fcf_flag & FCF_IN_USE) {
1406 if (lpfc_fab_name_match(phba->fcf.fabric_name,
1407 new_fcf_record) &&
1408 lpfc_mac_addr_match(phba, new_fcf_record)) {
1409 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1410 spin_unlock_irqrestore(&phba->hbalock, flags);
1411 goto out;
1412 }
1413 spin_unlock_irqrestore(&phba->hbalock, flags);
1414 goto read_next_fcf;
1415 }
1416 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1417 /*
1418 * If the current FCF record does not have boot flag
1419 * set and new fcf record has boot flag set, use the
1420 * new fcf record.
1421 */
1422 if (boot_flag && !(phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1423 /* Use this FCF record */
1424 lpfc_copy_fcf_record(phba, new_fcf_record);
1425 phba->fcf.addr_mode = addr_mode;
1426 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1427 if (vlan_id != 0xFFFF) {
1428 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1429 phba->fcf.vlan_id = vlan_id;
1430 }
1431 spin_unlock_irqrestore(&phba->hbalock, flags);
1432 goto read_next_fcf;
1433 }
1434 /*
1435 * If the current FCF record has boot flag set and the
1436 * new FCF record does not have boot flag, read the next
1437 * FCF record.
1438 */
1439 if (!boot_flag && (phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1440 spin_unlock_irqrestore(&phba->hbalock, flags);
1441 goto read_next_fcf;
1442 }
1443 /*
1444 * If there is a record with lower priority value for
1445 * the current FCF, use that record.
1446 */
1447 if (lpfc_fab_name_match(phba->fcf.fabric_name, new_fcf_record)
1448 && (new_fcf_record->fip_priority <
1449 phba->fcf.priority)) {
1450 /* Use this FCF record */
1451 lpfc_copy_fcf_record(phba, new_fcf_record);
1452 phba->fcf.addr_mode = addr_mode;
1453 if (vlan_id != 0xFFFF) {
1454 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1455 phba->fcf.vlan_id = vlan_id;
1456 }
1457 spin_unlock_irqrestore(&phba->hbalock, flags);
1458 goto read_next_fcf;
1459 }
1460 spin_unlock_irqrestore(&phba->hbalock, flags);
1461 goto read_next_fcf;
1462 }
1463 /*
1464 * This is the first available FCF record, use this
1465 * record.
1466 */
1467 lpfc_copy_fcf_record(phba, new_fcf_record);
1468 phba->fcf.addr_mode = addr_mode;
1469 if (boot_flag)
1470 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1471 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1472 if (vlan_id != 0xFFFF) {
1473 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1474 phba->fcf.vlan_id = vlan_id;
1475 }
1476 spin_unlock_irqrestore(&phba->hbalock, flags);
1477 goto read_next_fcf;
1478
1479read_next_fcf:
1480 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1481 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0)
1482 lpfc_register_fcf(phba);
1483 else
1484 lpfc_sli4_read_fcf_record(phba, next_fcf_index);
1485 return;
1486
1487out:
1488 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1489 lpfc_register_fcf(phba);
1490
1491 return;
1492}
1493
1494/**
1495 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
1496 * @phba: pointer to lpfc hba data structure.
1497 *
1498 * This function loops through the list of vports on the @phba and issues an
1499 * FDISC if possible.
1500 */
1501void
1502lpfc_start_fdiscs(struct lpfc_hba *phba)
1503{
1504 struct lpfc_vport **vports;
1505 int i;
1506
1507 vports = lpfc_create_vport_work_array(phba);
1508 if (vports != NULL) {
1509 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1510 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1511 continue;
1512 /* There are no vpi for this vport */
1513 if (vports[i]->vpi > phba->max_vpi) {
1514 lpfc_vport_set_state(vports[i],
1515 FC_VPORT_FAILED);
1516 continue;
1517 }
1518 if (phba->fc_topology == TOPOLOGY_LOOP) {
1519 lpfc_vport_set_state(vports[i],
1520 FC_VPORT_LINKDOWN);
1521 continue;
1522 }
1523 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1524 lpfc_initial_fdisc(vports[i]);
1525 else {
1526 lpfc_vport_set_state(vports[i],
1527 FC_VPORT_NO_FABRIC_SUPP);
1528 lpfc_printf_vlog(vports[i], KERN_ERR,
1529 LOG_ELS,
1530 "0259 No NPIV "
1531 "Fabric support\n");
1532 }
1533 }
1534 }
1535 lpfc_destroy_vport_work_array(phba, vports);
1536}
1537
1538void
1539lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1540{
1541 struct lpfc_dmabuf *dmabuf = mboxq->context1;
1542 struct lpfc_vport *vport = mboxq->vport;
1543
1544 if (mboxq->u.mb.mbxStatus) {
1545 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1546 "2018 REG_VFI mbxStatus error x%x "
1547 "HBA state x%x\n",
1548 mboxq->u.mb.mbxStatus, vport->port_state);
1549 if (phba->fc_topology == TOPOLOGY_LOOP) {
1550 /* FLOGI failed, use loop map to make discovery list */
1551 lpfc_disc_list_loopmap(vport);
1552 /* Start discovery */
1553 lpfc_disc_start(vport);
1554 goto fail_free_mem;
1555 }
1556 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1557 goto fail_free_mem;
1558 }
1559 /* Mark the vport has registered with its VFI */
1560 vport->vfi_state |= LPFC_VFI_REGISTERED;
1561
1562 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1563 lpfc_start_fdiscs(phba);
1564 lpfc_do_scr_ns_plogi(phba, vport);
1565 }
1566
1567fail_free_mem:
1568 mempool_free(mboxq, phba->mbox_mem_pool);
1569 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1570 kfree(dmabuf);
1571 return;
1572}
1573
1574static void
James Smart2e0fef82007-06-17 19:56:36 -05001575lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001576{
James Smart6fb120a2009-05-22 14:52:59 -04001577 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001578 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05001579 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001580
1581
1582 /* Check for error */
1583 if (mb->mbxStatus) {
1584 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001585 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1586 "0319 READ_SPARAM mbxStatus error x%x "
1587 "hba state x%x>\n",
1588 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001589 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05001590 goto out;
1591 }
1592
James Smart2e0fef82007-06-17 19:56:36 -05001593 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05001594 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05001595 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05001596 u64_to_wwn(phba->cfg_soft_wwnn,
1597 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04001598 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05001599 u64_to_wwn(phba->cfg_soft_wwpn,
1600 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05001601 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1602 sizeof(vport->fc_nodename));
1603 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1604 sizeof(vport->fc_portname));
1605 if (vport->port_type == LPFC_PHYSICAL_PORT) {
1606 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1607 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1608 }
1609
dea31012005-04-17 16:05:31 -05001610 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1611 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001612 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001613 return;
1614
1615out:
1616 pmb->context1 = NULL;
1617 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1618 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05001619 lpfc_issue_clear_la(phba, vport);
1620 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001621 return;
1622}
1623
1624static void
James Smart92d7f7b2007-06-17 19:56:38 -05001625lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05001626{
James Smart92d7f7b2007-06-17 19:56:38 -05001627 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04001628 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05001629 int i;
James Smart14691152006-12-02 13:34:28 -05001630 struct lpfc_dmabuf *mp;
1631 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001632 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05001633
dea31012005-04-17 16:05:31 -05001634 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001635
James Smart92d7f7b2007-06-17 19:56:38 -05001636 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001637 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05001638 case LA_1GHZ_LINK:
1639 phba->fc_linkspeed = LA_1GHZ_LINK;
1640 break;
1641 case LA_2GHZ_LINK:
1642 phba->fc_linkspeed = LA_2GHZ_LINK;
1643 break;
1644 case LA_4GHZ_LINK:
1645 phba->fc_linkspeed = LA_4GHZ_LINK;
1646 break;
1647 case LA_8GHZ_LINK:
1648 phba->fc_linkspeed = LA_8GHZ_LINK;
1649 break;
James Smartf4b4c682009-05-22 14:53:12 -04001650 case LA_10GHZ_LINK:
1651 phba->fc_linkspeed = LA_10GHZ_LINK;
1652 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001653 default:
1654 phba->fc_linkspeed = LA_UNKNW_LINK;
1655 break;
dea31012005-04-17 16:05:31 -05001656 }
1657
1658 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05001659 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05001660
1661 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05001662 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05001663
James Smart495a7142008-06-14 22:52:59 -04001664 if (phba->cfg_enable_npiv)
1665 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1666 "1309 Link Up Event npiv not supported in loop "
1667 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001668 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05001669 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05001670 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001671
James Smart2e0fef82007-06-17 19:56:36 -05001672 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05001673 i = la->un.lilpBde64.tus.f.bdeSize;
1674
1675 if (i == 0) {
1676 phba->alpa_map[0] = 0;
1677 } else {
James Smarte8b62012007-08-02 11:10:09 -04001678 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05001679 int numalpa, j, k;
1680 union {
1681 uint8_t pamap[16];
1682 struct {
1683 uint32_t wd1;
1684 uint32_t wd2;
1685 uint32_t wd3;
1686 uint32_t wd4;
1687 } pa;
1688 } un;
1689 numalpa = phba->alpa_map[0];
1690 j = 0;
1691 while (j < numalpa) {
1692 memset(un.pamap, 0, 16);
1693 for (k = 1; j < numalpa; k++) {
1694 un.pamap[k - 1] =
1695 phba->alpa_map[j + 1];
1696 j++;
1697 if (k == 16)
1698 break;
1699 }
1700 /* Link Up Event ALPA map */
1701 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05001702 KERN_WARNING,
1703 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001704 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05001705 "ALPA map Data: x%x "
1706 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001707 un.pa.wd1, un.pa.wd2,
1708 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05001709 }
1710 }
1711 }
1712 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05001713 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04001714 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05001715 (phba->sli_rev == 3))
1716 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1717 }
James Smart2e0fef82007-06-17 19:56:36 -05001718 vport->fc_myDID = phba->fc_pref_DID;
1719 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001720 }
James Smart92d7f7b2007-06-17 19:56:38 -05001721 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001722
1723 lpfc_linkup(phba);
1724 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001725 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05001726 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05001727 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04001728 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05001729 if (rc == MBX_NOT_FINISHED) {
1730 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1731 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1732 kfree(mp);
1733 mempool_free(sparam_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001734 goto out;
James Smart14691152006-12-02 13:34:28 -05001735 }
dea31012005-04-17 16:05:31 -05001736 }
1737
James Smart6fb120a2009-05-22 14:52:59 -04001738 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
1739 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1740 if (!cfglink_mbox)
1741 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05001742 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05001743 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05001744 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001745 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04001746 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04001747 if (rc == MBX_NOT_FINISHED) {
1748 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
1749 goto out;
1750 }
1751 } else {
1752 /*
1753 * Add the driver's default FCF record at FCF index 0 now. This
1754 * is phase 1 implementation that support FCF index 0 and driver
1755 * defaults.
1756 */
1757 if (phba->cfg_enable_fip == 0) {
1758 fcf_record = kzalloc(sizeof(struct fcf_record),
1759 GFP_KERNEL);
1760 if (unlikely(!fcf_record)) {
1761 lpfc_printf_log(phba, KERN_ERR,
1762 LOG_MBOX | LOG_SLI,
1763 "2554 Could not allocate memmory for "
1764 "fcf record\n");
1765 rc = -ENODEV;
1766 goto out;
1767 }
1768
1769 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
1770 LPFC_FCOE_FCF_DEF_INDEX);
1771 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
1772 if (unlikely(rc)) {
1773 lpfc_printf_log(phba, KERN_ERR,
1774 LOG_MBOX | LOG_SLI,
1775 "2013 Could not manually add FCF "
1776 "record 0, status %d\n", rc);
1777 rc = -ENODEV;
1778 kfree(fcf_record);
1779 goto out;
1780 }
1781 kfree(fcf_record);
1782 }
1783 /*
1784 * The driver is expected to do FIP/FCF. Call the port
1785 * and get the FCF Table.
1786 */
1787 rc = lpfc_sli4_read_fcf_record(phba,
1788 LPFC_FCOE_FCF_GET_FIRST);
1789 if (rc)
1790 goto out;
dea31012005-04-17 16:05:31 -05001791 }
James Smart6fb120a2009-05-22 14:52:59 -04001792
1793 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001794out:
1795 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001796 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1797 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1798 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05001799 lpfc_issue_clear_la(phba, vport);
1800 return;
dea31012005-04-17 16:05:31 -05001801}
1802
1803static void
James Smart84774a42008-08-24 21:50:06 -04001804lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05001805{
dea31012005-04-17 16:05:31 -05001806 uint32_t control;
1807 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05001808 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001809 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04001810 if (phba->sli_rev <= LPFC_SLI_REV3) {
1811 control = readl(phba->HCregaddr);
1812 control |= HC_LAINT_ENA;
1813 writel(control, phba->HCregaddr);
1814 readl(phba->HCregaddr); /* flush */
1815 }
James Smart2e0fef82007-06-17 19:56:36 -05001816 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001817}
1818
James Smart84774a42008-08-24 21:50:06 -04001819static void
1820lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1821{
1822 lpfc_linkdown(phba);
1823 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04001824 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04001825 /* turn on Link Attention interrupts - no CLEAR_LA needed */
1826}
1827
1828
dea31012005-04-17 16:05:31 -05001829/*
1830 * This routine handles processing a READ_LA mailbox
1831 * command upon completion. It is setup in the LPFC_MBOXQ
1832 * as the completion routine when the command is
1833 * handed off to the SLI layer.
1834 */
1835void
James Smart2e0fef82007-06-17 19:56:36 -05001836lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001837{
James Smart2e0fef82007-06-17 19:56:36 -05001838 struct lpfc_vport *vport = pmb->vport;
1839 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05001840 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04001841 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001842 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1843
James Smart0d2b6b82008-06-14 22:52:47 -04001844 /* Unblock ELS traffic */
1845 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001846 /* Check for error */
1847 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05001848 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001849 "1307 READ_LA mbox error x%x state x%x\n",
1850 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001851 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001852 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001853 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1854 }
1855
James Smart04c68492009-05-22 14:52:52 -04001856 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05001857
1858 memcpy(&phba->alpa_map[0], mp->virt, 128);
1859
James Smart2e0fef82007-06-17 19:56:36 -05001860 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001861 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05001862 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001863 else
James Smart2e0fef82007-06-17 19:56:36 -05001864 vport->fc_flag &= ~FC_BYPASSED_MODE;
1865 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001866
dea31012005-04-17 16:05:31 -05001867 if (((phba->fc_eventTag + 1) < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05001868 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05001869 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05001870 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05001871 if (phba->fc_eventTag != 0)
1872 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001873 }
dea31012005-04-17 16:05:31 -05001874
1875 phba->fc_eventTag = la->eventTag;
James Smart84774a42008-08-24 21:50:06 -04001876 if (la->mm)
1877 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
1878 else
1879 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
dea31012005-04-17 16:05:31 -05001880
James Smart84774a42008-08-24 21:50:06 -04001881 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05001882 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05001883 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05001884 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001885 "1306 Link Up Event in loop back mode "
1886 "x%x received Data: x%x x%x x%x x%x\n",
1887 la->eventTag, phba->fc_eventTag,
1888 la->granted_AL_PA, la->UlnkSpeed,
1889 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001890 } else {
1891 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001892 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04001893 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001894 la->eventTag, phba->fc_eventTag,
1895 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04001896 phba->alpa_map[0],
1897 la->mm, la->fa,
1898 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04001899 }
James Smart92d7f7b2007-06-17 19:56:38 -05001900 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04001901 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05001902 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05001903 if (phba->link_flag & LS_LOOPBACK_MODE) {
1904 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1905 "1308 Link Down Event in loop back mode "
1906 "x%x received "
1907 "Data: x%x x%x x%x\n",
1908 la->eventTag, phba->fc_eventTag,
1909 phba->pport->port_state, vport->fc_flag);
1910 }
1911 else {
1912 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001913 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04001914 "Data: x%x x%x x%x x%x x%x\n",
1915 la->eventTag, phba->fc_eventTag,
1916 phba->pport->port_state, vport->fc_flag,
1917 la->mm, la->fa);
1918 }
1919 lpfc_mbx_issue_link_down(phba);
1920 }
1921 if (la->mm && la->attType == AT_LINK_UP) {
1922 if (phba->link_state != LPFC_LINK_DOWN) {
1923 phba->fc_stat.LinkDown++;
1924 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1925 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05001926 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001927 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05001928 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04001929 lpfc_mbx_issue_link_down(phba);
1930 } else
1931 lpfc_enable_la(phba);
1932
1933 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1934 "1310 Menlo Maint Mode Link up Event x%x rcvd "
1935 "Data: x%x x%x x%x\n",
1936 la->eventTag, phba->fc_eventTag,
1937 phba->pport->port_state, vport->fc_flag);
1938 /*
1939 * The cmnd that triggered this will be waiting for this
1940 * signal.
1941 */
1942 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
1943 if (phba->wait_4_mlo_maint_flg) {
1944 phba->wait_4_mlo_maint_flg = 0;
1945 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05001946 }
James Smart84774a42008-08-24 21:50:06 -04001947 }
1948
1949 if (la->fa) {
1950 if (la->mm)
1951 lpfc_issue_clear_la(phba, vport);
1952 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1953 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05001954 }
1955
1956lpfc_mbx_cmpl_read_la_free_mbuf:
1957 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1958 kfree(mp);
1959 mempool_free(pmb, phba->mbox_mem_pool);
1960 return;
1961}
1962
1963/*
1964 * This routine handles processing a REG_LOGIN mailbox
1965 * command upon completion. It is setup in the LPFC_MBOXQ
1966 * as the completion routine when the command is
1967 * handed off to the SLI layer.
1968 */
1969void
James Smart2e0fef82007-06-17 19:56:36 -05001970lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001971{
James Smart2e0fef82007-06-17 19:56:36 -05001972 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001973 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05001974 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001975
dea31012005-04-17 16:05:31 -05001976 pmb->context1 = NULL;
1977
1978 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05001979 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05001980 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1981 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001982 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05001983 /* decrement the node reference count held for this callback
1984 * function.
1985 */
James Smart329f9bc2007-04-25 09:53:01 -04001986 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001987
1988 return;
1989}
1990
James Smart92d7f7b2007-06-17 19:56:38 -05001991static void
1992lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1993{
James Smart04c68492009-05-22 14:52:52 -04001994 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05001995 struct lpfc_vport *vport = pmb->vport;
1996 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1997
1998 switch (mb->mbxStatus) {
1999 case 0x0011:
2000 case 0x0020:
2001 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04002002 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2003 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2004 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002005 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002006 }
2007 vport->unreg_vpi_cmpl = VPORT_OK;
2008 mempool_free(pmb, phba->mbox_mem_pool);
2009 /*
2010 * This shost reference might have been taken at the beginning of
2011 * lpfc_vport_delete()
2012 */
2013 if (vport->load_flag & FC_UNLOADING)
2014 scsi_host_put(shost);
2015}
2016
James Smartd7c255b2008-08-24 21:50:00 -04002017int
James Smart92d7f7b2007-06-17 19:56:38 -05002018lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2019{
2020 struct lpfc_hba *phba = vport->phba;
2021 LPFC_MBOXQ_t *mbox;
2022 int rc;
2023
2024 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2025 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04002026 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002027
2028 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2029 mbox->vport = vport;
2030 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04002031 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05002032 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002033 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2034 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002035 mempool_free(mbox, phba->mbox_mem_pool);
2036 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04002037 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002038 }
James Smartd7c255b2008-08-24 21:50:00 -04002039 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002040}
2041
2042static void
2043lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2044{
2045 struct lpfc_vport *vport = pmb->vport;
2046 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04002047 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05002048
2049 switch (mb->mbxStatus) {
2050 case 0x0011:
2051 case 0x9601:
2052 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04002053 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2054 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2055 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05002056 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2057 spin_lock_irq(shost->host_lock);
2058 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2059 spin_unlock_irq(shost->host_lock);
2060 vport->fc_myDID = 0;
2061 goto out;
2062 }
James Smart92d7f7b2007-06-17 19:56:38 -05002063
2064 vport->num_disc_nodes = 0;
2065 /* go thru NPR list and issue ELS PLOGIs */
2066 if (vport->fc_npr_cnt)
2067 lpfc_els_disc_plogi(vport);
2068
2069 if (!vport->num_disc_nodes) {
2070 spin_lock_irq(shost->host_lock);
2071 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2072 spin_unlock_irq(shost->host_lock);
2073 lpfc_can_disctmo(vport);
2074 }
2075 vport->port_state = LPFC_VPORT_READY;
2076
2077out:
2078 mempool_free(pmb, phba->mbox_mem_pool);
2079 return;
2080}
2081
dea31012005-04-17 16:05:31 -05002082/*
2083 * This routine handles processing a Fabric REG_LOGIN mailbox
2084 * command upon completion. It is setup in the LPFC_MBOXQ
2085 * as the completion routine when the command is
2086 * handed off to the SLI layer.
2087 */
2088void
James Smart2e0fef82007-06-17 19:56:36 -05002089lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002090{
James Smart92d7f7b2007-06-17 19:56:38 -05002091 struct lpfc_vport *vport = pmb->vport;
James Smart2e0fef82007-06-17 19:56:36 -05002092 MAILBOX_t *mb = &pmb->mb;
2093 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05002094 struct lpfc_nodelist *ndlp;
James Smart549e55c2007-08-02 11:09:51 -04002095 struct lpfc_vport **vports;
2096 int i;
dea31012005-04-17 16:05:31 -05002097
James Smart549e55c2007-08-02 11:09:51 -04002098 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04002099 pmb->context1 = NULL;
2100 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05002101 if (mb->mbxStatus) {
2102 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2103 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002104 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002105
James Smart92d7f7b2007-06-17 19:56:38 -05002106 if (phba->fc_topology == TOPOLOGY_LOOP) {
2107 /* FLOGI failed, use loop map to make discovery list */
2108 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002109
James Smart92d7f7b2007-06-17 19:56:38 -05002110 /* Start discovery */
2111 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05002112 /* Decrement the reference count to ndlp after the
2113 * reference to the ndlp are done.
2114 */
2115 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05002116 return;
2117 }
2118
2119 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002120 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2121 "0258 Register Fabric login error: 0x%x\n",
2122 mb->mbxStatus);
James Smarte47c9092008-02-08 18:49:26 -05002123 /* Decrement the reference count to ndlp after the reference
2124 * to the ndlp are done.
2125 */
2126 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002127 return;
2128 }
2129
dea31012005-04-17 16:05:31 -05002130 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05002131 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002132 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002133
James Smart2e0fef82007-06-17 19:56:36 -05002134 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart549e55c2007-08-02 11:09:51 -04002135 vports = lpfc_create_vport_work_array(phba);
2136 if (vports != NULL)
2137 for(i = 0;
James Smart09372822008-01-11 01:52:54 -05002138 i <= phba->max_vpi && vports[i] != NULL;
James Smart549e55c2007-08-02 11:09:51 -04002139 i++) {
2140 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2141 continue;
James Smart58da1ff2008-04-07 10:15:56 -04002142 if (phba->fc_topology == TOPOLOGY_LOOP) {
2143 lpfc_vport_set_state(vports[i],
2144 FC_VPORT_LINKDOWN);
2145 continue;
2146 }
James Smart549e55c2007-08-02 11:09:51 -04002147 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2148 lpfc_initial_fdisc(vports[i]);
James Smart58da1ff2008-04-07 10:15:56 -04002149 else {
James Smart549e55c2007-08-02 11:09:51 -04002150 lpfc_vport_set_state(vports[i],
2151 FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -04002152 lpfc_printf_vlog(vport, KERN_ERR,
2153 LOG_ELS,
2154 "0259 No NPIV "
2155 "Fabric support\n");
James Smart549e55c2007-08-02 11:09:51 -04002156 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002157 }
James Smart09372822008-01-11 01:52:54 -05002158 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002159 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05002160 }
2161
2162 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2163 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002164 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002165
2166 /* Drop the reference count from the mbox at the end after
2167 * all the current reference to the ndlp have been done.
2168 */
2169 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002170 return;
2171}
2172
2173/*
2174 * This routine handles processing a NameServer REG_LOGIN mailbox
2175 * command upon completion. It is setup in the LPFC_MBOXQ
2176 * as the completion routine when the command is
2177 * handed off to the SLI layer.
2178 */
2179void
James Smart2e0fef82007-06-17 19:56:36 -05002180lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002181{
James Smart2e0fef82007-06-17 19:56:36 -05002182 MAILBOX_t *mb = &pmb->mb;
2183 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2184 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2185 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002186
2187 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05002188out:
James Smartfa4066b2008-01-11 01:53:27 -05002189 /* decrement the node reference count held for this
2190 * callback function.
2191 */
James Smart329f9bc2007-04-25 09:53:01 -04002192 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002193 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2194 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04002195 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04002196
2197 /* If no other thread is using the ndlp, free it */
2198 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05002199
James Smart92d7f7b2007-06-17 19:56:38 -05002200 if (phba->fc_topology == TOPOLOGY_LOOP) {
2201 /*
2202 * RegLogin failed, use loop map to make discovery
2203 * list
2204 */
2205 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002206
James Smart92d7f7b2007-06-17 19:56:38 -05002207 /* Start discovery */
2208 lpfc_disc_start(vport);
2209 return;
2210 }
2211 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002212 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2213 "0260 Register NameServer error: 0x%x\n",
2214 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05002215 return;
2216 }
2217
2218 pmb->context1 = NULL;
2219
dea31012005-04-17 16:05:31 -05002220 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05002221 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002222 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002223
James Smart2e0fef82007-06-17 19:56:36 -05002224 if (vport->port_state < LPFC_VPORT_READY) {
2225 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05002226 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
2227 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
2228 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2229 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
2230 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
2231
2232 /* Issue SCR just before NameServer GID_FT Query */
2233 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05002234 }
2235
James Smart2e0fef82007-06-17 19:56:36 -05002236 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002237 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05002238 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05002239 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05002240 goto out;
dea31012005-04-17 16:05:31 -05002241 }
2242
James Smartfa4066b2008-01-11 01:53:27 -05002243 /* decrement the node reference count held for this
2244 * callback function.
2245 */
James Smart329f9bc2007-04-25 09:53:01 -04002246 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002247 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2248 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002249 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002250
2251 return;
2252}
2253
2254static void
James Smart2e0fef82007-06-17 19:56:36 -05002255lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002256{
James Smart2e0fef82007-06-17 19:56:36 -05002257 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2258 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05002259 struct lpfc_rport_data *rdata;
2260 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05002261 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002262
2263 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07002264 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2265 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05002266 rport_ids.port_id = ndlp->nlp_DID;
2267 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05002268
James Smart329f9bc2007-04-25 09:53:01 -04002269 /*
2270 * We leave our node pointer in rport->dd_data when we unregister a
2271 * FCP target port. But fc_remote_port_add zeros the space to which
2272 * rport->dd_data points. So, if we're reusing a previously
2273 * registered port, drop the reference that we took the last time we
2274 * registered the port.
2275 */
2276 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05002277 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04002278 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05002279
2280 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
2281 "rport add: did:x%x flg:x%x type x%x",
2282 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2283
James Smart2e0fef82007-06-17 19:56:36 -05002284 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04002285 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05002286 dev_printk(KERN_WARNING, &phba->pcidev->dev,
2287 "Warning: fc_remote_port_add failed\n");
2288 return;
2289 }
2290
2291 /* initialize static port data */
2292 rport->maxframe_size = ndlp->nlp_maxframe;
2293 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05002294 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04002295 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05002296
2297 if (ndlp->nlp_type & NLP_FCP_TARGET)
2298 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2299 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
2300 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2301
2302
2303 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
2304 fc_remote_port_rolechg(rport, rport_ids.roles);
2305
James Smart071fbd3d2006-04-15 11:53:20 -04002306 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002307 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04002308 ndlp->nlp_sid = rport->scsi_target_id;
2309 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002310 return;
2311}
2312
2313static void
James Smart2e0fef82007-06-17 19:56:36 -05002314lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002315{
2316 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04002317
James Smart858c9f62007-06-17 19:56:39 -05002318 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
2319 "rport delete: did:x%x flg:x%x type x%x",
2320 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2321
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002322 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05002323
2324 return;
2325}
2326
James Smartde0c5b32007-04-25 09:52:27 -04002327static void
James Smart2e0fef82007-06-17 19:56:36 -05002328lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04002329{
James Smart2e0fef82007-06-17 19:56:36 -05002330 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2331
2332 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002333 switch (state) {
2334 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002335 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002336 break;
2337 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002338 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002339 break;
2340 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002341 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002342 break;
2343 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002344 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002345 break;
2346 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05002347 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002348 break;
2349 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002350 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002351 break;
2352 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002353 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002354 break;
2355 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05002356 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04002357 break;
2358 }
James Smart2e0fef82007-06-17 19:56:36 -05002359 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002360}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002361
James Smartde0c5b32007-04-25 09:52:27 -04002362static void
James Smart2e0fef82007-06-17 19:56:36 -05002363lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04002364 int old_state, int new_state)
2365{
James Smart2e0fef82007-06-17 19:56:36 -05002366 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2367
James Smartde0c5b32007-04-25 09:52:27 -04002368 if (new_state == NLP_STE_UNMAPPED_NODE) {
2369 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2370 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2371 ndlp->nlp_type |= NLP_FC_NODE;
2372 }
2373 if (new_state == NLP_STE_MAPPED_NODE)
2374 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2375 if (new_state == NLP_STE_NPR_NODE)
2376 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05002377
James Smartde0c5b32007-04-25 09:52:27 -04002378 /* Transport interface */
2379 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
2380 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002381 vport->phba->nport_event_cnt++;
2382 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002383 }
dea31012005-04-17 16:05:31 -05002384
James Smartde0c5b32007-04-25 09:52:27 -04002385 if (new_state == NLP_STE_MAPPED_NODE ||
2386 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05002387 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05002388 /*
2389 * Tell the fc transport about the port, if we haven't
2390 * already. If we have, and it's a scsi entity, be
2391 * sure to unblock any attached scsi devices
2392 */
2393 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002394 }
James Smartea2151b2008-09-07 11:52:10 -04002395 if ((new_state == NLP_STE_MAPPED_NODE) &&
2396 (vport->stat_data_enabled)) {
2397 /*
2398 * A new target is discovered, if there is no buffer for
2399 * statistical data collection allocate buffer.
2400 */
2401 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
2402 sizeof(struct lpfc_scsicmd_bkt),
2403 GFP_KERNEL);
2404
2405 if (!ndlp->lat_data)
2406 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2407 "0286 lpfc_nlp_state_cleanup failed to "
2408 "allocate statistical data buffer DID "
2409 "0x%x\n", ndlp->nlp_DID);
2410 }
James Smart858c9f62007-06-17 19:56:39 -05002411 /*
2412 * if we added to Mapped list, but the remote port
2413 * registration failed or assigned a target id outside
2414 * our presentable range - move the node to the
2415 * Unmapped List
2416 */
James Smartde0c5b32007-04-25 09:52:27 -04002417 if (new_state == NLP_STE_MAPPED_NODE &&
2418 (!ndlp->rport ||
2419 ndlp->rport->scsi_target_id == -1 ||
2420 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05002421 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04002422 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05002423 spin_unlock_irq(shost->host_lock);
2424 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002425 }
James Smartde0c5b32007-04-25 09:52:27 -04002426}
2427
James Smart685f0bf2007-04-25 09:53:08 -04002428static char *
2429lpfc_nlp_state_name(char *buffer, size_t size, int state)
2430{
2431 static char *states[] = {
2432 [NLP_STE_UNUSED_NODE] = "UNUSED",
2433 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
2434 [NLP_STE_ADISC_ISSUE] = "ADISC",
2435 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
2436 [NLP_STE_PRLI_ISSUE] = "PRLI",
2437 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
2438 [NLP_STE_MAPPED_NODE] = "MAPPED",
2439 [NLP_STE_NPR_NODE] = "NPR",
2440 };
2441
James Smart311464e2007-08-02 11:10:37 -04002442 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04002443 strlcpy(buffer, states[state], size);
2444 else
2445 snprintf(buffer, size, "unknown (%d)", state);
2446 return buffer;
2447}
2448
James Smartde0c5b32007-04-25 09:52:27 -04002449void
James Smart2e0fef82007-06-17 19:56:36 -05002450lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2451 int state)
James Smartde0c5b32007-04-25 09:52:27 -04002452{
James Smart2e0fef82007-06-17 19:56:36 -05002453 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04002454 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04002455 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04002456
James Smarte8b62012007-08-02 11:10:09 -04002457 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2458 "0904 NPort state transition x%06x, %s -> %s\n",
2459 ndlp->nlp_DID,
2460 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
2461 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05002462
2463 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2464 "node statechg did:x%x old:%d ste:%d",
2465 ndlp->nlp_DID, old_state, state);
2466
James Smartde0c5b32007-04-25 09:52:27 -04002467 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04002468 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05002469 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002470 if (old_state == NLP_STE_UNMAPPED_NODE) {
2471 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
2472 ndlp->nlp_type &= ~NLP_FC_NODE;
2473 }
2474
James Smart685f0bf2007-04-25 09:53:08 -04002475 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05002476 spin_lock_irq(shost->host_lock);
2477 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2478 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002479 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05002480 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04002481
2482 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05002483 lpfc_nlp_counters(vport, state, 1);
2484 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04002485}
2486
2487void
James Smarte47c9092008-02-08 18:49:26 -05002488lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2489{
2490 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2491
2492 if (list_empty(&ndlp->nlp_listp)) {
2493 spin_lock_irq(shost->host_lock);
2494 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2495 spin_unlock_irq(shost->host_lock);
2496 }
2497}
2498
2499void
James Smart2e0fef82007-06-17 19:56:36 -05002500lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04002501{
James Smart2e0fef82007-06-17 19:56:36 -05002502 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2503
James Smart0d2b6b82008-06-14 22:52:47 -04002504 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04002505 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05002506 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2507 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002508 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05002509 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002510 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05002511 NLP_STE_UNUSED_NODE);
2512}
2513
Adrian Bunk4d9db012008-02-14 23:24:02 +02002514static void
James Smarte47c9092008-02-08 18:49:26 -05002515lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2516{
James Smart0d2b6b82008-06-14 22:52:47 -04002517 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05002518 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2519 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2520 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2521 NLP_STE_UNUSED_NODE);
2522}
James Smart109f6ed2008-12-04 22:39:08 -05002523/**
James Smart3621a712009-04-06 18:47:14 -04002524 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05002525 * @vport: Pointer to Virtual Port object.
2526 * @ndlp: Pointer to FC node object.
2527 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04002528 *
2529 * This function is always called when node object need to be initialized.
2530 * It initializes all the fields of the node object. Although the reference
2531 * to phba from @ndlp can be obtained indirectly through it's reference to
2532 * @vport, a direct reference to phba is taken here by @ndlp. This is due
2533 * to the life-span of the @ndlp might go beyond the existence of @vport as
2534 * the final release of ndlp is determined by its reference count. And, the
2535 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05002536 **/
2537static inline void
2538lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2539 uint32_t did)
2540{
2541 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2542 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2543 init_timer(&ndlp->nlp_delayfunc);
2544 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2545 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2546 ndlp->nlp_DID = did;
2547 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04002548 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05002549 ndlp->nlp_sid = NLP_NO_SID;
2550 kref_init(&ndlp->kref);
2551 NLP_INT_NODE_ACT(ndlp);
2552 atomic_set(&ndlp->cmd_pending, 0);
2553 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2554}
James Smarte47c9092008-02-08 18:49:26 -05002555
2556struct lpfc_nodelist *
2557lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2558 int state)
2559{
2560 struct lpfc_hba *phba = vport->phba;
2561 uint32_t did;
2562 unsigned long flags;
2563
2564 if (!ndlp)
2565 return NULL;
2566
2567 spin_lock_irqsave(&phba->ndlp_lock, flags);
2568 /* The ndlp should not be in memory free mode */
2569 if (NLP_CHK_FREE_REQ(ndlp)) {
2570 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2571 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2572 "0277 lpfc_enable_node: ndlp:x%p "
2573 "usgmap:x%x refcnt:%d\n",
2574 (void *)ndlp, ndlp->nlp_usg_map,
2575 atomic_read(&ndlp->kref.refcount));
2576 return NULL;
2577 }
2578 /* The ndlp should not already be in active mode */
2579 if (NLP_CHK_NODE_ACT(ndlp)) {
2580 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2581 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2582 "0278 lpfc_enable_node: ndlp:x%p "
2583 "usgmap:x%x refcnt:%d\n",
2584 (void *)ndlp, ndlp->nlp_usg_map,
2585 atomic_read(&ndlp->kref.refcount));
2586 return NULL;
2587 }
2588
2589 /* Keep the original DID */
2590 did = ndlp->nlp_DID;
2591
2592 /* re-initialize ndlp except of ndlp linked list pointer */
2593 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
2594 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05002595 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05002596
2597 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2598
2599 if (state != NLP_STE_UNUSED_NODE)
2600 lpfc_nlp_set_state(vport, ndlp, state);
2601
2602 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2603 "node enable: did:x%x",
2604 ndlp->nlp_DID, 0, 0);
2605 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04002606}
2607
2608void
James Smart2e0fef82007-06-17 19:56:36 -05002609lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04002610{
James Smart87af33f2007-10-27 13:37:43 -04002611 /*
James Smartfa4066b2008-01-11 01:53:27 -05002612 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04002613 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05002614 * the ndlp from the vport. The ndlp marked as UNUSED on the list
2615 * until ALL other outstanding threads have completed. We check
2616 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04002617 */
James Smartfa4066b2008-01-11 01:53:27 -05002618 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2619 return;
James Smart51ef4c22007-08-02 11:10:31 -04002620 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04002621 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04002622 return;
dea31012005-04-17 16:05:31 -05002623}
2624
2625/*
2626 * Start / ReStart rescue timer for Discovery / RSCN handling
2627 */
2628void
James Smart2e0fef82007-06-17 19:56:36 -05002629lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002630{
James Smart2e0fef82007-06-17 19:56:36 -05002631 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2632 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002633 uint32_t tmo;
2634
James Smart2e0fef82007-06-17 19:56:36 -05002635 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002636 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002637 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
2638 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002639 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002640 * FC spec states we need 3 * ratov for CT requests
2641 */
2642 tmo = ((phba->fc_ratov * 3) + 3);
2643 }
dea31012005-04-17 16:05:31 -05002644
James Smart858c9f62007-06-17 19:56:39 -05002645
2646 if (!timer_pending(&vport->fc_disctmo)) {
2647 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2648 "set disc timer: tmo:x%x state:x%x flg:x%x",
2649 tmo, vport->port_state, vport->fc_flag);
2650 }
2651
James Smart2e0fef82007-06-17 19:56:36 -05002652 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
2653 spin_lock_irq(shost->host_lock);
2654 vport->fc_flag |= FC_DISC_TMO;
2655 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002656
2657 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002658 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2659 "0247 Start Discovery Timer state x%x "
2660 "Data: x%x x%lx x%x x%x\n",
2661 vport->port_state, tmo,
2662 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
2663 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002664
2665 return;
2666}
2667
2668/*
2669 * Cancel rescue timer for Discovery / RSCN handling
2670 */
2671int
James Smart2e0fef82007-06-17 19:56:36 -05002672lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002673{
James Smart2e0fef82007-06-17 19:56:36 -05002674 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002675 unsigned long iflags;
2676
James Smart858c9f62007-06-17 19:56:39 -05002677 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2678 "can disc timer: state:x%x rtry:x%x flg:x%x",
2679 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2680
dea31012005-04-17 16:05:31 -05002681 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05002682 if (vport->fc_flag & FC_DISC_TMO) {
2683 spin_lock_irqsave(shost->host_lock, iflags);
2684 vport->fc_flag &= ~FC_DISC_TMO;
2685 spin_unlock_irqrestore(shost->host_lock, iflags);
2686 del_timer_sync(&vport->fc_disctmo);
2687 spin_lock_irqsave(&vport->work_port_lock, iflags);
2688 vport->work_port_events &= ~WORKER_DISC_TMO;
2689 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05002690 }
2691
2692 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002693 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2694 "0248 Cancel Discovery Timer state x%x "
2695 "Data: x%x x%x x%x\n",
2696 vport->port_state, vport->fc_flag,
2697 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002698 return 0;
dea31012005-04-17 16:05:31 -05002699}
2700
2701/*
2702 * Check specified ring for outstanding IOCB on the SLI queue
2703 * Return true if iocb matches the specified nport
2704 */
2705int
James Smart2e0fef82007-06-17 19:56:36 -05002706lpfc_check_sli_ndlp(struct lpfc_hba *phba,
2707 struct lpfc_sli_ring *pring,
2708 struct lpfc_iocbq *iocb,
2709 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002710{
James Smart2e0fef82007-06-17 19:56:36 -05002711 struct lpfc_sli *psli = &phba->sli;
2712 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05002713 struct lpfc_vport *vport = ndlp->vport;
2714
2715 if (iocb->vport != vport)
2716 return 0;
2717
dea31012005-04-17 16:05:31 -05002718 if (pring->ringno == LPFC_ELS_RING) {
2719 switch (icmd->ulpCommand) {
2720 case CMD_GEN_REQUEST64_CR:
2721 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002722 return 1;
dea31012005-04-17 16:05:31 -05002723 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04002724 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
2725 return 1;
dea31012005-04-17 16:05:31 -05002726 case CMD_XMIT_ELS_RSP64_CX:
2727 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002728 return 1;
dea31012005-04-17 16:05:31 -05002729 }
James Smarta4bc3372006-12-02 13:34:16 -05002730 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05002731
2732 } else if (pring->ringno == psli->fcp_ring) {
2733 /* Skip match check if waiting to relogin to FCP target */
2734 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002735 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002736 return 0;
dea31012005-04-17 16:05:31 -05002737 }
2738 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002739 return 1;
dea31012005-04-17 16:05:31 -05002740 }
2741 } else if (pring->ringno == psli->next_ring) {
2742
2743 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002744 return 0;
dea31012005-04-17 16:05:31 -05002745}
2746
2747/*
2748 * Free resources / clean up outstanding I/Os
2749 * associated with nlp_rpi in the LPFC_NODELIST entry.
2750 */
2751static int
James Smart2e0fef82007-06-17 19:56:36 -05002752lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002753{
James Smart2534ba72007-04-25 09:52:20 -04002754 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002755 struct lpfc_sli *psli;
2756 struct lpfc_sli_ring *pring;
2757 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05002758 uint32_t rpi, i;
2759
James Smart92d7f7b2007-06-17 19:56:38 -05002760 lpfc_fabric_abort_nport(ndlp);
2761
dea31012005-04-17 16:05:31 -05002762 /*
2763 * Everything that matches on txcmplq will be returned
2764 * by firmware with a no rpi error.
2765 */
2766 psli = &phba->sli;
2767 rpi = ndlp->nlp_rpi;
2768 if (rpi) {
2769 /* Now process each ring */
2770 for (i = 0; i < psli->num_rings; i++) {
2771 pring = &psli->ring[i];
2772
James Smart2e0fef82007-06-17 19:56:36 -05002773 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002774 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05002775 list) {
dea31012005-04-17 16:05:31 -05002776 /*
2777 * Check to see if iocb matches the nport we are
2778 * looking for
2779 */
James Smart92d7f7b2007-06-17 19:56:38 -05002780 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
2781 ndlp))) {
dea31012005-04-17 16:05:31 -05002782 /* It matches, so deque and call compl
2783 with an error */
James Smart2534ba72007-04-25 09:52:20 -04002784 list_move_tail(&iocb->list,
2785 &completions);
dea31012005-04-17 16:05:31 -05002786 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002787 }
2788 }
James Smart2e0fef82007-06-17 19:56:36 -05002789 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002790 }
2791 }
James Smart2534ba72007-04-25 09:52:20 -04002792
James Smarta257bf92009-04-06 18:48:10 -04002793 /* Cancel all the IOCBs from the completions list */
2794 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2795 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04002796
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002797 return 0;
dea31012005-04-17 16:05:31 -05002798}
2799
2800/*
2801 * Free rpi associated with LPFC_NODELIST entry.
2802 * This routine is called from lpfc_freenode(), when we are removing
2803 * a LPFC_NODELIST entry. It is also called if the driver initiates a
2804 * LOGO that completes successfully, and we are waiting to PLOGI back
2805 * to the remote NPort. In addition, it is called after we receive
2806 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
2807 * we are waiting to PLOGI back to the remote NPort.
2808 */
2809int
James Smart2e0fef82007-06-17 19:56:36 -05002810lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002811{
James Smart2e0fef82007-06-17 19:56:36 -05002812 struct lpfc_hba *phba = vport->phba;
2813 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05002814 int rc;
2815
2816 if (ndlp->nlp_rpi) {
James Smart2e0fef82007-06-17 19:56:36 -05002817 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2818 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05002819 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05002820 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002821 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002822 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05002823 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05002824 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002825 }
dea31012005-04-17 16:05:31 -05002826 lpfc_no_rpi(phba, ndlp);
2827 ndlp->nlp_rpi = 0;
2828 return 1;
2829 }
2830 return 0;
2831}
2832
James Smart92d7f7b2007-06-17 19:56:38 -05002833void
2834lpfc_unreg_all_rpis(struct lpfc_vport *vport)
2835{
2836 struct lpfc_hba *phba = vport->phba;
2837 LPFC_MBOXQ_t *mbox;
2838 int rc;
2839
2840 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2841 if (mbox) {
2842 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
2843 mbox->vport = vport;
2844 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002845 mbox->context1 = NULL;
2846 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04002847 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05002848 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04002849
2850 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
2851 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2852 "1836 Could not issue "
2853 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05002854 }
2855}
2856
2857void
2858lpfc_unreg_default_rpis(struct lpfc_vport *vport)
2859{
2860 struct lpfc_hba *phba = vport->phba;
2861 LPFC_MBOXQ_t *mbox;
2862 int rc;
2863
2864 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2865 if (mbox) {
2866 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
2867 mbox->vport = vport;
2868 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002869 mbox->context1 = NULL;
2870 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04002871 if (rc != MBX_TIMEOUT)
2872 mempool_free(mbox, phba->mbox_mem_pool);
2873
2874 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04002875 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2876 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04002877 "unreg_did (default rpis) status %d\n",
2878 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05002879 }
2880}
2881
dea31012005-04-17 16:05:31 -05002882/*
2883 * Free resources associated with LPFC_NODELIST entry
2884 * so it can be freed.
2885 */
2886static int
James Smart2e0fef82007-06-17 19:56:36 -05002887lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002888{
James Smart2e0fef82007-06-17 19:56:36 -05002889 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2890 struct lpfc_hba *phba = vport->phba;
2891 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05002892 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05002893
2894 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04002895 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2896 "0900 Cleanup node for NPort x%x "
2897 "Data: x%x x%x x%x\n",
2898 ndlp->nlp_DID, ndlp->nlp_flag,
2899 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05002900 if (NLP_CHK_FREE_REQ(ndlp)) {
2901 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2902 "0280 lpfc_cleanup_node: ndlp:x%p "
2903 "usgmap:x%x refcnt:%d\n",
2904 (void *)ndlp, ndlp->nlp_usg_map,
2905 atomic_read(&ndlp->kref.refcount));
2906 lpfc_dequeue_node(vport, ndlp);
2907 } else {
2908 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2909 "0281 lpfc_cleanup_node: ndlp:x%p "
2910 "usgmap:x%x refcnt:%d\n",
2911 (void *)ndlp, ndlp->nlp_usg_map,
2912 atomic_read(&ndlp->kref.refcount));
2913 lpfc_disable_node(vport, ndlp);
2914 }
dea31012005-04-17 16:05:31 -05002915
dea31012005-04-17 16:05:31 -05002916 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
2917 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04002918 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05002919 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
2920 mb->context2 = NULL;
2921 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2922 }
2923 }
James Smart33ccf8d2006-08-17 11:57:58 -04002924
James Smart2e0fef82007-06-17 19:56:36 -05002925 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002926 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04002927 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002928 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05002929 mp = (struct lpfc_dmabuf *) (mb->context1);
2930 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05002931 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002932 kfree(mp);
2933 }
2934 list_del(&mb->list);
2935 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002936 /* We shall not invoke the lpfc_nlp_put to decrement
2937 * the ndlp reference count as we are in the process
2938 * of lpfc_nlp_release.
2939 */
dea31012005-04-17 16:05:31 -05002940 }
2941 }
James Smart2e0fef82007-06-17 19:56:36 -05002942 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002943
James Smarte47c9092008-02-08 18:49:26 -05002944 lpfc_els_abort(phba, ndlp);
2945
James Smart2e0fef82007-06-17 19:56:36 -05002946 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04002947 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05002948 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002949
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05002950 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05002951 del_timer_sync(&ndlp->nlp_delayfunc);
2952
James Smart0d2b6b82008-06-14 22:52:47 -04002953 list_del_init(&ndlp->els_retry_evt.evt_listp);
2954 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002955
James Smart2e0fef82007-06-17 19:56:36 -05002956 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05002957
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002958 return 0;
dea31012005-04-17 16:05:31 -05002959}
2960
2961/*
2962 * Check to see if we can free the nlp back to the freelist.
2963 * If we are in the middle of using the nlp in the discovery state
2964 * machine, defer the free till we reach the end of the state machine.
2965 */
James Smart329f9bc2007-04-25 09:53:01 -04002966static void
James Smart2e0fef82007-06-17 19:56:36 -05002967lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002968{
James Smarta8adb832007-10-27 13:37:53 -04002969 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04002970 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04002971 LPFC_MBOXQ_t *mbox;
2972 int rc;
dea31012005-04-17 16:05:31 -05002973
James Smart0d2b6b82008-06-14 22:52:47 -04002974 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04002975 if (ndlp->nlp_flag & NLP_DEFER_RM && !ndlp->nlp_rpi) {
2976 /* For this case we need to cleanup the default rpi
2977 * allocated by the firmware.
2978 */
2979 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
2980 != NULL) {
2981 rc = lpfc_reg_login(phba, vport->vpi, ndlp->nlp_DID,
2982 (uint8_t *) &vport->fc_sparam, mbox, 0);
2983 if (rc) {
2984 mempool_free(mbox, phba->mbox_mem_pool);
2985 }
2986 else {
2987 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2988 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2989 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05002990 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04002991 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2992 if (rc == MBX_NOT_FINISHED) {
2993 mempool_free(mbox, phba->mbox_mem_pool);
2994 }
2995 }
2996 }
2997 }
James Smart2e0fef82007-06-17 19:56:36 -05002998 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04002999
James Smart2e0fef82007-06-17 19:56:36 -05003000 /*
James Smart92d7f7b2007-06-17 19:56:38 -05003001 * We can get here with a non-NULL ndlp->rport because when we
3002 * unregister a rport we don't break the rport/node linkage. So if we
3003 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05003004 */
James Smart92d7f7b2007-06-17 19:56:38 -05003005 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04003006 rdata = ndlp->rport->dd_data;
3007 rdata->pnode = NULL;
3008 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05003009 }
dea31012005-04-17 16:05:31 -05003010}
3011
3012static int
James Smart2e0fef82007-06-17 19:56:36 -05003013lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3014 uint32_t did)
dea31012005-04-17 16:05:31 -05003015{
James Smart2e0fef82007-06-17 19:56:36 -05003016 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05003017
3018 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003019 return 0;
dea31012005-04-17 16:05:31 -05003020
dea31012005-04-17 16:05:31 -05003021 /* First check for Direct match */
3022 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003023 return 1;
dea31012005-04-17 16:05:31 -05003024
3025 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05003026 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05003027 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003028 return 0;
dea31012005-04-17 16:05:31 -05003029 }
3030
3031 matchdid.un.word = did;
3032 ndlpdid.un.word = ndlp->nlp_DID;
3033 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3034 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3035 (mydid.un.b.area == matchdid.un.b.area)) {
3036 if ((ndlpdid.un.b.domain == 0) &&
3037 (ndlpdid.un.b.area == 0)) {
3038 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003039 return 1;
dea31012005-04-17 16:05:31 -05003040 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003041 return 0;
dea31012005-04-17 16:05:31 -05003042 }
3043
3044 matchdid.un.word = ndlp->nlp_DID;
3045 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3046 (mydid.un.b.area == ndlpdid.un.b.area)) {
3047 if ((matchdid.un.b.domain == 0) &&
3048 (matchdid.un.b.area == 0)) {
3049 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003050 return 1;
dea31012005-04-17 16:05:31 -05003051 }
3052 }
3053 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003054 return 0;
dea31012005-04-17 16:05:31 -05003055}
3056
James Smart685f0bf2007-04-25 09:53:08 -04003057/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05003058static struct lpfc_nodelist *
3059__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003060{
James Smart2fb9bd82006-12-02 13:33:57 -05003061 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003062 uint32_t data1;
3063
James Smart2e0fef82007-06-17 19:56:36 -05003064 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3065 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04003066 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3067 ((uint32_t) ndlp->nlp_xri << 16) |
3068 ((uint32_t) ndlp->nlp_type << 8) |
3069 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04003070 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3071 "0929 FIND node DID "
3072 "Data: x%p x%x x%x x%x\n",
3073 ndlp, ndlp->nlp_DID,
3074 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04003075 return ndlp;
dea31012005-04-17 16:05:31 -05003076 }
3077 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003078
dea31012005-04-17 16:05:31 -05003079 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04003080 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3081 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05003082 return NULL;
3083}
3084
3085struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003086lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05003087{
James Smart2e0fef82007-06-17 19:56:36 -05003088 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003089 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003090
James Smart2e0fef82007-06-17 19:56:36 -05003091 spin_lock_irq(shost->host_lock);
3092 ndlp = __lpfc_findnode_did(vport, did);
3093 spin_unlock_irq(shost->host_lock);
3094 return ndlp;
3095}
3096
3097struct lpfc_nodelist *
3098lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3099{
3100 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3101 struct lpfc_nodelist *ndlp;
3102
3103 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003104 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05003105 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
3106 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05003107 return NULL;
3108 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05003109 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003110 if (!ndlp)
3111 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003112 lpfc_nlp_init(vport, ndlp, did);
3113 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3114 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003115 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003116 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003117 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05003118 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3119 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
3120 if (!ndlp)
3121 return NULL;
3122 spin_lock_irq(shost->host_lock);
3123 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3124 spin_unlock_irq(shost->host_lock);
3125 return ndlp;
dea31012005-04-17 16:05:31 -05003126 }
James Smarte47c9092008-02-08 18:49:26 -05003127
James Smart58da1ff2008-04-07 10:15:56 -04003128 if ((vport->fc_flag & FC_RSCN_MODE) &&
3129 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003130 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04003131 /* If we've already recieved a PLOGI from this NPort
3132 * we don't need to try to discover it again.
3133 */
3134 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
3135 return NULL;
3136
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003137 /* Since this node is marked for discovery,
3138 * delay timeout is not needed.
3139 */
James Smart0d2b6b82008-06-14 22:52:47 -04003140 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04003141 spin_lock_irq(shost->host_lock);
3142 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3143 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04003144 } else
dea31012005-04-17 16:05:31 -05003145 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003146 } else {
James Smart87af33f2007-10-27 13:37:43 -04003147 /* If we've already recieved a PLOGI from this NPort,
3148 * or we are already in the process of discovery on it,
3149 * we don't need to try to discover it again.
3150 */
James Smart685f0bf2007-04-25 09:53:08 -04003151 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04003152 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3153 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05003154 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003155 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3156 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003157 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05003158 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003159 }
3160 return ndlp;
3161}
3162
3163/* Build a list of nodes to discover based on the loopmap */
3164void
James Smart2e0fef82007-06-17 19:56:36 -05003165lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003166{
James Smart2e0fef82007-06-17 19:56:36 -05003167 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003168 int j;
3169 uint32_t alpa, index;
3170
James Smart2e0fef82007-06-17 19:56:36 -05003171 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003172 return;
James Smart2e0fef82007-06-17 19:56:36 -05003173
3174 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05003175 return;
dea31012005-04-17 16:05:31 -05003176
3177 /* Check for loop map present or not */
3178 if (phba->alpa_map[0]) {
3179 for (j = 1; j <= phba->alpa_map[0]; j++) {
3180 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05003181 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05003182 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003183 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003184 }
3185 } else {
3186 /* No alpamap, so try all alpa's */
3187 for (j = 0; j < FC_MAXLOOP; j++) {
3188 /* If cfg_scan_down is set, start from highest
3189 * ALPA (0xef) to lowest (0x1).
3190 */
James Smart3de2a652007-08-02 11:09:59 -04003191 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05003192 index = j;
3193 else
3194 index = FC_MAXLOOP - j - 1;
3195 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05003196 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05003197 continue;
James Smart2e0fef82007-06-17 19:56:36 -05003198 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05003199 }
3200 }
3201 return;
3202}
3203
dea31012005-04-17 16:05:31 -05003204void
James Smart2e0fef82007-06-17 19:56:36 -05003205lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003206{
dea31012005-04-17 16:05:31 -05003207 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05003208 struct lpfc_sli *psli = &phba->sli;
3209 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
3210 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
3211 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
3212 int rc;
3213
James Smart92d7f7b2007-06-17 19:56:38 -05003214 /*
3215 * if it's not a physical port or if we already send
3216 * clear_la then don't send it.
3217 */
3218 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04003219 (vport->port_type != LPFC_PHYSICAL_PORT) ||
3220 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05003221 return;
3222
James Smart2e0fef82007-06-17 19:56:36 -05003223 /* Link up discovery */
3224 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
3225 phba->link_state = LPFC_CLEAR_LA;
3226 lpfc_clear_la(phba, mbox);
3227 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
3228 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003229 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05003230 if (rc == MBX_NOT_FINISHED) {
3231 mempool_free(mbox, phba->mbox_mem_pool);
3232 lpfc_disc_flush_list(vport);
3233 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3234 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3235 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05003236 phba->link_state = LPFC_HBA_ERROR;
3237 }
3238 }
3239}
3240
3241/* Reg_vpi to tell firmware to resume normal operations */
3242void
3243lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
3244{
3245 LPFC_MBOXQ_t *regvpimbox;
3246
3247 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3248 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04003249 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003250 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
3251 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04003252 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05003253 == MBX_NOT_FINISHED) {
3254 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003255 }
3256 }
3257}
3258
3259/* Start Link up / RSCN discovery on NPR nodes */
3260void
3261lpfc_disc_start(struct lpfc_vport *vport)
3262{
3263 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3264 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04003265 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05003266 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04003267 int did_changed;
dea31012005-04-17 16:05:31 -05003268
James Smart2e0fef82007-06-17 19:56:36 -05003269 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003270 return;
James Smart2e0fef82007-06-17 19:56:36 -05003271
3272 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05003273 clear_la_pending = 1;
3274 else
3275 clear_la_pending = 0;
3276
James Smart2e0fef82007-06-17 19:56:36 -05003277 if (vport->port_state < LPFC_VPORT_READY)
3278 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05003279
James Smart2e0fef82007-06-17 19:56:36 -05003280 lpfc_set_disctmo(vport);
3281
3282 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05003283 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003284 else
dea31012005-04-17 16:05:31 -05003285 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003286
3287 vport->fc_prevDID = vport->fc_myDID;
3288 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05003289
3290 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003291 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3292 "0202 Start Discovery hba state x%x "
3293 "Data: x%x x%x x%x\n",
3294 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
3295 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003296
3297 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003298 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05003299
3300 if (num_sent)
3301 return;
3302
James Smart92d7f7b2007-06-17 19:56:38 -05003303 /*
3304 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
3305 * continue discovery.
3306 */
3307 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05003308 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04003309 !(vport->fc_flag & FC_RSCN_MODE) &&
3310 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003311 lpfc_issue_reg_vpi(phba, vport);
3312 return;
3313 }
James Smart2e0fef82007-06-17 19:56:36 -05003314
James Smart92d7f7b2007-06-17 19:56:38 -05003315 /*
3316 * For SLI2, we need to set port_state to READY and continue
3317 * discovery.
3318 */
3319 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
3320 /* If we get here, there is nothing to ADISC */
3321 if (vport->port_type == LPFC_PHYSICAL_PORT)
3322 lpfc_issue_clear_la(phba, vport);
3323
3324 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05003325 vport->num_disc_nodes = 0;
3326 /* go thru NPR nodes and issue ELS PLOGIs */
3327 if (vport->fc_npr_cnt)
3328 lpfc_els_disc_plogi(vport);
3329
3330 if (!vport->num_disc_nodes) {
3331 spin_lock_irq(shost->host_lock);
3332 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3333 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003334 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05003335 }
3336 }
James Smart92d7f7b2007-06-17 19:56:38 -05003337 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05003338 } else {
3339 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05003340 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05003341
3342 if (num_sent)
3343 return;
3344
James Smart2e0fef82007-06-17 19:56:36 -05003345 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05003346 /* Check to see if more RSCNs came in while we
3347 * were processing this one.
3348 */
James Smart2e0fef82007-06-17 19:56:36 -05003349 if ((vport->fc_rscn_id_cnt == 0) &&
3350 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
3351 spin_lock_irq(shost->host_lock);
3352 vport->fc_flag &= ~FC_RSCN_MODE;
3353 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003354 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003355 } else
James Smart2e0fef82007-06-17 19:56:36 -05003356 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05003357 }
3358 }
3359 return;
3360}
3361
3362/*
3363 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
3364 * ring the match the sppecified nodelist.
3365 */
3366static void
James Smart2e0fef82007-06-17 19:56:36 -05003367lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003368{
James Smart2534ba72007-04-25 09:52:20 -04003369 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003370 struct lpfc_sli *psli;
3371 IOCB_t *icmd;
3372 struct lpfc_iocbq *iocb, *next_iocb;
3373 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003374
3375 psli = &phba->sli;
3376 pring = &psli->ring[LPFC_ELS_RING];
3377
3378 /* Error matching iocb on txq or txcmplq
3379 * First check the txq.
3380 */
James Smart2e0fef82007-06-17 19:56:36 -05003381 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003382 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3383 if (iocb->context1 != ndlp) {
3384 continue;
3385 }
3386 icmd = &iocb->iocb;
3387 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
3388 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
3389
James Smart2534ba72007-04-25 09:52:20 -04003390 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05003391 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003392 }
3393 }
3394
3395 /* Next check the txcmplq */
3396 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
3397 if (iocb->context1 != ndlp) {
3398 continue;
3399 }
3400 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05003401 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
3402 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04003403 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05003404 }
3405 }
James Smart2e0fef82007-06-17 19:56:36 -05003406 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003407
James Smarta257bf92009-04-06 18:48:10 -04003408 /* Cancel all the IOCBs from the completions list */
3409 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3410 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003411}
3412
Adrian Bunka6ababd2007-11-05 18:07:33 +01003413static void
James Smart2e0fef82007-06-17 19:56:36 -05003414lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003415{
3416 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05003417 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003418
James Smart2e0fef82007-06-17 19:56:36 -05003419 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
3420 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04003421 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003422 if (!NLP_CHK_NODE_ACT(ndlp))
3423 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003424 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3425 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
3426 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04003427 }
dea31012005-04-17 16:05:31 -05003428 }
3429 }
dea31012005-04-17 16:05:31 -05003430}
3431
James Smart92d7f7b2007-06-17 19:56:38 -05003432void
3433lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
3434{
3435 lpfc_els_flush_rscn(vport);
3436 lpfc_els_flush_cmd(vport);
3437 lpfc_disc_flush_list(vport);
3438}
3439
dea31012005-04-17 16:05:31 -05003440/*****************************************************************************/
3441/*
3442 * NAME: lpfc_disc_timeout
3443 *
3444 * FUNCTION: Fibre Channel driver discovery timeout routine.
3445 *
3446 * EXECUTION ENVIRONMENT: interrupt only
3447 *
3448 * CALLED FROM:
3449 * Timer function
3450 *
3451 * RETURNS:
3452 * none
3453 */
3454/*****************************************************************************/
3455void
3456lpfc_disc_timeout(unsigned long ptr)
3457{
James Smart2e0fef82007-06-17 19:56:36 -05003458 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
3459 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04003460 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003461 unsigned long flags = 0;
3462
3463 if (unlikely(!phba))
3464 return;
3465
James Smart5e9d9b82008-06-14 22:52:53 -04003466 spin_lock_irqsave(&vport->work_port_lock, flags);
3467 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
3468 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05003469 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04003470 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05003471
James Smart5e9d9b82008-06-14 22:52:53 -04003472 if (!tmo_posted)
3473 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003474 return;
3475}
3476
3477static void
James Smart2e0fef82007-06-17 19:56:36 -05003478lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003479{
James Smart2e0fef82007-06-17 19:56:36 -05003480 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3481 struct lpfc_hba *phba = vport->phba;
3482 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003483 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05003484 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05003485 int rc, clrlaerr = 0;
3486
James Smart2e0fef82007-06-17 19:56:36 -05003487 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05003488 return;
3489
James Smart2e0fef82007-06-17 19:56:36 -05003490 spin_lock_irq(shost->host_lock);
3491 vport->fc_flag &= ~FC_DISC_TMO;
3492 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003493
James Smart858c9f62007-06-17 19:56:39 -05003494 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3495 "disc timeout: state:x%x rtry:x%x flg:x%x",
3496 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3497
James Smart2e0fef82007-06-17 19:56:36 -05003498 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05003499
3500 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05003501 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
3502 * FAN
3503 */
3504 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04003505 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3506 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003507 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05003508 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04003509 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003510 if (!NLP_CHK_NODE_ACT(ndlp))
3511 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003512 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3513 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003514 if (ndlp->nlp_type & NLP_FABRIC) {
3515 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05003516 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04003517
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003518 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003519 /* Fail outstanding IO now since device
3520 * is marked for PLOGI.
3521 */
James Smart2e0fef82007-06-17 19:56:36 -05003522 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003523 }
3524 }
James Smart92d7f7b2007-06-17 19:56:38 -05003525 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05003526 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05003527 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003528 }
dea31012005-04-17 16:05:31 -05003529 break;
3530
James Smart92d7f7b2007-06-17 19:56:38 -05003531 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05003532 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05003533 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05003534 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04003535 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3536 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04003537 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05003538
3539 /* Assume no Fabric and go on with discovery.
3540 * Check for outstanding ELS FLOGI to abort.
3541 */
3542
3543 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05003544 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003545
3546 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05003547 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05003548 break;
3549
3550 case LPFC_FABRIC_CFG_LINK:
3551 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
3552 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04003553 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3554 "0223 Timeout while waiting for "
3555 "NameServer login\n");
dea31012005-04-17 16:05:31 -05003556 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05003557 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05003558 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04003559 lpfc_els_abort(phba, ndlp);
3560
3561 /* ReStart discovery */
3562 goto restart_disc;
dea31012005-04-17 16:05:31 -05003563
3564 case LPFC_NS_QRY:
3565 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04003566 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3567 "0224 NameServer Query timeout "
3568 "Data: x%x x%x\n",
3569 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05003570
James Smart92d7f7b2007-06-17 19:56:38 -05003571 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
3572 /* Try it one more time */
3573 vport->fc_ns_retry++;
3574 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
3575 vport->fc_ns_retry, 0);
3576 if (rc == 0)
3577 break;
dea31012005-04-17 16:05:31 -05003578 }
James Smart92d7f7b2007-06-17 19:56:38 -05003579 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003580
James Smart87af33f2007-10-27 13:37:43 -04003581restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05003582 /*
3583 * Discovery is over.
3584 * set port_state to PORT_READY if SLI2.
3585 * cmpl_reg_vpi will set port_state to READY for SLI3.
3586 */
James Smart3772a992009-05-22 14:50:54 -04003587 if (phba->sli_rev < LPFC_SLI_REV4) {
3588 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3589 lpfc_issue_reg_vpi(phba, vport);
3590 else { /* NPIV Not enabled */
3591 lpfc_issue_clear_la(phba, vport);
3592 vport->port_state = LPFC_VPORT_READY;
3593 }
dea31012005-04-17 16:05:31 -05003594 }
3595
3596 /* Setup and issue mailbox INITIALIZE LINK command */
3597 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3598 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04003599 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3600 "0206 Device Discovery "
3601 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05003602 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003603 break;
3604 }
3605
3606 lpfc_linkdown(phba);
3607 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
3608 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04003609 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05003610 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003611 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04003612 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04003613 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05003614 if (rc == MBX_NOT_FINISHED)
3615 mempool_free(initlinkmbox, phba->mbox_mem_pool);
3616
3617 break;
3618
3619 case LPFC_DISC_AUTH:
3620 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04003621 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3622 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05003623 lpfc_disc_flush_list(vport);
3624
James Smart92d7f7b2007-06-17 19:56:38 -05003625 /*
3626 * set port_state to PORT_READY if SLI2.
3627 * cmpl_reg_vpi will set port_state to READY for SLI3.
3628 */
James Smart3772a992009-05-22 14:50:54 -04003629 if (phba->sli_rev < LPFC_SLI_REV4) {
3630 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3631 lpfc_issue_reg_vpi(phba, vport);
3632 else { /* NPIV Not enabled */
3633 lpfc_issue_clear_la(phba, vport);
3634 vport->port_state = LPFC_VPORT_READY;
3635 }
dea31012005-04-17 16:05:31 -05003636 }
3637 break;
3638
James Smart2e0fef82007-06-17 19:56:36 -05003639 case LPFC_VPORT_READY:
3640 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04003641 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3642 "0231 RSCN timeout Data: x%x "
3643 "x%x\n",
3644 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05003645
3646 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05003647 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05003648
James Smart2e0fef82007-06-17 19:56:36 -05003649 lpfc_els_flush_rscn(vport);
3650 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05003651 }
3652 break;
James Smart2e0fef82007-06-17 19:56:36 -05003653
James Smart92d7f7b2007-06-17 19:56:38 -05003654 default:
James Smarte8b62012007-08-02 11:10:09 -04003655 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04003656 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04003657 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05003658 break;
3659 }
3660
3661 switch (phba->link_state) {
3662 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05003663 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04003664 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3665 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05003666 clrlaerr = 1;
3667 break;
3668
James Smart09372822008-01-11 01:52:54 -05003669 case LPFC_LINK_UP:
3670 lpfc_issue_clear_la(phba, vport);
3671 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05003672 case LPFC_LINK_UNKNOWN:
3673 case LPFC_WARM_START:
3674 case LPFC_INIT_START:
3675 case LPFC_INIT_MBX_CMDS:
3676 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05003677 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04003678 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3679 "0230 Unexpected timeout, hba link "
3680 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05003681 clrlaerr = 1;
3682 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003683
3684 case LPFC_HBA_READY:
3685 break;
dea31012005-04-17 16:05:31 -05003686 }
3687
3688 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05003689 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05003690 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05003691 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
3692 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05003693 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05003694 }
3695
3696 return;
3697}
3698
dea31012005-04-17 16:05:31 -05003699/*
3700 * This routine handles processing a NameServer REG_LOGIN mailbox
3701 * command upon completion. It is setup in the LPFC_MBOXQ
3702 * as the completion routine when the command is
3703 * handed off to the SLI layer.
3704 */
3705void
James Smart2e0fef82007-06-17 19:56:36 -05003706lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003707{
James Smart04c68492009-05-22 14:52:52 -04003708 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003709 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3710 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3711 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003712
3713 pmb->context1 = NULL;
3714
dea31012005-04-17 16:05:31 -05003715 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05003716 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003717 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003718
James Smart2e0fef82007-06-17 19:56:36 -05003719 /*
3720 * Start issuing Fabric-Device Management Interface (FDMI) command to
3721 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
3722 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05003723 */
James Smart2e0fef82007-06-17 19:56:36 -05003724
James Smart3de2a652007-08-02 11:09:59 -04003725 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05003726 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
3727 else
3728 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05003729
James Smartfa4066b2008-01-11 01:53:27 -05003730 /* decrement the node reference count held for this callback
3731 * function.
3732 */
James Smart329f9bc2007-04-25 09:53:01 -04003733 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003734 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3735 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003736 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003737
3738 return;
3739}
3740
James Smart685f0bf2007-04-25 09:53:08 -04003741static int
3742lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
3743{
3744 uint16_t *rpi = param;
3745
3746 return ndlp->nlp_rpi == *rpi;
3747}
3748
3749static int
3750lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
3751{
3752 return memcmp(&ndlp->nlp_portname, param,
3753 sizeof(ndlp->nlp_portname)) == 0;
3754}
3755
Adrian Bunka6ababd2007-11-05 18:07:33 +01003756static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003757__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05003758{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04003759 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003760
James Smart2e0fef82007-06-17 19:56:36 -05003761 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04003762 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04003763 return ndlp;
3764 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04003765 return NULL;
dea31012005-04-17 16:05:31 -05003766}
3767
James Smart685f0bf2007-04-25 09:53:08 -04003768/*
3769 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05003770 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04003771 */
3772struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003773__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04003774{
James Smart2e0fef82007-06-17 19:56:36 -05003775 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04003776}
3777
James Smart488d1462006-03-07 15:02:37 -05003778/*
James Smart685f0bf2007-04-25 09:53:08 -04003779 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05003780 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05003781 */
3782struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003783lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05003784{
James Smart2e0fef82007-06-17 19:56:36 -05003785 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05003786 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05003787
James Smart2e0fef82007-06-17 19:56:36 -05003788 spin_lock_irq(shost->host_lock);
3789 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
3790 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003791 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05003792}
3793
dea31012005-04-17 16:05:31 -05003794void
James Smart2e0fef82007-06-17 19:56:36 -05003795lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3796 uint32_t did)
dea31012005-04-17 16:05:31 -05003797{
3798 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05003799
3800 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04003801 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05003802
3803 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3804 "node init: did:x%x",
3805 ndlp->nlp_DID, 0, 0);
3806
dea31012005-04-17 16:05:31 -05003807 return;
3808}
James Smart329f9bc2007-04-25 09:53:01 -04003809
James Smart98c9ea52007-10-27 13:37:33 -04003810/* This routine releases all resources associated with a specifc NPort's ndlp
3811 * and mempool_free's the nodelist.
3812 */
James Smart311464e2007-08-02 11:10:37 -04003813static void
James Smart329f9bc2007-04-25 09:53:01 -04003814lpfc_nlp_release(struct kref *kref)
3815{
James Smarte47c9092008-02-08 18:49:26 -05003816 struct lpfc_hba *phba;
3817 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04003818 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
3819 kref);
James Smart858c9f62007-06-17 19:56:39 -05003820
3821 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3822 "node release: did:x%x flg:x%x type:x%x",
3823 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3824
James Smarte47c9092008-02-08 18:49:26 -05003825 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3826 "0279 lpfc_nlp_release: ndlp:x%p "
3827 "usgmap:x%x refcnt:%d\n",
3828 (void *)ndlp, ndlp->nlp_usg_map,
3829 atomic_read(&ndlp->kref.refcount));
3830
3831 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05003832 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003833
3834 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04003835 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05003836 spin_lock_irqsave(&phba->ndlp_lock, flags);
3837 NLP_CLR_NODE_ACT(ndlp);
3838 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3839
3840 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04003841 if (NLP_CHK_FREE_REQ(ndlp)) {
3842 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04003843 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04003844 }
James Smart329f9bc2007-04-25 09:53:01 -04003845}
3846
James Smart98c9ea52007-10-27 13:37:33 -04003847/* This routine bumps the reference count for a ndlp structure to ensure
3848 * that one discovery thread won't free a ndlp while another discovery thread
3849 * is using it.
3850 */
James Smart329f9bc2007-04-25 09:53:01 -04003851struct lpfc_nodelist *
3852lpfc_nlp_get(struct lpfc_nodelist *ndlp)
3853{
James Smarte47c9092008-02-08 18:49:26 -05003854 struct lpfc_hba *phba;
3855 unsigned long flags;
3856
James Smart98c9ea52007-10-27 13:37:33 -04003857 if (ndlp) {
3858 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3859 "node get: did:x%x flg:x%x refcnt:x%x",
3860 ndlp->nlp_DID, ndlp->nlp_flag,
3861 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003862 /* The check of ndlp usage to prevent incrementing the
3863 * ndlp reference count that is in the process of being
3864 * released.
3865 */
James Smarta257bf92009-04-06 18:48:10 -04003866 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05003867 spin_lock_irqsave(&phba->ndlp_lock, flags);
3868 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
3869 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3870 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3871 "0276 lpfc_nlp_get: ndlp:x%p "
3872 "usgmap:x%x refcnt:%d\n",
3873 (void *)ndlp, ndlp->nlp_usg_map,
3874 atomic_read(&ndlp->kref.refcount));
3875 return NULL;
3876 } else
3877 kref_get(&ndlp->kref);
3878 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04003879 }
James Smart329f9bc2007-04-25 09:53:01 -04003880 return ndlp;
3881}
3882
James Smart98c9ea52007-10-27 13:37:33 -04003883/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05003884 * count goes to 0, this indicates the the associated nodelist should be
3885 * freed. Returning 1 indicates the ndlp resource has been released; on the
3886 * other hand, returning 0 indicates the ndlp resource has not been released
3887 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04003888 */
James Smart329f9bc2007-04-25 09:53:01 -04003889int
3890lpfc_nlp_put(struct lpfc_nodelist *ndlp)
3891{
James Smarte47c9092008-02-08 18:49:26 -05003892 struct lpfc_hba *phba;
3893 unsigned long flags;
3894
3895 if (!ndlp)
3896 return 1;
3897
3898 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3899 "node put: did:x%x flg:x%x refcnt:x%x",
3900 ndlp->nlp_DID, ndlp->nlp_flag,
3901 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04003902 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05003903 spin_lock_irqsave(&phba->ndlp_lock, flags);
3904 /* Check the ndlp memory free acknowledge flag to avoid the
3905 * possible race condition that kref_put got invoked again
3906 * after previous one has done ndlp memory free.
3907 */
3908 if (NLP_CHK_FREE_ACK(ndlp)) {
3909 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3910 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3911 "0274 lpfc_nlp_put: ndlp:x%p "
3912 "usgmap:x%x refcnt:%d\n",
3913 (void *)ndlp, ndlp->nlp_usg_map,
3914 atomic_read(&ndlp->kref.refcount));
3915 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003916 }
James Smarte47c9092008-02-08 18:49:26 -05003917 /* Check the ndlp inactivate log flag to avoid the possible
3918 * race condition that kref_put got invoked again after ndlp
3919 * is already in inactivating state.
3920 */
3921 if (NLP_CHK_IACT_REQ(ndlp)) {
3922 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3923 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3924 "0275 lpfc_nlp_put: ndlp:x%p "
3925 "usgmap:x%x refcnt:%d\n",
3926 (void *)ndlp, ndlp->nlp_usg_map,
3927 atomic_read(&ndlp->kref.refcount));
3928 return 1;
3929 }
3930 /* For last put, mark the ndlp usage flags to make sure no
3931 * other kref_get and kref_put on the same ndlp shall get
3932 * in between the process when the final kref_put has been
3933 * invoked on this ndlp.
3934 */
3935 if (atomic_read(&ndlp->kref.refcount) == 1) {
3936 /* Indicate ndlp is put to inactive state. */
3937 NLP_SET_IACT_REQ(ndlp);
3938 /* Acknowledge ndlp memory free has been seen. */
3939 if (NLP_CHK_FREE_REQ(ndlp))
3940 NLP_SET_FREE_ACK(ndlp);
3941 }
3942 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3943 /* Note, the kref_put returns 1 when decrementing a reference
3944 * count that was 1, it invokes the release callback function,
3945 * but it still left the reference count as 1 (not actually
3946 * performs the last decrementation). Otherwise, it actually
3947 * decrements the reference count and returns 0.
3948 */
3949 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04003950}
James Smart98c9ea52007-10-27 13:37:33 -04003951
3952/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05003953 * by any other discovery thread. This routine returns 1 if the
3954 * ndlp has been freed. A return value of 0 indicates the ndlp is
3955 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04003956 */
3957int
3958lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
3959{
3960 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3961 "node not used: did:x%x flg:x%x refcnt:x%x",
3962 ndlp->nlp_DID, ndlp->nlp_flag,
3963 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003964 if (atomic_read(&ndlp->kref.refcount) == 1)
3965 if (lpfc_nlp_put(ndlp))
3966 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003967 return 0;
3968}
James Smart6fb120a2009-05-22 14:52:59 -04003969
3970/**
3971 * lpfc_fcf_inuse - Check if FCF can be unregistered.
3972 * @phba: Pointer to hba context object.
3973 *
3974 * This function iterate through all FC nodes associated
3975 * will all vports to check if there is any node with
3976 * fc_rports associated with it. If there is an fc_rport
3977 * associated with the node, then the node is either in
3978 * discovered state or its devloss_timer is pending.
3979 */
3980static int
3981lpfc_fcf_inuse(struct lpfc_hba *phba)
3982{
3983 struct lpfc_vport **vports;
3984 int i, ret = 0;
3985 struct lpfc_nodelist *ndlp;
3986 struct Scsi_Host *shost;
3987
3988 vports = lpfc_create_vport_work_array(phba);
3989
3990 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3991 shost = lpfc_shost_from_vport(vports[i]);
3992 spin_lock_irq(shost->host_lock);
3993 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
3994 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
3995 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
3996 ret = 1;
3997 spin_unlock_irq(shost->host_lock);
3998 goto out;
3999 }
4000 }
4001 spin_unlock_irq(shost->host_lock);
4002 }
4003out:
4004 lpfc_destroy_vport_work_array(phba, vports);
4005 return ret;
4006}
4007
4008/**
4009 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4010 * @phba: Pointer to hba context object.
4011 * @mboxq: Pointer to mailbox object.
4012 *
4013 * This function frees memory associated with the mailbox command.
4014 */
4015static void
4016lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4017{
4018 struct lpfc_vport *vport = mboxq->vport;
4019
4020 if (mboxq->u.mb.mbxStatus) {
4021 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4022 "2555 UNREG_VFI mbxStatus error x%x "
4023 "HBA state x%x\n",
4024 mboxq->u.mb.mbxStatus, vport->port_state);
4025 }
4026 mempool_free(mboxq, phba->mbox_mem_pool);
4027 return;
4028}
4029
4030/**
4031 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4032 * @phba: Pointer to hba context object.
4033 * @mboxq: Pointer to mailbox object.
4034 *
4035 * This function frees memory associated with the mailbox command.
4036 */
4037static void
4038lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4039{
4040 struct lpfc_vport *vport = mboxq->vport;
4041
4042 if (mboxq->u.mb.mbxStatus) {
4043 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4044 "2550 UNREG_FCFI mbxStatus error x%x "
4045 "HBA state x%x\n",
4046 mboxq->u.mb.mbxStatus, vport->port_state);
4047 }
4048 mempool_free(mboxq, phba->mbox_mem_pool);
4049 return;
4050}
4051
4052/**
4053 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
4054 * @phba: Pointer to hba context object.
4055 *
4056 * This function check if there are any connected remote port for the FCF and
4057 * if all the devices are disconnected, this function unregister FCFI.
4058 * This function also tries to use another FCF for discovery.
4059 */
4060void
4061lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
4062{
4063 LPFC_MBOXQ_t *mbox;
4064 int rc;
4065 struct lpfc_vport **vports;
4066 int i;
4067
4068 spin_lock_irq(&phba->hbalock);
4069 /*
4070 * If HBA is not running in FIP mode or
4071 * If HBA does not support FCoE or
4072 * If FCF is not registered.
4073 * do nothing.
4074 */
4075 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
4076 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
4077 (phba->cfg_enable_fip == 0)) {
4078 spin_unlock_irq(&phba->hbalock);
4079 return;
4080 }
4081 spin_unlock_irq(&phba->hbalock);
4082
4083 if (lpfc_fcf_inuse(phba))
4084 return;
4085
4086
4087 /* Unregister VPIs */
4088 vports = lpfc_create_vport_work_array(phba);
4089 if (vports &&
4090 (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
4091 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4092 lpfc_mbx_unreg_vpi(vports[i]);
4093 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4094 vports[i]->vfi_state &= ~LPFC_VFI_REGISTERED;
4095 }
4096 lpfc_destroy_vport_work_array(phba, vports);
4097
4098 /* Unregister VFI */
4099 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4100 if (!mbox) {
4101 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4102 "2556 UNREG_VFI mbox allocation failed"
4103 "HBA state x%x\n",
4104 phba->pport->port_state);
4105 return;
4106 }
4107
4108 lpfc_unreg_vfi(mbox, phba->pport->vfi);
4109 mbox->vport = phba->pport;
4110 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
4111
4112 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4113 if (rc == MBX_NOT_FINISHED) {
4114 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4115 "2557 UNREG_VFI issue mbox failed rc x%x "
4116 "HBA state x%x\n",
4117 rc, phba->pport->port_state);
4118 mempool_free(mbox, phba->mbox_mem_pool);
4119 return;
4120 }
4121
4122 /* Unregister FCF */
4123 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4124 if (!mbox) {
4125 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4126 "2551 UNREG_FCFI mbox allocation failed"
4127 "HBA state x%x\n",
4128 phba->pport->port_state);
4129 return;
4130 }
4131
4132 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
4133 mbox->vport = phba->pport;
4134 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
4135 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4136
4137 if (rc == MBX_NOT_FINISHED) {
4138 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4139 "2552 UNREG_FCFI issue mbox failed rc x%x "
4140 "HBA state x%x\n",
4141 rc, phba->pport->port_state);
4142 mempool_free(mbox, phba->mbox_mem_pool);
4143 return;
4144 }
4145
4146 spin_lock_irq(&phba->hbalock);
4147 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_REGISTERED |
4148 FCF_DISCOVERED | FCF_BOOT_ENABLE | FCF_IN_USE |
4149 FCF_VALID_VLAN);
4150 spin_unlock_irq(&phba->hbalock);
4151
4152 /*
4153 * If driver is not unloading, check if there is any other
4154 * FCF record that can be used for discovery.
4155 */
4156 if ((phba->pport->load_flag & FC_UNLOADING) ||
4157 (phba->link_state < LPFC_LINK_UP))
4158 return;
4159
4160 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
4161
4162 if (rc)
4163 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4164 "2553 lpfc_unregister_unused_fcf failed to read FCF"
4165 " record HBA state x%x\n",
4166 phba->pport->port_state);
4167}
4168
4169/**
4170 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
4171 * @phba: Pointer to hba context object.
4172 * @buff: Buffer containing the FCF connection table as in the config
4173 * region.
4174 * This function create driver data structure for the FCF connection
4175 * record table read from config region 23.
4176 */
4177static void
4178lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
4179 uint8_t *buff)
4180{
4181 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4182 struct lpfc_fcf_conn_hdr *conn_hdr;
4183 struct lpfc_fcf_conn_rec *conn_rec;
4184 uint32_t record_count;
4185 int i;
4186
4187 /* Free the current connect table */
4188 list_for_each_entry_safe(conn_entry, next_conn_entry,
4189 &phba->fcf_conn_rec_list, list)
4190 kfree(conn_entry);
4191
4192 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
4193 record_count = conn_hdr->length * sizeof(uint32_t)/
4194 sizeof(struct lpfc_fcf_conn_rec);
4195
4196 conn_rec = (struct lpfc_fcf_conn_rec *)
4197 (buff + sizeof(struct lpfc_fcf_conn_hdr));
4198
4199 for (i = 0; i < record_count; i++) {
4200 if (!(conn_rec[i].flags & FCFCNCT_VALID))
4201 continue;
4202 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
4203 GFP_KERNEL);
4204 if (!conn_entry) {
4205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4206 "2566 Failed to allocate connection"
4207 " table entry\n");
4208 return;
4209 }
4210
4211 memcpy(&conn_entry->conn_rec, &conn_rec[i],
4212 sizeof(struct lpfc_fcf_conn_rec));
4213 conn_entry->conn_rec.vlan_tag =
4214 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
4215 conn_entry->conn_rec.flags =
4216 le16_to_cpu(conn_entry->conn_rec.flags);
4217 list_add_tail(&conn_entry->list,
4218 &phba->fcf_conn_rec_list);
4219 }
4220}
4221
4222/**
4223 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
4224 * @phba: Pointer to hba context object.
4225 * @buff: Buffer containing the FCoE parameter data structure.
4226 *
4227 * This function update driver data structure with config
4228 * parameters read from config region 23.
4229 */
4230static void
4231lpfc_read_fcoe_param(struct lpfc_hba *phba,
4232 uint8_t *buff)
4233{
4234 struct lpfc_fip_param_hdr *fcoe_param_hdr;
4235 struct lpfc_fcoe_params *fcoe_param;
4236
4237 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
4238 buff;
4239 fcoe_param = (struct lpfc_fcoe_params *)
4240 buff + sizeof(struct lpfc_fip_param_hdr);
4241
4242 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
4243 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
4244 return;
4245
4246 if (bf_get(lpfc_fip_param_hdr_fipp_mode, fcoe_param_hdr) ==
4247 FIPP_MODE_ON)
4248 phba->cfg_enable_fip = 1;
4249
4250 if (bf_get(lpfc_fip_param_hdr_fipp_mode, fcoe_param_hdr) ==
4251 FIPP_MODE_OFF)
4252 phba->cfg_enable_fip = 0;
4253
4254 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
4255 phba->valid_vlan = 1;
4256 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
4257 0xFFF;
4258 }
4259
4260 phba->fc_map[0] = fcoe_param->fc_map[0];
4261 phba->fc_map[1] = fcoe_param->fc_map[1];
4262 phba->fc_map[2] = fcoe_param->fc_map[2];
4263 return;
4264}
4265
4266/**
4267 * lpfc_get_rec_conf23 - Get a record type in config region data.
4268 * @buff: Buffer containing config region 23 data.
4269 * @size: Size of the data buffer.
4270 * @rec_type: Record type to be searched.
4271 *
4272 * This function searches config region data to find the begining
4273 * of the record specified by record_type. If record found, this
4274 * function return pointer to the record else return NULL.
4275 */
4276static uint8_t *
4277lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
4278{
4279 uint32_t offset = 0, rec_length;
4280
4281 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
4282 (size < sizeof(uint32_t)))
4283 return NULL;
4284
4285 rec_length = buff[offset + 1];
4286
4287 /*
4288 * One TLV record has one word header and number of data words
4289 * specified in the rec_length field of the record header.
4290 */
4291 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
4292 <= size) {
4293 if (buff[offset] == rec_type)
4294 return &buff[offset];
4295
4296 if (buff[offset] == LPFC_REGION23_LAST_REC)
4297 return NULL;
4298
4299 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
4300 rec_length = buff[offset + 1];
4301 }
4302 return NULL;
4303}
4304
4305/**
4306 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
4307 * @phba: Pointer to lpfc_hba data structure.
4308 * @buff: Buffer containing config region 23 data.
4309 * @size: Size of the data buffer.
4310 *
4311 * This fuction parse the FCoE config parameters in config region 23 and
4312 * populate driver data structure with the parameters.
4313 */
4314void
4315lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
4316 uint8_t *buff,
4317 uint32_t size)
4318{
4319 uint32_t offset = 0, rec_length;
4320 uint8_t *rec_ptr;
4321
4322 /*
4323 * If data size is less than 2 words signature and version cannot be
4324 * verified.
4325 */
4326 if (size < 2*sizeof(uint32_t))
4327 return;
4328
4329 /* Check the region signature first */
4330 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
4331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4332 "2567 Config region 23 has bad signature\n");
4333 return;
4334 }
4335
4336 offset += 4;
4337
4338 /* Check the data structure version */
4339 if (buff[offset] != LPFC_REGION23_VERSION) {
4340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4341 "2568 Config region 23 has bad version\n");
4342 return;
4343 }
4344 offset += 4;
4345
4346 rec_length = buff[offset + 1];
4347
4348 /* Read FCoE param record */
4349 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4350 size - offset, FCOE_PARAM_TYPE);
4351 if (rec_ptr)
4352 lpfc_read_fcoe_param(phba, rec_ptr);
4353
4354 /* Read FCF connection table */
4355 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4356 size - offset, FCOE_CONN_TBL_TYPE);
4357 if (rec_ptr)
4358 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
4359
4360}