blob: f3dc19dfac5b92e95272fac3cbaadddcb4b076e8 [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"
33#include "lpfc_disc.h"
34#include "lpfc_sli.h"
35#include "lpfc_scsi.h"
36#include "lpfc.h"
37#include "lpfc_logmsg.h"
38#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050039#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050040#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050041
42/* AlpaArray for assignment of scsid for scan-down and bind_method */
43static uint8_t lpfcAlpaArray[] = {
44 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
45 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
46 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
47 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
48 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
49 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
50 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
51 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
52 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
53 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
54 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
55 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
56 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
57};
58
James Smart2e0fef82007-06-17 19:56:36 -050059static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010060static void lpfc_disc_flush_list(struct lpfc_vport *vport);
dea31012005-04-17 16:05:31 -050061
James Smartc01f3202006-08-18 17:47:08 -040062void
63lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050064{
James Smartc01f3202006-08-18 17:47:08 -040065 struct lpfc_rport_data *rdata;
66 struct lpfc_nodelist * ndlp;
67 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050068
James Smartc01f3202006-08-18 17:47:08 -040069 rdata = rport->dd_data;
70 ndlp = rdata->pnode;
71
James Smart58da1ff2008-04-07 10:15:56 -040072 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040073 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
74 printk(KERN_ERR "Cannot find remote node"
75 " to terminate I/O Data x%x\n",
76 rport->port_id);
dea31012005-04-17 16:05:31 -050077 return;
78 }
79
James Smart2e0fef82007-06-17 19:56:36 -050080 phba = ndlp->vport->phba;
James Smart1a169682006-03-07 15:04:06 -050081
James Smart858c9f62007-06-17 19:56:39 -050082 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
83 "rport terminate: sid:x%x did:x%x flg:x%x",
84 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
85
James Smartc01f3202006-08-18 17:47:08 -040086 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040087 lpfc_sli_abort_iocb(ndlp->vport,
88 &phba->sli.ring[phba->sli.fcp_ring],
89 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040090 }
James Smartc01f3202006-08-18 17:47:08 -040091
James Smart51ef4c22007-08-02 11:10:31 -040092 /*
93 * A device is normally blocked for rediscovery and unblocked when
94 * devloss timeout happens. In case a vport is removed or driver
95 * unloaded before devloss timeout happens, we need to unblock here.
96 */
97 scsi_target_unblock(&rport->dev);
James Smartc01f3202006-08-18 17:47:08 -040098 return;
99}
100
101/*
102 * This function will be called when dev_loss_tmo fire.
103 */
104void
105lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
106{
107 struct lpfc_rport_data *rdata;
108 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500109 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500110 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500111 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400112 int put_node;
113 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400114
115 rdata = rport->dd_data;
116 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400117 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400118 return;
James Smartc01f3202006-08-18 17:47:08 -0400119
James Smart858c9f62007-06-17 19:56:39 -0500120 vport = ndlp->vport;
121 phba = vport->phba;
122
123 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
124 "rport devlosscb: sid:x%x did:x%x flg:x%x",
125 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
126
James Smarta8adb832007-10-27 13:37:53 -0400127 /* Don't defer this if we are in the process of deleting the vport
128 * or unloading the driver. The unload will cleanup the node
129 * appropriately we just need to cleanup the ndlp rport info here.
130 */
131 if (vport->load_flag & FC_UNLOADING) {
132 put_node = rdata->pnode != NULL;
133 put_rport = ndlp->rport != NULL;
134 rdata->pnode = NULL;
135 ndlp->rport = NULL;
136 if (put_node)
137 lpfc_nlp_put(ndlp);
138 if (put_rport)
139 put_device(&rport->dev);
140 return;
141 }
142
143 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
144 return;
145
James Smart858c9f62007-06-17 19:56:39 -0500146 evtp = &ndlp->dev_loss_evt;
147
148 if (!list_empty(&evtp->evt_listp))
149 return;
150
151 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500152 /* We need to hold the node by incrementing the reference
153 * count until this queued work is done
154 */
155 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500156 evtp->evt = LPFC_EVT_DEV_LOSS;
157 list_add_tail(&evtp->evt_listp, &phba->work_list);
158 if (phba->work_wait)
159 wake_up(phba->work_wait);
160
161 spin_unlock_irq(&phba->hbalock);
162
James Smart858c9f62007-06-17 19:56:39 -0500163 return;
164}
165
166/*
167 * This function is called from the worker thread when dev_loss_tmo
168 * expire.
169 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100170static void
James Smart858c9f62007-06-17 19:56:39 -0500171lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
172{
173 struct lpfc_rport_data *rdata;
174 struct fc_rport *rport;
175 struct lpfc_vport *vport;
176 struct lpfc_hba *phba;
177 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400178 int put_node;
179 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500180 int warn_on = 0;
181
182 rport = ndlp->rport;
183
184 if (!rport)
185 return;
186
187 rdata = rport->dd_data;
188 name = (uint8_t *) &ndlp->nlp_portname;
189 vport = ndlp->vport;
190 phba = vport->phba;
191
192 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
193 "rport devlosstmo:did:x%x type:x%x id:x%x",
194 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
195
James Smarta8adb832007-10-27 13:37:53 -0400196 /* Don't defer this if we are in the process of deleting the vport
197 * or unloading the driver. The unload will cleanup the node
198 * appropriately we just need to cleanup the ndlp rport info here.
199 */
200 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500201 if (ndlp->nlp_sid != NLP_NO_SID) {
202 /* flush the target */
203 lpfc_sli_abort_iocb(vport,
204 &phba->sli.ring[phba->sli.fcp_ring],
205 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
206 }
James Smarta8adb832007-10-27 13:37:53 -0400207 put_node = rdata->pnode != NULL;
208 put_rport = ndlp->rport != NULL;
209 rdata->pnode = NULL;
210 ndlp->rport = NULL;
211 if (put_node)
212 lpfc_nlp_put(ndlp);
213 if (put_rport)
214 put_device(&rport->dev);
215 return;
216 }
217
218 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
James Smart858c9f62007-06-17 19:56:39 -0500219 return;
220
James Smart92d7f7b2007-06-17 19:56:38 -0500221 if (ndlp->nlp_type & NLP_FABRIC) {
222 /* We will clean up these Nodes in linkup */
223 put_node = rdata->pnode != NULL;
224 put_rport = ndlp->rport != NULL;
225 rdata->pnode = NULL;
226 ndlp->rport = NULL;
227 if (put_node)
228 lpfc_nlp_put(ndlp);
229 if (put_rport)
230 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400231 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500232 }
James Smart82085712007-04-25 09:52:41 -0400233
dea31012005-04-17 16:05:31 -0500234 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400235 warn_on = 1;
dea31012005-04-17 16:05:31 -0500236 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400237 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
238 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500239 }
James Smart2e0fef82007-06-17 19:56:36 -0500240 if (vport->load_flag & FC_UNLOADING)
James Smartc01f3202006-08-18 17:47:08 -0400241 warn_on = 0;
242
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400243 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400244 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
245 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400246 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
247 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400248 *name, *(name+1), *(name+2), *(name+3),
249 *(name+4), *(name+5), *(name+6), *(name+7),
250 ndlp->nlp_DID, ndlp->nlp_flag,
251 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400252 } else {
James Smarte8b62012007-08-02 11:10:09 -0400253 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
254 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400255 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
256 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400257 *name, *(name+1), *(name+2), *(name+3),
258 *(name+4), *(name+5), *(name+6), *(name+7),
259 ndlp->nlp_DID, ndlp->nlp_flag,
260 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400261 }
262
James Smart87af33f2007-10-27 13:37:43 -0400263 put_node = rdata->pnode != NULL;
264 put_rport = ndlp->rport != NULL;
265 rdata->pnode = NULL;
266 ndlp->rport = NULL;
267 if (put_node)
268 lpfc_nlp_put(ndlp);
269 if (put_rport)
270 put_device(&rport->dev);
271
James Smart2e0fef82007-06-17 19:56:36 -0500272 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400273 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400274 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smarte47c9092008-02-08 18:49:26 -0500275 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
James Smart2e0fef82007-06-17 19:56:36 -0500276 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart92d7f7b2007-06-17 19:56:38 -0500277}
James Smartc01f3202006-08-18 17:47:08 -0400278
James Smart92d7f7b2007-06-17 19:56:38 -0500279
280void
281lpfc_worker_wake_up(struct lpfc_hba *phba)
282{
283 wake_up(phba->work_wait);
dea31012005-04-17 16:05:31 -0500284 return;
285}
286
287static void
James Smart2e0fef82007-06-17 19:56:36 -0500288lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500289{
290 struct lpfc_work_evt *evtp = NULL;
291 struct lpfc_nodelist *ndlp;
292 int free_evt;
293
James Smart2e0fef82007-06-17 19:56:36 -0500294 spin_lock_irq(&phba->hbalock);
295 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500296 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
297 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500298 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500299 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500300 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500301 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500302 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500303 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500304 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500305 /* decrement the node reference count held
306 * for this queued work
307 */
308 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500309 break;
James Smart858c9f62007-06-17 19:56:39 -0500310 case LPFC_EVT_DEV_LOSS:
311 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500312 lpfc_dev_loss_tmo_handler(ndlp);
313 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500314 /* decrement the node reference count held for
315 * this queued work
316 */
James Smart858c9f62007-06-17 19:56:39 -0500317 lpfc_nlp_put(ndlp);
318 break;
dea31012005-04-17 16:05:31 -0500319 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500320 if (phba->link_state < LPFC_LINK_DOWN)
321 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500322 else
James Smart2e0fef82007-06-17 19:56:36 -0500323 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500324 complete((struct completion *)(evtp->evt_arg2));
325 break;
James Smart46fa3112007-04-25 09:51:45 -0400326 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500327 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400328 lpfc_offline_prep(phba);
329 *(int *)(evtp->evt_arg1) = 0;
330 complete((struct completion *)(evtp->evt_arg2));
331 break;
332 case LPFC_EVT_OFFLINE:
333 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500334 lpfc_sli_brdrestart(phba);
335 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400336 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
337 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500338 complete((struct completion *)(evtp->evt_arg2));
339 break;
340 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400341 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500342 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500343 lpfc_sli_brdreset(phba);
344 lpfc_hba_down_post(phba);
345 *(int *)(evtp->evt_arg1) =
346 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400347 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500348 complete((struct completion *)(evtp->evt_arg2));
349 break;
350 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400351 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500352 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500353 = (phba->pport->stopped)
354 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400355 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500356 complete((struct completion *)(evtp->evt_arg2));
357 break;
358 }
359 if (free_evt)
360 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500361 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500362 }
James Smart2e0fef82007-06-17 19:56:36 -0500363 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500364
365}
366
James Smart311464e2007-08-02 11:10:37 -0400367static void
James Smart2e0fef82007-06-17 19:56:36 -0500368lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500369{
370 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500371 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400372 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400373 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400374 int i;
dea31012005-04-17 16:05:31 -0500375
James Smart2e0fef82007-06-17 19:56:36 -0500376 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500377 ha_copy = phba->work_ha;
378 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500379 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500380
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500381 if (ha_copy & HA_ERATT)
dea31012005-04-17 16:05:31 -0500382 lpfc_handle_eratt(phba);
383
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500384 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500385 lpfc_sli_handle_mb_event(phba);
386
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500387 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500388 lpfc_handle_latt(phba);
James Smart549e55c2007-08-02 11:09:51 -0400389 vports = lpfc_create_vport_work_array(phba);
390 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500391 for(i = 0; i <= phba->max_vpi; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400392 /*
393 * We could have no vports in array if unloading, so if
394 * this happens then just use the pport
395 */
396 if (vports[i] == NULL && i == 0)
397 vport = phba->pport;
398 else
399 vport = vports[i];
400 if (vport == NULL)
401 break;
James Smart58da1ff2008-04-07 10:15:56 -0400402 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400403 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400404 vport->work_port_events &= ~work_port_events;
405 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400406 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400407 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400408 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400409 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400410 if (work_port_events & WORKER_HB_TMO)
411 lpfc_hb_timeout_handler(phba);
412 if (work_port_events & WORKER_MBOX_TMO)
413 lpfc_mbox_timeout_handler(phba);
414 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
415 lpfc_unblock_fabric_iocbs(phba);
416 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400417 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400418 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
419 lpfc_ramp_down_queue_handler(phba);
420 if (work_port_events & WORKER_RAMP_UP_QUEUE)
421 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500422 }
James Smart09372822008-01-11 01:52:54 -0500423 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500424
James Smart858c9f62007-06-17 19:56:39 -0500425 pring = &phba->sli.ring[LPFC_ELS_RING];
426 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
427 status >>= (4*LPFC_ELS_RING);
428 if ((status & HA_RXMASK)
429 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400430 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500431 pring->flag |= LPFC_DEFERRED_RING_EVENT;
432 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400433 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500434 lpfc_sli_handle_slow_ring_event(phba, pring,
435 (status &
436 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500437 }
438 /*
439 * Turn on Ring interrupts
440 */
441 spin_lock_irq(&phba->hbalock);
442 control = readl(phba->HCregaddr);
443 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
James Smarta58cbd52007-08-02 11:09:43 -0400444 lpfc_debugfs_slow_ring_trc(phba,
445 "WRK Enable ring: cntl:x%x hacopy:x%x",
446 control, ha_copy, 0);
447
James Smart858c9f62007-06-17 19:56:39 -0500448 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -0500449 writel(control, phba->HCregaddr);
450 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500451 }
James Smarta58cbd52007-08-02 11:09:43 -0400452 else {
453 lpfc_debugfs_slow_ring_trc(phba,
454 "WRK Ring ok: cntl:x%x hacopy:x%x",
455 control, ha_copy, 0);
456 }
James Smart858c9f62007-06-17 19:56:39 -0500457 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500458 }
James Smart2e0fef82007-06-17 19:56:36 -0500459 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500460}
461
462static int
James Smart2e0fef82007-06-17 19:56:36 -0500463check_work_wait_done(struct lpfc_hba *phba)
464{
James Smart92d7f7b2007-06-17 19:56:38 -0500465 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400466 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart858c9f62007-06-17 19:56:39 -0500467 int rc = 0;
James Smarted957682007-06-17 19:56:37 -0500468
James Smart2e0fef82007-06-17 19:56:36 -0500469 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500470 list_for_each_entry(vport, &phba->port_list, listentry) {
471 if (vport->work_port_events) {
472 rc = 1;
James Smart549e55c2007-08-02 11:09:51 -0400473 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500474 }
475 }
James Smart549e55c2007-08-02 11:09:51 -0400476 if (rc || phba->work_ha || (!list_empty(&phba->work_list)) ||
477 kthread_should_stop() || pring->flag & LPFC_DEFERRED_RING_EVENT) {
James Smart2e0fef82007-06-17 19:56:36 -0500478 rc = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500479 phba->work_found++;
James Smart549e55c2007-08-02 11:09:51 -0400480 } else
James Smart92d7f7b2007-06-17 19:56:38 -0500481 phba->work_found = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500482 spin_unlock_irq(&phba->hbalock);
483 return rc;
dea31012005-04-17 16:05:31 -0500484}
485
James Smart92d7f7b2007-06-17 19:56:38 -0500486
dea31012005-04-17 16:05:31 -0500487int
488lpfc_do_work(void *p)
489{
490 struct lpfc_hba *phba = p;
491 int rc;
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800492 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(work_waitq);
dea31012005-04-17 16:05:31 -0500493
494 set_user_nice(current, -20);
495 phba->work_wait = &work_waitq;
James Smart92d7f7b2007-06-17 19:56:38 -0500496 phba->work_found = 0;
dea31012005-04-17 16:05:31 -0500497
498 while (1) {
499
500 rc = wait_event_interruptible(work_waitq,
James Smart92d7f7b2007-06-17 19:56:38 -0500501 check_work_wait_done(phba));
502
dea31012005-04-17 16:05:31 -0500503 BUG_ON(rc);
504
505 if (kthread_should_stop())
506 break;
507
508 lpfc_work_done(phba);
509
James Smart92d7f7b2007-06-17 19:56:38 -0500510 /* If there is alot of slow ring work, like during link up
511 * check_work_wait_done() may cause this thread to not give
512 * up the CPU for very long periods of time. This may cause
513 * soft lockups or other problems. To avoid these situations
514 * give up the CPU here after LPFC_MAX_WORKER_ITERATION
515 * consecutive iterations.
516 */
517 if (phba->work_found >= LPFC_MAX_WORKER_ITERATION) {
518 phba->work_found = 0;
519 schedule();
520 }
dea31012005-04-17 16:05:31 -0500521 }
James Smart58da1ff2008-04-07 10:15:56 -0400522 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500523 phba->work_wait = NULL;
James Smart58da1ff2008-04-07 10:15:56 -0400524 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500525 return 0;
526}
527
528/*
529 * This is only called to handle FC worker events. Since this a rare
530 * occurance, we allocate a struct lpfc_work_evt structure here instead of
531 * embedding it in the IOCB.
532 */
533int
James Smart2e0fef82007-06-17 19:56:36 -0500534lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500535 uint32_t evt)
536{
537 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500538 unsigned long flags;
dea31012005-04-17 16:05:31 -0500539
540 /*
541 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
542 * be queued to worker thread for processing
543 */
James Smart92d7f7b2007-06-17 19:56:38 -0500544 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500545 if (!evtp)
546 return 0;
547
548 evtp->evt_arg1 = arg1;
549 evtp->evt_arg2 = arg2;
550 evtp->evt = evt;
551
James Smarted957682007-06-17 19:56:37 -0500552 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400553 list_add_tail(&evtp->evt_listp, &phba->work_list);
dea31012005-04-17 16:05:31 -0500554 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -0500555 lpfc_worker_wake_up(phba);
James Smarted957682007-06-17 19:56:37 -0500556 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500557
558 return 1;
559}
560
James Smart92d7f7b2007-06-17 19:56:38 -0500561void
562lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
563{
James Smart09372822008-01-11 01:52:54 -0500564 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500565 struct lpfc_hba *phba = vport->phba;
566 struct lpfc_nodelist *ndlp, *next_ndlp;
567 int rc;
568
569 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500570 if (!NLP_CHK_NODE_ACT(ndlp))
571 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500572 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
573 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400574 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
575 ((vport->port_type == LPFC_NPIV_PORT) &&
576 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500577 lpfc_unreg_rpi(vport, ndlp);
578
579 /* Leave Fabric nodes alone on link down */
580 if (!remove && ndlp->nlp_type & NLP_FABRIC)
581 continue;
582 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
583 remove
584 ? NLP_EVT_DEVICE_RM
585 : NLP_EVT_DEVICE_RECOVERY);
586 }
587 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
588 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500589 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500590 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500591 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500592 }
593}
594
James Smart87af33f2007-10-27 13:37:43 -0400595void
James Smart98c9ea52007-10-27 13:37:33 -0400596lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500597{
James Smart92d7f7b2007-06-17 19:56:38 -0500598 /* Cleanup any outstanding RSCN activity */
599 lpfc_els_flush_rscn(vport);
600
601 /* Cleanup any outstanding ELS commands */
602 lpfc_els_flush_cmd(vport);
603
604 lpfc_cleanup_rpis(vport, 0);
605
James Smart92d7f7b2007-06-17 19:56:38 -0500606 /* Turn off discovery timer if its running */
607 lpfc_can_disctmo(vport);
608}
609
James Smart98c9ea52007-10-27 13:37:33 -0400610static void
611lpfc_linkdown_port(struct lpfc_vport *vport)
612{
613 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
614
615 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
616
617 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
618 "Link Down: state:x%x rtry:x%x flg:x%x",
619 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
620
621 lpfc_port_link_failure(vport);
622
623}
624
dea31012005-04-17 16:05:31 -0500625int
James Smart685f0bf2007-04-25 09:53:08 -0400626lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500627{
James Smart2e0fef82007-06-17 19:56:36 -0500628 struct lpfc_vport *vport = phba->pport;
629 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400630 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400631 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400632 int i;
dea31012005-04-17 16:05:31 -0500633
James Smart3163f722008-02-08 18:50:25 -0500634 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500635 return 0;
James Smart2e0fef82007-06-17 19:56:36 -0500636 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500637 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500638 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500639 phba->pport->fc_flag &= ~FC_LBIT;
640 }
James Smart2e0fef82007-06-17 19:56:36 -0500641 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400642 vports = lpfc_create_vport_work_array(phba);
643 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500644 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400645 /* Issue a LINK DOWN event to all nodes */
646 lpfc_linkdown_port(vports[i]);
647 }
James Smart09372822008-01-11 01:52:54 -0500648 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500649 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500650 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
651 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500652 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500653 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500654 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400655 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500656 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500657 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500658 }
659 }
660
dea31012005-04-17 16:05:31 -0500661 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500662 if (phba->pport->fc_flag & FC_PT2PT) {
663 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500664 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
665 if (mb) {
dea31012005-04-17 16:05:31 -0500666 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500667 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500668 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400669 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500670 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500671 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500672 }
673 }
James Smart2e0fef82007-06-17 19:56:36 -0500674 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500675 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500676 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500677 }
James Smart2e0fef82007-06-17 19:56:36 -0500678
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500679 return 0;
dea31012005-04-17 16:05:31 -0500680}
681
James Smart92d7f7b2007-06-17 19:56:38 -0500682static void
683lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500684{
James Smart92d7f7b2007-06-17 19:56:38 -0500685 struct lpfc_nodelist *ndlp;
686
687 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500688 if (!NLP_CHK_NODE_ACT(ndlp))
689 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500690 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
691 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500692 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500693 /* On Linkup its safe to clean up the ndlp
694 * from Fabric connections.
695 */
James Smart92d7f7b2007-06-17 19:56:38 -0500696 if (ndlp->nlp_DID != Fabric_DID)
697 lpfc_unreg_rpi(vport, ndlp);
698 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
699 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500700 /* Fail outstanding IO now since device is
701 * marked for PLOGI.
702 */
James Smart92d7f7b2007-06-17 19:56:38 -0500703 lpfc_unreg_rpi(vport, ndlp);
704 }
705 }
706}
707
708static void
709lpfc_linkup_port(struct lpfc_vport *vport)
710{
711 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500712 struct lpfc_hba *phba = vport->phba;
713
714 if ((vport->load_flag & FC_UNLOADING) != 0)
715 return;
716
James Smart858c9f62007-06-17 19:56:39 -0500717 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
718 "Link Up: top:x%x speed:x%x flg:x%x",
719 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
720
James Smart92d7f7b2007-06-17 19:56:38 -0500721 /* If NPIV is not enabled, only bring the physical port up */
722 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
723 (vport != phba->pport))
724 return;
dea31012005-04-17 16:05:31 -0500725
James Smart2e0fef82007-06-17 19:56:36 -0500726 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400727
James Smart2e0fef82007-06-17 19:56:36 -0500728 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500729 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
730 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
731 vport->fc_flag |= FC_NDISC_ACTIVE;
732 vport->fc_ns_retry = 0;
733 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500734
James Smart92d7f7b2007-06-17 19:56:38 -0500735 if (vport->fc_flag & FC_LBIT)
736 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500737
James Smart92d7f7b2007-06-17 19:56:38 -0500738}
739
740static int
741lpfc_linkup(struct lpfc_hba *phba)
742{
James Smart549e55c2007-08-02 11:09:51 -0400743 struct lpfc_vport **vports;
744 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500745
746 phba->link_state = LPFC_LINK_UP;
747
748 /* Unblock fabric iocbs if they are blocked */
749 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
750 del_timer_sync(&phba->fabric_block_timer);
751
James Smart549e55c2007-08-02 11:09:51 -0400752 vports = lpfc_create_vport_work_array(phba);
753 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500754 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400755 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500756 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500757 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
758 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500759
760 return 0;
761}
762
763/*
764 * This routine handles processing a CLEAR_LA mailbox
765 * command upon completion. It is setup in the LPFC_MBOXQ
766 * as the completion routine when the command is
767 * handed off to the SLI layer.
768 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100769static void
James Smart2e0fef82007-06-17 19:56:36 -0500770lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500771{
James Smart2e0fef82007-06-17 19:56:36 -0500772 struct lpfc_vport *vport = pmb->vport;
773 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
774 struct lpfc_sli *psli = &phba->sli;
775 MAILBOX_t *mb = &pmb->mb;
dea31012005-04-17 16:05:31 -0500776 uint32_t control;
777
dea31012005-04-17 16:05:31 -0500778 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500779 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500780 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
781 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
782
783 /* Check for error */
784 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500785 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400786 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
787 "0320 CLEAR_LA mbxStatus error x%x hba "
788 "state x%x\n",
789 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500790 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500791 goto out;
792 }
793
James Smart92d7f7b2007-06-17 19:56:38 -0500794 if (vport->port_type == LPFC_PHYSICAL_PORT)
795 phba->link_state = LPFC_HBA_READY;
796
797 spin_lock_irq(&phba->hbalock);
798 psli->sli_flag |= LPFC_PROCESS_LA;
799 control = readl(phba->HCregaddr);
800 control |= HC_LAINT_ENA;
801 writel(control, phba->HCregaddr);
802 readl(phba->HCregaddr); /* flush */
803 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500804 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500805 return;
dea31012005-04-17 16:05:31 -0500806
dea31012005-04-17 16:05:31 -0500807out:
808 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400809 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
810 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500811 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500812
James Smart2e0fef82007-06-17 19:56:36 -0500813 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400814 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500815 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500816
James Smart2e0fef82007-06-17 19:56:36 -0500817 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500818
819 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500820
821 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500822 psli->sli_flag |= LPFC_PROCESS_LA;
823 control = readl(phba->HCregaddr);
824 control |= HC_LAINT_ENA;
825 writel(control, phba->HCregaddr);
826 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500827 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500828
829 return;
830}
831
James Smart2e0fef82007-06-17 19:56:36 -0500832
dea31012005-04-17 16:05:31 -0500833static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500834lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500835{
James Smart2e0fef82007-06-17 19:56:36 -0500836 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500837
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500838 if (pmb->mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500839 goto out;
dea31012005-04-17 16:05:31 -0500840
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500841 mempool_free(pmb, phba->mbox_mem_pool);
842
843 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500844 vport->fc_flag & FC_PUBLIC_LOOP &&
845 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500846 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500847 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500848 * LPFC_LOCAL_CFG_LINK while waiting for FAN
849 */
James Smart2e0fef82007-06-17 19:56:36 -0500850 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500851 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500852 }
dea31012005-04-17 16:05:31 -0500853
James Smart2e0fef82007-06-17 19:56:36 -0500854 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500855 * LPFC_FLOGI while waiting for FLOGI cmpl
856 */
James Smart92d7f7b2007-06-17 19:56:38 -0500857 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500858 lpfc_initial_flogi(vport);
859 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500860 return;
dea31012005-04-17 16:05:31 -0500861
862out:
James Smarte8b62012007-08-02 11:10:09 -0400863 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
864 "0306 CONFIG_LINK mbxStatus error x%x "
865 "HBA state x%x\n",
866 pmb->mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500867 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500868
869 lpfc_linkdown(phba);
870
James Smarte8b62012007-08-02 11:10:09 -0400871 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
872 "0200 CONFIG_LINK bad hba state x%x\n",
873 vport->port_state);
dea31012005-04-17 16:05:31 -0500874
James Smart92d7f7b2007-06-17 19:56:38 -0500875 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500876 return;
877}
878
879static void
James Smart2e0fef82007-06-17 19:56:36 -0500880lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500881{
dea31012005-04-17 16:05:31 -0500882 MAILBOX_t *mb = &pmb->mb;
883 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500884 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500885
886
887 /* Check for error */
888 if (mb->mbxStatus) {
889 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400890 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
891 "0319 READ_SPARAM mbxStatus error x%x "
892 "hba state x%x>\n",
893 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -0500894 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -0500895 goto out;
896 }
897
James Smart2e0fef82007-06-17 19:56:36 -0500898 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -0500899 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -0500900 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500901 u64_to_wwn(phba->cfg_soft_wwnn,
902 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400903 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500904 u64_to_wwn(phba->cfg_soft_wwpn,
905 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -0500906 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
907 sizeof(vport->fc_nodename));
908 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
909 sizeof(vport->fc_portname));
910 if (vport->port_type == LPFC_PHYSICAL_PORT) {
911 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
912 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
913 }
914
dea31012005-04-17 16:05:31 -0500915 lpfc_mbuf_free(phba, mp->virt, mp->phys);
916 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -0500917 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500918 return;
919
920out:
921 pmb->context1 = NULL;
922 lpfc_mbuf_free(phba, mp->virt, mp->phys);
923 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -0500924 lpfc_issue_clear_la(phba, vport);
925 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500926 return;
927}
928
929static void
James Smart92d7f7b2007-06-17 19:56:38 -0500930lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -0500931{
James Smart92d7f7b2007-06-17 19:56:38 -0500932 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500933 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
James Smart2e0fef82007-06-17 19:56:36 -0500934 int i;
James Smart14691152006-12-02 13:34:28 -0500935 struct lpfc_dmabuf *mp;
936 int rc;
937
dea31012005-04-17 16:05:31 -0500938 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
939 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
940
James Smart92d7f7b2007-06-17 19:56:38 -0500941 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500942 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -0500943 case LA_1GHZ_LINK:
944 phba->fc_linkspeed = LA_1GHZ_LINK;
945 break;
946 case LA_2GHZ_LINK:
947 phba->fc_linkspeed = LA_2GHZ_LINK;
948 break;
949 case LA_4GHZ_LINK:
950 phba->fc_linkspeed = LA_4GHZ_LINK;
951 break;
952 case LA_8GHZ_LINK:
953 phba->fc_linkspeed = LA_8GHZ_LINK;
954 break;
955 default:
956 phba->fc_linkspeed = LA_UNKNW_LINK;
957 break;
dea31012005-04-17 16:05:31 -0500958 }
959
960 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -0500961 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -0500962
963 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -0500964 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -0500965
James Smart92d7f7b2007-06-17 19:56:38 -0500966 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -0500967 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -0500968 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500969
James Smart2e0fef82007-06-17 19:56:36 -0500970 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -0500971 i = la->un.lilpBde64.tus.f.bdeSize;
972
973 if (i == 0) {
974 phba->alpa_map[0] = 0;
975 } else {
James Smarte8b62012007-08-02 11:10:09 -0400976 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -0500977 int numalpa, j, k;
978 union {
979 uint8_t pamap[16];
980 struct {
981 uint32_t wd1;
982 uint32_t wd2;
983 uint32_t wd3;
984 uint32_t wd4;
985 } pa;
986 } un;
987 numalpa = phba->alpa_map[0];
988 j = 0;
989 while (j < numalpa) {
990 memset(un.pamap, 0, 16);
991 for (k = 1; j < numalpa; k++) {
992 un.pamap[k - 1] =
993 phba->alpa_map[j + 1];
994 j++;
995 if (k == 16)
996 break;
997 }
998 /* Link Up Event ALPA map */
999 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05001000 KERN_WARNING,
1001 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001002 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05001003 "ALPA map Data: x%x "
1004 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001005 un.pa.wd1, un.pa.wd2,
1006 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05001007 }
1008 }
1009 }
1010 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05001011 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04001012 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05001013 (phba->sli_rev == 3))
1014 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1015 }
James Smart2e0fef82007-06-17 19:56:36 -05001016 vport->fc_myDID = phba->fc_pref_DID;
1017 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001018 }
James Smart92d7f7b2007-06-17 19:56:38 -05001019 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001020
1021 lpfc_linkup(phba);
1022 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001023 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05001024 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05001025 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04001026 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05001027 if (rc == MBX_NOT_FINISHED) {
1028 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1029 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1030 kfree(mp);
1031 mempool_free(sparam_mbox, phba->mbox_mem_pool);
1032 if (cfglink_mbox)
1033 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001034 goto out;
James Smart14691152006-12-02 13:34:28 -05001035 }
dea31012005-04-17 16:05:31 -05001036 }
1037
1038 if (cfglink_mbox) {
James Smart2e0fef82007-06-17 19:56:36 -05001039 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05001040 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05001041 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001042 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04001043 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001044 if (rc != MBX_NOT_FINISHED)
1045 return;
1046 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001047 }
James Smart92d7f7b2007-06-17 19:56:38 -05001048out:
1049 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001050 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1051 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1052 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05001053 lpfc_issue_clear_la(phba, vport);
1054 return;
dea31012005-04-17 16:05:31 -05001055}
1056
1057static void
James Smart2e0fef82007-06-17 19:56:36 -05001058lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1059{
dea31012005-04-17 16:05:31 -05001060 uint32_t control;
1061 struct lpfc_sli *psli = &phba->sli;
1062
1063 lpfc_linkdown(phba);
1064
1065 /* turn on Link Attention interrupts - no CLEAR_LA needed */
James Smart2e0fef82007-06-17 19:56:36 -05001066 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001067 psli->sli_flag |= LPFC_PROCESS_LA;
1068 control = readl(phba->HCregaddr);
1069 control |= HC_LAINT_ENA;
1070 writel(control, phba->HCregaddr);
1071 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001072 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001073}
1074
1075/*
1076 * This routine handles processing a READ_LA mailbox
1077 * command upon completion. It is setup in the LPFC_MBOXQ
1078 * as the completion routine when the command is
1079 * handed off to the SLI layer.
1080 */
1081void
James Smart2e0fef82007-06-17 19:56:36 -05001082lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001083{
James Smart2e0fef82007-06-17 19:56:36 -05001084 struct lpfc_vport *vport = pmb->vport;
1085 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05001086 READ_LA_VAR *la;
1087 MAILBOX_t *mb = &pmb->mb;
1088 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1089
James Smart0d2b6b82008-06-14 22:52:47 -04001090 /* Unblock ELS traffic */
1091 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001092 /* Check for error */
1093 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05001094 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001095 "1307 READ_LA mbox error x%x state x%x\n",
1096 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001097 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001098 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001099 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1100 }
1101
1102 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1103
1104 memcpy(&phba->alpa_map[0], mp->virt, 128);
1105
James Smart2e0fef82007-06-17 19:56:36 -05001106 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001107 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05001108 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001109 else
James Smart2e0fef82007-06-17 19:56:36 -05001110 vport->fc_flag &= ~FC_BYPASSED_MODE;
1111 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001112
dea31012005-04-17 16:05:31 -05001113 if (((phba->fc_eventTag + 1) < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05001114 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05001115 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05001116 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05001117 if (phba->fc_eventTag != 0)
1118 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001119 }
dea31012005-04-17 16:05:31 -05001120
1121 phba->fc_eventTag = la->eventTag;
1122
1123 if (la->attType == AT_LINK_UP) {
1124 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05001125 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05001126 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001127 "1306 Link Up Event in loop back mode "
1128 "x%x received Data: x%x x%x x%x x%x\n",
1129 la->eventTag, phba->fc_eventTag,
1130 la->granted_AL_PA, la->UlnkSpeed,
1131 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001132 } else {
1133 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001134 "1303 Link Up Event x%x received "
1135 "Data: x%x x%x x%x x%x\n",
1136 la->eventTag, phba->fc_eventTag,
1137 la->granted_AL_PA, la->UlnkSpeed,
1138 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001139 }
James Smart92d7f7b2007-06-17 19:56:38 -05001140 lpfc_mbx_process_link_up(phba, la);
dea31012005-04-17 16:05:31 -05001141 } else {
1142 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05001143 if (phba->link_flag & LS_LOOPBACK_MODE) {
1144 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1145 "1308 Link Down Event in loop back mode "
1146 "x%x received "
1147 "Data: x%x x%x x%x\n",
1148 la->eventTag, phba->fc_eventTag,
1149 phba->pport->port_state, vport->fc_flag);
1150 }
1151 else {
1152 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001153 "1305 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05001154 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001155 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05001156 phba->pport->port_state, vport->fc_flag);
James Smart3163f722008-02-08 18:50:25 -05001157 }
dea31012005-04-17 16:05:31 -05001158 lpfc_mbx_issue_link_down(phba);
1159 }
1160
1161lpfc_mbx_cmpl_read_la_free_mbuf:
1162 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1163 kfree(mp);
1164 mempool_free(pmb, phba->mbox_mem_pool);
1165 return;
1166}
1167
1168/*
1169 * This routine handles processing a REG_LOGIN mailbox
1170 * command upon completion. It is setup in the LPFC_MBOXQ
1171 * as the completion routine when the command is
1172 * handed off to the SLI layer.
1173 */
1174void
James Smart2e0fef82007-06-17 19:56:36 -05001175lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001176{
James Smart2e0fef82007-06-17 19:56:36 -05001177 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001178 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05001179 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001180
dea31012005-04-17 16:05:31 -05001181 pmb->context1 = NULL;
1182
1183 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05001184 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05001185 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1186 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001187 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05001188 /* decrement the node reference count held for this callback
1189 * function.
1190 */
James Smart329f9bc2007-04-25 09:53:01 -04001191 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001192
1193 return;
1194}
1195
James Smart92d7f7b2007-06-17 19:56:38 -05001196static void
1197lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1198{
1199 MAILBOX_t *mb = &pmb->mb;
1200 struct lpfc_vport *vport = pmb->vport;
1201 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1202
1203 switch (mb->mbxStatus) {
1204 case 0x0011:
1205 case 0x0020:
1206 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04001207 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1208 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
1209 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001210 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001211 }
1212 vport->unreg_vpi_cmpl = VPORT_OK;
1213 mempool_free(pmb, phba->mbox_mem_pool);
1214 /*
1215 * This shost reference might have been taken at the beginning of
1216 * lpfc_vport_delete()
1217 */
1218 if (vport->load_flag & FC_UNLOADING)
1219 scsi_host_put(shost);
1220}
1221
1222void
1223lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1224{
1225 struct lpfc_hba *phba = vport->phba;
1226 LPFC_MBOXQ_t *mbox;
1227 int rc;
1228
1229 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1230 if (!mbox)
1231 return;
1232
1233 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1234 mbox->vport = vport;
1235 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04001236 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001237 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001238 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1239 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001240 mempool_free(mbox, phba->mbox_mem_pool);
1241 vport->unreg_vpi_cmpl = VPORT_ERROR;
1242 }
1243}
1244
1245static void
1246lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1247{
1248 struct lpfc_vport *vport = pmb->vport;
1249 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1250 MAILBOX_t *mb = &pmb->mb;
1251
1252 switch (mb->mbxStatus) {
1253 case 0x0011:
1254 case 0x9601:
1255 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04001256 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1257 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
1258 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001259 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1260 spin_lock_irq(shost->host_lock);
1261 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1262 spin_unlock_irq(shost->host_lock);
1263 vport->fc_myDID = 0;
1264 goto out;
1265 }
James Smart92d7f7b2007-06-17 19:56:38 -05001266
1267 vport->num_disc_nodes = 0;
1268 /* go thru NPR list and issue ELS PLOGIs */
1269 if (vport->fc_npr_cnt)
1270 lpfc_els_disc_plogi(vport);
1271
1272 if (!vport->num_disc_nodes) {
1273 spin_lock_irq(shost->host_lock);
1274 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1275 spin_unlock_irq(shost->host_lock);
1276 lpfc_can_disctmo(vport);
1277 }
1278 vport->port_state = LPFC_VPORT_READY;
1279
1280out:
1281 mempool_free(pmb, phba->mbox_mem_pool);
1282 return;
1283}
1284
dea31012005-04-17 16:05:31 -05001285/*
1286 * This routine handles processing a Fabric REG_LOGIN mailbox
1287 * command upon completion. It is setup in the LPFC_MBOXQ
1288 * as the completion routine when the command is
1289 * handed off to the SLI layer.
1290 */
1291void
James Smart2e0fef82007-06-17 19:56:36 -05001292lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001293{
James Smart92d7f7b2007-06-17 19:56:38 -05001294 struct lpfc_vport *vport = pmb->vport;
James Smart2e0fef82007-06-17 19:56:36 -05001295 MAILBOX_t *mb = &pmb->mb;
1296 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05001297 struct lpfc_nodelist *ndlp;
James Smart549e55c2007-08-02 11:09:51 -04001298 struct lpfc_vport **vports;
1299 int i;
dea31012005-04-17 16:05:31 -05001300
James Smart549e55c2007-08-02 11:09:51 -04001301 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04001302 pmb->context1 = NULL;
1303 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05001304 if (mb->mbxStatus) {
1305 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1306 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001307 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001308
James Smart92d7f7b2007-06-17 19:56:38 -05001309 if (phba->fc_topology == TOPOLOGY_LOOP) {
1310 /* FLOGI failed, use loop map to make discovery list */
1311 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001312
James Smart92d7f7b2007-06-17 19:56:38 -05001313 /* Start discovery */
1314 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05001315 /* Decrement the reference count to ndlp after the
1316 * reference to the ndlp are done.
1317 */
1318 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05001319 return;
1320 }
1321
1322 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001323 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1324 "0258 Register Fabric login error: 0x%x\n",
1325 mb->mbxStatus);
James Smarte47c9092008-02-08 18:49:26 -05001326 /* Decrement the reference count to ndlp after the reference
1327 * to the ndlp are done.
1328 */
1329 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001330 return;
1331 }
1332
dea31012005-04-17 16:05:31 -05001333 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001334 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001335 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001336
James Smart2e0fef82007-06-17 19:56:36 -05001337 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart549e55c2007-08-02 11:09:51 -04001338 vports = lpfc_create_vport_work_array(phba);
1339 if (vports != NULL)
1340 for(i = 0;
James Smart09372822008-01-11 01:52:54 -05001341 i <= phba->max_vpi && vports[i] != NULL;
James Smart549e55c2007-08-02 11:09:51 -04001342 i++) {
1343 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1344 continue;
James Smart58da1ff2008-04-07 10:15:56 -04001345 if (phba->fc_topology == TOPOLOGY_LOOP) {
1346 lpfc_vport_set_state(vports[i],
1347 FC_VPORT_LINKDOWN);
1348 continue;
1349 }
James Smart549e55c2007-08-02 11:09:51 -04001350 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1351 lpfc_initial_fdisc(vports[i]);
James Smart58da1ff2008-04-07 10:15:56 -04001352 else {
James Smart549e55c2007-08-02 11:09:51 -04001353 lpfc_vport_set_state(vports[i],
1354 FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -04001355 lpfc_printf_vlog(vport, KERN_ERR,
1356 LOG_ELS,
1357 "0259 No NPIV "
1358 "Fabric support\n");
James Smart549e55c2007-08-02 11:09:51 -04001359 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001360 }
James Smart09372822008-01-11 01:52:54 -05001361 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001362 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05001363 }
1364
1365 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1366 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001367 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05001368
1369 /* Drop the reference count from the mbox at the end after
1370 * all the current reference to the ndlp have been done.
1371 */
1372 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001373 return;
1374}
1375
1376/*
1377 * This routine handles processing a NameServer REG_LOGIN mailbox
1378 * command upon completion. It is setup in the LPFC_MBOXQ
1379 * as the completion routine when the command is
1380 * handed off to the SLI layer.
1381 */
1382void
James Smart2e0fef82007-06-17 19:56:36 -05001383lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001384{
James Smart2e0fef82007-06-17 19:56:36 -05001385 MAILBOX_t *mb = &pmb->mb;
1386 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1387 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1388 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001389
1390 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05001391out:
James Smartfa4066b2008-01-11 01:53:27 -05001392 /* decrement the node reference count held for this
1393 * callback function.
1394 */
James Smart329f9bc2007-04-25 09:53:01 -04001395 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001396 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1397 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04001398 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04001399
1400 /* If no other thread is using the ndlp, free it */
1401 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05001402
James Smart92d7f7b2007-06-17 19:56:38 -05001403 if (phba->fc_topology == TOPOLOGY_LOOP) {
1404 /*
1405 * RegLogin failed, use loop map to make discovery
1406 * list
1407 */
1408 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001409
James Smart92d7f7b2007-06-17 19:56:38 -05001410 /* Start discovery */
1411 lpfc_disc_start(vport);
1412 return;
1413 }
1414 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001415 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1416 "0260 Register NameServer error: 0x%x\n",
1417 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001418 return;
1419 }
1420
1421 pmb->context1 = NULL;
1422
dea31012005-04-17 16:05:31 -05001423 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001424 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001425 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001426
James Smart2e0fef82007-06-17 19:56:36 -05001427 if (vport->port_state < LPFC_VPORT_READY) {
1428 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05001429 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1430 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1431 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1432 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1433 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1434
1435 /* Issue SCR just before NameServer GID_FT Query */
1436 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05001437 }
1438
James Smart2e0fef82007-06-17 19:56:36 -05001439 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05001440 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05001441 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05001442 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05001443 goto out;
dea31012005-04-17 16:05:31 -05001444 }
1445
James Smartfa4066b2008-01-11 01:53:27 -05001446 /* decrement the node reference count held for this
1447 * callback function.
1448 */
James Smart329f9bc2007-04-25 09:53:01 -04001449 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001450 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1451 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001452 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001453
1454 return;
1455}
1456
1457static void
James Smart2e0fef82007-06-17 19:56:36 -05001458lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001459{
James Smart2e0fef82007-06-17 19:56:36 -05001460 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1461 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05001462 struct lpfc_rport_data *rdata;
1463 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05001464 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001465
1466 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001467 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1468 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001469 rport_ids.port_id = ndlp->nlp_DID;
1470 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05001471
James Smart329f9bc2007-04-25 09:53:01 -04001472 /*
1473 * We leave our node pointer in rport->dd_data when we unregister a
1474 * FCP target port. But fc_remote_port_add zeros the space to which
1475 * rport->dd_data points. So, if we're reusing a previously
1476 * registered port, drop the reference that we took the last time we
1477 * registered the port.
1478 */
1479 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05001480 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04001481 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05001482
1483 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1484 "rport add: did:x%x flg:x%x type x%x",
1485 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1486
James Smart2e0fef82007-06-17 19:56:36 -05001487 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04001488 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05001489 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1490 "Warning: fc_remote_port_add failed\n");
1491 return;
1492 }
1493
1494 /* initialize static port data */
1495 rport->maxframe_size = ndlp->nlp_maxframe;
1496 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05001497 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04001498 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001499
1500 if (ndlp->nlp_type & NLP_FCP_TARGET)
1501 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1502 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1503 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1504
1505
1506 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1507 fc_remote_port_rolechg(rport, rport_ids.roles);
1508
James Smart071fbd3d2006-04-15 11:53:20 -04001509 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001510 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04001511 ndlp->nlp_sid = rport->scsi_target_id;
1512 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001513 return;
1514}
1515
1516static void
James Smart2e0fef82007-06-17 19:56:36 -05001517lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001518{
1519 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04001520
James Smart858c9f62007-06-17 19:56:39 -05001521 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
1522 "rport delete: did:x%x flg:x%x type x%x",
1523 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1524
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001525 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05001526
1527 return;
1528}
1529
James Smartde0c5b32007-04-25 09:52:27 -04001530static void
James Smart2e0fef82007-06-17 19:56:36 -05001531lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04001532{
James Smart2e0fef82007-06-17 19:56:36 -05001533 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1534
1535 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001536 switch (state) {
1537 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001538 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001539 break;
1540 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001541 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001542 break;
1543 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001544 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001545 break;
1546 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001547 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001548 break;
1549 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001550 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001551 break;
1552 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001553 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001554 break;
1555 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001556 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001557 break;
1558 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001559 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001560 break;
1561 }
James Smart2e0fef82007-06-17 19:56:36 -05001562 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001563}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001564
James Smartde0c5b32007-04-25 09:52:27 -04001565static void
James Smart2e0fef82007-06-17 19:56:36 -05001566lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04001567 int old_state, int new_state)
1568{
James Smart2e0fef82007-06-17 19:56:36 -05001569 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1570
James Smartde0c5b32007-04-25 09:52:27 -04001571 if (new_state == NLP_STE_UNMAPPED_NODE) {
1572 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1573 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1574 ndlp->nlp_type |= NLP_FC_NODE;
1575 }
1576 if (new_state == NLP_STE_MAPPED_NODE)
1577 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1578 if (new_state == NLP_STE_NPR_NODE)
1579 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05001580
James Smartde0c5b32007-04-25 09:52:27 -04001581 /* Transport interface */
1582 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1583 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001584 vport->phba->nport_event_cnt++;
1585 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001586 }
dea31012005-04-17 16:05:31 -05001587
James Smartde0c5b32007-04-25 09:52:27 -04001588 if (new_state == NLP_STE_MAPPED_NODE ||
1589 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05001590 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05001591 /*
1592 * Tell the fc transport about the port, if we haven't
1593 * already. If we have, and it's a scsi entity, be
1594 * sure to unblock any attached scsi devices
1595 */
1596 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001597 }
James Smart858c9f62007-06-17 19:56:39 -05001598 /*
1599 * if we added to Mapped list, but the remote port
1600 * registration failed or assigned a target id outside
1601 * our presentable range - move the node to the
1602 * Unmapped List
1603 */
James Smartde0c5b32007-04-25 09:52:27 -04001604 if (new_state == NLP_STE_MAPPED_NODE &&
1605 (!ndlp->rport ||
1606 ndlp->rport->scsi_target_id == -1 ||
1607 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05001608 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001609 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05001610 spin_unlock_irq(shost->host_lock);
1611 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001612 }
James Smartde0c5b32007-04-25 09:52:27 -04001613}
1614
James Smart685f0bf2007-04-25 09:53:08 -04001615static char *
1616lpfc_nlp_state_name(char *buffer, size_t size, int state)
1617{
1618 static char *states[] = {
1619 [NLP_STE_UNUSED_NODE] = "UNUSED",
1620 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1621 [NLP_STE_ADISC_ISSUE] = "ADISC",
1622 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1623 [NLP_STE_PRLI_ISSUE] = "PRLI",
1624 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1625 [NLP_STE_MAPPED_NODE] = "MAPPED",
1626 [NLP_STE_NPR_NODE] = "NPR",
1627 };
1628
James Smart311464e2007-08-02 11:10:37 -04001629 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04001630 strlcpy(buffer, states[state], size);
1631 else
1632 snprintf(buffer, size, "unknown (%d)", state);
1633 return buffer;
1634}
1635
James Smartde0c5b32007-04-25 09:52:27 -04001636void
James Smart2e0fef82007-06-17 19:56:36 -05001637lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1638 int state)
James Smartde0c5b32007-04-25 09:52:27 -04001639{
James Smart2e0fef82007-06-17 19:56:36 -05001640 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04001641 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04001642 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04001643
James Smarte8b62012007-08-02 11:10:09 -04001644 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1645 "0904 NPort state transition x%06x, %s -> %s\n",
1646 ndlp->nlp_DID,
1647 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1648 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05001649
1650 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1651 "node statechg did:x%x old:%d ste:%d",
1652 ndlp->nlp_DID, old_state, state);
1653
James Smartde0c5b32007-04-25 09:52:27 -04001654 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04001655 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001656 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001657 if (old_state == NLP_STE_UNMAPPED_NODE) {
1658 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1659 ndlp->nlp_type &= ~NLP_FC_NODE;
1660 }
1661
James Smart685f0bf2007-04-25 09:53:08 -04001662 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05001663 spin_lock_irq(shost->host_lock);
1664 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1665 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001666 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05001667 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04001668
1669 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05001670 lpfc_nlp_counters(vport, state, 1);
1671 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04001672}
1673
1674void
James Smarte47c9092008-02-08 18:49:26 -05001675lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1676{
1677 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1678
1679 if (list_empty(&ndlp->nlp_listp)) {
1680 spin_lock_irq(shost->host_lock);
1681 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1682 spin_unlock_irq(shost->host_lock);
1683 }
1684}
1685
1686void
James Smart2e0fef82007-06-17 19:56:36 -05001687lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001688{
James Smart2e0fef82007-06-17 19:56:36 -05001689 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1690
James Smart0d2b6b82008-06-14 22:52:47 -04001691 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001692 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05001693 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1694 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001695 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05001696 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05001697 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05001698 NLP_STE_UNUSED_NODE);
1699}
1700
Adrian Bunk4d9db012008-02-14 23:24:02 +02001701static void
James Smarte47c9092008-02-08 18:49:26 -05001702lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1703{
James Smart0d2b6b82008-06-14 22:52:47 -04001704 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05001705 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1706 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1707 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
1708 NLP_STE_UNUSED_NODE);
1709}
1710
1711struct lpfc_nodelist *
1712lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1713 int state)
1714{
1715 struct lpfc_hba *phba = vport->phba;
1716 uint32_t did;
1717 unsigned long flags;
1718
1719 if (!ndlp)
1720 return NULL;
1721
1722 spin_lock_irqsave(&phba->ndlp_lock, flags);
1723 /* The ndlp should not be in memory free mode */
1724 if (NLP_CHK_FREE_REQ(ndlp)) {
1725 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1726 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1727 "0277 lpfc_enable_node: ndlp:x%p "
1728 "usgmap:x%x refcnt:%d\n",
1729 (void *)ndlp, ndlp->nlp_usg_map,
1730 atomic_read(&ndlp->kref.refcount));
1731 return NULL;
1732 }
1733 /* The ndlp should not already be in active mode */
1734 if (NLP_CHK_NODE_ACT(ndlp)) {
1735 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1736 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1737 "0278 lpfc_enable_node: ndlp:x%p "
1738 "usgmap:x%x refcnt:%d\n",
1739 (void *)ndlp, ndlp->nlp_usg_map,
1740 atomic_read(&ndlp->kref.refcount));
1741 return NULL;
1742 }
1743
1744 /* Keep the original DID */
1745 did = ndlp->nlp_DID;
1746
1747 /* re-initialize ndlp except of ndlp linked list pointer */
1748 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
1749 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
1750 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
1751 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
1752 init_timer(&ndlp->nlp_delayfunc);
1753 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
1754 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
1755 ndlp->nlp_DID = did;
1756 ndlp->vport = vport;
1757 ndlp->nlp_sid = NLP_NO_SID;
1758 /* ndlp management re-initialize */
1759 kref_init(&ndlp->kref);
1760 NLP_INT_NODE_ACT(ndlp);
1761
1762 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1763
1764 if (state != NLP_STE_UNUSED_NODE)
1765 lpfc_nlp_set_state(vport, ndlp, state);
1766
1767 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1768 "node enable: did:x%x",
1769 ndlp->nlp_DID, 0, 0);
1770 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04001771}
1772
1773void
James Smart2e0fef82007-06-17 19:56:36 -05001774lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001775{
James Smart87af33f2007-10-27 13:37:43 -04001776 /*
James Smartfa4066b2008-01-11 01:53:27 -05001777 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04001778 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05001779 * the ndlp from the vport. The ndlp marked as UNUSED on the list
1780 * until ALL other outstanding threads have completed. We check
1781 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04001782 */
James Smartfa4066b2008-01-11 01:53:27 -05001783 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1784 return;
James Smart51ef4c22007-08-02 11:10:31 -04001785 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04001786 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04001787 return;
dea31012005-04-17 16:05:31 -05001788}
1789
1790/*
1791 * Start / ReStart rescue timer for Discovery / RSCN handling
1792 */
1793void
James Smart2e0fef82007-06-17 19:56:36 -05001794lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001795{
James Smart2e0fef82007-06-17 19:56:36 -05001796 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1797 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001798 uint32_t tmo;
1799
James Smart2e0fef82007-06-17 19:56:36 -05001800 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001801 /* For FAN, timeout should be greater then edtov */
1802 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1803 } else {
1804 /* Normal discovery timeout should be > then ELS/CT timeout
1805 * FC spec states we need 3 * ratov for CT requests
1806 */
1807 tmo = ((phba->fc_ratov * 3) + 3);
1808 }
dea31012005-04-17 16:05:31 -05001809
James Smart858c9f62007-06-17 19:56:39 -05001810
1811 if (!timer_pending(&vport->fc_disctmo)) {
1812 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1813 "set disc timer: tmo:x%x state:x%x flg:x%x",
1814 tmo, vport->port_state, vport->fc_flag);
1815 }
1816
James Smart2e0fef82007-06-17 19:56:36 -05001817 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1818 spin_lock_irq(shost->host_lock);
1819 vport->fc_flag |= FC_DISC_TMO;
1820 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001821
1822 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001823 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1824 "0247 Start Discovery Timer state x%x "
1825 "Data: x%x x%lx x%x x%x\n",
1826 vport->port_state, tmo,
1827 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1828 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05001829
1830 return;
1831}
1832
1833/*
1834 * Cancel rescue timer for Discovery / RSCN handling
1835 */
1836int
James Smart2e0fef82007-06-17 19:56:36 -05001837lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001838{
James Smart2e0fef82007-06-17 19:56:36 -05001839 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001840 unsigned long iflags;
1841
James Smart858c9f62007-06-17 19:56:39 -05001842 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1843 "can disc timer: state:x%x rtry:x%x flg:x%x",
1844 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
1845
dea31012005-04-17 16:05:31 -05001846 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05001847 if (vport->fc_flag & FC_DISC_TMO) {
1848 spin_lock_irqsave(shost->host_lock, iflags);
1849 vport->fc_flag &= ~FC_DISC_TMO;
1850 spin_unlock_irqrestore(shost->host_lock, iflags);
1851 del_timer_sync(&vport->fc_disctmo);
1852 spin_lock_irqsave(&vport->work_port_lock, iflags);
1853 vport->work_port_events &= ~WORKER_DISC_TMO;
1854 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05001855 }
1856
1857 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001858 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1859 "0248 Cancel Discovery Timer state x%x "
1860 "Data: x%x x%x x%x\n",
1861 vport->port_state, vport->fc_flag,
1862 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001863 return 0;
dea31012005-04-17 16:05:31 -05001864}
1865
1866/*
1867 * Check specified ring for outstanding IOCB on the SLI queue
1868 * Return true if iocb matches the specified nport
1869 */
1870int
James Smart2e0fef82007-06-17 19:56:36 -05001871lpfc_check_sli_ndlp(struct lpfc_hba *phba,
1872 struct lpfc_sli_ring *pring,
1873 struct lpfc_iocbq *iocb,
1874 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001875{
James Smart2e0fef82007-06-17 19:56:36 -05001876 struct lpfc_sli *psli = &phba->sli;
1877 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05001878 struct lpfc_vport *vport = ndlp->vport;
1879
1880 if (iocb->vport != vport)
1881 return 0;
1882
dea31012005-04-17 16:05:31 -05001883 if (pring->ringno == LPFC_ELS_RING) {
1884 switch (icmd->ulpCommand) {
1885 case CMD_GEN_REQUEST64_CR:
1886 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001887 return 1;
dea31012005-04-17 16:05:31 -05001888 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04001889 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1890 return 1;
dea31012005-04-17 16:05:31 -05001891 case CMD_XMIT_ELS_RSP64_CX:
1892 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001893 return 1;
dea31012005-04-17 16:05:31 -05001894 }
James Smarta4bc3372006-12-02 13:34:16 -05001895 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05001896
1897 } else if (pring->ringno == psli->fcp_ring) {
1898 /* Skip match check if waiting to relogin to FCP target */
1899 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001900 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001901 return 0;
dea31012005-04-17 16:05:31 -05001902 }
1903 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001904 return 1;
dea31012005-04-17 16:05:31 -05001905 }
1906 } else if (pring->ringno == psli->next_ring) {
1907
1908 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001909 return 0;
dea31012005-04-17 16:05:31 -05001910}
1911
1912/*
1913 * Free resources / clean up outstanding I/Os
1914 * associated with nlp_rpi in the LPFC_NODELIST entry.
1915 */
1916static int
James Smart2e0fef82007-06-17 19:56:36 -05001917lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001918{
James Smart2534ba72007-04-25 09:52:20 -04001919 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001920 struct lpfc_sli *psli;
1921 struct lpfc_sli_ring *pring;
1922 struct lpfc_iocbq *iocb, *next_iocb;
1923 IOCB_t *icmd;
1924 uint32_t rpi, i;
1925
James Smart92d7f7b2007-06-17 19:56:38 -05001926 lpfc_fabric_abort_nport(ndlp);
1927
dea31012005-04-17 16:05:31 -05001928 /*
1929 * Everything that matches on txcmplq will be returned
1930 * by firmware with a no rpi error.
1931 */
1932 psli = &phba->sli;
1933 rpi = ndlp->nlp_rpi;
1934 if (rpi) {
1935 /* Now process each ring */
1936 for (i = 0; i < psli->num_rings; i++) {
1937 pring = &psli->ring[i];
1938
James Smart2e0fef82007-06-17 19:56:36 -05001939 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001940 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05001941 list) {
dea31012005-04-17 16:05:31 -05001942 /*
1943 * Check to see if iocb matches the nport we are
1944 * looking for
1945 */
James Smart92d7f7b2007-06-17 19:56:38 -05001946 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
1947 ndlp))) {
dea31012005-04-17 16:05:31 -05001948 /* It matches, so deque and call compl
1949 with an error */
James Smart2534ba72007-04-25 09:52:20 -04001950 list_move_tail(&iocb->list,
1951 &completions);
dea31012005-04-17 16:05:31 -05001952 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05001953 }
1954 }
James Smart2e0fef82007-06-17 19:56:36 -05001955 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001956 }
1957 }
James Smart2534ba72007-04-25 09:52:20 -04001958
1959 while (!list_empty(&completions)) {
1960 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05001961 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04001962
James Smart2e0fef82007-06-17 19:56:36 -05001963 if (!iocb->iocb_cmpl)
1964 lpfc_sli_release_iocbq(phba, iocb);
1965 else {
James Smart2534ba72007-04-25 09:52:20 -04001966 icmd = &iocb->iocb;
1967 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1968 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
James Smart2e0fef82007-06-17 19:56:36 -05001969 (iocb->iocb_cmpl)(phba, iocb, iocb);
1970 }
James Smart2534ba72007-04-25 09:52:20 -04001971 }
1972
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001973 return 0;
dea31012005-04-17 16:05:31 -05001974}
1975
1976/*
1977 * Free rpi associated with LPFC_NODELIST entry.
1978 * This routine is called from lpfc_freenode(), when we are removing
1979 * a LPFC_NODELIST entry. It is also called if the driver initiates a
1980 * LOGO that completes successfully, and we are waiting to PLOGI back
1981 * to the remote NPort. In addition, it is called after we receive
1982 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1983 * we are waiting to PLOGI back to the remote NPort.
1984 */
1985int
James Smart2e0fef82007-06-17 19:56:36 -05001986lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001987{
James Smart2e0fef82007-06-17 19:56:36 -05001988 struct lpfc_hba *phba = vport->phba;
1989 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05001990 int rc;
1991
1992 if (ndlp->nlp_rpi) {
James Smart2e0fef82007-06-17 19:56:36 -05001993 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1994 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001995 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05001996 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001997 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04001998 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05001999 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05002000 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002001 }
dea31012005-04-17 16:05:31 -05002002 lpfc_no_rpi(phba, ndlp);
2003 ndlp->nlp_rpi = 0;
2004 return 1;
2005 }
2006 return 0;
2007}
2008
James Smart92d7f7b2007-06-17 19:56:38 -05002009void
2010lpfc_unreg_all_rpis(struct lpfc_vport *vport)
2011{
2012 struct lpfc_hba *phba = vport->phba;
2013 LPFC_MBOXQ_t *mbox;
2014 int rc;
2015
2016 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2017 if (mbox) {
2018 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
2019 mbox->vport = vport;
2020 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002021 mbox->context1 = NULL;
2022 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05002023 if (rc == MBX_NOT_FINISHED) {
2024 mempool_free(mbox, phba->mbox_mem_pool);
2025 }
2026 }
2027}
2028
2029void
2030lpfc_unreg_default_rpis(struct lpfc_vport *vport)
2031{
2032 struct lpfc_hba *phba = vport->phba;
2033 LPFC_MBOXQ_t *mbox;
2034 int rc;
2035
2036 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2037 if (mbox) {
2038 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
2039 mbox->vport = vport;
2040 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002041 mbox->context1 = NULL;
2042 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05002043 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002044 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2045 "1815 Could not issue "
2046 "unreg_did (default rpis)\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002047 mempool_free(mbox, phba->mbox_mem_pool);
2048 }
2049 }
2050}
2051
dea31012005-04-17 16:05:31 -05002052/*
2053 * Free resources associated with LPFC_NODELIST entry
2054 * so it can be freed.
2055 */
2056static int
James Smart2e0fef82007-06-17 19:56:36 -05002057lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002058{
James Smart2e0fef82007-06-17 19:56:36 -05002059 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2060 struct lpfc_hba *phba = vport->phba;
2061 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05002062 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05002063
2064 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04002065 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2066 "0900 Cleanup node for NPort x%x "
2067 "Data: x%x x%x x%x\n",
2068 ndlp->nlp_DID, ndlp->nlp_flag,
2069 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05002070 if (NLP_CHK_FREE_REQ(ndlp)) {
2071 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2072 "0280 lpfc_cleanup_node: ndlp:x%p "
2073 "usgmap:x%x refcnt:%d\n",
2074 (void *)ndlp, ndlp->nlp_usg_map,
2075 atomic_read(&ndlp->kref.refcount));
2076 lpfc_dequeue_node(vport, ndlp);
2077 } else {
2078 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2079 "0281 lpfc_cleanup_node: ndlp:x%p "
2080 "usgmap:x%x refcnt:%d\n",
2081 (void *)ndlp, ndlp->nlp_usg_map,
2082 atomic_read(&ndlp->kref.refcount));
2083 lpfc_disable_node(vport, ndlp);
2084 }
dea31012005-04-17 16:05:31 -05002085
dea31012005-04-17 16:05:31 -05002086 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
2087 if ((mb = phba->sli.mbox_active)) {
2088 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
2089 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
2090 mb->context2 = NULL;
2091 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2092 }
2093 }
James Smart33ccf8d2006-08-17 11:57:58 -04002094
James Smart2e0fef82007-06-17 19:56:36 -05002095 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002096 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
2097 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002098 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05002099 mp = (struct lpfc_dmabuf *) (mb->context1);
2100 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05002101 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002102 kfree(mp);
2103 }
2104 list_del(&mb->list);
2105 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002106 /* We shall not invoke the lpfc_nlp_put to decrement
2107 * the ndlp reference count as we are in the process
2108 * of lpfc_nlp_release.
2109 */
dea31012005-04-17 16:05:31 -05002110 }
2111 }
James Smart2e0fef82007-06-17 19:56:36 -05002112 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002113
James Smarte47c9092008-02-08 18:49:26 -05002114 lpfc_els_abort(phba, ndlp);
2115
James Smart2e0fef82007-06-17 19:56:36 -05002116 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04002117 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05002118 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002119
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05002120 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05002121 del_timer_sync(&ndlp->nlp_delayfunc);
2122
James Smart0d2b6b82008-06-14 22:52:47 -04002123 list_del_init(&ndlp->els_retry_evt.evt_listp);
2124 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002125
James Smart2e0fef82007-06-17 19:56:36 -05002126 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05002127
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002128 return 0;
dea31012005-04-17 16:05:31 -05002129}
2130
2131/*
2132 * Check to see if we can free the nlp back to the freelist.
2133 * If we are in the middle of using the nlp in the discovery state
2134 * machine, defer the free till we reach the end of the state machine.
2135 */
James Smart329f9bc2007-04-25 09:53:01 -04002136static void
James Smart2e0fef82007-06-17 19:56:36 -05002137lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002138{
James Smarta8adb832007-10-27 13:37:53 -04002139 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04002140 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04002141 LPFC_MBOXQ_t *mbox;
2142 int rc;
dea31012005-04-17 16:05:31 -05002143
James Smart0d2b6b82008-06-14 22:52:47 -04002144 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04002145 if (ndlp->nlp_flag & NLP_DEFER_RM && !ndlp->nlp_rpi) {
2146 /* For this case we need to cleanup the default rpi
2147 * allocated by the firmware.
2148 */
2149 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
2150 != NULL) {
2151 rc = lpfc_reg_login(phba, vport->vpi, ndlp->nlp_DID,
2152 (uint8_t *) &vport->fc_sparam, mbox, 0);
2153 if (rc) {
2154 mempool_free(mbox, phba->mbox_mem_pool);
2155 }
2156 else {
2157 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2158 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2159 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05002160 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04002161 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2162 if (rc == MBX_NOT_FINISHED) {
2163 mempool_free(mbox, phba->mbox_mem_pool);
2164 }
2165 }
2166 }
2167 }
James Smart2e0fef82007-06-17 19:56:36 -05002168 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04002169
James Smart2e0fef82007-06-17 19:56:36 -05002170 /*
James Smart92d7f7b2007-06-17 19:56:38 -05002171 * We can get here with a non-NULL ndlp->rport because when we
2172 * unregister a rport we don't break the rport/node linkage. So if we
2173 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05002174 */
James Smart92d7f7b2007-06-17 19:56:38 -05002175 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04002176 rdata = ndlp->rport->dd_data;
2177 rdata->pnode = NULL;
2178 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05002179 }
dea31012005-04-17 16:05:31 -05002180}
2181
2182static int
James Smart2e0fef82007-06-17 19:56:36 -05002183lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2184 uint32_t did)
dea31012005-04-17 16:05:31 -05002185{
James Smart2e0fef82007-06-17 19:56:36 -05002186 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05002187
2188 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002189 return 0;
dea31012005-04-17 16:05:31 -05002190
dea31012005-04-17 16:05:31 -05002191 /* First check for Direct match */
2192 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002193 return 1;
dea31012005-04-17 16:05:31 -05002194
2195 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05002196 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05002197 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002198 return 0;
dea31012005-04-17 16:05:31 -05002199 }
2200
2201 matchdid.un.word = did;
2202 ndlpdid.un.word = ndlp->nlp_DID;
2203 if (matchdid.un.b.id == ndlpdid.un.b.id) {
2204 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
2205 (mydid.un.b.area == matchdid.un.b.area)) {
2206 if ((ndlpdid.un.b.domain == 0) &&
2207 (ndlpdid.un.b.area == 0)) {
2208 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002209 return 1;
dea31012005-04-17 16:05:31 -05002210 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002211 return 0;
dea31012005-04-17 16:05:31 -05002212 }
2213
2214 matchdid.un.word = ndlp->nlp_DID;
2215 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
2216 (mydid.un.b.area == ndlpdid.un.b.area)) {
2217 if ((matchdid.un.b.domain == 0) &&
2218 (matchdid.un.b.area == 0)) {
2219 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002220 return 1;
dea31012005-04-17 16:05:31 -05002221 }
2222 }
2223 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002224 return 0;
dea31012005-04-17 16:05:31 -05002225}
2226
James Smart685f0bf2007-04-25 09:53:08 -04002227/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05002228static struct lpfc_nodelist *
2229__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002230{
James Smart2fb9bd82006-12-02 13:33:57 -05002231 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002232 uint32_t data1;
2233
James Smart2e0fef82007-06-17 19:56:36 -05002234 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2235 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04002236 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2237 ((uint32_t) ndlp->nlp_xri << 16) |
2238 ((uint32_t) ndlp->nlp_type << 8) |
2239 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04002240 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2241 "0929 FIND node DID "
2242 "Data: x%p x%x x%x x%x\n",
2243 ndlp, ndlp->nlp_DID,
2244 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04002245 return ndlp;
dea31012005-04-17 16:05:31 -05002246 }
2247 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002248
dea31012005-04-17 16:05:31 -05002249 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04002250 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2251 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05002252 return NULL;
2253}
2254
2255struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002256lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002257{
James Smart2e0fef82007-06-17 19:56:36 -05002258 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002259 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002260
James Smart2e0fef82007-06-17 19:56:36 -05002261 spin_lock_irq(shost->host_lock);
2262 ndlp = __lpfc_findnode_did(vport, did);
2263 spin_unlock_irq(shost->host_lock);
2264 return ndlp;
2265}
2266
2267struct lpfc_nodelist *
2268lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2269{
2270 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2271 struct lpfc_nodelist *ndlp;
2272
2273 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002274 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05002275 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2276 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05002277 return NULL;
2278 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05002279 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002280 if (!ndlp)
2281 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002282 lpfc_nlp_init(vport, ndlp, did);
2283 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2284 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002285 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002286 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002287 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05002288 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2289 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
2290 if (!ndlp)
2291 return NULL;
2292 spin_lock_irq(shost->host_lock);
2293 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2294 spin_unlock_irq(shost->host_lock);
2295 return ndlp;
dea31012005-04-17 16:05:31 -05002296 }
James Smarte47c9092008-02-08 18:49:26 -05002297
James Smart58da1ff2008-04-07 10:15:56 -04002298 if ((vport->fc_flag & FC_RSCN_MODE) &&
2299 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002300 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04002301 /* If we've already recieved a PLOGI from this NPort
2302 * we don't need to try to discover it again.
2303 */
2304 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
2305 return NULL;
2306
James Smart2e0fef82007-06-17 19:56:36 -05002307 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002308 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002309 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002310
2311 /* Since this node is marked for discovery,
2312 * delay timeout is not needed.
2313 */
James Smart0d2b6b82008-06-14 22:52:47 -04002314 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart071fbd3d2006-04-15 11:53:20 -04002315 } else
dea31012005-04-17 16:05:31 -05002316 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002317 } else {
James Smart87af33f2007-10-27 13:37:43 -04002318 /* If we've already recieved a PLOGI from this NPort,
2319 * or we are already in the process of discovery on it,
2320 * we don't need to try to discover it again.
2321 */
James Smart685f0bf2007-04-25 09:53:08 -04002322 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04002323 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2324 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05002325 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002326 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2327 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002328 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002329 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002330 }
2331 return ndlp;
2332}
2333
2334/* Build a list of nodes to discover based on the loopmap */
2335void
James Smart2e0fef82007-06-17 19:56:36 -05002336lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002337{
James Smart2e0fef82007-06-17 19:56:36 -05002338 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002339 int j;
2340 uint32_t alpa, index;
2341
James Smart2e0fef82007-06-17 19:56:36 -05002342 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002343 return;
James Smart2e0fef82007-06-17 19:56:36 -05002344
2345 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05002346 return;
dea31012005-04-17 16:05:31 -05002347
2348 /* Check for loop map present or not */
2349 if (phba->alpa_map[0]) {
2350 for (j = 1; j <= phba->alpa_map[0]; j++) {
2351 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05002352 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05002353 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002354 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002355 }
2356 } else {
2357 /* No alpamap, so try all alpa's */
2358 for (j = 0; j < FC_MAXLOOP; j++) {
2359 /* If cfg_scan_down is set, start from highest
2360 * ALPA (0xef) to lowest (0x1).
2361 */
James Smart3de2a652007-08-02 11:09:59 -04002362 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05002363 index = j;
2364 else
2365 index = FC_MAXLOOP - j - 1;
2366 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05002367 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05002368 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002369 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002370 }
2371 }
2372 return;
2373}
2374
dea31012005-04-17 16:05:31 -05002375void
James Smart2e0fef82007-06-17 19:56:36 -05002376lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002377{
dea31012005-04-17 16:05:31 -05002378 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05002379 struct lpfc_sli *psli = &phba->sli;
2380 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2381 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2382 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2383 int rc;
2384
James Smart92d7f7b2007-06-17 19:56:38 -05002385 /*
2386 * if it's not a physical port or if we already send
2387 * clear_la then don't send it.
2388 */
2389 if ((phba->link_state >= LPFC_CLEAR_LA) ||
2390 (vport->port_type != LPFC_PHYSICAL_PORT))
2391 return;
2392
James Smart2e0fef82007-06-17 19:56:36 -05002393 /* Link up discovery */
2394 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2395 phba->link_state = LPFC_CLEAR_LA;
2396 lpfc_clear_la(phba, mbox);
2397 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2398 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002399 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05002400 if (rc == MBX_NOT_FINISHED) {
2401 mempool_free(mbox, phba->mbox_mem_pool);
2402 lpfc_disc_flush_list(vport);
2403 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2404 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2405 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05002406 phba->link_state = LPFC_HBA_ERROR;
2407 }
2408 }
2409}
2410
2411/* Reg_vpi to tell firmware to resume normal operations */
2412void
2413lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2414{
2415 LPFC_MBOXQ_t *regvpimbox;
2416
2417 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2418 if (regvpimbox) {
2419 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
2420 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2421 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002422 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05002423 == MBX_NOT_FINISHED) {
2424 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002425 }
2426 }
2427}
2428
2429/* Start Link up / RSCN discovery on NPR nodes */
2430void
2431lpfc_disc_start(struct lpfc_vport *vport)
2432{
2433 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2434 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04002435 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05002436 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04002437 int did_changed;
dea31012005-04-17 16:05:31 -05002438
James Smart2e0fef82007-06-17 19:56:36 -05002439 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002440 return;
James Smart2e0fef82007-06-17 19:56:36 -05002441
2442 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05002443 clear_la_pending = 1;
2444 else
2445 clear_la_pending = 0;
2446
James Smart2e0fef82007-06-17 19:56:36 -05002447 if (vport->port_state < LPFC_VPORT_READY)
2448 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05002449
James Smart2e0fef82007-06-17 19:56:36 -05002450 lpfc_set_disctmo(vport);
2451
2452 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05002453 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002454 else
dea31012005-04-17 16:05:31 -05002455 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002456
2457 vport->fc_prevDID = vport->fc_myDID;
2458 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05002459
2460 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002461 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2462 "0202 Start Discovery hba state x%x "
2463 "Data: x%x x%x x%x\n",
2464 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
2465 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002466
2467 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002468 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05002469
2470 if (num_sent)
2471 return;
2472
James Smart92d7f7b2007-06-17 19:56:38 -05002473 /*
2474 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2475 * continue discovery.
2476 */
2477 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05002478 !(vport->fc_flag & FC_PT2PT) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002479 !(vport->fc_flag & FC_RSCN_MODE)) {
2480 lpfc_issue_reg_vpi(phba, vport);
2481 return;
2482 }
James Smart2e0fef82007-06-17 19:56:36 -05002483
James Smart92d7f7b2007-06-17 19:56:38 -05002484 /*
2485 * For SLI2, we need to set port_state to READY and continue
2486 * discovery.
2487 */
2488 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2489 /* If we get here, there is nothing to ADISC */
2490 if (vport->port_type == LPFC_PHYSICAL_PORT)
2491 lpfc_issue_clear_la(phba, vport);
2492
2493 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05002494 vport->num_disc_nodes = 0;
2495 /* go thru NPR nodes and issue ELS PLOGIs */
2496 if (vport->fc_npr_cnt)
2497 lpfc_els_disc_plogi(vport);
2498
2499 if (!vport->num_disc_nodes) {
2500 spin_lock_irq(shost->host_lock);
2501 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2502 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002503 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05002504 }
2505 }
James Smart92d7f7b2007-06-17 19:56:38 -05002506 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002507 } else {
2508 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002509 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05002510
2511 if (num_sent)
2512 return;
2513
James Smart2e0fef82007-06-17 19:56:36 -05002514 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05002515 /* Check to see if more RSCNs came in while we
2516 * were processing this one.
2517 */
James Smart2e0fef82007-06-17 19:56:36 -05002518 if ((vport->fc_rscn_id_cnt == 0) &&
2519 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2520 spin_lock_irq(shost->host_lock);
2521 vport->fc_flag &= ~FC_RSCN_MODE;
2522 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002523 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002524 } else
James Smart2e0fef82007-06-17 19:56:36 -05002525 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05002526 }
2527 }
2528 return;
2529}
2530
2531/*
2532 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2533 * ring the match the sppecified nodelist.
2534 */
2535static void
James Smart2e0fef82007-06-17 19:56:36 -05002536lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002537{
James Smart2534ba72007-04-25 09:52:20 -04002538 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002539 struct lpfc_sli *psli;
2540 IOCB_t *icmd;
2541 struct lpfc_iocbq *iocb, *next_iocb;
2542 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002543
2544 psli = &phba->sli;
2545 pring = &psli->ring[LPFC_ELS_RING];
2546
2547 /* Error matching iocb on txq or txcmplq
2548 * First check the txq.
2549 */
James Smart2e0fef82007-06-17 19:56:36 -05002550 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002551 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2552 if (iocb->context1 != ndlp) {
2553 continue;
2554 }
2555 icmd = &iocb->iocb;
2556 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2557 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2558
James Smart2534ba72007-04-25 09:52:20 -04002559 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05002560 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002561 }
2562 }
2563
2564 /* Next check the txcmplq */
2565 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2566 if (iocb->context1 != ndlp) {
2567 continue;
2568 }
2569 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05002570 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2571 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04002572 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05002573 }
2574 }
James Smart2e0fef82007-06-17 19:56:36 -05002575 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002576
2577 while (!list_empty(&completions)) {
2578 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05002579 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04002580
James Smart2e0fef82007-06-17 19:56:36 -05002581 if (!iocb->iocb_cmpl)
2582 lpfc_sli_release_iocbq(phba, iocb);
2583 else {
James Smart2534ba72007-04-25 09:52:20 -04002584 icmd = &iocb->iocb;
2585 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2586 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2587 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002588 }
James Smart2534ba72007-04-25 09:52:20 -04002589 }
dea31012005-04-17 16:05:31 -05002590}
2591
Adrian Bunka6ababd2007-11-05 18:07:33 +01002592static void
James Smart2e0fef82007-06-17 19:56:36 -05002593lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002594{
2595 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05002596 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002597
James Smart2e0fef82007-06-17 19:56:36 -05002598 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2599 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002600 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002601 if (!NLP_CHK_NODE_ACT(ndlp))
2602 continue;
James Smart685f0bf2007-04-25 09:53:08 -04002603 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2604 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2605 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04002606 }
dea31012005-04-17 16:05:31 -05002607 }
2608 }
dea31012005-04-17 16:05:31 -05002609}
2610
James Smart92d7f7b2007-06-17 19:56:38 -05002611void
2612lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2613{
2614 lpfc_els_flush_rscn(vport);
2615 lpfc_els_flush_cmd(vport);
2616 lpfc_disc_flush_list(vport);
2617}
2618
dea31012005-04-17 16:05:31 -05002619/*****************************************************************************/
2620/*
2621 * NAME: lpfc_disc_timeout
2622 *
2623 * FUNCTION: Fibre Channel driver discovery timeout routine.
2624 *
2625 * EXECUTION ENVIRONMENT: interrupt only
2626 *
2627 * CALLED FROM:
2628 * Timer function
2629 *
2630 * RETURNS:
2631 * none
2632 */
2633/*****************************************************************************/
2634void
2635lpfc_disc_timeout(unsigned long ptr)
2636{
James Smart2e0fef82007-06-17 19:56:36 -05002637 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2638 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002639 unsigned long flags = 0;
2640
2641 if (unlikely(!phba))
2642 return;
2643
James Smart2e0fef82007-06-17 19:56:36 -05002644 if ((vport->work_port_events & WORKER_DISC_TMO) == 0) {
2645 spin_lock_irqsave(&vport->work_port_lock, flags);
2646 vport->work_port_events |= WORKER_DISC_TMO;
2647 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2648
James Smart92d7f7b2007-06-17 19:56:38 -05002649 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002650 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05002651 lpfc_worker_wake_up(phba);
2652 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002653 }
dea31012005-04-17 16:05:31 -05002654 return;
2655}
2656
2657static void
James Smart2e0fef82007-06-17 19:56:36 -05002658lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002659{
James Smart2e0fef82007-06-17 19:56:36 -05002660 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2661 struct lpfc_hba *phba = vport->phba;
2662 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002663 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05002664 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05002665 int rc, clrlaerr = 0;
2666
James Smart2e0fef82007-06-17 19:56:36 -05002667 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05002668 return;
2669
James Smart2e0fef82007-06-17 19:56:36 -05002670 spin_lock_irq(shost->host_lock);
2671 vport->fc_flag &= ~FC_DISC_TMO;
2672 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002673
James Smart858c9f62007-06-17 19:56:39 -05002674 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2675 "disc timeout: state:x%x rtry:x%x flg:x%x",
2676 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2677
James Smart2e0fef82007-06-17 19:56:36 -05002678 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05002679
2680 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05002681 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2682 * FAN
2683 */
2684 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04002685 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
2686 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002687 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05002688 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002689 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002690 if (!NLP_CHK_NODE_ACT(ndlp))
2691 continue;
James Smart685f0bf2007-04-25 09:53:08 -04002692 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2693 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002694 if (ndlp->nlp_type & NLP_FABRIC) {
2695 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05002696 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04002697
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002698 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002699 /* Fail outstanding IO now since device
2700 * is marked for PLOGI.
2701 */
James Smart2e0fef82007-06-17 19:56:36 -05002702 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002703 }
2704 }
James Smart92d7f7b2007-06-17 19:56:38 -05002705 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05002706 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05002707 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002708 }
dea31012005-04-17 16:05:31 -05002709 break;
2710
James Smart92d7f7b2007-06-17 19:56:38 -05002711 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05002712 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05002713 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05002714 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04002715 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2716 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04002717 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05002718
2719 /* Assume no Fabric and go on with discovery.
2720 * Check for outstanding ELS FLOGI to abort.
2721 */
2722
2723 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05002724 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002725
2726 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002727 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002728 break;
2729
2730 case LPFC_FABRIC_CFG_LINK:
2731 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2732 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04002733 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2734 "0223 Timeout while waiting for "
2735 "NameServer login\n");
dea31012005-04-17 16:05:31 -05002736 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05002737 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05002738 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04002739 lpfc_els_abort(phba, ndlp);
2740
2741 /* ReStart discovery */
2742 goto restart_disc;
dea31012005-04-17 16:05:31 -05002743
2744 case LPFC_NS_QRY:
2745 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04002746 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2747 "0224 NameServer Query timeout "
2748 "Data: x%x x%x\n",
2749 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002750
James Smart92d7f7b2007-06-17 19:56:38 -05002751 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2752 /* Try it one more time */
2753 vport->fc_ns_retry++;
2754 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2755 vport->fc_ns_retry, 0);
2756 if (rc == 0)
2757 break;
dea31012005-04-17 16:05:31 -05002758 }
James Smart92d7f7b2007-06-17 19:56:38 -05002759 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002760
James Smart87af33f2007-10-27 13:37:43 -04002761restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05002762 /*
2763 * Discovery is over.
2764 * set port_state to PORT_READY if SLI2.
2765 * cmpl_reg_vpi will set port_state to READY for SLI3.
2766 */
2767 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2768 lpfc_issue_reg_vpi(phba, vport);
2769 else { /* NPIV Not enabled */
2770 lpfc_issue_clear_la(phba, vport);
2771 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002772 }
2773
2774 /* Setup and issue mailbox INITIALIZE LINK command */
2775 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2776 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04002777 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2778 "0206 Device Discovery "
2779 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05002780 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002781 break;
2782 }
2783
2784 lpfc_linkdown(phba);
2785 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2786 phba->cfg_link_speed);
2787 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05002788 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002789 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002790 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04002791 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05002792 if (rc == MBX_NOT_FINISHED)
2793 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2794
2795 break;
2796
2797 case LPFC_DISC_AUTH:
2798 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04002799 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2800 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002801 lpfc_disc_flush_list(vport);
2802
James Smart92d7f7b2007-06-17 19:56:38 -05002803 /*
2804 * set port_state to PORT_READY if SLI2.
2805 * cmpl_reg_vpi will set port_state to READY for SLI3.
2806 */
2807 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2808 lpfc_issue_reg_vpi(phba, vport);
2809 else { /* NPIV Not enabled */
2810 lpfc_issue_clear_la(phba, vport);
2811 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002812 }
2813 break;
2814
James Smart2e0fef82007-06-17 19:56:36 -05002815 case LPFC_VPORT_READY:
2816 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04002817 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2818 "0231 RSCN timeout Data: x%x "
2819 "x%x\n",
2820 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002821
2822 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05002823 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05002824
James Smart2e0fef82007-06-17 19:56:36 -05002825 lpfc_els_flush_rscn(vport);
2826 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05002827 }
2828 break;
James Smart2e0fef82007-06-17 19:56:36 -05002829
James Smart92d7f7b2007-06-17 19:56:38 -05002830 default:
James Smarte8b62012007-08-02 11:10:09 -04002831 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2832 "0229 Unexpected discovery timeout, "
2833 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05002834 break;
2835 }
2836
2837 switch (phba->link_state) {
2838 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05002839 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04002840 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2841 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002842 clrlaerr = 1;
2843 break;
2844
James Smart09372822008-01-11 01:52:54 -05002845 case LPFC_LINK_UP:
2846 lpfc_issue_clear_la(phba, vport);
2847 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05002848 case LPFC_LINK_UNKNOWN:
2849 case LPFC_WARM_START:
2850 case LPFC_INIT_START:
2851 case LPFC_INIT_MBX_CMDS:
2852 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05002853 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04002854 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2855 "0230 Unexpected timeout, hba link "
2856 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05002857 clrlaerr = 1;
2858 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002859
2860 case LPFC_HBA_READY:
2861 break;
dea31012005-04-17 16:05:31 -05002862 }
2863
2864 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05002865 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05002866 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002867 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2868 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05002869 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002870 }
2871
2872 return;
2873}
2874
dea31012005-04-17 16:05:31 -05002875/*
2876 * This routine handles processing a NameServer REG_LOGIN mailbox
2877 * command upon completion. It is setup in the LPFC_MBOXQ
2878 * as the completion routine when the command is
2879 * handed off to the SLI layer.
2880 */
2881void
James Smart2e0fef82007-06-17 19:56:36 -05002882lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002883{
James Smart2e0fef82007-06-17 19:56:36 -05002884 MAILBOX_t *mb = &pmb->mb;
2885 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2886 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2887 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002888
2889 pmb->context1 = NULL;
2890
dea31012005-04-17 16:05:31 -05002891 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05002892 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002893 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002894
James Smart2e0fef82007-06-17 19:56:36 -05002895 /*
2896 * Start issuing Fabric-Device Management Interface (FDMI) command to
2897 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
2898 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05002899 */
James Smart2e0fef82007-06-17 19:56:36 -05002900
James Smart3de2a652007-08-02 11:09:59 -04002901 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05002902 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
2903 else
2904 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05002905
James Smartfa4066b2008-01-11 01:53:27 -05002906 /* decrement the node reference count held for this callback
2907 * function.
2908 */
James Smart329f9bc2007-04-25 09:53:01 -04002909 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002910 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2911 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002912 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002913
2914 return;
2915}
2916
James Smart685f0bf2007-04-25 09:53:08 -04002917static int
2918lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2919{
2920 uint16_t *rpi = param;
2921
2922 return ndlp->nlp_rpi == *rpi;
2923}
2924
2925static int
2926lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2927{
2928 return memcmp(&ndlp->nlp_portname, param,
2929 sizeof(ndlp->nlp_portname)) == 0;
2930}
2931
Adrian Bunka6ababd2007-11-05 18:07:33 +01002932static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002933__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05002934{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002935 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002936
James Smart2e0fef82007-06-17 19:56:36 -05002937 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04002938 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04002939 return ndlp;
2940 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002941 return NULL;
dea31012005-04-17 16:05:31 -05002942}
2943
James Smart685f0bf2007-04-25 09:53:08 -04002944/*
2945 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05002946 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04002947 */
2948struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002949__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04002950{
James Smart2e0fef82007-06-17 19:56:36 -05002951 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04002952}
2953
James Smart488d1462006-03-07 15:02:37 -05002954/*
James Smart685f0bf2007-04-25 09:53:08 -04002955 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05002956 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05002957 */
2958struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002959lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05002960{
James Smart2e0fef82007-06-17 19:56:36 -05002961 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05002962 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05002963
James Smart2e0fef82007-06-17 19:56:36 -05002964 spin_lock_irq(shost->host_lock);
2965 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
2966 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002967 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05002968}
2969
dea31012005-04-17 16:05:31 -05002970void
James Smart2e0fef82007-06-17 19:56:36 -05002971lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2972 uint32_t did)
dea31012005-04-17 16:05:31 -05002973{
2974 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -05002975 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05002976 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002977 init_timer(&ndlp->nlp_delayfunc);
2978 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2979 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2980 ndlp->nlp_DID = did;
James Smart2e0fef82007-06-17 19:56:36 -05002981 ndlp->vport = vport;
dea31012005-04-17 16:05:31 -05002982 ndlp->nlp_sid = NLP_NO_SID;
James Smart685f0bf2007-04-25 09:53:08 -04002983 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart329f9bc2007-04-25 09:53:01 -04002984 kref_init(&ndlp->kref);
James Smarte47c9092008-02-08 18:49:26 -05002985 NLP_INT_NODE_ACT(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05002986
2987 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2988 "node init: did:x%x",
2989 ndlp->nlp_DID, 0, 0);
2990
dea31012005-04-17 16:05:31 -05002991 return;
2992}
James Smart329f9bc2007-04-25 09:53:01 -04002993
James Smart98c9ea52007-10-27 13:37:33 -04002994/* This routine releases all resources associated with a specifc NPort's ndlp
2995 * and mempool_free's the nodelist.
2996 */
James Smart311464e2007-08-02 11:10:37 -04002997static void
James Smart329f9bc2007-04-25 09:53:01 -04002998lpfc_nlp_release(struct kref *kref)
2999{
James Smarte47c9092008-02-08 18:49:26 -05003000 struct lpfc_hba *phba;
3001 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04003002 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
3003 kref);
James Smart858c9f62007-06-17 19:56:39 -05003004
3005 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3006 "node release: did:x%x flg:x%x type:x%x",
3007 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3008
James Smarte47c9092008-02-08 18:49:26 -05003009 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3010 "0279 lpfc_nlp_release: ndlp:x%p "
3011 "usgmap:x%x refcnt:%d\n",
3012 (void *)ndlp, ndlp->nlp_usg_map,
3013 atomic_read(&ndlp->kref.refcount));
3014
3015 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05003016 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003017
3018 /* clear the ndlp active flag for all release cases */
3019 phba = ndlp->vport->phba;
3020 spin_lock_irqsave(&phba->ndlp_lock, flags);
3021 NLP_CLR_NODE_ACT(ndlp);
3022 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3023
3024 /* free ndlp memory for final ndlp release */
3025 if (NLP_CHK_FREE_REQ(ndlp))
3026 mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04003027}
3028
James Smart98c9ea52007-10-27 13:37:33 -04003029/* This routine bumps the reference count for a ndlp structure to ensure
3030 * that one discovery thread won't free a ndlp while another discovery thread
3031 * is using it.
3032 */
James Smart329f9bc2007-04-25 09:53:01 -04003033struct lpfc_nodelist *
3034lpfc_nlp_get(struct lpfc_nodelist *ndlp)
3035{
James Smarte47c9092008-02-08 18:49:26 -05003036 struct lpfc_hba *phba;
3037 unsigned long flags;
3038
James Smart98c9ea52007-10-27 13:37:33 -04003039 if (ndlp) {
3040 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3041 "node get: did:x%x flg:x%x refcnt:x%x",
3042 ndlp->nlp_DID, ndlp->nlp_flag,
3043 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003044 /* The check of ndlp usage to prevent incrementing the
3045 * ndlp reference count that is in the process of being
3046 * released.
3047 */
3048 phba = ndlp->vport->phba;
3049 spin_lock_irqsave(&phba->ndlp_lock, flags);
3050 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
3051 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3052 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3053 "0276 lpfc_nlp_get: ndlp:x%p "
3054 "usgmap:x%x refcnt:%d\n",
3055 (void *)ndlp, ndlp->nlp_usg_map,
3056 atomic_read(&ndlp->kref.refcount));
3057 return NULL;
3058 } else
3059 kref_get(&ndlp->kref);
3060 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04003061 }
James Smart329f9bc2007-04-25 09:53:01 -04003062 return ndlp;
3063}
3064
James Smart98c9ea52007-10-27 13:37:33 -04003065/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05003066 * count goes to 0, this indicates the the associated nodelist should be
3067 * freed. Returning 1 indicates the ndlp resource has been released; on the
3068 * other hand, returning 0 indicates the ndlp resource has not been released
3069 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04003070 */
James Smart329f9bc2007-04-25 09:53:01 -04003071int
3072lpfc_nlp_put(struct lpfc_nodelist *ndlp)
3073{
James Smarte47c9092008-02-08 18:49:26 -05003074 struct lpfc_hba *phba;
3075 unsigned long flags;
3076
3077 if (!ndlp)
3078 return 1;
3079
3080 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3081 "node put: did:x%x flg:x%x refcnt:x%x",
3082 ndlp->nlp_DID, ndlp->nlp_flag,
3083 atomic_read(&ndlp->kref.refcount));
3084 phba = ndlp->vport->phba;
3085 spin_lock_irqsave(&phba->ndlp_lock, flags);
3086 /* Check the ndlp memory free acknowledge flag to avoid the
3087 * possible race condition that kref_put got invoked again
3088 * after previous one has done ndlp memory free.
3089 */
3090 if (NLP_CHK_FREE_ACK(ndlp)) {
3091 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3092 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3093 "0274 lpfc_nlp_put: ndlp:x%p "
3094 "usgmap:x%x refcnt:%d\n",
3095 (void *)ndlp, ndlp->nlp_usg_map,
3096 atomic_read(&ndlp->kref.refcount));
3097 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003098 }
James Smarte47c9092008-02-08 18:49:26 -05003099 /* Check the ndlp inactivate log flag to avoid the possible
3100 * race condition that kref_put got invoked again after ndlp
3101 * is already in inactivating state.
3102 */
3103 if (NLP_CHK_IACT_REQ(ndlp)) {
3104 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3105 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3106 "0275 lpfc_nlp_put: ndlp:x%p "
3107 "usgmap:x%x refcnt:%d\n",
3108 (void *)ndlp, ndlp->nlp_usg_map,
3109 atomic_read(&ndlp->kref.refcount));
3110 return 1;
3111 }
3112 /* For last put, mark the ndlp usage flags to make sure no
3113 * other kref_get and kref_put on the same ndlp shall get
3114 * in between the process when the final kref_put has been
3115 * invoked on this ndlp.
3116 */
3117 if (atomic_read(&ndlp->kref.refcount) == 1) {
3118 /* Indicate ndlp is put to inactive state. */
3119 NLP_SET_IACT_REQ(ndlp);
3120 /* Acknowledge ndlp memory free has been seen. */
3121 if (NLP_CHK_FREE_REQ(ndlp))
3122 NLP_SET_FREE_ACK(ndlp);
3123 }
3124 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3125 /* Note, the kref_put returns 1 when decrementing a reference
3126 * count that was 1, it invokes the release callback function,
3127 * but it still left the reference count as 1 (not actually
3128 * performs the last decrementation). Otherwise, it actually
3129 * decrements the reference count and returns 0.
3130 */
3131 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04003132}
James Smart98c9ea52007-10-27 13:37:33 -04003133
3134/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05003135 * by any other discovery thread. This routine returns 1 if the
3136 * ndlp has been freed. A return value of 0 indicates the ndlp is
3137 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04003138 */
3139int
3140lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
3141{
3142 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3143 "node not used: did:x%x flg:x%x refcnt:x%x",
3144 ndlp->nlp_DID, ndlp->nlp_flag,
3145 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003146 if (atomic_read(&ndlp->kref.refcount) == 1)
3147 if (lpfc_nlp_put(ndlp))
3148 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003149 return 0;
3150}