blob: 897ef7d7a8e9b52dabdf4a14560aa841ac1f2df2 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smarte47c9092008-02-08 18:49:26 -05004 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/kthread.h>
25#include <linux/interrupt.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_device.h>
29#include <scsi/scsi_host.h>
30#include <scsi/scsi_transport_fc.h>
31
32#include "lpfc_hw.h"
33#include "lpfc_disc.h"
34#include "lpfc_sli.h"
35#include "lpfc_scsi.h"
36#include "lpfc.h"
37#include "lpfc_logmsg.h"
38#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050039#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050040#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050041
42/* AlpaArray for assignment of scsid for scan-down and bind_method */
43static uint8_t lpfcAlpaArray[] = {
44 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
45 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
46 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
47 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
48 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
49 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
50 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
51 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
52 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
53 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
54 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
55 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
56 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
57};
58
James Smart2e0fef82007-06-17 19:56:36 -050059static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010060static void lpfc_disc_flush_list(struct lpfc_vport *vport);
dea31012005-04-17 16:05:31 -050061
James Smartc01f3202006-08-18 17:47:08 -040062void
63lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050064{
James Smartc01f3202006-08-18 17:47:08 -040065 struct lpfc_rport_data *rdata;
66 struct lpfc_nodelist * ndlp;
67 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050068
James Smartc01f3202006-08-18 17:47:08 -040069 rdata = rport->dd_data;
70 ndlp = rdata->pnode;
71
James Smart58da1ff2008-04-07 10:15:56 -040072 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040073 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
74 printk(KERN_ERR "Cannot find remote node"
75 " to terminate I/O Data x%x\n",
76 rport->port_id);
dea31012005-04-17 16:05:31 -050077 return;
78 }
79
James Smart2e0fef82007-06-17 19:56:36 -050080 phba = ndlp->vport->phba;
James Smart1a169682006-03-07 15:04:06 -050081
James Smart858c9f62007-06-17 19:56:39 -050082 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
83 "rport terminate: sid:x%x did:x%x flg:x%x",
84 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
85
James Smartc01f3202006-08-18 17:47:08 -040086 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040087 lpfc_sli_abort_iocb(ndlp->vport,
88 &phba->sli.ring[phba->sli.fcp_ring],
89 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040090 }
James Smartc01f3202006-08-18 17:47:08 -040091}
92
93/*
94 * This function will be called when dev_loss_tmo fire.
95 */
96void
97lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
98{
99 struct lpfc_rport_data *rdata;
100 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500101 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500102 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500103 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400104 int put_node;
105 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400106
107 rdata = rport->dd_data;
108 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400109 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400110 return;
James Smartc01f3202006-08-18 17:47:08 -0400111
James Smart858c9f62007-06-17 19:56:39 -0500112 vport = ndlp->vport;
113 phba = vport->phba;
114
115 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
116 "rport devlosscb: sid:x%x did:x%x flg:x%x",
117 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
118
James Smarta8adb832007-10-27 13:37:53 -0400119 /* Don't defer this if we are in the process of deleting the vport
120 * or unloading the driver. The unload will cleanup the node
121 * appropriately we just need to cleanup the ndlp rport info here.
122 */
123 if (vport->load_flag & FC_UNLOADING) {
124 put_node = rdata->pnode != NULL;
125 put_rport = ndlp->rport != NULL;
126 rdata->pnode = NULL;
127 ndlp->rport = NULL;
128 if (put_node)
129 lpfc_nlp_put(ndlp);
130 if (put_rport)
131 put_device(&rport->dev);
132 return;
133 }
134
135 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
136 return;
137
James Smart858c9f62007-06-17 19:56:39 -0500138 evtp = &ndlp->dev_loss_evt;
139
140 if (!list_empty(&evtp->evt_listp))
141 return;
142
143 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500144 /* We need to hold the node by incrementing the reference
145 * count until this queued work is done
146 */
147 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart5e9d9b82008-06-14 22:52:53 -0400148 if (evtp->evt_arg1) {
149 evtp->evt = LPFC_EVT_DEV_LOSS;
150 list_add_tail(&evtp->evt_listp, &phba->work_list);
151 lpfc_worker_wake_up(phba);
152 }
James Smart858c9f62007-06-17 19:56:39 -0500153 spin_unlock_irq(&phba->hbalock);
154
James Smart858c9f62007-06-17 19:56:39 -0500155 return;
156}
157
158/*
159 * This function is called from the worker thread when dev_loss_tmo
160 * expire.
161 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100162static void
James Smart858c9f62007-06-17 19:56:39 -0500163lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
164{
165 struct lpfc_rport_data *rdata;
166 struct fc_rport *rport;
167 struct lpfc_vport *vport;
168 struct lpfc_hba *phba;
169 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400170 int put_node;
171 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500172 int warn_on = 0;
173
174 rport = ndlp->rport;
175
176 if (!rport)
177 return;
178
179 rdata = rport->dd_data;
180 name = (uint8_t *) &ndlp->nlp_portname;
181 vport = ndlp->vport;
182 phba = vport->phba;
183
184 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
185 "rport devlosstmo:did:x%x type:x%x id:x%x",
186 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
187
James Smarta8adb832007-10-27 13:37:53 -0400188 /* Don't defer this if we are in the process of deleting the vport
189 * or unloading the driver. The unload will cleanup the node
190 * appropriately we just need to cleanup the ndlp rport info here.
191 */
192 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500193 if (ndlp->nlp_sid != NLP_NO_SID) {
194 /* flush the target */
195 lpfc_sli_abort_iocb(vport,
196 &phba->sli.ring[phba->sli.fcp_ring],
197 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
198 }
James Smarta8adb832007-10-27 13:37:53 -0400199 put_node = rdata->pnode != NULL;
200 put_rport = ndlp->rport != NULL;
201 rdata->pnode = NULL;
202 ndlp->rport = NULL;
203 if (put_node)
204 lpfc_nlp_put(ndlp);
205 if (put_rport)
206 put_device(&rport->dev);
207 return;
208 }
209
James Smartd7c255b2008-08-24 21:50:00 -0400210 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
211 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
212 "0284 Devloss timeout Ignored on "
213 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
214 "NPort x%x\n",
215 *name, *(name+1), *(name+2), *(name+3),
216 *(name+4), *(name+5), *(name+6), *(name+7),
217 ndlp->nlp_DID);
James Smart858c9f62007-06-17 19:56:39 -0500218 return;
James Smartd7c255b2008-08-24 21:50:00 -0400219 }
James Smart858c9f62007-06-17 19:56:39 -0500220
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 Smartc01f3202006-08-18 17:47:08 -0400240
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400241 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400242 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
243 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400244 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
245 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400246 *name, *(name+1), *(name+2), *(name+3),
247 *(name+4), *(name+5), *(name+6), *(name+7),
248 ndlp->nlp_DID, ndlp->nlp_flag,
249 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400250 } else {
James Smarte8b62012007-08-02 11:10:09 -0400251 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
252 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400253 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
254 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400255 *name, *(name+1), *(name+2), *(name+3),
256 *(name+4), *(name+5), *(name+6), *(name+7),
257 ndlp->nlp_DID, ndlp->nlp_flag,
258 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400259 }
260
James Smart87af33f2007-10-27 13:37:43 -0400261 put_node = rdata->pnode != NULL;
262 put_rport = ndlp->rport != NULL;
263 rdata->pnode = NULL;
264 ndlp->rport = NULL;
265 if (put_node)
266 lpfc_nlp_put(ndlp);
267 if (put_rport)
268 put_device(&rport->dev);
269
James Smart2e0fef82007-06-17 19:56:36 -0500270 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400271 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400272 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smarte47c9092008-02-08 18:49:26 -0500273 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
James Smart2e0fef82007-06-17 19:56:36 -0500274 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart92d7f7b2007-06-17 19:56:38 -0500275}
James Smartc01f3202006-08-18 17:47:08 -0400276
dea31012005-04-17 16:05:31 -0500277static void
James Smart2e0fef82007-06-17 19:56:36 -0500278lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500279{
280 struct lpfc_work_evt *evtp = NULL;
281 struct lpfc_nodelist *ndlp;
282 int free_evt;
283
James Smart2e0fef82007-06-17 19:56:36 -0500284 spin_lock_irq(&phba->hbalock);
285 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500286 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
287 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500288 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500289 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500290 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500291 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500292 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500293 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500294 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500295 /* decrement the node reference count held
296 * for this queued work
297 */
298 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500299 break;
James Smart858c9f62007-06-17 19:56:39 -0500300 case LPFC_EVT_DEV_LOSS:
301 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500302 lpfc_dev_loss_tmo_handler(ndlp);
303 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500304 /* decrement the node reference count held for
305 * this queued work
306 */
James Smart858c9f62007-06-17 19:56:39 -0500307 lpfc_nlp_put(ndlp);
308 break;
dea31012005-04-17 16:05:31 -0500309 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500310 if (phba->link_state < LPFC_LINK_DOWN)
311 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500312 else
James Smart2e0fef82007-06-17 19:56:36 -0500313 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500314 complete((struct completion *)(evtp->evt_arg2));
315 break;
James Smart46fa3112007-04-25 09:51:45 -0400316 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500317 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400318 lpfc_offline_prep(phba);
319 *(int *)(evtp->evt_arg1) = 0;
320 complete((struct completion *)(evtp->evt_arg2));
321 break;
322 case LPFC_EVT_OFFLINE:
323 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500324 lpfc_sli_brdrestart(phba);
325 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400326 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
327 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500328 complete((struct completion *)(evtp->evt_arg2));
329 break;
330 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400331 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500332 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500333 lpfc_sli_brdreset(phba);
334 lpfc_hba_down_post(phba);
335 *(int *)(evtp->evt_arg1) =
336 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400337 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_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400341 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500342 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500343 = (phba->pport->stopped)
344 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400345 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500346 complete((struct completion *)(evtp->evt_arg2));
347 break;
348 }
349 if (free_evt)
350 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500351 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500352 }
James Smart2e0fef82007-06-17 19:56:36 -0500353 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500354
355}
356
James Smart311464e2007-08-02 11:10:37 -0400357static void
James Smart2e0fef82007-06-17 19:56:36 -0500358lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500359{
360 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500361 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400362 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400363 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400364 int i;
dea31012005-04-17 16:05:31 -0500365
James Smart2e0fef82007-06-17 19:56:36 -0500366 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500367 ha_copy = phba->work_ha;
368 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500369 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500370
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500371 if (ha_copy & HA_ERATT)
dea31012005-04-17 16:05:31 -0500372 lpfc_handle_eratt(phba);
373
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500374 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500375 lpfc_sli_handle_mb_event(phba);
376
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500377 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500378 lpfc_handle_latt(phba);
James Smart549e55c2007-08-02 11:09:51 -0400379 vports = lpfc_create_vport_work_array(phba);
380 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500381 for(i = 0; i <= phba->max_vpi; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400382 /*
383 * We could have no vports in array if unloading, so if
384 * this happens then just use the pport
385 */
386 if (vports[i] == NULL && i == 0)
387 vport = phba->pport;
388 else
389 vport = vports[i];
390 if (vport == NULL)
391 break;
James Smart58da1ff2008-04-07 10:15:56 -0400392 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400393 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400394 vport->work_port_events &= ~work_port_events;
395 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400396 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400397 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400398 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400399 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400400 if (work_port_events & WORKER_HB_TMO)
401 lpfc_hb_timeout_handler(phba);
402 if (work_port_events & WORKER_MBOX_TMO)
403 lpfc_mbox_timeout_handler(phba);
404 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
405 lpfc_unblock_fabric_iocbs(phba);
406 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400407 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400408 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
409 lpfc_ramp_down_queue_handler(phba);
410 if (work_port_events & WORKER_RAMP_UP_QUEUE)
411 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500412 }
James Smart09372822008-01-11 01:52:54 -0500413 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500414
James Smart858c9f62007-06-17 19:56:39 -0500415 pring = &phba->sli.ring[LPFC_ELS_RING];
416 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
417 status >>= (4*LPFC_ELS_RING);
418 if ((status & HA_RXMASK)
419 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400420 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500421 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400422 /* Set the lpfc data pending flag */
423 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500424 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400425 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500426 lpfc_sli_handle_slow_ring_event(phba, pring,
427 (status &
428 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500429 }
430 /*
431 * Turn on Ring interrupts
432 */
433 spin_lock_irq(&phba->hbalock);
434 control = readl(phba->HCregaddr);
435 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
James Smarta58cbd52007-08-02 11:09:43 -0400436 lpfc_debugfs_slow_ring_trc(phba,
437 "WRK Enable ring: cntl:x%x hacopy:x%x",
438 control, ha_copy, 0);
439
James Smart858c9f62007-06-17 19:56:39 -0500440 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -0500441 writel(control, phba->HCregaddr);
442 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500443 }
James Smarta58cbd52007-08-02 11:09:43 -0400444 else {
445 lpfc_debugfs_slow_ring_trc(phba,
446 "WRK Ring ok: cntl:x%x hacopy:x%x",
447 control, ha_copy, 0);
448 }
James Smart858c9f62007-06-17 19:56:39 -0500449 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500450 }
James Smart2e0fef82007-06-17 19:56:36 -0500451 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500452}
453
dea31012005-04-17 16:05:31 -0500454int
455lpfc_do_work(void *p)
456{
457 struct lpfc_hba *phba = p;
458 int rc;
dea31012005-04-17 16:05:31 -0500459
460 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400461 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500462
463 while (1) {
James Smart5e9d9b82008-06-14 22:52:53 -0400464 /* wait and check worker queue activities */
465 rc = wait_event_interruptible(phba->work_waitq,
466 (test_and_clear_bit(LPFC_DATA_READY,
467 &phba->data_flags)
468 || kthread_should_stop()));
dea31012005-04-17 16:05:31 -0500469 BUG_ON(rc);
470
471 if (kthread_should_stop())
472 break;
473
James Smart5e9d9b82008-06-14 22:52:53 -0400474 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500475 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500476 }
dea31012005-04-17 16:05:31 -0500477 return 0;
478}
479
480/*
481 * This is only called to handle FC worker events. Since this a rare
482 * occurance, we allocate a struct lpfc_work_evt structure here instead of
483 * embedding it in the IOCB.
484 */
485int
James Smart2e0fef82007-06-17 19:56:36 -0500486lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500487 uint32_t evt)
488{
489 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500490 unsigned long flags;
dea31012005-04-17 16:05:31 -0500491
492 /*
493 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
494 * be queued to worker thread for processing
495 */
James Smart92d7f7b2007-06-17 19:56:38 -0500496 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500497 if (!evtp)
498 return 0;
499
500 evtp->evt_arg1 = arg1;
501 evtp->evt_arg2 = arg2;
502 evtp->evt = evt;
503
James Smarted957682007-06-17 19:56:37 -0500504 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400505 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500506 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500507
James Smart5e9d9b82008-06-14 22:52:53 -0400508 lpfc_worker_wake_up(phba);
509
dea31012005-04-17 16:05:31 -0500510 return 1;
511}
512
James Smart92d7f7b2007-06-17 19:56:38 -0500513void
514lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
515{
James Smart09372822008-01-11 01:52:54 -0500516 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500517 struct lpfc_hba *phba = vport->phba;
518 struct lpfc_nodelist *ndlp, *next_ndlp;
519 int rc;
520
521 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500522 if (!NLP_CHK_NODE_ACT(ndlp))
523 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500524 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
525 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400526 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
527 ((vport->port_type == LPFC_NPIV_PORT) &&
528 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500529 lpfc_unreg_rpi(vport, ndlp);
530
531 /* Leave Fabric nodes alone on link down */
532 if (!remove && ndlp->nlp_type & NLP_FABRIC)
533 continue;
534 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
535 remove
536 ? NLP_EVT_DEVICE_RM
537 : NLP_EVT_DEVICE_RECOVERY);
538 }
539 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
540 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500541 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500542 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500543 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500544 }
545}
546
James Smart87af33f2007-10-27 13:37:43 -0400547void
James Smart98c9ea52007-10-27 13:37:33 -0400548lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500549{
James Smart92d7f7b2007-06-17 19:56:38 -0500550 /* Cleanup any outstanding RSCN activity */
551 lpfc_els_flush_rscn(vport);
552
553 /* Cleanup any outstanding ELS commands */
554 lpfc_els_flush_cmd(vport);
555
556 lpfc_cleanup_rpis(vport, 0);
557
James Smart92d7f7b2007-06-17 19:56:38 -0500558 /* Turn off discovery timer if its running */
559 lpfc_can_disctmo(vport);
560}
561
James Smart98c9ea52007-10-27 13:37:33 -0400562static void
563lpfc_linkdown_port(struct lpfc_vport *vport)
564{
565 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
566
567 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
568
569 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
570 "Link Down: state:x%x rtry:x%x flg:x%x",
571 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
572
573 lpfc_port_link_failure(vport);
574
575}
576
dea31012005-04-17 16:05:31 -0500577int
James Smart685f0bf2007-04-25 09:53:08 -0400578lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500579{
James Smart2e0fef82007-06-17 19:56:36 -0500580 struct lpfc_vport *vport = phba->pport;
581 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400582 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400583 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400584 int i;
dea31012005-04-17 16:05:31 -0500585
James Smart3163f722008-02-08 18:50:25 -0500586 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500587 return 0;
James Smart2e0fef82007-06-17 19:56:36 -0500588 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500589 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500590 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500591 phba->pport->fc_flag &= ~FC_LBIT;
592 }
James Smart2e0fef82007-06-17 19:56:36 -0500593 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400594 vports = lpfc_create_vport_work_array(phba);
595 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500596 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400597 /* Issue a LINK DOWN event to all nodes */
598 lpfc_linkdown_port(vports[i]);
599 }
James Smart09372822008-01-11 01:52:54 -0500600 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500601 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500602 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
603 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500604 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500605 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500606 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400607 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500608 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500609 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500610 }
611 }
612
dea31012005-04-17 16:05:31 -0500613 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500614 if (phba->pport->fc_flag & FC_PT2PT) {
615 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500616 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
617 if (mb) {
dea31012005-04-17 16:05:31 -0500618 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500619 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500620 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400621 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500622 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500623 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500624 }
625 }
James Smart2e0fef82007-06-17 19:56:36 -0500626 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500627 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500628 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500629 }
James Smart2e0fef82007-06-17 19:56:36 -0500630
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500631 return 0;
dea31012005-04-17 16:05:31 -0500632}
633
James Smart92d7f7b2007-06-17 19:56:38 -0500634static void
635lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500636{
James Smart92d7f7b2007-06-17 19:56:38 -0500637 struct lpfc_nodelist *ndlp;
638
639 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500640 if (!NLP_CHK_NODE_ACT(ndlp))
641 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500642 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
643 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500644 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500645 /* On Linkup its safe to clean up the ndlp
646 * from Fabric connections.
647 */
James Smart92d7f7b2007-06-17 19:56:38 -0500648 if (ndlp->nlp_DID != Fabric_DID)
649 lpfc_unreg_rpi(vport, ndlp);
650 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
651 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500652 /* Fail outstanding IO now since device is
653 * marked for PLOGI.
654 */
James Smart92d7f7b2007-06-17 19:56:38 -0500655 lpfc_unreg_rpi(vport, ndlp);
656 }
657 }
658}
659
660static void
661lpfc_linkup_port(struct lpfc_vport *vport)
662{
663 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500664 struct lpfc_hba *phba = vport->phba;
665
666 if ((vport->load_flag & FC_UNLOADING) != 0)
667 return;
668
James Smart858c9f62007-06-17 19:56:39 -0500669 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
670 "Link Up: top:x%x speed:x%x flg:x%x",
671 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
672
James Smart92d7f7b2007-06-17 19:56:38 -0500673 /* If NPIV is not enabled, only bring the physical port up */
674 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
675 (vport != phba->pport))
676 return;
dea31012005-04-17 16:05:31 -0500677
James Smart2e0fef82007-06-17 19:56:36 -0500678 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400679
James Smart2e0fef82007-06-17 19:56:36 -0500680 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500681 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
682 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
683 vport->fc_flag |= FC_NDISC_ACTIVE;
684 vport->fc_ns_retry = 0;
685 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500686
James Smart92d7f7b2007-06-17 19:56:38 -0500687 if (vport->fc_flag & FC_LBIT)
688 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500689
James Smart92d7f7b2007-06-17 19:56:38 -0500690}
691
692static int
693lpfc_linkup(struct lpfc_hba *phba)
694{
James Smart549e55c2007-08-02 11:09:51 -0400695 struct lpfc_vport **vports;
696 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500697
698 phba->link_state = LPFC_LINK_UP;
699
700 /* Unblock fabric iocbs if they are blocked */
701 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
702 del_timer_sync(&phba->fabric_block_timer);
703
James Smart549e55c2007-08-02 11:09:51 -0400704 vports = lpfc_create_vport_work_array(phba);
705 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500706 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400707 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500708 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500709 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
710 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500711
712 return 0;
713}
714
715/*
716 * This routine handles processing a CLEAR_LA mailbox
717 * command upon completion. It is setup in the LPFC_MBOXQ
718 * as the completion routine when the command is
719 * handed off to the SLI layer.
720 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100721static void
James Smart2e0fef82007-06-17 19:56:36 -0500722lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500723{
James Smart2e0fef82007-06-17 19:56:36 -0500724 struct lpfc_vport *vport = pmb->vport;
725 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
726 struct lpfc_sli *psli = &phba->sli;
727 MAILBOX_t *mb = &pmb->mb;
dea31012005-04-17 16:05:31 -0500728 uint32_t control;
729
dea31012005-04-17 16:05:31 -0500730 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500731 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500732 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
733 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
734
735 /* Check for error */
736 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500737 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400738 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
739 "0320 CLEAR_LA mbxStatus error x%x hba "
740 "state x%x\n",
741 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500742 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500743 goto out;
744 }
745
James Smart92d7f7b2007-06-17 19:56:38 -0500746 if (vport->port_type == LPFC_PHYSICAL_PORT)
747 phba->link_state = LPFC_HBA_READY;
748
749 spin_lock_irq(&phba->hbalock);
750 psli->sli_flag |= LPFC_PROCESS_LA;
751 control = readl(phba->HCregaddr);
752 control |= HC_LAINT_ENA;
753 writel(control, phba->HCregaddr);
754 readl(phba->HCregaddr); /* flush */
755 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -0500756 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500757 return;
dea31012005-04-17 16:05:31 -0500758
dea31012005-04-17 16:05:31 -0500759out:
760 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400761 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
762 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500763 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500764
James Smart2e0fef82007-06-17 19:56:36 -0500765 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -0400766 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -0500767 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500768
James Smart2e0fef82007-06-17 19:56:36 -0500769 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500770
771 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500772
773 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500774 psli->sli_flag |= LPFC_PROCESS_LA;
775 control = readl(phba->HCregaddr);
776 control |= HC_LAINT_ENA;
777 writel(control, phba->HCregaddr);
778 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500779 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500780
781 return;
782}
783
James Smart2e0fef82007-06-17 19:56:36 -0500784
dea31012005-04-17 16:05:31 -0500785static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500786lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500787{
James Smart2e0fef82007-06-17 19:56:36 -0500788 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500789
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500790 if (pmb->mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500791 goto out;
dea31012005-04-17 16:05:31 -0500792
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500793 mempool_free(pmb, phba->mbox_mem_pool);
794
795 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500796 vport->fc_flag & FC_PUBLIC_LOOP &&
797 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500798 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500799 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500800 * LPFC_LOCAL_CFG_LINK while waiting for FAN
801 */
James Smart2e0fef82007-06-17 19:56:36 -0500802 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500803 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500804 }
dea31012005-04-17 16:05:31 -0500805
James Smart2e0fef82007-06-17 19:56:36 -0500806 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500807 * LPFC_FLOGI while waiting for FLOGI cmpl
808 */
James Smart92d7f7b2007-06-17 19:56:38 -0500809 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500810 lpfc_initial_flogi(vport);
811 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500812 return;
dea31012005-04-17 16:05:31 -0500813
814out:
James Smarte8b62012007-08-02 11:10:09 -0400815 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
816 "0306 CONFIG_LINK mbxStatus error x%x "
817 "HBA state x%x\n",
818 pmb->mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500819 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500820
821 lpfc_linkdown(phba);
822
James Smarte8b62012007-08-02 11:10:09 -0400823 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
824 "0200 CONFIG_LINK bad hba state x%x\n",
825 vport->port_state);
dea31012005-04-17 16:05:31 -0500826
James Smart92d7f7b2007-06-17 19:56:38 -0500827 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500828 return;
829}
830
831static void
James Smart2e0fef82007-06-17 19:56:36 -0500832lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500833{
dea31012005-04-17 16:05:31 -0500834 MAILBOX_t *mb = &pmb->mb;
835 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500836 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500837
838
839 /* Check for error */
840 if (mb->mbxStatus) {
841 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400842 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
843 "0319 READ_SPARAM mbxStatus error x%x "
844 "hba state x%x>\n",
845 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -0500846 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -0500847 goto out;
848 }
849
James Smart2e0fef82007-06-17 19:56:36 -0500850 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -0500851 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -0500852 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500853 u64_to_wwn(phba->cfg_soft_wwnn,
854 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400855 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500856 u64_to_wwn(phba->cfg_soft_wwpn,
857 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -0500858 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
859 sizeof(vport->fc_nodename));
860 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
861 sizeof(vport->fc_portname));
862 if (vport->port_type == LPFC_PHYSICAL_PORT) {
863 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
864 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
865 }
866
dea31012005-04-17 16:05:31 -0500867 lpfc_mbuf_free(phba, mp->virt, mp->phys);
868 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -0500869 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500870 return;
871
872out:
873 pmb->context1 = NULL;
874 lpfc_mbuf_free(phba, mp->virt, mp->phys);
875 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -0500876 lpfc_issue_clear_la(phba, vport);
877 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500878 return;
879}
880
881static void
James Smart92d7f7b2007-06-17 19:56:38 -0500882lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -0500883{
James Smart92d7f7b2007-06-17 19:56:38 -0500884 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500885 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
James Smart2e0fef82007-06-17 19:56:36 -0500886 int i;
James Smart14691152006-12-02 13:34:28 -0500887 struct lpfc_dmabuf *mp;
888 int rc;
889
dea31012005-04-17 16:05:31 -0500890 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
891 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
892
James Smart92d7f7b2007-06-17 19:56:38 -0500893 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500894 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -0500895 case LA_1GHZ_LINK:
896 phba->fc_linkspeed = LA_1GHZ_LINK;
897 break;
898 case LA_2GHZ_LINK:
899 phba->fc_linkspeed = LA_2GHZ_LINK;
900 break;
901 case LA_4GHZ_LINK:
902 phba->fc_linkspeed = LA_4GHZ_LINK;
903 break;
904 case LA_8GHZ_LINK:
905 phba->fc_linkspeed = LA_8GHZ_LINK;
906 break;
907 default:
908 phba->fc_linkspeed = LA_UNKNW_LINK;
909 break;
dea31012005-04-17 16:05:31 -0500910 }
911
912 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -0500913 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -0500914
915 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -0500916 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -0500917
James Smart495a7142008-06-14 22:52:59 -0400918 if (phba->cfg_enable_npiv)
919 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
920 "1309 Link Up Event npiv not supported in loop "
921 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500922 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -0500923 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -0500924 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500925
James Smart2e0fef82007-06-17 19:56:36 -0500926 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -0500927 i = la->un.lilpBde64.tus.f.bdeSize;
928
929 if (i == 0) {
930 phba->alpa_map[0] = 0;
931 } else {
James Smarte8b62012007-08-02 11:10:09 -0400932 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -0500933 int numalpa, j, k;
934 union {
935 uint8_t pamap[16];
936 struct {
937 uint32_t wd1;
938 uint32_t wd2;
939 uint32_t wd3;
940 uint32_t wd4;
941 } pa;
942 } un;
943 numalpa = phba->alpa_map[0];
944 j = 0;
945 while (j < numalpa) {
946 memset(un.pamap, 0, 16);
947 for (k = 1; j < numalpa; k++) {
948 un.pamap[k - 1] =
949 phba->alpa_map[j + 1];
950 j++;
951 if (k == 16)
952 break;
953 }
954 /* Link Up Event ALPA map */
955 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -0500956 KERN_WARNING,
957 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400958 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -0500959 "ALPA map Data: x%x "
960 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500961 un.pa.wd1, un.pa.wd2,
962 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -0500963 }
964 }
965 }
966 } else {
James Smart92d7f7b2007-06-17 19:56:38 -0500967 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -0400968 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -0500969 (phba->sli_rev == 3))
970 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
971 }
James Smart2e0fef82007-06-17 19:56:36 -0500972 vport->fc_myDID = phba->fc_pref_DID;
973 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500974 }
James Smart92d7f7b2007-06-17 19:56:38 -0500975 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500976
977 lpfc_linkup(phba);
978 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -0500979 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -0500980 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -0500981 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -0400982 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -0500983 if (rc == MBX_NOT_FINISHED) {
984 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
985 lpfc_mbuf_free(phba, mp->virt, mp->phys);
986 kfree(mp);
987 mempool_free(sparam_mbox, phba->mbox_mem_pool);
988 if (cfglink_mbox)
989 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500990 goto out;
James Smart14691152006-12-02 13:34:28 -0500991 }
dea31012005-04-17 16:05:31 -0500992 }
993
994 if (cfglink_mbox) {
James Smart2e0fef82007-06-17 19:56:36 -0500995 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -0500996 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -0500997 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500998 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -0400999 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001000 if (rc != MBX_NOT_FINISHED)
1001 return;
1002 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001003 }
James Smart92d7f7b2007-06-17 19:56:38 -05001004out:
1005 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001006 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1007 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1008 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05001009 lpfc_issue_clear_la(phba, vport);
1010 return;
dea31012005-04-17 16:05:31 -05001011}
1012
1013static void
James Smart84774a42008-08-24 21:50:06 -04001014lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05001015{
dea31012005-04-17 16:05:31 -05001016 uint32_t control;
1017 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05001018 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001019 psli->sli_flag |= LPFC_PROCESS_LA;
1020 control = readl(phba->HCregaddr);
1021 control |= HC_LAINT_ENA;
1022 writel(control, phba->HCregaddr);
1023 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001024 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001025}
1026
James Smart84774a42008-08-24 21:50:06 -04001027static void
1028lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1029{
1030 lpfc_linkdown(phba);
1031 lpfc_enable_la(phba);
1032 /* turn on Link Attention interrupts - no CLEAR_LA needed */
1033}
1034
1035
dea31012005-04-17 16:05:31 -05001036/*
1037 * This routine handles processing a READ_LA mailbox
1038 * command upon completion. It is setup in the LPFC_MBOXQ
1039 * as the completion routine when the command is
1040 * handed off to the SLI layer.
1041 */
1042void
James Smart2e0fef82007-06-17 19:56:36 -05001043lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001044{
James Smart2e0fef82007-06-17 19:56:36 -05001045 struct lpfc_vport *vport = pmb->vport;
1046 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05001047 READ_LA_VAR *la;
1048 MAILBOX_t *mb = &pmb->mb;
1049 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1050
James Smart0d2b6b82008-06-14 22:52:47 -04001051 /* Unblock ELS traffic */
1052 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001053 /* Check for error */
1054 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05001055 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001056 "1307 READ_LA mbox error x%x state x%x\n",
1057 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001058 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001059 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001060 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1061 }
1062
1063 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1064
1065 memcpy(&phba->alpa_map[0], mp->virt, 128);
1066
James Smart2e0fef82007-06-17 19:56:36 -05001067 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001068 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05001069 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001070 else
James Smart2e0fef82007-06-17 19:56:36 -05001071 vport->fc_flag &= ~FC_BYPASSED_MODE;
1072 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001073
dea31012005-04-17 16:05:31 -05001074 if (((phba->fc_eventTag + 1) < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05001075 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05001076 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05001077 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05001078 if (phba->fc_eventTag != 0)
1079 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001080 }
dea31012005-04-17 16:05:31 -05001081
1082 phba->fc_eventTag = la->eventTag;
James Smart84774a42008-08-24 21:50:06 -04001083 if (la->mm)
1084 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
1085 else
1086 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
dea31012005-04-17 16:05:31 -05001087
James Smart84774a42008-08-24 21:50:06 -04001088 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05001089 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05001090 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05001091 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001092 "1306 Link Up Event in loop back mode "
1093 "x%x received Data: x%x x%x x%x x%x\n",
1094 la->eventTag, phba->fc_eventTag,
1095 la->granted_AL_PA, la->UlnkSpeed,
1096 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001097 } else {
1098 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001099 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04001100 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001101 la->eventTag, phba->fc_eventTag,
1102 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04001103 phba->alpa_map[0],
1104 la->mm, la->fa,
1105 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04001106 }
James Smart92d7f7b2007-06-17 19:56:38 -05001107 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04001108 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05001109 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05001110 if (phba->link_flag & LS_LOOPBACK_MODE) {
1111 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1112 "1308 Link Down Event in loop back mode "
1113 "x%x received "
1114 "Data: x%x x%x x%x\n",
1115 la->eventTag, phba->fc_eventTag,
1116 phba->pport->port_state, vport->fc_flag);
1117 }
1118 else {
1119 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001120 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04001121 "Data: x%x x%x x%x x%x x%x\n",
1122 la->eventTag, phba->fc_eventTag,
1123 phba->pport->port_state, vport->fc_flag,
1124 la->mm, la->fa);
1125 }
1126 lpfc_mbx_issue_link_down(phba);
1127 }
1128 if (la->mm && la->attType == AT_LINK_UP) {
1129 if (phba->link_state != LPFC_LINK_DOWN) {
1130 phba->fc_stat.LinkDown++;
1131 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1132 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05001133 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001134 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05001135 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04001136 lpfc_mbx_issue_link_down(phba);
1137 } else
1138 lpfc_enable_la(phba);
1139
1140 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1141 "1310 Menlo Maint Mode Link up Event x%x rcvd "
1142 "Data: x%x x%x x%x\n",
1143 la->eventTag, phba->fc_eventTag,
1144 phba->pport->port_state, vport->fc_flag);
1145 /*
1146 * The cmnd that triggered this will be waiting for this
1147 * signal.
1148 */
1149 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
1150 if (phba->wait_4_mlo_maint_flg) {
1151 phba->wait_4_mlo_maint_flg = 0;
1152 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05001153 }
James Smart84774a42008-08-24 21:50:06 -04001154 }
1155
1156 if (la->fa) {
1157 if (la->mm)
1158 lpfc_issue_clear_la(phba, vport);
1159 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1160 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05001161 }
1162
1163lpfc_mbx_cmpl_read_la_free_mbuf:
1164 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1165 kfree(mp);
1166 mempool_free(pmb, phba->mbox_mem_pool);
1167 return;
1168}
1169
1170/*
1171 * This routine handles processing a REG_LOGIN mailbox
1172 * command upon completion. It is setup in the LPFC_MBOXQ
1173 * as the completion routine when the command is
1174 * handed off to the SLI layer.
1175 */
1176void
James Smart2e0fef82007-06-17 19:56:36 -05001177lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001178{
James Smart2e0fef82007-06-17 19:56:36 -05001179 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001180 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05001181 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001182
dea31012005-04-17 16:05:31 -05001183 pmb->context1 = NULL;
1184
1185 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05001186 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05001187 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1188 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001189 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05001190 /* decrement the node reference count held for this callback
1191 * function.
1192 */
James Smart329f9bc2007-04-25 09:53:01 -04001193 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001194
1195 return;
1196}
1197
James Smart92d7f7b2007-06-17 19:56:38 -05001198static void
1199lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1200{
1201 MAILBOX_t *mb = &pmb->mb;
1202 struct lpfc_vport *vport = pmb->vport;
1203 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1204
1205 switch (mb->mbxStatus) {
1206 case 0x0011:
1207 case 0x0020:
1208 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04001209 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1210 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
1211 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001212 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001213 }
1214 vport->unreg_vpi_cmpl = VPORT_OK;
1215 mempool_free(pmb, phba->mbox_mem_pool);
1216 /*
1217 * This shost reference might have been taken at the beginning of
1218 * lpfc_vport_delete()
1219 */
1220 if (vport->load_flag & FC_UNLOADING)
1221 scsi_host_put(shost);
1222}
1223
James Smartd7c255b2008-08-24 21:50:00 -04001224int
James Smart92d7f7b2007-06-17 19:56:38 -05001225lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1226{
1227 struct lpfc_hba *phba = vport->phba;
1228 LPFC_MBOXQ_t *mbox;
1229 int rc;
1230
1231 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1232 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04001233 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05001234
1235 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1236 mbox->vport = vport;
1237 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04001238 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001239 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001240 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1241 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001242 mempool_free(mbox, phba->mbox_mem_pool);
1243 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04001244 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05001245 }
James Smartd7c255b2008-08-24 21:50:00 -04001246 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001247}
1248
1249static void
1250lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1251{
1252 struct lpfc_vport *vport = pmb->vport;
1253 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1254 MAILBOX_t *mb = &pmb->mb;
1255
1256 switch (mb->mbxStatus) {
1257 case 0x0011:
1258 case 0x9601:
1259 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04001260 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1261 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
1262 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001263 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1264 spin_lock_irq(shost->host_lock);
1265 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1266 spin_unlock_irq(shost->host_lock);
1267 vport->fc_myDID = 0;
1268 goto out;
1269 }
James Smart92d7f7b2007-06-17 19:56:38 -05001270
1271 vport->num_disc_nodes = 0;
1272 /* go thru NPR list and issue ELS PLOGIs */
1273 if (vport->fc_npr_cnt)
1274 lpfc_els_disc_plogi(vport);
1275
1276 if (!vport->num_disc_nodes) {
1277 spin_lock_irq(shost->host_lock);
1278 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1279 spin_unlock_irq(shost->host_lock);
1280 lpfc_can_disctmo(vport);
1281 }
1282 vport->port_state = LPFC_VPORT_READY;
1283
1284out:
1285 mempool_free(pmb, phba->mbox_mem_pool);
1286 return;
1287}
1288
dea31012005-04-17 16:05:31 -05001289/*
1290 * This routine handles processing a Fabric REG_LOGIN mailbox
1291 * command upon completion. It is setup in the LPFC_MBOXQ
1292 * as the completion routine when the command is
1293 * handed off to the SLI layer.
1294 */
1295void
James Smart2e0fef82007-06-17 19:56:36 -05001296lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001297{
James Smart92d7f7b2007-06-17 19:56:38 -05001298 struct lpfc_vport *vport = pmb->vport;
James Smart2e0fef82007-06-17 19:56:36 -05001299 MAILBOX_t *mb = &pmb->mb;
1300 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05001301 struct lpfc_nodelist *ndlp;
James Smart549e55c2007-08-02 11:09:51 -04001302 struct lpfc_vport **vports;
1303 int i;
dea31012005-04-17 16:05:31 -05001304
James Smart549e55c2007-08-02 11:09:51 -04001305 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04001306 pmb->context1 = NULL;
1307 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05001308 if (mb->mbxStatus) {
1309 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1310 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001311 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001312
James Smart92d7f7b2007-06-17 19:56:38 -05001313 if (phba->fc_topology == TOPOLOGY_LOOP) {
1314 /* FLOGI failed, use loop map to make discovery list */
1315 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001316
James Smart92d7f7b2007-06-17 19:56:38 -05001317 /* Start discovery */
1318 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05001319 /* Decrement the reference count to ndlp after the
1320 * reference to the ndlp are done.
1321 */
1322 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05001323 return;
1324 }
1325
1326 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001327 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1328 "0258 Register Fabric login error: 0x%x\n",
1329 mb->mbxStatus);
James Smarte47c9092008-02-08 18:49:26 -05001330 /* Decrement the reference count to ndlp after the reference
1331 * to the ndlp are done.
1332 */
1333 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001334 return;
1335 }
1336
dea31012005-04-17 16:05:31 -05001337 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001338 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001339 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001340
James Smart2e0fef82007-06-17 19:56:36 -05001341 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart549e55c2007-08-02 11:09:51 -04001342 vports = lpfc_create_vport_work_array(phba);
1343 if (vports != NULL)
1344 for(i = 0;
James Smart09372822008-01-11 01:52:54 -05001345 i <= phba->max_vpi && vports[i] != NULL;
James Smart549e55c2007-08-02 11:09:51 -04001346 i++) {
1347 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1348 continue;
James Smart58da1ff2008-04-07 10:15:56 -04001349 if (phba->fc_topology == TOPOLOGY_LOOP) {
1350 lpfc_vport_set_state(vports[i],
1351 FC_VPORT_LINKDOWN);
1352 continue;
1353 }
James Smart549e55c2007-08-02 11:09:51 -04001354 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1355 lpfc_initial_fdisc(vports[i]);
James Smart58da1ff2008-04-07 10:15:56 -04001356 else {
James Smart549e55c2007-08-02 11:09:51 -04001357 lpfc_vport_set_state(vports[i],
1358 FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -04001359 lpfc_printf_vlog(vport, KERN_ERR,
1360 LOG_ELS,
1361 "0259 No NPIV "
1362 "Fabric support\n");
James Smart549e55c2007-08-02 11:09:51 -04001363 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001364 }
James Smart09372822008-01-11 01:52:54 -05001365 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001366 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05001367 }
1368
1369 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1370 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001371 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05001372
1373 /* Drop the reference count from the mbox at the end after
1374 * all the current reference to the ndlp have been done.
1375 */
1376 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001377 return;
1378}
1379
1380/*
1381 * This routine handles processing a NameServer REG_LOGIN mailbox
1382 * command upon completion. It is setup in the LPFC_MBOXQ
1383 * as the completion routine when the command is
1384 * handed off to the SLI layer.
1385 */
1386void
James Smart2e0fef82007-06-17 19:56:36 -05001387lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001388{
James Smart2e0fef82007-06-17 19:56:36 -05001389 MAILBOX_t *mb = &pmb->mb;
1390 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1391 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1392 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001393
1394 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05001395out:
James Smartfa4066b2008-01-11 01:53:27 -05001396 /* decrement the node reference count held for this
1397 * callback function.
1398 */
James Smart329f9bc2007-04-25 09:53:01 -04001399 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001400 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1401 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04001402 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04001403
1404 /* If no other thread is using the ndlp, free it */
1405 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05001406
James Smart92d7f7b2007-06-17 19:56:38 -05001407 if (phba->fc_topology == TOPOLOGY_LOOP) {
1408 /*
1409 * RegLogin failed, use loop map to make discovery
1410 * list
1411 */
1412 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001413
James Smart92d7f7b2007-06-17 19:56:38 -05001414 /* Start discovery */
1415 lpfc_disc_start(vport);
1416 return;
1417 }
1418 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001419 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1420 "0260 Register NameServer error: 0x%x\n",
1421 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001422 return;
1423 }
1424
1425 pmb->context1 = NULL;
1426
dea31012005-04-17 16:05:31 -05001427 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001428 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001429 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001430
James Smart2e0fef82007-06-17 19:56:36 -05001431 if (vport->port_state < LPFC_VPORT_READY) {
1432 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05001433 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1434 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1435 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1436 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1437 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1438
1439 /* Issue SCR just before NameServer GID_FT Query */
1440 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05001441 }
1442
James Smart2e0fef82007-06-17 19:56:36 -05001443 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05001444 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05001445 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05001446 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05001447 goto out;
dea31012005-04-17 16:05:31 -05001448 }
1449
James Smartfa4066b2008-01-11 01:53:27 -05001450 /* decrement the node reference count held for this
1451 * callback function.
1452 */
James Smart329f9bc2007-04-25 09:53:01 -04001453 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001454 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1455 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001456 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001457
1458 return;
1459}
1460
1461static void
James Smart2e0fef82007-06-17 19:56:36 -05001462lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001463{
James Smart2e0fef82007-06-17 19:56:36 -05001464 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1465 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05001466 struct lpfc_rport_data *rdata;
1467 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05001468 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001469
1470 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001471 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1472 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001473 rport_ids.port_id = ndlp->nlp_DID;
1474 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05001475
James Smart329f9bc2007-04-25 09:53:01 -04001476 /*
1477 * We leave our node pointer in rport->dd_data when we unregister a
1478 * FCP target port. But fc_remote_port_add zeros the space to which
1479 * rport->dd_data points. So, if we're reusing a previously
1480 * registered port, drop the reference that we took the last time we
1481 * registered the port.
1482 */
1483 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05001484 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04001485 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05001486
1487 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1488 "rport add: did:x%x flg:x%x type x%x",
1489 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1490
James Smart2e0fef82007-06-17 19:56:36 -05001491 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04001492 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05001493 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1494 "Warning: fc_remote_port_add failed\n");
1495 return;
1496 }
1497
1498 /* initialize static port data */
1499 rport->maxframe_size = ndlp->nlp_maxframe;
1500 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05001501 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04001502 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001503
1504 if (ndlp->nlp_type & NLP_FCP_TARGET)
1505 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1506 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1507 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1508
1509
1510 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1511 fc_remote_port_rolechg(rport, rport_ids.roles);
1512
James Smart071fbd3d2006-04-15 11:53:20 -04001513 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001514 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04001515 ndlp->nlp_sid = rport->scsi_target_id;
1516 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001517 return;
1518}
1519
1520static void
James Smart2e0fef82007-06-17 19:56:36 -05001521lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001522{
1523 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04001524
James Smart858c9f62007-06-17 19:56:39 -05001525 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
1526 "rport delete: did:x%x flg:x%x type x%x",
1527 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1528
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001529 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05001530
1531 return;
1532}
1533
James Smartde0c5b32007-04-25 09:52:27 -04001534static void
James Smart2e0fef82007-06-17 19:56:36 -05001535lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04001536{
James Smart2e0fef82007-06-17 19:56:36 -05001537 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1538
1539 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001540 switch (state) {
1541 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001542 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001543 break;
1544 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001545 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001546 break;
1547 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001548 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001549 break;
1550 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001551 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001552 break;
1553 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001554 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001555 break;
1556 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001557 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001558 break;
1559 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001560 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001561 break;
1562 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001563 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001564 break;
1565 }
James Smart2e0fef82007-06-17 19:56:36 -05001566 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001567}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001568
James Smartde0c5b32007-04-25 09:52:27 -04001569static void
James Smart2e0fef82007-06-17 19:56:36 -05001570lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04001571 int old_state, int new_state)
1572{
James Smart2e0fef82007-06-17 19:56:36 -05001573 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1574
James Smartde0c5b32007-04-25 09:52:27 -04001575 if (new_state == NLP_STE_UNMAPPED_NODE) {
1576 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1577 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1578 ndlp->nlp_type |= NLP_FC_NODE;
1579 }
1580 if (new_state == NLP_STE_MAPPED_NODE)
1581 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1582 if (new_state == NLP_STE_NPR_NODE)
1583 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05001584
James Smartde0c5b32007-04-25 09:52:27 -04001585 /* Transport interface */
1586 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1587 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001588 vport->phba->nport_event_cnt++;
1589 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001590 }
dea31012005-04-17 16:05:31 -05001591
James Smartde0c5b32007-04-25 09:52:27 -04001592 if (new_state == NLP_STE_MAPPED_NODE ||
1593 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05001594 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05001595 /*
1596 * Tell the fc transport about the port, if we haven't
1597 * already. If we have, and it's a scsi entity, be
1598 * sure to unblock any attached scsi devices
1599 */
1600 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001601 }
James Smart858c9f62007-06-17 19:56:39 -05001602 /*
1603 * if we added to Mapped list, but the remote port
1604 * registration failed or assigned a target id outside
1605 * our presentable range - move the node to the
1606 * Unmapped List
1607 */
James Smartde0c5b32007-04-25 09:52:27 -04001608 if (new_state == NLP_STE_MAPPED_NODE &&
1609 (!ndlp->rport ||
1610 ndlp->rport->scsi_target_id == -1 ||
1611 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05001612 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001613 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05001614 spin_unlock_irq(shost->host_lock);
1615 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001616 }
James Smartde0c5b32007-04-25 09:52:27 -04001617}
1618
James Smart685f0bf2007-04-25 09:53:08 -04001619static char *
1620lpfc_nlp_state_name(char *buffer, size_t size, int state)
1621{
1622 static char *states[] = {
1623 [NLP_STE_UNUSED_NODE] = "UNUSED",
1624 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1625 [NLP_STE_ADISC_ISSUE] = "ADISC",
1626 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1627 [NLP_STE_PRLI_ISSUE] = "PRLI",
1628 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1629 [NLP_STE_MAPPED_NODE] = "MAPPED",
1630 [NLP_STE_NPR_NODE] = "NPR",
1631 };
1632
James Smart311464e2007-08-02 11:10:37 -04001633 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04001634 strlcpy(buffer, states[state], size);
1635 else
1636 snprintf(buffer, size, "unknown (%d)", state);
1637 return buffer;
1638}
1639
James Smartde0c5b32007-04-25 09:52:27 -04001640void
James Smart2e0fef82007-06-17 19:56:36 -05001641lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1642 int state)
James Smartde0c5b32007-04-25 09:52:27 -04001643{
James Smart2e0fef82007-06-17 19:56:36 -05001644 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04001645 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04001646 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04001647
James Smarte8b62012007-08-02 11:10:09 -04001648 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1649 "0904 NPort state transition x%06x, %s -> %s\n",
1650 ndlp->nlp_DID,
1651 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1652 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05001653
1654 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1655 "node statechg did:x%x old:%d ste:%d",
1656 ndlp->nlp_DID, old_state, state);
1657
James Smartde0c5b32007-04-25 09:52:27 -04001658 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04001659 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001660 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001661 if (old_state == NLP_STE_UNMAPPED_NODE) {
1662 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1663 ndlp->nlp_type &= ~NLP_FC_NODE;
1664 }
1665
James Smart685f0bf2007-04-25 09:53:08 -04001666 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05001667 spin_lock_irq(shost->host_lock);
1668 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1669 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001670 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05001671 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04001672
1673 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05001674 lpfc_nlp_counters(vport, state, 1);
1675 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04001676}
1677
1678void
James Smarte47c9092008-02-08 18:49:26 -05001679lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1680{
1681 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1682
1683 if (list_empty(&ndlp->nlp_listp)) {
1684 spin_lock_irq(shost->host_lock);
1685 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1686 spin_unlock_irq(shost->host_lock);
1687 }
1688}
1689
1690void
James Smart2e0fef82007-06-17 19:56:36 -05001691lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001692{
James Smart2e0fef82007-06-17 19:56:36 -05001693 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1694
James Smart0d2b6b82008-06-14 22:52:47 -04001695 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001696 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05001697 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1698 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001699 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05001700 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05001701 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05001702 NLP_STE_UNUSED_NODE);
1703}
1704
Adrian Bunk4d9db012008-02-14 23:24:02 +02001705static void
James Smarte47c9092008-02-08 18:49:26 -05001706lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1707{
James Smart0d2b6b82008-06-14 22:52:47 -04001708 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05001709 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1710 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1711 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
1712 NLP_STE_UNUSED_NODE);
1713}
1714
1715struct lpfc_nodelist *
1716lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1717 int state)
1718{
1719 struct lpfc_hba *phba = vport->phba;
1720 uint32_t did;
1721 unsigned long flags;
1722
1723 if (!ndlp)
1724 return NULL;
1725
1726 spin_lock_irqsave(&phba->ndlp_lock, flags);
1727 /* The ndlp should not be in memory free mode */
1728 if (NLP_CHK_FREE_REQ(ndlp)) {
1729 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1730 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1731 "0277 lpfc_enable_node: ndlp:x%p "
1732 "usgmap:x%x refcnt:%d\n",
1733 (void *)ndlp, ndlp->nlp_usg_map,
1734 atomic_read(&ndlp->kref.refcount));
1735 return NULL;
1736 }
1737 /* The ndlp should not already be in active mode */
1738 if (NLP_CHK_NODE_ACT(ndlp)) {
1739 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1740 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1741 "0278 lpfc_enable_node: ndlp:x%p "
1742 "usgmap:x%x refcnt:%d\n",
1743 (void *)ndlp, ndlp->nlp_usg_map,
1744 atomic_read(&ndlp->kref.refcount));
1745 return NULL;
1746 }
1747
1748 /* Keep the original DID */
1749 did = ndlp->nlp_DID;
1750
1751 /* re-initialize ndlp except of ndlp linked list pointer */
1752 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
1753 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
1754 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
1755 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
1756 init_timer(&ndlp->nlp_delayfunc);
1757 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
1758 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
1759 ndlp->nlp_DID = did;
1760 ndlp->vport = vport;
1761 ndlp->nlp_sid = NLP_NO_SID;
1762 /* ndlp management re-initialize */
1763 kref_init(&ndlp->kref);
1764 NLP_INT_NODE_ACT(ndlp);
1765
1766 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1767
1768 if (state != NLP_STE_UNUSED_NODE)
1769 lpfc_nlp_set_state(vport, ndlp, state);
1770
1771 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1772 "node enable: did:x%x",
1773 ndlp->nlp_DID, 0, 0);
1774 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04001775}
1776
1777void
James Smart2e0fef82007-06-17 19:56:36 -05001778lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001779{
James Smart87af33f2007-10-27 13:37:43 -04001780 /*
James Smartfa4066b2008-01-11 01:53:27 -05001781 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04001782 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05001783 * the ndlp from the vport. The ndlp marked as UNUSED on the list
1784 * until ALL other outstanding threads have completed. We check
1785 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04001786 */
James Smartfa4066b2008-01-11 01:53:27 -05001787 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1788 return;
James Smart51ef4c22007-08-02 11:10:31 -04001789 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04001790 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04001791 return;
dea31012005-04-17 16:05:31 -05001792}
1793
1794/*
1795 * Start / ReStart rescue timer for Discovery / RSCN handling
1796 */
1797void
James Smart2e0fef82007-06-17 19:56:36 -05001798lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001799{
James Smart2e0fef82007-06-17 19:56:36 -05001800 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1801 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001802 uint32_t tmo;
1803
James Smart2e0fef82007-06-17 19:56:36 -05001804 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001805 /* For FAN, timeout should be greater then edtov */
1806 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1807 } else {
1808 /* Normal discovery timeout should be > then ELS/CT timeout
1809 * FC spec states we need 3 * ratov for CT requests
1810 */
1811 tmo = ((phba->fc_ratov * 3) + 3);
1812 }
dea31012005-04-17 16:05:31 -05001813
James Smart858c9f62007-06-17 19:56:39 -05001814
1815 if (!timer_pending(&vport->fc_disctmo)) {
1816 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1817 "set disc timer: tmo:x%x state:x%x flg:x%x",
1818 tmo, vport->port_state, vport->fc_flag);
1819 }
1820
James Smart2e0fef82007-06-17 19:56:36 -05001821 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1822 spin_lock_irq(shost->host_lock);
1823 vport->fc_flag |= FC_DISC_TMO;
1824 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001825
1826 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001827 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1828 "0247 Start Discovery Timer state x%x "
1829 "Data: x%x x%lx x%x x%x\n",
1830 vport->port_state, tmo,
1831 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1832 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05001833
1834 return;
1835}
1836
1837/*
1838 * Cancel rescue timer for Discovery / RSCN handling
1839 */
1840int
James Smart2e0fef82007-06-17 19:56:36 -05001841lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001842{
James Smart2e0fef82007-06-17 19:56:36 -05001843 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001844 unsigned long iflags;
1845
James Smart858c9f62007-06-17 19:56:39 -05001846 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1847 "can disc timer: state:x%x rtry:x%x flg:x%x",
1848 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
1849
dea31012005-04-17 16:05:31 -05001850 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05001851 if (vport->fc_flag & FC_DISC_TMO) {
1852 spin_lock_irqsave(shost->host_lock, iflags);
1853 vport->fc_flag &= ~FC_DISC_TMO;
1854 spin_unlock_irqrestore(shost->host_lock, iflags);
1855 del_timer_sync(&vport->fc_disctmo);
1856 spin_lock_irqsave(&vport->work_port_lock, iflags);
1857 vport->work_port_events &= ~WORKER_DISC_TMO;
1858 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05001859 }
1860
1861 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001862 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1863 "0248 Cancel Discovery Timer state x%x "
1864 "Data: x%x x%x x%x\n",
1865 vport->port_state, vport->fc_flag,
1866 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001867 return 0;
dea31012005-04-17 16:05:31 -05001868}
1869
1870/*
1871 * Check specified ring for outstanding IOCB on the SLI queue
1872 * Return true if iocb matches the specified nport
1873 */
1874int
James Smart2e0fef82007-06-17 19:56:36 -05001875lpfc_check_sli_ndlp(struct lpfc_hba *phba,
1876 struct lpfc_sli_ring *pring,
1877 struct lpfc_iocbq *iocb,
1878 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001879{
James Smart2e0fef82007-06-17 19:56:36 -05001880 struct lpfc_sli *psli = &phba->sli;
1881 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05001882 struct lpfc_vport *vport = ndlp->vport;
1883
1884 if (iocb->vport != vport)
1885 return 0;
1886
dea31012005-04-17 16:05:31 -05001887 if (pring->ringno == LPFC_ELS_RING) {
1888 switch (icmd->ulpCommand) {
1889 case CMD_GEN_REQUEST64_CR:
1890 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001891 return 1;
dea31012005-04-17 16:05:31 -05001892 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04001893 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1894 return 1;
dea31012005-04-17 16:05:31 -05001895 case CMD_XMIT_ELS_RSP64_CX:
1896 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001897 return 1;
dea31012005-04-17 16:05:31 -05001898 }
James Smarta4bc3372006-12-02 13:34:16 -05001899 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05001900
1901 } else if (pring->ringno == psli->fcp_ring) {
1902 /* Skip match check if waiting to relogin to FCP target */
1903 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001904 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001905 return 0;
dea31012005-04-17 16:05:31 -05001906 }
1907 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001908 return 1;
dea31012005-04-17 16:05:31 -05001909 }
1910 } else if (pring->ringno == psli->next_ring) {
1911
1912 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001913 return 0;
dea31012005-04-17 16:05:31 -05001914}
1915
1916/*
1917 * Free resources / clean up outstanding I/Os
1918 * associated with nlp_rpi in the LPFC_NODELIST entry.
1919 */
1920static int
James Smart2e0fef82007-06-17 19:56:36 -05001921lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001922{
James Smart2534ba72007-04-25 09:52:20 -04001923 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001924 struct lpfc_sli *psli;
1925 struct lpfc_sli_ring *pring;
1926 struct lpfc_iocbq *iocb, *next_iocb;
1927 IOCB_t *icmd;
1928 uint32_t rpi, i;
1929
James Smart92d7f7b2007-06-17 19:56:38 -05001930 lpfc_fabric_abort_nport(ndlp);
1931
dea31012005-04-17 16:05:31 -05001932 /*
1933 * Everything that matches on txcmplq will be returned
1934 * by firmware with a no rpi error.
1935 */
1936 psli = &phba->sli;
1937 rpi = ndlp->nlp_rpi;
1938 if (rpi) {
1939 /* Now process each ring */
1940 for (i = 0; i < psli->num_rings; i++) {
1941 pring = &psli->ring[i];
1942
James Smart2e0fef82007-06-17 19:56:36 -05001943 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001944 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05001945 list) {
dea31012005-04-17 16:05:31 -05001946 /*
1947 * Check to see if iocb matches the nport we are
1948 * looking for
1949 */
James Smart92d7f7b2007-06-17 19:56:38 -05001950 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
1951 ndlp))) {
dea31012005-04-17 16:05:31 -05001952 /* It matches, so deque and call compl
1953 with an error */
James Smart2534ba72007-04-25 09:52:20 -04001954 list_move_tail(&iocb->list,
1955 &completions);
dea31012005-04-17 16:05:31 -05001956 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05001957 }
1958 }
James Smart2e0fef82007-06-17 19:56:36 -05001959 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001960 }
1961 }
James Smart2534ba72007-04-25 09:52:20 -04001962
1963 while (!list_empty(&completions)) {
1964 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05001965 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04001966
James Smart2e0fef82007-06-17 19:56:36 -05001967 if (!iocb->iocb_cmpl)
1968 lpfc_sli_release_iocbq(phba, iocb);
1969 else {
James Smart2534ba72007-04-25 09:52:20 -04001970 icmd = &iocb->iocb;
1971 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1972 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
James Smart2e0fef82007-06-17 19:56:36 -05001973 (iocb->iocb_cmpl)(phba, iocb, iocb);
1974 }
James Smart2534ba72007-04-25 09:52:20 -04001975 }
1976
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001977 return 0;
dea31012005-04-17 16:05:31 -05001978}
1979
1980/*
1981 * Free rpi associated with LPFC_NODELIST entry.
1982 * This routine is called from lpfc_freenode(), when we are removing
1983 * a LPFC_NODELIST entry. It is also called if the driver initiates a
1984 * LOGO that completes successfully, and we are waiting to PLOGI back
1985 * to the remote NPort. In addition, it is called after we receive
1986 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1987 * we are waiting to PLOGI back to the remote NPort.
1988 */
1989int
James Smart2e0fef82007-06-17 19:56:36 -05001990lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001991{
James Smart2e0fef82007-06-17 19:56:36 -05001992 struct lpfc_hba *phba = vport->phba;
1993 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05001994 int rc;
1995
1996 if (ndlp->nlp_rpi) {
James Smart2e0fef82007-06-17 19:56:36 -05001997 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1998 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001999 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05002000 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002001 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002002 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05002003 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05002004 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002005 }
dea31012005-04-17 16:05:31 -05002006 lpfc_no_rpi(phba, ndlp);
2007 ndlp->nlp_rpi = 0;
2008 return 1;
2009 }
2010 return 0;
2011}
2012
James Smart92d7f7b2007-06-17 19:56:38 -05002013void
2014lpfc_unreg_all_rpis(struct lpfc_vport *vport)
2015{
2016 struct lpfc_hba *phba = vport->phba;
2017 LPFC_MBOXQ_t *mbox;
2018 int rc;
2019
2020 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2021 if (mbox) {
2022 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
2023 mbox->vport = vport;
2024 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002025 mbox->context1 = NULL;
2026 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05002027 if (rc == MBX_NOT_FINISHED) {
2028 mempool_free(mbox, phba->mbox_mem_pool);
2029 }
2030 }
2031}
2032
2033void
2034lpfc_unreg_default_rpis(struct lpfc_vport *vport)
2035{
2036 struct lpfc_hba *phba = vport->phba;
2037 LPFC_MBOXQ_t *mbox;
2038 int rc;
2039
2040 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2041 if (mbox) {
2042 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
2043 mbox->vport = vport;
2044 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05002045 mbox->context1 = NULL;
2046 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05002047 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04002048 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2049 "1815 Could not issue "
2050 "unreg_did (default rpis)\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002051 mempool_free(mbox, phba->mbox_mem_pool);
2052 }
2053 }
2054}
2055
dea31012005-04-17 16:05:31 -05002056/*
2057 * Free resources associated with LPFC_NODELIST entry
2058 * so it can be freed.
2059 */
2060static int
James Smart2e0fef82007-06-17 19:56:36 -05002061lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002062{
James Smart2e0fef82007-06-17 19:56:36 -05002063 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2064 struct lpfc_hba *phba = vport->phba;
2065 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05002066 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05002067
2068 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04002069 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2070 "0900 Cleanup node for NPort x%x "
2071 "Data: x%x x%x x%x\n",
2072 ndlp->nlp_DID, ndlp->nlp_flag,
2073 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05002074 if (NLP_CHK_FREE_REQ(ndlp)) {
2075 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2076 "0280 lpfc_cleanup_node: ndlp:x%p "
2077 "usgmap:x%x refcnt:%d\n",
2078 (void *)ndlp, ndlp->nlp_usg_map,
2079 atomic_read(&ndlp->kref.refcount));
2080 lpfc_dequeue_node(vport, ndlp);
2081 } else {
2082 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2083 "0281 lpfc_cleanup_node: ndlp:x%p "
2084 "usgmap:x%x refcnt:%d\n",
2085 (void *)ndlp, ndlp->nlp_usg_map,
2086 atomic_read(&ndlp->kref.refcount));
2087 lpfc_disable_node(vport, ndlp);
2088 }
dea31012005-04-17 16:05:31 -05002089
dea31012005-04-17 16:05:31 -05002090 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
2091 if ((mb = phba->sli.mbox_active)) {
2092 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
2093 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
2094 mb->context2 = NULL;
2095 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2096 }
2097 }
James Smart33ccf8d2006-08-17 11:57:58 -04002098
James Smart2e0fef82007-06-17 19:56:36 -05002099 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002100 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
2101 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002102 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05002103 mp = (struct lpfc_dmabuf *) (mb->context1);
2104 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05002105 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002106 kfree(mp);
2107 }
2108 list_del(&mb->list);
2109 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05002110 /* We shall not invoke the lpfc_nlp_put to decrement
2111 * the ndlp reference count as we are in the process
2112 * of lpfc_nlp_release.
2113 */
dea31012005-04-17 16:05:31 -05002114 }
2115 }
James Smart2e0fef82007-06-17 19:56:36 -05002116 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002117
James Smarte47c9092008-02-08 18:49:26 -05002118 lpfc_els_abort(phba, ndlp);
2119
James Smart2e0fef82007-06-17 19:56:36 -05002120 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04002121 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05002122 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002123
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05002124 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05002125 del_timer_sync(&ndlp->nlp_delayfunc);
2126
James Smart0d2b6b82008-06-14 22:52:47 -04002127 list_del_init(&ndlp->els_retry_evt.evt_listp);
2128 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002129
James Smart2e0fef82007-06-17 19:56:36 -05002130 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05002131
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002132 return 0;
dea31012005-04-17 16:05:31 -05002133}
2134
2135/*
2136 * Check to see if we can free the nlp back to the freelist.
2137 * If we are in the middle of using the nlp in the discovery state
2138 * machine, defer the free till we reach the end of the state machine.
2139 */
James Smart329f9bc2007-04-25 09:53:01 -04002140static void
James Smart2e0fef82007-06-17 19:56:36 -05002141lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002142{
James Smarta8adb832007-10-27 13:37:53 -04002143 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04002144 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04002145 LPFC_MBOXQ_t *mbox;
2146 int rc;
dea31012005-04-17 16:05:31 -05002147
James Smart0d2b6b82008-06-14 22:52:47 -04002148 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04002149 if (ndlp->nlp_flag & NLP_DEFER_RM && !ndlp->nlp_rpi) {
2150 /* For this case we need to cleanup the default rpi
2151 * allocated by the firmware.
2152 */
2153 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
2154 != NULL) {
2155 rc = lpfc_reg_login(phba, vport->vpi, ndlp->nlp_DID,
2156 (uint8_t *) &vport->fc_sparam, mbox, 0);
2157 if (rc) {
2158 mempool_free(mbox, phba->mbox_mem_pool);
2159 }
2160 else {
2161 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2162 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2163 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05002164 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04002165 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2166 if (rc == MBX_NOT_FINISHED) {
2167 mempool_free(mbox, phba->mbox_mem_pool);
2168 }
2169 }
2170 }
2171 }
James Smart2e0fef82007-06-17 19:56:36 -05002172 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04002173
James Smart2e0fef82007-06-17 19:56:36 -05002174 /*
James Smart92d7f7b2007-06-17 19:56:38 -05002175 * We can get here with a non-NULL ndlp->rport because when we
2176 * unregister a rport we don't break the rport/node linkage. So if we
2177 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05002178 */
James Smart92d7f7b2007-06-17 19:56:38 -05002179 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04002180 rdata = ndlp->rport->dd_data;
2181 rdata->pnode = NULL;
2182 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05002183 }
dea31012005-04-17 16:05:31 -05002184}
2185
2186static int
James Smart2e0fef82007-06-17 19:56:36 -05002187lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2188 uint32_t did)
dea31012005-04-17 16:05:31 -05002189{
James Smart2e0fef82007-06-17 19:56:36 -05002190 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05002191
2192 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002193 return 0;
dea31012005-04-17 16:05:31 -05002194
dea31012005-04-17 16:05:31 -05002195 /* First check for Direct match */
2196 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002197 return 1;
dea31012005-04-17 16:05:31 -05002198
2199 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05002200 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05002201 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002202 return 0;
dea31012005-04-17 16:05:31 -05002203 }
2204
2205 matchdid.un.word = did;
2206 ndlpdid.un.word = ndlp->nlp_DID;
2207 if (matchdid.un.b.id == ndlpdid.un.b.id) {
2208 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
2209 (mydid.un.b.area == matchdid.un.b.area)) {
2210 if ((ndlpdid.un.b.domain == 0) &&
2211 (ndlpdid.un.b.area == 0)) {
2212 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002213 return 1;
dea31012005-04-17 16:05:31 -05002214 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002215 return 0;
dea31012005-04-17 16:05:31 -05002216 }
2217
2218 matchdid.un.word = ndlp->nlp_DID;
2219 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
2220 (mydid.un.b.area == ndlpdid.un.b.area)) {
2221 if ((matchdid.un.b.domain == 0) &&
2222 (matchdid.un.b.area == 0)) {
2223 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002224 return 1;
dea31012005-04-17 16:05:31 -05002225 }
2226 }
2227 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002228 return 0;
dea31012005-04-17 16:05:31 -05002229}
2230
James Smart685f0bf2007-04-25 09:53:08 -04002231/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05002232static struct lpfc_nodelist *
2233__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002234{
James Smart2fb9bd82006-12-02 13:33:57 -05002235 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002236 uint32_t data1;
2237
James Smart2e0fef82007-06-17 19:56:36 -05002238 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2239 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04002240 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2241 ((uint32_t) ndlp->nlp_xri << 16) |
2242 ((uint32_t) ndlp->nlp_type << 8) |
2243 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04002244 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2245 "0929 FIND node DID "
2246 "Data: x%p x%x x%x x%x\n",
2247 ndlp, ndlp->nlp_DID,
2248 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04002249 return ndlp;
dea31012005-04-17 16:05:31 -05002250 }
2251 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002252
dea31012005-04-17 16:05:31 -05002253 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04002254 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2255 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05002256 return NULL;
2257}
2258
2259struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002260lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002261{
James Smart2e0fef82007-06-17 19:56:36 -05002262 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002263 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002264
James Smart2e0fef82007-06-17 19:56:36 -05002265 spin_lock_irq(shost->host_lock);
2266 ndlp = __lpfc_findnode_did(vport, did);
2267 spin_unlock_irq(shost->host_lock);
2268 return ndlp;
2269}
2270
2271struct lpfc_nodelist *
2272lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2273{
2274 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2275 struct lpfc_nodelist *ndlp;
2276
2277 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002278 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05002279 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2280 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05002281 return NULL;
2282 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05002283 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002284 if (!ndlp)
2285 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002286 lpfc_nlp_init(vport, ndlp, did);
2287 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2288 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002289 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002290 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002291 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05002292 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2293 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
2294 if (!ndlp)
2295 return NULL;
2296 spin_lock_irq(shost->host_lock);
2297 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2298 spin_unlock_irq(shost->host_lock);
2299 return ndlp;
dea31012005-04-17 16:05:31 -05002300 }
James Smarte47c9092008-02-08 18:49:26 -05002301
James Smart58da1ff2008-04-07 10:15:56 -04002302 if ((vport->fc_flag & FC_RSCN_MODE) &&
2303 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002304 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04002305 /* If we've already recieved a PLOGI from this NPort
2306 * we don't need to try to discover it again.
2307 */
2308 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
2309 return NULL;
2310
James Smart2e0fef82007-06-17 19:56:36 -05002311 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002312 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002313 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002314
2315 /* Since this node is marked for discovery,
2316 * delay timeout is not needed.
2317 */
James Smart0d2b6b82008-06-14 22:52:47 -04002318 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart071fbd3d2006-04-15 11:53:20 -04002319 } else
dea31012005-04-17 16:05:31 -05002320 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002321 } else {
James Smart87af33f2007-10-27 13:37:43 -04002322 /* If we've already recieved a PLOGI from this NPort,
2323 * or we are already in the process of discovery on it,
2324 * we don't need to try to discover it again.
2325 */
James Smart685f0bf2007-04-25 09:53:08 -04002326 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04002327 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2328 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05002329 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002330 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2331 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002332 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002333 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002334 }
2335 return ndlp;
2336}
2337
2338/* Build a list of nodes to discover based on the loopmap */
2339void
James Smart2e0fef82007-06-17 19:56:36 -05002340lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002341{
James Smart2e0fef82007-06-17 19:56:36 -05002342 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002343 int j;
2344 uint32_t alpa, index;
2345
James Smart2e0fef82007-06-17 19:56:36 -05002346 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002347 return;
James Smart2e0fef82007-06-17 19:56:36 -05002348
2349 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05002350 return;
dea31012005-04-17 16:05:31 -05002351
2352 /* Check for loop map present or not */
2353 if (phba->alpa_map[0]) {
2354 for (j = 1; j <= phba->alpa_map[0]; j++) {
2355 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05002356 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05002357 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002358 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002359 }
2360 } else {
2361 /* No alpamap, so try all alpa's */
2362 for (j = 0; j < FC_MAXLOOP; j++) {
2363 /* If cfg_scan_down is set, start from highest
2364 * ALPA (0xef) to lowest (0x1).
2365 */
James Smart3de2a652007-08-02 11:09:59 -04002366 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05002367 index = j;
2368 else
2369 index = FC_MAXLOOP - j - 1;
2370 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05002371 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05002372 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002373 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002374 }
2375 }
2376 return;
2377}
2378
dea31012005-04-17 16:05:31 -05002379void
James Smart2e0fef82007-06-17 19:56:36 -05002380lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002381{
dea31012005-04-17 16:05:31 -05002382 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05002383 struct lpfc_sli *psli = &phba->sli;
2384 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2385 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2386 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2387 int rc;
2388
James Smart92d7f7b2007-06-17 19:56:38 -05002389 /*
2390 * if it's not a physical port or if we already send
2391 * clear_la then don't send it.
2392 */
2393 if ((phba->link_state >= LPFC_CLEAR_LA) ||
2394 (vport->port_type != LPFC_PHYSICAL_PORT))
2395 return;
2396
James Smart2e0fef82007-06-17 19:56:36 -05002397 /* Link up discovery */
2398 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2399 phba->link_state = LPFC_CLEAR_LA;
2400 lpfc_clear_la(phba, mbox);
2401 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2402 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002403 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05002404 if (rc == MBX_NOT_FINISHED) {
2405 mempool_free(mbox, phba->mbox_mem_pool);
2406 lpfc_disc_flush_list(vport);
2407 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2408 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2409 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05002410 phba->link_state = LPFC_HBA_ERROR;
2411 }
2412 }
2413}
2414
2415/* Reg_vpi to tell firmware to resume normal operations */
2416void
2417lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2418{
2419 LPFC_MBOXQ_t *regvpimbox;
2420
2421 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2422 if (regvpimbox) {
2423 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
2424 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2425 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002426 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05002427 == MBX_NOT_FINISHED) {
2428 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002429 }
2430 }
2431}
2432
2433/* Start Link up / RSCN discovery on NPR nodes */
2434void
2435lpfc_disc_start(struct lpfc_vport *vport)
2436{
2437 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2438 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04002439 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05002440 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04002441 int did_changed;
dea31012005-04-17 16:05:31 -05002442
James Smart2e0fef82007-06-17 19:56:36 -05002443 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002444 return;
James Smart2e0fef82007-06-17 19:56:36 -05002445
2446 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05002447 clear_la_pending = 1;
2448 else
2449 clear_la_pending = 0;
2450
James Smart2e0fef82007-06-17 19:56:36 -05002451 if (vport->port_state < LPFC_VPORT_READY)
2452 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05002453
James Smart2e0fef82007-06-17 19:56:36 -05002454 lpfc_set_disctmo(vport);
2455
2456 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05002457 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002458 else
dea31012005-04-17 16:05:31 -05002459 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002460
2461 vport->fc_prevDID = vport->fc_myDID;
2462 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05002463
2464 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002465 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2466 "0202 Start Discovery hba state x%x "
2467 "Data: x%x x%x x%x\n",
2468 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
2469 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002470
2471 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002472 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05002473
2474 if (num_sent)
2475 return;
2476
James Smart92d7f7b2007-06-17 19:56:38 -05002477 /*
2478 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2479 * continue discovery.
2480 */
2481 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05002482 !(vport->fc_flag & FC_PT2PT) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002483 !(vport->fc_flag & FC_RSCN_MODE)) {
2484 lpfc_issue_reg_vpi(phba, vport);
2485 return;
2486 }
James Smart2e0fef82007-06-17 19:56:36 -05002487
James Smart92d7f7b2007-06-17 19:56:38 -05002488 /*
2489 * For SLI2, we need to set port_state to READY and continue
2490 * discovery.
2491 */
2492 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2493 /* If we get here, there is nothing to ADISC */
2494 if (vport->port_type == LPFC_PHYSICAL_PORT)
2495 lpfc_issue_clear_la(phba, vport);
2496
2497 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05002498 vport->num_disc_nodes = 0;
2499 /* go thru NPR nodes and issue ELS PLOGIs */
2500 if (vport->fc_npr_cnt)
2501 lpfc_els_disc_plogi(vport);
2502
2503 if (!vport->num_disc_nodes) {
2504 spin_lock_irq(shost->host_lock);
2505 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2506 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002507 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05002508 }
2509 }
James Smart92d7f7b2007-06-17 19:56:38 -05002510 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002511 } else {
2512 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002513 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05002514
2515 if (num_sent)
2516 return;
2517
James Smart2e0fef82007-06-17 19:56:36 -05002518 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05002519 /* Check to see if more RSCNs came in while we
2520 * were processing this one.
2521 */
James Smart2e0fef82007-06-17 19:56:36 -05002522 if ((vport->fc_rscn_id_cnt == 0) &&
2523 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2524 spin_lock_irq(shost->host_lock);
2525 vport->fc_flag &= ~FC_RSCN_MODE;
2526 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002527 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002528 } else
James Smart2e0fef82007-06-17 19:56:36 -05002529 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05002530 }
2531 }
2532 return;
2533}
2534
2535/*
2536 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2537 * ring the match the sppecified nodelist.
2538 */
2539static void
James Smart2e0fef82007-06-17 19:56:36 -05002540lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002541{
James Smart2534ba72007-04-25 09:52:20 -04002542 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002543 struct lpfc_sli *psli;
2544 IOCB_t *icmd;
2545 struct lpfc_iocbq *iocb, *next_iocb;
2546 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002547
2548 psli = &phba->sli;
2549 pring = &psli->ring[LPFC_ELS_RING];
2550
2551 /* Error matching iocb on txq or txcmplq
2552 * First check the txq.
2553 */
James Smart2e0fef82007-06-17 19:56:36 -05002554 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002555 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2556 if (iocb->context1 != ndlp) {
2557 continue;
2558 }
2559 icmd = &iocb->iocb;
2560 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2561 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2562
James Smart2534ba72007-04-25 09:52:20 -04002563 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05002564 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002565 }
2566 }
2567
2568 /* Next check the txcmplq */
2569 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2570 if (iocb->context1 != ndlp) {
2571 continue;
2572 }
2573 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05002574 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2575 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04002576 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05002577 }
2578 }
James Smart2e0fef82007-06-17 19:56:36 -05002579 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002580
2581 while (!list_empty(&completions)) {
2582 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05002583 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04002584
James Smart2e0fef82007-06-17 19:56:36 -05002585 if (!iocb->iocb_cmpl)
2586 lpfc_sli_release_iocbq(phba, iocb);
2587 else {
James Smart2534ba72007-04-25 09:52:20 -04002588 icmd = &iocb->iocb;
2589 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2590 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2591 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002592 }
James Smart2534ba72007-04-25 09:52:20 -04002593 }
dea31012005-04-17 16:05:31 -05002594}
2595
Adrian Bunka6ababd2007-11-05 18:07:33 +01002596static void
James Smart2e0fef82007-06-17 19:56:36 -05002597lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002598{
2599 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05002600 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002601
James Smart2e0fef82007-06-17 19:56:36 -05002602 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2603 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002604 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002605 if (!NLP_CHK_NODE_ACT(ndlp))
2606 continue;
James Smart685f0bf2007-04-25 09:53:08 -04002607 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2608 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2609 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04002610 }
dea31012005-04-17 16:05:31 -05002611 }
2612 }
dea31012005-04-17 16:05:31 -05002613}
2614
James Smart92d7f7b2007-06-17 19:56:38 -05002615void
2616lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2617{
2618 lpfc_els_flush_rscn(vport);
2619 lpfc_els_flush_cmd(vport);
2620 lpfc_disc_flush_list(vport);
2621}
2622
dea31012005-04-17 16:05:31 -05002623/*****************************************************************************/
2624/*
2625 * NAME: lpfc_disc_timeout
2626 *
2627 * FUNCTION: Fibre Channel driver discovery timeout routine.
2628 *
2629 * EXECUTION ENVIRONMENT: interrupt only
2630 *
2631 * CALLED FROM:
2632 * Timer function
2633 *
2634 * RETURNS:
2635 * none
2636 */
2637/*****************************************************************************/
2638void
2639lpfc_disc_timeout(unsigned long ptr)
2640{
James Smart2e0fef82007-06-17 19:56:36 -05002641 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2642 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04002643 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05002644 unsigned long flags = 0;
2645
2646 if (unlikely(!phba))
2647 return;
2648
James Smart5e9d9b82008-06-14 22:52:53 -04002649 spin_lock_irqsave(&vport->work_port_lock, flags);
2650 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
2651 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05002652 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04002653 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05002654
James Smart5e9d9b82008-06-14 22:52:53 -04002655 if (!tmo_posted)
2656 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002657 return;
2658}
2659
2660static void
James Smart2e0fef82007-06-17 19:56:36 -05002661lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002662{
James Smart2e0fef82007-06-17 19:56:36 -05002663 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2664 struct lpfc_hba *phba = vport->phba;
2665 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002666 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05002667 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05002668 int rc, clrlaerr = 0;
2669
James Smart2e0fef82007-06-17 19:56:36 -05002670 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05002671 return;
2672
James Smart2e0fef82007-06-17 19:56:36 -05002673 spin_lock_irq(shost->host_lock);
2674 vport->fc_flag &= ~FC_DISC_TMO;
2675 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002676
James Smart858c9f62007-06-17 19:56:39 -05002677 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2678 "disc timeout: state:x%x rtry:x%x flg:x%x",
2679 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2680
James Smart2e0fef82007-06-17 19:56:36 -05002681 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05002682
2683 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05002684 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2685 * FAN
2686 */
2687 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04002688 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
2689 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002690 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05002691 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002692 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002693 if (!NLP_CHK_NODE_ACT(ndlp))
2694 continue;
James Smart685f0bf2007-04-25 09:53:08 -04002695 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2696 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002697 if (ndlp->nlp_type & NLP_FABRIC) {
2698 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05002699 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04002700
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002701 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002702 /* Fail outstanding IO now since device
2703 * is marked for PLOGI.
2704 */
James Smart2e0fef82007-06-17 19:56:36 -05002705 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002706 }
2707 }
James Smart92d7f7b2007-06-17 19:56:38 -05002708 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05002709 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05002710 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002711 }
dea31012005-04-17 16:05:31 -05002712 break;
2713
James Smart92d7f7b2007-06-17 19:56:38 -05002714 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05002715 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05002716 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05002717 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04002718 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2719 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04002720 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05002721
2722 /* Assume no Fabric and go on with discovery.
2723 * Check for outstanding ELS FLOGI to abort.
2724 */
2725
2726 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05002727 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002728
2729 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002730 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002731 break;
2732
2733 case LPFC_FABRIC_CFG_LINK:
2734 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2735 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04002736 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2737 "0223 Timeout while waiting for "
2738 "NameServer login\n");
dea31012005-04-17 16:05:31 -05002739 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05002740 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05002741 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04002742 lpfc_els_abort(phba, ndlp);
2743
2744 /* ReStart discovery */
2745 goto restart_disc;
dea31012005-04-17 16:05:31 -05002746
2747 case LPFC_NS_QRY:
2748 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04002749 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2750 "0224 NameServer Query timeout "
2751 "Data: x%x x%x\n",
2752 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002753
James Smart92d7f7b2007-06-17 19:56:38 -05002754 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2755 /* Try it one more time */
2756 vport->fc_ns_retry++;
2757 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2758 vport->fc_ns_retry, 0);
2759 if (rc == 0)
2760 break;
dea31012005-04-17 16:05:31 -05002761 }
James Smart92d7f7b2007-06-17 19:56:38 -05002762 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002763
James Smart87af33f2007-10-27 13:37:43 -04002764restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05002765 /*
2766 * Discovery is over.
2767 * set port_state to PORT_READY if SLI2.
2768 * cmpl_reg_vpi will set port_state to READY for SLI3.
2769 */
2770 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2771 lpfc_issue_reg_vpi(phba, vport);
2772 else { /* NPIV Not enabled */
2773 lpfc_issue_clear_la(phba, vport);
2774 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002775 }
2776
2777 /* Setup and issue mailbox INITIALIZE LINK command */
2778 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2779 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04002780 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2781 "0206 Device Discovery "
2782 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05002783 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002784 break;
2785 }
2786
2787 lpfc_linkdown(phba);
2788 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2789 phba->cfg_link_speed);
2790 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05002791 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002792 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002793 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04002794 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05002795 if (rc == MBX_NOT_FINISHED)
2796 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2797
2798 break;
2799
2800 case LPFC_DISC_AUTH:
2801 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04002802 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2803 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002804 lpfc_disc_flush_list(vport);
2805
James Smart92d7f7b2007-06-17 19:56:38 -05002806 /*
2807 * set port_state to PORT_READY if SLI2.
2808 * cmpl_reg_vpi will set port_state to READY for SLI3.
2809 */
2810 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2811 lpfc_issue_reg_vpi(phba, vport);
2812 else { /* NPIV Not enabled */
2813 lpfc_issue_clear_la(phba, vport);
2814 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002815 }
2816 break;
2817
James Smart2e0fef82007-06-17 19:56:36 -05002818 case LPFC_VPORT_READY:
2819 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04002820 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2821 "0231 RSCN timeout Data: x%x "
2822 "x%x\n",
2823 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002824
2825 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05002826 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05002827
James Smart2e0fef82007-06-17 19:56:36 -05002828 lpfc_els_flush_rscn(vport);
2829 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05002830 }
2831 break;
James Smart2e0fef82007-06-17 19:56:36 -05002832
James Smart92d7f7b2007-06-17 19:56:38 -05002833 default:
James Smarte8b62012007-08-02 11:10:09 -04002834 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04002835 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04002836 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05002837 break;
2838 }
2839
2840 switch (phba->link_state) {
2841 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05002842 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04002843 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2844 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002845 clrlaerr = 1;
2846 break;
2847
James Smart09372822008-01-11 01:52:54 -05002848 case LPFC_LINK_UP:
2849 lpfc_issue_clear_la(phba, vport);
2850 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05002851 case LPFC_LINK_UNKNOWN:
2852 case LPFC_WARM_START:
2853 case LPFC_INIT_START:
2854 case LPFC_INIT_MBX_CMDS:
2855 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05002856 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04002857 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2858 "0230 Unexpected timeout, hba link "
2859 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05002860 clrlaerr = 1;
2861 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002862
2863 case LPFC_HBA_READY:
2864 break;
dea31012005-04-17 16:05:31 -05002865 }
2866
2867 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05002868 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05002869 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002870 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2871 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05002872 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002873 }
2874
2875 return;
2876}
2877
dea31012005-04-17 16:05:31 -05002878/*
2879 * This routine handles processing a NameServer REG_LOGIN mailbox
2880 * command upon completion. It is setup in the LPFC_MBOXQ
2881 * as the completion routine when the command is
2882 * handed off to the SLI layer.
2883 */
2884void
James Smart2e0fef82007-06-17 19:56:36 -05002885lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002886{
James Smart2e0fef82007-06-17 19:56:36 -05002887 MAILBOX_t *mb = &pmb->mb;
2888 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2889 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2890 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002891
2892 pmb->context1 = NULL;
2893
dea31012005-04-17 16:05:31 -05002894 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05002895 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002896 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002897
James Smart2e0fef82007-06-17 19:56:36 -05002898 /*
2899 * Start issuing Fabric-Device Management Interface (FDMI) command to
2900 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
2901 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05002902 */
James Smart2e0fef82007-06-17 19:56:36 -05002903
James Smart3de2a652007-08-02 11:09:59 -04002904 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05002905 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
2906 else
2907 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05002908
James Smartfa4066b2008-01-11 01:53:27 -05002909 /* decrement the node reference count held for this callback
2910 * function.
2911 */
James Smart329f9bc2007-04-25 09:53:01 -04002912 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002913 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2914 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002915 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002916
2917 return;
2918}
2919
James Smart685f0bf2007-04-25 09:53:08 -04002920static int
2921lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2922{
2923 uint16_t *rpi = param;
2924
2925 return ndlp->nlp_rpi == *rpi;
2926}
2927
2928static int
2929lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2930{
2931 return memcmp(&ndlp->nlp_portname, param,
2932 sizeof(ndlp->nlp_portname)) == 0;
2933}
2934
Adrian Bunka6ababd2007-11-05 18:07:33 +01002935static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002936__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05002937{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002938 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002939
James Smart2e0fef82007-06-17 19:56:36 -05002940 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04002941 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04002942 return ndlp;
2943 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002944 return NULL;
dea31012005-04-17 16:05:31 -05002945}
2946
James Smart685f0bf2007-04-25 09:53:08 -04002947/*
2948 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05002949 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04002950 */
2951struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002952__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04002953{
James Smart2e0fef82007-06-17 19:56:36 -05002954 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04002955}
2956
James Smart488d1462006-03-07 15:02:37 -05002957/*
James Smart685f0bf2007-04-25 09:53:08 -04002958 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05002959 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05002960 */
2961struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002962lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05002963{
James Smart2e0fef82007-06-17 19:56:36 -05002964 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05002965 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05002966
James Smart2e0fef82007-06-17 19:56:36 -05002967 spin_lock_irq(shost->host_lock);
2968 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
2969 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002970 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05002971}
2972
dea31012005-04-17 16:05:31 -05002973void
James Smart2e0fef82007-06-17 19:56:36 -05002974lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2975 uint32_t did)
dea31012005-04-17 16:05:31 -05002976{
2977 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -05002978 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05002979 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002980 init_timer(&ndlp->nlp_delayfunc);
2981 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2982 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2983 ndlp->nlp_DID = did;
James Smart2e0fef82007-06-17 19:56:36 -05002984 ndlp->vport = vport;
dea31012005-04-17 16:05:31 -05002985 ndlp->nlp_sid = NLP_NO_SID;
James Smart685f0bf2007-04-25 09:53:08 -04002986 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart329f9bc2007-04-25 09:53:01 -04002987 kref_init(&ndlp->kref);
James Smarte47c9092008-02-08 18:49:26 -05002988 NLP_INT_NODE_ACT(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05002989
2990 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2991 "node init: did:x%x",
2992 ndlp->nlp_DID, 0, 0);
2993
dea31012005-04-17 16:05:31 -05002994 return;
2995}
James Smart329f9bc2007-04-25 09:53:01 -04002996
James Smart98c9ea52007-10-27 13:37:33 -04002997/* This routine releases all resources associated with a specifc NPort's ndlp
2998 * and mempool_free's the nodelist.
2999 */
James Smart311464e2007-08-02 11:10:37 -04003000static void
James Smart329f9bc2007-04-25 09:53:01 -04003001lpfc_nlp_release(struct kref *kref)
3002{
James Smarte47c9092008-02-08 18:49:26 -05003003 struct lpfc_hba *phba;
3004 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04003005 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
3006 kref);
James Smart858c9f62007-06-17 19:56:39 -05003007
3008 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3009 "node release: did:x%x flg:x%x type:x%x",
3010 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3011
James Smarte47c9092008-02-08 18:49:26 -05003012 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3013 "0279 lpfc_nlp_release: ndlp:x%p "
3014 "usgmap:x%x refcnt:%d\n",
3015 (void *)ndlp, ndlp->nlp_usg_map,
3016 atomic_read(&ndlp->kref.refcount));
3017
3018 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05003019 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003020
3021 /* clear the ndlp active flag for all release cases */
3022 phba = ndlp->vport->phba;
3023 spin_lock_irqsave(&phba->ndlp_lock, flags);
3024 NLP_CLR_NODE_ACT(ndlp);
3025 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3026
3027 /* free ndlp memory for final ndlp release */
3028 if (NLP_CHK_FREE_REQ(ndlp))
3029 mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04003030}
3031
James Smart98c9ea52007-10-27 13:37:33 -04003032/* This routine bumps the reference count for a ndlp structure to ensure
3033 * that one discovery thread won't free a ndlp while another discovery thread
3034 * is using it.
3035 */
James Smart329f9bc2007-04-25 09:53:01 -04003036struct lpfc_nodelist *
3037lpfc_nlp_get(struct lpfc_nodelist *ndlp)
3038{
James Smarte47c9092008-02-08 18:49:26 -05003039 struct lpfc_hba *phba;
3040 unsigned long flags;
3041
James Smart98c9ea52007-10-27 13:37:33 -04003042 if (ndlp) {
3043 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3044 "node get: did:x%x flg:x%x refcnt:x%x",
3045 ndlp->nlp_DID, ndlp->nlp_flag,
3046 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003047 /* The check of ndlp usage to prevent incrementing the
3048 * ndlp reference count that is in the process of being
3049 * released.
3050 */
3051 phba = ndlp->vport->phba;
3052 spin_lock_irqsave(&phba->ndlp_lock, flags);
3053 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
3054 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3055 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3056 "0276 lpfc_nlp_get: ndlp:x%p "
3057 "usgmap:x%x refcnt:%d\n",
3058 (void *)ndlp, ndlp->nlp_usg_map,
3059 atomic_read(&ndlp->kref.refcount));
3060 return NULL;
3061 } else
3062 kref_get(&ndlp->kref);
3063 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04003064 }
James Smart329f9bc2007-04-25 09:53:01 -04003065 return ndlp;
3066}
3067
James Smart98c9ea52007-10-27 13:37:33 -04003068/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05003069 * count goes to 0, this indicates the the associated nodelist should be
3070 * freed. Returning 1 indicates the ndlp resource has been released; on the
3071 * other hand, returning 0 indicates the ndlp resource has not been released
3072 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04003073 */
James Smart329f9bc2007-04-25 09:53:01 -04003074int
3075lpfc_nlp_put(struct lpfc_nodelist *ndlp)
3076{
James Smarte47c9092008-02-08 18:49:26 -05003077 struct lpfc_hba *phba;
3078 unsigned long flags;
3079
3080 if (!ndlp)
3081 return 1;
3082
3083 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3084 "node put: did:x%x flg:x%x refcnt:x%x",
3085 ndlp->nlp_DID, ndlp->nlp_flag,
3086 atomic_read(&ndlp->kref.refcount));
3087 phba = ndlp->vport->phba;
3088 spin_lock_irqsave(&phba->ndlp_lock, flags);
3089 /* Check the ndlp memory free acknowledge flag to avoid the
3090 * possible race condition that kref_put got invoked again
3091 * after previous one has done ndlp memory free.
3092 */
3093 if (NLP_CHK_FREE_ACK(ndlp)) {
3094 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3095 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3096 "0274 lpfc_nlp_put: ndlp:x%p "
3097 "usgmap:x%x refcnt:%d\n",
3098 (void *)ndlp, ndlp->nlp_usg_map,
3099 atomic_read(&ndlp->kref.refcount));
3100 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003101 }
James Smarte47c9092008-02-08 18:49:26 -05003102 /* Check the ndlp inactivate log flag to avoid the possible
3103 * race condition that kref_put got invoked again after ndlp
3104 * is already in inactivating state.
3105 */
3106 if (NLP_CHK_IACT_REQ(ndlp)) {
3107 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3108 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3109 "0275 lpfc_nlp_put: ndlp:x%p "
3110 "usgmap:x%x refcnt:%d\n",
3111 (void *)ndlp, ndlp->nlp_usg_map,
3112 atomic_read(&ndlp->kref.refcount));
3113 return 1;
3114 }
3115 /* For last put, mark the ndlp usage flags to make sure no
3116 * other kref_get and kref_put on the same ndlp shall get
3117 * in between the process when the final kref_put has been
3118 * invoked on this ndlp.
3119 */
3120 if (atomic_read(&ndlp->kref.refcount) == 1) {
3121 /* Indicate ndlp is put to inactive state. */
3122 NLP_SET_IACT_REQ(ndlp);
3123 /* Acknowledge ndlp memory free has been seen. */
3124 if (NLP_CHK_FREE_REQ(ndlp))
3125 NLP_SET_FREE_ACK(ndlp);
3126 }
3127 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3128 /* Note, the kref_put returns 1 when decrementing a reference
3129 * count that was 1, it invokes the release callback function,
3130 * but it still left the reference count as 1 (not actually
3131 * performs the last decrementation). Otherwise, it actually
3132 * decrements the reference count and returns 0.
3133 */
3134 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04003135}
James Smart98c9ea52007-10-27 13:37:33 -04003136
3137/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05003138 * by any other discovery thread. This routine returns 1 if the
3139 * ndlp has been freed. A return value of 0 indicates the ndlp is
3140 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04003141 */
3142int
3143lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
3144{
3145 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3146 "node not used: did:x%x flg:x%x refcnt:x%x",
3147 ndlp->nlp_DID, ndlp->nlp_flag,
3148 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05003149 if (atomic_read(&ndlp->kref.refcount) == 1)
3150 if (lpfc_nlp_put(ndlp))
3151 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04003152 return 0;
3153}