blob: 0fc66005d545cf0f7ab378d808f8b20a847e26e6 [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 Smarte47c9092008-02-08 18:49:26 -05004 * Copyright (C) 2004-2008 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 Smart92d7f7b2007-06-17 19:56:38 -0500278}
James Smartc01f3202006-08-18 17:47:08 -0400279
James Smartea2151b2008-09-07 11:52:10 -0400280/**
James Smart3621a712009-04-06 18:47:14 -0400281 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400282 * @phba: Pointer to hba context object.
283 *
284 * This function is called from the functions which need to post
285 * events from interrupt context. This function allocates data
286 * structure required for posting event. It also keeps track of
287 * number of events pending and prevent event storm when there are
288 * too many events.
289 **/
290struct lpfc_fast_path_event *
291lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
292 struct lpfc_fast_path_event *ret;
293
294 /* If there are lot of fast event do not exhaust memory due to this */
295 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
296 return NULL;
297
298 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
299 GFP_ATOMIC);
300 if (ret)
301 atomic_inc(&phba->fast_event_count);
302 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
303 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
304 return ret;
305}
306
307/**
James Smart3621a712009-04-06 18:47:14 -0400308 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400309 * @phba: Pointer to hba context object.
310 * @evt: Event object which need to be freed.
311 *
312 * This function frees the data structure required for posting
313 * events.
314 **/
315void
316lpfc_free_fast_evt(struct lpfc_hba *phba,
317 struct lpfc_fast_path_event *evt) {
318
319 atomic_dec(&phba->fast_event_count);
320 kfree(evt);
321}
322
323/**
James Smart3621a712009-04-06 18:47:14 -0400324 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400325 * @phba: Pointer to hba context object.
326 * @evtp: Event data structure.
327 *
328 * This function is called from worker thread, when the interrupt
329 * context need to post an event. This function posts the event
330 * to fc transport netlink interface.
331 **/
332static void
333lpfc_send_fastpath_evt(struct lpfc_hba *phba,
334 struct lpfc_work_evt *evtp)
335{
336 unsigned long evt_category, evt_sub_category;
337 struct lpfc_fast_path_event *fast_evt_data;
338 char *evt_data;
339 uint32_t evt_data_size;
340 struct Scsi_Host *shost;
341
342 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
343 work_evt);
344
345 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
346 evt_sub_category = (unsigned long) fast_evt_data->un.
347 fabric_evt.subcategory;
348 shost = lpfc_shost_from_vport(fast_evt_data->vport);
349 if (evt_category == FC_REG_FABRIC_EVENT) {
350 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
351 evt_data = (char *) &fast_evt_data->un.read_check_error;
352 evt_data_size = sizeof(fast_evt_data->un.
353 read_check_error);
354 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500355 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400356 evt_data = (char *) &fast_evt_data->un.fabric_evt;
357 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
358 } else {
359 lpfc_free_fast_evt(phba, fast_evt_data);
360 return;
361 }
362 } else if (evt_category == FC_REG_SCSI_EVENT) {
363 switch (evt_sub_category) {
364 case LPFC_EVENT_QFULL:
365 case LPFC_EVENT_DEVBSY:
366 evt_data = (char *) &fast_evt_data->un.scsi_evt;
367 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
368 break;
369 case LPFC_EVENT_CHECK_COND:
370 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
371 evt_data_size = sizeof(fast_evt_data->un.
372 check_cond_evt);
373 break;
374 case LPFC_EVENT_VARQUEDEPTH:
375 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
376 evt_data_size = sizeof(fast_evt_data->un.
377 queue_depth_evt);
378 break;
379 default:
380 lpfc_free_fast_evt(phba, fast_evt_data);
381 return;
382 }
383 } else {
384 lpfc_free_fast_evt(phba, fast_evt_data);
385 return;
386 }
387
388 fc_host_post_vendor_event(shost,
389 fc_get_event_number(),
390 evt_data_size,
391 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500392 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400393
394 lpfc_free_fast_evt(phba, fast_evt_data);
395 return;
396}
397
dea31012005-04-17 16:05:31 -0500398static void
James Smart2e0fef82007-06-17 19:56:36 -0500399lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500400{
401 struct lpfc_work_evt *evtp = NULL;
402 struct lpfc_nodelist *ndlp;
403 int free_evt;
404
James Smart2e0fef82007-06-17 19:56:36 -0500405 spin_lock_irq(&phba->hbalock);
406 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500407 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
408 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500409 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500410 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500411 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500412 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500413 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500414 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500415 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500416 /* decrement the node reference count held
417 * for this queued work
418 */
419 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500420 break;
James Smart858c9f62007-06-17 19:56:39 -0500421 case LPFC_EVT_DEV_LOSS:
422 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500423 lpfc_dev_loss_tmo_handler(ndlp);
424 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500425 /* decrement the node reference count held for
426 * this queued work
427 */
James Smart858c9f62007-06-17 19:56:39 -0500428 lpfc_nlp_put(ndlp);
429 break;
dea31012005-04-17 16:05:31 -0500430 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500431 if (phba->link_state < LPFC_LINK_DOWN)
432 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500433 else
James Smart2e0fef82007-06-17 19:56:36 -0500434 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500435 complete((struct completion *)(evtp->evt_arg2));
436 break;
James Smart46fa3112007-04-25 09:51:45 -0400437 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500438 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400439 lpfc_offline_prep(phba);
440 *(int *)(evtp->evt_arg1) = 0;
441 complete((struct completion *)(evtp->evt_arg2));
442 break;
443 case LPFC_EVT_OFFLINE:
444 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500445 lpfc_sli_brdrestart(phba);
446 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400447 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
448 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500449 complete((struct completion *)(evtp->evt_arg2));
450 break;
451 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400452 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500453 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500454 lpfc_sli_brdreset(phba);
455 lpfc_hba_down_post(phba);
456 *(int *)(evtp->evt_arg1) =
457 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400458 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500459 complete((struct completion *)(evtp->evt_arg2));
460 break;
461 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400462 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500463 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500464 = (phba->pport->stopped)
465 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400466 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500467 complete((struct completion *)(evtp->evt_arg2));
468 break;
James Smartea2151b2008-09-07 11:52:10 -0400469 case LPFC_EVT_FASTPATH_MGMT_EVT:
470 lpfc_send_fastpath_evt(phba, evtp);
471 free_evt = 0;
472 break;
dea31012005-04-17 16:05:31 -0500473 }
474 if (free_evt)
475 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500476 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500477 }
James Smart2e0fef82007-06-17 19:56:36 -0500478 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500479
480}
481
James Smart311464e2007-08-02 11:10:37 -0400482static void
James Smart2e0fef82007-06-17 19:56:36 -0500483lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500484{
485 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500486 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400487 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400488 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400489 int i;
dea31012005-04-17 16:05:31 -0500490
James Smart2e0fef82007-06-17 19:56:36 -0500491 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500492 ha_copy = phba->work_ha;
493 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500494 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500495
James Smartda0436e2009-05-22 14:51:39 -0400496 /* First, try to post the next mailbox command to SLI4 device */
497 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
498 lpfc_sli4_post_async_mbox(phba);
499
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500500 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400501 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500502 lpfc_handle_eratt(phba);
503
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500504 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500505 lpfc_sli_handle_mb_event(phba);
506
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500507 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500508 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400509
James Smartda0436e2009-05-22 14:51:39 -0400510 /* Process SLI4 events */
511 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
512 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
513 lpfc_sli4_fcp_xri_abort_event_proc(phba);
514 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
515 lpfc_sli4_els_xri_abort_event_proc(phba);
516 if (phba->hba_flag & ASYNC_EVENT)
517 lpfc_sli4_async_event_proc(phba);
518 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
519 spin_lock_irq(&phba->hbalock);
520 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
521 spin_unlock_irq(&phba->hbalock);
522 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
523 }
524 if (phba->hba_flag & HBA_RECEIVE_BUFFER)
525 lpfc_sli4_handle_received_buffer(phba);
526 }
527
James Smart549e55c2007-08-02 11:09:51 -0400528 vports = lpfc_create_vport_work_array(phba);
529 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400530 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400531 /*
532 * We could have no vports in array if unloading, so if
533 * this happens then just use the pport
534 */
535 if (vports[i] == NULL && i == 0)
536 vport = phba->pport;
537 else
538 vport = vports[i];
539 if (vport == NULL)
540 break;
James Smart58da1ff2008-04-07 10:15:56 -0400541 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400542 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400543 vport->work_port_events &= ~work_port_events;
544 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400545 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400546 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400547 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400548 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400549 if (work_port_events & WORKER_HB_TMO)
550 lpfc_hb_timeout_handler(phba);
551 if (work_port_events & WORKER_MBOX_TMO)
552 lpfc_mbox_timeout_handler(phba);
553 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
554 lpfc_unblock_fabric_iocbs(phba);
555 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400556 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400557 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
558 lpfc_ramp_down_queue_handler(phba);
559 if (work_port_events & WORKER_RAMP_UP_QUEUE)
560 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500561 }
James Smart09372822008-01-11 01:52:54 -0500562 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500563
James Smart858c9f62007-06-17 19:56:39 -0500564 pring = &phba->sli.ring[LPFC_ELS_RING];
565 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
566 status >>= (4*LPFC_ELS_RING);
567 if ((status & HA_RXMASK)
568 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400569 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500570 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400571 /* Set the lpfc data pending flag */
572 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500573 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400574 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500575 lpfc_sli_handle_slow_ring_event(phba, pring,
576 (status &
577 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500578 }
579 /*
580 * Turn on Ring interrupts
581 */
James Smart3772a992009-05-22 14:50:54 -0400582 if (phba->sli_rev <= LPFC_SLI_REV3) {
583 spin_lock_irq(&phba->hbalock);
584 control = readl(phba->HCregaddr);
585 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
586 lpfc_debugfs_slow_ring_trc(phba,
587 "WRK Enable ring: cntl:x%x hacopy:x%x",
588 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400589
James Smart3772a992009-05-22 14:50:54 -0400590 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
591 writel(control, phba->HCregaddr);
592 readl(phba->HCregaddr); /* flush */
593 } else {
594 lpfc_debugfs_slow_ring_trc(phba,
595 "WRK Ring ok: cntl:x%x hacopy:x%x",
596 control, ha_copy, 0);
597 }
598 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500599 }
600 }
James Smart2e0fef82007-06-17 19:56:36 -0500601 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500602}
603
dea31012005-04-17 16:05:31 -0500604int
605lpfc_do_work(void *p)
606{
607 struct lpfc_hba *phba = p;
608 int rc;
dea31012005-04-17 16:05:31 -0500609
610 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400611 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500612
James Smart3a55b532008-12-04 22:38:54 -0500613 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400614 /* wait and check worker queue activities */
615 rc = wait_event_interruptible(phba->work_waitq,
616 (test_and_clear_bit(LPFC_DATA_READY,
617 &phba->data_flags)
618 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500619 /* Signal wakeup shall terminate the worker thread */
620 if (rc) {
621 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
622 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500623 break;
James Smart3a55b532008-12-04 22:38:54 -0500624 }
dea31012005-04-17 16:05:31 -0500625
James Smart5e9d9b82008-06-14 22:52:53 -0400626 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500627 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500628 }
James Smart3a55b532008-12-04 22:38:54 -0500629 phba->worker_thread = NULL;
630 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
631 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500632 return 0;
633}
634
635/*
636 * This is only called to handle FC worker events. Since this a rare
637 * occurance, we allocate a struct lpfc_work_evt structure here instead of
638 * embedding it in the IOCB.
639 */
640int
James Smart2e0fef82007-06-17 19:56:36 -0500641lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500642 uint32_t evt)
643{
644 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500645 unsigned long flags;
dea31012005-04-17 16:05:31 -0500646
647 /*
648 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
649 * be queued to worker thread for processing
650 */
James Smart92d7f7b2007-06-17 19:56:38 -0500651 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500652 if (!evtp)
653 return 0;
654
655 evtp->evt_arg1 = arg1;
656 evtp->evt_arg2 = arg2;
657 evtp->evt = evt;
658
James Smarted957682007-06-17 19:56:37 -0500659 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400660 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500661 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500662
James Smart5e9d9b82008-06-14 22:52:53 -0400663 lpfc_worker_wake_up(phba);
664
dea31012005-04-17 16:05:31 -0500665 return 1;
666}
667
James Smart92d7f7b2007-06-17 19:56:38 -0500668void
669lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
670{
James Smart09372822008-01-11 01:52:54 -0500671 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500672 struct lpfc_hba *phba = vport->phba;
673 struct lpfc_nodelist *ndlp, *next_ndlp;
674 int rc;
675
676 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500677 if (!NLP_CHK_NODE_ACT(ndlp))
678 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500679 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
680 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400681 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
682 ((vport->port_type == LPFC_NPIV_PORT) &&
683 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500684 lpfc_unreg_rpi(vport, ndlp);
685
686 /* Leave Fabric nodes alone on link down */
687 if (!remove && ndlp->nlp_type & NLP_FABRIC)
688 continue;
689 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
690 remove
691 ? NLP_EVT_DEVICE_RM
692 : NLP_EVT_DEVICE_RECOVERY);
693 }
694 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
695 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500696 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500697 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500698 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500699 }
700}
701
James Smart87af33f2007-10-27 13:37:43 -0400702void
James Smart98c9ea52007-10-27 13:37:33 -0400703lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500704{
James Smart92d7f7b2007-06-17 19:56:38 -0500705 /* Cleanup any outstanding RSCN activity */
706 lpfc_els_flush_rscn(vport);
707
708 /* Cleanup any outstanding ELS commands */
709 lpfc_els_flush_cmd(vport);
710
711 lpfc_cleanup_rpis(vport, 0);
712
James Smart92d7f7b2007-06-17 19:56:38 -0500713 /* Turn off discovery timer if its running */
714 lpfc_can_disctmo(vport);
715}
716
James Smart3772a992009-05-22 14:50:54 -0400717void
James Smart98c9ea52007-10-27 13:37:33 -0400718lpfc_linkdown_port(struct lpfc_vport *vport)
719{
720 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
721
722 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
723
724 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
725 "Link Down: state:x%x rtry:x%x flg:x%x",
726 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
727
728 lpfc_port_link_failure(vport);
729
730}
731
dea31012005-04-17 16:05:31 -0500732int
James Smart685f0bf2007-04-25 09:53:08 -0400733lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500734{
James Smart2e0fef82007-06-17 19:56:36 -0500735 struct lpfc_vport *vport = phba->pport;
736 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400737 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400738 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400739 int i;
dea31012005-04-17 16:05:31 -0500740
James Smart3163f722008-02-08 18:50:25 -0500741 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500742 return 0;
James Smart2e0fef82007-06-17 19:56:36 -0500743 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500744 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500745 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500746 phba->pport->fc_flag &= ~FC_LBIT;
747 }
James Smart2e0fef82007-06-17 19:56:36 -0500748 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400749 vports = lpfc_create_vport_work_array(phba);
750 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500751 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400752 /* Issue a LINK DOWN event to all nodes */
753 lpfc_linkdown_port(vports[i]);
754 }
James Smart09372822008-01-11 01:52:54 -0500755 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500756 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500757 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
758 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500759 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500760 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500761 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400762 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500763 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500764 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500765 }
766 }
767
dea31012005-04-17 16:05:31 -0500768 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500769 if (phba->pport->fc_flag & FC_PT2PT) {
770 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500771 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
772 if (mb) {
dea31012005-04-17 16:05:31 -0500773 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500774 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500775 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400776 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500777 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500778 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500779 }
780 }
James Smart2e0fef82007-06-17 19:56:36 -0500781 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500782 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500783 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500784 }
James Smart2e0fef82007-06-17 19:56:36 -0500785
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500786 return 0;
dea31012005-04-17 16:05:31 -0500787}
788
James Smart92d7f7b2007-06-17 19:56:38 -0500789static void
790lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500791{
James Smart92d7f7b2007-06-17 19:56:38 -0500792 struct lpfc_nodelist *ndlp;
793
794 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500795 if (!NLP_CHK_NODE_ACT(ndlp))
796 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500797 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
798 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500799 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500800 /* On Linkup its safe to clean up the ndlp
801 * from Fabric connections.
802 */
James Smart92d7f7b2007-06-17 19:56:38 -0500803 if (ndlp->nlp_DID != Fabric_DID)
804 lpfc_unreg_rpi(vport, ndlp);
805 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
806 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500807 /* Fail outstanding IO now since device is
808 * marked for PLOGI.
809 */
James Smart92d7f7b2007-06-17 19:56:38 -0500810 lpfc_unreg_rpi(vport, ndlp);
811 }
812 }
813}
814
815static void
816lpfc_linkup_port(struct lpfc_vport *vport)
817{
818 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500819 struct lpfc_hba *phba = vport->phba;
820
821 if ((vport->load_flag & FC_UNLOADING) != 0)
822 return;
823
James Smart858c9f62007-06-17 19:56:39 -0500824 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
825 "Link Up: top:x%x speed:x%x flg:x%x",
826 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
827
James Smart92d7f7b2007-06-17 19:56:38 -0500828 /* If NPIV is not enabled, only bring the physical port up */
829 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
830 (vport != phba->pport))
831 return;
dea31012005-04-17 16:05:31 -0500832
James Smart2e0fef82007-06-17 19:56:36 -0500833 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400834
James Smart2e0fef82007-06-17 19:56:36 -0500835 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500836 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
837 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
838 vport->fc_flag |= FC_NDISC_ACTIVE;
839 vport->fc_ns_retry = 0;
840 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500841
James Smart92d7f7b2007-06-17 19:56:38 -0500842 if (vport->fc_flag & FC_LBIT)
843 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500844
James Smart92d7f7b2007-06-17 19:56:38 -0500845}
846
847static int
848lpfc_linkup(struct lpfc_hba *phba)
849{
James Smart549e55c2007-08-02 11:09:51 -0400850 struct lpfc_vport **vports;
851 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500852
853 phba->link_state = LPFC_LINK_UP;
854
855 /* Unblock fabric iocbs if they are blocked */
856 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
857 del_timer_sync(&phba->fabric_block_timer);
858
James Smart549e55c2007-08-02 11:09:51 -0400859 vports = lpfc_create_vport_work_array(phba);
860 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500861 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400862 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500863 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500864 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
865 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500866
867 return 0;
868}
869
870/*
871 * This routine handles processing a CLEAR_LA mailbox
872 * command upon completion. It is setup in the LPFC_MBOXQ
873 * as the completion routine when the command is
874 * handed off to the SLI layer.
875 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100876static void
James Smart2e0fef82007-06-17 19:56:36 -0500877lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500878{
James Smart2e0fef82007-06-17 19:56:36 -0500879 struct lpfc_vport *vport = pmb->vport;
880 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
881 struct lpfc_sli *psli = &phba->sli;
882 MAILBOX_t *mb = &pmb->mb;
dea31012005-04-17 16:05:31 -0500883 uint32_t control;
884
dea31012005-04-17 16:05:31 -0500885 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500886 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500887 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
888 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
889
890 /* Check for error */
891 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500892 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400893 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
894 "0320 CLEAR_LA mbxStatus error x%x hba "
895 "state x%x\n",
896 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500897 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500898 goto out;
899 }
900
James Smart92d7f7b2007-06-17 19:56:38 -0500901 if (vport->port_type == LPFC_PHYSICAL_PORT)
902 phba->link_state = LPFC_HBA_READY;
903
904 spin_lock_irq(&phba->hbalock);
905 psli->sli_flag |= LPFC_PROCESS_LA;
906 control = readl(phba->HCregaddr);
907 control |= HC_LAINT_ENA;
908 writel(control, phba->HCregaddr);
909 readl(phba->HCregaddr); /* flush */
910 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500911 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500912 return;
dea31012005-04-17 16:05:31 -0500913
dea31012005-04-17 16:05:31 -0500914out:
915 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400916 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
917 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500918 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500919
James Smart2e0fef82007-06-17 19:56:36 -0500920 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400921 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500922 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500923
James Smart2e0fef82007-06-17 19:56:36 -0500924 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500925
926 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500927
928 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500929 psli->sli_flag |= LPFC_PROCESS_LA;
930 control = readl(phba->HCregaddr);
931 control |= HC_LAINT_ENA;
932 writel(control, phba->HCregaddr);
933 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500934 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500935
936 return;
937}
938
James Smart2e0fef82007-06-17 19:56:36 -0500939
dea31012005-04-17 16:05:31 -0500940static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500941lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500942{
James Smart2e0fef82007-06-17 19:56:36 -0500943 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500944
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500945 if (pmb->mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500946 goto out;
dea31012005-04-17 16:05:31 -0500947
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500948 mempool_free(pmb, phba->mbox_mem_pool);
949
950 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500951 vport->fc_flag & FC_PUBLIC_LOOP &&
952 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500953 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500954 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500955 * LPFC_LOCAL_CFG_LINK while waiting for FAN
956 */
James Smart2e0fef82007-06-17 19:56:36 -0500957 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500958 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500959 }
dea31012005-04-17 16:05:31 -0500960
James Smart2e0fef82007-06-17 19:56:36 -0500961 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500962 * LPFC_FLOGI while waiting for FLOGI cmpl
963 */
James Smart92d7f7b2007-06-17 19:56:38 -0500964 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500965 lpfc_initial_flogi(vport);
966 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500967 return;
dea31012005-04-17 16:05:31 -0500968
969out:
James Smarte8b62012007-08-02 11:10:09 -0400970 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
971 "0306 CONFIG_LINK mbxStatus error x%x "
972 "HBA state x%x\n",
973 pmb->mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500974 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500975
976 lpfc_linkdown(phba);
977
James Smarte8b62012007-08-02 11:10:09 -0400978 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
979 "0200 CONFIG_LINK bad hba state x%x\n",
980 vport->port_state);
dea31012005-04-17 16:05:31 -0500981
James Smart92d7f7b2007-06-17 19:56:38 -0500982 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500983 return;
984}
985
986static void
James Smart2e0fef82007-06-17 19:56:36 -0500987lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500988{
dea31012005-04-17 16:05:31 -0500989 MAILBOX_t *mb = &pmb->mb;
990 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500991 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500992
993
994 /* Check for error */
995 if (mb->mbxStatus) {
996 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400997 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
998 "0319 READ_SPARAM mbxStatus error x%x "
999 "hba state x%x>\n",
1000 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001001 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05001002 goto out;
1003 }
1004
James Smart2e0fef82007-06-17 19:56:36 -05001005 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05001006 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05001007 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05001008 u64_to_wwn(phba->cfg_soft_wwnn,
1009 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04001010 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05001011 u64_to_wwn(phba->cfg_soft_wwpn,
1012 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05001013 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1014 sizeof(vport->fc_nodename));
1015 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1016 sizeof(vport->fc_portname));
1017 if (vport->port_type == LPFC_PHYSICAL_PORT) {
1018 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1019 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1020 }
1021
dea31012005-04-17 16:05:31 -05001022 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1023 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001024 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001025 return;
1026
1027out:
1028 pmb->context1 = NULL;
1029 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1030 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05001031 lpfc_issue_clear_la(phba, vport);
1032 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001033 return;
1034}
1035
1036static void
James Smart92d7f7b2007-06-17 19:56:38 -05001037lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05001038{
James Smart92d7f7b2007-06-17 19:56:38 -05001039 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -05001040 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
James Smart2e0fef82007-06-17 19:56:36 -05001041 int i;
James Smart14691152006-12-02 13:34:28 -05001042 struct lpfc_dmabuf *mp;
1043 int rc;
1044
dea31012005-04-17 16:05:31 -05001045 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1046 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1047
James Smart92d7f7b2007-06-17 19:56:38 -05001048 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001049 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05001050 case LA_1GHZ_LINK:
1051 phba->fc_linkspeed = LA_1GHZ_LINK;
1052 break;
1053 case LA_2GHZ_LINK:
1054 phba->fc_linkspeed = LA_2GHZ_LINK;
1055 break;
1056 case LA_4GHZ_LINK:
1057 phba->fc_linkspeed = LA_4GHZ_LINK;
1058 break;
1059 case LA_8GHZ_LINK:
1060 phba->fc_linkspeed = LA_8GHZ_LINK;
1061 break;
1062 default:
1063 phba->fc_linkspeed = LA_UNKNW_LINK;
1064 break;
dea31012005-04-17 16:05:31 -05001065 }
1066
1067 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05001068 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05001069
1070 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05001071 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05001072
James Smart495a7142008-06-14 22:52:59 -04001073 if (phba->cfg_enable_npiv)
1074 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1075 "1309 Link Up Event npiv not supported in loop "
1076 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001077 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05001078 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05001079 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001080
James Smart2e0fef82007-06-17 19:56:36 -05001081 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05001082 i = la->un.lilpBde64.tus.f.bdeSize;
1083
1084 if (i == 0) {
1085 phba->alpa_map[0] = 0;
1086 } else {
James Smarte8b62012007-08-02 11:10:09 -04001087 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05001088 int numalpa, j, k;
1089 union {
1090 uint8_t pamap[16];
1091 struct {
1092 uint32_t wd1;
1093 uint32_t wd2;
1094 uint32_t wd3;
1095 uint32_t wd4;
1096 } pa;
1097 } un;
1098 numalpa = phba->alpa_map[0];
1099 j = 0;
1100 while (j < numalpa) {
1101 memset(un.pamap, 0, 16);
1102 for (k = 1; j < numalpa; k++) {
1103 un.pamap[k - 1] =
1104 phba->alpa_map[j + 1];
1105 j++;
1106 if (k == 16)
1107 break;
1108 }
1109 /* Link Up Event ALPA map */
1110 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05001111 KERN_WARNING,
1112 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001113 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05001114 "ALPA map Data: x%x "
1115 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001116 un.pa.wd1, un.pa.wd2,
1117 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05001118 }
1119 }
1120 }
1121 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05001122 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04001123 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05001124 (phba->sli_rev == 3))
1125 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1126 }
James Smart2e0fef82007-06-17 19:56:36 -05001127 vport->fc_myDID = phba->fc_pref_DID;
1128 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001129 }
James Smart92d7f7b2007-06-17 19:56:38 -05001130 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001131
1132 lpfc_linkup(phba);
1133 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001134 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05001135 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05001136 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04001137 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05001138 if (rc == MBX_NOT_FINISHED) {
1139 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1140 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1141 kfree(mp);
1142 mempool_free(sparam_mbox, phba->mbox_mem_pool);
1143 if (cfglink_mbox)
1144 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001145 goto out;
James Smart14691152006-12-02 13:34:28 -05001146 }
dea31012005-04-17 16:05:31 -05001147 }
1148
1149 if (cfglink_mbox) {
James Smart2e0fef82007-06-17 19:56:36 -05001150 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05001151 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05001152 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001153 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04001154 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001155 if (rc != MBX_NOT_FINISHED)
1156 return;
1157 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001158 }
James Smart92d7f7b2007-06-17 19:56:38 -05001159out:
1160 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001161 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1162 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1163 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05001164 lpfc_issue_clear_la(phba, vport);
1165 return;
dea31012005-04-17 16:05:31 -05001166}
1167
1168static void
James Smart84774a42008-08-24 21:50:06 -04001169lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05001170{
dea31012005-04-17 16:05:31 -05001171 uint32_t control;
1172 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05001173 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001174 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04001175 if (phba->sli_rev <= LPFC_SLI_REV3) {
1176 control = readl(phba->HCregaddr);
1177 control |= HC_LAINT_ENA;
1178 writel(control, phba->HCregaddr);
1179 readl(phba->HCregaddr); /* flush */
1180 }
James Smart2e0fef82007-06-17 19:56:36 -05001181 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001182}
1183
James Smart84774a42008-08-24 21:50:06 -04001184static void
1185lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1186{
1187 lpfc_linkdown(phba);
1188 lpfc_enable_la(phba);
1189 /* turn on Link Attention interrupts - no CLEAR_LA needed */
1190}
1191
1192
dea31012005-04-17 16:05:31 -05001193/*
1194 * This routine handles processing a READ_LA mailbox
1195 * command upon completion. It is setup in the LPFC_MBOXQ
1196 * as the completion routine when the command is
1197 * handed off to the SLI layer.
1198 */
1199void
James Smart2e0fef82007-06-17 19:56:36 -05001200lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001201{
James Smart2e0fef82007-06-17 19:56:36 -05001202 struct lpfc_vport *vport = pmb->vport;
1203 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05001204 READ_LA_VAR *la;
1205 MAILBOX_t *mb = &pmb->mb;
1206 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1207
James Smart0d2b6b82008-06-14 22:52:47 -04001208 /* Unblock ELS traffic */
1209 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001210 /* Check for error */
1211 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05001212 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001213 "1307 READ_LA mbox error x%x state x%x\n",
1214 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001215 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001216 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001217 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1218 }
1219
1220 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1221
1222 memcpy(&phba->alpa_map[0], mp->virt, 128);
1223
James Smart2e0fef82007-06-17 19:56:36 -05001224 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001225 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05001226 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001227 else
James Smart2e0fef82007-06-17 19:56:36 -05001228 vport->fc_flag &= ~FC_BYPASSED_MODE;
1229 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001230
dea31012005-04-17 16:05:31 -05001231 if (((phba->fc_eventTag + 1) < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05001232 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05001233 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05001234 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05001235 if (phba->fc_eventTag != 0)
1236 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001237 }
dea31012005-04-17 16:05:31 -05001238
1239 phba->fc_eventTag = la->eventTag;
James Smart84774a42008-08-24 21:50:06 -04001240 if (la->mm)
1241 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
1242 else
1243 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
dea31012005-04-17 16:05:31 -05001244
James Smart84774a42008-08-24 21:50:06 -04001245 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05001246 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05001247 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05001248 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001249 "1306 Link Up Event in loop back mode "
1250 "x%x received Data: x%x x%x x%x x%x\n",
1251 la->eventTag, phba->fc_eventTag,
1252 la->granted_AL_PA, la->UlnkSpeed,
1253 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001254 } else {
1255 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001256 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04001257 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001258 la->eventTag, phba->fc_eventTag,
1259 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04001260 phba->alpa_map[0],
1261 la->mm, la->fa,
1262 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04001263 }
James Smart92d7f7b2007-06-17 19:56:38 -05001264 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04001265 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05001266 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05001267 if (phba->link_flag & LS_LOOPBACK_MODE) {
1268 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1269 "1308 Link Down Event in loop back mode "
1270 "x%x received "
1271 "Data: x%x x%x x%x\n",
1272 la->eventTag, phba->fc_eventTag,
1273 phba->pport->port_state, vport->fc_flag);
1274 }
1275 else {
1276 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001277 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04001278 "Data: x%x x%x x%x x%x x%x\n",
1279 la->eventTag, phba->fc_eventTag,
1280 phba->pport->port_state, vport->fc_flag,
1281 la->mm, la->fa);
1282 }
1283 lpfc_mbx_issue_link_down(phba);
1284 }
1285 if (la->mm && la->attType == AT_LINK_UP) {
1286 if (phba->link_state != LPFC_LINK_DOWN) {
1287 phba->fc_stat.LinkDown++;
1288 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1289 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05001290 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001291 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05001292 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04001293 lpfc_mbx_issue_link_down(phba);
1294 } else
1295 lpfc_enable_la(phba);
1296
1297 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1298 "1310 Menlo Maint Mode Link up Event x%x rcvd "
1299 "Data: x%x x%x x%x\n",
1300 la->eventTag, phba->fc_eventTag,
1301 phba->pport->port_state, vport->fc_flag);
1302 /*
1303 * The cmnd that triggered this will be waiting for this
1304 * signal.
1305 */
1306 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
1307 if (phba->wait_4_mlo_maint_flg) {
1308 phba->wait_4_mlo_maint_flg = 0;
1309 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05001310 }
James Smart84774a42008-08-24 21:50:06 -04001311 }
1312
1313 if (la->fa) {
1314 if (la->mm)
1315 lpfc_issue_clear_la(phba, vport);
1316 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1317 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05001318 }
1319
1320lpfc_mbx_cmpl_read_la_free_mbuf:
1321 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1322 kfree(mp);
1323 mempool_free(pmb, phba->mbox_mem_pool);
1324 return;
1325}
1326
1327/*
1328 * This routine handles processing a REG_LOGIN mailbox
1329 * command upon completion. It is setup in the LPFC_MBOXQ
1330 * as the completion routine when the command is
1331 * handed off to the SLI layer.
1332 */
1333void
James Smart2e0fef82007-06-17 19:56:36 -05001334lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001335{
James Smart2e0fef82007-06-17 19:56:36 -05001336 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001337 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05001338 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001339
dea31012005-04-17 16:05:31 -05001340 pmb->context1 = NULL;
1341
1342 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05001343 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05001344 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1345 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001346 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05001347 /* decrement the node reference count held for this callback
1348 * function.
1349 */
James Smart329f9bc2007-04-25 09:53:01 -04001350 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001351
1352 return;
1353}
1354
James Smart92d7f7b2007-06-17 19:56:38 -05001355static void
1356lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1357{
1358 MAILBOX_t *mb = &pmb->mb;
1359 struct lpfc_vport *vport = pmb->vport;
1360 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1361
1362 switch (mb->mbxStatus) {
1363 case 0x0011:
1364 case 0x0020:
1365 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04001366 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1367 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
1368 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001369 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001370 }
1371 vport->unreg_vpi_cmpl = VPORT_OK;
1372 mempool_free(pmb, phba->mbox_mem_pool);
1373 /*
1374 * This shost reference might have been taken at the beginning of
1375 * lpfc_vport_delete()
1376 */
1377 if (vport->load_flag & FC_UNLOADING)
1378 scsi_host_put(shost);
1379}
1380
James Smartd7c255b2008-08-24 21:50:00 -04001381int
James Smart92d7f7b2007-06-17 19:56:38 -05001382lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1383{
1384 struct lpfc_hba *phba = vport->phba;
1385 LPFC_MBOXQ_t *mbox;
1386 int rc;
1387
1388 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1389 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04001390 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05001391
1392 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1393 mbox->vport = vport;
1394 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04001395 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001396 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001397 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1398 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001399 mempool_free(mbox, phba->mbox_mem_pool);
1400 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04001401 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05001402 }
James Smartd7c255b2008-08-24 21:50:00 -04001403 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001404}
1405
1406static void
1407lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1408{
1409 struct lpfc_vport *vport = pmb->vport;
1410 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1411 MAILBOX_t *mb = &pmb->mb;
1412
1413 switch (mb->mbxStatus) {
1414 case 0x0011:
1415 case 0x9601:
1416 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04001417 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1418 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
1419 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001420 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1421 spin_lock_irq(shost->host_lock);
1422 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1423 spin_unlock_irq(shost->host_lock);
1424 vport->fc_myDID = 0;
1425 goto out;
1426 }
James Smart92d7f7b2007-06-17 19:56:38 -05001427
1428 vport->num_disc_nodes = 0;
1429 /* go thru NPR list and issue ELS PLOGIs */
1430 if (vport->fc_npr_cnt)
1431 lpfc_els_disc_plogi(vport);
1432
1433 if (!vport->num_disc_nodes) {
1434 spin_lock_irq(shost->host_lock);
1435 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1436 spin_unlock_irq(shost->host_lock);
1437 lpfc_can_disctmo(vport);
1438 }
1439 vport->port_state = LPFC_VPORT_READY;
1440
1441out:
1442 mempool_free(pmb, phba->mbox_mem_pool);
1443 return;
1444}
1445
dea31012005-04-17 16:05:31 -05001446/*
1447 * This routine handles processing a Fabric REG_LOGIN mailbox
1448 * command upon completion. It is setup in the LPFC_MBOXQ
1449 * as the completion routine when the command is
1450 * handed off to the SLI layer.
1451 */
1452void
James Smart2e0fef82007-06-17 19:56:36 -05001453lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001454{
James Smart92d7f7b2007-06-17 19:56:38 -05001455 struct lpfc_vport *vport = pmb->vport;
James Smart2e0fef82007-06-17 19:56:36 -05001456 MAILBOX_t *mb = &pmb->mb;
1457 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05001458 struct lpfc_nodelist *ndlp;
James Smart549e55c2007-08-02 11:09:51 -04001459 struct lpfc_vport **vports;
1460 int i;
dea31012005-04-17 16:05:31 -05001461
James Smart549e55c2007-08-02 11:09:51 -04001462 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04001463 pmb->context1 = NULL;
1464 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05001465 if (mb->mbxStatus) {
1466 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1467 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001468 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001469
James Smart92d7f7b2007-06-17 19:56:38 -05001470 if (phba->fc_topology == TOPOLOGY_LOOP) {
1471 /* FLOGI failed, use loop map to make discovery list */
1472 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001473
James Smart92d7f7b2007-06-17 19:56:38 -05001474 /* Start discovery */
1475 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05001476 /* Decrement the reference count to ndlp after the
1477 * reference to the ndlp are done.
1478 */
1479 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05001480 return;
1481 }
1482
1483 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001484 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1485 "0258 Register Fabric login error: 0x%x\n",
1486 mb->mbxStatus);
James Smarte47c9092008-02-08 18:49:26 -05001487 /* Decrement the reference count to ndlp after the reference
1488 * to the ndlp are done.
1489 */
1490 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001491 return;
1492 }
1493
dea31012005-04-17 16:05:31 -05001494 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001495 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001496 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001497
James Smart2e0fef82007-06-17 19:56:36 -05001498 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart549e55c2007-08-02 11:09:51 -04001499 vports = lpfc_create_vport_work_array(phba);
1500 if (vports != NULL)
1501 for(i = 0;
James Smart09372822008-01-11 01:52:54 -05001502 i <= phba->max_vpi && vports[i] != NULL;
James Smart549e55c2007-08-02 11:09:51 -04001503 i++) {
1504 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1505 continue;
James Smart58da1ff2008-04-07 10:15:56 -04001506 if (phba->fc_topology == TOPOLOGY_LOOP) {
1507 lpfc_vport_set_state(vports[i],
1508 FC_VPORT_LINKDOWN);
1509 continue;
1510 }
James Smart549e55c2007-08-02 11:09:51 -04001511 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1512 lpfc_initial_fdisc(vports[i]);
James Smart58da1ff2008-04-07 10:15:56 -04001513 else {
James Smart549e55c2007-08-02 11:09:51 -04001514 lpfc_vport_set_state(vports[i],
1515 FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -04001516 lpfc_printf_vlog(vport, KERN_ERR,
1517 LOG_ELS,
1518 "0259 No NPIV "
1519 "Fabric support\n");
James Smart549e55c2007-08-02 11:09:51 -04001520 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001521 }
James Smart09372822008-01-11 01:52:54 -05001522 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001523 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05001524 }
1525
1526 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1527 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001528 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05001529
1530 /* Drop the reference count from the mbox at the end after
1531 * all the current reference to the ndlp have been done.
1532 */
1533 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001534 return;
1535}
1536
1537/*
1538 * This routine handles processing a NameServer REG_LOGIN mailbox
1539 * command upon completion. It is setup in the LPFC_MBOXQ
1540 * as the completion routine when the command is
1541 * handed off to the SLI layer.
1542 */
1543void
James Smart2e0fef82007-06-17 19:56:36 -05001544lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001545{
James Smart2e0fef82007-06-17 19:56:36 -05001546 MAILBOX_t *mb = &pmb->mb;
1547 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1548 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1549 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001550
1551 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05001552out:
James Smartfa4066b2008-01-11 01:53:27 -05001553 /* decrement the node reference count held for this
1554 * callback function.
1555 */
James Smart329f9bc2007-04-25 09:53:01 -04001556 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001557 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1558 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04001559 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04001560
1561 /* If no other thread is using the ndlp, free it */
1562 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05001563
James Smart92d7f7b2007-06-17 19:56:38 -05001564 if (phba->fc_topology == TOPOLOGY_LOOP) {
1565 /*
1566 * RegLogin failed, use loop map to make discovery
1567 * list
1568 */
1569 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001570
James Smart92d7f7b2007-06-17 19:56:38 -05001571 /* Start discovery */
1572 lpfc_disc_start(vport);
1573 return;
1574 }
1575 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001576 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1577 "0260 Register NameServer error: 0x%x\n",
1578 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001579 return;
1580 }
1581
1582 pmb->context1 = NULL;
1583
dea31012005-04-17 16:05:31 -05001584 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001585 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001586 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001587
James Smart2e0fef82007-06-17 19:56:36 -05001588 if (vport->port_state < LPFC_VPORT_READY) {
1589 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05001590 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1591 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1592 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1593 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1594 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1595
1596 /* Issue SCR just before NameServer GID_FT Query */
1597 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05001598 }
1599
James Smart2e0fef82007-06-17 19:56:36 -05001600 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05001601 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05001602 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05001603 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05001604 goto out;
dea31012005-04-17 16:05:31 -05001605 }
1606
James Smartfa4066b2008-01-11 01:53:27 -05001607 /* decrement the node reference count held for this
1608 * callback function.
1609 */
James Smart329f9bc2007-04-25 09:53:01 -04001610 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001611 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1612 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001613 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001614
1615 return;
1616}
1617
1618static void
James Smart2e0fef82007-06-17 19:56:36 -05001619lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001620{
James Smart2e0fef82007-06-17 19:56:36 -05001621 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1622 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05001623 struct lpfc_rport_data *rdata;
1624 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05001625 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001626
1627 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001628 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1629 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001630 rport_ids.port_id = ndlp->nlp_DID;
1631 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05001632
James Smart329f9bc2007-04-25 09:53:01 -04001633 /*
1634 * We leave our node pointer in rport->dd_data when we unregister a
1635 * FCP target port. But fc_remote_port_add zeros the space to which
1636 * rport->dd_data points. So, if we're reusing a previously
1637 * registered port, drop the reference that we took the last time we
1638 * registered the port.
1639 */
1640 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05001641 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04001642 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05001643
1644 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1645 "rport add: did:x%x flg:x%x type x%x",
1646 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1647
James Smart2e0fef82007-06-17 19:56:36 -05001648 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04001649 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05001650 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1651 "Warning: fc_remote_port_add failed\n");
1652 return;
1653 }
1654
1655 /* initialize static port data */
1656 rport->maxframe_size = ndlp->nlp_maxframe;
1657 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05001658 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04001659 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001660
1661 if (ndlp->nlp_type & NLP_FCP_TARGET)
1662 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1663 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1664 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1665
1666
1667 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1668 fc_remote_port_rolechg(rport, rport_ids.roles);
1669
James Smart071fbd3d2006-04-15 11:53:20 -04001670 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001671 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04001672 ndlp->nlp_sid = rport->scsi_target_id;
1673 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001674 return;
1675}
1676
1677static void
James Smart2e0fef82007-06-17 19:56:36 -05001678lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001679{
1680 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04001681
James Smart858c9f62007-06-17 19:56:39 -05001682 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
1683 "rport delete: did:x%x flg:x%x type x%x",
1684 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1685
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001686 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05001687
1688 return;
1689}
1690
James Smartde0c5b32007-04-25 09:52:27 -04001691static void
James Smart2e0fef82007-06-17 19:56:36 -05001692lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04001693{
James Smart2e0fef82007-06-17 19:56:36 -05001694 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1695
1696 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001697 switch (state) {
1698 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001699 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001700 break;
1701 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001702 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001703 break;
1704 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001705 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001706 break;
1707 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001708 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001709 break;
1710 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001711 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001712 break;
1713 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001714 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001715 break;
1716 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001717 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001718 break;
1719 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001720 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001721 break;
1722 }
James Smart2e0fef82007-06-17 19:56:36 -05001723 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001724}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001725
James Smartde0c5b32007-04-25 09:52:27 -04001726static void
James Smart2e0fef82007-06-17 19:56:36 -05001727lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04001728 int old_state, int new_state)
1729{
James Smart2e0fef82007-06-17 19:56:36 -05001730 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1731
James Smartde0c5b32007-04-25 09:52:27 -04001732 if (new_state == NLP_STE_UNMAPPED_NODE) {
1733 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1734 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1735 ndlp->nlp_type |= NLP_FC_NODE;
1736 }
1737 if (new_state == NLP_STE_MAPPED_NODE)
1738 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1739 if (new_state == NLP_STE_NPR_NODE)
1740 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05001741
James Smartde0c5b32007-04-25 09:52:27 -04001742 /* Transport interface */
1743 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1744 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001745 vport->phba->nport_event_cnt++;
1746 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001747 }
dea31012005-04-17 16:05:31 -05001748
James Smartde0c5b32007-04-25 09:52:27 -04001749 if (new_state == NLP_STE_MAPPED_NODE ||
1750 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05001751 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05001752 /*
1753 * Tell the fc transport about the port, if we haven't
1754 * already. If we have, and it's a scsi entity, be
1755 * sure to unblock any attached scsi devices
1756 */
1757 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001758 }
James Smartea2151b2008-09-07 11:52:10 -04001759 if ((new_state == NLP_STE_MAPPED_NODE) &&
1760 (vport->stat_data_enabled)) {
1761 /*
1762 * A new target is discovered, if there is no buffer for
1763 * statistical data collection allocate buffer.
1764 */
1765 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
1766 sizeof(struct lpfc_scsicmd_bkt),
1767 GFP_KERNEL);
1768
1769 if (!ndlp->lat_data)
1770 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
1771 "0286 lpfc_nlp_state_cleanup failed to "
1772 "allocate statistical data buffer DID "
1773 "0x%x\n", ndlp->nlp_DID);
1774 }
James Smart858c9f62007-06-17 19:56:39 -05001775 /*
1776 * if we added to Mapped list, but the remote port
1777 * registration failed or assigned a target id outside
1778 * our presentable range - move the node to the
1779 * Unmapped List
1780 */
James Smartde0c5b32007-04-25 09:52:27 -04001781 if (new_state == NLP_STE_MAPPED_NODE &&
1782 (!ndlp->rport ||
1783 ndlp->rport->scsi_target_id == -1 ||
1784 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05001785 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001786 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05001787 spin_unlock_irq(shost->host_lock);
1788 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001789 }
James Smartde0c5b32007-04-25 09:52:27 -04001790}
1791
James Smart685f0bf2007-04-25 09:53:08 -04001792static char *
1793lpfc_nlp_state_name(char *buffer, size_t size, int state)
1794{
1795 static char *states[] = {
1796 [NLP_STE_UNUSED_NODE] = "UNUSED",
1797 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1798 [NLP_STE_ADISC_ISSUE] = "ADISC",
1799 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1800 [NLP_STE_PRLI_ISSUE] = "PRLI",
1801 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1802 [NLP_STE_MAPPED_NODE] = "MAPPED",
1803 [NLP_STE_NPR_NODE] = "NPR",
1804 };
1805
James Smart311464e2007-08-02 11:10:37 -04001806 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04001807 strlcpy(buffer, states[state], size);
1808 else
1809 snprintf(buffer, size, "unknown (%d)", state);
1810 return buffer;
1811}
1812
James Smartde0c5b32007-04-25 09:52:27 -04001813void
James Smart2e0fef82007-06-17 19:56:36 -05001814lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1815 int state)
James Smartde0c5b32007-04-25 09:52:27 -04001816{
James Smart2e0fef82007-06-17 19:56:36 -05001817 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04001818 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04001819 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04001820
James Smarte8b62012007-08-02 11:10:09 -04001821 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1822 "0904 NPort state transition x%06x, %s -> %s\n",
1823 ndlp->nlp_DID,
1824 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1825 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05001826
1827 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1828 "node statechg did:x%x old:%d ste:%d",
1829 ndlp->nlp_DID, old_state, state);
1830
James Smartde0c5b32007-04-25 09:52:27 -04001831 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04001832 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001833 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001834 if (old_state == NLP_STE_UNMAPPED_NODE) {
1835 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1836 ndlp->nlp_type &= ~NLP_FC_NODE;
1837 }
1838
James Smart685f0bf2007-04-25 09:53:08 -04001839 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05001840 spin_lock_irq(shost->host_lock);
1841 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1842 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001843 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05001844 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04001845
1846 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05001847 lpfc_nlp_counters(vport, state, 1);
1848 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04001849}
1850
1851void
James Smarte47c9092008-02-08 18:49:26 -05001852lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1853{
1854 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1855
1856 if (list_empty(&ndlp->nlp_listp)) {
1857 spin_lock_irq(shost->host_lock);
1858 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1859 spin_unlock_irq(shost->host_lock);
1860 }
1861}
1862
1863void
James Smart2e0fef82007-06-17 19:56:36 -05001864lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001865{
James Smart2e0fef82007-06-17 19:56:36 -05001866 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1867
James Smart0d2b6b82008-06-14 22:52:47 -04001868 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001869 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05001870 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1871 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001872 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05001873 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05001874 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05001875 NLP_STE_UNUSED_NODE);
1876}
1877
Adrian Bunk4d9db012008-02-14 23:24:02 +02001878static void
James Smarte47c9092008-02-08 18:49:26 -05001879lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1880{
James Smart0d2b6b82008-06-14 22:52:47 -04001881 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05001882 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1883 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1884 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
1885 NLP_STE_UNUSED_NODE);
1886}
James Smart109f6ed2008-12-04 22:39:08 -05001887/**
James Smart3621a712009-04-06 18:47:14 -04001888 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05001889 * @vport: Pointer to Virtual Port object.
1890 * @ndlp: Pointer to FC node object.
1891 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04001892 *
1893 * This function is always called when node object need to be initialized.
1894 * It initializes all the fields of the node object. Although the reference
1895 * to phba from @ndlp can be obtained indirectly through it's reference to
1896 * @vport, a direct reference to phba is taken here by @ndlp. This is due
1897 * to the life-span of the @ndlp might go beyond the existence of @vport as
1898 * the final release of ndlp is determined by its reference count. And, the
1899 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05001900 **/
1901static inline void
1902lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1903 uint32_t did)
1904{
1905 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
1906 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
1907 init_timer(&ndlp->nlp_delayfunc);
1908 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
1909 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
1910 ndlp->nlp_DID = did;
1911 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04001912 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05001913 ndlp->nlp_sid = NLP_NO_SID;
1914 kref_init(&ndlp->kref);
1915 NLP_INT_NODE_ACT(ndlp);
1916 atomic_set(&ndlp->cmd_pending, 0);
1917 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
1918}
James Smarte47c9092008-02-08 18:49:26 -05001919
1920struct lpfc_nodelist *
1921lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1922 int state)
1923{
1924 struct lpfc_hba *phba = vport->phba;
1925 uint32_t did;
1926 unsigned long flags;
1927
1928 if (!ndlp)
1929 return NULL;
1930
1931 spin_lock_irqsave(&phba->ndlp_lock, flags);
1932 /* The ndlp should not be in memory free mode */
1933 if (NLP_CHK_FREE_REQ(ndlp)) {
1934 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1935 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1936 "0277 lpfc_enable_node: ndlp:x%p "
1937 "usgmap:x%x refcnt:%d\n",
1938 (void *)ndlp, ndlp->nlp_usg_map,
1939 atomic_read(&ndlp->kref.refcount));
1940 return NULL;
1941 }
1942 /* The ndlp should not already be in active mode */
1943 if (NLP_CHK_NODE_ACT(ndlp)) {
1944 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1945 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1946 "0278 lpfc_enable_node: ndlp:x%p "
1947 "usgmap:x%x refcnt:%d\n",
1948 (void *)ndlp, ndlp->nlp_usg_map,
1949 atomic_read(&ndlp->kref.refcount));
1950 return NULL;
1951 }
1952
1953 /* Keep the original DID */
1954 did = ndlp->nlp_DID;
1955
1956 /* re-initialize ndlp except of ndlp linked list pointer */
1957 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
1958 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05001959 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05001960
1961 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1962
1963 if (state != NLP_STE_UNUSED_NODE)
1964 lpfc_nlp_set_state(vport, ndlp, state);
1965
1966 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1967 "node enable: did:x%x",
1968 ndlp->nlp_DID, 0, 0);
1969 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04001970}
1971
1972void
James Smart2e0fef82007-06-17 19:56:36 -05001973lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001974{
James Smart87af33f2007-10-27 13:37:43 -04001975 /*
James Smartfa4066b2008-01-11 01:53:27 -05001976 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04001977 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05001978 * the ndlp from the vport. The ndlp marked as UNUSED on the list
1979 * until ALL other outstanding threads have completed. We check
1980 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04001981 */
James Smartfa4066b2008-01-11 01:53:27 -05001982 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1983 return;
James Smart51ef4c22007-08-02 11:10:31 -04001984 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04001985 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04001986 return;
dea31012005-04-17 16:05:31 -05001987}
1988
1989/*
1990 * Start / ReStart rescue timer for Discovery / RSCN handling
1991 */
1992void
James Smart2e0fef82007-06-17 19:56:36 -05001993lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001994{
James Smart2e0fef82007-06-17 19:56:36 -05001995 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1996 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001997 uint32_t tmo;
1998
James Smart2e0fef82007-06-17 19:56:36 -05001999 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002000 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002001 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
2002 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002003 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002004 * FC spec states we need 3 * ratov for CT requests
2005 */
2006 tmo = ((phba->fc_ratov * 3) + 3);
2007 }
dea31012005-04-17 16:05:31 -05002008
James Smart858c9f62007-06-17 19:56:39 -05002009
2010 if (!timer_pending(&vport->fc_disctmo)) {
2011 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2012 "set disc timer: tmo:x%x state:x%x flg:x%x",
2013 tmo, vport->port_state, vport->fc_flag);
2014 }
2015
James Smart2e0fef82007-06-17 19:56:36 -05002016 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
2017 spin_lock_irq(shost->host_lock);
2018 vport->fc_flag |= FC_DISC_TMO;
2019 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002020
2021 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002022 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2023 "0247 Start Discovery Timer state x%x "
2024 "Data: x%x x%lx x%x x%x\n",
2025 vport->port_state, tmo,
2026 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
2027 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002028
2029 return;
2030}
2031
2032/*
2033 * Cancel rescue timer for Discovery / RSCN handling
2034 */
2035int
James Smart2e0fef82007-06-17 19:56:36 -05002036lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002037{
James Smart2e0fef82007-06-17 19:56:36 -05002038 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002039 unsigned long iflags;
2040
James Smart858c9f62007-06-17 19:56:39 -05002041 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2042 "can disc timer: state:x%x rtry:x%x flg:x%x",
2043 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2044
dea31012005-04-17 16:05:31 -05002045 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05002046 if (vport->fc_flag & FC_DISC_TMO) {
2047 spin_lock_irqsave(shost->host_lock, iflags);
2048 vport->fc_flag &= ~FC_DISC_TMO;
2049 spin_unlock_irqrestore(shost->host_lock, iflags);
2050 del_timer_sync(&vport->fc_disctmo);
2051 spin_lock_irqsave(&vport->work_port_lock, iflags);
2052 vport->work_port_events &= ~WORKER_DISC_TMO;
2053 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05002054 }
2055
2056 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002057 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2058 "0248 Cancel Discovery Timer state x%x "
2059 "Data: x%x x%x x%x\n",
2060 vport->port_state, vport->fc_flag,
2061 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002062 return 0;
dea31012005-04-17 16:05:31 -05002063}
2064
2065/*
2066 * Check specified ring for outstanding IOCB on the SLI queue
2067 * Return true if iocb matches the specified nport
2068 */
2069int
James Smart2e0fef82007-06-17 19:56:36 -05002070lpfc_check_sli_ndlp(struct lpfc_hba *phba,
2071 struct lpfc_sli_ring *pring,
2072 struct lpfc_iocbq *iocb,
2073 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002074{
James Smart2e0fef82007-06-17 19:56:36 -05002075 struct lpfc_sli *psli = &phba->sli;
2076 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05002077 struct lpfc_vport *vport = ndlp->vport;
2078
2079 if (iocb->vport != vport)
2080 return 0;
2081
dea31012005-04-17 16:05:31 -05002082 if (pring->ringno == LPFC_ELS_RING) {
2083 switch (icmd->ulpCommand) {
2084 case CMD_GEN_REQUEST64_CR:
2085 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002086 return 1;
dea31012005-04-17 16:05:31 -05002087 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04002088 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
2089 return 1;
dea31012005-04-17 16:05:31 -05002090 case CMD_XMIT_ELS_RSP64_CX:
2091 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002092 return 1;
dea31012005-04-17 16:05:31 -05002093 }
James Smarta4bc3372006-12-02 13:34:16 -05002094 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05002095
2096 } else if (pring->ringno == psli->fcp_ring) {
2097 /* Skip match check if waiting to relogin to FCP target */
2098 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002099 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002100 return 0;
dea31012005-04-17 16:05:31 -05002101 }
2102 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002103 return 1;
dea31012005-04-17 16:05:31 -05002104 }
2105 } else if (pring->ringno == psli->next_ring) {
2106
2107 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002108 return 0;
dea31012005-04-17 16:05:31 -05002109}
2110
2111/*
2112 * Free resources / clean up outstanding I/Os
2113 * associated with nlp_rpi in the LPFC_NODELIST entry.
2114 */
2115static int
James Smart2e0fef82007-06-17 19:56:36 -05002116lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002117{
James Smart2534ba72007-04-25 09:52:20 -04002118 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002119 struct lpfc_sli *psli;
2120 struct lpfc_sli_ring *pring;
2121 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05002122 uint32_t rpi, i;
2123
James Smart92d7f7b2007-06-17 19:56:38 -05002124 lpfc_fabric_abort_nport(ndlp);
2125
dea31012005-04-17 16:05:31 -05002126 /*
2127 * Everything that matches on txcmplq will be returned
2128 * by firmware with a no rpi error.
2129 */
2130 psli = &phba->sli;
2131 rpi = ndlp->nlp_rpi;
2132 if (rpi) {
2133 /* Now process each ring */
2134 for (i = 0; i < psli->num_rings; i++) {
2135 pring = &psli->ring[i];
2136
James Smart2e0fef82007-06-17 19:56:36 -05002137 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002138 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05002139 list) {
dea31012005-04-17 16:05:31 -05002140 /*
2141 * Check to see if iocb matches the nport we are
2142 * looking for
2143 */
James Smart92d7f7b2007-06-17 19:56:38 -05002144 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
2145 ndlp))) {
dea31012005-04-17 16:05:31 -05002146 /* It matches, so deque and call compl
2147 with an error */
James Smart2534ba72007-04-25 09:52:20 -04002148 list_move_tail(&iocb->list,
2149 &completions);
dea31012005-04-17 16:05:31 -05002150 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002151 }
2152 }
James Smart2e0fef82007-06-17 19:56:36 -05002153 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002154 }
2155 }
James Smart2534ba72007-04-25 09:52:20 -04002156
James Smarta257bf92009-04-06 18:48:10 -04002157 /* Cancel all the IOCBs from the completions list */
2158 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2159 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04002160
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002161 return 0;
dea31012005-04-17 16:05:31 -05002162}
2163
2164/*
2165 * Free rpi associated with LPFC_NODELIST entry.
2166 * This routine is called from lpfc_freenode(), when we are removing
2167 * a LPFC_NODELIST entry. It is also called if the driver initiates a
2168 * LOGO that completes successfully, and we are waiting to PLOGI back
2169 * to the remote NPort. In addition, it is called after we receive
2170 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
2171 * we are waiting to PLOGI back to the remote NPort.
2172 */
2173int
James Smart2e0fef82007-06-17 19:56:36 -05002174lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002175{
James Smart2e0fef82007-06-17 19:56:36 -05002176 struct lpfc_hba *phba = vport->phba;
2177 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05002178 int rc;
2179
2180 if (ndlp->nlp_rpi) {
James Smart2e0fef82007-06-17 19:56:36 -05002181 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2182 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05002183 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05002184 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002185 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002186 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05002187 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05002188 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002189 }
dea31012005-04-17 16:05:31 -05002190 lpfc_no_rpi(phba, ndlp);
2191 ndlp->nlp_rpi = 0;
2192 return 1;
2193 }
2194 return 0;
2195}
2196
James Smart92d7f7b2007-06-17 19:56:38 -05002197void
2198lpfc_unreg_all_rpis(struct lpfc_vport *vport)
2199{
2200 struct lpfc_hba *phba = vport->phba;
2201 LPFC_MBOXQ_t *mbox;
2202 int rc;
2203
2204 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2205 if (mbox) {
2206 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
2207 mbox->vport = vport;
2208 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002209 mbox->context1 = NULL;
2210 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04002211 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05002212 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04002213
2214 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
2215 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2216 "1836 Could not issue "
2217 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05002218 }
2219}
2220
2221void
2222lpfc_unreg_default_rpis(struct lpfc_vport *vport)
2223{
2224 struct lpfc_hba *phba = vport->phba;
2225 LPFC_MBOXQ_t *mbox;
2226 int rc;
2227
2228 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2229 if (mbox) {
2230 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
2231 mbox->vport = vport;
2232 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002233 mbox->context1 = NULL;
2234 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04002235 if (rc != MBX_TIMEOUT)
2236 mempool_free(mbox, phba->mbox_mem_pool);
2237
2238 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04002239 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2240 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04002241 "unreg_did (default rpis) status %d\n",
2242 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05002243 }
2244}
2245
dea31012005-04-17 16:05:31 -05002246/*
2247 * Free resources associated with LPFC_NODELIST entry
2248 * so it can be freed.
2249 */
2250static int
James Smart2e0fef82007-06-17 19:56:36 -05002251lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002252{
James Smart2e0fef82007-06-17 19:56:36 -05002253 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2254 struct lpfc_hba *phba = vport->phba;
2255 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05002256 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05002257
2258 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04002259 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2260 "0900 Cleanup node for NPort x%x "
2261 "Data: x%x x%x x%x\n",
2262 ndlp->nlp_DID, ndlp->nlp_flag,
2263 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05002264 if (NLP_CHK_FREE_REQ(ndlp)) {
2265 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2266 "0280 lpfc_cleanup_node: ndlp:x%p "
2267 "usgmap:x%x refcnt:%d\n",
2268 (void *)ndlp, ndlp->nlp_usg_map,
2269 atomic_read(&ndlp->kref.refcount));
2270 lpfc_dequeue_node(vport, ndlp);
2271 } else {
2272 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2273 "0281 lpfc_cleanup_node: ndlp:x%p "
2274 "usgmap:x%x refcnt:%d\n",
2275 (void *)ndlp, ndlp->nlp_usg_map,
2276 atomic_read(&ndlp->kref.refcount));
2277 lpfc_disable_node(vport, ndlp);
2278 }
dea31012005-04-17 16:05:31 -05002279
dea31012005-04-17 16:05:31 -05002280 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
2281 if ((mb = phba->sli.mbox_active)) {
2282 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
2283 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
2284 mb->context2 = NULL;
2285 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2286 }
2287 }
James Smart33ccf8d2006-08-17 11:57:58 -04002288
James Smart2e0fef82007-06-17 19:56:36 -05002289 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002290 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
2291 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002292 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05002293 mp = (struct lpfc_dmabuf *) (mb->context1);
2294 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05002295 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002296 kfree(mp);
2297 }
2298 list_del(&mb->list);
2299 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002300 /* We shall not invoke the lpfc_nlp_put to decrement
2301 * the ndlp reference count as we are in the process
2302 * of lpfc_nlp_release.
2303 */
dea31012005-04-17 16:05:31 -05002304 }
2305 }
James Smart2e0fef82007-06-17 19:56:36 -05002306 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002307
James Smarte47c9092008-02-08 18:49:26 -05002308 lpfc_els_abort(phba, ndlp);
2309
James Smart2e0fef82007-06-17 19:56:36 -05002310 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04002311 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05002312 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002313
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05002314 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05002315 del_timer_sync(&ndlp->nlp_delayfunc);
2316
James Smart0d2b6b82008-06-14 22:52:47 -04002317 list_del_init(&ndlp->els_retry_evt.evt_listp);
2318 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002319
James Smart2e0fef82007-06-17 19:56:36 -05002320 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05002321
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002322 return 0;
dea31012005-04-17 16:05:31 -05002323}
2324
2325/*
2326 * Check to see if we can free the nlp back to the freelist.
2327 * If we are in the middle of using the nlp in the discovery state
2328 * machine, defer the free till we reach the end of the state machine.
2329 */
James Smart329f9bc2007-04-25 09:53:01 -04002330static void
James Smart2e0fef82007-06-17 19:56:36 -05002331lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002332{
James Smarta8adb832007-10-27 13:37:53 -04002333 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04002334 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04002335 LPFC_MBOXQ_t *mbox;
2336 int rc;
dea31012005-04-17 16:05:31 -05002337
James Smart0d2b6b82008-06-14 22:52:47 -04002338 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04002339 if (ndlp->nlp_flag & NLP_DEFER_RM && !ndlp->nlp_rpi) {
2340 /* For this case we need to cleanup the default rpi
2341 * allocated by the firmware.
2342 */
2343 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
2344 != NULL) {
2345 rc = lpfc_reg_login(phba, vport->vpi, ndlp->nlp_DID,
2346 (uint8_t *) &vport->fc_sparam, mbox, 0);
2347 if (rc) {
2348 mempool_free(mbox, phba->mbox_mem_pool);
2349 }
2350 else {
2351 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2352 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2353 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05002354 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04002355 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2356 if (rc == MBX_NOT_FINISHED) {
2357 mempool_free(mbox, phba->mbox_mem_pool);
2358 }
2359 }
2360 }
2361 }
James Smart2e0fef82007-06-17 19:56:36 -05002362 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04002363
James Smart2e0fef82007-06-17 19:56:36 -05002364 /*
James Smart92d7f7b2007-06-17 19:56:38 -05002365 * We can get here with a non-NULL ndlp->rport because when we
2366 * unregister a rport we don't break the rport/node linkage. So if we
2367 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05002368 */
James Smart92d7f7b2007-06-17 19:56:38 -05002369 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04002370 rdata = ndlp->rport->dd_data;
2371 rdata->pnode = NULL;
2372 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05002373 }
dea31012005-04-17 16:05:31 -05002374}
2375
2376static int
James Smart2e0fef82007-06-17 19:56:36 -05002377lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2378 uint32_t did)
dea31012005-04-17 16:05:31 -05002379{
James Smart2e0fef82007-06-17 19:56:36 -05002380 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05002381
2382 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002383 return 0;
dea31012005-04-17 16:05:31 -05002384
dea31012005-04-17 16:05:31 -05002385 /* First check for Direct match */
2386 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002387 return 1;
dea31012005-04-17 16:05:31 -05002388
2389 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05002390 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05002391 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002392 return 0;
dea31012005-04-17 16:05:31 -05002393 }
2394
2395 matchdid.un.word = did;
2396 ndlpdid.un.word = ndlp->nlp_DID;
2397 if (matchdid.un.b.id == ndlpdid.un.b.id) {
2398 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
2399 (mydid.un.b.area == matchdid.un.b.area)) {
2400 if ((ndlpdid.un.b.domain == 0) &&
2401 (ndlpdid.un.b.area == 0)) {
2402 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002403 return 1;
dea31012005-04-17 16:05:31 -05002404 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002405 return 0;
dea31012005-04-17 16:05:31 -05002406 }
2407
2408 matchdid.un.word = ndlp->nlp_DID;
2409 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
2410 (mydid.un.b.area == ndlpdid.un.b.area)) {
2411 if ((matchdid.un.b.domain == 0) &&
2412 (matchdid.un.b.area == 0)) {
2413 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002414 return 1;
dea31012005-04-17 16:05:31 -05002415 }
2416 }
2417 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002418 return 0;
dea31012005-04-17 16:05:31 -05002419}
2420
James Smart685f0bf2007-04-25 09:53:08 -04002421/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05002422static struct lpfc_nodelist *
2423__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002424{
James Smart2fb9bd82006-12-02 13:33:57 -05002425 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002426 uint32_t data1;
2427
James Smart2e0fef82007-06-17 19:56:36 -05002428 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2429 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04002430 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2431 ((uint32_t) ndlp->nlp_xri << 16) |
2432 ((uint32_t) ndlp->nlp_type << 8) |
2433 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04002434 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2435 "0929 FIND node DID "
2436 "Data: x%p x%x x%x x%x\n",
2437 ndlp, ndlp->nlp_DID,
2438 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04002439 return ndlp;
dea31012005-04-17 16:05:31 -05002440 }
2441 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002442
dea31012005-04-17 16:05:31 -05002443 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04002444 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2445 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05002446 return NULL;
2447}
2448
2449struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002450lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002451{
James Smart2e0fef82007-06-17 19:56:36 -05002452 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002453 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002454
James Smart2e0fef82007-06-17 19:56:36 -05002455 spin_lock_irq(shost->host_lock);
2456 ndlp = __lpfc_findnode_did(vport, did);
2457 spin_unlock_irq(shost->host_lock);
2458 return ndlp;
2459}
2460
2461struct lpfc_nodelist *
2462lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2463{
2464 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2465 struct lpfc_nodelist *ndlp;
2466
2467 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002468 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05002469 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2470 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05002471 return NULL;
2472 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05002473 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002474 if (!ndlp)
2475 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002476 lpfc_nlp_init(vport, ndlp, did);
2477 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2478 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002479 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002480 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002481 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05002482 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2483 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
2484 if (!ndlp)
2485 return NULL;
2486 spin_lock_irq(shost->host_lock);
2487 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2488 spin_unlock_irq(shost->host_lock);
2489 return ndlp;
dea31012005-04-17 16:05:31 -05002490 }
James Smarte47c9092008-02-08 18:49:26 -05002491
James Smart58da1ff2008-04-07 10:15:56 -04002492 if ((vport->fc_flag & FC_RSCN_MODE) &&
2493 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002494 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04002495 /* If we've already recieved a PLOGI from this NPort
2496 * we don't need to try to discover it again.
2497 */
2498 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
2499 return NULL;
2500
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002501 /* Since this node is marked for discovery,
2502 * delay timeout is not needed.
2503 */
James Smart0d2b6b82008-06-14 22:52:47 -04002504 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04002505 spin_lock_irq(shost->host_lock);
2506 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2507 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04002508 } else
dea31012005-04-17 16:05:31 -05002509 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002510 } else {
James Smart87af33f2007-10-27 13:37:43 -04002511 /* If we've already recieved a PLOGI from this NPort,
2512 * or we are already in the process of discovery on it,
2513 * we don't need to try to discover it again.
2514 */
James Smart685f0bf2007-04-25 09:53:08 -04002515 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04002516 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2517 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05002518 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002519 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2520 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002521 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002522 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002523 }
2524 return ndlp;
2525}
2526
2527/* Build a list of nodes to discover based on the loopmap */
2528void
James Smart2e0fef82007-06-17 19:56:36 -05002529lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002530{
James Smart2e0fef82007-06-17 19:56:36 -05002531 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002532 int j;
2533 uint32_t alpa, index;
2534
James Smart2e0fef82007-06-17 19:56:36 -05002535 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002536 return;
James Smart2e0fef82007-06-17 19:56:36 -05002537
2538 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05002539 return;
dea31012005-04-17 16:05:31 -05002540
2541 /* Check for loop map present or not */
2542 if (phba->alpa_map[0]) {
2543 for (j = 1; j <= phba->alpa_map[0]; j++) {
2544 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05002545 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05002546 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002547 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002548 }
2549 } else {
2550 /* No alpamap, so try all alpa's */
2551 for (j = 0; j < FC_MAXLOOP; j++) {
2552 /* If cfg_scan_down is set, start from highest
2553 * ALPA (0xef) to lowest (0x1).
2554 */
James Smart3de2a652007-08-02 11:09:59 -04002555 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05002556 index = j;
2557 else
2558 index = FC_MAXLOOP - j - 1;
2559 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05002560 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05002561 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002562 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002563 }
2564 }
2565 return;
2566}
2567
dea31012005-04-17 16:05:31 -05002568void
James Smart2e0fef82007-06-17 19:56:36 -05002569lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002570{
dea31012005-04-17 16:05:31 -05002571 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05002572 struct lpfc_sli *psli = &phba->sli;
2573 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2574 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2575 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2576 int rc;
2577
James Smart92d7f7b2007-06-17 19:56:38 -05002578 /*
2579 * if it's not a physical port or if we already send
2580 * clear_la then don't send it.
2581 */
2582 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04002583 (vport->port_type != LPFC_PHYSICAL_PORT) ||
2584 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05002585 return;
2586
James Smart2e0fef82007-06-17 19:56:36 -05002587 /* Link up discovery */
2588 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2589 phba->link_state = LPFC_CLEAR_LA;
2590 lpfc_clear_la(phba, mbox);
2591 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2592 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002593 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05002594 if (rc == MBX_NOT_FINISHED) {
2595 mempool_free(mbox, phba->mbox_mem_pool);
2596 lpfc_disc_flush_list(vport);
2597 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2598 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2599 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05002600 phba->link_state = LPFC_HBA_ERROR;
2601 }
2602 }
2603}
2604
2605/* Reg_vpi to tell firmware to resume normal operations */
2606void
2607lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2608{
2609 LPFC_MBOXQ_t *regvpimbox;
2610
2611 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2612 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04002613 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002614 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2615 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002616 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05002617 == MBX_NOT_FINISHED) {
2618 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002619 }
2620 }
2621}
2622
2623/* Start Link up / RSCN discovery on NPR nodes */
2624void
2625lpfc_disc_start(struct lpfc_vport *vport)
2626{
2627 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2628 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04002629 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05002630 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04002631 int did_changed;
dea31012005-04-17 16:05:31 -05002632
James Smart2e0fef82007-06-17 19:56:36 -05002633 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002634 return;
James Smart2e0fef82007-06-17 19:56:36 -05002635
2636 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05002637 clear_la_pending = 1;
2638 else
2639 clear_la_pending = 0;
2640
James Smart2e0fef82007-06-17 19:56:36 -05002641 if (vport->port_state < LPFC_VPORT_READY)
2642 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05002643
James Smart2e0fef82007-06-17 19:56:36 -05002644 lpfc_set_disctmo(vport);
2645
2646 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05002647 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002648 else
dea31012005-04-17 16:05:31 -05002649 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002650
2651 vport->fc_prevDID = vport->fc_myDID;
2652 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05002653
2654 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002655 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2656 "0202 Start Discovery hba state x%x "
2657 "Data: x%x x%x x%x\n",
2658 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
2659 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002660
2661 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002662 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05002663
2664 if (num_sent)
2665 return;
2666
James Smart92d7f7b2007-06-17 19:56:38 -05002667 /*
2668 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2669 * continue discovery.
2670 */
2671 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05002672 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04002673 !(vport->fc_flag & FC_RSCN_MODE) &&
2674 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002675 lpfc_issue_reg_vpi(phba, vport);
2676 return;
2677 }
James Smart2e0fef82007-06-17 19:56:36 -05002678
James Smart92d7f7b2007-06-17 19:56:38 -05002679 /*
2680 * For SLI2, we need to set port_state to READY and continue
2681 * discovery.
2682 */
2683 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2684 /* If we get here, there is nothing to ADISC */
2685 if (vport->port_type == LPFC_PHYSICAL_PORT)
2686 lpfc_issue_clear_la(phba, vport);
2687
2688 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05002689 vport->num_disc_nodes = 0;
2690 /* go thru NPR nodes and issue ELS PLOGIs */
2691 if (vport->fc_npr_cnt)
2692 lpfc_els_disc_plogi(vport);
2693
2694 if (!vport->num_disc_nodes) {
2695 spin_lock_irq(shost->host_lock);
2696 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2697 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002698 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05002699 }
2700 }
James Smart92d7f7b2007-06-17 19:56:38 -05002701 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002702 } else {
2703 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002704 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05002705
2706 if (num_sent)
2707 return;
2708
James Smart2e0fef82007-06-17 19:56:36 -05002709 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05002710 /* Check to see if more RSCNs came in while we
2711 * were processing this one.
2712 */
James Smart2e0fef82007-06-17 19:56:36 -05002713 if ((vport->fc_rscn_id_cnt == 0) &&
2714 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2715 spin_lock_irq(shost->host_lock);
2716 vport->fc_flag &= ~FC_RSCN_MODE;
2717 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002718 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002719 } else
James Smart2e0fef82007-06-17 19:56:36 -05002720 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05002721 }
2722 }
2723 return;
2724}
2725
2726/*
2727 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2728 * ring the match the sppecified nodelist.
2729 */
2730static void
James Smart2e0fef82007-06-17 19:56:36 -05002731lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002732{
James Smart2534ba72007-04-25 09:52:20 -04002733 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002734 struct lpfc_sli *psli;
2735 IOCB_t *icmd;
2736 struct lpfc_iocbq *iocb, *next_iocb;
2737 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002738
2739 psli = &phba->sli;
2740 pring = &psli->ring[LPFC_ELS_RING];
2741
2742 /* Error matching iocb on txq or txcmplq
2743 * First check the txq.
2744 */
James Smart2e0fef82007-06-17 19:56:36 -05002745 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002746 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2747 if (iocb->context1 != ndlp) {
2748 continue;
2749 }
2750 icmd = &iocb->iocb;
2751 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2752 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2753
James Smart2534ba72007-04-25 09:52:20 -04002754 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05002755 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002756 }
2757 }
2758
2759 /* Next check the txcmplq */
2760 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2761 if (iocb->context1 != ndlp) {
2762 continue;
2763 }
2764 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05002765 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2766 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04002767 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05002768 }
2769 }
James Smart2e0fef82007-06-17 19:56:36 -05002770 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002771
James Smarta257bf92009-04-06 18:48:10 -04002772 /* Cancel all the IOCBs from the completions list */
2773 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2774 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05002775}
2776
Adrian Bunka6ababd2007-11-05 18:07:33 +01002777static void
James Smart2e0fef82007-06-17 19:56:36 -05002778lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002779{
2780 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05002781 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002782
James Smart2e0fef82007-06-17 19:56:36 -05002783 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2784 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002785 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002786 if (!NLP_CHK_NODE_ACT(ndlp))
2787 continue;
James Smart685f0bf2007-04-25 09:53:08 -04002788 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2789 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2790 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04002791 }
dea31012005-04-17 16:05:31 -05002792 }
2793 }
dea31012005-04-17 16:05:31 -05002794}
2795
James Smart92d7f7b2007-06-17 19:56:38 -05002796void
2797lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2798{
2799 lpfc_els_flush_rscn(vport);
2800 lpfc_els_flush_cmd(vport);
2801 lpfc_disc_flush_list(vport);
2802}
2803
dea31012005-04-17 16:05:31 -05002804/*****************************************************************************/
2805/*
2806 * NAME: lpfc_disc_timeout
2807 *
2808 * FUNCTION: Fibre Channel driver discovery timeout routine.
2809 *
2810 * EXECUTION ENVIRONMENT: interrupt only
2811 *
2812 * CALLED FROM:
2813 * Timer function
2814 *
2815 * RETURNS:
2816 * none
2817 */
2818/*****************************************************************************/
2819void
2820lpfc_disc_timeout(unsigned long ptr)
2821{
James Smart2e0fef82007-06-17 19:56:36 -05002822 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2823 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04002824 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05002825 unsigned long flags = 0;
2826
2827 if (unlikely(!phba))
2828 return;
2829
James Smart5e9d9b82008-06-14 22:52:53 -04002830 spin_lock_irqsave(&vport->work_port_lock, flags);
2831 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
2832 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05002833 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04002834 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05002835
James Smart5e9d9b82008-06-14 22:52:53 -04002836 if (!tmo_posted)
2837 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002838 return;
2839}
2840
2841static void
James Smart2e0fef82007-06-17 19:56:36 -05002842lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002843{
James Smart2e0fef82007-06-17 19:56:36 -05002844 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2845 struct lpfc_hba *phba = vport->phba;
2846 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002847 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05002848 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05002849 int rc, clrlaerr = 0;
2850
James Smart2e0fef82007-06-17 19:56:36 -05002851 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05002852 return;
2853
James Smart2e0fef82007-06-17 19:56:36 -05002854 spin_lock_irq(shost->host_lock);
2855 vport->fc_flag &= ~FC_DISC_TMO;
2856 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002857
James Smart858c9f62007-06-17 19:56:39 -05002858 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2859 "disc timeout: state:x%x rtry:x%x flg:x%x",
2860 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2861
James Smart2e0fef82007-06-17 19:56:36 -05002862 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05002863
2864 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05002865 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2866 * FAN
2867 */
2868 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04002869 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
2870 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002871 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05002872 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002873 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002874 if (!NLP_CHK_NODE_ACT(ndlp))
2875 continue;
James Smart685f0bf2007-04-25 09:53:08 -04002876 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2877 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002878 if (ndlp->nlp_type & NLP_FABRIC) {
2879 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05002880 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04002881
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002882 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002883 /* Fail outstanding IO now since device
2884 * is marked for PLOGI.
2885 */
James Smart2e0fef82007-06-17 19:56:36 -05002886 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002887 }
2888 }
James Smart92d7f7b2007-06-17 19:56:38 -05002889 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05002890 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05002891 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002892 }
dea31012005-04-17 16:05:31 -05002893 break;
2894
James Smart92d7f7b2007-06-17 19:56:38 -05002895 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05002896 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05002897 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05002898 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04002899 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2900 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04002901 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05002902
2903 /* Assume no Fabric and go on with discovery.
2904 * Check for outstanding ELS FLOGI to abort.
2905 */
2906
2907 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05002908 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002909
2910 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002911 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002912 break;
2913
2914 case LPFC_FABRIC_CFG_LINK:
2915 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2916 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04002917 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2918 "0223 Timeout while waiting for "
2919 "NameServer login\n");
dea31012005-04-17 16:05:31 -05002920 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05002921 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05002922 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04002923 lpfc_els_abort(phba, ndlp);
2924
2925 /* ReStart discovery */
2926 goto restart_disc;
dea31012005-04-17 16:05:31 -05002927
2928 case LPFC_NS_QRY:
2929 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04002930 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2931 "0224 NameServer Query timeout "
2932 "Data: x%x x%x\n",
2933 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002934
James Smart92d7f7b2007-06-17 19:56:38 -05002935 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2936 /* Try it one more time */
2937 vport->fc_ns_retry++;
2938 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2939 vport->fc_ns_retry, 0);
2940 if (rc == 0)
2941 break;
dea31012005-04-17 16:05:31 -05002942 }
James Smart92d7f7b2007-06-17 19:56:38 -05002943 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002944
James Smart87af33f2007-10-27 13:37:43 -04002945restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05002946 /*
2947 * Discovery is over.
2948 * set port_state to PORT_READY if SLI2.
2949 * cmpl_reg_vpi will set port_state to READY for SLI3.
2950 */
James Smart3772a992009-05-22 14:50:54 -04002951 if (phba->sli_rev < LPFC_SLI_REV4) {
2952 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2953 lpfc_issue_reg_vpi(phba, vport);
2954 else { /* NPIV Not enabled */
2955 lpfc_issue_clear_la(phba, vport);
2956 vport->port_state = LPFC_VPORT_READY;
2957 }
dea31012005-04-17 16:05:31 -05002958 }
2959
2960 /* Setup and issue mailbox INITIALIZE LINK command */
2961 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2962 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04002963 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2964 "0206 Device Discovery "
2965 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05002966 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002967 break;
2968 }
2969
2970 lpfc_linkdown(phba);
2971 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2972 phba->cfg_link_speed);
2973 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05002974 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002975 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002976 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04002977 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05002978 if (rc == MBX_NOT_FINISHED)
2979 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2980
2981 break;
2982
2983 case LPFC_DISC_AUTH:
2984 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04002985 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2986 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002987 lpfc_disc_flush_list(vport);
2988
James Smart92d7f7b2007-06-17 19:56:38 -05002989 /*
2990 * set port_state to PORT_READY if SLI2.
2991 * cmpl_reg_vpi will set port_state to READY for SLI3.
2992 */
James Smart3772a992009-05-22 14:50:54 -04002993 if (phba->sli_rev < LPFC_SLI_REV4) {
2994 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2995 lpfc_issue_reg_vpi(phba, vport);
2996 else { /* NPIV Not enabled */
2997 lpfc_issue_clear_la(phba, vport);
2998 vport->port_state = LPFC_VPORT_READY;
2999 }
dea31012005-04-17 16:05:31 -05003000 }
3001 break;
3002
James Smart2e0fef82007-06-17 19:56:36 -05003003 case LPFC_VPORT_READY:
3004 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04003005 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3006 "0231 RSCN timeout Data: x%x "
3007 "x%x\n",
3008 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05003009
3010 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05003011 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05003012
James Smart2e0fef82007-06-17 19:56:36 -05003013 lpfc_els_flush_rscn(vport);
3014 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05003015 }
3016 break;
James Smart2e0fef82007-06-17 19:56:36 -05003017
James Smart92d7f7b2007-06-17 19:56:38 -05003018 default:
James Smarte8b62012007-08-02 11:10:09 -04003019 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04003020 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04003021 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05003022 break;
3023 }
3024
3025 switch (phba->link_state) {
3026 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05003027 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04003028 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3029 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05003030 clrlaerr = 1;
3031 break;
3032
James Smart09372822008-01-11 01:52:54 -05003033 case LPFC_LINK_UP:
3034 lpfc_issue_clear_la(phba, vport);
3035 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05003036 case LPFC_LINK_UNKNOWN:
3037 case LPFC_WARM_START:
3038 case LPFC_INIT_START:
3039 case LPFC_INIT_MBX_CMDS:
3040 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05003041 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04003042 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3043 "0230 Unexpected timeout, hba link "
3044 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05003045 clrlaerr = 1;
3046 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003047
3048 case LPFC_HBA_READY:
3049 break;
dea31012005-04-17 16:05:31 -05003050 }
3051
3052 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05003053 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05003054 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05003055 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
3056 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05003057 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05003058 }
3059
3060 return;
3061}
3062
dea31012005-04-17 16:05:31 -05003063/*
3064 * This routine handles processing a NameServer REG_LOGIN mailbox
3065 * command upon completion. It is setup in the LPFC_MBOXQ
3066 * as the completion routine when the command is
3067 * handed off to the SLI layer.
3068 */
3069void
James Smart2e0fef82007-06-17 19:56:36 -05003070lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003071{
James Smart2e0fef82007-06-17 19:56:36 -05003072 MAILBOX_t *mb = &pmb->mb;
3073 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3074 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3075 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003076
3077 pmb->context1 = NULL;
3078
dea31012005-04-17 16:05:31 -05003079 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05003080 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003081 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003082
James Smart2e0fef82007-06-17 19:56:36 -05003083 /*
3084 * Start issuing Fabric-Device Management Interface (FDMI) command to
3085 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
3086 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05003087 */
James Smart2e0fef82007-06-17 19:56:36 -05003088
James Smart3de2a652007-08-02 11:09:59 -04003089 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05003090 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
3091 else
3092 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05003093
James Smartfa4066b2008-01-11 01:53:27 -05003094 /* decrement the node reference count held for this callback
3095 * function.
3096 */
James Smart329f9bc2007-04-25 09:53:01 -04003097 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003098 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3099 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003100 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003101
3102 return;
3103}
3104
James Smart685f0bf2007-04-25 09:53:08 -04003105static int
3106lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
3107{
3108 uint16_t *rpi = param;
3109
3110 return ndlp->nlp_rpi == *rpi;
3111}
3112
3113static int
3114lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
3115{
3116 return memcmp(&ndlp->nlp_portname, param,
3117 sizeof(ndlp->nlp_portname)) == 0;
3118}
3119
Adrian Bunka6ababd2007-11-05 18:07:33 +01003120static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003121__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05003122{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04003123 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003124
James Smart2e0fef82007-06-17 19:56:36 -05003125 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04003126 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04003127 return ndlp;
3128 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04003129 return NULL;
dea31012005-04-17 16:05:31 -05003130}
3131
James Smart685f0bf2007-04-25 09:53:08 -04003132/*
3133 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05003134 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04003135 */
3136struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003137__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04003138{
James Smart2e0fef82007-06-17 19:56:36 -05003139 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04003140}
3141
James Smart488d1462006-03-07 15:02:37 -05003142/*
James Smart685f0bf2007-04-25 09:53:08 -04003143 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05003144 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05003145 */
3146struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003147lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05003148{
James Smart2e0fef82007-06-17 19:56:36 -05003149 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05003150 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05003151
James Smart2e0fef82007-06-17 19:56:36 -05003152 spin_lock_irq(shost->host_lock);
3153 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
3154 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003155 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05003156}
3157
dea31012005-04-17 16:05:31 -05003158void
James Smart2e0fef82007-06-17 19:56:36 -05003159lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3160 uint32_t did)
dea31012005-04-17 16:05:31 -05003161{
3162 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05003163
3164 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04003165 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05003166
3167 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3168 "node init: did:x%x",
3169 ndlp->nlp_DID, 0, 0);
3170
dea31012005-04-17 16:05:31 -05003171 return;
3172}
James Smart329f9bc2007-04-25 09:53:01 -04003173
James Smart98c9ea52007-10-27 13:37:33 -04003174/* This routine releases all resources associated with a specifc NPort's ndlp
3175 * and mempool_free's the nodelist.
3176 */
James Smart311464e2007-08-02 11:10:37 -04003177static void
James Smart329f9bc2007-04-25 09:53:01 -04003178lpfc_nlp_release(struct kref *kref)
3179{
James Smarte47c9092008-02-08 18:49:26 -05003180 struct lpfc_hba *phba;
3181 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04003182 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
3183 kref);
James Smart858c9f62007-06-17 19:56:39 -05003184
3185 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3186 "node release: did:x%x flg:x%x type:x%x",
3187 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3188
James Smarte47c9092008-02-08 18:49:26 -05003189 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3190 "0279 lpfc_nlp_release: ndlp:x%p "
3191 "usgmap:x%x refcnt:%d\n",
3192 (void *)ndlp, ndlp->nlp_usg_map,
3193 atomic_read(&ndlp->kref.refcount));
3194
3195 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05003196 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003197
3198 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04003199 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05003200 spin_lock_irqsave(&phba->ndlp_lock, flags);
3201 NLP_CLR_NODE_ACT(ndlp);
3202 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3203
3204 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04003205 if (NLP_CHK_FREE_REQ(ndlp)) {
3206 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04003207 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04003208 }
James Smart329f9bc2007-04-25 09:53:01 -04003209}
3210
James Smart98c9ea52007-10-27 13:37:33 -04003211/* This routine bumps the reference count for a ndlp structure to ensure
3212 * that one discovery thread won't free a ndlp while another discovery thread
3213 * is using it.
3214 */
James Smart329f9bc2007-04-25 09:53:01 -04003215struct lpfc_nodelist *
3216lpfc_nlp_get(struct lpfc_nodelist *ndlp)
3217{
James Smarte47c9092008-02-08 18:49:26 -05003218 struct lpfc_hba *phba;
3219 unsigned long flags;
3220
James Smart98c9ea52007-10-27 13:37:33 -04003221 if (ndlp) {
3222 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3223 "node get: did:x%x flg:x%x refcnt:x%x",
3224 ndlp->nlp_DID, ndlp->nlp_flag,
3225 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003226 /* The check of ndlp usage to prevent incrementing the
3227 * ndlp reference count that is in the process of being
3228 * released.
3229 */
James Smarta257bf92009-04-06 18:48:10 -04003230 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05003231 spin_lock_irqsave(&phba->ndlp_lock, flags);
3232 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
3233 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3234 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3235 "0276 lpfc_nlp_get: ndlp:x%p "
3236 "usgmap:x%x refcnt:%d\n",
3237 (void *)ndlp, ndlp->nlp_usg_map,
3238 atomic_read(&ndlp->kref.refcount));
3239 return NULL;
3240 } else
3241 kref_get(&ndlp->kref);
3242 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04003243 }
James Smart329f9bc2007-04-25 09:53:01 -04003244 return ndlp;
3245}
3246
James Smart98c9ea52007-10-27 13:37:33 -04003247/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05003248 * count goes to 0, this indicates the the associated nodelist should be
3249 * freed. Returning 1 indicates the ndlp resource has been released; on the
3250 * other hand, returning 0 indicates the ndlp resource has not been released
3251 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04003252 */
James Smart329f9bc2007-04-25 09:53:01 -04003253int
3254lpfc_nlp_put(struct lpfc_nodelist *ndlp)
3255{
James Smarte47c9092008-02-08 18:49:26 -05003256 struct lpfc_hba *phba;
3257 unsigned long flags;
3258
3259 if (!ndlp)
3260 return 1;
3261
3262 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3263 "node put: did:x%x flg:x%x refcnt:x%x",
3264 ndlp->nlp_DID, ndlp->nlp_flag,
3265 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04003266 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05003267 spin_lock_irqsave(&phba->ndlp_lock, flags);
3268 /* Check the ndlp memory free acknowledge flag to avoid the
3269 * possible race condition that kref_put got invoked again
3270 * after previous one has done ndlp memory free.
3271 */
3272 if (NLP_CHK_FREE_ACK(ndlp)) {
3273 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3274 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3275 "0274 lpfc_nlp_put: ndlp:x%p "
3276 "usgmap:x%x refcnt:%d\n",
3277 (void *)ndlp, ndlp->nlp_usg_map,
3278 atomic_read(&ndlp->kref.refcount));
3279 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003280 }
James Smarte47c9092008-02-08 18:49:26 -05003281 /* Check the ndlp inactivate log flag to avoid the possible
3282 * race condition that kref_put got invoked again after ndlp
3283 * is already in inactivating state.
3284 */
3285 if (NLP_CHK_IACT_REQ(ndlp)) {
3286 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3287 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3288 "0275 lpfc_nlp_put: ndlp:x%p "
3289 "usgmap:x%x refcnt:%d\n",
3290 (void *)ndlp, ndlp->nlp_usg_map,
3291 atomic_read(&ndlp->kref.refcount));
3292 return 1;
3293 }
3294 /* For last put, mark the ndlp usage flags to make sure no
3295 * other kref_get and kref_put on the same ndlp shall get
3296 * in between the process when the final kref_put has been
3297 * invoked on this ndlp.
3298 */
3299 if (atomic_read(&ndlp->kref.refcount) == 1) {
3300 /* Indicate ndlp is put to inactive state. */
3301 NLP_SET_IACT_REQ(ndlp);
3302 /* Acknowledge ndlp memory free has been seen. */
3303 if (NLP_CHK_FREE_REQ(ndlp))
3304 NLP_SET_FREE_ACK(ndlp);
3305 }
3306 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3307 /* Note, the kref_put returns 1 when decrementing a reference
3308 * count that was 1, it invokes the release callback function,
3309 * but it still left the reference count as 1 (not actually
3310 * performs the last decrementation). Otherwise, it actually
3311 * decrements the reference count and returns 0.
3312 */
3313 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04003314}
James Smart98c9ea52007-10-27 13:37:33 -04003315
3316/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05003317 * by any other discovery thread. This routine returns 1 if the
3318 * ndlp has been freed. A return value of 0 indicates the ndlp is
3319 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04003320 */
3321int
3322lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
3323{
3324 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3325 "node not used: did:x%x flg:x%x refcnt:x%x",
3326 ndlp->nlp_DID, ndlp->nlp_flag,
3327 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003328 if (atomic_read(&ndlp->kref.refcount) == 1)
3329 if (lpfc_nlp_put(ndlp))
3330 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003331 return 0;
3332}