blob: 644d96012d565330ff870abe1895242d3bf9ba0c [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);
152 evtp->evt_arg1 = ndlp;
James Smart858c9f62007-06-17 19:56:39 -0500153 evtp->evt = LPFC_EVT_DEV_LOSS;
154 list_add_tail(&evtp->evt_listp, &phba->work_list);
155 if (phba->work_wait)
156 wake_up(phba->work_wait);
157
158 spin_unlock_irq(&phba->hbalock);
159
James Smart858c9f62007-06-17 19:56:39 -0500160 return;
161}
162
163/*
164 * This function is called from the worker thread when dev_loss_tmo
165 * expire.
166 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100167static void
James Smart858c9f62007-06-17 19:56:39 -0500168lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
169{
170 struct lpfc_rport_data *rdata;
171 struct fc_rport *rport;
172 struct lpfc_vport *vport;
173 struct lpfc_hba *phba;
174 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400175 int put_node;
176 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500177 int warn_on = 0;
178
179 rport = ndlp->rport;
180
181 if (!rport)
182 return;
183
184 rdata = rport->dd_data;
185 name = (uint8_t *) &ndlp->nlp_portname;
186 vport = ndlp->vport;
187 phba = vport->phba;
188
189 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
190 "rport devlosstmo:did:x%x type:x%x id:x%x",
191 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
192
James Smarta8adb832007-10-27 13:37:53 -0400193 /* Don't defer this if we are in the process of deleting the vport
194 * or unloading the driver. The unload will cleanup the node
195 * appropriately we just need to cleanup the ndlp rport info here.
196 */
197 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500198 if (ndlp->nlp_sid != NLP_NO_SID) {
199 /* flush the target */
200 lpfc_sli_abort_iocb(vport,
201 &phba->sli.ring[phba->sli.fcp_ring],
202 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
203 }
James Smarta8adb832007-10-27 13:37:53 -0400204 put_node = rdata->pnode != NULL;
205 put_rport = ndlp->rport != NULL;
206 rdata->pnode = NULL;
207 ndlp->rport = NULL;
208 if (put_node)
209 lpfc_nlp_put(ndlp);
210 if (put_rport)
211 put_device(&rport->dev);
212 return;
213 }
214
215 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
James Smart858c9f62007-06-17 19:56:39 -0500216 return;
217
James Smart92d7f7b2007-06-17 19:56:38 -0500218 if (ndlp->nlp_type & NLP_FABRIC) {
219 /* We will clean up these Nodes in linkup */
220 put_node = rdata->pnode != NULL;
221 put_rport = ndlp->rport != NULL;
222 rdata->pnode = NULL;
223 ndlp->rport = NULL;
224 if (put_node)
225 lpfc_nlp_put(ndlp);
226 if (put_rport)
227 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400228 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500229 }
James Smart82085712007-04-25 09:52:41 -0400230
dea31012005-04-17 16:05:31 -0500231 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400232 warn_on = 1;
dea31012005-04-17 16:05:31 -0500233 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400234 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
235 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500236 }
James Smart2e0fef82007-06-17 19:56:36 -0500237 if (vport->load_flag & FC_UNLOADING)
James Smartc01f3202006-08-18 17:47:08 -0400238 warn_on = 0;
239
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400240 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400241 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
242 "0203 Devloss timeout on "
243 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
244 "NPort x%x Data: x%x x%x x%x\n",
245 *name, *(name+1), *(name+2), *(name+3),
246 *(name+4), *(name+5), *(name+6), *(name+7),
247 ndlp->nlp_DID, ndlp->nlp_flag,
248 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400249 } else {
James Smarte8b62012007-08-02 11:10:09 -0400250 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
251 "0204 Devloss timeout on "
252 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
253 "NPort x%x Data: x%x x%x x%x\n",
254 *name, *(name+1), *(name+2), *(name+3),
255 *(name+4), *(name+5), *(name+6), *(name+7),
256 ndlp->nlp_DID, ndlp->nlp_flag,
257 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400258 }
259
James Smart87af33f2007-10-27 13:37:43 -0400260 put_node = rdata->pnode != NULL;
261 put_rport = ndlp->rport != NULL;
262 rdata->pnode = NULL;
263 ndlp->rport = NULL;
264 if (put_node)
265 lpfc_nlp_put(ndlp);
266 if (put_rport)
267 put_device(&rport->dev);
268
James Smart2e0fef82007-06-17 19:56:36 -0500269 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400270 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400271 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smart87af33f2007-10-27 13:37:43 -0400272 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -0500273 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart1dcb58e2007-04-25 09:51:30 -0400274 }
James Smart92d7f7b2007-06-17 19:56:38 -0500275}
James Smartc01f3202006-08-18 17:47:08 -0400276
James Smart92d7f7b2007-06-17 19:56:38 -0500277
278void
279lpfc_worker_wake_up(struct lpfc_hba *phba)
280{
281 wake_up(phba->work_wait);
dea31012005-04-17 16:05:31 -0500282 return;
283}
284
285static void
James Smart2e0fef82007-06-17 19:56:36 -0500286lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500287{
288 struct lpfc_work_evt *evtp = NULL;
289 struct lpfc_nodelist *ndlp;
290 int free_evt;
291
James Smart2e0fef82007-06-17 19:56:36 -0500292 spin_lock_irq(&phba->hbalock);
293 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500294 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
295 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500296 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500297 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500298 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500299 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500300 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500301 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500302 free_evt = 0; /* evt is part of ndlp */
dea31012005-04-17 16:05:31 -0500303 break;
James Smart858c9f62007-06-17 19:56:39 -0500304 case LPFC_EVT_DEV_LOSS:
305 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
306 lpfc_nlp_get(ndlp);
307 lpfc_dev_loss_tmo_handler(ndlp);
308 free_evt = 0;
James Smart858c9f62007-06-17 19:56:39 -0500309 lpfc_nlp_put(ndlp);
310 break;
dea31012005-04-17 16:05:31 -0500311 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500312 if (phba->link_state < LPFC_LINK_DOWN)
313 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500314 else
James Smart2e0fef82007-06-17 19:56:36 -0500315 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500316 complete((struct completion *)(evtp->evt_arg2));
317 break;
James Smart46fa3112007-04-25 09:51:45 -0400318 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500319 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400320 lpfc_offline_prep(phba);
321 *(int *)(evtp->evt_arg1) = 0;
322 complete((struct completion *)(evtp->evt_arg2));
323 break;
324 case LPFC_EVT_OFFLINE:
325 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500326 lpfc_sli_brdrestart(phba);
327 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400328 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
329 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500330 complete((struct completion *)(evtp->evt_arg2));
331 break;
332 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400333 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500334 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500335 lpfc_sli_brdreset(phba);
336 lpfc_hba_down_post(phba);
337 *(int *)(evtp->evt_arg1) =
338 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400339 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500340 complete((struct completion *)(evtp->evt_arg2));
341 break;
342 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400343 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500344 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500345 = (phba->pport->stopped)
346 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400347 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500348 complete((struct completion *)(evtp->evt_arg2));
349 break;
350 }
351 if (free_evt)
352 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500353 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500354 }
James Smart2e0fef82007-06-17 19:56:36 -0500355 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500356
357}
358
James Smart311464e2007-08-02 11:10:37 -0400359static void
James Smart2e0fef82007-06-17 19:56:36 -0500360lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500361{
362 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500363 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400364 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400365 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400366 int i;
dea31012005-04-17 16:05:31 -0500367
James Smart2e0fef82007-06-17 19:56:36 -0500368 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500369 ha_copy = phba->work_ha;
370 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500371 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500372
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500373 if (ha_copy & HA_ERATT)
dea31012005-04-17 16:05:31 -0500374 lpfc_handle_eratt(phba);
375
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500376 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500377 lpfc_sli_handle_mb_event(phba);
378
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500379 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500380 lpfc_handle_latt(phba);
James Smart549e55c2007-08-02 11:09:51 -0400381 vports = lpfc_create_vport_work_array(phba);
382 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500383 for(i = 0; i <= phba->max_vpi; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400384 /*
385 * We could have no vports in array if unloading, so if
386 * this happens then just use the pport
387 */
388 if (vports[i] == NULL && i == 0)
389 vport = phba->pport;
390 else
391 vport = vports[i];
392 if (vport == NULL)
393 break;
394 work_port_events = vport->work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400395 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400396 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400397 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400398 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400399 if (work_port_events & WORKER_HB_TMO)
400 lpfc_hb_timeout_handler(phba);
401 if (work_port_events & WORKER_MBOX_TMO)
402 lpfc_mbox_timeout_handler(phba);
403 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
404 lpfc_unblock_fabric_iocbs(phba);
405 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400406 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400407 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
408 lpfc_ramp_down_queue_handler(phba);
409 if (work_port_events & WORKER_RAMP_UP_QUEUE)
410 lpfc_ramp_up_queue_handler(phba);
James Smart51ef4c22007-08-02 11:10:31 -0400411 spin_lock_irq(&vport->work_port_lock);
412 vport->work_port_events &= ~work_port_events;
413 spin_unlock_irq(&vport->work_port_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500414 }
James Smart09372822008-01-11 01:52:54 -0500415 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500416
James Smart858c9f62007-06-17 19:56:39 -0500417 pring = &phba->sli.ring[LPFC_ELS_RING];
418 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
419 status >>= (4*LPFC_ELS_RING);
420 if ((status & HA_RXMASK)
421 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400422 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500423 pring->flag |= LPFC_DEFERRED_RING_EVENT;
424 } else {
425 lpfc_sli_handle_slow_ring_event(phba, pring,
426 (status &
427 HA_RXMASK));
428 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
429 }
430 /*
431 * Turn on Ring interrupts
432 */
433 spin_lock_irq(&phba->hbalock);
434 control = readl(phba->HCregaddr);
435 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
James Smarta58cbd52007-08-02 11:09:43 -0400436 lpfc_debugfs_slow_ring_trc(phba,
437 "WRK Enable ring: cntl:x%x hacopy:x%x",
438 control, ha_copy, 0);
439
James Smart858c9f62007-06-17 19:56:39 -0500440 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -0500441 writel(control, phba->HCregaddr);
442 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500443 }
James Smarta58cbd52007-08-02 11:09:43 -0400444 else {
445 lpfc_debugfs_slow_ring_trc(phba,
446 "WRK Ring ok: cntl:x%x hacopy:x%x",
447 control, ha_copy, 0);
448 }
James Smart858c9f62007-06-17 19:56:39 -0500449 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500450 }
James Smart2e0fef82007-06-17 19:56:36 -0500451 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500452}
453
454static int
James Smart2e0fef82007-06-17 19:56:36 -0500455check_work_wait_done(struct lpfc_hba *phba)
456{
James Smart92d7f7b2007-06-17 19:56:38 -0500457 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400458 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart858c9f62007-06-17 19:56:39 -0500459 int rc = 0;
James Smarted957682007-06-17 19:56:37 -0500460
James Smart2e0fef82007-06-17 19:56:36 -0500461 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500462 list_for_each_entry(vport, &phba->port_list, listentry) {
463 if (vport->work_port_events) {
464 rc = 1;
James Smart549e55c2007-08-02 11:09:51 -0400465 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500466 }
467 }
James Smart549e55c2007-08-02 11:09:51 -0400468 if (rc || phba->work_ha || (!list_empty(&phba->work_list)) ||
469 kthread_should_stop() || pring->flag & LPFC_DEFERRED_RING_EVENT) {
James Smart2e0fef82007-06-17 19:56:36 -0500470 rc = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500471 phba->work_found++;
James Smart549e55c2007-08-02 11:09:51 -0400472 } else
James Smart92d7f7b2007-06-17 19:56:38 -0500473 phba->work_found = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500474 spin_unlock_irq(&phba->hbalock);
475 return rc;
dea31012005-04-17 16:05:31 -0500476}
477
James Smart92d7f7b2007-06-17 19:56:38 -0500478
dea31012005-04-17 16:05:31 -0500479int
480lpfc_do_work(void *p)
481{
482 struct lpfc_hba *phba = p;
483 int rc;
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800484 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(work_waitq);
dea31012005-04-17 16:05:31 -0500485
486 set_user_nice(current, -20);
487 phba->work_wait = &work_waitq;
James Smart92d7f7b2007-06-17 19:56:38 -0500488 phba->work_found = 0;
dea31012005-04-17 16:05:31 -0500489
490 while (1) {
491
492 rc = wait_event_interruptible(work_waitq,
James Smart92d7f7b2007-06-17 19:56:38 -0500493 check_work_wait_done(phba));
494
dea31012005-04-17 16:05:31 -0500495 BUG_ON(rc);
496
497 if (kthread_should_stop())
498 break;
499
500 lpfc_work_done(phba);
501
James Smart92d7f7b2007-06-17 19:56:38 -0500502 /* If there is alot of slow ring work, like during link up
503 * check_work_wait_done() may cause this thread to not give
504 * up the CPU for very long periods of time. This may cause
505 * soft lockups or other problems. To avoid these situations
506 * give up the CPU here after LPFC_MAX_WORKER_ITERATION
507 * consecutive iterations.
508 */
509 if (phba->work_found >= LPFC_MAX_WORKER_ITERATION) {
510 phba->work_found = 0;
511 schedule();
512 }
dea31012005-04-17 16:05:31 -0500513 }
514 phba->work_wait = NULL;
515 return 0;
516}
517
518/*
519 * This is only called to handle FC worker events. Since this a rare
520 * occurance, we allocate a struct lpfc_work_evt structure here instead of
521 * embedding it in the IOCB.
522 */
523int
James Smart2e0fef82007-06-17 19:56:36 -0500524lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500525 uint32_t evt)
526{
527 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500528 unsigned long flags;
dea31012005-04-17 16:05:31 -0500529
530 /*
531 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
532 * be queued to worker thread for processing
533 */
James Smart92d7f7b2007-06-17 19:56:38 -0500534 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500535 if (!evtp)
536 return 0;
537
538 evtp->evt_arg1 = arg1;
539 evtp->evt_arg2 = arg2;
540 evtp->evt = evt;
541
James Smarted957682007-06-17 19:56:37 -0500542 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400543 list_add_tail(&evtp->evt_listp, &phba->work_list);
dea31012005-04-17 16:05:31 -0500544 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -0500545 lpfc_worker_wake_up(phba);
James Smarted957682007-06-17 19:56:37 -0500546 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500547
548 return 1;
549}
550
James Smart92d7f7b2007-06-17 19:56:38 -0500551void
552lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
553{
James Smart09372822008-01-11 01:52:54 -0500554 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500555 struct lpfc_hba *phba = vport->phba;
556 struct lpfc_nodelist *ndlp, *next_ndlp;
557 int rc;
558
559 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
560 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
561 continue;
562
James Smart98c9ea52007-10-27 13:37:33 -0400563 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
564 ((vport->port_type == LPFC_NPIV_PORT) &&
565 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500566 lpfc_unreg_rpi(vport, ndlp);
567
568 /* Leave Fabric nodes alone on link down */
569 if (!remove && ndlp->nlp_type & NLP_FABRIC)
570 continue;
571 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
572 remove
573 ? NLP_EVT_DEVICE_RM
574 : NLP_EVT_DEVICE_RECOVERY);
575 }
576 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
577 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500578 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500579 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500580 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500581 }
582}
583
James Smart87af33f2007-10-27 13:37:43 -0400584void
James Smart98c9ea52007-10-27 13:37:33 -0400585lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500586{
James Smart92d7f7b2007-06-17 19:56:38 -0500587 /* Cleanup any outstanding RSCN activity */
588 lpfc_els_flush_rscn(vport);
589
590 /* Cleanup any outstanding ELS commands */
591 lpfc_els_flush_cmd(vport);
592
593 lpfc_cleanup_rpis(vport, 0);
594
James Smart92d7f7b2007-06-17 19:56:38 -0500595 /* Turn off discovery timer if its running */
596 lpfc_can_disctmo(vport);
597}
598
James Smart98c9ea52007-10-27 13:37:33 -0400599static void
600lpfc_linkdown_port(struct lpfc_vport *vport)
601{
602 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
603
604 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
605
606 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
607 "Link Down: state:x%x rtry:x%x flg:x%x",
608 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
609
610 lpfc_port_link_failure(vport);
611
612}
613
dea31012005-04-17 16:05:31 -0500614int
James Smart685f0bf2007-04-25 09:53:08 -0400615lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500616{
James Smart2e0fef82007-06-17 19:56:36 -0500617 struct lpfc_vport *vport = phba->pport;
618 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400619 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400620 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400621 int i;
dea31012005-04-17 16:05:31 -0500622
James Smart2e0fef82007-06-17 19:56:36 -0500623 if (phba->link_state == LPFC_LINK_DOWN) {
624 return 0;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500625 }
James Smart2e0fef82007-06-17 19:56:36 -0500626 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500627 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500628 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500629 phba->pport->fc_flag &= ~FC_LBIT;
630 }
James Smart2e0fef82007-06-17 19:56:36 -0500631 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400632 vports = lpfc_create_vport_work_array(phba);
633 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500634 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400635 /* Issue a LINK DOWN event to all nodes */
636 lpfc_linkdown_port(vports[i]);
637 }
James Smart09372822008-01-11 01:52:54 -0500638 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500639 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500640 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
641 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500642 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500643 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500644 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400645 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500646 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500647 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500648 }
649 }
650
dea31012005-04-17 16:05:31 -0500651 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500652 if (phba->pport->fc_flag & FC_PT2PT) {
653 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500654 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
655 if (mb) {
dea31012005-04-17 16:05:31 -0500656 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500657 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500658 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400659 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500660 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500661 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500662 }
663 }
James Smart2e0fef82007-06-17 19:56:36 -0500664 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500665 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500666 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500667 }
James Smart2e0fef82007-06-17 19:56:36 -0500668
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500669 return 0;
dea31012005-04-17 16:05:31 -0500670}
671
James Smart92d7f7b2007-06-17 19:56:38 -0500672static void
673lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500674{
James Smart92d7f7b2007-06-17 19:56:38 -0500675 struct lpfc_nodelist *ndlp;
676
677 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
678 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
679 continue;
680
681 if (ndlp->nlp_type & NLP_FABRIC) {
682 /* On Linkup its safe to clean up the ndlp
683 * from Fabric connections.
684 */
685 if (ndlp->nlp_DID != Fabric_DID)
686 lpfc_unreg_rpi(vport, ndlp);
687 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
688 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
689 /* Fail outstanding IO now since device is
690 * marked for PLOGI.
691 */
692 lpfc_unreg_rpi(vport, ndlp);
693 }
694 }
695}
696
697static void
698lpfc_linkup_port(struct lpfc_vport *vport)
699{
700 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500701 struct lpfc_hba *phba = vport->phba;
702
703 if ((vport->load_flag & FC_UNLOADING) != 0)
704 return;
705
James Smart858c9f62007-06-17 19:56:39 -0500706 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
707 "Link Up: top:x%x speed:x%x flg:x%x",
708 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
709
James Smart92d7f7b2007-06-17 19:56:38 -0500710 /* If NPIV is not enabled, only bring the physical port up */
711 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
712 (vport != phba->pport))
713 return;
dea31012005-04-17 16:05:31 -0500714
James Smart2e0fef82007-06-17 19:56:36 -0500715 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400716
James Smart2e0fef82007-06-17 19:56:36 -0500717 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500718 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
719 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
720 vport->fc_flag |= FC_NDISC_ACTIVE;
721 vport->fc_ns_retry = 0;
722 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500723
James Smart92d7f7b2007-06-17 19:56:38 -0500724 if (vport->fc_flag & FC_LBIT)
725 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500726
James Smart92d7f7b2007-06-17 19:56:38 -0500727}
728
729static int
730lpfc_linkup(struct lpfc_hba *phba)
731{
James Smart549e55c2007-08-02 11:09:51 -0400732 struct lpfc_vport **vports;
733 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500734
735 phba->link_state = LPFC_LINK_UP;
736
737 /* Unblock fabric iocbs if they are blocked */
738 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
739 del_timer_sync(&phba->fabric_block_timer);
740
James Smart549e55c2007-08-02 11:09:51 -0400741 vports = lpfc_create_vport_work_array(phba);
742 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500743 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400744 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500745 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500746 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
747 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500748
749 return 0;
750}
751
752/*
753 * This routine handles processing a CLEAR_LA mailbox
754 * command upon completion. It is setup in the LPFC_MBOXQ
755 * as the completion routine when the command is
756 * handed off to the SLI layer.
757 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100758static void
James Smart2e0fef82007-06-17 19:56:36 -0500759lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500760{
James Smart2e0fef82007-06-17 19:56:36 -0500761 struct lpfc_vport *vport = pmb->vport;
762 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
763 struct lpfc_sli *psli = &phba->sli;
764 MAILBOX_t *mb = &pmb->mb;
dea31012005-04-17 16:05:31 -0500765 uint32_t control;
766
dea31012005-04-17 16:05:31 -0500767 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500768 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500769 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
770 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
771
772 /* Check for error */
773 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500774 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400775 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
776 "0320 CLEAR_LA mbxStatus error x%x hba "
777 "state x%x\n",
778 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500779 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500780 goto out;
781 }
782
James Smart92d7f7b2007-06-17 19:56:38 -0500783 if (vport->port_type == LPFC_PHYSICAL_PORT)
784 phba->link_state = LPFC_HBA_READY;
785
786 spin_lock_irq(&phba->hbalock);
787 psli->sli_flag |= LPFC_PROCESS_LA;
788 control = readl(phba->HCregaddr);
789 control |= HC_LAINT_ENA;
790 writel(control, phba->HCregaddr);
791 readl(phba->HCregaddr); /* flush */
792 spin_unlock_irq(&phba->hbalock);
793 return;
dea31012005-04-17 16:05:31 -0500794
James Smart2e0fef82007-06-17 19:56:36 -0500795 vport->num_disc_nodes = 0;
796 /* go thru NPR nodes and issue ELS PLOGIs */
797 if (vport->fc_npr_cnt)
798 lpfc_els_disc_plogi(vport);
799
800 if (!vport->num_disc_nodes) {
801 spin_lock_irq(shost->host_lock);
802 vport->fc_flag &= ~FC_NDISC_ACTIVE;
803 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500804 }
805
James Smart2e0fef82007-06-17 19:56:36 -0500806 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -0500807
808out:
809 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400810 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
811 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500812 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500813
James Smart2e0fef82007-06-17 19:56:36 -0500814 spin_lock_irq(shost->host_lock);
815 vport->fc_flag &= ~(FC_ABORT_DISCOVERY | FC_ESTABLISH_LINK);
816 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500817
818 del_timer_sync(&phba->fc_estabtmo);
819
James Smart2e0fef82007-06-17 19:56:36 -0500820 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500821
822 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500823
824 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500825 psli->sli_flag |= LPFC_PROCESS_LA;
826 control = readl(phba->HCregaddr);
827 control |= HC_LAINT_ENA;
828 writel(control, phba->HCregaddr);
829 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500830 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500831
832 return;
833}
834
James Smart2e0fef82007-06-17 19:56:36 -0500835
dea31012005-04-17 16:05:31 -0500836static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500837lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500838{
James Smart2e0fef82007-06-17 19:56:36 -0500839 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500840
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500841 if (pmb->mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500842 goto out;
dea31012005-04-17 16:05:31 -0500843
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500844 mempool_free(pmb, phba->mbox_mem_pool);
845
846 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500847 vport->fc_flag & FC_PUBLIC_LOOP &&
848 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500849 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500850 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500851 * LPFC_LOCAL_CFG_LINK while waiting for FAN
852 */
James Smart2e0fef82007-06-17 19:56:36 -0500853 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500854 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500855 }
dea31012005-04-17 16:05:31 -0500856
James Smart2e0fef82007-06-17 19:56:36 -0500857 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500858 * LPFC_FLOGI while waiting for FLOGI cmpl
859 */
James Smart92d7f7b2007-06-17 19:56:38 -0500860 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500861 lpfc_initial_flogi(vport);
862 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500863 return;
dea31012005-04-17 16:05:31 -0500864
865out:
James Smarte8b62012007-08-02 11:10:09 -0400866 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
867 "0306 CONFIG_LINK mbxStatus error x%x "
868 "HBA state x%x\n",
869 pmb->mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500870 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500871
872 lpfc_linkdown(phba);
873
James Smarte8b62012007-08-02 11:10:09 -0400874 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
875 "0200 CONFIG_LINK bad hba state x%x\n",
876 vport->port_state);
dea31012005-04-17 16:05:31 -0500877
James Smart92d7f7b2007-06-17 19:56:38 -0500878 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500879 return;
880}
881
882static void
James Smart2e0fef82007-06-17 19:56:36 -0500883lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500884{
dea31012005-04-17 16:05:31 -0500885 MAILBOX_t *mb = &pmb->mb;
886 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500887 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500888
889
890 /* Check for error */
891 if (mb->mbxStatus) {
892 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400893 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
894 "0319 READ_SPARAM mbxStatus error x%x "
895 "hba state x%x>\n",
896 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -0500897 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -0500898 goto out;
899 }
900
James Smart2e0fef82007-06-17 19:56:36 -0500901 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -0500902 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -0500903 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500904 u64_to_wwn(phba->cfg_soft_wwnn,
905 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400906 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500907 u64_to_wwn(phba->cfg_soft_wwpn,
908 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -0500909 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
910 sizeof(vport->fc_nodename));
911 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
912 sizeof(vport->fc_portname));
913 if (vport->port_type == LPFC_PHYSICAL_PORT) {
914 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
915 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
916 }
917
dea31012005-04-17 16:05:31 -0500918 lpfc_mbuf_free(phba, mp->virt, mp->phys);
919 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -0500920 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500921 return;
922
923out:
924 pmb->context1 = NULL;
925 lpfc_mbuf_free(phba, mp->virt, mp->phys);
926 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -0500927 lpfc_issue_clear_la(phba, vport);
928 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500929 return;
930}
931
932static void
James Smart92d7f7b2007-06-17 19:56:38 -0500933lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -0500934{
James Smart92d7f7b2007-06-17 19:56:38 -0500935 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500936 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
James Smart2e0fef82007-06-17 19:56:36 -0500937 int i;
James Smart14691152006-12-02 13:34:28 -0500938 struct lpfc_dmabuf *mp;
939 int rc;
940
dea31012005-04-17 16:05:31 -0500941 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
942 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
943
James Smart92d7f7b2007-06-17 19:56:38 -0500944 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500945 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -0500946 case LA_1GHZ_LINK:
947 phba->fc_linkspeed = LA_1GHZ_LINK;
948 break;
949 case LA_2GHZ_LINK:
950 phba->fc_linkspeed = LA_2GHZ_LINK;
951 break;
952 case LA_4GHZ_LINK:
953 phba->fc_linkspeed = LA_4GHZ_LINK;
954 break;
955 case LA_8GHZ_LINK:
956 phba->fc_linkspeed = LA_8GHZ_LINK;
957 break;
958 default:
959 phba->fc_linkspeed = LA_UNKNW_LINK;
960 break;
dea31012005-04-17 16:05:31 -0500961 }
962
963 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -0500964 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -0500965
966 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -0500967 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -0500968
James Smart92d7f7b2007-06-17 19:56:38 -0500969 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -0500970 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -0500971 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500972
James Smart2e0fef82007-06-17 19:56:36 -0500973 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -0500974 i = la->un.lilpBde64.tus.f.bdeSize;
975
976 if (i == 0) {
977 phba->alpa_map[0] = 0;
978 } else {
James Smarte8b62012007-08-02 11:10:09 -0400979 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -0500980 int numalpa, j, k;
981 union {
982 uint8_t pamap[16];
983 struct {
984 uint32_t wd1;
985 uint32_t wd2;
986 uint32_t wd3;
987 uint32_t wd4;
988 } pa;
989 } un;
990 numalpa = phba->alpa_map[0];
991 j = 0;
992 while (j < numalpa) {
993 memset(un.pamap, 0, 16);
994 for (k = 1; j < numalpa; k++) {
995 un.pamap[k - 1] =
996 phba->alpa_map[j + 1];
997 j++;
998 if (k == 16)
999 break;
1000 }
1001 /* Link Up Event ALPA map */
1002 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05001003 KERN_WARNING,
1004 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001005 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05001006 "ALPA map Data: x%x "
1007 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001008 un.pa.wd1, un.pa.wd2,
1009 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05001010 }
1011 }
1012 }
1013 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05001014 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04001015 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05001016 (phba->sli_rev == 3))
1017 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1018 }
James Smart2e0fef82007-06-17 19:56:36 -05001019 vport->fc_myDID = phba->fc_pref_DID;
1020 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05001021 }
James Smart92d7f7b2007-06-17 19:56:38 -05001022 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001023
1024 lpfc_linkup(phba);
1025 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001026 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05001027 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05001028 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04001029 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05001030 if (rc == MBX_NOT_FINISHED) {
1031 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1032 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1033 kfree(mp);
1034 mempool_free(sparam_mbox, phba->mbox_mem_pool);
1035 if (cfglink_mbox)
1036 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001037 goto out;
James Smart14691152006-12-02 13:34:28 -05001038 }
dea31012005-04-17 16:05:31 -05001039 }
1040
1041 if (cfglink_mbox) {
James Smart2e0fef82007-06-17 19:56:36 -05001042 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05001043 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05001044 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001045 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04001046 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001047 if (rc != MBX_NOT_FINISHED)
1048 return;
1049 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001050 }
James Smart92d7f7b2007-06-17 19:56:38 -05001051out:
1052 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001053 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1054 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1055 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05001056 lpfc_issue_clear_la(phba, vport);
1057 return;
dea31012005-04-17 16:05:31 -05001058}
1059
1060static void
James Smart2e0fef82007-06-17 19:56:36 -05001061lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1062{
dea31012005-04-17 16:05:31 -05001063 uint32_t control;
1064 struct lpfc_sli *psli = &phba->sli;
1065
1066 lpfc_linkdown(phba);
1067
1068 /* turn on Link Attention interrupts - no CLEAR_LA needed */
James Smart2e0fef82007-06-17 19:56:36 -05001069 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001070 psli->sli_flag |= LPFC_PROCESS_LA;
1071 control = readl(phba->HCregaddr);
1072 control |= HC_LAINT_ENA;
1073 writel(control, phba->HCregaddr);
1074 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001075 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001076}
1077
1078/*
1079 * This routine handles processing a READ_LA mailbox
1080 * command upon completion. It is setup in the LPFC_MBOXQ
1081 * as the completion routine when the command is
1082 * handed off to the SLI layer.
1083 */
1084void
James Smart2e0fef82007-06-17 19:56:36 -05001085lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001086{
James Smart2e0fef82007-06-17 19:56:36 -05001087 struct lpfc_vport *vport = pmb->vport;
1088 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05001089 READ_LA_VAR *la;
1090 MAILBOX_t *mb = &pmb->mb;
1091 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1092
1093 /* Check for error */
1094 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05001095 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001096 "1307 READ_LA mbox error x%x state x%x\n",
1097 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001098 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001099 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001100 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1101 }
1102
1103 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1104
1105 memcpy(&phba->alpa_map[0], mp->virt, 128);
1106
James Smart2e0fef82007-06-17 19:56:36 -05001107 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001108 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05001109 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001110 else
James Smart2e0fef82007-06-17 19:56:36 -05001111 vport->fc_flag &= ~FC_BYPASSED_MODE;
1112 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001113
dea31012005-04-17 16:05:31 -05001114 if (((phba->fc_eventTag + 1) < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05001115 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05001116 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05001117 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05001118 if (phba->fc_eventTag != 0)
1119 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001120 }
dea31012005-04-17 16:05:31 -05001121
1122 phba->fc_eventTag = la->eventTag;
1123
1124 if (la->attType == AT_LINK_UP) {
1125 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05001126 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart5b8bd0c2007-04-25 09:52:49 -04001127 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001128 "1306 Link Up Event in loop back mode "
1129 "x%x received Data: x%x x%x x%x x%x\n",
1130 la->eventTag, phba->fc_eventTag,
1131 la->granted_AL_PA, la->UlnkSpeed,
1132 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001133 } else {
1134 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001135 "1303 Link Up Event x%x received "
1136 "Data: x%x x%x x%x x%x\n",
1137 la->eventTag, phba->fc_eventTag,
1138 la->granted_AL_PA, la->UlnkSpeed,
1139 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001140 }
James Smart92d7f7b2007-06-17 19:56:38 -05001141 lpfc_mbx_process_link_up(phba, la);
dea31012005-04-17 16:05:31 -05001142 } else {
1143 phba->fc_stat.LinkDown++;
1144 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001145 "1305 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05001146 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001147 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05001148 phba->pport->port_state, vport->fc_flag);
dea31012005-04-17 16:05:31 -05001149 lpfc_mbx_issue_link_down(phba);
1150 }
1151
1152lpfc_mbx_cmpl_read_la_free_mbuf:
1153 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1154 kfree(mp);
1155 mempool_free(pmb, phba->mbox_mem_pool);
1156 return;
1157}
1158
1159/*
1160 * This routine handles processing a REG_LOGIN mailbox
1161 * command upon completion. It is setup in the LPFC_MBOXQ
1162 * as the completion routine when the command is
1163 * handed off to the SLI layer.
1164 */
1165void
James Smart2e0fef82007-06-17 19:56:36 -05001166lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001167{
James Smart2e0fef82007-06-17 19:56:36 -05001168 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001169 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05001170 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001171
dea31012005-04-17 16:05:31 -05001172 pmb->context1 = NULL;
1173
1174 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05001175 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05001176 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1177 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001178 mempool_free(pmb, phba->mbox_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04001179 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001180
1181 return;
1182}
1183
James Smart92d7f7b2007-06-17 19:56:38 -05001184static void
1185lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1186{
1187 MAILBOX_t *mb = &pmb->mb;
1188 struct lpfc_vport *vport = pmb->vport;
1189 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1190
1191 switch (mb->mbxStatus) {
1192 case 0x0011:
1193 case 0x0020:
1194 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04001195 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1196 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
1197 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001198 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001199 }
1200 vport->unreg_vpi_cmpl = VPORT_OK;
1201 mempool_free(pmb, phba->mbox_mem_pool);
1202 /*
1203 * This shost reference might have been taken at the beginning of
1204 * lpfc_vport_delete()
1205 */
1206 if (vport->load_flag & FC_UNLOADING)
1207 scsi_host_put(shost);
1208}
1209
1210void
1211lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1212{
1213 struct lpfc_hba *phba = vport->phba;
1214 LPFC_MBOXQ_t *mbox;
1215 int rc;
1216
1217 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1218 if (!mbox)
1219 return;
1220
1221 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1222 mbox->vport = vport;
1223 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04001224 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001225 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001226 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1227 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001228 mempool_free(mbox, phba->mbox_mem_pool);
1229 vport->unreg_vpi_cmpl = VPORT_ERROR;
1230 }
1231}
1232
1233static void
1234lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1235{
1236 struct lpfc_vport *vport = pmb->vport;
1237 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1238 MAILBOX_t *mb = &pmb->mb;
1239
1240 switch (mb->mbxStatus) {
1241 case 0x0011:
1242 case 0x9601:
1243 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04001244 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1245 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
1246 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001247 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1248 spin_lock_irq(shost->host_lock);
1249 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1250 spin_unlock_irq(shost->host_lock);
1251 vport->fc_myDID = 0;
1252 goto out;
1253 }
James Smart92d7f7b2007-06-17 19:56:38 -05001254
1255 vport->num_disc_nodes = 0;
1256 /* go thru NPR list and issue ELS PLOGIs */
1257 if (vport->fc_npr_cnt)
1258 lpfc_els_disc_plogi(vport);
1259
1260 if (!vport->num_disc_nodes) {
1261 spin_lock_irq(shost->host_lock);
1262 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1263 spin_unlock_irq(shost->host_lock);
1264 lpfc_can_disctmo(vport);
1265 }
1266 vport->port_state = LPFC_VPORT_READY;
1267
1268out:
1269 mempool_free(pmb, phba->mbox_mem_pool);
1270 return;
1271}
1272
dea31012005-04-17 16:05:31 -05001273/*
1274 * This routine handles processing a Fabric REG_LOGIN mailbox
1275 * command upon completion. It is setup in the LPFC_MBOXQ
1276 * as the completion routine when the command is
1277 * handed off to the SLI layer.
1278 */
1279void
James Smart2e0fef82007-06-17 19:56:36 -05001280lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001281{
James Smart92d7f7b2007-06-17 19:56:38 -05001282 struct lpfc_vport *vport = pmb->vport;
James Smart2e0fef82007-06-17 19:56:36 -05001283 MAILBOX_t *mb = &pmb->mb;
1284 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05001285 struct lpfc_nodelist *ndlp;
James Smart549e55c2007-08-02 11:09:51 -04001286 struct lpfc_vport **vports;
1287 int i;
dea31012005-04-17 16:05:31 -05001288
James Smart549e55c2007-08-02 11:09:51 -04001289 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04001290 pmb->context1 = NULL;
1291 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05001292 if (mb->mbxStatus) {
1293 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1294 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001295 mempool_free(pmb, phba->mbox_mem_pool);
1296 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001297
James Smart92d7f7b2007-06-17 19:56:38 -05001298 if (phba->fc_topology == TOPOLOGY_LOOP) {
1299 /* FLOGI failed, use loop map to make discovery list */
1300 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001301
James Smart92d7f7b2007-06-17 19:56:38 -05001302 /* Start discovery */
1303 lpfc_disc_start(vport);
1304 return;
1305 }
1306
1307 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001308 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1309 "0258 Register Fabric login error: 0x%x\n",
1310 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001311 return;
1312 }
1313
dea31012005-04-17 16:05:31 -05001314 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001315 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001316 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001317
James Smart329f9bc2007-04-25 09:53:01 -04001318 lpfc_nlp_put(ndlp); /* Drop the reference from the mbox */
1319
James Smart2e0fef82007-06-17 19:56:36 -05001320 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart549e55c2007-08-02 11:09:51 -04001321 vports = lpfc_create_vport_work_array(phba);
1322 if (vports != NULL)
1323 for(i = 0;
James Smart09372822008-01-11 01:52:54 -05001324 i <= phba->max_vpi && vports[i] != NULL;
James Smart549e55c2007-08-02 11:09:51 -04001325 i++) {
1326 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1327 continue;
1328 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1329 lpfc_initial_fdisc(vports[i]);
1330 else if (phba->sli3_options &
1331 LPFC_SLI3_NPIV_ENABLED) {
1332 lpfc_vport_set_state(vports[i],
1333 FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -04001334 lpfc_printf_vlog(vport, KERN_ERR,
1335 LOG_ELS,
1336 "0259 No NPIV "
1337 "Fabric support\n");
James Smart549e55c2007-08-02 11:09:51 -04001338 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001339 }
James Smart09372822008-01-11 01:52:54 -05001340 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001341 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05001342 }
1343
1344 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1345 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001346 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001347 return;
1348}
1349
1350/*
1351 * This routine handles processing a NameServer REG_LOGIN mailbox
1352 * command upon completion. It is setup in the LPFC_MBOXQ
1353 * as the completion routine when the command is
1354 * handed off to the SLI layer.
1355 */
1356void
James Smart2e0fef82007-06-17 19:56:36 -05001357lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001358{
James Smart2e0fef82007-06-17 19:56:36 -05001359 MAILBOX_t *mb = &pmb->mb;
1360 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1361 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1362 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001363
1364 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05001365out:
James Smart329f9bc2007-04-25 09:53:01 -04001366 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001367 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1368 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04001369 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04001370
1371 /* If no other thread is using the ndlp, free it */
1372 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05001373
James Smart92d7f7b2007-06-17 19:56:38 -05001374 if (phba->fc_topology == TOPOLOGY_LOOP) {
1375 /*
1376 * RegLogin failed, use loop map to make discovery
1377 * list
1378 */
1379 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001380
James Smart92d7f7b2007-06-17 19:56:38 -05001381 /* Start discovery */
1382 lpfc_disc_start(vport);
1383 return;
1384 }
1385 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001386 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1387 "0260 Register NameServer error: 0x%x\n",
1388 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001389 return;
1390 }
1391
1392 pmb->context1 = NULL;
1393
dea31012005-04-17 16:05:31 -05001394 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001395 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001396 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001397
James Smart2e0fef82007-06-17 19:56:36 -05001398 if (vport->port_state < LPFC_VPORT_READY) {
1399 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05001400 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1401 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1402 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1403 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1404 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1405
1406 /* Issue SCR just before NameServer GID_FT Query */
1407 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05001408 }
1409
James Smart2e0fef82007-06-17 19:56:36 -05001410 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05001411 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05001412 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05001413 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05001414 goto out;
dea31012005-04-17 16:05:31 -05001415 }
1416
James Smart329f9bc2007-04-25 09:53:01 -04001417 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001418 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1419 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001420 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001421
1422 return;
1423}
1424
1425static void
James Smart2e0fef82007-06-17 19:56:36 -05001426lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001427{
James Smart2e0fef82007-06-17 19:56:36 -05001428 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1429 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05001430 struct lpfc_rport_data *rdata;
1431 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05001432 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001433
1434 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001435 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1436 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001437 rport_ids.port_id = ndlp->nlp_DID;
1438 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05001439
James Smart329f9bc2007-04-25 09:53:01 -04001440 /*
1441 * We leave our node pointer in rport->dd_data when we unregister a
1442 * FCP target port. But fc_remote_port_add zeros the space to which
1443 * rport->dd_data points. So, if we're reusing a previously
1444 * registered port, drop the reference that we took the last time we
1445 * registered the port.
1446 */
1447 if (ndlp->rport && ndlp->rport->dd_data &&
James Smart92d7f7b2007-06-17 19:56:38 -05001448 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp) {
James Smart329f9bc2007-04-25 09:53:01 -04001449 lpfc_nlp_put(ndlp);
1450 }
James Smart858c9f62007-06-17 19:56:39 -05001451
1452 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1453 "rport add: did:x%x flg:x%x type x%x",
1454 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1455
James Smart2e0fef82007-06-17 19:56:36 -05001456 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04001457 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05001458 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1459 "Warning: fc_remote_port_add failed\n");
1460 return;
1461 }
1462
1463 /* initialize static port data */
1464 rport->maxframe_size = ndlp->nlp_maxframe;
1465 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05001466 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04001467 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001468
1469 if (ndlp->nlp_type & NLP_FCP_TARGET)
1470 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1471 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1472 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1473
1474
1475 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1476 fc_remote_port_rolechg(rport, rport_ids.roles);
1477
James Smart071fbd3d2006-04-15 11:53:20 -04001478 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001479 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04001480 ndlp->nlp_sid = rport->scsi_target_id;
1481 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001482 return;
1483}
1484
1485static void
James Smart2e0fef82007-06-17 19:56:36 -05001486lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001487{
1488 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04001489
James Smart858c9f62007-06-17 19:56:39 -05001490 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
1491 "rport delete: did:x%x flg:x%x type x%x",
1492 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1493
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001494 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05001495
1496 return;
1497}
1498
James Smartde0c5b32007-04-25 09:52:27 -04001499static void
James Smart2e0fef82007-06-17 19:56:36 -05001500lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04001501{
James Smart2e0fef82007-06-17 19:56:36 -05001502 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1503
1504 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001505 switch (state) {
1506 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001507 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001508 break;
1509 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001510 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001511 break;
1512 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001513 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001514 break;
1515 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001516 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001517 break;
1518 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001519 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001520 break;
1521 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001522 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001523 break;
1524 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001525 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001526 break;
1527 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001528 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001529 break;
1530 }
James Smart2e0fef82007-06-17 19:56:36 -05001531 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001532}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001533
James Smartde0c5b32007-04-25 09:52:27 -04001534static void
James Smart2e0fef82007-06-17 19:56:36 -05001535lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04001536 int old_state, int new_state)
1537{
James Smart2e0fef82007-06-17 19:56:36 -05001538 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1539
James Smartde0c5b32007-04-25 09:52:27 -04001540 if (new_state == NLP_STE_UNMAPPED_NODE) {
1541 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1542 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1543 ndlp->nlp_type |= NLP_FC_NODE;
1544 }
1545 if (new_state == NLP_STE_MAPPED_NODE)
1546 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1547 if (new_state == NLP_STE_NPR_NODE)
1548 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05001549
James Smartde0c5b32007-04-25 09:52:27 -04001550 /* Transport interface */
1551 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1552 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001553 vport->phba->nport_event_cnt++;
1554 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001555 }
dea31012005-04-17 16:05:31 -05001556
James Smartde0c5b32007-04-25 09:52:27 -04001557 if (new_state == NLP_STE_MAPPED_NODE ||
1558 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05001559 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05001560 /*
1561 * Tell the fc transport about the port, if we haven't
1562 * already. If we have, and it's a scsi entity, be
1563 * sure to unblock any attached scsi devices
1564 */
1565 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001566 }
James Smart858c9f62007-06-17 19:56:39 -05001567 /*
1568 * if we added to Mapped list, but the remote port
1569 * registration failed or assigned a target id outside
1570 * our presentable range - move the node to the
1571 * Unmapped List
1572 */
James Smartde0c5b32007-04-25 09:52:27 -04001573 if (new_state == NLP_STE_MAPPED_NODE &&
1574 (!ndlp->rport ||
1575 ndlp->rport->scsi_target_id == -1 ||
1576 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05001577 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001578 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05001579 spin_unlock_irq(shost->host_lock);
1580 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001581 }
James Smartde0c5b32007-04-25 09:52:27 -04001582}
1583
James Smart685f0bf2007-04-25 09:53:08 -04001584static char *
1585lpfc_nlp_state_name(char *buffer, size_t size, int state)
1586{
1587 static char *states[] = {
1588 [NLP_STE_UNUSED_NODE] = "UNUSED",
1589 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1590 [NLP_STE_ADISC_ISSUE] = "ADISC",
1591 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1592 [NLP_STE_PRLI_ISSUE] = "PRLI",
1593 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1594 [NLP_STE_MAPPED_NODE] = "MAPPED",
1595 [NLP_STE_NPR_NODE] = "NPR",
1596 };
1597
James Smart311464e2007-08-02 11:10:37 -04001598 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04001599 strlcpy(buffer, states[state], size);
1600 else
1601 snprintf(buffer, size, "unknown (%d)", state);
1602 return buffer;
1603}
1604
James Smartde0c5b32007-04-25 09:52:27 -04001605void
James Smart2e0fef82007-06-17 19:56:36 -05001606lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1607 int state)
James Smartde0c5b32007-04-25 09:52:27 -04001608{
James Smart2e0fef82007-06-17 19:56:36 -05001609 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04001610 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04001611 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04001612
James Smarte8b62012007-08-02 11:10:09 -04001613 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1614 "0904 NPort state transition x%06x, %s -> %s\n",
1615 ndlp->nlp_DID,
1616 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1617 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05001618
1619 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1620 "node statechg did:x%x old:%d ste:%d",
1621 ndlp->nlp_DID, old_state, state);
1622
James Smartde0c5b32007-04-25 09:52:27 -04001623 if (old_state == NLP_STE_NPR_NODE &&
1624 (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
1625 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001626 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001627 if (old_state == NLP_STE_UNMAPPED_NODE) {
1628 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1629 ndlp->nlp_type &= ~NLP_FC_NODE;
1630 }
1631
James Smart685f0bf2007-04-25 09:53:08 -04001632 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05001633 spin_lock_irq(shost->host_lock);
1634 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1635 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001636 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05001637 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04001638
1639 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05001640 lpfc_nlp_counters(vport, state, 1);
1641 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04001642}
1643
1644void
James Smart2e0fef82007-06-17 19:56:36 -05001645lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001646{
James Smart2e0fef82007-06-17 19:56:36 -05001647 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1648
James Smartde0c5b32007-04-25 09:52:27 -04001649 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
James Smart2e0fef82007-06-17 19:56:36 -05001650 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001651 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05001652 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1653 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001654 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05001655 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05001656 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
1657 NLP_STE_UNUSED_NODE);
James Smartde0c5b32007-04-25 09:52:27 -04001658}
1659
1660void
James Smart2e0fef82007-06-17 19:56:36 -05001661lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001662{
James Smart87af33f2007-10-27 13:37:43 -04001663 /*
1664 * Use of lpfc_drop_node and UNUSED list. lpfc_drop_node should
1665 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
1666 * the ndlp from the vport. The ndlp resides on the UNUSED list
1667 * until ALL other outstanding threads have completed. Thus, if a
1668 * ndlp is on the UNUSED list already, we should never do another
1669 * lpfc_drop_node() on it.
1670 */
James Smart51ef4c22007-08-02 11:10:31 -04001671 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04001672 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04001673 return;
dea31012005-04-17 16:05:31 -05001674}
1675
1676/*
1677 * Start / ReStart rescue timer for Discovery / RSCN handling
1678 */
1679void
James Smart2e0fef82007-06-17 19:56:36 -05001680lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001681{
James Smart2e0fef82007-06-17 19:56:36 -05001682 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1683 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001684 uint32_t tmo;
1685
James Smart2e0fef82007-06-17 19:56:36 -05001686 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001687 /* For FAN, timeout should be greater then edtov */
1688 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1689 } else {
1690 /* Normal discovery timeout should be > then ELS/CT timeout
1691 * FC spec states we need 3 * ratov for CT requests
1692 */
1693 tmo = ((phba->fc_ratov * 3) + 3);
1694 }
dea31012005-04-17 16:05:31 -05001695
James Smart858c9f62007-06-17 19:56:39 -05001696
1697 if (!timer_pending(&vport->fc_disctmo)) {
1698 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1699 "set disc timer: tmo:x%x state:x%x flg:x%x",
1700 tmo, vport->port_state, vport->fc_flag);
1701 }
1702
James Smart2e0fef82007-06-17 19:56:36 -05001703 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1704 spin_lock_irq(shost->host_lock);
1705 vport->fc_flag |= FC_DISC_TMO;
1706 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001707
1708 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001709 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1710 "0247 Start Discovery Timer state x%x "
1711 "Data: x%x x%lx x%x x%x\n",
1712 vport->port_state, tmo,
1713 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1714 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05001715
1716 return;
1717}
1718
1719/*
1720 * Cancel rescue timer for Discovery / RSCN handling
1721 */
1722int
James Smart2e0fef82007-06-17 19:56:36 -05001723lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001724{
James Smart2e0fef82007-06-17 19:56:36 -05001725 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001726 unsigned long iflags;
1727
James Smart858c9f62007-06-17 19:56:39 -05001728 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1729 "can disc timer: state:x%x rtry:x%x flg:x%x",
1730 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
1731
dea31012005-04-17 16:05:31 -05001732 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05001733 if (vport->fc_flag & FC_DISC_TMO) {
1734 spin_lock_irqsave(shost->host_lock, iflags);
1735 vport->fc_flag &= ~FC_DISC_TMO;
1736 spin_unlock_irqrestore(shost->host_lock, iflags);
1737 del_timer_sync(&vport->fc_disctmo);
1738 spin_lock_irqsave(&vport->work_port_lock, iflags);
1739 vport->work_port_events &= ~WORKER_DISC_TMO;
1740 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05001741 }
1742
1743 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001744 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1745 "0248 Cancel Discovery Timer state x%x "
1746 "Data: x%x x%x x%x\n",
1747 vport->port_state, vport->fc_flag,
1748 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001749 return 0;
dea31012005-04-17 16:05:31 -05001750}
1751
1752/*
1753 * Check specified ring for outstanding IOCB on the SLI queue
1754 * Return true if iocb matches the specified nport
1755 */
1756int
James Smart2e0fef82007-06-17 19:56:36 -05001757lpfc_check_sli_ndlp(struct lpfc_hba *phba,
1758 struct lpfc_sli_ring *pring,
1759 struct lpfc_iocbq *iocb,
1760 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001761{
James Smart2e0fef82007-06-17 19:56:36 -05001762 struct lpfc_sli *psli = &phba->sli;
1763 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05001764 struct lpfc_vport *vport = ndlp->vport;
1765
1766 if (iocb->vport != vport)
1767 return 0;
1768
dea31012005-04-17 16:05:31 -05001769 if (pring->ringno == LPFC_ELS_RING) {
1770 switch (icmd->ulpCommand) {
1771 case CMD_GEN_REQUEST64_CR:
1772 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001773 return 1;
dea31012005-04-17 16:05:31 -05001774 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04001775 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1776 return 1;
dea31012005-04-17 16:05:31 -05001777 case CMD_XMIT_ELS_RSP64_CX:
1778 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001779 return 1;
dea31012005-04-17 16:05:31 -05001780 }
James Smarta4bc3372006-12-02 13:34:16 -05001781 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05001782
1783 } else if (pring->ringno == psli->fcp_ring) {
1784 /* Skip match check if waiting to relogin to FCP target */
1785 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001786 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001787 return 0;
dea31012005-04-17 16:05:31 -05001788 }
1789 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001790 return 1;
dea31012005-04-17 16:05:31 -05001791 }
1792 } else if (pring->ringno == psli->next_ring) {
1793
1794 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001795 return 0;
dea31012005-04-17 16:05:31 -05001796}
1797
1798/*
1799 * Free resources / clean up outstanding I/Os
1800 * associated with nlp_rpi in the LPFC_NODELIST entry.
1801 */
1802static int
James Smart2e0fef82007-06-17 19:56:36 -05001803lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001804{
James Smart2534ba72007-04-25 09:52:20 -04001805 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001806 struct lpfc_sli *psli;
1807 struct lpfc_sli_ring *pring;
1808 struct lpfc_iocbq *iocb, *next_iocb;
1809 IOCB_t *icmd;
1810 uint32_t rpi, i;
1811
James Smart92d7f7b2007-06-17 19:56:38 -05001812 lpfc_fabric_abort_nport(ndlp);
1813
dea31012005-04-17 16:05:31 -05001814 /*
1815 * Everything that matches on txcmplq will be returned
1816 * by firmware with a no rpi error.
1817 */
1818 psli = &phba->sli;
1819 rpi = ndlp->nlp_rpi;
1820 if (rpi) {
1821 /* Now process each ring */
1822 for (i = 0; i < psli->num_rings; i++) {
1823 pring = &psli->ring[i];
1824
James Smart2e0fef82007-06-17 19:56:36 -05001825 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001826 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05001827 list) {
dea31012005-04-17 16:05:31 -05001828 /*
1829 * Check to see if iocb matches the nport we are
1830 * looking for
1831 */
James Smart92d7f7b2007-06-17 19:56:38 -05001832 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
1833 ndlp))) {
dea31012005-04-17 16:05:31 -05001834 /* It matches, so deque and call compl
1835 with an error */
James Smart2534ba72007-04-25 09:52:20 -04001836 list_move_tail(&iocb->list,
1837 &completions);
dea31012005-04-17 16:05:31 -05001838 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05001839 }
1840 }
James Smart2e0fef82007-06-17 19:56:36 -05001841 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001842 }
1843 }
James Smart2534ba72007-04-25 09:52:20 -04001844
1845 while (!list_empty(&completions)) {
1846 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05001847 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04001848
James Smart2e0fef82007-06-17 19:56:36 -05001849 if (!iocb->iocb_cmpl)
1850 lpfc_sli_release_iocbq(phba, iocb);
1851 else {
James Smart2534ba72007-04-25 09:52:20 -04001852 icmd = &iocb->iocb;
1853 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1854 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
James Smart2e0fef82007-06-17 19:56:36 -05001855 (iocb->iocb_cmpl)(phba, iocb, iocb);
1856 }
James Smart2534ba72007-04-25 09:52:20 -04001857 }
1858
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001859 return 0;
dea31012005-04-17 16:05:31 -05001860}
1861
1862/*
1863 * Free rpi associated with LPFC_NODELIST entry.
1864 * This routine is called from lpfc_freenode(), when we are removing
1865 * a LPFC_NODELIST entry. It is also called if the driver initiates a
1866 * LOGO that completes successfully, and we are waiting to PLOGI back
1867 * to the remote NPort. In addition, it is called after we receive
1868 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1869 * we are waiting to PLOGI back to the remote NPort.
1870 */
1871int
James Smart2e0fef82007-06-17 19:56:36 -05001872lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001873{
James Smart2e0fef82007-06-17 19:56:36 -05001874 struct lpfc_hba *phba = vport->phba;
1875 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05001876 int rc;
1877
1878 if (ndlp->nlp_rpi) {
James Smart2e0fef82007-06-17 19:56:36 -05001879 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1880 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001881 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05001882 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001883 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04001884 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05001885 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05001886 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001887 }
dea31012005-04-17 16:05:31 -05001888 lpfc_no_rpi(phba, ndlp);
1889 ndlp->nlp_rpi = 0;
1890 return 1;
1891 }
1892 return 0;
1893}
1894
James Smart92d7f7b2007-06-17 19:56:38 -05001895void
1896lpfc_unreg_all_rpis(struct lpfc_vport *vport)
1897{
1898 struct lpfc_hba *phba = vport->phba;
1899 LPFC_MBOXQ_t *mbox;
1900 int rc;
1901
1902 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1903 if (mbox) {
1904 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
1905 mbox->vport = vport;
1906 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05001907 mbox->context1 = NULL;
1908 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05001909 if (rc == MBX_NOT_FINISHED) {
1910 mempool_free(mbox, phba->mbox_mem_pool);
1911 }
1912 }
1913}
1914
1915void
1916lpfc_unreg_default_rpis(struct lpfc_vport *vport)
1917{
1918 struct lpfc_hba *phba = vport->phba;
1919 LPFC_MBOXQ_t *mbox;
1920 int rc;
1921
1922 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1923 if (mbox) {
1924 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
1925 mbox->vport = vport;
1926 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05001927 mbox->context1 = NULL;
1928 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smart92d7f7b2007-06-17 19:56:38 -05001929 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001930 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1931 "1815 Could not issue "
1932 "unreg_did (default rpis)\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001933 mempool_free(mbox, phba->mbox_mem_pool);
1934 }
1935 }
1936}
1937
dea31012005-04-17 16:05:31 -05001938/*
1939 * Free resources associated with LPFC_NODELIST entry
1940 * so it can be freed.
1941 */
1942static int
James Smart2e0fef82007-06-17 19:56:36 -05001943lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001944{
James Smart2e0fef82007-06-17 19:56:36 -05001945 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1946 struct lpfc_hba *phba = vport->phba;
1947 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05001948 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05001949
1950 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04001951 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1952 "0900 Cleanup node for NPort x%x "
1953 "Data: x%x x%x x%x\n",
1954 ndlp->nlp_DID, ndlp->nlp_flag,
1955 ndlp->nlp_state, ndlp->nlp_rpi);
James Smart2e0fef82007-06-17 19:56:36 -05001956 lpfc_dequeue_node(vport, ndlp);
dea31012005-04-17 16:05:31 -05001957
dea31012005-04-17 16:05:31 -05001958 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1959 if ((mb = phba->sli.mbox_active)) {
1960 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1961 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1962 mb->context2 = NULL;
1963 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1964 }
1965 }
James Smart33ccf8d2006-08-17 11:57:58 -04001966
James Smart2e0fef82007-06-17 19:56:36 -05001967 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001968 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1969 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001970 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05001971 mp = (struct lpfc_dmabuf *) (mb->context1);
1972 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05001973 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001974 kfree(mp);
1975 }
1976 list_del(&mb->list);
1977 mempool_free(mb, phba->mbox_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04001978 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001979 }
1980 }
James Smart2e0fef82007-06-17 19:56:36 -05001981 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001982
James Smart07951072007-04-25 09:51:38 -04001983 lpfc_els_abort(phba,ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05001984 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04001985 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05001986 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001987
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05001988 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05001989 del_timer_sync(&ndlp->nlp_delayfunc);
1990
dea31012005-04-17 16:05:31 -05001991 if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1992 list_del_init(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05001993 if (!list_empty(&ndlp->dev_loss_evt.evt_listp))
1994 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05001995
James Smart2e0fef82007-06-17 19:56:36 -05001996 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05001997
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001998 return 0;
dea31012005-04-17 16:05:31 -05001999}
2000
2001/*
2002 * Check to see if we can free the nlp back to the freelist.
2003 * If we are in the middle of using the nlp in the discovery state
2004 * machine, defer the free till we reach the end of the state machine.
2005 */
James Smart329f9bc2007-04-25 09:53:01 -04002006static void
James Smart2e0fef82007-06-17 19:56:36 -05002007lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002008{
James Smarta8adb832007-10-27 13:37:53 -04002009 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04002010 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04002011 LPFC_MBOXQ_t *mbox;
2012 int rc;
dea31012005-04-17 16:05:31 -05002013
2014 if (ndlp->nlp_flag & NLP_DELAY_TMO) {
James Smart2e0fef82007-06-17 19:56:36 -05002015 lpfc_cancel_retry_delay_tmo(vport, ndlp);
dea31012005-04-17 16:05:31 -05002016 }
2017
James Smarta8adb832007-10-27 13:37:53 -04002018 if (ndlp->nlp_flag & NLP_DEFER_RM && !ndlp->nlp_rpi) {
2019 /* For this case we need to cleanup the default rpi
2020 * allocated by the firmware.
2021 */
2022 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
2023 != NULL) {
2024 rc = lpfc_reg_login(phba, vport->vpi, ndlp->nlp_DID,
2025 (uint8_t *) &vport->fc_sparam, mbox, 0);
2026 if (rc) {
2027 mempool_free(mbox, phba->mbox_mem_pool);
2028 }
2029 else {
2030 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2031 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2032 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05002033 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04002034 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2035 if (rc == MBX_NOT_FINISHED) {
2036 mempool_free(mbox, phba->mbox_mem_pool);
2037 }
2038 }
2039 }
2040 }
2041
James Smart2e0fef82007-06-17 19:56:36 -05002042 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04002043
James Smart2e0fef82007-06-17 19:56:36 -05002044 /*
James Smart92d7f7b2007-06-17 19:56:38 -05002045 * We can get here with a non-NULL ndlp->rport because when we
2046 * unregister a rport we don't break the rport/node linkage. So if we
2047 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05002048 */
James Smart92d7f7b2007-06-17 19:56:38 -05002049 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04002050 rdata = ndlp->rport->dd_data;
2051 rdata->pnode = NULL;
2052 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05002053 }
dea31012005-04-17 16:05:31 -05002054}
2055
2056static int
James Smart2e0fef82007-06-17 19:56:36 -05002057lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2058 uint32_t did)
dea31012005-04-17 16:05:31 -05002059{
James Smart2e0fef82007-06-17 19:56:36 -05002060 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05002061
2062 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002063 return 0;
dea31012005-04-17 16:05:31 -05002064
2065 if (ndlp->nlp_DID == 0) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002066 return 0;
dea31012005-04-17 16:05:31 -05002067 }
2068
2069 /* First check for Direct match */
2070 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002071 return 1;
dea31012005-04-17 16:05:31 -05002072
2073 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05002074 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05002075 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002076 return 0;
dea31012005-04-17 16:05:31 -05002077 }
2078
2079 matchdid.un.word = did;
2080 ndlpdid.un.word = ndlp->nlp_DID;
2081 if (matchdid.un.b.id == ndlpdid.un.b.id) {
2082 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
2083 (mydid.un.b.area == matchdid.un.b.area)) {
2084 if ((ndlpdid.un.b.domain == 0) &&
2085 (ndlpdid.un.b.area == 0)) {
2086 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002087 return 1;
dea31012005-04-17 16:05:31 -05002088 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002089 return 0;
dea31012005-04-17 16:05:31 -05002090 }
2091
2092 matchdid.un.word = ndlp->nlp_DID;
2093 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
2094 (mydid.un.b.area == ndlpdid.un.b.area)) {
2095 if ((matchdid.un.b.domain == 0) &&
2096 (matchdid.un.b.area == 0)) {
2097 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002098 return 1;
dea31012005-04-17 16:05:31 -05002099 }
2100 }
2101 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002102 return 0;
dea31012005-04-17 16:05:31 -05002103}
2104
James Smart685f0bf2007-04-25 09:53:08 -04002105/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05002106static struct lpfc_nodelist *
2107__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002108{
James Smart2fb9bd82006-12-02 13:33:57 -05002109 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002110 uint32_t data1;
2111
James Smart2e0fef82007-06-17 19:56:36 -05002112 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2113 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04002114 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2115 ((uint32_t) ndlp->nlp_xri << 16) |
2116 ((uint32_t) ndlp->nlp_type << 8) |
2117 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04002118 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2119 "0929 FIND node DID "
2120 "Data: x%p x%x x%x x%x\n",
2121 ndlp, ndlp->nlp_DID,
2122 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04002123 return ndlp;
dea31012005-04-17 16:05:31 -05002124 }
2125 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002126
dea31012005-04-17 16:05:31 -05002127 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04002128 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2129 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05002130 return NULL;
2131}
2132
2133struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002134lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002135{
James Smart2e0fef82007-06-17 19:56:36 -05002136 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002137 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002138
James Smart2e0fef82007-06-17 19:56:36 -05002139 spin_lock_irq(shost->host_lock);
2140 ndlp = __lpfc_findnode_did(vport, did);
2141 spin_unlock_irq(shost->host_lock);
2142 return ndlp;
2143}
2144
2145struct lpfc_nodelist *
2146lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2147{
2148 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2149 struct lpfc_nodelist *ndlp;
2150
2151 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002152 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05002153 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2154 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05002155 return NULL;
2156 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05002157 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002158 if (!ndlp)
2159 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002160 lpfc_nlp_init(vport, ndlp, did);
2161 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2162 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002163 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002164 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002165 return ndlp;
2166 }
James Smart2e0fef82007-06-17 19:56:36 -05002167 if (vport->fc_flag & FC_RSCN_MODE) {
2168 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04002169 /* If we've already recieved a PLOGI from this NPort
2170 * we don't need to try to discover it again.
2171 */
2172 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
2173 return NULL;
2174
James Smart2e0fef82007-06-17 19:56:36 -05002175 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002176 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002177 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002178
2179 /* Since this node is marked for discovery,
2180 * delay timeout is not needed.
2181 */
James Smartfdcebe22006-03-07 15:04:01 -05002182 if (ndlp->nlp_flag & NLP_DELAY_TMO)
James Smart2e0fef82007-06-17 19:56:36 -05002183 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart071fbd3d2006-04-15 11:53:20 -04002184 } else
dea31012005-04-17 16:05:31 -05002185 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002186 } else {
James Smart87af33f2007-10-27 13:37:43 -04002187 /* If we've already recieved a PLOGI from this NPort,
2188 * or we are already in the process of discovery on it,
2189 * we don't need to try to discover it again.
2190 */
James Smart685f0bf2007-04-25 09:53:08 -04002191 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04002192 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2193 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05002194 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002195 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2196 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002197 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002198 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002199 }
2200 return ndlp;
2201}
2202
2203/* Build a list of nodes to discover based on the loopmap */
2204void
James Smart2e0fef82007-06-17 19:56:36 -05002205lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002206{
James Smart2e0fef82007-06-17 19:56:36 -05002207 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002208 int j;
2209 uint32_t alpa, index;
2210
James Smart2e0fef82007-06-17 19:56:36 -05002211 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002212 return;
James Smart2e0fef82007-06-17 19:56:36 -05002213
2214 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05002215 return;
dea31012005-04-17 16:05:31 -05002216
2217 /* Check for loop map present or not */
2218 if (phba->alpa_map[0]) {
2219 for (j = 1; j <= phba->alpa_map[0]; j++) {
2220 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05002221 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05002222 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002223 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002224 }
2225 } else {
2226 /* No alpamap, so try all alpa's */
2227 for (j = 0; j < FC_MAXLOOP; j++) {
2228 /* If cfg_scan_down is set, start from highest
2229 * ALPA (0xef) to lowest (0x1).
2230 */
James Smart3de2a652007-08-02 11:09:59 -04002231 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05002232 index = j;
2233 else
2234 index = FC_MAXLOOP - j - 1;
2235 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05002236 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05002237 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002238 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002239 }
2240 }
2241 return;
2242}
2243
dea31012005-04-17 16:05:31 -05002244void
James Smart2e0fef82007-06-17 19:56:36 -05002245lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002246{
dea31012005-04-17 16:05:31 -05002247 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05002248 struct lpfc_sli *psli = &phba->sli;
2249 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2250 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2251 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2252 int rc;
2253
James Smart92d7f7b2007-06-17 19:56:38 -05002254 /*
2255 * if it's not a physical port or if we already send
2256 * clear_la then don't send it.
2257 */
2258 if ((phba->link_state >= LPFC_CLEAR_LA) ||
2259 (vport->port_type != LPFC_PHYSICAL_PORT))
2260 return;
2261
James Smart2e0fef82007-06-17 19:56:36 -05002262 /* Link up discovery */
2263 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2264 phba->link_state = LPFC_CLEAR_LA;
2265 lpfc_clear_la(phba, mbox);
2266 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2267 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002268 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05002269 if (rc == MBX_NOT_FINISHED) {
2270 mempool_free(mbox, phba->mbox_mem_pool);
2271 lpfc_disc_flush_list(vport);
2272 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2273 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2274 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05002275 phba->link_state = LPFC_HBA_ERROR;
2276 }
2277 }
2278}
2279
2280/* Reg_vpi to tell firmware to resume normal operations */
2281void
2282lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2283{
2284 LPFC_MBOXQ_t *regvpimbox;
2285
2286 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2287 if (regvpimbox) {
2288 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
2289 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2290 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002291 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05002292 == MBX_NOT_FINISHED) {
2293 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002294 }
2295 }
2296}
2297
2298/* Start Link up / RSCN discovery on NPR nodes */
2299void
2300lpfc_disc_start(struct lpfc_vport *vport)
2301{
2302 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2303 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04002304 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05002305 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04002306 int did_changed;
dea31012005-04-17 16:05:31 -05002307
James Smart2e0fef82007-06-17 19:56:36 -05002308 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002309 return;
James Smart2e0fef82007-06-17 19:56:36 -05002310
2311 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05002312 clear_la_pending = 1;
2313 else
2314 clear_la_pending = 0;
2315
James Smart2e0fef82007-06-17 19:56:36 -05002316 if (vport->port_state < LPFC_VPORT_READY)
2317 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05002318
James Smart2e0fef82007-06-17 19:56:36 -05002319 lpfc_set_disctmo(vport);
2320
2321 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05002322 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002323 else
dea31012005-04-17 16:05:31 -05002324 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002325
2326 vport->fc_prevDID = vport->fc_myDID;
2327 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05002328
2329 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002330 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2331 "0202 Start Discovery hba state x%x "
2332 "Data: x%x x%x x%x\n",
2333 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
2334 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002335
2336 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002337 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05002338
2339 if (num_sent)
2340 return;
2341
James Smart92d7f7b2007-06-17 19:56:38 -05002342 /*
2343 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2344 * continue discovery.
2345 */
2346 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2347 !(vport->fc_flag & FC_RSCN_MODE)) {
2348 lpfc_issue_reg_vpi(phba, vport);
2349 return;
2350 }
James Smart2e0fef82007-06-17 19:56:36 -05002351
James Smart92d7f7b2007-06-17 19:56:38 -05002352 /*
2353 * For SLI2, we need to set port_state to READY and continue
2354 * discovery.
2355 */
2356 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2357 /* If we get here, there is nothing to ADISC */
2358 if (vport->port_type == LPFC_PHYSICAL_PORT)
2359 lpfc_issue_clear_la(phba, vport);
2360
2361 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05002362 vport->num_disc_nodes = 0;
2363 /* go thru NPR nodes and issue ELS PLOGIs */
2364 if (vport->fc_npr_cnt)
2365 lpfc_els_disc_plogi(vport);
2366
2367 if (!vport->num_disc_nodes) {
2368 spin_lock_irq(shost->host_lock);
2369 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2370 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002371 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05002372 }
2373 }
James Smart92d7f7b2007-06-17 19:56:38 -05002374 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002375 } else {
2376 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002377 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05002378
2379 if (num_sent)
2380 return;
2381
James Smart2e0fef82007-06-17 19:56:36 -05002382 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05002383 /* Check to see if more RSCNs came in while we
2384 * were processing this one.
2385 */
James Smart2e0fef82007-06-17 19:56:36 -05002386 if ((vport->fc_rscn_id_cnt == 0) &&
2387 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2388 spin_lock_irq(shost->host_lock);
2389 vport->fc_flag &= ~FC_RSCN_MODE;
2390 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002391 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002392 } else
James Smart2e0fef82007-06-17 19:56:36 -05002393 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05002394 }
2395 }
2396 return;
2397}
2398
2399/*
2400 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2401 * ring the match the sppecified nodelist.
2402 */
2403static void
James Smart2e0fef82007-06-17 19:56:36 -05002404lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002405{
James Smart2534ba72007-04-25 09:52:20 -04002406 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002407 struct lpfc_sli *psli;
2408 IOCB_t *icmd;
2409 struct lpfc_iocbq *iocb, *next_iocb;
2410 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002411
2412 psli = &phba->sli;
2413 pring = &psli->ring[LPFC_ELS_RING];
2414
2415 /* Error matching iocb on txq or txcmplq
2416 * First check the txq.
2417 */
James Smart2e0fef82007-06-17 19:56:36 -05002418 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002419 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2420 if (iocb->context1 != ndlp) {
2421 continue;
2422 }
2423 icmd = &iocb->iocb;
2424 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2425 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2426
James Smart2534ba72007-04-25 09:52:20 -04002427 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05002428 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002429 }
2430 }
2431
2432 /* Next check the txcmplq */
2433 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2434 if (iocb->context1 != ndlp) {
2435 continue;
2436 }
2437 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05002438 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2439 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04002440 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05002441 }
2442 }
James Smart2e0fef82007-06-17 19:56:36 -05002443 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002444
2445 while (!list_empty(&completions)) {
2446 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05002447 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04002448
James Smart2e0fef82007-06-17 19:56:36 -05002449 if (!iocb->iocb_cmpl)
2450 lpfc_sli_release_iocbq(phba, iocb);
2451 else {
James Smart2534ba72007-04-25 09:52:20 -04002452 icmd = &iocb->iocb;
2453 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2454 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2455 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002456 }
James Smart2534ba72007-04-25 09:52:20 -04002457 }
dea31012005-04-17 16:05:31 -05002458}
2459
Adrian Bunka6ababd2007-11-05 18:07:33 +01002460static void
James Smart2e0fef82007-06-17 19:56:36 -05002461lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002462{
2463 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05002464 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002465
James Smart2e0fef82007-06-17 19:56:36 -05002466 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2467 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002468 nlp_listp) {
2469 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2470 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2471 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04002472 }
dea31012005-04-17 16:05:31 -05002473 }
2474 }
dea31012005-04-17 16:05:31 -05002475}
2476
James Smart92d7f7b2007-06-17 19:56:38 -05002477void
2478lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2479{
2480 lpfc_els_flush_rscn(vport);
2481 lpfc_els_flush_cmd(vport);
2482 lpfc_disc_flush_list(vport);
2483}
2484
dea31012005-04-17 16:05:31 -05002485/*****************************************************************************/
2486/*
2487 * NAME: lpfc_disc_timeout
2488 *
2489 * FUNCTION: Fibre Channel driver discovery timeout routine.
2490 *
2491 * EXECUTION ENVIRONMENT: interrupt only
2492 *
2493 * CALLED FROM:
2494 * Timer function
2495 *
2496 * RETURNS:
2497 * none
2498 */
2499/*****************************************************************************/
2500void
2501lpfc_disc_timeout(unsigned long ptr)
2502{
James Smart2e0fef82007-06-17 19:56:36 -05002503 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2504 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002505 unsigned long flags = 0;
2506
2507 if (unlikely(!phba))
2508 return;
2509
James Smart2e0fef82007-06-17 19:56:36 -05002510 if ((vport->work_port_events & WORKER_DISC_TMO) == 0) {
2511 spin_lock_irqsave(&vport->work_port_lock, flags);
2512 vport->work_port_events |= WORKER_DISC_TMO;
2513 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2514
James Smart92d7f7b2007-06-17 19:56:38 -05002515 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002516 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05002517 lpfc_worker_wake_up(phba);
2518 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002519 }
dea31012005-04-17 16:05:31 -05002520 return;
2521}
2522
2523static void
James Smart2e0fef82007-06-17 19:56:36 -05002524lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002525{
James Smart2e0fef82007-06-17 19:56:36 -05002526 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2527 struct lpfc_hba *phba = vport->phba;
2528 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002529 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05002530 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05002531 int rc, clrlaerr = 0;
2532
James Smart2e0fef82007-06-17 19:56:36 -05002533 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05002534 return;
2535
James Smart2e0fef82007-06-17 19:56:36 -05002536 spin_lock_irq(shost->host_lock);
2537 vport->fc_flag &= ~FC_DISC_TMO;
2538 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002539
James Smart858c9f62007-06-17 19:56:39 -05002540 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2541 "disc timeout: state:x%x rtry:x%x flg:x%x",
2542 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2543
James Smart2e0fef82007-06-17 19:56:36 -05002544 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05002545
2546 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05002547 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2548 * FAN
2549 */
2550 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04002551 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
2552 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002553 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05002554 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002555 nlp_listp) {
2556 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2557 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002558 if (ndlp->nlp_type & NLP_FABRIC) {
2559 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05002560 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04002561
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002562 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002563 /* Fail outstanding IO now since device
2564 * is marked for PLOGI.
2565 */
James Smart2e0fef82007-06-17 19:56:36 -05002566 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002567 }
2568 }
James Smart92d7f7b2007-06-17 19:56:38 -05002569 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05002570 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05002571 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002572 }
dea31012005-04-17 16:05:31 -05002573 break;
2574
James Smart92d7f7b2007-06-17 19:56:38 -05002575 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05002576 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05002577 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05002578 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04002579 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2580 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04002581 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05002582
2583 /* Assume no Fabric and go on with discovery.
2584 * Check for outstanding ELS FLOGI to abort.
2585 */
2586
2587 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05002588 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002589
2590 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002591 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002592 break;
2593
2594 case LPFC_FABRIC_CFG_LINK:
2595 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2596 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04002597 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2598 "0223 Timeout while waiting for "
2599 "NameServer login\n");
dea31012005-04-17 16:05:31 -05002600 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05002601 ndlp = lpfc_findnode_did(vport, NameServer_DID);
dea31012005-04-17 16:05:31 -05002602 if (ndlp)
James Smart87af33f2007-10-27 13:37:43 -04002603 lpfc_els_abort(phba, ndlp);
2604
2605 /* ReStart discovery */
2606 goto restart_disc;
dea31012005-04-17 16:05:31 -05002607
2608 case LPFC_NS_QRY:
2609 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04002610 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2611 "0224 NameServer Query timeout "
2612 "Data: x%x x%x\n",
2613 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002614
James Smart92d7f7b2007-06-17 19:56:38 -05002615 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2616 /* Try it one more time */
2617 vport->fc_ns_retry++;
2618 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2619 vport->fc_ns_retry, 0);
2620 if (rc == 0)
2621 break;
dea31012005-04-17 16:05:31 -05002622 }
James Smart92d7f7b2007-06-17 19:56:38 -05002623 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002624
James Smart87af33f2007-10-27 13:37:43 -04002625restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05002626 /*
2627 * Discovery is over.
2628 * set port_state to PORT_READY if SLI2.
2629 * cmpl_reg_vpi will set port_state to READY for SLI3.
2630 */
2631 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2632 lpfc_issue_reg_vpi(phba, vport);
2633 else { /* NPIV Not enabled */
2634 lpfc_issue_clear_la(phba, vport);
2635 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002636 }
2637
2638 /* Setup and issue mailbox INITIALIZE LINK command */
2639 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2640 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04002641 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2642 "0206 Device Discovery "
2643 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05002644 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002645 break;
2646 }
2647
2648 lpfc_linkdown(phba);
2649 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2650 phba->cfg_link_speed);
2651 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05002652 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002653 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002654 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04002655 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05002656 if (rc == MBX_NOT_FINISHED)
2657 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2658
2659 break;
2660
2661 case LPFC_DISC_AUTH:
2662 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04002663 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2664 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002665 lpfc_disc_flush_list(vport);
2666
James Smart92d7f7b2007-06-17 19:56:38 -05002667 /*
2668 * set port_state to PORT_READY if SLI2.
2669 * cmpl_reg_vpi will set port_state to READY for SLI3.
2670 */
2671 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2672 lpfc_issue_reg_vpi(phba, vport);
2673 else { /* NPIV Not enabled */
2674 lpfc_issue_clear_la(phba, vport);
2675 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002676 }
2677 break;
2678
James Smart2e0fef82007-06-17 19:56:36 -05002679 case LPFC_VPORT_READY:
2680 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04002681 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2682 "0231 RSCN timeout Data: x%x "
2683 "x%x\n",
2684 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002685
2686 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05002687 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05002688
James Smart2e0fef82007-06-17 19:56:36 -05002689 lpfc_els_flush_rscn(vport);
2690 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05002691 }
2692 break;
James Smart2e0fef82007-06-17 19:56:36 -05002693
James Smart92d7f7b2007-06-17 19:56:38 -05002694 default:
James Smarte8b62012007-08-02 11:10:09 -04002695 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2696 "0229 Unexpected discovery timeout, "
2697 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05002698 break;
2699 }
2700
2701 switch (phba->link_state) {
2702 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05002703 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04002704 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2705 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002706 clrlaerr = 1;
2707 break;
2708
James Smart09372822008-01-11 01:52:54 -05002709 case LPFC_LINK_UP:
2710 lpfc_issue_clear_la(phba, vport);
2711 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05002712 case LPFC_LINK_UNKNOWN:
2713 case LPFC_WARM_START:
2714 case LPFC_INIT_START:
2715 case LPFC_INIT_MBX_CMDS:
2716 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05002717 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04002718 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2719 "0230 Unexpected timeout, hba link "
2720 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05002721 clrlaerr = 1;
2722 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002723
2724 case LPFC_HBA_READY:
2725 break;
dea31012005-04-17 16:05:31 -05002726 }
2727
2728 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05002729 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05002730 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002731 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2732 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05002733 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002734 }
2735
2736 return;
2737}
2738
dea31012005-04-17 16:05:31 -05002739/*
2740 * This routine handles processing a NameServer REG_LOGIN mailbox
2741 * command upon completion. It is setup in the LPFC_MBOXQ
2742 * as the completion routine when the command is
2743 * handed off to the SLI layer.
2744 */
2745void
James Smart2e0fef82007-06-17 19:56:36 -05002746lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002747{
James Smart2e0fef82007-06-17 19:56:36 -05002748 MAILBOX_t *mb = &pmb->mb;
2749 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2750 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2751 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002752
2753 pmb->context1 = NULL;
2754
dea31012005-04-17 16:05:31 -05002755 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05002756 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002757 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002758
James Smart2e0fef82007-06-17 19:56:36 -05002759 /*
2760 * Start issuing Fabric-Device Management Interface (FDMI) command to
2761 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
2762 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05002763 */
James Smart2e0fef82007-06-17 19:56:36 -05002764
James Smart3de2a652007-08-02 11:09:59 -04002765 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05002766 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
2767 else
2768 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05002769
James Smart329f9bc2007-04-25 09:53:01 -04002770 /* Mailbox took a reference to the node */
2771 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002772 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2773 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002774 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002775
2776 return;
2777}
2778
James Smart685f0bf2007-04-25 09:53:08 -04002779static int
2780lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2781{
2782 uint16_t *rpi = param;
2783
2784 return ndlp->nlp_rpi == *rpi;
2785}
2786
2787static int
2788lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2789{
2790 return memcmp(&ndlp->nlp_portname, param,
2791 sizeof(ndlp->nlp_portname)) == 0;
2792}
2793
Adrian Bunka6ababd2007-11-05 18:07:33 +01002794static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002795__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05002796{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002797 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002798
James Smart2e0fef82007-06-17 19:56:36 -05002799 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04002800 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04002801 return ndlp;
2802 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002803 return NULL;
dea31012005-04-17 16:05:31 -05002804}
2805
Adrian Bunka6ababd2007-11-05 18:07:33 +01002806#if 0
James Smart685f0bf2007-04-25 09:53:08 -04002807/*
2808 * Search node lists for a remote port matching filter criteria
James Smart92d7f7b2007-06-17 19:56:38 -05002809 * Caller needs to hold host_lock before calling this routine.
James Smart685f0bf2007-04-25 09:53:08 -04002810 */
2811struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002812lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
James Smart685f0bf2007-04-25 09:53:08 -04002813{
James Smart2e0fef82007-06-17 19:56:36 -05002814 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart685f0bf2007-04-25 09:53:08 -04002815 struct lpfc_nodelist *ndlp;
2816
James Smart2e0fef82007-06-17 19:56:36 -05002817 spin_lock_irq(shost->host_lock);
2818 ndlp = __lpfc_find_node(vport, filter, param);
2819 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002820 return ndlp;
2821}
Adrian Bunka6ababd2007-11-05 18:07:33 +01002822#endif /* 0 */
James Smart685f0bf2007-04-25 09:53:08 -04002823
2824/*
2825 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05002826 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04002827 */
2828struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002829__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04002830{
James Smart2e0fef82007-06-17 19:56:36 -05002831 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04002832}
2833
Adrian Bunka6ababd2007-11-05 18:07:33 +01002834#if 0
James Smart2534ba72007-04-25 09:52:20 -04002835struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002836lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart2534ba72007-04-25 09:52:20 -04002837{
James Smart2e0fef82007-06-17 19:56:36 -05002838 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2534ba72007-04-25 09:52:20 -04002839 struct lpfc_nodelist *ndlp;
2840
James Smart2e0fef82007-06-17 19:56:36 -05002841 spin_lock_irq(shost->host_lock);
2842 ndlp = __lpfc_findnode_rpi(vport, rpi);
2843 spin_unlock_irq(shost->host_lock);
James Smart2534ba72007-04-25 09:52:20 -04002844 return ndlp;
2845}
Adrian Bunka6ababd2007-11-05 18:07:33 +01002846#endif /* 0 */
James Smart2534ba72007-04-25 09:52:20 -04002847
James Smart488d1462006-03-07 15:02:37 -05002848/*
James Smart685f0bf2007-04-25 09:53:08 -04002849 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05002850 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05002851 */
2852struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002853lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05002854{
James Smart2e0fef82007-06-17 19:56:36 -05002855 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05002856 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05002857
James Smart2e0fef82007-06-17 19:56:36 -05002858 spin_lock_irq(shost->host_lock);
2859 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
2860 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002861 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05002862}
2863
dea31012005-04-17 16:05:31 -05002864void
James Smart2e0fef82007-06-17 19:56:36 -05002865lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2866 uint32_t did)
dea31012005-04-17 16:05:31 -05002867{
2868 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -05002869 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05002870 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002871 init_timer(&ndlp->nlp_delayfunc);
2872 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2873 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2874 ndlp->nlp_DID = did;
James Smart2e0fef82007-06-17 19:56:36 -05002875 ndlp->vport = vport;
dea31012005-04-17 16:05:31 -05002876 ndlp->nlp_sid = NLP_NO_SID;
James Smart685f0bf2007-04-25 09:53:08 -04002877 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart329f9bc2007-04-25 09:53:01 -04002878 kref_init(&ndlp->kref);
James Smart858c9f62007-06-17 19:56:39 -05002879
2880 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2881 "node init: did:x%x",
2882 ndlp->nlp_DID, 0, 0);
2883
dea31012005-04-17 16:05:31 -05002884 return;
2885}
James Smart329f9bc2007-04-25 09:53:01 -04002886
James Smart98c9ea52007-10-27 13:37:33 -04002887/* This routine releases all resources associated with a specifc NPort's ndlp
2888 * and mempool_free's the nodelist.
2889 */
James Smart311464e2007-08-02 11:10:37 -04002890static void
James Smart329f9bc2007-04-25 09:53:01 -04002891lpfc_nlp_release(struct kref *kref)
2892{
2893 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
2894 kref);
James Smart858c9f62007-06-17 19:56:39 -05002895
2896 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2897 "node release: did:x%x flg:x%x type:x%x",
2898 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2899
James Smart2e0fef82007-06-17 19:56:36 -05002900 lpfc_nlp_remove(ndlp->vport, ndlp);
2901 mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04002902}
2903
James Smart98c9ea52007-10-27 13:37:33 -04002904/* This routine bumps the reference count for a ndlp structure to ensure
2905 * that one discovery thread won't free a ndlp while another discovery thread
2906 * is using it.
2907 */
James Smart329f9bc2007-04-25 09:53:01 -04002908struct lpfc_nodelist *
2909lpfc_nlp_get(struct lpfc_nodelist *ndlp)
2910{
James Smart98c9ea52007-10-27 13:37:33 -04002911 if (ndlp) {
2912 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2913 "node get: did:x%x flg:x%x refcnt:x%x",
2914 ndlp->nlp_DID, ndlp->nlp_flag,
2915 atomic_read(&ndlp->kref.refcount));
James Smart329f9bc2007-04-25 09:53:01 -04002916 kref_get(&ndlp->kref);
James Smart98c9ea52007-10-27 13:37:33 -04002917 }
James Smart329f9bc2007-04-25 09:53:01 -04002918 return ndlp;
2919}
2920
James Smart98c9ea52007-10-27 13:37:33 -04002921
2922/* This routine decrements the reference count for a ndlp structure. If the
2923 * count goes to 0, this indicates the the associated nodelist should be freed.
2924 */
James Smart329f9bc2007-04-25 09:53:01 -04002925int
2926lpfc_nlp_put(struct lpfc_nodelist *ndlp)
2927{
James Smart98c9ea52007-10-27 13:37:33 -04002928 if (ndlp) {
2929 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2930 "node put: did:x%x flg:x%x refcnt:x%x",
2931 ndlp->nlp_DID, ndlp->nlp_flag,
2932 atomic_read(&ndlp->kref.refcount));
2933 }
James Smart329f9bc2007-04-25 09:53:01 -04002934 return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
2935}
James Smart98c9ea52007-10-27 13:37:33 -04002936
2937/* This routine free's the specified nodelist if it is not in use
2938 * by any other discovery thread. This routine returns 1 if the ndlp
2939 * is not being used by anyone and has been freed. A return value of
2940 * 0 indicates it is being used by another discovery thread and the
2941 * refcount is left unchanged.
2942 */
2943int
2944lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
2945{
2946 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2947 "node not used: did:x%x flg:x%x refcnt:x%x",
2948 ndlp->nlp_DID, ndlp->nlp_flag,
2949 atomic_read(&ndlp->kref.refcount));
2950
2951 if (atomic_read(&ndlp->kref.refcount) == 1) {
2952 lpfc_nlp_put(ndlp);
2953 return 1;
2954 }
2955 return 0;
2956}
2957