blob: e181a98caf1618eecd355da436ec58937ce338c3 [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;
160 int warn_on = 0;
161
162 rport = ndlp->rport;
163
164 if (!rport)
165 return;
166
167 rdata = rport->dd_data;
168 name = (uint8_t *) &ndlp->nlp_portname;
169 vport = ndlp->vport;
170 phba = vport->phba;
171
172 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
173 "rport devlosstmo:did:x%x type:x%x id:x%x",
174 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
175
176 if (!(vport->load_flag & FC_UNLOADING) &&
177 ndlp->nlp_state == NLP_STE_MAPPED_NODE)
178 return;
179
James Smart92d7f7b2007-06-17 19:56:38 -0500180 if (ndlp->nlp_type & NLP_FABRIC) {
James Smart858c9f62007-06-17 19:56:39 -0500181 int put_node;
182 int put_rport;
183
James Smart92d7f7b2007-06-17 19:56:38 -0500184 /* We will clean up these Nodes in linkup */
185 put_node = rdata->pnode != NULL;
186 put_rport = ndlp->rport != NULL;
187 rdata->pnode = NULL;
188 ndlp->rport = NULL;
189 if (put_node)
190 lpfc_nlp_put(ndlp);
191 if (put_rport)
192 put_device(&rport->dev);
James Smart82085712007-04-25 09:52:41 -0400193 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500194 }
James Smart82085712007-04-25 09:52:41 -0400195
dea31012005-04-17 16:05:31 -0500196 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400197 warn_on = 1;
dea31012005-04-17 16:05:31 -0500198 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400199 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
200 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500201 }
James Smart2e0fef82007-06-17 19:56:36 -0500202 if (vport->load_flag & FC_UNLOADING)
James Smartc01f3202006-08-18 17:47:08 -0400203 warn_on = 0;
204
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400205 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400206 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
207 "0203 Devloss timeout on "
208 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
209 "NPort x%x Data: x%x x%x x%x\n",
210 *name, *(name+1), *(name+2), *(name+3),
211 *(name+4), *(name+5), *(name+6), *(name+7),
212 ndlp->nlp_DID, ndlp->nlp_flag,
213 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400214 } else {
James Smarte8b62012007-08-02 11:10:09 -0400215 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
216 "0204 Devloss timeout on "
217 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
218 "NPort x%x Data: x%x x%x x%x\n",
219 *name, *(name+1), *(name+2), *(name+3),
220 *(name+4), *(name+5), *(name+6), *(name+7),
221 ndlp->nlp_DID, ndlp->nlp_flag,
222 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400223 }
224
James Smart2e0fef82007-06-17 19:56:36 -0500225 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400226 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400227 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
228 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
James Smart2e0fef82007-06-17 19:56:36 -0500229 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart1dcb58e2007-04-25 09:51:30 -0400230 else {
James Smart858c9f62007-06-17 19:56:39 -0500231 int put_node;
232 int put_rport;
233
James Smart92d7f7b2007-06-17 19:56:38 -0500234 put_node = rdata->pnode != NULL;
235 put_rport = ndlp->rport != NULL;
James Smart1dcb58e2007-04-25 09:51:30 -0400236 rdata->pnode = NULL;
237 ndlp->rport = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -0500238 if (put_node)
239 lpfc_nlp_put(ndlp);
240 if (put_rport)
241 put_device(&rport->dev);
James Smart1dcb58e2007-04-25 09:51:30 -0400242 }
James Smart92d7f7b2007-06-17 19:56:38 -0500243}
James Smartc01f3202006-08-18 17:47:08 -0400244
James Smart92d7f7b2007-06-17 19:56:38 -0500245
246void
247lpfc_worker_wake_up(struct lpfc_hba *phba)
248{
249 wake_up(phba->work_wait);
dea31012005-04-17 16:05:31 -0500250 return;
251}
252
253static void
James Smart2e0fef82007-06-17 19:56:36 -0500254lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500255{
256 struct lpfc_work_evt *evtp = NULL;
257 struct lpfc_nodelist *ndlp;
258 int free_evt;
259
James Smart2e0fef82007-06-17 19:56:36 -0500260 spin_lock_irq(&phba->hbalock);
261 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500262 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
263 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500264 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500265 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500266 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500267 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500268 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500269 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500270 free_evt = 0; /* evt is part of ndlp */
dea31012005-04-17 16:05:31 -0500271 break;
James Smart858c9f62007-06-17 19:56:39 -0500272 case LPFC_EVT_DEV_LOSS:
273 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
274 lpfc_nlp_get(ndlp);
275 lpfc_dev_loss_tmo_handler(ndlp);
276 free_evt = 0;
James Smart858c9f62007-06-17 19:56:39 -0500277 lpfc_nlp_put(ndlp);
278 break;
dea31012005-04-17 16:05:31 -0500279 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500280 if (phba->link_state < LPFC_LINK_DOWN)
281 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500282 else
James Smart2e0fef82007-06-17 19:56:36 -0500283 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500284 complete((struct completion *)(evtp->evt_arg2));
285 break;
James Smart46fa3112007-04-25 09:51:45 -0400286 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500287 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400288 lpfc_offline_prep(phba);
289 *(int *)(evtp->evt_arg1) = 0;
290 complete((struct completion *)(evtp->evt_arg2));
291 break;
292 case LPFC_EVT_OFFLINE:
293 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500294 lpfc_sli_brdrestart(phba);
295 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400296 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
297 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500298 complete((struct completion *)(evtp->evt_arg2));
299 break;
300 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400301 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500302 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500303 lpfc_sli_brdreset(phba);
304 lpfc_hba_down_post(phba);
305 *(int *)(evtp->evt_arg1) =
306 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400307 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500308 complete((struct completion *)(evtp->evt_arg2));
309 break;
310 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400311 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500312 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500313 = (phba->pport->stopped)
314 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400315 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500316 complete((struct completion *)(evtp->evt_arg2));
317 break;
318 }
319 if (free_evt)
320 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500321 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500322 }
James Smart2e0fef82007-06-17 19:56:36 -0500323 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500324
325}
326
James Smart311464e2007-08-02 11:10:37 -0400327static void
James Smart2e0fef82007-06-17 19:56:36 -0500328lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500329{
330 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500331 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400332 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400333 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400334 int i;
dea31012005-04-17 16:05:31 -0500335
James Smart2e0fef82007-06-17 19:56:36 -0500336 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500337 ha_copy = phba->work_ha;
338 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500339 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500340
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500341 if (ha_copy & HA_ERATT)
dea31012005-04-17 16:05:31 -0500342 lpfc_handle_eratt(phba);
343
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500344 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500345 lpfc_sli_handle_mb_event(phba);
346
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500347 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500348 lpfc_handle_latt(phba);
James Smart549e55c2007-08-02 11:09:51 -0400349 vports = lpfc_create_vport_work_array(phba);
350 if (vports != NULL)
James Smart51ef4c22007-08-02 11:10:31 -0400351 for(i = 0; i < LPFC_MAX_VPORTS; i++) {
352 /*
353 * We could have no vports in array if unloading, so if
354 * this happens then just use the pport
355 */
356 if (vports[i] == NULL && i == 0)
357 vport = phba->pport;
358 else
359 vport = vports[i];
360 if (vport == NULL)
361 break;
362 work_port_events = vport->work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400363 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400364 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400365 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400366 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400367 if (work_port_events & WORKER_HB_TMO)
368 lpfc_hb_timeout_handler(phba);
369 if (work_port_events & WORKER_MBOX_TMO)
370 lpfc_mbox_timeout_handler(phba);
371 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
372 lpfc_unblock_fabric_iocbs(phba);
373 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400374 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400375 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
376 lpfc_ramp_down_queue_handler(phba);
377 if (work_port_events & WORKER_RAMP_UP_QUEUE)
378 lpfc_ramp_up_queue_handler(phba);
James Smart51ef4c22007-08-02 11:10:31 -0400379 spin_lock_irq(&vport->work_port_lock);
380 vport->work_port_events &= ~work_port_events;
381 spin_unlock_irq(&vport->work_port_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500382 }
James Smart549e55c2007-08-02 11:09:51 -0400383 lpfc_destroy_vport_work_array(vports);
dea31012005-04-17 16:05:31 -0500384
James Smart858c9f62007-06-17 19:56:39 -0500385 pring = &phba->sli.ring[LPFC_ELS_RING];
386 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
387 status >>= (4*LPFC_ELS_RING);
388 if ((status & HA_RXMASK)
389 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400390 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500391 pring->flag |= LPFC_DEFERRED_RING_EVENT;
392 } else {
393 lpfc_sli_handle_slow_ring_event(phba, pring,
394 (status &
395 HA_RXMASK));
396 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
397 }
398 /*
399 * Turn on Ring interrupts
400 */
401 spin_lock_irq(&phba->hbalock);
402 control = readl(phba->HCregaddr);
403 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
James Smarta58cbd52007-08-02 11:09:43 -0400404 lpfc_debugfs_slow_ring_trc(phba,
405 "WRK Enable ring: cntl:x%x hacopy:x%x",
406 control, ha_copy, 0);
407
James Smart858c9f62007-06-17 19:56:39 -0500408 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -0500409 writel(control, phba->HCregaddr);
410 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500411 }
James Smarta58cbd52007-08-02 11:09:43 -0400412 else {
413 lpfc_debugfs_slow_ring_trc(phba,
414 "WRK Ring ok: cntl:x%x hacopy:x%x",
415 control, ha_copy, 0);
416 }
James Smart858c9f62007-06-17 19:56:39 -0500417 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500418 }
James Smart2e0fef82007-06-17 19:56:36 -0500419 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500420}
421
422static int
James Smart2e0fef82007-06-17 19:56:36 -0500423check_work_wait_done(struct lpfc_hba *phba)
424{
James Smart92d7f7b2007-06-17 19:56:38 -0500425 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400426 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart858c9f62007-06-17 19:56:39 -0500427 int rc = 0;
James Smarted957682007-06-17 19:56:37 -0500428
James Smart2e0fef82007-06-17 19:56:36 -0500429 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500430 list_for_each_entry(vport, &phba->port_list, listentry) {
431 if (vport->work_port_events) {
432 rc = 1;
James Smart549e55c2007-08-02 11:09:51 -0400433 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500434 }
435 }
James Smart549e55c2007-08-02 11:09:51 -0400436 if (rc || phba->work_ha || (!list_empty(&phba->work_list)) ||
437 kthread_should_stop() || pring->flag & LPFC_DEFERRED_RING_EVENT) {
James Smart2e0fef82007-06-17 19:56:36 -0500438 rc = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500439 phba->work_found++;
James Smart549e55c2007-08-02 11:09:51 -0400440 } else
James Smart92d7f7b2007-06-17 19:56:38 -0500441 phba->work_found = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500442 spin_unlock_irq(&phba->hbalock);
443 return rc;
dea31012005-04-17 16:05:31 -0500444}
445
James Smart92d7f7b2007-06-17 19:56:38 -0500446
dea31012005-04-17 16:05:31 -0500447int
448lpfc_do_work(void *p)
449{
450 struct lpfc_hba *phba = p;
451 int rc;
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800452 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(work_waitq);
dea31012005-04-17 16:05:31 -0500453
454 set_user_nice(current, -20);
455 phba->work_wait = &work_waitq;
James Smart92d7f7b2007-06-17 19:56:38 -0500456 phba->work_found = 0;
dea31012005-04-17 16:05:31 -0500457
458 while (1) {
459
460 rc = wait_event_interruptible(work_waitq,
James Smart92d7f7b2007-06-17 19:56:38 -0500461 check_work_wait_done(phba));
462
dea31012005-04-17 16:05:31 -0500463 BUG_ON(rc);
464
465 if (kthread_should_stop())
466 break;
467
468 lpfc_work_done(phba);
469
James Smart92d7f7b2007-06-17 19:56:38 -0500470 /* If there is alot of slow ring work, like during link up
471 * check_work_wait_done() may cause this thread to not give
472 * up the CPU for very long periods of time. This may cause
473 * soft lockups or other problems. To avoid these situations
474 * give up the CPU here after LPFC_MAX_WORKER_ITERATION
475 * consecutive iterations.
476 */
477 if (phba->work_found >= LPFC_MAX_WORKER_ITERATION) {
478 phba->work_found = 0;
479 schedule();
480 }
dea31012005-04-17 16:05:31 -0500481 }
482 phba->work_wait = NULL;
483 return 0;
484}
485
486/*
487 * This is only called to handle FC worker events. Since this a rare
488 * occurance, we allocate a struct lpfc_work_evt structure here instead of
489 * embedding it in the IOCB.
490 */
491int
James Smart2e0fef82007-06-17 19:56:36 -0500492lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500493 uint32_t evt)
494{
495 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500496 unsigned long flags;
dea31012005-04-17 16:05:31 -0500497
498 /*
499 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
500 * be queued to worker thread for processing
501 */
James Smart92d7f7b2007-06-17 19:56:38 -0500502 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500503 if (!evtp)
504 return 0;
505
506 evtp->evt_arg1 = arg1;
507 evtp->evt_arg2 = arg2;
508 evtp->evt = evt;
509
James Smarted957682007-06-17 19:56:37 -0500510 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400511 list_add_tail(&evtp->evt_listp, &phba->work_list);
dea31012005-04-17 16:05:31 -0500512 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -0500513 lpfc_worker_wake_up(phba);
James Smarted957682007-06-17 19:56:37 -0500514 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500515
516 return 1;
517}
518
James Smart92d7f7b2007-06-17 19:56:38 -0500519void
520lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
521{
522 struct lpfc_hba *phba = vport->phba;
523 struct lpfc_nodelist *ndlp, *next_ndlp;
524 int rc;
525
526 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
527 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
528 continue;
529
James Smart98c9ea52007-10-27 13:37:33 -0400530 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
531 ((vport->port_type == LPFC_NPIV_PORT) &&
532 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500533 lpfc_unreg_rpi(vport, ndlp);
534
535 /* Leave Fabric nodes alone on link down */
536 if (!remove && ndlp->nlp_type & NLP_FABRIC)
537 continue;
538 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
539 remove
540 ? NLP_EVT_DEVICE_RM
541 : NLP_EVT_DEVICE_RECOVERY);
542 }
543 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
544 lpfc_mbx_unreg_vpi(vport);
545 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
546 }
547}
548
549static void
James Smart98c9ea52007-10-27 13:37:33 -0400550lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500551{
552 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart858c9f62007-06-17 19:56:39 -0500553
James Smart92d7f7b2007-06-17 19:56:38 -0500554 /* Cleanup any outstanding RSCN activity */
555 lpfc_els_flush_rscn(vport);
556
557 /* Cleanup any outstanding ELS commands */
558 lpfc_els_flush_cmd(vport);
559
560 lpfc_cleanup_rpis(vport, 0);
561
562 /* free any ndlp's on unused list */
563 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
James Smart92d7f7b2007-06-17 19:56:38 -0500564 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
565 lpfc_drop_node(vport, ndlp);
566
567 /* Turn off discovery timer if its running */
568 lpfc_can_disctmo(vport);
569}
570
James Smart98c9ea52007-10-27 13:37:33 -0400571static void
572lpfc_linkdown_port(struct lpfc_vport *vport)
573{
574 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
575
576 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
577
578 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
579 "Link Down: state:x%x rtry:x%x flg:x%x",
580 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
581
582 lpfc_port_link_failure(vport);
583
584}
585
dea31012005-04-17 16:05:31 -0500586int
James Smart685f0bf2007-04-25 09:53:08 -0400587lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500588{
James Smart2e0fef82007-06-17 19:56:36 -0500589 struct lpfc_vport *vport = phba->pport;
590 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400591 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400592 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400593 int i;
dea31012005-04-17 16:05:31 -0500594
James Smart2e0fef82007-06-17 19:56:36 -0500595 if (phba->link_state == LPFC_LINK_DOWN) {
596 return 0;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -0500597 }
James Smart2e0fef82007-06-17 19:56:36 -0500598 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500599 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500600 phba->link_state = LPFC_LINK_DOWN;
James Smart92d7f7b2007-06-17 19:56:38 -0500601 phba->pport->fc_flag &= ~FC_LBIT;
602 }
James Smart2e0fef82007-06-17 19:56:36 -0500603 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -0400604 vports = lpfc_create_vport_work_array(phba);
605 if (vports != NULL)
606 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
607 /* Issue a LINK DOWN event to all nodes */
608 lpfc_linkdown_port(vports[i]);
609 }
610 lpfc_destroy_vport_work_array(vports);
dea31012005-04-17 16:05:31 -0500611 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500612 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
613 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500614 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500615 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500616 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400617 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500618 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500619 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500620 }
621 }
622
dea31012005-04-17 16:05:31 -0500623 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500624 if (phba->pport->fc_flag & FC_PT2PT) {
625 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500626 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
627 if (mb) {
dea31012005-04-17 16:05:31 -0500628 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500629 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500630 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400631 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500632 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500633 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500634 }
635 }
James Smart2e0fef82007-06-17 19:56:36 -0500636 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500637 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500638 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500639 }
James Smart2e0fef82007-06-17 19:56:36 -0500640
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500641 return 0;
dea31012005-04-17 16:05:31 -0500642}
643
James Smart92d7f7b2007-06-17 19:56:38 -0500644static void
645lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500646{
James Smart92d7f7b2007-06-17 19:56:38 -0500647 struct lpfc_nodelist *ndlp;
648
649 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
650 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
651 continue;
652
653 if (ndlp->nlp_type & NLP_FABRIC) {
654 /* On Linkup its safe to clean up the ndlp
655 * from Fabric connections.
656 */
657 if (ndlp->nlp_DID != Fabric_DID)
658 lpfc_unreg_rpi(vport, ndlp);
659 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
660 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
661 /* Fail outstanding IO now since device is
662 * marked for PLOGI.
663 */
664 lpfc_unreg_rpi(vport, ndlp);
665 }
666 }
667}
668
669static void
670lpfc_linkup_port(struct lpfc_vport *vport)
671{
672 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500673 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -0500674 struct lpfc_hba *phba = vport->phba;
675
676 if ((vport->load_flag & FC_UNLOADING) != 0)
677 return;
678
James Smart858c9f62007-06-17 19:56:39 -0500679 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
680 "Link Up: top:x%x speed:x%x flg:x%x",
681 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
682
James Smart92d7f7b2007-06-17 19:56:38 -0500683 /* If NPIV is not enabled, only bring the physical port up */
684 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
685 (vport != phba->pport))
686 return;
dea31012005-04-17 16:05:31 -0500687
James Smart2e0fef82007-06-17 19:56:36 -0500688 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400689
James Smart2e0fef82007-06-17 19:56:36 -0500690 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500691 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
692 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
693 vport->fc_flag |= FC_NDISC_ACTIVE;
694 vport->fc_ns_retry = 0;
695 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500696
James Smart92d7f7b2007-06-17 19:56:38 -0500697 if (vport->fc_flag & FC_LBIT)
698 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500699
James Smart92d7f7b2007-06-17 19:56:38 -0500700 /* free any ndlp's in unused state */
James Smart2e0fef82007-06-17 19:56:36 -0500701 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart92d7f7b2007-06-17 19:56:38 -0500702 nlp_listp)
James Smart685f0bf2007-04-25 09:53:08 -0400703 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
James Smart2e0fef82007-06-17 19:56:36 -0500704 lpfc_drop_node(vport, ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500705}
706
707static int
708lpfc_linkup(struct lpfc_hba *phba)
709{
James Smart549e55c2007-08-02 11:09:51 -0400710 struct lpfc_vport **vports;
711 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500712
713 phba->link_state = LPFC_LINK_UP;
714
715 /* Unblock fabric iocbs if they are blocked */
716 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
717 del_timer_sync(&phba->fabric_block_timer);
718
James Smart549e55c2007-08-02 11:09:51 -0400719 vports = lpfc_create_vport_work_array(phba);
720 if (vports != NULL)
721 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++)
722 lpfc_linkup_port(vports[i]);
723 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500724 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
725 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500726
727 return 0;
728}
729
730/*
731 * This routine handles processing a CLEAR_LA mailbox
732 * command upon completion. It is setup in the LPFC_MBOXQ
733 * as the completion routine when the command is
734 * handed off to the SLI layer.
735 */
736void
James Smart2e0fef82007-06-17 19:56:36 -0500737lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500738{
James Smart2e0fef82007-06-17 19:56:36 -0500739 struct lpfc_vport *vport = pmb->vport;
740 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
741 struct lpfc_sli *psli = &phba->sli;
742 MAILBOX_t *mb = &pmb->mb;
dea31012005-04-17 16:05:31 -0500743 uint32_t control;
744
dea31012005-04-17 16:05:31 -0500745 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -0500746 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500747 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
748 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
749
750 /* Check for error */
751 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500752 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400753 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
754 "0320 CLEAR_LA mbxStatus error x%x hba "
755 "state x%x\n",
756 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -0500757 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500758 goto out;
759 }
760
James Smart92d7f7b2007-06-17 19:56:38 -0500761 if (vport->port_type == LPFC_PHYSICAL_PORT)
762 phba->link_state = LPFC_HBA_READY;
763
764 spin_lock_irq(&phba->hbalock);
765 psli->sli_flag |= LPFC_PROCESS_LA;
766 control = readl(phba->HCregaddr);
767 control |= HC_LAINT_ENA;
768 writel(control, phba->HCregaddr);
769 readl(phba->HCregaddr); /* flush */
770 spin_unlock_irq(&phba->hbalock);
771 return;
dea31012005-04-17 16:05:31 -0500772
James Smart2e0fef82007-06-17 19:56:36 -0500773 vport->num_disc_nodes = 0;
774 /* go thru NPR nodes and issue ELS PLOGIs */
775 if (vport->fc_npr_cnt)
776 lpfc_els_disc_plogi(vport);
777
778 if (!vport->num_disc_nodes) {
779 spin_lock_irq(shost->host_lock);
780 vport->fc_flag &= ~FC_NDISC_ACTIVE;
781 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500782 }
783
James Smart2e0fef82007-06-17 19:56:36 -0500784 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -0500785
786out:
787 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -0400788 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
789 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -0500790 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500791
James Smart2e0fef82007-06-17 19:56:36 -0500792 spin_lock_irq(shost->host_lock);
793 vport->fc_flag &= ~(FC_ABORT_DISCOVERY | FC_ESTABLISH_LINK);
794 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500795
796 del_timer_sync(&phba->fc_estabtmo);
797
James Smart2e0fef82007-06-17 19:56:36 -0500798 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -0500799
800 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500801
802 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500803 psli->sli_flag |= LPFC_PROCESS_LA;
804 control = readl(phba->HCregaddr);
805 control |= HC_LAINT_ENA;
806 writel(control, phba->HCregaddr);
807 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500808 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500809
810 return;
811}
812
James Smart2e0fef82007-06-17 19:56:36 -0500813
dea31012005-04-17 16:05:31 -0500814static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500815lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500816{
James Smart2e0fef82007-06-17 19:56:36 -0500817 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500818
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500819 if (pmb->mb.mbxStatus)
dea31012005-04-17 16:05:31 -0500820 goto out;
dea31012005-04-17 16:05:31 -0500821
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500822 mempool_free(pmb, phba->mbox_mem_pool);
823
824 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -0500825 vport->fc_flag & FC_PUBLIC_LOOP &&
826 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500827 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -0500828 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500829 * LPFC_LOCAL_CFG_LINK while waiting for FAN
830 */
James Smart2e0fef82007-06-17 19:56:36 -0500831 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500832 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500833 }
dea31012005-04-17 16:05:31 -0500834
James Smart2e0fef82007-06-17 19:56:36 -0500835 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500836 * LPFC_FLOGI while waiting for FLOGI cmpl
837 */
James Smart92d7f7b2007-06-17 19:56:38 -0500838 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -0500839 lpfc_initial_flogi(vport);
840 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500841 return;
dea31012005-04-17 16:05:31 -0500842
843out:
James Smarte8b62012007-08-02 11:10:09 -0400844 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
845 "0306 CONFIG_LINK mbxStatus error x%x "
846 "HBA state x%x\n",
847 pmb->mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -0500848 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -0500849
850 lpfc_linkdown(phba);
851
James Smarte8b62012007-08-02 11:10:09 -0400852 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
853 "0200 CONFIG_LINK bad hba state x%x\n",
854 vport->port_state);
dea31012005-04-17 16:05:31 -0500855
James Smart92d7f7b2007-06-17 19:56:38 -0500856 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -0500857 return;
858}
859
860static void
James Smart2e0fef82007-06-17 19:56:36 -0500861lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500862{
dea31012005-04-17 16:05:31 -0500863 MAILBOX_t *mb = &pmb->mb;
864 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500865 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -0500866
867
868 /* Check for error */
869 if (mb->mbxStatus) {
870 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -0400871 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
872 "0319 READ_SPARAM mbxStatus error x%x "
873 "hba state x%x>\n",
874 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -0500875 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -0500876 goto out;
877 }
878
James Smart2e0fef82007-06-17 19:56:36 -0500879 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -0500880 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -0500881 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500882 u64_to_wwn(phba->cfg_soft_wwnn,
883 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400884 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500885 u64_to_wwn(phba->cfg_soft_wwpn,
886 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -0500887 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
888 sizeof(vport->fc_nodename));
889 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
890 sizeof(vport->fc_portname));
891 if (vport->port_type == LPFC_PHYSICAL_PORT) {
892 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
893 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
894 }
895
dea31012005-04-17 16:05:31 -0500896 lpfc_mbuf_free(phba, mp->virt, mp->phys);
897 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -0500898 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500899 return;
900
901out:
902 pmb->context1 = NULL;
903 lpfc_mbuf_free(phba, mp->virt, mp->phys);
904 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -0500905 lpfc_issue_clear_la(phba, vport);
906 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500907 return;
908}
909
910static void
James Smart92d7f7b2007-06-17 19:56:38 -0500911lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -0500912{
James Smart92d7f7b2007-06-17 19:56:38 -0500913 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500914 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
James Smart2e0fef82007-06-17 19:56:36 -0500915 int i;
James Smart14691152006-12-02 13:34:28 -0500916 struct lpfc_dmabuf *mp;
917 int rc;
918
dea31012005-04-17 16:05:31 -0500919 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
920 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
921
James Smart92d7f7b2007-06-17 19:56:38 -0500922 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500923 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -0500924 case LA_1GHZ_LINK:
925 phba->fc_linkspeed = LA_1GHZ_LINK;
926 break;
927 case LA_2GHZ_LINK:
928 phba->fc_linkspeed = LA_2GHZ_LINK;
929 break;
930 case LA_4GHZ_LINK:
931 phba->fc_linkspeed = LA_4GHZ_LINK;
932 break;
933 case LA_8GHZ_LINK:
934 phba->fc_linkspeed = LA_8GHZ_LINK;
935 break;
936 default:
937 phba->fc_linkspeed = LA_UNKNW_LINK;
938 break;
dea31012005-04-17 16:05:31 -0500939 }
940
941 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -0500942 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -0500943
944 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -0500945 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -0500946
James Smart92d7f7b2007-06-17 19:56:38 -0500947 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -0500948 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -0500949 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500950
James Smart2e0fef82007-06-17 19:56:36 -0500951 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -0500952 i = la->un.lilpBde64.tus.f.bdeSize;
953
954 if (i == 0) {
955 phba->alpa_map[0] = 0;
956 } else {
James Smarte8b62012007-08-02 11:10:09 -0400957 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -0500958 int numalpa, j, k;
959 union {
960 uint8_t pamap[16];
961 struct {
962 uint32_t wd1;
963 uint32_t wd2;
964 uint32_t wd3;
965 uint32_t wd4;
966 } pa;
967 } un;
968 numalpa = phba->alpa_map[0];
969 j = 0;
970 while (j < numalpa) {
971 memset(un.pamap, 0, 16);
972 for (k = 1; j < numalpa; k++) {
973 un.pamap[k - 1] =
974 phba->alpa_map[j + 1];
975 j++;
976 if (k == 16)
977 break;
978 }
979 /* Link Up Event ALPA map */
980 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -0500981 KERN_WARNING,
982 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400983 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -0500984 "ALPA map Data: x%x "
985 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500986 un.pa.wd1, un.pa.wd2,
987 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -0500988 }
989 }
990 }
991 } else {
James Smart92d7f7b2007-06-17 19:56:38 -0500992 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -0400993 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -0500994 (phba->sli_rev == 3))
995 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
996 }
James Smart2e0fef82007-06-17 19:56:36 -0500997 vport->fc_myDID = phba->fc_pref_DID;
998 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -0500999 }
James Smart92d7f7b2007-06-17 19:56:38 -05001000 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001001
1002 lpfc_linkup(phba);
1003 if (sparam_mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001004 lpfc_read_sparam(phba, sparam_mbox, 0);
James Smart2e0fef82007-06-17 19:56:36 -05001005 sparam_mbox->vport = vport;
dea31012005-04-17 16:05:31 -05001006 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
James Smart0b727fe2007-10-27 13:37:25 -04001007 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
James Smart14691152006-12-02 13:34:28 -05001008 if (rc == MBX_NOT_FINISHED) {
1009 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1010 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1011 kfree(mp);
1012 mempool_free(sparam_mbox, phba->mbox_mem_pool);
1013 if (cfglink_mbox)
1014 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001015 goto out;
James Smart14691152006-12-02 13:34:28 -05001016 }
dea31012005-04-17 16:05:31 -05001017 }
1018
1019 if (cfglink_mbox) {
James Smart2e0fef82007-06-17 19:56:36 -05001020 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05001021 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05001022 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001023 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04001024 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001025 if (rc != MBX_NOT_FINISHED)
1026 return;
1027 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001028 }
James Smart92d7f7b2007-06-17 19:56:38 -05001029out:
1030 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001031 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1032 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1033 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05001034 lpfc_issue_clear_la(phba, vport);
1035 return;
dea31012005-04-17 16:05:31 -05001036}
1037
1038static void
James Smart2e0fef82007-06-17 19:56:36 -05001039lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1040{
dea31012005-04-17 16:05:31 -05001041 uint32_t control;
1042 struct lpfc_sli *psli = &phba->sli;
1043
1044 lpfc_linkdown(phba);
1045
1046 /* turn on Link Attention interrupts - no CLEAR_LA needed */
James Smart2e0fef82007-06-17 19:56:36 -05001047 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001048 psli->sli_flag |= LPFC_PROCESS_LA;
1049 control = readl(phba->HCregaddr);
1050 control |= HC_LAINT_ENA;
1051 writel(control, phba->HCregaddr);
1052 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001053 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001054}
1055
1056/*
1057 * This routine handles processing a READ_LA mailbox
1058 * command upon completion. It is setup in the LPFC_MBOXQ
1059 * as the completion routine when the command is
1060 * handed off to the SLI layer.
1061 */
1062void
James Smart2e0fef82007-06-17 19:56:36 -05001063lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001064{
James Smart2e0fef82007-06-17 19:56:36 -05001065 struct lpfc_vport *vport = pmb->vport;
1066 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05001067 READ_LA_VAR *la;
1068 MAILBOX_t *mb = &pmb->mb;
1069 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1070
1071 /* Check for error */
1072 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05001073 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001074 "1307 READ_LA mbox error x%x state x%x\n",
1075 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05001076 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001077 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001078 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1079 }
1080
1081 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1082
1083 memcpy(&phba->alpa_map[0], mp->virt, 128);
1084
James Smart2e0fef82007-06-17 19:56:36 -05001085 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001086 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05001087 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001088 else
James Smart2e0fef82007-06-17 19:56:36 -05001089 vport->fc_flag &= ~FC_BYPASSED_MODE;
1090 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001091
dea31012005-04-17 16:05:31 -05001092 if (((phba->fc_eventTag + 1) < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05001093 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05001094 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05001095 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05001096 if (phba->fc_eventTag != 0)
1097 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001098 }
dea31012005-04-17 16:05:31 -05001099
1100 phba->fc_eventTag = la->eventTag;
1101
1102 if (la->attType == AT_LINK_UP) {
1103 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05001104 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart5b8bd0c2007-04-25 09:52:49 -04001105 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001106 "1306 Link Up Event in loop back mode "
1107 "x%x received Data: x%x x%x x%x x%x\n",
1108 la->eventTag, phba->fc_eventTag,
1109 la->granted_AL_PA, la->UlnkSpeed,
1110 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001111 } else {
1112 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001113 "1303 Link Up Event x%x received "
1114 "Data: x%x x%x x%x x%x\n",
1115 la->eventTag, phba->fc_eventTag,
1116 la->granted_AL_PA, la->UlnkSpeed,
1117 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04001118 }
James Smart92d7f7b2007-06-17 19:56:38 -05001119 lpfc_mbx_process_link_up(phba, la);
dea31012005-04-17 16:05:31 -05001120 } else {
1121 phba->fc_stat.LinkDown++;
1122 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001123 "1305 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05001124 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001125 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05001126 phba->pport->port_state, vport->fc_flag);
dea31012005-04-17 16:05:31 -05001127 lpfc_mbx_issue_link_down(phba);
1128 }
1129
1130lpfc_mbx_cmpl_read_la_free_mbuf:
1131 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1132 kfree(mp);
1133 mempool_free(pmb, phba->mbox_mem_pool);
1134 return;
1135}
1136
1137/*
1138 * This routine handles processing a REG_LOGIN mailbox
1139 * command upon completion. It is setup in the LPFC_MBOXQ
1140 * as the completion routine when the command is
1141 * handed off to the SLI layer.
1142 */
1143void
James Smart2e0fef82007-06-17 19:56:36 -05001144lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001145{
James Smart2e0fef82007-06-17 19:56:36 -05001146 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001147 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05001148 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea31012005-04-17 16:05:31 -05001149
dea31012005-04-17 16:05:31 -05001150 pmb->context1 = NULL;
1151
1152 /* Good status, call state machine */
James Smart2e0fef82007-06-17 19:56:36 -05001153 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea31012005-04-17 16:05:31 -05001154 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1155 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001156 mempool_free(pmb, phba->mbox_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04001157 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001158
1159 return;
1160}
1161
James Smart92d7f7b2007-06-17 19:56:38 -05001162static void
1163lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1164{
1165 MAILBOX_t *mb = &pmb->mb;
1166 struct lpfc_vport *vport = pmb->vport;
1167 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1168
1169 switch (mb->mbxStatus) {
1170 case 0x0011:
1171 case 0x0020:
1172 case 0x9700:
James Smarte8b62012007-08-02 11:10:09 -04001173 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1174 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
1175 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001176 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001177 }
1178 vport->unreg_vpi_cmpl = VPORT_OK;
1179 mempool_free(pmb, phba->mbox_mem_pool);
1180 /*
1181 * This shost reference might have been taken at the beginning of
1182 * lpfc_vport_delete()
1183 */
1184 if (vport->load_flag & FC_UNLOADING)
1185 scsi_host_put(shost);
1186}
1187
1188void
1189lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1190{
1191 struct lpfc_hba *phba = vport->phba;
1192 LPFC_MBOXQ_t *mbox;
1193 int rc;
1194
1195 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1196 if (!mbox)
1197 return;
1198
1199 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1200 mbox->vport = vport;
1201 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04001202 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001203 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001204 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1205 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001206 mempool_free(mbox, phba->mbox_mem_pool);
1207 vport->unreg_vpi_cmpl = VPORT_ERROR;
1208 }
1209}
1210
1211static void
1212lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1213{
1214 struct lpfc_vport *vport = pmb->vport;
1215 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1216 MAILBOX_t *mb = &pmb->mb;
1217
1218 switch (mb->mbxStatus) {
1219 case 0x0011:
1220 case 0x9601:
1221 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04001222 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1223 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
1224 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05001225 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1226 spin_lock_irq(shost->host_lock);
1227 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1228 spin_unlock_irq(shost->host_lock);
1229 vport->fc_myDID = 0;
1230 goto out;
1231 }
James Smart92d7f7b2007-06-17 19:56:38 -05001232
1233 vport->num_disc_nodes = 0;
1234 /* go thru NPR list and issue ELS PLOGIs */
1235 if (vport->fc_npr_cnt)
1236 lpfc_els_disc_plogi(vport);
1237
1238 if (!vport->num_disc_nodes) {
1239 spin_lock_irq(shost->host_lock);
1240 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1241 spin_unlock_irq(shost->host_lock);
1242 lpfc_can_disctmo(vport);
1243 }
1244 vport->port_state = LPFC_VPORT_READY;
1245
1246out:
1247 mempool_free(pmb, phba->mbox_mem_pool);
1248 return;
1249}
1250
dea31012005-04-17 16:05:31 -05001251/*
1252 * This routine handles processing a Fabric REG_LOGIN mailbox
1253 * command upon completion. It is setup in the LPFC_MBOXQ
1254 * as the completion routine when the command is
1255 * handed off to the SLI layer.
1256 */
1257void
James Smart2e0fef82007-06-17 19:56:36 -05001258lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001259{
James Smart92d7f7b2007-06-17 19:56:38 -05001260 struct lpfc_vport *vport = pmb->vport;
James Smart2e0fef82007-06-17 19:56:36 -05001261 MAILBOX_t *mb = &pmb->mb;
1262 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05001263 struct lpfc_nodelist *ndlp;
James Smart549e55c2007-08-02 11:09:51 -04001264 struct lpfc_vport **vports;
1265 int i;
dea31012005-04-17 16:05:31 -05001266
James Smart549e55c2007-08-02 11:09:51 -04001267 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04001268 pmb->context1 = NULL;
1269 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05001270 if (mb->mbxStatus) {
1271 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1272 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001273 mempool_free(pmb, phba->mbox_mem_pool);
1274 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001275
James Smart92d7f7b2007-06-17 19:56:38 -05001276 if (phba->fc_topology == TOPOLOGY_LOOP) {
1277 /* FLOGI failed, use loop map to make discovery list */
1278 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001279
James Smart92d7f7b2007-06-17 19:56:38 -05001280 /* Start discovery */
1281 lpfc_disc_start(vport);
1282 return;
1283 }
1284
1285 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001286 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1287 "0258 Register Fabric login error: 0x%x\n",
1288 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001289 return;
1290 }
1291
dea31012005-04-17 16:05:31 -05001292 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001293 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001294 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001295
James Smart329f9bc2007-04-25 09:53:01 -04001296 lpfc_nlp_put(ndlp); /* Drop the reference from the mbox */
1297
James Smart2e0fef82007-06-17 19:56:36 -05001298 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart549e55c2007-08-02 11:09:51 -04001299 vports = lpfc_create_vport_work_array(phba);
1300 if (vports != NULL)
1301 for(i = 0;
1302 i < LPFC_MAX_VPORTS && vports[i] != NULL;
1303 i++) {
1304 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1305 continue;
1306 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1307 lpfc_initial_fdisc(vports[i]);
1308 else if (phba->sli3_options &
1309 LPFC_SLI3_NPIV_ENABLED) {
1310 lpfc_vport_set_state(vports[i],
1311 FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -04001312 lpfc_printf_vlog(vport, KERN_ERR,
1313 LOG_ELS,
1314 "0259 No NPIV "
1315 "Fabric support\n");
James Smart549e55c2007-08-02 11:09:51 -04001316 }
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001317 }
James Smart549e55c2007-08-02 11:09:51 -04001318 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001319 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05001320 }
1321
1322 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1323 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04001324 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001325 return;
1326}
1327
1328/*
1329 * This routine handles processing a NameServer REG_LOGIN mailbox
1330 * command upon completion. It is setup in the LPFC_MBOXQ
1331 * as the completion routine when the command is
1332 * handed off to the SLI layer.
1333 */
1334void
James Smart2e0fef82007-06-17 19:56:36 -05001335lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001336{
James Smart2e0fef82007-06-17 19:56:36 -05001337 MAILBOX_t *mb = &pmb->mb;
1338 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1339 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1340 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001341
1342 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05001343out:
James Smart329f9bc2007-04-25 09:53:01 -04001344 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001345 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1346 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04001347 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05001348 lpfc_drop_node(vport, ndlp);
dea31012005-04-17 16:05:31 -05001349
James Smart92d7f7b2007-06-17 19:56:38 -05001350 if (phba->fc_topology == TOPOLOGY_LOOP) {
1351 /*
1352 * RegLogin failed, use loop map to make discovery
1353 * list
1354 */
1355 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05001356
James Smart92d7f7b2007-06-17 19:56:38 -05001357 /* Start discovery */
1358 lpfc_disc_start(vport);
1359 return;
1360 }
1361 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04001362 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1363 "0260 Register NameServer error: 0x%x\n",
1364 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05001365 return;
1366 }
1367
1368 pmb->context1 = NULL;
1369
dea31012005-04-17 16:05:31 -05001370 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05001371 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05001372 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001373
James Smart2e0fef82007-06-17 19:56:36 -05001374 if (vport->port_state < LPFC_VPORT_READY) {
1375 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05001376 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1377 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1378 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1379 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1380 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1381
1382 /* Issue SCR just before NameServer GID_FT Query */
1383 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05001384 }
1385
James Smart2e0fef82007-06-17 19:56:36 -05001386 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05001387 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05001388 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05001389 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05001390 goto out;
dea31012005-04-17 16:05:31 -05001391 }
1392
James Smart329f9bc2007-04-25 09:53:01 -04001393 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001394 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1395 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05001396 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001397
1398 return;
1399}
1400
1401static void
James Smart2e0fef82007-06-17 19:56:36 -05001402lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001403{
James Smart2e0fef82007-06-17 19:56:36 -05001404 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1405 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05001406 struct lpfc_rport_data *rdata;
1407 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05001408 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001409
1410 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001411 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1412 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001413 rport_ids.port_id = ndlp->nlp_DID;
1414 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05001415
James Smart329f9bc2007-04-25 09:53:01 -04001416 /*
1417 * We leave our node pointer in rport->dd_data when we unregister a
1418 * FCP target port. But fc_remote_port_add zeros the space to which
1419 * rport->dd_data points. So, if we're reusing a previously
1420 * registered port, drop the reference that we took the last time we
1421 * registered the port.
1422 */
1423 if (ndlp->rport && ndlp->rport->dd_data &&
James Smart92d7f7b2007-06-17 19:56:38 -05001424 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp) {
James Smart329f9bc2007-04-25 09:53:01 -04001425 lpfc_nlp_put(ndlp);
1426 }
James Smart858c9f62007-06-17 19:56:39 -05001427
1428 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1429 "rport add: did:x%x flg:x%x type x%x",
1430 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1431
James Smart2e0fef82007-06-17 19:56:36 -05001432 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04001433 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05001434 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1435 "Warning: fc_remote_port_add failed\n");
1436 return;
1437 }
1438
1439 /* initialize static port data */
1440 rport->maxframe_size = ndlp->nlp_maxframe;
1441 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05001442 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04001443 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05001444
1445 if (ndlp->nlp_type & NLP_FCP_TARGET)
1446 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1447 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1448 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1449
1450
1451 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1452 fc_remote_port_rolechg(rport, rport_ids.roles);
1453
James Smart071fbd3d2006-04-15 11:53:20 -04001454 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001455 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04001456 ndlp->nlp_sid = rport->scsi_target_id;
1457 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001458 return;
1459}
1460
1461static void
James Smart2e0fef82007-06-17 19:56:36 -05001462lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001463{
1464 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04001465
James Smart858c9f62007-06-17 19:56:39 -05001466 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
1467 "rport delete: did:x%x flg:x%x type x%x",
1468 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1469
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001470 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05001471
1472 return;
1473}
1474
James Smartde0c5b32007-04-25 09:52:27 -04001475static void
James Smart2e0fef82007-06-17 19:56:36 -05001476lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04001477{
James Smart2e0fef82007-06-17 19:56:36 -05001478 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1479
1480 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001481 switch (state) {
1482 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001483 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001484 break;
1485 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001486 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001487 break;
1488 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001489 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001490 break;
1491 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001492 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001493 break;
1494 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05001495 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001496 break;
1497 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001498 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001499 break;
1500 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001501 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001502 break;
1503 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05001504 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04001505 break;
1506 }
James Smart2e0fef82007-06-17 19:56:36 -05001507 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001508}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05001509
James Smartde0c5b32007-04-25 09:52:27 -04001510static void
James Smart2e0fef82007-06-17 19:56:36 -05001511lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04001512 int old_state, int new_state)
1513{
James Smart2e0fef82007-06-17 19:56:36 -05001514 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1515
James Smartde0c5b32007-04-25 09:52:27 -04001516 if (new_state == NLP_STE_UNMAPPED_NODE) {
1517 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1518 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1519 ndlp->nlp_type |= NLP_FC_NODE;
1520 }
1521 if (new_state == NLP_STE_MAPPED_NODE)
1522 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1523 if (new_state == NLP_STE_NPR_NODE)
1524 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05001525
James Smartde0c5b32007-04-25 09:52:27 -04001526 /* Transport interface */
1527 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1528 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001529 vport->phba->nport_event_cnt++;
1530 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001531 }
dea31012005-04-17 16:05:31 -05001532
James Smartde0c5b32007-04-25 09:52:27 -04001533 if (new_state == NLP_STE_MAPPED_NODE ||
1534 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05001535 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05001536 /*
1537 * Tell the fc transport about the port, if we haven't
1538 * already. If we have, and it's a scsi entity, be
1539 * sure to unblock any attached scsi devices
1540 */
1541 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001542 }
James Smart858c9f62007-06-17 19:56:39 -05001543 /*
1544 * if we added to Mapped list, but the remote port
1545 * registration failed or assigned a target id outside
1546 * our presentable range - move the node to the
1547 * Unmapped List
1548 */
James Smartde0c5b32007-04-25 09:52:27 -04001549 if (new_state == NLP_STE_MAPPED_NODE &&
1550 (!ndlp->rport ||
1551 ndlp->rport->scsi_target_id == -1 ||
1552 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05001553 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04001554 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05001555 spin_unlock_irq(shost->host_lock);
1556 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05001557 }
James Smartde0c5b32007-04-25 09:52:27 -04001558}
1559
James Smart685f0bf2007-04-25 09:53:08 -04001560static char *
1561lpfc_nlp_state_name(char *buffer, size_t size, int state)
1562{
1563 static char *states[] = {
1564 [NLP_STE_UNUSED_NODE] = "UNUSED",
1565 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1566 [NLP_STE_ADISC_ISSUE] = "ADISC",
1567 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1568 [NLP_STE_PRLI_ISSUE] = "PRLI",
1569 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1570 [NLP_STE_MAPPED_NODE] = "MAPPED",
1571 [NLP_STE_NPR_NODE] = "NPR",
1572 };
1573
James Smart311464e2007-08-02 11:10:37 -04001574 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04001575 strlcpy(buffer, states[state], size);
1576 else
1577 snprintf(buffer, size, "unknown (%d)", state);
1578 return buffer;
1579}
1580
James Smartde0c5b32007-04-25 09:52:27 -04001581void
James Smart2e0fef82007-06-17 19:56:36 -05001582lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1583 int state)
James Smartde0c5b32007-04-25 09:52:27 -04001584{
James Smart2e0fef82007-06-17 19:56:36 -05001585 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04001586 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04001587 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04001588
James Smarte8b62012007-08-02 11:10:09 -04001589 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1590 "0904 NPort state transition x%06x, %s -> %s\n",
1591 ndlp->nlp_DID,
1592 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1593 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05001594
1595 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1596 "node statechg did:x%x old:%d ste:%d",
1597 ndlp->nlp_DID, old_state, state);
1598
James Smartde0c5b32007-04-25 09:52:27 -04001599 if (old_state == NLP_STE_NPR_NODE &&
1600 (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
1601 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001602 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001603 if (old_state == NLP_STE_UNMAPPED_NODE) {
1604 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1605 ndlp->nlp_type &= ~NLP_FC_NODE;
1606 }
1607
James Smart98c9ea52007-10-27 13:37:33 -04001608 if ((old_state == NLP_STE_UNUSED_NODE) &&
1609 (state != NLP_STE_UNUSED_NODE) &&
1610 (ndlp->nlp_flag & NLP_DELAYED_RM)) {
1611 /* We are using the ndlp after all, so reverse
1612 * the delayed removal of it.
1613 */
1614 ndlp->nlp_flag &= ~NLP_DELAYED_RM;
1615 lpfc_nlp_get(ndlp);
1616 }
1617
James Smart685f0bf2007-04-25 09:53:08 -04001618 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05001619 spin_lock_irq(shost->host_lock);
1620 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1621 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001622 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05001623 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04001624
1625 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05001626 lpfc_nlp_counters(vport, state, 1);
1627 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04001628}
1629
1630void
James Smart2e0fef82007-06-17 19:56:36 -05001631lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001632{
James Smart2e0fef82007-06-17 19:56:36 -05001633 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1634
James Smartde0c5b32007-04-25 09:52:27 -04001635 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
James Smart2e0fef82007-06-17 19:56:36 -05001636 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04001637 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05001638 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1639 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04001640 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05001641 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05001642 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
1643 NLP_STE_UNUSED_NODE);
James Smartde0c5b32007-04-25 09:52:27 -04001644}
1645
1646void
James Smart2e0fef82007-06-17 19:56:36 -05001647lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04001648{
James Smart51ef4c22007-08-02 11:10:31 -04001649 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart98c9ea52007-10-27 13:37:33 -04001650 if (!(ndlp->nlp_flag & NLP_DELAYED_RM))
1651 lpfc_nlp_put(ndlp);
1652 return;
dea31012005-04-17 16:05:31 -05001653}
1654
1655/*
1656 * Start / ReStart rescue timer for Discovery / RSCN handling
1657 */
1658void
James Smart2e0fef82007-06-17 19:56:36 -05001659lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001660{
James Smart2e0fef82007-06-17 19:56:36 -05001661 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1662 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001663 uint32_t tmo;
1664
James Smart2e0fef82007-06-17 19:56:36 -05001665 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05001666 /* For FAN, timeout should be greater then edtov */
1667 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1668 } else {
1669 /* Normal discovery timeout should be > then ELS/CT timeout
1670 * FC spec states we need 3 * ratov for CT requests
1671 */
1672 tmo = ((phba->fc_ratov * 3) + 3);
1673 }
dea31012005-04-17 16:05:31 -05001674
James Smart858c9f62007-06-17 19:56:39 -05001675
1676 if (!timer_pending(&vport->fc_disctmo)) {
1677 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1678 "set disc timer: tmo:x%x state:x%x flg:x%x",
1679 tmo, vport->port_state, vport->fc_flag);
1680 }
1681
James Smart2e0fef82007-06-17 19:56:36 -05001682 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1683 spin_lock_irq(shost->host_lock);
1684 vport->fc_flag |= FC_DISC_TMO;
1685 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001686
1687 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001688 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1689 "0247 Start Discovery Timer state x%x "
1690 "Data: x%x x%lx x%x x%x\n",
1691 vport->port_state, tmo,
1692 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1693 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05001694
1695 return;
1696}
1697
1698/*
1699 * Cancel rescue timer for Discovery / RSCN handling
1700 */
1701int
James Smart2e0fef82007-06-17 19:56:36 -05001702lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001703{
James Smart2e0fef82007-06-17 19:56:36 -05001704 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001705 unsigned long iflags;
1706
James Smart858c9f62007-06-17 19:56:39 -05001707 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1708 "can disc timer: state:x%x rtry:x%x flg:x%x",
1709 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
1710
dea31012005-04-17 16:05:31 -05001711 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05001712 if (vport->fc_flag & FC_DISC_TMO) {
1713 spin_lock_irqsave(shost->host_lock, iflags);
1714 vport->fc_flag &= ~FC_DISC_TMO;
1715 spin_unlock_irqrestore(shost->host_lock, iflags);
1716 del_timer_sync(&vport->fc_disctmo);
1717 spin_lock_irqsave(&vport->work_port_lock, iflags);
1718 vport->work_port_events &= ~WORKER_DISC_TMO;
1719 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05001720 }
1721
1722 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001723 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1724 "0248 Cancel Discovery Timer state x%x "
1725 "Data: x%x x%x x%x\n",
1726 vport->port_state, vport->fc_flag,
1727 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001728 return 0;
dea31012005-04-17 16:05:31 -05001729}
1730
1731/*
1732 * Check specified ring for outstanding IOCB on the SLI queue
1733 * Return true if iocb matches the specified nport
1734 */
1735int
James Smart2e0fef82007-06-17 19:56:36 -05001736lpfc_check_sli_ndlp(struct lpfc_hba *phba,
1737 struct lpfc_sli_ring *pring,
1738 struct lpfc_iocbq *iocb,
1739 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001740{
James Smart2e0fef82007-06-17 19:56:36 -05001741 struct lpfc_sli *psli = &phba->sli;
1742 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05001743 struct lpfc_vport *vport = ndlp->vport;
1744
1745 if (iocb->vport != vport)
1746 return 0;
1747
dea31012005-04-17 16:05:31 -05001748 if (pring->ringno == LPFC_ELS_RING) {
1749 switch (icmd->ulpCommand) {
1750 case CMD_GEN_REQUEST64_CR:
1751 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001752 return 1;
dea31012005-04-17 16:05:31 -05001753 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04001754 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1755 return 1;
dea31012005-04-17 16:05:31 -05001756 case CMD_XMIT_ELS_RSP64_CX:
1757 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001758 return 1;
dea31012005-04-17 16:05:31 -05001759 }
James Smarta4bc3372006-12-02 13:34:16 -05001760 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05001761
1762 } else if (pring->ringno == psli->fcp_ring) {
1763 /* Skip match check if waiting to relogin to FCP target */
1764 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001765 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001766 return 0;
dea31012005-04-17 16:05:31 -05001767 }
1768 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001769 return 1;
dea31012005-04-17 16:05:31 -05001770 }
1771 } else if (pring->ringno == psli->next_ring) {
1772
1773 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001774 return 0;
dea31012005-04-17 16:05:31 -05001775}
1776
1777/*
1778 * Free resources / clean up outstanding I/Os
1779 * associated with nlp_rpi in the LPFC_NODELIST entry.
1780 */
1781static int
James Smart2e0fef82007-06-17 19:56:36 -05001782lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001783{
James Smart2534ba72007-04-25 09:52:20 -04001784 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001785 struct lpfc_sli *psli;
1786 struct lpfc_sli_ring *pring;
1787 struct lpfc_iocbq *iocb, *next_iocb;
1788 IOCB_t *icmd;
1789 uint32_t rpi, i;
1790
James Smart92d7f7b2007-06-17 19:56:38 -05001791 lpfc_fabric_abort_nport(ndlp);
1792
dea31012005-04-17 16:05:31 -05001793 /*
1794 * Everything that matches on txcmplq will be returned
1795 * by firmware with a no rpi error.
1796 */
1797 psli = &phba->sli;
1798 rpi = ndlp->nlp_rpi;
1799 if (rpi) {
1800 /* Now process each ring */
1801 for (i = 0; i < psli->num_rings; i++) {
1802 pring = &psli->ring[i];
1803
James Smart2e0fef82007-06-17 19:56:36 -05001804 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001805 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05001806 list) {
dea31012005-04-17 16:05:31 -05001807 /*
1808 * Check to see if iocb matches the nport we are
1809 * looking for
1810 */
James Smart92d7f7b2007-06-17 19:56:38 -05001811 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
1812 ndlp))) {
dea31012005-04-17 16:05:31 -05001813 /* It matches, so deque and call compl
1814 with an error */
James Smart2534ba72007-04-25 09:52:20 -04001815 list_move_tail(&iocb->list,
1816 &completions);
dea31012005-04-17 16:05:31 -05001817 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05001818 }
1819 }
James Smart2e0fef82007-06-17 19:56:36 -05001820 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001821 }
1822 }
James Smart2534ba72007-04-25 09:52:20 -04001823
1824 while (!list_empty(&completions)) {
1825 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05001826 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04001827
James Smart2e0fef82007-06-17 19:56:36 -05001828 if (!iocb->iocb_cmpl)
1829 lpfc_sli_release_iocbq(phba, iocb);
1830 else {
James Smart2534ba72007-04-25 09:52:20 -04001831 icmd = &iocb->iocb;
1832 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1833 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
James Smart2e0fef82007-06-17 19:56:36 -05001834 (iocb->iocb_cmpl)(phba, iocb, iocb);
1835 }
James Smart2534ba72007-04-25 09:52:20 -04001836 }
1837
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001838 return 0;
dea31012005-04-17 16:05:31 -05001839}
1840
1841/*
1842 * Free rpi associated with LPFC_NODELIST entry.
1843 * This routine is called from lpfc_freenode(), when we are removing
1844 * a LPFC_NODELIST entry. It is also called if the driver initiates a
1845 * LOGO that completes successfully, and we are waiting to PLOGI back
1846 * to the remote NPort. In addition, it is called after we receive
1847 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1848 * we are waiting to PLOGI back to the remote NPort.
1849 */
1850int
James Smart2e0fef82007-06-17 19:56:36 -05001851lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001852{
James Smart2e0fef82007-06-17 19:56:36 -05001853 struct lpfc_hba *phba = vport->phba;
1854 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05001855 int rc;
1856
1857 if (ndlp->nlp_rpi) {
James Smart2e0fef82007-06-17 19:56:36 -05001858 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1859 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05001860 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05001861 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05001862 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04001863 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05001864 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05001865 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001866 }
dea31012005-04-17 16:05:31 -05001867 lpfc_no_rpi(phba, ndlp);
1868 ndlp->nlp_rpi = 0;
1869 return 1;
1870 }
1871 return 0;
1872}
1873
James Smart92d7f7b2007-06-17 19:56:38 -05001874void
1875lpfc_unreg_all_rpis(struct lpfc_vport *vport)
1876{
1877 struct lpfc_hba *phba = vport->phba;
1878 LPFC_MBOXQ_t *mbox;
1879 int rc;
1880
1881 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1882 if (mbox) {
1883 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
1884 mbox->vport = vport;
1885 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04001886 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001887 if (rc == MBX_NOT_FINISHED) {
1888 mempool_free(mbox, phba->mbox_mem_pool);
1889 }
1890 }
1891}
1892
1893void
1894lpfc_unreg_default_rpis(struct lpfc_vport *vport)
1895{
1896 struct lpfc_hba *phba = vport->phba;
1897 LPFC_MBOXQ_t *mbox;
1898 int rc;
1899
1900 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1901 if (mbox) {
1902 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
1903 mbox->vport = vport;
1904 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04001905 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05001906 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04001907 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1908 "1815 Could not issue "
1909 "unreg_did (default rpis)\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001910 mempool_free(mbox, phba->mbox_mem_pool);
1911 }
1912 }
1913}
1914
dea31012005-04-17 16:05:31 -05001915/*
1916 * Free resources associated with LPFC_NODELIST entry
1917 * so it can be freed.
1918 */
1919static int
James Smart2e0fef82007-06-17 19:56:36 -05001920lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001921{
James Smart2e0fef82007-06-17 19:56:36 -05001922 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1923 struct lpfc_hba *phba = vport->phba;
1924 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05001925 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05001926
1927 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04001928 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1929 "0900 Cleanup node for NPort x%x "
1930 "Data: x%x x%x x%x\n",
1931 ndlp->nlp_DID, ndlp->nlp_flag,
1932 ndlp->nlp_state, ndlp->nlp_rpi);
James Smart2e0fef82007-06-17 19:56:36 -05001933 lpfc_dequeue_node(vport, ndlp);
dea31012005-04-17 16:05:31 -05001934
dea31012005-04-17 16:05:31 -05001935 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1936 if ((mb = phba->sli.mbox_active)) {
1937 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1938 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1939 mb->context2 = NULL;
1940 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1941 }
1942 }
James Smart33ccf8d2006-08-17 11:57:58 -04001943
James Smart2e0fef82007-06-17 19:56:36 -05001944 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001945 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1946 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05001947 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05001948 mp = (struct lpfc_dmabuf *) (mb->context1);
1949 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05001950 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001951 kfree(mp);
1952 }
1953 list_del(&mb->list);
1954 mempool_free(mb, phba->mbox_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04001955 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001956 }
1957 }
James Smart2e0fef82007-06-17 19:56:36 -05001958 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001959
James Smart07951072007-04-25 09:51:38 -04001960 lpfc_els_abort(phba,ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05001961 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04001962 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05001963 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001964
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05001965 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05001966 del_timer_sync(&ndlp->nlp_delayfunc);
1967
dea31012005-04-17 16:05:31 -05001968 if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1969 list_del_init(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05001970 if (!list_empty(&ndlp->dev_loss_evt.evt_listp))
1971 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05001972
James Smart2e0fef82007-06-17 19:56:36 -05001973 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05001974
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05001975 return 0;
dea31012005-04-17 16:05:31 -05001976}
1977
1978/*
1979 * Check to see if we can free the nlp back to the freelist.
1980 * If we are in the middle of using the nlp in the discovery state
1981 * machine, defer the free till we reach the end of the state machine.
1982 */
James Smart329f9bc2007-04-25 09:53:01 -04001983static void
James Smart2e0fef82007-06-17 19:56:36 -05001984lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001985{
James Smart1dcb58e2007-04-25 09:51:30 -04001986 struct lpfc_rport_data *rdata;
dea31012005-04-17 16:05:31 -05001987
1988 if (ndlp->nlp_flag & NLP_DELAY_TMO) {
James Smart2e0fef82007-06-17 19:56:36 -05001989 lpfc_cancel_retry_delay_tmo(vport, ndlp);
dea31012005-04-17 16:05:31 -05001990 }
1991
James Smart2e0fef82007-06-17 19:56:36 -05001992 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04001993
James Smart2e0fef82007-06-17 19:56:36 -05001994 /*
James Smart92d7f7b2007-06-17 19:56:38 -05001995 * We can get here with a non-NULL ndlp->rport because when we
1996 * unregister a rport we don't break the rport/node linkage. So if we
1997 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05001998 */
James Smart92d7f7b2007-06-17 19:56:38 -05001999 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04002000 rdata = ndlp->rport->dd_data;
2001 rdata->pnode = NULL;
2002 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05002003 }
dea31012005-04-17 16:05:31 -05002004}
2005
2006static int
James Smart2e0fef82007-06-17 19:56:36 -05002007lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2008 uint32_t did)
dea31012005-04-17 16:05:31 -05002009{
James Smart2e0fef82007-06-17 19:56:36 -05002010 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05002011
2012 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002013 return 0;
dea31012005-04-17 16:05:31 -05002014
2015 if (ndlp->nlp_DID == 0) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002016 return 0;
dea31012005-04-17 16:05:31 -05002017 }
2018
2019 /* First check for Direct match */
2020 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002021 return 1;
dea31012005-04-17 16:05:31 -05002022
2023 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05002024 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05002025 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002026 return 0;
dea31012005-04-17 16:05:31 -05002027 }
2028
2029 matchdid.un.word = did;
2030 ndlpdid.un.word = ndlp->nlp_DID;
2031 if (matchdid.un.b.id == ndlpdid.un.b.id) {
2032 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
2033 (mydid.un.b.area == matchdid.un.b.area)) {
2034 if ((ndlpdid.un.b.domain == 0) &&
2035 (ndlpdid.un.b.area == 0)) {
2036 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002037 return 1;
dea31012005-04-17 16:05:31 -05002038 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002039 return 0;
dea31012005-04-17 16:05:31 -05002040 }
2041
2042 matchdid.un.word = ndlp->nlp_DID;
2043 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
2044 (mydid.un.b.area == ndlpdid.un.b.area)) {
2045 if ((matchdid.un.b.domain == 0) &&
2046 (matchdid.un.b.area == 0)) {
2047 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002048 return 1;
dea31012005-04-17 16:05:31 -05002049 }
2050 }
2051 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002052 return 0;
dea31012005-04-17 16:05:31 -05002053}
2054
James Smart685f0bf2007-04-25 09:53:08 -04002055/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05002056static struct lpfc_nodelist *
2057__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002058{
James Smart2fb9bd82006-12-02 13:33:57 -05002059 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002060 uint32_t data1;
2061
James Smart2e0fef82007-06-17 19:56:36 -05002062 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2063 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04002064 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2065 ((uint32_t) ndlp->nlp_xri << 16) |
2066 ((uint32_t) ndlp->nlp_type << 8) |
2067 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04002068 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2069 "0929 FIND node DID "
2070 "Data: x%p x%x x%x x%x\n",
2071 ndlp, ndlp->nlp_DID,
2072 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04002073 return ndlp;
dea31012005-04-17 16:05:31 -05002074 }
2075 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05002076
dea31012005-04-17 16:05:31 -05002077 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04002078 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2079 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05002080 return NULL;
2081}
2082
2083struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002084lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05002085{
James Smart2e0fef82007-06-17 19:56:36 -05002086 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002087 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002088
James Smart2e0fef82007-06-17 19:56:36 -05002089 spin_lock_irq(shost->host_lock);
2090 ndlp = __lpfc_findnode_did(vport, did);
2091 spin_unlock_irq(shost->host_lock);
2092 return ndlp;
2093}
2094
2095struct lpfc_nodelist *
2096lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2097{
2098 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2099 struct lpfc_nodelist *ndlp;
2100
2101 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002102 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05002103 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2104 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05002105 return NULL;
2106 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05002107 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002108 if (!ndlp)
2109 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002110 lpfc_nlp_init(vport, ndlp, did);
2111 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2112 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002113 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002114 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002115 return ndlp;
2116 }
James Smart2e0fef82007-06-17 19:56:36 -05002117 if (vport->fc_flag & FC_RSCN_MODE) {
2118 if (lpfc_rscn_payload_check(vport, did)) {
2119 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002120 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002121 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002122
2123 /* Since this node is marked for discovery,
2124 * delay timeout is not needed.
2125 */
James Smartfdcebe22006-03-07 15:04:01 -05002126 if (ndlp->nlp_flag & NLP_DELAY_TMO)
James Smart2e0fef82007-06-17 19:56:36 -05002127 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart071fbd3d2006-04-15 11:53:20 -04002128 } else
dea31012005-04-17 16:05:31 -05002129 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002130 } else {
James Smart685f0bf2007-04-25 09:53:08 -04002131 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
2132 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE)
dea31012005-04-17 16:05:31 -05002133 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002134 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2135 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002136 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05002137 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002138 }
2139 return ndlp;
2140}
2141
2142/* Build a list of nodes to discover based on the loopmap */
2143void
James Smart2e0fef82007-06-17 19:56:36 -05002144lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002145{
James Smart2e0fef82007-06-17 19:56:36 -05002146 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002147 int j;
2148 uint32_t alpa, index;
2149
James Smart2e0fef82007-06-17 19:56:36 -05002150 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002151 return;
James Smart2e0fef82007-06-17 19:56:36 -05002152
2153 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05002154 return;
dea31012005-04-17 16:05:31 -05002155
2156 /* Check for loop map present or not */
2157 if (phba->alpa_map[0]) {
2158 for (j = 1; j <= phba->alpa_map[0]; j++) {
2159 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05002160 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05002161 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002162 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002163 }
2164 } else {
2165 /* No alpamap, so try all alpa's */
2166 for (j = 0; j < FC_MAXLOOP; j++) {
2167 /* If cfg_scan_down is set, start from highest
2168 * ALPA (0xef) to lowest (0x1).
2169 */
James Smart3de2a652007-08-02 11:09:59 -04002170 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05002171 index = j;
2172 else
2173 index = FC_MAXLOOP - j - 1;
2174 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05002175 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05002176 continue;
James Smart2e0fef82007-06-17 19:56:36 -05002177 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05002178 }
2179 }
2180 return;
2181}
2182
dea31012005-04-17 16:05:31 -05002183void
James Smart2e0fef82007-06-17 19:56:36 -05002184lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002185{
dea31012005-04-17 16:05:31 -05002186 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05002187 struct lpfc_sli *psli = &phba->sli;
2188 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2189 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2190 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2191 int rc;
2192
James Smart92d7f7b2007-06-17 19:56:38 -05002193 /*
2194 * if it's not a physical port or if we already send
2195 * clear_la then don't send it.
2196 */
2197 if ((phba->link_state >= LPFC_CLEAR_LA) ||
2198 (vport->port_type != LPFC_PHYSICAL_PORT))
2199 return;
2200
James Smart2e0fef82007-06-17 19:56:36 -05002201 /* Link up discovery */
2202 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2203 phba->link_state = LPFC_CLEAR_LA;
2204 lpfc_clear_la(phba, mbox);
2205 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2206 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002207 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05002208 if (rc == MBX_NOT_FINISHED) {
2209 mempool_free(mbox, phba->mbox_mem_pool);
2210 lpfc_disc_flush_list(vport);
2211 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2212 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2213 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05002214 phba->link_state = LPFC_HBA_ERROR;
2215 }
2216 }
2217}
2218
2219/* Reg_vpi to tell firmware to resume normal operations */
2220void
2221lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2222{
2223 LPFC_MBOXQ_t *regvpimbox;
2224
2225 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2226 if (regvpimbox) {
2227 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
2228 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2229 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04002230 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05002231 == MBX_NOT_FINISHED) {
2232 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002233 }
2234 }
2235}
2236
2237/* Start Link up / RSCN discovery on NPR nodes */
2238void
2239lpfc_disc_start(struct lpfc_vport *vport)
2240{
2241 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2242 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04002243 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05002244 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04002245 int did_changed;
dea31012005-04-17 16:05:31 -05002246
James Smart2e0fef82007-06-17 19:56:36 -05002247 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05002248 return;
James Smart2e0fef82007-06-17 19:56:36 -05002249
2250 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05002251 clear_la_pending = 1;
2252 else
2253 clear_la_pending = 0;
2254
James Smart2e0fef82007-06-17 19:56:36 -05002255 if (vport->port_state < LPFC_VPORT_READY)
2256 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05002257
James Smart2e0fef82007-06-17 19:56:36 -05002258 lpfc_set_disctmo(vport);
2259
2260 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05002261 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002262 else
dea31012005-04-17 16:05:31 -05002263 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002264
2265 vport->fc_prevDID = vport->fc_myDID;
2266 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05002267
2268 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002269 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2270 "0202 Start Discovery hba state x%x "
2271 "Data: x%x x%x x%x\n",
2272 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
2273 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05002274
2275 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002276 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05002277
2278 if (num_sent)
2279 return;
2280
James Smart92d7f7b2007-06-17 19:56:38 -05002281 /*
2282 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2283 * continue discovery.
2284 */
2285 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2286 !(vport->fc_flag & FC_RSCN_MODE)) {
2287 lpfc_issue_reg_vpi(phba, vport);
2288 return;
2289 }
James Smart2e0fef82007-06-17 19:56:36 -05002290
James Smart92d7f7b2007-06-17 19:56:38 -05002291 /*
2292 * For SLI2, we need to set port_state to READY and continue
2293 * discovery.
2294 */
2295 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2296 /* If we get here, there is nothing to ADISC */
2297 if (vport->port_type == LPFC_PHYSICAL_PORT)
2298 lpfc_issue_clear_la(phba, vport);
2299
2300 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05002301 vport->num_disc_nodes = 0;
2302 /* go thru NPR nodes and issue ELS PLOGIs */
2303 if (vport->fc_npr_cnt)
2304 lpfc_els_disc_plogi(vport);
2305
2306 if (!vport->num_disc_nodes) {
2307 spin_lock_irq(shost->host_lock);
2308 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2309 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002310 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05002311 }
2312 }
James Smart92d7f7b2007-06-17 19:56:38 -05002313 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002314 } else {
2315 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05002316 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05002317
2318 if (num_sent)
2319 return;
2320
James Smart2e0fef82007-06-17 19:56:36 -05002321 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05002322 /* Check to see if more RSCNs came in while we
2323 * were processing this one.
2324 */
James Smart2e0fef82007-06-17 19:56:36 -05002325 if ((vport->fc_rscn_id_cnt == 0) &&
2326 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2327 spin_lock_irq(shost->host_lock);
2328 vport->fc_flag &= ~FC_RSCN_MODE;
2329 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002330 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002331 } else
James Smart2e0fef82007-06-17 19:56:36 -05002332 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05002333 }
2334 }
2335 return;
2336}
2337
2338/*
2339 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2340 * ring the match the sppecified nodelist.
2341 */
2342static void
James Smart2e0fef82007-06-17 19:56:36 -05002343lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05002344{
James Smart2534ba72007-04-25 09:52:20 -04002345 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002346 struct lpfc_sli *psli;
2347 IOCB_t *icmd;
2348 struct lpfc_iocbq *iocb, *next_iocb;
2349 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002350
2351 psli = &phba->sli;
2352 pring = &psli->ring[LPFC_ELS_RING];
2353
2354 /* Error matching iocb on txq or txcmplq
2355 * First check the txq.
2356 */
James Smart2e0fef82007-06-17 19:56:36 -05002357 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002358 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2359 if (iocb->context1 != ndlp) {
2360 continue;
2361 }
2362 icmd = &iocb->iocb;
2363 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2364 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2365
James Smart2534ba72007-04-25 09:52:20 -04002366 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05002367 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05002368 }
2369 }
2370
2371 /* Next check the txcmplq */
2372 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2373 if (iocb->context1 != ndlp) {
2374 continue;
2375 }
2376 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05002377 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2378 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04002379 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05002380 }
2381 }
James Smart2e0fef82007-06-17 19:56:36 -05002382 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002383
2384 while (!list_empty(&completions)) {
2385 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
James Smart92d7f7b2007-06-17 19:56:38 -05002386 list_del_init(&iocb->list);
James Smart2534ba72007-04-25 09:52:20 -04002387
James Smart2e0fef82007-06-17 19:56:36 -05002388 if (!iocb->iocb_cmpl)
2389 lpfc_sli_release_iocbq(phba, iocb);
2390 else {
James Smart2534ba72007-04-25 09:52:20 -04002391 icmd = &iocb->iocb;
2392 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2393 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2394 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002395 }
James Smart2534ba72007-04-25 09:52:20 -04002396 }
dea31012005-04-17 16:05:31 -05002397}
2398
2399void
James Smart2e0fef82007-06-17 19:56:36 -05002400lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002401{
2402 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05002403 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002404
James Smart2e0fef82007-06-17 19:56:36 -05002405 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2406 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002407 nlp_listp) {
2408 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2409 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2410 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04002411 }
dea31012005-04-17 16:05:31 -05002412 }
2413 }
dea31012005-04-17 16:05:31 -05002414}
2415
James Smart92d7f7b2007-06-17 19:56:38 -05002416void
2417lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2418{
2419 lpfc_els_flush_rscn(vport);
2420 lpfc_els_flush_cmd(vport);
2421 lpfc_disc_flush_list(vport);
2422}
2423
dea31012005-04-17 16:05:31 -05002424/*****************************************************************************/
2425/*
2426 * NAME: lpfc_disc_timeout
2427 *
2428 * FUNCTION: Fibre Channel driver discovery timeout routine.
2429 *
2430 * EXECUTION ENVIRONMENT: interrupt only
2431 *
2432 * CALLED FROM:
2433 * Timer function
2434 *
2435 * RETURNS:
2436 * none
2437 */
2438/*****************************************************************************/
2439void
2440lpfc_disc_timeout(unsigned long ptr)
2441{
James Smart2e0fef82007-06-17 19:56:36 -05002442 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2443 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002444 unsigned long flags = 0;
2445
2446 if (unlikely(!phba))
2447 return;
2448
James Smart2e0fef82007-06-17 19:56:36 -05002449 if ((vport->work_port_events & WORKER_DISC_TMO) == 0) {
2450 spin_lock_irqsave(&vport->work_port_lock, flags);
2451 vport->work_port_events |= WORKER_DISC_TMO;
2452 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2453
James Smart92d7f7b2007-06-17 19:56:38 -05002454 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002455 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05002456 lpfc_worker_wake_up(phba);
2457 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05002458 }
dea31012005-04-17 16:05:31 -05002459 return;
2460}
2461
2462static void
James Smart2e0fef82007-06-17 19:56:36 -05002463lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002464{
James Smart2e0fef82007-06-17 19:56:36 -05002465 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2466 struct lpfc_hba *phba = vport->phba;
2467 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002468 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05002469 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05002470 int rc, clrlaerr = 0;
2471
James Smart2e0fef82007-06-17 19:56:36 -05002472 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05002473 return;
2474
James Smart2e0fef82007-06-17 19:56:36 -05002475 spin_lock_irq(shost->host_lock);
2476 vport->fc_flag &= ~FC_DISC_TMO;
2477 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002478
James Smart858c9f62007-06-17 19:56:39 -05002479 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2480 "disc timeout: state:x%x rtry:x%x flg:x%x",
2481 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2482
James Smart2e0fef82007-06-17 19:56:36 -05002483 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05002484
2485 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05002486 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2487 * FAN
2488 */
2489 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04002490 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
2491 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002492 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05002493 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04002494 nlp_listp) {
2495 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2496 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002497 if (ndlp->nlp_type & NLP_FABRIC) {
2498 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05002499 lpfc_drop_node(vport, ndlp);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002500 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002501 /* Fail outstanding IO now since device
2502 * is marked for PLOGI.
2503 */
James Smart2e0fef82007-06-17 19:56:36 -05002504 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002505 }
2506 }
James Smart92d7f7b2007-06-17 19:56:38 -05002507 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05002508 lpfc_initial_flogi(vport);
2509 }
dea31012005-04-17 16:05:31 -05002510 break;
2511
James Smart92d7f7b2007-06-17 19:56:38 -05002512 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05002513 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05002514 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05002515 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04002516 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2517 "0222 Initial %s timeout\n",
2518 vport->vpi ? "FLOGI" : "FDISC");
dea31012005-04-17 16:05:31 -05002519
2520 /* Assume no Fabric and go on with discovery.
2521 * Check for outstanding ELS FLOGI to abort.
2522 */
2523
2524 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05002525 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05002526
2527 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002528 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002529 break;
2530
2531 case LPFC_FABRIC_CFG_LINK:
2532 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2533 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04002534 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2535 "0223 Timeout while waiting for "
2536 "NameServer login\n");
dea31012005-04-17 16:05:31 -05002537 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05002538 ndlp = lpfc_findnode_did(vport, NameServer_DID);
dea31012005-04-17 16:05:31 -05002539 if (ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04002540 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002541 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05002542 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05002543 break;
2544
2545 case LPFC_NS_QRY:
2546 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04002547 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2548 "0224 NameServer Query timeout "
2549 "Data: x%x x%x\n",
2550 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002551
James Smart92d7f7b2007-06-17 19:56:38 -05002552 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2553 /* Try it one more time */
2554 vport->fc_ns_retry++;
2555 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2556 vport->fc_ns_retry, 0);
2557 if (rc == 0)
2558 break;
dea31012005-04-17 16:05:31 -05002559 }
James Smart92d7f7b2007-06-17 19:56:38 -05002560 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05002561
James Smart92d7f7b2007-06-17 19:56:38 -05002562 /*
2563 * Discovery is over.
2564 * set port_state to PORT_READY if SLI2.
2565 * cmpl_reg_vpi will set port_state to READY for SLI3.
2566 */
2567 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2568 lpfc_issue_reg_vpi(phba, vport);
2569 else { /* NPIV Not enabled */
2570 lpfc_issue_clear_la(phba, vport);
2571 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002572 }
2573
2574 /* Setup and issue mailbox INITIALIZE LINK command */
2575 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2576 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04002577 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2578 "0206 Device Discovery "
2579 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05002580 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002581 break;
2582 }
2583
2584 lpfc_linkdown(phba);
2585 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2586 phba->cfg_link_speed);
2587 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05002588 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002589 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04002590 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04002591 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05002592 if (rc == MBX_NOT_FINISHED)
2593 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2594
2595 break;
2596
2597 case LPFC_DISC_AUTH:
2598 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04002599 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2600 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002601 lpfc_disc_flush_list(vport);
2602
James Smart92d7f7b2007-06-17 19:56:38 -05002603 /*
2604 * set port_state to PORT_READY if SLI2.
2605 * cmpl_reg_vpi will set port_state to READY for SLI3.
2606 */
2607 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2608 lpfc_issue_reg_vpi(phba, vport);
2609 else { /* NPIV Not enabled */
2610 lpfc_issue_clear_la(phba, vport);
2611 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002612 }
2613 break;
2614
James Smart2e0fef82007-06-17 19:56:36 -05002615 case LPFC_VPORT_READY:
2616 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04002617 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2618 "0231 RSCN timeout Data: x%x "
2619 "x%x\n",
2620 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05002621
2622 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05002623 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05002624
James Smart2e0fef82007-06-17 19:56:36 -05002625 lpfc_els_flush_rscn(vport);
2626 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05002627 }
2628 break;
James Smart2e0fef82007-06-17 19:56:36 -05002629
James Smart92d7f7b2007-06-17 19:56:38 -05002630 default:
James Smarte8b62012007-08-02 11:10:09 -04002631 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2632 "0229 Unexpected discovery timeout, "
2633 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05002634 break;
2635 }
2636
2637 switch (phba->link_state) {
2638 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05002639 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04002640 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2641 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05002642 clrlaerr = 1;
2643 break;
2644
2645 case LPFC_LINK_UNKNOWN:
2646 case LPFC_WARM_START:
2647 case LPFC_INIT_START:
2648 case LPFC_INIT_MBX_CMDS:
2649 case LPFC_LINK_DOWN:
2650 case LPFC_LINK_UP:
2651 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04002652 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2653 "0230 Unexpected timeout, hba link "
2654 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05002655 clrlaerr = 1;
2656 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002657
2658 case LPFC_HBA_READY:
2659 break;
dea31012005-04-17 16:05:31 -05002660 }
2661
2662 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05002663 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05002664 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002665 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2666 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05002667 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05002668 }
2669
2670 return;
2671}
2672
dea31012005-04-17 16:05:31 -05002673/*
2674 * This routine handles processing a NameServer REG_LOGIN mailbox
2675 * command upon completion. It is setup in the LPFC_MBOXQ
2676 * as the completion routine when the command is
2677 * handed off to the SLI layer.
2678 */
2679void
James Smart2e0fef82007-06-17 19:56:36 -05002680lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002681{
James Smart2e0fef82007-06-17 19:56:36 -05002682 MAILBOX_t *mb = &pmb->mb;
2683 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2684 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2685 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002686
2687 pmb->context1 = NULL;
2688
dea31012005-04-17 16:05:31 -05002689 ndlp->nlp_rpi = mb->un.varWords[0];
dea31012005-04-17 16:05:31 -05002690 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05002691 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05002692
James Smart2e0fef82007-06-17 19:56:36 -05002693 /*
2694 * Start issuing Fabric-Device Management Interface (FDMI) command to
2695 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
2696 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05002697 */
James Smart2e0fef82007-06-17 19:56:36 -05002698
James Smart3de2a652007-08-02 11:09:59 -04002699 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05002700 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
2701 else
2702 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05002703
James Smart329f9bc2007-04-25 09:53:01 -04002704 /* Mailbox took a reference to the node */
2705 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05002706 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2707 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04002708 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002709
2710 return;
2711}
2712
James Smart685f0bf2007-04-25 09:53:08 -04002713static int
2714lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2715{
2716 uint16_t *rpi = param;
2717
2718 return ndlp->nlp_rpi == *rpi;
2719}
2720
2721static int
2722lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2723{
2724 return memcmp(&ndlp->nlp_portname, param,
2725 sizeof(ndlp->nlp_portname)) == 0;
2726}
2727
dea31012005-04-17 16:05:31 -05002728struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002729__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05002730{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002731 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05002732
James Smart2e0fef82007-06-17 19:56:36 -05002733 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart685f0bf2007-04-25 09:53:08 -04002734 if (ndlp->nlp_state != NLP_STE_UNUSED_NODE &&
2735 filter(ndlp, param))
2736 return ndlp;
2737 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04002738 return NULL;
dea31012005-04-17 16:05:31 -05002739}
2740
James Smart685f0bf2007-04-25 09:53:08 -04002741/*
2742 * Search node lists for a remote port matching filter criteria
James Smart92d7f7b2007-06-17 19:56:38 -05002743 * Caller needs to hold host_lock before calling this routine.
James Smart685f0bf2007-04-25 09:53:08 -04002744 */
2745struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002746lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
James Smart685f0bf2007-04-25 09:53:08 -04002747{
James Smart2e0fef82007-06-17 19:56:36 -05002748 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart685f0bf2007-04-25 09:53:08 -04002749 struct lpfc_nodelist *ndlp;
2750
James Smart2e0fef82007-06-17 19:56:36 -05002751 spin_lock_irq(shost->host_lock);
2752 ndlp = __lpfc_find_node(vport, filter, param);
2753 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04002754 return ndlp;
2755}
2756
2757/*
2758 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05002759 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04002760 */
2761struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002762__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04002763{
James Smart2e0fef82007-06-17 19:56:36 -05002764 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04002765}
2766
James Smart2534ba72007-04-25 09:52:20 -04002767struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002768lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart2534ba72007-04-25 09:52:20 -04002769{
James Smart2e0fef82007-06-17 19:56:36 -05002770 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2534ba72007-04-25 09:52:20 -04002771 struct lpfc_nodelist *ndlp;
2772
James Smart2e0fef82007-06-17 19:56:36 -05002773 spin_lock_irq(shost->host_lock);
2774 ndlp = __lpfc_findnode_rpi(vport, rpi);
2775 spin_unlock_irq(shost->host_lock);
James Smart2534ba72007-04-25 09:52:20 -04002776 return ndlp;
2777}
2778
James Smart488d1462006-03-07 15:02:37 -05002779/*
James Smart685f0bf2007-04-25 09:53:08 -04002780 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05002781 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05002782 */
2783struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05002784lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05002785{
James Smart2e0fef82007-06-17 19:56:36 -05002786 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05002787 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05002788
James Smart2e0fef82007-06-17 19:56:36 -05002789 spin_lock_irq(shost->host_lock);
2790 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
2791 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05002792 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05002793}
2794
dea31012005-04-17 16:05:31 -05002795void
James Smart2e0fef82007-06-17 19:56:36 -05002796lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2797 uint32_t did)
dea31012005-04-17 16:05:31 -05002798{
2799 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -05002800 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
James Smart92d7f7b2007-06-17 19:56:38 -05002801 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05002802 init_timer(&ndlp->nlp_delayfunc);
2803 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2804 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2805 ndlp->nlp_DID = did;
James Smart2e0fef82007-06-17 19:56:36 -05002806 ndlp->vport = vport;
dea31012005-04-17 16:05:31 -05002807 ndlp->nlp_sid = NLP_NO_SID;
James Smart685f0bf2007-04-25 09:53:08 -04002808 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart329f9bc2007-04-25 09:53:01 -04002809 kref_init(&ndlp->kref);
James Smart858c9f62007-06-17 19:56:39 -05002810
2811 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2812 "node init: did:x%x",
2813 ndlp->nlp_DID, 0, 0);
2814
dea31012005-04-17 16:05:31 -05002815 return;
2816}
James Smart329f9bc2007-04-25 09:53:01 -04002817
James Smart98c9ea52007-10-27 13:37:33 -04002818/* This routine releases all resources associated with a specifc NPort's ndlp
2819 * and mempool_free's the nodelist.
2820 */
James Smart311464e2007-08-02 11:10:37 -04002821static void
James Smart329f9bc2007-04-25 09:53:01 -04002822lpfc_nlp_release(struct kref *kref)
2823{
2824 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
2825 kref);
James Smart858c9f62007-06-17 19:56:39 -05002826
2827 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2828 "node release: did:x%x flg:x%x type:x%x",
2829 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2830
James Smart2e0fef82007-06-17 19:56:36 -05002831 lpfc_nlp_remove(ndlp->vport, ndlp);
2832 mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
James Smart329f9bc2007-04-25 09:53:01 -04002833}
2834
James Smart98c9ea52007-10-27 13:37:33 -04002835/* This routine bumps the reference count for a ndlp structure to ensure
2836 * that one discovery thread won't free a ndlp while another discovery thread
2837 * is using it.
2838 */
James Smart329f9bc2007-04-25 09:53:01 -04002839struct lpfc_nodelist *
2840lpfc_nlp_get(struct lpfc_nodelist *ndlp)
2841{
James Smart98c9ea52007-10-27 13:37:33 -04002842 if (ndlp) {
2843 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2844 "node get: did:x%x flg:x%x refcnt:x%x",
2845 ndlp->nlp_DID, ndlp->nlp_flag,
2846 atomic_read(&ndlp->kref.refcount));
James Smart329f9bc2007-04-25 09:53:01 -04002847 kref_get(&ndlp->kref);
James Smart98c9ea52007-10-27 13:37:33 -04002848 }
James Smart329f9bc2007-04-25 09:53:01 -04002849 return ndlp;
2850}
2851
James Smart98c9ea52007-10-27 13:37:33 -04002852
2853/* This routine decrements the reference count for a ndlp structure. If the
2854 * count goes to 0, this indicates the the associated nodelist should be freed.
2855 */
James Smart329f9bc2007-04-25 09:53:01 -04002856int
2857lpfc_nlp_put(struct lpfc_nodelist *ndlp)
2858{
James Smart98c9ea52007-10-27 13:37:33 -04002859 if (ndlp) {
2860 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2861 "node put: did:x%x flg:x%x refcnt:x%x",
2862 ndlp->nlp_DID, ndlp->nlp_flag,
2863 atomic_read(&ndlp->kref.refcount));
2864 }
James Smart329f9bc2007-04-25 09:53:01 -04002865 return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
2866}
James Smart98c9ea52007-10-27 13:37:33 -04002867
2868/* This routine free's the specified nodelist if it is not in use
2869 * by any other discovery thread. This routine returns 1 if the ndlp
2870 * is not being used by anyone and has been freed. A return value of
2871 * 0 indicates it is being used by another discovery thread and the
2872 * refcount is left unchanged.
2873 */
2874int
2875lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
2876{
2877 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
2878 "node not used: did:x%x flg:x%x refcnt:x%x",
2879 ndlp->nlp_DID, ndlp->nlp_flag,
2880 atomic_read(&ndlp->kref.refcount));
2881
2882 if (atomic_read(&ndlp->kref.refcount) == 1) {
2883 lpfc_nlp_put(ndlp);
2884 return 1;
2885 }
2886 return 0;
2887}
2888