blob: dc042bd97baa62e3ebb2fcd09b322f85f921df2e [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 Smart9413aff2007-04-25 09:53:35 -04004 * Copyright (C) 2004-2007 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/kthread.h>
25#include <linux/interrupt.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_device.h>
29#include <scsi/scsi_host.h>
30#include <scsi/scsi_transport_fc.h>
31
32#include "lpfc_hw.h"
33#include "lpfc_disc.h"
34#include "lpfc_sli.h"
35#include "lpfc_scsi.h"
36#include "lpfc.h"
37#include "lpfc_logmsg.h"
38#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050039#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050040#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050041
42/* AlpaArray for assignment of scsid for scan-down and bind_method */
43static uint8_t lpfcAlpaArray[] = {
44 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
45 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
46 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
47 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
48 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
49 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
50 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
51 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
52 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
53 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
54 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
55 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
56 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
57};
58
James Smart2e0fef82007-06-17 19:56:36 -050059static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010060static void lpfc_disc_flush_list(struct lpfc_vport *vport);
dea31012005-04-17 16:05:31 -050061
James Smartc01f3202006-08-18 17:47:08 -040062void
63lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050064{
James Smartc01f3202006-08-18 17:47:08 -040065 struct lpfc_rport_data *rdata;
66 struct lpfc_nodelist * ndlp;
67 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050068
James Smartc01f3202006-08-18 17:47:08 -040069 rdata = rport->dd_data;
70 ndlp = rdata->pnode;
71
72 if (!ndlp) {
73 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
74 printk(KERN_ERR "Cannot find remote node"
75 " to terminate I/O Data x%x\n",
76 rport->port_id);
dea31012005-04-17 16:05:31 -050077 return;
78 }
79
James Smart2e0fef82007-06-17 19:56:36 -050080 phba = ndlp->vport->phba;
James Smart1a169682006-03-07 15:04:06 -050081
James Smart858c9f62007-06-17 19:56:39 -050082 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
83 "rport terminate: sid:x%x did:x%x flg:x%x",
84 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
85
James Smartc01f3202006-08-18 17:47:08 -040086 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040087 lpfc_sli_abort_iocb(ndlp->vport,
88 &phba->sli.ring[phba->sli.fcp_ring],
89 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040090 }
James Smartc01f3202006-08-18 17:47:08 -040091
James Smart51ef4c22007-08-02 11:10:31 -040092 /*
93 * A device is normally blocked for rediscovery and unblocked when
94 * devloss timeout happens. In case a vport is removed or driver
95 * unloaded before devloss timeout happens, we need to unblock here.
96 */
97 scsi_target_unblock(&rport->dev);
James Smartc01f3202006-08-18 17:47:08 -040098 return;
99}
100
101/*
102 * This function will be called when dev_loss_tmo fire.
103 */
104void
105lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
106{
107 struct lpfc_rport_data *rdata;
108 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500109 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500110 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500111 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400112 int put_node;
113 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400114
115 rdata = rport->dd_data;
116 ndlp = rdata->pnode;
James Smart09372822008-01-11 01:52:54 -0500117 if (!ndlp)
James Smartc01f3202006-08-18 17:47:08 -0400118 return;
James Smartc01f3202006-08-18 17:47:08 -0400119
James Smart858c9f62007-06-17 19:56:39 -0500120 vport = ndlp->vport;
121 phba = vport->phba;
122
123 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
124 "rport devlosscb: sid:x%x did:x%x flg:x%x",
125 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
126
James Smarta8adb832007-10-27 13:37:53 -0400127 /* Don't defer this if we are in the process of deleting the vport
128 * or unloading the driver. The unload will cleanup the node
129 * appropriately we just need to cleanup the ndlp rport info here.
130 */
131 if (vport->load_flag & FC_UNLOADING) {
132 put_node = rdata->pnode != NULL;
133 put_rport = ndlp->rport != NULL;
134 rdata->pnode = NULL;
135 ndlp->rport = NULL;
136 if (put_node)
137 lpfc_nlp_put(ndlp);
138 if (put_rport)
139 put_device(&rport->dev);
140 return;
141 }
142
143 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
144 return;
145
James Smart858c9f62007-06-17 19:56:39 -0500146 evtp = &ndlp->dev_loss_evt;
147
148 if (!list_empty(&evtp->evt_listp))
149 return;
150
151 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500152 /* We need to hold the node by incrementing the reference
153 * count until this queued work is done
154 */
155 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500156 evtp->evt = LPFC_EVT_DEV_LOSS;
157 list_add_tail(&evtp->evt_listp, &phba->work_list);
158 if (phba->work_wait)
159 wake_up(phba->work_wait);
160
161 spin_unlock_irq(&phba->hbalock);
162
James Smart858c9f62007-06-17 19:56:39 -0500163 return;
164}
165
166/*
167 * This function is called from the worker thread when dev_loss_tmo
168 * expire.
169 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100170static void
James Smart858c9f62007-06-17 19:56:39 -0500171lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
172{
173 struct lpfc_rport_data *rdata;
174 struct fc_rport *rport;
175 struct lpfc_vport *vport;
176 struct lpfc_hba *phba;
177 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400178 int put_node;
179 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500180 int warn_on = 0;
181
182 rport = ndlp->rport;
183
184 if (!rport)
185 return;
186
187 rdata = rport->dd_data;
188 name = (uint8_t *) &ndlp->nlp_portname;
189 vport = ndlp->vport;
190 phba = vport->phba;
191
192 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
193 "rport devlosstmo:did:x%x type:x%x id:x%x",
194 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
195
James Smarta8adb832007-10-27 13:37:53 -0400196 /* Don't defer this if we are in the process of deleting the vport
197 * or unloading the driver. The unload will cleanup the node
198 * appropriately we just need to cleanup the ndlp rport info here.
199 */
200 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500201 if (ndlp->nlp_sid != NLP_NO_SID) {
202 /* flush the target */
203 lpfc_sli_abort_iocb(vport,
204 &phba->sli.ring[phba->sli.fcp_ring],
205 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
206 }
James Smarta8adb832007-10-27 13:37:53 -0400207 put_node = rdata->pnode != NULL;
208 put_rport = ndlp->rport != NULL;
209 rdata->pnode = NULL;
210 ndlp->rport = NULL;
211 if (put_node)
212 lpfc_nlp_put(ndlp);
213 if (put_rport)
214 put_device(&rport->dev);
215 return;
216 }
217
218 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
James Smart858c9f62007-06-17 19:56:39 -0500219 return;
220
James Smart92d7f7b2007-06-17 19:56:38 -0500221 if (ndlp->nlp_type & NLP_FABRIC) {
222 /* We will clean up these Nodes in linkup */
223 put_node = rdata->pnode != NULL;
224 put_rport = ndlp->rport != NULL;
225 rdata->pnode = NULL;
226 ndlp->rport = NULL;
227 if (put_node)
228 lpfc_nlp_put(ndlp);
229 if (put_rport)
230 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400231 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500232 }
James Smart82085712007-04-25 09:52:41 -0400233
dea31012005-04-17 16:05:31 -0500234 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400235 warn_on = 1;
dea31012005-04-17 16:05:31 -0500236 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400237 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
238 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500239 }
James Smart2e0fef82007-06-17 19:56:36 -0500240 if (vport->load_flag & FC_UNLOADING)
James Smartc01f3202006-08-18 17:47:08 -0400241 warn_on = 0;
242
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400243 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400244 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
245 "0203 Devloss timeout on "
246 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
247 "NPort x%x Data: x%x x%x x%x\n",
248 *name, *(name+1), *(name+2), *(name+3),
249 *(name+4), *(name+5), *(name+6), *(name+7),
250 ndlp->nlp_DID, ndlp->nlp_flag,
251 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400252 } else {
James Smarte8b62012007-08-02 11:10:09 -0400253 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
254 "0204 Devloss timeout on "
255 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
256 "NPort x%x Data: x%x x%x x%x\n",
257 *name, *(name+1), *(name+2), *(name+3),
258 *(name+4), *(name+5), *(name+6), *(name+7),
259 ndlp->nlp_DID, ndlp->nlp_flag,
260 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400261 }
262
James Smart87af33f2007-10-27 13:37:43 -0400263 put_node = rdata->pnode != NULL;
264 put_rport = ndlp->rport != NULL;
265 rdata->pnode = NULL;
266 ndlp->rport = NULL;
267 if (put_node)
268 lpfc_nlp_put(ndlp);
269 if (put_rport)
270 put_device(&rport->dev);
271
James Smart2e0fef82007-06-17 19:56:36 -0500272 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400273 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400274 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smart87af33f2007-10-27 13:37:43 -0400275 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -0500276 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart1dcb58e2007-04-25 09:51:30 -0400277 }
James Smart92d7f7b2007-06-17 19:56:38 -0500278}
James Smartc01f3202006-08-18 17:47:08 -0400279
James Smart92d7f7b2007-06-17 19:56:38 -0500280
281void
282lpfc_worker_wake_up(struct lpfc_hba *phba)
283{
284 wake_up(phba->work_wait);
dea31012005-04-17 16:05:31 -0500285 return;
286}
287
288static void
James Smart2e0fef82007-06-17 19:56:36 -0500289lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500290{
291 struct lpfc_work_evt *evtp = NULL;
292 struct lpfc_nodelist *ndlp;
293 int free_evt;
294
James Smart2e0fef82007-06-17 19:56:36 -0500295 spin_lock_irq(&phba->hbalock);
296 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500297 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
298 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500299 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500300 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500301 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500302 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500303 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500304 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500305 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500306 /* decrement the node reference count held
307 * for this queued work
308 */
309 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500310 break;
James Smart858c9f62007-06-17 19:56:39 -0500311 case LPFC_EVT_DEV_LOSS:
312 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smart858c9f62007-06-17 19:56:39 -0500313 lpfc_dev_loss_tmo_handler(ndlp);
314 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500315 /* decrement the node reference count held for
316 * this queued work
317 */
James Smart858c9f62007-06-17 19:56:39 -0500318 lpfc_nlp_put(ndlp);
319 break;
dea31012005-04-17 16:05:31 -0500320 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500321 if (phba->link_state < LPFC_LINK_DOWN)
322 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500323 else
James Smart2e0fef82007-06-17 19:56:36 -0500324 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500325 complete((struct completion *)(evtp->evt_arg2));
326 break;
James Smart46fa3112007-04-25 09:51:45 -0400327 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500328 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400329 lpfc_offline_prep(phba);
330 *(int *)(evtp->evt_arg1) = 0;
331 complete((struct completion *)(evtp->evt_arg2));
332 break;
333 case LPFC_EVT_OFFLINE:
334 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500335 lpfc_sli_brdrestart(phba);
336 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400337 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
338 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500339 complete((struct completion *)(evtp->evt_arg2));
340 break;
341 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400342 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500343 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500344 lpfc_sli_brdreset(phba);
345 lpfc_hba_down_post(phba);
346 *(int *)(evtp->evt_arg1) =
347 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400348 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500349 complete((struct completion *)(evtp->evt_arg2));
350 break;
351 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400352 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500353 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500354 = (phba->pport->stopped)
355 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400356 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500357 complete((struct completion *)(evtp->evt_arg2));
358 break;
359 }
360 if (free_evt)
361 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500362 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500363 }
James Smart2e0fef82007-06-17 19:56:36 -0500364 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500365
366}
367
James Smart311464e2007-08-02 11:10:37 -0400368static void
James Smart2e0fef82007-06-17 19:56:36 -0500369lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500370{
371 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500372 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400373 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400374 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400375 int i;
dea31012005-04-17 16:05:31 -0500376
James Smart2e0fef82007-06-17 19:56:36 -0500377 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500378 ha_copy = phba->work_ha;
379 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500380 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500381
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500382 if (ha_copy & HA_ERATT)
dea31012005-04-17 16:05:31 -0500383 lpfc_handle_eratt(phba);
384
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500385 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500386 lpfc_sli_handle_mb_event(phba);
387
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500388 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500389 lpfc_handle_latt(phba);
James Smart549e55c2007-08-02 11:09:51 -0400390 vports = lpfc_create_vport_work_array(phba);
391 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500392 for(i = 0; i <= phba->max_vpi; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400393 /*
394 * We could have no vports in array if unloading, so if
395 * this happens then just use the pport
396 */
397 if (vports[i] == NULL && i == 0)
398 vport = phba->pport;
399 else
400 vport = vports[i];
401 if (vport == NULL)
402 break;
403 work_port_events = vport->work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400404 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400405 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400406 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400407 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400408 if (work_port_events & WORKER_HB_TMO)
409 lpfc_hb_timeout_handler(phba);
410 if (work_port_events & WORKER_MBOX_TMO)
411 lpfc_mbox_timeout_handler(phba);
412 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
413 lpfc_unblock_fabric_iocbs(phba);
414 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400415 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400416 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
417 lpfc_ramp_down_queue_handler(phba);
418 if (work_port_events & WORKER_RAMP_UP_QUEUE)
419 lpfc_ramp_up_queue_handler(phba);
James Smart51ef4c22007-08-02 11:10:31 -0400420 spin_lock_irq(&vport->work_port_lock);
421 vport->work_port_events &= ~work_port_events;
422 spin_unlock_irq(&vport->work_port_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500423 }
James Smart09372822008-01-11 01:52:54 -0500424 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500425
James Smart858c9f62007-06-17 19:56:39 -0500426 pring = &phba->sli.ring[LPFC_ELS_RING];
427 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
428 status >>= (4*LPFC_ELS_RING);
429 if ((status & HA_RXMASK)
430 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400431 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500432 pring->flag |= LPFC_DEFERRED_RING_EVENT;
433 } else {
434 lpfc_sli_handle_slow_ring_event(phba, pring,
435 (status &
436 HA_RXMASK));
437 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
438 }
439 /*
440 * Turn on Ring interrupts
441 */
442 spin_lock_irq(&phba->hbalock);
443 control = readl(phba->HCregaddr);
444 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
James Smarta58cbd52007-08-02 11:09:43 -0400445 lpfc_debugfs_slow_ring_trc(phba,
446 "WRK Enable ring: cntl:x%x hacopy:x%x",
447 control, ha_copy, 0);
448
James Smart858c9f62007-06-17 19:56:39 -0500449 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -0500450 writel(control, phba->HCregaddr);
451 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500452 }
James Smarta58cbd52007-08-02 11:09:43 -0400453 else {
454 lpfc_debugfs_slow_ring_trc(phba,
455 "WRK Ring ok: cntl:x%x hacopy:x%x",
456 control, ha_copy, 0);
457 }
James Smart858c9f62007-06-17 19:56:39 -0500458 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500459 }
James Smart2e0fef82007-06-17 19:56:36 -0500460 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500461}
462
463static int
James Smart2e0fef82007-06-17 19:56:36 -0500464check_work_wait_done(struct lpfc_hba *phba)
465{
James Smart92d7f7b2007-06-17 19:56:38 -0500466 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400467 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart858c9f62007-06-17 19:56:39 -0500468 int rc = 0;
James Smarted957682007-06-17 19:56:37 -0500469
James Smart2e0fef82007-06-17 19:56:36 -0500470 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500471 list_for_each_entry(vport, &phba->port_list, listentry) {
472 if (vport->work_port_events) {
473 rc = 1;
James Smart549e55c2007-08-02 11:09:51 -0400474 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500475 }
476 }
James Smart549e55c2007-08-02 11:09:51 -0400477 if (rc || phba->work_ha || (!list_empty(&phba->work_list)) ||
478 kthread_should_stop() || pring->flag & LPFC_DEFERRED_RING_EVENT) {
James Smart2e0fef82007-06-17 19:56:36 -0500479 rc = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500480 phba->work_found++;
James Smart549e55c2007-08-02 11:09:51 -0400481 } else
James Smart92d7f7b2007-06-17 19:56:38 -0500482 phba->work_found = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500483 spin_unlock_irq(&phba->hbalock);
484 return rc;
dea31012005-04-17 16:05:31 -0500485}
486
James Smart92d7f7b2007-06-17 19:56:38 -0500487
dea31012005-04-17 16:05:31 -0500488int
489lpfc_do_work(void *p)
490{
491 struct lpfc_hba *phba = p;
492 int rc;
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800493 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(work_waitq);
dea31012005-04-17 16:05:31 -0500494
495 set_user_nice(current, -20);
496 phba->work_wait = &work_waitq;
James Smart92d7f7b2007-06-17 19:56:38 -0500497 phba->work_found = 0;
dea31012005-04-17 16:05:31 -0500498
499 while (1) {
500
501 rc = wait_event_interruptible(work_waitq,
James Smart92d7f7b2007-06-17 19:56:38 -0500502 check_work_wait_done(phba));
503
dea31012005-04-17 16:05:31 -0500504 BUG_ON(rc);
505
506 if (kthread_should_stop())
507 break;
508
509 lpfc_work_done(phba);
510
James Smart92d7f7b2007-06-17 19:56:38 -0500511 /* If there is alot of slow ring work, like during link up
512 * check_work_wait_done() may cause this thread to not give
513 * up the CPU for very long periods of time. This may cause
514 * soft lockups or other problems. To avoid these situations
515 * give up the CPU here after LPFC_MAX_WORKER_ITERATION
516 * consecutive iterations.
517 */
518 if (phba->work_found >= LPFC_MAX_WORKER_ITERATION) {
519 phba->work_found = 0;
520 schedule();
521 }
dea31012005-04-17 16:05:31 -0500522 }
523 phba->work_wait = NULL;
524 return 0;
525}
526
527/*
528 * This is only called to handle FC worker events. Since this a rare
529 * occurance, we allocate a struct lpfc_work_evt structure here instead of
530 * embedding it in the IOCB.
531 */
532int
James Smart2e0fef82007-06-17 19:56:36 -0500533lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500534 uint32_t evt)
535{
536 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500537 unsigned long flags;
dea31012005-04-17 16:05:31 -0500538
539 /*
540 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
541 * be queued to worker thread for processing
542 */
James Smart92d7f7b2007-06-17 19:56:38 -0500543 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500544 if (!evtp)
545 return 0;
546
547 evtp->evt_arg1 = arg1;
548 evtp->evt_arg2 = arg2;
549 evtp->evt = evt;
550
James Smarted957682007-06-17 19:56:37 -0500551 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400552 list_add_tail(&evtp->evt_listp, &phba->work_list);
dea31012005-04-17 16:05:31 -0500553 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -0500554 lpfc_worker_wake_up(phba);
James Smarted957682007-06-17 19:56:37 -0500555 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500556
557 return 1;
558}
559
James Smart92d7f7b2007-06-17 19:56:38 -0500560void
561lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
562{
James Smart09372822008-01-11 01:52:54 -0500563 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500564 struct lpfc_hba *phba = vport->phba;
565 struct lpfc_nodelist *ndlp, *next_ndlp;
566 int rc;
567
568 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
569 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
570 continue;
571
James Smart98c9ea52007-10-27 13:37:33 -0400572 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
573 ((vport->port_type == LPFC_NPIV_PORT) &&
574 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500575 lpfc_unreg_rpi(vport, ndlp);
576
577 /* Leave Fabric nodes alone on link down */
578 if (!remove && ndlp->nlp_type & NLP_FABRIC)
579 continue;
580 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
581 remove
582 ? NLP_EVT_DEVICE_RM
583 : NLP_EVT_DEVICE_RECOVERY);
584 }
585 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
586 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500587 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500588 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500589 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500590 }
591}
592
James Smart87af33f2007-10-27 13:37:43 -0400593void
James Smart98c9ea52007-10-27 13:37:33 -0400594lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500595{
James Smart92d7f7b2007-06-17 19:56:38 -0500596 /* Cleanup any outstanding RSCN activity */
597 lpfc_els_flush_rscn(vport);
598
599 /* Cleanup any outstanding ELS commands */
600 lpfc_els_flush_cmd(vport);
601
602 lpfc_cleanup_rpis(vport, 0);
603
James Smart92d7f7b2007-06-17 19:56:38 -0500604 /* Turn off discovery timer if its running */
605 lpfc_can_disctmo(vport);
606}
607
James Smart98c9ea52007-10-27 13:37:33 -0400608static void
609lpfc_linkdown_port(struct lpfc_vport *vport)
610{
611 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
612
613 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
614
615 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
616 "Link Down: state:x%x rtry:x%x flg:x%x",
617 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
618
619 lpfc_port_link_failure(vport);
620
621}
622
dea31012005-04-17 16:05:31 -0500623int
James Smart685f0bf2007-04-25 09:53:08 -0400624lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500625{
James Smart2e0fef82007-06-17 19:56:36 -0500626 struct lpfc_vport *vport = phba->pport;
627 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400628 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400629 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400630 int i;
dea31012005-04-17 16:05:31 -0500631
James Smart2e0fef82007-06-17 19:56:36 -0500632 if (phba->link_state == LPFC_LINK_DOWN) {
633 return 0;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500634 }
James Smart2e0fef82007-06-17 19:56:36 -0500635 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500636 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500637 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500638 phba->pport->fc_flag &= ~FC_LBIT;
639 }
James Smart2e0fef82007-06-17 19:56:36 -0500640 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400641 vports = lpfc_create_vport_work_array(phba);
642 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500643 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400644 /* Issue a LINK DOWN event to all nodes */
645 lpfc_linkdown_port(vports[i]);
646 }
James Smart09372822008-01-11 01:52:54 -0500647 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500648 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500649 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
650 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500651 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500652 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500653 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400654 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500655 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500656 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500657 }
658 }
659
dea31012005-04-17 16:05:31 -0500660 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500661 if (phba->pport->fc_flag & FC_PT2PT) {
662 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500663 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
664 if (mb) {
dea31012005-04-17 16:05:31 -0500665 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500666 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500667 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400668 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500669 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500670 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500671 }
672 }
James Smart2e0fef82007-06-17 19:56:36 -0500673 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500674 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500675 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500676 }
James Smart2e0fef82007-06-17 19:56:36 -0500677
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500678 return 0;
dea31012005-04-17 16:05:31 -0500679}
680
James Smart92d7f7b2007-06-17 19:56:38 -0500681static void
682lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500683{
James Smart92d7f7b2007-06-17 19:56:38 -0500684 struct lpfc_nodelist *ndlp;
685
686 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
687 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
688 continue;
689
690 if (ndlp->nlp_type & NLP_FABRIC) {
691 /* On Linkup its safe to clean up the ndlp
692 * from Fabric connections.
693 */
694 if (ndlp->nlp_DID != Fabric_DID)
695 lpfc_unreg_rpi(vport, ndlp);
696 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
697 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
698 /* Fail outstanding IO now since device is
699 * marked for PLOGI.
700 */
701 lpfc_unreg_rpi(vport, ndlp);
702 }
703 }
704}
705
706static void
707lpfc_linkup_port(struct lpfc_vport *vport)
708{
709 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500710 struct lpfc_hba *phba = vport->phba;
711
712 if ((vport->load_flag & FC_UNLOADING) != 0)
713 return;
714
James Smart858c9f62007-06-17 19:56:39 -0500715 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
716 "Link Up: top:x%x speed:x%x flg:x%x",
717 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
718
James Smart92d7f7b2007-06-17 19:56:38 -0500719 /* If NPIV is not enabled, only bring the physical port up */
720 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
721 (vport != phba->pport))
722 return;
dea31012005-04-17 16:05:31 -0500723
James Smart2e0fef82007-06-17 19:56:36 -0500724 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400725
James Smart2e0fef82007-06-17 19:56:36 -0500726 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500727 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
728 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
729 vport->fc_flag |= FC_NDISC_ACTIVE;
730 vport->fc_ns_retry = 0;
731 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500732
James Smart92d7f7b2007-06-17 19:56:38 -0500733 if (vport->fc_flag & FC_LBIT)
734 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500735
James Smart92d7f7b2007-06-17 19:56:38 -0500736}
737
738static int
739lpfc_linkup(struct lpfc_hba *phba)
740{
James Smart549e55c2007-08-02 11:09:51 -0400741 struct lpfc_vport **vports;
742 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500743
744 phba->link_state = LPFC_LINK_UP;
745
746 /* Unblock fabric iocbs if they are blocked */
747 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
748 del_timer_sync(&phba->fabric_block_timer);
749
James Smart549e55c2007-08-02 11:09:51 -0400750 vports = lpfc_create_vport_work_array(phba);
751 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500752 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400753 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500754 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500755 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
756 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500757
758 return 0;
759}
760
761/*
762 * This routine handles processing a CLEAR_LA mailbox
763 * command upon completion. It is setup in the LPFC_MBOXQ
764 * as the completion routine when the command is
765 * handed off to the SLI layer.
766 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100767static void
James Smart2e0fef82007-06-17 19:56:36 -0500768lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500769{
James Smart2e0fef82007-06-17 19:56:36 -0500770 struct lpfc_vport *vport = pmb->vport;
771 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
772 struct lpfc_sli *psli = &phba->sli;
773 MAILBOX_t *mb = &pmb->mb;
dea31012005-04-17 16:05:31 -0500774 uint32_t control;
775
dea31012005-04-17 16:05:31 -0500776 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500777 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500778 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
779 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
780
781 /* Check for error */
782 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500783 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400784 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
785 "0320 CLEAR_LA mbxStatus error x%x hba "
786 "state x%x\n",
787 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500788 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500789 goto out;
790 }
791
James Smart92d7f7b2007-06-17 19:56:38 -0500792 if (vport->port_type == LPFC_PHYSICAL_PORT)
793 phba->link_state = LPFC_HBA_READY;
794
795 spin_lock_irq(&phba->hbalock);
796 psli->sli_flag |= LPFC_PROCESS_LA;
797 control = readl(phba->HCregaddr);
798 control |= HC_LAINT_ENA;
799 writel(control, phba->HCregaddr);
800 readl(phba->HCregaddr); /* flush */
801 spin_unlock_irq(&phba->hbalock);
802 return;
dea31012005-04-17 16:05:31 -0500803
James Smart2e0fef82007-06-17 19:56:36 -0500804 vport->num_disc_nodes = 0;
805 /* go thru NPR nodes and issue ELS PLOGIs */
806 if (vport->fc_npr_cnt)
807 lpfc_els_disc_plogi(vport);
808
809 if (!vport->num_disc_nodes) {
810 spin_lock_irq(shost->host_lock);
811 vport->fc_flag &= ~FC_NDISC_ACTIVE;
812 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500813 }
814
James Smart2e0fef82007-06-17 19:56:36 -0500815 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -0500816
817out:
818 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400819 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
820 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500821 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500822
James Smart2e0fef82007-06-17 19:56:36 -0500823 spin_lock_irq(shost->host_lock);
824 vport->fc_flag &= ~(FC_ABORT_DISCOVERY | FC_ESTABLISH_LINK);
825 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500826
827 del_timer_sync(&phba->fc_estabtmo);
828
James Smart2e0fef82007-06-17 19:56:36 -0500829 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500830
831 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500832
833 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500834 psli->sli_flag |= LPFC_PROCESS_LA;
835 control = readl(phba->HCregaddr);
836 control |= HC_LAINT_ENA;
837 writel(control, phba->HCregaddr);
838 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500839 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500840
841 return;
842}
843
James Smart2e0fef82007-06-17 19:56:36 -0500844
dea31012005-04-17 16:05:31 -0500845static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500846lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500847{
James Smart2e0fef82007-06-17 19:56:36 -0500848 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500849
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500850 if (pmb->mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500851 goto out;
dea31012005-04-17 16:05:31 -0500852
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500853 mempool_free(pmb, phba->mbox_mem_pool);
854
855 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500856 vport->fc_flag & FC_PUBLIC_LOOP &&
857 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500858 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500859 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500860 * LPFC_LOCAL_CFG_LINK while waiting for FAN
861 */
James Smart2e0fef82007-06-17 19:56:36 -0500862 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500863 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500864 }
dea31012005-04-17 16:05:31 -0500865
James Smart2e0fef82007-06-17 19:56:36 -0500866 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500867 * LPFC_FLOGI while waiting for FLOGI cmpl
868 */
James Smart92d7f7b2007-06-17 19:56:38 -0500869 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500870 lpfc_initial_flogi(vport);
871 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500872 return;
dea31012005-04-17 16:05:31 -0500873
874out:
James Smarte8b62012007-08-02 11:10:09 -0400875 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
876 "0306 CONFIG_LINK mbxStatus error x%x "
877 "HBA state x%x\n",
878 pmb->mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500879 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500880
881 lpfc_linkdown(phba);
882
James Smarte8b62012007-08-02 11:10:09 -0400883 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
884 "0200 CONFIG_LINK bad hba state x%x\n",
885 vport->port_state);
dea31012005-04-17 16:05:31 -0500886
James Smart92d7f7b2007-06-17 19:56:38 -0500887 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500888 return;
889}
890
891static void
James Smart2e0fef82007-06-17 19:56:36 -0500892lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500893{
dea31012005-04-17 16:05:31 -0500894 MAILBOX_t *mb = &pmb->mb;
895 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500896 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500897
898
899 /* Check for error */
900 if (mb->mbxStatus) {
901 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400902 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
903 "0319 READ_SPARAM mbxStatus error x%x "
904 "hba state x%x>\n",
905 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -0500906 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -0500907 goto out;
908 }
909
James Smart2e0fef82007-06-17 19:56:36 -0500910 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -0500911 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -0500912 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500913 u64_to_wwn(phba->cfg_soft_wwnn,
914 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400915 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500916 u64_to_wwn(phba->cfg_soft_wwpn,
917 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -0500918 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
919 sizeof(vport->fc_nodename));
920 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
921 sizeof(vport->fc_portname));
922 if (vport->port_type == LPFC_PHYSICAL_PORT) {
923 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
924 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
925 }
926
dea31012005-04-17 16:05:31 -0500927 lpfc_mbuf_free(phba, mp->virt, mp->phys);
928 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -0500929 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500930 return;
931
932out:
933 pmb->context1 = NULL;
934 lpfc_mbuf_free(phba, mp->virt, mp->phys);
935 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -0500936 lpfc_issue_clear_la(phba, vport);
937 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500938 return;
939}
940
941static void
James Smart92d7f7b2007-06-17 19:56:38 -0500942lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -0500943{
James Smart92d7f7b2007-06-17 19:56:38 -0500944 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500945 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
James Smart2e0fef82007-06-17 19:56:36 -0500946 int i;
James Smart14691152006-12-02 13:34:28 -0500947 struct lpfc_dmabuf *mp;
948 int rc;
949
dea31012005-04-17 16:05:31 -0500950 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
951 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
952
James Smart92d7f7b2007-06-17 19:56:38 -0500953 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500954 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -0500955 case LA_1GHZ_LINK:
956 phba->fc_linkspeed = LA_1GHZ_LINK;
957 break;
958 case LA_2GHZ_LINK:
959 phba->fc_linkspeed = LA_2GHZ_LINK;
960 break;
961 case LA_4GHZ_LINK:
962 phba->fc_linkspeed = LA_4GHZ_LINK;
963 break;
964 case LA_8GHZ_LINK:
965 phba->fc_linkspeed = LA_8GHZ_LINK;
966 break;
967 default:
968 phba->fc_linkspeed = LA_UNKNW_LINK;
969 break;
dea31012005-04-17 16:05:31 -0500970 }
971
972 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -0500973 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -0500974
975 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -0500976 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -0500977
James Smart92d7f7b2007-06-17 19:56:38 -0500978 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -0500979 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -0500980 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500981
James Smart2e0fef82007-06-17 19:56:36 -0500982 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -0500983 i = la->un.lilpBde64.tus.f.bdeSize;
984
985 if (i == 0) {
986 phba->alpa_map[0] = 0;
987 } else {
James Smarte8b62012007-08-02 11:10:09 -0400988 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -0500989 int numalpa, j, k;
990 union {
991 uint8_t pamap[16];
992 struct {
993 uint32_t wd1;
994 uint32_t wd2;
995 uint32_t wd3;
996 uint32_t wd4;
997 } pa;
998 } un;
999 numalpa = phba->alpa_map[0];
1000 j = 0;
1001 while (j < numalpa) {
1002 memset(un.pamap, 0, 16);
1003 for (k = 1; j < numalpa; k++) {
1004 un.pamap[k - 1] =
1005 phba->alpa_map[j + 1];
1006 j++;
1007 if (k == 16)
1008 break;
1009 }
1010 /* Link Up Event ALPA map */
1011 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05001012 KERN_WARNING,
1013 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001014 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05001015 "ALPA map Data: x%x "
1016 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001017 un.pa.wd1, un.pa.wd2,
1018 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05001019 }
1020 }
1021 }
1022 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05001023 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04001024 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05001025 (phba->sli_rev == 3))
1026 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1027 }
James Smart2e0fef82007-06-17 19:56:36 -05001028 vport->fc_myDID = phba->fc_pref_DID;
1029 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001030 }
James Smart92d7f7b2007-06-17 19:56:38 -05001031 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001032
1033 lpfc_linkup(phba);
1034 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001035 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05001036 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05001037 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04001038 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05001039 if (rc == MBX_NOT_FINISHED) {
1040 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1041 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1042 kfree(mp);
1043 mempool_free(sparam_mbox, phba->mbox_mem_pool);
1044 if (cfglink_mbox)
1045 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001046 goto out;
James Smart14691152006-12-02 13:34:28 -05001047 }
dea31012005-04-17 16:05:31 -05001048 }
1049
1050 if (cfglink_mbox) {
James Smart2e0fef82007-06-17 19:56:36 -05001051 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05001052 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05001053 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001054 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04001055 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001056 if (rc != MBX_NOT_FINISHED)
1057 return;
1058 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001059 }
James Smart92d7f7b2007-06-17 19:56:38 -05001060out:
1061 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001062 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1063 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1064 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05001065 lpfc_issue_clear_la(phba, vport);
1066 return;
dea31012005-04-17 16:05:31 -05001067}
1068
1069static void
James Smart2e0fef82007-06-17 19:56:36 -05001070lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1071{
dea31012005-04-17 16:05:31 -05001072 uint32_t control;
1073 struct lpfc_sli *psli = &phba->sli;
1074
1075 lpfc_linkdown(phba);
1076
1077 /* turn on Link Attention interrupts - no CLEAR_LA needed */
James Smart2e0fef82007-06-17 19:56:36 -05001078 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001079 psli->sli_flag |= LPFC_PROCESS_LA;
1080 control = readl(phba->HCregaddr);
1081 control |= HC_LAINT_ENA;
1082 writel(control, phba->HCregaddr);
1083 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001084 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001085}
1086
1087/*
1088 * This routine handles processing a READ_LA mailbox
1089 * command upon completion. It is setup in the LPFC_MBOXQ
1090 * as the completion routine when the command is
1091 * handed off to the SLI layer.
1092 */
1093void
James Smart2e0fef82007-06-17 19:56:36 -05001094lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001095{
James Smart2e0fef82007-06-17 19:56:36 -05001096 struct lpfc_vport *vport = pmb->vport;
1097 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05001098 READ_LA_VAR *la;
1099 MAILBOX_t *mb = &pmb->mb;
1100 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1101
1102 /* Check for error */
1103 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05001104 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001105 "1307 READ_LA mbox error x%x state x%x\n",
1106 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001107 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001108 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001109 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1110 }
1111
1112 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1113
1114 memcpy(&phba->alpa_map[0], mp->virt, 128);
1115
James Smart2e0fef82007-06-17 19:56:36 -05001116 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001117 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05001118 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001119 else
James Smart2e0fef82007-06-17 19:56:36 -05001120 vport->fc_flag &= ~FC_BYPASSED_MODE;
1121 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001122
dea31012005-04-17 16:05:31 -05001123 if (((phba->fc_eventTag + 1) < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05001124 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05001125 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05001126 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05001127 if (phba->fc_eventTag != 0)
1128 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001129 }
dea31012005-04-17 16:05:31 -05001130
1131 phba->fc_eventTag = la->eventTag;
1132
1133 if (la->attType == AT_LINK_UP) {
1134 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05001135 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart5b8bd0c2007-04-25 09:52:49 -04001136 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001137 "1306 Link Up Event in loop back mode "
1138 "x%x received Data: x%x x%x x%x x%x\n",
1139 la->eventTag, phba->fc_eventTag,
1140 la->granted_AL_PA, la->UlnkSpeed,
1141 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001142 } else {
1143 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001144 "1303 Link Up Event x%x received "
1145 "Data: x%x x%x x%x x%x\n",
1146 la->eventTag, phba->fc_eventTag,
1147 la->granted_AL_PA, la->UlnkSpeed,
1148 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001149 }
James Smart92d7f7b2007-06-17 19:56:38 -05001150 lpfc_mbx_process_link_up(phba, la);
dea31012005-04-17 16:05:31 -05001151 } else {
1152 phba->fc_stat.LinkDown++;
1153 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001154 "1305 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05001155 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001156 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05001157 phba->pport->port_state, vport->fc_flag);
dea31012005-04-17 16:05:31 -05001158 lpfc_mbx_issue_link_down(phba);
1159 }
1160
1161lpfc_mbx_cmpl_read_la_free_mbuf:
1162 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1163 kfree(mp);
1164 mempool_free(pmb, phba->mbox_mem_pool);
1165 return;
1166}
1167
1168/*
1169 * This routine handles processing a REG_LOGIN mailbox
1170 * command upon completion. It is setup in the LPFC_MBOXQ
1171 * as the completion routine when the command is
1172 * handed off to the SLI layer.
1173 */
1174void
James Smart2e0fef82007-06-17 19:56:36 -05001175lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001176{
James Smart2e0fef82007-06-17 19:56:36 -05001177 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001178 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05001179 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001180
dea31012005-04-17 16:05:31 -05001181 pmb->context1 = NULL;
1182
1183 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05001184 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05001185 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1186 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001187 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05001188 /* decrement the node reference count held for this callback
1189 * function.
1190 */
James Smart329f9bc2007-04-25 09:53:01 -04001191 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001192
1193 return;
1194}
1195
James Smart92d7f7b2007-06-17 19:56:38 -05001196static void
1197lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1198{
1199 MAILBOX_t *mb = &pmb->mb;
1200 struct lpfc_vport *vport = pmb->vport;
1201 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1202
1203 switch (mb->mbxStatus) {
1204 case 0x0011:
1205 case 0x0020:
1206 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04001207 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1208 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
1209 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001210 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001211 }
1212 vport->unreg_vpi_cmpl = VPORT_OK;
1213 mempool_free(pmb, phba->mbox_mem_pool);
1214 /*
1215 * This shost reference might have been taken at the beginning of
1216 * lpfc_vport_delete()
1217 */
1218 if (vport->load_flag & FC_UNLOADING)
1219 scsi_host_put(shost);
1220}
1221
1222void
1223lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1224{
1225 struct lpfc_hba *phba = vport->phba;
1226 LPFC_MBOXQ_t *mbox;
1227 int rc;
1228
1229 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1230 if (!mbox)
1231 return;
1232
1233 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1234 mbox->vport = vport;
1235 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04001236 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001237 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001238 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1239 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001240 mempool_free(mbox, phba->mbox_mem_pool);
1241 vport->unreg_vpi_cmpl = VPORT_ERROR;
1242 }
1243}
1244
1245static void
1246lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1247{
1248 struct lpfc_vport *vport = pmb->vport;
1249 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1250 MAILBOX_t *mb = &pmb->mb;
1251
1252 switch (mb->mbxStatus) {
1253 case 0x0011:
1254 case 0x9601:
1255 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04001256 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1257 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
1258 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001259 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1260 spin_lock_irq(shost->host_lock);
1261 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1262 spin_unlock_irq(shost->host_lock);
1263 vport->fc_myDID = 0;
1264 goto out;
1265 }
James Smart92d7f7b2007-06-17 19:56:38 -05001266
1267 vport->num_disc_nodes = 0;
1268 /* go thru NPR list and issue ELS PLOGIs */
1269 if (vport->fc_npr_cnt)
1270 lpfc_els_disc_plogi(vport);
1271
1272 if (!vport->num_disc_nodes) {
1273 spin_lock_irq(shost->host_lock);
1274 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1275 spin_unlock_irq(shost->host_lock);
1276 lpfc_can_disctmo(vport);
1277 }
1278 vport->port_state = LPFC_VPORT_READY;
1279
1280out:
1281 mempool_free(pmb, phba->mbox_mem_pool);
1282 return;
1283}
1284
dea31012005-04-17 16:05:31 -05001285/*
1286 * This routine handles processing a Fabric REG_LOGIN mailbox
1287 * command upon completion. It is setup in the LPFC_MBOXQ
1288 * as the completion routine when the command is
1289 * handed off to the SLI layer.
1290 */
1291void
James Smart2e0fef82007-06-17 19:56:36 -05001292lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001293{
James Smart92d7f7b2007-06-17 19:56:38 -05001294 struct lpfc_vport *vport = pmb->vport;
James Smart2e0fef82007-06-17 19:56:36 -05001295 MAILBOX_t *mb = &pmb->mb;
1296 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05001297 struct lpfc_nodelist *ndlp;
James Smart549e55c2007-08-02 11:09:51 -04001298 struct lpfc_vport **vports;
1299 int i;
dea31012005-04-17 16:05:31 -05001300
James Smart549e55c2007-08-02 11:09:51 -04001301 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04001302 pmb->context1 = NULL;
1303 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05001304 if (mb->mbxStatus) {
1305 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1306 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001307 mempool_free(pmb, phba->mbox_mem_pool);
1308 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001309
James Smart92d7f7b2007-06-17 19:56:38 -05001310 if (phba->fc_topology == TOPOLOGY_LOOP) {
1311 /* FLOGI failed, use loop map to make discovery list */
1312 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001313
James Smart92d7f7b2007-06-17 19:56:38 -05001314 /* Start discovery */
1315 lpfc_disc_start(vport);
1316 return;
1317 }
1318
1319 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001320 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1321 "0258 Register Fabric login error: 0x%x\n",
1322 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001323 return;
1324 }
1325
dea31012005-04-17 16:05:31 -05001326 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001327 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001328 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001329
James Smart329f9bc2007-04-25 09:53:01 -04001330 lpfc_nlp_put(ndlp); /* Drop the reference from the mbox */
1331
James Smart2e0fef82007-06-17 19:56:36 -05001332 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart549e55c2007-08-02 11:09:51 -04001333 vports = lpfc_create_vport_work_array(phba);
1334 if (vports != NULL)
1335 for(i = 0;
James Smart09372822008-01-11 01:52:54 -05001336 i <= phba->max_vpi && vports[i] != NULL;
James Smart549e55c2007-08-02 11:09:51 -04001337 i++) {
1338 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1339 continue;
1340 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1341 lpfc_initial_fdisc(vports[i]);
1342 else if (phba->sli3_options &
1343 LPFC_SLI3_NPIV_ENABLED) {
1344 lpfc_vport_set_state(vports[i],
1345 FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -04001346 lpfc_printf_vlog(vport, KERN_ERR,
1347 LOG_ELS,
1348 "0259 No NPIV "
1349 "Fabric support\n");
James Smart549e55c2007-08-02 11:09:51 -04001350 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001351 }
James Smart09372822008-01-11 01:52:54 -05001352 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001353 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05001354 }
1355
1356 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1357 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001358 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001359 return;
1360}
1361
1362/*
1363 * This routine handles processing a NameServer REG_LOGIN mailbox
1364 * command upon completion. It is setup in the LPFC_MBOXQ
1365 * as the completion routine when the command is
1366 * handed off to the SLI layer.
1367 */
1368void
James Smart2e0fef82007-06-17 19:56:36 -05001369lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001370{
James Smart2e0fef82007-06-17 19:56:36 -05001371 MAILBOX_t *mb = &pmb->mb;
1372 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1373 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1374 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001375
1376 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05001377out:
James Smartfa4066b2008-01-11 01:53:27 -05001378 /* decrement the node reference count held for this
1379 * callback function.
1380 */
James Smart329f9bc2007-04-25 09:53:01 -04001381 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001382 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1383 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04001384 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04001385
1386 /* If no other thread is using the ndlp, free it */
1387 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05001388
James Smart92d7f7b2007-06-17 19:56:38 -05001389 if (phba->fc_topology == TOPOLOGY_LOOP) {
1390 /*
1391 * RegLogin failed, use loop map to make discovery
1392 * list
1393 */
1394 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001395
James Smart92d7f7b2007-06-17 19:56:38 -05001396 /* Start discovery */
1397 lpfc_disc_start(vport);
1398 return;
1399 }
1400 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001401 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1402 "0260 Register NameServer error: 0x%x\n",
1403 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001404 return;
1405 }
1406
1407 pmb->context1 = NULL;
1408
dea31012005-04-17 16:05:31 -05001409 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001410 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001411 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001412
James Smart2e0fef82007-06-17 19:56:36 -05001413 if (vport->port_state < LPFC_VPORT_READY) {
1414 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05001415 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1416 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1417 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1418 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1419 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1420
1421 /* Issue SCR just before NameServer GID_FT Query */
1422 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05001423 }
1424
James Smart2e0fef82007-06-17 19:56:36 -05001425 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05001426 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05001427 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05001428 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05001429 goto out;
dea31012005-04-17 16:05:31 -05001430 }
1431
James Smartfa4066b2008-01-11 01:53:27 -05001432 /* decrement the node reference count held for this
1433 * callback function.
1434 */
James Smart329f9bc2007-04-25 09:53:01 -04001435 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001436 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1437 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001438 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001439
1440 return;
1441}
1442
1443static void
James Smart2e0fef82007-06-17 19:56:36 -05001444lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001445{
James Smart2e0fef82007-06-17 19:56:36 -05001446 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1447 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05001448 struct lpfc_rport_data *rdata;
1449 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05001450 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001451
1452 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001453 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1454 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001455 rport_ids.port_id = ndlp->nlp_DID;
1456 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05001457
James Smart329f9bc2007-04-25 09:53:01 -04001458 /*
1459 * We leave our node pointer in rport->dd_data when we unregister a
1460 * FCP target port. But fc_remote_port_add zeros the space to which
1461 * rport->dd_data points. So, if we're reusing a previously
1462 * registered port, drop the reference that we took the last time we
1463 * registered the port.
1464 */
1465 if (ndlp->rport && ndlp->rport->dd_data &&
James Smart92d7f7b2007-06-17 19:56:38 -05001466 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp) {
James Smart329f9bc2007-04-25 09:53:01 -04001467 lpfc_nlp_put(ndlp);
1468 }
James Smart858c9f62007-06-17 19:56:39 -05001469
1470 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1471 "rport add: did:x%x flg:x%x type x%x",
1472 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1473
James Smart2e0fef82007-06-17 19:56:36 -05001474 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04001475 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05001476 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1477 "Warning: fc_remote_port_add failed\n");
1478 return;
1479 }
1480
1481 /* initialize static port data */
1482 rport->maxframe_size = ndlp->nlp_maxframe;
1483 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05001484 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04001485 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001486
1487 if (ndlp->nlp_type & NLP_FCP_TARGET)
1488 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1489 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1490 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1491
1492
1493 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1494 fc_remote_port_rolechg(rport, rport_ids.roles);
1495
James Smart071fbd3d2006-04-15 11:53:20 -04001496 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001497 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04001498 ndlp->nlp_sid = rport->scsi_target_id;
1499 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001500 return;
1501}
1502
1503static void
James Smart2e0fef82007-06-17 19:56:36 -05001504lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001505{
1506 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04001507
James Smart858c9f62007-06-17 19:56:39 -05001508 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
1509 "rport delete: did:x%x flg:x%x type x%x",
1510 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1511
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001512 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05001513
1514 return;
1515}
1516
James Smartde0c5b32007-04-25 09:52:27 -04001517static void
James Smart2e0fef82007-06-17 19:56:36 -05001518lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04001519{
James Smart2e0fef82007-06-17 19:56:36 -05001520 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1521
1522 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001523 switch (state) {
1524 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001525 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001526 break;
1527 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001528 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001529 break;
1530 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001531 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001532 break;
1533 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001534 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001535 break;
1536 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001537 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001538 break;
1539 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001540 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001541 break;
1542 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001543 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001544 break;
1545 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001546 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001547 break;
1548 }
James Smart2e0fef82007-06-17 19:56:36 -05001549 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001550}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001551
James Smartde0c5b32007-04-25 09:52:27 -04001552static void
James Smart2e0fef82007-06-17 19:56:36 -05001553lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04001554 int old_state, int new_state)
1555{
James Smart2e0fef82007-06-17 19:56:36 -05001556 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1557
James Smartde0c5b32007-04-25 09:52:27 -04001558 if (new_state == NLP_STE_UNMAPPED_NODE) {
1559 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1560 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1561 ndlp->nlp_type |= NLP_FC_NODE;
1562 }
1563 if (new_state == NLP_STE_MAPPED_NODE)
1564 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1565 if (new_state == NLP_STE_NPR_NODE)
1566 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05001567
James Smartde0c5b32007-04-25 09:52:27 -04001568 /* Transport interface */
1569 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1570 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001571 vport->phba->nport_event_cnt++;
1572 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001573 }
dea31012005-04-17 16:05:31 -05001574
James Smartde0c5b32007-04-25 09:52:27 -04001575 if (new_state == NLP_STE_MAPPED_NODE ||
1576 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05001577 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05001578 /*
1579 * Tell the fc transport about the port, if we haven't
1580 * already. If we have, and it's a scsi entity, be
1581 * sure to unblock any attached scsi devices
1582 */
1583 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001584 }
James Smart858c9f62007-06-17 19:56:39 -05001585 /*
1586 * if we added to Mapped list, but the remote port
1587 * registration failed or assigned a target id outside
1588 * our presentable range - move the node to the
1589 * Unmapped List
1590 */
James Smartde0c5b32007-04-25 09:52:27 -04001591 if (new_state == NLP_STE_MAPPED_NODE &&
1592 (!ndlp->rport ||
1593 ndlp->rport->scsi_target_id == -1 ||
1594 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05001595 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001596 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05001597 spin_unlock_irq(shost->host_lock);
1598 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001599 }
James Smartde0c5b32007-04-25 09:52:27 -04001600}
1601
James Smart685f0bf2007-04-25 09:53:08 -04001602static char *
1603lpfc_nlp_state_name(char *buffer, size_t size, int state)
1604{
1605 static char *states[] = {
1606 [NLP_STE_UNUSED_NODE] = "UNUSED",
1607 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1608 [NLP_STE_ADISC_ISSUE] = "ADISC",
1609 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1610 [NLP_STE_PRLI_ISSUE] = "PRLI",
1611 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1612 [NLP_STE_MAPPED_NODE] = "MAPPED",
1613 [NLP_STE_NPR_NODE] = "NPR",
1614 };
1615
James Smart311464e2007-08-02 11:10:37 -04001616 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04001617 strlcpy(buffer, states[state], size);
1618 else
1619 snprintf(buffer, size, "unknown (%d)", state);
1620 return buffer;
1621}
1622
James Smartde0c5b32007-04-25 09:52:27 -04001623void
James Smart2e0fef82007-06-17 19:56:36 -05001624lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1625 int state)
James Smartde0c5b32007-04-25 09:52:27 -04001626{
James Smart2e0fef82007-06-17 19:56:36 -05001627 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04001628 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04001629 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04001630
James Smarte8b62012007-08-02 11:10:09 -04001631 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1632 "0904 NPort state transition x%06x, %s -> %s\n",
1633 ndlp->nlp_DID,
1634 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1635 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05001636
1637 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1638 "node statechg did:x%x old:%d ste:%d",
1639 ndlp->nlp_DID, old_state, state);
1640
James Smartde0c5b32007-04-25 09:52:27 -04001641 if (old_state == NLP_STE_NPR_NODE &&
1642 (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
1643 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001644 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001645 if (old_state == NLP_STE_UNMAPPED_NODE) {
1646 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1647 ndlp->nlp_type &= ~NLP_FC_NODE;
1648 }
1649
James Smart685f0bf2007-04-25 09:53:08 -04001650 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05001651 spin_lock_irq(shost->host_lock);
1652 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1653 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001654 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05001655 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04001656
1657 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05001658 lpfc_nlp_counters(vport, state, 1);
1659 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04001660}
1661
1662void
James Smart2e0fef82007-06-17 19:56:36 -05001663lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001664{
James Smart2e0fef82007-06-17 19:56:36 -05001665 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1666
James Smartde0c5b32007-04-25 09:52:27 -04001667 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
James Smart2e0fef82007-06-17 19:56:36 -05001668 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001669 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05001670 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1671 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001672 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05001673 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05001674 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
1675 NLP_STE_UNUSED_NODE);
James Smartde0c5b32007-04-25 09:52:27 -04001676}
1677
1678void
James Smart2e0fef82007-06-17 19:56:36 -05001679lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001680{
James Smart87af33f2007-10-27 13:37:43 -04001681 /*
James Smartfa4066b2008-01-11 01:53:27 -05001682 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04001683 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05001684 * the ndlp from the vport. The ndlp marked as UNUSED on the list
1685 * until ALL other outstanding threads have completed. We check
1686 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04001687 */
James Smartfa4066b2008-01-11 01:53:27 -05001688 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1689 return;
James Smart51ef4c22007-08-02 11:10:31 -04001690 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04001691 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04001692 return;
dea31012005-04-17 16:05:31 -05001693}
1694
1695/*
1696 * Start / ReStart rescue timer for Discovery / RSCN handling
1697 */
1698void
James Smart2e0fef82007-06-17 19:56:36 -05001699lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001700{
James Smart2e0fef82007-06-17 19:56:36 -05001701 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1702 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001703 uint32_t tmo;
1704
James Smart2e0fef82007-06-17 19:56:36 -05001705 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001706 /* For FAN, timeout should be greater then edtov */
1707 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1708 } else {
1709 /* Normal discovery timeout should be > then ELS/CT timeout
1710 * FC spec states we need 3 * ratov for CT requests
1711 */
1712 tmo = ((phba->fc_ratov * 3) + 3);
1713 }
dea31012005-04-17 16:05:31 -05001714
James Smart858c9f62007-06-17 19:56:39 -05001715
1716 if (!timer_pending(&vport->fc_disctmo)) {
1717 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1718 "set disc timer: tmo:x%x state:x%x flg:x%x",
1719 tmo, vport->port_state, vport->fc_flag);
1720 }
1721
James Smart2e0fef82007-06-17 19:56:36 -05001722 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1723 spin_lock_irq(shost->host_lock);
1724 vport->fc_flag |= FC_DISC_TMO;
1725 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001726
1727 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001728 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1729 "0247 Start Discovery Timer state x%x "
1730 "Data: x%x x%lx x%x x%x\n",
1731 vport->port_state, tmo,
1732 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1733 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05001734
1735 return;
1736}
1737
1738/*
1739 * Cancel rescue timer for Discovery / RSCN handling
1740 */
1741int
James Smart2e0fef82007-06-17 19:56:36 -05001742lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001743{
James Smart2e0fef82007-06-17 19:56:36 -05001744 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001745 unsigned long iflags;
1746
James Smart858c9f62007-06-17 19:56:39 -05001747 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1748 "can disc timer: state:x%x rtry:x%x flg:x%x",
1749 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
1750
dea31012005-04-17 16:05:31 -05001751 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05001752 if (vport->fc_flag & FC_DISC_TMO) {
1753 spin_lock_irqsave(shost->host_lock, iflags);
1754 vport->fc_flag &= ~FC_DISC_TMO;
1755 spin_unlock_irqrestore(shost->host_lock, iflags);
1756 del_timer_sync(&vport->fc_disctmo);
1757 spin_lock_irqsave(&vport->work_port_lock, iflags);
1758 vport->work_port_events &= ~WORKER_DISC_TMO;
1759 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05001760 }
1761
1762 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001763 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1764 "0248 Cancel Discovery Timer state x%x "
1765 "Data: x%x x%x x%x\n",
1766 vport->port_state, vport->fc_flag,
1767 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001768 return 0;
dea31012005-04-17 16:05:31 -05001769}
1770
1771/*
1772 * Check specified ring for outstanding IOCB on the SLI queue
1773 * Return true if iocb matches the specified nport
1774 */
1775int
James Smart2e0fef82007-06-17 19:56:36 -05001776lpfc_check_sli_ndlp(struct lpfc_hba *phba,
1777 struct lpfc_sli_ring *pring,
1778 struct lpfc_iocbq *iocb,
1779 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001780{
James Smart2e0fef82007-06-17 19:56:36 -05001781 struct lpfc_sli *psli = &phba->sli;
1782 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05001783 struct lpfc_vport *vport = ndlp->vport;
1784
1785 if (iocb->vport != vport)
1786 return 0;
1787
dea31012005-04-17 16:05:31 -05001788 if (pring->ringno == LPFC_ELS_RING) {
1789 switch (icmd->ulpCommand) {
1790 case CMD_GEN_REQUEST64_CR:
1791 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001792 return 1;
dea31012005-04-17 16:05:31 -05001793 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04001794 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1795 return 1;
dea31012005-04-17 16:05:31 -05001796 case CMD_XMIT_ELS_RSP64_CX:
1797 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001798 return 1;
dea31012005-04-17 16:05:31 -05001799 }
James Smarta4bc3372006-12-02 13:34:16 -05001800 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05001801
1802 } else if (pring->ringno == psli->fcp_ring) {
1803 /* Skip match check if waiting to relogin to FCP target */
1804 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001805 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001806 return 0;
dea31012005-04-17 16:05:31 -05001807 }
1808 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001809 return 1;
dea31012005-04-17 16:05:31 -05001810 }
1811 } else if (pring->ringno == psli->next_ring) {
1812
1813 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001814 return 0;
dea31012005-04-17 16:05:31 -05001815}
1816
1817/*
1818 * Free resources / clean up outstanding I/Os
1819 * associated with nlp_rpi in the LPFC_NODELIST entry.
1820 */
1821static int
James Smart2e0fef82007-06-17 19:56:36 -05001822lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001823{
James Smart2534ba72007-04-25 09:52:20 -04001824 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001825 struct lpfc_sli *psli;
1826 struct lpfc_sli_ring *pring;
1827 struct lpfc_iocbq *iocb, *next_iocb;
1828 IOCB_t *icmd;
1829 uint32_t rpi, i;
1830
James Smart92d7f7b2007-06-17 19:56:38 -05001831 lpfc_fabric_abort_nport(ndlp);
1832
dea31012005-04-17 16:05:31 -05001833 /*
1834 * Everything that matches on txcmplq will be returned
1835 * by firmware with a no rpi error.
1836 */
1837 psli = &phba->sli;
1838 rpi = ndlp->nlp_rpi;
1839 if (rpi) {
1840 /* Now process each ring */
1841 for (i = 0; i < psli->num_rings; i++) {
1842 pring = &psli->ring[i];
1843
James Smart2e0fef82007-06-17 19:56:36 -05001844 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001845 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05001846 list) {
dea31012005-04-17 16:05:31 -05001847 /*
1848 * Check to see if iocb matches the nport we are
1849 * looking for
1850 */
James Smart92d7f7b2007-06-17 19:56:38 -05001851 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
1852 ndlp))) {
dea31012005-04-17 16:05:31 -05001853 /* It matches, so deque and call compl
1854 with an error */
James Smart2534ba72007-04-25 09:52:20 -04001855 list_move_tail(&iocb->list,
1856 &completions);
dea31012005-04-17 16:05:31 -05001857 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05001858 }
1859 }
James Smart2e0fef82007-06-17 19:56:36 -05001860 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001861 }
1862 }
James Smart2534ba72007-04-25 09:52:20 -04001863
1864 while (!list_empty(&completions)) {
1865 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05001866 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04001867
James Smart2e0fef82007-06-17 19:56:36 -05001868 if (!iocb->iocb_cmpl)
1869 lpfc_sli_release_iocbq(phba, iocb);
1870 else {
James Smart2534ba72007-04-25 09:52:20 -04001871 icmd = &iocb->iocb;
1872 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1873 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
James Smart2e0fef82007-06-17 19:56:36 -05001874 (iocb->iocb_cmpl)(phba, iocb, iocb);
1875 }
James Smart2534ba72007-04-25 09:52:20 -04001876 }
1877
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001878 return 0;
dea31012005-04-17 16:05:31 -05001879}
1880
1881/*
1882 * Free rpi associated with LPFC_NODELIST entry.
1883 * This routine is called from lpfc_freenode(), when we are removing
1884 * a LPFC_NODELIST entry. It is also called if the driver initiates a
1885 * LOGO that completes successfully, and we are waiting to PLOGI back
1886 * to the remote NPort. In addition, it is called after we receive
1887 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1888 * we are waiting to PLOGI back to the remote NPort.
1889 */
1890int
James Smart2e0fef82007-06-17 19:56:36 -05001891lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001892{
James Smart2e0fef82007-06-17 19:56:36 -05001893 struct lpfc_hba *phba = vport->phba;
1894 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05001895 int rc;
1896
1897 if (ndlp->nlp_rpi) {
James Smart2e0fef82007-06-17 19:56:36 -05001898 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1899 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001900 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05001901 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001902 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04001903 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05001904 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05001905 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001906 }
dea31012005-04-17 16:05:31 -05001907 lpfc_no_rpi(phba, ndlp);
1908 ndlp->nlp_rpi = 0;
1909 return 1;
1910 }
1911 return 0;
1912}
1913
James Smart92d7f7b2007-06-17 19:56:38 -05001914void
1915lpfc_unreg_all_rpis(struct lpfc_vport *vport)
1916{
1917 struct lpfc_hba *phba = vport->phba;
1918 LPFC_MBOXQ_t *mbox;
1919 int rc;
1920
1921 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1922 if (mbox) {
1923 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
1924 mbox->vport = vport;
1925 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05001926 mbox->context1 = NULL;
1927 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05001928 if (rc == MBX_NOT_FINISHED) {
1929 mempool_free(mbox, phba->mbox_mem_pool);
1930 }
1931 }
1932}
1933
1934void
1935lpfc_unreg_default_rpis(struct lpfc_vport *vport)
1936{
1937 struct lpfc_hba *phba = vport->phba;
1938 LPFC_MBOXQ_t *mbox;
1939 int rc;
1940
1941 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1942 if (mbox) {
1943 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
1944 mbox->vport = vport;
1945 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05001946 mbox->context1 = NULL;
1947 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05001948 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001949 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1950 "1815 Could not issue "
1951 "unreg_did (default rpis)\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001952 mempool_free(mbox, phba->mbox_mem_pool);
1953 }
1954 }
1955}
1956
dea31012005-04-17 16:05:31 -05001957/*
1958 * Free resources associated with LPFC_NODELIST entry
1959 * so it can be freed.
1960 */
1961static int
James Smart2e0fef82007-06-17 19:56:36 -05001962lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001963{
James Smart2e0fef82007-06-17 19:56:36 -05001964 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1965 struct lpfc_hba *phba = vport->phba;
1966 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05001967 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05001968
1969 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04001970 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1971 "0900 Cleanup node for NPort x%x "
1972 "Data: x%x x%x x%x\n",
1973 ndlp->nlp_DID, ndlp->nlp_flag,
1974 ndlp->nlp_state, ndlp->nlp_rpi);
James Smart2e0fef82007-06-17 19:56:36 -05001975 lpfc_dequeue_node(vport, ndlp);
dea31012005-04-17 16:05:31 -05001976
dea31012005-04-17 16:05:31 -05001977 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1978 if ((mb = phba->sli.mbox_active)) {
1979 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1980 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1981 mb->context2 = NULL;
1982 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1983 }
1984 }
James Smart33ccf8d2006-08-17 11:57:58 -04001985
James Smart2e0fef82007-06-17 19:56:36 -05001986 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001987 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1988 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001989 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05001990 mp = (struct lpfc_dmabuf *) (mb->context1);
1991 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05001992 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001993 kfree(mp);
1994 }
1995 list_del(&mb->list);
1996 mempool_free(mb, phba->mbox_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04001997 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001998 }
1999 }
James Smart2e0fef82007-06-17 19:56:36 -05002000 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002001
James Smart07951072007-04-25 09:51:38 -04002002 lpfc_els_abort(phba,ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05002003 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04002004 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05002005 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002006
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05002007 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05002008 del_timer_sync(&ndlp->nlp_delayfunc);
2009
dea31012005-04-17 16:05:31 -05002010 if (!list_empty(&ndlp->els_retry_evt.evt_listp))
2011 list_del_init(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05002012 if (!list_empty(&ndlp->dev_loss_evt.evt_listp))
2013 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002014
James Smart2e0fef82007-06-17 19:56:36 -05002015 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05002016
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002017 return 0;
dea31012005-04-17 16:05:31 -05002018}
2019
2020/*
2021 * Check to see if we can free the nlp back to the freelist.
2022 * If we are in the middle of using the nlp in the discovery state
2023 * machine, defer the free till we reach the end of the state machine.
2024 */
James Smart329f9bc2007-04-25 09:53:01 -04002025static void
James Smart2e0fef82007-06-17 19:56:36 -05002026lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002027{
James Smarta8adb832007-10-27 13:37:53 -04002028 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04002029 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04002030 LPFC_MBOXQ_t *mbox;
2031 int rc;
dea31012005-04-17 16:05:31 -05002032
2033 if (ndlp->nlp_flag & NLP_DELAY_TMO) {
James Smart2e0fef82007-06-17 19:56:36 -05002034 lpfc_cancel_retry_delay_tmo(vport, ndlp);
dea31012005-04-17 16:05:31 -05002035 }
2036
James Smarta8adb832007-10-27 13:37:53 -04002037 if (ndlp->nlp_flag & NLP_DEFER_RM && !ndlp->nlp_rpi) {
2038 /* For this case we need to cleanup the default rpi
2039 * allocated by the firmware.
2040 */
2041 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
2042 != NULL) {
2043 rc = lpfc_reg_login(phba, vport->vpi, ndlp->nlp_DID,
2044 (uint8_t *) &vport->fc_sparam, mbox, 0);
2045 if (rc) {
2046 mempool_free(mbox, phba->mbox_mem_pool);
2047 }
2048 else {
2049 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2050 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2051 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05002052 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04002053 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2054 if (rc == MBX_NOT_FINISHED) {
2055 mempool_free(mbox, phba->mbox_mem_pool);
2056 }
2057 }
2058 }
2059 }
2060
James Smart2e0fef82007-06-17 19:56:36 -05002061 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04002062
James Smart2e0fef82007-06-17 19:56:36 -05002063 /*
James Smart92d7f7b2007-06-17 19:56:38 -05002064 * We can get here with a non-NULL ndlp->rport because when we
2065 * unregister a rport we don't break the rport/node linkage. So if we
2066 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05002067 */
James Smart92d7f7b2007-06-17 19:56:38 -05002068 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04002069 rdata = ndlp->rport->dd_data;
2070 rdata->pnode = NULL;
2071 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05002072 }
dea31012005-04-17 16:05:31 -05002073}
2074
2075static int
James Smart2e0fef82007-06-17 19:56:36 -05002076lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2077 uint32_t did)
dea31012005-04-17 16:05:31 -05002078{
James Smart2e0fef82007-06-17 19:56:36 -05002079 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05002080
2081 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002082 return 0;
dea31012005-04-17 16:05:31 -05002083
2084 if (ndlp->nlp_DID == 0) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002085 return 0;
dea31012005-04-17 16:05:31 -05002086 }
2087
2088 /* First check for Direct match */
2089 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002090 return 1;
dea31012005-04-17 16:05:31 -05002091
2092 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05002093 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05002094 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002095 return 0;
dea31012005-04-17 16:05:31 -05002096 }
2097
2098 matchdid.un.word = did;
2099 ndlpdid.un.word = ndlp->nlp_DID;
2100 if (matchdid.un.b.id == ndlpdid.un.b.id) {
2101 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
2102 (mydid.un.b.area == matchdid.un.b.area)) {
2103 if ((ndlpdid.un.b.domain == 0) &&
2104 (ndlpdid.un.b.area == 0)) {
2105 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002106 return 1;
dea31012005-04-17 16:05:31 -05002107 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002108 return 0;
dea31012005-04-17 16:05:31 -05002109 }
2110
2111 matchdid.un.word = ndlp->nlp_DID;
2112 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
2113 (mydid.un.b.area == ndlpdid.un.b.area)) {
2114 if ((matchdid.un.b.domain == 0) &&
2115 (matchdid.un.b.area == 0)) {
2116 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002117 return 1;
dea31012005-04-17 16:05:31 -05002118 }
2119 }
2120 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002121 return 0;
dea31012005-04-17 16:05:31 -05002122}
2123
James Smart685f0bf2007-04-25 09:53:08 -04002124/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05002125static struct lpfc_nodelist *
2126__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002127{
James Smart2fb9bd82006-12-02 13:33:57 -05002128 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002129 uint32_t data1;
2130
James Smart2e0fef82007-06-17 19:56:36 -05002131 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2132 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04002133 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2134 ((uint32_t) ndlp->nlp_xri << 16) |
2135 ((uint32_t) ndlp->nlp_type << 8) |
2136 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04002137 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2138 "0929 FIND node DID "
2139 "Data: x%p x%x x%x x%x\n",
2140 ndlp, ndlp->nlp_DID,
2141 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04002142 return ndlp;
dea31012005-04-17 16:05:31 -05002143 }
2144 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002145
dea31012005-04-17 16:05:31 -05002146 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04002147 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2148 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05002149 return NULL;
2150}
2151
2152struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002153lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002154{
James Smart2e0fef82007-06-17 19:56:36 -05002155 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002156 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002157
James Smart2e0fef82007-06-17 19:56:36 -05002158 spin_lock_irq(shost->host_lock);
2159 ndlp = __lpfc_findnode_did(vport, did);
2160 spin_unlock_irq(shost->host_lock);
2161 return ndlp;
2162}
2163
2164struct lpfc_nodelist *
2165lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2166{
2167 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2168 struct lpfc_nodelist *ndlp;
2169
2170 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002171 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05002172 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2173 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05002174 return NULL;
2175 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05002176 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002177 if (!ndlp)
2178 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002179 lpfc_nlp_init(vport, ndlp, did);
2180 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2181 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002182 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002183 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002184 return ndlp;
2185 }
James Smart2e0fef82007-06-17 19:56:36 -05002186 if (vport->fc_flag & FC_RSCN_MODE) {
2187 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04002188 /* If we've already recieved a PLOGI from this NPort
2189 * we don't need to try to discover it again.
2190 */
2191 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
2192 return NULL;
2193
James Smart2e0fef82007-06-17 19:56:36 -05002194 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002195 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002196 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002197
2198 /* Since this node is marked for discovery,
2199 * delay timeout is not needed.
2200 */
James Smartfdcebe22006-03-07 15:04:01 -05002201 if (ndlp->nlp_flag & NLP_DELAY_TMO)
James Smart2e0fef82007-06-17 19:56:36 -05002202 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart071fbd3d2006-04-15 11:53:20 -04002203 } else
dea31012005-04-17 16:05:31 -05002204 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002205 } else {
James Smart87af33f2007-10-27 13:37:43 -04002206 /* If we've already recieved a PLOGI from this NPort,
2207 * or we are already in the process of discovery on it,
2208 * we don't need to try to discover it again.
2209 */
James Smart685f0bf2007-04-25 09:53:08 -04002210 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04002211 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2212 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05002213 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002214 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2215 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002216 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002217 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002218 }
2219 return ndlp;
2220}
2221
2222/* Build a list of nodes to discover based on the loopmap */
2223void
James Smart2e0fef82007-06-17 19:56:36 -05002224lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002225{
James Smart2e0fef82007-06-17 19:56:36 -05002226 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002227 int j;
2228 uint32_t alpa, index;
2229
James Smart2e0fef82007-06-17 19:56:36 -05002230 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002231 return;
James Smart2e0fef82007-06-17 19:56:36 -05002232
2233 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05002234 return;
dea31012005-04-17 16:05:31 -05002235
2236 /* Check for loop map present or not */
2237 if (phba->alpa_map[0]) {
2238 for (j = 1; j <= phba->alpa_map[0]; j++) {
2239 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05002240 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05002241 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002242 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002243 }
2244 } else {
2245 /* No alpamap, so try all alpa's */
2246 for (j = 0; j < FC_MAXLOOP; j++) {
2247 /* If cfg_scan_down is set, start from highest
2248 * ALPA (0xef) to lowest (0x1).
2249 */
James Smart3de2a652007-08-02 11:09:59 -04002250 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05002251 index = j;
2252 else
2253 index = FC_MAXLOOP - j - 1;
2254 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05002255 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05002256 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002257 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002258 }
2259 }
2260 return;
2261}
2262
dea31012005-04-17 16:05:31 -05002263void
James Smart2e0fef82007-06-17 19:56:36 -05002264lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002265{
dea31012005-04-17 16:05:31 -05002266 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05002267 struct lpfc_sli *psli = &phba->sli;
2268 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2269 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2270 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2271 int rc;
2272
James Smart92d7f7b2007-06-17 19:56:38 -05002273 /*
2274 * if it's not a physical port or if we already send
2275 * clear_la then don't send it.
2276 */
2277 if ((phba->link_state >= LPFC_CLEAR_LA) ||
2278 (vport->port_type != LPFC_PHYSICAL_PORT))
2279 return;
2280
James Smart2e0fef82007-06-17 19:56:36 -05002281 /* Link up discovery */
2282 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2283 phba->link_state = LPFC_CLEAR_LA;
2284 lpfc_clear_la(phba, mbox);
2285 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2286 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002287 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05002288 if (rc == MBX_NOT_FINISHED) {
2289 mempool_free(mbox, phba->mbox_mem_pool);
2290 lpfc_disc_flush_list(vport);
2291 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2292 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2293 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05002294 phba->link_state = LPFC_HBA_ERROR;
2295 }
2296 }
2297}
2298
2299/* Reg_vpi to tell firmware to resume normal operations */
2300void
2301lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2302{
2303 LPFC_MBOXQ_t *regvpimbox;
2304
2305 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2306 if (regvpimbox) {
2307 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
2308 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2309 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002310 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05002311 == MBX_NOT_FINISHED) {
2312 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002313 }
2314 }
2315}
2316
2317/* Start Link up / RSCN discovery on NPR nodes */
2318void
2319lpfc_disc_start(struct lpfc_vport *vport)
2320{
2321 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2322 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04002323 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05002324 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04002325 int did_changed;
dea31012005-04-17 16:05:31 -05002326
James Smart2e0fef82007-06-17 19:56:36 -05002327 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002328 return;
James Smart2e0fef82007-06-17 19:56:36 -05002329
2330 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05002331 clear_la_pending = 1;
2332 else
2333 clear_la_pending = 0;
2334
James Smart2e0fef82007-06-17 19:56:36 -05002335 if (vport->port_state < LPFC_VPORT_READY)
2336 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05002337
James Smart2e0fef82007-06-17 19:56:36 -05002338 lpfc_set_disctmo(vport);
2339
2340 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05002341 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002342 else
dea31012005-04-17 16:05:31 -05002343 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002344
2345 vport->fc_prevDID = vport->fc_myDID;
2346 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05002347
2348 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002349 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2350 "0202 Start Discovery hba state x%x "
2351 "Data: x%x x%x x%x\n",
2352 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
2353 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002354
2355 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002356 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05002357
2358 if (num_sent)
2359 return;
2360
James Smart92d7f7b2007-06-17 19:56:38 -05002361 /*
2362 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2363 * continue discovery.
2364 */
2365 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2366 !(vport->fc_flag & FC_RSCN_MODE)) {
2367 lpfc_issue_reg_vpi(phba, vport);
2368 return;
2369 }
James Smart2e0fef82007-06-17 19:56:36 -05002370
James Smart92d7f7b2007-06-17 19:56:38 -05002371 /*
2372 * For SLI2, we need to set port_state to READY and continue
2373 * discovery.
2374 */
2375 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2376 /* If we get here, there is nothing to ADISC */
2377 if (vport->port_type == LPFC_PHYSICAL_PORT)
2378 lpfc_issue_clear_la(phba, vport);
2379
2380 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05002381 vport->num_disc_nodes = 0;
2382 /* go thru NPR nodes and issue ELS PLOGIs */
2383 if (vport->fc_npr_cnt)
2384 lpfc_els_disc_plogi(vport);
2385
2386 if (!vport->num_disc_nodes) {
2387 spin_lock_irq(shost->host_lock);
2388 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2389 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002390 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05002391 }
2392 }
James Smart92d7f7b2007-06-17 19:56:38 -05002393 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002394 } else {
2395 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002396 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05002397
2398 if (num_sent)
2399 return;
2400
James Smart2e0fef82007-06-17 19:56:36 -05002401 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05002402 /* Check to see if more RSCNs came in while we
2403 * were processing this one.
2404 */
James Smart2e0fef82007-06-17 19:56:36 -05002405 if ((vport->fc_rscn_id_cnt == 0) &&
2406 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2407 spin_lock_irq(shost->host_lock);
2408 vport->fc_flag &= ~FC_RSCN_MODE;
2409 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002410 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002411 } else
James Smart2e0fef82007-06-17 19:56:36 -05002412 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05002413 }
2414 }
2415 return;
2416}
2417
2418/*
2419 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2420 * ring the match the sppecified nodelist.
2421 */
2422static void
James Smart2e0fef82007-06-17 19:56:36 -05002423lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002424{
James Smart2534ba72007-04-25 09:52:20 -04002425 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002426 struct lpfc_sli *psli;
2427 IOCB_t *icmd;
2428 struct lpfc_iocbq *iocb, *next_iocb;
2429 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002430
2431 psli = &phba->sli;
2432 pring = &psli->ring[LPFC_ELS_RING];
2433
2434 /* Error matching iocb on txq or txcmplq
2435 * First check the txq.
2436 */
James Smart2e0fef82007-06-17 19:56:36 -05002437 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002438 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2439 if (iocb->context1 != ndlp) {
2440 continue;
2441 }
2442 icmd = &iocb->iocb;
2443 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2444 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2445
James Smart2534ba72007-04-25 09:52:20 -04002446 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05002447 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002448 }
2449 }
2450
2451 /* Next check the txcmplq */
2452 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2453 if (iocb->context1 != ndlp) {
2454 continue;
2455 }
2456 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05002457 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2458 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04002459 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05002460 }
2461 }
James Smart2e0fef82007-06-17 19:56:36 -05002462 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002463
2464 while (!list_empty(&completions)) {
2465 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05002466 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04002467
James Smart2e0fef82007-06-17 19:56:36 -05002468 if (!iocb->iocb_cmpl)
2469 lpfc_sli_release_iocbq(phba, iocb);
2470 else {
James Smart2534ba72007-04-25 09:52:20 -04002471 icmd = &iocb->iocb;
2472 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2473 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2474 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002475 }
James Smart2534ba72007-04-25 09:52:20 -04002476 }
dea31012005-04-17 16:05:31 -05002477}
2478
Adrian Bunka6ababd2007-11-05 18:07:33 +01002479static void
James Smart2e0fef82007-06-17 19:56:36 -05002480lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002481{
2482 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05002483 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002484
James Smart2e0fef82007-06-17 19:56:36 -05002485 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2486 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002487 nlp_listp) {
2488 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2489 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2490 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04002491 }
dea31012005-04-17 16:05:31 -05002492 }
2493 }
dea31012005-04-17 16:05:31 -05002494}
2495
James Smart92d7f7b2007-06-17 19:56:38 -05002496void
2497lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2498{
2499 lpfc_els_flush_rscn(vport);
2500 lpfc_els_flush_cmd(vport);
2501 lpfc_disc_flush_list(vport);
2502}
2503
dea31012005-04-17 16:05:31 -05002504/*****************************************************************************/
2505/*
2506 * NAME: lpfc_disc_timeout
2507 *
2508 * FUNCTION: Fibre Channel driver discovery timeout routine.
2509 *
2510 * EXECUTION ENVIRONMENT: interrupt only
2511 *
2512 * CALLED FROM:
2513 * Timer function
2514 *
2515 * RETURNS:
2516 * none
2517 */
2518/*****************************************************************************/
2519void
2520lpfc_disc_timeout(unsigned long ptr)
2521{
James Smart2e0fef82007-06-17 19:56:36 -05002522 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2523 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002524 unsigned long flags = 0;
2525
2526 if (unlikely(!phba))
2527 return;
2528
James Smart2e0fef82007-06-17 19:56:36 -05002529 if ((vport->work_port_events & WORKER_DISC_TMO) == 0) {
2530 spin_lock_irqsave(&vport->work_port_lock, flags);
2531 vport->work_port_events |= WORKER_DISC_TMO;
2532 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2533
James Smart92d7f7b2007-06-17 19:56:38 -05002534 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002535 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05002536 lpfc_worker_wake_up(phba);
2537 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002538 }
dea31012005-04-17 16:05:31 -05002539 return;
2540}
2541
2542static void
James Smart2e0fef82007-06-17 19:56:36 -05002543lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002544{
James Smart2e0fef82007-06-17 19:56:36 -05002545 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2546 struct lpfc_hba *phba = vport->phba;
2547 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002548 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05002549 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05002550 int rc, clrlaerr = 0;
2551
James Smart2e0fef82007-06-17 19:56:36 -05002552 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05002553 return;
2554
James Smart2e0fef82007-06-17 19:56:36 -05002555 spin_lock_irq(shost->host_lock);
2556 vport->fc_flag &= ~FC_DISC_TMO;
2557 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002558
James Smart858c9f62007-06-17 19:56:39 -05002559 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2560 "disc timeout: state:x%x rtry:x%x flg:x%x",
2561 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2562
James Smart2e0fef82007-06-17 19:56:36 -05002563 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05002564
2565 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05002566 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2567 * FAN
2568 */
2569 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04002570 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
2571 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002572 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05002573 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002574 nlp_listp) {
2575 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2576 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002577 if (ndlp->nlp_type & NLP_FABRIC) {
2578 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05002579 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04002580
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002581 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002582 /* Fail outstanding IO now since device
2583 * is marked for PLOGI.
2584 */
James Smart2e0fef82007-06-17 19:56:36 -05002585 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002586 }
2587 }
James Smart92d7f7b2007-06-17 19:56:38 -05002588 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05002589 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05002590 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002591 }
dea31012005-04-17 16:05:31 -05002592 break;
2593
James Smart92d7f7b2007-06-17 19:56:38 -05002594 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05002595 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05002596 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05002597 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04002598 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2599 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04002600 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05002601
2602 /* Assume no Fabric and go on with discovery.
2603 * Check for outstanding ELS FLOGI to abort.
2604 */
2605
2606 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05002607 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002608
2609 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002610 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002611 break;
2612
2613 case LPFC_FABRIC_CFG_LINK:
2614 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2615 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04002616 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2617 "0223 Timeout while waiting for "
2618 "NameServer login\n");
dea31012005-04-17 16:05:31 -05002619 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05002620 ndlp = lpfc_findnode_did(vport, NameServer_DID);
dea31012005-04-17 16:05:31 -05002621 if (ndlp)
James Smart87af33f2007-10-27 13:37:43 -04002622 lpfc_els_abort(phba, ndlp);
2623
2624 /* ReStart discovery */
2625 goto restart_disc;
dea31012005-04-17 16:05:31 -05002626
2627 case LPFC_NS_QRY:
2628 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04002629 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2630 "0224 NameServer Query timeout "
2631 "Data: x%x x%x\n",
2632 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002633
James Smart92d7f7b2007-06-17 19:56:38 -05002634 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2635 /* Try it one more time */
2636 vport->fc_ns_retry++;
2637 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2638 vport->fc_ns_retry, 0);
2639 if (rc == 0)
2640 break;
dea31012005-04-17 16:05:31 -05002641 }
James Smart92d7f7b2007-06-17 19:56:38 -05002642 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002643
James Smart87af33f2007-10-27 13:37:43 -04002644restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05002645 /*
2646 * Discovery is over.
2647 * set port_state to PORT_READY if SLI2.
2648 * cmpl_reg_vpi will set port_state to READY for SLI3.
2649 */
2650 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2651 lpfc_issue_reg_vpi(phba, vport);
2652 else { /* NPIV Not enabled */
2653 lpfc_issue_clear_la(phba, vport);
2654 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002655 }
2656
2657 /* Setup and issue mailbox INITIALIZE LINK command */
2658 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2659 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04002660 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2661 "0206 Device Discovery "
2662 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05002663 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002664 break;
2665 }
2666
2667 lpfc_linkdown(phba);
2668 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2669 phba->cfg_link_speed);
2670 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05002671 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002672 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002673 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04002674 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05002675 if (rc == MBX_NOT_FINISHED)
2676 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2677
2678 break;
2679
2680 case LPFC_DISC_AUTH:
2681 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04002682 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2683 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002684 lpfc_disc_flush_list(vport);
2685
James Smart92d7f7b2007-06-17 19:56:38 -05002686 /*
2687 * set port_state to PORT_READY if SLI2.
2688 * cmpl_reg_vpi will set port_state to READY for SLI3.
2689 */
2690 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2691 lpfc_issue_reg_vpi(phba, vport);
2692 else { /* NPIV Not enabled */
2693 lpfc_issue_clear_la(phba, vport);
2694 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002695 }
2696 break;
2697
James Smart2e0fef82007-06-17 19:56:36 -05002698 case LPFC_VPORT_READY:
2699 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04002700 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2701 "0231 RSCN timeout Data: x%x "
2702 "x%x\n",
2703 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002704
2705 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05002706 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05002707
James Smart2e0fef82007-06-17 19:56:36 -05002708 lpfc_els_flush_rscn(vport);
2709 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05002710 }
2711 break;
James Smart2e0fef82007-06-17 19:56:36 -05002712
James Smart92d7f7b2007-06-17 19:56:38 -05002713 default:
James Smarte8b62012007-08-02 11:10:09 -04002714 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2715 "0229 Unexpected discovery timeout, "
2716 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05002717 break;
2718 }
2719
2720 switch (phba->link_state) {
2721 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05002722 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04002723 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2724 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002725 clrlaerr = 1;
2726 break;
2727
James Smart09372822008-01-11 01:52:54 -05002728 case LPFC_LINK_UP:
2729 lpfc_issue_clear_la(phba, vport);
2730 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05002731 case LPFC_LINK_UNKNOWN:
2732 case LPFC_WARM_START:
2733 case LPFC_INIT_START:
2734 case LPFC_INIT_MBX_CMDS:
2735 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05002736 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04002737 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2738 "0230 Unexpected timeout, hba link "
2739 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05002740 clrlaerr = 1;
2741 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002742
2743 case LPFC_HBA_READY:
2744 break;
dea31012005-04-17 16:05:31 -05002745 }
2746
2747 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05002748 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05002749 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002750 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2751 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05002752 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002753 }
2754
2755 return;
2756}
2757
dea31012005-04-17 16:05:31 -05002758/*
2759 * This routine handles processing a NameServer REG_LOGIN mailbox
2760 * command upon completion. It is setup in the LPFC_MBOXQ
2761 * as the completion routine when the command is
2762 * handed off to the SLI layer.
2763 */
2764void
James Smart2e0fef82007-06-17 19:56:36 -05002765lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002766{
James Smart2e0fef82007-06-17 19:56:36 -05002767 MAILBOX_t *mb = &pmb->mb;
2768 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2769 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2770 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002771
2772 pmb->context1 = NULL;
2773
dea31012005-04-17 16:05:31 -05002774 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05002775 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002776 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002777
James Smart2e0fef82007-06-17 19:56:36 -05002778 /*
2779 * Start issuing Fabric-Device Management Interface (FDMI) command to
2780 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
2781 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05002782 */
James Smart2e0fef82007-06-17 19:56:36 -05002783
James Smart3de2a652007-08-02 11:09:59 -04002784 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05002785 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
2786 else
2787 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05002788
James Smartfa4066b2008-01-11 01:53:27 -05002789 /* decrement the node reference count held for this callback
2790 * function.
2791 */
James Smart329f9bc2007-04-25 09:53:01 -04002792 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002793 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2794 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002795 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002796
2797 return;
2798}
2799
James Smart685f0bf2007-04-25 09:53:08 -04002800static int
2801lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2802{
2803 uint16_t *rpi = param;
2804
2805 return ndlp->nlp_rpi == *rpi;
2806}
2807
2808static int
2809lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2810{
2811 return memcmp(&ndlp->nlp_portname, param,
2812 sizeof(ndlp->nlp_portname)) == 0;
2813}
2814
Adrian Bunka6ababd2007-11-05 18:07:33 +01002815static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002816__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05002817{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002818 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002819
James Smart2e0fef82007-06-17 19:56:36 -05002820 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04002821 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04002822 return ndlp;
2823 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002824 return NULL;
dea31012005-04-17 16:05:31 -05002825}
2826
Adrian Bunka6ababd2007-11-05 18:07:33 +01002827#if 0
James Smart685f0bf2007-04-25 09:53:08 -04002828/*
2829 * Search node lists for a remote port matching filter criteria
James Smart92d7f7b2007-06-17 19:56:38 -05002830 * Caller needs to hold host_lock before calling this routine.
James Smart685f0bf2007-04-25 09:53:08 -04002831 */
2832struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002833lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
James Smart685f0bf2007-04-25 09:53:08 -04002834{
James Smart2e0fef82007-06-17 19:56:36 -05002835 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart685f0bf2007-04-25 09:53:08 -04002836 struct lpfc_nodelist *ndlp;
2837
James Smart2e0fef82007-06-17 19:56:36 -05002838 spin_lock_irq(shost->host_lock);
2839 ndlp = __lpfc_find_node(vport, filter, param);
2840 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002841 return ndlp;
2842}
Adrian Bunka6ababd2007-11-05 18:07:33 +01002843#endif /* 0 */
James Smart685f0bf2007-04-25 09:53:08 -04002844
2845/*
2846 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05002847 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04002848 */
2849struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002850__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04002851{
James Smart2e0fef82007-06-17 19:56:36 -05002852 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04002853}
2854
Adrian Bunka6ababd2007-11-05 18:07:33 +01002855#if 0
James Smart2534ba72007-04-25 09:52:20 -04002856struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002857lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart2534ba72007-04-25 09:52:20 -04002858{
James Smart2e0fef82007-06-17 19:56:36 -05002859 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2534ba72007-04-25 09:52:20 -04002860 struct lpfc_nodelist *ndlp;
2861
James Smart2e0fef82007-06-17 19:56:36 -05002862 spin_lock_irq(shost->host_lock);
2863 ndlp = __lpfc_findnode_rpi(vport, rpi);
2864 spin_unlock_irq(shost->host_lock);
James Smart2534ba72007-04-25 09:52:20 -04002865 return ndlp;
2866}
Adrian Bunka6ababd2007-11-05 18:07:33 +01002867#endif /* 0 */
James Smart2534ba72007-04-25 09:52:20 -04002868
James Smart488d1462006-03-07 15:02:37 -05002869/*
James Smart685f0bf2007-04-25 09:53:08 -04002870 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05002871 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05002872 */
2873struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002874lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05002875{
James Smart2e0fef82007-06-17 19:56:36 -05002876 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05002877 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05002878
James Smart2e0fef82007-06-17 19:56:36 -05002879 spin_lock_irq(shost->host_lock);
2880 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
2881 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002882 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05002883}
2884
dea31012005-04-17 16:05:31 -05002885void
James Smart2e0fef82007-06-17 19:56:36 -05002886lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2887 uint32_t did)
dea31012005-04-17 16:05:31 -05002888{
2889 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -05002890 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05002891 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002892 init_timer(&ndlp->nlp_delayfunc);
2893 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2894 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2895 ndlp->nlp_DID = did;
James Smart2e0fef82007-06-17 19:56:36 -05002896 ndlp->vport = vport;
dea31012005-04-17 16:05:31 -05002897 ndlp->nlp_sid = NLP_NO_SID;
James Smart685f0bf2007-04-25 09:53:08 -04002898 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart329f9bc2007-04-25 09:53:01 -04002899 kref_init(&ndlp->kref);
James Smart858c9f62007-06-17 19:56:39 -05002900
2901 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2902 "node init: did:x%x",
2903 ndlp->nlp_DID, 0, 0);
2904
dea31012005-04-17 16:05:31 -05002905 return;
2906}
James Smart329f9bc2007-04-25 09:53:01 -04002907
James Smart98c9ea52007-10-27 13:37:33 -04002908/* This routine releases all resources associated with a specifc NPort's ndlp
2909 * and mempool_free's the nodelist.
2910 */
James Smart311464e2007-08-02 11:10:37 -04002911static void
James Smart329f9bc2007-04-25 09:53:01 -04002912lpfc_nlp_release(struct kref *kref)
2913{
2914 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
2915 kref);
James Smart858c9f62007-06-17 19:56:39 -05002916
2917 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2918 "node release: did:x%x flg:x%x type:x%x",
2919 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2920
James Smart2e0fef82007-06-17 19:56:36 -05002921 lpfc_nlp_remove(ndlp->vport, ndlp);
2922 mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04002923}
2924
James Smart98c9ea52007-10-27 13:37:33 -04002925/* This routine bumps the reference count for a ndlp structure to ensure
2926 * that one discovery thread won't free a ndlp while another discovery thread
2927 * is using it.
2928 */
James Smart329f9bc2007-04-25 09:53:01 -04002929struct lpfc_nodelist *
2930lpfc_nlp_get(struct lpfc_nodelist *ndlp)
2931{
James Smart98c9ea52007-10-27 13:37:33 -04002932 if (ndlp) {
2933 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2934 "node get: did:x%x flg:x%x refcnt:x%x",
2935 ndlp->nlp_DID, ndlp->nlp_flag,
2936 atomic_read(&ndlp->kref.refcount));
James Smart329f9bc2007-04-25 09:53:01 -04002937 kref_get(&ndlp->kref);
James Smart98c9ea52007-10-27 13:37:33 -04002938 }
James Smart329f9bc2007-04-25 09:53:01 -04002939 return ndlp;
2940}
2941
James Smart98c9ea52007-10-27 13:37:33 -04002942
2943/* This routine decrements the reference count for a ndlp structure. If the
2944 * count goes to 0, this indicates the the associated nodelist should be freed.
2945 */
James Smart329f9bc2007-04-25 09:53:01 -04002946int
2947lpfc_nlp_put(struct lpfc_nodelist *ndlp)
2948{
James Smart98c9ea52007-10-27 13:37:33 -04002949 if (ndlp) {
2950 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2951 "node put: did:x%x flg:x%x refcnt:x%x",
2952 ndlp->nlp_DID, ndlp->nlp_flag,
2953 atomic_read(&ndlp->kref.refcount));
2954 }
James Smart329f9bc2007-04-25 09:53:01 -04002955 return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
2956}
James Smart98c9ea52007-10-27 13:37:33 -04002957
2958/* This routine free's the specified nodelist if it is not in use
2959 * by any other discovery thread. This routine returns 1 if the ndlp
2960 * is not being used by anyone and has been freed. A return value of
2961 * 0 indicates it is being used by another discovery thread and the
2962 * refcount is left unchanged.
2963 */
2964int
2965lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
2966{
2967 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2968 "node not used: did:x%x flg:x%x refcnt:x%x",
2969 ndlp->nlp_DID, ndlp->nlp_flag,
2970 atomic_read(&ndlp->kref.refcount));
2971
2972 if (atomic_read(&ndlp->kref.refcount) == 1) {
2973 lpfc_nlp_put(ndlp);
2974 return 1;
2975 }
2976 return 0;
2977}
2978