blob: f64ce88e8a068c849f89211c20ca387d8226402e [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 *);
dea31012005-04-17 16:05:31 -050060
James Smartc01f3202006-08-18 17:47:08 -040061void
62lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050063{
James Smartc01f3202006-08-18 17:47:08 -040064 struct lpfc_rport_data *rdata;
65 struct lpfc_nodelist * ndlp;
66 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050067
James Smartc01f3202006-08-18 17:47:08 -040068 rdata = rport->dd_data;
69 ndlp = rdata->pnode;
70
71 if (!ndlp) {
72 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
73 printk(KERN_ERR "Cannot find remote node"
74 " to terminate I/O Data x%x\n",
75 rport->port_id);
dea31012005-04-17 16:05:31 -050076 return;
77 }
78
James Smart2e0fef82007-06-17 19:56:36 -050079 phba = ndlp->vport->phba;
James Smart1a169682006-03-07 15:04:06 -050080
James Smart858c9f62007-06-17 19:56:39 -050081 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
82 "rport terminate: sid:x%x did:x%x flg:x%x",
83 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
84
James Smartc01f3202006-08-18 17:47:08 -040085 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040086 lpfc_sli_abort_iocb(ndlp->vport,
87 &phba->sli.ring[phba->sli.fcp_ring],
88 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040089 }
James Smartc01f3202006-08-18 17:47:08 -040090
James Smart51ef4c22007-08-02 11:10:31 -040091 /*
92 * A device is normally blocked for rediscovery and unblocked when
93 * devloss timeout happens. In case a vport is removed or driver
94 * unloaded before devloss timeout happens, we need to unblock here.
95 */
96 scsi_target_unblock(&rport->dev);
James Smartc01f3202006-08-18 17:47:08 -040097 return;
98}
99
100/*
101 * This function will be called when dev_loss_tmo fire.
102 */
103void
104lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
105{
106 struct lpfc_rport_data *rdata;
107 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500108 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500109 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500110 struct lpfc_work_evt *evtp;
James Smartc01f3202006-08-18 17:47:08 -0400111
112 rdata = rport->dd_data;
113 ndlp = rdata->pnode;
114
115 if (!ndlp) {
James Smart92d7f7b2007-06-17 19:56:38 -0500116 if (rport->scsi_target_id != -1) {
James Smartc01f3202006-08-18 17:47:08 -0400117 printk(KERN_ERR "Cannot find remote node"
James Smart92d7f7b2007-06-17 19:56:38 -0500118 " for rport in dev_loss_tmo_callbk x%x\n",
119 rport->port_id);
120 }
James Smartc01f3202006-08-18 17:47:08 -0400121 return;
122 }
123
James Smart858c9f62007-06-17 19:56:39 -0500124 vport = ndlp->vport;
125 phba = vport->phba;
126
127 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
128 "rport devlosscb: sid:x%x did:x%x flg:x%x",
129 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
130
James Smart858c9f62007-06-17 19:56:39 -0500131 evtp = &ndlp->dev_loss_evt;
132
133 if (!list_empty(&evtp->evt_listp))
134 return;
135
136 spin_lock_irq(&phba->hbalock);
137 evtp->evt_arg1 = ndlp;
James Smart858c9f62007-06-17 19:56:39 -0500138 evtp->evt = LPFC_EVT_DEV_LOSS;
139 list_add_tail(&evtp->evt_listp, &phba->work_list);
140 if (phba->work_wait)
141 wake_up(phba->work_wait);
142
143 spin_unlock_irq(&phba->hbalock);
144
James Smart858c9f62007-06-17 19:56:39 -0500145 return;
146}
147
148/*
149 * This function is called from the worker thread when dev_loss_tmo
150 * expire.
151 */
152void
153lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
154{
155 struct lpfc_rport_data *rdata;
156 struct fc_rport *rport;
157 struct lpfc_vport *vport;
158 struct lpfc_hba *phba;
159 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400160 int put_node;
161 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500162 int warn_on = 0;
163
164 rport = ndlp->rport;
165
166 if (!rport)
167 return;
168
169 rdata = rport->dd_data;
170 name = (uint8_t *) &ndlp->nlp_portname;
171 vport = ndlp->vport;
172 phba = vport->phba;
173
174 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
175 "rport devlosstmo:did:x%x type:x%x id:x%x",
176 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
177
178 if (!(vport->load_flag & FC_UNLOADING) &&
179 ndlp->nlp_state == NLP_STE_MAPPED_NODE)
180 return;
181
James Smart92d7f7b2007-06-17 19:56:38 -0500182 if (ndlp->nlp_type & NLP_FABRIC) {
183 /* We will clean up these Nodes in linkup */
184 put_node = rdata->pnode != NULL;
185 put_rport = ndlp->rport != NULL;
186 rdata->pnode = NULL;
187 ndlp->rport = NULL;
188 if (put_node)
189 lpfc_nlp_put(ndlp);
190 if (put_rport)
191 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400192 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500193 }
James Smart82085712007-04-25 09:52:41 -0400194
dea31012005-04-17 16:05:31 -0500195 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400196 warn_on = 1;
dea31012005-04-17 16:05:31 -0500197 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400198 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
199 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500200 }
James Smart2e0fef82007-06-17 19:56:36 -0500201 if (vport->load_flag & FC_UNLOADING)
James Smartc01f3202006-08-18 17:47:08 -0400202 warn_on = 0;
203
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400204 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400205 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
206 "0203 Devloss timeout on "
207 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
208 "NPort x%x Data: x%x x%x x%x\n",
209 *name, *(name+1), *(name+2), *(name+3),
210 *(name+4), *(name+5), *(name+6), *(name+7),
211 ndlp->nlp_DID, ndlp->nlp_flag,
212 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400213 } else {
James Smarte8b62012007-08-02 11:10:09 -0400214 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
215 "0204 Devloss timeout on "
216 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
217 "NPort x%x Data: x%x x%x x%x\n",
218 *name, *(name+1), *(name+2), *(name+3),
219 *(name+4), *(name+5), *(name+6), *(name+7),
220 ndlp->nlp_DID, ndlp->nlp_flag,
221 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400222 }
223
James Smart87af33f2007-10-27 13:37:43 -0400224 put_node = rdata->pnode != NULL;
225 put_rport = ndlp->rport != NULL;
226 rdata->pnode = NULL;
227 ndlp->rport = NULL;
228 if (put_node)
229 lpfc_nlp_put(ndlp);
230 if (put_rport)
231 put_device(&rport->dev);
232
James Smart2e0fef82007-06-17 19:56:36 -0500233 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400234 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400235 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smart87af33f2007-10-27 13:37:43 -0400236 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -0500237 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart1dcb58e2007-04-25 09:51:30 -0400238 }
James Smart92d7f7b2007-06-17 19:56:38 -0500239}
James Smartc01f3202006-08-18 17:47:08 -0400240
James Smart92d7f7b2007-06-17 19:56:38 -0500241
242void
243lpfc_worker_wake_up(struct lpfc_hba *phba)
244{
245 wake_up(phba->work_wait);
dea31012005-04-17 16:05:31 -0500246 return;
247}
248
249static void
James Smart2e0fef82007-06-17 19:56:36 -0500250lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500251{
252 struct lpfc_work_evt *evtp = NULL;
253 struct lpfc_nodelist *ndlp;
254 int free_evt;
255
James Smart2e0fef82007-06-17 19:56:36 -0500256 spin_lock_irq(&phba->hbalock);
257 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500258 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
259 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500260 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500261 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500262 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500263 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500264 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500265 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500266 free_evt = 0; /* evt is part of ndlp */
dea31012005-04-17 16:05:31 -0500267 break;
James Smart858c9f62007-06-17 19:56:39 -0500268 case LPFC_EVT_DEV_LOSS:
269 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
270 lpfc_nlp_get(ndlp);
271 lpfc_dev_loss_tmo_handler(ndlp);
272 free_evt = 0;
James Smart858c9f62007-06-17 19:56:39 -0500273 lpfc_nlp_put(ndlp);
274 break;
dea31012005-04-17 16:05:31 -0500275 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500276 if (phba->link_state < LPFC_LINK_DOWN)
277 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500278 else
James Smart2e0fef82007-06-17 19:56:36 -0500279 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500280 complete((struct completion *)(evtp->evt_arg2));
281 break;
James Smart46fa3112007-04-25 09:51:45 -0400282 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500283 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400284 lpfc_offline_prep(phba);
285 *(int *)(evtp->evt_arg1) = 0;
286 complete((struct completion *)(evtp->evt_arg2));
287 break;
288 case LPFC_EVT_OFFLINE:
289 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500290 lpfc_sli_brdrestart(phba);
291 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400292 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
293 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500294 complete((struct completion *)(evtp->evt_arg2));
295 break;
296 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400297 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500298 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500299 lpfc_sli_brdreset(phba);
300 lpfc_hba_down_post(phba);
301 *(int *)(evtp->evt_arg1) =
302 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400303 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500304 complete((struct completion *)(evtp->evt_arg2));
305 break;
306 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400307 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500308 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500309 = (phba->pport->stopped)
310 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400311 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500312 complete((struct completion *)(evtp->evt_arg2));
313 break;
314 }
315 if (free_evt)
316 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500317 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500318 }
James Smart2e0fef82007-06-17 19:56:36 -0500319 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500320
321}
322
James Smart311464e2007-08-02 11:10:37 -0400323static void
James Smart2e0fef82007-06-17 19:56:36 -0500324lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500325{
326 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500327 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400328 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400329 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400330 int i;
dea31012005-04-17 16:05:31 -0500331
James Smart2e0fef82007-06-17 19:56:36 -0500332 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500333 ha_copy = phba->work_ha;
334 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500335 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500336
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500337 if (ha_copy & HA_ERATT)
dea31012005-04-17 16:05:31 -0500338 lpfc_handle_eratt(phba);
339
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500340 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500341 lpfc_sli_handle_mb_event(phba);
342
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500343 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500344 lpfc_handle_latt(phba);
James Smart549e55c2007-08-02 11:09:51 -0400345 vports = lpfc_create_vport_work_array(phba);
346 if (vports != NULL)
James Smart51ef4c22007-08-02 11:10:31 -0400347 for(i = 0; i < LPFC_MAX_VPORTS; i++) {
348 /*
349 * We could have no vports in array if unloading, so if
350 * this happens then just use the pport
351 */
352 if (vports[i] == NULL && i == 0)
353 vport = phba->pport;
354 else
355 vport = vports[i];
356 if (vport == NULL)
357 break;
358 work_port_events = vport->work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400359 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400360 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400361 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400362 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400363 if (work_port_events & WORKER_HB_TMO)
364 lpfc_hb_timeout_handler(phba);
365 if (work_port_events & WORKER_MBOX_TMO)
366 lpfc_mbox_timeout_handler(phba);
367 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
368 lpfc_unblock_fabric_iocbs(phba);
369 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400370 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400371 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
372 lpfc_ramp_down_queue_handler(phba);
373 if (work_port_events & WORKER_RAMP_UP_QUEUE)
374 lpfc_ramp_up_queue_handler(phba);
James Smart51ef4c22007-08-02 11:10:31 -0400375 spin_lock_irq(&vport->work_port_lock);
376 vport->work_port_events &= ~work_port_events;
377 spin_unlock_irq(&vport->work_port_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500378 }
James Smart549e55c2007-08-02 11:09:51 -0400379 lpfc_destroy_vport_work_array(vports);
dea31012005-04-17 16:05:31 -0500380
James Smart858c9f62007-06-17 19:56:39 -0500381 pring = &phba->sli.ring[LPFC_ELS_RING];
382 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
383 status >>= (4*LPFC_ELS_RING);
384 if ((status & HA_RXMASK)
385 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400386 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500387 pring->flag |= LPFC_DEFERRED_RING_EVENT;
388 } else {
389 lpfc_sli_handle_slow_ring_event(phba, pring,
390 (status &
391 HA_RXMASK));
392 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
393 }
394 /*
395 * Turn on Ring interrupts
396 */
397 spin_lock_irq(&phba->hbalock);
398 control = readl(phba->HCregaddr);
399 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
James Smarta58cbd52007-08-02 11:09:43 -0400400 lpfc_debugfs_slow_ring_trc(phba,
401 "WRK Enable ring: cntl:x%x hacopy:x%x",
402 control, ha_copy, 0);
403
James Smart858c9f62007-06-17 19:56:39 -0500404 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -0500405 writel(control, phba->HCregaddr);
406 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500407 }
James Smarta58cbd52007-08-02 11:09:43 -0400408 else {
409 lpfc_debugfs_slow_ring_trc(phba,
410 "WRK Ring ok: cntl:x%x hacopy:x%x",
411 control, ha_copy, 0);
412 }
James Smart858c9f62007-06-17 19:56:39 -0500413 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500414 }
James Smart2e0fef82007-06-17 19:56:36 -0500415 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500416}
417
418static int
James Smart2e0fef82007-06-17 19:56:36 -0500419check_work_wait_done(struct lpfc_hba *phba)
420{
James Smart92d7f7b2007-06-17 19:56:38 -0500421 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400422 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart858c9f62007-06-17 19:56:39 -0500423 int rc = 0;
James Smarted957682007-06-17 19:56:37 -0500424
James Smart2e0fef82007-06-17 19:56:36 -0500425 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500426 list_for_each_entry(vport, &phba->port_list, listentry) {
427 if (vport->work_port_events) {
428 rc = 1;
James Smart549e55c2007-08-02 11:09:51 -0400429 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500430 }
431 }
James Smart549e55c2007-08-02 11:09:51 -0400432 if (rc || phba->work_ha || (!list_empty(&phba->work_list)) ||
433 kthread_should_stop() || pring->flag & LPFC_DEFERRED_RING_EVENT) {
James Smart2e0fef82007-06-17 19:56:36 -0500434 rc = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500435 phba->work_found++;
James Smart549e55c2007-08-02 11:09:51 -0400436 } else
James Smart92d7f7b2007-06-17 19:56:38 -0500437 phba->work_found = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500438 spin_unlock_irq(&phba->hbalock);
439 return rc;
dea31012005-04-17 16:05:31 -0500440}
441
James Smart92d7f7b2007-06-17 19:56:38 -0500442
dea31012005-04-17 16:05:31 -0500443int
444lpfc_do_work(void *p)
445{
446 struct lpfc_hba *phba = p;
447 int rc;
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800448 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(work_waitq);
dea31012005-04-17 16:05:31 -0500449
450 set_user_nice(current, -20);
451 phba->work_wait = &work_waitq;
James Smart92d7f7b2007-06-17 19:56:38 -0500452 phba->work_found = 0;
dea31012005-04-17 16:05:31 -0500453
454 while (1) {
455
456 rc = wait_event_interruptible(work_waitq,
James Smart92d7f7b2007-06-17 19:56:38 -0500457 check_work_wait_done(phba));
458
dea31012005-04-17 16:05:31 -0500459 BUG_ON(rc);
460
461 if (kthread_should_stop())
462 break;
463
464 lpfc_work_done(phba);
465
James Smart92d7f7b2007-06-17 19:56:38 -0500466 /* If there is alot of slow ring work, like during link up
467 * check_work_wait_done() may cause this thread to not give
468 * up the CPU for very long periods of time. This may cause
469 * soft lockups or other problems. To avoid these situations
470 * give up the CPU here after LPFC_MAX_WORKER_ITERATION
471 * consecutive iterations.
472 */
473 if (phba->work_found >= LPFC_MAX_WORKER_ITERATION) {
474 phba->work_found = 0;
475 schedule();
476 }
dea31012005-04-17 16:05:31 -0500477 }
478 phba->work_wait = NULL;
479 return 0;
480}
481
482/*
483 * This is only called to handle FC worker events. Since this a rare
484 * occurance, we allocate a struct lpfc_work_evt structure here instead of
485 * embedding it in the IOCB.
486 */
487int
James Smart2e0fef82007-06-17 19:56:36 -0500488lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500489 uint32_t evt)
490{
491 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500492 unsigned long flags;
dea31012005-04-17 16:05:31 -0500493
494 /*
495 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
496 * be queued to worker thread for processing
497 */
James Smart92d7f7b2007-06-17 19:56:38 -0500498 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500499 if (!evtp)
500 return 0;
501
502 evtp->evt_arg1 = arg1;
503 evtp->evt_arg2 = arg2;
504 evtp->evt = evt;
505
James Smarted957682007-06-17 19:56:37 -0500506 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400507 list_add_tail(&evtp->evt_listp, &phba->work_list);
dea31012005-04-17 16:05:31 -0500508 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -0500509 lpfc_worker_wake_up(phba);
James Smarted957682007-06-17 19:56:37 -0500510 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500511
512 return 1;
513}
514
James Smart92d7f7b2007-06-17 19:56:38 -0500515void
516lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
517{
518 struct lpfc_hba *phba = vport->phba;
519 struct lpfc_nodelist *ndlp, *next_ndlp;
520 int rc;
521
522 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
523 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
524 continue;
525
James Smart98c9ea52007-10-27 13:37:33 -0400526 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
527 ((vport->port_type == LPFC_NPIV_PORT) &&
528 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500529 lpfc_unreg_rpi(vport, ndlp);
530
531 /* Leave Fabric nodes alone on link down */
532 if (!remove && ndlp->nlp_type & NLP_FABRIC)
533 continue;
534 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
535 remove
536 ? NLP_EVT_DEVICE_RM
537 : NLP_EVT_DEVICE_RECOVERY);
538 }
539 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
540 lpfc_mbx_unreg_vpi(vport);
541 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
542 }
543}
544
James Smart87af33f2007-10-27 13:37:43 -0400545void
James Smart98c9ea52007-10-27 13:37:33 -0400546lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500547{
James Smart92d7f7b2007-06-17 19:56:38 -0500548 /* Cleanup any outstanding RSCN activity */
549 lpfc_els_flush_rscn(vport);
550
551 /* Cleanup any outstanding ELS commands */
552 lpfc_els_flush_cmd(vport);
553
554 lpfc_cleanup_rpis(vport, 0);
555
James Smart92d7f7b2007-06-17 19:56:38 -0500556 /* Turn off discovery timer if its running */
557 lpfc_can_disctmo(vport);
558}
559
James Smart98c9ea52007-10-27 13:37:33 -0400560static void
561lpfc_linkdown_port(struct lpfc_vport *vport)
562{
563 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
564
565 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
566
567 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
568 "Link Down: state:x%x rtry:x%x flg:x%x",
569 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
570
571 lpfc_port_link_failure(vport);
572
573}
574
dea31012005-04-17 16:05:31 -0500575int
James Smart685f0bf2007-04-25 09:53:08 -0400576lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500577{
James Smart2e0fef82007-06-17 19:56:36 -0500578 struct lpfc_vport *vport = phba->pport;
579 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400580 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400581 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400582 int i;
dea31012005-04-17 16:05:31 -0500583
James Smart2e0fef82007-06-17 19:56:36 -0500584 if (phba->link_state == LPFC_LINK_DOWN) {
585 return 0;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500586 }
James Smart2e0fef82007-06-17 19:56:36 -0500587 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500588 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500589 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500590 phba->pport->fc_flag &= ~FC_LBIT;
591 }
James Smart2e0fef82007-06-17 19:56:36 -0500592 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400593 vports = lpfc_create_vport_work_array(phba);
594 if (vports != NULL)
595 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
596 /* Issue a LINK DOWN event to all nodes */
597 lpfc_linkdown_port(vports[i]);
598 }
599 lpfc_destroy_vport_work_array(vports);
dea31012005-04-17 16:05:31 -0500600 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500601 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
602 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500603 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500604 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500605 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400606 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500607 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500608 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500609 }
610 }
611
dea31012005-04-17 16:05:31 -0500612 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500613 if (phba->pport->fc_flag & FC_PT2PT) {
614 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500615 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
616 if (mb) {
dea31012005-04-17 16:05:31 -0500617 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500618 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500619 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400620 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500621 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500622 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500623 }
624 }
James Smart2e0fef82007-06-17 19:56:36 -0500625 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500626 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500627 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500628 }
James Smart2e0fef82007-06-17 19:56:36 -0500629
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500630 return 0;
dea31012005-04-17 16:05:31 -0500631}
632
James Smart92d7f7b2007-06-17 19:56:38 -0500633static void
634lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500635{
James Smart92d7f7b2007-06-17 19:56:38 -0500636 struct lpfc_nodelist *ndlp;
637
638 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
639 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
640 continue;
641
642 if (ndlp->nlp_type & NLP_FABRIC) {
643 /* On Linkup its safe to clean up the ndlp
644 * from Fabric connections.
645 */
646 if (ndlp->nlp_DID != Fabric_DID)
647 lpfc_unreg_rpi(vport, ndlp);
648 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
649 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
650 /* Fail outstanding IO now since device is
651 * marked for PLOGI.
652 */
653 lpfc_unreg_rpi(vport, ndlp);
654 }
655 }
656}
657
658static void
659lpfc_linkup_port(struct lpfc_vport *vport)
660{
661 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500662 struct lpfc_hba *phba = vport->phba;
663
664 if ((vport->load_flag & FC_UNLOADING) != 0)
665 return;
666
James Smart858c9f62007-06-17 19:56:39 -0500667 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
668 "Link Up: top:x%x speed:x%x flg:x%x",
669 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
670
James Smart92d7f7b2007-06-17 19:56:38 -0500671 /* If NPIV is not enabled, only bring the physical port up */
672 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
673 (vport != phba->pport))
674 return;
dea31012005-04-17 16:05:31 -0500675
James Smart2e0fef82007-06-17 19:56:36 -0500676 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400677
James Smart2e0fef82007-06-17 19:56:36 -0500678 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500679 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
680 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
681 vport->fc_flag |= FC_NDISC_ACTIVE;
682 vport->fc_ns_retry = 0;
683 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500684
James Smart92d7f7b2007-06-17 19:56:38 -0500685 if (vport->fc_flag & FC_LBIT)
686 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500687
James Smart92d7f7b2007-06-17 19:56:38 -0500688}
689
690static int
691lpfc_linkup(struct lpfc_hba *phba)
692{
James Smart549e55c2007-08-02 11:09:51 -0400693 struct lpfc_vport **vports;
694 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500695
696 phba->link_state = LPFC_LINK_UP;
697
698 /* Unblock fabric iocbs if they are blocked */
699 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
700 del_timer_sync(&phba->fabric_block_timer);
701
James Smart549e55c2007-08-02 11:09:51 -0400702 vports = lpfc_create_vport_work_array(phba);
703 if (vports != NULL)
704 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++)
705 lpfc_linkup_port(vports[i]);
706 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500707 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
708 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500709
710 return 0;
711}
712
713/*
714 * This routine handles processing a CLEAR_LA mailbox
715 * command upon completion. It is setup in the LPFC_MBOXQ
716 * as the completion routine when the command is
717 * handed off to the SLI layer.
718 */
719void
James Smart2e0fef82007-06-17 19:56:36 -0500720lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500721{
James Smart2e0fef82007-06-17 19:56:36 -0500722 struct lpfc_vport *vport = pmb->vport;
723 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
724 struct lpfc_sli *psli = &phba->sli;
725 MAILBOX_t *mb = &pmb->mb;
dea31012005-04-17 16:05:31 -0500726 uint32_t control;
727
dea31012005-04-17 16:05:31 -0500728 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500729 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500730 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
731 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
732
733 /* Check for error */
734 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500735 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400736 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
737 "0320 CLEAR_LA mbxStatus error x%x hba "
738 "state x%x\n",
739 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500740 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500741 goto out;
742 }
743
James Smart92d7f7b2007-06-17 19:56:38 -0500744 if (vport->port_type == LPFC_PHYSICAL_PORT)
745 phba->link_state = LPFC_HBA_READY;
746
747 spin_lock_irq(&phba->hbalock);
748 psli->sli_flag |= LPFC_PROCESS_LA;
749 control = readl(phba->HCregaddr);
750 control |= HC_LAINT_ENA;
751 writel(control, phba->HCregaddr);
752 readl(phba->HCregaddr); /* flush */
753 spin_unlock_irq(&phba->hbalock);
754 return;
dea31012005-04-17 16:05:31 -0500755
James Smart2e0fef82007-06-17 19:56:36 -0500756 vport->num_disc_nodes = 0;
757 /* go thru NPR nodes and issue ELS PLOGIs */
758 if (vport->fc_npr_cnt)
759 lpfc_els_disc_plogi(vport);
760
761 if (!vport->num_disc_nodes) {
762 spin_lock_irq(shost->host_lock);
763 vport->fc_flag &= ~FC_NDISC_ACTIVE;
764 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500765 }
766
James Smart2e0fef82007-06-17 19:56:36 -0500767 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -0500768
769out:
770 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400771 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
772 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500773 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500774
James Smart2e0fef82007-06-17 19:56:36 -0500775 spin_lock_irq(shost->host_lock);
776 vport->fc_flag &= ~(FC_ABORT_DISCOVERY | FC_ESTABLISH_LINK);
777 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500778
779 del_timer_sync(&phba->fc_estabtmo);
780
James Smart2e0fef82007-06-17 19:56:36 -0500781 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500782
783 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500784
785 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500786 psli->sli_flag |= LPFC_PROCESS_LA;
787 control = readl(phba->HCregaddr);
788 control |= HC_LAINT_ENA;
789 writel(control, phba->HCregaddr);
790 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500791 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500792
793 return;
794}
795
James Smart2e0fef82007-06-17 19:56:36 -0500796
dea31012005-04-17 16:05:31 -0500797static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500798lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500799{
James Smart2e0fef82007-06-17 19:56:36 -0500800 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500801
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500802 if (pmb->mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500803 goto out;
dea31012005-04-17 16:05:31 -0500804
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500805 mempool_free(pmb, phba->mbox_mem_pool);
806
807 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500808 vport->fc_flag & FC_PUBLIC_LOOP &&
809 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500810 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500811 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500812 * LPFC_LOCAL_CFG_LINK while waiting for FAN
813 */
James Smart2e0fef82007-06-17 19:56:36 -0500814 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500815 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500816 }
dea31012005-04-17 16:05:31 -0500817
James Smart2e0fef82007-06-17 19:56:36 -0500818 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500819 * LPFC_FLOGI while waiting for FLOGI cmpl
820 */
James Smart92d7f7b2007-06-17 19:56:38 -0500821 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500822 lpfc_initial_flogi(vport);
823 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500824 return;
dea31012005-04-17 16:05:31 -0500825
826out:
James Smarte8b62012007-08-02 11:10:09 -0400827 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
828 "0306 CONFIG_LINK mbxStatus error x%x "
829 "HBA state x%x\n",
830 pmb->mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500831 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500832
833 lpfc_linkdown(phba);
834
James Smarte8b62012007-08-02 11:10:09 -0400835 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
836 "0200 CONFIG_LINK bad hba state x%x\n",
837 vport->port_state);
dea31012005-04-17 16:05:31 -0500838
James Smart92d7f7b2007-06-17 19:56:38 -0500839 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500840 return;
841}
842
843static void
James Smart2e0fef82007-06-17 19:56:36 -0500844lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500845{
dea31012005-04-17 16:05:31 -0500846 MAILBOX_t *mb = &pmb->mb;
847 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500848 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500849
850
851 /* Check for error */
852 if (mb->mbxStatus) {
853 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400854 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
855 "0319 READ_SPARAM mbxStatus error x%x "
856 "hba state x%x>\n",
857 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -0500858 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -0500859 goto out;
860 }
861
James Smart2e0fef82007-06-17 19:56:36 -0500862 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -0500863 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -0500864 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500865 u64_to_wwn(phba->cfg_soft_wwnn,
866 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400867 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500868 u64_to_wwn(phba->cfg_soft_wwpn,
869 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -0500870 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
871 sizeof(vport->fc_nodename));
872 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
873 sizeof(vport->fc_portname));
874 if (vport->port_type == LPFC_PHYSICAL_PORT) {
875 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
876 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
877 }
878
dea31012005-04-17 16:05:31 -0500879 lpfc_mbuf_free(phba, mp->virt, mp->phys);
880 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -0500881 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500882 return;
883
884out:
885 pmb->context1 = NULL;
886 lpfc_mbuf_free(phba, mp->virt, mp->phys);
887 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -0500888 lpfc_issue_clear_la(phba, vport);
889 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500890 return;
891}
892
893static void
James Smart92d7f7b2007-06-17 19:56:38 -0500894lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -0500895{
James Smart92d7f7b2007-06-17 19:56:38 -0500896 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500897 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
James Smart2e0fef82007-06-17 19:56:36 -0500898 int i;
James Smart14691152006-12-02 13:34:28 -0500899 struct lpfc_dmabuf *mp;
900 int rc;
901
dea31012005-04-17 16:05:31 -0500902 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
903 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
904
James Smart92d7f7b2007-06-17 19:56:38 -0500905 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500906 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -0500907 case LA_1GHZ_LINK:
908 phba->fc_linkspeed = LA_1GHZ_LINK;
909 break;
910 case LA_2GHZ_LINK:
911 phba->fc_linkspeed = LA_2GHZ_LINK;
912 break;
913 case LA_4GHZ_LINK:
914 phba->fc_linkspeed = LA_4GHZ_LINK;
915 break;
916 case LA_8GHZ_LINK:
917 phba->fc_linkspeed = LA_8GHZ_LINK;
918 break;
919 default:
920 phba->fc_linkspeed = LA_UNKNW_LINK;
921 break;
dea31012005-04-17 16:05:31 -0500922 }
923
924 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -0500925 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -0500926
927 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -0500928 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -0500929
James Smart92d7f7b2007-06-17 19:56:38 -0500930 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -0500931 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -0500932 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500933
James Smart2e0fef82007-06-17 19:56:36 -0500934 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -0500935 i = la->un.lilpBde64.tus.f.bdeSize;
936
937 if (i == 0) {
938 phba->alpa_map[0] = 0;
939 } else {
James Smarte8b62012007-08-02 11:10:09 -0400940 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -0500941 int numalpa, j, k;
942 union {
943 uint8_t pamap[16];
944 struct {
945 uint32_t wd1;
946 uint32_t wd2;
947 uint32_t wd3;
948 uint32_t wd4;
949 } pa;
950 } un;
951 numalpa = phba->alpa_map[0];
952 j = 0;
953 while (j < numalpa) {
954 memset(un.pamap, 0, 16);
955 for (k = 1; j < numalpa; k++) {
956 un.pamap[k - 1] =
957 phba->alpa_map[j + 1];
958 j++;
959 if (k == 16)
960 break;
961 }
962 /* Link Up Event ALPA map */
963 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -0500964 KERN_WARNING,
965 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400966 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -0500967 "ALPA map Data: x%x "
968 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500969 un.pa.wd1, un.pa.wd2,
970 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -0500971 }
972 }
973 }
974 } else {
James Smart92d7f7b2007-06-17 19:56:38 -0500975 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -0400976 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -0500977 (phba->sli_rev == 3))
978 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
979 }
James Smart2e0fef82007-06-17 19:56:36 -0500980 vport->fc_myDID = phba->fc_pref_DID;
981 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500982 }
James Smart92d7f7b2007-06-17 19:56:38 -0500983 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500984
985 lpfc_linkup(phba);
986 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -0500987 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -0500988 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -0500989 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -0400990 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -0500991 if (rc == MBX_NOT_FINISHED) {
992 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
993 lpfc_mbuf_free(phba, mp->virt, mp->phys);
994 kfree(mp);
995 mempool_free(sparam_mbox, phba->mbox_mem_pool);
996 if (cfglink_mbox)
997 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -0500998 goto out;
James Smart14691152006-12-02 13:34:28 -0500999 }
dea31012005-04-17 16:05:31 -05001000 }
1001
1002 if (cfglink_mbox) {
James Smart2e0fef82007-06-17 19:56:36 -05001003 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05001004 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05001005 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001006 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04001007 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001008 if (rc != MBX_NOT_FINISHED)
1009 return;
1010 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001011 }
James Smart92d7f7b2007-06-17 19:56:38 -05001012out:
1013 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001014 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1015 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1016 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05001017 lpfc_issue_clear_la(phba, vport);
1018 return;
dea31012005-04-17 16:05:31 -05001019}
1020
1021static void
James Smart2e0fef82007-06-17 19:56:36 -05001022lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1023{
dea31012005-04-17 16:05:31 -05001024 uint32_t control;
1025 struct lpfc_sli *psli = &phba->sli;
1026
1027 lpfc_linkdown(phba);
1028
1029 /* turn on Link Attention interrupts - no CLEAR_LA needed */
James Smart2e0fef82007-06-17 19:56:36 -05001030 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001031 psli->sli_flag |= LPFC_PROCESS_LA;
1032 control = readl(phba->HCregaddr);
1033 control |= HC_LAINT_ENA;
1034 writel(control, phba->HCregaddr);
1035 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001036 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001037}
1038
1039/*
1040 * This routine handles processing a READ_LA mailbox
1041 * command upon completion. It is setup in the LPFC_MBOXQ
1042 * as the completion routine when the command is
1043 * handed off to the SLI layer.
1044 */
1045void
James Smart2e0fef82007-06-17 19:56:36 -05001046lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001047{
James Smart2e0fef82007-06-17 19:56:36 -05001048 struct lpfc_vport *vport = pmb->vport;
1049 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05001050 READ_LA_VAR *la;
1051 MAILBOX_t *mb = &pmb->mb;
1052 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1053
1054 /* Check for error */
1055 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05001056 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001057 "1307 READ_LA mbox error x%x state x%x\n",
1058 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001059 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001060 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001061 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1062 }
1063
1064 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1065
1066 memcpy(&phba->alpa_map[0], mp->virt, 128);
1067
James Smart2e0fef82007-06-17 19:56:36 -05001068 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001069 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05001070 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001071 else
James Smart2e0fef82007-06-17 19:56:36 -05001072 vport->fc_flag &= ~FC_BYPASSED_MODE;
1073 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001074
dea31012005-04-17 16:05:31 -05001075 if (((phba->fc_eventTag + 1) < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05001076 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05001077 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05001078 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05001079 if (phba->fc_eventTag != 0)
1080 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001081 }
dea31012005-04-17 16:05:31 -05001082
1083 phba->fc_eventTag = la->eventTag;
1084
1085 if (la->attType == AT_LINK_UP) {
1086 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05001087 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart5b8bd0c2007-04-25 09:52:49 -04001088 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001089 "1306 Link Up Event in loop back mode "
1090 "x%x received Data: x%x x%x x%x x%x\n",
1091 la->eventTag, phba->fc_eventTag,
1092 la->granted_AL_PA, la->UlnkSpeed,
1093 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001094 } else {
1095 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001096 "1303 Link Up Event x%x received "
1097 "Data: x%x x%x x%x x%x\n",
1098 la->eventTag, phba->fc_eventTag,
1099 la->granted_AL_PA, la->UlnkSpeed,
1100 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001101 }
James Smart92d7f7b2007-06-17 19:56:38 -05001102 lpfc_mbx_process_link_up(phba, la);
dea31012005-04-17 16:05:31 -05001103 } else {
1104 phba->fc_stat.LinkDown++;
1105 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001106 "1305 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05001107 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001108 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05001109 phba->pport->port_state, vport->fc_flag);
dea31012005-04-17 16:05:31 -05001110 lpfc_mbx_issue_link_down(phba);
1111 }
1112
1113lpfc_mbx_cmpl_read_la_free_mbuf:
1114 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1115 kfree(mp);
1116 mempool_free(pmb, phba->mbox_mem_pool);
1117 return;
1118}
1119
1120/*
1121 * This routine handles processing a REG_LOGIN mailbox
1122 * command upon completion. It is setup in the LPFC_MBOXQ
1123 * as the completion routine when the command is
1124 * handed off to the SLI layer.
1125 */
1126void
James Smart2e0fef82007-06-17 19:56:36 -05001127lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001128{
James Smart2e0fef82007-06-17 19:56:36 -05001129 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001130 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05001131 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001132
dea31012005-04-17 16:05:31 -05001133 pmb->context1 = NULL;
1134
1135 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05001136 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05001137 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1138 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001139 mempool_free(pmb, phba->mbox_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04001140 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001141
1142 return;
1143}
1144
James Smart92d7f7b2007-06-17 19:56:38 -05001145static void
1146lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1147{
1148 MAILBOX_t *mb = &pmb->mb;
1149 struct lpfc_vport *vport = pmb->vport;
1150 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1151
1152 switch (mb->mbxStatus) {
1153 case 0x0011:
1154 case 0x0020:
1155 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04001156 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1157 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
1158 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001159 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001160 }
1161 vport->unreg_vpi_cmpl = VPORT_OK;
1162 mempool_free(pmb, phba->mbox_mem_pool);
1163 /*
1164 * This shost reference might have been taken at the beginning of
1165 * lpfc_vport_delete()
1166 */
1167 if (vport->load_flag & FC_UNLOADING)
1168 scsi_host_put(shost);
1169}
1170
1171void
1172lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1173{
1174 struct lpfc_hba *phba = vport->phba;
1175 LPFC_MBOXQ_t *mbox;
1176 int rc;
1177
1178 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1179 if (!mbox)
1180 return;
1181
1182 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1183 mbox->vport = vport;
1184 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04001185 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001186 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001187 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1188 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001189 mempool_free(mbox, phba->mbox_mem_pool);
1190 vport->unreg_vpi_cmpl = VPORT_ERROR;
1191 }
1192}
1193
1194static void
1195lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1196{
1197 struct lpfc_vport *vport = pmb->vport;
1198 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1199 MAILBOX_t *mb = &pmb->mb;
1200
1201 switch (mb->mbxStatus) {
1202 case 0x0011:
1203 case 0x9601:
1204 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04001205 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1206 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
1207 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001208 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1209 spin_lock_irq(shost->host_lock);
1210 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1211 spin_unlock_irq(shost->host_lock);
1212 vport->fc_myDID = 0;
1213 goto out;
1214 }
James Smart92d7f7b2007-06-17 19:56:38 -05001215
1216 vport->num_disc_nodes = 0;
1217 /* go thru NPR list and issue ELS PLOGIs */
1218 if (vport->fc_npr_cnt)
1219 lpfc_els_disc_plogi(vport);
1220
1221 if (!vport->num_disc_nodes) {
1222 spin_lock_irq(shost->host_lock);
1223 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1224 spin_unlock_irq(shost->host_lock);
1225 lpfc_can_disctmo(vport);
1226 }
1227 vport->port_state = LPFC_VPORT_READY;
1228
1229out:
1230 mempool_free(pmb, phba->mbox_mem_pool);
1231 return;
1232}
1233
dea31012005-04-17 16:05:31 -05001234/*
1235 * This routine handles processing a Fabric REG_LOGIN mailbox
1236 * command upon completion. It is setup in the LPFC_MBOXQ
1237 * as the completion routine when the command is
1238 * handed off to the SLI layer.
1239 */
1240void
James Smart2e0fef82007-06-17 19:56:36 -05001241lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001242{
James Smart92d7f7b2007-06-17 19:56:38 -05001243 struct lpfc_vport *vport = pmb->vport;
James Smart2e0fef82007-06-17 19:56:36 -05001244 MAILBOX_t *mb = &pmb->mb;
1245 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05001246 struct lpfc_nodelist *ndlp;
James Smart549e55c2007-08-02 11:09:51 -04001247 struct lpfc_vport **vports;
1248 int i;
dea31012005-04-17 16:05:31 -05001249
James Smart549e55c2007-08-02 11:09:51 -04001250 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04001251 pmb->context1 = NULL;
1252 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05001253 if (mb->mbxStatus) {
1254 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1255 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001256 mempool_free(pmb, phba->mbox_mem_pool);
1257 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001258
James Smart92d7f7b2007-06-17 19:56:38 -05001259 if (phba->fc_topology == TOPOLOGY_LOOP) {
1260 /* FLOGI failed, use loop map to make discovery list */
1261 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001262
James Smart92d7f7b2007-06-17 19:56:38 -05001263 /* Start discovery */
1264 lpfc_disc_start(vport);
1265 return;
1266 }
1267
1268 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001269 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1270 "0258 Register Fabric login error: 0x%x\n",
1271 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001272 return;
1273 }
1274
dea31012005-04-17 16:05:31 -05001275 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001276 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001277 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001278
James Smart329f9bc2007-04-25 09:53:01 -04001279 lpfc_nlp_put(ndlp); /* Drop the reference from the mbox */
1280
James Smart2e0fef82007-06-17 19:56:36 -05001281 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart549e55c2007-08-02 11:09:51 -04001282 vports = lpfc_create_vport_work_array(phba);
1283 if (vports != NULL)
1284 for(i = 0;
1285 i < LPFC_MAX_VPORTS && vports[i] != NULL;
1286 i++) {
1287 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1288 continue;
1289 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1290 lpfc_initial_fdisc(vports[i]);
1291 else if (phba->sli3_options &
1292 LPFC_SLI3_NPIV_ENABLED) {
1293 lpfc_vport_set_state(vports[i],
1294 FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -04001295 lpfc_printf_vlog(vport, KERN_ERR,
1296 LOG_ELS,
1297 "0259 No NPIV "
1298 "Fabric support\n");
James Smart549e55c2007-08-02 11:09:51 -04001299 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001300 }
James Smart549e55c2007-08-02 11:09:51 -04001301 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001302 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05001303 }
1304
1305 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1306 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001307 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001308 return;
1309}
1310
1311/*
1312 * This routine handles processing a NameServer REG_LOGIN mailbox
1313 * command upon completion. It is setup in the LPFC_MBOXQ
1314 * as the completion routine when the command is
1315 * handed off to the SLI layer.
1316 */
1317void
James Smart2e0fef82007-06-17 19:56:36 -05001318lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001319{
James Smart2e0fef82007-06-17 19:56:36 -05001320 MAILBOX_t *mb = &pmb->mb;
1321 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1322 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1323 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001324
1325 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05001326out:
James Smart329f9bc2007-04-25 09:53:01 -04001327 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001328 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1329 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04001330 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04001331
1332 /* If no other thread is using the ndlp, free it */
1333 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05001334
James Smart92d7f7b2007-06-17 19:56:38 -05001335 if (phba->fc_topology == TOPOLOGY_LOOP) {
1336 /*
1337 * RegLogin failed, use loop map to make discovery
1338 * list
1339 */
1340 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001341
James Smart92d7f7b2007-06-17 19:56:38 -05001342 /* Start discovery */
1343 lpfc_disc_start(vport);
1344 return;
1345 }
1346 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001347 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1348 "0260 Register NameServer error: 0x%x\n",
1349 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001350 return;
1351 }
1352
1353 pmb->context1 = NULL;
1354
dea31012005-04-17 16:05:31 -05001355 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001356 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001357 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001358
James Smart2e0fef82007-06-17 19:56:36 -05001359 if (vport->port_state < LPFC_VPORT_READY) {
1360 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05001361 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1362 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1363 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1364 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1365 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1366
1367 /* Issue SCR just before NameServer GID_FT Query */
1368 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05001369 }
1370
James Smart2e0fef82007-06-17 19:56:36 -05001371 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05001372 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05001373 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05001374 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05001375 goto out;
dea31012005-04-17 16:05:31 -05001376 }
1377
James Smart329f9bc2007-04-25 09:53:01 -04001378 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001379 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1380 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001381 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001382
1383 return;
1384}
1385
1386static void
James Smart2e0fef82007-06-17 19:56:36 -05001387lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001388{
James Smart2e0fef82007-06-17 19:56:36 -05001389 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1390 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05001391 struct lpfc_rport_data *rdata;
1392 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05001393 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001394
1395 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001396 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1397 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001398 rport_ids.port_id = ndlp->nlp_DID;
1399 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05001400
James Smart329f9bc2007-04-25 09:53:01 -04001401 /*
1402 * We leave our node pointer in rport->dd_data when we unregister a
1403 * FCP target port. But fc_remote_port_add zeros the space to which
1404 * rport->dd_data points. So, if we're reusing a previously
1405 * registered port, drop the reference that we took the last time we
1406 * registered the port.
1407 */
1408 if (ndlp->rport && ndlp->rport->dd_data &&
James Smart92d7f7b2007-06-17 19:56:38 -05001409 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp) {
James Smart329f9bc2007-04-25 09:53:01 -04001410 lpfc_nlp_put(ndlp);
1411 }
James Smart858c9f62007-06-17 19:56:39 -05001412
1413 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1414 "rport add: did:x%x flg:x%x type x%x",
1415 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1416
James Smart2e0fef82007-06-17 19:56:36 -05001417 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04001418 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05001419 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1420 "Warning: fc_remote_port_add failed\n");
1421 return;
1422 }
1423
1424 /* initialize static port data */
1425 rport->maxframe_size = ndlp->nlp_maxframe;
1426 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05001427 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04001428 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001429
1430 if (ndlp->nlp_type & NLP_FCP_TARGET)
1431 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1432 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1433 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1434
1435
1436 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1437 fc_remote_port_rolechg(rport, rport_ids.roles);
1438
James Smart071fbd3d2006-04-15 11:53:20 -04001439 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001440 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04001441 ndlp->nlp_sid = rport->scsi_target_id;
1442 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001443 return;
1444}
1445
1446static void
James Smart2e0fef82007-06-17 19:56:36 -05001447lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001448{
1449 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04001450
James Smart858c9f62007-06-17 19:56:39 -05001451 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
1452 "rport delete: did:x%x flg:x%x type x%x",
1453 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1454
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001455 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05001456
1457 return;
1458}
1459
James Smartde0c5b32007-04-25 09:52:27 -04001460static void
James Smart2e0fef82007-06-17 19:56:36 -05001461lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04001462{
James Smart2e0fef82007-06-17 19:56:36 -05001463 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1464
1465 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001466 switch (state) {
1467 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001468 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001469 break;
1470 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001471 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001472 break;
1473 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001474 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001475 break;
1476 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001477 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001478 break;
1479 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001480 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001481 break;
1482 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001483 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001484 break;
1485 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001486 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001487 break;
1488 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001489 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001490 break;
1491 }
James Smart2e0fef82007-06-17 19:56:36 -05001492 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001493}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001494
James Smartde0c5b32007-04-25 09:52:27 -04001495static void
James Smart2e0fef82007-06-17 19:56:36 -05001496lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04001497 int old_state, int new_state)
1498{
James Smart2e0fef82007-06-17 19:56:36 -05001499 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1500
James Smartde0c5b32007-04-25 09:52:27 -04001501 if (new_state == NLP_STE_UNMAPPED_NODE) {
1502 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1503 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1504 ndlp->nlp_type |= NLP_FC_NODE;
1505 }
1506 if (new_state == NLP_STE_MAPPED_NODE)
1507 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1508 if (new_state == NLP_STE_NPR_NODE)
1509 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05001510
James Smartde0c5b32007-04-25 09:52:27 -04001511 /* Transport interface */
1512 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1513 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001514 vport->phba->nport_event_cnt++;
1515 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001516 }
dea31012005-04-17 16:05:31 -05001517
James Smartde0c5b32007-04-25 09:52:27 -04001518 if (new_state == NLP_STE_MAPPED_NODE ||
1519 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05001520 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05001521 /*
1522 * Tell the fc transport about the port, if we haven't
1523 * already. If we have, and it's a scsi entity, be
1524 * sure to unblock any attached scsi devices
1525 */
1526 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001527 }
James Smart858c9f62007-06-17 19:56:39 -05001528 /*
1529 * if we added to Mapped list, but the remote port
1530 * registration failed or assigned a target id outside
1531 * our presentable range - move the node to the
1532 * Unmapped List
1533 */
James Smartde0c5b32007-04-25 09:52:27 -04001534 if (new_state == NLP_STE_MAPPED_NODE &&
1535 (!ndlp->rport ||
1536 ndlp->rport->scsi_target_id == -1 ||
1537 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05001538 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001539 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05001540 spin_unlock_irq(shost->host_lock);
1541 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001542 }
James Smartde0c5b32007-04-25 09:52:27 -04001543}
1544
James Smart685f0bf2007-04-25 09:53:08 -04001545static char *
1546lpfc_nlp_state_name(char *buffer, size_t size, int state)
1547{
1548 static char *states[] = {
1549 [NLP_STE_UNUSED_NODE] = "UNUSED",
1550 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1551 [NLP_STE_ADISC_ISSUE] = "ADISC",
1552 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1553 [NLP_STE_PRLI_ISSUE] = "PRLI",
1554 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1555 [NLP_STE_MAPPED_NODE] = "MAPPED",
1556 [NLP_STE_NPR_NODE] = "NPR",
1557 };
1558
James Smart311464e2007-08-02 11:10:37 -04001559 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04001560 strlcpy(buffer, states[state], size);
1561 else
1562 snprintf(buffer, size, "unknown (%d)", state);
1563 return buffer;
1564}
1565
James Smartde0c5b32007-04-25 09:52:27 -04001566void
James Smart2e0fef82007-06-17 19:56:36 -05001567lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1568 int state)
James Smartde0c5b32007-04-25 09:52:27 -04001569{
James Smart2e0fef82007-06-17 19:56:36 -05001570 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04001571 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04001572 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04001573
James Smarte8b62012007-08-02 11:10:09 -04001574 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1575 "0904 NPort state transition x%06x, %s -> %s\n",
1576 ndlp->nlp_DID,
1577 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1578 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05001579
1580 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1581 "node statechg did:x%x old:%d ste:%d",
1582 ndlp->nlp_DID, old_state, state);
1583
James Smartde0c5b32007-04-25 09:52:27 -04001584 if (old_state == NLP_STE_NPR_NODE &&
1585 (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
1586 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001587 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001588 if (old_state == NLP_STE_UNMAPPED_NODE) {
1589 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1590 ndlp->nlp_type &= ~NLP_FC_NODE;
1591 }
1592
James Smart685f0bf2007-04-25 09:53:08 -04001593 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05001594 spin_lock_irq(shost->host_lock);
1595 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1596 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001597 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05001598 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04001599
1600 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05001601 lpfc_nlp_counters(vport, state, 1);
1602 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04001603}
1604
1605void
James Smart2e0fef82007-06-17 19:56:36 -05001606lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001607{
James Smart2e0fef82007-06-17 19:56:36 -05001608 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1609
James Smartde0c5b32007-04-25 09:52:27 -04001610 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
James Smart2e0fef82007-06-17 19:56:36 -05001611 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001612 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05001613 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1614 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001615 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05001616 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05001617 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
1618 NLP_STE_UNUSED_NODE);
James Smartde0c5b32007-04-25 09:52:27 -04001619}
1620
1621void
James Smart2e0fef82007-06-17 19:56:36 -05001622lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001623{
James Smart87af33f2007-10-27 13:37:43 -04001624 /*
1625 * Use of lpfc_drop_node and UNUSED list. lpfc_drop_node should
1626 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
1627 * the ndlp from the vport. The ndlp resides on the UNUSED list
1628 * until ALL other outstanding threads have completed. Thus, if a
1629 * ndlp is on the UNUSED list already, we should never do another
1630 * lpfc_drop_node() on it.
1631 */
James Smart51ef4c22007-08-02 11:10:31 -04001632 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04001633 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04001634 return;
dea31012005-04-17 16:05:31 -05001635}
1636
1637/*
1638 * Start / ReStart rescue timer for Discovery / RSCN handling
1639 */
1640void
James Smart2e0fef82007-06-17 19:56:36 -05001641lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001642{
James Smart2e0fef82007-06-17 19:56:36 -05001643 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1644 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001645 uint32_t tmo;
1646
James Smart2e0fef82007-06-17 19:56:36 -05001647 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001648 /* For FAN, timeout should be greater then edtov */
1649 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1650 } else {
1651 /* Normal discovery timeout should be > then ELS/CT timeout
1652 * FC spec states we need 3 * ratov for CT requests
1653 */
1654 tmo = ((phba->fc_ratov * 3) + 3);
1655 }
dea31012005-04-17 16:05:31 -05001656
James Smart858c9f62007-06-17 19:56:39 -05001657
1658 if (!timer_pending(&vport->fc_disctmo)) {
1659 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1660 "set disc timer: tmo:x%x state:x%x flg:x%x",
1661 tmo, vport->port_state, vport->fc_flag);
1662 }
1663
James Smart2e0fef82007-06-17 19:56:36 -05001664 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1665 spin_lock_irq(shost->host_lock);
1666 vport->fc_flag |= FC_DISC_TMO;
1667 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001668
1669 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001670 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1671 "0247 Start Discovery Timer state x%x "
1672 "Data: x%x x%lx x%x x%x\n",
1673 vport->port_state, tmo,
1674 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1675 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05001676
1677 return;
1678}
1679
1680/*
1681 * Cancel rescue timer for Discovery / RSCN handling
1682 */
1683int
James Smart2e0fef82007-06-17 19:56:36 -05001684lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001685{
James Smart2e0fef82007-06-17 19:56:36 -05001686 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001687 unsigned long iflags;
1688
James Smart858c9f62007-06-17 19:56:39 -05001689 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1690 "can disc timer: state:x%x rtry:x%x flg:x%x",
1691 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
1692
dea31012005-04-17 16:05:31 -05001693 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05001694 if (vport->fc_flag & FC_DISC_TMO) {
1695 spin_lock_irqsave(shost->host_lock, iflags);
1696 vport->fc_flag &= ~FC_DISC_TMO;
1697 spin_unlock_irqrestore(shost->host_lock, iflags);
1698 del_timer_sync(&vport->fc_disctmo);
1699 spin_lock_irqsave(&vport->work_port_lock, iflags);
1700 vport->work_port_events &= ~WORKER_DISC_TMO;
1701 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05001702 }
1703
1704 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001705 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1706 "0248 Cancel Discovery Timer state x%x "
1707 "Data: x%x x%x x%x\n",
1708 vport->port_state, vport->fc_flag,
1709 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001710 return 0;
dea31012005-04-17 16:05:31 -05001711}
1712
1713/*
1714 * Check specified ring for outstanding IOCB on the SLI queue
1715 * Return true if iocb matches the specified nport
1716 */
1717int
James Smart2e0fef82007-06-17 19:56:36 -05001718lpfc_check_sli_ndlp(struct lpfc_hba *phba,
1719 struct lpfc_sli_ring *pring,
1720 struct lpfc_iocbq *iocb,
1721 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001722{
James Smart2e0fef82007-06-17 19:56:36 -05001723 struct lpfc_sli *psli = &phba->sli;
1724 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05001725 struct lpfc_vport *vport = ndlp->vport;
1726
1727 if (iocb->vport != vport)
1728 return 0;
1729
dea31012005-04-17 16:05:31 -05001730 if (pring->ringno == LPFC_ELS_RING) {
1731 switch (icmd->ulpCommand) {
1732 case CMD_GEN_REQUEST64_CR:
1733 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001734 return 1;
dea31012005-04-17 16:05:31 -05001735 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04001736 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1737 return 1;
dea31012005-04-17 16:05:31 -05001738 case CMD_XMIT_ELS_RSP64_CX:
1739 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001740 return 1;
dea31012005-04-17 16:05:31 -05001741 }
James Smarta4bc3372006-12-02 13:34:16 -05001742 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05001743
1744 } else if (pring->ringno == psli->fcp_ring) {
1745 /* Skip match check if waiting to relogin to FCP target */
1746 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001747 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001748 return 0;
dea31012005-04-17 16:05:31 -05001749 }
1750 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001751 return 1;
dea31012005-04-17 16:05:31 -05001752 }
1753 } else if (pring->ringno == psli->next_ring) {
1754
1755 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001756 return 0;
dea31012005-04-17 16:05:31 -05001757}
1758
1759/*
1760 * Free resources / clean up outstanding I/Os
1761 * associated with nlp_rpi in the LPFC_NODELIST entry.
1762 */
1763static int
James Smart2e0fef82007-06-17 19:56:36 -05001764lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001765{
James Smart2534ba72007-04-25 09:52:20 -04001766 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001767 struct lpfc_sli *psli;
1768 struct lpfc_sli_ring *pring;
1769 struct lpfc_iocbq *iocb, *next_iocb;
1770 IOCB_t *icmd;
1771 uint32_t rpi, i;
1772
James Smart92d7f7b2007-06-17 19:56:38 -05001773 lpfc_fabric_abort_nport(ndlp);
1774
dea31012005-04-17 16:05:31 -05001775 /*
1776 * Everything that matches on txcmplq will be returned
1777 * by firmware with a no rpi error.
1778 */
1779 psli = &phba->sli;
1780 rpi = ndlp->nlp_rpi;
1781 if (rpi) {
1782 /* Now process each ring */
1783 for (i = 0; i < psli->num_rings; i++) {
1784 pring = &psli->ring[i];
1785
James Smart2e0fef82007-06-17 19:56:36 -05001786 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001787 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05001788 list) {
dea31012005-04-17 16:05:31 -05001789 /*
1790 * Check to see if iocb matches the nport we are
1791 * looking for
1792 */
James Smart92d7f7b2007-06-17 19:56:38 -05001793 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
1794 ndlp))) {
dea31012005-04-17 16:05:31 -05001795 /* It matches, so deque and call compl
1796 with an error */
James Smart2534ba72007-04-25 09:52:20 -04001797 list_move_tail(&iocb->list,
1798 &completions);
dea31012005-04-17 16:05:31 -05001799 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05001800 }
1801 }
James Smart2e0fef82007-06-17 19:56:36 -05001802 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001803 }
1804 }
James Smart2534ba72007-04-25 09:52:20 -04001805
1806 while (!list_empty(&completions)) {
1807 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05001808 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04001809
James Smart2e0fef82007-06-17 19:56:36 -05001810 if (!iocb->iocb_cmpl)
1811 lpfc_sli_release_iocbq(phba, iocb);
1812 else {
James Smart2534ba72007-04-25 09:52:20 -04001813 icmd = &iocb->iocb;
1814 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1815 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
James Smart2e0fef82007-06-17 19:56:36 -05001816 (iocb->iocb_cmpl)(phba, iocb, iocb);
1817 }
James Smart2534ba72007-04-25 09:52:20 -04001818 }
1819
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001820 return 0;
dea31012005-04-17 16:05:31 -05001821}
1822
1823/*
1824 * Free rpi associated with LPFC_NODELIST entry.
1825 * This routine is called from lpfc_freenode(), when we are removing
1826 * a LPFC_NODELIST entry. It is also called if the driver initiates a
1827 * LOGO that completes successfully, and we are waiting to PLOGI back
1828 * to the remote NPort. In addition, it is called after we receive
1829 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1830 * we are waiting to PLOGI back to the remote NPort.
1831 */
1832int
James Smart2e0fef82007-06-17 19:56:36 -05001833lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001834{
James Smart2e0fef82007-06-17 19:56:36 -05001835 struct lpfc_hba *phba = vport->phba;
1836 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05001837 int rc;
1838
1839 if (ndlp->nlp_rpi) {
James Smart2e0fef82007-06-17 19:56:36 -05001840 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1841 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001842 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05001843 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001844 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04001845 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05001846 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05001847 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001848 }
dea31012005-04-17 16:05:31 -05001849 lpfc_no_rpi(phba, ndlp);
1850 ndlp->nlp_rpi = 0;
1851 return 1;
1852 }
1853 return 0;
1854}
1855
James Smart92d7f7b2007-06-17 19:56:38 -05001856void
1857lpfc_unreg_all_rpis(struct lpfc_vport *vport)
1858{
1859 struct lpfc_hba *phba = vport->phba;
1860 LPFC_MBOXQ_t *mbox;
1861 int rc;
1862
1863 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1864 if (mbox) {
1865 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
1866 mbox->vport = vport;
1867 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04001868 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001869 if (rc == MBX_NOT_FINISHED) {
1870 mempool_free(mbox, phba->mbox_mem_pool);
1871 }
1872 }
1873}
1874
1875void
1876lpfc_unreg_default_rpis(struct lpfc_vport *vport)
1877{
1878 struct lpfc_hba *phba = vport->phba;
1879 LPFC_MBOXQ_t *mbox;
1880 int rc;
1881
1882 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1883 if (mbox) {
1884 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
1885 mbox->vport = vport;
1886 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04001887 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001888 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001889 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1890 "1815 Could not issue "
1891 "unreg_did (default rpis)\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001892 mempool_free(mbox, phba->mbox_mem_pool);
1893 }
1894 }
1895}
1896
dea31012005-04-17 16:05:31 -05001897/*
1898 * Free resources associated with LPFC_NODELIST entry
1899 * so it can be freed.
1900 */
1901static int
James Smart2e0fef82007-06-17 19:56:36 -05001902lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001903{
James Smart2e0fef82007-06-17 19:56:36 -05001904 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1905 struct lpfc_hba *phba = vport->phba;
1906 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05001907 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05001908
1909 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04001910 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1911 "0900 Cleanup node for NPort x%x "
1912 "Data: x%x x%x x%x\n",
1913 ndlp->nlp_DID, ndlp->nlp_flag,
1914 ndlp->nlp_state, ndlp->nlp_rpi);
James Smart2e0fef82007-06-17 19:56:36 -05001915 lpfc_dequeue_node(vport, ndlp);
dea31012005-04-17 16:05:31 -05001916
dea31012005-04-17 16:05:31 -05001917 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1918 if ((mb = phba->sli.mbox_active)) {
1919 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1920 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1921 mb->context2 = NULL;
1922 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1923 }
1924 }
James Smart33ccf8d2006-08-17 11:57:58 -04001925
James Smart2e0fef82007-06-17 19:56:36 -05001926 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001927 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1928 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001929 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05001930 mp = (struct lpfc_dmabuf *) (mb->context1);
1931 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05001932 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001933 kfree(mp);
1934 }
1935 list_del(&mb->list);
1936 mempool_free(mb, phba->mbox_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04001937 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001938 }
1939 }
James Smart2e0fef82007-06-17 19:56:36 -05001940 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001941
James Smart07951072007-04-25 09:51:38 -04001942 lpfc_els_abort(phba,ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05001943 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04001944 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05001945 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001946
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05001947 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05001948 del_timer_sync(&ndlp->nlp_delayfunc);
1949
dea31012005-04-17 16:05:31 -05001950 if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1951 list_del_init(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05001952 if (!list_empty(&ndlp->dev_loss_evt.evt_listp))
1953 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05001954
James Smart2e0fef82007-06-17 19:56:36 -05001955 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05001956
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001957 return 0;
dea31012005-04-17 16:05:31 -05001958}
1959
1960/*
1961 * Check to see if we can free the nlp back to the freelist.
1962 * If we are in the middle of using the nlp in the discovery state
1963 * machine, defer the free till we reach the end of the state machine.
1964 */
James Smart329f9bc2007-04-25 09:53:01 -04001965static void
James Smart2e0fef82007-06-17 19:56:36 -05001966lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001967{
James Smart1dcb58e2007-04-25 09:51:30 -04001968 struct lpfc_rport_data *rdata;
dea31012005-04-17 16:05:31 -05001969
1970 if (ndlp->nlp_flag & NLP_DELAY_TMO) {
James Smart2e0fef82007-06-17 19:56:36 -05001971 lpfc_cancel_retry_delay_tmo(vport, ndlp);
dea31012005-04-17 16:05:31 -05001972 }
1973
James Smart2e0fef82007-06-17 19:56:36 -05001974 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04001975
James Smart2e0fef82007-06-17 19:56:36 -05001976 /*
James Smart92d7f7b2007-06-17 19:56:38 -05001977 * We can get here with a non-NULL ndlp->rport because when we
1978 * unregister a rport we don't break the rport/node linkage. So if we
1979 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05001980 */
James Smart92d7f7b2007-06-17 19:56:38 -05001981 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04001982 rdata = ndlp->rport->dd_data;
1983 rdata->pnode = NULL;
1984 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05001985 }
dea31012005-04-17 16:05:31 -05001986}
1987
1988static int
James Smart2e0fef82007-06-17 19:56:36 -05001989lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1990 uint32_t did)
dea31012005-04-17 16:05:31 -05001991{
James Smart2e0fef82007-06-17 19:56:36 -05001992 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05001993
1994 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001995 return 0;
dea31012005-04-17 16:05:31 -05001996
1997 if (ndlp->nlp_DID == 0) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001998 return 0;
dea31012005-04-17 16:05:31 -05001999 }
2000
2001 /* First check for Direct match */
2002 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002003 return 1;
dea31012005-04-17 16:05:31 -05002004
2005 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05002006 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05002007 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002008 return 0;
dea31012005-04-17 16:05:31 -05002009 }
2010
2011 matchdid.un.word = did;
2012 ndlpdid.un.word = ndlp->nlp_DID;
2013 if (matchdid.un.b.id == ndlpdid.un.b.id) {
2014 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
2015 (mydid.un.b.area == matchdid.un.b.area)) {
2016 if ((ndlpdid.un.b.domain == 0) &&
2017 (ndlpdid.un.b.area == 0)) {
2018 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002019 return 1;
dea31012005-04-17 16:05:31 -05002020 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002021 return 0;
dea31012005-04-17 16:05:31 -05002022 }
2023
2024 matchdid.un.word = ndlp->nlp_DID;
2025 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
2026 (mydid.un.b.area == ndlpdid.un.b.area)) {
2027 if ((matchdid.un.b.domain == 0) &&
2028 (matchdid.un.b.area == 0)) {
2029 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002030 return 1;
dea31012005-04-17 16:05:31 -05002031 }
2032 }
2033 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002034 return 0;
dea31012005-04-17 16:05:31 -05002035}
2036
James Smart685f0bf2007-04-25 09:53:08 -04002037/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05002038static struct lpfc_nodelist *
2039__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002040{
James Smart2fb9bd82006-12-02 13:33:57 -05002041 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002042 uint32_t data1;
2043
James Smart2e0fef82007-06-17 19:56:36 -05002044 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2045 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04002046 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2047 ((uint32_t) ndlp->nlp_xri << 16) |
2048 ((uint32_t) ndlp->nlp_type << 8) |
2049 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04002050 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2051 "0929 FIND node DID "
2052 "Data: x%p x%x x%x x%x\n",
2053 ndlp, ndlp->nlp_DID,
2054 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04002055 return ndlp;
dea31012005-04-17 16:05:31 -05002056 }
2057 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002058
dea31012005-04-17 16:05:31 -05002059 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04002060 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2061 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05002062 return NULL;
2063}
2064
2065struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002066lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002067{
James Smart2e0fef82007-06-17 19:56:36 -05002068 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002069 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002070
James Smart2e0fef82007-06-17 19:56:36 -05002071 spin_lock_irq(shost->host_lock);
2072 ndlp = __lpfc_findnode_did(vport, did);
2073 spin_unlock_irq(shost->host_lock);
2074 return ndlp;
2075}
2076
2077struct lpfc_nodelist *
2078lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2079{
2080 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2081 struct lpfc_nodelist *ndlp;
2082
2083 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002084 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05002085 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2086 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05002087 return NULL;
2088 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05002089 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002090 if (!ndlp)
2091 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002092 lpfc_nlp_init(vport, ndlp, did);
2093 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2094 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002095 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002096 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002097 return ndlp;
2098 }
James Smart2e0fef82007-06-17 19:56:36 -05002099 if (vport->fc_flag & FC_RSCN_MODE) {
2100 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04002101 /* If we've already recieved a PLOGI from this NPort
2102 * we don't need to try to discover it again.
2103 */
2104 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
2105 return NULL;
2106
James Smart2e0fef82007-06-17 19:56:36 -05002107 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002108 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002109 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002110
2111 /* Since this node is marked for discovery,
2112 * delay timeout is not needed.
2113 */
James Smartfdcebe22006-03-07 15:04:01 -05002114 if (ndlp->nlp_flag & NLP_DELAY_TMO)
James Smart2e0fef82007-06-17 19:56:36 -05002115 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart071fbd3d2006-04-15 11:53:20 -04002116 } else
dea31012005-04-17 16:05:31 -05002117 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002118 } else {
James Smart87af33f2007-10-27 13:37:43 -04002119 /* If we've already recieved a PLOGI from this NPort,
2120 * or we are already in the process of discovery on it,
2121 * we don't need to try to discover it again.
2122 */
James Smart685f0bf2007-04-25 09:53:08 -04002123 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04002124 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2125 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05002126 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002127 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2128 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002129 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002130 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002131 }
2132 return ndlp;
2133}
2134
2135/* Build a list of nodes to discover based on the loopmap */
2136void
James Smart2e0fef82007-06-17 19:56:36 -05002137lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002138{
James Smart2e0fef82007-06-17 19:56:36 -05002139 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002140 int j;
2141 uint32_t alpa, index;
2142
James Smart2e0fef82007-06-17 19:56:36 -05002143 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002144 return;
James Smart2e0fef82007-06-17 19:56:36 -05002145
2146 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05002147 return;
dea31012005-04-17 16:05:31 -05002148
2149 /* Check for loop map present or not */
2150 if (phba->alpa_map[0]) {
2151 for (j = 1; j <= phba->alpa_map[0]; j++) {
2152 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05002153 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05002154 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002155 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002156 }
2157 } else {
2158 /* No alpamap, so try all alpa's */
2159 for (j = 0; j < FC_MAXLOOP; j++) {
2160 /* If cfg_scan_down is set, start from highest
2161 * ALPA (0xef) to lowest (0x1).
2162 */
James Smart3de2a652007-08-02 11:09:59 -04002163 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05002164 index = j;
2165 else
2166 index = FC_MAXLOOP - j - 1;
2167 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05002168 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05002169 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002170 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002171 }
2172 }
2173 return;
2174}
2175
dea31012005-04-17 16:05:31 -05002176void
James Smart2e0fef82007-06-17 19:56:36 -05002177lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002178{
dea31012005-04-17 16:05:31 -05002179 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05002180 struct lpfc_sli *psli = &phba->sli;
2181 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2182 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2183 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2184 int rc;
2185
James Smart92d7f7b2007-06-17 19:56:38 -05002186 /*
2187 * if it's not a physical port or if we already send
2188 * clear_la then don't send it.
2189 */
2190 if ((phba->link_state >= LPFC_CLEAR_LA) ||
2191 (vport->port_type != LPFC_PHYSICAL_PORT))
2192 return;
2193
James Smart2e0fef82007-06-17 19:56:36 -05002194 /* Link up discovery */
2195 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2196 phba->link_state = LPFC_CLEAR_LA;
2197 lpfc_clear_la(phba, mbox);
2198 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2199 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002200 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05002201 if (rc == MBX_NOT_FINISHED) {
2202 mempool_free(mbox, phba->mbox_mem_pool);
2203 lpfc_disc_flush_list(vport);
2204 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2205 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2206 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05002207 phba->link_state = LPFC_HBA_ERROR;
2208 }
2209 }
2210}
2211
2212/* Reg_vpi to tell firmware to resume normal operations */
2213void
2214lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2215{
2216 LPFC_MBOXQ_t *regvpimbox;
2217
2218 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2219 if (regvpimbox) {
2220 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
2221 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2222 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002223 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05002224 == MBX_NOT_FINISHED) {
2225 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002226 }
2227 }
2228}
2229
2230/* Start Link up / RSCN discovery on NPR nodes */
2231void
2232lpfc_disc_start(struct lpfc_vport *vport)
2233{
2234 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2235 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04002236 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05002237 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04002238 int did_changed;
dea31012005-04-17 16:05:31 -05002239
James Smart2e0fef82007-06-17 19:56:36 -05002240 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002241 return;
James Smart2e0fef82007-06-17 19:56:36 -05002242
2243 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05002244 clear_la_pending = 1;
2245 else
2246 clear_la_pending = 0;
2247
James Smart2e0fef82007-06-17 19:56:36 -05002248 if (vport->port_state < LPFC_VPORT_READY)
2249 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05002250
James Smart2e0fef82007-06-17 19:56:36 -05002251 lpfc_set_disctmo(vport);
2252
2253 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05002254 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002255 else
dea31012005-04-17 16:05:31 -05002256 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002257
2258 vport->fc_prevDID = vport->fc_myDID;
2259 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05002260
2261 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002262 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2263 "0202 Start Discovery hba state x%x "
2264 "Data: x%x x%x x%x\n",
2265 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
2266 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002267
2268 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002269 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05002270
2271 if (num_sent)
2272 return;
2273
James Smart92d7f7b2007-06-17 19:56:38 -05002274 /*
2275 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2276 * continue discovery.
2277 */
2278 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2279 !(vport->fc_flag & FC_RSCN_MODE)) {
2280 lpfc_issue_reg_vpi(phba, vport);
2281 return;
2282 }
James Smart2e0fef82007-06-17 19:56:36 -05002283
James Smart92d7f7b2007-06-17 19:56:38 -05002284 /*
2285 * For SLI2, we need to set port_state to READY and continue
2286 * discovery.
2287 */
2288 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2289 /* If we get here, there is nothing to ADISC */
2290 if (vport->port_type == LPFC_PHYSICAL_PORT)
2291 lpfc_issue_clear_la(phba, vport);
2292
2293 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05002294 vport->num_disc_nodes = 0;
2295 /* go thru NPR nodes and issue ELS PLOGIs */
2296 if (vport->fc_npr_cnt)
2297 lpfc_els_disc_plogi(vport);
2298
2299 if (!vport->num_disc_nodes) {
2300 spin_lock_irq(shost->host_lock);
2301 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2302 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002303 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05002304 }
2305 }
James Smart92d7f7b2007-06-17 19:56:38 -05002306 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002307 } else {
2308 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002309 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05002310
2311 if (num_sent)
2312 return;
2313
James Smart2e0fef82007-06-17 19:56:36 -05002314 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05002315 /* Check to see if more RSCNs came in while we
2316 * were processing this one.
2317 */
James Smart2e0fef82007-06-17 19:56:36 -05002318 if ((vport->fc_rscn_id_cnt == 0) &&
2319 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2320 spin_lock_irq(shost->host_lock);
2321 vport->fc_flag &= ~FC_RSCN_MODE;
2322 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002323 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002324 } else
James Smart2e0fef82007-06-17 19:56:36 -05002325 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05002326 }
2327 }
2328 return;
2329}
2330
2331/*
2332 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2333 * ring the match the sppecified nodelist.
2334 */
2335static void
James Smart2e0fef82007-06-17 19:56:36 -05002336lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002337{
James Smart2534ba72007-04-25 09:52:20 -04002338 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002339 struct lpfc_sli *psli;
2340 IOCB_t *icmd;
2341 struct lpfc_iocbq *iocb, *next_iocb;
2342 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002343
2344 psli = &phba->sli;
2345 pring = &psli->ring[LPFC_ELS_RING];
2346
2347 /* Error matching iocb on txq or txcmplq
2348 * First check the txq.
2349 */
James Smart2e0fef82007-06-17 19:56:36 -05002350 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002351 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2352 if (iocb->context1 != ndlp) {
2353 continue;
2354 }
2355 icmd = &iocb->iocb;
2356 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2357 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2358
James Smart2534ba72007-04-25 09:52:20 -04002359 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05002360 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002361 }
2362 }
2363
2364 /* Next check the txcmplq */
2365 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2366 if (iocb->context1 != ndlp) {
2367 continue;
2368 }
2369 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05002370 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2371 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04002372 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05002373 }
2374 }
James Smart2e0fef82007-06-17 19:56:36 -05002375 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002376
2377 while (!list_empty(&completions)) {
2378 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05002379 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04002380
James Smart2e0fef82007-06-17 19:56:36 -05002381 if (!iocb->iocb_cmpl)
2382 lpfc_sli_release_iocbq(phba, iocb);
2383 else {
James Smart2534ba72007-04-25 09:52:20 -04002384 icmd = &iocb->iocb;
2385 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2386 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2387 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002388 }
James Smart2534ba72007-04-25 09:52:20 -04002389 }
dea31012005-04-17 16:05:31 -05002390}
2391
2392void
James Smart2e0fef82007-06-17 19:56:36 -05002393lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002394{
2395 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05002396 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002397
James Smart2e0fef82007-06-17 19:56:36 -05002398 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2399 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002400 nlp_listp) {
2401 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2402 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2403 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04002404 }
dea31012005-04-17 16:05:31 -05002405 }
2406 }
dea31012005-04-17 16:05:31 -05002407}
2408
James Smart92d7f7b2007-06-17 19:56:38 -05002409void
2410lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2411{
2412 lpfc_els_flush_rscn(vport);
2413 lpfc_els_flush_cmd(vport);
2414 lpfc_disc_flush_list(vport);
2415}
2416
dea31012005-04-17 16:05:31 -05002417/*****************************************************************************/
2418/*
2419 * NAME: lpfc_disc_timeout
2420 *
2421 * FUNCTION: Fibre Channel driver discovery timeout routine.
2422 *
2423 * EXECUTION ENVIRONMENT: interrupt only
2424 *
2425 * CALLED FROM:
2426 * Timer function
2427 *
2428 * RETURNS:
2429 * none
2430 */
2431/*****************************************************************************/
2432void
2433lpfc_disc_timeout(unsigned long ptr)
2434{
James Smart2e0fef82007-06-17 19:56:36 -05002435 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2436 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002437 unsigned long flags = 0;
2438
2439 if (unlikely(!phba))
2440 return;
2441
James Smart2e0fef82007-06-17 19:56:36 -05002442 if ((vport->work_port_events & WORKER_DISC_TMO) == 0) {
2443 spin_lock_irqsave(&vport->work_port_lock, flags);
2444 vport->work_port_events |= WORKER_DISC_TMO;
2445 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2446
James Smart92d7f7b2007-06-17 19:56:38 -05002447 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002448 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05002449 lpfc_worker_wake_up(phba);
2450 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002451 }
dea31012005-04-17 16:05:31 -05002452 return;
2453}
2454
2455static void
James Smart2e0fef82007-06-17 19:56:36 -05002456lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002457{
James Smart2e0fef82007-06-17 19:56:36 -05002458 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2459 struct lpfc_hba *phba = vport->phba;
2460 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002461 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05002462 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05002463 int rc, clrlaerr = 0;
2464
James Smart2e0fef82007-06-17 19:56:36 -05002465 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05002466 return;
2467
James Smart2e0fef82007-06-17 19:56:36 -05002468 spin_lock_irq(shost->host_lock);
2469 vport->fc_flag &= ~FC_DISC_TMO;
2470 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002471
James Smart858c9f62007-06-17 19:56:39 -05002472 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2473 "disc timeout: state:x%x rtry:x%x flg:x%x",
2474 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2475
James Smart2e0fef82007-06-17 19:56:36 -05002476 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05002477
2478 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05002479 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2480 * FAN
2481 */
2482 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04002483 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
2484 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002485 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05002486 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002487 nlp_listp) {
2488 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2489 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002490 if (ndlp->nlp_type & NLP_FABRIC) {
2491 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05002492 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04002493
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002494 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002495 /* Fail outstanding IO now since device
2496 * is marked for PLOGI.
2497 */
James Smart2e0fef82007-06-17 19:56:36 -05002498 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002499 }
2500 }
James Smart92d7f7b2007-06-17 19:56:38 -05002501 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05002502 lpfc_initial_flogi(vport);
2503 }
dea31012005-04-17 16:05:31 -05002504 break;
2505
James Smart92d7f7b2007-06-17 19:56:38 -05002506 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05002507 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05002508 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05002509 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04002510 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2511 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04002512 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05002513
2514 /* Assume no Fabric and go on with discovery.
2515 * Check for outstanding ELS FLOGI to abort.
2516 */
2517
2518 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05002519 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002520
2521 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002522 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002523 break;
2524
2525 case LPFC_FABRIC_CFG_LINK:
2526 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2527 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04002528 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2529 "0223 Timeout while waiting for "
2530 "NameServer login\n");
dea31012005-04-17 16:05:31 -05002531 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05002532 ndlp = lpfc_findnode_did(vport, NameServer_DID);
dea31012005-04-17 16:05:31 -05002533 if (ndlp)
James Smart87af33f2007-10-27 13:37:43 -04002534 lpfc_els_abort(phba, ndlp);
2535
2536 /* ReStart discovery */
2537 goto restart_disc;
dea31012005-04-17 16:05:31 -05002538
2539 case LPFC_NS_QRY:
2540 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04002541 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2542 "0224 NameServer Query timeout "
2543 "Data: x%x x%x\n",
2544 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002545
James Smart92d7f7b2007-06-17 19:56:38 -05002546 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2547 /* Try it one more time */
2548 vport->fc_ns_retry++;
2549 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2550 vport->fc_ns_retry, 0);
2551 if (rc == 0)
2552 break;
dea31012005-04-17 16:05:31 -05002553 }
James Smart92d7f7b2007-06-17 19:56:38 -05002554 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002555
James Smart87af33f2007-10-27 13:37:43 -04002556restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05002557 /*
2558 * Discovery is over.
2559 * set port_state to PORT_READY if SLI2.
2560 * cmpl_reg_vpi will set port_state to READY for SLI3.
2561 */
2562 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2563 lpfc_issue_reg_vpi(phba, vport);
2564 else { /* NPIV Not enabled */
2565 lpfc_issue_clear_la(phba, vport);
2566 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002567 }
2568
2569 /* Setup and issue mailbox INITIALIZE LINK command */
2570 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2571 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04002572 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2573 "0206 Device Discovery "
2574 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05002575 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002576 break;
2577 }
2578
2579 lpfc_linkdown(phba);
2580 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2581 phba->cfg_link_speed);
2582 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05002583 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002584 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002585 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04002586 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05002587 if (rc == MBX_NOT_FINISHED)
2588 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2589
2590 break;
2591
2592 case LPFC_DISC_AUTH:
2593 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04002594 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2595 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002596 lpfc_disc_flush_list(vport);
2597
James Smart92d7f7b2007-06-17 19:56:38 -05002598 /*
2599 * set port_state to PORT_READY if SLI2.
2600 * cmpl_reg_vpi will set port_state to READY for SLI3.
2601 */
2602 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2603 lpfc_issue_reg_vpi(phba, vport);
2604 else { /* NPIV Not enabled */
2605 lpfc_issue_clear_la(phba, vport);
2606 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002607 }
2608 break;
2609
James Smart2e0fef82007-06-17 19:56:36 -05002610 case LPFC_VPORT_READY:
2611 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04002612 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2613 "0231 RSCN timeout Data: x%x "
2614 "x%x\n",
2615 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002616
2617 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05002618 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05002619
James Smart2e0fef82007-06-17 19:56:36 -05002620 lpfc_els_flush_rscn(vport);
2621 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05002622 }
2623 break;
James Smart2e0fef82007-06-17 19:56:36 -05002624
James Smart92d7f7b2007-06-17 19:56:38 -05002625 default:
James Smarte8b62012007-08-02 11:10:09 -04002626 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2627 "0229 Unexpected discovery timeout, "
2628 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05002629 break;
2630 }
2631
2632 switch (phba->link_state) {
2633 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05002634 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04002635 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2636 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002637 clrlaerr = 1;
2638 break;
2639
2640 case LPFC_LINK_UNKNOWN:
2641 case LPFC_WARM_START:
2642 case LPFC_INIT_START:
2643 case LPFC_INIT_MBX_CMDS:
2644 case LPFC_LINK_DOWN:
2645 case LPFC_LINK_UP:
2646 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04002647 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2648 "0230 Unexpected timeout, hba link "
2649 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05002650 clrlaerr = 1;
2651 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002652
2653 case LPFC_HBA_READY:
2654 break;
dea31012005-04-17 16:05:31 -05002655 }
2656
2657 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05002658 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05002659 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002660 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2661 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05002662 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002663 }
2664
2665 return;
2666}
2667
dea31012005-04-17 16:05:31 -05002668/*
2669 * This routine handles processing a NameServer REG_LOGIN mailbox
2670 * command upon completion. It is setup in the LPFC_MBOXQ
2671 * as the completion routine when the command is
2672 * handed off to the SLI layer.
2673 */
2674void
James Smart2e0fef82007-06-17 19:56:36 -05002675lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002676{
James Smart2e0fef82007-06-17 19:56:36 -05002677 MAILBOX_t *mb = &pmb->mb;
2678 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2679 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2680 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002681
2682 pmb->context1 = NULL;
2683
dea31012005-04-17 16:05:31 -05002684 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05002685 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002686 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002687
James Smart2e0fef82007-06-17 19:56:36 -05002688 /*
2689 * Start issuing Fabric-Device Management Interface (FDMI) command to
2690 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
2691 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05002692 */
James Smart2e0fef82007-06-17 19:56:36 -05002693
James Smart3de2a652007-08-02 11:09:59 -04002694 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05002695 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
2696 else
2697 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05002698
James Smart329f9bc2007-04-25 09:53:01 -04002699 /* Mailbox took a reference to the node */
2700 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002701 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2702 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002703 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002704
2705 return;
2706}
2707
James Smart685f0bf2007-04-25 09:53:08 -04002708static int
2709lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2710{
2711 uint16_t *rpi = param;
2712
2713 return ndlp->nlp_rpi == *rpi;
2714}
2715
2716static int
2717lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2718{
2719 return memcmp(&ndlp->nlp_portname, param,
2720 sizeof(ndlp->nlp_portname)) == 0;
2721}
2722
dea31012005-04-17 16:05:31 -05002723struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002724__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05002725{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002726 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002727
James Smart2e0fef82007-06-17 19:56:36 -05002728 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04002729 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04002730 return ndlp;
2731 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002732 return NULL;
dea31012005-04-17 16:05:31 -05002733}
2734
James Smart685f0bf2007-04-25 09:53:08 -04002735/*
2736 * Search node lists for a remote port matching filter criteria
James Smart92d7f7b2007-06-17 19:56:38 -05002737 * Caller needs to hold host_lock before calling this routine.
James Smart685f0bf2007-04-25 09:53:08 -04002738 */
2739struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002740lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
James Smart685f0bf2007-04-25 09:53:08 -04002741{
James Smart2e0fef82007-06-17 19:56:36 -05002742 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart685f0bf2007-04-25 09:53:08 -04002743 struct lpfc_nodelist *ndlp;
2744
James Smart2e0fef82007-06-17 19:56:36 -05002745 spin_lock_irq(shost->host_lock);
2746 ndlp = __lpfc_find_node(vport, filter, param);
2747 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002748 return ndlp;
2749}
2750
2751/*
2752 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05002753 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04002754 */
2755struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002756__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04002757{
James Smart2e0fef82007-06-17 19:56:36 -05002758 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04002759}
2760
James Smart2534ba72007-04-25 09:52:20 -04002761struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002762lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart2534ba72007-04-25 09:52:20 -04002763{
James Smart2e0fef82007-06-17 19:56:36 -05002764 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2534ba72007-04-25 09:52:20 -04002765 struct lpfc_nodelist *ndlp;
2766
James Smart2e0fef82007-06-17 19:56:36 -05002767 spin_lock_irq(shost->host_lock);
2768 ndlp = __lpfc_findnode_rpi(vport, rpi);
2769 spin_unlock_irq(shost->host_lock);
James Smart2534ba72007-04-25 09:52:20 -04002770 return ndlp;
2771}
2772
James Smart488d1462006-03-07 15:02:37 -05002773/*
James Smart685f0bf2007-04-25 09:53:08 -04002774 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05002775 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05002776 */
2777struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002778lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05002779{
James Smart2e0fef82007-06-17 19:56:36 -05002780 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05002781 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05002782
James Smart2e0fef82007-06-17 19:56:36 -05002783 spin_lock_irq(shost->host_lock);
2784 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
2785 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002786 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05002787}
2788
dea31012005-04-17 16:05:31 -05002789void
James Smart2e0fef82007-06-17 19:56:36 -05002790lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2791 uint32_t did)
dea31012005-04-17 16:05:31 -05002792{
2793 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -05002794 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05002795 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002796 init_timer(&ndlp->nlp_delayfunc);
2797 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2798 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2799 ndlp->nlp_DID = did;
James Smart2e0fef82007-06-17 19:56:36 -05002800 ndlp->vport = vport;
dea31012005-04-17 16:05:31 -05002801 ndlp->nlp_sid = NLP_NO_SID;
James Smart685f0bf2007-04-25 09:53:08 -04002802 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart329f9bc2007-04-25 09:53:01 -04002803 kref_init(&ndlp->kref);
James Smart858c9f62007-06-17 19:56:39 -05002804
2805 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2806 "node init: did:x%x",
2807 ndlp->nlp_DID, 0, 0);
2808
dea31012005-04-17 16:05:31 -05002809 return;
2810}
James Smart329f9bc2007-04-25 09:53:01 -04002811
James Smart98c9ea52007-10-27 13:37:33 -04002812/* This routine releases all resources associated with a specifc NPort's ndlp
2813 * and mempool_free's the nodelist.
2814 */
James Smart311464e2007-08-02 11:10:37 -04002815static void
James Smart329f9bc2007-04-25 09:53:01 -04002816lpfc_nlp_release(struct kref *kref)
2817{
2818 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
2819 kref);
James Smart858c9f62007-06-17 19:56:39 -05002820
2821 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2822 "node release: did:x%x flg:x%x type:x%x",
2823 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2824
James Smart2e0fef82007-06-17 19:56:36 -05002825 lpfc_nlp_remove(ndlp->vport, ndlp);
2826 mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04002827}
2828
James Smart98c9ea52007-10-27 13:37:33 -04002829/* This routine bumps the reference count for a ndlp structure to ensure
2830 * that one discovery thread won't free a ndlp while another discovery thread
2831 * is using it.
2832 */
James Smart329f9bc2007-04-25 09:53:01 -04002833struct lpfc_nodelist *
2834lpfc_nlp_get(struct lpfc_nodelist *ndlp)
2835{
James Smart98c9ea52007-10-27 13:37:33 -04002836 if (ndlp) {
2837 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2838 "node get: did:x%x flg:x%x refcnt:x%x",
2839 ndlp->nlp_DID, ndlp->nlp_flag,
2840 atomic_read(&ndlp->kref.refcount));
James Smart329f9bc2007-04-25 09:53:01 -04002841 kref_get(&ndlp->kref);
James Smart98c9ea52007-10-27 13:37:33 -04002842 }
James Smart329f9bc2007-04-25 09:53:01 -04002843 return ndlp;
2844}
2845
James Smart98c9ea52007-10-27 13:37:33 -04002846
2847/* This routine decrements the reference count for a ndlp structure. If the
2848 * count goes to 0, this indicates the the associated nodelist should be freed.
2849 */
James Smart329f9bc2007-04-25 09:53:01 -04002850int
2851lpfc_nlp_put(struct lpfc_nodelist *ndlp)
2852{
James Smart98c9ea52007-10-27 13:37:33 -04002853 if (ndlp) {
2854 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2855 "node put: did:x%x flg:x%x refcnt:x%x",
2856 ndlp->nlp_DID, ndlp->nlp_flag,
2857 atomic_read(&ndlp->kref.refcount));
2858 }
James Smart329f9bc2007-04-25 09:53:01 -04002859 return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
2860}
James Smart98c9ea52007-10-27 13:37:33 -04002861
2862/* This routine free's the specified nodelist if it is not in use
2863 * by any other discovery thread. This routine returns 1 if the ndlp
2864 * is not being used by anyone and has been freed. A return value of
2865 * 0 indicates it is being used by another discovery thread and the
2866 * refcount is left unchanged.
2867 */
2868int
2869lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
2870{
2871 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2872 "node not used: did:x%x flg:x%x refcnt:x%x",
2873 ndlp->nlp_DID, ndlp->nlp_flag,
2874 atomic_read(&ndlp->kref.refcount));
2875
2876 if (atomic_read(&ndlp->kref.refcount) == 1) {
2877 lpfc_nlp_put(ndlp);
2878 return 1;
2879 }
2880 return 0;
2881}
2882