blob: 25892671bfb050c4469dc79c4de27fd61db7c01d [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
72 if (!ndlp) {
73 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 Smart09372822008-01-11 01:52:54 -0500117 if (!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 "
246 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
247 "NPort x%x Data: x%x x%x x%x\n",
248 *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 "
255 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
256 "NPort x%x Data: x%x x%x x%x\n",
257 *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;
402 work_port_events = vport->work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400403 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400404 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400405 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400406 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400407 if (work_port_events & WORKER_HB_TMO)
408 lpfc_hb_timeout_handler(phba);
409 if (work_port_events & WORKER_MBOX_TMO)
410 lpfc_mbox_timeout_handler(phba);
411 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
412 lpfc_unblock_fabric_iocbs(phba);
413 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400414 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400415 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
416 lpfc_ramp_down_queue_handler(phba);
417 if (work_port_events & WORKER_RAMP_UP_QUEUE)
418 lpfc_ramp_up_queue_handler(phba);
James Smart51ef4c22007-08-02 11:10:31 -0400419 spin_lock_irq(&vport->work_port_lock);
420 vport->work_port_events &= ~work_port_events;
421 spin_unlock_irq(&vport->work_port_lock);
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 {
433 lpfc_sli_handle_slow_ring_event(phba, pring,
434 (status &
435 HA_RXMASK));
436 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
437 }
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 }
522 phba->work_wait = NULL;
523 return 0;
524}
525
526/*
527 * This is only called to handle FC worker events. Since this a rare
528 * occurance, we allocate a struct lpfc_work_evt structure here instead of
529 * embedding it in the IOCB.
530 */
531int
James Smart2e0fef82007-06-17 19:56:36 -0500532lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500533 uint32_t evt)
534{
535 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500536 unsigned long flags;
dea31012005-04-17 16:05:31 -0500537
538 /*
539 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
540 * be queued to worker thread for processing
541 */
James Smart92d7f7b2007-06-17 19:56:38 -0500542 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500543 if (!evtp)
544 return 0;
545
546 evtp->evt_arg1 = arg1;
547 evtp->evt_arg2 = arg2;
548 evtp->evt = evt;
549
James Smarted957682007-06-17 19:56:37 -0500550 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400551 list_add_tail(&evtp->evt_listp, &phba->work_list);
dea31012005-04-17 16:05:31 -0500552 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -0500553 lpfc_worker_wake_up(phba);
James Smarted957682007-06-17 19:56:37 -0500554 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500555
556 return 1;
557}
558
James Smart92d7f7b2007-06-17 19:56:38 -0500559void
560lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
561{
James Smart09372822008-01-11 01:52:54 -0500562 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500563 struct lpfc_hba *phba = vport->phba;
564 struct lpfc_nodelist *ndlp, *next_ndlp;
565 int rc;
566
567 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500568 if (!NLP_CHK_NODE_ACT(ndlp))
569 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500570 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
571 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400572 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
573 ((vport->port_type == LPFC_NPIV_PORT) &&
574 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500575 lpfc_unreg_rpi(vport, ndlp);
576
577 /* Leave Fabric nodes alone on link down */
578 if (!remove && ndlp->nlp_type & NLP_FABRIC)
579 continue;
580 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
581 remove
582 ? NLP_EVT_DEVICE_RM
583 : NLP_EVT_DEVICE_RECOVERY);
584 }
585 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
586 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500587 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500588 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500589 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500590 }
591}
592
James Smart87af33f2007-10-27 13:37:43 -0400593void
James Smart98c9ea52007-10-27 13:37:33 -0400594lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500595{
James Smart92d7f7b2007-06-17 19:56:38 -0500596 /* Cleanup any outstanding RSCN activity */
597 lpfc_els_flush_rscn(vport);
598
599 /* Cleanup any outstanding ELS commands */
600 lpfc_els_flush_cmd(vport);
601
602 lpfc_cleanup_rpis(vport, 0);
603
James Smart92d7f7b2007-06-17 19:56:38 -0500604 /* Turn off discovery timer if its running */
605 lpfc_can_disctmo(vport);
606}
607
James Smart98c9ea52007-10-27 13:37:33 -0400608static void
609lpfc_linkdown_port(struct lpfc_vport *vport)
610{
611 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
612
613 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
614
615 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
616 "Link Down: state:x%x rtry:x%x flg:x%x",
617 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
618
619 lpfc_port_link_failure(vport);
620
621}
622
dea31012005-04-17 16:05:31 -0500623int
James Smart685f0bf2007-04-25 09:53:08 -0400624lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500625{
James Smart2e0fef82007-06-17 19:56:36 -0500626 struct lpfc_vport *vport = phba->pport;
627 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400628 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400629 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400630 int i;
dea31012005-04-17 16:05:31 -0500631
James Smart2e0fef82007-06-17 19:56:36 -0500632 if (phba->link_state == LPFC_LINK_DOWN) {
633 return 0;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500634 }
James Smart2e0fef82007-06-17 19:56:36 -0500635 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500636 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500637 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500638 phba->pport->fc_flag &= ~FC_LBIT;
639 }
James Smart2e0fef82007-06-17 19:56:36 -0500640 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400641 vports = lpfc_create_vport_work_array(phba);
642 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500643 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400644 /* Issue a LINK DOWN event to all nodes */
645 lpfc_linkdown_port(vports[i]);
646 }
James Smart09372822008-01-11 01:52:54 -0500647 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500648 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500649 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
650 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500651 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500652 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500653 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400654 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500655 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500656 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500657 }
658 }
659
dea31012005-04-17 16:05:31 -0500660 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500661 if (phba->pport->fc_flag & FC_PT2PT) {
662 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500663 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
664 if (mb) {
dea31012005-04-17 16:05:31 -0500665 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500666 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500667 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400668 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500669 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500670 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500671 }
672 }
James Smart2e0fef82007-06-17 19:56:36 -0500673 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500674 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500675 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500676 }
James Smart2e0fef82007-06-17 19:56:36 -0500677
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500678 return 0;
dea31012005-04-17 16:05:31 -0500679}
680
James Smart92d7f7b2007-06-17 19:56:38 -0500681static void
682lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500683{
James Smart92d7f7b2007-06-17 19:56:38 -0500684 struct lpfc_nodelist *ndlp;
685
686 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500687 if (!NLP_CHK_NODE_ACT(ndlp))
688 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500689 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
690 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500691 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500692 /* On Linkup its safe to clean up the ndlp
693 * from Fabric connections.
694 */
James Smart92d7f7b2007-06-17 19:56:38 -0500695 if (ndlp->nlp_DID != Fabric_DID)
696 lpfc_unreg_rpi(vport, ndlp);
697 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
698 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500699 /* Fail outstanding IO now since device is
700 * marked for PLOGI.
701 */
James Smart92d7f7b2007-06-17 19:56:38 -0500702 lpfc_unreg_rpi(vport, ndlp);
703 }
704 }
705}
706
707static void
708lpfc_linkup_port(struct lpfc_vport *vport)
709{
710 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500711 struct lpfc_hba *phba = vport->phba;
712
713 if ((vport->load_flag & FC_UNLOADING) != 0)
714 return;
715
James Smart858c9f62007-06-17 19:56:39 -0500716 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
717 "Link Up: top:x%x speed:x%x flg:x%x",
718 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
719
James Smart92d7f7b2007-06-17 19:56:38 -0500720 /* If NPIV is not enabled, only bring the physical port up */
721 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
722 (vport != phba->pport))
723 return;
dea31012005-04-17 16:05:31 -0500724
James Smart2e0fef82007-06-17 19:56:36 -0500725 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400726
James Smart2e0fef82007-06-17 19:56:36 -0500727 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500728 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
729 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
730 vport->fc_flag |= FC_NDISC_ACTIVE;
731 vport->fc_ns_retry = 0;
732 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500733
James Smart92d7f7b2007-06-17 19:56:38 -0500734 if (vport->fc_flag & FC_LBIT)
735 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500736
James Smart92d7f7b2007-06-17 19:56:38 -0500737}
738
739static int
740lpfc_linkup(struct lpfc_hba *phba)
741{
James Smart549e55c2007-08-02 11:09:51 -0400742 struct lpfc_vport **vports;
743 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500744
745 phba->link_state = LPFC_LINK_UP;
746
747 /* Unblock fabric iocbs if they are blocked */
748 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
749 del_timer_sync(&phba->fabric_block_timer);
750
James Smart549e55c2007-08-02 11:09:51 -0400751 vports = lpfc_create_vport_work_array(phba);
752 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500753 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400754 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500755 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500756 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
757 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500758
759 return 0;
760}
761
762/*
763 * This routine handles processing a CLEAR_LA mailbox
764 * command upon completion. It is setup in the LPFC_MBOXQ
765 * as the completion routine when the command is
766 * handed off to the SLI layer.
767 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100768static void
James Smart2e0fef82007-06-17 19:56:36 -0500769lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500770{
James Smart2e0fef82007-06-17 19:56:36 -0500771 struct lpfc_vport *vport = pmb->vport;
772 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
773 struct lpfc_sli *psli = &phba->sli;
774 MAILBOX_t *mb = &pmb->mb;
dea31012005-04-17 16:05:31 -0500775 uint32_t control;
776
dea31012005-04-17 16:05:31 -0500777 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500778 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500779 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
780 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
781
782 /* Check for error */
783 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500784 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400785 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
786 "0320 CLEAR_LA mbxStatus error x%x hba "
787 "state x%x\n",
788 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500789 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500790 goto out;
791 }
792
James Smart92d7f7b2007-06-17 19:56:38 -0500793 if (vport->port_type == LPFC_PHYSICAL_PORT)
794 phba->link_state = LPFC_HBA_READY;
795
796 spin_lock_irq(&phba->hbalock);
797 psli->sli_flag |= LPFC_PROCESS_LA;
798 control = readl(phba->HCregaddr);
799 control |= HC_LAINT_ENA;
800 writel(control, phba->HCregaddr);
801 readl(phba->HCregaddr); /* flush */
802 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500803 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500804 return;
dea31012005-04-17 16:05:31 -0500805
dea31012005-04-17 16:05:31 -0500806out:
807 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400808 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
809 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500810 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500811
James Smart2e0fef82007-06-17 19:56:36 -0500812 spin_lock_irq(shost->host_lock);
813 vport->fc_flag &= ~(FC_ABORT_DISCOVERY | FC_ESTABLISH_LINK);
814 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500815
816 del_timer_sync(&phba->fc_estabtmo);
817
James Smart2e0fef82007-06-17 19:56:36 -0500818 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500819
820 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500821
822 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500823 psli->sli_flag |= LPFC_PROCESS_LA;
824 control = readl(phba->HCregaddr);
825 control |= HC_LAINT_ENA;
826 writel(control, phba->HCregaddr);
827 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500828 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500829
830 return;
831}
832
James Smart2e0fef82007-06-17 19:56:36 -0500833
dea31012005-04-17 16:05:31 -0500834static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500835lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500836{
James Smart2e0fef82007-06-17 19:56:36 -0500837 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500838
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500839 if (pmb->mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500840 goto out;
dea31012005-04-17 16:05:31 -0500841
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500842 mempool_free(pmb, phba->mbox_mem_pool);
843
844 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500845 vport->fc_flag & FC_PUBLIC_LOOP &&
846 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500847 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500848 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500849 * LPFC_LOCAL_CFG_LINK while waiting for FAN
850 */
James Smart2e0fef82007-06-17 19:56:36 -0500851 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500852 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500853 }
dea31012005-04-17 16:05:31 -0500854
James Smart2e0fef82007-06-17 19:56:36 -0500855 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500856 * LPFC_FLOGI while waiting for FLOGI cmpl
857 */
James Smart92d7f7b2007-06-17 19:56:38 -0500858 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500859 lpfc_initial_flogi(vport);
860 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500861 return;
dea31012005-04-17 16:05:31 -0500862
863out:
James Smarte8b62012007-08-02 11:10:09 -0400864 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
865 "0306 CONFIG_LINK mbxStatus error x%x "
866 "HBA state x%x\n",
867 pmb->mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500868 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500869
870 lpfc_linkdown(phba);
871
James Smarte8b62012007-08-02 11:10:09 -0400872 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
873 "0200 CONFIG_LINK bad hba state x%x\n",
874 vport->port_state);
dea31012005-04-17 16:05:31 -0500875
James Smart92d7f7b2007-06-17 19:56:38 -0500876 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500877 return;
878}
879
880static void
James Smart2e0fef82007-06-17 19:56:36 -0500881lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500882{
dea31012005-04-17 16:05:31 -0500883 MAILBOX_t *mb = &pmb->mb;
884 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500885 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500886
887
888 /* Check for error */
889 if (mb->mbxStatus) {
890 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400891 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
892 "0319 READ_SPARAM mbxStatus error x%x "
893 "hba state x%x>\n",
894 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -0500895 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -0500896 goto out;
897 }
898
James Smart2e0fef82007-06-17 19:56:36 -0500899 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -0500900 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -0500901 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500902 u64_to_wwn(phba->cfg_soft_wwnn,
903 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400904 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500905 u64_to_wwn(phba->cfg_soft_wwpn,
906 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -0500907 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
908 sizeof(vport->fc_nodename));
909 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
910 sizeof(vport->fc_portname));
911 if (vport->port_type == LPFC_PHYSICAL_PORT) {
912 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
913 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
914 }
915
dea31012005-04-17 16:05:31 -0500916 lpfc_mbuf_free(phba, mp->virt, mp->phys);
917 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -0500918 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500919 return;
920
921out:
922 pmb->context1 = NULL;
923 lpfc_mbuf_free(phba, mp->virt, mp->phys);
924 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -0500925 lpfc_issue_clear_la(phba, vport);
926 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500927 return;
928}
929
930static void
James Smart92d7f7b2007-06-17 19:56:38 -0500931lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -0500932{
James Smart92d7f7b2007-06-17 19:56:38 -0500933 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500934 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
James Smart2e0fef82007-06-17 19:56:36 -0500935 int i;
James Smart14691152006-12-02 13:34:28 -0500936 struct lpfc_dmabuf *mp;
937 int rc;
938
dea31012005-04-17 16:05:31 -0500939 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
940 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
941
James Smart92d7f7b2007-06-17 19:56:38 -0500942 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500943 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -0500944 case LA_1GHZ_LINK:
945 phba->fc_linkspeed = LA_1GHZ_LINK;
946 break;
947 case LA_2GHZ_LINK:
948 phba->fc_linkspeed = LA_2GHZ_LINK;
949 break;
950 case LA_4GHZ_LINK:
951 phba->fc_linkspeed = LA_4GHZ_LINK;
952 break;
953 case LA_8GHZ_LINK:
954 phba->fc_linkspeed = LA_8GHZ_LINK;
955 break;
956 default:
957 phba->fc_linkspeed = LA_UNKNW_LINK;
958 break;
dea31012005-04-17 16:05:31 -0500959 }
960
961 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -0500962 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -0500963
964 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -0500965 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -0500966
James Smart92d7f7b2007-06-17 19:56:38 -0500967 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -0500968 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -0500969 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500970
James Smart2e0fef82007-06-17 19:56:36 -0500971 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -0500972 i = la->un.lilpBde64.tus.f.bdeSize;
973
974 if (i == 0) {
975 phba->alpa_map[0] = 0;
976 } else {
James Smarte8b62012007-08-02 11:10:09 -0400977 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -0500978 int numalpa, j, k;
979 union {
980 uint8_t pamap[16];
981 struct {
982 uint32_t wd1;
983 uint32_t wd2;
984 uint32_t wd3;
985 uint32_t wd4;
986 } pa;
987 } un;
988 numalpa = phba->alpa_map[0];
989 j = 0;
990 while (j < numalpa) {
991 memset(un.pamap, 0, 16);
992 for (k = 1; j < numalpa; k++) {
993 un.pamap[k - 1] =
994 phba->alpa_map[j + 1];
995 j++;
996 if (k == 16)
997 break;
998 }
999 /* Link Up Event ALPA map */
1000 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05001001 KERN_WARNING,
1002 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001003 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05001004 "ALPA map Data: x%x "
1005 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001006 un.pa.wd1, un.pa.wd2,
1007 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05001008 }
1009 }
1010 }
1011 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05001012 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04001013 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05001014 (phba->sli_rev == 3))
1015 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1016 }
James Smart2e0fef82007-06-17 19:56:36 -05001017 vport->fc_myDID = phba->fc_pref_DID;
1018 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001019 }
James Smart92d7f7b2007-06-17 19:56:38 -05001020 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001021
1022 lpfc_linkup(phba);
1023 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001024 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05001025 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05001026 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04001027 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05001028 if (rc == MBX_NOT_FINISHED) {
1029 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1030 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1031 kfree(mp);
1032 mempool_free(sparam_mbox, phba->mbox_mem_pool);
1033 if (cfglink_mbox)
1034 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001035 goto out;
James Smart14691152006-12-02 13:34:28 -05001036 }
dea31012005-04-17 16:05:31 -05001037 }
1038
1039 if (cfglink_mbox) {
James Smart2e0fef82007-06-17 19:56:36 -05001040 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05001041 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05001042 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001043 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04001044 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001045 if (rc != MBX_NOT_FINISHED)
1046 return;
1047 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001048 }
James Smart92d7f7b2007-06-17 19:56:38 -05001049out:
1050 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001051 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1052 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1053 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05001054 lpfc_issue_clear_la(phba, vport);
1055 return;
dea31012005-04-17 16:05:31 -05001056}
1057
1058static void
James Smart2e0fef82007-06-17 19:56:36 -05001059lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1060{
dea31012005-04-17 16:05:31 -05001061 uint32_t control;
1062 struct lpfc_sli *psli = &phba->sli;
1063
1064 lpfc_linkdown(phba);
1065
1066 /* turn on Link Attention interrupts - no CLEAR_LA needed */
James Smart2e0fef82007-06-17 19:56:36 -05001067 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001068 psli->sli_flag |= LPFC_PROCESS_LA;
1069 control = readl(phba->HCregaddr);
1070 control |= HC_LAINT_ENA;
1071 writel(control, phba->HCregaddr);
1072 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001073 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001074}
1075
1076/*
1077 * This routine handles processing a READ_LA mailbox
1078 * command upon completion. It is setup in the LPFC_MBOXQ
1079 * as the completion routine when the command is
1080 * handed off to the SLI layer.
1081 */
1082void
James Smart2e0fef82007-06-17 19:56:36 -05001083lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001084{
James Smart2e0fef82007-06-17 19:56:36 -05001085 struct lpfc_vport *vport = pmb->vport;
1086 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05001087 READ_LA_VAR *la;
1088 MAILBOX_t *mb = &pmb->mb;
1089 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1090
1091 /* Check for error */
1092 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05001093 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001094 "1307 READ_LA mbox error x%x state x%x\n",
1095 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001096 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001097 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001098 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1099 }
1100
1101 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1102
1103 memcpy(&phba->alpa_map[0], mp->virt, 128);
1104
James Smart2e0fef82007-06-17 19:56:36 -05001105 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001106 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05001107 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001108 else
James Smart2e0fef82007-06-17 19:56:36 -05001109 vport->fc_flag &= ~FC_BYPASSED_MODE;
1110 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001111
dea31012005-04-17 16:05:31 -05001112 if (((phba->fc_eventTag + 1) < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05001113 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05001114 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05001115 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05001116 if (phba->fc_eventTag != 0)
1117 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001118 }
dea31012005-04-17 16:05:31 -05001119
1120 phba->fc_eventTag = la->eventTag;
1121
1122 if (la->attType == AT_LINK_UP) {
1123 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05001124 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart5b8bd0c2007-04-25 09:52:49 -04001125 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001126 "1306 Link Up Event in loop back mode "
1127 "x%x received Data: x%x x%x x%x x%x\n",
1128 la->eventTag, phba->fc_eventTag,
1129 la->granted_AL_PA, la->UlnkSpeed,
1130 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001131 } else {
1132 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001133 "1303 Link Up Event x%x received "
1134 "Data: x%x x%x x%x x%x\n",
1135 la->eventTag, phba->fc_eventTag,
1136 la->granted_AL_PA, la->UlnkSpeed,
1137 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001138 }
James Smart92d7f7b2007-06-17 19:56:38 -05001139 lpfc_mbx_process_link_up(phba, la);
dea31012005-04-17 16:05:31 -05001140 } else {
1141 phba->fc_stat.LinkDown++;
1142 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001143 "1305 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05001144 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001145 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05001146 phba->pport->port_state, vport->fc_flag);
dea31012005-04-17 16:05:31 -05001147 lpfc_mbx_issue_link_down(phba);
1148 }
1149
1150lpfc_mbx_cmpl_read_la_free_mbuf:
1151 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1152 kfree(mp);
1153 mempool_free(pmb, phba->mbox_mem_pool);
1154 return;
1155}
1156
1157/*
1158 * This routine handles processing a REG_LOGIN mailbox
1159 * command upon completion. It is setup in the LPFC_MBOXQ
1160 * as the completion routine when the command is
1161 * handed off to the SLI layer.
1162 */
1163void
James Smart2e0fef82007-06-17 19:56:36 -05001164lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001165{
James Smart2e0fef82007-06-17 19:56:36 -05001166 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001167 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05001168 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001169
dea31012005-04-17 16:05:31 -05001170 pmb->context1 = NULL;
1171
1172 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05001173 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05001174 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1175 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001176 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05001177 /* decrement the node reference count held for this callback
1178 * function.
1179 */
James Smart329f9bc2007-04-25 09:53:01 -04001180 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001181
1182 return;
1183}
1184
James Smart92d7f7b2007-06-17 19:56:38 -05001185static void
1186lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1187{
1188 MAILBOX_t *mb = &pmb->mb;
1189 struct lpfc_vport *vport = pmb->vport;
1190 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1191
1192 switch (mb->mbxStatus) {
1193 case 0x0011:
1194 case 0x0020:
1195 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04001196 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1197 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
1198 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001199 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001200 }
1201 vport->unreg_vpi_cmpl = VPORT_OK;
1202 mempool_free(pmb, phba->mbox_mem_pool);
1203 /*
1204 * This shost reference might have been taken at the beginning of
1205 * lpfc_vport_delete()
1206 */
1207 if (vport->load_flag & FC_UNLOADING)
1208 scsi_host_put(shost);
1209}
1210
1211void
1212lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1213{
1214 struct lpfc_hba *phba = vport->phba;
1215 LPFC_MBOXQ_t *mbox;
1216 int rc;
1217
1218 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1219 if (!mbox)
1220 return;
1221
1222 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1223 mbox->vport = vport;
1224 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04001225 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001226 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001227 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1228 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001229 mempool_free(mbox, phba->mbox_mem_pool);
1230 vport->unreg_vpi_cmpl = VPORT_ERROR;
1231 }
1232}
1233
1234static void
1235lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1236{
1237 struct lpfc_vport *vport = pmb->vport;
1238 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1239 MAILBOX_t *mb = &pmb->mb;
1240
1241 switch (mb->mbxStatus) {
1242 case 0x0011:
1243 case 0x9601:
1244 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04001245 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1246 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
1247 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001248 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1249 spin_lock_irq(shost->host_lock);
1250 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1251 spin_unlock_irq(shost->host_lock);
1252 vport->fc_myDID = 0;
1253 goto out;
1254 }
James Smart92d7f7b2007-06-17 19:56:38 -05001255
1256 vport->num_disc_nodes = 0;
1257 /* go thru NPR list and issue ELS PLOGIs */
1258 if (vport->fc_npr_cnt)
1259 lpfc_els_disc_plogi(vport);
1260
1261 if (!vport->num_disc_nodes) {
1262 spin_lock_irq(shost->host_lock);
1263 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1264 spin_unlock_irq(shost->host_lock);
1265 lpfc_can_disctmo(vport);
1266 }
1267 vport->port_state = LPFC_VPORT_READY;
1268
1269out:
1270 mempool_free(pmb, phba->mbox_mem_pool);
1271 return;
1272}
1273
dea31012005-04-17 16:05:31 -05001274/*
1275 * This routine handles processing a Fabric REG_LOGIN mailbox
1276 * command upon completion. It is setup in the LPFC_MBOXQ
1277 * as the completion routine when the command is
1278 * handed off to the SLI layer.
1279 */
1280void
James Smart2e0fef82007-06-17 19:56:36 -05001281lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001282{
James Smart92d7f7b2007-06-17 19:56:38 -05001283 struct lpfc_vport *vport = pmb->vport;
James Smart2e0fef82007-06-17 19:56:36 -05001284 MAILBOX_t *mb = &pmb->mb;
1285 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05001286 struct lpfc_nodelist *ndlp;
James Smart549e55c2007-08-02 11:09:51 -04001287 struct lpfc_vport **vports;
1288 int i;
dea31012005-04-17 16:05:31 -05001289
James Smart549e55c2007-08-02 11:09:51 -04001290 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04001291 pmb->context1 = NULL;
1292 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05001293 if (mb->mbxStatus) {
1294 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1295 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001296 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001297
James Smart92d7f7b2007-06-17 19:56:38 -05001298 if (phba->fc_topology == TOPOLOGY_LOOP) {
1299 /* FLOGI failed, use loop map to make discovery list */
1300 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001301
James Smart92d7f7b2007-06-17 19:56:38 -05001302 /* Start discovery */
1303 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05001304 /* Decrement the reference count to ndlp after the
1305 * reference to the ndlp are done.
1306 */
1307 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05001308 return;
1309 }
1310
1311 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001312 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1313 "0258 Register Fabric login error: 0x%x\n",
1314 mb->mbxStatus);
James Smarte47c9092008-02-08 18:49:26 -05001315 /* Decrement the reference count to ndlp after the reference
1316 * to the ndlp are done.
1317 */
1318 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001319 return;
1320 }
1321
dea31012005-04-17 16:05:31 -05001322 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001323 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001324 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001325
James Smart2e0fef82007-06-17 19:56:36 -05001326 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart549e55c2007-08-02 11:09:51 -04001327 vports = lpfc_create_vport_work_array(phba);
1328 if (vports != NULL)
1329 for(i = 0;
James Smart09372822008-01-11 01:52:54 -05001330 i <= phba->max_vpi && vports[i] != NULL;
James Smart549e55c2007-08-02 11:09:51 -04001331 i++) {
1332 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1333 continue;
1334 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1335 lpfc_initial_fdisc(vports[i]);
1336 else if (phba->sli3_options &
1337 LPFC_SLI3_NPIV_ENABLED) {
1338 lpfc_vport_set_state(vports[i],
1339 FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -04001340 lpfc_printf_vlog(vport, KERN_ERR,
1341 LOG_ELS,
1342 "0259 No NPIV "
1343 "Fabric support\n");
James Smart549e55c2007-08-02 11:09:51 -04001344 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001345 }
James Smart09372822008-01-11 01:52:54 -05001346 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001347 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05001348 }
1349
1350 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1351 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001352 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05001353
1354 /* Drop the reference count from the mbox at the end after
1355 * all the current reference to the ndlp have been done.
1356 */
1357 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001358 return;
1359}
1360
1361/*
1362 * This routine handles processing a NameServer REG_LOGIN mailbox
1363 * command upon completion. It is setup in the LPFC_MBOXQ
1364 * as the completion routine when the command is
1365 * handed off to the SLI layer.
1366 */
1367void
James Smart2e0fef82007-06-17 19:56:36 -05001368lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001369{
James Smart2e0fef82007-06-17 19:56:36 -05001370 MAILBOX_t *mb = &pmb->mb;
1371 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1372 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1373 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001374
1375 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05001376out:
James Smartfa4066b2008-01-11 01:53:27 -05001377 /* decrement the node reference count held for this
1378 * callback function.
1379 */
James Smart329f9bc2007-04-25 09:53:01 -04001380 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001381 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1382 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04001383 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04001384
1385 /* If no other thread is using the ndlp, free it */
1386 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05001387
James Smart92d7f7b2007-06-17 19:56:38 -05001388 if (phba->fc_topology == TOPOLOGY_LOOP) {
1389 /*
1390 * RegLogin failed, use loop map to make discovery
1391 * list
1392 */
1393 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001394
James Smart92d7f7b2007-06-17 19:56:38 -05001395 /* Start discovery */
1396 lpfc_disc_start(vport);
1397 return;
1398 }
1399 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001400 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1401 "0260 Register NameServer error: 0x%x\n",
1402 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001403 return;
1404 }
1405
1406 pmb->context1 = NULL;
1407
dea31012005-04-17 16:05:31 -05001408 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001409 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001410 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001411
James Smart2e0fef82007-06-17 19:56:36 -05001412 if (vport->port_state < LPFC_VPORT_READY) {
1413 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05001414 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1415 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1416 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1417 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1418 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1419
1420 /* Issue SCR just before NameServer GID_FT Query */
1421 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05001422 }
1423
James Smart2e0fef82007-06-17 19:56:36 -05001424 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05001425 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05001426 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05001427 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05001428 goto out;
dea31012005-04-17 16:05:31 -05001429 }
1430
James Smartfa4066b2008-01-11 01:53:27 -05001431 /* decrement the node reference count held for this
1432 * callback function.
1433 */
James Smart329f9bc2007-04-25 09:53:01 -04001434 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001435 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1436 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001437 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001438
1439 return;
1440}
1441
1442static void
James Smart2e0fef82007-06-17 19:56:36 -05001443lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001444{
James Smart2e0fef82007-06-17 19:56:36 -05001445 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1446 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05001447 struct lpfc_rport_data *rdata;
1448 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05001449 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001450
1451 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001452 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1453 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001454 rport_ids.port_id = ndlp->nlp_DID;
1455 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05001456
James Smart329f9bc2007-04-25 09:53:01 -04001457 /*
1458 * We leave our node pointer in rport->dd_data when we unregister a
1459 * FCP target port. But fc_remote_port_add zeros the space to which
1460 * rport->dd_data points. So, if we're reusing a previously
1461 * registered port, drop the reference that we took the last time we
1462 * registered the port.
1463 */
1464 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05001465 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04001466 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05001467
1468 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1469 "rport add: did:x%x flg:x%x type x%x",
1470 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1471
James Smart2e0fef82007-06-17 19:56:36 -05001472 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04001473 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05001474 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1475 "Warning: fc_remote_port_add failed\n");
1476 return;
1477 }
1478
1479 /* initialize static port data */
1480 rport->maxframe_size = ndlp->nlp_maxframe;
1481 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05001482 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04001483 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001484
1485 if (ndlp->nlp_type & NLP_FCP_TARGET)
1486 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1487 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1488 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1489
1490
1491 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1492 fc_remote_port_rolechg(rport, rport_ids.roles);
1493
James Smart071fbd3d2006-04-15 11:53:20 -04001494 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001495 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04001496 ndlp->nlp_sid = rport->scsi_target_id;
1497 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001498 return;
1499}
1500
1501static void
James Smart2e0fef82007-06-17 19:56:36 -05001502lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001503{
1504 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04001505
James Smart858c9f62007-06-17 19:56:39 -05001506 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
1507 "rport delete: did:x%x flg:x%x type x%x",
1508 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1509
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001510 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05001511
1512 return;
1513}
1514
James Smartde0c5b32007-04-25 09:52:27 -04001515static void
James Smart2e0fef82007-06-17 19:56:36 -05001516lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04001517{
James Smart2e0fef82007-06-17 19:56:36 -05001518 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1519
1520 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001521 switch (state) {
1522 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001523 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001524 break;
1525 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001526 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001527 break;
1528 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001529 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001530 break;
1531 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001532 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001533 break;
1534 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001535 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001536 break;
1537 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001538 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001539 break;
1540 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001541 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001542 break;
1543 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001544 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001545 break;
1546 }
James Smart2e0fef82007-06-17 19:56:36 -05001547 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001548}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001549
James Smartde0c5b32007-04-25 09:52:27 -04001550static void
James Smart2e0fef82007-06-17 19:56:36 -05001551lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04001552 int old_state, int new_state)
1553{
James Smart2e0fef82007-06-17 19:56:36 -05001554 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1555
James Smartde0c5b32007-04-25 09:52:27 -04001556 if (new_state == NLP_STE_UNMAPPED_NODE) {
1557 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1558 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1559 ndlp->nlp_type |= NLP_FC_NODE;
1560 }
1561 if (new_state == NLP_STE_MAPPED_NODE)
1562 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1563 if (new_state == NLP_STE_NPR_NODE)
1564 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05001565
James Smartde0c5b32007-04-25 09:52:27 -04001566 /* Transport interface */
1567 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1568 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001569 vport->phba->nport_event_cnt++;
1570 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001571 }
dea31012005-04-17 16:05:31 -05001572
James Smartde0c5b32007-04-25 09:52:27 -04001573 if (new_state == NLP_STE_MAPPED_NODE ||
1574 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05001575 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05001576 /*
1577 * Tell the fc transport about the port, if we haven't
1578 * already. If we have, and it's a scsi entity, be
1579 * sure to unblock any attached scsi devices
1580 */
1581 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001582 }
James Smart858c9f62007-06-17 19:56:39 -05001583 /*
1584 * if we added to Mapped list, but the remote port
1585 * registration failed or assigned a target id outside
1586 * our presentable range - move the node to the
1587 * Unmapped List
1588 */
James Smartde0c5b32007-04-25 09:52:27 -04001589 if (new_state == NLP_STE_MAPPED_NODE &&
1590 (!ndlp->rport ||
1591 ndlp->rport->scsi_target_id == -1 ||
1592 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05001593 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001594 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05001595 spin_unlock_irq(shost->host_lock);
1596 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001597 }
James Smartde0c5b32007-04-25 09:52:27 -04001598}
1599
James Smart685f0bf2007-04-25 09:53:08 -04001600static char *
1601lpfc_nlp_state_name(char *buffer, size_t size, int state)
1602{
1603 static char *states[] = {
1604 [NLP_STE_UNUSED_NODE] = "UNUSED",
1605 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1606 [NLP_STE_ADISC_ISSUE] = "ADISC",
1607 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1608 [NLP_STE_PRLI_ISSUE] = "PRLI",
1609 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1610 [NLP_STE_MAPPED_NODE] = "MAPPED",
1611 [NLP_STE_NPR_NODE] = "NPR",
1612 };
1613
James Smart311464e2007-08-02 11:10:37 -04001614 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04001615 strlcpy(buffer, states[state], size);
1616 else
1617 snprintf(buffer, size, "unknown (%d)", state);
1618 return buffer;
1619}
1620
James Smartde0c5b32007-04-25 09:52:27 -04001621void
James Smart2e0fef82007-06-17 19:56:36 -05001622lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1623 int state)
James Smartde0c5b32007-04-25 09:52:27 -04001624{
James Smart2e0fef82007-06-17 19:56:36 -05001625 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04001626 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04001627 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04001628
James Smarte8b62012007-08-02 11:10:09 -04001629 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1630 "0904 NPort state transition x%06x, %s -> %s\n",
1631 ndlp->nlp_DID,
1632 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1633 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05001634
1635 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1636 "node statechg did:x%x old:%d ste:%d",
1637 ndlp->nlp_DID, old_state, state);
1638
James Smartde0c5b32007-04-25 09:52:27 -04001639 if (old_state == NLP_STE_NPR_NODE &&
1640 (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
1641 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001642 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001643 if (old_state == NLP_STE_UNMAPPED_NODE) {
1644 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1645 ndlp->nlp_type &= ~NLP_FC_NODE;
1646 }
1647
James Smart685f0bf2007-04-25 09:53:08 -04001648 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05001649 spin_lock_irq(shost->host_lock);
1650 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1651 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001652 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05001653 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04001654
1655 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05001656 lpfc_nlp_counters(vport, state, 1);
1657 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04001658}
1659
1660void
James Smarte47c9092008-02-08 18:49:26 -05001661lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1662{
1663 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1664
1665 if (list_empty(&ndlp->nlp_listp)) {
1666 spin_lock_irq(shost->host_lock);
1667 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1668 spin_unlock_irq(shost->host_lock);
1669 }
1670}
1671
1672void
James Smart2e0fef82007-06-17 19:56:36 -05001673lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001674{
James Smart2e0fef82007-06-17 19:56:36 -05001675 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1676
James Smartde0c5b32007-04-25 09:52:27 -04001677 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
James Smart2e0fef82007-06-17 19:56:36 -05001678 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001679 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05001680 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1681 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001682 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05001683 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05001684 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05001685 NLP_STE_UNUSED_NODE);
1686}
1687
1688void
1689lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1690{
1691 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
1692 lpfc_cancel_retry_delay_tmo(vport, ndlp);
1693 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1694 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1695 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
1696 NLP_STE_UNUSED_NODE);
1697}
1698
1699struct lpfc_nodelist *
1700lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1701 int state)
1702{
1703 struct lpfc_hba *phba = vport->phba;
1704 uint32_t did;
1705 unsigned long flags;
1706
1707 if (!ndlp)
1708 return NULL;
1709
1710 spin_lock_irqsave(&phba->ndlp_lock, flags);
1711 /* The ndlp should not be in memory free mode */
1712 if (NLP_CHK_FREE_REQ(ndlp)) {
1713 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1714 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1715 "0277 lpfc_enable_node: ndlp:x%p "
1716 "usgmap:x%x refcnt:%d\n",
1717 (void *)ndlp, ndlp->nlp_usg_map,
1718 atomic_read(&ndlp->kref.refcount));
1719 return NULL;
1720 }
1721 /* The ndlp should not already be in active mode */
1722 if (NLP_CHK_NODE_ACT(ndlp)) {
1723 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1724 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1725 "0278 lpfc_enable_node: ndlp:x%p "
1726 "usgmap:x%x refcnt:%d\n",
1727 (void *)ndlp, ndlp->nlp_usg_map,
1728 atomic_read(&ndlp->kref.refcount));
1729 return NULL;
1730 }
1731
1732 /* Keep the original DID */
1733 did = ndlp->nlp_DID;
1734
1735 /* re-initialize ndlp except of ndlp linked list pointer */
1736 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
1737 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
1738 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
1739 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
1740 init_timer(&ndlp->nlp_delayfunc);
1741 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
1742 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
1743 ndlp->nlp_DID = did;
1744 ndlp->vport = vport;
1745 ndlp->nlp_sid = NLP_NO_SID;
1746 /* ndlp management re-initialize */
1747 kref_init(&ndlp->kref);
1748 NLP_INT_NODE_ACT(ndlp);
1749
1750 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1751
1752 if (state != NLP_STE_UNUSED_NODE)
1753 lpfc_nlp_set_state(vport, ndlp, state);
1754
1755 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1756 "node enable: did:x%x",
1757 ndlp->nlp_DID, 0, 0);
1758 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04001759}
1760
1761void
James Smart2e0fef82007-06-17 19:56:36 -05001762lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001763{
James Smart87af33f2007-10-27 13:37:43 -04001764 /*
James Smartfa4066b2008-01-11 01:53:27 -05001765 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04001766 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05001767 * the ndlp from the vport. The ndlp marked as UNUSED on the list
1768 * until ALL other outstanding threads have completed. We check
1769 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04001770 */
James Smartfa4066b2008-01-11 01:53:27 -05001771 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1772 return;
James Smart51ef4c22007-08-02 11:10:31 -04001773 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04001774 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04001775 return;
dea31012005-04-17 16:05:31 -05001776}
1777
1778/*
1779 * Start / ReStart rescue timer for Discovery / RSCN handling
1780 */
1781void
James Smart2e0fef82007-06-17 19:56:36 -05001782lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001783{
James Smart2e0fef82007-06-17 19:56:36 -05001784 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1785 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001786 uint32_t tmo;
1787
James Smart2e0fef82007-06-17 19:56:36 -05001788 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001789 /* For FAN, timeout should be greater then edtov */
1790 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1791 } else {
1792 /* Normal discovery timeout should be > then ELS/CT timeout
1793 * FC spec states we need 3 * ratov for CT requests
1794 */
1795 tmo = ((phba->fc_ratov * 3) + 3);
1796 }
dea31012005-04-17 16:05:31 -05001797
James Smart858c9f62007-06-17 19:56:39 -05001798
1799 if (!timer_pending(&vport->fc_disctmo)) {
1800 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1801 "set disc timer: tmo:x%x state:x%x flg:x%x",
1802 tmo, vport->port_state, vport->fc_flag);
1803 }
1804
James Smart2e0fef82007-06-17 19:56:36 -05001805 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1806 spin_lock_irq(shost->host_lock);
1807 vport->fc_flag |= FC_DISC_TMO;
1808 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001809
1810 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001811 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1812 "0247 Start Discovery Timer state x%x "
1813 "Data: x%x x%lx x%x x%x\n",
1814 vport->port_state, tmo,
1815 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1816 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05001817
1818 return;
1819}
1820
1821/*
1822 * Cancel rescue timer for Discovery / RSCN handling
1823 */
1824int
James Smart2e0fef82007-06-17 19:56:36 -05001825lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001826{
James Smart2e0fef82007-06-17 19:56:36 -05001827 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001828 unsigned long iflags;
1829
James Smart858c9f62007-06-17 19:56:39 -05001830 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1831 "can disc timer: state:x%x rtry:x%x flg:x%x",
1832 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
1833
dea31012005-04-17 16:05:31 -05001834 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05001835 if (vport->fc_flag & FC_DISC_TMO) {
1836 spin_lock_irqsave(shost->host_lock, iflags);
1837 vport->fc_flag &= ~FC_DISC_TMO;
1838 spin_unlock_irqrestore(shost->host_lock, iflags);
1839 del_timer_sync(&vport->fc_disctmo);
1840 spin_lock_irqsave(&vport->work_port_lock, iflags);
1841 vport->work_port_events &= ~WORKER_DISC_TMO;
1842 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05001843 }
1844
1845 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001846 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1847 "0248 Cancel Discovery Timer state x%x "
1848 "Data: x%x x%x x%x\n",
1849 vport->port_state, vport->fc_flag,
1850 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001851 return 0;
dea31012005-04-17 16:05:31 -05001852}
1853
1854/*
1855 * Check specified ring for outstanding IOCB on the SLI queue
1856 * Return true if iocb matches the specified nport
1857 */
1858int
James Smart2e0fef82007-06-17 19:56:36 -05001859lpfc_check_sli_ndlp(struct lpfc_hba *phba,
1860 struct lpfc_sli_ring *pring,
1861 struct lpfc_iocbq *iocb,
1862 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001863{
James Smart2e0fef82007-06-17 19:56:36 -05001864 struct lpfc_sli *psli = &phba->sli;
1865 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05001866 struct lpfc_vport *vport = ndlp->vport;
1867
1868 if (iocb->vport != vport)
1869 return 0;
1870
dea31012005-04-17 16:05:31 -05001871 if (pring->ringno == LPFC_ELS_RING) {
1872 switch (icmd->ulpCommand) {
1873 case CMD_GEN_REQUEST64_CR:
1874 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001875 return 1;
dea31012005-04-17 16:05:31 -05001876 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04001877 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1878 return 1;
dea31012005-04-17 16:05:31 -05001879 case CMD_XMIT_ELS_RSP64_CX:
1880 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001881 return 1;
dea31012005-04-17 16:05:31 -05001882 }
James Smarta4bc3372006-12-02 13:34:16 -05001883 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05001884
1885 } else if (pring->ringno == psli->fcp_ring) {
1886 /* Skip match check if waiting to relogin to FCP target */
1887 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001888 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001889 return 0;
dea31012005-04-17 16:05:31 -05001890 }
1891 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001892 return 1;
dea31012005-04-17 16:05:31 -05001893 }
1894 } else if (pring->ringno == psli->next_ring) {
1895
1896 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001897 return 0;
dea31012005-04-17 16:05:31 -05001898}
1899
1900/*
1901 * Free resources / clean up outstanding I/Os
1902 * associated with nlp_rpi in the LPFC_NODELIST entry.
1903 */
1904static int
James Smart2e0fef82007-06-17 19:56:36 -05001905lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001906{
James Smart2534ba72007-04-25 09:52:20 -04001907 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001908 struct lpfc_sli *psli;
1909 struct lpfc_sli_ring *pring;
1910 struct lpfc_iocbq *iocb, *next_iocb;
1911 IOCB_t *icmd;
1912 uint32_t rpi, i;
1913
James Smart92d7f7b2007-06-17 19:56:38 -05001914 lpfc_fabric_abort_nport(ndlp);
1915
dea31012005-04-17 16:05:31 -05001916 /*
1917 * Everything that matches on txcmplq will be returned
1918 * by firmware with a no rpi error.
1919 */
1920 psli = &phba->sli;
1921 rpi = ndlp->nlp_rpi;
1922 if (rpi) {
1923 /* Now process each ring */
1924 for (i = 0; i < psli->num_rings; i++) {
1925 pring = &psli->ring[i];
1926
James Smart2e0fef82007-06-17 19:56:36 -05001927 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001928 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05001929 list) {
dea31012005-04-17 16:05:31 -05001930 /*
1931 * Check to see if iocb matches the nport we are
1932 * looking for
1933 */
James Smart92d7f7b2007-06-17 19:56:38 -05001934 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
1935 ndlp))) {
dea31012005-04-17 16:05:31 -05001936 /* It matches, so deque and call compl
1937 with an error */
James Smart2534ba72007-04-25 09:52:20 -04001938 list_move_tail(&iocb->list,
1939 &completions);
dea31012005-04-17 16:05:31 -05001940 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05001941 }
1942 }
James Smart2e0fef82007-06-17 19:56:36 -05001943 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001944 }
1945 }
James Smart2534ba72007-04-25 09:52:20 -04001946
1947 while (!list_empty(&completions)) {
1948 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05001949 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04001950
James Smart2e0fef82007-06-17 19:56:36 -05001951 if (!iocb->iocb_cmpl)
1952 lpfc_sli_release_iocbq(phba, iocb);
1953 else {
James Smart2534ba72007-04-25 09:52:20 -04001954 icmd = &iocb->iocb;
1955 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1956 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
James Smart2e0fef82007-06-17 19:56:36 -05001957 (iocb->iocb_cmpl)(phba, iocb, iocb);
1958 }
James Smart2534ba72007-04-25 09:52:20 -04001959 }
1960
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001961 return 0;
dea31012005-04-17 16:05:31 -05001962}
1963
1964/*
1965 * Free rpi associated with LPFC_NODELIST entry.
1966 * This routine is called from lpfc_freenode(), when we are removing
1967 * a LPFC_NODELIST entry. It is also called if the driver initiates a
1968 * LOGO that completes successfully, and we are waiting to PLOGI back
1969 * to the remote NPort. In addition, it is called after we receive
1970 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1971 * we are waiting to PLOGI back to the remote NPort.
1972 */
1973int
James Smart2e0fef82007-06-17 19:56:36 -05001974lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001975{
James Smart2e0fef82007-06-17 19:56:36 -05001976 struct lpfc_hba *phba = vport->phba;
1977 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05001978 int rc;
1979
1980 if (ndlp->nlp_rpi) {
James Smart2e0fef82007-06-17 19:56:36 -05001981 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1982 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001983 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05001984 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001985 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04001986 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05001987 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05001988 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001989 }
dea31012005-04-17 16:05:31 -05001990 lpfc_no_rpi(phba, ndlp);
1991 ndlp->nlp_rpi = 0;
1992 return 1;
1993 }
1994 return 0;
1995}
1996
James Smart92d7f7b2007-06-17 19:56:38 -05001997void
1998lpfc_unreg_all_rpis(struct lpfc_vport *vport)
1999{
2000 struct lpfc_hba *phba = vport->phba;
2001 LPFC_MBOXQ_t *mbox;
2002 int rc;
2003
2004 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2005 if (mbox) {
2006 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
2007 mbox->vport = vport;
2008 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002009 mbox->context1 = NULL;
2010 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05002011 if (rc == MBX_NOT_FINISHED) {
2012 mempool_free(mbox, phba->mbox_mem_pool);
2013 }
2014 }
2015}
2016
2017void
2018lpfc_unreg_default_rpis(struct lpfc_vport *vport)
2019{
2020 struct lpfc_hba *phba = vport->phba;
2021 LPFC_MBOXQ_t *mbox;
2022 int rc;
2023
2024 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2025 if (mbox) {
2026 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
2027 mbox->vport = vport;
2028 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002029 mbox->context1 = NULL;
2030 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05002031 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002032 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2033 "1815 Could not issue "
2034 "unreg_did (default rpis)\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002035 mempool_free(mbox, phba->mbox_mem_pool);
2036 }
2037 }
2038}
2039
dea31012005-04-17 16:05:31 -05002040/*
2041 * Free resources associated with LPFC_NODELIST entry
2042 * so it can be freed.
2043 */
2044static int
James Smart2e0fef82007-06-17 19:56:36 -05002045lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002046{
James Smart2e0fef82007-06-17 19:56:36 -05002047 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2048 struct lpfc_hba *phba = vport->phba;
2049 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05002050 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05002051
2052 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04002053 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2054 "0900 Cleanup node for NPort x%x "
2055 "Data: x%x x%x x%x\n",
2056 ndlp->nlp_DID, ndlp->nlp_flag,
2057 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05002058 if (NLP_CHK_FREE_REQ(ndlp)) {
2059 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2060 "0280 lpfc_cleanup_node: ndlp:x%p "
2061 "usgmap:x%x refcnt:%d\n",
2062 (void *)ndlp, ndlp->nlp_usg_map,
2063 atomic_read(&ndlp->kref.refcount));
2064 lpfc_dequeue_node(vport, ndlp);
2065 } else {
2066 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2067 "0281 lpfc_cleanup_node: ndlp:x%p "
2068 "usgmap:x%x refcnt:%d\n",
2069 (void *)ndlp, ndlp->nlp_usg_map,
2070 atomic_read(&ndlp->kref.refcount));
2071 lpfc_disable_node(vport, ndlp);
2072 }
dea31012005-04-17 16:05:31 -05002073
dea31012005-04-17 16:05:31 -05002074 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
2075 if ((mb = phba->sli.mbox_active)) {
2076 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
2077 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
2078 mb->context2 = NULL;
2079 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2080 }
2081 }
James Smart33ccf8d2006-08-17 11:57:58 -04002082
James Smart2e0fef82007-06-17 19:56:36 -05002083 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002084 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
2085 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002086 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05002087 mp = (struct lpfc_dmabuf *) (mb->context1);
2088 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05002089 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002090 kfree(mp);
2091 }
2092 list_del(&mb->list);
2093 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002094 /* We shall not invoke the lpfc_nlp_put to decrement
2095 * the ndlp reference count as we are in the process
2096 * of lpfc_nlp_release.
2097 */
dea31012005-04-17 16:05:31 -05002098 }
2099 }
James Smart2e0fef82007-06-17 19:56:36 -05002100 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002101
James Smarte47c9092008-02-08 18:49:26 -05002102 lpfc_els_abort(phba, ndlp);
2103
James Smart2e0fef82007-06-17 19:56:36 -05002104 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04002105 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05002106 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002107
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05002108 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05002109 del_timer_sync(&ndlp->nlp_delayfunc);
2110
dea31012005-04-17 16:05:31 -05002111 if (!list_empty(&ndlp->els_retry_evt.evt_listp))
2112 list_del_init(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05002113 if (!list_empty(&ndlp->dev_loss_evt.evt_listp))
2114 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002115
James Smart2e0fef82007-06-17 19:56:36 -05002116 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05002117
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002118 return 0;
dea31012005-04-17 16:05:31 -05002119}
2120
2121/*
2122 * Check to see if we can free the nlp back to the freelist.
2123 * If we are in the middle of using the nlp in the discovery state
2124 * machine, defer the free till we reach the end of the state machine.
2125 */
James Smart329f9bc2007-04-25 09:53:01 -04002126static void
James Smart2e0fef82007-06-17 19:56:36 -05002127lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002128{
James Smarta8adb832007-10-27 13:37:53 -04002129 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04002130 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04002131 LPFC_MBOXQ_t *mbox;
2132 int rc;
dea31012005-04-17 16:05:31 -05002133
2134 if (ndlp->nlp_flag & NLP_DELAY_TMO) {
James Smart2e0fef82007-06-17 19:56:36 -05002135 lpfc_cancel_retry_delay_tmo(vport, ndlp);
dea31012005-04-17 16:05:31 -05002136 }
2137
James Smarta8adb832007-10-27 13:37:53 -04002138 if (ndlp->nlp_flag & NLP_DEFER_RM && !ndlp->nlp_rpi) {
2139 /* For this case we need to cleanup the default rpi
2140 * allocated by the firmware.
2141 */
2142 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
2143 != NULL) {
2144 rc = lpfc_reg_login(phba, vport->vpi, ndlp->nlp_DID,
2145 (uint8_t *) &vport->fc_sparam, mbox, 0);
2146 if (rc) {
2147 mempool_free(mbox, phba->mbox_mem_pool);
2148 }
2149 else {
2150 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2151 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2152 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05002153 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04002154 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2155 if (rc == MBX_NOT_FINISHED) {
2156 mempool_free(mbox, phba->mbox_mem_pool);
2157 }
2158 }
2159 }
2160 }
James Smart2e0fef82007-06-17 19:56:36 -05002161 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04002162
James Smart2e0fef82007-06-17 19:56:36 -05002163 /*
James Smart92d7f7b2007-06-17 19:56:38 -05002164 * We can get here with a non-NULL ndlp->rport because when we
2165 * unregister a rport we don't break the rport/node linkage. So if we
2166 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05002167 */
James Smart92d7f7b2007-06-17 19:56:38 -05002168 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04002169 rdata = ndlp->rport->dd_data;
2170 rdata->pnode = NULL;
2171 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05002172 }
dea31012005-04-17 16:05:31 -05002173}
2174
2175static int
James Smart2e0fef82007-06-17 19:56:36 -05002176lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2177 uint32_t did)
dea31012005-04-17 16:05:31 -05002178{
James Smart2e0fef82007-06-17 19:56:36 -05002179 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05002180
2181 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002182 return 0;
dea31012005-04-17 16:05:31 -05002183
2184 if (ndlp->nlp_DID == 0) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002185 return 0;
dea31012005-04-17 16:05:31 -05002186 }
2187
2188 /* First check for Direct match */
2189 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002190 return 1;
dea31012005-04-17 16:05:31 -05002191
2192 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05002193 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05002194 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002195 return 0;
dea31012005-04-17 16:05:31 -05002196 }
2197
2198 matchdid.un.word = did;
2199 ndlpdid.un.word = ndlp->nlp_DID;
2200 if (matchdid.un.b.id == ndlpdid.un.b.id) {
2201 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
2202 (mydid.un.b.area == matchdid.un.b.area)) {
2203 if ((ndlpdid.un.b.domain == 0) &&
2204 (ndlpdid.un.b.area == 0)) {
2205 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002206 return 1;
dea31012005-04-17 16:05:31 -05002207 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002208 return 0;
dea31012005-04-17 16:05:31 -05002209 }
2210
2211 matchdid.un.word = ndlp->nlp_DID;
2212 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
2213 (mydid.un.b.area == ndlpdid.un.b.area)) {
2214 if ((matchdid.un.b.domain == 0) &&
2215 (matchdid.un.b.area == 0)) {
2216 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002217 return 1;
dea31012005-04-17 16:05:31 -05002218 }
2219 }
2220 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002221 return 0;
dea31012005-04-17 16:05:31 -05002222}
2223
James Smart685f0bf2007-04-25 09:53:08 -04002224/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05002225static struct lpfc_nodelist *
2226__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002227{
James Smart2fb9bd82006-12-02 13:33:57 -05002228 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002229 uint32_t data1;
2230
James Smart2e0fef82007-06-17 19:56:36 -05002231 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2232 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04002233 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2234 ((uint32_t) ndlp->nlp_xri << 16) |
2235 ((uint32_t) ndlp->nlp_type << 8) |
2236 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04002237 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2238 "0929 FIND node DID "
2239 "Data: x%p x%x x%x x%x\n",
2240 ndlp, ndlp->nlp_DID,
2241 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04002242 return ndlp;
dea31012005-04-17 16:05:31 -05002243 }
2244 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002245
dea31012005-04-17 16:05:31 -05002246 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04002247 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2248 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05002249 return NULL;
2250}
2251
2252struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002253lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002254{
James Smart2e0fef82007-06-17 19:56:36 -05002255 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002256 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002257
James Smart2e0fef82007-06-17 19:56:36 -05002258 spin_lock_irq(shost->host_lock);
2259 ndlp = __lpfc_findnode_did(vport, did);
2260 spin_unlock_irq(shost->host_lock);
2261 return ndlp;
2262}
2263
2264struct lpfc_nodelist *
2265lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2266{
2267 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2268 struct lpfc_nodelist *ndlp;
2269
2270 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002271 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05002272 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2273 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05002274 return NULL;
2275 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05002276 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002277 if (!ndlp)
2278 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002279 lpfc_nlp_init(vport, ndlp, did);
2280 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2281 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002282 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002283 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002284 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05002285 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2286 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
2287 if (!ndlp)
2288 return NULL;
2289 spin_lock_irq(shost->host_lock);
2290 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2291 spin_unlock_irq(shost->host_lock);
2292 return ndlp;
dea31012005-04-17 16:05:31 -05002293 }
James Smarte47c9092008-02-08 18:49:26 -05002294
James Smart2e0fef82007-06-17 19:56:36 -05002295 if (vport->fc_flag & FC_RSCN_MODE) {
2296 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04002297 /* If we've already recieved a PLOGI from this NPort
2298 * we don't need to try to discover it again.
2299 */
2300 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
2301 return NULL;
2302
James Smart2e0fef82007-06-17 19:56:36 -05002303 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002304 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002305 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002306
2307 /* Since this node is marked for discovery,
2308 * delay timeout is not needed.
2309 */
James Smartfdcebe22006-03-07 15:04:01 -05002310 if (ndlp->nlp_flag & NLP_DELAY_TMO)
James Smart2e0fef82007-06-17 19:56:36 -05002311 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart071fbd3d2006-04-15 11:53:20 -04002312 } else
dea31012005-04-17 16:05:31 -05002313 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002314 } else {
James Smart87af33f2007-10-27 13:37:43 -04002315 /* If we've already recieved a PLOGI from this NPort,
2316 * or we are already in the process of discovery on it,
2317 * we don't need to try to discover it again.
2318 */
James Smart685f0bf2007-04-25 09:53:08 -04002319 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04002320 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2321 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05002322 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002323 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2324 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002325 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002326 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002327 }
2328 return ndlp;
2329}
2330
2331/* Build a list of nodes to discover based on the loopmap */
2332void
James Smart2e0fef82007-06-17 19:56:36 -05002333lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002334{
James Smart2e0fef82007-06-17 19:56:36 -05002335 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002336 int j;
2337 uint32_t alpa, index;
2338
James Smart2e0fef82007-06-17 19:56:36 -05002339 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002340 return;
James Smart2e0fef82007-06-17 19:56:36 -05002341
2342 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05002343 return;
dea31012005-04-17 16:05:31 -05002344
2345 /* Check for loop map present or not */
2346 if (phba->alpa_map[0]) {
2347 for (j = 1; j <= phba->alpa_map[0]; j++) {
2348 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05002349 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05002350 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002351 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002352 }
2353 } else {
2354 /* No alpamap, so try all alpa's */
2355 for (j = 0; j < FC_MAXLOOP; j++) {
2356 /* If cfg_scan_down is set, start from highest
2357 * ALPA (0xef) to lowest (0x1).
2358 */
James Smart3de2a652007-08-02 11:09:59 -04002359 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05002360 index = j;
2361 else
2362 index = FC_MAXLOOP - j - 1;
2363 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05002364 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05002365 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002366 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002367 }
2368 }
2369 return;
2370}
2371
dea31012005-04-17 16:05:31 -05002372void
James Smart2e0fef82007-06-17 19:56:36 -05002373lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002374{
dea31012005-04-17 16:05:31 -05002375 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05002376 struct lpfc_sli *psli = &phba->sli;
2377 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2378 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2379 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2380 int rc;
2381
James Smart92d7f7b2007-06-17 19:56:38 -05002382 /*
2383 * if it's not a physical port or if we already send
2384 * clear_la then don't send it.
2385 */
2386 if ((phba->link_state >= LPFC_CLEAR_LA) ||
2387 (vport->port_type != LPFC_PHYSICAL_PORT))
2388 return;
2389
James Smart2e0fef82007-06-17 19:56:36 -05002390 /* Link up discovery */
2391 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2392 phba->link_state = LPFC_CLEAR_LA;
2393 lpfc_clear_la(phba, mbox);
2394 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2395 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002396 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05002397 if (rc == MBX_NOT_FINISHED) {
2398 mempool_free(mbox, phba->mbox_mem_pool);
2399 lpfc_disc_flush_list(vport);
2400 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2401 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2402 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05002403 phba->link_state = LPFC_HBA_ERROR;
2404 }
2405 }
2406}
2407
2408/* Reg_vpi to tell firmware to resume normal operations */
2409void
2410lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2411{
2412 LPFC_MBOXQ_t *regvpimbox;
2413
2414 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2415 if (regvpimbox) {
2416 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
2417 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2418 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002419 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05002420 == MBX_NOT_FINISHED) {
2421 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002422 }
2423 }
2424}
2425
2426/* Start Link up / RSCN discovery on NPR nodes */
2427void
2428lpfc_disc_start(struct lpfc_vport *vport)
2429{
2430 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2431 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04002432 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05002433 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04002434 int did_changed;
dea31012005-04-17 16:05:31 -05002435
James Smart2e0fef82007-06-17 19:56:36 -05002436 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002437 return;
James Smart2e0fef82007-06-17 19:56:36 -05002438
2439 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05002440 clear_la_pending = 1;
2441 else
2442 clear_la_pending = 0;
2443
James Smart2e0fef82007-06-17 19:56:36 -05002444 if (vport->port_state < LPFC_VPORT_READY)
2445 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05002446
James Smart2e0fef82007-06-17 19:56:36 -05002447 lpfc_set_disctmo(vport);
2448
2449 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05002450 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002451 else
dea31012005-04-17 16:05:31 -05002452 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002453
2454 vport->fc_prevDID = vport->fc_myDID;
2455 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05002456
2457 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002458 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2459 "0202 Start Discovery hba state x%x "
2460 "Data: x%x x%x x%x\n",
2461 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
2462 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002463
2464 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002465 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05002466
2467 if (num_sent)
2468 return;
2469
James Smart92d7f7b2007-06-17 19:56:38 -05002470 /*
2471 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2472 * continue discovery.
2473 */
2474 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05002475 !(vport->fc_flag & FC_PT2PT) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002476 !(vport->fc_flag & FC_RSCN_MODE)) {
2477 lpfc_issue_reg_vpi(phba, vport);
2478 return;
2479 }
James Smart2e0fef82007-06-17 19:56:36 -05002480
James Smart92d7f7b2007-06-17 19:56:38 -05002481 /*
2482 * For SLI2, we need to set port_state to READY and continue
2483 * discovery.
2484 */
2485 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2486 /* If we get here, there is nothing to ADISC */
2487 if (vport->port_type == LPFC_PHYSICAL_PORT)
2488 lpfc_issue_clear_la(phba, vport);
2489
2490 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05002491 vport->num_disc_nodes = 0;
2492 /* go thru NPR nodes and issue ELS PLOGIs */
2493 if (vport->fc_npr_cnt)
2494 lpfc_els_disc_plogi(vport);
2495
2496 if (!vport->num_disc_nodes) {
2497 spin_lock_irq(shost->host_lock);
2498 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2499 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002500 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05002501 }
2502 }
James Smart92d7f7b2007-06-17 19:56:38 -05002503 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002504 } else {
2505 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002506 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05002507
2508 if (num_sent)
2509 return;
2510
James Smart2e0fef82007-06-17 19:56:36 -05002511 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05002512 /* Check to see if more RSCNs came in while we
2513 * were processing this one.
2514 */
James Smart2e0fef82007-06-17 19:56:36 -05002515 if ((vport->fc_rscn_id_cnt == 0) &&
2516 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2517 spin_lock_irq(shost->host_lock);
2518 vport->fc_flag &= ~FC_RSCN_MODE;
2519 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002520 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002521 } else
James Smart2e0fef82007-06-17 19:56:36 -05002522 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05002523 }
2524 }
2525 return;
2526}
2527
2528/*
2529 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2530 * ring the match the sppecified nodelist.
2531 */
2532static void
James Smart2e0fef82007-06-17 19:56:36 -05002533lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002534{
James Smart2534ba72007-04-25 09:52:20 -04002535 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002536 struct lpfc_sli *psli;
2537 IOCB_t *icmd;
2538 struct lpfc_iocbq *iocb, *next_iocb;
2539 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002540
2541 psli = &phba->sli;
2542 pring = &psli->ring[LPFC_ELS_RING];
2543
2544 /* Error matching iocb on txq or txcmplq
2545 * First check the txq.
2546 */
James Smart2e0fef82007-06-17 19:56:36 -05002547 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002548 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2549 if (iocb->context1 != ndlp) {
2550 continue;
2551 }
2552 icmd = &iocb->iocb;
2553 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2554 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2555
James Smart2534ba72007-04-25 09:52:20 -04002556 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05002557 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002558 }
2559 }
2560
2561 /* Next check the txcmplq */
2562 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2563 if (iocb->context1 != ndlp) {
2564 continue;
2565 }
2566 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05002567 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2568 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04002569 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05002570 }
2571 }
James Smart2e0fef82007-06-17 19:56:36 -05002572 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002573
2574 while (!list_empty(&completions)) {
2575 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05002576 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04002577
James Smart2e0fef82007-06-17 19:56:36 -05002578 if (!iocb->iocb_cmpl)
2579 lpfc_sli_release_iocbq(phba, iocb);
2580 else {
James Smart2534ba72007-04-25 09:52:20 -04002581 icmd = &iocb->iocb;
2582 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2583 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2584 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002585 }
James Smart2534ba72007-04-25 09:52:20 -04002586 }
dea31012005-04-17 16:05:31 -05002587}
2588
Adrian Bunka6ababd2007-11-05 18:07:33 +01002589static void
James Smart2e0fef82007-06-17 19:56:36 -05002590lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002591{
2592 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05002593 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002594
James Smart2e0fef82007-06-17 19:56:36 -05002595 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2596 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002597 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002598 if (!NLP_CHK_NODE_ACT(ndlp))
2599 continue;
James Smart685f0bf2007-04-25 09:53:08 -04002600 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2601 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2602 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04002603 }
dea31012005-04-17 16:05:31 -05002604 }
2605 }
dea31012005-04-17 16:05:31 -05002606}
2607
James Smart92d7f7b2007-06-17 19:56:38 -05002608void
2609lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2610{
2611 lpfc_els_flush_rscn(vport);
2612 lpfc_els_flush_cmd(vport);
2613 lpfc_disc_flush_list(vport);
2614}
2615
dea31012005-04-17 16:05:31 -05002616/*****************************************************************************/
2617/*
2618 * NAME: lpfc_disc_timeout
2619 *
2620 * FUNCTION: Fibre Channel driver discovery timeout routine.
2621 *
2622 * EXECUTION ENVIRONMENT: interrupt only
2623 *
2624 * CALLED FROM:
2625 * Timer function
2626 *
2627 * RETURNS:
2628 * none
2629 */
2630/*****************************************************************************/
2631void
2632lpfc_disc_timeout(unsigned long ptr)
2633{
James Smart2e0fef82007-06-17 19:56:36 -05002634 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2635 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002636 unsigned long flags = 0;
2637
2638 if (unlikely(!phba))
2639 return;
2640
James Smart2e0fef82007-06-17 19:56:36 -05002641 if ((vport->work_port_events & WORKER_DISC_TMO) == 0) {
2642 spin_lock_irqsave(&vport->work_port_lock, flags);
2643 vport->work_port_events |= WORKER_DISC_TMO;
2644 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2645
James Smart92d7f7b2007-06-17 19:56:38 -05002646 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002647 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05002648 lpfc_worker_wake_up(phba);
2649 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002650 }
dea31012005-04-17 16:05:31 -05002651 return;
2652}
2653
2654static void
James Smart2e0fef82007-06-17 19:56:36 -05002655lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002656{
James Smart2e0fef82007-06-17 19:56:36 -05002657 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2658 struct lpfc_hba *phba = vport->phba;
2659 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002660 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05002661 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05002662 int rc, clrlaerr = 0;
2663
James Smart2e0fef82007-06-17 19:56:36 -05002664 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05002665 return;
2666
James Smart2e0fef82007-06-17 19:56:36 -05002667 spin_lock_irq(shost->host_lock);
2668 vport->fc_flag &= ~FC_DISC_TMO;
2669 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002670
James Smart858c9f62007-06-17 19:56:39 -05002671 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2672 "disc timeout: state:x%x rtry:x%x flg:x%x",
2673 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2674
James Smart2e0fef82007-06-17 19:56:36 -05002675 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05002676
2677 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05002678 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2679 * FAN
2680 */
2681 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04002682 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
2683 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002684 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05002685 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002686 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002687 if (!NLP_CHK_NODE_ACT(ndlp))
2688 continue;
James Smart685f0bf2007-04-25 09:53:08 -04002689 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2690 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002691 if (ndlp->nlp_type & NLP_FABRIC) {
2692 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05002693 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04002694
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002695 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002696 /* Fail outstanding IO now since device
2697 * is marked for PLOGI.
2698 */
James Smart2e0fef82007-06-17 19:56:36 -05002699 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002700 }
2701 }
James Smart92d7f7b2007-06-17 19:56:38 -05002702 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05002703 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05002704 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002705 }
dea31012005-04-17 16:05:31 -05002706 break;
2707
James Smart92d7f7b2007-06-17 19:56:38 -05002708 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05002709 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05002710 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05002711 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04002712 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2713 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04002714 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05002715
2716 /* Assume no Fabric and go on with discovery.
2717 * Check for outstanding ELS FLOGI to abort.
2718 */
2719
2720 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05002721 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002722
2723 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002724 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002725 break;
2726
2727 case LPFC_FABRIC_CFG_LINK:
2728 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2729 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04002730 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2731 "0223 Timeout while waiting for "
2732 "NameServer login\n");
dea31012005-04-17 16:05:31 -05002733 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05002734 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05002735 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04002736 lpfc_els_abort(phba, ndlp);
2737
2738 /* ReStart discovery */
2739 goto restart_disc;
dea31012005-04-17 16:05:31 -05002740
2741 case LPFC_NS_QRY:
2742 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04002743 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2744 "0224 NameServer Query timeout "
2745 "Data: x%x x%x\n",
2746 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002747
James Smart92d7f7b2007-06-17 19:56:38 -05002748 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2749 /* Try it one more time */
2750 vport->fc_ns_retry++;
2751 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2752 vport->fc_ns_retry, 0);
2753 if (rc == 0)
2754 break;
dea31012005-04-17 16:05:31 -05002755 }
James Smart92d7f7b2007-06-17 19:56:38 -05002756 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002757
James Smart87af33f2007-10-27 13:37:43 -04002758restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05002759 /*
2760 * Discovery is over.
2761 * set port_state to PORT_READY if SLI2.
2762 * cmpl_reg_vpi will set port_state to READY for SLI3.
2763 */
2764 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2765 lpfc_issue_reg_vpi(phba, vport);
2766 else { /* NPIV Not enabled */
2767 lpfc_issue_clear_la(phba, vport);
2768 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002769 }
2770
2771 /* Setup and issue mailbox INITIALIZE LINK command */
2772 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2773 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04002774 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2775 "0206 Device Discovery "
2776 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05002777 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002778 break;
2779 }
2780
2781 lpfc_linkdown(phba);
2782 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2783 phba->cfg_link_speed);
2784 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05002785 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002786 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002787 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04002788 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05002789 if (rc == MBX_NOT_FINISHED)
2790 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2791
2792 break;
2793
2794 case LPFC_DISC_AUTH:
2795 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04002796 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2797 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002798 lpfc_disc_flush_list(vport);
2799
James Smart92d7f7b2007-06-17 19:56:38 -05002800 /*
2801 * set port_state to PORT_READY if SLI2.
2802 * cmpl_reg_vpi will set port_state to READY for SLI3.
2803 */
2804 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2805 lpfc_issue_reg_vpi(phba, vport);
2806 else { /* NPIV Not enabled */
2807 lpfc_issue_clear_la(phba, vport);
2808 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002809 }
2810 break;
2811
James Smart2e0fef82007-06-17 19:56:36 -05002812 case LPFC_VPORT_READY:
2813 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04002814 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2815 "0231 RSCN timeout Data: x%x "
2816 "x%x\n",
2817 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002818
2819 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05002820 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05002821
James Smart2e0fef82007-06-17 19:56:36 -05002822 lpfc_els_flush_rscn(vport);
2823 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05002824 }
2825 break;
James Smart2e0fef82007-06-17 19:56:36 -05002826
James Smart92d7f7b2007-06-17 19:56:38 -05002827 default:
James Smarte8b62012007-08-02 11:10:09 -04002828 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2829 "0229 Unexpected discovery timeout, "
2830 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05002831 break;
2832 }
2833
2834 switch (phba->link_state) {
2835 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05002836 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04002837 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2838 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002839 clrlaerr = 1;
2840 break;
2841
James Smart09372822008-01-11 01:52:54 -05002842 case LPFC_LINK_UP:
2843 lpfc_issue_clear_la(phba, vport);
2844 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05002845 case LPFC_LINK_UNKNOWN:
2846 case LPFC_WARM_START:
2847 case LPFC_INIT_START:
2848 case LPFC_INIT_MBX_CMDS:
2849 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05002850 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04002851 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2852 "0230 Unexpected timeout, hba link "
2853 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05002854 clrlaerr = 1;
2855 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002856
2857 case LPFC_HBA_READY:
2858 break;
dea31012005-04-17 16:05:31 -05002859 }
2860
2861 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05002862 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05002863 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002864 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2865 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05002866 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002867 }
2868
2869 return;
2870}
2871
dea31012005-04-17 16:05:31 -05002872/*
2873 * This routine handles processing a NameServer REG_LOGIN mailbox
2874 * command upon completion. It is setup in the LPFC_MBOXQ
2875 * as the completion routine when the command is
2876 * handed off to the SLI layer.
2877 */
2878void
James Smart2e0fef82007-06-17 19:56:36 -05002879lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002880{
James Smart2e0fef82007-06-17 19:56:36 -05002881 MAILBOX_t *mb = &pmb->mb;
2882 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2883 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2884 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002885
2886 pmb->context1 = NULL;
2887
dea31012005-04-17 16:05:31 -05002888 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05002889 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002890 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002891
James Smart2e0fef82007-06-17 19:56:36 -05002892 /*
2893 * Start issuing Fabric-Device Management Interface (FDMI) command to
2894 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
2895 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05002896 */
James Smart2e0fef82007-06-17 19:56:36 -05002897
James Smart3de2a652007-08-02 11:09:59 -04002898 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05002899 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
2900 else
2901 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05002902
James Smartfa4066b2008-01-11 01:53:27 -05002903 /* decrement the node reference count held for this callback
2904 * function.
2905 */
James Smart329f9bc2007-04-25 09:53:01 -04002906 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002907 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2908 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002909 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002910
2911 return;
2912}
2913
James Smart685f0bf2007-04-25 09:53:08 -04002914static int
2915lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2916{
2917 uint16_t *rpi = param;
2918
2919 return ndlp->nlp_rpi == *rpi;
2920}
2921
2922static int
2923lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2924{
2925 return memcmp(&ndlp->nlp_portname, param,
2926 sizeof(ndlp->nlp_portname)) == 0;
2927}
2928
Adrian Bunka6ababd2007-11-05 18:07:33 +01002929static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002930__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05002931{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002932 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002933
James Smart2e0fef82007-06-17 19:56:36 -05002934 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04002935 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04002936 return ndlp;
2937 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002938 return NULL;
dea31012005-04-17 16:05:31 -05002939}
2940
Adrian Bunka6ababd2007-11-05 18:07:33 +01002941#if 0
James Smart685f0bf2007-04-25 09:53:08 -04002942/*
2943 * Search node lists for a remote port matching filter criteria
James Smart92d7f7b2007-06-17 19:56:38 -05002944 * Caller needs to hold host_lock before calling this routine.
James Smart685f0bf2007-04-25 09:53:08 -04002945 */
2946struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002947lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
James Smart685f0bf2007-04-25 09:53:08 -04002948{
James Smart2e0fef82007-06-17 19:56:36 -05002949 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart685f0bf2007-04-25 09:53:08 -04002950 struct lpfc_nodelist *ndlp;
2951
James Smart2e0fef82007-06-17 19:56:36 -05002952 spin_lock_irq(shost->host_lock);
2953 ndlp = __lpfc_find_node(vport, filter, param);
2954 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002955 return ndlp;
2956}
Adrian Bunka6ababd2007-11-05 18:07:33 +01002957#endif /* 0 */
James Smart685f0bf2007-04-25 09:53:08 -04002958
2959/*
2960 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05002961 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04002962 */
2963struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002964__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04002965{
James Smart2e0fef82007-06-17 19:56:36 -05002966 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04002967}
2968
Adrian Bunka6ababd2007-11-05 18:07:33 +01002969#if 0
James Smart2534ba72007-04-25 09:52:20 -04002970struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002971lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart2534ba72007-04-25 09:52:20 -04002972{
James Smart2e0fef82007-06-17 19:56:36 -05002973 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2534ba72007-04-25 09:52:20 -04002974 struct lpfc_nodelist *ndlp;
2975
James Smart2e0fef82007-06-17 19:56:36 -05002976 spin_lock_irq(shost->host_lock);
2977 ndlp = __lpfc_findnode_rpi(vport, rpi);
2978 spin_unlock_irq(shost->host_lock);
James Smart2534ba72007-04-25 09:52:20 -04002979 return ndlp;
2980}
Adrian Bunka6ababd2007-11-05 18:07:33 +01002981#endif /* 0 */
James Smart2534ba72007-04-25 09:52:20 -04002982
James Smart488d1462006-03-07 15:02:37 -05002983/*
James Smart685f0bf2007-04-25 09:53:08 -04002984 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05002985 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05002986 */
2987struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002988lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05002989{
James Smart2e0fef82007-06-17 19:56:36 -05002990 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05002991 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05002992
James Smart2e0fef82007-06-17 19:56:36 -05002993 spin_lock_irq(shost->host_lock);
2994 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
2995 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002996 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05002997}
2998
dea31012005-04-17 16:05:31 -05002999void
James Smart2e0fef82007-06-17 19:56:36 -05003000lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3001 uint32_t did)
dea31012005-04-17 16:05:31 -05003002{
3003 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -05003004 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05003005 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05003006 init_timer(&ndlp->nlp_delayfunc);
3007 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3008 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3009 ndlp->nlp_DID = did;
James Smart2e0fef82007-06-17 19:56:36 -05003010 ndlp->vport = vport;
dea31012005-04-17 16:05:31 -05003011 ndlp->nlp_sid = NLP_NO_SID;
James Smart685f0bf2007-04-25 09:53:08 -04003012 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart329f9bc2007-04-25 09:53:01 -04003013 kref_init(&ndlp->kref);
James Smarte47c9092008-02-08 18:49:26 -05003014 NLP_INT_NODE_ACT(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003015
3016 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3017 "node init: did:x%x",
3018 ndlp->nlp_DID, 0, 0);
3019
dea31012005-04-17 16:05:31 -05003020 return;
3021}
James Smart329f9bc2007-04-25 09:53:01 -04003022
James Smart98c9ea52007-10-27 13:37:33 -04003023/* This routine releases all resources associated with a specifc NPort's ndlp
3024 * and mempool_free's the nodelist.
3025 */
James Smart311464e2007-08-02 11:10:37 -04003026static void
James Smart329f9bc2007-04-25 09:53:01 -04003027lpfc_nlp_release(struct kref *kref)
3028{
James Smarte47c9092008-02-08 18:49:26 -05003029 struct lpfc_hba *phba;
3030 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04003031 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
3032 kref);
James Smart858c9f62007-06-17 19:56:39 -05003033
3034 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3035 "node release: did:x%x flg:x%x type:x%x",
3036 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3037
James Smarte47c9092008-02-08 18:49:26 -05003038 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3039 "0279 lpfc_nlp_release: ndlp:x%p "
3040 "usgmap:x%x refcnt:%d\n",
3041 (void *)ndlp, ndlp->nlp_usg_map,
3042 atomic_read(&ndlp->kref.refcount));
3043
3044 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05003045 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003046
3047 /* clear the ndlp active flag for all release cases */
3048 phba = ndlp->vport->phba;
3049 spin_lock_irqsave(&phba->ndlp_lock, flags);
3050 NLP_CLR_NODE_ACT(ndlp);
3051 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3052
3053 /* free ndlp memory for final ndlp release */
3054 if (NLP_CHK_FREE_REQ(ndlp))
3055 mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04003056}
3057
James Smart98c9ea52007-10-27 13:37:33 -04003058/* This routine bumps the reference count for a ndlp structure to ensure
3059 * that one discovery thread won't free a ndlp while another discovery thread
3060 * is using it.
3061 */
James Smart329f9bc2007-04-25 09:53:01 -04003062struct lpfc_nodelist *
3063lpfc_nlp_get(struct lpfc_nodelist *ndlp)
3064{
James Smarte47c9092008-02-08 18:49:26 -05003065 struct lpfc_hba *phba;
3066 unsigned long flags;
3067
James Smart98c9ea52007-10-27 13:37:33 -04003068 if (ndlp) {
3069 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3070 "node get: did:x%x flg:x%x refcnt:x%x",
3071 ndlp->nlp_DID, ndlp->nlp_flag,
3072 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003073 /* The check of ndlp usage to prevent incrementing the
3074 * ndlp reference count that is in the process of being
3075 * released.
3076 */
3077 phba = ndlp->vport->phba;
3078 spin_lock_irqsave(&phba->ndlp_lock, flags);
3079 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
3080 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3081 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3082 "0276 lpfc_nlp_get: ndlp:x%p "
3083 "usgmap:x%x refcnt:%d\n",
3084 (void *)ndlp, ndlp->nlp_usg_map,
3085 atomic_read(&ndlp->kref.refcount));
3086 return NULL;
3087 } else
3088 kref_get(&ndlp->kref);
3089 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04003090 }
James Smart329f9bc2007-04-25 09:53:01 -04003091 return ndlp;
3092}
3093
James Smart98c9ea52007-10-27 13:37:33 -04003094/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05003095 * count goes to 0, this indicates the the associated nodelist should be
3096 * freed. Returning 1 indicates the ndlp resource has been released; on the
3097 * other hand, returning 0 indicates the ndlp resource has not been released
3098 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04003099 */
James Smart329f9bc2007-04-25 09:53:01 -04003100int
3101lpfc_nlp_put(struct lpfc_nodelist *ndlp)
3102{
James Smarte47c9092008-02-08 18:49:26 -05003103 struct lpfc_hba *phba;
3104 unsigned long flags;
3105
3106 if (!ndlp)
3107 return 1;
3108
3109 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3110 "node put: did:x%x flg:x%x refcnt:x%x",
3111 ndlp->nlp_DID, ndlp->nlp_flag,
3112 atomic_read(&ndlp->kref.refcount));
3113 phba = ndlp->vport->phba;
3114 spin_lock_irqsave(&phba->ndlp_lock, flags);
3115 /* Check the ndlp memory free acknowledge flag to avoid the
3116 * possible race condition that kref_put got invoked again
3117 * after previous one has done ndlp memory free.
3118 */
3119 if (NLP_CHK_FREE_ACK(ndlp)) {
3120 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3121 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3122 "0274 lpfc_nlp_put: ndlp:x%p "
3123 "usgmap:x%x refcnt:%d\n",
3124 (void *)ndlp, ndlp->nlp_usg_map,
3125 atomic_read(&ndlp->kref.refcount));
3126 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003127 }
James Smarte47c9092008-02-08 18:49:26 -05003128 /* Check the ndlp inactivate log flag to avoid the possible
3129 * race condition that kref_put got invoked again after ndlp
3130 * is already in inactivating state.
3131 */
3132 if (NLP_CHK_IACT_REQ(ndlp)) {
3133 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3134 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3135 "0275 lpfc_nlp_put: ndlp:x%p "
3136 "usgmap:x%x refcnt:%d\n",
3137 (void *)ndlp, ndlp->nlp_usg_map,
3138 atomic_read(&ndlp->kref.refcount));
3139 return 1;
3140 }
3141 /* For last put, mark the ndlp usage flags to make sure no
3142 * other kref_get and kref_put on the same ndlp shall get
3143 * in between the process when the final kref_put has been
3144 * invoked on this ndlp.
3145 */
3146 if (atomic_read(&ndlp->kref.refcount) == 1) {
3147 /* Indicate ndlp is put to inactive state. */
3148 NLP_SET_IACT_REQ(ndlp);
3149 /* Acknowledge ndlp memory free has been seen. */
3150 if (NLP_CHK_FREE_REQ(ndlp))
3151 NLP_SET_FREE_ACK(ndlp);
3152 }
3153 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3154 /* Note, the kref_put returns 1 when decrementing a reference
3155 * count that was 1, it invokes the release callback function,
3156 * but it still left the reference count as 1 (not actually
3157 * performs the last decrementation). Otherwise, it actually
3158 * decrements the reference count and returns 0.
3159 */
3160 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04003161}
James Smart98c9ea52007-10-27 13:37:33 -04003162
3163/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05003164 * by any other discovery thread. This routine returns 1 if the
3165 * ndlp has been freed. A return value of 0 indicates the ndlp is
3166 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04003167 */
3168int
3169lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
3170{
3171 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3172 "node not used: did:x%x flg:x%x refcnt:x%x",
3173 ndlp->nlp_DID, ndlp->nlp_flag,
3174 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003175 if (atomic_read(&ndlp->kref.refcount) == 1)
3176 if (lpfc_nlp_put(ndlp))
3177 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003178 return 0;
3179}