blob: 311ed6dea726b49638bdcdf3d8193f4642797b56 [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
32#include "lpfc_hw.h"
James Smartea2151b2008-09-07 11:52:10 -040033#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050034#include "lpfc_disc.h"
35#include "lpfc_sli.h"
36#include "lpfc_scsi.h"
37#include "lpfc.h"
38#include "lpfc_logmsg.h"
39#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050040#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050041#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050042
43/* AlpaArray for assignment of scsid for scan-down and bind_method */
44static uint8_t lpfcAlpaArray[] = {
45 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
46 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
47 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
48 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
49 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
50 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
51 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
52 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
53 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
54 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
55 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
56 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
57 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
58};
59
James Smart2e0fef82007-06-17 19:56:36 -050060static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010061static void lpfc_disc_flush_list(struct lpfc_vport *vport);
dea31012005-04-17 16:05:31 -050062
James Smartc01f3202006-08-18 17:47:08 -040063void
64lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050065{
James Smartc01f3202006-08-18 17:47:08 -040066 struct lpfc_rport_data *rdata;
67 struct lpfc_nodelist * ndlp;
68 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050069
James Smartc01f3202006-08-18 17:47:08 -040070 rdata = rport->dd_data;
71 ndlp = rdata->pnode;
72
James Smart58da1ff2008-04-07 10:15:56 -040073 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040074 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
75 printk(KERN_ERR "Cannot find remote node"
76 " to terminate I/O Data x%x\n",
77 rport->port_id);
dea31012005-04-17 16:05:31 -050078 return;
79 }
80
James Smart2e0fef82007-06-17 19:56:36 -050081 phba = ndlp->vport->phba;
James Smart1a169682006-03-07 15:04:06 -050082
James Smart858c9f62007-06-17 19:56:39 -050083 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
84 "rport terminate: sid:x%x did:x%x flg:x%x",
85 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
86
James Smartc01f3202006-08-18 17:47:08 -040087 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040088 lpfc_sli_abort_iocb(ndlp->vport,
89 &phba->sli.ring[phba->sli.fcp_ring],
90 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040091 }
James Smartc01f3202006-08-18 17:47:08 -040092}
93
94/*
95 * This function will be called when dev_loss_tmo fire.
96 */
97void
98lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
99{
100 struct lpfc_rport_data *rdata;
101 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500102 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500103 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500104 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400105 int put_node;
106 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400107
108 rdata = rport->dd_data;
109 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400110 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400111 return;
James Smartc01f3202006-08-18 17:47:08 -0400112
James Smart858c9f62007-06-17 19:56:39 -0500113 vport = ndlp->vport;
114 phba = vport->phba;
115
116 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
117 "rport devlosscb: sid:x%x did:x%x flg:x%x",
118 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
119
James Smarta8adb832007-10-27 13:37:53 -0400120 /* Don't defer this if we are in the process of deleting the vport
121 * or unloading the driver. The unload will cleanup the node
122 * appropriately we just need to cleanup the ndlp rport info here.
123 */
124 if (vport->load_flag & FC_UNLOADING) {
125 put_node = rdata->pnode != NULL;
126 put_rport = ndlp->rport != NULL;
127 rdata->pnode = NULL;
128 ndlp->rport = NULL;
129 if (put_node)
130 lpfc_nlp_put(ndlp);
131 if (put_rport)
132 put_device(&rport->dev);
133 return;
134 }
135
136 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
137 return;
138
James Smart858c9f62007-06-17 19:56:39 -0500139 evtp = &ndlp->dev_loss_evt;
140
141 if (!list_empty(&evtp->evt_listp))
142 return;
143
144 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500145 /* We need to hold the node by incrementing the reference
146 * count until this queued work is done
147 */
148 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart5e9d9b82008-06-14 22:52:53 -0400149 if (evtp->evt_arg1) {
150 evtp->evt = LPFC_EVT_DEV_LOSS;
151 list_add_tail(&evtp->evt_listp, &phba->work_list);
152 lpfc_worker_wake_up(phba);
153 }
James Smart858c9f62007-06-17 19:56:39 -0500154 spin_unlock_irq(&phba->hbalock);
155
James Smart858c9f62007-06-17 19:56:39 -0500156 return;
157}
158
159/*
160 * This function is called from the worker thread when dev_loss_tmo
161 * expire.
162 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100163static void
James Smart858c9f62007-06-17 19:56:39 -0500164lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
165{
166 struct lpfc_rport_data *rdata;
167 struct fc_rport *rport;
168 struct lpfc_vport *vport;
169 struct lpfc_hba *phba;
170 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400171 int put_node;
172 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500173 int warn_on = 0;
174
175 rport = ndlp->rport;
176
177 if (!rport)
178 return;
179
180 rdata = rport->dd_data;
181 name = (uint8_t *) &ndlp->nlp_portname;
182 vport = ndlp->vport;
183 phba = vport->phba;
184
185 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
186 "rport devlosstmo:did:x%x type:x%x id:x%x",
187 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
188
James Smarta8adb832007-10-27 13:37:53 -0400189 /* Don't defer this if we are in the process of deleting the vport
190 * or unloading the driver. The unload will cleanup the node
191 * appropriately we just need to cleanup the ndlp rport info here.
192 */
193 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500194 if (ndlp->nlp_sid != NLP_NO_SID) {
195 /* flush the target */
196 lpfc_sli_abort_iocb(vport,
197 &phba->sli.ring[phba->sli.fcp_ring],
198 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
199 }
James Smarta8adb832007-10-27 13:37:53 -0400200 put_node = rdata->pnode != NULL;
201 put_rport = ndlp->rport != NULL;
202 rdata->pnode = NULL;
203 ndlp->rport = NULL;
204 if (put_node)
205 lpfc_nlp_put(ndlp);
206 if (put_rport)
207 put_device(&rport->dev);
208 return;
209 }
210
James Smartd7c255b2008-08-24 21:50:00 -0400211 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
212 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
213 "0284 Devloss timeout Ignored on "
214 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
215 "NPort x%x\n",
216 *name, *(name+1), *(name+2), *(name+3),
217 *(name+4), *(name+5), *(name+6), *(name+7),
218 ndlp->nlp_DID);
James Smart858c9f62007-06-17 19:56:39 -0500219 return;
James Smartd7c255b2008-08-24 21:50:00 -0400220 }
James Smart858c9f62007-06-17 19:56:39 -0500221
James Smart92d7f7b2007-06-17 19:56:38 -0500222 if (ndlp->nlp_type & NLP_FABRIC) {
223 /* We will clean up these Nodes in linkup */
224 put_node = rdata->pnode != NULL;
225 put_rport = ndlp->rport != NULL;
226 rdata->pnode = NULL;
227 ndlp->rport = NULL;
228 if (put_node)
229 lpfc_nlp_put(ndlp);
230 if (put_rport)
231 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400232 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500233 }
James Smart82085712007-04-25 09:52:41 -0400234
dea31012005-04-17 16:05:31 -0500235 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400236 warn_on = 1;
dea31012005-04-17 16:05:31 -0500237 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400238 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
239 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500240 }
James Smartc01f3202006-08-18 17:47:08 -0400241
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400242 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400243 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
244 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400245 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
246 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400247 *name, *(name+1), *(name+2), *(name+3),
248 *(name+4), *(name+5), *(name+6), *(name+7),
249 ndlp->nlp_DID, ndlp->nlp_flag,
250 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400251 } else {
James Smarte8b62012007-08-02 11:10:09 -0400252 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
253 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400254 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
255 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400256 *name, *(name+1), *(name+2), *(name+3),
257 *(name+4), *(name+5), *(name+6), *(name+7),
258 ndlp->nlp_DID, ndlp->nlp_flag,
259 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400260 }
261
James Smart87af33f2007-10-27 13:37:43 -0400262 put_node = rdata->pnode != NULL;
263 put_rport = ndlp->rport != NULL;
264 rdata->pnode = NULL;
265 ndlp->rport = NULL;
266 if (put_node)
267 lpfc_nlp_put(ndlp);
268 if (put_rport)
269 put_device(&rport->dev);
270
James Smart2e0fef82007-06-17 19:56:36 -0500271 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400272 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400273 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smarte47c9092008-02-08 18:49:26 -0500274 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
James Smart2e0fef82007-06-17 19:56:36 -0500275 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart92d7f7b2007-06-17 19:56:38 -0500276}
James Smartc01f3202006-08-18 17:47:08 -0400277
James Smartea2151b2008-09-07 11:52:10 -0400278/**
279 * lpfc_alloc_fast_evt: Allocates data structure for posting event.
280 * @phba: Pointer to hba context object.
281 *
282 * This function is called from the functions which need to post
283 * events from interrupt context. This function allocates data
284 * structure required for posting event. It also keeps track of
285 * number of events pending and prevent event storm when there are
286 * too many events.
287 **/
288struct lpfc_fast_path_event *
289lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
290 struct lpfc_fast_path_event *ret;
291
292 /* If there are lot of fast event do not exhaust memory due to this */
293 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
294 return NULL;
295
296 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
297 GFP_ATOMIC);
298 if (ret)
299 atomic_inc(&phba->fast_event_count);
300 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
301 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
302 return ret;
303}
304
305/**
306 * lpfc_free_fast_evt: Frees event data structure.
307 * @phba: Pointer to hba context object.
308 * @evt: Event object which need to be freed.
309 *
310 * This function frees the data structure required for posting
311 * events.
312 **/
313void
314lpfc_free_fast_evt(struct lpfc_hba *phba,
315 struct lpfc_fast_path_event *evt) {
316
317 atomic_dec(&phba->fast_event_count);
318 kfree(evt);
319}
320
321/**
322 * lpfc_send_fastpath_evt: Posts events generated from fast path.
323 * @phba: Pointer to hba context object.
324 * @evtp: Event data structure.
325 *
326 * This function is called from worker thread, when the interrupt
327 * context need to post an event. This function posts the event
328 * to fc transport netlink interface.
329 **/
330static void
331lpfc_send_fastpath_evt(struct lpfc_hba *phba,
332 struct lpfc_work_evt *evtp)
333{
334 unsigned long evt_category, evt_sub_category;
335 struct lpfc_fast_path_event *fast_evt_data;
336 char *evt_data;
337 uint32_t evt_data_size;
338 struct Scsi_Host *shost;
339
340 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
341 work_evt);
342
343 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
344 evt_sub_category = (unsigned long) fast_evt_data->un.
345 fabric_evt.subcategory;
346 shost = lpfc_shost_from_vport(fast_evt_data->vport);
347 if (evt_category == FC_REG_FABRIC_EVENT) {
348 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
349 evt_data = (char *) &fast_evt_data->un.read_check_error;
350 evt_data_size = sizeof(fast_evt_data->un.
351 read_check_error);
352 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500353 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400354 evt_data = (char *) &fast_evt_data->un.fabric_evt;
355 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
356 } else {
357 lpfc_free_fast_evt(phba, fast_evt_data);
358 return;
359 }
360 } else if (evt_category == FC_REG_SCSI_EVENT) {
361 switch (evt_sub_category) {
362 case LPFC_EVENT_QFULL:
363 case LPFC_EVENT_DEVBSY:
364 evt_data = (char *) &fast_evt_data->un.scsi_evt;
365 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
366 break;
367 case LPFC_EVENT_CHECK_COND:
368 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
369 evt_data_size = sizeof(fast_evt_data->un.
370 check_cond_evt);
371 break;
372 case LPFC_EVENT_VARQUEDEPTH:
373 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
374 evt_data_size = sizeof(fast_evt_data->un.
375 queue_depth_evt);
376 break;
377 default:
378 lpfc_free_fast_evt(phba, fast_evt_data);
379 return;
380 }
381 } else {
382 lpfc_free_fast_evt(phba, fast_evt_data);
383 return;
384 }
385
386 fc_host_post_vendor_event(shost,
387 fc_get_event_number(),
388 evt_data_size,
389 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500390 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400391
392 lpfc_free_fast_evt(phba, fast_evt_data);
393 return;
394}
395
dea31012005-04-17 16:05:31 -0500396static void
James Smart2e0fef82007-06-17 19:56:36 -0500397lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500398{
399 struct lpfc_work_evt *evtp = NULL;
400 struct lpfc_nodelist *ndlp;
401 int free_evt;
402
James Smart2e0fef82007-06-17 19:56:36 -0500403 spin_lock_irq(&phba->hbalock);
404 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500405 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
406 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500407 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500408 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500409 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500410 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500411 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500412 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500413 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500414 /* decrement the node reference count held
415 * for this queued work
416 */
417 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500418 break;
James Smart858c9f62007-06-17 19:56:39 -0500419 case LPFC_EVT_DEV_LOSS:
420 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500421 lpfc_dev_loss_tmo_handler(ndlp);
422 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500423 /* decrement the node reference count held for
424 * this queued work
425 */
James Smart858c9f62007-06-17 19:56:39 -0500426 lpfc_nlp_put(ndlp);
427 break;
dea31012005-04-17 16:05:31 -0500428 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500429 if (phba->link_state < LPFC_LINK_DOWN)
430 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500431 else
James Smart2e0fef82007-06-17 19:56:36 -0500432 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500433 complete((struct completion *)(evtp->evt_arg2));
434 break;
James Smart46fa3112007-04-25 09:51:45 -0400435 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500436 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400437 lpfc_offline_prep(phba);
438 *(int *)(evtp->evt_arg1) = 0;
439 complete((struct completion *)(evtp->evt_arg2));
440 break;
441 case LPFC_EVT_OFFLINE:
442 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500443 lpfc_sli_brdrestart(phba);
444 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400445 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
446 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500447 complete((struct completion *)(evtp->evt_arg2));
448 break;
449 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400450 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500451 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500452 lpfc_sli_brdreset(phba);
453 lpfc_hba_down_post(phba);
454 *(int *)(evtp->evt_arg1) =
455 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400456 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500457 complete((struct completion *)(evtp->evt_arg2));
458 break;
459 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400460 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500461 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500462 = (phba->pport->stopped)
463 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400464 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500465 complete((struct completion *)(evtp->evt_arg2));
466 break;
James Smartea2151b2008-09-07 11:52:10 -0400467 case LPFC_EVT_FASTPATH_MGMT_EVT:
468 lpfc_send_fastpath_evt(phba, evtp);
469 free_evt = 0;
470 break;
dea31012005-04-17 16:05:31 -0500471 }
472 if (free_evt)
473 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500474 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500475 }
James Smart2e0fef82007-06-17 19:56:36 -0500476 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500477
478}
479
James Smart311464e2007-08-02 11:10:37 -0400480static void
James Smart2e0fef82007-06-17 19:56:36 -0500481lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500482{
483 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500484 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400485 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400486 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400487 int i;
dea31012005-04-17 16:05:31 -0500488
James Smart2e0fef82007-06-17 19:56:36 -0500489 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500490 ha_copy = phba->work_ha;
491 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500492 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500493
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500494 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400495 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500496 lpfc_handle_eratt(phba);
497
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500498 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500499 lpfc_sli_handle_mb_event(phba);
500
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500501 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500502 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400503
James Smart549e55c2007-08-02 11:09:51 -0400504 vports = lpfc_create_vport_work_array(phba);
505 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500506 for(i = 0; i <= phba->max_vpi; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400507 /*
508 * We could have no vports in array if unloading, so if
509 * this happens then just use the pport
510 */
511 if (vports[i] == NULL && i == 0)
512 vport = phba->pport;
513 else
514 vport = vports[i];
515 if (vport == NULL)
516 break;
James Smart58da1ff2008-04-07 10:15:56 -0400517 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400518 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400519 vport->work_port_events &= ~work_port_events;
520 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400521 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400522 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400523 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400524 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400525 if (work_port_events & WORKER_HB_TMO)
526 lpfc_hb_timeout_handler(phba);
527 if (work_port_events & WORKER_MBOX_TMO)
528 lpfc_mbox_timeout_handler(phba);
529 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
530 lpfc_unblock_fabric_iocbs(phba);
531 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400532 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400533 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
534 lpfc_ramp_down_queue_handler(phba);
535 if (work_port_events & WORKER_RAMP_UP_QUEUE)
536 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500537 }
James Smart09372822008-01-11 01:52:54 -0500538 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500539
James Smart858c9f62007-06-17 19:56:39 -0500540 pring = &phba->sli.ring[LPFC_ELS_RING];
541 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
542 status >>= (4*LPFC_ELS_RING);
543 if ((status & HA_RXMASK)
544 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400545 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500546 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400547 /* Set the lpfc data pending flag */
548 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500549 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400550 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500551 lpfc_sli_handle_slow_ring_event(phba, pring,
552 (status &
553 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500554 }
555 /*
556 * Turn on Ring interrupts
557 */
558 spin_lock_irq(&phba->hbalock);
559 control = readl(phba->HCregaddr);
560 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
James Smarta58cbd52007-08-02 11:09:43 -0400561 lpfc_debugfs_slow_ring_trc(phba,
562 "WRK Enable ring: cntl:x%x hacopy:x%x",
563 control, ha_copy, 0);
564
James Smart858c9f62007-06-17 19:56:39 -0500565 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -0500566 writel(control, phba->HCregaddr);
567 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500568 }
James Smarta58cbd52007-08-02 11:09:43 -0400569 else {
570 lpfc_debugfs_slow_ring_trc(phba,
571 "WRK Ring ok: cntl:x%x hacopy:x%x",
572 control, ha_copy, 0);
573 }
James Smart858c9f62007-06-17 19:56:39 -0500574 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500575 }
James Smart2e0fef82007-06-17 19:56:36 -0500576 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500577}
578
dea31012005-04-17 16:05:31 -0500579int
580lpfc_do_work(void *p)
581{
582 struct lpfc_hba *phba = p;
583 int rc;
dea31012005-04-17 16:05:31 -0500584
585 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400586 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500587
James Smart3a55b532008-12-04 22:38:54 -0500588 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400589 /* wait and check worker queue activities */
590 rc = wait_event_interruptible(phba->work_waitq,
591 (test_and_clear_bit(LPFC_DATA_READY,
592 &phba->data_flags)
593 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500594 /* Signal wakeup shall terminate the worker thread */
595 if (rc) {
596 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
597 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500598 break;
James Smart3a55b532008-12-04 22:38:54 -0500599 }
dea31012005-04-17 16:05:31 -0500600
James Smart5e9d9b82008-06-14 22:52:53 -0400601 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500602 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500603 }
James Smart3a55b532008-12-04 22:38:54 -0500604 phba->worker_thread = NULL;
605 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
606 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500607 return 0;
608}
609
610/*
611 * This is only called to handle FC worker events. Since this a rare
612 * occurance, we allocate a struct lpfc_work_evt structure here instead of
613 * embedding it in the IOCB.
614 */
615int
James Smart2e0fef82007-06-17 19:56:36 -0500616lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500617 uint32_t evt)
618{
619 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500620 unsigned long flags;
dea31012005-04-17 16:05:31 -0500621
622 /*
623 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
624 * be queued to worker thread for processing
625 */
James Smart92d7f7b2007-06-17 19:56:38 -0500626 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500627 if (!evtp)
628 return 0;
629
630 evtp->evt_arg1 = arg1;
631 evtp->evt_arg2 = arg2;
632 evtp->evt = evt;
633
James Smarted957682007-06-17 19:56:37 -0500634 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400635 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500636 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500637
James Smart5e9d9b82008-06-14 22:52:53 -0400638 lpfc_worker_wake_up(phba);
639
dea31012005-04-17 16:05:31 -0500640 return 1;
641}
642
James Smart92d7f7b2007-06-17 19:56:38 -0500643void
644lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
645{
James Smart09372822008-01-11 01:52:54 -0500646 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500647 struct lpfc_hba *phba = vport->phba;
648 struct lpfc_nodelist *ndlp, *next_ndlp;
649 int rc;
650
651 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500652 if (!NLP_CHK_NODE_ACT(ndlp))
653 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500654 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
655 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400656 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
657 ((vport->port_type == LPFC_NPIV_PORT) &&
658 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500659 lpfc_unreg_rpi(vport, ndlp);
660
661 /* Leave Fabric nodes alone on link down */
662 if (!remove && ndlp->nlp_type & NLP_FABRIC)
663 continue;
664 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
665 remove
666 ? NLP_EVT_DEVICE_RM
667 : NLP_EVT_DEVICE_RECOVERY);
668 }
669 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
670 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500671 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500672 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500673 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500674 }
675}
676
James Smart87af33f2007-10-27 13:37:43 -0400677void
James Smart98c9ea52007-10-27 13:37:33 -0400678lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500679{
James Smart92d7f7b2007-06-17 19:56:38 -0500680 /* Cleanup any outstanding RSCN activity */
681 lpfc_els_flush_rscn(vport);
682
683 /* Cleanup any outstanding ELS commands */
684 lpfc_els_flush_cmd(vport);
685
686 lpfc_cleanup_rpis(vport, 0);
687
James Smart92d7f7b2007-06-17 19:56:38 -0500688 /* Turn off discovery timer if its running */
689 lpfc_can_disctmo(vport);
690}
691
James Smart98c9ea52007-10-27 13:37:33 -0400692static void
693lpfc_linkdown_port(struct lpfc_vport *vport)
694{
695 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
696
697 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
698
699 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
700 "Link Down: state:x%x rtry:x%x flg:x%x",
701 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
702
703 lpfc_port_link_failure(vport);
704
705}
706
dea31012005-04-17 16:05:31 -0500707int
James Smart685f0bf2007-04-25 09:53:08 -0400708lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500709{
James Smart2e0fef82007-06-17 19:56:36 -0500710 struct lpfc_vport *vport = phba->pport;
711 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400712 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400713 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400714 int i;
dea31012005-04-17 16:05:31 -0500715
James Smart3163f722008-02-08 18:50:25 -0500716 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500717 return 0;
James Smart2e0fef82007-06-17 19:56:36 -0500718 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500719 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500720 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500721 phba->pport->fc_flag &= ~FC_LBIT;
722 }
James Smart2e0fef82007-06-17 19:56:36 -0500723 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400724 vports = lpfc_create_vport_work_array(phba);
725 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500726 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400727 /* Issue a LINK DOWN event to all nodes */
728 lpfc_linkdown_port(vports[i]);
729 }
James Smart09372822008-01-11 01:52:54 -0500730 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500731 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500732 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
733 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500734 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500735 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500736 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400737 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500738 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500739 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500740 }
741 }
742
dea31012005-04-17 16:05:31 -0500743 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500744 if (phba->pport->fc_flag & FC_PT2PT) {
745 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500746 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
747 if (mb) {
dea31012005-04-17 16:05:31 -0500748 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500749 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500750 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400751 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500752 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500753 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500754 }
755 }
James Smart2e0fef82007-06-17 19:56:36 -0500756 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500757 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500758 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500759 }
James Smart2e0fef82007-06-17 19:56:36 -0500760
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500761 return 0;
dea31012005-04-17 16:05:31 -0500762}
763
James Smart92d7f7b2007-06-17 19:56:38 -0500764static void
765lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500766{
James Smart92d7f7b2007-06-17 19:56:38 -0500767 struct lpfc_nodelist *ndlp;
768
769 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500770 if (!NLP_CHK_NODE_ACT(ndlp))
771 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500772 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
773 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500774 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500775 /* On Linkup its safe to clean up the ndlp
776 * from Fabric connections.
777 */
James Smart92d7f7b2007-06-17 19:56:38 -0500778 if (ndlp->nlp_DID != Fabric_DID)
779 lpfc_unreg_rpi(vport, ndlp);
780 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
781 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500782 /* Fail outstanding IO now since device is
783 * marked for PLOGI.
784 */
James Smart92d7f7b2007-06-17 19:56:38 -0500785 lpfc_unreg_rpi(vport, ndlp);
786 }
787 }
788}
789
790static void
791lpfc_linkup_port(struct lpfc_vport *vport)
792{
793 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500794 struct lpfc_hba *phba = vport->phba;
795
796 if ((vport->load_flag & FC_UNLOADING) != 0)
797 return;
798
James Smart858c9f62007-06-17 19:56:39 -0500799 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
800 "Link Up: top:x%x speed:x%x flg:x%x",
801 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
802
James Smart92d7f7b2007-06-17 19:56:38 -0500803 /* If NPIV is not enabled, only bring the physical port up */
804 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
805 (vport != phba->pport))
806 return;
dea31012005-04-17 16:05:31 -0500807
James Smart2e0fef82007-06-17 19:56:36 -0500808 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400809
James Smart2e0fef82007-06-17 19:56:36 -0500810 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500811 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
812 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
813 vport->fc_flag |= FC_NDISC_ACTIVE;
814 vport->fc_ns_retry = 0;
815 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500816
James Smart92d7f7b2007-06-17 19:56:38 -0500817 if (vport->fc_flag & FC_LBIT)
818 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500819
James Smart92d7f7b2007-06-17 19:56:38 -0500820}
821
822static int
823lpfc_linkup(struct lpfc_hba *phba)
824{
James Smart549e55c2007-08-02 11:09:51 -0400825 struct lpfc_vport **vports;
826 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500827
828 phba->link_state = LPFC_LINK_UP;
829
830 /* Unblock fabric iocbs if they are blocked */
831 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
832 del_timer_sync(&phba->fabric_block_timer);
833
James Smart549e55c2007-08-02 11:09:51 -0400834 vports = lpfc_create_vport_work_array(phba);
835 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500836 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400837 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500838 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500839 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
840 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500841
842 return 0;
843}
844
845/*
846 * This routine handles processing a CLEAR_LA mailbox
847 * command upon completion. It is setup in the LPFC_MBOXQ
848 * as the completion routine when the command is
849 * handed off to the SLI layer.
850 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100851static void
James Smart2e0fef82007-06-17 19:56:36 -0500852lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500853{
James Smart2e0fef82007-06-17 19:56:36 -0500854 struct lpfc_vport *vport = pmb->vport;
855 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
856 struct lpfc_sli *psli = &phba->sli;
857 MAILBOX_t *mb = &pmb->mb;
dea31012005-04-17 16:05:31 -0500858 uint32_t control;
859
dea31012005-04-17 16:05:31 -0500860 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500861 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500862 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
863 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
864
865 /* Check for error */
866 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500867 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400868 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
869 "0320 CLEAR_LA mbxStatus error x%x hba "
870 "state x%x\n",
871 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500872 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500873 goto out;
874 }
875
James Smart92d7f7b2007-06-17 19:56:38 -0500876 if (vport->port_type == LPFC_PHYSICAL_PORT)
877 phba->link_state = LPFC_HBA_READY;
878
879 spin_lock_irq(&phba->hbalock);
880 psli->sli_flag |= LPFC_PROCESS_LA;
881 control = readl(phba->HCregaddr);
882 control |= HC_LAINT_ENA;
883 writel(control, phba->HCregaddr);
884 readl(phba->HCregaddr); /* flush */
885 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500886 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500887 return;
dea31012005-04-17 16:05:31 -0500888
dea31012005-04-17 16:05:31 -0500889out:
890 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400891 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
892 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500893 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500894
James Smart2e0fef82007-06-17 19:56:36 -0500895 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400896 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500897 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500898
James Smart2e0fef82007-06-17 19:56:36 -0500899 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500900
901 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500902
903 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500904 psli->sli_flag |= LPFC_PROCESS_LA;
905 control = readl(phba->HCregaddr);
906 control |= HC_LAINT_ENA;
907 writel(control, phba->HCregaddr);
908 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500909 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500910
911 return;
912}
913
James Smart2e0fef82007-06-17 19:56:36 -0500914
dea31012005-04-17 16:05:31 -0500915static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500916lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500917{
James Smart2e0fef82007-06-17 19:56:36 -0500918 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500919
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500920 if (pmb->mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500921 goto out;
dea31012005-04-17 16:05:31 -0500922
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500923 mempool_free(pmb, phba->mbox_mem_pool);
924
925 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500926 vport->fc_flag & FC_PUBLIC_LOOP &&
927 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500928 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500929 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500930 * LPFC_LOCAL_CFG_LINK while waiting for FAN
931 */
James Smart2e0fef82007-06-17 19:56:36 -0500932 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500933 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500934 }
dea31012005-04-17 16:05:31 -0500935
James Smart2e0fef82007-06-17 19:56:36 -0500936 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500937 * LPFC_FLOGI while waiting for FLOGI cmpl
938 */
James Smart92d7f7b2007-06-17 19:56:38 -0500939 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500940 lpfc_initial_flogi(vport);
941 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500942 return;
dea31012005-04-17 16:05:31 -0500943
944out:
James Smarte8b62012007-08-02 11:10:09 -0400945 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
946 "0306 CONFIG_LINK mbxStatus error x%x "
947 "HBA state x%x\n",
948 pmb->mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500949 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500950
951 lpfc_linkdown(phba);
952
James Smarte8b62012007-08-02 11:10:09 -0400953 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
954 "0200 CONFIG_LINK bad hba state x%x\n",
955 vport->port_state);
dea31012005-04-17 16:05:31 -0500956
James Smart92d7f7b2007-06-17 19:56:38 -0500957 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500958 return;
959}
960
961static void
James Smart2e0fef82007-06-17 19:56:36 -0500962lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500963{
dea31012005-04-17 16:05:31 -0500964 MAILBOX_t *mb = &pmb->mb;
965 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500966 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500967
968
969 /* Check for error */
970 if (mb->mbxStatus) {
971 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400972 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
973 "0319 READ_SPARAM mbxStatus error x%x "
974 "hba state x%x>\n",
975 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -0500976 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -0500977 goto out;
978 }
979
James Smart2e0fef82007-06-17 19:56:36 -0500980 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -0500981 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -0500982 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500983 u64_to_wwn(phba->cfg_soft_wwnn,
984 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400985 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500986 u64_to_wwn(phba->cfg_soft_wwpn,
987 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -0500988 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
989 sizeof(vport->fc_nodename));
990 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
991 sizeof(vport->fc_portname));
992 if (vport->port_type == LPFC_PHYSICAL_PORT) {
993 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
994 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
995 }
996
dea31012005-04-17 16:05:31 -0500997 lpfc_mbuf_free(phba, mp->virt, mp->phys);
998 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -0500999 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001000 return;
1001
1002out:
1003 pmb->context1 = NULL;
1004 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1005 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05001006 lpfc_issue_clear_la(phba, vport);
1007 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001008 return;
1009}
1010
1011static void
James Smart92d7f7b2007-06-17 19:56:38 -05001012lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05001013{
James Smart92d7f7b2007-06-17 19:56:38 -05001014 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -05001015 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
James Smart2e0fef82007-06-17 19:56:36 -05001016 int i;
James Smart14691152006-12-02 13:34:28 -05001017 struct lpfc_dmabuf *mp;
1018 int rc;
1019
dea31012005-04-17 16:05:31 -05001020 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1021 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1022
James Smart92d7f7b2007-06-17 19:56:38 -05001023 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001024 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05001025 case LA_1GHZ_LINK:
1026 phba->fc_linkspeed = LA_1GHZ_LINK;
1027 break;
1028 case LA_2GHZ_LINK:
1029 phba->fc_linkspeed = LA_2GHZ_LINK;
1030 break;
1031 case LA_4GHZ_LINK:
1032 phba->fc_linkspeed = LA_4GHZ_LINK;
1033 break;
1034 case LA_8GHZ_LINK:
1035 phba->fc_linkspeed = LA_8GHZ_LINK;
1036 break;
1037 default:
1038 phba->fc_linkspeed = LA_UNKNW_LINK;
1039 break;
dea31012005-04-17 16:05:31 -05001040 }
1041
1042 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05001043 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05001044
1045 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05001046 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05001047
James Smart495a7142008-06-14 22:52:59 -04001048 if (phba->cfg_enable_npiv)
1049 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1050 "1309 Link Up Event npiv not supported in loop "
1051 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001052 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05001053 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05001054 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001055
James Smart2e0fef82007-06-17 19:56:36 -05001056 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05001057 i = la->un.lilpBde64.tus.f.bdeSize;
1058
1059 if (i == 0) {
1060 phba->alpa_map[0] = 0;
1061 } else {
James Smarte8b62012007-08-02 11:10:09 -04001062 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05001063 int numalpa, j, k;
1064 union {
1065 uint8_t pamap[16];
1066 struct {
1067 uint32_t wd1;
1068 uint32_t wd2;
1069 uint32_t wd3;
1070 uint32_t wd4;
1071 } pa;
1072 } un;
1073 numalpa = phba->alpa_map[0];
1074 j = 0;
1075 while (j < numalpa) {
1076 memset(un.pamap, 0, 16);
1077 for (k = 1; j < numalpa; k++) {
1078 un.pamap[k - 1] =
1079 phba->alpa_map[j + 1];
1080 j++;
1081 if (k == 16)
1082 break;
1083 }
1084 /* Link Up Event ALPA map */
1085 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05001086 KERN_WARNING,
1087 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001088 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05001089 "ALPA map Data: x%x "
1090 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001091 un.pa.wd1, un.pa.wd2,
1092 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05001093 }
1094 }
1095 }
1096 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05001097 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04001098 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05001099 (phba->sli_rev == 3))
1100 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1101 }
James Smart2e0fef82007-06-17 19:56:36 -05001102 vport->fc_myDID = phba->fc_pref_DID;
1103 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001104 }
James Smart92d7f7b2007-06-17 19:56:38 -05001105 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001106
1107 lpfc_linkup(phba);
1108 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001109 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05001110 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05001111 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04001112 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05001113 if (rc == MBX_NOT_FINISHED) {
1114 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1115 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1116 kfree(mp);
1117 mempool_free(sparam_mbox, phba->mbox_mem_pool);
1118 if (cfglink_mbox)
1119 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001120 goto out;
James Smart14691152006-12-02 13:34:28 -05001121 }
dea31012005-04-17 16:05:31 -05001122 }
1123
1124 if (cfglink_mbox) {
James Smart2e0fef82007-06-17 19:56:36 -05001125 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05001126 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05001127 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001128 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04001129 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001130 if (rc != MBX_NOT_FINISHED)
1131 return;
1132 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001133 }
James Smart92d7f7b2007-06-17 19:56:38 -05001134out:
1135 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001136 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1137 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1138 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05001139 lpfc_issue_clear_la(phba, vport);
1140 return;
dea31012005-04-17 16:05:31 -05001141}
1142
1143static void
James Smart84774a42008-08-24 21:50:06 -04001144lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05001145{
dea31012005-04-17 16:05:31 -05001146 uint32_t control;
1147 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05001148 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001149 psli->sli_flag |= LPFC_PROCESS_LA;
1150 control = readl(phba->HCregaddr);
1151 control |= HC_LAINT_ENA;
1152 writel(control, phba->HCregaddr);
1153 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001154 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001155}
1156
James Smart84774a42008-08-24 21:50:06 -04001157static void
1158lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1159{
1160 lpfc_linkdown(phba);
1161 lpfc_enable_la(phba);
1162 /* turn on Link Attention interrupts - no CLEAR_LA needed */
1163}
1164
1165
dea31012005-04-17 16:05:31 -05001166/*
1167 * This routine handles processing a READ_LA mailbox
1168 * command upon completion. It is setup in the LPFC_MBOXQ
1169 * as the completion routine when the command is
1170 * handed off to the SLI layer.
1171 */
1172void
James Smart2e0fef82007-06-17 19:56:36 -05001173lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001174{
James Smart2e0fef82007-06-17 19:56:36 -05001175 struct lpfc_vport *vport = pmb->vport;
1176 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05001177 READ_LA_VAR *la;
1178 MAILBOX_t *mb = &pmb->mb;
1179 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1180
James Smart0d2b6b82008-06-14 22:52:47 -04001181 /* Unblock ELS traffic */
1182 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001183 /* Check for error */
1184 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05001185 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001186 "1307 READ_LA mbox error x%x state x%x\n",
1187 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001188 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001189 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001190 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1191 }
1192
1193 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1194
1195 memcpy(&phba->alpa_map[0], mp->virt, 128);
1196
James Smart2e0fef82007-06-17 19:56:36 -05001197 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001198 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05001199 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001200 else
James Smart2e0fef82007-06-17 19:56:36 -05001201 vport->fc_flag &= ~FC_BYPASSED_MODE;
1202 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001203
dea31012005-04-17 16:05:31 -05001204 if (((phba->fc_eventTag + 1) < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05001205 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05001206 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05001207 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05001208 if (phba->fc_eventTag != 0)
1209 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001210 }
dea31012005-04-17 16:05:31 -05001211
1212 phba->fc_eventTag = la->eventTag;
James Smart84774a42008-08-24 21:50:06 -04001213 if (la->mm)
1214 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
1215 else
1216 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
dea31012005-04-17 16:05:31 -05001217
James Smart84774a42008-08-24 21:50:06 -04001218 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05001219 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05001220 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05001221 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001222 "1306 Link Up Event in loop back mode "
1223 "x%x received Data: x%x x%x x%x x%x\n",
1224 la->eventTag, phba->fc_eventTag,
1225 la->granted_AL_PA, la->UlnkSpeed,
1226 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001227 } else {
1228 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001229 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04001230 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001231 la->eventTag, phba->fc_eventTag,
1232 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04001233 phba->alpa_map[0],
1234 la->mm, la->fa,
1235 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04001236 }
James Smart92d7f7b2007-06-17 19:56:38 -05001237 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04001238 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05001239 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05001240 if (phba->link_flag & LS_LOOPBACK_MODE) {
1241 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1242 "1308 Link Down Event in loop back mode "
1243 "x%x received "
1244 "Data: x%x x%x x%x\n",
1245 la->eventTag, phba->fc_eventTag,
1246 phba->pport->port_state, vport->fc_flag);
1247 }
1248 else {
1249 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001250 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04001251 "Data: x%x x%x x%x x%x x%x\n",
1252 la->eventTag, phba->fc_eventTag,
1253 phba->pport->port_state, vport->fc_flag,
1254 la->mm, la->fa);
1255 }
1256 lpfc_mbx_issue_link_down(phba);
1257 }
1258 if (la->mm && la->attType == AT_LINK_UP) {
1259 if (phba->link_state != LPFC_LINK_DOWN) {
1260 phba->fc_stat.LinkDown++;
1261 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1262 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05001263 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001264 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05001265 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04001266 lpfc_mbx_issue_link_down(phba);
1267 } else
1268 lpfc_enable_la(phba);
1269
1270 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1271 "1310 Menlo Maint Mode Link up Event x%x rcvd "
1272 "Data: x%x x%x x%x\n",
1273 la->eventTag, phba->fc_eventTag,
1274 phba->pport->port_state, vport->fc_flag);
1275 /*
1276 * The cmnd that triggered this will be waiting for this
1277 * signal.
1278 */
1279 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
1280 if (phba->wait_4_mlo_maint_flg) {
1281 phba->wait_4_mlo_maint_flg = 0;
1282 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05001283 }
James Smart84774a42008-08-24 21:50:06 -04001284 }
1285
1286 if (la->fa) {
1287 if (la->mm)
1288 lpfc_issue_clear_la(phba, vport);
1289 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1290 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05001291 }
1292
1293lpfc_mbx_cmpl_read_la_free_mbuf:
1294 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1295 kfree(mp);
1296 mempool_free(pmb, phba->mbox_mem_pool);
1297 return;
1298}
1299
1300/*
1301 * This routine handles processing a REG_LOGIN mailbox
1302 * command upon completion. It is setup in the LPFC_MBOXQ
1303 * as the completion routine when the command is
1304 * handed off to the SLI layer.
1305 */
1306void
James Smart2e0fef82007-06-17 19:56:36 -05001307lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001308{
James Smart2e0fef82007-06-17 19:56:36 -05001309 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001310 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05001311 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001312
dea31012005-04-17 16:05:31 -05001313 pmb->context1 = NULL;
1314
1315 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05001316 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05001317 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1318 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001319 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05001320 /* decrement the node reference count held for this callback
1321 * function.
1322 */
James Smart329f9bc2007-04-25 09:53:01 -04001323 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001324
1325 return;
1326}
1327
James Smart92d7f7b2007-06-17 19:56:38 -05001328static void
1329lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1330{
1331 MAILBOX_t *mb = &pmb->mb;
1332 struct lpfc_vport *vport = pmb->vport;
1333 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1334
1335 switch (mb->mbxStatus) {
1336 case 0x0011:
1337 case 0x0020:
1338 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04001339 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1340 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
1341 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001342 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001343 }
1344 vport->unreg_vpi_cmpl = VPORT_OK;
1345 mempool_free(pmb, phba->mbox_mem_pool);
1346 /*
1347 * This shost reference might have been taken at the beginning of
1348 * lpfc_vport_delete()
1349 */
1350 if (vport->load_flag & FC_UNLOADING)
1351 scsi_host_put(shost);
1352}
1353
James Smartd7c255b2008-08-24 21:50:00 -04001354int
James Smart92d7f7b2007-06-17 19:56:38 -05001355lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1356{
1357 struct lpfc_hba *phba = vport->phba;
1358 LPFC_MBOXQ_t *mbox;
1359 int rc;
1360
1361 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1362 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04001363 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05001364
1365 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1366 mbox->vport = vport;
1367 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04001368 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001369 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001370 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1371 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001372 mempool_free(mbox, phba->mbox_mem_pool);
1373 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04001374 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05001375 }
James Smartd7c255b2008-08-24 21:50:00 -04001376 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001377}
1378
1379static void
1380lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1381{
1382 struct lpfc_vport *vport = pmb->vport;
1383 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1384 MAILBOX_t *mb = &pmb->mb;
1385
1386 switch (mb->mbxStatus) {
1387 case 0x0011:
1388 case 0x9601:
1389 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04001390 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1391 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
1392 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001393 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1394 spin_lock_irq(shost->host_lock);
1395 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1396 spin_unlock_irq(shost->host_lock);
1397 vport->fc_myDID = 0;
1398 goto out;
1399 }
James Smart92d7f7b2007-06-17 19:56:38 -05001400
1401 vport->num_disc_nodes = 0;
1402 /* go thru NPR list and issue ELS PLOGIs */
1403 if (vport->fc_npr_cnt)
1404 lpfc_els_disc_plogi(vport);
1405
1406 if (!vport->num_disc_nodes) {
1407 spin_lock_irq(shost->host_lock);
1408 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1409 spin_unlock_irq(shost->host_lock);
1410 lpfc_can_disctmo(vport);
1411 }
1412 vport->port_state = LPFC_VPORT_READY;
1413
1414out:
1415 mempool_free(pmb, phba->mbox_mem_pool);
1416 return;
1417}
1418
dea31012005-04-17 16:05:31 -05001419/*
1420 * This routine handles processing a Fabric REG_LOGIN mailbox
1421 * command upon completion. It is setup in the LPFC_MBOXQ
1422 * as the completion routine when the command is
1423 * handed off to the SLI layer.
1424 */
1425void
James Smart2e0fef82007-06-17 19:56:36 -05001426lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001427{
James Smart92d7f7b2007-06-17 19:56:38 -05001428 struct lpfc_vport *vport = pmb->vport;
James Smart2e0fef82007-06-17 19:56:36 -05001429 MAILBOX_t *mb = &pmb->mb;
1430 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05001431 struct lpfc_nodelist *ndlp;
James Smart549e55c2007-08-02 11:09:51 -04001432 struct lpfc_vport **vports;
1433 int i;
dea31012005-04-17 16:05:31 -05001434
James Smart549e55c2007-08-02 11:09:51 -04001435 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04001436 pmb->context1 = NULL;
1437 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05001438 if (mb->mbxStatus) {
1439 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1440 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001441 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001442
James Smart92d7f7b2007-06-17 19:56:38 -05001443 if (phba->fc_topology == TOPOLOGY_LOOP) {
1444 /* FLOGI failed, use loop map to make discovery list */
1445 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001446
James Smart92d7f7b2007-06-17 19:56:38 -05001447 /* Start discovery */
1448 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05001449 /* Decrement the reference count to ndlp after the
1450 * reference to the ndlp are done.
1451 */
1452 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05001453 return;
1454 }
1455
1456 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001457 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1458 "0258 Register Fabric login error: 0x%x\n",
1459 mb->mbxStatus);
James Smarte47c9092008-02-08 18:49:26 -05001460 /* Decrement the reference count to ndlp after the reference
1461 * to the ndlp are done.
1462 */
1463 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001464 return;
1465 }
1466
dea31012005-04-17 16:05:31 -05001467 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001468 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001469 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001470
James Smart2e0fef82007-06-17 19:56:36 -05001471 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart549e55c2007-08-02 11:09:51 -04001472 vports = lpfc_create_vport_work_array(phba);
1473 if (vports != NULL)
1474 for(i = 0;
James Smart09372822008-01-11 01:52:54 -05001475 i <= phba->max_vpi && vports[i] != NULL;
James Smart549e55c2007-08-02 11:09:51 -04001476 i++) {
1477 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1478 continue;
James Smart58da1ff2008-04-07 10:15:56 -04001479 if (phba->fc_topology == TOPOLOGY_LOOP) {
1480 lpfc_vport_set_state(vports[i],
1481 FC_VPORT_LINKDOWN);
1482 continue;
1483 }
James Smart549e55c2007-08-02 11:09:51 -04001484 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1485 lpfc_initial_fdisc(vports[i]);
James Smart58da1ff2008-04-07 10:15:56 -04001486 else {
James Smart549e55c2007-08-02 11:09:51 -04001487 lpfc_vport_set_state(vports[i],
1488 FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -04001489 lpfc_printf_vlog(vport, KERN_ERR,
1490 LOG_ELS,
1491 "0259 No NPIV "
1492 "Fabric support\n");
James Smart549e55c2007-08-02 11:09:51 -04001493 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001494 }
James Smart09372822008-01-11 01:52:54 -05001495 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001496 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05001497 }
1498
1499 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1500 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001501 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05001502
1503 /* Drop the reference count from the mbox at the end after
1504 * all the current reference to the ndlp have been done.
1505 */
1506 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001507 return;
1508}
1509
1510/*
1511 * This routine handles processing a NameServer REG_LOGIN mailbox
1512 * command upon completion. It is setup in the LPFC_MBOXQ
1513 * as the completion routine when the command is
1514 * handed off to the SLI layer.
1515 */
1516void
James Smart2e0fef82007-06-17 19:56:36 -05001517lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001518{
James Smart2e0fef82007-06-17 19:56:36 -05001519 MAILBOX_t *mb = &pmb->mb;
1520 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1521 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1522 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001523
1524 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05001525out:
James Smartfa4066b2008-01-11 01:53:27 -05001526 /* decrement the node reference count held for this
1527 * callback function.
1528 */
James Smart329f9bc2007-04-25 09:53:01 -04001529 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001530 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1531 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04001532 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04001533
1534 /* If no other thread is using the ndlp, free it */
1535 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05001536
James Smart92d7f7b2007-06-17 19:56:38 -05001537 if (phba->fc_topology == TOPOLOGY_LOOP) {
1538 /*
1539 * RegLogin failed, use loop map to make discovery
1540 * list
1541 */
1542 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001543
James Smart92d7f7b2007-06-17 19:56:38 -05001544 /* Start discovery */
1545 lpfc_disc_start(vport);
1546 return;
1547 }
1548 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001549 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1550 "0260 Register NameServer error: 0x%x\n",
1551 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001552 return;
1553 }
1554
1555 pmb->context1 = NULL;
1556
dea31012005-04-17 16:05:31 -05001557 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001558 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001559 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001560
James Smart2e0fef82007-06-17 19:56:36 -05001561 if (vport->port_state < LPFC_VPORT_READY) {
1562 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05001563 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1564 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1565 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1566 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1567 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1568
1569 /* Issue SCR just before NameServer GID_FT Query */
1570 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05001571 }
1572
James Smart2e0fef82007-06-17 19:56:36 -05001573 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05001574 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05001575 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05001576 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05001577 goto out;
dea31012005-04-17 16:05:31 -05001578 }
1579
James Smartfa4066b2008-01-11 01:53:27 -05001580 /* decrement the node reference count held for this
1581 * callback function.
1582 */
James Smart329f9bc2007-04-25 09:53:01 -04001583 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001584 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1585 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001586 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001587
1588 return;
1589}
1590
1591static void
James Smart2e0fef82007-06-17 19:56:36 -05001592lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001593{
James Smart2e0fef82007-06-17 19:56:36 -05001594 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1595 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05001596 struct lpfc_rport_data *rdata;
1597 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05001598 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001599
1600 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001601 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1602 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001603 rport_ids.port_id = ndlp->nlp_DID;
1604 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05001605
James Smart329f9bc2007-04-25 09:53:01 -04001606 /*
1607 * We leave our node pointer in rport->dd_data when we unregister a
1608 * FCP target port. But fc_remote_port_add zeros the space to which
1609 * rport->dd_data points. So, if we're reusing a previously
1610 * registered port, drop the reference that we took the last time we
1611 * registered the port.
1612 */
1613 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05001614 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04001615 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05001616
1617 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1618 "rport add: did:x%x flg:x%x type x%x",
1619 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1620
James Smart2e0fef82007-06-17 19:56:36 -05001621 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04001622 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05001623 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1624 "Warning: fc_remote_port_add failed\n");
1625 return;
1626 }
1627
1628 /* initialize static port data */
1629 rport->maxframe_size = ndlp->nlp_maxframe;
1630 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05001631 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04001632 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001633
1634 if (ndlp->nlp_type & NLP_FCP_TARGET)
1635 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1636 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1637 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1638
1639
1640 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1641 fc_remote_port_rolechg(rport, rport_ids.roles);
1642
James Smart071fbd3d2006-04-15 11:53:20 -04001643 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001644 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04001645 ndlp->nlp_sid = rport->scsi_target_id;
1646 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001647 return;
1648}
1649
1650static void
James Smart2e0fef82007-06-17 19:56:36 -05001651lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001652{
1653 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04001654
James Smart858c9f62007-06-17 19:56:39 -05001655 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
1656 "rport delete: did:x%x flg:x%x type x%x",
1657 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1658
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001659 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05001660
1661 return;
1662}
1663
James Smartde0c5b32007-04-25 09:52:27 -04001664static void
James Smart2e0fef82007-06-17 19:56:36 -05001665lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04001666{
James Smart2e0fef82007-06-17 19:56:36 -05001667 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1668
1669 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001670 switch (state) {
1671 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001672 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001673 break;
1674 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001675 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001676 break;
1677 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001678 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001679 break;
1680 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001681 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001682 break;
1683 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001684 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001685 break;
1686 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001687 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001688 break;
1689 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001690 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001691 break;
1692 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001693 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001694 break;
1695 }
James Smart2e0fef82007-06-17 19:56:36 -05001696 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001697}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001698
James Smartde0c5b32007-04-25 09:52:27 -04001699static void
James Smart2e0fef82007-06-17 19:56:36 -05001700lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04001701 int old_state, int new_state)
1702{
James Smart2e0fef82007-06-17 19:56:36 -05001703 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1704
James Smartde0c5b32007-04-25 09:52:27 -04001705 if (new_state == NLP_STE_UNMAPPED_NODE) {
1706 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1707 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1708 ndlp->nlp_type |= NLP_FC_NODE;
1709 }
1710 if (new_state == NLP_STE_MAPPED_NODE)
1711 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1712 if (new_state == NLP_STE_NPR_NODE)
1713 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05001714
James Smartde0c5b32007-04-25 09:52:27 -04001715 /* Transport interface */
1716 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1717 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001718 vport->phba->nport_event_cnt++;
1719 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001720 }
dea31012005-04-17 16:05:31 -05001721
James Smartde0c5b32007-04-25 09:52:27 -04001722 if (new_state == NLP_STE_MAPPED_NODE ||
1723 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05001724 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05001725 /*
1726 * Tell the fc transport about the port, if we haven't
1727 * already. If we have, and it's a scsi entity, be
1728 * sure to unblock any attached scsi devices
1729 */
1730 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001731 }
James Smartea2151b2008-09-07 11:52:10 -04001732 if ((new_state == NLP_STE_MAPPED_NODE) &&
1733 (vport->stat_data_enabled)) {
1734 /*
1735 * A new target is discovered, if there is no buffer for
1736 * statistical data collection allocate buffer.
1737 */
1738 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
1739 sizeof(struct lpfc_scsicmd_bkt),
1740 GFP_KERNEL);
1741
1742 if (!ndlp->lat_data)
1743 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
1744 "0286 lpfc_nlp_state_cleanup failed to "
1745 "allocate statistical data buffer DID "
1746 "0x%x\n", ndlp->nlp_DID);
1747 }
James Smart858c9f62007-06-17 19:56:39 -05001748 /*
1749 * if we added to Mapped list, but the remote port
1750 * registration failed or assigned a target id outside
1751 * our presentable range - move the node to the
1752 * Unmapped List
1753 */
James Smartde0c5b32007-04-25 09:52:27 -04001754 if (new_state == NLP_STE_MAPPED_NODE &&
1755 (!ndlp->rport ||
1756 ndlp->rport->scsi_target_id == -1 ||
1757 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05001758 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001759 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05001760 spin_unlock_irq(shost->host_lock);
1761 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001762 }
James Smartde0c5b32007-04-25 09:52:27 -04001763}
1764
James Smart685f0bf2007-04-25 09:53:08 -04001765static char *
1766lpfc_nlp_state_name(char *buffer, size_t size, int state)
1767{
1768 static char *states[] = {
1769 [NLP_STE_UNUSED_NODE] = "UNUSED",
1770 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1771 [NLP_STE_ADISC_ISSUE] = "ADISC",
1772 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1773 [NLP_STE_PRLI_ISSUE] = "PRLI",
1774 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1775 [NLP_STE_MAPPED_NODE] = "MAPPED",
1776 [NLP_STE_NPR_NODE] = "NPR",
1777 };
1778
James Smart311464e2007-08-02 11:10:37 -04001779 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04001780 strlcpy(buffer, states[state], size);
1781 else
1782 snprintf(buffer, size, "unknown (%d)", state);
1783 return buffer;
1784}
1785
James Smartde0c5b32007-04-25 09:52:27 -04001786void
James Smart2e0fef82007-06-17 19:56:36 -05001787lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1788 int state)
James Smartde0c5b32007-04-25 09:52:27 -04001789{
James Smart2e0fef82007-06-17 19:56:36 -05001790 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04001791 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04001792 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04001793
James Smarte8b62012007-08-02 11:10:09 -04001794 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1795 "0904 NPort state transition x%06x, %s -> %s\n",
1796 ndlp->nlp_DID,
1797 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1798 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05001799
1800 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1801 "node statechg did:x%x old:%d ste:%d",
1802 ndlp->nlp_DID, old_state, state);
1803
James Smartde0c5b32007-04-25 09:52:27 -04001804 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04001805 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001806 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001807 if (old_state == NLP_STE_UNMAPPED_NODE) {
1808 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1809 ndlp->nlp_type &= ~NLP_FC_NODE;
1810 }
1811
James Smart685f0bf2007-04-25 09:53:08 -04001812 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05001813 spin_lock_irq(shost->host_lock);
1814 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1815 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001816 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05001817 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04001818
1819 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05001820 lpfc_nlp_counters(vport, state, 1);
1821 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04001822}
1823
1824void
James Smarte47c9092008-02-08 18:49:26 -05001825lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1826{
1827 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1828
1829 if (list_empty(&ndlp->nlp_listp)) {
1830 spin_lock_irq(shost->host_lock);
1831 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1832 spin_unlock_irq(shost->host_lock);
1833 }
1834}
1835
1836void
James Smart2e0fef82007-06-17 19:56:36 -05001837lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001838{
James Smart2e0fef82007-06-17 19:56:36 -05001839 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1840
James Smart0d2b6b82008-06-14 22:52:47 -04001841 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001842 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05001843 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1844 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001845 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05001846 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05001847 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05001848 NLP_STE_UNUSED_NODE);
1849}
1850
Adrian Bunk4d9db012008-02-14 23:24:02 +02001851static void
James Smarte47c9092008-02-08 18:49:26 -05001852lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1853{
James Smart0d2b6b82008-06-14 22:52:47 -04001854 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05001855 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1856 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1857 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
1858 NLP_STE_UNUSED_NODE);
1859}
James Smart109f6ed2008-12-04 22:39:08 -05001860/**
1861 * lpfc_initialize_node: Initialize all fields of node object.
1862 * @vport: Pointer to Virtual Port object.
1863 * @ndlp: Pointer to FC node object.
1864 * @did: FC_ID of the node.
1865 * This function is always called when node object need to
1866 * be initialized. It initializes all the fields of the node
1867 * object.
1868 **/
1869static inline void
1870lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1871 uint32_t did)
1872{
1873 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
1874 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
1875 init_timer(&ndlp->nlp_delayfunc);
1876 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
1877 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
1878 ndlp->nlp_DID = did;
1879 ndlp->vport = vport;
1880 ndlp->nlp_sid = NLP_NO_SID;
1881 kref_init(&ndlp->kref);
1882 NLP_INT_NODE_ACT(ndlp);
1883 atomic_set(&ndlp->cmd_pending, 0);
1884 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
1885}
James Smarte47c9092008-02-08 18:49:26 -05001886
1887struct lpfc_nodelist *
1888lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1889 int state)
1890{
1891 struct lpfc_hba *phba = vport->phba;
1892 uint32_t did;
1893 unsigned long flags;
1894
1895 if (!ndlp)
1896 return NULL;
1897
1898 spin_lock_irqsave(&phba->ndlp_lock, flags);
1899 /* The ndlp should not be in memory free mode */
1900 if (NLP_CHK_FREE_REQ(ndlp)) {
1901 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1902 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1903 "0277 lpfc_enable_node: ndlp:x%p "
1904 "usgmap:x%x refcnt:%d\n",
1905 (void *)ndlp, ndlp->nlp_usg_map,
1906 atomic_read(&ndlp->kref.refcount));
1907 return NULL;
1908 }
1909 /* The ndlp should not already be in active mode */
1910 if (NLP_CHK_NODE_ACT(ndlp)) {
1911 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1912 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1913 "0278 lpfc_enable_node: ndlp:x%p "
1914 "usgmap:x%x refcnt:%d\n",
1915 (void *)ndlp, ndlp->nlp_usg_map,
1916 atomic_read(&ndlp->kref.refcount));
1917 return NULL;
1918 }
1919
1920 /* Keep the original DID */
1921 did = ndlp->nlp_DID;
1922
1923 /* re-initialize ndlp except of ndlp linked list pointer */
1924 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
1925 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05001926 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05001927
1928 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1929
1930 if (state != NLP_STE_UNUSED_NODE)
1931 lpfc_nlp_set_state(vport, ndlp, state);
1932
1933 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1934 "node enable: did:x%x",
1935 ndlp->nlp_DID, 0, 0);
1936 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04001937}
1938
1939void
James Smart2e0fef82007-06-17 19:56:36 -05001940lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001941{
James Smart87af33f2007-10-27 13:37:43 -04001942 /*
James Smartfa4066b2008-01-11 01:53:27 -05001943 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04001944 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05001945 * the ndlp from the vport. The ndlp marked as UNUSED on the list
1946 * until ALL other outstanding threads have completed. We check
1947 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04001948 */
James Smartfa4066b2008-01-11 01:53:27 -05001949 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1950 return;
James Smart51ef4c22007-08-02 11:10:31 -04001951 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04001952 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04001953 return;
dea31012005-04-17 16:05:31 -05001954}
1955
1956/*
1957 * Start / ReStart rescue timer for Discovery / RSCN handling
1958 */
1959void
James Smart2e0fef82007-06-17 19:56:36 -05001960lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001961{
James Smart2e0fef82007-06-17 19:56:36 -05001962 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1963 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001964 uint32_t tmo;
1965
James Smart2e0fef82007-06-17 19:56:36 -05001966 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001967 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001968 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1969 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001970 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001971 * FC spec states we need 3 * ratov for CT requests
1972 */
1973 tmo = ((phba->fc_ratov * 3) + 3);
1974 }
dea31012005-04-17 16:05:31 -05001975
James Smart858c9f62007-06-17 19:56:39 -05001976
1977 if (!timer_pending(&vport->fc_disctmo)) {
1978 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1979 "set disc timer: tmo:x%x state:x%x flg:x%x",
1980 tmo, vport->port_state, vport->fc_flag);
1981 }
1982
James Smart2e0fef82007-06-17 19:56:36 -05001983 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1984 spin_lock_irq(shost->host_lock);
1985 vport->fc_flag |= FC_DISC_TMO;
1986 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001987
1988 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001989 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1990 "0247 Start Discovery Timer state x%x "
1991 "Data: x%x x%lx x%x x%x\n",
1992 vport->port_state, tmo,
1993 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1994 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05001995
1996 return;
1997}
1998
1999/*
2000 * Cancel rescue timer for Discovery / RSCN handling
2001 */
2002int
James Smart2e0fef82007-06-17 19:56:36 -05002003lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002004{
James Smart2e0fef82007-06-17 19:56:36 -05002005 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002006 unsigned long iflags;
2007
James Smart858c9f62007-06-17 19:56:39 -05002008 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2009 "can disc timer: state:x%x rtry:x%x flg:x%x",
2010 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2011
dea31012005-04-17 16:05:31 -05002012 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05002013 if (vport->fc_flag & FC_DISC_TMO) {
2014 spin_lock_irqsave(shost->host_lock, iflags);
2015 vport->fc_flag &= ~FC_DISC_TMO;
2016 spin_unlock_irqrestore(shost->host_lock, iflags);
2017 del_timer_sync(&vport->fc_disctmo);
2018 spin_lock_irqsave(&vport->work_port_lock, iflags);
2019 vport->work_port_events &= ~WORKER_DISC_TMO;
2020 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05002021 }
2022
2023 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002024 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2025 "0248 Cancel Discovery Timer state x%x "
2026 "Data: x%x x%x x%x\n",
2027 vport->port_state, vport->fc_flag,
2028 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002029 return 0;
dea31012005-04-17 16:05:31 -05002030}
2031
2032/*
2033 * Check specified ring for outstanding IOCB on the SLI queue
2034 * Return true if iocb matches the specified nport
2035 */
2036int
James Smart2e0fef82007-06-17 19:56:36 -05002037lpfc_check_sli_ndlp(struct lpfc_hba *phba,
2038 struct lpfc_sli_ring *pring,
2039 struct lpfc_iocbq *iocb,
2040 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002041{
James Smart2e0fef82007-06-17 19:56:36 -05002042 struct lpfc_sli *psli = &phba->sli;
2043 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05002044 struct lpfc_vport *vport = ndlp->vport;
2045
2046 if (iocb->vport != vport)
2047 return 0;
2048
dea31012005-04-17 16:05:31 -05002049 if (pring->ringno == LPFC_ELS_RING) {
2050 switch (icmd->ulpCommand) {
2051 case CMD_GEN_REQUEST64_CR:
2052 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002053 return 1;
dea31012005-04-17 16:05:31 -05002054 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04002055 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
2056 return 1;
dea31012005-04-17 16:05:31 -05002057 case CMD_XMIT_ELS_RSP64_CX:
2058 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002059 return 1;
dea31012005-04-17 16:05:31 -05002060 }
James Smarta4bc3372006-12-02 13:34:16 -05002061 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05002062
2063 } else if (pring->ringno == psli->fcp_ring) {
2064 /* Skip match check if waiting to relogin to FCP target */
2065 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002066 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002067 return 0;
dea31012005-04-17 16:05:31 -05002068 }
2069 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002070 return 1;
dea31012005-04-17 16:05:31 -05002071 }
2072 } else if (pring->ringno == psli->next_ring) {
2073
2074 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002075 return 0;
dea31012005-04-17 16:05:31 -05002076}
2077
2078/*
2079 * Free resources / clean up outstanding I/Os
2080 * associated with nlp_rpi in the LPFC_NODELIST entry.
2081 */
2082static int
James Smart2e0fef82007-06-17 19:56:36 -05002083lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002084{
James Smart2534ba72007-04-25 09:52:20 -04002085 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002086 struct lpfc_sli *psli;
2087 struct lpfc_sli_ring *pring;
2088 struct lpfc_iocbq *iocb, *next_iocb;
2089 IOCB_t *icmd;
2090 uint32_t rpi, i;
2091
James Smart92d7f7b2007-06-17 19:56:38 -05002092 lpfc_fabric_abort_nport(ndlp);
2093
dea31012005-04-17 16:05:31 -05002094 /*
2095 * Everything that matches on txcmplq will be returned
2096 * by firmware with a no rpi error.
2097 */
2098 psli = &phba->sli;
2099 rpi = ndlp->nlp_rpi;
2100 if (rpi) {
2101 /* Now process each ring */
2102 for (i = 0; i < psli->num_rings; i++) {
2103 pring = &psli->ring[i];
2104
James Smart2e0fef82007-06-17 19:56:36 -05002105 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002106 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05002107 list) {
dea31012005-04-17 16:05:31 -05002108 /*
2109 * Check to see if iocb matches the nport we are
2110 * looking for
2111 */
James Smart92d7f7b2007-06-17 19:56:38 -05002112 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
2113 ndlp))) {
dea31012005-04-17 16:05:31 -05002114 /* It matches, so deque and call compl
2115 with an error */
James Smart2534ba72007-04-25 09:52:20 -04002116 list_move_tail(&iocb->list,
2117 &completions);
dea31012005-04-17 16:05:31 -05002118 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002119 }
2120 }
James Smart2e0fef82007-06-17 19:56:36 -05002121 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002122 }
2123 }
James Smart2534ba72007-04-25 09:52:20 -04002124
2125 while (!list_empty(&completions)) {
2126 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05002127 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04002128
James Smart2e0fef82007-06-17 19:56:36 -05002129 if (!iocb->iocb_cmpl)
2130 lpfc_sli_release_iocbq(phba, iocb);
2131 else {
James Smart2534ba72007-04-25 09:52:20 -04002132 icmd = &iocb->iocb;
2133 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2134 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
James Smart2e0fef82007-06-17 19:56:36 -05002135 (iocb->iocb_cmpl)(phba, iocb, iocb);
2136 }
James Smart2534ba72007-04-25 09:52:20 -04002137 }
2138
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002139 return 0;
dea31012005-04-17 16:05:31 -05002140}
2141
2142/*
2143 * Free rpi associated with LPFC_NODELIST entry.
2144 * This routine is called from lpfc_freenode(), when we are removing
2145 * a LPFC_NODELIST entry. It is also called if the driver initiates a
2146 * LOGO that completes successfully, and we are waiting to PLOGI back
2147 * to the remote NPort. In addition, it is called after we receive
2148 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
2149 * we are waiting to PLOGI back to the remote NPort.
2150 */
2151int
James Smart2e0fef82007-06-17 19:56:36 -05002152lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002153{
James Smart2e0fef82007-06-17 19:56:36 -05002154 struct lpfc_hba *phba = vport->phba;
2155 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05002156 int rc;
2157
2158 if (ndlp->nlp_rpi) {
James Smart2e0fef82007-06-17 19:56:36 -05002159 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2160 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05002161 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05002162 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002163 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002164 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05002165 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05002166 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002167 }
dea31012005-04-17 16:05:31 -05002168 lpfc_no_rpi(phba, ndlp);
2169 ndlp->nlp_rpi = 0;
2170 return 1;
2171 }
2172 return 0;
2173}
2174
James Smart92d7f7b2007-06-17 19:56:38 -05002175void
2176lpfc_unreg_all_rpis(struct lpfc_vport *vport)
2177{
2178 struct lpfc_hba *phba = vport->phba;
2179 LPFC_MBOXQ_t *mbox;
2180 int rc;
2181
2182 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2183 if (mbox) {
2184 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
2185 mbox->vport = vport;
2186 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002187 mbox->context1 = NULL;
2188 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05002189 if (rc == MBX_NOT_FINISHED) {
2190 mempool_free(mbox, phba->mbox_mem_pool);
2191 }
2192 }
2193}
2194
2195void
2196lpfc_unreg_default_rpis(struct lpfc_vport *vport)
2197{
2198 struct lpfc_hba *phba = vport->phba;
2199 LPFC_MBOXQ_t *mbox;
2200 int rc;
2201
2202 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2203 if (mbox) {
2204 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
2205 mbox->vport = vport;
2206 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002207 mbox->context1 = NULL;
2208 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05002209 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002210 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2211 "1815 Could not issue "
2212 "unreg_did (default rpis)\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002213 mempool_free(mbox, phba->mbox_mem_pool);
2214 }
2215 }
2216}
2217
dea31012005-04-17 16:05:31 -05002218/*
2219 * Free resources associated with LPFC_NODELIST entry
2220 * so it can be freed.
2221 */
2222static int
James Smart2e0fef82007-06-17 19:56:36 -05002223lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002224{
James Smart2e0fef82007-06-17 19:56:36 -05002225 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2226 struct lpfc_hba *phba = vport->phba;
2227 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05002228 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05002229
2230 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04002231 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2232 "0900 Cleanup node for NPort x%x "
2233 "Data: x%x x%x x%x\n",
2234 ndlp->nlp_DID, ndlp->nlp_flag,
2235 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05002236 if (NLP_CHK_FREE_REQ(ndlp)) {
2237 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2238 "0280 lpfc_cleanup_node: ndlp:x%p "
2239 "usgmap:x%x refcnt:%d\n",
2240 (void *)ndlp, ndlp->nlp_usg_map,
2241 atomic_read(&ndlp->kref.refcount));
2242 lpfc_dequeue_node(vport, ndlp);
2243 } else {
2244 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2245 "0281 lpfc_cleanup_node: ndlp:x%p "
2246 "usgmap:x%x refcnt:%d\n",
2247 (void *)ndlp, ndlp->nlp_usg_map,
2248 atomic_read(&ndlp->kref.refcount));
2249 lpfc_disable_node(vport, ndlp);
2250 }
dea31012005-04-17 16:05:31 -05002251
dea31012005-04-17 16:05:31 -05002252 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
2253 if ((mb = phba->sli.mbox_active)) {
2254 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
2255 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
2256 mb->context2 = NULL;
2257 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2258 }
2259 }
James Smart33ccf8d2006-08-17 11:57:58 -04002260
James Smart2e0fef82007-06-17 19:56:36 -05002261 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002262 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
2263 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002264 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05002265 mp = (struct lpfc_dmabuf *) (mb->context1);
2266 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05002267 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002268 kfree(mp);
2269 }
2270 list_del(&mb->list);
2271 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002272 /* We shall not invoke the lpfc_nlp_put to decrement
2273 * the ndlp reference count as we are in the process
2274 * of lpfc_nlp_release.
2275 */
dea31012005-04-17 16:05:31 -05002276 }
2277 }
James Smart2e0fef82007-06-17 19:56:36 -05002278 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002279
James Smarte47c9092008-02-08 18:49:26 -05002280 lpfc_els_abort(phba, ndlp);
2281
James Smart2e0fef82007-06-17 19:56:36 -05002282 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04002283 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05002284 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002285
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05002286 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05002287 del_timer_sync(&ndlp->nlp_delayfunc);
2288
James Smart0d2b6b82008-06-14 22:52:47 -04002289 list_del_init(&ndlp->els_retry_evt.evt_listp);
2290 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002291
James Smart2e0fef82007-06-17 19:56:36 -05002292 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05002293
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002294 return 0;
dea31012005-04-17 16:05:31 -05002295}
2296
2297/*
2298 * Check to see if we can free the nlp back to the freelist.
2299 * If we are in the middle of using the nlp in the discovery state
2300 * machine, defer the free till we reach the end of the state machine.
2301 */
James Smart329f9bc2007-04-25 09:53:01 -04002302static void
James Smart2e0fef82007-06-17 19:56:36 -05002303lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002304{
James Smarta8adb832007-10-27 13:37:53 -04002305 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04002306 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04002307 LPFC_MBOXQ_t *mbox;
2308 int rc;
dea31012005-04-17 16:05:31 -05002309
James Smart0d2b6b82008-06-14 22:52:47 -04002310 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04002311 if (ndlp->nlp_flag & NLP_DEFER_RM && !ndlp->nlp_rpi) {
2312 /* For this case we need to cleanup the default rpi
2313 * allocated by the firmware.
2314 */
2315 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
2316 != NULL) {
2317 rc = lpfc_reg_login(phba, vport->vpi, ndlp->nlp_DID,
2318 (uint8_t *) &vport->fc_sparam, mbox, 0);
2319 if (rc) {
2320 mempool_free(mbox, phba->mbox_mem_pool);
2321 }
2322 else {
2323 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2324 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2325 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05002326 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04002327 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2328 if (rc == MBX_NOT_FINISHED) {
2329 mempool_free(mbox, phba->mbox_mem_pool);
2330 }
2331 }
2332 }
2333 }
James Smart2e0fef82007-06-17 19:56:36 -05002334 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04002335
James Smart2e0fef82007-06-17 19:56:36 -05002336 /*
James Smart92d7f7b2007-06-17 19:56:38 -05002337 * We can get here with a non-NULL ndlp->rport because when we
2338 * unregister a rport we don't break the rport/node linkage. So if we
2339 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05002340 */
James Smart92d7f7b2007-06-17 19:56:38 -05002341 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04002342 rdata = ndlp->rport->dd_data;
2343 rdata->pnode = NULL;
2344 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05002345 }
dea31012005-04-17 16:05:31 -05002346}
2347
2348static int
James Smart2e0fef82007-06-17 19:56:36 -05002349lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2350 uint32_t did)
dea31012005-04-17 16:05:31 -05002351{
James Smart2e0fef82007-06-17 19:56:36 -05002352 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05002353
2354 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002355 return 0;
dea31012005-04-17 16:05:31 -05002356
dea31012005-04-17 16:05:31 -05002357 /* First check for Direct match */
2358 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002359 return 1;
dea31012005-04-17 16:05:31 -05002360
2361 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05002362 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05002363 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002364 return 0;
dea31012005-04-17 16:05:31 -05002365 }
2366
2367 matchdid.un.word = did;
2368 ndlpdid.un.word = ndlp->nlp_DID;
2369 if (matchdid.un.b.id == ndlpdid.un.b.id) {
2370 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
2371 (mydid.un.b.area == matchdid.un.b.area)) {
2372 if ((ndlpdid.un.b.domain == 0) &&
2373 (ndlpdid.un.b.area == 0)) {
2374 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002375 return 1;
dea31012005-04-17 16:05:31 -05002376 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002377 return 0;
dea31012005-04-17 16:05:31 -05002378 }
2379
2380 matchdid.un.word = ndlp->nlp_DID;
2381 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
2382 (mydid.un.b.area == ndlpdid.un.b.area)) {
2383 if ((matchdid.un.b.domain == 0) &&
2384 (matchdid.un.b.area == 0)) {
2385 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002386 return 1;
dea31012005-04-17 16:05:31 -05002387 }
2388 }
2389 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002390 return 0;
dea31012005-04-17 16:05:31 -05002391}
2392
James Smart685f0bf2007-04-25 09:53:08 -04002393/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05002394static struct lpfc_nodelist *
2395__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002396{
James Smart2fb9bd82006-12-02 13:33:57 -05002397 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002398 uint32_t data1;
2399
James Smart2e0fef82007-06-17 19:56:36 -05002400 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2401 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04002402 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2403 ((uint32_t) ndlp->nlp_xri << 16) |
2404 ((uint32_t) ndlp->nlp_type << 8) |
2405 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04002406 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2407 "0929 FIND node DID "
2408 "Data: x%p x%x x%x x%x\n",
2409 ndlp, ndlp->nlp_DID,
2410 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04002411 return ndlp;
dea31012005-04-17 16:05:31 -05002412 }
2413 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002414
dea31012005-04-17 16:05:31 -05002415 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04002416 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2417 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05002418 return NULL;
2419}
2420
2421struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002422lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002423{
James Smart2e0fef82007-06-17 19:56:36 -05002424 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002425 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002426
James Smart2e0fef82007-06-17 19:56:36 -05002427 spin_lock_irq(shost->host_lock);
2428 ndlp = __lpfc_findnode_did(vport, did);
2429 spin_unlock_irq(shost->host_lock);
2430 return ndlp;
2431}
2432
2433struct lpfc_nodelist *
2434lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2435{
2436 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2437 struct lpfc_nodelist *ndlp;
2438
2439 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002440 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05002441 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2442 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05002443 return NULL;
2444 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05002445 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002446 if (!ndlp)
2447 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002448 lpfc_nlp_init(vport, ndlp, did);
2449 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2450 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002451 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002452 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002453 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05002454 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2455 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
2456 if (!ndlp)
2457 return NULL;
2458 spin_lock_irq(shost->host_lock);
2459 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2460 spin_unlock_irq(shost->host_lock);
2461 return ndlp;
dea31012005-04-17 16:05:31 -05002462 }
James Smarte47c9092008-02-08 18:49:26 -05002463
James Smart58da1ff2008-04-07 10:15:56 -04002464 if ((vport->fc_flag & FC_RSCN_MODE) &&
2465 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002466 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04002467 /* If we've already recieved a PLOGI from this NPort
2468 * we don't need to try to discover it again.
2469 */
2470 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
2471 return NULL;
2472
James Smart2e0fef82007-06-17 19:56:36 -05002473 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002474 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002475 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002476
2477 /* Since this node is marked for discovery,
2478 * delay timeout is not needed.
2479 */
James Smart0d2b6b82008-06-14 22:52:47 -04002480 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart071fbd3d2006-04-15 11:53:20 -04002481 } else
dea31012005-04-17 16:05:31 -05002482 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002483 } else {
James Smart87af33f2007-10-27 13:37:43 -04002484 /* If we've already recieved a PLOGI from this NPort,
2485 * or we are already in the process of discovery on it,
2486 * we don't need to try to discover it again.
2487 */
James Smart685f0bf2007-04-25 09:53:08 -04002488 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04002489 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2490 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05002491 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002492 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2493 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002494 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002495 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002496 }
2497 return ndlp;
2498}
2499
2500/* Build a list of nodes to discover based on the loopmap */
2501void
James Smart2e0fef82007-06-17 19:56:36 -05002502lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002503{
James Smart2e0fef82007-06-17 19:56:36 -05002504 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002505 int j;
2506 uint32_t alpa, index;
2507
James Smart2e0fef82007-06-17 19:56:36 -05002508 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002509 return;
James Smart2e0fef82007-06-17 19:56:36 -05002510
2511 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05002512 return;
dea31012005-04-17 16:05:31 -05002513
2514 /* Check for loop map present or not */
2515 if (phba->alpa_map[0]) {
2516 for (j = 1; j <= phba->alpa_map[0]; j++) {
2517 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05002518 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05002519 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002520 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002521 }
2522 } else {
2523 /* No alpamap, so try all alpa's */
2524 for (j = 0; j < FC_MAXLOOP; j++) {
2525 /* If cfg_scan_down is set, start from highest
2526 * ALPA (0xef) to lowest (0x1).
2527 */
James Smart3de2a652007-08-02 11:09:59 -04002528 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05002529 index = j;
2530 else
2531 index = FC_MAXLOOP - j - 1;
2532 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05002533 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05002534 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002535 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002536 }
2537 }
2538 return;
2539}
2540
dea31012005-04-17 16:05:31 -05002541void
James Smart2e0fef82007-06-17 19:56:36 -05002542lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002543{
dea31012005-04-17 16:05:31 -05002544 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05002545 struct lpfc_sli *psli = &phba->sli;
2546 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2547 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2548 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2549 int rc;
2550
James Smart92d7f7b2007-06-17 19:56:38 -05002551 /*
2552 * if it's not a physical port or if we already send
2553 * clear_la then don't send it.
2554 */
2555 if ((phba->link_state >= LPFC_CLEAR_LA) ||
2556 (vport->port_type != LPFC_PHYSICAL_PORT))
2557 return;
2558
James Smart2e0fef82007-06-17 19:56:36 -05002559 /* Link up discovery */
2560 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2561 phba->link_state = LPFC_CLEAR_LA;
2562 lpfc_clear_la(phba, mbox);
2563 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2564 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002565 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05002566 if (rc == MBX_NOT_FINISHED) {
2567 mempool_free(mbox, phba->mbox_mem_pool);
2568 lpfc_disc_flush_list(vport);
2569 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2570 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2571 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05002572 phba->link_state = LPFC_HBA_ERROR;
2573 }
2574 }
2575}
2576
2577/* Reg_vpi to tell firmware to resume normal operations */
2578void
2579lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2580{
2581 LPFC_MBOXQ_t *regvpimbox;
2582
2583 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2584 if (regvpimbox) {
2585 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
2586 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2587 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002588 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05002589 == MBX_NOT_FINISHED) {
2590 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002591 }
2592 }
2593}
2594
2595/* Start Link up / RSCN discovery on NPR nodes */
2596void
2597lpfc_disc_start(struct lpfc_vport *vport)
2598{
2599 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2600 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04002601 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05002602 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04002603 int did_changed;
dea31012005-04-17 16:05:31 -05002604
James Smart2e0fef82007-06-17 19:56:36 -05002605 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002606 return;
James Smart2e0fef82007-06-17 19:56:36 -05002607
2608 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05002609 clear_la_pending = 1;
2610 else
2611 clear_la_pending = 0;
2612
James Smart2e0fef82007-06-17 19:56:36 -05002613 if (vport->port_state < LPFC_VPORT_READY)
2614 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05002615
James Smart2e0fef82007-06-17 19:56:36 -05002616 lpfc_set_disctmo(vport);
2617
2618 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05002619 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002620 else
dea31012005-04-17 16:05:31 -05002621 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002622
2623 vport->fc_prevDID = vport->fc_myDID;
2624 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05002625
2626 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002627 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2628 "0202 Start Discovery hba state x%x "
2629 "Data: x%x x%x x%x\n",
2630 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
2631 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002632
2633 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002634 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05002635
2636 if (num_sent)
2637 return;
2638
James Smart92d7f7b2007-06-17 19:56:38 -05002639 /*
2640 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2641 * continue discovery.
2642 */
2643 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05002644 !(vport->fc_flag & FC_PT2PT) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002645 !(vport->fc_flag & FC_RSCN_MODE)) {
2646 lpfc_issue_reg_vpi(phba, vport);
2647 return;
2648 }
James Smart2e0fef82007-06-17 19:56:36 -05002649
James Smart92d7f7b2007-06-17 19:56:38 -05002650 /*
2651 * For SLI2, we need to set port_state to READY and continue
2652 * discovery.
2653 */
2654 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2655 /* If we get here, there is nothing to ADISC */
2656 if (vport->port_type == LPFC_PHYSICAL_PORT)
2657 lpfc_issue_clear_la(phba, vport);
2658
2659 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05002660 vport->num_disc_nodes = 0;
2661 /* go thru NPR nodes and issue ELS PLOGIs */
2662 if (vport->fc_npr_cnt)
2663 lpfc_els_disc_plogi(vport);
2664
2665 if (!vport->num_disc_nodes) {
2666 spin_lock_irq(shost->host_lock);
2667 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2668 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002669 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05002670 }
2671 }
James Smart92d7f7b2007-06-17 19:56:38 -05002672 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002673 } else {
2674 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002675 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05002676
2677 if (num_sent)
2678 return;
2679
James Smart2e0fef82007-06-17 19:56:36 -05002680 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05002681 /* Check to see if more RSCNs came in while we
2682 * were processing this one.
2683 */
James Smart2e0fef82007-06-17 19:56:36 -05002684 if ((vport->fc_rscn_id_cnt == 0) &&
2685 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2686 spin_lock_irq(shost->host_lock);
2687 vport->fc_flag &= ~FC_RSCN_MODE;
2688 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002689 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002690 } else
James Smart2e0fef82007-06-17 19:56:36 -05002691 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05002692 }
2693 }
2694 return;
2695}
2696
2697/*
2698 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2699 * ring the match the sppecified nodelist.
2700 */
2701static void
James Smart2e0fef82007-06-17 19:56:36 -05002702lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002703{
James Smart2534ba72007-04-25 09:52:20 -04002704 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002705 struct lpfc_sli *psli;
2706 IOCB_t *icmd;
2707 struct lpfc_iocbq *iocb, *next_iocb;
2708 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002709
2710 psli = &phba->sli;
2711 pring = &psli->ring[LPFC_ELS_RING];
2712
2713 /* Error matching iocb on txq or txcmplq
2714 * First check the txq.
2715 */
James Smart2e0fef82007-06-17 19:56:36 -05002716 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002717 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2718 if (iocb->context1 != ndlp) {
2719 continue;
2720 }
2721 icmd = &iocb->iocb;
2722 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2723 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2724
James Smart2534ba72007-04-25 09:52:20 -04002725 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05002726 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002727 }
2728 }
2729
2730 /* Next check the txcmplq */
2731 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2732 if (iocb->context1 != ndlp) {
2733 continue;
2734 }
2735 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05002736 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2737 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04002738 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05002739 }
2740 }
James Smart2e0fef82007-06-17 19:56:36 -05002741 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002742
2743 while (!list_empty(&completions)) {
2744 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05002745 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04002746
James Smart2e0fef82007-06-17 19:56:36 -05002747 if (!iocb->iocb_cmpl)
2748 lpfc_sli_release_iocbq(phba, iocb);
2749 else {
James Smart2534ba72007-04-25 09:52:20 -04002750 icmd = &iocb->iocb;
2751 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2752 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2753 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002754 }
James Smart2534ba72007-04-25 09:52:20 -04002755 }
dea31012005-04-17 16:05:31 -05002756}
2757
Adrian Bunka6ababd2007-11-05 18:07:33 +01002758static void
James Smart2e0fef82007-06-17 19:56:36 -05002759lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002760{
2761 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05002762 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002763
James Smart2e0fef82007-06-17 19:56:36 -05002764 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2765 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002766 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002767 if (!NLP_CHK_NODE_ACT(ndlp))
2768 continue;
James Smart685f0bf2007-04-25 09:53:08 -04002769 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2770 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2771 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04002772 }
dea31012005-04-17 16:05:31 -05002773 }
2774 }
dea31012005-04-17 16:05:31 -05002775}
2776
James Smart92d7f7b2007-06-17 19:56:38 -05002777void
2778lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2779{
2780 lpfc_els_flush_rscn(vport);
2781 lpfc_els_flush_cmd(vport);
2782 lpfc_disc_flush_list(vport);
2783}
2784
dea31012005-04-17 16:05:31 -05002785/*****************************************************************************/
2786/*
2787 * NAME: lpfc_disc_timeout
2788 *
2789 * FUNCTION: Fibre Channel driver discovery timeout routine.
2790 *
2791 * EXECUTION ENVIRONMENT: interrupt only
2792 *
2793 * CALLED FROM:
2794 * Timer function
2795 *
2796 * RETURNS:
2797 * none
2798 */
2799/*****************************************************************************/
2800void
2801lpfc_disc_timeout(unsigned long ptr)
2802{
James Smart2e0fef82007-06-17 19:56:36 -05002803 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2804 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04002805 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05002806 unsigned long flags = 0;
2807
2808 if (unlikely(!phba))
2809 return;
2810
James Smart5e9d9b82008-06-14 22:52:53 -04002811 spin_lock_irqsave(&vport->work_port_lock, flags);
2812 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
2813 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05002814 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04002815 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05002816
James Smart5e9d9b82008-06-14 22:52:53 -04002817 if (!tmo_posted)
2818 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002819 return;
2820}
2821
2822static void
James Smart2e0fef82007-06-17 19:56:36 -05002823lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002824{
James Smart2e0fef82007-06-17 19:56:36 -05002825 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2826 struct lpfc_hba *phba = vport->phba;
2827 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002828 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05002829 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05002830 int rc, clrlaerr = 0;
2831
James Smart2e0fef82007-06-17 19:56:36 -05002832 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05002833 return;
2834
James Smart2e0fef82007-06-17 19:56:36 -05002835 spin_lock_irq(shost->host_lock);
2836 vport->fc_flag &= ~FC_DISC_TMO;
2837 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002838
James Smart858c9f62007-06-17 19:56:39 -05002839 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2840 "disc timeout: state:x%x rtry:x%x flg:x%x",
2841 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2842
James Smart2e0fef82007-06-17 19:56:36 -05002843 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05002844
2845 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05002846 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2847 * FAN
2848 */
2849 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04002850 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
2851 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002852 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05002853 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002854 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002855 if (!NLP_CHK_NODE_ACT(ndlp))
2856 continue;
James Smart685f0bf2007-04-25 09:53:08 -04002857 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2858 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002859 if (ndlp->nlp_type & NLP_FABRIC) {
2860 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05002861 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04002862
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002863 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002864 /* Fail outstanding IO now since device
2865 * is marked for PLOGI.
2866 */
James Smart2e0fef82007-06-17 19:56:36 -05002867 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002868 }
2869 }
James Smart92d7f7b2007-06-17 19:56:38 -05002870 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05002871 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05002872 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002873 }
dea31012005-04-17 16:05:31 -05002874 break;
2875
James Smart92d7f7b2007-06-17 19:56:38 -05002876 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05002877 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05002878 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05002879 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04002880 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2881 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04002882 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05002883
2884 /* Assume no Fabric and go on with discovery.
2885 * Check for outstanding ELS FLOGI to abort.
2886 */
2887
2888 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05002889 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002890
2891 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002892 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002893 break;
2894
2895 case LPFC_FABRIC_CFG_LINK:
2896 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2897 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04002898 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2899 "0223 Timeout while waiting for "
2900 "NameServer login\n");
dea31012005-04-17 16:05:31 -05002901 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05002902 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05002903 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04002904 lpfc_els_abort(phba, ndlp);
2905
2906 /* ReStart discovery */
2907 goto restart_disc;
dea31012005-04-17 16:05:31 -05002908
2909 case LPFC_NS_QRY:
2910 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04002911 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2912 "0224 NameServer Query timeout "
2913 "Data: x%x x%x\n",
2914 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002915
James Smart92d7f7b2007-06-17 19:56:38 -05002916 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2917 /* Try it one more time */
2918 vport->fc_ns_retry++;
2919 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2920 vport->fc_ns_retry, 0);
2921 if (rc == 0)
2922 break;
dea31012005-04-17 16:05:31 -05002923 }
James Smart92d7f7b2007-06-17 19:56:38 -05002924 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002925
James Smart87af33f2007-10-27 13:37:43 -04002926restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05002927 /*
2928 * Discovery is over.
2929 * set port_state to PORT_READY if SLI2.
2930 * cmpl_reg_vpi will set port_state to READY for SLI3.
2931 */
2932 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2933 lpfc_issue_reg_vpi(phba, vport);
2934 else { /* NPIV Not enabled */
2935 lpfc_issue_clear_la(phba, vport);
2936 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002937 }
2938
2939 /* Setup and issue mailbox INITIALIZE LINK command */
2940 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2941 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04002942 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2943 "0206 Device Discovery "
2944 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05002945 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002946 break;
2947 }
2948
2949 lpfc_linkdown(phba);
2950 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2951 phba->cfg_link_speed);
2952 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05002953 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002954 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002955 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04002956 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05002957 if (rc == MBX_NOT_FINISHED)
2958 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2959
2960 break;
2961
2962 case LPFC_DISC_AUTH:
2963 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04002964 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2965 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002966 lpfc_disc_flush_list(vport);
2967
James Smart92d7f7b2007-06-17 19:56:38 -05002968 /*
2969 * set port_state to PORT_READY if SLI2.
2970 * cmpl_reg_vpi will set port_state to READY for SLI3.
2971 */
2972 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2973 lpfc_issue_reg_vpi(phba, vport);
2974 else { /* NPIV Not enabled */
2975 lpfc_issue_clear_la(phba, vport);
2976 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002977 }
2978 break;
2979
James Smart2e0fef82007-06-17 19:56:36 -05002980 case LPFC_VPORT_READY:
2981 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04002982 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2983 "0231 RSCN timeout Data: x%x "
2984 "x%x\n",
2985 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002986
2987 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05002988 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05002989
James Smart2e0fef82007-06-17 19:56:36 -05002990 lpfc_els_flush_rscn(vport);
2991 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05002992 }
2993 break;
James Smart2e0fef82007-06-17 19:56:36 -05002994
James Smart92d7f7b2007-06-17 19:56:38 -05002995 default:
James Smarte8b62012007-08-02 11:10:09 -04002996 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04002997 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04002998 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05002999 break;
3000 }
3001
3002 switch (phba->link_state) {
3003 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05003004 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04003005 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3006 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05003007 clrlaerr = 1;
3008 break;
3009
James Smart09372822008-01-11 01:52:54 -05003010 case LPFC_LINK_UP:
3011 lpfc_issue_clear_la(phba, vport);
3012 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05003013 case LPFC_LINK_UNKNOWN:
3014 case LPFC_WARM_START:
3015 case LPFC_INIT_START:
3016 case LPFC_INIT_MBX_CMDS:
3017 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05003018 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04003019 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3020 "0230 Unexpected timeout, hba link "
3021 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05003022 clrlaerr = 1;
3023 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003024
3025 case LPFC_HBA_READY:
3026 break;
dea31012005-04-17 16:05:31 -05003027 }
3028
3029 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05003030 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05003031 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05003032 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
3033 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05003034 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05003035 }
3036
3037 return;
3038}
3039
dea31012005-04-17 16:05:31 -05003040/*
3041 * This routine handles processing a NameServer REG_LOGIN mailbox
3042 * command upon completion. It is setup in the LPFC_MBOXQ
3043 * as the completion routine when the command is
3044 * handed off to the SLI layer.
3045 */
3046void
James Smart2e0fef82007-06-17 19:56:36 -05003047lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003048{
James Smart2e0fef82007-06-17 19:56:36 -05003049 MAILBOX_t *mb = &pmb->mb;
3050 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3051 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3052 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003053
3054 pmb->context1 = NULL;
3055
dea31012005-04-17 16:05:31 -05003056 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05003057 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003058 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003059
James Smart2e0fef82007-06-17 19:56:36 -05003060 /*
3061 * Start issuing Fabric-Device Management Interface (FDMI) command to
3062 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
3063 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05003064 */
James Smart2e0fef82007-06-17 19:56:36 -05003065
James Smart3de2a652007-08-02 11:09:59 -04003066 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05003067 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
3068 else
3069 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05003070
James Smartfa4066b2008-01-11 01:53:27 -05003071 /* decrement the node reference count held for this callback
3072 * function.
3073 */
James Smart329f9bc2007-04-25 09:53:01 -04003074 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003075 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3076 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003077 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003078
3079 return;
3080}
3081
James Smart685f0bf2007-04-25 09:53:08 -04003082static int
3083lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
3084{
3085 uint16_t *rpi = param;
3086
3087 return ndlp->nlp_rpi == *rpi;
3088}
3089
3090static int
3091lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
3092{
3093 return memcmp(&ndlp->nlp_portname, param,
3094 sizeof(ndlp->nlp_portname)) == 0;
3095}
3096
Adrian Bunka6ababd2007-11-05 18:07:33 +01003097static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003098__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05003099{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04003100 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003101
James Smart2e0fef82007-06-17 19:56:36 -05003102 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04003103 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04003104 return ndlp;
3105 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04003106 return NULL;
dea31012005-04-17 16:05:31 -05003107}
3108
James Smart685f0bf2007-04-25 09:53:08 -04003109/*
3110 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05003111 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04003112 */
3113struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003114__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04003115{
James Smart2e0fef82007-06-17 19:56:36 -05003116 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04003117}
3118
James Smart488d1462006-03-07 15:02:37 -05003119/*
James Smart685f0bf2007-04-25 09:53:08 -04003120 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05003121 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05003122 */
3123struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05003124lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05003125{
James Smart2e0fef82007-06-17 19:56:36 -05003126 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05003127 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05003128
James Smart2e0fef82007-06-17 19:56:36 -05003129 spin_lock_irq(shost->host_lock);
3130 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
3131 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003132 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05003133}
3134
dea31012005-04-17 16:05:31 -05003135void
James Smart2e0fef82007-06-17 19:56:36 -05003136lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3137 uint32_t did)
dea31012005-04-17 16:05:31 -05003138{
3139 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05003140
3141 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04003142 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05003143
3144 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3145 "node init: did:x%x",
3146 ndlp->nlp_DID, 0, 0);
3147
dea31012005-04-17 16:05:31 -05003148 return;
3149}
James Smart329f9bc2007-04-25 09:53:01 -04003150
James Smart98c9ea52007-10-27 13:37:33 -04003151/* This routine releases all resources associated with a specifc NPort's ndlp
3152 * and mempool_free's the nodelist.
3153 */
James Smart311464e2007-08-02 11:10:37 -04003154static void
James Smart329f9bc2007-04-25 09:53:01 -04003155lpfc_nlp_release(struct kref *kref)
3156{
James Smarte47c9092008-02-08 18:49:26 -05003157 struct lpfc_hba *phba;
3158 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04003159 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
3160 kref);
James Smart858c9f62007-06-17 19:56:39 -05003161
3162 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3163 "node release: did:x%x flg:x%x type:x%x",
3164 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3165
James Smarte47c9092008-02-08 18:49:26 -05003166 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3167 "0279 lpfc_nlp_release: ndlp:x%p "
3168 "usgmap:x%x refcnt:%d\n",
3169 (void *)ndlp, ndlp->nlp_usg_map,
3170 atomic_read(&ndlp->kref.refcount));
3171
3172 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05003173 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003174
3175 /* clear the ndlp active flag for all release cases */
3176 phba = ndlp->vport->phba;
3177 spin_lock_irqsave(&phba->ndlp_lock, flags);
3178 NLP_CLR_NODE_ACT(ndlp);
3179 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3180
3181 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04003182 if (NLP_CHK_FREE_REQ(ndlp)) {
3183 kfree(ndlp->lat_data);
James Smarte47c9092008-02-08 18:49:26 -05003184 mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04003185 }
James Smart329f9bc2007-04-25 09:53:01 -04003186}
3187
James Smart98c9ea52007-10-27 13:37:33 -04003188/* This routine bumps the reference count for a ndlp structure to ensure
3189 * that one discovery thread won't free a ndlp while another discovery thread
3190 * is using it.
3191 */
James Smart329f9bc2007-04-25 09:53:01 -04003192struct lpfc_nodelist *
3193lpfc_nlp_get(struct lpfc_nodelist *ndlp)
3194{
James Smarte47c9092008-02-08 18:49:26 -05003195 struct lpfc_hba *phba;
3196 unsigned long flags;
3197
James Smart98c9ea52007-10-27 13:37:33 -04003198 if (ndlp) {
3199 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3200 "node get: did:x%x flg:x%x refcnt:x%x",
3201 ndlp->nlp_DID, ndlp->nlp_flag,
3202 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003203 /* The check of ndlp usage to prevent incrementing the
3204 * ndlp reference count that is in the process of being
3205 * released.
3206 */
3207 phba = ndlp->vport->phba;
3208 spin_lock_irqsave(&phba->ndlp_lock, flags);
3209 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
3210 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3211 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3212 "0276 lpfc_nlp_get: ndlp:x%p "
3213 "usgmap:x%x refcnt:%d\n",
3214 (void *)ndlp, ndlp->nlp_usg_map,
3215 atomic_read(&ndlp->kref.refcount));
3216 return NULL;
3217 } else
3218 kref_get(&ndlp->kref);
3219 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04003220 }
James Smart329f9bc2007-04-25 09:53:01 -04003221 return ndlp;
3222}
3223
James Smart98c9ea52007-10-27 13:37:33 -04003224/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05003225 * count goes to 0, this indicates the the associated nodelist should be
3226 * freed. Returning 1 indicates the ndlp resource has been released; on the
3227 * other hand, returning 0 indicates the ndlp resource has not been released
3228 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04003229 */
James Smart329f9bc2007-04-25 09:53:01 -04003230int
3231lpfc_nlp_put(struct lpfc_nodelist *ndlp)
3232{
James Smarte47c9092008-02-08 18:49:26 -05003233 struct lpfc_hba *phba;
3234 unsigned long flags;
3235
3236 if (!ndlp)
3237 return 1;
3238
3239 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3240 "node put: did:x%x flg:x%x refcnt:x%x",
3241 ndlp->nlp_DID, ndlp->nlp_flag,
3242 atomic_read(&ndlp->kref.refcount));
3243 phba = ndlp->vport->phba;
3244 spin_lock_irqsave(&phba->ndlp_lock, flags);
3245 /* Check the ndlp memory free acknowledge flag to avoid the
3246 * possible race condition that kref_put got invoked again
3247 * after previous one has done ndlp memory free.
3248 */
3249 if (NLP_CHK_FREE_ACK(ndlp)) {
3250 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3251 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3252 "0274 lpfc_nlp_put: ndlp:x%p "
3253 "usgmap:x%x refcnt:%d\n",
3254 (void *)ndlp, ndlp->nlp_usg_map,
3255 atomic_read(&ndlp->kref.refcount));
3256 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003257 }
James Smarte47c9092008-02-08 18:49:26 -05003258 /* Check the ndlp inactivate log flag to avoid the possible
3259 * race condition that kref_put got invoked again after ndlp
3260 * is already in inactivating state.
3261 */
3262 if (NLP_CHK_IACT_REQ(ndlp)) {
3263 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3264 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3265 "0275 lpfc_nlp_put: ndlp:x%p "
3266 "usgmap:x%x refcnt:%d\n",
3267 (void *)ndlp, ndlp->nlp_usg_map,
3268 atomic_read(&ndlp->kref.refcount));
3269 return 1;
3270 }
3271 /* For last put, mark the ndlp usage flags to make sure no
3272 * other kref_get and kref_put on the same ndlp shall get
3273 * in between the process when the final kref_put has been
3274 * invoked on this ndlp.
3275 */
3276 if (atomic_read(&ndlp->kref.refcount) == 1) {
3277 /* Indicate ndlp is put to inactive state. */
3278 NLP_SET_IACT_REQ(ndlp);
3279 /* Acknowledge ndlp memory free has been seen. */
3280 if (NLP_CHK_FREE_REQ(ndlp))
3281 NLP_SET_FREE_ACK(ndlp);
3282 }
3283 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3284 /* Note, the kref_put returns 1 when decrementing a reference
3285 * count that was 1, it invokes the release callback function,
3286 * but it still left the reference count as 1 (not actually
3287 * performs the last decrementation). Otherwise, it actually
3288 * decrements the reference count and returns 0.
3289 */
3290 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04003291}
James Smart98c9ea52007-10-27 13:37:33 -04003292
3293/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05003294 * by any other discovery thread. This routine returns 1 if the
3295 * ndlp has been freed. A return value of 0 indicates the ndlp is
3296 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04003297 */
3298int
3299lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
3300{
3301 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3302 "node not used: did:x%x flg:x%x refcnt:x%x",
3303 ndlp->nlp_DID, ndlp->nlp_flag,
3304 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003305 if (atomic_read(&ndlp->kref.refcount) == 1)
3306 if (lpfc_nlp_put(ndlp))
3307 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003308 return 0;
3309}